1
0

libinput-list-devices.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * Copyright © 2015 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 <errno.h>
  25. #include <getopt.h>
  26. #include <libevdev/libevdev.h>
  27. #include <libinput-version.h>
  28. #include <libinput.h>
  29. #include <libudev.h>
  30. #include <stdbool.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "util-strings.h"
  35. #include "shared.h"
  36. static const char *
  37. tap_default(struct libinput_device *device)
  38. {
  39. if (!libinput_device_config_tap_get_finger_count(device))
  40. return "n/a";
  41. if (libinput_device_config_tap_get_default_enabled(device))
  42. return "enabled";
  43. return "disabled";
  44. }
  45. static const char *
  46. tap_button_map(struct libinput_device *device)
  47. {
  48. if (!libinput_device_config_tap_get_finger_count(device))
  49. return "n/a";
  50. switch (libinput_device_config_tap_get_button_map(device)) {
  51. case LIBINPUT_CONFIG_TAP_MAP_LRM:
  52. return "left/right/middle";
  53. case LIBINPUT_CONFIG_TAP_MAP_LMR:
  54. return "left/middle/right";
  55. }
  56. return "<invalid value>";
  57. }
  58. static const char *
  59. drag_default(struct libinput_device *device)
  60. {
  61. if (!libinput_device_config_tap_get_finger_count(device))
  62. return "n/a";
  63. if (libinput_device_config_tap_get_default_drag_enabled(device))
  64. return "enabled";
  65. return "disabled";
  66. }
  67. static const char *
  68. draglock_default(struct libinput_device *device)
  69. {
  70. if (!libinput_device_config_tap_get_finger_count(device))
  71. return "n/a";
  72. if (libinput_device_config_tap_get_default_drag_lock_enabled(device))
  73. return "enabled";
  74. return "disabled";
  75. }
  76. static const char *
  77. left_handed_default(struct libinput_device *device)
  78. {
  79. if (!libinput_device_config_left_handed_is_available(device))
  80. return "n/a";
  81. if (libinput_device_config_left_handed_get_default(device))
  82. return "enabled";
  83. return "disabled";
  84. }
  85. static const char *
  86. nat_scroll_default(struct libinput_device *device)
  87. {
  88. if (!libinput_device_config_scroll_has_natural_scroll(device))
  89. return "n/a";
  90. if (libinput_device_config_scroll_get_default_natural_scroll_enabled(device))
  91. return "enabled";
  92. return "disabled";
  93. }
  94. static const char *
  95. middle_emulation_default(struct libinput_device *device)
  96. {
  97. if (!libinput_device_config_middle_emulation_is_available(device))
  98. return "n/a";
  99. if (libinput_device_config_middle_emulation_get_default_enabled(device))
  100. return "enabled";
  101. return "disabled";
  102. }
  103. static char *
  104. calibration_default(struct libinput_device *device)
  105. {
  106. char *str;
  107. float calibration[6];
  108. if (!libinput_device_config_calibration_has_matrix(device)) {
  109. xasprintf(&str, "n/a");
  110. return str;
  111. }
  112. if (libinput_device_config_calibration_get_default_matrix(device,
  113. calibration) == 0) {
  114. xasprintf(&str, "identity matrix");
  115. return str;
  116. }
  117. xasprintf(&str,
  118. "%.2f %.2f %.2f %.2f %.2f %.2f",
  119. calibration[0],
  120. calibration[1],
  121. calibration[2],
  122. calibration[3],
  123. calibration[4],
  124. calibration[5]);
  125. return str;
  126. }
  127. static char *
  128. scroll_defaults(struct libinput_device *device)
  129. {
  130. uint32_t scroll_methods;
  131. char *str;
  132. enum libinput_config_scroll_method method;
  133. scroll_methods = libinput_device_config_scroll_get_methods(device);
  134. if (scroll_methods == LIBINPUT_CONFIG_SCROLL_NO_SCROLL) {
  135. xasprintf(&str, "none");
  136. return str;
  137. }
  138. method = libinput_device_config_scroll_get_default_method(device);
  139. xasprintf(&str,
  140. "%s%s%s%s%s%s",
  141. (method == LIBINPUT_CONFIG_SCROLL_2FG) ? "*" : "",
  142. (scroll_methods & LIBINPUT_CONFIG_SCROLL_2FG) ? "two-finger " : "",
  143. (method == LIBINPUT_CONFIG_SCROLL_EDGE) ? "*" : "",
  144. (scroll_methods & LIBINPUT_CONFIG_SCROLL_EDGE) ? "edge " : "",
  145. (method == LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) ? "*" : "",
  146. (scroll_methods & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) ? "button"
  147. : "");
  148. return str;
  149. }
  150. static const char *
  151. scroll_button_default(struct libinput_device *device)
  152. {
  153. uint32_t scroll_methods = libinput_device_config_scroll_get_methods(device);
  154. if (scroll_methods & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) {
  155. uint32_t button =
  156. libinput_device_config_scroll_get_default_button(device);
  157. return libevdev_event_code_get_name(EV_KEY, button);
  158. }
  159. return "n/a";
  160. }
  161. static const char *
  162. scroll_button_lock_default(struct libinput_device *device)
  163. {
  164. uint32_t scroll_methods = libinput_device_config_scroll_get_methods(device);
  165. if (scroll_methods & LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN) {
  166. switch (libinput_device_config_scroll_get_default_button_lock(device)) {
  167. case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_ENABLED:
  168. return "enabled";
  169. case LIBINPUT_CONFIG_SCROLL_BUTTON_LOCK_DISABLED:
  170. return "disabled";
  171. }
  172. return "<invalid value>";
  173. }
  174. return "n/a";
  175. }
  176. static char *
  177. click_defaults(struct libinput_device *device)
  178. {
  179. uint32_t click_methods;
  180. char *str;
  181. enum libinput_config_click_method method;
  182. click_methods = libinput_device_config_click_get_methods(device);
  183. if (click_methods == LIBINPUT_CONFIG_CLICK_METHOD_NONE) {
  184. xasprintf(&str, "none");
  185. return str;
  186. }
  187. method = libinput_device_config_click_get_default_method(device);
  188. xasprintf(&str,
  189. "%s%s%s%s",
  190. (method == LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS) ? "*" : "",
  191. (click_methods & LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS)
  192. ? "button-areas "
  193. : "",
  194. (method == LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER) ? "*" : "",
  195. (click_methods & LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER)
  196. ? "clickfinger "
  197. : "");
  198. return str;
  199. }
  200. static const char *
  201. clickfinger_button_map(struct libinput_device *device)
  202. {
  203. uint32_t click_methods = libinput_device_config_click_get_methods(device);
  204. if (click_methods & LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER) {
  205. switch (libinput_device_config_click_get_default_clickfinger_button_map(
  206. device)) {
  207. case LIBINPUT_CONFIG_CLICKFINGER_MAP_LMR:
  208. return "left/middle/right";
  209. case LIBINPUT_CONFIG_CLICKFINGER_MAP_LRM:
  210. return "left/right/middle";
  211. }
  212. return "<invalid value>";
  213. } else {
  214. return "n/a";
  215. }
  216. }
  217. static char *
  218. accel_profiles(struct libinput_device *device)
  219. {
  220. uint32_t profiles;
  221. char *str;
  222. enum libinput_config_accel_profile profile;
  223. if (!libinput_device_config_accel_is_available(device)) {
  224. xasprintf(&str, "n/a");
  225. return str;
  226. }
  227. profiles = libinput_device_config_accel_get_profiles(device);
  228. if (profiles == LIBINPUT_CONFIG_ACCEL_PROFILE_NONE) {
  229. xasprintf(&str, "none");
  230. return str;
  231. }
  232. profile = libinput_device_config_accel_get_default_profile(device);
  233. xasprintf(&str,
  234. "%s%s %s%s %s%s",
  235. (profile == LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT) ? "*" : "",
  236. (profiles & LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT) ? "flat" : "",
  237. (profile == LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE) ? "*" : "",
  238. (profiles & LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE) ? "adaptive" : "",
  239. (profile == LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM) ? "*" : "",
  240. (profiles & LIBINPUT_CONFIG_ACCEL_PROFILE_CUSTOM) ? "custom" : "");
  241. return str;
  242. }
  243. static const char *
  244. dwt_default(struct libinput_device *device)
  245. {
  246. if (!libinput_device_config_dwt_is_available(device))
  247. return "n/a";
  248. if (libinput_device_config_dwt_get_default_enabled(device))
  249. return "enabled";
  250. return "disabled";
  251. }
  252. static const char *
  253. dwtp_default(struct libinput_device *device)
  254. {
  255. if (!libinput_device_config_dwtp_is_available(device))
  256. return "n/a";
  257. if (libinput_device_config_dwtp_get_default_enabled(device))
  258. return "enabled";
  259. return "disabled";
  260. }
  261. static char *
  262. rotation_default(struct libinput_device *device)
  263. {
  264. char *str;
  265. double angle;
  266. if (!libinput_device_config_rotation_is_available(device)) {
  267. xasprintf(&str, "n/a");
  268. return str;
  269. }
  270. angle = libinput_device_config_rotation_get_angle(device);
  271. xasprintf(&str, "%.1f", angle);
  272. return str;
  273. }
  274. static char *
  275. area_rectangle(struct libinput_device *device)
  276. {
  277. if (libinput_device_config_area_has_rectangle(device)) {
  278. struct libinput_config_area_rectangle rect =
  279. libinput_device_config_area_get_default_rectangle(device);
  280. char *str;
  281. xasprintf(&str,
  282. "(%.2f, %.2f) - (%.2f, %.2f)",
  283. rect.x1,
  284. rect.y1,
  285. rect.x2,
  286. rect.y2);
  287. return str;
  288. }
  289. return safe_strdup("n/a");
  290. }
  291. static void
  292. print_pad_info(struct libinput_device *device)
  293. {
  294. int nbuttons, nrings, nstrips, ndials, ngroups, nmodes;
  295. struct libinput_tablet_pad_mode_group *group;
  296. nbuttons = libinput_device_tablet_pad_get_num_buttons(device);
  297. nrings = libinput_device_tablet_pad_get_num_rings(device);
  298. nstrips = libinput_device_tablet_pad_get_num_strips(device);
  299. ndials = libinput_device_tablet_pad_get_num_dials(device);
  300. ngroups = libinput_device_tablet_pad_get_num_mode_groups(device);
  301. printf("Pad:\n");
  302. printf(" Rings: %d\n", nrings);
  303. printf(" Strips: %d\n", nstrips);
  304. printf(" Dials: %d\n", ndials);
  305. printf(" Buttons: %d\n", nbuttons);
  306. printf(" Mode groups: %d\n", ngroups);
  307. for (int g = 0; g < ngroups; g++) {
  308. group = libinput_device_tablet_pad_get_mode_group(device, g);
  309. nmodes = libinput_tablet_pad_mode_group_get_num_modes(group);
  310. printf(" Group %d:\n", g);
  311. printf(" Modes: %d\n", nmodes);
  312. if (nbuttons > 0) {
  313. printf(" Buttons:");
  314. for (int b = 0; b < nbuttons; b++) {
  315. if (libinput_tablet_pad_mode_group_has_button(group, b))
  316. printf("%s%s%d",
  317. b == 0 ? " " : ", ",
  318. libinput_tablet_pad_mode_group_button_is_toggle(
  319. group,
  320. b)
  321. ? "*"
  322. : "",
  323. b);
  324. }
  325. printf("\n");
  326. }
  327. if (nrings > 0) {
  328. printf(" Rings:");
  329. for (int r = 0; r < nrings; r++) {
  330. if (libinput_tablet_pad_mode_group_has_ring(group, r))
  331. printf("%s%d", r == 0 ? " " : ", ", r);
  332. }
  333. printf("\n");
  334. }
  335. if (nstrips > 0) {
  336. printf(" Strips:");
  337. for (int s = 0; s < nstrips; s++) {
  338. if (libinput_tablet_pad_mode_group_has_strip(group, s))
  339. printf("%s%d", s == 0 ? " " : ", ", s);
  340. }
  341. printf("\n");
  342. }
  343. if (ndials > 0) {
  344. printf(" Dials:");
  345. for (int d = 0; d < ndials; d++) {
  346. if (libinput_tablet_pad_mode_group_has_dial(group, d))
  347. printf("%s%d", d == 0 ? " " : ", ", d);
  348. }
  349. printf("\n");
  350. }
  351. }
  352. }
  353. #define print_aligned(topic, fmt, ...) do {\
  354. printf("%-25s" fmt "\n", topic ":", __VA_ARGS__); \
  355. } while (0)
  356. static void
  357. print_device_notify(struct libinput_event *ev)
  358. {
  359. struct libinput_device *dev = libinput_event_get_device(ev);
  360. struct libinput_seat *seat = libinput_device_get_seat(dev);
  361. struct libinput_device_group *group;
  362. struct udev_device *udev_device;
  363. double w, h;
  364. static int next_group_id = 0;
  365. intptr_t group_id;
  366. const char *devnode;
  367. char *str;
  368. const char *bustype = "<unknown>";
  369. group = libinput_device_get_device_group(dev);
  370. group_id = (intptr_t)libinput_device_group_get_user_data(group);
  371. if (!group_id) {
  372. group_id = ++next_group_id;
  373. libinput_device_group_set_user_data(group, (void *)group_id);
  374. }
  375. udev_device = libinput_device_get_udev_device(dev);
  376. devnode = udev_device_get_devnode(udev_device);
  377. print_aligned("Device", "%s", libinput_device_get_name(dev));
  378. print_aligned("Kernel", "%s", devnode);
  379. switch (libinput_device_get_id_bustype(dev)) {
  380. case BUS_USB:
  381. bustype = "usb";
  382. break;
  383. case BUS_BLUETOOTH:
  384. bustype = "bluetooth";
  385. break;
  386. case BUS_VIRTUAL:
  387. bustype = "virtual";
  388. break;
  389. case BUS_I2C:
  390. bustype = "i2c";
  391. break;
  392. case BUS_HOST:
  393. bustype = "host";
  394. break;
  395. case BUS_I8042:
  396. bustype = "serial";
  397. break;
  398. }
  399. print_aligned("Id",
  400. "%s:%04x:%04x",
  401. bustype,
  402. libinput_device_get_id_vendor(dev),
  403. libinput_device_get_id_product(dev));
  404. print_aligned("Group", "%d", (int)group_id);
  405. print_aligned("Seat",
  406. "%s, %s",
  407. libinput_seat_get_physical_name(seat),
  408. libinput_seat_get_logical_name(seat));
  409. udev_device_unref(udev_device);
  410. if (libinput_device_get_size(dev, &w, &h) == 0)
  411. print_aligned("Size", "%.fx%.fmm", w, h);
  412. print_aligned(
  413. "Capabilities",
  414. "%s%s%s%s%s%s%s",
  415. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_KEYBOARD)
  416. ? "keyboard "
  417. : "",
  418. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_POINTER)
  419. ? "pointer "
  420. : "",
  421. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TOUCH)
  422. ? "touch "
  423. : "",
  424. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TABLET_TOOL)
  425. ? "tablet "
  426. : "",
  427. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)
  428. ? "tablet-pad"
  429. : "",
  430. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_GESTURE)
  431. ? "gesture"
  432. : "",
  433. libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_SWITCH)
  434. ? "switch"
  435. : "");
  436. print_aligned("Tap-to-click", "%s", tap_default(dev));
  437. print_aligned("Tap-and-drag", "%s", drag_default(dev));
  438. print_aligned("Tap button map", "%s", tap_button_map(dev));
  439. print_aligned("Tap drag lock", "%s", draglock_default(dev));
  440. print_aligned("Left-handed", "%s", left_handed_default(dev));
  441. print_aligned("Nat.scrolling", "%s", nat_scroll_default(dev));
  442. print_aligned("Middle emulation", "%s", middle_emulation_default(dev));
  443. str = calibration_default(dev);
  444. print_aligned("Calibration", "%s", str);
  445. free(str);
  446. str = scroll_defaults(dev);
  447. print_aligned("Scroll methods", "%s", str);
  448. free(str);
  449. print_aligned("Scroll button", "%s", scroll_button_default(dev));
  450. print_aligned("Scroll button lock", "%s", scroll_button_lock_default(dev));
  451. str = click_defaults(dev);
  452. print_aligned("Click methods", "%s", str);
  453. free(str);
  454. print_aligned("Clickfinger button map", "%s", clickfinger_button_map(dev));
  455. print_aligned("Disable-w-typing", "%s", dwt_default(dev));
  456. print_aligned("Disable-w-trackpointing", "%s", dwtp_default(dev));
  457. str = accel_profiles(dev);
  458. print_aligned("Accel profiles", "%s", str);
  459. free(str);
  460. str = rotation_default(dev);
  461. print_aligned("Rotation", "%s", str);
  462. free(str);
  463. str = area_rectangle(dev);
  464. print_aligned("Area rectangle", "%s", str);
  465. free(str);
  466. if (libinput_device_has_capability(dev, LIBINPUT_DEVICE_CAP_TABLET_PAD))
  467. print_pad_info(dev);
  468. printf("\n");
  469. }
  470. static inline void
  471. usage(void)
  472. {
  473. printf("Usage: libinput list-devices [--help|--version]\n");
  474. printf("\n"
  475. "--help ...... show this help and exit\n"
  476. "--version ... show version information and exit\n"
  477. "\n");
  478. }
  479. int
  480. main(int argc, char **argv)
  481. {
  482. struct libinput *li;
  483. struct libinput_event *ev;
  484. bool grab = false;
  485. while (1) {
  486. int c;
  487. int option_index = 0;
  488. enum {
  489. OPT_HELP = 1,
  490. OPT_VERBOSE,
  491. };
  492. static struct option opts[] = {
  493. CONFIGURATION_OPTIONS,
  494. { "help", no_argument, 0, 'h' },
  495. { "verbose", no_argument, 0, OPT_VERBOSE },
  496. { 0, 0, 0, 0 }
  497. };
  498. c = getopt_long(argc, argv, "h", opts, &option_index);
  499. if (c == -1)
  500. break;
  501. switch (c) {
  502. case '?':
  503. return EXIT_INVALID_USAGE;
  504. case 'h':
  505. case OPT_HELP:
  506. usage();
  507. return EXIT_SUCCESS;
  508. default:
  509. return EXIT_INVALID_USAGE;
  510. }
  511. }
  512. if (optind < argc) {
  513. const char *devices[32] = { NULL };
  514. size_t ndevices = 0;
  515. do {
  516. if (ndevices >= ARRAY_LENGTH(devices) - 1) {
  517. usage();
  518. return EXIT_INVALID_USAGE;
  519. }
  520. devices[ndevices++] = argv[optind];
  521. } while (++optind < argc);
  522. li = tools_open_backend(BACKEND_DEVICE,
  523. devices,
  524. false,
  525. &grab,
  526. false,
  527. NULL);
  528. } else {
  529. const char *seat[2] = { "seat0", NULL };
  530. li = tools_open_backend(BACKEND_UDEV, seat, false, &grab, false, NULL);
  531. }
  532. if (!li)
  533. return 1;
  534. libinput_dispatch(li);
  535. while ((ev = libinput_get_event(li))) {
  536. if (libinput_event_get_type(ev) == LIBINPUT_EVENT_DEVICE_ADDED)
  537. print_device_notify(ev);
  538. libinput_event_destroy(ev);
  539. libinput_dispatch(li);
  540. }
  541. libinput_unref(li);
  542. return EXIT_SUCCESS;
  543. }