TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_window_backend_sdl.h
Go to the documentation of this file.
1#pragma once
2
8
9#include <atomic>
10#include <vector>
11#include <unordered_set>
12
13struct SDL_Window;
14struct SDL_Cursor;
15union SDL_Event;
16
17namespace tachyon
18{
21
22 class GraphicsContext;
23
24 // Outside the class: GCC can't use a nested struct's member initializers in a default
25 // argument of the class being defined.
28 {
31 const AllocationHooks* hooks = nullptr;
35 bool pumpEvents = true;
40 bool physicalButtons = true;
41 };
42
46 {
47 public:
50
52 explicit SDLWindowBackend(const Options& options = {});
54
57 void SetGraphicsContext(GraphicsContext* graphicsContext) override;
58
59 // IWindowBackend
60 OSWindowHandle CreateWindow(std::string_view title, int2 size, tachyon::WindowFlags windowFlags, OSWindowHandle parentOSHandle) override;
61 OSWindowHandle CreateWindow(std::string_view title, IntRect rect, tachyon::WindowFlags windowFlags, OSWindowHandle parentOSHandle) override;
62
63
65 void AttachContextToWindow(OSWindowHandle window, IWindowContext* context) override;
66
69 bool IsWindow(OSWindowHandle) override;
70
72
76 uint32_t GetFocusChangeCount() const override { return m_focusChanges; }
77
78 void PollEvents(std::function<void(OSWindowHandle, IWindowContext*)> cleanup) override;
80 void WaitForEvents(float timeoutSecs) override;
82 void SleepFor(float secs) override;
84 void Wake() override;
85 uint64_t GetEventCount() const override { return m_events.load(); }
86 bool TakeWindowsExposed() override { return m_exposed.exchange(false); }
88 std::optional<color4b> ReadScreenPixel(int2 screenPos) override;
89 bool TakeCloseRequest(OSWindowHandle window) override;
92 uint32_t ShowFade(OSWindowHandle window, IWindowContext* context, IntRect screenRect, float opacity) override;
93 void MoveFade(uint32_t fade, IntRect screenRect, float opacity) override;
94 void EndFade(uint32_t fade) override;
95 bool PrefersReducedMotion() override;
96
97 void SetCursor(MouseCursor cursor) override;
98 void SetTextInputArea(OSWindowHandle window, IntRect rect) override;
99 float GetWindowDisplayScale(OSWindowHandle window) override;
101
103
107
108 bool SetWindowRect(OSWindowHandle, const IntRect& clientRect) override;
109 void SetWindowVisible(OSWindowHandle, bool visible) override;
110 bool SetWindowCloaked(OSWindowHandle, bool cloaked) override;
113
114 std::vector<MonitorInfo> GetMonitors() const override;
115 int GetMonitorAt(int2 pos) const override;
116 int GetMonitorFromWindow(OSWindowHandle handle) const override;
117
118 // Platform-specific operations
119 uint32_t GetTicks() const override;
120 std::string GetPreferredLanguage() const override;
121 int32_t GetCursorSize() const override;
122
124
127 static SDL_Window* ToSDL(OSWindowHandle h) { return reinterpret_cast<SDL_Window*>(h); }
128 static OSWindowHandle FromSDL(SDL_Window* w) { return reinterpret_cast<OSWindowHandle>(w); }
130
131 private:
132 void UpdateMouseState();
133
134 Options m_options;
135
136 std::unordered_set<SDL_Window*> m_windows;
138 std::vector<SDL_Window*> m_modalWindows;
140 int2 m_mousePos{};
141
143 float OsPerPixel(SDL_Window* window) const;
144 public:
146 int2 WindowPointToPixels(SDL_Window* window, float x, float y) const;
147 private:
148 MouseButtons m_mouseButtons = MouseButtons::None;
149
151 std::unordered_set<SDL_Window*> m_pendingDestroy;
152 std::unordered_set<SDL_Window*> m_closeRequests;
153 bool m_sdlInitialized = false;
154 bool m_ownsSDL = false;
155 bool m_routesSDLMemory = false;
156 bool m_cursorVisible = true;
158 static constexpr int kSystemCursorSlots = 32;
159 SDL_Cursor* m_systemCursors[kSystemCursorSlots] = {};
161 bool (*m_eventWatch)(void*, SDL_Event*) = nullptr;
163 static bool BackendEventWatch(void* userdata, SDL_Event* event);
164 uint32_t m_focusChanges = 0;
165 uint32_t m_wakeEvent = 0;
166 std::atomic<uint64_t> m_events = 0;
167 std::atomic<bool> m_exposed = false;
168 bool m_checkFocusLeftApp = false;
169 bool m_suppressLiveResize = false;
170 SDL_Window* m_lastFocusableWindow = nullptr;
171 LayeredFades m_fades;
172
173 SDL_Window* GetTopModalWindowSDL() const;
174 public:
176 SDL_Window* ModalBlockerFor(const SDL_Event* event) const;
177 private:
178 };
179
181} // namespace tachyon
A graphics backend: the device, the window contexts that render the GUI's windows,...
Definition gui_graphics_context.h:53
Platform-level input interface: receives raw OS events (keyboard, mouse) and forwards them into the G...
Definition gui_platform_input_handler.h:25
IWindowBackend()
Definition gui_window_backend.h:114
A window's rendering state: made by GraphicsContext::CreateWindowContext for each OS window,...
Definition gui_window_backend.h:72
Layered windows that stand in for a window while it fades in or out: each shows a fixed picture (the ...
Definition gui_layered_fade.h:20
int GetMonitorAt(int2 pos) const override
Index into GetMonitors of the monitor holding the screen point pos; 0 (the primary) when no monitor h...
IWindowContext * GetWindowContext(OSWindowHandle) override
The context attached with AttachContextToWindow, or nullptr.
SDLWindowBackendOptions Options
See SDLWindowBackendOptions.
Definition gui_window_backend_sdl.h:49
bool SetWindowCloaked(OSWindowHandle, bool cloaked) override
Cloaks a shown window: it takes frames but stays invisible, so it can be drawn before it appears.
bool IsWindow(OSWindowHandle) override
True while the handle is a live window of this backend; false once it was destroyed (by code or the u...
void EndFade(uint32_t fade) override
Removes a stand-in.
void SetWindowVisible(OSWindowHandle, bool visible) override
Shows or hides the window; hiding doesn't destroy it.
void SwapBuffers(OSWindowHandle) override
Presents for a graphics API that swaps through the window (OpenGL).
SDL_Window * ModalBlockerFor(const SDL_Event *event) const
The modal window that blocks the event's window (one of ours), or null.
void SetWindowMode(OSWindowHandle, WindowMode) override
Sets the window's state: minimized, maximized, restored or fullscreen.
static SDL_Window * ToSDL(OSWindowHandle h)
Converts between handles and SDL windows: a handle is the SDL_Window pointer.
Definition gui_window_backend_sdl.h:127
void MoveFade(uint32_t fade, IntRect screenRect, float opacity) override
Moves a stand-in and sets its opacity.
IntRect GetWindowPixelRect(OSWindowHandle window) override
The window's render target: OS position (for OS compositing) and size in physical pixels.
uint32_t GetFocusChangeCount() const override
Goes up whenever keyboard focus moves between windows, or to or from another application; open popups...
Definition gui_window_backend_sdl.h:76
uint32_t ShowFade(OSWindowHandle window, IWindowContext *context, IntRect screenRect, float opacity) override
A layered stand-in for window showing what context drew last, at screenRect (screen pixels) and opaci...
int2 GetWindowPos(OSWindowHandle) override
The client area's top-left in screen pixels.
bool TakeCloseRequest(OSWindowHandle window) override
Whether the OS asked to close a WindowFlags::ConfirmClose window since last asked.
int32_t GetCursorSize() const override
The OS mouse cursor's size in pixels (its accessibility size at the display's scale),...
OSWindowHandle CreateWindow(std::string_view title, int2 size, tachyon::WindowFlags windowFlags, OSWindowHandle parentOSHandle) override
Creates a window whose client area is size pixels, placed by the OS.
void SleepFor(float secs) override
SDL_DelayNS: a high-resolution timer where the OS has one.
MouseButtons GetMouseButtons() override
The mouse buttons held down, wherever the mouse is (SDL: as of the last PollEvents).
void WaitForEvents(float timeoutSecs) override
SDL_WaitEventTimeout; with pumpEvents off it doesn't wait, the host runs SDL's loop.
std::vector< MonitorInfo > GetMonitors() const override
Every monitor, the primary first.
int2 WindowPointToPixels(SDL_Window *window, float x, float y) const
A position inside window (OS window coordinates) in screen pixels.
void SetCursor(MouseCursor cursor) override
Shows the system cursor for that shape; MouseCursor::None hides the cursor.
WindowMode GetWindowMode(OSWindowHandle) const override
The window's state as the OS has it now; WindowMode::Normal by default.
static OSWindowHandle FromSDL(SDL_Window *w)
Converts between handles and SDL windows: a handle is the SDL_Window pointer.
Definition gui_window_backend_sdl.h:128
bool DestroyWindow(OSWindowHandle) override
Queues the window for destruction: the next PollEvents passes its context to cleanup,...
OSWindowHandle CreateWindow(std::string_view title, IntRect rect, tachyon::WindowFlags windowFlags, OSWindowHandle parentOSHandle) override
Creates a window whose client area is rect (screen pixels); otherwise as the size overload.
float GetWindowDisplayScale(OSWindowHandle window) override
The UI scale the OS asks for in this window: pixel density times the monitor's content scale (1....
SDLWindowBackend(const Options &options={})
Starts SDL video if needed; failing that, logs an error and the backend makes no windows.
IntRect GetWindowRect(OSWindowHandle) override
Client area in pixels; {0,0,0,0} while minimized.
void AttachContextToWindow(OSWindowHandle window, IWindowContext *context) override
Stores the window's context (not owned), for GetWindowContext, for the ID passed with the window's in...
uint64_t GetEventCount() const override
How many OS events (input, window changes, Wake) the backend has seen; it only goes up,...
Definition gui_window_backend_sdl.h:85
bool PrefersReducedMotion() override
Whether the OS asks for less motion (animations turned off); false where the platform has no such set...
void DetachWindowContext(OSWindowHandle) override
Clears the context pointer stored in the OS window, so PollEvents' cleanup callback gets nullptr inst...
void BringToFront(OSWindowHandle) override
Raises the window above the others and asks the OS for the input focus.
int2 GetScreenMousePos() override
The mouse position in screen pixels (SDL: as of the last PollEvents).
void AttachInputHandlerToWindow(OSWindowHandle window, IPlatformInputHandler *inputHandler) override
Where the window's input goes (see IPlatformInputHandler); not owned, nullptr for none.
OSWindowHandle GetActiveWindow() override
The window with the keyboard focus, or nullptr when none of the backend's windows has it.
bool SetWindowRect(OSWindowHandle, const IntRect &clientRect) override
Moves and resizes the window so its client area is clientRect (screen pixels); the backend accounts f...
std::optional< color4b > ReadScreenPixel(int2 screenPos) override
The color of a screen pixel, anywhere on the desktop (an eyedropper); nothing where the platform can'...
void Wake() override
Pushes an event of its own, which ends SDL's wait.
std::string GetPreferredLanguage() const override
The user's preferred language as a BCP 47 tag ("ja", "en-US"), or empty when the platform doesn't say...
int GetMonitorFromWindow(OSWindowHandle handle) const override
Index into GetMonitors of the window's monitor; -1 for an unknown window.
void * GetNativeHandle(OSWindowHandle) override
The OS's own window handle, passed to GraphicsContext::CreateWindowContext (HWND on Windows,...
void PollEvents(std::function< void(OSWindowHandle, IWindowContext *)> cleanup) override
Handles the OS's pending events, delivering input to the attached handlers.
float GetDoubleClickMilliseconds() override
The OS's double-click time in milliseconds, or 0 where the platform has none.
bool TakeWindowsExposed() override
True when a window was uncovered, shown or restored since the last call, so it must be drawn again ev...
Definition gui_window_backend_sdl.h:86
uint32_t GetTicks() const override
A millisecond clock (SDL: since SDL started).
void SetTextInputArea(OSWindowHandle window, IntRect rect) override
Where text is being typed in the window (window coordinates), so the IME places its candidate list ne...
void SetGraphicsContext(GraphicsContext *graphicsContext) override
Windows are created for graphicsContext->GetAPI(); for a VulkanGraphicsContext this also installs its...
OSWindowHandle GetWindowUnderMouse() override
The backend's window under the mouse, or nullptr.
int2 GetWindowSize(OSWindowHandle) override
The client area's size in pixels; {0,0} while minimized.
MouseButtons
Mouse buttons, combined with |.
Definition gui_input_enums.h:38
@ None
Definition gui_input_enums.h:39
MouseCursor
Mouse cursor shown over an element (CSS: cursor).
Definition gui_style.h:481
WindowFlags
How an OS window is made and behaves (IWindowBackend::CreateWindow); flags combine with |.
Definition gui_window_flags.h:14
WindowMode
A window's state (IWindowBackend::SetWindowMode). Any window can go fullscreen.
Definition gui_window_backend.h:60
void * OSWindowHandle
An OS window, opaque; what it points to is up to the window backend.
Definition gui_window_backend.h:37
Definition gui_logging.h:24
Memory functions a host gives the library, for the allocations that can be routed: Vulkan objects,...
Definition gui_allocation_hooks.h:17
Definition gui_base_types.h:406
Options for SDLWindowBackend (SDLWindowBackend::Options).
Definition gui_window_backend_sdl.h:28
bool pumpEvents
PollEvents empties SDL's event queue.
Definition gui_window_backend_sdl.h:35
const AllocationHooks * hooks
Hooks for SDL's memory functions, which are process-wide.
Definition gui_window_backend_sdl.h:31
bool physicalButtons
GetMouseButtons reads the physical mouse buttons, wherever the pointer is: while a popup is open the ...
Definition gui_window_backend_sdl.h:40
Integer 2D vector.
Definition gui_base_types.h:18