Changeset View
Changeset View
Standalone View
Standalone View
kcms/input/backends/x11/x11_libinput_dummydevice.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Roman Gilg <subdiff@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | #include "x11_libinput_dummydevice.h" | ||||
19 | #include "libinput_settings.h" | ||||
20 | | ||||
21 | #include <libinput-properties.h> | ||||
22 | | ||||
23 | #include <X11/Xlib.h> | ||||
24 | #include <X11/Xatom.h> | ||||
25 | #include <X11/extensions/XInput2.h> | ||||
26 | | ||||
27 | template<typename Callback> | ||||
28 | static void XI2ForallPointerDevices(Display* dpy, const Callback& callback) | ||||
29 | { | ||||
30 | int ndevices_return; | ||||
31 | XIDeviceInfo* info = XIQueryDevice(dpy, XIAllDevices, &ndevices_return); | ||||
32 | if (!info) { | ||||
33 | return; | ||||
34 | } | ||||
35 | for (int i = 0; i < ndevices_return; ++i) { | ||||
36 | if ((info + i)->use == XISlavePointer) { | ||||
37 | callback(info + i); | ||||
38 | } | ||||
39 | } | ||||
40 | XIFreeDeviceInfo(info); | ||||
41 | } | ||||
42 | | ||||
43 | struct ScopedXDeleter { | ||||
44 | static inline void cleanup(void* pointer) | ||||
45 | { | ||||
46 | if (pointer) { | ||||
47 | XFree(pointer); | ||||
48 | } | ||||
49 | } | ||||
50 | }; | ||||
51 | | ||||
52 | namespace { | ||||
53 | template<typename T> | ||||
54 | void valueWriterPart(T val, Atom valAtom, Display *dpy) | ||||
55 | { | ||||
56 | Q_UNUSED(val); | ||||
57 | Q_UNUSED(valAtom); | ||||
58 | Q_UNUSED(dpy); | ||||
59 | } | ||||
60 | | ||||
61 | template<> | ||||
62 | void valueWriterPart<bool>(bool val, Atom valAtom, Display *dpy) | ||||
63 | { | ||||
64 | XI2ForallPointerDevices(dpy, [&] (XIDeviceInfo *info) { | ||||
65 | Status status; | ||||
66 | Atom type_return; | ||||
67 | int format_return; | ||||
68 | unsigned long num_items_return; | ||||
69 | unsigned long bytes_after_return; | ||||
70 | | ||||
71 | unsigned char *_data = nullptr; | ||||
72 | //data returned is an 1 byte boolean | ||||
73 | status = XIGetProperty(dpy, info->deviceid, valAtom, 0, 1, | ||||
74 | False, XA_INTEGER, &type_return, &format_return, | ||||
75 | &num_items_return, &bytes_after_return, &_data); | ||||
76 | if (status != Success) { | ||||
77 | return; | ||||
78 | } | ||||
79 | | ||||
80 | QScopedArrayPointer<unsigned char, ScopedXDeleter> data(_data); | ||||
81 | _data = nullptr; | ||||
82 | | ||||
83 | | ||||
84 | if (type_return != XA_INTEGER || !data || format_return != 8 || num_items_return != 1) { | ||||
85 | return; | ||||
86 | } | ||||
87 | | ||||
88 | unsigned char sendVal = val ? 1 : 0; | ||||
89 | | ||||
90 | XIChangeProperty(dpy, info->deviceid, valAtom, XA_INTEGER, | ||||
91 | 8, XIPropModeReplace, &sendVal, 1); | ||||
92 | | ||||
93 | }); | ||||
94 | } | ||||
95 | | ||||
96 | template<> | ||||
97 | void valueWriterPart<qreal>(qreal val, Atom valAtom, Display *dpy) | ||||
98 | { | ||||
99 | XI2ForallPointerDevices(dpy, [&] (XIDeviceInfo *info) { | ||||
100 | Status status; | ||||
101 | Atom float_type = XInternAtom (dpy, "FLOAT", False); | ||||
102 | Atom type_return; | ||||
103 | int format_return; | ||||
104 | unsigned long num_items_return; | ||||
105 | unsigned long bytes_after_return; | ||||
106 | | ||||
107 | unsigned char *_data = nullptr; | ||||
108 | //data returned is an 1 byte boolean | ||||
109 | status = XIGetProperty(dpy, info->deviceid, valAtom, 0, 1, | ||||
110 | False, float_type, &type_return, &format_return, | ||||
111 | &num_items_return, &bytes_after_return, &_data); | ||||
112 | if (status != Success) { | ||||
113 | return; | ||||
114 | } | ||||
115 | | ||||
116 | QScopedArrayPointer<unsigned char, ScopedXDeleter> data(_data); | ||||
117 | _data = nullptr; | ||||
118 | | ||||
119 | | ||||
120 | if (type_return != float_type || !data || format_return != 32 || num_items_return != 1) { | ||||
121 | return; | ||||
122 | } | ||||
123 | | ||||
124 | unsigned char buffer[4096]; | ||||
125 | float *sendPtr = (float*)buffer; | ||||
126 | *sendPtr = val; | ||||
127 | | ||||
128 | XIChangeProperty(dpy, info->deviceid, valAtom, float_type, | ||||
129 | format_return, XIPropModeReplace, buffer, 1); | ||||
130 | | ||||
131 | }); | ||||
132 | } | ||||
133 | } | ||||
134 | | ||||
135 | X11LibinputDummyDevice::X11LibinputDummyDevice(QObject *parent, Display *dpy) | ||||
136 | : QObject(parent), | ||||
137 | m_settings(new LibinputSettings()), | ||||
138 | m_dpy(dpy) | ||||
139 | { | ||||
140 | m_leftHanded.atom = XInternAtom(dpy, LIBINPUT_PROP_LEFT_HANDED, True); | ||||
141 | m_middleEmulation.atom = XInternAtom(dpy, LIBINPUT_PROP_MIDDLE_EMULATION_ENABLED, True); | ||||
142 | m_naturalScroll.atom = XInternAtom(dpy, LIBINPUT_PROP_NATURAL_SCROLL, True); | ||||
143 | m_pointerAcceleration.atom = XInternAtom(dpy, LIBINPUT_PROP_ACCEL, True); | ||||
144 | m_pointerAccelerationProfileFlat.atom = XInternAtom(dpy, LIBINPUT_PROP_ACCEL_PROFILE_ENABLED, True); | ||||
145 | | ||||
146 | m_supportsDisableEvents.val = false; | ||||
147 | m_enabled.val = true; | ||||
148 | m_supportedButtons.val = Qt::LeftButton | Qt::MidButton | Qt::RightButton; | ||||
149 | m_supportsLeftHanded.val = true; | ||||
150 | m_supportsMiddleEmulation.val = true; | ||||
151 | m_middleEmulationEnabledByDefault.val = false; | ||||
152 | | ||||
153 | m_supportsPointerAcceleration.val = true; | ||||
154 | m_defaultPointerAcceleration.val = 0; | ||||
155 | | ||||
156 | m_supportsPointerAccelerationProfileAdaptive.val = true; | ||||
157 | m_supportsPointerAccelerationProfileFlat.val = true; | ||||
158 | | ||||
159 | m_defaultPointerAccelerationProfileAdaptive.val = true; | ||||
160 | m_defaultPointerAccelerationProfileFlat.val = false; | ||||
161 | | ||||
162 | m_supportsNaturalScroll.val = true; | ||||
163 | m_naturalScrollEnabledByDefault.val = false; | ||||
164 | } | ||||
165 | | ||||
166 | X11LibinputDummyDevice::~X11LibinputDummyDevice() | ||||
167 | { | ||||
168 | delete m_settings; | ||||
169 | } | ||||
170 | | ||||
171 | bool X11LibinputDummyDevice::getConfig() | ||||
172 | { | ||||
173 | auto reset = [this](Prop<bool> &prop, bool defVal) { | ||||
174 | prop.reset(m_settings->load(prop.cfgName, defVal)); | ||||
175 | }; | ||||
176 | | ||||
177 | reset(m_leftHanded, false); | ||||
178 | | ||||
179 | reset(m_middleEmulation, false); | ||||
180 | reset(m_naturalScroll, false); | ||||
181 | reset(m_pointerAccelerationProfileFlat, false); | ||||
182 | | ||||
183 | m_pointerAccelerationProfileAdaptive.reset(!m_settings->load(m_pointerAccelerationProfileFlat.cfgName, false)); | ||||
184 | m_pointerAcceleration.reset(m_settings->load(m_pointerAcceleration.cfgName, 0.)); | ||||
185 | | ||||
186 | return true; | ||||
187 | } | ||||
188 | | ||||
189 | bool X11LibinputDummyDevice::getDefaultConfig() | ||||
190 | { | ||||
191 | m_leftHanded.set(false); | ||||
192 | | ||||
193 | m_pointerAcceleration.set(m_defaultPointerAcceleration); | ||||
194 | m_pointerAccelerationProfileFlat.set(m_defaultPointerAccelerationProfileFlat); | ||||
195 | m_pointerAccelerationProfileAdaptive.set(m_defaultPointerAccelerationProfileAdaptive); | ||||
196 | | ||||
197 | m_middleEmulation.set(m_middleEmulationEnabledByDefault); | ||||
198 | m_naturalScroll.set(m_naturalScrollEnabledByDefault); | ||||
199 | | ||||
200 | return true; | ||||
201 | } | ||||
202 | | ||||
203 | bool X11LibinputDummyDevice::applyConfig() | ||||
204 | { | ||||
205 | valueWriter(m_leftHanded); | ||||
206 | valueWriter(m_middleEmulation); | ||||
207 | valueWriter(m_naturalScroll); | ||||
208 | valueWriter(m_pointerAcceleration); | ||||
209 | valueWriter(m_pointerAccelerationProfileFlat); | ||||
210 | | ||||
211 | return true; | ||||
212 | } | ||||
213 | | ||||
214 | template<typename T> | ||||
215 | bool X11LibinputDummyDevice::valueWriter(Prop<T> &prop) | ||||
216 | { | ||||
217 | // Check atom availability first. | ||||
218 | if (prop.atom == None) { | ||||
219 | return false; | ||||
220 | } | ||||
221 | | ||||
222 | if (prop.val != prop.old) { | ||||
223 | m_settings->save(prop.cfgName, prop.val); | ||||
224 | } | ||||
225 | | ||||
226 | valueWriterPart(prop.val, prop.atom, m_dpy); | ||||
227 | | ||||
228 | prop.old = prop.val; | ||||
229 | | ||||
230 | return true; | ||||
231 | } | ||||
232 | | ||||
233 | bool X11LibinputDummyDevice::isChangedConfig() const | ||||
234 | { | ||||
235 | return m_leftHanded.changed() || | ||||
236 | m_pointerAcceleration.changed() || | ||||
237 | m_pointerAccelerationProfileFlat.changed() || | ||||
238 | m_pointerAccelerationProfileAdaptive.changed() || | ||||
239 | m_middleEmulation.changed() || | ||||
240 | m_naturalScroll.changed(); | ||||
241 | } |