imgui_impl_sdl3.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. // 2024-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_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);
  372. ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod);
  373. ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode);
  374. io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
  375. 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.
  376. return true;
  377. }
  378. case SDL_EVENT_DISPLAY_ORIENTATION:
  379. case SDL_EVENT_DISPLAY_ADDED:
  380. case SDL_EVENT_DISPLAY_REMOVED:
  381. case SDL_EVENT_DISPLAY_MOVED:
  382. case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED:
  383. {
  384. bd->WantUpdateMonitors = true;
  385. return true;
  386. }
  387. case SDL_EVENT_WINDOW_MOUSE_ENTER:
  388. {
  389. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
  390. return false;
  391. bd->MouseWindowID = event->window.windowID;
  392. bd->MousePendingLeaveFrame = 0;
  393. return true;
  394. }
  395. // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
  396. // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
  397. // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
  398. // FIXME: Unconfirmed whether this is still needed with SDL3.
  399. case SDL_EVENT_WINDOW_MOUSE_LEAVE:
  400. {
  401. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
  402. return false;
  403. bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1;
  404. return true;
  405. }
  406. case SDL_EVENT_WINDOW_FOCUS_GAINED:
  407. case SDL_EVENT_WINDOW_FOCUS_LOST:
  408. {
  409. if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr)
  410. return false;
  411. io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED);
  412. return true;
  413. }
  414. case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
  415. case SDL_EVENT_WINDOW_MOVED:
  416. case SDL_EVENT_WINDOW_RESIZED:
  417. {
  418. ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID);
  419. if (viewport == NULL)
  420. return false;
  421. if (event->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED)
  422. viewport->PlatformRequestClose = true;
  423. if (event->type == SDL_EVENT_WINDOW_MOVED)
  424. viewport->PlatformRequestMove = true;
  425. if (event->type == SDL_EVENT_WINDOW_RESIZED)
  426. viewport->PlatformRequestResize = true;
  427. return true;
  428. }
  429. case SDL_EVENT_GAMEPAD_ADDED:
  430. case SDL_EVENT_GAMEPAD_REMOVED:
  431. {
  432. bd->WantUpdateGamepadsList = true;
  433. return true;
  434. }
  435. }
  436. return false;
  437. }
  438. static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
  439. {
  440. viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window);
  441. viewport->PlatformHandleRaw = nullptr;
  442. #if defined(_WIN32) && !defined(__WINRT__)
  443. viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
  444. #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
  445. viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
  446. #endif
  447. }
  448. static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
  449. {
  450. ImGuiIO& io = ImGui::GetIO();
  451. IMGUI_CHECKVERSION();
  452. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  453. IM_UNUSED(sdl_gl_context); // Unused in this branch
  454. // Check and store if we are on a SDL backend that supports global mouse position
  455. // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
  456. bool mouse_can_use_global_state = false;
  457. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  458. const char* sdl_backend = SDL_GetCurrentVideoDriver();
  459. const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
  460. for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
  461. if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
  462. mouse_can_use_global_state = true;
  463. #endif
  464. // Setup backend capabilities flags
  465. ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
  466. io.BackendPlatformUserData = (void*)bd;
  467. io.BackendPlatformName = "imgui_impl_sdl3";
  468. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  469. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  470. if (mouse_can_use_global_state)
  471. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  472. bd->Window = window;
  473. bd->WindowID = SDL_GetWindowID(window);
  474. bd->Renderer = renderer;
  475. // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960)
  476. // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame.
  477. bd->MouseCanUseGlobalState = mouse_can_use_global_state;
  478. #ifndef __APPLE__
  479. bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState;
  480. #else
  481. bd->MouseCanReportHoveredViewport = false;
  482. #endif
  483. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  484. platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
  485. platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
  486. platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData;
  487. // Update monitor a first time during init
  488. ImGui_ImplSDL3_UpdateMonitors();
  489. // Gamepad handling
  490. bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst;
  491. bd->WantUpdateGamepadsList = true;
  492. // Load mouse cursors
  493. bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
  494. bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT);
  495. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE);
  496. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE);
  497. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE);
  498. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE);
  499. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE);
  500. bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER);
  501. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED);
  502. // Set platform dependent data in viewport
  503. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  504. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  505. ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window);
  506. // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
  507. // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
  508. // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
  509. // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
  510. // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
  511. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  512. // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
  513. SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
  514. // SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659
  515. SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
  516. // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports.
  517. // We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings.
  518. if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports)
  519. ImGui_ImplSDL3_InitMultiViewportSupport(window, sdl_gl_context);
  520. return true;
  521. }
  522. // Should technically be a SDL_GLContext but due to typedef it is sane to keep it void* in public interface.
  523. bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  524. {
  525. return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context);
  526. }
  527. bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window)
  528. {
  529. if (!ImGui_ImplSDL3_Init(window, nullptr, nullptr))
  530. return false;
  531. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  532. bd->UseVulkan = true;
  533. return true;
  534. }
  535. bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window)
  536. {
  537. #if !defined(_WIN32)
  538. IM_ASSERT(0 && "Unsupported");
  539. #endif
  540. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  541. }
  542. bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window)
  543. {
  544. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  545. }
  546. bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
  547. {
  548. return ImGui_ImplSDL3_Init(window, renderer, nullptr);
  549. }
  550. bool ImGui_ImplSDL3_InitForOther(SDL_Window* window)
  551. {
  552. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  553. }
  554. static void ImGui_ImplSDL3_CloseGamepads();
  555. void ImGui_ImplSDL3_Shutdown()
  556. {
  557. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  558. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  559. ImGuiIO& io = ImGui::GetIO();
  560. ImGui_ImplSDL3_ShutdownMultiViewportSupport();
  561. if (bd->ClipboardTextData)
  562. SDL_free(bd->ClipboardTextData);
  563. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  564. SDL_DestroyCursor(bd->MouseCursors[cursor_n]);
  565. ImGui_ImplSDL3_CloseGamepads();
  566. io.BackendPlatformName = nullptr;
  567. io.BackendPlatformUserData = nullptr;
  568. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport);
  569. IM_DELETE(bd);
  570. }
  571. // This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4.
  572. static void ImGui_ImplSDL3_UpdateMouseData()
  573. {
  574. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  575. ImGuiIO& io = ImGui::GetIO();
  576. // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
  577. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  578. // 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
  579. SDL_CaptureMouse(bd->MouseButtonsDown != 0);
  580. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  581. const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL));
  582. #else
  583. SDL_Window* focused_window = bd->Window;
  584. 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
  585. #endif
  586. if (is_app_focused)
  587. {
  588. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
  589. if (io.WantSetMousePos)
  590. {
  591. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  592. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  593. SDL_WarpMouseGlobal(io.MousePos.x, io.MousePos.y);
  594. else
  595. #endif
  596. SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
  597. }
  598. // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
  599. if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0)
  600. {
  601. // 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)
  602. // 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)
  603. float mouse_x, mouse_y;
  604. int window_x, window_y;
  605. SDL_GetGlobalMouseState(&mouse_x, &mouse_y);
  606. if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable))
  607. {
  608. SDL_GetWindowPosition(focused_window, &window_x, &window_y);
  609. mouse_x -= window_x;
  610. mouse_y -= window_y;
  611. }
  612. io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
  613. }
  614. }
  615. // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
  616. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
  617. // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag.
  618. // 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
  619. // 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
  620. // by the backend, and use its flawed heuristic to guess the viewport behind.
  621. // - [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).
  622. if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
  623. {
  624. ImGuiID mouse_viewport_id = 0;
  625. if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL3_GetViewportForWindowID(bd->MouseWindowID))
  626. mouse_viewport_id = mouse_viewport->ID;
  627. io.AddMouseViewportEvent(mouse_viewport_id);
  628. }
  629. }
  630. static void ImGui_ImplSDL3_UpdateMouseCursor()
  631. {
  632. ImGuiIO& io = ImGui::GetIO();
  633. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  634. return;
  635. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  636. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  637. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  638. {
  639. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  640. SDL_HideCursor();
  641. }
  642. else
  643. {
  644. // Show OS mouse cursor
  645. SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
  646. if (bd->MouseLastCursor != expected_cursor)
  647. {
  648. SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
  649. bd->MouseLastCursor = expected_cursor;
  650. }
  651. SDL_ShowCursor();
  652. }
  653. }
  654. static void ImGui_ImplSDL3_CloseGamepads()
  655. {
  656. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  657. if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
  658. for (SDL_Gamepad* gamepad : bd->Gamepads)
  659. SDL_CloseGamepad(gamepad);
  660. bd->Gamepads.resize(0);
  661. }
  662. void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count)
  663. {
  664. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  665. ImGui_ImplSDL3_CloseGamepads();
  666. if (mode == ImGui_ImplSDL3_GamepadMode_Manual)
  667. {
  668. IM_ASSERT(manual_gamepads_array != nullptr && manual_gamepads_count > 0);
  669. for (int n = 0; n < manual_gamepads_count; n++)
  670. bd->Gamepads.push_back(manual_gamepads_array[n]);
  671. }
  672. else
  673. {
  674. IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
  675. bd->WantUpdateGamepadsList = true;
  676. }
  677. bd->GamepadMode = mode;
  678. }
  679. static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no)
  680. {
  681. bool merged_value = false;
  682. for (SDL_Gamepad* gamepad : bd->Gamepads)
  683. merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0;
  684. io.AddKeyEvent(key, merged_value);
  685. }
  686. static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
  687. static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1)
  688. {
  689. float merged_value = 0.0f;
  690. for (SDL_Gamepad* gamepad : bd->Gamepads)
  691. {
  692. float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
  693. if (merged_value < vn)
  694. merged_value = vn;
  695. }
  696. io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
  697. }
  698. static void ImGui_ImplSDL3_UpdateGamepads()
  699. {
  700. ImGuiIO& io = ImGui::GetIO();
  701. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  702. // Update list of gamepads to use
  703. if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual)
  704. {
  705. ImGui_ImplSDL3_CloseGamepads();
  706. int sdl_gamepads_count = 0;
  707. SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count);
  708. for (int n = 0; n < sdl_gamepads_count; n++)
  709. if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n]))
  710. {
  711. bd->Gamepads.push_back(gamepad);
  712. if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst)
  713. break;
  714. }
  715. bd->WantUpdateGamepadsList = false;
  716. SDL_free(sdl_gamepads);
  717. }
  718. // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  719. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
  720. return;
  721. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  722. if (bd->Gamepads.Size == 0)
  723. return;
  724. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  725. // Update gamepad inputs
  726. const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value.
  727. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
  728. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
  729. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
  730. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
  731. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
  732. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
  733. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
  734. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
  735. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
  736. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
  737. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
  738. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
  739. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
  740. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
  741. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
  742. ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
  743. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768);
  744. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767);
  745. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768);
  746. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767);
  747. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768);
  748. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767);
  749. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768);
  750. ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767);
  751. }
  752. static void ImGui_ImplSDL3_UpdateMonitors()
  753. {
  754. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  755. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  756. platform_io.Monitors.resize(0);
  757. bd->WantUpdateMonitors = false;
  758. int display_count;
  759. SDL_DisplayID* displays = SDL_GetDisplays(&display_count);
  760. for (int n = 0; n < display_count; n++)
  761. {
  762. // 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.
  763. SDL_DisplayID display_id = displays[n];
  764. ImGuiPlatformMonitor monitor;
  765. SDL_Rect r;
  766. SDL_GetDisplayBounds(display_id, &r);
  767. monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
  768. monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
  769. SDL_GetDisplayUsableBounds(display_id, &r);
  770. monitor.WorkPos = ImVec2((float)r.x, (float)r.y);
  771. monitor.WorkSize = ImVec2((float)r.w, (float)r.h);
  772. // FIXME-VIEWPORT: On MacOS SDL reports actual monitor DPI scale, ignoring OS configuration. We may want to set
  773. // DpiScale to cocoa_window.backingScaleFactor here.
  774. monitor.DpiScale = SDL_GetDisplayContentScale(display_id);
  775. monitor.PlatformHandle = (void*)(intptr_t)n;
  776. if (monitor.DpiScale <= 0.0f)
  777. continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
  778. platform_io.Monitors.push_back(monitor);
  779. }
  780. SDL_free(displays);
  781. }
  782. void ImGui_ImplSDL3_NewFrame()
  783. {
  784. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  785. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?");
  786. ImGuiIO& io = ImGui::GetIO();
  787. // Setup display size (every frame to accommodate for window resizing)
  788. int w, h;
  789. int display_w, display_h;
  790. SDL_GetWindowSize(bd->Window, &w, &h);
  791. if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
  792. w = h = 0;
  793. SDL_GetWindowSizeInPixels(bd->Window, &display_w, &display_h);
  794. io.DisplaySize = ImVec2((float)w, (float)h);
  795. if (w > 0 && h > 0)
  796. io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
  797. // Update monitors
  798. if (bd->WantUpdateMonitors)
  799. ImGui_ImplSDL3_UpdateMonitors();
  800. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  801. // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
  802. static Uint64 frequency = SDL_GetPerformanceFrequency();
  803. Uint64 current_time = SDL_GetPerformanceCounter();
  804. if (current_time <= bd->Time)
  805. current_time = bd->Time + 1;
  806. io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
  807. bd->Time = current_time;
  808. if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
  809. {
  810. bd->MouseWindowID = 0;
  811. bd->MousePendingLeaveFrame = 0;
  812. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  813. }
  814. // Our io.AddMouseViewportEvent() calls will only be valid when not capturing.
  815. // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects.
  816. if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr)
  817. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
  818. else
  819. io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport;
  820. ImGui_ImplSDL3_UpdateMouseData();
  821. ImGui_ImplSDL3_UpdateMouseCursor();
  822. // Update game controllers (if enabled and available)
  823. ImGui_ImplSDL3_UpdateGamepads();
  824. }
  825. //--------------------------------------------------------------------------------------------------------
  826. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  827. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  828. // 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..
  829. //--------------------------------------------------------------------------------------------------------
  830. // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data.
  831. struct ImGui_ImplSDL3_ViewportData
  832. {
  833. SDL_Window* Window;
  834. SDL_Window* ParentWindow;
  835. Uint32 WindowID;
  836. bool WindowOwned;
  837. SDL_GLContext GLContext;
  838. ImGui_ImplSDL3_ViewportData() { Window = ParentWindow = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; }
  839. ~ImGui_ImplSDL3_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); }
  840. };
  841. static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewportID(ImGuiID viewport_id)
  842. {
  843. if (viewport_id != 0)
  844. if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id))
  845. {
  846. SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle;
  847. return SDL_GetWindowFromID(window_id);
  848. }
  849. return nullptr;
  850. }
  851. static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport)
  852. {
  853. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  854. ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
  855. viewport->PlatformUserData = vd;
  856. vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
  857. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  858. ImGui_ImplSDL3_ViewportData* main_viewport_data = (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData;
  859. // Share GL resources with main context
  860. bool use_opengl = (main_viewport_data->GLContext != nullptr);
  861. SDL_GLContext backup_context = nullptr;
  862. if (use_opengl)
  863. {
  864. backup_context = SDL_GL_GetCurrentContext();
  865. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  866. SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext);
  867. }
  868. SDL_WindowFlags sdl_flags = 0;
  869. sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0);
  870. sdl_flags |= SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_HIGH_PIXEL_DENSITY;
  871. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0;
  872. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE;
  873. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_UTILITY : 0;
  874. sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0;
  875. vd->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags);
  876. SDL_SetWindowParent(vd->Window, vd->ParentWindow);
  877. SDL_SetWindowPosition(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y);
  878. vd->WindowOwned = true;
  879. if (use_opengl)
  880. {
  881. vd->GLContext = SDL_GL_CreateContext(vd->Window);
  882. SDL_GL_SetSwapInterval(0);
  883. }
  884. if (use_opengl && backup_context)
  885. SDL_GL_MakeCurrent(vd->Window, backup_context);
  886. ImGui_ImplSDL3_SetupPlatformHandles(viewport, vd->Window);
  887. }
  888. static void ImGui_ImplSDL3_DestroyWindow(ImGuiViewport* viewport)
  889. {
  890. if (ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData)
  891. {
  892. if (vd->GLContext && vd->WindowOwned)
  893. SDL_GL_DestroyContext(vd->GLContext);
  894. if (vd->Window && vd->WindowOwned)
  895. SDL_DestroyWindow(vd->Window);
  896. vd->GLContext = nullptr;
  897. vd->Window = nullptr;
  898. IM_DELETE(vd);
  899. }
  900. viewport->PlatformUserData = viewport->PlatformHandle = nullptr;
  901. }
  902. static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport)
  903. {
  904. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  905. #if defined(_WIN32) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || WINAPI_FAMILY == WINAPI_FAMILY_GAMES))
  906. HWND hwnd = (HWND)viewport->PlatformHandleRaw;
  907. // SDL hack: Show icon in task bar (#7989)
  908. // Note: SDL_WINDOW_UTILITY can be used to control task bar visibility, but on Windows, it does not affect child windows.
  909. if (!(viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon))
  910. {
  911. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  912. ex_style |= WS_EX_APPWINDOW;
  913. ex_style &= ~WS_EX_TOOLWINDOW;
  914. ::ShowWindow(hwnd, SW_HIDE);
  915. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  916. }
  917. #endif
  918. SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "0" : "1");
  919. SDL_ShowWindow(vd->Window);
  920. }
  921. static void ImGui_ImplSDL3_UpdateWindow(ImGuiViewport* viewport)
  922. {
  923. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  924. // Update SDL3 parent if it changed _after_ creation.
  925. // This is for advanced apps that are manipulating ParentViewportID manually.
  926. SDL_Window* new_parent = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId);
  927. if (new_parent != vd->ParentWindow)
  928. {
  929. vd->ParentWindow = new_parent;
  930. SDL_SetWindowParent(vd->Window, vd->ParentWindow);
  931. }
  932. }
  933. static ImVec2 ImGui_ImplSDL3_GetWindowPos(ImGuiViewport* viewport)
  934. {
  935. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  936. int x = 0, y = 0;
  937. SDL_GetWindowPosition(vd->Window, &x, &y);
  938. return ImVec2((float)x, (float)y);
  939. }
  940. static void ImGui_ImplSDL3_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  941. {
  942. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  943. SDL_SetWindowPosition(vd->Window, (int)pos.x, (int)pos.y);
  944. }
  945. static ImVec2 ImGui_ImplSDL3_GetWindowSize(ImGuiViewport* viewport)
  946. {
  947. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  948. int w = 0, h = 0;
  949. SDL_GetWindowSize(vd->Window, &w, &h);
  950. return ImVec2((float)w, (float)h);
  951. }
  952. static void ImGui_ImplSDL3_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  953. {
  954. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  955. SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y);
  956. }
  957. static void ImGui_ImplSDL3_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  958. {
  959. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  960. SDL_SetWindowTitle(vd->Window, title);
  961. }
  962. static void ImGui_ImplSDL3_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  963. {
  964. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  965. SDL_SetWindowOpacity(vd->Window, alpha);
  966. }
  967. static void ImGui_ImplSDL3_SetWindowFocus(ImGuiViewport* viewport)
  968. {
  969. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  970. SDL_RaiseWindow(vd->Window);
  971. }
  972. static bool ImGui_ImplSDL3_GetWindowFocus(ImGuiViewport* viewport)
  973. {
  974. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  975. return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0;
  976. }
  977. static bool ImGui_ImplSDL3_GetWindowMinimized(ImGuiViewport* viewport)
  978. {
  979. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  980. return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0;
  981. }
  982. static void ImGui_ImplSDL3_RenderWindow(ImGuiViewport* viewport, void*)
  983. {
  984. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  985. if (vd->GLContext)
  986. SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
  987. }
  988. static void ImGui_ImplSDL3_SwapBuffers(ImGuiViewport* viewport, void*)
  989. {
  990. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  991. if (vd->GLContext)
  992. {
  993. SDL_GL_MakeCurrent(vd->Window, vd->GLContext);
  994. SDL_GL_SwapWindow(vd->Window);
  995. }
  996. }
  997. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  998. // SDL is graceful enough to _not_ need <vulkan/vulkan.h> so we can safely include this.
  999. #include <SDL3/SDL_vulkan.h>
  1000. static int ImGui_ImplSDL3_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  1001. {
  1002. ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData;
  1003. (void)vk_allocator;
  1004. bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
  1005. return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY
  1006. }
  1007. static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context)
  1008. {
  1009. // Register platform interface (will be coupled with a renderer interface)
  1010. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  1011. platform_io.Platform_CreateWindow = ImGui_ImplSDL3_CreateWindow;
  1012. platform_io.Platform_DestroyWindow = ImGui_ImplSDL3_DestroyWindow;
  1013. platform_io.Platform_ShowWindow = ImGui_ImplSDL3_ShowWindow;
  1014. platform_io.Platform_UpdateWindow = ImGui_ImplSDL3_UpdateWindow;
  1015. platform_io.Platform_SetWindowPos = ImGui_ImplSDL3_SetWindowPos;
  1016. platform_io.Platform_GetWindowPos = ImGui_ImplSDL3_GetWindowPos;
  1017. platform_io.Platform_SetWindowSize = ImGui_ImplSDL3_SetWindowSize;
  1018. platform_io.Platform_GetWindowSize = ImGui_ImplSDL3_GetWindowSize;
  1019. platform_io.Platform_SetWindowFocus = ImGui_ImplSDL3_SetWindowFocus;
  1020. platform_io.Platform_GetWindowFocus = ImGui_ImplSDL3_GetWindowFocus;
  1021. platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL3_GetWindowMinimized;
  1022. platform_io.Platform_SetWindowTitle = ImGui_ImplSDL3_SetWindowTitle;
  1023. platform_io.Platform_RenderWindow = ImGui_ImplSDL3_RenderWindow;
  1024. platform_io.Platform_SwapBuffers = ImGui_ImplSDL3_SwapBuffers;
  1025. platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL3_SetWindowAlpha;
  1026. platform_io.Platform_CreateVkSurface = ImGui_ImplSDL3_CreateVkSurface;
  1027. // Register main window handle (which is owned by the main application, not by us)
  1028. // 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.
  1029. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  1030. ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)();
  1031. vd->Window = window;
  1032. vd->WindowID = SDL_GetWindowID(window);
  1033. vd->WindowOwned = false;
  1034. vd->GLContext = (SDL_GLContext)sdl_gl_context;
  1035. main_viewport->PlatformUserData = vd;
  1036. main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID;
  1037. }
  1038. static void ImGui_ImplSDL3_ShutdownMultiViewportSupport()
  1039. {
  1040. ImGui::DestroyPlatformWindows();
  1041. }
  1042. //-----------------------------------------------------------------------------
  1043. #if defined(__clang__)
  1044. #pragma clang diagnostic pop
  1045. #endif
  1046. #endif // #ifndef IMGUI_DISABLE