(Wayland) Dedupe VK/GL code

This commit is contained in:
Colin Kinloch 2022-03-20 02:59:26 +00:00 committed by Autechre
parent c6892d03c3
commit 47c850c7fd
7 changed files with 943 additions and 1212 deletions

View File

@ -1262,6 +1262,7 @@ ifeq ($(HAVE_WAYLAND), 1)
OBJ += gfx/drivers_context/wayland_ctx.o \
input/common/wayland_common.o \
input/drivers/wayland_input.o \
gfx/common/wayland_common.o \
gfx/common/wayland/xdg-shell.o \
gfx/common/wayland/idle-inhibit-unstable-v1.o \
gfx/common/wayland/xdg-decoration-unstable-v1.o

754
gfx/common/wayland_common.c Normal file
View File

@ -0,0 +1,754 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2020 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <poll.h>
#include <unistd.h>
#include "wayland_common.h"
#include "../../frontend/frontend_driver.h"
#define SPLASH_SHM_NAME "retroarch-wayland-vk-splash"
#ifdef HAVE_LIBDECOR
#include <libdecor.h>
#endif
#define DEFAULT_WINDOWED_WIDTH 640
#define DEFAULT_WINDOWED_HEIGHT 480
void xdg_toplevel_handle_configure_common(gfx_ctx_wayland_data_t *wl,
void *toplevel,
int32_t width, int32_t height, struct wl_array *states)
{
const uint32_t *state;
wl->fullscreen = false;
wl->maximized = false;
WL_ARRAY_FOR_EACH(state, states, const uint32_t*)
{
switch (*state)
{
case XDG_TOPLEVEL_STATE_FULLSCREEN:
wl->fullscreen = true;
break;
case XDG_TOPLEVEL_STATE_MAXIMIZED:
wl->maximized = true;
break;
case XDG_TOPLEVEL_STATE_RESIZING:
wl->resize = true;
break;
case XDG_TOPLEVEL_STATE_ACTIVATED:
wl->activated = true;
break;
}
}
wl->width = width > 0 ? width : DEFAULT_WINDOWED_WIDTH;
wl->height = height > 0 ? height : DEFAULT_WINDOWED_HEIGHT;
}
void xdg_toplevel_handle_close(void *data,
struct xdg_toplevel *xdg_toplevel)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
command_event(CMD_EVENT_QUIT, NULL);
}
#ifdef HAVE_LIBDECOR
void libdecor_frame_handle_configure_common(struct libdecor_frame *frame,
struct libdecor_configuration *configuration,
gfx_ctx_wayland_data_t *wl)
{
int width, height;
struct libdecor_state *state = NULL;
static const enum
libdecor_window_state tiled_states = (
LIBDECOR_WINDOW_STATE_TILED_LEFT
| LIBDECOR_WINDOW_STATE_TILED_RIGHT
| LIBDECOR_WINDOW_STATE_TILED_TOP
| LIBDECOR_WINDOW_STATE_TILED_BOTTOM
);
enum libdecor_window_state window_state;
bool focused = false;
bool tiled = false;
wl->fullscreen = false;
wl->maximized = false;
if (libdecor_configuration_get_window_state(
configuration, &window_state))
{
wl->fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 0;
wl->maximized = (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) != 0;
focused = (window_state & LIBDECOR_WINDOW_STATE_ACTIVE) != 0;
tiled = (window_state & tiled_states) != 0;
}
if (!libdecor_configuration_get_content_size(configuration, frame,
&width, &height))
{
width = wl->floating_width;
height = wl->floating_height;
}
if ( width > 0
&& height > 0)
{
wl->width = width;
wl->height = height;
}
state = libdecor_state_new(wl->width, wl->height);
libdecor_frame_commit(frame, state, configuration);
libdecor_state_free(state);
if (libdecor_frame_is_floating(frame)) {
wl->floating_width = width;
wl->floating_height = height;
}
}
void libdecor_frame_handle_close(struct libdecor_frame *frame,
void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
command_event(CMD_EVENT_QUIT, NULL);
}
void libdecor_frame_handle_commit(struct libdecor_frame *frame,
void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
}
#endif
void gfx_ctx_wl_get_video_size_common(gfx_ctx_wayland_data_t *wl,
unsigned *width, unsigned *height)
{
if (!wl->reported_display_size) {
wl->reported_display_size = true;
output_info_t *oi, *tmp;
oi = wl->current_output;
// If window is not ready get any monitor
if (!oi)
wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link)
break;
*width = oi->width;
*height = oi->height;
} else {
*width = wl->width * wl->buffer_scale;
*height = wl->height * wl->buffer_scale;
}
}
void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl)
{
if (wl->input.dpy != NULL && wl->input.fd >= 0)
close(wl->input.fd);
#ifdef HAVE_XKBCOMMON
free_xkb();
#endif
if (wl->wl_keyboard)
wl_keyboard_destroy(wl->wl_keyboard);
if (wl->wl_pointer)
wl_pointer_destroy(wl->wl_pointer);
if (wl->wl_touch)
wl_touch_destroy(wl->wl_touch);
if (wl->cursor.theme)
wl_cursor_theme_destroy(wl->cursor.theme);
if (wl->cursor.surface)
wl_surface_destroy(wl->cursor.surface);
if (wl->seat)
wl_seat_destroy(wl->seat);
if (wl->xdg_shell)
xdg_wm_base_destroy(wl->xdg_shell);
if (wl->compositor)
wl_compositor_destroy(wl->compositor);
if (wl->registry)
wl_registry_destroy(wl->registry);
if (wl->xdg_surface)
xdg_surface_destroy(wl->xdg_surface);
if (wl->surface)
wl_surface_destroy(wl->surface);
if (wl->xdg_toplevel)
xdg_toplevel_destroy(wl->xdg_toplevel);
if (wl->idle_inhibit_manager)
zwp_idle_inhibit_manager_v1_destroy(wl->idle_inhibit_manager);
if (wl->deco)
zxdg_toplevel_decoration_v1_destroy(wl->deco);
if (wl->deco_manager)
zxdg_decoration_manager_v1_destroy(wl->deco_manager);
if (wl->idle_inhibitor)
zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor);
if (wl->input.dpy)
{
wl_display_flush(wl->input.dpy);
wl_display_disconnect(wl->input.dpy);
}
wl->xdg_shell = NULL;
wl->compositor = NULL;
wl->registry = NULL;
wl->input.dpy = NULL;
wl->xdg_surface = NULL;
wl->surface = NULL;
wl->xdg_toplevel = NULL;
wl->width = 0;
wl->height = 0;
}
void gfx_ctx_wl_update_title_common(gfx_ctx_wayland_data_t *wl)
{
char title[128];
title[0] = '\0';
video_driver_get_window_title(title, sizeof(title));
#ifdef HAVE_LIBDECOR
if (wl && title[0])
libdecor_frame_set_title(wl->libdecor_frame, title);
#else
if (wl && title[0])
{
if (wl->deco)
zxdg_toplevel_decoration_v1_set_mode(wl->deco,
ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
xdg_toplevel_set_title(wl->xdg_toplevel, title);
}
#endif
}
bool gfx_ctx_wl_get_metrics_common(gfx_ctx_wayland_data_t *wl,
enum display_metric_types type, float *value)
{
output_info_t *oi, *tmp;
oi = wl->current_output;
if (!oi)
wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link)
break;
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
*value = (float)oi->physical_width;
break;
case DISPLAY_METRIC_MM_HEIGHT:
*value = (float)oi->physical_height;
break;
case DISPLAY_METRIC_DPI:
*value = (float)oi->width * 25.4f /
(float)oi->physical_width;
break;
default:
*value = 0.0f;
return false;
}
return true;
}
bool gfx_ctx_wl_init_common(void *video_driver,
toplevel_listener_t **toplevel_listener, gfx_ctx_wayland_data_t **wwl)
{
int i;
*wwl = calloc(1, sizeof(gfx_ctx_wayland_data_t));
gfx_ctx_wayland_data_t *wl = *wwl;
if (!wl)
return false;
wl_list_init(&wl->all_outputs);
frontend_driver_destroy_signal_handler_state();
wl->input.dpy = wl_display_connect(NULL);
wl->last_buffer_scale = 1;
wl->buffer_scale = 1;
wl->floating_width = DEFAULT_WINDOWED_WIDTH;
wl->floating_height = DEFAULT_WINDOWED_HEIGHT;
if (!wl->input.dpy)
{
RARCH_ERR("[Wayland]: Failed to connect to Wayland server.\n");
goto error;
}
frontend_driver_install_signal_handler();
wl->registry = wl_display_get_registry(wl->input.dpy);
wl_registry_add_listener(wl->registry, &registry_listener, wl);
wl_display_roundtrip(wl->input.dpy);
if (!wl->compositor)
{
RARCH_ERR("[Wayland]: Failed to create compositor.\n");
goto error;
}
if (!wl->shm)
{
RARCH_ERR("[Wayland]: Failed to create shm.\n");
goto error;
}
if (!wl->xdg_shell)
{
RARCH_ERR("[Wayland]: Failed to create shell.\n");
goto error;
}
if (!wl->idle_inhibit_manager)
{
RARCH_LOG("[Wayland]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol\n");
}
if (!wl->deco_manager)
{
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, toplevel_listener, 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, 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. */
if (!draw_splash_screen(wl))
RARCH_ERR("[Wayland`]: Failed to draw splash screen\n");
wl_display_roundtrip(wl->input.dpy);
wl->input.fd = wl_display_get_fd(wl->input.dpy);
wl->input.keyboard_focus = true;
wl->input.mouse.focus = true;
wl->cursor.surface = wl_compositor_create_surface(wl->compositor);
wl->cursor.theme = wl_cursor_theme_load(NULL, 16, wl->shm);
wl->cursor.default_cursor = wl_cursor_theme_get_cursor(wl->cursor.theme, "left_ptr");
wl->num_active_touches = 0;
for (i = 0;i < MAX_TOUCHES;i++)
{
wl->active_touch_positions[i].active = false;
wl->active_touch_positions[i].id = -1;
wl->active_touch_positions[i].x = (unsigned) 0;
wl->active_touch_positions[i].y = (unsigned) 0;
}
flush_wayland_fd(&wl->input);
return true;
error:
return false;
}
bool gfx_ctx_wl_set_video_mode_common_size(gfx_ctx_wayland_data_t *wl,
unsigned width, unsigned height)
{
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_set_buffer_scale(wl->surface, wl->buffer_scale);
#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
return true;
error:
return false;
}
bool gfx_ctx_wl_set_video_mode_common_fullscreen(gfx_ctx_wayland_data_t *wl,
bool fullscreen)
{
settings_t *settings = config_get_ptr();
unsigned video_monitor_index = settings->uints.video_monitor_index;
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]: 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]: Fullscreen on display %i \"%s\" \"%s\"\n", output_i, oi->make, oi->model);
break;
}
}
if (output == NULL)
RARCH_LOG("[Wayland] Failed to specify monitor for fullscreen, letting compositor decide\n");
#ifdef HAVE_LIBDECOR
libdecor_frame_set_fullscreen(wl->libdecor_frame, output);
#else
xdg_toplevel_set_fullscreen(wl->xdg_toplevel, output);
#endif
}
flush_wayland_fd(&wl->input);
if (fullscreen)
{
wl->cursor.visible = false;
gfx_ctx_wl_show_mouse(wl, false);
}
else
wl->cursor.visible = true;
return true;
error:
return false;
}
bool gfx_ctx_wl_suppress_screensaver(void *data, bool state)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl->idle_inhibit_manager)
return false;
if (state == (!!wl->idle_inhibitor))
return true;
if (state)
{
RARCH_LOG("[Wayland]: Enabling idle inhibitor\n");
struct zwp_idle_inhibit_manager_v1 *mgr = wl->idle_inhibit_manager;
wl->idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(mgr, wl->surface);
}
else
{
RARCH_LOG("[Wayland]: Disabling the idle inhibitor\n");
zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor);
wl->idle_inhibitor = NULL;
}
return true;
}
float gfx_ctx_wl_get_refresh_rate(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl || !wl->current_output)
return false;
return (float) wl->current_output->refresh_rate / 1000.0f;
}
bool gfx_ctx_wl_has_focus(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
return wl->input.keyboard_focus;
}
void gfx_ctx_wl_check_window_common(gfx_ctx_wayland_data_t *wl,
void (*get_video_size)(void*, unsigned*, unsigned*), bool *quit,
bool *resize, unsigned *width, unsigned *height)
{
/* this function works with SCALED sizes, it's used from the renderer */
unsigned new_width, new_height;
flush_wayland_fd(&wl->input);
new_width = *width * wl->last_buffer_scale;
new_height = *height * wl->last_buffer_scale;
get_video_size(wl, &new_width, &new_height);
if ( new_width != *width * wl->last_buffer_scale
|| new_height != *height * wl->last_buffer_scale)
{
*width = new_width;
*height = new_height;
*resize = true;
wl->last_buffer_scale = wl->buffer_scale;
}
*quit = (bool)frontend_driver_get_signal_handler_state();
}
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);
}
static void xdg_surface_handle_configure(void *data, struct xdg_surface *surface,
uint32_t serial)
{
xdg_surface_ack_configure(surface, serial);
}
#ifdef HAVE_LIBDECOR
static void libdecor_handle_error(struct libdecor *context,
enum libdecor_error error, const char *message)
{
RARCH_ERR("[Wayland]: libdecor Caught error (%d): %s\n", error, message);
}
#endif
int create_shm_file(off_t size)
{
int fd;
int ret;
#ifdef HAVE_MEMFD_CREATE
fd = memfd_create(SPLASH_SHM_NAME, MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (fd >= 0) {
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;
fd = -1;
}
}
if (fd < 0)
#endif
{
for (unsigned retry_count = 0; retry_count < 100; retry_count++)
{
char *name;
if (asprintf (&name, "%s-%02d", SPLASH_SHM_NAME, retry_count) < 0)
continue;
fd = shm_open(name, O_RDWR | O_CREAT, 0600);
if (fd >= 0)
{
shm_unlink(name);
free(name);
ftruncate(fd, size);
break;
}
free(name);
}
}
return fd;
}
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;
if (size <= 0)
return NULL;
fd = create_shm_file(size);
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);
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;
}
}
}
}
}
bool 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);
if (buffer == NULL)
return false;
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);
return true;
}
const struct wl_buffer_listener shm_buffer_listener = {
shm_buffer_handle_release,
};
#ifdef HAVE_LIBDECOR
const struct libdecor_interface libdecor_interface = {
.error = libdecor_handle_error,
};
#endif

