1// dear imgui: Platform Backend for SDL2
2// This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
3// (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
4// (Prefer SDL 2.0.5+ for full feature support.)
5
6// Implemented features:
7// [X] Platform: Clipboard support.
8// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
9// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
10// [X] Platform: Gamepad support.
11// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
12// [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
13// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
14// Missing features or Issues:
15// [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows).
16// [ ] Platform: Multi-viewport: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
17
18// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
19// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
20// Learn about Dear ImGui:
21// - FAQ https://dearimgui.com/faq
22// - Getting Started https://dearimgui.com/getting-started
23// - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
24// - Introduction, links and more at the top of imgui.cpp
25
26// CHANGELOG
27// (minor and older changes stripped away, please see git history for details)
28// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
29// 2025-07-08: Made ImGui_ImplSDL2_GetContentScaleForWindow(), ImGui_ImplSDL2_GetContentScaleForDisplay() helpers return 1.0f on Emscripten and Android platforms, matching macOS logic. (#8742, #8733)
30// 2025-06-11: Added ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) and ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) helper to facilitate making DPI-aware apps.
31// 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors.
32// 2025-04-09: [Docking] Revert update monitors and work areas information every frame. Only do it on Windows. (#8415, #8558)
33// 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
34// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
35// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
36// 2025-02-26: Only start SDL_CaptureMouse() when mouse is being dragged, to mitigate issues with e.g.Linux debuggers not claiming capture back. (#6410, #3650)
37// 2025-02-25: [Docking] Revert to use SDL_GetDisplayBounds() for WorkPos/WorkRect if SDL_GetDisplayUsableBounds() failed.
38// 2025-02-24: Avoid calling SDL_GetGlobalMouseState() when mouse is in relative mode.
39// 2025-02-21: [Docking] Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (#8415)
40// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
41// 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler.
42// 2025-01-20: Made ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode_Manual) accept an empty array.
43// 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f.
44// 2024-09-09: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190)
45// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
46// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
47// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
48// - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn
49// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
50// 2024-08-19: Storing SDL's Uint32 WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
51// 2024-08-19: ImGui_ImplSDL2_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
52// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
53// 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library.
54// 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode().
55// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
56// 2023-04-06: Inputs: Avoid calling SDL_StartTextInput()/SDL_StopTextInput() as they don't only pertain to IME. It's unclear exactly what their relation is to IME. (#6306)
57// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
58// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
59// 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()).
60// 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3.
61// 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version).
62// 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096)
63// 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019)
64// 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463)
65// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
66// 2022-09-26: Inputs: Disable SDL 2.0.22 new "auto capture" (SDL_HINT_MOUSE_AUTO_CAPTURE) which prevents drag and drop across windows for multi-viewport support + don't capture when drag and dropping. (#5710)
67// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
68// 2022-03-22: Inputs: Fix mouse position issues when dragging outside of boundaries. SDL_CaptureMouse() erroneously still gives out LEAVE events when hovering OS decorations.
69// 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2).
70// 2022-02-04: Added SDL_Renderer* parameter to ImGui_ImplSDL2_InitForSDLRenderer(), so we can use SDL_GetRendererOutputSize() instead of SDL_GL_GetDrawableSize() when bound to a SDL_Renderer.
71// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
72// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
73// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
74// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates.
75// 2022-01-12: Update mouse inputs using SDL_MOUSEMOTION/SDL_WINDOWEVENT_LEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API.
76// 2022-01-12: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted.
77// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
78// 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST.
79// 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+)
80// 2021-06:29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary.
81// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
82// 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950)
83// 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends.
84// 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2).
85// 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state).
86// 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
87// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
88// 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
89// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
90// 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls.
91// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
92// 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'.
93// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
94// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
95// 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
96// 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
97// 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
98// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
99// 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
100// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
101// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
102// 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
103// 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
104// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
105// 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
106// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
107// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
108// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
109
110#include "imgui.h"
111#ifndef IMGUI_DISABLE
112#include "imgui_impl_sdl2.h"
113
114// Clang warnings with -Weverything
115#if defined(__clang__)
116#pragma clang diagnostic push
117#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
118#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
119#endif
120
121// SDL
122// (the multi-viewports feature requires SDL features supported from SDL 2.0.4+. SDL 2.0.5+ is highly recommended)
123#include <SDL.h>
124#include <SDL_syswm.h>
125#include <stdio.h> // for snprintf()
126#ifdef __APPLE__
127#include <TargetConditionals.h>
128#endif
129#ifdef __EMSCRIPTEN__
130#include <emscripten/em_js.h>
131#endif
132#undef Status // X11 headers are leaking this.
133
134#if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
135#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
136#else
137#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
138#endif
139#define SDL_HAS_WINDOW_ALPHA SDL_VERSION_ATLEAST(2,0,5)
140#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5)
141#define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5)
142#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4)
143#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
144#define SDL_HAS_DISPLAY_EVENT SDL_VERSION_ATLEAST(2,0,9)
145#define SDL_HAS_OPEN_URL SDL_VERSION_ATLEAST(2,0,14)
146#define SDL_HAS_SHOW_WINDOW_ACTIVATION_HINT SDL_VERSION_ATLEAST(2,0,18)
147#if SDL_HAS_VULKAN
148#include <SDL_vulkan.h>
149#else
150static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
151#endif
152
153// SDL Data
154struct ImGui_ImplSDL2_Data
155{
156 SDL_Window* Window;
157 Uint32 WindowID; // Stored in ImGuiViewport::PlatformHandle. Use SDL_GetWindowFromID() to get SDL_Window* from Uint32 WindowID.
158 SDL_Renderer* Renderer;
159 Uint64 Time;
160 char* ClipboardTextData;
161 char BackendPlatformName[48];
162 bool UseVulkan;
163 bool WantUpdateMonitors;
164
165 // Mouse handling
166 Uint32 MouseWindowID;
167 int MouseButtonsDown;
168 SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
169 SDL_Cursor* MouseLastCursor;
170 int MouseLastLeaveFrame;
171 bool MouseCanUseGlobalState;
172 bool MouseCanUseCapture;
173 bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
174
175 // Gamepad handling
176 ImVector<SDL_GameController*> Gamepads;
177 ImGui_ImplSDL2_GamepadMode GamepadMode;
178 bool WantUpdateGamepadsList;
179
180 ImGui_ImplSDL2_Data() { memset(s: (void*)this, c: 0, n: sizeof(*this)); }
181};
182
183// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
184// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
185// FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
186// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
187static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData()
188{
189 return ImGui::GetCurrentContext() ? (ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
190}
191
192// Forward Declarations
193static void ImGui_ImplSDL2_UpdateMonitors();
194static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context);
195static void ImGui_ImplSDL2_ShutdownMultiViewportSupport();
196
197// Functions
198static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*)
199{
200 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
201 if (bd->ClipboardTextData)
202 SDL_free(mem: bd->ClipboardTextData);
203 bd->ClipboardTextData = SDL_GetClipboardText();
204 return bd->ClipboardTextData;
205}
206
207static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*, const char* text)
208{
209 SDL_SetClipboardText(text);
210}
211
212// Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow().
213static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
214{
215 if (data->WantVisible)
216 {
217 SDL_Rect r;
218 r.x = (int)(data->InputPos.x - viewport->Pos.x);
219 r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
220 r.w = 1;
221 r.h = (int)data->InputLineHeight;
222 SDL_SetTextInputRect(rect: &r);
223 }
224}
225
226// Not static to allow third-party code to use that if they want to (but undocumented)
227ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
228ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
229{
230 switch (keycode)
231 {
232 case SDLK_TAB: return ImGuiKey_Tab;
233 case SDLK_LEFT: return ImGuiKey_LeftArrow;
234 case SDLK_RIGHT: return ImGuiKey_RightArrow;
235 case SDLK_UP: return ImGuiKey_UpArrow;
236 case SDLK_DOWN: return ImGuiKey_DownArrow;
237 case SDLK_PAGEUP: return ImGuiKey_PageUp;
238 case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
239 case SDLK_HOME: return ImGuiKey_Home;
240 case SDLK_END: return ImGuiKey_End;
241 case SDLK_INSERT: return ImGuiKey_Insert;
242 case SDLK_DELETE: return ImGuiKey_Delete;
243 case SDLK_BACKSPACE: return ImGuiKey_Backspace;
244 case SDLK_SPACE: return ImGuiKey_Space;
245 case SDLK_RETURN: return ImGuiKey_Enter;
246 case SDLK_ESCAPE: return ImGuiKey_Escape;
247 //case SDLK_QUOTE: return ImGuiKey_Apostrophe;
248 case SDLK_COMMA: return ImGuiKey_Comma;
249 //case SDLK_MINUS: return ImGuiKey_Minus;
250 case SDLK_PERIOD: return ImGuiKey_Period;
251 //case SDLK_SLASH: return ImGuiKey_Slash;
252 case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
253 //case SDLK_EQUALS: return ImGuiKey_Equal;
254 //case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
255 //case SDLK_BACKSLASH: return ImGuiKey_Backslash;
256 //case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
257 //case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent;
258 case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
259 case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
260 case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
261 case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
262 case SDLK_PAUSE: return ImGuiKey_Pause;
263 case SDLK_KP_0: return ImGuiKey_Keypad0;
264 case SDLK_KP_1: return ImGuiKey_Keypad1;
265 case SDLK_KP_2: return ImGuiKey_Keypad2;
266 case SDLK_KP_3: return ImGuiKey_Keypad3;
267 case SDLK_KP_4: return ImGuiKey_Keypad4;
268 case SDLK_KP_5: return ImGuiKey_Keypad5;
269 case SDLK_KP_6: return ImGuiKey_Keypad6;
270 case SDLK_KP_7: return ImGuiKey_Keypad7;
271 case SDLK_KP_8: return ImGuiKey_Keypad8;
272 case SDLK_KP_9: return ImGuiKey_Keypad9;
273 case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal;
274 case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide;
275 case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
276 case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract;
277 case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd;
278 case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter;
279 case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual;
280 case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
281 case SDLK_LSHIFT: return ImGuiKey_LeftShift;
282 case SDLK_LALT: return ImGuiKey_LeftAlt;
283 case SDLK_LGUI: return ImGuiKey_LeftSuper;
284 case SDLK_RCTRL: return ImGuiKey_RightCtrl;
285 case SDLK_RSHIFT: return ImGuiKey_RightShift;
286 case SDLK_RALT: return ImGuiKey_RightAlt;
287 case SDLK_RGUI: return ImGuiKey_RightSuper;
288 case SDLK_APPLICATION: return ImGuiKey_Menu;
289 case SDLK_0: return ImGuiKey_0;
290 case SDLK_1: return ImGuiKey_1;
291 case SDLK_2: return ImGuiKey_2;
292 case SDLK_3: return ImGuiKey_3;
293 case SDLK_4: return ImGuiKey_4;
294 case SDLK_5: return ImGuiKey_5;
295 case SDLK_6: return ImGuiKey_6;
296 case SDLK_7: return ImGuiKey_7;
297 case SDLK_8: return ImGuiKey_8;
298 case SDLK_9: return ImGuiKey_9;
299 case SDLK_a: return ImGuiKey_A;
300 case SDLK_b: return ImGuiKey_B;
301 case SDLK_c: return ImGuiKey_C;
302 case SDLK_d: return ImGuiKey_D;
303 case SDLK_e: return ImGuiKey_E;
304 case SDLK_f: return ImGuiKey_F;
305 case SDLK_g: return ImGuiKey_G;
306 case SDLK_h: return ImGuiKey_H;
307 case SDLK_i: return ImGuiKey_I;
308 case SDLK_j: return ImGuiKey_J;
309 case SDLK_k: return ImGuiKey_K;
310 case SDLK_l: return ImGuiKey_L;
311 case SDLK_m: return ImGuiKey_M;
312 case SDLK_n: return ImGuiKey_N;
313 case SDLK_o: return ImGuiKey_O;
314 case SDLK_p: return ImGuiKey_P;
315 case SDLK_q: return ImGuiKey_Q;
316 case SDLK_r: return ImGuiKey_R;
317 case SDLK_s: return ImGuiKey_S;
318 case SDLK_t: return ImGuiKey_T;
319 case SDLK_u: return ImGuiKey_U;
320 case SDLK_v: return ImGuiKey_V;
321 case SDLK_w: return ImGuiKey_W;
322 case SDLK_x: return ImGuiKey_X;
323 case SDLK_y: return ImGuiKey_Y;
324 case SDLK_z: return ImGuiKey_Z;
325 case SDLK_F1: return ImGuiKey_F1;
326 case SDLK_F2: return ImGuiKey_F2;
327 case SDLK_F3: return ImGuiKey_F3;
328 case SDLK_F4: return ImGuiKey_F4;
329 case SDLK_F5: return ImGuiKey_F5;
330 case SDLK_F6: return ImGuiKey_F6;
331 case SDLK_F7: return ImGuiKey_F7;
332 case SDLK_F8: return ImGuiKey_F8;
333 case SDLK_F9: return ImGuiKey_F9;
334 case SDLK_F10: return ImGuiKey_F10;
335 case SDLK_F11: return ImGuiKey_F11;
336 case SDLK_F12: return ImGuiKey_F12;
337 case SDLK_F13: return ImGuiKey_F13;
338 case SDLK_F14: return ImGuiKey_F14;
339 case SDLK_F15: return ImGuiKey_F15;
340 case SDLK_F16: return ImGuiKey_F16;
341 case SDLK_F17: return ImGuiKey_F17;
342 case SDLK_F18: return ImGuiKey_F18;
343 case SDLK_F19: return ImGuiKey_F19;
344 case SDLK_F20: return ImGuiKey_F20;
345 case SDLK_F21: return ImGuiKey_F21;
346 case SDLK_F22: return ImGuiKey_F22;
347 case SDLK_F23: return ImGuiKey_F23;
348 case SDLK_F24: return ImGuiKey_F24;
349 case SDLK_AC_BACK: return ImGuiKey_AppBack;
350 case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
351 default: break;
352 }
353
354 // Fallback to scancode
355 switch (scancode)
356 {
357 case SDL_SCANCODE_GRAVE: return ImGuiKey_GraveAccent;
358 case SDL_SCANCODE_MINUS: return ImGuiKey_Minus;
359 case SDL_SCANCODE_EQUALS: return ImGuiKey_Equal;
360 case SDL_SCANCODE_LEFTBRACKET: return ImGuiKey_LeftBracket;
361 case SDL_SCANCODE_RIGHTBRACKET: return ImGuiKey_RightBracket;
362 case SDL_SCANCODE_NONUSBACKSLASH: return ImGuiKey_Oem102;
363 case SDL_SCANCODE_BACKSLASH: return ImGuiKey_Backslash;
364 case SDL_SCANCODE_SEMICOLON: return ImGuiKey_Semicolon;
365 case SDL_SCANCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
366 case SDL_SCANCODE_COMMA: return ImGuiKey_Comma;
367 case SDL_SCANCODE_PERIOD: return ImGuiKey_Period;
368 case SDL_SCANCODE_SLASH: return ImGuiKey_Slash;
369 default: break;
370 }
371 return ImGuiKey_None;
372}
373
374static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
375{
376 ImGuiIO& io = ImGui::GetIO();
377 io.AddKeyEvent(key: ImGuiMod_Ctrl, down: (sdl_key_mods & KMOD_CTRL) != 0);
378 io.AddKeyEvent(key: ImGuiMod_Shift, down: (sdl_key_mods & KMOD_SHIFT) != 0);
379 io.AddKeyEvent(key: ImGuiMod_Alt, down: (sdl_key_mods & KMOD_ALT) != 0);
380 io.AddKeyEvent(key: ImGuiMod_Super, down: (sdl_key_mods & KMOD_GUI) != 0);
381}
382
383static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id)
384{
385 return ImGui::FindViewportByPlatformHandle(platform_handle: (void*)(intptr_t)window_id);
386}
387
388// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
389// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
390// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
391// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
392bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
393{
394 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
395 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
396 ImGuiIO& io = ImGui::GetIO();
397
398 switch (event->type)
399 {
400 case SDL_MOUSEMOTION:
401 {
402 if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->motion.windowID) == nullptr)
403 return false;
404 ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
405 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
406 {
407 int window_x, window_y;
408 SDL_GetWindowPosition(window: SDL_GetWindowFromID(id: event->motion.windowID), x: &window_x, y: &window_y);
409 mouse_pos.x += window_x;
410 mouse_pos.y += window_y;
411 }
412 io.AddMouseSourceEvent(source: event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
413 io.AddMousePosEvent(x: mouse_pos.x, y: mouse_pos.y);
414 return true;
415 }
416 case SDL_MOUSEWHEEL:
417 {
418 if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->wheel.windowID) == nullptr)
419 return false;
420 //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
421#if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten!
422 float wheel_x = -event->wheel.preciseX;
423 float wheel_y = event->wheel.preciseY;
424#else
425 float wheel_x = -(float)event->wheel.x;
426 float wheel_y = (float)event->wheel.y;
427#endif
428#if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0)
429 wheel_x /= 100.0f;
430#endif
431 io.AddMouseSourceEvent(source: event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
432 io.AddMouseWheelEvent(wheel_x, wheel_y);
433 return true;
434 }
435 case SDL_MOUSEBUTTONDOWN:
436 case SDL_MOUSEBUTTONUP:
437 {
438 if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->button.windowID) == nullptr)
439 return false;
440 int mouse_button = -1;
441 if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
442 if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
443 if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
444 if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
445 if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
446 if (mouse_button == -1)
447 break;
448 io.AddMouseSourceEvent(source: event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
449 io.AddMouseButtonEvent(button: mouse_button, down: (event->type == SDL_MOUSEBUTTONDOWN));
450 bd->MouseButtonsDown = (event->type == SDL_MOUSEBUTTONDOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
451 return true;
452 }
453 case SDL_TEXTINPUT:
454 {
455 if (ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->text.windowID) == nullptr)
456 return false;
457 io.AddInputCharactersUTF8(str: event->text.text);
458 return true;
459 }
460 case SDL_KEYDOWN:
461 case SDL_KEYUP:
462 {
463 ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->key.windowID);
464 if (viewport == nullptr)
465 return false;
466 //IMGUI_DEBUG_LOG("SDL_KEY%s : key=0x%08X ('%s'), scancode=%d ('%s'), mod=%X, windowID=%d, viewport=%08X\n",
467 // (event->type == SDL_KEYDOWN) ? "DOWN" : "UP ", event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym), event->key.keysym.scancode, SDL_GetScancodeName(event->key.keysym.scancode), event->key.keysym.mod, event->key.windowID, viewport ? viewport->ID : 0);
468 ImGui_ImplSDL2_UpdateKeyModifiers(sdl_key_mods: (SDL_Keymod)event->key.keysym.mod);
469 ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(keycode: event->key.keysym.sym, scancode: event->key.keysym.scancode);
470 io.AddKeyEvent(key, down: (event->type == SDL_KEYDOWN));
471 io.SetKeyEventNativeData(key, native_keycode: (int)event->key.keysym.sym, native_scancode: (int)event->key.keysym.scancode, native_legacy_index: (int)event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
472 return true;
473 }
474#if SDL_HAS_DISPLAY_EVENT
475 case SDL_DISPLAYEVENT:
476 {
477 // 2.0.26 has SDL_DISPLAYEVENT_CONNECTED/SDL_DISPLAYEVENT_DISCONNECTED/SDL_DISPLAYEVENT_ORIENTATION,
478 // so change of DPI/Scaling are not reflected in this event. (SDL3 has it)
479 bd->WantUpdateMonitors = true;
480 return true;
481 }
482#endif
483 case SDL_WINDOWEVENT:
484 {
485 ImGuiViewport* viewport = ImGui_ImplSDL2_GetViewportForWindowID(window_id: event->window.windowID);
486 if (viewport == NULL)
487 return false;
488
489 // - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right.
490 // - However we won't get a correct LEAVE event for a captured window.
491 // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
492 // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
493 // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
494 Uint8 window_event = event->window.event;
495 if (window_event == SDL_WINDOWEVENT_ENTER)
496 {
497 bd->MouseWindowID = event->window.windowID;
498 bd->MouseLastLeaveFrame = 0;
499 }
500 if (window_event == SDL_WINDOWEVENT_LEAVE)
501 bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1;
502 //if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED) { IMGUI_DEBUG_LOG("SDL_WINDOWEVENT_FOCUS_GAINED: windowId %d, viewport: %08X\n", event->window.windowID, viewport ? viewport->ID : 0); }
503 //if (window_event == SDL_WINDOWEVENT_FOCUS_LOST) { IMGUI_DEBUG_LOG("SDL_WINDOWEVENT_FOCUS_LOST: windowId %d, viewport: %08X\n", event->window.windowID, viewport ? viewport->ID : 0); }
504 if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
505 io.AddFocusEvent(focused: true);
506 if (window_event == SDL_WINDOWEVENT_FOCUS_LOST)
507 io.AddFocusEvent(focused: false);
508 if (window_event == SDL_WINDOWEVENT_CLOSE)
509 viewport->PlatformRequestClose = true;
510 if (window_event == SDL_WINDOWEVENT_MOVED)
511 viewport->PlatformRequestMove = true;
512 if (window_event == SDL_WINDOWEVENT_RESIZED)
513 viewport->PlatformRequestResize = true;
514 return true;
515 }
516 case SDL_CONTROLLERDEVICEADDED:
517 case SDL_CONTROLLERDEVICEREMOVED:
518 {
519 bd->WantUpdateGamepadsList = true;
520 return true;
521 }
522 default:
523 break;
524 }
525 return false;
526}
527
528#ifdef __EMSCRIPTEN__
529EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); });
530#endif
531
532static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
533{
534 ImGuiIO& io = ImGui::GetIO();
535 IMGUI_CHECKVERSION();
536 IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
537 //SDL_SetHint(SDL_HINT_EVENT_LOGGING, "2");
538
539 // Obtain compiled and runtime versions
540 SDL_version ver_compiled;
541 SDL_version ver_runtime;
542 SDL_VERSION(&ver_compiled);
543 SDL_GetVersion(ver: &ver_runtime);
544
545 // Setup backend capabilities flags
546 ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)();
547 snprintf(s: bd->BackendPlatformName, maxlen: sizeof(bd->BackendPlatformName), format: "imgui_impl_sdl2 (%u.%u.%u, %u.%u.%u)",
548 ver_compiled.major, ver_compiled.minor, ver_compiled.patch, ver_runtime.major, ver_runtime.minor, ver_runtime.patch);
549 io.BackendPlatformUserData = (void*)bd;
550 io.BackendPlatformName = bd->BackendPlatformName;
551 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
552 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
553 // (ImGuiBackendFlags_PlatformHasViewports may be set just below)
554
555 bd->Window = window;
556 bd->WindowID = SDL_GetWindowID(window);
557 bd->Renderer = renderer;
558
559 // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
560 // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
561#ifndef __APPLE__
562 bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
563#else
564 bd->MouseCanReportHoveredViewport = false;
565#endif
566
567 // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
568 // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
569 bd->MouseCanUseGlobalState = false;
570 bd->MouseCanUseCapture = false;
571#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
572 const char* sdl_backend = SDL_GetCurrentVideoDriver();
573 const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
574 for (const char* item : capture_and_global_state_whitelist)
575 if (strncmp(s1: sdl_backend, s2: item, n: strlen(s: item)) == 0)
576 bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
577#endif
578 if (bd->MouseCanUseGlobalState)
579 io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
580
581 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
582 platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
583 platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
584 platform_io.Platform_ClipboardUserData = nullptr;
585 platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData;
586#ifdef __EMSCRIPTEN__
587 platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; };
588#elif SDL_HAS_OPEN_URL
589 platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { return SDL_OpenURL(url) == 0; };
590#endif
591
592 // Update monitor a first time during init
593 ImGui_ImplSDL2_UpdateMonitors();
594
595 // Gamepad handling
596 bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
597 bd->WantUpdateGamepadsList = true;
598
599 // Load mouse cursors
600 bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_ARROW);
601 bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_IBEAM);
602 bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZEALL);
603 bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZENS);
604 bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZEWE);
605 bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZENESW);
606 bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_SIZENWSE);
607 bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_HAND);
608 bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_WAIT);
609 bd->MouseCursors[ImGuiMouseCursor_Progress] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_WAITARROW);
610 bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(id: SDL_SYSTEM_CURSOR_NO);
611
612 // Set platform dependent data in viewport
613 // Our mouse update function expect PlatformHandle to be filled for the main viewport
614 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
615 main_viewport->PlatformHandle = (void*)(intptr_t)bd->WindowID;
616 main_viewport->PlatformHandleRaw = nullptr;
617 SDL_SysWMinfo info;
618 SDL_VERSION(&info.version);
619 if (SDL_GetWindowWMInfo(window, info: &info))
620 {
621#if defined(SDL_VIDEO_DRIVER_WINDOWS)
622 main_viewport->PlatformHandleRaw = (void*)info.info.win.window;
623#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
624 main_viewport->PlatformHandleRaw = (void*)info.info.cocoa.window;
625#endif
626 }
627
628 // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
629 // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
630 // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
631 // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
632 // you can ignore SDL_MOUSEBUTTONDOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
633#ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
634 SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, value: "1");
635#endif
636
637 // From 2.0.18: Enable native IME.
638 // IMPORTANT: This is used at the time of SDL_CreateWindow() so this will only affects secondary windows, if any.
639 // For the main window to be affected, your application needs to call this manually before calling SDL_CreateWindow().
640#ifdef SDL_HINT_IME_SHOW_UI
641 SDL_SetHint(SDL_HINT_IME_SHOW_UI, value: "1");
642#endif
643
644 // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
645#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
646 SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
647#endif
648
649 // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
650 // We left the call to ImGui_ImplSDL2_InitMultiViewportSupport() outside of #ifdef to avoid unused-function warnings.
651 if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports)
652 ImGui_ImplSDL2_InitMultiViewportSupport(window, sdl_gl_context);
653
654 return true;
655}
656
657bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
658{
659 return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context);
660}
661
662bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
663{
664#if !SDL_HAS_VULKAN
665 IM_ASSERT(0 && "Unsupported");
666#endif
667 if (!ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr))
668 return false;
669 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
670 bd->UseVulkan = true;
671 return true;
672}
673
674bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
675{
676#if !defined(_WIN32)
677 IM_ASSERT(0 && "Unsupported");
678#endif
679 return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr);
680}
681
682bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
683{
684 return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr);
685}
686
687bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
688{
689 return ImGui_ImplSDL2_Init(window, renderer, sdl_gl_context: nullptr);
690}
691
692bool ImGui_ImplSDL2_InitForOther(SDL_Window* window)
693{
694 return ImGui_ImplSDL2_Init(window, renderer: nullptr, sdl_gl_context: nullptr);
695}
696
697static void ImGui_ImplSDL2_CloseGamepads();
698
699void ImGui_ImplSDL2_Shutdown()
700{
701 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
702 IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
703 ImGuiIO& io = ImGui::GetIO();
704
705 ImGui_ImplSDL2_ShutdownMultiViewportSupport();
706
707 if (bd->ClipboardTextData)
708 SDL_free(mem: bd->ClipboardTextData);
709 for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
710 SDL_FreeCursor(cursor: bd->MouseCursors[cursor_n]);
711 ImGui_ImplSDL2_CloseGamepads();
712
713 io.BackendPlatformName = nullptr;
714 io.BackendPlatformUserData = nullptr;
715 io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
716 IM_DELETE(p: bd);
717}
718
719// This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4.
720static void ImGui_ImplSDL2_UpdateMouseData()
721{
722 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
723 ImGuiIO& io = ImGui::GetIO();
724
725 // We forward mouse input when hovered or captured (via SDL_MOUSEMOTION) or when focused (below)
726#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
727 // - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside.
728 // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture.
729 if (bd->MouseCanUseCapture)
730 {
731 bool want_capture = false;
732 for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
733 if (ImGui::IsMouseDragging(button: button_n, lock_threshold: 1.0f))
734 want_capture = true;
735 SDL_CaptureMouse(enabled: want_capture ? SDL_TRUE : SDL_FALSE);
736 }
737
738 SDL_Window* focused_window = SDL_GetKeyboardFocus();
739 const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL2_GetViewportForWindowID(window_id: SDL_GetWindowID(window: focused_window)) != NULL));
740#else
741 SDL_Window* focused_window = bd->Window;
742 const bool is_app_focused = (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; // SDL 2.0.3 and non-windowed systems: single-viewport only
743#endif
744
745 if (is_app_focused)
746 {
747 // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
748 if (io.WantSetMousePos)
749 {
750#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
751 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
752 SDL_WarpMouseGlobal(x: (int)io.MousePos.x, y: (int)io.MousePos.y);
753 else
754#endif
755 SDL_WarpMouseInWindow(window: bd->Window, x: (int)io.MousePos.x, y: (int)io.MousePos.y);
756 }
757
758 // (Optional) Fallback to provide mouse position when focused (SDL_MOUSEMOTION already provides this when hovered or captured)
759 const bool is_relative_mouse_mode = SDL_GetRelativeMouseMode() != 0;
760 if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
761 {
762 // Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
763 // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
764 int mouse_x, mouse_y, window_x, window_y;
765 SDL_GetGlobalMouseState(x: &mouse_x, y: &mouse_y);
766 if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
767 {
768 SDL_GetWindowPosition(window: focused_window, x: &window_x, y: &window_y);
769 mouse_x -= window_x;
770 mouse_y -= window_y;
771 }
772 io.AddMousePosEvent(x: (float)mouse_x, y: (float)mouse_y);
773 }
774 }
775
776 // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
777 // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
778 // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag.
779 // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window
780 // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported
781 // by the backend, and use its flawed heuristic to guess the viewport behind.
782 // - [X] SDL backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target).
783 if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
784 {
785 ImGuiID mouse_viewport_id = 0;
786 if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL2_GetViewportForWindowID(window_id: bd->MouseWindowID))
787 mouse_viewport_id = mouse_viewport->ID;
788 io.AddMouseViewportEvent(id: mouse_viewport_id);
789 }
790}
791
792static void ImGui_ImplSDL2_UpdateMouseCursor()
793{
794 ImGuiIO& io = ImGui::GetIO();
795 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
796 return;
797 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
798
799 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
800 if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
801 {
802 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
803 SDL_ShowCursor(toggle: SDL_FALSE);
804 }
805 else
806 {
807 // Show OS mouse cursor
808 SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
809 if (bd->MouseLastCursor != expected_cursor)
810 {
811 SDL_SetCursor(cursor: expected_cursor); // SDL function doesn't have an early out (see #6113)
812 bd->MouseLastCursor = expected_cursor;
813 }
814 SDL_ShowCursor(toggle: SDL_TRUE);
815 }
816}
817
818// - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend.
819// - Apple platforms use FramebufferScale so we always return 1.0f.
820// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
821float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window)
822{
823 return ImGui_ImplSDL2_GetContentScaleForDisplay(display_index: SDL_GetWindowDisplayIndex(window));
824}
825
826float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index)
827{
828#if SDL_HAS_PER_MONITOR_DPI
829#if !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
830 float dpi = 0.0f;
831 if (SDL_GetDisplayDPI(displayIndex: display_index, ddpi: &dpi, hdpi: nullptr, vdpi: nullptr) == 0)
832 return dpi / 96.0f;
833#endif
834#endif
835 IM_UNUSED(display_index);
836 return 1.0f;
837}
838
839static void ImGui_ImplSDL2_CloseGamepads()
840{
841 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
842 if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
843 for (SDL_GameController* gamepad : bd->Gamepads)
844 SDL_GameControllerClose(gamecontroller: gamepad);
845 bd->Gamepads.resize(new_size: 0);
846}
847
848void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count)
849{
850 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
851 ImGui_ImplSDL2_CloseGamepads();
852 if (mode == ImGui_ImplSDL2_GamepadMode_Manual)
853 {
854 IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0);
855 for (int n = 0; n < manual_gamepads_count; n++)
856 bd->Gamepads.push_back(v: manual_gamepads_array[n]);
857 }
858 else
859 {
860 IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
861 bd->WantUpdateGamepadsList = true;
862 }
863 bd->GamepadMode = mode;
864}
865
866static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no)
867{
868 bool merged_value = false;
869 for (SDL_GameController* gamepad : bd->Gamepads)
870 merged_value |= SDL_GameControllerGetButton(gamecontroller: gamepad, button: button_no) != 0;
871 io.AddKeyEvent(key, down: merged_value);
872}
873
874static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
875static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1)
876{
877 float merged_value = 0.0f;
878 for (SDL_GameController* gamepad : bd->Gamepads)
879 {
880 float vn = Saturate(v: (float)(SDL_GameControllerGetAxis(gamecontroller: gamepad, axis: axis_no) - v0) / (float)(v1 - v0));
881 if (merged_value < vn)
882 merged_value = vn;
883 }
884 io.AddKeyAnalogEvent(key, down: merged_value > 0.1f, v: merged_value);
885}
886
887static void ImGui_ImplSDL2_UpdateGamepads()
888{
889 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
890 ImGuiIO& io = ImGui::GetIO();
891
892 // Update list of controller(s) to use
893 if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
894 {
895 ImGui_ImplSDL2_CloseGamepads();
896 int joystick_count = SDL_NumJoysticks();
897 for (int n = 0; n < joystick_count; n++)
898 if (SDL_IsGameController(joystick_index: n))
899 if (SDL_GameController* gamepad = SDL_GameControllerOpen(joystick_index: n))
900 {
901 bd->Gamepads.push_back(v: gamepad);
902 if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst)
903 break;
904 }
905 bd->WantUpdateGamepadsList = false;
906 }
907
908 io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
909 if (bd->Gamepads.Size == 0)
910 return;
911 io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
912
913 // Update gamepad inputs
914 const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
915 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadStart, button_no: SDL_CONTROLLER_BUTTON_START);
916 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadBack, button_no: SDL_CONTROLLER_BUTTON_BACK);
917 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceLeft, button_no: SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square
918 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceRight, button_no: SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle
919 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceUp, button_no: SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle
920 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadFaceDown, button_no: SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross
921 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadLeft, button_no: SDL_CONTROLLER_BUTTON_DPAD_LEFT);
922 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadRight, button_no: SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
923 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadUp, button_no: SDL_CONTROLLER_BUTTON_DPAD_UP);
924 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadDpadDown, button_no: SDL_CONTROLLER_BUTTON_DPAD_DOWN);
925 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadL1, button_no: SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
926 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadR1, button_no: SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
927 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadL2, axis_no: SDL_CONTROLLER_AXIS_TRIGGERLEFT, v0: 0.0f, v1: 32767);
928 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadR2, axis_no: SDL_CONTROLLER_AXIS_TRIGGERRIGHT, v0: 0.0f, v1: 32767);
929 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadL3, button_no: SDL_CONTROLLER_BUTTON_LEFTSTICK);
930 ImGui_ImplSDL2_UpdateGamepadButton(bd, io, key: ImGuiKey_GamepadR3, button_no: SDL_CONTROLLER_BUTTON_RIGHTSTICK);
931 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickLeft, axis_no: SDL_CONTROLLER_AXIS_LEFTX, v0: -thumb_dead_zone, v1: -32768);
932 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickRight, axis_no: SDL_CONTROLLER_AXIS_LEFTX, v0: +thumb_dead_zone, v1: +32767);
933 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickUp, axis_no: SDL_CONTROLLER_AXIS_LEFTY, v0: -thumb_dead_zone, v1: -32768);
934 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadLStickDown, axis_no: SDL_CONTROLLER_AXIS_LEFTY, v0: +thumb_dead_zone, v1: +32767);
935 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickLeft, axis_no: SDL_CONTROLLER_AXIS_RIGHTX, v0: -thumb_dead_zone, v1: -32768);
936 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickRight, axis_no: SDL_CONTROLLER_AXIS_RIGHTX, v0: +thumb_dead_zone, v1: +32767);
937 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickUp, axis_no: SDL_CONTROLLER_AXIS_RIGHTY, v0: -thumb_dead_zone, v1: -32768);
938 ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, key: ImGuiKey_GamepadRStickDown, axis_no: SDL_CONTROLLER_AXIS_RIGHTY, v0: +thumb_dead_zone, v1: +32767);
939}
940
941// FIXME: Note that doesn't update with DPI/Scaling change only as SDL2 doesn't have an event for it (SDL3 has).
942static void ImGui_ImplSDL2_UpdateMonitors()
943{
944 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
945 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
946 platform_io.Monitors.resize(new_size: 0);
947 bd->WantUpdateMonitors = false;
948 int display_count = SDL_GetNumVideoDisplays();
949 for (int n = 0; n < display_count; n++)
950 {
951 // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
952 ImGuiPlatformMonitor monitor;
953 SDL_Rect r;
954 SDL_GetDisplayBounds(displayIndex: n, rect: &r);
955 monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
956 monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
957#if SDL_HAS_USABLE_DISPLAY_BOUNDS
958 if (SDL_GetDisplayUsableBounds(displayIndex: n, rect: &r) == 0 && r.w > 0 && r.h > 0)
959 {
960 monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
961 monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
962 }
963#endif
964 float dpi_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(display_index: n);
965 if (dpi_scale <= 0.0f)
966 continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
967 monitor.DpiScale = dpi_scale;
968 monitor.PlatformHandle = (void*)(intptr_t)n;
969 platform_io.Monitors.push_back(v: monitor);
970 }
971}
972
973static void ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(SDL_Window* window, SDL_Renderer* renderer, ImVec2* out_size, ImVec2* out_framebuffer_scale)
974{
975 int w, h;
976 int display_w, display_h;
977 SDL_GetWindowSize(window, w: &w, h: &h);
978 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
979 w = h = 0;
980 if (renderer != nullptr)
981 SDL_GetRendererOutputSize(renderer, w: &display_w, h: &display_h);
982#if SDL_HAS_VULKAN
983 else if (SDL_GetWindowFlags(window) & SDL_WINDOW_VULKAN)
984 SDL_Vulkan_GetDrawableSize(window, w: &display_w, h: &display_h);
985#endif
986 else
987 SDL_GL_GetDrawableSize(window, w: &display_w, h: &display_h);
988 if (out_size != nullptr)
989 *out_size = ImVec2((float)w, (float)h);
990 if (out_framebuffer_scale != nullptr)
991 *out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f);
992}
993
994void ImGui_ImplSDL2_NewFrame()
995{
996 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
997 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
998 ImGuiIO& io = ImGui::GetIO();
999
1000 // Setup main viewport size (every frame to accommodate for window resizing)
1001 ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(window: bd->Window, renderer: bd->Renderer, out_size: &io.DisplaySize, out_framebuffer_scale: &io.DisplayFramebufferScale);
1002
1003 // Update monitors
1004#ifdef WIN32
1005 bd->WantUpdateMonitors = true; // Keep polling under Windows to handle changes of work area when resizing task-bar (#8415)
1006#endif
1007 if (bd->WantUpdateMonitors)
1008 ImGui_ImplSDL2_UpdateMonitors();
1009
1010 // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
1011 // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
1012 static Uint64 frequency = SDL_GetPerformanceFrequency();
1013 Uint64 current_time = SDL_GetPerformanceCounter();
1014 if (current_time <= bd->Time)
1015 current_time = bd->Time + 1;
1016 io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
1017 bd->Time = current_time;
1018
1019 if (bd->MouseLastLeaveFrame && bd->MouseLastLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
1020 {
1021 bd->MouseWindowID = 0;
1022 bd->MouseLastLeaveFrame = 0;
1023 io.AddMousePosEvent(x: -FLT_MAX, y: -FLT_MAX);
1024 }
1025
1026 // Our io.AddMouseViewportEvent() calls will only be valid when not capturing.
1027 // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects.
1028 if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr)
1029 io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
1030 else
1031 io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
1032
1033 ImGui_ImplSDL2_UpdateMouseData();
1034 ImGui_ImplSDL2_UpdateMouseCursor();
1035
1036 // Update game controllers (if enabled and available)
1037 ImGui_ImplSDL2_UpdateGamepads();
1038}
1039
1040//--------------------------------------------------------------------------------------------------------
1041// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
1042// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
1043// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
1044//--------------------------------------------------------------------------------------------------------
1045
1046// Helper structure we store in the void* PlatformUserData field of each ImGuiViewport to easily retrieve our backend data.
1047struct ImGui_ImplSDL2_ViewportData
1048{
1049 SDL_Window* Window;
1050 Uint32 WindowID; // Stored in ImGuiViewport::PlatformHandle. Use SDL_GetWindowFromID() to get SDL_Window* from Uint32 WindowID.
1051 bool WindowOwned;
1052 SDL_GLContext GLContext;
1053
1054 ImGui_ImplSDL2_ViewportData() { Window = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; }
1055 ~ImGui_ImplSDL2_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); }
1056};
1057
1058static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport)
1059{
1060 ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
1061 ImGui_ImplSDL2_ViewportData* vd = IM_NEW(ImGui_ImplSDL2_ViewportData)();
1062 viewport->PlatformUserData = vd;
1063
1064 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
1065 ImGui_ImplSDL2_ViewportData* main_viewport_data = (ImGui_ImplSDL2_ViewportData*)main_viewport->PlatformUserData;
1066
1067 // Share GL resources with main context
1068 bool use_opengl = (main_viewport_data->GLContext != nullptr);
1069 SDL_GLContext backup_context = nullptr;
1070 if (use_opengl)
1071 {
1072 backup_context = SDL_GL_GetCurrentContext();
1073 SDL_GL_SetAttribute(attr: SDL_GL_SHARE_WITH_CURRENT_CONTEXT, value: 1);
1074 SDL_GL_MakeCurrent(window: main_viewport_data->Window, context: main_viewport_data->GLContext);
1075 }
1076
1077 Uint32 sdl_flags = 0;
1078 sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0);
1079 sdl_flags |= SDL_GetWindowFlags(window: bd->Window) & SDL_WINDOW_ALLOW_HIGHDPI;
1080 sdl_flags |= SDL_WINDOW_HIDDEN;
1081 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
1082 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
1083#if !defined(_WIN32)
1084 // See SDL hack in ImGui_ImplSDL2_ShowWindow().
1085 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_SKIP_TASKBAR : 0;
1086#endif
1087#if SDL_HAS_ALWAYS_ON_TOP
1088 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
1089#endif
1090 vd->Window = SDL_CreateWindow(title: "No Title Yet", x: (int)viewport->Pos.x, y: (int)viewport->Pos.y, w: (int)viewport->Size.x, h: (int)viewport->Size.y, flags: sdl_flags);
1091 vd->WindowOwned = true;
1092 if (use_opengl)
1093 {
1094 vd->GLContext = SDL_GL_CreateContext(window: vd->Window);
1095 SDL_GL_SetSwapInterval(interval: 0);
1096 }
1097 if (use_opengl && backup_context)
1098 SDL_GL_MakeCurrent(window: vd->Window, context: backup_context);
1099
1100 viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window: vd->Window);
1101 viewport->PlatformHandleRaw = nullptr;
1102 SDL_SysWMinfo info;
1103 SDL_VERSION(&info.version);
1104 if (SDL_GetWindowWMInfo(window: vd->Window, info: &info))
1105 {
1106#if defined(SDL_VIDEO_DRIVER_WINDOWS)
1107 viewport->PlatformHandleRaw = info.info.win.window;
1108#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
1109 viewport->PlatformHandleRaw = (void*)info.info.cocoa.window;
1110#endif
1111 }
1112}
1113
1114static void ImGui_ImplSDL2_DestroyWindow(ImGuiViewport* viewport)
1115{
1116 if (ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData)
1117 {
1118 if (vd->GLContext && vd->WindowOwned)
1119 SDL_GL_DeleteContext(context: vd->GLContext);
1120 if (vd->Window && vd->WindowOwned)
1121 SDL_DestroyWindow(window: vd->Window);
1122 vd->GLContext = nullptr;
1123 vd->Window = nullptr;
1124 IM_DELETE(p: vd);
1125 }
1126 viewport->PlatformUserData = viewport->PlatformHandle = nullptr;
1127}
1128
1129static void ImGui_ImplSDL2_ShowWindow(ImGuiViewport* viewport)
1130{
1131 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1132#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES)))
1133 HWND hwnd = (HWND)viewport->PlatformHandleRaw;
1134
1135 // SDL hack: Hide icon from task bar
1136 // Note: SDL 2.0.6+ has a SDL_WINDOW_SKIP_TASKBAR flag which is supported under Windows but the way it create the window breaks our seamless transition.
1137 if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
1138 {
1139 LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
1140 ex_style &= ~WS_EX_APPWINDOW;
1141 ex_style |= WS_EX_TOOLWINDOW;
1142 ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
1143 }
1144#endif
1145
1146#if SDL_HAS_SHOW_WINDOW_ACTIVATION_HINT
1147 SDL_SetHint(SDL_HINT_WINDOW_NO_ACTIVATION_WHEN_SHOWN, value: (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "1" : "0");
1148#elif defined(_WIN32)
1149 // SDL hack: SDL always activate/focus windows :/
1150 if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
1151 {
1152 ::ShowWindow(hwnd, SW_SHOWNA);
1153 return;
1154 }
1155#endif
1156 SDL_ShowWindow(window: vd->Window);
1157}
1158
1159static ImVec2 ImGui_ImplSDL2_GetWindowPos(ImGuiViewport* viewport)
1160{
1161 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1162 int x = 0, y = 0;
1163 SDL_GetWindowPosition(window: vd->Window, x: &x, y: &y);
1164 return ImVec2((float)x, (float)y);
1165}
1166
1167static void ImGui_ImplSDL2_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
1168{
1169 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1170 SDL_SetWindowPosition(window: vd->Window, x: (int)pos.x, y: (int)pos.y);
1171}
1172
1173static ImVec2 ImGui_ImplSDL2_GetWindowSize(ImGuiViewport* viewport)
1174{
1175 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1176 int w = 0, h = 0;
1177 SDL_GetWindowSize(window: vd->Window, w: &w, h: &h);
1178 return ImVec2((float)w, (float)h);
1179}
1180
1181static void ImGui_ImplSDL2_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
1182{
1183 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1184 SDL_SetWindowSize(window: vd->Window, w: (int)size.x, h: (int)size.y);
1185}
1186
1187static ImVec2 ImGui_ImplSDL2_GetWindowFramebufferScale(ImGuiViewport* viewport)
1188{
1189 // FIXME: SDL_Renderer does not support multi-viewport.
1190 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1191 ImVec2 framebuffer_scale;
1192 ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(window: vd->Window, renderer: nullptr, out_size: nullptr, out_framebuffer_scale: &framebuffer_scale);
1193 return framebuffer_scale;
1194}
1195
1196static void ImGui_ImplSDL2_SetWindowTitle(ImGuiViewport* viewport, const char* title)
1197{
1198 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1199 SDL_SetWindowTitle(window: vd->Window, title);
1200}
1201
1202#if SDL_HAS_WINDOW_ALPHA
1203static void ImGui_ImplSDL2_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
1204{
1205 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1206 SDL_SetWindowOpacity(window: vd->Window, opacity: alpha);
1207}
1208#endif
1209
1210static void ImGui_ImplSDL2_SetWindowFocus(ImGuiViewport* viewport)
1211{
1212 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1213 SDL_RaiseWindow(window: vd->Window);
1214}
1215
1216static bool ImGui_ImplSDL2_GetWindowFocus(ImGuiViewport* viewport)
1217{
1218 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1219 return (SDL_GetWindowFlags(window: vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
1220}
1221
1222static bool ImGui_ImplSDL2_GetWindowMinimized(ImGuiViewport* viewport)
1223{
1224 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1225 return (SDL_GetWindowFlags(window: vd->Window) & SDL_WINDOW_MINIMIZED) != 0;
1226}
1227
1228static void ImGui_ImplSDL2_RenderWindow(ImGuiViewport* viewport, void*)
1229{
1230 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1231 if (vd->GLContext)
1232 SDL_GL_MakeCurrent(window: vd->Window, context: vd->GLContext);
1233}
1234
1235static void ImGui_ImplSDL2_SwapBuffers(ImGuiViewport* viewport, void*)
1236{
1237 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1238 if (vd->GLContext)
1239 {
1240 SDL_GL_MakeCurrent(window: vd->Window, context: vd->GLContext);
1241 SDL_GL_SwapWindow(window: vd->Window);
1242 }
1243}
1244
1245// Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
1246// SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
1247#if SDL_HAS_VULKAN
1248#include <SDL_vulkan.h>
1249static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
1250{
1251 ImGui_ImplSDL2_ViewportData* vd = (ImGui_ImplSDL2_ViewportData*)viewport->PlatformUserData;
1252 (void)vk_allocator;
1253 SDL_bool ret = SDL_Vulkan_CreateSurface(window: vd->Window, instance: (VkInstance)vk_instance, surface: (VkSurfaceKHR*)out_vk_surface);
1254 return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
1255}
1256#endif // SDL_HAS_VULKAN
1257
1258static void ImGui_ImplSDL2_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context)
1259{
1260 // Register platform interface (will be coupled with a renderer interface)
1261 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
1262 platform_io.Platform_CreateWindow = ImGui_ImplSDL2_CreateWindow;
1263 platform_io.Platform_DestroyWindow = ImGui_ImplSDL2_DestroyWindow;
1264 platform_io.Platform_ShowWindow = ImGui_ImplSDL2_ShowWindow;
1265 platform_io.Platform_SetWindowPos = ImGui_ImplSDL2_SetWindowPos;
1266 platform_io.Platform_GetWindowPos = ImGui_ImplSDL2_GetWindowPos;
1267 platform_io.Platform_SetWindowSize = ImGui_ImplSDL2_SetWindowSize;
1268 platform_io.Platform_GetWindowSize = ImGui_ImplSDL2_GetWindowSize;
1269 platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplSDL2_GetWindowFramebufferScale;
1270 platform_io.Platform_SetWindowFocus = ImGui_ImplSDL2_SetWindowFocus;
1271 platform_io.Platform_GetWindowFocus = ImGui_ImplSDL2_GetWindowFocus;
1272 platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL2_GetWindowMinimized;
1273 platform_io.Platform_SetWindowTitle = ImGui_ImplSDL2_SetWindowTitle;
1274 platform_io.Platform_RenderWindow = ImGui_ImplSDL2_RenderWindow;
1275 platform_io.Platform_SwapBuffers = ImGui_ImplSDL2_SwapBuffers;
1276#if SDL_HAS_WINDOW_ALPHA
1277 platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL2_SetWindowAlpha;
1278#endif
1279#if SDL_HAS_VULKAN
1280 platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface;
1281#endif
1282
1283 // Register main window handle (which is owned by the main application, not by us)
1284 // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports.
1285 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
1286 ImGui_ImplSDL2_ViewportData* vd = IM_NEW(ImGui_ImplSDL2_ViewportData)();
1287 vd->Window = window;
1288 vd->WindowID = SDL_GetWindowID(window);
1289 vd->WindowOwned = false;
1290 vd->GLContext = sdl_gl_context;
1291 main_viewport->PlatformUserData = vd;
1292 main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID;
1293}
1294
1295static void ImGui_ImplSDL2_ShutdownMultiViewportSupport()
1296{
1297 ImGui::DestroyPlatformWindows();
1298}
1299
1300//-----------------------------------------------------------------------------
1301
1302#if defined(__clang__)
1303#pragma clang diagnostic pop
1304#endif
1305
1306#endif // #ifndef IMGUI_DISABLE
1307

source code of imgui/backends/imgui_impl_sdl2.cpp