From 023d129a7522a64c39588f1d1653464d0f535f93 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sun, 16 Jul 2017 17:58:18 -1000 Subject: [PATCH] Use smart pointers for Xlib windows --- lib/x11/ApplicationWayland.hpp | 4 ++-- lib/x11/ApplicationXlib.hpp | 23 ++++++++++++----------- lib/x11/WindowWayland.cpp | 9 +++++++-- lib/x11/WindowXlib.cpp | 21 +++++++++++++++------ 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lib/x11/ApplicationWayland.hpp b/lib/x11/ApplicationWayland.hpp index 32cb7bb..f6df2a1 100644 --- a/lib/x11/ApplicationWayland.hpp +++ b/lib/x11/ApplicationWayland.hpp @@ -10,7 +10,7 @@ DBusConnection* RegisterDBus(const char* appName, bool& isFirst); namespace boo { -IWindow* _WindowWaylandNew(const std::string& title); +std::shared_ptr _WindowWaylandNew(const std::string& title); class ApplicationWayland final : public IApplication { @@ -71,7 +71,7 @@ public: return m_args; } - IWindow* newWindow(const std::string& title, uint32_t drawSamples) + std::shared_ptr newWindow(const std::string& title, uint32_t drawSamples) { return _WindowWaylandNew(title); } diff --git a/lib/x11/ApplicationXlib.hpp b/lib/x11/ApplicationXlib.hpp index eb95295..59037e1 100644 --- a/lib/x11/ApplicationXlib.hpp +++ b/lib/x11/ApplicationXlib.hpp @@ -114,10 +114,10 @@ static Window GetWindowOfEvent(XEvent* event, bool& windowEvent) return 0; } -IWindow* _WindowXlibNew(const std::string& title, - Display* display, void* xcbConn, - int defaultScreen, XIM xIM, XIMStyle bestInputStyle, XFontSet fontset, - GLXContext lastCtx, void* vulkanHandle, uint32_t drawSamples); +std::shared_ptr _WindowXlibNew(const std::string& title, + Display* display, void* xcbConn, + int defaultScreen, XIM xIM, XIMStyle bestInputStyle, XFontSet fontset, + GLXContext lastCtx, void* vulkanHandle, uint32_t drawSamples); static XIMStyle ChooseBetterStyle(XIMStyle style1, XIMStyle style2) { @@ -168,7 +168,7 @@ class ApplicationXlib final : public IApplication DBusConnection* m_dbus = nullptr; /* All windows */ - std::unordered_map m_windows; + std::unordered_map> m_windows; Display* m_xDisp = nullptr; XIM m_xIM = nullptr; @@ -467,7 +467,8 @@ public: { auto window = m_windows.find(evWindow); if (window != m_windows.end()) - window->second->_incomingEvent(&event); + if (std::shared_ptr w = window->second.lock()) + w->_incomingEvent(&event); } } XUnlockDisplay(m_xDisp); @@ -525,14 +526,14 @@ public: return m_args; } - IWindow* newWindow(const std::string& title, uint32_t drawSamples) + std::shared_ptr newWindow(const std::string& title, uint32_t drawSamples) { #if BOO_HAS_VULKAN - IWindow* newWindow = _WindowXlibNew(title, m_xDisp, m_xcbConn, m_xDefaultScreen, m_xIM, - m_bestStyle, m_fontset, m_lastGlxCtx, (void*)m_getVkProc, drawSamples); + std::shared_ptr newWindow = _WindowXlibNew(title, m_xDisp, m_xcbConn, m_xDefaultScreen, m_xIM, + m_bestStyle, m_fontset, m_lastGlxCtx, (void*)m_getVkProc, drawSamples); #else - IWindow* newWindow = _WindowXlibNew(title, m_xDisp, nullptr, m_xDefaultScreen, m_xIM, - m_bestStyle, m_fontset, m_lastGlxCtx, nullptr, drawSamples); + std::shared_ptr newWindow = _WindowXlibNew(title, m_xDisp, nullptr, m_xDefaultScreen, m_xIM, + m_bestStyle, m_fontset, m_lastGlxCtx, nullptr, drawSamples); #endif m_windows[(Window)newWindow->getPlatformHandle()] = newWindow; return newWindow; diff --git a/lib/x11/WindowWayland.cpp b/lib/x11/WindowWayland.cpp index 11abc75..3db2967 100644 --- a/lib/x11/WindowWayland.cpp +++ b/lib/x11/WindowWayland.cpp @@ -108,6 +108,11 @@ struct WindowWayland : IWindow void setCallback(IWindowCallback* cb) { + } + + void closeWindow() + { + } void showWindow() @@ -239,9 +244,9 @@ struct WindowWayland : IWindow }; -IWindow* _WindowWaylandNew(const std::string& title) +std::shared_ptr _WindowWaylandNew(const std::string& title) { - return new WindowWayland(title); + return std::make_shared(title); } } diff --git a/lib/x11/WindowXlib.cpp b/lib/x11/WindowXlib.cpp index 5af7c1b..60a1646 100644 --- a/lib/x11/WindowXlib.cpp +++ b/lib/x11/WindowXlib.cpp @@ -1070,6 +1070,14 @@ public: { m_callback = cb; } + + void closeWindow() + { + // TODO: Free window resources and prevent further access + XLockDisplay(m_xDisp); + XUnmapWindow(m_xDisp, m_windowId); + XUnlockDisplay(m_xDisp); + } void showWindow() { @@ -1980,14 +1988,15 @@ public: } }; -IWindow* _WindowXlibNew(const std::string& title, - Display* display, void* xcbConn, - int defaultScreen, XIM xIM, XIMStyle bestInputStyle, XFontSet fontset, - GLXContext lastCtx, void* vulkanHandle, uint32_t drawSamples) +std::shared_ptr _WindowXlibNew(const std::string& title, + Display* display, void* xcbConn, + int defaultScreen, XIM xIM, XIMStyle bestInputStyle, XFontSet fontset, + GLXContext lastCtx, void* vulkanHandle, uint32_t drawSamples) { XLockDisplay(display); - IWindow* ret = new WindowXlib(title, display, xcbConn, defaultScreen, xIM, - bestInputStyle, fontset, lastCtx, vulkanHandle, drawSamples); + std::shared_ptr ret = std::make_shared(title, display, xcbConn, + defaultScreen, xIM, bestInputStyle, fontset, lastCtx, + vulkanHandle, drawSamples); XUnlockDisplay(display); return ret; }