Debian Codegraph @

src/shared.c

raw symbols SCIP
1 /* Adapted from test/shared.c from the libinput distribution */
2 /* tools/shared.c 1.23.0:
3 *
4 * Copyright © 2014 Red Hat, Inc.
5 * Modifications: Copyright © 2019 Bill Allombert <[email protected]>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <fnmatch.h>
30 #include <getopt.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 #include <libudev.h>
37 #include <unistd.h>
38
39 #include <libevdev/libevdev.h>
40
41 #include "shared.h"
42 #include "util-strings.h"
43
44 static void
45 log_handler(struct libinput *li,
46 enum libinput_log_priority priority,
47 const char *format,
48 va_list args)
49 {
50 vprintf(format, args);
51 }
52
53 void
54 tools_init_options(struct tools_options *options)
55 {
56 memset(options, 0, sizeof(*options));
57 options->tapping = -1;
58 options->tap_map = -1;
59 options->drag = -1;
60 options->drag_lock = -1;
61 options->natural_scroll = -1;
62 options->left_handed = -1;
63 options->middlebutton = -1;
64 options->dwt = -1;
65 options->dwtp = -1;
66 options->click_method = -1;
67 options->scroll_method = -1;
68 options->scroll_button = -1;
69 options->scroll_button_lock = -1;
70 options->speed = 0.0;
71 options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_NONE;
72 /* initialize accel args */
73 #ifdef HAVE_LIBINPUT_1_23
74 static double points[] = {0.0, 1.0};
75 options->custom_points = points;
76 options->custom_npoints = ARRAY_LENGTH(points);
77 options->custom_type = LIBINPUT_ACCEL_TYPE_FALLBACK;
78 options->custom_step = 1.0;
79 #endif
80 }
81
82 int
83 tools_parse_option(int option,
84 const char *optarg,
85 struct tools_options *options)
86 {
87 switch(option) {
88 case OPT_TAP_ENABLE:
89 options->tapping = 1;
90 break;
91 case OPT_TAP_DISABLE:
92 options->tapping = 0;
93 break;
94 case OPT_TAP_MAP:
95 if (!optarg)
96 return 1;
97
98 if (streq(optarg, "lrm")) {
99 options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LRM;
100 } else if (streq(optarg, "lmr")) {
101 options->tap_map = LIBINPUT_CONFIG_TAP_MAP_LMR;
102 } else {
103 return 1;
104 }
105 break;
106 case OPT_DRAG_ENABLE:
107 options->drag = 1;
108 break;
109 case OPT_DRAG_DISABLE:
110 options->drag = 0;
111 break;
112 case OPT_DRAG_LOCK_ENABLE:
113 options->drag_lock = 1;
114 break;
115 case OPT_DRAG_LOCK_DISABLE:
116 options->drag_lock = 0;
117 break;
118 case OPT_NATURAL_SCROLL_ENABLE:
119 options->natural_scroll = 1;
120 break;
121 case OPT_NATURAL_SCROLL_DISABLE:
122 options->natural_scroll = 0;
123 break;
124 case OPT_LEFT_HANDED_ENABLE:
125 options->left_handed = 1;
126 break;
127 case OPT_LEFT_HANDED_DISABLE:
128 options->left_handed = 0;
129 break;
130 case OPT_MIDDLEBUTTON_ENABLE:
131 options->middlebutton = 1;
132 break;
133 case OPT_MIDDLEBUTTON_DISABLE:
134 options->middlebutton = 0;
135 break;
136 case OPT_DWT_ENABLE:
137 options->dwt = LIBINPUT_CONFIG_DWT_ENABLED;
138 break;
139 case OPT_DWT_DISABLE:
140 options->dwt = LIBINPUT_CONFIG_DWT_DISABLED;
141 break;
142 case OPT_DWTP_ENABLE:
143 #ifdef HAVE_LIBINPUT_1_21
144 options->dwtp = LIBINPUT_CONFIG_DWTP_ENABLED;
145 #endif
146 break;
147 case OPT_DWTP_DISABLE:
148 #ifdef HAVE_LIBINPUT_1_21
149 options->dwtp = LIBINPUT_CONFIG_DWTP_DISABLED;
150 #endif
151 break;
152 case OPT_CLICK_METHOD:
153 if (!optarg)
154 return 1;
155
156 if (streq(optarg, "none")) {
157 options->click_method =
158 LIBINPUT_CONFIG_CLICK_METHOD_NONE;
159 } else if (streq(optarg, "clickfinger")) {
160 options->click_method =
161 LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER;
162 } else if (streq(optarg, "buttonareas")) {
163 options->click_method =
164 LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS;
165 } else {
166 return 1;
167 }
168 break;
169 case OPT_CLICKFINGER_MAP:
170 if (!optarg)
171 return 1;
172
173 if (streq(optarg, "lrm")) {
174 #ifdef HAVE_LIBINPUT_1_26
175 options->clickfinger_map = LIBINPUT_CONFIG_CLICKFINGER_MAP_LRM;
176 #endif
177 } else if (streq(optarg, "lmr")) {
178 #ifdef HAVE_LIBINPUT_1_26
179 options->clickfinger_map = LIBINPUT_CONFIG_CLICKFINGER_MAP_LMR;
180 #endif
181 } else {
182 return 1;
183 }
184 break;
185 case OPT_SCROLL_METHOD:
186 if (!optarg)
187 return 1;
188
189 if (streq(optarg, "none")) {
190 options->scroll_method =
191 LIBINPUT_CONFIG_SCROLL_NO_SCROLL;
192 } else if (streq(optarg, "twofinger")) {
193 options->scroll_method =
194 LIBINPUT_CONFIG_SCROLL_2FG;
195 } else if (streq(optarg, "edge")) {
196 options->scroll_method =
197 LIBINPUT_CONFIG_SCROLL_EDGE;
198 } else if (streq(optarg, "button")) {
199 options->scroll_method =
200 LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN;
201 } else {
202 return 1;
203 }
204 break;
205 case OPT_SCROLL_BUTTON:
206 if (!optarg) {
207 return 1;
208 }
209 options->scroll_button =
210 libevdev_event_code_from_name(EV_KEY,
211 optarg);
212 if (options->scroll_button == -1) {
213 fprintf(stderr,
214 "Invalid button %s\n",
215 optarg);
216 return 1;
217 }
218 break;
219 case OPT_SCROLL_BUTTON_LOCK_ENABLE:
220 options->scroll_button_lock = true;
221 break;
222 case OPT_SCROLL_BUTTON_LOCK_DISABLE:
223 options->scroll_button_lock = false;
224 break;
225 case OPT_SPEED:
226 if (!optarg)
227 return 1;
228 options->speed = atof(optarg);
229 break;
230 case OPT_PROFILE:
231 if (!optarg)
232 return 1;
233
234 if (streq(optarg, "adaptive"))
235 options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE;
236 else if (streq(optarg, "flat"))
237 options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT;
238 #ifdef HAVE_LIBINPUT_1_23
239 else if (streq(optarg, "custom"))
240 options->profile = LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM;
241 #endif
242 else
243 return 1;
244 break;
245 case OPT_DISABLE_SENDEVENTS:
246 if (!optarg)
247 return 1;
248
249 snprintf(options->disable_pattern,
250 sizeof(options->disable_pattern),
251 "%s",
252 optarg);
253 break;
254 case OPT_APPLY_TO:
255 if (!optarg)
256 return 1;
257
258 snprintf(options->match,
259 sizeof(options->match),
260 "%s",
261 optarg);
262 break;
263 case OPT_CUSTOM_POINTS:
264 if (!optarg)
265 return 1;
266 #ifdef HAVE_LIBINPUT_1_23
267 options->custom_points = double_array_from_string(optarg,
268 ";",
269 &options->custom_npoints);
270 if (!options->custom_points || options->custom_npoints < 2) {
271 fprintf(stderr,
272 "Invalid --set-custom-points\n"
273 "Please provide at least 2 points separated by a semicolon\n"
274 " e.g. --set-custom-points=\"1.0;1.5\"\n");
275 return 1;
276 }
277 #endif
278 break;
279 case OPT_CUSTOM_STEP:
280 if (!optarg)
281 return 1;
282 #ifdef HAVE_LIBINPUT_1_23
283 options->custom_step = strtod(optarg, NULL);
284 #endif
285 break;
286 case OPT_CUSTOM_TYPE:
287 if (!optarg)
288 return 1;
289 #ifdef HAVE_LIBINPUT_1_23
290 if (streq(optarg, "fallback"))
291 options->custom_type = LIBINPUT_ACCEL_TYPE_FALLBACK;
292 else if (streq(optarg, "motion"))
293 options->custom_type = LIBINPUT_ACCEL_TYPE_MOTION;
294 else if (streq(optarg, "scroll"))
295 options->custom_type = LIBINPUT_ACCEL_TYPE_SCROLL;
296 else {
297 fprintf(stderr, "Invalid --set-custom-type\n"
298 "Valid custom types: fallback|motion|scroll\n");
299 return 1;
300 }
301 #endif
302 break;
303 case OPT_ROTATION_ANGLE:
304 if (!optarg)
305 return 1;
306
307 if (!safe_atou(optarg, &options->angle)) {
308 fprintf(stderr, "Invalid --set-rotation-angle value\n");
309 return 1;
310 }
311 }
312 return 0;
313 }
314
315 static int
316 open_restricted(const char *path, int flags, void *user_data)
317 {
318 bool *grab = user_data;
319 int fd = open(path, flags);
320
321 if (fd < 0)
322 fprintf(stderr, "Failed to open %s (%s)\n",
323 path, strerror(errno));
324 else if (grab && *grab && ioctl(fd, EVIOCGRAB, (void*)1) == -1)
325 fprintf(stderr, "Grab requested, but failed for %s (%s)\n",
326 path, strerror(errno));
327
328 return fd < 0 ? -errno : fd;
329 }
330
331 static void
332 close_restricted(int fd, void *user_data)
333 {
334 close(fd);
335 }
336
337 static const struct libinput_interface interface = {
338 .open_restricted = open_restricted,
339 .close_restricted = close_restricted,
340 };
341
342 static struct libinput *
343 tools_open_udev(const char *seat, bool verbose, bool *grab)
344 {
345 struct libinput *li;
346 struct udev *udev = udev_new();
347
348 if (!udev) {
349 fprintf(stderr, "Failed to initialize udev\n");
350 return NULL;
351 }
352
353 li = libinput_udev_create_context(&interface, grab, udev);
354 if (!li) {
355 fprintf(stderr, "Failed to initialize context from udev\n");
356 goto out;
357 }
358
359 libinput_log_set_handler(li, log_handler);
360 if (verbose)
361 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
362
363 if (libinput_udev_assign_seat(li, seat)) {
364 fprintf(stderr, "Failed to set seat\n");
365 libinput_unref(li);
366 li = NULL;
367 goto out;
368 }
369
370 out:
371 udev_unref(udev);
372 return li;
373 }
374
375 static struct libinput *
376 tools_open_device(const char **paths, bool verbose, bool *grab)
377 {
378 struct libinput_device *device;
379 struct libinput *li;
380 const char **p = paths;
381
382 li = libinput_path_create_context(&interface, grab);
383 if (!li) {
384 fprintf(stderr, "Failed to initialize path context\n");
385 return NULL;
386 }
387
388 if (verbose) {
389 libinput_log_set_handler(li, log_handler);
390 libinput_log_set_priority(li, LIBINPUT_LOG_PRIORITY_DEBUG);
391 }
392
393 while (*p) {
394 device = libinput_path_add_device(li, *p);
395 if (!device) {
396 fprintf(stderr, "Failed to initialize device %s\n", *p);
397 libinput_unref(li);
398 li = NULL;
399 break;
400 }
401 p++;
402 }
403
404 return li;
405 }
406
407 struct libinput *
408 tools_open_backend(enum tools_backend which,
409 const char **seat_or_device,
410 bool verbose,
411 bool *grab)
412 {
413 struct libinput *li;
414
415 switch (which) {
416 case BACKEND_UDEV:
417 li = tools_open_udev(seat_or_device[0], verbose, grab);
418 break;
419 case BACKEND_DEVICE:
420 li = tools_open_device(seat_or_device, verbose, grab);
421 break;
422 default:
423 abort();
424 }
425
426 return li;
427 }
428
429 void
430 tools_device_apply_config(struct libinput_device *device,
431 struct tools_options *options)
432 {
433 const char *name = libinput_device_get_name(device);
434
435 if (libinput_device_config_send_events_get_modes(device) &
436 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED &&
437 fnmatch(options->disable_pattern, name, 0) != FNM_NOMATCH) {
438 libinput_device_config_send_events_set_mode(device,
439 LIBINPUT_CONFIG_SEND_EVENTS_DISABLED);
440 }
441
442 if (strlen(options->match) > 0 &&
443 fnmatch(options->match, name, 0) == FNM_NOMATCH)
444 return;
445
446 if (options->tapping != -1)
447 libinput_device_config_tap_set_enabled(device, options->tapping);
448 if (options->tap_map != (enum libinput_config_tap_button_map)-1)
449 libinput_device_config_tap_set_button_map(device,
450 options->tap_map);
451 if (options->drag != -1)
452 libinput_device_config_tap_set_drag_enabled(device,
453 options->drag);
454 if (options->drag_lock != -1)
455 libinput_device_config_tap_set_drag_lock_enabled(device,
456 options->drag_lock);
457 if (options->natural_scroll != -1)
458 libinput_device_config_scroll_set_natural_scroll_enabled(device,
459 options->natural_scroll);
460 if (options->left_handed != -1)
461 libinput_device_config_left_handed_set(device, options->left_handed);
462 if (options->middlebutton != -1)
463 libinput_device_config_middle_emulation_set_enabled(device,
464 options->middlebutton);
465
466 if (options->dwt != -1)
467 libinput_device_config_dwt_set_enabled(device, options->dwt);
468
469 #ifdef HAVE_LIBINPUT_1_21
470 if (options->dwtp != -1)
471 libinput_device_config_dwtp_set_enabled(device, options->dwtp);
472 #endif
473
474 if (options->click_method != (enum libinput_config_click_method)-1)
475 libinput_device_config_click_set_method(device, options->click_method);
476
477 #ifdef HAVE_LIBINPUT_1_26
478 if (options->clickfinger_map != (enum libinput_config_clickfinger_button_map)-1)
479 libinput_device_config_click_set_clickfinger_button_map(device,
480 options->clickfinger_map);
481 #endif
482
483 if (options->scroll_method != (enum libinput_config_scroll_method)-1)
484 libinput_device_config_scroll_set_method(device,
485 options->scroll_method);
486 if (options->scroll_button != -1)
487 libinput_device_config_scroll_set_button(device,
488 options->scroll_button);
489 #ifdef HAVE_LIBINPUT_1_15
490 if (options->scroll_button_lock != -1)
491 libinput_device_config_scroll_set_button_lock(device,
492 options->scroll_button_lock);
493 #endif
494
495
496 if (libinput_device_config_accel_is_available(device)) {
497 libinput_device_config_accel_set_speed(device,
498 options->speed);
499 if (options->profile != LIBINPUT_CONFIG_ACCEL_PROFILE_NONE)
500 libinput_device_config_accel_set_profile(device,
501 options->profile);
502 }
503 #ifdef HAVE_LIBINPUT_1_23
504 if (options->profile == LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM) {
505 struct libinput_config_accel *config =
506 libinput_config_accel_create(LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM);
507 libinput_config_accel_set_points(config,
508 options->custom_type,
509 options->custom_step,
510 options->custom_npoints,
511 options->custom_points);
512 libinput_device_config_accel_apply(device, config);
513 libinput_config_accel_destroy(config);
514 }
515 #endif
516
517 if (options->angle != 0)
518 libinput_device_config_rotation_set_angle(device, options->angle % 360);
519 }
520
521 static char*
522 find_device(const char *udev_tag)
523 {
524 struct udev *udev;
525 struct udev_enumerate *e = NULL;
526 struct udev_list_entry *entry = NULL;
527 struct udev_device *device;
528 const char *path, *sysname;
529 char *device_node = NULL;
530
531 udev = udev_new();
532 if (!udev)
533 goto out;
534
535 e = udev_enumerate_new(udev);
536 udev_enumerate_add_match_subsystem(e, "input");
537 udev_enumerate_scan_devices(e);
538
539 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
540 path = udev_list_entry_get_name(entry);
541 device = udev_device_new_from_syspath(udev, path);
542 if (!device)
543 continue;
544
545 sysname = udev_device_get_sysname(device);
546 if (!strneq("event", sysname, 5)) {
547 udev_device_unref(device);
548 continue;
549 }
550
551 if (udev_device_get_property_value(device, udev_tag))
552 device_node = safe_strdup(udev_device_get_devnode(device));
553
554 udev_device_unref(device);
555
556 if (device_node)
557 break;
558 }
559 out:
560 udev_enumerate_unref(e);
561 udev_unref(udev);
562
563 return device_node;
564 }
565
566 bool
567 find_touchpad_device(char *path, size_t path_len)
568 {
569 char *devnode = find_device("ID_INPUT_TOUCHPAD");
570
571 if (devnode) {
572 snprintf(path, path_len, "%s", devnode);
573 free(devnode);
574 }
575
576 return devnode != NULL;
577 }
578
579 bool
580 is_touchpad_device(const char *devnode)
581 {
582 struct udev *udev;
583 struct udev_device *dev = NULL;
584 struct stat st;
585 bool is_touchpad = false;
586
587 if (stat(devnode, &st) < 0)
588 return false;
589
590 udev = udev_new();
591 if (!udev)
592 goto out;
593
594 dev = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
595 if (!dev)
596 goto out;
597
598 is_touchpad = udev_device_get_property_value(dev, "ID_INPUT_TOUCHPAD");
599 out:
600 if (dev)
601 udev_device_unref(dev);
602 udev_unref(udev);
603
604 return is_touchpad;
605 }