From 13dfa1216d5e21de5788326eeee4d84014c07e87 Mon Sep 17 00:00:00 2001 From: Redacted Date: Sat, 12 Jul 2025 17:21:30 -0400 Subject: [PATCH] Linux Fullscreen Graphics Mode --- main.cpp | 9 +++--- src/platform/linux/Window.cpp | 61 +++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/main.cpp b/main.cpp index 944d661..bb2d3ff 100644 --- a/main.cpp +++ b/main.cpp @@ -13,7 +13,10 @@ class MyWindow : public OpenGLWindow { void OnMouseMove(const MouseMoveEvent& e) override {} - void OnKeyDown(const KeyDownEvent& e) override {} + void OnKeyDown(const KeyDownEvent& e) override { + if (e.key == Keys::F11) + SetFullscreen(!IsFullscreen()); + } bool OnResizeRequest(const WindowResizeRequestEvent& e) override { return true; } @@ -37,9 +40,6 @@ class MyWindow : public OpenGLWindow { if (IsMouseButtonDown(MouseButtons::Mouse5)) std::cout << "Mouse5 Mouse Button" << std::endl; - if (IsKeyDown(Keys::N)) - std::cout << "Gotteem" << std::endl; - SwapBuffers(); } }; @@ -52,7 +52,6 @@ int main() { Logger::Debug(std::format("Opened window '{}'", window->GetTitle())); Logger::Debug("TODO: Cannot set flags until after window is open"); - window->SetFullscreen(false); window->SetVsyncEnabled(true); window->SetResizable(true); window->SetKeyRepeatEnabled(false); diff --git a/src/platform/linux/Window.cpp b/src/platform/linux/Window.cpp index a52915a..7dd59e8 100644 --- a/src/platform/linux/Window.cpp +++ b/src/platform/linux/Window.cpp @@ -431,41 +431,68 @@ void RWindow::SetPosition(const std::pair& pos) { } void RWindow::Fullscreen() { - Logger::Info(std::format("Fullscreening '{}'", this->title)); - fullscreen_mode = true; + Atom wm_state = XInternAtom(platform->display, "_NET_WM_STATE", False); + Atom wm_fullscreen = XInternAtom(platform->display, "_NET_WM_STATE_FULLSCREEN", False); - XEvent xev; - Atom wm_state = XInternAtom(platform->display, "_NET_WM_STATE", true); - Atom wm_fullscreen = XInternAtom(platform->display, "_NET_WM_STATE_FULLSCREEN", true); + if (!wm_state || !wm_fullscreen) { + Logger::Error("We don't have the required atom for fullscreen graphics mode?"); + return; + } - XChangeProperty(platform->display, platform->window, wm_state, XA_ATOM, 32, PropModeReplace, (unsigned char *)&wm_fullscreen, 1); - memset(&xev, 0, sizeof(xev)); + XEvent xev{}; xev.type = ClientMessage; xev.xclient.window = platform->window; xev.xclient.message_type = wm_state; xev.xclient.format = 32; - xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD - xev.xclient.data.l[1] = fullscreen_mode; + xev.xclient.data.l[0] = 1; + xev.xclient.data.l[1] = wm_fullscreen; xev.xclient.data.l[2] = 0; - XSendEvent(platform->display, DefaultRootWindow(platform->display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev); + xev.xclient.data.l[3] = 1; + xev.xclient.data.l[4] = 0; + + XSendEvent( + platform->display, + DefaultRootWindow(platform->display), + False, + SubstructureNotifyMask | SubstructureRedirectMask, + &xev + ); + + XFlush(platform->display); + fullscreen_mode = true; Logger::Debug(std::format("Fullscreened '{}'", this->title)); } void RWindow::RestoreFromFullscreen() { - Logger::Debug(std::format("Restoring '{}' from Fullscreen", this->title)); - fullscreen_mode = false; - XEvent xev; Atom wm_state = XInternAtom(platform->display, "_NET_WM_STATE", False); Atom fullscreen = XInternAtom(platform->display, "_NET_WM_STATE_FULLSCREEN", False); - memset(&xev, 0, sizeof(xev)); + + if (!wm_state || !fullscreen) { + Logger::Error("We don't have the required atom for fullscreen graphics mode?"); + return; + } + + XEvent xev{}; xev.type = ClientMessage; xev.xclient.window = platform->window; xev.xclient.message_type = wm_state; xev.xclient.format = 32; - xev.xclient.data.l[0] = 0; // _NET_WM_STATE_REMOVE - xev.xclient.data.l[1] = fullscreen_mode; + xev.xclient.data.l[0] = 0; + xev.xclient.data.l[1] = fullscreen; xev.xclient.data.l[2] = 0; - XSendEvent(platform->display, DefaultRootWindow(platform->display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev); + xev.xclient.data.l[3] = 1; + xev.xclient.data.l[4] = 0; + + XSendEvent( + platform->display, + DefaultRootWindow(platform->display), + False, + SubstructureNotifyMask | SubstructureRedirectMask, + &xev + ); + + XFlush(platform->display); + fullscreen_mode = false; Logger::Debug(std::format("Restored '{}' from Fullscreen", this->title)); }