]> diplodocus.org Git - xorg-xinput/blob - src/list.c
0e6f9bea01246afcf1d5f7dee46e136855b8fb84
[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_SHORT,
29 FORMAT_LONG,
30 FORMAT_NAME
31 };
32
33
34 static void
35 print_info(Display* dpy, XDeviceInfo *info, enum print_format format)
36 {
37 int i,j;
38 XAnyClassPtr any;
39 XKeyInfoPtr k;
40 XButtonInfoPtr b;
41 XValuatorInfoPtr v;
42 XAxisInfoPtr a;
43
44 if (format == FORMAT_NAME)
45 {
46 printf("%s\n", info->name);
47 return;
48 }
49
50 printf("\"%s\"\tid=%ld\t[", info->name, info->id);
51
52 switch (info->use) {
53 case IsXPointer:
54 printf("XPointer");
55 break;
56 case IsXKeyboard:
57 printf("XKeyboard");
58 break;
59 case IsXExtensionDevice:
60 printf("XExtensionDevice");
61 break;
62 case IsXExtensionKeyboard:
63 printf("XExtensionKeyboard");
64 break;
65 case IsXExtensionPointer:
66 printf("XExtensionPointer");
67 break;
68 default:
69 printf("Unknown class");
70 break;
71 }
72 printf("]\n");
73
74 if (format == FORMAT_SHORT)
75 return;
76
77 if(info->type != None)
78 printf("\tType is %s\n", XGetAtomName(dpy, info->type));
79
80 if (info->num_classes > 0) {
81 any = (XAnyClassPtr) (info->inputclassinfo);
82 for (i=0; i<info->num_classes; i++) {
83 switch (any->class) {
84 case KeyClass:
85 k = (XKeyInfoPtr) any;
86 printf("\tNum_keys is %d\n", k->num_keys);
87 printf("\tMin_keycode is %d\n", k->min_keycode);
88 printf("\tMax_keycode is %d\n", k->max_keycode);
89 break;
90
91 case ButtonClass:
92 b = (XButtonInfoPtr) any;
93 printf("\tNum_buttons is %d\n", b->num_buttons);
94 break;
95
96 case ValuatorClass:
97 v = (XValuatorInfoPtr) any;
98 a = (XAxisInfoPtr) ((char *) v +
99 sizeof (XValuatorInfo));
100 printf("\tNum_axes is %d\n", v->num_axes);
101 printf("\tMode is %s\n", (v->mode == Absolute) ? "Absolute" : "Relative");
102 printf("\tMotion_buffer is %ld\n", v->motion_buffer);
103 for (j=0; j<v->num_axes; j++, a++) {
104 printf("\tAxis %d :\n", j);
105 printf("\t\tMin_value is %d\n", a->min_value);
106 printf("\t\tMax_value is %d\n", a->max_value);
107 printf ("\t\tResolution is %d\n", a->resolution);
108 }
109 break;
110 default:
111 printf ("unknown class\n");
112 }
113 any = (XAnyClassPtr) ((char *) any + any->length);
114 }
115 }
116 }
117
118 static int list_xi1(Display *display,
119 enum print_format format)
120 {
121 XDeviceInfo *info;
122 int loop;
123 int num_devices;
124
125 info = XListInputDevices(display, &num_devices);
126 for(loop=0; loop<num_devices; loop++) {
127 print_info(display, info+loop, format);
128 }
129 return EXIT_SUCCESS;
130 }
131
132 #ifdef HAVE_XI2
133 /* also used from test_xi2.c */
134 void
135 print_classes_xi2(Display* display, XIAnyClassInfo **classes,
136 int num_classes)
137 {
138 int i, j;
139
140 printf("\tReporting %d classes:\n", num_classes);
141 for (i = 0; i < num_classes; i++)
142 {
143 printf("\t\tClass originated from: %d\n", classes[i]->sourceid);
144 switch(classes[i]->type)
145 {
146 case XIButtonClass:
147 {
148 XIButtonClassInfo *b = (XIButtonClassInfo*)classes[i];
149 char *name;
150 printf("\t\tButtons supported: %d\n", b->num_buttons);
151 printf("\t\tButton labels:");
152 for (j = 0; j < b->num_buttons; j++)
153 {
154 name = (b->labels[j]) ? XGetAtomName(display, b->labels[j]) : NULL;
155 printf(" %s", (name) ? name : "None");
156 XFree(name);
157 }
158 printf("\n");
159 printf("\t\tButton state:");
160 for (j = 0; j < b->state.mask_len * 8; j++)
161 if (XIMaskIsSet(b->state.mask, j))
162 printf(" %d", j);
163 printf("\n");
164
165 }
166 break;
167 case XIKeyClass:
168 {
169 XIKeyClassInfo *k = (XIKeyClassInfo*)classes[i];
170 printf("\t\tKeycodes supported: %d\n", k->num_keycodes);
171 }
172 break;
173 case XIValuatorClass:
174 {
175 XIValuatorClassInfo *v = (XIValuatorClassInfo*)classes[i];
176 char *name = v->label ? XGetAtomName(display, v->label) : NULL;
177
178 /* XXX: Bug in X servers 1.7..1.8.1, mode was |
179 OutOfProximity. Remove this once 1.9 is out. */
180 v->mode &= DeviceMode;
181
182 printf("\t\tDetail for Valuator %d:\n", v->number);
183 printf("\t\t Label: %s\n", (name) ? name : "None");
184 printf("\t\t Range: %f - %f\n", v->min, v->max);
185 printf("\t\t Resolution: %d units/m\n", v->resolution);
186 printf("\t\t Mode: %s\n", v->mode == Absolute ? "absolute" :
187 "relative");
188 if (v->mode == Absolute)
189 printf("\t\t Current value: %f\n", v->value);
190 XFree(name);
191 }
192 break;
193 }
194 }
195
196 printf("\n");
197 }
198
199 static void
200 print_info_xi2(Display* display, XIDeviceInfo *dev, enum print_format format)
201 {
202 if (format == FORMAT_NAME)
203 {
204 printf("%s\n", dev->name);
205 return;
206 }
207
208 printf("%-40s\tid=%d\t[", dev->name, dev->deviceid);
209 switch(dev->use)
210 {
211 case XIMasterPointer:
212 printf("master pointer (%d)]\n", dev->attachment);
213 break;
214 case XIMasterKeyboard:
215 printf("master keyboard (%d)]\n", dev->attachment);
216 break;
217 case XISlavePointer:
218 printf("slave pointer (%d)]\n", dev->attachment);
219 break;
220 case XISlaveKeyboard:
221 printf("slave keyboard (%d)]\n", dev->attachment);
222 break;
223 case XIFloatingSlave:
224 printf("floating slave]\n");
225 break;
226 }
227
228 if (format == FORMAT_SHORT)
229 return;
230
231 if (!dev->enabled)
232 printf("\tThis device is disabled\n");
233
234 print_classes_xi2(display, dev->classes, dev->num_classes);
235 }
236
237
238 static int
239 list_xi2(Display *display,
240 enum print_format format)
241 {
242 int major = XI_2_Major,
243 minor = XI_2_Minor;
244 int ndevices;
245 int i, j;
246 XIDeviceInfo *info, *dev;
247
248 if (XIQueryVersion(display, &major, &minor) != Success ||
249 (major * 1000 + minor) < (XI_2_Major * 1000 + XI_2_Minor))
250 {
251 fprintf(stderr, "XI2 not supported.\n");
252 return EXIT_FAILURE;
253 }
254
255 info = XIQueryDevice(display, XIAllDevices, &ndevices);
256
257 for(i = 0; i < ndevices; i++)
258 {
259 dev = &info[i];
260 if (dev->use == XIMasterPointer || dev->use == XIMasterKeyboard)
261 {
262 if (format == FORMAT_SHORT || format == FORMAT_LONG)
263 {
264 if (dev->use == XIMasterPointer)
265 printf("⎡ ");
266 else
267 printf("⎣ ");
268 }
269
270 print_info_xi2(display, dev, format);
271 for (j = 0; j < ndevices; j++)
272 {
273 XIDeviceInfo* sd = &info[j];
274
275 if ((sd->use == XISlavePointer || sd->use == XISlaveKeyboard) &&
276 (sd->attachment == dev->deviceid))
277 {
278 if (format == FORMAT_SHORT || format == FORMAT_LONG)
279 printf("%s ↳ ", dev->use == XIMasterPointer ? "⎜" : " ");
280 print_info_xi2(display, sd, format);
281 }
282 }
283 }
284 }
285
286 for (i = 0; i < ndevices; i++)
287 {
288 dev = &info[i];
289 if (dev->use == XIFloatingSlave)
290 {
291 printf("∼ ");
292 print_info_xi2(display, dev, format);
293 }
294 }
295
296
297 XIFreeDeviceInfo(info);
298 return EXIT_SUCCESS;
299 }
300 #endif
301
302 int
303 list(Display *display,
304 int argc,
305 char *argv[],
306 char *name,
307 char *desc)
308 {
309 enum print_format format = FORMAT_SHORT;
310 int arg_dev = 0;
311
312 if (argc >= 1)
313 {
314 if (strcmp(argv[0], "--short") == 0)
315 format = FORMAT_SHORT;
316 else if (strcmp(argv[0], "--long") == 0)
317 format = FORMAT_LONG;
318 else if (strcmp(argv[0], "--name-only") == 0)
319 format = FORMAT_NAME;
320 arg_dev++;
321 }
322
323 if (argc > arg_dev)
324 {
325 #ifdef HAVE_XI2
326 if (xinput_version(display) == XI_2_Major)
327 {
328 XIDeviceInfo *info = xi2_find_device_info(display, argv[arg_dev]);
329
330 if (!info) {
331 fprintf(stderr, "unable to find device %s\n", argv[arg_dev]);
332 return EXIT_FAILURE;
333 } else {
334 print_info_xi2(display, info, format);
335 return EXIT_SUCCESS;
336 }
337 } else
338 #endif
339 {
340 XDeviceInfo *info = find_device_info(display, argv[arg_dev], False);
341
342 if (!info) {
343 fprintf(stderr, "unable to find device %s\n", argv[arg_dev]);
344 return EXIT_FAILURE;
345 } else {
346 print_info(display, info, format);
347 return EXIT_SUCCESS;
348 }
349 }
350 } else {
351 #ifdef HAVE_XI2
352 if (xinput_version(display) == XI_2_Major)
353 return list_xi2(display, format);
354 #endif
355 return list_xi1(display, format);
356 }
357 }
358
359 /* end of list.c */