[PATCH weston v2 2/7] libweston: Make weston_pointer destruction safe

Alexandros Frantzis alexandros.frantzis at collabora.com
Thu Feb 8 13:37:53 UTC 2018


Properly clean up all sub-objects (e.g., weston_pointer_client objects)
when a weston_pointer object is destroyed. The clean-up ensures that the
server is able to safely handle client requests to any associated
pointer resources, which, as a consenquence of a weston_pointer
destruction, have now become inert.

The clean-up involves, among other things, unsetting the destroyed
weston_pointer object from the user data of pointer resources, and
handling this NULL user data case where required. Note that in many
sites affected by this change the existing code already properly handles
NULL weston_pointer (e.g. in init_pointer_constraint), so there is no
need for additional updates there.

Signed-off-by: Alexandros Frantzis <alexandros.frantzis at collabora.com>
---
Changes in v2:
 - Removed NULL check before calling init_pointer_constraint, since
   init_pointer_constraint now handles this case.
 - Handle NULL weston_pointer in zoom functions.

 libweston/input.c | 35 ++++++++++++++++++++++++++++++-----
 libweston/zoom.c  |  5 ++++-
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/libweston/input.c b/libweston/input.c
index 390698c7..647268af 100644
--- a/libweston/input.c
+++ b/libweston/input.c
@@ -105,6 +105,19 @@ weston_pointer_client_create(struct wl_client *client)
 static void
 weston_pointer_client_destroy(struct weston_pointer_client *pointer_client)
 {
+	struct wl_resource *resource;
+
+	wl_resource_for_each(resource, &pointer_client->pointer_resources) {
+		wl_resource_set_user_data(resource, NULL);
+	}
+
+	wl_resource_for_each(resource,
+			     &pointer_client->relative_pointer_resources) {
+		wl_resource_set_user_data(resource, NULL);
+	}
+
+	wl_list_remove(&pointer_client->pointer_resources);
+	wl_list_remove(&pointer_client->relative_pointer_resources);
 	free(pointer_client);
 }
 
@@ -170,11 +183,14 @@ unbind_pointer_client_resource(struct wl_resource *resource)
 	struct wl_client *client = wl_resource_get_client(resource);
 	struct weston_pointer_client *pointer_client;
 
-	pointer_client = weston_pointer_get_pointer_client(pointer, client);
-	assert(pointer_client);
-
 	wl_list_remove(wl_resource_get_link(resource));
-	weston_pointer_cleanup_pointer_client(pointer, pointer_client);
+
+	if (pointer) {
+		pointer_client = weston_pointer_get_pointer_client(pointer,
+								   client);
+		assert(pointer_client);
+		weston_pointer_cleanup_pointer_client(pointer, pointer_client);
+	}
 }
 
 static void unbind_resource(struct wl_resource *resource)
@@ -1092,12 +1108,18 @@ weston_pointer_create(struct weston_seat *seat)
 WL_EXPORT void
 weston_pointer_destroy(struct weston_pointer *pointer)
 {
+	struct weston_pointer_client *pointer_client, *tmp;
+
 	wl_signal_emit(&pointer->destroy_signal, pointer);
 
 	if (pointer->sprite)
 		pointer_unmap_sprite(pointer);
 
-	/* XXX: What about pointer->resource_list? */
+	wl_list_for_each_safe(pointer_client, tmp, &pointer->pointer_clients,
+			      link) {
+		wl_list_remove(&pointer_client->link);
+		weston_pointer_client_destroy(pointer_client);
+	}
 
 	wl_list_remove(&pointer->focus_resource_listener.link);
 	wl_list_remove(&pointer->focus_view_listener.link);
@@ -2318,6 +2340,9 @@ pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
 	struct weston_pointer *pointer = wl_resource_get_user_data(resource);
 	struct weston_surface *surface = NULL;
 
+	if (!pointer)
+		return;
+
 	if (surface_resource)
 		surface = wl_resource_get_user_data(surface_resource);
 
diff --git a/libweston/zoom.c b/libweston/zoom.c
index 84f1a320..b89264f7 100644
--- a/libweston/zoom.c
+++ b/libweston/zoom.c
@@ -125,6 +125,9 @@ weston_output_update_zoom(struct weston_output *output)
 	struct weston_seat *seat = output->zoom.seat;
 	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
 
+	if (!pointer)
+		return;
+
 	assert(output->zoom.active);
 
 	output->zoom.current.x = wl_fixed_to_double(pointer->x);
@@ -151,7 +154,7 @@ weston_output_activate_zoom(struct weston_output *output,
 {
 	struct weston_pointer *pointer = weston_seat_get_pointer(seat);
 
-	if (output->zoom.active)
+	if (!pointer || output->zoom.active)
 		return;
 
 	output->zoom.active = true;
-- 
2.14.1



More information about the wayland-devel mailing list