diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c index 6e4a5b4315..400dc9cb32 100644 --- a/gfx/drivers_context/wayland_ctx.c +++ b/gfx/drivers_context/wayland_ctx.c @@ -59,6 +59,9 @@ static enum gfx_ctx_api wl_api = GFX_CTX_NONE; #define EGL_PLATFORM_WAYLAND_KHR 0x31D8 #endif +#define DEFAULT_WINDOWED_WIDTH 640 +#define DEFAULT_WINDOWED_HEIGHT 480 + static void handle_toplevel_config_common(void *data, void *toplevel, int32_t width, int32_t height, struct wl_array *states) @@ -126,7 +129,8 @@ static void gfx_ctx_wl_get_video_size(void *data, { gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data; - if (wl->surface == NULL) { + if (!wl->reported_display_size) { + wl->reported_display_size = true; output_info_t *oi, *tmp; oi = wl->current_output; @@ -409,9 +413,6 @@ static struct libdecor_frame_interface libdecor_frame_interface = { }; #endif -#define DEFAULT_WINDOWED_WIDTH 640 -#define DEFAULT_WINDOWED_HEIGHT 480 - #ifdef HAVE_EGL #define WL_EGL_ATTRIBS_BASE \ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \ @@ -509,6 +510,8 @@ static void *gfx_ctx_wl_init(void *video_driver) wl->input.dpy = wl_display_connect(NULL); wl->last_buffer_scale = 1; wl->buffer_scale = 1; + wl->width = DEFAULT_WINDOWED_WIDTH; + wl->height = DEFAULT_WINDOWED_HEIGHT; if (!wl->input.dpy) { @@ -542,14 +545,77 @@ static void *gfx_ctx_wl_init(void *video_driver) if (!wl->idle_inhibit_manager) { - RARCH_WARN("[Wayland]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol!\n"); + RARCH_LOG("[Wayland]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol\n"); } if (!wl->deco_manager) { - RARCH_WARN("[Wayland]: Compositor doesn't support zxdg_decoration_manager_v1 protocol!\n"); + RARCH_LOG("[Wayland]: Compositor doesn't support zxdg_decoration_manager_v1 protocol\n"); } + wl->surface = wl_compositor_create_surface(wl->compositor); + + wl_surface_set_buffer_scale(wl->surface, wl->buffer_scale); + wl_surface_add_listener(wl->surface, &wl_surface_listener, wl); + +#ifdef HAVE_LIBDECOR + wl->libdecor_context = libdecor_new(wl->input.dpy, &libdecor_interface); + if (wl->libdecor_context) + { + wl->libdecor_frame = libdecor_decorate(wl->libdecor_context, wl->surface, &libdecor_frame_interface, wl); + if (!wl->libdecor_frame) + { + RARCH_ERR("[Wayland]: Failed to crate libdecor frame\n"); + goto error; + } + + libdecor_frame_set_app_id(wl->libdecor_frame, "retroarch"); + libdecor_frame_set_title(wl->libdecor_frame, "RetroArch"); + libdecor_frame_map(wl->libdecor_frame); + } + + /* Waiting for libdecor to be configured before starting to draw */ + wl_surface_commit(wl->surface); + wl->configured = true; + + while (wl->configured) + { + if (libdecor_dispatch(wl->libdecor_context, 0) < 0) + { + RARCH_ERR("[Wayland]: libdecor failed to dispatch\n"); + goto error; + } + } +#else + wl->xdg_surface = xdg_wm_base_get_xdg_surface(wl->xdg_shell, wl->surface); + xdg_surface_add_listener(wl->xdg_surface, &xdg_surface_listener, wl); + + wl->xdg_toplevel = xdg_surface_get_toplevel(wl->xdg_surface); + xdg_toplevel_add_listener(wl->xdg_toplevel, &xdg_toplevel_listener, wl); + + xdg_toplevel_set_app_id(wl->xdg_toplevel, "retroarch"); + xdg_toplevel_set_title(wl->xdg_toplevel, "RetroArch"); + + if (wl->deco_manager) + wl->deco = zxdg_decoration_manager_v1_get_toplevel_decoration( + wl->deco_manager, wl->xdg_toplevel); + + /* Waiting for xdg_toplevel to be configured before starting to draw */ + wl_surface_commit(wl->surface); + wl->configured = true; + + while (wl->configured) + wl_display_dispatch(wl->input.dpy); +#endif + + wl_display_roundtrip(wl->input.dpy); + xdg_wm_base_add_listener(wl->xdg_shell, &xdg_shell_listener, NULL); + + /* Bind SHM based wl_buffer to wl_surface until the vulkan surface is ready. + * This shows the window which assigns us a display (wl_output) + * which is usefull for HiDPI and auto selecting a display for fullscreen. */ + draw_splash_screen(wl); + wl->input.fd = wl_display_get_fd(wl->input.dpy); #ifdef HAVE_EGL @@ -703,72 +769,24 @@ static bool gfx_ctx_wl_set_video_mode(void *data, (gfx_ctx_wayland_data_t*)data, egl_attribs); #endif gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data; + settings_t *settings = config_get_ptr(); + unsigned video_monitor_index = settings->uints.video_monitor_index; wl->width = width ? width : DEFAULT_WINDOWED_WIDTH; wl->height = height ? height : DEFAULT_WINDOWED_HEIGHT; - wl->surface = wl_compositor_create_surface(wl->compositor); - wl_surface_set_buffer_scale(wl->surface, wl->buffer_scale); - wl_surface_add_listener(wl->surface, &wl_surface_listener, wl); + +#ifdef HAVE_LIBDECOR + struct libdecor_state *state = libdecor_state_new( width, height); + libdecor_frame_commit(wl->libdecor_frame, state, NULL); + libdecor_state_free(state); +#endif #ifdef HAVE_EGL wl->win = wl_egl_window_create(wl->surface, wl->width * wl->buffer_scale, wl->height * wl->buffer_scale); #endif -#ifdef HAVE_LIBDECOR - wl->libdecor_context = libdecor_new(wl->input.dpy, &libdecor_interface); - if (wl->libdecor_context) - { - wl->libdecor_frame = libdecor_decorate(wl->libdecor_context, wl->surface, &libdecor_frame_interface, wl); - if (!wl->libdecor_frame) - { - RARCH_ERR("[Wayland]: Failed to crate libdecor frame\n"); - goto error; - } - - libdecor_frame_set_app_id(wl->libdecor_frame, "retroarch"); - libdecor_frame_set_title(wl->libdecor_frame, "RetroArch"); - libdecor_frame_map(wl->libdecor_frame); - } - - /* Waiting for libdecor to be configured before starting to draw */ - wl_surface_commit(wl->surface); - wl->configured = true; - - while (wl->configured) - { - if (libdecor_dispatch(wl->libdecor_context, 0) < 0) - { - RARCH_ERR("[Wayland]: libdecor failed to dispatch\n"); - goto error; - } - } -#else - wl->xdg_surface = xdg_wm_base_get_xdg_surface(wl->xdg_shell, wl->surface); - xdg_surface_add_listener(wl->xdg_surface, &xdg_surface_listener, wl); - - wl->xdg_toplevel = xdg_surface_get_toplevel(wl->xdg_surface); - xdg_toplevel_add_listener(wl->xdg_toplevel, &xdg_toplevel_listener, wl); - - xdg_toplevel_set_app_id(wl->xdg_toplevel, "retroarch"); - xdg_toplevel_set_title(wl->xdg_toplevel, "RetroArch"); - - if (wl->deco_manager) - wl->deco = zxdg_decoration_manager_v1_get_toplevel_decoration( - wl->deco_manager, wl->xdg_toplevel); - - /* Waiting for xdg_toplevel to be configured before starting to draw */ - wl_surface_commit(wl->surface); - wl->configured = true; - - while (wl->configured) - wl_display_dispatch(wl->input.dpy); -#endif - - wl_display_roundtrip(wl->input.dpy); - xdg_wm_base_add_listener(wl->xdg_shell, &xdg_shell_listener, NULL); - #ifdef HAVE_EGL if (!egl_create_context(&wl->egl, (attr != egl_attribs) ? egl_attribs : NULL)) @@ -784,12 +802,35 @@ static bool gfx_ctx_wl_set_video_mode(void *data, if (fullscreen) { + struct wl_output *output = NULL; + int output_i = 0; + output_info_t *oi, *tmp; + + if (video_monitor_index <= 0 && wl->current_output != NULL) + { + oi = wl->current_output; + output = oi->output; + RARCH_LOG("[Wayland/Vulkan]: Auto fullscreen on display \"%s\" \"%s\"\n", oi->make, oi->model); + } + else wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link) + { + if (++output_i == video_monitor_index) + { + output = oi->output; + RARCH_LOG("[Wayland/Vulkan]: Fullscreen on display %i \"%s\" \"%s\"\n", output_i, oi->make, oi->model); + break; + } + } + + if (output == NULL) + RARCH_LOG("[Wayland/Vulkan] Failed to specify monitor for fullscreen, letting compositor decide\n"); + #ifdef HAVE_LIBDECOR - libdecor_frame_set_fullscreen(wl->libdecor_frame, NULL); + libdecor_frame_set_fullscreen(wl->libdecor_frame, output); #else - xdg_toplevel_set_fullscreen(wl->xdg_toplevel, NULL); + xdg_toplevel_set_fullscreen(wl->xdg_toplevel, output); #endif - } + } flush_wayland_fd(&wl->input); diff --git a/gfx/drivers_context/wayland_vk_ctx.c b/gfx/drivers_context/wayland_vk_ctx.c index 14bae682af..05406ee1ec 100644 --- a/gfx/drivers_context/wayland_vk_ctx.c +++ b/gfx/drivers_context/wayland_vk_ctx.c @@ -52,6 +52,9 @@ #define EGL_PLATFORM_WAYLAND_KHR 0x31D8 #endif +#define DEFAULT_WINDOWED_WIDTH 640 +#define DEFAULT_WINDOWED_HEIGHT 480 + static void handle_toplevel_config_common(void *data, void *toplevel, int32_t width, int32_t height, struct wl_array *states) @@ -109,7 +112,8 @@ static void gfx_ctx_wl_get_video_size(void *data, { gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data; - if (wl->surface == NULL) { + if (!wl->reported_display_size) { + wl->reported_display_size = true; output_info_t *oi, *tmp; oi = wl->current_output; @@ -389,9 +393,6 @@ static struct libdecor_frame_interface libdecor_frame_interface = { }; #endif -#define DEFAULT_WINDOWED_WIDTH 640 -#define DEFAULT_WINDOWED_HEIGHT 480 - static void *gfx_ctx_wl_init(void *video_driver) { int i; @@ -408,6 +409,8 @@ static void *gfx_ctx_wl_init(void *video_driver) wl->input.dpy = wl_display_connect(NULL); wl->last_buffer_scale = 1; wl->buffer_scale = 1; + wl->width = DEFAULT_WINDOWED_WIDTH; + wl->height = DEFAULT_WINDOWED_HEIGHT; if (!wl->input.dpy) { @@ -441,14 +444,77 @@ static void *gfx_ctx_wl_init(void *video_driver) if (!wl->idle_inhibit_manager) { - RARCH_WARN("[Wayland]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol!\n"); + RARCH_LOG("[Wayland]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol\n"); } if (!wl->deco_manager) { - RARCH_WARN("[Wayland]: Compositor doesn't support zxdg_decoration_manager_v1 protocol!\n"); + RARCH_LOG("[Wayland]: Compositor doesn't support zxdg_decoration_manager_v1 protocol\n"); } + wl->surface = wl_compositor_create_surface(wl->compositor); + + wl_surface_set_buffer_scale(wl->surface, wl->buffer_scale); + wl_surface_add_listener(wl->surface, &wl_surface_listener, wl); + +#ifdef HAVE_LIBDECOR + wl->libdecor_context = libdecor_new(wl->input.dpy, &libdecor_interface); + if (wl->libdecor_context) + { + wl->libdecor_frame = libdecor_decorate(wl->libdecor_context, wl->surface, &libdecor_frame_interface, wl); + if (!wl->libdecor_frame) + { + RARCH_ERR("[Wayland/Vulkan]: Failed to crate libdecor frame\n"); + goto error; + } + + libdecor_frame_set_app_id(wl->libdecor_frame, "retroarch"); + libdecor_frame_set_title(wl->libdecor_frame, "RetroArch"); + libdecor_frame_map(wl->libdecor_frame); + } + + /* Waiting for libdecor to be configured before starting to draw */ + wl_surface_commit(wl->surface); + wl->configured = true; + + while (wl->configured) + { + if (libdecor_dispatch(wl->libdecor_context, 0) < 0) + { + RARCH_ERR("[Wayland/Vulkan]: libdecor failed to dispatch\n"); + goto error; + } + } +#else + wl->xdg_surface = xdg_wm_base_get_xdg_surface(wl->xdg_shell, wl->surface); + xdg_surface_add_listener(wl->xdg_surface, &xdg_surface_listener, wl); + + wl->xdg_toplevel = xdg_surface_get_toplevel(wl->xdg_surface); + xdg_toplevel_add_listener(wl->xdg_toplevel, &xdg_toplevel_listener, wl); + + xdg_toplevel_set_app_id(wl->xdg_toplevel, "retroarch"); + xdg_toplevel_set_title(wl->xdg_toplevel, "RetroArch"); + + if (wl->deco_manager) + wl->deco = zxdg_decoration_manager_v1_get_toplevel_decoration( + wl->deco_manager, wl->xdg_toplevel); + + /* Waiting for xdg_toplevel to be configured before starting to draw */ + wl_surface_commit(wl->surface); + wl->configured = true; + + while (wl->configured) + wl_display_dispatch(wl->input.dpy); +#endif + + wl_display_roundtrip(wl->input.dpy); + xdg_wm_base_add_listener(wl->xdg_shell, &xdg_shell_listener, NULL); + + /* Bind SHM based wl_buffer to wl_surface until the vulkan surface is ready. + * This shows the window which assigns us a display (wl_output) + * which is usefull for HiDPI and auto selecting a display for fullscreen. */ + draw_splash_screen(wl); + wl->input.fd = wl_display_get_fd(wl->input.dpy); if (!vulkan_context_init(&wl->vk, VULKAN_WSI_WAYLAND)) @@ -518,74 +584,49 @@ static bool gfx_ctx_wl_set_video_mode(void *data, bool fullscreen) { gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data; + settings_t *settings = config_get_ptr(); + unsigned video_monitor_index = settings->uints.video_monitor_index; wl->width = width ? width : DEFAULT_WINDOWED_WIDTH; wl->height = height ? height : DEFAULT_WINDOWED_HEIGHT; - wl->surface = wl_compositor_create_surface(wl->compositor); - wl_surface_set_buffer_scale(wl->surface, wl->buffer_scale); - wl_surface_add_listener(wl->surface, &wl_surface_listener, wl); #ifdef HAVE_LIBDECOR - wl->libdecor_context = libdecor_new(wl->input.dpy, &libdecor_interface); - if (wl->libdecor_context) - { - wl->libdecor_frame = libdecor_decorate(wl->libdecor_context, wl->surface, &libdecor_frame_interface, wl); - if (!wl->libdecor_frame) - { - RARCH_ERR("[Wayland/Vulkan]: Failed to crate libdecor frame\n"); - goto error; - } - - libdecor_frame_set_app_id(wl->libdecor_frame, "retroarch"); - libdecor_frame_set_title(wl->libdecor_frame, "RetroArch"); - libdecor_frame_map(wl->libdecor_frame); - } - - /* Waiting for libdecor to be configured before starting to draw */ - wl_surface_commit(wl->surface); - wl->configured = true; - - while (wl->configured) - { - if (libdecor_dispatch(wl->libdecor_context, 0) < 0) - { - RARCH_ERR("[Wayland/Vulkan]: libdecor failed to dispatch\n"); - goto error; - } - } -#else - wl->xdg_surface = xdg_wm_base_get_xdg_surface(wl->xdg_shell, wl->surface); - xdg_surface_add_listener(wl->xdg_surface, &xdg_surface_listener, wl); - - wl->xdg_toplevel = xdg_surface_get_toplevel(wl->xdg_surface); - xdg_toplevel_add_listener(wl->xdg_toplevel, &xdg_toplevel_listener, wl); - - xdg_toplevel_set_app_id(wl->xdg_toplevel, "retroarch"); - xdg_toplevel_set_title(wl->xdg_toplevel, "RetroArch"); - - if (wl->deco_manager) - wl->deco = zxdg_decoration_manager_v1_get_toplevel_decoration( - wl->deco_manager, wl->xdg_toplevel); - - /* Waiting for xdg_toplevel to be configured before starting to draw */ - wl_surface_commit(wl->surface); - wl->configured = true; - - while (wl->configured) - wl_display_dispatch(wl->input.dpy); + struct libdecor_state *state = libdecor_state_new( width, height); + libdecor_frame_commit(wl->libdecor_frame, state, NULL); + libdecor_state_free(state); #endif - wl_display_roundtrip(wl->input.dpy); - xdg_wm_base_add_listener(wl->xdg_shell, &xdg_shell_listener, NULL); - if (fullscreen) { + struct wl_output *output = NULL; + int output_i = 0; + output_info_t *oi, *tmp; + + if (video_monitor_index <= 0 && wl->current_output != NULL) + { + oi = wl->current_output; + output = oi->output; + RARCH_LOG("[Wayland/Vulkan]: Auto fullscreen on display \"%s\" \"%s\"\n", oi->make, oi->model); + } + else wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link) + { + if (++output_i == video_monitor_index) + { + output = oi->output; + RARCH_LOG("[Wayland/Vulkan]: Fullscreen on display %i \"%s\" \"%s\"\n", output_i, oi->make, oi->model); + break; + } + } + + if (output == NULL) + RARCH_LOG("[Wayland/Vulkan] Failed to specify monitor for fullscreen, letting compositor decide\n"); + #ifdef HAVE_LIBDECOR - libdecor_frame_set_fullscreen(wl->libdecor_frame, NULL); + libdecor_frame_set_fullscreen(wl->libdecor_frame, output); #else - xdg_toplevel_set_fullscreen(wl->xdg_toplevel, NULL); + xdg_toplevel_set_fullscreen(wl->xdg_toplevel, output); #endif } diff --git a/input/common/wayland_common.c b/input/common/wayland_common.c index 1b1dd99a42..98a01b11c4 100644 --- a/input/common/wayland_common.c +++ b/input/common/wayland_common.c @@ -13,9 +13,17 @@ * If not, see . */ +// Needed for memfd_create +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* See feature_test_macros(7) */ +#endif + #include #include +#include +#include +#include #include #include @@ -30,6 +38,8 @@ #include "../input_keymaps.h" #include "../../frontend/frontend_driver.h" +#define SPLASH_SHM_NAME "retroarch-wayland-vk-splash" + static void keyboard_handle_keymap(void* data, struct wl_keyboard* keyboard, uint32_t format, @@ -513,6 +523,8 @@ static void display_handle_geometry(void *data, output_info_t *oi = (output_info_t*)data; oi->physical_width = physical_width; oi->physical_height = physical_height; + oi->make = strdup(make); + oi->model = strdup(model); } static void display_handle_mode(void *data, @@ -548,9 +560,11 @@ static void registry_handle_global(void *data, struct wl_registry *reg, { gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data; + RARCH_DBG("[Wayland]: Add global %u, interface %s, version %u\n", id, interface, version); + if (string_is_equal(interface, "wl_compositor")) wl->compositor = (struct wl_compositor*)wl_registry_bind(reg, - id, &wl_compositor_interface, 3); + id, &wl_compositor_interface, MIN(version, 4)); else if (string_is_equal(interface, "wl_output")) { output_info_t *oi = (output_info_t*) @@ -558,27 +572,27 @@ static void registry_handle_global(void *data, struct wl_registry *reg, oi->global_id = id; oi->output = (struct wl_output*)wl_registry_bind(reg, - id, &wl_output_interface, 2); + id, &wl_output_interface, MIN(version, 2)); wl_output_add_listener(oi->output, &output_listener, oi); wl_list_insert(&wl->all_outputs, &oi->link); wl_display_roundtrip(wl->input.dpy); } else if (string_is_equal(interface, "xdg_wm_base")) wl->xdg_shell = (struct xdg_wm_base*) - wl_registry_bind(reg, id, &xdg_wm_base_interface, 1); + wl_registry_bind(reg, id, &xdg_wm_base_interface, MIN(version, 3)); else if (string_is_equal(interface, "wl_shm")) - wl->shm = (struct wl_shm*)wl_registry_bind(reg, id, &wl_shm_interface, 1); + wl->shm = (struct wl_shm*)wl_registry_bind(reg, id, &wl_shm_interface, MIN(version, 1)); else if (string_is_equal(interface, "wl_seat")) { - wl->seat = (struct wl_seat*)wl_registry_bind(reg, id, &wl_seat_interface, 2); + wl->seat = (struct wl_seat*)wl_registry_bind(reg, id, &wl_seat_interface, MIN(version, 2)); wl_seat_add_listener(wl->seat, &seat_listener, wl); } else if (string_is_equal(interface, "zwp_idle_inhibit_manager_v1")) wl->idle_inhibit_manager = (struct zwp_idle_inhibit_manager_v1*)wl_registry_bind( - reg, id, &zwp_idle_inhibit_manager_v1_interface, 1); + reg, id, &zwp_idle_inhibit_manager_v1_interface, MIN(version, 1)); else if (string_is_equal(interface, "zxdg_decoration_manager_v1")) wl->deco_manager = (struct zxdg_decoration_manager_v1*)wl_registry_bind( - reg, id, &zxdg_decoration_manager_v1_interface, 1); + reg, id, &zxdg_decoration_manager_v1_interface, MIN(version, 1)); } static void registry_handle_global_remove(void *data, @@ -598,6 +612,16 @@ static void registry_handle_global_remove(void *data, } } +static void shm_buffer_handle_release(void *data, + struct wl_buffer *wl_buffer) +{ + shm_buffer_t *buffer = data; + + wl_buffer_destroy(buffer->wl_buffer); + munmap(buffer->data, buffer->data_size); + free(buffer); +} + const struct wl_registry_listener registry_listener = { registry_handle_global, registry_handle_global_remove, @@ -654,6 +678,10 @@ const struct wl_pointer_listener pointer_listener = { pointer_handle_axis, }; +const struct wl_buffer_listener shm_buffer_listener = { + shm_buffer_handle_release, +}; + void flush_wayland_fd(void *data) { struct pollfd fd = {0}; @@ -679,3 +707,128 @@ void flush_wayland_fd(void *data) wl_display_flush(wl->dpy); } } + +#ifdef HAVE_MEMFD_CREATE +int create_anonymous_file(off_t size) +{ + int fd; + + int ret; + + fd = memfd_create(SPLASH_SHM_NAME, MFD_CLOEXEC | MFD_ALLOW_SEALING); + + if (fd < 0) + return -1; + + fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK); + + do { + ret = posix_fallocate(fd, 0, size); + } while (ret == EINTR); + if (ret != 0) { + close(fd); + errno = ret; + return -1; + } + + return fd; +} +#endif + +shm_buffer_t *create_shm_buffer(gfx_ctx_wayland_data_t *wl, int width, + int height, + uint32_t format) +{ + struct wl_shm_pool *pool; + int fd, size, stride, ofd; + void *data; + shm_buffer_t *buffer; + + stride = width * 4; + size = stride * height; + +#ifdef HAVE_MEMFD_CREATE + fd = create_anonymous_file(size); +#else + fd = shm_open(SPLASH_SHM_NAME, O_RDWR | O_CREAT, 0660); + ftruncate(fd, size); +#endif + if (fd < 0) { + RARCH_ERR("[Wayland] [SHM]: Creating a buffer file for %d B failed: %s\n", + size, strerror(errno)); + return NULL; + } + + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + RARCH_ERR("[Wayland] [SHM]: mmap failed: %s\n", strerror(errno)); + close(fd); + return NULL; + } + + buffer = calloc(1, sizeof *buffer); + + pool = wl_shm_create_pool(wl->shm, fd, size); + buffer->wl_buffer = wl_shm_pool_create_buffer(pool, 0, + width, height, + stride, format); + wl_buffer_add_listener(buffer->wl_buffer, &shm_buffer_listener, buffer); + wl_shm_pool_destroy(pool); +#ifndef HAVE_MEMFD_CREATE + shm_unlink(SPLASH_SHM_NAME); +#endif + close(fd); + + buffer->data = data; + buffer->data_size = size; + + return buffer; +} + + +void shm_buffer_paint_checkerboard(shm_buffer_t *buffer, + int width, int height, int scale, + size_t chk, uint32_t bg, uint32_t fg) +{ + uint32_t *pixels = buffer->data; + uint32_t color; + int y, x, sx, sy; + size_t off; + int stride = width * scale; + + for (y = 0; y < height; y++) { + for (x = 0; x < width; x++) { + color = (x & chk) ^ (y & chk) ? fg : bg; + for (sx = 0; sx < scale; sx++) { + for (sy = 0; sy < scale; sy++) { + off = x * scale + sx + + (y * scale + sy) * stride; + pixels[off] = color; + } + } + } + } +} + + +void draw_splash_screen(gfx_ctx_wayland_data_t *wl) +{ + shm_buffer_t *buffer; + + buffer = create_shm_buffer(wl, + wl->width * wl->buffer_scale, + wl->height * wl->buffer_scale, + WL_SHM_FORMAT_XRGB8888); + shm_buffer_paint_checkerboard(buffer, wl->width, + wl->height, wl->buffer_scale, + 16, 0xffbcbcbc, 0xff8e8e8e); + + wl_surface_attach(wl->surface, buffer->wl_buffer, 0, 0); + wl_surface_set_buffer_scale(wl->surface, wl->buffer_scale); + if (wl_surface_get_version(wl->surface) >= WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION) + wl_surface_damage_buffer(wl->surface, 0, 0, + wl->width * wl->buffer_scale, + wl->height * wl->buffer_scale); + wl_surface_commit(wl->surface); +} + diff --git a/input/common/wayland_common.h b/input/common/wayland_common.h index 6cf363357b..4f245843b2 100644 --- a/input/common/wayland_common.h +++ b/input/common/wayland_common.h @@ -77,6 +77,8 @@ typedef struct output_info unsigned physical_width; unsigned physical_height; unsigned scale; + char *make; + char *model; struct wl_list link; /* wl->all_outputs */ } output_info_t; @@ -96,7 +98,7 @@ typedef struct input_ctx_wayland_data struct { - struct wl_pointer *surface; + struct wl_surface *surface; int last_x, last_y; int x, y; int delta_x, delta_y; @@ -167,8 +169,15 @@ typedef struct gfx_ctx_wayland_data bool resize; bool configured; bool activated; + bool reported_display_size; } gfx_ctx_wayland_data_t; +typedef struct shm_buffer { + struct wl_buffer *wl_buffer; + void *data; + size_t data_size; +} shm_buffer_t; + #ifdef HAVE_XKBCOMMON /* FIXME: Move this into a header? */ int init_xkb(int fd, size_t size); @@ -185,6 +194,17 @@ void handle_toplevel_close(void *data, void flush_wayland_fd(void *data); +int create_anonymous_file(off_t size); + +shm_buffer_t *create_shm_buffer(gfx_ctx_wayland_data_t *wl, + int width, int height, uint32_t format); + +void shm_buffer_paint_checkerboard(shm_buffer_t *buffer, + int width, int height, int scale, + size_t chk, uint32_t bg, uint32_t fg); + +void draw_splash_screen(gfx_ctx_wayland_data_t *wl); + extern const struct wl_keyboard_listener keyboard_listener; extern const struct wl_pointer_listener pointer_listener; @@ -203,4 +223,6 @@ extern const struct wl_output_listener output_listener; extern const struct wl_registry_listener registry_listener; +extern const struct wl_buffer_listener shm_buffer_listener; + #endif diff --git a/qb/config.libs.sh b/qb/config.libs.sh index 5775f7d5d3..c418034ca0 100644 --- a/qb/config.libs.sh +++ b/qb/config.libs.sh @@ -550,6 +550,7 @@ fi check_lib '' STRCASESTR "$CLIB" strcasestr check_lib '' MMAP "$CLIB" mmap +check_lib '' MEMFD_CREATE "$CLIB" memfd_create check_enabled CXX VULKAN vulkan 'The C++ compiler is' false check_enabled CXX OPENGL_CORE 'OpenGL core' 'The C++ compiler is' false diff --git a/qb/config.params.sh b/qb/config.params.sh index dd977fa0cd..f6017a283e 100644 --- a/qb/config.params.sh +++ b/qb/config.params.sh @@ -195,4 +195,5 @@ HAVE_LIBSHAKE=no # libShake haptic feedback support HAVE_CHECK=no # check support for unit tests HAVE_WIFI=no # wifi driver support HAVE_CRTSWITCHRES=auto # CRT mode switching support (requires C++11) +HAVE_MEMFD_CREATE=auto # libc supports memfd_create C89_CRTSWITCHRES=no