From 4d9ffdb0251af90bfa7aed2004cb3f32ff84601b Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 28 Jan 2023 19:43:11 +0200 Subject: [PATCH] Fix centered window and message box locations on Linux --- .../Engine/Platform/Linux/LinuxPlatform.cpp | 7 +++-- Source/Engine/Platform/Linux/LinuxWindow.cpp | 28 +++++++++++++------ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.cpp b/Source/Engine/Platform/Linux/LinuxPlatform.cpp index a5bd099c9..242f8fb4a 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.cpp +++ b/Source/Engine/Platform/Linux/LinuxPlatform.cpp @@ -431,8 +431,11 @@ static int X11_MessageBoxCreateWindow(MessageBoxData* data) } else { - x = (X11_DisplayWidth(display, data->screen) - data->dialog_width) / 2; - y = (X11_DisplayHeight(display, data->screen) - data->dialog_height) / 3; + int screenCount; + X11::XineramaScreenInfo* xsi = X11::XineramaQueryScreens(xDisplay, &screenCount); + ASSERT(data->screen < screenCount); + x = (float)xsi[data->screen].x_org + ((float)xsi[data->screen].width - data->dialog_width) / 2; + y = (float)xsi[data->screen].y_org + ((float)xsi[data->screen].height - data->dialog_height) / 2; } X11::XMoveWindow(display, data->window, x, y); diff --git a/Source/Engine/Platform/Linux/LinuxWindow.cpp b/Source/Engine/Platform/Linux/LinuxWindow.cpp index cbcfca0af..7694836cb 100644 --- a/Source/Engine/Platform/Linux/LinuxWindow.cpp +++ b/Source/Engine/Platform/Linux/LinuxWindow.cpp @@ -47,6 +47,11 @@ static X11::Time MouseLastButtonPressTime = 0; LinuxWindow::LinuxWindow(const CreateWindowSettings& settings) : WindowBase(settings) { + auto display = (X11::Display*)LinuxPlatform::GetXDisplay(); + if (!display) + return; + auto screen = XDefaultScreen(display); + // Cache data int32 width = Math::TruncToInt(settings.Size.X); int32 height = Math::TruncToInt(settings.Size.Y); @@ -64,9 +69,21 @@ LinuxWindow::LinuxWindow(const CreateWindowSettings& settings) break; case WindowStartPosition::CenterScreen: { - Float2 desktopSize = Platform::GetDesktopSize(); - x = Math::TruncToInt((desktopSize.X - _clientSize.X) * 0.5f); - y = Math::TruncToInt((desktopSize.Y - _clientSize.Y) * 0.5f); + Rectangle desktopBounds; + int event, err; + const bool ok = X11::XineramaQueryExtension(display, &event, &err); + if (X11::XineramaQueryExtension(display, &event, &err)) + { + int count; + X11::XineramaScreenInfo* xsi = X11::XineramaQueryScreens(display, &count); + ASSERT(screen < count); + desktopBounds = Rectangle(Float2((float)xsi[screen].x_org, (float)xsi[screen].y_org), Float2((float)xsi[screen].width, (float)xsi[screen].height)); + X11::XFree(xsi); + } + else + desktopBounds = Rectangle(Float2::Zero, Platform::GetDesktopSize()); + x = Math::TruncToInt(desktopBounds.Location.X + (desktopBounds.Size.X - _clientSize.X) * 0.5f); + y = Math::TruncToInt(desktopBounds.Location.Y + (desktopBounds.Size.Y - _clientSize.Y) * 0.5f); } break; case WindowStartPosition::Manual: @@ -76,11 +93,6 @@ LinuxWindow::LinuxWindow(const CreateWindowSettings& settings) } _resizeDisabled = !settings.HasSizingFrame; - auto display = (X11::Display*)LinuxPlatform::GetXDisplay(); - if (!display) - return; - - auto screen = XDefaultScreen(display); auto rootWindow = XRootWindow(display, screen); long visualMask = VisualScreenMask;