Log planime invocations.
[utils.git] / evcat.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <sys/ioctl.h>
7 #include <linux/input.h>
8
9 int main(int argc, char **argv)
10 {
11     int c, ret, fd;
12     struct input_event iev;
13     int grab, decode;
14     
15     if(argc < 2) {
16         fprintf(stderr, "usage: evcat [-gd] FILE\n");
17         exit(1);
18     }
19     grab = decode = 0;
20     while((c = getopt(argc, argv, "gd")) > 0) {
21         switch(c) {
22         case 'g':
23             grab = 1;
24             break;
25         case 'd':
26             decode = 1;
27             break;
28         default:
29             exit(1);
30         }
31     }
32     if((fd = open(argv[optind], O_RDONLY)) < 0) {
33         perror(argv[optind]);
34         exit(1);
35     }
36     if(grab && ioctl(fd, EVIOCGRAB, 1)) {
37         perror("EVIOCGRAB");
38         exit(1);
39     }
40     while(1) {
41         ret = read(fd, &iev, sizeof(iev));
42         if(ret < 0) {
43             perror("read");
44             exit(1);
45         }
46         if(ret == 0)
47             break;
48         if(decode) {
49             printf("%010lu.%06lu:\t", iev.time.tv_sec, iev.time.tv_usec);
50             switch(iev.type) {
51             case EV_SYN:
52                 printf("sync\t");
53                 switch(iev.code) {
54                 case SYN_REPORT:
55                     printf("report");
56                     break;
57                 case SYN_CONFIG:
58                     printf("config");
59                     break;
60                 default:
61                     printf("%04x", iev.code);
62                 }
63                 break;
64             case EV_KEY:
65                 printf("key\t%i\t%i", iev.code, iev.value);
66                 break;
67             case EV_REL:
68                 printf("rel\t");
69                 switch(iev.code) {
70                 case REL_X:
71                     printf("x\t%04i", iev.value);
72                     break;
73                 case REL_Y:
74                     printf("y\t%04i", iev.value);
75                     break;
76                 case REL_Z:
77                     printf("z\t%04i", iev.value);
78                     break;
79                 case REL_RX:
80                     printf("rx\t%04i", iev.value);
81                     break;
82                 case REL_RY:
83                     printf("ry\t%04i", iev.value);
84                     break;
85                 case REL_RZ:
86                     printf("rz\t%04i", iev.value);
87                     break;
88                 case REL_HWHEEL:
89                     printf("hwheel\t%04i", iev.value);
90                     break;
91                 case REL_DIAL:
92                     printf("dial\t%04i", iev.value);
93                     break;
94                 case REL_WHEEL:
95                     printf("wheel\t%04i", iev.value);
96                     break;
97                 case REL_MISC:
98                     printf("misc\t%04i", iev.value);
99                     break;
100                 }
101                 break;
102             case EV_ABS:
103                 printf("abs\t");
104                 switch(iev.code) {
105                 case ABS_X:
106                     printf("x\t%04i", iev.value);
107                     break;
108                 case ABS_Y:
109                     printf("y\t%04i", iev.value);
110                     break;
111                 case ABS_Z:
112                     printf("z\t%04i", iev.value);
113                     break;
114                 case ABS_RX:
115                     printf("rx\t%04i", iev.value);
116                     break;
117                 case ABS_RY:
118                     printf("ry\t%04i", iev.value);
119                     break;
120                 case ABS_RZ:
121                     printf("rz\t%04i", iev.value);
122                     break;
123                 case ABS_THROTTLE:
124                     printf("throttle\t%04i", iev.value);
125                     break;
126                 case ABS_RUDDER:
127                     printf("rudder\t%04i", iev.value);
128                     break;
129                 case ABS_WHEEL:
130                     printf("wheel\t%04i", iev.value);
131                     break;
132                 case ABS_GAS:
133                     printf("gas\t%04i", iev.value);
134                     break;
135                 case ABS_BRAKE:
136                     printf("brake\t%04i", iev.value);
137                     break;
138                 case ABS_PRESSURE:
139                     printf("pressure\t%04i", iev.value);
140                     break;
141                 case ABS_DISTANCE:
142                     printf("distance\t%04i", iev.value);
143                     break;
144                 case ABS_TILT_X:
145                     printf("tilt-x\t%04i", iev.value);
146                     break;
147                 case ABS_TILT_Y:
148                     printf("tilt-y\t%04i", iev.value);
149                     break;
150                 case ABS_TOOL_WIDTH:
151                     printf("width\t%04i", iev.value);
152                     break;
153                 case ABS_VOLUME:
154                     printf("volume\t%04i", iev.value);
155                     break;
156                 case ABS_MISC:
157                     printf("misc\t%04i", iev.value);
158                     break;
159                 }
160                 break;
161             case EV_MSC:
162                 printf("misc\t");
163                 switch(iev.code) {
164                 case MSC_SERIAL:
165                     printf("serial\t%i", iev.value);
166                     break;
167                 case MSC_PULSELED:
168                     printf("pled\t%i", iev.value);
169                     break;
170                 case MSC_GESTURE:
171                     printf("gesture\t%i", iev.value);
172                     break;
173                 case MSC_RAW:
174                     printf("raw\t%i", iev.value);
175                     break;
176                 case MSC_SCAN:
177                     printf("scan\t%i", iev.value);
178                     break;
179                 }
180                 break;
181             case EV_LED:
182                 printf("led\t");
183                 switch(iev.code) {
184                 case LED_NUML:
185                     printf("num\t");
186                     break;
187                 case LED_CAPSL:
188                     printf("caps\t");
189                     break;
190                 case LED_SCROLLL:
191                     printf("scroll\t");
192                     break;
193                 default:
194                     printf("%i\t", iev.code);
195                     break;
196                 }
197                 printf("%i", iev.value);
198                 break;
199             case EV_SND:
200                 printf("sound\t%i\t%i", iev.code, iev.value);
201                 break;
202             case EV_REP:
203                 printf("repeat\t");
204                 switch(iev.code) {
205                 case REP_DELAY:
206                     printf("delay\t");
207                     break;
208                 case REP_PERIOD:
209                     printf("period\t");
210                     break;
211                 default:
212                     printf("%i\t", iev.code);
213                     break;
214                 }
215                 printf("%i", iev.value);
216                 break;
217             default:
218                 printf("%04x %04x %08x", iev.type, iev.code, iev.value);
219                 break;
220             }
221             printf("\n");
222         } else {
223             printf("%010lu.%06lu: %04x %04x %08x\n", iev.time.tv_sec, iev.time.tv_usec, iev.type, iev.code, iev.value);
224         }
225         fflush(stdout);
226     }
227     if(grab && ioctl(fd, EVIOCGRAB, 0)) {
228         perror("EVIOC(un)GRAB");
229         exit(1);
230     }
231     close(fd);
232     return(0);
233 }