103
gfx/common/wayland_common.h Normal file
View File

@ -0,0 +1,103 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef HAVE_LIBDECOR
#include <libdecor.h>
#endif
#include "../../input/common/wayland_common.h"
#ifdef HAVE_LIBDECOR
typedef struct libdecor_frame_interface toplevel_listener_t;
#else
typedef struct xdg_toplevel_listener toplevel_listener_t;
#endif
typedef struct shm_buffer {
struct wl_buffer *wl_buffer;
void *data;
size_t data_size;
} shm_buffer_t;
void xdg_toplevel_handle_configure_common(gfx_ctx_wayland_data_t *wl, void *toplevel,
int32_t width, int32_t height, struct wl_array *states);
void xdg_toplevel_handle_close(void *data,
struct xdg_toplevel *xdg_toplevel);
#ifdef HAVE_LIBDECOR
void libdecor_frame_handle_configure_common(struct libdecor_frame *frame,
struct libdecor_configuration *configuration, gfx_ctx_wayland_data_t *wl);
void libdecor_frame_handle_close(struct libdecor_frame *frame,
void *data);
void libdecor_frame_handle_commit(struct libdecor_frame *frame,
void *data);
#endif
void gfx_ctx_wl_get_video_size_common(gfx_ctx_wayland_data_t *wl,
unsigned *width, unsigned *height);
void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl);
void gfx_ctx_wl_update_title_common(gfx_ctx_wayland_data_t *wl);
bool gfx_ctx_wl_get_metrics_common(gfx_ctx_wayland_data_t *wl,
enum display_metric_types type, float *value);
bool gfx_ctx_wl_init_common(void *video_driver,
toplevel_listener_t **toplevel_listener,
gfx_ctx_wayland_data_t **wl);
bool gfx_ctx_wl_set_video_mode_common_size(gfx_ctx_wayland_data_t *wl,
unsigned width, unsigned height);
bool gfx_ctx_wl_set_video_mode_common_fullscreen(gfx_ctx_wayland_data_t *wl,
bool fullscreen);
bool gfx_ctx_wl_suppress_screensaver(void *data, bool state);
bool gfx_ctx_wl_set_video_mode_common(gfx_ctx_wayland_data_t *wl,
unsigned width, unsigned height,
bool fullscreen);
float gfx_ctx_wl_get_refresh_rate(void *data);
bool gfx_ctx_wl_has_focus(void *data);
void gfx_ctx_wl_check_window_common(gfx_ctx_wayland_data_t *wl,
void (*get_video_size)(void*, unsigned*, unsigned*), bool *quit,
bool *resize, unsigned *width, unsigned *height);
int create_shm_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);
bool draw_splash_screen(gfx_ctx_wayland_data_t *wl);
#ifdef HAVE_LIBDECOR
extern const struct libdecor_interface libdecor_interface;
#endif

