diff --git a/include/Nazara/Platform/Window.hpp b/include/Nazara/Platform/Window.hpp index 47d416930..eabe8e942 100644 --- a/include/Nazara/Platform/Window.hpp +++ b/include/Nazara/Platform/Window.hpp @@ -32,13 +32,14 @@ namespace Nz class NAZARA_PLATFORM_API Window { friend WindowImpl; + friend class EventImpl; friend class Mouse; friend class Platform; public: Window(); inline Window(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default); - inline explicit Window(WindowHandle handle); + inline explicit Window(void* handle); Window(const Window&) = delete; Window(Window&& window); virtual ~Window(); @@ -46,7 +47,7 @@ namespace Nz inline void Close(); bool Create(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default); - bool Create(WindowHandle handle); + bool Create(void* handle); void Destroy(); @@ -61,10 +62,10 @@ namespace Nz inline const CursorRef& GetCursor() const; inline CursorController& GetCursorController(); inline EventHandler& GetEventHandler(); - WindowHandle GetHandle() const; Vector2i GetPosition() const; Vector2ui GetSize() const; WindowStyleFlags GetStyle() const; + WindowHandle GetSystemHandle() const; String GetTitle() const; bool HasFocus() const; @@ -106,6 +107,8 @@ namespace Nz Window& operator=(Window&& window); protected: + void* GetHandle(); + virtual bool OnWindowCreated(); virtual void OnWindowDestroy(); virtual void OnWindowResized(); @@ -116,6 +119,9 @@ namespace Nz void ConnectSlots(); void DisconnectSlots(); + inline WindowImpl* GetImpl(); + inline const WindowImpl* GetImpl() const; + void IgnoreNextMouseEvent(int mouseX, int mouseY) const; void HandleEvent(const WindowEvent& event); diff --git a/include/Nazara/Platform/Window.inl b/include/Nazara/Platform/Window.inl index 1be1a773f..89bb7e76d 100644 --- a/include/Nazara/Platform/Window.inl +++ b/include/Nazara/Platform/Window.inl @@ -20,7 +20,7 @@ namespace Nz Create(mode, title, style); } - inline Window::Window(WindowHandle handle) : + inline Window::Window(void* handle) : Window() { ErrorFlags flags(ErrorFlag_ThrowException, true); @@ -111,6 +111,16 @@ namespace Nz } } } + + inline WindowImpl* Window::GetImpl() + { + return m_impl; + } + + inline const WindowImpl* Window::GetImpl() const + { + return m_impl; + } } #include diff --git a/include/Nazara/Platform/WindowHandle.hpp b/include/Nazara/Platform/WindowHandle.hpp index de6e43304..c6053f4bf 100644 --- a/include/Nazara/Platform/WindowHandle.hpp +++ b/include/Nazara/Platform/WindowHandle.hpp @@ -8,11 +8,44 @@ #define NAZARA_WINDOWHANDLE_HPP #include +#include namespace Nz { - // Real type is SDL_Window - using WindowHandle = void*; + enum class WindowManager + { + None, + + X11, + Wayland, + Windows + }; + + struct WindowHandle + { + WindowManager type = WindowManager::None; + + union + { + struct + { + void* display; //< Display* + void* window; //< Window + } x11; + + struct + { + void* display; //< wl_display* + void* surface; //< wl_surface* + void* shellSurface; //< wl_shell_surface* + } wayland; + + struct + { + void* window; //< HWND + } windows; + }; + }; } #endif // NAZARA_WINDOWHANDLE_HPP diff --git a/include/Nazara/Prerequisites.hpp b/include/Nazara/Prerequisites.hpp index 0f4611766..24bc14dc1 100644 --- a/include/Nazara/Prerequisites.hpp +++ b/include/Nazara/Prerequisites.hpp @@ -122,14 +122,12 @@ #define NAZARA_EXPORT __attribute__((visibility ("default"))) #define NAZARA_IMPORT __attribute__((visibility ("default"))) /*#elif defined(__APPLE__) && defined(__MACH__) - #define NAZARA_CORE_API #define NAZARA_PLATFORM_MACOSX #define NAZARA_PLATFORM_POSIX*/ #else #error This operating system is not fully supported by the Nazara Engine #define NAZARA_PLATFORM_UNKNOWN - #define NAZARA_CORE_API #endif // Detect 64 bits diff --git a/include/Nazara/Renderer/ContextParameters.hpp b/include/Nazara/Renderer/ContextParameters.hpp index 830a1bdcf..8c939bb79 100644 --- a/include/Nazara/Renderer/ContextParameters.hpp +++ b/include/Nazara/Renderer/ContextParameters.hpp @@ -26,7 +26,7 @@ namespace Nz minorVersion(defaultMinorVersion), stencilBits(parameters.stencilBits), shareContext(defaultShareContext), - window(0), + window(nullptr), compatibilityProfile(defaultCompatibilityProfile), debugMode(defaultDebugMode), doubleBuffered(defaultDoubleBuffered), @@ -41,7 +41,7 @@ namespace Nz UInt8 minorVersion; UInt8 stencilBits; const Context* shareContext; - WindowHandle window; + void* window; bool compatibilityProfile; bool debugMode; bool doubleBuffered; diff --git a/include/Nazara/Renderer/RenderWindow.hpp b/include/Nazara/Renderer/RenderWindow.hpp index f5138f4e3..1555effa4 100644 --- a/include/Nazara/Renderer/RenderWindow.hpp +++ b/include/Nazara/Renderer/RenderWindow.hpp @@ -29,7 +29,7 @@ namespace Nz public: RenderWindow() = default; RenderWindow(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default, const ContextParameters& parameters = ContextParameters()); - explicit RenderWindow(WindowHandle handle, const ContextParameters& parameters = ContextParameters()); + explicit RenderWindow(void* handle, const ContextParameters& parameters = ContextParameters()); RenderWindow(const RenderWindow&) = delete; RenderWindow(RenderWindow&&) = delete; ///TODO virtual ~RenderWindow(); @@ -38,7 +38,7 @@ namespace Nz bool CopyToImage(AbstractImage* image, const Rectui& rect, const Vector3ui& dstPos = Vector3ui(0U)) const; bool Create(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default, const ContextParameters& parameters = ContextParameters()); - bool Create(WindowHandle handle, const ContextParameters& parameters = ContextParameters()); + bool Create(void* handle, const ContextParameters& parameters = ContextParameters()); void Display(); diff --git a/src/Nazara/Platform/SDL2/InputImpl.cpp b/src/Nazara/Platform/SDL2/InputImpl.cpp index 6363fbd54..1b609e71c 100644 --- a/src/Nazara/Platform/SDL2/InputImpl.cpp +++ b/src/Nazara/Platform/SDL2/InputImpl.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -45,21 +46,10 @@ namespace Nz Vector2i EventImpl::GetMousePosition(const Window& relativeTo) { - auto handle = relativeTo.GetHandle(); - if (handle) - { - auto windowPos = relativeTo.GetPosition(); - auto mousePos = GetMousePosition(); + auto windowPos = relativeTo.GetPosition(); + auto mousePos = GetMousePosition(); - return mousePos - windowPos; - } - else - { - NazaraError("Invalid window handle"); - - // Attention que (-1, -1) est une position tout à fait valide et ne doit pas servir de test - return Vector2i(-1, -1); - } + return mousePos - windowPos; } bool EventImpl::IsKeyPressed(Keyboard::Scancode key) @@ -98,7 +88,7 @@ namespace Nz void EventImpl::SetMousePosition(int x, int y, const Window& relativeTo) { - auto handle = static_cast(relativeTo.GetHandle()); + SDL_Window* handle = static_cast(relativeTo.GetImpl())->GetHandle(); if (handle) SDL_WarpMouseInWindow(handle, x, y); else diff --git a/src/Nazara/Platform/SDL2/WindowImpl.cpp b/src/Nazara/Platform/SDL2/WindowImpl.cpp index 2db2b55be..a87155be2 100644 --- a/src/Nazara/Platform/SDL2/WindowImpl.cpp +++ b/src/Nazara/Platform/SDL2/WindowImpl.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include namespace Nz { @@ -124,10 +126,9 @@ namespace Nz return true; } - bool WindowImpl::Create(WindowHandle handle) + bool WindowImpl::Create(void* handle) { - m_handle = static_cast(handle); - + m_handle = SDL_CreateWindowFrom(handle); if (!m_handle || !SDL_GetWindowID(m_handle)) { NazaraError("Invalid handle"); @@ -171,7 +172,7 @@ namespace Nz m_smoothScrolling = enable; } - WindowHandle WindowImpl::GetHandle() const + SDL_Window* WindowImpl::GetHandle() const { return m_handle; } @@ -190,6 +191,56 @@ namespace Nz { return m_style; } + + WindowHandle WindowImpl::GetSystemHandle() const + { + SDL_SysWMinfo wmInfo; + if (SDL_GetWindowWMInfo(m_handle, &wmInfo) != SDL_TRUE) + { + ErrorFlags flags(ErrorFlag_ThrowException, true); + NazaraError(std::string("failed to retrieve window manager info: ") + SDL_GetError()); + } + + WindowHandle handle; + + switch (wmInfo.subsystem) + { +#if defined(SDL_VIDEO_DRIVER_X11) + case SDL_SYSWM_X11: + { + handle.type = WindowManager::X11; + handle.x11.display = wmInfo.info.x11.display; + handle.x11.window = wmInfo.info.x11.window; + break; + } +#endif +#if defined(SDL_VIDEO_DRIVER_WAYLAND) + case SDL_SYSWM_WAYLAND: + { + handle.type = WindowManager::Wayland; + handle.wayland.display = wmInfo.info.wl.display; + handle.wayland.surface = wmInfo.info.wl.surface; + handle.wayland.shellSurface = wmInfo.info.wl.shell_surface; + break; + } +#endif +#if defined(SDL_VIDEO_DRIVER_WINDOWS) + case SDL_SYSWM_WINDOWS: + { + handle.type = WindowManager::Windows; + handle.windows.window = wmInfo.info.win.window; + break; + } +#endif + default: + { + ErrorFlags flags(ErrorFlag_ThrowException, true); + NazaraError("unhandled window subsystem"); + } + } + + return handle; + } String WindowImpl::GetTitle() const { diff --git a/src/Nazara/Platform/SDL2/WindowImpl.hpp b/src/Nazara/Platform/SDL2/WindowImpl.hpp index 8d960eaff..9acad3951 100644 --- a/src/Nazara/Platform/SDL2/WindowImpl.hpp +++ b/src/Nazara/Platform/SDL2/WindowImpl.hpp @@ -38,17 +38,18 @@ namespace Nz ~WindowImpl() = default; bool Create(const VideoMode& mode, const String& title, WindowStyleFlags style); - bool Create(WindowHandle handle); + bool Create(void* handle); void Destroy(); void EnableKeyRepeat(bool enable); void EnableSmoothScrolling(bool enable); - WindowHandle GetHandle() const; + SDL_Window* GetHandle() const; Vector2i GetPosition() const; Vector2ui GetSize() const; WindowStyleFlags GetStyle() const; + WindowHandle GetSystemHandle() const; String GetTitle() const; bool HasFocus() const; diff --git a/src/Nazara/Platform/Window.cpp b/src/Nazara/Platform/Window.cpp index cfdba8cd1..3dd044593 100644 --- a/src/Nazara/Platform/Window.cpp +++ b/src/Nazara/Platform/Window.cpp @@ -124,7 +124,7 @@ namespace Nz return true; } - bool Window::Create(WindowHandle handle) + bool Window::Create(void* handle) { Destroy(); @@ -197,19 +197,6 @@ namespace Nz m_impl->EnableSmoothScrolling(enable); } - WindowHandle Window::GetHandle() const - { - #if NAZARA_PLATFORM_SAFE - if (!m_impl) - { - NazaraError("Window not created"); - return static_cast(0); - } - #endif - - return m_impl->GetHandle(); - } - Vector2i Window::GetPosition() const { #if NAZARA_PLATFORM_SAFE @@ -249,6 +236,19 @@ namespace Nz return m_impl->GetStyle(); } + WindowHandle Window::GetSystemHandle() const + { +#if NAZARA_PLATFORM_SAFE + if (!m_impl) + { + NazaraError("Window not created"); + return {}; + } +#endif + + return m_impl->GetSystemHandle(); + } + String Window::GetTitle() const { #if NAZARA_PLATFORM_SAFE @@ -613,6 +613,11 @@ namespace Nz return *this; } + void* Window::GetHandle() + { + return (m_impl) ? m_impl->GetHandle() : nullptr; + } + bool Window::OnWindowCreated() { return true; diff --git a/src/Nazara/Renderer/RenderWindow.cpp b/src/Nazara/Renderer/RenderWindow.cpp index b1ade1d95..36e364508 100644 --- a/src/Nazara/Renderer/RenderWindow.cpp +++ b/src/Nazara/Renderer/RenderWindow.cpp @@ -20,7 +20,7 @@ namespace Nz Create(mode, title, style, parameters); } - RenderWindow::RenderWindow(WindowHandle handle, const ContextParameters& parameters) : + RenderWindow::RenderWindow(void* handle, const ContextParameters& parameters) : RenderTarget(), Window() { ErrorFlags flags(ErrorFlag_ThrowException, true); @@ -125,7 +125,7 @@ namespace Nz return Window::Create(mode, title, style); } - bool RenderWindow::Create(WindowHandle handle, const ContextParameters& parameters) + bool RenderWindow::Create(void* handle, const ContextParameters& parameters) { m_parameters = parameters; return Window::Create(handle);