1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtGui>
41#include <QtX11Extras/QX11Info>
42
43class IntensivePainter : public QWindow
44{
45public:
46 IntensivePainter() : m_backingStore(new QBackingStore(this))
47 { setGeometry(posx: 100, posy: 100, w: 900, h: 600); }
48
49protected:
50 bool interrupted() const { return m_stopPainting; }
51 void stopPainting() { m_stopPainting = true; }
52 void beginPaining() { m_stopPainting = false; }
53
54 void exposeEvent(QExposeEvent *)
55 {
56 bool useGreenColor = false;
57 QRect rect(0, 0, width(), height());
58 m_backingStore->resize(size: QSize(width(), height()));
59
60 qint32 peekerId = QX11Info::generatePeekerId();
61 if (peekerId == -1) {
62 qWarning() << "Internal error: QX11Info::generatePeekerId() returned -1";
63 exit(EXIT_FAILURE);
64 }
65
66 while (!interrupted()) { // Begin a long operation
67 m_backingStore->beginPaint(rect);
68 QPaintDevice *device = m_backingStore->paintDevice();
69 QPainter painter(device);
70 painter.fillRect(r: rect, c: useGreenColor ? Qt::green : Qt::red);
71 useGreenColor = !useGreenColor;
72 painter.drawText(r: rect, flags: Qt::AlignCenter, QStringLiteral("Press any key to exit.\n"
73 "(make sure the window is in focus when testing)\n\n If key press does not "
74 "immediately exit the application,\n then that should be considered a bug/regression."
75 "\n\n A demo might appear frozen to some window managers\n and therefore "
76 "might get grayed out (e.g Unity), this is the expected behavior.\n On KWin, for "
77 "example, this demo does not get grayed out.\n Resizing the window on any system will "
78 "cause rendering artefacts, that is not a bug,\n but simply the way the test is "
79 "implemented."));
80 m_backingStore->endPaint();
81 m_backingStore->flush(region: rect);
82 QThread::msleep(500); // Reduce the speed of re-painting (blinking)
83
84 QX11Info::peekEventQueue(peeker: [](xcb_generic_event_t *event, void *data) {
85 bool isKeyPress = (event->response_type & ~0x80) == XCB_KEY_PRESS;
86 if (isKeyPress) {
87 IntensivePainter *painter = static_cast<IntensivePainter *>(data);
88 painter->stopPainting();
89 }
90 return isKeyPress;
91 }, peekerData: this, option: QX11Info::PeekOption::PeekFromCachedIndex, peekerId);
92 }
93
94 QX11Info::removePeekerId(peekerId);
95 exit(EXIT_SUCCESS);
96 }
97
98private:
99 QBackingStore *m_backingStore;
100 bool m_stopPainting = false;
101};
102
103int main(int argc, char** argv)
104{
105 QGuiApplication app(argc, argv);
106
107 IntensivePainter painterWindow;
108 painterWindow.show();
109
110 return app.exec();
111}
112

source code of qtx11extras/tests/manual/peeker/main.cpp