aboutsummaryrefslogtreecommitdiffstats
path: root/sway/input/libinput.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2019-07-18 01:06:25 -0400
committerLibravatar Simon Ser <contact@emersion.fr>2019-07-23 20:45:46 +0300
commit1a16262903dce09a60f94698afa96c1b2c91c264 (patch)
tree5232ce26dccf8008bcc2cb0cda95dcde382e1c39 /sway/input/libinput.c
parentipc: add an input event (diff)
downloadsway-1a16262903dce09a60f94698afa96c1b2c91c264.tar.gz
sway-1a16262903dce09a60f94698afa96c1b2c91c264.tar.zst
sway-1a16262903dce09a60f94698afa96c1b2c91c264.zip
ipc: add input::libinput_config event
This adds a libinput_config change type to the input event for when the libinput config for a device changes In order for this to be possible to track, the libinput config code had to be refactored. It is now extracted into a separate file to isolate it from the rest of the input management code.
Diffstat (limited to 'sway/input/libinput.c')
-rw-r--r--sway/input/libinput.c382
1 files changed, 382 insertions, 0 deletions
diff --git a/sway/input/libinput.c b/sway/input/libinput.c
new file mode 100644
index 00000000..60d7da46
--- /dev/null
+++ b/sway/input/libinput.c
@@ -0,0 +1,382 @@
1#include <float.h>
2#include <libinput.h>
3#include <limits.h>
4#include <wlr/backend/libinput.h>
5#include "log.h"
6#include "sway/config.h"
7#include "sway/input/input-manager.h"
8#include "sway/ipc-server.h"
9
10static void log_status(enum libinput_config_status status) {
11 if (status != LIBINPUT_CONFIG_STATUS_SUCCESS) {
12 sway_log(SWAY_ERROR, "Failed to apply libinput config: %s",
13 libinput_config_status_to_str(status));
14 }
15}
16
17static bool set_send_events(struct libinput_device *device, uint32_t mode) {
18 if ((libinput_device_config_send_events_get_mode(device) & mode) == 0) {
19 return false;
20 }
21 sway_log(SWAY_DEBUG, "send_events_set_mode(%d)", mode);
22 log_status(libinput_device_config_send_events_set_mode(device, mode));
23 return true;
24}
25
26static bool set_tap(struct libinput_device *device,
27 enum libinput_config_tap_state tap) {
28 if (libinput_device_config_tap_get_finger_count(device) <= 0 ||
29 libinput_device_config_tap_get_enabled(device) == tap) {
30 return false;
31 }
32 sway_log(SWAY_DEBUG, "tap_set_enabled(%d)", tap);
33 log_status(libinput_device_config_tap_set_enabled(device, tap));
34 return true;
35}
36
37static bool set_tap_button_map(struct libinput_device *device,
38 enum libinput_config_tap_button_map map) {
39 if (libinput_device_config_tap_get_finger_count(device) <= 0 ||
40 libinput_device_config_tap_get_button_map(device) == map) {
41 return false;
42 }
43 sway_log(SWAY_DEBUG, "tap_set_button_map(%d)", map);
44 log_status(libinput_device_config_tap_set_button_map(device, map));
45 return true;
46}
47
48static bool set_tap_drag(struct libinput_device *device,
49 enum libinput_config_drag_state drag) {
50 if (libinput_device_config_tap_get_finger_count(device) <= 0 ||
51 libinput_device_config_tap_get_drag_enabled(device) == drag) {
52 return false;
53 }
54 sway_log(SWAY_DEBUG, "tap_set_drag_enabled(%d)", drag);
55 log_status(libinput_device_config_tap_set_drag_enabled(device, drag));
56 return true;
57}
58
59static bool set_tap_drag_lock(struct libinput_device *device,
60 enum libinput_config_drag_lock_state lock) {
61 if (libinput_device_config_tap_get_finger_count(device) <= 0 ||
62 libinput_device_config_tap_get_drag_lock_enabled(device) == lock) {
63 return false;
64 }
65 sway_log(SWAY_DEBUG, "tap_set_drag_lock_enabled(%d)", lock);
66 log_status(libinput_device_config_tap_set_drag_lock_enabled(device, lock));
67 return true;
68}
69
70static bool set_accel_speed(struct libinput_device *device, double speed) {
71 if (!libinput_device_config_accel_is_available(device) ||
72 libinput_device_config_accel_get_speed(device) == speed) {
73 return false;
74 }
75 sway_log(SWAY_DEBUG, "accel_set_speed(%f)", speed);
76 log_status(libinput_device_config_accel_set_speed(device, speed));
77 return true;
78}
79
80static bool set_accel_profile(struct libinput_device *device,
81 enum libinput_config_accel_profile profile) {
82 if (!libinput_device_config_accel_is_available(device) ||
83 libinput_device_config_accel_get_profile(device) == profile) {
84 return false;
85 }
86 sway_log(SWAY_DEBUG, "accel_set_profile(%d)", profile);
87 log_status(libinput_device_config_accel_set_profile(device, profile));
88 return true;
89}
90
91static bool set_natural_scroll(struct libinput_device *d, bool n) {
92 if (!libinput_device_config_scroll_has_natural_scroll(d) ||
93 libinput_device_config_scroll_get_natural_scroll_enabled(d) == n) {
94 return false;
95 }
96 sway_log(SWAY_DEBUG, "scroll_set_natural_scroll(%d)", n);
97 log_status(libinput_device_config_scroll_set_natural_scroll_enabled(d, n));
98 return true;
99}
100
101static bool set_left_handed(struct libinput_device *device, bool left) {
102 if (!libinput_device_config_left_handed_is_available(device) ||
103 libinput_device_config_left_handed_get(device) == left) {
104 return false;
105 }
106 sway_log(SWAY_DEBUG, "left_handed_set(%d)", left);
107 log_status(libinput_device_config_left_handed_set(device, left));
108 return true;
109}
110
111static bool set_click_method(struct libinput_device *device,
112 enum libinput_config_click_method method) {
113 uint32_t click = libinput_device_config_click_get_methods(device);
114 if ((click & ~LIBINPUT_CONFIG_CLICK_METHOD_NONE) == 0 ||
115 libinput_device_config_click_get_method(device) == method) {
116 return false;
117 }
118 sway_log(SWAY_DEBUG, "click_set_method(%d)", method);
119 log_status(libinput_device_config_click_set_method(device, method));
120 return true;
121}
122
123static bool set_middle_emulation(struct libinput_device *dev,
124 enum libinput_config_middle_emulation_state mid) {
125 if (!libinput_device_config_middle_emulation_is_available(dev) ||
126 libinput_device_config_middle_emulation_get_enabled(dev) == mid) {
127 return false;
128 }
129 sway_log(SWAY_DEBUG, "middle_emulation_set_enabled(%d)", mid);
130 log_status(libinput_device_config_left_handed_set(dev, mid));
131 return true;
132}
133
134static bool set_scroll_method(struct libinput_device *device,
135 enum libinput_config_scroll_method method) {
136 uint32_t scroll = libinput_device_config_scroll_get_methods(device);
137 if ((scroll & ~LIBINPUT_CONFIG_SCROLL_NO_SCROLL) == 0 ||
138 libinput_device_config_scroll_get_method(device) == method) {
139 return false;
140 }
141 sway_log(SWAY_DEBUG, "scroll_set_method(%d)", method);
142 log_status(libinput_device_config_scroll_set_method(device, method));
143 return true;
144}
145
146static bool set_scroll_button(struct libinput_device *dev, uint32_t button) {
147 uint32_t scroll = libinput_device_config_scroll_get_methods(dev);
148 if ((scroll & ~LIBINPUT_CONFIG_SCROLL_NO_SCROLL) == 0 ||
149 libinput_device_config_scroll_get_button(dev) == button) {
150 return false;
151 }
152 sway_log(SWAY_DEBUG, "scroll_set_button(%d)", button);
153 log_status(libinput_device_config_scroll_set_button(dev, button));
154 return true;
155}
156
157static bool set_dwt(struct libinput_device *device, bool dwt) {
158 if (!libinput_device_config_dwt_is_available(device) ||
159 libinput_device_config_dwt_get_enabled(device) == dwt) {
160 return false;
161 }
162 sway_log(SWAY_DEBUG, "dwt_set_enabled(%d)", dwt);
163 log_status(libinput_device_config_dwt_set_enabled(device, dwt));
164 return true;
165}
166
167static bool set_calibration_matrix(struct libinput_device *dev, float mat[6]) {
168 if (!libinput_device_config_calibration_has_matrix(dev)) {
169 return false;
170 }
171 bool changed = false;
172 float current[6];
173 libinput_device_config_calibration_get_matrix(dev, current);
174 for (int i = 0; i < 6; i++) {
175 if (current[i] != mat[i]) {
176 changed = true;
177 break;
178 }
179 }
180 if (changed) {
181 sway_log(SWAY_DEBUG, "calibration_set_matrix(%f, %f, %f, %f, %f, %f)",
182 mat[0], mat[1], mat[2], mat[3], mat[4], mat[5]);
183 log_status(libinput_device_config_calibration_set_matrix(dev, mat));
184 }
185 return changed;
186}
187
188static bool config_libinput_pointer(struct libinput_device *device,
189 struct input_config *ic, const char *device_id) {
190 sway_log(SWAY_DEBUG, "config_libinput_pointer('%s' on '%s')",
191 ic->identifier, device_id);
192 bool changed = false;
193 if (ic->send_events != INT_MIN) {
194 changed |= set_send_events(device, ic->send_events);
195 }
196 if (ic->tap != INT_MIN) {
197 changed |= set_tap(device, ic->tap);
198 }
199 if (ic->tap_button_map != INT_MIN) {
200 changed |= set_tap_button_map(device, ic->tap_button_map);
201 }
202 if (ic->drag != INT_MIN) {
203 changed |= set_tap_drag(device, ic->drag);
204 }
205 if (ic->drag_lock != INT_MIN) {
206 changed |= set_tap_drag_lock(device, ic->drag_lock);
207 }
208
209 if (ic->pointer_accel != FLT_MIN) {
210 changed |= set_accel_speed(device, ic->pointer_accel);
211 }
212 if (ic->accel_profile != INT_MIN) {
213 changed |= set_accel_profile(device, ic->accel_profile);
214 }
215 if (ic->natural_scroll != INT_MIN) {
216 changed |= set_natural_scroll(device, ic->natural_scroll);
217 }
218 if (ic->left_handed != INT_MIN) {
219 changed |= set_left_handed(device, ic->left_handed);
220 }
221 if (ic->click_method != INT_MIN) {
222 changed |= set_click_method(device, ic->click_method);
223 }
224 if (ic->middle_emulation != INT_MIN) {
225 changed |= set_middle_emulation(device, ic->middle_emulation);
226 }
227 if (ic->scroll_method != INT_MIN) {
228 changed |= set_scroll_method(device, ic->scroll_method);
229 }
230 if (ic->scroll_button != INT_MIN) {
231 changed |= set_scroll_button(device, ic->scroll_button);
232 }
233 if (ic->dwt != INT_MIN) {
234 changed |= set_dwt(device, ic->dwt);
235 }
236 return changed;
237}
238
239static bool config_libinput_keyboard(struct libinput_device *device,
240 struct input_config *ic, const char *device_id) {
241 sway_log(SWAY_DEBUG, "config_libinput_keyboard('%s' on '%s')",
242 ic->identifier, device_id);
243 if (ic->send_events != INT_MIN) {
244 return set_send_events(device, ic->send_events);
245 }
246 return false;
247}
248
249static bool config_libinput_switch(struct libinput_device *device,
250 struct input_config *ic, const char *device_id) {
251 sway_log(SWAY_DEBUG, "config_libinput_switch('%s' on '%s')",
252 ic->identifier, device_id);
253 if (ic->send_events != INT_MIN) {
254 return set_send_events(device, ic->send_events);
255 }
256 return false;
257}
258
259static bool config_libinput_touch(struct libinput_device *device,
260 struct input_config *ic, const char *device_id) {
261 sway_log(SWAY_DEBUG, "config_libinput_touch('%s' on '%s')",
262 ic->identifier, device_id);
263 bool changed = false;
264 if (ic->send_events != INT_MIN) {
265 changed |= set_send_events(device, ic->send_events);
266 }
267 if (ic->calibration_matrix.configured) {
268 changed |= set_calibration_matrix(device, ic->calibration_matrix.matrix);
269 }
270 return changed;
271}
272
273void sway_input_configure_libinput_device(struct sway_input_device *device) {
274 struct input_config *ic = input_device_get_config(device);
275 if (!ic || !wlr_input_device_is_libinput(device->wlr_device)) {
276 return;
277 }
278 bool changed = false;
279 const char *device_id = device->identifier;
280 struct libinput_device *libinput_device =
281 wlr_libinput_get_device_handle(device->wlr_device);
282 if (device->wlr_device->type == WLR_INPUT_DEVICE_POINTER ||
283 device->wlr_device->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
284 changed = config_libinput_pointer(libinput_device, ic, device_id);
285 } else if (device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
286 changed = config_libinput_keyboard(libinput_device, ic, device_id);
287 } else if (device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
288 changed = config_libinput_switch(libinput_device, ic, device_id);
289 } else if (device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
290 changed = config_libinput_touch(libinput_device, ic, device_id);
291 }
292 if (changed) {
293 ipc_event_input("libinput_config", device);
294 }
295}
296
297static bool reset_libinput_pointer(struct libinput_device *device,
298 const char *device_id) {
299 sway_log(SWAY_DEBUG, "reset_libinput_pointer(%s)", device_id);
300 bool changed = false;
301 changed |= set_send_events(device,
302 libinput_device_config_send_events_get_default_mode(device));
303 changed |= set_tap(device,
304 libinput_device_config_tap_get_default_enabled(device));
305 changed |= set_tap_button_map(device,
306 libinput_device_config_tap_get_button_map(device));
307 changed |= set_tap_drag(device,
308 libinput_device_config_tap_get_default_drag_enabled(device));
309 changed |= set_tap_drag_lock(device,
310 libinput_device_config_tap_get_default_drag_lock_enabled(device));
311 changed |= set_accel_speed(device,
312 libinput_device_config_accel_get_default_speed(device));
313 changed |= set_accel_profile(device,
314 libinput_device_config_accel_get_default_profile(device));
315 changed |= set_natural_scroll(device,
316 libinput_device_config_scroll_get_default_natural_scroll_enabled(
317 device));
318 changed |= set_left_handed(device,
319 libinput_device_config_left_handed_get_default(device));
320 changed |= set_click_method(device,
321 libinput_device_config_click_get_default_method(device));
322 changed |= set_middle_emulation(device,
323 libinput_device_config_middle_emulation_get_default_enabled(device));
324 changed |= set_scroll_method(device,
325 libinput_device_config_scroll_get_default_method(device));
326 changed |= set_scroll_button(device,
327 libinput_device_config_scroll_get_default_button(device));
328 changed |= set_dwt(device,
329 libinput_device_config_dwt_get_default_enabled(device));
330 return changed;
331}
332
333static bool reset_libinput_keyboard(struct libinput_device *device,
334 const char *device_id) {
335 sway_log(SWAY_DEBUG, "reset_libinput_keyboard(%s)", device_id);
336 return set_send_events(device,
337 libinput_device_config_send_events_get_default_mode(device));
338}
339
340static bool reset_libinput_switch(struct libinput_device *device,
341 const char *device_id) {
342 sway_log(SWAY_DEBUG, "reset_libinput_switch(%s)", device_id);
343 return set_send_events(device,
344 libinput_device_config_send_events_get_default_mode(device));
345}
346
347static bool reset_libinput_touch(struct libinput_device *device,
348 const char *device_id) {
349 sway_log(SWAY_DEBUG, "reset_libinput_touch(%s)", device_id);
350 bool changed = false;
351
352 changed |= set_send_events(device,
353 libinput_device_config_send_events_get_default_mode(device));
354
355 float matrix[6];
356 libinput_device_config_calibration_get_matrix(device, matrix);
357 changed |= set_calibration_matrix(device, matrix);
358
359 return changed;
360}
361
362void sway_input_reset_libinput_device(struct sway_input_device *device) {
363 if (!wlr_input_device_is_libinput(device->wlr_device)) {
364 return;
365 }
366 bool changed = false;
367 struct libinput_device *libinput_device =
368 wlr_libinput_get_device_handle(device->wlr_device);
369 if (device->wlr_device->type == WLR_INPUT_DEVICE_POINTER ||
370 device->wlr_device->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
371 changed = reset_libinput_pointer(libinput_device, device->identifier);
372 } else if (device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
373 changed = reset_libinput_keyboard(libinput_device, device->identifier);
374 } else if (device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
375 changed = reset_libinput_switch(libinput_device, device->identifier);
376 } else if (device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
377 changed = reset_libinput_touch(libinput_device, device->identifier);
378 }
379 if (changed) {
380 ipc_event_input("libinput_config", device);
381 }
382}