imgui_impl_sdl3.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. // dear imgui: Platform Backend for SDL3 (*EXPERIMENTAL*)
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
  4. // (**IMPORTANT: SDL 3.0.0 is NOT YET RELEASED AND CURRENTLY HAS A FAST CHANGING API. THIS CODE BREAKS OFTEN AS SDL3 CHANGES.**)
  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. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  10. // [X] Platform: Mouse cursor shape and visibility. 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. // Issues:
  13. // [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows).
  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. // 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  24. // 2024-09-11: (Docking) Added support for viewport->ParentViewportId field to support parenting at OS level. (#7973)
  25. // 2024-10-24: Emscripten: SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f on Emscripten.
  26. // 2024-09-03: Update for SDL3 api changes: SDL_GetGamepads() memory ownership revert. (#7918, #7898, #7807)
  27. // 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
  28. // - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
  29. // - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
  30. // - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
  31. // 2024-08-19: Storing SDL_WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
  32. // 2024-08-19: ImGui_ImplSDL3_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
  33. // 2024-07-22: Update for SDL3 api changes: SDL_GetGamepads() memory ownership change. (#7807)
  34. // 2024-07-18: Update for SDL3 api changes: SDL_GetClipboardText() memory ownership change. (#7801)
  35. // 2024-07-15: Update for SDL3 api changes: SDL_GetProperty() change to SDL_GetPointerProperty(). (#7794)
  36. // 2024-07-02: Update for SDL3 api changes: SDLK_x renames and SDLK_KP_x removals (#7761, #7762).
  37. // 2024-07-01: Update for SDL3 api changes: SDL_SetTextInputRect() changed to SDL_SetTextInputArea().
  38. // 2024-06-26: Update for SDL3 api changes: SDL_StartTextInput()/SDL_StopTextInput()/SDL_SetTextInputRect() functions signatures.
  39. // 2024-06-24: Update for SDL3 api changes: SDL_EVENT_KEY_DOWN/SDL_EVENT_KEY_UP contents.
  40. // 2024-06-03; Update for SDL3 api changes: SDL_SYSTEM_CURSOR_ renames.
  41. // 2024-05-15: Update for SDL3 api changes: SDLK_ renames.
  42. // 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.
  43. // 2024-02-13: Inputs: Fixed gamepad support. Handle gamepad disconnection. Added ImGui_ImplSDL3_SetGamepadMode().
  44. // 2023-11-13: Updated for recent SDL3 API changes.
  45. // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
  46. // 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391)
  47. // 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)
  48. // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
  49. // 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
  50. // 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.
  51. #include "imgui.h"
  52. #ifndef IMGUI_DISABLE
  53. #include "imgui_impl_sdl3.h"
  54. // Clang warnings with -Weverything
  55. #if defined(__clang__)
  56. #pragma clang diagnostic push
  57. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  58. #endif
  59. // SDL
  60. #include <SDL3/SDL.h>
  61. #if defined(__APPLE__)
  62. #include <TargetConditionals.h>
  63. #endif
  64. #ifdef _WIN32
  65. #ifndef WIN32_LEAN_AND_MEAN
  66. #define WIN32_LEAN_AND_MEAN
  67. #endif
  68. #include <windows.h>
  69. #endif
  70. #if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
  71. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
  72. #else
  73. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
  74. #endif
  75. // FIXME-LEGACY: remove when SDL 3.1.3 preview is released.
  76. #ifndef SDLK_APOSTROPHE
  77. #define SDLK_APOSTROPHE SDLK_QUOTE
  78. #endif
  79. #ifndef SDLK_GRAVE
  80. #define SDLK_GRAVE SDLK_BACKQUOTE
  81. #endif
  82. // SDL Data
  83. struct ImGui_ImplSDL3_Data
  84. {
  85. SDL_Window* Window;
  86. SDL_WindowID WindowID;
  87. SDL_Renderer* Renderer;
  88. Uint64 Time;
  89. char* ClipboardTextData;
  90. bool UseVulkan;
  91. bool WantUpdateMonitors;
  92. // IME handling
  93. SDL_Window* ImeWindow;
  94. // Mouse handling
  95. Uint32 MouseWindowID;
  96. int MouseButtonsDown;
  97. SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
  98. SDL_Cursor* MouseLastCursor;
  99. int MousePendingLeaveFrame;
  100. bool MouseCanUseGlobalState;
  101. bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state.
  102. // Gamepad handling
  103. ImVector<SDL_Gamepad*> Gamepads;
  104. ImGui_ImplSDL3_GamepadMode GamepadMode;
  105. bool WantUpdateGamepadsList;
  106. ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
  107. };
  108. // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
  109. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  110. // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
  111. // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
  112. static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData()
  113. {
  114. return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
  115. }
  116. // Forward Declarations
  117. static void ImGui_ImplSDL3_UpdateMonitors();
  118. static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context);
  119. static void ImGui_ImplSDL3_ShutdownMultiViewportSupport();
  120. // Functions
  121. static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*)
  122. {
  123. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  124. if (bd->ClipboardTextData)
  125. SDL_free(bd->ClipboardTextData);
  126. const char* sdl_clipboard_text = SDL_GetClipboardText();
  127. bd->ClipboardTextData = sdl_clipboard_text ? SDL_strdup(sdl_clipboard_text) : NULL;
  128. return bd->ClipboardTextData;
  129. }
  130. static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text)
  131. {
  132. SDL_SetClipboardText(text);
  133. }
  134. static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, ImGuiPlatformImeData* data)
  135. {
  136. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  137. SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
  138. SDL_Window* window = SDL_GetWindowFromID(window_id);
  139. if ((data->WantVisible == false || bd->ImeWindow != window) && bd->ImeWindow != NULL)
  140. {
  141. SDL_StopTextInput(bd->ImeWindow);
  142. bd->ImeWindow = nullptr;
  143. }
  144. if (data->WantVisible)
  145. {
  146. SDL_Rect r;
  147. r.x = (int)(data->InputPos.x - viewport->Pos.x);
  148. r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight);
  149. r.w = 1;
  150. r.h = (int)data->InputLineHeight;
  151. SDL_SetTextInputArea(window, &r, 0);
  152. SDL_StartTextInput(window);
  153. bd->ImeWindow = window;
  154. }
  155. }
  156. // Not static to allow third-party code to use that if they want to (but undocumented)
  157. ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
  158. ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
  159. {
  160. // Keypad doesn't have individual key values in SDL3
  161. switch (scancode)
  162. {
  163. case SDL_SCANCODE_KP_0: return ImGuiKey_Keypad0;
  164. case SDL_SCANCODE_KP_1: return ImGuiKey_Keypad1;
  165. case SDL_SCANCODE_KP_2: return ImGuiKey_Keypad2;
  166. case SDL_SCANCODE_KP_3: return ImGuiKey_Keypad3;
  167. case SDL_SCANCODE_KP_4: return ImGuiKey_Keypad4;
  168. case SDL_SCANCODE_KP_5: return ImGuiKey_Keypad5;
  169. case SDL_SCANCODE_KP_6: return ImGuiKey_Keypad6;
  170. case SDL_SCANCODE_KP_7: return ImGuiKey_Keypad7;
  171. case SDL_SCANCODE_KP_8: return ImGuiKey_Keypad8;
  172. case SDL_SCANCODE_KP_9: return ImGuiKey_Keypad9;
  173. case SDL_SCANCODE_KP_PERIOD: return ImGuiKey_KeypadDecimal;
  174. case SDL_SCANCODE_KP_DIVIDE: return ImGuiKey_KeypadDivide;
  175. case SDL_SCANCODE_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
  176. case SDL_SCANCODE_KP_MINUS: return ImGuiKey_KeypadSubtract;
  177. case SDL_SCANCODE_KP_PLUS: return ImGuiKey_KeypadAdd;
  178. case SDL_SCANCODE_KP_ENTER: return ImGuiKey_KeypadEnter;
  179. case SDL_SCANCODE_KP_EQUALS: return ImGuiKey_KeypadEqual;
  180. default: break;
  181. }
  182. switch (keycode)
  183. {
  184. case SDLK_TAB: return ImGuiKey_Tab;
  185. case SDLK_LEFT: return ImGuiKey_LeftArrow;
  186. case SDLK_RIGHT: return ImGuiKey_RightArrow;
  187. case SDLK_UP: return ImGuiKey_UpArrow;
  188. case SDLK_DOWN: return ImGuiKey_DownArrow;
  189. case SDLK_PAGEUP: return ImGuiKey_PageUp;
  190. case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
  191. case SDLK_HOME: return ImGuiKey_Home;
  192. case SDLK_END: return ImGuiKey_End;
  193. case SDLK_INSERT: return ImGuiKey_Insert;
  194. case SDLK_DELETE: return ImGuiKey_Delete;
  195. case SDLK_BACKSPACE: return ImGuiKey_Backspace;
  196. case SDLK_SPACE: return ImGuiKey_Space;
  197. case SDLK_RETURN: return ImGuiKey_Enter;
  198. case SDLK_ESCAPE: return ImGuiKey_Escape;
  199. case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe;
  200. case SDLK_COMMA: return ImGuiKey_Comma;
  201. case SDLK_MINUS: return ImGuiKey_Minus;
  202. case SDLK_PERIOD: return ImGuiKey_Period;
  203. case SDLK_SLASH: return ImGuiKey_Slash;
  204. case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
  205. case SDLK_EQUALS: return ImGuiKey_Equal;
  206. case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
  207. case SDLK_BACKSLASH: return ImGuiKey_Backslash;
  208. case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
  209. case SDLK_GRAVE: return ImGuiKey_GraveAccent;
  210. case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
  211. case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
  212. case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
  213. case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
  214. case SDLK_PAUSE: return ImGuiKey_Pause;
  215. case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
  216. case SDLK_LSHIFT: return ImGuiKey_LeftShift;
  217. case SDLK_LALT: return ImGuiKey_LeftAlt;
  218. case SDLK_LGUI: return ImGuiKey_LeftSuper;
  219. case SDLK_RCTRL: return ImGuiKey_RightCtrl;
  220. case SDLK_RSHIFT: return ImGuiKey_RightShift;
  221. case SDLK_RALT: return ImGuiKey_RightAlt;
  222. case SDLK_RGUI: return ImGuiKey_RightSuper;
  223. case SDLK_APPLICATION: return ImGuiKey_Menu;
  224. case SDLK_0: return ImGuiKey_0;
  225. case SDLK_1: return ImGuiKey_1;
  226. case SDLK_2: return ImGuiKey_2;
  227. case SDLK_3: return ImGuiKey_3;
  228. case SDLK_4: return ImGuiKey_4;
  229. case SDLK_5: return ImGuiKey_5;
  230. case SDLK_6: return ImGuiKey_6;
  231. case SDLK_7: return ImGuiKey_7;
  232. case SDLK_8: return ImGuiKey_8;
  233. case SDLK_9: return ImGuiKey_9;
  234. case SDLK_A: return ImGuiKey_A;
  235. case SDLK_B: return ImGuiKey_B;
  236. case SDLK_C: return ImGuiKey_C;
  237. case SDLK_D: return ImGuiKey_D;
  238. case SDLK_E: return ImGuiKey_E;
  239. case SDLK_F: return ImGuiKey_F;
  240. case SDLK_G: return ImGuiKey_G;
  241. case SDLK_H: return ImGuiKey_H;
  242. case SDLK_I: return ImGuiKey_I;
  243. case SDLK_J: return ImGuiKey_J;
  244. case SDLK_K: return ImGuiKey_K;
  245. case SDLK_L: return ImGuiKey_L;
  246. case SDLK_M: return ImGuiKey_M;
  247. case SDLK_N: return ImGuiKey_N;
  248. case SDLK_O: return ImGuiKey_O;
  249. case SDLK_P: return ImGuiKey_P;
  250. case SDLK_Q: return ImGuiKey_Q;
  251. case SDLK_R: return ImGuiKey_R;
  252. case SDLK_S: return ImGuiKey_S;
  253. case SDLK_T: return ImGuiKey_T;
  254. case SDLK_U: return ImGuiKey_U;
  255. case SDLK_V: return ImGuiKey_V;
  256. case SDLK_W: return ImGuiKey_W;
  257. case SDLK_X: return ImGuiKey_X;
  258. case SDLK_Y: return ImGuiKey_Y;
  259. case SDLK_Z: return ImGuiKey_Z;
  260. case SDLK_F1: return ImGuiKey_F1;
  261. case SDLK_F2: return ImGuiKey_F2;
  262. case SDLK_F3: return ImGuiKey_F3;
  263. case SDLK_F4: return ImGuiKey_F4;
  264. case SDLK_F5: return ImGuiKey_F5;
  265. case SDLK_F6: return ImGuiKey_F6;
  266. case SDLK_F7: return ImGuiKey_F7;
  267. case SDLK_F8: return ImGuiKey_F8;
  268. case SDLK_F9: return ImGuiKey_F9;
  269. case SDLK_F10: return ImGuiKey_F10;
  270. case SDLK_F11: return ImGuiKey_F11;
  271. case SDLK_F12: return ImGuiKey_F12;
  272. case SDLK_F13: return ImGuiKey_F13;
  273. case SDLK_F14: return ImGuiKey_F14;
  274. case SDLK_F15: return ImGuiKey_F15;
  275. case SDLK_F16: return ImGuiKey_F16;
  276. case SDLK_F17: return ImGuiKey_F17;
  277. case SDLK_F18: return ImGuiKey_F18;
  278. case SDLK_F19: return ImGuiKey_F19;
  279. case SDLK_F20: return ImGuiKey_F20;
  280. case SDLK_F21: return ImGuiKey_F21;
  281. case SDLK_F22: return ImGuiKey_F22;
  282. case SDLK_F23: return ImGuiKey_F23;
  283. case SDLK_F24: return ImGuiKey_F24;
  284. case SDLK_AC_BACK: return ImGuiKey_AppBack;
  285. case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
  286. default: break;
  287. }
  288. return ImGuiKey_None;
  289. }
  290. static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
  291. {
  292. ImGuiIO& io = ImGui::GetIO();
  293. io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0);
  294. io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0);
  295. io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0);
  296. io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
  297. }
  298. static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id)
  299. {
  300. return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id);
  301. }
  302. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  303. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  304. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  305. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  306. bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
  307. {
  308. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  309. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
  310. ImGuiIO& io = ImGui::GetIO();
  311. switch (event->type)
  312. {
  313. case SDL_EVENT_MOUSE_MOTION:
  314. {
  315. if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == NULL)
  316. return false;
  317. ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
  318. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  319. {
  320. int window_x, window_y;
  321. SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, &window_y);
  322. mouse_pos.x += window_x;
  323. mouse_pos.y += window_y;
  324. }
  325. io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  326. io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
  327. return true;
  328. }
  329. case SDL_EVENT_MOUSE_WHEEL:
  330. {
  331. if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == NULL)
  332. return false;
  333. //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
  334. float wheel_x = -event->wheel.x;
  335. float wheel_y = event->wheel.y;
  336. io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  337. io.AddMouseWheelEvent(wheel_x, wheel_y);
  338. return true;
  339. }
  340. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  341. case SDL_EVENT_MOUSE_BUTTON_UP:
  342. {
  343. if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == NULL)
  344. return false;
  345. int mouse_button = -1;
  346. if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
  347. if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
  348. if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
  349. if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
  350. if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
  351. if (mouse_button == -1)
  352. break;
  353. io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  354. io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN));
  355. bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
  356. return true;
  357. }
  358. case SDL_EVENT_TEXT_INPUT:
  359. {
  360. if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == NULL)
  361. return false;
  362. io.AddInputCharactersUTF8(event->text.text);
  363. return true;
  364. }
  365. case SDL_EVENT_KEY_DOWN:
  366. case SDL_EVENT_KEY_UP:
  367. {
  368. if (ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID) == NULL)
  369. return false;
  370. //IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%d: key=%d, scancode=%d, mod=%X\n", (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP", event->key.key, event->key.scancode, event->key.mod);
  371. ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
  372. ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
  373. io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
  374. 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.
  375. return true;
  376. }
  377. case SDL_EVENT_DISPLAY_ORIENTATION:
  378. case SDL_EVENT_DISPLAY_ADDED:
  379. case SDL_EVENT_DISPLAY_REMOVED:
  380. case SDL_EVENT_DISPLAY_MOVED:
  381. case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
  382. {
  383. bd->WantUpdateMonitors = true;
  384. return true;
  385. }
  386. case SDL_EVENT_WINDOW_MOUSE_ENTER:
  387. {
  388. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == NULL)
  389. return false;
  390. bd->MouseWindowID = event->window.windowID;
  391. bd->MousePendingLeaveFrame = 0;
  392. return true;
  393. }
  394. // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
  395. // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
  396. // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
  397. // FIXME: Unconfirmed whether this is still needed with SDL3.
  398. case SDL_EVENT_WINDOW_MOUSE_LEAVE:
  399. {
  400. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == NULL)
  401. return false;
  402. bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1;
  403. return true;
  404. }
  405. case SDL_EVENT_WINDOW_FOCUS_GAINED:
  406. case SDL_EVENT_WINDOW_FOCUS_LOST:
  407. {
  408. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == NULL)
  409. return false;
  410. io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
  411. return true;
  412. }
  413. case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
  414. case SDL_EVENT_WINDOW_MOVED:
  415. case SDL_EVENT_WINDOW_RESIZED:
  416. {
  417. ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID);
  418. if (viewport == NULL)
  419. return false;
  420. if (event->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)
  421. viewport->PlatformRequestClose = true;
  422. if (event->type == SDL_EVENT_WINDOW_MOVED)
  423. viewport->PlatformRequestMove = true;
  424. if (event->type == SDL_EVENT_WINDOW_RESIZED)
  425. viewport->PlatformRequestResize = true;
  426. return true;
  427. }
  428. case SDL_EVENT_GAMEPAD_ADDED:
  429. case SDL_EVENT_GAMEPAD_REMOVED:
  430. {
  431. bd->WantUpdateGamepadsList = true;
  432. return true;
  433. }
  434. }
  435. return false;
  436. }
  437. static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
  438. {
  439. viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window);
  440. viewport->PlatformHandleRaw = nullptr;
  441. #if defined(_WIN32) && !defined(__WINRT__)
  442. viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
  443. #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
  444. viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
  445. #endif
  446. }
  447. static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
  448. {
  449. ImGuiIO& io = ImGui::GetIO();
  450. IMGUI_CHECKVERSION();
  451. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  452. IM_UNUSED(sdl_gl_context); // Unused in this branch
  453. // Check and store if we are on a SDL backend that supports global mouse position
  454. // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
  455. bool mouse_can_use_global_state = false;
  456. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  457. const char* sdl_backend = SDL_GetCurrentVideoDriver();
  458. const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
  459. for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
  460. if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
  461. mouse_can_use_global_state = true;
  462. #endif
  463. // Setup backend capabilities flags
  464. ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
  465. io.BackendPlatformUserData = (void*)bd;
  466. io.BackendPlatformName = "imgui_impl_sdl3";
  467. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  468. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  469. if (mouse_can_use_global_state)
  470. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  471. bd->Window = window;
  472. bd->WindowID = SDL_GetWindowID(window);
  473. bd->Renderer = renderer;
  474. // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
  475. // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
  476. bd->MouseCanUseGlobalState = mouse_can_use_global_state;
  477. #ifndef __APPLE__
  478. bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
  479. #else
  480. bd->MouseCanReportHoveredViewport = false;
  481. #endif
  482. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  483. platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
  484. platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
  485. platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData;
  486. // Update monitor a first time during init
  487. ImGui_ImplSDL3_UpdateMonitors();
  488. // Gamepad handling
  489. bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
  490. bd->WantUpdateGamepadsList = true;
  491. // Load mouse cursors
  492. bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
  493. bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
  494. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE);
  495. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE);
  496. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE);
  497. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE);
  498. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE);
  499. bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
  500. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED);
  501. // Set platform dependent data in viewport
  502. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  503. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  504. ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window);
  505. // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
  506. // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
  507. // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
  508. // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
  509. // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
  510. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  511. // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
  512. SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
  513. // SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659
  514. SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
  515. // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
  516. // We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
  517. if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports)
  518. ImGui_ImplSDL3_InitMultiViewportSupport(window, sdl_gl_context);
  519. return true;
  520. }
  521. // Should technically be a SDL_GLContext but due to typedef it is sane to keep it void* in public interface.
  522. bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  523. {
  524. return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context);
  525. }
  526. bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window)
  527. {
  528. if (!ImGui_ImplSDL3_Init(window, nullptr, nullptr))
  529. return false;
  530. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  531. bd->UseVulkan = true;
  532. return true;
  533. }
  534. bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window)
  535. {
  536. #if !defined(_WIN32)
  537. IM_ASSERT(0 && "Unsupported");
  538. #endif
  539. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  540. }
  541. bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window)
  542. {
  543. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  544. }
  545. bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
  546. {
  547. return ImGui_ImplSDL3_Init(window, renderer, nullptr);
  548. }
  549. bool ImGui_ImplSDL3_InitForOther(SDL_Window* window)
  550. {
  551. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  552. }
  553. static void ImGui_ImplSDL3_CloseGamepads();
  554. void ImGui_ImplSDL3_Shutdown()
  555. {
  556. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  557. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  558. ImGuiIO& io = ImGui::GetIO();
  559. ImGui_ImplSDL3_ShutdownMultiViewportSupport();
  560. if (bd->ClipboardTextData)
  561. SDL_free(bd->ClipboardTextData);
  562. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  563. SDL_DestroyCursor(bd->MouseCursors[cursor_n]);
  564. ImGui_ImplSDL3_CloseGamepads();
  565. io.BackendPlatformName = nullptr;
  566. io.BackendPlatformUserData = nullptr;
  567. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
  568. IM_DELETE(bd);
  569. }
  570. // This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4.
  571. static void ImGui_ImplSDL3_UpdateMouseData()
  572. {
  573. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  574. ImGuiIO& io = ImGui::GetIO();
  575. // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
  576. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  577. // 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
  578. SDL_CaptureMouse(bd->MouseButtonsDown != 0);
  579. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  580. const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
  581. #else
  582. SDL_Window* focused_window = bd->Window;
  583. 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
  584. #endif
  585. if (is_app_focused)
  586. {
  587. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
  588. if (io.WantSetMousePos)
  589. {
  590. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  591. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  592. SDL_WarpMouseGlobal(io.MousePos.x, io.MousePos.y);
  593. else
  594. #endif
  595. SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
  596. }
  597. // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
  598. if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0)
  599. {
  600. // 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)
  601. // 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)
  602. float mouse_x, mouse_y;
  603. int window_x, window_y;
  604. SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
  605. if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
  606. {
  607. SDL_GetWindowPosition(focused_window, &window_x, &window_y);
  608. mouse_x -= window_x;
  609. mouse_y -= window_y;
  610. }
  611. io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
  612. }
  613. }
  614. // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
  615. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
  616. // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag.
  617. // 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
  618. // 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
  619. // by the backend, and use its flawed heuristic to guess the viewport behind.
  620. // - [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).
  621. if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
  622. {
  623. ImGuiID mouse_viewport_id = 0;
  624. if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL3_GetViewportForWindowID(bd->MouseWindowID))
  625. mouse_viewport_id = mouse_viewport->ID;
  626. io.AddMouseViewportEvent(mouse_viewport_id);
  627. }
  628. }
  629. static void ImGui_ImplSDL3_UpdateMouseCursor()
  630. {
  631. ImGuiIO& io = ImGui::GetIO();
  632. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  633. return;
  634. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  635. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  636. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  637. {
  638. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  639. SDL_HideCursor();
  640. }
  641. else
  642. {
  643. // Show OS mouse cursor
  644. SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
  645. if (bd->MouseLastCursor != expected_cursor)
  646. {
  647. SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
  648. bd->MouseLastCursor = expected_cursor;
  649. }
  650. SDL_ShowCursor();
  651. }
  652. }
  653. static void ImGui_ImplSDL3_CloseGamepads()
  654. {
  655. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  656. if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
  657. for (SDL_Gamepad* gamepad : bd->Gamepads)
  658. SDL_CloseGamepad(gamepad);
  659. bd->Gamepads.resize(0);
  660. }
  661. void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count)
  662. {
  663. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  664. ImGui_ImplSDL3_CloseGamepads();
  665. if (mode == ImGui_ImplSDL3_GamepadMode_Manual)
  666. {
  667. IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0);
  668. for (int n = 0; n < manual_gamepads_count; n++)
  669. bd->Gamepads.push_back(manual_gamepads_array[n]);
  670. }
  671. else
  672. {
  673. IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
  674. bd->WantUpdateGamepadsList = true;
  675. }
  676. bd->GamepadMode = mode;
  677. }
  678. static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no)
  679. {
  680. bool merged_value = false;
  681. for (SDL_Gamepad* gamepad : bd->Gamepads)
  682. merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0;
  683. io.AddKeyEvent(key, merged_value);
  684. }
  685. static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
  686. static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1)
  687. {
  688. float merged_value = 0.0f;
  689. for (SDL_Gamepad* gamepad : bd->Gamepads)
  690. {
  691. float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
  692. if (merged_value < vn)
  693. merged_value = vn;
  694. }
  695. io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
  696. }
  697. static void ImGui_ImplSDL3_UpdateGamepads()
  698. {
  699. ImGuiIO& io = ImGui::GetIO();
  700. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  701. // Update list of gamepads to use
  702. if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
  703. {
  704. ImGui_ImplSDL3_CloseGamepads();
  705. int sdl_gamepads_count = 0;
  706. SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count);
  707. for (int n = 0; n < sdl_gamepads_count; n++)
  708. if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n]))
  709. {
  710. bd->Gamepads.push_back(gamepad);
  711. if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst)
  712. break;
  713. }
  714. bd->WantUpdateGamepadsList = false;
  715. SDL_free(sdl_gamepads);
  716. }
  717. // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  718. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
  719. return;
  720. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  721. if (bd->Gamepads.Size == 0)
  722. return;
  723. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  724. // Update gamepad inputs
  725. const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value.
  726. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
  727. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
  728. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
  729. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
  730. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
  731. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
  732. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
  733. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
  734. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
  735. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
  736. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
  737. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
  738. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
  739. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
  740. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
  741. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
  742. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768);
  743. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767);
  744. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768);
  745. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767);
  746. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768);
  747. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767);
  748. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768);
  749. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767);
  750. }
  751. static void ImGui_ImplSDL3_UpdateMonitors()
  752. {
  753. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  754. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  755. platform_io.Monitors.resize(0);
  756. bd->WantUpdateMonitors = false;
  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. if (bd->WantUpdateMonitors)
  798. ImGui_ImplSDL3_UpdateMonitors();
  799. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  800. // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
  801. static Uint64 frequency = SDL_GetPerformanceFrequency();
  802. Uint64 current_time = SDL_GetPerformanceCounter();
  803. if (current_time <= bd->Time)
  804. current_time = bd->Time + 1;
  805. io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
  806. bd->Time = current_time;
  807. if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
  808. {
  809. bd->MouseWindowID = 0;
  810. bd->MousePendingLeaveFrame = 0;
  811. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  812. }
  813. // Our io.AddMouseViewportEvent() calls will only be valid when not capturing.
  814. // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects.
  815. if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr)
  816. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
  817. else
  818. io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
  819. ImGui_ImplSDL3_UpdateMouseData();
  820. ImGui_ImplSDL3_UpdateMouseCursor();
  821. // Update game controllers (if enabled and available)
  822. ImGui_ImplSDL3_UpdateGamepads();
  823. }
  824. //--------------------------------------------------------------------------------------------------------
  825. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  826. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  827. // 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..
  828. //--------------------------------------------------------------------------------------------------------
  829. // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
  830. struct ImGui_ImplSDL3_ViewportData
  831. {
  832. SDL_Window* Window;
  833. SDL_Window* ParentWindow;
  834. Uint32 WindowID;
  835. bool WindowOwned;
  836. SDL_GLContext GLContext;
  837. ImGui_ImplSDL3_ViewportData() { Window = ParentWindow = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; }
  838. ~ImGui_ImplSDL3_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); }
  839. };
  840. static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewportID(ImGuiID viewport_id)
  841. {
  842. if (viewport_id != 0)
  843. if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id))
  844. {
  845. SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
  846. return SDL_GetWindowFromID(window_id);
  847. }
  848. return nullptr;
  849. }
  850. static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport)
  851. {
  852. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  853. ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
  854. viewport->PlatformUserData = vd;
  855. vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
  856. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  857. ImGui_ImplSDL3_ViewportData* main_viewport_data = (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData;
  858. // Share GL resources with main context
  859. bool use_opengl = (main_viewport_data->GLContext != nullptr);
  860. SDL_GLContext backup_context = nullptr;
  861. if (use_opengl)
  862. {
  863. backup_context = SDL_GL_GetCurrentContext();
  864. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  865. SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
  866. }
  867. SDL_WindowFlags sdl_flags = 0;
  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