imgui_impl_sdl3.cpp 56 KB

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