tizen 2.4 release
[framework/uifw/libevdev.git] / tools / libevdev-events.c
1 /*
2  * Copyright © 2013 Red Hat, Inc.
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 copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS 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 PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <assert.h>
32 #include <linux/input.h>
33
34 #include "libevdev.h"
35
36 static void
37 print_abs_bits(struct libevdev *dev, int axis)
38 {
39         const struct input_absinfo *abs;
40
41         if (!libevdev_has_event_code(dev, EV_ABS, axis))
42                 return;
43
44         abs = libevdev_get_abs_info(dev, axis);
45
46         printf("        Value   %6d\n", abs->value);
47         printf("        Min     %6d\n", abs->minimum);
48         printf("        Max     %6d\n", abs->maximum);
49         if (abs->fuzz)
50                 printf("        Fuzz    %6d\n", abs->fuzz);
51         if (abs->flat)
52                 printf("        Flat    %6d\n", abs->flat);
53         if (abs->resolution)
54                 printf("        Resolution      %6d\n", abs->resolution);
55 }
56
57 static void
58 print_code_bits(struct libevdev *dev, unsigned int type, unsigned int max)
59 {
60         unsigned int i;
61         for (i = 0; i <= max; i++) {
62                 if (!libevdev_has_event_code(dev, type, i))
63                         continue;
64
65                 printf("    Event code %i (%s)\n", i, libevdev_event_code_get_name(type, i));
66                 if (type == EV_ABS)
67                         print_abs_bits(dev, i);
68         }
69 }
70
71 static void
72 print_bits(struct libevdev *dev)
73 {
74         unsigned int i;
75         printf("Supported events:\n");
76
77         for (i = 0; i <= EV_MAX; i++) {
78                 if (libevdev_has_event_type(dev, i))
79                         printf("  Event type %d (%s)\n", i, libevdev_event_type_get_name(i));
80                 switch(i) {
81                         case EV_KEY:
82                                 print_code_bits(dev, EV_KEY, KEY_MAX);
83                                 break;
84                         case EV_REL:
85                                 print_code_bits(dev, EV_REL, REL_MAX);
86                                 break;
87                         case EV_ABS:
88                                 print_code_bits(dev, EV_ABS, ABS_MAX);
89                                 break;
90                         case EV_LED:
91                                 print_code_bits(dev, EV_LED, LED_MAX);
92                                 break;
93                 }
94         }
95 }
96
97 static void
98 print_props(struct libevdev *dev)
99 {
100         unsigned int i;
101         printf("Properties:\n");
102
103         for (i = 0; i <= INPUT_PROP_MAX; i++) {
104                 if (libevdev_has_property(dev, i))
105                         printf("  Property type %d (%s)\n", i,
106                                         libevdev_property_get_name(i));
107         }
108 }
109
110 static int
111 print_event(struct input_event *ev)
112 {
113         if (ev->type == EV_SYN)
114                 printf("Event: time %ld.%06ld, ++++++++++++++++++++ %s +++++++++++++++\n",
115                                 ev->time.tv_sec,
116                                 ev->time.tv_usec,
117                                 libevdev_event_type_get_name(ev->type));
118         else
119                 printf("Event: time %ld.%06ld, type %d (%s), code %d (%s), value %d\n",
120                         ev->time.tv_sec,
121                         ev->time.tv_usec,
122                         ev->type,
123                         libevdev_event_type_get_name(ev->type),
124                         ev->code,
125                         libevdev_event_code_get_name(ev->type, ev->code),
126                         ev->value);
127         return 0;
128 }
129
130 static int
131 print_sync_event(struct input_event *ev)
132 {
133         printf("SYNC: ");
134         print_event(ev);
135         return 0;
136 }
137
138 int
139 main(int argc, char **argv)
140 {
141         struct libevdev *dev = NULL;
142         const char *file;
143         int fd;
144         int rc = 1;
145
146         if (argc < 2)
147                 goto out;
148
149         file = argv[1];
150         fd = open(file, O_RDONLY);
151         if (fd < 0) {
152                 perror("Failed to open device");
153                 goto out;
154         }
155
156         rc = libevdev_new_from_fd(fd, &dev);
157         if (rc < 0) {
158                 fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
159                 goto out;
160         }
161
162         printf("Input device ID: bus %#x vendor %#x product %#x\n",
163                         libevdev_get_id_bustype(dev),
164                         libevdev_get_id_vendor(dev),
165                         libevdev_get_id_product(dev));
166         printf("Evdev version: %x\n", libevdev_get_driver_version(dev));
167         printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
168         printf("Phys location: %s\n", libevdev_get_phys(dev));
169         printf("Uniq identifier: %s\n", libevdev_get_uniq(dev));
170         print_bits(dev);
171         print_props(dev);
172
173         do {
174                 struct input_event ev;
175                 rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL|LIBEVDEV_READ_FLAG_BLOCKING, &ev);
176                 if (rc == LIBEVDEV_READ_STATUS_SYNC) {
177                         printf("::::::::::::::::::::: dropped ::::::::::::::::::::::\n");
178                         while (rc == LIBEVDEV_READ_STATUS_SYNC) {
179                                 print_sync_event(&ev);
180                                 rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_SYNC, &ev);
181                         }
182                         printf("::::::::::::::::::::: re-synced ::::::::::::::::::::::\n");
183                 } else if (rc == LIBEVDEV_READ_STATUS_SUCCESS)
184                         print_event(&ev);
185         } while (rc == LIBEVDEV_READ_STATUS_SYNC || rc == LIBEVDEV_READ_STATUS_SUCCESS || rc == -EAGAIN);
186
187         if (rc != LIBEVDEV_READ_STATUS_SUCCESS && rc != -EAGAIN)
188                 fprintf(stderr, "Failed to handle events: %s\n", strerror(rc));
189
190         rc = 0;
191 out:
192         libevdev_free(dev);
193
194         return rc;
195 }