commit 06a2686

Michael Forney  ·  2013-09-14 03:29:16 +0000 UTC
parent 3fe7676
evdev_device: Use libevdev
4 files changed,  +33, -69
+1, -0
1@@ -22,6 +22,7 @@ PKG_PROG_PKG_CONFIG([0.9.0])
2 dnl Check for libraries {{{
3 PKG_CHECK_MODULES([wayland_server], [wayland-server])
4 PKG_CHECK_MODULES([udev], [libudev])
5+PKG_CHECK_MODULES([libevdev], [libevdev])
6 PKG_CHECK_MODULES([xkbcommon], [xkbcommon])
7 PKG_CHECK_MODULES([drm], [libdrm])
8 PKG_CHECK_MODULES([pixman], [pixman-1])
+3, -3
 1@@ -1,7 +1,7 @@
 2 # swc: libswc/Makefile.am
 3 
 4 AM_CPPFLAGS = -I$(top_srcdir) -I$(PROTOCOL_DIR)
 5-AM_CFLAGS = $(drm_CFLAGS) $(pixman_CFLAGS) $(wld_CFLAGS)
 6+AM_CFLAGS = $(drm_CFLAGS) $(pixman_CFLAGS) $(wld_CFLAGS) $(libevdev_CFLAGS)
 7 
 8 lib_LTLIBRARIES = libswc.la
 9 
10@@ -30,7 +30,7 @@ libswc_la_SOURCES = \
11     drm_buffer.c drm_buffer.h \
12     ../protocol/wayland-drm-protocol.c
13 
14-libswc_la_LIBADD = $(wayland_server_LIBS) $(udev_LIBS) $(xkbcommon_LIBS) \
15-                   $(drm_LIBS) $(pixman_LIBS) $(wld_LIBS) \
16+libswc_la_LIBADD = $(wayland_server_LIBS) $(udev_LIBS) $(libevdev_LIBS) \
17+                   $(xkbcommon_LIBS) $(drm_LIBS) $(pixman_LIBS) $(wld_LIBS) \
18                    ../launch/liblaunch-protocol.la
19 
+26, -66
  1@@ -8,12 +8,11 @@
  2 #include <string.h>
  3 #include <fcntl.h>
  4 #include <unistd.h>
  5-
  6-#define BITS(var, n) uint64_t var[((n)-1)/64+1]
  7-#define TEST_BIT(var, n) \
  8-    (((var)[(n)/((sizeof (var)[0])*8)] >> ((n)%((sizeof (var)[0])*8))) & 1)
  9+#include <errno.h>
 10+#include <libevdev/libevdev.h>
 11 
 12 #define AXIS_STEP_DISTANCE 10
 13+#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
 14 
 15 static inline uint32_t timeval_to_msec(struct timeval * time)
 16 {
 17@@ -126,45 +125,30 @@ static void handle_motion_events(struct swc_evdev_device * device,
 18     }
 19 }
 20 
 21-static void process_events(struct swc_evdev_device * device,
 22-                           struct input_event * events, uint32_t event_count)
 23-{
 24-    struct input_event * event, * end = events + event_count;
 25-
 26-    for (event = events; event != end; ++event)
 27-    {
 28-        if (!is_motion_event(event))
 29-            handle_motion_events(device, timeval_to_msec(&event->time));
 30-
 31-        /*
 32-        printf("processing event, type: %u, code: %u, value: %d\n",
 33-               event->type, event->code, event->value);
 34-        */
 35-
 36-        if (event->type < (sizeof event_handlers / sizeof event_handlers[0])
 37-            && event_handlers[event->type])
 38-        {
 39-            event_handlers[event->type](device, event);
 40-        }
 41-    }
 42-}
 43-
 44 static int handle_data(int fd, uint32_t mask, void * data)
 45 {
 46     struct swc_evdev_device * device = data;
 47-    struct input_event events[32];
 48-    ssize_t bytes_read;
 49+    struct input_event event;
 50+    int ret;
 51 
 52     do
 53     {
 54-        bytes_read = read(fd, events, sizeof events);
 55+        ret = libevdev_next_event(device->dev, LIBEVDEV_READ_NORMAL, &event);
 56+
 57+        while (ret == 1)
 58+            ret = libevdev_next_event(device->dev, LIBEVDEV_READ_SYNC, &event);
 59 
 60-        /* Stop on error */
 61-        if (bytes_read == -1)
 62-            return 1;
 63+        if (!is_motion_event(&event))
 64+            handle_motion_events(device, timeval_to_msec(&event.time));
 65 
 66-        process_events(device, events, bytes_read / sizeof events[0]);
 67-    } while (bytes_read > 0);
 68+        if (event.type < ARRAY_SIZE(event_handlers)
 69+            && event_handlers[event.type])
 70+        {
 71+            event_handlers[event.type](device, &event);
 72+        }
 73+    } while (ret != -EAGAIN);
 74+
 75+    handle_motion_events(device, timeval_to_msec(&event.time));
 76 
 77     return 1;
 78 }
 79@@ -173,7 +157,6 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device,
 80                                  struct udev_device * udev_device)
 81 {
 82     const char * path, * model, * vendor;
 83-    BITS(ev_bits, EV_MAX);
 84     uint32_t index;
 85 
 86     path = udev_device_get_devnode(udev_device);
 87@@ -195,9 +178,13 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device,
 88         goto error_base;
 89     }
 90 
 91-    printf("adding device %s %s\n", device->vendor, device->model);
 92+    if (libevdev_new_from_fd(device->fd, &device->dev) != 0)
 93+    {
 94+        fprintf(stderr, "Could not create libevdev device\n");
 95+        goto error_fd;
 96+    }
 97 
 98-    ioctl(device->fd, EVIOCGBIT(0, sizeof ev_bits), &ev_bits);
 99+    printf("adding device %s %s\n", device->vendor, device->model);
100 
101     device->capabilities = 0;
102     /* XXX: touch devices */
103@@ -215,34 +202,6 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device,
104         printf("\tthis device is a pointer\n");
105     }
106 
107-    if (device->capabilities & WL_SEAT_CAPABILITY_POINTER)
108-    {
109-        /* Check if the device has relative motion. */
110-        if (TEST_BIT(ev_bits, EV_REL))
111-        {
112-            BITS(rel_bits, REL_MAX);
113-
114-            ioctl(device->fd, EVIOCGBIT(EV_REL, sizeof rel_bits), &rel_bits);
115-
116-            if (TEST_BIT(rel_bits, REL_X) || TEST_BIT(rel_bits, REL_Y))
117-            {
118-            }
119-        }
120-
121-        /* Check if the device has absolute motion. */
122-        if (TEST_BIT(ev_bits, EV_ABS))
123-        {
124-            BITS(abs_bits, ABS_MAX);
125-
126-            ioctl(device->fd, EVIOCGBIT(EV_ABS, sizeof abs_bits), &abs_bits);
127-
128-            if (TEST_BIT(abs_bits, ABS_X))
129-                ioctl(device->fd, EVIOCGABS(ABS_X), &device->motion.abs.info.x);
130-            if (TEST_BIT(abs_bits, ABS_Y))
131-                ioctl(device->fd, EVIOCGABS(ABS_X), &device->motion.abs.info.y);
132-        }
133-    }
134-
135     return true;
136 
137   error_fd:
138@@ -254,6 +213,7 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device,
139 void swc_evdev_device_finish(struct swc_evdev_device * device)
140 {
141     wl_event_source_remove(device->source);
142+    libevdev_free(device->dev);
143     free(device->model);
144     free(device->vendor);
145     close(device->fd);
+3, -0
 1@@ -6,6 +6,8 @@
 2 #include <linux/input.h>
 3 #include <wayland-server.h>
 4 
 5+struct libevdev_device;
 6+
 7 enum swc_evdev_device_event_type
 8 {
 9     SWC_EVDEV_DEVICE_EVENT_KEY,
10@@ -48,6 +50,7 @@ struct swc_evdev_device_event_data
11 struct swc_evdev_device
12 {
13     int fd;
14+    struct libevdev * dev;
15     char * model, * vendor;
16 
17     struct