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