File indexing completed on 2024-05-19 16:35:32

0001 /*
0002     SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "xdgactivation_v1_interface.h"
0008 #include "display.h"
0009 #include "seat_interface.h"
0010 #include "surface_interface.h"
0011 
0012 #include "qwayland-server-xdg-activation-v1.h"
0013 
0014 namespace KWaylandServer
0015 {
0016 static const int s_version = 1;
0017 
0018 class XdgActivationTokenV1Interface : public QtWaylandServer::xdg_activation_token_v1
0019 {
0020 public:
0021     XdgActivationTokenV1Interface(XdgActivationV1Interface::CreatorFunction creator, ClientConnection *client, uint32_t newId)
0022         : QtWaylandServer::xdg_activation_token_v1(*client, newId, s_version)
0023         , m_creator(creator)
0024         , m_client(client)
0025     {
0026     }
0027 
0028 protected:
0029     void xdg_activation_token_v1_set_serial(Resource *resource, uint32_t serial, struct ::wl_resource *seat) override;
0030     void xdg_activation_token_v1_set_app_id(Resource *resource, const QString &app_id) override;
0031     void xdg_activation_token_v1_set_surface(QtWaylandServer::xdg_activation_token_v1::Resource *resource, struct ::wl_resource *surface) override;
0032     void xdg_activation_token_v1_commit(Resource *resource) override;
0033     void xdg_activation_token_v1_destroy(Resource *resource) override;
0034     void xdg_activation_token_v1_destroy_resource(Resource *resource) override;
0035 
0036     const XdgActivationV1Interface::CreatorFunction m_creator;
0037     QPointer<SurfaceInterface> m_surface;
0038     uint m_serial = 0;
0039     struct ::wl_resource *m_seat = nullptr;
0040     QString m_appId;
0041     bool m_committed = false;
0042     ClientConnection *const m_client;
0043 };
0044 
0045 void XdgActivationTokenV1Interface::xdg_activation_token_v1_set_serial(Resource *resource, uint32_t serial, struct ::wl_resource *seat)
0046 {
0047     m_serial = serial;
0048     m_seat = seat;
0049 }
0050 
0051 void XdgActivationTokenV1Interface::xdg_activation_token_v1_set_app_id(Resource *resource, const QString &app_id)
0052 {
0053     m_appId = app_id;
0054 }
0055 
0056 void XdgActivationTokenV1Interface::xdg_activation_token_v1_set_surface(Resource *resource, struct ::wl_resource *surface)
0057 {
0058     m_surface = SurfaceInterface::get(surface);
0059 }
0060 
0061 void XdgActivationTokenV1Interface::xdg_activation_token_v1_commit(Resource *resource)
0062 {
0063     QString token;
0064     if (m_creator) {
0065         token = m_creator(m_client, m_surface, m_serial, SeatInterface::get(m_seat), m_appId);
0066     }
0067 
0068     m_committed = true;
0069     send_done(resource->handle, token);
0070 }
0071 
0072 void XdgActivationTokenV1Interface::xdg_activation_token_v1_destroy(Resource *resource)
0073 {
0074     if (!m_committed) {
0075         send_done(resource->handle, {});
0076     }
0077     wl_resource_destroy(resource->handle);
0078 }
0079 
0080 void XdgActivationTokenV1Interface::xdg_activation_token_v1_destroy_resource(Resource *resource)
0081 {
0082     delete this;
0083 }
0084 
0085 class XdgActivationV1InterfacePrivate : public QtWaylandServer::xdg_activation_v1
0086 {
0087 public:
0088     XdgActivationV1InterfacePrivate(Display *display, XdgActivationV1Interface *q)
0089         : QtWaylandServer::xdg_activation_v1(*display, s_version)
0090         , q(q)
0091         , m_display(display)
0092     {
0093     }
0094 
0095 protected:
0096     void xdg_activation_v1_get_activation_token(Resource *resource, uint32_t id) override;
0097     void xdg_activation_v1_activate(Resource *resource, const QString &token, struct ::wl_resource *surface) override;
0098     void xdg_activation_v1_destroy(Resource *resource) override;
0099 
0100 public:
0101     XdgActivationV1Interface::CreatorFunction m_creator;
0102     XdgActivationV1Interface *const q;
0103     Display *const m_display;
0104 };
0105 
0106 void XdgActivationV1InterfacePrivate::xdg_activation_v1_get_activation_token(Resource *resource, uint32_t id)
0107 {
0108     new XdgActivationTokenV1Interface(m_creator, m_display->getConnection(resource->client()), id);
0109 }
0110 
0111 void XdgActivationV1InterfacePrivate::xdg_activation_v1_activate(Resource *resource, const QString &token, struct ::wl_resource *surface)
0112 {
0113     Q_EMIT q->activateRequested(SurfaceInterface::get(surface), token);
0114 }
0115 
0116 void XdgActivationV1InterfacePrivate::xdg_activation_v1_destroy(Resource *resource)
0117 {
0118     wl_resource_destroy(resource->handle);
0119 }
0120 
0121 XdgActivationV1Interface::XdgActivationV1Interface(Display *display, QObject *parent)
0122     : QObject(parent)
0123     , d(new XdgActivationV1InterfacePrivate(display, this))
0124 {
0125 }
0126 
0127 XdgActivationV1Interface::~XdgActivationV1Interface()
0128 {
0129 }
0130 
0131 void XdgActivationV1Interface::setActivationTokenCreator(const CreatorFunction &creator)
0132 {
0133     d->m_creator = creator;
0134 }
0135 
0136 }