]> diplodocus.org Git - xorg-xinput/blob - src/test.c
a81d34a4e0e1cd07c9c193e4bc2e628c57f11f13
[xorg-xinput] / src / test.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
26 static const char rcs_id[] = "$Id: test.c,v 1.3 1997/06/09 16:06:31 fred Exp $";
27
28 #define INVALID_EVENT_TYPE -1
29
30 static int motion_type = INVALID_EVENT_TYPE;
31 static int button_press_type = INVALID_EVENT_TYPE;
32 static int button_release_type = INVALID_EVENT_TYPE;
33 static int key_press_type = INVALID_EVENT_TYPE;
34 static int key_release_type = INVALID_EVENT_TYPE;
35 static int proximity_in_type = INVALID_EVENT_TYPE;
36 static int proximity_out_type = INVALID_EVENT_TYPE;
37
38 static int
39 register_events(Display *dpy,
40 XDeviceInfo *info,
41 char *dev_name,
42 Bool handle_proximity)
43 {
44 int number = 0; /* number of events registered */
45 XEventClass event_list[7];
46 int i;
47 XAnyClassPtr any;
48 XDevice *device;
49 Window root_win;
50 unsigned long screen;
51 XInputClassInfo *ip;
52
53 screen = DefaultScreen(dpy);
54 root_win = RootWindow(dpy, screen);
55
56 device = XOpenDevice(dpy, info->id);
57
58 if (!device) {
59 fprintf(stderr, "unable to open device %s\n", dev_name);
60 return 0;
61 }
62
63 if (device->num_classes > 0) {
64 for (ip = device->classes, i=0; i<info->num_classes; ip++, i++) {
65 switch (ip->input_class) {
66 case KeyClass:
67 DeviceKeyPress(device, key_press_type, event_list[number]); number++;
68 DeviceKeyRelease(device, key_release_type, event_list[number]); number++;
69 break;
70
71 case ButtonClass:
72 DeviceButtonPress(device, button_press_type, event_list[number]); number++;
73 DeviceButtonRelease(device, button_release_type, event_list[number]); number++;
74 break;
75
76 case ValuatorClass:
77 DeviceMotionNotify(device, motion_type, event_list[number]); number++;
78 if (handle_proximity) {
79 ProximityIn(device, proximity_in_type, event_list[number]); number++;
80 ProximityOut(device, proximity_out_type, event_list[number]); number++;
81 }
82 break;
83
84 default:
85 fprintf(stderr, "unknown class\n");
86 break;
87 }
88 }
89
90 if (XSelectExtensionEvent(dpy, root_win, event_list, number)) {
91 fprintf(stderr, "error selecting extended events\n");
92 return 0;
93 }
94 }
95 return number;
96 }
97
98 static void
99 print_events(Display *dpy)
100 {
101 XEvent Event;
102
103 while(1) {
104 XNextEvent(dpy, &Event);
105
106 if (Event.type == motion_type) {
107 int loop;
108 XDeviceMotionEvent *motion = (XDeviceMotionEvent *) &Event;
109
110 printf("motion ");
111
112 for(loop=0; loop<motion->axes_count; loop++) {
113 printf("a[%d]=%d ", motion->first_axis + loop, motion->axis_data[loop]);
114 }
115 printf("\n");
116 } else if ((Event.type == button_press_type) ||
117 (Event.type == button_release_type)) {
118 int loop;
119 XDeviceButtonEvent *button = (XDeviceButtonEvent *) &Event;
120
121 printf("button %s %d ", (Event.type == button_release_type) ? "release" : "press ",
122 button->button);
123
124 for(loop=0; loop<button->axes_count; loop++) {
125 printf("a[%d]=%d ", button->first_axis + loop, button->axis_data[loop]);
126 }
127 printf("\n");
128 } else if ((Event.type == key_press_type) ||
129 (Event.type == key_release_type)) {
130 int loop;
131 XDeviceKeyEvent *key = (XDeviceKeyEvent *) &Event;
132
133 printf("key %s %d ", (Event.type == key_release_type) ? "release" : "press ",
134 key->keycode);
135
136 for(loop=0; loop<key->axes_count; loop++) {
137 printf("a[%d]=%d ", key->first_axis + loop, key->axis_data[loop]);
138 }
139 printf("\n");
140 } else if ((Event.type == proximity_out_type) ||
141 (Event.type == proximity_in_type)) {
142 int loop;
143 XProximityNotifyEvent *prox = (XProximityNotifyEvent *) &Event;
144
145 printf("proximity %s ", (Event.type == proximity_in_type) ? "in " : "out");
146
147 for(loop=0; loop<prox->axes_count; loop++) {
148 printf("a[%d]=%d ", prox->first_axis + loop, prox->axis_data[loop]);
149 }
150 printf("\n");
151 }
152 else {
153 printf("what's that %d\n", Event.type);
154 }
155 }
156 }
157
158 int
159 test(Display *display,
160 int argc,
161 char *argv[],
162 char *name,
163 char *desc)
164 {
165 XDeviceInfo *info;
166
167 if (argc != 1 && argc != 2) {
168 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
169 return EXIT_FAILURE;
170 } else {
171 Bool handle_proximity = False;
172 int idx = 0;
173
174 if (argc == 2) {
175 if (strcmp("-proximity", argv[0]) != 0) {
176 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
177 return EXIT_FAILURE;
178 }
179 handle_proximity = 1;
180 idx = 1;
181 }
182
183 info = find_device_info(display, argv[idx], True);
184
185 if (!info) {
186 fprintf(stderr, "unable to find device %s\n", argv[idx]);
187 return EXIT_FAILURE;
188 } else {
189 if (register_events(display, info, argv[idx], handle_proximity)) {
190 print_events(display);
191 }
192 else {
193 fprintf(stderr, "no event registered...\n");
194 return EXIT_FAILURE;
195 }
196 }
197 }
198 return EXIT_FAILURE;
199 }
200
201 /* end of test.c
202 */