Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwaylandxdgshellintegration.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtWaylandCompositor/QWaylandXdgSurface>
8#include <QtWaylandCompositor/QWaylandCompositor>
9#include <QtWaylandCompositor/QWaylandSeat>
10
12
13namespace QtWayland {
14
15static void handlePopupCreated(QWaylandQuickShellSurfaceItem *parentItem, QWaylandXdgPopup *popup)
16{
17 if (parentItem->shellSurface() == popup->parentXdgSurface())
18 QWaylandQuickShellSurfaceItemPrivate::get(parentItem)->maybeCreateAutoPopup(popup->xdgSurface());
19}
20
21XdgToplevelIntegration::XdgToplevelIntegration(QWaylandQuickShellSurfaceItem *item)
22 : QWaylandQuickShellIntegration(item)
23 , m_item(item)
24 , m_xdgSurface(qobject_cast<QWaylandXdgSurface *>(item->shellSurface()))
25 , m_toplevel(m_xdgSurface->toplevel())
26 , grabberState(GrabberState::Default)
27{
28 Q_ASSERT(m_toplevel);
29
30 m_item->setSurface(m_xdgSurface->surface());
31
32 connect(m_toplevel, &QWaylandXdgToplevel::startMove, this, &XdgToplevelIntegration::handleStartMove);
33 connect(m_toplevel, &QWaylandXdgToplevel::startResize, this, &XdgToplevelIntegration::handleStartResize);
34 connect(m_toplevel, &QWaylandXdgToplevel::setMaximized, this, &XdgToplevelIntegration::handleSetMaximized);
35 connect(m_toplevel, &QWaylandXdgToplevel::unsetMaximized, this, &XdgToplevelIntegration::handleUnsetMaximized);
36 connect(m_toplevel, &QWaylandXdgToplevel::maximizedChanged, this, &XdgToplevelIntegration::handleMaximizedChanged);
37 connect(m_toplevel, &QWaylandXdgToplevel::setFullscreen, this, &XdgToplevelIntegration::handleSetFullscreen);
38 connect(m_toplevel, &QWaylandXdgToplevel::unsetFullscreen, this, &XdgToplevelIntegration::handleUnsetFullscreen);
39 connect(m_toplevel, &QWaylandXdgToplevel::fullscreenChanged, this, &XdgToplevelIntegration::handleFullscreenChanged);
40 connect(m_toplevel, &QWaylandXdgToplevel::activatedChanged, this, &XdgToplevelIntegration::handleActivatedChanged);
41 connect(m_xdgSurface->shell(), &QWaylandXdgShell::popupCreated, this, [item](QWaylandXdgPopup *popup, QWaylandXdgSurface *){
42 handlePopupCreated(item, popup);
43 });
44 connect(m_xdgSurface->surface(), &QWaylandSurface::destinationSizeChanged, this, &XdgToplevelIntegration::handleSurfaceSizeChanged);
45 connect(m_toplevel, &QObject::destroyed, this, &XdgToplevelIntegration::handleToplevelDestroyed);
46}
47
48bool XdgToplevelIntegration::eventFilter(QObject *object, QEvent *event)
49{
50 if (event->type() == QEvent::MouseMove) {
51 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
52 return filterMouseMoveEvent(mouseEvent);
53 } else if (event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
54 return filterPointerReleaseEvent();
55 } else if (event->type() == QEvent::TouchUpdate) {
56 QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
57 return filterTouchUpdateEvent(touchEvent);
58 }
59 return QWaylandQuickShellIntegration::eventFilter(object, event);
60}
61
62bool XdgToplevelIntegration::filterPointerMoveEvent(const QPointF &scenePosition)
63{
64 if (grabberState == GrabberState::Resize) {
65 if (!resizeState.initialized) {
66 resizeState.initialMousePos = scenePosition;
67 resizeState.initialized = true;
68 return true;
69 }
70 QPointF delta = m_item->mapToSurface(scenePosition - resizeState.initialMousePos);
71 QSize newSize = m_toplevel->sizeForResize(resizeState.initialWindowSize, delta, resizeState.resizeEdges);
72 m_toplevel->sendResizing(newSize);
73 } else if (grabberState == GrabberState::Move) {
74 QQuickItem *moveItem = m_item->moveItem();
75 if (!moveState.initialized) {
76 moveState.initialOffset = moveItem->mapFromItem(nullptr, scenePosition);
77 moveState.initialized = true;
78 return true;
79 }
80 if (!moveItem->parentItem())
81 return true;
82 QPointF parentPos = moveItem->parentItem()->mapFromItem(nullptr, scenePosition);
83 moveItem->setPosition(parentPos - moveState.initialOffset);
84 }
85 return false;
86}
87
88bool XdgToplevelIntegration::filterTouchUpdateEvent(QTouchEvent *event)
89{
90 if (event->pointCount() == 0)
91 return false;
92
93 Q_ASSERT(grabberState != GrabberState::Move || moveState.seat == m_item->compositor()->seatFor(event));
94 Q_ASSERT(grabberState != GrabberState::Resize || resizeState.seat == m_item->compositor()->seatFor(event));
95
96 QEventPoint point = event->points().first();
97 return filterPointerMoveEvent(point.scenePosition());
98 }
99
100bool XdgToplevelIntegration::filterMouseMoveEvent(QMouseEvent *event)
101{
102 Q_ASSERT(grabberState != GrabberState::Move || moveState.seat == m_item->compositor()->seatFor(event));
103 Q_ASSERT(grabberState != GrabberState::Resize || resizeState.seat == m_item->compositor()->seatFor(event));
104
105 return filterPointerMoveEvent(event->scenePosition());
106}
107
108bool XdgToplevelIntegration::filterPointerReleaseEvent()
109{
110 if (grabberState != GrabberState::Default) {
111 grabberState = GrabberState::Default;
112 return true;
113 }
114 return false;
115}
116
117void XdgToplevelIntegration::handleStartMove(QWaylandSeat *seat)
118{
119 grabberState = GrabberState::Move;
120 moveState.seat = seat;
121 moveState.initialized = false;
122}
123
124void XdgToplevelIntegration::handleStartResize(QWaylandSeat *seat, Qt::Edges edges)
125{
126 grabberState = GrabberState::Resize;
127 resizeState.seat = seat;
128 resizeState.resizeEdges = edges;
129 resizeState.initialWindowSize = m_xdgSurface->windowGeometry().size();
130 resizeState.initialPosition = m_item->moveItem()->position();
131 resizeState.initialSurfaceSize = m_item->surface()->destinationSize();
132 resizeState.initialized = false;
133}
134
135void XdgToplevelIntegration::handleSetMaximized()
136{
137 if (!m_item->view()->isPrimary())
138 return;
139
140 QList<QWaylandXdgToplevel::State> states = m_toplevel->states();
141
142 if (!states.contains(QWaylandXdgToplevel::State::FullscreenState) && !states.contains(QWaylandXdgToplevel::State::MaximizedState)) {
143 windowedGeometry.initialWindowSize = m_xdgSurface->windowGeometry().size();
144 windowedGeometry.initialPosition = m_item->moveItem()->position();
145 }
146
147 // Any prior output-resize handlers are irrelevant at this point.
148 disconnect(nonwindowedState.sizeChangedConnection);
149 nonwindowedState.output = m_item->view()->output();
150 nonwindowedState.sizeChangedConnection = connect(nonwindowedState.output, &QWaylandOutput::availableGeometryChanged, this, &XdgToplevelIntegration::handleMaximizedSizeChanged);
151 handleMaximizedSizeChanged();
152}
153
154void XdgToplevelIntegration::handleMaximizedSizeChanged()
155{
156 // Insurance against handleToplevelDestroyed() not managing to disconnect this
157 // handler in time.
158 if (m_toplevel == nullptr)
159 return;
160
161 m_toplevel->sendMaximized(nonwindowedState.output->availableGeometry().size() / nonwindowedState.output->scaleFactor());
162}
163
164void XdgToplevelIntegration::handleUnsetMaximized()
165{
166 if (!m_item->view()->isPrimary())
167 return;
168
169 // If no prior windowed size was recorded, send a 0x0 configure event
170 // to allow the client to choose its preferred size.
171 if (windowedGeometry.initialWindowSize.isValid())
172 m_toplevel->sendUnmaximized(windowedGeometry.initialWindowSize);
173 else
174 m_toplevel->sendUnmaximized();
175}
176
177void XdgToplevelIntegration::handleMaximizedChanged()
178{
179 if (m_toplevel->maximized()) {
180 if (auto *output = m_item->view()->output()) {
181 m_item->moveItem()->setPosition(output->position() + output->availableGeometry().topLeft());
182 } else {
183 qCWarning(qLcWaylandCompositor) << "The view does not have a corresponding output,"
184 << "ignoring maximized state";
185 }
186 } else {
187 m_item->moveItem()->setPosition(windowedGeometry.initialPosition);
188 }
189}
190
191void XdgToplevelIntegration::handleSetFullscreen()
192{
193 if (!m_item->view()->isPrimary())
194 return;
195
196 QList<QWaylandXdgToplevel::State> states = m_toplevel->states();
197
198 if (!states.contains(QWaylandXdgToplevel::State::FullscreenState) && !states.contains(QWaylandXdgToplevel::State::MaximizedState)) {
199 windowedGeometry.initialWindowSize = m_xdgSurface->windowGeometry().size();
200 windowedGeometry.initialPosition = m_item->moveItem()->position();
201 }
202
203 // Any prior output-resize handlers are irrelevant at this point.
204 disconnect(nonwindowedState.sizeChangedConnection);
205 nonwindowedState.output = m_item->view()->output();
206 nonwindowedState.sizeChangedConnection = connect(nonwindowedState.output, &QWaylandOutput::geometryChanged, this, &XdgToplevelIntegration::handleFullscreenSizeChanged);
207 handleFullscreenSizeChanged();
208}
209
210void XdgToplevelIntegration::handleFullscreenSizeChanged()
211{
212 // Insurance against handleToplevelDestroyed() not managing to disconnect this
213 // handler in time.
214 if (m_toplevel == nullptr)
215 return;
216
217 m_toplevel->sendFullscreen(nonwindowedState.output->geometry().size() / nonwindowedState.output->scaleFactor());
218}
219
220void XdgToplevelIntegration::handleUnsetFullscreen()
221{
222 if (!m_item->view()->isPrimary())
223 return;
224
225 // If no prior windowed size was recorded, send a 0x0 configure event
226 // to allow the client to choose its preferred size.
227 if (windowedGeometry.initialWindowSize.isValid())
228 m_toplevel->sendUnmaximized(windowedGeometry.initialWindowSize);
229 else
230 m_toplevel->sendUnmaximized();
231}
232
233void XdgToplevelIntegration::handleFullscreenChanged()
234{
235 if (m_toplevel->fullscreen()) {
236 if (auto *output = m_item->view()->output()) {
237 m_item->moveItem()->setPosition(output->position() + output->geometry().topLeft());
238 } else {
239 qCWarning(qLcWaylandCompositor) << "The view does not have a corresponding output,"
240 << "ignoring fullscreen state";
241 }
242 } else {
243 m_item->moveItem()->setPosition(windowedGeometry.initialPosition);
244 }
245}
246
247void XdgToplevelIntegration::handleActivatedChanged()
248{
249 if (m_toplevel->activated())
250 m_item->raise();
251}
252
253void XdgToplevelIntegration::handleSurfaceSizeChanged()
254{
255 if (grabberState == GrabberState::Resize) {
256 qreal dx = 0;
257 qreal dy = 0;
258 if (resizeState.resizeEdges & Qt::TopEdge)
259 dy = resizeState.initialSurfaceSize.height() - m_item->surface()->destinationSize().height();
260 if (resizeState.resizeEdges & Qt::LeftEdge)
261 dx = resizeState.initialSurfaceSize.width() - m_item->surface()->destinationSize().width();
262 QPointF offset = m_item->mapFromSurface({dx, dy});
263 m_item->moveItem()->setPosition(resizeState.initialPosition + offset);
264 }
265}
266
267void XdgToplevelIntegration::handleToplevelDestroyed()
268{
269 // Disarm any handlers that might fire on the now-stale toplevel pointer
270 nonwindowedState.output = nullptr;
271 disconnect(nonwindowedState.sizeChangedConnection);
272}
273
274XdgPopupIntegration::XdgPopupIntegration(QWaylandQuickShellSurfaceItem *item)
275 : m_item(item)
276 , m_xdgSurface(qobject_cast<QWaylandXdgSurface *>(item->shellSurface()))
277 , m_popup(m_xdgSurface->popup())
278{
279 Q_ASSERT(m_popup);
280
281 m_item->setSurface(m_xdgSurface->surface());
282 handleGeometryChanged();
283
284 connect(m_popup, &QWaylandXdgPopup::configuredGeometryChanged, this, &XdgPopupIntegration::handleGeometryChanged);
285 connect(m_xdgSurface->shell(), &QWaylandXdgShell::popupCreated, this, [item](QWaylandXdgPopup *popup, QWaylandXdgSurface *){
286 handlePopupCreated(item, popup);
287 });
288}
289
290void XdgPopupIntegration::handleGeometryChanged()
291{
292 if (m_item->view()->output()) {
293 const QPoint windowOffset = m_popup->parentXdgSurface()->windowGeometry().topLeft();
294 const QPoint surfacePosition = m_popup->unconstrainedPosition() + windowOffset;
295 const QPoint itemPosition = m_item->mapFromSurface(surfacePosition).toPoint();
296 //TODO: positioner size or other size...?
297 //TODO check positioner constraints etc... sliding, flipping
298 m_item->moveItem()->setPosition(itemPosition);
299 } else {
300 qWarning() << "XdgPopupIntegration popup item without output" << m_item;
301 }
302}
303
304}
305
306QT_END_NAMESPACE
307
308#include "moc_qwaylandxdgshellintegration_p.cpp"
bool eventFilter(QObject *object, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
Combined button and popup list for selecting options.
static void handlePopupCreated(QWaylandQuickShellSurfaceItem *parentItem, QWaylandXdgPopup *popup)