compositor-wayland: Add support for running on top of wl_fullscreen_shell
authorJason Ekstrand <jason@jlekstrand.net>
Thu, 3 Apr 2014 00:53:54 +0000 (19:53 -0500)
committerKristian Høgsberg <krh@bitplanet.net>
Thu, 3 Apr 2014 04:26:39 +0000 (21:26 -0700)
Signed-off-by: Jason Ekstrand <jason@jlekstrand.net>
Makefile.am
src/compositor-wayland.c

index 464d086..a2f9038 100644 (file)
@@ -241,6 +241,9 @@ wayland_backend_la_CFLAGS =                 \
        $(WAYLAND_COMPOSITOR_CFLAGS)            \
        $(GCC_CFLAGS)
 wayland_backend_la_SOURCES = src/compositor-wayland.c
+nodist_wayland_backend_la_SOURCES =                    \
+       protocol/fullscreen-shell-protocol.c            \
+       protocol/fullscreen-shell-client-protocol.h
 endif
 
 if ENABLE_RPI_COMPOSITOR
index 796f6a0..49f74ce 100644 (file)
@@ -42,6 +42,7 @@
 #include "../shared/image-loader.h"
 #include "../shared/os-compatibility.h"
 #include "../shared/cairo-util.h"
+#include "fullscreen-shell-client-protocol.h"
 
 #define WINDOW_TITLE "Weston Compositor"
 
@@ -53,8 +54,11 @@ struct wayland_compositor {
                struct wl_registry *registry;
                struct wl_compositor *compositor;
                struct wl_shell *shell;
+               struct _wl_fullscreen_shell *fshell;
                struct wl_shm *shm;
 
+               struct wl_list output_list;
+
                struct wl_event_source *wl_source;
                uint32_t event_mask;
        } parent;
