1 /*
2     SPDX-FileCopyrightText: 2018 Roman Gilg <subdiff@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #ifndef KWINWAYLANDDEVICE_H
8 #define KWINWAYLANDDEVICE_H
9 
10 #include <QObject>
11 #include <QString>
12 
13 class QDBusInterface;
14 
15 class KWinWaylandDevice : public QObject
16 {
17     Q_OBJECT
18 
19     //
20     // general
21     Q_PROPERTY(QString name READ name CONSTANT)
22     Q_PROPERTY(bool supportsDisableEvents READ supportsDisableEvents CONSTANT)
23     Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
24 
25     //
26     // advanced
27     Q_PROPERTY(Qt::MouseButtons supportedButtons READ supportedButtons CONSTANT)
28 
29     Q_PROPERTY(bool supportsLeftHanded READ supportsLeftHanded CONSTANT)
30     Q_PROPERTY(bool leftHandedEnabledByDefault READ leftHandedEnabledByDefault CONSTANT)
31     Q_PROPERTY(bool leftHanded READ isLeftHanded WRITE setLeftHanded NOTIFY leftHandedChanged)
32 
33     Q_PROPERTY(bool supportsMiddleEmulation READ supportsMiddleEmulation CONSTANT)
34     Q_PROPERTY(bool middleEmulationEnabledByDefault READ middleEmulationEnabledByDefault CONSTANT)
35     Q_PROPERTY(bool middleEmulation READ isMiddleEmulation WRITE setMiddleEmulation NOTIFY middleEmulationChanged)
36 
37     //
38     // acceleration speed and profile
39     Q_PROPERTY(bool supportsPointerAcceleration READ supportsPointerAcceleration CONSTANT)
40     Q_PROPERTY(qreal pointerAcceleration READ pointerAcceleration WRITE setPointerAcceleration NOTIFY pointerAccelerationChanged)
41 
42     Q_PROPERTY(bool supportsPointerAccelerationProfileFlat READ supportsPointerAccelerationProfileFlat CONSTANT)
43     Q_PROPERTY(bool defaultPointerAccelerationProfileFlat READ defaultPointerAccelerationProfileFlat CONSTANT)
44     Q_PROPERTY(bool pointerAccelerationProfileFlat READ pointerAccelerationProfileFlat WRITE setPointerAccelerationProfileFlat NOTIFY
45                    pointerAccelerationProfileChanged)
46 
47     Q_PROPERTY(bool supportsPointerAccelerationProfileAdaptive READ supportsPointerAccelerationProfileAdaptive CONSTANT)
48     Q_PROPERTY(bool defaultPointerAccelerationProfileAdaptive READ defaultPointerAccelerationProfileAdaptive CONSTANT)
49     Q_PROPERTY(bool pointerAccelerationProfileAdaptive READ pointerAccelerationProfileAdaptive WRITE setPointerAccelerationProfileAdaptive NOTIFY
50                    pointerAccelerationProfileChanged)
51 
52     //
53     // scrolling
54     Q_PROPERTY(bool supportsNaturalScroll READ supportsNaturalScroll CONSTANT)
55     Q_PROPERTY(bool naturalScrollEnabledByDefault READ naturalScrollEnabledByDefault CONSTANT)
56     Q_PROPERTY(bool naturalScroll READ isNaturalScroll WRITE setNaturalScroll NOTIFY naturalScrollChanged)
57     Q_PROPERTY(qreal scrollFactor READ scrollFactor WRITE setScrollFactor NOTIFY scrollFactorChanged)
58 
59 public:
60     KWinWaylandDevice(QString dbusName);
61     ~KWinWaylandDevice() override;
62 
63     bool init();
64 
65     bool getConfig();
66     bool getDefaultConfig();
67     bool applyConfig();
68     bool isChangedConfig() const;
69 
70     //
71     // general
name()72     QString name() const
73     {
74         return m_name.val;
75     }
sysName()76     QString sysName() const
77     {
78         return m_sysName.val;
79     }
supportsDisableEvents()80     bool supportsDisableEvents() const
81     {
82         return m_supportsDisableEvents.val;
83     }
setEnabled(bool enabled)84     void setEnabled(bool enabled)
85     {
86         m_enabled.set(enabled);
87     }
isEnabled()88     bool isEnabled() const
89     {
90         return m_enabled.val;
91     }
supportedButtons()92     Qt::MouseButtons supportedButtons() const
93     {
94         return m_supportedButtons.val;
95     }
96 
97     //
98     // advanced
supportsLeftHanded()99     bool supportsLeftHanded() const
100     {
101         return m_supportsLeftHanded.val;
102     }
leftHandedEnabledByDefault()103     bool leftHandedEnabledByDefault() const
104     {
105         return m_leftHandedEnabledByDefault.val;
106     }
isLeftHanded()107     bool isLeftHanded() const
108     {
109         return m_leftHanded.val;
110     }
setLeftHanded(bool set)111     void setLeftHanded(bool set)
112     {
113         m_leftHanded.set(set);
114     }
115 
supportsMiddleEmulation()116     bool supportsMiddleEmulation() const
117     {
118         return m_supportsMiddleEmulation.val;
119     }
middleEmulationEnabledByDefault()120     bool middleEmulationEnabledByDefault() const
121     {
122         return m_middleEmulationEnabledByDefault.val;
123     }
isMiddleEmulation()124     bool isMiddleEmulation() const
125     {
126         return m_middleEmulation.val;
127     }
setMiddleEmulation(bool set)128     void setMiddleEmulation(bool set)
129     {
130         m_middleEmulation.set(set);
131     }
132 
133     //
134     // acceleration speed and profile
supportsPointerAcceleration()135     bool supportsPointerAcceleration() const
136     {
137         return m_supportsPointerAcceleration.val;
138     }
pointerAcceleration()139     qreal pointerAcceleration() const
140     {
141         return m_pointerAcceleration.val;
142     }
setPointerAcceleration(qreal acceleration)143     void setPointerAcceleration(qreal acceleration)
144     {
145         m_pointerAcceleration.set(acceleration);
146     }
147 
supportsPointerAccelerationProfileFlat()148     bool supportsPointerAccelerationProfileFlat() const
149     {
150         return m_supportsPointerAccelerationProfileFlat.val;
151     }
defaultPointerAccelerationProfileFlat()152     bool defaultPointerAccelerationProfileFlat() const
153     {
154         return m_defaultPointerAccelerationProfileFlat.val;
155     }
pointerAccelerationProfileFlat()156     bool pointerAccelerationProfileFlat() const
157     {
158         return m_pointerAccelerationProfileFlat.val;
159     }
setPointerAccelerationProfileFlat(bool set)160     void setPointerAccelerationProfileFlat(bool set)
161     {
162         m_pointerAccelerationProfileFlat.set(set);
163     }
164 
supportsPointerAccelerationProfileAdaptive()165     bool supportsPointerAccelerationProfileAdaptive() const
166     {
167         return m_supportsPointerAccelerationProfileAdaptive.val;
168     }
defaultPointerAccelerationProfileAdaptive()169     bool defaultPointerAccelerationProfileAdaptive() const
170     {
171         return m_defaultPointerAccelerationProfileAdaptive.val;
172     }
pointerAccelerationProfileAdaptive()173     bool pointerAccelerationProfileAdaptive() const
174     {
175         return m_pointerAccelerationProfileAdaptive.val;
176     }
setPointerAccelerationProfileAdaptive(bool set)177     void setPointerAccelerationProfileAdaptive(bool set)
178     {
179         m_pointerAccelerationProfileAdaptive.set(set);
180     }
181 
182     //
183     // scrolling
supportsNaturalScroll()184     bool supportsNaturalScroll() const
185     {
186         return m_supportsNaturalScroll.val;
187     }
naturalScrollEnabledByDefault()188     bool naturalScrollEnabledByDefault() const
189     {
190         return m_naturalScrollEnabledByDefault.val;
191     }
isNaturalScroll()192     bool isNaturalScroll() const
193     {
194         return m_naturalScroll.val;
195     }
setNaturalScroll(bool set)196     void setNaturalScroll(bool set)
197     {
198         m_naturalScroll.set(set);
199     }
200 
scrollFactor()201     qreal scrollFactor() const
202     {
203         return m_scrollFactor.val;
204     }
setScrollFactor(qreal set)205     void setScrollFactor(qreal set)
206     {
207         m_scrollFactor.set(set);
208     }
209 
210 Q_SIGNALS:
211     void leftHandedChanged();
212     void pointerAccelerationChanged();
213     void pointerAccelerationProfileChanged();
214     void enabledChanged();
215     void middleEmulationChanged();
216     void naturalScrollChanged();
217     void scrollFactorChanged();
218 
219 private:
220     template<typename T>
221     struct Prop {
PropProp222         explicit Prop(const QByteArray &dbusName)
223             : dbus(dbusName)
224         {
225         }
226 
setProp227         void set(T newVal)
228         {
229             if (avail && val != newVal) {
230                 val = newVal;
231             }
232         }
setProp233         void set(const Prop<T> &p)
234         {
235             if (avail && val != p.val) {
236                 val = p.val;
237             }
238         }
changedProp239         bool changed() const
240         {
241             return avail && (old != val);
242         }
243 
244         QByteArray dbus;
245         bool avail;
246         T old;
247         T val;
248     };
249 
250     template<typename T>
251     bool valueLoader(Prop<T> &prop);
252 
253     template<typename T>
254     QString valueWriter(const Prop<T> &prop);
255 
256     //
257     // general
258     Prop<QString> m_name = Prop<QString>("name");
259     Prop<QString> m_sysName = Prop<QString>("sysName");
260     Prop<bool> m_supportsDisableEvents = Prop<bool>("supportsDisableEvents");
261     Prop<bool> m_enabled = Prop<bool>("enabled");
262 
263     //
264     // advanced
265     Prop<Qt::MouseButtons> m_supportedButtons = Prop<Qt::MouseButtons>("supportedButtons");
266 
267     Prop<bool> m_supportsLeftHanded = Prop<bool>("supportsLeftHanded");
268     Prop<bool> m_leftHandedEnabledByDefault = Prop<bool>("leftHandedEnabledByDefault");
269     Prop<bool> m_leftHanded = Prop<bool>("leftHanded");
270 
271     Prop<bool> m_supportsMiddleEmulation = Prop<bool>("supportsMiddleEmulation");
272     Prop<bool> m_middleEmulationEnabledByDefault = Prop<bool>("middleEmulationEnabledByDefault");
273     Prop<bool> m_middleEmulation = Prop<bool>("middleEmulation");
274 
275     //
276     // acceleration speed and profile
277     Prop<bool> m_supportsPointerAcceleration = Prop<bool>("supportsPointerAcceleration");
278     Prop<qreal> m_defaultPointerAcceleration = Prop<qreal>("defaultPointerAcceleration");
279     Prop<qreal> m_pointerAcceleration = Prop<qreal>("pointerAcceleration");
280 
281     Prop<bool> m_supportsPointerAccelerationProfileFlat = Prop<bool>("supportsPointerAccelerationProfileFlat");
282     Prop<bool> m_defaultPointerAccelerationProfileFlat = Prop<bool>("defaultPointerAccelerationProfileFlat");
283     Prop<bool> m_pointerAccelerationProfileFlat = Prop<bool>("pointerAccelerationProfileFlat");
284 
285     Prop<bool> m_supportsPointerAccelerationProfileAdaptive = Prop<bool>("supportsPointerAccelerationProfileAdaptive");
286     Prop<bool> m_defaultPointerAccelerationProfileAdaptive = Prop<bool>("defaultPointerAccelerationProfileAdaptive");
287     Prop<bool> m_pointerAccelerationProfileAdaptive = Prop<bool>("pointerAccelerationProfileAdaptive");
288 
289     //
290     // scrolling
291     Prop<bool> m_supportsNaturalScroll = Prop<bool>("supportsNaturalScroll");
292     Prop<bool> m_naturalScrollEnabledByDefault = Prop<bool>("naturalScrollEnabledByDefault");
293     Prop<bool> m_naturalScroll = Prop<bool>("naturalScroll");
294     Prop<qreal> m_scrollFactor = Prop<qreal>("scrollFactor");
295 
296     QDBusInterface *m_iface;
297 };
298 
299 #endif // KWINWAYLANDDEVICE_H
300