]> diplodocus.org Git - xorg-xinput/blob - src/list.c
236011e0afb56cdbe1979e8323200e55de55e2d2
[xorg-xinput] / src / list.c
1 /*
2 * Copyright 1996 by Frederic Lepied, France. <Frederic.Lepied@sugix.frmug.org>
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the authors not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. The authors make no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
13 *
14 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 */
23
24 #include "xinput.h"
25 #include <string.h>
26
27 enum print_format {
28 FORMAT_NONE,
29 FORMAT_SHORT,
30 FORMAT_LONG,
31 FORMAT_NAME,
32 FORMAT_ID,
33 };
34
35
36 static void
37 print_info(Display* dpy, XDeviceInfo *info, enum print_format format)
38 {
39 int i,j;
40 XAnyClassPtr any;
41 XKeyInfoPtr k;
42 XButtonInfoPtr b;
43 XValuatorInfoPtr v;
44 XAxisInfoPtr a;
45
46 if (format == FORMAT_NAME)
47 {
48 printf("%s\n", info->name);
49 return;
50 } else if (format == FORMAT_ID)
51 {
52 printf("%ld\n", info->id);
53 return;
54 }
55
56 printf("\"%s\"\tid=%ld\t[", info->name, info->id);
57
58 switch (info->use) {
59 case IsXPointer:
60 printf("XPointer");
61 break;
62 case IsXKeyboard:
63 printf("XKeyboard");
64 break;
65 case IsXExtensionDevice:
66 printf("XExtensionDevice");
67 break;
68 case IsXExtensionKeyboard:
69 printf("XExtensionKeyboard");
70 break;
71 case IsXExtensionPointer:
72 printf("XExtensionPointer");
73 break;
74 default:
75 printf("Unknown class");
76 break;
77 }
78 printf("]\n");
79
80 if (format == FORMAT_SHORT)
81 return;
82
83 if(info->type != None)
84 printf("\tType is %s\n", XGetAtomName(dpy, info->type));
85
86 if (info->num_classes > 0) {
87 any = (XAnyClassPtr) (info->inputclassinfo);
88 for (i=0; i<info->num_classes; i++) {
89 switch (any->class) {
90 case KeyClass:
91 k = (XKeyInfoPtr) any;
92 printf("\tNum_keys is %d\n", k->num_keys);
93 printf("\tMin_keycode is %d\n", k->min_keycode);
94 printf("\tMax_keycode is %d\n", k->max_keycode);
95 break;
96
97 case ButtonClass:
98 b = (XButtonInfoPtr) any;
99 printf("\tNum_buttons is %d\n", b->num_buttons);
100 break;
101
102 case ValuatorClass:
103 v = (XValuatorInfoPtr) any;
104 a = (XAxisInfoPtr) ((char *) v +
105 sizeof (XValuatorInfo));
106 printf("\tNum_axes is %d\n", v->num_axes);
107 printf("\tMode is %s\n", (v->mode == Absolute) ? "Absolute" : "Relative");
108 printf("\tMotion_buffer is %ld\n", v->motion_buffer);
109 for (j=0; j<v->num_axes; j++, a++) {
110 printf("\tAxis %d :\n", j);
111 printf("\t\tMin_value is %d\n", a->min_value);
112 printf("\t\tMax_value is %d\n", a->max_value);
113 printf ("\t\tResolution is %d\n", a->resolution);
114 }
115 break;
116 default:
117 printf ("unknown class\n");
118 }
119 any = (XAnyClassPtr) ((char *) any + any->length);
120 }
121 }
122 }
123
124 static int list_xi1(Display *display,
125 enum print_format format)
126 {
127 XDeviceInfo *info;
128 int loop;
129 int num_devices;
130
131 info = XListInputDevices(display, &num_devices);
132 for(loop=0; loop<num_devices; loop++) {
133 print_info(display, info+loop, format);
134 }
135 return EXIT_SUCCESS;
136 }
137
138 #ifdef HAVE_XI2
139 /* also used from test_xi2.c */
140 void
141 print_classes_xi2(Display* display, XIAnyClassInfo **classes,
142 int num_classes)
143 {
144 int i, j;
145
146 printf("\tReporting %d classes:\n", num_classes);
147 for (i = 0; i < num_classes; i++)
148 {
149 printf("\t\tClass originated from: %d. Type: ", classes[i]->sourceid);
150 switch(classes[i]->type)
151 {
152 case XIButtonClass:
153 {
154 XIButtonClassInfo *b = (XIButtonClassInfo*)classes[i];
155 char *name;
156 printf("XIButtonClass\n");
157 printf("\t\tButtons supported: %d\n", b->num_buttons);
158 printf("\t\tButton labels:");
159 for (j = 0; j < b->num_buttons; j++)
160 {
161 name = (b->labels[j]) ? XGetAtomName(display, b->labels[j]) : NULL;
162 if (name)
163 printf(" \"%s\"", name);
164 else
165 printf(" None");
166 XFree(name);
167 }
168 printf("\n");
169 printf("\t\tButton state:");
170 for (j = 0; j < b->state.mask_len * 8; j++)
171 if (XIMaskIsSet(b->state.mask, j))
172 printf(" %d", j);
173 printf("\n");
174
175 }
176 break;
177 case XIKeyClass:
178 {
179 XIKeyClassInfo *k = (XIKeyClassInfo*)classes[i];
180 printf("XIKeyClass\n");
181 printf("\t\tKeycodes supported: %d\n", k->num_keycodes);
182 }
183 break;
184 case XIValuatorClass:
185 {
186 XIValuatorClassInfo *v = (XIValuatorClassInfo*)classes[i];
187 char *name = v->label ? XGetAtomName(display, v->label) : NULL;
188
189 /* Bug in X servers 1.7..1.8.1, mode was | OutOfProximity */
190 v->mode &= DeviceMode;
191
192 printf("XIValuatorClass\n");
193 printf("\t\tDetail for Valuator %d:\n", v->number);
194 printf("\t\t Label: %s\n", (name) ? name : "None");
195 printf("\t\t Range: %f - %f\n", v->min, v->max);
196 printf("\t\t Resolution: %d units/m\n", v->resolution);
197 printf("\t\t Mode: %s\n", v->mode == Absolute ? "absolute" :
198 "relative");
199 if (v->mode == Absolute)
200 printf("\t\t Current value: %f\n", v->value);
201 XFree(name);
202 }
203 break;
204 }
205 }
206
207 printf("\n");
208 }
209
210 static void
211 print_info_xi2(Display* display, XIDeviceInfo *dev, enum print_format format)
212 {
213 if (format == FORMAT_NAME)
214 {
215 printf("%s\n", dev->name);
216 return;
217 } else if (format == FORMAT_ID)
218 {
219 printf("%d\n", dev->deviceid);
220 return;
221 }
222
223 printf("%-40s\tid=%d\t[", dev->name, dev->deviceid);
224 switch(dev->use)
225 {
226 case XIMasterPointer:
227 printf("master pointer (%d)]\n", dev->attachment);
228 break;
229 case XIMasterKeyboard:
230 printf("master keyboard (%d)]\n", dev->attachment);
231 break;
232 case XISlavePointer:
233 printf("slave pointer (%d)]\n", dev->attachment);
234 break;
235 case XISlaveKeyboard:
236 printf("slave keyboard (%d)]\n", dev->attachment);
237 break;
238 case XIFloatingSlave:
239 printf("floating slave]\n");
240 break;
241 }
242
243 if (format == FORMAT_SHORT)
244 return;
245
246 if (!dev->enabled)
247 printf("\tThis device is disabled\n");
248
249 print_classes_xi2(display, dev->classes, dev->num_classes);
250 }
251
252
253 static int
254 list_xi2(Display *display,
255 enum print_format format)
256 {
257 int major = 2,
258 minor = 0;
259 int ndevices;
260 int i, j;
261 XIDeviceInfo *info, *dev;
262
263 if (XIQueryVersion(display, &major, &minor) != Success ||
264 (major * 1000 + minor) < 2000)
265 {
266 fprintf(stderr, "XI2 not supported.\n");
267 return EXIT_FAILURE;
268 }
269
270 info = XIQueryDevice(display, XIAllDevices, &ndevices);
271
272 for(i = 0; i < ndevices; i++)
273 {
274 dev = &info[i];
275 if (dev->use == XIMasterPointer || dev->use == XIMasterKeyboard)
276 {
277 if (format == FORMAT_SHORT || format == FORMAT_LONG)
278 {
279 if (dev->use == XIMasterPointer)
280 printf("⎡ ");
281 else
282 printf("⎣ ");
283 }
284
285 print_info_xi2(display, dev, format);
286 for (j = 0; j < ndevices; j++)
287 {
288 XIDeviceInfo* sd = &info[j];
289
290 if ((sd->use == XISlavePointer || sd->use == XISlaveKeyboard) &&
291 (sd->attachment == dev->deviceid))
292 {
293 if (format == FORMAT_SHORT || format == FORMAT_LONG)
294 printf("%s ↳ ", dev->use == XIMasterPointer ? "⎜" : " ");
295 print_info_xi2(display, sd, format);
296 }
297 }
298 }
299 }
300
301 for (i = 0; i < ndevices; i++)
302 {
303 dev = &info[i];
304 if (dev->use == XIFloatingSlave)
305 {
306 printf("∼ ");
307 print_info_xi2(display, dev, format);
308 }
309 }
310
311
312 XIFreeDeviceInfo(info);
313 return EXIT_SUCCESS;
314 }
315 #endif
316
317 int
318 list(Display *display,
319 int argc,
320 char *argv[],
321 char *name,
322 char *desc)
323 {
324 enum print_format format = FORMAT_NONE;
325 int arg_dev = 1;
326
327 if (argc >= 1)
328 {
329 if (strcmp(argv[0], "--short") == 0)
330 format = FORMAT_SHORT;
331 else if (strcmp(argv[0], "--long") == 0)
332 format = FORMAT_LONG;
333 else if (strcmp(argv[0], "--name-only") == 0)
334 format = FORMAT_NAME;
335 else if (strcmp(argv[0], "--id-only") == 0)
336 format = FORMAT_ID;
337 else
338 arg_dev--;
339 }
340
341 if (argc > arg_dev)
342 {
343 if (format == FORMAT_NONE)
344 format = FORMAT_LONG;
345 #ifdef HAVE_XI2
346 if (xinput_version(display) == XI_2_Major)
347 {
348 XIDeviceInfo *info = xi2_find_device_info(display, argv[arg_dev]);
349
350 if (!info) {
351 fprintf(stderr, "unable to find device %s\n", argv[arg_dev]);
352 return EXIT_FAILURE;
353 } else {
354 print_info_xi2(display, info, format);
355 return EXIT_SUCCESS;
356 }
357 } else
358 #endif
359 {
360 XDeviceInfo *info = find_device_info(display, argv[arg_dev], False);
361
362 if (!info) {
363 fprintf(stderr, "unable to find device %s\n", argv[arg_dev]);
364 return EXIT_FAILURE;
365 } else {
366 print_info(display, info, format);
367 return EXIT_SUCCESS;
368 }
369 }
370 } else {
371 if (format == FORMAT_NONE)
372 format = FORMAT_SHORT;
373 #ifdef HAVE_XI2
374 if (xinput_version(display) == XI_2_Major)
375 return list_xi2(display, format);
376 #endif
377 return list_xi1(display, format);
378 }
379 }
380
381 /* end of list.c */