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
qwaylandxdgshell.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:critical reason:network-protocol
4
7
8#if QT_CONFIG(wayland_compositor_quick)
9#include "qwaylandxdgshellintegration_p.h"
10#endif
11#include <QtWaylandCompositor/private/qwaylandutils_p.h>
12
14
15#include <QtWaylandCompositor/QWaylandCompositor>
16#include <QtWaylandCompositor/QWaylandSeat>
17#include <QtWaylandCompositor/QWaylandSurface>
18#include <QtWaylandCompositor/QWaylandSurfaceRole>
19#include <QtWaylandCompositor/QWaylandResource>
20
21#include <QtCore/QObject>
22
23#include <algorithm>
24
25QT_BEGIN_NAMESPACE
26
27QWaylandXdgShellPrivate::QWaylandXdgShellPrivate()
28{
29}
30
31void QWaylandXdgShellPrivate::ping(QtWaylandServer::xdg_wm_base::Resource *resource, uint32_t serial)
32{
33 m_pings.insert(serial);
34 send_ping(resource->handle, serial);
35}
36
37void QWaylandXdgShellPrivate::registerXdgSurface(QWaylandXdgSurface *xdgSurface)
38{
39 m_xdgSurfaces.insert(xdgSurface->surface()->client()->client(), xdgSurface);
40}
41
42void QWaylandXdgShellPrivate::unregisterXdgSurface(QWaylandXdgSurface *xdgSurface)
43{
44 auto xdgSurfacePrivate = QWaylandXdgSurfacePrivate::get(xdgSurface);
45 if (!m_xdgSurfaces.remove(xdgSurfacePrivate->resource()->client(), xdgSurface))
46 qWarning("%s Unexpected state. Can't find registered xdg surface\n", Q_FUNC_INFO);
47}
48
49QWaylandXdgSurface *QWaylandXdgShellPrivate::xdgSurfaceFromSurface(QWaylandSurface *surface)
50{
51 for (QWaylandXdgSurface *xdgSurface : std::as_const(m_xdgSurfaces)) {
52 if (surface == xdgSurface->surface())
53 return xdgSurface;
54 }
55 return nullptr;
56}
57
58void QWaylandXdgShellPrivate::xdg_wm_base_destroy(Resource *resource)
59{
60 if (!m_xdgSurfaces.values(resource->client()).empty())
61 wl_resource_post_error(resource->handle, XDG_WM_BASE_ERROR_DEFUNCT_SURFACES,
62 "xdg_shell was destroyed before children");
63
64 wl_resource_destroy(resource->handle);
65}
66
67void QWaylandXdgShellPrivate::xdg_wm_base_create_positioner(QtWaylandServer::xdg_wm_base::Resource *resource, uint32_t id)
68{
69 QWaylandResource positionerResource(wl_resource_create(resource->client(), &xdg_positioner_interface,
70 wl_resource_get_version(resource->handle), id));
71
72 new QWaylandXdgPositioner(positionerResource);
73}
74
75void QWaylandXdgShellPrivate::xdg_wm_base_get_xdg_surface(Resource *resource, uint32_t id, wl_resource *surfaceResource)
76{
77 Q_Q(QWaylandXdgShell);
78 QWaylandSurface *surface = QWaylandSurface::fromResource(surfaceResource);
79
80 if (surface->hasContent()) {
81 //TODO: According to the spec, this is a client error, but there's no appropriate error code
82 qWarning() << "get_xdg_surface requested on a xdg_surface with content";
83 }
84
85 QWaylandResource xdgSurfaceResource(wl_resource_create(resource->client(), &xdg_surface_interface,
86 wl_resource_get_version(resource->handle), id));
87
88 QWaylandXdgSurface *xdgSurface = new QWaylandXdgSurface(q, surface, xdgSurfaceResource);
89
90 registerXdgSurface(xdgSurface);
91 emit q->xdgSurfaceCreated(xdgSurface);
92}
93
94void QWaylandXdgShellPrivate::xdg_wm_base_pong(Resource *resource, uint32_t serial)
95{
96 Q_UNUSED(resource);
97 Q_Q(QWaylandXdgShell);
98 if (m_pings.remove(serial))
99 emit q->pong(serial);
100 else
101 qWarning("Received an unexpected pong!");
102}
103
104/*!
105 * \qmltype XdgShell
106 * \nativetype QWaylandXdgShell
107 * \inqmlmodule QtWayland.Compositor.XdgShell
108 * \since 5.12
109 * \brief Provides an extension for desktop-style user interfaces.
110 *
111 * The XdgShell extension provides a way to associate a XdgToplevel or XdgPopup
112 * with a regular Wayland surface. Using the XdgToplevel interface, the client
113 * can request that the surface is resized, moved, and so on.
114 *
115 * XdgShell corresponds to the Wayland interface, \c xdg_shell.
116 *
117 * To provide the functionality of the shell extension in a compositor, create
118 * an instance of the XdgShell component and add it to the list of extensions
119 * supported by the compositor:
120 *
121 * \qml
122 * import QtWayland.Compositor.XdgShell
123 *
124 * WaylandCompositor {
125 * XdgShell {
126 * // ...
127 * }
128 * }
129 * \endqml
130 */
131
132/*!
133 * \class QWaylandXdgShell
134 * \inmodule QtWaylandCompositor
135 * \since 5.12
136 * \brief The QWaylandXdgShell class is an extension for desktop-style user interfaces.
137 *
138 * The QWaylandXdgShell extension provides a way to associate a QWaylandXdgToplevel or
139 * QWaylandXdgPopup with a regular Wayland surface. Using the QWaylandXdgToplevel interface,
140 * the client can request that the surface is resized, moved, and so on.
141 *
142 * QWaylandXdgShell corresponds to the Wayland interface, \c xdg_shell.
143 */
144
145/*!
146 * Constructs a QWaylandXdgShell object.
147 */
148QWaylandXdgShell::QWaylandXdgShell()
149 : QWaylandShellTemplate<QWaylandXdgShell>(*new QWaylandXdgShellPrivate())
150{
151}
152
153/*!
154 * Constructs a QWaylandXdgShell object for the provided \a compositor.
155 */
156QWaylandXdgShell::QWaylandXdgShell(QWaylandCompositor *compositor)
157 : QWaylandShellTemplate<QWaylandXdgShell>(compositor, *new QWaylandXdgShellPrivate())
158{
159}
160
161/*!
162 * Initializes the shell extension.
163 */
164void QWaylandXdgShell::initialize()
165{
166 Q_D(QWaylandXdgShell);
167 QWaylandShellTemplate::initialize();
168 QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
169 if (!compositor) {
170 qWarning() << "Failed to find QWaylandCompositor when initializing QWaylandXdgShell";
171 return;
172 }
173 d->init(compositor->display(), 1);
174
175 handleSeatChanged(compositor->defaultSeat(), nullptr);
176
177 connect(compositor, &QWaylandCompositor::defaultSeatChanged,
178 this, &QWaylandXdgShell::handleSeatChanged);
179
180 // Support the dialog extension unconditionally.
181 QObject *dialogExtension = new QWaylandXdgDialogV1Global(compositor);
182 dialogExtension->setParent(this);
183}
184
185/*!
186 * Returns the Wayland interface for the QWaylandXdgShell.
187 */
188const struct wl_interface *QWaylandXdgShell::interface()
189{
190 return QWaylandXdgShellPrivate::interface();
191}
192
193QByteArray QWaylandXdgShell::interfaceName()
194{
195 return QWaylandXdgShellPrivate::interfaceName();
196}
197
198/*!
199 * \qmlmethod void XdgShell::ping(WaylandClient client)
200 *
201 * Sends a ping event to \a client. If the client replies to the event the
202 * \l pong signal will be emitted.
203 */
204
205/*!
206 * Sends a ping event to \a client. If the client replies to the event the
207 * \l pong signal will be emitted.
208 */
209uint QWaylandXdgShell::ping(QWaylandClient *client)
210{
211 Q_D(QWaylandXdgShell);
212
213 QWaylandCompositor *compositor = static_cast<QWaylandCompositor *>(extensionContainer());
214 Q_ASSERT(compositor);
215
216 uint32_t serial = compositor->nextSerial();
217
218 QWaylandXdgShellPrivate::Resource *clientResource = d->resourceMap().value(client->client(), nullptr);
219 Q_ASSERT(clientResource);
220
221 d->ping(clientResource, serial);
222 return serial;
223}
224
225void QWaylandXdgShell::handleSeatChanged(QWaylandSeat *newSeat, QWaylandSeat *oldSeat)
226{
227 if (oldSeat != nullptr) {
228 disconnect(oldSeat, &QWaylandSeat::keyboardFocusChanged,
229 this, &QWaylandXdgShell::handleFocusChanged);
230 }
231
232 if (newSeat != nullptr) {
233 connect(newSeat, &QWaylandSeat::keyboardFocusChanged,
234 this, &QWaylandXdgShell::handleFocusChanged);
235 }
236}
237
238void QWaylandXdgShell::handleFocusChanged(QWaylandSurface *newSurface, QWaylandSurface *oldSurface)
239{
240 Q_D(QWaylandXdgShell);
241
242 QWaylandXdgSurface *newXdgSurface = d->xdgSurfaceFromSurface(newSurface);
243 QWaylandXdgSurface *oldXdgSurface = d->xdgSurfaceFromSurface(oldSurface);
244
245 if (newXdgSurface)
246 QWaylandXdgSurfacePrivate::get(newXdgSurface)->handleFocusReceived();
247
248 if (oldXdgSurface)
249 QWaylandXdgSurfacePrivate::get(oldXdgSurface)->handleFocusLost();
250}
251
252QWaylandXdgSurfacePrivate::QWaylandXdgSurfacePrivate()
253{
254}
255
256void QWaylandXdgSurfacePrivate::setWindowType(Qt::WindowType windowType)
257{
258 if (m_windowType == windowType)
259 return;
260
261 m_windowType = windowType;
262
263 Q_Q(QWaylandXdgSurface);
264 emit q->windowTypeChanged();
265}
266
267void QWaylandXdgSurfacePrivate::handleFocusLost()
268{
269 if (m_toplevel)
270 QWaylandXdgToplevelPrivate::get(m_toplevel)->handleFocusLost();
271}
272
273void QWaylandXdgSurfacePrivate::handleFocusReceived()
274{
275 if (m_toplevel)
276 QWaylandXdgToplevelPrivate::get(m_toplevel)->handleFocusReceived();
277}
278
279QRect QWaylandXdgSurfacePrivate::calculateFallbackWindowGeometry() const
280{
281 // TODO: The unset window geometry should include subsurfaces as well, so this solution
282 // won't work too well on those kinds of clients.
283 return QRect(QPoint(), m_surface->destinationSize());
284}
285
286void QWaylandXdgSurfacePrivate::updateFallbackWindowGeometry()
287{
288 Q_Q(QWaylandXdgSurface);
289 if (!m_unsetWindowGeometry)
290 return;
291
292 const QRect unsetGeometry = calculateFallbackWindowGeometry();
293 if (unsetGeometry == m_windowGeometry)
294 return;
295
296 m_windowGeometry = unsetGeometry;
297 emit q->windowGeometryChanged();
298}
299
300void QWaylandXdgSurfacePrivate::xdg_surface_destroy_resource(QtWaylandServer::xdg_surface::Resource *resource)
301{
302 Q_UNUSED(resource);
303 Q_Q(QWaylandXdgSurface);
304 QWaylandXdgShellPrivate::get(m_xdgShell)->unregisterXdgSurface(q);
305 delete q;
306}
307
308void QWaylandXdgSurfacePrivate::xdg_surface_destroy(QtWaylandServer::xdg_surface::Resource *resource)
309{
310 wl_resource_destroy(resource->handle);
311}
312
313void QWaylandXdgSurfacePrivate::xdg_surface_get_toplevel(QtWaylandServer::xdg_surface::Resource *resource, uint32_t id)
314{
315 Q_Q(QWaylandXdgSurface);
316
317 if (m_toplevel || m_popup) {
318 wl_resource_post_error(resource->handle, XDG_SURFACE_ERROR_ALREADY_CONSTRUCTED,
319 "xdg_surface already has a role object");
320 return;
321 }
322
323 if (!m_surface->setRole(QWaylandXdgToplevel::role(), resource->handle, XDG_WM_BASE_ERROR_ROLE))
324 return;
325
326 QWaylandResource topLevelResource(wl_resource_create(resource->client(), &xdg_toplevel_interface,
327 wl_resource_get_version(resource->handle), id));
328
329 m_toplevel = new QWaylandXdgToplevel(q, topLevelResource);
330 emit q->toplevelCreated();
331 emit m_xdgShell->toplevelCreated(m_toplevel, q);
332 q->connect(m_toplevel, &QWaylandXdgToplevel::modalChanged, q, [q, this](){
333 q->setModal(m_toplevel->isModal());
334 });
335}
336
337void QWaylandXdgSurfacePrivate::xdg_surface_get_popup(QtWaylandServer::xdg_surface::Resource *resource, uint32_t id, wl_resource *parentResource, wl_resource *positionerResource)
338{
339 Q_Q(QWaylandXdgSurface);
340
341 if (m_toplevel || m_popup) {
342 wl_resource_post_error(resource->handle, XDG_SURFACE_ERROR_ALREADY_CONSTRUCTED,
343 "xdg_surface already has a role object");
344 return;
345 }
346
347 QWaylandXdgSurface *parent = QWaylandXdgSurface::fromResource(parentResource);
348 if (!parent) {
349 wl_resource_post_error(resource->handle, XDG_WM_BASE_ERROR_INVALID_POPUP_PARENT,
350 "xdg_surface.get_popup with invalid popup parent");
351 return;
352 }
353
354 QWaylandXdgPositioner *positioner = QWaylandXdgPositioner::fromResource(positionerResource);
355 if (!positioner) {
356 wl_resource_post_error(resource->handle, XDG_WM_BASE_ERROR_INVALID_POSITIONER,
357 "xdg_surface.get_popup without positioner");
358 return;
359 }
360
361 if (!positioner->m_data.isComplete()) {
362 QWaylandXdgPositionerData p = positioner->m_data;
363 wl_resource_post_error(resource->handle, XDG_WM_BASE_ERROR_INVALID_POSITIONER,
364 "xdg_surface.get_popup with invalid positioner (size: %dx%d, anchorRect: %dx%d)",
365 p.size.width(), p.size.height(), p.anchorRect.width(), p.anchorRect.height());
366 return;
367 }
368
369 QRect anchorBounds(QPoint(0, 0), parent->windowGeometry().size());
370 if (!anchorBounds.contains(positioner->m_data.anchorRect)) {
371 // TODO: this is a protocol error and should ideally be handled like this:
372 //wl_resource_post_error(resource->handle, XDG_WM_BASE_ERROR_INVALID_POSITIONER,
373 // "xdg_positioner anchor rect extends beyound its parent's window geometry");
374 //return;
375 // However, our own clients currently do this, so we'll settle for a gentle warning instead.
376 qCWarning(qLcWaylandCompositor) << "Ignoring client protocol error: xdg_positioner anchor"
377 << "rect extends beyond its parent's window geometry";
378 }
379
380 if (!m_surface->setRole(QWaylandXdgPopup::role(), resource->handle, XDG_WM_BASE_ERROR_ROLE))
381 return;
382
383 QWaylandResource popupResource(wl_resource_create(resource->client(), &xdg_popup_interface,
384 wl_resource_get_version(resource->handle), id));
385
386 m_popup = new QWaylandXdgPopup(q, parent, positioner, popupResource);
387 emit q->popupCreated();
388 emit m_xdgShell->popupCreated(m_popup, q);
389}
390
391void QWaylandXdgSurfacePrivate::xdg_surface_ack_configure(QtWaylandServer::xdg_surface::Resource *resource, uint32_t serial)
392{
393 if (m_toplevel) {
394 QWaylandXdgToplevelPrivate::get(m_toplevel)->handleAckConfigure(serial);
395 } else if (m_popup) {
396 QWaylandXdgPopupPrivate::get(m_popup)->handleAckConfigure(serial);
397 } else {
398 wl_resource_post_error(resource->handle, XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
399 "ack_configure requested on an unconstructed xdg_surface");
400 }
401}
402
403void QWaylandXdgSurfacePrivate::xdg_surface_set_window_geometry(QtWaylandServer::xdg_surface::Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height)
404{
405 Q_Q(QWaylandXdgSurface);
406
407 if (!q->surface()->role()) {
408 wl_resource_post_error(resource->handle, XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
409 "set_window_geometry requested on an unconstructed xdg_surface");
410 return;
411 }
412
413 if (width <= 0 || height <= 0) {
414 // The protocol spec says "setting an invalid size will raise an error". But doesn't tell
415 // which error to raise, and there's no fitting error in the xdg_surface_error enum.
416 // So until this is fixed, just output a warning and return.
417 qWarning() << "Invalid (non-positive) dimensions received in set_window_geometry";
418 return;
419 }
420
421 m_unsetWindowGeometry = false;
422
423 QRect geometry(x, y, width, height);
424
425 if (m_windowGeometry == geometry)
426 return;
427
428 m_windowGeometry = geometry;
429 emit q->windowGeometryChanged();
430}
431
432/*!
433 * \qmltype XdgSurface
434 * \nativetype QWaylandXdgSurface
435 * \inqmlmodule QtWayland.Compositor.XdgShell
436 * \since 5.12
437 * \brief XdgSurface provides desktop-style compositor-specific features to an xdg surface.
438 *
439 * This type is part of the \l{XdgShell} extension and provides a way to
440 * extend the functionality of an existing \l{WaylandSurface} with features
441 * specific to desktop-style compositors, such as resizing and moving the
442 * surface.
443 *
444 * It corresponds to the Wayland interface \c xdg_surface.
445 */
446
447/*!
448 * \class QWaylandXdgSurface
449 * \inmodule QtWaylandCompositor
450 * \since 5.12
451 * \brief The QWaylandXdgSurface class provides desktop-style compositor-specific features to an xdg surface.
452 *
453 * This class is part of the QWaylandXdgShell extension and provides a way to
454 * extend the functionality of an existing QWaylandSurface with features
455 * specific to desktop-style compositors, such as resizing and moving the
456 * surface.
457 *
458 * It corresponds to the Wayland interface \c xdg_surface.
459 */
460
461/*!
462 * Constructs a QWaylandXdgSurface.
463 */
464QWaylandXdgSurface::QWaylandXdgSurface()
465 : QWaylandShellSurfaceTemplate<QWaylandXdgSurface>(*new QWaylandXdgSurfacePrivate)
466{
467}
468
469/*!
470 * Constructs a QWaylandXdgSurface for \a surface and initializes it with the
471 * given \a xdgShell, \a surface, and resource \a res.
472 */
473QWaylandXdgSurface::QWaylandXdgSurface(QWaylandXdgShell *xdgShell, QWaylandSurface *surface, const QWaylandResource &res)
474 : QWaylandShellSurfaceTemplate<QWaylandXdgSurface>(*new QWaylandXdgSurfacePrivate)
475{
476 initialize(xdgShell, surface, res);
477}
478
479/*!
480 * \qmlmethod void XdgSurface::initialize(object xdgShell, object surface, object client, int id)
481 *
482 * Initializes the XdgSurface, associating it with the given \a xdgShell, \a surface,
483 * \a client, and \a id.
484 */
485
486/*!
487 * Initializes the QWaylandXdgSurface, associating it with the given \a xdgShell, \a surface
488 * and \a resource.
489 */
490void QWaylandXdgSurface::initialize(QWaylandXdgShell *xdgShell, QWaylandSurface *surface, const QWaylandResource &resource)
491{
492 Q_D(QWaylandXdgSurface);
493 d->m_xdgShell = xdgShell;
494 d->m_surface = surface;
495 d->init(resource.resource());
496 setExtensionContainer(surface);
497 d->m_windowGeometry = d->calculateFallbackWindowGeometry();
498 connect(surface, &QWaylandSurface::destinationSizeChanged, this, &QWaylandXdgSurface::handleSurfaceSizeChanged);
499 connect(surface, &QWaylandSurface::bufferScaleChanged, this, &QWaylandXdgSurface::handleBufferScaleChanged);
500 emit shellChanged();
501 emit surfaceChanged();
502 QWaylandCompositorExtension::initialize();
503}
504
505/*!
506 * \qmlproperty enum XdgSurface::windowType
507 *
508 * This property holds the window type of the XdgSurface.
509 */
510Qt::WindowType QWaylandXdgSurface::windowType() const
511{
512 Q_D(const QWaylandXdgSurface);
513 return d->m_windowType;
514}
515
516/*!
517 * \qmlproperty rect XdgSurface::windowGeometry
518 *
519 * This property holds the window geometry of this XdgSurface. The window
520 * geometry describes the window's visible bounds from the user's perspective.
521 * The geometry includes title bars and borders if drawn by the client, but
522 * excludes drop shadows. It is meant to be used for aligning and tiling
523 * windows.
524 */
525
526/*!
527 * \property QWaylandXdgSurface::windowGeometry
528 *
529 * This property holds the window geometry of the QWaylandXdgSurface. The window
530 * geometry describes the window's visible bounds from the user's perspective.
531 * The geometry includes title bars and borders if drawn by the client, but
532 * excludes drop shadows. It is meant to be used for aligning and tiling
533 * windows.
534 */
535QRect QWaylandXdgSurface::windowGeometry() const
536{
537 Q_D(const QWaylandXdgSurface);
538 return d->m_windowGeometry;
539}
540
541/*!
542 * \internal
543 */
544void QWaylandXdgSurface::initialize()
545{
546 QWaylandCompositorExtension::initialize();
547}
548
549void QWaylandXdgSurface::handleSurfaceSizeChanged()
550{
551 Q_D(QWaylandXdgSurface);
552 d->updateFallbackWindowGeometry();
553}
554
555void QWaylandXdgSurface::handleBufferScaleChanged()
556{
557 Q_D(QWaylandXdgSurface);
558 d->updateFallbackWindowGeometry();
559}
560
561/*!
562 * \qmlproperty XdgShell XdgSurface::shell
563 *
564 * This property holds the shell associated with this XdgSurface.
565 */
566
567/*!
568 * \property QWaylandXdgSurface::shell
569 *
570 * This property holds the shell associated with this QWaylandXdgSurface.
571 */
572QWaylandXdgShell *QWaylandXdgSurface::shell() const
573{
574 Q_D(const QWaylandXdgSurface);
575 return d->m_xdgShell;
576}
577
578/*!
579 * \qmlproperty WaylandSurface XdgSurface::surface
580 *
581 * This property holds the surface associated with this XdgSurface.
582 */
583
584/*!
585 * \property QWaylandXdgSurface::surface
586 *
587 * This property holds the surface associated with this QWaylandXdgSurface.
588 */
589QWaylandSurface *QWaylandXdgSurface::surface() const
590{
591 Q_D(const QWaylandXdgSurface);
592 return d->m_surface;
593}
594
595/*!
596 * \qmlproperty XdgToplevel XdgSurface::toplevel
597 *
598 * This property holds the properties and methods that are specific to the
599 * toplevel XdgSurface.
600 *
601 * \sa popup, XdgShell::toplevelCreated
602 */
603
604/*!
605 * \property QWaylandXdgSurface::toplevel
606 *
607 * This property holds the properties and methods that are specific to the
608 * toplevel QWaylandXdgSurface.
609 *
610 * \sa QWaylandXdgSurface::popup, QWaylandXdgShell::toplevelCreated
611 */
612QWaylandXdgToplevel *QWaylandXdgSurface::toplevel() const
613{
614 Q_D(const QWaylandXdgSurface);
615 return d->m_toplevel;
616}
617
618/*!
619 * \qmlproperty XdgPopup XdgSurface::popup
620 *
621 * This property holds the properties and methods that are specific to the
622 * popup XdgSurface.
623 *
624 * \sa toplevel, XdgShell::popupCreated
625 */
626
627/*!
628 * \property QWaylandXdgSurface::popup
629 *
630 * This property holds the properties and methods that are specific to the
631 * popup QWaylandXdgSurface.
632 *
633 * \sa QWaylandXdgSurface::toplevel, QWaylandXdgShell::popupCreated
634 */
635QWaylandXdgPopup *QWaylandXdgSurface::popup() const
636{
637 Q_D(const QWaylandXdgSurface);
638 return d->m_popup;
639}
640
641/*!
642 * Returns the Wayland interface for the QWaylandXdgSurface.
643 */
644const wl_interface *QWaylandXdgSurface::interface()
645{
646 return QWaylandXdgSurfacePrivate::interface();
647}
648
649/*!
650 * \internal
651 */
652QByteArray QWaylandXdgSurface::interfaceName()
653{
654 return QWaylandXdgSurfacePrivate::interfaceName();
655}
656
657/*!
658 * Returns the QWaylandXdgSurface corresponding to the \a resource.
659 */
660QWaylandXdgSurface *QWaylandXdgSurface::fromResource(wl_resource *resource)
661{
662 if (auto p = QtWayland::fromResource<QWaylandXdgSurfacePrivate *>(resource))
663 return p->q_func();
664 return nullptr;
665}
666
667#if QT_CONFIG(wayland_compositor_quick)
668QWaylandQuickShellIntegration *QWaylandXdgSurface::createIntegration(QWaylandQuickShellSurfaceItem *item)
669{
670 Q_D(const QWaylandXdgSurface);
671
672 if (d->m_toplevel)
673 return new QtWayland::XdgToplevelIntegration(item);
674
675 if (d->m_popup)
676 return new QtWayland::XdgPopupIntegration(item);
677
678 return nullptr;
679}
680#endif
681
682/*!
683 * \qmltype XdgToplevel
684 * \nativetype QWaylandXdgToplevel
685 * \inqmlmodule QtWayland.Compositor.XdgShell
686 * \since 5.12
687 * \brief XdgToplevel represents the toplevel window specific parts of an xdg surface.
688 *
689 * This type is part of the \l{XdgShell} extension and provides a way to
690 * extend the functionality of an XdgSurface with features
691 * specific to desktop-style windows.
692 *
693 * It corresponds to the Wayland interface \c xdg_toplevel.
694 */
695
696/*!
697 * \class QWaylandXdgToplevel
698 * \inmodule QtWaylandCompositor
699 * \since 5.12
700 * \brief The QWaylandXdgToplevel class represents the toplevel window specific parts of an xdg surface.
701 *
702 * This class is part of the QWaylandXdgShell extension and provides a way to
703 * extend the functionality of an QWaylandXdgSurface with features
704 * specific to desktop-style windows.
705 *
706 * It corresponds to the Wayland interface \c xdg_toplevel.
707 */
708
709/*!
710 * Constructs a QWaylandXdgToplevel for the given \a xdgSurface and \a resource.
711 */
712QWaylandXdgToplevel::QWaylandXdgToplevel(QWaylandXdgSurface *xdgSurface, QWaylandResource &resource)
713 : QObject(*new QWaylandXdgToplevelPrivate(xdgSurface, resource))
714{
715 QList<QWaylandXdgToplevel::State> states;
716 sendConfigure({0, 0}, states);
717}
718
719QWaylandXdgToplevel::~QWaylandXdgToplevel()
720{
721 Q_D(QWaylandXdgToplevel);
722 // Usually, the decoration is destroyed by the client (according to the protocol),
723 // but if the client misbehaves, or is shut down, we need to clean up here.
724 if (Q_UNLIKELY(d->m_decoration))
725 wl_resource_destroy(d->m_decoration->resource()->handle);
726 Q_ASSERT(!d->m_decoration);
727}
728
729/*!
730 * \qmlproperty XdgSurface XdgToplevel::xdgSurface
731 *
732 * This property holds the XdgSurface for this XdgToplevel.
733 */
734
735/*!
736 * \property QWaylandXdgToplevel::xdgSurface
737 *
738 * This property holds the QWaylandXdgSurface for this QWaylandXdgToplevel.
739 */
740QWaylandXdgSurface *QWaylandXdgToplevel::xdgSurface() const
741{
742 Q_D(const QWaylandXdgToplevel);
743 return d->m_xdgSurface;
744}
745
746/*!
747 * \qmlproperty XdgToplevel XdgToplevel::parentToplevel
748 *
749 * This property holds the XdgToplevel parent of this XdgToplevel.
750 */
751
752/*!
753 * \property QWaylandXdgToplevel::parentToplevel
754 *
755 * This property holds the XdgToplevel parent of this XdgToplevel.
756 *
757 */
758QWaylandXdgToplevel *QWaylandXdgToplevel::parentToplevel() const
759{
760 Q_D(const QWaylandXdgToplevel);
761 return d->m_parentToplevel;
762}
763
764/*!
765 * \qmlproperty string XdgToplevel::title
766 *
767 * This property holds the title of the XdgToplevel.
768 */
769
770/*!
771 * \property QWaylandXdgToplevel::title
772 *
773 * This property holds the title of the QWaylandXdgToplevel.
774 */
775QString QWaylandXdgToplevel::title() const
776{
777 Q_D(const QWaylandXdgToplevel);
778 return d->m_title;
779}
780
781/*!
782 * \qmlproperty string XdgToplevel::appId
783 *
784 * This property holds the app id of the XdgToplevel. It corresponds to the
785 * \l {QGuiApplication::desktopFileName}{desktopFileName} in Qt.
786 */
787
788/*!
789 * \property QWaylandXdgToplevel::appId
790 *
791 * This property holds the app id of the QWaylandXdgToplevel. It corresponds to
792 * the \l {QGuiApplication::desktopFileName}{desktopFileName} in Qt.
793 */
794QString QWaylandXdgToplevel::appId() const
795{
796 Q_D(const QWaylandXdgToplevel);
797 return d->m_appId;
798}
799
800/*!
801 * \qmlproperty size XdgToplevel::maxSize
802 *
803 * This property holds the maximum size of the XdgToplevel as requested by the client.
804 *
805 * The compositor is free to ignore this value and request a larger size.
806 */
807
808/*!
809 * \property QWaylandXdgToplevel::maxSize
810 *
811 * This property holds the maximum size of the QWaylandXdgToplevel.
812 *
813 * The compositor is free to ignore this value and request a larger size.
814 */
815QSize QWaylandXdgToplevel::maxSize() const
816{
817 Q_D(const QWaylandXdgToplevel);
818 return d->m_maxSize;
819}
820
821/*!
822 * \qmlproperty size XdgToplevel::minSize
823 *
824 * This property holds the minimum size of the XdgToplevel as requested by the client.
825 *
826 * The compositor is free to ignore this value and request a smaller size.
827 */
828
829/*!
830 * \property QWaylandXdgToplevel::minSize
831 *
832 * This property holds the minimum size of the QWaylandXdgToplevel.
833 *
834 * The compositor is free to ignore this value and request a smaller size.
835 */
836QSize QWaylandXdgToplevel::minSize() const
837{
838 Q_D(const QWaylandXdgToplevel);
839 return d->m_minSize;
840}
841
842/*!
843 * \property QWaylandXdgToplevel::states
844 *
845 * This property holds the last states the client acknowledged for this QWaylandToplevel.
846 */
847QList<QWaylandXdgToplevel::State> QWaylandXdgToplevel::states() const
848{
849 Q_D(const QWaylandXdgToplevel);
850 return d->m_lastAckedConfigure.states;
851}
852
853/*!
854 * \qmlproperty bool XdgToplevel::maximized
855 *
856 * This property holds whether the client has acknowledged that it should be maximized.
857 */
858
859/*!
860 * \property QWaylandXdgToplevel::maximized
861 *
862 * This property holds whether the client has acknowledged that it should be maximized.
863 */
864bool QWaylandXdgToplevel::maximized() const
865{
866 Q_D(const QWaylandXdgToplevel);
867 return d->m_lastAckedConfigure.states.contains(QWaylandXdgToplevel::State::MaximizedState);
868}
869
870/*!
871 * \qmlproperty bool XdgToplevel::fullscreen
872 *
873 * This property holds whether the client has acknowledged that it should be fullscreen.
874 */
875
876/*!
877 * \property QWaylandXdgToplevel::fullscreen
878 *
879 * This property holds whether the client has acknowledged that it should be fullscreen.
880 */
881bool QWaylandXdgToplevel::fullscreen() const
882{
883 Q_D(const QWaylandXdgToplevel);
884 return d->m_lastAckedConfigure.states.contains(QWaylandXdgToplevel::State::FullscreenState);
885}
886
887/*!
888 * \qmlproperty bool XdgToplevel::resizing
889 *
890 * This property holds whether the client has acknowledged that it is being resized.
891 */
892
893/*!
894 * \property QWaylandXdgToplevel::resizing
895 *
896 * This property holds whether the client has acknowledged that it is being resized.
897 */
898bool QWaylandXdgToplevel::resizing() const
899{
900 Q_D(const QWaylandXdgToplevel);
901 return d->m_lastAckedConfigure.states.contains(QWaylandXdgToplevel::State::ResizingState);
902}
903
904/*!
905 * \qmlproperty bool XdgToplevel::activated
906 *
907 * This property holds whether toplevel is drawing itself as having input focus.
908 */
909
910/*!
911 * \property QWaylandXdgToplevel::activated
912 *
913 * This property holds whether toplevel is drawing itself as having input focus.
914 */
915bool QWaylandXdgToplevel::activated() const
916{
917 Q_D(const QWaylandXdgToplevel);
918 return d->m_lastAckedConfigure.states.contains(QWaylandXdgToplevel::State::ActivatedState);
919}
920
921/*!
922 * \qmlproperty bool XdgToplevel::modal
923 *
924 * This property holds whether toplevel blocks other windows from receiving input.
925 * \since 6.8
926 */
927
928/*!
929 * \property QWaylandXdgToplevel::modal
930 *
931 * This property holds whether toplevel blocks other windows from receiving input.
932 * \since 6.8
933 */
934bool QWaylandXdgToplevel::isModal() const
935{
936 Q_D(const QWaylandXdgToplevel);
937 return d->m_modal;
938}
939
940void QWaylandXdgToplevel::setModal(bool newModal)
941{
942 Q_D(QWaylandXdgToplevel);
943 if (d->m_modal == newModal)
944 return;
945 d->m_modal = newModal;
946 emit modalChanged();
947}
948
949/*!
950 * \enum QWaylandXdgToplevel::DecorationMode
951 *
952 * This enum type is used to specify the window decoration mode for toplevel windows.
953 *
954 * \value ServerSideDecoration The compositor should draw window decorations.
955 * \value ClientSideDecoration The client should draw window decorations.
956 */
957
958/*!
959 * \qmlproperty enumeration XdgToplevel::decorationMode
960 *
961 * This property holds the current window decoration mode for this toplevel.
962 *
963 * The possible values are:
964 * \value XdgToplevel.ServerSideDecoration The compositor should draw window decorations.
965 * \value XdgToplevel.ClientSideDecoration The client should draw window decorations.
966 *
967 * \sa XdgDecorationManagerV1
968 */
969
970/*!
971 * \property QWaylandXdgToplevel::decorationMode
972 *
973 * This property holds the current window decoration mode for this toplevel.
974 *
975 * \sa QWaylandXdgDecorationManagerV1
976 */
977QWaylandXdgToplevel::DecorationMode QWaylandXdgToplevel::decorationMode() const
978{
979 Q_D(const QWaylandXdgToplevel);
980 return d->m_decoration ? d->m_decoration->configuredMode() : DecorationMode::ClientSideDecoration;
981}
982
983/*!
984 * \qmlmethod size XdgToplevel::sizeForResize(size size, point delta, uint edges)
985 *
986 * Convenience for computing the new size given the current \a size, a \a delta, and
987 * the \a edges active in the drag.
988 */
989
990/*!
991 * Convenience for computing the new size given the current \a size, a \a delta, and
992 * the \a edges active in the drag.
993 */
994QSize QWaylandXdgToplevel::sizeForResize(const QSizeF &size, const QPointF &delta, Qt::Edges edges) const
995{
996 qreal width = size.width();
997 qreal height = size.height();
998 if (edges & Qt::LeftEdge)
999 width -= delta.x();
1000 else if (edges & Qt::RightEdge)
1001 width += delta.x();
1002
1003 if (edges & Qt::TopEdge)
1004 height -= delta.y();
1005 else if (edges & Qt::BottomEdge)
1006 height += delta.y();
1007
1008 QSize newSize = QSize(width, height)
1009 .expandedTo(minSize())
1010 .expandedTo({1, 1}); // We don't want to send a size of (0,0) as that means that the client decides
1011
1012 if (maxSize().isValid())
1013 newSize = newSize.boundedTo(maxSize());
1014
1015 return newSize;
1016}
1017
1018/*!
1019 * Sends a configure event to the client. Parameter \a size contains the pixel size
1020 * of the surface. A size of zero means the client is free to decide the size.
1021 * Known \a states are enumerated in QWaylandXdgToplevel::State.
1022 */
1023uint QWaylandXdgToplevel::sendConfigure(const QSize &size, const QList<QWaylandXdgToplevel::State> &states)
1024{
1025 if (!size.isValid()) {
1026 qWarning() << "Can't configure xdg_toplevel with an invalid size" << size;
1027 return 0;
1028 }
1029 Q_D(QWaylandXdgToplevel);
1030 auto statesBytes = QByteArray::fromRawData(reinterpret_cast<const char *>(states.data()),
1031 states.size() * static_cast<int>(sizeof(State)));
1032 uint32_t serial = d->m_xdgSurface->surface()->compositor()->nextSerial();
1033 d->m_pendingConfigures.append(QWaylandXdgToplevelPrivate::ConfigureEvent{states, size, serial});
1034 d->send_configure(size.width(), size.height(), statesBytes);
1035 QWaylandXdgSurfacePrivate::get(d->m_xdgSurface)->send_configure(serial);
1036 return serial;
1037}
1038
1039/*!
1040 * \qmlmethod int XdgToplevel::sendConfigure(size size, list<int> states)
1041 *
1042 * Sends a configure event to the client. \a size contains the pixel size of the surface.
1043 * A size of zero means the client is free to decide the size.
1044 *
1045 * Possible values for \a states are:
1046 * \value XdgToplevel.MaximizedState The surface is maximized
1047 * \value XdgToplevel.FullscreenState The surface is fullscreen
1048 * \value XdgToplevel.ResizingState The surface is being resized
1049 * \value XdgToplevel.ActivatedState The surface is now active
1050 */
1051uint QWaylandXdgToplevel::sendConfigure(const QSize &size, const QList<int> &states)
1052{
1053 QList<State> s;
1054 for (auto state : states)
1055 s << State(state);
1056 return sendConfigure(size, s);
1057}
1058
1059/*!
1060 * \qmlmethod void XdgToplevel::sendClose()
1061 *
1062 * Sends a close event to the client. The client may choose to ignore the event.
1063 */
1064
1065/*!
1066 * Sends a close event to the client. The client may choose to ignore the event.
1067 */
1068void QWaylandXdgToplevel::sendClose()
1069{
1070 Q_D(QWaylandXdgToplevel);
1071 d->send_close();
1072}
1073
1074/*!
1075 * \qmlmethod void XdgToplevel::sendMaximized(size size)
1076 *
1077 * Convenience for sending a configure event with the maximized state set, and
1078 * fullscreen and resizing removed. The activated state is left in its current state.
1079 *
1080 * \a size is the new size of the window.
1081 */
1082
1083/*!
1084 * Convenience for sending a configure event with the maximized state set, and
1085 * fullscreen and resizing removed. The activated state is left in its current state.
1086 *
1087 * \a size is the new size of the window.
1088 */
1089uint QWaylandXdgToplevel::sendMaximized(const QSize &size)
1090{
1091 Q_D(QWaylandXdgToplevel);
1092 QWaylandXdgToplevelPrivate::ConfigureEvent conf = d->lastSentConfigure();
1093
1094 if (!conf.states.contains(QWaylandXdgToplevel::State::MaximizedState))
1095 conf.states.append(QWaylandXdgToplevel::State::MaximizedState);
1096 conf.states.removeOne(QWaylandXdgToplevel::State::FullscreenState);
1097 conf.states.removeOne(QWaylandXdgToplevel::State::ResizingState);
1098
1099 return sendConfigure(size, conf.states);
1100}
1101
1102/*!
1103 * \qmlmethod void XdgToplevel::sendUnmaximized(size size)
1104 *
1105 * Convenience for sending a configure event with the maximized, fullscreen and
1106 * resizing states removed, and fullscreen and resizing removed. The activated
1107 * state is left in its current state.
1108 *
1109 * \a size is the new size of the window. If \a size is zero, the client decides the size.
1110 */
1111
1112/*!
1113 * Convenience for sending a configure event with the maximized, fullscreen and
1114 * resizing states removed, and fullscreen and resizing removed. The activated
1115 * state is left in its current state.
1116 *
1117 * \a size is the new size of the window. If \a size is zero, the client decides the size.
1118 */
1119uint QWaylandXdgToplevel::sendUnmaximized(const QSize &size)
1120{
1121 Q_D(QWaylandXdgToplevel);
1122 QWaylandXdgToplevelPrivate::ConfigureEvent conf = d->lastSentConfigure();
1123
1124 conf.states.removeOne(QWaylandXdgToplevel::State::MaximizedState);
1125 conf.states.removeOne(QWaylandXdgToplevel::State::FullscreenState);
1126 conf.states.removeOne(QWaylandXdgToplevel::State::ResizingState);
1127
1128 return sendConfigure(size, conf.states);
1129
1130}
1131
1132/*!
1133 * \qmlmethod void XdgToplevel::sendFullscreen(size size)
1134 *
1135 * Convenience for sending a configure event with the fullscreen state set, and
1136 * maximized and resizing removed. The activated state is left in its current state.
1137 *
1138 * \sa sendUnmaximized
1139 *
1140 * \a size is the new size of the window.
1141 */
1142
1143/*!
1144 * Convenience for sending a configure event with the fullscreen state set, and
1145 * maximized and resizing removed. The activated state is left in its current state.
1146 *
1147 * \sa sendUnmaximized
1148 *
1149 * \a size is the new size of the window.
1150 */
1151uint QWaylandXdgToplevel::sendFullscreen(const QSize &size)
1152{
1153 Q_D(QWaylandXdgToplevel);
1154 QWaylandXdgToplevelPrivate::ConfigureEvent conf = d->lastSentConfigure();
1155
1156 if (!conf.states.contains(QWaylandXdgToplevel::State::FullscreenState))
1157 conf.states.append(QWaylandXdgToplevel::State::FullscreenState);
1158 conf.states.removeOne(QWaylandXdgToplevel::State::MaximizedState);
1159 conf.states.removeOne(QWaylandXdgToplevel::State::ResizingState);
1160
1161 return sendConfigure(size, conf.states);
1162}
1163
1164/*!
1165 * \qmlmethod void XdgToplevel::sendResizing(size size)
1166 *
1167 * Convenience for sending a configure event with the resizing state set, and
1168 * maximized and fullscreen removed. The activated state is left in its current state.
1169 *
1170 * \a size is the new size of the window.
1171 */
1172
1173/*!
1174 * Convenience for sending a configure event with the resizing state set, and
1175 * maximized and fullscreen removed. The activated state is left in its current state.
1176 *
1177 * \a size is the new size of the window.
1178 */
1179uint QWaylandXdgToplevel::sendResizing(const QSize &size)
1180{
1181 Q_D(QWaylandXdgToplevel);
1182 QWaylandXdgToplevelPrivate::ConfigureEvent conf = d->lastSentConfigure();
1183
1184 if (!conf.states.contains(QWaylandXdgToplevel::State::ResizingState))
1185 conf.states.append(QWaylandXdgToplevel::State::ResizingState);
1186 conf.states.removeOne(QWaylandXdgToplevel::State::MaximizedState);
1187 conf.states.removeOne(QWaylandXdgToplevel::State::FullscreenState);
1188
1189 return sendConfigure(size, conf.states);
1190}
1191
1192/*!
1193 * Returns the surface role for the QWaylandToplevel.
1194 */
1195QWaylandSurfaceRole *QWaylandXdgToplevel::role()
1196{
1197 return &QWaylandXdgToplevelPrivate::s_role;
1198}
1199
1200/*!
1201 * Returns the QWaylandXdgToplevel corresponding to the \a resource.
1202 */
1203QWaylandXdgToplevel *QWaylandXdgToplevel::fromResource(wl_resource *resource)
1204{
1205 if (auto p = QtWayland::fromResource<QWaylandXdgToplevelPrivate *>(resource))
1206 return p->q_func();
1207 return nullptr;
1208}
1209
1210/*!
1211 * \qmlsignal XdgShell::xdgSurfaceCreated(XdgSurface xdgSurface)
1212 *
1213 * This signal is emitted when the client has created a \c xdg_surface.
1214 * Note that \a xdgSurface is not mapped, i.e. according to the \c xdg-shell
1215 * protocol it should not be displayed, until it has received a role object.
1216 *
1217 * \sa toplevelCreated(), popupCreated()
1218 */
1219
1220/*!
1221 * \fn void QWaylandXdgShell::xdgSurfaceCreated(QWaylandXdgSurface *xdgSurface)
1222 *
1223 * This signal is emitted when the client has created a \c xdg_surface.
1224 * Note that \a xdgSurface is not mapped, i.e. according to the \c xdg-shell
1225 * protocol it should not be displayed, until it has received a role object.
1226 *
1227 * \sa toplevelCreated(), popupCreated()
1228 */
1229
1230/*!
1231 * \qmlsignal XdgShell::toplevelCreated(XdgToplevel toplevel, XdgSurface xdgSurface)
1232 *
1233 * This signal is emitted when the client has created a \c xdg_toplevel.
1234 * A common use case is to let the handler of this signal instantiate a ShellSurfaceItem or
1235 * WaylandQuickItem for displaying \a toplevel in a QtQuick scene.
1236 *
1237 * \a xdgSurface is the XdgSurface \a toplevel is the role object for.
1238 */
1239
1240/*!
1241 * \fn void QWaylandXdgShell::toplevelCreated(QWaylandXdgToplevel *toplevel, QWaylandXdgSurface *xdgSurface)
1242 *
1243 * This signal is emitted when the client has created a \c xdg_toplevel.
1244 * A common use case is to let the handler of this signal instantiate a QWaylandShellSurfaceItem or
1245 * QWaylandQuickItem for displaying \a toplevel in a QtQuick scene.
1246 *
1247 * \a xdgSurface is the XdgSurface \a toplevel is the role object for.
1248 */
1249
1250/*!
1251 * \qmlsignal XdgShell::popupCreated(XdgPopup popup, XdgSurface xdgSurface)
1252 *
1253 * This signal is emitted when the client has created a \c xdg_popup.
1254 * A common use case is to let the handler of this signal instantiate a ShellSurfaceItem or
1255 * WaylandQuickItem for displaying \a popup in a QtQuick scene.
1256 *
1257 * \a xdgSurface is the XdgSurface \a popup is the role object for.
1258 */
1259
1260/*!
1261 * \fn void QWaylandXdgShell::popupCreated(QWaylandXdgPopup *popup, QWaylandXdgSurface *xdgSurface)
1262 *
1263 * This signal is emitted when the client has created a \c xdg_popup.
1264 * A common use case is to let the handler of this signal instantiate a QWaylandShellSurfaceItem or
1265 * QWaylandQuickItem for displaying \a popup in a QtQuick scene.
1266 *
1267 * \a xdgSurface is the XdgSurface \a popup is the role object for.
1268 */
1269
1270/*!
1271 * \qmlsignal XdgShell::pong(int serial)
1272 *
1273 * This signal is emitted when the client has responded to a ping event with serial, \a serial.
1274 *
1275 * \sa ping()
1276 */
1277
1278/*!
1279 * \fn void QWaylandXdgShell::pong(uint serial)
1280 *
1281 * This signal is emitted when the client has responded to a ping event with serial, \a serial.
1282 *
1283 * \sa QWaylandXdgShell::ping()
1284 */
1285
1286QList<int> QWaylandXdgToplevel::statesAsInts() const
1287{
1288 QList<int> list;
1289 const auto s = states();
1290 list.reserve(s.size());
1291 for (auto state : s) {
1292 list << static_cast<int>(state);
1293 }
1294 return list;
1295}
1296
1297QWaylandSurfaceRole QWaylandXdgToplevelPrivate::s_role("xdg_toplevel");
1298
1299QWaylandXdgToplevelPrivate::QWaylandXdgToplevelPrivate(QWaylandXdgSurface *xdgSurface, const QWaylandResource &resource)
1300 : m_xdgSurface(xdgSurface)
1301{
1302 init(resource.resource());
1303}
1304
1305void QWaylandXdgToplevelPrivate::handleAckConfigure(uint serial)
1306{
1307 Q_Q(QWaylandXdgToplevel);
1308 ConfigureEvent config;
1309 Q_FOREVER {
1310 if (m_pendingConfigures.empty()) {
1311 qWarning("Toplevel received an unexpected ack_configure!");
1312 return;
1313 }
1314
1315 // This won't work unless there always is a toplevel.configure for each xdgsurface.configure
1316 config = m_pendingConfigures.takeFirst();
1317
1318 if (config.serial == serial)
1319 break;
1320 }
1321
1322 QList<uint> changedStates;
1323 std::set_symmetric_difference(
1324 m_lastAckedConfigure.states.begin(), m_lastAckedConfigure.states.end(),
1325 config.states.begin(), config.states.end(),
1326 std::back_inserter(changedStates));
1327
1328 m_lastAckedConfigure = config;
1329
1330 for (uint state : std::as_const(changedStates)) {
1331 switch (state) {
1332 case state_maximized:
1333 emit q->maximizedChanged();
1334 break;
1335 case state_fullscreen:
1336 emit q->fullscreenChanged();
1337 break;
1338 case state_resizing:
1339 emit q->resizingChanged();
1340 break;
1341 case state_activated:
1342 emit q->activatedChanged();
1343 break;
1344 }
1345 }
1346
1347 if (!changedStates.empty())
1348 emit q->statesChanged();
1349}
1350
1351void QWaylandXdgToplevelPrivate::handleFocusLost()
1352{
1353 Q_Q(QWaylandXdgToplevel);
1354 QWaylandXdgToplevelPrivate::ConfigureEvent current = lastSentConfigure();
1355 current.states.removeOne(QWaylandXdgToplevel::State::ActivatedState);
1356 q->sendConfigure(current.size, current.states);
1357}
1358
1359void QWaylandXdgToplevelPrivate::handleFocusReceived()
1360{
1361 Q_Q(QWaylandXdgToplevel);
1362 QWaylandXdgToplevelPrivate::ConfigureEvent current = lastSentConfigure();
1363 if (!current.states.contains(QWaylandXdgToplevel::State::ActivatedState)) {
1364 current.states.push_back(QWaylandXdgToplevel::State::ActivatedState);
1365 q->sendConfigure(current.size, current.states);
1366 }
1367}
1368
1369Qt::Edges QWaylandXdgToplevelPrivate::convertToEdges(resize_edge edge)
1370{
1371 return Qt::Edges(((edge & 0b1100) >> 1) | ((edge & 0b0010) << 2) | (edge & 0b0001));
1372}
1373
1374void QWaylandXdgToplevelPrivate::xdg_toplevel_destroy_resource(QtWaylandServer::xdg_toplevel::Resource *resource)
1375{
1376 Q_UNUSED(resource);
1377 Q_Q(QWaylandXdgToplevel);
1378 delete q;
1379}
1380
1381void QWaylandXdgToplevelPrivate::xdg_toplevel_destroy(QtWaylandServer::xdg_toplevel::Resource *resource)
1382{
1383 if (Q_UNLIKELY(m_decoration))
1384 qWarning() << "Client error: xdg_toplevel destroyed before its decoration object";
1385
1386 wl_resource_destroy(resource->handle);
1387 //TODO: Should the xdg surface be desroyed as well? Or is it allowed to recreate a new toplevel for it?
1388}
1389
1390void QWaylandXdgToplevelPrivate::xdg_toplevel_set_parent(QtWaylandServer::xdg_toplevel::Resource *resource, wl_resource *parent)
1391{
1392 Q_UNUSED(resource);
1393 QWaylandXdgToplevel *parentToplevel = QWaylandXdgToplevel::fromResource(parent);
1394
1395 Q_Q(QWaylandXdgToplevel);
1396
1397 if (m_parentToplevel != parentToplevel) {
1398 m_parentToplevel = parentToplevel;
1399 emit q->parentToplevelChanged();
1400 }
1401
1402 if (m_parentToplevel && m_xdgSurface->windowType() != Qt::WindowType::SubWindow) {
1403 // There's a parent now, which means the surface is transient
1404 QWaylandXdgSurfacePrivate::get(m_xdgSurface)->setWindowType(Qt::WindowType::SubWindow);
1405 } else if (!m_parentToplevel && m_xdgSurface->windowType() != Qt::WindowType::Window) {
1406 // When the surface has no parent it is toplevel
1407 QWaylandXdgSurfacePrivate::get(m_xdgSurface)->setWindowType(Qt::WindowType::Window);
1408 }
1409}
1410
1411void QWaylandXdgToplevelPrivate::xdg_toplevel_set_title(QtWaylandServer::xdg_toplevel::Resource *resource, const QString &title)
1412{
1413 Q_UNUSED(resource);
1414 if (title == m_title)
1415 return;
1416 Q_Q(QWaylandXdgToplevel);
1417 m_title = title;
1418 emit q->titleChanged();
1419}
1420
1421void QWaylandXdgToplevelPrivate::xdg_toplevel_set_app_id(QtWaylandServer::xdg_toplevel::Resource *resource, const QString &app_id)
1422{
1423 Q_UNUSED(resource);
1424 if (app_id == m_appId)
1425 return;
1426 Q_Q(QWaylandXdgToplevel);
1427 m_appId = app_id;
1428 emit q->appIdChanged();
1429}
1430
1431void QWaylandXdgToplevelPrivate::xdg_toplevel_show_window_menu(QtWaylandServer::xdg_toplevel::Resource *resource, wl_resource *seatResource, uint32_t serial, int32_t x, int32_t y)
1432{
1433 Q_UNUSED(resource);
1434 Q_UNUSED(serial);
1435 QPoint position(x, y);
1436 auto seat = QWaylandSeat::fromSeatResource(seatResource);
1437 Q_Q(QWaylandXdgToplevel);
1438 emit q->showWindowMenu(seat, position);
1439}
1440
1441void QWaylandXdgToplevelPrivate::xdg_toplevel_move(Resource *resource, wl_resource *seatResource, uint32_t serial)
1442{
1443 Q_UNUSED(resource);
1444 Q_UNUSED(serial);
1445 Q_Q(QWaylandXdgToplevel);
1446 QWaylandSeat *seat = QWaylandSeat::fromSeatResource(seatResource);
1447 emit q->startMove(seat);
1448}
1449
1450void QWaylandXdgToplevelPrivate::xdg_toplevel_resize(QtWaylandServer::xdg_toplevel::Resource *resource, wl_resource *seatResource, uint32_t serial, uint32_t edges)
1451{
1452 Q_UNUSED(resource);
1453 Q_UNUSED(serial);
1454 Q_Q(QWaylandXdgToplevel);
1455 QWaylandSeat *seat = QWaylandSeat::fromSeatResource(seatResource);
1456 emit q->startResize(seat, convertToEdges(resize_edge(edges)));
1457}
1458
1459void QWaylandXdgToplevelPrivate::xdg_toplevel_set_max_size(QtWaylandServer::xdg_toplevel::Resource *resource, int32_t width, int32_t height)
1460{
1461 Q_UNUSED(resource);
1462
1463 QSize maxSize(width, height);
1464 if (width == 0 && height == 0)
1465 maxSize = QSize(); // Wayland size of zero means unspecified which best translates to invalid
1466
1467 if (m_maxSize == maxSize)
1468 return;
1469
1470 if (width < 0 || height < 0) {
1471 // The spec says raise a protocol error, but there's no matching error defined
1472 qWarning() << "Received a xdg_toplevel.set_max_size request with a negative size";
1473 return;
1474 }
1475
1476 if (m_minSize.isValid() && maxSize.isValid() &&
1477 (maxSize.width() < m_minSize.width() || maxSize.height() < m_minSize.height())) {
1478 // The spec says raise a protocol error, but there's no matching error defined
1479 qWarning() << "Received a xdg_toplevel.set_max_size request with a size smaller than the minimium size";
1480 return;
1481 }
1482
1483 m_maxSize = maxSize;
1484
1485 Q_Q(QWaylandXdgToplevel);
1486 emit q->maxSizeChanged();
1487}
1488
1489void QWaylandXdgToplevelPrivate::xdg_toplevel_set_min_size(QtWaylandServer::xdg_toplevel::Resource *resource, int32_t width, int32_t height)
1490{
1491 Q_UNUSED(resource);
1492
1493 QSize minSize(width, height);
1494 if (width == 0 && height == 0)
1495 minSize = QSize(); // Wayland size of zero means unspecified
1496
1497 if (m_minSize == minSize)
1498 return;
1499
1500 if (width < 0 || height < 0) {
1501 // The spec says raise a protocol error, but there's no matching error defined
1502 qWarning() << "Received a xdg_toplevel.set_min_size request with a negative size";
1503 return;
1504 }
1505
1506 if (m_maxSize.isValid() && minSize.isValid() &&
1507 (minSize.width() > m_maxSize.width() || minSize.height() > m_maxSize.height())) {
1508 // The spec says raise a protocol error, but there's no matching error defined
1509 qWarning() << "Received a xdg_toplevel.set_min_size request with a size larger than the maximum size";
1510 return;
1511 }
1512
1513 m_minSize = minSize;
1514
1515 Q_Q(QWaylandXdgToplevel);
1516 emit q->minSizeChanged();
1517}
1518
1519void QWaylandXdgToplevelPrivate::xdg_toplevel_set_maximized(QtWaylandServer::xdg_toplevel::Resource *resource)
1520{
1521 Q_UNUSED(resource);
1522 Q_Q(QWaylandXdgToplevel);
1523 emit q->setMaximized();
1524}
1525
1526void QWaylandXdgToplevelPrivate::xdg_toplevel_unset_maximized(QtWaylandServer::xdg_toplevel::Resource *resource)
1527{
1528 Q_UNUSED(resource);
1529 Q_Q(QWaylandXdgToplevel);
1530 emit q->unsetMaximized();
1531}
1532
1533void QWaylandXdgToplevelPrivate::xdg_toplevel_set_fullscreen(QtWaylandServer::xdg_toplevel::Resource *resource, wl_resource *output_res)
1534{
1535 Q_UNUSED(resource);
1536 Q_Q(QWaylandXdgToplevel);
1537 QWaylandOutput *output = output_res ? QWaylandOutput::fromResource(output_res) : nullptr;
1538 emit q->setFullscreen(output);
1539}
1540
1541void QWaylandXdgToplevelPrivate::xdg_toplevel_unset_fullscreen(QtWaylandServer::xdg_toplevel::Resource *resource)
1542{
1543 Q_UNUSED(resource);
1544 Q_Q(QWaylandXdgToplevel);
1545 emit q->unsetFullscreen();
1546}
1547
1548void QWaylandXdgToplevelPrivate::xdg_toplevel_set_minimized(QtWaylandServer::xdg_toplevel::Resource *resource)
1549{
1550 Q_UNUSED(resource);
1551 Q_Q(QWaylandXdgToplevel);
1552 emit q->setMinimized();
1553}
1554
1555/*!
1556 * \qmltype XdgPopup
1557 * \nativetype QWaylandXdgPopup
1558 * \inqmlmodule QtWayland.Compositor.XdgShell
1559 * \since 5.12
1560 * \brief XdgPopup represents the popup specific parts of and xdg surface.
1561 *
1562 * This type is part of the \l{XdgShell} extension and provides a way to extend
1563 * the functionality of an \l{XdgSurface} with features
1564 * specific to desktop-style menus for an xdg surface.
1565 *
1566 * It corresponds to the Wayland interface \c xdg_popup.
1567 */
1568
1569/*!
1570 * \class QWaylandXdgPopup
1571 * \inmodule QtWaylandCompositor
1572 * \since 5.12
1573 * \brief The QWaylandXdgPopup class represents the popup specific parts of an xdg surface.
1574 *
1575 * This class is part of the QWaylandXdgShell extension and provides a way to
1576 * extend the functionality of a QWaylandXdgSurface with features
1577 * specific to desktop-style menus for an xdg surface.
1578 *
1579 * It corresponds to the Wayland interface \c xdg_popup.
1580 */
1581
1582/*!
1583 * Constructs a QWaylandXdgPopup.
1584 */
1585QWaylandXdgPopup::QWaylandXdgPopup(QWaylandXdgSurface *xdgSurface, QWaylandXdgSurface *parentXdgSurface,
1586 QWaylandXdgPositioner *positioner, QWaylandResource &resource)
1587 : QObject(*new QWaylandXdgPopupPrivate(xdgSurface, parentXdgSurface, positioner, resource))
1588{
1589}
1590
1591/*!
1592 * \qmlproperty XdgSurface XdgPopup::xdgSurface
1593 *
1594 * This property holds the XdgSurface associated with this XdgPopup.
1595 */
1596
1597/*!
1598 * \property QWaylandXdgPopup::xdgSurface
1599 *
1600 * This property holds the QWaylandXdgSurface associated with this QWaylandXdgPopup.
1601 */
1602QWaylandXdgSurface *QWaylandXdgPopup::xdgSurface() const
1603{
1604 Q_D(const QWaylandXdgPopup);
1605 return d->m_xdgSurface;
1606}
1607
1608/*!
1609 * \qmlproperty XdgSurface XdgPopup::parentXdgSurface
1610 *
1611 * This property holds the XdgSurface associated with the parent of this XdgPopup.
1612 */
1613
1614/*!
1615 * \property QWaylandXdgPopup::parentXdgSurface
1616 *
1617 * This property holds the QWaylandXdgSurface associated with the parent of this
1618 * QWaylandXdgPopup.
1619 */
1620QWaylandXdgSurface *QWaylandXdgPopup::parentXdgSurface() const
1621{
1622 Q_D(const QWaylandXdgPopup);
1623 return d->m_parentXdgSurface;
1624}
1625
1626/*!
1627 * \qmlproperty rect XdgPopup::configuredGeometry
1628 *
1629 * The window geometry the popup received in the configure event. Relative to the
1630 * upper left corner of the parent surface.
1631 */
1632
1633/*!
1634 * \property QWaylandXdgPopup::configuredGeometry
1635 *
1636 * The window geometry the popup received in the configure event. Relative to the
1637 * upper left corner of the parent surface.
1638 */
1639QRect QWaylandXdgPopup::configuredGeometry() const
1640{
1641 Q_D(const QWaylandXdgPopup);
1642 return d->m_geometry;
1643}
1644
1645/*!
1646 * \qmlproperty rect XdgPopup::anchorRect
1647 *
1648 * The anchor rectangle relative to the parent window geometry that the child
1649 * surface should be placed relative to.
1650 */
1651
1652/*!
1653 * \property QWaylandXdgPopup::anchorRect
1654 *
1655 * Returns the anchor rectangle relative to the parent window geometry that the child
1656 * surface should be placed relative to.
1657 */
1658QRect QWaylandXdgPopup::anchorRect() const
1659{
1660 Q_D(const QWaylandXdgPopup);
1661 return d->m_positionerData.anchorRect;
1662}
1663
1664/*!
1665 * \qmlproperty enumeration XdgPopup::anchorEdges
1666 *
1667 * This property holds the set of edges on the anchor rect that the child surface should be placed
1668 * relative to. If no edges are specified in a direction, the anchor point should be
1669 * centered between the edges.
1670 *
1671 * The possible values are:
1672 * \value Qt.TopEdge The top edge of the rectangle.
1673 * \value Qt.LeftEdge The left edge of the rectangle.
1674 * \value Qt.RightEdge The right edge of the rectangle.
1675 * \value Qt.BottomEdge The bottom edge of the rectangle.
1676 */
1677
1678/*!
1679 * \property QWaylandXdgPopup::anchorEdges
1680 *
1681 * Returns the set of edges on the anchor rect that the child surface should be placed
1682 * relative to. If no edges are specified in a direction, the anchor point should be
1683 * centered between the edges.
1684 */
1685Qt::Edges QWaylandXdgPopup::anchorEdges() const
1686{
1687 Q_D(const QWaylandXdgPopup);
1688 return d->m_positionerData.anchorEdges;
1689}
1690
1691/*!
1692 * \qmlproperty rect XdgPopup::gravityEdges
1693 *
1694 * Specifies in what direction the surface should be positioned, relative to the anchor
1695 * point.
1696 *
1697 * The possible values are:
1698 * \value Qt.TopEdge The surface should slide towards the top of the screen.
1699 * \value Qt.LeftEdge The surface should slide towards the left of the screen.
1700 * \value Qt.RightEdge The surface should slide towards the right of the screen.
1701 * \value Qt.BottomEdge The surface should slide towards the bottom of the screen.
1702 */
1703
1704/*!
1705 * \property QWaylandXdgPopup::gravityEdges
1706 *
1707 * Specifies in what direction the surface should be positioned, relative to the anchor
1708 * point.
1709 */
1710Qt::Edges QWaylandXdgPopup::gravityEdges() const
1711{
1712 Q_D(const QWaylandXdgPopup);
1713 return d->m_positionerData.gravityEdges;
1714}
1715
1716/*!
1717 * \qmlproperty enumeration XdgPopup::slideConstraints
1718 *
1719 * This property holds the orientations in which the child should slide to fit within the screen.
1720 *
1721 * Possible values:
1722 * \value Qt.Horizontal Horizontal
1723 * \value Qt.Vertical Vertical
1724 */
1725
1726/*!
1727 * \property QWaylandXdgPopup::slideConstraints
1728 *
1729 * This property holds the orientations in which the child should slide to fit within the screen.
1730 */
1731Qt::Orientations QWaylandXdgPopup::slideConstraints() const
1732{
1733 Q_D(const QWaylandXdgPopup);
1734 const uint flags = d->m_positionerData.constraintAdjustments;
1735
1736 Qt::Orientations constraints = {};
1737
1738 if (flags & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X)
1739 constraints |= Qt::Horizontal;
1740 if (flags & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y)
1741 constraints |= Qt::Vertical;
1742
1743 return constraints;
1744}
1745
1746/*!
1747 * \qmlproperty enumeration XdgPopup::flipConstraints
1748 *
1749 * This property holds the orientations in which the child should flip to fit within the screen.
1750 *
1751 * Possible values:
1752 * \value Qt.Horizontal Horizontal
1753 * \value Qt.Vertical Vertical
1754 */
1755
1756/*!
1757 * \property QWaylandXdgPopup::flipConstraints
1758 *
1759 * This property holds the orientations in which the child should flip to fit within the screen.
1760 */
1761Qt::Orientations QWaylandXdgPopup::flipConstraints() const
1762{
1763 Q_D(const QWaylandXdgPopup);
1764 const uint flags = d->m_positionerData.constraintAdjustments;
1765
1766 Qt::Orientations constraints = {};
1767
1768 if (flags & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X)
1769 constraints |= Qt::Horizontal;
1770 if (flags & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y)
1771 constraints |= Qt::Vertical;
1772
1773 return constraints;
1774}
1775
1776/*!
1777 * \qmlproperty enumeration XdgPopup::resizeConstraints
1778 *
1779 * This property holds the orientations in which the child should resize to fit within the screen.
1780 *
1781 * Possible values:
1782 * \value Qt.Horizontal Horizontal
1783 * \value Qt.Vertical Vertical
1784 */
1785
1786/*!
1787 * \property QWaylandXdgPopup::resizeConstraints
1788 *
1789 * This property holds the orientations in which the child should resize to fit within the screen.
1790 */
1791Qt::Orientations QWaylandXdgPopup::resizeConstraints() const
1792{
1793 Q_D(const QWaylandXdgPopup);
1794 const uint flags = d->m_positionerData.constraintAdjustments;
1795
1796 Qt::Orientations constraints = {};
1797
1798 if (flags & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X)
1799 constraints |= Qt::Horizontal;
1800 if (flags & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y)
1801 constraints |= Qt::Vertical;
1802
1803 return constraints;
1804}
1805
1806/*!
1807 * \qmlproperty point XdgPopup::offset
1808 *
1809 * The position relative to the position of the anchor on the anchor rectangle and
1810 * the anchor on the surface.
1811 */
1812
1813/*!
1814 * \property QWaylandXdgPopup::offset
1815 *
1816 * Returns the surface position relative to the position of the anchor on the anchor
1817 * rectangle and the anchor on the surface.
1818 */
1819QPoint QWaylandXdgPopup::offset() const
1820{
1821 Q_D(const QWaylandXdgPopup);
1822 return d->m_positionerData.offset;
1823}
1824
1825/*!
1826 * \qmlproperty size XdgPopup::positionerSize
1827 *
1828 * The size requested for the window geometry by the positioner object.
1829 */
1830
1831/*!
1832 * \property QWaylandXdgPopup::positionerSize
1833 *
1834 * Returns the size requested for the window geometry by the positioner object.
1835 */
1836QSize QWaylandXdgPopup::positionerSize() const
1837{
1838 Q_D(const QWaylandXdgPopup);
1839 return d->m_positionerData.size;
1840}
1841
1842/*!
1843 * \qmlproperty point XdgPopup::unconstrainedPosition
1844 *
1845 * The position of the surface relative to the parent window geometry if the surface
1846 * is not constrained. I.e. when not moved to fit inside the screen or similar.
1847 */
1848
1849/*!
1850 * \property QWaylandXdgPopup::unconstrainedPosition
1851 *
1852 * The position of the surface relative to the parent window geometry if the surface
1853 * is not constrained. I.e. when not moved to fit inside the screen or similar.
1854 */
1855QPoint QWaylandXdgPopup::unconstrainedPosition() const
1856{
1857 Q_D(const QWaylandXdgPopup);
1858 return d->m_positionerData.unconstrainedPosition();
1859}
1860
1861/*!
1862 * \qmlmethod int XdgPopup::sendConfigure(rect geometry)
1863 *
1864 * Sends a configure event to the client. \a geometry contains the window geometry
1865 * relative to the upper left corner of the window geometry of the parent surface.
1866 *
1867 * This implicitly sends a configure event to the corresponding XdgSurface as well.
1868 */
1869
1870/*!
1871 * Sends a configure event to the client. \a geometry contains the window geometry
1872 * relative to the upper left corner of the window geometry of the parent surface.
1873 *
1874 * This implicitly sends a configure event to the corresponding QWaylandXdgSurface
1875 * as well.
1876 */
1877uint QWaylandXdgPopup::sendConfigure(const QRect &geometry)
1878{
1879 Q_D(QWaylandXdgPopup);
1880 return d->sendConfigure(geometry);
1881}
1882
1883/*!
1884 * \qmlmethod void XdgPopup::sendPopupDone()
1885 * \since 5.14
1886 *
1887 * Dismiss the popup. According to the \c xdg-shell protocol this should make the
1888 * client destroy the popup.
1889 */
1890
1891/*!
1892 * \since 5.14
1893 *
1894 * Dismiss the popup. According to the \c xdg-shell protocol this should make the
1895 * client destroy the popup.
1896 */
1897void QWaylandXdgPopup::sendPopupDone()
1898{
1899 Q_D(QWaylandXdgPopup);
1900 d->send_popup_done();
1901}
1902
1903/*!
1904 * Returns the surface role for the QWaylandPopup.
1905 */
1906QWaylandSurfaceRole *QWaylandXdgPopup::role()
1907{
1908 return &QWaylandXdgPopupPrivate::s_role;
1909}
1910
1911QWaylandXdgPopupPrivate::QWaylandXdgPopupPrivate(QWaylandXdgSurface *xdgSurface, QWaylandXdgSurface *parentXdgSurface,
1912 QWaylandXdgPositioner *positioner, const QWaylandResource &resource)
1913 : m_xdgSurface(xdgSurface)
1914 , m_parentXdgSurface(parentXdgSurface)
1915 , m_positionerData(positioner->m_data)
1916{
1917 Q_ASSERT(m_positionerData.isComplete());
1918 init(resource.resource());
1919
1920 QWaylandXdgSurfacePrivate::get(m_xdgSurface)->setWindowType(Qt::WindowType::Popup);
1921
1922 //TODO: Need an API for sending a different initial configure
1923 sendConfigure(QRect(m_positionerData.unconstrainedPosition(), m_positionerData.size));
1924}
1925
1926void QWaylandXdgPopupPrivate::handleAckConfigure(uint serial)
1927{
1928 Q_Q(QWaylandXdgPopup);
1929 ConfigureEvent config;
1930 Q_FOREVER {
1931 if (m_pendingConfigures.empty()) {
1932 qWarning("Popup received an unexpected ack_configure!");
1933 return;
1934 }
1935
1936 // This won't work unless there always is a popup.configure for each xdgsurface.configure
1937 config = m_pendingConfigures.takeFirst();
1938
1939 if (config.serial == serial)
1940 break;
1941 }
1942
1943 if (m_geometry == config.geometry)
1944 return;
1945
1946 m_geometry = config.geometry;
1947 emit q->configuredGeometryChanged();
1948}
1949
1950uint QWaylandXdgPopupPrivate::sendConfigure(const QRect &geometry)
1951{
1952 uint32_t serial = m_xdgSurface->surface()->compositor()->nextSerial();
1953 m_pendingConfigures.append(QWaylandXdgPopupPrivate::ConfigureEvent{geometry, serial});
1954 send_configure(geometry.x(), geometry.y(), geometry.width(), geometry.height());
1955 QWaylandXdgSurfacePrivate::get(m_xdgSurface)->send_configure(serial);
1956 return serial;
1957}
1958
1959void QWaylandXdgPopupPrivate::xdg_popup_destroy(QtWaylandServer::xdg_popup::Resource *resource)
1960{
1961 Q_UNUSED(resource);
1962 qWarning() << Q_FUNC_INFO << "Not implemented"; //TODO
1963}
1964
1965void QWaylandXdgPopupPrivate::xdg_popup_grab(QtWaylandServer::xdg_popup::Resource *resource, wl_resource *seat, uint32_t serial)
1966{
1967 Q_UNUSED(resource);
1968 Q_UNUSED(serial);
1969 Q_UNUSED(seat);
1970 qWarning() << Q_FUNC_INFO << "Not implemented"; //TODO
1971 //switch keyboard focus
1972 //eventually send configure with activated.
1973}
1974
1975QWaylandSurfaceRole QWaylandXdgPopupPrivate::s_role("xdg_popup");
1976
1977QWaylandXdgPositionerData::QWaylandXdgPositionerData()
1978 : offset(0, 0)
1979{}
1980
1981bool QWaylandXdgPositionerData::isComplete() const
1982{
1983 return size.width() > 0 && size.height() > 0 && anchorRect.size().width() > 0 && anchorRect.size().height() > 0;
1984}
1985
1986QPoint QWaylandXdgPositionerData::anchorPoint() const
1987{
1988 int yPosition = 0;
1989 if (anchorEdges & Qt::TopEdge)
1990 yPosition = anchorRect.top();
1991 else if (anchorEdges & Qt::BottomEdge)
1992 yPosition = anchorRect.bottom() + 1;
1993 else
1994 yPosition = anchorRect.top() + anchorRect.height() / 2;
1995
1996 int xPosition = 0;
1997 if (anchorEdges & Qt::LeftEdge)
1998 xPosition = anchorRect.left();
1999 else if (anchorEdges & Qt::RightEdge)
2000 xPosition = anchorRect.right() + 1;
2001 else
2002 xPosition = anchorRect.left() + anchorRect.width() / 2;
2003
2004 return QPoint(xPosition, yPosition);
2005}
2006
2007QPoint QWaylandXdgPositionerData::unconstrainedPosition() const
2008{
2009 int gravityOffsetY = 0;
2010 if (gravityEdges & Qt::TopEdge)
2011 gravityOffsetY = -size.height();
2012 else if (!(gravityEdges & Qt::BottomEdge))
2013 gravityOffsetY = -size.height() / 2;
2014
2015 int gravityOffsetX = 0;
2016 if (gravityEdges & Qt::LeftEdge)
2017 gravityOffsetX = -size.width();
2018 else if (!(gravityEdges & Qt::RightEdge))
2019 gravityOffsetX = -size.width() / 2;
2020
2021 QPoint gravityOffset(gravityOffsetX, gravityOffsetY);
2022 return anchorPoint() + gravityOffset + offset;
2023}
2024
2025QWaylandXdgPositioner::QWaylandXdgPositioner(const QWaylandResource &resource)
2026{
2027 init(resource.resource());
2028}
2029
2030void QWaylandXdgPositioner::xdg_positioner_destroy_resource(QtWaylandServer::xdg_positioner::Resource *resource)
2031{
2032 Q_UNUSED(resource);
2033 delete this;
2034}
2035
2036void QWaylandXdgPositioner::xdg_positioner_destroy(QtWaylandServer::xdg_positioner::Resource *resource)
2037{
2038 wl_resource_destroy(resource->handle);
2039}
2040
2041void QWaylandXdgPositioner::xdg_positioner_set_size(QtWaylandServer::xdg_positioner::Resource *resource, int32_t width, int32_t height)
2042{
2043 if (width <= 0 || height <= 0) {
2044 wl_resource_post_error(resource->handle, XDG_POSITIONER_ERROR_INVALID_INPUT,
2045 "xdg_positioner.set_size requested with non-positive dimensions");
2046 return;
2047 }
2048
2049 QSize size(width, height);
2050 m_data.size = size;
2051}
2052
2053void QWaylandXdgPositioner::xdg_positioner_set_anchor_rect(QtWaylandServer::xdg_positioner::Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height)
2054{
2055 if (width <= 0 || height <= 0) {
2056 wl_resource_post_error(resource->handle, XDG_POSITIONER_ERROR_INVALID_INPUT,
2057 "xdg_positioner.set_anchor_rect requested with non-positive dimensions");
2058 return;
2059 }
2060
2061 QRect anchorRect(x, y, width, height);
2062 m_data.anchorRect = anchorRect;
2063}
2064
2065void QWaylandXdgPositioner::xdg_positioner_set_anchor(QtWaylandServer::xdg_positioner::Resource *resource, uint32_t anchor)
2066{
2067 Qt::Edges anchorEdges = convertToEdges(xdg_positioner::anchor(anchor));
2068
2069 if ((anchorEdges & Qt::BottomEdge && anchorEdges & Qt::TopEdge) ||
2070 (anchorEdges & Qt::LeftEdge && anchorEdges & Qt::RightEdge)) {
2071 wl_resource_post_error(resource->handle, XDG_POSITIONER_ERROR_INVALID_INPUT,
2072 "xdg_positioner.set_anchor requested with parallel edges");
2073 return;
2074 }
2075
2076 m_data.anchorEdges = anchorEdges;
2077}
2078
2079void QWaylandXdgPositioner::xdg_positioner_set_gravity(QtWaylandServer::xdg_positioner::Resource *resource, uint32_t gravity)
2080{
2081 Qt::Edges gravityEdges = convertToEdges(xdg_positioner::gravity(gravity));
2082
2083 if ((gravityEdges & Qt::BottomEdge && gravityEdges & Qt::TopEdge) ||
2084 (gravityEdges & Qt::LeftEdge && gravityEdges & Qt::RightEdge)) {
2085 wl_resource_post_error(resource->handle, XDG_POSITIONER_ERROR_INVALID_INPUT,
2086 "xdg_positioner.set_gravity requested with parallel edges");
2087 return;
2088 }
2089
2090 m_data.gravityEdges = gravityEdges;
2091}
2092
2093void QWaylandXdgPositioner::xdg_positioner_set_constraint_adjustment(QtWaylandServer::xdg_positioner::Resource *resource, uint32_t constraint_adjustment)
2094{
2095 Q_UNUSED(resource);
2096 m_data.constraintAdjustments = constraint_adjustment;
2097}
2098
2099void QWaylandXdgPositioner::xdg_positioner_set_offset(QtWaylandServer::xdg_positioner::Resource *resource, int32_t x, int32_t y)
2100{
2101 Q_UNUSED(resource);
2102 m_data.offset = QPoint(x, y);
2103}
2104
2105QWaylandXdgPositioner *QWaylandXdgPositioner::fromResource(wl_resource *resource)
2106{
2107 return QtWayland::fromResource<QWaylandXdgPositioner *>(resource);
2108}
2109
2110Qt::Edges QWaylandXdgPositioner::convertToEdges(anchor anchor)
2111{
2112 switch (anchor) {
2113 case anchor_none:
2114 return Qt::Edges();
2115 case anchor_top:
2116 return Qt::TopEdge;
2117 case anchor_bottom:
2118 return Qt::BottomEdge;
2119 case anchor_left:
2120 return Qt::LeftEdge;
2121 case anchor_right:
2122 return Qt::RightEdge;
2123 case anchor_top_left:
2124 return Qt::TopEdge | Qt::LeftEdge;
2125 case anchor_bottom_left:
2126 return Qt::BottomEdge | Qt::LeftEdge;
2127 case anchor_top_right:
2128 return Qt::TopEdge | Qt::RightEdge;
2129 case anchor_bottom_right:
2130 return Qt::BottomEdge | Qt::RightEdge;
2131 default:
2132 qWarning() << "Unknown Wayland xdg edge" << anchor;
2133 return Qt::Edges();
2134 }
2135}
2136
2137Qt::Edges QWaylandXdgPositioner::convertToEdges(QWaylandXdgPositioner::gravity gravity)
2138{
2139 return convertToEdges(anchor(gravity));
2140}
2141
2142
2143QT_END_NAMESPACE
2144
2145#include "moc_qwaylandxdgshell.cpp"