imgui_impl_sdl3.cpp 56 KB

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