libinput-device-group.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <libudev.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "libinput-util.h"
  29. #ifdef HAVE_LIBWACOM
  30. #include <libwacom/libwacom.h>
  31. static void
  32. wacom_handle_paired(struct udev_device *device, int *vendor_id, int *product_id)
  33. {
  34. WacomDeviceDatabase *db = NULL;
  35. WacomDevice *tablet = NULL;
  36. const WacomMatch *paired;
  37. db = libwacom_database_new();
  38. if (!db)
  39. goto out;
  40. tablet = libwacom_new_from_usbid(db, *vendor_id, *product_id, NULL);
  41. if (!tablet)
  42. goto out;
  43. paired = libwacom_get_paired_device(tablet);
  44. if (!paired)
  45. goto out;
  46. *vendor_id = libwacom_match_get_vendor_id(paired);
  47. *product_id = libwacom_match_get_product_id(paired);
  48. out:
  49. if (tablet)
  50. libwacom_destroy(tablet);
  51. if (db)
  52. libwacom_database_destroy(db);
  53. }
  54. static int
  55. find_tree_distance(struct udev_device *a, struct udev_device *b)
  56. {
  57. struct udev_device *ancestor_a = a;
  58. int dist_a = 0;
  59. while (ancestor_a != NULL) {
  60. const char *path_a = udev_device_get_syspath(ancestor_a);
  61. struct udev_device *ancestor_b = b;
  62. int dist_b = 0;
  63. while (ancestor_b != NULL) {
  64. const char *path_b = udev_device_get_syspath(ancestor_b);
  65. if (streq(path_a, path_b))
  66. return dist_a + dist_b;
  67. dist_b++;
  68. ancestor_b = udev_device_get_parent(ancestor_b);
  69. }
  70. dist_a++;
  71. ancestor_a = udev_device_get_parent(ancestor_a);
  72. }
  73. return -1;
  74. }
  75. static void
  76. wacom_handle_ekr(struct udev_device *device,
  77. int *vendor_id,
  78. int *product_id,
  79. char **phys_attr)
  80. {
  81. struct udev *udev;
  82. struct udev_enumerate *e;
  83. struct udev_list_entry *entry = NULL;
  84. int best_dist = -1;
  85. udev = udev_device_get_udev(device);
  86. e = udev_enumerate_new(udev);
  87. udev_enumerate_add_match_subsystem(e, "input");
  88. udev_enumerate_add_match_sysname(e, "input*");
  89. udev_enumerate_scan_devices(e);
  90. udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
  91. struct udev_device *d;
  92. _autofree_ char *phys = NULL;
  93. const char *path;
  94. const char *pidstr, *vidstr;
  95. int pid, vid, dist;
  96. /* Find and use the closest Wacom device on the system,
  97. * relying on wacom_handle_paired() to fix our ID later
  98. * if needed.
  99. */
  100. path = udev_list_entry_get_name(entry);
  101. d = udev_device_new_from_syspath(udev, path);
  102. if (!d)
  103. continue;
  104. vidstr = udev_device_get_property_value(d, "ID_VENDOR_ID");
  105. pidstr = udev_device_get_property_value(d, "ID_MODEL_ID");
  106. phys = str_sanitize(udev_device_get_sysattr_value(d, "phys"));
  107. if (vidstr && pidstr && phys && safe_atoi_base(vidstr, &vid, 16) &&
  108. safe_atoi_base(pidstr, &pid, 16) && vid == VENDOR_ID_WACOM &&
  109. pid != PRODUCT_ID_WACOM_EKR) {
  110. dist = find_tree_distance(device, d);
  111. if (dist > 0 && (dist < best_dist || best_dist < 0)) {
  112. *vendor_id = vid;
  113. *product_id = pid;
  114. best_dist = dist;
  115. free(*phys_attr);
  116. *phys_attr = steal(&phys);
  117. }
  118. }
  119. udev_device_unref(d);
  120. }
  121. udev_enumerate_unref(e);
  122. }
  123. #endif
  124. int
  125. main(int argc, char **argv)
  126. {
  127. int rc = 1;
  128. struct udev *udev = NULL;
  129. struct udev_device *device = NULL;
  130. _autofree_ char *phys = NULL;
  131. const char *syspath = NULL;
  132. const char *product;
  133. int bustype, vendor_id, product_id, version;
  134. char group[1024];
  135. char *str;
  136. if (argc != 2)
  137. return 1;
  138. syspath = argv[1];
  139. udev = udev_new();
  140. if (!udev)
  141. goto out;
  142. device = udev_device_new_from_syspath(udev, syspath);
  143. if (!device)
  144. goto out;
  145. /* Find the first parent with ATTRS{phys} set. For tablets that
  146. * value looks like usb-0000:00:14.0-1/input1. Drop the /input1
  147. * bit and use the remainder as device group identifier */
  148. while (device != NULL) {
  149. struct udev_device *parent;
  150. phys = str_sanitize(udev_device_get_sysattr_value(device, "phys"));
  151. if (phys)
  152. break;
  153. parent = udev_device_get_parent(device);
  154. udev_device_ref(parent);
  155. udev_device_unref(device);
  156. device = parent;
  157. }
  158. if (!phys)
  159. goto out;
  160. /* udev sets PRODUCT on the same device we find PHYS on, let's rely
  161. on that*/
  162. product = udev_device_get_property_value(device, "PRODUCT");
  163. if (!product)
  164. product = "00/00/00/00";
  165. if (sscanf(product,
  166. "%x/%x/%x/%x",
  167. &bustype,
  168. &vendor_id,
  169. &product_id,
  170. &version) != 4) {
  171. snprintf(group, sizeof(group), "%s:%s", product, phys);
  172. } else {
  173. char *physmatch = NULL;
  174. #ifdef HAVE_LIBWACOM
  175. if (vendor_id == VENDOR_ID_WACOM) {
  176. if (product_id == PRODUCT_ID_WACOM_EKR)
  177. wacom_handle_ekr(device,
  178. &vendor_id,
  179. &product_id,
  180. &physmatch);
  181. /* This is called for the EKR as well */
  182. wacom_handle_paired(device, &vendor_id, &product_id);
  183. }
  184. #endif
  185. snprintf(group,
  186. sizeof(group),
  187. "%x/%x/%x:%s",
  188. bustype,
  189. vendor_id,
  190. product_id,
  191. physmatch ? physmatch : phys);
  192. free(physmatch);
  193. }
  194. str = strstr(group, "/input");
  195. if (str)
  196. *str = '\0';
  197. /* Cintiq 22HD Touch has
  198. usb-0000:00:14.0-6.3.1/input0 for the touch
  199. usb-0000:00:14.0-6.3.0/input0 for the pen
  200. Check if there's a . after the last -, if so, cut off the string
  201. there.
  202. */
  203. str = strrchr(group, '.');
  204. if (str && str > strrchr(group, '-'))
  205. *str = '\0';
  206. printf("LIBINPUT_DEVICE_GROUP=%s\n", group);
  207. rc = 0;
  208. out:
  209. if (device)
  210. udev_device_unref(device);
  211. if (udev)
  212. udev_unref(udev);
  213. return rc;
  214. }