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