1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins 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#ifndef QXCBWINDOW_H
41#define QXCBWINDOW_H
42
43#include <qpa/qplatformwindow.h>
44#include <QtGui/QSurfaceFormat>
45#include <QtGui/QImage>
46
47#include <xcb/xcb.h>
48#include <xcb/sync.h>
49
50#include "qxcbobject.h"
51
52#include <QtPlatformHeaders/qxcbwindowfunctions.h>
53
54QT_BEGIN_NAMESPACE
55
56class QXcbScreen;
57class QXcbSyncWindowRequest;
58class QIcon;
59
60class Q_XCB_EXPORT QXcbWindow : public QXcbObject, public QXcbWindowEventListener, public QPlatformWindow
61{
62public:
63 enum NetWmState {
64 NetWmStateAbove = 0x1,
65 NetWmStateBelow = 0x2,
66 NetWmStateFullScreen = 0x4,
67 NetWmStateMaximizedHorz = 0x8,
68 NetWmStateMaximizedVert = 0x10,
69 NetWmStateModal = 0x20,
70 NetWmStateStaysOnTop = 0x40,
71 NetWmStateDemandsAttention = 0x80
72 };
73
74 Q_DECLARE_FLAGS(NetWmStates, NetWmState)
75
76 QXcbWindow(QWindow *window);
77 ~QXcbWindow();
78
79 void setGeometry(const QRect &rect) override;
80
81 QMargins frameMargins() const override;
82
83 void setVisible(bool visible) override;
84 void setWindowFlags(Qt::WindowFlags flags) override;
85 void setWindowState(Qt::WindowStates state) override;
86 WId winId() const override;
87 void setParent(const QPlatformWindow *window) override;
88
89 bool isExposed() const override;
90 bool isEmbedded() const override;
91 QPoint mapToGlobal(const QPoint &pos) const override;
92 QPoint mapFromGlobal(const QPoint &pos) const override;
93
94 void setWindowTitle(const QString &title) override;
95 void setWindowIconText(const QString &title);
96 void setWindowIcon(const QIcon &icon) override;
97 void raise() override;
98 void lower() override;
99 void propagateSizeHints() override;
100
101 void requestActivateWindow() override;
102
103 bool setKeyboardGrabEnabled(bool grab) override;
104 bool setMouseGrabEnabled(bool grab) override;
105
106 QSurfaceFormat format() const override;
107
108 bool windowEvent(QEvent *event) override;
109
110 bool startSystemResize(const QPoint &pos, Qt::Corner corner) override;
111 bool startSystemMove(const QPoint &pos) override;
112
113 void setOpacity(qreal level) override;
114 void setMask(const QRegion &region) override;
115
116 void setAlertState(bool enabled) override;
117 bool isAlertState() const override { return m_alertState; }
118
119 xcb_window_t xcb_window() const { return m_window; }
120 uint depth() const { return m_depth; }
121 QImage::Format imageFormat() const { return m_imageFormat; }
122 bool imageNeedsRgbSwap() const { return m_imageRgbSwap; }
123
124 bool handleNativeEvent(xcb_generic_event_t *event) override;
125
126 void handleExposeEvent(const xcb_expose_event_t *event) override;
127 void handleClientMessageEvent(const xcb_client_message_event_t *event) override;
128 void handleConfigureNotifyEvent(const xcb_configure_notify_event_t *event) override;
129 void handleMapNotifyEvent(const xcb_map_notify_event_t *event) override;
130 void handleUnmapNotifyEvent(const xcb_unmap_notify_event_t *event) override;
131 void handleButtonPressEvent(const xcb_button_press_event_t *event) override;
132 void handleButtonReleaseEvent(const xcb_button_release_event_t *event) override;
133 void handleMotionNotifyEvent(const xcb_motion_notify_event_t *event) override;
134
135 void handleEnterNotifyEvent(const xcb_enter_notify_event_t *event) override;
136 void handleLeaveNotifyEvent(const xcb_leave_notify_event_t *event) override;
137 void handleFocusInEvent(const xcb_focus_in_event_t *event) override;
138 void handleFocusOutEvent(const xcb_focus_out_event_t *event) override;
139 void handlePropertyNotifyEvent(const xcb_property_notify_event_t *event) override;
140#if QT_CONFIG(xcb_xinput)
141 void handleXIMouseEvent(xcb_ge_event_t *, Qt::MouseEventSource source = Qt::MouseEventNotSynthesized) override;
142 void handleXIEnterLeave(xcb_ge_event_t *) override;
143#endif
144
145 QXcbWindow *toWindow() override;
146
147 void handleMouseEvent(xcb_timestamp_t time, const QPoint &local, const QPoint &global,
148 Qt::KeyboardModifiers modifiers, QEvent::Type type, Qt::MouseEventSource source);
149
150 void updateNetWmUserTime(xcb_timestamp_t timestamp);
151
152 static void setWmWindowTypeStatic(QWindow *window, QXcbWindowFunctions::WmWindowTypes windowTypes);
153 static void setWmWindowRoleStatic(QWindow *window, const QByteArray &role);
154 static uint visualIdStatic(QWindow *window);
155
156 QXcbWindowFunctions::WmWindowTypes wmWindowTypes() const;
157 void setWmWindowType(QXcbWindowFunctions::WmWindowTypes types, Qt::WindowFlags flags);
158 void setWmWindowRole(const QByteArray &role);
159
160 static void setWindowIconTextStatic(QWindow *window, const QString &text);
161
162 void setParentRelativeBackPixmap();
163 bool requestSystemTrayWindowDock();
164 uint visualId() const;
165
166 bool needsSync() const;
167
168 void postSyncWindowRequest();
169 void clearSyncWindowRequest() { m_pendingSyncRequest = nullptr; }
170
171 QXcbScreen *xcbScreen() const;
172
173 bool startSystemMoveResize(const QPoint &pos, int corner);
174 void doStartSystemMoveResize(const QPoint &globalPos, int corner);
175
176 static bool isTrayIconWindow(QWindow *window)
177 {
178 return window->objectName() == QLatin1String("QSystemTrayIconSysWindow");
179 }
180
181 virtual void create();
182 virtual void destroy();
183
184 static void setWindowTitle(const QXcbConnection *conn, xcb_window_t window, const QString &title);
185 static QString windowTitle(const QXcbConnection *conn, xcb_window_t window);
186
187 int swapInterval() const { return m_swapInterval; }
188 void setSwapInterval(int swapInterval) { m_swapInterval = swapInterval; }
189
190public Q_SLOTS:
191 void updateSyncRequestCounter();
192
193protected:
194 virtual void resolveFormat(const QSurfaceFormat &format) { m_format = format; }
195 virtual const xcb_visualtype_t *createVisual();
196 void setImageFormatForVisual(const xcb_visualtype_t *visual);
197
198 QXcbScreen *parentScreen();
199 QXcbScreen *initialScreen() const;
200
201 void setNetWmState(bool set, xcb_atom_t one, xcb_atom_t two = 0);
202 void setNetWmState(Qt::WindowFlags flags);
203 void setNetWmState(Qt::WindowStates state);
204 void setNetWmStateOnUnmappedWindow();
205 NetWmStates netWmStates();
206
207 void setMotifWmHints(Qt::WindowFlags flags);
208
209 void setTransparentForMouseEvents(bool transparent);
210 void updateDoesNotAcceptFocus(bool doesNotAcceptFocus);
211
212 void sendXEmbedMessage(xcb_window_t window, quint32 message,
213 quint32 detail = 0, quint32 data1 = 0, quint32 data2 = 0);
214 void handleXEmbedMessage(const xcb_client_message_event_t *event);
215
216 void show();
217 void hide();
218
219 bool relayFocusToModalWindow() const;
220 void doFocusIn();
221 void doFocusOut();
222
223 void handleButtonPressEvent(int event_x, int event_y, int root_x, int root_y,
224 int detail, Qt::KeyboardModifiers modifiers, xcb_timestamp_t timestamp,
225 QEvent::Type type, Qt::MouseEventSource source = Qt::MouseEventNotSynthesized);
226
227 void handleButtonReleaseEvent(int event_x, int event_y, int root_x, int root_y,
228 int detail, Qt::KeyboardModifiers modifiers, xcb_timestamp_t timestamp,
229 QEvent::Type type, Qt::MouseEventSource source = Qt::MouseEventNotSynthesized);
230
231 void handleMotionNotifyEvent(int event_x, int event_y, int root_x, int root_y,
232 Qt::KeyboardModifiers modifiers, xcb_timestamp_t timestamp,
233 QEvent::Type type, Qt::MouseEventSource source = Qt::MouseEventNotSynthesized);
234
235 void handleEnterNotifyEvent(int event_x, int event_y, int root_x, int root_y,
236 quint8 mode, quint8 detail, xcb_timestamp_t timestamp);
237
238 void handleLeaveNotifyEvent(int root_x, int root_y,
239 quint8 mode, quint8 detail, xcb_timestamp_t timestamp);
240
241 xcb_window_t m_window = 0;
242 xcb_colormap_t m_cmap = 0;
243
244 uint m_depth = 0;
245 QImage::Format m_imageFormat = QImage::Format_ARGB32_Premultiplied;
246 bool m_imageRgbSwap = false;
247
248 xcb_sync_int64_t m_syncValue;
249 xcb_sync_counter_t m_syncCounter = 0;
250
251 Qt::WindowStates m_windowState = Qt::WindowNoState;
252
253 bool m_mapped = false;
254 bool m_transparent = false;
255 bool m_deferredActivation = false;
256 bool m_embedded = false;
257 bool m_alertState = false;
258 bool m_minimized = false;
259 bool m_trayIconWindow = false;
260 xcb_window_t m_netWmUserTimeWindow = XCB_NONE;
261
262 QSurfaceFormat m_format;
263
264 mutable bool m_dirtyFrameMargins = false;
265 mutable QMargins m_frameMargins;
266
267 QRegion m_exposeRegion;
268 QSize m_oldWindowSize;
269
270 xcb_visualid_t m_visualId = 0;
271 // Last sent state. Initialized to an invalid state, on purpose.
272 Qt::WindowStates m_lastWindowStateEvent = Qt::WindowActive;
273
274 enum SyncState {
275 NoSyncNeeded,
276 SyncReceived,
277 SyncAndConfigureReceived
278 };
279 SyncState m_syncState = NoSyncNeeded;
280
281 QXcbSyncWindowRequest *m_pendingSyncRequest = nullptr;
282 int m_swapInterval = -1;
283};
284
285class QXcbForeignWindow : public QXcbWindow
286{
287public:
288 QXcbForeignWindow(QWindow *window, WId nativeHandle)
289 : QXcbWindow(window) { m_window = nativeHandle; }
290 ~QXcbForeignWindow();
291 bool isForeignWindow() const override { return true; }
292
293protected:
294 void create() override {} // No-op
295};
296
297QVector<xcb_rectangle_t> qRegionToXcbRectangleList(const QRegion &region);
298
299QT_END_NAMESPACE
300
301Q_DECLARE_METATYPE(QXcbWindow*)
302
303#endif
304