File indexing completed on 2024-11-10 04:57:27

0001 /*
0002     SPDX-FileCopyrightText: 2020 Benjamin Port <benjamin.port@enioka.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "keyboard_shortcuts_inhibit_v1.h"
0008 #include "display.h"
0009 #include "seat.h"
0010 #include "surface.h"
0011 
0012 #include <qwayland-server-keyboard-shortcuts-inhibit-unstable-v1.h>
0013 
0014 #include <QPointer>
0015 
0016 static const int s_version = 1;
0017 
0018 namespace KWin
0019 {
0020 class KeyboardShortcutsInhibitorV1InterfacePrivate : public QtWaylandServer::zwp_keyboard_shortcuts_inhibitor_v1
0021 {
0022 public:
0023     KeyboardShortcutsInhibitorV1InterfacePrivate(SurfaceInterface *surface,
0024                                                  SeatInterface *seat,
0025                                                  KeyboardShortcutsInhibitManagerV1Interface *manager,
0026                                                  KeyboardShortcutsInhibitorV1Interface *q,
0027                                                  wl_resource *resource);
0028 
0029     KeyboardShortcutsInhibitorV1Interface *q;
0030     QPointer<KeyboardShortcutsInhibitManagerV1Interface> m_manager;
0031     SurfaceInterface *const m_surface;
0032     SeatInterface *const m_seat;
0033     bool m_active;
0034 
0035 protected:
0036     void zwp_keyboard_shortcuts_inhibitor_v1_destroy_resource(Resource *resource) override;
0037 
0038     void zwp_keyboard_shortcuts_inhibitor_v1_destroy(Resource *resource) override;
0039 };
0040 
0041 class KeyboardShortcutsInhibitManagerV1InterfacePrivate : public QtWaylandServer::zwp_keyboard_shortcuts_inhibit_manager_v1
0042 {
0043 public:
0044     KeyboardShortcutsInhibitManagerV1InterfacePrivate(Display *display, KeyboardShortcutsInhibitManagerV1Interface *q);
0045 
0046     void zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(Resource *resource,
0047                                                                      uint32_t id,
0048                                                                      struct ::wl_resource *surface_resource,
0049                                                                      struct ::wl_resource *seat_resource) override;
0050 
0051     KeyboardShortcutsInhibitorV1Interface *findInhibitor(SurfaceInterface *surface, SeatInterface *seat) const;
0052 
0053     void removeInhibitor(SurfaceInterface *const surface, SeatInterface *const seat);
0054 
0055     KeyboardShortcutsInhibitManagerV1Interface *q;
0056     Display *const m_display;
0057     QHash<QPair<SurfaceInterface *, SeatInterface *>, KeyboardShortcutsInhibitorV1Interface *> m_inhibitors;
0058 
0059 protected:
0060     void zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(Resource *resource) override;
0061 };
0062 
0063 KeyboardShortcutsInhibitorV1InterfacePrivate::KeyboardShortcutsInhibitorV1InterfacePrivate(SurfaceInterface *surface,
0064                                                                                            SeatInterface *seat,
0065                                                                                            KeyboardShortcutsInhibitManagerV1Interface *manager,
0066                                                                                            KeyboardShortcutsInhibitorV1Interface *q,
0067                                                                                            wl_resource *resource)
0068     : zwp_keyboard_shortcuts_inhibitor_v1(resource)
0069     , q(q)
0070     , m_manager(manager)
0071     , m_surface(surface)
0072     , m_seat(seat)
0073     , m_active(false)
0074 {
0075 }
0076 
0077 void KeyboardShortcutsInhibitorV1InterfacePrivate::zwp_keyboard_shortcuts_inhibitor_v1_destroy(Resource *resource)
0078 {
0079     wl_resource_destroy(resource->handle);
0080 }
0081 
0082 void KeyboardShortcutsInhibitorV1InterfacePrivate::zwp_keyboard_shortcuts_inhibitor_v1_destroy_resource(Resource *resource)
0083 {
0084     // Ensure manager don't track anymore this inhibitor
0085     if (m_manager) {
0086         m_manager->removeInhibitor(m_surface, m_seat);
0087     }
0088     delete q;
0089 }
0090 
0091 KeyboardShortcutsInhibitorV1Interface::KeyboardShortcutsInhibitorV1Interface(SurfaceInterface *surface,
0092                                                                              SeatInterface *seat,
0093                                                                              KeyboardShortcutsInhibitManagerV1Interface *manager,
0094                                                                              wl_resource *resource)
0095     : QObject(nullptr)
0096     , d(new KeyboardShortcutsInhibitorV1InterfacePrivate(surface, seat, manager, this, resource))
0097 {
0098 }
0099 
0100 KeyboardShortcutsInhibitorV1Interface::~KeyboardShortcutsInhibitorV1Interface() = default;
0101 
0102 void KeyboardShortcutsInhibitorV1Interface::setActive(bool active)
0103 {
0104     if (d->m_active == active) {
0105         return;
0106     }
0107     d->m_active = active;
0108     if (active) {
0109         d->send_active();
0110     } else {
0111         d->send_inactive();
0112     }
0113 }
0114 
0115 bool KeyboardShortcutsInhibitorV1Interface::isActive() const
0116 {
0117     return d->m_active;
0118 }
0119 
0120 SeatInterface *KeyboardShortcutsInhibitorV1Interface::seat() const
0121 {
0122     return d->m_seat;
0123 }
0124 
0125 SurfaceInterface *KeyboardShortcutsInhibitorV1Interface::surface() const
0126 {
0127     return d->m_surface;
0128 }
0129 
0130 KeyboardShortcutsInhibitManagerV1InterfacePrivate::KeyboardShortcutsInhibitManagerV1InterfacePrivate(Display *display,
0131                                                                                                      KeyboardShortcutsInhibitManagerV1Interface *q)
0132     : zwp_keyboard_shortcuts_inhibit_manager_v1(*display, s_version)
0133     , q(q)
0134     , m_display(display)
0135 {
0136 }
0137 
0138 void KeyboardShortcutsInhibitManagerV1InterfacePrivate::zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(Resource *resource,
0139                                                                                                                     uint32_t id,
0140                                                                                                                     wl_resource *surface_resource,
0141                                                                                                                     wl_resource *seat_resource)
0142 {
0143     SeatInterface *seat = SeatInterface::get(seat_resource);
0144     SurfaceInterface *surface = SurfaceInterface::get(surface_resource);
0145     if (m_inhibitors.contains({surface, seat})) {
0146         wl_resource_post_error(resource->handle, error::error_already_inhibited, "the shortcuts are already inhibited for this surface and seat");
0147         return;
0148     }
0149 
0150     wl_resource *inhibitorResource = wl_resource_create(resource->client(), &zwp_keyboard_shortcuts_inhibitor_v1_interface, resource->version(), id);
0151     auto inhibitor = new KeyboardShortcutsInhibitorV1Interface(surface, seat, q, inhibitorResource);
0152     m_inhibitors[{surface, seat}] = inhibitor;
0153     Q_EMIT q->inhibitorCreated(inhibitor);
0154     inhibitor->setActive(true);
0155 }
0156 
0157 KeyboardShortcutsInhibitorV1Interface *KeyboardShortcutsInhibitManagerV1InterfacePrivate::findInhibitor(SurfaceInterface *surface, SeatInterface *seat) const
0158 {
0159     return m_inhibitors.value({surface, seat}, nullptr);
0160 }
0161 
0162 void KeyboardShortcutsInhibitManagerV1InterfacePrivate::zwp_keyboard_shortcuts_inhibit_manager_v1_destroy(Resource *resource)
0163 {
0164     wl_resource_destroy(resource->handle);
0165 }
0166 
0167 void KeyboardShortcutsInhibitManagerV1InterfacePrivate::removeInhibitor(SurfaceInterface *const surface, SeatInterface *const seat)
0168 {
0169     m_inhibitors.remove({surface, seat});
0170 }
0171 
0172 KeyboardShortcutsInhibitManagerV1Interface::KeyboardShortcutsInhibitManagerV1Interface(Display *display, QObject *parent)
0173     : QObject(parent)
0174     , d(new KeyboardShortcutsInhibitManagerV1InterfacePrivate(display, this))
0175 {
0176 }
0177 
0178 KeyboardShortcutsInhibitManagerV1Interface::~KeyboardShortcutsInhibitManagerV1Interface() = default;
0179 
0180 KeyboardShortcutsInhibitorV1Interface *KeyboardShortcutsInhibitManagerV1Interface::findInhibitor(SurfaceInterface *surface, SeatInterface *seat) const
0181 {
0182     return d->findInhibitor(surface, seat);
0183 }
0184 
0185 void KeyboardShortcutsInhibitManagerV1Interface::removeInhibitor(SurfaceInterface *surface, SeatInterface *seat)
0186 {
0187     d->removeInhibitor(surface, seat);
0188 }
0189 
0190 }
0191 
0192 #include "moc_keyboard_shortcuts_inhibit_v1.cpp"