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;
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);
61 parse_args(int argc, char **argv)
66 static struct option opts[] = {
67 { "device", 1, 0, 'd' },
68 { "udev", 0, 0, 'u' },
69 { "help", 0, 0, 'h' },
73 c = getopt_long(argc, argv, "h", opts, &option_index);
78 case 'h': /* --help */
81 case 'd': /* --device */
89 case 'u': /* --udev */
110 open_restricted(const char *path, int flags, void *user_data)
112 int fd = open(path, flags);
113 int clockid = CLOCK_MONOTONIC;
115 if (fd >= 0 && ioctl(fd, EVIOCSCLOCKID, &clockid) < 0)
116 fprintf(stderr, "Changing clock on %s failed, timestamps "
117 "will be off\n", path);
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_TOUCH:
203 type = "TOUCH_TOUCH";
205 case LIBINPUT_EVENT_TOUCH_FRAME:
206 type = "TOUCH_FRAME";
210 printf("%-7s %s ", libinput_device_get_sysname(dev), type);
214 print_event_time(uint32_t time)
216 printf("%+6.2fs ", (time - start_time) / 1000.0);
220 print_device_notify(struct libinput_event *ev)
222 struct libinput_device *dev = libinput_event_get_device(ev);
223 struct libinput_seat *seat = libinput_device_get_seat(dev);
226 libinput_seat_get_physical_name(seat),
227 libinput_seat_get_logical_name(seat));
231 print_key_event(struct libinput_event *ev)
233 struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
234 enum libinput_keyboard_key_state state;
236 print_event_time(libinput_event_keyboard_get_time(k));
237 state = libinput_event_keyboard_get_key_state(k);
239 libinput_event_keyboard_get_key(k),
240 state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
244 print_motion_event(struct libinput_event *ev)
246 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
247 li_fixed_t x = libinput_event_pointer_get_dx(p),
248 y = libinput_event_pointer_get_dy(p);
250 print_event_time(libinput_event_pointer_get_time(p));
252 printf("%6.2f/%6.2f\n",
253 li_fixed_to_double(x),
254 li_fixed_to_double(y));
258 print_absmotion_event(struct libinput_event *ev)
260 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
261 li_fixed_t x = libinput_event_pointer_get_absolute_x_transformed(
263 li_fixed_t y = libinput_event_pointer_get_absolute_y_transformed(
266 print_event_time(libinput_event_pointer_get_time(p));
267 printf("%6.2f/%6.2f\n",
268 li_fixed_to_double(x),
269 li_fixed_to_double(y));
273 print_button_event(struct libinput_event *ev)
275 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
276 enum libinput_pointer_button_state state;
278 print_event_time(libinput_event_pointer_get_time(p));
280 state = libinput_event_pointer_get_button_state(p);
282 libinput_event_pointer_get_button(p),
283 state == LIBINPUT_POINTER_BUTTON_STATE_PRESSED ? "pressed" : "released");
287 print_axis_event(struct libinput_event *ev)
289 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
290 enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
295 case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
298 case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
305 print_event_time(libinput_event_pointer_get_time(p));
306 val = libinput_event_pointer_get_axis_value(p);
308 ax, li_fixed_to_double(val));
312 print_touch_frame_event(struct libinput_event *ev)
314 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
316 print_event_time(libinput_event_touch_get_time(t));
321 print_touch_event(struct libinput_event *ev)
323 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
324 li_fixed_t x = libinput_event_touch_get_x_transformed(t, screen_width),
325 y = libinput_event_touch_get_y_transformed(t, screen_height);
328 switch (libinput_event_touch_get_touch_type(t)) {
329 case LIBINPUT_TOUCH_TYPE_DOWN: type = "down"; break;
330 case LIBINPUT_TOUCH_TYPE_UP: type = "up"; break;
331 case LIBINPUT_TOUCH_TYPE_MOTION: type = "motion"; break;
332 case LIBINPUT_TOUCH_TYPE_CANCEL: type = "cancel"; break;
337 print_event_time(libinput_event_touch_get_time(t));
339 printf("%6s %d (%d) %5.2f/%5.2f\n",
341 libinput_event_touch_get_slot(t),
342 libinput_event_touch_get_seat_slot(t),
343 li_fixed_to_double(x),
344 li_fixed_to_double(y));
348 handle_and_print_events(struct libinput *li)
351 struct libinput_event *ev;
353 libinput_dispatch(li);
354 while ((ev = libinput_get_event(li))) {
355 print_event_header(ev);
357 switch (libinput_event_get_type(ev)) {
358 case LIBINPUT_EVENT_NONE:
360 case LIBINPUT_EVENT_DEVICE_ADDED:
361 case LIBINPUT_EVENT_DEVICE_REMOVED:
362 print_device_notify(ev);
364 case LIBINPUT_EVENT_KEYBOARD_KEY:
367 case LIBINPUT_EVENT_POINTER_MOTION:
368 print_motion_event(ev);
370 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
371 print_absmotion_event(ev);
373 case LIBINPUT_EVENT_POINTER_BUTTON:
374 print_button_event(ev);
376 case LIBINPUT_EVENT_POINTER_AXIS:
377 print_axis_event(ev);
379 case LIBINPUT_EVENT_TOUCH_TOUCH:
380 print_touch_event(ev);
382 case LIBINPUT_EVENT_TOUCH_FRAME:
383 print_touch_frame_event(ev);
387 libinput_event_destroy(ev);
388 libinput_dispatch(li);
395 mainloop(struct libinput *li)
397 struct pollfd fds[2];
400 fds[0].fd = libinput_get_fd(li);
401 fds[0].events = POLLIN;
405 sigaddset(&mask, SIGINT);
407 fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
408 fds[1].events = POLLIN;
411 if (fds[1].fd == -1 ||
412 sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
413 fprintf(stderr, "Failed to set up signal handling (%s)\n",
417 /* Handle already-pending device added events */
418 if (handle_and_print_events(li))
419 fprintf(stderr, "Expected device added events on startup but got none. "
420 "Maybe you don't have the right permissions?\n");
422 while (poll(fds, 2, -1) > -1) {
426 handle_and_print_events(li);
433 main(int argc, char **argv)
438 if (parse_args(argc, argv))
441 if (mode == MODE_UDEV) {
444 } else if (mode == MODE_DEVICE) {
445 if (open_device(&li, device))
450 clock_gettime(CLOCK_MONOTONIC, &tp);
451 start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;