mirror of https://github.com/encounter/SDL.git
wayland: Implement basic window move events via wl_surface_listener.
This unearthed an unspeakably large amount of bugs in the wl_output enumerator, notably the fact that the wl_output user pointer was to temporary memory! This was "fixed" ine862856
, and was then pointed out as a leak in4183211
, which was undone ind9ba204
. The busted fix was correct that the malloc was an issue, but wrong about _why_; SDL_AddVideoDisplay copies by value and does not reuse the pointer, so generally you want your VideoDisplay to be on the stack, but of course the callbacks don't allow that, so a malloc was a workaround. But we can do better and just host our temporary display inside WaylandOutputData because that will be persistent while also not leaking. Wait, wasn't I talking about move events? Right, that: wl_surface_listener does at least give us the ability to know what monitor we're on, even though we have no idea where we are on the monitor. All we need to do is check the wl_output against the display list and then push a move event that both indicates the correct display while also not being _too_ much of a lie (but enough of a lie to where our event doesn't get discarded as "undefined" or whatever). The index check for the video display is what spawned the great nightmare you see before you; aside from the bugfix this is actually a really basic patch.
This commit is contained in:
parent
859230ec82
commit
ed24c3452a
|
@ -249,10 +249,10 @@ display_handle_geometry(void *data,
|
||||||
int transform)
|
int transform)
|
||||||
|
|
||||||
{
|
{
|
||||||
SDL_VideoDisplay *display = data;
|
SDL_WaylandOutputData *driverdata = data;
|
||||||
|
|
||||||
display->name = SDL_strdup(model);
|
driverdata->placeholder.name = SDL_strdup(model);
|
||||||
((SDL_WaylandOutputData*)display->driverdata)->transform = transform;
|
driverdata->transform = transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -263,8 +263,7 @@ display_handle_mode(void *data,
|
||||||
int height,
|
int height,
|
||||||
int refresh)
|
int refresh)
|
||||||
{
|
{
|
||||||
SDL_VideoDisplay *display = data;
|
SDL_WaylandOutputData* driverdata = data;
|
||||||
SDL_WaylandOutputData* driverdata = display->driverdata;
|
|
||||||
SDL_DisplayMode mode;
|
SDL_DisplayMode mode;
|
||||||
|
|
||||||
if (flags & WL_OUTPUT_MODE_CURRENT) {
|
if (flags & WL_OUTPUT_MODE_CURRENT) {
|
||||||
|
@ -290,15 +289,14 @@ display_handle_mode(void *data,
|
||||||
}
|
}
|
||||||
mode.refresh_rate = refresh / 1000; /* mHz to Hz */
|
mode.refresh_rate = refresh / 1000; /* mHz to Hz */
|
||||||
mode.driverdata = driverdata->output;
|
mode.driverdata = driverdata->output;
|
||||||
SDL_AddDisplayMode(display, &mode);
|
SDL_AddDisplayMode(&driverdata->placeholder, &mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
display_handle_done(void *data,
|
display_handle_done(void *data,
|
||||||
struct wl_output *output)
|
struct wl_output *output)
|
||||||
{
|
{
|
||||||
SDL_VideoDisplay *display = data;
|
SDL_WaylandOutputData* driverdata = data;
|
||||||
SDL_WaylandOutputData* driverdata = display->driverdata;
|
|
||||||
SDL_DisplayMode mode;
|
SDL_DisplayMode mode;
|
||||||
|
|
||||||
if (driverdata->done)
|
if (driverdata->done)
|
||||||
|
@ -317,12 +315,13 @@ display_handle_done(void *data,
|
||||||
}
|
}
|
||||||
mode.refresh_rate = driverdata->refresh / 1000; /* mHz to Hz */
|
mode.refresh_rate = driverdata->refresh / 1000; /* mHz to Hz */
|
||||||
mode.driverdata = driverdata->output;
|
mode.driverdata = driverdata->output;
|
||||||
SDL_AddDisplayMode(display, &mode);
|
SDL_AddDisplayMode(&driverdata->placeholder, &mode);
|
||||||
display->current_mode = mode;
|
driverdata->placeholder.current_mode = mode;
|
||||||
display->desktop_mode = mode;
|
driverdata->placeholder.desktop_mode = mode;
|
||||||
|
|
||||||
SDL_AddVideoDisplay(display, SDL_FALSE);
|
driverdata->placeholder.driverdata = driverdata;
|
||||||
SDL_free(display->name);
|
SDL_AddVideoDisplay(&driverdata->placeholder, SDL_FALSE);
|
||||||
|
SDL_zero(driverdata->placeholder);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -330,8 +329,8 @@ display_handle_scale(void *data,
|
||||||
struct wl_output *output,
|
struct wl_output *output,
|
||||||
int32_t factor)
|
int32_t factor)
|
||||||
{
|
{
|
||||||
SDL_VideoDisplay *display = data;
|
SDL_WaylandOutputData *driverdata = data;
|
||||||
((SDL_WaylandOutputData*)display->driverdata)->scale_factor = factor;
|
driverdata->scale_factor = factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct wl_output_listener output_listener = {
|
static const struct wl_output_listener output_listener = {
|
||||||
|
@ -346,26 +345,18 @@ Wayland_add_display(SDL_VideoData *d, uint32_t id)
|
||||||
{
|
{
|
||||||
struct wl_output *output;
|
struct wl_output *output;
|
||||||
SDL_WaylandOutputData *data;
|
SDL_WaylandOutputData *data;
|
||||||
SDL_VideoDisplay *display = SDL_malloc(sizeof *display);
|
|
||||||
if (!display) {
|
|
||||||
SDL_OutOfMemory();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
SDL_zero(*display);
|
|
||||||
|
|
||||||
output = wl_registry_bind(d->registry, id, &wl_output_interface, 2);
|
output = wl_registry_bind(d->registry, id, &wl_output_interface, 2);
|
||||||
if (!output) {
|
if (!output) {
|
||||||
SDL_SetError("Failed to retrieve output.");
|
SDL_SetError("Failed to retrieve output.");
|
||||||
SDL_free(display);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
data = SDL_malloc(sizeof *data);
|
data = SDL_malloc(sizeof *data);
|
||||||
|
SDL_zerop(data);
|
||||||
data->output = output;
|
data->output = output;
|
||||||
data->scale_factor = 1.0;
|
data->scale_factor = 1.0;
|
||||||
data->done = SDL_FALSE;
|
|
||||||
display->driverdata = data;
|
|
||||||
|
|
||||||
wl_output_add_listener(output, &output_listener, display);
|
wl_output_add_listener(output, &output_listener, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
|
#ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
#include <EGL/egl.h>
|
#include <EGL/egl.h>
|
||||||
#include "wayland-util.h"
|
#include "wayland-util.h"
|
||||||
|
|
||||||
|
#include "../SDL_sysvideo.h"
|
||||||
#include "../../core/linux/SDL_dbus.h"
|
#include "../../core/linux/SDL_dbus.h"
|
||||||
#include "../../core/linux/SDL_ime.h"
|
#include "../../core/linux/SDL_ime.h"
|
||||||
|
|
||||||
|
@ -93,6 +94,7 @@ typedef struct {
|
||||||
struct wl_output *output;
|
struct wl_output *output;
|
||||||
float scale_factor;
|
float scale_factor;
|
||||||
int width, height, refresh, transform;
|
int width, height, refresh, transform;
|
||||||
|
SDL_VideoDisplay placeholder;
|
||||||
SDL_bool done;
|
SDL_bool done;
|
||||||
} SDL_WaylandOutputData;
|
} SDL_WaylandOutputData;
|
||||||
|
|
||||||
|
|
|
@ -467,8 +467,7 @@ update_scale_factor(SDL_WindowData *window) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < window->num_outputs; i++) {
|
for (i = 0; i < window->num_outputs; i++) {
|
||||||
SDL_VideoDisplay *display = wl_output_get_user_data(window->outputs[i]);
|
SDL_WaylandOutputData* driverdata = wl_output_get_user_data(window->outputs[i]);
|
||||||
SDL_WaylandOutputData* driverdata = display->driverdata;
|
|
||||||
float factor = driverdata->scale_factor;
|
float factor = driverdata->scale_factor;
|
||||||
if (factor > new_factor) {
|
if (factor > new_factor) {
|
||||||
new_factor = factor;
|
new_factor = factor;
|
||||||
|
@ -487,26 +486,50 @@ update_scale_factor(SDL_WindowData *window) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* While we can't get window position from the compositor, we do at least know
|
||||||
|
* what monitor we're on, so let's send move events that put the window at the
|
||||||
|
* center of the whatever display the wl_surface_listener events give us.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
Wayland_move_window(SDL_Window *window,
|
||||||
|
SDL_WaylandOutputData *driverdata)
|
||||||
|
{
|
||||||
|
int i, numdisplays = SDL_GetNumVideoDisplays();
|
||||||
|
for (i = 0; i < numdisplays; i += 1) {
|
||||||
|
if (SDL_GetDisplay(i)->driverdata == driverdata) {
|
||||||
|
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED,
|
||||||
|
SDL_WINDOWPOS_CENTERED_DISPLAY(i),
|
||||||
|
SDL_WINDOWPOS_CENTERED_DISPLAY(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handle_surface_enter(void *data, struct wl_surface *surface,
|
handle_surface_enter(void *data, struct wl_surface *surface,
|
||||||
struct wl_output *output) {
|
struct wl_output *output)
|
||||||
|
{
|
||||||
SDL_WindowData *window = data;
|
SDL_WindowData *window = data;
|
||||||
|
|
||||||
window->outputs = SDL_realloc(window->outputs, (window->num_outputs + 1) * sizeof *window->outputs);
|
window->outputs = SDL_realloc(window->outputs, (window->num_outputs + 1) * sizeof *window->outputs);
|
||||||
window->outputs[window->num_outputs++] = output;
|
window->outputs[window->num_outputs++] = output;
|
||||||
update_scale_factor(window);
|
update_scale_factor(window);
|
||||||
|
|
||||||
|
Wayland_move_window(window->sdlwindow, wl_output_get_user_data(output));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
handle_surface_leave(void *data, struct wl_surface *surface,
|
handle_surface_leave(void *data, struct wl_surface *surface,
|
||||||
struct wl_output *output) {
|
struct wl_output *output)
|
||||||
|
{
|
||||||
SDL_WindowData *window = data;
|
SDL_WindowData *window = data;
|
||||||
int i;
|
int i, send_move_event = 0;
|
||||||
|
|
||||||
for (i = 0; i < window->num_outputs; i++) {
|
for (i = 0; i < window->num_outputs; i++) {
|
||||||
if (window->outputs[i] == output) { /* remove this one */
|
if (window->outputs[i] == output) { /* remove this one */
|
||||||
if (i == (window->num_outputs-1)) {
|
if (i == (window->num_outputs-1)) {
|
||||||
window->outputs[i] = NULL;
|
window->outputs[i] = NULL;
|
||||||
|
send_move_event = 1;
|
||||||
} else {
|
} else {
|
||||||
SDL_memmove(&window->outputs[i], &window->outputs[i+1], sizeof (output) * ((window->num_outputs - i) - 1));
|
SDL_memmove(&window->outputs[i], &window->outputs[i+1], sizeof (output) * ((window->num_outputs - i) - 1));
|
||||||
}
|
}
|
||||||
|
@ -518,6 +541,9 @@ handle_surface_leave(void *data, struct wl_surface *surface,
|
||||||
if (window->num_outputs == 0) {
|
if (window->num_outputs == 0) {
|
||||||
SDL_free(window->outputs);
|
SDL_free(window->outputs);
|
||||||
window->outputs = NULL;
|
window->outputs = NULL;
|
||||||
|
} else if (send_move_event) {
|
||||||
|
Wayland_move_window(window->sdlwindow,
|
||||||
|
wl_output_get_user_data(window->outputs[window->num_outputs - 1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
update_scale_factor(window);
|
update_scale_factor(window);
|
||||||
|
|
Loading…
Reference in New Issue