Debian Codegraph @

src/input.c

raw symbols SCIP
1 /* Adapted from test/event-debug.c from the libinput distribution */
2 /* test/event-debug.c 1.3.3:
3 * Copyright © 2014 Red Hat, Inc.
4 * Modifications: Copyright © 2016 Bill Allombert <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #define _GNU_SOURCE
28 #include <errno.h>
29 #include <inttypes.h>
30 #include <fcntl.h>
31 #include <getopt.h>
32 #include <poll.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <linux/input.h>
38 #include <sys/ioctl.h>
39
40 #include <libinput.h>
41 #include <libevdev/libevdev.h>
42 #include "shared.h"
43 #include "consolation.h"
44
45
46 int nodaemon = false;
47 unsigned int screen_width;
48 unsigned int screen_height;
49 int do_mouse_reporting = true;
50 enum mouse_reporting_mode mouse_reporting = MOUSE_REPORTING_OFF;
51
52 static struct tools_options options;
53 static volatile unsigned int stop = 0;
54 static enum tools_backend backend = BACKEND_NONE;
55 #define MAX_DEVICES 64
56 static const char *seat_or_devices[MAX_DEVICES+1] = {"seat0"};
57 static size_t ndevices = 0;
58 static bool grab = false;
59 static bool verbose = false;
60 static const char *word_chars = NULL;
61 static int action_map[256];
62
63 static void
64 handle_motion_event(struct libinput_event *ev)
65 {
66 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
67 double x = libinput_event_pointer_get_dx(p);
68 double y = libinput_event_pointer_get_dy(p);
69 move_pointer(x, y);
70 }
71
72 static void
73 handle_absmotion_event(struct libinput_event *ev)
74 {
75 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
76 double x = libinput_event_pointer_get_absolute_x_transformed(p, screen_width);
77 double y = libinput_event_pointer_get_absolute_y_transformed(p, screen_height);
78 set_pointer(x, y);
79 }
80
81 static void
82 handle_pointer_button_event(struct libinput_event *ev)
83 {
84 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
85 enum libinput_button_state state;
86 int button;
87 button = libinput_event_pointer_get_button(p);
88 if (button >= 0x100 && button < 0x200)
89 button = action_map[button - 0x100] + 0x100;
90 state = libinput_event_pointer_get_button_state(p);
91 switch(button)
92 {
93 case BTN_LEFT:
94 if (state==LIBINPUT_BUTTON_STATE_PRESSED)
95 press_left_button();
96 else
97 release_left_button();
98 break;
99 case BTN_MIDDLE:
100 if (state==LIBINPUT_BUTTON_STATE_PRESSED)
101 press_middle_button();
102 else
103 release_middle_button();
104 break;
105 case BTN_RIGHT:
106 if (state==LIBINPUT_BUTTON_STATE_PRESSED)
107 press_right_button();
108 else
109 release_right_button();
110 break;
111 }
112 }
113
114 static void
115 handle_pointer_axis_event(struct libinput_event *ev)
116 {
117 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
118 double v = 0;
119 if (libinput_event_pointer_has_axis(p,
120 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
121 {
122 v = libinput_event_pointer_get_axis_value(p,
123 LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
124 vertical_axis(v);
125 }
126 }
127
128 static void
129 handle_touch_event_down(struct libinput_event *ev)
130 {
131 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
132 double x = libinput_event_touch_get_x_transformed(t, screen_width);
133 double y = libinput_event_touch_get_y_transformed(t, screen_height);
134 set_pointer(x, y);
135 press_left_button();
136 }
137
138 static void
139 handle_touch_event_motion(struct libinput_event *ev)
140 {
141 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
142 double x = libinput_event_touch_get_x_transformed(t, screen_width);
143 double y = libinput_event_touch_get_y_transformed(t, screen_height);
144 set_pointer(x, y);
145 }
146
147 static void
148 handle_touch_event_up(struct libinput_event *ev)
149 {
150 release_left_button();
151 }
152
153 static int
154 handle_events(struct libinput *li)
155 {
156 int rc = -1;
157 struct libinput_event *ev;
158
159 libinput_dispatch(li);
160 set_screen_size_and_mouse_reporting();
161 while ((ev = libinput_get_event(li))) {
162
163 switch (libinput_event_get_type(ev)) {
164 case LIBINPUT_EVENT_NONE:
165 abort();
166 case LIBINPUT_EVENT_DEVICE_ADDED:
167 case LIBINPUT_EVENT_DEVICE_REMOVED:
168 tools_device_apply_config(libinput_event_get_device(ev),
169 &options);
170 break;
171 case LIBINPUT_EVENT_POINTER_MOTION:
172 handle_motion_event(ev);
173 break;
174 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
175 handle_absmotion_event(ev);
176 break;
177 case LIBINPUT_EVENT_POINTER_BUTTON:
178 handle_pointer_button_event(ev);
179 break;
180 case LIBINPUT_EVENT_POINTER_AXIS:
181 handle_pointer_axis_event(ev);
182 break;
183 case LIBINPUT_EVENT_TOUCH_DOWN:
184 handle_touch_event_down(ev);
185 break;
186 case LIBINPUT_EVENT_TOUCH_MOTION:
187 handle_touch_event_motion(ev);
188 break;
189 case LIBINPUT_EVENT_TOUCH_UP:
190 handle_touch_event_up(ev);
191 break;
192 default:
193 break;
194 }
195 libinput_event_destroy(ev);
196 libinput_dispatch(li);
197 rc = 0;
198 }
199 return rc;
200 }
201
202 static void
203 sighandler(int signal, siginfo_t *siginfo, void *userdata)
204 {
205 stop = 1;
206 }
207
208 static void
209 mainloop(struct libinput *li)
210 {
211 struct pollfd fds;
212
213 fds.fd = libinput_get_fd(li);
214 fds.events = POLLIN;
215 fds.revents = 0;
216
217 /* Handle already-pending device added events */
218 if (handle_events(li))
219 fprintf(stderr, "Expected device added events on startup but got none. "
220 "Maybe you don't have the right permissions?\n");
221
222 while (!stop && poll(&fds, 1, -1) > -1)
223 handle_events(li);
224 }
225
226 int
227 usage(void)
228 {
229 printf("Usage: %s [options] [--udev [<seat>]|--device /dev/input/event0]\n"
230 "--udev <seat>.... Use udev device discovery (default).\n"
231 " Specifying a seat ID is optional.\n"
232 "--device /path/to/device .... open the given device(s) only\n"
233 "--apply-to=\"pattern\".... Configuration options are only applied where the device name matches the pattern\n"
234 "--disable-sendevents=\"pattern\".... Set the send-events option to disabled for the devices matching patterns\n"
235 "\n"
236 "Features:\n"
237 "--enable-tap\n"
238 "--disable-tap.... enable/disable tapping\n"
239 "--enable-drag\n"
240 "--disable-drag.... enable/disable tap-n-drag\n"
241 "--enable-drag-lock\n"
242 "--disable-drag-lock.... enable/disable tapping drag lock\n"
243 "--enable-natural-scrolling\n"
244 "--disable-natural-scrolling.... enable/disable natural scrolling\n"
245 "--enable-left-handed\n"
246 "--disable-left-handed.... enable/disable left-handed button configuration\n"
247 "--enable-middlebutton\n"
248 "--disable-middlebutton.... enable/disable middle button emulation\n"
249 "--enable-dwt\n"
250 "--disable-dwt.... enable/disable disable-while-typing\n"
251 #ifdef HAVE_LIBINPUT_1_21
252 "--enable-dwtp\n"
253 "--disable-dwtp.... enable/disable disable-while-trackpointing\n"
254 #endif
255 #ifdef HAVE_LIBINPUT_1_15
256 "--enable-scroll-button-lock\n"
257 "--disable-scroll-button-lock.... enable/disable the scroll button lock\n"
258 #endif
259 "--set-click-method=[none|clickfinger|buttonareas] .... set the desired click method\n"
260 #ifdef HAVE_LIBINPUT_1_26
261 "--set-clickfinger-map=[lrm|lmr].... set button mapping for clickfinger\n"
262 #endif
263 "--set-scroll-method=[none|twofinger|edge|button] ... set the desired scroll method\n"
264 "--set-scroll-button=BTN_MIDDLE ... set the button to the given button code\n"
265 "--set-speed=<value>.... set pointer acceleration speed (allowed range [-1, 1]) \n"
266 "--set-tap-map=[lrm|lmr] ... set button mapping for tapping\n"
267 "--set-rotation-angle=<degrees>.... set the rotation angle in degrees (0 to 360)\n"
268 #ifdef HAVE_LIBINPUT_1_23
269 "--set-profile=[adaptive|flat|custom].... set pointer acceleration profile\n"
270 "If profile is custom:\n"
271 " --set-custom-points=0.0;1.0 set the n points defining a custom acceleration function\n"
272 " --set-custom-step=1.0 sets the distance along the x-axis between each point, starting from 0\n"
273 " --set-custom-type=[fallback|motion|scroll] set the type of the custom acceleration function\n"
274 #else
275 "--set-profile=[adaptive|flat].... set pointer acceleration profile\n"
276 #endif
277 "\n"
278 "These options apply to all applicable devices, if a feature\n"
279 "is not explicitly specified it is left at each device's default.\n"
280 "\n"
281 "Other options:\n"
282 "--word-chars=<string>.... list of characters that make up words.\n"
283 " Ranges (a-z, A-Z, 0-9 etc.) are allowed.\n"
284 "--grab .......... exclusively grab all opened devices.\n"
285 "--map button=action .... set button to action.\n"
286 "--no-daemon ..... do not detach and run in the background.\n"
287 "--disable-mouse-reporting .... disable mouse reporting.\n"
288 "--verbose ....... print debugging output.\n"
289 "--version ....... print version information.\n"
290 "--help .......... print this help.\n",
291 program_invocation_short_name);
292 return 1;
293 }
294
295 static void
296 version(void)
297 {
298 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
299 }
300
301 static void
302 init_map(void)
303 {
304 int i;
305 for (i=0; i<256; i++)
306 action_map[i] = i;
307 }
308
309 static int
310 decode_action(char *act)
311 {
312 if (!strcmp(act,"select") || !strcmp(act,"SELECT"))
313 return BTN_LEFT;
314 if (!strcmp(act,"paste") || !strcmp(act,"PASTE"))
315 return BTN_MIDDLE;
316 if (!strcmp(act,"endselect") || !strcmp(act,"ENDSELECT"))
317 return BTN_RIGHT;
318 return libevdev_event_code_from_name(EV_KEY, act);
319 }
320
321 static int
322 parse_args(int argc, char **argv)
323 {
324 tools_init_options(&options);
325 init_map();
326 while (1) {
327 int c;
328 int option_index = 0;
329 enum {
330 OPT_DEVICE = 1,
331 OPT_UDEV,
332 OPT_GRAB,
333 OPT_MAP,
334 OPT_NO_DAEMON,
335 OPT_HELP,
336 OPT_VERBOSE,
337 OPT_VERSION,
338 OPT_WORD_CHARS,
339 OPT_DISABLE_MOUSE_REPORTING
340 };
341 static struct option opts[] = {
342 CONFIGURATION_OPTIONS,
343 { "help", no_argument, 0, 'h' },
344 { "device", required_argument, 0, OPT_DEVICE },
345 { "udev", required_argument, 0, OPT_UDEV },
346 { "grab", no_argument, 0, OPT_GRAB },
347 { "map", required_argument, 0, OPT_MAP },
348 { "no-daemon", no_argument, 0, OPT_NO_DAEMON },
349 { "verbose", no_argument, 0, OPT_VERBOSE },
350 { "version", no_argument, 0, OPT_VERSION },
351 { "word-chars", required_argument, 0, OPT_WORD_CHARS },
352 { "disable-mouse-reporting", no_argument, 0, OPT_DISABLE_MOUSE_REPORTING },
353 { 0, 0, 0, 0}
354 };
355
356 c = getopt_long(argc, argv, "h", opts, &option_index);
357 if (c == -1)
358 break;
359
360 switch(c) {
361 case '?':
362 exit(1);
363 break;
364 case 'h':
365 usage();
366 exit(0);
367 break;
368 case OPT_VERSION:
369 version();
370 exit(0);
371 break;
372 case OPT_DEVICE:
373 if (backend == BACKEND_UDEV)
374 return usage();
375 backend = BACKEND_DEVICE;
376 if (ndevices >= MAX_DEVICES)
377 {
378 fprintf(stderr, "Too many --device options\n");
379 return 1;
380 }
381 seat_or_devices[ndevices++] = optarg;
382 break;
383 case OPT_UDEV:
384 if (backend == BACKEND_DEVICE)
385 return usage();
386 backend = BACKEND_UDEV;
387 seat_or_devices[0] = optarg;
388 ndevices = 1;
389 break;
390 case OPT_GRAB:
391 grab = true;
392 break;
393 case OPT_NO_DAEMON:
394 nodaemon = true;
395 break;
396 case OPT_VERBOSE:
397 verbose = true;
398 break;
399 case OPT_WORD_CHARS:
400 word_chars = optarg;
401 break;
402 case OPT_MAP:
403 {
404 int button, action;
405 char *btn = strdup(optarg), *act;
406 char *eq = strchr(btn,'=');
407 if (!eq)
408 return usage();
409 *eq = 0; act = eq+1;
410 button = libevdev_event_code_from_name(EV_KEY, btn);
411 action = decode_action(act);
412 free(btn);
413 if (button == -1 || action == -1)
414 return usage();
415 if (button >= 0x100 && button < 0x200)
416 action_map[button - 0x100] = action - 0x100;
417 }
418 break;
419 case OPT_DISABLE_MOUSE_REPORTING:
420 do_mouse_reporting = false;
421 break;
422 default:
423 if (tools_parse_option(c, optarg, &options) != 0) {
424 usage();
425 return 1;
426 }
427 break;
428 }
429 }
430 if (optind < argc) {
431 usage();
432 return 1;
433 }
434 if (backend == BACKEND_NONE)
435 {
436 backend = BACKEND_UDEV;
437 ndevices = 1;
438 }
439 seat_or_devices[ndevices] = NULL;
440 return 0;
441 }
442
443
444 int
445 event_init(int argc, char **argv)
446 {
447 tools_init_options(&options);
448 return parse_args(argc, argv);
449 }
450
451 int
452 event_main(void)
453 {
454 struct libinput *li;
455 struct sigaction act;
456
457 memset(&act, 0, sizeof(act));
458 act.sa_sigaction = sighandler;
459 act.sa_flags = SA_SIGINFO;
460
461 if (sigaction(SIGINT, &act, NULL) == -1) {
462 fprintf(stderr, "Failed to set up signal handling (%s)\n",
463 strerror(errno));
464 return 0;
465 }
466
467 set_lut(word_chars);
468 li = tools_open_backend(backend, seat_or_devices, verbose, &grab);
469 if (!li)
470 return 1;
471
472 mainloop(li);
473
474 libinput_unref(li);
475
476 return 0;
477 }