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