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