Wayland: Properly discover displays and their modes.

Fixes Bugzilla #2913.
This commit is contained in:
Emmanuel Gil Peyrot 2015-03-18 01:14:45 +01:00
parent e7ba78a35a
commit 89be609adf
3 changed files with 74 additions and 144 deletions

View File

@ -43,11 +43,6 @@
#define WAYLANDVID_DRIVER_NAME "wayland" #define WAYLANDVID_DRIVER_NAME "wayland"
struct wayland_mode {
SDL_DisplayMode mode;
struct wl_list link;
};
/* Initialization/Query functions */ /* Initialization/Query functions */
static int static int
Wayland_VideoInit(_THIS); Wayland_VideoInit(_THIS);
@ -135,27 +130,6 @@ VideoBootStrap Wayland_bootstrap = {
Wayland_Available, Wayland_CreateDevice Wayland_Available, Wayland_CreateDevice
}; };
static void
wayland_add_mode(SDL_VideoData *d, SDL_DisplayMode m)
{
struct wayland_mode *mode;
/* Check for duplicate mode */
wl_list_for_each(mode, &d->modes_list, link)
if (mode->mode.w == m.w && mode->mode.h == m.h &&
mode->mode.refresh_rate == m.refresh_rate)
return;
/* Add new mode to the list */
mode = (struct wayland_mode *) SDL_calloc(1, sizeof *mode);
if (!mode)
return;
mode->mode = m;
WAYLAND_wl_list_insert(&d->modes_list, &mode->link);
}
static void static void
display_handle_geometry(void *data, display_handle_geometry(void *data,
struct wl_output *output, struct wl_output *output,
@ -168,55 +142,79 @@ display_handle_geometry(void *data,
int transform) int transform)
{ {
SDL_VideoData *d = data; SDL_VideoDisplay *display = data;
d->screen_allocation.x = x; display->name = strdup(model);
d->screen_allocation.y = y; display->driverdata = output;
} }
static void static void
display_handle_mode(void *data, display_handle_mode(void *data,
struct wl_output *wl_output, struct wl_output *output,
uint32_t flags, uint32_t flags,
int width, int width,
int height, int height,
int refresh) int refresh)
{ {
SDL_VideoData *d = data; SDL_VideoDisplay *display = data;
SDL_DisplayMode mode; SDL_DisplayMode mode;
SDL_zero(mode); SDL_zero(mode);
mode.w = width; mode.w = width;
mode.h = height; mode.h = height;
mode.refresh_rate = refresh / 1000; mode.refresh_rate = refresh / 1000; // mHz to Hz
SDL_AddDisplayMode(display, &mode);
wayland_add_mode(d, mode);
if (flags & WL_OUTPUT_MODE_CURRENT) { if (flags & WL_OUTPUT_MODE_CURRENT) {
d->screen_allocation.width = width; display->current_mode = mode;
d->screen_allocation.height = height; display->desktop_mode = mode;
} }
} }
static void
display_handle_done(void *data,
struct wl_output *output)
{
SDL_VideoDisplay *display = data;
SDL_AddVideoDisplay(display);
SDL_free(display->name);
SDL_free(display);
}
static void
display_handle_scale(void *data,
struct wl_output *output,
int32_t factor)
{
// TODO: do HiDPI stuff.
}
static const struct wl_output_listener output_listener = { static const struct wl_output_listener output_listener = {
display_handle_geometry, display_handle_geometry,
display_handle_mode display_handle_mode,
display_handle_done,
display_handle_scale
}; };
static void static void
shm_handle_format(void *data, Wayland_add_display(SDL_VideoData *d, uint32_t id)
struct wl_shm *shm,
uint32_t format)
{ {
SDL_VideoData *d = data; SDL_VideoDisplay *display = SDL_malloc(sizeof *display);
if (!display) {
SDL_OutOfMemory();
return;
}
SDL_zero(*display);
d->shm_formats |= (1 << format); struct wl_output *output = wl_registry_bind(d->registry, id, &wl_output_interface, 2);
if (!output) {
SDL_SetError("Failed to retrieve output.");
return;
}
wl_output_add_listener(output, &output_listener, display);
} }
static const struct wl_shm_listener shm_listener = {
shm_handle_format
};
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
static void static void
windowmanager_hints(void *data, struct qt_windowmanager *qt_windowmanager, windowmanager_hints(void *data, struct qt_windowmanager *qt_windowmanager,
@ -245,8 +243,7 @@ display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
if (strcmp(interface, "wl_compositor") == 0) { if (strcmp(interface, "wl_compositor") == 0) {
d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, 1); d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, 1);
} else if (strcmp(interface, "wl_output") == 0) { } else if (strcmp(interface, "wl_output") == 0) {
d->output = wl_registry_bind(d->registry, id, &wl_output_interface, 1); Wayland_add_display(d, id);
wl_output_add_listener(d->output, &output_listener, d);
} else if (strcmp(interface, "wl_seat") == 0) { } else if (strcmp(interface, "wl_seat") == 0) {
Wayland_display_add_input(d, id); Wayland_display_add_input(d, id);
} else if (strcmp(interface, "wl_shell") == 0) { } else if (strcmp(interface, "wl_shell") == 0) {
@ -255,7 +252,6 @@ display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1); d->shm = wl_registry_bind(registry, id, &wl_shm_interface, 1);
d->cursor_theme = WAYLAND_wl_cursor_theme_load(NULL, 32, d->shm); d->cursor_theme = WAYLAND_wl_cursor_theme_load(NULL, 32, d->shm);
d->default_cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr"); d->default_cursor = WAYLAND_wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr");
wl_shm_add_listener(d->shm, &shm_listener, d);
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
} else if (strcmp(interface, "qt_touch_extension") == 0) { } else if (strcmp(interface, "qt_touch_extension") == 0) {
@ -278,20 +274,13 @@ static const struct wl_registry_listener registry_listener = {
int int
Wayland_VideoInit(_THIS) Wayland_VideoInit(_THIS)
{ {
SDL_VideoData *data; SDL_VideoData *data = SDL_malloc(sizeof *data);
SDL_VideoDisplay display;
SDL_DisplayMode mode;
int i;
data = malloc(sizeof *data);
if (data == NULL) if (data == NULL)
return 0; return 0;
memset(data, 0, sizeof *data); memset(data, 0, sizeof *data);
_this->driverdata = data; _this->driverdata = data;
WAYLAND_wl_list_init(&data->modes_list);
data->display = WAYLAND_wl_display_connect(NULL); data->display = WAYLAND_wl_display_connect(NULL);
if (data->display == NULL) { if (data->display == NULL) {
SDL_SetError("Failed to connect to a Wayland display"); SDL_SetError("Failed to connect to a Wayland display");
@ -299,25 +288,18 @@ Wayland_VideoInit(_THIS)
} }
data->registry = wl_display_get_registry(data->display); data->registry = wl_display_get_registry(data->display);
if (data->registry == NULL) {
if ( data->registry == NULL) {
SDL_SetError("Failed to get the Wayland registry"); SDL_SetError("Failed to get the Wayland registry");
return 0; return 0;
} }
wl_registry_add_listener(data->registry, &registry_listener, data); wl_registry_add_listener(data->registry, &registry_listener, data);
for (i=0; i < 100; i++) { // First roundtrip to receive all registry objects.
if (data->screen_allocation.width != 0 || WAYLAND_wl_display_get_error(data->display) != 0) { wl_display_roundtrip(data->display);
break;
}
WAYLAND_wl_display_dispatch(data->display);
}
if (data->screen_allocation.width == 0) { // Second roundtrip to receive all output events.
SDL_SetError("Failed while waiting for screen allocation: %d ", WAYLAND_wl_display_get_error(data->display)); wl_display_roundtrip(data->display);
return 0;
}
data->xkb_context = WAYLAND_xkb_context_new(0); data->xkb_context = WAYLAND_xkb_context_new(0);
if (!data->xkb_context) { if (!data->xkb_context) {
@ -325,20 +307,7 @@ Wayland_VideoInit(_THIS)
return 0; return 0;
} }
/* Use a fake 32-bpp desktop mode */ Wayland_InitMouse();
mode.format = SDL_PIXELFORMAT_RGB888;
mode.w = data->screen_allocation.width;
mode.h = data->screen_allocation.height;
mode.refresh_rate = 0;
mode.driverdata = NULL;
wayland_add_mode(data, mode);
SDL_zero(display);
display.desktop_mode = mode;
display.current_mode = mode;
display.driverdata = NULL;
SDL_AddVideoDisplay(&display);
Wayland_InitMouse ();
WAYLAND_wl_display_flush(data->display); WAYLAND_wl_display_flush(data->display);
@ -348,46 +317,30 @@ Wayland_VideoInit(_THIS)
static void static void
Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display) Wayland_GetDisplayModes(_THIS, SDL_VideoDisplay *sdl_display)
{ {
SDL_VideoData *data = _this->driverdata; // Nothing to do here, everything was already done in the wl_output
SDL_DisplayMode mode; // callbacks.
struct wayland_mode *m;
Wayland_PumpEvents(_this);
wl_list_for_each(m, &data->modes_list, link) {
m->mode.format = SDL_PIXELFORMAT_RGB888;
SDL_AddDisplayMode(sdl_display, &m->mode);
m->mode.format = SDL_PIXELFORMAT_RGBA8888;
SDL_AddDisplayMode(sdl_display, &m->mode);
}
mode.w = data->screen_allocation.width;
mode.h = data->screen_allocation.height;
mode.refresh_rate = 0;
mode.driverdata = NULL;
mode.format = SDL_PIXELFORMAT_RGB888;
SDL_AddDisplayMode(sdl_display, &mode);
mode.format = SDL_PIXELFORMAT_RGBA8888;
SDL_AddDisplayMode(sdl_display, &mode);
} }
static int static int
Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode) Wayland_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode)
{ {
return 0; SDL_SetError("SetDisplayMode not (yet?) supported on Wayland.");
return -1;
} }
void void
Wayland_VideoQuit(_THIS) Wayland_VideoQuit(_THIS)
{ {
SDL_VideoData *data = _this->driverdata; SDL_VideoData *data = _this->driverdata;
struct wayland_mode *t, *m; int i;
Wayland_FiniMouse (); Wayland_FiniMouse ();
if (data->output) for (i = 0; i < _this->num_displays; ++i) {
wl_output_destroy(data->output); SDL_VideoDisplay *display = &_this->displays[i];
wl_output_destroy(display->driverdata);
display->driverdata = NULL;
}
Wayland_display_destroy_input(data); Wayland_display_destroy_input(data);
@ -417,17 +370,14 @@ Wayland_VideoQuit(_THIS)
if (data->compositor) if (data->compositor)
wl_compositor_destroy(data->compositor); wl_compositor_destroy(data->compositor);
if (data->registry)
wl_registry_destroy(data->registry);
if (data->display) { if (data->display) {
WAYLAND_wl_display_flush(data->display); WAYLAND_wl_display_flush(data->display);
WAYLAND_wl_display_disconnect(data->display); WAYLAND_wl_display_disconnect(data->display);
} }
wl_list_for_each_safe(m, t, &data->modes_list, link) {
WAYLAND_wl_list_remove(&m->link);
free(m);
}
free(data); free(data);
_this->driverdata = NULL; _this->driverdata = NULL;
} }

View File

@ -40,19 +40,12 @@ typedef struct {
struct wl_display *display; struct wl_display *display;
struct wl_registry *registry; struct wl_registry *registry;
struct wl_compositor *compositor; struct wl_compositor *compositor;
struct wl_output *output;
struct wl_shm *shm; struct wl_shm *shm;
struct wl_cursor_theme *cursor_theme; struct wl_cursor_theme *cursor_theme;
struct wl_cursor *default_cursor; struct wl_cursor *default_cursor;
struct wl_pointer *pointer; struct wl_pointer *pointer;
struct wl_shell *shell; struct wl_shell *shell;
struct {
int32_t x, y, width, height;
} screen_allocation;
struct wl_list modes_list;
EGLDisplay edpy; EGLDisplay edpy;
EGLContext context; EGLContext context;
EGLConfig econf; EGLConfig econf;
@ -65,8 +58,6 @@ typedef struct {
struct qt_surface_extension *surface_extension; struct qt_surface_extension *surface_extension;
struct qt_windowmanager *windowmanager; struct qt_windowmanager *windowmanager;
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */ #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
uint32_t shm_formats;
} SDL_VideoData; } SDL_VideoData;
#endif /* _SDL_waylandvideo_h */ #endif /* _SDL_waylandvideo_h */

View File

@ -179,17 +179,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
} }
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */ #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
/**
* If the user specified 0x0 as the size (turned to 1x1 by SDL_CreateWindow
* in SDL_video.c), we want to make the window fill the whole screen
**/
if (window->w == 1) {
window->w = c->screen_allocation.width;
}
if (window->h == 1) {
window->h = c->screen_allocation.height;
}
data->egl_window = WAYLAND_wl_egl_window_create(data->surface, data->egl_window = WAYLAND_wl_egl_window_create(data->surface,
window->w, window->h); window->w, window->h);