• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 LIBINPUT_ATTRIBUTE_PRINTF(1, 2)
76 static void
print_line(const char * format,...)77 print_line(const char *format, ...)
78 {
79 	char empty[] = "                                                                                ";
80 	const int width = 80;
81 	int n;
82 	va_list args;
83 
84 	printf("\r");
85 
86 	va_start(args, format);
87 	n = vprintf(format, args);
88 	va_end(args);
89 	printf("%.*s\n", width - n, empty);
90 }
91 
92 static void
print_buttons(struct context * ctx,unsigned int * buttons,size_t sz)93 print_buttons(struct context *ctx, unsigned int *buttons, size_t sz)
94 {
95 	char buf[256] = {0};
96 	size_t len = 0;
97 
98 	for (size_t i = 0; i < sz; i++) {
99 		const char *name;
100 
101 		if (buttons[i] == 0)
102 			continue;
103 
104 		name = libevdev_event_code_get_name(EV_KEY, buttons[i]);
105 		len += snprintf(&buf[len], sizeof(buf) - len, "%s ", name);
106 	}
107 	print_line("  buttons: %s", buf);
108 }
109 
110 static void
print_bar(const char * header,double value,double normalized)111 print_bar(const char *header, double value, double normalized)
112 {
113 	char empty[termwidth];
114 	bool oob = false;
115 	/* the bar is minimum 10 chars, otherwise 78 or whatever fits.
116 	   32 is the manually-added up length of the prefix + [|] */
117 	const int width = max(10, min(78, termwidth - 32));
118 	int left_pad, right_pad;
119 
120 	memset(empty, '-', sizeof empty);
121 
122 	if (normalized < 0.0 || normalized > 1.0) {
123 		normalized = min(max(normalized, 0.0), 1.0);
124 		oob = true;
125 	}
126 
127 	left_pad = width * normalized + 0.5;
128 	right_pad = width - left_pad;
129 
130 	printf("\r  %s%-16s %8.2f [%.*s|%.*s]%s\n",
131 	       oob ? ANSI_RED : "",
132 	       header,
133 	       value,
134 	       left_pad, empty,
135 	       right_pad, empty,
136 	       oob ? ANSI_NORMAL : "");
137 }
138 
139 static double
normalize(struct libevdev * evdev,int code,int value)140 normalize(struct libevdev *evdev, int code, int value)
141 {
142 	const struct input_absinfo *abs;
143 
144 	if (!evdev)
145 		return 0.0;
146 
147 	abs = libevdev_get_abs_info(evdev, code);
148 
149 	if (!abs)
150 		return 0.0;
151 
152 	return 1.0 * (value - abs->minimum)/(abs->maximum - abs->minimum + 1);
153 }
154 
155 static int
print_state(struct context * ctx)156 print_state(struct context *ctx)
157 {
158 	const char *tool_str;
159 	double w, h;
160 	int lines_printed = 0;
161 
162 	if (!ctx->device) {
163 		print_line(ANSI_RED "No device connected" ANSI_NORMAL);
164 		lines_printed++;
165 	} else {
166 		libinput_device_get_size(ctx->device, &w, &h);
167 		print_line("Device: %s (%s)",
168 			   libinput_device_get_name(ctx->device),
169 			   libinput_device_get_sysname(ctx->device));
170 		lines_printed++;
171 	}
172 
173 	if (!ctx->tool) {
174 		print_line(ANSI_RED "No tool in proximity " ANSI_NORMAL);
175 		lines_printed++;
176 	} else {
177 		switch (libinput_tablet_tool_get_type(ctx->tool)) {
178 		case LIBINPUT_TABLET_TOOL_TYPE_PEN:
179 			tool_str = "pen";
180 			break;
181 		case LIBINPUT_TABLET_TOOL_TYPE_ERASER:
182 			tool_str = "eraser";
183 			break;
184 		case LIBINPUT_TABLET_TOOL_TYPE_BRUSH:
185 			tool_str = "brush";
186 			break;
187 		case LIBINPUT_TABLET_TOOL_TYPE_PENCIL:
188 			tool_str = "pencil";
189 			break;
190 		case LIBINPUT_TABLET_TOOL_TYPE_AIRBRUSH:
191 			tool_str = "airbrush";
192 			break;
193 		case LIBINPUT_TABLET_TOOL_TYPE_MOUSE:
194 			tool_str = "mouse";
195 			break;
196 		case LIBINPUT_TABLET_TOOL_TYPE_LENS:
197 			tool_str = "lens";
198 			break;
199 		case LIBINPUT_TABLET_TOOL_TYPE_TOTEM:
200 			tool_str = "totem";
201 			break;
202 		default:
203 			abort();
204 		}
205 
206 		printf("\rTool: %s serial %#" PRIx64 ", id %#" PRIx64 "\n",
207 		       tool_str,
208 		       libinput_tablet_tool_get_serial(ctx->tool),
209 		       libinput_tablet_tool_get_tool_id(ctx->tool));
210 		lines_printed++;
211 	}
212 	printf("libinput:\n");
213 	print_line("tip: %s", ctx->tip_is_down ? "down" : "up");
214 	print_bar("x:", ctx->x, ctx->x_norm);
215 	print_bar("y:", ctx->y, ctx->y_norm);
216 	print_bar("tilt x:", ctx->tx, (ctx->tx + 90)/180);
217 	print_bar("tilt y:", ctx->ty, (ctx->ty + 90)/180);
218 	print_bar("dist:", ctx->dist, ctx->dist);
219 	print_bar("pressure:", ctx->pressure, ctx->pressure);
220 	print_bar("rotation:", ctx->rotation, ctx->rotation/360.0);
221 	print_bar("slider:", ctx->slider, (ctx->slider + 1.0)/2.0);
222 	print_buttons(ctx,
223 		      ctx->buttons_down,
224 		      ARRAY_LENGTH(ctx->buttons_down));
225 	lines_printed += 11;
226 
227 	printf("evdev:\n");
228 	print_bar("ABS_X:", ctx->abs.x, normalize(ctx->evdev, ABS_X, ctx->abs.x));
229 	print_bar("ABS_Y:", ctx->abs.y, normalize(ctx->evdev, ABS_Y, ctx->abs.y));
230 	print_bar("ABS_Z:", ctx->abs.z, normalize(ctx->evdev, ABS_Z, ctx->abs.z));
231 	print_bar("ABS_TILT_X:", ctx->abs.tilt_x, normalize(ctx->evdev, ABS_TILT_X, ctx->abs.tilt_x));
232 	print_bar("ABS_TILT_Y:", ctx->abs.tilt_y, normalize(ctx->evdev, ABS_TILT_Y, ctx->abs.tilt_y));
233 	print_bar("ABS_DISTANCE:", ctx->abs.distance, normalize(ctx->evdev, ABS_DISTANCE, ctx->abs.distance));
234 	print_bar("ABS_PRESSURE:", ctx->abs.pressure, normalize(ctx->evdev, ABS_PRESSURE, ctx->abs.pressure));
235 	print_buttons(ctx,
236 		      ctx->evdev_buttons_down,
237 		      ARRAY_LENGTH(ctx->evdev_buttons_down));
238 	lines_printed += 9;
239 
240 	return lines_printed;
241 }
242 
243 static void
handle_device_added(struct context * ctx,struct libinput_event * ev)244 handle_device_added(struct context *ctx, struct libinput_event *ev)
245 {
246 	struct libinput_device *device = libinput_event_get_device(ev);
247 	struct udev_device *udev_device;
248 	const char *devnode;
249 
250 	if (ctx->device)
251 		return;
252 
253 	if (!libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL))
254 		return;
255 
256 	ctx->device = libinput_device_ref(device);
257 
258 	udev_device = libinput_device_get_udev_device(device);
259 	if (!udev_device)
260 		return;
261 
262 	devnode = udev_device_get_devnode(udev_device);
263 	if (devnode) {
264 		int fd = open(devnode, O_RDONLY|O_NONBLOCK);
265 		assert(fd != -1);
266 		assert(libevdev_new_from_fd(fd, &ctx->evdev) == 0);
267 	}
268 
269 	udev_device_unref(udev_device);
270 }
271 
272 static void
handle_device_removed(struct context * ctx,struct libinput_event * ev)273 handle_device_removed(struct context *ctx, struct libinput_event *ev)
274 {
275 	struct libinput_device *device = libinput_event_get_device(ev);
276 
277 	if (ctx->device != device)
278 		return;
279 
280 	libinput_device_unref(ctx->device);
281 	ctx->device = NULL;
282 
283 	libevdev_free(ctx->evdev);
284 	ctx->evdev = NULL;
285 
286 	close(ctx->fds[1].fd);
287 	ctx->fds[1].fd = -1;
288 }
289 
290 static void
update_tablet_axes(struct context * ctx,struct libinput_event_tablet_tool * t)291 update_tablet_axes(struct context *ctx, struct libinput_event_tablet_tool *t)
292 {
293 	ctx->x = libinput_event_tablet_tool_get_x(t);
294 	ctx->y = libinput_event_tablet_tool_get_y(t);
295 	ctx->x_norm = libinput_event_tablet_tool_get_x_transformed(t, 1.0);
296 	ctx->y_norm = libinput_event_tablet_tool_get_y_transformed(t, 1.0);
297 	ctx->tx = libinput_event_tablet_tool_get_tilt_x(t);
298 	ctx->ty = libinput_event_tablet_tool_get_tilt_y(t);
299 	ctx->dist = libinput_event_tablet_tool_get_distance(t);
300 	ctx->pressure = libinput_event_tablet_tool_get_pressure(t);
301 	ctx->rotation = libinput_event_tablet_tool_get_rotation(t);
302 	ctx->slider = libinput_event_tablet_tool_get_slider_position(t);
303 }
304 
305 static void
handle_tablet_button_event(struct context * ctx,struct libinput_event * ev)306 handle_tablet_button_event(struct context *ctx, struct libinput_event *ev)
307 {
308 	struct libinput_event_tablet_tool *t = libinput_event_get_tablet_tool_event(ev);
309 	unsigned int button = libinput_event_tablet_tool_get_button(t);
310 	unsigned int *btn;
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