1
0

libinput-debug-tablet-pad.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright © 2025 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. #include "config.h"
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <getopt.h>
  28. #include <inttypes.h>
  29. #include <libevdev/libevdev.h>
  30. #include <libinput.h>
  31. #include <libudev.h>
  32. #include <poll.h>
  33. #include <signal.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <sys/ioctl.h>
  37. #include <unistd.h>
  38. #include "util-files.h"
  39. #include "util-input-event.h"
  40. #include "util-macros.h"
  41. #include "util-mem.h"
  42. #include "shared.h"
  43. DEFINE_UNREF_CLEANUP_FUNC(udev_device);
  44. static volatile sig_atomic_t stop = 0;
  45. static struct tools_options options;
  46. static int termwidth = 78;
  47. struct context {
  48. struct libinput *libinput;
  49. struct libinput_device *device;
  50. struct libevdev *evdev;
  51. /* fd[0] ... libinput fd
  52. fd[1] ... libevdev fd */
  53. struct pollfd fds[2];
  54. /* libinput device state */
  55. double ring[2];
  56. double strip[2];
  57. double dial[2];
  58. unsigned int buttons_down[32];
  59. unsigned int evdev_buttons_down[BTN_START - BTN_0 + 1];
  60. /* keys[i] = keycode if a keycode is down, 8 keys simultaneously is enough */
  61. uint32_t keys[8];
  62. unsigned int nbuttons;
  63. /* libevdev device state */
  64. struct {
  65. int wheel;
  66. int throttle;
  67. int rx;
  68. int ry;
  69. } abs;
  70. struct {
  71. int wheel[2];
  72. int wheel_v120[2];
  73. } rel;
  74. };
  75. LIBINPUT_ATTRIBUTE_PRINTF(2, 3)
  76. static void
  77. print_line(const char *label, const char *format, ...)
  78. {
  79. va_list args;
  80. va_start(args, format);
  81. _autofree_ char *msg = strdup_vprintf(format, args);
  82. va_end(args);
  83. _autofree_ char *prefix = strdup_printf("%s:", label);
  84. printf(ANSI_CLEAR_LINE " %-19s %s\n", prefix, msg);
  85. }
  86. static void
  87. print_buttons(struct context *ctx, unsigned int *buttons, size_t nbuttons)
  88. {
  89. _autostrvfree_ char **strv = NULL;
  90. for (size_t i = 0; i < nbuttons; i++) {
  91. strv = strv_append_printf(strv, "%2zd: %c", i, buttons[i] ? 'X' : ' ');
  92. }
  93. _autofree_ char *btnstr = strv_join(strv, " ");
  94. print_line("buttons", "%s", btnstr ? btnstr : "");
  95. }
  96. static void
  97. print_dial(const char *prefix, double value)
  98. {
  99. print_line(prefix, "% 8.2f", value);
  100. }
  101. static void
  102. print_buttons_evdev(struct context *ctx, unsigned int *buttons, size_t nbuttons)
  103. {
  104. _autostrvfree_ char **strv = NULL;
  105. for (size_t i = 0; i < nbuttons; i++) {
  106. if (!buttons[i])
  107. continue;
  108. unsigned int button = BTN_0 + i;
  109. strv = strv_append_printf(strv,
  110. "%s",
  111. libevdev_event_code_get_name(EV_KEY, button));
  112. }
  113. _autofree_ char *btnstr = strv_join(strv, ", ");
  114. print_line("buttons", "%s", btnstr ? btnstr : "");
  115. }
  116. static void
  117. print_rel_wheel(struct context *ctx, unsigned int code, int value)
  118. {
  119. print_line(libevdev_event_code_get_name(EV_REL, code), "% 5d", value);
  120. }
  121. static void
  122. print_bar(const char *header, double value, double normalized)
  123. {
  124. char empty[256];
  125. bool oob = false;
  126. /* the bar is minimum 10 chars, otherwise 78 or whatever fits.
  127. 32 is the manually-added up length of the prefix + [|] */
  128. const int width = clamp(termwidth - 32, 10, min(78, (int)sizeof(empty) - 1));
  129. int left_pad, right_pad;
  130. memset(empty, '-', sizeof empty);
  131. if (normalized < 0.0 || normalized > 1.0) {
  132. normalized = clamp(normalized, 0.0, 1.0);
  133. oob = true;
  134. }
  135. left_pad = width * normalized + 0.5;
  136. right_pad = width - left_pad;
  137. print_line(header,
  138. "%s%8.2f [%.*s|%.*s]%s",
  139. oob ? ANSI_RED : "",
  140. value,
  141. left_pad,
  142. empty,
  143. right_pad,
  144. empty,
  145. oob ? ANSI_NORMAL : "");
  146. }
  147. static double
  148. normalize(struct libevdev *evdev, int code, int value)
  149. {
  150. const struct input_absinfo *abs;
  151. if (!evdev)
  152. return 0.0;
  153. abs = libevdev_get_abs_info(evdev, code);
  154. if (!abs)
  155. return 0.0;
  156. return 1.0 * (value - abs->minimum) / absinfo_range(abs);
  157. }
  158. static int
  159. print_state(struct context *ctx)
  160. {
  161. double w, h;
  162. int lines_printed = 0;
  163. if (!ctx->device) {
  164. printf(ANSI_RED "No device connected" ANSI_NORMAL ANSI_CLEAR_EOL "\n");
  165. } else {
  166. libinput_device_get_size(ctx->device, &w, &h);
  167. printf("Device: %s (%s)%s\n",
  168. libinput_device_get_name(ctx->device),
  169. libinput_device_get_sysname(ctx->device),
  170. ANSI_CLEAR_EOL);
  171. }
  172. lines_printed++;
  173. printf("libinput:\n");
  174. print_bar("ring 0", ctx->ring[0], ctx->ring[0] / 360.0);
  175. print_bar("ring 1", ctx->ring[1], ctx->ring[1] / 360.0);
  176. print_bar("strip 0", ctx->strip[0], ctx->strip[0]);
  177. print_bar("strip 1", ctx->strip[1], ctx->strip[1]);
  178. print_dial("dial 0", ctx->dial[0]);
  179. print_dial("dial 1", ctx->dial[1]);
  180. print_buttons(ctx,
  181. ctx->buttons_down,
  182. min(ARRAY_LENGTH(ctx->buttons_down), ctx->nbuttons));
  183. lines_printed += 8;
  184. printf("evdev:\n");
  185. print_bar("ABS_WHEEL",
  186. ctx->abs.wheel,
  187. normalize(ctx->evdev, ABS_WHEEL, ctx->abs.wheel));
  188. print_bar("ABS_THROTTLE",
  189. ctx->abs.throttle,
  190. normalize(ctx->evdev, ABS_THROTTLE, ctx->abs.throttle));
  191. print_bar("ABS_RX", ctx->abs.rx, normalize(ctx->evdev, ABS_RX, ctx->abs.rx));
  192. print_bar("ABS_RY", ctx->abs.ry, normalize(ctx->evdev, ABS_RY, ctx->abs.ry));
  193. print_rel_wheel(ctx, REL_WHEEL, ctx->rel.wheel[0]);
  194. print_rel_wheel(ctx, REL_WHEEL_HI_RES, ctx->rel.wheel_v120[0]);
  195. print_rel_wheel(ctx, REL_HWHEEL, ctx->rel.wheel[1]);
  196. print_rel_wheel(ctx, REL_HWHEEL_HI_RES, ctx->rel.wheel_v120[1]);
  197. print_buttons_evdev(ctx,
  198. ctx->evdev_buttons_down,
  199. ARRAY_LENGTH(ctx->evdev_buttons_down));
  200. lines_printed += 10;
  201. return lines_printed;
  202. }
  203. static void
  204. handle_device_added(struct context *ctx, struct libinput_event *ev)
  205. {
  206. struct libinput_device *device = libinput_event_get_device(ev);
  207. _unref_(udev_device) *udev_device = NULL;
  208. const char *devnode;
  209. if (ctx->device)
  210. return;
  211. if (!libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD))
  212. return;
  213. ctx->device = libinput_device_ref(device);
  214. ctx->nbuttons = libinput_device_tablet_pad_get_num_buttons(device);
  215. udev_device = libinput_device_get_udev_device(device);
  216. if (!udev_device)
  217. return;
  218. devnode = udev_device_get_devnode(udev_device);
  219. if (devnode) {
  220. int fd = open(devnode, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
  221. if (fd == -1)
  222. return;
  223. if (libevdev_new_from_fd(fd, &ctx->evdev) != 0) {
  224. xclose(&fd);
  225. return;
  226. }
  227. }
  228. }
  229. static void
  230. handle_device_removed(struct context *ctx, struct libinput_event *ev)
  231. {
  232. struct libinput_device *device = libinput_event_get_device(ev);
  233. if (ctx->device != device)
  234. return;
  235. libinput_device_unref(steal(&ctx->device));
  236. libevdev_free(steal(&ctx->evdev));
  237. xclose(&ctx->fds[1].fd);
  238. }
  239. static void
  240. handle_libinput_events(struct context *ctx)
  241. {
  242. struct libinput *li = ctx->libinput;
  243. struct libinput_event *ev;
  244. struct libinput_event_tablet_pad *pev;
  245. uint32_t number;
  246. double value;
  247. enum libinput_button_state state;
  248. libinput_dispatch(li);
  249. while ((ev = libinput_get_event(li))) {
  250. switch (libinput_event_get_type(ev)) {
  251. case LIBINPUT_EVENT_NONE:
  252. abort();
  253. case LIBINPUT_EVENT_DEVICE_ADDED:
  254. handle_device_added(ctx, ev);
  255. tools_device_apply_config(libinput_event_get_device(ev),
  256. &options);
  257. break;
  258. case LIBINPUT_EVENT_DEVICE_REMOVED:
  259. handle_device_removed(ctx, ev);
  260. break;
  261. case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
  262. pev = libinput_event_get_tablet_pad_event(ev);
  263. number = libinput_event_tablet_pad_get_button_number(pev);
  264. state = libinput_event_tablet_pad_get_button_state(pev);
  265. if (number < ARRAY_LENGTH(ctx->buttons_down))
  266. ctx->buttons_down[number] =
  267. state == LIBINPUT_BUTTON_STATE_PRESSED ? 1 : 0;
  268. break;
  269. case LIBINPUT_EVENT_TABLET_PAD_RING:
  270. pev = libinput_event_get_tablet_pad_event(ev);
  271. number = libinput_event_tablet_pad_get_ring_number(pev);
  272. value = libinput_event_tablet_pad_get_ring_position(pev);
  273. if (number < ARRAY_LENGTH(ctx->ring))
  274. ctx->ring[number] = value;
  275. break;
  276. case LIBINPUT_EVENT_TABLET_PAD_STRIP:
  277. pev = libinput_event_get_tablet_pad_event(ev);
  278. number = libinput_event_tablet_pad_get_strip_number(pev);
  279. value = libinput_event_tablet_pad_get_strip_position(pev);
  280. if (number < ARRAY_LENGTH(ctx->strip))
  281. ctx->strip[number] = value;
  282. break;
  283. case LIBINPUT_EVENT_TABLET_PAD_DIAL: {
  284. pev = libinput_event_get_tablet_pad_event(ev);
  285. number = libinput_event_tablet_pad_get_dial_number(pev);
  286. value = libinput_event_tablet_pad_get_dial_delta_v120(pev);
  287. if (number < ARRAY_LENGTH(ctx->dial))
  288. ctx->dial[number] = value;
  289. break;
  290. }
  291. case LIBINPUT_EVENT_TABLET_PAD_KEY: {
  292. pev = libinput_event_get_tablet_pad_event(ev);
  293. uint32_t key = libinput_event_tablet_pad_get_key(pev);
  294. if (libinput_event_tablet_pad_get_key_state(pev) ==
  295. LIBINPUT_KEY_STATE_PRESSED) {
  296. ARRAY_FOR_EACH(ctx->keys, k) {
  297. if (*k == 0) {
  298. *k = key;
  299. }
  300. }
  301. } else {
  302. ARRAY_FOR_EACH(ctx->keys, k) {
  303. if (*k == key) {
  304. *k = 0;
  305. }
  306. }
  307. }
  308. break;
  309. }
  310. default:
  311. break;
  312. }
  313. libinput_event_destroy(ev);
  314. }
  315. }
  316. static void
  317. handle_libevdev_events(struct context *ctx)
  318. {
  319. struct libevdev *evdev = ctx->evdev;
  320. struct input_event event;
  321. #define evbit(_t, _c) (((_t) << 16) | (_c))
  322. if (!evdev)
  323. return;
  324. while (libevdev_next_event(evdev, LIBEVDEV_READ_FLAG_NORMAL, &event) ==
  325. LIBEVDEV_READ_STATUS_SUCCESS) {
  326. switch (evbit(event.type, event.code)) {
  327. case evbit(EV_KEY, BTN_0)... evbit(EV_KEY, BTN_START):
  328. ctx->evdev_buttons_down[event.code - BTN_0] =
  329. event.value ? event.code : 0;
  330. break;
  331. case evbit(EV_REL, REL_WHEEL):
  332. ctx->rel.wheel[0] = event.value;
  333. break;
  334. case evbit(EV_REL, REL_HWHEEL):
  335. ctx->rel.wheel[1] = event.value;
  336. break;
  337. case evbit(EV_REL, REL_WHEEL_HI_RES):
  338. ctx->rel.wheel_v120[0] = event.value;
  339. break;
  340. case evbit(EV_REL, REL_HWHEEL_HI_RES):
  341. ctx->rel.wheel_v120[1] = event.value;
  342. break;
  343. case evbit(EV_ABS, ABS_WHEEL):
  344. ctx->abs.wheel = event.value;
  345. break;
  346. case evbit(EV_ABS, ABS_THROTTLE):
  347. ctx->abs.throttle = event.value;
  348. break;
  349. case evbit(EV_ABS, ABS_RX):
  350. ctx->abs.rx = event.value;
  351. break;
  352. case evbit(EV_ABS, ABS_RY):
  353. ctx->abs.ry = event.value;
  354. break;
  355. }
  356. }
  357. }
  358. static void
  359. sighandler(int signal, siginfo_t *siginfo, void *userdata)
  360. {
  361. stop = 1;
  362. }
  363. static void
  364. mainloop(struct context *ctx)
  365. {
  366. unsigned int lines_printed = 20;
  367. ctx->fds[0].fd = libinput_get_fd(ctx->libinput);
  368. /* pre-load the lines */
  369. for (unsigned int i = 0; i < lines_printed; i++)
  370. printf("\n");
  371. do {
  372. handle_libinput_events(ctx);
  373. handle_libevdev_events(ctx);
  374. printf(ANSI_LEFT, 1000);
  375. printf(ANSI_UP, lines_printed);
  376. lines_printed = print_state(ctx);
  377. } while (!stop && poll(ctx->fds, 2, -1) > -1);
  378. printf("\n");
  379. }
  380. static void
  381. usage(void)
  382. {
  383. printf("Usage: libinput debug-tablet [options] [--udev <seat>|--device /dev/input/event0]\n");
  384. }
  385. static void
  386. init_context(struct context *ctx)
  387. {
  388. memset(ctx, 0, sizeof *ctx);
  389. ctx->fds[0].fd = -1; /* libinput fd */
  390. ctx->fds[0].events = POLLIN;
  391. ctx->fds[0].revents = 0;
  392. ctx->fds[1].fd = -1; /* libevdev fd */
  393. ctx->fds[1].events = POLLIN;
  394. ctx->fds[1].revents = 0;
  395. }
  396. int
  397. main(int argc, char **argv)
  398. {
  399. struct context ctx;
  400. struct libinput *li;
  401. enum tools_backend backend = BACKEND_NONE;
  402. const char *seat_or_device[2] = { "seat0", NULL };
  403. struct sigaction act;
  404. bool grab = false;
  405. init_context(&ctx);
  406. tools_init_options(&options);
  407. while (1) {
  408. int c;
  409. int option_index = 0;
  410. enum {
  411. OPT_DEVICE = 1,
  412. OPT_UDEV,
  413. };
  414. static struct option opts[] = {
  415. CONFIGURATION_OPTIONS,
  416. { "help", no_argument, 0, 'h' },
  417. { "device", required_argument, 0, OPT_DEVICE },
  418. { "udev", required_argument, 0, OPT_UDEV },
  419. { 0, 0, 0, 0 }
  420. };
  421. c = getopt_long(argc, argv, "h", opts, &option_index);
  422. if (c == -1)
  423. break;
  424. switch (c) {
  425. case '?':
  426. exit(EXIT_INVALID_USAGE);
  427. break;
  428. case 'h':
  429. usage();
  430. exit(EXIT_SUCCESS);
  431. break;
  432. case OPT_DEVICE:
  433. backend = BACKEND_DEVICE;
  434. seat_or_device[0] = optarg;
  435. break;
  436. case OPT_UDEV:
  437. backend = BACKEND_UDEV;
  438. seat_or_device[0] = optarg;
  439. break;
  440. default:
  441. if (tools_parse_option(c, optarg, &options) != 0) {
  442. usage();
  443. return EXIT_INVALID_USAGE;
  444. }
  445. break;
  446. }
  447. }
  448. if (optind < argc) {
  449. if (optind < argc - 1 || backend != BACKEND_NONE) {
  450. usage();
  451. return EXIT_INVALID_USAGE;
  452. }
  453. backend = BACKEND_DEVICE;
  454. seat_or_device[0] = argv[optind];
  455. } else if (backend == BACKEND_NONE) {
  456. backend = BACKEND_UDEV;
  457. }
  458. memset(&act, 0, sizeof(act));
  459. act.sa_sigaction = sighandler;
  460. act.sa_flags = SA_SIGINFO;
  461. if (sigaction(SIGINT, &act, NULL) == -1) {
  462. fprintf(stderr,
  463. "Failed to set up signal handling (%s)\n",
  464. strerror(errno));
  465. return EXIT_FAILURE;
  466. }
  467. bool with_plugins = (options.plugins == 1);
  468. li = tools_open_backend(backend,
  469. seat_or_device,
  470. false,
  471. &grab,
  472. with_plugins,
  473. steal(&options.plugin_paths));
  474. if (!li)
  475. return EXIT_FAILURE;
  476. struct winsize w;
  477. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1)
  478. termwidth = w.ws_col;
  479. ctx.libinput = li;
  480. mainloop(&ctx);
  481. libinput_unref(li);
  482. return EXIT_SUCCESS;
  483. }