Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for Mac/X11/Win32 fullscreen window placement #7286

Merged
merged 2 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -1226,11 +1226,12 @@ SDL_DisplayID SDL_GetDisplayForWindowCoordinate(int coordinate)
return displayID;
}

SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
static SDL_DisplayID SDL_GetDisplayForWindowPosition(SDL_Window *window)
{
SDL_DisplayID displayID = 0;

CHECK_WINDOW_MAGIC(window, 0);

if (_this->GetDisplayForWindow) {
displayID = _this->GetDisplayForWindow(_this, window);
}
Expand All @@ -1248,10 +1249,41 @@ SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
if (!displayID) {
displayID = GetDisplayForRect(window->x, window->y, window->w, window->h);
}
if (!displayID && (window->flags & SDL_WINDOW_FULLSCREEN)) {
/* Use the explicit fullscreen display if retrieval via the window position fails */
if (!displayID) {
/* Use the primary display for a window if we can't find it anywhere else */
displayID = SDL_GetPrimaryDisplay();
}
return displayID;
}

SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)
{
SDL_DisplayID displayID = 0;

CHECK_WINDOW_MAGIC(window, 0);

/* An explicit fullscreen display overrides all */
if (window->flags & SDL_WINDOW_FULLSCREEN) {
displayID = window->fullscreen_mode.displayID;
}

if (!displayID && _this->GetDisplayForWindow) {
displayID = _this->GetDisplayForWindow(_this, window);
}

/* A backend implementation may fail to get a display for the window
* (for example if the window is off-screen), but other code may expect it
* to succeed in that situation, so we fall back to a generic position-
* based implementation in that case. */
if (!displayID) {
displayID = SDL_GetDisplayForWindowCoordinate(window->windowed.x);
}
if (!displayID) {
displayID = SDL_GetDisplayForWindowCoordinate(window->windowed.y);
}
if (!displayID) {
displayID = GetDisplayForRect(window->x, window->y, window->w, window->h);
}
if (!displayID) {
/* Use the primary display for a window if we can't find it anywhere else */
displayID = SDL_GetPrimaryDisplay();
Expand All @@ -1261,7 +1293,7 @@ SDL_DisplayID SDL_GetDisplayForWindow(SDL_Window *window)

void SDL_CheckWindowDisplayChanged(SDL_Window *window)
{
SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
SDL_DisplayID displayID = SDL_GetDisplayForWindowPosition(window);

if (displayID != window->last_displayID) {
int i, display_index;
Expand Down Expand Up @@ -1498,17 +1530,8 @@ int SDL_SetWindowFullscreenMode(SDL_Window *window, const SDL_DisplayMode *mode)

if (SDL_WINDOW_FULLSCREEN_VISIBLE(window)) {
SDL_UpdateFullscreenMode(window, SDL_TRUE);
} else if (window->flags & SDL_WINDOW_FULLSCREEN) {
/* If fullscreen and not visible, just update the position so the window will be
* on the correct display when shown/restored.
*/
if (mode) {
SDL_Rect r;
if (SDL_GetDisplayBounds(mode->displayID, &r) == 0) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MOVED, r.x, r.y);
}
}
}

return 0;
}

Expand Down
5 changes: 5 additions & 0 deletions src/video/x11/SDL_x11window.c
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,11 @@ static void X11_SetWindowFullscreenViaWM(_THIS, SDL_Window *window, SDL_VideoDis
X11_XSendEvent(display, RootWindow(display, displaydata->screen), 0,
SubstructureNotifyMask | SubstructureRedirectMask, &e);

/* Set the position so the window will be on the target display */
if (fullscreen) {
X11_XMoveWindow(display, data->xwindow, displaydata->x, displaydata->y);
}

/* Fullscreen windows sometimes end up being marked maximized by
window managers. Force it back to how we expect it to be. */
if (!fullscreen) {
Expand Down