Debian Codegraph @

tools/debug-device.c

raw
1 /*
2 * Copyright © 2024 Red Hat, Inc.
3 *
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of Red Hat
9 * not be used in advertising or publicity pertaining to distribution
10 * of the software without specific, written prior permission. Red
11 * Hat makes no representations about the suitability of this software
12 * for any purpose. It is provided "as is" without express or implied
13 * warranty.
14 *
15 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
17 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
19 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 *
23 * Authors:
24 * Peter Hutterer <[email protected]>
25 */
26
27 #include "config.h"
28
29 #include <assert.h>
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include "libwacom.h"
38
39 static char *database_path;
40 static gboolean with_styli;
41
42 /* clang-format off */
43 static GOptionEntry opts[] = {
44 { "database", 0, 0, G_OPTION_ARG_FILENAME, &database_path, N_("Path to device database"), NULL },
45 { "with-styli", 0, 0, G_OPTION_ARG_NONE, &with_styli, N_("Select to also list styli for this device"), NULL },
46 { .long_name = NULL }
47 };
48 /* clang-format on */
49
50 static int indent = 0;
51
52 #define push() indent += 2
53 #define pop() indent -= 2
54
55 #define ip(fmt_, ...) \
56 printf("%-*s" fmt_, indent, "", __VA_ARGS__)
57 /* Usage: p("function_name", "return value is %d", a) */
58 #define p(f_, fmt_, ...) \
59 printf("%-*s%-*s -> " fmt_ "\n", indent, "", (46 - indent), f_, __VA_ARGS__)
60
61 /* Usage: func(myfunc, "return value is %d", a) */
62 #define func(f_, fmt_, ...) \
63 p(#f_"()", fmt_, __VA_ARGS__)
64
65 /* Usage: func(myfunc, "%d", argval, "return value is %d", a) */
66 #define func_arg(f_, arg_fmt_, arg_val_, fmt_, ...) \
67 do { \
68 char buf_[256]; \
69 snprintf(buf_, sizeof(buf_), #f_"(" arg_fmt_ ")", arg_val_); \
70 p(buf_, fmt_, __VA_ARGS__); \
71 } while(0)
72
73 #define strfunc(f_, dev_) \
74 func(f_, "\"%s\"", f_(dev_))
75
76 #define hexfunc(f_, dev_) \
77 func(f_, "0x%04x", f_(dev_))
78
79 #define intfunc(f_, dev_) \
80 func(f_, "%d", f_(dev_))
81
82 static void
83 handle_match(const WacomMatch *m)
84 {
85 if (m == NULL) {
86 printf(" <none>\n");
87 return;
88 }
89 push();
90 ip("%s\n", "{");
91 push();
92 strfunc(libwacom_match_get_match_string, m);
93 strfunc(libwacom_match_get_name, m);
94 strfunc(libwacom_match_get_uniq, m);
95 hexfunc(libwacom_match_get_bustype, m);
96 hexfunc(libwacom_match_get_vendor_id, m);
97 hexfunc(libwacom_match_get_product_id, m);
98 pop();
99 ip("%s\n", "}");
100 pop();
101 }
102
103 static WacomDevice *
104 device_from_device_match(WacomDeviceDatabase *db,
105 char **parts)
106 {
107 WacomBusType bustype;
108 guint64 vid, pid;
109 const char *name = NULL;
110 const char *uniq = NULL;
111 WacomBuilder *builder;
112 WacomDevice *device;
113
114 if (g_str_equal(parts[0], "usb"))
115 bustype = WBUSTYPE_USB;
116 else if (g_str_equal(parts[0], "serial"))
117 bustype = WBUSTYPE_SERIAL;
118 else if (g_str_equal(parts[0], "bluetooth"))
119 bustype = WBUSTYPE_BLUETOOTH;
120 else if (g_str_equal(parts[0], "i2c"))
121 bustype = WBUSTYPE_I2C;
122 else {
123 fprintf(stderr, "Unknown bus type %s\n", parts[0]);
124 return NULL;
125 }
126
127 if (!g_ascii_string_to_unsigned(parts[1], 16, 0, 0xffff, &vid, NULL) ||
128 !g_ascii_string_to_unsigned(parts[2], 16, 0, 0xffff, &pid, NULL)) {
129 fprintf(stderr, "Failed to parse vid/pid\n");
130 return NULL;
131 }
132
133 if (parts[3]) {
134 name = parts[3];
135 if (parts[4])
136 uniq = parts[4];
137 }
138
139 builder = libwacom_builder_new();
140 libwacom_builder_set_bustype(builder, bustype);
141 libwacom_builder_set_usbid(builder, vid, pid);
142 if (name)
143 libwacom_builder_set_match_name(builder, name);
144 if (name)
145 libwacom_builder_set_uniq(builder, uniq);
146
147 device = libwacom_new_from_builder(db, builder, WFALLBACK_NONE, NULL);
148 libwacom_builder_destroy(builder);
149 return device;
150 }
151
152 static int
153 handle_device(WacomDeviceDatabase *db,
154 const char *path)
155 {
156 WacomDevice *device;
157 g_auto(GStrv) parts = g_strsplit(path, "|", 5);
158
159 if (parts && parts[0] && parts[1]) {
160 device = device_from_device_match(db, parts);
161 } else {
162 device = libwacom_new_from_path(db, path, WFALLBACK_NONE, NULL);
163 }
164
165 if (!device) {
166 fprintf(stderr, "Device not known to libwacom\n");
167 return EXIT_FAILURE;
168 }
169
170 strfunc(libwacom_get_name, device);
171 strfunc(libwacom_get_model_name, device);
172 strfunc(libwacom_get_layout_filename, device);
173
174 hexfunc(libwacom_get_vendor_id, device);
175 hexfunc(libwacom_get_product_id, device);
176 {
177 char *busstr = NULL;
178 switch (libwacom_get_bustype(device)) {
179 case WBUSTYPE_UNKNOWN:
180 busstr = "UNKNOWN";
181 break;
182 case WBUSTYPE_USB:
183 busstr = "USB";
184 break;
185 case WBUSTYPE_SERIAL:
186 busstr = "SERIAL";
187 break;
188 case WBUSTYPE_BLUETOOTH:
189 busstr = "BLUETOOTH";
190 break;
191 case WBUSTYPE_I2C:
192 busstr = "I2C";
193 break;
194 }
195 func(libwacom_get_bustype, "%s", busstr);
196 }
197
198 {
199 #pragma GCC diagnostic push
200 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
201 WacomClass cls = libwacom_get_class(device);
202 #pragma GCC diagnostic pop
203 const char *str = NULL;
204
205 switch (cls) {
206 case WCLASS_UNKNOWN:
207 str = "UNKNOWN";
208 break;
209 case WCLASS_INTUOS3:
210 str = "INTUOS3";
211 break;
212 case WCLASS_INTUOS4:
213 str = "INTUOS4";
214 break;
215 case WCLASS_INTUOS5:
216 str = "INTUOS5";
217 break;
218 case WCLASS_CINTIQ:
219 str = "CINTIQ";
220 break;
221 case WCLASS_BAMBOO:
222 str = "BAMBOO";
223 break;
224 case WCLASS_GRAPHIRE:
225 str = "GRAPHIRE";
226 break;
227 case WCLASS_ISDV4:
228 str = "ISDV4";
229 break;
230 case WCLASS_INTUOS:
231 str = "INTUOS";
232 break;
233 case WCLASS_INTUOS2:
234 str = "INTUOS2";
235 break;
236 case WCLASS_PEN_DISPLAYS:
237 str = "PEN_DISPLAYS";
238 break;
239 case WCLASS_REMOTE:
240 str = "REMOTE";
241 break;
242 break;
243 }
244 func(libwacom_get_class, "%s", str);
245 }
246
247 intfunc(libwacom_get_width, device);
248 intfunc(libwacom_get_height, device);
249
250 intfunc(libwacom_is_reversible, device);
251
252 {
253 const WacomMatch **matches = libwacom_get_matches(device);
254 const WacomMatch **m;
255
256 printf("libwacom_get_matches() -> {\n");
257 for (m = matches; *m; m++) {
258 handle_match(*m);
259 }
260 printf("}\n");
261 }
262
263 strfunc(libwacom_get_match, device);
264
265 printf("libwacom_get_paired_device() -> {");
266 handle_match(libwacom_get_paired_device(device));
267 printf("}\n");
268
269 intfunc(libwacom_has_stylus, device);
270 intfunc(libwacom_has_touch, device);
271 intfunc(libwacom_get_num_buttons, device);
272 intfunc(libwacom_get_num_keys, device);
273 #pragma GCC diagnostic push
274 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
275 intfunc(libwacom_has_ring, device);
276 intfunc(libwacom_has_ring2, device);
277 #pragma GCC diagnostic pop
278 intfunc(libwacom_has_touchswitch, device);
279 intfunc(libwacom_get_ring_num_modes, device);
280 intfunc(libwacom_get_ring2_num_modes, device);
281 intfunc(libwacom_get_num_strips, device);
282 intfunc(libwacom_get_strips_num_modes, device);
283 intfunc(libwacom_get_num_dials, device);
284 intfunc(libwacom_get_dial_num_modes, device);
285 intfunc(libwacom_get_dial2_num_modes, device);
286
287 {
288 WacomIntegrationFlags flags = libwacom_get_integration_flags(device);
289 func(libwacom_get_integration_flags,
290 "%s%s %s %s",
291 flags == WACOM_DEVICE_INTEGRATED_NONE ? "NONE" : "",
292 flags == WACOM_DEVICE_INTEGRATED_DISPLAY ? "DISPLAY" : "",
293 flags == WACOM_DEVICE_INTEGRATED_SYSTEM ? "SYSTEM" : "",
294 flags == WACOM_DEVICE_INTEGRATED_REMOTE ? "REMOTE" : "");
295 }
296
297 {
298 for (int i = 0; i < libwacom_get_num_buttons(device); i++) {
299 char b = 'A' + i;
300 func_arg(libwacom_get_button_led_group,
301 "%c",
302 b,
303 "%d",
304 libwacom_get_button_led_group(device, b));
305 }
306
307 for (int i = 0; i < libwacom_get_num_buttons(device); i++) {
308 char b = 'A' + i;
309 func_arg(libwacom_get_button_evdev_code,
310 "%c",
311 b,
312 "0x%x",
313 libwacom_get_button_evdev_code(device, b));
314 }
315
316 for (int i = 0; i < libwacom_get_num_buttons(device); i++) {
317 char b = 'A' + i;
318 WacomButtonFlags flags;
319
320 flags = libwacom_get_button_flag(device, b);
321 func_arg(libwacom_get_button_flag,
322 "%c",
323 b,
324 "%s%s%s%s%s%s%s%s%s%s%s%s",
325 flags == WACOM_BUTTON_NONE ? "NONE" : "",
326 flags & WACOM_BUTTON_POSITION_LEFT ? "POSITION_LEFT|"
327 : "",
328 flags & WACOM_BUTTON_POSITION_RIGHT ? "POSITION_RIGHT|"
329 : "",
330 flags & WACOM_BUTTON_POSITION_TOP ? "POSITION_TOP|"
331 : "",
332 flags & WACOM_BUTTON_POSITION_BOTTOM
333 ? "POSITION_BOTTOM|"
334 : "",
335 flags & WACOM_BUTTON_RING_MODESWITCH
336 ? "RING_MODESWITCH|"
337 : "",
338 flags & WACOM_BUTTON_RING2_MODESWITCH
339 ? "RING2_MODESWITCH|"
340 : "",
341 flags & WACOM_BUTTON_TOUCHSTRIP_MODESWITCH
342 ? "TOUCHSTRIP_MODESWITCH|"
343 : "",
344 flags & WACOM_BUTTON_TOUCHSTRIP2_MODESWITCH
345 ? "TOUCHSTRIP2_MODESWITCH|"
346 : "",
347 flags & WACOM_BUTTON_DIAL_MODESWITCH
348 ? "DIAL_MODESWITCH|"
349 : "",
350 flags & WACOM_BUTTON_DIAL2_MODESWITCH
351 ? "DIAL2_MODESWITCH|"
352 : "",
353 flags & WACOM_BUTTON_OLED ? "OLED " : "");
354 }
355
356 for (int i = 0; i < libwacom_get_num_buttons(device); i++) {
357 char b = 'A' + i;
358 WacomModeSwitch mode;
359
360 if ((libwacom_get_button_flag(device, b) &
361 WACOM_BUTTON_MODESWITCH) == 0)
362 continue;
363
364 mode = libwacom_get_button_modeswitch_mode(device, b);
365 if (mode == WACOM_MODE_SWITCH_NEXT)
366 func_arg(libwacom_get_button_modeswitch_mode,
367 "%c",
368 b,
369 "%s",
370 "MODE_SWITCH_NEXT");
371 else
372 func_arg(libwacom_get_button_modeswitch_mode,
373 "%c",
374 b,
375 "%d",
376 (int)mode);
377 }
378 }
379
380 {
381 char buf[1024] = { 0 };
382 int nleds;
383 const WacomStatusLEDs *leds = libwacom_get_status_leds(device, &nleds);
384
385 for (int i = 0; i < nleds; i++) {
386 char *ledstr = NULL;
387 switch (leds[i]) {
388 case WACOM_STATUS_LED_UNAVAILABLE:
389 ledstr = "UNAVAILABLE";
390 break;
391 case WACOM_STATUS_LED_RING:
392 ledstr = "RING";
393 break;
394 case WACOM_STATUS_LED_RING2:
395 ledstr = "RING2";
396 break;
397 case WACOM_STATUS_LED_TOUCHSTRIP:
398 ledstr = "TOUCHSTRIP";
399 break;
400 case WACOM_STATUS_LED_TOUCHSTRIP2:
401 ledstr = "TOUCHSTRIP2";
402 break;
403 case WACOM_STATUS_LED_DIAL:
404 ledstr = "DIAL";
405 break;
406 case WACOM_STATUS_LED_DIAL2:
407 ledstr = "DIAL2";
408 break;
409 }
410 snprintf(buf + strlen(buf),
411 sizeof(buf) - strlen(buf),
412 "%s%s",
413 i > 0 ? ", " : "",
414 ledstr);
415 }
416 func(libwacom_get_status_leds, "[%s]", buf);
417 }
418
419 {
420 int nstyli;
421 #pragma GCC diagnostic push
422 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
423 const int *styli = libwacom_get_supported_styli(device, &nstyli);
424 #pragma GCC diagnostic pop
425
426 {
427 char buf[1024] = { 0 };
428 for (int i = 0; i < nstyli; i++)
429 snprintf(buf + strlen(buf),
430 sizeof(buf) - strlen(buf),
431 "%s0x%06x",
432 i > 0 ? ", " : "",
433 styli[i]);
434
435 func(libwacom_get_supported_styli, "[%s]", buf);
436 }
437 }
438
439 {
440 int nstyli;
441 g_autofree const WacomStylus **styli =
442 libwacom_get_styli(device, &nstyli);
443 {
444 char buf[1024] = { 0 };
445 for (int i = 0; i < nstyli; i++) {
446 const WacomStylus *s = styli[i];
447 snprintf(buf + strlen(buf),
448 sizeof(buf) - strlen(buf),
449 "%s[0x%04x, 0x%06x]",
450 i > 0 ? ", " : "",
451 libwacom_stylus_get_vendor_id(s),
452 libwacom_stylus_get_id(s));
453 }
454
455 func(libwacom_get_styli, "[%s]", buf);
456 }
457
458 if (with_styli) {
459 printf("\n---------- Listing styli for this device ----------\n");
460
461 for (int i = 0; i < nstyli; i++) {
462 const WacomStylus *stylus = styli[i];
463 int id = libwacom_stylus_get_id(stylus);
464
465 ip("%s\n", "{");
466 push();
467 func_arg(libwacom_stylus_get_id,
468 "0x%04x",
469 id,
470 "0x%04x",
471 libwacom_stylus_get_id(stylus));
472 func_arg(libwacom_stylus_get_name,
473 "0x%04x",
474 id,
475 "%s",
476 libwacom_stylus_get_name(stylus));
477 func_arg(libwacom_stylus_get_num_buttons,
478 "0x%04x",
479 id,
480 "%d",
481 libwacom_stylus_get_num_buttons(stylus));
482 func_arg(libwacom_stylus_has_eraser,
483 "0x%04x",
484 id,
485 "%d",
486 libwacom_stylus_has_eraser(stylus));
487 func_arg(libwacom_stylus_is_eraser,
488 "0x%04x",
489 id,
490 "%d",
491 libwacom_stylus_is_eraser(stylus));
492 func_arg(libwacom_stylus_has_lens,
493 "0x%04x",
494 id,
495 "%d",
496 libwacom_stylus_has_lens(stylus));
497 func_arg(libwacom_stylus_has_wheel,
498 "0x%04x",
499 id,
500 "%d",
501 libwacom_stylus_has_wheel(stylus));
502
503 {
504 char buf[1024] = { 0 };
505 int npaired;
506 #pragma GCC diagnostic push
507 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
508 const int *paired =
509 libwacom_stylus_get_paired_ids(
510 stylus,
511 &npaired);
512 #pragma GCC diagnostic pop
513
514 for (int i = 0; i < npaired; i++)
515 snprintf(buf + strlen(buf),
516 sizeof(buf) - strlen(buf),
517 "%s0x%06x",
518 i > 0 ? ", " : "",
519 paired[i]);
520 func_arg(libwacom_stylus_get_paired_ids,
521 "0x%04x",
522 id,
523 "[%s]",
524 buf);
525 }
526
527 {
528 char buf[1024] = { 0 };
529 int npaired;
530 g_autofree const WacomStylus **paired =
531 libwacom_stylus_get_paired_styli(
532 stylus,
533 &npaired);
534
535 for (int i = 0; i < npaired; i++) {
536 const WacomStylus *p = paired[i];
537 snprintf(buf + strlen(buf),
538 sizeof(buf) - strlen(buf),
539 "%s[0x%04x, 0x%06x]",
540 i > 0 ? ", " : "",
541 libwacom_stylus_get_vendor_id(
542 p),
543 libwacom_stylus_get_id(p));
544 }
545 func_arg(libwacom_stylus_get_paired_ids,
546 "0x%04x",
547 id,
548 "[%s]",
549 buf);
550 }
551
552 {
553 WacomAxisTypeFlags flags =
554 libwacom_stylus_get_axes(stylus);
555 func_arg(libwacom_stylus_has_wheel,
556 "0x%04x",
557 id,
558 "%s%s%s%s%s%s",
559 flags == WACOM_AXIS_TYPE_NONE ? "NONE"
560 : "",
561 flags & WACOM_AXIS_TYPE_TILT ? "TILT|"
562 : "",
563 flags & WACOM_AXIS_TYPE_ROTATION_Z
564 ? "ROTATION_Z|"
565 : "",
566 flags & WACOM_AXIS_TYPE_DISTANCE
567 ? "DISTANCE|"
568 : "",
569 flags & WACOM_AXIS_TYPE_PRESSURE
570 ? "PRESSURE|"
571 : "",
572 flags & WACOM_AXIS_TYPE_SLIDER
573 ? "SLIDER"
574 : "");
575 }
576
577 {
578 const char *typestr = NULL;
579 switch (libwacom_stylus_get_type(stylus)) {
580 case WSTYLUS_UNKNOWN:
581 typestr = "UNKNOWN";
582 break;
583 case WSTYLUS_GENERAL:
584 typestr = "GENERAL";
585 break;
586 case WSTYLUS_INKING:
587 typestr = "INKING";
588 break;
589 case WSTYLUS_AIRBRUSH:
590 typestr = "AIRBRUSH";
591 break;
592 case WSTYLUS_CLASSIC:
593 typestr = "CLASSIC";
594 break;
595 case WSTYLUS_MARKER:
596 typestr = "MARKER";
597 break;
598 case WSTYLUS_STROKE:
599 typestr = "STROKE";
600 break;
601 case WSTYLUS_PUCK:
602 typestr = "PUCK";
603 break;
604 case WSTYLUS_3D:
605 typestr = "3D";
606 break;
607 case WSTYLUS_MOBILE:
608 typestr = "MOBILE";
609 break;
610 }
611
612 func_arg(libwacom_stylus_get_type,
613 "0x%04x",
614 id,
615 "%s",
616 typestr);
617 }
618
619 {
620 const char *eraserstr = NULL;
621 switch (libwacom_stylus_get_eraser_type(
622 stylus)) {
623 case WACOM_ERASER_UNKNOWN:
624 eraserstr = "UNKNOWN";
625 break;
626 case WACOM_ERASER_NONE:
627 eraserstr = "NONE";
628 break;
629 case WACOM_ERASER_INVERT:
630 eraserstr = "INVERT";
631 break;
632 case WACOM_ERASER_BUTTON:
633 eraserstr = "BUTTON";
634 break;
635 }
636
637 func_arg(libwacom_stylus_get_type,
638 "0x%04x",
639 id,
640 "%s",
641 eraserstr);
642 }
643 pop();
644 ip("%s\n", "}");
645 }
646 }
647 }
648
649 libwacom_destroy(device);
650
651 return EXIT_SUCCESS;
652 }
653
654 int
655 main(int argc,
656 char **argv)
657 {
658 WacomDeviceDatabase *db;
659 GOptionContext *context;
660 g_autoptr(GError) error = NULL;
661 int rc;
662
663 context = g_option_context_new(
664 "[/dev/input/event0 | \"usb|0123|abcd|some tablet\"]");
665 g_option_context_set_description(
666 context,
667 "The argument may be a device node or a single "
668 "DeviceMatch string as listed in .tablet files.");
669
670 g_option_context_add_main_entries(context, opts, NULL);
671
672 if (!g_option_context_parse(context, &argc, &argv, &error)) {
673 if (error != NULL)
674 fprintf(stderr, "%s\n", error->message);
675 return EXIT_FAILURE;
676 }
677
678 g_option_context_free(context);
679
680 if (database_path) {
681 db = libwacom_database_new_for_path(database_path);
682 g_free(database_path);
683 } else {
684 #ifdef DATABASEPATH
685 db = libwacom_database_new_for_path(DATABASEPATH);
686 #else
687 db = libwacom_database_new();
688 #endif
689 }
690
691 if (!db) {
692 fprintf(stderr, "Failed to initialize device database\n");
693 return EXIT_FAILURE;
694 }
695
696 if (argc <= 1) {
697 fprintf(stderr, "Missing device node or match string\n");
698 libwacom_database_destroy(db);
699 return EXIT_FAILURE;
700 }
701
702 rc = handle_device(db, argv[1]);
703
704 libwacom_database_destroy(db);
705 return rc;
706 }
707
708 /* vim: set noexpandtab tabstop=8 shiftwidth=8: */