From 45d3bff62010e84471e18f0b62739588e73a2df5 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 22 Aug 2023 10:06:19 +0200 Subject: [PATCH] Unix: Rename X11Display to X11Window and make it work again with Spectre/Window/Window --- engine.cmake | 2 +- source/Platform/Unix/GLXContext.cpp | 6 +- source/Platform/Unix/GLXContext.h | 6 +- source/Platform/Unix/X11Keyboard.cpp | 4 +- source/Platform/Unix/X11Mouse.cpp | 4 +- .../Unix/{X11Display.cpp => X11Window.cpp} | 70 +++++++++---------- .../Unix/{X11Display.h => X11Window.h} | 18 +++-- .../Platform/Unix/X11WindowEventHandler.cpp | 8 +-- source/Platform/Unix/X11WindowEventHandler.h | 4 +- 9 files changed, 60 insertions(+), 62 deletions(-) rename source/Platform/Unix/{X11Display.cpp => X11Window.cpp} (86%) rename source/Platform/Unix/{X11Display.h => X11Window.h} (75%) diff --git a/engine.cmake b/engine.cmake index deeeda0..bf11d58 100644 --- a/engine.cmake +++ b/engine.cmake @@ -119,7 +119,7 @@ set(ENGINE_PLATFORM_UNIX_SRC # X11 source/Platform/Unix/Xlib.cpp source/Platform/Unix/XRandR.cpp - source/Platform/Unix/X11Display.cpp + source/Platform/Unix/X11Window.cpp source/Platform/Unix/X11Input.cpp source/Platform/Unix/X11Keyboard.cpp source/Platform/Unix/X11Mouse.cpp diff --git a/source/Platform/Unix/GLXContext.cpp b/source/Platform/Unix/GLXContext.cpp index 0fee7bc..a39f77b 100644 --- a/source/Platform/Unix/GLXContext.cpp +++ b/source/Platform/Unix/GLXContext.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include "glad_glx.h" @@ -70,12 +70,12 @@ GLXContext::~GLXContext() destroy(); } -bool GLXContext::create(const PlatformDisplay* display) +bool GLXContext::create(const PlatformWindow* window) { // Destroy any previous context first. destroy(); - m_win = (::Window) display->getHandle(); + m_win = (::Window) window->getHandle(); if (!createGLContext()) { destroy(); diff --git a/source/Platform/Unix/GLXContext.h b/source/Platform/Unix/GLXContext.h index 7941e5b..ec0c4f6 100644 --- a/source/Platform/Unix/GLXContext.h +++ b/source/Platform/Unix/GLXContext.h @@ -5,7 +5,7 @@ // X11 OpenGL Context (glx) #include "glad_glx.h" -#include +#include namespace sp { @@ -15,8 +15,8 @@ public : GLXContext(); ~GLXContext(); - // Create a context associated with a display. - bool create(const PlatformDisplay* display); + // Create a context associated with a window. + bool create(const PlatformWindow* window); void destroy(); diff --git a/source/Platform/Unix/X11Keyboard.cpp b/source/Platform/Unix/X11Keyboard.cpp index b345ecb..d1f0b90 100644 --- a/source/Platform/Unix/X11Keyboard.cpp +++ b/source/Platform/Unix/X11Keyboard.cpp @@ -1,7 +1,7 @@ #include #include "X11Keyboard.h" -#include "X11Display.h" +#include "X11Window.h" #include "Xlib.h" #include #include @@ -354,7 +354,7 @@ bool X11Keyboard::handleMessage(XKeyEvent* xkeyevent, Event& event) void X11Keyboard::update(InputModule *input) { - X11Display *focus = X11Display::getFocused(); + X11Window *focus = X11Window::getFocused(); m_win = focus ? (::Window) focus->getHandle() : 0; // If we have focus. diff --git a/source/Platform/Unix/X11Mouse.cpp b/source/Platform/Unix/X11Mouse.cpp index 3c7ccb5..201ac13 100644 --- a/source/Platform/Unix/X11Mouse.cpp +++ b/source/Platform/Unix/X11Mouse.cpp @@ -2,7 +2,7 @@ #include #include #include "Xlib.h" -#include "X11Display.h" +#include "X11Window.h" #include "X11Mouse.h" #define XBUTTON1BIT (1<<0) @@ -89,7 +89,7 @@ void X11Mouse::update(InputModule *input) void X11Mouse::updateFocusedWindow() { - X11Display *focus = X11Display::getFocused(); + X11Window *focus = X11Window::getFocused(); m_win = focus ? (::Window) focus->getHandle() : 0; } diff --git a/source/Platform/Unix/X11Display.cpp b/source/Platform/Unix/X11Window.cpp similarity index 86% rename from source/Platform/Unix/X11Display.cpp rename to source/Platform/Unix/X11Window.cpp index 2fba1b7..cf33c80 100644 --- a/source/Platform/Unix/X11Display.cpp +++ b/source/Platform/Unix/X11Window.cpp @@ -8,7 +8,7 @@ #include "Xlib.h" #include "wm_hints.h" #include "GLXContext.h" -#include "X11Display.h" +#include "X11Window.h" // Sometimes not defined in headers. #ifndef _NET_WM_STATE_TOGGLE @@ -19,17 +19,17 @@ namespace sp { namespace _priv { - // Pointer to the display that has focus (or NULL if none have). - X11Display* focused_display = NULL; + // Pointer to the window that has focus (or NULL if none have). + X11Window* focused_window = NULL; } -X11Display* X11Display::getFocused() +X11Window* X11Window::getFocused() { - return _priv::focused_display; + return _priv::focused_window; } -X11Display:: -X11Display() : +X11Window:: +X11Window() : m_screen (0), m_size (200,200), m_cur_last (0), @@ -37,13 +37,13 @@ m_cur_hidden (0) { } -bool X11Display::create(DisplayDescription description) +bool X11Window::create(WindowDescription description) { XSetWindowAttributes attr; XVisualInfo* vi; Atom protocols; Visual* visual; - Window root_win; + ::Window root_win; ::Display* disp = Xlib::getDisplay(); m_screen = DefaultScreen(disp); @@ -92,7 +92,7 @@ bool X11Display::create(DisplayDescription description) return true; } -void X11Display::destroy() +void X11Window::destroy() { if (m_win) { ::Display* disp = Xlib::getDisplay(); @@ -101,17 +101,17 @@ void X11Display::destroy() } } -bool X11Display::isValid() +bool X11Window::isValid() { return m_win; } -void* X11Display::getHandle() const +void* X11Window::getHandle() const { return (void*) m_win; } -void X11Display::setSize(unsigned int width, unsigned int height) +void X11Window::setSize(unsigned int width, unsigned int height) { Log::info("X11: Set size %dx%d", width, height); @@ -128,7 +128,7 @@ void X11Display::setSize(unsigned int width, unsigned int height) ::XResizeWindow(Xlib::getDisplay(), m_win, m_size.x, m_size.y); } -Vector2u X11Display::getSize() const +Vector2u X11Window::getSize() const { int x, y; unsigned int w, h, bw, d; @@ -146,12 +146,12 @@ Vector2u X11Display::getSize() const return Vector2u(w, h); } -void X11Display::setPosition(unsigned int x, unsigned int y) +void X11Window::setPosition(unsigned int x, unsigned int y) { ::XMoveWindow(Xlib::getDisplay(), m_win, x, y); } -Vector2u X11Display::getPosition() const +Vector2u X11Window::getPosition() const { int x, y; unsigned int w, h, bw, d; @@ -169,7 +169,7 @@ Vector2u X11Display::getPosition() const return Vector2u(x, y); } -void X11Display::setVisible(bool visible) +void X11Window::setVisible(bool visible) { if (visible) { ::XMapWindow(Xlib::getDisplay(), m_win); @@ -178,7 +178,7 @@ void X11Display::setVisible(bool visible) } } -void X11Display::setDecoration(unsigned decoration) +void X11Window::setDecoration(unsigned decoration) { Atom WMHintsAtom = Xlib::getAtom("_MOTIF_WM_HINTS", false); @@ -191,19 +191,19 @@ void X11Display::setDecoration(unsigned decoration) hints.decorations = 0; hints.functions = 0; - if (decoration & DisplayDecorate::Menu) { + if (decoration & WindowDecorate::Menu) { Log::info("X11: Decoration Menu"); hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_TITLE | MWM_DECOR_MINIMIZE | MWM_DECOR_MENU; hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE; } - if (decoration & DisplayDecorate::Resize) { + if (decoration & WindowDecorate::Resize) { Log::info("X11: Decoration Resize"); hints.decorations |= MWM_DECOR_MAXIMIZE | MWM_DECOR_RESIZEH; hints.functions |= MWM_FUNC_MAXIMIZE | MWM_FUNC_RESIZE; } - if (decoration & DisplayDecorate::Close) { + if (decoration & WindowDecorate::Close) { Log::info("X11: Decoration Close"); hints.decorations |= 0; hints.functions |= MWM_FUNC_CLOSE; @@ -215,12 +215,12 @@ void X11Display::setDecoration(unsigned decoration) } } -void X11Display::minimize() +void X11Window::minimize() { ::XIconifyWindow(Xlib::getDisplay(), m_win, m_screen); } -void X11Display::maximize() +void X11Window::maximize() { ::XClientMessageEvent ev = {}; ::Display* disp = Xlib::getDisplay(); @@ -238,7 +238,7 @@ void X11Display::maximize() SubstructureNotifyMask, (XEvent*) &ev); } -void X11Display::enterFullscreen(DisplayMode mode) +void X11Window::enterFullscreen(DisplayMode mode) { if (!XRandR::FindMode(Xlib::getDisplay(), mode.width, mode.height, mode.bpp, &m_fullscreen_mode.size, &m_fullscreen_mode.rate)) { Log::warn("X11: Failed to find a mode"); @@ -248,7 +248,7 @@ void X11Display::enterFullscreen(DisplayMode mode) toggleFullscreen(true); } -void X11Display::toggleFullscreen(bool enable) +void X11Window::toggleFullscreen(bool enable) { ::Display* disp = Xlib::getDisplay(); ::Window root; @@ -272,7 +272,7 @@ void X11Display::toggleFullscreen(bool enable) XRRSetScreenConfigAndRate(disp, conf, root, mode->size, RR_Rotate_0, mode->rate, CurrentTime); } -void X11Display::exitFullscreen() +void X11Window::exitFullscreen() { if (m_fullscreen_mode.rate > 0) { toggleFullscreen(false); @@ -286,7 +286,7 @@ void X11Display::exitFullscreen() #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */ #define _NET_WM_STATE_ADD 1 /* add/set property */ -void X11Display::wm_fullscreen(bool enabled) { +void X11Window::wm_fullscreen(bool enabled) { ::Display* disp = sp::Xlib::getDisplay(); @@ -309,12 +309,12 @@ void X11Display::wm_fullscreen(bool enabled) { } -void X11Display::setCaption(const std::string& caption) +void X11Window::setCaption(const std::string& caption) { ::XStoreName(Xlib::getDisplay(), m_win, caption.c_str()); } -void X11Display::setIcon(unsigned int width, unsigned int height, const uint8_t *pixels) +void X11Window::setIcon(unsigned int width, unsigned int height, const uint8_t *pixels) { ::Display* disp = Xlib::getDisplay(); ::Atom net_wm_icon = Xlib::getAtom("_NET_WM_ICON", False); @@ -342,7 +342,7 @@ void X11Display::setIcon(unsigned int width, unsigned int height, const uint8_t XFlush(disp); } -void X11Display::createHiddenCursor() +void X11Window::createHiddenCursor() { ::Display* disp = Xlib::getDisplay(); XColor c; @@ -361,12 +361,12 @@ void X11Display::createHiddenCursor() ::XFreeGC(disp, gc); } -void X11Display::showCursor(bool value) +void X11Window::showCursor(bool value) { XDefineCursor(Xlib::getDisplay(), m_win, value ? m_cur_last : m_cur_hidden); } -void X11Display::grabCursor(bool value) +void X11Window::grabCursor(bool value) { if (value) { int result = ::XGrabPointer(Xlib::getDisplay(), m_win, True, None, @@ -380,7 +380,7 @@ void X11Display::grabCursor(bool value) } } -void X11Display::processEvent(const ::XEvent& event) +void X11Window::processEvent(const ::XEvent& event) { Vector2u size; @@ -400,7 +400,7 @@ void X11Display::processEvent(const ::XEvent& event) break; case FocusIn: Log::debug("X11: FocusIn"); - _priv::focused_display = this; + _priv::focused_window = this; if (m_fullscreen_mode.rate > 0) { toggleFullscreen(true); @@ -409,7 +409,7 @@ void X11Display::processEvent(const ::XEvent& event) break; case FocusOut: Log::debug("X11: FocusOut"); - _priv::focused_display = NULL; + _priv::focused_window = NULL; if (m_fullscreen_mode.rate > 0) { toggleFullscreen(false); minimize(); diff --git a/source/Platform/Unix/X11Display.h b/source/Platform/Unix/X11Window.h similarity index 75% rename from source/Platform/Unix/X11Display.h rename to source/Platform/Unix/X11Window.h index 2c2ca79..612619a 100644 --- a/source/Platform/Unix/X11Display.h +++ b/source/Platform/Unix/X11Window.h @@ -1,27 +1,25 @@ -#ifndef PLATFORM_UNIX_X11DISPLAY_H -#define PLATFORM_UNIX_X11DISPLAY_H +#ifndef PLATFORM_UNIX_X11WINDOW_H +#define PLATFORM_UNIX_X11WINDOW_H #include #include #include #include -#include +#include #include "Xrandr.h" namespace sp { -// NOTE: This class wraps a X11 Window and Screen handle -// and is NOT a Class wrapper around X11's `Display` type. -class X11Display : public PlatformDisplay +class X11Window : public PlatformWindow { public : - static X11Display* getFocused(); + static X11Window* getFocused(); - X11Display(); + X11Window(); - virtual bool create(DisplayDescription description); + virtual bool create(WindowDescription description); virtual void destroy(); @@ -85,4 +83,4 @@ protected : } // namespace sp -#endif /* PLATFORM_UNIX_X11DISPLAY_H */ +#endif /* PLATFORM_UNIX_X11WINDOW_H */ diff --git a/source/Platform/Unix/X11WindowEventHandler.cpp b/source/Platform/Unix/X11WindowEventHandler.cpp index fa6eb45..afae0eb 100644 --- a/source/Platform/Unix/X11WindowEventHandler.cpp +++ b/source/Platform/Unix/X11WindowEventHandler.cpp @@ -1,15 +1,15 @@ #include -#include "X11Display.h" +#include "X11Window.h" #include "X11WindowEventHandler.h" #include namespace sp { -// Context used to store X11Display pointer. +// Context used to store X11Window pointer. ::XContext X11WindowEventHandler::win_context = None; -void X11WindowEventHandler::registerHandler(::Display* disp, ::Window window, X11Display *ptr) +void X11WindowEventHandler::registerHandler(::Display* disp, ::Window window, X11Window *ptr) { // Initialize context before use. if (win_context == None) { @@ -41,7 +41,7 @@ void X11WindowEventHandler::process(::Display* disp, const ::XEvent& event) // Get the pointer for window ID. if (XFindContext(disp, event.xany.window, win_context, &ptr) == 0) { - X11Display* disp_ptr = (X11Display*) ptr; + X11Window* disp_ptr = (X11Window*) ptr; // Delegate disp_ptr->processEvent(event); diff --git a/source/Platform/Unix/X11WindowEventHandler.h b/source/Platform/Unix/X11WindowEventHandler.h index 79f6ed2..ce6f594 100644 --- a/source/Platform/Unix/X11WindowEventHandler.h +++ b/source/Platform/Unix/X11WindowEventHandler.h @@ -7,13 +7,13 @@ namespace sp { -class X11Display; +class X11Window; class X11WindowEventHandler { public: - static void registerHandler(::Display* disp, ::Window window, X11Display *ptr); + static void registerHandler(::Display* disp, ::Window window, X11Window *ptr); static void unregisterHandler(::Display* disp, ::Window window);