libweston: Make weston_pointer destruction safe
authorAlexandros Frantzis <alexandros.frantzis@collabora.com>
Thu, 8 Feb 2018 13:37:53 +0000 (15:37 +0200)
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>
Wed, 14 Feb 2018 13:14:16 +0000 (15:14 +0200)
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@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
libweston/input.c
libweston/zoom.c

index 390698c..647268a 100644 (file)
@@ -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);
 
index 84f1a32..b89264f 100644 (file)
@@ -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;