2 * Copyright © 2014 Red Hat, Inc.
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.
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.
34 #include "linux/input.h"
35 #include <sys/ioctl.h>
36 #include <sys/signalfd.h>
44 static const char *device;
45 static const char *seat = "seat0";
46 static struct udev *udev;
48 static const uint32_t screen_width = 100;
49 static const uint32_t screen_height = 100;
50 static int verbose = 0;
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);
64 parse_args(int argc, char **argv)
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'},
77 c = getopt_long(argc, argv, "h", opts, &option_index);
82 case 'h': /* --help */
85 case 'd': /* --device */
93 case 'u': /* --udev */
98 case 'v': /* --verbose */
117 open_restricted(const char *path, int flags, void *user_data)
119 int fd = open(path, flags);
120 return fd < 0 ? -errno : fd;
124 close_restricted(int fd, void *user_data)
129 static const struct libinput_interface interface = {
130 .open_restricted = open_restricted,
131 .close_restricted = close_restricted,
135 open_udev(struct libinput **li)
139 fprintf(stderr, "Failed to initialize udev\n");
143 *li = libinput_udev_create_for_seat(&interface, NULL, udev, seat);
145 fprintf(stderr, "Failed to initialize context from udev\n");
153 open_device(struct libinput **li, const char *path)
155 struct libinput_device *device;
157 *li = libinput_path_create_context(&interface, NULL);
159 fprintf(stderr, "Failed to initialize context from %s\n", path);
163 device = libinput_path_add_device(*li, path);
165 fprintf(stderr, "Failed to initialized device %s\n", path);
166 libinput_destroy(*li);
174 print_event_header(struct libinput_event *ev)
176 struct libinput_device *dev = libinput_event_get_device(ev);
179 switch(libinput_event_get_type(ev)) {
180 case LIBINPUT_EVENT_NONE:
182 case LIBINPUT_EVENT_DEVICE_ADDED:
183 type = "DEVICE_ADDED";
185 case LIBINPUT_EVENT_DEVICE_REMOVED:
186 type = "DEVICE_REMOVED";
188 case LIBINPUT_EVENT_KEYBOARD_KEY:
189 type = "KEYBOARD_KEY";
191 case LIBINPUT_EVENT_POINTER_MOTION:
192 type = "POINTER_MOTION";
194 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
195 type = "POINTER_MOTION_ABSOLUTE";
197 case LIBINPUT_EVENT_POINTER_BUTTON:
198 type = "POINTER_BUTTON";
200 case LIBINPUT_EVENT_POINTER_AXIS:
201 type = "POINTER_AXIS";
203 case LIBINPUT_EVENT_TOUCH_DOWN:
206 case LIBINPUT_EVENT_TOUCH_MOTION:
207 type = "TOUCH_MOTION";
209 case LIBINPUT_EVENT_TOUCH_UP:
212 case LIBINPUT_EVENT_TOUCH_CANCEL:
213 type = "TOUCH_CANCEL";
215 case LIBINPUT_EVENT_TOUCH_FRAME:
216 type = "TOUCH_FRAME";
220 printf("%-7s %s ", libinput_device_get_sysname(dev), type);
224 print_event_time(uint32_t time)
226 printf("%+6.2fs ", (time - start_time) / 1000.0);
230 print_device_notify(struct libinput_event *ev)
232 struct libinput_device *dev = libinput_event_get_device(ev);
233 struct libinput_seat *seat = libinput_device_get_seat(dev);
236 libinput_seat_get_physical_name(seat),
237 libinput_seat_get_logical_name(seat));
241 print_key_event(struct libinput_event *ev)
243 struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
244 enum libinput_keyboard_key_state state;
246 print_event_time(libinput_event_keyboard_get_time(k));
247 state = libinput_event_keyboard_get_key_state(k);
249 libinput_event_keyboard_get_key(k),
250 state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
254 print_motion_event(struct libinput_event *ev)
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);
260 print_event_time(libinput_event_pointer_get_time(p));
262 printf("%6.2f/%6.2f\n", x, y);
266 print_absmotion_event(struct libinput_event *ev)
268 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
269 double x = libinput_event_pointer_get_absolute_x_transformed(
271 double y = libinput_event_pointer_get_absolute_y_transformed(
274 print_event_time(libinput_event_pointer_get_time(p));
275 printf("%6.2f/%6.2f\n", x, y);
279 print_button_event(struct libinput_event *ev)
281 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
282 enum libinput_button_state state;
284 print_event_time(libinput_event_pointer_get_time(p));
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));
294 print_axis_event(struct libinput_event *ev)
296 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
297 enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
302 case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
305 case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
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);
318 print_touch_event_without_coords(struct libinput_event *ev)
320 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
322 print_event_time(libinput_event_touch_get_time(t));
327 print_touch_event_with_coords(struct libinput_event *ev)
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);
335 print_event_time(libinput_event_touch_get_time(t));
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),
345 handle_and_print_events(struct libinput *li)
348 struct libinput_event *ev;
350 libinput_dispatch(li);
351 while ((ev = libinput_get_event(li))) {
352 print_event_header(ev);
354 switch (libinput_event_get_type(ev)) {
355 case LIBINPUT_EVENT_NONE:
357 case LIBINPUT_EVENT_DEVICE_ADDED:
358 case LIBINPUT_EVENT_DEVICE_REMOVED:
359 print_device_notify(ev);
361 case LIBINPUT_EVENT_KEYBOARD_KEY:
364 case LIBINPUT_EVENT_POINTER_MOTION:
365 print_motion_event(ev);
367 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
368 print_absmotion_event(ev);
370 case LIBINPUT_EVENT_POINTER_BUTTON:
371 print_button_event(ev);
373 case LIBINPUT_EVENT_POINTER_AXIS:
374 print_axis_event(ev);
376 case LIBINPUT_EVENT_TOUCH_DOWN:
377 print_touch_event_with_coords(ev);
379 case LIBINPUT_EVENT_TOUCH_MOTION:
380 print_touch_event_with_coords(ev);
382 case LIBINPUT_EVENT_TOUCH_UP:
383 print_touch_event_without_coords(ev);
385 case LIBINPUT_EVENT_TOUCH_CANCEL:
386 print_touch_event_without_coords(ev);
388 case LIBINPUT_EVENT_TOUCH_FRAME:
389 print_touch_event_without_coords(ev);
393 libinput_event_destroy(ev);
394 libinput_dispatch(li);
401 mainloop(struct libinput *li)
403 struct pollfd fds[2];
406 fds[0].fd = libinput_get_fd(li);
407 fds[0].events = POLLIN;
411 sigaddset(&mask, SIGINT);
413 fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
414 fds[1].events = POLLIN;
417 if (fds[1].fd == -1 ||
418 sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
419 fprintf(stderr, "Failed to set up signal handling (%s)\n",
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");
428 while (poll(fds, 2, -1) > -1) {
432 handle_and_print_events(li);
439 log_handler(enum libinput_log_priority priority,
444 vprintf(format, args);
448 main(int argc, char **argv)
453 if (parse_args(argc, argv))
457 libinput_log_set_handler(log_handler, NULL);
458 libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
461 if (mode == MODE_UDEV) {
464 } else if (mode == MODE_DEVICE) {
465 if (open_device(&li, device))
470 clock_gettime(CLOCK_MONOTONIC, &tp);
471 start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
475 libinput_destroy(li);