Imported Upstream version 1.15.1
[deb_xorg-server.git] / hw / dmx / examples / ev.c
CommitLineData
a09e091a
JB
1/*
2 * Copyright 2002 Red Hat Inc., Durham, North Carolina.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 *
27 * This is a simple test program that reads from /dev/input/event*,
28 * decoding events into a human readable form.
29 */
30
31/*
32 * Authors:
33 * Rickard E. (Rik) Faith <faith@redhat.com>
34 *
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <string.h>
41#include <sys/types.h>
42#include <fcntl.h>
43#include <errno.h>
44#include <time.h>
45#include <linux/input.h>
46
47struct input_event event;
48
49int
50main(int argc, char **argv)
51{
52 char name[64]; /* RATS: Use ok, but could be better */
53 char buf[256] = { 0, }; /* RATS: Use ok */
54 unsigned char mask[EV_MAX / 8 + 1]; /* RATS: Use ok */
55 int version;
56 int fd = 0;
57 int rc;
58 int i, j;
59 char *tmp;
60
61#define test_bit(bit) (mask[(bit)/8] & (1 << ((bit)%8)))
62
63 for (i = 0; i < 32; i++) {
64 snprintf(name, sizeof(name), "/dev/input/event%d", i);
65 if ((fd = open(name, O_RDONLY, 0)) >= 0) {
66 ioctl(fd, EVIOCGVERSION, &version);
67 ioctl(fd, EVIOCGNAME(sizeof(buf)), buf);
68 ioctl(fd, EVIOCGBIT(0, sizeof(mask)), mask);
69 printf("%s\n", name);
70 printf(" evdev version: %d.%d.%d\n",
71 version >> 16, (version >> 8) & 0xff, version & 0xff);
72 printf(" name: %s\n", buf);
73 printf(" features:");
74 for (j = 0; j < EV_MAX; j++) {
75 if (test_bit(j)) {
76 const char *type = "unknown";
77
78 switch (j) {
79 case EV_KEY:
80 type = "keys/buttons";
81 break;
82 case EV_REL:
83 type = "relative";
84 break;
85 case EV_ABS:
86 type = "absolute";
87 break;
88 case EV_MSC:
89 type = "reserved";
90 break;
91 case EV_LED:
92 type = "leds";
93 break;
94 case EV_SND:
95 type = "sound";
96 break;
97 case EV_REP:
98 type = "repeat";
99 break;
100 case EV_FF:
101 type = "feedback";
102 break;
103 }
104 printf(" %s", type);
105 }
106 }
107 printf("\n");
108 close(fd);
109 }
110 }
111
112 if (argc > 1) {
113 snprintf(name, sizeof(name), "/dev/input/event%d", atoi(argv[1]));
114 if ((fd = open(name, O_RDWR, 0)) >= 0) {
115 printf("%s: open, fd = %d\n", name, fd);
116 for (i = 0; i < LED_MAX; i++) {
117 event.time.tv_sec = time(0);
118 event.time.tv_usec = 0;
119 event.type = EV_LED;
120 event.code = i;
121 event.value = 0;
122 write(fd, &event, sizeof(event));
123 }
124
125 while ((rc = read(fd, &event, sizeof(event))) > 0) {
126 printf("%-24.24s.%06lu type 0x%04x; code 0x%04x;"
127 " value 0x%08x; ",
128 ctime(&event.time.tv_sec),
129 event.time.tv_usec, event.type, event.code, event.value);
130 switch (event.type) {
131 case EV_KEY:
132 if (event.code > BTN_MISC) {
133 printf("Button %d %s",
134 event.code & 0xff,
135 event.value ? "press" : "release");
136 }
137 else {
138 printf("Key %d (0x%x) %s",
139 event.code & 0xff,
140 event.code & 0xff,
141 event.value ? "press" : "release");
142 }
143 break;
144 case EV_REL:
145 switch (event.code) {
146 case REL_X:
147 tmp = "X";
148 break;
149 case REL_Y:
150 tmp = "Y";
151 break;
152 case REL_HWHEEL:
153 tmp = "HWHEEL";
154 break;
155 case REL_DIAL:
156 tmp = "DIAL";
157 break;
158 case REL_WHEEL:
159 tmp = "WHEEL";
160 break;
161 case REL_MISC:
162 tmp = "MISC";
163 break;
164 default:
165 tmp = "UNKNOWN";
166 break;
167 }
168 printf("Relative %s %d", tmp, event.value);
169 break;
170 case EV_ABS:
171 switch (event.code) {
172 case ABS_X:
173 tmp = "X";
174 break;
175 case ABS_Y:
176 tmp = "Y";
177 break;
178 case ABS_Z:
179 tmp = "Z";
180 break;
181 case ABS_RX:
182 tmp = "RX";
183 break;
184 case ABS_RY:
185 tmp = "RY";
186 break;
187 case ABS_RZ:
188 tmp = "RZ";
189 break;
190 case ABS_THROTTLE:
191 tmp = "THROTTLE";
192 break;
193 case ABS_RUDDER:
194 tmp = "RUDDER";
195 break;
196 case ABS_WHEEL:
197 tmp = "WHEEL";
198 break;
199 case ABS_GAS:
200 tmp = "GAS";
201 break;
202 case ABS_BRAKE:
203 tmp = "BRAKE";
204 break;
205 case ABS_HAT0X:
206 tmp = "HAT0X";
207 break;
208 case ABS_HAT0Y:
209 tmp = "HAT0Y";
210 break;
211 case ABS_HAT1X:
212 tmp = "HAT1X";
213 break;
214 case ABS_HAT1Y:
215 tmp = "HAT1Y";
216 break;
217 case ABS_HAT2X:
218 tmp = "HAT2X";
219 break;
220 case ABS_HAT2Y:
221 tmp = "HAT2Y";
222 break;
223 case ABS_HAT3X:
224 tmp = "HAT3X";
225 break;
226 case ABS_HAT3Y:
227 tmp = "HAT3Y";
228 break;
229 case ABS_PRESSURE:
230 tmp = "PRESSURE";
231 break;
232 case ABS_DISTANCE:
233 tmp = "DISTANCE";
234 break;
235 case ABS_TILT_X:
236 tmp = "TILT_X";
237 break;
238 case ABS_TILT_Y:
239 tmp = "TILT_Y";
240 break;
241 case ABS_MISC:
242 tmp = "MISC";
243 break;
244 default:
245 tmp = "UNKNOWN";
246 break;
247 }
248 printf("Absolute %s %d", tmp, event.value);
249 break;
250 case EV_MSC:
251 printf("Misc");
252 break;
253 case EV_LED:
254 printf("Led");
255 break;
256 case EV_SND:
257 printf("Snd");
258 break;
259 case EV_REP:
260 printf("Rep");
261 break;
262 case EV_FF:
263 printf("FF");
264 break;
265 break;
266 }
267 printf("\n");
268 }
269 printf("rc = %d, (%s)\n", rc, strerror(errno));
270 close(fd);
271 }
272 }
273 return 0;
274}