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.
33 #include <linux/input.h>
34 #include <sys/ioctl.h>
35 #include <sys/signalfd.h>
43 static const char *device;
44 static const char *seat = "seat0";
45 static struct udev *udev;
47 static const uint32_t screen_width = 100;
48 static const uint32_t screen_height = 100;
49 static int verbose = 0;
54 printf("Usage: %s [--verbose] [--udev [<seat>]|--device /dev/input/event0]\n"
55 "--verbose ....... Print debugging output.\n"
56 "--udev <seat>.... Use udev device discovery (default).\n"
57 " Specifying a seat ID is optional.\n"
58 "--device /path/to/device .... open the given device only\n",
59 program_invocation_short_name);
63 parse_args(int argc, char **argv)
68 static struct option opts[] = {
69 { "device", 1, 0, 'd' },
70 { "udev", 0, 0, 'u' },
71 { "help", 0, 0, 'h' },
72 { "verbose", 0, 0, 'v'},
76 c = getopt_long(argc, argv, "h", opts, &option_index);
81 case 'h': /* --help */
84 case 'd': /* --device */
92 case 'u': /* --udev */
97 case 'v': /* --verbose */
116 open_restricted(const char *path, int flags, void *user_data)
118 int fd = open(path, flags);
119 return fd < 0 ? -errno : fd;
123 close_restricted(int fd, void *user_data)
128 const static struct libinput_interface interface = {
129 .open_restricted = open_restricted,
130 .close_restricted = close_restricted,
134 open_udev(struct libinput **li)
138 fprintf(stderr, "Failed to initialize udev\n");
142 *li = libinput_udev_create_for_seat(&interface, NULL, udev, seat);
144 fprintf(stderr, "Failed to initialize context from udev\n");
152 open_device(struct libinput **li, const char *path)
154 struct libinput_device *device;
156 *li = libinput_path_create_context(&interface, NULL);
158 fprintf(stderr, "Failed to initialize context from %s\n", path);
162 device = libinput_path_add_device(*li, path);
164 fprintf(stderr, "Failed to initialized device %s\n", path);
165 libinput_destroy(*li);
173 print_event_header(struct libinput_event *ev)
175 struct libinput_device *dev = libinput_event_get_device(ev);
178 switch(libinput_event_get_type(ev)) {
179 case LIBINPUT_EVENT_NONE:
181 case LIBINPUT_EVENT_DEVICE_ADDED:
182 type = "DEVICE_ADDED";
184 case LIBINPUT_EVENT_DEVICE_REMOVED:
185 type = "DEVICE_REMOVED";
187 case LIBINPUT_EVENT_KEYBOARD_KEY:
188 type = "KEYBOARD_KEY";
190 case LIBINPUT_EVENT_POINTER_MOTION:
191 type = "POINTER_MOTION";
193 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
194 type = "POINTER_MOTION_ABSOLUTE";
196 case LIBINPUT_EVENT_POINTER_BUTTON:
197 type = "POINTER_BUTTON";
199 case LIBINPUT_EVENT_POINTER_AXIS:
200 type = "POINTER_AXIS";
202 case LIBINPUT_EVENT_TOUCH_DOWN:
205 case LIBINPUT_EVENT_TOUCH_MOTION:
206 type = "TOUCH_MOTION";
208 case LIBINPUT_EVENT_TOUCH_UP:
211 case LIBINPUT_EVENT_TOUCH_CANCEL:
212 type = "TOUCH_CANCEL";
214 case LIBINPUT_EVENT_TOUCH_FRAME:
215 type = "TOUCH_FRAME";
219 printf("%-7s %s ", libinput_device_get_sysname(dev), type);
223 print_event_time(uint32_t time)
225 printf("%+6.2fs ", (time - start_time) / 1000.0);
229 print_device_notify(struct libinput_event *ev)
231 struct libinput_device *dev = libinput_event_get_device(ev);
232 struct libinput_seat *seat = libinput_device_get_seat(dev);
235 libinput_seat_get_physical_name(seat),
236 libinput_seat_get_logical_name(seat));
240 print_key_event(struct libinput_event *ev)
242 struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
243 enum libinput_keyboard_key_state state;
245 print_event_time(libinput_event_keyboard_get_time(k));
246 state = libinput_event_keyboard_get_key_state(k);
248 libinput_event_keyboard_get_key(k),
249 state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
253 print_motion_event(struct libinput_event *ev)
255 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
256 li_fixed_t x = libinput_event_pointer_get_dx(p),
257 y = libinput_event_pointer_get_dy(p);
259 print_event_time(libinput_event_pointer_get_time(p));
261 printf("%6.2f/%6.2f\n",
262 li_fixed_to_double(x),
263 li_fixed_to_double(y));
267 print_absmotion_event(struct libinput_event *ev)
269 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
270 li_fixed_t x = libinput_event_pointer_get_absolute_x_transformed(
272 li_fixed_t y = libinput_event_pointer_get_absolute_y_transformed(
275 print_event_time(libinput_event_pointer_get_time(p));
276 printf("%6.2f/%6.2f\n",
277 li_fixed_to_double(x),
278 li_fixed_to_double(y));
282 print_button_event(struct libinput_event *ev)
284 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
285 enum libinput_pointer_button_state state;
287 print_event_time(libinput_event_pointer_get_time(p));
289 state = libinput_event_pointer_get_button_state(p);
290 printf("%3d %s, seat count: %u\n",
291 libinput_event_pointer_get_button(p),
292 state == LIBINPUT_POINTER_BUTTON_STATE_PRESSED ? "pressed" : "released",
293 libinput_event_pointer_get_seat_button_count(p));
297 print_axis_event(struct libinput_event *ev)
299 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
300 enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
305 case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
308 case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
315 print_event_time(libinput_event_pointer_get_time(p));
316 val = libinput_event_pointer_get_axis_value(p);
318 ax, li_fixed_to_double(val));
322 print_touch_event_without_coords(struct libinput_event *ev)
324 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
326 print_event_time(libinput_event_touch_get_time(t));
331 print_touch_event_with_coords(struct libinput_event *ev)
333 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
334 li_fixed_t x = libinput_event_touch_get_x_transformed(t, screen_width),
335 y = libinput_event_touch_get_y_transformed(t, screen_height);
337 print_event_time(libinput_event_touch_get_time(t));
339 printf("%d (%d) %5.2f/%5.2f\n",
340 libinput_event_touch_get_slot(t),
341 libinput_event_touch_get_seat_slot(t),
342 li_fixed_to_double(x),
343 li_fixed_to_double(y));
347 handle_and_print_events(struct libinput *li)
350 struct libinput_event *ev;
352 libinput_dispatch(li);
353 while ((ev = libinput_get_event(li))) {
354 print_event_header(ev);
356 switch (libinput_event_get_type(ev)) {
357 case LIBINPUT_EVENT_NONE:
359 case LIBINPUT_EVENT_DEVICE_ADDED:
360 case LIBINPUT_EVENT_DEVICE_REMOVED:
361 print_device_notify(ev);
363 case LIBINPUT_EVENT_KEYBOARD_KEY:
366 case LIBINPUT_EVENT_POINTER_MOTION:
367 print_motion_event(ev);
369 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
370 print_absmotion_event(ev);
372 case LIBINPUT_EVENT_POINTER_BUTTON:
373 print_button_event(ev);
375 case LIBINPUT_EVENT_POINTER_AXIS:
376 print_axis_event(ev);
378 case LIBINPUT_EVENT_TOUCH_DOWN:
379 print_touch_event_with_coords(ev);
381 case LIBINPUT_EVENT_TOUCH_MOTION:
382 print_touch_event_with_coords(ev);
384 case LIBINPUT_EVENT_TOUCH_UP:
385 print_touch_event_without_coords(ev);
387 case LIBINPUT_EVENT_TOUCH_CANCEL:
388 print_touch_event_without_coords(ev);
390 case LIBINPUT_EVENT_TOUCH_FRAME:
391 print_touch_event_without_coords(ev);
395 libinput_event_destroy(ev);
396 libinput_dispatch(li);
403 mainloop(struct libinput *li)
405 struct pollfd fds[2];
408 fds[0].fd = libinput_get_fd(li);
409 fds[0].events = POLLIN;
413 sigaddset(&mask, SIGINT);
415 fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
416 fds[1].events = POLLIN;
419 if (fds[1].fd == -1 ||
420 sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
421 fprintf(stderr, "Failed to set up signal handling (%s)\n",
425 /* Handle already-pending device added events */
426 if (handle_and_print_events(li))
427 fprintf(stderr, "Expected device added events on startup but got none. "
428 "Maybe you don't have the right permissions?\n");
430 while (poll(fds, 2, -1) > -1) {
434 handle_and_print_events(li);
441 log_handler(enum libinput_log_priority priority,
446 vprintf(format, args);
450 main(int argc, char **argv)
455 if (parse_args(argc, argv))
459 libinput_log_set_handler(log_handler, NULL);
460 libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
463 if (mode == MODE_UDEV) {
466 } else if (mode == MODE_DEVICE) {
467 if (open_device(&li, device))
472 clock_gettime(CLOCK_MONOTONIC, &tp);
473 start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;