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

0001 /*
0002     SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redondo.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "xwaylandkeyboardgrab_v1.h"
0008 
0009 #include <qwayland-server-xwayland-keyboard-grab-unstable-v1.h>
0010 
0011 #include "display.h"
0012 #include "seat.h"
0013 #include "surface.h"
0014 
0015 constexpr int version = 1;
0016 
0017 namespace KWin
0018 {
0019 
0020 class XWaylandKeyboardGrabManagerV1InterfacePrivate : QtWaylandServer::zwp_xwayland_keyboard_grab_manager_v1
0021 {
0022 public:
0023     XWaylandKeyboardGrabManagerV1InterfacePrivate(Display *display, XWaylandKeyboardGrabManagerV1Interface *q);
0024     XWaylandKeyboardGrabManagerV1Interface *const q;
0025     QHash<QPair<const SurfaceInterface *, const SeatInterface *>, XWaylandKeyboardGrabV1Interface *> m_grabs;
0026 
0027 protected:
0028     void zwp_xwayland_keyboard_grab_manager_v1_destroy(Resource *resource) override;
0029     void zwp_xwayland_keyboard_grab_manager_v1_grab_keyboard(Resource *resource, uint32_t id, wl_resource *surface, wl_resource *seat) override;
0030 };
0031 
0032 class XWaylandKeyboardGrabV1InterfacePrivate : QtWaylandServer::zwp_xwayland_keyboard_grab_v1
0033 {
0034 public:
0035     XWaylandKeyboardGrabV1InterfacePrivate(XWaylandKeyboardGrabV1Interface *q, wl_resource *resource);
0036     XWaylandKeyboardGrabV1Interface *const q;
0037 
0038 protected:
0039     void zwp_xwayland_keyboard_grab_v1_destroy(Resource *resource) override;
0040     void zwp_xwayland_keyboard_grab_v1_destroy_resource(Resource *resource) override;
0041 };
0042 
0043 XWaylandKeyboardGrabManagerV1InterfacePrivate::XWaylandKeyboardGrabManagerV1InterfacePrivate(Display *display, XWaylandKeyboardGrabManagerV1Interface *q)
0044     : zwp_xwayland_keyboard_grab_manager_v1(*display, version)
0045     , q(q)
0046 {
0047 }
0048 
0049 void XWaylandKeyboardGrabManagerV1InterfacePrivate::zwp_xwayland_keyboard_grab_manager_v1_destroy(Resource *resource)
0050 {
0051     wl_resource_destroy(resource->handle);
0052 }
0053 
0054 void XWaylandKeyboardGrabManagerV1InterfacePrivate::zwp_xwayland_keyboard_grab_manager_v1_grab_keyboard(Resource *resource, uint32_t id, wl_resource *surface, wl_resource *seat)
0055 {
0056     wl_resource *keyboardGrab = wl_resource_create(resource->client(), &zwp_xwayland_keyboard_grab_v1_interface, version, id);
0057     if (!keyboardGrab) {
0058         wl_client_post_no_memory(resource->client());
0059         return;
0060     }
0061     const auto surfaceInterface = SurfaceInterface::get(surface);
0062     const auto seatInterface = SeatInterface::get(seat);
0063     auto grab = new XWaylandKeyboardGrabV1Interface(keyboardGrab);
0064     QObject::connect(grab, &QObject::destroyed, q, [this, surfaceInterface, seatInterface] {
0065         m_grabs.remove({surfaceInterface, seatInterface});
0066     });
0067     m_grabs.insert({SurfaceInterface::get(surface), SeatInterface::get(seat)}, grab);
0068 }
0069 
0070 XWaylandKeyboardGrabManagerV1Interface::XWaylandKeyboardGrabManagerV1Interface(Display *display, QObject *parent)
0071     : QObject(parent)
0072     , d(std::make_unique<XWaylandKeyboardGrabManagerV1InterfacePrivate>(display, this))
0073 {
0074 }
0075 
0076 XWaylandKeyboardGrabManagerV1Interface::~XWaylandKeyboardGrabManagerV1Interface()
0077 {
0078 }
0079 
0080 bool XWaylandKeyboardGrabManagerV1Interface::hasGrab(SurfaceInterface *surface, SeatInterface *seat) const
0081 {
0082     return d->m_grabs.contains({surface, seat});
0083 }
0084 
0085 XWaylandKeyboardGrabV1InterfacePrivate::XWaylandKeyboardGrabV1InterfacePrivate(XWaylandKeyboardGrabV1Interface *q, wl_resource *resource)
0086     : zwp_xwayland_keyboard_grab_v1(resource)
0087     , q(q)
0088 {
0089 }
0090 
0091 void XWaylandKeyboardGrabV1InterfacePrivate::zwp_xwayland_keyboard_grab_v1_destroy(Resource *resource)
0092 {
0093     wl_resource_destroy(resource->handle);
0094 }
0095 
0096 void XWaylandKeyboardGrabV1InterfacePrivate::zwp_xwayland_keyboard_grab_v1_destroy_resource(Resource *resource)
0097 {
0098     delete q;
0099 }
0100 
0101 XWaylandKeyboardGrabV1Interface::XWaylandKeyboardGrabV1Interface(wl_resource *resource)
0102     : d(std::make_unique<XWaylandKeyboardGrabV1InterfacePrivate>(this, resource))
0103 {
0104 }
0105 
0106 XWaylandKeyboardGrabV1Interface::~XWaylandKeyboardGrabV1Interface()
0107 {
0108 }
0109 
0110 }
0111 
0112 #include "moc_xwaylandkeyboardgrab_v1.cpp"