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);
237 libinput_seat_get_physical_name(seat),
238 libinput_seat_get_logical_name(seat));
240 if (libinput_device_get_size(dev, &w, &h) == 0)
241 printf("\tsize %.2f/%.2fmm", w, h);
247 print_key_event(struct libinput_event *ev)
249 struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
250 enum libinput_keyboard_key_state state;
252 print_event_time(libinput_event_keyboard_get_time(k));
253 state = libinput_event_keyboard_get_key_state(k);
255 libinput_event_keyboard_get_key(k),
256 state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
260 print_motion_event(struct libinput_event *ev)
262 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
263 double x = libinput_event_pointer_get_dx(p);
264 double y = libinput_event_pointer_get_dy(p);
266 print_event_time(libinput_event_pointer_get_time(p));
268 printf("%6.2f/%6.2f\n", x, y);
272 print_absmotion_event(struct libinput_event *ev)
274 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
275 double x = libinput_event_pointer_get_absolute_x_transformed(
277 double y = libinput_event_pointer_get_absolute_y_transformed(
280 print_event_time(libinput_event_pointer_get_time(p));
281 printf("%6.2f/%6.2f\n", x, y);
285 print_button_event(struct libinput_event *ev)
287 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
288 enum libinput_button_state state;
290 print_event_time(libinput_event_pointer_get_time(p));
292 state = libinput_event_pointer_get_button_state(p);
293 printf("%3d %s, seat count: %u\n",
294 libinput_event_pointer_get_button(p),
295 state == LIBINPUT_BUTTON_STATE_PRESSED ? "pressed" : "released",
296 libinput_event_pointer_get_seat_button_count(p));
300 print_axis_event(struct libinput_event *ev)
302 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
303 enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
308 case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
311 case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
318 print_event_time(libinput_event_pointer_get_time(p));
319 val = libinput_event_pointer_get_axis_value(p);
320 printf("%s %.2f\n", ax, val);
324 print_touch_event_without_coords(struct libinput_event *ev)
326 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
328 print_event_time(libinput_event_touch_get_time(t));
333 print_touch_event_with_coords(struct libinput_event *ev)
335 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
336 double x = libinput_event_touch_get_x_transformed(t, screen_width);
337 double y = libinput_event_touch_get_y_transformed(t, screen_height);
338 double xmm = libinput_event_touch_get_x(t);
339 double ymm = libinput_event_touch_get_y(t);
341 print_event_time(libinput_event_touch_get_time(t));
343 printf("%d (%d) %5.2f/%5.2f (%5.2f/%5.2fmm)\n",
344 libinput_event_touch_get_slot(t),
345 libinput_event_touch_get_seat_slot(t),
351 handle_and_print_events(struct libinput *li)
354 struct libinput_event *ev;
356 libinput_dispatch(li);
357 while ((ev = libinput_get_event(li))) {
358 print_event_header(ev);
360 switch (libinput_event_get_type(ev)) {
361 case LIBINPUT_EVENT_NONE:
363 case LIBINPUT_EVENT_DEVICE_ADDED:
364 case LIBINPUT_EVENT_DEVICE_REMOVED:
365 print_device_notify(ev);
367 case LIBINPUT_EVENT_KEYBOARD_KEY:
370 case LIBINPUT_EVENT_POINTER_MOTION:
371 print_motion_event(ev);
373 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
374 print_absmotion_event(ev);
376 case LIBINPUT_EVENT_POINTER_BUTTON:
377 print_button_event(ev);
379 case LIBINPUT_EVENT_POINTER_AXIS:
380 print_axis_event(ev);
382 case LIBINPUT_EVENT_TOUCH_DOWN:
383 print_touch_event_with_coords(ev);
385 case LIBINPUT_EVENT_TOUCH_MOTION:
386 print_touch_event_with_coords(ev);
388 case LIBINPUT_EVENT_TOUCH_UP:
389 print_touch_event_without_coords(ev);
391 case LIBINPUT_EVENT_TOUCH_CANCEL:
392 print_touch_event_without_coords(ev);
394 case LIBINPUT_EVENT_TOUCH_FRAME:
395 print_touch_event_without_coords(ev);
399 libinput_event_destroy(ev);
400 libinput_dispatch(li);
407 mainloop(struct libinput *li)
409 struct pollfd fds[2];
412 fds[0].fd = libinput_get_fd(li);
413 fds[0].events = POLLIN;
417 sigaddset(&mask, SIGINT);
419 fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
420 fds[1].events = POLLIN;
423 if (fds[1].fd == -1 ||
424 sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
425 fprintf(stderr, "Failed to set up signal handling (%s)\n",
429 /* Handle already-pending device added events */
430 if (handle_and_print_events(li))
431 fprintf(stderr, "Expected device added events on startup but got none. "
432 "Maybe you don't have the right permissions?\n");
434 while (poll(fds, 2, -1) > -1) {
438 handle_and_print_events(li);
445 log_handler(enum libinput_log_priority priority,
450 vprintf(format, args);
454 main(int argc, char **argv)
459 if (parse_args(argc, argv))
463 libinput_log_set_handler(log_handler, NULL);
464 libinput_log_set_priority(LIBINPUT_LOG_PRIORITY_DEBUG);
467 if (mode == MODE_UDEV) {
470 } else if (mode == MODE_DEVICE) {
471 if (open_device(&li, device))
476 clock_gettime(CLOCK_MONOTONIC, &tp);
477 start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
481 libinput_destroy(li);