Replace output screen size callback with transform helpers
[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_create_from_udev(&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         *li = libinput_create_from_path(&interface, NULL, path);
155         if (!*li) {
156                 fprintf(stderr, "Failed to initialize context from %s\n", path);
157                 return 1;
158         }
159         return 0;
160 }
161
162 static void
163 print_event_header(struct libinput_event *ev)
164 {
165         struct libinput_device *dev = libinput_event_get_device(ev);
166         const char *type;
167
168         switch(libinput_event_get_type(ev)) {
169         case LIBINPUT_EVENT_NONE:
170                 abort();
171         case LIBINPUT_EVENT_DEVICE_ADDED:
172                 type = "DEVICE_ADDED";
173                 break;
174         case LIBINPUT_EVENT_DEVICE_REMOVED:
175                 type = "DEVICE_REMOVED";
176                 break;
177         case LIBINPUT_EVENT_KEYBOARD_KEY:
178                 type = "KEYBOARD_KEY";
179                 break;
180         case LIBINPUT_EVENT_POINTER_MOTION:
181                 type = "POINTER_MOTION";
182                 break;
183         case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
184                 type = "POINTER_MOTION_ABSOLUTE";
185                 break;
186         case LIBINPUT_EVENT_POINTER_BUTTON:
187                 type = "POINTER_BUTTON";
188                 break;
189         case LIBINPUT_EVENT_POINTER_AXIS:
190                 type = "POINTER_AXIS";
191                 break;
192         case LIBINPUT_EVENT_TOUCH_TOUCH:
193                 type = "TOUCH_TOUCH";
194                 break;
195         case LIBINPUT_EVENT_TOUCH_FRAME:
196                 type = "TOUCH_FRAME";
197                 break;
198         }
199
200         printf("%-7s    %s      ", libinput_device_get_sysname(dev), type);
201 }
202
203 static void
204 print_event_time(uint32_t time)
205 {
206         printf("%+6.2fs ", (time - start_time) / 1000.0);
207 }
208
209 static void
210 print_device_notify(struct libinput_event *ev)
211 {
212         struct libinput_device *dev = libinput_event_get_device(ev);
213         struct libinput_seat *seat = libinput_device_get_seat(dev);
214
215         printf("%s      %s\n",
216                libinput_seat_get_physical_name(seat),
217                libinput_seat_get_logical_name(seat));
218 }
219
220 static void
221 print_key_event(struct libinput_event *ev)
222 {
223         struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
224         enum libinput_keyboard_key_state state;
225
226         print_event_time(libinput_event_keyboard_get_time(k));
227         state = libinput_event_keyboard_get_key_state(k);
228         printf("%d %s\n",
229                libinput_event_keyboard_get_key(k),
230                state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
231 }
232
233 static void
234 print_motion_event(struct libinput_event *ev)
235 {
236         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
237         li_fixed_t x =  libinput_event_pointer_get_dx(p),
238                    y = libinput_event_pointer_get_dy(p);
239
240         print_event_time(libinput_event_pointer_get_time(p));
241
242         printf("%6.2f/%6.2f\n",
243                li_fixed_to_double(x),
244                li_fixed_to_double(y));
245 }
246
247 static void
248 print_absmotion_event(struct libinput_event *ev)
249 {
250         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
251         li_fixed_t x = libinput_event_pointer_get_absolute_x_transformed(
252                 p, screen_width);
253         li_fixed_t y = libinput_event_pointer_get_absolute_y_transformed(
254                 p, screen_height);
255
256         print_event_time(libinput_event_pointer_get_time(p));
257         printf("%6.2f/%6.2f\n",
258                li_fixed_to_double(x),
259                li_fixed_to_double(y));
260 }
261
262 static void
263 print_button_event(struct libinput_event *ev)
264 {
265         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
266         enum libinput_pointer_button_state state;
267
268         print_event_time(libinput_event_pointer_get_time(p));
269
270         state = libinput_event_pointer_get_button_state(p);
271         printf("%3d %s\n",
272                libinput_event_pointer_get_button(p),
273                state == LIBINPUT_POINTER_BUTTON_STATE_PRESSED ? "pressed" : "released");
274 }
275
276 static void
277 print_axis_event(struct libinput_event *ev)
278 {
279         struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
280         enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
281         const char *ax;
282         li_fixed_t val;
283
284         switch (axis) {
285         case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
286                 ax = "vscroll";
287                 break;
288         case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
289                 ax = "hscroll";
290                 break;
291         default:
292                 abort();
293         }
294
295         print_event_time(libinput_event_pointer_get_time(p));
296         val = libinput_event_pointer_get_axis_value(p);
297         printf("%s %.2f\n",
298                ax, li_fixed_to_double(val));
299 }
300
301 static void
302 print_touch_frame_event(struct libinput_event *ev)
303 {
304         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
305
306         print_event_time(libinput_event_touch_get_time(t));
307         printf("\n");
308 }
309
310 static void
311 print_touch_event(struct libinput_event *ev)
312 {
313         struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
314         li_fixed_t x = libinput_event_touch_get_x_transformed(t, screen_width),
315                    y = libinput_event_touch_get_y_transformed(t, screen_height);
316         const char *type;
317
318         switch (libinput_event_touch_get_touch_type(t)) {
319         case LIBINPUT_TOUCH_TYPE_DOWN: type = "down"; break;
320         case LIBINPUT_TOUCH_TYPE_UP: type = "up"; break;
321         case LIBINPUT_TOUCH_TYPE_MOTION: type = "motion"; break;
322         case LIBINPUT_TOUCH_TYPE_CANCEL: type = "cancel"; break;
323         default:
324                 abort();
325         }
326
327         print_event_time(libinput_event_touch_get_time(t));
328
329         printf("%6s %u %5.2f/%5.2f\n",
330                type,
331                libinput_event_touch_get_slot(t),
332                li_fixed_to_double(x),
333                li_fixed_to_double(y));
334 }
335
336 static int
337 handle_and_print_events(struct libinput *li)
338 {
339         int rc = -1;
340         struct libinput_event *ev;
341
342         libinput_dispatch(li);
343         while ((ev = libinput_get_event(li))) {
344                 print_event_header(ev);
345
346                 switch (libinput_event_get_type(ev)) {
347                 case LIBINPUT_EVENT_NONE:
348                         abort();
349                 case LIBINPUT_EVENT_DEVICE_ADDED:
350                 case LIBINPUT_EVENT_DEVICE_REMOVED:
351                         print_device_notify(ev);
352                         break;
353                 case LIBINPUT_EVENT_KEYBOARD_KEY:
354                         print_key_event(ev);
355                         break;
356                 case LIBINPUT_EVENT_POINTER_MOTION:
357                         print_motion_event(ev);
358                         break;
359                 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
360                         print_absmotion_event(ev);
361                         break;
362                 case LIBINPUT_EVENT_POINTER_BUTTON:
363                         print_button_event(ev);
364                         break;
365                 case LIBINPUT_EVENT_POINTER_AXIS:
366                         print_axis_event(ev);
367                         break;
368                 case LIBINPUT_EVENT_TOUCH_TOUCH:
369                         print_touch_event(ev);
370                         break;
371                 case LIBINPUT_EVENT_TOUCH_FRAME:
372                         print_touch_frame_event(ev);
373                         break;
374                 }
375
376                 libinput_event_destroy(ev);
377                 libinput_dispatch(li);
378                 rc = 0;
379         }
380         return rc;
381 }
382
383 void
384 mainloop(struct libinput *li)
385 {
386         struct pollfd fds[2];
387         sigset_t mask;
388
389         fds[0].fd = libinput_get_fd(li);
390         fds[0].events = POLLIN;
391         fds[0].revents = 0;
392
393         sigemptyset(&mask);
394         sigaddset(&mask, SIGINT);
395
396         fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
397         fds[1].events = POLLIN;
398         fds[1].revents = 0;
399
400         if (fds[1].fd == -1 ||
401             sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
402                 fprintf(stderr, "Failed to set up signal handling (%s)\n",
403                                 strerror(errno));
404         }
405
406         /* Handle already-pending device added events */
407         if (handle_and_print_events(li))
408                 fprintf(stderr, "Expected device added events on startup but got none. "
409                                 "Maybe you don't have the right permissions?\n");
410
411         while (poll(fds, 2, -1) > -1) {
412                 if (fds[1].revents)
413                         break;
414
415                 handle_and_print_events(li);
416         }
417
418         close(fds[1].fd);
419 }
420
421 int
422 main(int argc, char **argv)
423 {
424         struct libinput *li;
425         struct timespec tp;
426
427         if (parse_args(argc, argv))
428                 return 1;
429
430         if (mode == MODE_UDEV) {
431                 if (open_udev(&li))
432                         return 1;
433         } else if (mode == MODE_DEVICE) {
434                 if (open_device(&li, device))
435                         return 1;
436         } else
437                 abort();
438
439         clock_gettime(CLOCK_MONOTONIC, &tp);
440         start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
441
442         mainloop(li);
443
444         if (udev)
445                 udev_unref(udev);
446
447         return 0;
448 }