]> diplodocus.org Git - xorg-xinput/blob - src/list.c
ee6faa2e2e7cad552d0abd9b6656787a0176359f
[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 Frederic Lepied not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Frederic Lepied makes 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 * FREDERIC LEPIED DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL FREDERIC LEPIED 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(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 XAttachInfoPtr att;
38
39 printf("\"%s\"\tid=%ld\t[", info->name, info->id);
40
41 switch (info->use) {
42 case IsXPointer:
43 printf("XPointer");
44 break;
45 case IsXKeyboard:
46 printf("XKeyboard");
47 break;
48 case IsXExtensionDevice:
49 printf("XExtensionDevice");
50 break;
51 case IsXExtensionKeyboard:
52 printf("XExtensionKeyboard");
53 break;
54 case IsXExtensionPointer:
55 printf("XExtensionPointer");
56 break;
57 default:
58 printf("Unknown class");
59 break;
60 }
61 printf("]\n");
62
63 if (shortformat)
64 return;
65
66 if (info->num_classes > 0) {
67 any = (XAnyClassPtr) (info->inputclassinfo);
68 for (i=0; i<info->num_classes; i++) {
69 switch (any->class) {
70 case KeyClass:
71 k = (XKeyInfoPtr) any;
72 printf("\tNum_keys is %d\n", k->num_keys);
73 printf("\tMin_keycode is %d\n", k->min_keycode);
74 printf("\tMax_keycode is %d\n", k->max_keycode);
75 break;
76
77 case ButtonClass:
78 b = (XButtonInfoPtr) any;
79 printf("\tNum_buttons is %d\n", b->num_buttons);
80 break;
81
82 case ValuatorClass:
83 v = (XValuatorInfoPtr) any;
84 a = (XAxisInfoPtr) ((char *) v +
85 sizeof (XValuatorInfo));
86 printf("\tNum_axes is %d\n", v->num_axes);
87 printf("\tMode is %s\n", (v->mode == Absolute) ? "Absolute" : "Relative");
88 printf("\tMotion_buffer is %ld\n", v->motion_buffer);
89 for (j=0; j<v->num_axes; j++, a++) {
90 printf("\tAxis %d :\n", j);
91 printf("\t\tMin_value is %d\n", a->min_value);
92 printf("\t\tMax_value is %d\n", a->max_value);
93 printf ("\t\tResolution is %d\n", a->resolution);
94 }
95 break;
96 case AttachClass:
97 att = (XAttachInfoPtr)any;
98 printf("\tAttached to %d\n", att->attached);
99 break;
100
101 default:
102 printf ("unknown class\n");
103 }
104 any = (XAnyClassPtr) ((char *) any + any->length);
105 }
106 }
107 }
108
109 int
110 list(Display *display,
111 int argc,
112 char *argv[],
113 char *name,
114 char *desc)
115 {
116 XDeviceInfo *info;
117 int loop;
118 int shortformat = False;
119 int daemon = False;
120
121 shortformat = (argc == 1 && strcmp(argv[0], "--short") == 0);
122 daemon = (argc == 1 && strcmp(argv[0], "--loop") == 0);
123
124 if (argc == 0 || shortformat || daemon) {
125 int num_devices;
126 XEvent ev;
127
128 if (daemon)
129 {
130 XiSelectEvent(display, DefaultRootWindow(display),
131 XI_DeviceHierarchyChangedMask |
132 XI_DeviceClassesChangedMask);
133 }
134
135 do {
136 info = XListInputDevices(display, &num_devices);
137 for(loop=0; loop<num_devices; loop++) {
138 print_info(info+loop, shortformat);
139 }
140
141 /* just wait for the next generic event to come along */
142 while (daemon && !XNextEvent(display, &ev))
143 {
144 if (ev.type == GenericEvent)
145 {
146 XGenericEvent* gev = (XGenericEvent*)&ev;
147 /* we just assume that extension is IReqCode, pretty save
148 since we don't register for other events. */
149 if (gev->evtype == XI_DeviceHierarchyChangedNotify)
150 {
151 printf("Hierarchy change.\n");
152 } else if (gev->evtype == XI_DeviceClassesChangedNotify)
153 {
154 printf("Device classes changed.\n");
155 free(((XDeviceClassesChangedEvent*)&ev)->inputclassinfo);
156 }
157 break;
158 }
159 }
160 } while(daemon);
161 } else {
162 int ret = EXIT_SUCCESS;
163
164 for(loop=0; loop<argc; loop++) {
165 info = find_device_info(display, argv[0], False);
166
167 if (!info) {
168 fprintf(stderr, "unable to find device %s\n", argv[0]);
169 ret = EXIT_FAILURE;
170 } else {
171 print_info(info, shortformat);
172 }
173 }
174 return ret;
175 }
176 return EXIT_SUCCESS;
177 }
178
179 /* end of list.c */