Add a function to get the size of a device
[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         double w, h;
235
236         printf("%s      %s",
237                libinput_seat_get_physical_name(seat),
238                libinput_seat_get_logical_name(seat));
239
240         if (libinput_device_get_size(dev, &w, &h) == 0)
241                 printf("\tsize %.2f/%.2fmm", w, h);
242
243         printf("\n");
244 }
245
246 static void
247 print_key_event(struct libinput_event *ev)
248 {
249         struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
250         enum libinput_keyboard_key_state state;
251
252         print_event_time(libinput_event_keyboard_get_time(k));
253         state = libinput_event_keyboard_get_key_state(k);
254         printf("%d %s\n",
255                libinput_event_keyboard_get_key(k),
256                state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
257 }
258
259 static void
260 print_motion_event(struct libinput_event *ev)
261 {
262         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
263         double x = libinput_event_pointer_get_dx(p);
264         double y = libinput_event_pointer_get_dy(p);
265
266         print_event_time(libinput_event_pointer_get_time(p));
267
268         printf("%6.2f/%6.2f\n", x, y);
269 }
270
271 static void
272 print_absmotion_event(struct libinput_event *ev)
273 {
274         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
275         double x = libinput_event_pointer_get_absolute_x_transformed(
276                 p, screen_width);
277         double y = libinput_event_pointer_get_absolute_y_transformed(
278                 p, screen_height);
279
280         print_event_time(libinput_event_pointer_get_time(p));
281         printf("%6.2f/%6.2f\n", x, y);
282 }
283
284 static void
285 print_button_event(struct libinput_event *ev)
286 {
287         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
288         enum libinput_button_state state;
289
290         print_event_time(libinput_event_pointer_get_time(p));
291
292         state = libinput_event_pointer_get_button_state(p);
293         printf("%3d %s, seat count: %u\n",
294                libinput_event_pointer_get_button(p),
295                state == LIBINPUT_BUTTON_STATE_PRESSED ? "pressed" : "released",
296                libinput_event_pointer_get_seat_button_count(p));
297 }
298
299 static void
300 print_axis_event(struct libinput_event *ev)
301 {
302         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
303         enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
304         const char *ax;
305         double val;
306
307         switch (axis) {
308         case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
309                 ax = "vscroll";
310                 break;
311         case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
312                 ax = "hscroll";
313                 break;
314         default:
315                 abort();
316         }
317
318         print_event_time(libinput_event_pointer_get_time(p));
319         val = libinput_event_pointer_get_axis_value(p);
320         printf("%s %.2f\n", ax, val);
321 }
322
323 static void
324 print_touch_event_without_coords(struct libinput_event *ev)
325 {
326         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
327
328         print_event_time(libinput_event_touch_get_time(t));
329         printf("\n");
330 }
331
332 static void
333 print_touch_event_with_coords(struct libinput_event *ev)
334 {
335         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
336         double x = libinput_event_touch_get_x_transformed(t, screen_width);
337         double y = libinput_event_touch_get_y_transformed(t, screen_height);
338         double xmm = libinput_event_touch_get_x(t);
339         double ymm = libinput_event_touch_get_y(t);
340
341         print_event_time(libinput_event_touch_get_time(t));
342
343         printf("%d (%d) %5.2f/%5.2f (%5.2f/%5.2fmm)\n",
344                libinput_event_touch_get_slot(t),
345                libinput_event_touch_get_seat_slot(t),
346                x, y,
347                xmm, ymm);
348 }
349
350 static int
351 handle_and_print_events(struct libinput *li)
352 {
353         int rc = -1;
354         struct libinput_event *ev;
355
356         libinput_dispatch(li);
357         while ((ev = libinput_get_event(li))) {
358                 print_event_header(ev);
359
360                 switch (libinput_event_get_type(ev)) {
361                 case LIBINPUT_EVENT_NONE:
362                         abort();
363                 case LIBINPUT_EVENT_DEVICE_ADDED:
364                 case LIBINPUT_EVENT_DEVICE_REMOVED:
365                         print_device_notify(ev);
366                         break;
367                 case LIBINPUT_EVENT_KEYBOARD_KEY:
368                         print_key_event(ev);
369                         break;
370                 case LIBINPUT_EVENT_POINTER_MOTION:
371                         print_motion_event(ev);
372                         break;
373                 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
374                         print_absmotion_event(ev);
375                         break;
376                 case LIBINPUT_EVENT_POINTER_BUTTON:
377                         print_button_event(ev);
378                         break;
379                 case LIBINPUT_EVENT_POINTER_AXIS:
380                         print_axis_event(ev);
381                         break;
382                 case LIBINPUT_EVENT_TOUCH_DOWN:
383                         print_touch_event_with_coords(ev);
384                         break;
385                 case LIBINPUT_EVENT_TOUCH_MOTION:
386                         print_touch_event_with_coords(ev);
387                         break;
388                 case LIBINPUT_EVENT_TOUCH_UP:
389                         print_touch_event_without_coords(ev);
390                         break;
391                 case LIBINPUT_EVENT_TOUCH_CANCEL:
392                         print_touch_event_without_coords(ev);
393                         break;
394                 case LIBINPUT_EVENT_TOUCH_FRAME:
395                         print_touch_event_without_coords(ev);
396                         break;
397                 }
398
399                 libinput_event_destroy(ev);
400                 libinput_dispatch(li);
401                 rc = 0;
402         }
403         return rc;
404 }
405
406 void
407 mainloop(struct libinput *li)
408 {
409         struct pollfd fds[2];
410         sigset_t mask;
411
412         fds[0].fd = libinput_get_fd(li);
413         fds[0].events = POLLIN;
414         fds[0].revents = 0;
415
416         sigemptyset(&mask);
417         sigaddset(&mask, SIGINT);
418
419         fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
420         fds[1].events = POLLIN;
421         fds[1].revents = 0;
422
423         if (fds[1].fd == -1 ||
424             sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
425                 fprintf(stderr, "Failed to set up signal handling (%s)\n",
426                                 strerror(errno));
427         }
428
429         /* Handle already-pending device added events */
430         if (handle_and_print_events(li))
431                 fprintf(stderr, "Expected device added events on startup but got none. "
432                                 "Maybe you don't have the right permissions?\n");
433
434         while (poll(fds, 2, -1) > -1) {
435                 if (fds[1].revents)
436                         break;
437
438                 handle_and_print_events(li);
439         }
440
441         close(fds[1].fd);
442 }
443
444 static void
445 log_handler(enum libinput_log_priority priority,
446             void *user_data,
447             const char *format,
448             va_list args)
449 {
450         vprintf(format, args);
451 }
452
453 int
454 main(int argc, char **argv)
455 {
456         struct libinput *li;
457         struct timespec tp;
458
459         if (parse_args(argc, argv))
460                 return 1;
461
462         if (verbose) {
463                 libinput_log_set_handler(log_handler, NULL);
464                 libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
465         }
466
467         if (mode == MODE_UDEV) {
468                 if (open_udev(&li))
469                         return 1;
470         } else if (mode == MODE_DEVICE) {
471                 if (open_device(&li, device))
472                         return 1;
473         } else
474                 abort();
475
476         clock_gettime(CLOCK_MONOTONIC, &tp);
477         start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
478
479         mainloop(li);
480
481         libinput_destroy(li);
482         if (udev)
483                 udev_unref(udev);
484
485         return 0;
486 }