commit 1893d92

Michael Forney  ·  2013-09-14 03:46:38 +0000 UTC
parent 2814349
evdev: Use handler instead of signals
4 files changed,  +68, -128
+25, -45
  1@@ -22,65 +22,52 @@ static inline uint32_t timeval_to_msec(struct timeval * time)
  2 static void handle_key_event(struct swc_evdev_device * device,
  3                              struct input_event * input_event)
  4 {
  5-    struct swc_event event;
  6-    struct swc_evdev_device_event_data data;
  7-
  8-    event.data = &data;
  9-    data.time = timeval_to_msec(&input_event->time);
 10+    uint32_t time = timeval_to_msec(&input_event->time);
 11+    uint32_t state;
 12 
 13     if ((input_event->code >= BTN_MISC && input_event->code <= BTN_GEAR_UP)
 14         || input_event->code >= BTN_TRIGGER_HAPPY)
 15     {
 16-        event.type = SWC_EVDEV_DEVICE_EVENT_BUTTON;
 17-        data.button.button = input_event->code;
 18-        data.button.state = input_event->value ? WL_POINTER_BUTTON_STATE_PRESSED
 19-                                               : WL_POINTER_BUTTON_STATE_RELEASED;
 20+        state = input_event->value ? WL_POINTER_BUTTON_STATE_PRESSED
 21+                                   : WL_POINTER_BUTTON_STATE_RELEASED;
 22+        device->handler->button(device->handler, time,
 23+                                input_event->code, state);
 24     }
 25     else
 26     {
 27-        event.type = SWC_EVDEV_DEVICE_EVENT_KEY;
 28-        data.key.key = input_event->code;
 29-        data.key.state = input_event->value ? WL_KEYBOARD_KEY_STATE_PRESSED
 30-                                            : WL_KEYBOARD_KEY_STATE_RELEASED;
 31+        state = input_event->value ? WL_KEYBOARD_KEY_STATE_PRESSED
 32+                                   : WL_KEYBOARD_KEY_STATE_RELEASED;
 33+        device->handler->key(device->handler, time, input_event->code, state);
 34     }
 35-
 36-    wl_signal_emit(&device->event_signal, &event);
 37 }
 38 
 39 static void handle_rel_event(struct swc_evdev_device * device,
 40                              struct input_event * input_event)
 41 {
 42-    struct swc_event event;
 43-    struct swc_evdev_device_event_data data;
 44-
 45-    event.data = &data;
 46-    data.time = timeval_to_msec(&input_event->time);
 47+    uint32_t time = timeval_to_msec(&input_event->time);
 48+    uint32_t axis, amount;
 49 
 50     switch (input_event->code)
 51     {
 52         case REL_X:
 53             device->motion.rel.dx += input_event->value;
 54             device->motion.rel.pending = true;
 55-            break;
 56+            return;
 57         case REL_Y:
 58             device->motion.rel.dy += input_event->value;
 59             device->motion.rel.pending = true;
 60-            break;
 61+            return;
 62         case REL_WHEEL:
 63-            event.type = SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION;
 64-            data.axis_motion.axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
 65-            data.axis_motion.amount
 66-                = -AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value);
 67-            wl_signal_emit(&device->event_signal, &event);
 68+            axis = WL_POINTER_AXIS_VERTICAL_SCROLL;
 69+            amount = -AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value);
 70             break;
 71         case REL_HWHEEL:
 72-            event.type = SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION;
 73-            data.axis_motion.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
 74-            data.axis_motion.amount
 75-                = AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value);
 76-            wl_signal_emit(&device->event_signal, &event);
 77+            axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL;
 78+            amount = AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value);
 79             break;
 80     }
 81+
 82+    device->handler->axis(device->handler, time, axis, amount);
 83 }
 84 
 85 static void handle_abs_event(struct swc_evdev_device * device,
 86@@ -105,19 +92,12 @@ static bool is_motion_event(struct input_event * event)
 87 static void handle_motion_events(struct swc_evdev_device * device,
 88                                  uint32_t time)
 89 {
 90-    struct swc_event event;
 91-    struct swc_evdev_device_event_data data;
 92-
 93-    event.data = &data;
 94-    data.time = time;
 95-
 96     if (device->motion.rel.pending)
 97     {
 98-        event.type = SWC_EVDEV_DEVICE_EVENT_RELATIVE_MOTION;
 99-        data.relative_motion.dx = wl_fixed_from_int(device->motion.rel.dx);
100-        data.relative_motion.dy = wl_fixed_from_int(device->motion.rel.dy);
101+        wl_fixed_t dx = wl_fixed_from_int(device->motion.rel.dx);
102+        wl_fixed_t dy = wl_fixed_from_int(device->motion.rel.dy);
103 
104-        wl_signal_emit(&device->event_signal, &event);
105+        device->handler->relative_motion(device->handler, time, dx, dy);
106 
107         device->motion.rel.pending = false;
108         device->motion.rel.dx = 0;
109@@ -154,15 +134,14 @@ static int handle_data(int fd, uint32_t mask, void * data)
110 }
111 
112 bool swc_evdev_device_initialize(struct swc_evdev_device * device,
113-                                 const char * path)
114+                                 const char * path,
115+                                 const struct swc_evdev_device_handler * handler)
116 {
117     uint32_t index;
118 
119     device->fd = open(path, O_RDWR | O_NONBLOCK | O_CLOEXEC);
120     memset(&device->motion, 0, sizeof device->motion);
121 
122-    wl_signal_init(&device->event_signal);
123-
124     if (device->fd == -1)
125     {
126         printf("couldn't open input device at %s\n", path);
127@@ -177,6 +156,7 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device,
128 
129     printf("Adding device %s\n", libevdev_get_name(device->dev));
130 
131+    device->handler = handler;
132     device->capabilities = 0;
133     /* XXX: touch devices */
134 
+15, -39
 1@@ -6,44 +6,18 @@
 2 #include <wayland-server.h>
 3 
 4 struct libevdev_device;
 5+struct swc_evdev_device;
 6 
 7-enum swc_evdev_device_event_type
 8+struct swc_evdev_device_handler
 9 {
10-    SWC_EVDEV_DEVICE_EVENT_KEY,
11-    SWC_EVDEV_DEVICE_EVENT_BUTTON,
12-    SWC_EVDEV_DEVICE_EVENT_RELATIVE_MOTION,
13-    SWC_EVDEV_DEVICE_EVENT_ABSOLUTE_MOTION,
14-    SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION
15-};
16-
17-struct swc_evdev_device_event_data
18-{
19-    uint32_t time;
20-    union
21-    {
22-        struct
23-        {
24-            wl_fixed_t dx, dy;
25-        } relative_motion;
26-
27-        struct
28-        {
29-            wl_fixed_t amount;
30-            enum wl_pointer_axis axis;
31-        } axis_motion;
32-
33-        struct
34-        {
35-            uint32_t key;
36-            enum wl_keyboard_key_state state;
37-        } key;
38-
39-        struct
40-        {
41-            uint32_t button;
42-            enum wl_pointer_button_state state;
43-        } button;
44-    };
45+    void (* key)(const struct swc_evdev_device_handler * handler,
46+                 uint32_t time, uint32_t key, uint32_t state);
47+    void (* button)(const struct swc_evdev_device_handler * handler,
48+                    uint32_t time, uint32_t key, uint32_t state);
49+    void (* axis)(const struct swc_evdev_device_handler * handler,
50+                  uint32_t time, uint32_t axis, wl_fixed_t amount);
51+    void (* relative_motion)(const struct swc_evdev_device_handler * handler,
52+                             uint32_t time, wl_fixed_t dx, wl_fixed_t dy);
53 };
54 
55 struct swc_evdev_device
56@@ -51,6 +25,8 @@ struct swc_evdev_device
57     int fd;
58     struct libevdev * dev;
59 
60+    const struct swc_evdev_device_handler * handler;
61+
62     struct
63     {
64         struct
65@@ -74,12 +50,12 @@ struct swc_evdev_device
66     uint32_t capabilities;
67 
68     struct wl_event_source * source;
69-    struct wl_signal event_signal;
70     struct wl_list link;
71 };
72 
73-bool swc_evdev_device_initialize(struct swc_evdev_device * device,
74-                                 const char * path);
75+bool swc_evdev_device_initialize
76+    (struct swc_evdev_device * device, const char * path,
77+     const struct swc_evdev_device_handler * handler);
78 
79 void swc_evdev_device_finish(struct swc_evdev_device * device);
80 
+26, -44
  1@@ -17,61 +17,41 @@ struct evdev_device_entry
  2     struct wl_list link;
  3 };
  4 
  5-static void handle_key(struct swc_seat * seat, uint32_t time, uint32_t key,
  6-                       uint32_t state)
  7+static void handle_key(const struct swc_evdev_device_handler * handler,
  8+                       uint32_t time, uint32_t key, uint32_t state)
  9 {
 10+    struct swc_seat * seat = swc_container_of(handler, typeof(*seat),
 11+                                              evdev_handler);
 12+
 13     swc_keyboard_handle_key(&seat->keyboard, time, key, state);
 14 }
 15 
 16-static void handle_button(struct swc_seat * seat, uint32_t time,
 17-                          uint32_t button, uint32_t state)
 18+static void handle_button(const struct swc_evdev_device_handler * handler,
 19+                          uint32_t time, uint32_t button, uint32_t state)
 20 {
 21+    struct swc_seat * seat = swc_container_of(handler, typeof(*seat),
 22+                                              evdev_handler);
 23+
 24     swc_pointer_handle_button(&seat->pointer, time, button, state);
 25 }
 26 
 27-static void handle_relative_motion(struct swc_seat * seat, uint32_t time,
 28-                                   wl_fixed_t dx, wl_fixed_t dy)
 29+static void handle_axis(const struct swc_evdev_device_handler * handler,
 30+                        uint32_t time, uint32_t axis, wl_fixed_t amount)
 31 {
 32-    swc_pointer_handle_relative_motion(&seat->pointer, time, dx, dy);
 33-}
 34+    struct swc_seat * seat = swc_container_of(handler, typeof(*seat),
 35+                                              evdev_handler);
 36 
 37-static void handle_axis_motion(struct swc_seat * seat, uint32_t time,
 38-                               enum wl_pointer_axis axis, wl_fixed_t amount)
 39-{
 40     swc_pointer_handle_axis(&seat->pointer, time, axis, amount);
 41 }
 42 
 43-static void handle_evdev_event(struct wl_listener * listener, void * data)
 44+static void handle_relative_motion
 45+    (const struct swc_evdev_device_handler * handler,
 46+     uint32_t time, wl_fixed_t dx, wl_fixed_t dy)
 47 {
 48-    struct evdev_device_entry * entry;
 49-    struct swc_event * event = data;
 50-    struct swc_evdev_device_event_data * evdev_data = event->data;
 51+    struct swc_seat * seat = swc_container_of(handler, typeof(*seat),
 52+                                              evdev_handler);
 53 
 54-    entry = swc_container_of(listener, typeof(*entry), event_listener);
 55-
 56-    switch (event->type)
 57-    {
 58-        case SWC_EVDEV_DEVICE_EVENT_KEY:
 59-            handle_key(entry->seat, evdev_data->time, evdev_data->key.key,
 60-                       evdev_data->key.state);
 61-            break;
 62-        case SWC_EVDEV_DEVICE_EVENT_BUTTON:
 63-            handle_button(entry->seat, evdev_data->time,
 64-                          evdev_data->button.button, evdev_data->button.state);
 65-            break;
 66-        case SWC_EVDEV_DEVICE_EVENT_RELATIVE_MOTION:
 67-            handle_relative_motion(entry->seat, evdev_data->time,
 68-                                   evdev_data->relative_motion.dx,
 69-                                   evdev_data->relative_motion.dy);
 70-            break;
 71-        case SWC_EVDEV_DEVICE_EVENT_ABSOLUTE_MOTION:
 72-            break;
 73-        case SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION:
 74-            handle_axis_motion(entry->seat, evdev_data->time,
 75-                               evdev_data->axis_motion.axis,
 76-                               evdev_data->axis_motion.amount);
 77-            break;
 78-    }
 79+    swc_pointer_handle_relative_motion(&seat->pointer, time, dx, dy);
 80 }
 81 
 82 static void handle_keyboard_focus_event(struct wl_listener * listener,
 83@@ -202,16 +182,14 @@ static void add_device(struct swc_seat * seat, struct udev_device * udev_device)
 84     }
 85 
 86     entry->seat = seat;
 87-    entry->event_listener.notify = &handle_evdev_event;
 88 
 89-    if (!swc_evdev_device_initialize(&entry->device, device_path))
 90+    if (!swc_evdev_device_initialize(&entry->device, device_path,
 91+                                     &seat->evdev_handler))
 92     {
 93         free(entry);
 94         return;
 95     }
 96 
 97-    wl_signal_add(&entry->device.event_signal, &entry->event_listener);
 98-
 99     if (~seat->capabilities & entry->device.capabilities)
100     {
101         seat->capabilities |= entry->device.capabilities;
102@@ -228,6 +206,10 @@ bool swc_seat_initialize(struct swc_seat * seat, struct udev * udev,
103     seat->capabilities = 0;
104     seat->keyboard_focus_listener.notify = &handle_keyboard_focus_event;
105     seat->data_device_listener.notify = &handle_data_device_event;
106+    seat->evdev_handler.key = &handle_key;
107+    seat->evdev_handler.button = &handle_button;
108+    seat->evdev_handler.axis = &handle_axis;
109+    seat->evdev_handler.relative_motion = &handle_relative_motion;
110 
111     if (!swc_data_device_initialize(&seat->data_device))
112     {
+2, -0
 1@@ -5,6 +5,7 @@
 2 #include "data_device.h"
 3 #include "keyboard.h"
 4 #include "pointer.h"
 5+#include "evdev_device.h"
 6 
 7 #include <stdint.h>
 8 #include <stdbool.h>
 9@@ -28,6 +29,7 @@ struct swc_seat
10     struct swc_pointer pointer;
11 
12     struct wl_list devices;
13+    struct swc_evdev_device_handler evdev_handler;
14 };
15 
16 bool swc_seat_initialize(struct swc_seat * seat, struct udev * udev,