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_create_from_udev(&interface, NULL, udev, seat);
144 fprintf(stderr, "Failed to initialize context from udev\n");
152 open_device(struct libinput **li, const char *path)
154 *li = libinput_create_from_path(&interface, NULL, path);
156 fprintf(stderr, "Failed to initialize context from %s\n", path);
163 print_event_header(struct libinput_event *ev)
165 struct libinput_device *dev = libinput_event_get_device(ev);
168 switch(libinput_event_get_type(ev)) {
169 case LIBINPUT_EVENT_NONE:
171 case LIBINPUT_EVENT_DEVICE_ADDED:
172 type = "DEVICE_ADDED";
174 case LIBINPUT_EVENT_DEVICE_REMOVED:
175 type = "DEVICE_REMOVED";
177 case LIBINPUT_EVENT_KEYBOARD_KEY:
178 type = "KEYBOARD_KEY";
180 case LIBINPUT_EVENT_POINTER_MOTION:
181 type = "POINTER_MOTION";
183 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
184 type = "POINTER_MOTION_ABSOLUTE";
186 case LIBINPUT_EVENT_POINTER_BUTTON:
187 type = "POINTER_BUTTON";
189 case LIBINPUT_EVENT_POINTER_AXIS:
190 type = "POINTER_AXIS";
192 case LIBINPUT_EVENT_TOUCH_TOUCH:
193 type = "TOUCH_TOUCH";
195 case LIBINPUT_EVENT_TOUCH_FRAME:
196 type = "TOUCH_FRAME";
200 printf("%-7s %s ", libinput_device_get_sysname(dev), type);
204 print_event_time(uint32_t time)
206 printf("%+6.2fs ", (time - start_time) / 1000.0);
210 print_device_notify(struct libinput_event *ev)
212 struct libinput_device *dev = libinput_event_get_device(ev);
213 struct libinput_seat *seat = libinput_device_get_seat(dev);
216 libinput_seat_get_physical_name(seat),
217 libinput_seat_get_logical_name(seat));
221 print_key_event(struct libinput_event *ev)
223 struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
224 enum libinput_keyboard_key_state state;
226 print_event_time(libinput_event_keyboard_get_time(k));
227 state = libinput_event_keyboard_get_key_state(k);
229 libinput_event_keyboard_get_key(k),
230 state == LIBINPUT_KEYBOARD_KEY_STATE_PRESSED ? "pressed" : "released");
234 print_motion_event(struct libinput_event *ev)
236 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
237 li_fixed_t x = libinput_event_pointer_get_dx(p),
238 y = libinput_event_pointer_get_dy(p);
240 print_event_time(libinput_event_pointer_get_time(p));
242 printf("%6.2f/%6.2f\n",
243 li_fixed_to_double(x),
244 li_fixed_to_double(y));
248 print_absmotion_event(struct libinput_event *ev)
250 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
251 li_fixed_t x = libinput_event_pointer_get_absolute_x_transformed(
253 li_fixed_t y = libinput_event_pointer_get_absolute_y_transformed(
256 print_event_time(libinput_event_pointer_get_time(p));
257 printf("%6.2f/%6.2f\n",
258 li_fixed_to_double(x),
259 li_fixed_to_double(y));
263 print_button_event(struct libinput_event *ev)
265 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
266 enum libinput_pointer_button_state state;
268 print_event_time(libinput_event_pointer_get_time(p));
270 state = libinput_event_pointer_get_button_state(p);
272 libinput_event_pointer_get_button(p),
273 state == LIBINPUT_POINTER_BUTTON_STATE_PRESSED ? "pressed" : "released");
277 print_axis_event(struct libinput_event *ev)
279 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
280 enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
285 case LIBINPUT_POINTER_AXIS_VERTICAL_SCROLL:
288 case LIBINPUT_POINTER_AXIS_HORIZONTAL_SCROLL:
295 print_event_time(libinput_event_pointer_get_time(p));
296 val = libinput_event_pointer_get_axis_value(p);
298 ax, li_fixed_to_double(val));
302 print_touch_frame_event(struct libinput_event *ev)
304 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
306 print_event_time(libinput_event_touch_get_time(t));
311 print_touch_event(struct libinput_event *ev)
313 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
314 li_fixed_t x = libinput_event_touch_get_x_transformed(t, screen_width),
315 y = libinput_event_touch_get_y_transformed(t, screen_height);
318 switch (libinput_event_touch_get_touch_type(t)) {
319 case LIBINPUT_TOUCH_TYPE_DOWN: type = "down"; break;
320 case LIBINPUT_TOUCH_TYPE_UP: type = "up"; break;
321 case LIBINPUT_TOUCH_TYPE_MOTION: type = "motion"; break;
322 case LIBINPUT_TOUCH_TYPE_CANCEL: type = "cancel"; break;
327 print_event_time(libinput_event_touch_get_time(t));
329 printf("%6s %u %5.2f/%5.2f\n",
331 libinput_event_touch_get_slot(t),
332 li_fixed_to_double(x),
333 li_fixed_to_double(y));
337 handle_and_print_events(struct libinput *li)
340 struct libinput_event *ev;
342 libinput_dispatch(li);
343 while ((ev = libinput_get_event(li))) {
344 print_event_header(ev);
346 switch (libinput_event_get_type(ev)) {
347 case LIBINPUT_EVENT_NONE:
349 case LIBINPUT_EVENT_DEVICE_ADDED:
350 case LIBINPUT_EVENT_DEVICE_REMOVED:
351 print_device_notify(ev);
353 case LIBINPUT_EVENT_KEYBOARD_KEY:
356 case LIBINPUT_EVENT_POINTER_MOTION:
357 print_motion_event(ev);
359 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
360 print_absmotion_event(ev);
362 case LIBINPUT_EVENT_POINTER_BUTTON:
363 print_button_event(ev);
365 case LIBINPUT_EVENT_POINTER_AXIS:
366 print_axis_event(ev);
368 case LIBINPUT_EVENT_TOUCH_TOUCH:
369 print_touch_event(ev);
371 case LIBINPUT_EVENT_TOUCH_FRAME:
372 print_touch_frame_event(ev);
376 libinput_event_destroy(ev);
377 libinput_dispatch(li);
384 mainloop(struct libinput *li)
386 struct pollfd fds[2];
389 fds[0].fd = libinput_get_fd(li);
390 fds[0].events = POLLIN;
394 sigaddset(&mask, SIGINT);
396 fds[1].fd = signalfd(-1, &mask, SFD_NONBLOCK);
397 fds[1].events = POLLIN;
400 if (fds[1].fd == -1 ||
401 sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
402 fprintf(stderr, "Failed to set up signal handling (%s)\n",
406 /* Handle already-pending device added events */
407 if (handle_and_print_events(li))
408 fprintf(stderr, "Expected device added events on startup but got none. "
409 "Maybe you don't have the right permissions?\n");
411 while (poll(fds, 2, -1) > -1) {
415 handle_and_print_events(li);
422 main(int argc, char **argv)
427 if (parse_args(argc, argv))
430 if (mode == MODE_UDEV) {
433 } else if (mode == MODE_DEVICE) {
434 if (open_device(&li, device))
439 clock_gettime(CLOCK_MONOTONIC, &tp);
440 start_time = tp.tv_sec * 1000 + tp.tv_nsec / 1000000;