diff options
Diffstat (limited to 'src/compositor-wayland.c')
-rw-r--r-- | src/compositor-wayland.c | 552 |
1 files changed, 501 insertions, 51 deletions
diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c index 899c3293..f35db9c3 100644 --- a/src/compositor-wayland.c +++ b/src/compositor-wayland.c @@ -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,13 +54,17 @@ 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; int use_pixman; + int sprawl_across_outputs; struct theme *theme; cairo_device_t *frame_device; @@ -75,6 +80,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 +112,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; @@ -135,6 +167,7 @@ struct wayland_input { } cursor; } parent; + enum weston_key_state_update keyboard_state_update; uint32_t key_serial; uint32_t enter_serial; int focus; @@ -211,13 +244,13 @@ wayland_output_get_shm_buffer(struct wayland_output *output) fd = os_create_anonymous_file(height * stride); if (fd < 0) { - perror("os_create_anonymous_file"); + weston_log("could not create an anonymous file buffer: %m\n"); return NULL; } data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { - perror("mmap"); + weston_log("could not mmap %d memory for data: %m\n", height * stride); close(fd); return NULL; } @@ -289,10 +322,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 +585,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); @@ -593,7 +626,9 @@ wayland_output_init_gl_renderer(struct wayland_output *output) } if (gl_renderer->output_create(&output->base, - output->gl.egl_window) < 0) + output->gl.egl_window, + gl_renderer->alpha_attribs, + NULL) < 0) goto cleanup_window; return 0; @@ -736,6 +771,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; @@ -743,8 +781,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 * @@ -776,15 +983,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); @@ -825,7 +1035,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); @@ -833,7 +1043,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: @@ -921,6 +1132,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) @@ -966,7 +1242,8 @@ input_set_cursor(struct wayland_input *input) image = input->compositor->cursor->images[0]; buffer = wl_cursor_image_get_buffer(image); - + if (!buffer) + return; wl_pointer_set_cursor(input->parent.pointer, input->enter_serial, input->parent.cursor.surface, @@ -1141,40 +1418,52 @@ input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format, struct xkb_keymap *keymap; char *map_str; - if (!data) { - close(fd); - return; - } + if (!data) + goto error; - if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { - close(fd); - return; - } + if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { + map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (map_str == MAP_FAILED) { + weston_log("mmap failed: %m\n"); + goto error; + } - map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); - if (map_str == MAP_FAILED) { - close(fd); - return; - } + keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, + map_str, + XKB_KEYMAP_FORMAT_TEXT_V1, + 0); + munmap(map_str, size); - keymap = xkb_map_new_from_string(input->compositor->base.xkb_context, - map_str, - XKB_KEYMAP_FORMAT_TEXT_V1, - 0); - munmap(map_str, size); - close(fd); + if (!keymap) { + weston_log("failed to compile keymap\n"); + goto error; + } - if (!keymap) { - weston_log("failed to compile keymap\n"); - return; + input->keyboard_state_update = STATE_UPDATE_NONE; + } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) { + weston_log("No keymap provided; falling back to defalt\n"); + keymap = NULL; + input->keyboard_state_update = STATE_UPDATE_AUTOMATIC; + } else { + weston_log("Invalid keymap\n"); + goto error; } + close(fd); + if (input->base.keyboard) weston_seat_update_keymap(&input->base, keymap); else weston_seat_init_keyboard(&input->base, keymap); - xkb_map_unref(keymap); + if (keymap) + xkb_map_unref(keymap); + + return; + +error: + wl_keyboard_release(input->parent.keyboard); + close(fd); } static void @@ -1249,7 +1538,7 @@ input_handle_key(void *data, struct wl_keyboard *keyboard, notify_key(&input->base, time, key, state ? WL_KEYBOARD_KEY_STATE_PRESSED : WL_KEYBOARD_KEY_STATE_RELEASED, - STATE_UPDATE_NONE); + input->keyboard_state_update); } static void @@ -1339,6 +1628,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->sprawl_across_outputs) { + 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) { @@ -1352,16 +1765,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 @@ -1426,6 +1858,10 @@ create_cursor(struct wayland_compositor *c, struct weston_config *config) weston_config_section_get_int(s, "cursor-size", &size, 32); c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm); + if (!c->cursor_theme) { + fprintf(stderr, "could not load cursor theme\n"); + return; + } free(theme); @@ -1485,6 +1921,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, ®istry_listener, c); @@ -1534,10 +1971,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); @@ -1577,8 +2010,9 @@ 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; + int x, count, width, height, scale, use_pixman, fullscreen, sprawl; const char *section_name, *display_name; char *name; @@ -1590,6 +2024,7 @@ backend_init(struct wl_display *display, int *argc, char *argv[], { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman }, { WESTON_OPTION_INTEGER, "output-count", 0, &count }, { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen }, + { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl }, }; width = 0; @@ -1599,6 +2034,7 @@ backend_init(struct wl_display *display, int *argc, char *argv[], use_pixman = 0; count = 1; fullscreen = 0; + sprawl = 0; parse_options(wayland_options, ARRAY_LENGTH(wayland_options), argc, argv); @@ -1607,6 +2043,16 @@ backend_init(struct wl_display *display, int *argc, char *argv[], if (!c) return NULL; + if (sprawl || c->parent.fshell) { + c->sprawl_across_outputs = 1; + 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); @@ -1661,6 +2107,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: |