@@ -75,6 +79,10 @@ struct wayland_output {
        struct {
                int draw_initial_frame;
                struct wl_surface *surface;
+
+               struct wl_output *output;
+               uint32_t global_id;
+
                struct wl_shell_surface *shell_surface;
                int configure_width, configure_height;
        } parent;
@@ -103,6 +111,29 @@ struct wayland_output {
        uint32_t scale;
 };
 
+struct wayland_parent_output {
+       struct wayland_output *output;
+       struct wl_list link;
+
+       struct wl_output *global;
+       uint32_t id;
+
+       struct {
+               char *make;
+               char *model;
+               int32_t width, height;
+               uint32_t subpixel;
+       } physical;
+
+       int32_t x, y;
+       uint32_t transform;
+       uint32_t scale;
+
+       struct wl_list mode_list;
+       struct weston_mode *preferred_mode;
+       struct weston_mode *current_mode;
+};
+
 struct wayland_shm_buffer {
        struct wayland_output *output;
        struct wl_list link;
@@ -289,10 +320,9 @@ draw_initial_frame(struct wayland_output *output)
                sb->output = NULL;
 
        wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
-
-       /* We only need to damage some part, as its only transparant
-        * pixels anyway. */
-       wl_surface_damage(output->parent.surface, 0, 0, 1, 1);
+       wl_surface_damage(output->parent.surface, 0, 0,
+                         output->base.current_mode->width,
+                         output->base.current_mode->height);
 }
 
 static void
@@ -553,7 +583,8 @@ wayland_output_destroy(struct weston_output *output_base)
 
        wl_egl_window_destroy(output->gl.egl_window);
        wl_surface_destroy(output->parent.surface);
-       wl_shell_surface_destroy(output->parent.shell_surface);
+       if (output->parent.shell_surface)
+               wl_shell_surface_destroy(output->parent.shell_surface);
 
        if (output->frame)
                frame_destroy(output->frame);
@@ -738,6 +769,9 @@ wayland_output_set_fullscreen(struct wayland_output *output,
                              enum wl_shell_surface_fullscreen_method method,
                              uint32_t framerate, struct wl_output *target)
 {
+       struct wayland_compositor *c =
+               (struct wayland_compositor *)output->base.compositor;
+
        if (output->frame) {
                frame_destroy(output->frame);
                output->frame = NULL;
@@ -745,8 +779,177 @@ wayland_output_set_fullscreen(struct wayland_output *output,
 
        wayland_output_resize_surface(output);
 
-       wl_shell_surface_set_fullscreen(output->parent.shell_surface,
-                                       method, framerate, target);
+       if (output->parent.shell_surface) {
+               wl_shell_surface_set_fullscreen(output->parent.shell_surface,
+                                               method, framerate, target);
+       } else if (c->parent.fshell) {
+               _wl_fullscreen_shell_present_surface(c->parent.fshell,
+                                                    output->parent.surface,
+                                                    method, target);
+       }
+}
+
+static struct weston_mode *
+wayland_output_choose_mode(struct wayland_output *output,
+                          struct weston_mode *ref_mode)
+{
+       struct weston_mode *mode;
+
+       /* First look for an exact match */
+       wl_list_for_each(mode, &output->base.mode_list, link)
+               if (mode->width == ref_mode->width &&
+                   mode->height == ref_mode->height &&
+                   mode->refresh == ref_mode->refresh)
+                       return mode;
+
+       /* If we can't find an exact match, ignore refresh and try again */
+       wl_list_for_each(mode, &output->base.mode_list, link)
+               if (mode->width == ref_mode->width &&
+                   mode->height == ref_mode->height)
+                       return mode;
+
+       /* Yeah, we failed */
+       return NULL;
+}
+
+enum mode_status {
+       MODE_STATUS_UNKNOWN,
+       MODE_STATUS_SUCCESS,
+       MODE_STATUS_FAIL,
+       MODE_STATUS_CANCEL,
+};
+
+static void
+mode_feedback_successful(void *data,
+                        struct _wl_fullscreen_shell_mode_feedback *fb)
+{
+       enum mode_status *value = data;
+
+       printf("Mode switch successful\n");
+
+       *value = MODE_STATUS_SUCCESS;
+}
+
+static void
+mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
+{
+       enum mode_status *value = data;
+
+       printf("Mode switch failed\n");
+
+       *value = MODE_STATUS_FAIL;
+}
+
+static void
+mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
+{
+       enum mode_status *value = data;
+
+       printf("Mode switch cancelled\n");
+
+       *value = MODE_STATUS_CANCEL;
+}
+
+struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
+       mode_feedback_successful,
+       mode_feedback_failed,
+       mode_feedback_cancelled,
+};
+
+static int
+wayland_output_switch_mode(struct weston_output *output_base,
+                          struct weston_mode *mode)
+{
+       struct wayland_output *output = (struct wayland_output *) output_base;
+       struct wayland_compositor *c;
+       struct wl_surface *old_surface;
+       struct weston_mode *old_mode;
+       struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
+       enum mode_status mode_status;
+       int ret = 0;
+
+       if (output_base == NULL) {
+               weston_log("output is NULL.\n");
+               return -1;
+       }
+
+       if (mode == NULL) {
+               weston_log("mode is NULL.\n");
+               return -1;
+       }
+
+       c = (struct wayland_compositor *)output_base->compositor;
+
+       if (output->parent.shell_surface || !c->parent.fshell)
+               return -1;
+
+       mode = wayland_output_choose_mode(output, mode);
+       if (mode == NULL)
+               return -1;
+
+       if (output->base.current_mode == mode)
+               return 0;
+
+       old_mode = output->base.current_mode;
+       old_surface = output->parent.surface;
+       output->base.current_mode = mode;
+       output->parent.surface =
+               wl_compositor_create_surface(c->parent.compositor);
+       wl_surface_set_user_data(output->parent.surface, output);
+
+       /* Blow the old buffers because we changed size/surfaces */
+       wayland_output_resize_surface(output);
+
+       mode_feedback =
+               _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
+                                                             output->parent.surface,
+                                                             output->parent.output,
+                                                             mode->refresh);
+       _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
+                                                       &mode_feedback_listener,
+                                                       &mode_status);
+
+       /* This should kick-start things again */
+       output->parent.draw_initial_frame = 1;
+       wayland_output_start_repaint_loop(&output->base);
+
+       mode_status = MODE_STATUS_UNKNOWN;
+       while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
+               ret = wl_display_dispatch(c->parent.wl_display);
+
+       _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
+
+       if (mode_status == MODE_STATUS_FAIL) {
+               output->base.current_mode = old_mode;
+               wl_surface_destroy(output->parent.surface);
+               output->parent.surface = old_surface;
+               wayland_output_resize_surface(output);
+
+               return -1;
+       }
+
+       old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
+       output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
+
+       if (c->use_pixman) {
+               pixman_renderer_output_destroy(output_base);
+               if (wayland_output_init_pixman_renderer(output) < 0)
+                       goto err_output;
+       } else {
+               gl_renderer->output_destroy(output_base);
+               wl_egl_window_destroy(output->gl.egl_window);
+               if (wayland_output_init_gl_renderer(output) < 0)
+                       goto err_output;
+       }
+       wl_surface_destroy(old_surface);
+
+       weston_output_schedule_repaint(&output->base);
+
+       return 0;
+
+err_output:
+       /* XXX */
+       return -1;
 }
 
 static struct wayland_output *
