From 090646b61aee413cff7e20a75f553702c01dbeba Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 25 Dec 2020 17:17:51 +0100 Subject: [PATCH] Platform/Unix: Rename X11SharedDisplay to Xlib, and remove Display member variable from all classes. We now initialize/destroy the display in Xlib::init/shutdown that is called in UnixApplication::init/shutdown and therefore is valid through the whole lifetime. So no need for classes to keep references. --- engine.build.lua | 2 +- source/Platform/Unix/GLXContext.cpp | 37 +++---- source/Platform/Unix/GLXContext.h | 2 - source/Platform/Unix/UnixApplication.cpp | 5 +- source/Platform/Unix/UnixMisc.cpp | 13 +-- source/Platform/Unix/X11Display.cpp | 98 +++++++++---------- source/Platform/Unix/X11Display.h | 2 - source/Platform/Unix/X11EventQueue.cpp | 30 ++---- source/Platform/Unix/X11EventQueue.h | 5 - source/Platform/Unix/X11Keyboard.cpp | 10 +- source/Platform/Unix/X11Mouse.cpp | 17 +--- source/Platform/Unix/X11Mouse.h | 2 - source/Platform/Unix/X11SharedDisplay.cpp | 40 -------- source/Platform/Unix/X11SharedDisplay.h | 18 ---- .../Platform/Unix/X11WindowEventHandler.cpp | 1 - source/Platform/Unix/Xlib.cpp | 34 +++++++ source/Platform/Unix/Xlib.h | 22 +++++ 17 files changed, 137 insertions(+), 201 deletions(-) delete mode 100644 source/Platform/Unix/X11SharedDisplay.cpp delete mode 100644 source/Platform/Unix/X11SharedDisplay.h create mode 100644 source/Platform/Unix/Xlib.cpp create mode 100644 source/Platform/Unix/Xlib.h diff --git a/engine.build.lua b/engine.build.lua index 831d539..c050a73 100644 --- a/engine.build.lua +++ b/engine.build.lua @@ -64,8 +64,8 @@ if TARGET_OS == "Win32" then }) elseif TARGET_OS == "Unix" then platform_spec_module = Module("source/Platform/Unix", { + "Xlib.cpp", "UnixApplication.cpp", - "X11SharedDisplay.cpp", "X11Display.cpp", "GLXContext.cpp", "X11Input.cpp", diff --git a/source/Platform/Unix/GLXContext.cpp b/source/Platform/Unix/GLXContext.cpp index 53465a5..ea93438 100644 --- a/source/Platform/Unix/GLXContext.cpp +++ b/source/Platform/Unix/GLXContext.cpp @@ -4,7 +4,7 @@ #include #include "glad_glx.h" -#include "X11SharedDisplay.h" +#include "Xlib.h" #include "GLXContext.h" namespace sp { @@ -60,7 +60,6 @@ static bool ensureExtensionsLoaded(::Display* disp, int screen) } GLXContext::GLXContext() : -m_disp (NULL), m_win (0), m_ctx (NULL) { @@ -76,13 +75,6 @@ bool GLXContext::create(const PlatformDisplay* display) // Destroy any previous context first. destroy(); - m_disp = XGetDisplay(); - if (m_disp == NULL) { - destroy(); - Log::warn("X11: Could not open display"); - return false; - } - m_win = (::Window) display->getHandle(); if (!createGLContext()) { @@ -95,6 +87,7 @@ bool GLXContext::create(const PlatformDisplay* display) bool GLXContext::createGLContext() { + ::Display* disp = Xlib::getDisplay(); ::GLXFBConfig *fbc; ::XVisualInfo *info; int fbcount; @@ -117,31 +110,31 @@ bool GLXContext::createGLContext() }; // Ensure glx extensions are loaded. - if (!ensureExtensionsLoaded(m_disp, DefaultScreen(m_disp))) { + if (!ensureExtensionsLoaded(disp, DefaultScreen(disp))) { return false; } - info = ::glXChooseVisual(m_disp, DefaultScreen(m_disp), vi_attr); + info = ::glXChooseVisual(disp, DefaultScreen(disp), vi_attr); if (info == NULL) { Log::warn("GLX: Could not find a valid VisualInfo."); return false; } // Setup GL settings. - fbc = glXChooseFBConfig(m_disp, DefaultScreen(m_disp), (const int*) info->visual, &fbcount); + fbc = glXChooseFBConfig(disp, DefaultScreen(disp), (const int*) info->visual, &fbcount); if (fbc == NULL) { Log::warn("GLX: Could not find FB Config."); return false; } // Create context. - m_ctx = glXCreateContextAttribsARB(m_disp, fbc[0], NULL, GL_TRUE, ctx_attr); + m_ctx = glXCreateContextAttribsARB(disp, fbc[0], NULL, GL_TRUE, ctx_attr); if (m_ctx == NULL) { Log::warn("GLX: Failed to create context."); return false; } - glXMakeCurrent(m_disp, m_win, m_ctx); + glXMakeCurrent(disp, m_win, m_ctx); // Load OpenGL if (!loadGL()) { @@ -155,29 +148,24 @@ bool GLXContext::createGLContext() void GLXContext::destroy() { if (m_ctx) { - ::glXDestroyContext(m_disp, m_ctx); + ::glXDestroyContext(Xlib::getDisplay(), m_ctx); m_ctx = NULL; } - if (m_disp) { - XReleaseDisplay(m_disp); - m_disp = NULL; - } - m_win = None; } bool GLXContext::activate() { if (m_win && m_ctx) { - return ::glXMakeCurrent(m_disp, m_win, m_ctx); + return ::glXMakeCurrent(Xlib::getDisplay(), m_win, m_ctx); } return false; } bool GLXContext::deactivate() { - return ::glXMakeCurrent(m_disp, None, NULL); + return ::glXMakeCurrent(Xlib::getDisplay(), None, NULL); } bool GLXContext::isActive() const @@ -187,7 +175,8 @@ bool GLXContext::isActive() const bool GLXContext::setSwapInterval(int interval) { - ensureExtensionsLoaded(m_disp, DefaultScreen(m_disp)); + ::Display *disp = Xlib::getDisplay(); + ensureExtensionsLoaded(disp, DefaultScreen(disp)); if (GLAD_GLX_MESA_swap_control) { return glXSwapIntervalMESA(interval); @@ -215,7 +204,7 @@ void GLXContext::setSize(const Vector2u size) void GLXContext::swapBuffers() { - glXSwapBuffers(m_disp, m_win); + glXSwapBuffers(Xlib::getDisplay(), m_win); } } // namespace sp diff --git a/source/Platform/Unix/GLXContext.h b/source/Platform/Unix/GLXContext.h index 305d789..878a6f7 100644 --- a/source/Platform/Unix/GLXContext.h +++ b/source/Platform/Unix/GLXContext.h @@ -39,8 +39,6 @@ private : private : - ::Display* m_disp; - ::Window m_win; ::GLXContext m_ctx; diff --git a/source/Platform/Unix/UnixApplication.cpp b/source/Platform/Unix/UnixApplication.cpp index a8925d4..f2c175f 100644 --- a/source/Platform/Unix/UnixApplication.cpp +++ b/source/Platform/Unix/UnixApplication.cpp @@ -1,16 +1,17 @@ #include "UnixApplication.h" +#include "Xlib.h" namespace sp { void UnixApplication::init() { - // TODO + Xlib::init(); } void UnixApplication::shutdown() { - // TODO + Xlib::shutdown(); } PlatformInput& UnixApplication::getInput() diff --git a/source/Platform/Unix/UnixMisc.cpp b/source/Platform/Unix/UnixMisc.cpp index afe2deb..fd557f2 100644 --- a/source/Platform/Unix/UnixMisc.cpp +++ b/source/Platform/Unix/UnixMisc.cpp @@ -1,8 +1,7 @@ #include #include -#include "X11SharedDisplay.h" -#include +#include "Xlib.h" #include #include @@ -13,7 +12,7 @@ namespace sp { void PlatformMisc::GetDisplayModes(std::vector& modes) { - ::Display *disp = XGetDisplay(); + ::Display *disp = Xlib::getDisplay(); if (disp) { int scr = DefaultScreen(disp); @@ -28,7 +27,7 @@ void PlatformMisc::GetDisplayModes(std::vector& modes) XRRScreenSize* sizes = ::XRRConfigSizes(config, &nbSizes); if (sizes && nbSizes > 0) { int nbDepths = 0; - int* depths = XListDepths(disp, scr, &nbDepths); + int* depths = ::XListDepths(disp, scr, &nbDepths); if (depths && nbDepths > 0) { for(int i = 0; i < nbDepths; i++) { @@ -59,8 +58,6 @@ void PlatformMisc::GetDisplayModes(std::vector& modes) } else { Log::error("Failed to use XRandR extension while trying to get display modes."); } - - XReleaseDisplay(disp); } else { Log::error("Failed to connect to the X server while trying to get display modes."); } @@ -70,7 +67,7 @@ DisplayMode PlatformMisc::GetDesktopMode() { DisplayMode mode; - ::Display *disp = XGetDisplay(); + ::Display *disp = Xlib::getDisplay(); if (disp) { int scr = DefaultScreen(disp); @@ -103,8 +100,6 @@ DisplayMode PlatformMisc::GetDesktopMode() } else { Log::error("Failed to use XRandR extension while trying to get desktop display mode."); } - - XReleaseDisplay(disp); } else { Log::error("Failed to connect to the X server while trying to get desktop display mode."); } diff --git a/source/Platform/Unix/X11Display.cpp b/source/Platform/Unix/X11Display.cpp index 577ee10..d960670 100644 --- a/source/Platform/Unix/X11Display.cpp +++ b/source/Platform/Unix/X11Display.cpp @@ -2,7 +2,7 @@ #include #include #include "X11WindowEventHandler.h" -#include "X11SharedDisplay.h" +#include "Xlib.h" #include "GLXContext.h" #include "X11Display.h" @@ -27,7 +27,6 @@ X11Display* X11Display::getFocused() X11Display:: X11Display() : m_screen (0), -m_disp (NULL), m_size (200,200), m_cur_last (0), m_cur_hidden (0) @@ -41,49 +40,44 @@ bool X11Display::create(DisplayDescription description) Atom protocols; Visual* visual; Window root_win; + ::Display* disp = Xlib::getDisplay(); - m_disp = XGetDisplay(); - if (m_disp == NULL) { - Log::warn("X11: Could not open display"); - return false; - } + m_screen = DefaultScreen(disp); + root_win = XRootWindow(disp, m_screen); + visual = DefaultVisual(disp, m_screen); - m_screen = DefaultScreen(m_disp); - root_win = XRootWindow(m_disp, m_screen); - visual = DefaultVisual(m_disp, m_screen); - - attr.border_pixel = BlackPixel(m_disp, m_screen); - attr.background_pixel = WhitePixel(m_disp, m_screen); + attr.border_pixel = BlackPixel(disp, m_screen); + attr.background_pixel = WhitePixel(disp, m_screen); //attr.override_redirect = True; - attr.colormap = ::XCreateColormap(m_disp, root_win, visual, AllocNone); + attr.colormap = ::XCreateColormap(disp, root_win, visual, AllocNone); // We want InputFocus,Keyboard,Mouse,Resize,Exposure (repaint) events. attr.event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | ExposureMask; - m_win = ::XCreateWindow(m_disp, root_win, + m_win = ::XCreateWindow(disp, root_win, 0, 0, /* Position */ - m_size.x, m_size.y, 0 /* Border width */, DefaultDepth(m_disp, m_screen), + m_size.x, m_size.y, 0 /* Border width */, DefaultDepth(disp, m_screen), InputOutput, visual, CWBackPixel | CWColormap | CWBorderPixel | CWEventMask, &attr); // Register event handler - X11WindowEventHandler::registerHandler(m_disp, m_win, this); + X11WindowEventHandler::registerHandler(disp, m_win, this); // X11 does not handle pressing the X button on a window. // that is the job of the window manager. // Here we can request the WM to send us a delete event so we can handle it ourself. - protocols = getAtom("WM_DELETE_WINDOW"); - XSetWMProtocols(m_disp, m_win, &protocols, 1); + protocols = Xlib::getAtom("WM_DELETE_WINDOW"); + XSetWMProtocols(disp, m_win, &protocols, 1); setVisible(true); setSize(description.mode.width, description.mode.height); // Clear and take focus - XClearWindow(m_disp, m_win); - XMapRaised(m_disp, m_win); + XClearWindow(disp, m_win); + XMapRaised(disp, m_win); Log::info("X11: Created display"); @@ -94,21 +88,16 @@ bool X11Display::create(DisplayDescription description) void X11Display::destroy() { - if (m_disp) { - - if (m_win) { - X11WindowEventHandler::unregisterHandler(m_disp, m_win); - ::XDestroyWindow(m_disp, m_win); - } - - XReleaseDisplay(m_disp); - m_disp = NULL; + if (m_win) { + ::Display* disp = Xlib::getDisplay(); + X11WindowEventHandler::unregisterHandler(disp, m_win); + ::XDestroyWindow(disp, m_win); } } bool X11Display::isValid() { - return m_disp && m_win; + return m_win; } void* X11Display::getHandle() const @@ -128,7 +117,7 @@ void X11Display::setSize(unsigned int width, unsigned int height) } m_size = Vector2u(width, height); - ::XResizeWindow(m_disp, m_win, m_size.x, m_size.y); + ::XResizeWindow(Xlib::getDisplay(), m_win, m_size.x, m_size.y); } Vector2u X11Display::getSize() const @@ -138,7 +127,7 @@ Vector2u X11Display::getSize() const void X11Display::setPosition(unsigned int x, unsigned int y) { - ::XMoveWindow(m_disp, m_win, x, y); + ::XMoveWindow(Xlib::getDisplay(), m_win, x, y); } Vector2u X11Display::getPosition() const @@ -146,7 +135,7 @@ Vector2u X11Display::getPosition() const Vector2u pos(0, 0); XWindowAttributes attr; - if (XGetWindowAttributes(m_disp, m_win, &attr)) { + if (XGetWindowAttributes(Xlib::getDisplay(), m_win, &attr)) { pos.x = attr.x; pos.y = attr.y; } @@ -156,37 +145,38 @@ Vector2u X11Display::getPosition() const void X11Display::setVisible(bool visible) { if (visible) { - ::XMapWindow(m_disp, m_win); + ::XMapWindow(Xlib::getDisplay(), m_win); } else { - ::XUnmapWindow(m_disp, m_win); + ::XUnmapWindow(Xlib::getDisplay(), m_win); } } void X11Display::minimize() { - ::XIconifyWindow(m_disp, m_win, m_screen); + ::XIconifyWindow(Xlib::getDisplay(), m_win, m_screen); } void X11Display::maximize() { ::XClientMessageEvent ev = {}; + ::Display* disp = Xlib::getDisplay(); ev.type = ClientMessage; ev.window = m_win; - ev.message_type = getAtom("_NET_WM_STATE"); + ev.message_type = Xlib::getAtom("_NET_WM_STATE"); ev.format = 32; ev.data.l[0] = _NET_WM_STATE_TOGGLE; - ev.data.l[1] = getAtom("_NET_WM_STATE_MAXIMIZED_HORZ"); - ev.data.l[2] = getAtom("_NET_WM_STATE_MAXIMIZED_VERT"); + ev.data.l[1] = Xlib::getAtom("_NET_WM_STATE_MAXIMIZED_HORZ"); + ev.data.l[2] = Xlib::getAtom("_NET_WM_STATE_MAXIMIZED_VERT"); ev.data.l[3] = 1; - ::XSendEvent(m_disp, DefaultRootWindow(m_disp), False, + ::XSendEvent(disp, DefaultRootWindow(disp), False, SubstructureNotifyMask, (XEvent*) &ev); } void X11Display::setCaption(const std::string& caption) { - ::XStoreName(m_disp, m_win, caption.c_str()); + ::XStoreName(Xlib::getDisplay(), m_win, caption.c_str()); } // TODO: Move this up to the non-platform layer. @@ -201,8 +191,9 @@ void X11Display::setIcon(const std::string& icon) void X11Display::setIcon(unsigned int width, unsigned int height, const uint8_t *pixels) { - ::Atom net_wm_icon = getAtom("_NET_WM_ICON", False); - ::Atom cardinal = getAtom("CARDINAL", False); + ::Display* disp = Xlib::getDisplay(); + ::Atom net_wm_icon = Xlib::getAtom("_NET_WM_ICON", False); + ::Atom cardinal = Xlib::getAtom("CARDINAL", False); std::vector buffer(2 + width * height); uint64_t *ptr = &buffer[0]; @@ -217,36 +208,37 @@ void X11Display::setIcon(unsigned int width, unsigned int height, const uint8_t | (pixels[i * 4 + 3] << 24); } - ::XChangeProperty(m_disp, m_win, + ::XChangeProperty(disp, m_win, net_wm_icon, cardinal, 32, PropModeReplace, reinterpret_cast(&buffer[0]), 2 + width * height); - XFlush(m_disp); + XFlush(disp); } void X11Display::createHiddenCursor() { + ::Display* disp = Xlib::getDisplay(); XColor c; - Pixmap pix = ::XCreatePixmap(m_disp, m_win, 1, 1, 1); - GC gc = ::XCreateGC(m_disp, pix, 0, NULL); + Pixmap pix = ::XCreatePixmap(disp, m_win, 1, 1, 1); + GC gc = ::XCreateGC(disp, pix, 0, NULL); // Draw transparent pixel. - ::XDrawPoint(m_disp, pix, gc, 0, 0); + ::XDrawPoint(disp, pix, gc, 0, 0); c.red = c.green = c.blue = 0; c.flags = DoRed | DoGreen | DoBlue; - m_cur_hidden = XCreatePixmapCursor(m_disp, pix, pix, &c, &c, 0, 0); + m_cur_hidden = XCreatePixmapCursor(disp, pix, pix, &c, &c, 0, 0); // Free GC and pixmap. - ::XFreePixmap(m_disp, pix); - ::XFreeGC(m_disp, gc); + ::XFreePixmap(disp, pix); + ::XFreeGC(disp, gc); } void X11Display::showCursor(bool value) { - XDefineCursor(m_disp, m_win, value ? m_cur_last : m_cur_hidden); + XDefineCursor(Xlib::getDisplay(), m_win, value ? m_cur_last : m_cur_hidden); } void X11Display::grabCursor(bool value) diff --git a/source/Platform/Unix/X11Display.h b/source/Platform/Unix/X11Display.h index 708e4ca..403aaaf 100644 --- a/source/Platform/Unix/X11Display.h +++ b/source/Platform/Unix/X11Display.h @@ -60,8 +60,6 @@ protected : protected : - ::Display* m_disp; - ::Window m_win; int m_screen; diff --git a/source/Platform/Unix/X11EventQueue.cpp b/source/Platform/Unix/X11EventQueue.cpp index d251eab..86931f8 100644 --- a/source/Platform/Unix/X11EventQueue.cpp +++ b/source/Platform/Unix/X11EventQueue.cpp @@ -1,38 +1,24 @@ #include + #include "X11EventQueue.h" -#include "X11SharedDisplay.h" +#include "Xlib.h" #include "X11Keyboard.h" #include "X11Mouse.h" #include "X11WindowEventHandler.h" namespace sp { -X11EventQueue::X11EventQueue() -{ - m_disp = XGetDisplay(); -} - -X11EventQueue::~X11EventQueue() -{ - if (m_disp) { - XReleaseDisplay(m_disp); - } -} - bool X11EventQueue::poll(Event& event) { - Atom del_win = getAtom("WM_DELETE_WINDOW"); - Atom wm_proto = getAtom("WM_PROTOCOLS"); + ::Display* disp = Xlib::getDisplay(); + Atom del_win = Xlib::getAtom("WM_DELETE_WINDOW"); + Atom wm_proto = Xlib::getAtom("WM_PROTOCOLS"); - if (m_disp == NULL) { - return false; - } - - if (XPending(m_disp)) { + if (XPending(disp)) { XEvent xevent; - XNextEvent(m_disp, &xevent); + XNextEvent(disp, &xevent); switch(xevent.type) { case ClientMessage: @@ -64,7 +50,7 @@ bool X11EventQueue::poll(Event& event) default: // Pass to window. Log::info("X11: Window Event"); - X11WindowEventHandler::process(m_disp, xevent); + X11WindowEventHandler::process(disp, xevent); } } diff --git a/source/Platform/Unix/X11EventQueue.h b/source/Platform/Unix/X11EventQueue.h index 73b363f..b34c3dc 100644 --- a/source/Platform/Unix/X11EventQueue.h +++ b/source/Platform/Unix/X11EventQueue.h @@ -11,13 +11,8 @@ namespace sp { class X11EventQueue : public PlatformEventQueue { public : - X11EventQueue(); - virtual ~X11EventQueue(); virtual bool poll(Event& event); - -private : - ::Display* m_disp; }; } // namespace sp diff --git a/source/Platform/Unix/X11Keyboard.cpp b/source/Platform/Unix/X11Keyboard.cpp index a7e30df..b345ecb 100644 --- a/source/Platform/Unix/X11Keyboard.cpp +++ b/source/Platform/Unix/X11Keyboard.cpp @@ -2,7 +2,7 @@ #include #include "X11Keyboard.h" #include "X11Display.h" -#include "X11SharedDisplay.h" +#include "Xlib.h" #include #include #include @@ -211,14 +211,10 @@ m_win(0) X11Keyboard::~X11Keyboard() { - if (m_disp) { - XReleaseDisplay(m_disp); - } } void X11Keyboard::init() { - m_disp = XGetDisplay(); } bool X11Keyboard::isKeyDown(Keyboard::Key key) @@ -334,7 +330,7 @@ bool X11Keyboard::isKeyDown(Keyboard::Key key) default : sym = 0; break; } - KeyCode keycode = ::XKeysymToKeycode(m_disp, sym); + KeyCode keycode = ::XKeysymToKeycode(Xlib::getDisplay(), sym); if (keycode) { return m_key_state[keycode / 8] & (1 << (keycode % 8)); @@ -365,7 +361,7 @@ void X11Keyboard::update(InputModule *input) if (m_win) { // Query keyboard state. - ::XQueryKeymap(m_disp, m_key_state); + ::XQueryKeymap(Xlib::getDisplay(), m_key_state); } } diff --git a/source/Platform/Unix/X11Mouse.cpp b/source/Platform/Unix/X11Mouse.cpp index 9ef045a..cabbedc 100644 --- a/source/Platform/Unix/X11Mouse.cpp +++ b/source/Platform/Unix/X11Mouse.cpp @@ -1,29 +1,19 @@ #include #include +#include "Xlib.h" #include "X11Display.h" #include "X11Mouse.h" -#include "X11SharedDisplay.h" - namespace sp { X11Mouse::X11Mouse() : -m_disp(NULL), m_btn_state(0) { } -X11Mouse::~X11Mouse() -{ - if (m_disp) { - XReleaseDisplay(m_disp); - } -} - void X11Mouse::init() { - m_disp = XGetDisplay(); updateFocusedWindow(); } @@ -62,14 +52,15 @@ bool X11Mouse::isButtonDown(Mouse::Button button) const void X11Mouse::update(InputModule *input) { + ::Display* disp = Xlib::getDisplay(); ::Window root, child; int rx, ry, x = 0, y = 0; updateFocusedWindow(); // Query position and button state. - XQueryPointer(m_disp, - m_win ? m_win : ::XDefaultRootWindow(m_disp), + XQueryPointer(disp, + m_win ? m_win : ::XDefaultRootWindow(disp), &root, &child, &rx, &ry, &x, &y, &m_btn_state); diff --git a/source/Platform/Unix/X11Mouse.h b/source/Platform/Unix/X11Mouse.h index 26717c2..9cb0872 100644 --- a/source/Platform/Unix/X11Mouse.h +++ b/source/Platform/Unix/X11Mouse.h @@ -11,7 +11,6 @@ class X11Mouse : public Mouse { public : X11Mouse(); - ~X11Mouse(); virtual void init(); @@ -32,7 +31,6 @@ protected : void updateFocusedWindow(); protected : - ::Display* m_disp; ::Window m_win; // Focused window. Vector2f m_position; diff --git a/source/Platform/Unix/X11SharedDisplay.cpp b/source/Platform/Unix/X11SharedDisplay.cpp deleted file mode 100644 index 51f0d10..0000000 --- a/source/Platform/Unix/X11SharedDisplay.cpp +++ /dev/null @@ -1,40 +0,0 @@ - -#include -#include "X11SharedDisplay.h" - -namespace sp { - -// Define a global display (for simplicity, we always connect to local one.) -::Display* sharedDisplay = NULL; -unsigned int refcount = 0; - -::Display* XGetDisplay() { - - // Create if we dont have any references. - if (refcount == 0) { - sharedDisplay = XOpenDisplay(NULL); - } - - refcount++; - return sharedDisplay; -} - -void XReleaseDisplay(::Display* disp) { - - assert(disp == sharedDisplay); - - if (refcount < 1) { - return; - } - - if (--refcount == 0) { - XCloseDisplay(sharedDisplay); - } -} - -Atom getAtom(const std::string& name, bool onlyIfExists) { - - return XInternAtom(sharedDisplay, name.c_str(), onlyIfExists); -} - -} // namespace sp diff --git a/source/Platform/Unix/X11SharedDisplay.h b/source/Platform/Unix/X11SharedDisplay.h deleted file mode 100644 index b662afc..0000000 --- a/source/Platform/Unix/X11SharedDisplay.h +++ /dev/null @@ -1,18 +0,0 @@ - -#ifndef SPECTRE_PLATFORM_UNIX_X11SHAREDDISPLAY_H -#define SPECTRE_PLATFORM_UNIX_X11SHAREDDISPLAY_H - -#include -#include - -namespace sp { - -::Display* XGetDisplay(); - -void XReleaseDisplay(::Display* disp); - -Atom getAtom(const std::string& name, bool onlyIfExists = false); - -} // namespace sp - -#endif /* SPECTRE_PLATFORM_UNIX_X11SHAREDDISPLAY_H */ diff --git a/source/Platform/Unix/X11WindowEventHandler.cpp b/source/Platform/Unix/X11WindowEventHandler.cpp index a4ec542..fa6eb45 100644 --- a/source/Platform/Unix/X11WindowEventHandler.cpp +++ b/source/Platform/Unix/X11WindowEventHandler.cpp @@ -1,7 +1,6 @@ #include #include "X11Display.h" -#include "X11SharedDisplay.h" #include "X11WindowEventHandler.h" #include diff --git a/source/Platform/Unix/Xlib.cpp b/source/Platform/Unix/Xlib.cpp new file mode 100644 index 0000000..32e36fb --- /dev/null +++ b/source/Platform/Unix/Xlib.cpp @@ -0,0 +1,34 @@ + +#include +#include "Xlib.h" + +namespace sp { namespace Xlib { + +namespace _priv { + // Define a global display (for simplicity, we always connect to local one.) + ::Display* display = NULL; +}; + +void init() +{ + _priv::display = XOpenDisplay(NULL); +} + +void shutdown() +{ + XCloseDisplay(_priv::display); + _priv::display = NULL; +} + +::Display* getDisplay() +{ + assert(_priv::display != NULL); + return _priv::display; +} + +::Atom getAtom(const std::string& name, bool onlyIfExists) +{ + return XInternAtom(getDisplay(), name.c_str(), onlyIfExists); +} + +} } // namespace sp::Xlib diff --git a/source/Platform/Unix/Xlib.h b/source/Platform/Unix/Xlib.h new file mode 100644 index 0000000..cc5cb2d --- /dev/null +++ b/source/Platform/Unix/Xlib.h @@ -0,0 +1,22 @@ + +#ifndef SPECTRE_PLATFORM_UNIX_XLIB_H +#define SPECTRE_PLATFORM_UNIX_XLIB_H + +#include +#include + +// Wrapper for some Xlib functions. + +namespace sp { namespace Xlib { + +void init(); + +void shutdown(); + +::Display* getDisplay(); + +::Atom getAtom(const std::string& name, bool onlyIfExists = false); + +} } // namespace sp::xlib + +#endif /* SPECTRE_PLATFORM_UNIX_XLIB_H */