tests/shell: get rid of static variables
authorPekka Paalanen <pekka.paalanen@collabora.co.uk>
Fri, 27 Jan 2017 16:30:24 +0000 (17:30 +0100)
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>
Tue, 7 Feb 2017 11:26:06 +0000 (13:26 +0200)
Stop using static variables and clean up when we're done.

[Emilio: update to latest weston_layer API]

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Signed-off-by: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
Reviewed-by: Micah Fedke <micah.fedke@collabora.co.uk>
Reviewed-by: Quentin Glidic <sardemff7+git@sardemff7.net>
[Pekka: fix build after previous commit's fix]

tests/weston-test-desktop-shell.c

index 591a626..2cf2271 100644 (file)
 #define static_assert(cond, msg)
 #endif
 
-static struct weston_desktop *desktop = NULL;
-static struct weston_layer background_layer;
-static struct weston_surface *background_surface = NULL;
-static struct weston_view *background_view = NULL;
-static struct weston_layer layer;
-static struct weston_view *view = NULL;
-/*
- * libweston-desktop
- */
+struct desktest_shell {
+       struct wl_listener compositor_destroy_listener;
+       struct weston_desktop *desktop;
+       struct weston_layer background_layer;
+       struct weston_surface *background_surface;
+       struct weston_view *background_view;
+       struct weston_layer layer;
+       struct weston_view *view;
+};
 
 static void
 desktop_surface_added(struct weston_desktop_surface *desktop_surface,
                      void *shell)
 {
+       struct desktest_shell *dts = shell;
 
-       assert(!view);
+       assert(!dts->view);
 
-       view = weston_desktop_surface_create_view(desktop_surface);
+       dts->view = weston_desktop_surface_create_view(desktop_surface);
 
-       assert(view);
+       assert(dts->view);
 }
 
 static void
 desktop_surface_removed(struct weston_desktop_surface *desktop_surface,
                        void *shell)
 {
-       assert(view);
+       struct desktest_shell *dts = shell;
 
-       weston_desktop_surface_unlink_view(view);
-       weston_view_destroy(view);
-       view = NULL;
+       assert(dts->view);
+
+       weston_desktop_surface_unlink_view(dts->view);
+       weston_view_destroy(dts->view);
+       dts->view = NULL;
 }
 
 static void
 desktop_surface_committed(struct weston_desktop_surface *desktop_surface,
-                         int32_t sx, int32_t sy, void *data)
+                         int32_t sx, int32_t sy, void *shell)
 {
+       struct desktest_shell *dts = shell;
        struct weston_surface *surface =
                weston_desktop_surface_get_surface(desktop_surface);
        struct weston_geometry geometry =
                weston_desktop_surface_get_geometry(desktop_surface);
 
-       assert(view);
+       assert(dts->view);
 
        if (weston_surface_is_mapped(surface))
                return;
 
        surface->is_mapped = true;
-       weston_layer_entry_insert(&layer.view_list, &view->layer_link);
-       weston_view_set_position(view, 0 - geometry.x, 0 - geometry.y);
-       weston_view_update_transform(view);
-       view->is_mapped = true;
+       weston_layer_entry_insert(&dts->layer.view_list, &dts->view->layer_link);
+       weston_view_set_position(dts->view, 0 - geometry.x, 0 - geometry.y);
+       weston_view_update_transform(dts->view);
+       dts->view->is_mapped = true;
 }
 
 static void
@@ -157,42 +161,74 @@ static const struct weston_desktop_api shell_desktop_api = {
        .pong = desktop_surface_pong,
 };
 
-/* ************************ *
- * end of libweston-desktop *
- * ************************ */
+static void
+shell_destroy(struct wl_listener *listener, void *data)
+{
+       struct desktest_shell *dts;
+
+       dts = container_of(listener, struct desktest_shell,
+                          compositor_destroy_listener);
+
+       weston_desktop_destroy(dts->desktop);
+       weston_view_destroy(dts->background_view);
+       weston_surface_destroy(dts->background_surface);
+       free(dts);
+}
 
 WL_EXPORT int
 wet_shell_init(struct weston_compositor *ec,
               int *argc, char *argv[])
 {
-       weston_layer_init(&layer, ec);
-       weston_layer_init(&background_layer, ec);
+       struct desktest_shell *dts;
+
+       dts = zalloc(sizeof *dts);
+       if (!dts)
+               return -1;
 
-       weston_layer_set_position(&layer,
+       dts->compositor_destroy_listener.notify = shell_destroy;
+       wl_signal_add(&ec->destroy_signal, &dts->compositor_destroy_listener);
+
+       weston_layer_init(&dts->layer, ec);
+       weston_layer_init(&dts->background_layer, ec);
+
+       weston_layer_set_position(&dts->layer,
                                  WESTON_LAYER_POSITION_NORMAL);
-       weston_layer_set_position(&background_layer,
+       weston_layer_set_position(&dts->background_layer,
                                  WESTON_LAYER_POSITION_BACKGROUND);
 
-       background_surface = weston_surface_create(ec);
-       if (background_surface == NULL)
-               return -1;
-       background_view = weston_view_create(background_surface);
-       if (background_view == NULL) {
-               weston_surface_destroy(background_surface);
-               return -1;
-       }
+       dts->background_surface = weston_surface_create(ec);
+       if (dts->background_surface == NULL)
+               goto out_free;
+
+       dts->background_view = weston_view_create(dts->background_surface);
+       if (dts->background_view == NULL)
+               goto out_surface;
+
+       weston_surface_set_color(dts->background_surface, 0.0, 0.0, 0.0, 1);
+       pixman_region32_fini(&dts->background_surface->opaque);
+       pixman_region32_init_rect(&dts->background_surface->opaque, 0, 0, 2000, 2000);
+       pixman_region32_fini(&dts->background_surface->input);
+       pixman_region32_init_rect(&dts->background_surface->input, 0, 0, 2000, 2000);
+
+       weston_surface_set_size(dts->background_surface, 2000, 2000);
+       weston_view_set_position(dts->background_view, 0, 0);
+       weston_layer_entry_insert(&dts->background_layer.view_list, &dts->background_view->layer_link);
+       weston_view_update_transform(dts->background_view);
+
+       dts->desktop = weston_desktop_create(ec, &shell_desktop_api, dts);
+       if (dts->desktop == NULL)
+               goto out_view;
+
+       return 0;
+
+out_view:
+       weston_view_destroy(dts->background_view);
 
-       weston_surface_set_color(background_surface, 0.0, 0.0, 0.0, 1);
-       pixman_region32_fini(&background_surface->opaque);
-       pixman_region32_init_rect(&background_surface->opaque, 0, 0, 2000, 2000);
-       pixman_region32_fini(&background_surface->input);
-       pixman_region32_init_rect(&background_surface->input, 0, 0, 2000, 2000);
+out_surface:
+       weston_surface_destroy(dts->background_surface);
 
-       weston_surface_set_size(background_surface, 2000, 2000);
-       weston_view_set_position(background_view, 0, 0);
-       weston_layer_entry_insert(&background_layer.view_list, &background_view->layer_link);
-       weston_view_update_transform(background_view);
+out_free:
+       free(dts);
 
-       desktop = weston_desktop_create(ec, &shell_desktop_api, NULL);
-       return desktop ? 0 : -1;
+       return -1;
 }