From 7402896ce69e03d0218d7803f3f5c851b66207bb Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 30 Dec 2019 05:33:14 +0100 Subject: [PATCH] Adding Platform/Unix/X11WindowEventHandler --- engine.build.lua | 1 + .../Platform/Unix/X11WindowEventHandler.cpp | 52 +++++++++++++++++++ source/Platform/Unix/X11WindowEventHandler.h | 29 +++++++++++ 3 files changed, 82 insertions(+) create mode 100644 source/Platform/Unix/X11WindowEventHandler.cpp create mode 100644 source/Platform/Unix/X11WindowEventHandler.h diff --git a/engine.build.lua b/engine.build.lua index 55ff80f..831d539 100644 --- a/engine.build.lua +++ b/engine.build.lua @@ -72,6 +72,7 @@ elseif TARGET_OS == "Unix" then "X11Keyboard.cpp", "X11Mouse.cpp", "X11EventQueue.cpp", + "X11WindowEventHandler.cpp", "UnixMisc.cpp", "UnixSystem.cpp", "glad_glx.c" diff --git a/source/Platform/Unix/X11WindowEventHandler.cpp b/source/Platform/Unix/X11WindowEventHandler.cpp new file mode 100644 index 0000000..a4ec542 --- /dev/null +++ b/source/Platform/Unix/X11WindowEventHandler.cpp @@ -0,0 +1,52 @@ + +#include +#include "X11Display.h" +#include "X11SharedDisplay.h" +#include "X11WindowEventHandler.h" +#include + +namespace sp { + +// Context used to store X11Display pointer. +::XContext X11WindowEventHandler::win_context = None; + +void X11WindowEventHandler::registerHandler(::Display* disp, ::Window window, X11Display *ptr) +{ + // Initialize context before use. + if (win_context == None) { + win_context = XUniqueContext(); + } + + // Save pointer to window. + XSaveContext(disp, window, win_context, (XPointer) ptr); +} + +void X11WindowEventHandler::unregisterHandler(::Display* disp, ::Window window) +{ + // No context. nothing to do. + if (win_context == None) { + return; + } + + XDeleteContext(disp, window, win_context); +} + +void X11WindowEventHandler::process(::Display* disp, const ::XEvent& event) +{ + XPointer ptr; + + // No context. nothing to do. + if (win_context == None) { + return; + } + + // Get the pointer for window ID. + if (XFindContext(disp, event.xany.window, win_context, &ptr) == 0) { + X11Display* disp_ptr = (X11Display*) ptr; + + // Delegate + disp_ptr->processEvent(event); + } +} + +} // namespace sp diff --git a/source/Platform/Unix/X11WindowEventHandler.h b/source/Platform/Unix/X11WindowEventHandler.h new file mode 100644 index 0000000..79f6ed2 --- /dev/null +++ b/source/Platform/Unix/X11WindowEventHandler.h @@ -0,0 +1,29 @@ + +#ifndef PLATFORM_UNIX_X11_WINDOW_EVENT_HANDLER_H +#define PLATFORM_UNIX_X11_WINDOW_EVENT_HANDLER_H + +#include +#include + +namespace sp { + +class X11Display; + +class X11WindowEventHandler +{ +public: + + static void registerHandler(::Display* disp, ::Window window, X11Display *ptr); + + static void unregisterHandler(::Display* disp, ::Window window); + + static void process(::Display* disp, const ::XEvent& event); + +protected : + + static ::XContext win_context; +}; + +} // namespace sp + +#endif /* PLATFORM_UNIX_WINDOW_EVENT_HANDLER_H */