swc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/swc
Log | Files | Refs | README | LICENSE

commit 1893d9226d80bf68326df91e9ce1fe2925c7bc90
parent 2814349c0270f1b4235e37d5965ae362f3416d4f
Author: Michael Forney <mforney@mforney.org>
Date:   Fri, 13 Sep 2013 20:46:38 -0700

evdev: Use handler instead of signals

Diffstat:
Mlibswc/evdev_device.c | 70+++++++++++++++++++++++++---------------------------------------------
Mlibswc/evdev_device.h | 54+++++++++++++++---------------------------------------
Mlibswc/seat.c | 70++++++++++++++++++++++++++--------------------------------------------
Mlibswc/seat.h | 2++
4 files changed, 68 insertions(+), 128 deletions(-)

diff --git a/libswc/evdev_device.c b/libswc/evdev_device.c @@ -22,65 +22,52 @@ static inline uint32_t timeval_to_msec(struct timeval * time) static void handle_key_event(struct swc_evdev_device * device, struct input_event * input_event) { - struct swc_event event; - struct swc_evdev_device_event_data data; - - event.data = &data; - data.time = timeval_to_msec(&input_event->time); + uint32_t time = timeval_to_msec(&input_event->time); + uint32_t state; if ((input_event->code >= BTN_MISC && input_event->code <= BTN_GEAR_UP) || input_event->code >= BTN_TRIGGER_HAPPY) { - event.type = SWC_EVDEV_DEVICE_EVENT_BUTTON; - data.button.button = input_event->code; - data.button.state = input_event->value ? WL_POINTER_BUTTON_STATE_PRESSED - : WL_POINTER_BUTTON_STATE_RELEASED; + state = input_event->value ? WL_POINTER_BUTTON_STATE_PRESSED + : WL_POINTER_BUTTON_STATE_RELEASED; + device->handler->button(device->handler, time, + input_event->code, state); } else { - event.type = SWC_EVDEV_DEVICE_EVENT_KEY; - data.key.key = input_event->code; - data.key.state = input_event->value ? WL_KEYBOARD_KEY_STATE_PRESSED - : WL_KEYBOARD_KEY_STATE_RELEASED; + state = input_event->value ? WL_KEYBOARD_KEY_STATE_PRESSED + : WL_KEYBOARD_KEY_STATE_RELEASED; + device->handler->key(device->handler, time, input_event->code, state); } - - wl_signal_emit(&device->event_signal, &event); } static void handle_rel_event(struct swc_evdev_device * device, struct input_event * input_event) { - struct swc_event event; - struct swc_evdev_device_event_data data; - - event.data = &data; - data.time = timeval_to_msec(&input_event->time); + uint32_t time = timeval_to_msec(&input_event->time); + uint32_t axis, amount; switch (input_event->code) { case REL_X: device->motion.rel.dx += input_event->value; device->motion.rel.pending = true; - break; + return; case REL_Y: device->motion.rel.dy += input_event->value; device->motion.rel.pending = true; - break; + return; case REL_WHEEL: - event.type = SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION; - data.axis_motion.axis = WL_POINTER_AXIS_VERTICAL_SCROLL; - data.axis_motion.amount - = -AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); - wl_signal_emit(&device->event_signal, &event); + axis = WL_POINTER_AXIS_VERTICAL_SCROLL; + amount = -AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); break; case REL_HWHEEL: - event.type = SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION; - data.axis_motion.axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; - data.axis_motion.amount - = AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); - wl_signal_emit(&device->event_signal, &event); + axis = WL_POINTER_AXIS_HORIZONTAL_SCROLL; + amount = AXIS_STEP_DISTANCE * wl_fixed_from_int(input_event->value); break; } + + device->handler->axis(device->handler, time, axis, amount); } static void handle_abs_event(struct swc_evdev_device * device, @@ -105,19 +92,12 @@ static bool is_motion_event(struct input_event * event) static void handle_motion_events(struct swc_evdev_device * device, uint32_t time) { - struct swc_event event; - struct swc_evdev_device_event_data data; - - event.data = &data; - data.time = time; - if (device->motion.rel.pending) { - event.type = SWC_EVDEV_DEVICE_EVENT_RELATIVE_MOTION; - data.relative_motion.dx = wl_fixed_from_int(device->motion.rel.dx); - data.relative_motion.dy = wl_fixed_from_int(device->motion.rel.dy); + wl_fixed_t dx = wl_fixed_from_int(device->motion.rel.dx); + wl_fixed_t dy = wl_fixed_from_int(device->motion.rel.dy); - wl_signal_emit(&device->event_signal, &event); + device->handler->relative_motion(device->handler, time, dx, dy); device->motion.rel.pending = false; device->motion.rel.dx = 0; @@ -154,15 +134,14 @@ static int handle_data(int fd, uint32_t mask, void * data) } bool swc_evdev_device_initialize(struct swc_evdev_device * device, - const char * path) + const char * path, + const struct swc_evdev_device_handler * handler) { uint32_t index; device->fd = open(path, O_RDWR | O_NONBLOCK | O_CLOEXEC); memset(&device->motion, 0, sizeof device->motion); - wl_signal_init(&device->event_signal); - if (device->fd == -1) { printf("couldn't open input device at %s\n", path); @@ -177,6 +156,7 @@ bool swc_evdev_device_initialize(struct swc_evdev_device * device, printf("Adding device %s\n", libevdev_get_name(device->dev)); + device->handler = handler; device->capabilities = 0; /* XXX: touch devices */ diff --git a/libswc/evdev_device.h b/libswc/evdev_device.h @@ -6,44 +6,18 @@ #include <wayland-server.h> struct libevdev_device; +struct swc_evdev_device; -enum swc_evdev_device_event_type +struct swc_evdev_device_handler { - SWC_EVDEV_DEVICE_EVENT_KEY, - SWC_EVDEV_DEVICE_EVENT_BUTTON, - SWC_EVDEV_DEVICE_EVENT_RELATIVE_MOTION, - SWC_EVDEV_DEVICE_EVENT_ABSOLUTE_MOTION, - SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION -}; - -struct swc_evdev_device_event_data -{ - uint32_t time; - union - { - struct - { - wl_fixed_t dx, dy; - } relative_motion; - - struct - { - wl_fixed_t amount; - enum wl_pointer_axis axis; - } axis_motion; - - struct - { - uint32_t key; - enum wl_keyboard_key_state state; - } key; - - struct - { - uint32_t button; - enum wl_pointer_button_state state; - } button; - }; + void (* key)(const struct swc_evdev_device_handler * handler, + uint32_t time, uint32_t key, uint32_t state); + void (* button)(const struct swc_evdev_device_handler * handler, + uint32_t time, uint32_t key, uint32_t state); + void (* axis)(const struct swc_evdev_device_handler * handler, + uint32_t time, uint32_t axis, wl_fixed_t amount); + void (* relative_motion)(const struct swc_evdev_device_handler * handler, + uint32_t time, wl_fixed_t dx, wl_fixed_t dy); }; struct swc_evdev_device @@ -51,6 +25,8 @@ struct swc_evdev_device int fd; struct libevdev * dev; + const struct swc_evdev_device_handler * handler; + struct { struct @@ -74,12 +50,12 @@ struct swc_evdev_device uint32_t capabilities; struct wl_event_source * source; - struct wl_signal event_signal; struct wl_list link; }; -bool swc_evdev_device_initialize(struct swc_evdev_device * device, - const char * path); +bool swc_evdev_device_initialize + (struct swc_evdev_device * device, const char * path, + const struct swc_evdev_device_handler * handler); void swc_evdev_device_finish(struct swc_evdev_device * device); diff --git a/libswc/seat.c b/libswc/seat.c @@ -17,61 +17,41 @@ struct evdev_device_entry struct wl_list link; }; -static void handle_key(struct swc_seat * seat, uint32_t time, uint32_t key, - uint32_t state) +static void handle_key(const struct swc_evdev_device_handler * handler, + uint32_t time, uint32_t key, uint32_t state) { + struct swc_seat * seat = swc_container_of(handler, typeof(*seat), + evdev_handler); + swc_keyboard_handle_key(&seat->keyboard, time, key, state); } -static void handle_button(struct swc_seat * seat, uint32_t time, - uint32_t button, uint32_t state) +static void handle_button(const struct swc_evdev_device_handler * handler, + uint32_t time, uint32_t button, uint32_t state) { + struct swc_seat * seat = swc_container_of(handler, typeof(*seat), + evdev_handler); + swc_pointer_handle_button(&seat->pointer, time, button, state); } -static void handle_relative_motion(struct swc_seat * seat, uint32_t time, - wl_fixed_t dx, wl_fixed_t dy) +static void handle_axis(const struct swc_evdev_device_handler * handler, + uint32_t time, uint32_t axis, wl_fixed_t amount) { - swc_pointer_handle_relative_motion(&seat->pointer, time, dx, dy); -} + struct swc_seat * seat = swc_container_of(handler, typeof(*seat), + evdev_handler); -static void handle_axis_motion(struct swc_seat * seat, uint32_t time, - enum wl_pointer_axis axis, wl_fixed_t amount) -{ swc_pointer_handle_axis(&seat->pointer, time, axis, amount); } -static void handle_evdev_event(struct wl_listener * listener, void * data) +static void handle_relative_motion + (const struct swc_evdev_device_handler * handler, + uint32_t time, wl_fixed_t dx, wl_fixed_t dy) { - struct evdev_device_entry * entry; - struct swc_event * event = data; - struct swc_evdev_device_event_data * evdev_data = event->data; + struct swc_seat * seat = swc_container_of(handler, typeof(*seat), + evdev_handler); - entry = swc_container_of(listener, typeof(*entry), event_listener); - - switch (event->type) - { - case SWC_EVDEV_DEVICE_EVENT_KEY: - handle_key(entry->seat, evdev_data->time, evdev_data->key.key, - evdev_data->key.state); - break; - case SWC_EVDEV_DEVICE_EVENT_BUTTON: - handle_button(entry->seat, evdev_data->time, - evdev_data->button.button, evdev_data->button.state); - break; - case SWC_EVDEV_DEVICE_EVENT_RELATIVE_MOTION: - handle_relative_motion(entry->seat, evdev_data->time, - evdev_data->relative_motion.dx, - evdev_data->relative_motion.dy); - break; - case SWC_EVDEV_DEVICE_EVENT_ABSOLUTE_MOTION: - break; - case SWC_EVDEV_DEVICE_EVENT_AXIS_MOTION: - handle_axis_motion(entry->seat, evdev_data->time, - evdev_data->axis_motion.axis, - evdev_data->axis_motion.amount); - break; - } + swc_pointer_handle_relative_motion(&seat->pointer, time, dx, dy); } static void handle_keyboard_focus_event(struct wl_listener * listener, @@ -202,16 +182,14 @@ static void add_device(struct swc_seat * seat, struct udev_device * udev_device) } entry->seat = seat; - entry->event_listener.notify = &handle_evdev_event; - if (!swc_evdev_device_initialize(&entry->device, device_path)) + if (!swc_evdev_device_initialize(&entry->device, device_path, + &seat->evdev_handler)) { free(entry); return; } - wl_signal_add(&entry->device.event_signal, &entry->event_listener); - if (~seat->capabilities & entry->device.capabilities) { seat->capabilities |= entry->device.capabilities; @@ -228,6 +206,10 @@ bool swc_seat_initialize(struct swc_seat * seat, struct udev * udev, seat->capabilities = 0; seat->keyboard_focus_listener.notify = &handle_keyboard_focus_event; seat->data_device_listener.notify = &handle_data_device_event; + seat->evdev_handler.key = &handle_key; + seat->evdev_handler.button = &handle_button; + seat->evdev_handler.axis = &handle_axis; + seat->evdev_handler.relative_motion = &handle_relative_motion; if (!swc_data_device_initialize(&seat->data_device)) { diff --git a/libswc/seat.h b/libswc/seat.h @@ -5,6 +5,7 @@ #include "data_device.h" #include "keyboard.h" #include "pointer.h" +#include "evdev_device.h" #include <stdint.h> #include <stdbool.h> @@ -28,6 +29,7 @@ struct swc_seat struct swc_pointer pointer; struct wl_list devices; + struct swc_evdev_device_handler evdev_handler; }; bool swc_seat_initialize(struct swc_seat * seat, struct udev * udev,