@@ -778,15 +981,18 @@ wayland_output_create(struct wayland_compositor *c, int x, int y,
        wl_surface_set_user_data(output->parent.surface, output);
 
        output->parent.draw_initial_frame = 1;
-       output->parent.shell_surface =
-               wl_shell_get_shell_surface(c->parent.shell,
-                                          output->parent.surface);
-       if (!output->parent.shell_surface)
-               goto err_surface;
-       wl_shell_surface_add_listener(output->parent.shell_surface,
-                                     &shell_surface_listener, output);
 
-       if (fullscreen) {
+       if (c->parent.shell) {
+               output->parent.shell_surface =
+                       wl_shell_get_shell_surface(c->parent.shell,
+                                                  output->parent.surface);
+               if (!output->parent.shell_surface)
+                       goto err_surface;
+               wl_shell_surface_add_listener(output->parent.shell_surface,
+                                             &shell_surface_listener, output);
+       }
+
+       if (fullscreen && c->parent.shell) {
                wl_shell_surface_set_fullscreen(output->parent.shell_surface,
                                                0, 0, NULL);
                wl_display_roundtrip(c->parent.wl_display);
@@ -827,7 +1033,7 @@ wayland_output_create(struct wayland_compositor *c, int x, int y,
        output->base.assign_planes = NULL;
        output->base.set_backlight = NULL;
        output->base.set_dpms = NULL;
-       output->base.switch_mode = NULL;
+       output->base.switch_mode = wayland_output_switch_mode;
 
        wl_list_insert(c->base.output_list.prev, &output->base.link);
 
@@ -835,7 +1041,8 @@ wayland_output_create(struct wayland_compositor *c, int x, int y,
 
 err_output:
        weston_output_destroy(&output->base);
-       wl_shell_surface_destroy(output->parent.shell_surface);
+       if (output->parent.shell_surface)
+               wl_shell_surface_destroy(output->parent.shell_surface);
 err_surface:
        wl_surface_destroy(output->parent.surface);
 err_name:
@@ -923,6 +1130,71 @@ wayland_output_create_for_config(struct wayland_compositor *c,
        return output;
 }
 
+static struct wayland_output *
+wayland_output_create_for_parent_output(struct wayland_compositor *c,
+                                       struct wayland_parent_output *poutput)
+{
+       struct wayland_output *output;
+       struct weston_mode *mode;
+       int32_t x;
+
+       if (poutput->current_mode) {
+               mode = poutput->current_mode;
+       } else if (poutput->preferred_mode) {
+               mode = poutput->current_mode;
+       } else if (!wl_list_empty(&poutput->mode_list)) {
+               mode = container_of(poutput->mode_list.next,
+                                   struct weston_mode, link);
+       } else {
+               weston_log("No valid modes found.  Skipping output");
+               return NULL;
+       }
+
+       if (!wl_list_empty(&c->base.output_list)) {
+               output = container_of(c->base.output_list.prev,
+                                     struct wayland_output, base.link);
+               x = output->base.x + output->base.current_mode->width;
+       } else {
+               x = 0;
+       }
+
+       output = wayland_output_create(c, x, 0, mode->width, mode->height,
+                                      NULL, 0,
+                                      WL_OUTPUT_TRANSFORM_NORMAL, 1);
+       if (!output)
+               return NULL;
+
+       output->parent.output = poutput->global;
+
+       output->base.make = poutput->physical.make;
+       output->base.model = poutput->physical.model;
+       wl_list_init(&output->base.mode_list);
+       wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
+       wl_list_init(&poutput->mode_list);
+
+       wayland_output_set_fullscreen(output,
+                                     WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
+                                     mode->refresh, poutput->global);
+
+       if (output->parent.shell_surface) {
+               wl_shell_surface_set_fullscreen(output->parent.shell_surface,
+                                               WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
+                                               mode->refresh, poutput->global);
+       } else if (c->parent.fshell) {
+               _wl_fullscreen_shell_present_surface(c->parent.fshell,
+                                                    output->parent.surface,
+                                                    _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
+                                                    poutput->global);
+               _wl_fullscreen_shell_mode_feedback_destroy(
+                       _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
+                                                                     output->parent.surface,
+                                                                     poutput->global,
+                                                                     mode->refresh));
+       }
+
+       return output;
+}
+
 static void
 shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
                   uint32_t serial)
@@ -1342,6 +1614,130 @@ display_add_seat(struct wayland_compositor *c, uint32_t id)
 }
 
 static void
+wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
+                              int32_t x, int32_t y,
+                              int32_t physical_width, int32_t physical_height,
+                              int32_t subpixel, const char *make,
+                              const char *model, int32_t transform)
+{
+       struct wayland_parent_output *output = data;
+
+       output->x = x;
+       output->y = y;
+       output->physical.width = physical_width;
+       output->physical.height = physical_height;
+       output->physical.subpixel = subpixel;
+
+       free(output->physical.make);
+       output->physical.make = strdup(make);
+       free(output->physical.model);
+       output->physical.model = strdup(model);
+
+       output->transform = transform;
+}
+
+static struct weston_mode *
+find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
+{
+       struct weston_mode *mode;
+
+       wl_list_for_each(mode, list, link) {
+               if (mode->width == width && mode->height == height &&
+                   mode->refresh == refresh)
+                       return mode;
+       }
+
+       mode = zalloc(sizeof *mode);
+       if (!mode)
+               return NULL;
+
+       mode->width = width;
+       mode->height = height;
+       mode->refresh = refresh;
+       wl_list_insert(list, &mode->link);
+
+       return mode;
+}
+
+static void
+wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
+                          uint32_t flags, int32_t width, int32_t height,
+                          int32_t refresh)
+{
+       struct wayland_parent_output *output = data;
+       struct weston_mode *mode;
+
+       if (output->output) {
+               mode = find_mode(&output->output->base.mode_list,
+                                width, height, refresh);
+               if (!mode)
+                       return;
+               mode->flags = flags;
+               /* Do a mode-switch on current mode change? */
+       } else {
+               mode = find_mode(&output->mode_list, width, height, refresh);
+               if (!mode)
+                       return;
+               mode->flags = flags;
+               if (flags & WL_OUTPUT_MODE_CURRENT)
+                       output->current_mode = mode;
+               if (flags & WL_OUTPUT_MODE_PREFERRED)
+                       output->preferred_mode = mode;
+       }
+}
+
+static const struct wl_output_listener output_listener = {
+       wayland_parent_output_geometry,
+       wayland_parent_output_mode
+};
+
+static void
+wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
+{
+       struct wayland_parent_output *output;
+
+       output = zalloc(sizeof *output);
+       if (!output)
+               return;
+
+       output->id = id;
+       output->global = wl_registry_bind(c->parent.registry, id,
+                                         &wl_output_interface, 1);
+       if (!output->global)
+               return;
+       wl_output_add_listener(output->global, &output_listener, output);
+
+       output->scale = 0;
+       output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+       output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
+       wl_list_init(&output->mode_list);
+       wl_list_insert(&c->parent.output_list, &output->link);
+
+       if (c->parent.fshell) {
+               wl_display_roundtrip(c->parent.wl_display);
+               wayland_output_create_for_parent_output(c, output);
+       }
+}
+
+static void
+wayland_parent_output_destroy(struct wayland_parent_output *output)
+{
+       struct weston_mode *mode, *next;
+
+       if (output->output)
+               wayland_output_destroy(&output->output->base);
+
+       wl_output_destroy(output->global);
+       free(output->physical.make);
+       free(output->physical.model);
+
+       wl_list_for_each_safe(mode, next, &output->mode_list, link) {
+               wl_list_remove(&mode->link);
+               free(mode);
+       }
+}
+
+static void
 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
                       const char *interface, uint32_t version)
 {
@@ -1355,16 +1751,35 @@ registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
                c->parent.shell =
                        wl_registry_bind(registry, name,
                                         &wl_shell_interface, 1);
+       } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
+               c->parent.fshell =
+                       wl_registry_bind(registry, name,
+                                        &_wl_fullscreen_shell_interface, 1);
        } else if (strcmp(interface, "wl_seat") == 0) {
                display_add_seat(c, name);
+       } else if (strcmp(interface, "wl_output") == 0) {
+               wayland_compositor_register_output(c, name);
        } else if (strcmp(interface, "wl_shm") == 0) {
                c->parent.shm =
                        wl_registry_bind(registry, name, &wl_shm_interface, 1);
        }
 }
 
