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_pointer_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_POINTER_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);
333 print_event_time(libinput_event_touch_get_time(t));
335 printf("%d (%d) %5.2f/%5.2f\n",
336 libinput_event_touch_get_slot(t),
337 libinput_event_touch_get_seat_slot(t),
342 handle_and_print_events(struct libinput *li)
345 struct libinput_event *ev;
347 libinput_dispatch(li);
348 while ((ev = libinput_get_event(li))) {
349 print_event_header(ev);
351 switch (libinput_event_get_type(ev)) {
352 case LIBINPUT_EVENT_NONE:
354 case LIBINPUT_EVENT_DEVICE_ADDED:
355 case LIBINPUT_EVENT_DEVICE_REMOVED:
356 print_device_notify(ev);
358 case LIBINPUT_EVENT_KEYBOARD_KEY:
361 case LIBINPUT_EVENT_POINTER_MOTION:
362 print_motion_event(ev);
364 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
365 print_absmotion_event(ev);
367 case LIBINPUT_EVENT_POINTER_BUTTON:
368 print_button_event(ev);
370 case LIBINPUT_EVENT_POINTER_AXIS:
371 print_axis_event(ev);
373 case LIBINPUT_EVENT_TOUCH_DOWN:
374 print_touch_event_with_coords(ev);
376 case LIBINPUT_EVENT_TOUCH_MOTION:
377 print_touch_event_with_coords(ev);
379 case LIBINPUT_EVENT_TOUCH_UP:
380 print_touch_event_without_coords(ev);
382 case LIBINPUT_EVENT_TOUCH_CANCEL:
383 print_touch_event_without_coords(ev);
385 case LIBINPUT_EVENT_TOUCH_FRAME:
386 print_touch_event_without_coords(ev);
390 libinput_event_destroy(ev);
391 libinput_dispatch(li);
398 mainloop(struct libinput *li)
400 struct pollfd fds[2];
403 fds[0].fd = libinput_get_fd(li);
404 fds[0].events = POLLIN;
408 sigaddset(&mask, SIGINT);
410 fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
411 fds[1].events = POLLIN;
414 if (fds[1].fd == -1 ||
415 sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
416 fprintf(stderr, "Failed to set up signal handling (%s)\n",
420 /* Handle already-pending device added events */
421 if (handle_and_print_events(li))
422 fprintf(stderr, "Expected device added events on startup but got none. "
423 "Maybe you don't have the right permissions?\n");
425 while (poll(fds, 2, -1) > -1) {
429 handle_and_print_events(li);
436 log_handler(enum libinput_log_priority priority,
441 vprintf(format, args);
445 main(int argc, char **argv)
450 if (parse_args(argc, argv))
454 libinput_log_set_handler(log_handler, NULL);
455 libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
458 if (mode == MODE_UDEV) {
461 } else if (mode == MODE_DEVICE) {
462 if (open_device(&li, device))
467 clock_gettime(CLOCK_MONOTONIC, &tp);
468 start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
472 libinput_destroy(li);