3 #include <linux/input.h>
7 #include <linux/input.h>
11 struct wl_input_device {
12 struct wl_object base;
13 struct wl_event_source *source;
14 struct wl_display *display;
20 static const struct wl_method input_device_methods[] = {
23 static const struct wl_interface input_device_interface = {
25 ARRAY_LENGTH(input_device_methods),
29 static void wl_input_device_data(int fd, uint32_t mask, void *data)
31 struct wl_input_device *device = data;
32 struct input_event ev[8], *e, *end;
33 int len, value, dx, dy, absolute_event;
39 len = read(fd, &ev, sizeof ev);
40 if (len < 0 || len % sizeof e[0] != 0) {
41 /* FIXME: handle error... reopen device? */;
46 end = (void *) ev + len;
47 for (e = ev; e < end; e++) {
48 /* Get the signed value, earlier kernels had this as unsigned */
86 case BTN_TOOL_AIRBRUSH:
90 device->tool = value ? e->code : 0;
94 wl_display_post_button_event(device->display,
95 &device->base, 0, value);
99 wl_display_post_button_event(device->display,
100 &device->base, 2, value);
104 wl_display_post_button_event(device->display,
105 &device->base, 1, value);
111 if (dx != 0 || dy != 0)
112 wl_display_post_relative_event(device->display,
113 &device->base, dx, dy);
114 if (absolute_event && device->tool)
115 wl_display_post_absolute_event(device->display,
117 device->x, device->y);
121 wl_input_device_create(struct wl_display *display,
122 const char *path, uint32_t id)
124 struct wl_input_device *device;
125 struct wl_event_loop *loop;
127 device = malloc(sizeof *device);
131 device->base.interface = &input_device_interface;
132 device->display = display;
135 device->fd = open(path, O_RDONLY);
136 if (device->fd < 0) {
138 fprintf(stderr, "couldn't create pointer for %s: %m\n", path);
142 loop = wl_display_get_event_loop(display);
143 device->source = wl_event_loop_add_fd(loop, device->fd,
145 wl_input_device_data, device);
146 if (device->source == NULL) {
152 return &device->base;