Debian Codegraph @

tools/list-local-devices.c

raw
1 /*
2 * Copyright © 2012 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 <dirent.h>
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gudev/gudev.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include "libwacom.h"
40
41 static enum output_format {
42 YAML,
43 DATAFILE,
44 } output_format = YAML;
45
46 static char *database_path;
47
48 /* Most devices have 2-3 event nodes, let's have a wrapper struct to group
49 * those together */
50 struct tablet {
51 WacomDevice *dev;
52 GList *nodes; /* list of "/dev/input/eventX" paths */
53 };
54
55 static void
56 tablet_destroy(gpointer data)
57 {
58 struct tablet *d = data;
59
60 libwacom_destroy(d->dev);
61 g_list_free_full(d->nodes, free);
62 free(data);
63 }
64
65 /* Note: users with two identical devices plugged in will see
66 * as one device with twice the event nodes.
67 * Too niche to worry about.
68 */
69 static gint
70 tablet_compare(gconstpointer list_elem,
71 gconstpointer dev)
72 {
73 const struct tablet *t = list_elem;
74
75 return libwacom_compare(t->dev, dev, WCOMPARE_MATCHES);
76 }
77
78 static void
79 print_node(gpointer data,
80 gpointer user_data)
81 {
82 printf("# - %s\n", (char *)data);
83 }
84
85 static void
86 tablet_print(gpointer data,
87 gpointer user_data)
88 {
89 struct tablet *d = data;
90
91 printf("# %s\n", libwacom_get_name(d->dev));
92 g_list_foreach(d->nodes, print_node, NULL);
93 libwacom_print_device_description(STDOUT_FILENO, d->dev);
94 printf("---------------------------------------------------------------\n");
95 }
96
97 static void
98 print_devnode(gpointer data,
99 gpointer user_data)
100 {
101 const gchar *devnode = data;
102 g_autofree gchar *name = NULL;
103 gsize size;
104 g_autoptr(GError) error = NULL;
105
106 g_autofree gchar *basename = g_path_get_basename(devnode);
107 g_autofree char *path =
108 g_strdup_printf("/sys/class/input/%s/device/name", basename);
109
110 if (g_file_get_contents(path, &name, &size, &error)) {
111 printf(" - %s: '%.*s'\n", devnode, (int)(size - 1), name);
112 } else {
113 fprintf(stderr, "%s\n", error->message);
114 }
115 }
116
117 static void
118 tablet_print_yaml(gpointer data,
119 gpointer user_data)
120 {
121 struct tablet *d = data;
122 const char *name = libwacom_get_name(d->dev);
123 const char *bus = "unknown";
124 int vid = libwacom_get_vendor_id(d->dev);
125 int pid = libwacom_get_product_id(d->dev);
126 WacomBusType bustype = libwacom_get_bustype(d->dev);
127 g_autofree const WacomStylus **styli;
128 int nstyli;
129
130 switch (bustype) {
131 case WBUSTYPE_USB:
132 bus = "usb";
133 break;
134 case WBUSTYPE_SERIAL:
135 bus = "serial";
136 break;
137 case WBUSTYPE_BLUETOOTH:
138 bus = "bluetooth";
139 break;
140 case WBUSTYPE_I2C:
141 bus = "i2c";
142 break;
143 default:
144 break;
145 }
146
147 printf(" - name: '%s'\n", name);
148 printf(" bus: '%s'\n", bus);
149 printf(" vid: 0x%04x\n", vid);
150 printf(" pid: 0x%04x\n", pid);
151 printf(" nodes: \n");
152 g_list_foreach(d->nodes, print_devnode, NULL);
153
154 styli = libwacom_get_styli(d->dev, &nstyli);
155 printf(" styli:%s\n", nstyli > 0 ? "" : " []");
156 for (int i = 0; i < nstyli; i++) {
157 const WacomStylus *stylus = styli[i];
158 const char *type = "invalid";
159 WacomAxisTypeFlags axes = libwacom_stylus_get_axes(stylus);
160 WacomEraserType eraser_type = libwacom_stylus_get_eraser_type(stylus);
161 const char *etype = "unknown";
162
163 /* warning: Value stored to 'type' during its initialization is never
164 * read [deadcode.DeadStores] */
165 (void)type;
166 (void)etype;
167
168 switch (libwacom_stylus_get_type(stylus)) {
169 case WSTYLUS_UNKNOWN:
170 type = "unknown";
171 break;
172 case WSTYLUS_GENERAL:
173 type = "general";
174 break;
175 case WSTYLUS_INKING:
176 type = "inking";
177 break;
178 case WSTYLUS_AIRBRUSH:
179 type = "airbrush";
180 break;
181 case WSTYLUS_CLASSIC:
182 type = "classic";
183 break;
184 case WSTYLUS_MARKER:
185 type = "marker";
186 break;
187 case WSTYLUS_STROKE:
188 type = "stroke";
189 break;
190 case WSTYLUS_PUCK:
191 type = "puck";
192 break;
193 case WSTYLUS_3D:
194 type = "3D";
195 break;
196 case WSTYLUS_MOBILE:
197 type = "mobile";
198 break;
199 }
200 printf(" - id: 0x%x\n", libwacom_stylus_get_id(stylus));
201 printf(" vid: 0x%04x\n", libwacom_stylus_get_vendor_id(stylus));
202 printf(" name: '%s'\n", libwacom_stylus_get_name(stylus));
203 printf(" type: '%s'\n", type);
204 printf(" axes: ['x', 'y' %s%s%s%s%s]\n",
205 axes & WACOM_AXIS_TYPE_TILT ? ", 'tilt'" : "",
206 axes & WACOM_AXIS_TYPE_ROTATION_Z ? ", 'rotation'" : "",
207 axes & WACOM_AXIS_TYPE_DISTANCE ? ", 'distance'" : "",
208 axes & WACOM_AXIS_TYPE_PRESSURE ? ", 'pressure'" : "",
209 axes & WACOM_AXIS_TYPE_SLIDER ? ", 'slider'" : "");
210 printf(" buttons: %d\n",
211 libwacom_stylus_get_num_buttons(stylus));
212
213 switch (eraser_type) {
214 case WACOM_ERASER_UNKNOWN:
215 etype = "unknown";
216 break;
217 case WACOM_ERASER_NONE:
218 etype = "none";
219 break;
220 case WACOM_ERASER_INVERT:
221 etype = "invert";
222 break;
223 case WACOM_ERASER_BUTTON:
224 etype = "button";
225 break;
226 }
227
228 if (libwacom_stylus_is_eraser(stylus)) {
229 printf(" is_eraser: 'true'\n");
230 printf(" eraser_type: '%s'\n", etype);
231 } else {
232 printf(" erasers: [");
233 if (libwacom_stylus_has_eraser(stylus)) {
234 int npaired;
235 g_autofree const WacomStylus **paired =
236 libwacom_stylus_get_paired_styli(stylus,
237 &npaired);
238 for (int j = 0; j < npaired; j++) {
239 const WacomStylus *p = paired[j];
240 printf("%s0x%x",
241 j == 0 ? "" : ", ",
242 libwacom_stylus_get_id(p));
243 }
244 }
245 printf("]\n");
246 }
247 }
248 }
249
250 static void
251 check_if_udev_tablet(const char *path)
252 {
253 const char *const subsystems[] = { "input", NULL };
254 g_autoptr(GUdevClient) client = g_udev_client_new(subsystems);
255 g_autoptr(GUdevDevice) device =
256 g_udev_client_query_by_device_file(client, path);
257
258 if (device &&
259 g_udev_device_get_property_as_boolean(device, "ID_INPUT_TABLET")) {
260 g_autoptr(GUdevDevice) parent = g_udev_device_get_parent(device);
261 g_autofree char *info = NULL;
262
263 if (parent) {
264 const char *bus = g_udev_device_get_property(parent, "ID_BUS");
265 const char *vid =
266 g_udev_device_get_property(parent, "ID_VENDOR_ID");
267 const char *pid =
268 g_udev_device_get_property(parent, "ID_MODEL_ID");
269 const char *name = g_udev_device_get_property(parent, "NAME");
270
271 if (bus && vid && pid && name)
272 info = g_strdup_printf("(%s:%s:%s - %s) ",
273 bus,
274 vid,
275 pid,
276 name);
277 }
278
279 fprintf(stderr,
280 "%s %sis a tablet but not supported by libwacom\n",
281 path,
282 info ? info : "");
283 }
284 }
285
286 static gboolean
287 check_format(const gchar *option_name,
288 const gchar *value,
289 gpointer data,
290 GError **error)
291 {
292 if (g_str_equal(value, "datafile"))
293 output_format = DATAFILE;
294 else if (g_str_equal(value, "yaml"))
295 output_format = YAML;
296 else
297 return FALSE;
298 return TRUE;
299 }
300
301 /* clang-format off */
302 static GOptionEntry opts[] = {
303 { "database", 0, 0, G_OPTION_ARG_FILENAME, &database_path, N_("Path to device database"), NULL },
304 { "format", 0, 0, G_OPTION_ARG_CALLBACK, check_format, N_("Output format, one of 'yaml', 'datafile'"), NULL },
305 { .long_name = NULL }
306 };
307 /* clang-format on */
308
309 int
310 main(int argc,
311 char **argv)
312 {
313 WacomDeviceDatabase *db;
314 g_autoptr(GOptionContext) context = g_option_context_new(NULL);
315 g_autoptr(GError) error = NULL;
316 GList *tabletlist = NULL;
317 GDir *dir = NULL;
318 const char *filename;
319
320 g_option_context_add_main_entries(context, opts, NULL);
321
322 if (!g_option_context_parse(context, &argc, &argv, &error)) {
323 if (error != NULL)
324 fprintf(stderr, "%s\n", error->message);
325 return EXIT_FAILURE;
326 }
327
328 if (database_path) {
329 db = libwacom_database_new_for_path(database_path);
330 g_free(database_path);
331 } else {
332 #ifdef DATABASEPATH
333 db = libwacom_database_new_for_path(DATABASEPATH);
334 #else
335 db = libwacom_database_new();
336 #endif
337 }
338
339 if (!db) {
340 fprintf(stderr, "Failed to initialize device database\n");
341 return EXIT_FAILURE;
342 }
343
344 dir = g_dir_open("/dev/input", 0, &error);
345 if (!dir) {
346 fprintf(stderr, "%s\n", error->message);
347 return EXIT_FAILURE;
348 }
349
350 while ((filename = g_dir_read_name(dir))) {
351 WacomDevice *dev;
352 char fname[PATH_MAX];
353 GList *found;
354
355 if (!g_str_has_prefix(filename, "event"))
356 continue;
357
358 snprintf(fname, sizeof(fname), "/dev/input/%s", filename);
359
360 dev = libwacom_new_from_path(db, fname, WFALLBACK_NONE, NULL);
361 if (!dev) {
362 check_if_udev_tablet(fname);
363 continue;
364 }
365
366 found = g_list_find_custom(tabletlist, dev, tablet_compare);
367 if (found) {
368 struct tablet *t = found->data;
369 t->nodes = g_list_append(t->nodes, g_strdup(fname));
370 libwacom_destroy(dev);
371 } else {
372 struct tablet *t = g_new0(struct tablet, 1);
373 t->dev = dev;
374 t->nodes = g_list_append(t->nodes, g_strdup(fname));
375 tabletlist = g_list_append(tabletlist, t);
376 }
377 }
378
379 if (!tabletlist) {
380 fprintf(stderr, "Failed to find any devices known to libwacom.\n");
381 } else {
382 switch (output_format) {
383 case DATAFILE:
384 g_list_foreach(tabletlist, tablet_print, NULL);
385 break;
386 case YAML:
387 printf("devices:\n");
388 g_list_foreach(tabletlist, tablet_print_yaml, db);
389 break;
390 default:
391 abort();
392 }
393 }
394
395 g_list_free_full(tabletlist, tablet_destroy);
396 g_dir_close(dir);
397 libwacom_database_destroy(db);
398 return 0;
399 }
400
401 /* vim: set noexpandtab tabstop=8 shiftwidth=8: */