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.
25 #include <linux/input.h>
41 #include <libinput-util.h>
43 #define clip(val_, min_, max_) min((max_), max((min_), (val_)))
53 int width, height; /* of window */
61 /* scroll bar positions */
66 struct touch touches[32];
68 /* l/m/r mouse buttons */
73 error(const char *fmt, ...)
76 fprintf(stderr, "error: ");
79 vfprintf(stderr, fmt, args);
86 msg(const char *fmt, ...)
99 printf("%s [path/to/device]\n", program_invocation_short_name);
103 draw(GtkWidget *widget, cairo_t *cr, gpointer data)
105 struct window *w = data;
108 cairo_set_source_rgb(cr, 1, 1, 1);
109 cairo_rectangle(cr, 0, 0, w->width, w->height);
112 /* draw pointer sprite */
113 cairo_set_source_rgb(cr, 0, 0, 0);
115 cairo_move_to(cr, w->x, w->y);
116 cairo_rel_line_to(cr, 10, 15);
117 cairo_rel_line_to(cr, -10, 0);
118 cairo_rel_line_to(cr, 0, -15);
122 /* draw scroll bars */
123 cairo_set_source_rgb(cr, .4, .8, 0);
126 cairo_rectangle(cr, w->vx - 10, w->vy - 20, 20, 40);
127 cairo_rectangle(cr, w->hx - 20, w->hy - 10, 40, 20);
132 cairo_set_source_rgb(cr, .8, .2, .2);
134 ARRAY_FOR_EACH(w->touches, t) {
136 cairo_arc(cr, t->x, t->y, 10, 0, 2 * M_PI);
142 cairo_set_source_rgb(cr, .2, .4, .8);
145 cairo_arc(cr, w->absx, w->absy, 10, 0, 2 * M_PI);
151 if (w->l || w->m || w->r) {
152 cairo_set_source_rgb(cr, .2, .8, .8);
154 cairo_rectangle(cr, w->width/2 - 100, w->height - 200, 70, 30);
156 cairo_rectangle(cr, w->width/2 - 20, w->height - 200, 40, 30);
158 cairo_rectangle(cr, w->width/2 + 30, w->height - 200, 70, 30);
162 cairo_set_source_rgb(cr, 0, 0, 0);
163 cairo_rectangle(cr, w->width/2 - 100, w->height - 200, 70, 30);
164 cairo_rectangle(cr, w->width/2 - 20, w->height - 200, 40, 30);
165 cairo_rectangle(cr, w->width/2 + 30, w->height - 200, 70, 30);
173 map_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
175 struct window *w = data;
177 gtk_window_get_size(GTK_WINDOW(widget), &w->width, &w->height);
187 g_signal_connect(G_OBJECT(w->area), "draw", G_CALLBACK(draw), w);
189 gdk_window_set_cursor(gtk_widget_get_window(w->win),
190 gdk_cursor_new(GDK_BLANK_CURSOR));
194 window_init(struct window *w)
196 memset(w, 0, sizeof(*w));
198 w->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
199 gtk_widget_set_events(w->win, 0);
200 gtk_window_set_title(GTK_WINDOW(w->win), "libinput debugging tool");
201 gtk_window_set_default_size(GTK_WINDOW(w->win), 1024, 768);
202 gtk_window_maximize(GTK_WINDOW(w->win));
203 gtk_window_set_resizable(GTK_WINDOW(w->win), TRUE);
204 gtk_widget_realize(w->win);
205 g_signal_connect(G_OBJECT(w->win), "map-event", G_CALLBACK(map_event_cb), w);
206 g_signal_connect(G_OBJECT(w->win), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
208 w->area = gtk_drawing_area_new();
209 gtk_widget_set_events(w->area, 0);
210 gtk_container_add(GTK_CONTAINER(w->win), w->area);
211 gtk_widget_show_all(w->win);
215 handle_event_device_notify(struct libinput_event *ev)
217 struct libinput_device *dev = libinput_event_get_device(ev);
220 if (libinput_event_get_type(ev) == LIBINPUT_EVENT_DEVICE_ADDED)
225 msg("%s %s\n", libinput_device_get_sysname(dev), type);
229 handle_event_motion(struct libinput_event *ev, struct window *w)
231 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
232 double dx = libinput_event_pointer_get_dx(p),
233 dy = libinput_event_pointer_get_dy(p);
237 w->x = clip(w->x, 0.0, w->width);
238 w->y = clip(w->y, 0.0, w->height);
242 handle_event_absmotion(struct libinput_event *ev, struct window *w)
244 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
245 double x = libinput_event_pointer_get_absolute_x_transformed(p, w->width),
246 y = libinput_event_pointer_get_absolute_y_transformed(p, w->height);
253 handle_event_touch(struct libinput_event *ev, struct window *w)
255 struct libinput_event_touch *t = libinput_event_get_touch_event(ev);
256 int slot = libinput_event_touch_get_seat_slot(t);
260 if (slot == -1 || slot >= (int) ARRAY_LENGTH(w->touches))
263 touch = &w->touches[slot];
265 if (libinput_event_get_type(ev) == LIBINPUT_EVENT_TOUCH_UP) {
270 x = libinput_event_touch_get_x_transformed(t, w->width),
271 y = libinput_event_touch_get_y_transformed(t, w->height);
279 handle_event_axis(struct libinput_event *ev, struct window *w)
281 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
282 enum libinput_pointer_axis axis = libinput_event_pointer_get_axis(p);
283 double v = libinput_event_pointer_get_axis_value(p);
286 case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL:
288 w->vy = clip(w->vy, 0, w->height);
290 case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL:
292 w->hx = clip(w->hx, 0, w->width);
300 handle_event_keyboard(struct libinput_event *ev, struct window *w)
302 struct libinput_event_keyboard *k = libinput_event_get_keyboard_event(ev);
304 if (libinput_event_keyboard_get_key(k) == KEY_ESC)
311 handle_event_button(struct libinput_event *ev, struct window *w)
313 struct libinput_event_pointer *p = libinput_event_get_pointer_event(ev);
314 unsigned int button = libinput_event_pointer_get_button(p);
317 is_press = libinput_event_pointer_get_button_state(p) == LIBINPUT_BUTTON_STATE_PRESSED;
334 handle_event_libinput(GIOChannel *source, GIOCondition condition, gpointer data)
336 struct libinput *li = data;
337 struct window *w = libinput_get_user_data(li);
338 struct libinput_event *ev;
340 libinput_dispatch(li);
342 while ((ev = libinput_get_event(li))) {
343 switch (libinput_event_get_type(ev)) {
344 case LIBINPUT_EVENT_NONE:
346 case LIBINPUT_EVENT_DEVICE_ADDED:
347 case LIBINPUT_EVENT_DEVICE_REMOVED:
348 handle_event_device_notify(ev);
350 case LIBINPUT_EVENT_POINTER_MOTION:
351 handle_event_motion(ev, w);
353 case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
354 handle_event_absmotion(ev, w);
356 case LIBINPUT_EVENT_TOUCH_DOWN:
357 case LIBINPUT_EVENT_TOUCH_MOTION:
358 case LIBINPUT_EVENT_TOUCH_UP:
359 handle_event_touch(ev, w);
361 case LIBINPUT_EVENT_POINTER_AXIS:
362 handle_event_axis(ev, w);
364 case LIBINPUT_EVENT_TOUCH_CANCEL:
365 case LIBINPUT_EVENT_TOUCH_FRAME:
367 case LIBINPUT_EVENT_POINTER_BUTTON:
368 handle_event_button(ev, w);
370 case LIBINPUT_EVENT_KEYBOARD_KEY:
371 if (handle_event_keyboard(ev, w)) {
372 libinput_event_destroy(ev);
379 libinput_event_destroy(ev);
380 libinput_dispatch(li);
382 gtk_widget_queue_draw(w->area);
388 sockets_init(struct libinput *li)
390 GIOChannel *c = g_io_channel_unix_new(libinput_get_fd(li));
392 g_io_channel_set_encoding(c, NULL, NULL);
393 g_io_add_watch(c, G_IO_IN, handle_event_libinput, li);
397 parse_opts(int argc, char *argv[])
400 static struct option long_options[] = {
401 { "help", no_argument, 0, 'h' },
404 int option_index = 0;
407 c = getopt_long(argc, argv, "h", long_options,
427 open_restricted(const char *path, int flags, void *user_data)
429 int fd = open(path, flags);
430 return fd < 0 ? -errno : fd;
434 close_restricted(int fd, void *user_data)
439 static const struct libinput_interface interface = {
440 .open_restricted = open_restricted,
441 .close_restricted = close_restricted,
445 main(int argc, char *argv[])
451 gtk_init(&argc, &argv);
453 if (parse_opts(argc, argv) != 0)
458 error("Failed to initialize udev\n");
460 li = libinput_udev_create_context(&interface, &w, udev);
461 if (!li || libinput_udev_assign_seat(li, "seat0") != 0)
462 error("Failed to initialize context from udev\n");