1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QWAYLANDWINDOW_H |
5 | #define QWAYLANDWINDOW_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/QWaitCondition> |
19 | #include <QtCore/QMutex> |
20 | #include <QtCore/QReadWriteLock> |
21 | |
22 | #include <QtGui/QIcon> |
23 | #include <QtGui/QEventPoint> |
24 | #include <QtCore/QVariant> |
25 | #include <QtCore/QLoggingCategory> |
26 | #include <QtCore/QElapsedTimer> |
27 | #include <QtCore/QList> |
28 | #include <QtCore/QMap> // for QVariantMap |
29 | |
30 | #include <qpa/qplatformwindow.h> |
31 | #include <qpa/qplatformwindow_p.h> |
32 | |
33 | #include <QtWaylandClient/private/qwayland-wayland.h> |
34 | #include <QtWaylandClient/private/qwaylanddisplay_p.h> |
35 | #include <QtWaylandClient/qtwaylandclientglobal.h> |
36 | #include <QtWaylandClient/private/qwaylandshellsurface_p.h> |
37 | |
38 | struct wl_egl_window; |
39 | |
40 | QT_BEGIN_NAMESPACE |
41 | |
42 | namespace QtWaylandClient { |
43 | |
44 | Q_DECLARE_LOGGING_CATEGORY(lcWaylandBackingstore) |
45 | |
46 | class QWaylandDisplay; |
47 | class QWaylandBuffer; |
48 | class QWaylandShellSurface; |
49 | class QWaylandSubSurface; |
50 | class QWaylandAbstractDecoration; |
51 | class QWaylandInputDevice; |
52 | class QWaylandScreen; |
53 | class QWaylandShellIntegration; |
54 | class QWaylandShmBackingStore; |
55 | class QWaylandPointerEvent; |
56 | class QWaylandPointerGestureSwipeEvent; |
57 | class QWaylandPointerGesturePinchEvent; |
58 | class QWaylandSurface; |
59 | class QWaylandFractionalScale; |
60 | class QWaylandViewport; |
61 | |
62 | class Q_WAYLANDCLIENT_EXPORT QWaylandWindow : public QNativeInterface::Private::QWaylandWindow, |
63 | public QPlatformWindow |
64 | { |
65 | Q_OBJECT |
66 | public: |
67 | enum WindowType { |
68 | Shm, |
69 | Egl, |
70 | Vulkan |
71 | }; |
72 | |
73 | enum ToplevelWindowTilingState { |
74 | WindowNoState = 0, |
75 | WindowTiledLeft = 1, |
76 | WindowTiledRight = 2, |
77 | WindowTiledTop = 4, |
78 | WindowTiledBottom = 8 |
79 | }; |
80 | Q_DECLARE_FLAGS(ToplevelWindowTilingStates, ToplevelWindowTilingState) |
81 | |
82 | QWaylandWindow(QWindow *window, QWaylandDisplay *display); |
83 | ~QWaylandWindow() override; |
84 | |
85 | // Keep Toplevels position on the top left corner of their screen |
86 | static inline bool fixedToplevelPositions = true; |
87 | |
88 | virtual WindowType windowType() const = 0; |
89 | virtual void ensureSize(); |
90 | WId winId() const override; |
91 | void setVisible(bool visible) override; |
92 | void setParent(const QPlatformWindow *parent) override; |
93 | |
94 | void setWindowTitle(const QString &title) override; |
95 | |
96 | inline QIcon windowIcon() const; |
97 | void setWindowIcon(const QIcon &icon) override; |
98 | |
99 | void setGeometry(const QRect &rect) override; |
100 | void resizeFromApplyConfigure(const QSize &sizeWithMargins, const QPoint &offset = {0, 0}); |
101 | void repositionFromApplyConfigure(const QPoint &position); |
102 | void setGeometryFromApplyConfigure(const QPoint &globalPosition, const QSize &sizeWithMargins); |
103 | |
104 | void applyConfigureWhenPossible(); //rename to possible? |
105 | |
106 | void attach(QWaylandBuffer *buffer, int x, int y); |
107 | void attachOffset(QWaylandBuffer *buffer); |
108 | QPoint attachOffset() const; |
109 | |
110 | void damage(const QRect &rect); |
111 | |
112 | void safeCommit(QWaylandBuffer *buffer, const QRegion &damage); |
113 | void handleExpose(const QRegion ®ion); |
114 | void commit(QWaylandBuffer *buffer, const QRegion &damage); |
115 | |
116 | void commit(); |
117 | |
118 | bool waitForFrameSync(int timeout); |
119 | |
120 | QMargins frameMargins() const override; |
121 | QMargins clientSideMargins() const; |
122 | void setCustomMargins(const QMargins &margins) override; |
123 | QSize surfaceSize() const; |
124 | QMargins windowContentMargins() const; |
125 | QRect windowContentGeometry() const; |
126 | QPointF mapFromWlSurface(const QPointF &surfacePosition) const; |
127 | |
128 | QWaylandSurface *waylandSurface() const { return mSurface.data(); } |
129 | ::wl_surface *wlSurface(); |
130 | ::wl_surface *surface() const override |
131 | { |
132 | return const_cast<QWaylandWindow *>(this)->wlSurface(); |
133 | } |
134 | static QWaylandWindow *fromWlSurface(::wl_surface *surface); |
135 | |
136 | QWaylandDisplay *display() const { return mDisplay; } |
137 | QWaylandShellSurface *shellSurface() const; |
138 | std::any _surfaceRole() const override; |
139 | QWaylandSubSurface *subSurfaceWindow() const; |
140 | QWaylandScreen *waylandScreen() const; |
141 | |
142 | void handleContentOrientationChange(Qt::ScreenOrientation orientation) override; |
143 | void setOrientationMask(Qt::ScreenOrientations mask); |
144 | |
145 | ToplevelWindowTilingStates toplevelWindowTilingStates() const; |
146 | void handleToplevelWindowTilingStatesChanged(ToplevelWindowTilingStates states); |
147 | |
148 | Qt::WindowStates windowStates() const; |
149 | void setWindowState(Qt::WindowStates states) override; |
150 | void setWindowFlags(Qt::WindowFlags flags) override; |
151 | void handleWindowStatesChanged(Qt::WindowStates states); |
152 | |
153 | void raise() override; |
154 | void lower() override; |
155 | |
156 | void setMask(const QRegion ®ion) override; |
157 | |
158 | void setAlertState(bool enabled) override; |
159 | bool isAlertState() const override; |
160 | |
161 | qreal scale() const; |
162 | qreal devicePixelRatio() const override; |
163 | |
164 | void requestActivateWindow() override; |
165 | bool isExposed() const override; |
166 | bool isActive() const override; |
167 | |
168 | QWaylandAbstractDecoration *decoration() const; |
169 | |
170 | void handleMouse(QWaylandInputDevice *inputDevice, const QWaylandPointerEvent &e); |
171 | #ifndef QT_NO_GESTURES |
172 | void handleSwipeGesture(QWaylandInputDevice *inputDevice, |
173 | const QWaylandPointerGestureSwipeEvent &e); |
174 | void handlePinchGesture(QWaylandInputDevice *inputDevice, |
175 | const QWaylandPointerGesturePinchEvent &e); |
176 | #endif |
177 | |
178 | bool touchDragDecoration(QWaylandInputDevice *inputDevice, const QPointF &local, const QPointF &global, |
179 | QEventPoint::State state, Qt::KeyboardModifiers mods); |
180 | |
181 | bool createDecoration(); |
182 | |
183 | #if QT_CONFIG(cursor) |
184 | void setMouseCursor(QWaylandInputDevice *device, const QCursor &cursor); |
185 | void restoreMouseCursor(QWaylandInputDevice *device); |
186 | #endif |
187 | |
188 | QWaylandWindow *transientParent() const; |
189 | |
190 | QMutex *resizeMutex() { return &mResizeLock; } |
191 | void doApplyConfigure(); |
192 | void setCanResize(bool canResize); |
193 | |
194 | bool setMouseGrabEnabled(bool grab) override; |
195 | static QWaylandWindow *mouseGrab() { return mMouseGrab; } |
196 | |
197 | void sendProperty(const QString &name, const QVariant &value); |
198 | void setProperty(const QString &name, const QVariant &value); |
199 | |
200 | QVariantMap properties() const; |
201 | QVariant property(const QString &name); |
202 | QVariant property(const QString &name, const QVariant &defaultValue); |
203 | |
204 | #ifdef QT_PLATFORM_WINDOW_HAS_VIRTUAL_SET_BACKING_STORE |
205 | void setBackingStore(QPlatformBackingStore *store) override; |
206 | #else |
207 | void setBackingStore(QWaylandShmBackingStore *backingStore) { mBackingStore = backingStore; } |
208 | #endif |
209 | QWaylandShmBackingStore *backingStore() const { return mBackingStore; } |
210 | |
211 | void setShellIntegration(QWaylandShellIntegration *shellIntegration); |
212 | QWaylandShellIntegration *shellIntegration() const { return mShellIntegration; } |
213 | |
214 | bool setKeyboardGrabEnabled(bool) override { return false; } |
215 | void propagateSizeHints() override; |
216 | void addAttachOffset(const QPoint point); |
217 | |
218 | bool startSystemResize(Qt::Edges edges) override; |
219 | bool startSystemMove() override; |
220 | |
221 | void timerEvent(QTimerEvent *event) override; |
222 | void requestUpdate() override; |
223 | void handleUpdate(); |
224 | void deliverUpdateRequest() override; |
225 | |
226 | void setXdgActivationToken(const QString &token); |
227 | void requestXdgActivationToken(uint serial) override; |
228 | |
229 | void beginFrame(); |
230 | void endFrame(); |
231 | |
232 | void closeChildPopups(); |
233 | |
234 | virtual void reinit(); |
235 | void reset(); |
236 | |
237 | public slots: |
238 | void applyConfigure(); |
239 | |
240 | signals: |
241 | void wlSurfaceCreated(); |
242 | void wlSurfaceDestroyed(); |
243 | |
244 | protected: |
245 | virtual void doHandleFrameCallback(); |
246 | virtual QRect defaultGeometry() const; |
247 | void sendExposeEvent(const QRect &rect); |
248 | |
249 | QWaylandDisplay *mDisplay = nullptr; |
250 | |
251 | // mSurface can be written by the main thread. Other threads should claim a read lock for access |
252 | mutable QReadWriteLock mSurfaceLock; |
253 | QScopedPointer<QWaylandSurface> mSurface; |
254 | QScopedPointer<QWaylandFractionalScale> mFractionalScale; |
255 | QScopedPointer<QWaylandViewport> mViewport; |
256 | |
257 | QWaylandShellIntegration *mShellIntegration = nullptr; |
258 | QWaylandShellSurface *mShellSurface = nullptr; |
259 | QWaylandSubSurface *mSubSurfaceWindow = nullptr; |
260 | QList<QWaylandSubSurface *> mChildren; |
261 | |
262 | QWaylandAbstractDecoration *mWindowDecoration = nullptr; |
263 | bool mWindowDecorationEnabled = false; |
264 | bool mMouseEventsInContentArea = false; |
265 | Qt::MouseButtons mMousePressedInContentArea = Qt::NoButton; |
266 | |
267 | #ifndef QT_NO_GESTURES |
268 | enum GestureState { |
269 | GestureNotActive, |
270 | GestureActiveInContentArea, |
271 | GestureActiveInDecoration |
272 | }; |
273 | |
274 | // We want gestures started in the decoration area to be completely ignored even if the mouse |
275 | // pointer is later moved to content area. Likewise, gestures started in the content area should |
276 | // keep sending events even if the mouse pointer is moved over the decoration (consider that |
277 | // the events for that gesture will be sent to us even if it's moved outside the window). |
278 | // So we track the gesture state and accept or ignore events based on that. Note that |
279 | // concurrent gestures of different types are not allowed in the protocol, so single state is |
280 | // enough |
281 | GestureState mGestureState = GestureNotActive; |
282 | #endif |
283 | |
284 | WId mWindowId; |
285 | bool mFrameCallbackTimedOut = false; // Whether the frame callback has timed out |
286 | int mFrameCallbackCheckIntervalTimerId = -1; |
287 | QAtomicInt mWaitingForUpdateDelivery = false; |
288 | |
289 | bool mWaitingForFrameCallback = false; // Protected by mFrameSyncMutex |
290 | QElapsedTimer mFrameCallbackElapsedTimer; // Protected by mFrameSyncMutex |
291 | struct ::wl_callback *mFrameCallback = nullptr; // Protected by mFrameSyncMutex |
292 | QMutex mFrameSyncMutex; |
293 | QWaitCondition mFrameSyncWait; |
294 | |
295 | // True when we have called deliverRequestUpdate, but the client has not yet attached a new buffer |
296 | bool mWaitingForUpdate = false; |
297 | |
298 | QMutex mResizeLock; |
299 | bool mWaitingToApplyConfigure = false; |
300 | bool mCanResize = true; |
301 | bool mResizeDirty = false; |
302 | bool mResizeAfterSwap; |
303 | int mFrameCallbackTimeout = 100; |
304 | QVariantMap m_properties; |
305 | |
306 | bool mSentInitialResize = false; |
307 | QPoint mOffset; |
308 | qreal mScale = 1; |
309 | QPlatformScreen *mLastReportedScreen = nullptr; |
310 | |
311 | QIcon mWindowIcon; |
312 | |
313 | Qt::WindowFlags mFlags; |
314 | QRegion mMask; |
315 | |
316 | // Empty QRegion maps to "infinite" input region, needs a dedicated "deliberately empty" state. |
317 | QRegion mInputRegion; |
318 | bool mTransparentInputRegion = false; |
319 | |
320 | QRegion mOpaqueArea; |
321 | Qt::WindowStates mLastReportedWindowStates = Qt::WindowNoState; |
322 | ToplevelWindowTilingStates mLastReportedToplevelWindowTilingStates = WindowNoState; |
323 | |
324 | QWaylandShmBackingStore *mBackingStore = nullptr; |
325 | QWaylandBuffer *mQueuedBuffer = nullptr; |
326 | QRegion mQueuedBufferDamage; |
327 | |
328 | QMargins mCustomMargins; |
329 | |
330 | QPointer<QWaylandWindow> mTransientParent; |
331 | QList<QPointer<QWaylandWindow>> mChildPopups; |
332 | |
333 | private slots: |
334 | void doApplyConfigureFromOtherThread(); |
335 | |
336 | private: |
337 | void setGeometry_helper(const QRect &rect); |
338 | void initWindow(); |
339 | void initializeWlSurface(); |
340 | bool shouldCreateShellSurface() const; |
341 | bool shouldCreateSubSurface() const; |
342 | QPlatformScreen *calculateScreenFromSurfaceEvents() const; |
343 | void setOpaqueArea(const QRegion &opaqueArea); |
344 | bool isOpaque() const; |
345 | void updateInputRegion(); |
346 | void updateViewport(); |
347 | |
348 | void handleMouseEventWithDecoration(QWaylandInputDevice *inputDevice, const QWaylandPointerEvent &e); |
349 | void handleScreensChanged(); |
350 | void sendRecursiveExposeEvent(); |
351 | |
352 | QWaylandWindow *closestTransientParent() const; |
353 | void addChildPopup(QWaylandWindow *child); |
354 | void removeChildPopup(QWaylandWindow *child); |
355 | |
356 | bool mInResizeFromApplyConfigure = false; |
357 | bool lastVisible = false; |
358 | QRect mLastExposeGeometry; |
359 | |
360 | static const wl_callback_listener callbackListener; |
361 | void handleFrameCallback(struct ::wl_callback* callback); |
362 | |
363 | static QWaylandWindow *mMouseGrab; |
364 | |
365 | friend class QWaylandSubSurface; |
366 | }; |
367 | |
368 | Q_DECLARE_OPERATORS_FOR_FLAGS(QWaylandWindow::ToplevelWindowTilingStates) |
369 | |
370 | inline QIcon QWaylandWindow::windowIcon() const |
371 | { |
372 | return mWindowIcon; |
373 | } |
374 | |
375 | inline QPoint QWaylandWindow::attachOffset() const |
376 | { |
377 | return mOffset; |
378 | } |
379 | |
380 | } |
381 | |
382 | QT_END_NAMESPACE |
383 | |
384 | #endif // QWAYLANDWINDOW_H |
385 | |