event-debug: Display touch event seats as signed integer
[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 <linux/input.h>
34 #include <sys/ioctl.h>
35 #include <sys/signalfd.h>
36
37 #include <libinput.h>
38
39 static enum {
40         MODE_UDEV,
41         MODE_DEVICE,
42 } mode = MODE_UDEV;
43 static const char *device;
44 static const char *seat = "seat0";
45 static struct udev *udev;
46 uint32_t start_time;
47 static const uint32_t screen_width = 100;
48 static const uint32_t screen_height = 100;
49
50 static void
51 usage(void)
52 {
53         printf("Usage: %s [--udev [<seat>]|--device /dev/input/event0]\n"
54                "--udev <seat>.... Use udev device discovery (default).\n"
55                "                  Specifying a seat ID is optional.\n"
56                "--device /path/to/device .... open the given device only\n",
57                 program_invocation_short_name);
58 }
59
60 static int
61 parse_args(int argc, char **argv)
62 {
63         while (1) {
64                 int c;
65                 int option_index = 0;
66                 static struct option opts[] = {
67                         { "device", 1, 0, 'd' },
68                         { "udev", 0, 0, 'u' },
69                         { "help", 0, 0, 'h' },
70                         { 0, 0, 0, 0}
71                 };
72
73                 c = getopt_long(argc, argv, "h", opts, &option_index);
74                 if (c == -1)
75                         break;
76
77                 switch(c) {
78                         case 'h': /* --help */
79                                 usage();
80                                 exit(0);
81                         case 'd': /* --device */
82                                 mode = MODE_DEVICE;
83                                 if (!optarg) {
84                                         usage();
85                                         return 1;
86                                 }
87                                 device = optarg;
88                                 break;
89                         case 'u': /* --udev */
90                                 mode = MODE_UDEV;
91                                 if (optarg)
92                                         seat = optarg;
93                                 break;
94                         default:
95                                 usage();
96                                 return 1;
97                 }
98
99         }
100
101         if (optind < argc) {
102                 usage();
103                 return 1;
104         }
105
106         return 0;
107 }
108
109 static int
110 open_restricted(const char *path, int flags, void *user_data)
111 {
112         int fd = open(path, flags);
113         int clockid = CLOCK_MONOTONIC;
114
115         if (fd >= 0 && ioctl(fd, EVIOCSCLOCKID, &clockid) < 0)
116                 fprintf(stderr, "Changing clock on %s failed, timestamps "
117                                 "will be off\n", path);
118
119         return fd < 0 ? -errno : fd;
120 }
121
122 static void
123 close_restricted(int fd, void *user_data)
124 {
125         close(fd);
126 }
127
128 const static struct libinput_interface interface = {
129         .open_restricted = open_restricted,
130         .close_restricted = close_restricted,
131 };
132
133 static int
134 open_udev(struct libinput **li)
135 {
136         udev = udev_new();
137         if (!udev) {
138                 fprintf(stderr, "Failed to initialize udev\n");
139                 return 1;
140         }
141
142         *li = libinput_udev_create_for_seat(&interface, NULL, udev, seat);
143         if (!*li) {
144                 fprintf(stderr, "Failed to initialize context from udev\n");
145                 return 1;
146         }
147
148         return 0;
149 }
150
151 static int
152 open_device(struct libinput **li, const char *path)
153 {
154         struct libinput_device *device;
155
156         *li = libinput_path_create_context(&interface, NULL);
157         if (!*li) {
158                 fprintf(stderr, "Failed to initialize context from %s\n", path);
159                 return 1;
160         }
161
162         device = libinput_path_add_device(*li, path);
163         if (!device) {
164                 fprintf(stderr, "Failed to initialized device %s\n", path);
165                 libinput_destroy(*li);
166                 return 1;
167         }
168
169         return 0;
170 }
171
172 static void
173 print_event_header(struct libinput_event *ev)
174 {
175         struct libinput_device *dev = libinput_event_get_device(ev);
176         const char *type;
177
178         switch(libinput_event_get_type(ev)) {
179         case LIBINPUT_EVENT_NONE:
180                 abort();
181         case LIBINPUT_EVENT_DEVICE_ADDED:
182                 type = "DEVICE_ADDED";
183                 break;
184         case LIBINPUT_EVENT_DEVICE_REMOVED:
185                 type = "DEVICE_REMOVED";
186                 break;
187         case LIBINPUT_EVENT_KEYBOARD_KEY:
188                 type = "KEYBOARD_KEY";
189                 break;
190         case LIBINPUT_EVENT_POINTER_MOTION:
191                 type = "POINTER_MOTION";
192                 break;
193         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
194                 type = "POINTER_MOTION_ABSOLUTE";
195                 break;
196         case LIBINPUT_EVENT_POINTER_BUTTON:
197                 type = "POINTER_BUTTON";
198                 break;
199         case LIBINPUT_EVENT_POINTER_AXIS:
200                 type = "POINTER_AXIS";
201                 break;
202         case LIBINPUT_EVENT_TOUCH_TOUCH:
203                 type = "TOUCH_TOUCH";
204                 break;
205         case LIBINPUT_EVENT_TOUCH_FRAME:
206                 type = "TOUCH_FRAME";
207                 break;
208         }
209
210         printf("%-7s    %s      ", libinput_device_get_sysname(dev), type);
211 }
212
213 static void
214 print_event_time(uint32_t time)
215 {
216         printf("%+6.2fs ", (time - start_time) / 1000.0);
217 }
218
219 static void
220 print_device_notify(struct libinput_event *ev)
221 {
222         struct libinput_device *dev = libinput_event_get_device(ev);
223         struct libinput_seat *seat = libinput_device_get_seat(dev);
224
225         printf("%s      %s\n",
226                libinput_seat_get_physical_name(seat),
227                libinput_seat_get_logical_name(seat));
228 }
229
230 static void
231 print_key_event(struct libinput_event *ev)
232 {
233         struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
234         enum libinput_keyboard_key_state state;
235
236         print_event_time(libinput_event_keyboard_get_time(k));
237         state = libinput_event_keyboard_get_key_state(k);
238         printf("%d %s\n",
239                libinput_event_keyboard_get_key(k),
240                state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
241 }
242
243 static void
244 print_motion_event(struct libinput_event *ev)
245 {
246         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
247         li_fixed_t x =  libinput_event_pointer_get_dx(p),
248                    y = libinput_event_pointer_get_dy(p);
249
250         print_event_time(libinput_event_pointer_get_time(p));
251
252         printf("%6.2f/%6.2f\n",
253                li_fixed_to_double(x),
254                li_fixed_to_double(y));
255 }
256
257 static void
258 print_absmotion_event(struct libinput_event *ev)
259 {
260         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
261         li_fixed_t x = libinput_event_pointer_get_absolute_x_transformed(
262                 p, screen_width);
263         li_fixed_t y = libinput_event_pointer_get_absolute_y_transformed(
264                 p, screen_height);
265
266         print_event_time(libinput_event_pointer_get_time(p));
267         printf("%6.2f/%6.2f\n",
268                li_fixed_to_double(x),
269                li_fixed_to_double(y));
270 }
271
272 static void
273 print_button_event(struct libinput_event *ev)
274 {
275         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
276         enum libinput_pointer_button_state state;
277
278         print_event_time(libinput_event_pointer_get_time(p));
279
280         state = libinput_event_pointer_get_button_state(p);
281         printf("%3d %s\n",
282                libinput_event_pointer_get_button(p),
283                state == LIBINPUT_POINTER_BUTTON_STATE_PRESSED ? "pressed" : "released");
284 }
285
286 static void
287 print_axis_event(struct libinput_event *ev)
288 {
289         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
290         enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
291         const char *ax;
292         li_fixed_t val;
293
294         switch (axis) {
295         case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
296                 ax = "vscroll";
297                 break;
298         case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
299                 ax = "hscroll";
300                 break;
301         default:
302                 abort();
303         }
304
305         print_event_time(libinput_event_pointer_get_time(p));
306         val = libinput_event_pointer_get_axis_value(p);
307         printf("%s %.2f\n",
308                ax, li_fixed_to_double(val));
309 }
310
311 static void
312 print_touch_frame_event(struct libinput_event *ev)
313 {
314         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
315
316         print_event_time(libinput_event_touch_get_time(t));
317         printf("\n");
318 }
319
320 static void
321 print_touch_event(struct libinput_event *ev)
322 {
323         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
324         li_fixed_t x = libinput_event_touch_get_x_transformed(t, screen_width),
325                    y = libinput_event_touch_get_y_transformed(t, screen_height);
326         const char *type;
327
328         switch (libinput_event_touch_get_touch_type(t)) {
329         case LIBINPUT_TOUCH_TYPE_DOWN: type = "down"; break;
330         case LIBINPUT_TOUCH_TYPE_UP: type = "up"; break;
331         case LIBINPUT_TOUCH_TYPE_MOTION: type = "motion"; break;
332         case LIBINPUT_TOUCH_TYPE_CANCEL: type = "cancel"; break;
333         default:
334                 abort();
335         }
336
337         print_event_time(libinput_event_touch_get_time(t));
338
339         printf("%6s %d %5.2f/%5.2f\n",
340                type,
341                libinput_event_touch_get_slot(t),
342                li_fixed_to_double(x),
343                li_fixed_to_double(y));
344 }
345
346 static int
347 handle_and_print_events(struct libinput *li)
348 {
349         int rc = -1;
350         struct libinput_event *ev;
351
352         libinput_dispatch(li);
353         while ((ev = libinput_get_event(li))) {
354                 print_event_header(ev);
355
356                 switch (libinput_event_get_type(ev)) {
357                 case LIBINPUT_EVENT_NONE:
358                         abort();
359                 case LIBINPUT_EVENT_DEVICE_ADDED:
360                 case LIBINPUT_EVENT_DEVICE_REMOVED:
361                         print_device_notify(ev);
362                         break;
363                 case LIBINPUT_EVENT_KEYBOARD_KEY:
364                         print_key_event(ev);
365                         break;
366                 case LIBINPUT_EVENT_POINTER_MOTION:
367                         print_motion_event(ev);
368                         break;
369                 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
370                         print_absmotion_event(ev);
371                         break;
372                 case LIBINPUT_EVENT_POINTER_BUTTON:
373                         print_button_event(ev);
374                         break;
375                 case LIBINPUT_EVENT_POINTER_AXIS:
376                         print_axis_event(ev);
377                         break;
378                 case LIBINPUT_EVENT_TOUCH_TOUCH:
379                         print_touch_event(ev);
380                         break;
381                 case LIBINPUT_EVENT_TOUCH_FRAME:
382                         print_touch_frame_event(ev);
383                         break;
384                 }
385
386                 libinput_event_destroy(ev);
387                 libinput_dispatch(li);
388                 rc = 0;
389         }
390         return rc;
391 }
392
393 void
394 mainloop(struct libinput *li)
395 {
396         struct pollfd fds[2];
397         sigset_t mask;
398
399         fds[0].fd = libinput_get_fd(li);
400         fds[0].events = POLLIN;
401         fds[0].revents = 0;
402
403         sigemptyset(&mask);
404         sigaddset(&mask, SIGINT);
405
406         fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
407         fds[1].events = POLLIN;
408         fds[1].revents = 0;
409
410         if (fds[1].fd == -1 ||
411             sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
412                 fprintf(stderr, "Failed to set up signal handling (%s)\n",
413                                 strerror(errno));
414         }
415
416         /* Handle already-pending device added events */
417         if (handle_and_print_events(li))
418                 fprintf(stderr, "Expected device added events on startup but got none. "
419                                 "Maybe you don't have the right permissions?\n");
420
421         while (poll(fds, 2, -1) > -1) {
422                 if (fds[1].revents)
423                         break;
424
425                 handle_and_print_events(li);
426         }
427
428         close(fds[1].fd);
429 }
430
431 int
432 main(int argc, char **argv)
433 {
434         struct libinput *li;
435         struct timespec tp;
436
437         if (parse_args(argc, argv))
438                 return 1;
439
440         if (mode == MODE_UDEV) {
441                 if (open_udev(&li))
442                         return 1;
443         } else if (mode == MODE_DEVICE) {
444                 if (open_device(&li, device))
445                         return 1;
446         } else
447                 abort();
448
449         clock_gettime(CLOCK_MONOTONIC, &tp);
450         start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
451
452         mainloop(li);
453
454         if (udev)
455                 udev_unref(udev);
456
457         return 0;
458 }