View File

@ -25,10 +25,7 @@
#include "../../config.h"
#endif
#ifdef HAVE_LIBDECOR
#include <libdecor.h>
#endif
#include "../common/wayland_common.h"
#include "../../frontend/frontend_driver.h"
#include "../../input/common/wayland_common.h"
#include "../../input/input_driver.h"
@ -59,41 +56,13 @@ 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,
/* Shell surface callbacks. */
static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *toplevel,
int32_t width, int32_t height, struct wl_array *states)
{
const uint32_t *state;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->fullscreen = false;
wl->maximized = false;
WL_ARRAY_FOR_EACH(state, states, const uint32_t*)
{
switch (*state)
{
case XDG_TOPLEVEL_STATE_FULLSCREEN:
wl->fullscreen = true;
break;
case XDG_TOPLEVEL_STATE_MAXIMIZED:
wl->maximized = true;
break;
case XDG_TOPLEVEL_STATE_RESIZING:
wl->resize = true;
break;
case XDG_TOPLEVEL_STATE_ACTIVATED:
wl->activated = true;
break;
}
}
wl->width = width > 0 ? width : DEFAULT_WINDOWED_WIDTH;
wl->height = height > 0 ? height : DEFAULT_WINDOWED_HEIGHT;
xdg_toplevel_handle_configure_common(wl, toplevel, width, height, states);
#ifdef HAVE_EGL
if (wl->win)
wl_egl_window_resize(wl->win, wl->width, wl->height, 0, 0);
@ -106,40 +75,12 @@ static void handle_toplevel_config_common(void *data,
wl->configured = false;
}
/* Shell surface callbacks. */
static void handle_toplevel_config(void *data,
struct xdg_toplevel *toplevel,
int32_t width, int32_t height, struct wl_array *states)
{
handle_toplevel_config_common(data, toplevel, width, height, states);
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
handle_toplevel_config,
handle_toplevel_close,
};
static void gfx_ctx_wl_get_video_size(void *data,
unsigned *width, unsigned *height)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl->reported_display_size) {
wl->reported_display_size = true;
output_info_t *oi, *tmp;
oi = wl->current_output;
// If window is not ready get any monitor
if (!oi)
wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link)
break;
*width = oi->width;
*height = oi->height;
} else {
*width = wl->width * wl->buffer_scale;
*height = wl->height * wl->buffer_scale;
}
gfx_ctx_wl_get_video_size_common(wl, width, height);
}
static void gfx_ctx_wl_destroy_resources(gfx_ctx_wayland_data_t *wl)
@ -154,92 +95,18 @@ static void gfx_ctx_wl_destroy_resources(gfx_ctx_wayland_data_t *wl)
wl_egl_window_destroy(wl->win);
#endif
#ifdef HAVE_XKBCOMMON
free_xkb();
#endif
if (wl->wl_keyboard)
wl_keyboard_destroy(wl->wl_keyboard);
if (wl->wl_pointer)
wl_pointer_destroy(wl->wl_pointer);
if (wl->wl_touch)
wl_touch_destroy(wl->wl_touch);
if (wl->cursor.theme)
wl_cursor_theme_destroy(wl->cursor.theme);
if (wl->cursor.surface)
wl_surface_destroy(wl->cursor.surface);
if (wl->seat)
wl_seat_destroy(wl->seat);
if (wl->xdg_shell)
xdg_wm_base_destroy(wl->xdg_shell);
if (wl->compositor)
wl_compositor_destroy(wl->compositor);
if (wl->registry)
wl_registry_destroy(wl->registry);
if (wl->xdg_surface)
xdg_surface_destroy(wl->xdg_surface);
if (wl->surface)
wl_surface_destroy(wl->surface);
if (wl->xdg_toplevel)
xdg_toplevel_destroy(wl->xdg_toplevel);
if (wl->idle_inhibit_manager)
zwp_idle_inhibit_manager_v1_destroy(wl->idle_inhibit_manager);
if (wl->deco)
zxdg_toplevel_decoration_v1_destroy(wl->deco);
if (wl->deco_manager)
zxdg_decoration_manager_v1_destroy(wl->deco_manager);
if (wl->idle_inhibitor)
zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor);
if (wl->input.dpy)
{
wl_display_flush(wl->input.dpy);
wl_display_disconnect(wl->input.dpy);
}
gfx_ctx_wl_destroy_resources_common(wl);
#ifdef HAVE_EGL
wl->win = NULL;
#endif
wl->xdg_shell = NULL;
wl->compositor = NULL;
wl->registry = NULL;
wl->input.dpy = NULL;
wl->xdg_surface = NULL;
wl->surface = NULL;
wl->xdg_toplevel = NULL;
wl->width = 0;
wl->height = 0;
}
static void gfx_ctx_wl_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height)
{
/* this function works with SCALED sizes, it's used from the renderer */
unsigned new_width, new_height;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
flush_wayland_fd(&wl->input);
new_width = *width * wl->last_buffer_scale;
new_height = *height * wl->last_buffer_scale;
gfx_ctx_wl_get_video_size(data, &new_width, &new_height);
if ( new_width != *width * wl->last_buffer_scale
|| new_height != *height * wl->last_buffer_scale)
{
*width = new_width;
*height = new_height;
*resize = true;
wl->last_buffer_scale = wl->buffer_scale;
}
*quit = (bool)frontend_driver_get_signal_handler_state();
gfx_ctx_wl_check_window_common(wl, gfx_ctx_wl_get_video_size, quit, resize, width, height);
}
static bool gfx_ctx_wl_set_resize(void *data, unsigned width, unsigned height)
@ -256,117 +123,24 @@ static bool gfx_ctx_wl_set_resize(void *data, unsigned width, unsigned height)
static void gfx_ctx_wl_update_title(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
char title[128];
title[0] = '\0';
video_driver_get_window_title(title, sizeof(title));
#ifdef HAVE_LIBDECOR
if (wl && title[0])
libdecor_frame_set_title(wl->libdecor_frame, title);
#else
if (wl && title[0])
{
if (wl->deco)
zxdg_toplevel_decoration_v1_set_mode(wl->deco,
ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
xdg_toplevel_set_title(wl->xdg_toplevel, title);
}
#endif
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
gfx_ctx_wl_update_title_common(wl);
}
static bool gfx_ctx_wl_get_metrics(void *data,
enum display_metric_types type, float *value)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
output_info_t *oi, *tmp;
oi = wl->current_output;
if (!oi)
wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link)
break;
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
*value = (float)oi->physical_width;
break;
case DISPLAY_METRIC_MM_HEIGHT:
*value = (float)oi->physical_height;
break;
case DISPLAY_METRIC_DPI:
*value = (float)oi->width * 25.4f /
(float)oi->physical_width;
break;
default:
*value = 0.0f;
return false;
}
return true;
return gfx_ctx_wl_get_metrics_common(wl, type, value);
}
#ifdef HAVE_LIBDECOR
static void
handle_libdecor_error(struct libdecor *context,
enum libdecor_error error, const char *message)
{
RARCH_ERR("[Wayland/GL]: libdecor Caught error (%d): %s\n", error, message);
}
static struct libdecor_interface libdecor_interface = {
.error = handle_libdecor_error,
};
static void
handle_libdecor_frame_configure(struct libdecor_frame *frame,
libdecor_frame_handle_configure(struct libdecor_frame *frame,
struct libdecor_configuration *configuration, void *data)
{
int width, height;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
struct libdecor_state *state = NULL;
static const enum
libdecor_window_state tiled_states = (
LIBDECOR_WINDOW_STATE_TILED_LEFT
| LIBDECOR_WINDOW_STATE_TILED_RIGHT
| LIBDECOR_WINDOW_STATE_TILED_TOP
| LIBDECOR_WINDOW_STATE_TILED_BOTTOM
);
enum libdecor_window_state window_state;
bool focused = false;
bool tiled = false;
wl->fullscreen = false;
wl->maximized = false;
if (libdecor_configuration_get_window_state(
configuration, &window_state))
{
wl->fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 0;
wl->maximized = (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) != 0;
focused = (window_state & LIBDECOR_WINDOW_STATE_ACTIVE) != 0;
tiled = (window_state & tiled_states) != 0;
}
if (!libdecor_configuration_get_content_size(configuration, frame,
&width, &height))
{
width = wl->floating_width;
height = wl->floating_height;
}
if ( width > 0
&& height > 0)
{
wl->width = width;
wl->height = height;
}
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
libdecor_frame_handle_configure_common(frame, configuration, wl);
#ifdef HAVE_EGL
if (wl->win)
@ -378,37 +152,18 @@ handle_libdecor_frame_configure(struct libdecor_frame *frame,
wl->height * wl->buffer_scale);
#endif
state = libdecor_state_new(wl->width, wl->height);
libdecor_frame_commit(frame, state, configuration);
libdecor_state_free(state);
if (libdecor_frame_is_floating(frame)) {
wl->floating_width = width;
wl->floating_height = height;
}
wl->configured = false;
}
static void
handle_libdecor_frame_close(struct libdecor_frame *frame,
void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
command_event(CMD_EVENT_QUIT, NULL);
}
static void
handle_libdecor_frame_commit(struct libdecor_frame *frame,
void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
}
static struct libdecor_frame_interface libdecor_frame_interface = {
handle_libdecor_frame_configure,
handle_libdecor_frame_close,
handle_libdecor_frame_commit,
static const toplevel_listener_t toplevel_listener = {
libdecor_frame_handle_configure,
libdecor_frame_handle_close,
libdecor_frame_handle_commit,
};
#else
static const toplevel_listener_t toplevel_listener = {
xdg_toplevel_handle_configure,
xdg_toplevel_handle_close,
};
#endif
@ -420,12 +175,9 @@ static struct libdecor_frame_interface libdecor_frame_interface = {
EGL_BLUE_SIZE, 1, \
EGL_ALPHA_SIZE, 0, \
EGL_DEPTH_SIZE, 0
#endif
static void *gfx_ctx_wl_init(void *video_driver)
static bool gfx_ctx_wl_egl_init_context(gfx_ctx_wayland_data_t *wl)
{
int i;
#ifdef HAVE_EGL
static const EGLint egl_attribs_gl[] = {
WL_EGL_ATTRIBS_BASE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
@ -462,16 +214,7 @@ static void *gfx_ctx_wl_init(void *video_driver)
EGLint n;
EGLint major = 0, minor = 0;
const EGLint *attrib_ptr = NULL;
#endif
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)
calloc(1, sizeof(gfx_ctx_wayland_data_t));
if (!wl)
return NULL;
wl_list_init(&wl->all_outputs);
#ifdef HAVE_EGL
switch (wl_api)
{
case GFX_CTX_OPENGL_API:
@ -502,125 +245,7 @@ static void *gfx_ctx_wl_init(void *video_driver)
default:
break;
}
#endif
frontend_driver_destroy_signal_handler_state();
wl->input.dpy = wl_display_connect(NULL);
wl->last_buffer_scale = 1;
wl->buffer_scale = 1;
wl->floating_width = DEFAULT_WINDOWED_WIDTH;
wl->floating_height = DEFAULT_WINDOWED_HEIGHT;
if (!wl->input.dpy)
{
RARCH_ERR("[Wayland/GL]: Failed to connect to Wayland server.\n");
goto error;
}
frontend_driver_install_signal_handler();
wl->registry = wl_display_get_registry(wl->input.dpy);
wl_registry_add_listener(wl->registry, &registry_listener, wl);
wl_display_roundtrip(wl->input.dpy);
if (!wl->compositor)
{
RARCH_ERR("[Wayland/GL]: Failed to create compositor.\n");
goto error;
}
if (!wl->shm)
{
RARCH_ERR("[Wayland/GL]: Failed to create shm.\n");
goto error;
}
if (!wl->xdg_shell)
{
RARCH_ERR("[Wayland/GL]: Failed to create shell.\n");
goto error;
}
if (!wl->idle_inhibit_manager)
{
RARCH_LOG("[Wayland/GL]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol\n");
}
if (!wl->deco_manager)
{
RARCH_LOG("[Wayland/GL]: 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/GL]: 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/GL]: 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. */
if (!draw_splash_screen(wl))
RARCH_ERR("[Wayland/GL]: Failed to draw splash screen\n");
wl_display_roundtrip(wl->input.dpy);
wl->input.fd = wl_display_get_fd(wl->input.dpy);
#ifdef HAVE_EGL
if (!egl_init_context(&wl->egl,
EGL_PLATFORM_WAYLAND_KHR,
(EGLNativeDisplayType)wl->input.dpy,
@ -633,26 +258,25 @@ static void *gfx_ctx_wl_init(void *video_driver)
if (n == 0 || !egl_has_config(&wl->egl))
goto error;
return true;
error:
return false;
}
#endif
wl->input.keyboard_focus = true;
wl->input.mouse.focus = true;
static void *gfx_ctx_wl_init(void *video_driver)
{
int i;
gfx_ctx_wayland_data_t *wl = NULL;
wl->cursor.surface = wl_compositor_create_surface(wl->compositor);
wl->cursor.theme = wl_cursor_theme_load(NULL, 16, wl->shm);
wl->cursor.default_cursor = wl_cursor_theme_get_cursor(wl->cursor.theme, "left_ptr");
if (!gfx_ctx_wl_init_common(video_driver, &toplevel_listener, &wl))
goto error;
wl->num_active_touches = 0;
for (i = 0;i < MAX_TOUCHES;i++)
{
wl->active_touch_positions[i].active = false;
wl->active_touch_positions[i].id = -1;
wl->active_touch_positions[i].x = (unsigned) 0;
wl->active_touch_positions[i].y = (unsigned) 0;
}
flush_wayland_fd(&wl->input);
#ifdef HAVE_EGL
if (!gfx_ctx_wl_egl_init_context(wl))
goto error;
#endif
return wl;
@ -765,33 +389,20 @@ static bool gfx_ctx_wl_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!gfx_ctx_wl_set_video_mode_common_size(wl, width, height))
goto error;
#ifdef HAVE_EGL
EGLint egl_attribs[16];
EGLint *attr = egl_fill_attribs(
(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_set_buffer_scale(wl->surface, wl->buffer_scale);
#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_EGL
if (!egl_create_context(&wl->egl, (attr != egl_attribs)
? egl_attribs : NULL))
{
@ -804,47 +415,8 @@ static bool gfx_ctx_wl_set_video_mode(void *data,
egl_set_swap_interval(&wl->egl, wl->egl.interval);
#endif
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/GL]: 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/GL]: Fullscreen on display %i \"%s\" \"%s\"\n", output_i, oi->make, oi->model);
break;
}
}
if (output == NULL)
RARCH_LOG("[Wayland/GL] Failed to specify monitor for fullscreen, letting compositor decide\n");
#ifdef HAVE_LIBDECOR
libdecor_frame_set_fullscreen(wl->libdecor_frame, output);
#else
xdg_toplevel_set_fullscreen(wl->xdg_toplevel, output);
#endif
}
flush_wayland_fd(&wl->input);
if (fullscreen)
{
wl->cursor.visible = false;
gfx_ctx_wl_show_mouse(wl, false);
}
else
wl->cursor.visible = true;
if (!gfx_ctx_wl_set_video_mode_common_fullscreen(wl, fullscreen))
goto error;
return true;
@ -877,37 +449,6 @@ static void gfx_ctx_wl_input_driver(void *data,
}
}
static bool gfx_ctx_wl_has_focus(void *data)
{
(void)data;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
return wl->input.keyboard_focus;
}
static bool gfx_ctx_wl_suppress_screensaver(void *data, bool state)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl->idle_inhibit_manager)
return false;
if (state == (!!wl->idle_inhibitor))
return true;
if (state)
{
RARCH_LOG("[Wayland/GL]: Enabling idle inhibitor\n");
struct zwp_idle_inhibit_manager_v1 *mgr = wl->idle_inhibit_manager;
wl->idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(mgr, wl->surface);
}
else
{
RARCH_LOG("[Wayland/GL]: Disabling the idle inhibitor\n");
zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor);
wl->idle_inhibitor = NULL;
}
return true;
}
static enum gfx_ctx_api gfx_ctx_wl_get_api(void *data)
{
return wl_api;
@ -1011,16 +552,6 @@ static void gfx_ctx_wl_set_flags(void *data, uint32_t flags)
wl->core_hw_context_enable = true;
}
static float gfx_ctx_wl_get_refresh_rate(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl || !wl->current_output)
return false;
return (float) wl->current_output->refresh_rate / 1000.0f;
}
const gfx_ctx_driver_t gfx_ctx_wayland = {
gfx_ctx_wl_init,
gfx_ctx_wl_destroy,

View File

@ -25,10 +25,7 @@
#include "../../config.h"
#endif
#ifdef HAVE_LIBDECOR
#include <libdecor.h>
#endif
#include "../common/wayland_common.h"
#include "../../frontend/frontend_driver.h"
#include "../../input/common/wayland_common.h"
#include "../../input/input_driver.h"
@ -52,177 +49,44 @@
#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)
{
const uint32_t *state;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
wl->fullscreen = false;
wl->maximized = false;
WL_ARRAY_FOR_EACH(state, states, const uint32_t*)
{
switch (*state)
{
case XDG_TOPLEVEL_STATE_FULLSCREEN:
wl->fullscreen = true;
break;
case XDG_TOPLEVEL_STATE_MAXIMIZED:
wl->maximized = true;
break;
case XDG_TOPLEVEL_STATE_RESIZING:
wl->resize = true;
break;
case XDG_TOPLEVEL_STATE_ACTIVATED:
wl->activated = true;
break;
}
}
wl->width = width > 0 ? width : DEFAULT_WINDOWED_WIDTH;
wl->height = height > 0 ? height : DEFAULT_WINDOWED_HEIGHT;
wl->configured = false;
}
/* Shell surface callbacks. */
static void handle_toplevel_config(void *data,
static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *toplevel,
int32_t width, int32_t height, struct wl_array *states)
{
handle_toplevel_config_common(data, toplevel, width, height, states);
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
xdg_toplevel_handle_configure_common(wl, toplevel, width, height, states);
wl->configured = false;
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
handle_toplevel_config,
handle_toplevel_close,
};
static void gfx_ctx_wl_get_video_size(void *data,
unsigned *width, unsigned *height)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl->reported_display_size) {
wl->reported_display_size = true;
output_info_t *oi, *tmp;
oi = wl->current_output;
// If window is not ready get any monitor
if (!oi)
wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link)
break;
*width = oi->width;
*height = oi->height;
} else {
*width = wl->width * wl->buffer_scale;
*height = wl->height * wl->buffer_scale;
}
gfx_ctx_wl_get_video_size_common(wl, width, height);
}
static void gfx_ctx_wl_destroy_resources(gfx_ctx_wayland_data_t *wl)
{
if (!wl)
return;
vulkan_context_destroy(&wl->vk, wl->surface);
if (wl->input.dpy != NULL && wl->input.fd >= 0)
close(wl->input.fd);
#ifdef HAVE_XKBCOMMON
free_xkb();
#endif
if (wl->wl_keyboard)
wl_keyboard_destroy(wl->wl_keyboard);
if (wl->wl_pointer)
wl_pointer_destroy(wl->wl_pointer);
if (wl->wl_touch)
wl_touch_destroy(wl->wl_touch);
if (wl->cursor.theme)
wl_cursor_theme_destroy(wl->cursor.theme);
if (wl->cursor.surface)
wl_surface_destroy(wl->cursor.surface);
if (wl->seat)
wl_seat_destroy(wl->seat);
if (wl->xdg_shell)
xdg_wm_base_destroy(wl->xdg_shell);
if (wl->compositor)
wl_compositor_destroy(wl->compositor);
if (wl->registry)
wl_registry_destroy(wl->registry);
if (wl->xdg_surface)
xdg_surface_destroy(wl->xdg_surface);
if (wl->surface)
wl_surface_destroy(wl->surface);
if (wl->xdg_toplevel)
xdg_toplevel_destroy(wl->xdg_toplevel);
if (wl->idle_inhibit_manager)
zwp_idle_inhibit_manager_v1_destroy(wl->idle_inhibit_manager);
if (wl->deco)
zxdg_toplevel_decoration_v1_destroy(wl->deco);
if (wl->deco_manager)
zxdg_decoration_manager_v1_destroy(wl->deco_manager);
if (wl->idle_inhibitor)
zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor);
if (wl->input.dpy)
{
wl_display_flush(wl->input.dpy);
wl_display_disconnect(wl->input.dpy);
}
wl->xdg_shell = NULL;
wl->compositor = NULL;
wl->registry = NULL;
wl->input.dpy = NULL;
wl->xdg_surface = NULL;
wl->surface = NULL;
wl->xdg_toplevel = NULL;
wl->width = 0;
wl->height = 0;
gfx_ctx_wl_destroy_resources_common(wl);
}
static void gfx_ctx_wl_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height)
{
/* this function works with SCALED sizes, it's used from the renderer */
unsigned new_width, new_height;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
flush_wayland_fd(&wl->input);
new_width = *width * wl->last_buffer_scale;
new_height = *height * wl->last_buffer_scale;
gfx_ctx_wl_get_video_size(data, &new_width, &new_height);
/* Swapchains are recreated in set_resize as a
* central place, so use that to trigger swapchain reinit. */
*resize = wl->vk.need_new_swapchain;
if ( new_width != *width * wl->last_buffer_scale
|| new_height != *height * wl->last_buffer_scale)
{
*width = new_width;
*height = new_height;
*resize = true;
gfx_ctx_wl_check_window_common(wl, gfx_ctx_wl_get_video_size, quit, resize,
width, height);
wl->last_buffer_scale = wl->buffer_scale;
}
*quit = (bool)frontend_driver_get_signal_handler_state();
}
static bool gfx_ctx_wl_set_resize(void *data, unsigned width, unsigned height)
@ -250,301 +114,51 @@ static bool gfx_ctx_wl_set_resize(void *data, unsigned width, unsigned height)
static void gfx_ctx_wl_update_title(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
char title[128];
title[0] = '\0';
video_driver_get_window_title(title, sizeof(title));
#ifdef HAVE_LIBDECOR
if (wl && title[0])
libdecor_frame_set_title(wl->libdecor_frame, title);
#else
if (wl && title[0])
{
if (wl->deco)
zxdg_toplevel_decoration_v1_set_mode(wl->deco,
ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
xdg_toplevel_set_title(wl->xdg_toplevel, title);
}
#endif
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
gfx_ctx_wl_update_title_common(wl);
}
static bool gfx_ctx_wl_get_metrics(void *data,
enum display_metric_types type, float *value)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
output_info_t *oi, *tmp;
oi = wl->current_output;
if (!oi)
wl_list_for_each_safe(oi, tmp, &wl->all_outputs, link)
break;
switch (type)
{
case DISPLAY_METRIC_MM_WIDTH:
*value = (float)oi->physical_width;
break;
case DISPLAY_METRIC_MM_HEIGHT:
*value = (float)oi->physical_height;
break;
case DISPLAY_METRIC_DPI:
*value = (float)oi->width * 25.4f /
(float)oi->physical_width;
break;
default:
*value = 0.0f;
return false;
}
return true;
return gfx_ctx_wl_get_metrics_common(wl, type, value);
}
#ifdef HAVE_LIBDECOR
static void
handle_libdecor_error(struct libdecor *context,
enum libdecor_error error, const char *message)
{
RARCH_ERR("[Wayland/Vulkan]: libdecor Caught error (%d): %s\n", error, message);
}
static struct libdecor_interface libdecor_interface = {
.error = handle_libdecor_error,
};
static void
handle_libdecor_frame_configure(struct libdecor_frame *frame,
libdecor_frame_handle_configure(struct libdecor_frame *frame,
struct libdecor_configuration *configuration, void *data)
{
int width, height;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
struct libdecor_state *state = NULL;
static const enum
libdecor_window_state tiled_states = (
LIBDECOR_WINDOW_STATE_TILED_LEFT
| LIBDECOR_WINDOW_STATE_TILED_RIGHT
| LIBDECOR_WINDOW_STATE_TILED_TOP
| LIBDECOR_WINDOW_STATE_TILED_BOTTOM
);
enum libdecor_window_state window_state;
bool focused = false;
bool tiled = false;
wl->fullscreen = false;
wl->maximized = false;
if (libdecor_configuration_get_window_state(
configuration, &window_state))
{
wl->fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 0;
wl->maximized = (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) != 0;
focused = (window_state & LIBDECOR_WINDOW_STATE_ACTIVE) != 0;
tiled = (window_state & tiled_states) != 0;
}
if (!libdecor_configuration_get_content_size(configuration, frame,
&width, &height))
{
width = wl->floating_width;
height = wl->floating_height;
}
if ( width > 0
&& height > 0)
{
wl->width = width;
wl->height = height;
}
state = libdecor_state_new(wl->width, wl->height);
libdecor_frame_commit(frame, state, configuration);
libdecor_state_free(state);
if (libdecor_frame_is_floating(frame)) {
wl->floating_width = width;
wl->floating_height = height;
}
libdecor_frame_handle_configure_common(frame, configuration, wl);
wl->configured = false;
}
static void
handle_libdecor_frame_close(struct libdecor_frame *frame,
void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
command_event(CMD_EVENT_QUIT, NULL);
}
static void
handle_libdecor_frame_commit(struct libdecor_frame *frame,
void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
}
static struct libdecor_frame_interface libdecor_frame_interface = {
handle_libdecor_frame_configure,
handle_libdecor_frame_close,
handle_libdecor_frame_commit,
static const toplevel_listener_t toplevel_listener = {
libdecor_frame_handle_configure,
libdecor_frame_handle_close,
libdecor_frame_handle_commit,
};
#else
static const toplevel_listener_t toplevel_listener = {
xdg_toplevel_handle_configure,
xdg_toplevel_handle_close,
};
#endif
static void *gfx_ctx_wl_init(void *video_driver)
{
int i;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)
calloc(1, sizeof(gfx_ctx_wayland_data_t));
gfx_ctx_wayland_data_t *wl = NULL;
if (!wl)
return NULL;
wl_list_init(&wl->all_outputs);
frontend_driver_destroy_signal_handler_state();
wl->input.dpy = wl_display_connect(NULL);
wl->last_buffer_scale = 1;
wl->buffer_scale = 1;
wl->floating_width = DEFAULT_WINDOWED_WIDTH;
wl->floating_height = DEFAULT_WINDOWED_HEIGHT;
if (!wl->input.dpy)
{
RARCH_ERR("[Wayland/Vulkan]: Failed to connect to Wayland server.\n");
if (!gfx_ctx_wl_init_common(video_driver, &toplevel_listener, &wl))
goto error;
}
frontend_driver_install_signal_handler();
wl->registry = wl_display_get_registry(wl->input.dpy);
wl_registry_add_listener(wl->registry, &registry_listener, wl);
wl_display_roundtrip(wl->input.dpy);
if (!wl->compositor)
{
RARCH_ERR("[Wayland/Vulkan]: Failed to create compositor.\n");
goto error;
}
if (!wl->shm)
{
RARCH_ERR("[Wayland/Vulkan]: Failed to create shm.\n");
goto error;
}
if (!wl->xdg_shell)
{
RARCH_ERR("[Wayland/Vulkan]: Failed to create shell.\n");
goto error;
}
if (!wl->idle_inhibit_manager)
{
RARCH_LOG("[Wayland/Vulkan]: Compositor doesn't support zwp_idle_inhibit_manager_v1 protocol\n");
}
if (!wl->deco_manager)
{
RARCH_LOG("[Wayland/Vulkan]: 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. */
if (!draw_splash_screen(wl))
RARCH_ERR("[Wayland/Vulkan]: Failed to draw splash screen\n");
wl_display_roundtrip(wl->input.dpy);
wl->input.fd = wl_display_get_fd(wl->input.dpy);
if (!vulkan_context_init(&wl->vk, VULKAN_WSI_WAYLAND))
goto error;
wl->input.keyboard_focus = true;
wl->input.mouse.focus = true;
wl->cursor.surface = wl_compositor_create_surface(wl->compositor);
wl->cursor.theme = wl_cursor_theme_load(NULL, 16, wl->shm);
wl->cursor.default_cursor = wl_cursor_theme_get_cursor(wl->cursor.theme, "left_ptr");
wl->num_active_touches = 0;
for (i = 0;i < MAX_TOUCHES;i++)
{
wl->active_touch_positions[i].active = false;
wl->active_touch_positions[i].id = -1;
wl->active_touch_positions[i].x = (unsigned) 0;
wl->active_touch_positions[i].y = (unsigned) 0;
}
flush_wayland_fd(&wl->input);
return wl;
error:
@ -590,19 +204,9 @@ 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_set_buffer_scale(wl->surface, wl->buffer_scale);
#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
if (!gfx_ctx_wl_set_video_mode_common_size(wl, width, height))
goto error;
if (!vulkan_surface_create(&wl->vk, VULKAN_WSI_WAYLAND,
wl->input.dpy, wl->surface,
@ -611,47 +215,8 @@ static bool gfx_ctx_wl_set_video_mode(void *data,
wl->swap_interval))
goto error;
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, output);
#else
xdg_toplevel_set_fullscreen(wl->xdg_toplevel, output);
#endif
}
flush_wayland_fd(&wl->input);
if (fullscreen)
{
wl->cursor.visible = false;
gfx_ctx_wl_show_mouse(wl, false);
}
else
wl->cursor.visible = true;
if (!gfx_ctx_wl_set_video_mode_common_fullscreen(wl, fullscreen))
goto error;
return true;
@ -684,37 +249,6 @@ static void gfx_ctx_wl_input_driver(void *data,
}
}
static bool gfx_ctx_wl_has_focus(void *data)
{
(void)data;
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
return wl->input.keyboard_focus;
}
static bool gfx_ctx_wl_suppress_screensaver(void *data, bool state)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl->idle_inhibit_manager)
return false;
if (state == (!!wl->idle_inhibitor))
return true;
if (state)
{
RARCH_LOG("[Wayland/Vulkan]: Enabling idle inhibitor\n");
struct zwp_idle_inhibit_manager_v1 *mgr = wl->idle_inhibit_manager;
wl->idle_inhibitor = zwp_idle_inhibit_manager_v1_create_inhibitor(mgr, wl->surface);
}
else
{
RARCH_LOG("[Wayland/Vulkan]: Disabling the idle inhibitor\n");
zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor);
wl->idle_inhibitor = NULL;
}
return true;
}
static enum gfx_ctx_api gfx_ctx_wl_get_api(void *data)
{
return GFX_CTX_VULKAN_API;
@ -773,16 +307,6 @@ static uint32_t gfx_ctx_wl_get_flags(void *data)
static void gfx_ctx_wl_set_flags(void *data, uint32_t flags) { }
static float gfx_ctx_wl_get_refresh_rate(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
if (!wl || !wl->current_output)
return false;
return (float) wl->current_output->refresh_rate / 1000.0f;
}
const gfx_ctx_driver_t gfx_ctx_vk_wayland = {
gfx_ctx_wl_init,
gfx_ctx_wl_destroy,

View File

@ -507,19 +507,12 @@ static void xdg_shell_ping(void *data, struct xdg_wm_base *shell, uint32_t seria
xdg_wm_base_pong(shell, serial);
}
static void handle_surface_config(void *data, struct xdg_surface *surface,
static void xdg_surface_handle_configure(void *data, struct xdg_surface *surface,
uint32_t serial)
{
xdg_surface_ack_configure(surface, serial);
}
void handle_toplevel_close(void *data,
struct xdg_toplevel *xdg_toplevel)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;
command_event(CMD_EVENT_QUIT, NULL);
}
static void display_handle_geometry(void *data,
struct wl_output *output,
int x, int y,
@ -571,10 +564,10 @@ static void registry_handle_global(void *data, struct wl_registry *reg,
RARCH_DBG("[Wayland]: Add global %u, interface %s, version %u\n", id, interface, version);
if (string_is_equal(interface, "wl_compositor"))
if (string_is_equal(interface, wl_compositor_interface.name))
wl->compositor = (struct wl_compositor*)wl_registry_bind(reg,
id, &wl_compositor_interface, MIN(version, 4));
else if (string_is_equal(interface, "wl_output"))
else if (string_is_equal(interface, wl_output_interface.name))
{
output_info_t *oi = (output_info_t*)
calloc(1, sizeof(output_info_t));
@ -586,12 +579,12 @@ static void registry_handle_global(void *data, struct wl_registry *reg,
wl_list_insert(&wl->all_outputs, &oi->link);
wl_display_roundtrip(wl->input.dpy);
}
else if (string_is_equal(interface, "xdg_wm_base"))
else if (string_is_equal(interface, xdg_wm_base_interface.name))
wl->xdg_shell = (struct xdg_wm_base*)
wl_registry_bind(reg, id, &xdg_wm_base_interface, MIN(version, 3));
else if (string_is_equal(interface, "wl_shm"))
else if (string_is_equal(interface, wl_shm_interface.name))
wl->shm = (struct wl_shm*)wl_registry_bind(reg, id, &wl_shm_interface, MIN(version, 1));
else if (string_is_equal(interface, "wl_seat"))
else if (string_is_equal(interface, wl_seat_interface.name))
{
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);
@ -599,13 +592,13 @@ static void registry_handle_global(void *data, struct wl_registry *reg,
if (wl->data_device != NULL)
wl_data_device_add_listener(wl->data_device, &data_device_listener, wl);
}
else if (string_is_equal(interface, "wl_data_device_manager"))
else if (string_is_equal(interface, wl_data_device_manager_interface.name))
wl->data_device_manager = (struct wl_data_device_manager*)wl_registry_bind(
reg, id, &wl_data_device_manager_interface, MIN(version, 3));
else if (string_is_equal(interface, "zwp_idle_inhibit_manager_v1"))
else if (string_is_equal(interface, zwp_idle_inhibit_manager_v1_interface.name))
wl->idle_inhibit_manager = (struct zwp_idle_inhibit_manager_v1*)wl_registry_bind(
reg, id, &zwp_idle_inhibit_manager_v1_interface, MIN(version, 1));
else if (string_is_equal(interface, "zxdg_decoration_manager_v1"))
else if (string_is_equal(interface, zxdg_decoration_manager_v1_interface.name))
wl->deco_manager = (struct zxdg_decoration_manager_v1*)wl_registry_bind(
reg, id, &zxdg_decoration_manager_v1_interface, MIN(version, 1));
}
@ -873,16 +866,6 @@ static void data_offer_handle_action(void *data,
struct wl_data_offer *offer,
enum wl_data_device_manager_dnd_action dnd_action) { }
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,
@ -901,7 +884,7 @@ const struct xdg_wm_base_listener xdg_shell_listener = {
};
const struct xdg_surface_listener xdg_surface_listener = {
handle_surface_config,
xdg_surface_handle_configure,
};
const struct wl_surface_listener wl_surface_listener = {
@ -954,10 +937,6 @@ const struct wl_data_offer_listener data_offer_listener = {
.action = data_offer_handle_action,
};
const struct wl_buffer_listener shm_buffer_listener = {
shm_buffer_handle_release,
};
void flush_wayland_fd(void *data)
{
struct pollfd fd = {0};
@ -984,145 +963,4 @@ void flush_wayland_fd(void *data)
}
}
int create_shm_file(off_t size)
{
int fd;
int ret;
#ifdef HAVE_MEMFD_CREATE
fd = memfd_create(SPLASH_SHM_NAME, MFD_CLOEXEC | MFD_ALLOW_SEALING);
if (fd >= 0) {
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;
fd = -1;
}
}
if (fd < 0)
#endif
{
for (unsigned retry_count = 0; retry_count < 100; retry_count++)
{
char *name;
if (asprintf (&name, "%s-%02d", SPLASH_SHM_NAME, retry_count) < 0)
continue;
fd = shm_open(name, O_RDWR | O_CREAT, 0600);
if (fd >= 0)
{
shm_unlink(name);
free(name);
ftruncate(fd, size);
break;
}
free(name);
}
}
return fd;
}
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;
if (size <= 0)
return NULL;
fd = create_shm_file(size);
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);
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;
}
}
}
}
}
bool 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);
if (buffer == NULL)
return false;
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);
return true;
}

View File

@ -184,12 +184,6 @@ typedef struct gfx_ctx_wayland_data
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);
@ -201,22 +195,8 @@ void free_xkb(void);
void gfx_ctx_wl_show_mouse(void *data, bool state);
void handle_toplevel_close(void *data,
struct xdg_toplevel *xdg_toplevel);
void flush_wayland_fd(void *data);
int create_shm_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);
bool 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;