imgui_impl_sdl2.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. // dear imgui: Platform Backend for SDL2
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // (Info: SDL2 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.)
  4. // (Prefer SDL 2.0.5+ for full feature support.)
  5. // Implemented features:
  6. // [X] Platform: Clipboard support.
  7. // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen.
  8. // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5]
  9. // [X] Platform: Gamepad support.
  10. // [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  11. // [X] Platform: Basic IME support. App needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
  12. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  13. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  14. // Learn about Dear ImGui:
  15. // - FAQ https://dearimgui.com/faq
  16. // - Getting Started https://dearimgui.com/getting-started
  17. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  18. // - Introduction, links and more at the top of imgui.cpp
  19. // CHANGELOG
  20. // (minor and older changes stripped away, please see git history for details)
  21. // 2025-07-08: Made ImGui_ImplSDL2_GetContentScaleForWindow(), ImGui_ImplSDL2_GetContentScaleForDisplay() helpers return 1.0f on Emscripten and Android platforms, matching macOS logic. (#8742, #8733)
  22. // 2025-06-11: Added ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) and ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) helper to facilitate making DPI-aware apps.
  23. // 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561)
  24. // 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set.
  25. // 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468)
  26. // 2025-02-26: Only start SDL_CaptureMouse() when mouse is being dragged, to mitigate issues with e.g.Linux debuggers not claiming capture back. (#6410, #3650)
  27. // 2025-02-24: Avoid calling SDL_GetGlobalMouseState() when mouse is in relative mode.
  28. // 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support.
  29. // 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler.
  30. // 2025-01-20: Made ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode_Manual) accept an empty array.
  31. // 2024-10-24: Emscripten: from SDL 2.30.9, SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f.
  32. // 2024-09-09: use SDL_Vulkan_GetDrawableSize() when available. (#7967, #3190)
  33. // 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO:
  34. // - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn
  35. // - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn
  36. // - io.PlatformOpenInShellFn -> platform_io.Platform_OpenInShellFn
  37. // - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn
  38. // 2024-08-19: Storing SDL's Uint32 WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*.
  39. // 2024-08-19: ImGui_ImplSDL2_ProcessEvent() now ignores events intended for other SDL windows. (#7853)
  40. // 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
  41. // 2024-07-02: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() renaming in main library.
  42. // 2024-02-14: Inputs: Handle gamepad disconnection. Added ImGui_ImplSDL2_SetGamepadMode().
  43. // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
  44. // 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)
  45. // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
  46. // 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
  47. // 2023-02-07: Implement IME handler (io.SetPlatformImeDataFn will call SDL_SetTextInputRect()/SDL_StartTextInput()).
  48. // 2023-02-07: *BREAKING CHANGE* Renamed this backend file from imgui_impl_sdl.cpp/.h to imgui_impl_sdl2.cpp/.h in prevision for the future release of SDL3.
  49. // 2023-02-02: Avoid calling SDL_SetCursor() when cursor has not changed, as the function is surprisingly costly on Mac with latest SDL (may be fixed in next SDL version).
  50. // 2023-02-02: Added support for SDL 2.0.18+ preciseX/preciseY mouse wheel data for smooth scrolling + Scaling X value on Emscripten (bug?). (#4019, #6096)
  51. // 2023-02-02: Removed SDL_MOUSEWHEEL value clamping, as values seem correct in latest Emscripten. (#4019)
  52. // 2023-02-01: Flipping SDL_MOUSEWHEEL 'wheel.x' value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463)
  53. // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
  54. // 2022-09-26: Inputs: Disable SDL 2.0.22 new "auto capture" (SDL_HINT_MOUSE_AUTO_CAPTURE) which prevents drag and drop across windows for multi-viewport support + don't capture when drag and dropping. (#5710)
  55. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
  56. // 2022-03-22: Inputs: Fix mouse position issues when dragging outside of boundaries. SDL_CaptureMouse() erroneously still gives out LEAVE events when hovering OS decorations.
  57. // 2022-03-22: Inputs: Added support for extra mouse buttons (SDL_BUTTON_X1/SDL_BUTTON_X2).
  58. // 2022-02-04: Added SDL_Renderer* parameter to ImGui_ImplSDL2_InitForSDLRenderer(), so we can use SDL_GetRendererOutputSize() instead of SDL_GL_GetDrawableSize() when bound to a SDL_Renderer.
  59. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
  60. // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
  61. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
  62. // 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates.
  63. // 2022-01-12: Update mouse inputs using SDL_MOUSEMOTION/SDL_WINDOWEVENT_LEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API.
  64. // 2022-01-12: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted.
  65. // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
  66. // 2021-08-17: Calling io.AddFocusEvent() on SDL_WINDOWEVENT_FOCUS_GAINED/SDL_WINDOWEVENT_FOCUS_LOST.
  67. // 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using SDL_GetMouseFocus() + SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, requires SDL 2.0.5+)
  68. // 2021-06-29: *BREAKING CHANGE* Removed 'SDL_Window* window' parameter to ImGui_ImplSDL2_NewFrame() which was unnecessary.
  69. // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
  70. // 2021-03-22: Rework global mouse pos availability check listing supported platforms explicitly, effectively fixing mouse access on Raspberry Pi. (#2837, #3950)
  71. // 2020-05-25: Misc: Report a zero display-size when window is minimized, to be consistent with other backends.
  72. // 2020-02-20: Inputs: Fixed mapping for ImGuiKey_KeyPadEnter (using SDL_SCANCODE_KP_ENTER instead of SDL_SCANCODE_RETURN2).
  73. // 2019-12-17: Inputs: On Wayland, use SDL_GetMouseState (because there is no global mouse state).
  74. // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
  75. // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
  76. // 2019-04-23: Inputs: Added support for SDL_GameController (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
  77. // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
  78. // 2018-12-21: Inputs: Workaround for Android/iOS which don't seem to handle focus related calls.
  79. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  80. // 2018-11-14: Changed the signature of ImGui_ImplSDL2_ProcessEvent() to take a 'const SDL_Event*'.
  81. // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
  82. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  83. // 2018-06-08: Misc: Extracted imgui_impl_sdl.cpp/.h away from the old combined SDL2+OpenGL/Vulkan examples.
  84. // 2018-06-08: Misc: ImGui_ImplSDL2_InitForOpenGL() now takes a SDL_GLContext parameter.
  85. // 2018-05-09: Misc: Fixed clipboard paste memory leak (we didn't call SDL_FreeMemory on the data returned by SDL_GetClipboardText).
  86. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  87. // 2018-02-16: Inputs: Added support for mouse cursors, honoring ImGui::GetMouseCursor() value.
  88. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  89. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  90. // 2018-02-05: Misc: Using SDL_GetPerformanceCounter() instead of SDL_GetTicks() to be able to handle very high framerate (1000+ FPS).
  91. // 2018-02-05: Inputs: Keyboard mapping is using scancodes everywhere instead of a confusing mixture of keycodes and scancodes.
  92. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  93. // 2018-01-19: Inputs: When available (SDL 2.0.4+) using SDL_CaptureMouse() to retrieve coordinates outside of client area when dragging. Otherwise (SDL 2.0.3 and before) testing for SDL_WINDOW_INPUT_FOCUS instead of SDL_WINDOW_MOUSE_FOCUS.
  94. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  95. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  96. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  97. #include "imgui.h"
  98. #ifndef IMGUI_DISABLE
  99. #include "imgui_impl_sdl2.h"
  100. // Clang warnings with -Weverything
  101. #if defined(__clang__)
  102. #pragma clang diagnostic push
  103. #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
  104. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  105. #endif
  106. // SDL
  107. #include <SDL.h>
  108. #include <SDL_syswm.h>
  109. #include <stdio.h> // for snprintf()
  110. #ifdef __APPLE__
  111. #include <TargetConditionals.h>
  112. #endif
  113. #ifdef __EMSCRIPTEN__
  114. #include <emscripten/em_js.h>
  115. #endif
  116. #undef Status // X11 headers are leaking this.
  117. #if SDL_VERSION_ATLEAST(2,0,4) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
  118. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
  119. #else
  120. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
  121. #endif
  122. #define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4)
  123. #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
  124. #define SDL_HAS_OPEN_URL SDL_VERSION_ATLEAST(2,0,14)
  125. #if SDL_HAS_VULKAN
  126. #include <SDL_vulkan.h>
  127. #endif
  128. // SDL Data
  129. struct ImGui_ImplSDL2_Data
  130. {
  131. SDL_Window* Window;
  132. Uint32 WindowID;
  133. SDL_Renderer* Renderer;
  134. Uint64 Time;
  135. char* ClipboardTextData;
  136. char BackendPlatformName[48];
  137. // Mouse handling
  138. Uint32 MouseWindowID;
  139. int MouseButtonsDown;
  140. SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
  141. SDL_Cursor* MouseLastCursor;
  142. int MouseLastLeaveFrame;
  143. bool MouseCanUseGlobalState;
  144. bool MouseCanUseCapture;
  145. // Gamepad handling
  146. ImVector<SDL_GameController*> Gamepads;
  147. ImGui_ImplSDL2_GamepadMode GamepadMode;
  148. bool WantUpdateGamepadsList;
  149. ImGui_ImplSDL2_Data() { memset((void*)this, 0, sizeof(*this)); }
  150. };
  151. // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
  152. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  153. // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
  154. // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
  155. static ImGui_ImplSDL2_Data* ImGui_ImplSDL2_GetBackendData()
  156. {
  157. return ImGui::GetCurrentContext() ? (ImGui_ImplSDL2_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
  158. }
  159. // Functions
  160. static const char* ImGui_ImplSDL2_GetClipboardText(ImGuiContext*)
  161. {
  162. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  163. if (bd->ClipboardTextData)
  164. SDL_free(bd->ClipboardTextData);
  165. bd->ClipboardTextData = SDL_GetClipboardText();
  166. return bd->ClipboardTextData;
  167. }
  168. static void ImGui_ImplSDL2_SetClipboardText(ImGuiContext*, const char* text)
  169. {
  170. SDL_SetClipboardText(text);
  171. }
  172. // Note: native IME will only display if user calls SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1") _before_ SDL_CreateWindow().
  173. static void ImGui_ImplSDL2_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data)
  174. {
  175. if (data->WantVisible)
  176. {
  177. SDL_Rect r;
  178. r.x = (int)data->InputPos.x;
  179. r.y = (int)data->InputPos.y;
  180. r.w = 1;
  181. r.h = (int)data->InputLineHeight;
  182. SDL_SetTextInputRect(&r);
  183. }
  184. }
  185. // Not static to allow third-party code to use that if they want to (but undocumented)
  186. ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode);
  187. ImGuiKey ImGui_ImplSDL2_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode)
  188. {
  189. switch (keycode)
  190. {
  191. case SDLK_TAB: return ImGuiKey_Tab;
  192. case SDLK_LEFT: return ImGuiKey_LeftArrow;
  193. case SDLK_RIGHT: return ImGuiKey_RightArrow;
  194. case SDLK_UP: return ImGuiKey_UpArrow;
  195. case SDLK_DOWN: return ImGuiKey_DownArrow;
  196. case SDLK_PAGEUP: return ImGuiKey_PageUp;
  197. case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
  198. case SDLK_HOME: return ImGuiKey_Home;
  199. case SDLK_END: return ImGuiKey_End;
  200. case SDLK_INSERT: return ImGuiKey_Insert;
  201. case SDLK_DELETE: return ImGuiKey_Delete;
  202. case SDLK_BACKSPACE: return ImGuiKey_Backspace;
  203. case SDLK_SPACE: return ImGuiKey_Space;
  204. case SDLK_RETURN: return ImGuiKey_Enter;
  205. case SDLK_ESCAPE: return ImGuiKey_Escape;
  206. //case SDLK_QUOTE: return ImGuiKey_Apostrophe;
  207. case SDLK_COMMA: return ImGuiKey_Comma;
  208. //case SDLK_MINUS: return ImGuiKey_Minus;
  209. case SDLK_PERIOD: return ImGuiKey_Period;
  210. //case SDLK_SLASH: return ImGuiKey_Slash;
  211. case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
  212. //case SDLK_EQUALS: return ImGuiKey_Equal;
  213. //case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
  214. //case SDLK_BACKSLASH: return ImGuiKey_Backslash;
  215. //case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
  216. //case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent;
  217. case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
  218. case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
  219. case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
  220. case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
  221. case SDLK_PAUSE: return ImGuiKey_Pause;
  222. case SDLK_KP_0: return ImGuiKey_Keypad0;
  223. case SDLK_KP_1: return ImGuiKey_Keypad1;
  224. case SDLK_KP_2: return ImGuiKey_Keypad2;
  225. case SDLK_KP_3: return ImGuiKey_Keypad3;
  226. case SDLK_KP_4: return ImGuiKey_Keypad4;
  227. case SDLK_KP_5: return ImGuiKey_Keypad5;
  228. case SDLK_KP_6: return ImGuiKey_Keypad6;
  229. case SDLK_KP_7: return ImGuiKey_Keypad7;
  230. case SDLK_KP_8: return ImGuiKey_Keypad8;
  231. case SDLK_KP_9: return ImGuiKey_Keypad9;
  232. case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal;
  233. case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide;
  234. case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
  235. case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract;
  236. case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd;
  237. case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter;
  238. case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual;
  239. case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
  240. case SDLK_LSHIFT: return ImGuiKey_LeftShift;
  241. case SDLK_LALT: return ImGuiKey_LeftAlt;
  242. case SDLK_LGUI: return ImGuiKey_LeftSuper;
  243. case SDLK_RCTRL: return ImGuiKey_RightCtrl;
  244. case SDLK_RSHIFT: return ImGuiKey_RightShift;
  245. case SDLK_RALT: return ImGuiKey_RightAlt;
  246. case SDLK_RGUI: return ImGuiKey_RightSuper;
  247. case SDLK_APPLICATION: return ImGuiKey_Menu;
  248. case SDLK_0: return ImGuiKey_0;
  249. case SDLK_1: return ImGuiKey_1;
  250. case SDLK_2: return ImGuiKey_2;
  251. case SDLK_3: return ImGuiKey_3;
  252. case SDLK_4: return ImGuiKey_4;
  253. case SDLK_5: return ImGuiKey_5;
  254. case SDLK_6: return ImGuiKey_6;
  255. case SDLK_7: return ImGuiKey_7;
  256. case SDLK_8: return ImGuiKey_8;
  257. case SDLK_9: return ImGuiKey_9;
  258. case SDLK_a: return ImGuiKey_A;
  259. case SDLK_b: return ImGuiKey_B;
  260. case SDLK_c: return ImGuiKey_C;
  261. case SDLK_d: return ImGuiKey_D;
  262. case SDLK_e: return ImGuiKey_E;
  263. case SDLK_f: return ImGuiKey_F;
  264. case SDLK_g: return ImGuiKey_G;
  265. case SDLK_h: return ImGuiKey_H;
  266. case SDLK_i: return ImGuiKey_I;
  267. case SDLK_j: return ImGuiKey_J;
  268. case SDLK_k: return ImGuiKey_K;
  269. case SDLK_l: return ImGuiKey_L;
  270. case SDLK_m: return ImGuiKey_M;
  271. case SDLK_n: return ImGuiKey_N;
  272. case SDLK_o: return ImGuiKey_O;
  273. case SDLK_p: return ImGuiKey_P;
  274. case SDLK_q: return ImGuiKey_Q;
  275. case SDLK_r: return ImGuiKey_R;
  276. case SDLK_s: return ImGuiKey_S;
  277. case SDLK_t: return ImGuiKey_T;
  278. case SDLK_u: return ImGuiKey_U;
  279. case SDLK_v: return ImGuiKey_V;
  280. case SDLK_w: return ImGuiKey_W;
  281. case SDLK_x: return ImGuiKey_X;
  282. case SDLK_y: return ImGuiKey_Y;
  283. case SDLK_z: return ImGuiKey_Z;
  284. case SDLK_F1: return ImGuiKey_F1;
  285. case SDLK_F2: return ImGuiKey_F2;
  286. case SDLK_F3: return ImGuiKey_F3;
  287. case SDLK_F4: return ImGuiKey_F4;
  288. case SDLK_F5: return ImGuiKey_F5;
  289. case SDLK_F6: return ImGuiKey_F6;
  290. case SDLK_F7: return ImGuiKey_F7;
  291. case SDLK_F8: return ImGuiKey_F8;
  292. case SDLK_F9: return ImGuiKey_F9;
  293. case SDLK_F10: return ImGuiKey_F10;
  294. case SDLK_F11: return ImGuiKey_F11;
  295. case SDLK_F12: return ImGuiKey_F12;
  296. case SDLK_F13: return ImGuiKey_F13;
  297. case SDLK_F14: return ImGuiKey_F14;
  298. case SDLK_F15: return ImGuiKey_F15;
  299. case SDLK_F16: return ImGuiKey_F16;
  300. case SDLK_F17: return ImGuiKey_F17;
  301. case SDLK_F18: return ImGuiKey_F18;
  302. case SDLK_F19: return ImGuiKey_F19;
  303. case SDLK_F20: return ImGuiKey_F20;
  304. case SDLK_F21: return ImGuiKey_F21;
  305. case SDLK_F22: return ImGuiKey_F22;
  306. case SDLK_F23: return ImGuiKey_F23;
  307. case SDLK_F24: return ImGuiKey_F24;
  308. case SDLK_AC_BACK: return ImGuiKey_AppBack;
  309. case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
  310. default: break;
  311. }
  312. // Fallback to scancode
  313. switch (scancode)
  314. {
  315. case SDL_SCANCODE_GRAVE: return ImGuiKey_GraveAccent;
  316. case SDL_SCANCODE_MINUS: return ImGuiKey_Minus;
  317. case SDL_SCANCODE_EQUALS: return ImGuiKey_Equal;
  318. case SDL_SCANCODE_LEFTBRACKET: return ImGuiKey_LeftBracket;
  319. case SDL_SCANCODE_RIGHTBRACKET: return ImGuiKey_RightBracket;
  320. case SDL_SCANCODE_NONUSBACKSLASH: return ImGuiKey_Oem102;
  321. case SDL_SCANCODE_BACKSLASH: return ImGuiKey_Backslash;
  322. case SDL_SCANCODE_SEMICOLON: return ImGuiKey_Semicolon;
  323. case SDL_SCANCODE_APOSTROPHE: return ImGuiKey_Apostrophe;
  324. case SDL_SCANCODE_COMMA: return ImGuiKey_Comma;
  325. case SDL_SCANCODE_PERIOD: return ImGuiKey_Period;
  326. case SDL_SCANCODE_SLASH: return ImGuiKey_Slash;
  327. default: break;
  328. }
  329. return ImGuiKey_None;
  330. }
  331. static void ImGui_ImplSDL2_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
  332. {
  333. ImGuiIO& io = ImGui::GetIO();
  334. io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & KMOD_CTRL) != 0);
  335. io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & KMOD_SHIFT) != 0);
  336. io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & KMOD_ALT) != 0);
  337. io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & KMOD_GUI) != 0);
  338. }
  339. static ImGuiViewport* ImGui_ImplSDL2_GetViewportForWindowID(Uint32 window_id)
  340. {
  341. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  342. return (window_id == bd->WindowID) ? ImGui::GetMainViewport() : nullptr;
  343. }
  344. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  345. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  346. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  347. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  348. // If you have multiple SDL events and some of them are not meant to be used by dear imgui, you may need to filter events based on their windowID field.
  349. bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
  350. {
  351. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  352. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
  353. ImGuiIO& io = ImGui::GetIO();
  354. switch (event->type)
  355. {
  356. case SDL_MOUSEMOTION:
  357. {
  358. if (ImGui_ImplSDL2_GetViewportForWindowID(event->motion.windowID) == nullptr)
  359. return false;
  360. ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
  361. io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  362. io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
  363. return true;
  364. }
  365. case SDL_MOUSEWHEEL:
  366. {
  367. if (ImGui_ImplSDL2_GetViewportForWindowID(event->wheel.windowID) == nullptr)
  368. return false;
  369. //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
  370. #if SDL_VERSION_ATLEAST(2,0,18) // If this fails to compile on Emscripten: update to latest Emscripten!
  371. float wheel_x = -event->wheel.preciseX;
  372. float wheel_y = event->wheel.preciseY;
  373. #else
  374. float wheel_x = -(float)event->wheel.x;
  375. float wheel_y = (float)event->wheel.y;
  376. #endif
  377. #if defined(__EMSCRIPTEN__) && !SDL_VERSION_ATLEAST(2,31,0)
  378. wheel_x /= 100.0f;
  379. #endif
  380. io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  381. io.AddMouseWheelEvent(wheel_x, wheel_y);
  382. return true;
  383. }
  384. case SDL_MOUSEBUTTONDOWN:
  385. case SDL_MOUSEBUTTONUP:
  386. {
  387. if (ImGui_ImplSDL2_GetViewportForWindowID(event->button.windowID) == nullptr)
  388. return false;
  389. int mouse_button = -1;
  390. if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
  391. if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
  392. if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
  393. if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
  394. if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
  395. if (mouse_button == -1)
  396. break;
  397. io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  398. io.AddMouseButtonEvent(mouse_button, (event->type == SDL_MOUSEBUTTONDOWN));
  399. bd->MouseButtonsDown = (event->type == SDL_MOUSEBUTTONDOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
  400. return true;
  401. }
  402. case SDL_TEXTINPUT:
  403. {
  404. if (ImGui_ImplSDL2_GetViewportForWindowID(event->text.windowID) == nullptr)
  405. return false;
  406. io.AddInputCharactersUTF8(event->text.text);
  407. return true;
  408. }
  409. case SDL_KEYDOWN:
  410. case SDL_KEYUP:
  411. {
  412. if (ImGui_ImplSDL2_GetViewportForWindowID(event->key.windowID) == nullptr)
  413. return false;
  414. ImGui_ImplSDL2_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
  415. //IMGUI_DEBUG_LOG("SDL_KEY_%s : key=%d ('%s'), scancode=%d ('%s'), mod=%X\n",
  416. // (event->type == SDL_KEYDOWN) ? "DOWN" : "UP ", event->key.keysym.sym, SDL_GetKeyName(event->key.keysym.sym), event->key.keysym.scancode, SDL_GetScancodeName(event->key.keysym.scancode), event->key.keysym.mod);
  417. ImGuiKey key = ImGui_ImplSDL2_KeyEventToImGuiKey(event->key.keysym.sym, event->key.keysym.scancode);
  418. io.AddKeyEvent(key, (event->type == SDL_KEYDOWN));
  419. io.SetKeyEventNativeData(key, (int)event->key.keysym.sym, (int)event->key.keysym.scancode, (int)event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
  420. return true;
  421. }
  422. case SDL_WINDOWEVENT:
  423. {
  424. if (ImGui_ImplSDL2_GetViewportForWindowID(event->window.windowID) == nullptr)
  425. return false;
  426. // - When capturing mouse, SDL will send a bunch of conflicting LEAVE/ENTER event on every mouse move, but the final ENTER tends to be right.
  427. // - However we won't get a correct LEAVE event for a captured window.
  428. // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
  429. // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
  430. // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
  431. Uint8 window_event = event->window.event;
  432. if (window_event == SDL_WINDOWEVENT_ENTER)
  433. {
  434. bd->MouseWindowID = event->window.windowID;
  435. bd->MouseLastLeaveFrame = 0;
  436. }
  437. if (window_event == SDL_WINDOWEVENT_LEAVE)
  438. bd->MouseLastLeaveFrame = ImGui::GetFrameCount() + 1;
  439. if (window_event == SDL_WINDOWEVENT_FOCUS_GAINED)
  440. io.AddFocusEvent(true);
  441. else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST)
  442. io.AddFocusEvent(false);
  443. return true;
  444. }
  445. case SDL_CONTROLLERDEVICEADDED:
  446. case SDL_CONTROLLERDEVICEREMOVED:
  447. {
  448. bd->WantUpdateGamepadsList = true;
  449. return true;
  450. }
  451. default:
  452. break;
  453. }
  454. return false;
  455. }
  456. #ifdef __EMSCRIPTEN__
  457. EM_JS(void, ImGui_ImplSDL2_EmscriptenOpenURL, (char const* url), { url = url ? UTF8ToString(url) : null; if (url) window.open(url, '_blank'); });
  458. #endif
  459. static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
  460. {
  461. ImGuiIO& io = ImGui::GetIO();
  462. IMGUI_CHECKVERSION();
  463. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  464. // Obtain compiled and runtime versions
  465. SDL_version ver_compiled;
  466. SDL_version ver_runtime;
  467. SDL_VERSION(&ver_compiled);
  468. SDL_GetVersion(&ver_runtime);
  469. // Setup backend capabilities flags
  470. ImGui_ImplSDL2_Data* bd = IM_NEW(ImGui_ImplSDL2_Data)();
  471. snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl2 (%u.%u.%u, %u.%u.%u)",
  472. ver_compiled.major, ver_compiled.minor, ver_compiled.patch, ver_runtime.major, ver_runtime.minor, ver_runtime.patch);
  473. io.BackendPlatformUserData = (void*)bd;
  474. io.BackendPlatformName = bd->BackendPlatformName;
  475. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  476. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  477. bd->Window = window;
  478. bd->WindowID = SDL_GetWindowID(window);
  479. bd->Renderer = renderer;
  480. // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse()
  481. // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
  482. bd->MouseCanUseGlobalState = false;
  483. bd->MouseCanUseCapture = false;
  484. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  485. const char* sdl_backend = SDL_GetCurrentVideoDriver();
  486. const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
  487. for (const char* item : capture_and_global_state_whitelist)
  488. if (strncmp(sdl_backend, item, strlen(item)) == 0)
  489. bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true;
  490. #endif
  491. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  492. platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
  493. platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
  494. platform_io.Platform_ClipboardUserData = nullptr;
  495. platform_io.Platform_SetImeDataFn = ImGui_ImplSDL2_PlatformSetImeData;
  496. #ifdef __EMSCRIPTEN__
  497. platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { ImGui_ImplSDL2_EmscriptenOpenURL(url); return true; };
  498. #elif SDL_HAS_OPEN_URL
  499. platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { return SDL_OpenURL(url) == 0; };
  500. #endif
  501. // Gamepad handling
  502. bd->GamepadMode = ImGui_ImplSDL2_GamepadMode_AutoFirst;
  503. bd->WantUpdateGamepadsList = true;
  504. // Load mouse cursors
  505. bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  506. bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  507. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  508. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  509. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  510. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  511. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  512. bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
  513. bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT);
  514. bd->MouseCursors[ImGuiMouseCursor_Progress] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAITARROW);
  515. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
  516. // Set platform dependent data in viewport
  517. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  518. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  519. main_viewport->PlatformHandle = (void*)(intptr_t)bd->WindowID;
  520. main_viewport->PlatformHandleRaw = nullptr;
  521. SDL_SysWMinfo info;
  522. SDL_VERSION(&info.version);
  523. if (SDL_GetWindowWMInfo(window, &info))
  524. {
  525. #if defined(SDL_VIDEO_DRIVER_WINDOWS)
  526. main_viewport->PlatformHandleRaw = (void*)info.info.win.window;
  527. #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
  528. main_viewport->PlatformHandleRaw = (void*)info.info.cocoa.window;
  529. #endif
  530. }
  531. // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
  532. // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
  533. // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
  534. // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
  535. // you can ignore SDL_MOUSEBUTTONDOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
  536. #ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
  537. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  538. #endif
  539. // From 2.0.18: Enable native IME.
  540. // IMPORTANT: This is used at the time of SDL_CreateWindow() so this will only affects secondary windows, if any.
  541. // For the main window to be affected, your application needs to call this manually before calling SDL_CreateWindow().
  542. #ifdef SDL_HINT_IME_SHOW_UI
  543. SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
  544. #endif
  545. // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
  546. #ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
  547. SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
  548. #endif
  549. (void)sdl_gl_context; // Unused in 'master' branch.
  550. return true;
  551. }
  552. bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  553. {
  554. return ImGui_ImplSDL2_Init(window, nullptr, sdl_gl_context);
  555. }
  556. bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
  557. {
  558. #if !SDL_HAS_VULKAN
  559. IM_ASSERT(0 && "Unsupported");
  560. #endif
  561. return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
  562. }
  563. bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
  564. {
  565. #if !defined(_WIN32)
  566. IM_ASSERT(0 && "Unsupported");
  567. #endif
  568. return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
  569. }
  570. bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
  571. {
  572. return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
  573. }
  574. bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
  575. {
  576. return ImGui_ImplSDL2_Init(window, renderer, nullptr);
  577. }
  578. bool ImGui_ImplSDL2_InitForOther(SDL_Window* window)
  579. {
  580. return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
  581. }
  582. static void ImGui_ImplSDL2_CloseGamepads();
  583. void ImGui_ImplSDL2_Shutdown()
  584. {
  585. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  586. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  587. ImGuiIO& io = ImGui::GetIO();
  588. if (bd->ClipboardTextData)
  589. SDL_free(bd->ClipboardTextData);
  590. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  591. SDL_FreeCursor(bd->MouseCursors[cursor_n]);
  592. ImGui_ImplSDL2_CloseGamepads();
  593. io.BackendPlatformName = nullptr;
  594. io.BackendPlatformUserData = nullptr;
  595. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad);
  596. IM_DELETE(bd);
  597. }
  598. static void ImGui_ImplSDL2_UpdateMouseData()
  599. {
  600. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  601. ImGuiIO& io = ImGui::GetIO();
  602. // We forward mouse input when hovered or captured (via SDL_MOUSEMOTION) or when focused (below)
  603. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  604. // - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside.
  605. // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture.
  606. if (bd->MouseCanUseCapture)
  607. {
  608. bool want_capture = false;
  609. for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++)
  610. if (ImGui::IsMouseDragging(button_n, 1.0f))
  611. want_capture = true;
  612. SDL_CaptureMouse(want_capture ? SDL_TRUE : SDL_FALSE);
  613. }
  614. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  615. const bool is_app_focused = (bd->Window == focused_window);
  616. #else
  617. 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
  618. #endif
  619. if (is_app_focused)
  620. {
  621. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user)
  622. if (io.WantSetMousePos)
  623. SDL_WarpMouseInWindow(bd->Window, (int)io.MousePos.x, (int)io.MousePos.y);
  624. // (Optional) Fallback to provide mouse position when focused (SDL_MOUSEMOTION already provides this when hovered or captured)
  625. const bool is_relative_mouse_mode = SDL_GetRelativeMouseMode() != 0;
  626. if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode)
  627. {
  628. // 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)
  629. int window_x, window_y, mouse_x_global, mouse_y_global;
  630. SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
  631. SDL_GetWindowPosition(bd->Window, &window_x, &window_y);
  632. io.AddMousePosEvent((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y));
  633. }
  634. }
  635. }
  636. static void ImGui_ImplSDL2_UpdateMouseCursor()
  637. {
  638. ImGuiIO& io = ImGui::GetIO();
  639. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  640. return;
  641. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  642. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  643. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  644. {
  645. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  646. SDL_ShowCursor(SDL_FALSE);
  647. }
  648. else
  649. {
  650. // Show OS mouse cursor
  651. SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
  652. if (bd->MouseLastCursor != expected_cursor)
  653. {
  654. SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
  655. bd->MouseLastCursor = expected_cursor;
  656. }
  657. SDL_ShowCursor(SDL_TRUE);
  658. }
  659. }
  660. // - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend.
  661. // - Apple platforms use FramebufferScale so we always return 1.0f.
  662. // - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
  663. float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window)
  664. {
  665. return ImGui_ImplSDL2_GetContentScaleForDisplay(SDL_GetWindowDisplayIndex(window));
  666. }
  667. float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index)
  668. {
  669. #if SDL_HAS_PER_MONITOR_DPI
  670. #if !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
  671. float dpi = 0.0f;
  672. if (SDL_GetDisplayDPI(display_index, &dpi, nullptr, nullptr) == 0)
  673. return dpi / 96.0f;
  674. #endif
  675. #endif
  676. IM_UNUSED(display_index);
  677. return 1.0f;
  678. }
  679. static void ImGui_ImplSDL2_CloseGamepads()
  680. {
  681. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  682. if (bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
  683. for (SDL_GameController* gamepad : bd->Gamepads)
  684. SDL_GameControllerClose(gamepad);
  685. bd->Gamepads.resize(0);
  686. }
  687. void ImGui_ImplSDL2_SetGamepadMode(ImGui_ImplSDL2_GamepadMode mode, struct _SDL_GameController** manual_gamepads_array, int manual_gamepads_count)
  688. {
  689. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  690. ImGui_ImplSDL2_CloseGamepads();
  691. if (mode == ImGui_ImplSDL2_GamepadMode_Manual)
  692. {
  693. IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0);
  694. for (int n = 0; n < manual_gamepads_count; n++)
  695. bd->Gamepads.push_back(manual_gamepads_array[n]);
  696. }
  697. else
  698. {
  699. IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0);
  700. bd->WantUpdateGamepadsList = true;
  701. }
  702. bd->GamepadMode = mode;
  703. }
  704. static void ImGui_ImplSDL2_UpdateGamepadButton(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerButton button_no)
  705. {
  706. bool merged_value = false;
  707. for (SDL_GameController* gamepad : bd->Gamepads)
  708. merged_value |= SDL_GameControllerGetButton(gamepad, button_no) != 0;
  709. io.AddKeyEvent(key, merged_value);
  710. }
  711. static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
  712. static void ImGui_ImplSDL2_UpdateGamepadAnalog(ImGui_ImplSDL2_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GameControllerAxis axis_no, float v0, float v1)
  713. {
  714. float merged_value = 0.0f;
  715. for (SDL_GameController* gamepad : bd->Gamepads)
  716. {
  717. float vn = Saturate((float)(SDL_GameControllerGetAxis(gamepad, axis_no) - v0) / (float)(v1 - v0));
  718. if (merged_value < vn)
  719. merged_value = vn;
  720. }
  721. io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value);
  722. }
  723. static void ImGui_ImplSDL2_UpdateGamepads()
  724. {
  725. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  726. ImGuiIO& io = ImGui::GetIO();
  727. // Update list of controller(s) to use
  728. if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL2_GamepadMode_Manual)
  729. {
  730. ImGui_ImplSDL2_CloseGamepads();
  731. int joystick_count = SDL_NumJoysticks();
  732. for (int n = 0; n < joystick_count; n++)
  733. if (SDL_IsGameController(n))
  734. if (SDL_GameController* gamepad = SDL_GameControllerOpen(n))
  735. {
  736. bd->Gamepads.push_back(gamepad);
  737. if (bd->GamepadMode == ImGui_ImplSDL2_GamepadMode_AutoFirst)
  738. break;
  739. }
  740. bd->WantUpdateGamepadsList = false;
  741. }
  742. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  743. if (bd->Gamepads.Size == 0)
  744. return;
  745. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  746. // Update gamepad inputs
  747. const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
  748. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_CONTROLLER_BUTTON_START);
  749. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_CONTROLLER_BUTTON_BACK);
  750. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_CONTROLLER_BUTTON_X); // Xbox X, PS Square
  751. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_CONTROLLER_BUTTON_B); // Xbox B, PS Circle
  752. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_CONTROLLER_BUTTON_Y); // Xbox Y, PS Triangle
  753. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_CONTROLLER_BUTTON_A); // Xbox A, PS Cross
  754. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_CONTROLLER_BUTTON_DPAD_LEFT);
  755. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
  756. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_CONTROLLER_BUTTON_DPAD_UP);
  757. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_CONTROLLER_BUTTON_DPAD_DOWN);
  758. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
  759. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
  760. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0.0f, 32767);
  761. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0.0f, 32767);
  762. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_CONTROLLER_BUTTON_LEFTSTICK);
  763. ImGui_ImplSDL2_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_CONTROLLER_BUTTON_RIGHTSTICK);
  764. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_CONTROLLER_AXIS_LEFTX, -thumb_dead_zone, -32768);
  765. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_CONTROLLER_AXIS_LEFTX, +thumb_dead_zone, +32767);
  766. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_CONTROLLER_AXIS_LEFTY, -thumb_dead_zone, -32768);
  767. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_CONTROLLER_AXIS_LEFTY, +thumb_dead_zone, +32767);
  768. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_CONTROLLER_AXIS_RIGHTX, -thumb_dead_zone, -32768);
  769. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_CONTROLLER_AXIS_RIGHTX, +thumb_dead_zone, +32767);
  770. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_CONTROLLER_AXIS_RIGHTY, -thumb_dead_zone, -32768);
  771. ImGui_ImplSDL2_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_CONTROLLER_AXIS_RIGHTY, +thumb_dead_zone, +32767);
  772. }
  773. static void ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(SDL_Window* window, SDL_Renderer* renderer, ImVec2* out_size, ImVec2* out_framebuffer_scale)
  774. {
  775. int w, h;
  776. int display_w, display_h;
  777. SDL_GetWindowSize(window, &w, &h);
  778. if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
  779. w = h = 0;
  780. if (renderer != nullptr)
  781. SDL_GetRendererOutputSize(renderer, &display_w, &display_h);
  782. #if SDL_HAS_VULKAN
  783. else if (SDL_GetWindowFlags(window) & SDL_WINDOW_VULKAN)
  784. SDL_Vulkan_GetDrawableSize(window, &display_w, &display_h);
  785. #endif
  786. else
  787. SDL_GL_GetDrawableSize(window, &display_w, &display_h);
  788. if (out_size != nullptr)
  789. *out_size = ImVec2((float)w, (float)h);
  790. if (out_framebuffer_scale != nullptr)
  791. *out_framebuffer_scale = (w > 0 && h > 0) ? ImVec2((float)display_w / w, (float)display_h / h) : ImVec2(1.0f, 1.0f);
  792. }
  793. void ImGui_ImplSDL2_NewFrame()
  794. {
  795. ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
  796. IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL2_Init()?");
  797. ImGuiIO& io = ImGui::GetIO();
  798. // Setup main viewport size (every frame to accommodate for window resizing)
  799. ImGui_ImplSDL2_GetWindowSizeAndFramebufferScale(bd->Window, bd->Renderer, &io.DisplaySize, &io.DisplayFramebufferScale);
  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->MouseLastLeaveFrame && bd->MouseLastLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
  809. {
  810. bd->MouseWindowID = 0;
  811. bd->MouseLastLeaveFrame = 0;
  812. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  813. }
  814. ImGui_ImplSDL2_UpdateMouseData();
  815. ImGui_ImplSDL2_UpdateMouseCursor();
  816. // Update game controllers (if enabled and available)
  817. ImGui_ImplSDL2_UpdateGamepads();
  818. }
  819. //-----------------------------------------------------------------------------
  820. #if defined(__clang__)
  821. #pragma clang diagnostic pop
  822. #endif
  823. #endif // #ifndef IMGUI_DISABLE