imgui_impl_sdl3.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. IT IS POSSIBLE THAT ITS SPECS/API WILL CHANGE BEFORE RELEASE)
  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 will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
  9. // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  10. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  11. // Missing features:
  12. // [x] Platform: Basic IME support. Position somehow broken in SDL3 + app needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!.
  13. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  14. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  15. // Learn about Dear ImGui:
  16. // - FAQ https://dearimgui.com/faq
  17. // - Getting Started https://dearimgui.com/getting-started
  18. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  19. // - Introduction, links and more at the top of imgui.cpp
  20. // CHANGELOG
  21. // (minor and older changes stripped away, please see git history for details)
  22. // 2023-11-13: Updated for recent SDL3 API changes.
  23. // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys.
  24. // 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391)
  25. // 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)
  26. // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702)
  27. // 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644)
  28. // 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.
  29. #include "imgui.h"
  30. #ifndef IMGUI_DISABLE
  31. #include "imgui_impl_sdl3.h"
  32. // Clang warnings with -Weverything
  33. #if defined(__clang__)
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision
  36. #endif
  37. // SDL
  38. #include <SDL3/SDL.h>
  39. #if defined(__APPLE__)
  40. #include <TargetConditionals.h>
  41. #endif
  42. #ifdef _WIN32
  43. #ifndef WIN32_LEAN_AND_MEAN
  44. #define WIN32_LEAN_AND_MEAN
  45. #endif
  46. #include <windows.h>
  47. #endif
  48. #if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__)
  49. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1
  50. #else
  51. #define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0
  52. #endif
  53. // SDL Data
  54. struct ImGui_ImplSDL3_Data
  55. {
  56. SDL_Window* Window;
  57. SDL_Renderer* Renderer;
  58. Uint64 Time;
  59. Uint32 MouseWindowID;
  60. int MouseButtonsDown;
  61. SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT];
  62. SDL_Cursor* LastMouseCursor;
  63. int PendingMouseLeaveFrame;
  64. char* ClipboardTextData;
  65. bool MouseCanUseGlobalState;
  66. ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); }
  67. };
  68. // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
  69. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  70. // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
  71. // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
  72. static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData()
  73. {
  74. return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
  75. }
  76. // Functions
  77. static const char* ImGui_ImplSDL3_GetClipboardText(void*)
  78. {
  79. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  80. if (bd->ClipboardTextData)
  81. SDL_free(bd->ClipboardTextData);
  82. bd->ClipboardTextData = SDL_GetClipboardText();
  83. return bd->ClipboardTextData;
  84. }
  85. static void ImGui_ImplSDL3_SetClipboardText(void*, const char* text)
  86. {
  87. SDL_SetClipboardText(text);
  88. }
  89. static void ImGui_ImplSDL3_SetPlatformImeData(ImGuiViewport*, ImGuiPlatformImeData* data)
  90. {
  91. if (data->WantVisible)
  92. {
  93. SDL_Rect r;
  94. r.x = (int)data->InputPos.x;
  95. r.y = (int)data->InputPos.y;
  96. r.w = 1;
  97. r.h = (int)data->InputLineHeight;
  98. SDL_SetTextInputRect(&r);
  99. }
  100. }
  101. static ImGuiKey ImGui_ImplSDL3_KeycodeToImGuiKey(int keycode)
  102. {
  103. switch (keycode)
  104. {
  105. case SDLK_TAB: return ImGuiKey_Tab;
  106. case SDLK_LEFT: return ImGuiKey_LeftArrow;
  107. case SDLK_RIGHT: return ImGuiKey_RightArrow;
  108. case SDLK_UP: return ImGuiKey_UpArrow;
  109. case SDLK_DOWN: return ImGuiKey_DownArrow;
  110. case SDLK_PAGEUP: return ImGuiKey_PageUp;
  111. case SDLK_PAGEDOWN: return ImGuiKey_PageDown;
  112. case SDLK_HOME: return ImGuiKey_Home;
  113. case SDLK_END: return ImGuiKey_End;
  114. case SDLK_INSERT: return ImGuiKey_Insert;
  115. case SDLK_DELETE: return ImGuiKey_Delete;
  116. case SDLK_BACKSPACE: return ImGuiKey_Backspace;
  117. case SDLK_SPACE: return ImGuiKey_Space;
  118. case SDLK_RETURN: return ImGuiKey_Enter;
  119. case SDLK_ESCAPE: return ImGuiKey_Escape;
  120. case SDLK_QUOTE: return ImGuiKey_Apostrophe;
  121. case SDLK_COMMA: return ImGuiKey_Comma;
  122. case SDLK_MINUS: return ImGuiKey_Minus;
  123. case SDLK_PERIOD: return ImGuiKey_Period;
  124. case SDLK_SLASH: return ImGuiKey_Slash;
  125. case SDLK_SEMICOLON: return ImGuiKey_Semicolon;
  126. case SDLK_EQUALS: return ImGuiKey_Equal;
  127. case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket;
  128. case SDLK_BACKSLASH: return ImGuiKey_Backslash;
  129. case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket;
  130. case SDLK_BACKQUOTE: return ImGuiKey_GraveAccent;
  131. case SDLK_CAPSLOCK: return ImGuiKey_CapsLock;
  132. case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock;
  133. case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock;
  134. case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen;
  135. case SDLK_PAUSE: return ImGuiKey_Pause;
  136. case SDLK_KP_0: return ImGuiKey_Keypad0;
  137. case SDLK_KP_1: return ImGuiKey_Keypad1;
  138. case SDLK_KP_2: return ImGuiKey_Keypad2;
  139. case SDLK_KP_3: return ImGuiKey_Keypad3;
  140. case SDLK_KP_4: return ImGuiKey_Keypad4;
  141. case SDLK_KP_5: return ImGuiKey_Keypad5;
  142. case SDLK_KP_6: return ImGuiKey_Keypad6;
  143. case SDLK_KP_7: return ImGuiKey_Keypad7;
  144. case SDLK_KP_8: return ImGuiKey_Keypad8;
  145. case SDLK_KP_9: return ImGuiKey_Keypad9;
  146. case SDLK_KP_PERIOD: return ImGuiKey_KeypadDecimal;
  147. case SDLK_KP_DIVIDE: return ImGuiKey_KeypadDivide;
  148. case SDLK_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
  149. case SDLK_KP_MINUS: return ImGuiKey_KeypadSubtract;
  150. case SDLK_KP_PLUS: return ImGuiKey_KeypadAdd;
  151. case SDLK_KP_ENTER: return ImGuiKey_KeypadEnter;
  152. case SDLK_KP_EQUALS: return ImGuiKey_KeypadEqual;
  153. case SDLK_LCTRL: return ImGuiKey_LeftCtrl;
  154. case SDLK_LSHIFT: return ImGuiKey_LeftShift;
  155. case SDLK_LALT: return ImGuiKey_LeftAlt;
  156. case SDLK_LGUI: return ImGuiKey_LeftSuper;
  157. case SDLK_RCTRL: return ImGuiKey_RightCtrl;
  158. case SDLK_RSHIFT: return ImGuiKey_RightShift;
  159. case SDLK_RALT: return ImGuiKey_RightAlt;
  160. case SDLK_RGUI: return ImGuiKey_RightSuper;
  161. case SDLK_APPLICATION: return ImGuiKey_Menu;
  162. case SDLK_0: return ImGuiKey_0;
  163. case SDLK_1: return ImGuiKey_1;
  164. case SDLK_2: return ImGuiKey_2;
  165. case SDLK_3: return ImGuiKey_3;
  166. case SDLK_4: return ImGuiKey_4;
  167. case SDLK_5: return ImGuiKey_5;
  168. case SDLK_6: return ImGuiKey_6;
  169. case SDLK_7: return ImGuiKey_7;
  170. case SDLK_8: return ImGuiKey_8;
  171. case SDLK_9: return ImGuiKey_9;
  172. case SDLK_a: return ImGuiKey_A;
  173. case SDLK_b: return ImGuiKey_B;
  174. case SDLK_c: return ImGuiKey_C;
  175. case SDLK_d: return ImGuiKey_D;
  176. case SDLK_e: return ImGuiKey_E;
  177. case SDLK_f: return ImGuiKey_F;
  178. case SDLK_g: return ImGuiKey_G;
  179. case SDLK_h: return ImGuiKey_H;
  180. case SDLK_i: return ImGuiKey_I;
  181. case SDLK_j: return ImGuiKey_J;
  182. case SDLK_k: return ImGuiKey_K;
  183. case SDLK_l: return ImGuiKey_L;
  184. case SDLK_m: return ImGuiKey_M;
  185. case SDLK_n: return ImGuiKey_N;
  186. case SDLK_o: return ImGuiKey_O;
  187. case SDLK_p: return ImGuiKey_P;
  188. case SDLK_q: return ImGuiKey_Q;
  189. case SDLK_r: return ImGuiKey_R;
  190. case SDLK_s: return ImGuiKey_S;
  191. case SDLK_t: return ImGuiKey_T;
  192. case SDLK_u: return ImGuiKey_U;
  193. case SDLK_v: return ImGuiKey_V;
  194. case SDLK_w: return ImGuiKey_W;
  195. case SDLK_x: return ImGuiKey_X;
  196. case SDLK_y: return ImGuiKey_Y;
  197. case SDLK_z: return ImGuiKey_Z;
  198. case SDLK_F1: return ImGuiKey_F1;
  199. case SDLK_F2: return ImGuiKey_F2;
  200. case SDLK_F3: return ImGuiKey_F3;
  201. case SDLK_F4: return ImGuiKey_F4;
  202. case SDLK_F5: return ImGuiKey_F5;
  203. case SDLK_F6: return ImGuiKey_F6;
  204. case SDLK_F7: return ImGuiKey_F7;
  205. case SDLK_F8: return ImGuiKey_F8;
  206. case SDLK_F9: return ImGuiKey_F9;
  207. case SDLK_F10: return ImGuiKey_F10;
  208. case SDLK_F11: return ImGuiKey_F11;
  209. case SDLK_F12: return ImGuiKey_F12;
  210. case SDLK_F13: return ImGuiKey_F13;
  211. case SDLK_F14: return ImGuiKey_F14;
  212. case SDLK_F15: return ImGuiKey_F15;
  213. case SDLK_F16: return ImGuiKey_F16;
  214. case SDLK_F17: return ImGuiKey_F17;
  215. case SDLK_F18: return ImGuiKey_F18;
  216. case SDLK_F19: return ImGuiKey_F19;
  217. case SDLK_F20: return ImGuiKey_F20;
  218. case SDLK_F21: return ImGuiKey_F21;
  219. case SDLK_F22: return ImGuiKey_F22;
  220. case SDLK_F23: return ImGuiKey_F23;
  221. case SDLK_F24: return ImGuiKey_F24;
  222. case SDLK_AC_BACK: return ImGuiKey_AppBack;
  223. case SDLK_AC_FORWARD: return ImGuiKey_AppForward;
  224. }
  225. return ImGuiKey_None;
  226. }
  227. static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods)
  228. {
  229. ImGuiIO& io = ImGui::GetIO();
  230. io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0);
  231. io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0);
  232. io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0);
  233. io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0);
  234. }
  235. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  236. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
  237. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
  238. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  239. // 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.
  240. bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)
  241. {
  242. ImGuiIO& io = ImGui::GetIO();
  243. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  244. switch (event->type)
  245. {
  246. case SDL_EVENT_MOUSE_MOTION:
  247. {
  248. ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y);
  249. io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  250. io.AddMousePosEvent(mouse_pos.x, mouse_pos.y);
  251. return true;
  252. }
  253. case SDL_EVENT_MOUSE_WHEEL:
  254. {
  255. //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY);
  256. float wheel_x = -event->wheel.x;
  257. float wheel_y = event->wheel.y;
  258. #ifdef __EMSCRIPTEN__
  259. wheel_x /= 100.0f;
  260. #endif
  261. io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  262. io.AddMouseWheelEvent(wheel_x, wheel_y);
  263. return true;
  264. }
  265. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  266. case SDL_EVENT_MOUSE_BUTTON_UP:
  267. {
  268. int mouse_button = -1;
  269. if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; }
  270. if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; }
  271. if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; }
  272. if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; }
  273. if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; }
  274. if (mouse_button == -1)
  275. break;
  276. io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse);
  277. io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN));
  278. bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button));
  279. return true;
  280. }
  281. case SDL_EVENT_TEXT_INPUT:
  282. {
  283. io.AddInputCharactersUTF8(event->text.text);
  284. return true;
  285. }
  286. case SDL_EVENT_KEY_DOWN:
  287. case SDL_EVENT_KEY_UP:
  288. {
  289. ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.keysym.mod);
  290. ImGuiKey key = ImGui_ImplSDL3_KeycodeToImGuiKey(event->key.keysym.sym);
  291. io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN));
  292. io.SetKeyEventNativeData(key, event->key.keysym.sym, event->key.keysym.scancode, event->key.keysym.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions.
  293. return true;
  294. }
  295. case SDL_EVENT_WINDOW_MOUSE_ENTER:
  296. {
  297. bd->MouseWindowID = event->window.windowID;
  298. bd->PendingMouseLeaveFrame = 0;
  299. return true;
  300. }
  301. // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late,
  302. // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why
  303. // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details.
  304. // FIXME: Unconfirmed whether this is still needed with SDL3.
  305. case SDL_EVENT_WINDOW_MOUSE_LEAVE:
  306. {
  307. bd->PendingMouseLeaveFrame = ImGui::GetFrameCount() + 1;
  308. return true;
  309. }
  310. case SDL_EVENT_WINDOW_FOCUS_GAINED:
  311. io.AddFocusEvent(true);
  312. return true;
  313. case SDL_EVENT_WINDOW_FOCUS_LOST:
  314. io.AddFocusEvent(false);
  315. return true;
  316. }
  317. return false;
  318. }
  319. static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
  320. {
  321. IM_UNUSED(window);
  322. viewport->PlatformHandleRaw = nullptr;
  323. #if defined(__WIN32__) && !defined(__WINRT__)
  324. viewport->PlatformHandleRaw = (HWND)SDL_GetProperty(SDL_GetWindowProperties(window), "SDL.window.win32.hwnd", nullptr);
  325. #elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA)
  326. viewport->PlatformHandleRaw = (void*)SDL_GetProperty(SDL_GetWindowProperties(window), "SDL.window.cocoa.window", nullptr);
  327. #endif
  328. }
  329. static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
  330. {
  331. ImGuiIO& io = ImGui::GetIO();
  332. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  333. IM_UNUSED(sdl_gl_context); // Unused in this branch
  334. // Check and store if we are on a SDL backend that supports global mouse position
  335. // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list)
  336. bool mouse_can_use_global_state = false;
  337. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  338. const char* sdl_backend = SDL_GetCurrentVideoDriver();
  339. const char* global_mouse_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" };
  340. for (int n = 0; n < IM_ARRAYSIZE(global_mouse_whitelist); n++)
  341. if (strncmp(sdl_backend, global_mouse_whitelist[n], strlen(global_mouse_whitelist[n])) == 0)
  342. mouse_can_use_global_state = true;
  343. #endif
  344. // Setup backend capabilities flags
  345. ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)();
  346. io.BackendPlatformUserData = (void*)bd;
  347. io.BackendPlatformName = "imgui_impl_sdl3";
  348. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  349. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  350. bd->Window = window;
  351. bd->Renderer = renderer;
  352. bd->MouseCanUseGlobalState = mouse_can_use_global_state;
  353. io.SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText;
  354. io.GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText;
  355. io.ClipboardUserData = nullptr;
  356. io.SetPlatformImeDataFn = ImGui_ImplSDL3_SetPlatformImeData;
  357. // Load mouse cursors
  358. bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
  359. bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM);
  360. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL);
  361. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS);
  362. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE);
  363. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW);
  364. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE);
  365. bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
  366. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO);
  367. // Set platform dependent data in viewport
  368. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  369. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  370. ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window);
  371. // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event.
  372. // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered.
  373. // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application.
  374. // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click:
  375. // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED)
  376. #ifdef SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH
  377. SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
  378. #endif
  379. // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710)
  380. #ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
  381. SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
  382. #endif
  383. return true;
  384. }
  385. bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
  386. {
  387. IM_UNUSED(sdl_gl_context); // Viewport branch will need this.
  388. return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context);
  389. }
  390. bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window)
  391. {
  392. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  393. }
  394. bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window)
  395. {
  396. #if !defined(_WIN32)
  397. IM_ASSERT(0 && "Unsupported");
  398. #endif
  399. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  400. }
  401. bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window)
  402. {
  403. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  404. }
  405. bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
  406. {
  407. return ImGui_ImplSDL3_Init(window, renderer, nullptr);
  408. }
  409. bool ImGui_ImplSDL3_InitForOther(SDL_Window* window)
  410. {
  411. return ImGui_ImplSDL3_Init(window, nullptr, nullptr);
  412. }
  413. void ImGui_ImplSDL3_Shutdown()
  414. {
  415. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  416. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  417. ImGuiIO& io = ImGui::GetIO();
  418. if (bd->ClipboardTextData)
  419. SDL_free(bd->ClipboardTextData);
  420. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  421. SDL_DestroyCursor(bd->MouseCursors[cursor_n]);
  422. bd->LastMouseCursor = nullptr;
  423. io.BackendPlatformName = nullptr;
  424. io.BackendPlatformUserData = nullptr;
  425. io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad);
  426. IM_DELETE(bd);
  427. }
  428. static void ImGui_ImplSDL3_UpdateMouseData()
  429. {
  430. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  431. ImGuiIO& io = ImGui::GetIO();
  432. // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below)
  433. #if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE
  434. // 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
  435. SDL_CaptureMouse((bd->MouseButtonsDown != 0) ? SDL_TRUE : SDL_FALSE);
  436. SDL_Window* focused_window = SDL_GetKeyboardFocus();
  437. const bool is_app_focused = (bd->Window == focused_window);
  438. #else
  439. SDL_Window* focused_window = bd->Window;
  440. 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
  441. #endif
  442. if (is_app_focused)
  443. {
  444. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  445. if (io.WantSetMousePos)
  446. SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y);
  447. // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured)
  448. if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0)
  449. {
  450. // 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)
  451. float mouse_x_global, mouse_y_global;
  452. int window_x, window_y;
  453. SDL_GetGlobalMouseState(&mouse_x_global, &mouse_y_global);
  454. SDL_GetWindowPosition(focused_window, &window_x, &window_y);
  455. io.AddMousePosEvent(mouse_x_global - window_x, mouse_y_global - window_y);
  456. }
  457. }
  458. }
  459. static void ImGui_ImplSDL3_UpdateMouseCursor()
  460. {
  461. ImGuiIO& io = ImGui::GetIO();
  462. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  463. return;
  464. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  465. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  466. if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None)
  467. {
  468. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  469. SDL_HideCursor();
  470. }
  471. else
  472. {
  473. // Show OS mouse cursor
  474. SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow];
  475. if (bd->LastMouseCursor != expected_cursor)
  476. {
  477. SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113)
  478. bd->LastMouseCursor = expected_cursor;
  479. }
  480. SDL_ShowCursor();
  481. }
  482. }
  483. static void ImGui_ImplSDL3_UpdateGamepads()
  484. {
  485. ImGuiIO& io = ImGui::GetIO();
  486. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  487. return;
  488. // Get gamepad
  489. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  490. SDL_Gamepad* gamepad = SDL_OpenGamepad(0);
  491. if (!gamepad)
  492. return;
  493. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  494. // Update gamepad inputs
  495. #define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V)
  496. #define MAP_BUTTON(KEY_NO, BUTTON_NO) { io.AddKeyEvent(KEY_NO, SDL_GetGamepadButton(gamepad, BUTTON_NO) != 0); }
  497. #define MAP_ANALOG(KEY_NO, AXIS_NO, V0, V1) { float vn = (float)(SDL_GetGamepadAxis(gamepad, AXIS_NO) - V0) / (float)(V1 - V0); vn = IM_SATURATE(vn); io.AddKeyAnalogEvent(KEY_NO, vn > 0.1f, vn); }
  498. const int thumb_dead_zone = 8000; // SDL_gamecontroller.h suggests using this value.
  499. MAP_BUTTON(ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START);
  500. MAP_BUTTON(ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK);
  501. MAP_BUTTON(ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square
  502. MAP_BUTTON(ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle
  503. MAP_BUTTON(ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle
  504. MAP_BUTTON(ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross
  505. MAP_BUTTON(ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT);
  506. MAP_BUTTON(ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT);
  507. MAP_BUTTON(ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP);
  508. MAP_BUTTON(ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN);
  509. MAP_BUTTON(ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER);
  510. MAP_BUTTON(ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER);
  511. MAP_ANALOG(ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767);
  512. MAP_ANALOG(ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767);
  513. MAP_BUTTON(ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK);
  514. MAP_BUTTON(ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK);
  515. MAP_ANALOG(ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768);
  516. MAP_ANALOG(ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767);
  517. MAP_ANALOG(ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768);
  518. MAP_ANALOG(ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767);
  519. MAP_ANALOG(ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768);
  520. MAP_ANALOG(ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767);
  521. MAP_ANALOG(ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768);
  522. MAP_ANALOG(ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767);
  523. #undef MAP_BUTTON
  524. #undef MAP_ANALOG
  525. }
  526. void ImGui_ImplSDL3_NewFrame()
  527. {
  528. ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData();
  529. IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplSDL3_Init()?");
  530. ImGuiIO& io = ImGui::GetIO();
  531. // Setup display size (every frame to accommodate for window resizing)
  532. int w, h;
  533. int display_w, display_h;
  534. SDL_GetWindowSize(bd->Window, &w, &h);
  535. if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED)
  536. w = h = 0;
  537. SDL_GetWindowSizeInPixels(bd->Window, &display_w, &display_h);
  538. io.DisplaySize = ImVec2((float)w, (float)h);
  539. if (w > 0 && h > 0)
  540. io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h);
  541. // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution)
  542. // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644)
  543. static Uint64 frequency = SDL_GetPerformanceFrequency();
  544. Uint64 current_time = SDL_GetPerformanceCounter();
  545. if (current_time <= bd->Time)
  546. current_time = bd->Time + 1;
  547. io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f);
  548. bd->Time = current_time;
  549. if (bd->PendingMouseLeaveFrame && bd->PendingMouseLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0)
  550. {
  551. bd->MouseWindowID = 0;
  552. bd->PendingMouseLeaveFrame = 0;
  553. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  554. }
  555. ImGui_ImplSDL3_UpdateMouseData();
  556. ImGui_ImplSDL3_UpdateMouseCursor();
  557. // Update game controllers (if enabled and available)
  558. ImGui_ImplSDL3_UpdateGamepads();
  559. }
  560. //-----------------------------------------------------------------------------
  561. #if defined(__clang__)
  562. #pragma clang diagnostic pop
  563. #endif
  564. #endif // #ifndef IMGUI_DISABLE