+static void
+registry_handle_global_remove(void *data, struct wl_registry *registry,
+                             uint32_t name)
+{
+       struct wayland_compositor *c = data;
+       struct wayland_parent_output *output;
+
+       wl_list_for_each(output, &c->parent.output_list, link)
+               if (output->id == name)
+                       wayland_parent_output_destroy(output);
+}
+
 static const struct wl_registry_listener registry_listener = {
-       registry_handle_global
+       registry_handle_global,
+       registry_handle_global_remove
 };
 
 static int
@@ -1492,6 +1907,7 @@ wayland_compositor_create(struct wl_display *display, int use_pixman,
                goto err_compositor;
        }
 
+       wl_list_init(&c->parent.output_list);
        wl_list_init(&c->input_list);
        c->parent.registry = wl_display_get_registry(c->parent.wl_display);
        wl_registry_add_listener(c->parent.registry, &registry_listener, c);
@@ -1541,10 +1957,6 @@ wayland_compositor_create(struct wl_display *display, int use_pixman,
 
        wl_event_source_check(c->parent.wl_source);
 
-       weston_compositor_add_key_binding(&c->base, KEY_F,
-                                         MODIFIER_CTRL | MODIFIER_ALT,
-                                         fullscreen_binding, c);
-
        return c;
 err_renderer:
        c->base.renderer->destroy(&c->base);
@@ -1584,6 +1996,7 @@ backend_init(struct wl_display *display, int *argc, char *argv[],
 {
        struct wayland_compositor *c;
        struct wayland_output *output;
+       struct wayland_parent_output *poutput;
        struct weston_config_section *section;
        int x, count, width, height, scale, use_pixman, fullscreen;
        const char *section_name, *display_name;
@@ -1614,6 +2027,15 @@ backend_init(struct wl_display *display, int *argc, char *argv[],
        if (!c)
                return NULL;
 
+       if (c->parent.fshell) {
+               wl_display_roundtrip(c->parent.wl_display);
+
+               wl_list_for_each(poutput, &c->parent.output_list, link)
+                       wayland_output_create_for_parent_output(c, poutput);
+
+               return &c->base;
+       }
+
        if (fullscreen) {
                output = wayland_output_create(c, 0, 0, width, height,
                                               NULL, 1, 0, 1);
@@ -1668,6 +2090,10 @@ backend_init(struct wl_display *display, int *argc, char *argv[],
                --count;
        }
 
+       weston_compositor_add_key_binding(&c->base, KEY_F,
+                                         MODIFIER_CTRL | MODIFIER_ALT,
+                                         fullscreen_binding, c);
+
        return &c->base;
 
 err_outputs: