ffb4524951b8d2721b365f7396fe01a62203637e
[platform/upstream/libinput.git] / tools / event-debug.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that 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 copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #define _GNU_SOURCE
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <poll.h>
28 #include <stdio.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <time.h>
32 #include <unistd.h>
33 #include <libudev.h>
34 #include "linux/input.h"
35 #include <sys/ioctl.h>
36 #include <sys/signalfd.h>
37
38 #include <libinput.h>
39
40 static enum {
41         MODE_UDEV,
42         MODE_DEVICE,
43 } mode = MODE_UDEV;
44 static const char *device;
45 static const char *seat = "seat0";
46 static struct udev *udev;
47 uint32_t start_time;
48 static const uint32_t screen_width = 100;
49 static const uint32_t screen_height = 100;
50 static int verbose = 0;
51
52 static void
53 usage(void)
54 {
55         printf("Usage: %s [--verbose] [--udev [<seat>]|--device /dev/input/event0]\n"
56                "--verbose ....... Print debugging output.\n"
57                "--udev <seat>.... Use udev device discovery (default).\n"
58                "                  Specifying a seat ID is optional.\n"
59                "--device /path/to/device .... open the given device only\n",
60                 program_invocation_short_name);
61 }
62
63 static int
64 parse_args(int argc, char **argv)
65 {
66         while (1) {
67                 int c;
68                 int option_index = 0;
69                 static struct option opts[] = {
70                         { "device", 1, 0, 'd' },
71                         { "udev", 0, 0, 'u' },
72                         { "help", 0, 0, 'h' },
73                         { "verbose", 0, 0, 'v'},
74                         { 0, 0, 0, 0}
75                 };
76
77                 c = getopt_long(argc, argv, "h", opts, &option_index);
78                 if (c == -1)
79                         break;
80
81                 switch(c) {
82                         case 'h': /* --help */
83                                 usage();
84                                 exit(0);
85                         case 'd': /* --device */
86                                 mode = MODE_DEVICE;
87                                 if (!optarg) {
88                                         usage();
89                                         return 1;
90                                 }
91                                 device = optarg;
92                                 break;
93                         case 'u': /* --udev */
94                                 mode = MODE_UDEV;
95                                 if (optarg)
96                                         seat = optarg;
97                                 break;
98                         case 'v': /* --verbose */
99                                 verbose = 1;
100                                 break;
101                         default:
102                                 usage();
103                                 return 1;
104                 }
105
106         }
107
108         if (optind < argc) {
109                 usage();
110                 return 1;
111         }
112
113         return 0;
114 }
115
116 static int
117 open_restricted(const char *path, int flags, void *user_data)
118 {
119         int fd = open(path, flags);
120         return fd < 0 ? -errno : fd;
121 }
122
123 static void
124 close_restricted(int fd, void *user_data)
125 {
126         close(fd);
127 }
128
129 static const struct libinput_interface interface = {
130         .open_restricted = open_restricted,
131         .close_restricted = close_restricted,
132 };
133
134 static int
135 open_udev(struct libinput **li)
136 {
137         udev = udev_new();
138         if (!udev) {
139                 fprintf(stderr, "Failed to initialize udev\n");
140                 return 1;
141         }
142
143         *li = libinput_udev_create_for_seat(&interface, NULL, udev, seat);
144         if (!*li) {
145                 fprintf(stderr, "Failed to initialize context from udev\n");
146                 return 1;
147         }
148
149         return 0;
150 }
151
152 static int
153 open_device(struct libinput **li, const char *path)
154 {
155         struct libinput_device *device;
156
157         *li = libinput_path_create_context(&interface, NULL);
158         if (!*li) {
159                 fprintf(stderr, "Failed to initialize context from %s\n", path);
160                 return 1;
161         }
162
163         device = libinput_path_add_device(*li, path);
164         if (!device) {
165                 fprintf(stderr, "Failed to initialized device %s\n", path);
166                 libinput_destroy(*li);
167                 return 1;
168         }
169
170         return 0;
171 }
172
173 static void
174 print_event_header(struct libinput_event *ev)
175 {
176         struct libinput_device *dev = libinput_event_get_device(ev);
177         const char *type;
178
179         switch(libinput_event_get_type(ev)) {
180         case LIBINPUT_EVENT_NONE:
181                 abort();
182         case LIBINPUT_EVENT_DEVICE_ADDED:
183                 type = "DEVICE_ADDED";
184                 break;
185         case LIBINPUT_EVENT_DEVICE_REMOVED:
186                 type = "DEVICE_REMOVED";
187                 break;
188         case LIBINPUT_EVENT_KEYBOARD_KEY:
189                 type = "KEYBOARD_KEY";
190                 break;
191         case LIBINPUT_EVENT_POINTER_MOTION:
192                 type = "POINTER_MOTION";
193                 break;
194         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
195                 type = "POINTER_MOTION_ABSOLUTE";
196                 break;
197         case LIBINPUT_EVENT_POINTER_BUTTON:
198                 type = "POINTER_BUTTON";
199                 break;
200         case LIBINPUT_EVENT_POINTER_AXIS:
201                 type = "POINTER_AXIS";
202                 break;
203         case LIBINPUT_EVENT_TOUCH_DOWN:
204                 type = "TOUCH_DOWN";
205                 break;
206         case LIBINPUT_EVENT_TOUCH_MOTION:
207                 type = "TOUCH_MOTION";
208                 break;
209         case LIBINPUT_EVENT_TOUCH_UP:
210                 type = "TOUCH_UP";
211                 break;
212         case LIBINPUT_EVENT_TOUCH_CANCEL:
213                 type = "TOUCH_CANCEL";
214                 break;
215         case LIBINPUT_EVENT_TOUCH_FRAME:
216                 type = "TOUCH_FRAME";
217                 break;
218         }
219
220         printf("%-7s    %s      ", libinput_device_get_sysname(dev), type);
221 }
222
223 static void
224 print_event_time(uint32_t time)
225 {
226         printf("%+6.2fs ", (time - start_time) / 1000.0);
227 }
228
229 static void
230 print_device_notify(struct libinput_event *ev)
231 {
232         struct libinput_device *dev = libinput_event_get_device(ev);
233         struct libinput_seat *seat = libinput_device_get_seat(dev);
234
235         printf("%s      %s\n",
236                libinput_seat_get_physical_name(seat),
237                libinput_seat_get_logical_name(seat));
238 }
239
240 static void
241 print_key_event(struct libinput_event *ev)
242 {
243         struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
244         enum libinput_keyboard_key_state state;
245
246         print_event_time(libinput_event_keyboard_get_time(k));
247         state = libinput_event_keyboard_get_key_state(k);
248         printf("%d %s\n",
249                libinput_event_keyboard_get_key(k),
250                state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
251 }
252
253 static void
254 print_motion_event(struct libinput_event *ev)
255 {
256         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
257         double x = libinput_event_pointer_get_dx(p);
258         double y = libinput_event_pointer_get_dy(p);
259
260         print_event_time(libinput_event_pointer_get_time(p));
261
262         printf("%6.2f/%6.2f\n", x, y);
263 }
264
265 static void
266 print_absmotion_event(struct libinput_event *ev)
267 {
268         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
269         double x = libinput_event_pointer_get_absolute_x_transformed(
270                 p, screen_width);
271         double y = libinput_event_pointer_get_absolute_y_transformed(
272                 p, screen_height);
273
274         print_event_time(libinput_event_pointer_get_time(p));
275         printf("%6.2f/%6.2f\n", x, y);
276 }
277
278 static void
279 print_button_event(struct libinput_event *ev)
280 {
281         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
282         enum libinput_button_state state;
283
284         print_event_time(libinput_event_pointer_get_time(p));
285
286         state = libinput_event_pointer_get_button_state(p);
287         printf("%3d %s, seat count: %u\n",
288                libinput_event_pointer_get_button(p),
289                state == LIBINPUT_BUTTON_STATE_PRESSED ? "pressed" : "released",
290                libinput_event_pointer_get_seat_button_count(p));
291 }
292
293 static void
294 print_axis_event(struct libinput_event *ev)
295 {
296         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
297         enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
298         const char *ax;
299         double val;
300
301         switch (axis) {
302         case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
303                 ax = "vscroll";
304                 break;
305         case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
306                 ax = "hscroll";
307                 break;
308         default:
309                 abort();
310         }
311
312         print_event_time(libinput_event_pointer_get_time(p));
313         val = libinput_event_pointer_get_axis_value(p);
314         printf("%s %.2f\n", ax, val);
315 }
316
317 static void
318 print_touch_event_without_coords(struct libinput_event *ev)
319 {
320         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
321
322         print_event_time(libinput_event_touch_get_time(t));
323         printf("\n");
324 }
325
326 static void
327 print_touch_event_with_coords(struct libinput_event *ev)
328 {
329         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
330         double x = libinput_event_touch_get_x_transformed(t, screen_width);
331         double y = libinput_event_touch_get_y_transformed(t, screen_height);
332         double xmm = libinput_event_touch_get_x(t);
333         double ymm = libinput_event_touch_get_y(t);
334
335         print_event_time(libinput_event_touch_get_time(t));
336
337         printf("%d (%d) %5.2f/%5.2f (%5.2f/%5.2fmm)\n",
338                libinput_event_touch_get_slot(t),
339                libinput_event_touch_get_seat_slot(t),
340                x, y,
341                xmm, ymm);
342 }
343
344 static int
345 handle_and_print_events(struct libinput *li)
346 {
347         int rc = -1;
348         struct libinput_event *ev;
349
350         libinput_dispatch(li);
351         while ((ev = libinput_get_event(li))) {
352                 print_event_header(ev);
353
354                 switch (libinput_event_get_type(ev)) {
355                 case LIBINPUT_EVENT_NONE:
356                         abort();
357                 case LIBINPUT_EVENT_DEVICE_ADDED:
358                 case LIBINPUT_EVENT_DEVICE_REMOVED:
359                         print_device_notify(ev);
360                         break;
361                 case LIBINPUT_EVENT_KEYBOARD_KEY:
362                         print_key_event(ev);
363                         break;
364                 case LIBINPUT_EVENT_POINTER_MOTION:
365                         print_motion_event(ev);
366                         break;
367                 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
368                         print_absmotion_event(ev);
369                         break;
370                 case LIBINPUT_EVENT_POINTER_BUTTON:
371                         print_button_event(ev);
372                         break;
373                 case LIBINPUT_EVENT_POINTER_AXIS:
374                         print_axis_event(ev);
375                         break;
376                 case LIBINPUT_EVENT_TOUCH_DOWN:
377                         print_touch_event_with_coords(ev);
378                         break;
379                 case LIBINPUT_EVENT_TOUCH_MOTION:
380                         print_touch_event_with_coords(ev);
381                         break;
382                 case LIBINPUT_EVENT_TOUCH_UP:
383                         print_touch_event_without_coords(ev);
384                         break;
385                 case LIBINPUT_EVENT_TOUCH_CANCEL:
386                         print_touch_event_without_coords(ev);
387                         break;
388                 case LIBINPUT_EVENT_TOUCH_FRAME:
389                         print_touch_event_without_coords(ev);
390                         break;
391                 }
392
393                 libinput_event_destroy(ev);
394                 libinput_dispatch(li);
395                 rc = 0;
396         }
397         return rc;
398 }
399
400 void
401 mainloop(struct libinput *li)
402 {
403         struct pollfd fds[2];
404         sigset_t mask;
405
406         fds[0].fd = libinput_get_fd(li);
407         fds[0].events = POLLIN;
408         fds[0].revents = 0;
409
410         sigemptyset(&mask);
411         sigaddset(&mask, SIGINT);
412
413         fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
414         fds[1].events = POLLIN;
415         fds[1].revents = 0;
416
417         if (fds[1].fd == -1 ||
418             sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
419                 fprintf(stderr, "Failed to set up signal handling (%s)\n",
420                                 strerror(errno));
421         }
422
423         /* Handle already-pending device added events */
424         if (handle_and_print_events(li))
425                 fprintf(stderr, "Expected device added events on startup but got none. "
426                                 "Maybe you don't have the right permissions?\n");
427
428         while (poll(fds, 2, -1) > -1) {
429                 if (fds[1].revents)
430                         break;
431
432                 handle_and_print_events(li);
433         }
434
435         close(fds[1].fd);
436 }
437
438 static void
439 log_handler(enum libinput_log_priority priority,
440             void *user_data,
441             const char *format,
442             va_list args)
443 {
444         vprintf(format, args);
445 }
446
447 int
448 main(int argc, char **argv)
449 {
450         struct libinput *li;
451         struct timespec tp;
452
453         if (parse_args(argc, argv))
454                 return 1;
455
456         if (verbose) {
457                 libinput_log_set_handler(log_handler, NULL);
458                 libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
459         }
460
461         if (mode == MODE_UDEV) {
462                 if (open_udev(&li))
463                         return 1;
464         } else if (mode == MODE_DEVICE) {
465                 if (open_device(&li, device))
466                         return 1;
467         } else
468                 abort();
469
470         clock_gettime(CLOCK_MONOTONIC, &tp);
471         start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
472
473         mainloop(li);
474
475         libinput_destroy(li);
476         if (udev)
477                 udev_unref(udev);
478
479         return 0;
480 }