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

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of imgui/backends/imgui_impl_sdl2.cpp