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