diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 23914240..b8df9373 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(wlr-backend libinput/backend.c libinput/events.c libinput/keyboard.c + libinput/pointer.c multi/backend.c backend.c diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 2ce368e3..5ef58a3e 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -33,6 +33,22 @@ static struct wlr_input_device_impl input_device_impl = { .destroy = wlr_libinput_device_destroy }; +static struct wlr_input_device *allocate_device(struct libinput_device *device, + list_t *devices, enum wlr_input_device_type type) { + int vendor = libinput_device_get_id_vendor(device); + int product = libinput_device_get_id_product(device); + const char *name = libinput_device_get_name(device); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + devstate->handle = device; + libinput_device_ref(device); + struct wlr_input_device *wlr_device = wlr_input_device_create( + type, &input_device_impl, devstate, + name, vendor, product); + list_add(devices, wlr_device); + return wlr_device; +} + static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); @@ -49,19 +65,16 @@ static void handle_device_added(struct wlr_backend_state *state, wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { - struct wlr_input_device_state *devstate = - calloc(1, sizeof(struct wlr_input_device_state)); - devstate->handle = device; - libinput_device_ref(device); - struct wlr_input_device *wlr_device = wlr_input_device_create( - WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, - name, vendor, product); + struct wlr_input_device *wlr_device = allocate_device(device, devices, + WLR_INPUT_DEVICE_KEYBOARD); wlr_device->keyboard = wlr_libinput_keyboard_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); - list_add(devices, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { - // TODO + struct wlr_input_device *wlr_device = allocate_device(device, devices, + WLR_INPUT_DEVICE_POINTER); + wlr_device->pointer = wlr_libinput_pointer_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { // TODO @@ -109,6 +122,18 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_KEYBOARD_KEY: handle_keyboard_key(event, device); break; + case LIBINPUT_EVENT_POINTER_MOTION: + handle_pointer_motion(event, device); + break; + case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: + handle_pointer_motion_abs(event, device); + break; + case LIBINPUT_EVENT_POINTER_BUTTON: + handle_pointer_button(event, device); + break; + case LIBINPUT_EVENT_POINTER_AXIS: + handle_pointer_axis(event, device); + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c new file mode 100644 index 00000000..0139fd60 --- /dev/null +++ b/backend/libinput/pointer.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device) { + assert(device); + return wlr_pointer_create(NULL, NULL); +} + +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->delta_x = libinput_event_pointer_get_dx(pevent); + wlr_event->delta_y = libinput_event_pointer_get_dy(pevent); + wl_signal_emit(&dev->pointer->events.motion, wlr_event); +} + +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion_absolute *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion_absolute)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->x_mm = libinput_event_pointer_get_absolute_x(pevent); + wlr_event->y_mm = libinput_event_pointer_get_absolute_y(pevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->pointer->events.motion_absolute, wlr_event); +} + +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_button *wlr_event = + calloc(1, sizeof(struct wlr_pointer_button)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->button = libinput_event_pointer_get_button(pevent); + switch (libinput_event_pointer_get_button_state(pevent)) { + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + } + wl_signal_emit(&dev->pointer->events.button, wlr_event); +} + +void handle_pointer_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_axis *wlr_event = + calloc(1, sizeof(struct wlr_pointer_axis)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + switch (libinput_event_pointer_get_axis_source(pevent)) { + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_FINGER: + wlr_event->source = WLR_AXIS_SOURCE_FINGER; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS: + wlr_event->source = WLR_AXIS_SOURCE_CONTINUOUS; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL_TILT: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL_TILT; + break; + } + enum libinput_pointer_axis axies[] = { + LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, + LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, + }; + for (size_t i = 0; i < sizeof(axies) / sizeof(axies[0]); ++i) { + if (libinput_event_pointer_has_axis(pevent, axies[i])) { + switch (axies[i]) { + case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_VERTICAL; + break; + case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_HORIZONTAL; + break; + } + wlr_event->delta = libinput_event_pointer_get_axis_value( + pevent, axies[i]); + } + wl_signal_emit(&dev->pointer->events.axis, wlr_event); + } +} diff --git a/example/rotation.c b/example/rotation.c index 2634ea03..892083e6 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -21,7 +21,6 @@ struct sample_state { struct wl_list config; - // TODO: Move renderer into shared struct wlr_renderer *renderer; struct wlr_surface *cat_texture; }; diff --git a/include/backend/libinput.h b/include/backend/libinput.h index f6622695..7c318746 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -30,9 +30,20 @@ struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device); +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device); void handle_keyboard_key(struct libinput_event *event, struct libinput_device *device); -struct wlr_keyboard *wlr_libinput_keyboard_create( + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device); +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_axis(struct libinput_event *event, struct libinput_device *device); #endif diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 5ac1baf6..91ab9da0 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories( add_library(wlr-types wlr_output.c wlr_keyboard.c + wlr_pointer.c wlr_input_device.c ) diff --git a/types/wlr_pointer.c b/types/wlr_pointer.c new file mode 100644 index 00000000..b1364c34 --- /dev/null +++ b/types/wlr_pointer.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, + struct wlr_pointer_state *state) { + struct wlr_pointer *pointer = calloc(1, sizeof(struct wlr_pointer)); + pointer->impl = impl; + pointer->state = state; + wl_signal_init(&pointer->events.motion); + wl_signal_init(&pointer->events.motion_absolute); + wl_signal_init(&pointer->events.button); + wl_signal_init(&pointer->events.axis); + return pointer; +} + +void wlr_pointer_destroy(struct wlr_pointer *kb) { + if (!kb) return; + kb->impl->destroy(kb->state); + free(kb); +}