Debian Codegraph @

udev/libinput-device-group.c

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