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