Debian Codegraph @

extern/imgui_patched/backends/imgui_impl_sdl3.cpp

raw
1 // dear imgui: Platform Backend for SDL3
2 // This needs to be used along with a Renderer (e.g. SDL_GPU, DirectX11, OpenGL3, Vulkan..)
3 // (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
4
5 // Implemented features:
6 // [X] Platform: Clipboard support.
7 // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
8 // [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]
9 // [X] Platform: Gamepad support.
10 // [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
11 // [x] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable' -> the OS animation effect when window gets created/destroyed is problematic. SDL2 backend doesn't have issue.
12 // Missing features or Issues:
13 // [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows).
14 // [x] Platform: IME support. Position somehow broken in SDL3 + app needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
15
16 // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
17 // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
18 // Learn about Dear ImGui:
19 // - FAQ https://dearimgui.com/faq
20 // - Getting Started https://dearimgui.com/getting-started
21 // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
22 // - Introduction, links and more at the top of imgui.cpp
23
24 // CHANGELOG
25 // (minor and older changes stripped away, please see git history for details)
26 // 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
27 // 2025-06-27: IME: avoid calling SDL_StartTextInput() again if already active. (#8727)
28 // 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors.
29 // 2025-05-06: [Docking] macOS: fixed secondary viewports not appearing on other monitors before of parenting.
30 // 2025-04-09: [Docking] Revert update monitors and work areas information every frame. Only do it on Windows. (#8415, #8558)
31 // 2025-04-22: IME: honor ImGuiPlatformImeData->WantTextInput as an alternative way to call SDL_StartTextInput(), without IME being necessarily visible.
32 // 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
33 // 2025-03-30: Update for SDL3 api changes: Revert SDL_GetClipboardText() memory ownership change. (#8530, #7801)
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_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode_Manual) accept an empty array.
43 // 2024-10-24: Emscripten: SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f on Emscripten.
44 // 2024-09-11: (Docking) Added support for viewport->ParentViewportId field to support parenting at OS level. (#7973)
45 // 2024-09-03: Update for SDL3 api changes: SDL_GetGamepads() memory ownership revert. (#7918, #7898, #7807)
46 // 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
47 // - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
48 // - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
49 // - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
50 // 2024-08-19: Storing SDL_WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
51 // 2024-08-19: ImGui_ImplSDL3_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
52 // 2024-07-22: Update for SDL3 api changes: SDL_GetGamepads() memory ownership change. (#7807)
53 // 2024-07-18: Update for SDL3 api changes: SDL_GetClipboardText() memory ownership change. (#7801)
54 // 2024-07-15: Update for SDL3 api changes: SDL_GetProperty() change to SDL_GetPointerProperty(). (#7794)
55 // 2024-07-02: Update for SDL3 api changes: SDLK_x renames and SDLK_KP_x removals (#7761, #7762).
56 // 2024-07-01: Update for SDL3 api changes: SDL_SetTextInputRect() changed to SDL_SetTextInputArea().
57 // 2024-06-26: Update for SDL3 api changes: SDL_StartTextInput()/SDL_StopTextInput()/SDL_SetTextInputRect() functions signatures.
58 // 2024-06-24: Update for SDL3 api changes: SDL_EVENT_KEY_DOWN/SDL_EVENT_KEY_UP contents.
59 // 2024-06-03; Update for SDL3 api changes: SDL_SYSTEM_CURSOR_ renames.
60 // 2024-05-15: Update for SDL3 api changes: SDLK_ renames.
61 // 2024-04-15: Inputs: Re-enable calling SDL_StartTextInput()/SDL_StopTextInput() as SDL3 no longer enables it by default and should play nicer with IME.
62 // 2024-02-13: Inputs: Fixed gamepad support. Handle gamepad disconnection. Added ImGui_ImplSDL3_SetGamepadMode().
63 // 2023-11-13: Updated for recent SDL3 API changes.
64 // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
65 // 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391)
66 // 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)
67 // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
68 // 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
69 // 2023-02-07: Forked "imgui_impl_sdl2" into "imgui_impl_sdl3". Removed version checks for old feature. Refer to imgui_impl_sdl2.cpp for older changelog.
70
71 #include "imgui.h"
72 #ifndef IMGUI_DISABLE
73 #include "imgui_impl_sdl3.h"
74
75 // Clang warnings with -Weverything
76 #if defined(__clang__)
77 #pragma clang diagnostic push
78 #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
79 #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
80 #endif
81
82 // SDL
83 #include <SDL3/SDL.h>
84 #include <stdio.h> // for snprintf()
85 #if defined(__APPLE__)
86 #include <TargetConditionals.h>
87 #endif
88 #ifdef _WIN32
89 #ifndef WIN32_LEAN_AND_MEAN
90 #define WIN32_LEAN_AND_MEAN
91 #endif
92 #include <windows.h>
93 #endif
94
95 #if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
96 #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
97 #else
98 #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
99 #endif
100
101 // FIXME-LEGACY: remove when SDL 3.1.3 preview is released.
102 #ifndef SDLK_APOSTROPHE
103 #define SDLK_APOSTROPHE SDLK_QUOTE
104 #endif
105 #ifndef SDLK_GRAVE
106 #define SDLK_GRAVE SDLK_BACKQUOTE
107 #endif
108
109 // SDL Data
110 struct ImGui_ImplSDL3_Data
111 {
112 SDL_Window* Window;
113 SDL_WindowID WindowID;
114 SDL_Renderer* Renderer;
115 Uint64 Time;
116 char* ClipboardTextData;
117 char BackendPlatformName[48];
118 bool UseVulkan;
119 bool WantUpdateMonitors;
120
121 // IME handling
122 SDL_Window* ImeWindow;
123
124 // Mouse handling
125 Uint32 MouseWindowID;
126 int MouseButtonsDown;
127 SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
128 SDL_Cursor* MouseLastCursor;
129 int MousePendingLeaveFrame;
130 bool MouseCanUseGlobalState;
131 bool MouseCanUseCapture;
132 bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
133
134 // Gamepad handling
135 ImVector<SDL_Gamepad*> Gamepads;
136 ImGui_ImplSDL3_GamepadMode GamepadMode;
137 bool WantUpdateGamepadsList;
138
139 ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
140 };
141
142 // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
143 // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
144 // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
145 // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
146 static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData()
147 {
148 return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
149 }
150
151 // Forward Declarations
152 static void ImGui_ImplSDL3_UpdateMonitors();
153 static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context);
154 static void ImGui_ImplSDL3_ShutdownMultiViewportSupport();
155
156 // Functions
157 static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*)
158 {
159 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
160 if (bd->ClipboardTextData)
161 SDL_free(bd->ClipboardTextData);
162 bd->ClipboardTextData = SDL_GetClipboardText();
163 return bd->ClipboardTextData;
164 }
165
166 static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text)
167 {
168 SDL_SetClipboardText(text);
169 }
170
171 static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
172 {
173 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
174 SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
175 SDL_Window* window = SDL_GetWindowFromID(window_id);
176 if ((!(data->WantVisible || data->WantTextInput) || bd->ImeWindow != window) && bd->ImeWindow != nullptr)
177 {
178 SDL_StopTextInput(bd->ImeWindow);
179 bd->ImeWindow = nullptr;
180 }
181 if (data->WantVisible)
182 {
183 SDL_Rect r;
184 r.x = (int)(data->InputPos.x - viewport->Pos.x);
185 r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
186 r.w = 1;
187 r.h = (int)data->InputLineHeight;
188 SDL_SetTextInputArea(window, &r, 0);
189 bd->ImeWindow = window;
190 }
191 if (!SDL_TextInputActive(window) && (data->WantVisible || data->WantTextInput))
192 SDL_StartTextInput(window);
193 }
194
195 // Not static to allow third-party code to use that if they want to (but undocumented)
196 ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
197 ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
198 {
199 // Keypad doesn't have individual key values in SDL3
200 switch (scancode)
201 {
202 case SDL_SCANCODE_KP_0: return ImGuiKey_Keypad0;
203 case SDL_SCANCODE_KP_1: return ImGuiKey_Keypad1;
204 case SDL_SCANCODE_KP_2: return ImGuiKey_Keypad2;
205 case SDL_SCANCODE_KP_3: return ImGuiKey_Keypad3;
206 case SDL_SCANCODE_KP_4: return ImGuiKey_Keypad4;
207 case SDL_SCANCODE_KP_5: return ImGuiKey_Keypad5;
208 case SDL_SCANCODE_KP_6: return ImGuiKey_Keypad6;
209 case SDL_SCANCODE_KP_7: return ImGuiKey_Keypad7;
210 case SDL_SCANCODE_KP_8: return ImGuiKey_Keypad8;
211 case SDL_SCANCODE_KP_9: return ImGuiKey_Keypad9;
212 case SDL_SCANCODE_KP_PERIOD: return ImGuiKey_KeypadDecimal;
213 case SDL_SCANCODE_KP_DIVIDE: return ImGuiKey_KeypadDivide;
214 case SDL_SCANCODE_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
215 case SDL_SCANCODE_KP_MINUS: return ImGuiKey_KeypadSubtract;
216 case SDL_SCANCODE_KP_PLUS: return ImGuiKey_KeypadAdd;
217 case SDL_SCANCODE_KP_ENTER: return ImGuiKey_KeypadEnter;
218 case SDL_SCANCODE_KP_EQUALS: return ImGuiKey_KeypadEqual;
219 default: break;
220 }
221 switch (keycode)
222 {
223 case SDLK_TAB: return ImGuiKey_Tab;
224 case SDLK_LEFT: return ImGuiKey_LeftArrow;
225 case SDLK_RIGHT: return ImGuiKey_RightArrow;
226 case SDLK_UP: return ImGuiKey_UpArrow;
227 case SDLK_DOWN: return ImGuiKey_DownArrow;
228 case SDLK_PAGEUP: return ImGuiKey_PageUp;
229 case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
230 case SDLK_HOME: return ImGuiKey_Home;
231 case SDLK_END: return ImGuiKey_End;
232 case SDLK_INSERT: return ImGuiKey_Insert;
233 case SDLK_DELETE: return ImGuiKey_Delete;
234 case SDLK_BACKSPACE: return ImGuiKey_Backspace;
235 case SDLK_SPACE: return ImGuiKey_Space;
236 case SDLK_RETURN: return ImGuiKey_Enter;
237 case SDLK_ESCAPE: return ImGuiKey_Escape;
238 //case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe;
239 case SDLK_COMMA: return ImGuiKey_Comma;
240 //case SDLK_MINUS: return ImGuiKey_Minus;
241 case SDLK_PERIOD: return ImGuiKey_Period;
242 //case SDLK_SLASH: return ImGuiKey_Slash;
243 case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
244 //case SDLK_EQUALS: return ImGuiKey_Equal;
245 //case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
246 //case SDLK_BACKSLASH: return ImGuiKey_Backslash;
247 //case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
248 //case SDLK_GRAVE: return ImGuiKey_GraveAccent;
249 case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
250 case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
251 case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
252 case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
253 case SDLK_PAUSE: return ImGuiKey_Pause;
254 case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
255 case SDLK_LSHIFT: return ImGuiKey_LeftShift;
256 case SDLK_LALT: return ImGuiKey_LeftAlt;
257 case SDLK_LGUI: return ImGuiKey_LeftSuper;
258 case SDLK_RCTRL: return ImGuiKey_RightCtrl;
259 case SDLK_RSHIFT: return ImGuiKey_RightShift;
260 case SDLK_RALT: return ImGuiKey_RightAlt;
261 case SDLK_RGUI: return ImGuiKey_RightSuper;
262 case SDLK_APPLICATION: return ImGuiKey_Menu;
263 case SDLK_0: return ImGuiKey_0;
264 case SDLK_1: return ImGuiKey_1;
265 case SDLK_2: return ImGuiKey_2;
266 case SDLK_3: return ImGuiKey_3;
267 case SDLK_4: return ImGuiKey_4;
268 case SDLK_5: return ImGuiKey_5;
269 case SDLK_6: return ImGuiKey_6;
270 case SDLK_7: return ImGuiKey_7;
271 case SDLK_8: return ImGuiKey_8;
272 case SDLK_9: return ImGuiKey_9;
273 case SDLK_A: return ImGuiKey_A;
274 case SDLK_B: return ImGuiKey_B;
275 case SDLK_C: return ImGuiKey_C;
276 case SDLK_D: return ImGuiKey_D;
277 case SDLK_E: return ImGuiKey_E;
278 case SDLK_F: return ImGuiKey_F;
279 case SDLK_G: return ImGuiKey_G;
280 case SDLK_H: return ImGuiKey_H;
281 case SDLK_I: return ImGuiKey_I;
282 case SDLK_J: return ImGuiKey_J;
283 case SDLK_K: return ImGuiKey_K;
284 case SDLK_L: return ImGuiKey_L;
285 case SDLK_M: return ImGuiKey_M;
286 case SDLK_N: return ImGuiKey_N;
287 case SDLK_O: return ImGuiKey_O;
288 case SDLK_P: return ImGuiKey_P;
289 case SDLK_Q: return ImGuiKey_Q;
290 case SDLK_R: return ImGuiKey_R;
291 case SDLK_S: return ImGuiKey_S;
292 case SDLK_T: return ImGuiKey_T;
293 case SDLK_U: return ImGuiKey_U;
294 case SDLK_V: return ImGuiKey_V;
295 case SDLK_W: return ImGuiKey_W;
296 case SDLK_X: return ImGuiKey_X;
297 case SDLK_Y: return ImGuiKey_Y;
298 case SDLK_Z: return ImGuiKey_Z;
299 case SDLK_F1: return ImGuiKey_F1;
300 case SDLK_F2: return ImGuiKey_F2;
301 case SDLK_F3: return ImGuiKey_F3;
302 case SDLK_F4: return ImGuiKey_F4;
303 case SDLK_F5: return ImGuiKey_F5;
304 case SDLK_F6: return ImGuiKey_F6;
305 case SDLK_F7: return ImGuiKey_F7;
306 case SDLK_F8: return ImGuiKey_F8;
307 case SDLK_F9: return ImGuiKey_F9;
308 case SDLK_F10: return ImGuiKey_F10;
309 case SDLK_F11: return ImGuiKey_F11;
310 case SDLK_F12: return ImGuiKey_F12;
311 case SDLK_F13: return ImGuiKey_F13;
312 case SDLK_F14: return ImGuiKey_F14;
313 case SDLK_F15: return ImGuiKey_F15;
314 case SDLK_F16: return ImGuiKey_F16;
315 case SDLK_F17: return ImGuiKey_F17;
316 case SDLK_F18: return ImGuiKey_F18;
317 case SDLK_F19: return ImGuiKey_F19;
318 case SDLK_F20: return ImGuiKey_F20;
319 case SDLK_F21: return ImGuiKey_F21;
320 case SDLK_F22: return ImGuiKey_F22;
321 case SDLK_F23: return ImGuiKey_F23;
322 case SDLK_F24: return ImGuiKey_F24;
323 case SDLK_AC_BACK: return ImGuiKey_AppBack;
324 case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
325 default: break;
326 }
327
328 // Fallback to scancode
329 switch (scancode)
330 {
331 case SDL_SCANCODE_GRAVE: return ImGuiKey_GraveAccent;
332 case SDL_SCANCODE_MINUS: return ImGuiKey_Minus;
333 case SDL_SCANCODE_EQUALS: return ImGuiKey_Equal;
334 case SDL_SCANCODE_LEFTBRACKET: return ImGuiKey_LeftBracket;
335 case SDL_SCANCODE_RIGHTBRACKET: return ImGuiKey_RightBracket;
336 case SDL_SCANCODE_NONUSBACKSLASH: return ImGuiKey_Oem102;
337 case SDL_SCANCODE_BACKSLASH: return ImGuiKey_Backslash;
338 case SDL_SCANCODE_SEMICOLON: return ImGuiKey_Semicolon;
339 case SDL_SCANCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
340 case SDL_SCANCODE_COMMA: return ImGuiKey_Comma;
341 case SDL_SCANCODE_PERIOD: return ImGuiKey_Period;
342 case SDL_SCANCODE_SLASH: return ImGuiKey_Slash;
343 default: break;
344 }
345 return ImGuiKey_None;
346 }
347
348 static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
349 {
350 ImGuiIO& io = ImGui::GetIO();
351 io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0);
352 io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0);
353 io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0);
354 io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
355 }
356
357 static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id)
358 {
359 return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id);
360 }
361
362 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
363 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
364 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
365 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
366 bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
367 {
368 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
369 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
370 ImGuiIO& io = ImGui::GetIO();
371
372 switch (event->type)
373 {
374 case SDL_EVENT_MOUSE_MOTION:
375 {
376 if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == nullptr)
377 return false;
378 ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
379 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
380 {
381 int window_x, window_y;
382 SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, &window_y);
383 mouse_pos.x += window_x;
384 mouse_pos.y += window_y;
385 }
386 io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
387 io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
388 return true;
389 }
390 case SDL_EVENT_MOUSE_WHEEL:
391 {
392 if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == nullptr)
393 return false;
394 //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
395 float wheel_x = -event->wheel.x;
396 float wheel_y = event->wheel.y;
397 io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
398 io.AddMouseWheelEvent(wheel_x, wheel_y);
399 return true;
400 }
401 case SDL_EVENT_MOUSE_BUTTON_DOWN:
402 case SDL_EVENT_MOUSE_BUTTON_UP:
403 {
404 if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == nullptr)
405 return false;
406 int mouse_button = -1;
407 if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
408 if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
409 if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
410 if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
411 if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
412 if (mouse_button == -1)
413 break;
414 io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
415 io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN));
416 bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
417 return true;
418 }
419 case SDL_EVENT_TEXT_INPUT:
420 {
421 if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == nullptr)
422 return false;
423 io.AddInputCharactersUTF8(event->text.text);
424 return true;
425 }
426 case SDL_EVENT_KEY_DOWN:
427 case SDL_EVENT_KEY_UP:
428 {
429 ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID);
430 if (viewport == nullptr)
431 return false;
432 //IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%s : key=0x%08X ('%s'), scancode=%d ('%s'), mod=%X, windowID=%d, viewport=%08X\n",
433 // (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP ", event->key.key, SDL_GetKeyName(event->key.key), event->key.scancode, SDL_GetScancodeName(event->key.scancode), event->key.mod, event->key.windowID, viewport ? viewport->ID : 0);
434 ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
435 ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
436 io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
437 io.SetKeyEventNativeData(key, (int)event->key.key, (int)event->key.scancode, (int)event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
438 return true;
439 }
440 case SDL_EVENT_DISPLAY_ORIENTATION:
441 case SDL_EVENT_DISPLAY_ADDED:
442 case SDL_EVENT_DISPLAY_REMOVED:
443 case SDL_EVENT_DISPLAY_MOVED:
444 case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
445 {
446 bd->WantUpdateMonitors = true;
447 return true;
448 }
449 case SDL_EVENT_WINDOW_MOUSE_ENTER:
450 {
451 if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
452 return false;
453 bd->MouseWindowID = event->window.windowID;
454 bd->MousePendingLeaveFrame = 0;
455 return true;
456 }
457 // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
458 // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
459 // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
460 // FIXME: Unconfirmed whether this is still needed with SDL3.
461 case SDL_EVENT_WINDOW_MOUSE_LEAVE:
462 {
463 if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
464 return false;
465 bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1;
466 return true;
467 }
468 case SDL_EVENT_WINDOW_FOCUS_GAINED:
469 case SDL_EVENT_WINDOW_FOCUS_LOST:
470 {
471 ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID);
472 if (viewport == nullptr)
473 return false;
474 //IMGUI_DEBUG_LOG("%s: windowId %d, viewport: %08X\n", (event->type == SDL_EVENT_WINDOW_FOCUS_GAINED) ? "SDL_EVENT_WINDOW_FOCUS_GAINED" : "SDL_WINDOWEVENT_FOCUS_LOST", event->window.windowID, viewport ? viewport->ID : 0);
475 io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
476 return true;
477 }
478 case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
479 case SDL_EVENT_WINDOW_MOVED:
480 case SDL_EVENT_WINDOW_RESIZED:
481 {
482 ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID);
483 if (viewport == NULL)
484 return false;
485 if (event->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)
486 viewport->PlatformRequestClose = true;
487 if (event->type == SDL_EVENT_WINDOW_MOVED)
488 viewport->PlatformRequestMove = true;
489 if (event->type == SDL_EVENT_WINDOW_RESIZED)
490 viewport->PlatformRequestResize = true;
491 return true;
492 }
493 case SDL_EVENT_GAMEPAD_ADDED:
494 case SDL_EVENT_GAMEPAD_REMOVED:
495 {
496 bd->WantUpdateGamepadsList = true;
497 return true;
498 }
499 default:
500 break;
501 }
502 return false;
503 }
504
505 static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
506 {
507 viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window);
508 viewport->PlatformHandleRaw = nullptr;
509 #if defined(_WIN32) && !defined(__WINRT__)
510 viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
511 #elif defined(__APPLE__)
512 viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
513 #endif
514 }
515
516 static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
517 {
518 ImGuiIO& io = ImGui::GetIO();
519 IMGUI_CHECKVERSION();
520 IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
521 IM_UNUSED(sdl_gl_context); // Unused in this branch
522 //SDL_SetHint(SDL_HINT_EVENT_LOGGING, "2");
523
524 const int ver_linked = SDL_GetVersion();
525
526 // Setup backend capabilities flags
527 ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
528 snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl3 (%d.%d.%d; %d.%d.%d)",
529 SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION, SDL_VERSIONNUM_MAJOR(ver_linked), SDL_VERSIONNUM_MINOR(ver_linked), SDL_VERSIONNUM_MICRO(ver_linked));
530 io.BackendPlatformUserData = (void*)bd;
531 io.BackendPlatformName = bd->BackendPlatformName;
532 io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
533 io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
534 // (ImGuiBackendFlags_PlatformHasViewports may be set just below)
535
536 bd->Window = window;
537 bd->WindowID = SDL_GetWindowID(window);
538 bd->Renderer = renderer;
539
540 // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
541 // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
542 #ifndef __APPLE__
543 bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
544 #else
545 bd->MouseCanReportHoveredViewport = false;
546 #endif
547
548 // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
549 // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
550 bd->MouseCanUseGlobalState = false;
551 bd->MouseCanUseCapture = false;
552 #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
553 const char* sdl_backend = SDL_GetCurrentVideoDriver();
554 const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
555 for (const char* item : capture_and_global_state_whitelist)
556 if (strncmp(sdl_backend, item, strlen(item)) == 0)
557 bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
558 #endif
559 if (bd->MouseCanUseGlobalState)
560 io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
561
562 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
563 platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
564 platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
565 platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData;
566 platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { return SDL_OpenURL(url) == 0; };
567
568 // Update monitor a first time during init
569 ImGui_ImplSDL3_UpdateMonitors();
570
571 // Gamepad handling
572 bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
573 bd->WantUpdateGamepadsList = true;
574
575 // Load mouse cursors
576 bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
577 bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
578 bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE);
579 bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE);
580 bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE);
581 bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE);
582 bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE);
583 bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
584 bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT);
585 bd->MouseCursors[ImGuiMouseCursor_Progress] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_PROGRESS);
586 bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED);
587
588 // Set platform dependent data in viewport
589 // Our mouse update function expect PlatformHandle to be filled for the main viewport
590 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
591 ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window);
592
593 // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
594 // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
595 // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
596 // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
597 // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_EVENT_WINDOW_FOCUS_GAINED)
598 SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
599
600 // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
601 SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
602
603 // SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659
604 SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
605
606 // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
607 // We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
608 if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports)
609 ImGui_ImplSDL3_InitMultiViewportSupport(window, sdl_gl_context);
610
611 return true;
612 }
613
614 // Should technically be a SDL_GLContext but due to typedef it is sane to keep it void* in public interface.
615 bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
616 {
617 return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context);
618 }
619
620 bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window)
621 {
622 if (!ImGui_ImplSDL3_Init(window, nullptr, nullptr))
623 return false;
624 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
625 bd->UseVulkan = true;
626 return true;
627 }
628
629 bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window)
630 {
631 #if !defined(_WIN32)
632 IM_ASSERT(0 && "Unsupported");
633 #endif
634 return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
635 }
636
637 bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window)
638 {
639 return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
640 }
641
642 bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
643 {
644 return ImGui_ImplSDL3_Init(window, renderer, nullptr);
645 }
646
647 bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window)
648 {
649 return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
650 }
651
652 bool ImGui_ImplSDL3_InitForOther(SDL_Window* window)
653 {
654 return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
655 }
656
657 static void ImGui_ImplSDL3_CloseGamepads();
658
659 void ImGui_ImplSDL3_Shutdown()
660 {
661 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
662 IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
663 ImGuiIO& io = ImGui::GetIO();
664
665 ImGui_ImplSDL3_ShutdownMultiViewportSupport();
666
667 if (bd->ClipboardTextData)
668 SDL_free(bd->ClipboardTextData);
669 for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
670 SDL_DestroyCursor(bd->MouseCursors[cursor_n]);
671 ImGui_ImplSDL3_CloseGamepads();
672
673 io.BackendPlatformName = nullptr;
674 io.BackendPlatformUserData = nullptr;
675 io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
676 IM_DELETE(bd);
677 }
678
679 // This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4.
680 static void ImGui_ImplSDL3_UpdateMouseData()
681 {
682 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
683 ImGuiIO& io = ImGui::GetIO();
684
685 // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
686 #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
687 // - 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.
688 // - 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.
689 if (bd->MouseCanUseCapture)
690 {
691 bool want_capture = false;
692 for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
693 if (ImGui::IsMouseDragging(button_n, 1.0f))
694 want_capture = true;
695 SDL_CaptureMouse(want_capture);
696 }
697
698 SDL_Window* focused_window = SDL_GetKeyboardFocus();
699 const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
700 #else
701 SDL_Window* focused_window = bd->Window;
702 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
703 #endif
704 if (is_app_focused)
705 {
706 // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
707 if (io.WantSetMousePos)
708 {
709 #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
710 if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
711 SDL_WarpMouseGlobal(io.MousePos.x, io.MousePos.y);
712 else
713 #endif
714 SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
715 }
716
717 // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
718 const bool is_relative_mouse_mode = SDL_GetWindowRelativeMouseMode(bd->Window);
719 if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
720 {
721 // 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)
722 // 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)
723 float mouse_x, mouse_y;
724 int window_x, window_y;
725 SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
726 if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
727 {
728 SDL_GetWindowPosition(focused_window, &window_x, &window_y);
729 mouse_x -= window_x;
730 mouse_y -= window_y;
731 }
732 io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
733 }
734 }
735
736 // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
737 // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
738 // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag.
739 // 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
740 // 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
741 // by the backend, and use its flawed heuristic to guess the viewport behind.
742 // - [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).
743 if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
744 {
745 ImGuiID mouse_viewport_id = 0;
746 if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL3_GetViewportForWindowID(bd->MouseWindowID))
747 mouse_viewport_id = mouse_viewport->ID;
748 io.AddMouseViewportEvent(mouse_viewport_id);
749 }
750 }
751
752 static void ImGui_ImplSDL3_UpdateMouseCursor()
753 {
754 ImGuiIO& io = ImGui::GetIO();
755 if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
756 return;
757 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
758
759 ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
760 if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
761 {
762 // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
763 SDL_HideCursor();
764 }
765 else
766 {
767 // Show OS mouse cursor
768 SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
769 if (bd->MouseLastCursor != expected_cursor)
770 {
771 SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
772 bd->MouseLastCursor = expected_cursor;
773 }
774 SDL_ShowCursor();
775 }
776 }
777
778 static void ImGui_ImplSDL3_CloseGamepads()
779 {
780 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
781 if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
782 for (SDL_Gamepad* gamepad : bd->Gamepads)
783 SDL_CloseGamepad(gamepad);
784 bd->Gamepads.resize(0);
785 }
786
787 void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count)
788 {
789 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
790 ImGui_ImplSDL3_CloseGamepads();
791 if (mode == ImGui_ImplSDL3_GamepadMode_Manual)
792 {
793 IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0);
794 for (int n = 0; n < manual_gamepads_count; n++)
795 bd->Gamepads.push_back(manual_gamepads_array[n]);
796 }
797 else
798 {
799 IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
800 bd->WantUpdateGamepadsList = true;
801 }
802 bd->GamepadMode = mode;
803 }
804
805 static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no)
806 {
807 bool merged_value = false;
808 for (SDL_Gamepad* gamepad : bd->Gamepads)
809 merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0;
810 io.AddKeyEvent(key, merged_value);
811 }
812
813 static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
814 static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1)
815 {
816 float merged_value = 0.0f;
817 for (SDL_Gamepad* gamepad : bd->Gamepads)
818 {
819 float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
820 if (merged_value < vn)
821 merged_value = vn;
822 }
823 io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
824 }
825
826 static void ImGui_ImplSDL3_UpdateGamepads()
827 {
828 ImGuiIO& io = ImGui::GetIO();
829 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
830
831 // Update list of gamepads to use
832 if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
833 {
834 ImGui_ImplSDL3_CloseGamepads();
835 int sdl_gamepads_count = 0;
836 SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count);
837 for (int n = 0; n < sdl_gamepads_count; n++)
838 if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n]))
839 {
840 bd->Gamepads.push_back(gamepad);
841 if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst)
842 break;
843 }
844 bd->WantUpdateGamepadsList = false;
845 SDL_free(sdl_gamepads);
846 }
847
848 io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
849 if (bd->Gamepads.Size == 0)
850 return;
851 io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
852
853 // Update gamepad inputs
854 const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value.
855 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
856 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
857 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
858 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
859 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
860 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
861 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
862 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
863 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
864 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
865 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
866 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
867 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
868 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
869 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
870 ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
871 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768);
872 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767);
873 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768);
874 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767);
875 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768);
876 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767);
877 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768);
878 ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767);
879 }
880
881 static void ImGui_ImplSDL3_UpdateMonitors()
882 {
883 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
884 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
885 platform_io.Monitors.resize(0);
886 bd->WantUpdateMonitors = false;
887
888 int display_count;
889 SDL_DisplayID* displays = SDL_GetDisplays(&display_count);
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 SDL_DisplayID display_id = displays[n];
894 ImGuiPlatformMonitor monitor;
895 SDL_Rect r;
896 SDL_GetDisplayBounds(display_id, &r);
897 monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
898 monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
899 if (SDL_GetDisplayUsableBounds(display_id, &r) && 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 monitor.DpiScale = SDL_GetDisplayContentScale(display_id); // See https://wiki.libsdl.org/SDL3/README-highdpi for details.
905 monitor.PlatformHandle = (void*)(intptr_t)n;
906 if (monitor.DpiScale <= 0.0f)
907 continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
908 platform_io.Monitors.push_back(monitor);
909 }
910 SDL_free(displays);
911 }
912
913 static void ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(SDL_Window* window, ImVec2* out_size, ImVec2* out_framebuffer_scale)
914 {
915 int w, h;
916 int display_w, display_h;
917 SDL_GetWindowSize(window, &w, &h);
918 if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
919 w = h = 0;
920 SDL_GetWindowSizeInPixels(window, &display_w, &display_h);
921 if (out_size != nullptr)
922 *out_size = ImVec2((float)w, (float)h);
923 if (out_framebuffer_scale != nullptr)
924 *out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f);
925 }
926
927 void ImGui_ImplSDL3_NewFrame()
928 {
929 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
930 IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
931 ImGuiIO& io = ImGui::GetIO();
932
933 // Setup main viewport size (every frame to accommodate for window resizing)
934 ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(bd->Window, &io.DisplaySize, &io.DisplayFramebufferScale);
935
936 // Update monitors
937 #ifdef WIN32
938 bd->WantUpdateMonitors = true; // Keep polling under Windows to handle changes of work area when resizing task-bar (#8415)
939 #endif
940 if (bd->WantUpdateMonitors)
941 ImGui_ImplSDL3_UpdateMonitors();
942
943 // Setup time step (we could also use SDL_GetTicksNS() available since SDL3)
944 // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
945 static Uint64 frequency = SDL_GetPerformanceFrequency();
946 Uint64 current_time = SDL_GetPerformanceCounter();
947 if (current_time <= bd->Time)
948 current_time = bd->Time + 1;
949 io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
950 bd->Time = current_time;
951
952 if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
953 {
954 bd->MouseWindowID = 0;
955 bd->MousePendingLeaveFrame = 0;
956 io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
957 }
958
959 // Our io.AddMouseViewportEvent() calls will only be valid when not capturing.
960 // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects.
961 if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr)
962 io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
963 else
964 io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
965
966 ImGui_ImplSDL3_UpdateMouseData();
967 ImGui_ImplSDL3_UpdateMouseCursor();
968
969 // Update game controllers (if enabled and available)
970 ImGui_ImplSDL3_UpdateGamepads();
971 }
972
973 //--------------------------------------------------------------------------------------------------------
974 // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
975 // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
976 // 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..
977 //--------------------------------------------------------------------------------------------------------
978
979 // Helper structure we store in the void* PlatformUserData field of each ImGuiViewport to easily retrieve our backend data.
980 struct ImGui_ImplSDL3_ViewportData
981 {
982 SDL_Window* Window;
983 SDL_Window* ParentWindow;
984 Uint32 WindowID; // Stored in ImGuiViewport::PlatformHandle. Use SDL_GetWindowFromID() to get SDL_Window* from Uint32 WindowID.
985 bool WindowOwned;
986 SDL_GLContext GLContext;
987
988 ImGui_ImplSDL3_ViewportData() { Window = ParentWindow = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; }
989 ~ImGui_ImplSDL3_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); }
990 };
991
992 static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewportID(ImGuiID viewport_id)
993 {
994 if (viewport_id != 0)
995 if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id))
996 {
997 SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
998 return SDL_GetWindowFromID(window_id);
999 }
1000 return nullptr;
1001 }
1002
1003 static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport)
1004 {
1005 ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
1006 ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
1007 viewport->PlatformUserData = vd;
1008
1009 vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
1010
1011 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
1012 ImGui_ImplSDL3_ViewportData* main_viewport_data = (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData;
1013
1014 // Share GL resources with main context
1015 bool use_opengl = (main_viewport_data->GLContext != nullptr);
1016 SDL_GLContext backup_context = nullptr;
1017 if (use_opengl)
1018 {
1019 backup_context = SDL_GL_GetCurrentContext();
1020 SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
1021 SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
1022 }
1023
1024 SDL_WindowFlags sdl_flags = 0;
1025 sdl_flags |= SDL_WINDOW_HIDDEN;
1026 sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0);
1027 sdl_flags |= SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_HIGH_PIXEL_DENSITY;
1028 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
1029 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
1030 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_UTILITY : 0;
1031 sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
1032 vd->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags);
1033 #ifndef __APPLE__ // On Mac, SDL3 Parenting appears to prevent viewport from appearing in another monitor
1034 SDL_SetWindowParent(vd->Window, vd->ParentWindow);
1035 #endif
1036 SDL_SetWindowPosition(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y);
1037 vd->WindowOwned = true;
1038 if (use_opengl)
1039 {
1040 vd->GLContext = SDL_GL_CreateContext(vd->Window);
1041 SDL_GL_SetSwapInterval(0);
1042 }
1043 if (use_opengl && backup_context)
1044 SDL_GL_MakeCurrent(vd->Window, backup_context);
1045
1046 ImGui_ImplSDL3_SetupPlatformHandles(viewport, vd->Window);
1047 }
1048
1049 static void ImGui_ImplSDL3_DestroyWindow(ImGuiViewport* viewport)
1050 {
1051 if (ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData)
1052 {
1053 if (vd->GLContext && vd->WindowOwned)
1054 SDL_GL_DestroyContext(vd->GLContext);
1055 if (vd->Window && vd->WindowOwned)
1056 SDL_DestroyWindow(vd->Window);
1057 vd->GLContext = nullptr;
1058 vd->Window = nullptr;
1059 IM_DELETE(vd);
1060 }
1061 viewport->PlatformUserData = viewport->PlatformHandle = nullptr;
1062 }
1063
1064 static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport)
1065 {
1066 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1067 #if defined(_WIN32) && !(defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES)))
1068 HWND hwnd = (HWND)viewport->PlatformHandleRaw;
1069
1070 // SDL hack: Show icon in task bar (#7989)
1071 // Note: SDL_WINDOW_UTILITY can be used to control task bar visibility, but on Windows, it does not affect child windows.
1072 if (!(viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon))
1073 {
1074 LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
1075 ex_style |= WS_EX_APPWINDOW;
1076 ex_style &= ~WS_EX_TOOLWINDOW;
1077 ::ShowWindow(hwnd, SW_HIDE);
1078 ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
1079 }
1080 #endif
1081
1082 #ifdef __APPLE__
1083 SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, "1"); // Otherwise new window appear under
1084 #else
1085 SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "0" : "1");
1086 #endif
1087 SDL_ShowWindow(vd->Window);
1088 }
1089
1090 static void ImGui_ImplSDL3_UpdateWindow(ImGuiViewport* viewport)
1091 {
1092 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1093 IM_UNUSED(vd);
1094
1095 #ifndef __APPLE__ // On Mac, SDL3 Parenting appears to prevent viewport from appearing in another monitor
1096 // Update SDL3 parent if it changed _after_ creation.
1097 // This is for advanced apps that are manipulating ParentViewportID manually.
1098 SDL_Window* new_parent = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
1099 if (new_parent != vd->ParentWindow)
1100 {
1101 vd->ParentWindow = new_parent;
1102 SDL_SetWindowParent(vd->Window, vd->ParentWindow);
1103 }
1104 #endif
1105 }
1106
1107 static ImVec2 ImGui_ImplSDL3_GetWindowPos(ImGuiViewport* viewport)
1108 {
1109 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1110 int x = 0, y = 0;
1111 SDL_GetWindowPosition(vd->Window, &x, &y);
1112 return ImVec2((float)x, (float)y);
1113 }
1114
1115 static void ImGui_ImplSDL3_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
1116 {
1117 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1118 SDL_SetWindowPosition(vd->Window, (int)pos.x, (int)pos.y);
1119 }
1120
1121 static ImVec2 ImGui_ImplSDL3_GetWindowSize(ImGuiViewport* viewport)
1122 {
1123 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1124 int w = 0, h = 0;
1125 SDL_GetWindowSize(vd->Window, &w, &h);
1126 return ImVec2((float)w, (float)h);
1127 }
1128
1129 static void ImGui_ImplSDL3_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
1130 {
1131 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1132 SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y);
1133 }
1134
1135 static ImVec2 ImGui_ImplSDL3_GetWindowFramebufferScale(ImGuiViewport* viewport)
1136 {
1137 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1138 ImVec2 framebuffer_scale;
1139 ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(vd->Window, nullptr, &framebuffer_scale);
1140 return framebuffer_scale;
1141 }
1142
1143 static void ImGui_ImplSDL3_SetWindowTitle(ImGuiViewport* viewport, const char* title)
1144 {
1145 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1146 SDL_SetWindowTitle(vd->Window, title);
1147 }
1148
1149 static void ImGui_ImplSDL3_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
1150 {
1151 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1152 SDL_SetWindowOpacity(vd->Window, alpha);
1153 }
1154
1155 static void ImGui_ImplSDL3_SetWindowFocus(ImGuiViewport* viewport)
1156 {
1157 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1158 SDL_RaiseWindow(vd->Window);
1159 }
1160
1161 static bool ImGui_ImplSDL3_GetWindowFocus(ImGuiViewport* viewport)
1162 {
1163 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1164 return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
1165 }
1166
1167 static bool ImGui_ImplSDL3_GetWindowMinimized(ImGuiViewport* viewport)
1168 {
1169 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1170 return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0;
1171 }
1172
1173 static void ImGui_ImplSDL3_RenderWindow(ImGuiViewport* viewport, void*)
1174 {
1175 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1176 if (vd->GLContext)
1177 SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
1178 }
1179
1180 static void ImGui_ImplSDL3_SwapBuffers(ImGuiViewport* viewport, void*)
1181 {
1182 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1183 if (vd->GLContext)
1184 {
1185 SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
1186 SDL_GL_SwapWindow(vd->Window);
1187 }
1188 }
1189
1190 // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
1191 // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
1192 #include <SDL3/SDL_vulkan.h>
1193 static int ImGui_ImplSDL3_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
1194 {
1195 ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
1196 (void)vk_allocator;
1197 bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
1198 return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
1199 }
1200
1201 static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context)
1202 {
1203 // Register platform interface (will be coupled with a renderer interface)
1204 ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
1205 platform_io.Platform_CreateWindow = ImGui_ImplSDL3_CreateWindow;
1206 platform_io.Platform_DestroyWindow = ImGui_ImplSDL3_DestroyWindow;
1207 platform_io.Platform_ShowWindow = ImGui_ImplSDL3_ShowWindow;
1208 platform_io.Platform_UpdateWindow = ImGui_ImplSDL3_UpdateWindow;
1209 platform_io.Platform_SetWindowPos = ImGui_ImplSDL3_SetWindowPos;
1210 platform_io.Platform_GetWindowPos = ImGui_ImplSDL3_GetWindowPos;
1211 platform_io.Platform_SetWindowSize = ImGui_ImplSDL3_SetWindowSize;
1212 platform_io.Platform_GetWindowSize = ImGui_ImplSDL3_GetWindowSize;
1213 platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplSDL3_GetWindowFramebufferScale;
1214 platform_io.Platform_SetWindowFocus = ImGui_ImplSDL3_SetWindowFocus;
1215 platform_io.Platform_GetWindowFocus = ImGui_ImplSDL3_GetWindowFocus;
1216 platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL3_GetWindowMinimized;
1217 platform_io.Platform_SetWindowTitle = ImGui_ImplSDL3_SetWindowTitle;
1218 platform_io.Platform_RenderWindow = ImGui_ImplSDL3_RenderWindow;
1219 platform_io.Platform_SwapBuffers = ImGui_ImplSDL3_SwapBuffers;
1220 platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL3_SetWindowAlpha;
1221 platform_io.Platform_CreateVkSurface = ImGui_ImplSDL3_CreateVkSurface;
1222
1223 // Register main window handle (which is owned by the main application, not by us)
1224 // 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.
1225 ImGuiViewport* main_viewport = ImGui::GetMainViewport();
1226 ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
1227 vd->Window = window;
1228 vd->WindowID = SDL_GetWindowID(window);
1229 vd->WindowOwned = false;
1230 vd->GLContext = (SDL_GLContext)sdl_gl_context;
1231 main_viewport->PlatformUserData = vd;
1232 main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID;
1233 }
1234
1235 static void ImGui_ImplSDL3_ShutdownMultiViewportSupport()
1236 {
1237 ImGui::DestroyPlatformWindows();
1238 }
1239
1240 //-----------------------------------------------------------------------------
1241
1242 #if defined(__clang__)
1243 #pragma clang diagnostic pop
1244 #endif
1245
1246 #endif // #ifndef IMGUI_DISABLE