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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003     SPDX-FileCopyrightText: 2021 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 #pragma once
0008 
0009 // KWayland
0010 #include "seat_interface.h"
0011 // Qt
0012 #include <QHash>
0013 #include <QMap>
0014 #include <QPointer>
0015 #include <QVector>
0016 
0017 #include <optional>
0018 
0019 #include "qwayland-server-wayland.h"
0020 
0021 namespace KWaylandServer
0022 {
0023 class AbstractDataSource;
0024 class DataDeviceInterface;
0025 class DataSourceInterface;
0026 class DataControlDeviceV1Interface;
0027 class TextInputV1Interface;
0028 class TextInputV2Interface;
0029 class TextInputV3Interface;
0030 class PrimarySelectionDeviceV1Interface;
0031 class DragAndDropIcon;
0032 
0033 class SeatInterfacePrivate : public QtWaylandServer::wl_seat
0034 {
0035 public:
0036     // exported for unit tests
0037     KWIN_EXPORT static SeatInterfacePrivate *get(SeatInterface *seat);
0038     SeatInterfacePrivate(SeatInterface *q, Display *display);
0039 
0040     void sendCapabilities();
0041     QVector<DataDeviceInterface *> dataDevicesForSurface(SurfaceInterface *surface) const;
0042     void registerPrimarySelectionDevice(PrimarySelectionDeviceV1Interface *primarySelectionDevice);
0043     void registerDataDevice(DataDeviceInterface *dataDevice);
0044     void registerDataControlDevice(DataControlDeviceV1Interface *dataDevice);
0045     void endDrag();
0046     void cancelDrag();
0047 
0048     SeatInterface *q;
0049     QPointer<Display> display;
0050     QString name;
0051     std::chrono::milliseconds timestamp = std::chrono::milliseconds::zero();
0052     quint32 capabilities = 0;
0053     std::unique_ptr<KeyboardInterface> keyboard;
0054     std::unique_ptr<PointerInterface> pointer;
0055     std::unique_ptr<TouchInterface> touch;
0056     QVector<DataDeviceInterface *> dataDevices;
0057     QVector<PrimarySelectionDeviceV1Interface *> primarySelectionDevices;
0058     QVector<DataControlDeviceV1Interface *> dataControlDevices;
0059 
0060     QPointer<TextInputV1Interface> textInputV1;
0061     // TextInput v2
0062     QPointer<TextInputV2Interface> textInputV2;
0063     QPointer<TextInputV3Interface> textInputV3;
0064 
0065     SurfaceInterface *focusedTextInputSurface = nullptr;
0066     QMetaObject::Connection focusedSurfaceDestroyConnection;
0067 
0068     // the last thing copied into the clipboard content
0069     AbstractDataSource *currentSelection = nullptr;
0070     AbstractDataSource *currentPrimarySelection = nullptr;
0071 
0072     // Pointer related members
0073     struct Pointer
0074     {
0075         enum class State {
0076             Released,
0077             Pressed,
0078         };
0079         QHash<quint32, quint32> buttonSerials;
0080         QHash<quint32, State> buttonStates;
0081         QPointF pos;
0082         struct Focus
0083         {
0084             SurfaceInterface *surface = nullptr;
0085             QMetaObject::Connection destroyConnection;
0086             QPointF offset = QPointF();
0087             QMatrix4x4 transformation;
0088             quint32 serial = 0;
0089         };
0090         Focus focus;
0091     };
0092     Pointer globalPointer;
0093     void updatePointerButtonSerial(quint32 button, quint32 serial);
0094     void updatePointerButtonState(quint32 button, Pointer::State state);
0095 
0096     // Keyboard related members
0097     struct Keyboard
0098     {
0099         struct Focus
0100         {
0101             SurfaceInterface *surface = nullptr;
0102             QMetaObject::Connection destroyConnection;
0103             quint32 serial = 0;
0104             QVector<DataDeviceInterface *> selections;
0105             QVector<PrimarySelectionDeviceV1Interface *> primarySelections;
0106         };
0107         Focus focus;
0108     };
0109     Keyboard globalKeyboard;
0110 
0111     // Touch related members
0112     struct Touch
0113     {
0114         struct Focus
0115         {
0116             SurfaceInterface *surface = nullptr;
0117             QMetaObject::Connection destroyConnection;
0118             QPointF offset = QPointF();
0119             QPointF firstTouchPos;
0120             QMatrix4x4 transformation;
0121         };
0122         Focus focus;
0123         QMap<qint32, quint32> ids;
0124     };
0125     Touch globalTouch;
0126 
0127     struct Drag
0128     {
0129         enum class Mode {
0130             None,
0131             Pointer,
0132             Touch,
0133         };
0134         Mode mode = Mode::None;
0135         AbstractDataSource *source = nullptr;
0136         QPointer<SurfaceInterface> surface;
0137         QPointer<AbstractDropHandler> target;
0138         QPointer<DragAndDropIcon> dragIcon;
0139         QMatrix4x4 transformation;
0140         std::optional<quint32> dragImplicitGrabSerial;
0141         QMetaObject::Connection dragSourceDestroyConnection;
0142     };
0143     Drag drag;
0144 
0145 protected:
0146     void seat_bind_resource(Resource *resource) override;
0147     void seat_get_pointer(Resource *resource, uint32_t id) override;
0148     void seat_get_keyboard(Resource *resource, uint32_t id) override;
0149     void seat_get_touch(Resource *resource, uint32_t id) override;
0150     void seat_release(Resource *resource) override;
0151 
0152 private:
0153     void updateSelection(DataDeviceInterface *dataDevice);
0154     void updatePrimarySelection(PrimarySelectionDeviceV1Interface *primarySelectionDevice);
0155 };
0156 
0157 } // namespace KWaylandServer