File indexing completed on 2024-06-16 05:05:39

0001 /*
0002     SPDX-FileCopyrightText: 2022 David Redondo <kde@david-redono.de>
0003     SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #pragma once
0009 
0010 #include "plugin.h"
0011 #include <array>
0012 #include <optional>
0013 #include <variant>
0014 
0015 #include "core/inputdevice.h"
0016 #include "input.h"
0017 #include "input_event.h"
0018 
0019 #include <QKeySequence>
0020 
0021 class InputDevice : public KWin::InputDevice
0022 {
0023     QString sysName() const override;
0024     QString name() const override;
0025 
0026     bool isEnabled() const override;
0027     void setEnabled(bool enabled) override;
0028 
0029     void setLeds(KWin::LEDs leds) override;
0030     KWin::LEDs leds() const override;
0031 
0032     bool isKeyboard() const override;
0033     bool isAlphaNumericKeyboard() const override;
0034     bool isPointer() const override;
0035     bool isTouchpad() const override;
0036     bool isTouch() const override;
0037     bool isTabletTool() const override;
0038     bool isTabletPad() const override;
0039     bool isTabletModeSwitch() const override;
0040     bool isLidSwitch() const override;
0041 };
0042 
0043 struct Trigger
0044 {
0045     QString device;
0046     uint button;
0047     bool operator==(const Trigger &o) const
0048     {
0049         return button == o.button && device == o.device;
0050     }
0051 };
0052 
0053 class ButtonRebindsFilter : public KWin::Plugin, public KWin::InputEventFilter
0054 {
0055     Q_OBJECT
0056 public:
0057     enum TriggerType {
0058         Pointer,
0059         TabletPad,
0060         TabletToolButtonType,
0061         LastType
0062     };
0063     Q_ENUM(TriggerType)
0064     struct TabletToolButton
0065     {
0066         quint32 button;
0067     };
0068     struct MouseButton
0069     {
0070         quint32 button;
0071     };
0072 
0073     explicit ButtonRebindsFilter();
0074     bool pointerEvent(KWin::MouseEvent *event, quint32 nativeButton) override;
0075     bool tabletPadButtonEvent(uint button, bool pressed, const KWin::TabletPadId &tabletPadId, std::chrono::microseconds time) override;
0076     bool tabletToolButtonEvent(uint button, bool pressed, const KWin::TabletToolId &tabletToolId, std::chrono::microseconds time) override;
0077 
0078 private:
0079     void loadConfig(const KConfigGroup &group);
0080     void insert(TriggerType type, const Trigger &trigger, const QStringList &action);
0081     bool send(TriggerType type, const Trigger &trigger, bool pressed, std::chrono::microseconds timestamp);
0082     bool sendKeySequence(const QKeySequence &sequence, bool pressed, std::chrono::microseconds time);
0083     bool sendMouseButton(quint32 button, bool pressed, std::chrono::microseconds time);
0084     bool sendTabletToolButton(quint32 button, bool pressed, std::chrono::microseconds time);
0085 
0086     InputDevice m_inputDevice;
0087     std::array<QHash<Trigger, std::variant<QKeySequence, MouseButton, TabletToolButton>>, LastType> m_actions;
0088     KConfigWatcher::Ptr m_configWatcher;
0089     std::optional<KWin::TabletToolId> m_tabletTool;
0090 };