File indexing completed on 2024-05-12 05:35:51

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "tabletevents.h"
0008 #include "qwayland-tablet-unstable-v2.h"
0009 #include <QQuickWindow>
0010 #include <QWaylandClientExtensionTemplate>
0011 #include <qguiapplication.h>
0012 #if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
0013 #include <qpa/qplatformnativeinterface.h>
0014 #endif
0015 #include <qtwaylandclientversion.h>
0016 
0017 class TabletPad : public QObject, public QtWayland::zwp_tablet_pad_v2
0018 {
0019 public:
0020     TabletPad(TabletEvents *events, ::zwp_tablet_pad_v2 *t)
0021         : QObject(events)
0022         , QtWayland::zwp_tablet_pad_v2(t)
0023         , m_events(events)
0024     {
0025     }
0026 
0027     ~TabletPad()
0028     {
0029         destroy();
0030     }
0031 
0032     void zwp_tablet_pad_v2_path(const QString &path) override
0033     {
0034         m_path = path;
0035     }
0036 
0037     void zwp_tablet_pad_v2_buttons(uint32_t buttons) override
0038     {
0039         m_buttons = buttons;
0040     }
0041 
0042     void zwp_tablet_pad_v2_done() override
0043     {
0044         m_events->padButtonsChanged(m_path, m_buttons);
0045     }
0046 
0047     void zwp_tablet_pad_v2_button(uint32_t /*time*/, uint32_t button, uint32_t state) override
0048     {
0049         Q_EMIT m_events->padButtonReceived(m_path, button, state);
0050     }
0051 
0052     TabletEvents *const m_events;
0053     QString m_path;
0054     uint m_buttons = 0;
0055 };
0056 
0057 class Tool : public QObject, public QtWayland::zwp_tablet_tool_v2
0058 {
0059 public:
0060     Tool(TabletEvents *const events, ::zwp_tablet_tool_v2 *t)
0061         : QObject(events)
0062         , QtWayland::zwp_tablet_tool_v2(t)
0063         , m_events(events)
0064     {
0065     }
0066 
0067     ~Tool()
0068     {
0069         destroy();
0070     }
0071 
0072     void zwp_tablet_tool_v2_hardware_serial(uint32_t hardware_serial_hi, uint32_t hardware_serial_lo) override
0073     {
0074         m_hardware_serial_hi = hardware_serial_hi;
0075         m_hardware_serial_lo = hardware_serial_lo;
0076     }
0077 
0078     void zwp_tablet_tool_v2_button(uint32_t /*serial*/, uint32_t button, uint32_t state) override
0079     {
0080         Q_EMIT m_events->toolButtonReceived(m_hardware_serial_hi, m_hardware_serial_lo, button, state);
0081     }
0082 
0083     void zwp_tablet_tool_v2_motion(wl_fixed_t x, wl_fixed_t y) override
0084     {
0085         m_tool_x = x;
0086         m_tool_y = y;
0087         Q_EMIT m_events->toolMotion(m_hardware_serial_hi, m_hardware_serial_lo, wl_fixed_to_double(m_tool_x), wl_fixed_to_double(m_tool_y));
0088     }
0089 
0090     void zwp_tablet_tool_v2_down(uint32_t serial) override
0091     {
0092         Q_UNUSED(serial)
0093         Q_EMIT m_events->toolDown(m_hardware_serial_hi, m_hardware_serial_lo, wl_fixed_to_double(m_tool_x), wl_fixed_to_double(m_tool_y));
0094     }
0095 
0096     void zwp_tablet_tool_v2_up() override
0097     {
0098         Q_EMIT m_events->toolUp(m_hardware_serial_hi, m_hardware_serial_lo, wl_fixed_to_double(m_tool_x), wl_fixed_to_double(m_tool_y));
0099     }
0100 
0101     uint32_t m_hardware_serial_hi = 0;
0102     uint32_t m_hardware_serial_lo = 0;
0103     uint32_t m_tool_x = 0;
0104     uint32_t m_tool_y = 0;
0105     TabletEvents *const m_events;
0106 };
0107 
0108 class TabletManager : public QWaylandClientExtensionTemplate<TabletManager>, public QtWayland::zwp_tablet_manager_v2
0109 {
0110 public:
0111     TabletManager(TabletEvents *q)
0112         : QWaylandClientExtensionTemplate<TabletManager>(ZWP_TABLET_MANAGER_V2_GET_TABLET_SEAT_SINCE_VERSION)
0113         , q(q)
0114     {
0115         setParent(q);
0116         initialize();
0117         Q_ASSERT(isInitialized());
0118     }
0119 
0120     ~TabletManager()
0121     {
0122         destroy();
0123     }
0124 
0125     TabletEvents *const q;
0126 };
0127 
0128 class TabletSeat : public QObject, public QtWayland::zwp_tablet_seat_v2
0129 {
0130 public:
0131     TabletSeat(TabletEvents *events, ::zwp_tablet_seat_v2 *seat)
0132         : QObject(events)
0133         , QtWayland::zwp_tablet_seat_v2(seat)
0134         , m_events(events)
0135     {
0136     }
0137 
0138     ~TabletSeat()
0139     {
0140         destroy();
0141     }
0142 
0143     void zwp_tablet_seat_v2_tool_added(struct ::zwp_tablet_tool_v2 *id) override
0144     {
0145         new Tool(m_events, id);
0146     }
0147 
0148     void zwp_tablet_seat_v2_pad_added(struct ::zwp_tablet_pad_v2 *id) override
0149     {
0150         new TabletPad(m_events, id);
0151     }
0152 
0153     TabletEvents *const m_events;
0154 };
0155 
0156 TabletEvents::TabletEvents(QQuickItem *parent)
0157     : QQuickItem(parent)
0158 {
0159 #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
0160     auto waylandApp = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>();
0161     if (!waylandApp) {
0162         return;
0163     }
0164     auto seat = waylandApp->seat();
0165 #else
0166     auto seat = static_cast<wl_seat *>(QGuiApplication::platformNativeInterface()->nativeResourceForIntegration("wl_seat"));
0167 #endif
0168 
0169     auto tabletClient = new TabletManager(this);
0170     auto _seat = tabletClient->get_tablet_seat(seat);
0171     new TabletSeat(this, _seat);
0172 }