1 /*
2 * Copyright © 2019 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "config.h"
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <inttypes.h>
30 #include <getopt.h>
31 #include <poll.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <signal.h>
35 #include <sys/ioctl.h>
36 #include <unistd.h>
37 #include <libinput.h>
38 #include <libevdev/libevdev.h>
39
40 #include "shared.h"
41 #include "util-macros.h"
42 #include "util-input-event.h"
43
44 static volatile sig_atomic_t stop = 0;
45 static struct tools_options options;
46 static int termwidth = 78;
47
48 struct context {
49 struct libinput *libinput;
50 struct libinput_device *device;
51 struct libinput_tablet_tool *tool;
52 struct libevdev *evdev;
53
54 /* fd[0] ... libinput fd
55 fd[1] ... libevdev fd */
56 struct pollfd fds[2];
57
58 /* libinput device state */
59 bool tip_is_down;
60 double x, y;
61 double x_norm, y_norm;
62 double tx, ty;
63 double dist, pressure;
64 double rotation, slider;
65 unsigned int buttons_down[32];
66 unsigned int evdev_buttons_down[32];
67
68 /* libevdev device state */
69 struct {
70 int x, y, z;
71 int tilt_x, tilt_y;
72 int distance, pressure;
73 } abs;
74 };
75
76 LIBINPUT_ATTRIBUTE_PRINTF(1, 2)
77 static void
print_line(const char * format,...)78 print_line(const char *format, ...)
79 {
80 char empty[] = " ";
81 const int width = 80;
82 int n;
83 va_list args;
84
85 printf("\r");
86
87 va_start(args, format);
88 n = vprintf(format, args);
89 va_end(args);
90 printf("%.*s\n", width - n, empty);
91 }
92
93 static void
print_buttons(struct context * ctx,unsigned int * buttons,size_t sz)94 print_buttons(struct context *ctx, unsigned int *buttons, size_t sz)
95 {
96 char buf[256] = {0};
97 size_t len = 0;
98
99 for (size_t i = 0; i < sz; i++) {
100 const char *name;
101
102 if (buttons[i] == 0)
103 continue;
104
105 name = libevdev_event_code_get_name(EV_KEY, buttons[i]);
106 len += snprintf(&buf[len], sizeof(buf) - len, "%s ", name);
107 }
108 print_line(" buttons: %s", buf);
109 }
110
111 static void
print_bar(const char * header,double value,double normalized)112 print_bar(const char *header, double value, double normalized)
113 {
114 char empty[termwidth];
115 bool oob = false;
116 /* the bar is minimum 10 chars, otherwise 78 or whatever fits.
117 32 is the manually-added up length of the prefix + [|] */
118 const int width = max(10, min(78, termwidth - 32));
119 int left_pad, right_pad;
120
121 memset(empty, '-', sizeof empty);
122
123 if (normalized < 0.0 || normalized > 1.0) {
124 normalized = min(max(normalized, 0.0), 1.0);
125 oob = true;
126 }
127
128 left_pad = width * normalized + 0.5;
129 right_pad = width - left_pad;
130
131 printf("\r %s%-16s %8.2f [%.*s|%.*s]%s\n",
132 oob ? ANSI_RED : "",
133 header,
134 value,
135 left_pad, empty,
136 right_pad, empty,
137 oob ? ANSI_NORMAL : "");
138 }
139
140 static double
normalize(struct libevdev * evdev,int code,int value)141 normalize(struct libevdev *evdev, int code, int value)
142 {
143 const struct input_absinfo *abs;
144
145 if (!evdev)
146 return 0.0;
147
148 abs = libevdev_get_abs_info(evdev, code);
149
150 if (!abs)
151 return 0.0;
152
153 return 1.0 * (value - abs->minimum)/absinfo_range(abs);
154 }
155
156 static int
print_state(struct context * ctx)157 print_state(struct context *ctx)
158 {
159 const char *tool_str;
160 double w, h;
161 int lines_printed = 0;
162
163 if (!ctx->device) {
164 print_line(ANSI_RED "No device connected" ANSI_NORMAL);
165 lines_printed++;
166 } else {
167 libinput_device_get_size(ctx->device, &w, &h);
168 print_line("Device: %s (%s)",
169 libinput_device_get_name(ctx->device),
170 libinput_device_get_sysname(ctx->device));
171 lines_printed++;
172 }
173
174 if (!ctx->tool) {
175 print_line(ANSI_RED "No tool in proximity " ANSI_NORMAL);
176 lines_printed++;
177 } else {
178 switch (libinput_tablet_tool_get_type(ctx->tool)) {
179 case LIBINPUT_TABLET_TOOL_TYPE_PEN:
180 tool_str = "pen";
181 break;
182 case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
183 tool_str = "eraser";
184 break;
185 case LIBINPUT_TABLET_TOOL_TYPE_BRUSH:
186 tool_str = "brush";
187 break;
188 case LIBINPUT_TABLET_TOOL_TYPE_PENCIL:
189 tool_str = "pencil";
190 break;
191 case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH:
192 tool_str = "airbrush";
193 break;
194 case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
195 tool_str = "mouse";
196 break;
197 case LIBINPUT_TABLET_TOOL_TYPE_LENS:
198 tool_str = "lens";
199 break;
200 case LIBINPUT_TABLET_TOOL_TYPE_TOTEM:
201 tool_str = "totem";
202 break;
203 default:
204 abort();
205 }
206
207 printf("\rTool: %s serial %#" PRIx64 ", id %#" PRIx64 "\n",
208 tool_str,
209 libinput_tablet_tool_get_serial(ctx->tool),
210 libinput_tablet_tool_get_tool_id(ctx->tool));
211 lines_printed++;
212 }
213 printf("libinput:\n");
214 print_line("tip: %s", ctx->tip_is_down ? "down" : "up");
215 print_bar("x:", ctx->x, ctx->x_norm);
216 print_bar("y:", ctx->y, ctx->y_norm);
217 print_bar("tilt x:", ctx->tx, (ctx->tx + 90)/180);
218 print_bar("tilt y:", ctx->ty, (ctx->ty + 90)/180);
219 print_bar("dist:", ctx->dist, ctx->dist);
220 print_bar("pressure:", ctx->pressure, ctx->pressure);
221 print_bar("rotation:", ctx->rotation, ctx->rotation/360.0);
222 print_bar("slider:", ctx->slider, (ctx->slider + 1.0)/2.0);
223 print_buttons(ctx,
224 ctx->buttons_down,
225 ARRAY_LENGTH(ctx->buttons_down));
226 lines_printed += 11;
227
228 printf("evdev:\n");
229 print_bar("ABS_X:", ctx->abs.x, normalize(ctx->evdev, ABS_X, ctx->abs.x));
230 print_bar("ABS_Y:", ctx->abs.y, normalize(ctx->evdev, ABS_Y, ctx->abs.y));
231 print_bar("ABS_Z:", ctx->abs.z, normalize(ctx->evdev, ABS_Z, ctx->abs.z));
232 print_bar("ABS_TILT_X:", ctx->abs.tilt_x, normalize(ctx->evdev, ABS_TILT_X, ctx->abs.tilt_x));
233 print_bar("ABS_TILT_Y:", ctx->abs.tilt_y, normalize(ctx->evdev, ABS_TILT_Y, ctx->abs.tilt_y));
234 print_bar("ABS_DISTANCE:", ctx->abs.distance, normalize(ctx->evdev, ABS_DISTANCE, ctx->abs.distance));
235 print_bar("ABS_PRESSURE:", ctx->abs.pressure, normalize(ctx->evdev, ABS_PRESSURE, ctx->abs.pressure));
236 print_buttons(ctx,
237 ctx->evdev_buttons_down,
238 ARRAY_LENGTH(ctx->evdev_buttons_down));
239 lines_printed += 9;
240
241 return lines_printed;
242 }
243
244 static void
handle_device_added(struct context * ctx,struct libinput_event * ev)245 handle_device_added(struct context *ctx, struct libinput_event *ev)
246 {
247 struct libinput_device *device = libinput_event_get_device(ev);
248 struct udev_device *udev_device;
249 const char *devnode;
250
251 if (ctx->device)
252 return;
253
254 if (!libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL))
255 return;
256
257 ctx->device = libinput_device_ref(device);
258
259 udev_device = libinput_device_get_udev_device(device);
260 if (!udev_device)
261 return;
262
263 devnode = udev_device_get_devnode(udev_device);
264 if (devnode) {
265 int fd = open(devnode, O_RDONLY|O_NONBLOCK);
266 assert(fd != -1);
267 assert(libevdev_new_from_fd(fd, &ctx->evdev) == 0);
268 }
269
270 udev_device_unref(udev_device);
271 }
272
273 static void
handle_device_removed(struct context * ctx,struct libinput_event * ev)274 handle_device_removed(struct context *ctx, struct libinput_event *ev)
275 {
276 struct libinput_device *device = libinput_event_get_device(ev);
277
278 if (ctx->device != device)
279 return;
280
281 libinput_device_unref(ctx->device);
282 ctx->device = NULL;
283
284 libevdev_free(ctx->evdev);
285 ctx->evdev = NULL;
286
287 close(ctx->fds[1].fd);
288 ctx->fds[1].fd = -1;
289 }
290
291 static void
update_tablet_axes(struct context * ctx,struct libinput_event_tablet_tool * t)292 update_tablet_axes(struct context *ctx, struct libinput_event_tablet_tool *t)
293 {
294 ctx->x = libinput_event_tablet_tool_get_x(t);
295 ctx->y = libinput_event_tablet_tool_get_y(t);
296 ctx->x_norm = libinput_event_tablet_tool_get_x_transformed(t, 1.0);
297 ctx->y_norm = libinput_event_tablet_tool_get_y_transformed(t, 1.0);
298 ctx->tx = libinput_event_tablet_tool_get_tilt_x(t);
299 ctx->ty = libinput_event_tablet_tool_get_tilt_y(t);
300 ctx->dist = libinput_event_tablet_tool_get_distance(t);
301 ctx->pressure = libinput_event_tablet_tool_get_pressure(t);
302 ctx->rotation = libinput_event_tablet_tool_get_rotation(t);
303 ctx->slider = libinput_event_tablet_tool_get_slider_position(t);
304 }
305
306 static void
handle_tablet_button_event(struct context * ctx,struct libinput_event * ev)307 handle_tablet_button_event(struct context *ctx, struct libinput_event *ev)
308 {
309 struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev);
310 unsigned int button = libinput_event_tablet_tool_get_button(t);
311 enum libinput_button_state state = libinput_event_tablet_tool_get_button_state(t);
312
313 ARRAY_FOR_EACH(ctx->buttons_down, btn) {
314 if (state == LIBINPUT_BUTTON_STATE_PRESSED) {
315 if (*btn == 0) {
316 *btn = button;
317 break;
318 }
319 } else {
320 if (*btn == button) {
321 *btn = 0;
322 break;
323 }
324 }
325 }
326 }
327
328 static void
handle_tablet_axis_event(struct context * ctx,struct libinput_event * ev)329 handle_tablet_axis_event(struct context *ctx, struct libinput_event *ev)
330 {
331 struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev);
332
333 update_tablet_axes(ctx, t);
334 }
335
336 static void
handle_tablet_proximity_event(struct context * ctx,struct libinput_event * ev)337 handle_tablet_proximity_event(struct context *ctx, struct libinput_event *ev)
338 {
339 struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev);
340 struct libinput_tablet_tool *tool = libinput_event_tablet_tool_get_tool(t);
341
342 if (ctx->tool) {
343 libinput_tablet_tool_unref(ctx->tool);
344 ctx->tool = NULL;
345 }
346
347 if (libinput_event_tablet_tool_get_proximity_state(t) == LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN)
348 ctx->tool = libinput_tablet_tool_ref(tool);
349 }
350
351 static void
handle_tablet_tip_event(struct context * ctx,struct libinput_event * ev)352 handle_tablet_tip_event(struct context *ctx, struct libinput_event *ev)
353 {
354 struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev);
355
356 ctx->tip_is_down = libinput_event_tablet_tool_get_tip_state(t) == LIBINPUT_TABLET_TOOL_TIP_DOWN;
357 }
358
359 static void
handle_libinput_events(struct context * ctx)360 handle_libinput_events(struct context *ctx)
361 {
362 struct libinput *li = ctx->libinput;
363 struct libinput_event *ev;
364
365 libinput_dispatch(li);
366 while ((ev = libinput_get_event(li))) {
367 switch (libinput_event_get_type(ev)) {
368 case LIBINPUT_EVENT_NONE:
369 abort();
370 case LIBINPUT_EVENT_DEVICE_ADDED:
371 handle_device_added(ctx, ev);
372 tools_device_apply_config(libinput_event_get_device(ev),
373 &options);
374 break;
375 case LIBINPUT_EVENT_DEVICE_REMOVED:
376 handle_device_removed(ctx, ev);
377 break;
378 case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
379 handle_tablet_button_event(ctx, ev);
380 break;
381 case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
382 handle_tablet_axis_event(ctx, ev);
383 break;
384 case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
385 handle_tablet_proximity_event(ctx, ev);
386 break;
387 case LIBINPUT_EVENT_TABLET_TOOL_TIP:
388 handle_tablet_tip_event(ctx, ev);
389 break;
390 default:
391 break;
392 }
393
394 libinput_event_destroy(ev);
395 }
396 }
397
398 static void
handle_libevdev_events(struct context * ctx)399 handle_libevdev_events(struct context *ctx)
400 {
401 struct libevdev *evdev = ctx->evdev;
402 struct input_event event;
403
404 #define evbit(_t, _c) (((_t) << 16) | (_c))
405
406 if (!evdev)
407 return;
408
409 while (libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event)
410 == LIBEVDEV_READ_STATUS_SUCCESS)
411 {
412 switch(evbit(event.type, event.code)) {
413 case evbit(EV_KEY, BTN_TOOL_PEN):
414 case evbit(EV_KEY, BTN_TOOL_RUBBER):
415 case evbit(EV_KEY, BTN_TOOL_BRUSH):
416 case evbit(EV_KEY, BTN_TOOL_PENCIL):
417 case evbit(EV_KEY, BTN_TOOL_AIRBRUSH):
418 case evbit(EV_KEY, BTN_TOOL_MOUSE):
419 case evbit(EV_KEY, BTN_TOOL_LENS):
420 ctx->evdev_buttons_down[event.code - BTN_TOOL_PEN] = event.value ? event.code : 0;
421 break;
422 /* above tools should be mutually exclusive but let's leave
423 * enough space */
424 case evbit(EV_KEY, BTN_TOUCH):
425 ctx->evdev_buttons_down[7] = event.value ? event.code : 0;
426 break;
427 case evbit(EV_KEY, BTN_STYLUS):
428 ctx->evdev_buttons_down[8] = event.value ? event.code : 0;
429 break;
430 case evbit(EV_KEY, BTN_STYLUS2):
431 ctx->evdev_buttons_down[9] = event.value ? event.code : 0;
432 break;
433 case evbit(EV_KEY, BTN_STYLUS3):
434 ctx->evdev_buttons_down[10] = event.value ? event.code : 0;
435 break;
436 case evbit(EV_ABS, ABS_X):
437 ctx->abs.x = event.value;
438 break;
439 case evbit(EV_ABS, ABS_Y):
440 ctx->abs.y = event.value;
441 break;
442 case evbit(EV_ABS, ABS_Z):
443 ctx->abs.z = event.value;
444 break;
445 case evbit(EV_ABS, ABS_PRESSURE):
446 ctx->abs.pressure = event.value;
447 break;
448 case evbit(EV_ABS, ABS_TILT_X):
449 ctx->abs.tilt_x = event.value;
450 break;
451 case evbit(EV_ABS, ABS_TILT_Y):
452 ctx->abs.tilt_y = event.value;
453 break;
454 case evbit(EV_ABS, ABS_DISTANCE):
455 ctx->abs.distance = event.value;
456 break;
457 }
458 }
459 }
460
461 static void
sighandler(int signal,siginfo_t * siginfo,void * userdata)462 sighandler(int signal, siginfo_t *siginfo, void *userdata)
463 {
464 stop = 1;
465 }
466
467 static void
mainloop(struct context * ctx)468 mainloop(struct context *ctx)
469 {
470 unsigned int lines_printed = 20;
471
472 ctx->fds[0].fd = libinput_get_fd(ctx->libinput);
473
474 /* pre-load the lines */
475 for (unsigned int i = 0; i < lines_printed; i++)
476 printf("\n");
477
478 do {
479 handle_libinput_events(ctx);
480 handle_libevdev_events(ctx);
481
482 printf(ANSI_LEFT, 1000);
483 printf(ANSI_UP, lines_printed);
484 lines_printed = print_state(ctx);
485 } while (!stop && poll(ctx->fds, 2, -1) > -1);
486
487 printf("\n");
488 }
489
490 static void
usage(void)491 usage(void) {
492 printf("Usage: libinput debug-tablet [options] [--udev <seat>|--device /dev/input/event0]\n");
493 }
494
495 static void
init_context(struct context * ctx)496 init_context(struct context *ctx)
497 {
498
499 memset(ctx, 0, sizeof *ctx);
500
501 ctx->fds[0].fd = -1; /* libinput fd */
502 ctx->fds[0].events = POLLIN;
503 ctx->fds[0].revents = 0;
504 ctx->fds[1].fd = -1; /* libevdev fd */
505 ctx->fds[1].events = POLLIN;
506 ctx->fds[1].revents = 0;
507 }
508
509 int
main(int argc,char ** argv)510 main(int argc, char **argv)
511 {
512 struct context ctx;
513 struct libinput *li;
514 enum tools_backend backend = BACKEND_NONE;
515 const char *seat_or_device[2] = {"seat0", NULL};
516 struct sigaction act;
517 bool grab = false;
518
519 init_context(&ctx);
520
521 tools_init_options(&options);
522
523 while (1) {
524 int c;
525 int option_index = 0;
526 enum {
527 OPT_DEVICE = 1,
528 OPT_UDEV,
529 };
530 static struct option opts[] = {
531 CONFIGURATION_OPTIONS,
532 { "help", no_argument, 0, 'h' },
533 { "device", required_argument, 0, OPT_DEVICE },
534 { "udev", required_argument, 0, OPT_UDEV },
535 { 0, 0, 0, 0}
536 };
537
538 c = getopt_long(argc, argv, "h", opts, &option_index);
539 if (c == -1)
540 break;
541
542 switch(c) {
543 case '?':
544 exit(EXIT_INVALID_USAGE);
545 break;
546 case 'h':
547 usage();
548 exit(EXIT_SUCCESS);
549 break;
550 case OPT_DEVICE:
551 backend = BACKEND_DEVICE;
552 seat_or_device[0] = optarg;
553 break;
554 case OPT_UDEV:
555 backend = BACKEND_UDEV;
556 seat_or_device[0] = optarg;
557 break;
558 }
559
560 }
561
562 if (optind < argc) {
563 if (optind < argc - 1 || backend != BACKEND_NONE) {
564 usage();
565 return EXIT_INVALID_USAGE;
566 }
567 backend = BACKEND_DEVICE;
568 seat_or_device[0] = argv[optind];
569 } else if (backend == BACKEND_NONE) {
570 backend = BACKEND_UDEV;
571 }
572
573 memset(&act, 0, sizeof(act));
574 act.sa_sigaction = sighandler;
575 act.sa_flags = SA_SIGINFO;
576
577 if (sigaction(SIGINT, &act, NULL) == -1) {
578 fprintf(stderr, "Failed to set up signal handling (%s)\n",
579 strerror(errno));
580 return EXIT_FAILURE;
581 }
582
583 li = tools_open_backend(backend, seat_or_device, false, &grab);
584 if (!li)
585 return EXIT_FAILURE;
586
587 struct winsize w;
588 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
589 termwidth = w.ws_col;
590
591 ctx.libinput = li;
592 mainloop(&ctx);
593
594 libinput_unref(li);
595
596 return EXIT_SUCCESS;
597 }
598