From 1ac5257357fdf51b2864149f4896c51afd2eda09 Mon Sep 17 00:00:00 2001 From: Mykola Orliuk Date: Sat, 3 Oct 2020 17:34:32 +0200 Subject: [PATCH] backend/wayland: factor out wlr_wl_seat --- backend/wayland/backend.c | 28 ++++++------- backend/wayland/output.c | 7 +++- backend/wayland/seat.c | 84 +++++++++++++++++++++++++++------------ include/backend/wayland.h | 17 +++++--- 4 files changed, 88 insertions(+), 48 deletions(-) diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index a09126ce2..2059b7266 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -103,9 +103,9 @@ static void registry_global(void *data, struct wl_registry *registry, wl->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4); } else if (strcmp(iface, wl_seat_interface.name) == 0) { - wl->seat = wl_registry_bind(registry, name, + struct wl_seat *wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 5); - wl_seat_add_listener(wl->seat, &seat_listener, wl); + create_wl_seat(wl_seat, wl); } else if (strcmp(iface, xdg_wm_base_interface.name) == 0) { wl->xdg_wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); @@ -155,13 +155,16 @@ static bool backend_start(struct wlr_backend *backend) { wl->started = true; - if (wl->keyboard) { - create_wl_keyboard(wl->keyboard, wl); - } + struct wlr_wl_seat *seat = wl->seat; + if (seat != NULL) { + if (seat->keyboard) { + create_wl_keyboard(seat->keyboard, wl); + } - if (wl->tablet_manager && wl->seat) { - wl_add_tablet_seat(wl->tablet_manager, - wl->seat, wl); + if (wl->tablet_manager) { + wl_add_tablet_seat(wl->tablet_manager, + seat->wl_seat, wl); + } } for (size_t i = 0; i < wl->requested_outputs; ++i) { @@ -192,8 +195,6 @@ static void backend_destroy(struct wlr_backend *backend) { wl_list_remove(&wl->local_display_destroy.link); - free(wl->seat_name); - wl_event_source_remove(wl->remote_display_src); wlr_renderer_destroy(wl->renderer); @@ -201,12 +202,7 @@ static void backend_destroy(struct wlr_backend *backend) { wlr_drm_format_set_finish(&wl->linux_dmabuf_v1_formats); - if (wl->pointer) { - wl_pointer_destroy(wl->pointer); - } - if (wl->seat) { - wl_seat_destroy(wl->seat); - } + destroy_wl_seats(wl); if (wl->zxdg_decoration_manager_v1) { zxdg_decoration_manager_v1_destroy(wl->zxdg_decoration_manager_v1); } diff --git a/backend/wayland/output.c b/backend/wayland/output.c index b234c6a0b..568d90db6 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -586,8 +586,11 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *wlr_backend) { wlr_signal_emit_safe(&backend->backend.events.new_output, wlr_output); - if (backend->pointer != NULL) { - create_wl_pointer(backend->pointer, output); + struct wlr_wl_seat *seat = backend->seat; + if (seat != NULL) { + if (seat->pointer) { + create_wl_pointer(seat->pointer, output); + } } return wlr_output; diff --git a/backend/wayland/seat.c b/backend/wayland/seat.c index cce5c0dce..f20619d51 100644 --- a/backend/wayland/seat.c +++ b/backend/wayland/seat.c @@ -372,12 +372,49 @@ static struct wlr_wl_input_device *get_wl_input_device_from_input_device( return (struct wlr_wl_input_device *)wlr_dev; } +void create_wl_seat(struct wl_seat *wl_seat, struct wlr_wl_backend *wl) { + assert(!wl->seat); // only one seat supported at the moment + struct wlr_wl_seat *seat = calloc(1, sizeof(struct wlr_wl_seat)); + seat->wl_seat = wl_seat; + wl->seat = seat; + wl_seat_add_listener(wl_seat, &seat_listener, wl); +} + +void destroy_wl_seats(struct wlr_wl_backend *wl) { + struct wlr_wl_seat *seat = wl->seat; + if (!seat) { + return; + } + + if (seat->pointer) { + wl_pointer_destroy(seat->pointer); + } + free(seat->name); + if (seat->wl_seat) { + wl_seat_destroy(seat->wl_seat); + } + free(seat); +} + +static struct wlr_wl_seat *backend_get_seat(struct wlr_wl_backend *backend, struct wl_seat *wl_seat) { + assert(backend->seat && backend->seat->wl_seat == wl_seat); + return backend->seat; +} + +static struct wlr_wl_seat *input_device_get_seat(struct wlr_input_device *wlr_dev) { + struct wlr_wl_input_device *dev = + get_wl_input_device_from_input_device(wlr_dev); + assert(dev->backend->seat); + return dev->backend->seat; +} + static void input_device_destroy(struct wlr_input_device *wlr_dev) { struct wlr_wl_input_device *dev = get_wl_input_device_from_input_device(wlr_dev); if (dev->wlr_input_device.type == WLR_INPUT_DEVICE_KEYBOARD) { - wl_keyboard_release(dev->backend->keyboard); - dev->backend->keyboard = NULL; + struct wlr_wl_seat *seat = input_device_get_seat(wlr_dev); + wl_keyboard_release(seat->keyboard); + seat->keyboard = NULL; } wl_list_remove(&dev->wlr_input_device.link); free(dev); @@ -678,20 +715,20 @@ void create_wl_touch(struct wl_touch *wl_touch, struct wlr_wl_backend *wl) { static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, enum wl_seat_capability caps) { struct wlr_wl_backend *backend = data; - assert(backend->seat == wl_seat); + struct wlr_wl_seat *seat = backend_get_seat(backend, wl_seat); - if ((caps & WL_SEAT_CAPABILITY_POINTER) && backend->pointer == NULL) { + if ((caps & WL_SEAT_CAPABILITY_POINTER) && seat->pointer == NULL) { wlr_log(WLR_DEBUG, "seat %p offered pointer", (void *)wl_seat); struct wl_pointer *wl_pointer = wl_seat_get_pointer(wl_seat); - backend->pointer = wl_pointer; + seat->pointer = wl_pointer; struct wlr_wl_output *output; wl_list_for_each(output, &backend->outputs, link) { create_wl_pointer(wl_pointer, output); } } - if (!(caps & WL_SEAT_CAPABILITY_POINTER) && backend->pointer != NULL) { + if (!(caps & WL_SEAT_CAPABILITY_POINTER) && seat->pointer != NULL) { wlr_log(WLR_DEBUG, "seat %p dropped pointer", (void *)wl_seat); struct wlr_input_device *device, *tmp; @@ -701,21 +738,21 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, } } - wl_pointer_release(backend->pointer); - backend->pointer = NULL; + wl_pointer_release(seat->pointer); + seat->pointer = NULL; } - if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && backend->keyboard == NULL) { + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && seat->keyboard == NULL) { wlr_log(WLR_DEBUG, "seat %p offered keyboard", (void *)wl_seat); struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat); - backend->keyboard = wl_keyboard; + seat->keyboard = wl_keyboard; if (backend->started) { create_wl_keyboard(wl_keyboard, backend); } } - if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && backend->keyboard != NULL) { + if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && seat->keyboard != NULL) { wlr_log(WLR_DEBUG, "seat %p dropped keyboard", (void *)wl_seat); struct wlr_input_device *device, *tmp; @@ -724,18 +761,18 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, wlr_input_device_destroy(device); } } - assert(backend->keyboard == NULL); // free'ed by input_device_destroy + assert(seat->keyboard == NULL); // free'ed by input_device_destroy } - if ((caps & WL_SEAT_CAPABILITY_TOUCH) && backend->touch == NULL) { + if ((caps & WL_SEAT_CAPABILITY_TOUCH) && seat->touch == NULL) { wlr_log(WLR_DEBUG, "seat %p offered touch", (void *)wl_seat); - backend->touch = wl_seat_get_touch(wl_seat); + seat->touch = wl_seat_get_touch(wl_seat); if (backend->started) { - create_wl_touch(backend->touch, backend); + create_wl_touch(seat->touch, backend); } } - if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && backend->touch != NULL) { + if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && seat->touch != NULL) { wlr_log(WLR_DEBUG, "seat %p dropped touch", (void *)wl_seat); struct wlr_input_device *device, *tmp; @@ -745,18 +782,17 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, } } - wl_touch_release(backend->touch); - backend->touch = NULL; + wl_touch_release(seat->touch); + seat->touch = NULL; } } static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { struct wlr_wl_backend *backend = data; - assert(backend->seat == wl_seat); - // Do we need to check if seatName was previously set for name change? - free(backend->seat_name); - backend->seat_name = strdup(name); + struct wlr_wl_seat *seat = backend_get_seat(backend, wl_seat); + free(seat->name); + seat->name = strdup(name); } const struct wl_seat_listener seat_listener = { @@ -765,7 +801,5 @@ const struct wl_seat_listener seat_listener = { }; struct wl_seat *wlr_wl_input_device_get_seat(struct wlr_input_device *wlr_dev) { - struct wlr_wl_input_device *dev = - get_wl_input_device_from_input_device(wlr_dev); - return dev->backend->seat; + return input_device_get_seat(wlr_dev)->wl_seat; } diff --git a/include/backend/wayland.h b/include/backend/wayland.h index a053adedd..61b349a5e 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -38,13 +38,9 @@ struct wlr_wl_backend { struct wp_presentation *presentation; struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1; struct zwp_relative_pointer_manager_v1 *zwp_relative_pointer_manager_v1; - struct wl_seat *seat; - struct wl_touch *touch; - struct wl_pointer *pointer; - struct wl_keyboard *keyboard; + struct wlr_wl_seat *seat; struct wlr_wl_pointer *current_pointer; struct zwp_tablet_manager_v2 *tablet_manager; - char *seat_name; struct wlr_drm_format_set linux_dmabuf_v1_formats; }; @@ -108,6 +104,15 @@ struct wlr_wl_pointer { struct wl_listener output_destroy; }; +struct wlr_wl_seat { + struct wl_seat *wl_seat; + + char *name; + struct wl_touch *touch; + struct wl_pointer *pointer; + struct wl_keyboard *keyboard; +}; + struct wlr_wl_backend *get_wl_backend_from_backend(struct wlr_backend *backend); void update_wl_output_cursor(struct wlr_wl_output *output); struct wlr_wl_pointer *pointer_get_wl(struct wlr_pointer *wlr_pointer); @@ -116,6 +121,8 @@ void create_wl_keyboard(struct wl_keyboard *wl_keyboard, struct wlr_wl_backend * void create_wl_touch(struct wl_touch *wl_touch, struct wlr_wl_backend *wl); struct wlr_wl_input_device *create_wl_input_device( struct wlr_wl_backend *backend, enum wlr_input_device_type type); +void create_wl_seat(struct wl_seat *wl_seat, struct wlr_wl_backend *wl); +void destroy_wl_seats(struct wlr_wl_backend *wl); extern const struct wl_seat_listener seat_listener;