From 0f67580aab115e9667650d52104e3ea3a5dfe20b Mon Sep 17 00:00:00 2001 From: Kirill Primak Date: Thu, 22 Jun 2023 15:05:34 +0300 Subject: [PATCH] compositor: introduce wlr_surface_set_role_object() --- include/wlr/types/wlr_compositor.h | 19 ++++++++++++------- types/data_device/wlr_data_device.c | 3 +-- types/seat/wlr_seat_pointer.c | 2 +- types/tablet_v2/wlr_tablet_v2_tool.c | 2 +- types/wlr_compositor.c | 15 ++++++++++----- types/wlr_input_method_v2.c | 26 +++++++++++++++----------- types/wlr_layer_shell_v1.c | 4 +++- types/wlr_session_lock_v1.c | 4 +++- types/wlr_subcompositor.c | 4 +++- types/xdg_shell/wlr_xdg_popup.c | 4 +++- types/xdg_shell/wlr_xdg_toplevel.c | 4 +++- xwayland/shell.c | 4 +++- 12 files changed, 58 insertions(+), 33 deletions(-) diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index 6a8bebdd..c8b63a59 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -230,17 +230,22 @@ typedef void (*wlr_surface_iterator_func_t)(struct wlr_surface *surface, int sx, int sy, void *data); /** - * Set the lifetime role for this surface. Returns true on success or false if - * the role cannot be set. + * Set the lifetime role for this surface. * - * If the role is represented by an object, role_data must be non-NULL. - * Alternatively, if the role isn't represented by any object, role_data must - * be NULL. + * If the surface already has a different role and/or has a role object set, + * the function fails and sends an error to the client. + * + * Returns true on success, false otherwise. */ -bool wlr_surface_set_role(struct wlr_surface *surface, - const struct wlr_surface_role *role, void *role_data, +bool wlr_surface_set_role(struct wlr_surface *surface, const struct wlr_surface_role *role, struct wl_resource *error_resource, uint32_t error_code); +/** + * Set the role object for this surface. The surface must have a role and + * no already set role object. + */ +void wlr_surface_set_role_object(struct wlr_surface *surface, void *role_data); + /** * Destroy the object representing the surface's role. If it doesn't exist, * this function is no-op. diff --git a/types/data_device/wlr_data_device.c b/types/data_device/wlr_data_device.c index 808aeeb8..9a796356 100644 --- a/types/data_device/wlr_data_device.c +++ b/types/data_device/wlr_data_device.c @@ -65,7 +65,7 @@ static void data_device_start_drag(struct wl_client *client, struct wlr_surface *icon = NULL; if (icon_resource) { icon = wlr_surface_from_resource(icon_resource); - if (!wlr_surface_set_role(icon, &drag_icon_surface_role, NULL, + if (!wlr_surface_set_role(icon, &drag_icon_surface_role, icon_resource, WL_DATA_DEVICE_ERROR_ROLE)) { return; } @@ -102,7 +102,6 @@ static void data_device_handle_resource_destroy(struct wl_resource *resource) { wl_list_init(wl_resource_get_link(resource)); } - static void device_resource_send_selection(struct wl_resource *device_resource) { struct wlr_seat_client *seat_client = seat_client_from_data_device_resource(device_resource); diff --git a/types/seat/wlr_seat_pointer.c b/types/seat/wlr_seat_pointer.c index 830a885e..d5f0f9ff 100644 --- a/types/seat/wlr_seat_pointer.c +++ b/types/seat/wlr_seat_pointer.c @@ -96,7 +96,7 @@ static void pointer_set_cursor(struct wl_client *client, struct wlr_surface *surface = NULL; if (surface_resource != NULL) { surface = wlr_surface_from_resource(surface_resource); - if (!wlr_surface_set_role(surface, &pointer_cursor_surface_role, NULL, + if (!wlr_surface_set_role(surface, &pointer_cursor_surface_role, surface_resource, WL_POINTER_ERROR_ROLE)) { return; } diff --git a/types/tablet_v2/wlr_tablet_v2_tool.c b/types/tablet_v2/wlr_tablet_v2_tool.c index 96ce8f42..5f76954e 100644 --- a/types/tablet_v2/wlr_tablet_v2_tool.c +++ b/types/tablet_v2/wlr_tablet_v2_tool.c @@ -41,7 +41,7 @@ static void handle_tablet_tool_v2_set_cursor(struct wl_client *client, struct wlr_surface *surface = NULL; if (surface_resource != NULL) { surface = wlr_surface_from_resource(surface_resource); - if (!wlr_surface_set_role(surface, &tablet_tool_cursor_surface_role, NULL, + if (!wlr_surface_set_role(surface, &tablet_tool_cursor_surface_role, surface_resource, ZWP_TABLET_TOOL_V2_ERROR_ROLE)) { return; } diff --git a/types/wlr_compositor.c b/types/wlr_compositor.c index bc0fd4f6..21f22666 100644 --- a/types/wlr_compositor.c +++ b/types/wlr_compositor.c @@ -765,11 +765,9 @@ void wlr_surface_unmap(struct wlr_surface *surface) { } } -bool wlr_surface_set_role(struct wlr_surface *surface, - const struct wlr_surface_role *role, void *role_data, +bool wlr_surface_set_role(struct wlr_surface *surface, const struct wlr_surface_role *role, struct wl_resource *error_resource, uint32_t error_code) { assert(role != NULL); - assert((role_data == NULL) == role->no_object); if (surface->role != NULL && surface->role != role) { if (error_resource != NULL) { @@ -780,7 +778,7 @@ bool wlr_surface_set_role(struct wlr_surface *surface, } return false; } - if (surface->role_data != NULL && surface->role_data != role_data) { + if (surface->role_data != NULL) { wl_resource_post_error(error_resource, error_code, "Cannot reassign role %s to wl_surface@%" PRIu32 ", role object still exists", role->name, wl_resource_get_id(surface->resource)); @@ -788,10 +786,17 @@ bool wlr_surface_set_role(struct wlr_surface *surface, } surface->role = role; - surface->role_data = role_data; return true; } +void wlr_surface_set_role_object(struct wlr_surface *surface, void *role_data) { + assert(surface->role != NULL); + assert(!surface->role->no_object); + assert(surface->role_data == NULL); + assert(role_data != NULL); + surface->role_data = role_data; +} + void wlr_surface_destroy_role_object(struct wlr_surface *surface) { if (surface->role_data == NULL) { return; diff --git a/types/wlr_input_method_v2.c b/types/wlr_input_method_v2.c index ed8f4b16..856abb4f 100644 --- a/types/wlr_input_method_v2.c +++ b/types/wlr_input_method_v2.c @@ -191,30 +191,34 @@ static void im_get_input_popup_surface(struct wl_client *client, return; } - struct wl_resource *popup_resource = wl_resource_create( - client, &zwp_input_popup_surface_v2_interface, - wl_resource_get_version(resource), id); - if (!popup_resource) { - wl_client_post_no_memory(client); - return; - } - struct wlr_input_popup_surface_v2 *popup_surface = calloc(1, sizeof(struct wlr_input_popup_surface_v2)); if (!popup_surface) { wl_client_post_no_memory(client); return; } - wl_resource_set_implementation(popup_resource, &input_popup_impl, - popup_surface, popup_resource_destroy); struct wlr_surface *surface = wlr_surface_from_resource(surface_resource); if (!wlr_surface_set_role(surface, &input_popup_surface_v2_role, - popup_surface, resource, ZWP_INPUT_METHOD_V2_ERROR_ROLE)) { + resource, ZWP_INPUT_METHOD_V2_ERROR_ROLE)) { free(popup_surface); return; } + struct wl_resource *popup_resource = wl_resource_create( + client, &zwp_input_popup_surface_v2_interface, + wl_resource_get_version(resource), id); + if (!popup_resource) { + free(popup_surface); + wl_client_post_no_memory(client); + return; + } + + wl_resource_set_implementation(popup_resource, &input_popup_impl, + popup_surface, popup_resource_destroy); + + wlr_surface_set_role_object(surface, popup_surface); + popup_surface->resource = popup_resource; popup_surface->input_method = input_method; popup_surface->surface = surface; diff --git a/types/wlr_layer_shell_v1.c b/types/wlr_layer_shell_v1.c index 82b87bc9..b4348f7e 100644 --- a/types/wlr_layer_shell_v1.c +++ b/types/wlr_layer_shell_v1.c @@ -398,7 +398,7 @@ static void layer_shell_handle_get_layer_surface(struct wl_client *wl_client, return; } - if (!wlr_surface_set_role(wlr_surface, &layer_surface_role, surface, + if (!wlr_surface_set_role(wlr_surface, &layer_surface_role, client_resource, ZWLR_LAYER_SHELL_V1_ERROR_ROLE)) { free(surface); return; @@ -444,6 +444,8 @@ static void layer_shell_handle_get_layer_surface(struct wl_client *wl_client, surface, surface->resource); wl_resource_set_implementation(surface->resource, &layer_surface_implementation, surface, layer_surface_resource_destroy); + + wlr_surface_set_role_object(wlr_surface, surface); } static const struct zwlr_layer_shell_v1_interface layer_shell_implementation = { diff --git a/types/wlr_session_lock_v1.c b/types/wlr_session_lock_v1.c index cd7f6e7a..69205804 100644 --- a/types/wlr_session_lock_v1.c +++ b/types/wlr_session_lock_v1.c @@ -265,7 +265,7 @@ static void lock_handle_get_lock_surface(struct wl_client *client, return; } - if (!wlr_surface_set_role(surface, &lock_surface_role, lock_surface, + if (!wlr_surface_set_role(surface, &lock_surface_role, lock_resource, EXT_SESSION_LOCK_V1_ERROR_ROLE)) { free(lock_surface); return; @@ -274,6 +274,8 @@ static void lock_handle_get_lock_surface(struct wl_client *client, lock_surface->resource = lock_surface_resource; wl_resource_set_user_data(lock_surface_resource, lock_surface); + wlr_surface_set_role_object(surface, lock_surface); + wl_list_insert(&lock->surfaces, &lock_surface->link); lock_surface->output = output; diff --git a/types/wlr_subcompositor.c b/types/wlr_subcompositor.c index 248b7d17..51a7baa9 100644 --- a/types/wlr_subcompositor.c +++ b/types/wlr_subcompositor.c @@ -317,7 +317,7 @@ static void subcompositor_handle_get_subsurface(struct wl_client *client, return; } - if (!wlr_surface_set_role(surface, &subsurface_role, subsurface, + if (!wlr_surface_set_role(surface, &subsurface_role, resource, WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE)) { free(subsurface); return; @@ -344,6 +344,8 @@ static void subcompositor_handle_get_subsurface(struct wl_client *client, wl_resource_set_implementation(subsurface->resource, &subsurface_implementation, subsurface, subsurface_resource_destroy); + wlr_surface_set_role_object(surface, subsurface); + wl_signal_init(&subsurface->events.destroy); wl_signal_add(&surface->events.client_commit, diff --git a/types/xdg_shell/wlr_xdg_popup.c b/types/xdg_shell/wlr_xdg_popup.c index c5fdef8e..4ef83ebf 100644 --- a/types/xdg_shell/wlr_xdg_popup.c +++ b/types/xdg_shell/wlr_xdg_popup.c @@ -387,7 +387,7 @@ void create_xdg_popup(struct wlr_xdg_surface *surface, } if (!wlr_surface_set_role(surface->surface, &xdg_popup_surface_role, - surface, surface->resource, XDG_WM_BASE_ERROR_ROLE)) { + surface->resource, XDG_WM_BASE_ERROR_ROLE)) { return; } @@ -412,6 +412,8 @@ void create_xdg_popup(struct wlr_xdg_surface *surface, &xdg_popup_implementation, surface->popup, xdg_popup_handle_resource_destroy); + wlr_surface_set_role_object(surface->surface, surface); + surface->role = WLR_XDG_SURFACE_ROLE_POPUP; wlr_xdg_positioner_rules_get_geometry( diff --git a/types/xdg_shell/wlr_xdg_toplevel.c b/types/xdg_shell/wlr_xdg_toplevel.c index 7e6ff606..73a0f496 100644 --- a/types/xdg_shell/wlr_xdg_toplevel.c +++ b/types/xdg_shell/wlr_xdg_toplevel.c @@ -482,7 +482,7 @@ const struct wlr_surface_role xdg_toplevel_surface_role = { void create_xdg_toplevel(struct wlr_xdg_surface *surface, uint32_t id) { if (!wlr_surface_set_role(surface->surface, &xdg_toplevel_surface_role, - surface, surface->resource, XDG_WM_BASE_ERROR_ROLE)) { + surface->resource, XDG_WM_BASE_ERROR_ROLE)) { return; } @@ -524,6 +524,8 @@ void create_xdg_toplevel(struct wlr_xdg_surface *surface, &xdg_toplevel_implementation, surface->toplevel, xdg_toplevel_handle_resource_destroy); + wlr_surface_set_role_object(surface->surface, surface); + surface->role = WLR_XDG_SURFACE_ROLE_TOPLEVEL; } diff --git a/xwayland/shell.c b/xwayland/shell.c index 8d34ea04..d362cb7d 100644 --- a/xwayland/shell.c +++ b/xwayland/shell.c @@ -111,7 +111,7 @@ static void shell_handle_get_xwayland_surface(struct wl_client *client, return; } - if (!wlr_surface_set_role(surface, &xwl_surface_role, xwl_surface, + if (!wlr_surface_set_role(surface, &xwl_surface_role, shell_resource, XWAYLAND_SHELL_V1_ERROR_ROLE)) { free(xwl_surface); return; @@ -131,6 +131,8 @@ static void shell_handle_get_xwayland_surface(struct wl_client *client, wl_resource_set_implementation(xwl_surface->resource, &xwl_surface_impl, xwl_surface, xwl_surface_handle_resource_destroy); + wlr_surface_set_role_object(surface, xwl_surface); + wl_list_insert(&shell->surfaces, &xwl_surface->link); xwl_surface->surface_destroy.notify = xwl_surface_handle_surface_destroy;