From cead66d3c65c026e0f812c9460df5ede74feb23a Mon Sep 17 00:00:00 2001 From: rainmakerv2 <30595646+rainmakerv3@users.noreply.github.com> Date: Mon, 13 Apr 2026 06:40:11 +0800 Subject: [PATCH] Big Picture Mode (#4250) * imguitest * button tests * fix gamepad nav * placeholder hardcoded eboot path * set focus correctly, move to own files * get installed game information * dynamically adjust rows * use slider for ui scale * launch big picture with CLI arg * Use emulator settings for UI scale and window size * center scrolling on focused item * fix item focus on scrolling when navigating with keyboard or pad * minor fixups and comments * fix performance degradation * adjust fonts to show TM symbol and higher overscale * reuse and clang * add exists check before iterator * flatten navigation (gamepad navigation crosses child window container) * cleanup and comments * simplify update checker a bit --------- Co-authored-by: georgemoralis --- .gitmodules | 3 + CMakeLists.txt | 9 +- externals/CMakeLists.txt | 23 + externals/sdl3_image | 1 + src/core/devtools/layer.cpp | 8 +- src/core/devtools/layer.h | 4 +- src/core/emulator_settings.h | 4 +- src/imgui/big_picture.cpp | 339 +++++ src/imgui/big_picture.h | 22 + src/imgui/renderer/imgui_impl_sdl3_bpm.cpp | 1313 +++++++++++++++++ src/imgui/renderer/imgui_impl_sdl3_bpm.h | 39 + .../renderer/imgui_impl_sdlrenderer3.cpp | 289 ++++ src/imgui/renderer/imgui_impl_sdlrenderer3.h | 33 + src/main.cpp | 21 +- 14 files changed, 2094 insertions(+), 14 deletions(-) create mode 160000 externals/sdl3_image create mode 100644 src/imgui/big_picture.cpp create mode 100644 src/imgui/big_picture.h create mode 100644 src/imgui/renderer/imgui_impl_sdl3_bpm.cpp create mode 100644 src/imgui/renderer/imgui_impl_sdl3_bpm.h create mode 100644 src/imgui/renderer/imgui_impl_sdlrenderer3.cpp create mode 100644 src/imgui/renderer/imgui_impl_sdlrenderer3.h diff --git a/.gitmodules b/.gitmodules index d24b23996..40f293a32 100644 --- a/.gitmodules +++ b/.gitmodules @@ -129,6 +129,9 @@ [submodule "externals/openal-soft"] path = externals/openal-soft url = https://github.com/shadexternals/openal-soft.git +[submodule "externals/sdl3_image"] + path = externals/sdl3_image + url = https://github.com/libsdl-org/SDL_image [submodule "externals/libusb"] path = externals/libusb url = https://github.com/shadexternals/libusb.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 3d69da660..3211f9061 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -234,6 +234,7 @@ find_package(PNG 1.6 MODULE) find_package(OpenAL CONFIG) find_package(RenderDoc 1.6.0 MODULE) find_package(SDL3_mixer 2.8.1 CONFIG) +find_package(SDL3_image CONFIG) if (SDL3_mixer_FOUND) find_package(SDL3 3.1.2 CONFIG) endif() @@ -1099,10 +1100,16 @@ set(IMGUI src/imgui/imgui_config.h src/imgui/renderer/imgui_core.h src/imgui/renderer/imgui_impl_sdl3.cpp src/imgui/renderer/imgui_impl_sdl3.h + src/imgui/renderer/imgui_impl_sdl3_bpm.cpp + src/imgui/renderer/imgui_impl_sdl3_bpm.h + src/imgui/renderer/imgui_impl_sdlrenderer3.cpp + src/imgui/renderer/imgui_impl_sdlrenderer3.h src/imgui/renderer/imgui_impl_vulkan.cpp src/imgui/renderer/imgui_impl_vulkan.h src/imgui/renderer/texture_manager.cpp src/imgui/renderer/texture_manager.h + src/imgui/big_picture.cpp + src/imgui/big_picture.h ) set(INPUT src/input/controller.cpp @@ -1140,7 +1147,7 @@ add_executable(shadps4 create_target_directory_groups(shadps4) target_link_libraries(shadps4 PRIVATE magic_enum::magic_enum fmt::fmt toml11::toml11 tsl::robin_map xbyak::xbyak Tracy::TracyClient RenderDoc::API FFmpeg::ffmpeg Dear_ImGui gcn half::half ZLIB::ZLIB PNG::PNG) -target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator LibAtrac9 sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::glslang SDL3::SDL3 SDL3_mixer::SDL3_mixer pugixml::pugixml) +target_link_libraries(shadps4 PRIVATE Boost::headers GPUOpen::VulkanMemoryAllocator LibAtrac9 sirit Vulkan::Headers xxHash::xxhash Zydis::Zydis glslang::glslang SDL3::SDL3 SDL3_image::SDL3_image SDL3_mixer::SDL3_mixer pugixml::pugixml) target_link_libraries(shadps4 PRIVATE stb::headers lfreist-hwinfo::hwinfo nlohmann_json::nlohmann_json miniz::miniz fdk-aac CLI11::CLI11 OpenAL::OpenAL Cpp_Httplib) if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index 85931f81a..6fc3e471c 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -63,6 +63,29 @@ if (NOT TARGET SDL3::SDL3) add_subdirectory(sdl3) endif() +# SDL3_image +if (NOT TARGET SDL3_image::SDL3_image) + set(SDLIMAGE_VENDORED OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_ANI OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_AVIF OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_BMP OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_GIF OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_JPG OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_JXL OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_LBM OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_PCX OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_PNM OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_QOI OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_SVG OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_TGA OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_TIF OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_WEBP OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_XCF OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_XPM OFF CACHE BOOL "" FORCE) + set(SDLIMAGE_XV OFF CACHE BOOL "" FORCE) + add_subdirectory(sdl3_image) +endif() + # SDL3_mixer if (NOT TARGET SDL3_mixer::SDL3_mixer) set(SDLMIXER_FLAC OFF) diff --git a/externals/sdl3_image b/externals/sdl3_image new file mode 160000 index 000000000..1aedddcbd --- /dev/null +++ b/externals/sdl3_image @@ -0,0 +1 @@ +Subproject commit 1aedddcbd205c4e1ea0f99fdb2c785acc8e2489b diff --git a/src/core/devtools/layer.cpp b/src/core/devtools/layer.cpp index 10e5f911c..db28e2ec1 100644 --- a/src/core/devtools/layer.cpp +++ b/src/core/devtools/layer.cpp @@ -436,7 +436,7 @@ void L::Draw() { ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoDocking)) { SetWindowFontScale(1.5f); - TextCentered("Are you sure you want to quit?"); + Overlay::TextCentered("Are you sure you want to quit?"); NewLine(); Text("Press Escape or Circle/B button to cancel"); Text("Press Enter or Cross/A button to quit"); @@ -481,7 +481,9 @@ void L::Draw() { PopID(); } -void L::TextCentered(const std::string& text) { +namespace Overlay { + +void TextCentered(const std::string& text) { float window_width = GetWindowSize().x; float text_width = CalcTextSize(text.c_str()).x; float text_indentation = (window_width - text_width) * 0.5f; @@ -490,8 +492,6 @@ void L::TextCentered(const std::string& text) { Text("%s", text.c_str()); } -namespace Overlay { - void ToggleSimpleFps() { show_simple_fps = !show_simple_fps; visibility_toggled = true; diff --git a/src/core/devtools/layer.h b/src/core/devtools/layer.h index 761135baf..9106394b6 100644 --- a/src/core/devtools/layer.h +++ b/src/core/devtools/layer.h @@ -21,8 +21,6 @@ private: static void DrawMenuBar(); static void DrawAdvanced(); static void DrawSimple(); - - static void TextCentered(const std::string& text); }; } // namespace Core::Devtools @@ -34,4 +32,6 @@ void SetSimpleFps(bool enabled); void ToggleQuitWindow(); void ShowVolume(); +void TextCentered(const std::string& text); + } // namespace Overlay diff --git a/src/core/emulator_settings.h b/src/core/emulator_settings.h index 25ac4cab2..6d9d2ef09 100644 --- a/src/core/emulator_settings.h +++ b/src/core/emulator_settings.h @@ -187,6 +187,7 @@ struct GeneralSettings { Setting discord_rpc_enabled{false}; Setting show_fps_counter{false}; Setting console_language{1}; + Setting big_picture_scale{1000}; // return a vector of override descriptors (runtime, but tiny) std::vector GetOverrideableFields() const { @@ -218,7 +219,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GeneralSettings, install_dirs, addon_install_ trophy_notification_duration, log_filter, log_type, show_splash, identical_log_grouped, trophy_notification_side, connected_to_network, discord_rpc_enabled, show_fps_counter, - console_language) + console_language, big_picture_scale) // ------------------------------- // Debug settings @@ -557,6 +558,7 @@ public: SETTING_FORWARD_BOOL(m_general, DiscordRPCEnabled, discord_rpc_enabled) SETTING_FORWARD_BOOL(m_general, ShowFpsCounter, show_fps_counter) SETTING_FORWARD(m_general, ConsoleLanguage, console_language) + SETTING_FORWARD(m_general, BigPictureScale, big_picture_scale) // Audio settings SETTING_FORWARD(m_audio, AudioBackend, audio_backend) diff --git a/src/imgui/big_picture.cpp b/src/imgui/big_picture.cpp new file mode 100644 index 000000000..8cf83db02 --- /dev/null +++ b/src/imgui/big_picture.cpp @@ -0,0 +1,339 @@ +// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include "big_picture.h" +#include "common/logging/log.h" +#include "core/devtools/layer.h" +#include "core/file_format/psf.h" +#include "emulator.h" +#include "imgui/renderer/imgui_impl_sdl3_bpm.h" +#include "imgui/renderer/imgui_impl_sdlrenderer3.h" + +#include "imgui_fonts/notosansjp_regular.ttf.g.cpp" +#include "imgui_fonts/proggyvector_regular.ttf.g.cpp" + +namespace BigPictureMode { + +const float gameImageSize = 200.f; + +static bool done = false; +static bool runGame = false; +static std::filesystem::path runEbootPath = ""; +static std::vector gameVec = {}; +static std::vector focusState = {}; + +static float uiScale = 1.0f; +static int scaleSelected = 1; + +static SDL_Window* window = nullptr; +static SDL_Renderer* renderer = nullptr; + +void Launch() { + if (!SDL_Init(SDL_INIT_VIDEO)) { + LOG_ERROR(ImGui, "SDL_INIT_VIDEO Error: {}", SDL_GetError()); + SDL_Quit(); + return; + } + + if (!SDL_Init(SDL_INIT_GAMEPAD)) { + LOG_ERROR(ImGui, "SDL_INIT_GAMEPAD Error: {}", SDL_GetError()); + SDL_Quit(); + return; + } + + window = SDL_CreateWindow("shadPS4 Big Picture Mode", EmulatorSettings.GetWindowWidth(), + EmulatorSettings.GetWindowHeight(), SDL_WINDOW_RESIZABLE); + renderer = SDL_CreateRenderer(window, nullptr); + + if (EmulatorSettings.IsFullScreen()) { + SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); + } + + // Check if window creation failed + if (window == nullptr) { + LOG_ERROR(ImGui, "SDL Window Creation Error: {}", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_Quit(); + return; + } + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGui::StyleColorsDark(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigNavCursorVisibleAlways = true; + + ImFontConfig config; + config.OversampleH = 3; + config.OversampleV = 3; + config.MergeMode = true; + + ImFontConfig config2; + config.OversampleH = 3; + config.OversampleV = 3; + + // tm symbol + static const ImWchar icon_ranges[] = {0x2122, 0x2122, 0x3000, 0x30FF, 0}; + + ImFontGlyphRangesBuilder rb{}; + rb.AddRanges(io.Fonts->GetGlyphRangesDefault()); + rb.AddRanges(io.Fonts->GetGlyphRangesGreek()); + rb.AddRanges(io.Fonts->GetGlyphRangesKorean()); + rb.AddRanges(io.Fonts->GetGlyphRangesJapanese()); + rb.AddRanges(io.Fonts->GetGlyphRangesCyrillic()); + + ImVector ranges{}; + rb.BuildRanges(&ranges); + + ImFont* myFont = io.Fonts->AddFontFromMemoryCompressedTTF( + imgui_font_notosansjp_regular_compressed_data, + imgui_font_notosansjp_regular_compressed_size, 32.0f, &config2, icon_ranges); + + io.Fonts->AddFontFromMemoryCompressedTTF(imgui_font_notosansjp_regular_compressed_data, + imgui_font_notosansjp_regular_compressed_size, 32.0f, + &config, ranges.Data); + + io.Fonts->AddFontFromMemoryCompressedTTF(imgui_font_proggyvector_regular_compressed_data, + imgui_font_proggyvector_regular_compressed_size, 32.0f, + &config, io.Fonts->GetGlyphRangesDefault()); + ImGuiStyle& style = ImGui::GetStyle(); + ImVec4* colors = style.Colors; + + colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 1.00f); // black + colors[ImGuiCol_Header] = ImVec4(0.20f, 0.40f, 0.70f, 1.00f); // blue + colors[ImGuiCol_HeaderHovered] = ImVec4(0.25f, 0.50f, 0.85f, 1.00f); // lighter blue + + style.WindowRounding = 0.0f; + style.FrameRounding = 5.0f; + style.ItemSpacing = ImVec2(10.0f * uiScale, 10.0f * uiScale); + style.FramePadding = ImVec2(10.0f * uiScale, 10.0f * uiScale); + style.WindowBorderSize = 0.0f; + style.WindowPadding = ImVec2(20.0f * uiScale, 20.0f * uiScale); + + ImGui_ImplSDL3_InitForSDLRenderer(window, renderer); + ImGui_ImplSDLRenderer3_Init(renderer); + GetGameInfo(); + + uiScale = static_cast(EmulatorSettings.GetBigPictureScale() / 1000.f); + float tempScale = uiScale; + + while (!done) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + ImGui_ImplSDL3_ProcessEvent(&event); + if (event.type == SDL_EVENT_QUIT) { + done = true; + } + } + + ImGui_ImplSDLRenderer3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + ImGui::NewFrame(); + + ImGuiViewport* viewport = ImGui::GetMainViewport(); + ImGui::SetNextWindowPos(viewport->WorkPos); + ImGui::SetNextWindowSize(viewport->WorkSize); + ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration; + + ImGui::PushFont(myFont); + ImGui::Begin("Game Window", &done, window_flags); + ImGui::SetWindowFontScale(uiScale); + + ImGuiWindowFlags child_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NavFlattened; + + if (ImGui::IsWindowAppearing()) { + ImGui::SetNextWindowFocus(); + } + + ImGui::BeginChild("ContentRegion", ImVec2(0, -ImGui::GetFrameHeightWithSpacing()), true, + child_flags); + Overlay::TextCentered("Select Game"); + ImGui::Dummy(ImVec2(0.0f, 10.f * uiScale)); + + if (ImGui::IsWindowAppearing()) { + ImGui::SetKeyboardFocusHere(); + } + + SetGameIcons(); + ImGui::EndChild(); + ImGui::Separator(); + + ImGui::SetNextItemWidth(300.0f * uiScale); + if (ImGui::SliderFloat("UI Scale", &tempScale, 0.25f, 3.0f)) { + // Dynamically changes UI scale + } + + // Only update when user is not interacting with slider + if (ImGui::IsItemDeactivatedAfterEdit()) { + uiScale = tempScale; + tempScale = uiScale; + } + ImGui::SameLine(); + + // Align buttons right + float buttonsWidth = + ImGui::CalcTextSize("Settings (Under Construction)").x + ImGui::CalcTextSize("Exit").x + + ImGui::GetStyle().FramePadding.x * 4.0f + ImGui::GetStyle().ItemSpacing.x; + ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x - buttonsWidth); + + if (ImGui::Button("Settings (Under Construction)")) { + // Todo + } + ImGui::SameLine(); + + if (ImGui::Button("Exit")) { + ImGui::OpenPopup("Confirm Exit"); + } + + ImVec2 center = ImGui::GetMainViewport()->GetCenter(); + ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); + if (ImGui::BeginPopupModal("Confirm Exit", NULL, ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::Text("This will exit shadPS4!\nAre you sure?"); + ImGui::Separator(); + + if (ImGui::Button("OK", ImVec2(120 * uiScale, 0))) { + ImGui::CloseCurrentPopup(); + done = true; + } + ImGui::SameLine(); + + if (ImGui::Button("Cancel", ImVec2(120 * uiScale, 0))) { + ImGui::CloseCurrentPopup(); + } + + if (ImGui::IsWindowAppearing()) { + ImGui::SetItemDefaultFocus(); + } + + ImGui::EndPopup(); + } + + ImGui::PopFont(); + ImGui::End(); + ImGui::Render(); + SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255); + SDL_RenderClear(renderer); + ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer); + SDL_RenderPresent(renderer); + } + + ImGui_ImplSDLRenderer3_Shutdown(); + ImGui_ImplSDL3_Shutdown(); + ImGui::DestroyContext(); + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); + SDL_Quit(); + + EmulatorSettings.SetBigPictureScale(static_cast(uiScale * 1000)); + EmulatorSettings.Save(); + + if (runGame) { + auto* emulator = Common::Singleton::Instance(); + emulator->Run(runEbootPath); + } +} + +void SetGameIcons() { + ImGuiStyle& style = ImGui::GetStyle(); + const float maxAvailableWidth = ImGui::GetContentRegionAvail().x; + const float itemSpacing = style.ItemSpacing.x; // already scaled + const float padding = 10.0f * uiScale; + float rowContentWidth = gameImageSize * uiScale + itemSpacing; + + // Use same line if content fits horizontally, move to next line if not + for (int i = 0; i < gameVec.size(); i++) { + ImGui::BeginGroup(); + + std::string ButtonName = "Button" + std::to_string(i); + const char* ButtonNameChar = ButtonName.c_str(); + + if (ImGui::ImageButton(ButtonNameChar, (ImTextureID)gameVec[i].iconTexture, + ImVec2(gameImageSize * uiScale, gameImageSize * uiScale))) { + runGame = true; + done = true; + runEbootPath = gameVec[i].ebootPath; + } + + // Scroll to item only when newly-focused + if (ImGui::IsItemFocused() && !focusState[i]) { + ImGui::SetScrollHereY(0.5f); + } + focusState[i] = ImGui::IsItemFocused(); + + ImGui::PushTextWrapPos(ImGui::GetCursorPos().x + gameImageSize * uiScale); + ImGui::TextWrapped("%s", gameVec[i].title.c_str()); + ImGui::PopTextWrapPos(); + ImGui::EndGroup(); + + rowContentWidth += (gameImageSize * uiScale + itemSpacing * 2 + padding); + if (rowContentWidth < maxAvailableWidth) { + ImGui::SameLine(0.0f, padding); + } else { + ImGui::Dummy(ImVec2(0.0f, padding)); + rowContentWidth = gameImageSize * uiScale + itemSpacing; + } + } +} + +std::filesystem::path UpdateChecker(const std::string sceItem, std::filesystem::path game_folder) { + std::filesystem::path outputPath; + auto update_folder = game_folder; + update_folder += "-UPDATE"; + + auto patch_folder = game_folder; + patch_folder += "-patch"; + + if (std::filesystem::exists(update_folder / "sce_sys" / sceItem)) { + outputPath = update_folder / "sce_sys" / sceItem; + } else if (std::filesystem::exists(patch_folder / "sce_sys" / sceItem)) { + outputPath = patch_folder / "sce_sys" / sceItem; + } else { + outputPath = game_folder / "sce_sys" / sceItem; + } + + return outputPath; +} + +void GetGameInfo() { + gameVec.clear(); + for (const auto& installLoc : EmulatorSettings.GetAllGameInstallDirs()) { + if (installLoc.enabled && std::filesystem::exists(installLoc.path)) { + for (const auto& entry : std::filesystem::directory_iterator(installLoc.path)) { + if (entry.path().filename().string().ends_with("-UPDATE") || + entry.path().filename().string().ends_with("-patch") || !entry.is_directory()) { + continue; + } + + Game game; + game.ebootPath = entry.path() / "eboot.bin"; + + const std::string iconFileName = "icon0.png"; + std::filesystem::path iconPath = UpdateChecker(iconFileName, entry.path()); + game.iconTexture = IMG_LoadTexture(renderer, iconPath.string().c_str()); + + PSF psf; + const std::string sfoFileName = "param.sfo"; + std::filesystem::path sfoPath = UpdateChecker(sfoFileName, entry.path()); + + if (psf.Open(sfoPath)) { + if (const auto title = psf.GetString("TITLE"); title.has_value()) { + game.title = *title; + } + } else { + continue; + } + + gameVec.push_back(game); + focusState.push_back(false); + } + } + } +} + +} // namespace BigPictureMode diff --git a/src/imgui/big_picture.h b/src/imgui/big_picture.h new file mode 100644 index 000000000..1d5b45f73 --- /dev/null +++ b/src/imgui/big_picture.h @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +namespace BigPictureMode { + +struct Game { + SDL_Texture* iconTexture; + std::filesystem::path ebootPath; + std::string title; +}; + +void Launch(); +void SetGameIcons(); +void GetGameInfo(); +std::filesystem::path UpdateChecker(const std::string sceItem, std::filesystem::path game_folder); + +} // namespace BigPictureMode diff --git a/src/imgui/renderer/imgui_impl_sdl3_bpm.cpp b/src/imgui/renderer/imgui_impl_sdl3_bpm.cpp new file mode 100644 index 000000000..6fa8b3936 --- /dev/null +++ b/src/imgui/renderer/imgui_impl_sdl3_bpm.cpp @@ -0,0 +1,1313 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +// Based on imgui_impl_sdl3.cpp from Dear ImGui repository + +#include "imgui.h" +#ifndef IMGUI_DISABLE +#include "imgui_impl_sdl3_bpm.h" + +// Clang warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored \ + "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose + // precision +#endif + +// SDL +#include +#if defined(__APPLE__) +#include +#endif +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#endif + +#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && \ + !defined(__amigaos4__) +#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1 +#else +#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 +#endif + +// FIXME-LEGACY: remove when SDL 3.1.3 preview is released. +#ifndef SDLK_APOSTROPHE +#define SDLK_APOSTROPHE SDLK_QUOTE +#endif +#ifndef SDLK_GRAVE +#define SDLK_GRAVE SDLK_BACKQUOTE +#endif + +// SDL Data +struct ImGui_ImplSDL3_Data { + SDL_Window* Window; + SDL_WindowID WindowID; + SDL_Renderer* Renderer; + Uint64 Time; + char* ClipboardTextData; + bool UseVulkan; + bool WantUpdateMonitors; + + // IME handling + SDL_Window* ImeWindow; + + // Mouse handling + Uint32 MouseWindowID; + int MouseButtonsDown; + SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT]; + SDL_Cursor* MouseLastCursor; + int MousePendingLeaveFrame; + bool MouseCanUseGlobalState; + bool MouseCanUseCapture; + bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set + // ImGuiBackendFlags_HasMouseHoveredViewport dynamically + // based on state. + + // Gamepad handling + ImVector Gamepads; + ImGui_ImplSDL3_GamepadMode GamepadMode; + bool WantUpdateGamepadsList; + + ImGui_ImplSDL3_Data() { + memset((void*)this, 0, sizeof(*this)); + } +}; + +static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData() { + return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData + : nullptr; +} + +// Forward Declarations +static void ImGui_ImplSDL3_UpdateMonitors(); +static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context); +static void ImGui_ImplSDL3_ShutdownMultiViewportSupport(); + +// Functions +static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*) { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + if (bd->ClipboardTextData) + SDL_free(bd->ClipboardTextData); + bd->ClipboardTextData = SDL_GetClipboardText(); + return bd->ClipboardTextData; +} + +static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text) { + SDL_SetClipboardText(text); +} + +static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport* viewport, + ImGuiPlatformImeData* data) { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle; + SDL_Window* window = SDL_GetWindowFromID(window_id); + if ((data->WantVisible == false || bd->ImeWindow != window) && bd->ImeWindow != nullptr) { + SDL_StopTextInput(bd->ImeWindow); + bd->ImeWindow = nullptr; + } + if (data->WantVisible) { + SDL_Rect r; + r.x = (int)(data->InputPos.x - viewport->Pos.x); + r.y = (int)(data->InputPos.y - viewport->Pos.y + data->InputLineHeight); + r.w = 1; + r.h = (int)data->InputLineHeight; + SDL_SetTextInputArea(window, &r, 0); + SDL_StartTextInput(window); + bd->ImeWindow = window; + } +} + +// Not static to allow third-party code to use that if they want to (but undocumented) +ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode); +ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode) { + // Keypad doesn't have individual key values in SDL3 + switch (scancode) { + case SDL_SCANCODE_KP_0: + return ImGuiKey_Keypad0; + case SDL_SCANCODE_KP_1: + return ImGuiKey_Keypad1; + case SDL_SCANCODE_KP_2: + return ImGuiKey_Keypad2; + case SDL_SCANCODE_KP_3: + return ImGuiKey_Keypad3; + case SDL_SCANCODE_KP_4: + return ImGuiKey_Keypad4; + case SDL_SCANCODE_KP_5: + return ImGuiKey_Keypad5; + case SDL_SCANCODE_KP_6: + return ImGuiKey_Keypad6; + case SDL_SCANCODE_KP_7: + return ImGuiKey_Keypad7; + case SDL_SCANCODE_KP_8: + return ImGuiKey_Keypad8; + case SDL_SCANCODE_KP_9: + return ImGuiKey_Keypad9; + case SDL_SCANCODE_KP_PERIOD: + return ImGuiKey_KeypadDecimal; + case SDL_SCANCODE_KP_DIVIDE: + return ImGuiKey_KeypadDivide; + case SDL_SCANCODE_KP_MULTIPLY: + return ImGuiKey_KeypadMultiply; + case SDL_SCANCODE_KP_MINUS: + return ImGuiKey_KeypadSubtract; + case SDL_SCANCODE_KP_PLUS: + return ImGuiKey_KeypadAdd; + case SDL_SCANCODE_KP_ENTER: + return ImGuiKey_KeypadEnter; + case SDL_SCANCODE_KP_EQUALS: + return ImGuiKey_KeypadEqual; + default: + break; + } + + switch (keycode) { + case SDLK_TAB: + return ImGuiKey_Tab; + case SDLK_LEFT: + return ImGuiKey_LeftArrow; + case SDLK_RIGHT: + return ImGuiKey_RightArrow; + case SDLK_UP: + return ImGuiKey_UpArrow; + case SDLK_DOWN: + return ImGuiKey_DownArrow; + case SDLK_PAGEUP: + return ImGuiKey_PageUp; + case SDLK_PAGEDOWN: + return ImGuiKey_PageDown; + case SDLK_HOME: + return ImGuiKey_Home; + case SDLK_END: + return ImGuiKey_End; + case SDLK_INSERT: + return ImGuiKey_Insert; + case SDLK_DELETE: + return ImGuiKey_Delete; + case SDLK_BACKSPACE: + return ImGuiKey_Backspace; + case SDLK_SPACE: + return ImGuiKey_Space; + case SDLK_RETURN: + return ImGuiKey_Enter; + case SDLK_ESCAPE: + return ImGuiKey_Escape; + // case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe; + case SDLK_COMMA: + return ImGuiKey_Comma; + // case SDLK_MINUS: return ImGuiKey_Minus; + case SDLK_PERIOD: + return ImGuiKey_Period; + // case SDLK_SLASH: return ImGuiKey_Slash; + case SDLK_SEMICOLON: + return ImGuiKey_Semicolon; + // case SDLK_EQUALS: return ImGuiKey_Equal; + // case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket; + // case SDLK_BACKSLASH: return ImGuiKey_Backslash; + // case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket; + // case SDLK_GRAVE: return ImGuiKey_GraveAccent; + case SDLK_CAPSLOCK: + return ImGuiKey_CapsLock; + case SDLK_SCROLLLOCK: + return ImGuiKey_ScrollLock; + case SDLK_NUMLOCKCLEAR: + return ImGuiKey_NumLock; + case SDLK_PRINTSCREEN: + return ImGuiKey_PrintScreen; + case SDLK_PAUSE: + return ImGuiKey_Pause; + case SDLK_LCTRL: + return ImGuiKey_LeftCtrl; + case SDLK_LSHIFT: + return ImGuiKey_LeftShift; + case SDLK_LALT: + return ImGuiKey_LeftAlt; + case SDLK_LGUI: + return ImGuiKey_LeftSuper; + case SDLK_RCTRL: + return ImGuiKey_RightCtrl; + case SDLK_RSHIFT: + return ImGuiKey_RightShift; + case SDLK_RALT: + return ImGuiKey_RightAlt; + case SDLK_RGUI: + return ImGuiKey_RightSuper; + case SDLK_APPLICATION: + return ImGuiKey_Menu; + case SDLK_0: + return ImGuiKey_0; + case SDLK_1: + return ImGuiKey_1; + case SDLK_2: + return ImGuiKey_2; + case SDLK_3: + return ImGuiKey_3; + case SDLK_4: + return ImGuiKey_4; + case SDLK_5: + return ImGuiKey_5; + case SDLK_6: + return ImGuiKey_6; + case SDLK_7: + return ImGuiKey_7; + case SDLK_8: + return ImGuiKey_8; + case SDLK_9: + return ImGuiKey_9; + case SDLK_A: + return ImGuiKey_LeftArrow; + case SDLK_B: + return ImGuiKey_B; + case SDLK_C: + return ImGuiKey_C; + case SDLK_D: + return ImGuiKey_RightArrow; + case SDLK_E: + return ImGuiKey_E; + case SDLK_F: + return ImGuiKey_F; + case SDLK_G: + return ImGuiKey_G; + case SDLK_H: + return ImGuiKey_H; + case SDLK_I: + return ImGuiKey_I; + case SDLK_J: + return ImGuiKey_J; + case SDLK_K: + return ImGuiKey_K; + case SDLK_L: + return ImGuiKey_L; + case SDLK_M: + return ImGuiKey_M; + case SDLK_N: + return ImGuiKey_N; + case SDLK_O: + return ImGuiKey_O; + case SDLK_P: + return ImGuiKey_P; + case SDLK_Q: + return ImGuiKey_Q; + case SDLK_R: + return ImGuiKey_R; + case SDLK_S: + return ImGuiKey_DownArrow; + case SDLK_T: + return ImGuiKey_T; + case SDLK_U: + return ImGuiKey_U; + case SDLK_V: + return ImGuiKey_V; + case SDLK_W: + return ImGuiKey_UpArrow; + case SDLK_X: + return ImGuiKey_X; + case SDLK_Y: + return ImGuiKey_Y; + case SDLK_Z: + return ImGuiKey_Z; + case SDLK_F1: + return ImGuiKey_F1; + case SDLK_F2: + return ImGuiKey_F2; + case SDLK_F3: + return ImGuiKey_F3; + case SDLK_F4: + return ImGuiKey_F4; + case SDLK_F5: + return ImGuiKey_F5; + case SDLK_F6: + return ImGuiKey_F6; + case SDLK_F7: + return ImGuiKey_F7; + case SDLK_F8: + return ImGuiKey_F8; + case SDLK_F9: + return ImGuiKey_F9; + case SDLK_F10: + return ImGuiKey_F10; + case SDLK_F11: + return ImGuiKey_F11; + case SDLK_F12: + return ImGuiKey_F12; + case SDLK_F13: + return ImGuiKey_F13; + case SDLK_F14: + return ImGuiKey_F14; + case SDLK_F15: + return ImGuiKey_F15; + case SDLK_F16: + return ImGuiKey_F16; + case SDLK_F17: + return ImGuiKey_F17; + case SDLK_F18: + return ImGuiKey_F18; + case SDLK_F19: + return ImGuiKey_F19; + case SDLK_F20: + return ImGuiKey_F20; + case SDLK_F21: + return ImGuiKey_F21; + case SDLK_F22: + return ImGuiKey_F22; + case SDLK_F23: + return ImGuiKey_F23; + case SDLK_F24: + return ImGuiKey_F24; + case SDLK_AC_BACK: + return ImGuiKey_AppBack; + case SDLK_AC_FORWARD: + return ImGuiKey_AppForward; + default: + break; + } + + // Fallback to scancode + switch (scancode) { + case SDL_SCANCODE_GRAVE: + return ImGuiKey_GraveAccent; + case SDL_SCANCODE_MINUS: + return ImGuiKey_Minus; + case SDL_SCANCODE_EQUALS: + return ImGuiKey_Equal; + case SDL_SCANCODE_LEFTBRACKET: + return ImGuiKey_LeftBracket; + case SDL_SCANCODE_RIGHTBRACKET: + return ImGuiKey_RightBracket; + case SDL_SCANCODE_NONUSBACKSLASH: + return ImGuiKey_Oem102; + case SDL_SCANCODE_BACKSLASH: + return ImGuiKey_Backslash; + case SDL_SCANCODE_SEMICOLON: + return ImGuiKey_Semicolon; + case SDL_SCANCODE_APOSTROPHE: + return ImGuiKey_Apostrophe; + case SDL_SCANCODE_COMMA: + return ImGuiKey_Comma; + case SDL_SCANCODE_PERIOD: + return ImGuiKey_Period; + case SDL_SCANCODE_SLASH: + return ImGuiKey_Slash; + default: + break; + } + + return ImGuiKey_None; +} + +static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods) { + ImGuiIO& io = ImGui::GetIO(); + io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0); + io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0); + io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0); + io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0); +} + +static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id) { + return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id); +} + +// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to +// use your inputs. +// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or +// clear/overwrite your copy of the mouse data. +// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main +// application, or clear/overwrite your copy of the keyboard data. Generally you may always pass all +// inputs to dear imgui, and hide them from your application based on those two flags. +bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event) { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + IM_ASSERT(bd != nullptr && + "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?"); + ImGuiIO& io = ImGui::GetIO(); + + switch (event->type) { + case SDL_EVENT_MOUSE_MOTION: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == nullptr) + return false; + ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { + int window_x, window_y; + SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, + &window_y); + mouse_pos.x += window_x; + mouse_pos.y += window_y; + } + io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID + ? ImGuiMouseSource_TouchScreen + : ImGuiMouseSource_Mouse); + io.AddMousePosEvent(mouse_pos.x, mouse_pos.y); + return true; + } + case SDL_EVENT_MOUSE_WHEEL: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == nullptr) + return false; + // IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, + // (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY); + float wheel_x = -event->wheel.x; + float wheel_y = event->wheel.y; + io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID + ? ImGuiMouseSource_TouchScreen + : ImGuiMouseSource_Mouse); + io.AddMouseWheelEvent(wheel_x, wheel_y); + return true; + } + case SDL_EVENT_MOUSE_BUTTON_DOWN: + case SDL_EVENT_MOUSE_BUTTON_UP: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == nullptr) + return false; + int mouse_button = -1; + if (event->button.button == SDL_BUTTON_LEFT) { + mouse_button = 0; + } + if (event->button.button == SDL_BUTTON_RIGHT) { + mouse_button = 1; + } + if (event->button.button == SDL_BUTTON_MIDDLE) { + mouse_button = 2; + } + if (event->button.button == SDL_BUTTON_X1) { + mouse_button = 3; + } + if (event->button.button == SDL_BUTTON_X2) { + mouse_button = 4; + } + if (mouse_button == -1) + break; + io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID + ? ImGuiMouseSource_TouchScreen + : ImGuiMouseSource_Mouse); + io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN)); + bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) + ? (bd->MouseButtonsDown | (1 << mouse_button)) + : (bd->MouseButtonsDown & ~(1 << mouse_button)); + return true; + } + case SDL_EVENT_TEXT_INPUT: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == nullptr) + return false; + io.AddInputCharactersUTF8(event->text.text); + return true; + } + case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID) == nullptr) + return false; + ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod); + // IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%s : key=%d ('%s'), scancode=%d ('%s'), mod=%X\n", + // (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP ", event->key.key, + // SDL_GetKeyName(event->key.key), event->key.scancode, + // SDL_GetScancodeName(event->key.scancode), event->key.mod); + ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode); + io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN)); + 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. + return true; + } + case SDL_EVENT_DISPLAY_ORIENTATION: + case SDL_EVENT_DISPLAY_ADDED: + case SDL_EVENT_DISPLAY_REMOVED: + case SDL_EVENT_DISPLAY_MOVED: + case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED: { + bd->WantUpdateMonitors = true; + return true; + } + case SDL_EVENT_WINDOW_MOUSE_ENTER: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) + return false; + bd->MouseWindowID = event->window.windowID; + bd->MousePendingLeaveFrame = 0; + return true; + } + // - In some cases, when detaching a window from main viewport SDL may + // send SDL_WINDOWEVENT_ENTER one frame too late, + // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear + // mouse position. This is why we delay process the SDL_WINDOWEVENT_LEAVE events by one + // frame. See issue #5012 for details. + // FIXME: Unconfirmed whether this is still needed with SDL3. + case SDL_EVENT_WINDOW_MOUSE_LEAVE: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) + return false; + bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1; + return true; + } + case SDL_EVENT_WINDOW_FOCUS_GAINED: + case SDL_EVENT_WINDOW_FOCUS_LOST: { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) + return false; + io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED); + return true; + } + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + case SDL_EVENT_WINDOW_MOVED: + case SDL_EVENT_WINDOW_RESIZED: { + ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID); + if (viewport == NULL) + return false; + if (event->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) + viewport->PlatformRequestClose = true; + if (event->type == SDL_EVENT_WINDOW_MOVED) + viewport->PlatformRequestMove = true; + if (event->type == SDL_EVENT_WINDOW_RESIZED) + viewport->PlatformRequestResize = true; + return true; + } + case SDL_EVENT_GAMEPAD_ADDED: + case SDL_EVENT_GAMEPAD_REMOVED: { + bd->WantUpdateGamepadsList = true; + return true; + } + } + return false; +} + +static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window) { + viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window); + viewport->PlatformHandleRaw = nullptr; +#if defined(_WIN32) && !defined(__WINRT__) + viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty( + SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr); +#elif defined(__APPLE__) && defined(SDL_VIDEO_DRIVER_COCOA) + viewport->PlatformHandleRaw = SDL_GetPointerProperty( + SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr); +#endif +} + +static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context) { + ImGuiIO& io = ImGui::GetIO(); + IMGUI_CHECKVERSION(); + IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); + IM_UNUSED(sdl_gl_context); // Unused in this branch + + // Setup backend capabilities flags + ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)(); + io.BackendPlatformUserData = (void*)bd; + io.BackendPlatformName = "imgui_impl_sdl3"; + io.BackendFlags |= + ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values(optional) + io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests + // (optional, rarely used) + // (ImGuiBackendFlags_PlatformHasViewports may be set just below) + + bd->Window = window; + bd->WindowID = SDL_GetWindowID(window); + bd->Renderer = renderer; + + // SDL on Linux/OSX doesn't report events for unfocused windows (see + // https://github.com/ocornut/imgui/issues/4960) We will use 'MouseCanReportHoveredViewport' to + // set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame. +#ifndef __APPLE__ + bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState; +#else + bd->MouseCanReportHoveredViewport = false; +#endif + + // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and + // SDL_CaptureMouse() + // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a + // black-list) + bd->MouseCanUseGlobalState = false; + bd->MouseCanUseCapture = false; +#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE + const char* sdl_backend = SDL_GetCurrentVideoDriver(); + const char* capture_and_global_state_whitelist[] = {"windows", "cocoa", "x11", "DIVE", "VMAN"}; + for (const char* item : capture_and_global_state_whitelist) + if (strncmp(sdl_backend, item, strlen(item)) == 0) + bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true; +#endif + if (bd->MouseCanUseGlobalState) + io.BackendFlags |= + ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform + // side (optional) + + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText; + platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText; + platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData; + platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { + return SDL_OpenURL(url) == 0; + }; + + // Update monitor a first time during init + ImGui_ImplSDL3_UpdateMonitors(); + + // Gamepad handling + bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst; + bd->WantUpdateGamepadsList = true; + + // Load mouse cursors + bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT); + bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); + bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE); + bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = + SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = + SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = + SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = + SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER); + bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); + bd->MouseCursors[ImGuiMouseCursor_Progress] = + SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_PROGRESS); + bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = + SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED); + + // Set platform dependent data in viewport + // Our mouse update function expect PlatformHandle to be filled for the main viewport + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window); + + // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL + // doesn'temit the event. Without this, when clicking to gain focus, our widgets wouldn't + // activate even though they showed as hovered. (This is unfortunately a global SDL setting, so + // enabling it might have a side-effect on your application. It is unlikely to make a + // difference, but if your app absolutely needs to ignore the initial on-focus click: you can + // ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_WINDOWEVENT_FOCUS_GAINED) + SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); + + // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple + // windows(see #5710) + SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); + + // SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659 + SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0"); + + // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support + // multiple viewports. We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of + // #ifdef to avoid unused-function warnings. + if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports) + ImGui_ImplSDL3_InitMultiViewportSupport(window, sdl_gl_context); + + return true; +} + +// Should technically be a SDL_GLContext but due to typedef it is sane to keep it void* in public +// interface. +bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) { + return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context); +} + +bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window) { + if (!ImGui_ImplSDL3_Init(window, nullptr, nullptr)) + return false; + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + bd->UseVulkan = true; + return true; +} + +bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window) { +#if !defined(_WIN32) + IM_ASSERT(0 && "Unsupported"); +#endif + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window) { + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer) { + return ImGui_ImplSDL3_Init(window, renderer, nullptr); +} + +bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window) { + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +bool ImGui_ImplSDL3_InitForOther(SDL_Window* window) { + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +static void ImGui_ImplSDL3_CloseGamepads(); + +void ImGui_ImplSDL3_Shutdown() { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); + ImGuiIO& io = ImGui::GetIO(); + + ImGui_ImplSDL3_ShutdownMultiViewportSupport(); + + if (bd->ClipboardTextData) + SDL_free(bd->ClipboardTextData); + for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) + SDL_DestroyCursor(bd->MouseCursors[cursor_n]); + ImGui_ImplSDL3_CloseGamepads(); + + io.BackendPlatformName = nullptr; + io.BackendPlatformUserData = nullptr; + io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | + ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | + ImGuiBackendFlags_HasMouseHoveredViewport); + IM_DELETE(bd); +} + +// This code is incredibly messy because some of the functions we need for full viewport supportare +// not available in SDL < 2.0.4. +static void ImGui_ImplSDL3_UpdateMouseData() { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGuiIO& io = ImGui::GetIO(); + + // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or whenfocused + // (below) +#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE + // - 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. + // - 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. + if (bd->MouseCanUseCapture) { + bool want_capture = false; + for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++) + if (ImGui::IsMouseDragging(button_n, 1.0f)) + want_capture = true; + SDL_CaptureMouse(want_capture); + } + + SDL_Window* focused_window = SDL_GetKeyboardFocus(); + const bool is_app_focused = + (focused_window && + (bd->Window == focused_window || + ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL)); +#else + SDL_Window* focused_window = bd->Window; + 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 +#endif + if (is_app_focused) { + // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only + // when io.ConfigNavMoveSetMousePos is enabled by user) + if (io.WantSetMousePos) { +#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + SDL_WarpMouseGlobal(io.MousePos.x, io.MousePos.y); + else +#endif + SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y); + } + + // (Optional) Fallback to provide mouse position when focused (SDL_EVENT_MOUSE_MOTION + // already provides this when hovered or captured) + const bool is_relative_mouse_mode = SDL_GetWindowRelativeMouseMode(bd->Window); + if (bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode) { + // 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) 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) + float mouse_x, mouse_y; + int window_x, window_y; + SDL_GetGlobalMouseState(&mouse_x, &mouse_y); + if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)) { + SDL_GetWindowPosition(focused_window, &window_x, &window_y); + mouse_x -= window_x; + mouse_y -= window_y; + } + io.AddMousePosEvent((float)mouse_x, (float)mouse_y); + } + } + + // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the + // viewport the OS mouse cursor is hovering. If ImGuiBackendFlags_HasMouseHoveredViewport is not + // set by the backend, Dear imGui will ignore this field and infer the information using its + // flawed heuristic. + // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag. + // 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 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 by the backend, and use its flawed + // heuristic to guess the viewport behind. + // - [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). + if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport) { + ImGuiID mouse_viewport_id = 0; + if (ImGuiViewport* mouse_viewport = + ImGui_ImplSDL3_GetViewportForWindowID(bd->MouseWindowID)) + mouse_viewport_id = mouse_viewport->ID; + io.AddMouseViewportEvent(mouse_viewport_id); + } +} + +static void ImGui_ImplSDL3_UpdateMouseCursor() { + ImGuiIO& io = ImGui::GetIO(); + if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) + return; + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + + ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); + if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) { + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + SDL_HideCursor(); + } else { + // Show OS mouse cursor + SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] + ? bd->MouseCursors[imgui_cursor] + : bd->MouseCursors[ImGuiMouseCursor_Arrow]; + if (bd->MouseLastCursor != expected_cursor) { + SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113) + bd->MouseLastCursor = expected_cursor; + } + SDL_ShowCursor(); + } +} + +static void ImGui_ImplSDL3_CloseGamepads() { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual) + for (SDL_Gamepad* gamepad : bd->Gamepads) + SDL_CloseGamepad(gamepad); + bd->Gamepads.resize(0); +} + +void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, + SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count) { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGui_ImplSDL3_CloseGamepads(); + if (mode == ImGui_ImplSDL3_GamepadMode_Manual) { + IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0); + for (int n = 0; n < manual_gamepads_count; n++) + bd->Gamepads.push_back(manual_gamepads_array[n]); + } else { + IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0); + bd->WantUpdateGamepadsList = true; + } + bd->GamepadMode = mode; +} + +static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, + SDL_GamepadButton button_no) { + bool merged_value = false; + for (SDL_Gamepad* gamepad : bd->Gamepads) + merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0; + io.AddKeyEvent(key, merged_value); +} + +static inline float Saturate(float v) { + return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; +} +static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, + SDL_GamepadAxis axis_no, float v0, float v1) { + float merged_value = 0.0f; + for (SDL_Gamepad* gamepad : bd->Gamepads) { + float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0)); + if (merged_value < vn) + merged_value = vn; + } + io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value); +} + +static void ImGui_ImplSDL3_UpdateGamepads() { + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + + // Update list of gamepads to use + if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual) { + ImGui_ImplSDL3_CloseGamepads(); + int sdl_gamepads_count = 0; + SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count); + for (int n = 0; n < sdl_gamepads_count; n++) + if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n])) { + bd->Gamepads.push_back(gamepad); + if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst) + break; + } + bd->WantUpdateGamepadsList = false; + SDL_free(sdl_gamepads); + } + + io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; + if (bd->Gamepads.Size == 0) + return; + io.BackendFlags |= ImGuiBackendFlags_HasGamepad; + + // Update gamepad inputs + const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value. + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, + SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, + SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, + SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, + SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, + SDL_GAMEPAD_BUTTON_DPAD_LEFT); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, + SDL_GAMEPAD_BUTTON_DPAD_RIGHT); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, + SDL_GAMEPAD_BUTTON_DPAD_DOWN); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, + SDL_GAMEPAD_BUTTON_LEFT_SHOULDER); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, + SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, + 0.0f, 32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, + 0.0f, 32767); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, + -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, + +thumb_dead_zone, +32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, + -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, + +thumb_dead_zone, +32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, + -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, + +thumb_dead_zone, +32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, + -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, + +thumb_dead_zone, +32767); +} + +static void ImGui_ImplSDL3_UpdateMonitors() { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Monitors.resize(0); + bd->WantUpdateMonitors = false; + + int display_count; + SDL_DisplayID* displays = SDL_GetDisplays(&display_count); + for (int n = 0; n < display_count; n++) { + // Warning: the validity of monitor DPI information on Windows dependson the application DPI + // awareness settings, which generally needs to be set in the manifest or at runtime. + SDL_DisplayID display_id = displays[n]; + ImGuiPlatformMonitor monitor; + SDL_Rect r; + SDL_GetDisplayBounds(display_id, &r); + monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y); + monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h); + if (SDL_GetDisplayUsableBounds(display_id, &r) && r.w > 0 && r.h > 0) { + monitor.WorkPos = ImVec2((float)r.x, (float)r.y); + monitor.WorkSize = ImVec2((float)r.w, (float)r.h); + } + // FIXME-VIEWPORT: On MacOS SDL reports actual monitor DPI scale, ignoring OS + // configuration. We may want to set + // DpiScale to cocoa_window.backingScaleFactor here. + monitor.DpiScale = SDL_GetDisplayContentScale(display_id); + monitor.PlatformHandle = (void*)(intptr_t)n; + if (monitor.DpiScale <= 0.0f) + continue; // Some accessibility applications are declaring virtual monitors with a DPI + // of 0, see #7902. + platform_io.Monitors.push_back(monitor); + } + SDL_free(displays); +} + +void ImGui_ImplSDL3_NewFrame() { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + IM_ASSERT(bd != nullptr && + "Context or backend not initialized! Did you callImGui_ImplSDL3_Init()?"); + ImGuiIO& io = ImGui::GetIO(); + + // Setup display size (every frame to accommodate for window resizing) + int w, h; + int display_w, display_h; + SDL_GetWindowSize(bd->Window, &w, &h); + if (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_MINIMIZED) + w = h = 0; + SDL_GetWindowSizeInPixels(bd->Window, &display_w, &display_h); + io.DisplaySize = ImVec2((float)w, (float)h); + if (w > 0 && h > 0) + io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); + + // Update monitors +#ifdef WIN32 + bd->WantUpdateMonitors = true; // Keep polling under Windows to handle changes of work area + // whenresizing task-bar (#8415) +#endif + if (bd->WantUpdateMonitors) + ImGui_ImplSDL3_UpdateMonitors(); + + // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) + // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens + // in VMs and Emscripten, see #6189, #6114, #3644) + static Uint64 frequency = SDL_GetPerformanceFrequency(); + Uint64 current_time = SDL_GetPerformanceCounter(); + if (current_time <= bd->Time) + current_time = bd->Time + 1; + io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) + : (float)(1.0f / 60.0f); + bd->Time = current_time; + + if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && + bd->MouseButtonsDown == 0) { + bd->MouseWindowID = 0; + bd->MousePendingLeaveFrame = 0; + io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); + } + + // Our io.AddMouseViewportEvent() calls will only be valid when not capturing. + // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but + // testingfor payload reduces noise and potential side-effects. + if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr) + io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; + else + io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport; + + ImGui_ImplSDL3_UpdateMouseData(); + ImGui_ImplSDL3_UpdateMouseCursor(); + + // Update game controllers (if enabled and available) + ImGui_ImplSDL3_UpdateGamepads(); +} + +//-------------------------------------------------------------------------------------------------------- +// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT +// This is an _advanced_ and _optional_ feature, allowing the backend to create andhandle multiple +// viewports simultaneously. 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.. +//-------------------------------------------------------------------------------------------------------- + +// Helper structure we store in the void* PlatformUserData field of each ImGuiViewport to easily +// retrieveour backend data. +struct ImGui_ImplSDL3_ViewportData { + SDL_Window* Window; + SDL_Window* ParentWindow; + Uint32 WindowID; // Stored in ImGuiViewport::PlatformHandle. Use SDL_GetWindowFromID()to get + // SDL_Window* from Uint32 WindowID. + bool WindowOwned; + SDL_GLContext GLContext; + + ImGui_ImplSDL3_ViewportData() { + Window = ParentWindow = nullptr; + WindowID = 0; + WindowOwned = false; + GLContext = nullptr; + } + ~ImGui_ImplSDL3_ViewportData() { + IM_ASSERT(Window == nullptr && GLContext == nullptr); + } +}; + +static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewportID(ImGuiID viewport_id) { + if (viewport_id != 0) + if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id)) { + SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle; + return SDL_GetWindowFromID(window_id); + } + return nullptr; +} + +static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport) { + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)(); + viewport->PlatformUserData = vd; + + vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId); + + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui_ImplSDL3_ViewportData* main_viewport_data = + (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData; + + // Share GL resources with main context + bool use_opengl = (main_viewport_data->GLContext != nullptr); + SDL_GLContext backup_context = nullptr; + if (use_opengl) { + backup_context = SDL_GL_GetCurrentContext(); + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); + SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext); + } + + SDL_WindowFlags sdl_flags = 0; + sdl_flags |= SDL_WINDOW_HIDDEN; + sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0); + sdl_flags |= SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_HIGH_PIXEL_DENSITY; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_UTILITY : 0; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0; + vd->Window = + SDL_CreateWindow("No Title Yet", (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags); + SDL_SetWindowParent(vd->Window, vd->ParentWindow); + SDL_SetWindowPosition(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y); + vd->WindowOwned = true; + if (use_opengl) { + vd->GLContext = SDL_GL_CreateContext(vd->Window); + SDL_GL_SetSwapInterval(0); + } + if (use_opengl && backup_context) + SDL_GL_MakeCurrent(vd->Window, backup_context); + + ImGui_ImplSDL3_SetupPlatformHandles(viewport, vd->Window); +} + +static void ImGui_ImplSDL3_DestroyWindow(ImGuiViewport* viewport) { + if (ImGui_ImplSDL3_ViewportData* vd = + (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData) { + if (vd->GLContext && vd->WindowOwned) + SDL_GL_DestroyContext(vd->GLContext); + if (vd->Window && vd->WindowOwned) + SDL_DestroyWindow(vd->Window); + vd->GLContext = nullptr; + vd->Window = nullptr; + IM_DELETE(vd); + } + viewport->PlatformUserData = viewport->PlatformHandle = nullptr; +} + +static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; +#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP || \ + WINAPI_FAMILY == WINAPI_FAMILY_GAMES)) + HWND hwnd = (HWND)viewport->PlatformHandleRaw; + + // SDL hack: Show icon in task bar (#7989) + // Note: SDL_WINDOW_UTILITY can be used to control task bar visibility, but on Windows,it does + // not affect child windows. + if (!(viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)) { + LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); + ex_style |= WS_EX_APPWINDOW; + ex_style &= ~WS_EX_TOOLWINDOW; + ::ShowWindow(hwnd, SW_HIDE); + ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style); + } +#endif + + SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, + (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "0" : "1"); + SDL_ShowWindow(vd->Window); +} + +static void ImGui_ImplSDL3_UpdateWindow(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + + // Update SDL3 parent if it changed _after_ creation. + // This is for advanced apps that are manipulating ParentViewportID manually. + SDL_Window* new_parent = ImGui_ImplSDL3_GetSDLWindowFromViewportID(viewport->ParentViewportId); + if (new_parent != vd->ParentWindow) { + vd->ParentWindow = new_parent; + SDL_SetWindowParent(vd->Window, vd->ParentWindow); + } +} + +static ImVec2 ImGui_ImplSDL3_GetWindowPos(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + int x = 0, y = 0; + SDL_GetWindowPosition(vd->Window, &x, &y); + return ImVec2((float)x, (float)y); +} + +static void ImGui_ImplSDL3_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowPosition(vd->Window, (int)pos.x, (int)pos.y); +} + +static ImVec2 ImGui_ImplSDL3_GetWindowSize(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + int w = 0, h = 0; + SDL_GetWindowSize(vd->Window, &w, &h); + return ImVec2((float)w, (float)h); +} + +static void ImGui_ImplSDL3_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y); +} + +static void ImGui_ImplSDL3_SetWindowTitle(ImGuiViewport* viewport, const char* title) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowTitle(vd->Window, title); +} + +static void ImGui_ImplSDL3_SetWindowAlpha(ImGuiViewport* viewport, float alpha) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowOpacity(vd->Window, alpha); +} + +static void ImGui_ImplSDL3_SetWindowFocus(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_RaiseWindow(vd->Window); +} + +static bool ImGui_ImplSDL3_GetWindowFocus(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; +} + +static bool ImGui_ImplSDL3_GetWindowMinimized(ImGuiViewport* viewport) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0; +} + +static void ImGui_ImplSDL3_RenderWindow(ImGuiViewport* viewport, void*) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + if (vd->GLContext) + SDL_GL_MakeCurrent(vd->Window, vd->GLContext); +} + +static void ImGui_ImplSDL3_SwapBuffers(ImGuiViewport* viewport, void*) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + if (vd->GLContext) { + SDL_GL_MakeCurrent(vd->Window, vd->GLContext); + SDL_GL_SwapWindow(vd->Window); + } +} + +// Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the +// surface) SDL is graceful enough to _not_ need so we can safely include this. +#include +static int ImGui_ImplSDL3_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, + const void* vk_allocator, ImU64* out_vk_surface) { + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + (void)vk_allocator; + bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, + (VkAllocationCallbacks*)vk_allocator, + (VkSurfaceKHR*)out_vk_surface); + return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY +} + +static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context) { + // Register platform interface (will be coupled with a renderer interface) + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Platform_CreateWindow = ImGui_ImplSDL3_CreateWindow; + platform_io.Platform_DestroyWindow = ImGui_ImplSDL3_DestroyWindow; + platform_io.Platform_ShowWindow = ImGui_ImplSDL3_ShowWindow; + platform_io.Platform_UpdateWindow = ImGui_ImplSDL3_UpdateWindow; + platform_io.Platform_SetWindowPos = ImGui_ImplSDL3_SetWindowPos; + platform_io.Platform_GetWindowPos = ImGui_ImplSDL3_GetWindowPos; + platform_io.Platform_SetWindowSize = ImGui_ImplSDL3_SetWindowSize; + platform_io.Platform_GetWindowSize = ImGui_ImplSDL3_GetWindowSize; + platform_io.Platform_SetWindowFocus = ImGui_ImplSDL3_SetWindowFocus; + platform_io.Platform_GetWindowFocus = ImGui_ImplSDL3_GetWindowFocus; + platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL3_GetWindowMinimized; + platform_io.Platform_SetWindowTitle = ImGui_ImplSDL3_SetWindowTitle; + platform_io.Platform_RenderWindow = ImGui_ImplSDL3_RenderWindow; + platform_io.Platform_SwapBuffers = ImGui_ImplSDL3_SwapBuffers; + platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL3_SetWindowAlpha; + platform_io.Platform_CreateVkSurface = ImGui_ImplSDL3_CreateVkSurface; + + // Register main window handle (which is owned by the main application, not by us) + // 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. + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)(); + vd->Window = window; + vd->WindowID = SDL_GetWindowID(window); + vd->WindowOwned = false; + vd->GLContext = (SDL_GLContext)sdl_gl_context; + main_viewport->PlatformUserData = vd; + main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID; +} + +static void ImGui_ImplSDL3_ShutdownMultiViewportSupport() { + ImGui::DestroyPlatformWindows(); +} + +//----------------------------------------------------------------------------- + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/imgui/renderer/imgui_impl_sdl3_bpm.h b/src/imgui/renderer/imgui_impl_sdl3_bpm.h new file mode 100644 index 000000000..c52c31bc2 --- /dev/null +++ b/src/imgui/renderer/imgui_impl_sdl3_bpm.h @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +// Based on imgui_impl_sdl3.h from Dear ImGui repository + +#pragma once +#include "imgui.h" // IMGUI_IMPL_API +#ifndef IMGUI_DISABLE + +struct SDL_Window; +struct SDL_Renderer; +struct SDL_Gamepad; +typedef union SDL_Event SDL_Event; + +// Follow "Getting Started" link and check examples/ folder to learn about using backends! +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForOther(SDL_Window* window); +IMGUI_IMPL_API void ImGui_ImplSDL3_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplSDL3_NewFrame(); +IMGUI_IMPL_API bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event); + +// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad.You +// may override this. When using manual mode, caller is responsible for opening/closing gamepad. +enum ImGui_ImplSDL3_GamepadMode { + ImGui_ImplSDL3_GamepadMode_AutoFirst, + ImGui_ImplSDL3_GamepadMode_AutoAll, + ImGui_ImplSDL3_GamepadMode_Manual +}; + +IMGUI_IMPL_API void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, + SDL_Gamepad** manual_gamepads_array = nullptr, + int manual_gamepads_count = -1); + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/imgui/renderer/imgui_impl_sdlrenderer3.cpp b/src/imgui/renderer/imgui_impl_sdlrenderer3.cpp new file mode 100644 index 000000000..600e7e243 --- /dev/null +++ b/src/imgui/renderer/imgui_impl_sdlrenderer3.cpp @@ -0,0 +1,289 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +// Based on imgui_impl_sdlrenderer3.cpp from Dear ImGui repository + +#include "imgui.h" +#ifndef IMGUI_DISABLE +#include // intptr_t +#include "imgui_impl_sdlrenderer3.h" + +// Clang warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored \ + "-Wsign-conversion" // warning: implicit conversion changes signedness +#endif + +// SDL +#include +#if !SDL_VERSION_ATLEAST(3, 0, 0) +#error This backend requires SDL 3.0.0+ +#endif + +// SDL_Renderer data +struct ImGui_ImplSDLRenderer3_Data { + SDL_Renderer* Renderer; // Main viewport's renderer + SDL_Texture* FontTexture; + ImVector ColorBuffer; + + ImGui_ImplSDLRenderer3_Data() { + memset((void*)this, 0, sizeof(*this)); + } +}; + +// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui +// contexts It is STRONGLY preferred that you use docking branch with multi-viewports (==single Dear +// ImGui context + multiple windows) instead of multiple Dear ImGui contexts. +static ImGui_ImplSDLRenderer3_Data* ImGui_ImplSDLRenderer3_GetBackendData() { + return ImGui::GetCurrentContext() + ? (ImGui_ImplSDLRenderer3_Data*)ImGui::GetIO().BackendRendererUserData + : nullptr; +} + +// Functions +bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer) { + ImGuiIO& io = ImGui::GetIO(); + IMGUI_CHECKVERSION(); + IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); + IM_ASSERT(renderer != nullptr && "SDL_Renderer not initialized!"); + + // Setup backend capabilities flags + ImGui_ImplSDLRenderer3_Data* bd = IM_NEW(ImGui_ImplSDLRenderer3_Data)(); + io.BackendRendererUserData = (void*)bd; + io.BackendRendererName = "imgui_impl_sdlrenderer3"; + io.BackendFlags |= + ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset + // field, allowing for large meshes. + + bd->Renderer = renderer; + + return true; +} + +void ImGui_ImplSDLRenderer3_Shutdown() { + ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); + IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); + ImGuiIO& io = ImGui::GetIO(); + + ImGui_ImplSDLRenderer3_DestroyDeviceObjects(); + + io.BackendRendererName = nullptr; + io.BackendRendererUserData = nullptr; + io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; + IM_DELETE(bd); +} + +static void ImGui_ImplSDLRenderer3_SetupRenderState(SDL_Renderer* renderer) { + // Clear out any viewports and cliprect set by the user + // FIXME: Technically speaking there are lots of other things we could backup/setup/restore + // during our render process. + SDL_SetRenderViewport(renderer, nullptr); + SDL_SetRenderClipRect(renderer, nullptr); +} + +void ImGui_ImplSDLRenderer3_NewFrame() { + ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); + IM_ASSERT(bd != nullptr && + "Context or backend not initialized! Did you call ImGui_ImplSDLRenderer3_Init()?"); + + if (!bd->FontTexture) + ImGui_ImplSDLRenderer3_CreateDeviceObjects(); +} + +// https://github.com/libsdl-org/SDL/issues/9009 +static int SDL_RenderGeometryRaw8BitColor(SDL_Renderer* renderer, ImVector& colors_out, + SDL_Texture* texture, const float* xy, int xy_stride, + const SDL_Color* color, int color_stride, const float* uv, + int uv_stride, int num_vertices, const void* indices, + int num_indices, int size_indices) { + const Uint8* color2 = (const Uint8*)color; + colors_out.resize(num_vertices); + SDL_FColor* color3 = colors_out.Data; + for (int i = 0; i < num_vertices; i++) { + color3[i].r = color->r / 255.0f; + color3[i].g = color->g / 255.0f; + color3[i].b = color->b / 255.0f; + color3[i].a = color->a / 255.0f; + color2 += color_stride; + color = (const SDL_Color*)color2; + } + return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color3, sizeof(*color3), uv, + uv_stride, num_vertices, indices, num_indices, size_indices); +} + +void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, SDL_Renderer* renderer) { + ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); + + // If there's a scale factor set by the user, use that instead + // If the user has specified a scale factor to SDL_Renderer already via SDL_RenderSetScale(), + // SDL will scale whatever we pass to SDL_RenderGeometryRaw() by that scale factor. In that case + // we don't want to be also scaling it ourselves here. + float rsx = 1.0f; + float rsy = 1.0f; + SDL_GetRenderScale(renderer, &rsx, &rsy); + ImVec2 render_scale; + render_scale.x = (rsx == 1.0f) ? draw_data->FramebufferScale.x : 1.0f; + render_scale.y = (rsy == 1.0f) ? draw_data->FramebufferScale.y : 1.0f; + + // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != + // framebuffer coordinates) + int fb_width = (int)(draw_data->DisplaySize.x * render_scale.x); + int fb_height = (int)(draw_data->DisplaySize.y * render_scale.y); + if (fb_width == 0 || fb_height == 0) + return; + + // Backup SDL_Renderer state that will be modified to restore it afterwards + struct BackupSDLRendererState { + SDL_Rect Viewport; + bool ViewportEnabled; + bool ClipEnabled; + SDL_Rect ClipRect; + }; + BackupSDLRendererState old = {}; + old.ViewportEnabled = SDL_RenderViewportSet(renderer); + old.ClipEnabled = SDL_RenderClipEnabled(renderer); + SDL_GetRenderViewport(renderer, &old.Viewport); + SDL_GetRenderClipRect(renderer, &old.ClipRect); + + // Setup desired state + ImGui_ImplSDLRenderer3_SetupRenderState(renderer); + + // Setup render state structure (for callbacks and custom texture bindings) + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + ImGui_ImplSDLRenderer3_RenderState render_state; + render_state.Renderer = renderer; + platform_io.Renderer_RenderState = &render_state; + + // Will project scissor/clipping rectangles into framebuffer space + ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports + ImVec2 clip_scale = render_scale; + + // Render command lists + for (int n = 0; n < draw_data->CmdListsCount; n++) { + const ImDrawList* draw_list = draw_data->CmdLists[n]; + const ImDrawVert* vtx_buffer = draw_list->VtxBuffer.Data; + const ImDrawIdx* idx_buffer = draw_list->IdxBuffer.Data; + + for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) { + const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; + if (pcmd->UserCallback) { + // User callback, registered via ImDrawList::AddCallback() + // (ImDrawCallback_ResetRenderState is a special callback value used by the user to + // request the renderer to reset render state.) + if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) + ImGui_ImplSDLRenderer3_SetupRenderState(renderer); + else + pcmd->UserCallback(draw_list, pcmd); + } else { + // Project scissor/clipping rectangles into framebuffer space + ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, + (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); + ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, + (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); + if (clip_min.x < 0.0f) { + clip_min.x = 0.0f; + } + if (clip_min.y < 0.0f) { + clip_min.y = 0.0f; + } + if (clip_max.x > (float)fb_width) { + clip_max.x = (float)fb_width; + } + if (clip_max.y > (float)fb_height) { + clip_max.y = (float)fb_height; + } + if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) + continue; + + SDL_Rect r = {(int)(clip_min.x), (int)(clip_min.y), (int)(clip_max.x - clip_min.x), + (int)(clip_max.y - clip_min.y)}; + SDL_SetRenderClipRect(renderer, &r); + + const float* xy = + (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + + offsetof(ImDrawVert, pos)); + const float* uv = + (const float*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + + offsetof(ImDrawVert, uv)); + const SDL_Color* color = + (const SDL_Color*)(const void*)((const char*)(vtx_buffer + pcmd->VtxOffset) + + offsetof(ImDrawVert, col)); // SDL 2.0.19+ + + // Bind texture, Draw + SDL_Texture* tex = (SDL_Texture*)pcmd->GetTexID(); + SDL_RenderGeometryRaw8BitColor( + renderer, bd->ColorBuffer, tex, xy, (int)sizeof(ImDrawVert), color, + (int)sizeof(ImDrawVert), uv, (int)sizeof(ImDrawVert), + draw_list->VtxBuffer.Size - pcmd->VtxOffset, idx_buffer + pcmd->IdxOffset, + pcmd->ElemCount, sizeof(ImDrawIdx)); + } + } + } + platform_io.Renderer_RenderState = nullptr; + + // Restore modified SDL_Renderer state + SDL_SetRenderViewport(renderer, old.ViewportEnabled ? &old.Viewport : nullptr); + SDL_SetRenderClipRect(renderer, old.ClipEnabled ? &old.ClipRect : nullptr); +} + +// Called by Init/NewFrame/Shutdown +bool ImGui_ImplSDLRenderer3_CreateFontsTexture() { + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); + + // Build texture atlas + unsigned char* pixels; + int width, height; + io.Fonts->GetTexDataAsRGBA32( + &pixels, &width, + &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) + // because it is more likely to be compatible with user's existing shaders. If + // your ImTextureId represent a higher-level concept than just a GL texture id, + // consider calling GetTexDataAsAlpha8() instead to save on GPU memory. + + // Upload texture to graphics system + // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= + // ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow + // point/nearest sampling) + bd->FontTexture = SDL_CreateTexture(bd->Renderer, SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STATIC, width, height); + if (bd->FontTexture == nullptr) { + SDL_Log("error creating texture"); + return false; + } + SDL_UpdateTexture(bd->FontTexture, nullptr, pixels, 4 * width); + SDL_SetTextureBlendMode(bd->FontTexture, SDL_BLENDMODE_BLEND); + SDL_SetTextureScaleMode(bd->FontTexture, SDL_SCALEMODE_LINEAR); + + // Store our identifier + io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture); + + return true; +} + +void ImGui_ImplSDLRenderer3_DestroyFontsTexture() { + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplSDLRenderer3_Data* bd = ImGui_ImplSDLRenderer3_GetBackendData(); + if (bd->FontTexture) { + io.Fonts->SetTexID(0); + SDL_DestroyTexture(bd->FontTexture); + bd->FontTexture = nullptr; + } +} + +bool ImGui_ImplSDLRenderer3_CreateDeviceObjects() { + return ImGui_ImplSDLRenderer3_CreateFontsTexture(); +} + +void ImGui_ImplSDLRenderer3_DestroyDeviceObjects() { + ImGui_ImplSDLRenderer3_DestroyFontsTexture(); +} + +//----------------------------------------------------------------------------- + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/imgui/renderer/imgui_impl_sdlrenderer3.h b/src/imgui/renderer/imgui_impl_sdlrenderer3.h new file mode 100644 index 000000000..c207c66a3 --- /dev/null +++ b/src/imgui/renderer/imgui_impl_sdlrenderer3.h @@ -0,0 +1,33 @@ +// SPDX-FileCopyrightText: Copyright 2026 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +// Based on imgui_impl_sdlrenderer3.h from Dear ImGui repository + +#pragma once +#include "imgui.h" // IMGUI_IMPL_API +#ifndef IMGUI_DISABLE + +struct SDL_Renderer; + +// Follow "Getting Started" link and check examples/ folder to learn about using backends! +IMGUI_IMPL_API bool ImGui_ImplSDLRenderer3_Init(SDL_Renderer* renderer); +IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_NewFrame(); +IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_RenderDrawData(ImDrawData* draw_data, + SDL_Renderer* renderer); + +// Called by Init/NewFrame/Shutdown +IMGUI_IMPL_API bool ImGui_ImplSDLRenderer3_CreateFontsTexture(); +IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_DestroyFontsTexture(); +IMGUI_IMPL_API bool ImGui_ImplSDLRenderer3_CreateDeviceObjects(); +IMGUI_IMPL_API void ImGui_ImplSDLRenderer3_DestroyDeviceObjects(); + +// [BETA] Selected render state data shared with callbacks. +// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the +// ImGui_ImplSDLRenderer3_RenderDrawData() call. (Please open an issue if you feel you need access +// to more data) +struct ImGui_ImplSDLRenderer3_RenderState { + SDL_Renderer* Renderer; +}; + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/main.cpp b/src/main.cpp index fb9ff3078..826c8ab03 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,8 @@ #include "core/file_sys/fs.h" #include "core/ipc/ipc.h" #include "emulator.h" +#include "imgui/big_picture.h" + #ifdef _WIN32 #include #endif @@ -73,6 +75,7 @@ int main(int argc, char* argv[]) { bool configClean = false; bool configGlobal = false; bool logAppend = false; + bool bigPicture = false; std::optional addGameFolder; std::optional setAddonFolder; @@ -84,6 +87,8 @@ int main(int argc, char* argv[]) { app.add_flag("-i,--ignore-game-patch", ignoreGamePatch, "Disable automatic loading of game patches"); + app.add_flag("-b,--big-picture", bigPicture, "Start in Big Picture Mode"); + // FULLSCREEN: behavior-identical app.add_option("-f,--fullscreen", fullscreenStr, "Fullscreen mode (true|false)"); @@ -140,7 +145,7 @@ int main(int argc, char* argv[]) { return 0; } - if (!gamePath.has_value()) { + if (!gamePath.has_value() && !bigPicture) { if (!gameArgs.empty()) { gamePath = gameArgs.front(); gameArgs.erase(gameArgs.begin()); @@ -200,7 +205,7 @@ int main(int argc, char* argv[]) { break; } } - if (!found) { + if (!found && !bigPicture) { std::cerr << "Error: Game ID or file path not found: " << *gamePath << "\n"; return 1; } @@ -209,10 +214,14 @@ int main(int argc, char* argv[]) { if (waitPid) Core::Debugger::WaitForPid(*waitPid); - auto* emulator = Common::Singleton::Instance(); - emulator->executableName = argv[0]; - emulator->waitForDebuggerBeforeRun = waitForDebugger; - emulator->Run(ebootPath, gameArgs, overrideRoot); + if (bigPicture) { + BigPictureMode::Launch(); + } else { + auto* emulator = Common::Singleton::Instance(); + emulator->executableName = argv[0]; + emulator->waitForDebuggerBeforeRun = waitForDebugger; + emulator->Run(ebootPath, gameArgs, overrideRoot); + } return 0; }