1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtWaylandCompositor module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qwaylandxdgshellv6integration_p.h"
38
39#include <QtWaylandCompositor/QWaylandXdgSurfaceV6>
40#include <QtWaylandCompositor/QWaylandCompositor>
41#include <QtWaylandCompositor/QWaylandSeat>
42
43QT_BEGIN_NAMESPACE
44
45#if QT_DEPRECATED_SINCE(5, 15)
46
47namespace QtWayland {
48
49static void handlePopupCreated(QWaylandQuickShellSurfaceItem *parentItem, QWaylandXdgPopupV6 *popup)
50{
51 if (parentItem->shellSurface() == popup->parentXdgSurface())
52 QWaylandQuickShellSurfaceItemPrivate::get(item: parentItem)->maybeCreateAutoPopup(shellSurface: popup->xdgSurface());
53}
54
55XdgToplevelV6Integration::XdgToplevelV6Integration(QWaylandQuickShellSurfaceItem *item)
56 : QWaylandQuickShellIntegration(item)
57 , m_item(item)
58 , m_xdgSurface(qobject_cast<QWaylandXdgSurfaceV6 *>(object: item->shellSurface()))
59 , m_toplevel(m_xdgSurface->toplevel())
60 , grabberState(GrabberState::Default)
61{
62 Q_ASSERT(m_toplevel);
63
64 m_item->setSurface(m_xdgSurface->surface());
65
66 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::startMove, receiver: this, slot: &XdgToplevelV6Integration::handleStartMove);
67 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::startResize, receiver: this, slot: &XdgToplevelV6Integration::handleStartResize);
68 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::setMaximized, receiver: this, slot: &XdgToplevelV6Integration::handleSetMaximized);
69 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::unsetMaximized, receiver: this, slot: &XdgToplevelV6Integration::handleUnsetMaximized);
70 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::maximizedChanged, receiver: this, slot: &XdgToplevelV6Integration::handleMaximizedChanged);
71 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::setFullscreen, receiver: this, slot: &XdgToplevelV6Integration::handleSetFullscreen);
72 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::unsetFullscreen, receiver: this, slot: &XdgToplevelV6Integration::handleUnsetFullscreen);
73 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::fullscreenChanged, receiver: this, slot: &XdgToplevelV6Integration::handleFullscreenChanged);
74 connect(sender: m_toplevel, signal: &QWaylandXdgToplevelV6::activatedChanged, receiver: this, slot: &XdgToplevelV6Integration::handleActivatedChanged);
75 connect(sender: m_xdgSurface->shell(), signal: &QWaylandXdgShellV6::popupCreated, context: this, slot: [item](QWaylandXdgPopupV6 *popup, QWaylandXdgSurfaceV6 *){
76 handlePopupCreated(parentItem: item, popup);
77 });
78 connect(sender: m_xdgSurface->surface(), signal: &QWaylandSurface::destinationSizeChanged, receiver: this, slot: &XdgToplevelV6Integration::handleSurfaceSizeChanged);
79 connect(sender: m_toplevel, signal: &QObject::destroyed, receiver: this, slot: &XdgToplevelV6Integration::handleToplevelDestroyed);
80}
81
82bool XdgToplevelV6Integration::eventFilter(QObject *object, QEvent *event)
83{
84 if (event->type() == QEvent::MouseMove) {
85 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
86 return filterMouseMoveEvent(event: mouseEvent);
87 } else if (event->type() == QEvent::MouseButtonRelease) {
88 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
89 return filterMouseReleaseEvent(event: mouseEvent);
90 }
91 return QWaylandQuickShellIntegration::eventFilter(watched: object, event);
92}
93
94bool XdgToplevelV6Integration::filterMouseMoveEvent(QMouseEvent *event)
95{
96 if (grabberState == GrabberState::Resize) {
97 Q_ASSERT(resizeState.seat == m_item->compositor()->seatFor(event));
98 if (!resizeState.initialized) {
99 resizeState.initialMousePos = event->windowPos();
100 resizeState.initialized = true;
101 return true;
102 }
103 QPointF delta = m_item->mapToSurface(point: event->windowPos() - resizeState.initialMousePos);
104 QSize newSize = m_toplevel->sizeForResize(size: resizeState.initialWindowSize, delta, edges: resizeState.resizeEdges);
105 m_toplevel->sendResizing(maxSize: newSize);
106 } else if (grabberState == GrabberState::Move) {
107 Q_ASSERT(moveState.seat == m_item->compositor()->seatFor(event));
108 QQuickItem *moveItem = m_item->moveItem();
109 if (!moveState.initialized) {
110 moveState.initialOffset = moveItem->mapFromItem(item: nullptr, point: event->windowPos());
111 moveState.initialized = true;
112 return true;
113 }
114 if (!moveItem->parentItem())
115 return true;
116 QPointF parentPos = moveItem->parentItem()->mapFromItem(item: nullptr, point: event->windowPos());
117 moveItem->setPosition(parentPos - moveState.initialOffset);
118 }
119 return false;
120}
121
122bool XdgToplevelV6Integration::filterMouseReleaseEvent(QMouseEvent *event)
123{
124 Q_UNUSED(event);
125
126 if (grabberState != GrabberState::Default) {
127 grabberState = GrabberState::Default;
128 return true;
129 }
130 return false;
131}
132
133void XdgToplevelV6Integration::handleStartMove(QWaylandSeat *seat)
134{
135 grabberState = GrabberState::Move;
136 moveState.seat = seat;
137 moveState.initialized = false;
138}
139
140void XdgToplevelV6Integration::handleStartResize(QWaylandSeat *seat, Qt::Edges edges)
141{
142 grabberState = GrabberState::Resize;
143 resizeState.seat = seat;
144 resizeState.resizeEdges = edges;
145 resizeState.initialWindowSize = m_xdgSurface->windowGeometry().size();
146 resizeState.initialPosition = m_item->moveItem()->position();
147 resizeState.initialSurfaceSize = m_item->surface()->destinationSize();
148 resizeState.initialized = false;
149}
150
151void XdgToplevelV6Integration::handleSetMaximized()
152{
153 if (!m_item->view()->isPrimary())
154 return;
155
156 QVector<QWaylandXdgToplevelV6::State> states = m_toplevel->states();
157
158 if (!states.contains(t: QWaylandXdgToplevelV6::State::FullscreenState) && !states.contains(t: QWaylandXdgToplevelV6::State::MaximizedState)) {
159 windowedGeometry.initialWindowSize = m_xdgSurface->windowGeometry().size();
160 windowedGeometry.initialPosition = m_item->moveItem()->position();
161 }
162
163 // Any prior output-resize handlers are irrelevant at this point.
164 disconnect(nonwindowedState.sizeChangedConnection);
165 nonwindowedState.output = m_item->view()->output();
166 nonwindowedState.sizeChangedConnection = connect(sender: nonwindowedState.output, signal: &QWaylandOutput::availableGeometryChanged, receiver: this, slot: &XdgToplevelV6Integration::handleMaximizedSizeChanged);
167 handleMaximizedSizeChanged();
168}
169
170void XdgToplevelV6Integration::handleMaximizedSizeChanged()
171{
172 // Insurance against handleToplevelDestroyed() not managing to disconnect this
173 // handler in time.
174 if (m_toplevel == nullptr)
175 return;
176
177 m_toplevel->sendMaximized(size: nonwindowedState.output->availableGeometry().size() / nonwindowedState.output->scaleFactor());
178}
179
180void XdgToplevelV6Integration::handleUnsetMaximized()
181{
182 if (!m_item->view()->isPrimary())
183 return;
184
185 // If no prior windowed size was recorded, send a 0x0 configure event
186 // to allow the client to choose its preferred size.
187 if (windowedGeometry.initialWindowSize.isValid())
188 m_toplevel->sendUnmaximized(size: windowedGeometry.initialWindowSize);
189 else
190 m_toplevel->sendUnmaximized();
191}
192
193void XdgToplevelV6Integration::handleMaximizedChanged()
194{
195 if (m_toplevel->maximized()) {
196 QWaylandOutput *output = m_item->view()->output();
197 m_item->moveItem()->setPosition(output->position() + output->availableGeometry().topLeft());
198 } else {
199 m_item->moveItem()->setPosition(windowedGeometry.initialPosition);
200 }
201}
202
203void XdgToplevelV6Integration::handleSetFullscreen()
204{
205 if (!m_item->view()->isPrimary())
206 return;
207
208 QVector<QWaylandXdgToplevelV6::State> states = m_toplevel->states();
209
210 if (!states.contains(t: QWaylandXdgToplevelV6::State::FullscreenState) && !states.contains(t: QWaylandXdgToplevelV6::State::MaximizedState)) {
211 windowedGeometry.initialWindowSize = m_xdgSurface->windowGeometry().size();
212 windowedGeometry.initialPosition = m_item->moveItem()->position();
213 }
214
215 // Any prior output-resize handlers are irrelevant at this point.
216 disconnect(nonwindowedState.sizeChangedConnection);
217 nonwindowedState.output = m_item->view()->output();
218 nonwindowedState.sizeChangedConnection = connect(sender: nonwindowedState.output, signal: &QWaylandOutput::geometryChanged, receiver: this, slot: &XdgToplevelV6Integration::handleFullscreenSizeChanged);
219 handleFullscreenSizeChanged();
220}
221
222void XdgToplevelV6Integration::handleFullscreenSizeChanged()
223{
224 // Insurance against handleToplevelDestroyed() not managing to disconnect this
225 // handler in time.
226 if (m_toplevel == nullptr)
227 return;
228
229 m_toplevel->sendFullscreen(size: nonwindowedState.output->geometry().size() / nonwindowedState.output->scaleFactor());
230}
231
232void XdgToplevelV6Integration::handleUnsetFullscreen()
233{
234 if (!m_item->view()->isPrimary())
235 return;
236
237 // If no prior windowed size was recorded, send a 0x0 configure event
238 // to allow the client to choose its preferred size.
239 if (windowedGeometry.initialWindowSize.isValid())
240 m_toplevel->sendUnmaximized(size: windowedGeometry.initialWindowSize);
241 else
242 m_toplevel->sendUnmaximized();
243}
244
245void XdgToplevelV6Integration::handleFullscreenChanged()
246{
247 if (m_toplevel->fullscreen()) {
248 QWaylandOutput *output = m_item->view()->output();
249 m_item->moveItem()->setPosition(output->position() + output->geometry().topLeft());
250 } else {
251 m_item->moveItem()->setPosition(windowedGeometry.initialPosition);
252 }
253}
254
255void XdgToplevelV6Integration::handleActivatedChanged()
256{
257 if (m_toplevel->activated())
258 m_item->raise();
259}
260
261void XdgToplevelV6Integration::handleSurfaceSizeChanged()
262{
263 if (grabberState == GrabberState::Resize) {
264 qreal dx = 0;
265 qreal dy = 0;
266 if (resizeState.resizeEdges & Qt::TopEdge)
267 dy = resizeState.initialSurfaceSize.height() - m_item->surface()->destinationSize().height();
268 if (resizeState.resizeEdges & Qt::LeftEdge)
269 dx = resizeState.initialSurfaceSize.width() - m_item->surface()->destinationSize().width();
270 QPointF offset = m_item->mapFromSurface(point: {dx, dy});
271 m_item->moveItem()->setPosition(resizeState.initialPosition + offset);
272 }
273}
274
275void XdgToplevelV6Integration::handleToplevelDestroyed()
276{
277 // Disarm any handlers that might fire on the now-stale toplevel pointer
278 nonwindowedState.output = nullptr;
279 disconnect(nonwindowedState.sizeChangedConnection);
280}
281
282XdgPopupV6Integration::XdgPopupV6Integration(QWaylandQuickShellSurfaceItem *item)
283 : m_item(item)
284 , m_xdgSurface(qobject_cast<QWaylandXdgSurfaceV6 *>(object: item->shellSurface()))
285 , m_popup(m_xdgSurface->popup())
286{
287 Q_ASSERT(m_popup);
288
289 m_item->setSurface(m_xdgSurface->surface());
290 handleGeometryChanged();
291
292 connect(sender: m_popup, signal: &QWaylandXdgPopupV6::configuredGeometryChanged, receiver: this, slot: &XdgPopupV6Integration::handleGeometryChanged);
293 connect(sender: m_xdgSurface->shell(), signal: &QWaylandXdgShellV6::popupCreated, context: this, slot: [item](QWaylandXdgPopupV6 *popup, QWaylandXdgSurfaceV6 *){
294 handlePopupCreated(parentItem: item, popup);
295 });
296}
297
298void XdgPopupV6Integration::handleGeometryChanged()
299{
300 if (m_item->view()->output()) {
301 const QPoint windowOffset = m_popup->parentXdgSurface()->windowGeometry().topLeft();
302 const QPoint surfacePosition = m_popup->unconstrainedPosition() + windowOffset;
303 const QPoint itemPosition = m_item->mapFromSurface(point: surfacePosition).toPoint();
304 //TODO: positioner size or other size...?
305 //TODO check positioner constraints etc... sliding, flipping
306 m_item->moveItem()->setPosition(itemPosition);
307 } else {
308 qWarning() << "XdgPopupV6Integration popup item without output" << m_item;
309 }
310}
311
312}
313
314#endif // QT_DEPRECATED_SINCE(5, 15)
315
316QT_END_NAMESPACE
317

source code of qtwayland/src/compositor/extensions/qwaylandxdgshellv6integration.cpp