diff options
author | Drew DeVault <sir@cmpwn.com> | 2017-06-22 12:31:35 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-22 12:31:35 -0400 |
commit | 5055d899550a92d72362efe6add44b2547147599 (patch) | |
tree | 6f003636aee79364ef15af1dd51f743a6ff78031 /backend | |
parent | bb16025318e1c3d3844e71e6e8f29b1e8634ec3e (diff) | |
parent | a9547af35833ba06b7446bcba800d7d6a764508c (diff) |
Merge pull request #19 from nyorain/wayland-input
Basic wayland backend input
Diffstat (limited to 'backend')
-rw-r--r-- | backend/wayland/backend.c | 20 | ||||
-rw-r--r-- | backend/wayland/wl_seat.c | 219 |
2 files changed, 209 insertions, 30 deletions
diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index a58c8691..ef480c18 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -76,14 +76,14 @@ static void wlr_wl_backend_destroy(struct wlr_backend_state *state) { wlr_output_destroy(state->outputs->items[i]); } - for (size_t i = 0; state->devices && i < state->devices->length; ++i) { + for (size_t i = 0; i < state->devices->length; ++i) { wlr_input_device_destroy(state->devices->items[i]); } list_free(state->devices); + list_free(state->outputs); wlr_egl_free(&state->egl); - free(state->outputs); if (state->seat) wl_seat_destroy(state->seat); if (state->shm) wl_shm_destroy(state->shm); if (state->shell) wl_shell_destroy(state->shell); @@ -102,6 +102,17 @@ bool wlr_backend_is_wl(struct wlr_backend *b) { return b->impl == &backend_impl; } +struct wlr_output *wlr_wl_output_for_surface(struct wlr_backend_state *backend, + struct wl_surface *surface) { + for (size_t i = 0; i < backend->outputs->length; ++i) { + struct wlr_output *output = backend->outputs->items[i]; + if(output->state->surface == surface) + return output; + } + + return NULL; +} + struct wlr_backend *wlr_wl_backend_create(struct wl_display *display) { wlr_log(L_INFO, "Creating wayland backend"); @@ -133,9 +144,8 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display) { error: if (state) { - free(state->outputs); - free(state->devices); - free(state->devices); + list_free(state->devices); + list_free(state->outputs); } free(state); free(backend); diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index 5e6583ba..ab3de74d 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -8,34 +8,201 @@ #include <wlr/interfaces/wlr_input_device.h> #include <wlr/interfaces/wlr_pointer.h> #include <wlr/interfaces/wlr_keyboard.h> +#include <wlr/interfaces/wlr_touch.h> #include <wlr/util/log.h> #include "backend/wayland.h" -static void wlr_wl_device_destroy(struct wlr_input_device_state *state) { +static void pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, + wl_fixed_t surface_y) { + struct wlr_input_device *dev = data; + assert(dev && dev->pointer && dev->pointer->state); + struct wlr_output* output = wlr_wl_output_for_surface(dev->state->backend, + surface); + + if (!output) { + wlr_log(L_ERROR, "pointer entered invalid surface"); + return; + } + + dev->pointer->state->current_output = output; +} + +static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface) { + struct wlr_input_device *dev = data; + assert(dev && dev->pointer && dev->pointer->state); + dev->pointer->state->current_output = NULL; +} + +static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, + uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct wlr_input_device *dev = data; + assert(dev && dev->pointer && dev->pointer->state); + struct wlr_pointer_state *state = dev->pointer->state; + + if(!state->current_output) { + wlr_log(L_ERROR, "pointer motion event without current output"); + return; + } + + int width, height; + wl_egl_window_get_attached_size(state->current_output->state->egl_window, + &width, &height); + + struct wlr_event_pointer_motion_absolute wlr_event; + wlr_event.time_sec = time / 1000; + wlr_event.time_usec = time * 1000; + wlr_event.width_mm = width; + wlr_event.height_mm = height; + wlr_event.x_mm = wl_fixed_to_double(surface_x); + wlr_event.y_mm = wl_fixed_to_double(surface_y); + wl_signal_emit(&dev->pointer->events.motion_absolute, &wlr_event); +} + +static void pointer_handle_button(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { + struct wlr_input_device *dev = data; + assert(dev && dev->pointer); + + struct wlr_event_pointer_button wlr_event; + wlr_event.button = button; + wlr_event.state = state; + wlr_event.time_sec = time / 1000; + wlr_event.time_usec = time * 1000; + wl_signal_emit(&dev->pointer->events.button, &wlr_event); +} + +static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) { + struct wlr_input_device *dev = data; + assert(dev && dev->pointer); + + struct wlr_event_pointer_axis wlr_event; + wlr_event.delta = value; + wlr_event.orientation = axis; + wlr_event.time_sec = time / 1000; + wlr_event.time_usec = time * 1000; + wlr_event.source = dev->pointer->state->axis_source; + wl_signal_emit(&dev->pointer->events.axis, &wlr_event); +} + +static void pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) { + +} + +static void pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer, + uint32_t axis_source) { + struct wlr_input_device *dev = data; + assert(dev && dev->pointer && dev->pointer->state); + dev->pointer->state->axis_source = axis_source; +} + +static void pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis) { + +} + +static void pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer, + uint32_t axis, int32_t discrete) { + +} + +static const struct wl_pointer_listener pointer_listener = { + .enter = pointer_handle_enter, + .leave = pointer_handle_leave, + .motion = pointer_handle_motion, + .button = pointer_handle_button, + .axis = pointer_handle_axis, + .frame = pointer_handle_frame, + .axis_source = pointer_handle_axis_source, + .axis_stop = pointer_handle_axis_stop, + .axis_discrete = pointer_handle_axis_discrete +}; + +static void keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, + uint32_t format, int32_t fd, uint32_t size) { + +} + +static void keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { + +} + +static void keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, struct wl_surface *surface) { + +} + +static void keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { + struct wlr_input_device *dev = data; + assert(dev && dev->keyboard); + + struct wlr_event_keyboard_key wlr_event; + wlr_event.keycode = key; + wlr_event.state = state; + wlr_event.time_sec = time / 1000; + wlr_event.time_usec = time * 1000; + wl_signal_emit(&dev->keyboard->events.key, &wlr_event); +} + +static void keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, + uint32_t mods_locked, uint32_t group) { + +} + +static void keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard, + int32_t rate, int32_t delay) { + +} + +static struct wl_keyboard_listener keyboard_listener = { + .keymap = keyboard_handle_keymap, + .enter = keyboard_handle_enter, + .leave = keyboard_handle_leave, + .key = keyboard_handle_key, + .modifiers = keyboard_handle_modifiers, + .repeat_info = keyboard_handle_repeat_info +}; + +static void input_device_destroy(struct wlr_input_device_state *state) { + if (state->resource) + wl_proxy_destroy(state->resource); free(state); } static struct wlr_input_device_impl input_device_impl = { - .destroy = wlr_wl_device_destroy + .destroy = input_device_destroy +}; + +static void pointer_destroy(struct wlr_pointer_state *state) { + free(state); +} + +static struct wlr_pointer_impl pointer_impl = { + .destroy = pointer_destroy }; static struct wlr_input_device *allocate_device(struct wlr_backend_state *state, enum wlr_input_device_type type) { - struct wlr_input_device_state *devstate = - calloc(1, sizeof(struct wlr_input_device_state)); - if(!devstate) { + struct wlr_input_device_state *devstate; + if (!(devstate = calloc(1, sizeof(struct wlr_input_device_state)))) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); return NULL; } - // TODO: any way to retrieve those information? + devstate->backend = state; + int vendor = 0; int product = 0; - const char *name = "unknown;wayland"; + const char *name = "wayland"; struct wlr_input_device *wlr_device = wlr_input_device_create( type, &input_device_impl, devstate, name, vendor, product); - if(!wlr_device) { + if (!wlr_device) { free(devstate); return NULL; } @@ -49,40 +216,42 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, struct wlr_backend_state *state = data; assert(state->seat == wl_seat); - // TODO: add listeners and receive input if ((caps & WL_SEAT_CAPABILITY_POINTER)) { - wlr_log(L_DEBUG, "seat %p offered pointer", wl_seat); - struct wl_pointer *wl_pointer = wl_seat_get_pointer(wl_seat); + wlr_log(L_DEBUG, "seat %p offered pointer", (void*) wl_seat); + struct wlr_pointer_state *pointer_state; + if (!(pointer_state = calloc(1, sizeof(struct wlr_pointer_state)))) { + wlr_log(L_ERROR, "Unable to allocate wlr_pointer_state"); + return; + } - struct wlr_input_device *wlr_device = allocate_device(state, - WLR_INPUT_DEVICE_POINTER); - if(!wlr_device) { - wl_pointer_destroy(wl_pointer); - wlr_log(L_ERROR, "Unable to allocate wl_pointer device"); + struct wlr_input_device *wlr_device; + if (!(wlr_device = allocate_device(state, WLR_INPUT_DEVICE_POINTER))) { + free(pointer_state); + wlr_log(L_ERROR, "Unable to allocate wlr_device for pointer"); return; } - wlr_device->pointer = wlr_pointer_create(NULL, NULL); - list_add(state->devices, wlr_device); + struct wl_pointer *wl_pointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(wl_pointer, &pointer_listener, wlr_device); + wlr_device->pointer = wlr_pointer_create(&pointer_impl, pointer_state); + wlr_device->state->resource = wl_pointer; wl_signal_emit(&state->backend->events.input_add, wlr_device); } if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { - wlr_log(L_DEBUG, "seat %p offered keyboard", wl_seat); - struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat); + wlr_log(L_DEBUG, "seat %p offered keyboard", (void*) wl_seat); struct wlr_input_device *wlr_device = allocate_device(state, WLR_INPUT_DEVICE_KEYBOARD); - if(!wlr_device) { - wl_keyboard_release(wl_keyboard); + if (!wlr_device) { wlr_log(L_ERROR, "Unable to allocate wl_pointer device"); return; } + struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat); + wl_keyboard_add_listener(wl_keyboard, &keyboard_listener, wlr_device); wlr_device->keyboard = wlr_keyboard_create(NULL, NULL); - list_add(state->devices, wlr_device); + wlr_device->state->resource = wl_keyboard; wl_signal_emit(&state->backend->events.input_add, wlr_device); } - - // TODO: touch } static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { |