From ee3d37e237c69c6384ef57ae3b4168887599e2ff Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:03:36 -0500 Subject: [PATCH 01/22] feat(streaming): add local stream controls and input support --- README.md | 88 +- cmake/nxdk.cmake | 29 +- cmake/sources.cmake | 3 + cmake/stage-lucide.cmake | 3 +- cmake/xbox-build.cmake | 3 + src/app/client_state.cpp | 2 + src/app/client_state.h | 4 + src/app/settings_storage.cpp | 16 +- src/app/settings_storage.h | 2 + src/input/navigation_input.cpp | 4 +- src/input/stream_exit.cpp | 2 +- src/input/stream_exit.h | 8 +- src/input/stream_input.cpp | 369 +++++++ src/input/stream_input.h | 188 ++++ src/input/stream_menu.cpp | 451 ++++++++ src/input/stream_menu.h | 159 +++ src/input/ui_input.cpp | 69 ++ src/input/ui_input.h | 126 +++ src/input/usb_hid.cpp | 103 ++ src/input/usb_hid.h | 86 ++ src/main.cpp | 2 + src/platform/usb_input.cpp | 213 ++++ src/startup/storage_paths.cpp | 7 + src/startup/storage_paths.h | 7 + src/streaming/ffmpeg_stream_backend.cpp | 72 +- src/streaming/ffmpeg_stream_backend.h | 46 +- src/streaming/pvideo_presenter.cpp | 2 +- src/streaming/pvideo_presenter.h | 2 +- src/streaming/session.cpp | 535 +-------- src/streaming/session.h | 4 +- src/streaming/stream_controls.cpp | 1084 +++++++++++++++++++ src/streaming/stream_controls.h | 200 ++++ src/streaming/video_overlay.cpp | 352 +++++- src/streaming/video_overlay.h | 116 +- src/ui/asset_path.cpp | 18 + src/ui/asset_path.h | 16 + src/ui/dialog_surface.h | 48 + src/ui/shell_screen.cpp | 390 +++---- src/ui/shell_view.cpp | 85 ++ src/ui/shell_view.h | 2 + src/ui/svg_assets.cpp | 100 ++ src/ui/svg_assets.h | 20 + tests/unit/app/settings_storage_test.cpp | 38 +- tests/unit/input/navigation_input_test.cpp | 19 +- tests/unit/input/stream_exit_test.cpp | 4 +- tests/unit/input/stream_input_test.cpp | 276 +++++ tests/unit/input/stream_menu_test.cpp | 253 +++++ tests/unit/input/ui_input_test.cpp | 134 +++ tests/unit/input/usb_hid_test.cpp | 115 ++ tests/unit/startup/storage_paths_test.cpp | 20 + tests/unit/streaming/video_overlay_test.cpp | 484 +++++++++ tests/unit/ui/asset_path_test.cpp | 19 + tests/unit/ui/input_hints_test.cpp | 103 ++ xbe/assets/README.md | 10 + xbe/assets/icons/button-guide.svg | 1 + xbe/assets/icons/button-left-stick.svg | 1 + xbe/assets/icons/button-right-stick.svg | 1 + xbe/assets/icons/button-share.svg | 1 + 58 files changed, 5712 insertions(+), 803 deletions(-) create mode 100644 src/input/stream_input.cpp create mode 100644 src/input/stream_input.h create mode 100644 src/input/stream_menu.cpp create mode 100644 src/input/stream_menu.h create mode 100644 src/input/ui_input.cpp create mode 100644 src/input/ui_input.h create mode 100644 src/input/usb_hid.cpp create mode 100644 src/input/usb_hid.h create mode 100644 src/platform/usb_input.cpp create mode 100644 src/streaming/stream_controls.cpp create mode 100644 src/streaming/stream_controls.h create mode 100644 src/ui/asset_path.cpp create mode 100644 src/ui/asset_path.h create mode 100644 src/ui/dialog_surface.h create mode 100644 src/ui/svg_assets.cpp create mode 100644 src/ui/svg_assets.h create mode 100644 tests/unit/input/stream_input_test.cpp create mode 100644 tests/unit/input/stream_menu_test.cpp create mode 100644 tests/unit/input/ui_input_test.cpp create mode 100644 tests/unit/input/usb_hid_test.cpp create mode 100644 tests/unit/startup/storage_paths_test.cpp create mode 100644 tests/unit/ui/asset_path_test.cpp create mode 100644 tests/unit/ui/input_hints_test.cpp create mode 100644 xbe/assets/icons/button-guide.svg create mode 100644 xbe/assets/icons/button-left-stick.svg create mode 100644 xbe/assets/icons/button-right-stick.svg create mode 100644 xbe/assets/icons/button-share.svg diff --git a/README.md b/README.md index 47be3c5..28153f8 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,12 @@ captions, and a blue focus ring shows which card will open. Running apps carry a Missing artwork uses Lucide desktop or gamepad placeholders. Use the D-pad or left stick to navigate, **A** to select, **B** to go back and **Y** for host or app actions. Move up from the first PC row to reach Settings, Support and Add Host. +Keyboard arrows support held navigation with the same initial delay and repeat rate as the gamepad. +Use **Enter** to select, **Esc** to go back, and **M** for host or app actions. Helper glyphs switch to +keyboard keycaps when a key is pressed and return to controller glyphs when a button or deliberate +stick movement is used. Small stick drift and device connection events do not change the helpers. +On the hosts page, **Alt + F4** exits the client. **PgUp / PgDn** scroll the log viewer; +hold **Shift** for faster scrolling. **Backspace** dismisses a notification or changes the log pane. PCs use grayscale moon artwork inspired by One: a full moon for paired online PCs, a half moon for online PCs awaiting pairing, and a crescent for offline PCs. A separate orbit shows controller focus. @@ -78,9 +84,83 @@ Bitrates are displayed in Mbps. You can then adjust **Stream Bitrate** manually; until resolution or FPS changes again. These are starting values for image quality and bandwidth, not a guarantee of Xbox decoding performance. -To quit a stream, press **Back/Select + Black + White (LB + RB)** together on any connected controller. -The client displays **Stopping stream...**, then a stream summary. Release the buttons, then press a -button to return to the menu. +Press **Back/Select + Black + White (LB + RB)** together on any connected controller to open stream +controls. Release the buttons, select a choice with either stick, then press **A** to choose it. +**B** resumes playback. A USB keyboard can open the same menu with **Ctrl + Alt + Shift + Q**; +use **Tab** to cycle through all choices, arrows to select the left wheel, **Enter** to choose, and +**Esc** to go back. Opening local controls releases held host input. Center the sticks and release +the buttons before resuming gameplay. The same shortcut returns to the wheels from the keyboard, +logs or quit confirmation. Lucide icons identify each action, and either stick updates the highlight +continuously as it moves. A translucent dark backdrop dims the live stream behind the wheels; the wheel sectors stay opaque +for readability. Guide and Share use Xbox Series-style symbols. + +| Stick | Up | Right | Down | Left | +| ----- | -------- | ---------- | ---------- | ---------------------- | +| Left | Keyboard | Mouse mode | View logs | Quit stream | +| Right | Guide | Share | Screenshot | Enable / disable stats | + +**Quit stream** asks for confirmation before disconnecting. The client displays **Stopping stream...**, +then a stream summary. Release the buttons, then press a button to return to the library. +The host application keeps running. + +The on-screen keyboard uses an Xbox One-style layout on a solid dark panel: a number row, +QWERTY keys, a wide space bar, tall side controls, and an outlined focus key. Use **D-pad / left stick** +to move, **A** to type, **X** for Backspace, **Y** for Space, and **Start** for host Enter. +**Right stick click** applies Shift to the next key; **left stick click** toggles persistent Caps. +**White / Black (LB / RB)** move the host text cursor left / right. **LT** cycles through letters, +symbols, and PC keys. The symbols page groups numbers and punctuation; PC keys include F1–F12, +navigation keys, Ctrl / Alt / Win toggles, and shortcuts for Copy, Paste, Cut, Undo, Redo, Select All, +Alt+Tab, Alt+F4, Ctrl+Alt+Delete, Win+D, Win+E and Win+R. Focus a shortcut to see its full combination +above the keys. Named shortcuts send their own modifier combination. **RT** moves the panel between +the top and bottom edges; the placement is saved when the stream ends. **B** returns to the wheels. +Leaving the keyboard clears held modifiers. Keys use the host's US keyboard layout. +Controller shortcut icons retain the same colors as the main menus, with smaller glyphs inside keys. +When using a physical keyboard, use **Tab** to change page, **Shift** for one-shot Shift, **Caps Lock** +to toggle Caps, **F2** to move the panel, **Space / Backspace** to send those keys, and **Esc** to return. +In the numeric address/port keypad, **Space** types the focused key and **Enter** accepts the field; +**PgUp / PgDn** change the address section. + +Stream logs appear in a live side panel with severity colors and a scrollbar. Scroll upward to read +older entries; return to the bottom to follow new entries. The disconnect dialog uses the same modal +surface as the main menus, starts on **Cancel**, and leaves the host app running after disconnecting. +Returning from the stream resets local keyboard/controller holds before library navigation resumes. + +Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Change +`screenshot_directory` in the `[streaming]` section of Moonlight's `moonlight.toml` to use another writable +folder; the folder is created when the first screenshot is saved. An empty value restores the default. +Use a TOML literal string for Xbox paths, for example `screenshot_directory = 'F:\Screenshots'`. +The same section stores `keyboard_dock_top = false` (bottom) or `true` (top). + +Mouse mode uses the invoking controller's **left stick** for movement and **A / B** for left / right +clicks. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; +other gamepads continue to work. Physical keyboard and mouse input can be used alongside virtual input. + +Up to four gamepads are sent as separate controllers to Sunshine. Disconnecting a controller releases +its slot without renumbering the remaining players. Newly connected pads reuse free slots. +Standard USB boot keyboards and three-button boot mice are supported through Xbox-to-USB adapters +attached to the controller ports. Devices must support HID boot protocol; report-only devices and +nonstandard gaming-device reports are not supported. Boot mouse wheel and extra buttons are not +decoded. When SDL supplies wheel or extra mouse button events, they are forwarded to the host. +USB disconnects release held keys and mouse buttons. Console USB compatibility and host controller +creation should be checked with the actual devices and Sunshine configuration in use. + +For xemu, open its Monitor with **~** and enter `device_add usb-kbd,id=kbd` or +`device_add usb-mouse,id=mouse` to attach an emulated USB device to an available port. +Remove them with `device_del kbd` and `device_del mouse`. See the +[xemu keyboard setup guide](https://xemu.app/docs/keyboard/) for explicit controller-port selection. + +When Sunshine and xemu run on the same PC, prevent Sunshine's virtual controllers from being attached +to xemu. They can appear when streaming starts. If they are visible to xemu, disable automatic controller +binding in **Input**, manually assign the physical controller, and remove any saved virtual-controller +assignments. Leave keyboard/mouse ports unassigned. Otherwise xemu can attach a virtual gamepad to a +port already occupied by a USB keyboard or mouse and exit with +`usb_claim_port(): ... not found (in use?)`. + +**View logs** shows a snapshot of recent runtime entries; scroll with up/down and reopen it to refresh. +**Guide** and **Share** send controller button presses to the host. Share behavior depends on the host's +selected virtual controller and operating system. **Screenshot** saves the clean decoded frame as a BMP +under the title's `E:\UDATA\` save directory and displays the saved path. Screenshots exclude local menus +and statistics. Capture requires a decoded frame and writable save storage. Enable **Settings → Display → Performance Stats** for a live panel during playback and additional performance details in the summary. The panel works with GPU and software presentation, refreshes @@ -89,6 +169,8 @@ decoded and rendered FPS, video bitrate, timing, frame drops, buffering and the The stream header shows the requested FPS; the three FPS counters show measured rates. Network latency is round-trip time. Present measures CPU frame preparation and submission, including any scanout wait. +The stream menu can toggle these statistics for the current session; the saved setting sets the +initial state of the next session. Video decoding runs on the CPU. Optional experimental GPU presentation accelerates color conversion and scaling, with software fallback for unsupported frames or presentation failures. GPU diff --git a/cmake/nxdk.cmake b/cmake/nxdk.cmake index bef9761..22ca385 100644 --- a/cmake/nxdk.cmake +++ b/cmake/nxdk.cmake @@ -230,10 +230,36 @@ function(_moonlight_prepare_nxdk_tools nxdk_dir cxbe_path source_dir build_dir o ) endfunction() +# Install USB hooks into the owned SDK copy and fingerprint the resulting sources. +function(_moonlight_prepare_usb_input_hooks nxdk_dir out_signature) + # The vendored SDL Xbox event pump is a stub. Install small hooks only in + # the owned SDK copy; the project adapter implements boot HID and SDL events. + file(READ "${MOONLIGHT_NXDK_DIR}/lib/sdl/SDL2/src/joystick/xbox/SDL_xboxjoystick.c" joystick_source) + string(CONCAT initialize_hook + "usbh_xid_init();\n" + " extern void moonlight_usb_input_initialize(void);\n" + " moonlight_usb_input_initialize();") + string(REPLACE "usbh_xid_init();" "${initialize_hook}" + joystick_source "${joystick_source}") + file(CONFIGURE OUTPUT "${nxdk_dir}/lib/sdl/SDL2/src/joystick/xbox/SDL_xboxjoystick.c" # cmake-lint: disable=E1126 + CONTENT "${joystick_source}" @ONLY) + file(READ "${MOONLIGHT_NXDK_DIR}/lib/sdl/SDL2/src/video/xbox/SDL_xbevents.c" events_source) + string(REPLACE "/* do nothing. */" + "extern void moonlight_usb_input_pump(void);\n moonlight_usb_input_pump();" + events_source "${events_source}") + file(CONFIGURE OUTPUT "${nxdk_dir}/lib/sdl/SDL2/src/video/xbox/SDL_xbevents.c" # cmake-lint: disable=E1126 + CONTENT "${events_source}" @ONLY) + string(SHA256 signature "${joystick_source}\n${events_source}\nNXDK_USB_ENABLE_HID=y") + + set(${out_signature} "${signature}" PARENT_SCOPE) +endfunction() + # Bootstrap the vendored nxdk outputs required by the host-first Moonlight build. function(moonlight_prepare_nxdk nxdk_dir state_dir) file(MAKE_DIRECTORY "${state_dir}") + _moonlight_prepare_usb_input_hooks("${nxdk_dir}" input_signature) + file(READ "${nxdk_dir}/.moonlight-source.signature" nxdk_revision) set(signature_inputs "NXDK_DIR=${nxdk_dir}" @@ -241,6 +267,7 @@ function(moonlight_prepare_nxdk nxdk_dir state_dir) "HOST_SYSTEM=${CMAKE_HOST_SYSTEM_NAME}" "NXDK_TOOLCHAIN=${nxdk_dir}/share/toolchain-nxdk.cmake" "NXDK_ENV_MODE=cmake-driver" + "USB_INPUT=${input_signature}" ) list(JOIN signature_inputs "\n" signature_text) string(SHA256 signature "${signature_text}") @@ -293,7 +320,7 @@ function(moonlight_prepare_nxdk nxdk_dir state_dir) if(need_prepare_libraries) message(STATUS "Preparing nxdk libraries at ${nxdk_dir}") - _moonlight_run_nxdk_make("${nxdk_dir}" "nxdk bootstrap" NXDK_ONLY=y -j4) + _moonlight_run_nxdk_make("${nxdk_dir}" "nxdk bootstrap" NXDK_ONLY=y NXDK_USB_ENABLE_HID=y -j4) else() message(STATUS "Using existing nxdk library outputs from ${nxdk_dir}") endif() diff --git a/cmake/sources.cmake b/cmake/sources.cmake index fd5cfd1..47075a8 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -19,7 +19,10 @@ set(MOONLIGHT_TEST_EXCLUDED_SOURCES "${MOONLIGHT_SOURCE_ROOT}/src/streaming/ffmpeg_stream_backend.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/startup/memory_stats.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/streaming/session.cpp" + "${MOONLIGHT_SOURCE_ROOT}/src/streaming/stream_controls.cpp" + "${MOONLIGHT_SOURCE_ROOT}/src/platform/usb_input.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/ui/shell_screen.cpp" + "${MOONLIGHT_SOURCE_ROOT}/src/ui/svg_assets.cpp" ) set(MOONLIGHT_HOST_TESTABLE_SOURCES ${MOONLIGHT_SOURCES}) diff --git a/cmake/stage-lucide.cmake b/cmake/stage-lucide.cmake index 1aacd89..6a3c6ff 100644 --- a/cmake/stage-lucide.cmake +++ b/cmake/stage-lucide.cmake @@ -6,7 +6,8 @@ endif() file(MAKE_DIRECTORY "${LUCIDE_ASSET_DIR}/icons" "${LUCIDE_ASSET_DIR}/licenses") foreach(icon IN ITEMS settings circle-question-mark circle-plus monitor gamepad-2 scroll-text rotate-ccw - network hash activity key-round save loader-circle circle-check circle-x info) + network hash activity key-round save loader-circle circle-check circle-x info + keyboard mouse power panels-top-left share-2 camera chart-no-axes-combined) set(_icon_source "${LUCIDE_SOURCE_DIR}/icons/${icon}.svg") if(NOT EXISTS "${_icon_source}") message(FATAL_ERROR "Required Lucide icon not found: ${_icon_source}") diff --git a/cmake/xbox-build.cmake b/cmake/xbox-build.cmake index a9ad293..bfb1429 100644 --- a/cmake/xbox-build.cmake +++ b/cmake/xbox-build.cmake @@ -84,6 +84,8 @@ target_include_directories(${CMAKE_PROJECT_NAME} "${MOONLIGHT_NXDK_NET_INCLUDE_DIR}" "${MOONLIGHT_NXDK_LIBC_EXTENSIONS_DIR}" "${MOONLIGHT_NXDK_LWIP_POSIX_COMPAT_DIR}" + "${NXDK_DIR}/lib/usb/libusbohci/inc" + "${NXDK_DIR}/lib/usb/libusbohci_xbox" ) target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC @@ -111,6 +113,7 @@ target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE XBOX NXDK + USBH_USE_EXTERNAL_CONFIG="usbh_config_xbox.h" TOML_EXCEPTIONS=0 TOML_ENABLE_WINDOWS_COMPAT=0) target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 20c1668..53ab593 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -1607,6 +1607,8 @@ namespace app { state.settings.playAudioOnPc = false; state.settings.showPerformanceStats = false; state.settings.playAudioOnXbox = true; + state.settings.screenshotDirectory.clear(); + state.settings.keyboardDockTop = false; state.settings.dirty = false; state.settings.savedFilesDirty = true; return state; diff --git a/src/app/client_state.h b/src/app/client_state.h index a601dfa..8f75e25 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -18,6 +18,7 @@ #include "src/app/host_records.h" #include "src/app/pairing_flow.h" #include "src/input/navigation_input.h" +#include "src/input/ui_input.h" #include "src/logging/logger.h" #include "src/startup/saved_files.h" #include "src/ui/menu_model.h" @@ -192,6 +193,7 @@ namespace app { */ struct ShellState { ScreenId activeScreen = ScreenId::hosts; ///< Screen currently shown by the shell. + input::UiInputDevice inputDevice = input::UiInputDevice::gamepad; ///< Last intentional UI input, used for contextual helper glyphs. bool overlayVisible = false; ///< True when the diagnostics overlay is visible. bool shouldExit = false; ///< True when the application should terminate. std::size_t overlayScrollOffset = 0U; ///< Scroll offset used by long overlay content. @@ -363,6 +365,8 @@ namespace app { bool playAudioOnPc = false; ///< True when the host PC should continue local audio playback during streaming. bool showPerformanceStats = false; ///< Display live performance telemetry and include it in the stream summary. bool playAudioOnXbox = true; ///< True when the Xbox should decode and play streamed audio locally. + std::string screenshotDirectory; ///< Screenshot folder; empty selects the title-scoped screenshots directory. + bool keyboardDockTop = false; ///< Dock the stream keyboard to the top instead of the bottom. bool dirty = false; ///< True when persisted TOML-backed settings changed and should be saved. std::vector savedFiles; ///< Saved-file catalog shown on the reset settings page. bool savedFilesDirty = true; ///< True when the saved-file catalog should be refreshed. diff --git a/src/app/settings_storage.cpp b/src/app/settings_storage.cpp index ffa244c..bd5f36a 100644 --- a/src/app/settings_storage.cpp +++ b/src/app/settings_storage.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -349,6 +350,11 @@ namespace { content += std::string("play_audio_on_pc = ") + (settings.playAudioOnPc ? "true" : "false") + "\n"; content += std::string("play_audio_on_xbox = ") + (settings.playAudioOnXbox ? "true" : "false") + "\n"; content += "# Show stream telemetry after streaming ends.\n"; + std::ostringstream screenshotValue; + screenshotValue << toml::value(settings.screenshotDirectory); + content += "# Empty selects the screenshots folder under this title's save directory.\n"; + content += "screenshot_directory = " + screenshotValue.str() + "\n"; + content += std::string("keyboard_dock_top = ") + (settings.keyboardDockTop ? "true" : "false") + "\n"; content += std::string("show_performance_stats = ") + (settings.showPerformanceStats ? "true" : "false") + "\n"; return content; } @@ -392,7 +398,7 @@ namespace { void inspect_streaming_keys(const toml::table &streamingTable, const std::string &filePath, app::LoadAppSettingsResult *result) { for (const auto &[rawKey, node] : streamingTable) { const std::string key(rawKey.str()); - if (key == "video_width" || key == "video_height" || key == "video_bpp" || key == "video_refresh" || key == "video_mode_selected" || key == "fps" || key == "bitrate_kbps" || key == "play_audio_on_pc" || key == "play_audio_on_xbox" || key == "show_performance_stats") { + if (key == "video_width" || key == "video_height" || key == "video_bpp" || key == "video_refresh" || key == "video_mode_selected" || key == "fps" || key == "bitrate_kbps" || key == "play_audio_on_pc" || key == "play_audio_on_xbox" || key == "show_performance_stats" || key == "screenshot_directory" || key == "keyboard_dock_top") { continue; } @@ -493,6 +499,14 @@ namespace app { load_boolean_setting(settingsTable["streaming"]["play_audio_on_pc"], filePath, "streaming.play_audio_on_pc", &result.settings.playAudioOnPc, &result.warnings); load_boolean_setting(settingsTable["streaming"]["play_audio_on_xbox"], filePath, "streaming.play_audio_on_xbox", &result.settings.playAudioOnXbox, &result.warnings); load_boolean_setting(settingsTable["streaming"]["show_performance_stats"], filePath, "streaming.show_performance_stats", &result.settings.showPerformanceStats, &result.warnings); + load_boolean_setting(settingsTable["streaming"]["keyboard_dock_top"], filePath, "streaming.keyboard_dock_top", &result.settings.keyboardDockTop, &result.warnings); + if (const auto node = settingsTable["streaming"]["screenshot_directory"]; node) { + if (const auto path = node.value(); path && path->find_first_of("\r\n") == std::string::npos && path->find('\0') == std::string::npos) { + result.settings.screenshotDirectory = *path; + } else { + append_invalid_value_warning(&result.warnings, filePath, "streaming.screenshot_directory", ""); + } + } return result; } diff --git a/src/app/settings_storage.h b/src/app/settings_storage.h index e0a5334..5f926ca 100644 --- a/src/app/settings_storage.h +++ b/src/app/settings_storage.h @@ -27,6 +27,8 @@ namespace app { bool playAudioOnPc = false; ///< True when the host PC should continue local audio playback during streaming. bool showPerformanceStats = false; ///< Display live performance telemetry and include it in the stream summary. bool playAudioOnXbox = true; ///< True when the Xbox should decode and play streamed audio locally. + std::string screenshotDirectory; ///< Screenshot folder; empty selects the title-scoped screenshots directory. + bool keyboardDockTop = false; ///< Dock the stream keyboard to the top instead of the bottom. }; /** diff --git a/src/input/navigation_input.cpp b/src/input/navigation_input.cpp index f60bd59..439e6ca 100644 --- a/src/input/navigation_input.cpp +++ b/src/input/navigation_input.cpp @@ -74,9 +74,9 @@ namespace input { case KeyboardKey::tab: return shiftPressed ? UiCommand::previous_page : UiCommand::next_page; case KeyboardKey::page_up: - return UiCommand::previous_page; + return shiftPressed ? UiCommand::fast_previous_page : UiCommand::previous_page; case KeyboardKey::page_down: - return UiCommand::next_page; + return shiftPressed ? UiCommand::fast_next_page : UiCommand::next_page; case KeyboardKey::i: case KeyboardKey::m: return UiCommand::open_context_menu; diff --git a/src/input/stream_exit.cpp b/src/input/stream_exit.cpp index d3175c1..eeb4dea 100644 --- a/src/input/stream_exit.cpp +++ b/src/input/stream_exit.cpp @@ -1,6 +1,6 @@ /** * @file src/input/stream_exit.cpp - * @brief Recognizes the digital Back, Black and White exit chord from snapshots and events. + * @brief Recognizes the Back, Black and White stream menu shortcut from snapshots and events. */ #include "src/input/stream_exit.h" diff --git a/src/input/stream_exit.h b/src/input/stream_exit.h index f1652b1..3140edc 100644 --- a/src/input/stream_exit.h +++ b/src/input/stream_exit.h @@ -1,16 +1,16 @@ /** * @file src/input/stream_exit.h - * @brief Defines a digital stream exit chord that does not invoke xemu's Start and Back menu. + * @brief Defines the former stream exit shortcut, now used to open the stream menu. */ #pragma once #include namespace input { - constexpr const char *STREAM_EXIT_HINT = "Press Back/Select + Black + White (LB + RB) together to stop streaming. No hold required."; ///< Shared startup and streaming instruction. + constexpr const char *STREAM_EXIT_HINT = "Back/Select + Black + White (LB + RB): stream menu. No hold required."; ///< Shared startup and streaming instruction. /** - * @brief Recognize the digital exit chord without consulting axes or a clock. + * @brief Recognize the digital stream menu chord without consulting axes or a clock. * * @param buttons Current Moonlight controller button flags. * @param controllerPresent True when the sampled controller is connected. @@ -19,7 +19,7 @@ namespace input { bool is_stream_exit_requested(std::uint32_t buttons, bool controllerPresent); /** - * @brief Track digital button events so a complete chord is detected before queued releases are drained. + * @brief Track the menu shortcut so a complete chord is detected before queued releases are drained. */ class StreamExitChord { public: diff --git a/src/input/stream_input.cpp b/src/input/stream_input.cpp new file mode 100644 index 0000000..428cca7 --- /dev/null +++ b/src/input/stream_input.cpp @@ -0,0 +1,369 @@ +/** + * @file src/input/stream_input.cpp + * @brief Routes ordered controller, keyboard and pointer input to Moonlight. + */ +#include "src/input/stream_input.h" + +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include +#include +#include + +namespace { + constexpr int VIRTUAL_MOUSE_SOURCE = -2; ///< Reserved source for controller pointer buttons. + + /** + * @brief Compare every component of a host gamepad sample. + * @param a First sample. + * @param b Second sample. + * @return True when every component matches. + */ + bool equal(const input::ControllerState &a, const input::ControllerState &b) { + return a.buttonFlags == b.buttonFlags && a.leftTrigger == b.leftTrigger && a.rightTrigger == b.rightTrigger && a.leftStickX == b.leftStickX && a.leftStickY == b.leftStickY && a.rightStickX == b.rightStickX && a.rightStickY == b.rightStickY; + } + + /** + * @brief Rescale an axis outside the mouse dead zone. + * @param value Signed axis value. + * @return Normalized deflection with a central dead zone. + */ + double mouse_axis(short value) { + constexpr int deadzone = 7000; + const int magnitude = std::abs(static_cast(value)); + return magnitude <= deadzone ? 0.0 : std::copysign((std::min(magnitude, 32767) - deadzone) / 25767.0, value); + } +} // namespace + +namespace input { + StreamInput::StreamInput(Sink sink): + sink_(std::move(sink)) {} + + std::uint16_t StreamInput::active_mask() const { + std::uint16_t mask = 0; + for (int i = 0; i < 4; ++i) { + if (slots_[i].device != -1) { + mask |= 1U << i; + } + } + return mask; + } + + int StreamInput::connect(int device) { + if (device < 0) { + return -1; + } + for (int i = 0; i < 4; ++i) { + if (slots_[i].device == device) { + return i; + } + } + for (int i = 0; i < 4; ++i) { + if (slots_[i].device == -1) { + slots_[i] = {}; + slots_[i].device = device; + slots_[i].blocked = captured_; + sink_({StreamPacketType::arrival, i, active_mask()}); + send_controller(i, {}); + return i; + } + } + return -1; + } + + void StreamInput::send_controller(int slot, ControllerState state) { + sink_({StreamPacketType::controller, slot, active_mask(), state}); + slots_[slot].last = state; + } + + void StreamInput::disconnect(int device) { + for (int i = 0; i < 4; ++i) { + if (slots_[i].device == device) { + if (mouseDevice_ == device) { + set_mouse_device(-1); + } + slots_[i] = {}; + send_controller(i, {}); + } + } + } + + void StreamInput::controller(int device, ControllerState state) { + for (int i = 0; i < 4; ++i) { + auto &slot = slots_[i]; + if (slot.device != device) { + continue; + } + slot.sampled = state; + // Local buttons must be released, and sticks centered, before gameplay resumes. + const bool neutral = state.buttonFlags == 0 && state.leftTrigger == 0 && state.rightTrigger == 0 && std::abs(static_cast(state.leftStickX)) < 7000 && std::abs(static_cast(state.leftStickY)) < 7000 && std::abs(static_cast(state.rightStickX)) < 7000 && std::abs(static_cast(state.rightStickY)) < 7000; + if (captured_) { + slot.blocked = true; + } else if (neutral) { + slot.blocked = false; + } + if (!captured_ && !slot.blocked && mouseDevice_ == device) { + mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_LEFT, (state.buttonFlags & A_FLAG) != 0); + mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_RIGHT, (state.buttonFlags & B_FLAG) != 0); + } + if (captured_ || slot.blocked || mouseDevice_ == device) { + state = {}; + } + if (!equal(state, slot.last)) { + send_controller(i, state); + } + } + } + + void StreamInput::set_captured(bool captured) { + if (captured_ == captured) { + return; + } + release_all(); + captured_ = captured; + for (auto &slot : slots_) { + slot.blocked = true; + } + } + + void StreamInput::set_mouse_device(int device) { + release_source(VIRTUAL_MOUSE_SOURCE); + const int previous = mouseDevice_; + mouseDevice_ = device; + mouseX_ = mouseY_ = 0; + for (int i = 0; i < 4; ++i) { + if (slots_[i].device != -1 && (slots_[i].device == previous || slots_[i].device == device)) { + if (!equal(slots_[i].last, {})) { + send_controller(i, {}); + } + slots_[i].blocked = true; + } + } + } + + void StreamInput::tick(unsigned milliseconds) { + if (captured_ || mouseDevice_ < 0) { + return; + } + for (const auto &slot : slots_) { + if (slot.device != mouseDevice_ || slot.blocked) { + continue; + } + const auto &state = slot.sampled; + mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_LEFT, (state.buttonFlags & A_FLAG) != 0); + mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_RIGHT, (state.buttonFlags & B_FLAG) != 0); + const double seconds = std::min(milliseconds, 50U) / 1000.0; + mouseX_ += mouse_axis(state.leftStickX) * 900 * seconds; + mouseY_ -= mouse_axis(state.leftStickY) * 900 * seconds; + const int x = static_cast(mouseX_); + const int y = static_cast(mouseY_); + mouseX_ -= x; + mouseY_ -= y; + mouse_move(x, y); + } + } + + void StreamInput::digital(StreamPacketType type, std::map> &held, int source, int code, bool pressed, int modifiers) { + if (code <= 0 || (captured_ && pressed)) { + return; + } + auto &sources = held[code]; + const bool wasPressed = !sources.empty(); + if (pressed) { + sources.insert(source); + } else { + sources.erase(source); + } + if (wasPressed != !sources.empty()) { + StreamPacket packet {type}; + packet.code = code; + packet.pressed = !sources.empty(); + packet.modifiers = modifiers; + sink_(packet); + } + if (sources.empty()) { + held.erase(code); + } + } + + void StreamInput::key(int source, int code, bool pressed, int modifiers) { + digital(StreamPacketType::key, keys_, source, code, pressed, modifiers); + } + + void StreamInput::mouse_button(int source, int button, bool pressed) { + digital(StreamPacketType::mouse_button, buttons_, source, button, pressed, 0); + } + + void StreamInput::mouse_move(int x, int y) { + if (!captured_ && (x != 0 || y != 0)) { + StreamPacket packet {StreamPacketType::mouse_move}; + packet.x = std::clamp(x, -32768, 32767); + packet.y = std::clamp(y, -32768, 32767); + sink_(packet); + } + } + + void StreamInput::scroll(int x, int y) { + if (!captured_ && (x != 0 || y != 0)) { + StreamPacket packet {StreamPacketType::scroll}; + packet.x = std::clamp(x, -127, 127); + packet.y = std::clamp(y, -127, 127); + sink_(packet); + } + } + + void StreamInput::release_source(int source) { + // Collect codes first because the transition removes empty map entries. + for (auto *held : {&keys_, &buttons_}) { + std::set codes; + for (const auto &[code, sources] : *held) { + if (sources.count(source) != 0) { + codes.insert(code); + } + } + for (int code : codes) { + digital(held == &keys_ ? StreamPacketType::key : StreamPacketType::mouse_button, *held, source, code, false, 0); + } + } + } + + void StreamInput::release_all() { + for (int i = 0; i < 4; ++i) { + if (slots_[i].device != -1 && !equal(slots_[i].last, {})) { + send_controller(i, {}); + } + } + for (auto *held : {&keys_, &buttons_}) { + for (const auto &[code, sources] : *held) { + StreamPacket packet {held == &keys_ ? StreamPacketType::key : StreamPacketType::mouse_button}; + packet.code = code; + sink_(packet); + } + held->clear(); + } + } + + short invert_stick_axis(short value) { + return static_cast(std::min(-static_cast(value), 32767)); + } + + int keyboard_virtual_key(int usage) { + if (usage >= 4 && usage <= 29) { + return 'A' + usage - 4; + } + if (usage >= 30 && usage <= 38) { + return '1' + usage - 30; + } + if (usage >= 58 && usage <= 69) { + return 0x70 + usage - 58; + } + if (usage >= 104 && usage <= 115) { + return 0x7C + usage - 104; + } + if (usage >= 89 && usage <= 97) { + return 0x61 + usage - 89; + } + switch (usage) { + case 39: + return '0'; + case 40: + return 0x0D; + case 41: + return 0x1B; + case 42: + return 0x08; + case 43: + return 0x09; + case 44: + return 0x20; + case 45: + return 0xBD; + case 46: + return 0xBB; + case 47: + return 0xDB; + case 48: + return 0xDD; + case 49: + case 50: + case 100: + return 0xDC; + case 51: + return 0xBA; + case 52: + return 0xDE; + case 53: + return 0xC0; + case 54: + return 0xBC; + case 55: + return 0xBE; + case 56: + return 0xBF; + case 57: + return 0x14; + case 70: + return 0x12C; + case 71: + return 0x91; + case 72: + return 0x13; + case 73: + return 0x12D; + case 74: + return 0x124; + case 75: + return 0x121; + case 76: + return 0x12E; + case 77: + return 0x123; + case 78: + return 0x122; + case 79: + return 0x127; + case 80: + return 0x125; + case 81: + return 0x128; + case 82: + return 0x126; + case 83: + return 0x190; + case 84: + return 0x16F; + case 85: + return 0x6A; + case 86: + return 0x6D; + case 87: + return 0x6B; + case 88: + return 0x10D; + case 98: + return 0x60; + case 99: + return 0x6E; + case 101: + return 0x15D; + case 224: + return 0xA2; + case 225: + return 0xA0; + case 226: + return 0xA4; + case 227: + return 0x15B; + case 228: + return 0x1A3; + case 229: + return 0xA1; + case 230: + return 0x1A5; + case 231: + return 0x15C; + default: + return 0; + } + } +} // namespace input diff --git a/src/input/stream_input.h b/src/input/stream_input.h new file mode 100644 index 0000000..6f8b98b --- /dev/null +++ b/src/input/stream_input.h @@ -0,0 +1,188 @@ +/** + * @file src/input/stream_input.h + * @brief Platform-neutral stream input routing and host input lifetime management. + */ +#pragma once + +#include +#include +#include +#include +#include + +namespace input { + /** + * @brief One complete Moonlight gamepad sample. + */ + struct ControllerState { + int buttonFlags = 0; ///< Moonlight digital button flags. + unsigned char leftTrigger = 0; ///< Left trigger pressure. + unsigned char rightTrigger = 0; ///< Right trigger pressure. + short leftStickX = 0; ///< Left horizontal axis. + short leftStickY = 0; ///< Left vertical axis, positive upward. + short rightStickX = 0; ///< Right horizontal axis. + short rightStickY = 0; ///< Right vertical axis, positive upward. + }; + + /** + * @brief Input packet type delivered to the transport adapter. + */ + enum class StreamPacketType { + arrival, ///< Controller arrival and capability announcement. + controller, ///< Full controller sample or removal. + key, ///< Keyboard virtual key transition. + mouse_button, ///< Mouse button transition. + mouse_move, ///< Relative pointer movement. + scroll, ///< Horizontal and vertical wheel ticks. + }; + + /** + * @brief Transport-independent input packet; unused fields remain zero. + */ + struct StreamPacket { + StreamPacketType type {}; ///< Selected transport operation. + int slot = 0; ///< Stable host controller index. + std::uint16_t mask = 0; ///< All connected host controller slots. + ControllerState controller {}; ///< Complete controller state. + int code = 0; ///< Virtual key or Moonlight mouse button. + bool pressed = false; ///< Key or mouse button transition. + int modifiers = 0; ///< Moonlight keyboard modifier flags. + int x = 0; ///< Horizontal movement or scroll ticks. + int y = 0; ///< Vertical movement or scroll ticks. + }; + + /** + * @brief Routes four stable controller slots and merges physical and virtual input sources. + */ + class StreamInput { + public: + using Sink = std::function; ///< Transport packet consumer. + /** + * @brief Construct a router owned exclusively by the SDL event thread. + * @param sink Receives ordered packets; Moonlight transport calls only enqueue data. + */ + explicit StreamInput(Sink sink); + /** + * @brief Reserve the lowest free controller slot without renumbering connected pads. + * @param device SDL instance ID. + * @return Slot index, or -1 when four controllers are already connected. + */ + int connect(int device); + /** + * @brief Release a disconnected pad and update the host's active mask. + * @param device SDL instance ID. + */ + void disconnect(int device); + /** + * @brief Forward a controller sample unless local controls own it. + * @param device SDL instance ID. + * @param state Sampled state. + */ + void controller(int device, ControllerState state); + /** + * @brief Release host input before a menu or focus transition and suppress gameplay. + * @param captured True while a local menu owns input. + */ + void set_captured(bool captured); + /** + * @brief Change the controller that drives virtual mouse input. + * @param device SDL instance ID, or -1 to disable mouse mode. + */ + void set_mouse_device(int device); + /** + * @brief Integrate stick movement at a bounded time step, preserving fractional motion. + * @param milliseconds Elapsed event-thread time. + */ + void tick(unsigned milliseconds); + /** + * @brief Merge a physical or virtual key transition by source. + * @param source Unique input source ID. + * @param code Win32 virtual key code, including the optional extended bit at 0x100. + * @param pressed True for a press. + * @param modifiers Moonlight modifiers. + */ + void key(int source, int code, bool pressed, int modifiers = 0); + /** + * @brief Merge a physical or virtual mouse button transition by source. + * @param source Unique input source ID. + * @param button Moonlight button number. + * @param pressed True for a press. + */ + void mouse_button(int source, int button, bool pressed); + /** + * @brief Forward relative pointer movement. + * @param x Horizontal delta. + * @param y Vertical delta. + */ + void mouse_move(int x, int y); + /** + * @brief Forward wheel ticks on both axes. + * @param x Horizontal ticks. + * @param y Vertical ticks. + */ + void scroll(int x, int y); + /** + * @brief Release every key and mouse button belonging to one source. + * @param source Source that disconnected or changed mode. + */ + void release_source(int source); + /** + * @brief Release all held host input before transport shutdown. + */ + void release_all(); + /** + * @brief Read the connected controller bit mask. + * @return Bits zero through three. + */ + std::uint16_t active_mask() const; + + private: + /** + * @brief Stable controller slot state. + */ + struct Slot { + int device = -1; ///< SDL instance ID, or free. + ControllerState last {}; ///< Last host state. + ControllerState sampled {}; ///< Latest local state. + bool blocked = false; ///< Wait for neutral after capture ends. + }; + + /** + * @brief Send a complete controller sample. + * @param slot Host controller slot. + * @param state State to send. + */ + void send_controller(int slot, ControllerState state); + /** + * @brief Merge a shared digital input transition. + * @param type Key or mouse packet type. + * @param held Source sets per host input. + * @param source Input source. + * @param code Host input code. + * @param pressed Transition direction. + * @param modifiers Keyboard modifiers. + */ + void digital(StreamPacketType type, std::map> &held, int source, int code, bool pressed, int modifiers); + Sink sink_; ///< Ordered transport sink. + std::array slots_ {}; ///< Stable slots, never compacted on removal. + std::map> keys_; ///< Sources holding each host key. + std::map> buttons_; ///< Sources holding each host mouse button. + bool captured_ = false; ///< Local UI currently owns gameplay input. + int mouseDevice_ = -1; ///< Pad controlling the mouse, or disabled. + double mouseX_ = 0; ///< Fractional horizontal motion. + double mouseY_ = 0; ///< Fractional vertical motion. + }; + + /** + * @brief Map a USB HID/SDL scancode to a normalized US virtual key. + * @param usage Keyboard usage page code. + * @return Virtual key with 0x100 for extended keys, or zero for unsupported usages. + */ + int keyboard_virtual_key(int usage); + /** + * @brief Invert an SDL axis without overflowing at -32768. + * @param value SDL axis value. + * @return Saturated inverted value. + */ + short invert_stick_axis(short value); +} // namespace input diff --git a/src/input/stream_menu.cpp b/src/input/stream_menu.cpp new file mode 100644 index 0000000..0cc401a --- /dev/null +++ b/src/input/stream_menu.cpp @@ -0,0 +1,451 @@ +/** + * @file src/input/stream_menu.cpp + * @brief Implements radial selection and host keyboard navigation. + */ +#include "src/input/stream_menu.h" + +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include +#include + +namespace { + /** + * @brief Arrange a central character grid with Xbox-style side controls and a wide space bar. + * @param keys Host key catalog indices for the four character rows. + * @return Complete focusable keyboard geometry. + */ + std::array make_keyboard_cells(const std::array &keys) { + std::array cells {}; + for (int i = 0; i < 40; ++i) { + cells[i] = {keys[i], 3 + (i % 10) * 2, i / 10, 2, 1}; + } + cells[40] = {44, 3, 4, 20, 1}; + cells[41] = {input::StreamMenu::KEYBOARD_PAGE_KEY, 0, 0, 3, 1}; + cells[42] = {57, 0, 1, 3, 2}; + cells[43] = {input::StreamMenu::KEYBOARD_CAPS_KEY, 0, 3, 3, 2}; + cells[44] = {47, 23, 0, 3, 1}; + cells[45] = {59, 23, 1, 3, 2}; + cells[46] = {46, 23, 3, 3, 2}; + return cells; + } +} // namespace + +namespace input { + const std::array StreamMenu::KEYS {{ + {"1", "!", '1'}, + {"2", "@", '2'}, + {"3", "#", '3'}, + {"4", "$", '4'}, + {"5", "%", '5'}, + {"6", "^", '6'}, + {"7", "&", '7'}, + {"8", "*", '8'}, + {"9", "(", '9'}, + {"0", ")", '0'}, + {"q", "Q", 'Q'}, + {"w", "W", 'W'}, + {"e", "E", 'E'}, + {"r", "R", 'R'}, + {"t", "T", 'T'}, + {"y", "Y", 'Y'}, + {"u", "U", 'U'}, + {"i", "I", 'I'}, + {"o", "O", 'O'}, + {"p", "P", 'P'}, + {"a", "A", 'A'}, + {"s", "S", 'S'}, + {"d", "D", 'D'}, + {"f", "F", 'F'}, + {"g", "G", 'G'}, + {"h", "H", 'H'}, + {"j", "J", 'J'}, + {"k", "K", 'K'}, + {"l", "L", 'L'}, + {";", ":", 0xBA}, + {"z", "Z", 'Z'}, + {"x", "X", 'X'}, + {"c", "C", 'C'}, + {"v", "V", 'V'}, + {"b", "B", 'B'}, + {"n", "N", 'N'}, + {"m", "M", 'M'}, + {",", "<", 0xBC}, + {".", ">", 0xBE}, + {"/", "?", 0xBF}, + {"Shift", "Shift", -MODIFIER_SHIFT}, + {"Ctrl", "Ctrl", -MODIFIER_CTRL}, + {"Alt", "Alt", -MODIFIER_ALT}, + {"Win", "Win", -MODIFIER_META}, + {"Space", "Space", 0x20}, + {"Tab", "Tab", 0x09}, + {"Enter", "Enter", 0x0D}, + {"Bksp", "Bksp", 0x08}, + {"Del", "Del", 0x12E}, + {"Esc", "Esc", 0x1B}, + {"-", "_", 0xBD}, + {"=", "+", 0xBB}, + {"[", "{", 0xDB}, + {"]", "}", 0xDD}, + {"\\", "|", 0xDC}, + {"'", "\"", 0xDE}, + {"`", "~", 0xC0}, + {"Left", "Left", 0x125}, + {"Down", "Down", 0x128}, + {"Right", "Right", 0x127}, + {"F1", "F1", 0x70}, + {"F2", "F2", 0x71}, + {"F3", "F3", 0x72}, + {"F4", "F4", 0x73}, + {"F5", "F5", 0x74}, + {"F6", "F6", 0x75}, + {"Home", "Home", 0x124}, + {"End", "End", 0x123}, + {"Up", "Up", 0x126}, + {"PgDn", "PgDn", 0x122}, + {"F7", "F7", 0x76}, + {"F8", "F8", 0x77}, + {"F9", "F9", 0x78}, + {"F10", "F10", 0x79}, + {"F11", "F11", 0x7A}, + {"F12", "F12", 0x7B}, + {"PgUp", "PgUp", 0x121}, + {"Ins", "Ins", 0x12D}, + {"Caps", "Caps", 0x14}, + {"PrtSc", "PrtSc", 0x12C}, + {"!", "!", '1', MODIFIER_SHIFT}, + {"@", "@", '2', MODIFIER_SHIFT}, + {"#", "#", '3', MODIFIER_SHIFT}, + {"$", "$", '4', MODIFIER_SHIFT}, + {"%", "%", '5', MODIFIER_SHIFT}, + {"^", "^", '6', MODIFIER_SHIFT}, + {"&", "&", '7', MODIFIER_SHIFT}, + {"*", "*", '8', MODIFIER_SHIFT}, + {"(", "(", '9', MODIFIER_SHIFT}, + {")", ")", '0', MODIFIER_SHIFT}, + {"_", "_", 0xBD, MODIFIER_SHIFT}, + {"+", "+", 0xBB, MODIFIER_SHIFT}, + {"{", "{", 0xDB, MODIFIER_SHIFT}, + {"}", "}", 0xDD, MODIFIER_SHIFT}, + {"|", "|", 0xDC, MODIFIER_SHIFT}, + {":", ":", 0xBA, MODIFIER_SHIFT}, + {"\"", "\"", 0xDE, MODIFIER_SHIFT}, + {"~", "~", 0xC0, MODIFIER_SHIFT}, + {"<", "<", 0xBC, MODIFIER_SHIFT}, + {">", ">", 0xBE, MODIFIER_SHIFT}, + {"?", "?", 0xBF, MODIFIER_SHIFT}, + {"-", "-", 0xBD, 0}, + {"=", "=", 0xBB, 0}, + {"[", "[", 0xDB, 0}, + {"]", "]", 0xDD, 0}, + {"\\", "\\", 0xDC, 0}, + {"Copy", "Copy", 'C', MODIFIER_CTRL}, + {"Paste", "Paste", 'V', MODIFIER_CTRL}, + {"Cut", "Cut", 'X', MODIFIER_CTRL}, + {"Undo", "Undo", 'Z', MODIFIER_CTRL}, + {"Redo", "Redo", 'Y', MODIFIER_CTRL}, + {"All", "All", 'A', MODIFIER_CTRL}, + {"Task", "Task", 0x09, MODIFIER_ALT}, + {"Close", "Close", 0x73, MODIFIER_ALT}, + {"C+A+Del", "C+A+Del", 0x12E, MODIFIER_CTRL | MODIFIER_ALT}, + {"Desk", "Desk", 'D', MODIFIER_META}, + {"Explorer", "Explorer", 'E', MODIFIER_META}, + {"Run", "Run", 'R', MODIFIER_META}, + }}; + + void StreamMenu::open(int device) { + owner = device; + page = StreamMenuPage::radial; + selected = StreamAction::none; + modifiers = 0; + logOffset = 0; + keyIndex = 10; + keyboardLayout = 0; + shiftOnce = false; + caps = false; + confirmQuit = false; + keyboardColumn_ = 4; + keyboardRow_ = 1; + previousSticks_ = {}; + } + + void StreamMenu::close() { + page = StreamMenuPage::closed; + selected = StreamAction::none; + modifiers = 0; + shiftOnce = false; + caps = false; + } + + bool StreamMenu::select_stick(bool right, short x, short y) { + if (page != StreamMenuPage::radial || std::max(std::abs(static_cast(x)), std::abs(static_cast(y))) < WHEEL_DEADZONE) { + return false; + } + const int sector = std::abs(static_cast(y)) >= std::abs(static_cast(x)) ? (y > 0 ? 0 : 2) : (x > 0 ? 1 : 3); + const auto action = static_cast((right ? 4 : 0) + sector); + const bool changed = action != selected; + selected = action; + return changed; + } + + bool StreamMenu::sample_sticks(short leftX, short leftY, short rightX, short rightY) { + const std::array current {leftX, leftY, rightX, rightY}; + const bool leftChanged = current[0] != previousSticks_[0] || current[1] != previousSticks_[1]; + const bool rightChanged = current[2] != previousSticks_[2] || current[3] != previousSticks_[3]; + previousSticks_ = current; + bool changed = false; + if (leftChanged || selected == StreamAction::none) { + changed = select_stick(false, leftX, leftY); + } + if (rightChanged || selected == StreamAction::none) { + changed = select_stick(true, rightX, rightY) || changed; + } + return changed; + } + + void StreamMenu::navigate(int x, int y) { + if (page == StreamMenuPage::keyboard) { + const auto &cells = keyboard_cells(); + keyIndex = std::clamp(keyIndex, 0, static_cast(cells.size()) - 1); + const auto ¤t = cells[keyIndex]; + keyboardColumn_ = std::clamp(keyboardColumn_, current.column, current.column + current.width - 1); + keyboardRow_ = std::clamp(keyboardRow_, current.row, current.row + current.height - 1); + if (x != 0) { + keyboardColumn_ = (x > 0 ? current.column + current.width : current.column - 1 + KEYBOARD_COLUMNS) % KEYBOARD_COLUMNS; + } + if (y != 0) { + keyboardRow_ = (y > 0 ? current.row + current.height : current.row - 1 + KEYBOARD_ROWS) % KEYBOARD_ROWS; + } + const auto next = std::find_if(cells.begin(), cells.end(), [this](const StreamKeyboardCell &cell) { + return keyboardColumn_ >= cell.column && keyboardColumn_ < cell.column + cell.width && keyboardRow_ >= cell.row && keyboardRow_ < cell.row + cell.height; + }); + if (next != cells.end()) { + keyIndex = static_cast(next - cells.begin()); + } + } else if (page == StreamMenuPage::quit_confirmation && (x != 0 || y != 0)) { + confirmQuit = !confirmQuit; + } else if (page == StreamMenuPage::logs) { + logOffset = std::clamp(logOffset - y * 5, 0, 255); + } else if (page == StreamMenuPage::radial && (x != 0 || y != 0)) { + select_stick(false, static_cast(x * 20000), static_cast(-y * 20000)); + } + } + + int StreamMenu::keyboard_key() { + const int key = keyboard_cells().at(keyIndex).key; + if (key == KEYBOARD_PAGE_KEY) { + toggle_keyboard_page(); + return 0; + } + if (key == KEYBOARD_CAPS_KEY) { + caps = !caps; + return 0; + } + const int code = KEYS[key].code; + if (code < 0) { + if (code == -MODIFIER_SHIFT) { + toggle_shift(); + } else { + modifiers ^= -code; + } + return 0; + } + return code; + } + + std::string StreamMenu::key_label(int index) const { + const int catalogIndex = keyboard_cells().at(index).key; + if (catalogIndex == KEYBOARD_PAGE_KEY) { + return keyboardLayout == 0 ? "?123" : keyboardLayout == 1 ? "PC keys" : + "ABC"; + } + if (catalogIndex == KEYBOARD_CAPS_KEY) { + return caps ? "[Caps]" : "Caps"; + } + const auto &key = KEYS.at(catalogIndex); + if (key.code == -MODIFIER_SHIFT) { + return shiftOnce ? "[Shift]" : "Shift"; + } + if (key.code < 0 && (modifiers & -key.code) != 0) { + return std::string("[") + key.label + "]"; + } + return (key_modifiers(key.code) & MODIFIER_SHIFT) != 0 ? key.shifted : key.label; + } + + int StreamMenu::key_modifiers(int code) const { + const int key = keyboard_cells().at(keyIndex).key; + const int required = key < static_cast(KEYS.size()) && KEYS[key].code == code ? KEYS[key].modifiers : 0; + if (key >= 106 && key < static_cast(KEYS.size()) && KEYS[key].code == code) { + return required; + } + const bool upper = shiftOnce != (caps && code >= 'A' && code <= 'Z'); + return modifiers | required | (upper ? MODIFIER_SHIFT : 0); + } + + void StreamMenu::toggle_shift() { + shiftOnce = !shiftOnce; + } + + void StreamMenu::key_sent() { + shiftOnce = false; + } + + const std::array &StreamMenu::keyboard_cells() const { + static const auto letters = make_keyboard_cells({{ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28, + 55, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + }}); + static const auto symbols = make_keyboard_cells({{ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 80, + 81, + 82, + 83, + 84, + 85, + 86, + 87, + 88, + 89, + 101, + 102, + 103, + 104, + 105, + 55, + 96, + 56, + 97, + 95, + 90, + 91, + 92, + 93, + 94, + 98, + 99, + 100, + 37, + 38, + }}); + static const auto desktop = make_keyboard_cells({{ + 60, + 61, + 62, + 63, + 64, + 65, + 70, + 71, + 72, + 73, + 74, + 75, + 49, + 45, + 77, + 48, + 66, + 67, + 76, + 69, + 106, + 107, + 108, + 109, + 110, + 111, + 112, + 113, + 114, + 115, + 41, + 42, + 43, + 40, + 78, + 79, + 68, + 58, + 116, + 117, + }}); + return keyboardLayout == 0 ? letters : keyboardLayout == 1 ? symbols : + desktop; + } + + void StreamMenu::toggle_keyboard_page() { + keyboardLayout = (keyboardLayout + 1) % 3; + } + + const char *stream_action_label(StreamAction action, bool mouseEnabled, bool statisticsEnabled) { + switch (action) { + case StreamAction::keyboard: + return "Keyboard"; + case StreamAction::mouse: + return mouseEnabled ? "Mouse: On" : "Mouse: Off"; + case StreamAction::logs: + return "View logs"; + case StreamAction::quit: + return "Quit stream"; + case StreamAction::guide: + return "Guide"; + case StreamAction::share: + return "Share"; + case StreamAction::screenshot: + return "Screenshot"; + case StreamAction::statistics: + return statisticsEnabled ? "Stats: On" : "Stats: Off"; + case StreamAction::none: + return "Select with either stick"; + } + return ""; + } +} // namespace input diff --git a/src/input/stream_menu.h b/src/input/stream_menu.h new file mode 100644 index 0000000..dac3ee6 --- /dev/null +++ b/src/input/stream_menu.h @@ -0,0 +1,159 @@ +/** + * @file src/input/stream_menu.h + * @brief Testable dual-stick stream menu and on-screen keyboard navigation. + */ +#pragma once + +#include +#include + +namespace input { + /** + * @brief Actions arranged clockwise from the top of each stick wheel. + */ + enum class StreamAction { + keyboard, ///< Left wheel, up. + mouse, ///< Left wheel, right. + logs, ///< Left wheel, down. + quit, ///< Left wheel, left. + guide, ///< Right wheel, up. + share, ///< Right wheel, right. + screenshot, ///< Right wheel, down. + statistics, ///< Right wheel, left. + none, ///< No highlighted action. + }; + /** + * @brief Local stream control screen. + */ + enum class StreamMenuPage { + closed, ///< Host owns input. + radial, ///< Two stick wheels. + keyboard, ///< On-screen keyboard. + logs, ///< Scrollable runtime log. + quit_confirmation, ///< Explicit confirmation before disconnecting. + }; + + /** + * @brief A keyboard cell with an unshifted and shifted label. + */ + struct StreamKeyboardKey { + const char *label; ///< Base label. + const char *shifted; ///< Shifted label. + int code; ///< Win32 virtual key, or modifier toggle code. + int modifiers = 0; ///< Required modifiers for a symbol or a named keyboard shortcut. + }; + + /** + * @brief One focusable key in the five-row keyboard, measured in half-key column units. + */ + struct StreamKeyboardCell { + int key = 0; ///< Index in the host key catalog, or KEYBOARD_PAGE_KEY for the alternate layout. + int column = 0; ///< Left column in the 26-unit keyboard. + int row = 0; ///< Top row in the five-row keyboard. + int width = 2; ///< Horizontal span in half-key units. + int height = 1; ///< Vertical span in rows. + }; + + /** + * @brief Dual-wheel selection with persistent keyboard and log navigation. + */ + class StreamMenu { + public: + /** + * @brief Open the menu on the controller that invoked the shortcut. + * @param device SDL instance ID, or -1 for keyboard control. + */ + void open(int device); + /** + * @brief Close local controls and reset the highlighted action. + */ + void close(); + /** + * @brief Select a cardinal sector with either stick; centered sticks retain selection. + * @param right True for the right wheel. + * @param x Stick X, positive rightward. + * @param y Stick Y, positive upward. + * @return True only when the highlighted action changes and needs repainting. + */ + bool select_stick(bool right, short x, short y); + /** + * @brief Update wheel selection from changed sampled axes, retaining the last active stick. + * @param leftX Left-stick X. + * @param leftY Left-stick Y, positive upward. + * @param rightX Right-stick X. + * @param rightY Right-stick Y, positive upward. + * @return True when selection changes. + */ + bool sample_sticks(short leftX, short leftY, short rightX, short rightY); + /** + * @brief Move keyboard focus with wraparound, or scroll logs. + * @param x Horizontal step. + * @param y Vertical step, positive downward. + */ + void navigate(int x, int y); + /** + * @brief Return the selected on-screen key and apply modifier toggles. + * @return A virtual key to tap, or zero for a modifier toggle. + */ + int keyboard_key(); + /** + * @brief Return a cell's label according to the current Caps and one-shot Shift state. + * @param index Cell index. + * @return Visible label. + */ + std::string key_label(int index) const; + /** + * @brief Get the current letters, symbols or desktop-shortcuts layout. + * @return Focusable cells, including wide space and tall side controls. + */ + const std::array &keyboard_cells() const; + /** + * @brief Toggle the alternate keyboard without losing the focused key's visual location. + */ + void toggle_keyboard_page(); + /** + * @brief Compute modifiers for a host key, including symbols, Caps and one-shot Shift. + * @param code Host virtual key to send. + * @return Moonlight modifier flags. + */ + int key_modifiers(int code) const; + /** + * @brief Toggle Shift for the next host key without changing Caps. + */ + void toggle_shift(); + /** + * @brief Consume one-shot Shift after sending a host key. + */ + void key_sent(); + StreamMenuPage page = StreamMenuPage::closed; ///< Current screen. + StreamAction selected = StreamAction::none; ///< Highlighted wheel action. + int owner = -1; ///< Controller that opened local controls. + int keyIndex = 10; ///< Selected visual cell; the letter Q is focused initially. + int keyboardLayout = 0; ///< Letters, symbols, or desktop keys and shortcuts (0 through 2). + bool shiftOnce = false; ///< Shift applies to the next host key only. + bool caps = false; ///< Persistent uppercase letters, independent of the host Caps Lock state. + bool confirmQuit = false; ///< Disconnect button selected; Cancel is the initial selection. + int modifiers = 0; ///< Latched Moonlight modifier toggles. + int logOffset = 0; ///< Lines scrolled backward from the newest log. + static constexpr int KEYBOARD_COLUMNS = 26; ///< Half-key column units, including both side control rails. + static constexpr int KEYBOARD_ROWS = 5; ///< Four character rows and one space-bar row. + static constexpr int KEYBOARD_PAGE_KEY = 128; ///< Layout toggle, never sent to the host. + static constexpr int KEYBOARD_CAPS_KEY = 129; ///< Local Caps toggle, never sent to the host. + static constexpr int WHEEL_DEADZONE = 9000; ///< Deliberate wheel movement without requiring a half-stick deflection. + static const std::array KEYS; ///< Host keys, explicit symbols and common desktop shortcuts. + + private: + int keyboardColumn_ = 4; ///< Preferred column retained while traversing wide keys. + int keyboardRow_ = 1; ///< Preferred row retained while traversing tall side controls. + std::array previousSticks_ {}; ///< Previous sampled axes so a stationary stick cannot steal the other wheel's selection. + }; + + /** + * @brief Return the visible wheel action label. + * @param action Action to label. + * @param mouseEnabled Current virtual mouse state. + * @param statisticsEnabled Current statistics state. + * @return Visible action label. + */ + const char *stream_action_label(StreamAction action, bool mouseEnabled, bool statisticsEnabled); +} // namespace input diff --git a/src/input/ui_input.cpp b/src/input/ui_input.cpp new file mode 100644 index 0000000..9c2cca5 --- /dev/null +++ b/src/input/ui_input.cpp @@ -0,0 +1,69 @@ +/** + * @file src/input/ui_input.cpp + * @brief Implements repeat timing without depending on SDL or platform key repeat. + */ +#include "src/input/ui_input.h" + +#include +#include + +namespace input { + bool is_gamepad_ui_activity(short value, bool trigger) { + return trigger ? value >= 16000 : std::abs(static_cast(value)) >= 16000; + } + + bool is_repeatable_ui_command(UiCommand command) { + switch (command) { + case UiCommand::move_up: + case UiCommand::move_down: + case UiCommand::move_left: + case UiCommand::move_right: + case UiCommand::previous_page: + case UiCommand::next_page: + case UiCommand::fast_previous_page: + case UiCommand::fast_next_page: + return true; + default: + return false; + } + } + + UiCommand KeyboardNavigation::press(int key, UiCommand command, std::uint32_t now) { + if (command == UiCommand::none || held_.count(key) != 0) { + return UiCommand::none; + } + if (!is_repeatable_ui_command(command)) { + // A held arrow must not carry through an activation into a different dialog. + reset(); + } + held_[key] = {command, now, now, ++order_}; + return command; + } + + void KeyboardNavigation::release(int key) { + held_.erase(key); + } + + UiCommand KeyboardNavigation::poll(std::uint32_t now) { + if (held_.empty()) { + return UiCommand::none; + } + const auto newest = std::max_element(held_.begin(), held_.end(), [](const auto &a, const auto &b) { + return a.second.order < b.second.order; + }); + auto &key = newest->second; + if (!is_repeatable_ui_command(key.command)) { + return UiCommand::none; + } + if (now - key.pressedAt < NAVIGATION_INITIAL_REPEAT_MILLISECONDS || now - key.repeatedAt < NAVIGATION_REPEAT_MILLISECONDS) { + return UiCommand::none; + } + key.repeatedAt = now; + return key.command; + } + + void KeyboardNavigation::reset() { + held_.clear(); + order_ = 0; + } +} // namespace input diff --git a/src/input/ui_input.h b/src/input/ui_input.h new file mode 100644 index 0000000..7919ef1 --- /dev/null +++ b/src/input/ui_input.h @@ -0,0 +1,126 @@ +/** + * @file src/input/ui_input.h + * @brief Shared UI navigation timing and keyboard hold tracking. + */ +#pragma once + +#include "src/input/navigation_input.h" + +#include +#include + +namespace input { + constexpr std::uint32_t NAVIGATION_INITIAL_REPEAT_MILLISECONDS = 150; ///< Delay shared by keyboard and controller navigation. + constexpr std::uint32_t NAVIGATION_REPEAT_MILLISECONDS = 45; ///< Interval shared by keyboard and controller navigation. + + /** + * @brief Device whose intentional input most recently controlled the UI. + */ + enum class UiInputDevice { + gamepad, ///< Show controller button glyphs. + keyboard, ///< Show keyboard keycaps. + }; + + /** + * @brief Decide whether analog input is deliberate enough to select controller hints. + * @param value SDL axis value. + * @param trigger True for a unipolar trigger; false for a signed stick axis. + * @return True outside the UI dead zone; neutral and small drift are ignored. + */ + bool is_gamepad_ui_activity(short value, bool trigger); + + /** + * @brief Classify commands that can repeat safely while a keyboard key remains held. + * @param command Mapped UI command. + * @return True for directional navigation and paging, never confirmation or toggles. + */ + bool is_repeatable_ui_command(UiCommand command); + + /** + * @brief Generates UI repeats from transitions, including USB keyboards without OS autorepeat. + */ + class KeyboardNavigation { + public: + /** + * @brief Register an initial key press and issue its command exactly once. + * @param key Physical key identity, such as an SDL scancode. + * @param command Mapped command, or none for an unmapped key. + * @param now Monotonic millisecond tick count. + * @return Initial command, or none for duplicate presses and unmapped keys. + */ + UiCommand press(int key, UiCommand command, std::uint32_t now); + /** + * @brief Stop repeating a released or disconnected key. + * @param key Physical key identity. + */ + void release(int key); + /** + * @brief Repeat the most recently pressed navigation key when its timer is due. + * @param now Monotonic millisecond tick count, allowed to wrap. + * @return At most one repeat command; long stalls do not generate catch-up bursts. + */ + UiCommand poll(std::uint32_t now); + /** + * @brief Clear held keys on focus loss, device changes or modal activation. + */ + void reset(); + + private: + /** + * @brief One keyboard navigation hold. + */ + struct HeldKey { + UiCommand command = UiCommand::none; ///< Command associated with this physical key. + std::uint32_t pressedAt = 0; ///< Initial press tick. + std::uint32_t repeatedAt = 0; ///< Last emitted repeat tick. + std::uint64_t order = 0; ///< Press order, independent of scancode ordering. + }; + + std::map held_; ///< Current mapped keys, including action keys that must not repeat. + std::uint64_t order_ = 0; ///< Monotonic ordering within the current input lifetime. + }; + + /** + * @brief Repeat timing for one held controller direction. + */ + struct ControllerNavigationHoldState { + bool active = false; ///< Direction is held. + std::uint32_t activatedTick = 0; ///< Initial activation tick. + std::uint32_t lastRepeatTick = 0; ///< Last repeat tick. + }; + + /** + * @brief Shell input lifetime, separate from the stream's input ownership. + */ + struct ShellInputState { + KeyboardNavigation keyboardNavigation; ///< Keyboard repeat state. + bool leftTriggerPressed = false; ///< Held-input or navigation latch. + bool rightTriggerPressed = false; ///< Held-input or navigation latch. + bool leftShoulderPressed = false; ///< Held-input or navigation latch. + bool rightShoulderPressed = false; ///< Held-input or navigation latch. + bool controllerStartPressed = false; ///< Held-input or navigation latch. + bool controllerBackPressed = false; ///< Held-input or navigation latch. + bool controllerExitComboArmed = false; ///< Held-input or navigation latch. + bool controllerExitComboTriggered = false; ///< Held-input or navigation latch. + bool controllerNavigationNeutralRequired = false; ///< Held-input or navigation latch. + std::uint32_t controllerStartDownTick = 0; ///< Press or repeat tick for this input. + std::uint32_t controllerBackDownTick = 0; ///< Press or repeat tick for this input. + std::uint32_t leftShoulderRepeatTick = 0; ///< Press or repeat tick for this input. + std::uint32_t rightShoulderRepeatTick = 0; ///< Press or repeat tick for this input. + std::uint32_t leftTriggerRepeatTick = 0; ///< Press or repeat tick for this input. + std::uint32_t rightTriggerRepeatTick = 0; ///< Press or repeat tick for this input. + ControllerNavigationHoldState moveUpHoldState {}; ///< Directional repeat state. + ControllerNavigationHoldState moveDownHoldState {}; ///< Directional repeat state. + ControllerNavigationHoldState moveLeftHoldState {}; ///< Directional repeat state. + ControllerNavigationHoldState moveRightHoldState {}; ///< Directional repeat state. + + /** + * @brief Clear stale holds after streaming consumed their release events, then wait for neutral. + */ + void reset_after_stream() { + *this = {}; + controllerNavigationNeutralRequired = true; + } + }; + +} // namespace input diff --git a/src/input/usb_hid.cpp b/src/input/usb_hid.cpp new file mode 100644 index 0000000..f0dda13 --- /dev/null +++ b/src/input/usb_hid.cpp @@ -0,0 +1,103 @@ +/** + * @file src/input/usb_hid.cpp + * @brief Decodes standard boot HID input with rollover and disconnect handling. + */ +#include "src/input/usb_hid.h" + +#include +#include + +namespace input { + BootHidInput::BootHidInput(Sink sink): + sink_(std::move(sink)) {} + + void BootHidInput::connect(int device, int protocol) { + disconnect(device); + if (protocol == 1 || protocol == 2) { + devices_[device].protocol = protocol; + } + } + + void BootHidInput::update(Device &device, const std::bitset<256> &next) { + const auto changed = device.held ^ next; + const auto aggregate = [&]() { + std::bitset<256> all; + for (const auto &[id, other] : devices_) { + if (other.protocol == device.protocol) { + all |= other.held; + } + } + return all; + }; + const auto before = aggregate(); + device.held = next; + const auto after = aggregate(); + // Modifiers precede regular keys so SDL generates correct key symbols and modifier flags. + for (int pass = 0; pass < 2; ++pass) { + for (int code = 0; code < 256; ++code) { + if ((code >= 224) != (pass == 0) || !changed[code] || before[code] == after[code]) { + continue; + } + sink_({device.protocol == 1 ? BootHidEventType::key : BootHidEventType::button, code, after[code]}); + } + } + } + + void BootHidInput::disconnect(int device) { + const auto found = devices_.find(device); + if (found != devices_.end()) { + update(found->second, {}); + devices_.erase(found); + } + } + + void BootHidInput::clear() { + while (!devices_.empty()) { + disconnect(devices_.begin()->first); + } + } + + void BootHidInput::report(int device, const std::uint8_t *bytes, std::size_t length) { + const auto found = devices_.find(device); + if (found == devices_.end() || bytes == nullptr) { + return; + } + auto &state = found->second; + if (state.protocol == 1) { + if (length < 8) { + return; + } + auto held = state.held; + for (int bit = 0; bit < 8; ++bit) { + held[224 + bit] = (bytes[0] & (1U << bit)) != 0; + } + const bool rollover = std::any_of(bytes + 2, bytes + 8, [](std::uint8_t code) { + return code >= 1 && code <= 3; + }); + if (!rollover) { + for (int code = 0; code < 224; ++code) { + held[code] = false; + } + for (int i = 2; i < 8; ++i) { + if (bytes[i] >= 4 && bytes[i] < 224) { + held[bytes[i]] = true; + } + } + } + update(state, held); + } else if (length >= 3) { + std::bitset<256> held; + // Boot protocol guarantees exactly three buttons and two signed 8-bit axes. + // Wheel/report-ID layouts require descriptor parsing and must not be guessed. + for (int bit = 0; bit < 3; ++bit) { + held[bit + 1] = (bytes[0] & (1U << bit)) != 0; + } + update(state, held); + const int x = bytes[1] < 128 ? bytes[1] : bytes[1] - 256; + const int y = bytes[2] < 128 ? bytes[2] : bytes[2] - 256; + if (x != 0 || y != 0) { + sink_({BootHidEventType::motion, 0, false, x, y}); + } + } + } +} // namespace input diff --git a/src/input/usb_hid.h b/src/input/usb_hid.h new file mode 100644 index 0000000..5504a30 --- /dev/null +++ b/src/input/usb_hid.h @@ -0,0 +1,86 @@ +/** + * @file src/input/usb_hid.h + * @brief Safe USB boot keyboard and mouse report decoding. + */ +#pragma once + +#include +#include +#include +#include +#include + +namespace input { + /** + * @brief Decoded boot HID input operation. + */ + enum class BootHidEventType { + key, ///< Aggregated keyboard usage transition. + button, ///< Aggregated mouse button transition. + motion, ///< Relative mouse movement. + }; + + /** + * @brief A validated boot HID event for the platform adapter. + */ + struct BootHidEvent { + BootHidEventType type {}; ///< Operation type. + int code = 0; ///< Keyboard usage or USB mouse button index starting at one. + bool pressed = false; ///< Digital transition. + int x = 0; ///< Relative X motion. + int y = 0; ///< Relative Y motion. + }; + + /** + * @brief Tracks independent boot devices and merges shared keys and mouse buttons. + */ + class BootHidInput { + public: + using Sink = std::function; ///< Validated event consumer. + /** + * @brief Construct a boot report decoder. + * @param sink Ordered event consumer on the event thread. + */ + explicit BootHidInput(Sink sink); + /** + * @brief Add a keyboard or mouse in boot protocol. + * @param device Unique device generation ID. + * @param protocol One for keyboard, two for mouse. + */ + void connect(int device, int protocol); + /** + * @brief Release all keys or buttons belonging to a removed device. + * @param device Unique device generation ID. + */ + void disconnect(int device); + /** + * @brief Decode a report without reading beyond its storage. + * @param device Unique device generation ID. + * @param bytes Report data. + * @param length Available bytes; boot mice use only the standard first three bytes. + */ + void report(int device, const std::uint8_t *bytes, std::size_t length); + /** + * @brief Release all devices after a platform queue overflow. + */ + void clear(); + + private: + /** + * @brief Retained state for one HID interface. + */ + struct Device { + int protocol = 0; ///< Boot interface protocol. + std::bitset<256> held; ///< Keyboard usages or mouse buttons currently down. + }; + + /** + * @brief Publish aggregate transitions after one device changes state. + * @param device Device being updated. + * @param next New held inputs. + */ + void update(Device &device, const std::bitset<256> &next); + Sink sink_; ///< Validated event consumer. + std::map devices_; ///< Per-interface state. + }; +} // namespace input diff --git a/src/main.cpp b/src/main.cpp index d889c44..e3166f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,8 @@ namespace { state.settings.playAudioOnPc = settings.playAudioOnPc; state.settings.showPerformanceStats = settings.showPerformanceStats; state.settings.playAudioOnXbox = settings.playAudioOnXbox; + state.settings.screenshotDirectory = settings.screenshotDirectory; + state.settings.keyboardDockTop = settings.keyboardDockTop; state.settings.dirty = false; } diff --git a/src/platform/usb_input.cpp b/src/platform/usb_input.cpp new file mode 100644 index 0000000..7718930 --- /dev/null +++ b/src/platform/usb_input.cpp @@ -0,0 +1,213 @@ +/** + * @file src/platform/usb_input.cpp + * @brief Bridges nxdk USB boot HID interfaces into SDL's keyboard and mouse event state. + */ +#include +#include +#include +#include +#include +#include +#include + +// usb.h depends on the Xbox configuration and must precede the HID class declarations. +extern "C" { +#include +#include +} + +#include "src/input/usb_hid.h" + +// These are the vendored SDL2 internal event entrypoints. Calling them on the pump +// thread updates both SDL state and its queue; SDL_PushEvent alone cannot do that. +extern "C" { + /** + * @brief Update SDL keyboard state. + * + * @param state SDL press/release. + * @param scancode USB usage. + * @return SDL event result. + */ + int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode); + /** + * @brief Update SDL pointer state. + * + * @param window Target window. + * @param mouseID Pointer ID. + * @param relative Relative mode. + * @param x X delta. + * @param y Y delta. + * @return SDL event result. + */ + int SDL_SendMouseMotion(SDL_Window *window, Uint32 mouseID, int relative, int x, int y); + /** + * @brief Update SDL mouse buttons. + * + * @param window Target window. + * @param mouseID Pointer ID. + * @param state Press/release. + * @param button SDL button. + * @return SDL event result. + */ + int SDL_SendMouseButton(SDL_Window *window, Uint32 mouseID, Uint8 state, Uint8 button); +} + +namespace { + /** + * @brief Fixed-size transfer copied at DPC level and decoded only by SDL's event thread. + */ + struct UsbReport { + int device = 0; ///< Unique interface generation ID. + int operation = 0; ///< Zero for report, one/two for connect, minus one for disconnect. + std::size_t length = 0; ///< Valid report bytes. + std::array bytes {}; ///< Maximum boot keyboard report. + }; + + std::array reports {}; ///< Single-CPU DPC-to-event-thread queue. + unsigned head = 0; ///< Next queue write index, guarded by DPC IRQL. + unsigned tail = 0; ///< Next queue read index, guarded by DPC IRQL. + bool overflow = false; ///< An overflow requires a full input release. + int nextDevice = 1; ///< Distinguishes reused USB interface addresses. + + /** + * @brief Copy an operation without allocation or SDL calls from an interrupt callback. + * @param report Boot report or lifecycle operation. + */ + void enqueue(const UsbReport &report) { + const KIRQL previous = KeRaiseIrqlToDpcLevel(); + const unsigned next = (head + 1) % reports.size(); + if (next == tail) { + overflow = true; + } else { + reports[head] = report; + head = next; + } + KfLowerIrql(previous); + } + + /** + * @brief Copy one successful interrupt report into the bounded queue. + * @param device HID interface. + * @param endpoint Interrupt endpoint address. + * @param status Transfer result. + * @param data Report bytes. + * @param length Report length. + */ + void receive(HID_DEV_T *device, std::uint16_t endpoint, int status, std::uint8_t *data, std::uint32_t length) { + (void) endpoint; + if (device == nullptr || device->user_data == nullptr) { + return; + } + UsbReport report; + report.device = static_cast(reinterpret_cast(device->user_data)); + if (status < 0) { + report.operation = -1; + } else { + report.length = std::min(length, report.bytes.size()); + if (data == nullptr) { + return; + } + std::memcpy(report.bytes.data(), data, report.length); + } + enqueue(report); + } + + /** + * @brief Negotiate standard boot protocol and start one keyboard or mouse interface. + * @param device New HID interface. + * @param status Connection status. + */ + void connected(HID_DEV_T *device, int status) { + (void) status; + if (device->bSubClassCode != 1 || (device->bProtocolCode != 1 && device->bProtocolCode != 2)) { + return; + } + if (usbh_hid_set_protocol(device, 0) != 0) { + return; + } + const int id = nextDevice++; + device->user_data = reinterpret_cast(static_cast(id)); + enqueue({id, device->bProtocolCode}); + if (usbh_hid_start_int_read(device, 0, receive) != 0) { + enqueue({id, -1}); + device->user_data = nullptr; + } + } + + /** + * @brief Queue releases before the USB driver frees a device's state. + * @param device Removed HID interface. + * @param status Disconnect status. + */ + void disconnected(HID_DEV_T *device, int status) { + (void) status; + if (device->user_data != nullptr) { + enqueue({static_cast(reinterpret_cast(device->user_data)), -1}); + } + } + + /** + * @brief Feed decoded aggregate transitions into SDL on the event thread. + * @param event Validated boot HID input. + */ + void dispatch(const input::BootHidEvent &event) { + if (event.type == input::BootHidEventType::key) { + SDL_SendKeyboardKey(event.pressed ? SDL_PRESSED : SDL_RELEASED, static_cast(event.code)); + } else if (event.type == input::BootHidEventType::motion) { + SDL_SendMouseMotion(SDL_GetKeyboardFocus(), 0, 1, event.x, event.y); + } else { + const Uint8 button = event.code == 1 ? SDL_BUTTON_LEFT : event.code == 2 ? SDL_BUTTON_RIGHT : + SDL_BUTTON_MIDDLE; + SDL_SendMouseButton(SDL_GetKeyboardFocus(), 0, event.pressed ? SDL_PRESSED : SDL_RELEASED, button); + } + } +} // namespace + +/** + * @brief Register boot HID before nxdk's SDL joystick driver begins USB enumeration. + */ +extern "C" void moonlight_usb_input_initialize() { + usbh_hid_init(); + usbh_install_hid_conn_callback(connected, disconnected); +} + +/** + * @brief Drain USB input from SDL's platform event pump without calling SDL at DPC level. + */ +extern "C" void moonlight_usb_input_pump() { + static input::BootHidInput input(dispatch); + for (;;) { + UsbReport report; + const KIRQL previous = KeRaiseIrqlToDpcLevel(); + const bool reset = overflow; + overflow = false; + if (reset) { + tail = head; + } + const bool available = tail != head; + if (available) { + report = reports[tail]; + tail = (tail + 1) % reports.size(); + } + KfLowerIrql(previous); + // Keep device identities after overflow; release their old state and accept future reports. + if (reset) { + input.clear(); + for (auto *device = usbh_hid_get_device_list(); device != nullptr; device = device->next) { + if (device->user_data != nullptr) { + input.connect(static_cast(reinterpret_cast(device->user_data)), device->bProtocolCode); + } + } + } + if (!available) { + break; + } + if (report.operation == -1) { + input.disconnect(report.device); + } else if (report.operation != 0) { + input.connect(report.device, report.operation); + } else { + input.report(report.device, report.bytes.data(), report.length); + } + } +} diff --git a/src/startup/storage_paths.cpp b/src/startup/storage_paths.cpp index aa12c7d..7ab4a78 100644 --- a/src/startup/storage_paths.cpp +++ b/src/startup/storage_paths.cpp @@ -45,4 +45,11 @@ namespace startup { return std::string(entryName); } + std::string screenshot_storage_path(std::string_view directory, std::string_view filename) { + std::string path = directory.empty() ? default_storage_path("screenshots") : std::string(directory); + if (path.back() != '/' && path.back() != '\\') { + path += path.find('\\') == std::string::npos ? '/' : '\\'; + } + return path + std::string(filename); + } } // namespace startup diff --git a/src/startup/storage_paths.h b/src/startup/storage_paths.h index bb3acc6..ba3e8ae 100644 --- a/src/startup/storage_paths.h +++ b/src/startup/storage_paths.h @@ -32,4 +32,11 @@ namespace startup { */ std::string default_storage_path(std::string_view entryName); + /** + * @brief Build a screenshot filename beneath the configured folder or the title's screenshots folder. + * @param directory User-configured directory; empty selects the default. + * @param filename Screenshot basename. + * @return Joined path, preserving the directory's path separator convention. + */ + std::string screenshot_storage_path(std::string_view directory, std::string_view filename); } // namespace startup diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index c4c68af..251d1a9 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -31,6 +31,7 @@ extern "C" { // local includes #include "src/logging/logger.h" +#include "src/platform/filesystem_utils.h" #ifdef NXDK #include @@ -348,8 +349,17 @@ namespace streaming { return presenter_.active() ? "GPU" : (acceleration_.softwareFrames > 0 ? "Software" : "Waiting"); } - void FfmpegStreamBackend::set_statistics_bitmap(int width, int height, std::vector grayscale) { - statisticsOverlay_.set_bitmap(width, height, std::move(grayscale)); + void FfmpegStreamBackend::set_statistics_bitmap(int width, int height, std::vector grayscale, VideoOverlayPlacement placement, std::vector opacity) { + statisticsOverlay_.set_bitmap(width, height, std::move(grayscale), placement, std::move(opacity)); + invalidate_overlay_texture(); + } + + void FfmpegStreamBackend::set_color_overlay(int width, int height, std::vector pixels, VideoOverlayPlacement placement) { + statisticsOverlay_.set_argb_bitmap(width, height, std::move(pixels), placement); + invalidate_overlay_texture(); + } + + void FfmpegStreamBackend::invalidate_overlay_texture() { statisticsBitmapChanged_ = statisticsTextureChanged_ = true; if (statisticsTexture_ != nullptr) { SDL_DestroyTexture(statisticsTexture_); @@ -357,6 +367,55 @@ namespace streaming { } } + bool FfmpegStreamBackend::has_pending_overlay() const { + return statisticsBitmapChanged_; + } + + void FfmpegStreamBackend::render_local_overlay(SDL_Renderer *renderer, int width, int height) { + render_sdl_statistics(renderer, {0, 0, width, height}); + } + + void FfmpegStreamBackend::local_overlay_size(int &width, int &height) const { + if (video_.renderFrame.width > 0 && video_.renderFrame.height > 0) { + const auto destination = build_letterboxed_destination(width, height, video_.renderFrame.width, video_.renderFrame.height); + width = destination.w; + height = destination.h; + } + } + + bool FfmpegStreamBackend::save_screenshot(const std::string &path) { + // renderFrame belongs exclusively to the event thread and contains no composited UI. + const auto &frame = video_.renderFrame; + if (frame.width <= 0 || frame.height <= 0) { + logging::warn("stream", "Cannot take a screenshot before the first video frame is presented"); + return false; + } + if (!platform::ensure_directory_exists(platform::parent_directory(path))) { + return false; + } + SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, frame.width, frame.height, 24, SDL_PIXELFORMAT_BGR24); + if (surface == nullptr) { + return false; + } + SwsContext *conversion = sws_getContext(frame.width, frame.height, AV_PIX_FMT_YUV420P, frame.width, frame.height, AV_PIX_FMT_BGR24, SWS_POINT, nullptr, nullptr, nullptr); + bool saved = false; + if (conversion != nullptr) { + const int *coefficients = sws_getCoefficients(frame.matrix == VideoColorMatrix::bt709 ? SWS_CS_ITU709 : SWS_CS_ITU601); + sws_setColorspaceDetails(conversion, coefficients, frame.fullRange, coefficients, 1, 0, 1 << 16, 1 << 16); + const std::array source {frame.yPlane.data(), frame.uPlane.data(), frame.vPlane.data(), nullptr}; + const std::array sourcePitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; + std::array destination {static_cast(surface->pixels), nullptr, nullptr, nullptr}; + const std::array destinationPitch {surface->pitch, 0, 0, 0}; + saved = sws_scale(conversion, source.data(), sourcePitch.data(), 0, frame.height, destination.data(), destinationPitch.data()) == frame.height && SDL_SaveBMP(surface, path.c_str()) == 0; + sws_freeContext(conversion); + } + SDL_FreeSurface(surface); + if (!saved) { + logging::warn("stream", "Failed to save screenshot: " + path); + } + return saved; + } + FfmpegStreamBackend::~FfmpegStreamBackend() { shutdown(); } @@ -598,6 +657,9 @@ namespace streaming { } if (statisticsTexture_ == nullptr) { statisticsTexture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); + if (statisticsTexture_ != nullptr) { + SDL_SetTextureBlendMode(statisticsTexture_, SDL_BLENDMODE_BLEND); + } statisticsTextureChanged_ = true; } if (statisticsTexture_ == nullptr) { @@ -610,8 +672,10 @@ namespace streaming { } statisticsTextureChanged_ = false; } - const SDL_Rect panel {destination.x + VideoOverlay::MARGIN, destination.y + VideoOverlay::MARGIN, width, height}; - SDL_RenderCopy(renderer, statisticsTexture_, nullptr, &panel); + const auto bounds = statisticsOverlay_.rectangle({destination.x, destination.y, destination.w, destination.h}); + const SDL_Rect panel {bounds.x, bounds.y, bounds.width, bounds.height}; + const SDL_Rect source {0, 0, bounds.width, bounds.height}; + SDL_RenderCopy(renderer, statisticsTexture_, &source, &panel); } int FfmpegStreamBackend::setup_video_decoder(int videoFormat, int width, int height, int redrawRate, int drFlags) { diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index e82fae6..76bc089 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -97,9 +97,47 @@ namespace streaming { * @brief Replace the shared GPU/software statistics bitmap on the render thread. * @param width Bitmap width. * @param height Bitmap height. - * @param grayscale Packed grayscale pixels with an opaque background. + * @param grayscale Packed, unpremultiplied grayscale pixels. + * @param placement Panel anchor shared by GPU and software paths. + * @param opacity Optional per-pixel coverage; empty for opaque statistics. */ - void set_statistics_bitmap(int width, int height, std::vector grayscale); + void set_statistics_bitmap(int width, int height, std::vector grayscale, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left, std::vector opacity = {}); + /** + * @brief Replace the same shared overlay with colored UI pixels on the render thread. + * @param width Bitmap width. + * @param height Bitmap height. + * @param pixels Straight ARGB bytes preserving the bundled assets' colors and opacity. + * @param placement Panel anchor shared by GPU and software paths. + */ + void set_color_overlay(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left); + + /** + * @brief Check whether an overlay change requires presenting an otherwise idle frame. + * @return True when the shared bitmap changed since the last presentation. + */ + bool has_pending_overlay() const; + + /** + * @brief Draw local controls before a stream has produced its first video frame. + * @param renderer SDL renderer. + * @param width Output width. + * @param height Output height. + */ + void render_local_overlay(SDL_Renderer *renderer, int width, int height); + + /** + * @brief Save the current clean decoded frame as a BMP, independently of presentation path. + * @param path Destination file under writable Moonlight storage. + * @return True when conversion and file writing succeed. + */ + bool save_screenshot(const std::string &path); + + /** + * @brief Resolve the video rectangle available to shared GPU/software controls. + * @param width Output width, replaced with the fitted video width. + * @param height Output height, replaced with the fitted video height. + */ + void local_overlay_size(int &width, int &height) const; /** * @brief Render the latest decoded video frame to the supplied renderer. @@ -236,6 +274,10 @@ namespace streaming { * @param destination Output video rectangle. */ void render_sdl_statistics(SDL_Renderer *renderer, const SDL_Rect &destination); + /** + * @brief Mark either bitmap format changed and discard the previous SDL texture. + */ + void invalidate_overlay_texture(); /** * @brief Record a retrieved frame before any client discard. * diff --git a/src/streaming/pvideo_presenter.cpp b/src/streaming/pvideo_presenter.cpp index 8c927d6..c1e3345 100644 --- a/src/streaming/pvideo_presenter.cpp +++ b/src/streaming/pvideo_presenter.cpp @@ -269,7 +269,7 @@ namespace streaming { } if (failure.empty()) { if (overlay != nullptr) { - overlay->draw_yuy2(buffers_[nextBuffer_], bufferSize_, pitch_, frame.width, frame.height, destination); + overlay->draw_yuy2(buffers_[nextBuffer_], bufferSize_, pitch_, frame.width, frame.height, destination, frame.matrix); } __atomic_thread_fence(__ATOMIC_SEQ_CST); // Rotate memory buffers through overlay bank zero. xemu composes bank diff --git a/src/streaming/pvideo_presenter.h b/src/streaming/pvideo_presenter.h index 0deb3d5..871c7fc 100644 --- a/src/streaming/pvideo_presenter.h +++ b/src/streaming/pvideo_presenter.h @@ -49,7 +49,7 @@ namespace streaming { * @param destination Letterboxed output rectangle. * @param error Receives the reason acceleration could not be used. * @param sampling Optional crop used by the standalone prediction experiment. - * @param overlay Optional cached statistics bitmap, composited before GPU scanout. + * @param overlay Optional cached UI or statistics, composited using the frame's color matrix before GPU scanout. * * @return True after the overlay registers have accepted the new buffer. */ diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index 6c94ca8..6d96349 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -10,6 +10,7 @@ #include "src/startup/memory_stats.h" #include "src/streaming/ffmpeg_stream_backend.h" #include "src/streaming/stats_overlay.h" +#include "src/streaming/stream_controls.h" #include "third-party/moonlight-common-c/src/Limelight.h" #include @@ -64,7 +65,6 @@ namespace { constexpr Uint8 ACCENT_BLUE = 0xD4; constexpr Uint32 STREAM_FRAME_DELAY_MILLISECONDS = 16U; constexpr Uint32 STREAM_PRESENT_POLL_MILLISECONDS = 2U; - constexpr Uint32 STREAM_INPUT_POLL_MILLISECONDS = 4U; constexpr Uint32 STREAM_END_STATS_POLL_MILLISECONDS = 50U; constexpr Uint32 STREAM_VIDEO_IDLE_IDR_MILLISECONDS = 1500U; constexpr Uint32 STREAM_VIDEO_IDLE_IDR_COOLDOWN_MILLISECONDS = 2000U; @@ -76,125 +76,6 @@ namespace { constexpr int MAX_STREAM_BITRATE_KBPS = 50000; constexpr int DEFAULT_PACKET_SIZE = 1024; constexpr std::size_t MAX_CONNECTION_PROTOCOL_MESSAGES = 24U; - constexpr uint16_t PRESENT_GAMEPAD_MASK = 0x0001; - constexpr uint32_t CONTROLLER_BUTTON_CAPABILITIES = - static_cast(A_FLAG | B_FLAG | X_FLAG | Y_FLAG | UP_FLAG | DOWN_FLAG | LEFT_FLAG | RIGHT_FLAG | LB_FLAG | RB_FLAG | PLAY_FLAG | BACK_FLAG | LS_CLK_FLAG | RS_CLK_FLAG | SPECIAL_FLAG); - constexpr uint16_t CONTROLLER_CAPABILITIES = LI_CCAP_ANALOG_TRIGGERS | LI_CCAP_RUMBLE; - - struct ControllerSnapshot { - int buttonFlags = 0; - unsigned char leftTrigger = 0; - unsigned char rightTrigger = 0; - short leftStickX = 0; - short leftStickY = 0; - short rightStickX = 0; - short rightStickY = 0; - }; - - /** - * @brief Check whether a sampled controller has a button or trigger pressed. - * - * @param snapshot Sampled controller input. - * @return True when the sample can dismiss the stream summary. - */ - bool controller_snapshot_has_button_press(const ControllerSnapshot &snapshot) { - return snapshot.buttonFlags != 0 || snapshot.leftTrigger != 0 || snapshot.rightTrigger != 0; - } - - /** - * @brief Own the controller on the event thread and publish snapshots to the input sender. - */ - class StreamControllerHandle { - public: - /** - * @brief Adopt the primary controller reference and open other devices for local quit detection. - * - * @param controller Owned primary controller reference used for forwarded gameplay input. - */ - void reset(SDL_GameController *controller); - - /** - * @brief Close all owned controller references and clear the published state. - */ - void close(); - - /** - * @brief Open a newly connected controller for gameplay or additional local quit detection. - * - * @param deviceIndex SDL joystick device index reported by the add event. - */ - void open_if_needed(int deviceIndex); - - /** - * @brief Close the controller when SDL reports that its joystick was removed. - * - * @param joystickInstanceId SDL joystick instance identifier from the remove event. - */ - void close_if_removed(int joystickInstanceId); - - /** - * @brief Sample SDL controller state on the event thread after pumping events. - * - * @return True when any connected controller has the complete digital quit chord pressed. - */ - bool poll(); - - /** - * @brief Check all sampled controllers when waiting for summary dismissal. - * - * @return True when any controller has a digital button or trigger pressed. - */ - [[nodiscard]] bool any_button_pressed() const; - - /** - * @brief Transfer the controller used to dismiss the summary back to the shell without closing it. - * - * @return Owned reference to a pressed controller, another connected controller, or nullptr. - */ - [[nodiscard]] SDL_GameController *take_controller_for_shell(); - - /** - * @brief Apply a queued digital button transition to its originating controller's exit chord. - * - * @param event Button press or release delivered by SDL. - * @return True if the transition completes the digital exit chord. - */ - bool process_button_event(const SDL_ControllerButtonEvent &event); - - /** - * @brief Describe mapped and raw digital button state for an opt-in diagnostic build. - * - * @return Lines showing connectivity, active device, button state and sampling progress. - */ - [[nodiscard]] std::vector diagnostic_lines() const; - - /** - * @brief Read the last published snapshot without accessing SDL from the sender thread. - * - * @param controllerPresent Optional output set to true when a controller was read. - * @return Current controller input state, or a zeroed snapshot when unavailable. - */ - [[nodiscard]] ControllerSnapshot read_snapshot(bool *controllerPresent) const; - - private: - /** - * @brief Additional controller monitored independently for local quit and summary dismissal. - */ - struct QuitController { - SDL_GameController *handle = nullptr; ///< Owned SDL reference, closed with the stream UI. - input::StreamExitChord chord; ///< Per-device digital event state; never combined with another pad. - ControllerSnapshot snapshot {}; ///< Most recent state sampled on the event thread. - }; - - mutable std::mutex mutex_; ///< Guards the published snapshot shared with the input sender. - SDL_GameController *controller_ = nullptr; ///< SDL handle accessed only on the event thread. - ControllerSnapshot snapshot_ {}; ///< Last input state sampled by the event thread. - bool controllerPresent_ = false; ///< Connection state of the published input sample. - input::StreamExitChord exitChord_; ///< Digital button state in SDL event order, accessed by the event thread. - std::vector quitControllers_; ///< Other connected controllers allowed to stop the local session. - std::uint32_t sampleCount_ = 0; ///< Number of snapshots published on the event thread. - std::uint32_t buttonEventCount_ = 0; ///< Number of button transitions delivered to the event thread. - }; struct StreamUiResources { SDL_Renderer *renderer = nullptr; @@ -203,7 +84,7 @@ namespace { TTF_Font *statisticsFont = nullptr; ///< Font used only when rebuilding the live panel. bool statisticsEnabled = false; ///< Performance Stats controls the live panel and end metrics. streaming::VideoStatisticsSampler statisticsSampler; ///< One-second telemetry intervals. - StreamControllerHandle controller; + streaming::StreamControls controller; streaming::FfmpegStreamBackend mediaBackend {}; bool ttfInitialized = false; }; @@ -263,12 +144,6 @@ namespace { StreamProtocolLogBuffer protocolLog; ///< Recent protocol messages reported by Moonlight callbacks. }; - struct StreamInputThreadState { - StreamUiResources *resources = nullptr; - StreamConnectionState *connectionState = nullptr; - std::atomic stopRequested = false; - }; - struct StreamStartContext { StreamConnectionState *connectionState = nullptr; STREAM_CONFIGURATION streamConfiguration {}; @@ -802,7 +677,7 @@ namespace { } resources->controller.reset(open_primary_controller(preferredController)); - if (resources->statisticsEnabled) { + { resources->statisticsFont = TTF_OpenFont(fontPath.c_str(), std::max(12, videoMode.height / 45)); if (resources->statisticsFont == nullptr) { resources->statisticsEnabled = false; @@ -1145,336 +1020,26 @@ namespace { */ void request_stream_stop(StreamConnectionState *connectionState) { if (connectionState != nullptr && !connectionState->stopRequested.exchange(true)) { - logging::info("input", "Local stream quit requested by Back + Black + White (LB + RB)"); + logging::info("input", "Local stream stop confirmed"); } } + /** + * @brief Route stream input and local controls on the SDL event thread. + * @param resources Input owner, fonts and video backend. + * @param connectionState Transport readiness and stop request. + */ void pump_stream_events(StreamUiResources *resources, StreamConnectionState *connectionState) { - SDL_PumpEvents(); - SDL_Event event {}; - // Pump once: polling again for every queued event can starve input sampling on noisy devices. - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0) { - if (resources == nullptr) { - continue; - } - - if (event.type == SDL_CONTROLLERDEVICEADDED) { - const SDL_ControllerDeviceEvent controllerEvent = copy_controller_device_event(event); - open_controller_if_needed(resources, controllerEvent.which); - } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { - const SDL_ControllerDeviceEvent controllerEvent = copy_controller_device_event(event); - close_controller_if_removed(resources, controllerEvent.which); - } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) { - SDL_ControllerButtonEvent buttonEvent {}; - std::memcpy(&buttonEvent, &event, sizeof(buttonEvent)); - if (resources->controller.process_button_event(buttonEvent)) { - request_stream_stop(connectionState); - } - } - } - if (resources != nullptr) { - if (resources->controller.poll()) { - request_stream_stop(connectionState); - } - } - } - - unsigned char convert_trigger_axis(Sint16 value) { - const int normalized = std::clamp(static_cast(value), 0, 32767); - return static_cast((normalized * 255) / 32767); - } - - ControllerSnapshot read_controller_snapshot(SDL_GameController *controller) { - ControllerSnapshot snapshot {}; - if (controller == nullptr) { - return snapshot; - } - - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A) != 0) { - snapshot.buttonFlags |= A_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_B) != 0) { - snapshot.buttonFlags |= B_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_X) != 0) { - snapshot.buttonFlags |= X_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_Y) != 0) { - snapshot.buttonFlags |= Y_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP) != 0) { - snapshot.buttonFlags |= UP_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN) != 0) { - snapshot.buttonFlags |= DOWN_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT) != 0) { - snapshot.buttonFlags |= LEFT_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) != 0) { - snapshot.buttonFlags |= RIGHT_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) != 0) { - snapshot.buttonFlags |= LB_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) != 0) { - snapshot.buttonFlags |= RB_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_START) != 0) { - snapshot.buttonFlags |= PLAY_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_BACK) != 0) { - snapshot.buttonFlags |= BACK_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSTICK) != 0) { - snapshot.buttonFlags |= LS_CLK_FLAG; - } - if (SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSTICK) != 0) { - snapshot.buttonFlags |= RS_CLK_FLAG; - } - - snapshot.leftTrigger = convert_trigger_axis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT)); - snapshot.rightTrigger = convert_trigger_axis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)); - snapshot.leftStickX = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX); - snapshot.leftStickY = static_cast(-SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY)); - snapshot.rightStickX = SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX); - snapshot.rightStickY = static_cast(-SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY)); - return snapshot; - } - - void StreamControllerHandle::reset(SDL_GameController *controller) { - controller_ = controller; - for (int deviceIndex = 0; deviceIndex < SDL_NumJoysticks(); ++deviceIndex) { - open_if_needed(deviceIndex); - } - poll(); - } - - void StreamControllerHandle::close() { - close_controller(controller_); - controller_ = nullptr; - for (const auto &controller : quitControllers_) { - close_controller(controller.handle); - } - quitControllers_.clear(); - poll(); - } - - void StreamControllerHandle::open_if_needed(int deviceIndex) { - if (!SDL_IsGameController(deviceIndex)) { - return; - } - const SDL_JoystickID instance = SDL_JoystickGetDeviceInstanceID(deviceIndex); - const auto matches = [instance](SDL_GameController *handle) { - return handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(handle)) == instance; - }; - if (matches(controller_) || std::any_of(quitControllers_.begin(), quitControllers_.end(), [&](const QuitController &controller) { - return matches(controller.handle); - })) { - return; - } - if (SDL_GameController *controller = SDL_GameControllerOpen(deviceIndex); controller != nullptr) { - if (controller_ == nullptr) { - controller_ = controller; - } else { - quitControllers_.push_back({controller, {}, {}}); - } - logging::info("input", "Opened stream input device " + std::to_string(instance) + " for local quit detection"); - poll(); - } - } - - void StreamControllerHandle::close_if_removed(int joystickInstanceId) { - SDL_Joystick *joystick = controller_ != nullptr ? SDL_GameControllerGetJoystick(controller_) : nullptr; - if (joystick != nullptr && SDL_JoystickInstanceID(joystick) == joystickInstanceId) { - close_controller(controller_); - controller_ = nullptr; - } - for (auto controller = quitControllers_.begin(); controller != quitControllers_.end();) { - if (SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller->handle)) == joystickInstanceId) { - close_controller(controller->handle); - controller = quitControllers_.erase(controller); - } else { - ++controller; - } - } - if (controller_ == nullptr && !quitControllers_.empty()) { - controller_ = quitControllers_.front().handle; - quitControllers_.erase(quitControllers_.begin()); - } - poll(); - } - - bool StreamControllerHandle::poll() { - const bool controllerPresent = controller_ != nullptr && SDL_GameControllerGetAttached(controller_) != SDL_FALSE; - const ControllerSnapshot snapshot = controllerPresent ? read_controller_snapshot(controller_) : ControllerSnapshot {}; - bool quitRequested = exitChord_.update(snapshot.buttonFlags, controllerPresent); - for (auto &controller : quitControllers_) { - const bool present = SDL_GameControllerGetAttached(controller.handle) != SDL_FALSE; - controller.snapshot = present ? read_controller_snapshot(controller.handle) : ControllerSnapshot {}; - quitRequested = controller.chord.update(controller.snapshot.buttonFlags, present) || quitRequested; - } - ++sampleCount_; - std::scoped_lock lock(mutex_); - snapshot_ = snapshot; - controllerPresent_ = controllerPresent; - return quitRequested; - } - - bool StreamControllerHandle::any_button_pressed() const { - bool present = false; - const ControllerSnapshot snapshot = read_snapshot(&present); - return (present && controller_snapshot_has_button_press(snapshot)) || std::any_of(quitControllers_.begin(), quitControllers_.end(), [](const QuitController &controller) { - return controller_snapshot_has_button_press(controller.snapshot); - }); - } - - SDL_GameController *StreamControllerHandle::take_controller_for_shell() { - poll(); - bool present = false; - const ControllerSnapshot snapshot = read_snapshot(&present); - if (present && controller_snapshot_has_button_press(snapshot)) { - return std::exchange(controller_, nullptr); - } - const auto takeAdditional = [this](std::vector::iterator controller) { - SDL_GameController *handle = controller->handle; - quitControllers_.erase(controller); - return handle; - }; - const auto pressed = std::find_if(quitControllers_.begin(), quitControllers_.end(), [](const QuitController &controller) { - return SDL_GameControllerGetAttached(controller.handle) != SDL_FALSE && controller_snapshot_has_button_press(controller.snapshot); - }); - if (pressed != quitControllers_.end()) { - return takeAdditional(pressed); - } - if (present) { - return std::exchange(controller_, nullptr); - } - const auto connected = std::find_if(quitControllers_.begin(), quitControllers_.end(), [](const QuitController &controller) { - return SDL_GameControllerGetAttached(controller.handle) != SDL_FALSE; - }); - return connected != quitControllers_.end() ? takeAdditional(connected) : nullptr; - } - - bool StreamControllerHandle::process_button_event(const SDL_ControllerButtonEvent &event) { - ++buttonEventCount_; - std::uint32_t flag = 0; - switch (event.button) { - case SDL_CONTROLLER_BUTTON_BACK: - flag = BACK_FLAG; - break; - case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: - flag = LB_FLAG; - break; - case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: - flag = RB_FLAG; - break; - default: - return false; - } - if (controller_ != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller_)) == event.which) { - return exitChord_.update_button(flag, event.state == SDL_PRESSED); - } - for (auto &controller : quitControllers_) { - if (SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller.handle)) == event.which) { - return controller.chord.update_button(flag, event.state == SDL_PRESSED); - } - } - return false; - } - - std::vector StreamControllerHandle::diagnostic_lines() const { - bool present = false; - const ControllerSnapshot snapshot = read_snapshot(&present); - SDL_Joystick *joystick = controller_ != nullptr ? SDL_GameControllerGetJoystick(controller_) : nullptr; - const auto button = [&](std::uint32_t flag) { - return (snapshot.buttonFlags & flag) != 0 ? "1" : "0"; - }; - const auto raw = [&](int index) { - return joystick != nullptr && SDL_JoystickGetButton(joystick, index) != 0 ? "1" : "0"; - }; - std::vector lines { - "DIGITAL QUIT: Back + White/LB + Black/RB on any pad", - std::string("Mapped Back=") + button(BACK_FLAG) + " LB=" + button(LB_FLAG) + " RB=" + button(RB_FLAG) + " | raw=" + raw(6) + "/" + raw(4) + "/" + raw(5), - std::string(present ? "Connected" : "DISCONNECTED") + " | id=" + std::to_string(joystick != nullptr ? SDL_JoystickInstanceID(joystick) : -1) + " | samples=" + std::to_string(sampleCount_) + " events=" + std::to_string(buttonEventCount_), - std::string("SDL focus=") + (SDL_GetKeyboardFocus() != nullptr ? "yes" : "none") + " | background input=" + (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, SDL_FALSE) ? "yes" : "no") + " | devices=" + std::to_string(SDL_NumJoysticks()), - }; - for (const auto &controller : quitControllers_) { - const auto flags = controller.snapshot.buttonFlags; - lines.push_back("Quit pad id=" + std::to_string(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller.handle))) + " Back=" + std::to_string((flags & BACK_FLAG) != 0) + " LB=" + std::to_string((flags & LB_FLAG) != 0) + " RB=" + std::to_string((flags & RB_FLAG) != 0)); - } - return lines; - } - - ControllerSnapshot StreamControllerHandle::read_snapshot(bool *controllerPresent) const { - std::scoped_lock lock(mutex_); - if (controllerPresent != nullptr) { - *controllerPresent = controllerPresent_; - } - return snapshot_; - } - - bool controller_snapshots_match(const ControllerSnapshot &left, const ControllerSnapshot &right) { - return left.buttonFlags == right.buttonFlags && left.leftTrigger == right.leftTrigger && left.rightTrigger == right.rightTrigger && left.leftStickX == right.leftStickX && left.leftStickY == right.leftStickY && - left.rightStickX == right.rightStickX && left.rightStickY == right.rightStickY; - } - - void update_stream_exit_combo_from_snapshot(const ControllerSnapshot &snapshot, bool controllerPresent, StreamConnectionState *connectionState) { - if (input::is_stream_exit_requested(snapshot.buttonFlags, controllerPresent)) { + int width = 0; + int height = 0; + SDL_GetRendererOutputSize(resources->renderer, &width, &height); + const bool wasVisible = resources->controller.visible(); + if (resources->controller.pump(resources->mediaBackend, resources->bodyFont, width, height, connectionState->connectionStarted.load(), resources->statisticsEnabled)) { request_stream_stop(connectionState); } - } - - ControllerSnapshot read_controller_snapshot(const StreamUiResources *resources, bool *controllerPresent) { - if (controllerPresent != nullptr) { - *controllerPresent = false; + if (wasVisible && !resources->controller.visible()) { + resources->statisticsSampler = {}; } - if (resources == nullptr) { - return {}; - } - - return resources->controller.read_snapshot(controllerPresent); - } - - void send_controller_snapshot_if_needed(const ControllerSnapshot &snapshot, bool controllerPresent, bool *arrivalSent, ControllerSnapshot *lastSnapshot) { - if (!controllerPresent || arrivalSent == nullptr || lastSnapshot == nullptr) { - return; - } - - if (!*arrivalSent) { - LiSendControllerArrivalEvent(0, PRESENT_GAMEPAD_MASK, LI_CTYPE_XBOX, CONTROLLER_BUTTON_CAPABILITIES, CONTROLLER_CAPABILITIES); - *arrivalSent = true; - } - - if (controller_snapshots_match(snapshot, *lastSnapshot)) { - return; - } - - LiSendControllerEvent(snapshot.buttonFlags, snapshot.leftTrigger, snapshot.rightTrigger, snapshot.leftStickX, snapshot.leftStickY, snapshot.rightStickX, snapshot.rightStickY); - *lastSnapshot = snapshot; - } - - int run_stream_input_thread(void *context) { - auto *inputThreadState = static_cast(context); - if (inputThreadState == nullptr || inputThreadState->resources == nullptr || inputThreadState->connectionState == nullptr) { - return -1; - } - - if (SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH) != 0) { - logging::debug("stream", std::string("SDL_SetThreadPriority failed for stream input: ") + SDL_GetError()); - } - - bool controllerArrivalSent = false; - ControllerSnapshot lastControllerSnapshot {}; - - while (!inputThreadState->stopRequested.load() && !inputThreadState->connectionState->connectionTerminated.load() && !inputThreadState->connectionState->stopRequested.load()) { - bool controllerPresent = false; - const ControllerSnapshot snapshot = read_controller_snapshot(inputThreadState->resources, &controllerPresent); - send_controller_snapshot_if_needed(snapshot, controllerPresent, &controllerArrivalSent, &lastControllerSnapshot); - SDL_Delay(STREAM_INPUT_POLL_MILLISECONDS); - } - - return 0; } /** @@ -1532,7 +1097,7 @@ namespace { * @return True when the video must be refreshed to display a new panel. */ bool update_stream_statistics_overlay(const StreamStartContext &context, const StreamConnectionState &connectionState, StreamUiResources *resources) { - if (resources == nullptr || !resources->statisticsEnabled || resources->statisticsFont == nullptr || !resources->mediaBackend.has_decoded_video()) { + if (resources == nullptr || resources->controller.visible() || !resources->statisticsEnabled || resources->statisticsFont == nullptr || !resources->mediaBackend.has_decoded_video()) { return false; } const auto now = PltGetMicroseconds(); @@ -1677,6 +1242,9 @@ namespace { render_text_lines(resources->renderer, resources->bodyFont, lines, screenWidth, &cursorY); + if (resources->controller.visible() && mediaBackend != nullptr) { + mediaBackend->render_local_overlay(resources->renderer, screenWidth, screenHeight); + } SDL_RenderPresent(resources->renderer); render_stream_input_diagnostics(resources); return true; @@ -1792,9 +1360,6 @@ namespace { if (SDL_Thread *startThread = SDL_CreateThread(run_stream_start_thread, "start-stream", &attempt.startContext); startThread != nullptr) { while (!attempt.connectionState.startCompleted.load() && !attempt.connectionState.stopRequested.load()) { pump_stream_events(&attempt.resources, &attempt.connectionState); - bool controllerPresent = false; - const ControllerSnapshot snapshot = read_controller_snapshot(&attempt.resources, &controllerPresent); - update_stream_exit_combo_from_snapshot(snapshot, controllerPresent, &attempt.connectionState); if (attempt.connectionState.stopRequested.load()) { render_stream_stopping_frame(&attempt.resources); LiInterruptConnection(); @@ -1843,25 +1408,10 @@ namespace { return run_stream_start_attempt(attempt); } - void poll_fallback_controller_if_needed( - const SDL_Thread *inputThread, - const StreamUiResources *resources, - bool *controllerArrivalSent, - ControllerSnapshot *lastControllerSnapshot - ) { - if (inputThread != nullptr) { - return; - } - - bool controllerPresent = false; - const ControllerSnapshot snapshot = read_controller_snapshot(resources, &controllerPresent); - send_controller_snapshot_if_needed(snapshot, controllerPresent, controllerArrivalSent, lastControllerSnapshot); - } - void render_active_stream_frame_if_needed(const ActiveStreamLoopContext &loopContext, Uint32 *lastIdleVideoIdrRequestTick) { const bool hasDecodedVideo = loopContext.resources.mediaBackend.has_decoded_video(); const bool statisticsChanged = update_stream_statistics_overlay(loopContext.startContext, loopContext.connectionState, &loopContext.resources); - if (const bool shouldRenderFrame = !hasDecodedVideo || statisticsChanged || loopContext.resources.mediaBackend.has_unrendered_video_frame(); shouldRenderFrame) { + if (const bool shouldRenderFrame = !hasDecodedVideo || statisticsChanged || loopContext.resources.mediaBackend.has_pending_overlay() || loopContext.resources.mediaBackend.has_unrendered_video_frame(); shouldRenderFrame) { render_stream_frame( loopContext.host, loopContext.app, @@ -1875,45 +1425,21 @@ namespace { SDL_Delay(hasDecodedVideo ? STREAM_PRESENT_POLL_MILLISECONDS : STREAM_FRAME_DELAY_MILLISECONDS); } - void wait_for_stream_input_thread(SDL_Thread *inputThread, StreamInputThreadState *inputThreadState) { - if (inputThreadState != nullptr) { - inputThreadState->stopRequested.store(true); - } - if (inputThread == nullptr) { - return; - } - - int inputThreadResult = 0; - SDL_WaitThread(inputThread, &inputThreadResult); - (void) inputThreadResult; - } - + /** + * @brief Pump input and render until transport termination or confirmed local quit. + * @param loopContext Active stream resources. + */ void run_active_stream_loop(const ActiveStreamLoopContext &loopContext) { - StreamInputThreadState inputThreadState {}; - inputThreadState.resources = &loopContext.resources; - inputThreadState.connectionState = &loopContext.connectionState; - SDL_Thread *inputThread = SDL_CreateThread(run_stream_input_thread, "stream-input", &inputThreadState); - if (inputThread == nullptr) { - logging::warn("stream", std::string("Failed to start the stream input thread; polling controller from the render loop: ") + SDL_GetError()); - } - - bool fallbackControllerArrivalSent = false; - ControllerSnapshot fallbackLastControllerSnapshot {}; Uint32 lastIdleVideoIdrRequestTick = 0U; while (!loopContext.connectionState.connectionTerminated.load() && !loopContext.connectionState.stopRequested.load()) { pump_stream_events(&loopContext.resources, &loopContext.connectionState); - bool controllerPresent = false; - const ControllerSnapshot snapshot = read_controller_snapshot(&loopContext.resources, &controllerPresent); - update_stream_exit_combo_from_snapshot(snapshot, controllerPresent, &loopContext.connectionState); if (loopContext.connectionState.stopRequested.load()) { render_stream_stopping_frame(&loopContext.resources); break; } - poll_fallback_controller_if_needed(inputThread, &loopContext.resources, &fallbackControllerArrivalSent, &fallbackLastControllerSnapshot); render_active_stream_frame_if_needed(loopContext, &lastIdleVideoIdrRequestTick); } - - wait_for_stream_input_thread(inputThread, &inputThreadState); + loopContext.resources.controller.release_host_input(); } std::string describe_start_failure(const StreamConnectionState &connectionState) { @@ -1956,7 +1482,7 @@ namespace streaming { bool run_stream_session( SDL_Window *window, const VIDEO_MODE &videoMode, - const app::SettingsState &settings, + app::SettingsState &settings, const app::HostRecord &host, const app::HostAppRecord &app, const network::PairingIdentity &clientIdentity, @@ -1979,6 +1505,7 @@ namespace streaming { logging::error("stream", initializationError); return false; } + resources.controller.configure(settings.screenshotDirectory, settings.keyboardDockTop); ScopedThreadPriority streamThreadPriority(SDL_THREAD_PRIORITY_HIGH); startup::log_memory_statistics(); @@ -2086,7 +1613,7 @@ namespace streaming { active_connection_state() = nullptr; const streaming::VideoAccelerationReport acceleration = resources.mediaBackend.acceleration_report(); finalStatistics.accelerationSummary = acceleration.summary_lines(); - finalStatistics.showPerformanceMetrics = settings.showPerformanceStats; + finalStatistics.showPerformanceMetrics = resources.statisticsEnabled; std::string finalMessage = describe_session_end(connectionState, app.name); if (!acceleration.fallbackReason.empty()) { @@ -2097,6 +1624,10 @@ namespace streaming { logging::info("stream", line); } startup::log_memory_statistics(); + if (settings.keyboardDockTop != resources.controller.keyboard_docked_top()) { + settings.keyboardDockTop = resources.controller.keyboard_docked_top(); + settings.dirty = true; + } show_stream_end_statistics(host, app, startContext, finalStatistics, finalMessage, &resources); if (returnController != nullptr) { *returnController = resources.controller.take_controller_for_shell(); diff --git a/src/streaming/session.h b/src/streaming/session.h index 389aaee..ea56600 100644 --- a/src/streaming/session.h +++ b/src/streaming/session.h @@ -32,7 +32,7 @@ namespace streaming { * * @param window Shared SDL window reused from the shell. * @param videoMode Active Xbox video mode. - * @param settings Active shell settings that control stream resolution, bitrate, frame rate, host audio playback, and optional performance metrics in the end-of-stream summary. + * @param settings Active stream preferences; receives OSK placement changes for persistence on return to the shell. * @param host Selected paired host. * @param app Selected host app. * @param clientIdentity Paired client identity used for authenticated launch requests. @@ -44,7 +44,7 @@ namespace streaming { bool run_stream_session( SDL_Window *window, const VIDEO_MODE &videoMode, - const app::SettingsState &settings, + app::SettingsState &settings, const app::HostRecord &host, const app::HostAppRecord &app, const network::PairingIdentity &clientIdentity, diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp new file mode 100644 index 0000000..bb1458d --- /dev/null +++ b/src/streaming/stream_controls.cpp @@ -0,0 +1,1084 @@ +/** + * @file src/streaming/stream_controls.cpp + * @brief Implements dual-stick stream controls and SDL-to-Moonlight input routing. + */ +#include "src/streaming/stream_controls.h" + +#include "src/logging/log_file.h" +#include "src/logging/logger.h" +#include "src/startup/storage_paths.h" +#include "src/streaming/ffmpeg_stream_backend.h" +#include "src/ui/asset_path.h" +#include "src/ui/dialog_surface.h" +#include "src/ui/svg_assets.h" +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include +#include +#include +#include + +namespace { + /** + * @brief Map local menu navigation keys to the shared repeat commands. + * @param key SDL key symbol. + * @return Repeatable local command, or none for activation keys. + */ + input::UiCommand menu_navigation_command(SDL_Keycode key) { + switch (key) { + case SDLK_UP: + return input::UiCommand::move_up; + case SDLK_DOWN: + return input::UiCommand::move_down; + case SDLK_LEFT: + return input::UiCommand::move_left; + case SDLK_RIGHT: + return input::UiCommand::move_right; + default: + return input::UiCommand::none; + } + } + + /** + * @brief Convert a shared repeat command back to its local menu key. + * @param command Repeated navigation command. + * @return SDL key symbol, or unknown when no repeat is due. + */ + SDL_Keycode menu_navigation_key(input::UiCommand command) { + switch (command) { + case input::UiCommand::move_up: + return SDLK_UP; + case input::UiCommand::move_down: + return SDLK_DOWN; + case input::UiCommand::move_left: + return SDLK_LEFT; + case input::UiCommand::move_right: + return SDLK_RIGHT; + default: + return SDLK_UNKNOWN; + } + } + + /** + * @brief Translate an SDL controller button to Moonlight's bit mask. + * @param button SDL controller button. + * @return Host flag, or zero for unsupported buttons. + */ + int controller_flag(int button) { + switch (button) { + case SDL_CONTROLLER_BUTTON_A: + return A_FLAG; + case SDL_CONTROLLER_BUTTON_B: + return B_FLAG; + case SDL_CONTROLLER_BUTTON_X: + return X_FLAG; + case SDL_CONTROLLER_BUTTON_Y: + return Y_FLAG; + case SDL_CONTROLLER_BUTTON_DPAD_UP: + return UP_FLAG; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + return DOWN_FLAG; + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + return LEFT_FLAG; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + return RIGHT_FLAG; + case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + return LB_FLAG; + case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + return RB_FLAG; + case SDL_CONTROLLER_BUTTON_START: + return PLAY_FLAG; + case SDL_CONTROLLER_BUTTON_BACK: + return BACK_FLAG; + case SDL_CONTROLLER_BUTTON_LEFTSTICK: + return LS_CLK_FLAG; + case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + return RS_CLK_FLAG; + case SDL_CONTROLLER_BUTTON_GUIDE: + return SPECIAL_FLAG; + default: + return 0; + } + } + + /** + * @brief Resolve a raw joystick button bound to one of the three menu shortcut buttons. + * @param controller Connected game controller. + * @param button Raw joystick button index. + * @return Shortcut flag, or zero for another control. + */ + int joystick_chord_flag(SDL_GameController *controller, int button) { + int flags = 0; + for (const auto mapped : {SDL_CONTROLLER_BUTTON_BACK, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}) { + const auto binding = SDL_GameControllerGetBindForButton(controller, mapped); + if (binding.bindType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding.value.button == button) { + flags |= controller_flag(mapped); + } + } + return flags; + } + + /** + * @brief Read a connected SDL controller with saturated Y inversion. + * @param handle SDL controller reference. + * @return Complete host-oriented controller sample. + */ + input::ControllerState sample_controller(SDL_GameController *handle) { + input::ControllerState state; + for (int button = 0; button < SDL_CONTROLLER_BUTTON_MAX; ++button) { + if (SDL_GameControllerGetButton(handle, static_cast(button)) != 0) { + state.buttonFlags |= controller_flag(button); + } + } + // Read the shortcut's physical bindings too, independently of translated + // controller events. This also handles a button held across a UI handoff. + SDL_Joystick *joystick = SDL_GameControllerGetJoystick(handle); + for (int button = 0; button < SDL_JoystickNumButtons(joystick); ++button) { + if (SDL_JoystickGetButton(joystick, button) != 0) { + state.buttonFlags |= joystick_chord_flag(handle, button); + } + } + state.leftTrigger = std::clamp(static_cast(SDL_GameControllerGetAxis(handle, SDL_CONTROLLER_AXIS_TRIGGERLEFT)), 0, 32767) * 255 / 32767; + state.rightTrigger = std::clamp(static_cast(SDL_GameControllerGetAxis(handle, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)), 0, 32767) * 255 / 32767; + state.leftStickX = SDL_GameControllerGetAxis(handle, SDL_CONTROLLER_AXIS_LEFTX); + state.leftStickY = input::invert_stick_axis(SDL_GameControllerGetAxis(handle, SDL_CONTROLLER_AXIS_LEFTY)); + state.rightStickX = SDL_GameControllerGetAxis(handle, SDL_CONTROLLER_AXIS_RIGHTX); + state.rightStickY = input::invert_stick_axis(SDL_GameControllerGetAxis(handle, SDL_CONTROLLER_AXIS_RIGHTY)); + return state; + } + + /** + * @brief Enqueue one ordered host input packet; transport calls do not wait for network I/O. + * @param packet Transport-neutral input operation. + */ + void send_packet(const input::StreamPacket &packet) { + const auto &state = packet.controller; + switch (packet.type) { + case input::StreamPacketType::arrival: + { + constexpr std::uint32_t buttons = A_FLAG | B_FLAG | X_FLAG | Y_FLAG | UP_FLAG | DOWN_FLAG | LEFT_FLAG | RIGHT_FLAG | LB_FLAG | RB_FLAG | PLAY_FLAG | BACK_FLAG | LS_CLK_FLAG | RS_CLK_FLAG | SPECIAL_FLAG | MISC_FLAG; + // Rumble is not advertised until a host-to-device feedback callback exists. + LiSendControllerArrivalEvent(packet.slot, packet.mask, LI_CTYPE_XBOX, buttons, LI_CCAP_ANALOG_TRIGGERS); + break; + } + case input::StreamPacketType::controller: + LiSendMultiControllerEvent(packet.slot, packet.mask, state.buttonFlags, state.leftTrigger, state.rightTrigger, state.leftStickX, state.leftStickY, state.rightStickX, state.rightStickY); + break; + case input::StreamPacketType::key: + LiSendKeyboardEvent(packet.code & 0xFF, packet.pressed ? KEY_ACTION_DOWN : KEY_ACTION_UP, packet.modifiers | ((packet.code & 0x100) != 0 ? MODIFIER_EXTENDED : 0)); + break; + case input::StreamPacketType::mouse_button: + LiSendMouseButtonEvent(packet.pressed ? BUTTON_ACTION_PRESS : BUTTON_ACTION_RELEASE, packet.code); + break; + case input::StreamPacketType::mouse_move: + LiSendMouseMoveEvent(packet.x, packet.y); + break; + case input::StreamPacketType::scroll: + if (packet.x != 0) { + LiSendHScrollEvent(packet.x); + } + if (packet.y != 0) { + LiSendScrollEvent(packet.y); + } + break; + } + } + + /** + * @brief Rasterize clipped text with optional leading alignment and a background. + * @param renderer Software panel renderer. + * @param font Label font. + * @param text UTF-8 label. + * @param bounds Label rectangle. + * @param leading Align text to the top-left for console output instead of centering it. + * @param background True to paint a label background; false to retain its key surface. + * @param fitSingleLine Scale an unwrapped key label down when needed to fit SD cells. + * @param foreground Text color, including log severity and focused modal buttons. + */ + void label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading = false, bool background = true, bool fitSingleLine = false, SDL_Color foreground = {255, 255, 255, 255}) { + if (background) { + SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); + SDL_RenderFillRect(renderer, &bounds); + } + std::unique_ptr surface(fitSingleLine ? TTF_RenderUTF8_Blended(font, text.c_str(), foreground) : TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), foreground, std::max(1, bounds.w - 4)), SDL_FreeSurface); + if (!surface) { + return; + } + std::unique_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); + if (!texture) { + return; + } + // nxdk's SDL 2.0.9 software queue can dereference a missing viewport when + // a standalone clip command follows a texture flush. Crop the source instead. + auto layout = streaming::layout_overlay_label(surface->w, surface->h, {bounds.x, bounds.y, bounds.w, bounds.h}, fitSingleLine); + if (leading) { + layout.source.x = layout.source.y = 0; + layout.destination.x = bounds.x; + layout.destination.y = bounds.y; + } + SDL_Rect source {layout.source.x, layout.source.y, layout.source.width, layout.source.height}; + SDL_Rect destination {layout.destination.x, layout.destination.y, layout.destination.width, layout.destination.height}; + SDL_RenderCopy(renderer, texture.get(), &source, &destination); + } +} // namespace + +namespace streaming { + StreamControls::StreamControls(): + router_(send_packet) {} + + StreamControls::~StreamControls() { + close(); + } + + void StreamControls::reset(SDL_GameController *controller) { + close(); + devices_[0].handle = controller; + for (int i = 0; i < SDL_NumJoysticks(); ++i) { + open_if_needed(i); + } + poll(); + } + + void StreamControls::configure(std::string screenshotDirectory, bool keyboardTop) { + screenshotDirectory_ = std::move(screenshotDirectory); + keyboardTop_ = keyboardTop; + } + + bool StreamControls::keyboard_docked_top() const { + return keyboardTop_; + } + + void StreamControls::close() { + icons_.clear(); + for (auto &device : devices_) { + if (device.handle != nullptr) { + SDL_GameControllerClose(device.handle); + } + device = {}; + } + } + + void StreamControls::open_if_needed(int deviceIndex) { + if (!SDL_IsGameController(deviceIndex)) { + return; + } + const auto instance = SDL_JoystickGetDeviceInstanceID(deviceIndex); + for (const auto &device : devices_) { + if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == instance) { + return; + } + } + for (auto &device : devices_) { + if (device.handle == nullptr) { + device = {}; + device.handle = SDL_GameControllerOpen(deviceIndex); + if (device.handle != nullptr) { + logging::info("input", "Opened stream gamepad " + std::to_string(instance)); + } + return; + } + } + } + + void StreamControls::close_if_removed(int instance) { + if (pulseSlot_ >= 0) { + send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); + pulseSlot_ = -1; + } + router_.disconnect(instance); + if (mouseDevice_ == instance) { + mouseDevice_ = -1; + } + if (menu_.owner == instance) { + menu_.owner = -1; + armed_ = true; + } + for (auto &device : devices_) { + if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == instance) { + SDL_GameControllerClose(device.handle); + device = {}; + } + } + // Recover a fifth device that was waiting when the four slots were occupied. + for (int i = 0; i < SDL_NumJoysticks(); ++i) { + open_if_needed(i); + } + } + + bool StreamControls::poll() { + bool chord = false; + for (auto &device : devices_) { + const bool present = device.handle != nullptr && SDL_GameControllerGetAttached(device.handle) != SDL_FALSE; + device.sample = present ? sample_controller(device.handle) : input::ControllerState {}; + chord = input::is_stream_exit_requested(device.sample.buttonFlags, present) || chord; + } + return chord; + } + + bool StreamControls::any_button_pressed() const { + return std::any_of(devices_.begin(), devices_.end(), [](const Device &d) { + return d.sample.buttonFlags != 0 || d.sample.leftTrigger != 0 || d.sample.rightTrigger != 0; + }); + } + + SDL_GameController *StreamControls::take_controller_for_shell() { + poll(); + for (auto &device : devices_) { + if (device.handle != nullptr && (device.sample.buttonFlags != 0 || device.sample.leftTrigger != 0 || device.sample.rightTrigger != 0)) { + return std::exchange(device.handle, nullptr); + } + } + for (auto &device : devices_) { + if (device.handle != nullptr && SDL_GameControllerGetAttached(device.handle) != SDL_FALSE) { + return std::exchange(device.handle, nullptr); + } + } + return nullptr; + } + + std::vector StreamControls::diagnostic_lines() const { + std::vector lines {"STREAM MENU: Back + Black + White"}; + for (const auto &device : devices_) { + if (device.handle != nullptr) { + lines.push_back("Pad " + std::to_string(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle))) + " buttons=" + std::to_string(device.sample.buttonFlags)); + } + } + return lines; + } + + bool StreamControls::visible() const { + return menu_.page != input::StreamMenuPage::closed; + } + + void StreamControls::open_menu(int device) { + if (menu_.page == input::StreamMenuPage::radial) { + return; + } + menu_.open(device); + keyboardNavigation_.reset(); + keyboardPageTriggerDown_ = false; + keyboardDockTriggerDown_ = false; + inputDevice_ = device < 0 ? input::UiInputDevice::keyboard : input::UiInputDevice::gamepad; + router_.set_captured(true); + armed_ = false; + dirty_ = true; + message_.clear(); + logging::info("input", "Opened stream menu from " + (device < 0 ? std::string("keyboard") : "gamepad " + std::to_string(device))); + } + + void StreamControls::tap_key(int code) { + if (code == 0) { + return; + } + const int keyModifiers = menu_.key_modifiers(code); + // No gameplay sample is forwarded during this event-thread-only local key tap. + router_.set_captured(false); + constexpr std::array modifiers {MODIFIER_SHIFT, MODIFIER_CTRL, MODIFIER_ALT, MODIFIER_META}; + constexpr std::array keys {0xA0, 0xA2, 0xA4, 0x15B}; + for (std::size_t i = 0; i < modifiers.size(); ++i) { + if ((keyModifiers & modifiers[i]) != 0) { + router_.key(-3, keys[i], true); + } + } + router_.key(-3, code, true, keyModifiers); + router_.key(-3, code, false, keyModifiers); + router_.release_source(-3); + router_.set_captured(true); + menu_.key_sent(); + dirty_ = true; + } + + bool StreamControls::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) { + dirty_ = true; + if (key == SDLK_ESCAPE) { + if (menu_.page == input::StreamMenuPage::radial) { + menu_.close(); + } else { + menu_.page = input::StreamMenuPage::radial; + menu_.modifiers = 0; + menu_.shiftOnce = false; + } + return false; + } + if (menu_.page == input::StreamMenuPage::keyboard) { + if (key == SDLK_TAB) { + menu_.toggle_keyboard_page(); + return false; + } + if (key == SDLK_LSHIFT || key == SDLK_RSHIFT) { + menu_.toggle_shift(); + return false; + } + if (key == SDLK_CAPSLOCK) { + menu_.caps = !menu_.caps; + return false; + } + if (key == SDLK_F2) { + keyboardTop_ = !keyboardTop_; + return false; + } + if (key == SDLK_BACKSPACE || key == SDLK_SPACE) { + if (active) { + tap_key(key == SDLK_SPACE ? 0x20 : 0x08); + } + return false; + } + } + if (key != SDLK_RETURN && key != SDLK_SPACE) { + if (key == SDLK_TAB && menu_.page == input::StreamMenuPage::radial) { + menu_.selected = menu_.selected == input::StreamAction::none ? input::StreamAction::keyboard : static_cast((static_cast(menu_.selected) + 1) % 8); + } else { + menu_.navigate(key == SDLK_RIGHT ? 1 : key == SDLK_LEFT ? -1 : + 0, + key == SDLK_DOWN ? 1 : key == SDLK_UP ? -1 : + 0); + } + return false; + } + if (menu_.page == input::StreamMenuPage::quit_confirmation) { + if (menu_.confirmQuit) { + return true; + } + menu_.page = input::StreamMenuPage::radial; + return false; + } + if (menu_.page == input::StreamMenuPage::keyboard) { + if (active) { + tap_key(menu_.keyboard_key()); + } + return false; + } + if (menu_.page != input::StreamMenuPage::radial) { + return false; + } + switch (menu_.selected) { + case input::StreamAction::quit: + menu_.page = input::StreamMenuPage::quit_confirmation; + menu_.confirmQuit = false; + break; + case input::StreamAction::keyboard: + menu_.page = input::StreamMenuPage::keyboard; + break; + case input::StreamAction::logs: + menu_.page = input::StreamMenuPage::logs; + logs_.clear(); + for (const auto &entry : logging::snapshot()) { + logs_.push_back(logging::format_entry(entry)); + } + if (logs_.empty()) { + logs_ = logging::load_log_file().lines; + } + break; + case input::StreamAction::mouse: + if (mouseDevice_ < 0 && menu_.owner < 0) { + message_ = "Open the menu with a controller to enable mouse mode."; + break; + } + mouseDevice_ = mouseDevice_ < 0 ? menu_.owner : -1; + router_.set_mouse_device(mouseDevice_); + menu_.close(); + break; + case input::StreamAction::statistics: + stats = !stats; + menu_.close(); + break; + case input::StreamAction::screenshot: + { + const std::string path = startup::screenshot_storage_path(screenshotDirectory_, "screenshot-" + std::to_string(SDL_GetTicks()) + ".bmp"); + message_ = backend.save_screenshot(path) ? "Saved " + path : "Screenshot failed; check the logs."; + logging::info("stream", message_); + break; + } + case input::StreamAction::guide: + case input::StreamAction::share: + { + const int slot = active ? router_.connect(menu_.owner) : -1; + if (slot < 0) { + message_ = "A connected controller and active stream are required."; + break; + } + if (pulseSlot_ >= 0) { + send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); + } + pulseSlot_ = slot; + pulseUntil_ = SDL_GetTicks() + 120; + input::StreamPacket packet {input::StreamPacketType::controller, slot, router_.active_mask()}; + packet.controller.buttonFlags = menu_.selected == input::StreamAction::guide ? SPECIAL_FLAG : MISC_FLAG; + send_packet(packet); + message_ = menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; + break; + } + case input::StreamAction::none: + break; + } + return false; + } + + bool StreamControls::pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled) { + // Sampling and transport enqueueing share one owner so capture cannot race a stale gameplay packet. + SDL_PumpEvents(); + bool stop = false; + const bool wasVisible = visible(); + SDL_Event event {}; + while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0) { + const bool gamepadActivity = event.type == SDL_CONTROLLERBUTTONDOWN || (event.type == SDL_CONTROLLERAXISMOTION && input::is_gamepad_ui_activity(event.caxis.value, event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)); + if (visible() && gamepadActivity && menu_.owner < 0) { + menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; + } + if (visible() && gamepadActivity && inputDevice_ != input::UiInputDevice::gamepad) { + inputDevice_ = input::UiInputDevice::gamepad; + keyboardNavigation_.reset(); + dirty_ = true; + } + if (visible() && menu_.page == input::StreamMenuPage::keyboard && event.type == SDL_CONTROLLERAXISMOTION && event.caxis.which == menu_.owner) { + const bool pressed = event.caxis.value >= 16000; + if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT) { + if (pressed && !keyboardPageTriggerDown_ && armed_) { + menu_.toggle_keyboard_page(); + dirty_ = true; + } + keyboardPageTriggerDown_ = pressed; + } else if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { + if (pressed && !keyboardDockTriggerDown_ && armed_) { + keyboardTop_ = !keyboardTop_; + dirty_ = true; + } + keyboardDockTriggerDown_ = pressed; + } + } + if (event.type == SDL_CONTROLLERDEVICEADDED) { + open_if_needed(event.cdevice.which); + } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { + close_if_removed(event.cdevice.which); + } else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) { + for (auto &device : devices_) { + if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != event.jbutton.which) { + continue; + } + const int flag = joystick_chord_flag(device.handle, event.jbutton.button); + if (flag != 0) { + const bool down = device.chord.update_button(flag, event.jbutton.state == SDL_PRESSED); + if (down && !device.chordDown) { + open_menu(event.jbutton.which); + } + device.chordDown = down; + } + } + } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) { + const auto &button = event.cbutton; + for (auto &device : devices_) { + if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != button.which) { + continue; + } + const int flag = controller_flag(button.button); + const bool down = device.chord.update_button(flag, button.state == SDL_PRESSED); + if (down && !device.chordDown) { + open_menu(button.which); + } + device.chordDown = down; + if (button.state == SDL_PRESSED) { + device.sample.buttonFlags |= flag; + } else { + device.sample.buttonFlags &= ~flag; + } + if (active) { + router_.connect(button.which); + router_.controller(button.which, device.sample); + } + } + if (visible() && armed_ && button.state == SDL_PRESSED) { + if (menu_.owner < 0) { + menu_.owner = button.which; + } + if (button.which != menu_.owner) { + continue; + } + if (button.button >= SDL_CONTROLLER_BUTTON_DPAD_UP && button.button <= SDL_CONTROLLER_BUTTON_DPAD_RIGHT) { + navigationTick_ = SDL_GetTicks(); + } + switch (button.button) { + case SDL_CONTROLLER_BUTTON_A: + stop = menu_key(SDLK_RETURN, backend, active, statisticsEnabled) || stop; + break; + case SDL_CONTROLLER_BUTTON_B: + stop = menu_key(SDLK_ESCAPE, backend, active, statisticsEnabled) || stop; + break; + case SDL_CONTROLLER_BUTTON_DPAD_UP: + menu_key(SDLK_UP, backend, active, statisticsEnabled); + break; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + menu_key(SDLK_DOWN, backend, active, statisticsEnabled); + break; + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + menu_key(SDLK_LEFT, backend, active, statisticsEnabled); + break; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + menu_key(SDLK_RIGHT, backend, active, statisticsEnabled); + break; + case SDL_CONTROLLER_BUTTON_X: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x08); + } + break; + case SDL_CONTROLLER_BUTTON_Y: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x20); + } + break; + case SDL_CONTROLLER_BUTTON_LEFTSTICK: + if (menu_.page == input::StreamMenuPage::keyboard) { + menu_.caps = !menu_.caps; + dirty_ = true; + } + break; + case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + if (menu_.page == input::StreamMenuPage::keyboard) { + menu_.toggle_shift(); + dirty_ = true; + } + break; + case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(button.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ? 0x125 : 0x127); + } + break; + case SDL_CONTROLLER_BUTTON_START: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x0D); + } + break; + default: + break; + } + } + } else if (event.type == SDL_CONTROLLERAXISMOTION && visible() && armed_ && event.caxis.which == menu_.owner && menu_.page == input::StreamMenuPage::radial) { + for (auto &device : devices_) { + if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != event.caxis.which) { + continue; + } + // Preserve the event's deflection even when a queued center event follows it. + auto &sample = device.sample; + switch (event.caxis.axis) { + case SDL_CONTROLLER_AXIS_LEFTX: + sample.leftStickX = event.caxis.value; + break; + case SDL_CONTROLLER_AXIS_LEFTY: + sample.leftStickY = input::invert_stick_axis(event.caxis.value); + break; + case SDL_CONTROLLER_AXIS_RIGHTX: + sample.rightStickX = event.caxis.value; + break; + case SDL_CONTROLLER_AXIS_RIGHTY: + sample.rightStickY = input::invert_stick_axis(event.caxis.value); + break; + default: + break; + } + if (event.caxis.axis <= SDL_CONTROLLER_AXIS_RIGHTY) { + dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; + } + } + } else if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { + const bool down = event.type == SDL_KEYDOWN; + const auto &key = event.key; + const bool shortcut = key.keysym.scancode == SDL_SCANCODE_Q && (key.keysym.mod & KMOD_CTRL) != 0 && (key.keysym.mod & KMOD_ALT) != 0 && (key.keysym.mod & KMOD_SHIFT) != 0; + if (down && key.repeat == 0 && shortcut) { + open_menu(-1); + armed_ = true; + } else if (visible()) { + if (!down) { + keyboardNavigation_.release(key.keysym.scancode); + } + if (down && key.repeat == 0) { + if (inputDevice_ != input::UiInputDevice::keyboard) { + inputDevice_ = input::UiInputDevice::keyboard; + dirty_ = true; + } + const auto command = menu_navigation_command(key.keysym.sym); + if (command != input::UiCommand::none) { + if (keyboardNavigation_.press(key.keysym.scancode, command, SDL_GetTicks()) != input::UiCommand::none) { + menu_key(key.keysym.sym, backend, active, statisticsEnabled); + } + } else { + if (key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_SPACE || key.keysym.sym == SDLK_ESCAPE) { + keyboardNavigation_.reset(); + } + stop = menu_key(key.keysym.sym, backend, active, statisticsEnabled) || stop; + } + } + } else if (active && key.repeat == 0) { + const int mods = ((key.keysym.mod & KMOD_SHIFT) != 0 ? MODIFIER_SHIFT : 0) | ((key.keysym.mod & KMOD_CTRL) != 0 ? MODIFIER_CTRL : 0) | ((key.keysym.mod & KMOD_ALT) != 0 ? MODIFIER_ALT : 0) | ((key.keysym.mod & KMOD_GUI) != 0 ? MODIFIER_META : 0); + router_.key(0, input::keyboard_virtual_key(key.keysym.scancode), down, mods); + } + } else if (active && event.type == SDL_MOUSEMOTION) { + router_.mouse_move(event.motion.xrel, event.motion.yrel); + } else if (active && (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)) { + const int button = event.button.button; + const int hostButton = button == SDL_BUTTON_LEFT ? BUTTON_LEFT : button == SDL_BUTTON_RIGHT ? BUTTON_RIGHT : + button == SDL_BUTTON_MIDDLE ? BUTTON_MIDDLE : + button == SDL_BUTTON_X1 ? BUTTON_X1 : + button == SDL_BUTTON_X2 ? BUTTON_X2 : + 0; + router_.mouse_button(1, hostButton, event.type == SDL_MOUSEBUTTONDOWN); + } else if (active && event.type == SDL_MOUSEWHEEL) { + const int direction = event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1; + router_.scroll(event.wheel.x * direction, event.wheel.y * direction); + } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { + focused_ = false; + keyboardNavigation_.reset(); + router_.set_captured(true); + } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { + focused_ = true; + router_.set_captured(visible()); + } else if (event.type == SDL_QUIT) { + stop = true; + } + } + poll(); + const Uint32 now = SDL_GetTicks(); + if (menu_.page == input::StreamMenuPage::logs && now - logRefreshTick_ >= 1000) { + logRefreshTick_ = now; + std::vector lines; + for (const auto &entry : logging::snapshot()) { + lines.push_back(logging::format_entry(entry)); + } + if (!lines.empty() && lines != logs_) { + // Preserve the same entries when reading older output; offset zero follows live output. + if (menu_.logOffset > 0) { + menu_.logOffset += std::max(0, static_cast(lines.size()) - static_cast(logs_.size())); + } + logs_ = std::move(lines); + dirty_ = true; + } + } + if (visible() && inputDevice_ == input::UiInputDevice::keyboard) { + const auto repeatedKey = menu_navigation_key(keyboardNavigation_.poll(now)); + if (repeatedKey != SDLK_UNKNOWN) { + menu_key(repeatedKey, backend, active, statisticsEnabled); + } + } + for (auto &device : devices_) { + if (device.handle == nullptr) { + continue; + } + const auto instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)); + const bool present = SDL_GameControllerGetAttached(device.handle) != SDL_FALSE; + const bool down = device.chord.update(device.sample.buttonFlags, present); + if (down && !device.chordDown) { + open_menu(instance); + } + device.chordDown = down; + if (active) { + if (present) { + router_.connect(instance); + router_.controller(instance, device.sample); + } else { + router_.disconnect(instance); + } + } + if (!visible() || instance != menu_.owner || inputDevice_ != input::UiInputDevice::gamepad) { + continue; + } + const auto &sample = device.sample; + if (sample.buttonFlags == 0 && sample.leftTrigger == 0 && sample.rightTrigger == 0) { + armed_ = true; + } + if (!armed_) { + continue; + } + if (menu_.page == input::StreamMenuPage::radial) { + // Poll on every pump as well as consuming events: Xbox input can change + // between queued axis events, and a selected item must not freeze polling. + dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; + } else if (menu_.page != input::StreamMenuPage::radial && now - navigationTick_ >= 180) { + const int x = (sample.buttonFlags & RIGHT_FLAG) != 0 || sample.leftStickX > 18000 ? 1 : (sample.buttonFlags & LEFT_FLAG) != 0 || sample.leftStickX < -18000 ? -1 : + 0; + const int y = (sample.buttonFlags & UP_FLAG) != 0 || sample.leftStickY > 18000 ? -1 : (sample.buttonFlags & DOWN_FLAG) != 0 || sample.leftStickY < -18000 ? 1 : + 0; + if (x != 0 || y != 0) { + menu_.navigate(x, y); + navigationTick_ = now; + dirty_ = true; + } + } + } + if (pulseSlot_ >= 0 && (!visible() || static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { + send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); + pulseSlot_ = -1; + } + if (!visible() && (wasVisible || dirty_)) { + keyboardNavigation_.reset(); + router_.set_captured(!focused_); + backend.set_statistics_bitmap(0, 0, {}); + dirty_ = false; + } + backend.local_overlay_size(width, height); + if (visible() && (width != menuWidth_ || height != menuHeight_)) { + dirty_ = true; + menuWidth_ = width; + menuHeight_ = height; + } + if (visible() && dirty_) { + render_menu(backend, font, width, height, statisticsEnabled); + } + if (active) { + router_.tick(now - lastTick_); + } + lastTick_ = now; + return stop; + } + + void StreamControls::release_host_input() { + router_.release_all(); + if (pulseSlot_ >= 0) { + send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); + pulseSlot_ = -1; + } + } + + bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds) { + const int size = std::min(bounds.w, bounds.h); + if (size <= 0) { + return false; + } + const auto key = std::make_pair(std::string(asset), size); + auto found = icons_.find(key); + if (found == icons_.end()) { + const std::string path = ui::build_asset_path(std::string("icons/") + asset); + std::shared_ptr raster(ui::load_svg_surface(path, size, size), SDL_FreeSurface); + if (!raster) { + logging::warn("ui", "Unable to load stream icon " + path + ": " + SDL_GetError()); + } + found = icons_.emplace(key, std::move(raster)).first; + } + if (!found->second) { + return false; + } + const auto &surface = found->second; + std::unique_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); + SDL_Rect destination {bounds.x + (bounds.w - surface->w) / 2, bounds.y + (bounds.h - surface->h) / 2, surface->w, surface->h}; + return texture && SDL_RenderCopy(renderer, texture.get(), nullptr, &destination) == 0; + } + + void StreamControls::render_hint(SDL_Renderer *renderer, TTF_Font *font, const char *asset, const char *key, const char *text, SDL_Rect bounds, bool compact) { + if (bounds.w <= 0 || bounds.h <= 0) { + return; + } + const bool keyboard = inputDevice_ == input::UiInputDevice::keyboard; + const int size = std::min(bounds.h, compact ? std::max(12, TTF_FontLineSkip(font) * 3 / 4) : TTF_FontLineSkip(font) + 12); + int textWidth = 0; + int textHeight = 0; + TTF_SizeUTF8(font, text, &textWidth, &textHeight); + const int capWidth = std::min(bounds.w, keyboard ? std::max(size, static_cast(std::char_traits::length(key)) * size / 2 + 8) : size); + const int groupWidth = std::min(bounds.w, capWidth + 4 + textWidth); + const int left = bounds.x + (bounds.w - groupWidth) / 2; + SDL_Rect icon {left, bounds.y + (bounds.h - size) / 2, capWidth, size}; + if (keyboard) { + SDL_SetRenderDrawColor(renderer, 85, 85, 85, 255); + SDL_RenderDrawRect(renderer, &icon); + label(renderer, font, key, {icon.x + 2, icon.y + 2, icon.w - 4, icon.h - 4}, false, false, true); + } else { + render_icon(renderer, asset, icon); + } + label(renderer, font, text, {left + capWidth + 4, bounds.y, std::max(1, groupWidth - capWidth - 4), bounds.h}, false, false, true); + } + + void StreamControls::render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight) { + const auto &cells = menu_.keyboard_cells(); + constexpr int padding = 8; + const int header = TTF_FontLineSkip(font) + 4; + const auto &focusedCell = cells[menu_.keyIndex]; + std::string description = menu_.keyboardLayout == 0 ? "Keyboard / ABC" : menu_.keyboardLayout == 1 ? "Keyboard / ?123" : + "Keyboard / PC keys"; + if (focusedCell.key >= 106 && focusedCell.key < static_cast(input::StreamMenu::KEYS.size())) { + static const std::array shortcuts {"Copy: Ctrl+C", "Paste: Ctrl+V", "Cut: Ctrl+X", "Undo: Ctrl+Z", "Redo: Ctrl+Y", "Select all: Ctrl+A", "Switch app: Alt+Tab", "Close app: Alt+F4", "Ctrl+Alt+Delete", "Desktop: Win+D", "Explorer: Win+E", "Run: Win+R"}; + description = shortcuts[focusedCell.key - 106]; + } + label(renderer, font, description, {padding, 2, width - padding * 2, header}, false, false, true); + const int gridWidth = width - padding * 2; + const bool keyboardInput = inputDevice_ == input::UiInputDevice::keyboard; + for (int i = 0; i < static_cast(cells.size()); ++i) { + const auto &cell = cells[i]; + const int left = padding + cell.column * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; + const int right = padding + (cell.column + cell.width) * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; + SDL_Rect key {left + 1, padding + header + cell.row * rowHeight + 1, right - left - 3, cell.height * rowHeight - 3}; + const bool modifier = cell.key < input::StreamMenu::KEYBOARD_PAGE_KEY && input::StreamMenu::KEYS[cell.key].code < 0; + const bool latched = cell.key == input::StreamMenu::KEYBOARD_CAPS_KEY ? menu_.caps : cell.key == 40 ? menu_.shiftOnce : + modifier && (menu_.modifiers & -input::StreamMenu::KEYS[cell.key].code) != 0; + const int gray = i == menu_.keyIndex ? 54 : latched ? 65 : + cell.width > 2 ? 42 : + 30; + SDL_SetRenderDrawColor(renderer, gray, gray, gray, 255); + SDL_RenderFillRect(renderer, &key); + std::string text = modifier ? input::StreamMenu::KEYS[cell.key].label : menu_.key_label(i); + + const char *shortcutIcon = nullptr; + const char *shortcutKey = ""; + switch (cell.key) { + case input::StreamMenu::KEYBOARD_PAGE_KEY: + shortcutIcon = "button-lt.svg"; + shortcutKey = "Tab"; + break; + case 47: + shortcutIcon = "button-x.svg"; + text = "Bksp"; + break; + case 57: + shortcutIcon = "button-lb.svg"; + text = "<"; + break; + case 59: + shortcutIcon = "button-rb.svg"; + text = ">"; + break; + case input::StreamMenu::KEYBOARD_CAPS_KEY: + shortcutIcon = "button-left-stick.svg"; + shortcutKey = "Caps"; + break; + case 44: + shortcutIcon = "button-y.svg"; + break; + case 46: + shortcutIcon = "button-start.svg"; + text = "Enter"; + break; + default: + break; + } + if (cell.width > 2 && shortcutIcon != nullptr && (!keyboardInput || *shortcutKey != '\0')) { + if (cell.height > 1) { + label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h / 2}, false, false, true); + render_hint(renderer, font, shortcutIcon, shortcutKey, "", {key.x + 2, key.y + key.h / 2, key.w - 4, key.h / 2 - 2}, true); + } else { + render_hint(renderer, font, shortcutIcon, shortcutKey, text.c_str(), {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, true); + } + } else { + label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, false, false, true); + } + if (i == menu_.keyIndex) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_Rect focus {key.x - 1, key.y - 1, key.w + 2, key.h + 2}; + SDL_RenderDrawRect(renderer, &focus); + focus = {key.x - 2, key.y - 2, key.w + 4, key.h + 4}; + SDL_RenderDrawRect(renderer, &focus); + } + } + const int line = TTF_FontLineSkip(font) + 8; + const int footerY = padding + header + input::StreamMenu::KEYBOARD_ROWS * rowHeight + 4; + const int footerHeight = std::min(line + 4, height - footerY); + const std::array assets {"button-a.svg", "button-b.svg", "button-lt.svg", "button-right-stick.svg", "button-left-stick.svg", "button-rt.svg"}; + const std::array keys {"Enter", "Esc", "Tab", "Shift", "Caps", "F2"}; + const std::array actions {"Select", "Back", "Pages", menu_.shiftOnce ? "Shift: On" : "Shift", menu_.caps ? "Caps: On" : "Caps", keyboardTop_ ? "Move down" : "Move up"}; + for (int i = 0; i < 6; ++i) { + render_hint(renderer, font, assets[i], keys[i], actions[i], {(i % 3) * width / 3 + 4, footerY + (i / 3) * line, width / 3 - 8, footerHeight}); + } + } + + void StreamControls::render_menu(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool stats) { + if (font == nullptr) { + return; + } + const bool keyboard = menu_.page == input::StreamMenuPage::keyboard; + const int rowHeight = std::max(TTF_FontLineSkip(font) + 8, width * 9 / 200); + const int safeBottom = std::max(8, height / 40); + width = keyboard ? width * 9 / 10 : width; + height = keyboard ? std::min(height - VideoOverlay::MARGIN * 2, rowHeight * input::StreamMenu::KEYBOARD_ROWS + 28 + TTF_FontLineSkip(font) + (TTF_FontLineSkip(font) + 8) * 2 + safeBottom) : height; + if (width <= 0 || height <= 0 || font == nullptr) { + return; + } + std::unique_ptr panel(SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_ARGB8888), SDL_FreeSurface); + if (!panel) { + return; + } + std::unique_ptr renderer(SDL_CreateSoftwareRenderer(panel.get()), SDL_DestroyRenderer); + if (!renderer) { + return; + } + const Uint8 background = keyboard ? 16 : 0; + SDL_SetRenderDrawColor(renderer.get(), background, background, background, keyboard ? 255 : 150); + SDL_RenderClear(renderer.get()); + const int line = TTF_FontLineSkip(font) + 8; + if (menu_.page == input::StreamMenuPage::radial) { + label(renderer.get(), font, "Stream controls", {8, 4, width - 16, line}, false, false); + } + if (keyboard) { + render_keyboard(renderer.get(), font, width, height, rowHeight); + } else if (menu_.page == input::StreamMenuPage::radial) { + const int cy = height / 2; + const int radius = std::min(width / 5, (height - line * 4) / 2); + for (int wheel = 0; wheel < 2; ++wheel) { + const int cx = width * (wheel == 0 ? 1 : 3) / 4; + SDL_RenderFlush(renderer.get()); + rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, static_cast(menu_.selected) / 4 == wheel ? static_cast(menu_.selected) % 4 : -1); + render_icon(renderer.get(), wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}); + constexpr std::array dx {0, 1, 0, -1}; + constexpr std::array dy {-1, 0, 1, 0}; + for (int sector = 0; sector < 4; ++sector) { + const auto action = static_cast(wheel * 4 + sector); + const int labelWidth = std::min(line * 6, width / 5); + const int x = std::clamp(cx + dx[sector] * radius * 2 / 3 - labelWidth / 2, 0, width - labelWidth); + const int iconSize = std::max(18, line); + const int groupY = cy + dy[sector] * radius * 2 / 3 - (iconSize + line * 2) / 2; + constexpr std::array icons {"keyboard.svg", "mouse.svg", "scroll-text.svg", "power.svg", "button-guide.svg", "button-share.svg", "camera.svg", "chart-no-axes-combined.svg"}; + render_icon(renderer.get(), icons[wheel * 4 + sector], {x + (labelWidth - iconSize) / 2, groupY, iconSize, iconSize}); + label(renderer.get(), font, input::stream_action_label(action, mouseDevice_ >= 0, stats), {x, groupY + iconSize + 2, labelWidth, line * 2}, false, false); + } + } + const bool keyboardHints = inputDevice_ == input::UiInputDevice::keyboard; + const std::array assets {"button-left-stick.svg", "button-right-stick.svg", "button-a.svg", "button-b.svg"}; + const std::array keys {"Arrows", "Tab", "Enter", "Esc"}; + const std::array actions {keyboardHints ? "Select" : "Left wheel", keyboardHints ? "Next" : "Right wheel", "Choose", "Resume"}; + for (int i = 0; i < 4; ++i) { + render_hint(renderer.get(), font, assets[i], keys[i], actions[i], {i * width / 4 + 4, height - line * 2, width / 4 - 8, line + 4}); + } + } else if (menu_.page == input::StreamMenuPage::logs) { + const SDL_Rect card {width / 8, line / 2, width * 7 / 8 - line / 2, height - line}; + ui::draw_dialog_surface(renderer.get(), card); + label(renderer.get(), font, "Stream logs", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); + const int row = line * 2; + const int count = std::max(1, (card.h - line * 4) / row); + menu_.logOffset = std::min(menu_.logOffset, std::max(0, static_cast(logs_.size()) - count)); + const int end = static_cast(logs_.size()) - menu_.logOffset; + const int start = std::max(0, end - count); + for (int i = start; i < end; ++i) { + const auto &text = logs_[i]; + const SDL_Color color = text.find("[ERROR]") != std::string::npos || text.find("[FATAL]") != std::string::npos ? SDL_Color {255, 125, 125, 255} : text.find("[WARN") != std::string::npos ? SDL_Color {255, 211, 115, 255} : + SDL_Color {224, 232, 241, 255}; + label(renderer.get(), font, text, {card.x + 16, card.y + line * 2 + (i - start) * row, card.w - 42, row}, true, false, false, color); + } + const int trackHeight = count * row; + const int thumbHeight = std::max(8, trackHeight * count / std::max(count, static_cast(logs_.size()))); + SDL_Rect thumb {card.x + card.w - 12, card.y + line * 2 + (trackHeight - thumbHeight) * start / std::max(1, static_cast(logs_.size()) - count), 3, thumbHeight}; + SDL_SetRenderDrawColor(renderer.get(), 130, 180, 255, 255); + SDL_RenderFillRect(renderer.get(), &thumb); + if (logs_.empty()) { + label(renderer.get(), font, "No retained log entries.", {card.x + 16, card.y + line * 2, card.w - 32, line}, false, false); + } + render_hint(renderer.get(), font, "button-left-stick.svg", "Arrows", menu_.logOffset == 0 ? "Live / scroll" : "Scroll", {card.x + 8, card.y + card.h - line - 12, card.w / 2 - 16, line}); + render_hint(renderer.get(), font, "button-b.svg", "Esc", "Back", {card.x + card.w / 2, card.y + card.h - line - 12, card.w / 2 - 16, line}); + } else { + const int cardWidth = std::min(width - 32, std::max(380, width * 3 / 5)); + const int cardHeight = line * 7; + const SDL_Rect card {(width - cardWidth) / 2, (height - cardHeight) / 2, cardWidth, cardHeight}; + ui::draw_dialog_surface(renderer.get(), card); + label(renderer.get(), font, "Disconnect from stream?", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); + label(renderer.get(), font, "The app will keep running on your PC.", {card.x + 20, card.y + 28 + line, card.w - 40, line * 2}, false, false); + for (int i = 0; i < 2; ++i) { + const bool selected = menu_.confirmQuit == (i == 1); + SDL_Rect button {card.x + 20 + i * (card.w - 32) / 2, card.y + card.h - line * 2 - 12, (card.w - 48) / 2, line + 8}; + ui::draw_dialog_round_rect(renderer.get(), button, selected ? SDL_Color {130, 180, 255, 255} : SDL_Color {28, 40, 61, 255}, 6); + label(renderer.get(), font, i == 0 ? "Cancel" : "Disconnect", button, false, false, true, selected ? SDL_Color {13, 18, 29, 255} : SDL_Color {242, 245, 248, 255}); + } + render_hint(renderer.get(), font, "button-a.svg", "Enter", "Select", {card.x, card.y + card.h - line, card.w / 2, line}); + render_hint(renderer.get(), font, "button-b.svg", "Esc", "Back", {card.x + card.w / 2, card.y + card.h - line, card.w / 2, line}); + } + if (!message_.empty() && menu_.page == input::StreamMenuPage::radial) { + label(renderer.get(), font, message_, {4, line + 4, width - 8, line * 2}, false, false); + } + SDL_RenderPresent(renderer.get()); + auto pixels = extract_overlay_bitmap(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, keyboard); + backend.set_color_overlay(width, height, std::move(pixels), keyboard ? (keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom) : VideoOverlayPlacement::full_screen); + dirty_ = false; + } +} // namespace streaming diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h new file mode 100644 index 0000000..907a08c --- /dev/null +++ b/src/streaming/stream_controls.h @@ -0,0 +1,200 @@ +/** + * @file src/streaming/stream_controls.h + * @brief SDL input devices and local controls for an active stream. + */ +#pragma once + +#include "src/input/stream_exit.h" +#include "src/input/stream_input.h" +#include "src/input/stream_menu.h" +#include "src/input/ui_input.h" + +#include +#include +#include +#include +#include +#include +#include + +namespace streaming { + class FfmpegStreamBackend; + + /** + * @brief Owns four gamepads, local stream controls and ordered host input. + */ + class StreamControls { + public: + /** + * @brief Construct an event-thread input router. + */ + StreamControls(); + /** + * @brief Close owned controllers. + */ + ~StreamControls(); + StreamControls(const StreamControls &) = delete; + StreamControls &operator=(const StreamControls &) = delete; + /** + * @brief Adopt a controller and discover other devices. + * @param controller Owned SDL controller reference. + */ + void reset(SDL_GameController *controller); + /** + * @brief Apply persisted local-control preferences for this session. + * @param screenshotDirectory Screenshot folder, or empty for the title-scoped default. + * @param keyboardTop True to dock the keyboard to the top edge. + */ + void configure(std::string screenshotDirectory, bool keyboardTop); + /** + * @brief Read the placement selected while streaming. + * @return True for top docking. + */ + bool keyboard_docked_top() const; + /** + * @brief Close all owned SDL references. + */ + void close(); + /** + * @brief Open a controller in the lowest free local slot. + * @param deviceIndex SDL joystick index. + */ + void open_if_needed(int deviceIndex); + /** + * @brief Remove a controller while preserving all other slot numbers. + * @param instance SDL joystick instance ID. + */ + void close_if_removed(int instance); + /** + * @brief Sample devices for summary dismissal. + * @return True when any controller has the menu chord pressed. + */ + bool poll(); + /** + * @brief Check summary dismissal buttons. + * @return True if any connected pad has a pressed button or trigger. + */ + bool any_button_pressed() const; + /** + * @brief Transfer a connected controller to the shell. + * @return Owned reference, or nullptr. + */ + SDL_GameController *take_controller_for_shell(); + /** + * @brief Describe controller connectivity for diagnostic builds. + * @return One line for each occupied local slot. + */ + std::vector diagnostic_lines() const; + /** + * @brief Route SDL events and rebuild local UI only when it changes. + * @param backend Active video backend receiving the shared GPU/software overlay. + * @param font Font for menu labels and keyboard cells. + * @param width Output width. + * @param height Output height. + * @param active True once the transport is ready for input. + * @param statisticsEnabled Mutable session statistics toggle. + * @return True when the user confirms quit or closes the application window. + */ + bool pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled); + /** + * @brief Check whether a local screen currently owns the video overlay. + * @return True while the radial menu, keyboard, log viewer or confirmation is visible. + */ + bool visible() const; + /** + * @brief Release all held host input before transport shutdown. + */ + void release_host_input(); + + private: + /** + * @brief Independent controller state and shortcut latch. + */ + struct Device { + SDL_GameController *handle = nullptr; ///< Owned SDL reference. + input::StreamExitChord chord; ///< Event-order chord detection. + input::ControllerState sample {}; ///< Latest sampled state. + bool chordDown = false; ///< Chord edge latch. + }; + + /** + * @brief Open the wheels from gameplay or any local page and release host inputs. + * @param device Controller instance ID, or -1 for keyboard control. + */ + void open_menu(int device); + /** + * @brief Draw colored controls with a solid OSK panel and a transparent wheel background. + * @param backend Video backend. + * @param font Menu font. + * @param width Output width. + * @param height Output height. + * @param stats Current statistics toggle. + */ + void render_menu(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool stats); + /** + * @brief Draw the Xbox-style character grid, wide space bar and tall shortcut controls. + * @param renderer Software panel renderer. + * @param font Keyboard font. + * @param width Panel width. + * @param height Panel height, including the helper footer. + * @param rowHeight Height of one character row. + */ + void render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight); + /** + * @brief Draw a cached SVG raster on the current software panel. + * @param renderer Panel renderer. + * @param asset Icon filename under assets/icons. + * @param bounds Destination bounds. + * @return True when the icon was drawn. + */ + bool render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds); + /** + * @brief Draw a controller glyph or keyboard keycap beside its action label. + * @param renderer Panel renderer. + * @param font Label font. + * @param asset Controller SVG filename. + * @param key Keyboard keycap label. + * @param text Action label. + * @param bounds Available hint rectangle. + * @param compact Use smaller glyphs inside keyboard keys, preserving footer sizing. + */ + void render_hint(SDL_Renderer *renderer, TTF_Font *font, const char *asset, const char *key, const char *text, SDL_Rect bounds, bool compact = false); + /** + * @brief Process a local navigation or activation key. + * @param key SDL key code. + * @param backend Video backend for screenshots. + * @param active Transport readiness. + * @param stats Mutable statistics toggle. + * @return True on confirmed quit. + */ + bool menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats); + /** + * @brief Tap a host key with the on-screen keyboard's modifier toggles. + * @param code Virtual key code. + */ + void tap_key(int code); + std::array devices_ {}; ///< Stable local controller slots. + input::StreamInput router_; ///< Ordered event-thread host input. + input::StreamMenu menu_; ///< Local menu state. + input::KeyboardNavigation keyboardNavigation_; ///< Local keyboard repeats, independent of gameplay key forwarding. + input::UiInputDevice inputDevice_ = input::UiInputDevice::gamepad; ///< Device used by local helper text. + bool armed_ = false; ///< Opening buttons have been released. + bool dirty_ = false; ///< Overlay requires rasterization. + bool focused_ = true; ///< Window focus state. + int mouseDevice_ = -1; ///< Virtual mouse controller, or disabled. + Uint32 lastTick_ = 0; ///< Pointer integration clock. + Uint32 navigationTick_ = 0; ///< Stick navigation repeat clock. + int pulseSlot_ = -1; ///< Controller with a pending guide/share release. + Uint32 pulseUntil_ = 0; ///< Guide/share pulse deadline. + std::string message_; ///< Last action result. + std::vector logs_; ///< Snapshot of runtime log lines. + int menuWidth_ = 0; ///< Last video rectangle width used for local controls. + int menuHeight_ = 0; ///< Last video rectangle height used for local controls. + bool keyboardPageTriggerDown_ = false; ///< Edge latch for the left-trigger keyboard layout shortcut. + bool keyboardDockTriggerDown_ = false; ///< Edge latch for right-trigger keyboard docking. + bool keyboardTop_ = false; ///< Dock the keyboard flush with the top edge. + std::string screenshotDirectory_; ///< Configured screenshot folder, empty for the default. + Uint32 logRefreshTick_ = 0; ///< Last live log refresh. + std::map, std::shared_ptr> icons_; ///< Cached SVG rasters survive panel renderer replacement. + }; +} // namespace streaming diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index be8aea3..349f93a 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -1,10 +1,11 @@ /** * @file src/streaming/video_overlay.cpp - * @brief Converts a cached statistics bitmap for GPU scanout and software framebuffers. + * @brief Converts cached UI panels for GPU scanout and software framebuffers. */ #include "src/streaming/video_overlay.h" #include +#include #include #include @@ -19,24 +20,223 @@ namespace { * @param bytes Bytes per pixel. * @return True when every row fits its storage. */ - bool valid_storage(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int bytes) { + bool valid_storage(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int bytes) { return pixels != nullptr && width > 0 && height > 0 && bytes > 0 && pitch > 0 && static_cast(pitch) >= static_cast(width) * bytes && capacity >= static_cast(height - 1) * pitch + static_cast(width) * bytes; } + + /** + * @brief Blend one component with exact transparent and opaque endpoints. + * @param background Existing component. + * @param foreground UI component. + * @param opacity UI coverage in the range 0 through 255. + * @return Rounded composite component. + */ + std::uint8_t blend_byte(unsigned background, unsigned foreground, unsigned opacity) { + return static_cast((foreground * opacity + background * (255 - opacity) + 127) / 255); + } + + /** + * @brief Blend a grayscale pixel into a packed Xbox RGB framebuffer. + * @param pixel Writable destination pixel. + * @param bpp Validated RGB depth: 15, 16 or 32. + * @param foreground Straight foreground ARGB color. + * @param opacity Nonzero UI coverage. + */ + void blend_rgb_pixel(std::uint8_t *pixel, int bpp, std::uint32_t foreground, unsigned opacity) { + std::uint32_t packed = 0; + const int bytes = bpp == 32 ? 4 : 2; + std::memcpy(&packed, pixel, bytes); + unsigned red = 0; + unsigned green = 0; + unsigned blue = 0; + if (bpp == 32) { + red = (packed >> 16) & 255; + green = (packed >> 8) & 255; + blue = packed & 255; + } else { + red = (packed >> (bpp == 16 ? 11 : 10)) & 31; + green = (packed >> 5) & (bpp == 16 ? 63 : 31); + blue = packed & 31; + red = (red << 3) | (red >> 2); + green = bpp == 16 ? (green << 2) | (green >> 4) : (green << 3) | (green >> 2); + blue = (blue << 3) | (blue >> 2); + } + red = blend_byte(red, (foreground >> 16) & 255, opacity); + green = blend_byte(green, (foreground >> 8) & 255, opacity); + blue = blend_byte(blue, foreground & 255, opacity); + if (bpp == 32) { + packed = 0xff000000U | (red << 16) | (green << 8) | blue; + } else if (bpp == 16) { + packed = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3); + } else { + packed = 0x8000U | ((red >> 3) << 10) | ((green >> 3) << 5) | (blue >> 3); + } + std::memcpy(pixel, &packed, bytes); + } + + /** + * @brief Encode an original UI color in the host video's limited-range YUV matrix. + * @param color Straight ARGB source pixel. + * @param matrix Validated BT.601 or BT.709 matrix. + * @return Luma, U and V components. + */ + std::array rgb_to_yuv(std::uint32_t color, streaming::VideoColorMatrix matrix) { + const int red = (color >> 16) & 255; + const int green = (color >> 8) & 255; + const int blue = color & 255; + if (red == green && green == blue) { + return {static_cast(16 + (red * 219 + 127) / 255), 128, 128}; + } + // 16-bit fixed-point RGB-to-limited-YUV coefficients, using the same + // BT.601/709 primaries and 219/224 ranges as FFmpeg's colorspace conversion. + constexpr std::array bt601 {16829, 33039, 6416, -9714, -19071, 28784, 28784, -24103, -4681}; + constexpr std::array bt709 {11966, 40254, 4064, -6596, -22189, 28784, 28784, -26145, -2639}; + const auto &coefficients = matrix == streaming::VideoColorMatrix::bt709 ? bt709 : bt601; + std::array result {}; + for (int component = 0; component < 3; ++component) { + const int offset = component == 0 ? 16 : 128; + const int value = offset * 65536 + coefficients[component * 3] * red + coefficients[component * 3 + 1] * green + coefficients[component * 3 + 2] * blue; + result[component] = std::clamp((value + 32768) / 65536, 0, 255); + } + return result; + } } // namespace namespace streaming { - void VideoOverlay::set_bitmap(int width, int height, std::vector pixels) { - if (width <= 0 || height <= 0 || width > 2048 || height > 2048 || pixels.size() != static_cast(width) * height) { + std::vector extract_overlay_bitmap(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, bool opaqueBackground) { + if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048) { + return {}; + } + std::vector bitmap(static_cast(width) * height * 4); + for (int row = 0; row < height; ++row) { + for (int column = 0; column < width; ++column) { + const auto *pixel = pixels + static_cast(row) * pitch + column * 4; + const unsigned opacity = pixel[3]; + const auto index = (static_cast(row) * width + column) * 4; + bitmap[index + 3] = opacity; + if (opaqueBackground || opacity == 255) { + std::memcpy(bitmap.data() + index, pixel, 4); + // Legacy SDL blitters can round an opaque destination alpha to 254. + // The OSK is already composited onto its solid panel; keep it opaque. + bitmap[index + 3] = 255; + continue; + } + if (opacity == 0) { + continue; + } + for (int component = 0; component < 3; ++component) { + // SDL composites onto transparent black using premultiplied color. + // Undo that once, preserving each original SVG color for every path. + bitmap[index + component] = std::min(255U, (pixel[component] * 255 + opacity / 2) / opacity); + } + } + } + return bitmap; + } + + bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected) { + if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048 || centerX < 0 || centerX >= width || centerY < 0 || centerY >= height || radius < 1 || radius > 2048) { + return false; + } + for (int row = std::max(0, centerY - radius); row <= std::min(height - 1, centerY + radius); ++row) { + const int y = row - centerY; + for (int column = std::max(0, centerX - radius); column <= std::min(width - 1, centerX + radius); ++column) { + const int x = column - centerX; + const int distance = x * x + y * y; + if (distance > radius * radius || distance < radius * radius / 9) { + continue; + } + const int sector = std::abs(y) >= std::abs(x) ? (y < 0 ? 0 : 2) : (x > 0 ? 1 : 3); + const std::uint32_t gray = std::abs(std::abs(x) - std::abs(y)) < 2 ? 110 : sector == selected ? 85 : + 36; + const std::uint32_t color = 0xff000000U | gray * 0x010101U; + std::memcpy(pixels + static_cast(row) * pitch + column * 4, &color, 4); + } + } + return true; + } + + OverlayLabelLayout layout_overlay_label(int width, int height, const VideoRectangle &bounds, bool shrinkToFit) { + if (width <= 0 || height <= 0 || bounds.width <= 0 || bounds.height <= 0) { + return {}; + } + OverlayLabelLayout layout {{0, 0, width, height}, bounds}; + if (shrinkToFit) { + const double scale = std::min({1.0, static_cast(std::max(1, bounds.width - 2)) / width, static_cast(std::max(1, bounds.height - 2)) / height}); + layout.destination.width = std::max(1, static_cast(width * scale)); + layout.destination.height = std::max(1, static_cast(height * scale)); + } else { + layout.source = {std::max(0, (width - bounds.width) / 2), std::max(0, (height - bounds.height) / 2), std::min(width, bounds.width), std::min(height, bounds.height)}; + layout.destination.width = layout.source.width; + layout.destination.height = layout.source.height; + } + layout.destination.x += (bounds.width - layout.destination.width) / 2; + layout.destination.y += (bounds.height - layout.destination.height) / 2; + return layout; + } + + void VideoOverlay::set_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement, std::vector opacity) { + if (width <= 0 || height <= 0 || width > 2048 || height > 2048 || pixels.size() != static_cast(width) * height || (!opacity.empty() && opacity.size() != pixels.size())) { width = height = 0; pixels.clear(); + opacity.clear(); } width_ = width; height_ = height; + placement_ = placement; grayscale_ = std::move(pixels); + opacity_ = std::move(opacity); + std::vector().swap(argb_); + opaque_ = opacity_.empty() || std::all_of(opacity_.begin(), opacity_.end(), [](std::uint8_t opacity) { + return opacity == 255; + }); + invalidate_caches(); + } + + void VideoOverlay::set_argb_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement) { + if (width <= 0 || height <= 0 || width > 2048 || height > 2048 || pixels.size() != static_cast(width) * height * 4) { + set_bitmap(0, 0, {}); + return; + } + width_ = width; + height_ = height; + placement_ = placement; + argb_ = std::move(pixels); + std::vector().swap(grayscale_); + std::vector().swap(opacity_); + opaque_ = true; + for (std::size_t index = 3; index < argb_.size(); index += 4) { + if (argb_[index] != 255) { + opaque_ = false; + break; + } + } + invalidate_caches(); + } + + std::uint32_t VideoOverlay::color_at(std::size_t index) const { + if (!argb_.empty()) { + std::uint32_t color = 0; + std::memcpy(&color, argb_.data() + index * 4, 4); + return color; + } + const std::uint32_t opacity = opacity_.empty() ? 255 : opacity_[index]; + return (opacity << 24) | grayscale_[index] * 0x010101U; + } + + void VideoOverlay::invalidate_caches() { rgb_.clear(); rgbBpp_ = 0; yuy2_.clear(); + yuy2Opacity_.clear(); + if (width_ == 0) { + // Local menus must return their converted buffers to the 64 MiB console + // budget when dismissed, rather than retaining full-screen capacities. + std::vector().swap(rgb_); + std::vector().swap(yuy2_); + std::vector().swap(yuy2Opacity_); + } frameWidth_ = 0; } @@ -48,32 +248,51 @@ namespace streaming { return height_; } + VideoRectangle VideoOverlay::rectangle(const VideoRectangle &destination) const { + const bool docked = placement_ == VideoOverlayPlacement::dock_bottom || placement_ == VideoOverlayPlacement::dock_top; + const bool bottom = placement_ == VideoOverlayPlacement::bottom_center || placement_ == VideoOverlayPlacement::dock_bottom; + const bool centered = bottom || docked; + const int margin = docked || placement_ == VideoOverlayPlacement::full_screen ? 0 : MARGIN; + const int width = std::min(width_, std::max(0, destination.width - margin * (centered ? 2 : 1))); + const int height = std::min(height_, std::max(0, destination.height - margin * (centered ? 2 : 1))); + if (width == 0 || height == 0) { + return {}; + } + return {destination.x + (centered ? (destination.width - width) / 2 : margin), destination.y + (bottom ? destination.height - height - margin : margin), width, height}; + } + void VideoOverlay::prepare_rgb(int bpp) { - if (rgbBpp_ == bpp) { + if (rgbBpp_ == bpp || (bpp == 32 && !argb_.empty())) { return; } const int bytes = bpp == 32 ? 4 : 2; - rgb_.resize(grayscale_.size() * bytes); - for (std::size_t i = 0; i < grayscale_.size(); ++i) { - const std::uint32_t gray = grayscale_[i]; + const auto count = static_cast(width_) * height_; + rgb_.resize(count * bytes); + for (std::size_t i = 0; i < count; ++i) { + const auto color = color_at(i); if (bpp == 32) { - const std::uint32_t color = 0xff000000U | gray * 0x010101U; std::memcpy(rgb_.data() + i * 4, &color, 4); } else { - const auto color = static_cast(bpp == 16 ? ((gray >> 3U) << 11U) | ((gray >> 2U) << 5U) | (gray >> 3U) : 0x8000U | ((gray >> 3U) << 10U) | ((gray >> 3U) << 5U) | (gray >> 3U)); - std::memcpy(rgb_.data() + i * 2, &color, 2); + const auto red = (color >> 16) & 255; + const auto green = (color >> 8) & 255; + const auto blue = color & 255; + const auto packed = static_cast(bpp == 16 ? ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3) : 0x8000U | ((red >> 3) << 10) | ((green >> 3) << 5) | (blue >> 3)); + std::memcpy(rgb_.data() + i * 2, &packed, 2); } } rgbBpp_ = bpp; } const std::vector &VideoOverlay::argb_pixels() { + if (!argb_.empty()) { + return argb_; + } prepare_rgb(32); return rgb_; } bool VideoOverlay::draw_rgb(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int bpp, const VideoRectangle &destination) { - if (grayscale_.empty()) { + if (width_ == 0) { return true; } const int bytes = bpp == 32 ? 4 : (bpp == 15 || bpp == 16 ? 2 : 0); @@ -81,55 +300,120 @@ namespace streaming { return false; } prepare_rgb(bpp); - const int copyWidth = std::min(width_, destination.width - MARGIN); - const int copyHeight = std::min(height_, destination.height - MARGIN); - const std::size_t columnBytes = (static_cast(destination.x) + MARGIN) * bytes; - const std::size_t copyBytes = static_cast(copyWidth) * bytes; - for (int row = 0; row < copyHeight; ++row) { - const std::size_t framebufferRow = static_cast(destination.y) + MARGIN + row; - std::memcpy(pixels + framebufferRow * pitch + columnBytes, rgb_.data() + static_cast(row) * width_ * bytes, copyBytes); + const auto &converted = bpp == 32 && !argb_.empty() ? argb_ : rgb_; + const auto bounds = rectangle(destination); + const std::size_t columnBytes = static_cast(bounds.x) * bytes; + const std::size_t copyBytes = static_cast(bounds.width) * bytes; + for (int row = 0; row < bounds.height; ++row) { + const std::size_t framebufferRow = static_cast(bounds.y) + row; + auto *destinationRow = pixels + framebufferRow * pitch + columnBytes; + const auto bitmapRow = static_cast(row) * width_; + if (opaque_) { + std::memcpy(destinationRow, converted.data() + bitmapRow * bytes, copyBytes); + } else { + for (int column = 0; column < bounds.width; ++column) { + const auto index = bitmapRow + column; + const auto color = color_at(index); + const auto opacity = color >> 24; + if (opacity == 255) { + std::memcpy(destinationRow + column * bytes, converted.data() + index * bytes, bytes); + } else if (opacity != 0) { + blend_rgb_pixel(destinationRow + column * bytes, bpp, color, opacity); + } + } + } } return true; } - bool VideoOverlay::draw_yuy2(std::uint8_t *pixels, std::size_t capacity, int pitch, int frameWidth, int frameHeight, const VideoRectangle &destination) { - if (grayscale_.empty()) { + bool VideoOverlay::draw_yuy2(std::uint8_t *pixels, std::size_t capacity, int pitch, int frameWidth, int frameHeight, const VideoRectangle &destination, VideoColorMatrix matrix) { + if (width_ == 0) { return true; } - if (!valid_storage(pixels, capacity, pitch, frameWidth, frameHeight, 2) || (frameWidth & 1) != 0 || frameWidth > 2046 || frameHeight > 2047 || destination.width <= MARGIN || destination.height <= MARGIN) { + if (!valid_storage(pixels, capacity, pitch, frameWidth, frameHeight, 2) || (frameWidth & 1) != 0 || frameWidth > 2046 || frameHeight > 2047 || destination.width <= MARGIN || destination.height <= MARGIN || matrix == VideoColorMatrix::unsupported) { return false; } - if (frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height) { + if (frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height || matrix_ != matrix) { + matrix_ = matrix; frameWidth_ = frameWidth; frameHeight_ = frameHeight; outputWidth_ = destination.width; outputHeight_ = destination.height; - source_.x = ((MARGIN * frameWidth / destination.width) + 1) & ~1; - source_.y = MARGIN * frameHeight / destination.height; - const int visibleWidth = std::min(width_, destination.width - MARGIN); - const int visibleHeight = std::min(height_, destination.height - MARGIN); + const auto bounds = rectangle({0, 0, destination.width, destination.height}); + if (bounds.width == 0 || bounds.height == 0) { + source_ = {}; + return true; + } + source_.x = (static_cast(static_cast(bounds.x) * frameWidth / destination.width) + 1) & ~1; + source_.y = static_cast(static_cast(bounds.y) * frameHeight / destination.height); + const int visibleWidth = bounds.width; + const int visibleHeight = bounds.height; source_.width = std::min(frameWidth - source_.x, ((visibleWidth * frameWidth + destination.width - 1) / destination.width + 1) & ~1); source_.height = std::min(frameHeight - source_.y, (visibleHeight * frameHeight + destination.height - 1) / destination.height); if (source_.width <= 0 || source_.height <= 0) { return true; } yuy2_.resize(static_cast(source_.width) * source_.height * 2); + if (!opaque_) { + yuy2Opacity_.resize(yuy2_.size() / 2); + } for (int row = 0; row < source_.height; ++row) { const int bitmapRow = std::min(visibleHeight - 1, row * visibleHeight / source_.height); - for (int column = 0; column < source_.width; ++column) { - const int bitmapColumn = std::min(visibleWidth - 1, column * visibleWidth / source_.width); - const auto gray = grayscale_[bitmapRow * width_ + bitmapColumn]; - const std::size_t offset = (static_cast(row) * source_.width + column) * 2; - yuy2_[offset] = static_cast(16 + (gray * 219 + 127) / 255); - yuy2_[offset + 1] = 128; + for (int column = 0; column < source_.width; column += 2) { + const int firstColumn = std::min(visibleWidth - 1, column * visibleWidth / source_.width); + const int secondColumn = std::min(visibleWidth - 1, (column + 1) * visibleWidth / source_.width); + const auto firstColor = color_at(static_cast(bitmapRow) * width_ + firstColumn); + const auto secondColor = color_at(static_cast(bitmapRow) * width_ + secondColumn); + const auto first = rgb_to_yuv(firstColor, matrix); + const auto second = rgb_to_yuv(secondColor, matrix); + const unsigned firstAlpha = firstColor >> 24; + const unsigned secondAlpha = secondColor >> 24; + const unsigned coverage = firstAlpha + secondAlpha; + const auto offset = (static_cast(row) * source_.width + column) * 2; + yuy2_[offset] = first[0]; + yuy2_[offset + 2] = second[0]; + // Weight shared chroma by coverage so a transparent colored pixel + // cannot tint its opaque neighbor or desaturate the host underneath. + yuy2_[offset + 1] = coverage == 0 ? 128 : (first[1] * firstAlpha + second[1] * secondAlpha + coverage / 2) / coverage; + yuy2_[offset + 3] = coverage == 0 ? 128 : (first[2] * firstAlpha + second[2] * secondAlpha + coverage / 2) / coverage; + if (!opaque_) { + yuy2Opacity_[offset / 2] = firstAlpha; + yuy2Opacity_[offset / 2 + 1] = secondAlpha; + } } } } + if (source_.width <= 0 || source_.height <= 0) { return true; } for (int row = 0; row < source_.height; ++row) { - std::memcpy(pixels + static_cast(source_.y + row) * pitch + source_.x * 2, yuy2_.data() + static_cast(row) * source_.width * 2, source_.width * 2); + auto *destinationRow = pixels + static_cast(source_.y + row) * pitch + source_.x * 2; + const auto rowOffset = static_cast(row) * source_.width; + const auto *foreground = yuy2_.data() + rowOffset * 2; + if (opaque_) { + std::memcpy(destinationRow, foreground, source_.width * 2); + } else { + for (int column = 0; column < source_.width; column += 2) { + const unsigned left = yuy2Opacity_[rowOffset + column]; + const unsigned right = yuy2Opacity_[rowOffset + column + 1]; + if (left == 0 && right == 0) { + continue; + } + auto *pair = destinationRow + column * 2; + if (left == 255 && right == 255) { + std::memcpy(pair, foreground + column * 2, 4); + continue; + } + pair[0] = blend_byte(pair[0], foreground[column * 2], left); + pair[2] = blend_byte(pair[2], foreground[column * 2 + 2], right); + // A YUY2 pair shares chroma: average its two luma coverages rather + // than destroying the host color beside a transparent UI pixel. + const unsigned chroma = (left + right + 1) / 2; + pair[1] = blend_byte(pair[1], foreground[column * 2 + 1], chroma); + pair[3] = blend_byte(pair[3], foreground[column * 2 + 3], chroma); + } + } } return true; } diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index 596bc3f..5823347 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -1,6 +1,6 @@ /** * @file src/streaming/video_overlay.h - * @brief Caches one grayscale statistics panel for both YUY2 and RGB presentation. + * @brief Caches statistics, menus and docked keyboards for both YUY2 and RGB presentation. */ #pragma once @@ -8,17 +8,96 @@ namespace streaming { /** - * @brief Opaque grayscale panel anchored inside the top-left of the video rectangle. + * @brief Extract straight ARGB colors from a premultiplied Xbox surface without discarding SVG colors. + * @param pixels Source bytes in little-endian B, G, R, A order. + * @param capacity Accessible source bytes. + * @param pitch Source row stride. + * @param width Surface width, at most 2048. + * @param height Surface height, at most 2048. + * @param opaqueBackground Source was drawn onto an opaque panel; preserve colors and correct SDL's alpha rounding. + * @return Unpremultiplied B, G, R, A bytes, or empty storage for invalid dimensions. + */ + std::vector extract_overlay_bitmap(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, bool opaqueBackground = false); + + /** + * @brief Source crop and destination for a centered overlay label. + */ + struct OverlayLabelLayout { + VideoRectangle source; ///< Visible portion of the text surface. + VideoRectangle destination; ///< Bounded output rectangle. + }; + + /** + * @brief Fit or crop centered text without changing SDL renderer clipping state. + * + * Standalone clip commands can crash nxdk's SDL 2.0.9 software renderer after + * a texture flush. An explicit source crop also works on current native SDL. + * + * @param width Text surface width. + * @param height Text surface height. + * @param bounds Available label rectangle. + * @param shrinkToFit Shrink a single line with a one-pixel inset instead of cropping it. + * @return Source and destination rectangles, empty for nonpositive dimensions. + */ + OverlayLabelLayout layout_overlay_label(int width, int height, const VideoRectangle &bounds, bool shrinkToFit); + + /** + * @brief Rasterize a grayscale selection wheel directly into an ARGB panel. + * + * Avoid thousands of immediately flushed software-renderer commands per highlight. + * + * @param pixels ARGB8888 destination storage. + * @param capacity Accessible bytes. + * @param pitch Destination row stride. + * @param width Panel width, at most 2048. + * @param height Panel height, at most 2048. + * @param centerX Wheel center X inside the panel. + * @param centerY Wheel center Y inside the panel. + * @param radius Outer radius in pixels, between 1 and 2048. + * @param selected Selected clockwise sector starting at up, or -1 for none. + * @return False when storage or geometry is invalid, without writing pixels. + */ + bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected); + + /** + * @brief Placement of a shared panel inside the presented video rectangle. + */ + enum class VideoOverlayPlacement { + top_left, ///< Statistics and full-menu placement. + bottom_center, ///< Centered bottom panel with the legacy safe-area inset. + dock_bottom, ///< Centered keyboard flush with the bottom edge. + dock_top, ///< Centered keyboard flush with the top edge. + full_screen, ///< Full-stream overlay without an inset. + }; + + /** + * @brief Colored UI or grayscale statistics with a shared anchor for every presentation path. */ class VideoOverlay { public: /** - * @brief Replace the panel and invalidate its converted caches. + * @brief Replace the panel, invalidate caches, and release converted storage when cleared. * @param width Bitmap width. * @param height Bitmap height. * @param pixels Packed grayscale bytes; invalid storage clears the panel. + * @param placement Anchor inside the video rectangle. + * @param opacity Optional per-pixel opacity; empty means fully opaque. Invalid storage clears the panel. + */ + void set_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left, std::vector opacity = {}); + /** + * @brief Replace the panel with straight ARGB pixels and their original SVG colors. + * @param width Bitmap width. + * @param height Bitmap height. + * @param pixels Packed B, G, R, A bytes; invalid storage clears the panel. + * @param placement Anchor inside the video rectangle. */ - void set_bitmap(int width, int height, std::vector pixels); + void set_argb_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left); + /** + * @brief Resolve clipped panel bounds in output pixels for every presentation path. + * @param destination Presented video rectangle. + * @return Anchored panel rectangle, empty when the panel or available area is empty. + */ + VideoRectangle rectangle(const VideoRectangle &destination) const; /** * @brief Get the panel width. * @@ -32,22 +111,25 @@ namespace streaming { */ int height() const; /** - * @brief Get cached opaque ARGB pixels for SDL. + * @brief Get cached straight ARGB pixels for SDL alpha blending. * * @return Packed pixels. */ const std::vector &argb_pixels(); /** - * @brief Composite the panel into a validated packed YUY2 frame before GPU submission. + * @brief Alpha-composite the panel into a validated packed YUY2 frame before GPU submission. + * + * Luma uses per-pixel coverage; shared chroma uses the mean coverage of each pixel pair. * @param pixels Destination frame. * @param capacity Available bytes. * @param pitch Row stride in bytes. * @param frameWidth Frame width. * @param frameHeight Frame height. * @param destination Output video rectangle controlling panel scale. + * @param matrix Host video's color matrix used to encode UI colors. * @return True when the panel was copied or is empty. */ - bool draw_yuy2(std::uint8_t *pixels, std::size_t capacity, int pitch, int frameWidth, int frameHeight, const VideoRectangle &destination); + bool draw_yuy2(std::uint8_t *pixels, std::size_t capacity, int pitch, int frameWidth, int frameHeight, const VideoRectangle &destination, VideoColorMatrix matrix = VideoColorMatrix::bt601); /** * @brief Composite the same panel into the final software framebuffer. * @@ -68,21 +150,37 @@ namespace streaming { private: /** - * @brief Convert the grayscale bitmap only when its pixel depth changes. + * @brief Resolve either source format to one straight ARGB pixel. + * @param index Packed source pixel index. + * @return ARGB color with its coverage in the high byte. + */ + std::uint32_t color_at(std::size_t index) const; + /** + * @brief Invalidate converted colors and release their storage when the panel is empty. + */ + void invalidate_caches(); + /** + * @brief Convert source colors only when the software framebuffer's pixel depth changes. * * @param bpp RGB pixel depth. */ void prepare_rgb(int bpp); int width_ = 0; ///< Packed panel width. int height_ = 0; ///< Packed panel height. - std::vector grayscale_; ///< Font raster and opaque background. + VideoOverlayPlacement placement_ = VideoOverlayPlacement::top_left; ///< Panel anchor shared by SDL, RGB and YUY2 paths. + std::vector grayscale_; ///< Unpremultiplied font and UI colors. + std::vector opacity_; ///< Optional source coverage; empty for legacy opaque statistics. + std::vector argb_; ///< Original straight ARGB UI colors; empty for grayscale statistics. + bool opaque_ = true; ///< Fully opaque panels use row-copy presentation paths. std::vector rgb_; ///< Cached RGB panel. int rgbBpp_ = 0; ///< Pixel format of the RGB cache. std::vector yuy2_; ///< Cached source-resolution panel for scanout scaling. + std::vector yuy2Opacity_; ///< Coverage scaled with the packed GPU panel. VideoRectangle source_ {}; ///< Panel bounds in source luma pixels. int frameWidth_ = 0; ///< Source width used by the YUY2 cache. int frameHeight_ = 0; ///< Source height used by the YUY2 cache. int outputWidth_ = 0; ///< Output width used by the YUY2 cache. int outputHeight_ = 0; ///< Output height used by the YUY2 cache. + VideoColorMatrix matrix_ = VideoColorMatrix::bt601; ///< Host matrix used to encode cached UI colors. }; } // namespace streaming diff --git a/src/ui/asset_path.cpp b/src/ui/asset_path.cpp new file mode 100644 index 0000000..b50fcaa --- /dev/null +++ b/src/ui/asset_path.cpp @@ -0,0 +1,18 @@ +/** + * @file src/ui/asset_path.cpp + * @brief Resolves bundled asset paths consistently across UI screens. + */ +#include "src/ui/asset_path.h" + +#include "src/os.h" + +#include + +namespace ui { + std::string build_asset_path(const std::string &relativePath) { + std::string path = std::string(DATA_PATH) + "assets" + PATH_SEP + relativePath; + // Xbox SDL rejects forward slashes even though native SDL accepts them. + std::replace(path.begin(), path.end(), '/', '\\'); + return path; + } +} // namespace ui diff --git a/src/ui/asset_path.h b/src/ui/asset_path.h new file mode 100644 index 0000000..0216916 --- /dev/null +++ b/src/ui/asset_path.h @@ -0,0 +1,16 @@ +/** + * @file src/ui/asset_path.h + * @brief Shared Xbox asset paths for shell and streaming UI. + */ +#pragma once + +#include + +namespace ui { + /** + * @brief Resolve a bundled asset with the separators required by Xbox SDL file I/O. + * @param relativePath Path inside the assets directory, using either separator. + * @return Absolute Xbox path using backslashes throughout. + */ + std::string build_asset_path(const std::string &relativePath); +} // namespace ui diff --git a/src/ui/dialog_surface.h b/src/ui/dialog_surface.h new file mode 100644 index 0000000..a2c6238 --- /dev/null +++ b/src/ui/dialog_surface.h @@ -0,0 +1,48 @@ +/** + * @file src/ui/dialog_surface.h + * @brief Shared dialog surface for shell and in-stream modals on both SDL render paths. + */ +#pragma once + +#include +#include +#include + +namespace ui { + /** + * @brief Paint rounded horizontal spans without renderer clipping or GPU-specific primitives. + * @param renderer Active SDL renderer. + * @param rect Surface bounds. + * @param color Surface color. + * @param radius Corner radius. + */ + inline void draw_dialog_round_rect(SDL_Renderer *renderer, SDL_Rect rect, SDL_Color color, int radius) { + radius = std::max(0, std::min(radius, std::min(rect.w, rect.h) / 2)); + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); + SDL_Rect middle {rect.x, rect.y + radius, rect.w, rect.h - radius * 2}; + SDL_RenderFillRect(renderer, &middle); + for (int y = 0; y < radius; ++y) { + const int dy = radius - y - 1; + const int inset = radius - static_cast(std::sqrt(radius * radius - dy * dy)); + SDL_Rect row {rect.x + inset, rect.y + y, rect.w - inset * 2, 1}; + SDL_RenderFillRect(renderer, &row); + row.y = rect.y + rect.h - y - 1; + SDL_RenderFillRect(renderer, &row); + } + } + + /** + * @brief Draw the shared opaque navy dialog, shadow and blue accent. + * @param renderer Active SDL renderer. + * @param rect Dialog bounds. + */ + inline void draw_dialog_surface(SDL_Renderer *renderer, const SDL_Rect &rect) { + SDL_BlendMode previousBlend = SDL_BLENDMODE_NONE; + SDL_GetRenderDrawBlendMode(renderer, &previousBlend); + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + draw_dialog_round_rect(renderer, {rect.x + 5, rect.y + 7, rect.w, rect.h}, {0, 0, 0, 0x70}, 12); + draw_dialog_round_rect(renderer, rect, {0x15, 0x1E, 0x2E, 255}, 12); + draw_dialog_round_rect(renderer, {rect.x + 20, rect.y + 16, 36, 3}, {0x82, 0xB4, 0xFF, 255}, 1); + SDL_SetRenderDrawBlendMode(renderer, previousBlend); + } +} // namespace ui diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index eae1fc5..65277d0 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -20,8 +20,6 @@ // nxdk includes #include -#include -#include #include #include #include @@ -44,9 +42,12 @@ #include "src/startup/host_storage.h" #include "src/startup/saved_files.h" #include "src/streaming/session.h" +#include "src/ui/asset_path.h" +#include "src/ui/dialog_surface.h" #include "src/ui/host_probe_result_queue.h" #include "src/ui/shell_layout.h" #include "src/ui/shell_view.h" +#include "src/ui/svg_assets.h" namespace { @@ -78,17 +79,15 @@ namespace { constexpr Sint16 LEFT_STICK_NAVIGATION_THRESHOLD = 16000; constexpr Sint16 LEFT_STICK_NAVIGATION_RELEASE_THRESHOLD = 12000; constexpr Uint32 EXIT_COMBO_HOLD_MILLISECONDS = 900U; - constexpr Uint32 CONTROLLER_NAVIGATION_INITIAL_REPEAT_MILLISECONDS = 150U; - constexpr Uint32 CONTROLLER_NAVIGATION_REPEAT_MILLISECONDS = 45U; + constexpr Uint32 CONTROLLER_NAVIGATION_INITIAL_REPEAT_MILLISECONDS = input::NAVIGATION_INITIAL_REPEAT_MILLISECONDS; + constexpr Uint32 CONTROLLER_NAVIGATION_REPEAT_MILLISECONDS = input::NAVIGATION_REPEAT_MILLISECONDS; constexpr Uint32 LOG_VIEWER_SCROLL_REPEAT_MILLISECONDS = 110U; constexpr Uint32 LOG_VIEWER_FAST_SCROLL_REPEAT_MILLISECONDS = 45U; constexpr int SHELL_EVENT_WAIT_TIMEOUT_MILLISECONDS = 2; constexpr std::size_t LOG_VIEWER_MAX_LOADED_LINES = 512U; constexpr std::size_t LOG_VIEWER_MAX_RENDER_CHARACTERS = 320U; - std::string build_asset_path(const char *relativePath) { - return std::string(DATA_PATH) + "assets" + PATH_SEP + relativePath; - } + using ui::build_asset_path; bool asset_path_uses_svg(const char *relativePath) { if (relativePath == nullptr) { @@ -125,42 +124,10 @@ namespace { } /** - * @brief Calculate the largest integer size that fits inside a destination rectangle. - * - * @param sourceWidth Source asset width. - * @param sourceHeight Source asset height. - * @param maxWidth Maximum destination width. - * @param maxHeight Maximum destination height. - * @param fittedWidth Receives the fitted width. - * @param fittedHeight Receives the fitted height. + * @brief Normalize a loaded asset to the shell's blended ARGB texture format. + * @param surface Owned source surface, freed during normalization. + * @return Owned normalized surface, or nullptr on failure. */ - void calculate_fitted_dimensions(double sourceWidth, double sourceHeight, int maxWidth, int maxHeight, int *fittedWidth, int *fittedHeight) { - if (fittedWidth != nullptr) { - *fittedWidth = 0; - } - if (fittedHeight != nullptr) { - *fittedHeight = 0; - } - if (sourceWidth <= 0.0 || sourceHeight <= 0.0 || maxWidth <= 0 || maxHeight <= 0) { - return; - } - - int targetWidth = maxWidth; - int targetHeight = maxHeight; - if ((sourceWidth * static_cast(maxHeight)) > (sourceHeight * static_cast(maxWidth))) { - targetHeight = std::max(1, static_cast((sourceHeight * static_cast(maxWidth)) / sourceWidth)); - } else { - targetWidth = std::max(1, static_cast((sourceWidth * static_cast(maxHeight)) / sourceHeight)); - } - - if (fittedWidth != nullptr) { - *fittedWidth = targetWidth; - } - if (fittedHeight != nullptr) { - *fittedHeight = targetHeight; - } - } - SDL_Surface *normalize_asset_surface(SDL_Surface *surface) { if (surface == nullptr) { return nullptr; @@ -187,94 +154,6 @@ namespace { SDL_Texture *load_texture_from_asset(SDL_Renderer *renderer, const char *relativePath, int maxWidth, int maxHeight); - /** - * @brief Load and rasterize an SVG asset so it matches the requested destination bounds. - * - * @param relativePath Asset path relative to `DATA_PATH/assets`. - * @param maxWidth Maximum raster width in pixels. - * @param maxHeight Maximum raster height in pixels. - * @return Rasterized 32-bit RGBA surface sized to fit within the destination bounds. - */ - SDL_Surface *load_svg_surface_from_asset(const char *relativePath, int maxWidth, int maxHeight) { - if (!asset_path_uses_svg(relativePath) || maxWidth <= 0 || maxHeight <= 0) { - return nullptr; - } - - const std::string assetPath = build_asset_path(relativePath); - SDL_RWops *rw = SDL_RWFromFile(assetPath.c_str(), "rb"); - if (rw == nullptr) { - return nullptr; - } - - size_t dataSize = 0U; - auto *data = static_cast(SDL_LoadFile_RW(rw, &dataSize, 1)); - if (data == nullptr) { - return nullptr; - } - - NSVGimage *image = nsvgParse(data, "px", 96.0f); - SDL_free(data); - if (image == nullptr) { - return nullptr; - } - - int targetWidth = 0; - int targetHeight = 0; - calculate_fitted_dimensions(image->width, image->height, maxWidth, maxHeight, &targetWidth, &targetHeight); - if (targetWidth <= 0 || targetHeight <= 0) { - nsvgDelete(image); - return nullptr; - } - - NSVGrasterizer *rasterizer = nsvgCreateRasterizer(); - if (rasterizer == nullptr) { - nsvgDelete(image); - return nullptr; - } - - SDL_Surface *surface = SDL_CreateRGBSurface( - SDL_SWSURFACE, - targetWidth, - targetHeight, - 32, - 0x000000FF, - 0x0000FF00, - 0x00FF0000, - 0xFF000000 - ); - if (surface == nullptr) { - nsvgDeleteRasterizer(rasterizer); - nsvgDelete(image); - return nullptr; - } - - const bool mustLockSurface = SDL_MUSTLOCK(surface) != 0; - if (mustLockSurface && SDL_LockSurface(surface) != 0) { - SDL_FreeSurface(surface); - nsvgDeleteRasterizer(rasterizer); - nsvgDelete(image); - return nullptr; - } - - SDL_memset(surface->pixels, 0, static_cast(surface->pitch) * static_cast(surface->h)); - const float rasterScale = std::min(static_cast(targetWidth) / image->width, static_cast(targetHeight) / image->height); - nsvgRasterize(rasterizer, image, 0.0f, 0.0f, rasterScale, static_cast(surface->pixels), surface->w, surface->h, surface->pitch); - - if (mustLockSurface) { - SDL_UnlockSurface(surface); - } - - nsvgDeleteRasterizer(rasterizer); - nsvgDelete(image); - - if (SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND) != 0) { - SDL_FreeSurface(surface); - return nullptr; - } - - return surface; - } - SDL_Texture *create_texture_from_surface_with_scale_quality(SDL_Renderer *renderer, SDL_Surface *surface, const char *scaleQualityHint) { if (renderer == nullptr || surface == nullptr || scaleQualityHint == nullptr) { return nullptr; @@ -367,6 +246,16 @@ namespace { std::vector buttonLabelTextures; ///< Cached rendered keypad button labels. }; + /** + * @brief Measure wrapped input helper rows before laying out a modal. + * @param font Footer font. + * @param actions Input helper actions. + * @param width Footer width. + * @param compact Use compact spacing. + * @return Required footer height. + */ + int measure_footer_actions_height(TTF_Font *font, const std::vector &actions, int width, bool compact = false); + bool render_footer_actions( SDL_Renderer *renderer, TTF_Font *font, @@ -635,7 +524,7 @@ namespace { return nullptr; } - SDL_Surface *surface = (maxWidth > 0 && maxHeight > 0 && asset_path_uses_svg(relativePath)) ? load_svg_surface_from_asset(relativePath, maxWidth, maxHeight) : nullptr; + SDL_Surface *surface = (maxWidth > 0 && maxHeight > 0 && asset_path_uses_svg(relativePath)) ? ui::load_svg_surface(build_asset_path(relativePath), maxWidth, maxHeight) : nullptr; if (surface == nullptr) { const std::string assetPath = build_asset_path(relativePath); surface = IMG_Load(assetPath.c_str()); @@ -777,9 +666,7 @@ namespace { * @param rect Dialog bounds. */ void render_dialog_surface(SDL_Renderer *renderer, const SDL_Rect &rect) { - fill_rounded_rect(renderer, {rect.x + 5, rect.y + 7, rect.w, rect.h}, {0, 0, 0, 0x70}, 12); - fill_rounded_rect(renderer, rect, {PANEL_ALT_RED, PANEL_ALT_GREEN, PANEL_ALT_BLUE, 0xFF}, 12); - fill_rounded_rect(renderer, {rect.x + 20, rect.y + 16, 36, 3}, {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}, 1); + ui::draw_dialog_surface(renderer, rect); } bool render_text_line( // NOSONAR(cpp:S107) helper keeps the SDL text rendering callsite contract explicit @@ -1501,15 +1388,9 @@ namespace { return false; } - const int hintHeight = std::max(30, TTF_FontLineSkip(smallFont) + 10); + const int hintHeight = measure_footer_actions_height(smallFont, viewModel.modal.footerActions, modalRect.w - 36); const SDL_Rect hintRect {modalRect.x + 18, modalRect.y + 64 + pathHeight + 8, modalRect.w - 36, hintHeight}; - if (const std::vector logViewerActions = { - {"change-view", "Change View", "icons\\button-x.svg", "icons\\button-y.svg", false}, - {"scroll", "Scroll", "icons\\button-lb.svg", "icons\\button-rb.svg", false}, - {"fast-scroll", "Fast Scroll", "icons\\button-lt.svg", "icons\\button-rt.svg", false}, - {"close", "Close", "icons\\button-a.svg", "icons\\button-b.svg", false}, - }; - !render_footer_actions(renderer, smallFont, assetCache, logViewerActions, hintRect)) { + if (!render_footer_actions(renderer, smallFont, assetCache, viewModel.modal.footerActions, hintRect)) { return false; } @@ -1695,11 +1576,13 @@ namespace { */ struct FooterActionChipLayout { int labelWidth = 0; ///< Width of the action label. - int iconSize = 0; ///< Square controller-glyph size. - int iconBlockWidth = 0; ///< Combined width of the controller glyphs. + int iconSize = 0; ///< Height of controller glyphs or keyboard keycaps. + int iconBlockWidth = 0; ///< Combined input glyph width. int chipWidth = 0; ///< Total width including content and padding. int horizontalPadding = 10; ///< Inset at each horizontal edge. int iconLabelGap = 8; ///< Space between the glyphs and action label. + int primaryIconWidth = 0; ///< Primary glyph width, including wide keyboard labels. + int secondaryIconWidth = 0; ///< Optional secondary glyph width. }; int measure_footer_action_label_width(TTF_Font *font, const std::string &label) { @@ -1720,50 +1603,76 @@ namespace { * @return Measured icon, label and button widths. */ FooterActionChipLayout measure_footer_action_chip(TTF_Font *font, const ui::ShellFooterAction &action, int chipHeight, bool compact) { - const int iconCount = (action.iconAssetPath.empty() ? 0 : 1) + (action.secondaryIconAssetPath.empty() ? 0 : 1); - const int iconSize = (action.iconAssetPath.empty() && action.secondaryIconAssetPath.empty()) ? 0 : std::max(18, chipHeight - 14); - const int iconBlockWidth = iconCount == 0 ? 0 : (iconSize * iconCount) + ((iconCount - 1) * 4); - const int padding = compact ? 6 : 10; - const int labelGap = compact ? 4 : 8; - return { - measure_footer_action_label_width(font, action.label), - iconSize, - iconBlockWidth, - padding * 2 + iconBlockWidth + (iconBlockWidth > 0 ? labelGap : 0) + measure_footer_action_label_width(font, action.label), - padding, - labelGap, - }; + FooterActionChipLayout layout; + const bool keyboard = !action.keyLabel.empty() || !action.secondaryKeyLabel.empty(); + layout.iconSize = keyboard ? std::max(18, TTF_FontHeight(font) + 6) : std::max(18, chipHeight - 14); + layout.primaryIconWidth = !action.keyLabel.empty() ? measure_footer_action_label_width(font, action.keyLabel) + 12 : action.iconAssetPath.empty() ? 0 : + layout.iconSize; + layout.secondaryIconWidth = !action.secondaryKeyLabel.empty() ? measure_footer_action_label_width(font, action.secondaryKeyLabel) + 12 : action.secondaryIconAssetPath.empty() ? 0 : + layout.iconSize; + layout.iconBlockWidth = layout.primaryIconWidth + layout.secondaryIconWidth + (layout.primaryIconWidth > 0 && layout.secondaryIconWidth > 0 ? 4 : 0); + layout.horizontalPadding = compact ? 6 : 10; + layout.iconLabelGap = compact ? 4 : 8; + layout.labelWidth = measure_footer_action_label_width(font, action.label); + layout.chipWidth = layout.horizontalPadding * 2 + layout.iconBlockWidth + (layout.iconBlockWidth > 0 ? layout.iconLabelGap : 0) + layout.labelWidth; + return layout; + } + + /** + * @brief Reserve enough footer rows to keep wide keyboard keycaps and every action visible. + * @param font Footer font. + * @param actions Ordered input hints. + * @param width Available footer width, including its horizontal inset. + * @param compact Use compact keypad spacing. + * @return Height including vertical padding and wrapped rows. + */ + int measure_footer_actions_height(TTF_Font *font, const std::vector &actions, int width, bool compact) { + const int chipHeight = std::max(30, TTF_FontLineSkip(font) + 12); + const int available = std::max(1, width - 32); + const int gap = compact ? 6 : 12; + int rows = 1; + int used = 0; + for (const auto &action : actions) { + const int chipWidth = std::min(available, measure_footer_action_chip(font, action, chipHeight, compact).chipWidth); + if (used != 0 && used + gap + chipWidth > available) { + ++rows; + used = 0; + } + used += (used == 0 ? 0 : gap) + chipWidth; + } + return rows * chipHeight + (rows - 1) * 6 + 16; } /** * @brief Draw controller glyphs using the same spacing as the measured action chip. * * @param renderer Active renderer. + * @param font Font for keyboard keycaps. * @param assetCache Cached controller icons. - * @param action Controller glyph paths. + * @param action Controller glyph paths or keyboard labels. * @param chipRect Action bounds. * @param layout Measured icon sizes and spacing. * @return Horizontal starting point for the action label. */ - int render_footer_action_icons(SDL_Renderer *renderer, AssetTextureCache *assetCache, const ui::ShellFooterAction &action, const SDL_Rect &chipRect, const FooterActionChipLayout &layout) { + int render_footer_action_icons(SDL_Renderer *renderer, TTF_Font *font, AssetTextureCache *assetCache, const ui::ShellFooterAction &action, const SDL_Rect &chipRect, const FooterActionChipLayout &layout) { int contentX = chipRect.x + layout.horizontalPadding; - const int iconSize = layout.iconSize; - if (iconSize <= 0) { - return contentX; - } - - const auto render_icon = [&](const std::string &assetPath) { - if (assetPath.empty()) { + const auto render_glyph = [&](const std::string &assetPath, const std::string &keyLabel, int width) { + if (width <= 0) { return; } - const SDL_Rect iconRect {contentX, chipRect.y + (chipRect.h - iconSize) / 2, iconSize, iconSize}; - render_asset_icon(renderer, assetCache, assetPath, iconRect); - contentX += iconSize + 4; + const SDL_Rect rect {contentX, chipRect.y + (chipRect.h - layout.iconSize) / 2, width, layout.iconSize}; + if (!keyLabel.empty()) { + fill_rounded_rect(renderer, rect, {0x2C, 0x32, 0x3B, 0xFF}, 3); + draw_rounded_rect_outline(renderer, rect, {0xB8, 0xC2, 0xD0, 0xFF}, 3); + render_text_centered_simple(renderer, font, keyLabel, {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, rect); + } else { + render_asset_icon(renderer, assetCache, assetPath, rect); + } + contentX += width + 4; }; - - render_icon(action.iconAssetPath); - render_icon(action.secondaryIconAssetPath); - return chipRect.x + layout.horizontalPadding + layout.iconBlockWidth + layout.iconLabelGap; + render_glyph(action.iconAssetPath, action.keyLabel, layout.primaryIconWidth); + render_glyph(action.secondaryIconAssetPath, action.secondaryKeyLabel, layout.secondaryIconWidth); + return chipRect.x + layout.horizontalPadding + layout.iconBlockWidth + (layout.iconBlockWidth > 0 ? layout.iconLabelGap : 0); } /** @@ -1786,7 +1695,7 @@ namespace { const SDL_Rect &chipRect ) { fill_rounded_rect(renderer, chipRect, {PANEL_RED, PANEL_GREEN, PANEL_BLUE, 0xFF}, 6); - const int contentX = render_footer_action_icons(renderer, assetCache, action, chipRect, layout); + const int contentX = render_footer_action_icons(renderer, font, assetCache, action, chipRect, layout); return render_text_line(renderer, font, action.label, {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, contentX, chipRect.y + std::max(6, (chipRect.h - TTF_FontLineSkip(font)) / 2), chipRect.w - (contentX - chipRect.x) - layout.horizontalPadding); } @@ -1811,19 +1720,21 @@ namespace { ) { int cursorX = footerRect.x + 16; const int availableRight = footerRect.x + footerRect.w - 16; - const int chipHeight = std::max(30, footerRect.h - 18); - const int chipY = footerRect.y + (footerRect.h - chipHeight) / 2; - - for (const ui::ShellFooterAction &action : actions) { - const FooterActionChipLayout layout = measure_footer_action_chip(font, action, chipHeight, compact); - if (cursorX + layout.chipWidth > availableRight) { + const int chipHeight = std::max(30, TTF_FontLineSkip(font) + 12); + int cursorY = footerRect.y + std::max(0, std::min(8, (footerRect.h - chipHeight) / 2)); + for (const auto &action : actions) { + auto layout = measure_footer_action_chip(font, action, chipHeight, compact); + layout.chipWidth = std::min(layout.chipWidth, std::max(1, footerRect.w - 32)); + if (cursorX + layout.chipWidth > availableRight && cursorX > footerRect.x + 16) { + cursorX = footerRect.x + 16; + cursorY += chipHeight + 6; + } + if (cursorY + chipHeight > footerRect.y + footerRect.h) { break; } - - if (const SDL_Rect chipRect {cursorX, chipY, layout.chipWidth, chipHeight}; !render_footer_action_chip(renderer, font, assetCache, action, layout, chipRect)) { + if (!render_footer_action_chip(renderer, font, assetCache, action, layout, {cursorX, cursorY, layout.chipWidth, chipHeight})) { return false; } - cursorX += layout.chipWidth + (compact ? 6 : 12); } @@ -2019,11 +1930,7 @@ namespace { /** * @brief Tracks the repeat timing for one controller navigation direction. */ - struct ControllerNavigationHoldState { - bool active = false; ///< True while the direction remains held past activation. - Uint32 activatedTick = 0U; ///< Tick count when the direction first became active. - Uint32 lastRepeatTick = 0U; ///< Tick count when the last repeat command fired. - }; + using input::ControllerNavigationHoldState; /** * @brief Clears the held-repeat bookkeeping for every controller navigation direction. @@ -2395,6 +2302,11 @@ namespace { } } + /** + * @brief Snapshot persisted shell and stream preferences, including screenshot storage and OSK placement. + * @param state Current client state. + * @return Settings snapshot ready for TOML serialization. + */ app::AppSettings persistent_settings_from_state(const app::ClientState &state) { return { state.settings.loggingLevel, @@ -2407,6 +2319,8 @@ namespace { state.settings.playAudioOnPc, state.settings.showPerformanceStats, state.settings.playAudioOnXbox, + state.settings.screenshotDirectory, + state.settings.keyboardDockTop, }; } @@ -3939,7 +3853,7 @@ namespace { const int titleHeight = TTF_FontLineSkip(bodyFont) + 14; const int hintHeight = keypad_modal_text_height(renderer, smallFont, viewModel, innerWidth, cache); const int valueHeight = TTF_FontLineSkip(bodyFont) + 20; - const int footerHeight = std::max(38, TTF_FontLineSkip(smallFont) + 18); + const int footerHeight = measure_footer_actions_height(smallFont, viewModel.keypad.footerActions, innerWidth + 12, true); const int preferredKeyHeight = std::max(36, TTF_FontLineSkip(bodyFont) + 18); const int desiredHeight = padding * 2 + 12 + titleHeight + hintHeight + valueHeight + 20 + rows * (preferredKeyHeight + gap) + footerHeight; const SDL_Rect bounds = sdl_rect(ui::calculate_shell_dialog(width, height, dialogWidth, desiredHeight)); @@ -4200,7 +4114,7 @@ namespace { draw_rect(renderer, modalRect, ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE); render_text_line_simple(renderer, bodyFont, "Log File", {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 16, modalRect.w - 32); render_text_line_simple(renderer, smallFont, "The full log viewer could not be rendered safely.", {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 54, modalRect.w - 32); - render_text_line_simple(renderer, smallFont, "Press B to close.", {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 54 + TTF_FontLineSkip(smallFont) + 8, modalRect.w - 32); + render_text_line_simple(renderer, smallFont, (viewModel.modal.footerActions.empty() || viewModel.modal.footerActions.back().keyLabel.empty() ? "Press B to close." : "Press Esc to close."), {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 54 + TTF_FontLineSkip(smallFont) + 8, modalRect.w - 32); } } else if (viewModel.modal.visible) { if (!render_shell_dialog(renderer, titleFont, bodyFont, smallFont, assetCache, viewModel.modal, screenWidth, screenHeight)) { @@ -4246,34 +4160,10 @@ namespace { } } - input::UiCommand translate_unrepeated_keydown(const SDL_KeyboardEvent &event) { - if (event.repeat != 0) { - return input::UiCommand::none; - } - return translate_keyboard_key(event.keysym.sym, event.keysym.mod); - } - - struct ShellInputState { - bool leftTriggerPressed = false; - bool rightTriggerPressed = false; - bool leftShoulderPressed = false; - bool rightShoulderPressed = false; - bool controllerStartPressed = false; - bool controllerBackPressed = false; - bool controllerExitComboArmed = false; - bool controllerExitComboTriggered = false; - Uint32 controllerStartDownTick = 0U; - Uint32 controllerBackDownTick = 0U; - Uint32 leftShoulderRepeatTick = 0U; - Uint32 rightShoulderRepeatTick = 0U; - Uint32 leftTriggerRepeatTick = 0U; - Uint32 rightTriggerRepeatTick = 0U; - ControllerNavigationHoldState moveUpHoldState {}; - ControllerNavigationHoldState moveDownHoldState {}; - ControllerNavigationHoldState moveLeftHoldState {}; - ControllerNavigationHoldState moveRightHoldState {}; - bool controllerNavigationNeutralRequired = false; - }; + /** + * @brief Retains controller and keyboard hold state between shell frames. + */ + using input::ShellInputState; void reset_shell_input_state(ShellInputState *inputState) { if (inputState == nullptr) { @@ -4283,6 +4173,19 @@ namespace { *inputState = {}; } + /** + * @brief Switch contextual helpers and stop held navigation from the previous device. + * @param state Shell input device metadata. + * @param inputState Held input state to clear on a device switch. + * @param device Most recently used input device. + */ + void set_shell_input_device(app::ClientState &state, ShellInputState *inputState, input::UiInputDevice device) { + if (state.shell.inputDevice != device) { + reset_shell_input_state(inputState); + state.shell.inputDevice = device; + } + } + void disarm_controller_exit_combo(ShellInputState *inputState) { if (inputState == nullptr) { return; @@ -4318,7 +4221,7 @@ namespace { ShellInputState *inputState, const ProcessCommand &processCommand ) { - if (inputState == nullptr || state.modal.id != app::ModalId::log_viewer) { + if (inputState == nullptr || state.modal.id != app::ModalId::log_viewer || state.shell.inputDevice != input::UiInputDevice::gamepad) { return; } @@ -4445,6 +4348,14 @@ namespace { return command; } + /** + * @brief Route device transitions and maintain the input source used by contextual helpers. + * @param state Reducer state and current input device. + * @param event SDL event to process. + * @param controller Owned primary controller reference. + * @param inputState Keyboard and controller hold timers. + * @return Initial command from the event, or none for lifecycle and release events. + */ input::UiCommand handle_shell_event( app::ClientState &state, const SDL_Event &event, @@ -4462,6 +4373,7 @@ namespace { handle_controller_device_removed(controller, event.cdevice, inputState); return input::UiCommand::none; case SDL_CONTROLLERBUTTONDOWN: + set_shell_input_device(state, inputState, input::UiInputDevice::gamepad); return handle_controller_button_down_event(event.cbutton, state, inputState); case SDL_CONTROLLERBUTTONUP: return handle_controller_button_up_event(event.cbutton, inputState); @@ -4470,9 +4382,30 @@ namespace { return input::UiCommand::none; } + if (input::is_gamepad_ui_activity(event.caxis.value, event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) { + set_shell_input_device(state, inputState, input::UiInputDevice::gamepad); + } return translate_trigger_axis(event.caxis, &inputState->leftTriggerPressed, &inputState->rightTriggerPressed); case SDL_KEYDOWN: - return translate_unrepeated_keydown(event.key); + if (event.key.repeat != 0 || inputState == nullptr) { + return input::UiCommand::none; + } + set_shell_input_device(state, inputState, input::UiInputDevice::keyboard); + if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) != 0 && hosts_screen_exit_combo_allowed(state) && !state.modal.active()) { + state.shell.shouldExit = true; + return input::UiCommand::none; + } + return inputState->keyboardNavigation.press(event.key.keysym.scancode, translate_keyboard_key(event.key.keysym.sym, event.key.keysym.mod), SDL_GetTicks()); + case SDL_KEYUP: + if (inputState != nullptr) { + inputState->keyboardNavigation.release(event.key.keysym.scancode); + } + return input::UiCommand::none; + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { + reset_shell_input_state(inputState); + } + return input::UiCommand::none; default: return input::UiCommand::none; } @@ -4753,6 +4686,7 @@ namespace { return; } + runtime->inputState.reset_after_stream(); reset_pairing_task(&runtime->pairingTask); reset_app_list_task(&runtime->appListTask); reset_app_art_task(&runtime->appArtTask); @@ -4902,6 +4836,9 @@ namespace { } initialize_shell_runtime(state, runtime); + app::AppUpdate settingsUpdate {}; + settingsUpdate.persistence.settingsChanged = state.settings.dirty; + persist_settings_if_needed(state, settingsUpdate); if (streamStarted && state.hosts.activeLoaded) { state.hosts.active.appListState = app::HostAppListState::loading; state.hosts.active.appListStatusMessage = "Refreshing host apps after the stream session..."; @@ -4998,11 +4935,18 @@ namespace { start_shell_background_tasks_if_needed(state, runtime, SDL_GetTicks()); update_exit_combo_hold(state, &runtime->inputState); - process_log_viewer_repeat_commands(state, SDL_GetTicks(), &runtime->inputState, processCommand); - + const auto previousInputDevice = state.shell.inputDevice; bool skipPolledControllerNavigation = false; process_polled_shell_events(state, &resources->controller, &runtime->inputState, processCommand, &skipPolledControllerNavigation); - process_controller_navigation(resources->controller, &runtime->inputState, skipPolledControllerNavigation, processCommand); + if (state.shell.inputDevice == input::UiInputDevice::keyboard) { + processCommand(runtime->inputState.keyboardNavigation.poll(SDL_GetTicks())); + } else { + process_log_viewer_repeat_commands(state, SDL_GetTicks(), &runtime->inputState, processCommand); + process_controller_navigation(resources->controller, &runtime->inputState, skipPolledControllerNavigation, processCommand); + } + if (previousInputDevice != state.shell.inputDevice) { + runtime->keypadRedrawRequested = true; + } finish_shell_background_tasks(state, resources, runtime); start_shell_background_tasks_if_needed(state, runtime, SDL_GetTicks()); diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index a7d1354..11306bb 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -705,6 +705,90 @@ namespace { return {}; } + /** + * @brief Translate a bundled controller glyph to its working keyboard equivalent. + * @param icon Controller icon path. + * @return Keyboard keycap label, or an empty string for a non-controller icon. + */ + std::string keyboard_hint_for_icon(const std::string &icon) { + if (icon == "icons\\button-a.svg" || icon == "icons\\button-start.svg") { + return "Enter"; + } + if (icon == "icons\\button-b.svg" || icon == "icons\\button-select.svg") { + return "Esc"; + } + if (icon == "icons\\button-x.svg") { + return "Bksp"; + } + if (icon == "icons\\button-y.svg") { + return "M"; + } + if (icon == "icons\\button-lb.svg") { + return "PgUp"; + } + if (icon == "icons\\button-rb.svg") { + return "PgDn"; + } + if (icon == "icons\\button-lt.svg") { + return "Shift+PgUp"; + } + if (icon == "icons\\button-rt.svg") { + return "Shift+PgDn"; + } + return {}; + } + + /** + * @brief Replace controller footer glyphs with scalable keyboard keycaps. + * @param actions Page, modal or notification actions. + */ + void use_keyboard_hints(std::vector &actions) { + for (auto &action : actions) { + action.keyLabel = keyboard_hint_for_icon(action.iconAssetPath); + action.secondaryKeyLabel = keyboard_hint_for_icon(action.secondaryIconAssetPath); + if (action.id == "exit") { + action.keyLabel = "Alt+F4"; + action.secondaryKeyLabel.clear(); + } + if (action.id == "enter") { + action.keyLabel = "Space"; + } + if (action.keyLabel == action.secondaryKeyLabel) { + action.secondaryKeyLabel.clear(); + } + if (!action.keyLabel.empty()) { + action.iconAssetPath.clear(); + } + if (!action.secondaryKeyLabel.empty() || action.secondaryIconAssetPath == "icons\\button-start.svg") { + action.secondaryIconAssetPath.clear(); + } + } + } + + /** + * @brief Supply default modal actions and choose glyphs for the most recently used input device. + * @param state Source input device. + * @param viewModel Receives contextual helpers throughout the shell. + */ + void fill_input_hints(const app::ClientState &state, ui::ShellViewModel &viewModel) { + if (viewModel.modal.visible && !viewModel.modal.busy && viewModel.modal.footerActions.empty()) { + if (viewModel.modal.actions.empty()) { + viewModel.modal.footerActions = {{"close", "Close", "icons\\button-b.svg", {}, false}}; + } else { + viewModel.modal.footerActions = {{"select", "Select", "icons\\button-a.svg", {}, true}, {"back", "Back", "icons\\button-b.svg", {}, false}}; + } + } + if (state.shell.inputDevice != input::UiInputDevice::keyboard) { + return; + } + use_keyboard_hints(viewModel.frame.footerActions); + use_keyboard_hints(viewModel.keypad.footerActions); + use_keyboard_hints(viewModel.modal.footerActions); + for (auto ¬ification : viewModel.notification.items) { + use_keyboard_hints(notification.actions); + } + } + } // namespace namespace ui { @@ -835,6 +919,7 @@ namespace ui { } fill_modal_view(state, &viewModel); + fill_input_hints(state, viewModel); fill_view_model_overlay(state, logEntries, statsLines, &viewModel); diff --git a/src/ui/shell_view.h b/src/ui/shell_view.h index 271e057..199c123 100644 --- a/src/ui/shell_view.h +++ b/src/ui/shell_view.h @@ -77,6 +77,8 @@ namespace ui { std::string iconAssetPath; ///< Primary icon asset path. std::string secondaryIconAssetPath; ///< Optional secondary icon asset path. bool emphasized = false; ///< True when the action should receive visual emphasis. + std::string keyLabel; ///< Primary keyboard keycap label, replacing the primary controller icon. + std::string secondaryKeyLabel; ///< Optional second keyboard keycap label. }; /** diff --git a/src/ui/svg_assets.cpp b/src/ui/svg_assets.cpp new file mode 100644 index 0000000..ca82d66 --- /dev/null +++ b/src/ui/svg_assets.cpp @@ -0,0 +1,100 @@ +/** + * @file src/ui/svg_assets.cpp + * @brief Rasterizes shared shell and streaming SVG assets with NanoSVG. + */ +#include "src/ui/svg_assets.h" + +#include +#include +#include +#include + +namespace ui { + SDL_Surface *load_svg_surface(const std::string &assetPath, int maxWidth, int maxHeight) { + if (maxWidth <= 0 || maxHeight <= 0) { + return nullptr; + } + + SDL_RWops *rw = SDL_RWFromFile(assetPath.c_str(), "rb"); + if (rw == nullptr) { + return nullptr; + } + + size_t dataSize = 0U; + auto *data = static_cast(SDL_LoadFile_RW(rw, &dataSize, 1)); + if (data == nullptr) { + return nullptr; + } + + NSVGimage *image = nsvgParse(data, "px", 96.0f); + SDL_free(data); + if (image == nullptr) { + return nullptr; + } + + int targetWidth = 0; + int targetHeight = 0; + if (image->width > 0 && image->height > 0) { + targetWidth = maxWidth; + targetHeight = maxHeight; + if (image->width * static_cast(maxHeight) > image->height * static_cast(maxWidth)) { + targetHeight = std::max(1, static_cast(image->height * static_cast(maxWidth) / image->width)); + } else { + targetWidth = std::max(1, static_cast(image->width * static_cast(maxHeight) / image->height)); + } + } + if (targetWidth <= 0 || targetHeight <= 0) { + nsvgDelete(image); + return nullptr; + } + + NSVGrasterizer *rasterizer = nsvgCreateRasterizer(); + if (rasterizer == nullptr) { + nsvgDelete(image); + return nullptr; + } + + SDL_Surface *surface = SDL_CreateRGBSurface( + SDL_SWSURFACE, + targetWidth, + targetHeight, + 32, + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0xFF000000 + ); + if (surface == nullptr) { + nsvgDeleteRasterizer(rasterizer); + nsvgDelete(image); + return nullptr; + } + + const bool mustLockSurface = SDL_MUSTLOCK(surface) != 0; + if (mustLockSurface && SDL_LockSurface(surface) != 0) { + SDL_FreeSurface(surface); + nsvgDeleteRasterizer(rasterizer); + nsvgDelete(image); + return nullptr; + } + + SDL_memset(surface->pixels, 0, static_cast(surface->pitch) * static_cast(surface->h)); + const float rasterScale = std::min(static_cast(targetWidth) / image->width, static_cast(targetHeight) / image->height); + nsvgRasterize(rasterizer, image, 0.0f, 0.0f, rasterScale, static_cast(surface->pixels), surface->w, surface->h, surface->pitch); + + if (mustLockSurface) { + SDL_UnlockSurface(surface); + } + + nsvgDeleteRasterizer(rasterizer); + nsvgDelete(image); + + if (SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND) != 0) { + SDL_FreeSurface(surface); + return nullptr; + } + + return surface; + } + +} // namespace ui diff --git a/src/ui/svg_assets.h b/src/ui/svg_assets.h new file mode 100644 index 0000000..d5faeb4 --- /dev/null +++ b/src/ui/svg_assets.h @@ -0,0 +1,20 @@ +/** + * @file src/ui/svg_assets.h + * @brief Shared SVG rasterization for the shell and streaming controls. + */ +#pragma once + +#include + +struct SDL_Surface; + +namespace ui { + /** + * @brief Load a transparent RGBA SVG raster fitted to the requested bounds. + * @param assetPath Full path to the SVG file. + * @param maxWidth Maximum raster width in pixels. + * @param maxHeight Maximum raster height in pixels. + * @return Owned SDL surface to free with SDL_FreeSurface, or nullptr on failure. + */ + SDL_Surface *load_svg_surface(const std::string &assetPath, int maxWidth, int maxHeight); +} // namespace ui diff --git a/tests/unit/app/settings_storage_test.cpp b/tests/unit/app/settings_storage_test.cpp index c0b15c9..5d51706 100644 --- a/tests/unit/app/settings_storage_test.cpp +++ b/tests/unit/app/settings_storage_test.cpp @@ -2,18 +2,16 @@ * @file tests/unit/app/settings_storage_test.cpp * @brief Verifies application settings persistence. */ -// test header include -#include "src/app/settings_storage.h" +// test includes +#include // standard includes #include #include #include -// lib includes -#include - -// test includes +// local includes +#include "src/app/settings_storage.h" #include "tests/support/filesystem_test_utils.h" namespace { @@ -247,3 +245,31 @@ namespace { } } // namespace + +/** + * @brief Screenshot paths with backslashes, quotes and spaces survive TOML serialization with keyboard docking. + */ +TEST_F(SettingsStorageTest, PreservesScreenshotDirectoryAndKeyboardDocking) { + app::AppSettings settings; + settings.screenshotDirectory = "F:\\Screenshots\\Moonlight \"test\""; + settings.keyboardDockTop = true; + ASSERT_TRUE(app::save_app_settings(settings, settingsPath).success); + const auto loaded = app::load_app_settings(settingsPath); + EXPECT_TRUE(loaded.warnings.empty()); + EXPECT_FALSE(loaded.cleanupRequired); + EXPECT_EQ(loaded.settings.screenshotDirectory, settings.screenshotDirectory); + EXPECT_TRUE(loaded.settings.keyboardDockTop); +} + +/** + * @brief Missing settings preserve sensible defaults; malformed paths and docking types produce warnings. + */ +TEST_F(SettingsStorageTest, DefaultsInvalidScreenshotAndDockingSettings) { + write_text_file(settingsPath, "[streaming]\nscreenshot_directory = 42\nkeyboard_dock_top = 'yes'\n"); + const auto loaded = app::load_app_settings(settingsPath); + EXPECT_TRUE(loaded.settings.screenshotDirectory.empty()); + EXPECT_FALSE(loaded.settings.keyboardDockTop); + EXPECT_EQ(loaded.warnings.size(), 2); + write_text_file(settingsPath, "[streaming]\nscreenshot_directory = \"bad\\npath\"\n"); + EXPECT_TRUE(app::load_app_settings(settingsPath).settings.screenshotDirectory.empty()); +} diff --git a/tests/unit/input/navigation_input_test.cpp b/tests/unit/input/navigation_input_test.cpp index f1a811e..69d7b39 100644 --- a/tests/unit/input/navigation_input_test.cpp +++ b/tests/unit/input/navigation_input_test.cpp @@ -2,14 +2,17 @@ * @file tests/unit/input/navigation_input_test.cpp * @brief Verifies controller navigation input handling. */ -// class header include -#include "src/input/navigation_input.h" - -// lib includes +// test includes #include +// local includes +#include "src/input/navigation_input.h" + namespace { + /** + * @brief Controller buttons map to their documented navigation and action commands. + */ TEST(NavigationInputTest, MapsControllerButtonsToNavigationCommands) { EXPECT_EQ(input::map_gamepad_button_to_ui_command(input::GamepadButton::dpad_up), input::UiCommand::move_up); EXPECT_EQ(input::map_gamepad_button_to_ui_command(input::GamepadButton::dpad_down), input::UiCommand::move_down); @@ -26,6 +29,9 @@ namespace { EXPECT_EQ(input::map_gamepad_button_to_ui_command(static_cast(999)), input::UiCommand::none); } + /** + * @brief Every signed stick direction maps to its corresponding navigation command. + */ TEST(NavigationInputTest, MapsControllerAxisDirectionsToNavigationCommands) { EXPECT_EQ(input::map_gamepad_axis_direction_to_ui_command(input::GamepadAxisDirection::left_stick_up), input::UiCommand::move_up); EXPECT_EQ(input::map_gamepad_axis_direction_to_ui_command(input::GamepadAxisDirection::left_stick_down), input::UiCommand::move_down); @@ -34,6 +40,9 @@ namespace { EXPECT_EQ(input::map_gamepad_axis_direction_to_ui_command(static_cast(999)), input::UiCommand::none); } + /** + * @brief Keyboard helper labels correspond to the same commands as the controller controls. + */ TEST(NavigationInputTest, MapsKeyboardKeysToNavigationCommands) { EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::up), input::UiCommand::move_up); EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::down), input::UiCommand::move_down); @@ -48,6 +57,8 @@ namespace { EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::tab, true), input::UiCommand::previous_page); EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::page_up), input::UiCommand::previous_page); EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::page_down), input::UiCommand::next_page); + EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::page_up, true), input::UiCommand::fast_previous_page); + EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::page_down, true), input::UiCommand::fast_next_page); EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::i), input::UiCommand::open_context_menu); EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::m), input::UiCommand::open_context_menu); EXPECT_EQ(input::map_keyboard_key_to_ui_command(input::KeyboardKey::f3), input::UiCommand::toggle_overlay); diff --git a/tests/unit/input/stream_exit_test.cpp b/tests/unit/input/stream_exit_test.cpp index 1e2ea0a..184e44a 100644 --- a/tests/unit/input/stream_exit_test.cpp +++ b/tests/unit/input/stream_exit_test.cpp @@ -1,6 +1,6 @@ /** * @file tests/unit/input/stream_exit_test.cpp - * @brief Verifies digital-only exit detection, event ordering and disconnect cleanup. + * @brief Verifies digital-only stream menu shortcut detection, event ordering and disconnect cleanup. */ // test includes #include @@ -14,7 +14,7 @@ #include "third-party/moonlight-common-c/src/Limelight.h" /** - * @brief Back and both shoulders quit immediately without Start, axes or a timer. + * @brief Back and both shoulders request the menu immediately without Start, axes or a timer. */ TEST(StreamExitTest, RequiresAllThreeDigitalButtons) { const std::uint32_t chord = BACK_FLAG | LB_FLAG | RB_FLAG; diff --git a/tests/unit/input/stream_input_test.cpp b/tests/unit/input/stream_input_test.cpp new file mode 100644 index 0000000..6cc167e --- /dev/null +++ b/tests/unit/input/stream_input_test.cpp @@ -0,0 +1,276 @@ +/** + * @file tests/unit/input/stream_input_test.cpp + * @brief Verifies independent gamepads, capture lifetimes and merged pointer/keyboard input. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/input/stream_input.h" +#include "third-party/moonlight-common-c/src/Limelight.h" + +/** + * @brief Captures ordered transport packets without a host connection. + */ +class StreamInputTest: public testing::Test { +protected: + std::vector packets; ///< Packets emitted by the router. + input::StreamInput router {[this](const input::StreamPacket &packet) { + packets.push_back(packet); + }}; ///< Router under test. +}; + +/** + * @brief Four controllers arrive independently, and a removed slot is reused without shifting players. + */ +TEST_F(StreamInputTest, MaintainsFourStableSlotsAndRemovalMask) { + EXPECT_EQ(router.connect(-1), -1); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(router.connect(10 + i), i); + ASSERT_EQ(packets.size(), static_cast((i + 1) * 2)); + EXPECT_EQ(packets[packets.size() - 2].type, input::StreamPacketType::arrival); + EXPECT_EQ(packets.back().mask, (1 << (i + 1)) - 1); + } + EXPECT_EQ(router.connect(12), 2); + EXPECT_EQ(router.connect(99), -1); + EXPECT_EQ(router.active_mask(), 15); + router.disconnect(11); + EXPECT_EQ(packets.back().slot, 1); + EXPECT_EQ(packets.back().mask, 13); + EXPECT_EQ(packets.back().controller.buttonFlags, 0); + EXPECT_EQ(router.connect(99), 1); + EXPECT_EQ(router.connect(12), 2); + EXPECT_EQ(router.connect(13), 3); + const auto count = packets.size(); + router.disconnect(400); + EXPECT_EQ(packets.size(), count); +} + +/** + * @brief Full analog state and quick digital press/release transitions reach each pad's own slot. + */ +TEST_F(StreamInputTest, PreservesAxesTriggersAndQuickTransitions) { + router.connect(30); + router.connect(31); + input::ControllerState sample {A_FLAG, 128, 255, -32768, 32767, 12345, -12345}; + packets.clear(); + router.controller(31, sample); + router.controller(31, sample); + router.controller(31, {}); + ASSERT_EQ(packets.size(), 2); + EXPECT_EQ(packets[0].slot, 1); + EXPECT_EQ(packets[0].mask, 3); + EXPECT_EQ(packets[0].controller.leftTrigger, 128); + EXPECT_EQ(packets[0].controller.rightTrigger, 255); + EXPECT_EQ(packets[0].controller.leftStickX, -32768); + EXPECT_EQ(packets[0].controller.leftStickY, 32767); + EXPECT_EQ(packets[0].controller.rightStickX, 12345); + EXPECT_EQ(packets[0].controller.rightStickY, -12345); + EXPECT_EQ(packets[1].controller.buttonFlags, 0); + router.controller(400, sample); + EXPECT_EQ(packets.size(), 2); +} + +/** + * @brief Opening local controls releases all host devices and waits for pad neutral before resuming. + */ +TEST_F(StreamInputTest, CaptureReleasesHostAndPreventsInputLeakage) { + router.connect(1); + router.connect(2); + router.controller(1, {A_FLAG}); + router.controller(2, {B_FLAG}); + router.key(0, 'W', true); + router.mouse_button(1, BUTTON_LEFT, true); + packets.clear(); + router.set_captured(true); + ASSERT_EQ(packets.size(), 4); + EXPECT_EQ(packets[0].controller.buttonFlags, 0); + EXPECT_EQ(packets[1].controller.buttonFlags, 0); + EXPECT_FALSE(packets[2].pressed); + EXPECT_FALSE(packets[3].pressed); + router.set_captured(true); + router.controller(1, {X_FLAG}); + router.key(0, 'A', true); + router.mouse_button(1, BUTTON_RIGHT, true); + router.mouse_move(10, 20); + router.scroll(1, 1); + EXPECT_EQ(packets.size(), 4); + router.set_captured(false); + router.controller(1, {X_FLAG}); + EXPECT_EQ(packets.size(), 4); + router.controller(1, {}); + router.controller(1, {X_FLAG}); + ASSERT_EQ(packets.size(), 5); + EXPECT_EQ(packets.back().controller.buttonFlags, X_FLAG); + // Connecting while a menu is open must also wait for neutral. + router.set_captured(true); + router.connect(3); + router.set_captured(false); + packets.clear(); + router.controller(3, {Y_FLAG}); + EXPECT_TRUE(packets.empty()); + router.controller(3, {}); + router.controller(3, {Y_FLAG}); + EXPECT_EQ(packets.size(), 1); +} + +/** + * @brief Physical and virtual sources cannot release each other's held key or mouse button. + */ +TEST_F(StreamInputTest, MergesDigitalSourcesAndReleasesDisconnects) { + router.key(10, 'A', true, MODIFIER_SHIFT); + router.key(10, 'A', true); + router.key(20, 'A', true); + router.key(10, 'B', true); + router.mouse_button(10, BUTTON_LEFT, true); + router.mouse_button(20, BUTTON_LEFT, true); + EXPECT_EQ(packets.size(), 3); + EXPECT_EQ(packets[0].modifiers, MODIFIER_SHIFT); + router.release_source(10); + ASSERT_EQ(packets.size(), 4); + EXPECT_EQ(packets.back().code, 'B'); + EXPECT_FALSE(packets.back().pressed); + router.release_source(20); + ASSERT_EQ(packets.size(), 6); + EXPECT_FALSE(packets[4].pressed); + EXPECT_FALSE(packets[5].pressed); + router.key(50, 0, true); + router.key(50, 'Z', false); + router.release_source(100); + router.release_all(); + EXPECT_EQ(packets.size(), 6); +} + +/** + * @brief Controller pointer movement is time-based, dead-zoned and isolated from gameplay. + */ +TEST_F(StreamInputTest, MouseModeIntegratesMotionAndReleasesOnRemoval) { + router.connect(10); + router.connect(11); + router.set_mouse_device(10); + router.controller(10, {}); + packets.clear(); + router.controller(10, {A_FLAG | B_FLAG, 0, 0, 32767, 32767}); + router.controller(11, {X_FLAG}); + router.tick(10); + ASSERT_EQ(packets.size(), 4); + EXPECT_EQ(packets[0].code, BUTTON_LEFT); + EXPECT_EQ(packets[1].code, BUTTON_RIGHT); + EXPECT_EQ(packets[2].slot, 1); + EXPECT_EQ(packets[3].x, 9); + EXPECT_EQ(packets[3].y, -9); + packets.clear(); + router.tick(5000); + ASSERT_EQ(packets.size(), 1); + EXPECT_EQ(packets[0].x, 45); + router.disconnect(10); + ASSERT_EQ(packets.size(), 4); + EXPECT_FALSE(packets[1].pressed); + EXPECT_FALSE(packets[2].pressed); + EXPECT_EQ(packets.back().mask, 2); + packets.clear(); + router.tick(10); + EXPECT_TRUE(packets.empty()); +} + +/** + * @brief Quick virtual pointer clicks survive a complete press/release within one event pump. + */ +TEST_F(StreamInputTest, PreservesQuickVirtualMouseClicks) { + router.connect(5); + router.set_mouse_device(5); + router.controller(5, {}); + packets.clear(); + router.controller(5, {A_FLAG}); + router.controller(5, {}); + ASSERT_EQ(packets.size(), 2); + EXPECT_EQ(packets[0].type, input::StreamPacketType::mouse_button); + EXPECT_TRUE(packets[0].pressed); + EXPECT_FALSE(packets[1].pressed); +} + +/** + * @brief Fractional pointer motion accumulates and capture/mode changes clear held clicks. + */ +TEST_F(StreamInputTest, MouseDeadzoneFractionAndCapture) { + router.connect(1); + router.set_mouse_device(1); + router.controller(1, {}); + packets.clear(); + router.controller(1, {0, 0, 0, 6999, -6999}); + router.tick(10); + EXPECT_TRUE(packets.empty()); + router.controller(1, {0, 0, 0, 7100, 0}); + for (int i = 0; i < 40; ++i) { + router.tick(10); + } + EXPECT_FALSE(packets.empty()); + router.controller(1, {A_FLAG}); + router.tick(10); + EXPECT_TRUE(packets.back().pressed); + router.set_captured(true); + EXPECT_FALSE(packets.back().pressed); + const auto count = packets.size(); + router.tick(10); + EXPECT_EQ(packets.size(), count); + router.set_captured(false); + router.controller(1, {}); + router.controller(1, {B_FLAG}); + router.tick(10); + router.set_mouse_device(-1); + EXPECT_EQ(packets.back().code, BUTTON_RIGHT); + EXPECT_FALSE(packets.back().pressed); +} + +/** + * @brief Physical relative movement and both wheel axes use bounded transport deltas. + */ +TEST_F(StreamInputTest, BoundsPhysicalPointerAndWheelEvents) { + router.mouse_move(0, 0); + router.scroll(0, 0); + EXPECT_TRUE(packets.empty()); + router.mouse_move(90000, -90000); + router.scroll(-500, 500); + ASSERT_EQ(packets.size(), 2); + EXPECT_EQ(packets[0].x, 32767); + EXPECT_EQ(packets[0].y, -32768); + EXPECT_EQ(packets[1].x, -127); + EXPECT_EQ(packets[1].y, 127); +} + +/** + * @brief Saturating inversion preserves all extrema and USB usages map to distinct host keys. + */ +TEST(StreamKeyMappingTest, PreservesExtendedModifiersNavigationAndKeypad) { + EXPECT_EQ(input::invert_stick_axis(-32768), 32767); + EXPECT_EQ(input::invert_stick_axis(32767), -32767); + EXPECT_EQ(input::invert_stick_axis(0), 0); + EXPECT_EQ(input::keyboard_virtual_key(4), 'A'); + EXPECT_EQ(input::keyboard_virtual_key(29), 'Z'); + EXPECT_EQ(input::keyboard_virtual_key(30), '1'); + EXPECT_EQ(input::keyboard_virtual_key(39), '0'); + EXPECT_EQ(input::keyboard_virtual_key(40), 0x0D); + EXPECT_EQ(input::keyboard_virtual_key(88), 0x10D); + EXPECT_EQ(input::keyboard_virtual_key(224), 0xA2); + EXPECT_EQ(input::keyboard_virtual_key(228), 0x1A3); + EXPECT_EQ(input::keyboard_virtual_key(82), 0x126); + EXPECT_EQ(input::keyboard_virtual_key(98), 0x60); + EXPECT_EQ(input::keyboard_virtual_key(58), 0x70); + EXPECT_EQ(input::keyboard_virtual_key(115), 0x87); + EXPECT_EQ(input::keyboard_virtual_key(0), 0); + EXPECT_EQ(input::keyboard_virtual_key(300), 0); + // Ordinary keyboard, navigation, keypad and modifier blocks must all be usable. + for (int usage = 4; usage <= 101; ++usage) { + EXPECT_NE(input::keyboard_virtual_key(usage), 0) << usage; + } + for (int usage = 104; usage <= 115; ++usage) { + EXPECT_NE(input::keyboard_virtual_key(usage), 0) << usage; + } + for (int usage = 224; usage <= 231; ++usage) { + EXPECT_NE(input::keyboard_virtual_key(usage), 0) << usage; + } +} diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp new file mode 100644 index 0000000..f68e10f --- /dev/null +++ b/tests/unit/input/stream_menu_test.cpp @@ -0,0 +1,253 @@ +/** + * @file tests/unit/input/stream_menu_test.cpp + * @brief Verifies dual-stick sectors, dead zones, keyboard modifiers and log navigation. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/input/stream_menu.h" +#include "third-party/moonlight-common-c/src/Limelight.h" + +/** + * @brief Both sticks select all eight actions and never select an action in the dead zone. + */ +TEST(StreamMenuTest, SelectsEverySectorAndRetainsCenteredSelection) { + input::StreamMenu menu; + menu.open(42); + EXPECT_EQ(menu.owner, 42); + EXPECT_EQ(menu.selected, input::StreamAction::none); + EXPECT_FALSE(menu.select_stick(false, input::StreamMenu::WHEEL_DEADZONE - 1, -input::StreamMenu::WHEEL_DEADZONE + 1)); + EXPECT_EQ(menu.selected, input::StreamAction::none); + const std::array x {0, 32767, 0, -32768}; + const std::array y {32767, 0, -32768, 0}; + for (int wheel = 0; wheel < 2; ++wheel) { + for (int sector = 0; sector < 4; ++sector) { + EXPECT_TRUE(menu.select_stick(wheel != 0, x[sector], y[sector])); + EXPECT_EQ(static_cast(menu.selected), wheel * 4 + sector); + EXPECT_FALSE(menu.select_stick(wheel != 0, x[sector], y[sector])); + EXPECT_FALSE(menu.select_stick(wheel != 0, 0, 0)); + EXPECT_EQ(static_cast(menu.selected), wheel * 4 + sector); + } + } + menu.page = input::StreamMenuPage::keyboard; + EXPECT_FALSE(menu.select_stick(false, 0, 32767)); + EXPECT_EQ(menu.selected, input::StreamAction::statistics); + menu.close(); + EXPECT_EQ(menu.page, input::StreamMenuPage::closed); + EXPECT_EQ(menu.selected, input::StreamAction::none); +} + +/** + * @brief Sample-only input keeps updating a selected sector and a stationary stick cannot steal focus. + */ +TEST(StreamMenuTest, UpdatesSampledSelectionAfterTheFirstSector) { + input::StreamMenu menu; + menu.open(1); + EXPECT_TRUE(menu.sample_sticks(0, 9000, 0, 0)); + EXPECT_EQ(menu.selected, input::StreamAction::keyboard); + EXPECT_TRUE(menu.sample_sticks(9000, 0, 0, 0)); + EXPECT_EQ(menu.selected, input::StreamAction::mouse); + EXPECT_TRUE(menu.sample_sticks(9000, 0, 0, 9000)); + EXPECT_EQ(menu.selected, input::StreamAction::guide); + EXPECT_FALSE(menu.sample_sticks(9000, 0, 0, 9000)); + EXPECT_EQ(menu.selected, input::StreamAction::guide); + EXPECT_FALSE(menu.sample_sticks(9000, 0, 0, 0)); + EXPECT_EQ(menu.selected, input::StreamAction::guide); + EXPECT_TRUE(menu.sample_sticks(-9000, 0, 0, 0)); + EXPECT_EQ(menu.selected, input::StreamAction::quit); + menu.close(); + menu.open(1); + EXPECT_TRUE(menu.sample_sticks(-9000, 0, 0, 0)); + EXPECT_EQ(menu.selected, input::StreamAction::quit); +} + +/** + * @brief Keyboard focus follows the QWERTY grid and preserves its row across tall controls. + */ +TEST(StreamMenuTest, NavigatesWideAndTallKeyboardCells) { + input::StreamMenu menu; + menu.open(-1); + menu.page = input::StreamMenuPage::keyboard; + EXPECT_EQ(menu.keyboard_key(), 'Q'); + menu.navigate(-1, 0); + EXPECT_EQ(menu.keyboard_key(), 0x125); + menu.navigate(1, 0); + EXPECT_EQ(menu.keyboard_key(), 'Q'); + menu.navigate(0, 1); + EXPECT_EQ(menu.keyboard_key(), 'A'); + menu.navigate(0, -1); + EXPECT_EQ(menu.keyboard_key(), 'Q'); + menu.navigate(0, -1); + EXPECT_EQ(menu.keyboard_key(), '1'); + menu.navigate(0, -1); + EXPECT_EQ(menu.keyboard_key(), 0x20); + menu.navigate(0, 1); + EXPECT_EQ(menu.keyboard_key(), '1'); + menu.navigate(0, -1); + EXPECT_EQ(menu.keyboard_key(), 0x20); + menu.navigate(1, 0); + EXPECT_EQ(menu.keyboard_key(), 0x0D); + menu.navigate(-1, 0); + EXPECT_EQ(menu.keyboard_key(), 0x20); + menu.navigate(0, 1); + EXPECT_EQ(menu.keyboard_key(), '0'); +} + +/** + * @brief Every host key remains accessible, with complete geometry on all three layouts. + */ +TEST(StreamMenuTest, PreservesHostKeysAcrossThreeLayouts) { + input::StreamMenu menu; + menu.open(-1); + menu.page = input::StreamMenuPage::keyboard; + std::set codes; + for (int page = 0; page < 3; ++page) { + EXPECT_EQ(menu.keyboardLayout, page); + std::array coverage {}; + for (const auto &cell : menu.keyboard_cells()) { + if (cell.key < static_cast(input::StreamMenu::KEYS.size())) { + codes.insert(input::StreamMenu::KEYS[cell.key].code); + } + for (int y = cell.row; y < cell.row + cell.height; ++y) { + for (int x = cell.column; x < cell.column + cell.width; ++x) { + ++coverage.at(y * input::StreamMenu::KEYBOARD_COLUMNS + x); + } + } + } + for (int count : coverage) { + EXPECT_EQ(count, 1); + } + menu.keyIndex = 41; + EXPECT_FALSE(menu.key_label(41).empty()); + EXPECT_EQ(menu.keyboard_key(), 0); + } + EXPECT_EQ(menu.keyboardLayout, 0); + for (const auto &key : input::StreamMenu::KEYS) { + EXPECT_EQ(codes.count(key.code), 1) << key.label; + } +} + +/** + * @brief One-shot Shift survives local toggles, affects one key and reverses persistent Caps for letters. + */ +TEST(StreamMenuTest, ConsumesShiftOnceAndKeepsCapsSeparate) { + input::StreamMenu menu; + menu.open(1); + menu.page = input::StreamMenuPage::keyboard; + menu.toggle_shift(); + EXPECT_EQ(menu.key_label(10), "Q"); + EXPECT_EQ(menu.key_modifiers('Q'), MODIFIER_SHIFT); + EXPECT_EQ(menu.key_label(0), "!"); + menu.key_sent(); + EXPECT_FALSE(menu.shiftOnce); + EXPECT_EQ(menu.key_modifiers('Q'), 0); + menu.keyIndex = 43; + EXPECT_EQ(menu.keyboard_key(), 0); + EXPECT_TRUE(menu.caps); + EXPECT_EQ(menu.key_label(10), "Q"); + EXPECT_EQ(menu.key_label(0), "1"); + menu.toggle_shift(); + EXPECT_EQ(menu.key_label(10), "q"); + menu.key_sent(); + EXPECT_EQ(menu.key_label(10), "Q"); + menu.keyboard_key(); + EXPECT_FALSE(menu.caps); + menu.toggle_shift(); + menu.toggle_shift(); + EXPECT_FALSE(menu.shiftOnce); + menu.toggle_shift(); + menu.close(); + EXPECT_FALSE(menu.shiftOnce); + EXPECT_FALSE(menu.caps); +} + +/** + * @brief Explicit symbols send their required Shift and named combinations send their full modifier chord. + */ +TEST(StreamMenuTest, SendsSymbolsAndDesktopCombinations) { + input::StreamMenu menu; + menu.open(-1); + menu.page = input::StreamMenuPage::keyboard; + menu.toggle_keyboard_page(); + menu.keyIndex = 10; + EXPECT_EQ(menu.key_label(10), "!"); + EXPECT_EQ(menu.keyboard_key(), '1'); + EXPECT_EQ(menu.key_modifiers('1'), MODIFIER_SHIFT); + menu.toggle_keyboard_page(); + menu.caps = true; + menu.shiftOnce = true; + menu.modifiers = MODIFIER_META; + for (int i = 20; i < 30; ++i) { + menu.keyIndex = i; + const auto &binding = input::StreamMenu::KEYS[menu.keyboard_cells()[i].key]; + EXPECT_EQ(menu.keyboard_key(), binding.code); + EXPECT_EQ(menu.key_modifiers(binding.code), binding.modifiers); + } + menu.caps = false; + menu.shiftOnce = false; + menu.modifiers = 0; + for (int i = 30; i < 34; ++i) { + menu.keyIndex = i; + EXPECT_EQ(menu.keyboard_key(), 0); + } + EXPECT_EQ(menu.modifiers, MODIFIER_CTRL | MODIFIER_ALT | MODIFIER_META); + EXPECT_TRUE(menu.shiftOnce); + EXPECT_EQ(menu.key_label(30), "[Ctrl]"); + EXPECT_EQ(menu.key_label(33), "[Shift]"); + menu.close(); + EXPECT_EQ(menu.modifiers, 0); +} + +/** + * @brief Disconnect confirmation starts with Cancel and changes only on deliberate navigation. + */ +TEST(StreamMenuTest, DefaultsToCancelOnDisconnectConfirmation) { + input::StreamMenu menu; + menu.open(1); + menu.page = input::StreamMenuPage::quit_confirmation; + EXPECT_FALSE(menu.confirmQuit); + menu.navigate(0, 0); + EXPECT_FALSE(menu.confirmQuit); + menu.navigate(1, 0); + EXPECT_TRUE(menu.confirmQuit); + menu.navigate(-1, 0); + EXPECT_FALSE(menu.confirmQuit); +} + +/** + * @brief D-pad navigation selects a wheel sector and logs clamp to their scroll range. + */ +TEST(StreamMenuTest, SupportsDigitalNavigationAndLogScrolling) { + input::StreamMenu menu; + menu.open(0); + menu.navigate(1, 0); + EXPECT_EQ(menu.selected, input::StreamAction::mouse); + menu.navigate(0, -1); + EXPECT_EQ(menu.selected, input::StreamAction::keyboard); + menu.page = input::StreamMenuPage::logs; + menu.navigate(0, -1); + EXPECT_EQ(menu.logOffset, 5); + menu.navigate(0, -100); + EXPECT_EQ(menu.logOffset, 255); + menu.navigate(0, 100); + EXPECT_EQ(menu.logOffset, 0); + menu.close(); + menu.navigate(1, 1); + EXPECT_EQ(menu.selected, input::StreamAction::none); +} + +/** + * @brief All wheel choices have visible labels and toggles describe their current state. + */ +TEST(StreamMenuTest, LabelsAllActionsAndToggleStates) { + for (int i = 0; i <= 8; ++i) { + EXPECT_STRNE(input::stream_action_label(static_cast(i), false, false), ""); + } + EXPECT_STREQ(input::stream_action_label(input::StreamAction::mouse, true, false), "Mouse: On"); + EXPECT_STREQ(input::stream_action_label(input::StreamAction::statistics, false, true), "Stats: On"); +} diff --git a/tests/unit/input/ui_input_test.cpp b/tests/unit/input/ui_input_test.cpp new file mode 100644 index 0000000..20b0cf1 --- /dev/null +++ b/tests/unit/input/ui_input_test.cpp @@ -0,0 +1,134 @@ +/** + * @file tests/unit/input/ui_input_test.cpp + * @brief Verifies keyboard navigation holds, input-source arbitration and repeat-safe commands. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/input/ui_input.h" + +/** + * @brief Keyboard navigation uses the same initial delay and interval as controller navigation. + */ +TEST(KeyboardNavigationTest, RepeatsHeldArrowsWithoutOperatingSystemEvents) { + input::KeyboardNavigation navigation; + EXPECT_EQ(navigation.poll(100), input::UiCommand::none); + EXPECT_EQ(navigation.press(1, input::UiCommand::move_down, 100), input::UiCommand::move_down); + EXPECT_EQ(navigation.poll(249), input::UiCommand::none); + EXPECT_EQ(navigation.poll(250), input::UiCommand::move_down); + EXPECT_EQ(navigation.poll(294), input::UiCommand::none); + EXPECT_EQ(navigation.poll(295), input::UiCommand::move_down); + EXPECT_EQ(navigation.poll(10000), input::UiCommand::move_down); + EXPECT_EQ(navigation.poll(10000), input::UiCommand::none); + navigation.release(1); + EXPECT_EQ(navigation.poll(11000), input::UiCommand::none); +} + +/** + * @brief Duplicate press events neither navigate twice nor postpone a held key's initial repeat. + */ +TEST(KeyboardNavigationTest, IgnoresDuplicatePressesAndUnknownKeys) { + input::KeyboardNavigation navigation; + EXPECT_EQ(navigation.press(1, input::UiCommand::move_left, 100), input::UiCommand::move_left); + EXPECT_EQ(navigation.press(1, input::UiCommand::move_left, 200), input::UiCommand::none); + EXPECT_EQ(navigation.press(2, input::UiCommand::none, 220), input::UiCommand::none); + EXPECT_EQ(navigation.poll(250), input::UiCommand::move_left); + navigation.release(400); + EXPECT_EQ(navigation.poll(295), input::UiCommand::move_left); +} + +/** + * @brief The most recently pressed direction wins, with a still-held earlier direction restored on release. + */ +TEST(KeyboardNavigationTest, ResolvesOverlappingNavigationKeysInPressOrder) { + input::KeyboardNavigation navigation; + navigation.press(20, input::UiCommand::move_down, 100); + EXPECT_EQ(navigation.press(10, input::UiCommand::move_up, 200), input::UiCommand::move_up); + EXPECT_EQ(navigation.poll(349), input::UiCommand::none); + EXPECT_EQ(navigation.poll(350), input::UiCommand::move_up); + navigation.release(10); + EXPECT_EQ(navigation.poll(351), input::UiCommand::move_down); + navigation.reset(); + EXPECT_EQ(navigation.poll(2000), input::UiCommand::none); +} + +/** + * @brief Confirmations and toggles fire once and stop an old held arrow from carrying into a new modal. + */ +TEST(KeyboardNavigationTest, NeverRepeatsActivationOrToggleCommands) { + for (const auto command : {input::UiCommand::activate, input::UiCommand::confirm, input::UiCommand::back, input::UiCommand::open_context_menu, input::UiCommand::toggle_overlay, input::UiCommand::delete_character}) { + input::KeyboardNavigation navigation; + navigation.press(1, input::UiCommand::move_down, 0); + EXPECT_EQ(navigation.press(2, command, 10), command); + EXPECT_EQ(navigation.press(2, command, 20), input::UiCommand::none); + EXPECT_EQ(navigation.poll(1000), input::UiCommand::none); + navigation.release(2); + EXPECT_EQ(navigation.poll(2000), input::UiCommand::none); + } +} + +/** + * @brief Paging keys repeat and the repeat clock remains correct across the SDL tick counter's wraparound. + */ +TEST(KeyboardNavigationTest, RepeatsPagingAcrossTickWraparound) { + for (const auto command : {input::UiCommand::move_right, input::UiCommand::previous_page, input::UiCommand::next_page, input::UiCommand::fast_previous_page, input::UiCommand::fast_next_page}) { + input::KeyboardNavigation navigation; + const auto start = std::numeric_limits::max() - 80; + EXPECT_EQ(navigation.press(1, command, start), command); + EXPECT_EQ(navigation.poll(start + 149), input::UiCommand::none); + EXPECT_EQ(navigation.poll(start + 150), command); + EXPECT_EQ(navigation.poll(start + 195), command); + } +} + +/** + * @brief Centered sticks, small drift and released triggers do not replace keyboard helper icons. + */ +TEST(UiInputActivityTest, RequiresDeliberateAnalogInput) { + for (short value : {0, 1, -1, 15999, -15999}) { + EXPECT_FALSE(input::is_gamepad_ui_activity(value, false)); + } + for (short value : {16000, -16000, 32767, -32768}) { + EXPECT_TRUE(input::is_gamepad_ui_activity(value, false)); + } + EXPECT_FALSE(input::is_gamepad_ui_activity(-32768, true)); + EXPECT_FALSE(input::is_gamepad_ui_activity(15999, true)); + EXPECT_TRUE(input::is_gamepad_ui_activity(16000, true)); + EXPECT_TRUE(input::is_gamepad_ui_activity(32767, true)); +} + +/** + * @brief Returning from a stream clears swallowed key releases, controller holds, repeat clocks and exit latches. + */ +TEST(ShellInputStateTest, ResumesNavigationAfterStreamingConsumesReleaseEvents) { + input::ShellInputState state; + state.keyboardNavigation.press(1, input::UiCommand::move_down, 100); + state.keyboardNavigation.press(2, input::UiCommand::confirm, 110); + state.leftTriggerPressed = state.rightTriggerPressed = true; + state.leftShoulderPressed = state.rightShoulderPressed = true; + state.controllerStartPressed = state.controllerBackPressed = true; + state.controllerExitComboArmed = state.controllerExitComboTriggered = true; + state.controllerStartDownTick = state.controllerBackDownTick = 120; + state.leftShoulderRepeatTick = state.rightShoulderRepeatTick = 120; + state.leftTriggerRepeatTick = state.rightTriggerRepeatTick = 120; + state.moveUpHoldState = state.moveDownHoldState = state.moveLeftHoldState = state.moveRightHoldState = {true, 100, 200}; + state.reset_after_stream(); + EXPECT_FALSE(state.leftTriggerPressed || state.rightTriggerPressed || state.leftShoulderPressed || state.rightShoulderPressed); + EXPECT_FALSE(state.controllerStartPressed || state.controllerBackPressed || state.controllerExitComboArmed || state.controllerExitComboTriggered); + EXPECT_EQ(state.controllerStartDownTick + state.controllerBackDownTick + state.leftShoulderRepeatTick + state.rightShoulderRepeatTick + state.leftTriggerRepeatTick + state.rightTriggerRepeatTick, 0); + for (const auto &direction : {state.moveUpHoldState, state.moveDownHoldState, state.moveLeftHoldState, state.moveRightHoldState}) { + EXPECT_FALSE(direction.active); + EXPECT_EQ(direction.activatedTick, 0); + EXPECT_EQ(direction.lastRepeatTick, 0); + } + EXPECT_TRUE(state.controllerNavigationNeutralRequired); + EXPECT_EQ(state.keyboardNavigation.poll(10000), input::UiCommand::none); + EXPECT_EQ(state.keyboardNavigation.press(1, input::UiCommand::move_down, 10000), input::UiCommand::move_down); + EXPECT_EQ(state.keyboardNavigation.poll(10150), input::UiCommand::move_down); + EXPECT_EQ(state.keyboardNavigation.press(2, input::UiCommand::confirm, 10200), input::UiCommand::confirm); +} diff --git a/tests/unit/input/usb_hid_test.cpp b/tests/unit/input/usb_hid_test.cpp new file mode 100644 index 0000000..7d7082b --- /dev/null +++ b/tests/unit/input/usb_hid_test.cpp @@ -0,0 +1,115 @@ +/** + * @file tests/unit/input/usb_hid_test.cpp + * @brief Verifies boot HID rollover, signed motion, truncation and independent device lifetimes. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/input/usb_hid.h" + +/** + * @brief Records decoded HID operations without USB hardware. + */ +class BootHidTest: public testing::Test { +protected: + std::vector events; ///< Decoded transitions. + input::BootHidInput hid {[this](const input::BootHidEvent &event) { + events.push_back(event); + }}; ///< Decoder under test. +}; + +/** + * @brief Modifiers precede ordinary keys and duplicate report slots create only one press. + */ +TEST_F(BootHidTest, DecodesKeyboardAndDisconnectReleases) { + hid.connect(1, 1); + const std::array report {0x22, 0, 4, 4, 5, 0, 0, 0}; + hid.report(1, report.data(), report.size()); + ASSERT_EQ(events.size(), 4); + EXPECT_EQ(events[0].code, 225); + EXPECT_EQ(events[1].code, 229); + EXPECT_EQ(events[2].code, 4); + EXPECT_EQ(events[3].code, 5); + hid.report(1, report.data(), report.size()); + EXPECT_EQ(events.size(), 4); + hid.disconnect(1); + ASSERT_EQ(events.size(), 8); + for (int i = 4; i < 8; ++i) { + EXPECT_FALSE(events[i].pressed); + } + hid.disconnect(1); + EXPECT_EQ(events.size(), 8); +} + +/** + * @brief Keyboard rollover retains held regular keys until a valid report resolves the state. + */ +TEST_F(BootHidTest, HandlesRolloverAndTruncatedReports) { + hid.connect(1, 1); + std::array report {0, 0, 4, 0, 0, 0, 0, 0}; + hid.report(1, report.data(), 8); + for (int error = 1; error <= 3; ++error) { + report[2] = error; + hid.report(1, report.data(), 8); + } + EXPECT_EQ(events.size(), 1); + hid.report(1, nullptr, 8); + hid.report(1, report.data(), 7); + hid.report(99, report.data(), 8); + EXPECT_EQ(events.size(), 1); + report = {}; + hid.report(1, report.data(), 8); + ASSERT_EQ(events.size(), 2); + EXPECT_FALSE(events.back().pressed); +} + +/** + * @brief Shared keys on two keyboards remain held until both keyboards release or disconnect. + */ +TEST_F(BootHidTest, MergesKeyboardSourcesAndReusedDeviceIds) { + hid.connect(1, 1); + hid.connect(2, 1); + const std::array report {0, 0, 4, 0, 0, 0, 0, 0}; + hid.report(1, report.data(), 8); + hid.report(2, report.data(), 8); + hid.disconnect(1); + EXPECT_EQ(events.size(), 1); + hid.connect(2, 2); + ASSERT_EQ(events.size(), 2); + EXPECT_FALSE(events.back().pressed); + hid.connect(3, 3); + hid.report(3, report.data(), 8); + EXPECT_EQ(events.size(), 2); +} + +/** + * @brief Boot mice decode signed extrema, merge buttons and ignore nonstandard extra bytes. + */ +TEST_F(BootHidTest, DecodesMouseWithoutGuessingReportLayouts) { + hid.connect(1, 2); + hid.connect(2, 2); + const std::array report {7, 128, 127, 99}; + hid.report(1, report.data(), 4); + ASSERT_EQ(events.size(), 4); + EXPECT_EQ(events[0].code, 1); + EXPECT_EQ(events[1].code, 2); + EXPECT_EQ(events[2].code, 3); + EXPECT_EQ(events[3].type, input::BootHidEventType::motion); + EXPECT_EQ(events[3].x, -128); + EXPECT_EQ(events[3].y, 127); + const std::array held {7, 0, 0}; + hid.report(2, held.data(), 3); + hid.report(1, held.data(), 2); + hid.disconnect(1); + EXPECT_EQ(events.size(), 4); + hid.clear(); + ASSERT_EQ(events.size(), 7); + EXPECT_FALSE(events.back().pressed); + hid.clear(); + EXPECT_EQ(events.size(), 7); +} diff --git a/tests/unit/startup/storage_paths_test.cpp b/tests/unit/startup/storage_paths_test.cpp new file mode 100644 index 0000000..e3663c3 --- /dev/null +++ b/tests/unit/startup/storage_paths_test.cpp @@ -0,0 +1,20 @@ +/** + * @file tests/unit/startup/storage_paths_test.cpp + * @brief Verifies configured screenshot folders and the default save-data subdirectory. + */ +// test includes +#include + +// local includes +#include "src/startup/storage_paths.h" + +/** + * @brief Default screenshots live together under save data; custom Xbox and host separators are preserved. + */ +TEST(StoragePathsTest, ResolvesScreenshotFoldersAndTrailingSeparators) { + EXPECT_EQ(startup::screenshot_storage_path("", "capture.bmp"), startup::default_storage_path("screenshots") + "/capture.bmp"); + EXPECT_EQ(startup::screenshot_storage_path("F:\\Screenshots", "capture.bmp"), "F:\\Screenshots\\capture.bmp"); + EXPECT_EQ(startup::screenshot_storage_path("F:\\Screenshots\\", "capture.bmp"), "F:\\Screenshots\\capture.bmp"); + EXPECT_EQ(startup::screenshot_storage_path("/tmp/screenshots", "capture.bmp"), "/tmp/screenshots/capture.bmp"); + EXPECT_EQ(startup::screenshot_storage_path("shots/", "capture.bmp"), "shots/capture.bmp"); +} diff --git a/tests/unit/streaming/video_overlay_test.cpp b/tests/unit/streaming/video_overlay_test.cpp index ea5ac28..6d56c46 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -6,13 +6,363 @@ #include // standard includes +#include #include #include #include +#include +#include // local includes #include "src/streaming/video_overlay.h" +/** + * @brief Opaque OSK colors are not brightened or made translucent by legacy SDL alpha rounding. + */ +TEST(VideoOverlayTest, KeepsTheOskOpaqueDespiteSdlAlphaRounding) { + const std::array pixel {100, 150, 200, 254}; + EXPECT_EQ(streaming::extract_overlay_bitmap(pixel.data(), pixel.size(), 4, 1, 1, true), (std::vector {100, 150, 200, 255})); +} + +/** + * @brief The same original SVG colors and translucent edges survive all Xbox RGB depths. + */ +TEST(VideoOverlayTest, PreservesSvgColorsAcrossRgbFramebuffers) { + streaming::VideoOverlay overlay; + const std::vector colors {0, 255, 0, 255, 0, 0, 255, 128, 255, 0, 0, 255, 255, 255, 255, 0}; + overlay.set_argb_bitmap(4, 1, colors); + const std::array depths {15, 16, 32}; + const std::array, 3> expectedColors {{ + {0x83e0U, 0xc00fU, 0x801fU, 0x801fU}, + {0x07e0U, 0x800fU, 0x001fU, 0x001fU}, + {0xff00ff00U, 0xff80007fU, 0xff0000ffU, 0xff0000ffU}, + }}; + for (std::size_t format = 0; format < depths.size(); ++format) { + const int bytes = depths[format] == 32 ? 4 : 2; + const int pitch = 24 * bytes + 5; + std::vector frame(pitch * 16, 0xcc); + for (int column = 0; column < 4; ++column) { + std::memcpy(frame.data() + 8 * pitch + (8 + column) * bytes, &expectedColors[format][3], bytes); + } + auto expected = frame; + for (int column = 0; column < 4; ++column) { + std::memcpy(expected.data() + 8 * pitch + (8 + column) * bytes, &expectedColors[format][column], bytes); + } + ASSERT_TRUE(overlay.draw_rgb(frame.data(), frame.size(), pitch, 24, 16, depths[format], {0, 0, 24, 16})); + EXPECT_EQ(frame, expected); + } + EXPECT_EQ(overlay.argb_pixels(), colors); +} + +/** + * @brief Colored GPU panels follow BT.601 and BT.709 and invalidate cached chroma when the matrix changes. + */ +TEST(VideoOverlayTest, EncodesSvgColorsUsingTheHostVideoMatrix) { + streaming::VideoOverlay overlay; + overlay.set_argb_bitmap(4, 1, {0, 0, 255, 255, 0, 0, 255, 255, 0, 255, 0, 255, 255, 0, 0, 255}); + constexpr int pitch = 24 * 2; + std::array frame; + frame.fill(70); + ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16}, streaming::VideoColorMatrix::bt601)); + const std::array bt601 {81, 90, 81, 240, 145, 147, 41, 72}; + EXPECT_TRUE(std::equal(bt601.begin(), bt601.end(), frame.begin() + 8 * pitch + 8 * 2)); + frame.fill(70); + ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16}, streaming::VideoColorMatrix::bt709)); + const std::array bt709 {63, 102, 63, 240, 173, 141, 32, 72}; + EXPECT_TRUE(std::equal(bt709.begin(), bt709.end(), frame.begin() + 8 * pitch + 8 * 2)); + const auto before = frame; + EXPECT_FALSE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16}, streaming::VideoColorMatrix::unsupported)); + EXPECT_EQ(frame, before); +} + +/** + * @brief Transparent colored pixels do not tint shared GPU chroma beside an opaque glyph. + */ +TEST(VideoOverlayTest, WeightsGpuChromaByVisibleColorCoverage) { + streaming::VideoOverlay overlay; + overlay.set_argb_bitmap(4, 1, {0, 0, 255, 255, 255, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 0}); + constexpr int pitch = 24 * 2; + std::array frame; + frame.fill(128); + const int at = 8 * pitch + 8 * 2; + frame[at] = frame[at + 2] = 70; + ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16})); + EXPECT_EQ(frame[at], 81); + EXPECT_EQ(frame[at + 1], 109); + EXPECT_EQ(frame[at + 2], 70); + EXPECT_EQ(frame[at + 3], 184); + EXPECT_EQ(frame[at + 4], 128); + EXPECT_EQ(frame[at + 5], 128); +} + +/** + * @brief Switching between opaque colored OSK pixels and grayscale statistics cannot reuse stale caches. + */ +TEST(VideoOverlayTest, SwitchesBetweenColorAndStatisticsAndRejectsInvalidArgb) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(2, 1, {64, 128}); + EXPECT_EQ(overlay.argb_pixels().size(), 8); + const std::vector colors {0, 0, 255, 255, 0, 255, 0, 255}; + overlay.set_argb_bitmap(2, 1, colors); + std::array frame {}; + ASSERT_TRUE(overlay.draw_rgb(frame.data(), frame.size(), 16 * 4, 16, 16, 32, {0, 0, 16, 16})); + EXPECT_TRUE(std::equal(colors.begin(), colors.end(), frame.begin() + (8 * 16 + 8) * 4)); + overlay.set_bitmap(1, 1, {64}); + std::uint32_t gray = 0; + std::memcpy(&gray, overlay.argb_pixels().data(), 4); + EXPECT_EQ(gray, 0xff404040U); + overlay.set_argb_bitmap(2, 1, {0}); + EXPECT_EQ(overlay.width(), 0); + EXPECT_TRUE(overlay.argb_pixels().empty()); + overlay.set_argb_bitmap(0, 1, {}); + EXPECT_EQ(overlay.height(), 0); + overlay.set_argb_bitmap(1, 0, {}); + EXPECT_EQ(overlay.width(), 0); + overlay.set_argb_bitmap(2049, 1, {}); + EXPECT_EQ(overlay.width(), 0); + overlay.set_argb_bitmap(1, 2049, {}); + EXPECT_EQ(overlay.height(), 0); +} + +/** + * @brief Extracted text keeps its opacity without darkening antialiased edges twice. + */ +TEST(VideoOverlayTest, ExtractsOriginalColorsFromTransparentArgb) { + const std::array surface { + 64, + 64, + 64, + 128, + 255, + 255, + 255, + 0, + 1, + 2, + 3, + 4, + 0, + 0, + 255, + 255, + 128, + 128, + 128, + 128, + 5, + 6, + 7, + 8, + }; + const auto bitmap = streaming::extract_overlay_bitmap(surface.data(), surface.size(), 12, 2, 2); + EXPECT_EQ(bitmap, (std::vector {128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 128})); + EXPECT_TRUE(streaming::extract_overlay_bitmap(nullptr, surface.size(), 12, 2, 2).empty()); + EXPECT_TRUE(streaming::extract_overlay_bitmap(surface.data(), 1, 12, 2, 2).empty()); + std::array oversized {}; + EXPECT_TRUE(streaming::extract_overlay_bitmap(oversized.data(), oversized.size(), 2049 * 4, 2049, 1).empty()); + EXPECT_TRUE(streaming::extract_overlay_bitmap(oversized.data(), oversized.size(), 4, 1, 2049).empty()); +} + +/** + * @brief Transparent areas preserve the host framebuffer while text blends in every Xbox RGB format. + */ +TEST(VideoOverlayTest, AlphaBlendsRgbAndPreservesUncoveredStorage) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(4, 1, {255, 255, 64, 32}, streaming::VideoOverlayPlacement::top_left, {0, 128, 255, 255}); + const std::array depths {15, 16, 32}; + const std::array, 3> colors {{ + {0xc210U, 0xe318U, 0xa108U, 0x9084U}, + {0x8410U, 0xc618U, 0x4208U, 0x2104U}, + {0x77102030U, 0xff889098U, 0xff404040U, 0xff202020U}, + }}; + for (std::size_t format = 0; format < depths.size(); ++format) { + const int bytes = depths[format] == 32 ? 4 : 2; + const int pitch = 24 * bytes + 7; + std::vector frame(pitch * 16, 0xcc); + for (int pixel = 0; pixel < 4; ++pixel) { + std::memcpy(frame.data() + 8 * pitch + (8 + pixel) * bytes, &colors[format][0], bytes); + } + auto expected = frame; + for (int pixel = 0; pixel < 4; ++pixel) { + std::memcpy(expected.data() + 8 * pitch + (8 + pixel) * bytes, &colors[format][pixel], bytes); + } + ASSERT_TRUE(overlay.draw_rgb(frame.data(), frame.size(), pitch, 24, 16, depths[format], {0, 0, 24, 16})); + EXPECT_EQ(frame, expected); + } + const auto &argb = overlay.argb_pixels(); + std::uint32_t transparent = 0; + std::uint32_t half = 0; + std::memcpy(&transparent, argb.data(), 4); + std::memcpy(&half, argb.data() + 4, 4); + EXPECT_EQ(transparent, 0x00ffffffU); + EXPECT_EQ(half, 0x80ffffffU); +} + +/** + * @brief GPU composition preserves transparent luma and averages coverage for shared YUY2 chroma. + */ +TEST(VideoOverlayTest, AlphaBlendsYuy2WithoutBakingTheVideoIntoItsCache) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(6, 1, {255, 255, 64, 192, 255, 255}, streaming::VideoOverlayPlacement::top_left, {0, 128, 255, 255, 0, 0}); + constexpr int pitch = 24 * 2 + 4; + std::array frame; + frame.fill(0xcc); + for (int row = 0; row < 16; ++row) { + for (int column = 0; column < 24; column += 2) { + const int at = row * pitch + column * 2; + frame[at] = frame[at + 2] = 50; + frame[at + 1] = 90; + frame[at + 3] = 170; + } + } + auto expected = frame; + const std::array composite {50, 100, 143, 159, 71, 128, 181, 128}; + std::copy(composite.begin(), composite.end(), expected.begin() + 8 * pitch + 8 * 2); + ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16})); + EXPECT_EQ(frame, expected); + + // A new host frame must supply the background, even with the same cached UI. + frame.fill(70); + ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16})); + EXPECT_EQ(frame[8 * pitch + 8 * 2], 70); + EXPECT_EQ(frame[8 * pitch + 9 * 2], 153); + EXPECT_EQ(frame[8 * pitch + 12 * 2], 70); +} + +/** + * @brief GPU upscaling retains transparent coverage instead of filling the whole overlay rectangle. + */ +TEST(VideoOverlayTest, ScalesOpacityAlongsideGpuColors) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(2, 1, {255, 255}, streaming::VideoOverlayPlacement::bottom_center, {0, 255}); + std::vector frame(64 * 64 * 2, 40); + ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), 128, 64, 64, {0, 0, 32, 32})); + EXPECT_EQ(frame[46 * 128 + 30 * 2], 40); + EXPECT_EQ(frame[46 * 128 + 31 * 2], 40); + EXPECT_EQ(frame[46 * 128 + 32 * 2], 235); + EXPECT_EQ(frame[46 * 128 + 32 * 2 + 1], 128); + EXPECT_EQ(frame[45 * 128 + 32 * 2], 40); +} + +/** + * @brief Malformed coverage clears converted caches, and subsequent opaque statistics remain opaque. + */ +TEST(VideoOverlayTest, RejectsInvalidOpacityAndRestoresOpaqueStatistics) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(2, 1, {255, 255}, streaming::VideoOverlayPlacement::top_left, {0, 128}); + EXPECT_EQ(overlay.argb_pixels().size(), 8); + overlay.set_bitmap(2, 1, {255, 255}, streaming::VideoOverlayPlacement::top_left, {0}); + EXPECT_EQ(overlay.width(), 0); + EXPECT_TRUE(overlay.argb_pixels().empty()); + overlay.set_bitmap(1, 1, {64}); + std::uint32_t pixel = 0; + std::memcpy(&pixel, overlay.argb_pixels().data(), 4); + EXPECT_EQ(pixel, 0xff404040U); +} + +/** + * @brief Direct wheel painting highlights all sectors without touching the hole or row padding. + */ +TEST(VideoOverlayTest, RasterizesWheelWithoutSoftwareRenderCommands) { + constexpr int pitch = 20 * 4 + 4; + std::array pixels; + const std::array x {10, 15, 10, 5}; + const std::array y {5, 10, 15, 10}; + for (int selected = 0; selected < 4; ++selected) { + pixels.fill(0xcc); + ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), pitch, 20, 20, 10, 10, 6, selected)); + for (int sector = 0; sector < 4; ++sector) { + EXPECT_EQ(pixels[y[sector] * pitch + x[sector] * 4], sector == selected ? 85 : 36); + EXPECT_EQ(pixels[y[sector] * pitch + x[sector] * 4 + 3], 255); + } + EXPECT_EQ(pixels[6 * pitch + 14 * 4], 110); + EXPECT_EQ(pixels[10 * pitch + 10 * 4], 0xcc); + EXPECT_EQ(pixels[0], 0xcc); + EXPECT_EQ(pixels[10 * pitch + 20 * 4], 0xcc); + } + ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), pitch, 20, 20, 0, 0, 4, -1)); + EXPECT_EQ(pixels[3 * 4], 36); +} + +/** + * @brief Invalid wheel storage and geometry are rejected before a pixel is written. + */ +TEST(VideoOverlayTest, RejectsInvalidWheelRasterStorage) { + std::array pixels; + pixels.fill(0xcc); + EXPECT_FALSE(streaming::rasterize_menu_wheel(nullptr, pixels.size(), 80, 20, 20, 10, 10, 6, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), 1, 80, 20, 20, 10, 10, 6, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, -1, 10, 6, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 20, 10, 6, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, -1, 6, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, 20, 6, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, 10, 0, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, 10, 2049, 0)); + std::array oversized {}; + EXPECT_FALSE(streaming::rasterize_menu_wheel(oversized.data(), oversized.size(), 2049 * 4, 2049, 1, 0, 0, 1, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(oversized.data(), oversized.size(), 4, 1, 2049, 0, 0, 1, 0)); + EXPECT_TRUE(std::all_of(pixels.begin(), pixels.end(), [](std::uint8_t value) { + return value == 0xcc; + })); +} + +/** + * @brief Crop wrapped labels on every edge without changing the renderer clip state. + */ +TEST(VideoOverlayTest, CropsOversizedCenteredLabels) { + const auto layout = streaming::layout_overlay_label(100, 90, {10, 20, 50, 30}, false); + EXPECT_EQ(layout.source.x, 25); + EXPECT_EQ(layout.source.y, 30); + EXPECT_EQ(layout.source.width, 50); + EXPECT_EQ(layout.source.height, 30); + EXPECT_EQ(layout.destination.x, 10); + EXPECT_EQ(layout.destination.y, 20); + EXPECT_EQ(layout.destination.width, 50); + EXPECT_EQ(layout.destination.height, 30); + + const auto small = streaming::layout_overlay_label(40, 12, {10, 20, 50, 30}, false); + EXPECT_EQ(small.source.x, 0); + EXPECT_EQ(small.source.y, 0); + EXPECT_EQ(small.destination.x, 15); + EXPECT_EQ(small.destination.y, 29); + EXPECT_EQ(small.destination.width, 40); + EXPECT_EQ(small.destination.height, 12); +} + +/** + * @brief Single-line key labels shrink within their cells while retaining the complete source. + */ +TEST(VideoOverlayTest, FitsSingleLineKeyLabelsWithoutCropping) { + const auto layout = streaming::layout_overlay_label(100, 50, {10, 20, 50, 30}, true); + EXPECT_EQ(layout.source.x, 0); + EXPECT_EQ(layout.source.y, 0); + EXPECT_EQ(layout.source.width, 100); + EXPECT_EQ(layout.source.height, 50); + EXPECT_EQ(layout.destination.x, 11); + EXPECT_EQ(layout.destination.y, 23); + EXPECT_EQ(layout.destination.width, 48); + EXPECT_EQ(layout.destination.height, 24); + + const auto small = streaming::layout_overlay_label(12, 8, {0, 0, 50, 30}, true); + EXPECT_EQ(small.destination.width, 12); + EXPECT_EQ(small.destination.height, 8); + const auto tall = streaming::layout_overlay_label(20, 100, {0, 0, 50, 30}, true); + EXPECT_EQ(tall.destination.height, 28); + const auto tiny = streaming::layout_overlay_label(100, 100, {0, 0, 1, 1}, true); + EXPECT_EQ(tiny.destination.width, 1); + EXPECT_EQ(tiny.destination.height, 1); +} + +/** + * @brief Missing text and empty label bounds never generate a copy rectangle. + */ +TEST(VideoOverlayTest, SkipsEmptyOverlayLabels) { + EXPECT_EQ(streaming::layout_overlay_label(0, 10, {0, 0, 20, 20}, false).destination.width, 0); + EXPECT_EQ(streaming::layout_overlay_label(10, -1, {0, 0, 20, 20}, true).destination.height, 0); + EXPECT_EQ(streaming::layout_overlay_label(10, 10, {0, 0, 0, 20}, false).source.width, 0); + EXPECT_EQ(streaming::layout_overlay_label(10, 10, {0, 0, 20, -1}, true).source.height, 0); +} + /** * @brief YUY2 drawing leaves padding untouched and uses limited-range neutral grayscale. */ @@ -32,6 +382,86 @@ TEST(VideoOverlayTest, CopiesLimitedRangePanelIntoPackedVideo) { EXPECT_EQ(output[8 * 40 + 39], 0xcc); } +/** + * @brief A full menu fits a letterboxed stream on both GPU and software paths without clipping its footer. + */ +TEST(VideoOverlayTest, FitsFullMenuInsideLetterboxedVideo) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(624, 344, std::vector(624 * 344, 128)); + std::vector packed(320 * 180 * 2, 0xcc); + ASSERT_TRUE(overlay.draw_yuy2(packed.data(), packed.size(), 640, 320, 180, {0, 60, 640, 360})); + EXPECT_EQ(packed[4 * 640 + 4 * 2], 126); + EXPECT_EQ(packed[175 * 640 + 315 * 2], 126); + EXPECT_EQ(packed[175 * 640 + 315 * 2 + 1], 128); + EXPECT_EQ(packed[176 * 640 + 315 * 2], 0xcc); + EXPECT_EQ(packed[175 * 640 + 316 * 2], 0xcc); + + std::vector rgb(640 * 480 * 4, 0xcc); + ASSERT_TRUE(overlay.draw_rgb(rgb.data(), rgb.size(), 640 * 4, 640, 480, 32, {0, 60, 640, 360})); + std::uint32_t first = 0; + std::uint32_t last = 0; + std::memcpy(&first, rgb.data() + (68 * 640 + 8) * 4, 4); + std::memcpy(&last, rgb.data() + (411 * 640 + 631) * 4, 4); + EXPECT_EQ(first, 0xff808080U); + EXPECT_EQ(last, first); + EXPECT_EQ(rgb[(412 * 640 + 631) * 4], 0xcc); + EXPECT_EQ(rgb[(411 * 640 + 632) * 4], 0xcc); +} + +/** + * @brief A docked keyboard occupies identical bottom-center bounds in GPU and software video. + */ +TEST(VideoOverlayTest, DocksKeyboardWithoutCoveringVideoAboveIt) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(16, 4, std::vector(64, 255), streaming::VideoOverlayPlacement::bottom_center); + const auto bounds = overlay.rectangle({4, 6, 40, 32}); + EXPECT_EQ(bounds.x, 16); + EXPECT_EQ(bounds.y, 26); + EXPECT_EQ(bounds.width, 16); + EXPECT_EQ(bounds.height, 4); + std::vector rgb(48 * 48 * 4, 0xcc); + ASSERT_TRUE(overlay.draw_rgb(rgb.data(), rgb.size(), 48 * 4, 48, 48, 32, {4, 6, 40, 32})); + EXPECT_EQ(rgb[(26 * 48 + 16) * 4], 255); + EXPECT_EQ(rgb[(29 * 48 + 31) * 4], 255); + EXPECT_EQ(rgb[(25 * 48 + 16) * 4], 0xcc); + EXPECT_EQ(rgb[(30 * 48 + 16) * 4], 0xcc); + + std::vector yuy2(40 * 32 * 2, 0xcc); + ASSERT_TRUE(overlay.draw_yuy2(yuy2.data(), yuy2.size(), 80, 40, 32, {4, 6, 40, 32})); + EXPECT_EQ(yuy2[20 * 80 + 12 * 2], 235); + EXPECT_EQ(yuy2[23 * 80 + 27 * 2], 235); + EXPECT_EQ(yuy2[19 * 80 + 12 * 2], 0xcc); + EXPECT_EQ(yuy2[24 * 80 + 12 * 2], 0xcc); + + // Changing the anchor must invalidate the cached GPU panel even at the same resolution. + overlay.set_bitmap(16, 4, std::vector(64, 255)); + std::fill(yuy2.begin(), yuy2.end(), 0xcc); + ASSERT_TRUE(overlay.draw_yuy2(yuy2.data(), yuy2.size(), 80, 40, 32, {4, 6, 40, 32})); + EXPECT_EQ(yuy2[8 * 80 + 8 * 2], 235); + EXPECT_EQ(yuy2[20 * 80 + 12 * 2], 0xcc); +} + +/** + * @brief Small or empty presentation areas never produce negative keyboard panel copies. + */ +TEST(VideoOverlayTest, ClipsDockedPanelsAndSkipsTinyAreas) { + streaming::VideoOverlay overlay; + overlay.set_bitmap(30, 20, std::vector(600, 255), streaming::VideoOverlayPlacement::bottom_center); + const auto bounds = overlay.rectangle({0, 0, 32, 24}); + EXPECT_EQ(bounds.x, 8); + EXPECT_EQ(bounds.y, 8); + EXPECT_EQ(bounds.width, 16); + EXPECT_EQ(bounds.height, 8); + EXPECT_EQ(overlay.rectangle({0, 0, 16, 16}).width, 0); + std::array pixels {}; + EXPECT_TRUE(overlay.draw_yuy2(pixels.data(), pixels.size(), 32, 16, 16, {0, 0, 16, 16})); + EXPECT_TRUE(overlay.draw_yuy2(pixels.data(), pixels.size(), 32, 16, 16, {0, 0, 16, 16})); + EXPECT_TRUE(overlay.draw_rgb(pixels.data(), pixels.size(), 32, 16, 16, 16, {0, 0, 16, 16})); + for (auto byte : pixels) { + EXPECT_EQ(byte, 0); + } +} + /** * @brief Software output uses the same grayscale panel in every Xbox framebuffer format. */ @@ -113,3 +543,57 @@ TEST(VideoOverlayTest, RejectsTruncatedStorageAndClearsInvalidBitmaps) { EXPECT_EQ(overlay.width(), 0); EXPECT_TRUE(overlay.draw_rgb(nullptr, 0, 0, 0, 0, 0, {})); } + +/** + * @brief Edge-docked keyboards and full-screen dimming use identical flush bounds for GPU and software paths. + */ +TEST(VideoOverlayTest, DocksToEdgesWithoutTheLegacyInset) { + streaming::VideoOverlay overlay; + const streaming::VideoRectangle target {10, 20, 100, 80}; + for (const auto placement : {streaming::VideoOverlayPlacement::dock_top, streaming::VideoOverlayPlacement::dock_bottom}) { + overlay.set_argb_bitmap(40, 20, std::vector(40 * 20 * 4, 255), placement); + const auto bounds = overlay.rectangle(target); + EXPECT_EQ(bounds.x, 40); + EXPECT_EQ(bounds.y, placement == streaming::VideoOverlayPlacement::dock_top ? 20 : 80); + EXPECT_EQ(bounds.width, 40); + EXPECT_EQ(bounds.height, 20); + } + overlay.set_argb_bitmap(200, 200, std::vector(200 * 200 * 4, 150), streaming::VideoOverlayPlacement::full_screen); + const auto bounds = overlay.rectangle(target); + EXPECT_EQ(bounds.x, 10); + EXPECT_EQ(bounds.y, 20); + EXPECT_EQ(bounds.width, 100); + EXPECT_EQ(bounds.height, 80); +} + +/** + * @brief A translucent black backdrop dims live RGB and YUY2 frames instead of replacing the stream. + */ +TEST(VideoOverlayTest, DimsTheStreamThroughBothPresentationPaths) { + streaming::VideoOverlay overlay; + std::vector backdrop(16 * 16 * 4, 0); + std::vector rgb(backdrop.size()); + auto expected = rgb; + for (std::size_t i = 0; i < backdrop.size(); i += 4) { + backdrop[i + 3] = 128; + const std::array source {80, 100, 120, 255}; + const std::array dimmed {40, 50, 60, 255}; + std::copy(source.begin(), source.end(), rgb.begin() + i); + std::copy(dimmed.begin(), dimmed.end(), expected.begin() + i); + } + overlay.set_argb_bitmap(16, 16, std::move(backdrop), streaming::VideoOverlayPlacement::full_screen); + ASSERT_TRUE(overlay.draw_rgb(rgb.data(), rgb.size(), 64, 16, 16, 32, {0, 0, 16, 16})); + EXPECT_EQ(rgb, expected); + for (const auto matrix : {streaming::VideoColorMatrix::bt601, streaming::VideoColorMatrix::bt709}) { + std::vector yuy2(16 * 16 * 2); + expected.resize(yuy2.size()); + for (std::size_t i = 0; i < yuy2.size(); i += 4) { + const std::array source {100, 90, 150, 170}; + const std::array dimmed {58, 109, 83, 149}; + std::copy(source.begin(), source.end(), yuy2.begin() + i); + std::copy(dimmed.begin(), dimmed.end(), expected.begin() + i); + } + ASSERT_TRUE(overlay.draw_yuy2(yuy2.data(), yuy2.size(), 32, 16, 16, {0, 0, 16, 16}, matrix)); + EXPECT_EQ(yuy2, expected); + } +} diff --git a/tests/unit/ui/asset_path_test.cpp b/tests/unit/ui/asset_path_test.cpp new file mode 100644 index 0000000..2df6f81 --- /dev/null +++ b/tests/unit/ui/asset_path_test.cpp @@ -0,0 +1,19 @@ +/** + * @file tests/unit/ui/asset_path_test.cpp + * @brief Verifies that shell and streaming assets use valid Xbox SDL paths. + */ +// test includes +#include + +// local includes +#include "src/ui/asset_path.h" + +/** + * @brief Existing shell paths and portable menu paths resolve to the same Xbox file. + */ +TEST(AssetPathTest, NormalizesShellAndStreamingSeparators) { + EXPECT_EQ(ui::build_asset_path("icons/keyboard.svg"), "D:\\assets\\icons\\keyboard.svg"); + EXPECT_EQ(ui::build_asset_path("icons\\keyboard.svg"), ui::build_asset_path("icons/keyboard.svg")); + EXPECT_EQ(ui::build_asset_path("icons/subdirectory\\button.svg"), "D:\\assets\\icons\\subdirectory\\button.svg"); + EXPECT_EQ(ui::build_asset_path("fonts\\vegur-regular.ttf"), "D:\\assets\\fonts\\vegur-regular.ttf"); +} diff --git a/tests/unit/ui/input_hints_test.cpp b/tests/unit/ui/input_hints_test.cpp new file mode 100644 index 0000000..e57a34b --- /dev/null +++ b/tests/unit/ui/input_hints_test.cpp @@ -0,0 +1,103 @@ +/** + * @file tests/unit/ui/input_hints_test.cpp + * @brief Verifies keyboard keycaps throughout the UI and restoration of controller glyphs. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/ui/shell_view.h" + +/** + * @brief Main-page helpers follow keyboard input and restore the original glyphs after gamepad input. + */ +TEST(InputHintsTest, SwitchesMainPageHelpersBetweenKeyboardAndGamepad) { + auto state = app::create_initial_state(); + state.shell.inputDevice = input::UiInputDevice::keyboard; + auto view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.frame.footerActions.size(), 2); + EXPECT_EQ(view.frame.footerActions[0].keyLabel, "Enter"); + EXPECT_EQ(view.frame.footerActions[1].keyLabel, "Alt+F4"); + for (const auto &action : view.frame.footerActions) { + EXPECT_TRUE(action.iconAssetPath.empty()); + EXPECT_TRUE(action.secondaryIconAssetPath.empty()); + } + state.shell.inputDevice = input::UiInputDevice::gamepad; + view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.frame.footerActions[0].iconAssetPath, "icons\\button-a.svg"); + EXPECT_EQ(view.frame.footerActions[1].iconAssetPath, "icons\\button-select.svg"); + EXPECT_EQ(view.frame.footerActions[1].secondaryIconAssetPath, "icons\\button-start.svg"); + EXPECT_TRUE(view.frame.footerActions[0].keyLabel.empty()); +} + +/** + * @brief The numeric keypad distinguishes Space for typing a selected key from Enter for accepting the field. + */ +TEST(InputHintsTest, UsesWorkingKeyboardBindingsForTheAddressKeypad) { + auto state = app::create_initial_state(); + state.shell.inputDevice = input::UiInputDevice::keyboard; + state.shell.activeScreen = app::ScreenId::add_host; + state.addHostDraft.keypad.visible = true; + state.addHostDraft.activeField = app::AddHostField::address; + const auto view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.keypad.footerActions.size(), 5); + EXPECT_EQ(view.keypad.footerActions[0].keyLabel, "Space"); + EXPECT_EQ(view.keypad.footerActions[1].keyLabel, "Bksp"); + EXPECT_EQ(view.keypad.footerActions[2].keyLabel, "Esc"); + EXPECT_EQ(view.keypad.footerActions[3].keyLabel, "PgUp"); + EXPECT_EQ(view.keypad.footerActions[3].secondaryKeyLabel, "PgDn"); + EXPECT_EQ(view.keypad.footerActions[4].keyLabel, "Enter"); + EXPECT_TRUE(view.frame.footerActions.empty()); + for (const auto &action : view.keypad.footerActions) { + EXPECT_TRUE(action.iconAssetPath.empty()); + EXPECT_TRUE(action.secondaryIconAssetPath.empty()); + } +} + +/** + * @brief Alternate controller buttons for the same keyboard action collapse to one keycap. + */ +TEST(InputHintsTest, UsesKeyboardHintsInSupportAndDefaultDialogs) { + auto state = app::create_initial_state(); + state.shell.inputDevice = input::UiInputDevice::keyboard; + state.modal.id = app::ModalId::support; + auto view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.footerActions.size(), 2); + EXPECT_EQ(view.modal.footerActions[0].keyLabel, "Enter"); + EXPECT_TRUE(view.modal.footerActions[0].secondaryKeyLabel.empty()); + EXPECT_TRUE(view.modal.footerActions[0].secondaryIconAssetPath.empty()); + EXPECT_EQ(view.modal.footerActions[1].keyLabel, "Esc"); + state.modal.id = app::ModalId::connection_test; + state.connectionTest.inProgress = false; + view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.footerActions.size(), 1); + EXPECT_EQ(view.modal.footerActions[0].keyLabel, "Esc"); + state.connectionTest.inProgress = true; + EXPECT_TRUE(ui::build_shell_view_model(state, {}).modal.footerActions.empty()); +} + +/** + * @brief Log viewer paging, fast paging, pane movement and close hints all use available keyboard bindings. + */ +TEST(InputHintsTest, LabelsLogViewerShortcutsAndNotificationDismissal) { + auto state = app::create_initial_state(); + state.shell.inputDevice = input::UiInputDevice::keyboard; + state.modal.id = app::ModalId::log_viewer; + const auto view = ui::build_shell_view_model(state, {}); + const std::array expected {"PgUp", "PgDn", "Shift+PgUp", "Shift+PgDn", "Bksp", "Esc"}; + ASSERT_EQ(view.modal.footerActions.size(), expected.size()); + for (std::size_t i = 0; i < expected.size(); ++i) { + EXPECT_EQ(view.modal.footerActions[i].keyLabel, expected[i]); + EXPECT_TRUE(view.modal.footerActions[i].iconAssetPath.empty()); + } + state.modal.id = app::ModalId::none; + app::show_notification(state, app::NotificationKind::error, "Storage", "Free some space."); + const auto notificationView = ui::build_shell_view_model(state, {}); + ASSERT_FALSE(notificationView.notification.items.empty()); + ASSERT_FALSE(notificationView.notification.items.back().actions.empty()); + EXPECT_EQ(notificationView.notification.items.back().actions[0].keyLabel, "Bksp"); +} diff --git a/xbe/assets/README.md b/xbe/assets/README.md index 2685e6d..4c7a85d 100644 --- a/xbe/assets/README.md +++ b/xbe/assets/README.md @@ -15,3 +15,13 @@ crescent phases indicate online paired, online unpaired and offline PCs respecti rendered separately so selection remains independent of reachability. The Moonlight logo and controller button glyphs are separate bundled assets. +The first-party left/right stick glyphs are also used by the stream wheels and on-screen keyboard. +The shell and stream controls share the same asset-path resolver and SVG rasterizer. Bundled paths +use Xbox backslashes throughout because the Xbox SDL file loader rejects forward slashes. + +Keyboard helper keycaps are drawn with SDL primitives and the bundled UI font rather than SVG text. +Their measured widths adapt to labels such as Enter and Shift+PgUp, and crowded modal helpers wrap +onto additional rows. The active input device chooses between these keycaps and the controller glyphs. + +Stream controls reuse the original colored controller button assets and SVG rasterizer. The stick, +Guide and Share symbols are local vector assets; Lucide supplies the other stream action symbols. diff --git a/xbe/assets/icons/button-guide.svg b/xbe/assets/icons/button-guide.svg new file mode 100644 index 0000000..6a25ef5 --- /dev/null +++ b/xbe/assets/icons/button-guide.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/xbe/assets/icons/button-left-stick.svg b/xbe/assets/icons/button-left-stick.svg new file mode 100644 index 0000000..ddab33a --- /dev/null +++ b/xbe/assets/icons/button-left-stick.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/xbe/assets/icons/button-right-stick.svg b/xbe/assets/icons/button-right-stick.svg new file mode 100644 index 0000000..c1856cf --- /dev/null +++ b/xbe/assets/icons/button-right-stick.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/xbe/assets/icons/button-share.svg b/xbe/assets/icons/button-share.svg new file mode 100644 index 0000000..07b97fb --- /dev/null +++ b/xbe/assets/icons/button-share.svg @@ -0,0 +1 @@ + \ No newline at end of file From bd13571c8b65c4735364dbaea8feee0e44b85e9f Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:23:34 -0500 Subject: [PATCH 02/22] fix(streaming): refine controls and improve input pacing --- README.md | 13 +- src/input/stream_input.cpp | 18 +- src/input/stream_input.h | 3 +- src/input/stream_menu.cpp | 35 ++- src/input/stream_menu.h | 13 +- src/input/ui_input.cpp | 23 ++ src/input/ui_input.h | 24 ++ src/streaming/ffmpeg_stream_backend.cpp | 8 +- src/streaming/ffmpeg_stream_backend.h | 2 +- src/streaming/stream_controls.cpp | 237 +++++++++++++------- src/streaming/stream_controls.h | 46 +++- src/streaming/video_overlay.cpp | 79 ++++++- src/streaming/video_overlay.h | 3 +- tests/unit/input/stream_input_test.cpp | 51 +++++ tests/unit/input/stream_menu_test.cpp | 46 +++- tests/unit/input/ui_input_test.cpp | 32 +++ tests/unit/streaming/video_overlay_test.cpp | 81 +++++++ xbe/assets/README.md | 6 +- xbe/assets/icons/button-guide.svg | 2 +- xbe/assets/licenses/simple-icons.txt | 30 +++ 20 files changed, 631 insertions(+), 121 deletions(-) create mode 100644 xbe/assets/licenses/simple-icons.txt diff --git a/README.md b/README.md index 28153f8..50652d8 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,8 @@ for readability. Guide and Share use Xbox Series-style symbols. | Left | Keyboard | Mouse mode | View logs | Quit stream | | Right | Guide | Share | Screenshot | Enable / disable stats | -**Quit stream** asks for confirmation before disconnecting. The client displays **Stopping stream...**, +**Quit stream** opens a confirmation dialog: **A / Enter** disconnects and **B / Esc** cancels. +The icons sit on their buttons; arrow navigation is not required. The client displays **Stopping stream...**, then a stream summary. Release the buttons, then press a button to return to the library. The host application keeps running. @@ -114,7 +115,9 @@ Alt+Tab, Alt+F4, Ctrl+Alt+Delete, Win+D, Win+E and Win+R. Focus a shortcut to se above the keys. Named shortcuts send their own modifier combination. **RT** moves the panel between the top and bottom edges; the placement is saved when the stream ends. **B** returns to the wheels. Leaving the keyboard clears held modifiers. Keys use the host's US keyboard layout. -Controller shortcut icons retain the same colors as the main menus, with smaller glyphs inside keys. +Controller shortcut icons retain the same colors as the main menus. Caps and Enter use larger side-key +glyphs; the four footer helpers (Select, Back, Shift and Move) share one row. Held controller navigation +uses the same 150 ms initial delay and 45 ms repeat interval as keyboard navigation. When using a physical keyboard, use **Tab** to change page, **Shift** for one-shot Shift, **Caps Lock** to toggle Caps, **F2** to move the panel, **Space / Backspace** to send those keys, and **Esc** to return. In the numeric address/port keypad, **Space** types the focused key and **Enter** accepts the field; @@ -122,7 +125,8 @@ In the numeric address/port keypad, **Space** types the focused key and **Enter* Stream logs appear in a live side panel with severity colors and a scrollbar. Scroll upward to read older entries; return to the bottom to follow new entries. The disconnect dialog uses the same modal -surface as the main menus, starts on **Cancel**, and leaves the host app running after disconnecting. +surface as the main menus, accepts **A / Enter** to disconnect and **B / Esc** to cancel, and leaves +the host app running after disconnecting. Returning from the stream resets local keyboard/controller holds before library navigation resumes. Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Change @@ -132,7 +136,8 @@ Use a TOML literal string for Xbox paths, for example `screenshot_directory = 'F The same section stores `keyboard_dock_top = false` (bottom) or `true` (top). Mouse mode uses the invoking controller's **left stick** for movement and **A / B** for left / right -clicks. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; +clicks. Stick-driven mouse movement is reported at up to 125 Hz, while button presses and releases +are sent immediately. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; other gamepads continue to work. Physical keyboard and mouse input can be used alongside virtual input. Up to four gamepads are sent as separate controllers to Sunshine. Disconnecting a controller releases diff --git a/src/input/stream_input.cpp b/src/input/stream_input.cpp index 428cca7..5de79a8 100644 --- a/src/input/stream_input.cpp +++ b/src/input/stream_input.cpp @@ -131,6 +131,7 @@ namespace input { const int previous = mouseDevice_; mouseDevice_ = device; mouseX_ = mouseY_ = 0; + mouseElapsed_ = 0; for (int i = 0; i < 4; ++i) { if (slots_[i].device != -1 && (slots_[i].device == previous || slots_[i].device == device)) { if (!equal(slots_[i].last, {})) { @@ -145,6 +146,12 @@ namespace input { if (captured_ || mouseDevice_ < 0) { return; } + mouseElapsed_ = std::min(50U, mouseElapsed_ + std::min(milliseconds, 50U)); + if (mouseElapsed_ < 8) { + return; + } + const double seconds = mouseElapsed_ / 1000.0; + mouseElapsed_ = 0; for (const auto &slot : slots_) { if (slot.device != mouseDevice_ || slot.blocked) { continue; @@ -152,7 +159,6 @@ namespace input { const auto &state = slot.sampled; mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_LEFT, (state.buttonFlags & A_FLAG) != 0); mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_RIGHT, (state.buttonFlags & B_FLAG) != 0); - const double seconds = std::min(milliseconds, 50U) / 1000.0; mouseX_ += mouse_axis(state.leftStickX) * 900 * seconds; mouseY_ -= mouse_axis(state.leftStickY) * 900 * seconds; const int x = static_cast(mouseX_); @@ -167,7 +173,14 @@ namespace input { if (code <= 0 || (captured_ && pressed)) { return; } - auto &sources = held[code]; + auto entry = held.find(code); + if (entry == held.end()) { + if (!pressed) { + return; + } + entry = held.emplace(code, std::set {}).first; + } + auto &sources = entry->second; const bool wasPressed = !sources.empty(); if (pressed) { sources.insert(source); @@ -228,6 +241,7 @@ namespace input { } void StreamInput::release_all() { + mouseElapsed_ = 0; for (int i = 0; i < 4; ++i) { if (slots_[i].device != -1 && !equal(slots_[i].last, {})) { send_controller(i, {}); diff --git a/src/input/stream_input.h b/src/input/stream_input.h index 6f8b98b..2ff43dd 100644 --- a/src/input/stream_input.h +++ b/src/input/stream_input.h @@ -90,7 +90,7 @@ namespace input { */ void set_mouse_device(int device); /** - * @brief Integrate stick movement at a bounded time step, preserving fractional motion. + * @brief Integrate stick movement at up to 125 reports/second, preserving fractional motion and immediate clicks. * @param milliseconds Elapsed event-thread time. */ void tick(unsigned milliseconds); @@ -171,6 +171,7 @@ namespace input { int mouseDevice_ = -1; ///< Pad controlling the mouse, or disabled. double mouseX_ = 0; ///< Fractional horizontal motion. double mouseY_ = 0; ///< Fractional vertical motion. + unsigned mouseElapsed_ = 0; ///< Milliseconds accumulated toward the next 8 ms pointer report. }; /** diff --git a/src/input/stream_menu.cpp b/src/input/stream_menu.cpp index 0cc401a..1bdd29d 100644 --- a/src/input/stream_menu.cpp +++ b/src/input/stream_menu.cpp @@ -163,7 +163,6 @@ namespace input { keyboardLayout = 0; shiftOnce = false; caps = false; - confirmQuit = false; keyboardColumn_ = 4; keyboardRow_ = 1; previousSticks_ = {}; @@ -222,8 +221,7 @@ namespace input { if (next != cells.end()) { keyIndex = static_cast(next - cells.begin()); } - } else if (page == StreamMenuPage::quit_confirmation && (x != 0 || y != 0)) { - confirmQuit = !confirmQuit; + } else if (page == StreamMenuPage::logs) { logOffset = std::clamp(logOffset - y * 5, 0, 255); } else if (page == StreamMenuPage::radial && (x != 0 || y != 0)) { @@ -260,7 +258,7 @@ namespace input { "ABC"; } if (catalogIndex == KEYBOARD_CAPS_KEY) { - return caps ? "[Caps]" : "Caps"; + return "Caps"; } const auto &key = KEYS.at(catalogIndex); if (key.code == -MODIFIER_SHIFT) { @@ -269,17 +267,34 @@ namespace input { if (key.code < 0 && (modifiers & -key.code) != 0) { return std::string("[") + key.label + "]"; } - return (key_modifiers(key.code) & MODIFIER_SHIFT) != 0 ? key.shifted : key.label; + return (binding_modifiers(catalogIndex) & MODIFIER_SHIFT) != 0 ? key.shifted : key.label; } int StreamMenu::key_modifiers(int code) const { const int key = keyboard_cells().at(keyIndex).key; - const int required = key < static_cast(KEYS.size()) && KEYS[key].code == code ? KEYS[key].modifiers : 0; - if (key >= 106 && key < static_cast(KEYS.size()) && KEYS[key].code == code) { - return required; + if (key < static_cast(KEYS.size()) && KEYS[key].code == code) { + return binding_modifiers(key); + } + return modifiers | ((shiftOnce != (caps && code >= 'A' && code <= 'Z')) ? MODIFIER_SHIFT : 0); + } + + int StreamMenu::binding_modifiers(int index) const { + const auto &key = KEYS.at(index); + if (index >= 106) { + return key.modifiers; + } + const bool upper = shiftOnce != (caps && key.code >= 'A' && key.code <= 'Z'); + return modifiers | key.modifiers | (upper ? MODIFIER_SHIFT : 0); + } + + bool StreamMenu::confirm_disconnect(bool accept) { + if (page != StreamMenuPage::quit_confirmation) { + return false; + } + if (!accept) { + page = StreamMenuPage::radial; } - const bool upper = shiftOnce != (caps && code >= 'A' && code <= 'Z'); - return modifiers | required | (upper ? MODIFIER_SHIFT : 0); + return accept; } void StreamMenu::toggle_shift() { diff --git a/src/input/stream_menu.h b/src/input/stream_menu.h index dac3ee6..3143aa9 100644 --- a/src/input/stream_menu.h +++ b/src/input/stream_menu.h @@ -125,6 +125,12 @@ namespace input { * @brief Consume one-shot Shift after sending a host key. */ void key_sent(); + /** + * @brief Complete the disconnect dialog directly, without directional selection. + * @param accept True for A/Enter to disconnect; false for B/Escape to cancel. + * @return True only for an accepted disconnect on the confirmation page. + */ + bool confirm_disconnect(bool accept); StreamMenuPage page = StreamMenuPage::closed; ///< Current screen. StreamAction selected = StreamAction::none; ///< Highlighted wheel action. int owner = -1; ///< Controller that opened local controls. @@ -132,7 +138,6 @@ namespace input { int keyboardLayout = 0; ///< Letters, symbols, or desktop keys and shortcuts (0 through 2). bool shiftOnce = false; ///< Shift applies to the next host key only. bool caps = false; ///< Persistent uppercase letters, independent of the host Caps Lock state. - bool confirmQuit = false; ///< Disconnect button selected; Cancel is the initial selection. int modifiers = 0; ///< Latched Moonlight modifier toggles. int logOffset = 0; ///< Lines scrolled backward from the newest log. static constexpr int KEYBOARD_COLUMNS = 26; ///< Half-key column units, including both side control rails. @@ -143,6 +148,12 @@ namespace input { static const std::array KEYS; ///< Host keys, explicit symbols and common desktop shortcuts. private: + /** + * @brief Resolve a cell's own binding without consulting the highlighted cell. + * @param index Host key catalog index. + * @return Effective modifiers for that binding. + */ + int binding_modifiers(int index) const; int keyboardColumn_ = 4; ///< Preferred column retained while traversing wide keys. int keyboardRow_ = 1; ///< Preferred row retained while traversing tall side controls. std::array previousSticks_ {}; ///< Previous sampled axes so a stationary stick cannot steal the other wheel's selection. diff --git a/src/input/ui_input.cpp b/src/input/ui_input.cpp index 9c2cca5..ad5182b 100644 --- a/src/input/ui_input.cpp +++ b/src/input/ui_input.cpp @@ -8,6 +8,29 @@ #include namespace input { + UiCommand DirectionNavigation::update(UiCommand command, std::uint32_t now) { + if (command == UiCommand::none || !is_repeatable_ui_command(command)) { + reset(); + return UiCommand::none; + } + if (command != command_) { + command_ = command; + pressedAt_ = repeatedAt_ = now; + repeated_ = false; + return command; + } + if ((!repeated_ && now - pressedAt_ >= NAVIGATION_INITIAL_REPEAT_MILLISECONDS) || (repeated_ && now - repeatedAt_ >= NAVIGATION_REPEAT_MILLISECONDS)) { + repeatedAt_ = now; + repeated_ = true; + return command; + } + return UiCommand::none; + } + + void DirectionNavigation::reset() { + *this = {}; + } + bool is_gamepad_ui_activity(short value, bool trigger) { return trigger ? value >= 16000 : std::abs(static_cast(value)) >= 16000; } diff --git a/src/input/ui_input.h b/src/input/ui_input.h index 7919ef1..b0d6d56 100644 --- a/src/input/ui_input.h +++ b/src/input/ui_input.h @@ -80,6 +80,30 @@ namespace input { std::uint64_t order_ = 0; ///< Monotonic ordering within the current input lifetime. }; + /** + * @brief Repeat one sampled controller direction with the shared keyboard/menu timing. + */ + class DirectionNavigation { + public: + /** + * @brief Emit a changed direction immediately, then repeat after the shared hold delay. + * @param command Sampled direction, or none at neutral. + * @param now Monotonic millisecond tick, allowed to wrap. + * @return Direction to apply, or none until the next repeat. + */ + UiCommand update(UiCommand command, std::uint32_t now); + /** + * @brief Clear the held direction on a page or input-device transition. + */ + void reset(); + + private: + UiCommand command_ = UiCommand::none; ///< Currently held direction. + std::uint32_t pressedAt_ = 0; ///< First sample of the held direction. + std::uint32_t repeatedAt_ = 0; ///< Most recent emitted repeat. + bool repeated_ = false; ///< The initial repeat delay has elapsed. + }; + /** * @brief Repeat timing for one held controller direction. */ diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index 251d1a9..f94e6a2 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -362,8 +362,12 @@ namespace streaming { void FfmpegStreamBackend::invalidate_overlay_texture() { statisticsBitmapChanged_ = statisticsTextureChanged_ = true; if (statisticsTexture_ != nullptr) { - SDL_DestroyTexture(statisticsTexture_); - statisticsTexture_ = nullptr; + int width = 0; + int height = 0; + if (SDL_QueryTexture(statisticsTexture_, nullptr, nullptr, &width, &height) != 0 || width != statisticsOverlay_.width() || height != statisticsOverlay_.height()) { + SDL_DestroyTexture(statisticsTexture_); + statisticsTexture_ = nullptr; + } } } diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index 76bc089..c24ebf9 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -275,7 +275,7 @@ namespace streaming { */ void render_sdl_statistics(SDL_Renderer *renderer, const SDL_Rect &destination); /** - * @brief Mark either bitmap format changed and discard the previous SDL texture. + * @brief Mark pixels changed, retaining the SDL texture when its dimensions still match. */ void invalidate_overlay_texture(); /** diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index bb1458d..1568afb 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -184,33 +184,49 @@ namespace { } } - /** - * @brief Rasterize clipped text with optional leading alignment and a background. - * @param renderer Software panel renderer. - * @param font Label font. - * @param text UTF-8 label. - * @param bounds Label rectangle. - * @param leading Align text to the top-left for console output instead of centering it. - * @param background True to paint a label background; false to retain its key surface. - * @param fitSingleLine Scale an unwrapped key label down when needed to fit SD cells. - * @param foreground Text color, including log severity and focused modal buttons. - */ - void label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading = false, bool background = true, bool fitSingleLine = false, SDL_Color foreground = {255, 255, 255, 255}) { +} // namespace + +namespace streaming { + void StreamControls::clear_panel() { + text_.clear(); + textBytes_ = 0; + iconTextures_.clear(); + panelRenderer_.reset(); + panel_.reset(); + } + + void StreamControls::label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading, bool background, bool fitSingleLine, SDL_Color foreground) { if (background) { SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); SDL_RenderFillRect(renderer, &bounds); } - std::unique_ptr surface(fitSingleLine ? TTF_RenderUTF8_Blended(font, text.c_str(), foreground) : TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), foreground, std::max(1, bounds.w - 4)), SDL_FreeSurface); - if (!surface) { + if (text.empty()) { return; } - std::unique_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); - if (!texture) { - return; + const Uint32 color = foreground.r | (static_cast(foreground.g) << 8) | (static_cast(foreground.b) << 16) | (static_cast(foreground.a) << 24); + const auto key = std::make_tuple(font, text, fitSingleLine ? 0 : std::max(1, bounds.w - 4), color); + auto found = text_.find(key); + if (found == text_.end()) { + std::unique_ptr surface(fitSingleLine ? TTF_RenderUTF8_Blended(font, text.c_str(), foreground) : TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), foreground, std::max(1, bounds.w - 4)), SDL_FreeSurface); + if (!surface) { + return; + } + const auto bytes = static_cast(surface->w) * surface->h * 4; + if (textBytes_ + bytes > 1024U * 1024U) { + SDL_RenderFlush(renderer); + text_.clear(); + textBytes_ = 0; + } + std::shared_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); + if (!texture) { + return; + } + found = text_.emplace(key, CachedText {surface->w, surface->h, std::move(texture)}).first; + textBytes_ += bytes; } - // nxdk's SDL 2.0.9 software queue can dereference a missing viewport when - // a standalone clip command follows a texture flush. Crop the source instead. - auto layout = streaming::layout_overlay_label(surface->w, surface->h, {bounds.x, bounds.y, bounds.w, bounds.h}, fitSingleLine); + const auto &raster = found->second; + // nxdk SDL 2.0.9 requires source cropping rather than standalone clip commands. + auto layout = layout_overlay_label(raster.width, raster.height, {bounds.x, bounds.y, bounds.w, bounds.h}, fitSingleLine); if (leading) { layout.source.x = layout.source.y = 0; layout.destination.x = bounds.x; @@ -218,11 +234,47 @@ namespace { } SDL_Rect source {layout.source.x, layout.source.y, layout.source.width, layout.source.height}; SDL_Rect destination {layout.destination.x, layout.destination.y, layout.destination.width, layout.destination.height}; - SDL_RenderCopy(renderer, texture.get(), &source, &destination); + SDL_RenderCopy(renderer, raster.texture.get(), &source, &destination); + } + + void StreamControls::navigate_controller(const input::ControllerState &sample, Uint32 now) { + if (menu_.page != input::StreamMenuPage::keyboard && menu_.page != input::StreamMenuPage::logs) { + controllerNavigation_.reset(); + return; + } + int x = (sample.buttonFlags & RIGHT_FLAG) != 0 ? 1 : (sample.buttonFlags & LEFT_FLAG) != 0 ? -1 : + 0; + int y = (sample.buttonFlags & DOWN_FLAG) != 0 ? 1 : (sample.buttonFlags & UP_FLAG) != 0 ? -1 : + 0; + if (x == 0 && y == 0) { + if (std::max(std::abs(static_cast(sample.leftStickX)), std::abs(static_cast(sample.leftStickY))) >= input::StreamMenu::WHEEL_DEADZONE) { + if (std::abs(static_cast(sample.leftStickX)) > std::abs(static_cast(sample.leftStickY))) { + x = sample.leftStickX > 0 ? 1 : -1; + } else { + y = sample.leftStickY > 0 ? -1 : 1; + } + } + } + if (navigationNeutralRequired_) { + if (x == 0 && y == 0) { + navigationNeutralRequired_ = false; + } + return; + } + const auto command = x > 0 ? input::UiCommand::move_right : x < 0 ? input::UiCommand::move_left : + y > 0 ? input::UiCommand::move_down : + y < 0 ? input::UiCommand::move_up : + input::UiCommand::none; + const auto key = menu_navigation_key(controllerNavigation_.update(command, now)); + if (key != SDLK_UNKNOWN) { + menu_.navigate(key == SDLK_RIGHT ? 1 : key == SDLK_LEFT ? -1 : + 0, + key == SDLK_DOWN ? 1 : key == SDLK_UP ? -1 : + 0); + dirty_ = true; + } } -} // namespace -namespace streaming { StreamControls::StreamControls(): router_(send_packet) {} @@ -249,6 +301,7 @@ namespace streaming { } void StreamControls::close() { + clear_panel(); icons_.clear(); for (auto &device : devices_) { if (device.handle != nullptr) { @@ -356,6 +409,8 @@ namespace streaming { } menu_.open(device); keyboardNavigation_.reset(); + controllerNavigation_.reset(); + navigationNeutralRequired_ = false; keyboardPageTriggerDown_ = false; keyboardDockTriggerDown_ = false; inputDevice_ = device < 0 ? input::UiInputDevice::keyboard : input::UiInputDevice::gamepad; @@ -389,6 +444,16 @@ namespace streaming { } bool StreamControls::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) { + if (menu_.page == input::StreamMenuPage::quit_confirmation) { + if (key == SDLK_RETURN) { + return menu_.confirm_disconnect(true); + } + if (key == SDLK_ESCAPE) { + dirty_ = true; + return menu_.confirm_disconnect(false); + } + return false; + } dirty_ = true; if (key == SDLK_ESCAPE) { if (menu_.page == input::StreamMenuPage::radial) { @@ -435,13 +500,6 @@ namespace streaming { } return false; } - if (menu_.page == input::StreamMenuPage::quit_confirmation) { - if (menu_.confirmQuit) { - return true; - } - menu_.page = input::StreamMenuPage::radial; - return false; - } if (menu_.page == input::StreamMenuPage::keyboard) { if (active) { tap_key(menu_.keyboard_key()); @@ -454,13 +512,16 @@ namespace streaming { switch (menu_.selected) { case input::StreamAction::quit: menu_.page = input::StreamMenuPage::quit_confirmation; - menu_.confirmQuit = false; break; case input::StreamAction::keyboard: menu_.page = input::StreamMenuPage::keyboard; + controllerNavigation_.reset(); + navigationNeutralRequired_ = true; break; case input::StreamAction::logs: menu_.page = input::StreamMenuPage::logs; + controllerNavigation_.reset(); + navigationNeutralRequired_ = true; logs_.clear(); for (const auto &entry : logging::snapshot()) { logs_.push_back(logging::format_entry(entry)); @@ -520,8 +581,10 @@ namespace streaming { bool stop = false; const bool wasVisible = visible(); SDL_Event event {}; - while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0) { - const bool gamepadActivity = event.type == SDL_CONTROLLERBUTTONDOWN || (event.type == SDL_CONTROLLERAXISMOTION && input::is_gamepad_ui_activity(event.caxis.value, event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)); + const Uint32 eventStart = SDL_GetTicks(); + for (int count = 0; count < 128 && (count == 0 || SDL_GetTicks() - eventStart < 4) && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0; ++count) { + const bool triggerAxis = event.type == SDL_CONTROLLERAXISMOTION && (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT); + const bool gamepadActivity = event.type == SDL_CONTROLLERBUTTONDOWN || (event.type == SDL_CONTROLLERAXISMOTION && (triggerAxis ? event.caxis.value >= 16000 : std::abs(static_cast(event.caxis.value)) >= input::StreamMenu::WHEEL_DEADZONE)); if (visible() && gamepadActivity && menu_.owner < 0) { menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; } @@ -586,6 +649,13 @@ namespace streaming { router_.controller(button.which, device.sample); } } + if (visible() && armed_ && button.state == SDL_RELEASED && button.which == menu_.owner && menu_.page != input::StreamMenuPage::radial) { + for (const auto &device : devices_) { + if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == button.which) { + navigate_controller(device.sample, SDL_GetTicks()); + } + } + } if (visible() && armed_ && button.state == SDL_PRESSED) { if (menu_.owner < 0) { menu_.owner = button.which; @@ -593,9 +663,6 @@ namespace streaming { if (button.which != menu_.owner) { continue; } - if (button.button >= SDL_CONTROLLER_BUTTON_DPAD_UP && button.button <= SDL_CONTROLLER_BUTTON_DPAD_RIGHT) { - navigationTick_ = SDL_GetTicks(); - } switch (button.button) { case SDL_CONTROLLER_BUTTON_A: stop = menu_key(SDLK_RETURN, backend, active, statisticsEnabled) || stop; @@ -604,16 +671,23 @@ namespace streaming { stop = menu_key(SDLK_ESCAPE, backend, active, statisticsEnabled) || stop; break; case SDL_CONTROLLER_BUTTON_DPAD_UP: - menu_key(SDLK_UP, backend, active, statisticsEnabled); - break; case SDL_CONTROLLER_BUTTON_DPAD_DOWN: - menu_key(SDLK_DOWN, backend, active, statisticsEnabled); - break; case SDL_CONTROLLER_BUTTON_DPAD_LEFT: - menu_key(SDLK_LEFT, backend, active, statisticsEnabled); - break; case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: - menu_key(SDLK_RIGHT, backend, active, statisticsEnabled); + if (menu_.page == input::StreamMenuPage::radial) { + menu_key(button.button == SDL_CONTROLLER_BUTTON_DPAD_UP ? SDLK_UP : button.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ? SDLK_DOWN : + button.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ? SDLK_LEFT : + SDLK_RIGHT, + backend, + active, + statisticsEnabled); + } else { + for (const auto &device : devices_) { + if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == button.which) { + navigate_controller(device.sample, SDL_GetTicks()); + } + } + } break; case SDL_CONTROLLER_BUTTON_X: if (active && menu_.page == input::StreamMenuPage::keyboard) { @@ -652,7 +726,7 @@ namespace streaming { break; } } - } else if (event.type == SDL_CONTROLLERAXISMOTION && visible() && armed_ && event.caxis.which == menu_.owner && menu_.page == input::StreamMenuPage::radial) { + } else if (event.type == SDL_CONTROLLERAXISMOTION && visible() && armed_ && event.caxis.which == menu_.owner) { for (auto &device : devices_) { if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != event.caxis.which) { continue; @@ -676,7 +750,11 @@ namespace streaming { break; } if (event.caxis.axis <= SDL_CONTROLLER_AXIS_RIGHTY) { - dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; + if (menu_.page == input::StreamMenuPage::radial) { + dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; + } else { + navigate_controller(sample, SDL_GetTicks()); + } } } } else if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { @@ -735,6 +813,10 @@ namespace streaming { stop = true; } } + if (stop) { + router_.set_captured(true); + return true; + } poll(); const Uint32 now = SDL_GetTicks(); if (menu_.page == input::StreamMenuPage::logs && now - logRefreshTick_ >= 1000) { @@ -791,16 +873,8 @@ namespace streaming { // Poll on every pump as well as consuming events: Xbox input can change // between queued axis events, and a selected item must not freeze polling. dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; - } else if (menu_.page != input::StreamMenuPage::radial && now - navigationTick_ >= 180) { - const int x = (sample.buttonFlags & RIGHT_FLAG) != 0 || sample.leftStickX > 18000 ? 1 : (sample.buttonFlags & LEFT_FLAG) != 0 || sample.leftStickX < -18000 ? -1 : - 0; - const int y = (sample.buttonFlags & UP_FLAG) != 0 || sample.leftStickY > 18000 ? -1 : (sample.buttonFlags & DOWN_FLAG) != 0 || sample.leftStickY < -18000 ? 1 : - 0; - if (x != 0 || y != 0) { - menu_.navigate(x, y); - navigationTick_ = now; - dirty_ = true; - } + } else { + navigate_controller(sample, now); } } if (pulseSlot_ >= 0 && (!visible() || static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { @@ -811,6 +885,7 @@ namespace streaming { keyboardNavigation_.reset(); router_.set_captured(!focused_); backend.set_statistics_bitmap(0, 0, {}); + clear_panel(); dirty_ = false; } backend.local_overlay_size(width, height); @@ -856,9 +931,16 @@ namespace streaming { return false; } const auto &surface = found->second; - std::unique_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); + auto texture = iconTextures_.find(key); + if (texture == iconTextures_.end()) { + auto loaded = std::shared_ptr(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); + if (!loaded) { + return false; + } + texture = iconTextures_.emplace(key, std::move(loaded)).first; + } SDL_Rect destination {bounds.x + (bounds.w - surface->w) / 2, bounds.y + (bounds.h - surface->h) / 2, surface->w, surface->h}; - return texture && SDL_RenderCopy(renderer, texture.get(), nullptr, &destination) == 0; + return SDL_RenderCopy(renderer, texture->second.get(), nullptr, &destination) == 0; } void StreamControls::render_hint(SDL_Renderer *renderer, TTF_Font *font, const char *asset, const char *key, const char *text, SDL_Rect bounds, bool compact) { @@ -949,7 +1031,7 @@ namespace streaming { if (cell.width > 2 && shortcutIcon != nullptr && (!keyboardInput || *shortcutKey != '\0')) { if (cell.height > 1) { label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h / 2}, false, false, true); - render_hint(renderer, font, shortcutIcon, shortcutKey, "", {key.x + 2, key.y + key.h / 2, key.w - 4, key.h / 2 - 2}, true); + render_hint(renderer, font, shortcutIcon, shortcutKey, "", {key.x + 2, key.y + key.h / 2, key.w - 4, key.h / 2 - 2}, cell.key != input::StreamMenu::KEYBOARD_CAPS_KEY && cell.key != 46); } else { render_hint(renderer, font, shortcutIcon, shortcutKey, text.c_str(), {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, true); } @@ -967,11 +1049,11 @@ namespace streaming { const int line = TTF_FontLineSkip(font) + 8; const int footerY = padding + header + input::StreamMenu::KEYBOARD_ROWS * rowHeight + 4; const int footerHeight = std::min(line + 4, height - footerY); - const std::array assets {"button-a.svg", "button-b.svg", "button-lt.svg", "button-right-stick.svg", "button-left-stick.svg", "button-rt.svg"}; - const std::array keys {"Enter", "Esc", "Tab", "Shift", "Caps", "F2"}; - const std::array actions {"Select", "Back", "Pages", menu_.shiftOnce ? "Shift: On" : "Shift", menu_.caps ? "Caps: On" : "Caps", keyboardTop_ ? "Move down" : "Move up"}; - for (int i = 0; i < 6; ++i) { - render_hint(renderer, font, assets[i], keys[i], actions[i], {(i % 3) * width / 3 + 4, footerY + (i / 3) * line, width / 3 - 8, footerHeight}); + const std::array assets {"button-a.svg", "button-b.svg", "button-right-stick.svg", "button-rt.svg"}; + const std::array keys {"Enter", "Esc", "Shift", "F2"}; + const std::array actions {"Select", "Back", menu_.shiftOnce ? "Shift: On" : "Shift", keyboardTop_ ? "Move down" : "Move up"}; + for (int i = 0; i < 4; ++i) { + render_hint(renderer, font, assets[i], keys[i], actions[i], {i * width / 4 + 4, footerY, width / 4 - 8, footerHeight}); } } @@ -983,18 +1065,26 @@ namespace streaming { const int rowHeight = std::max(TTF_FontLineSkip(font) + 8, width * 9 / 200); const int safeBottom = std::max(8, height / 40); width = keyboard ? width * 9 / 10 : width; - height = keyboard ? std::min(height - VideoOverlay::MARGIN * 2, rowHeight * input::StreamMenu::KEYBOARD_ROWS + 28 + TTF_FontLineSkip(font) + (TTF_FontLineSkip(font) + 8) * 2 + safeBottom) : height; + height = keyboard ? std::min(height - VideoOverlay::MARGIN * 2, rowHeight * input::StreamMenu::KEYBOARD_ROWS + 28 + TTF_FontLineSkip(font) + (TTF_FontLineSkip(font) + 8) + safeBottom) : height; if (width <= 0 || height <= 0 || font == nullptr) { return; } - std::unique_ptr panel(SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_ARGB8888), SDL_FreeSurface); - if (!panel) { - return; + if (!panel_ || panel_->w != width || panel_->h != height) { + clear_panel(); + panel_.reset(SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_ARGB8888)); + if (panel_) { + panelRenderer_.reset(SDL_CreateSoftwareRenderer(panel_.get())); + } } - std::unique_ptr renderer(SDL_CreateSoftwareRenderer(panel.get()), SDL_DestroyRenderer); - if (!renderer) { + if (!panelRenderer_) { return; } + if (text_.size() > 256) { + text_.clear(); + textBytes_ = 0; + } + auto &panel = panel_; + auto &renderer = panelRenderer_; const Uint8 background = keyboard ? 16 : 0; SDL_SetRenderDrawColor(renderer.get(), background, background, background, keyboard ? 255 : 150); SDL_RenderClear(renderer.get()); @@ -1065,13 +1155,10 @@ namespace streaming { label(renderer.get(), font, "Disconnect from stream?", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); label(renderer.get(), font, "The app will keep running on your PC.", {card.x + 20, card.y + 28 + line, card.w - 40, line * 2}, false, false); for (int i = 0; i < 2; ++i) { - const bool selected = menu_.confirmQuit == (i == 1); - SDL_Rect button {card.x + 20 + i * (card.w - 32) / 2, card.y + card.h - line * 2 - 12, (card.w - 48) / 2, line + 8}; - ui::draw_dialog_round_rect(renderer.get(), button, selected ? SDL_Color {130, 180, 255, 255} : SDL_Color {28, 40, 61, 255}, 6); - label(renderer.get(), font, i == 0 ? "Cancel" : "Disconnect", button, false, false, true, selected ? SDL_Color {13, 18, 29, 255} : SDL_Color {242, 245, 248, 255}); + SDL_Rect button {card.x + 20 + i * (card.w - 32) / 2, card.y + card.h - line * 2, (card.w - 48) / 2, line + 12}; + ui::draw_dialog_round_rect(renderer.get(), button, {28, 40, 61, 255}, 6); + render_hint(renderer.get(), font, i == 0 ? "button-b.svg" : "button-a.svg", i == 0 ? "Esc" : "Enter", i == 0 ? "Cancel" : "Disconnect", {button.x + 4, button.y, button.w - 8, button.h}); } - render_hint(renderer.get(), font, "button-a.svg", "Enter", "Select", {card.x, card.y + card.h - line, card.w / 2, line}); - render_hint(renderer.get(), font, "button-b.svg", "Esc", "Back", {card.x + card.w / 2, card.y + card.h - line, card.w / 2, line}); } if (!message_.empty() && menu_.page == input::StreamMenuPage::radial) { label(renderer.get(), font, message_, {4, line + 4, width - 8, line * 2}, false, false); diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 907a08c..96f6c0a 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -10,11 +10,13 @@ #include "src/input/ui_input.h" #include +#include #include #include #include #include #include +#include #include namespace streaming { @@ -86,7 +88,7 @@ namespace streaming { */ std::vector diagnostic_lines() const; /** - * @brief Route SDL events and rebuild local UI only when it changes. + * @brief Route a bounded batch of SDL events and update cached local controls without starving video presentation. * @param backend Active video backend receiving the shared GPU/software overlay. * @param font Font for menu labels and keyboard cells. * @param width Output width. @@ -107,6 +109,38 @@ namespace streaming { void release_host_input(); private: + /** + * @brief One renderer-owned text texture reused between focus updates. + */ + struct CachedText { + int width = 0; ///< Natural raster width. + int height = 0; ///< Natural raster height. + std::shared_ptr texture; ///< Texture tied to the retained panel renderer. + }; + + /** + * @brief Release panel caches before destroying their renderer and surface. + */ + void clear_panel(); + /** + * @brief Draw cached text without rerasterizing unchanged labels on each selection. + * @param renderer Retained panel renderer. + * @param font Label font. + * @param text UTF-8 label. + * @param bounds Label bounds. + * @param leading Align console output to its top-left corner. + * @param background Paint an opaque label background. + * @param fitSingleLine Shrink a single-line key label to fit. + * @param foreground Text color. + */ + void label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading = false, bool background = true, bool fitSingleLine = false, SDL_Color foreground = {255, 255, 255, 255}); + /** + * @brief Apply sampled or queued digital/stick navigation with shared repeat timing. + * @param sample Controller state, with positive stick Y upward. + * @param now Current SDL tick. + */ + void navigate_controller(const input::ControllerState &sample, Uint32 now); + /** * @brief Independent controller state and shortcut latch. */ @@ -123,7 +157,7 @@ namespace streaming { */ void open_menu(int device); /** - * @brief Draw colored controls with a solid OSK panel and a transparent wheel background. + * @brief Draw cached colored controls with an opaque OSK and a translucent wheel backdrop. * @param backend Video backend. * @param font Menu font. * @param width Output width. @@ -177,13 +211,14 @@ namespace streaming { input::StreamInput router_; ///< Ordered event-thread host input. input::StreamMenu menu_; ///< Local menu state. input::KeyboardNavigation keyboardNavigation_; ///< Local keyboard repeats, independent of gameplay key forwarding. + input::DirectionNavigation controllerNavigation_; ///< Immediate direction changes with the shared repeat interval. + bool navigationNeutralRequired_ = false; ///< Release wheel navigation before moving around a newly opened page. input::UiInputDevice inputDevice_ = input::UiInputDevice::gamepad; ///< Device used by local helper text. bool armed_ = false; ///< Opening buttons have been released. bool dirty_ = false; ///< Overlay requires rasterization. bool focused_ = true; ///< Window focus state. int mouseDevice_ = -1; ///< Virtual mouse controller, or disabled. Uint32 lastTick_ = 0; ///< Pointer integration clock. - Uint32 navigationTick_ = 0; ///< Stick navigation repeat clock. int pulseSlot_ = -1; ///< Controller with a pending guide/share release. Uint32 pulseUntil_ = 0; ///< Guide/share pulse deadline. std::string message_; ///< Last action result. @@ -196,5 +231,10 @@ namespace streaming { std::string screenshotDirectory_; ///< Configured screenshot folder, empty for the default. Uint32 logRefreshTick_ = 0; ///< Last live log refresh. std::map, std::shared_ptr> icons_; ///< Cached SVG rasters survive panel renderer replacement. + std::unique_ptr panel_ {nullptr, SDL_FreeSurface}; ///< Retained software composition target. + std::unique_ptr panelRenderer_ {nullptr, SDL_DestroyRenderer}; ///< Renderer reused at a stable panel size. + std::map, CachedText> text_; ///< Bounded cache of unchanged label rasters. + std::size_t textBytes_ = 0; ///< Estimated text texture bytes, bounded to protect the Xbox memory budget. + std::map, std::shared_ptr> iconTextures_; ///< Renderer-owned SVG textures. }; } // namespace streaming diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index 349f93a..4cbb078 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -199,20 +199,64 @@ namespace streaming { set_bitmap(0, 0, {}); return; } + const bool sameGeometry = width_ == width && height_ == height && placement_ == placement && !argb_.empty(); + if (sameGeometry && argb_ == pixels) { + return; + } + bool opaque = true; + for (std::size_t index = 3; index < pixels.size(); index += 4) { + if (pixels[index] != 255) { + opaque = false; + break; + } + } + const bool reuse = sameGeometry && opaque == opaque_; + VideoRectangle changed {}; + if (reuse) { + int left = width; + int right = 0; + int top = height; + int bottom = 0; + for (int row = 0; row < height; ++row) { + const auto offset = static_cast(row) * width * 4; + if (std::memcmp(argb_.data() + offset, pixels.data() + offset, static_cast(width) * 4) == 0) { + continue; + } + int first = 0; + while (std::memcmp(argb_.data() + offset + first * 4, pixels.data() + offset + first * 4, 4) == 0) { + ++first; + } + int last = width - 1; + while (std::memcmp(argb_.data() + offset + last * 4, pixels.data() + offset + last * 4, 4) == 0) { + --last; + } + left = std::min(left, first); + right = std::max(right, last + 1); + top = std::min(top, row); + bottom = row + 1; + } + if (damage_.width > 0 && damage_.height > 0) { + left = std::min(left, damage_.x); + right = std::max(right, damage_.x + damage_.width); + top = std::min(top, damage_.y); + bottom = std::max(bottom, damage_.y + damage_.height); + } + changed = {left, top, right - left, bottom - top}; + } width_ = width; height_ = height; placement_ = placement; argb_ = std::move(pixels); std::vector().swap(grayscale_); std::vector().swap(opacity_); - opaque_ = true; - for (std::size_t index = 3; index < argb_.size(); index += 4) { - if (argb_[index] != 255) { - opaque_ = false; - break; - } + opaque_ = opaque; + if (reuse) { + rgb_.clear(); + rgbBpp_ = 0; + damage_ = changed; + } else { + invalidate_caches(); } - invalidate_caches(); } std::uint32_t VideoOverlay::color_at(std::size_t index) const { @@ -226,6 +270,7 @@ namespace streaming { } void VideoOverlay::invalidate_caches() { + damage_ = {0, 0, width_, height_}; rgb_.clear(); rgbBpp_ = 0; yuy2_.clear(); @@ -333,7 +378,8 @@ namespace streaming { if (!valid_storage(pixels, capacity, pitch, frameWidth, frameHeight, 2) || (frameWidth & 1) != 0 || frameWidth > 2046 || frameHeight > 2047 || destination.width <= MARGIN || destination.height <= MARGIN || matrix == VideoColorMatrix::unsupported) { return false; } - if (frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height || matrix_ != matrix) { + const bool rebuild = frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height || matrix_ != matrix; + if (rebuild) { matrix_ = matrix; frameWidth_ = frameWidth; frameHeight_ = frameHeight; @@ -357,9 +403,21 @@ namespace streaming { if (!opaque_) { yuy2Opacity_.resize(yuy2_.size() / 2); } - for (int row = 0; row < source_.height; ++row) { + } + if (rebuild || (damage_.width > 0 && damage_.height > 0)) { + const auto bounds = rectangle({0, 0, destination.width, destination.height}); + const int visibleWidth = bounds.width; + const int visibleHeight = bounds.height; + if (visibleWidth == 0 || visibleHeight == 0) { + return true; + } + const int firstRow = rebuild ? 0 : std::min(source_.height, damage_.y * source_.height / visibleHeight); + const int lastRow = rebuild ? source_.height : std::min(source_.height, ((damage_.y + damage_.height) * source_.height + visibleHeight - 1) / visibleHeight); + const int firstColumn = rebuild ? 0 : std::min(source_.width, (damage_.x * source_.width / visibleWidth) & ~1); + const int lastColumn = rebuild ? source_.width : std::min(source_.width, ((((damage_.x + damage_.width) * source_.width + visibleWidth - 1) / visibleWidth) + 1) & ~1); + for (int row = firstRow; row < lastRow; ++row) { const int bitmapRow = std::min(visibleHeight - 1, row * visibleHeight / source_.height); - for (int column = 0; column < source_.width; column += 2) { + for (int column = firstColumn; column < lastColumn; column += 2) { const int firstColumn = std::min(visibleWidth - 1, column * visibleWidth / source_.width); const int secondColumn = std::min(visibleWidth - 1, (column + 1) * visibleWidth / source_.width); const auto firstColor = color_at(static_cast(bitmapRow) * width_ + firstColumn); @@ -383,6 +441,7 @@ namespace streaming { } } } + damage_ = {}; if (source_.width <= 0 || source_.height <= 0) { return true; diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index 5823347..5e136bf 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -85,7 +85,7 @@ namespace streaming { */ void set_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left, std::vector opacity = {}); /** - * @brief Replace the panel with straight ARGB pixels and their original SVG colors. + * @brief Replace colored pixels, retaining conversion caches and tracking damage for stable panel geometry. * @param width Bitmap width. * @param height Bitmap height. * @param pixels Packed B, G, R, A bytes; invalid storage clears the panel. @@ -168,6 +168,7 @@ namespace streaming { int width_ = 0; ///< Packed panel width. int height_ = 0; ///< Packed panel height. VideoOverlayPlacement placement_ = VideoOverlayPlacement::top_left; ///< Panel anchor shared by SDL, RGB and YUY2 paths. + VideoRectangle damage_ {}; ///< Pending source-pixel changes; only affected YUY2 pairs need reconversion. std::vector grayscale_; ///< Unpremultiplied font and UI colors. std::vector opacity_; ///< Optional source coverage; empty for legacy opaque statistics. std::vector argb_; ///< Original straight ARGB UI colors; empty for grayscale statistics. diff --git a/tests/unit/input/stream_input_test.cpp b/tests/unit/input/stream_input_test.cpp index 6cc167e..c76460e 100644 --- a/tests/unit/input/stream_input_test.cpp +++ b/tests/unit/input/stream_input_test.cpp @@ -274,3 +274,54 @@ TEST(StreamKeyMappingTest, PreservesExtendedModifiersNavigationAndKeypad) { EXPECT_NE(input::keyboard_virtual_key(usage), 0) << usage; } } + +/** + * @brief Stick mouse reports are capped at 125 Hz while press/release transitions remain immediate. + */ +TEST_F(StreamInputTest, BoundsMouseReportRateWithoutDelayingClicks) { + router.connect(0); + router.set_mouse_device(0); + router.controller(0, {}); + packets.clear(); + router.controller(0, {0, 0, 0, 32767, 0}); + router.tick(1); + EXPECT_TRUE(packets.empty()); + router.controller(0, {A_FLAG, 0, 0, 32767, 0}); + ASSERT_EQ(packets.size(), 1); + EXPECT_TRUE(packets.back().pressed); + router.controller(0, {0, 0, 0, 32767, 0}); + ASSERT_EQ(packets.size(), 2); + EXPECT_FALSE(packets.back().pressed); + router.tick(7); + ASSERT_EQ(packets.size(), 3); + EXPECT_EQ(packets.back().type, input::StreamPacketType::mouse_move); + EXPECT_EQ(packets.back().x, 7); + router.tick(0); + EXPECT_EQ(packets.size(), 3); +} + +/** + * @brief A minute of high-frequency polling preserves mouse speed without flooding the transport. + */ +TEST(StreamInputRateTest, SustainsMouseMovementWithBoundedTraffic) { + int reports = 0; + int distance = 0; + input::StreamInput router([&](const input::StreamPacket &packet) { + if (packet.type == input::StreamPacketType::mouse_move) { + ++reports; + distance += packet.x; + EXPECT_GE(packet.x, 0); + EXPECT_LE(packet.x, 8); + } + }); + router.connect(0); + router.set_mouse_device(0); + router.controller(0, {}); + router.controller(0, {0, 0, 0, 32767, 0}); + for (int i = 0; i < 60000; ++i) { + router.controller(0, {0, 0, 0, 32767, 0}); + router.tick(1); + } + EXPECT_EQ(reports, 7500); + EXPECT_NEAR(distance, 54000, 1); +} diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp index f68e10f..f99d8b9 100644 --- a/tests/unit/input/stream_menu_test.cpp +++ b/tests/unit/input/stream_menu_test.cpp @@ -204,19 +204,49 @@ TEST(StreamMenuTest, SendsSymbolsAndDesktopCombinations) { } /** - * @brief Disconnect confirmation starts with Cancel and changes only on deliberate navigation. + * @brief Disconnect is accepted directly and arrows cannot change A/B semantics. */ -TEST(StreamMenuTest, DefaultsToCancelOnDisconnectConfirmation) { +TEST(StreamMenuTest, RequiresExplicitDisconnectAndCancelsDirectly) { input::StreamMenu menu; + EXPECT_FALSE(menu.confirm_disconnect(true)); menu.open(1); + EXPECT_FALSE(menu.confirm_disconnect(true)); menu.page = input::StreamMenuPage::quit_confirmation; - EXPECT_FALSE(menu.confirmQuit); - menu.navigate(0, 0); - EXPECT_FALSE(menu.confirmQuit); menu.navigate(1, 0); - EXPECT_TRUE(menu.confirmQuit); - menu.navigate(-1, 0); - EXPECT_FALSE(menu.confirmQuit); + menu.navigate(0, -1); + EXPECT_EQ(menu.page, input::StreamMenuPage::quit_confirmation); + EXPECT_FALSE(menu.confirm_disconnect(false)); + EXPECT_EQ(menu.page, input::StreamMenuPage::radial); + menu.page = input::StreamMenuPage::quit_confirmation; + EXPECT_TRUE(menu.confirm_disconnect(true)); +} + +/** + * @brief A symbol sharing a digit's virtual key never changes that digit's label or sent modifiers. + */ +TEST(StreamMenuTest, LabelsEveryCellIndependentlyOfTheFocusedBinding) { + input::StreamMenu menu; + menu.open(-1); + menu.page = input::StreamMenuPage::keyboard; + menu.toggle_keyboard_page(); + const std::array digits {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}; + for (int focus = 0; focus < 47; ++focus) { + menu.keyIndex = focus; + for (int digit = 0; digit < 10; ++digit) { + EXPECT_EQ(menu.key_label(digit), digits[digit]) << "focus=" << focus; + } + } + menu.keyIndex = 12; + EXPECT_EQ(menu.key_label(2), "3"); + EXPECT_EQ(menu.key_label(12), "#"); + EXPECT_EQ(menu.keyboard_key(), '3'); + EXPECT_EQ(menu.key_modifiers('3'), MODIFIER_SHIFT); + menu.keyIndex = 2; + EXPECT_EQ(menu.keyboard_key(), '3'); + EXPECT_EQ(menu.key_modifiers('3'), 0); + menu.caps = true; + EXPECT_EQ(menu.key_label(43), "Caps"); + EXPECT_EQ(menu.key_label(2), "3"); } /** diff --git a/tests/unit/input/ui_input_test.cpp b/tests/unit/input/ui_input_test.cpp index 20b0cf1..76fb95a 100644 --- a/tests/unit/input/ui_input_test.cpp +++ b/tests/unit/input/ui_input_test.cpp @@ -132,3 +132,35 @@ TEST(ShellInputStateTest, ResumesNavigationAfterStreamingConsumesReleaseEvents) EXPECT_EQ(state.keyboardNavigation.poll(10150), input::UiCommand::move_down); EXPECT_EQ(state.keyboardNavigation.press(2, input::UiCommand::confirm, 10200), input::UiCommand::confirm); } + +/** + * @brief Controller direction changes respond immediately and held movement matches keyboard repeats. + */ +TEST(DirectionNavigationTest, RespondsImmediatelyAndUsesSharedRepeatTiming) { + input::DirectionNavigation navigation; + EXPECT_EQ(navigation.update(input::UiCommand::move_down, 100), input::UiCommand::move_down); + EXPECT_EQ(navigation.update(input::UiCommand::move_down, 249), input::UiCommand::none); + EXPECT_EQ(navigation.update(input::UiCommand::move_down, 250), input::UiCommand::move_down); + EXPECT_EQ(navigation.update(input::UiCommand::move_down, 294), input::UiCommand::none); + EXPECT_EQ(navigation.update(input::UiCommand::move_down, 295), input::UiCommand::move_down); + EXPECT_EQ(navigation.update(input::UiCommand::move_left, 296), input::UiCommand::move_left); + EXPECT_EQ(navigation.update(input::UiCommand::move_right, 297), input::UiCommand::move_right); + EXPECT_EQ(navigation.update(input::UiCommand::move_right, 10000), input::UiCommand::move_right); + EXPECT_EQ(navigation.update(input::UiCommand::move_right, 10000), input::UiCommand::none); +} + +/** + * @brief Neutral, actions and page transitions cancel a hold, including when the clock wraps. + */ +TEST(DirectionNavigationTest, ClearsOnNeutralOrResetAndHandlesClockWrap) { + input::DirectionNavigation navigation; + constexpr auto start = std::numeric_limits::max() - 100; + EXPECT_EQ(navigation.update(input::UiCommand::move_up, start), input::UiCommand::move_up); + EXPECT_EQ(navigation.update(input::UiCommand::move_up, 49), input::UiCommand::move_up); + EXPECT_EQ(navigation.update(input::UiCommand::none, 50), input::UiCommand::none); + EXPECT_EQ(navigation.update(input::UiCommand::move_up, 51), input::UiCommand::move_up); + EXPECT_EQ(navigation.update(input::UiCommand::confirm, 52), input::UiCommand::none); + EXPECT_EQ(navigation.update(input::UiCommand::move_up, 53), input::UiCommand::move_up); + navigation.reset(); + EXPECT_EQ(navigation.update(input::UiCommand::move_up, 54), input::UiCommand::move_up); +} diff --git a/tests/unit/streaming/video_overlay_test.cpp b/tests/unit/streaming/video_overlay_test.cpp index 6d56c46..a283249 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -597,3 +597,84 @@ TEST(VideoOverlayTest, DimsTheStreamThroughBothPresentationPaths) { EXPECT_EQ(yuy2, expected); } } + +/** + * @brief Cached UI damage produces the same colors, alpha and chroma as a fresh overlay at both scales. + */ +TEST(VideoOverlayTest, RecolorsChangedRegionsWithoutStaleGpuPixels) { + for (const int scale : {32, 128}) { + for (const auto matrix : {streaming::VideoColorMatrix::bt601, streaming::VideoColorMatrix::bt709}) { + streaming::VideoOverlay incremental; + std::vector pixels(16 * 12 * 4, 0); + for (std::size_t i = 3; i < pixels.size(); i += 4) { + pixels[i] = 150; + } + const streaming::VideoRectangle destination {0, 0, scale, scale * 3 / 4}; + for (int step = 0; step < 8; ++step) { + const auto index = static_cast((step * 7) % (16 * 12)) * 4; + pixels[index] = 200; + pixels[index + 1] = 40; + pixels[index + 2] = 70; + pixels[index + 3] = step % 2 ? 255 : 0; + incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + if (step % 2 == 0) { + const auto other = static_cast(16 * 12 - step - 1) * 4; + pixels[other + 1] = 240; + incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + } + // An identical update must preserve pending damage until presentation. + incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + streaming::VideoOverlay fresh; + fresh.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + std::vector actual(64 * 48 * 2, 100); + auto expected = actual; + ASSERT_TRUE(incremental.draw_yuy2(actual.data(), actual.size(), 128, 64, 48, destination, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 128, 64, 48, destination, matrix)); + EXPECT_EQ(actual, expected) << "scale=" << scale << " step=" << step; + // A new host frame must still be composed with the cached UI. + actual.assign(actual.size(), 180); + expected = actual; + ASSERT_TRUE(incremental.draw_yuy2(actual.data(), actual.size(), 128, 64, 48, destination, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 128, 64, 48, destination, matrix)); + EXPECT_EQ(actual, expected); + } + } + } +} + +/** + * @brief RGB output and GPU conversion survive opacity transitions, clipped damage and a matrix change. + */ +TEST(VideoOverlayTest, RebuildsCachesWhenOpacityOrMatrixChanges) { + streaming::VideoOverlay overlay; + std::vector pixels(20 * 20 * 4, 255); + const auto compare = [&](streaming::VideoColorMatrix matrix) { + streaming::VideoOverlay fresh; + fresh.set_argb_bitmap(20, 20, pixels, streaming::VideoOverlayPlacement::full_screen); + for (int bpp : {15, 16, 32}) { + const int bytes = bpp == 32 ? 4 : 2; + std::vector actual(16 * 16 * bytes, 90); + auto expected = actual; + ASSERT_TRUE(overlay.draw_rgb(actual.data(), actual.size(), 16 * bytes, 16, 16, bpp, {0, 0, 16, 16})); + ASSERT_TRUE(fresh.draw_rgb(expected.data(), expected.size(), 16 * bytes, 16, 16, bpp, {0, 0, 16, 16})); + EXPECT_EQ(actual, expected); + } + std::vector actual(16 * 16 * 2, 90); + auto expected = actual; + ASSERT_TRUE(overlay.draw_yuy2(actual.data(), actual.size(), 32, 16, 16, {0, 0, 16, 16}, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 32, 16, 16, {0, 0, 16, 16}, matrix)); + EXPECT_EQ(actual, expected); + }; + overlay.set_argb_bitmap(20, 20, pixels, streaming::VideoOverlayPlacement::full_screen); + compare(streaming::VideoColorMatrix::bt601); + pixels[3] = 80; + overlay.set_argb_bitmap(20, 20, pixels, streaming::VideoOverlayPlacement::full_screen); + compare(streaming::VideoColorMatrix::bt601); + pixels[(19 * 20 + 19) * 4] = 40; + overlay.set_argb_bitmap(20, 20, pixels, streaming::VideoOverlayPlacement::full_screen); + compare(streaming::VideoColorMatrix::bt601); + compare(streaming::VideoColorMatrix::bt709); + pixels[3] = 255; + overlay.set_argb_bitmap(20, 20, pixels, streaming::VideoOverlayPlacement::full_screen); + compare(streaming::VideoColorMatrix::bt709); +} diff --git a/xbe/assets/README.md b/xbe/assets/README.md index 4c7a85d..2c064c0 100644 --- a/xbe/assets/README.md +++ b/xbe/assets/README.md @@ -23,5 +23,7 @@ Keyboard helper keycaps are drawn with SDL primitives and the bundled UI font ra Their measured widths adapt to labels such as Enter and Shift+PgUp, and crowded modal helpers wrap onto additional rows. The active input device chooses between these keycaps and the controller glyphs. -Stream controls reuse the original colored controller button assets and SVG rasterizer. The stick, -Guide and Share symbols are local vector assets; Lucide supplies the other stream action symbols. +Stream controls reuse the original colored controller button assets and SVG rasterizer. The stick and +Share symbols are local vector assets; Lucide supplies the other stream action symbols. +The Guide button uses the [Xbox symbol from Simple Icons 11.15.0](https://github.com/simple-icons/simple-icons/blob/11.15.0/icons/xbox.svg), +under [CC0](licenses/simple-icons.txt), with matching 24-unit dimensions and an explicit white fill for the older Xbox NanoSVG parser. diff --git a/xbe/assets/icons/button-guide.svg b/xbe/assets/icons/button-guide.svg index 6a25ef5..af696c4 100644 --- a/xbe/assets/icons/button-guide.svg +++ b/xbe/assets/icons/button-guide.svg @@ -1 +1 @@ - \ No newline at end of file +Xbox diff --git a/xbe/assets/licenses/simple-icons.txt b/xbe/assets/licenses/simple-icons.txt new file mode 100644 index 0000000..f0f4b03 --- /dev/null +++ b/xbe/assets/licenses/simple-icons.txt @@ -0,0 +1,30 @@ +# CC0 1.0 Universal + +## Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an “owner”) of an original work of authorship and/or a database (each, a “Work”). + +Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works (“Commons”) that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the “Affirmer”), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights (“Copyright and Related Rights”). Copyright and Related Rights include, but are not limited to, the following: + 1. the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; + 2. moral rights retained by the original author(s) and/or performer(s); + 3. publicity and privacy rights pertaining to a person’s image or likeness depicted in a Work; + 4. rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(i), below; + 5. rights protecting the extraction, dissemination, use and reuse of data in a Work; + 6. database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and + 7. other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the “Waiver”). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer’s heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer’s express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer’s express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer’s Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the “License”). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer’s express Statement of Purpose. + +4. Limitations and Disclaimers. + 1. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. + 2. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. + 3. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person’s Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. + 4. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. + +For more information, please see https://creativecommons.org/publicdomain/zero/1.0/. From 1e0bd2083584e91c251612f97933c02ced50430e Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:26:06 -0500 Subject: [PATCH 03/22] fix(streaming): speed up overlays and correct OSK navigation --- README.md | 3 +- src/input/stream_menu.cpp | 4 +- src/input/stream_menu.h | 4 +- src/input/ui_input.cpp | 8 ++ src/input/ui_input.h | 10 ++ src/streaming/ffmpeg_stream_backend.cpp | 23 +++- src/streaming/ffmpeg_stream_backend.h | 7 ++ src/streaming/stream_controls.cpp | 126 ++++++++++++++++---- src/streaming/stream_controls.h | 18 ++- src/streaming/video_overlay.cpp | 103 ++++++++++++---- src/streaming/video_overlay.h | 11 +- src/ui/shell_screen.cpp | 4 +- tests/unit/input/stream_menu_test.cpp | 27 +++++ tests/unit/input/ui_input_test.cpp | 36 ++++++ tests/unit/streaming/video_overlay_test.cpp | 107 +++++++++++++++++ 15 files changed, 434 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 50652d8..2cd2db1 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,8 @@ QWERTY keys, a wide space bar, tall side controls, and an outlined focus key. Us to move, **A** to type, **X** for Backspace, **Y** for Space, and **Start** for host Enter. **Right stick click** applies Shift to the next key; **left stick click** toggles persistent Caps. **White / Black (LB / RB)** move the host text cursor left / right. **LT** cycles through letters, -symbols, and PC keys. The symbols page groups numbers and punctuation; PC keys include F1–F12, +symbols, and PC keys. The symbols page keeps its explicit numbers and punctuation unchanged by Shift; +PC keys include F1–F12, navigation keys, Ctrl / Alt / Win toggles, and shortcuts for Copy, Paste, Cut, Undo, Redo, Select All, Alt+Tab, Alt+F4, Ctrl+Alt+Delete, Win+D, Win+E and Win+R. Focus a shortcut to see its full combination above the keys. Named shortcuts send their own modifier combination. **RT** moves the panel between diff --git a/src/input/stream_menu.cpp b/src/input/stream_menu.cpp index 1bdd29d..78bca1e 100644 --- a/src/input/stream_menu.cpp +++ b/src/input/stream_menu.cpp @@ -283,7 +283,9 @@ namespace input { if (index >= 106) { return key.modifiers; } - const bool upper = shiftOnce != (caps && key.code >= 'A' && key.code <= 'Z'); + // This page already exposes both digits and their symbols as separate keys. + // Its explicit bindings must agree with the labels even with one-shot Shift. + const bool upper = keyboardLayout != 1 && (shiftOnce != (caps && key.code >= 'A' && key.code <= 'Z')); return modifiers | key.modifiers | (upper ? MODIFIER_SHIFT : 0); } diff --git a/src/input/stream_menu.h b/src/input/stream_menu.h index 3143aa9..0bb94b3 100644 --- a/src/input/stream_menu.h +++ b/src/input/stream_menu.h @@ -112,7 +112,7 @@ namespace input { */ void toggle_keyboard_page(); /** - * @brief Compute modifiers for a host key, including symbols, Caps and one-shot Shift. + * @brief Compute host modifiers, preserving explicit symbols-page bindings regardless of one-shot Shift. * @param code Host virtual key to send. * @return Moonlight modifier flags. */ @@ -149,7 +149,7 @@ namespace input { private: /** - * @brief Resolve a cell's own binding without consulting the highlighted cell. + * @brief Resolve a cell's binding, applying Caps and one-shot Shift outside the explicit symbols page. * @param index Host key catalog index. * @return Effective modifiers for that binding. */ diff --git a/src/input/ui_input.cpp b/src/input/ui_input.cpp index ad5182b..a6a2ab2 100644 --- a/src/input/ui_input.cpp +++ b/src/input/ui_input.cpp @@ -8,6 +8,14 @@ #include namespace input { + UiCommand DirectionNavigation::update_stick(short x, short y, std::uint32_t now) { + const int horizontal = std::abs(static_cast(x)); + const int vertical = std::abs(static_cast(y)); + const auto direction = horizontal > vertical ? (x > 0 ? UiCommand::move_right : UiCommand::move_left) : (y > 0 ? UiCommand::move_up : UiCommand::move_down); + const int threshold = direction == command_ ? STICK_NAVIGATION_RELEASE_THRESHOLD : STICK_NAVIGATION_THRESHOLD; + return update(std::max(horizontal, vertical) >= threshold ? direction : UiCommand::none, now); + } + UiCommand DirectionNavigation::update(UiCommand command, std::uint32_t now) { if (command == UiCommand::none || !is_repeatable_ui_command(command)) { reset(); diff --git a/src/input/ui_input.h b/src/input/ui_input.h index b0d6d56..4f380a1 100644 --- a/src/input/ui_input.h +++ b/src/input/ui_input.h @@ -12,6 +12,8 @@ namespace input { constexpr std::uint32_t NAVIGATION_INITIAL_REPEAT_MILLISECONDS = 150; ///< Delay shared by keyboard and controller navigation. constexpr std::uint32_t NAVIGATION_REPEAT_MILLISECONDS = 45; ///< Interval shared by keyboard and controller navigation. + constexpr short STICK_NAVIGATION_THRESHOLD = 16000; ///< Deflection required to begin directional navigation. + constexpr short STICK_NAVIGATION_RELEASE_THRESHOLD = 12000; ///< Lower release threshold prevents duplicate steps from stick jitter. /** * @brief Device whose intentional input most recently controlled the UI. @@ -92,6 +94,14 @@ namespace input { * @return Direction to apply, or none until the next repeat. */ UiCommand update(UiCommand command, std::uint32_t now); + /** + * @brief Navigate along the dominant stick axis using the main menus' hysteresis and repeat timing. + * @param x Signed horizontal deflection, positive rightward. + * @param y Signed vertical deflection, positive upward. + * @param now Monotonic millisecond tick, allowed to wrap. + * @return At most one directional step, or none at neutral or before a repeat is due. + */ + UiCommand update_stick(short x, short y, std::uint32_t now); /** * @brief Clear the held direction on a page or input-device transition. */ diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index f94e6a2..b014d0b 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -361,6 +361,7 @@ namespace streaming { void FfmpegStreamBackend::invalidate_overlay_texture() { statisticsBitmapChanged_ = statisticsTextureChanged_ = true; + statisticsTextureDamage_ = {0, 0, statisticsOverlay_.width(), statisticsOverlay_.height()}; if (statisticsTexture_ != nullptr) { int width = 0; int height = 0; @@ -371,6 +372,23 @@ namespace streaming { } } + void FfmpegStreamBackend::update_color_overlay(const VideoRectangle ®ion, const std::vector &pixels) { + if (!statisticsOverlay_.update_argb_bitmap(region, pixels)) { + return; + } + if (statisticsTextureChanged_) { + const int right = std::max(statisticsTextureDamage_.x + statisticsTextureDamage_.width, region.x + region.width); + const int bottom = std::max(statisticsTextureDamage_.y + statisticsTextureDamage_.height, region.y + region.height); + statisticsTextureDamage_.x = std::min(statisticsTextureDamage_.x, region.x); + statisticsTextureDamage_.y = std::min(statisticsTextureDamage_.y, region.y); + statisticsTextureDamage_.width = right - statisticsTextureDamage_.x; + statisticsTextureDamage_.height = bottom - statisticsTextureDamage_.y; + } else { + statisticsTextureDamage_ = region; + } + statisticsBitmapChanged_ = statisticsTextureChanged_ = true; + } + bool FfmpegStreamBackend::has_pending_overlay() const { return statisticsBitmapChanged_; } @@ -665,13 +683,16 @@ namespace streaming { SDL_SetTextureBlendMode(statisticsTexture_, SDL_BLENDMODE_BLEND); } statisticsTextureChanged_ = true; + statisticsTextureDamage_ = {0, 0, width, height}; } if (statisticsTexture_ == nullptr) { return; } if (statisticsTextureChanged_) { const auto &pixels = statisticsOverlay_.argb_pixels(); - if (SDL_UpdateTexture(statisticsTexture_, nullptr, pixels.data(), width * 4) != 0) { + const SDL_Rect damage {statisticsTextureDamage_.x, statisticsTextureDamage_.y, statisticsTextureDamage_.width, statisticsTextureDamage_.height}; + const auto offset = (static_cast(damage.y) * width + damage.x) * 4; + if (SDL_UpdateTexture(statisticsTexture_, &damage, pixels.data() + offset, width * 4) != 0) { return; } statisticsTextureChanged_ = false; diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index c24ebf9..0db5cdc 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -110,6 +110,12 @@ namespace streaming { * @param placement Panel anchor shared by GPU and software paths. */ void set_color_overlay(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left); + /** + * @brief Submit a small focus repaint while retaining the rest of the panel and its conversion caches. + * @param region Updated rectangle in panel pixels. + * @param pixels Packed straight ARGB bytes for that rectangle. + */ + void update_color_overlay(const VideoRectangle ®ion, const std::vector &pixels); /** * @brief Check whether an overlay change requires presenting an otherwise idle frame. @@ -442,6 +448,7 @@ namespace streaming { SDL_Texture *statisticsTexture_ = nullptr; ///< Cached panel texture for SDL's final fallback. bool statisticsBitmapChanged_ = false; ///< A panel update requires refreshing a paused video frame. bool statisticsTextureChanged_ = false; ///< SDL texture requires the current bitmap. + VideoRectangle statisticsTextureDamage_ {}; ///< Pending SDL texture upload, accumulated until software presentation. VideoAccelerationReport acceleration_; ///< Session diagnostics survive decoder cleanup. bool overlayFailed_ = false; ///< Avoid repeating a known failing hardware path this session. std::atomic audioPlaybackEnabled_ = false; diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 1568afb..796e165 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -193,6 +193,8 @@ namespace streaming { iconTextures_.clear(); panelRenderer_.reset(); panel_.reset(); + panelContent_.reset(); + panelDamage_.clear(); } void StreamControls::label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading, bool background, bool fitSingleLine, SDL_Color foreground) { @@ -246,17 +248,8 @@ namespace streaming { 0; int y = (sample.buttonFlags & DOWN_FLAG) != 0 ? 1 : (sample.buttonFlags & UP_FLAG) != 0 ? -1 : 0; - if (x == 0 && y == 0) { - if (std::max(std::abs(static_cast(sample.leftStickX)), std::abs(static_cast(sample.leftStickY))) >= input::StreamMenu::WHEEL_DEADZONE) { - if (std::abs(static_cast(sample.leftStickX)) > std::abs(static_cast(sample.leftStickY))) { - x = sample.leftStickX > 0 ? 1 : -1; - } else { - y = sample.leftStickY > 0 ? -1 : 1; - } - } - } if (navigationNeutralRequired_) { - if (x == 0 && y == 0) { + if (x == 0 && y == 0 && std::max(std::abs(static_cast(sample.leftStickX)), std::abs(static_cast(sample.leftStickY))) < input::STICK_NAVIGATION_RELEASE_THRESHOLD) { navigationNeutralRequired_ = false; } return; @@ -265,8 +258,10 @@ namespace streaming { y > 0 ? input::UiCommand::move_down : y < 0 ? input::UiCommand::move_up : input::UiCommand::none; - const auto key = menu_navigation_key(controllerNavigation_.update(command, now)); - if (key != SDLK_UNKNOWN) { + const auto step = x == 0 && y == 0 ? controllerNavigation_.update_stick(sample.leftStickX, sample.leftStickY, now) : controllerNavigation_.update(command, now); + const auto key = menu_navigation_key(step); + if (key != SDLK_UNKNOWN && !controllerNavigationApplied_) { + controllerNavigationApplied_ = true; menu_.navigate(key == SDLK_RIGHT ? 1 : key == SDLK_LEFT ? -1 : 0, key == SDLK_DOWN ? 1 : key == SDLK_UP ? -1 : @@ -577,6 +572,7 @@ namespace streaming { bool StreamControls::pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled) { // Sampling and transport enqueueing share one owner so capture cannot race a stale gameplay packet. + controllerNavigationApplied_ = false; SDL_PumpEvents(); bool stop = false; const bool wasVisible = visible(); @@ -912,7 +908,7 @@ namespace streaming { } } - bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds) { + bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds, const SDL_Rect *clip) { const int size = std::min(bounds.w, bounds.h); if (size <= 0) { return false; @@ -940,6 +936,14 @@ namespace streaming { texture = iconTextures_.emplace(key, std::move(loaded)).first; } SDL_Rect destination {bounds.x + (bounds.w - surface->w) / 2, bounds.y + (bounds.h - surface->h) / 2, surface->w, surface->h}; + if (clip != nullptr) { + SDL_Rect intersection {}; + if (!SDL_IntersectRect(&destination, clip, &intersection)) { + return true; + } + const SDL_Rect source {intersection.x - destination.x, intersection.y - destination.y, intersection.w, intersection.h}; + return SDL_RenderCopy(renderer, texture->second.get(), &source, &intersection) == 0; + } return SDL_RenderCopy(renderer, texture->second.get(), nullptr, &destination) == 0; } @@ -966,7 +970,7 @@ namespace streaming { label(renderer, font, text, {left + capWidth + 4, bounds.y, std::max(1, groupWidth - capWidth - 4), bounds.h}, false, false, true); } - void StreamControls::render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight) { + void StreamControls::render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight, bool focusOnly) { const auto &cells = menu_.keyboard_cells(); constexpr int padding = 8; const int header = TTF_FontLineSkip(font) + 4; @@ -977,10 +981,34 @@ namespace streaming { static const std::array shortcuts {"Copy: Ctrl+C", "Paste: Ctrl+V", "Cut: Ctrl+X", "Undo: Ctrl+Z", "Redo: Ctrl+Y", "Select all: Ctrl+A", "Switch app: Alt+Tab", "Close app: Alt+F4", "Ctrl+Alt+Delete", "Desktop: Win+D", "Explorer: Win+E", "Run: Win+R"}; description = shortcuts[focusedCell.key - 106]; } - label(renderer, font, description, {padding, 2, width - padding * 2, header}, false, false, true); const int gridWidth = width - padding * 2; + // Erase both outlines before painting either key: adjacent focus rectangles + // touch the same gap pixels. No SDL renderer clipping commands are needed. + if (focusOnly) { + SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); + for (const int index : {panelSelection_, menu_.keyIndex}) { + const auto &cell = cells[index]; + const int left = padding + cell.column * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; + const int right = padding + (cell.column + cell.width) * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; + SDL_Rect area {left - 1, padding + header + cell.row * rowHeight - 1, right - left + 1, cell.height * rowHeight + 1}; + SDL_RenderFillRect(renderer, &area); + panelDamage_.push_back({area.x, area.y, area.w, area.h}); + } + } + if (!focusOnly || menu_.keyboardLayout == 2) { + SDL_Rect area {0, 0, width, header + 4}; + if (focusOnly) { + SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); + SDL_RenderFillRect(renderer, &area); + panelDamage_.push_back({area.x, area.y, area.w, area.h}); + } + label(renderer, font, description, {padding, 2, width - padding * 2, header}, false, false, true); + } const bool keyboardInput = inputDevice_ == input::UiInputDevice::keyboard; for (int i = 0; i < static_cast(cells.size()); ++i) { + if (focusOnly && i != panelSelection_ && i != menu_.keyIndex) { + continue; + } const auto &cell = cells[i]; const int left = padding + cell.column * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; const int right = padding + (cell.column + cell.width) * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; @@ -1046,6 +1074,9 @@ namespace streaming { SDL_RenderDrawRect(renderer, &focus); } } + if (focusOnly) { + return; + } const int line = TTF_FontLineSkip(font) + 8; const int footerY = padding + header + input::StreamMenu::KEYBOARD_ROWS * rowHeight + 4; const int footerHeight = std::min(line + 4, height - footerY); @@ -1085,31 +1116,64 @@ namespace streaming { } auto &panel = panel_; auto &renderer = panelRenderer_; + const PanelContent content {font, inputDevice_, menu_.page, menu_.keyboardLayout, menu_.modifiers, menu_.caps, menu_.shiftOnce, stats, mouseDevice_, keyboardTop_, message_}; + const bool focusOnly = panelContent_ && *panelContent_ == content && (keyboard || menu_.page == input::StreamMenuPage::radial); + if (focusOnly && panelSelection_ == (keyboard ? menu_.keyIndex : static_cast(menu_.selected))) { + dirty_ = false; + return; + } + panelDamage_.clear(); const Uint8 background = keyboard ? 16 : 0; - SDL_SetRenderDrawColor(renderer.get(), background, background, background, keyboard ? 255 : 150); - SDL_RenderClear(renderer.get()); + if (!focusOnly) { + SDL_SetRenderDrawColor(renderer.get(), background, background, background, keyboard ? 255 : 150); + SDL_RenderClear(renderer.get()); + } const int line = TTF_FontLineSkip(font) + 8; - if (menu_.page == input::StreamMenuPage::radial) { + if (!focusOnly && menu_.page == input::StreamMenuPage::radial) { label(renderer.get(), font, "Stream controls", {8, 4, width - 16, line}, false, false); } if (keyboard) { - render_keyboard(renderer.get(), font, width, height, rowHeight); + render_keyboard(renderer.get(), font, width, height, rowHeight, focusOnly); } else if (menu_.page == input::StreamMenuPage::radial) { const int cy = height / 2; const int radius = std::min(width / 5, (height - line * 4) / 2); for (int wheel = 0; wheel < 2; ++wheel) { const int cx = width * (wheel == 0 ? 1 : 3) / 4; - SDL_RenderFlush(renderer.get()); - rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, static_cast(menu_.selected) / 4 == wheel ? static_cast(menu_.selected) % 4 : -1); - render_icon(renderer.get(), wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}); + const int selected = static_cast(menu_.selected) / 4 == wheel ? static_cast(menu_.selected) % 4 : -1; + if (!focusOnly) { + SDL_RenderFlush(renderer.get()); + rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, selected); + render_icon(renderer.get(), wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}); + } constexpr std::array dx {0, 1, 0, -1}; constexpr std::array dy {-1, 0, 1, 0}; for (int sector = 0; sector < 4; ++sector) { + if (focusOnly && wheel * 4 + sector != panelSelection_ && wheel * 4 + sector != static_cast(menu_.selected)) { + continue; + } const auto action = static_cast(wheel * 4 + sector); const int labelWidth = std::min(line * 6, width / 5); const int x = std::clamp(cx + dx[sector] * radius * 2 / 3 - labelWidth / 2, 0, width - labelWidth); const int iconSize = std::max(18, line); const int groupY = cy + dy[sector] * radius * 2 / 3 - (iconSize + line * 2) / 2; + if (focusOnly) { + // Restore the label's complete background, including any pixels in + // the central hole, before blending its antialiased text again. + const SDL_Rect group {x, groupY, labelWidth, iconSize + 2 + line * 2}; + SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 150); + SDL_RenderFillRect(renderer.get(), &group); + SDL_RenderFlush(renderer.get()); + rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, selected, sector); + const VideoRectangle clip {group.x, group.y, group.w, group.h}; + rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, selected, -1, &clip); + render_icon(renderer.get(), wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}, &group); + const VideoRectangle area = sector == 0 ? VideoRectangle {cx - radius, cy - radius, radius * 2 + 1, radius + 1} : sector == 1 ? VideoRectangle {cx, cy - radius, radius + 1, radius * 2 + 1} : + sector == 2 ? VideoRectangle {cx - radius, cy, radius * 2 + 1, radius + 1} : + VideoRectangle {cx - radius, cy - radius, radius + 1, radius * 2 + 1}; + const int left = std::min(area.x, clip.x); + const int top = std::min(area.y, clip.y); + panelDamage_.push_back({left, top, std::max(area.x + area.width, clip.x + clip.width) - left, std::max(area.y + area.height, clip.y + clip.height) - top}); + } constexpr std::array icons {"keyboard.svg", "mouse.svg", "scroll-text.svg", "power.svg", "button-guide.svg", "button-share.svg", "camera.svg", "chart-no-axes-combined.svg"}; render_icon(renderer.get(), icons[wheel * 4 + sector], {x + (labelWidth - iconSize) / 2, groupY, iconSize, iconSize}); label(renderer.get(), font, input::stream_action_label(action, mouseDevice_ >= 0, stats), {x, groupY + iconSize + 2, labelWidth, line * 2}, false, false); @@ -1119,7 +1183,7 @@ namespace streaming { const std::array assets {"button-left-stick.svg", "button-right-stick.svg", "button-a.svg", "button-b.svg"}; const std::array keys {"Arrows", "Tab", "Enter", "Esc"}; const std::array actions {keyboardHints ? "Select" : "Left wheel", keyboardHints ? "Next" : "Right wheel", "Choose", "Resume"}; - for (int i = 0; i < 4; ++i) { + for (int i = 0; !focusOnly && i < 4; ++i) { render_hint(renderer.get(), font, assets[i], keys[i], actions[i], {i * width / 4 + 4, height - line * 2, width / 4 - 8, line + 4}); } } else if (menu_.page == input::StreamMenuPage::logs) { @@ -1160,12 +1224,22 @@ namespace streaming { render_hint(renderer.get(), font, i == 0 ? "button-b.svg" : "button-a.svg", i == 0 ? "Esc" : "Enter", i == 0 ? "Cancel" : "Disconnect", {button.x + 4, button.y, button.w - 8, button.h}); } } - if (!message_.empty() && menu_.page == input::StreamMenuPage::radial) { + if (!focusOnly && !message_.empty() && menu_.page == input::StreamMenuPage::radial) { label(renderer.get(), font, message_, {4, line + 4, width - 8, line * 2}, false, false); } SDL_RenderPresent(renderer.get()); - auto pixels = extract_overlay_bitmap(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, keyboard); - backend.set_color_overlay(width, height, std::move(pixels), keyboard ? (keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom) : VideoOverlayPlacement::full_screen); + if (focusOnly) { + for (const auto &area : panelDamage_) { + const auto offset = static_cast(area.y) * panel->pitch + area.x * 4; + const auto pixels = extract_overlay_bitmap(static_cast(panel->pixels) + offset, static_cast(panel->pitch) * height - offset, panel->pitch, area.width, area.height, keyboard); + backend.update_color_overlay(area, pixels); + } + } else { + auto pixels = extract_overlay_bitmap(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, keyboard); + backend.set_color_overlay(width, height, std::move(pixels), keyboard ? (keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom) : VideoOverlayPlacement::full_screen); + } + panelContent_ = content; + panelSelection_ = keyboard ? menu_.keyIndex : static_cast(menu_.selected); dirty_ = false; } } // namespace streaming diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 96f6c0a..9f42286 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -8,11 +8,13 @@ #include "src/input/stream_input.h" #include "src/input/stream_menu.h" #include "src/input/ui_input.h" +#include "src/streaming/video_overlay.h" #include #include #include #include +#include #include #include #include @@ -166,22 +168,24 @@ namespace streaming { */ void render_menu(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool stats); /** - * @brief Draw the Xbox-style character grid, wide space bar and tall shortcut controls. + * @brief Draw the keyboard, optionally repainting only the old and new focus cells. * @param renderer Software panel renderer. * @param font Keyboard font. * @param width Panel width. * @param height Panel height, including the helper footer. * @param rowHeight Height of one character row. + * @param focusOnly Retain unchanged keys and helpers during directional navigation. */ - void render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight); + void render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight, bool focusOnly = false); /** * @brief Draw a cached SVG raster on the current software panel. * @param renderer Panel renderer. * @param asset Icon filename under assets/icons. * @param bounds Destination bounds. + * @param clip Optional repaint rectangle, applied through source cropping rather than renderer clipping. * @return True when the icon was drawn. */ - bool render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds); + bool render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds, const SDL_Rect *clip = nullptr); /** * @brief Draw a controller glyph or keyboard keycap beside its action label. * @param renderer Panel renderer. @@ -212,6 +216,7 @@ namespace streaming { input::StreamMenu menu_; ///< Local menu state. input::KeyboardNavigation keyboardNavigation_; ///< Local keyboard repeats, independent of gameplay key forwarding. input::DirectionNavigation controllerNavigation_; ///< Immediate direction changes with the shared repeat interval. + bool controllerNavigationApplied_ = false; ///< Queued events and the polling sample can advance focus only once per input pass. bool navigationNeutralRequired_ = false; ///< Release wheel navigation before moving around a newly opened page. input::UiInputDevice inputDevice_ = input::UiInputDevice::gamepad; ///< Device used by local helper text. bool armed_ = false; ///< Opening buttons have been released. @@ -236,5 +241,12 @@ namespace streaming { std::map, CachedText> text_; ///< Bounded cache of unchanged label rasters. std::size_t textBytes_ = 0; ///< Estimated text texture bytes, bounded to protect the Xbox memory budget. std::map, std::shared_ptr> iconTextures_; ///< Renderer-owned SVG textures. + /** + * @brief Content dependencies other than the focused key or wheel sector. + */ + using PanelContent = std::tuple; + std::optional panelContent_; ///< Last fully drawn content; changes invalidate incremental focus painting. + int panelSelection_ = -1; ///< Previously painted keyboard cell or wheel sector. + std::vector panelDamage_; ///< Regions repainted during the current input pass. }; } // namespace streaming diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index 4cbb078..4f8a1fa 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -115,11 +115,16 @@ namespace streaming { const unsigned opacity = pixel[3]; const auto index = (static_cast(row) * width + column) * 4; bitmap[index + 3] = opacity; - if (opaqueBackground || opacity == 255) { - std::memcpy(bitmap.data() + index, pixel, 4); + if (opaqueBackground || opacity == 255 || (pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0)) { + // nxdk is freestanding: a four-byte std::memcpy becomes a library + // call for every pixel. Scalar byte accesses also permit unaligned + // SDL rows while allowing native compilers to combine the loads. + bitmap[index] = pixel[0]; + bitmap[index + 1] = pixel[1]; + bitmap[index + 2] = pixel[2]; // Legacy SDL blitters can round an opaque destination alpha to 254. // The OSK is already composited onto its solid panel; keep it opaque. - bitmap[index + 3] = 255; + bitmap[index + 3] = opaqueBackground ? 255 : opacity; continue; } if (opacity == 0) { @@ -135,23 +140,41 @@ namespace streaming { return bitmap; } - bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected) { - if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048 || centerX < 0 || centerX >= width || centerY < 0 || centerY >= height || radius < 1 || radius > 2048) { + bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected, int sector, const VideoRectangle *clip) { + if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048 || centerX < 0 || centerX >= width || centerY < 0 || centerY >= height || radius < 1 || radius > 2048 || sector < -1 || sector > 3) { return false; } - for (int row = std::max(0, centerY - radius); row <= std::min(height - 1, centerY + radius); ++row) { + if (clip != nullptr && (clip->x < 0 || clip->y < 0 || clip->width <= 0 || clip->height <= 0 || clip->width > width || clip->height > height || clip->x > width - clip->width || clip->y > height - clip->height)) { + return false; + } + const int firstRow = std::max({0, centerY - radius, clip != nullptr ? clip->y : 0}); + const int lastRow = std::min({height - 1, centerY + radius, clip != nullptr ? clip->y + clip->height - 1 : height - 1}); + const int firstColumn = std::max({0, centerX - radius, clip != nullptr ? clip->x : 0}); + const int lastColumn = std::min({width - 1, centerX + radius, clip != nullptr ? clip->x + clip->width - 1 : width - 1}); + for (int row = firstRow; row <= lastRow; ++row) { const int y = row - centerY; - for (int column = std::max(0, centerX - radius); column <= std::min(width - 1, centerX + radius); ++column) { + if ((sector == 0 && y > 0) || (sector == 2 && y < 0)) { + continue; + } + const int start = sector == 0 || sector == 2 ? std::max(firstColumn, centerX - std::abs(y)) : sector == 1 ? std::max(firstColumn, centerX + std::abs(y) + 1) : + firstColumn; + const int end = sector == 0 || sector == 2 ? std::min(lastColumn, centerX + std::abs(y)) : sector == 3 ? std::min(lastColumn, centerX - std::abs(y) - 1) : + lastColumn; + for (int column = start; column <= end; ++column) { const int x = column - centerX; const int distance = x * x + y * y; if (distance > radius * radius || distance < radius * radius / 9) { continue; } - const int sector = std::abs(y) >= std::abs(x) ? (y < 0 ? 0 : 2) : (x > 0 ? 1 : 3); - const std::uint32_t gray = std::abs(std::abs(x) - std::abs(y)) < 2 ? 110 : sector == selected ? 85 : - 36; - const std::uint32_t color = 0xff000000U | gray * 0x010101U; - std::memcpy(pixels + static_cast(row) * pitch + column * 4, &color, 4); + const int direction = std::abs(y) >= std::abs(x) ? (y < 0 ? 0 : 2) : (x > 0 ? 1 : 3); + if (sector >= 0 && sector != direction) { + continue; + } + const std::uint32_t gray = std::abs(std::abs(x) - std::abs(y)) < 2 ? 110 : direction == selected ? 85 : + 36; + auto *pixel = pixels + static_cast(row) * pitch + column * 4; + pixel[0] = pixel[1] = pixel[2] = gray; + pixel[3] = 255; } } return true; @@ -259,11 +282,43 @@ namespace streaming { } } + bool VideoOverlay::update_argb_bitmap(const VideoRectangle ®ion, const std::vector &pixels) { + if (argb_.empty() || region.x < 0 || region.y < 0 || region.width <= 0 || region.height <= 0 || region.width > width_ || region.height > height_ || region.x > width_ - region.width || region.y > height_ - region.height || pixels.size() != static_cast(region.width) * region.height * 4) { + return false; + } + for (int row = 0; row < region.height; ++row) { + std::memcpy(argb_.data() + (static_cast(region.y + row) * width_ + region.x) * 4, pixels.data() + static_cast(row) * region.width * 4, static_cast(region.width) * 4); + } + if (opaque_) { + for (std::size_t index = 3; index < pixels.size(); index += 4) { + if (pixels[index] != 255) { + opaque_ = false; + // Cached opaque YUY2 storage has no alpha plane. Rebuild it before + // accepting a patch that introduces transparency. + invalidate_caches(); + break; + } + } + } + if (damage_.width > 0 && damage_.height > 0) { + const int right = std::max(damage_.x + damage_.width, region.x + region.width); + const int bottom = std::max(damage_.y + damage_.height, region.y + region.height); + damage_.x = std::min(damage_.x, region.x); + damage_.y = std::min(damage_.y, region.y); + damage_.width = right - damage_.x; + damage_.height = bottom - damage_.y; + } else { + damage_ = region; + } + rgb_.clear(); + rgbBpp_ = 0; + return true; + } + std::uint32_t VideoOverlay::color_at(std::size_t index) const { if (!argb_.empty()) { - std::uint32_t color = 0; - std::memcpy(&color, argb_.data() + index * 4, 4); - return color; + const auto *pixel = argb_.data() + index * 4; + return static_cast(pixel[0]) | (static_cast(pixel[1]) << 8) | (static_cast(pixel[2]) << 16) | (static_cast(pixel[3]) << 24); } const std::uint32_t opacity = opacity_.empty() ? 255 : opacity_[index]; return (opacity << 24) | grayscale_[index] * 0x010101U; @@ -415,11 +470,12 @@ namespace streaming { const int lastRow = rebuild ? source_.height : std::min(source_.height, ((damage_.y + damage_.height) * source_.height + visibleHeight - 1) / visibleHeight); const int firstColumn = rebuild ? 0 : std::min(source_.width, (damage_.x * source_.width / visibleWidth) & ~1); const int lastColumn = rebuild ? source_.width : std::min(source_.width, ((((damage_.x + damage_.width) * source_.width + visibleWidth - 1) / visibleWidth) + 1) & ~1); + const bool nativeWidth = visibleWidth == source_.width; for (int row = firstRow; row < lastRow; ++row) { const int bitmapRow = std::min(visibleHeight - 1, row * visibleHeight / source_.height); for (int column = firstColumn; column < lastColumn; column += 2) { - const int firstColumn = std::min(visibleWidth - 1, column * visibleWidth / source_.width); - const int secondColumn = std::min(visibleWidth - 1, (column + 1) * visibleWidth / source_.width); + const int firstColumn = nativeWidth ? column : std::min(visibleWidth - 1, column * visibleWidth / source_.width); + const int secondColumn = nativeWidth ? column + 1 : std::min(visibleWidth - 1, (column + 1) * visibleWidth / source_.width); const auto firstColor = color_at(static_cast(bitmapRow) * width_ + firstColumn); const auto secondColor = color_at(static_cast(bitmapRow) * width_ + secondColumn); const auto first = rgb_to_yuv(firstColor, matrix); @@ -432,8 +488,12 @@ namespace streaming { yuy2_[offset + 2] = second[0]; // Weight shared chroma by coverage so a transparent colored pixel // cannot tint its opaque neighbor or desaturate the host underneath. - yuy2_[offset + 1] = coverage == 0 ? 128 : (first[1] * firstAlpha + second[1] * secondAlpha + coverage / 2) / coverage; - yuy2_[offset + 3] = coverage == 0 ? 128 : (first[2] * firstAlpha + second[2] * secondAlpha + coverage / 2) / coverage; + // Most wheel/key pixels are gray. Equal chroma needs no coverage + // division, including antialiased text and partially dimmed video. + yuy2_[offset + 1] = coverage == 0 ? 128 : first[1] == second[1] ? first[1] : + (first[1] * firstAlpha + second[1] * secondAlpha + coverage / 2) / coverage; + yuy2_[offset + 3] = coverage == 0 ? 128 : first[2] == second[2] ? first[2] : + (first[2] * firstAlpha + second[2] * secondAlpha + coverage / 2) / coverage; if (!opaque_) { yuy2Opacity_[offset / 2] = firstAlpha; yuy2Opacity_[offset / 2 + 1] = secondAlpha; @@ -461,7 +521,10 @@ namespace streaming { } auto *pair = destinationRow + column * 2; if (left == 255 && right == 255) { - std::memcpy(pair, foreground + column * 2, 4); + pair[0] = foreground[column * 2]; + pair[1] = foreground[column * 2 + 1]; + pair[2] = foreground[column * 2 + 2]; + pair[3] = foreground[column * 2 + 3]; continue; } pair[0] = blend_byte(pair[0], foreground[column * 2], left); diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index 5e136bf..2ae1038 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -55,9 +55,11 @@ namespace streaming { * @param centerY Wheel center Y inside the panel. * @param radius Outer radius in pixels, between 1 and 2048. * @param selected Selected clockwise sector starting at up, or -1 for none. + * @param sector Repaint only this clockwise sector, or -1 for the whole ring. + * @param clip Optional pixel rectangle restricting background restoration behind a sector label. * @return False when storage or geometry is invalid, without writing pixels. */ - bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected); + bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected, int sector = -1, const VideoRectangle *clip = nullptr); /** * @brief Placement of a shared panel inside the presented video rectangle. @@ -92,6 +94,13 @@ namespace streaming { * @param placement Anchor inside the video rectangle. */ void set_argb_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left); + /** + * @brief Replace a bounded part of an existing colored panel without scanning or copying unchanged pixels. + * @param region Rectangle in panel pixels; must be wholly inside the current panel. + * @param pixels Tightly packed straight B, G, R, A bytes for the region. + * @return True after updating pixels and invalidating the affected conversion cache, false for invalid input. + */ + bool update_argb_bitmap(const VideoRectangle ®ion, const std::vector &pixels); /** * @brief Resolve clipped panel bounds in output pixels for every presentation path. * @param destination Presented video rectangle. diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 65277d0..5a8ec9f 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -76,8 +76,8 @@ namespace { constexpr Uint8 MUTED_GREEN = 0xAB; constexpr Uint8 MUTED_BLUE = 0xB5; constexpr Sint16 TRIGGER_PAGE_SCROLL_THRESHOLD = 16000; - constexpr Sint16 LEFT_STICK_NAVIGATION_THRESHOLD = 16000; - constexpr Sint16 LEFT_STICK_NAVIGATION_RELEASE_THRESHOLD = 12000; + constexpr Sint16 LEFT_STICK_NAVIGATION_THRESHOLD = input::STICK_NAVIGATION_THRESHOLD; ///< Shared shell/OSK stick activation threshold. + constexpr Sint16 LEFT_STICK_NAVIGATION_RELEASE_THRESHOLD = input::STICK_NAVIGATION_RELEASE_THRESHOLD; ///< Shared hysteresis prevents near-center jitter. constexpr Uint32 EXIT_COMBO_HOLD_MILLISECONDS = 900U; constexpr Uint32 CONTROLLER_NAVIGATION_INITIAL_REPEAT_MILLISECONDS = input::NAVIGATION_INITIAL_REPEAT_MILLISECONDS; constexpr Uint32 CONTROLLER_NAVIGATION_REPEAT_MILLISECONDS = input::NAVIGATION_REPEAT_MILLISECONDS; diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp index f99d8b9..299f760 100644 --- a/tests/unit/input/stream_menu_test.cpp +++ b/tests/unit/input/stream_menu_test.cpp @@ -249,6 +249,33 @@ TEST(StreamMenuTest, LabelsEveryCellIndependentlyOfTheFocusedBinding) { EXPECT_EQ(menu.key_label(2), "3"); } +/** + * @brief The symbols page retains its explicit digits and punctuation when one-shot Shift or Caps is active. + */ +TEST(StreamMenuTest, ShiftDoesNotReplaceExplicitSymbolsPageKeys) { + input::StreamMenu menu; + menu.open(-1); + menu.page = input::StreamMenuPage::keyboard; + menu.toggle_keyboard_page(); + std::array labels; + for (int i = 0; i < 40; ++i) { + labels[i] = menu.key_label(i); + } + for (bool caps : {false, true}) { + menu.caps = caps; + menu.shiftOnce = true; + menu.modifiers = MODIFIER_CTRL; + for (int i = 0; i < 40; ++i) { + menu.keyIndex = i; + const auto &binding = input::StreamMenu::KEYS[menu.keyboard_cells()[i].key]; + EXPECT_EQ(menu.key_label(i), labels[i]); + EXPECT_EQ(menu.key_modifiers(menu.keyboard_key()), MODIFIER_CTRL | binding.modifiers); + } + } + menu.key_sent(); + EXPECT_FALSE(menu.shiftOnce); +} + /** * @brief D-pad navigation selects a wheel sector and logs clamp to their scroll range. */ diff --git a/tests/unit/input/ui_input_test.cpp b/tests/unit/input/ui_input_test.cpp index 76fb95a..d58b846 100644 --- a/tests/unit/input/ui_input_test.cpp +++ b/tests/unit/input/ui_input_test.cpp @@ -8,6 +8,7 @@ // standard includes #include #include +#include // local includes #include "src/input/ui_input.h" @@ -164,3 +165,38 @@ TEST(DirectionNavigationTest, ClearsOnNeutralOrResetAndHandlesClockWrap) { navigation.reset(); EXPECT_EQ(navigation.update(input::UiCommand::move_up, 54), input::UiCommand::move_up); } + +/** + * @brief Both stick axes use the main menu dead zone, retain a noisy hold and release without a second tap. + */ +TEST(DirectionNavigationTest, StickFlicksStepOnceWithHysteresisOnBothAxes) { + for (const auto &axis : {std::pair {20000, 0}, {-20000, 0}, {0, 20000}, {0, -20000}}) { + input::DirectionNavigation navigation; + const auto direction = axis.first > 0 ? input::UiCommand::move_right : axis.first < 0 ? input::UiCommand::move_left : + axis.second > 0 ? input::UiCommand::move_up : + input::UiCommand::move_down; + EXPECT_EQ(navigation.update_stick(axis.first / 2, axis.second / 2, 0), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 1), direction); + EXPECT_EQ(navigation.update_stick(axis.first * 3 / 5, axis.second * 3 / 5, 40), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(axis.first * 7 / 10, axis.second * 7 / 10, 80), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 149), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(0, 0, 150), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(axis.first / 2, axis.second / 2, 151), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 152), direction); + EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 302), direction); + } +} + +/** + * @brief Dominant axis changes respond immediately, including ties and the signed axis endpoints. + */ +TEST(DirectionNavigationTest, StickChangesDirectionWithoutWaitingForRepeat) { + input::DirectionNavigation navigation; + EXPECT_EQ(navigation.update_stick(32767, -32000, 0), input::UiCommand::move_right); + EXPECT_EQ(navigation.update_stick(32767, -32768, 1), input::UiCommand::move_down); + EXPECT_EQ(navigation.update_stick(-32768, 0, 2), input::UiCommand::move_left); + EXPECT_EQ(navigation.update_stick(16000, 16000, 3), input::UiCommand::move_up); + EXPECT_EQ(navigation.update_stick(0, 11999, 4), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(0, 15999, 5), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(0, 16000, 6), input::UiCommand::move_up); +} diff --git a/tests/unit/streaming/video_overlay_test.cpp b/tests/unit/streaming/video_overlay_test.cpp index a283249..ffca996 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -678,3 +678,110 @@ TEST(VideoOverlayTest, RebuildsCachesWhenOpacityOrMatrixChanges) { overlay.set_argb_bitmap(20, 20, pixels, streaming::VideoOverlayPlacement::full_screen); compare(streaming::VideoColorMatrix::bt709); } + +/** + * @brief Partial ARGB updates match full repaints in RGB and YUY2, including alpha changes and accumulated damage. + */ +TEST(VideoOverlayTest, AppliesSmallPatchesAcrossBothPresentationPaths) { + for (const int scale : {12, 32, 80}) { + streaming::VideoOverlay overlay; + std::vector pixels(20 * 16 * 4, 255); + overlay.set_argb_bitmap(20, 16, pixels, streaming::VideoOverlayPlacement::full_screen); + for (int step = 0; step < 6; ++step) { + const streaming::VideoRectangle area {step * 3, step * 2, 3, 2}; + std::vector patch(area.width * area.height * 4, 0); + for (std::size_t i = 0; i < patch.size(); i += 4) { + patch[i] = step * 30; + patch[i + 1] = 40; + patch[i + 2] = 210; + patch[i + 3] = step % 3 == 0 ? 255 : step % 3 == 1 ? 150 : + 0; + } + for (int row = 0; row < area.height; ++row) { + std::copy_n(patch.data() + row * area.width * 4, area.width * 4, pixels.data() + ((area.y + row) * 20 + area.x) * 4); + } + ASSERT_TRUE(overlay.update_argb_bitmap(area, patch)); + EXPECT_EQ(overlay.argb_pixels(), pixels); + // Accumulate two updates before presentation as a focus change does. + if (step % 2 == 0) { + continue; + } + streaming::VideoOverlay fresh; + fresh.set_argb_bitmap(20, 16, pixels, streaming::VideoOverlayPlacement::full_screen); + const streaming::VideoRectangle destination {0, 0, scale, scale}; + const streaming::VideoRectangle rgbDestination {0, 0, std::min(32, scale), std::min(32, scale)}; + for (int bpp : {15, 16, 32}) { + const int bytes = bpp == 32 ? 4 : 2; + std::vector actual(32 * 32 * bytes, 100); + auto expected = actual; + ASSERT_TRUE(overlay.draw_rgb(actual.data(), actual.size(), 32 * bytes, 32, 32, bpp, rgbDestination)); + ASSERT_TRUE(fresh.draw_rgb(expected.data(), expected.size(), 32 * bytes, 32, 32, bpp, rgbDestination)); + EXPECT_EQ(actual, expected); + } + for (const auto matrix : {streaming::VideoColorMatrix::bt601, streaming::VideoColorMatrix::bt709}) { + std::vector actual(32 * 32 * 2, 100); + auto expected = actual; + ASSERT_TRUE(overlay.draw_yuy2(actual.data(), actual.size(), 64, 32, 32, destination, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 64, 32, 32, destination, matrix)); + EXPECT_EQ(actual, expected) << "step=" << step << " scale=" << scale; + } + } + } +} + +/** + * @brief Invalid patch rectangles, storage and non-ARGB panels are rejected without changing any pixels. + */ +TEST(VideoOverlayTest, RejectsInvalidColorPatchesWithoutChangingThePanel) { + streaming::VideoOverlay overlay; + EXPECT_FALSE(overlay.update_argb_bitmap({0, 0, 1, 1}, {1, 2, 3, 255})); + overlay.set_bitmap(2, 2, {1, 2, 3, 4}); + EXPECT_FALSE(overlay.update_argb_bitmap({0, 0, 1, 1}, {1, 2, 3, 255})); + const std::vector original(4 * 4 * 4, 255); + overlay.set_argb_bitmap(4, 4, original); + for (const auto area : {streaming::VideoRectangle {-1, 0, 1, 1}, {0, -1, 1, 1}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 5, 1}, {0, 0, 1, 5}, {4, 0, 1, 1}, {0, 4, 1, 1}, {1, 1, 4, 4}}) { + EXPECT_FALSE(overlay.update_argb_bitmap(area, {1, 2, 3, 255})); + EXPECT_EQ(overlay.argb_pixels(), original); + } + EXPECT_FALSE(overlay.update_argb_bitmap({0, 0, 1, 1}, {1, 2, 3})); + EXPECT_EQ(overlay.argb_pixels(), original); +} + +/** + * @brief Repainting only the old and new wheel sectors is identical to rasterizing the complete ring. + */ +TEST(VideoOverlayTest, RepaintsWheelSectorsAndClippedLabelBackgrounds) { + std::vector pixels(40 * 40 * 4, 150); + const auto backdrop = pixels; + int previous = -1; + for (int selected : {0, 1, 2, 3, -1, 2, 0}) { + if (previous >= 0) { + ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, selected, previous)); + } + if (selected >= 0) { + ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, selected, selected)); + } + auto expected = backdrop; + ASSERT_TRUE(streaming::rasterize_menu_wheel(expected.data(), expected.size(), 160, 40, 40, 20, 20, 18, selected)); + // Initialize the unchanged sectors on the first frame. + if (previous == -1 && selected == 0) { + pixels = expected; + } + EXPECT_EQ(pixels, expected); + const streaming::VideoRectangle clip {17, 2, 6, 6}; + auto clipped = backdrop; + ASSERT_TRUE(streaming::rasterize_menu_wheel(clipped.data(), clipped.size(), 160, 40, 40, 20, 20, 18, selected, -1, &clip)); + for (int y = 0; y < 40; ++y) { + for (int x = 0; x < 40; ++x) { + const int index = (y * 40 + x) * 4; + EXPECT_EQ(clipped[index], x >= clip.x && x < clip.x + clip.width && y >= clip.y && y < clip.y + clip.height ? expected[index] : backdrop[index]); + } + } + previous = selected; + } + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, 0, -2)); + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, 0, 4)); + for (const auto clip : {streaming::VideoRectangle {-1, 0, 1, 1}, {0, -1, 1, 1}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 41, 1}, {0, 0, 1, 41}, {40, 0, 1, 1}, {0, 40, 1, 1}}) { + EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, 0, -1, &clip)); + } +} From f946a36bfff3c455c6fde2bc0bf8b0091cf82444 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:30:31 -0500 Subject: [PATCH 04/22] feat(settings): add screenshot folder picker and creation --- README.md | 11 +- src/app/client_state.cpp | 177 +++++++++++++- src/app/client_state.h | 36 +++ src/platform/filesystem_utils.cpp | 200 +++++++++++++++ src/platform/filesystem_utils.h | 48 +++- src/ui/shell_screen.cpp | 81 +++++- src/ui/shell_view.cpp | 66 +++++ src/ui/shell_view.h | 9 +- tests/unit/app/directory_picker_test.cpp | 231 ++++++++++++++++++ tests/unit/platform/filesystem_utils_test.cpp | 118 ++++++++- 10 files changed, 956 insertions(+), 21 deletions(-) create mode 100644 tests/unit/app/directory_picker_test.cpp diff --git a/README.md b/README.md index 2cd2db1..b667c68 100644 --- a/README.md +++ b/README.md @@ -130,9 +130,14 @@ surface as the main menus, accepts **A / Enter** to disconnect and **B / Esc** t the host app running after disconnecting. Returning from the stream resets local keyboard/controller holds before library navigation resumes. -Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Change -`screenshot_directory` in the `[streaming]` section of Moonlight's `moonlight.toml` to use another writable -folder; the folder is created when the first screenshot is saved. An empty value restores the default. +Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Open +**Settings > Display > Screenshot Folder** to browse drives and subfolders, select the current folder, +or create a new folder with the on-screen keyboard or a USB keyboard. **Use default location** restores +the title-scoped screenshot folder. **B / Esc** cancels without changing the saved destination. +New folder names use up to 42 printable characters; **Start / Enter** creates the folder. The destination +is checked for write access before it is saved. You can also change `screenshot_directory` in the +`[streaming]` section of `moonlight.toml`; a configured folder is created when the first screenshot is +saved, and an empty value restores the default. Use a TOML literal string for Xbox paths, for example `screenshot_directory = 'F:\Screenshots'`. The same section stores `keyboard_dock_top = false` (bottom) or `true` (top). diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 53ab593..df4a4d5 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -15,6 +15,8 @@ // local includes #include "src/app/stream_bitrate.h" #include "src/network/host_pairing.h" +#include "src/platform/filesystem_utils.h" +#include "src/startup/storage_paths.h" namespace { @@ -536,6 +538,11 @@ namespace { return {}; } + /** + * @brief Build the active settings options, including the screenshot destination picker. + * @param state Current screen and settings values. + * @return Selectable settings rows for the current category. + */ std::vector build_detail_menu_for_state(const app::ClientState &state) { if (state.shell.activeScreen != app::ScreenId::settings) { return {}; @@ -586,6 +593,7 @@ namespace { "Show performance statistics during playback and after the stream ends.", true, }, + {"screenshot-directory", "Screenshot Folder: " + (state.settings.screenshotDirectory.empty() ? std::string("Default") : state.settings.screenshotDirectory), "Choose or create a folder for stream screenshots. Default: " + startup::default_storage_path("screenshots"), true}, }; case app::SettingsCategory::input: return { @@ -637,7 +645,14 @@ namespace { } } + /** + * @brief Close the active dialog and release staged picker data before streaming can begin. + * @param state Client state containing the dialog and any temporary folder listing. + */ void close_modal(app::ClientState &state) { + if (state.modal.id == app::ModalId::directory_picker) { + state.directoryPicker = {}; + } state.modal = {}; reset_confirmation(state); } @@ -713,6 +728,11 @@ namespace { state.settings.logViewerScrollOffset = state.settings.logViewerScrollOffset > step ? state.settings.logViewerScrollOffset - step : 0U; } + /** + * @brief Count available dialog rows, including directory entries and picker actions. + * @param state Current modal and staged folder list. + * @return Number of focusable action positions. + */ std::size_t modal_action_count(const app::ClientState &state) { switch (state.modal.id) { case app::ModalId::host_actions: @@ -721,6 +741,8 @@ namespace { return 3U; case app::ModalId::confirmation: return 2U; + case app::ModalId::directory_picker: + return state.directoryPicker.folders.size() + 4U; case app::ModalId::none: case app::ModalId::support: case app::ModalId::host_details: @@ -1486,6 +1508,7 @@ namespace { close_modal_and_mark_closed(state, update); return true; case app::ModalId::log_viewer: + case app::ModalId::directory_picker: return true; case app::ModalId::confirmation: return handle_confirmation_modal_activation(state, update); @@ -1500,6 +1523,119 @@ namespace { return false; } + /** + * @brief Keep folder browsing and local text editing separate from saved settings until acceptance. + * @param state Active client and picker state. + * @param command Controller or keyboard action. + * @param update Receives requested filesystem operations and persistence effects. + * @return True when the directory picker owns this command. + */ + bool handle_directory_picker_command(app::ClientState &state, input::UiCommand command, app::AppUpdate *update) { + if (state.modal.id != app::ModalId::directory_picker) { + return false; + } + auto &picker = state.directoryPicker; + if (picker.creating) { + auto &keyboard = picker.keyboard; + if (command == input::UiCommand::back) { + picker.creating = false; + picker.error.clear(); + } else if (command == input::UiCommand::move_up || command == input::UiCommand::move_down || command == input::UiCommand::move_left || command == input::UiCommand::move_right) { + keyboard.navigate(command == input::UiCommand::move_left ? -1 : command == input::UiCommand::move_right ? 1 : + 0, + command == input::UiCommand::move_up ? -1 : command == input::UiCommand::move_down ? 1 : + 0); + } else if (command == input::UiCommand::previous_page || command == input::UiCommand::next_page) { + keyboard.keyboardLayout = keyboard.keyboardLayout == 0 ? 1 : 0; + } else if (command == input::UiCommand::open_context_menu) { + keyboard.toggle_shift(); + } else if (command == input::UiCommand::delete_character) { + if (picker.caret > 0) { + picker.name.erase(--picker.caret, 1); + } + } else if (command == input::UiCommand::activate) { + const int index = keyboard.keyboard_cells()[keyboard.keyIndex].key; + if (index == input::StreamMenu::KEYBOARD_PAGE_KEY) { + keyboard.keyboardLayout = keyboard.keyboardLayout == 0 ? 1 : 0; + } else { + const int code = keyboard.keyboard_key(); + if (code == 0x0D) { + return handle_directory_picker_command(state, input::UiCommand::confirm, update); + } + if (code == 0x08) { + return handle_directory_picker_command(state, input::UiCommand::delete_character, update); + } + if (code == 0x125 && picker.caret > 0) { + --picker.caret; + } else if (code == 0x127 && picker.caret < picker.name.size()) { + ++picker.caret; + } else if (code == 0x20) { + app::append_directory_name(state, " "); + keyboard.key_sent(); + } else if (code > 0 && keyboard.key_label(keyboard.keyIndex).size() == 1) { + app::append_directory_name(state, keyboard.key_label(keyboard.keyIndex)); + keyboard.key_sent(); + } + } + } else if (command == input::UiCommand::confirm) { + if (platform::valid_directory_name(picker.name)) { + update->requests.directoryCreateRequested = true; + } else { + picker.error = "Use 1-42 characters, without reserved characters or a trailing space or period."; + } + } + return true; + } + if (command == input::UiCommand::back) { + close_modal_and_mark_closed(state, update); + return true; + } + if (command == input::UiCommand::move_up || command == input::UiCommand::move_down) { + move_modal_selection(state, command == input::UiCommand::move_up ? -1 : 1); + return true; + } + if (command == input::UiCommand::move_left || command == input::UiCommand::previous_page) { + state.modal.selectedActionIndex = 2; + command = input::UiCommand::activate; + } + if (command == input::UiCommand::open_context_menu) { + state.modal.selectedActionIndex = 0; + command = input::UiCommand::activate; + } + if (command != input::UiCommand::activate && command != input::UiCommand::confirm) { + return true; + } + const auto selected = state.modal.selectedActionIndex; + if (selected == 0 && picker.available && !picker.path.empty()) { + update->requests.directorySelectRequested = true; + } else if (selected == 1 && picker.available && !picker.path.empty()) { + picker.creating = true; + picker.name.clear(); + picker.caret = 0; + picker.error.clear(); + picker.keyboard.open(-1); + picker.keyboard.page = input::StreamMenuPage::keyboard; + } else if (selected == 2 && !picker.path.empty()) { + picker.path = platform::directory_parent(picker.path); + update->requests.directoryListRequested = true; + state.modal.selectedActionIndex = 0; + } else if (selected == 3) { + app::accept_screenshot_directory(state, *update, {}); + } else if (selected >= 4 && selected - 4 < picker.folders.size()) { + picker.path = picker.folders[selected - 4]; + update->requests.directoryListRequested = true; + state.modal.selectedActionIndex = 0; + } + return true; + } + + /** + * @brief Route local dialog input before allowing the underlying screen to navigate. + * @param state Current dialog and shell state. + * @param command Abstract controller or keyboard input. + * @param update Receives modal lifecycle and requested operations. + * @return True when an active modal consumes the command. + */ bool handle_modal_command(app::ClientState &state, input::UiCommand command, app::AppUpdate *update) { if (!state.modal.active()) { return false; @@ -1509,6 +1645,10 @@ namespace { return true; } + if (handle_directory_picker_command(state, command, update)) { + return true; + } + if (handle_log_viewer_modal_command(state, command, update)) { return true; } @@ -2158,7 +2298,7 @@ namespace app { } /** - * @brief Update settings inline without producing routine confirmation notifications. + * @brief Update settings inline or open the screenshot folder picker, emitting only required side effects. * * @param state Client state containing the settings menus. * @param detailUpdate Activated detail-menu update. @@ -2238,6 +2378,14 @@ namespace app { rebuild_menu(state, "toggle-show-performance-stats"); return; } + if (detailUpdate.activatedItemId == "screenshot-directory") { + state.directoryPicker = {}; + state.directoryPicker.path = state.settings.screenshotDirectory.empty() ? startup::default_storage_path("screenshots") : state.settings.screenshotDirectory; + open_modal(state, ModalId::directory_picker); + update->requests.directoryListRequested = true; + update->navigation.modalOpened = true; + return; + } if (detailUpdate.activatedItemId == "factory-reset") { open_confirmation( state, @@ -2603,6 +2751,33 @@ namespace app { } } + void append_directory_name(ClientState &state, std::string_view text) { + auto &picker = state.directoryPicker; + if (state.modal.id != ModalId::directory_picker || !picker.creating) { + return; + } + picker.caret = std::min(picker.caret, picker.name.size()); + for (unsigned char character : text) { + if (picker.name.size() < 42 && character >= 32 && character < 127) { + picker.name.insert(picker.caret++, 1, static_cast(character)); + } + } + picker.error.clear(); + } + + void accept_screenshot_directory(ClientState &state, AppUpdate &update, const std::string &path) { + if (state.modal.id != ModalId::directory_picker) { + return; + } + if (state.settings.screenshotDirectory != path) { + state.settings.screenshotDirectory = path; + state.settings.dirty = true; + update.persistence.settingsChanged = true; + } + close_modal_and_mark_closed(state, &update); + rebuild_menu(state, "screenshot-directory"); + } + AppUpdate handle_command(ClientState &state, input::UiCommand command) { AppUpdate update {}; diff --git a/src/app/client_state.h b/src/app/client_state.h index 8f75e25..c957ed8 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -9,6 +9,7 @@ #include #include #include +#include #include // nxdk includes @@ -18,6 +19,7 @@ #include "src/app/host_records.h" #include "src/app/pairing_flow.h" #include "src/input/navigation_input.h" +#include "src/input/stream_menu.h" #include "src/input/ui_input.h" #include "src/logging/logger.h" #include "src/startup/saved_files.h" @@ -58,6 +60,7 @@ namespace app { confirmation, ///< Destructive-action confirmation dialog. log_viewer, ///< Dedicated log viewer modal. connection_test, ///< Progress and result of a manual connection test. + directory_picker, ///< Screenshot destination browser and local folder-name keyboard. }; /** @@ -345,6 +348,20 @@ namespace app { bool refreshFailureNotified = false; ///< Suppress repeated cached-library refresh warnings until recovery. }; + /** + * @brief Staged directory selection; the saved setting changes only after successful selection. + */ + struct DirectoryPickerState { + std::string path; ///< Current directory, or empty for mounted drives. + std::vector folders; ///< Immediate child directory paths. + std::string error; ///< Listing, creation or destination validation feedback. + bool available = false; ///< The current directory was read successfully. + bool creating = false; ///< Show the local folder-name keyboard. + std::string name; ///< Draft child folder name. + std::size_t caret = 0; ///< Insertion point inside the folder name. + input::StreamMenu keyboard; ///< Reuse the stream keyboard's pages, labels and focus geometry locally. + }; + /** * @brief State owned by the settings, log viewer, and saved-file workflows. */ @@ -386,6 +403,7 @@ namespace app { PairingDraft pairingDraft; ///< Draft state for the active pairing workflow. ModalState modal; ///< Context modal currently stacked over the shell. SettingsState settings; ///< Settings, log viewer, and saved-file workflow state. + DirectoryPickerState directoryPicker; ///< Pending screenshot-directory workflow. ConfirmationDialogState confirmation; ///< Confirmation dialog content for destructive actions. }; @@ -418,6 +436,9 @@ namespace app { bool appsBrowseShowHidden = false; ///< Hidden-app visibility requested for the app browse action. bool streamLaunchRequested = false; ///< True when the selected host app should start or resume streaming. bool logViewRequested = false; ///< True when the log viewer should be refreshed from disk. + bool directoryListRequested = false; ///< Read the picker's current directory or mounted drives. + bool directoryCreateRequested = false; ///< Create the named child of the current picker directory. + bool directorySelectRequested = false; ///< Check destination writability before accepting the picker directory. }; /** @@ -668,4 +689,19 @@ namespace app { */ AppUpdate handle_command(ClientState &state, input::UiCommand command); + /** + * @brief Insert printable text into the local folder-name draft, without forwarding it to a host. + * @param state Current client state. + * @param text ASCII text to append, limited to the Xbox component length. + */ + void append_directory_name(ClientState &state, std::string_view text); + + /** + * @brief Complete a validated directory selection and rebuild its settings label. + * @param state Current client state. + * @param update Receives persistence and modal closure flags. + * @param path Selected folder; empty restores the title-scoped default. + */ + void accept_screenshot_directory(ClientState &state, AppUpdate &update, const std::string &path); + } // namespace app diff --git a/src/platform/filesystem_utils.cpp b/src/platform/filesystem_utils.cpp index a4a5f70..505d875 100644 --- a/src/platform/filesystem_utils.cpp +++ b/src/platform/filesystem_utils.cpp @@ -6,8 +6,10 @@ #include "src/platform/filesystem_utils.h" // standard includes +#include #include #include +#include #include #include @@ -18,12 +20,38 @@ extern "C" { #include } #else + #include + #include #include #include + #include +#endif +#ifdef NXDK + #include #endif namespace { +#ifdef NXDK + /** + * @brief Mount a standard Xbox partition alias on first use, retaining dashboard-provided aliases. + * @param path Absolute folder whose drive may not have been mounted during direct disc boot. + */ + void mount_directory_drive(std::string_view path) { + if (path.size() < 2 || path[1] != ':') { + return; + } + constexpr std::string_view letters = "CEFGXYZ"; + constexpr int partitions[] = {2, 1, 6, 7, 3, 4, 5}; + const char letter = static_cast(std::toupper(static_cast(path[0]))); + const auto index = letters.find(letter); + if (index != std::string_view::npos && !nxIsDriveMounted(letter)) { + const auto target = std::string("\\Device\\Harddisk0\\Partition") + std::to_string(partitions[index]) + "\\"; + nxMountDrive(letter, target.c_str()); + } + } +#endif + bool is_root_path(std::string_view path) { #if defined(_WIN32) || defined(NXDK) return path.size() >= 2 && path.size() <= 3 && path[1] == ':'; @@ -71,6 +99,175 @@ namespace { namespace platform { + DirectoryListing list_directories(const std::string &path) { + DirectoryListing result; +#ifdef NXDK + mount_directory_drive(path); +#endif +#if defined(_WIN32) || defined(NXDK) + if (path.empty()) { + #ifdef NXDK + // nxdk normally automounts only D:. Expose existing standard disk + // partitions without replacing aliases supplied by a dashboard. + for (const char letter : std::string_view("CEFGXYZ")) { + mount_directory_drive(std::string(1, letter) + ":\\"); + } + #endif + const DWORD drives = GetLogicalDrives(); + for (int i = 0; i < 26; ++i) { + const std::string root = std::string(1, static_cast('A' + i)) + ":\\"; + const auto attributes = (drives & (1UL << i)) != 0 ? GetFileAttributesA(root.c_str()) : INVALID_FILE_ATTRIBUTES; + if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { + result.paths.push_back(root); + } + } + return result; + } + const DWORD attributes = GetFileAttributesA(path.c_str()); + if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { + result.error = "Folder is unavailable: " + path; + return result; + } + WIN32_FIND_DATAA data {}; + const auto search = join_path(path, "*"); + HANDLE handle = FindFirstFileA(search.c_str(), &data); + if (handle == INVALID_HANDLE_VALUE) { + if (GetLastError() != ERROR_FILE_NOT_FOUND && GetLastError() != ERROR_NO_MORE_FILES) { + result.error = "Unable to read folder (error " + std::to_string(GetLastError()) + ")."; + } + return result; + } + do { + const std::string name = data.cFileName; + if (name != "." && name != ".." && (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { + result.paths.push_back(join_path(path, name)); + } + } while (FindNextFileA(handle, &data)); + const DWORD readError = GetLastError(); + FindClose(handle); + if (readError != ERROR_NO_MORE_FILES) { + result.error = "Unable to finish reading folder (error " + std::to_string(readError) + ")."; + } +#else + if (path.empty()) { + result.paths = {"/"}; + return result; + } + DIR *directory = opendir(path.c_str()); + if (directory == nullptr) { + result.error = "Unable to read folder: " + std::string(std::strerror(errno)); + return result; + } + errno = 0; + while (const auto *entry = readdir(directory)) { + const std::string name = entry->d_name; + if (name != "." && name != "..") { + const auto child = join_path(path, name); + struct stat status {}; + if (stat(child.c_str(), &status) == 0 && S_ISDIR(status.st_mode)) { + result.paths.push_back(child); + } + } + errno = 0; + } + const int readError = errno; + closedir(directory); + if (readError != 0) { + result.error = "Unable to finish reading folder: " + std::string(std::strerror(readError)); + } +#endif + std::sort(result.paths.begin(), result.paths.end()); + return result; + } + + std::string directory_parent(std::string path) { + while (path.size() > 1 && is_path_separator(path.back()) && !(path.size() == 3 && path[1] == ':')) { + path.pop_back(); + } + if (path == "/" || path == "\\" || (path.size() >= 2 && path.size() <= 3 && path[1] == ':')) { + return {}; + } + const auto index = path.find_last_of("\\/"); + if (index == std::string::npos) { + return {}; + } + return path.substr(0, index == 0 || (index == 2 && path[1] == ':') ? index + 1 : index); + } + + bool valid_directory_name(std::string_view name) { + return !name.empty() && name.size() <= 42 && name != "." && name != ".." && name.back() != ' ' && name.back() != '.' && std::none_of(name.begin(), name.end(), [](unsigned char character) { + return character < 32 || character >= 127 || std::string_view("<>:\"/\\|?*+=,;[]").find(static_cast(character)) != std::string_view::npos; + }); + } + + bool create_child_directory(const std::string &parent, const std::string &name, std::string &error) { + error.clear(); + if (parent.empty() || !valid_directory_name(name)) { + error = "Use a name of 1-42 printable characters without reserved characters or a trailing space or period."; + return false; + } +#ifdef NXDK + mount_directory_drive(parent); +#endif + const auto child = join_path(parent, name); +#if defined(_WIN32) || defined(NXDK) + const int result = _mkdir(child.c_str()); +#else + const int result = mkdir(child.c_str(), 0750); +#endif + if (result != 0) { + error = errno == EEXIST ? "A file or folder with this name already exists." : "Could not create folder: " + std::string(std::strerror(errno)); + return false; + } + return true; + } + + bool directory_is_writable(const std::string &path, std::string &error) { + error.clear(); +#ifdef NXDK + mount_directory_drive(path); +#endif + if (path.empty()) { + error = "Choose a folder first."; + return false; + } + for (int attempt = 0; attempt < 32; ++attempt) { + const auto probe = join_path(path, "moonlight-write-" + std::to_string(attempt + 1) + ".tmp"); +#if defined(_WIN32) || defined(NXDK) + HANDLE file = CreateFileA(probe.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); + if (file == INVALID_HANDLE_VALUE) { + if (GetLastError() == ERROR_FILE_EXISTS || GetLastError() == ERROR_ALREADY_EXISTS) { + continue; + } + error = "Cannot write to this folder (error " + std::to_string(GetLastError()) + ")."; + return false; + } + CloseHandle(file); + if (!DeleteFileA(probe.c_str())) { + error = "Cannot remove temporary files in this folder."; + return false; + } +#else + const int file = open(probe.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0600); + if (file < 0) { + if (errno == EEXIST) { + continue; + } + error = "Cannot write to this folder: " + std::string(std::strerror(errno)); + return false; + } + close(file); + if (unlink(probe.c_str()) != 0) { + error = "Cannot remove temporary files in this folder."; + return false; + } +#endif + return true; + } + error = "Could not create a unique temporary file in this folder."; + return false; + } + char preferred_path_separator() { #if defined(_WIN32) || defined(NXDK) return '\\'; @@ -110,6 +307,9 @@ namespace platform { } bool ensure_directory_exists(const std::string &directoryPath, std::string *errorMessage) { +#ifdef NXDK + mount_directory_drive(directoryPath); +#endif if (directoryPath.empty()) { return true; } diff --git a/src/platform/filesystem_utils.h b/src/platform/filesystem_utils.h index 994a85b..7663f08 100644 --- a/src/platform/filesystem_utils.h +++ b/src/platform/filesystem_utils.h @@ -8,9 +8,55 @@ #include #include #include +#include namespace platform { + /** + * @brief Immediate subdirectories, or mounted drive roots, available in the folder picker. + */ + struct DirectoryListing { + std::vector paths; ///< Sorted full paths; files and dot entries are excluded. + std::string error; ///< Empty on success, including an empty directory. + }; + + /** + * @brief List child directories, or mounted drives when the path is empty. + * @param path Directory to inspect; empty selects the drive list. + * @return Child paths and any enumeration failure. + */ + DirectoryListing list_directories(const std::string &path); + + /** + * @brief Find the parent folder without turning a drive root into a drive-relative path. + * @param path Folder path, optionally ending in separators. + * @return Parent folder, or empty to return to the drive list. + */ + std::string directory_parent(std::string path); + + /** + * @brief Validate a single printable folder name using the Xbox's 42-byte component limit. + * @param name Proposed name, without its parent directory. + * @return True for a usable component, excluding traversal and reserved characters. + */ + bool valid_directory_name(std::string_view name); + /** + * @brief Create one new child folder, refusing existing names and multi-component paths. + * @param parent Existing parent folder. + * @param name Valid single-component child name. + * @param error Receives creation or validation failures; cleared on success. + * @return True only when a new directory was created. + */ + bool create_child_directory(const std::string &parent, const std::string &name, std::string &error); + + /** + * @brief Verify a destination by creating and removing a unique empty file. + * @param path Existing destination directory. + * @param error Receives an actionable failure; cleared on success. + * @return True when the directory permits creating and deleting files. + */ + bool directory_is_writable(const std::string &path, std::string &error); + /** * @brief Return the preferred path separator for the active platform. * @@ -52,7 +98,7 @@ namespace platform { std::string file_name_from_path(std::string_view path); /** - * @brief Ensure that a directory path exists, creating missing segments as needed. + * @brief Ensure a directory exists, mounting its standard Xbox drive and creating missing segments as needed. * * @param directoryPath Directory path to create. * @param errorMessage Optional output for creation failures. diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 5a8ec9f..5e8d1ff 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -28,6 +28,7 @@ // local includes #include "src/app/settings_storage.h" #include "src/input/navigation_input.h" +#include "src/input/stream_input.h" #include "src/logging/log_file.h" #include "src/logging/logger.h" #include "src/network/host_discovery.h" @@ -2302,11 +2303,47 @@ namespace { } } + /** + * @brief Execute explicit folder-picker requests and accept only writable screenshot destinations. + * @param state Reducer state, including the staged picker directory and name. + * @param update Requests to execute and persistence flags to set on acceptance. + */ + void process_directory_picker_requests(app::ClientState &state, app::AppUpdate &update) { + auto &picker = state.directoryPicker; + if (update.requests.directorySelectRequested) { + if (platform::directory_is_writable(picker.path, picker.error)) { + app::accept_screenshot_directory(state, update, picker.path); + } + } + if (update.requests.directoryCreateRequested && platform::create_child_directory(picker.path, picker.name, picker.error)) { + picker.path = platform::join_path(picker.path, picker.name); + picker.creating = false; + state.modal.selectedActionIndex = 0; + update.requests.directoryListRequested = true; + } + if (update.requests.directoryListRequested) { + auto listing = platform::list_directories(picker.path); + // A configured/default folder can have been removed, or may not have + // received its first screenshot yet. Start at its nearest existing parent. + while (update.navigation.modalOpened && !listing.error.empty() && !picker.path.empty()) { + picker.path = platform::directory_parent(picker.path); + listing = platform::list_directories(picker.path); + } + picker.folders = std::move(listing.paths); + picker.error = std::move(listing.error); + picker.available = picker.error.empty(); + if (picker.path.empty()) { + state.modal.selectedActionIndex = picker.folders.empty() ? 3 : 4; + } + } + } + /** * @brief Snapshot persisted shell and stream preferences, including screenshot storage and OSK placement. * @param state Current client state. * @return Settings snapshot ready for TOML serialization. */ + app::AppSettings persistent_settings_from_state(const app::ClientState &state) { return { state.settings.loggingLevel, @@ -3774,7 +3811,7 @@ namespace { } /** - * @brief Render a content-sized dialog with a status icon and contained actions or progress. + * @brief Render a content-sized dialog with persistent guidance, scrolling actions or progress. * * @param renderer Active renderer. * @param titleFont Dialog heading font. @@ -3808,7 +3845,10 @@ namespace { } const int footerY = bounds.y + bounds.h - padding - footerHeight; int cursorY = bounds.y + padding + titleHeight + 10; - const int actionSpace = std::min(actionsHeight, std::max(0, footerY - cursorY - 12)); + // Reserve the directory path / dialog guidance before sizing a scrolling + // action list, so a large folder cannot push its own breadcrumb offscreen. + const int availableHeight = std::max(0, footerY - cursorY - 12); + const int actionSpace = std::min(actionsHeight, std::max(0, availableHeight - std::min(textHeight, availableHeight / 3))); const int textBottom = footerY - actionSpace - 12; for (const auto &line : modal.lines) { int drawnHeight = 0; @@ -3831,7 +3871,7 @@ namespace { } /** - * @brief Render numeric entry with compact centered IPv4 sections and key columns. + * @brief Render local numeric or text entry with cached labels and optional shared keyboard geometry. * * @param renderer Active renderer. * @param bodyFont Heading, input and key font. @@ -3848,7 +3888,8 @@ namespace { const int dialogWidth = std::min(720, width * 4 / 5); const int innerWidth = dialogWidth - padding * 2; const int columns = std::max(1, static_cast(viewModel.keypad.columnCount)); - const int rows = std::max(1, static_cast((viewModel.keypad.buttons.size() + columns - 1) / columns)); + const bool keyboardGeometry = viewModel.keypad.rowCount != 0; + const int rows = keyboardGeometry ? static_cast(viewModel.keypad.rowCount) : std::max(1, static_cast((viewModel.keypad.buttons.size() + columns - 1) / columns)); const int gap = 8; const int titleHeight = TTF_FontLineSkip(bodyFont) + 14; const int hintHeight = keypad_modal_text_height(renderer, smallFont, viewModel, innerWidth, cache); @@ -3904,8 +3945,13 @@ namespace { cache->buttonLabelTextures.resize(viewModel.keypad.buttons.size()); for (std::size_t index = 0; index < viewModel.keypad.buttons.size(); ++index) { const auto &column = keyColumns[index % columns]; - const SDL_Rect key {column.x, keysY + static_cast(index / columns) * (keyHeight + gap), column.w, keyHeight}; + SDL_Rect key {column.x, keysY + static_cast(index / columns) * (keyHeight + gap), column.w, keyHeight}; const auto &button = viewModel.keypad.buttons[index]; + if (keyboardGeometry) { + const int left = bounds.x + padding + button.column * innerWidth / columns; + const int right = bounds.x + padding + (button.column + button.width) * innerWidth / columns; + key = {left + 1, keysY + button.row * (keyHeight + gap), right - left - 3, button.height * (keyHeight + gap) - gap}; + } render_card_surface(renderer, key, button.selected); if (!ensure_single_line_text_texture(renderer, bodyFont, button.label, {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, key.w, &cache->buttonLabelTextures[index]) || !render_cached_centered_text_texture(renderer, cache->buttonLabelTextures[index], key)) { return false; @@ -4395,6 +4441,26 @@ namespace { state.shell.shouldExit = true; return input::UiCommand::none; } + if (state.modal.id == app::ModalId::directory_picker && state.directoryPicker.creating && event.key.keysym.sym >= 32 && event.key.keysym.sym < 127 && (event.key.keysym.mod & (KMOD_CTRL | KMOD_ALT | KMOD_GUI)) == 0) { + const int code = input::keyboard_virtual_key(event.key.keysym.scancode); + if (code == 0x20) { + app::append_directory_name(state, " "); + } else { + for (std::size_t i = 0; i < 60; ++i) { + const auto &key = input::StreamMenu::KEYS[i]; + if (key.code != code) { + continue; + } + const bool shifted = ((event.key.keysym.mod & KMOD_SHIFT) != 0) != (code >= 'A' && code <= 'Z' && (event.key.keysym.mod & KMOD_CAPS) != 0); + const std::string text = shifted ? key.shifted : key.label; + if (text.size() == 1) { + app::append_directory_name(state, text); + } + break; + } + } + return input::UiCommand::none; + } return inputState->keyboardNavigation.press(event.key.keysym.scancode, translate_keyboard_key(event.key.keysym.sym, event.key.keysym.mod), SDL_GetTicks()); case SDL_KEYUP: if (inputState != nullptr) { @@ -4873,7 +4939,8 @@ namespace { runtime->keypadRedrawRequested = true; const app::ScreenId previousScreen = state.shell.activeScreen; - const app::AppUpdate update = app::handle_command(state, command); + app::AppUpdate update = app::handle_command(state, command); + process_directory_picker_requests(state, update); logging::set_file_minimum_level(state.settings.loggingLevel); logging::set_debugger_console_minimum_level(state.settings.xemuConsoleLoggingLevel); log_app_update(state, update); @@ -4901,7 +4968,7 @@ namespace { if ((previousScreen != state.shell.activeScreen || update.navigation.screenChanged) && !draw_current_shell_frame(activeVideoMode, state, resources, runtime)) { return; } - if (state.shell.activeScreen != app::ScreenId::add_host || !state.addHostDraft.keypad.visible) { + if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && !(state.modal.id == app::ModalId::directory_picker && state.directoryPicker.creating)) { clear_keypad_modal_layout_cache(&resources->keypadModalLayoutCache); } } diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index 11306bb..d434083 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -6,6 +6,8 @@ #include "src/ui/shell_view.h" // standard includes +#include "src/platform/filesystem_utils.h" + #include #include #include @@ -593,12 +595,73 @@ namespace { }; } + /** + * @brief Present staged folder selection or the same keyboard geometry used by stream controls. + * @param state Current directory picker and input source. + * @param viewModel Receives a scrollable folder dialog or local name-entry keyboard. + */ + void fill_directory_picker_view(const app::ClientState &state, ui::ShellViewModel *viewModel) { + const auto &picker = state.directoryPicker; + if (picker.creating) { + viewModel->modal.visible = false; + auto &view = viewModel->keypad; + view.visible = true; + view.title = "New Folder"; + view.value = picker.name; + view.value.insert(std::min(picker.caret, view.value.size()), "|"); + if (!picker.error.empty()) { + view.lines = {picker.error}; + } + view.columnCount = input::StreamMenu::KEYBOARD_COLUMNS; + view.rowCount = input::StreamMenu::KEYBOARD_ROWS; + const auto &cells = picker.keyboard.keyboard_cells(); + for (std::size_t i = 0; i < cells.size(); ++i) { + const auto &cell = cells[i]; + std::string label = picker.keyboard.key_label(static_cast(i)); + if (cell.key == input::StreamMenu::KEYBOARD_PAGE_KEY && picker.keyboard.keyboardLayout == 1) { + label = "ABC"; + } else if (cell.key == 46) { + label = "OK"; + } + view.buttons.push_back({label, true, i == static_cast(picker.keyboard.keyIndex), cell.column, cell.row, cell.width, cell.height}); + } + if (state.shell.inputDevice == input::UiInputDevice::keyboard) { + view.footerActions = {{"create", "Create", "icons\\button-start.svg", {}, false}, {"cancel", "Cancel", "icons\\button-b.svg", {}, false}}; + } else { + view.footerActions = {{"type", "Type", "icons\\button-a.svg", {}, true}, {"delete", "Delete", "icons\\button-x.svg", {}, false}, {"shift", picker.keyboard.shiftOnce ? "Shift: On" : "Shift", "icons\\button-y.svg", {}, false}, {"create", "Create", "icons\\button-start.svg", {}, false}, {"cancel", "Cancel", "icons\\button-b.svg", {}, false}}; + } + return; + } + auto &view = viewModel->modal; + view.title = "Screenshot Folder"; + const auto path = picker.path.empty() ? std::string("Choose a drive") : picker.path; + view.lines = {path.size() > 72 ? "..." + path.substr(path.size() - 69) : path}; + if (!picker.error.empty()) { + view.lines.push_back(picker.error); + } else if (!picker.path.empty() && picker.folders.empty()) { + view.lines.push_back("No subfolders here."); + } + const bool canUse = picker.available && !picker.path.empty(); + view.actions = { + {"use-folder", "Use this folder", canUse, state.modal.selectedActionIndex == 0}, + {"new-folder", "New folder...", canUse, state.modal.selectedActionIndex == 1}, + {"parent-folder", "Up / Drives", !picker.path.empty(), state.modal.selectedActionIndex == 2}, + {"default-folder", "Use default location", true, state.modal.selectedActionIndex == 3}, + }; + for (std::size_t i = 0; i < picker.folders.size(); ++i) { + const auto &folder = picker.folders[i]; + view.actions.push_back({folder, picker.path.empty() ? folder : platform::file_name_from_path(folder), true, state.modal.selectedActionIndex == i + 4}); + } + view.footerActions = {{"select", "Select", "icons\\button-a.svg", {}, true}, {"use-folder", "Use folder", "icons\\button-y.svg", {}, false}, {"up", "Up", "icons\\button-lb.svg", {}, false}, {"cancel", "Cancel", "icons\\button-b.svg", {}, false}}; + } + /** * @brief Build the active dialog, including connection-test status icons and diagnostics. * * @param state Current reducer-owned modal state. * @param viewModel Receives render-ready dialog content. */ + void fill_modal_view(const app::ClientState &state, ui::ShellViewModel *viewModel) { if (!state.modal.active()) { return; @@ -627,6 +690,9 @@ namespace { case app::ModalId::confirmation: fill_confirmation_modal_view(state, viewModel); return; + case app::ModalId::directory_picker: + fill_directory_picker_view(state, viewModel); + return; case app::ModalId::connection_test: viewModel->modal.busy = state.connectionTest.inProgress; viewModel->modal.title = state.connectionTest.inProgress ? "Testing connection" : (state.connectionTest.succeeded ? "Connection successful" : "Connection failed"); diff --git a/src/ui/shell_view.h b/src/ui/shell_view.h index 199c123..40ce313 100644 --- a/src/ui/shell_view.h +++ b/src/ui/shell_view.h @@ -92,12 +92,16 @@ namespace ui { }; /** - * @brief Render-ready button in the keypad modal. + * @brief Render-ready numeric or text key with optional explicit keyboard geometry. */ struct ShellModalButton { std::string label; ///< User-facing button label. bool enabled = true; ///< True when the button can be activated. bool selected = false; ///< True when the button currently has focus. + int column = -1; ///< Explicit half-column origin for the shared keyboard; negative uses the numeric grid. + int row = 0; ///< Explicit keyboard row. + int width = 1; ///< Explicit key width in half-columns. + int height = 1; ///< Explicit key height in rows. }; /** @@ -172,7 +176,7 @@ namespace ui { }; /** - * @brief Add-host keypad modal view rendered above the split add-host screen. + * @brief Local numeric or text entry modal, shared by host entry and folder creation. */ struct ShellKeypadModalView { bool visible = false; ///< True when the keypad modal should be rendered. @@ -184,6 +188,7 @@ namespace ui { std::vector lines; ///< Instruction and draft lines shown in the keypad modal. std::vector buttons; ///< Buttons rendered inside the keypad modal. std::size_t columnCount = 0U; ///< Number of columns used to lay out keypad buttons. + std::size_t rowCount = 0U; ///< Explicit keyboard row count; zero derives rows from the numeric grid. std::vector footerActions; ///< Input, delete, cancel and accept controls inside the dialog. }; diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp new file mode 100644 index 0000000..381d403 --- /dev/null +++ b/tests/unit/app/directory_picker_test.cpp @@ -0,0 +1,231 @@ +/** + * @file tests/unit/app/directory_picker_test.cpp + * @brief Verifies staged screenshot destination selection and the local folder-name keyboard. + */ +// test includes +#include + +// standard includes +#include + +// local includes +#include "src/app/client_state.h" +#include "src/startup/storage_paths.h" +#include "src/ui/shell_view.h" + +namespace { + /** + * @brief Open screenshot destination settings using normal shell navigation. + * @param configured Existing screenshot destination. + * @return State with the folder picker open and a pending directory read. + */ + app::ClientState open_picker(const std::string &configured = {}) { + auto state = app::create_initial_state(); + state.settings.screenshotDirectory = configured; + app::handle_command(state, input::UiCommand::move_left); + app::handle_command(state, input::UiCommand::move_left); + app::handle_command(state, input::UiCommand::activate); + app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(state.detailMenu.select_item_by_id("screenshot-directory")); + const auto update = app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(update.requests.directoryListRequested); + EXPECT_TRUE(update.navigation.modalOpened); + EXPECT_EQ(state.modal.id, app::ModalId::directory_picker); + return state; + } + + /** + * @brief Begin local name entry in an available directory. + * @param state Open folder picker. + */ + void start_folder_name(app::ClientState &state) { + state.directoryPicker.available = true; + state.modal.selectedActionIndex = 1; + app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(state.directoryPicker.creating); + } +} // namespace + +/** + * @brief Opening or canceling the picker leaves the persisted screenshot folder unchanged. + */ +TEST(DirectoryPickerTest, StagesConfiguredAndDefaultLocationsUntilAccepted) { + for (const std::string configured : {std::string {}, std::string("F:\\Shots")}) { + auto state = open_picker(configured); + EXPECT_EQ(state.directoryPicker.path, configured.empty() ? startup::default_storage_path("screenshots") : configured); + auto view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.modal.title, "Screenshot Folder"); + EXPECT_FALSE(view.modal.actions[0].enabled); + const auto update = app::handle_command(state, input::UiCommand::back); + EXPECT_TRUE(update.navigation.modalClosed); + EXPECT_EQ(state.settings.screenshotDirectory, configured); + EXPECT_TRUE(state.directoryPicker.folders.empty()); + EXPECT_FALSE(update.persistence.settingsChanged); + } +} + +/** + * @brief Child, parent and drive navigation request new listings without changing the setting. + */ +TEST(DirectoryPickerTest, BrowsesChildrenParentsAndDrives) { + auto state = open_picker("F:\\Shots"); + state.directoryPicker.available = true; + state.directoryPicker.folders = {"F:\\Shots\\Games", "F:\\Shots\\Desktop"}; + state.modal.selectedActionIndex = 4; + EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).requests.directoryListRequested); + EXPECT_EQ(state.directoryPicker.path, "F:\\Shots\\Games"); + EXPECT_TRUE(app::handle_command(state, input::UiCommand::move_left).requests.directoryListRequested); + EXPECT_EQ(state.directoryPicker.path, "F:\\Shots"); + app::handle_command(state, input::UiCommand::previous_page); + EXPECT_EQ(state.directoryPicker.path, "F:\\"); + app::handle_command(state, input::UiCommand::previous_page); + EXPECT_TRUE(state.directoryPicker.path.empty()); + EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Shots"); + state.modal.selectedActionIndex = 0; + EXPECT_FALSE(app::handle_command(state, input::UiCommand::activate).requests.directorySelectRequested); + state.modal.selectedActionIndex = 1; + app::handle_command(state, input::UiCommand::activate); + EXPECT_FALSE(state.directoryPicker.creating); +} + +/** + * @brief Selection requests a write check; only its successful completion updates and persists the setting. + */ +TEST(DirectoryPickerTest, AcceptsValidatedSelectionAndRestoresTheDefault) { + auto state = open_picker("F:\\Old"); + state.directoryPicker.path = "E:\\Screenshots"; + state.directoryPicker.available = true; + state.modal.selectedActionIndex = 9; + EXPECT_TRUE(app::handle_command(state, input::UiCommand::open_context_menu).requests.directorySelectRequested); + auto update = app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(update.requests.directorySelectRequested); + EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Old"); + app::accept_screenshot_directory(state, update, state.directoryPicker.path); + EXPECT_EQ(state.settings.screenshotDirectory, "E:\\Screenshots"); + EXPECT_TRUE(state.settings.dirty); + EXPECT_TRUE(update.persistence.settingsChanged); + EXPECT_TRUE(update.navigation.modalClosed); + EXPECT_TRUE(state.directoryPicker.path.empty()); + EXPECT_EQ(state.detailMenu.selected_item()->id, "screenshot-directory"); + app::handle_command(state, input::UiCommand::activate); + state.modal.selectedActionIndex = 3; + update = app::handle_command(state, input::UiCommand::confirm); + EXPECT_TRUE(state.settings.screenshotDirectory.empty()); + EXPECT_TRUE(update.persistence.settingsChanged); + app::handle_command(state, input::UiCommand::activate); + state.modal.selectedActionIndex = 3; + EXPECT_FALSE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); + app::accept_screenshot_directory(state, update, "ignored"); + EXPECT_TRUE(state.settings.screenshotDirectory.empty()); +} + +/** + * @brief Folder name entry reuses the OSK's caps, one-shot shift, symbols and tall-key navigation. + */ +TEST(DirectoryPickerTest, TypesLocallyWithTheSharedKeyboardAndCaret) { + auto state = open_picker("F:\\Shots"); + start_folder_name(state); + auto &picker = state.directoryPicker; + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(picker.name, "q"); + picker.keyboard.keyIndex = 43; + app::handle_command(state, input::UiCommand::activate); + picker.keyboard.keyIndex = 10; + app::handle_command(state, input::UiCommand::activate); + app::handle_command(state, input::UiCommand::open_context_menu); + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(picker.name, "qQq"); + EXPECT_FALSE(picker.keyboard.shiftOnce); + picker.keyboard.keyIndex = 42; + app::handle_command(state, input::UiCommand::activate); + app::append_directory_name(state, "Z"); + EXPECT_EQ(picker.name, "qQZq"); + picker.keyboard.keyIndex = 45; + app::handle_command(state, input::UiCommand::activate); + app::handle_command(state, input::UiCommand::delete_character); + EXPECT_EQ(picker.name, "qQZ"); + app::handle_command(state, input::UiCommand::next_page); + picker.keyboard.keyIndex = 12; + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(picker.name, "qQZ#"); + picker.keyboard.keyIndex = 40; + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(picker.name.back(), ' '); + picker.keyboard.keyIndex = 44; + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(picker.name, "qQZ#"); + picker.keyboard.keyIndex = 41; + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(picker.keyboard.keyboardLayout, 0); + app::handle_command(state, input::UiCommand::move_up); + app::handle_command(state, input::UiCommand::move_right); + app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_left); + picker.keyboard.keyIndex = 46; + EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).requests.directoryCreateRequested); + EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Shots"); + EXPECT_FALSE(state.settings.dirty); +} + +/** + * @brief Invalid, oversized and non-printable names never issue a filesystem request; cancellation keeps the browser. + */ +TEST(DirectoryPickerTest, ValidatesCreationAndPreservesTheBrowserOnCancel) { + auto state = open_picker("F:\\Shots"); + start_folder_name(state); + auto &picker = state.directoryPicker; + EXPECT_FALSE(app::handle_command(state, input::UiCommand::confirm).requests.directoryCreateRequested); + EXPECT_FALSE(picker.error.empty()); + app::append_directory_name(state, "../invalid"); + EXPECT_TRUE(picker.error.empty()); + EXPECT_FALSE(app::handle_command(state, input::UiCommand::confirm).requests.directoryCreateRequested); + picker.name.clear(); + picker.caret = 100; + app::append_directory_name(state, std::string(60, 'a') + "\n"); + EXPECT_EQ(picker.name.size(), 42); + EXPECT_EQ(picker.caret, 42); + EXPECT_TRUE(app::handle_command(state, input::UiCommand::confirm).requests.directoryCreateRequested); + app::handle_command(state, input::UiCommand::back); + EXPECT_FALSE(picker.creating); + EXPECT_EQ(state.modal.id, app::ModalId::directory_picker); + EXPECT_EQ(picker.path, "F:\\Shots"); + app::append_directory_name(state, "ignored"); + EXPECT_EQ(picker.name.size(), 42); +} + +/** + * @brief The browser supports large lists, error feedback and the shared keyboard's exact cell geometry and device hints. + */ +TEST(DirectoryPickerTest, BuildsScrollableFoldersAndControllerOrKeyboardNameEntry) { + auto state = open_picker("F:\\Shots"); + state.directoryPicker.available = true; + for (int i = 0; i < 100; ++i) { + state.directoryPicker.folders.push_back("F:\\Shots\\Folder" + std::to_string(i)); + } + app::handle_command(state, input::UiCommand::move_up); + auto view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.actions.size(), 104); + EXPECT_TRUE(view.modal.actions.back().selected); + EXPECT_EQ(view.modal.actions.back().label, "Folder99"); + app::handle_command(state, input::UiCommand::move_down); + EXPECT_EQ(state.modal.selectedActionIndex, 0); + state.directoryPicker.error = "Cannot write here"; + view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.modal.lines.back(), "Cannot write here"); + start_folder_name(state); + view = ui::build_shell_view_model(state, {}); + EXPECT_FALSE(view.modal.visible); + EXPECT_TRUE(view.keypad.visible); + EXPECT_EQ(view.keypad.buttons.size(), 47); + EXPECT_EQ(view.keypad.rowCount, 5); + EXPECT_EQ(view.keypad.columnCount, 26); + EXPECT_EQ(view.keypad.buttons[43].height, 2); + EXPECT_EQ(view.keypad.buttons[46].label, "OK"); + EXPECT_EQ(view.keypad.footerActions.size(), 5); + state.shell.inputDevice = input::UiInputDevice::keyboard; + view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.keypad.footerActions.size(), 2); + EXPECT_EQ(view.keypad.footerActions[0].keyLabel, "Enter"); + EXPECT_EQ(view.keypad.footerActions[1].keyLabel, "Esc"); +} diff --git a/tests/unit/platform/filesystem_utils_test.cpp b/tests/unit/platform/filesystem_utils_test.cpp index 659a495..f94dc94 100644 --- a/tests/unit/platform/filesystem_utils_test.cpp +++ b/tests/unit/platform/filesystem_utils_test.cpp @@ -2,20 +2,20 @@ * @file tests/unit/platform/filesystem_utils_test.cpp * @brief Verifies filesystem utility helpers. */ -// test header include -#include "src/platform/filesystem_utils.h" +// test includes +#include "tests/support/filesystem_test_utils.h" + +#include // standard includes #include #include #include +#include #include -// lib includes -#include - -// test includes -#include "tests/support/filesystem_test_utils.h" +// local includes +#include "src/platform/filesystem_utils.h" namespace { @@ -84,4 +84,108 @@ namespace { #endif } + /** + * @brief Directory parents preserve absolute drive and POSIX roots and handle trailing separators. + */ + TEST(FolderPickerFilesystemTest, KeepsRootsAbsoluteWhenGoingUp) { + EXPECT_EQ(platform::directory_parent("F:\\Shots\\Games\\"), "F:\\Shots"); + EXPECT_EQ(platform::directory_parent("F:\\Shots"), "F:\\"); + EXPECT_EQ(platform::directory_parent("F:\\"), ""); + EXPECT_EQ(platform::directory_parent("F:"), ""); + EXPECT_EQ(platform::directory_parent("/shots/games/"), "/shots"); + EXPECT_EQ(platform::directory_parent("/shots"), "/"); + EXPECT_EQ(platform::directory_parent("/"), ""); + EXPECT_EQ(platform::directory_parent("\\"), ""); + EXPECT_EQ(platform::directory_parent("relative"), ""); + EXPECT_EQ(platform::directory_parent(""), ""); + } + + /** + * @brief Folder names exclude traversal, reserved characters and Xbox-incompatible component lengths. + */ + TEST(FolderPickerFilesystemTest, ValidatesSingleFolderNames) { + for (const std::string name : {"Screenshots", "Game captures", "Shots-2026_(1)", "!#$%&'@^{}~"}) { + EXPECT_TRUE(platform::valid_directory_name(name)) << name; + } + EXPECT_TRUE(platform::valid_directory_name(std::string(42, 'x'))); + EXPECT_FALSE(platform::valid_directory_name(std::string(43, 'x'))); + for (const std::string name : {"", ".", "..", "../outside", "child\\outside", "F:Shots", " trailing ", "end.", "one\ntwo", "\x7f", "bad?", "bad*", "bad|", "bad<", "bad>", "bad\"", "bad+", "bad=", "bad,", "bad;", "bad[", "bad]"}) { + EXPECT_FALSE(platform::valid_directory_name(name)) << name; + } + } + + /** + * @brief Listing returns sorted immediate folders, including empty ones, and never files or dot entries. + */ + TEST_F(FilesystemUtilsTest, ListsOnlyImmediateDirectoriesAndReportsUnavailablePaths) { + ASSERT_TRUE(platform::ensure_directory_exists(nestedDirectory)); + const auto empty = platform::list_directories(nestedDirectory); + EXPECT_TRUE(empty.error.empty()); + EXPECT_TRUE(empty.paths.empty()); + const auto another = test_support::join_path(rootDirectory, "another"); + ASSERT_TRUE(platform::ensure_directory_exists(another)); + write_test_file(test_support::join_path(rootDirectory, "file.txt"), "keep"); + auto result = platform::list_directories(rootDirectory); + EXPECT_TRUE(result.error.empty()); + ASSERT_EQ(result.paths.size(), 2); + EXPECT_EQ(result.paths[0], another); + EXPECT_EQ(result.paths[1], test_support::join_path(rootDirectory, "level-one")); + EXPECT_FALSE(platform::list_directories(test_support::join_path(rootDirectory, "missing")).error.empty()); + EXPECT_FALSE(platform::list_directories(test_support::join_path(rootDirectory, "file.txt")).error.empty()); + result = platform::list_directories({}); + EXPECT_TRUE(result.error.empty()); + EXPECT_FALSE(result.paths.empty()); + } + + /** + * @brief Folder creation refuses existing files/directories and cannot create traversal paths or missing parents. + */ + TEST_F(FilesystemUtilsTest, CreatesOnlyANewValidatedChildFolder) { + ASSERT_TRUE(platform::ensure_directory_exists(rootDirectory)); + std::string error = "old"; + EXPECT_TRUE(platform::create_child_directory(rootDirectory, "New folder", error)); + EXPECT_TRUE(error.empty()); + EXPECT_TRUE(std::filesystem::is_directory(test_support::join_path(rootDirectory, "New folder"))); + EXPECT_FALSE(platform::create_child_directory(rootDirectory, "New folder", error)); + EXPECT_FALSE(error.empty()); + write_test_file(test_support::join_path(rootDirectory, "file.txt"), "keep"); + EXPECT_FALSE(platform::create_child_directory(rootDirectory, "file.txt", error)); + EXPECT_FALSE(platform::create_child_directory(rootDirectory, "../escape", error)); + EXPECT_FALSE(platform::create_child_directory({}, "Folder", error)); + EXPECT_FALSE(platform::create_child_directory(test_support::join_path(rootDirectory, "missing"), "Folder", error)); + EXPECT_EQ(std::filesystem::file_size(test_support::join_path(rootDirectory, "file.txt")), 4); + } + + /** + * @brief Destination validation removes its own probe and never overwrites a pre-existing probe name. + */ + TEST_F(FilesystemUtilsTest, ChecksWritabilityWithoutLeavingFilesOrOverwritingCollisions) { + ASSERT_TRUE(platform::ensure_directory_exists(rootDirectory)); + const auto sentinel = test_support::join_path(rootDirectory, "moonlight-write-1.tmp"); + write_test_file(sentinel, "keep"); + std::string error = "old"; + EXPECT_TRUE(platform::directory_is_writable(rootDirectory, error)) << error; + EXPECT_TRUE(error.empty()); + EXPECT_EQ(std::filesystem::file_size(sentinel), 4); + EXPECT_EQ(std::distance(std::filesystem::directory_iterator(rootDirectory), std::filesystem::directory_iterator {}), 1); + EXPECT_FALSE(platform::directory_is_writable({}, error)); + EXPECT_FALSE(platform::directory_is_writable(sentinel, error)); + EXPECT_FALSE(platform::directory_is_writable(test_support::join_path(rootDirectory, "missing"), error)); + EXPECT_FALSE(error.empty()); + } + + /** + * @brief Exhausted temporary names fail safely and leave every existing file intact. + */ + TEST_F(FilesystemUtilsTest, FailsSafelyWhenAllWriteProbeNamesAlreadyExist) { + ASSERT_TRUE(platform::ensure_directory_exists(rootDirectory)); + for (int i = 1; i <= 32; ++i) { + write_test_file(test_support::join_path(rootDirectory, "moonlight-write-" + std::to_string(i) + ".tmp"), "keep"); + } + std::string error; + EXPECT_FALSE(platform::directory_is_writable(rootDirectory, error)); + EXPECT_FALSE(error.empty()); + EXPECT_EQ(std::distance(std::filesystem::directory_iterator(rootDirectory), std::filesystem::directory_iterator {}), 32); + } + } // namespace From 4ab5a5363a76ca559d82a2703a8402ed22aace57 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:39:30 -0500 Subject: [PATCH 05/22] perf(ui): retain folder navigation and group stream settings --- README.md | 2 +- src/app/client_state.cpp | 16 +- src/app/client_state.h | 2 +- src/ui/shell_layout.cpp | 23 +++ src/ui/shell_layout.h | 20 +++ src/ui/shell_screen.cpp | 205 +++++++++++++++++++++-- src/ui/shell_view.cpp | 30 +++- src/ui/shell_view.h | 9 + tests/unit/app/client_state_test.cpp | 40 ++--- tests/unit/app/directory_picker_test.cpp | 2 + tests/unit/ui/shell_layout_test.cpp | 49 ++++++ tests/unit/ui/shell_view_test.cpp | 88 ++++++++++ 12 files changed, 437 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index b667c68..f49a26c 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ the host app running after disconnecting. Returning from the stream resets local keyboard/controller holds before library navigation resumes. Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Open -**Settings > Display > Screenshot Folder** to browse drives and subfolders, select the current folder, +**Settings > Streaming > Screenshot Folder** to browse drives and subfolders, select the current folder, or create a new folder with the on-screen keyboard or a USB keyboard. **Use default location** restores the title-scoped screenshot folder. **B / Esc** cancels without changing the saved destination. New folder names use up to 42 printable characters; **Start / Enter** creates the folder. The destination diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index df4a4d5..8dd235a 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -84,8 +84,8 @@ namespace { switch (category) { case app::SettingsCategory::logging: return std::string(SETTINGS_CATEGORY_PREFIX) + "logging"; - case app::SettingsCategory::display: - return std::string(SETTINGS_CATEGORY_PREFIX) + "display"; + case app::SettingsCategory::streaming: + return std::string(SETTINGS_CATEGORY_PREFIX) + "streaming"; case app::SettingsCategory::input: return std::string(SETTINGS_CATEGORY_PREFIX) + "input"; case app::SettingsCategory::reset: @@ -96,8 +96,8 @@ namespace { } app::SettingsCategory settings_category_from_menu_id(std::string_view itemId) { - if (itemId == settings_category_menu_id(app::SettingsCategory::display)) { - return app::SettingsCategory::display; + if (itemId == settings_category_menu_id(app::SettingsCategory::streaming)) { + return app::SettingsCategory::streaming; } if (itemId == settings_category_menu_id(app::SettingsCategory::input)) { return app::SettingsCategory::input; @@ -112,8 +112,8 @@ namespace { switch (category) { case app::SettingsCategory::logging: return "Control the runtime log file, the in-app log viewer, and xemu debugger output verbosity."; - case app::SettingsCategory::display: - return "Tune streaming video resolution, frame rate, bitrate, audio playback, and the in-stream diagnostics overlay."; + case app::SettingsCategory::streaming: + return "Set stream video, audio, performance statistics and screenshot storage."; case app::SettingsCategory::input: return "Input options will live here when controller and navigation customization is added."; case app::SettingsCategory::reset: @@ -505,7 +505,7 @@ namespace { case app::ScreenId::settings: return { {settings_category_menu_id(app::SettingsCategory::logging), "Logging", settings_category_description(app::SettingsCategory::logging), true}, - {settings_category_menu_id(app::SettingsCategory::display), "Display", settings_category_description(app::SettingsCategory::display), true}, + {settings_category_menu_id(app::SettingsCategory::streaming), "Streaming", settings_category_description(app::SettingsCategory::streaming), true}, {settings_category_menu_id(app::SettingsCategory::input), "Input", settings_category_description(app::SettingsCategory::input), true}, {settings_category_menu_id(app::SettingsCategory::reset), "Reset", settings_category_description(app::SettingsCategory::reset), true}, }; @@ -555,7 +555,7 @@ namespace { {"cycle-log-level", std::string("File Logging Level: ") + logging::to_string(state.settings.loggingLevel), "Choose the minimum severity written to moonlight.log. Lower levels produce more detail but increase disk writes.", true}, {"cycle-xemu-console-log-level", std::string("xemu Console Level: ") + logging::to_string(state.settings.xemuConsoleLoggingLevel), "Choose the minimum severity mirrored to xemu through DbgPrint() when you launch xemu with a serial console.", true}, }; - case app::SettingsCategory::display: + case app::SettingsCategory::streaming: return { { "cycle-stream-video-mode", diff --git a/src/app/client_state.h b/src/app/client_state.h index c957ed8..1c3d463 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -85,7 +85,7 @@ namespace app { */ enum class SettingsCategory { logging, ///< Logging and diagnostics options. - display, ///< Display and video presentation options. + streaming, ///< Stream video, audio, telemetry and screenshot options. input, ///< Input and controller options. reset, ///< Reset and cleanup actions. }; diff --git a/src/ui/shell_layout.cpp b/src/ui/shell_layout.cpp index 25cb27a..99d7963 100644 --- a/src/ui/shell_layout.cpp +++ b/src/ui/shell_layout.cpp @@ -10,6 +10,29 @@ namespace ui { + ShellRect framebuffer_repaint_rect(ShellRect logical, int logicalWidth, int framebufferWidth, int framebufferHeight) { + if (logical.w <= 0 || logical.h <= 0 || logicalWidth <= 0 || framebufferWidth <= 0 || framebufferHeight <= 0) { + return {}; + } + const auto left = std::clamp(static_cast(logical.x) * framebufferWidth / logicalWidth - 2, 0, framebufferWidth); + const auto right = std::clamp(((static_cast(logical.x) + logical.w) * framebufferWidth + logicalWidth - 1) / logicalWidth + 2, 0, framebufferWidth); + const auto top = std::clamp(static_cast(logical.y) - 1, 0, framebufferHeight); + const auto bottom = std::clamp(static_cast(logical.y) + logical.h + 1, 0, framebufferHeight); + return right > left && bottom > top ? ShellRect {static_cast(left), static_cast(top), static_cast(right - left), static_cast(bottom - top)} : ShellRect {}; + } + + std::size_t stable_list_first_row(std::size_t count, std::size_t selected, std::size_t visible, std::size_t previousFirst) { + if (count == 0 || visible == 0 || count <= visible) { + return 0; + } + selected = std::min(selected, count - 1); + const auto first = std::min(previousFirst, count - visible); + if (selected < first) { + return selected; + } + return selected - first >= visible ? selected - visible + 1 : first; + } + ShellLayout calculate_shell_layout(int width, int height) { // A five-percent inset keeps controls clear of overscan on CRT televisions. const int marginX = width / 20; diff --git a/src/ui/shell_layout.h b/src/ui/shell_layout.h index 2756c40..5260d90 100644 --- a/src/ui/shell_layout.h +++ b/src/ui/shell_layout.h @@ -11,6 +11,16 @@ namespace ui { + /** + * @brief Keep list focus visible without scrolling rows that already contain it. + * @param count Total rows. + * @param selected Focused row; oversized values clamp to the final row. + * @param visible Maximum number of visible rows. + * @param previousFirst First row of the last rendered window. + * @return First visible row, or zero for an empty list or viewport. + */ + std::size_t stable_list_first_row(std::size_t count, std::size_t selected, std::size_t visible, std::size_t previousFirst); + inline constexpr std::size_t MAX_VISIBLE_NOTIFICATIONS = 3U; ///< Maximum number of simultaneous notification cards. /** @@ -23,6 +33,16 @@ namespace ui { int h = 0; ///< Height. }; + /** + * @brief Map a logical repaint to clipped framebuffer pixels with a small raster-rounding guard. + * @param logical Logical rectangle, with unscaled vertical coordinates. + * @param logicalWidth Logical display width before encoder correction. + * @param framebufferWidth Physical framebuffer width. + * @param framebufferHeight Physical framebuffer height. + * @return Bounded physical rectangle; empty for invalid or wholly offscreen input. + */ + ShellRect framebuffer_repaint_rect(ShellRect logical, int logicalWidth, int framebufferWidth, int framebufferHeight); + /** * @brief Shared screen regions with television-safe margins. */ diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 5e8d1ff..50e0060 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -236,9 +237,30 @@ namespace { }; /** - * @brief Stores reusable layout and text textures for the add-host keypad modal. + * @brief Retained folder-dialog pixels and text for only the visible list window. + */ + struct FolderDialogCache { + ui::ShellModalView content; ///< Static dialog content, excluding the changing selection. + std::map labels; ///< Visible row labels; offscreen textures are released. + SDL_Rect rows {}; ///< Logical list rectangle inside the dialog. + int rowHeight = 0; ///< Painted row height, excluding the six-pixel gap. + std::size_t first = 0; ///< First visible row, retained until focus leaves the window. + std::size_t selected = 0; ///< Previously painted selection. + int logicalWidth = 0; ///< Logical output width used to paint the retained frame. + int framebufferWidth = 0; ///< Actual output width before encoder correction. + int framebufferHeight = 0; ///< Actual output height. + TTF_Font *titleFont = nullptr; ///< Font identity used for the retained heading. + TTF_Font *bodyFont = nullptr; ///< Font identity used for row labels. + TTF_Font *smallFont = nullptr; ///< Font identity used for guidance and helpers. + bool valid = false; ///< A complete frame has been presented for these dependencies. + }; + + /** + * @brief Stores reusable layout, text and retained folder-dialog state for local input modals. */ struct KeypadModalLayoutCache { + SDL_Window *window = nullptr; ///< Shared window; needed only for validated partial Xbox framebuffer presentation. + FolderDialogCache folder; ///< Retained folder browser, sharing the local-dialog lifetime. int modalInnerWidth = 0; ///< Last modal content width used to build the cached line layout. int modalTextHeight = 0; ///< Cached combined height of the modal body text. std::vector lines; ///< Last body lines used to populate the cache. @@ -330,7 +352,19 @@ namespace { } /** - * @brief Releases every cached text texture stored for the keypad modal. + * @brief Release the folder window's labels and invalidate its retained frame. + * @param cache Folder cache owned by the current renderer. + */ + void clear_folder_dialog_cache(FolderDialogCache &cache) { + for (auto &[index, label] : cache.labels) { + (void) index; + clear_cached_text_texture(&label); + } + cache = {}; + } + + /** + * @brief Releases every cached text texture and retained frame stored for local dialogs. * * @param cache Keypad modal cache to clear. */ @@ -339,6 +373,7 @@ namespace { return; } + clear_folder_dialog_cache(cache->folder); clear_cached_text_texture(&cache->titleTexture); for (CachedTextTexture &lineTexture : cache->lineTextures) { clear_cached_text_texture(&lineTexture); @@ -1450,6 +1485,8 @@ namespace { * @param rowHeight Height of an action row. * @param assetCache Optional cache for menu icons. * @param active Whether this list currently owns controller focus. + * @param folder Optional retained folder viewport and visible label textures. + * @param repaint Repaint affected rows in the retained frame instead of the complete list. * @return True when visible labels rendered successfully. */ bool render_action_rows( @@ -1459,7 +1496,9 @@ namespace { const SDL_Rect &rect, int rowHeight, AssetTextureCache *assetCache = nullptr, - bool active = true + bool active = true, + FolderDialogCache *folder = nullptr, + bool repaint = false ) { if (rows.empty()) { return true; @@ -1487,13 +1526,41 @@ namespace { startIndex = maxStartIndex; } } + if (folder != nullptr) { + startIndex = ui::stable_list_first_row(rows.size(), selectedIndex, visibleRowCount, folder->first); + } + const bool windowChanged = folder != nullptr && startIndex != folder->first; const std::size_t endIndex = std::min(rows.size(), startIndex + visibleRowCount); const bool overflow = rows.size() > visibleRowCount; const int scrollBarWidth = overflow ? 8 : 0; const int contentWidth = std::max(1, rect.w - (overflow ? scrollBarWidth + 10 : 0)); - int y = rect.y; + if (folder != nullptr) { + if (repaint) { + if (windowChanged) { + fill_rect(renderer, rect, 0x15, 0x1E, 0x2E); + } else { + for (const auto index : {folder->selected, selectedIndex}) { + if (index >= startIndex && index < endIndex) { + fill_rect(renderer, {rect.x, rect.y + static_cast(index - startIndex) * rowStep, contentWidth, rowHeight}, 0x15, 0x1E, 0x2E); + } + } + } + } + for (auto it = folder->labels.begin(); it != folder->labels.end();) { + if (it->first < startIndex || it->first >= endIndex) { + clear_cached_text_texture(&it->second); + it = folder->labels.erase(it); + } else { + ++it; + } + } + } for (std::size_t index = startIndex; index < endIndex; ++index) { + if (folder != nullptr && repaint && !windowChanged && index != folder->selected && index != selectedIndex) { + continue; + } + const int y = rect.y + static_cast(index - startIndex) * rowStep; const ui::ShellActionRow &row = rows[index]; const SDL_Rect rowRect {rect.x, y, contentWidth, rowHeight}; if (row.selected) { @@ -1522,14 +1589,20 @@ namespace { draw_rounded_rect_outline(renderer, checkbox, {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, 4); } } - if (const SDL_Color color = row.enabled ? SDL_Color {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF} : SDL_Color {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}; !render_text_line_simple(renderer, font, row.label, color, labelX, rowRect.y + (rowRect.h - TTF_FontLineSkip(font)) / 2, rowRect.x + rowRect.w - labelX - 12 - (row.checkable ? checkboxSize + 16 : 0))) { + const SDL_Color color = row.enabled ? SDL_Color {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF} : SDL_Color {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}; + const int labelWidth = rowRect.x + rowRect.w - labelX - 12 - (row.checkable ? checkboxSize + 16 : 0); + const int labelY = rowRect.y + (rowRect.h - TTF_FontLineSkip(font)) / 2; + if (folder != nullptr) { + auto &label = folder->labels[index]; + if (!ensure_single_line_text_texture(renderer, font, row.label, color, labelWidth, &label) || !render_cached_text_texture(renderer, label, labelX, labelY)) { + return false; + } + } else if (!render_text_line_simple(renderer, font, row.label, color, labelX, labelY, labelWidth)) { return false; } - - y += rowStep; } - if (overflow) { + if (overflow && (!repaint || windowChanged)) { render_vertical_scrollbar( renderer, {rect.x + rect.w - scrollBarWidth, rect.y, scrollBarWidth, rect.h}, @@ -1539,6 +1612,10 @@ namespace { ); } + if (folder != nullptr) { + folder->first = startIndex; + folder->selected = selectedIndex; + } return true; } @@ -3821,9 +3898,10 @@ namespace { * @param modal Dialog content. * @param width Logical display width. * @param height Framebuffer height. + * @param folder Optional folder cache to populate for incremental navigation. * @return True when the dialog rendered successfully. */ - bool render_shell_dialog(SDL_Renderer *renderer, TTF_Font *titleFont, TTF_Font *bodyFont, TTF_Font *smallFont, AssetTextureCache *assetCache, const ui::ShellModalView &modal, int width, int height) { + bool render_shell_dialog(SDL_Renderer *renderer, TTF_Font *titleFont, TTF_Font *bodyFont, TTF_Font *smallFont, AssetTextureCache *assetCache, const ui::ShellModalView &modal, int width, int height, FolderDialogCache *folder = nullptr) { const int padding = std::max(18, height / 40); const int dialogWidth = std::min(760, width * 3 / 4); const int innerWidth = dialogWidth - padding * 2; @@ -3857,7 +3935,12 @@ namespace { } cursorY += drawnHeight + 8; } - if (!modal.actions.empty() && !render_action_rows(renderer, bodyFont, modal.actions, {bounds.x + padding, cursorY + 4, innerWidth, footerY - cursorY - 12}, rowHeight, assetCache)) { + const SDL_Rect rows {bounds.x + padding, cursorY + 4, innerWidth, footerY - cursorY - 12}; + if (folder != nullptr) { + folder->rows = rows; + folder->rowHeight = rowHeight; + } + if (!modal.actions.empty() && !render_action_rows(renderer, bodyFont, modal.actions, rows, rowHeight, assetCache, true, folder)) { return false; } draw_line(renderer, bounds.x + padding, footerY - 6, bounds.x + bounds.w - padding, footerY - 6, MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0x25); @@ -3963,7 +4046,55 @@ namespace { } /** - * @brief Compose a television-safe shell frame from compact navigation and page content. + * @brief Present changed folder rows without the Xbox SDL driver's full-screen copy, retaining SDL fallback. + * @param renderer Software shell renderer to flush. + * @param window Shared window backing the renderer, or null for ordinary SDL presentation. + * @param logicalDamage Repainted rows in logical display coordinates. + * @param logicalWidth Logical width before encoder correction. + */ + void present_folder_damage(SDL_Renderer *renderer, SDL_Window *window, const SDL_Rect &logicalDamage, int logicalWidth) { +#ifdef NXDK + SDL_RendererInfo info {}; + if (window != nullptr && SDL_GetRendererInfo(renderer, &info) == 0 && (info.flags & SDL_RENDERER_SOFTWARE) != 0) { + SDL_RenderFlush(renderer); + SDL_Surface *surface = SDL_GetWindowSurface(window); + const VIDEO_MODE mode = XVideoGetMode(); + void *framebuffer = XVideoGetFB(); + const Uint32 format = mode.bpp == 32 ? SDL_PIXELFORMAT_RGB888 : mode.bpp == 16 ? SDL_PIXELFORMAT_RGB565 : + SDL_PIXELFORMAT_RGB555; + const int bytes = mode.bpp == 32 ? 4 : mode.bpp == 15 || mode.bpp == 16 ? 2 : + 0; + // The Xbox software window surface uses the video mode's RGB format. + // Validate that relationship before copying into write-combined scanout. + if (surface != nullptr && framebuffer != nullptr && bytes > 0 && surface->w == mode.width && surface->h == mode.height && surface->format->format == format && surface->format->BytesPerPixel == bytes && surface->pitch >= mode.width * bytes) { + const auto area = ui::framebuffer_repaint_rect({logicalDamage.x, logicalDamage.y, logicalDamage.w, logicalDamage.h}, logicalWidth, mode.width, mode.height); + const bool locked = SDL_MUSTLOCK(surface) != 0; + if (locked && SDL_LockSurface(surface) != 0) { + SDL_RenderPresent(renderer); + return; + } + for (int y = area.y; y < area.y + area.h; ++y) { + const auto *source = static_cast(surface->pixels) + static_cast(y) * surface->pitch + area.x * bytes; + auto *destination = static_cast(framebuffer) + (static_cast(y) * mode.width + area.x) * bytes; + std::memcpy(destination, source, static_cast(area.w) * bytes); + } + if (locked) { + SDL_UnlockSurface(surface); + } + XVideoFlushFB(); + return; + } + } +#else + (void) window; + (void) logicalDamage; + (void) logicalWidth; +#endif + SDL_RenderPresent(renderer); + } + + /** + * @brief Compose the shell or update retained folder rows without rebuilding unchanged page pixels. * * @param renderer Active SDL renderer, including the software fallback. * @param videoMode Output resolution and pixel aspect information. @@ -3999,6 +4130,38 @@ namespace { const int screenWidth = splash::calculate_display_width(screenHeight, videoMode, encoderSettings); const float horizontalScale = static_cast(framebufferWidth) / static_cast(screenWidth); + auto *folder = keypadModalLayoutCache != nullptr ? &keypadModalLayoutCache->folder : nullptr; + const bool folderBrowser = viewModel.modal.folderBrowser && !viewModel.overlay.visible && !viewModel.keypad.visible; + if (folder != nullptr) { + const bool reusable = folderBrowser && folder->valid && folder->logicalWidth == screenWidth && folder->framebufferWidth == framebufferWidth && folder->framebufferHeight == framebufferHeight && folder->titleFont == titleFont && folder->bodyFont == bodyFont && folder->smallFont == smallFont && ui::same_folder_dialog_content(folder->content, viewModel.modal); + if (reusable) { + std::size_t selected = 0; + for (std::size_t i = 0; i < viewModel.modal.actions.size(); ++i) { + if (viewModel.modal.actions[i].selected) { + selected = i; + break; + } + } + if (selected == folder->selected) { + return true; + } + SDL_RenderSetScale(renderer, horizontalScale, 1.0f); + const auto oldFirst = folder->first; + const auto oldSelected = folder->selected; + const bool painted = render_action_rows(renderer, bodyFont, viewModel.modal.actions, folder->rows, folder->rowHeight, assetCache, true, folder, true); + SDL_Rect damage = folder->rows; + if (oldFirst == folder->first) { + const auto top = std::min(oldSelected, selected) - folder->first; + const auto bottom = std::max(oldSelected, selected) - folder->first; + damage.y += static_cast(top) * (folder->rowHeight + 6); + damage.h = static_cast(bottom - top) * (folder->rowHeight + 6) + folder->rowHeight; + } + present_folder_damage(renderer, keypadModalLayoutCache->window, damage, screenWidth); + SDL_RenderSetScale(renderer, 1.0f, 1.0f); + return painted; + } + clear_folder_dialog_cache(*folder); + } SDL_RenderSetScale(renderer, 1.0f, 1.0f); const ui::ShellLayout layout = ui::calculate_shell_layout(screenWidth, screenHeight); @@ -4163,7 +4326,7 @@ namespace { render_text_line_simple(renderer, smallFont, (viewModel.modal.footerActions.empty() || viewModel.modal.footerActions.back().keyLabel.empty() ? "Press B to close." : "Press Esc to close."), {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 54 + TTF_FontLineSkip(smallFont) + 8, modalRect.w - 32); } } else if (viewModel.modal.visible) { - if (!render_shell_dialog(renderer, titleFont, bodyFont, smallFont, assetCache, viewModel.modal, screenWidth, screenHeight)) { + if (!render_shell_dialog(renderer, titleFont, bodyFont, smallFont, assetCache, viewModel.modal, screenWidth, screenHeight, folderBrowser ? folder : nullptr)) { return false; } } @@ -4172,6 +4335,16 @@ namespace { } SDL_RenderPresent(renderer); + if (folder != nullptr && folderBrowser) { + folder->content = viewModel.modal; + folder->logicalWidth = screenWidth; + folder->framebufferWidth = framebufferWidth; + folder->framebufferHeight = framebufferHeight; + folder->titleFont = titleFont; + folder->bodyFont = bodyFont; + folder->smallFont = smallFont; + folder->valid = true; + } SDL_RenderSetScale(renderer, 1.0f, 1.0f); return true; } @@ -4577,7 +4750,7 @@ namespace { */ struct ShellRuntimeState { bool running = true; ///< False when the shell should stop processing frames. - bool keypadRedrawRequested = true; ///< True when the add-host keypad must redraw immediately. + bool keypadRedrawRequested = true; ///< True when a retained local input dialog needs a new frame. ShellInputState inputState {}; ///< Current controller and trigger input state. Uint32 nextHostDiscoveryTick = 0; ///< Next scheduled host auto-discovery time. Uint32 nextHostProbeTick = 0; ///< Next scheduled host probe time. @@ -4710,6 +4883,7 @@ namespace { SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); resources->renderer = SDL_CreateRenderer(window, -1, 0); + resources->keypadModalLayoutCache.window = window; if (resources->renderer == nullptr) { failure->category = "sdl"; failure->message = std::string("SDL_CreateRenderer failed: ") + SDL_GetError(); @@ -4968,7 +5142,7 @@ namespace { if ((previousScreen != state.shell.activeScreen || update.navigation.screenChanged) && !draw_current_shell_frame(activeVideoMode, state, resources, runtime)) { return; } - if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && !(state.modal.id == app::ModalId::directory_picker && state.directoryPicker.creating)) { + if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && state.modal.id != app::ModalId::directory_picker) { clear_keypad_modal_layout_cache(&resources->keypadModalLayoutCache); } } @@ -5018,7 +5192,8 @@ namespace { finish_shell_background_tasks(state, resources, runtime); start_shell_background_tasks_if_needed(state, runtime, SDL_GetTicks()); - if ((state.shell.activeScreen != app::ScreenId::add_host || !state.addHostDraft.keypad.visible || runtime->keypadRedrawRequested) && !draw_current_shell_frame(videoMode != nullptr ? *videoMode : VIDEO_MODE {}, state, resources, runtime)) { + const bool retainedInputDialog = (state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) || (state.modal.id == app::ModalId::directory_picker && !state.directoryPicker.creating && !state.shell.overlayVisible); + if ((!retainedInputDialog || runtime->keypadRedrawRequested) && !draw_current_shell_frame(videoMode != nullptr ? *videoMode : VIDEO_MODE {}, state, resources, runtime)) { return false; } diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index d434083..5942db8 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -9,6 +9,7 @@ #include "src/platform/filesystem_utils.h" #include +#include #include #include @@ -207,8 +208,8 @@ namespace { switch (category) { case app::SettingsCategory::logging: return "Logging"; - case app::SettingsCategory::display: - return "Display"; + case app::SettingsCategory::streaming: + return "Streaming"; case app::SettingsCategory::input: return "Input"; case app::SettingsCategory::reset: @@ -369,7 +370,7 @@ namespace { } return lines; } - if (state.settings.selectedCategory == app::SettingsCategory::display) { + if (state.settings.selectedCategory == app::SettingsCategory::streaming) { lines.emplace_back("Display options will be added here."); return lines; } @@ -406,7 +407,7 @@ namespace { if (id == "settings-category:logging") { return "icons\\scroll-text.svg"; } - if (id == "settings-category:display") { + if (id == "settings-category:streaming") { return "icons\\monitor.svg"; } if (id == "settings-category:input") { @@ -634,6 +635,7 @@ namespace { } auto &view = viewModel->modal; view.title = "Screenshot Folder"; + view.folderBrowser = true; const auto path = picker.path.empty() ? std::string("Choose a drive") : picker.path; view.lines = {path.size() > 72 ? "..." + path.substr(path.size() - 69) : path}; if (!picker.error.empty()) { @@ -858,6 +860,26 @@ namespace { } // namespace namespace ui { + bool same_folder_dialog_content(const ShellModalView &previous, const ShellModalView ¤t) { + if (!previous.folderBrowser || !current.folderBrowser || !previous.visible || !current.visible || previous.busy || current.busy || previous.title != current.title || previous.iconAssetPath != current.iconAssetPath || previous.lines != current.lines || previous.actions.size() != current.actions.size() || previous.footerActions.size() != current.footerActions.size()) { + return false; + } + for (std::size_t i = 0; i < current.actions.size(); ++i) { + const auto &a = previous.actions[i]; + const auto &b = current.actions[i]; + if (std::tie(a.id, a.label, a.enabled, a.checked, a.iconAssetPath, a.checkable) != std::tie(b.id, b.label, b.enabled, b.checked, b.iconAssetPath, b.checkable)) { + return false; + } + } + for (std::size_t i = 0; i < current.footerActions.size(); ++i) { + const auto &a = previous.footerActions[i]; + const auto &b = current.footerActions[i]; + if (std::tie(a.id, a.label, a.iconAssetPath, a.secondaryIconAssetPath, a.emphasized, a.keyLabel, a.secondaryKeyLabel) != std::tie(b.id, b.label, b.iconAssetPath, b.secondaryIconAssetPath, b.emphasized, b.keyLabel, b.secondaryKeyLabel)) { + return false; + } + } + return true; + } /** * @brief Return whether a screen uses the split menu-and-detail layout. diff --git a/src/ui/shell_view.h b/src/ui/shell_view.h index 40ce313..5295af1 100644 --- a/src/ui/shell_view.h +++ b/src/ui/shell_view.h @@ -157,6 +157,7 @@ namespace ui { struct ShellModalView { bool visible = false; ///< True when a modal dialog should be rendered. bool busy = false; ///< Show progress instead of dismissal controls while work is pending. + bool folderBrowser = false; ///< Retain the folder dialog while only list focus changes. std::string title; ///< Modal dialog title. std::string iconAssetPath; ///< Optional status icon beside the dialog title. std::vector lines; ///< Modal dialog body lines. @@ -164,6 +165,14 @@ namespace ui { std::vector footerActions; ///< Footer actions displayed while a modal is open. }; + /** + * @brief Check whether folder-dialog pixels can be retained while list selection changes. + * @param previous Previously painted dialog. + * @param current Current dialog, including path, error, entries and input helpers. + * @return True when every visible property except the focused action is unchanged. + */ + bool same_folder_dialog_content(const ShellModalView &previous, const ShellModalView ¤t); + /** * @brief Embedded log viewer state surfaced by the log-file modal. */ diff --git a/tests/unit/app/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index b536b5a..a13a5dc 100644 --- a/tests/unit/app/client_state_test.cpp +++ b/tests/unit/app/client_state_test.cpp @@ -155,11 +155,11 @@ namespace { } /** - * @brief Navigates from the initial hosts toolbar selection to the display settings options. + * @brief Navigates from the initial hosts toolbar selection to the streaming settings options. * - * @param state Client state to drive through the display settings commands. + * @param state Client state to drive through the streaming settings commands. */ - void open_display_settings_options(app::ClientState &state) { + void open_streaming_settings_options(app::ClientState &state) { app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::activate); @@ -167,7 +167,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); - ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::display); + ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::streaming); ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); } @@ -1289,7 +1289,7 @@ namespace { EXPECT_TRUE(state.hosts.active.apps.front().favorite); } - TEST(ClientStateTest, DisplaySettingsCanBeActivatedAndBackNavigationReturnsFocusToCategories) { + TEST(ClientStateTest, StreamingSettingsCanBeActivatedAndBackNavigationReturnsFocusToCategories) { app::ClientState state = app::create_initial_state(); state.settings.availableVideoModes = { VIDEO_MODE {640, 480, 32, 60}, @@ -1304,7 +1304,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); - EXPECT_EQ(update.navigation.activatedItemId, "settings-category:display"); + EXPECT_EQ(update.navigation.activatedItemId, "settings-category:streaming"); EXPECT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); update = app::handle_command(state, input::UiCommand::activate); @@ -1321,7 +1321,7 @@ namespace { /** * @brief Changing stream resolution resets custom bitrate, persists both values and refreshes the menu. */ - TEST(ClientStateTest, DisplaySettingsCanCycleXboxVideoModeStreamChoices) { + TEST(ClientStateTest, StreamingSettingsCanCycleXboxVideoModeStreamChoices) { app::ClientState state = app::create_initial_state(); state.settings.availableVideoModes = { VIDEO_MODE {640, 480, 32, 60}, @@ -1332,7 +1332,7 @@ namespace { state.settings.preferredVideoModeSet = true; state.settings.streamBitrateKbps = 750; - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(update.navigation.activatedItemId, "cycle-stream-video-mode"); @@ -1357,13 +1357,13 @@ namespace { /** * @brief Cycling an unchanged or unavailable resolution preserves a manually selected bitrate. */ - TEST(ClientStateTest, DisplaySettingsPreserveCustomBitrateWithoutAResolutionChange) { + TEST(ClientStateTest, StreamingSettingsPreserveCustomBitrateWithoutAResolutionChange) { app::ClientState state = app::create_initial_state(); state.settings.availableVideoModes = {VIDEO_MODE {1280, 720, 32, 60}}; state.settings.preferredVideoMode = state.settings.availableVideoModes.front(); state.settings.preferredVideoModeSet = true; state.settings.streamBitrateKbps = 750; - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(state.settings.streamBitrateKbps, 750); @@ -1382,13 +1382,13 @@ namespace { /** * @brief FPS changes reset bitrate using the selected stream dimensions rather than display refresh. */ - TEST(ClientStateTest, DisplaySettingsCanCycleStreamFramerateThroughSixtyFps) { + TEST(ClientStateTest, StreamingSettingsCanCycleStreamFramerateThroughSixtyFps) { app::ClientState state = app::create_initial_state(); state.settings.preferredVideoMode = VIDEO_MODE {1280, 720, 32, 50}; state.settings.preferredVideoModeSet = true; state.settings.streamBitrateKbps = 750; - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-stream-framerate")); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); @@ -1418,9 +1418,9 @@ namespace { /** * @brief FPS changes before mode detection use the normal 640x480 default dimensions. */ - TEST(ClientStateTest, DisplaySettingsUseDefaultResolutionForFramerateChangesBeforeDetection) { + TEST(ClientStateTest, StreamingSettingsUseDefaultResolutionForFramerateChangesBeforeDetection) { app::ClientState state = app::create_initial_state(); - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-stream-framerate")); app::handle_command(state, input::UiCommand::activate); @@ -1431,10 +1431,10 @@ namespace { /** * @brief Manual bitrate adjustment covers all Xbox defaults, wraps and formats fractional Mbps. */ - TEST(ClientStateTest, DisplaySettingsCycleManualBitrateInMbpsThroughTheHighestDefault) { + TEST(ClientStateTest, StreamingSettingsCycleManualBitrateInMbpsThroughTheHighestDefault) { app::ClientState state = app::create_initial_state(); state.settings.streamBitrateKbps = 7000; - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-stream-bitrate")); EXPECT_EQ(state.detailMenu.selected_item()->label, "Stream Bitrate: 7 Mbps"); @@ -1463,10 +1463,10 @@ namespace { /** * @brief Other display controls leave a manually selected bitrate unchanged. */ - TEST(ClientStateTest, DisplaySettingsPreserveManualBitrateWhenOtherOptionsChange) { + TEST(ClientStateTest, StreamingSettingsPreserveManualBitrateWhenOtherOptionsChange) { app::ClientState state = app::create_initial_state(); state.settings.streamBitrateKbps = 1500; - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); constexpr std::array controls {"toggle-play-audio-on-pc", "toggle-play-audio-on-xbox", "toggle-show-performance-stats"}; for (const char *control : controls) { ASSERT_TRUE(state.detailMenu.select_item_by_id(control)); @@ -1475,12 +1475,12 @@ namespace { } } - TEST(ClientStateTest, DisplaySettingsCanToggleXboxAudioAndPerformanceStats) { + TEST(ClientStateTest, StreamingSettingsCanToggleXboxAudioAndPerformanceStats) { app::ClientState state = app::create_initial_state(); ASSERT_TRUE(state.settings.playAudioOnXbox); ASSERT_FALSE(state.settings.showPerformanceStats); - ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("toggle-play-audio-on-xbox")); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp index 381d403..333881c 100644 --- a/tests/unit/app/directory_picker_test.cpp +++ b/tests/unit/app/directory_picker_test.cpp @@ -53,6 +53,8 @@ namespace { TEST(DirectoryPickerTest, StagesConfiguredAndDefaultLocationsUntilAccepted) { for (const std::string configured : {std::string {}, std::string("F:\\Shots")}) { auto state = open_picker(configured); + EXPECT_EQ(state.settings.selectedCategory, app::SettingsCategory::streaming); + EXPECT_EQ(state.menu.items()[1].label, "Streaming"); EXPECT_EQ(state.directoryPicker.path, configured.empty() ? startup::default_storage_path("screenshots") : configured); auto view = ui::build_shell_view_model(state, {}); EXPECT_EQ(view.modal.title, "Screenshot Folder"); diff --git a/tests/unit/ui/shell_layout_test.cpp b/tests/unit/ui/shell_layout_test.cpp index 30d1385..c2474a2 100644 --- a/tests/unit/ui/shell_layout_test.cpp +++ b/tests/unit/ui/shell_layout_test.cpp @@ -305,3 +305,52 @@ namespace { } } // namespace + +/** + * @brief A list retains its viewport until focus reaches an edge, then scrolls by the necessary rows. + */ +TEST(ShellListViewportTest, KeepsVisibleRowsStableAndScrollsAtTheEdges) { + EXPECT_EQ(ui::stable_list_first_row(80, 0, 8, 0), 0); + EXPECT_EQ(ui::stable_list_first_row(80, 7, 8, 0), 0); + EXPECT_EQ(ui::stable_list_first_row(80, 8, 8, 0), 1); + EXPECT_EQ(ui::stable_list_first_row(80, 9, 8, 1), 2); + EXPECT_EQ(ui::stable_list_first_row(80, 2, 8, 2), 2); + EXPECT_EQ(ui::stable_list_first_row(80, 1, 8, 2), 1); + EXPECT_EQ(ui::stable_list_first_row(80, 79, 8, 0), 72); + EXPECT_EQ(ui::stable_list_first_row(80, 0, 8, 72), 0); +} + +/** + * @brief Empty, short and resized lists clamp stale selection and viewport values without unsigned underflow. + */ +TEST(ShellListViewportTest, HandlesEmptyShortAndResizedLists) { + EXPECT_EQ(ui::stable_list_first_row(0, 100, 8, 100), 0); + EXPECT_EQ(ui::stable_list_first_row(80, 100, 0, 100), 0); + EXPECT_EQ(ui::stable_list_first_row(4, 3, 8, 100), 0); + EXPECT_EQ(ui::stable_list_first_row(8, 7, 8, 100), 0); + EXPECT_EQ(ui::stable_list_first_row(20, 100, 8, 100), 12); + EXPECT_EQ(ui::stable_list_first_row(20, 15, 8, 100), 12); +} + +/** + * @brief Partial presentation covers scaled row edges while remaining inside the physical framebuffer. + */ +TEST(ShellListViewportTest, MapsRepaintsToBoundedPhysicalPixels) { + auto area = ui::framebuffer_repaint_rect({138, 155, 684, 42}, 960, 1280, 720); + EXPECT_EQ(area.x, 182); + EXPECT_EQ(area.y, 154); + EXPECT_EQ(area.w, 916); + EXPECT_EQ(area.h, 44); + area = ui::framebuffer_repaint_rect({-10, -10, 1000, 800}, 960, 1280, 720); + EXPECT_EQ(area.x, 0); + EXPECT_EQ(area.y, 0); + EXPECT_EQ(area.w, 1280); + EXPECT_EQ(area.h, 720); + EXPECT_EQ(ui::framebuffer_repaint_rect({2000, 0, 10, 10}, 960, 1280, 720).w, 0); + EXPECT_EQ(ui::framebuffer_repaint_rect({0, 900, 10, 10}, 960, 1280, 720).h, 0); + EXPECT_EQ(ui::framebuffer_repaint_rect({0, 0, 0, 10}, 960, 1280, 720).w, 0); + EXPECT_EQ(ui::framebuffer_repaint_rect({0, 0, 10, -1}, 960, 1280, 720).h, 0); + EXPECT_EQ(ui::framebuffer_repaint_rect({0, 0, 10, 10}, 0, 1280, 720).w, 0); + EXPECT_EQ(ui::framebuffer_repaint_rect({0, 0, 10, 10}, 960, 0, 720).w, 0); + EXPECT_EQ(ui::framebuffer_repaint_rect({0, 0, 10, 10}, 960, 1280, 0).w, 0); +} diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index 130d8f8..c9e3c53 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -875,3 +875,91 @@ namespace { } } // namespace + +/** + * @brief Folder focus changes can reuse retained pixels; path, listing, error and input-device changes invalidate them. + */ +TEST(ShellViewTest, InvalidatesRetainedFolderContentsForEveryVisibleChange) { + auto state = app::create_initial_state(); + state.modal.id = app::ModalId::directory_picker; + state.directoryPicker.path = "E:\\Shots"; + state.directoryPicker.available = true; + state.directoryPicker.folders = {"E:\\Shots\\Game"}; + const auto original = ui::build_shell_view_model(state, {}).modal; + ASSERT_TRUE(original.folderBrowser); + auto changed = original; + changed.actions[0].selected = false; + changed.actions[4].selected = true; + EXPECT_TRUE(ui::same_folder_dialog_content(original, changed)); + const auto reject = [&](auto mutate) { + auto other = original; + mutate(other); + EXPECT_FALSE(ui::same_folder_dialog_content(original, other)); + EXPECT_FALSE(ui::same_folder_dialog_content(other, original)); + }; + reject([](auto &view) { + view.visible = false; + }); + reject([](auto &view) { + view.folderBrowser = false; + }); + reject([](auto &view) { + view.busy = true; + }); + reject([](auto &view) { + view.title += " changed"; + }); + reject([](auto &view) { + view.iconAssetPath = "icons\\info.svg"; + }); + reject([](auto &view) { + view.lines.push_back("Read-only folder"); + }); + reject([](auto &view) { + view.actions.pop_back(); + }); + reject([](auto &view) { + view.actions[0].id += "-new"; + }); + reject([](auto &view) { + view.actions[0].label += " changed"; + }); + reject([](auto &view) { + view.actions[0].enabled = false; + }); + reject([](auto &view) { + view.actions[0].checked = true; + }); + reject([](auto &view) { + view.actions[0].checkable = true; + }); + reject([](auto &view) { + view.actions[0].iconAssetPath = "icons\\info.svg"; + }); + reject([](auto &view) { + view.footerActions.pop_back(); + }); + reject([](auto &view) { + view.footerActions[0].id += "-new"; + }); + reject([](auto &view) { + view.footerActions[0].label += " changed"; + }); + reject([](auto &view) { + view.footerActions[0].iconAssetPath.clear(); + }); + reject([](auto &view) { + view.footerActions[0].secondaryIconAssetPath = "icons\\button-b.svg"; + }); + reject([](auto &view) { + view.footerActions[0].emphasized = false; + }); + reject([](auto &view) { + view.footerActions[0].keyLabel = "Enter"; + }); + reject([](auto &view) { + view.footerActions[0].secondaryKeyLabel = "Space"; + }); + state.shell.inputDevice = input::UiInputDevice::keyboard; + EXPECT_FALSE(ui::same_folder_dialog_content(original, ui::build_shell_view_model(state, {}).modal)); +} From fd6fb46fa1c5d5958a785cb6fa22bf0f7e1fec7f Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:10:42 -0500 Subject: [PATCH 06/22] fix(settings): put screenshots in a separate Storage category --- README.md | 2 +- src/app/client_state.cpp | 44 +++++++++++++++++++----- src/app/client_state.h | 3 +- src/ui/shell_view.cpp | 30 +++++++++++++--- tests/unit/app/client_state_test.cpp | 43 ++++++++++++----------- tests/unit/app/directory_picker_test.cpp | 34 ++++++++++++++++-- tests/unit/ui/shell_view_test.cpp | 8 +++-- 7 files changed, 122 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index f49a26c..f97a43d 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ the host app running after disconnecting. Returning from the stream resets local keyboard/controller holds before library navigation resumes. Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Open -**Settings > Streaming > Screenshot Folder** to browse drives and subfolders, select the current folder, +**Settings > Storage > Screenshot Folder** to browse drives and subfolders, select the current folder, or create a new folder with the on-screen keyboard or a USB keyboard. **Use default location** restores the title-scoped screenshot folder. **B / Esc** cancels without changing the saved destination. New folder names use up to 42 printable characters; **Start / Enter** creates the folder. The destination diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 8dd235a..1f6a65e 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -80,14 +80,21 @@ namespace { return field == app::AddHostField::address ? "edit-address" : "edit-port"; } + /** + * @brief Map each independent settings category to its stable navigation identifier. + * @param category Settings section to identify. + * @return Menu identifier for Logging, Display, Input, Storage or Reset. + */ std::string settings_category_menu_id(app::SettingsCategory category) { switch (category) { case app::SettingsCategory::logging: return std::string(SETTINGS_CATEGORY_PREFIX) + "logging"; - case app::SettingsCategory::streaming: - return std::string(SETTINGS_CATEGORY_PREFIX) + "streaming"; + case app::SettingsCategory::display: + return std::string(SETTINGS_CATEGORY_PREFIX) + "display"; case app::SettingsCategory::input: return std::string(SETTINGS_CATEGORY_PREFIX) + "input"; + case app::SettingsCategory::storage: + return std::string(SETTINGS_CATEGORY_PREFIX) + "storage"; case app::SettingsCategory::reset: return std::string(SETTINGS_CATEGORY_PREFIX) + "reset"; } @@ -95,27 +102,42 @@ namespace { return std::string(SETTINGS_CATEGORY_PREFIX) + "logging"; } + /** + * @brief Resolve a category identifier while defaulting unknown identifiers to Logging. + * @param itemId Selected navigation identifier. + * @return Corresponding settings category. + */ app::SettingsCategory settings_category_from_menu_id(std::string_view itemId) { - if (itemId == settings_category_menu_id(app::SettingsCategory::streaming)) { - return app::SettingsCategory::streaming; + if (itemId == settings_category_menu_id(app::SettingsCategory::display)) { + return app::SettingsCategory::display; } if (itemId == settings_category_menu_id(app::SettingsCategory::input)) { return app::SettingsCategory::input; } + if (itemId == settings_category_menu_id(app::SettingsCategory::storage)) { + return app::SettingsCategory::storage; + } if (itemId == settings_category_menu_id(app::SettingsCategory::reset)) { return app::SettingsCategory::reset; } return app::SettingsCategory::logging; } + /** + * @brief Describe the options owned by a settings category. + * @param category Section whose scope should be explained. + * @return Category guidance, keeping file destinations separate from Display. + */ const char *settings_category_description(app::SettingsCategory category) { switch (category) { case app::SettingsCategory::logging: return "Control the runtime log file, the in-app log viewer, and xemu debugger output verbosity."; - case app::SettingsCategory::streaming: - return "Set stream video, audio, performance statistics and screenshot storage."; + case app::SettingsCategory::display: + return "Tune streaming video resolution, frame rate, bitrate, audio playback and performance statistics."; case app::SettingsCategory::input: return "Input options will live here when controller and navigation customization is added."; + case app::SettingsCategory::storage: + return "Choose the folder used to save stream screenshots."; case app::SettingsCategory::reset: return "Review and delete Moonlight saved data, or remove everything with a full factory reset."; } @@ -495,7 +517,7 @@ namespace { } /** - * @brief Build navigation actions, exposing host operations only after an address is supplied. + * @brief Build screen actions and settings categories, exposing host operations after an address is supplied. * * @param state Current screen and entered host information. * @return Menu entries available for the current task. @@ -505,8 +527,9 @@ namespace { case app::ScreenId::settings: return { {settings_category_menu_id(app::SettingsCategory::logging), "Logging", settings_category_description(app::SettingsCategory::logging), true}, - {settings_category_menu_id(app::SettingsCategory::streaming), "Streaming", settings_category_description(app::SettingsCategory::streaming), true}, + {settings_category_menu_id(app::SettingsCategory::display), "Display", settings_category_description(app::SettingsCategory::display), true}, {settings_category_menu_id(app::SettingsCategory::input), "Input", settings_category_description(app::SettingsCategory::input), true}, + {settings_category_menu_id(app::SettingsCategory::storage), "Storage", settings_category_description(app::SettingsCategory::storage), true}, {settings_category_menu_id(app::SettingsCategory::reset), "Reset", settings_category_description(app::SettingsCategory::reset), true}, }; case app::ScreenId::add_host: @@ -555,7 +578,7 @@ namespace { {"cycle-log-level", std::string("File Logging Level: ") + logging::to_string(state.settings.loggingLevel), "Choose the minimum severity written to moonlight.log. Lower levels produce more detail but increase disk writes.", true}, {"cycle-xemu-console-log-level", std::string("xemu Console Level: ") + logging::to_string(state.settings.xemuConsoleLoggingLevel), "Choose the minimum severity mirrored to xemu through DbgPrint() when you launch xemu with a serial console.", true}, }; - case app::SettingsCategory::streaming: + case app::SettingsCategory::display: return { { "cycle-stream-video-mode", @@ -593,6 +616,9 @@ namespace { "Show performance statistics during playback and after the stream ends.", true, }, + }; + case app::SettingsCategory::storage: + return { {"screenshot-directory", "Screenshot Folder: " + (state.settings.screenshotDirectory.empty() ? std::string("Default") : state.settings.screenshotDirectory), "Choose or create a folder for stream screenshots. Default: " + startup::default_storage_path("screenshots"), true}, }; case app::SettingsCategory::input: diff --git a/src/app/client_state.h b/src/app/client_state.h index 1c3d463..9aa6cb8 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -85,8 +85,9 @@ namespace app { */ enum class SettingsCategory { logging, ///< Logging and diagnostics options. - streaming, ///< Stream video, audio, telemetry and screenshot options. + display, ///< Display, stream video, audio and telemetry options. input, ///< Input and controller options. + storage, ///< Screenshot destinations and other saved-file location options. reset, ///< Reset and cleanup actions. }; diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index 5942db8..9fc967d 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -204,14 +204,21 @@ namespace { return state.addHostDraft.portInput; } + /** + * @brief Name the selected settings section without conflating Display and Storage. + * @param category Active settings category. + * @return User-facing category name. + */ const char *settings_category_label(app::SettingsCategory category) { switch (category) { case app::SettingsCategory::logging: return "Logging"; - case app::SettingsCategory::streaming: - return "Streaming"; + case app::SettingsCategory::display: + return "Display"; case app::SettingsCategory::input: return "Input"; + case app::SettingsCategory::storage: + return "Storage"; case app::SettingsCategory::reset: return "Reset"; } @@ -345,6 +352,11 @@ namespace { return lines; } + /** + * @brief Describe the selected category and its supported settings. + * @param state Current settings and saved-file state. + * @return Context lines for the settings detail panel. + */ std::vector settings_body_lines(const app::ClientState &state) { std::vector lines = { std::string("Category: ") + settings_category_label(state.settings.selectedCategory), @@ -370,8 +382,13 @@ namespace { } return lines; } - if (state.settings.selectedCategory == app::SettingsCategory::streaming) { - lines.emplace_back("Display options will be added here."); + if (state.settings.selectedCategory == app::SettingsCategory::display) { + lines.emplace_back("Choose stream video, audio and performance statistics options."); + return lines; + } + + if (state.settings.selectedCategory == app::SettingsCategory::storage) { + lines.emplace_back("Choose where screenshots are saved."); return lines; } @@ -407,12 +424,15 @@ namespace { if (id == "settings-category:logging") { return "icons\\scroll-text.svg"; } - if (id == "settings-category:streaming") { + if (id == "settings-category:display") { return "icons\\monitor.svg"; } if (id == "settings-category:input") { return "icons\\gamepad-2.svg"; } + if (id == "settings-category:storage") { + return "icons\\save.svg"; + } if (id == "settings-category:reset") { return "icons\\rotate-ccw.svg"; } diff --git a/tests/unit/app/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index a13a5dc..1f47adc 100644 --- a/tests/unit/app/client_state_test.cpp +++ b/tests/unit/app/client_state_test.cpp @@ -155,11 +155,11 @@ namespace { } /** - * @brief Navigates from the initial hosts toolbar selection to the streaming settings options. + * @brief Navigates from the initial hosts toolbar selection to the display settings options. * - * @param state Client state to drive through the streaming settings commands. + * @param state Client state to drive through the display settings commands. */ - void open_streaming_settings_options(app::ClientState &state) { + void open_display_settings_options(app::ClientState &state) { app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::activate); @@ -167,7 +167,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); - ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::streaming); + ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::display); ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); } @@ -842,6 +842,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::reset); ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); @@ -868,6 +869,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::reset); ASSERT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); @@ -1289,7 +1291,7 @@ namespace { EXPECT_TRUE(state.hosts.active.apps.front().favorite); } - TEST(ClientStateTest, StreamingSettingsCanBeActivatedAndBackNavigationReturnsFocusToCategories) { + TEST(ClientStateTest, DisplaySettingsCanBeActivatedAndBackNavigationReturnsFocusToCategories) { app::ClientState state = app::create_initial_state(); state.settings.availableVideoModes = { VIDEO_MODE {640, 480, 32, 60}, @@ -1304,7 +1306,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); - EXPECT_EQ(update.navigation.activatedItemId, "settings-category:streaming"); + EXPECT_EQ(update.navigation.activatedItemId, "settings-category:display"); EXPECT_EQ(state.settings.focusArea, app::SettingsFocusArea::options); update = app::handle_command(state, input::UiCommand::activate); @@ -1321,7 +1323,7 @@ namespace { /** * @brief Changing stream resolution resets custom bitrate, persists both values and refreshes the menu. */ - TEST(ClientStateTest, StreamingSettingsCanCycleXboxVideoModeStreamChoices) { + TEST(ClientStateTest, DisplaySettingsCanCycleXboxVideoModeStreamChoices) { app::ClientState state = app::create_initial_state(); state.settings.availableVideoModes = { VIDEO_MODE {640, 480, 32, 60}, @@ -1332,7 +1334,7 @@ namespace { state.settings.preferredVideoModeSet = true; state.settings.streamBitrateKbps = 750; - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(update.navigation.activatedItemId, "cycle-stream-video-mode"); @@ -1357,13 +1359,13 @@ namespace { /** * @brief Cycling an unchanged or unavailable resolution preserves a manually selected bitrate. */ - TEST(ClientStateTest, StreamingSettingsPreserveCustomBitrateWithoutAResolutionChange) { + TEST(ClientStateTest, DisplaySettingsPreserveCustomBitrateWithoutAResolutionChange) { app::ClientState state = app::create_initial_state(); state.settings.availableVideoModes = {VIDEO_MODE {1280, 720, 32, 60}}; state.settings.preferredVideoMode = state.settings.availableVideoModes.front(); state.settings.preferredVideoModeSet = true; state.settings.streamBitrateKbps = 750; - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); app::handle_command(state, input::UiCommand::activate); EXPECT_EQ(state.settings.streamBitrateKbps, 750); @@ -1382,13 +1384,13 @@ namespace { /** * @brief FPS changes reset bitrate using the selected stream dimensions rather than display refresh. */ - TEST(ClientStateTest, StreamingSettingsCanCycleStreamFramerateThroughSixtyFps) { + TEST(ClientStateTest, DisplaySettingsCanCycleStreamFramerateThroughSixtyFps) { app::ClientState state = app::create_initial_state(); state.settings.preferredVideoMode = VIDEO_MODE {1280, 720, 32, 50}; state.settings.preferredVideoModeSet = true; state.settings.streamBitrateKbps = 750; - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-stream-framerate")); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); @@ -1418,9 +1420,9 @@ namespace { /** * @brief FPS changes before mode detection use the normal 640x480 default dimensions. */ - TEST(ClientStateTest, StreamingSettingsUseDefaultResolutionForFramerateChangesBeforeDetection) { + TEST(ClientStateTest, DisplaySettingsUseDefaultResolutionForFramerateChangesBeforeDetection) { app::ClientState state = app::create_initial_state(); - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-stream-framerate")); app::handle_command(state, input::UiCommand::activate); @@ -1431,10 +1433,10 @@ namespace { /** * @brief Manual bitrate adjustment covers all Xbox defaults, wraps and formats fractional Mbps. */ - TEST(ClientStateTest, StreamingSettingsCycleManualBitrateInMbpsThroughTheHighestDefault) { + TEST(ClientStateTest, DisplaySettingsCycleManualBitrateInMbpsThroughTheHighestDefault) { app::ClientState state = app::create_initial_state(); state.settings.streamBitrateKbps = 7000; - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("cycle-stream-bitrate")); EXPECT_EQ(state.detailMenu.selected_item()->label, "Stream Bitrate: 7 Mbps"); @@ -1463,10 +1465,10 @@ namespace { /** * @brief Other display controls leave a manually selected bitrate unchanged. */ - TEST(ClientStateTest, StreamingSettingsPreserveManualBitrateWhenOtherOptionsChange) { + TEST(ClientStateTest, DisplaySettingsPreserveManualBitrateWhenOtherOptionsChange) { app::ClientState state = app::create_initial_state(); state.settings.streamBitrateKbps = 1500; - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); constexpr std::array controls {"toggle-play-audio-on-pc", "toggle-play-audio-on-xbox", "toggle-show-performance-stats"}; for (const char *control : controls) { ASSERT_TRUE(state.detailMenu.select_item_by_id(control)); @@ -1475,12 +1477,12 @@ namespace { } } - TEST(ClientStateTest, StreamingSettingsCanToggleXboxAudioAndPerformanceStats) { + TEST(ClientStateTest, DisplaySettingsCanToggleXboxAudioAndPerformanceStats) { app::ClientState state = app::create_initial_state(); ASSERT_TRUE(state.settings.playAudioOnXbox); ASSERT_FALSE(state.settings.showPerformanceStats); - ASSERT_NO_FATAL_FAILURE(open_streaming_settings_options(state)); + ASSERT_NO_FATAL_FAILURE(open_display_settings_options(state)); ASSERT_TRUE(state.detailMenu.select_item_by_id("toggle-play-audio-on-xbox")); app::AppUpdate update = app::handle_command(state, input::UiCommand::activate); @@ -1509,6 +1511,7 @@ namespace { app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); ASSERT_EQ(state.settings.selectedCategory, app::SettingsCategory::reset); diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp index 333881c..7b753d4 100644 --- a/tests/unit/app/directory_picker_test.cpp +++ b/tests/unit/app/directory_picker_test.cpp @@ -26,8 +26,11 @@ namespace { app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::activate); app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); + app::handle_command(state, input::UiCommand::move_down); app::handle_command(state, input::UiCommand::activate); - EXPECT_TRUE(state.detailMenu.select_item_by_id("screenshot-directory")); + state.detailMenu.select_item_by_id("screenshot-directory"); + EXPECT_EQ(state.detailMenu.selected_item()->id, "screenshot-directory"); const auto update = app::handle_command(state, input::UiCommand::activate); EXPECT_TRUE(update.requests.directoryListRequested); EXPECT_TRUE(update.navigation.modalOpened); @@ -53,8 +56,9 @@ namespace { TEST(DirectoryPickerTest, StagesConfiguredAndDefaultLocationsUntilAccepted) { for (const std::string configured : {std::string {}, std::string("F:\\Shots")}) { auto state = open_picker(configured); - EXPECT_EQ(state.settings.selectedCategory, app::SettingsCategory::streaming); - EXPECT_EQ(state.menu.items()[1].label, "Streaming"); + EXPECT_EQ(state.settings.selectedCategory, app::SettingsCategory::storage); + EXPECT_EQ(state.menu.items()[1].label, "Display"); + EXPECT_EQ(state.menu.items()[3].label, "Storage"); EXPECT_EQ(state.directoryPicker.path, configured.empty() ? startup::default_storage_path("screenshots") : configured); auto view = ui::build_shell_view_model(state, {}); EXPECT_EQ(view.modal.title, "Screenshot Folder"); @@ -231,3 +235,27 @@ TEST(DirectoryPickerTest, BuildsScrollableFoldersAndControllerOrKeyboardNameEntr EXPECT_EQ(view.keypad.footerActions[0].keyLabel, "Enter"); EXPECT_EQ(view.keypad.footerActions[1].keyLabel, "Esc"); } + +/** + * @brief Storage owns screenshot destinations while Display retains its original stream and presentation options. + */ +TEST(DirectoryPickerTest, KeepsStorageSeparateFromTheExistingDisplayCategory) { + auto state = open_picker("F:\\Screenshots"); + ASSERT_EQ(state.menu.items().size(), 5); + EXPECT_EQ(state.menu.items()[3].id, "settings-category:storage"); + EXPECT_EQ(state.menu.items()[4].id, "settings-category:reset"); + app::handle_command(state, input::UiCommand::back); + ASSERT_EQ(state.detailMenu.items().size(), 1); + EXPECT_EQ(state.detailMenu.selected_item()->id, "screenshot-directory"); + const auto view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.content.bodyLines[0], "Category: Storage"); + app::handle_command(state, input::UiCommand::back); + app::handle_command(state, input::UiCommand::move_up); + app::handle_command(state, input::UiCommand::move_up); + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(state.settings.selectedCategory, app::SettingsCategory::display); + EXPECT_EQ(state.detailMenu.items().size(), 6); + EXPECT_EQ(state.detailMenu.selected_item()->id, "cycle-stream-video-mode"); + EXPECT_FALSE(state.detailMenu.select_item_by_id("screenshot-directory")); + EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Screenshots"); +} diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index c9e3c53..a1a0cdc 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -150,7 +150,7 @@ namespace { } /** - * @brief Every settings category has a recognizable packaged Lucide icon. + * @brief All five settings categories, including separate Display and Storage sections, have packaged icons. */ TEST(ShellViewTest, DecoratesSettingsCategoriesWithIcons) { app::ClientState state = app::create_initial_state(); @@ -158,11 +158,13 @@ namespace { app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::activate); const auto view = ui::build_shell_view_model(state, {}); - ASSERT_EQ(view.content.menuRows.size(), 4U); + ASSERT_EQ(view.content.menuRows.size(), 5U); EXPECT_EQ(view.content.menuRows[0].iconAssetPath, "icons\\scroll-text.svg"); EXPECT_EQ(view.content.menuRows[1].iconAssetPath, "icons\\monitor.svg"); EXPECT_EQ(view.content.menuRows[2].iconAssetPath, "icons\\gamepad-2.svg"); - EXPECT_EQ(view.content.menuRows[3].iconAssetPath, "icons\\rotate-ccw.svg"); + EXPECT_EQ(view.content.menuRows[3].label, "Storage"); + EXPECT_EQ(view.content.menuRows[3].iconAssetPath, "icons\\save.svg"); + EXPECT_EQ(view.content.menuRows[4].iconAssetPath, "icons\\rotate-ccw.svg"); } /** From 433198f848843504791da5b8c8abbdd0463a17c0 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:21:58 -0500 Subject: [PATCH 07/22] feat(ui): add per-app screenshot viewer --- README.md | 10 + src/app/client_state.cpp | 43 +++- src/app/client_state.h | 16 +- src/app/screenshot_library.cpp | 259 +++++++++++++++++++++ src/app/screenshot_library.h | 107 +++++++++ src/platform/filesystem_utils.cpp | 3 + src/platform/filesystem_utils.h | 2 +- src/streaming/session.cpp | 2 +- src/streaming/stream_controls.cpp | 19 +- src/streaming/stream_controls.h | 5 +- src/ui/screenshot_image.cpp | 61 +++++ src/ui/screenshot_image.h | 37 +++ src/ui/shell_screen.cpp | 140 +++++++++-- src/ui/shell_view.cpp | 24 +- src/ui/shell_view.h | 11 + tests/unit/app/screenshot_library_test.cpp | 244 +++++++++++++++++++ tests/unit/ui/screenshot_image_test.cpp | 106 +++++++++ tests/unit/ui/shell_view_test.cpp | 4 +- 18 files changed, 1063 insertions(+), 30 deletions(-) create mode 100644 src/app/screenshot_library.cpp create mode 100644 src/app/screenshot_library.h create mode 100644 src/ui/screenshot_image.cpp create mode 100644 src/ui/screenshot_image.h create mode 100644 tests/unit/app/screenshot_library_test.cpp create mode 100644 tests/unit/ui/screenshot_image_test.cpp diff --git a/README.md b/README.md index f97a43d..edad0bd 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,16 @@ saved, and an empty value restores the default. Use a TOML literal string for Xbox paths, for example `screenshot_directory = 'F:\Screenshots'`. The same section stores `keyboard_dock_top = false` (bottom) or `true` (top). +Open a game or app's action menu and choose **Screenshots** to view its captures. The viewer works +offline and shows the newest capture first; use **LB / RB** or **PgUp / PgDn** to browse, and +**B / Esc** to return to the app's actions. Left and right navigation also changes images. +New screenshots are linked to the server UUID and app ID in the title-scoped `screenshots.tsv` index; +servers without a UUID use their saved address and port. Renaming a server or app does not change its +library. Changing the screenshot folder preserves access to previously indexed locations. Keep the +index with your save data, and keep the bitmap files in their original locations. Moved or deleted +files display an unavailable message. Captures saved before this index was introduced remain on disk +and are not automatically assigned to an app. + Mouse mode uses the invoking controller's **left stick** for movement and **A / B** for left / right clicks. Stick-driven mouse movement is reported at up to 125 Hz, while button presses and releases are sent immediately. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 1f6a65e..5f580c3 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -679,6 +679,9 @@ namespace { if (state.modal.id == app::ModalId::directory_picker) { state.directoryPicker = {}; } + if (state.modal.id == app::ModalId::screenshot_viewer) { + state.screenshotViewer = {}; + } state.modal = {}; reset_confirmation(state); } @@ -764,7 +767,7 @@ namespace { case app::ModalId::host_actions: return 4U; case app::ModalId::app_actions: - return 3U; + return 4U; case app::ModalId::confirmation: return 2U; case app::ModalId::directory_picker: @@ -775,6 +778,7 @@ namespace { case app::ModalId::app_details: case app::ModalId::log_viewer: case app::ModalId::connection_test: + case app::ModalId::screenshot_viewer: return 0U; } return 0U; @@ -1187,7 +1191,7 @@ namespace { } /** - * @brief Open a paired PC's library or notify the user why it cannot be opened. + * @brief Open a paired PC's library, retaining offline access to cached apps and screenshots. * * @param state Mutable client state. * @param showHiddenApps Include hidden apps in the library. @@ -1198,7 +1202,7 @@ namespace { if (host == nullptr) { return false; } - if (host->reachability == app::HostReachability::offline) { + if (host->reachability == app::HostReachability::offline && host->apps.empty()) { state.shell.statusMessage = "Host is offline. Bring it online before opening apps."; app::show_notification(state, app::NotificationKind::error, "PC is offline", "Turn on the PC and make sure Sunshine is running to open its apps."); return false; @@ -1214,8 +1218,9 @@ namespace { state.apps.selectedAppIndex = 0U; state.apps.scrollPage = 0U; state.apps.refreshFailureNotified = false; - state.hosts.active.appListState = app::HostAppListState::loading; - state.hosts.active.appListStatusMessage = (state.hosts.active.apps.empty() ? "Loading apps for " : "Refreshing apps for ") + state.hosts.active.displayName + "..."; + const bool offline = state.hosts.active.reachability == app::HostReachability::offline; + state.hosts.active.appListState = offline ? app::HostAppListState::ready : app::HostAppListState::loading; + state.hosts.active.appListStatusMessage = offline ? "PC offline. Showing saved apps and local screenshots." : (state.hosts.active.apps.empty() ? "Loading apps for " : "Refreshing apps for ") + state.hosts.active.displayName + "..."; state.shell.statusMessage.clear(); set_screen(state, app::ScreenId::apps); return true; @@ -1492,7 +1497,7 @@ namespace { } app::HostAppRecord &appRecord = mutableHost->apps[indices[state.apps.selectedAppIndex]]; - switch (state.modal.selectedActionIndex % 3U) { + switch (state.modal.selectedActionIndex % 4U) { case 0: appRecord.hidden = !appRecord.hidden; state.hosts.dirty = true; @@ -1513,6 +1518,14 @@ namespace { update->persistence.hostsChanged = true; } return true; + case 3: + state.screenshotViewer = {}; + state.screenshotViewer.source = app::screenshot_source(*host, appRecord); + open_modal(state, app::ModalId::screenshot_viewer); + if (update != nullptr) { + update->requests.screenshotsRequested = true; + } + return true; default: return true; } @@ -1535,6 +1548,7 @@ namespace { return true; case app::ModalId::log_viewer: case app::ModalId::directory_picker: + case app::ModalId::screenshot_viewer: return true; case app::ModalId::confirmation: return handle_confirmation_modal_activation(state, update); @@ -1671,6 +1685,23 @@ namespace { return true; } + if (state.modal.id == app::ModalId::screenshot_viewer) { + auto &viewer = state.screenshotViewer; + if (command == input::UiCommand::back) { + viewer = {}; + open_modal(state, app::ModalId::app_actions, 3U); + } else if (!viewer.entries.empty()) { + const auto count = viewer.entries.size(); + viewer.selectedIndex = std::min(viewer.selectedIndex, count - 1); + if (command == input::UiCommand::move_left || command == input::UiCommand::previous_page) { + viewer.selectedIndex = (viewer.selectedIndex + count - 1) % count; + } else if (command == input::UiCommand::move_right || command == input::UiCommand::next_page) { + viewer.selectedIndex = (viewer.selectedIndex + 1) % count; + } + } + return true; + } + if (handle_directory_picker_command(state, command, update)) { return true; } diff --git a/src/app/client_state.h b/src/app/client_state.h index 9aa6cb8..88ad524 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -18,6 +18,7 @@ // standard includes #include "src/app/host_records.h" #include "src/app/pairing_flow.h" +#include "src/app/screenshot_library.h" #include "src/input/navigation_input.h" #include "src/input/stream_menu.h" #include "src/input/ui_input.h" @@ -61,6 +62,7 @@ namespace app { log_viewer, ///< Dedicated log viewer modal. connection_test, ///< Progress and result of a manual connection test. directory_picker, ///< Screenshot destination browser and local folder-name keyboard. + screenshot_viewer, ///< Local captures associated with the selected server and app. }; /** @@ -391,7 +393,17 @@ namespace app { }; /** - * @brief Serializable app state for the menu-driven client shell. + * @brief Transient captures and selection for one app's local screenshot viewer. + */ + struct ScreenshotViewerState { + ScreenshotSource source; ///< Stable app and host identity captured when the viewer opens. + std::vector entries; ///< Matching captures ordered newest first. + std::size_t selectedIndex = 0; ///< Image currently displayed; zero for an empty library. + std::string error; ///< Any recoverable journal warning or read error. + }; + + /** + * @brief Complete local application state, including transient screenshot browsing. */ struct ClientState { ShellState shell; ///< Shell-wide status and overlay state. @@ -405,6 +417,7 @@ namespace app { ModalState modal; ///< Context modal currently stacked over the shell. SettingsState settings; ///< Settings, log viewer, and saved-file workflow state. DirectoryPickerState directoryPicker; ///< Pending screenshot-directory workflow. + ScreenshotViewerState screenshotViewer; ///< Local app screenshot library, released when browsing ends. ConfirmationDialogState confirmation; ///< Confirmation dialog content for destructive actions. }; @@ -440,6 +453,7 @@ namespace app { bool directoryListRequested = false; ///< Read the picker's current directory or mounted drives. bool directoryCreateRequested = false; ///< Create the named child of the current picker directory. bool directorySelectRequested = false; ///< Check destination writability before accepting the picker directory. + bool screenshotsRequested = false; ///< Load captures for the viewer's retained server and app identity. }; /** diff --git a/src/app/screenshot_library.cpp b/src/app/screenshot_library.cpp new file mode 100644 index 0000000..b90148f --- /dev/null +++ b/src/app/screenshot_library.cpp @@ -0,0 +1,259 @@ +/** + * @file src/app/screenshot_library.cpp + * @brief Implements the append-only screenshot journal and source matching. + */ +// class header include +#include "src/app/screenshot_library.h" + +// standard includes +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// local includes +#include "src/platform/filesystem_utils.h" +#include "src/startup/storage_paths.h" + +namespace { + constexpr std::size_t MAX_RECORD_BYTES = 8192; ///< Bound journal parsing and reject unbounded damaged lines. + + /** + * @brief Escape arbitrary labels and paths into one tab-separated journal field. + * @param field Unescaped field value. + * @return Escaped field with no literal record or field separators. + */ + std::string escape_field(std::string_view field) { + std::string result; + for (char c : field) { + switch (c) { + case '\\': + result += "\\\\"; + break; + case '\t': + result += "\\t"; + break; + case '\n': + result += "\\n"; + break; + case '\r': + result += "\\r"; + break; + default: + result += c; + break; + } + } + return result; + } + + /** + * @brief Compare UUIDs case-insensitively without changing endpoint or display labels. + * @param first First UUID. + * @param second Second UUID. + * @return True when the UUID bytes agree apart from ASCII case. + */ + bool same_uuid(std::string_view first, std::string_view second) { + return first.size() == second.size() && std::equal(first.begin(), first.end(), second.begin(), [](unsigned char a, unsigned char b) { + return std::tolower(a) == std::tolower(b); + }); + } +} // namespace + +namespace app { + ScreenshotSource screenshot_source(const HostRecord &host, const HostAppRecord &application) { + return {host.uuid, host.address, effective_host_port(host.port), host.displayName, application.id, application.name}; + } + + bool screenshot_source_matches(const ScreenshotSource &captured, const ScreenshotSource &requested) { + if (captured.appId != requested.appId) { + return false; + } + if (!captured.hostUuid.empty() && !requested.hostUuid.empty()) { + return same_uuid(captured.hostUuid, requested.hostUuid); + } + return !captured.hostAddress.empty() && captured.hostAddress == requested.hostAddress && effective_host_port(captured.hostPort) == effective_host_port(requested.hostPort); + } + + std::string screenshot_library_path() { + return startup::default_storage_path("screenshots.tsv"); + } + + std::string serialize_screenshot(const ScreenshotRecord &record) { + if (record.path.empty() || record.capturedAt < 0 || record.source.appId <= 0 || (record.source.hostUuid.empty() && record.source.hostAddress.empty())) { + return {}; + } + std::string result = "1\t" + std::to_string(record.capturedAt) + "\t" + std::to_string(record.source.appId) + "\t" + std::to_string(effective_host_port(record.source.hostPort)); + for (const auto *field : {&record.source.hostUuid, &record.source.hostAddress, &record.source.hostName, &record.source.appName, &record.path}) { + if (field->find('\0') != std::string::npos) { + return {}; + } + result += '\t' + escape_field(*field); + } + result += "\tend"; + return result.size() < MAX_RECORD_BYTES ? result : std::string {}; + } + + std::optional parse_screenshot(std::string_view line) { + if (line.empty() || line.size() >= MAX_RECORD_BYTES || line.find('\0') != std::string_view::npos) { + return std::nullopt; + } + std::vector fields(1); + bool escaped = false; + for (char c : line) { + if (escaped) { + switch (c) { + case '\\': + fields.back() += '\\'; + break; + case 't': + fields.back() += '\t'; + break; + case 'n': + fields.back() += '\n'; + break; + case 'r': + fields.back() += '\r'; + break; + default: + return std::nullopt; + } + escaped = false; + } else if (c == '\\') { + escaped = true; + } else if (c == '\t') { + fields.emplace_back(); + } else if (c == '\n' || c == '\r') { + return std::nullopt; + } else { + fields.back() += c; + } + } + if (escaped || fields.size() != 10 || fields[0] != "1" || fields[9] != "end") { + return std::nullopt; + } + ScreenshotRecord record; + unsigned int port = 0; + std::istringstream numbers(fields[1] + " " + fields[2] + " " + fields[3]); + if (!(numbers >> record.capturedAt >> record.source.appId >> port) || !(numbers >> std::ws).eof() || port == 0 || port > 65535) { + return std::nullopt; + } + record.source.hostPort = static_cast(port); + record.source.hostUuid = fields[4]; + record.source.hostAddress = fields[5]; + record.source.hostName = fields[6]; + record.source.appName = fields[7]; + record.path = fields[8]; + if (serialize_screenshot(record).empty()) { + return std::nullopt; + } + return record; + } + + bool append_screenshot(const ScreenshotRecord &record, const std::string &journalPath, std::string &error) { + error.clear(); + const std::string line = serialize_screenshot(record); + if (line.empty()) { + error = "Invalid screenshot metadata."; + return false; + } + if (!platform::ensure_directory_exists(platform::parent_directory(journalPath), &error)) { + return false; + } + FILE *file = std::fopen(journalPath.c_str(), "ab"); + if (file == nullptr) { + error = "Cannot open screenshot index: " + std::string(std::strerror(errno)); + return false; + } + // A leading newline isolates a previously interrupted append. Only newline-terminated + // records are read, so an incomplete capture never poisons the remaining index. + const std::string append = "\n" + line + "\n"; + const bool written = std::fwrite(append.data(), 1, append.size(), file) == append.size(); + const bool closed = std::fclose(file) == 0; + if (!written || !closed) { + error = "Cannot finalize screenshot index."; + return false; + } + return true; + } + + ScreenshotLibraryResult load_screenshots(const ScreenshotSource &source, const std::string &journalPath) { + ScreenshotLibraryResult result; + FILE *file = std::fopen(journalPath.c_str(), "rb"); + if (file == nullptr) { + if (errno != ENOENT) { + result.error = "Cannot read screenshot index: " + std::string(std::strerror(errno)); + } + return result; + } + std::array buffer {}; + bool oversized = false; + while (std::fgets(buffer.data(), static_cast(buffer.size()), file) != nullptr) { + std::string_view line(buffer.data()); + const bool complete = !line.empty() && line.back() == '\n'; + if (!complete || oversized) { + oversized = !complete; + result.error = "Some incomplete or damaged screenshot records were skipped."; + continue; + } + line.remove_suffix(1); + if (!line.empty() && line.back() == '\r') { + line.remove_suffix(1); + } + if (line.empty()) { + continue; + } + if (auto record = parse_screenshot(line); record.has_value()) { + if (screenshot_source_matches(record->source, source)) { + result.entries.push_back(std::move(*record)); + } + } else { + result.error = "Some incomplete or damaged screenshot records were skipped."; + } + } + if (std::ferror(file) != 0) { + result.error = "Reading the screenshot index failed."; + } + std::fclose(file); + // Reverse first to give the newest append priority when the clock has not advanced. + std::reverse(result.entries.begin(), result.entries.end()); + std::stable_sort(result.entries.begin(), result.entries.end(), [](const ScreenshotRecord &a, const ScreenshotRecord &b) { + return a.capturedAt > b.capturedAt; + }); + return result; + } + + std::string next_screenshot_path(std::string_view directory, std::int64_t capturedAt, std::string &error) { + error.clear(); + const std::string folder = startup::screenshot_storage_path(directory, {}); + if (capturedAt < 0 || !platform::ensure_directory_exists(folder, &error)) { + if (error.empty()) { + error = "Invalid screenshot timestamp."; + } + return {}; + } + for (unsigned int suffix = 0; suffix < 10000; ++suffix) { + const std::string path = folder + "screenshot-" + std::to_string(capturedAt) + "-" + std::to_string(suffix) + ".bmp"; + if (!platform::try_get_file_size(path)) { + return path; + } + } + error = "No unused screenshot filename is available."; + return {}; + } + + std::string screenshot_time_label(std::int64_t capturedAt) { + const auto time = static_cast(capturedAt); + std::array label {}; + const std::tm *utc = capturedAt >= 0 && static_cast(time) == capturedAt ? std::gmtime(&time) : nullptr; + if (utc == nullptr || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", utc) == 0) { + return "Capture time unavailable"; + } + return label.data(); + } +} // namespace app diff --git a/src/app/screenshot_library.h b/src/app/screenshot_library.h new file mode 100644 index 0000000..b8a01cb --- /dev/null +++ b/src/app/screenshot_library.h @@ -0,0 +1,107 @@ +/** + * @file src/app/screenshot_library.h + * @brief Associates local screenshots with their host and app, independently of the current capture folder. + */ +#pragma once + +// standard includes +#include +#include +#include +#include +#include + +// local includes +#include "src/app/host_records.h" + +namespace app { + /** + * @brief Identity and display labels of the stream that produced a capture. + */ + struct ScreenshotSource { + std::string hostUuid; ///< Stable server UUID, preferred over its network endpoint. + std::string hostAddress; ///< Saved endpoint used when a UUID is unavailable. + uint16_t hostPort = DEFAULT_HOST_PORT; ///< Effective HTTP port of the saved endpoint. + std::string hostName; ///< Server label at capture time. + int appId = 0; ///< Host-defined app identifier. + std::string appName; ///< App label at capture time. + }; + + /** + * @brief One saved bitmap and its stream provenance. + */ + struct ScreenshotRecord { + ScreenshotSource source; ///< Host and app responsible for this capture. + std::string path; ///< Original bitmap location, retained when the capture folder changes. + std::int64_t capturedAt = 0; ///< Capture time in UTC seconds since the Unix epoch. + }; + + /** + * @brief Captures returned from the local journal, newest first. + */ + struct ScreenshotLibraryResult { + std::vector entries; ///< Matching records, including unavailable images for explicit viewer errors. + std::string error; ///< Read failures or a warning about skipped damaged records. + }; + + /** + * @brief Snapshot a host and app's persistent identity for a capture or lookup. + * @param host Server record, including its saved endpoint and UUID. + * @param application Selected host app. + * @return Identity and names independent of subsequent shell selection changes. + */ + ScreenshotSource screenshot_source(const HostRecord &host, const HostAppRecord &application); + /** + * @brief Match app IDs and server UUIDs, falling back to endpoints only when a UUID is missing. + * @param captured Source stored with the capture. + * @param requested Source currently being browsed. + * @return True for the same app on the same server. + */ + bool screenshot_source_matches(const ScreenshotSource &captured, const ScreenshotSource &requested); + /** + * @brief Locate the title-scoped screenshot journal, separate from the configurable bitmap folder. + * @return Persistent journal path. + */ + std::string screenshot_library_path(); + /** + * @brief Encode one versioned journal record with escaped tabs, newlines and backslashes. + * @param record Complete capture metadata. + * @return Single journal line without its terminating newline; empty for invalid metadata. + */ + std::string serialize_screenshot(const ScreenshotRecord &record); + /** + * @brief Validate and decode a single journal record. + * @param line Serialized metadata without a terminating newline. + * @return Record, or no value for damaged or unsupported data. + */ + std::optional parse_screenshot(std::string_view line); + /** + * @brief Append a completed capture without rewriting existing journal records. + * @param record Metadata for a bitmap that has already been saved. + * @param journalPath Journal destination. + * @param error Receives any write or validation failure. + * @return True when the record was finalized successfully. + */ + bool append_screenshot(const ScreenshotRecord &record, const std::string &journalPath, std::string &error); + /** + * @brief Read locally indexed captures for an app without contacting the host. + * @param source Server and app to match. + * @param journalPath Journal to read; a missing journal is an empty library. + * @return Matching captures and any recoverable journal warning. + */ + ScreenshotLibraryResult load_screenshots(const ScreenshotSource &source, const std::string &journalPath); + /** + * @brief Choose a FATX-compatible capture name that never replaces an existing bitmap. + * @param directory Configured capture folder, or empty for the default. + * @param capturedAt UTC timestamp used in the filename. + * @param error Receives directory or name-allocation failures. + * @return Unused bitmap path, or empty on failure. The event thread owns capture creation. + */ + std::string next_screenshot_path(std::string_view directory, std::int64_t capturedAt, std::string &error); + /** + * @brief Format capture time for the viewer without relying on a local timezone configuration. + * @param capturedAt UTC timestamp stored in the journal. + * @return Readable UTC time, or an unavailable-time label. + */ + std::string screenshot_time_label(std::int64_t capturedAt); +} // namespace app diff --git a/src/platform/filesystem_utils.cpp b/src/platform/filesystem_utils.cpp index 505d875..d6f91a3 100644 --- a/src/platform/filesystem_utils.cpp +++ b/src/platform/filesystem_utils.cpp @@ -349,6 +349,9 @@ namespace platform { } bool try_get_file_size(std::string_view path, std::uint64_t *sizeBytes) { +#ifdef NXDK + mount_directory_drive(path); +#endif #if defined(_WIN32) || defined(NXDK) WIN32_FILE_ATTRIBUTE_DATA fileData {}; if (const std::string ownedPath(path); !GetFileAttributesExA(ownedPath.c_str(), GetFileExInfoStandard, &fileData)) { diff --git a/src/platform/filesystem_utils.h b/src/platform/filesystem_utils.h index 7663f08..be4346a 100644 --- a/src/platform/filesystem_utils.h +++ b/src/platform/filesystem_utils.h @@ -107,7 +107,7 @@ namespace platform { bool ensure_directory_exists(const std::string &directoryPath, std::string *errorMessage = nullptr); /** - * @brief Try to read the size of a regular file. + * @brief Try to read the size of a regular file, mounting its standard Xbox drive when necessary. * * @param path File path to inspect. * @param sizeBytes Optional output for the file size in bytes. diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index 6d96349..92faa24 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -1505,7 +1505,7 @@ namespace streaming { logging::error("stream", initializationError); return false; } - resources.controller.configure(settings.screenshotDirectory, settings.keyboardDockTop); + resources.controller.configure(settings.screenshotDirectory, settings.keyboardDockTop, app::screenshot_source(host, app)); ScopedThreadPriority streamThreadPriority(SDL_THREAD_PRIORITY_HIGH); startup::log_memory_statistics(); diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 796e165..2b18370 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -6,7 +6,6 @@ #include "src/logging/log_file.h" #include "src/logging/logger.h" -#include "src/startup/storage_paths.h" #include "src/streaming/ffmpeg_stream_backend.h" #include "src/ui/asset_path.h" #include "src/ui/dialog_surface.h" @@ -15,6 +14,7 @@ #include #include +#include #include #include @@ -286,9 +286,10 @@ namespace streaming { poll(); } - void StreamControls::configure(std::string screenshotDirectory, bool keyboardTop) { + void StreamControls::configure(std::string screenshotDirectory, bool keyboardTop, app::ScreenshotSource source) { screenshotDirectory_ = std::move(screenshotDirectory); keyboardTop_ = keyboardTop; + screenshotSource_ = std::move(source); } bool StreamControls::keyboard_docked_top() const { @@ -540,8 +541,18 @@ namespace streaming { break; case input::StreamAction::screenshot: { - const std::string path = startup::screenshot_storage_path(screenshotDirectory_, "screenshot-" + std::to_string(SDL_GetTicks()) + ".bmp"); - message_ = backend.save_screenshot(path) ? "Saved " + path : "Screenshot failed; check the logs."; + const auto capturedAt = static_cast(std::time(nullptr)); + std::string error; + const std::string path = app::next_screenshot_path(screenshotDirectory_, capturedAt, error); + if (!path.empty() && backend.save_screenshot(path)) { + const app::ScreenshotRecord record {screenshotSource_, path, capturedAt}; + message_ = app::append_screenshot(record, app::screenshot_library_path(), error) ? "Saved " + path : "Saved " + path + "; could not add it to the viewer."; + } else { + message_ = "Screenshot failed; check the logs."; + } + if (!error.empty()) { + logging::warn("stream", error); + } logging::info("stream", message_); break; } diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 9f42286..940d5c5 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -4,6 +4,7 @@ */ #pragma once +#include "src/app/screenshot_library.h" #include "src/input/stream_exit.h" #include "src/input/stream_input.h" #include "src/input/stream_menu.h" @@ -48,8 +49,9 @@ namespace streaming { * @brief Apply persisted local-control preferences for this session. * @param screenshotDirectory Screenshot folder, or empty for the title-scoped default. * @param keyboardTop True to dock the keyboard to the top edge. + * @param source Stable host and app metadata for saved screenshots. */ - void configure(std::string screenshotDirectory, bool keyboardTop); + void configure(std::string screenshotDirectory, bool keyboardTop, app::ScreenshotSource source = {}); /** * @brief Read the placement selected while streaming. * @return True for top docking. @@ -234,6 +236,7 @@ namespace streaming { bool keyboardDockTriggerDown_ = false; ///< Edge latch for right-trigger keyboard docking. bool keyboardTop_ = false; ///< Dock the keyboard flush with the top edge. std::string screenshotDirectory_; ///< Configured screenshot folder, empty for the default. + app::ScreenshotSource screenshotSource_; ///< Stream identity retained with each capture. Uint32 logRefreshTick_ = 0; ///< Last live log refresh. std::map, std::shared_ptr> icons_; ///< Cached SVG rasters survive panel renderer replacement. std::unique_ptr panel_ {nullptr, SDL_FreeSurface}; ///< Retained software composition target. diff --git a/src/ui/screenshot_image.cpp b/src/ui/screenshot_image.cpp new file mode 100644 index 0000000..bbb59b1 --- /dev/null +++ b/src/ui/screenshot_image.cpp @@ -0,0 +1,61 @@ +/** + * @file src/ui/screenshot_image.cpp + * @brief Implements bounded BMP validation and aspect-preserving screenshot layout. + */ +// class header include +#include "src/ui/screenshot_image.h" + +// standard includes +#include + +namespace { + /** + * @brief Read one little-endian unsigned header field without unaligned pointer casts. + * @param header Already length-checked BMP header. + * @param offset First field byte. + * @param count Number of field bytes, at most four. + * @return Decoded unsigned value. + */ + std::uint32_t little_endian(std::string_view header, std::size_t offset, std::size_t count) { + std::uint32_t value = 0; + for (std::size_t i = 0; i < count; ++i) { + value |= static_cast(static_cast(header[offset + i])) << (8U * i); + } + return value; + } +} // namespace + +namespace ui { + ScreenshotImageSize screenshot_bitmap_size(std::string_view header, std::uint64_t fileSize) { + if (header.size() < 54 || header.substr(0, 2) != "BM" || fileSize > 9U * 1024U * 1024U) { + return {}; + } + const auto offset = little_endian(header, 10, 4); + const auto dibSize = little_endian(header, 14, 4); + const auto width = little_endian(header, 18, 4); + const auto height = little_endian(header, 22, 4); + const auto planes = little_endian(header, 26, 2); + const auto bits = little_endian(header, 28, 2); + const auto compression = little_endian(header, 30, 4); + // Captures are 24-bit BI_RGB. Also accept ordinary 32-bit BI_RGB files, with + // a two-megapixel ceiling so a damaged header cannot exhaust the Xbox heap. + if (dibSize < 40 || dibSize > 124 || offset < 14U + dibSize || width == 0 || width > 1920 || height == 0 || height > 1080 || planes != 1 || (bits != 24 && bits != 32) || compression != 0) { + return {}; + } + const std::uint64_t stride = (static_cast(width) * bits + 31U) / 32U * 4U; + if (offset + stride * height > fileSize) { + return {}; + } + return {static_cast(width), static_cast(height)}; + } + + ShellRect fit_screenshot(ScreenshotImageSize image, ShellRect area) { + if (image.width <= 0 || image.height <= 0 || area.w <= 0 || area.h <= 0) { + return {}; + } + const bool fitWidth = static_cast(image.width) * area.h > static_cast(image.height) * area.w; + const int width = fitWidth ? area.w : std::max(1, static_cast(static_cast(image.width) * area.h / image.height)); + const int height = fitWidth ? std::max(1, static_cast(static_cast(image.height) * area.w / image.width)) : area.h; + return {area.x + (area.w - width) / 2, area.y + (area.h - height) / 2, width, height}; + } +} // namespace ui diff --git a/src/ui/screenshot_image.h b/src/ui/screenshot_image.h new file mode 100644 index 0000000..8a31362 --- /dev/null +++ b/src/ui/screenshot_image.h @@ -0,0 +1,37 @@ +/** + * @file src/ui/screenshot_image.h + * @brief Validates capture bitmaps before allocation and fits them without cropping. + */ +#pragma once + +// standard includes +#include +#include + +// local includes +#include "src/ui/shell_layout.h" + +namespace ui { + /** + * @brief Dimensions of a supported uncompressed screenshot bitmap. + */ + struct ScreenshotImageSize { + int width = 0; ///< Pixel width; zero for an unsupported or invalid bitmap. + int height = 0; ///< Pixel height; zero for an unsupported or invalid bitmap. + }; + + /** + * @brief Validate a BMP header and file length before SDL allocates its image surface. + * @param header First 54 bytes of the bitmap. + * @param fileSize Total bytes available, including pixel data. + * @return Supported dimensions, or zeroes for damaged, oversized or unsupported images. + */ + ScreenshotImageSize screenshot_bitmap_size(std::string_view header, std::uint64_t fileSize); + /** + * @brief Center an entire screenshot in the available square-pixel UI area. + * @param image Source pixel dimensions. + * @param area Available rectangle before display encoder correction. + * @return Aspect-preserving image bounds, or an empty rectangle for invalid inputs. + */ + ShellRect fit_screenshot(ScreenshotImageSize image, ShellRect area); +} // namespace ui diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 50e0060..c213d1b 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -47,6 +47,7 @@ #include "src/ui/asset_path.h" #include "src/ui/dialog_surface.h" #include "src/ui/host_probe_result_queue.h" +#include "src/ui/screenshot_image.h" #include "src/ui/shell_layout.h" #include "src/ui/shell_view.h" #include "src/ui/svg_assets.h" @@ -256,11 +257,21 @@ namespace { }; /** - * @brief Stores reusable layout, text and retained folder-dialog state for local input modals. + * @brief Retains one decoded screenshot and its load result for the current renderer. + */ + struct ScreenshotTextureCache { + SDL_Texture *texture = nullptr; ///< Single open bitmap, released before a different image is decoded. + std::string path; ///< Last attempted image path, including failed loads. + std::string error; ///< User-facing unavailable or decoding error for this path. + }; + + /** + * @brief Renderer-owned resources retained across local input and image dialogs. */ struct KeypadModalLayoutCache { SDL_Window *window = nullptr; ///< Shared window; needed only for validated partial Xbox framebuffer presentation. FolderDialogCache folder; ///< Retained folder browser, sharing the local-dialog lifetime. + ScreenshotTextureCache screenshot; ///< At most one decoded capture is retained. int modalInnerWidth = 0; ///< Last modal content width used to build the cached line layout. int modalTextHeight = 0; ///< Cached combined height of the modal body text. std::vector lines; ///< Last body lines used to populate the cache. @@ -374,6 +385,8 @@ namespace { } clear_folder_dialog_cache(cache->folder); + destroy_texture(cache->screenshot.texture); + cache->screenshot = {}; clear_cached_text_texture(&cache->titleTexture); for (CachedTextTexture &lineTexture : cache->lineTextures) { clear_cached_text_texture(&lineTexture); @@ -1137,6 +1150,14 @@ namespace { return render_text_line(renderer, font, text, color, x, y, rect.w, drawnHeight); } + /** + * @brief Fit cached artwork or a screenshot inside a square-pixel shell rectangle without cropping. + * + * @param renderer Active shell renderer. + * @param texture Loaded image texture. + * @param rect Available logical bounds before encoder correction. + * @return True when the fitted image was copied successfully. + */ bool render_texture_fit(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect &rect) { if (renderer == nullptr || texture == nullptr || rect.w <= 0 || rect.h <= 0) { return false; @@ -1148,14 +1169,7 @@ namespace { return false; } - SDL_Rect destination = rect; - if ((textureWidth * rect.h) > (textureHeight * rect.w)) { - destination.h = std::max(1, (textureHeight * rect.w) / textureWidth); - destination.y = rect.y + std::max(0, (rect.h - destination.h) / 2); - } else { - destination.w = std::max(1, (textureWidth * rect.h) / textureHeight); - destination.x = rect.x + std::max(0, (rect.w - destination.w) / 2); - } + const SDL_Rect destination = sdl_rect(ui::fit_screenshot({textureWidth, textureHeight}, {rect.x, rect.y, rect.w, rect.h})); return SDL_RenderCopy(renderer, texture, nullptr, &destination) == 0; } @@ -3182,6 +3196,11 @@ namespace { return; } + if (host->reachability == app::HostReachability::offline && host->pairingState == app::PairingState::paired && !host->apps.empty()) { + app::begin_selected_host_app_browse(state, update.requests.appsBrowseShowHidden); + return; + } + const std::string address = host->address; const uint16_t port = host->resolvedHttpPort == 0 ? app::effective_host_port(host->port) : host->resolvedHttpPort; network::PairingIdentity clientIdentity {}; @@ -4094,7 +4113,90 @@ namespace { } /** - * @brief Compose the shell or update retained folder rows without rebuilding unchanged page pixels. + * @brief Load a different capture once, bounding allocations before decoding its BMP pixels. + * @param renderer Renderer that owns the single image texture. + * @param path Current capture path, or empty for an empty library. + * @param cache Image and any load error retained until selection changes. + */ + void load_screenshot_texture(SDL_Renderer *renderer, const std::string &path, ScreenshotTextureCache &cache) { + if (cache.path == path) { + return; + } + destroy_texture(cache.texture); + cache = {}; + cache.path = path; + if (path.empty()) { + return; + } + std::uint64_t size = 0; + if (!platform::try_get_file_size(path, &size)) { + cache.error = "Screenshot unavailable. The file may have been moved or deleted."; + return; + } + FILE *file = std::fopen(path.c_str(), "rb"); + std::array header {}; + const bool read = file != nullptr && std::fread(header.data(), 1, header.size(), file) == header.size(); + if (file != nullptr) { + std::fclose(file); + } + const auto image = read ? ui::screenshot_bitmap_size({header.data(), header.size()}, size) : ui::ScreenshotImageSize {}; + if (image.width == 0) { + cache.error = "Cannot open this screenshot. The bitmap is damaged or unsupported."; + return; + } + SDL_Surface *surface = SDL_LoadBMP(path.c_str()); + if (surface != nullptr) { + cache.texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); + } + if (cache.texture == nullptr) { + cache.error = "Cannot load the screenshot. Close the viewer and try again."; + logging::warn("screenshots", "Failed to load " + path + ": " + SDL_GetError()); + } + } + + /** + * @brief Present an app capture with the shell's existing image fitting and input glyphs. + * @param renderer Active software renderer, also used by the Xbox framebuffer path. + * @param bodyFont Heading and error font. + * @param smallFont Caption and helper font. + * @param assets Shared main-menu SVG textures. + * @param view Current capture, metadata and input hints. + * @param bounds Television-safe logical content area. + * @param cache Single retained capture texture. + * @return True when the viewer frame was presented successfully. + */ + bool render_screenshot_viewer(SDL_Renderer *renderer, TTF_Font *bodyFont, TTF_Font *smallFont, AssetTextureCache *assets, const ui::ShellViewModel &view, const SDL_Rect &bounds, ScreenshotTextureCache &cache) { + load_screenshot_texture(renderer, view.screenshot.path, cache); + const SDL_Color text {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}; + const SDL_Color muted {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}; + const int heading = TTF_FontLineSkip(bodyFont) + 8; + const int caption = TTF_FontLineSkip(smallFont) + 6; + const int footer = measure_footer_actions_height(smallFont, view.modal.footerActions, bounds.w); + const SDL_Rect picture {bounds.x, bounds.y + heading + caption, bounds.w, std::max(1, bounds.h - heading - caption * 2 - footer - 12)}; + if (!render_text_line_simple(renderer, bodyFont, view.modal.title, text, bounds.x, bounds.y, bounds.w) || !render_text_line_simple(renderer, smallFont, view.screenshot.caption, muted, bounds.x, bounds.y + heading, bounds.w)) { + return false; + } + if (cache.texture != nullptr) { + if (!render_texture_fit(renderer, cache.texture, picture)) { + return false; + } + } else { + const std::string message = !cache.error.empty() ? cache.error : view.screenshot.status; + if (!render_bounded_text(renderer, bodyFont, message, text, picture)) { + return false; + } + } + const std::string details = !view.screenshot.status.empty() && !view.screenshot.path.empty() ? view.screenshot.status : view.screenshot.path; + if (!render_text_line_simple(renderer, smallFont, details, muted, bounds.x, picture.y + picture.h + 6, bounds.w) || !render_footer_actions(renderer, smallFont, assets, view.modal.footerActions, {bounds.x, bounds.y + bounds.h - footer, bounds.w, footer})) { + return false; + } + SDL_RenderPresent(renderer); + return true; + } + + /** + * @brief Compose the shell, show a capture, or update retained folder rows without rebuilding unchanged page pixels. * * @param renderer Active SDL renderer, including the software fallback. * @param videoMode Output resolution and pixel aspect information. @@ -4176,6 +4278,10 @@ namespace { return false; } + if (viewModel.screenshot.visible && !viewModel.overlay.visible && keypadModalLayoutCache != nullptr) { + return render_screenshot_viewer(renderer, bodyFont, smallFont, assetCache, viewModel, {outerMargin, outerMargin, screenWidth - outerMargin * 2, screenHeight - outerMargin * 2}, keypadModalLayoutCache->screenshot); + } + const int logoSize = std::max(26, headerRect.h - 12); const SDL_Rect logoRect {headerRect.x, headerRect.y + (headerRect.h - logoSize) / 2, logoSize, logoSize}; render_asset_icon(renderer, assetCache, "moonlight-logo.svg", logoRect); @@ -5115,6 +5221,14 @@ namespace { const app::ScreenId previousScreen = state.shell.activeScreen; app::AppUpdate update = app::handle_command(state, command); process_directory_picker_requests(state, update); + if (update.requests.screenshotsRequested) { + auto result = app::load_screenshots(state.screenshotViewer.source, app::screenshot_library_path()); + state.screenshotViewer.entries = std::move(result.entries); + state.screenshotViewer.error = std::move(result.error); + // The image viewer draws no app tiles, so their textures can be released + // before allocating a full stream-sized capture on a 64 MB Xbox. + clear_cover_art_texture_cache(&resources->coverArtTextureCache); + } logging::set_file_minimum_level(state.settings.loggingLevel); logging::set_debugger_console_minimum_level(state.settings.xemuConsoleLoggingLevel); log_app_update(state, update); @@ -5142,13 +5256,13 @@ namespace { if ((previousScreen != state.shell.activeScreen || update.navigation.screenChanged) && !draw_current_shell_frame(activeVideoMode, state, resources, runtime)) { return; } - if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && state.modal.id != app::ModalId::directory_picker) { + if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && state.modal.id != app::ModalId::directory_picker && state.modal.id != app::ModalId::screenshot_viewer) { clear_keypad_modal_layout_cache(&resources->keypadModalLayoutCache); } } /** - * @brief Process shell input, notification lifetimes, background tasks and redraws. + * @brief Process shell input and background tasks, retaining idle local dialogs and screenshot frames. * * @param window Shared SDL window reused for shell and streaming rendering. * @param videoMode Active output mode used by the shell renderer. @@ -5192,7 +5306,7 @@ namespace { finish_shell_background_tasks(state, resources, runtime); start_shell_background_tasks_if_needed(state, runtime, SDL_GetTicks()); - const bool retainedInputDialog = (state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) || (state.modal.id == app::ModalId::directory_picker && !state.directoryPicker.creating && !state.shell.overlayVisible); + const bool retainedInputDialog = (state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) || (state.modal.id == app::ModalId::directory_picker && !state.directoryPicker.creating && !state.shell.overlayVisible) || (state.modal.id == app::ModalId::screenshot_viewer && !state.shell.overlayVisible); if ((!retainedInputDialog || runtime->keypadRedrawRequested) && !draw_current_shell_frame(videoMode != nullptr ? *videoMode : VIDEO_MODE {}, state, resources, runtime)) { return false; } diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index 9fc967d..1612468 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -563,7 +563,7 @@ namespace { } /** - * @brief Build app actions with explicit hidden and favorite checkbox states. + * @brief Build app actions with hidden/favorite checkboxes and the local screenshot viewer entry. * * @param state Current app and modal selection. * @param viewModel Receives the modal action rows. @@ -575,6 +575,7 @@ namespace { {"toggle-hidden-app", "Hide app", true, state.modal.selectedActionIndex == 0U, appRecord->hidden, {}, true}, {"view-app-details", "View details", true, state.modal.selectedActionIndex == 1U, false}, {"toggle-favorite", "Set favorite", true, state.modal.selectedActionIndex == 2U, appRecord->favorite, {}, true}, + {"view-screenshots", "Screenshots", true, state.modal.selectedActionIndex == 3U, false, "icons\\camera.svg"}, }; } } @@ -715,6 +716,27 @@ namespace { case app::ModalId::directory_picker: fill_directory_picker_view(state, viewModel); return; + case app::ModalId::screenshot_viewer: + { + const auto &viewer = state.screenshotViewer; + viewModel->modal.title = viewer.source.appName + " / " + viewer.source.hostName; + viewModel->screenshot.visible = true; + viewModel->screenshot.status = viewer.error; + if (viewer.entries.empty()) { + viewModel->screenshot.status = viewer.error.empty() ? "No screenshots for this app yet. Take one from the stream controls." : viewer.error; + } else { + const auto selected = std::min(viewer.selectedIndex, viewer.entries.size() - 1); + const auto &entry = viewer.entries[selected]; + viewModel->screenshot.path = entry.path; + viewModel->screenshot.caption = std::to_string(selected + 1) + " / " + std::to_string(viewer.entries.size()) + " " + app::screenshot_time_label(entry.capturedAt); + viewModel->modal.footerActions = { + {"previous-screenshot", "Previous", "icons\\button-lb.svg", {}, false}, + {"next-screenshot", "Next", "icons\\button-rb.svg", {}, false}, + }; + } + viewModel->modal.footerActions.push_back({"back", "Back", "icons\\button-b.svg", {}, false}); + return; + } case app::ModalId::connection_test: viewModel->modal.busy = state.connectionTest.inProgress; viewModel->modal.title = state.connectionTest.inProgress ? "Testing connection" : (state.connectionTest.succeeded ? "Connection successful" : "Connection failed"); diff --git a/src/ui/shell_view.h b/src/ui/shell_view.h index 5295af1..7410e6c 100644 --- a/src/ui/shell_view.h +++ b/src/ui/shell_view.h @@ -201,6 +201,16 @@ namespace ui { std::vector footerActions; ///< Input, delete, cancel and accept controls inside the dialog. }; + /** + * @brief Current screenshot and captions for the local full-screen image viewer. + */ + struct ShellScreenshotView { + bool visible = false; ///< True while the app screenshot viewer owns the display. + std::string path; ///< Current bitmap path; empty when no captures are indexed. + std::string caption; ///< Capture time and position within this app's library. + std::string status; ///< Empty-library guidance or a recoverable journal warning. + }; + /** * @brief Render-ready shell state derived from the app model. */ @@ -212,6 +222,7 @@ namespace ui { ShellModalView modal; ///< Modal dialog content. ShellLogViewerView logViewer; ///< Embedded log viewer surfaced by the log modal. ShellKeypadModalView keypad; ///< Add-host keypad modal content. + ShellScreenshotView screenshot; ///< Single-image local viewer with app-specific navigation. }; /** diff --git a/tests/unit/app/screenshot_library_test.cpp b/tests/unit/app/screenshot_library_test.cpp new file mode 100644 index 0000000..9e28546 --- /dev/null +++ b/tests/unit/app/screenshot_library_test.cpp @@ -0,0 +1,244 @@ +/** + * @file tests/unit/app/screenshot_library_test.cpp + * @brief Verifies capture provenance, journal recovery, unique filenames and app-scoped navigation. + */ +// test includes +#include "tests/support/filesystem_test_utils.h" + +#include + +// standard includes +#include +#include +#include + +// local includes +#include "src/app/client_state.h" +#include "src/app/screenshot_library.h" +#include "src/startup/storage_paths.h" +#include "src/ui/shell_view.h" + +namespace { + /** + * @brief Own isolated capture and journal files for one native test. + */ + class ScreenshotLibraryTest: public BaseTest { // NOSONAR(cpp:S3656) gtest fixture lifetime + protected: + std::string root = "screenshot-library-test"; ///< Fixture-owned storage root. + std::string journal = root + "/index.tsv"; ///< Isolated screenshot index. + app::ScreenshotSource source {"server-one", "192.0.2.1", 47989, "Office PC", 7, "Game"}; ///< Default capture identity. + + /** + * @brief Remove only fixture-owned files after every test. + */ + void TearDown() override { + test_support::remove_tree_if_present(root); + BaseTest::TearDown(); + } + }; + + /** + * @brief UUID and app ID isolate libraries despite renamed labels, DHCP changes or identical app names. + */ + TEST_F(ScreenshotLibraryTest, MatchesStableServerAndAppIdentity) { + app::HostRecord host {"Office PC", "192.0.2.1"}; + host.uuid = "SERVER-ONE"; + auto requested = app::screenshot_source(host, {"Renamed Game", 7}); + EXPECT_EQ(requested.hostPort, app::DEFAULT_HOST_PORT); + requested.hostAddress = "192.0.2.2"; + EXPECT_TRUE(app::screenshot_source_matches(source, requested)); + requested.appId = 8; + EXPECT_FALSE(app::screenshot_source_matches(source, requested)); + requested.appId = 7; + requested.hostUuid = "server-two"; + requested.hostAddress = source.hostAddress; + EXPECT_FALSE(app::screenshot_source_matches(source, requested)); + requested.hostUuid.clear(); + EXPECT_TRUE(app::screenshot_source_matches(source, requested)); + requested.hostPort = 48000; + EXPECT_FALSE(app::screenshot_source_matches(source, requested)); + requested.hostPort = 0; + EXPECT_TRUE(app::screenshot_source_matches(source, requested)); + requested.hostAddress.clear(); + EXPECT_FALSE(app::screenshot_source_matches(source, requested)); + EXPECT_EQ(app::screenshot_library_path(), startup::default_storage_path("screenshots.tsv")); + } + + /** + * @brief Labels and paths round-trip without breaking the journal's line boundaries. + */ + TEST_F(ScreenshotLibraryTest, EscapesMetadataAndRejectsDamagedRecords) { + auto record = app::ScreenshotRecord {source, "F:\\My shots\\capture.bmp", 1700000000}; + record.source.hostName = "PC\tname\nwith\\escapes\r"; + record.source.appName = "Game / \"name\""; + const auto encoded = app::serialize_screenshot(record); + EXPECT_EQ(encoded.find('\n'), std::string::npos); + const auto decoded = app::parse_screenshot(encoded); + ASSERT_TRUE(decoded.has_value()); + EXPECT_EQ(decoded->source.hostName, record.source.hostName); + EXPECT_EQ(decoded->source.appName, record.source.appName); + EXPECT_EQ(decoded->path, record.path); + EXPECT_EQ(decoded->capturedAt, record.capturedAt); + for (const auto &invalid : {std::string {}, std::string("2") + encoded.substr(1), encoded + "\tunexpected", encoded.substr(0, encoded.size() - 2), encoded + "\\", std::string(8192, 'x'), encoded + std::string(1, '\0'), encoded + "\n", encoded + "\r"}) { + EXPECT_FALSE(app::parse_screenshot(invalid).has_value()); + } + auto badEscape = encoded; + badEscape.replace(badEscape.find("\\t"), 2, "\\z"); + EXPECT_FALSE(app::parse_screenshot(badEscape).has_value()); + auto badPort = encoded; + badPort.replace(badPort.find("47989"), 5, "65536"); + EXPECT_FALSE(app::parse_screenshot(badPort).has_value()); + badPort.replace(badPort.find("65536"), 5, "0"); + EXPECT_FALSE(app::parse_screenshot(badPort).has_value()); + auto invalid = record; + invalid.path.clear(); + EXPECT_TRUE(app::serialize_screenshot(invalid).empty()); + invalid = record; + invalid.source.appId = -1; + EXPECT_TRUE(app::serialize_screenshot(invalid).empty()); + invalid = record; + invalid.capturedAt = -1; + EXPECT_TRUE(app::serialize_screenshot(invalid).empty()); + invalid = record; + invalid.source.hostUuid.clear(); + invalid.source.hostAddress.clear(); + EXPECT_TRUE(app::serialize_screenshot(invalid).empty()); + invalid = record; + invalid.path += '\0'; + EXPECT_TRUE(app::serialize_screenshot(invalid).empty()); + invalid = record; + invalid.path.assign(8192, 'x'); + EXPECT_TRUE(app::serialize_screenshot(invalid).empty()); + EXPECT_FALSE(app::parse_screenshot("1\t999999999999999999999\t7\t47989\tuuid\tip\thost\tapp\tfile.bmp\tend")); + EXPECT_FALSE(app::parse_screenshot("1\tnan\t7\t47989\tuuid\tip\thost\tapp\tfile.bmp\tend")); + } + + /** + * @brief Captures from old destinations and missing files remain linked, filtered and newest first. + */ + TEST_F(ScreenshotLibraryTest, LoadsAcrossFoldersAndRecoversAfterInterruptedAppends) { + EXPECT_TRUE(app::load_screenshots(source, journal).entries.empty()); + EXPECT_TRUE(app::load_screenshots(source, journal).error.empty()); + std::string error; + EXPECT_TRUE(app::append_screenshot({source, root + "/old/capture.bmp", 10}, journal, error)) << error; + auto other = source; + other.appId = 8; + EXPECT_TRUE(app::append_screenshot({other, root + "/other.bmp", 99}, journal, error)); + { + std::ofstream damaged(journal, std::ios::app | std::ios::binary); + damaged << std::string(9000, 'x') << "\ninvalid\n" + << "1\t20\t7\t47989\tserver-one\tip\thost\tapp\tpartial.bmp"; + } + EXPECT_TRUE(app::append_screenshot({source, root + "/new/first.bmp", 20}, journal, error)); + EXPECT_TRUE(app::append_screenshot({source, root + "/new/second.bmp", 20}, journal, error)); + auto result = app::load_screenshots(source, journal); + ASSERT_EQ(result.entries.size(), 3U); + EXPECT_EQ(result.entries[0].path, root + "/new/second.bmp"); + EXPECT_EQ(result.entries[1].path, root + "/new/first.bmp"); + EXPECT_EQ(result.entries[2].path, root + "/old/capture.bmp"); + EXPECT_FALSE(result.error.empty()); + { + std::ofstream partial(journal, std::ios::app | std::ios::binary); + partial << app::serialize_screenshot({source, "not-finalized.bmp", 100}); + } + EXPECT_EQ(app::load_screenshots(source, journal).entries.size(), 3U); + EXPECT_FALSE(app::append_screenshot({}, journal, error)); + EXPECT_FALSE(error.empty()); + EXPECT_FALSE(app::append_screenshot({source, "file.bmp", 30}, journal + "/not-a-directory", error)); + EXPECT_FALSE(error.empty()); + EXPECT_FALSE(app::append_screenshot({source, "file.bmp", 30}, root, error)); + EXPECT_FALSE(error.empty()); + } + + /** + * @brief Capture names survive restarts and repeated captures in the same second without replacing files. + */ + TEST_F(ScreenshotLibraryTest, ChoosesUnusedFatxNames) { + std::string error; + const std::string first = app::next_screenshot_path(root + "/shots", 1700000000, error); + ASSERT_FALSE(first.empty()) << error; + { + std::ofstream image(first); + image << "original capture"; + } + const std::string second = app::next_screenshot_path(root + "/shots", 1700000000, error); + EXPECT_NE(first, second); + EXPECT_LT(std::filesystem::path(second).filename().string().size(), 43U); + EXPECT_EQ(std::filesystem::file_size(first), 16U); + EXPECT_TRUE(app::next_screenshot_path(first + "/blocked", 1, error).empty()); + EXPECT_FALSE(error.empty()); + EXPECT_TRUE(app::next_screenshot_path(root, -1, error).empty()); + EXPECT_FALSE(error.empty()); + EXPECT_EQ(app::screenshot_time_label(1700000000), "2023-11-14 22:13:20 UTC"); + EXPECT_EQ(app::screenshot_time_label(-1), "Capture time unavailable"); + } + + /** + * @brief App actions open an offline library, keep navigation inside it and restore the parent menu. + */ + TEST(ScreenshotViewerTest, OpensBrowsesAndReturnsToAppActions) { + auto state = app::create_initial_state(); + app::HostRecord host {"Offline PC", "192.0.2.1", 0, app::PairingState::paired, app::HostReachability::offline}; + host.uuid = "server-one"; + host.apps = {{"Game", 7}, {"Other", 8}}; + app::replace_hosts(state, {host}); + ASSERT_TRUE(app::begin_selected_host_app_browse(state, false)); + EXPECT_EQ(state.hosts.active.appListState, app::HostAppListState::ready); + EXPECT_NE(state.hosts.active.appListStatusMessage.find("PC offline"), std::string::npos); + app::handle_command(state, input::UiCommand::open_context_menu); + for (int i = 0; i < 3; ++i) { + app::handle_command(state, input::UiCommand::move_down); + } + auto view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.actions.size(), 4U); + EXPECT_EQ(view.modal.actions[3].id, "view-screenshots"); + EXPECT_TRUE(view.modal.actions[3].selected); + const auto opened = app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(opened.requests.screenshotsRequested); + EXPECT_FALSE(opened.requests.appsBrowseRequested); + EXPECT_EQ(state.modal.id, app::ModalId::screenshot_viewer); + EXPECT_EQ(state.screenshotViewer.source.hostUuid, "server-one"); + EXPECT_EQ(state.screenshotViewer.source.appId, 7); + view = ui::build_shell_view_model(state, {}); + EXPECT_TRUE(view.screenshot.visible); + EXPECT_TRUE(view.screenshot.path.empty()); + EXPECT_NE(view.screenshot.status.find("No screenshots"), std::string::npos); + app::handle_command(state, input::UiCommand::next_page); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); + state.screenshotViewer.entries = {{{}, "first.bmp", 20}, {{}, "second.bmp", 10}}; + app::handle_command(state, input::UiCommand::move_right); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 1U); + view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.screenshot.path, "second.bmp"); + EXPECT_EQ(view.screenshot.caption, "2 / 2 1970-01-01 00:00:10 UTC"); + app::handle_command(state, input::UiCommand::next_page); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); + app::handle_command(state, input::UiCommand::move_left); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 1U); + app::handle_command(state, input::UiCommand::previous_page); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); + app::handle_command(state, input::UiCommand::activate); + app::handle_command(state, input::UiCommand::move_down); + EXPECT_EQ(state.apps.selectedAppIndex, 0U); + EXPECT_EQ(state.modal.id, app::ModalId::screenshot_viewer); + state.screenshotViewer.selectedIndex = 999; + view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.screenshot.path, "second.bmp"); + app::handle_command(state, input::UiCommand::previous_page); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); + state.shell.inputDevice = input::UiInputDevice::keyboard; + view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.footerActions.size(), 3U); + EXPECT_EQ(view.modal.footerActions[0].keyLabel, "PgUp"); + EXPECT_EQ(view.modal.footerActions[1].keyLabel, "PgDn"); + EXPECT_EQ(view.modal.footerActions[2].keyLabel, "Esc"); + app::handle_command(state, input::UiCommand::back); + EXPECT_EQ(state.modal.id, app::ModalId::app_actions); + EXPECT_EQ(state.modal.selectedActionIndex, 3U); + EXPECT_TRUE(state.screenshotViewer.entries.empty()); + app::handle_command(state, input::UiCommand::back); + EXPECT_EQ(state.modal.id, app::ModalId::none); + app::handle_command(state, input::UiCommand::move_right); + EXPECT_EQ(state.apps.selectedAppIndex, 1U); + } +} // namespace diff --git a/tests/unit/ui/screenshot_image_test.cpp b/tests/unit/ui/screenshot_image_test.cpp new file mode 100644 index 0000000..71fad26 --- /dev/null +++ b/tests/unit/ui/screenshot_image_test.cpp @@ -0,0 +1,106 @@ +/** + * @file tests/unit/ui/screenshot_image_test.cpp + * @brief Verifies bounded capture decoding and aspect-preserving image placement. + */ +// test includes +#include + +// standard includes +#include +#include +#include + +// local includes +#include "src/ui/screenshot_image.h" + +namespace { + /** + * @brief Write a little-endian BMP header field for decoder validation cases. + * @param header Mutable test header. + * @param offset Field position. + * @param value Field value. + * @param bytes Field size in bytes. + */ + void header_field(std::string &header, std::size_t offset, std::uint32_t value, std::size_t bytes = 4) { + for (std::size_t i = 0; i < bytes; ++i) { + header[offset + i] = static_cast(value >> (i * 8U)); + } + } + + /** + * @brief Construct the uncompressed 720p BMP header emitted by capture saving. + * @return Header with 24-bit pixels, a single plane and ordinary RGB encoding. + */ + std::string bitmap_header() { + std::string header(54, '\0'); + header[0] = 'B'; + header[1] = 'M'; + header_field(header, 10, 54); + header_field(header, 14, 40); + header_field(header, 18, 1280); + header_field(header, 22, 720); + header_field(header, 26, 1, 2); + header_field(header, 28, 24, 2); + return header; + } + + /** + * @brief Valid captures load while malformed dimensions and truncated pixels are rejected before allocation. + */ + TEST(ScreenshotImageTest, ValidatesBitmapDimensionsAndPixelStorage) { + const auto original = bitmap_header(); + constexpr std::uint64_t size = 54 + 1280 * 720 * 3; + auto dimensions = ui::screenshot_bitmap_size(original, size); + EXPECT_EQ(dimensions.width, 1280); + EXPECT_EQ(dimensions.height, 720); + EXPECT_EQ(ui::screenshot_bitmap_size(original.substr(0, 53), size).width, 0); + EXPECT_EQ(ui::screenshot_bitmap_size("XX" + original.substr(2), size).width, 0); + EXPECT_EQ(ui::screenshot_bitmap_size(original, size - 1).width, 0); + EXPECT_EQ(ui::screenshot_bitmap_size(original, 10 * 1024 * 1024).width, 0); + for (const auto &[offset, value] : {std::pair {10U, 53U}, {14U, 39U}, {14U, 125U}, {18U, 0U}, {18U, 1921U}, {22U, 0U}, {22U, 1081U}, {22U, 0xffffffffU}, {26U, 2U}, {28U, 16U}, {30U, 1U}}) { + auto broken = original; + header_field(broken, offset, value, offset == 26 || offset == 28 ? 2 : 4); + EXPECT_EQ(ui::screenshot_bitmap_size(broken, size).width, 0) << offset << ": " << value; + } + auto rgba = original; + header_field(rgba, 28, 32, 2); + EXPECT_EQ(ui::screenshot_bitmap_size(rgba, 54 + 1280 * 720 * 4).width, 1280); + auto padded = original; + header_field(padded, 18, 3); + header_field(padded, 22, 2); + EXPECT_EQ(ui::screenshot_bitmap_size(padded, 54 + 12 * 2).height, 2); + EXPECT_EQ(ui::screenshot_bitmap_size(padded, 54 + 9 * 2).width, 0); + } + + /** + * @brief Landscape, portrait and extreme aspect ratios stay centered inside SD and HD viewer bounds. + */ + TEST(ScreenshotImageTest, FitsWithoutCroppingOrDistortingAspectRatio) { + auto rect = ui::fit_screenshot({1280, 720}, {10, 20, 620, 400}); + EXPECT_EQ(rect.x, 10); + EXPECT_EQ(rect.w, 620); + EXPECT_EQ(rect.h, 348); + EXPECT_EQ(rect.y, 46); + rect = ui::fit_screenshot({480, 640}, {10, 20, 620, 400}); + EXPECT_EQ(rect.x, 170); + EXPECT_EQ(rect.w, 300); + EXPECT_EQ(rect.y, 20); + EXPECT_EQ(rect.h, 400); + rect = ui::fit_screenshot({1280, 720}, {0, 0, 1280, 720}); + EXPECT_EQ(rect.x, 0); + EXPECT_EQ(rect.y, 0); + EXPECT_EQ(rect.w, 1280); + EXPECT_EQ(rect.h, 720); + EXPECT_EQ(ui::fit_screenshot({0, 1}, {0, 0, 20, 20}).w, 0); + EXPECT_EQ(ui::fit_screenshot({1, 0}, {0, 0, 20, 20}).w, 0); + EXPECT_EQ(ui::fit_screenshot({1, 1}, {0, 0, 0, 20}).w, 0); + EXPECT_EQ(ui::fit_screenshot({1, 1}, {0, 0, 20, 0}).w, 0); + const int largest = std::numeric_limits::max(); + rect = ui::fit_screenshot({largest, 1}, {0, 0, 1280, 720}); + EXPECT_EQ(rect.w, 1280); + EXPECT_EQ(rect.h, 1); + rect = ui::fit_screenshot({1, largest}, {0, 0, 1280, 720}); + EXPECT_EQ(rect.w, 1); + EXPECT_EQ(rect.h, 720); + } +} // namespace diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index a1a0cdc..483e283 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -512,7 +512,7 @@ namespace { state.hosts.active.apps = {{"Desktop", 1}}; state.modal.id = app::ModalId::app_actions; auto view = ui::build_shell_view_model(state, {}); - ASSERT_EQ(view.modal.actions.size(), 3U); + ASSERT_EQ(view.modal.actions.size(), 4U); EXPECT_TRUE(view.modal.actions[0].checkable); EXPECT_FALSE(view.modal.actions[0].checked); EXPECT_FALSE(view.modal.actions[1].checkable); @@ -524,7 +524,7 @@ namespace { EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).persistence.hostsChanged); app::handle_command(state, input::UiCommand::open_context_menu); view = ui::build_shell_view_model(state, {}); - ASSERT_EQ(view.modal.actions.size(), 3U); + ASSERT_EQ(view.modal.actions.size(), 4U); EXPECT_TRUE(view.modal.actions[2].checked); EXPECT_EQ(view.modal.actions[2].label, "Set favorite"); } From 16c084bd5f348d8918eb856f097fa77a42c66f24 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:05:27 -0500 Subject: [PATCH 08/22] feat(streaming): make menus responsive and add host app exit controls --- README.md | 13 +- cmake/stage-lucide.cmake | 2 +- src/app/client_state.cpp | 98 ++++++++- src/app/client_state.h | 36 +++- src/app/screenshot_library.cpp | 47 ++++- src/app/screenshot_library.h | 9 + src/input/stream_menu.cpp | 10 +- src/input/stream_menu.h | 16 +- src/network/host_pairing.cpp | 39 ++++ src/network/host_pairing.h | 11 + src/streaming/ffmpeg_stream_backend.cpp | 93 +++++++-- src/streaming/ffmpeg_stream_backend.h | 38 +++- src/streaming/session.cpp | 8 + src/streaming/stream_controls.cpp | 94 +++++---- src/streaming/stream_controls.h | 11 + src/ui/shell_screen.cpp | 127 ++++++++++-- src/ui/shell_view.cpp | 41 +++- src/ui/shell_view.h | 14 +- tests/unit/app/menu_actions_test.cpp | 222 +++++++++++++++++++++ tests/unit/app/screenshot_library_test.cpp | 75 ++++++- tests/unit/input/stream_menu_test.cpp | 15 +- tests/unit/network/host_quit_test.cpp | 161 +++++++++++++++ tests/unit/ui/shell_view_test.cpp | 12 +- 23 files changed, 1074 insertions(+), 118 deletions(-) create mode 100644 tests/unit/app/menu_actions_test.cpp create mode 100644 tests/unit/network/host_quit_test.cpp diff --git a/README.md b/README.md index edad0bd..885b053 100644 --- a/README.md +++ b/README.md @@ -125,9 +125,11 @@ In the numeric address/port keypad, **Space** types the focused key and **Enter* **PgUp / PgDn** change the address section. Stream logs appear in a live side panel with severity colors and a scrollbar. Scroll upward to read -older entries; return to the bottom to follow new entries. The disconnect dialog uses the same modal -surface as the main menus, accepts **A / Enter** to disconnect and **B / Esc** to cancel, and leaves -the host app running after disconnecting. +older entries; return to the bottom to follow new entries. **Exit** opens a three-button dialog: +**B / Esc** cancels, **A / Enter** disconnects while leaving the app running on the PC, and **X / Q** +quits the app on the PC. Quitting can lose unsaved progress. An in-progress app has a pause icon on +its poster and can be selected to resume; its app action menu also offers **Quit app**, with confirmation. +The pause marker describes a resumable streaming session; the PC app continues running after disconnecting. Returning from the stream resets local keyboard/controller holds before library navigation resumes. Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Open @@ -144,6 +146,7 @@ The same section stores `keyboard_dock_top = false` (bottom) or `true` (top). Open a game or app's action menu and choose **Screenshots** to view its captures. The viewer works offline and shows the newest capture first; use **LB / RB** or **PgUp / PgDn** to browse, and **B / Esc** to return to the app's actions. Left and right navigation also changes images. +Use **X / Backspace / Delete** in the viewer to delete the selected image after confirmation. New screenshots are linked to the server UUID and app ID in the title-scoped `screenshots.tsv` index; servers without a UUID use their saved address and port. Renaming a server or app does not change its library. Changing the screenshot folder preserves access to previously indexed locations. Keep the @@ -151,6 +154,10 @@ index with your save data, and keep the bitmap files in their original locations files display an unavailable message. Captures saved before this index was introduced remain on disk and are not automatically assigned to an app. +Taking a screenshot snapshots the currently displayed host frame and saves it in the background. +Only one capture saves at a time; completion and errors are written to the log. The wheels close +automatically after screenshot, Guide, Share, statistics and mouse-mode actions. + Mouse mode uses the invoking controller's **left stick** for movement and **A / B** for left / right clicks. Stick-driven mouse movement is reported at up to 125 Hz, while button presses and releases are sent immediately. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; diff --git a/cmake/stage-lucide.cmake b/cmake/stage-lucide.cmake index 6a3c6ff..dd6553a 100644 --- a/cmake/stage-lucide.cmake +++ b/cmake/stage-lucide.cmake @@ -7,7 +7,7 @@ file(MAKE_DIRECTORY "${LUCIDE_ASSET_DIR}/icons" "${LUCIDE_ASSET_DIR}/licenses") foreach(icon IN ITEMS settings circle-question-mark circle-plus monitor gamepad-2 scroll-text rotate-ccw network hash activity key-round save loader-circle circle-check circle-x info - keyboard mouse power panels-top-left share-2 camera chart-no-axes-combined) + keyboard mouse power log-out pause panels-top-left share-2 camera chart-no-axes-combined) set(_icon_source "${LUCIDE_SOURCE_DIR}/icons/${icon}.svg") if(NOT EXISTS "${_icon_source}") message(FATAL_ERROR "Required Lucide icon not found: ${_icon_source}") diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 5f580c3..3e5dcd0 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -767,7 +767,7 @@ namespace { case app::ModalId::host_actions: return 4U; case app::ModalId::app_actions: - return 4U; + return app::selected_app(state) != nullptr && app::selected_app(state)->running ? 5U : 4U; case app::ModalId::confirmation: return 2U; case app::ModalId::directory_picker: @@ -779,6 +779,7 @@ namespace { case app::ModalId::log_viewer: case app::ModalId::connection_test: case app::ModalId::screenshot_viewer: + case app::ModalId::quit_progress: return 0U; } return 0U; @@ -790,7 +791,7 @@ namespace { return false; } - const std::size_t current = state.modal.selectedActionIndex % count; + const std::size_t current = state.modal.id == app::ModalId::app_actions ? std::min(state.modal.selectedActionIndex, count - 1) : state.modal.selectedActionIndex % count; state.modal.selectedActionIndex = direction < 0 ? (current + count - 1U) % count : (current + 1U) % count; return state.modal.selectedActionIndex != current; } @@ -1411,7 +1412,12 @@ namespace { const bool confirmed = state.modal.selectedActionIndex % 2U == 0U; const app::ConfirmationAction action = state.confirmation.action; const std::string targetPath = state.confirmation.targetPath; + const auto parent = state.confirmation.returnModal; + const auto parentIndex = state.confirmation.returnActionIndex; close_modal_and_mark_closed(state, update); + if (parent != app::ModalId::none) { + open_modal(state, parent, parentIndex); + } if (!confirmed) { state.shell.statusMessage = "Cancelled the pending reset action"; return true; @@ -1427,6 +1433,14 @@ namespace { if (action == app::ConfirmationAction::factory_reset) { update->persistence.factoryResetRequested = true; } + if (action == app::ConfirmationAction::delete_screenshot) { + update->requests.screenshotDeleteRequested = true; + } + if (action == app::ConfirmationAction::quit_app) { + state.appQuit.inProgress = true; + open_modal(state, app::ModalId::quit_progress); + update->requests.quitAppRequested = true; + } return true; } @@ -1444,7 +1458,7 @@ namespace { return true; } - switch (state.modal.selectedActionIndex % 4U) { + switch (state.modal.selectedActionIndex % modal_action_count(state)) { case 0: close_modal_and_mark_closed(state, update); if (host->pairingState == app::PairingState::paired) { @@ -1497,7 +1511,7 @@ namespace { } app::HostAppRecord &appRecord = mutableHost->apps[indices[state.apps.selectedAppIndex]]; - switch (state.modal.selectedActionIndex % 4U) { + switch (state.modal.selectedActionIndex) { case 0: appRecord.hidden = !appRecord.hidden; state.hosts.dirty = true; @@ -1526,6 +1540,16 @@ namespace { update->requests.screenshotsRequested = true; } return true; + case 4: + if (appRecord.running) { + state.appQuit = {host->address, host->port, appRecord.id, appRecord.name, false}; + open_confirmation(state, app::ConfirmationAction::quit_app, "Quit " + appRecord.name + "?", {"The app will close on your PC. Unsaved progress may be lost."}); + state.confirmation.returnModal = app::ModalId::app_actions; + state.confirmation.returnActionIndex = 4; + } else { + state.modal.selectedActionIndex = 3; + } + return true; default: return true; } @@ -1549,6 +1573,7 @@ namespace { case app::ModalId::log_viewer: case app::ModalId::directory_picker: case app::ModalId::screenshot_viewer: + case app::ModalId::quit_progress: return true; case app::ModalId::confirmation: return handle_confirmation_modal_activation(state, update); @@ -1684,12 +1709,27 @@ namespace { if (state.modal.id == app::ModalId::connection_test && state.connectionTest.inProgress) { return true; } + if (state.modal.id == app::ModalId::quit_progress) { + return true; + } + if (state.modal.id == app::ModalId::confirmation && command == input::UiCommand::back && state.confirmation.returnModal != app::ModalId::none) { + const auto parent = state.confirmation.returnModal; + const auto index = state.confirmation.returnActionIndex; + close_modal_and_mark_closed(state, update); + open_modal(state, parent, index); + return true; + } if (state.modal.id == app::ModalId::screenshot_viewer) { auto &viewer = state.screenshotViewer; if (command == input::UiCommand::back) { viewer = {}; open_modal(state, app::ModalId::app_actions, 3U); + } else if (command == input::UiCommand::delete_character && !viewer.entries.empty()) { + viewer.selectedIndex = std::min(viewer.selectedIndex, viewer.entries.size() - 1); + const auto &entry = viewer.entries[viewer.selectedIndex]; + open_confirmation(state, app::ConfirmationAction::delete_screenshot, "Delete screenshot?", {"This permanently deletes the saved image.", app::screenshot_time_label(entry.capturedAt)}, entry.path); + state.confirmation.returnModal = app::ModalId::screenshot_viewer; } else if (!viewer.entries.empty()) { const auto count = viewer.entries.size(); viewer.selectedIndex = std::min(viewer.selectedIndex, count - 1); @@ -2835,6 +2875,56 @@ namespace app { rebuild_menu(state, "screenshot-directory"); } + void apply_screenshot_delete_result(ClientState &state, bool succeeded, std::string error) { + auto &viewer = state.screenshotViewer; + if (state.modal.id != ModalId::screenshot_viewer || viewer.entries.empty()) { + return; + } + viewer.selectedIndex = std::min(viewer.selectedIndex, viewer.entries.size() - 1); + if (succeeded) { + viewer.entries.erase(viewer.entries.begin() + viewer.selectedIndex); + viewer.selectedIndex = viewer.entries.empty() ? 0 : std::min(viewer.selectedIndex, viewer.entries.size() - 1); + viewer.error.clear(); + } else { + viewer.error = std::move(error); + } + } + + void apply_app_quit_result(ClientState &state, bool succeeded, std::string message) { + const auto target = state.appQuit; + if (!target.inProgress) { + return; + } + if (succeeded) { + const auto clearRunning = [&](HostRecord &host) { + if (!host_matches_endpoint(host, target.address, target.port)) { + return; + } + if (host.runningGameId == static_cast(target.appId)) { + host.runningGameId = 0; + } + for (auto &application : host.apps) { + if (application.id == target.appId) { + application.running = false; + } + } + }; + for (auto &host : state.hosts.items) { + clearRunning(host); + } + if (state.hosts.activeLoaded) { + clearRunning(state.hosts.active); + state.hosts.active.lastAppListRefreshTick = 0; + } + state.hosts.dirty = true; + } + if (state.modal.id == ModalId::quit_progress) { + close_modal(state); + } + show_notification(state, succeeded ? NotificationKind::success : NotificationKind::error, succeeded ? "App closed" : "Couldn't quit app", message.empty() ? target.appName : std::move(message), succeeded ? 5000 : 0); + state.appQuit = {}; + } + AppUpdate handle_command(ClientState &state, input::UiCommand command) { AppUpdate update {}; diff --git a/src/app/client_state.h b/src/app/client_state.h index 88ad524..ada4499 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -63,6 +63,7 @@ namespace app { connection_test, ///< Progress and result of a manual connection test. directory_picker, ///< Screenshot destination browser and local folder-name keyboard. screenshot_viewer, ///< Local captures associated with the selected server and app. + quit_progress, ///< Authenticated host-app quit operation running in the background. }; /** @@ -100,6 +101,8 @@ namespace app { none, ///< No destructive action is pending confirmation. delete_saved_file, ///< Delete one saved file or directory. factory_reset, ///< Remove all persisted Moonlight state. + delete_screenshot, ///< Remove the selected screenshot and its journal entry. + quit_app, ///< Terminate the selected in-progress app on its host. }; /** @@ -172,6 +175,8 @@ namespace app { std::string targetPath; ///< File or directory path targeted by the action, when applicable. std::string title; ///< Modal title presented to the user. std::vector lines; ///< Body lines describing the consequence of the action. + ModalId returnModal = ModalId::none; ///< Dialog restored after confirmation or cancellation. + std::size_t returnActionIndex = 0; ///< Focus to restore in the parent dialog. }; /** @@ -403,7 +408,18 @@ namespace app { }; /** - * @brief Complete local application state, including transient screenshot browsing. + * @brief Retain the user-confirmed host and app until its quit operation completes. + */ + struct AppQuitState { + std::string address; ///< Saved server endpoint captured before confirmation. + uint16_t port = 0; ///< Saved HTTP port override. + int appId = 0; ///< Expected running app; prevents quitting a different session. + std::string appName; ///< App label for progress and completion feedback. + bool inProgress = false; ///< True while the background quit request owns the progress dialog. + }; + + /** + * @brief Complete local application state, including transient dialogs and screenshot browsing. */ struct ClientState { ShellState shell; ///< Shell-wide status and overlay state. @@ -418,6 +434,7 @@ namespace app { SettingsState settings; ///< Settings, log viewer, and saved-file workflow state. DirectoryPickerState directoryPicker; ///< Pending screenshot-directory workflow. ScreenshotViewerState screenshotViewer; ///< Local app screenshot library, released when browsing ends. + AppQuitState appQuit; ///< Retained target for a confirmed host-app quit operation. ConfirmationDialogState confirmation; ///< Confirmation dialog content for destructive actions. }; @@ -454,6 +471,8 @@ namespace app { bool directoryCreateRequested = false; ///< Create the named child of the current picker directory. bool directorySelectRequested = false; ///< Check destination writability before accepting the picker directory. bool screenshotsRequested = false; ///< Load captures for the viewer's retained server and app identity. + bool screenshotDeleteRequested = false; ///< Delete the selected capture after confirmation. + bool quitAppRequested = false; ///< Start the retained host-app quit request in a background task. }; /** @@ -704,6 +723,21 @@ namespace app { */ AppUpdate handle_command(ClientState &state, input::UiCommand command); + /** + * @brief Apply a completed quit operation and clear only the confirmed app's in-progress state. + * @param state Client state containing the retained quit target. + * @param succeeded True after the host confirmed that no app is running. + * @param message Completion or actionable failure detail. + */ + void apply_app_quit_result(ClientState &state, bool succeeded, std::string message); + /** + * @brief Remove a deleted capture from the open viewer and keep its selection in range. + * @param state Viewer that requested deletion. + * @param succeeded True when the bitmap and index deletion both succeeded. + * @param error Failure detail to keep visible without hiding the selected image. + */ + void apply_screenshot_delete_result(ClientState &state, bool succeeded, std::string error); + /** * @brief Insert printable text into the local folder-name draft, without forwarding it to a host. * @param state Current client state. diff --git a/src/app/screenshot_library.cpp b/src/app/screenshot_library.cpp index b90148f..19fa48b 100644 --- a/src/app/screenshot_library.cpp +++ b/src/app/screenshot_library.cpp @@ -88,7 +88,7 @@ namespace app { if (record.path.empty() || record.capturedAt < 0 || record.source.appId <= 0 || (record.source.hostUuid.empty() && record.source.hostAddress.empty())) { return {}; } - std::string result = "1\t" + std::to_string(record.capturedAt) + "\t" + std::to_string(record.source.appId) + "\t" + std::to_string(effective_host_port(record.source.hostPort)); + std::string result = std::string(record.deleted ? "D\t" : "1\t") + std::to_string(record.capturedAt) + "\t" + std::to_string(record.source.appId) + "\t" + std::to_string(effective_host_port(record.source.hostPort)); for (const auto *field : {&record.source.hostUuid, &record.source.hostAddress, &record.source.hostName, &record.source.appName, &record.path}) { if (field->find('\0') != std::string::npos) { return {}; @@ -134,10 +134,11 @@ namespace app { fields.back() += c; } } - if (escaped || fields.size() != 10 || fields[0] != "1" || fields[9] != "end") { + if (escaped || fields.size() != 10 || (fields[0] != "1" && fields[0] != "D") || fields[9] != "end") { return std::nullopt; } ScreenshotRecord record; + record.deleted = fields[0] == "D"; unsigned int port = 0; std::istringstream numbers(fields[1] + " " + fields[2] + " " + fields[3]); if (!(numbers >> record.capturedAt >> record.source.appId >> port) || !(numbers >> std::ws).eof() || port == 0 || port > 65535) { @@ -210,7 +211,15 @@ namespace app { } if (auto record = parse_screenshot(line); record.has_value()) { if (screenshot_source_matches(record->source, source)) { - result.entries.push_back(std::move(*record)); + if (record->deleted) { + auto &entries = result.entries; + entries.erase(std::remove_if(entries.begin(), entries.end(), [&](const ScreenshotRecord &entry) { + return entry.path == record->path; + }), + entries.end()); + } else { + result.entries.push_back(std::move(*record)); + } } } else { result.error = "Some incomplete or damaged screenshot records were skipped."; @@ -228,6 +237,38 @@ namespace app { return result; } + bool delete_screenshot(const ScreenshotRecord &record, const std::string &journalPath, std::string &error) { + auto tombstone = record; + tombstone.deleted = true; + const std::string line = serialize_screenshot(tombstone); + error.clear(); + if (line.empty()) { + error = "Invalid screenshot metadata."; + return false; + } + // Open the index before deleting the image, so a read-only index cannot turn + // an ordinary delete into a missing image that still appears in the library. + FILE *file = std::fopen(journalPath.c_str(), "ab"); + if (file == nullptr) { + error = "Cannot open the screenshot index for deletion."; + return false; + } + platform::try_get_file_size(record.path); // Mount a saved Xbox drive on cold boot. + if (std::remove(record.path.c_str()) != 0 && errno != ENOENT) { + error = "Cannot delete screenshot: " + std::string(std::strerror(errno)); + std::fclose(file); + return false; + } + const std::string append = "\n" + line + "\n"; + const bool written = std::fwrite(append.data(), 1, append.size(), file) == append.size(); + const bool closed = std::fclose(file) == 0; + if (!written || !closed) { + error = "Screenshot deleted, but its index could not be updated."; + return false; + } + return true; + } + std::string next_screenshot_path(std::string_view directory, std::int64_t capturedAt, std::string &error) { error.clear(); const std::string folder = startup::screenshot_storage_path(directory, {}); diff --git a/src/app/screenshot_library.h b/src/app/screenshot_library.h index b8a01cb..e7a9e6a 100644 --- a/src/app/screenshot_library.h +++ b/src/app/screenshot_library.h @@ -34,6 +34,7 @@ namespace app { ScreenshotSource source; ///< Host and app responsible for this capture. std::string path; ///< Original bitmap location, retained when the capture folder changes. std::int64_t capturedAt = 0; ///< Capture time in UTC seconds since the Unix epoch. + bool deleted = false; ///< Journal tombstone; never returned as a visible capture. }; /** @@ -83,6 +84,14 @@ namespace app { * @return True when the record was finalized successfully. */ bool append_screenshot(const ScreenshotRecord &record, const std::string &journalPath, std::string &error); + /** + * @brief Delete a selected bitmap and append its removal to the journal without rewriting other captures. + * @param record Indexed capture selected by the user. + * @param journalPath Persistent screenshot journal. + * @param error Receives deletion or index failures; an already absent bitmap can still be removed from the index. + * @return True when both the bitmap and its library entry have been removed. + */ + bool delete_screenshot(const ScreenshotRecord &record, const std::string &journalPath, std::string &error); /** * @brief Read locally indexed captures for an app without contacting the host. * @param source Server and app to match. diff --git a/src/input/stream_menu.cpp b/src/input/stream_menu.cpp index 78bca1e..982f37f 100644 --- a/src/input/stream_menu.cpp +++ b/src/input/stream_menu.cpp @@ -154,6 +154,7 @@ namespace input { }}; void StreamMenu::open(int device) { + disconnectAction = StreamDisconnectAction::cancel; owner = device; page = StreamMenuPage::radial; selected = StreamAction::none; @@ -289,14 +290,15 @@ namespace input { return modifiers | key.modifiers | (upper ? MODIFIER_SHIFT : 0); } - bool StreamMenu::confirm_disconnect(bool accept) { + bool StreamMenu::confirm_disconnect(StreamDisconnectAction action) { if (page != StreamMenuPage::quit_confirmation) { return false; } - if (!accept) { + disconnectAction = action; + if (action == StreamDisconnectAction::cancel) { page = StreamMenuPage::radial; } - return accept; + return action != StreamDisconnectAction::cancel; } void StreamMenu::toggle_shift() { @@ -451,7 +453,7 @@ namespace input { case StreamAction::logs: return "View logs"; case StreamAction::quit: - return "Quit stream"; + return "Exit"; case StreamAction::guide: return "Guide"; case StreamAction::share: diff --git a/src/input/stream_menu.h b/src/input/stream_menu.h index 0bb94b3..e910838 100644 --- a/src/input/stream_menu.h +++ b/src/input/stream_menu.h @@ -33,6 +33,15 @@ namespace input { quit_confirmation, ///< Explicit confirmation before disconnecting. }; + /** + * @brief Explicit outcome of the three-button stream exit dialog. + */ + enum class StreamDisconnectAction { + cancel, ///< Resume the current stream. + disconnect, ///< Disconnect and leave the app running on the PC. + quit_app, ///< Disconnect and ask the PC to terminate the running app. + }; + /** * @brief A keyboard cell with an unshifted and shifted label. */ @@ -127,10 +136,11 @@ namespace input { void key_sent(); /** * @brief Complete the disconnect dialog directly, without directional selection. - * @param accept True for A/Enter to disconnect; false for B/Escape to cancel. - * @return True only for an accepted disconnect on the confirmation page. + * @param action Cancel, disconnect or quit the host app. + * @return True for either confirmed exit action on the confirmation page; false for cancellation. */ - bool confirm_disconnect(bool accept); + bool confirm_disconnect(StreamDisconnectAction action); + StreamDisconnectAction disconnectAction = StreamDisconnectAction::cancel; ///< Confirmed exit intent, retained until session cleanup. StreamMenuPage page = StreamMenuPage::closed; ///< Current screen. StreamAction selected = StreamAction::none; ///< Highlighted wheel action. int owner = -1; ///< Controller that opened local controls. diff --git a/src/network/host_pairing.cpp b/src/network/host_pairing.cpp index 9bbc68d..367f18e 100644 --- a/src/network/host_pairing.cpp +++ b/src/network/host_pairing.cpp @@ -2712,6 +2712,45 @@ namespace network { return true; } + bool quit_host_app(const std::string &address, uint16_t preferredHttpPort, const PairingIdentity &clientIdentity, int expectedAppId, std::string *errorMessage) { + if (address.empty() || expectedAppId <= 0) { + return append_error(errorMessage, "Quitting an app requires a valid host address and app ID"); + } + if (!is_valid_pairing_identity(clientIdentity)) { + return append_error(errorMessage, std::string(UNPAIRED_CLIENT_ERROR_MESSAGE)); + } + HostPairingServerInfo server {}; + if (!query_server_info(address, preferredHttpPort, &clientIdentity, &server, errorMessage)) { + return false; + } + if (!server.paired || server.httpsPort == 0) { + return append_error(errorMessage, "The PC must be paired and provide HTTPS to quit an app"); + } + if (server.runningGameId == 0) { + return true; + } + if (server.runningGameId != static_cast(expectedAppId)) { + return append_error(errorMessage, "The running app changed. Refresh the library before quitting it"); + } + const std::string requestAddress = resolve_reachable_address(address, server); + const std::string path = "/cancel?uniqueid=" + std::string(resolve_client_unique_id(&clientIdentity)) + "&uuid=" + std::string(DEFAULT_SERVERINFO_UUID); + HttpResponse response {}; + if (!http_get(requestAddress, server.httpsPort, path, true, &clientIdentity, {}, &response, errorMessage)) { + return false; + } + uint32_t status = 0; + std::string message; + if (response.statusCode != 200 || !extract_root_status(response.body, &status, &message) || status != 200) { + return append_error(errorMessage, message.empty() ? "The host rejected the quit request. Another client may own this session" : message); + } + // Like the other Moonlight clients, verify /serverinfo: some hosts return + // success from /cancel even when a different client owns the running app. + if (!query_server_info(address, preferredHttpPort, &clientIdentity, &server, errorMessage)) { + return false; + } + return server.runningGameId == 0 || append_error(errorMessage, "The app is still running. Only the client that started it may be able to quit it"); + } + bool launch_or_resume_stream( const std::string &address, uint16_t preferredHttpPort, diff --git a/src/network/host_pairing.h b/src/network/host_pairing.h index cb9f392..fc8c7ac 100644 --- a/src/network/host_pairing.h +++ b/src/network/host_pairing.h @@ -256,6 +256,17 @@ namespace network { std::string *errorMessage = nullptr ); + /** + * @brief Quit a confirmed in-progress host app through authenticated HTTPS and verify it stopped. + * @param address Saved host address. + * @param preferredHttpPort Saved HTTP port override. + * @param clientIdentity Paired client credentials used for the cancel request. + * @param expectedAppId App the user confirmed; a different running app is never intentionally canceled. + * @param errorMessage Receives transport, authorization or host rejection details. + * @return True when the host reports no running app, including an already stopped app. + */ + bool quit_host_app(const std::string &address, uint16_t preferredHttpPort, const PairingIdentity &clientIdentity, int expectedAppId, std::string *errorMessage = nullptr); + /** * @brief Pair the client with a host using the provided request parameters. * diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index b014d0b..a021e62 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -405,37 +406,83 @@ namespace streaming { } } - bool FfmpegStreamBackend::save_screenshot(const std::string &path) { - // renderFrame belongs exclusively to the event thread and contains no composited UI. - const auto &frame = video_.renderFrame; - if (frame.width <= 0 || frame.height <= 0) { - logging::warn("stream", "Cannot take a screenshot before the first video frame is presented"); - return false; - } - if (!platform::ensure_directory_exists(platform::parent_directory(path))) { - return false; + bool FfmpegStreamBackend::request_screenshot(const std::string &directory, const app::ScreenshotSource &source) { + if (const auto result = take_screenshot_result(); !result.empty()) { + logging::info("screenshots", result); } - SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, frame.width, frame.height, 24, SDL_PIXELFORMAT_BGR24); - if (surface == nullptr) { + if (screenshot_.thread != nullptr || video_.renderFrame.width <= 0 || video_.renderFrame.height <= 0) { + logging::warn("screenshots", "Capture not queued: another screenshot is pending or no video frame is available"); return false; } - SwsContext *conversion = sws_getContext(frame.width, frame.height, AV_PIX_FMT_YUV420P, frame.width, frame.height, AV_PIX_FMT_BGR24, SWS_POINT, nullptr, nullptr, nullptr); + // The event thread owns renderFrame. Copy only its planar YUV payload here; + // conversion, directory creation, BMP writes and indexing belong to the worker. + const auto &frame = video_.renderFrame; + auto &snapshot = screenshot_.frame; + snapshot.width = frame.width; + snapshot.height = frame.height; + snapshot.yPitch = frame.yPitch; + snapshot.uPitch = frame.uPitch; + snapshot.vPitch = frame.vPitch; + snapshot.matrix = frame.matrix; + snapshot.fullRange = frame.fullRange; + const std::array sources {&frame.yPlane, &frame.uPlane, &frame.vPlane}; + const std::array destinations {&snapshot.yPlane, &snapshot.uPlane, &snapshot.vPlane}; + for (std::size_t i = 0; i < sources.size(); ++i) { + destinations[i]->resize(sources[i]->size()); + std::memcpy(destinations[i]->data(), sources[i]->data(), sources[i]->size()); + } + screenshot_.source = source; + screenshot_.directory = directory; + screenshot_.capturedAt = static_cast(std::time(nullptr)); + screenshot_.result.clear(); + screenshot_.complete.store(false, std::memory_order_relaxed); + screenshot_.thread = SDL_CreateThread(save_screenshot_worker, "save-screenshot", &screenshot_); + if (screenshot_.thread == nullptr) { + screenshot_.frame = {}; + logging::warn("screenshots", std::string("Cannot start screenshot worker: ") + SDL_GetError()); + } + return screenshot_.thread != nullptr; + } + + std::string FfmpegStreamBackend::take_screenshot_result() { + if (screenshot_.thread == nullptr || !screenshot_.complete.load(std::memory_order_acquire)) { + return {}; + } + SDL_WaitThread(screenshot_.thread, nullptr); + screenshot_.thread = nullptr; + return std::exchange(screenshot_.result, {}); + } + + int FfmpegStreamBackend::save_screenshot_worker(void *context) { + auto &job = *static_cast(context); + // Stay below the high-priority streaming event loop without starving behind + // ongoing USB/audio work on the Xbox's single CPU. + SDL_SetThreadPriority(SDL_THREAD_PRIORITY_NORMAL); + std::string error; + const std::string path = app::next_screenshot_path(job.directory, job.capturedAt, error); + const auto &frame = job.frame; + SDL_Surface *surface = path.empty() ? nullptr : SDL_CreateRGBSurfaceWithFormat(0, frame.width, frame.height, 24, SDL_PIXELFORMAT_BGR24); + SwsContext *conversion = surface == nullptr ? nullptr : sws_getContext(frame.width, frame.height, AV_PIX_FMT_YUV420P, frame.width, frame.height, AV_PIX_FMT_BGR24, SWS_POINT, nullptr, nullptr, nullptr); bool saved = false; if (conversion != nullptr) { const int *coefficients = sws_getCoefficients(frame.matrix == VideoColorMatrix::bt709 ? SWS_CS_ITU709 : SWS_CS_ITU601); sws_setColorspaceDetails(conversion, coefficients, frame.fullRange, coefficients, 1, 0, 1 << 16, 1 << 16); - const std::array source {frame.yPlane.data(), frame.uPlane.data(), frame.vPlane.data(), nullptr}; - const std::array sourcePitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; - std::array destination {static_cast(surface->pixels), nullptr, nullptr, nullptr}; - const std::array destinationPitch {surface->pitch, 0, 0, 0}; - saved = sws_scale(conversion, source.data(), sourcePitch.data(), 0, frame.height, destination.data(), destinationPitch.data()) == frame.height && SDL_SaveBMP(surface, path.c_str()) == 0; + const std::array pixels {frame.yPlane.data(), frame.uPlane.data(), frame.vPlane.data(), nullptr}; + const std::array pitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; + std::array output {static_cast(surface->pixels), nullptr, nullptr, nullptr}; + const std::array outputPitch {surface->pitch, 0, 0, 0}; + saved = sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height && SDL_SaveBMP(surface, path.c_str()) == 0; sws_freeContext(conversion); } SDL_FreeSurface(surface); - if (!saved) { - logging::warn("stream", "Failed to save screenshot: " + path); + if (saved) { + job.result = app::append_screenshot({job.source, path, job.capturedAt}, app::screenshot_library_path(), error) ? "Saved " + path : "Saved " + path + "; could not update the screenshot library: " + error; + } else { + job.result = "Screenshot failed: " + (error.empty() ? std::string(SDL_GetError()) : error); } - return saved; + job.frame = {}; + job.complete.store(true, std::memory_order_release); + return 0; } FfmpegStreamBackend::~FfmpegStreamBackend() { @@ -476,6 +523,12 @@ namespace streaming { void FfmpegStreamBackend::shutdown() { cleanup_audio_decoder(); cleanup_video_decoder(); + if (screenshot_.thread != nullptr) { + SDL_WaitThread(screenshot_.thread, nullptr); + screenshot_.thread = nullptr; + logging::info("screenshots", screenshot_.result); + screenshot_.frame = {}; + } } bool FfmpegStreamBackend::has_decoded_video() const { diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index 0db5cdc..539dd2f 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -15,6 +15,7 @@ #include // local includes +#include "src/app/screenshot_library.h" #include "src/streaming/aligned_media_buffer.h" #include "src/streaming/pvideo_presenter.h" #include "src/streaming/stats_overlay.h" @@ -71,7 +72,7 @@ namespace streaming { void set_audio_playback_enabled(bool enabled); /** - * @brief Release all FFmpeg, SDL, and cached frame resources. + * @brief Drain any pending screenshot and release FFmpeg, SDL, and cached frame resources. */ void shutdown(); @@ -132,11 +133,17 @@ namespace streaming { void render_local_overlay(SDL_Renderer *renderer, int width, int height); /** - * @brief Save the current clean decoded frame as a BMP, independently of presentation path. - * @param path Destination file under writable Moonlight storage. - * @return True when conversion and file writing succeed. + * @brief Snapshot the displayed host frame and save it on a bounded background worker. + * @param directory Configured screenshot destination, or empty for the default. + * @param source Server and app identity captured at request time. + * @return True when queued; false before the first frame or while another capture is pending. */ - bool save_screenshot(const std::string &path); + bool request_screenshot(const std::string &directory, const app::ScreenshotSource &source); + /** + * @brief Reap a completed screenshot worker without blocking the streaming event loop. + * @return Completion or error detail, or empty when no result is ready. + */ + std::string take_screenshot_result(); /** * @brief Resolve the video rectangle available to shared GPU/software controls. @@ -372,6 +379,26 @@ namespace streaming { bool interlaced = false; ///< Source contains interlaced fields. }; + /** + * @brief Own one bounded screenshot job independently of live rendering and decoding. + */ + struct ScreenshotTask { + SDL_Thread *thread = nullptr; ///< Owned worker, joined after completion or at shutdown. + std::atomic complete = false; ///< Release/acquire publication of the completed result. + LatestVideoFrame frame; ///< Independently owned snapshot of the uncomposited YUV frame. + app::ScreenshotSource source; ///< Original host and app identity. + std::string directory; ///< Destination at request time. + std::int64_t capturedAt = 0; ///< UTC capture time. + std::string result; ///< Detail read only after the worker completes. + }; + + /** + * @brief Convert and save an owned YUV snapshot with lower priority than streaming. + * @param context ScreenshotTask owned by this backend until the worker is joined. + * @return Zero after publishing a completion result. + */ + static int save_screenshot_worker(void *context); + /** * @brief Hold FFmpeg and SDL state used by the video path. */ @@ -442,6 +469,7 @@ namespace streaming { VideoState video_ {}; AudioState audio_ {}; + ScreenshotTask screenshot_ {}; ///< Single capture slot, bounded to one additional YUV frame. PvideoPresenter presenter_; ///< Main-thread GPU presentation resources. VideoOverlay statisticsOverlay_; ///< Shared cached panel for all presentation paths. VideoStatistics statistics_; ///< Cumulative decode and presentation telemetry. diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index 92faa24..c3edcc3 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -1616,6 +1616,14 @@ namespace streaming { finalStatistics.showPerformanceMetrics = resources.statisticsEnabled; std::string finalMessage = describe_session_end(connectionState, app.name); + if (resources.controller.quit_app_requested()) { + std::string quitError; + if (network::quit_host_app(host.address, host.port, clientIdentity, app.id, &quitError)) { + finalMessage = "Quit " + app.name + " on " + host.displayName; + } else { + finalMessage += " | Couldn't quit app: " + quitError; + } + } if (!acceleration.fallbackReason.empty()) { finalMessage += " | SOFTWARE FALLBACK USED"; } diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 2b18370..0f10b20 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -271,7 +271,16 @@ namespace streaming { } StreamControls::StreamControls(): - router_(send_packet) {} + router_([this](const input::StreamPacket &packet) { + auto outgoing = packet; + if (packet.type == input::StreamPacketType::controller && packet.slot >= 0 && packet.slot < 4) { + latestHostController_[packet.slot] = packet.controller; + if (packet.slot == pulseSlot_) { + outgoing.controller.buttonFlags |= pulseButtons_; + } + } + send_packet(outgoing); + }) {} StreamControls::~StreamControls() { close(); @@ -296,6 +305,20 @@ namespace streaming { return keyboardTop_; } + bool StreamControls::quit_app_requested() const { + return menu_.disconnectAction == input::StreamDisconnectAction::quit_app; + } + + void StreamControls::release_pulse() { + if (pulseSlot_ >= 0) { + input::StreamPacket packet {input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}; + packet.controller = latestHostController_[pulseSlot_]; + pulseSlot_ = -1; + pulseButtons_ = 0; + send_packet(packet); + } + } + void StreamControls::close() { clear_panel(); icons_.clear(); @@ -330,10 +353,7 @@ namespace streaming { } void StreamControls::close_if_removed(int instance) { - if (pulseSlot_ >= 0) { - send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); - pulseSlot_ = -1; - } + release_pulse(); router_.disconnect(instance); if (mouseDevice_ == instance) { mouseDevice_ = -1; @@ -442,11 +462,14 @@ namespace streaming { bool StreamControls::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) { if (menu_.page == input::StreamMenuPage::quit_confirmation) { if (key == SDLK_RETURN) { - return menu_.confirm_disconnect(true); + return menu_.confirm_disconnect(input::StreamDisconnectAction::disconnect); + } + if (key == SDLK_q) { + return menu_.confirm_disconnect(input::StreamDisconnectAction::quit_app); } if (key == SDLK_ESCAPE) { dirty_ = true; - return menu_.confirm_disconnect(false); + return menu_.confirm_disconnect(input::StreamDisconnectAction::cancel); } return false; } @@ -541,19 +564,9 @@ namespace streaming { break; case input::StreamAction::screenshot: { - const auto capturedAt = static_cast(std::time(nullptr)); - std::string error; - const std::string path = app::next_screenshot_path(screenshotDirectory_, capturedAt, error); - if (!path.empty() && backend.save_screenshot(path)) { - const app::ScreenshotRecord record {screenshotSource_, path, capturedAt}; - message_ = app::append_screenshot(record, app::screenshot_library_path(), error) ? "Saved " + path : "Saved " + path + "; could not add it to the viewer."; - } else { - message_ = "Screenshot failed; check the logs."; - } - if (!error.empty()) { - logging::warn("stream", error); - } + message_ = backend.request_screenshot(screenshotDirectory_, screenshotSource_) ? "Saving screenshot in the background" : "Screenshot not queued; another capture may still be saving."; logging::info("stream", message_); + menu_.close(); break; } case input::StreamAction::guide: @@ -564,15 +577,16 @@ namespace streaming { message_ = "A connected controller and active stream are required."; break; } - if (pulseSlot_ >= 0) { - send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); - } + release_pulse(); pulseSlot_ = slot; pulseUntil_ = SDL_GetTicks() + 120; + pulseButtons_ = menu_.selected == input::StreamAction::guide ? SPECIAL_FLAG : MISC_FLAG; input::StreamPacket packet {input::StreamPacketType::controller, slot, router_.active_mask()}; - packet.controller.buttonFlags = menu_.selected == input::StreamAction::guide ? SPECIAL_FLAG : MISC_FLAG; + packet.controller = latestHostController_[slot]; + packet.controller.buttonFlags |= pulseButtons_; send_packet(packet); message_ = menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; + menu_.close(); break; } case input::StreamAction::none: @@ -582,6 +596,9 @@ namespace streaming { } bool StreamControls::pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled) { + if (const auto result = backend.take_screenshot_result(); !result.empty()) { + logging::info("screenshots", result); + } // Sampling and transport enqueueing share one owner so capture cannot race a stale gameplay packet. controllerNavigationApplied_ = false; SDL_PumpEvents(); @@ -697,7 +714,9 @@ namespace streaming { } break; case SDL_CONTROLLER_BUTTON_X: - if (active && menu_.page == input::StreamMenuPage::keyboard) { + if (menu_.page == input::StreamMenuPage::quit_confirmation) { + stop = menu_key(SDLK_q, backend, active, statisticsEnabled) || stop; + } else if (active && menu_.page == input::StreamMenuPage::keyboard) { tap_key(0x08); } break; @@ -884,9 +903,8 @@ namespace streaming { navigate_controller(sample, now); } } - if (pulseSlot_ >= 0 && (!visible() || static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { - send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); - pulseSlot_ = -1; + if (pulseSlot_ >= 0 && (static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { + release_pulse(); } if (!visible() && (wasVisible || dirty_)) { keyboardNavigation_.reset(); @@ -913,10 +931,7 @@ namespace streaming { void StreamControls::release_host_input() { router_.release_all(); - if (pulseSlot_ >= 0) { - send_packet({input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}); - pulseSlot_ = -1; - } + release_pulse(); } bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds, const SDL_Rect *clip) { @@ -1185,7 +1200,7 @@ namespace streaming { const int top = std::min(area.y, clip.y); panelDamage_.push_back({left, top, std::max(area.x + area.width, clip.x + clip.width) - left, std::max(area.y + area.height, clip.y + clip.height) - top}); } - constexpr std::array icons {"keyboard.svg", "mouse.svg", "scroll-text.svg", "power.svg", "button-guide.svg", "button-share.svg", "camera.svg", "chart-no-axes-combined.svg"}; + constexpr std::array icons {"keyboard.svg", "mouse.svg", "scroll-text.svg", "log-out.svg", "button-guide.svg", "button-share.svg", "camera.svg", "chart-no-axes-combined.svg"}; render_icon(renderer.get(), icons[wheel * 4 + sector], {x + (labelWidth - iconSize) / 2, groupY, iconSize, iconSize}); label(renderer.get(), font, input::stream_action_label(action, mouseDevice_ >= 0, stats), {x, groupY + iconSize + 2, labelWidth, line * 2}, false, false); } @@ -1223,16 +1238,19 @@ namespace streaming { render_hint(renderer.get(), font, "button-left-stick.svg", "Arrows", menu_.logOffset == 0 ? "Live / scroll" : "Scroll", {card.x + 8, card.y + card.h - line - 12, card.w / 2 - 16, line}); render_hint(renderer.get(), font, "button-b.svg", "Esc", "Back", {card.x + card.w / 2, card.y + card.h - line - 12, card.w / 2 - 16, line}); } else { - const int cardWidth = std::min(width - 32, std::max(380, width * 3 / 5)); + const int cardWidth = std::min(width - 32, std::max(560, width * 4 / 5)); const int cardHeight = line * 7; const SDL_Rect card {(width - cardWidth) / 2, (height - cardHeight) / 2, cardWidth, cardHeight}; ui::draw_dialog_surface(renderer.get(), card); - label(renderer.get(), font, "Disconnect from stream?", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); - label(renderer.get(), font, "The app will keep running on your PC.", {card.x + 20, card.y + 28 + line, card.w - 40, line * 2}, false, false); - for (int i = 0; i < 2; ++i) { - SDL_Rect button {card.x + 20 + i * (card.w - 32) / 2, card.y + card.h - line * 2, (card.w - 48) / 2, line + 12}; + label(renderer.get(), font, "Exit stream?", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); + label(renderer.get(), font, "Disconnect keeps the app running. Quit app closes it on the PC; unsaved progress may be lost.", {card.x + 20, card.y + 28 + line, card.w - 40, line * 2}, false, false); + constexpr std::array buttons {"button-b.svg", "button-a.svg", "button-x.svg"}; + constexpr std::array keys {"Esc", "Enter", "Q"}; + constexpr std::array labels {"Cancel", "Disconnect", "Quit app"}; + for (int i = 0; i < 3; ++i) { + SDL_Rect button {card.x + 16 + i * (card.w - 24) / 3, card.y + card.h - line * 2, (card.w - 48) / 3, line + 12}; ui::draw_dialog_round_rect(renderer.get(), button, {28, 40, 61, 255}, 6); - render_hint(renderer.get(), font, i == 0 ? "button-b.svg" : "button-a.svg", i == 0 ? "Esc" : "Enter", i == 0 ? "Cancel" : "Disconnect", {button.x + 4, button.y, button.w - 8, button.h}); + render_hint(renderer.get(), font, buttons[i], keys[i], labels[i], {button.x + 4, button.y, button.w - 8, button.h}); } } if (!focusOnly && !message_.empty() && menu_.page == input::StreamMenuPage::radial) { diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 940d5c5..9947948 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -57,6 +57,11 @@ namespace streaming { * @return True for top docking. */ bool keyboard_docked_top() const; + /** + * @brief Read whether exit confirmation requested terminating the host app. + * @return True only for the explicit Quit app action. + */ + bool quit_app_requested() const; /** * @brief Close all owned SDL references. */ @@ -213,6 +218,10 @@ namespace streaming { * @param code Virtual key code. */ void tap_key(int code); + /** + * @brief End a Guide/Share pulse while restoring the latest gameplay state. + */ + void release_pulse(); std::array devices_ {}; ///< Stable local controller slots. input::StreamInput router_; ///< Ordered event-thread host input. input::StreamMenu menu_; ///< Local menu state. @@ -228,6 +237,8 @@ namespace streaming { Uint32 lastTick_ = 0; ///< Pointer integration clock. int pulseSlot_ = -1; ///< Controller with a pending guide/share release. Uint32 pulseUntil_ = 0; ///< Guide/share pulse deadline. + std::uint32_t pulseButtons_ = 0; ///< Button held across normal packets after the wheel closes. + std::array latestHostController_ {}; ///< Latest unmodified transport state for pulse release. std::string message_; ///< Last action result. std::vector logs_; ///< Snapshot of runtime log lines. int menuWidth_ = 0; ///< Last video rectangle width used for local controls. diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index c213d1b..a7544b1 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -270,7 +270,7 @@ namespace { */ struct KeypadModalLayoutCache { SDL_Window *window = nullptr; ///< Shared window; needed only for validated partial Xbox framebuffer presentation. - FolderDialogCache folder; ///< Retained folder browser, sharing the local-dialog lifetime. + FolderDialogCache folder; ///< Shared retained rows for host/app menus, other action dialogs and the folder browser. ScreenshotTextureCache screenshot; ///< At most one decoded capture is retained. int modalInnerWidth = 0; ///< Last modal content width used to build the cached line layout. int modalTextHeight = 0; ///< Cached combined height of the modal body text. @@ -1292,7 +1292,13 @@ namespace { draw_rect(renderer, {rect.x - inset, rect.y - inset, rect.w + inset * 2, rect.h + inset * 2}, ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE); } } - const std::string badge = tile.running ? "PLAYING" : tile.badgeLabel; + if (tile.running) { + const int size = std::min(42, std::max(24, rect.w / 3)); + const SDL_Rect paused {rect.x + rect.w - size - 8, rect.y + rect.h - size - 8, size, size}; + fill_rounded_rect(renderer, paused, {BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE, 0xEF}, 6); + render_asset_icon(renderer, assetCache, "icons\\pause.svg", {paused.x + 4, paused.y + 4, size - 8, size - 8}); + } + const std::string badge = tile.badgeLabel; if (!badge.empty()) { const int badgeHeight = TTF_FontLineSkip(labelFont) + 8; const int badgeWidth = std::min(rect.w - 12, static_cast(badge.size()) * std::max(7, TTF_FontHeight(labelFont) / 2) + 16); @@ -2804,6 +2810,17 @@ namespace { network::HostPairingServerInfo serverInfo {}; ///< Metadata returned by the host. }; + /** + * @brief One confirmed host quit operation, isolated from UI selection changes. + */ + struct AppQuitTaskState { + SDL_Thread *thread = nullptr; ///< Owned background HTTP worker. + std::atomic completed = false; ///< Publishes success and error detail to the shell. + app::AppQuitState target; ///< Server and app confirmed by the user. + bool success = false; ///< True after the host verified termination. + std::string message; ///< Host error or completion detail. + }; + struct AppListTaskState { SDL_Thread *thread = nullptr; std::atomic completed = false; @@ -4233,9 +4250,9 @@ namespace { const float horizontalScale = static_cast(framebufferWidth) / static_cast(screenWidth); auto *folder = keypadModalLayoutCache != nullptr ? &keypadModalLayoutCache->folder : nullptr; - const bool folderBrowser = viewModel.modal.folderBrowser && !viewModel.overlay.visible && !viewModel.keypad.visible; + const bool folderBrowser = viewModel.modal.visible && !viewModel.modal.busy && !viewModel.modal.actions.empty() && !viewModel.logViewer.visible && !viewModel.screenshot.visible && !viewModel.overlay.visible && !viewModel.keypad.visible; if (folder != nullptr) { - const bool reusable = folderBrowser && folder->valid && folder->logicalWidth == screenWidth && folder->framebufferWidth == framebufferWidth && folder->framebufferHeight == framebufferHeight && folder->titleFont == titleFont && folder->bodyFont == bodyFont && folder->smallFont == smallFont && ui::same_folder_dialog_content(folder->content, viewModel.modal); + const bool reusable = folderBrowser && folder->valid && folder->logicalWidth == screenWidth && folder->framebufferWidth == framebufferWidth && folder->framebufferHeight == framebufferHeight && folder->titleFont == titleFont && folder->bodyFont == bodyFont && folder->smallFont == smallFont && ui::same_action_dialog_content(folder->content, viewModel.modal); if (reusable) { std::size_t selected = 0; for (std::size_t i = 0; i < viewModel.modal.actions.size(); ++i) { @@ -4762,7 +4779,8 @@ namespace { SDL_GameController **controller, ShellInputState *inputState, const ProcessCommand &processCommand, - bool *skipPolledControllerNavigation + bool *skipPolledControllerNavigation, + bool *redrawRequested = nullptr ) { SDL_Event event; if (skipPolledControllerNavigation != nullptr) { @@ -4773,6 +4791,9 @@ namespace { } do { + if (redrawRequested != nullptr && (event.type == SDL_KEYDOWN || event.type == SDL_TEXTINPUT || event.type == SDL_WINDOWEVENT || event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERDEVICEADDED || event.type == SDL_CONTROLLERDEVICEREMOVED)) { + *redrawRequested = true; + } const input::UiCommand command = handle_shell_event(state, event, controller, inputState); if (event.type == SDL_CONTROLLERAXISMOTION && inputState != nullptr) { update_trigger_repeat_tick(command, SDL_GetTicks(), &inputState->leftTriggerRepeatTick, &inputState->rightTriggerRepeatTick); @@ -4835,7 +4856,58 @@ namespace { } /** - * @brief Stores the SDL resources and render caches owned by the shell loop. + * @brief Perform the authenticated quit request on a worker using only its copied target. + * @param context AppQuitTaskState retained until this callback completes. + * @return Zero after publishing the result to the shell. + */ + int run_app_quit_task(void *context) { // NOSONAR(cpp:S5008) SDL thread callback ABI + auto &task = *static_cast(context); + network::PairingIdentity identity {}; + if (load_saved_pairing_identity_for_streaming(&identity, &task.message)) { + task.success = network::quit_host_app(task.target.address, task.target.port, identity, task.target.appId, &task.message); + } + task.completed.store(true, std::memory_order_release); + return 0; + } + + /** + * @brief Start a confirmed quit operation without blocking navigation or rendering on HTTP. + * @param state Retained target and progress dialog. + * @param task Worker slot owned by the shell runtime. + */ + void start_app_quit_task(app::ClientState &state, AppQuitTaskState &task) { + if (task.thread != nullptr || !state.appQuit.inProgress) { + return; + } + task.target = state.appQuit; + task.success = false; + task.message.clear(); + task.completed.store(false, std::memory_order_relaxed); + task.thread = SDL_CreateThread(run_app_quit_task, "quit-host-app", &task); + if (task.thread == nullptr) { + app::apply_app_quit_result(state, false, std::string("Could not start quit request: ") + SDL_GetError()); + } + } + + /** + * @brief Apply and persist a completed host quit without waiting on unfinished workers. + * @param state Local host library and progress dialog. + * @param task Runtime-owned quit worker. + */ + void finish_app_quit_task(app::ClientState &state, AppQuitTaskState &task) { + if (task.thread == nullptr || !task.completed.load(std::memory_order_acquire)) { + return; + } + SDL_WaitThread(task.thread, nullptr); + task.thread = nullptr; + app::apply_app_quit_result(state, task.success, task.message); + app::AppUpdate update; + update.persistence.hostsChanged = task.success; + persist_hosts_if_needed(state, update); + } + + /** + * @brief Stores renderer resources and retained menu and screenshot caches. */ struct ShellResources { SDL_Renderer *renderer = nullptr; ///< Renderer used for all shell drawing. @@ -4856,7 +4928,7 @@ namespace { */ struct ShellRuntimeState { bool running = true; ///< False when the shell should stop processing frames. - bool keypadRedrawRequested = true; ///< True when a retained local input dialog needs a new frame. + bool keypadRedrawRequested = true; ///< True when visible shell state changed since its last retained frame. ShellInputState inputState {}; ///< Current controller and trigger input state. Uint32 nextHostDiscoveryTick = 0; ///< Next scheduled host auto-discovery time. Uint32 nextHostProbeTick = 0; ///< Next scheduled host probe time. @@ -4864,6 +4936,7 @@ namespace { PairingTaskState pairingTask {}; ///< Background pairing workflow state. ConnectionTestTaskState connectionTestTask {}; ///< Background manual connection test. AppListTaskState appListTask {}; ///< Background app-list fetch state. + AppQuitTaskState appQuitTask {}; ///< Background termination of a confirmed running host app. AppArtTaskState appArtTask {}; ///< Background box-art download state. HostDiscoveryTaskState hostDiscoveryTask {}; ///< Background host auto-discovery state. HostProbeTaskState hostProbeTask {}; ///< Background host probe state. @@ -5083,9 +5156,22 @@ namespace { return; } + const auto ready = [](const auto &task) { + return task.thread != nullptr && task.completed.load(std::memory_order_acquire); + }; + bool changed = ready(runtime->connectionTestTask) || ready(runtime->appListTask) || ready(runtime->appArtTask) || ready(runtime->hostDiscoveryTask) || ready(runtime->appQuitTask); + if (runtime->pairingTask.activeAttempt != nullptr) { + changed = changed || ready(*runtime->pairingTask.activeAttempt); + } + for (const auto &worker : runtime->hostProbeTask.workers) { + changed = changed || (worker != nullptr && ready(*worker)); + } + runtime->keypadRedrawRequested = runtime->keypadRedrawRequested || changed; + finish_pairing_task_if_ready(state, &runtime->pairingTask); finish_connection_test_if_ready(state, &runtime->connectionTestTask); finish_app_list_task_if_ready(state, &runtime->appListTask); + finish_app_quit_task(state, runtime->appQuitTask); finish_app_art_task_if_ready(state, &runtime->appArtTask, &resources->coverArtTextureCache); finish_host_discovery_task_if_ready(state, &runtime->hostDiscoveryTask, &runtime->nextHostProbeTick); finish_host_probe_task_if_ready(state, &runtime->hostProbeTask); @@ -5221,6 +5307,20 @@ namespace { const app::ScreenId previousScreen = state.shell.activeScreen; app::AppUpdate update = app::handle_command(state, command); process_directory_picker_requests(state, update); + if (update.requests.quitAppRequested) { + start_app_quit_task(state, runtime->appQuitTask); + } + if (update.requests.screenshotDeleteRequested && !state.screenshotViewer.entries.empty()) { + auto &viewer = state.screenshotViewer; + const auto &entry = viewer.entries[std::min(viewer.selectedIndex, viewer.entries.size() - 1)]; + std::string error; + const bool deleted = app::delete_screenshot(entry, app::screenshot_library_path(), error); + if (deleted) { + destroy_texture(resources->keypadModalLayoutCache.screenshot.texture); + resources->keypadModalLayoutCache.screenshot = {}; + } + app::apply_screenshot_delete_result(state, deleted, std::move(error)); + } if (update.requests.screenshotsRequested) { auto result = app::load_screenshots(state.screenshotViewer.source, app::screenshot_library_path()); state.screenshotViewer.entries = std::move(result.entries); @@ -5256,13 +5356,13 @@ namespace { if ((previousScreen != state.shell.activeScreen || update.navigation.screenChanged) && !draw_current_shell_frame(activeVideoMode, state, resources, runtime)) { return; } - if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && state.modal.id != app::ModalId::directory_picker && state.modal.id != app::ModalId::screenshot_viewer) { + if (!(state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) && state.modal.id == app::ModalId::none) { clear_keypad_modal_layout_cache(&resources->keypadModalLayoutCache); } } /** - * @brief Process shell input and background tasks, retaining idle local dialogs and screenshot frames. + * @brief Process shell input and background tasks, retaining every static screen until visible state changes. * * @param window Shared SDL window reused for shell and streaming rendering. * @param videoMode Active output mode used by the shell renderer. @@ -5277,7 +5377,9 @@ namespace { } const Uint32 notificationNow = SDL_GetTicks(); + const auto notificationCount = state.shell.notifications.size(); app::advance_notifications(state, notificationNow - runtime->notificationTick); + runtime->keypadRedrawRequested = runtime->keypadRedrawRequested || state.shell.notifications.size() != notificationCount; runtime->notificationTick = notificationNow; const auto processCommand = [window, &state, videoMode, resources, runtime](input::UiCommand command) { @@ -5292,7 +5394,7 @@ namespace { update_exit_combo_hold(state, &runtime->inputState); const auto previousInputDevice = state.shell.inputDevice; bool skipPolledControllerNavigation = false; - process_polled_shell_events(state, &resources->controller, &runtime->inputState, processCommand, &skipPolledControllerNavigation); + process_polled_shell_events(state, &resources->controller, &runtime->inputState, processCommand, &skipPolledControllerNavigation, &runtime->keypadRedrawRequested); if (state.shell.inputDevice == input::UiInputDevice::keyboard) { processCommand(runtime->inputState.keyboardNavigation.poll(SDL_GetTicks())); } else { @@ -5306,8 +5408,7 @@ namespace { finish_shell_background_tasks(state, resources, runtime); start_shell_background_tasks_if_needed(state, runtime, SDL_GetTicks()); - const bool retainedInputDialog = (state.shell.activeScreen == app::ScreenId::add_host && state.addHostDraft.keypad.visible) || (state.modal.id == app::ModalId::directory_picker && !state.directoryPicker.creating && !state.shell.overlayVisible) || (state.modal.id == app::ModalId::screenshot_viewer && !state.shell.overlayVisible); - if ((!retainedInputDialog || runtime->keypadRedrawRequested) && !draw_current_shell_frame(videoMode != nullptr ? *videoMode : VIDEO_MODE {}, state, resources, runtime)) { + if ((runtime->keypadRedrawRequested || ui::shell_requires_animation(state)) && !draw_current_shell_frame(videoMode != nullptr ? *videoMode : VIDEO_MODE {}, state, resources, runtime)) { return false; } @@ -5338,6 +5439,8 @@ namespace { } wait_for_thread(runtime->appListTask.thread); + wait_for_thread(runtime->appQuitTask.thread); + runtime->appQuitTask.thread = nullptr; wait_for_thread(runtime->connectionTestTask.thread); runtime->connectionTestTask.thread = nullptr; wait_for_thread(runtime->appArtTask.thread); diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index 1612468..b83244f 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -161,7 +161,7 @@ namespace { continue; } - const std::string detail = appRecord.running ? "Running now" : std::string {}; + const std::string detail = appRecord.running ? "In progress / Resume" : std::string {}; std::string badgeLabel; if (appRecord.favorite) { @@ -570,13 +570,17 @@ namespace { */ void fill_app_actions_modal_view(const app::ClientState &state, ui::ShellViewModel *viewModel) { if (const app::HostAppRecord *appRecord = app::selected_app(state); appRecord != nullptr) { + const auto selection = std::min(state.modal.selectedActionIndex, appRecord->running ? std::size_t {4} : std::size_t {3}); viewModel->modal.title = appRecord->name; viewModel->modal.actions = { - {"toggle-hidden-app", "Hide app", true, state.modal.selectedActionIndex == 0U, appRecord->hidden, {}, true}, - {"view-app-details", "View details", true, state.modal.selectedActionIndex == 1U, false}, - {"toggle-favorite", "Set favorite", true, state.modal.selectedActionIndex == 2U, appRecord->favorite, {}, true}, - {"view-screenshots", "Screenshots", true, state.modal.selectedActionIndex == 3U, false, "icons\\camera.svg"}, + {"toggle-hidden-app", "Hide app", true, selection == 0U, appRecord->hidden, {}, true}, + {"view-app-details", "View details", true, selection == 1U, false}, + {"toggle-favorite", "Set favorite", true, selection == 2U, appRecord->favorite, {}, true}, + {"view-screenshots", "Screenshots", true, selection == 3U, false, "icons\\camera.svg"}, }; + if (appRecord->running) { + viewModel->modal.actions.push_back({"quit-app", "Quit app", true, state.modal.selectedActionIndex == 4U, false, "icons\\power.svg"}); + } } } @@ -707,6 +711,12 @@ namespace { case app::ModalId::app_details: fill_app_details_modal_view(state, viewModel); return; + case app::ModalId::quit_progress: + viewModel->modal.title = "Quitting " + state.appQuit.appName; + viewModel->modal.lines = {"Waiting for the PC to close the app..."}; + viewModel->modal.busy = true; + viewModel->modal.iconAssetPath = "icons\\loader-circle.svg"; + return; case app::ModalId::log_viewer: fill_log_viewer_modal_view(state, viewModel); return; @@ -732,6 +742,7 @@ namespace { viewModel->modal.footerActions = { {"previous-screenshot", "Previous", "icons\\button-lb.svg", {}, false}, {"next-screenshot", "Next", "icons\\button-rb.svg", {}, false}, + {"delete-screenshot", "Delete", "icons\\button-x.svg", {}, false}, }; } viewModel->modal.footerActions.push_back({"back", "Back", "icons\\button-b.svg", {}, false}); @@ -778,7 +789,7 @@ namespace { { std::vector actions; if (app::selected_app(state) != nullptr) { - actions.emplace_back(ui::ShellFooterAction {"launch", "Launch", "icons\\button-a.svg", {}, true}); + actions.emplace_back(ui::ShellFooterAction {"launch", app::selected_app(state)->running ? "Resume" : "Launch", "icons\\button-a.svg", {}, true}); actions.emplace_back(ui::ShellFooterAction {"app-menu", "App Menu", "icons\\button-y.svg", {}, false}); } actions.emplace_back(ui::ShellFooterAction {"back", "Back", "icons\\button-b.svg", {}, false}); @@ -902,8 +913,22 @@ namespace { } // namespace namespace ui { - bool same_folder_dialog_content(const ShellModalView &previous, const ShellModalView ¤t) { - if (!previous.folderBrowser || !current.folderBrowser || !previous.visible || !current.visible || previous.busy || current.busy || previous.title != current.title || previous.iconAssetPath != current.iconAssetPath || previous.lines != current.lines || previous.actions.size() != current.actions.size() || previous.footerActions.size() != current.footerActions.size()) { + bool shell_requires_animation(const app::ClientState &state) { + if (state.shell.overlayVisible || state.modal.id == app::ModalId::quit_progress || (state.modal.id == app::ModalId::connection_test && state.connectionTest.inProgress)) { + return true; + } + if (state.modal.active()) { + return false; + } + if (state.shell.activeScreen == app::ScreenId::pair_host) { + return true; + } + const auto *host = app::apps_host(state); + return state.shell.activeScreen == app::ScreenId::apps && host != nullptr && host->appListState == app::HostAppListState::loading; + } + + bool same_action_dialog_content(const ShellModalView &previous, const ShellModalView ¤t) { + if (previous.folderBrowser != current.folderBrowser || !previous.visible || !current.visible || previous.busy || current.busy || previous.title != current.title || previous.iconAssetPath != current.iconAssetPath || previous.lines != current.lines || previous.actions.size() != current.actions.size() || previous.footerActions.size() != current.footerActions.size()) { return false; } for (std::size_t i = 0; i < current.actions.size(); ++i) { diff --git a/src/ui/shell_view.h b/src/ui/shell_view.h index 7410e6c..6340815 100644 --- a/src/ui/shell_view.h +++ b/src/ui/shell_view.h @@ -157,7 +157,7 @@ namespace ui { struct ShellModalView { bool visible = false; ///< True when a modal dialog should be rendered. bool busy = false; ///< Show progress instead of dismissal controls while work is pending. - bool folderBrowser = false; ///< Retain the folder dialog while only list focus changes. + bool folderBrowser = false; ///< Directory-dialog style; all static action dialogs share retained focus repainting. std::string title; ///< Modal dialog title. std::string iconAssetPath; ///< Optional status icon beside the dialog title. std::vector lines; ///< Modal dialog body lines. @@ -166,12 +166,18 @@ namespace ui { }; /** - * @brief Check whether folder-dialog pixels can be retained while list selection changes. + * @brief Check whether any action dialog can retain its pixels while list selection changes. * @param previous Previously painted dialog. - * @param current Current dialog, including path, error, entries and input helpers. + * @param current Current dialog, including guidance, action rows and input helpers. * @return True when every visible property except the focused action is unchanged. */ - bool same_folder_dialog_content(const ShellModalView &previous, const ShellModalView ¤t); + bool same_action_dialog_content(const ShellModalView &previous, const ShellModalView ¤t); + /** + * @brief Identify views whose visible contents animate without input or a completed task. + * @param state Current shell and workflow state. + * @return True for diagnostics and active progress indicators; static pages retain their frame. + */ + bool shell_requires_animation(const app::ClientState &state); /** * @brief Embedded log viewer state surfaced by the log-file modal. diff --git a/tests/unit/app/menu_actions_test.cpp b/tests/unit/app/menu_actions_test.cpp new file mode 100644 index 0000000..204477f --- /dev/null +++ b/tests/unit/app/menu_actions_test.cpp @@ -0,0 +1,222 @@ +/** + * @file tests/unit/app/menu_actions_test.cpp + * @brief Verifies retained action menus, confirmed capture deletion and host-app termination. + */ +// test includes +#include + +// standard includes +#include + +// local includes +#include "src/app/client_state.h" +#include "src/ui/shell_view.h" + +namespace { + /** + * @brief Open a cached app library with a running app and its local context menu. + * @return State whose first app owns the host's retained session. + */ + app::ClientState running_app_menu() { + auto state = app::create_initial_state(); + app::HostRecord host {"Gaming PC", "192.0.2.1", 0, app::PairingState::paired}; + host.uuid = "server-one"; + host.runningGameId = 7; + host.apps = {{"Game", 7}, {"Other", 8}}; + host.apps[0].running = true; + app::replace_hosts(state, {host}); + EXPECT_TRUE(app::begin_selected_host_app_browse(state, false)); + state.hosts.active.appListState = app::HostAppListState::ready; + app::handle_command(state, input::UiCommand::open_context_menu); + return state; + } + + /** + * @brief Open quit confirmation through its real context-menu action. + * @param state Library with its first app's actions open. + */ + void confirm_quit_menu(app::ClientState &state) { + state.modal.id = app::ModalId::app_actions; + state.modal.selectedActionIndex = 4; + app::handle_command(state, input::UiCommand::activate); + EXPECT_EQ(state.modal.id, app::ModalId::confirmation); + EXPECT_EQ(state.confirmation.action, app::ConfirmationAction::quit_app); + } + + /** + * @brief Host and app dialogs share selection-only retention and invalidate when their content changes. + */ + TEST(MenuActionsTest, RetainsHostAndAppActionsAcrossFocusChanges) { + for (const auto modal : {app::ModalId::host_actions, app::ModalId::app_actions}) { + auto state = running_app_menu(); + state.hosts.items = {state.hosts.active}; + state.hosts.loaded = true; + state.modal.id = modal; + const auto original = ui::build_shell_view_model(state, {}).modal; + EXPECT_FALSE(original.folderBrowser); + for (int i = 0; i < 8; ++i) { + app::handle_command(state, input::UiCommand::move_down); + EXPECT_TRUE(ui::same_action_dialog_content(original, ui::build_shell_view_model(state, {}).modal)); + } + auto changed = original; + changed.actions[0].label += " changed"; + EXPECT_FALSE(ui::same_action_dialog_content(original, changed)); + changed = original; + changed.busy = true; + EXPECT_FALSE(ui::same_action_dialog_content(original, changed)); + state.shell.inputDevice = input::UiInputDevice::keyboard; + EXPECT_FALSE(ui::same_action_dialog_content(original, ui::build_shell_view_model(state, {}).modal)); + } + } + + /** + * @brief Static screens and local dialogs stop idle redraws while diagnostics and progress keep animating. + */ + TEST(MenuActionsTest, AnimatesOnlyViewsThatNeedClockUpdates) { + auto state = running_app_menu(); + for (const auto modal : {app::ModalId::host_actions, app::ModalId::app_actions, app::ModalId::confirmation, app::ModalId::directory_picker, app::ModalId::screenshot_viewer, app::ModalId::log_viewer, app::ModalId::support}) { + state.modal.id = modal; + EXPECT_FALSE(ui::shell_requires_animation(state)); + } + state.modal.id = app::ModalId::none; + EXPECT_FALSE(ui::shell_requires_animation(state)); + state.hosts.active.appListState = app::HostAppListState::loading; + EXPECT_TRUE(ui::shell_requires_animation(state)); + state.modal.id = app::ModalId::app_actions; + EXPECT_FALSE(ui::shell_requires_animation(state)); + state.modal.id = app::ModalId::quit_progress; + EXPECT_TRUE(ui::shell_requires_animation(state)); + state.modal.id = app::ModalId::connection_test; + EXPECT_FALSE(ui::shell_requires_animation(state)); + state.connectionTest.inProgress = true; + EXPECT_TRUE(ui::shell_requires_animation(state)); + state.modal.id = app::ModalId::support; + state.shell.overlayVisible = true; + EXPECT_TRUE(ui::shell_requires_animation(state)); + state.shell.overlayVisible = false; + state.modal.id = app::ModalId::none; + for (auto screen : {app::ScreenId::hosts, app::ScreenId::settings, app::ScreenId::add_host}) { + state.shell.activeScreen = screen; + EXPECT_FALSE(ui::shell_requires_animation(state)); + } + state.shell.activeScreen = app::ScreenId::pair_host; + EXPECT_TRUE(ui::shell_requires_animation(state)); + } + + /** + * @brief Deleting a capture requires confirmation, preserves the selected capture on failure and handles the last image. + */ + TEST(MenuActionsTest, ConfirmsScreenshotDeletionAndRestoresViewerNavigation) { + auto state = running_app_menu(); + state.modal.selectedActionIndex = 3; + EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).requests.screenshotsRequested); + const auto source = state.screenshotViewer.source; + state.screenshotViewer.entries = {{source, "first.bmp", 20}, {source, "second.bmp", 10}}; + state.screenshotViewer.selectedIndex = 1; + EXPECT_FALSE(app::handle_command(state, input::UiCommand::delete_character).requests.screenshotDeleteRequested); + EXPECT_EQ(state.confirmation.targetPath, "second.bmp"); + EXPECT_EQ(state.modal.id, app::ModalId::confirmation); + app::handle_command(state, input::UiCommand::back); + EXPECT_EQ(state.modal.id, app::ModalId::screenshot_viewer); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 1U); + EXPECT_EQ(state.screenshotViewer.entries.size(), 2U); + app::handle_command(state, input::UiCommand::delete_character); + const auto accepted = app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(accepted.requests.screenshotDeleteRequested); + EXPECT_EQ(state.modal.id, app::ModalId::screenshot_viewer); + app::apply_screenshot_delete_result(state, false, "Read-only folder"); + EXPECT_EQ(state.screenshotViewer.entries.size(), 2U); + EXPECT_EQ(state.screenshotViewer.error, "Read-only folder"); + app::apply_screenshot_delete_result(state, true, {}); + ASSERT_EQ(state.screenshotViewer.entries.size(), 1U); + EXPECT_EQ(state.screenshotViewer.entries[0].path, "first.bmp"); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); + EXPECT_TRUE(state.screenshotViewer.error.empty()); + app::handle_command(state, input::UiCommand::delete_character); + app::handle_command(state, input::UiCommand::move_down); + EXPECT_FALSE(app::handle_command(state, input::UiCommand::activate).requests.screenshotDeleteRequested); + EXPECT_EQ(state.modal.id, app::ModalId::screenshot_viewer); + EXPECT_EQ(state.screenshotViewer.entries.size(), 1U); + app::apply_screenshot_delete_result(state, true, {}); + EXPECT_TRUE(state.screenshotViewer.entries.empty()); + EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); + app::apply_screenshot_delete_result(state, true, {}); + EXPECT_FALSE(app::handle_command(state, input::UiCommand::delete_character).requests.screenshotDeleteRequested); + EXPECT_EQ(state.modal.id, app::ModalId::screenshot_viewer); + app::handle_command(state, input::UiCommand::back); + app::apply_screenshot_delete_result(state, false, "stale result"); + EXPECT_TRUE(state.screenshotViewer.error.empty()); + EXPECT_EQ(state.modal.id, app::ModalId::app_actions); + } + + /** + * @brief Quit targets are retained through confirmation, errors keep the session resumable, and success clears its marker. + */ + TEST(MenuActionsTest, ConfirmsQuitAndClearsOnlyTheCompletedApp) { + auto state = running_app_menu(); + const auto view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.actions.size(), 5U); + EXPECT_EQ(view.modal.actions[4].id, "quit-app"); + EXPECT_TRUE(view.content.appTiles[0].running); + EXPECT_EQ(view.content.appTiles[0].detail, "In progress / Resume"); + confirm_quit_menu(state); + EXPECT_EQ(state.appQuit.appId, 7); + app::handle_command(state, input::UiCommand::back); + EXPECT_EQ(state.modal.id, app::ModalId::app_actions); + EXPECT_EQ(state.modal.selectedActionIndex, 4U); + EXPECT_FALSE(state.appQuit.inProgress); + confirm_quit_menu(state); + const auto update = app::handle_command(state, input::UiCommand::activate); + EXPECT_TRUE(update.requests.quitAppRequested); + EXPECT_TRUE(state.appQuit.inProgress); + EXPECT_EQ(state.modal.id, app::ModalId::quit_progress); + EXPECT_TRUE(ui::build_shell_view_model(state, {}).modal.busy); + app::handle_command(state, input::UiCommand::back); + EXPECT_EQ(state.modal.id, app::ModalId::quit_progress); + app::apply_app_quit_result(state, false, "Another client owns the app"); + EXPECT_TRUE(state.hosts.active.apps[0].running); + EXPECT_EQ(state.hosts.active.runningGameId, 7U); + EXPECT_FALSE(state.appQuit.inProgress); + EXPECT_EQ(state.modal.id, app::ModalId::none); + confirm_quit_menu(state); + app::handle_command(state, input::UiCommand::activate); + state.hosts.active.apps[1].running = true; + app::HostRecord unrelated {"Other PC", "192.0.2.2"}; + unrelated.runningGameId = 7; + unrelated.apps = {{"Other PC Game", 7}}; + unrelated.apps[0].running = true; + state.hosts.items = {state.hosts.active, unrelated}; + app::apply_app_quit_result(state, true, {}); + EXPECT_FALSE(state.hosts.active.apps[0].running); + EXPECT_TRUE(state.hosts.active.apps[1].running); + EXPECT_EQ(state.hosts.active.runningGameId, 0U); + EXPECT_FALSE(state.hosts.items[0].apps[0].running); + EXPECT_TRUE(state.hosts.items[1].apps[0].running); + EXPECT_EQ(state.hosts.items[1].runningGameId, 7U); + EXPECT_TRUE(state.hosts.dirty); + EXPECT_EQ(state.modal.id, app::ModalId::none); + app::handle_command(state, input::UiCommand::move_right); + EXPECT_EQ(state.apps.selectedAppIndex, 1U); + app::apply_app_quit_result(state, true, "stale completion"); + EXPECT_TRUE(state.hosts.active.apps[1].running); + } + + /** + * @brief A removed quit action cannot wrap into Hide app when a background refresh marks the app stopped. + */ + TEST(MenuActionsTest, StoppedAppCannotTurnAStaleQuitSelectionIntoHide) { + auto state = running_app_menu(); + state.modal.selectedActionIndex = 4; + state.hosts.active.apps[0].running = false; + EXPECT_TRUE(ui::build_shell_view_model(state, {}).modal.actions[3].selected); + const auto update = app::handle_command(state, input::UiCommand::activate); + EXPECT_FALSE(update.requests.quitAppRequested); + EXPECT_FALSE(state.hosts.active.apps[0].hidden); + EXPECT_EQ(state.modal.id, app::ModalId::app_actions); + EXPECT_EQ(state.modal.selectedActionIndex, 3U); + EXPECT_EQ(ui::build_shell_view_model(state, {}).modal.actions.size(), 4U); + state.modal.selectedActionIndex = 4; + app::handle_command(state, input::UiCommand::move_down); + EXPECT_EQ(state.modal.selectedActionIndex, 0U); + } +} // namespace diff --git a/tests/unit/app/screenshot_library_test.cpp b/tests/unit/app/screenshot_library_test.cpp index 9e28546..44491a6 100644 --- a/tests/unit/app/screenshot_library_test.cpp +++ b/tests/unit/app/screenshot_library_test.cpp @@ -228,10 +228,10 @@ namespace { EXPECT_EQ(state.screenshotViewer.selectedIndex, 0U); state.shell.inputDevice = input::UiInputDevice::keyboard; view = ui::build_shell_view_model(state, {}); - ASSERT_EQ(view.modal.footerActions.size(), 3U); + ASSERT_EQ(view.modal.footerActions.size(), 4U); EXPECT_EQ(view.modal.footerActions[0].keyLabel, "PgUp"); EXPECT_EQ(view.modal.footerActions[1].keyLabel, "PgDn"); - EXPECT_EQ(view.modal.footerActions[2].keyLabel, "Esc"); + EXPECT_EQ(view.modal.footerActions[3].keyLabel, "Esc"); app::handle_command(state, input::UiCommand::back); EXPECT_EQ(state.modal.id, app::ModalId::app_actions); EXPECT_EQ(state.modal.selectedActionIndex, 3U); @@ -241,4 +241,75 @@ namespace { app::handle_command(state, input::UiCommand::move_right); EXPECT_EQ(state.apps.selectedAppIndex, 1U); } + + /** + * @brief Deletion removes the selected bitmap and persists only its removal across library reloads. + */ + TEST_F(ScreenshotLibraryTest, DeletesOnlyTheChosenCaptureAndCanReuseItsFilename) { + std::string error; + const auto first = app::next_screenshot_path(root + "/shots", 10, error); + const auto otherPath = root + "/other.bmp"; + { + std::ofstream image(first); + image << "first image"; + } + { + std::ofstream image(otherPath); + image << "other app image"; + } + const app::ScreenshotRecord record {source, first, 10}; + auto other = source; + other.appId = 8; + ASSERT_TRUE(app::append_screenshot(record, journal, error)); + ASSERT_TRUE(app::append_screenshot({other, otherPath, 11}, journal, error)); + ASSERT_TRUE(app::delete_screenshot(record, journal, error)) << error; + EXPECT_FALSE(std::filesystem::exists(first)); + EXPECT_TRUE(std::filesystem::exists(otherPath)); + EXPECT_TRUE(app::load_screenshots(source, journal).entries.empty()); + EXPECT_EQ(app::load_screenshots(other, journal).entries.size(), 1U); + EXPECT_TRUE(app::load_screenshots(source, journal).error.empty()); + EXPECT_TRUE(app::delete_screenshot(record, journal, error)); + { + std::ofstream replacement(first); + replacement << "replacement capture"; + } + ASSERT_TRUE(app::append_screenshot({source, first, 20}, journal, error)); + const auto reloaded = app::load_screenshots(source, journal); + ASSERT_EQ(reloaded.entries.size(), 1U); + EXPECT_EQ(reloaded.entries[0].capturedAt, 20); + EXPECT_FALSE(reloaded.entries[0].deleted); + auto tombstone = record; + tombstone.deleted = true; + const auto parsed = app::parse_screenshot(app::serialize_screenshot(tombstone)); + ASSERT_TRUE(parsed.has_value()); + EXPECT_TRUE(parsed->deleted); + } + + /** + * @brief Invalid metadata, an unavailable index and filesystem errors never silently remove a visible capture. + */ + TEST_F(ScreenshotLibraryTest, ReportsDeletionFailuresWithoutLosingOtherCaptures) { + std::string error; + const auto first = app::next_screenshot_path(root, 10, error); + { + std::ofstream image(first); + image << "capture"; + } + const app::ScreenshotRecord record {source, first, 10}; + ASSERT_TRUE(app::append_screenshot(record, journal, error)); + EXPECT_FALSE(app::delete_screenshot({}, journal, error)); + EXPECT_FALSE(error.empty()); + EXPECT_FALSE(app::delete_screenshot(record, root + "/missing/index.tsv", error)); + EXPECT_TRUE(std::filesystem::exists(first)); + std::filesystem::create_directory(root + "/protected.bmp"); + { + std::ofstream child(root + "/protected.bmp/child.txt"); + child << "keep"; + } + EXPECT_FALSE(app::delete_screenshot({source, root + "/protected.bmp", 10}, journal, error)); + EXPECT_FALSE(error.empty()); + EXPECT_TRUE(std::filesystem::exists(root + "/protected.bmp/child.txt")); + EXPECT_EQ(app::load_screenshots(source, journal).entries.size(), 1U); + EXPECT_TRUE(std::filesystem::exists(first)); + } } // namespace diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp index 299f760..580053c 100644 --- a/tests/unit/input/stream_menu_test.cpp +++ b/tests/unit/input/stream_menu_test.cpp @@ -208,17 +208,24 @@ TEST(StreamMenuTest, SendsSymbolsAndDesktopCombinations) { */ TEST(StreamMenuTest, RequiresExplicitDisconnectAndCancelsDirectly) { input::StreamMenu menu; - EXPECT_FALSE(menu.confirm_disconnect(true)); + EXPECT_FALSE(menu.confirm_disconnect(input::StreamDisconnectAction::disconnect)); menu.open(1); - EXPECT_FALSE(menu.confirm_disconnect(true)); + EXPECT_FALSE(menu.confirm_disconnect(input::StreamDisconnectAction::disconnect)); menu.page = input::StreamMenuPage::quit_confirmation; menu.navigate(1, 0); menu.navigate(0, -1); EXPECT_EQ(menu.page, input::StreamMenuPage::quit_confirmation); - EXPECT_FALSE(menu.confirm_disconnect(false)); + EXPECT_FALSE(menu.confirm_disconnect(input::StreamDisconnectAction::cancel)); EXPECT_EQ(menu.page, input::StreamMenuPage::radial); menu.page = input::StreamMenuPage::quit_confirmation; - EXPECT_TRUE(menu.confirm_disconnect(true)); + EXPECT_TRUE(menu.confirm_disconnect(input::StreamDisconnectAction::disconnect)); + EXPECT_EQ(menu.disconnectAction, input::StreamDisconnectAction::disconnect); + menu.open(1); + EXPECT_EQ(menu.disconnectAction, input::StreamDisconnectAction::cancel); + menu.page = input::StreamMenuPage::quit_confirmation; + EXPECT_TRUE(menu.confirm_disconnect(input::StreamDisconnectAction::quit_app)); + EXPECT_EQ(menu.disconnectAction, input::StreamDisconnectAction::quit_app); + EXPECT_STREQ(input::stream_action_label(input::StreamAction::quit, false, false), "Exit"); } /** diff --git a/tests/unit/network/host_quit_test.cpp b/tests/unit/network/host_quit_test.cpp new file mode 100644 index 0000000..bdaa289 --- /dev/null +++ b/tests/unit/network/host_quit_test.cpp @@ -0,0 +1,161 @@ +/** + * @file tests/unit/network/host_quit_test.cpp + * @brief Verifies authenticated cancellation, stale-target protection and host quit verification. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/network/host_pairing.h" + +namespace { + /** + * @brief Own paired credentials and a scripted host without real network traffic. + */ + class HostQuitTest: public BaseTest { // NOSONAR(cpp:S3656) gtest fixture lifetime + protected: + network::PairingIdentity identity; ///< Valid client certificate and unique ID. + int running = 7; ///< App currently reported by the host. + bool paired = true; ///< Pairing status reported in serverinfo. + bool keepRunning = false; ///< Simulate a host that accepts cancel but refuses to terminate. + bool cancelTransportFailure = false; ///< Fail the authenticated cancel request. + bool serverTransportFailure = false; ///< Fail serverinfo requests. + bool failVerification = false; ///< Fail only the post-cancel serverinfo query. + int cancelStatus = 200; ///< HTTP status for cancel responses. + std::string cancelBody = "1"; ///< Scripted protocol result. + unsigned cancelRequests = 0; ///< Number of actual cancel requests. + + /** + * @brief Return sufficient valid host metadata for both plain and authenticated serverinfo queries. + * @return Serverinfo XML with the current app and pairing state. + */ + std::string server_info() const { + return "Test PC7.1.0.0server-one" + std::to_string(running) + "4798947990" + (paired ? "1" : "0") + ""; + } + + /** + * @brief Install deterministic serverinfo and cancel responses. + */ + void SetUp() override { + BaseTest::SetUp(); + identity = network::create_pairing_identity(); + ASSERT_TRUE(network::is_valid_pairing_identity(identity)); + network::testing::set_host_pairing_http_test_handler([this](const auto &request, auto *response, std::string *error, const std::atomic *) { + if (request.pathAndQuery.rfind("/cancel?", 0) == 0) { + ++cancelRequests; + EXPECT_TRUE(request.useTls); + EXPECT_EQ(request.port, 47990U); + EXPECT_EQ(request.tlsClientIdentity, &identity); + EXPECT_NE(request.pathAndQuery.find("uniqueid=" + identity.uniqueId), std::string::npos); + if (cancelTransportFailure) { + if (error != nullptr) { + *error = "Cancel request timed out"; + } + return false; + } + response->statusCode = cancelStatus; + response->body = cancelBody; + if (!keepRunning && cancelStatus == 200 && cancelBody.find("status_code=\"200\"") != std::string::npos) { + running = 0; + } + return true; + } + EXPECT_EQ(request.pathAndQuery.rfind("/serverinfo?", 0), 0U); + if (serverTransportFailure || (failVerification && cancelRequests != 0)) { + if (error != nullptr) { + *error = "Serverinfo unavailable"; + } + return false; + } + response->statusCode = 200; + response->body = server_info(); + return true; + }); + } + + /** + * @brief Remove the scripted transport after every test. + */ + void TearDown() override { + network::testing::clear_host_pairing_http_test_handler(); + BaseTest::TearDown(); + } + }; + + /** + * @brief Quit uses the paired HTTPS channel and verifies that the server reports an idle app slot. + */ + TEST_F(HostQuitTest, CancelsAndVerifiesTheConfirmedApp) { + std::string error; + EXPECT_TRUE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)) << error; + EXPECT_EQ(cancelRequests, 1U); + EXPECT_EQ(running, 0); + EXPECT_TRUE(error.empty()); + } + + /** + * @brief Already stopped and replaced sessions never receive a cancel for an unconfirmed target. + */ + TEST_F(HostQuitTest, ProtectsAlreadyStoppedAndDifferentRunningApps) { + std::string error; + running = 0; + EXPECT_TRUE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_EQ(cancelRequests, 0U); + running = 8; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_NE(error.find("changed"), std::string::npos); + EXPECT_EQ(cancelRequests, 0U); + } + + /** + * @brief Invalid targets, missing credentials and unpaired hosts are rejected before cancel. + */ + TEST_F(HostQuitTest, RequiresAValidPairedTarget) { + EXPECT_FALSE(network::quit_host_app({}, 47989, identity, 7)); + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 0)); + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, {}, 7)); + paired = false; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7)); + EXPECT_EQ(cancelRequests, 0U); + } + + /** + * @brief Transport, HTTP and protocol rejections retain an actionable failure instead of reporting success. + */ + TEST_F(HostQuitTest, ReportsTransportAndHostFailures) { + std::string error; + serverTransportFailure = true; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_NE(error.find("Serverinfo"), std::string::npos); + serverTransportFailure = false; + cancelTransportFailure = true; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_NE(error.find("timed out"), std::string::npos); + cancelTransportFailure = false; + cancelStatus = 403; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + cancelStatus = 200; + cancelBody = ""; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_NE(error.find("Another client"), std::string::npos); + cancelBody = "malformed response"; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + } + + /** + * @brief A successful cancel response alone cannot clear the local in-progress state. + */ + TEST_F(HostQuitTest, VerifiesTheHostActuallyClosedTheApp) { + std::string error; + keepRunning = true; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_NE(error.find("still running"), std::string::npos); + failVerification = true; + EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); + EXPECT_NE(error.find("Serverinfo"), std::string::npos); + } +} // namespace diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index 483e283..03d1e64 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -552,12 +552,12 @@ namespace { EXPECT_EQ(viewModel.content.appTiles[0].name, "Steam"); EXPECT_TRUE(viewModel.content.appTiles[0].selected); EXPECT_TRUE(viewModel.content.bodyLines.empty()); - EXPECT_EQ(viewModel.content.appTiles[0].detail, "Running now"); + EXPECT_EQ(viewModel.content.appTiles[0].detail, "In progress / Resume"); EXPECT_EQ(viewModel.content.appTiles[0].badgeLabel, "HDR"); EXPECT_TRUE(viewModel.content.appTiles[0].boxArtCached); EXPECT_TRUE(viewModel.content.appTiles[1].detail.empty()); ASSERT_EQ(viewModel.frame.footerActions.size(), 3U); - EXPECT_EQ(viewModel.frame.footerActions[0].label, "Launch"); + EXPECT_EQ(viewModel.frame.footerActions[0].label, "Resume"); EXPECT_EQ(viewModel.frame.footerActions[1].label, "App Menu"); EXPECT_EQ(viewModel.frame.footerActions[2].label, "Back"); } @@ -892,12 +892,12 @@ TEST(ShellViewTest, InvalidatesRetainedFolderContentsForEveryVisibleChange) { auto changed = original; changed.actions[0].selected = false; changed.actions[4].selected = true; - EXPECT_TRUE(ui::same_folder_dialog_content(original, changed)); + EXPECT_TRUE(ui::same_action_dialog_content(original, changed)); const auto reject = [&](auto mutate) { auto other = original; mutate(other); - EXPECT_FALSE(ui::same_folder_dialog_content(original, other)); - EXPECT_FALSE(ui::same_folder_dialog_content(other, original)); + EXPECT_FALSE(ui::same_action_dialog_content(original, other)); + EXPECT_FALSE(ui::same_action_dialog_content(other, original)); }; reject([](auto &view) { view.visible = false; @@ -963,5 +963,5 @@ TEST(ShellViewTest, InvalidatesRetainedFolderContentsForEveryVisibleChange) { view.footerActions[0].secondaryKeyLabel = "Space"; }); state.shell.inputDevice = input::UiInputDevice::keyboard; - EXPECT_FALSE(ui::same_folder_dialog_content(original, ui::build_shell_view_model(state, {}).modal)); + EXPECT_FALSE(ui::same_action_dialog_content(original, ui::build_shell_view_model(state, {}).modal)); } From 0c3ab09ba5e954cdc3a6639ecbb5c6dc4f0edb59 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:20:45 -0500 Subject: [PATCH 09/22] fix(input): recover menu gamepads after host app quit --- README.md | 2 + cmake/sources.cmake | 1 + src/input/shell_controller.cpp | 37 ++++ src/input/shell_controller.h | 21 ++ src/input/ui_input.h | 11 ++ src/ui/shell_screen.cpp | 49 +++-- tests/CMakeLists.txt | 18 ++ tests/controller/shell_controller_test.cpp | 219 +++++++++++++++++++++ tests/support/shell_controller/SDL.h | 49 +++++ tests/unit/input/ui_input_test.cpp | 31 +++ 10 files changed, 420 insertions(+), 18 deletions(-) create mode 100644 src/input/shell_controller.cpp create mode 100644 src/input/shell_controller.h create mode 100644 tests/controller/shell_controller_test.cpp create mode 100644 tests/support/shell_controller/SDL.h diff --git a/README.md b/README.md index 885b053..da709dc 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,8 @@ quits the app on the PC. Quitting can lose unsaved progress. An in-progress app its poster and can be selected to resume; its app action menu also offers **Quit app**, with confirmation. The pause marker describes a resumable streaming session; the PC app continues running after disconnecting. Returning from the stream resets local keyboard/controller holds before library navigation resumes. +If the menu's gamepad disconnects, the client automatically selects another connected gamepad without +requiring it to be unplugged or the client restarted. Keyboard navigation remains independent of this handoff. Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Open **Settings > Storage > Screenshot Folder** to browse drives and subfolders, select the current folder, diff --git a/cmake/sources.cmake b/cmake/sources.cmake index 47075a8..f1b74c8 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -14,6 +14,7 @@ list(REMOVE_ITEM MOONLIGHT_SOURCES "${MOONLIGHT_SOURCE_ROOT}/src/_nxdk_compat/poll_compat.cpp") set(MOONLIGHT_TEST_EXCLUDED_SOURCES + "${MOONLIGHT_SOURCE_ROOT}/src/input/shell_controller.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/main.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/splash/splash_screen.cpp" "${MOONLIGHT_SOURCE_ROOT}/src/streaming/ffmpeg_stream_backend.cpp" diff --git a/src/input/shell_controller.cpp b/src/input/shell_controller.cpp new file mode 100644 index 0000000..9348e84 --- /dev/null +++ b/src/input/shell_controller.cpp @@ -0,0 +1,37 @@ +/** + * @file src/input/shell_controller.cpp + * @brief Implements menu controller recovery across host virtual-gamepad removal and hotplug. + */ +// class header include +#include "src/input/shell_controller.h" + +namespace input { + bool recover_shell_controller(SDL_GameController **controller) { + if (controller == nullptr || (*controller != nullptr && SDL_GameControllerGetAttached(*controller) == SDL_TRUE)) { + return false; + } + const bool changed = *controller != nullptr; + if (*controller != nullptr) { + SDL_GameControllerClose(*controller); + *controller = nullptr; + } + for (int index = 0; index < SDL_NumJoysticks(); ++index) { + if (SDL_IsGameController(index) == SDL_FALSE) { + continue; + } + SDL_GameController *candidate = SDL_GameControllerOpen(index); + if (candidate == nullptr) { + continue; + } + // Removal and enumeration can overlap. Never retain another detached + // reference just because SDL still exposed its device index this frame. + if (SDL_GameControllerGetAttached(candidate) == SDL_FALSE) { + SDL_GameControllerClose(candidate); + continue; + } + *controller = candidate; + return true; + } + return changed; + } +} // namespace input diff --git a/src/input/shell_controller.h b/src/input/shell_controller.h new file mode 100644 index 0000000..132c7ff --- /dev/null +++ b/src/input/shell_controller.h @@ -0,0 +1,21 @@ +/** + * @file src/input/shell_controller.h + * @brief Recovers the shell's active gamepad without depending on rendering or device-added events. + */ +#pragma once + +// lib includes +#include + +namespace input { + /** + * @brief Preserve a working controller, or acquire an already attached replacement after removal. + * + * The caller owns the returned SDL reference. A missing device is retried on + * later input iterations, including when the shell retains an idle frame. + * + * @param controller Owned menu controller reference to preserve or replace; null is ignored. + * @return True when the owned menu reference changed, so the caller can reset stale gamepad holds. + */ + bool recover_shell_controller(SDL_GameController **controller); +} // namespace input diff --git a/src/input/ui_input.h b/src/input/ui_input.h index 4f380a1..c190a8a 100644 --- a/src/input/ui_input.h +++ b/src/input/ui_input.h @@ -8,6 +8,7 @@ #include #include +#include namespace input { constexpr std::uint32_t NAVIGATION_INITIAL_REPEAT_MILLISECONDS = 150; ///< Delay shared by keyboard and controller navigation. @@ -155,6 +156,16 @@ namespace input { *this = {}; controllerNavigationNeutralRequired = true; } + + /** + * @brief Clear a removed gamepad's holds while preserving any independent keyboard repeat. + */ + void reset_after_controller_change() { + auto keyboard = std::move(keyboardNavigation); + *this = {}; + keyboardNavigation = std::move(keyboard); + controllerNavigationNeutralRequired = true; + } }; } // namespace input diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index a7544b1..4c01a46 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -29,6 +29,7 @@ // local includes #include "src/app/settings_storage.h" #include "src/input/navigation_input.h" +#include "src/input/shell_controller.h" #include "src/input/stream_input.h" #include "src/logging/log_file.h" #include "src/logging/logger.h" @@ -4585,13 +4586,21 @@ namespace { } } - void handle_controller_device_added(SDL_GameController **controller, const SDL_ControllerDeviceEvent &event) { + /** + * @brief Acquire an added gamepad without interrupting keyboard holds or replacing a working controller. + * @param controller Owned menu controller. + * @param event SDL device-added notification. + * @param inputState Holds to reset after controller ownership changes. + */ + void handle_controller_device_added(SDL_GameController **controller, const SDL_ControllerDeviceEvent &event, ShellInputState *inputState) { if (controller == nullptr || !should_open_added_controller(*controller, event)) { return; } - *controller = SDL_GameControllerOpen(event.which); - if (*controller != nullptr) { + if (input::recover_shell_controller(controller) && *controller != nullptr) { + if (inputState != nullptr) { + inputState->reset_after_controller_change(); + } logging::info("input", "Controller connected"); } } @@ -4603,7 +4612,9 @@ namespace { close_controller(*controller); *controller = nullptr; - reset_shell_input_state(inputState); + if (inputState != nullptr) { + inputState->reset_after_controller_change(); + } logging::warn("input", "Controller disconnected"); } @@ -4709,7 +4720,7 @@ namespace { state.shell.shouldExit = true; return input::UiCommand::none; case SDL_CONTROLLERDEVICEADDED: - handle_controller_device_added(controller, event.cdevice); + handle_controller_device_added(controller, event.cdevice, inputState); return input::UiCommand::none; case SDL_CONTROLLERDEVICEREMOVED: handle_controller_device_removed(controller, event.cdevice, inputState); @@ -4973,21 +4984,11 @@ namespace { * @return The opened controller, or nullptr when none could be opened. */ SDL_GameController *open_primary_controller() { - for (int joystickIndex = 0; joystickIndex < SDL_NumJoysticks(); ++joystickIndex) { - if (!SDL_IsGameController(joystickIndex)) { - continue; - } - - SDL_GameController *controller = SDL_GameControllerOpen(joystickIndex); - if (controller == nullptr) { - continue; - } - + SDL_GameController *controller = nullptr; + if (input::recover_shell_controller(&controller)) { logging::info("input", "Opened primary controller"); - return controller; } - - return nullptr; + return controller; } /** @@ -5395,6 +5396,18 @@ namespace { const auto previousInputDevice = state.shell.inputDevice; bool skipPolledControllerNavigation = false; process_polled_shell_events(state, &resources->controller, &runtime->inputState, processCommand, &skipPolledControllerNavigation, &runtime->keypadRedrawRequested); + // Sunshine can remove a virtual pad after a quit completes. Other Xbox + // ports may already be attached, so no new ADDED event will arrive for them. + // Recover during input polling, even when an idle frame is not being drawn. + if (input::recover_shell_controller(&resources->controller)) { + runtime->inputState.reset_after_controller_change(); + runtime->keypadRedrawRequested = true; + if (resources->controller != nullptr) { + logging::info("input", "Recovered menu controller instance " + std::to_string(SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(resources->controller)))); + } else { + logging::warn("input", "Menu controller unavailable; waiting for an attached gamepad"); + } + } if (state.shell.inputDevice == input::UiInputDevice::keyboard) { processCommand(runtime->inputState.keyboardNavigation.poll(SDL_GetTicks())); } else { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d344033..66d37f3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -44,6 +44,7 @@ file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/tests/*.h ${CMAKE_SOURCE_DIR}/tests/*.cpp) list(FILTER TEST_SOURCES EXCLUDE REGEX "/tests/gpu/") +list(FILTER TEST_SOURCES EXCLUDE REGEX "/tests/controller/") add_executable(${PROJECT_NAME} ${TEST_SOURCES} @@ -114,3 +115,20 @@ target_link_options(test_pvideo_presenter PRIVATE ${TEST_COVERAGE_LINK_OPTIONS}) gtest_discover_tests(test_pvideo_presenter DISCOVERY_MODE PRE_TEST PROPERTIES ${TEST_RUNTIME_PROPERTIES}) + +# Exercise production SDL controller ownership without requiring an SDL installation +# or hardware in the native test build. +add_executable(test_shell_controller + "${CMAKE_SOURCE_DIR}/tests/controller/shell_controller_test.cpp" + "${CMAKE_SOURCE_DIR}/src/input/shell_controller.cpp") +target_compile_features(test_shell_controller PRIVATE cxx_std_17) +target_include_directories(test_shell_controller PRIVATE + "${CMAKE_SOURCE_DIR}/tests/support/shell_controller" + "${CMAKE_SOURCE_DIR}") +target_link_libraries(test_shell_controller PRIVATE gtest_main lizardbyte::test_support) +target_compile_options(test_shell_controller PRIVATE ${TEST_COVERAGE_COMPILE_OPTIONS}) +target_link_options(test_shell_controller PRIVATE ${TEST_COVERAGE_LINK_OPTIONS}) + +gtest_discover_tests(test_shell_controller + DISCOVERY_MODE PRE_TEST + PROPERTIES ${TEST_RUNTIME_PROPERTIES}) diff --git a/tests/controller/shell_controller_test.cpp b/tests/controller/shell_controller_test.cpp new file mode 100644 index 0000000..7c91044 --- /dev/null +++ b/tests/controller/shell_controller_test.cpp @@ -0,0 +1,219 @@ +/** + * @file tests/controller/shell_controller_test.cpp + * @brief Verifies recovery from removed virtual gamepads and preservation of live menu-controller references. + */ +// test includes +#include + +// standard includes +#include +#include +#include + +// local includes +#include "src/input/shell_controller.h" + +/** + * @brief Stable controller handle whose device can disappear from enumeration while its reference remains open. + */ +struct _SDL_GameController { + int id = 0; ///< Registry identity, independent of shifting SDL device indices. +}; + +namespace { + /** + * @brief Simulated SDL device and reference ownership counters. + */ + struct Device { + bool attached = true; ///< Current connectivity. + bool mapped = true; ///< Whether the device is a gamepad. + bool openable = true; ///< Whether opening the device succeeds. + bool removedWhileOpening = false; ///< Simulate removal between enumeration and acquisition. + int references = 0; ///< Outstanding SDL controller references. + int opens = 0; ///< Successful open calls. + int closes = 0; ///< Close calls, including detached candidates. + }; + + std::array devices; ///< Devices survive removal until all open references are closed. + std::array handles; ///< Stable objects returned by successful opens. + std::vector available; ///< Current enumeration order; removal changes indices, not identities. + int enumerationCalls = 0; ///< Detect unnecessary rescans while the active pad remains attached. + + /** + * @brief Own an isolated SDL registry for one controller recovery scenario. + */ + class ShellControllerTest: public BaseTest { // NOSONAR(cpp:S3656) gtest fixture lifetime + protected: + /** + * @brief Reset the fake registry and all reference counters before each test. + */ + void SetUp() override { + BaseTest::SetUp(); + devices = {}; + handles = {}; + available.clear(); + enumerationCalls = 0; + } + + /** + * @brief Add a device without delivering a device-added notification to the recovery code. + * @param id Stable registry identity. + */ + void attach(int id) { + handles[id].id = id; + available.push_back(id); + } + + /** + * @brief Remove a device from enumeration while retaining its outstanding SDL handle. + * @param id Stable registry identity. + */ + void detach(int id) { + devices[id].attached = false; + available.erase(std::remove(available.begin(), available.end(), id), available.end()); + } + }; +} // namespace + +extern "C" { + int SDL_NumJoysticks() { + ++enumerationCalls; + return static_cast(available.size()); + } + + SDL_bool SDL_IsGameController(int index) { + return devices[available[index]].mapped ? SDL_TRUE : SDL_FALSE; + } + + SDL_GameController *SDL_GameControllerOpen(int index) { + const int id = available[index]; + auto &device = devices[id]; + if (!device.openable) { + return nullptr; + } + ++device.references; + ++device.opens; + if (device.removedWhileOpening) { + device.attached = false; + } + return &handles[id]; + } + + void SDL_GameControllerClose(SDL_GameController *controller) { + auto &device = devices[controller->id]; + EXPECT_GT(device.references, 0); + --device.references; + ++device.closes; + } + + SDL_bool SDL_GameControllerGetAttached(SDL_GameController *controller) { + return devices[controller->id].attached ? SDL_TRUE : SDL_FALSE; + } +} + +namespace { + /** + * @brief A newly appearing host virtual pad must not steal the working menu controller. + */ + TEST_F(ShellControllerTest, PreservesTheWorkingControllerAndItsReference) { + attach(0); + auto *active = SDL_GameControllerOpen(0); + attach(1); + available = {1, 0}; + EXPECT_FALSE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, &handles[0]); + EXPECT_EQ(devices[0].references, 1); + EXPECT_EQ(devices[0].closes, 0); + EXPECT_EQ(devices[1].opens, 0); + EXPECT_EQ(enumerationCalls, 0); + SDL_GameControllerClose(active); + } + + /** + * @brief Quit-related removal falls back to a pad that was already attached, without any new ADDED event. + */ + TEST_F(ShellControllerTest, ReplacesARemovedPrimaryWithAnAlreadyConnectedPad) { + attach(0); + attach(1); + auto *active = SDL_GameControllerOpen(0); + detach(0); + EXPECT_TRUE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, &handles[1]); + EXPECT_EQ(devices[0].closes, 1); + EXPECT_EQ(devices[0].references, 0); + EXPECT_EQ(devices[1].references, 1); + EXPECT_FALSE(input::recover_shell_controller(&active)); + EXPECT_EQ(devices[1].opens, 1); + SDL_GameControllerClose(active); + } + + /** + * @brief A removal handler may already have closed the reference before input polling scans the remaining ports. + */ + TEST_F(ShellControllerTest, AcquiresExistingDevicesAfterTheRemovalHandlerClosesThePrimary) { + attach(0); + attach(1); + auto *active = SDL_GameControllerOpen(0); + detach(0); + SDL_GameControllerClose(active); + active = nullptr; + EXPECT_TRUE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, &handles[1]); + EXPECT_EQ(devices[0].closes, 1); + SDL_GameControllerClose(active); + } + + /** + * @brief Recovery retries after a temporary empty device list even when no hotplug event accompanies its return. + */ + TEST_F(ShellControllerTest, RetriesWithoutRenderingOrDeviceAddedEvents) { + attach(0); + auto *active = SDL_GameControllerOpen(0); + detach(0); + EXPECT_TRUE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, nullptr); + EXPECT_FALSE(input::recover_shell_controller(&active)); + attach(1); + EXPECT_TRUE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, &handles[1]); + EXPECT_EQ(devices[0].closes, 1); + SDL_GameControllerClose(active); + } + + /** + * @brief Unmapped, failed and concurrently removed candidates cannot prevent selection of a later live controller. + */ + TEST_F(ShellControllerTest, SkipsUnavailableCandidatesAndClosesDetachedReferences) { + for (int id = 0; id < 5; ++id) { + attach(id); + } + devices[0].mapped = false; + devices[1].openable = false; + devices[2].removedWhileOpening = true; + SDL_GameController *active = nullptr; + EXPECT_TRUE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, &handles[3]); + EXPECT_EQ(devices[0].opens + devices[1].opens, 0); + EXPECT_EQ(devices[2].closes, 1); + EXPECT_EQ(devices[2].references, 0); + EXPECT_EQ(devices[4].opens, 0); + SDL_GameControllerClose(active); + } + + /** + * @brief No device or a null ownership slot does not report a spurious controller handoff every input frame. + */ + TEST_F(ShellControllerTest, HandlesAbsentDevicesAndNullOwnershipSlots) { + EXPECT_FALSE(input::recover_shell_controller(nullptr)); + EXPECT_EQ(enumerationCalls, 0); + SDL_GameController *active = nullptr; + EXPECT_FALSE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, nullptr); + attach(0); + devices[0].removedWhileOpening = true; + EXPECT_FALSE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, nullptr); + EXPECT_EQ(devices[0].references, 0); + EXPECT_EQ(devices[0].closes, 1); + } +} // namespace diff --git a/tests/support/shell_controller/SDL.h b/tests/support/shell_controller/SDL.h new file mode 100644 index 0000000..f80bc83 --- /dev/null +++ b/tests/support/shell_controller/SDL.h @@ -0,0 +1,49 @@ +/** + * @file tests/support/shell_controller/SDL.h + * @brief Minimal SDL controller API for native ownership and hotplug regression tests. + */ +#pragma once + +/** + * @brief SDL's boolean values used by the production recovery adapter. + */ +enum SDL_bool { + SDL_FALSE = 0, ///< No or detached. + SDL_TRUE = 1, ///< Yes or attached. +}; + +/** + * @brief Opaque SDL controller handle implemented by the test device registry. + */ +typedef struct _SDL_GameController SDL_GameController; + +extern "C" { + /** + * @brief Count currently enumerable joystick devices. + * @return Device count. + */ + int SDL_NumJoysticks(); + /** + * @brief Check whether an enumerable device has a gamepad mapping. + * @param index Current device index. + * @return True for mapped gamepads. + */ + SDL_bool SDL_IsGameController(int index); + /** + * @brief Acquire one SDL reference to a mapped gamepad. + * @param index Current device index. + * @return Owned handle, or null on failure. + */ + SDL_GameController *SDL_GameControllerOpen(int index); + /** + * @brief Release one owned gamepad reference. + * @param controller Owned handle being closed. + */ + void SDL_GameControllerClose(SDL_GameController *controller); + /** + * @brief Check whether an open gamepad still has a connected device. + * @param controller Open controller reference. + * @return True while the device is attached. + */ + SDL_bool SDL_GameControllerGetAttached(SDL_GameController *controller); +} diff --git a/tests/unit/input/ui_input_test.cpp b/tests/unit/input/ui_input_test.cpp index d58b846..eb44c1a 100644 --- a/tests/unit/input/ui_input_test.cpp +++ b/tests/unit/input/ui_input_test.cpp @@ -134,6 +134,37 @@ TEST(ShellInputStateTest, ResumesNavigationAfterStreamingConsumesReleaseEvents) EXPECT_EQ(state.keyboardNavigation.press(2, input::UiCommand::confirm, 10200), input::UiCommand::confirm); } +/** + * @brief A gamepad replacement clears stale controller input while an independently held keyboard key keeps repeating. + */ +TEST(ShellInputStateTest, ControllerHandoffPreservesKeyboardAndResetsGamepadHolds) { + input::ShellInputState state; + state.leftTriggerPressed = state.rightTriggerPressed = state.leftShoulderPressed = state.rightShoulderPressed = true; + state.controllerStartPressed = state.controllerBackPressed = state.controllerExitComboArmed = state.controllerExitComboTriggered = true; + state.controllerStartDownTick = state.controllerBackDownTick = state.leftShoulderRepeatTick = state.rightShoulderRepeatTick = state.leftTriggerRepeatTick = state.rightTriggerRepeatTick = 100; + state.moveUpHoldState = state.moveDownHoldState = state.moveLeftHoldState = state.moveRightHoldState = {true, 100, 200}; + EXPECT_EQ(state.keyboardNavigation.press(1, input::UiCommand::move_right, 100), input::UiCommand::move_right); + state.reset_after_controller_change(); + EXPECT_FALSE(state.leftTriggerPressed || state.rightTriggerPressed || state.leftShoulderPressed || state.rightShoulderPressed); + EXPECT_FALSE(state.controllerStartPressed || state.controllerBackPressed || state.controllerExitComboArmed || state.controllerExitComboTriggered); + EXPECT_EQ(state.controllerStartDownTick + state.controllerBackDownTick + state.leftShoulderRepeatTick + state.rightShoulderRepeatTick + state.leftTriggerRepeatTick + state.rightTriggerRepeatTick, 0U); + for (const auto &hold : {state.moveUpHoldState, state.moveDownHoldState, state.moveLeftHoldState, state.moveRightHoldState}) { + EXPECT_FALSE(hold.active); + EXPECT_EQ(hold.activatedTick, 0U); + EXPECT_EQ(hold.lastRepeatTick, 0U); + } + EXPECT_TRUE(state.controllerNavigationNeutralRequired); + EXPECT_EQ(state.keyboardNavigation.poll(249), input::UiCommand::none); + EXPECT_EQ(state.keyboardNavigation.poll(250), input::UiCommand::move_right); + state.keyboardNavigation.release(1); + EXPECT_EQ(state.keyboardNavigation.poll(500), input::UiCommand::none); + EXPECT_EQ(state.keyboardNavigation.press(2, input::UiCommand::confirm, 500), input::UiCommand::confirm); + state.reset_after_controller_change(); + EXPECT_EQ(state.keyboardNavigation.press(2, input::UiCommand::confirm, 600), input::UiCommand::none); + state.keyboardNavigation.release(2); + EXPECT_EQ(state.keyboardNavigation.press(2, input::UiCommand::confirm, 700), input::UiCommand::confirm); +} + /** * @brief Controller direction changes respond immediately and held movement matches keyboard repeats. */ From 39206f6f7e7936698253863268aa694d7a3b741b Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:14:10 -0500 Subject: [PATCH 10/22] fix(streaming): prevent screenshot exit hangs and show save progress --- README.md | 2 + cmake/nxdk.cmake | 16 +++- src/streaming/ffmpeg_stream_backend.cpp | 66 +++++++++++++++- src/streaming/ffmpeg_stream_backend.h | 7 ++ src/streaming/screenshot_status.cpp | 47 +++++++++++ src/streaming/screenshot_status.h | 56 ++++++++++++++ src/streaming/session.cpp | 8 +- src/streaming/stream_controls.cpp | 71 ++++++++++++++++- src/streaming/stream_controls.h | 15 ++++ .../unit/streaming/screenshot_status_test.cpp | 77 +++++++++++++++++++ 10 files changed, 354 insertions(+), 11 deletions(-) create mode 100644 src/streaming/screenshot_status.cpp create mode 100644 src/streaming/screenshot_status.h create mode 100644 tests/unit/streaming/screenshot_status_test.cpp diff --git a/README.md b/README.md index da709dc..deb80d6 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ files display an unavailable message. Captures saved before this index was intro and are not automatically assigned to an app. Taking a screenshot snapshots the currently displayed host frame and saves it in the background. +A progress popup remains visible while saving, then shows the saved path for five seconds. Capture errors +remain visible for eight seconds. Neither popup captures controller, mouse or keyboard input. Only one capture saves at a time; completion and errors are written to the log. The wheels close automatically after screenshot, Guide, Share, statistics and mouse-mode actions. diff --git a/cmake/nxdk.cmake b/cmake/nxdk.cmake index 22ca385..47036e3 100644 --- a/cmake/nxdk.cmake +++ b/cmake/nxdk.cmake @@ -230,8 +230,8 @@ function(_moonlight_prepare_nxdk_tools nxdk_dir cxbe_path source_dir build_dir o ) endfunction() -# Install USB hooks into the owned SDK copy and fingerprint the resulting sources. -function(_moonlight_prepare_usb_input_hooks nxdk_dir out_signature) +# Install SDL adapters into the owned SDK copy and fingerprint the resulting sources. +function(_moonlight_prepare_sdl_hooks nxdk_dir out_signature) # The vendored SDL Xbox event pump is a stub. Install small hooks only in # the owned SDK copy; the project adapter implements boot HID and SDL events. file(READ "${MOONLIGHT_NXDK_DIR}/lib/sdl/SDL2/src/joystick/xbox/SDL_xboxjoystick.c" joystick_source) @@ -249,7 +249,15 @@ function(_moonlight_prepare_usb_input_hooks nxdk_dir out_signature) events_source "${events_source}") file(CONFIGURE OUTPUT "${nxdk_dir}/lib/sdl/SDL2/src/video/xbox/SDL_xbevents.c" # cmake-lint: disable=E1126 CONTENT "${events_source}" @ONLY) - string(SHA256 signature "${joystick_source}\n${events_source}\nNXDK_USB_ENABLE_HID=y") + # An AC97 underrun can stop completion notifications. An unbounded wait + # leaves SDL_CloseAudioDevice joining a playback thread that never wakes. + file(READ "${MOONLIGHT_NXDK_DIR}/lib/sdl/SDL2/src/audio/xbox/SDL_xboxaudio.c" audio_source) + string(REPLACE "SDL_SemWait(_this->hidden->playsem);" + "SDL_SemWaitTimeout(_this->hidden->playsem, 100);" + audio_source "${audio_source}") + file(CONFIGURE OUTPUT "${nxdk_dir}/lib/sdl/SDL2/src/audio/xbox/SDL_xboxaudio.c" # cmake-lint: disable=E1126 + CONTENT "${audio_source}" @ONLY) + string(SHA256 signature "${joystick_source}\n${events_source}\n${audio_source}\nNXDK_USB_ENABLE_HID=y") set(${out_signature} "${signature}" PARENT_SCOPE) endfunction() @@ -258,7 +266,7 @@ endfunction() function(moonlight_prepare_nxdk nxdk_dir state_dir) file(MAKE_DIRECTORY "${state_dir}") - _moonlight_prepare_usb_input_hooks("${nxdk_dir}" input_signature) + _moonlight_prepare_sdl_hooks("${nxdk_dir}" input_signature) file(READ "${nxdk_dir}/.moonlight-source.signature" nxdk_revision) set(signature_inputs diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index a021e62..85ea65a 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -435,6 +436,7 @@ namespace streaming { screenshot_.directory = directory; screenshot_.capturedAt = static_cast(std::time(nullptr)); screenshot_.result.clear(); + screenshot_.progress.store(0, std::memory_order_relaxed); screenshot_.complete.store(false, std::memory_order_relaxed); screenshot_.thread = SDL_CreateThread(save_screenshot_worker, "save-screenshot", &screenshot_); if (screenshot_.thread == nullptr) { @@ -453,11 +455,57 @@ namespace streaming { return std::exchange(screenshot_.result, {}); } + int FfmpegStreamBackend::screenshot_progress() const { + return screenshot_.thread != nullptr ? screenshot_.progress.load(std::memory_order_relaxed) : -1; + } + + /** + * @brief Track actual completed BMP writes while retaining SDL's platform file implementation. + */ + struct ScreenshotFileProgress { + SDL_RWops *file; ///< Worker-owned file, closed after SDL finalizes the BMP header. + std::atomic &progress; ///< UI-readable percentage independent of file and surface lifetimes. + Sint64 expected; ///< Final 24-bit BMP length, including scanline padding and header. + Sint64 position = 0; ///< Current file position, including SDL's header fixup seeks. + Sint64 written = 0; ///< Highest written position, preventing header fixups from reducing progress. + }; + + /** + * @brief Delegate a BMP file seek while keeping progress accounting in sync. + * @param stream Progress-decorated SDL file. + * @param offset Byte offset relative to whence. + * @param whence SDL seek origin. + * @return New byte position or a negative value on error. + */ + Sint64 screenshot_file_seek(SDL_RWops *stream, Sint64 offset, int whence) { + auto &writer = *static_cast(stream->hidden.unknown.data1); + writer.position = SDL_RWseek(writer.file, offset, whence); + return writer.position; + } + + /** + * @brief Forward one BMP write and publish progress only for bytes actually written. + * @param stream Progress-decorated SDL file. + * @param pixels Header or pixel bytes. + * @param size Bytes per object. + * @param count Requested object count. + * @return Number of complete objects written. + */ + std::size_t screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { + auto &writer = *static_cast(stream->hidden.unknown.data1); + const auto written = SDL_RWwrite(writer.file, pixels, size, count); + writer.position += static_cast(written * size); + writer.written = std::max(writer.written, writer.position); + writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected), std::memory_order_relaxed); + return written; + } + int FfmpegStreamBackend::save_screenshot_worker(void *context) { auto &job = *static_cast(context); // Stay below the high-priority streaming event loop without starving behind // ongoing USB/audio work on the Xbox's single CPU. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_NORMAL); + job.progress.store(1, std::memory_order_relaxed); std::string error; const std::string path = app::next_screenshot_path(job.directory, job.capturedAt, error); const auto &frame = job.frame; @@ -471,11 +519,27 @@ namespace streaming { const std::array pitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; std::array output {static_cast(surface->pixels), nullptr, nullptr, nullptr}; const std::array outputPitch {surface->pitch, 0, 0, 0}; - saved = sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height && SDL_SaveBMP(surface, path.c_str()) == 0; + if (sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height) { + job.progress.store(10, std::memory_order_relaxed); + if (SDL_RWops *file = SDL_RWFromFile(path.c_str(), "wb"); file != nullptr) { + ScreenshotFileProgress writer {file, job.progress, 54 + static_cast((frame.width * 3 + 3) & ~3) * frame.height}; + SDL_RWops progressFile {}; + progressFile.hidden.unknown.data1 = &writer; + progressFile.seek = screenshot_file_seek; + progressFile.write = screenshot_file_write; + const bool encoded = SDL_SaveBMP_RW(surface, &progressFile, 0) == 0; + const bool closed = SDL_RWclose(file) == 0; + saved = encoded && closed; + if (!saved) { + std::remove(path.c_str()); + } + } + } sws_freeContext(conversion); } SDL_FreeSurface(surface); if (saved) { + job.progress.store(99, std::memory_order_relaxed); job.result = app::append_screenshot({job.source, path, job.capturedAt}, app::screenshot_library_path(), error) ? "Saved " + path : "Saved " + path + "; could not update the screenshot library: " + error; } else { job.result = "Screenshot failed: " + (error.empty() ? std::string(SDL_GetError()) : error); diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index 539dd2f..bb070d6 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -145,6 +145,12 @@ namespace streaming { */ std::string take_screenshot_result(); + /** + * @brief Read background capture progress without waiting for conversion or disk I/O. + * @return Zero through 99 while a capture is pending, or -1 when there is no worker. + */ + int screenshot_progress() const; + /** * @brief Resolve the video rectangle available to shared GPU/software controls. * @param width Output width, replaced with the fitted video width. @@ -385,6 +391,7 @@ namespace streaming { struct ScreenshotTask { SDL_Thread *thread = nullptr; ///< Owned worker, joined after completion or at shutdown. std::atomic complete = false; ///< Release/acquire publication of the completed result. + std::atomic progress = 0; ///< Conversion, BMP write and index progress; completion is published separately. LatestVideoFrame frame; ///< Independently owned snapshot of the uncomposited YUV frame. app::ScreenshotSource source; ///< Original host and app identity. std::string directory; ///< Destination at request time. diff --git a/src/streaming/screenshot_status.cpp b/src/streaming/screenshot_status.cpp new file mode 100644 index 0000000..2faba31 --- /dev/null +++ b/src/streaming/screenshot_status.cpp @@ -0,0 +1,47 @@ +/** + * @file src/streaming/screenshot_status.cpp + * @brief Implements throttled screenshot progress and automatic popup dismissal. + */ +#include "src/streaming/screenshot_status.h" + +// standard includes +#include +#include + +namespace streaming { + void ScreenshotNotification::begin() { + *this = {}; + visible = true; + saving = true; + } + + bool ScreenshotNotification::update(int progress) { + if (!saving) { + return false; + } + const int next = std::max(percent, std::clamp(progress, 0, 99) / 5 * 5); + return std::exchange(percent, next) != next; + } + + void ScreenshotNotification::finish(bool success, std::string message, std::uint32_t now) { + visible = true; + saving = false; + saved = success; + percent = success ? 100 : percent; + detail = std::move(message); + completedAt_ = now; + } + + bool ScreenshotNotification::tick(std::uint32_t now) { + if (!visible || saving || now - completedAt_ < (saved ? 5000U : 8000U)) { + return false; + } + visible = false; + return true; + } + + std::string ScreenshotNotification::title() const { + return saving ? "Saving screenshot... " + std::to_string(percent) + "%" : saved ? "Screenshot saved" : + "Screenshot failed"; + } +} // namespace streaming diff --git a/src/streaming/screenshot_status.h b/src/streaming/screenshot_status.h new file mode 100644 index 0000000..eb17d71 --- /dev/null +++ b/src/streaming/screenshot_status.h @@ -0,0 +1,56 @@ +/** + * @file src/streaming/screenshot_status.h + * @brief Tracks screenshot notification progress and completion lifetime without SDL dependencies. + */ +#pragma once + +// standard includes +#include +#include + +namespace streaming { + + /** + * @brief Keep saving feedback visible until completion, then dismiss it after a bounded interval. + */ + class ScreenshotNotification { + public: + /** + * @brief Start a new capture notification at zero percent. + */ + void begin(); + /** + * @brief Advance a pending capture, repainting only at five-percent boundaries. + * @param percent Completed work, bounded below completion until the file and index are finalized. + * @return True when the displayed progress changed. + */ + bool update(int percent); + /** + * @brief Publish the final result and start its dismissal timer. + * @param saved True when an image was saved successfully. + * @param detail Saved path or failure information. + * @param now Monotonic milliseconds, allowed to wrap. + */ + void finish(bool saved, std::string detail, std::uint32_t now); + /** + * @brief Dismiss completed feedback after five seconds, or errors after eight seconds. + * @param now Monotonic milliseconds, allowed to wrap. + * @return True when visibility changed. + */ + bool tick(std::uint32_t now); + /** + * @brief Describe current progress or the completed result for the popup title. + * @return Human-readable notification title. + */ + std::string title() const; + + bool visible = false; ///< True while saving or before the completion timer expires. + bool saving = false; ///< True until a worker publishes its result. + bool saved = false; ///< Whether the completed image exists on disk. + int percent = 0; ///< Displayed progress, with 100 reserved for successful completion. + std::string detail; ///< Completed path or an actionable failure description. + + private: + std::uint32_t completedAt_ = 0; ///< Tick at which completion was observed by the UI. + }; +} // namespace streaming diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index c3edcc3..a846c74 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -1033,11 +1033,11 @@ namespace { int width = 0; int height = 0; SDL_GetRendererOutputSize(resources->renderer, &width, &height); - const bool wasVisible = resources->controller.visible(); + const bool wasVisible = resources->controller.has_overlay(); if (resources->controller.pump(resources->mediaBackend, resources->bodyFont, width, height, connectionState->connectionStarted.load(), resources->statisticsEnabled)) { request_stream_stop(connectionState); } - if (wasVisible && !resources->controller.visible()) { + if (wasVisible && !resources->controller.has_overlay()) { resources->statisticsSampler = {}; } } @@ -1097,7 +1097,7 @@ namespace { * @return True when the video must be refreshed to display a new panel. */ bool update_stream_statistics_overlay(const StreamStartContext &context, const StreamConnectionState &connectionState, StreamUiResources *resources) { - if (resources == nullptr || resources->controller.visible() || !resources->statisticsEnabled || resources->statisticsFont == nullptr || !resources->mediaBackend.has_decoded_video()) { + if (resources == nullptr || resources->controller.has_overlay() || !resources->statisticsEnabled || resources->statisticsFont == nullptr || !resources->mediaBackend.has_decoded_video()) { return false; } const auto now = PltGetMicroseconds(); @@ -1242,7 +1242,7 @@ namespace { render_text_lines(resources->renderer, resources->bodyFont, lines, screenWidth, &cursorY); - if (resources->controller.visible() && mediaBackend != nullptr) { + if (resources->controller.has_overlay() && mediaBackend != nullptr) { mediaBackend->render_local_overlay(resources->renderer, screenWidth, screenHeight); } SDL_RenderPresent(resources->renderer); diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 0f10b20..a6f29ee 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -309,6 +309,10 @@ namespace streaming { return menu_.disconnectAction == input::StreamDisconnectAction::quit_app; } + bool StreamControls::has_overlay() const { + return visible() || screenshotNotification_.visible; + } + void StreamControls::release_pulse() { if (pulseSlot_ >= 0) { input::StreamPacket packet {input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}; @@ -564,7 +568,12 @@ namespace streaming { break; case input::StreamAction::screenshot: { - message_ = backend.request_screenshot(screenshotDirectory_, screenshotSource_) ? "Saving screenshot in the background" : "Screenshot not queued; another capture may still be saving."; + if (backend.request_screenshot(screenshotDirectory_, screenshotSource_)) { + screenshotNotification_.begin(); + } else if (backend.screenshot_progress() < 0) { + screenshotNotification_.finish(false, "Could not start capture. Check the stream log for details.", SDL_GetTicks()); + } + message_ = screenshotNotification_.title(); logging::info("stream", message_); menu_.close(); break; @@ -596,8 +605,13 @@ namespace streaming { } bool StreamControls::pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled) { + bool screenshotChanged = false; if (const auto result = backend.take_screenshot_result(); !result.empty()) { logging::info("screenshots", result); + screenshotNotification_.finish(result.rfind("Saved ", 0) == 0, result, SDL_GetTicks()); + screenshotChanged = true; + } else { + screenshotChanged = screenshotNotification_.update(backend.screenshot_progress()); } // Sampling and transport enqueueing share one owner so capture cannot race a stale gameplay packet. controllerNavigationApplied_ = false; @@ -845,6 +859,7 @@ namespace streaming { } poll(); const Uint32 now = SDL_GetTicks(); + screenshotChanged = screenshotNotification_.tick(now) || screenshotChanged; if (menu_.page == input::StreamMenuPage::logs && now - logRefreshTick_ >= 1000) { logRefreshTick_ = now; std::vector lines; @@ -909,11 +924,26 @@ namespace streaming { if (!visible() && (wasVisible || dirty_)) { keyboardNavigation_.reset(); router_.set_captured(!focused_); - backend.set_statistics_bitmap(0, 0, {}); + if (!screenshotNotification_.visible) { + backend.set_statistics_bitmap(0, 0, {}); + } clear_panel(); dirty_ = false; + screenshotChanged = true; } backend.local_overlay_size(width, height); + if (!visible() && screenshotChanged) { + if (screenshotNotification_.visible) { + render_screenshot_notification(backend, font, width, height); + } else { + backend.set_statistics_bitmap(0, 0, {}); + clear_panel(); + } + } + if (visible() && screenshotNotification_.visible && message_ != screenshotNotification_.title()) { + message_ = screenshotNotification_.title(); + dirty_ = true; + } if (visible() && (width != menuWidth_ || height != menuHeight_)) { dirty_ = true; menuWidth_ = width; @@ -934,6 +964,43 @@ namespace streaming { release_pulse(); } + void StreamControls::render_screenshot_notification(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height) { + if (font == nullptr || width <= 0 || height <= 0) { + return; + } + const int line = TTF_FontLineSkip(font); + const int panelWidth = std::min(width - 16, std::min(600, width * 3 / 4)); + const int panelHeight = std::min(height - 16, line * 3 + 32); + if (panelWidth <= 0 || panelHeight <= 0) { + return; + } + if (!panel_ || panel_->w != panelWidth || panel_->h != panelHeight) { + clear_panel(); + panel_.reset(SDL_CreateRGBSurfaceWithFormat(0, panelWidth, panelHeight, 32, SDL_PIXELFORMAT_ARGB8888)); + if (panel_) { + panelRenderer_.reset(SDL_CreateSoftwareRenderer(panel_.get())); + } + } + if (!panelRenderer_) { + return; + } + auto *renderer = panelRenderer_.get(); + SDL_SetRenderDrawColor(renderer, 22, 31, 46, 245); + SDL_RenderClear(renderer); + label(renderer, font, screenshotNotification_.title(), {12, 8, panelWidth - 24, line}, false, false, true); + const auto detail = screenshotNotification_.saving ? "You can keep playing while the image is saved." : screenshotNotification_.detail; + label(renderer, font, detail, {12, line + 12, panelWidth - 24, line * 2}, true, false); + SDL_Rect bar {12, panelHeight - 12, panelWidth - 24, 4}; + SDL_SetRenderDrawColor(renderer, 50, 65, 86, 255); + SDL_RenderFillRect(renderer, &bar); + bar.w = bar.w * screenshotNotification_.percent / 100; + SDL_SetRenderDrawColor(renderer, screenshotNotification_.saving || screenshotNotification_.saved ? 130 : 255, screenshotNotification_.saving || screenshotNotification_.saved ? 180 : 110, screenshotNotification_.saving || screenshotNotification_.saved ? 255 : 110, 255); + SDL_RenderFillRect(renderer, &bar); + SDL_RenderPresent(renderer); + auto pixels = extract_overlay_bitmap(static_cast(panel_->pixels), static_cast(panel_->pitch) * panelHeight, panel_->pitch, panelWidth, panelHeight, false); + backend.set_color_overlay(panelWidth, panelHeight, std::move(pixels), VideoOverlayPlacement::bottom_center); + } + bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds, const SDL_Rect *clip) { const int size = std::min(bounds.w, bounds.h); if (size <= 0) { diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 9947948..81c77f1 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -9,6 +9,7 @@ #include "src/input/stream_input.h" #include "src/input/stream_menu.h" #include "src/input/ui_input.h" +#include "src/streaming/screenshot_status.h" #include "src/streaming/video_overlay.h" #include @@ -112,6 +113,11 @@ namespace streaming { * @return True while the radial menu, keyboard, log viewer or confirmation is visible. */ bool visible() const; + /** + * @brief Check whether local controls or a screenshot popup currently own the shared overlay. + * @return True while a menu or capture notification is visible. + */ + bool has_overlay() const; /** * @brief Release all held host input before transport shutdown. */ @@ -174,6 +180,14 @@ namespace streaming { * @param stats Current statistics toggle. */ void render_menu(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool stats); + /** + * @brief Draw a compact screenshot progress or completion popup without capturing host input. + * @param backend Shared GPU/software overlay owner. + * @param font Notification font. + * @param width Available video width. + * @param height Available video height. + */ + void render_screenshot_notification(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height); /** * @brief Draw the keyboard, optionally repainting only the old and new focus cells. * @param renderer Software panel renderer. @@ -247,6 +261,7 @@ namespace streaming { bool keyboardDockTriggerDown_ = false; ///< Edge latch for right-trigger keyboard docking. bool keyboardTop_ = false; ///< Dock the keyboard flush with the top edge. std::string screenshotDirectory_; ///< Configured screenshot folder, empty for the default. + ScreenshotNotification screenshotNotification_; ///< Progress and timed completion feedback independent of menu capture. app::ScreenshotSource screenshotSource_; ///< Stream identity retained with each capture. Uint32 logRefreshTick_ = 0; ///< Last live log refresh. std::map, std::shared_ptr> icons_; ///< Cached SVG rasters survive panel renderer replacement. diff --git a/tests/unit/streaming/screenshot_status_test.cpp b/tests/unit/streaming/screenshot_status_test.cpp new file mode 100644 index 0000000..73c45ff --- /dev/null +++ b/tests/unit/streaming/screenshot_status_test.cpp @@ -0,0 +1,77 @@ +/** + * @file tests/unit/streaming/screenshot_status_test.cpp + * @brief Verifies persistent save progress and timed screenshot completion feedback. + */ +// test includes +#include + +// standard includes +#include +#include + +// local includes +#include "src/streaming/screenshot_status.h" + +/** + * @brief Saving never times out, never reports premature completion, and avoids redraws for every disk row. + */ +TEST(ScreenshotNotificationTest, RetainsPendingCaptureAndThrottlesProgress) { + streaming::ScreenshotNotification notification; + EXPECT_FALSE(notification.visible); + EXPECT_FALSE(notification.update(20)); + notification.begin(); + EXPECT_TRUE(notification.visible); + EXPECT_TRUE(notification.saving); + EXPECT_EQ(notification.title(), "Saving screenshot... 0%"); + EXPECT_FALSE(notification.update(-1)); + EXPECT_FALSE(notification.update(4)); + EXPECT_TRUE(notification.update(7)); + EXPECT_EQ(notification.percent, 5); + EXPECT_FALSE(notification.update(9)); + EXPECT_FALSE(notification.update(0)); + EXPECT_TRUE(notification.update(1000)); + EXPECT_EQ(notification.percent, 95); + EXPECT_FALSE(notification.tick(std::numeric_limits::max())); + EXPECT_TRUE(notification.visible); +} + +/** + * @brief Successful completion displays its path for five seconds, including across tick wraparound. + */ +TEST(ScreenshotNotificationTest, DisplaysSuccessUntilDeadlineAcrossTickWrap) { + streaming::ScreenshotNotification notification; + notification.begin(); + const auto completedAt = std::numeric_limits::max() - 100U; + notification.finish(true, "Saved E:\\Shots\\capture.bmp", completedAt); + EXPECT_TRUE(notification.saved); + EXPECT_FALSE(notification.saving); + EXPECT_EQ(notification.percent, 100); + EXPECT_EQ(notification.title(), "Screenshot saved"); + EXPECT_EQ(notification.detail, "Saved E:\\Shots\\capture.bmp"); + EXPECT_FALSE(notification.update(5)); + EXPECT_FALSE(notification.tick(completedAt + 4999U)); + EXPECT_TRUE(notification.tick(completedAt + 5000U)); + EXPECT_FALSE(notification.visible); + EXPECT_FALSE(notification.tick(completedAt + 9000U)); +} + +/** + * @brief Failed writes retain their explanation longer and a new capture clears the old result and timer. + */ +TEST(ScreenshotNotificationTest, DisplaysFailureAndResetsForAnotherCapture) { + streaming::ScreenshotNotification notification; + notification.begin(); + EXPECT_TRUE(notification.update(52)); + notification.finish(false, "Screenshot failed: disk is full", 0); + EXPECT_FALSE(notification.saved); + EXPECT_EQ(notification.percent, 50); + EXPECT_EQ(notification.title(), "Screenshot failed"); + EXPECT_FALSE(notification.tick(7999)); + EXPECT_TRUE(notification.tick(8000)); + notification.begin(); + EXPECT_TRUE(notification.visible); + EXPECT_TRUE(notification.saving); + EXPECT_TRUE(notification.detail.empty()); + EXPECT_EQ(notification.percent, 0); + EXPECT_FALSE(notification.tick(16000)); +} From 2701e9e1677fdd71749603d7cb6ee3a197f974d6 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:43:09 -0500 Subject: [PATCH 11/22] fix(streaming): avoid overlay memory spikes during screenshot saves --- src/streaming/ffmpeg_stream_backend.cpp | 16 ++++- src/streaming/ffmpeg_stream_backend.h | 11 ++++ src/streaming/screenshot_status.cpp | 20 ++++++ src/streaming/screenshot_status.h | 26 ++++++++ src/streaming/stream_controls.cpp | 30 ++++++--- src/streaming/video_overlay.cpp | 17 ++++- src/streaming/video_overlay.h | 19 +++++- .../unit/streaming/screenshot_status_test.cpp | 36 +++++++++++ tests/unit/streaming/video_overlay_test.cpp | 64 +++++++++++++++++++ 9 files changed, 223 insertions(+), 16 deletions(-) diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index 85ea65a..b4e9894 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -361,6 +361,12 @@ namespace streaming { invalidate_overlay_texture(); } + void FfmpegStreamBackend::set_color_overlay_surface(int width, int height, const std::uint8_t *pixels, std::size_t capacity, int pitch, VideoOverlayPlacement placement, bool opaqueBackground) { + if (statisticsOverlay_.set_argb_surface(width, height, pixels, capacity, pitch, placement, opaqueBackground)) { + invalidate_overlay_texture(); + } + } + void FfmpegStreamBackend::invalidate_overlay_texture() { statisticsBitmapChanged_ = statisticsTextureChanged_ = true; statisticsTextureDamage_ = {0, 0, statisticsOverlay_.width(), statisticsOverlay_.height()}; @@ -519,10 +525,15 @@ namespace streaming { const std::array pitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; std::array output {static_cast(surface->pixels), nullptr, nullptr, nullptr}; const std::array outputPitch {surface->pitch, 0, 0, 0}; - if (sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height) { + const bool converted = sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height; + sws_freeContext(conversion); + // The BGR surface now owns the image. Return the YUV snapshot and scale + // buffers before disk I/O so opening a full-screen menu still fits in 64 MiB. + job.frame = {}; + if (converted) { job.progress.store(10, std::memory_order_relaxed); if (SDL_RWops *file = SDL_RWFromFile(path.c_str(), "wb"); file != nullptr) { - ScreenshotFileProgress writer {file, job.progress, 54 + static_cast((frame.width * 3 + 3) & ~3) * frame.height}; + ScreenshotFileProgress writer {file, job.progress, 54 + static_cast((surface->w * 3 + 3) & ~3) * surface->h}; SDL_RWops progressFile {}; progressFile.hidden.unknown.data1 = &writer; progressFile.seek = screenshot_file_seek; @@ -535,7 +546,6 @@ namespace streaming { } } } - sws_freeContext(conversion); } SDL_FreeSurface(surface); if (saved) { diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index bb070d6..f456348 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -111,6 +111,17 @@ namespace streaming { * @param placement Panel anchor shared by GPU and software paths. */ void set_color_overlay(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left); + /** + * @brief Replace the shared panel directly from SDL storage, reusing its full-screen color allocation. + * @param width Source width. + * @param height Source height. + * @param pixels Premultiplied B, G, R, A source bytes. + * @param capacity Accessible source byte count. + * @param pitch Source row stride. + * @param placement Shared GPU/software panel anchor. + * @param opaqueBackground Source was rendered onto an opaque panel. + */ + void set_color_overlay_surface(int width, int height, const std::uint8_t *pixels, std::size_t capacity, int pitch, VideoOverlayPlacement placement, bool opaqueBackground = false); /** * @brief Submit a small focus repaint while retaining the rest of the panel and its conversion caches. * @param region Updated rectangle in panel pixels. diff --git a/src/streaming/screenshot_status.cpp b/src/streaming/screenshot_status.cpp index 2faba31..6d0d52a 100644 --- a/src/streaming/screenshot_status.cpp +++ b/src/streaming/screenshot_status.cpp @@ -9,6 +9,14 @@ #include namespace streaming { + ScreenshotNotificationLayout layout_screenshot_notification(int width, int height, int lineHeight, int titleWidth, int detailWidth, bool saving) { + if (width <= 16 || height <= 16 || lineHeight <= 0) { + return {}; + } + const int textWidth = std::max(titleWidth, saving ? 0 : detailWidth); + return {std::min(width - 16, std::max(280, textWidth + 24)), std::min(height - 16, lineHeight * (saving ? 1 : 2) + (saving ? 28 : 32))}; + } + void ScreenshotNotification::begin() { *this = {}; visible = true; @@ -44,4 +52,16 @@ namespace streaming { return saving ? "Saving screenshot... " + std::to_string(percent) + "%" : saved ? "Screenshot saved" : "Screenshot failed"; } + + std::string_view ScreenshotNotification::body() const { + if (saving) { + return {}; + } + std::string_view text = detail; + const std::string_view prefix = saved ? "Saved " : "Screenshot failed: "; + if (text.substr(0, prefix.size()) == prefix) { + text.remove_prefix(prefix.size()); + } + return text; + } } // namespace streaming diff --git a/src/streaming/screenshot_status.h b/src/streaming/screenshot_status.h index eb17d71..9061b74 100644 --- a/src/streaming/screenshot_status.h +++ b/src/streaming/screenshot_status.h @@ -7,9 +7,30 @@ // standard includes #include #include +#include namespace streaming { + /** + * @brief Content-sized screenshot popup constrained to the video safe area. + */ + struct ScreenshotNotificationLayout { + int width = 0; ///< Panel width including text padding. + int height = 0; ///< One line while saving, two lines on completion. + }; + + /** + * @brief Fit a notification to measured text, reserving a second line only for a completed result. + * @param width Available video width. + * @param height Available video height. + * @param lineHeight Font line height. + * @param titleWidth Measured single-line title width. + * @param detailWidth Measured path or error width, ignored while saving. + * @param saving True while a capture is pending. + * @return Bounded panel dimensions, or an empty layout for an unusable viewport or font. + */ + ScreenshotNotificationLayout layout_screenshot_notification(int width, int height, int lineHeight, int titleWidth, int detailWidth, bool saving); + /** * @brief Keep saving feedback visible until completion, then dismiss it after a bounded interval. */ @@ -43,6 +64,11 @@ namespace streaming { * @return Human-readable notification title. */ std::string title() const; + /** + * @brief Return the saved path or error without repeating the popup title. + * @return Single-line detail, empty while saving. + */ + std::string_view body() const; bool visible = false; ///< True while saving or before the completion timer expires. bool saving = false; ///< True until a worker publishes its result. diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index a6f29ee..250d775 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -969,8 +969,17 @@ namespace streaming { return; } const int line = TTF_FontLineSkip(font); - const int panelWidth = std::min(width - 16, std::min(600, width * 3 / 4)); - const int panelHeight = std::min(height - 16, line * 3 + 32); + const auto title = screenshotNotification_.title(); + const std::string detail(screenshotNotification_.body()); + int titleWidth = 0; + int detailWidth = 0; + TTF_SizeUTF8(font, title.c_str(), &titleWidth, nullptr); + if (!detail.empty()) { + TTF_SizeUTF8(font, detail.c_str(), &detailWidth, nullptr); + } + const auto layout = layout_screenshot_notification(width, height, line, titleWidth, detailWidth, screenshotNotification_.saving); + const int panelWidth = layout.width; + const int panelHeight = layout.height; if (panelWidth <= 0 || panelHeight <= 0) { return; } @@ -985,11 +994,14 @@ namespace streaming { return; } auto *renderer = panelRenderer_.get(); - SDL_SetRenderDrawColor(renderer, 22, 31, 46, 245); + SDL_SetRenderDrawColor(renderer, 22, 31, 46, 170); SDL_RenderClear(renderer); - label(renderer, font, screenshotNotification_.title(), {12, 8, panelWidth - 24, line}, false, false, true); - const auto detail = screenshotNotification_.saving ? "You can keep playing while the image is saved." : screenshotNotification_.detail; - label(renderer, font, detail, {12, line + 12, panelWidth - 24, line * 2}, true, false); + label(renderer, font, title, {12, 8, panelWidth - 24, line}, false, false, true); + if (!detail.empty()) { + // SDL_ttf's legacy wrapper cannot reliably split an uninterrupted Xbox + // path. Fit one complete line instead of clipping or overlapping it. + label(renderer, font, detail, {12, line + 12, panelWidth - 24, line}, false, false, true); + } SDL_Rect bar {12, panelHeight - 12, panelWidth - 24, 4}; SDL_SetRenderDrawColor(renderer, 50, 65, 86, 255); SDL_RenderFillRect(renderer, &bar); @@ -997,8 +1009,7 @@ namespace streaming { SDL_SetRenderDrawColor(renderer, screenshotNotification_.saving || screenshotNotification_.saved ? 130 : 255, screenshotNotification_.saving || screenshotNotification_.saved ? 180 : 110, screenshotNotification_.saving || screenshotNotification_.saved ? 255 : 110, 255); SDL_RenderFillRect(renderer, &bar); SDL_RenderPresent(renderer); - auto pixels = extract_overlay_bitmap(static_cast(panel_->pixels), static_cast(panel_->pitch) * panelHeight, panel_->pitch, panelWidth, panelHeight, false); - backend.set_color_overlay(panelWidth, panelHeight, std::move(pixels), VideoOverlayPlacement::bottom_center); + backend.set_color_overlay_surface(panelWidth, panelHeight, static_cast(panel_->pixels), static_cast(panel_->pitch) * panelHeight, panel_->pitch, VideoOverlayPlacement::bottom_center); } bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds, const SDL_Rect *clip) { @@ -1331,8 +1342,7 @@ namespace streaming { backend.update_color_overlay(area, pixels); } } else { - auto pixels = extract_overlay_bitmap(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, keyboard); - backend.set_color_overlay(width, height, std::move(pixels), keyboard ? (keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom) : VideoOverlayPlacement::full_screen); + backend.set_color_overlay_surface(width, height, static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, keyboard ? (keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom) : VideoOverlayPlacement::full_screen, keyboard); } panelContent_ = content; panelSelection_ = keyboard ? menu_.keyIndex : static_cast(menu_.selected); diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index 4f8a1fa..4b46891 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -104,11 +104,11 @@ namespace { } // namespace namespace streaming { - std::vector extract_overlay_bitmap(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, bool opaqueBackground) { + std::vector extract_overlay_bitmap(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, bool opaqueBackground, std::vector bitmap) { if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048) { return {}; } - std::vector bitmap(static_cast(width) * height * 4); + bitmap.resize(static_cast(width) * height * 4); for (int row = 0; row < height; ++row) { for (int column = 0; column < width; ++column) { const auto *pixel = pixels + static_cast(row) * pitch + column * 4; @@ -128,6 +128,7 @@ namespace streaming { continue; } if (opacity == 0) { + bitmap[index] = bitmap[index + 1] = bitmap[index + 2] = 0; continue; } for (int component = 0; component < 3; ++component) { @@ -315,6 +316,18 @@ namespace streaming { return true; } + bool VideoOverlay::set_argb_surface(int width, int height, const std::uint8_t *pixels, std::size_t capacity, int pitch, VideoOverlayPlacement placement, bool opaqueBackground) { + if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048) { + return false; + } + if (width != width_ || height != height_) { + set_bitmap(0, 0, {}); + } + auto bitmap = extract_overlay_bitmap(pixels, capacity, pitch, width, height, opaqueBackground, std::move(argb_)); + set_argb_bitmap(width, height, std::move(bitmap), placement); + return true; + } + std::uint32_t VideoOverlay::color_at(std::size_t index) const { if (!argb_.empty()) { const auto *pixel = argb_.data() + index * 4; diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index 2ae1038..1c7bc85 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -15,9 +15,10 @@ namespace streaming { * @param width Surface width, at most 2048. * @param height Surface height, at most 2048. * @param opaqueBackground Source was drawn onto an opaque panel; preserve colors and correct SDL's alpha rounding. + * @param bitmap Optional reusable destination storage, avoiding a second full-screen allocation on repaint. * @return Unpremultiplied B, G, R, A bytes, or empty storage for invalid dimensions. */ - std::vector extract_overlay_bitmap(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, bool opaqueBackground = false); + std::vector extract_overlay_bitmap(const std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, bool opaqueBackground = false, std::vector bitmap = {}); /** * @brief Source crop and destination for a centered overlay label. @@ -94,6 +95,22 @@ namespace streaming { * @param placement Anchor inside the video rectangle. */ void set_argb_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left); + /** + * @brief Copy a premultiplied SDL panel using the existing color buffer for same-size repaints. + * + * Release obsolete geometry and conversion caches before allocating a new panel. This avoids + * overlapping full-screen copies while a background screenshot owns its capture buffers. + * + * @param width Source width, at most 2048. + * @param height Source height, at most 2048. + * @param pixels Premultiplied B, G, R, A source bytes, independent of this overlay's storage. + * @param capacity Accessible source byte count. + * @param pitch Source row stride. + * @param placement Panel anchor inside the video rectangle. + * @param opaqueBackground Source was rendered onto an opaque panel. + * @return False for invalid source storage, preserving the previous overlay. + */ + bool set_argb_surface(int width, int height, const std::uint8_t *pixels, std::size_t capacity, int pitch, VideoOverlayPlacement placement, bool opaqueBackground = false); /** * @brief Replace a bounded part of an existing colored panel without scanning or copying unchanged pixels. * @param region Rectangle in panel pixels; must be wholly inside the current panel. diff --git a/tests/unit/streaming/screenshot_status_test.cpp b/tests/unit/streaming/screenshot_status_test.cpp index 73c45ff..77a1f35 100644 --- a/tests/unit/streaming/screenshot_status_test.cpp +++ b/tests/unit/streaming/screenshot_status_test.cpp @@ -75,3 +75,39 @@ TEST(ScreenshotNotificationTest, DisplaysFailureAndResetsForAnotherCapture) { EXPECT_EQ(notification.percent, 0); EXPECT_FALSE(notification.tick(16000)); } + +/** + * @brief A pending popup needs one line; completion expands to fit the full path within the video safe area. + */ +TEST(ScreenshotNotificationTest, SizesPopupForContentAndFitsLongPaths) { + const auto saving = streaming::layout_screenshot_notification(1280, 720, 24, 260, 1000, true); + EXPECT_EQ(saving.width, 284); + EXPECT_EQ(saving.height, 52); + const auto saved = streaming::layout_screenshot_notification(1280, 720, 24, 200, 900, false); + EXPECT_EQ(saved.width, 924); + EXPECT_EQ(saved.height, 80); + const auto longPath = streaming::layout_screenshot_notification(640, 480, 18, 180, 2000, false); + EXPECT_EQ(longPath.width, 624); + EXPECT_EQ(longPath.height, 68); + const auto tiny = streaming::layout_screenshot_notification(100, 50, 18, 180, 2000, false); + EXPECT_EQ(tiny.width, 84); + EXPECT_EQ(tiny.height, 34); + EXPECT_EQ(streaming::layout_screenshot_notification(16, 50, 18, 10, 10, false).width, 0); + EXPECT_EQ(streaming::layout_screenshot_notification(50, 16, 18, 10, 10, false).height, 0); + EXPECT_EQ(streaming::layout_screenshot_notification(50, 50, 0, 10, 10, false).width, 0); +} + +/** + * @brief Completion shows the path or error once, with no explanatory body while saving. + */ +TEST(ScreenshotNotificationTest, RemovesRedundantResultPrefixes) { + streaming::ScreenshotNotification notification; + notification.begin(); + EXPECT_TRUE(notification.body().empty()); + notification.finish(true, "Saved E:\\UDATA\\FFFF0002\\screenshots\\capture.bmp", 0); + EXPECT_EQ(notification.body(), "E:\\UDATA\\FFFF0002\\screenshots\\capture.bmp"); + notification.finish(false, "Screenshot failed: Disk full", 0); + EXPECT_EQ(notification.body(), "Disk full"); + notification.finish(false, "Cannot start capture", 0); + EXPECT_EQ(notification.body(), "Cannot start capture"); +} diff --git a/tests/unit/streaming/video_overlay_test.cpp b/tests/unit/streaming/video_overlay_test.cpp index ffca996..d5efe23 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -16,6 +16,70 @@ // local includes #include "src/streaming/video_overlay.h" +/** + * @brief Full-screen dialog repaints reuse their color allocation while screenshots own additional buffers. + */ +TEST(VideoOverlayTest, ReusesFullScreenSurfaceStorageAcrossDialogRepaints) { + constexpr int width = 1280; + constexpr int height = 720; + std::vector source(width * height * 4, 150); + streaming::VideoOverlay overlay; + ASSERT_TRUE(overlay.set_argb_surface(width, height, source.data(), source.size(), width * 4, streaming::VideoOverlayPlacement::full_screen)); + const auto *allocation = overlay.argb_pixels().data(); + source[0] = 15; + source[1] = 30; + source[2] = 60; + source[3] = 0; + ASSERT_TRUE(overlay.set_argb_surface(width, height, source.data(), source.size(), width * 4, streaming::VideoOverlayPlacement::full_screen)); + EXPECT_EQ(overlay.argb_pixels().data(), allocation); + EXPECT_EQ(overlay.argb_pixels(), streaming::extract_overlay_bitmap(source.data(), source.size(), width * 4, width, height)); + EXPECT_EQ(overlay.argb_pixels()[0], 0); + ASSERT_TRUE(overlay.set_argb_surface(8, 8, source.data(), source.size(), width * 4, streaming::VideoOverlayPlacement::bottom_center, true)); + EXPECT_LT(overlay.argb_pixels().capacity(), static_cast(width * height)); + EXPECT_EQ(overlay.argb_pixels()[3], 255); + const auto expected = overlay.argb_pixels(); + EXPECT_FALSE(overlay.set_argb_surface(8, 8, nullptr, source.size(), width * 4, streaming::VideoOverlayPlacement::top_left)); + EXPECT_FALSE(overlay.set_argb_surface(2049, 1, source.data(), source.size(), 2049 * 4, streaming::VideoOverlayPlacement::top_left)); + EXPECT_FALSE(overlay.set_argb_surface(1, 2049, source.data(), source.size(), 4, streaming::VideoOverlayPlacement::top_left)); + EXPECT_FALSE(overlay.set_argb_surface(8, 8, source.data(), 1, width * 4, streaming::VideoOverlayPlacement::top_left)); + EXPECT_EQ(overlay.argb_pixels(), expected); + EXPECT_EQ(overlay.width(), 8); +} + +/** + * @brief Reused SDL surfaces refresh GPU and software caches when their colors or opacity change. + */ +TEST(VideoOverlayTest, RefreshesPresentationCachesAfterReusingSurfaceStorage) { + streaming::VideoOverlay overlay; + for (const std::uint8_t opacity : {255, 128, 0, 255}) { + std::vector source(8 * 8 * 4); + for (std::size_t index = 0; index < source.size(); index += 4) { + source[index] = 20; + source[index + 1] = 40; + source[index + 2] = 60; + source[index + 3] = opacity; + } + ASSERT_TRUE(overlay.set_argb_surface(8, 8, source.data(), source.size(), 32, streaming::VideoOverlayPlacement::top_left)); + streaming::VideoOverlay fresh; + fresh.set_argb_bitmap(8, 8, streaming::extract_overlay_bitmap(source.data(), source.size(), 32, 8, 8)); + for (const auto matrix : {streaming::VideoColorMatrix::bt601, streaming::VideoColorMatrix::bt709}) { + std::vector actual(32 * 32 * 2, 100); + auto expected = actual; + ASSERT_TRUE(overlay.draw_yuy2(actual.data(), actual.size(), 64, 32, 32, {0, 0, 32, 32}, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 64, 32, 32, {0, 0, 32, 32}, matrix)); + EXPECT_EQ(actual, expected); + } + for (const int bpp : {15, 16, 32}) { + const int bytes = bpp == 32 ? 4 : 2; + std::vector actual(32 * 32 * bytes, 100); + auto expected = actual; + ASSERT_TRUE(overlay.draw_rgb(actual.data(), actual.size(), 32 * bytes, 32, 32, bpp, {0, 0, 32, 32})); + ASSERT_TRUE(fresh.draw_rgb(expected.data(), expected.size(), 32 * bytes, 32, 32, bpp, {0, 0, 32, 32})); + EXPECT_EQ(actual, expected); + } + } +} + /** * @brief Opaque OSK colors are not brightened or made translucent by legacy SDL alpha rounding. */ From 5b348f5983992e606b5eb8290e52aa75779b1db4 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:05:21 -0500 Subject: [PATCH 12/22] docs(streaming): add stream controls and capture screenshots --- README.md | 10 ++++++++-- docs/images/screenshots/12-stream-controls.png | Bin 0 -> 40022 bytes docs/images/screenshots/13-stream-keyboard.png | Bin 0 -> 34390 bytes .../screenshots/14-screenshot-notification.png | Bin 0 -> 21794 bytes 4 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 docs/images/screenshots/12-stream-controls.png create mode 100644 docs/images/screenshots/13-stream-keyboard.png create mode 100644 docs/images/screenshots/14-screenshot-notification.png diff --git a/README.md b/README.md index deb80d6..3f1d43a 100644 --- a/README.md +++ b/README.md @@ -99,10 +99,12 @@ for readability. Guide and Share use Xbox Series-style symbols. | Left | Keyboard | Mouse mode | View logs | Quit stream | | Right | Guide | Share | Screenshot | Enable / disable stats | -**Quit stream** opens a confirmation dialog: **A / Enter** disconnects and **B / Esc** cancels. +![Stream controls](./docs/images/screenshots/12-stream-controls.png) + +**Exit** opens a confirmation dialog: **A / Enter** disconnects, **X / Q** quits the host app and **B / Esc** cancels. The icons sit on their buttons; arrow navigation is not required. The client displays **Stopping stream...**, then a stream summary. Release the buttons, then press a button to return to the library. -The host application keeps running. +The host application keeps running when you choose Disconnect. The on-screen keyboard uses an Xbox One-style layout on a solid dark panel: a number row, QWERTY keys, a wide space bar, tall side controls, and an outlined focus key. Use **D-pad / left stick** @@ -124,6 +126,8 @@ to toggle Caps, **F2** to move the panel, **Space / Backspace** to send those ke In the numeric address/port keypad, **Space** types the focused key and **Enter** accepts the field; **PgUp / PgDn** change the address section. +![On-screen keyboard](./docs/images/screenshots/13-stream-keyboard.png) + Stream logs appear in a live side panel with severity colors and a scrollbar. Scroll upward to read older entries; return to the bottom to follow new entries. **Exit** opens a three-button dialog: **B / Esc** cancels, **A / Enter** disconnects while leaving the app running on the PC, and **X / Q** @@ -162,6 +166,8 @@ remain visible for eight seconds. Neither popup captures controller, mouse or ke Only one capture saves at a time; completion and errors are written to the log. The wheels close automatically after screenshot, Guide, Share, statistics and mouse-mode actions. +![Screenshot notification](./docs/images/screenshots/14-screenshot-notification.png) + Mouse mode uses the invoking controller's **left stick** for movement and **A / B** for left / right clicks. Stick-driven mouse movement is reported at up to 125 Hz, while button presses and releases are sent immediately. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; diff --git a/docs/images/screenshots/12-stream-controls.png b/docs/images/screenshots/12-stream-controls.png new file mode 100644 index 0000000000000000000000000000000000000000..9bdabad193759a0646a20b78bde0352834aa8582 GIT binary patch literal 40022 zcmeFZXH-*L`!5=r2q>s1Ak~JbbWu7)Wh*KIDk>l)C@KO%g7lgoqDZwNDoq7MX#uIy z5~(86dkqkzLkJ}V((X)9w|l?uhkMTb-!sM?<9<<+wbq2Ky0<%K{X zo6ns7>mmfg1^#4j-pCIAJ0^9O4FZXiIP=%>OJ2P*1D?+>xt9}Yl<~5r;%RMUzi*R@ zU1Pk8>zl2D`@_S{?QXOykE6_$_Jr(tz!73zD}Me`RruE2J)t6yY83CrJ=?fW9#alC zGILBo)8llGb_{fM7<#&QWOdnfTtC`T;^B@Y(P@LrbG`X1ND2Y!_+he-;oM#H9=_Iv zp)aG6v}qV){ZGcXetPU?8_2}3FMeKwvK6pxg8Y5o`qwg8RTSHsKOSfO$=SU@39|I( zs^IHHNFQVa>Ytk-X@WK!$A0eo_bSGy&Hu4e7nDu)Kh}gXIJ+Uu|Kb42euqN@@^8kB zQBIKm+=?gre~Z^q4hCX;`@WU7~ek1JU-EHE;uDIfHdHVYl0O`yy6k0 zY!-iqWY0iS*6IDt`C=P`K`wMI#ObJhY;RY??W9ZyqK)E79bK@dSj;g7rx}B^E*lx_ zPXUgnGM+VQEAE@B?|{t~)gJGOhgy`3zpYKYTGZX>EAiCLfRR~tvtz5xViZf@f;M8< zbvZE_OAV38E2rCE=_wv+8(A_&H9-Hpde;pGl8X1-eZg1KM~-bjc1-H`GCw7_*w6TO z8Fv3KE4LrZ(l1smOb)hYSFRSIioF6NZ#D1 zGSicOx6(S;Nqi8F#W!g~;a}dt8RS|;Q*^*&$4AQ2q(!^gd+&$h$f-U;V@Rr55&2m? zx@48%*0-;`4?fgGXm~9l0LvSd@_aGJV9@>l{EOYK&mJ@;rYdRd5kg3;|fD)ti6Mg3JTw&J(G1MQ5C;8(#kpuFc ze#^^h*Xr8_z10R>8yX4)cDk5rpB#=ChPmra(tW9GYEs`{i5Wcjq)VK+uRdw|a=r`a z_l)+5TgvyYx}Dtom=+=UUJZum9w$ygB^lkhQ&6c`c4+W~0KtD|l9-4#d-Y-_zkbm0 ziZCqIqUG$^_jut@N;BSA%Jth$dHN=4PW9JO1txV-+Qf5I>zu0;%&I=iiT`#dhH+uO z8gBRYBOfn)=p~Q{ZM}ML+GxZL@WO_Ira4Qy`uIwv%3A`ubI;wHe98g@b%hKP+{V0n zu5*j~w_>uEp9wq^fl{kfa z7%K}ML`H^b)7}sTA}KH{weJBXNSNnhMueA)s*nhxaka;jJ3(vD9M2hR$F|wf{-bmR zqFGrc#Wx^_F61&bQ=~GuMPYHvgd3xx+9063@m8=!a#d4yi${s740bVq%2$!n)&E?+ zx!G0gzwrun#`I=)k%eK6{LS$sQb{o3+KYo&VXIYRc$u{eP2R;$yvV|9JVZ7tLJQ`1 z^RDX;akrU+(1fQx?)YWgQPPu{gaoY_>ybwFx#G{uF!IA`+dy?f7h`cT|8>U5+6B+# z4WV;UzO5zAC+9@rc|r<8-$XrY`g$|B4PA?|G^aNyEkydh793LN*QSechxRcVpe*?N z1M>_9Mk>=?#NnC`R3i&$v$_up&Y~!BN^j;HJqKgAdP;isng@GI`uH)57IT|Sw-4@w z+t)n#o-|jg7`fS~)>c@@pm>~o?{c+osq|bwZ73;L7P%H~ev*(OG|<}8LiBKKp`|j& z8)h9n>0IUAtgAqBOb9uKQajrgnGj z3vatexqE6|+H;QGc5uZ6h_{OK&9tSG@y#wx0^C-}MW-hvsw;N)uJ9n_nnM$Z`{1zv z76@H3@vU-38bXO5BN@H_RazwV(U&)_5U;hX@*&Ke`!%k$ruUr5{nRIKJ7-Y82{ZG0 zw6ap6EUmV)r0QW{n?;o&FQR3w9+Nvy8d$-_?8QbjaSO>fIA27C_2)})74;;(^=31Y z%8N8{O2z0_!GtylV5;bFdJ8KS{2j1xGxCl6(cjbNpJ6k147pv;e20G*<~*6#6l3Xc z>tXB{kgvz3o|U+rZ%OKKMj3zL+(tI%#of)p<7=VDFb3iPWd-Ni<0bZok^YL#aMFPZ zDgGoH-I!6_2@U9-^}s%j`67ykCeBLD1)~IoAwQ_Tk<|WiK zp>iT=sUoR_53bg)xnKUgAA6eTYh9iz8tieGO}#!2Z<9SpQ+ck=b7gqeRE)Nk8cp#3 z@P}%DMP~-$l9RZAL45Uu!5W`&= zo`uMM%rb#w;aeZy+NENL6shm`t1X+vXwABxwwx@~N|UR~u9|irIBYdw;1PTVtA|-=mNFO)sw5b>Db65KHxE_?OA^GE}#w za)-fbt4&C2$3=-5#+VcnWG=bueT^8#rO8$C+@YmP zH(&3CX{cs*VNx>GzU;Gatzjt%VBZQyc!! z|F8T8saMQC?=5Vte6zRDWo&#t9Z|8RMVDp-vcc)j(TcZW22h$Dzg48{-1DS(iiG!x z4{*zs1B1N$4UsaMD;2T$P`SCy)`8j`Np9FOE3@G)N2EQNxpE zjZDAEra)9VvO<~5u04*IDc@!xqq|aayRJ3022<2^t+waCIO!j0D^K<>Gyfx<{acZL zq_T++vHwU7(I8{|KbPX$wIku?92zWhUc|2?(; z<$?B7)gOyIv(Jb&j+|J!Q$uNBw-JYRoT z692T{|92{`|9NHoooxIA6#jc@`rlpke}UEiAC>+96y~4*L#V`vES|+M=xc?HpC}~j z=XkprLu2*>n2vz+@y~qF&6r*9EWk`@YM1n9m{l~Z=+>A)GOk9_aP;*K#nE1*o_sAG z&RB0Tz-DO#m~jr{XJL;yy9WP|%36yRP_$ARv=p7^-M^E!VheBS0!A!yBde8=Nc6ZT z@H+u-@{%P~l8}o6>&+9DL8JL=zhm=G_z!>PGP~D%D6M@whwuEZC?_*ZH!+uC@iSG3 zJk|Rs2W-M@s&qLq!t(@e!L3`EW&H;<{gup4RmMx!MN|6z8{Je}uqf~yZ*+Sa&-z2t zi7SVhTR&uWVIY&y#gDK3Eb5kTBS?OT?{aKI{v69nL)2WL4KdXu+ z-&ffn!_#9nR8T8ttzKrcsy}4>isHHTtwG?x`leX3J^`&y=OUjp9tHcYGLd$<1N!6H zA!X)A;DYFM`pL8^u=E%+2)7$B#EsL=1~EUz53DitSlB6$h z`^{oc%%wBmp<@iA&s%Lc-UzPQSw0nDt;KwJD$(VP?h)qGz`%|*EH!=FXDoV$uFSOU z{8niFQ*Ha~)mkcOilIa&63x%hS@#0Drelz|q*vQOlZPeP%LCBZr=nnC6-k3Yu*0%ltmA4o;Oo#c7_)Pev#MF33YU+U7BdIoj12Lex)hD6F%AJ zXfmkoI{$dN3kHMlX#P!IJJjUqTFWm266mikQf^@p6LT+DejHmHh6%2#Ci)6&-5DeP>)ajG<)`@SGEw9`}D~H+7{c?9dCP zJ~j?xByx=VjDKp=%MK(Ko8M(^p%UHodyO8butV)84NBbS#jzM<-et$g`qxsjT?k4I zP#;ia~^6ibBr0t}LrcU*lQi)ffeCs!LtCx+gG-hL}gzCcbB*Rc={ z{Fa zJEe@1okTZb*Ses~(Q+dX@uVGuM0Jr|{JyMeHOD)opzsSvR#axn$buQ@nrif9vmf3|<|3uBVqIf=6 z^A3}l`aJnwGnGm5CTzRuS^o{>`APxBtDlHEKjcpRmqM>}fTCIJjGc$6TyFqds|{6DctJ4JAtsfL5ml^A(g&T?npk<}0QNP<}5 ze#{PmN8GA~jb57Zt~LODHrSA4IbU+Hz!|+lPcd2eo{Nut)^Tk->h{9!@i^DHv<_U= zZ2#f|)z8PYvp>5tH)M)J31R{}Xn@kUL<}G2mbm0)97msOr_=Zd8uM5V#iIE=?B{~m zx~XV}=oCy-xoz}Af|@ULhTE z^yuKgfG@#cRMbj=Oy`z#D4h=yfEJT_9fjq^1)`#lSgR)(jf*qwmhI)**?RW4a>~@` z$6$7Gl33x4;n0>LQ|tI`VFL*K>0fjoBEZsi8@!N*XM8M-C{Mbb_MyZ2yt()Sp zH2ff#&(f=<`bRgs*vhRgB{(VqARM1?NNO81U<%qmZed#QK?sGo04DNYF7(>S_LKO_ z%Zis@1_m{fQ$1ba6z~rv(c|hWDb|_z80oV@E&%`hAyeAfgKYfK{$roVE%=7KPCwxg zwBg@`fH#x7gTI$gi=K-DH~SERV$RCSvbM=Tua*9k2bV7eO^J)(A`Vln0Ub zbU{e?q{WPH1w91g+G{#mx)zr+I*CmzdvQXA@*{;d03Dw4(`3UGHV_&BH6)^_wHvJt7=7ED1u&KT;e|ndRwKslZt(ntRr1p~n z0A_+VG!t0e;n5ja`~v2cKvM#R_?i!qu}@{NXcs~|IN>J?5?YYkdQQ^1)3NbT+pj%# zg@kXm!9B;@*OB%dYPl&u+5kkb8eKACf2E@{X40ck19}FYp$)+LClmbQko#}P?#_SV zZi3|{m%ZtuzS4NX!ZjvSP)c;go!o{3F{iu>ene`RU|-m0!Tm-KWTFE$=k8_}R1~mj zA|U63MH}XaE48ko@CTQ^2eQ3{Ok;x9Vy+s4ND5tDS|nYc1BO?{n-B5=X2L(w&30^? zgC+=Bc5|hx5^u|Mdd)sR0PYJ|_dL&d8da+LBZ9PtFxr(m`D`6xn*%veVx#+OC4Kv` zZN~6gi;VN>AnpR=*7|^~GvU`HKjsRH$@)yIIm@q0KJVYk#4$je1LQpWR;JOJF`@~u zg&6t5{zJOuB@Ytm5z)7w{#^S42WnFY!UnP*yN32-JjDxe2N>&Cv*TB ztk<&xK+b=m!C=6MrfmH!>~#9m_xwx=!u04q2Nm~Z^3u0~FdhMki`c_2wiLb%R=(L1rTFE_2bBC0~T;9 zfH7Bx3)6jE!0Ml1RB5d0vFQ!R9=ZIGE)RJQ$4HCZ0SWmUdggRXNGKR9}VmVE{k3KhyJk(`jEf6;f^v}SYmReZ-Z*)k9`}~NtC7bn*KG_vn~KA zTaneNT|2jcr6T~vc(`2|yU>%hJeCNsHM8pc2Lm@!GG#Xa_c7%`GCx$~u5PnY+@(qE zoJ)El5|WK8@RzKk61X_&cWt(Q9Q9ITcy>lWy0rZ@vcZ$atS5IMCz(&eK!$pWA1iKl zpT^Eaw(*6SeIeQF;trmu4?NioFH>fEIPa>#j=$E~dvjvx2X@a)VU`HM9aub7defeD z^`76o1~w#FKsPac>ngoT{RFyQs`4d(9Ai!mqG zcZYlrJeiy}D~#e%VluHVxeq#PcZd8&=~8WI7$%LzgSRt)CPzdIP%?Kj7w+OjPW}~d z7wc5;d8+oqJ=%yOTUOHM^U`}?U24BhC)qY?5la7lN{o`ZgY^_I^2|w{@i#&IXP;l< zVoJnPQS41xjY|H6`rg^TLPFZ*^brf&n(*D&i_;_vK^AYj=@-Ksz7lJt7+4O0@9|nI z^@;s7Q`9?SbG`dn&`qopG+(8bbwfT#6v-wpTa%`gdM5U3bE_Yz1{Lo*ME(? zDSNaoAU*C2F!%hguum6;<3K`m{6j~&DIvE>cXqC$NoS@1s$=HKb*{zUoSm!>)T%h$ zhKt6KN5TY+wOh_)??aL-8dyTSK))Cnnqt?Kz&5e#l2Y-PGug=-^ix<_Aa|AmTO>vn z$*^tIrOfOX*8TC^#M+NoO%AybcLxtP1Ep6jzt&9lQs#IfQnypZsWaM;^aa}y zONn4(VQ)g&O8KfXp`0?eI-ZClPj28BCoj}1qRn#lBq-41kJz`O*f>I8d>(yju-`bL z)NQhrdG9nD6IogCaI;wFC_%)aCTgV?N+(8Y&3!F*U#OEYj#F2@A}J6Rr2_+M;Z$EC z@nJbdkQ#`H{boD8cyAX>UEp!pot2_fP*?5I?^3I-aHr?f&Erkxxwkfp9{B)@5%2MM z+d8aEX{gz!8+9KpB>5~&Tzj@Nifn*#bmwSqewixeBs8?8JzCbZri-{A@yuX@XAqLv z%jF0p?jBa}yCEkItF?Q8F9CVE5moIRQ-6wn?I6%?Tfr~5{%JGkVyI9U?tla#UZ?u`L6eP$^4UU%olL)PqozphI8FVcHox63BOJor zjE`z}v5UK{NgtI!7DS>G{YgDF?0!{S^{ezl@HL=MT4vew-AxdOr6kT}}>Gt}S_Yz`ZNQS9f*v85#aQ z=Ag>0?;%>~(uHposbS08%QyRu$9vYqzpWy^fEUZ~&z*Jfna(V{6&Ad^R>uX237~z_ z(hnE8;I)=D-@VjDtijlp-t-@RqAx^0MckQo15cSNffu`iE(wz2J0qr1fSY1rQRZUI zeK)slcE;+%_jxzY3t8NGlJ&p~AE9c%0n=iE|$NC+=@)}Tc}%-83zSILJ_pQuOICSzOG>>hDRW4 z$wXd4xUBU1m4G&_?vph}C|5Y<@>iz2i_gb|y4@a8`hYd@BY$9L_p98jwr`7Yro5HR zyZOK_7}B4r_5sWy{fL1{de*>FoQ@_5AVE3jI^?i@$ZO?jq-CgLR`FEYeqjTGfE|4F zy(Mrx7dvXOI;{|nE6;g~btQcnB5yn|w?$uXhmKWCN!61+I7VkK%6MlAL&3VVh3BBT+h7=@*AY!CJZ0|6mZ0H!i>h`Ewxp75ti3~5NkVi0g9LLRz$c4}R3f+Mc7$tz zh5dXg;nR&1}E+@BD}BpoPSlD&+YD{ z7ikHnE`52#`B_-?h2|dRCVWjqnA$5b#wGa#A5E+I?60A1+(A5u_-=SUGXO%>s43DC z&ox}fI1@&4PXo?^!z8J`BYBat(pVR{%TzUbmF1F36?agWLMsglRW{aKFbOW;p5Y|zO&p8~B z#w{kzpZlpJ{3sA32)pT~C;{~$)KY>|oRUW4gjtbU9Q1SDib2@6W1Alt>K{+VCstk% zgAN#~4P08nF40}%wiaK0cr3L|7l&lfdf0d8-Q=B5O$-1dFuY+2dm1NCqNcyG!W_Em zNB&bi0i7fB+Y?o%21+~~xr&Svb%7`+Kc3}7yiui#38N19gw_b0({=)bOCmR!uxQUQ zddFkep_{yjeT~oJG7g_3GefSBflOIQQ%^xr1&`qVOWE94P@4wdZCDn+*E#E^@^`7Iq(<`*ZU=1Hmqww2!9(A2GellTB#;UjJ1GPVP zniher1Ni9O$TzZp(rvLtBNsH|4wkr*=Z7aNwO#0G6)GnaJ$mi+rTpE7d6{w2Y#~TM zcOxaDeo=MRjo39kli8Jx)0gz#H@E48UHUhLpzSeR8zo~0FGf=Eyjvy{xcG#&uOX=B zn*N^(#n~c*+a5mTQ~bIema3P;gRts>M=Jp<#W}j!D}w~c+y|~|^g~4I=r@J)&$q!B zRfUF@z6_ZKv>B=yu5jK^u>^xtAcX3_yJ2a)+hRX|?pdUdf}f2~>zna6$Afs03059G z(g9avzkjU&NIfgIt5$`}{-6nJugh1HwPyJ6%|PZf1yRVwa{BhdXx-v?lcHwr<$X)) zWe>$yE2Zeoqis42>f+=;*O_Pgr5``+d|)fA;9b4LG&yQV^-+=QIc{vpMKZebaq@z~ zGUsH&pIxreDm;z@lNYbp<0L%eY5R!NRidr=@f#6kJ@BJ|H9XpM>Y`DFZSFIxYWo*> z`5v;?-O|mm{G|?Z*e^e-+Q>&tkwovYS7z$;5&*q@)-~Ii$=Mk8=q~INuj!-yvW3o&|l&KAWZE zc@ArJn2 zlgEd*^zM)PWJKV2xc1<2ZQ@hB$rqyOBpKDZHjRcG>@)qowhlhx-sOktT@ zNMT<GdV9t7pg zp=(NPH8Aw#$*R*|VC>=L%xa~RD+rVZJ~tKj)NEW=A}B(b)q;<|;&rznX4cKBQq&ZI zV}sCA9^!J%a!3($JNe}*e!|h(o>QE3P$is#F~3`^U~*zj3=`o?!~ScV0mbXv|FzAs z;&nzXvrt;ElB_{71gLK~alJ`+9Y{ej2wEJ}o9uf?-c(RltkXRf%#=cP_&$;c;APXV z%Bu!;W!H7+zt=M@3hb`r0-<@mt`cUlBD?AMOy4~WXy7xeIhLBVk5^eoV1P_pfN-;M zr7U;J=E{Nc{X;T$H*EDi{BM3M#H)1W*3}V~&9cQX%q^vy6=p&IhX7^ePRtbkbJq&# zDoNRYZK}kZgJwPneCnq?td*H}i_QJgZJ@;A85Rw`~h0 z`JWn_-<+fNZ^AP4W*+{FFr6yN1OFy$OAdkOpM(i)^^G5bo3PJt{na1N$uBo!dPto8 z5wQ7FRrLL2i&Zfe%`>JBPnjAEeiou*2+Wd0iCgFz%T!HYKI*RJTvyID%u``dOj36Q z>>(-qQ?J(!UdL=(HYq|Uh~$hjjT5M=B0!pD@3QAk&8Ym8N;YhXWhE7MRfBuh zrOrp$!9*R|j8ii*Cs5PeSPwTQ|97!W52$delKj_woAhVIerhKhR=2L5rmeogKk1k; zg)jj+TOtSg(@81L?60z79`KuRa5Mu?_G z=Y!GoR)xhVc?BkmW>YPJTGN_w`3jX2tP(AFt9$5@M-|o9g5?u0;0xXB*} zU7mf;6GA(CnTf|}#L%~Ncnnas*_hqnRQwdo{t45BQxYVK8dvKB z*6B}?C@RRRG`A29FFyeUsE%+?gI{j=z4_Nb$7#8Y{2$l}S*G=KnC8;1bWkgQK3uDb zkhgx2{NRX)e=v)3hby?LIAk{(@O}z5xrIBcWg5{L%8`W53uSE`ATEN`Y7ZouPK9DP z&D0TwY4B*{V=Ti7%L{`I!}T43xd3z?#^ztcT-IgFfw9@J%YAzAA{Zkv3rv+$-4s%Q zKU<=X$b>!2U6T;;PjOVb^B^h}=fs1YujcM*S zOVLN|n4+$G#eru%3`Ci2GY{23Bs5lB&F2p?w&C_X% z4_MbYk;HZFx*ZN$^pt3HxRq^X1Q9`=oEJud#SoNp$2_js*M|9XAY`oHolmaXB@LJ- zhhe}!2B-+9kHTx6Yn(S~1%abpw*;=jvIU`~Og@NM8dHy(96+pLA<(_@W%eQRj^z9v zms*LHJ_hQz9e<>S-&O_-4envUqW3V%vRDt3gDM)jJcdp@sOQg|3J2z1Pdk%s8uelR z6#o{d&bZISOtUwG%%NAStH@MeaD`^%+rRzhI z{{npt<%Jg)646%&%>4P_;AmEe51ca8;2i1|%6w~gi_y3@UE-pTFb!tI(EhuAxPbZ> z=mW&FD+{t&N4B$xpr&_&%7~^#Xp(!)rfXfKPScPpKd^R1PFw&OMmVP9*?=d5_Rjc4 z^}J>2l8FHs$%^2NrzjIcU*miU;XvqVNlp|kZHBh=0eh$W4NKllJE&{J-bi z5{GOUTKf+Af$_xUiZDg{QmmmGZkvACk#fd61BC|3qVWV@Hm3R-KjFa1%Pmr0A&y7$ z3yhk1jS|0fRNJ~zy)d`r(-omD7FM&rZM zv&vOnhqfhN00qe=tb_tq5u++CDJCV+f44sKoJGncANH~=#64lNYPZemH^nw>1n4{+ zIf8tT+K=PZ9r?Pq&C^yo=MXdO^_i1Oz|o?SI&oh-(`;^^a8#je9*FSeeJM^830%tl9wf3>#=GFs|0$Z~3JFkMtf3MS|x=O15%wwRt)MVe4IlDMz^;4FU zCKZG#=6#VM}KMX$hOk zf%hJbujD7FtS;Nfz+7&67~8T%R`ydlqU9uK#OEWh$#lrZYSwSZfWIKf+6u=^N#sl@F5E_-P-Xw2DtFu zQ>`TgF|jTg3otSCJuqsuB@pk9xP_6$P}fFHjN~or%=H?3z+pNQkYYds9bm{=%MAy8 zg%3CrN};*B5`a>5BzwPWo1yKy5DUNKOm00M)5?{IQY2pj4Z1l+ht%y#Y;8KsEvZSR zZX<~a8$P1oD+5=xq_lt!>KPe(stqk24{8*T8z4c4u%KliB|psAeFF{^cwsoUo=2|fiAi~PzpQJQNn zTiM0Y-L^UJxNFkp3mxFATZj6S??WEa%9p;4)<~T?v{ilyam|+4jTd?YR^}A zLM(X^HB6aGJOOR`@l|0Np>9G@L`;us6H*=V2?sEv(@Pm#CtK(}+Iv(KuKoK#*Qsaa5KCG1V?d4o1&nfV6e+I{pQTs&UD4`UhhCU} zy5sF?s-0#5f+IE4o=tZoWUpxFkP4QFU}s{DjzHjCOOk#%e_+7Ph37L{CO$6;cXmiW zfq+9@CGZX-xlfbgKU{r+GTWJH@#sevI7Arm!08nA_p<}T`laH~1WreC2;bqy;N=Z| z3fEBY4hUGCso6YQWa7L4MS?O^8B7BG?!M^Ur|}-WkAP`_q<}?&E2mfY>R#U2LQ^!1 zvZw+Qd5~=Y?hVI2rY6?E+1WLskcVsTx>;{(>Asww(uJTI?ggVW-b2T^0YHKJjky!V z^eXUOWWa`nSGhfkkn!V?DLd{+VVwPZP4sus&Y5kuLEOQ^U)3qhiy;FKX)de=X-K8O zt3RVfc@<(VdcAr^K8?6K79=pr#JYF@_~(=A8pr z14_}wL)a&-jT&ms(n7nKt}H8$9Zyg@l8YlJ9RmIZAQ7)4KZgzR34}-)tp^83+iQz#OrrZsXv6@Rde%kH{2OQU36E2 z4oy7E`h4@dp=Up3;kiBfoWHDAvyQW(VSw$3qX}B#=vb)ODH?sqmi`0U*^mQiuSsgM~n6_w|ZW$z^HtBvwo%55zg`nQ;?{bLtaA zIVv*)YHvvT6{PF8kFmh{W`uizUO;Rm9F(pcUgH3j0r`; ztJ#B18IjupyY3xzPiYsN7!FxKjYUioxqHH{Bl zOhmd5Y(Y65C|Q~-^O{WUIYmVbQj5NMwoH11xe7ayrj~PWgKznicCp+-gWqCmqIP=SOj z^}DsXjaJ8d;$jDO!csY8M@l9Wk|K5YH-3^VB)_{XPz6$h^~}7XT)=PfgN00)W(rZC zZ{(Q1E!t|rLI*N99ug4tLY`rFv2o7T4cB?MkSKp;VJ^HU*wMPk%^UpwKzlKyy`y{p zbR}PI{=~QUsAm}0slmgCgjh(n+0kQj@-$r3+|91v#BIKsMtX zV&JEC1y`g}9*s}R?DcF1{(VMDsl(!^^*#bqoy_X+W0*_#qmsEn|Fs%-!NWoc$d(@x4+kqOv@ej7i5zZo z<4-!1kCl`Gg|KL3mfF|FJ0U2wKKrQEFb47Mn_*$=D?8UzZ_U?+y~rx;FoMXjnk}r# z2E;o)i09Yk4LBb{(565EZK{_v5DYQ7M?lGG?R#Uf?rsPlh;Sh9{i(^Pb`Y4)to%;u zdn4aeJ*elUk;T)*c7{4+CUlz3jMWtZ8RiIjiw#5w;@s5dQ|4h#T1~rOwi>3}^I;q< zYYgV9QzNBz(e{DzXRYIb1xGe0LBSTdioRko=K?(3Q_*Gm^QtOYc7rZc0I!alt?GER zTiG&PwtWrM`*!~aQHDeaF_x_scuZ3Qo@?B|gNny>v!vYTLM_8*Q9#xd6@f0dcB71> z!}0r?FgVIF>JD2lZkY|4hYl3Xlo@-B)lI70V)QABe)}?t7VV@U2Ja=(FHu+1B*ox8 z*6iZYV^oozodeCb(L5olT0CAy^#X3t>4!@)X8Xly(dLq&)=u!o<*n}tB|TUA@V}N( zoBKpm?MX}oj*{=EFTZU_rBM^@58WgVJx9`?Kw3H40;w|lSY)w84Tm5|?d-CB8EYij9Vd60u!cz_9kerqd> zm(#<<$*-s3H85oov&~b+K?X2??lIp*kJ<9$%Nr3?rJlqZf}f4}FEIeRaI^r-*bB?l z+V{q~i+;luiCN@#*Jd}*|0Dwgq4)I0Dp-@e@AaEB8QqS4{tlctsh zBa2(&vL3t5MiDx|JualyJq&p5ylA$J@iw>~r$V|$-BHQZbGsr+v3TnJ{lX5MU*Yin zvNVvo>xW9oWg@#%x*+SC_~=S6xPn5GkR`WjRbr3@^1z9}k)Yfv=X|Co^yPek4Y9Se z*Pe@0-I*AavJ77CmHL}(M=mn=Qw4K?UX8xF^QN1SO!;FV0n}H*+Xmm4EnB-}^%Bv7?jI~)h4(&PIZcg5Iz%j z2TMT|79ZRH<_TdLbgg3wzHLx}Qt94t5K9cYX-rgPIi~tD3Z=gY?z{+hhnzaU7wv7^->KZ39cKgr*e0OEZ z>}M!FbSDZz;md1Z-fhPB!tX`5$Mme{(zi7QA-51z9~W10=r8 zKV;tqUQ~|o9r--9eTe)e?!MkSn80JXyuh)&fUeyEdgw41Wj2F&;ZtSKo%M>Pu34~1 zNLhvw`@??X+Eg3{)T=y#u*ot?Ig9cQHf%#u&bD*NBg3;vQNk=bNNvZjd+Kb&g6~_#~_q4zBr;WuLgt^R`X7y2&Lm}KTQcTZ(LZPB^EtT`q727L?+4qwk5_rX#XFJ ztjRUUI_1;)qlASt=3kc70(byM%7lj^3B=Nwox6-BysqT)AgHH%){w|~<-bXmx87jC zXllmm z`0BKTsMh>M$z0`66N%=pI6IeaTdNblYh^X91LI(Z_Tf|ycu9WJ`iR0#iEh6!<~>*}K@I6526~O_o!11mey7}7Ja6D5*Jsx8y|>sD z7^E-#Q7io`kA=FVpyxc;DihJUJAdN~3>E7-#1_=}3zBH;>dpc>%DVWGxBB8;EdVcM z(PbZ(zTs=X3Y9ZjQXLH-Gt?SgRuthMA!PF@$iq-b#89TE+}$Dl1g{OqY7ibEa_*al ztjkvF=o=+Ww^IvYN1j&>%}qOf^7}Crxn+pVHd6bQPAzRIC}T6b8f{(X1BP&{_=FXx zba2K_U8o1sTsd)Y&=h(hDtg~bLPE-M)L+#Co?RFyzFN`qhH z;g)cF%<~X*0me6B_PcgmHr)x&=N~E>@L37kDM}F#zc?0))zi29G((Wdkz;r;^zf=Z zcc!2`E$_kpi@)GrSuFYiZJ21-e;jEhW?12Bb6054zW8JE*Yea70vzh-QPAM**xDK%@l zy%~o0IS+L9Y(b{)zG)=E>XB}iLJ`62UyUeuQvyAcU}kYO)5T0$@t9(X^Cglet`3b^ z4r9y=zTM=!AY?3R98VJwrViJ<7DVw&1Wn%j@p!l1Ys>dwh|eW&I2~R7TEM!-U>wYy zRVyBL9s3R@oJzo`Ar1_{r3g=p-N`hu+3*%%S`mg1cuisILUp{~wJ9RrVR@>9 zThhzM!%hT$Fg5OCQMc$GG(N<xoFY^sC z`fpE9s*K;5FR`1cjOIpY+mkq|Y9G^$3nL&!h)vda>!gVUFdP|cd26EiEpY&hYidiC z`jL9<@P%GrQV_JOTgW9Yc7C!#USQ5I%=%L*3|Y3^DeH6k5!1$ zv?5>TYc+$6(eWy({-+sO`g0~iOo#cJ<&n8v6)A}~H&W4Cg-xH96Ww}D6v?Bxk{G(7 zJnylGkzc?Z&OTieVL}#|wpWdad1U+H`l|ttK6&oae%mOW5mRzKjQ$CX@~IVmhX*Xi zn(Y$m^IsXLe`R(^b$rWE@d$ja`HOO`xJekhW!t)BxQf1i3}SX&vo5lggL+F8BkSIJ zMy5L6b-ZzQu*?^K^oX~vJU(008a0P(f7CN6jTh@=)U%_2-;}T){kO(y%ZP~xmo|B`dTZIP~?ISSSi6ptF*@YX^1_%Q#~GG zs}ne{4zC42rDa7P@KUBRM-r`yk0dzOJ(W&66>-SVb@G9jGkGcYMs#qtpUN#^Xr?4y0T1k!BZ$}78sM_E#-vyz-0rxQ|JX>zPx0e!HOWL&$N6@7pLdxyCI|EzU3fLL9}A3LSjNa{hn}>M`fOfXtB%W%NxvXcC33iTRCHvc;uW)Wif`{0bN=N?!B;lYKHho_IHOV27Da zZ=OBz%R}bW=ToA4!LiZiI%GEftLmKN=uqn{(Ua@+t zqig;1LE1>l6m#T?uj*Qj3q5w}#hu~F7z6FnDx2@LW0kCqoPyod*CKIb9dAgj_pzEN zsl*SWoH*Ae9ln@5F8k{`g`_A^&{x%;=dQCK6zm5w(&BRyOhxI=72&T3iWp5co!qM* zDMxj5d8U{#J-lT?RiN4k7pOnz0^hwAUnSyl_iN-Ect*i|hL}r{bnH^3{9qV$04-C&wpJD!*o_b^5z{Ik?;W{cIIE%|V2 zWF9t=zLYw1y%+qVnD;`T`jMo3l`~f@O7(Zhq}R{MaWjQc!jEVZW;2p$4bj^_vUq}M zm-c3%csdJAr>$^Jn2*D*c*%9%t2V$dpctgE2c#45j6+w7Z0bH$>_u;gQ+fBE>&;qr_se1NnP7^wSyfr%8FvCip1_u zeyaPp;6l2+%F;EeFpqqZx^@gn!7Y2jlxYjqcN1~KoMoh+VGvOIR6!$wTYyN zm^*XB(i?2&xJ5yqK9dijVWKZ;*pu_^;AG|b2z~P~l{3XMk&_b}5uiI&>auopYF4hf z^1)^UgF#^K@>zk9t}4Ff24wDdN$%M8r-z-NS^LsLY}edDdFS|947?jzQyF$e-M@x= z+Bx5qpYzRiTP6Ryid#frwH?)0GOsR=Lm3+UVGehPmn1J>Gj7N8z+K$j9J$nW`=pvg zR;qF&iRMWaCN5R5FR!(1lILfWgq{U_*}C`Cb89)>60@Z-k5r686L^ipGLae)21yY( z=tm{ix@^B5Rsxd~)Hh0LlZ#RJyyPmNs4TLm!wfU7d$B30ku#!OQuNJVshoNC9IlA) z^tp@0F`ev9ni8hnl=ad0NP8&_rA$o8Upc z=u3+zIM6TvLP}ULhCJ2xE?SP_*ReybpH#ou_wq2`MCafkjSYA%N9qc&&M0)0=G2FG zP3%VxVj~2eh8OyO+d4GljDIW2?Od&7+`eg~MhYX!8~MQxO!i9Nc}|XXTgAXNHD>Sg z2%c3G;@ckY{H6^SGk$6pv9Lh{sIDzRD3R#svQo0tYS5Pc9klPz)@37m{U#Mnz|iEQ zFE0s!55Q@JGTmdY{lb{k`}(*uuPrTn14~5dW0$8vEqc^@qnjq1mETj{!SK*@zHLK( z5cPrLJEhBppapfmX@)czstB~%0P3C3ATc+!QakJndQNI{;r~~4ZygnN*Y%Ch00JU{ zC`d_&0t(U%5-JLk(nt$RclV$u9g5Q3UD6#A2Hhzj-5?z^^E)%>bzffh{jT5htoM2T zc(1jtwdDJqIcJ|;XP>=4XO9)nL6(A3@OtqcYZZ}~R7g`N@Aw@*5{aAGbN9VxH8T@j zP3|a{iZL^Kw$9ZJ;fJe@iG?79|!os4o_mPc!2PBxV&{W;~+k2G7cCuWV%4z=g z{<7X_u+6*u(&o?|o{E$7_oKuKPi`Po2brLjmh1~|Ni!hDFYgr_C-{|kn$swZVR%Rf zwFOmD19fS9TkzU`qiV5@6!a1ey1^x#cRjwGF^rX1#>?!-xur+r@C}f8Fek{GpQ3g? z9o1HjTv^(EiZ=a^5b3Vy}?o z@~LBIUEHLe_eH#yRFU$2g`2fIT|CI}**KcwoI^hYE#v&;?A*duE8+_+;Wxj!Z_9!j z<%4vB7c%WPt-!@m7CJVnB%;kzZep!lHQON?1RR0b2K8vWLts^Y$xc;a%mllKiLJ#t zt|f3ttI}SxlbPyz01`XmDw*_>mO4`_|qtWcmOgS*z`r!&u6} zO^246q2*QPf=aqHVFU}DxxGf+CcoLVWiLF}Oo|6Qy;@Ymw!o{A#S5ZE zq?o2%aRSA$;8JnjL!8Gv{g|xRxlhuBZSI8Jpy(S@KJ_y* zgcHPK(P>}G!ZFbdQS+kI#o^b+<7Z{Kn&}^i$q73dpEAT`3yBI4l>pI)9KFv32uu5= zw+`KV0p3B@%WUJ^5~^hy=w2EnI^n!pmf9xcZm}Z$e(KiE4Q8b&SC_A)u^W{KV+u~x z;1Dee`Y2wzAU1!_dA)2ngwHef+SB08yWo7}ZL!SF3oz}{neIVuA6M~sPvNfxmrKNK ztR1o(v_#n-+0=TmzBY~{MeCH{LWFZlX?fDgdr^jGA|86TYvyi)T_nDFihtHXklyXuJEBFcc+MA6 z6voANSE)ZJV7M5?JE)GaOo7W2>W_me55=tB3z^RPMp6eIQKL6vmd1H`D&PTtbF;HD zE<4jDHLutjVr#*E*Ixg^A5Q`*cRi90-G?wrw>mel|9-8u+HA z10y8fmiUp@BJGA&t+UNcqIs;QbH${i^d}Z#zVD>2;KGn22xi;l?WbSjv?X8LrW4Xp z#}S`Z|4P@>wg^fp&x#Wcvu48RrGJzV`ck_Z_e%Zf{&KJ9utxooZ~}8d?`5NRodiX- zK<$38^>156H@M}k&-l_1Fu`^VK_@v*11 zAnLFG_6Rl3?`+2}oYKRudZ1J)`p1;h47^_PilE?|F1G{#0e@kM0!u@hAX@W~tJnWl z7XY)O5=Een^B2YMK9b`%4&wj+5@>SK8(L`!y@LWnp|kn`Ojp4U9py<^0kp4R6-fr} z{vA{GAEs&oC#iFgz`s`!Aa~Zg!BgIUJ_RMvKZJrUtqI6fIj8sT=kniIiCKhRrtm+P zDfkvFp8lUt!Q%gvRs@eBU?}DvPp8Y}#T=DP#+|XJzhbJVb*6^@A(ZSJ*uwxRc7R5T zJ^2eMhTkRXweE~!PLSE}D#oj(Bb+~BXf?7Z7@CS4xRwGb7-1PJ|Mn%OK^&cG|0QCm z`q=#`Vn_kLhte8)xZk|SEV3u?FxytoqY(ay=M7|71b|a#x!vXG$3xe=%?|7&$nHQ; zSys}yFW1t`0h^<}NoF(Rwj*xx7;Zh#`^UrL9q_f+Y4=#sY?JnlW&oj+_qnz6q^ zl`PJinN_doPkF`5OvN->i?H(gHT|Iqp8$F;+%b9;S~%wv6Q2(15P47p0t=0F{a|uD5maISN-G)^X{mt?qcfzCk!93U$ z*ngId6tznPAWjpvKI#3b^f{K)g?5nP-SR6SmGcYshsAFNY17+X-!-9&gmjHw{ScGN z_D2n>FWSy29>RYxuJD(oo*`&=^9Au2$?E>#m^CGzmo~YFo&St`Dyerbz|Nq!JeGmB z_H;wTm1~fZh%<075RRiQx&W{=esr@-*ogyG4M!Pd;W`S8IQ|j3*0U5VdU+TVyz&6A z0;0)b7AOEMNd1rR*s4HB%74{ofYN}w&tzV$L0W_C1y`2zQMdn3NlG{nV?V&um4lRx zmcjJ`vo@0(NV1@MAmY}$K@H_KD0HCSZhqm@>;xwVd~}slAG=LgQx^@DJ`V5@L$5i? ztyP5fQ1bvIsL4>Lm{HPFBormpj%t1ROQ#lPkm2zQxEzZC@OwGDSeId~?#%l*O;ZGQ zM0ydD(!)pXvM7-$iweQgIdSB^K0+tR+0IP;L?X0I7Zi)TaiImDR&NDF zE3XMC)}W^`R5*ihLwJ1yMqpZZ4qb5Fqto!Q!{TeX=-O|GiM}qX(nV(e=t^<>!W%Ss z<_!DlXXJex>jH43L1bxvg8T6(eIjdOjb1P4e;##uT*^C<0GnwAbrKtpqgBX;Uqx9+ zOuP761zO*|JM~o}K?2R`N19sw@%~Bk_=;6Mb9LtFcw{c#h1 z5btD!FKB0-YYD6TxdL!Ea)hFGTbK66-tS1D$r;KKt9Dr3-%1S)-yvNDute$$W*rGA zu!mRYQ$7}FoB$`}XP?{|4PYDFa+F#{+~me($3#PS|A9;)7X8}G?U9hHAml`MBK--8k$l2|Mt91m2~ zy%=TU#J$#M&P-Y^yyFe2E+~jZc2bSIACntJcsk$dnJ_RY0}WBeKLJ?Ua3A(1JEe2B z9mVb8hw2L8Zl?VXi!5u2^Rm$!QenEr{T=sywCzQXF+cE4)WvAJ!CIRY7v7>zuXCm_i`NUH6uA^oP{|<(Ru9UTy=81)bXv?rusG0M-5%QTJZOp!;bIfVJxEZvm zK_>fB6 zH%bV%J_4BKips47w+)A;hotS{FK4gnw!k#qCG&XmMH3{)_uT<~;OcJ6wSgzw-XG(z zz9B-<)_2L(Ox?Tj!~N!#NCPN_IWh2V-}zybVjV(*0Iz9m@AxXXdHmuHD;*eo?|f15 z6GoK(HS2m8aDEG*z&Yta#jgX{GY7~UI4-(8@V%^O07W6z_`ITQ1i{rRB$V9Q7_oh zfWfl;xT)TTi$XlM>DozOys%~EG)m7>^z&Qc1!T`Pe`H(ya!oNAl=l&EmWz&i9^ za=f~TBl2%S-+-z;Nov}s;!4x}cT z{GnM4f%2N7$bex+Nu()Z@r&3HU$XdlSN%=Ja7JzY@%ZMC{qI3fx2F^;CaI0O`*Mok zQ=+t&$5*lyi~l3Y>KHOm%czS4{(Qtu;*Bc*QLhG56XA6|LH|4SEVL;=4%R|`d zd-HB1G?TC|zxM+snq~vZnz>!BE*bYYaEl^D9rnvA9$i6CsVljPRoMxX+$S|QK%=!| znA2?bN1Ym4+JVFVZiU4BZKJ4GQo*-@DG7{14Dpuzz^>o{Fz{D+xNdgCjqib<2B?Pv z2LURBQ;A3ST&DprEKlE#C>QKu-lJIe&G9}U9^3@Igk|vE#$NU3*&d24iQ>UQ=k27xWa4SGrI%W>R0$r zg2UUDmPQW&WIwLrB50N1*bN#Wm9lx$kRMd427h$#iG-}uz|xboLsdXuebXt;yzZktb^WBXNb#vLgST9jLG^Tx=+TgeAaI^Bd#c8?*mh+t=dJF#ljuqISQ#Hi z(D7v~iXe!A*GwqlGS*8(AHu-PJFO{;?Osw7y`~38I|N6~OF{RQm^YCgKKp5}?kn0J-7y%x^_O3xBxV0Oir=qM z#Qq$G^c%q?Kw}HZ7Z31l_TxbGn)JP@AZkgNufb9kT38_1_`k^~#q2m&sZT`<2=${g z!(rDW(>qLUz`B>zK&HpqNU{M*uE)AZ{1V%sk^MIxI2yIQDwob)0GZK7TNE0pv0hsFDQn7 zF`$P}_IHj~j@WxW0SrwU`sa%%2QOT@zC#|iu?j1~09u@1j1kK_j~1-f9U(FCxE4@X zPE!Ghf$du*HnWey$=UTFF;Xoh$8?QoZrMS}mGQD3qQ;aFDHF`_8{J{SR4>GsT+X4>C${d35Zs&=C!M>$kD!u&};DT~U1N z`edbOJH>ZrYUjTKAyVUg6ih*H5-nhtv07m^TrgM>AqTFnt#W=MVODK{T-na3$7M$` z+Ui$T~HVsO?!L= z88eon!W@tM{O-na-LV>Bv1YkBxoaK*+;r;TcU+5Vv$7ifm=2oA6uio5G6A5QK9_-~ zz4-K380FIRiP`oZdZB&6EW2Ey2uqa3v|#>6Egtu|tsn&h-iUhxCc- zF5F~RD=_cRkX@RJE%ZaKTphS|riLkkVlCkTpiU>Ld^0!%o~xSQhXAN27qZ^2cp+*3 zy(H(!Y6l7`kDqgoNqV{$#u4z7)mgA3=KWk=US7BfxFdH8*V1y^U)zQ$9f9&$1gxttrdP&t&CBuw$s!thoxGSOc@`7dGn zaJP2QleO6`anOZULvy`sC+~@a~CraeCh|ww)SS+1-MezpheWf*U{3_{^-3k;* zC)3MAU&+bJ)`4QlhmrdJXW~4MG$i1sJKYfl!-cxK8d;o}I0W2`y%oo|O!&@Gd+aLo zlf3O(2EWpEyLGS&-0L~vZVaa2;Vj<=^?dnLxV!cWE4TSb&+#T$u#}KQ$hGX{`VP5C zUA`X{e2oj_mEW!9N;{w2Z@s0n^4kobNfvQqeenxfn*f+0N{vT^279+u&T;E}LFko( z8yf^v1D`a{o29JF(p!=2KW!6i7Bq>4 zMRDfv(}3~5Lt#&DvTN5kmhGczBgv_eRF9Mms_%cprxH{a5{2(7?I=|E?kS{ZcS;Bw z4M}iukFDTDWtZ=k4rM~Ag&gy$EY@xrT3!z&=W0Vd*X;JBYTex$y`Hr|#j-iRBOaH_ zh7kny5k00%Q~(uCfGE@ScBWSeX(jf#NSOhTVa>h&2JgEdzx$!}iJU0YT4s;~;%H}a zJn&0WjBlUFeBa_@_W*uuvNMXaFvW>IqKt}^GUdwl?`W2HJoA4LoYk8HY7(DyGC<%z z=5l&yv$GAFYLtCT))DKgvkp$?Oie45$i*|e_Ce|H&=RC)r5+igx%$`j2Btyp0v0TLT)<^8?W|s*{a*7ygIPr2Yw5#ol*?894A+bU| zeO3}2sY?{sInpDYJ4;E= zpFeN72#RxczW3h5;`aeRaN8h$%JQ2wJhMXU_%6`@_A_@k-RR)uM@L|XGlpB_qT~eX zl;1J#tPe;CGbdcV42or3FXr5WM=FeaLF2uI9zxd(YA2!8!c0ebT2&u)dIsf!D?Uhw zbgjrvx}Q^=jo7m}J>33o6tNMbFXXt^qxHeRoYW@W@mO2e^XP?mb6)JwaivK=gG6y> zEZ_KIH~SUKc`zvU(AHRu*3cH-d;C4B^0 zmDW4W+|FBixg!9**dZq!R7UY0u3}W;SRCQ?u~770OT+K5WBV#aKXY>FxW?@dPaT^O zVG655`JH0(py$+0ditj)>zMDf>OdR3{p%~LVGNr+*yxlKVWRQH1%Uo8~m26@->sX2-Aboas=&z{E}ST|$ZIkziZazG9xXJ?6{3}MZp)LU#I!StY5>9_WP z-#}z9`%zGv!r=Wu{Og42Jw0K52NiYUutELTF?ILp=eal2a|5D;+Pkt#7Z4*gza>(T3XtNwRTiaH{p zjuS7JAVjYvz;RxC=aFdvf$T~0XgR7W6{%O*oh#3!5`L@cud{?0t)LkA=dTz7eHOxw zx^O88ix!uz=wD;s199U51{Nz6_Z4p5UqhT7%D|k`kXN*?vcVwhqNpR%*`Z9!%ZmYW z@6yXK;8YN(!`fMFYzM`NhhW{qk^)Zo5Pi-;G=K8!0|e_GS?xJ&6PPjbuZh7)l9aacyx1k^ zJDo9_G1y=H!~x1k3pGykPs##R1F8Iz7YaYq|D5EEsOM3*uXpiM>F{K@@4v|t<9c1GXesiaVrUWnGOYcd zX}a~_qsiYttgz-kOhNle{K-FramJ?)QMZn&{}0Q4_#c=3dkWFBRe;HUY4PPNFMCj{ zpaH@I`h6@7&xHS;1o!nuAEFUx0#Lv}gd)=9mx|LfU?c;2!DEzQQyK2+-vvw!OstGhMi3kTMteAKyIDr@IB#f{D_eKM3&B|3WkWz}^Hl4F&_2`Vf|l z{6+O8e#9+(A}rMCDB^$t?nWLH0(CqOK_31HRlFLugrfFuU)*YkyoYRsnj!p*Wr!n> z<^MjPuPAmDmKTg;HninYW+iGdC{lpS=+#{jJ>_V;2{{cEhRsF%`?r8KV#SEwNAc_> z;05p-r0{g0ylC}*)g?;H1YV((`8Nk=P=4$!AmruiPlj^W)BlmQ|HjB? z>i1uzO$Ou4!qMvXOYCT!|HZw3X$0nf{^+@hubIysm-)8f2#*!pcFXZl9k=pk+9 z`Yp|*L_S`?%C*l>$YcK%?+G=@6AlsvsIk{H?BStW{*?XNhijtQIr>caFkq&?sYMwF zqAol;8qqcKg;6e1CG+U>wvs@J=*mvxji;R&@MWK$Lm8oC(OgFLcv(?+tCy%GF?t|P z4WAEnjUB!`{N0rglvCY|1nrfeBwQY5 zR9T-MLnvquNufw7lc%e^7J;lP4oL*QqI)z5`Y>c!r;@aTd`+^^2H*)1VgPp7E{YsG zuOc)kvmR5ijN|bdd=OKMm^A4{4XYdBH3g*ea)8S1GOpT_{(adu;O@H?&dS!+b;0*2 zR`Uc&6JSM3nep>tW`gRWLOk**!DZ}Uau&oxL}@PgIb32z}j36h)_!^A?~ z|DjtYHVsOPnxiz6(u)^?hMp6Jqsr>Ojfuyh&=C3% zqEyBlzFKe%@D}wg0czIen}XqTXsL=Woa(}yWXzRR7*R|{w0=OAMVDQ^YIN+>V94B% z)ufUgj$#rrd@Sd~<73Vf2IR)Dxu7^sGkWCCh%x??8YLrcAvxQelW(uBLY&!B73>nJ z%;Lo}`_4OzrO11|)`&)OIre?GIzaCBr}s}KF89D+HZLH}bS7+_4pOo!i;o=!kUwrg z9y?IFrL72SyLy%{TxjPt-92=x*{|Jb!wOX%G?{$~Q^*KwPc%eOc3L&m7@OMKH-r#e zS`Q^Z(z5h9DVh3+%!A36XpDUg$ywCVbA^k3D15>_7`uS z9g?^}%A&m;NpSGY%fw462oi+UGptn3PaNHyJ6K%1fE=rMya`*1X<$%kmy4g-5iL3n z44$Iqcix|<0_U`&(>H-N2cAL}L$$o|uFpVHv{dwAgG=p!W{zFVZM(@jlbNqtNPW|L zw5JW6DsKBQ_v5&ulYN>>>zN0idl(Fk zbiBf-Z>7L;p7YUh=c9|Mhq=QR6Sc_cnUcjWQXh9EP=smS#P}d>X?;2n{i$6}RWfIJ z#c+z8r)@V`8ea8E9y3pA`L$qvdu-#If!P5U)qHK3cKQI_Y$!AX%0z(}R+bd;4#?22K{Ym z?JGPnLl-A4hxBs8bBAT)IV9qj<<;^99FE=A-K{2`+<_6skf@WY+jO@41ZWpTF<-|-YEC@IA z=Gn0`r@;t4Z0A=Q1HbE2w>yU0zP^A|%F)ktj30Mc4}@kQK20so>efK(Gas?+9P;Jr z^OR)1Q0$Dli$$O^^5iH3l-Mxsp$syy=^Iwyx1IFOyd3URrMxFihjy6{sxBUF+75Cw za*NT+dm#l}swHxKxEjZ*?N<%zS9%wlth008OBi{T)$Mfqk5$2Q3p?8z8jb+qCs9kS ziWi#N?h1Sf~X)=*B$Da}V5;%2P6mE#- zH-;6HokA!@lqiL_JNU;=44stWZ}C+iPG+o!W3m^nTN@vBj`C?=x$wr}w;Z;Pj%8O6 z8H3#c`Ou;B4t{bvAFxNrCaq6Ubhm1FiE36f-B1z6FET9R(yim%4MtOK;U%=^qAmnG z+-|4K9VgQhOoCd$3hJ^|)#Ziw9_*{X;3kYPY~-B3V)(}48#0=_DNHgwcTgDAw@ufC z>!|fF-vJxojmZ^yWt;f1uY(XmS^4hx@iLhg!w+JVbG5DC1c(1N5%-eKr0RwjUm076 zAgd@P2~CC;B-r$~5ZV%46U>VY*lWMhv#TpG66Uy&mqAupWIn$VJMM@F&)s7tpb|Om zPnSL+q^1yXy+8VV(JgGxvbwVdADh1jqtni#(|sKJn@NA%y+c+DqyQ?7n7BFAT6mnV*GA5cwCEO>w7V?C+i&rG?* zGL@@jD{*h?&^g|E9*K+}gd=A{g@wci9cCjeh$g4L@P(HST86Q#3ppS9aXv&cStwCf z5U9!I2j5ggdh(y7b__WQPP^AWuc}u}M2utlw#g)1eb$s(Je&Cd@6z1^yoK`>bLO;w>i@6l@~4;pOEwU0$oIAiF$>$AcHo^WYu z2*pnH>9}#jyo6_T-Hb{#@MNJW+2`aE1oMSO5xYtyA;8H z?qSuu7i_d{)7sdW*JPj(k9Xu$)G)Qu4aYu(ia7k(eY!p@qc+d1dLXF+4_LzAYEEY^ z-RV4Nd!3tNQM1dzh7H@>@Pfy1SB(YijH+vWJqsDYBYXly5m?WRP6BDq6G zzAz7`6+vXLF1VwN;H&@HCUki?omsW>$hp|Wg8PewYv4G%LQ>?L9`L#;BbLszdC>au zX9YSFhduKce7ha71l9MXDu2S(6h0wo3R;vMJP_f-bexUfv(wf$sNu?O9X+WD6$qZz zjq1WeGmf(Bs?sU@$#zrFtI_qCi5zy%x}w0Zo9)G}EpJLZLmfZgRPQ~0yvN&kdpeY1qz(pB1Wa8+IA&*Bl|ELwsK`=5Cd>CdLFF z8d;jjyW~SH)P|YWUB_7>b;2|B>-!BGUmD=rLVLL1#<jzHB6|*e~MHZ4ja+bCo3%W*! z$*W<;Sus82#&TWv>15&s<)Veug`7c&o#!L(dh<6|^pTYGjr*df?~D)`@vg7u8y@}~ zH3h)}OyAh+n{3qt;}GG{tH#I!8A-qjUPQ@!e0+)y&d58dp${6m<`X~oRozOF>k0-B z0!J0HuJ<93b&{bu^b{R-O7VdDjP9y&_b2PKaCfnn22F zpTsb6U^65%diUjCq-yqXOL7005=)(nc#afcW@Oy;o&`_q%ise8k2dgz9obzSR4@N)51_tXHB*bereFc?op^ z(FMh{yV7ks%)8DbfnTt!sQj$JvDA7=3m8*+Gq56 zkYrUU$}(|1-8GZM9kZq1bV`S9r&x%Q;>GOL%l^xOZ5uUu5$uE2+ z8JcpgOkOf3JnE{*%sJ&W^YBIS@-Ty!>Ot&<6FFazV`UW(bB_%3nv9tb``h6epG7Y1 zEs$Kp+{R{kkusehW##Wu+uM5}-wswhu)}3c20nIsO_1X9EP`aKpsjMKKVma6*VvzQ zbHlUpH6QHN-q)0}UHfzTRnMP;vOV z#L(1dr@a2upk{DBE2X-9O;CSjC`7nf_7%wk%sW%LRf5W#sq<9uMAz#TnO_zWCw~*RX@vPmqQ!GSUu3PilQZJ?)S#h;I?>+xU zYa;g1WtMdTTitGkKg41@-6;#o+q!mZV;x>J2{G$070#eRf>ssk7vheU)mKE<<)5*6 zm$n{oN{&fQMwxjbH8KlQPlwd+coQ>C&GfY-+)(L{t;ta5CmNQu2z^{sTjymEDzB0b zB0&!eZ5pzyjZI&!g_a?Re4>Dw@6sFxN8?IoGwKoMG@>F4d}0u|*e0xPLLw*_(UZXN0dHbTr|*?A=BX*`e5Wjk-^<q&mmJ^(}{WrFdU_x!@(jVcOeV=R&%N zz2IciBLpw&&G+7O?NXDw6a-%)CR`=b9p#u*4ndGXnnk?ow#NulB4(G;bSYuvenj%W~ zztNgUG_Y^fJhs59ewi6_4ecZK8o+^iLPG4*cd}(uFI_Uu0XN6deX1IMg5GS~ME?1X zy-~>g_&M%=L*vi-2RJnSw_cbvU^7J)sPgGz=CungsHyMNyPC%kOXRXGRlW6`>~Oi@ zsp)vUGTZ0HE9;W%i&njf`z5OF>?mH#6lNtLMuKCTWAX=WrxQp46G zH0*yrmDpclF*Z4SC`EmkJL$GJSiPsIrKFM>Y%G@g2@?A_T;kNA){JEEirS>!P(njJ z_k`fwXrym!d@s44-IUJ$f-ebiXQ$kv(Ca84QiB)`I3 zH?N*8cLj-u%Ar@$5x&&ExK)tm8!v=Ehig30>hD`|`5-cud5Qg8EeJrp?=-5c^6g0uf~?N9 ztr4z*+jj`_Uc42Y9R9|m@_vPv)ep++(KG3`|0Xx3yY6T8X!c?wO&3D4Od~A&3uBqY zr`xHL^kyXV4(_CPo+4T^SiYA==4)Am9TYF>d^{B5M0uT(hd-6DQ8ofsy-_t06l8tj z)Ya-m<}mWX5neC+h_f!WO_q20hRPsC0|kCIMS*}uutXVy!hwNTnZw#q(o?<1x!0DJ z4WT#JO{Ug8hxIjD9S0G`6<3SkKuPm{-K*0(cmf9x=;)<{s>ku@E)W{G`1r0?es z-OtV$09aM5alhUxd$f|Ajt`;(h*_jA`cm|HMAVPuwgyzKL-RXxW zNI!zuSa7LBgW~jB_6f^b{tyrM-UWJ8d{2>eI_dv@WlyRcoJrF;0%Go^Dl2LLjnmZV zZy*aE%KmWM)gi_4{aTN%X~6Amw)w|t3A#KTmPUzt9y5I;6x+qf>Exrp$g<^uZ!ej3 z-=rhJbe*c{GJTPwd=FPV9sCG_HVPe8)c@lB?UTETcWOR)ggH;fMyL!0%280Qb6l_o zTAKM&5#+=?D{Q}99J2Uc@O^8^<~?3t?$Aut4HiDD;qycgEC_;Q5hnX)j)$@#NF`@Q zS#XAArjV$2Ay{Mul`1{=v+<%?cr1C7ogPU&QyC}7!o@hxs!T<8??tJ8G~+&UxV%&W{CxgW4CJM*Je z;S{PG$8QibYvMgr>iAXY8-jS^F$X0M5_GC}yU66u?$)lo=!ROWG8xD@<69?dc}1*O zlM&_>znl4v<2b2kY7{l|K~RhFI;tq3n{`_9ag&ppe?@t4M(}a(7R}+>TSb)>b(*s0 z%|R{~<~#|hL{>Ny?xLsryzs^{OwrZ2u4nIMr-kQf@`ImSFeT|T~hSd$uz?m-RdO&r@Mav=uo# zxGLuAB$>PE`B8uHsMf@m8z&q-FO23d2PN_3-7@8aQcBka%j0`AsDcCRbf+->NtsgT zK!HoFXOF}oDzW*{30i9US;&Afk>5j=Y93@a7djAG-I$@zdCXpH%B-e3t}yo}sy+j0 z`?D$pIjr+|Yaz?#T}BSU?L&JaB6u-4VWm~Y@EG|R4)D9IlxF1hTxfjpo+}HLYyVy9 z1DW*53rT9P7-eHURytfx!WuHWb$Se^QZbk^nr}ibUOFwrh1FfY>nIhHsI2l!Ouwr` z1^{cY5&OMowa;q$gli#Ry$;b+{62#*A6*4f87SOpO2o!R6vs)Y>PvwHB!gURnfc(@ zgLb*oh#bbVN)cf8S-}o9C_t(JFvUO1cK>@%L6P@&3F)u8&;Oy|^3^sxvteM$oJ+xILRZWGB@Ec5|M!dFXu#0FRnh5x-DE}6c7~w5dkAeR#1?vq9R!(No*9MB_lb5qF@GzBFR9GN^Y{C5*ivL z=Wc`s$NfD)1wD6D>LT=M0w;83g%rD4sv7>DoQf7ZAnV7Au5X<9ZT;sgGtmTmR?~b^W79 zY$s?wrXRRiuUjvH3{(i*R(0ad3kU6b2@|ygZw}mWF2PMC$BwqbIumM<69HgWBSf=d7qh)#yB}2;`+)Q{KIDC+^mlk#NhjnOW{FYxnmR@OJ{$f8M_Md&K`A(UCg*P|ux-Ge6-F1l9k9z3N{&1^!|( z|8MO4e;0TDf@yzWyZ>aM`p@GOzeCf%OKJWV$oqG7WE!*YIC|F7Ar z{-smk_c8d_;QarMoqq>p{2hY)FXin28d~`~qWaI{6#r)N+rkZefkkbh?*=z=^l!t8&%@c7U>ar_1KgRBl z;NGppV&0N#VL?k?S?u`qrjQphCNSsfY5kx#f&u~+YI4I}bZJxw#Q#jD)f^AsEdaq5sIAOx+ zHUDMsnN+67(v-9PRxRLmweDdWjoVxw0q;107Df&2cJb{AHZNZ%pqbRA-wX0H?mKy` zd>vG?t~|=7XjC&a*7fT{%S@En#|OI_S)dl@yoh{=v^r(Uo*Ug|ECD%GdG2x-Zy2J;WLoIi_s3%WcldWr6oc zS?5Q_7;KFKJc@1Z?UMLVjqD*%16x{6@|jRRms?DhJ=tcL9OA@c5=NpOFkIft*S~&z zxVaLy*>y2tV+kuq*HVDGUaN!fYKxT#+nP^Tl~SDd`4)gq^o$hBqZ7;xQhe8aH7z|` zNxT2n+o#>x1dP;lbeM42@>fsdOckTLd4IZgm{_N=pFsUiEWz~9{Jm{V**8J`-m`*`?u8MW(ydE&hP(fDPs$p%g((f_dVB7CH_!PNC@$P0?hL@b(3%xvpal-Z6L_{R^=Jvr6j0#{Fj zQKL#g)nmHUC7fruZ4))0tww{3FmmTz$rr0?0TWke(+_p(B{m%2D6?dsjV@o29`KxN z5YIvgYRGsIYI$7xoH}L(s<3V&G3g=iV%}#7A_hUHM!t$l2@bJG6Vk(pRSD%Tlj=vV z=gUAYlijCkSHLLQqU#*$PbiEot|!TD@FIi?ad%hvS7y5}+~cLfsSu*Ce@2PT*70Z6 zOHG&Ltcz$kwo9yjdplY6rwKetTQT}U|L!f$=$0`H)bpt1NGnt=cU0(6#T)R%@;(wc z!mHfxKAz#J`#2&&NdY^V-;+sf#4ojC{blBx953)h2Rl}%b=q9|v{FLvrs^1- zkZEGKQFTXz#=gB)5Feg(q97d^=e4-1Gruwt7jcrPzB#X+)7qzP?E%T+d?|vPTi8*8 z{L^4KSMtRBT}`ZO>4Zyyjz)e)wLJ~ijEm|=sdKLM#>jqqzEHq7=2gtyy9<>6y!t|)MX3EP$~wEGhqMUM%=bJ(HTc zwY+h3yODF3X-=!E&H6-6qgZuJpX$cNm>Nd}ud9@oAu!Ey} zhQ}G}wIoK3^WIoB%`5X-%l{-HUhfxD+?&%N)+6BL7p+`A>zELGmGzrrH0Jck*SO*` z(_>Dly=BfczfOEYko*o_KP%Cf1$Yb7$^mWu0aU);DrZOFA)zH{u*UG_7;?jGM}Jhf zu&7ABihkReMP6i(1~S&o%toYO9ii%=mh$A2b>t&tr1bH^r(~z!K0|m+=7~v0+>=Y_ zcIq%pB649`f<_UPmVOM3Lh+1g} zq1j9KvE+2goJCrKkF7R(k8aL-^QOd-y*?4;ub6rj6%vV$+*2eAElp#6#>kf6Rqf#4 zCOs-sa8Yz`VR}u-2%csQ6U&O@pgfAs0X{yUCXnG^f-n-9tXi3H)31jBO z0GBS)2!Z;iAOx=7iBPPW_-XCzsMuVz-PE4yGOHX~jV(&4&+Q%CX!c*t+FZ@L*V$Xs zQ2#k$(?P@nEmzQEQ+#UO7`18Ok*v;Md?vy?ZqD;n_x5eMAO?-v#N1!G&){>&!9DLN zgj{#9cdZKT&`1>-a9EW~l5?mk2uFCY5HeP0OEZW8VD|Ni&|F)wN8!WyOWPSJK%4^6 zip3X%dQ}%l&1t+{u(7ov_{hMjL;$+#4n}Ts-JDoy&n{u|kjri8c`sR}Onwqr!nqQ$|)$eNA>_KiH@7}(`00&8L+qA|n4U~q~+bU`s ztNa-(58I*rDC!QZxXb&IS_E}fNtp3FXR>RgJV@Ji)Dln`*9JI8=3uj8sf~B7|6+>MMRSVXX{d>P%}W(Rz|%#=4-dov=$) zBlQ!DzCgL4cv_2KNgFeZ?#<`+S6+!#Ijk@a9XiLI_iQft%GD63ctUFiK$l>~zzA-)?hp;b6)1gejBI`nI|7L#t$MDSp>7(DeEweae+#M59b#cYR zFDl4vO|gFnn(|SSVUWVqG^w z9OF2}Z45dx4W+PY=#X%G+k9fJIt-Y%Fn6j!!rJXbDYfZuj=g=xehj`~&w=MNnANFF z+MK4EvFT9*g?`iwcKXlc>P= zF(N^G7;wW-_b(s$OT>@E?DieeQO)S~|8{lYE_8>2a>vdCcD~0AX)$kl8qG`KR+4qj z(v#EB?mut_2c0Foj_lgDif723r@;j>@E6khg{j6M8zs_pKX@K4h*V2ZpFcfKcGto6 z`{im;P~eXnNpwa4l8QZxgP#O{p{;-?-zm;NKYbR`hI^LM__t?w`*Jjb!YatfLG?*p z1wtJ(;Y$6ZJqJik=(lIL8bhj})c2Nuzw@WcoUz*>yj8$YMe_ewP5GrdFcVOMoTdFh z{b*=Guk8et`|EyPUP>3t@BF$tp5?PPSx~+*90jw6w}G-nv!B`57RJR5%j4DKWWmI( z1#6g~T(+^;BaZc)1JK0C@yi9YON5?9N{E`|D8Nvg;39!Xc4a685Tw_(*fMgv5!qP5 z_aAqU#Q6}%v&xAJL+$_`t4ZKPQ6sU%lC*$$;JzquZGiCZxjLEzK!V}Ej#C{5IhEV_ zHP?sCV%>%%Rw7dORJLWby}MqOS|U1h&EW$T>rlhMT%#NoK;)Py9c%S(eZ@;)=N1PM zzC_*|EKq>;FJu{&@_jpQhpVXO^6_krS7hQ=amC%C5d%HYYS zN#WUk3F8WgtWa=aC`7#sgekDK;Th%DgFc~#K ztNI+sh0SITi*uhYU9th(67Z3e<~#`Ql_TW%Vq}bycA3iOG7yaCcmU^l>=~(Dgb&wu zUWKH`({rW(QIaS?E5p=?&q5&!UnY z!8^kH4rG{AF^H$z4@(R_5%ne}@ZFzruv}=qUb;6%+~q#Vc> z!IjCsG8@B#y>mp#25d-_JxX4G&3^V1s}BIdO6U}X zH2}^0XnpGosO4NR89T5$g%q-37KfNBo-XUL%2C~jb6p+?nCnXq5omo#F_W5>x!S6s z-qw`L;)?~Op+Ov5qTI(YU~uT6yv?#E)=%W|yNbU*o-}y+%INefvx>S!xc?&{ty#^MK$r+4is;(6 zsETVLku?qeB|}4{dW;VMfsjHBd)W^oXm5SRTviKLlbXyWnD+DdfSGE*r3*GgDHq7- z)`DMnFY;?M&4$5RO@!|0QnEr~uCzN|y9K*RbO9@W%8JZ2&TA>k4C=RDk}<=HPrt9` z#Bwe_#`C+hd}V=P7|d&lA~6%-IW+I5jJYHYqOwR0tN z7<9%$$>;9-eyLQr`z(6N*A;F2s}IWD;qUo!`xCQ{fK(NrX9f+BwCD0n$@=_LR$qR0 z-(sinMX z3X)xD)j*eGhZC{R3z&Bk+34^!L%}R|XYG64PBXpB8G`jG7s7Auw{mj!w@TB!nvl_i{<4sIs<^bF}A;cjK%W?A7Ww!PFnnRBr>!4^$Tb~s)Vi?f# z#JJ`BG|02&V8F=)3cIzlomM#whl1E-#~N2s2ZKk9qt+mzcu)%pvox+t4K$pq3Dt2cRp4~H zxlsw?NGbhOwYcYMdwp3A9BaL}Lz{}5_Q@9u_uJNPl6POVpBoOY*Y8W!h=FkzjUeU;0r=aaLzD}kUB z6V80cF)!Y!My0^K6a&ojXl=b@y?EJ^rhh3av9W>C2{N z^&)c)!yoG&I+0)hd{L|VHNAhMVAJY-0P@a_%Ioa9M<0P6SA8``7{8*4mvI0TY21^{ z^@FK+8rOV}*OxF>qv`bf9db!)Z_-*Bw_z+=*nL|*5n#dVLBrlPX{R@BP9nnUi<#*; zK<5ZQO0(at0w|W7W-&lITm@?mt?xL@oxaJ0Vhb-w0ZPST4eQd?u{1oZnF2GIYKeVh znN&EDZ%eD}7egE4yap0RH)718(N`~OJskGG%v4ro$OGzScx)!yw`=;HKqfuae(f+t zwEY@CM=Bt-OJma1iqI568Jk~n$E-O|>EO_q({O<_ae2^TkiYaRd*-~($AgE9m6nB< zHd6QdYM}1MVxEq2w}d`}8k~-a%EbX$Q{qs(@+Iywl0Y0ZBT@u)xAmA%-UvQrnQ}(Np0zB4PH&`tXFk1|hxuGqet*TvNV<+_%x@LvOCWG0!}Z>pm8% z+n!yHw~~{evg8k?aEvw9)4wVwXCsl&xaPb;S)}_yENZp~z&&XVikWRZF$Q!MQS;dO zuh*-l%BG2{(E6eWm&$anjya=Zk{i@FznFLH#ZG>iW7%r48~WwaI$?EVs9ep4HA7gOp(F?-3zgT;=zf3vm}cI94`lIIQ(UMjBR_0A9(Xn;}Hhl1+*^ebu?MdgjCvdC}FI9&AWaQ2gCk8dQ6HH0PyyU1*KD0Q1>nF$4Ot8-)|hqHcvB>m>pZw=xF3PtDDhAzTnQ z?|X)RDR&}UE(CJSYF&)Kbm&PJjDBnpl7Nyvb~=ucLo$~b5t_LSz{OlB@jNy6NdWq;^UsN$9>VZUbpsITWn~HnfIwtp%*>g ziQ9A;yJq*nz9?>K*55Y9Z8&lpgrd;)mfwer4LAbn4A9%=cbV~1phZjKc^jq7h0bMC z_IWM!B|O&Qb0{?%NpEE{a=#g-aKFRmwx%f5p~EQ<@$5K$;x*`{NCHpkG-0Ae`o;mq z+{Vkfgm26AdTBUJP}xDZ$x(-EAI5Ktk_Qci*fdsg>$pkkts03Pr4aveP~l!ZdCaxl zcMSHRkKdsW*>-{R!1N9~WGRIKma2%d?+MY+IAy_|D7jZ%hxMC*Dw;V#+ULFW_R}sL z%~7wsW9ZNGVe8IkWIWTa&wHVlHR>;S8^)9Z0@H>XFUJ)?tlCUgDr`80O=%>L3F`ff z)#j`Snqm?a>n_uV1>i`0a@q8G^p57pi861;GN>2TI3t7_3NBf9>38E9tOZ-}iCVdg z#GA1O8!4gkkvN`$Wea6WT&qy=#MMMs^E#^MW)h)1V|X>!jY^TF^J;gIgpwvzEE~aH zIUq&lp+O#GK>cax<}2rMDTl!bepAs+-OH2KWlxHyiuiZ1g=wdps9+fMlU&-1TUC6= z(nEZ>GK_fFfkS#s&Qz}Vc?_Ykr?+V*GS?XQbyF>MO9F`z&90eZ;8n9+ZSN$Kbc`*$ zd4@+Xc5%e{_UJxAz%xWoK}lj9@4AxWyiSBco(p*$1+zL~Qz>g;?%Hmvz`r+P^A4)I zrq3B1I@XdD2wKcIhJV87(0WJ3eAl#L0l+ z{xS1*S-YmQG?ACE~^9=j?-SkCR(5cHdqH zIHzC)nl@eOn?Gz`LeZ>un&4$XgnhExoz-Y_!ZY%I`Rs{HqXW_RYjYRIhYp$SsAjW! zuJAs0U1pTS3*by-HTtD4Psh>m>sDYkW$dE&>cI(itLF_qE126Fn<+G`3eftj?>A zPBMy|S_i;nvcrFbTAde4YPqmar9ZES5dFT}!FO$GS{QQq>bF{Phhokq>iyA%n3a4E zS(?*1mo>*!3SL_{TnZOXLy5+-s-I0>pwGH1&o=eK`^*B}NQ_H=;^mVr$ePX|5Pu6y z8yg=@Y{Q$`s_h+PwE^(gl+whMk!h6Q5!^{V0MZs2JI=ZLjvPe*<<&5Cem}1Djef5I zb^t{@jI|gPHebklRoaT96v(|Zp&~Q0JhAg@M3lC^9_Qik!`l8kkrPV#-inDQYlLf87fj}< zaB9w%8y7-LR)(`L2|aV1eHT(2Ca;Fde`$F}%ejfF2rb-4M!@K%>!3>WZlDi!Ohu+fb}0*Yc~?5zerrLwU3P)0%2%v_J#f6|8-X3Noy5E-1dfvT01X zo>Iniqx)PC)&JMoP@V7Wtu~Ywq`3%{&`CF1$`| zWvXOysM5k>u&a4mKv-_6!gBHuM|)k)H8Y~)t;vDO6LZX_MITGn8D*CbXs~Hn zAd^o8NS7oDblFugw0H>^IwOgnzH&!b2^W65i?;5{!OpJK@L01gNiDPxq>;Jx{NX)} zf*rN`Hf)P8UJvJ;sB#lBoUBGWB`+i8(l=-8-L=Y8M%L|HWaahDFkS)xdK2IB72E46 z6Jzd#i!FTdG9`{U-cY=Rx%O;5>an^to0S0M@@-I8NND)zNQ$0LfwtC}P9Rwxn(0Eu z!O6+UdeJ%K2njVVkQI%T%T|VLB+q58;u(&si?hjkx_@N`;_v1|K-Vkz266<$KS z*_*H{LEYY;U`Dyns@{hd{5t=!UM5u2^P%p*WOTP_OnT&K zp2!W;#F)>p(zM%G>ho;+hKhTuz`f{xJlQ3-*%$H`Fbi|GJNP1(3wRmt?)L4eiY|~y zLo;L=(A28jsr5kZ=*+Y1u$ZEEJK$_1J2Z9HZ3+!ovqZ=U%3ShX|>waU)H9Nu#UvSI}gyV3QPCETGkJh3tarO9M*IQR)S!`4*cPF{p!!R{zG^B_13?q zE&Y{lMyk!xpEWmkDzsPg2 zmIi7G1jHRcAOlL?U1;K@5vb!Y_rYWTS2AAIUO2ZwQa(4H!JT69Xd&mN!6GF|3*E}a z!zh4~O^%r--D(#OB0YdQiNk!IqHsoDN|9W0h=mQzh4BIK#zmTXLdNdajdgf*uYCRK{>`g5o5ZD=)HJ zYw9J$e851jTRRXwF)Z)f^(?!7y^+#bisByTiCrCUQZLBB_Z2zih<373lFIo$0JIg( z)LW7*sROkqPeZna1O5dd-QD@#q$Ot@aPB_fOQB}u>bVXyppD+q&2?O9*u({ZpO&k8E`)t>W%ksOUuS?g5O!Z-|LuEQ3)QZLPv;vIE?+YfG5pn(`0W!bqBl(^D|-egjjEJm^JIkz zft;v-5V?+Gm+9=AfijCV5fbruJJ}8chp_u7_UQYp26ktYp~pcE6D8<^gfE91vG>r zxa?~V=G_nAbiWm@^75qlXhKA$=HnW-b6y3nOrA+_)A;~B6p(s(Xeym&ENll5Td5pZ zp=gaZgK%YABcOXbeYwCI=25OJ@c#S;0muqPEi^dL5}7bL7XZJ*gtJN)ySw|QRmJ;q z1>k(>93J>yBsb1(JQ2m*>f%`kz8KrOhRj7KUvK?jh?iQ_wpD~^cheOhxap>_JQp%y zK!bF_jDQai-6_GWu#p(@cXN+SkpBQ|ZKMp(jH3?qEe^X1GACa@w&wM@4Xh~U*UEsPu5%DdO1nxlxLAad98Zs2=Z!<+EV+y8cu*42%)rR>*faL^ z7p>FD$bErvNtexa!x$>UV#Y2)LJL4L8o?v$e1)7g;yQGZ9B_B*JwCP(=b)7fdPnW$ zv^MHO>wX~Vvy&A*4ZGqRi8##41{5Bcb?d;J4Q@0Uj5I&f9=ym^_e?Xsb0Nm6I$>P` zB9%GU-MEXA&p~Qa4TjkDPqJRCLk$IDeiv4Eo#%G(a^N~mHo^K`T@s0Ua2OQ1Fi_udRqrNmiKd;5|m@8P&&|o5_Ml8U&#Uk5ur8C zAD_UZp2S#@!k=0A=6vtR63^xWTdctT6F1JwHtv5H2}#7=bbkjb$+Q;LKM1$CC#njp zv>4Iu-nLqE-O7aE!f2){ot%{9@*2Q0oF1Mb|8za@+k`pQ)V!pfC>qa<+%4?lD=Oul zu(J;L;NIry{enK{D99j><%(H@Cs;81!jXzpJRjUA-dB&Y4iCLHI8fslOg=;3VHRbr z?aZ^)Vb|D-B0YnbdK1F+l%Ux6b$dGq$6mFlNoC|8ceoLDA5;-cI$N{C+L-=%Hw>?# z?lph15!B+!63EJ>*PNAcajnx(7cEeb4Cn|?@>#B=6BdAUM4_a$s?gOnt?KI_za%h6 zKJ@eizl$h(lup-za6IhukDA0F^Z+UOL=4xFV zORU#om#Nb8NYBCv6*5;4f*Mwk!<~(NwBIX^qK~r+e%vw|=ZujUIU>6abUuDS94~9P z+t~et=@}wEN5l8f9D-2&E3Lx-X=R+a#*eCg~-*8r~vA3J-{Xo-_6TMqANF zUt#DSxyZ{?$d;$_Ey7i#j{1@PbLsJB0AL%%sDt7GEd}Lh<)7K!TB+poyi}Taia$Q=ERm#(>9fa^; zG!5QUmeq)~fyZ9NljVc_r?(mgWdD4|Qj(l|c`U{Gp(Fw>&XIf5!X7VFjjPp)febzi zCy4mCT3_eH>tqgxt9=0tQKpGMkCk3JHgb}UIhx}zlOO)}WpX=stEdrn|FP04ZW5fm1;kKZ-@I>)CdH7a`4N|UpyS)eTIoo#=)O~J-jR( z1gS32Aq7q6=(bgqC-5=*_UPvYh)M&5=V*RH-zf8aH(M_44rYa0j5=o|&qA0SpB5k4 z(n)oeWtDqaq#-J_|vo9KyFc^tt2@B(PmX-=aQUi z{L~em?0sLym(~e43gr1=0Fx?8gEIppq2B*z5c(Zj!sP4hKmExl0yEKe*Z7C+s~DyvnHt&PwvG{bstm6#^hed}!;kIpF$ z7GS6c+4yVleJqtP8Q=El}Pul>x zzCHLEp9gy}VJA88hrI)|#&Q?A-6t$tGuIHvkBzryM3IUDJ7YXnu%O$yU8qk9B%QxC zJ1u&q4VuBElxX51i}kE7%}B)RQDe4qycf)R`i08~!=cUK58I0;-xmOt zf%PsVUDXEiY#@EdD-8Rx@<^pxoB|1VV6JfhwilQVgIdLA)}6tchL6i6%eYkW&n(bx(CzX$N;UU6-~y{d{$p~A0s=>fAbVsXDtIIS>fR_JHc~MKBy45r+0;X zrs9rYQmi*x;g5HF^pMX?lF!VyR@|Y09n3I?^DE|7c`*ahZVO;1yjz{aAl8Wk=KXcp zP6LJm#=D*Gu4~D|1`(J`(B4#c#fRwtwJ;E%z&QOhumSU@zpDgC3gm!`XM$e1WagY% zMJ5h&S?YtT`ZAbG!nT3H)vncl5FpzaK#qoE=L^yxX^@QAuJp9cpcZUf6kM(eBD+N6BN_NCF9Q;m9?Q$(C@%cOp9BQ14N9OE4(85_1{2s>;}(YPEP3+?eTEOyR7$l4EGBJ0Fl?uvO`(*I;<3c?jOAK*Ez zLN+`kRJQ?m$=nJ6wkD(y-#C5Z5|~@kOsUg)8&h7lZldi;W! z=lP9F;3-e%-9c{WL0bWQu?wJ_P74asf^vPCH!p&=Lbc^*nBfHK3$iQMIdWi-!I+h| znh~>BAzUJ2F^@#3ERRKzf{;h~infBSgy<&2Jn+pZ>trq-A6IAEB~q(8x)^!gkd3Y{ zRMF<;gP;^ILN^V=zSe|K6~<(R7D*^)e}9ejCyQ2$YyMC#C%GMJDN2I8o}831i@xI! z<+9Sx?-XS+wcBe>v8=xGS(G=eG4JQFY1ujfE_q4d6FMpEQ;a*pY!bE2DwR?WXV?d@9c3o96t|68U14 zw+u4CL(yn?z)>Q=tBBb<7_c3R%D$b|Ahx=^5MNb$Vv>l|di)_M!rJE^O`_LA= zCIu|v>6ez7yiBFTlZA(9-6k}Q2-gER+TVEsch8=!_rMf}w{!;Nu22T2)B<0dhU;D= z0Ra+d8Uzl66%FpPR6z+K$EN|8K-c~Y#=~-b?{#U9&LOSzs|YGi{E&_y1R@vC{N+%} zXjlv-zs6fEKz}Riz(W-UEc~R^L6QL_uR)lD($(*PWb@7w=;M~*yL;?3ZOVf^rB5j22sMW-=>jFklAGDJq0Cmc{!nNCZWS^ZPs5tP6{9tf z71PF7A6WSItdMvhq9vK4NJ z8(NbmU2qk5+>!@jgE9!?u!d5yzG^c7EFuSH2$c@w!aG33uyFPVRtq&K6L+aRTuMHZ zCan4}-W)(|P=#P+AzdR6U|WPe6yq5MM#GqzU|_9;F)o$ysRgL_i|woJ6{$=Z@|Xa& zGN6l%(!~^iaKkBN&W`LaxEhIkdaC_WME(hr&qMx5?iDa$r9ci6@xjmC-$xFFS|%Q~ zdKF{T{-!KMIrs1xvO*Ls8_R6X;XsvWgFxBUTW9K+Dn;k7z+pj1lzUQ|?@|gTV3&gOSF>q;K zA^=ONdg`3jaVdE()9w*qwmP&{0X#iZ)*pHf1KTgWpKV!SziFBvLp|6LNP z-AW97pY#9dv3H{Xrw;AUhyU#vDbOBZEQjmeAb;A;UNEoqsxvjoW!`-iWG(6s2TC7; zq=1`~M1PXyaZR(p?Q6hpo%p{DnzLWI8$Nw#t7DSzC;Hd@~O?s2RyrrHNcCf+N3aDb+D@feS z^EMb#YM@wcsSCIBRIL*3=7xvM;@$l$a zd0VN+5|EIc!FI#!yV(@d7q0buu#1Y-Ac$4UeyC>kekA$?9q`LHNDRMtYeBOBwyN<5 zho9A4NZ!W89e?;U4VR}KAOH;j9ckGuuVFW1C;JI{#6yn-f;nwKo$d02Bg6{{STu_as43w?CDYy zbP(3nS-j`JC?v%=>**d_-d4eUz7TAi{lG41Axl$OqHG0xiREZp^fYfdY>r6V?F*6? zofEU*C^U>e={aAl*uK))EP@a3SUrm-Xb-uHfoV0gUr1OY$JbQzQty-KC$wSV30^OlgE-f#bEnS;*^}{rL7#9q=k=H^S zI5=?7YrU=#0Ki-eveSX*iJ-CB3oVCaH`nK~1Lh}t3bvD-j))IgX<_GYJT0gUgu&%`Cu3e0nSR;YQOys!2?j-(wwk+|B1bTLO`_-aw`|5~~IV4Xv0# z-J-oiM}0tU3=9=wm|hq$jInFmF{bE+3XbWBB`V*4qe(rY^YJ>2s>zEdvXwUoAk%V; z4xoS~C8}}*3Ve(=RLbv=I6~)eE#(}9Iq^RKh)!Bk_VE}6 zN;v?1Y1w$fysu7@z3!vNbxTHBWh+V?Z2T|}@M{k^u8|}Wr@gTq_HV8pdJ2db^492E z7a;tCGblF%edIrLj${o<_JIrz3{fW~A>_-niEN2ctHD)s>Dn|lhq#Ydr`m?ZE==L_ ztf)sr!^CF5$rX5c!RH@zUz8Z?TLb%V@SPHwLOxzMPi;FP^}TM=;z)Lqcl z<)&MvoOa#o0n8?#V(p>2yI=116uyfzQDT>8)?pQ&`S5UO# zYlk>qlz4}hC558Ma56Zk8iW5>mTnNGn6}t=){h#<#3N>GAx;2mL|}s&MHMesanRB6akwgqP+$YAL$Myu3nH~6`;Jqemydlc6j`ur)#sCums zKs(V95*;V#9N7ed`o+WSYo0z95~O~la|9BP?o~G(sE-sim>C3t7JQ%}5wOpCBFwer0t??c#jTBt0!Ej2`yM)z5_^_CEWQO?ClwN+Ve_~jjm5{D10L}!{eK*T`&68rDkg0{0 zeFNW4xNRDU#9u^=(i*DXGZ3vq+qSEePgW0rk3$HtzMgXvIzA#Z2B(a16V;<{+TTgD zQoR(BH!3dLc{v5@o$2l=y9^qD+9fh29^jwo^1>pnku1}P)=oG0_2UIOR}c~lq@Cif zLKscYg`}wjHE_05pk99aKH}w`wGdeS4xyNVzu;i#;;X@*RbroLf`V!39>KT5dSWlK z&oU7&JalxI0aHG;{+e4>R0{<0q%08P@+PvA zqV1K~JIX=cwPn|hF%x`JE4Q*=t#u~Bbjmh#IVPm<$uTw7YM{i_dq$<*Vk35cZ0(o_ z7BBVi0t}%ds${?!XlNF+Zn4*mJYrWyhqwnxErV6cHa2~sO@v+-OzduxQu74Vr@v-X&)kdR-FUPg2w@vO#G5a?#Oi~!#^XG8ag zhoMjRPt3x{e$^Dgf#{pv+7xzG_198*kzk5jD8r|XRgUWD>9`Fm04W1LmL3r|+Z-kn zYg(d2De~5JMPwWJRKlta2;FWas{3?|JoND3OBA~L{h^P08)Q(tUS6U;M$}3tXCIw$ z@I#pGuT9sUsz0;%QNk=w?4U6LDH8B zyjZ@m?Wn-#9$`FOQg*=*9MlH`)O&@(E-{`@<3xe!UOP5%gj|s^AK7UW(S6?13h4rk zPnsCeEgPra($zH#m^JvdeJmjKQmZdF9w`93DWAPGI<&*2`dNe2gc5&-X84er>zQCr zGmM{1cUz>Y`+E5G6#!$30c*4=nf?5+Jh6W6i$OrWwf=CVl}+5tFm<`~B=@G7(R_&v z5I@|gCqQDJPnPWkiSqBdmT*uYx9OY~sB`{@`nVmhVh;3zlX7hG9SO>;WUkzlxcaf) z@UjND6(7j$_;|XyESb)=Uo&}{lBJuNk?Ip|%eEhD0bwY@L4r}J9Yl>7{L1t2a+FS% zQQlkEHt_Y2)o{%r*YL*=1bNDe0HVvD0mN8+b0#&WYkstaN2b{ReaaKR2CBD-PkgP5 za2H_+uIyrel6sbbHQwT@FSNyYCb{I4W~A> zcOEQIw0|g_W467b+!n&LwPhb~v1=7>#QO`Be0{_ZRDr@A@NJ1i9m$wsi?6_8*9G#A ztJ8_ahM&up#yf=LM$|OjlvG!=Ii+6|;J}LUPa?(w`$58{k(sfuPj1;$yn1NB5kYV$ zFwASmyW6V39Rv=VR)+Fb<-I)3>l^c0=0G%nkf@W=Kw70ERf4Kro94I;Kz{fej*0m( zKnActhtSonz(mTW6^d5+)}WdsQrs4=7$UNw#QDw{EKxI|H@%+84F_~Qu8zazilqGs z)%L?5UxHP^`jo&U6vZa2Hht!eL1R$>p zGn&Ku$}U?Ll||7Yu?~$g3?Z%CfFEkB_s*p>Yozm5 z;Q%j?Y;y1&pXO23fiNF)K@LzEQ|}NQccp!9bG26SVVT?16&os~=8R~gbck&1=pjC^ z_s1NB8taYNCNoOHzB~EuIX@1HJa7MQ*ciEWob^|J2>Sgq&`1!zU<7`0|5*%E;+}a^ zzya|mvc4r8{CYPj;{Kl-_PFuTG_wPt$!o4-)(R|ok=D&?U$q$ zsUemUTOLJKgv4XG`G0WptwiEeh3`~->(@3;y&c=Pl60+CNM%mn*m_>V2^;_c1he=B z$@FMHmQ2#(7KIW?N57*b`9R6-t|gDR&VnaBBjsCNPW&hgmS_ln+}zCqDwqC`n@LsO z^w$HF%DKlYklnV(6evD{oNFr)A!QAuT+<6%Q>q`8Avpni?@-K)S*G9an_(|sw!gZq z6n_|zZxGz3jqq~%`9=`ffT||MDQ4BX{4m6J(fjts1AUiX(?(`(qd7UE$6~hvkYLAc ziv;;^=A}n-)iS!XYLJ>u>J{9xUA{WHrfnk#@4WL^jmC9!^xR@{W2vAVNXYP?I4$>r zYw3H-wlc(%G7w{@)f}TNXUH)RtbCwQNvDWLVA%qf?g}x03(YuOEza>S@_XMLC5dD< zZNYbv;&VzPd8-}jfipS`&}H78tlwRw8} zB6c+}_)eV10Nz=a^B-e$3@jvOh6+|gd-J>F6pVqAdWVq3p3C1F_j zwCmJqw6D}<-|X5xbptNI7eexux-Ut%ts2-ZTHei0+ex{X%=-Xkp;8#1j%{y|-HLl; z@?M1~FWWJChwFQIjXtZBX56c?IopxQL;!_z5(X>y_@S8@+4uT>Fj4 zwC~itsYE>{gYKy%y!fWA%11xSoc4-glKL#R%u5bHJ>Vq-{|y|$#H@jp06uHOj1h#+ zU@{~zv7(JW|=fa9j-tb_%@g_>5A}`kf)R9%pJW4QJhh`PLLa%9J6GU96}y^ zd+}-TDo|_+07ox46cQW^9IEwf)KJZ9t?JNFJ+cM6aEM<=Q?pfnuz&Ws%BMpWw04=y zxb4h*y@gIA^QgzNnqsHPv@gHsGvNAQNDm#5?94|zoZ@jvE!(i~txnN;x?~|={HQfQ zZn6R#*nbYeA1jEy3i%pnIJK5EJfqexy~t2KuYS_nqSm@#s;|+gaNh9^#@?+JpC5LnR^)=8_t6R<>Pf&uE7NBOOz=-lKe zaa$$dB$ddoQjyJ$;CmpZ=63qB)Yb`o5{lJ*1-2HUq8eG{AeI|lvnV{ItLL0ZdjYTV+ zE9~8QJ9u>yFJu@NYuoc9+RitXQMvXO-o^qw2qf1~7Hz3j0gO>@IlZfD=%>PnvJ$sB zGv+k&+)y0&^JsTonv5#IrRuTLDe_7sw+Anp&j39H$pJoq6CjHR4(L7r!GLGTrnm5Q zXtxleOd2Z%U3Pkc$yR(fpL)CPrIX6t`;c4{d33%b%6q(YRhA206>MlaVM7c&`lpS) zv6;jX_?)cn?z_{rp2py@PA79?gEzyh;iZ0o&Bi3-4o>N1VB~V${;vJvBj?+R{&=!( zvu_?9$FzrlG`HMO=9~I<^Oo*glj`yv$%k_TU%k;hu(h7sv(~%C3owR7dPP%LZ<+diqMom;&uu`Qh4x;ys@ zm)W+F6pf_Z^Y4208W()LsA`J-;v>K-ZSt9Z!Hw8J!T*?=(Xs$|qMyatJd-4VMD-l0 ze|*1nXXqDFUb}#QHBAnJtsiof;JER%x=gh8imyWHqA)f>DatvfH#_;*r?hKu)O+2O z_5U??)nQR}>wYMu1Ze~*6;whcL;-1OksNYpX@xNvN~Q)k$Q$%Y?Y zDP_G4&oEXTUUGU{COlAX(=u3MW}2GvhG&#)_?{%B57AK~+;K$<4DwB&UA;B!&>5zZO#nHLdbigKrl_w53g9set03d3CPx zsL0~Dxnne3S%$6Ky1_+$1o{$4`LQMlwY@7(Nlq^v772+hS4MUw-->c&hMb)& z>#O*wcOARg_I0FE^o)9Lqr}chSG>*F&1u74r%{9DbtN%`Lj=&2Iy(Gs(X0@;C%erg zNGDw@)lws}NBWd^13?a#yzjN1EKu51V1HC+M!~;-n)AE-kv?gQe?RIwwl7yxFCzYG zp-$F|S9d&@-dpSH>2(lXW>&}Qruc9sYQvlEX+YfMsK?!VU~Qc_G6%eA++2|q$fTRj zb^+k~T~HOm@XQsjkO$2C&WUIq9oYzv^A^9cdosnOO3AvjkOZH8Lq^4{db<=> zm&qF`W>bmx=1sR_EV0dtha}G!YxdVa{@DgBln$nW&gp{tafar?D74>>3se4gY!XAe zW^hyY2gQy&)ih|X$#dXT#JSw#;J?4=h@h_zJoH7}DK@Lt*a#r{t2GCW-|*7T=Z(hS zW8R$hq>iJH(d@DZbBQoU6|`5cUNy1x0-Bc1U*Kw7w3GZKkKzmOT(L?yS}qw_N4WwX_a*jr*6hZ|&uu3X zFYHGxH9Z6f$-AICVFI{J&>?KhLCeXbkMUFqrPqziQCGviv`FT8mD3W0HA=uT#MA_J`0g5~P?Yem zy4i%hQasjur%7Pw&E|=A3cBoz)#zQ6jaXq_J_n> zlwsDz%KQ5D>!}DL2%#z9sna(uT)4oenf4gWk@eBnhAx8tnj^INTtX->$4Zva}V|6YsNiqjcc9B{=YhK#k9U=9@NEJHV;&bQwXI=?Q| znpP=-e{~*{#$)}!hj%7~DCGWwBmXIeDQV1*XnitkX=92o#I>=xIZe{toN4o1?S;U;pc(jHT*1v=H|t1<3Zo-5zw@$1(=*-#{jHWjO>Jx2@AX6> zn<-axcl%ycAC_A%#2Ojjc76#=7UpDq%-dDv$esxi=r4pO zi&B8`Oqp6nZian18eLtL&Xm2^{FLY!A6E6dqUPd(YXjdEjXN~dlOIFWRtwCrj_upr z_`q@e(-WnV0)_u<4*gj2?YH<{BF^10?#@e~ZDj%~JO0~)+onGbYyBxg8P)3zznDt> zvKVFAN20|XdqYa;U=oPgjhYwL#MPjw!nN3L)0&48fxmYN$-97_4+8MWzB7f8Vj1KJ zWSHoD2#bRs7w8wn%*^!MH7H-BnF>r_?zdTtG;^_9*gURT9m~=#*G-#0X%)YChHMsq zL(W4b82`O>V9WJh=r7n>9HM2t!7Z4etf24-pqmnN3<}6F;98OownP5&XT?mB%j7!2 z7JdQLd2|HXa(c>U%=M!I@4cjF9U9&ow08>qySbLpC2~;q$HHk-fkOj+us#`dXtbMf z`puH5d0ZQ3Rnp2>ojX`U^Yin-yXBTJ+H+Yq8OSw>1RpBq0$rKyRJAnmWc{p2W7;pm zUlSXKd!9mLBRf+Yy1Qh#T_@@RF0Hwi&#Ms+kogpVg@?d`M1m1x7Bqry&h=y}ttJv3 zfe1RCiPlzCtTTD)i2F?F6=ut_z+1ap$K(lDDBTxzc7-yq$Y+e_*@#*KHZKm}dXpK%^-o3cP$)3{U znQw=uql}QCl-dAl_fF`}`>p3AZ= zM)88i>j0NSvrW~RY}=-9L5S9x-v=2t9A~xMe>hPy9-^ocmDn0^n#LsBKs%jWU}$|4 zDu0KC;9dvf1wXdg3))SosxpsV|Ovo8l?hyD@Rh#dUIfqDpd7olndqw>-}JJd7!l~9`WRS zpECu#6x%gARU~b`GiP|2DQIJKwY|w;?Q1@e=MnD6%1^;yNvtm2CfyFPDL69o(O{zvG^I+hee;djGixc%OSJ zF96h%%mvPj9 za0hUO%g6#4kN>YBs(kzVl~TRLqk=fKX2+Yf@pibjGF!tm-F5~{j-d`wK$WZq87Yue zH(#^3r{LRkf?J?(^zPLuka1Mz=XaMTfXQMO?c^qcUdC7V{hEAtS*wr>-;sQ4O9bn) zF_WgmbPcu(!XEqK);T(DVAeWM{`gEQyR>^%+HYI`D_{YZF_J`|nzmEX(2J!hvGn;6 z!0y=7yxAL)N;?P5pecX{9i8eB%H!H=I6ia@x`x}|S|kK2qCCZZsU#R-e7F2zc?~|3 zLTN3`yJ|oAp<|>*Hon!Ez|YE$kWO0%yf&Viv{Xn-8ZrO_@l^$UkM)9%0G;5S+Y@{{ zr$8s`)QKJ^hco}MaVpAuE`m?x9VEf!b# z*f29Og+o~Q^&Pr1m9PW^ z%~1W+ZvE4d*y}hD5v*Ytvi}T7S$qU;Xh5gPK*)7y9xypiq?1}Dyw;dqIf#FCOhpYn zc}NskxYdW}lzFV5OZ@wLv*t(X008qfMN82)o0_hgubOidd4e>!Lt}4PuEDyivw~QJ zOkz_~9&+5Yb<%|*ML;SC9qESY(Vy)JI}L&F=U>~j*wG6llA5;*%ze+-2?UG>FpuP& z;J8k6#uZ(Ba`(*ZFGiC{Riua=5{}kACj%=9MsD)+XGCr1l}w_fSYEEcMv<--)gb-D z6^DZlVVvu3HoZU2o^*B#N(xHZ>U7HB<+>2=Ra5vSCV@ijrIm%dU+9!*eT}afDUs=r z^<0NlpO${_K|u(hWqYNaAxD&eP^qfskKrrQ(9jSiLBeV%!o=*+7r;vy6%VItcIRj$ zY}fMxZff&t;j=2&Z)v5R6~u4iFm8*T^X@Dd#Ny%=T#s;E`f-Dmoo;&jkW0LO)iWD& zrlL>FpWQHY3=Hl-sDfPE;ip_yBbE|gF!O+*>@2AKtbA1*IWOHfHh@2DCLL>Sj~E zO(n}mhX)%N#dykJBUFYWOqQ-6jx!qk+Ka3Y87+f2o%qZZsh6UUcUxq~t_hQ}RwQk6 zNTMiyy^Jos?-P_H&Toit>eyaV3=X656P`O#0r~QWwt<1HIO`0Xn07=&&*&A1nWhUF zzw#Q8HZ_LFOU{Ch>WJgR)gU%oO9RFY&>bkq=(h)T5A`N$0|>Vos)puUlgRNeUa{Z3md<}zAp8VprclIMVB(Rb`jjD*(;43MYA510fD z`pHCq)-kTMD@Yt#e%!iADKbA={5VI$S>u3ZK2XeyT!_#0vf=)r3ja!2Vi*Q5a)f4?( z+3|YgQkYs<+rzzpM%RfSd+WT66$%qCBMxGZgf`SXKvhh5$a+FQ!qo7&9ZC@_Ci%!sGX#kC1}fu%uTiXra}KpLCYB$JvZgzk=umA0Ln@_-HtrH;9RbOUlYRKD+el zc^!N*chRK*Q>^BzcAQ+nnk1s z=9>9gGxQ5(HeQShS>F^cRq1fQl*Q2UhTRD<@Dk!mEU4!p&|O;;a7Zi~7C&$uPiABPS3N?GAh)>t9dF=Qgc)~>RYGIk=i};o9j=grsng(aF8g}YKAJj4 z;Y*k($^y&AH5G3j8e)6y(w3G%_5v4vaF3nxESlJ&IP@) z>k~%OI(NtBW`dwkdlFO*|7*{H+1`{cbwx`0qct?LgjF! z$DWP1{roldk{#!5QLZRv<%-Bc{C7`@2fk#9n;=}|rle4H4Jm|MU~agelKo9Sags~A*kA#5zROkhqW|{-rk3JO!tya}iGj#4yaj)e@ z>n|Q$qUTd)kXqtpw&Cabn}{sb1yenzJqy{*wS+}^$0p!?(2@+11r~dkkuC8Nnfh}C z)?xCYJa+h*$=Z2e3-7A zk2N}2^#Y(s_Cy29yu9C;Q$QYT*OewcLI3^>Yltx`1^1HvHQnj{cW-{DkZN3F6k504 zIJMo_R-*9f0uH#ZYfl4L`YOb$k?KblHCABP;iBccNLevae6Y)?8CJ9m;x-Z;RUs9` z)!PpEH1~v(_-m#;$F^^$aVpsT@uztmsvhl7K7M5~upiJP%n4>sq8Udr`Li4x9N~w7 z`y(aXGW{Ed2tJD@F{gf2Cw}LF3V#{W;mXb$uT}GdOGex38Jt!oQ;O_h7v>=q>xk%O z==0iN4LU}f>O>OP+qN_NWuKBr<|Pp|+hRCjW`-<=$ze`PnJ7tt*c8JNy8-biP`49wFcWcBhdn(T93sw0V*rd^-K9&YI5%8D$sl4SVtVP{MWJNDg7^5LrU$ zfrHC$&+mergNMWH%Are`zP|G5d9v4^cD1R{kd3Nm+^oA}85zrlr6IsN!VsUQm2l0B z(dI$#X6Saj(P2t;5of0Xv4^|}M?%%__w88?Yrx!cb7rt3@vwkP(z==6`9{OY0Gr*+ zsvD!_hl%kL*+DvgVC`bc+KTrNB0W(>L1ayw{B_xdNV33y)3*BCaj zhclQPq5}1f4_{zp9*t{P-9VYo6c9hMTkLW6;6mz)_F-0GsKa@NfgNS4kB*6Y-hIwQ|_wS~O%U-@i)9_)r;1`P6wLFR|-968*Th^*L;Va zdzar3mhvxdeIBD}0I(}xT)@78Xsj{zmKr6#m2 z2>MlTomwT&m9de2`s&SS=JH!q+*g&a!^dTm%y_;oU?ZoV{4U_~)oln2*yuF8sVsE0 zm}(h(`ylTYk+@9hxx6i}Lcf|snk=Azi%AF=U?kd~iK=kJJ!88_u#L5`WzX0UK5oXP zaO-M^cIktKOIhYgCR$??3B7BBiH_Iw0=$aRdO5)d%Kp)x!jcAQ`vc>Y&Pn(|TpUD9 zq20G!CJe;ZbzB*w4j5w1Lf?2IN$Ub6@^uRZU-`DLzrpNmYV=t(w+O$FFEzNBbGS#dw0i&z>yDTk3UV5mAW z5&Fc)#dVCsL=x!Ul%F`FY7zlA_%;qvK`6S5svckjsGO^9;gT3~bI< z&V*dR0m-5tOF3Pv;G#f9LpJ`q=vCJ9ns29xT-&*&Uox>1NQs|S*lxP4wga06b>_QW z_7J!Ph%8HIU5z^AH-<~66LF?QcIS1OYug4iFZnd7-(k+*=YK82kEUF!+z^$c$@Kab zD4)OjO3&VJbD?8Ik)Stk-+EW7OQ7@a44oEVJY0B74-C4WG~zNtFGDrpFfOCN*gXP^ z_rcB5!^ZPG?UInbx~jNzIuNOFKAxRutdpkPg5P`_@a^~3qw;lII8jD{+N~Bj@DDY- zR7imwU<7}BA5xLDK&OOOk{uL96+aWNYL_4)C-1=3`+XyVe!Cex09Z(lA?1qGyxJnX zz=~VH3TZb`bjfi>t|Z|*x(vSU@HJSJzIpMD^<$E)7cf@ zTVfv((ix>YFr)!sONH8bUTqUxn0Q|gi@Cjl5kEOq#0tnp`fO0~n zpPWf&`G;CQL3Dp^Ksfo&pZ{&?M4!G8>Hq)$ literal 0 HcmV?d00001 diff --git a/docs/images/screenshots/14-screenshot-notification.png b/docs/images/screenshots/14-screenshot-notification.png new file mode 100644 index 0000000000000000000000000000000000000000..95321633191e6d2ec684d949d4b8c1f0e5916e25 GIT binary patch literal 21794 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 literal 0 HcmV?d00001 From a0ac0b01784c7ffca0204751df88bfc92a0a3c60 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:13:16 -0500 Subject: [PATCH 13/22] style(cmake): list each Lucide icon on its own line --- cmake/stage-lucide.cmake | 45 +++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/cmake/stage-lucide.cmake b/cmake/stage-lucide.cmake index dd6553a..4229939 100644 --- a/cmake/stage-lucide.cmake +++ b/cmake/stage-lucide.cmake @@ -5,9 +5,32 @@ endif() file(MAKE_DIRECTORY "${LUCIDE_ASSET_DIR}/icons" "${LUCIDE_ASSET_DIR}/licenses") foreach(icon IN ITEMS - settings circle-question-mark circle-plus monitor gamepad-2 scroll-text rotate-ccw - network hash activity key-round save loader-circle circle-check circle-x info - keyboard mouse power log-out pause panels-top-left share-2 camera chart-no-axes-combined) + settings + circle-question-mark + circle-plus + monitor + gamepad-2 + scroll-text + rotate-ccw + network + hash + activity + key-round + save + loader-circle + circle-check + circle-x + info + keyboard + mouse + power + log-out + pause + panels-top-left + share-2 + camera + chart-no-axes-combined +) set(_icon_source "${LUCIDE_SOURCE_DIR}/icons/${icon}.svg") if(NOT EXISTS "${_icon_source}") message(FATAL_ERROR "Required Lucide icon not found: ${_icon_source}") @@ -40,7 +63,19 @@ configure_file("${LUCIDE_SOURCE_DIR}/LICENSE" "${LUCIDE_ASSET_DIR}/licenses/luci # Remove obsolete first-party icons from existing build directories, too. foreach(legacy_icon IN ITEMS - gear support add-host host-monitor-online host-monitor-pairing host-monitor-offline - circle-help life-buoy monitor-check monitor-dot monitor-off ethernet-port question-mark) + gear + support + add-host + host-monitor-online + host-monitor-pairing + host-monitor-offline + circle-help + life-buoy + monitor-check + monitor-dot + monitor-off + ethernet-port + question-mark +) file(REMOVE "${LUCIDE_ASSET_DIR}/icons/${legacy_icon}.svg") endforeach() From 47cc00c23f8c537995319e03b8372f2e1018241f Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:14:22 -0500 Subject: [PATCH 14/22] style(cmake): alphabetize Lucide icon lists --- cmake/stage-lucide.cmake | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/cmake/stage-lucide.cmake b/cmake/stage-lucide.cmake index 4229939..514bedc 100644 --- a/cmake/stage-lucide.cmake +++ b/cmake/stage-lucide.cmake @@ -5,31 +5,31 @@ endif() file(MAKE_DIRECTORY "${LUCIDE_ASSET_DIR}/icons" "${LUCIDE_ASSET_DIR}/licenses") foreach(icon IN ITEMS - settings - circle-question-mark - circle-plus - monitor - gamepad-2 - scroll-text - rotate-ccw - network - hash activity - key-round - save - loader-circle + camera + chart-no-axes-combined circle-check + circle-plus + circle-question-mark circle-x + gamepad-2 + hash info + key-round keyboard - mouse - power + loader-circle log-out - pause + monitor + mouse + network panels-top-left + pause + power + rotate-ccw + save + scroll-text + settings share-2 - camera - chart-no-axes-combined ) set(_icon_source "${LUCIDE_SOURCE_DIR}/icons/${icon}.svg") if(NOT EXISTS "${_icon_source}") @@ -63,19 +63,19 @@ configure_file("${LUCIDE_SOURCE_DIR}/LICENSE" "${LUCIDE_ASSET_DIR}/licenses/luci # Remove obsolete first-party icons from existing build directories, too. foreach(legacy_icon IN ITEMS - gear - support add-host + circle-help + ethernet-port + gear + host-monitor-offline host-monitor-online host-monitor-pairing - host-monitor-offline - circle-help life-buoy monitor-check monitor-dot monitor-off - ethernet-port question-mark + support ) file(REMOVE "${LUCIDE_ASSET_DIR}/icons/${legacy_icon}.svg") endforeach() From f87cc125b101564c97bfbda0b6b4838c30520c91 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:18:45 -0500 Subject: [PATCH 15/22] style(assets): add final newlines to controller icons --- xbe/assets/icons/button-left-stick.svg | 2 +- xbe/assets/icons/button-right-stick.svg | 2 +- xbe/assets/icons/button-share.svg | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xbe/assets/icons/button-left-stick.svg b/xbe/assets/icons/button-left-stick.svg index ddab33a..b40f232 100644 --- a/xbe/assets/icons/button-left-stick.svg +++ b/xbe/assets/icons/button-left-stick.svg @@ -1 +1 @@ - \ No newline at end of file + diff --git a/xbe/assets/icons/button-right-stick.svg b/xbe/assets/icons/button-right-stick.svg index c1856cf..623006f 100644 --- a/xbe/assets/icons/button-right-stick.svg +++ b/xbe/assets/icons/button-right-stick.svg @@ -1 +1 @@ - \ No newline at end of file + diff --git a/xbe/assets/icons/button-share.svg b/xbe/assets/icons/button-share.svg index 07b97fb..caf60c5 100644 --- a/xbe/assets/icons/button-share.svg +++ b/xbe/assets/icons/button-share.svg @@ -1 +1 @@ - \ No newline at end of file + From a6eb8893d566e55d4791d47f9c2e3e202a4cde6a Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:33:22 -0500 Subject: [PATCH 16/22] style: document include groups and keep Doxygen blocks adjacent --- src/app/client_state.h | 2 +- src/app/settings_storage.cpp | 1 + src/input/stream_exit.cpp | 2 ++ src/input/stream_exit.h | 1 + src/input/stream_input.cpp | 7 ++++-- src/input/stream_input.h | 1 + src/input/stream_menu.cpp | 7 ++++-- src/input/stream_menu.h | 1 + src/input/ui_input.cpp | 2 ++ src/input/ui_input.h | 6 +++-- src/input/usb_hid.cpp | 2 ++ src/input/usb_hid.h | 1 + src/main.cpp | 12 +++++----- src/network/host_pairing.cpp | 5 ++--- src/platform/usb_input.cpp | 7 ++++++ src/startup/storage_paths.cpp | 4 ++++ src/streaming/ffmpeg_stream_backend.cpp | 2 ++ src/streaming/ffmpeg_stream_backend.h | 3 ++- src/streaming/pvideo_presenter.cpp | 4 ++++ src/streaming/pvideo_presenter.h | 1 + src/streaming/screenshot_status.cpp | 1 + src/streaming/session.cpp | 30 ++++++++++++++++--------- src/streaming/stream_controls.cpp | 19 ++++++++++------ src/streaming/stream_controls.h | 24 +++++++++++--------- src/streaming/video_overlay.cpp | 2 ++ src/streaming/video_overlay.h | 1 + src/ui/asset_path.cpp | 7 ++++-- src/ui/asset_path.h | 1 + src/ui/dialog_surface.h | 3 +++ src/ui/shell_screen.cpp | 7 +++--- src/ui/shell_view.cpp | 6 ++--- src/ui/svg_assets.cpp | 5 +++++ src/ui/svg_assets.h | 1 + tests/unit/input/stream_exit_test.cpp | 4 +++- tests/unit/input/stream_input_test.cpp | 4 +++- tests/unit/input/stream_menu_test.cpp | 4 +++- 36 files changed, 135 insertions(+), 55 deletions(-) diff --git a/src/app/client_state.h b/src/app/client_state.h index ada4499..cabdf8a 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -15,7 +15,7 @@ // nxdk includes #include -// standard includes +// local includes #include "src/app/host_records.h" #include "src/app/pairing_flow.h" #include "src/app/screenshot_library.h" diff --git a/src/app/settings_storage.cpp b/src/app/settings_storage.cpp index bd5f36a..c97b25e 100644 --- a/src/app/settings_storage.cpp +++ b/src/app/settings_storage.cpp @@ -27,6 +27,7 @@ extern "C" FILE *_wfopen(const wchar_t *path, const wchar_t *mode); #endif +// lib includes #include // local includes diff --git a/src/input/stream_exit.cpp b/src/input/stream_exit.cpp index eeb4dea..d08fbd7 100644 --- a/src/input/stream_exit.cpp +++ b/src/input/stream_exit.cpp @@ -2,8 +2,10 @@ * @file src/input/stream_exit.cpp * @brief Recognizes the Back, Black and White stream menu shortcut from snapshots and events. */ +// class header include #include "src/input/stream_exit.h" +// lib includes #include "third-party/moonlight-common-c/src/Limelight.h" namespace input { diff --git a/src/input/stream_exit.h b/src/input/stream_exit.h index 3140edc..67ce003 100644 --- a/src/input/stream_exit.h +++ b/src/input/stream_exit.h @@ -4,6 +4,7 @@ */ #pragma once +// standard includes #include namespace input { diff --git a/src/input/stream_input.cpp b/src/input/stream_input.cpp index 5de79a8..1284b76 100644 --- a/src/input/stream_input.cpp +++ b/src/input/stream_input.cpp @@ -2,14 +2,17 @@ * @file src/input/stream_input.cpp * @brief Routes ordered controller, keyboard and pointer input to Moonlight. */ +// class header include #include "src/input/stream_input.h" -#include "third-party/moonlight-common-c/src/Limelight.h" - +// standard includes #include #include #include +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + namespace { constexpr int VIRTUAL_MOUSE_SOURCE = -2; ///< Reserved source for controller pointer buttons. diff --git a/src/input/stream_input.h b/src/input/stream_input.h index 2ff43dd..32ddc0c 100644 --- a/src/input/stream_input.h +++ b/src/input/stream_input.h @@ -4,6 +4,7 @@ */ #pragma once +// standard includes #include #include #include diff --git a/src/input/stream_menu.cpp b/src/input/stream_menu.cpp index 982f37f..45b7c6a 100644 --- a/src/input/stream_menu.cpp +++ b/src/input/stream_menu.cpp @@ -2,13 +2,16 @@ * @file src/input/stream_menu.cpp * @brief Implements radial selection and host keyboard navigation. */ +// class header include #include "src/input/stream_menu.h" -#include "third-party/moonlight-common-c/src/Limelight.h" - +// standard includes #include #include +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + namespace { /** * @brief Arrange a central character grid with Xbox-style side controls and a wide space bar. diff --git a/src/input/stream_menu.h b/src/input/stream_menu.h index e910838..61ee9c9 100644 --- a/src/input/stream_menu.h +++ b/src/input/stream_menu.h @@ -4,6 +4,7 @@ */ #pragma once +// standard includes #include #include diff --git a/src/input/ui_input.cpp b/src/input/ui_input.cpp index a6a2ab2..8ea0df0 100644 --- a/src/input/ui_input.cpp +++ b/src/input/ui_input.cpp @@ -2,8 +2,10 @@ * @file src/input/ui_input.cpp * @brief Implements repeat timing without depending on SDL or platform key repeat. */ +// class header include #include "src/input/ui_input.h" +// standard includes #include #include diff --git a/src/input/ui_input.h b/src/input/ui_input.h index c190a8a..218dc7b 100644 --- a/src/input/ui_input.h +++ b/src/input/ui_input.h @@ -4,12 +4,14 @@ */ #pragma once -#include "src/input/navigation_input.h" - +// standard includes #include #include #include +// local includes +#include "src/input/navigation_input.h" + namespace input { constexpr std::uint32_t NAVIGATION_INITIAL_REPEAT_MILLISECONDS = 150; ///< Delay shared by keyboard and controller navigation. constexpr std::uint32_t NAVIGATION_REPEAT_MILLISECONDS = 45; ///< Interval shared by keyboard and controller navigation. diff --git a/src/input/usb_hid.cpp b/src/input/usb_hid.cpp index f0dda13..c76d827 100644 --- a/src/input/usb_hid.cpp +++ b/src/input/usb_hid.cpp @@ -2,8 +2,10 @@ * @file src/input/usb_hid.cpp * @brief Decodes standard boot HID input with rollover and disconnect handling. */ +// class header include #include "src/input/usb_hid.h" +// standard includes #include #include diff --git a/src/input/usb_hid.h b/src/input/usb_hid.h index 5504a30..0d3d0ec 100644 --- a/src/input/usb_hid.h +++ b/src/input/usb_hid.h @@ -4,6 +4,7 @@ */ #pragma once +// standard includes #include #include #include diff --git a/src/main.cpp b/src/main.cpp index e3166f6..6722720 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,11 +2,6 @@ * @file src/main.cpp * @brief Runs the Moonlight Xbox startup sequence and main loop. */ -// nxdk includes -#include -#include -#include // NOSONAR(cpp:S3806) nxdk requires lowercase header names - // standard includes #include #include @@ -15,6 +10,13 @@ #include #include +// lib includes +#include + +// nxdk includes +#include +#include // NOSONAR(cpp:S3806) nxdk requires lowercase header names + // local includes #include "src/app/client_state.h" #include "src/app/settings_storage.h" diff --git a/src/network/host_pairing.cpp b/src/network/host_pairing.cpp index 367f18e..78341c1 100644 --- a/src/network/host_pairing.cpp +++ b/src/network/host_pairing.cpp @@ -22,6 +22,8 @@ #include // lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + #include #include #include @@ -36,9 +38,6 @@ #include "src/network/socket_bio.h" #include "src/platform/error_utils.h" -// third-party includes -#include "third-party/moonlight-common-c/src/Limelight.h" - // platform includes #ifdef NXDK #include diff --git a/src/platform/usb_input.cpp b/src/platform/usb_input.cpp index 7718930..63b87b3 100644 --- a/src/platform/usb_input.cpp +++ b/src/platform/usb_input.cpp @@ -2,20 +2,27 @@ * @file src/platform/usb_input.cpp * @brief Bridges nxdk USB boot HID interfaces into SDL's keyboard and mouse event state. */ +// standard includes #include #include #include #include + +// lib includes #include + +// nxdk includes #include #include +// lib includes // usb.h depends on the Xbox configuration and must precede the HID class declarations. extern "C" { #include #include } +// local includes #include "src/input/usb_hid.h" // These are the vendored SDL2 internal event entrypoints. Calling them on the pump diff --git a/src/startup/storage_paths.cpp b/src/startup/storage_paths.cpp index 7ab4a78..bb4f440 100644 --- a/src/startup/storage_paths.cpp +++ b/src/startup/storage_paths.cpp @@ -14,8 +14,12 @@ #if __has_include() #include #endif + + // standard includes #include #include + + // platform includes #include #endif diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index b4e9894..7f9d1da 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -2,6 +2,7 @@ * @file src/streaming/ffmpeg_stream_backend.cpp * @brief Implements the FFmpeg-backed streaming decode backend for Xbox sessions. */ +// class header include #include "src/streaming/ffmpeg_stream_backend.h" // standard includes @@ -35,6 +36,7 @@ extern "C" { #include "src/logging/logger.h" #include "src/platform/filesystem_utils.h" +// nxdk includes #ifdef NXDK #include #endif diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index f456348..555e55c 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -12,6 +12,8 @@ #include // lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + #include // local includes @@ -19,7 +21,6 @@ #include "src/streaming/aligned_media_buffer.h" #include "src/streaming/pvideo_presenter.h" #include "src/streaming/stats_overlay.h" -#include "third-party/moonlight-common-c/src/Limelight.h" struct AVCodecContext; struct AVFrame; diff --git a/src/streaming/pvideo_presenter.cpp b/src/streaming/pvideo_presenter.cpp index c1e3345..c1143f2 100644 --- a/src/streaming/pvideo_presenter.cpp +++ b/src/streaming/pvideo_presenter.cpp @@ -2,11 +2,15 @@ * @file src/streaming/pvideo_presenter.cpp * @brief Submits bounded YUY2 overlay buffers using documented NV2A PVIDEO registers. */ +// class header include #include "src/streaming/pvideo_presenter.h" #ifdef NXDK + // standard includes #include #include + + // nxdk includes #include #include #include diff --git a/src/streaming/pvideo_presenter.h b/src/streaming/pvideo_presenter.h index 871c7fc..aae1021 100644 --- a/src/streaming/pvideo_presenter.h +++ b/src/streaming/pvideo_presenter.h @@ -4,6 +4,7 @@ */ #pragma once +// local includes #include "src/streaming/video_acceleration.h" #include "src/streaming/video_overlay.h" diff --git a/src/streaming/screenshot_status.cpp b/src/streaming/screenshot_status.cpp index 6d0d52a..4801872 100644 --- a/src/streaming/screenshot_status.cpp +++ b/src/streaming/screenshot_status.cpp @@ -2,6 +2,7 @@ * @file src/streaming/screenshot_status.cpp * @brief Implements throttled screenshot progress and automatic popup dismissal. */ +// class header include #include "src/streaming/screenshot_status.h" // standard includes diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index a846c74..b376ff3 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -2,17 +2,10 @@ * @file src/streaming/session.cpp * @brief Implements the Xbox streaming session runtime. */ +// class header include #include "src/streaming/session.h" -#include "src/input/stream_exit.h" -#include "src/logging/logger.h" -#include "src/os.h" -#include "src/startup/memory_stats.h" -#include "src/streaming/ffmpeg_stream_backend.h" -#include "src/streaming/stats_overlay.h" -#include "src/streaming/stream_controls.h" -#include "third-party/moonlight-common-c/src/Limelight.h" - +// standard includes #include #include #include @@ -23,18 +16,33 @@ #include #include #include -#include -#include #include #include #include #include + +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include +#include + +// platform includes #include // NOSONAR(cpp:S3806) nxdk requires lowercase header names #if defined(NXDK) && MOONLIGHT_INPUT_DIAGNOSTICS #include #endif +// local includes +#include "src/input/stream_exit.h" +#include "src/logging/logger.h" +#include "src/os.h" +#include "src/startup/memory_stats.h" +#include "src/streaming/ffmpeg_stream_backend.h" +#include "src/streaming/stats_overlay.h" +#include "src/streaming/stream_controls.h" + #ifdef NXDK /** * @brief Fill an integer with cryptographically strong random bytes on nxdk. diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 250d775..53b9cf4 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -2,21 +2,26 @@ * @file src/streaming/stream_controls.cpp * @brief Implements dual-stick stream controls and SDL-to-Moonlight input routing. */ +// class header include #include "src/streaming/stream_controls.h" +// standard includes +#include +#include +#include +#include +#include + +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + +// local includes #include "src/logging/log_file.h" #include "src/logging/logger.h" #include "src/streaming/ffmpeg_stream_backend.h" #include "src/ui/asset_path.h" #include "src/ui/dialog_surface.h" #include "src/ui/svg_assets.h" -#include "third-party/moonlight-common-c/src/Limelight.h" - -#include -#include -#include -#include -#include namespace { /** diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 81c77f1..435c9eb 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -4,25 +4,29 @@ */ #pragma once -#include "src/app/screenshot_library.h" -#include "src/input/stream_exit.h" -#include "src/input/stream_input.h" -#include "src/input/stream_menu.h" -#include "src/input/ui_input.h" -#include "src/streaming/screenshot_status.h" -#include "src/streaming/video_overlay.h" - +// standard includes #include #include #include #include #include -#include -#include #include #include #include +// lib includes +#include +#include + +// local includes +#include "src/app/screenshot_library.h" +#include "src/input/stream_exit.h" +#include "src/input/stream_input.h" +#include "src/input/stream_menu.h" +#include "src/input/ui_input.h" +#include "src/streaming/screenshot_status.h" +#include "src/streaming/video_overlay.h" + namespace streaming { class FfmpegStreamBackend; diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index 4b46891..e21d5a5 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -2,8 +2,10 @@ * @file src/streaming/video_overlay.cpp * @brief Converts cached UI panels for GPU scanout and software framebuffers. */ +// class header include #include "src/streaming/video_overlay.h" +// standard includes #include #include #include diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index 1c7bc85..7a9a2a9 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -4,6 +4,7 @@ */ #pragma once +// local includes #include "src/streaming/video_acceleration.h" namespace streaming { diff --git a/src/ui/asset_path.cpp b/src/ui/asset_path.cpp index b50fcaa..642e5bb 100644 --- a/src/ui/asset_path.cpp +++ b/src/ui/asset_path.cpp @@ -2,12 +2,15 @@ * @file src/ui/asset_path.cpp * @brief Resolves bundled asset paths consistently across UI screens. */ +// class header include #include "src/ui/asset_path.h" -#include "src/os.h" - +// standard includes #include +// local includes +#include "src/os.h" + namespace ui { std::string build_asset_path(const std::string &relativePath) { std::string path = std::string(DATA_PATH) + "assets" + PATH_SEP + relativePath; diff --git a/src/ui/asset_path.h b/src/ui/asset_path.h index 0216916..88c12ce 100644 --- a/src/ui/asset_path.h +++ b/src/ui/asset_path.h @@ -4,6 +4,7 @@ */ #pragma once +// standard includes #include namespace ui { diff --git a/src/ui/dialog_surface.h b/src/ui/dialog_surface.h index a2c6238..727ead6 100644 --- a/src/ui/dialog_surface.h +++ b/src/ui/dialog_surface.h @@ -4,8 +4,11 @@ */ #pragma once +// standard includes #include #include + +// lib includes #include namespace ui { diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 4c01a46..30b6f1e 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -19,11 +19,13 @@ #include #include -// nxdk includes -#include +// lib includes #include #include #include + +// nxdk includes +#include #include // NOSONAR(cpp:S3806) nxdk requires lowercase header names // local includes @@ -2441,7 +2443,6 @@ namespace { * @param state Current client state. * @return Settings snapshot ready for TOML serialization. */ - app::AppSettings persistent_settings_from_state(const app::ClientState &state) { return { state.settings.loggingLevel, diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index b83244f..ac633b9 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -6,13 +6,14 @@ #include "src/ui/shell_view.h" // standard includes -#include "src/platform/filesystem_utils.h" - #include #include #include #include +// local includes +#include "src/platform/filesystem_utils.h" + namespace { std::string format_file_size(std::uint64_t sizeBytes) { @@ -688,7 +689,6 @@ namespace { * @param state Current reducer-owned modal state. * @param viewModel Receives render-ready dialog content. */ - void fill_modal_view(const app::ClientState &state, ui::ShellViewModel *viewModel) { if (!state.modal.active()) { return; diff --git a/src/ui/svg_assets.cpp b/src/ui/svg_assets.cpp index ca82d66..81c9726 100644 --- a/src/ui/svg_assets.cpp +++ b/src/ui/svg_assets.cpp @@ -2,9 +2,14 @@ * @file src/ui/svg_assets.cpp * @brief Rasterizes shared shell and streaming SVG assets with NanoSVG. */ +// class header include #include "src/ui/svg_assets.h" +// standard includes #include + +// lib includes +// NanoSVG declares the image types required by the rasterizer header. #include #include #include diff --git a/src/ui/svg_assets.h b/src/ui/svg_assets.h index d5faeb4..7d8e10e 100644 --- a/src/ui/svg_assets.h +++ b/src/ui/svg_assets.h @@ -4,6 +4,7 @@ */ #pragma once +// standard includes #include struct SDL_Surface; diff --git a/tests/unit/input/stream_exit_test.cpp b/tests/unit/input/stream_exit_test.cpp index 184e44a..c13a941 100644 --- a/tests/unit/input/stream_exit_test.cpp +++ b/tests/unit/input/stream_exit_test.cpp @@ -9,9 +9,11 @@ #include #include +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + // local includes #include "src/input/stream_exit.h" -#include "third-party/moonlight-common-c/src/Limelight.h" /** * @brief Back and both shoulders request the menu immediately without Start, axes or a timer. diff --git a/tests/unit/input/stream_input_test.cpp b/tests/unit/input/stream_input_test.cpp index c76460e..5704eed 100644 --- a/tests/unit/input/stream_input_test.cpp +++ b/tests/unit/input/stream_input_test.cpp @@ -9,9 +9,11 @@ #include #include +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + // local includes #include "src/input/stream_input.h" -#include "third-party/moonlight-common-c/src/Limelight.h" /** * @brief Captures ordered transport packets without a host connection. diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp index 580053c..8bb67ed 100644 --- a/tests/unit/input/stream_menu_test.cpp +++ b/tests/unit/input/stream_menu_test.cpp @@ -9,9 +9,11 @@ #include #include +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + // local includes #include "src/input/stream_menu.h" -#include "third-party/moonlight-common-c/src/Limelight.h" /** * @brief Both sticks select all eight actions and never select an action in the dead zone. From 5dd75d413ebcb5f22c6a25a1a0e1adae0c4856be Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:44:30 -0500 Subject: [PATCH 17/22] style(tests): restore header-under-test include groups --- AGENTS.md | 8 +++++--- tests/controller/shell_controller_test.cpp | 8 ++++---- tests/unit/app/client_state_test.cpp | 13 +++++++------ tests/unit/app/directory_picker_test.cpp | 8 +++++--- tests/unit/app/menu_actions_test.cpp | 8 +++++--- tests/unit/app/screenshot_library_test.cpp | 13 ++++++++----- tests/unit/app/settings_storage_test.cpp | 10 ++++++---- tests/unit/input/navigation_input_test.cpp | 8 ++++---- tests/unit/input/stream_exit_test.cpp | 7 +++---- tests/unit/input/stream_input_test.cpp | 7 +++---- tests/unit/input/stream_menu_test.cpp | 7 +++---- tests/unit/input/ui_input_test.cpp | 8 ++++---- tests/unit/input/usb_hid_test.cpp | 8 ++++---- tests/unit/network/host_quit_test.cpp | 8 ++++---- tests/unit/platform/filesystem_utils_test.cpp | 13 +++++++------ tests/unit/startup/storage_paths_test.cpp | 8 ++++---- tests/unit/streaming/screenshot_status_test.cpp | 8 ++++---- tests/unit/streaming/video_overlay_test.cpp | 8 ++++---- tests/unit/ui/asset_path_test.cpp | 8 ++++---- tests/unit/ui/input_hints_test.cpp | 8 ++++---- tests/unit/ui/screenshot_image_test.cpp | 8 ++++---- tests/unit/ui/shell_layout_test.cpp | 8 ++++---- tests/unit/ui/shell_view_test.cpp | 13 +++++++------ 23 files changed, 107 insertions(+), 96 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index a4d0b35..dad3db1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,9 +38,11 @@ The project uses GoogleTest. Tests must include `` GoogleTest directly. This supplies the shared test support and output capture; Sunshine's `tests/tests_common.h` is not part of this repository. -Group test includes under `// test includes`, then `// standard includes`, `// lib includes` and -`// local includes`, with blank lines between groups and no empty groups. Keep platform-specific headers -inside their preprocessor guards. Preserve and explain intentional include-order dependencies. +In test translation units, put the header under test first under `// test header include`. Group remaining +includes under `// standard includes`, `// lib includes`, `// local includes` and `// test includes`, in that +order, with blank lines between groups and no empty groups. Put `` in the +library group and test-only support headers in the final test group. Keep platform-specific headers inside +their preprocessor guards. Preserve and explain intentional include-order dependencies. Add or update meaningful regression tests for new or changed source behavior. Target full coverage of changed testable source code. Keep platform-neutral logic accessible to native tests and document any diff --git a/tests/controller/shell_controller_test.cpp b/tests/controller/shell_controller_test.cpp index 7c91044..2cb8018 100644 --- a/tests/controller/shell_controller_test.cpp +++ b/tests/controller/shell_controller_test.cpp @@ -2,16 +2,16 @@ * @file tests/controller/shell_controller_test.cpp * @brief Verifies recovery from removed virtual gamepads and preservation of live menu-controller references. */ -// test includes -#include +// test header include +#include "src/input/shell_controller.h" // standard includes #include #include #include -// local includes -#include "src/input/shell_controller.h" +// lib includes +#include /** * @brief Stable controller handle whose device can disappear from enumeration while its reference remains open. diff --git a/tests/unit/app/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index 1f47adc..b31bc4c 100644 --- a/tests/unit/app/client_state_test.cpp +++ b/tests/unit/app/client_state_test.cpp @@ -2,10 +2,8 @@ * @file tests/unit/app/client_state_test.cpp * @brief Verifies client state models and transitions. */ -// test includes -#include "tests/support/network_test_constants.h" - -#include +// test header include +#include "src/app/client_state.h" // standard includes #include @@ -13,8 +11,11 @@ #include #include -// local includes -#include "src/app/client_state.h" +// lib includes +#include + +// test includes +#include "tests/support/network_test_constants.h" namespace { diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp index 7b753d4..3815947 100644 --- a/tests/unit/app/directory_picker_test.cpp +++ b/tests/unit/app/directory_picker_test.cpp @@ -2,14 +2,16 @@ * @file tests/unit/app/directory_picker_test.cpp * @brief Verifies staged screenshot destination selection and the local folder-name keyboard. */ -// test includes -#include +// test header include +#include "src/app/client_state.h" // standard includes #include +// lib includes +#include + // local includes -#include "src/app/client_state.h" #include "src/startup/storage_paths.h" #include "src/ui/shell_view.h" diff --git a/tests/unit/app/menu_actions_test.cpp b/tests/unit/app/menu_actions_test.cpp index 204477f..8baa252 100644 --- a/tests/unit/app/menu_actions_test.cpp +++ b/tests/unit/app/menu_actions_test.cpp @@ -2,14 +2,16 @@ * @file tests/unit/app/menu_actions_test.cpp * @brief Verifies retained action menus, confirmed capture deletion and host-app termination. */ -// test includes -#include +// test header include +#include "src/app/client_state.h" // standard includes #include +// lib includes +#include + // local includes -#include "src/app/client_state.h" #include "src/ui/shell_view.h" namespace { diff --git a/tests/unit/app/screenshot_library_test.cpp b/tests/unit/app/screenshot_library_test.cpp index 44491a6..a370dd0 100644 --- a/tests/unit/app/screenshot_library_test.cpp +++ b/tests/unit/app/screenshot_library_test.cpp @@ -2,22 +2,25 @@ * @file tests/unit/app/screenshot_library_test.cpp * @brief Verifies capture provenance, journal recovery, unique filenames and app-scoped navigation. */ -// test includes -#include "tests/support/filesystem_test_utils.h" - -#include +// test header include +#include "src/app/screenshot_library.h" // standard includes #include #include #include +// lib includes +#include + // local includes #include "src/app/client_state.h" -#include "src/app/screenshot_library.h" #include "src/startup/storage_paths.h" #include "src/ui/shell_view.h" +// test includes +#include "tests/support/filesystem_test_utils.h" + namespace { /** * @brief Own isolated capture and journal files for one native test. diff --git a/tests/unit/app/settings_storage_test.cpp b/tests/unit/app/settings_storage_test.cpp index 5d51706..b3880ce 100644 --- a/tests/unit/app/settings_storage_test.cpp +++ b/tests/unit/app/settings_storage_test.cpp @@ -2,16 +2,18 @@ * @file tests/unit/app/settings_storage_test.cpp * @brief Verifies application settings persistence. */ -// test includes -#include +// test header include +#include "src/app/settings_storage.h" // standard includes #include #include #include -// local includes -#include "src/app/settings_storage.h" +// lib includes +#include + +// test includes #include "tests/support/filesystem_test_utils.h" namespace { diff --git a/tests/unit/input/navigation_input_test.cpp b/tests/unit/input/navigation_input_test.cpp index 69d7b39..d5be02d 100644 --- a/tests/unit/input/navigation_input_test.cpp +++ b/tests/unit/input/navigation_input_test.cpp @@ -2,12 +2,12 @@ * @file tests/unit/input/navigation_input_test.cpp * @brief Verifies controller navigation input handling. */ -// test includes -#include - -// local includes +// test header include #include "src/input/navigation_input.h" +// lib includes +#include + namespace { /** diff --git a/tests/unit/input/stream_exit_test.cpp b/tests/unit/input/stream_exit_test.cpp index c13a941..97b4a17 100644 --- a/tests/unit/input/stream_exit_test.cpp +++ b/tests/unit/input/stream_exit_test.cpp @@ -2,8 +2,8 @@ * @file tests/unit/input/stream_exit_test.cpp * @brief Verifies digital-only stream menu shortcut detection, event ordering and disconnect cleanup. */ -// test includes -#include +// test header include +#include "src/input/stream_exit.h" // standard includes #include @@ -12,8 +12,7 @@ // lib includes #include "third-party/moonlight-common-c/src/Limelight.h" -// local includes -#include "src/input/stream_exit.h" +#include /** * @brief Back and both shoulders request the menu immediately without Start, axes or a timer. diff --git a/tests/unit/input/stream_input_test.cpp b/tests/unit/input/stream_input_test.cpp index 5704eed..db88e91 100644 --- a/tests/unit/input/stream_input_test.cpp +++ b/tests/unit/input/stream_input_test.cpp @@ -2,8 +2,8 @@ * @file tests/unit/input/stream_input_test.cpp * @brief Verifies independent gamepads, capture lifetimes and merged pointer/keyboard input. */ -// test includes -#include +// test header include +#include "src/input/stream_input.h" // standard includes #include @@ -12,8 +12,7 @@ // lib includes #include "third-party/moonlight-common-c/src/Limelight.h" -// local includes -#include "src/input/stream_input.h" +#include /** * @brief Captures ordered transport packets without a host connection. diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp index 8bb67ed..efdc43f 100644 --- a/tests/unit/input/stream_menu_test.cpp +++ b/tests/unit/input/stream_menu_test.cpp @@ -2,8 +2,8 @@ * @file tests/unit/input/stream_menu_test.cpp * @brief Verifies dual-stick sectors, dead zones, keyboard modifiers and log navigation. */ -// test includes -#include +// test header include +#include "src/input/stream_menu.h" // standard includes #include @@ -12,8 +12,7 @@ // lib includes #include "third-party/moonlight-common-c/src/Limelight.h" -// local includes -#include "src/input/stream_menu.h" +#include /** * @brief Both sticks select all eight actions and never select an action in the dead zone. diff --git a/tests/unit/input/ui_input_test.cpp b/tests/unit/input/ui_input_test.cpp index eb44c1a..b997cc9 100644 --- a/tests/unit/input/ui_input_test.cpp +++ b/tests/unit/input/ui_input_test.cpp @@ -2,16 +2,16 @@ * @file tests/unit/input/ui_input_test.cpp * @brief Verifies keyboard navigation holds, input-source arbitration and repeat-safe commands. */ -// test includes -#include +// test header include +#include "src/input/ui_input.h" // standard includes #include #include #include -// local includes -#include "src/input/ui_input.h" +// lib includes +#include /** * @brief Keyboard navigation uses the same initial delay and interval as controller navigation. diff --git a/tests/unit/input/usb_hid_test.cpp b/tests/unit/input/usb_hid_test.cpp index 7d7082b..cc9f1d9 100644 --- a/tests/unit/input/usb_hid_test.cpp +++ b/tests/unit/input/usb_hid_test.cpp @@ -2,15 +2,15 @@ * @file tests/unit/input/usb_hid_test.cpp * @brief Verifies boot HID rollover, signed motion, truncation and independent device lifetimes. */ -// test includes -#include +// test header include +#include "src/input/usb_hid.h" // standard includes #include #include -// local includes -#include "src/input/usb_hid.h" +// lib includes +#include /** * @brief Records decoded HID operations without USB hardware. diff --git a/tests/unit/network/host_quit_test.cpp b/tests/unit/network/host_quit_test.cpp index bdaa289..cacefde 100644 --- a/tests/unit/network/host_quit_test.cpp +++ b/tests/unit/network/host_quit_test.cpp @@ -2,15 +2,15 @@ * @file tests/unit/network/host_quit_test.cpp * @brief Verifies authenticated cancellation, stale-target protection and host quit verification. */ -// test includes -#include +// test header include +#include "src/network/host_pairing.h" // standard includes #include #include -// local includes -#include "src/network/host_pairing.h" +// lib includes +#include namespace { /** diff --git a/tests/unit/platform/filesystem_utils_test.cpp b/tests/unit/platform/filesystem_utils_test.cpp index f94dc94..0892d72 100644 --- a/tests/unit/platform/filesystem_utils_test.cpp +++ b/tests/unit/platform/filesystem_utils_test.cpp @@ -2,10 +2,8 @@ * @file tests/unit/platform/filesystem_utils_test.cpp * @brief Verifies filesystem utility helpers. */ -// test includes -#include "tests/support/filesystem_test_utils.h" - -#include +// test header include +#include "src/platform/filesystem_utils.h" // standard includes #include @@ -14,8 +12,11 @@ #include #include -// local includes -#include "src/platform/filesystem_utils.h" +// lib includes +#include + +// test includes +#include "tests/support/filesystem_test_utils.h" namespace { diff --git a/tests/unit/startup/storage_paths_test.cpp b/tests/unit/startup/storage_paths_test.cpp index e3663c3..9404cd5 100644 --- a/tests/unit/startup/storage_paths_test.cpp +++ b/tests/unit/startup/storage_paths_test.cpp @@ -2,12 +2,12 @@ * @file tests/unit/startup/storage_paths_test.cpp * @brief Verifies configured screenshot folders and the default save-data subdirectory. */ -// test includes -#include - -// local includes +// test header include #include "src/startup/storage_paths.h" +// lib includes +#include + /** * @brief Default screenshots live together under save data; custom Xbox and host separators are preserved. */ diff --git a/tests/unit/streaming/screenshot_status_test.cpp b/tests/unit/streaming/screenshot_status_test.cpp index 77a1f35..86e4a3f 100644 --- a/tests/unit/streaming/screenshot_status_test.cpp +++ b/tests/unit/streaming/screenshot_status_test.cpp @@ -2,15 +2,15 @@ * @file tests/unit/streaming/screenshot_status_test.cpp * @brief Verifies persistent save progress and timed screenshot completion feedback. */ -// test includes -#include +// test header include +#include "src/streaming/screenshot_status.h" // standard includes #include #include -// local includes -#include "src/streaming/screenshot_status.h" +// lib includes +#include /** * @brief Saving never times out, never reports premature completion, and avoids redraws for every disk row. diff --git a/tests/unit/streaming/video_overlay_test.cpp b/tests/unit/streaming/video_overlay_test.cpp index d5efe23..08ac802 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -2,8 +2,8 @@ * @file tests/unit/streaming/video_overlay_test.cpp * @brief Verifies equivalent GPU/software panel pixels and destination bounds. */ -// test includes -#include +// test header include +#include "src/streaming/video_overlay.h" // standard includes #include @@ -13,8 +13,8 @@ #include #include -// local includes -#include "src/streaming/video_overlay.h" +// lib includes +#include /** * @brief Full-screen dialog repaints reuse their color allocation while screenshots own additional buffers. diff --git a/tests/unit/ui/asset_path_test.cpp b/tests/unit/ui/asset_path_test.cpp index 2df6f81..1e88fe6 100644 --- a/tests/unit/ui/asset_path_test.cpp +++ b/tests/unit/ui/asset_path_test.cpp @@ -2,12 +2,12 @@ * @file tests/unit/ui/asset_path_test.cpp * @brief Verifies that shell and streaming assets use valid Xbox SDL paths. */ -// test includes -#include - -// local includes +// test header include #include "src/ui/asset_path.h" +// lib includes +#include + /** * @brief Existing shell paths and portable menu paths resolve to the same Xbox file. */ diff --git a/tests/unit/ui/input_hints_test.cpp b/tests/unit/ui/input_hints_test.cpp index e57a34b..4ac9383 100644 --- a/tests/unit/ui/input_hints_test.cpp +++ b/tests/unit/ui/input_hints_test.cpp @@ -2,15 +2,15 @@ * @file tests/unit/ui/input_hints_test.cpp * @brief Verifies keyboard keycaps throughout the UI and restoration of controller glyphs. */ -// test includes -#include +// test header include +#include "src/ui/shell_view.h" // standard includes #include #include -// local includes -#include "src/ui/shell_view.h" +// lib includes +#include /** * @brief Main-page helpers follow keyboard input and restore the original glyphs after gamepad input. diff --git a/tests/unit/ui/screenshot_image_test.cpp b/tests/unit/ui/screenshot_image_test.cpp index 71fad26..a014a73 100644 --- a/tests/unit/ui/screenshot_image_test.cpp +++ b/tests/unit/ui/screenshot_image_test.cpp @@ -2,16 +2,16 @@ * @file tests/unit/ui/screenshot_image_test.cpp * @brief Verifies bounded capture decoding and aspect-preserving image placement. */ -// test includes -#include +// test header include +#include "src/ui/screenshot_image.h" // standard includes #include #include #include -// local includes -#include "src/ui/screenshot_image.h" +// lib includes +#include namespace { /** diff --git a/tests/unit/ui/shell_layout_test.cpp b/tests/unit/ui/shell_layout_test.cpp index c2474a2..5db3d6b 100644 --- a/tests/unit/ui/shell_layout_test.cpp +++ b/tests/unit/ui/shell_layout_test.cpp @@ -2,16 +2,16 @@ * @file tests/unit/ui/shell_layout_test.cpp * @brief Verifies TV-safe layout and focus visibility across Xbox display modes. */ -// test includes -#include +// test header include +#include "src/ui/shell_layout.h" // standard includes #include #include #include -// local includes -#include "src/ui/shell_layout.h" +// lib includes +#include namespace { diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index 03d1e64..e6ec2de 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -2,18 +2,19 @@ * @file tests/unit/ui/shell_view_test.cpp * @brief Verifies the shell view renderer and layout helpers. */ -// test includes -#include "tests/support/network_test_constants.h" - -#include +// test header include +#include "src/ui/shell_view.h" // standard includes #include #include #include -// local includes -#include "src/ui/shell_view.h" +// lib includes +#include + +// test includes +#include "tests/support/network_test_constants.h" namespace { From 029f93f3f7252196d52886adbfaa8db4eb34451e Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:30:28 -0500 Subject: [PATCH 18/22] fix: resolve stream controls analyzer findings --- src/_nxdk_compat/include/moonlight_time.h | 48 + src/app/client_state.cpp | 203 ++- src/app/client_state.h | 13 +- src/app/screenshot_library.cpp | 59 +- src/input/stream_input.cpp | 173 +-- src/input/stream_input.h | 6 +- src/input/stream_menu.cpp | 22 +- src/input/stream_menu.h | 2 + src/input/ui_input.cpp | 30 +- src/input/ui_input.h | 45 + src/input/usb_hid.cpp | 76 +- src/input/usb_hid.h | 15 +- src/main.cpp | 4 +- src/network/host_pairing.cpp | 3 +- src/platform/filesystem_utils.cpp | 26 +- src/platform/usb_input.cpp | 101 +- src/streaming/ffmpeg_stream_backend.cpp | 129 +- src/streaming/ffmpeg_stream_backend.h | 13 +- src/streaming/screenshot_status.cpp | 9 +- src/streaming/screenshot_status.h | 1 - src/streaming/session.cpp | 6 +- src/streaming/stream_controls.cpp | 1499 +++++++++++-------- src/streaming/stream_controls.h | 259 +++- src/streaming/video_overlay.cpp | 311 ++-- src/streaming/video_overlay.h | 79 +- src/ui/screenshot_image.cpp | 6 +- src/ui/shell_screen.cpp | 280 ++-- src/ui/shell_view.cpp | 4 +- tests/CMakeLists.txt | 14 + tests/controller/shell_controller_test.cpp | 116 +- tests/support/shell_controller/SDL.h | 15 +- tests/support/xbox_time/xboxkrnl/xboxkrnl.h | 36 + tests/unit/app/directory_picker_test.cpp | 21 +- tests/unit/app/screenshot_library_test.cpp | 4 +- tests/unit/app/settings_storage_test.cpp | 2 +- tests/unit/input/stream_input_test.cpp | 4 +- tests/unit/input/stream_menu_test.cpp | 21 +- tests/unit/input/ui_input_test.cpp | 88 +- tests/unit/input/usb_hid_test.cpp | 2 +- tests/unit/network/host_quit_test.cpp | 71 +- tests/unit/streaming/video_overlay_test.cpp | 128 +- tests/xbox/time_test.cpp | 130 ++ 42 files changed, 2637 insertions(+), 1437 deletions(-) create mode 100644 src/_nxdk_compat/include/moonlight_time.h create mode 100644 tests/support/xbox_time/xboxkrnl/xboxkrnl.h create mode 100644 tests/xbox/time_test.cpp diff --git a/src/_nxdk_compat/include/moonlight_time.h b/src/_nxdk_compat/include/moonlight_time.h new file mode 100644 index 0000000..c567cc0 --- /dev/null +++ b/src/_nxdk_compat/include/moonlight_time.h @@ -0,0 +1,48 @@ +/** + * @file src/_nxdk_compat/include/moonlight_time.h + * @brief Converts UTC timestamps with caller-owned storage through the Xbox kernel. + */ +#pragma once + +// standard includes +#include +#include +#include +#include + +// lib includes +#include + +/** + * @brief Convert nonnegative Unix time into caller-owned UTC calendar fields. + * + * PDCLib declares gmtime_s without supplying its implementation. Its gmtime uses + * shared static storage, so use the kernel conversion without changing libc feature macros. + * + * @param timestamp Unix timestamp in seconds. + * @param result Receives the complete UTC calendar fields. + * @return False if the timestamp cannot be represented as signed NT time. + */ +inline bool moonlight_gmtime(std::time_t timestamp, std::tm &result) { + constexpr std::int64_t epochOffset = 11644473600LL; + constexpr std::int64_t ticksPerSecond = 10000000; + if (timestamp < 0 || timestamp > std::numeric_limits::max() / ticksPerSecond - epochOffset) { + return false; + } + LARGE_INTEGER time {}; + time.QuadPart = (static_cast(timestamp) + epochOffset) * ticksPerSecond; + TIME_FIELDS fields {}; + RtlTimeToTimeFields(&time, &fields); + result = {}; + result.tm_year = fields.Year - 1900; + result.tm_mon = fields.Month - 1; + result.tm_mday = fields.Day; + result.tm_hour = fields.Hour; + result.tm_min = fields.Minute; + result.tm_sec = fields.Second; + result.tm_wday = fields.Weekday; + constexpr std::array monthStarts {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + const bool leap = fields.Year % 4 == 0 && (fields.Year % 100 != 0 || fields.Year % 400 == 0); + result.tm_yday = monthStarts.at(result.tm_mon) + fields.Day - 1 + (fields.Month > 2 && leap ? 1 : 0); + return true; +} diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 3e5dcd0..7553da8 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -619,7 +619,7 @@ namespace { }; case app::SettingsCategory::storage: return { - {"screenshot-directory", "Screenshot Folder: " + (state.settings.screenshotDirectory.empty() ? std::string("Default") : state.settings.screenshotDirectory), "Choose or create a folder for stream screenshots. Default: " + startup::default_storage_path("screenshots"), true}, + {"screenshot-directory", "Screenshot Folder: " + (state.settings.controls.screenshotDirectory.empty() ? std::string("Default") : state.settings.controls.screenshotDirectory), "Choose or create a folder for stream screenshots. Default: " + startup::default_storage_path("screenshots"), true}, }; case app::SettingsCategory::input: return { @@ -1221,7 +1221,12 @@ namespace { state.apps.refreshFailureNotified = false; const bool offline = state.hosts.active.reachability == app::HostReachability::offline; state.hosts.active.appListState = offline ? app::HostAppListState::ready : app::HostAppListState::loading; - state.hosts.active.appListStatusMessage = offline ? "PC offline. Showing saved apps and local screenshots." : (state.hosts.active.apps.empty() ? "Loading apps for " : "Refreshing apps for ") + state.hosts.active.displayName + "..."; + if (offline) { + state.hosts.active.appListStatusMessage = "PC offline. Showing saved apps and local screenshots."; + } else { + const std::string prefix = state.hosts.active.apps.empty() ? "Loading apps for " : "Refreshing apps for "; + state.hosts.active.appListStatusMessage = prefix + state.hosts.active.displayName + "..."; + } state.shell.statusMessage.clear(); set_screen(state, app::ScreenId::apps); return true; @@ -1589,66 +1594,102 @@ namespace { } /** - * @brief Keep folder browsing and local text editing separate from saved settings until acceptance. - * @param state Active client and picker state. - * @param command Controller or keyboard action. - * @param update Receives requested filesystem operations and persistence effects. - * @return True when the directory picker owns this command. + * @brief Type the focused folder-name key or return its editing command. + * @param state Folder-name draft and keyboard focus. + * @return Confirm, delete or none after handling the selected key. */ - bool handle_directory_picker_command(app::ClientState &state, input::UiCommand command, app::AppUpdate *update) { - if (state.modal.id != app::ModalId::directory_picker) { - return false; + input::UiCommand activate_directory_key(app::ClientState &state) { + auto &picker = state.directoryPicker; + auto &keyboard = picker.keyboard; + if (keyboard.keyboard_cells()[keyboard.keyIndex].key == input::StreamMenu::KEYBOARD_PAGE_KEY) { + keyboard.keyboardLayout = keyboard.keyboardLayout == 0 ? 1 : 0; + return input::UiCommand::none; + } + const int code = keyboard.keyboard_key(); + if (code == 0x0D) { + return input::UiCommand::confirm; + } + if (code == 0x08) { + return input::UiCommand::delete_character; + } + if (code == 0x125 && picker.caret > 0) { + --picker.caret; + } else if (code == 0x127 && picker.caret < picker.name.size()) { + ++picker.caret; + } else if (code == 0x20) { + app::append_directory_name(state, " "); + keyboard.key_sent(); + } else if (code > 0 && keyboard.key_label(keyboard.keyIndex).size() == 1) { + app::append_directory_name(state, keyboard.key_label(keyboard.keyIndex)); + keyboard.key_sent(); } + return input::UiCommand::none; + } + + /** + * @brief Handle navigation and editing while creating a screenshot folder. + * @param state Folder-name draft and keyboard focus. + * @param command Local editing or navigation command. + * @param update Receives the create-directory request after validation. + */ + void handle_directory_name_command(app::ClientState &state, input::UiCommand command, app::AppUpdate *update) { auto &picker = state.directoryPicker; - if (picker.creating) { - auto &keyboard = picker.keyboard; - if (command == input::UiCommand::back) { + auto &keyboard = picker.keyboard; + if (command == input::UiCommand::activate) { + command = activate_directory_key(state); + } + switch (command) { + case input::UiCommand::back: picker.creating = false; picker.error.clear(); - } else if (command == input::UiCommand::move_up || command == input::UiCommand::move_down || command == input::UiCommand::move_left || command == input::UiCommand::move_right) { - keyboard.navigate(command == input::UiCommand::move_left ? -1 : command == input::UiCommand::move_right ? 1 : - 0, - command == input::UiCommand::move_up ? -1 : command == input::UiCommand::move_down ? 1 : - 0); - } else if (command == input::UiCommand::previous_page || command == input::UiCommand::next_page) { + break; + case input::UiCommand::move_up: + case input::UiCommand::move_down: + case input::UiCommand::move_left: + case input::UiCommand::move_right: + { + const auto [dx, dy] = input::navigation_step(command); + keyboard.navigate(dx, dy); + break; + } + case input::UiCommand::previous_page: + case input::UiCommand::next_page: keyboard.keyboardLayout = keyboard.keyboardLayout == 0 ? 1 : 0; - } else if (command == input::UiCommand::open_context_menu) { + break; + case input::UiCommand::open_context_menu: keyboard.toggle_shift(); - } else if (command == input::UiCommand::delete_character) { + break; + case input::UiCommand::delete_character: if (picker.caret > 0) { picker.name.erase(--picker.caret, 1); } - } else if (command == input::UiCommand::activate) { - const int index = keyboard.keyboard_cells()[keyboard.keyIndex].key; - if (index == input::StreamMenu::KEYBOARD_PAGE_KEY) { - keyboard.keyboardLayout = keyboard.keyboardLayout == 0 ? 1 : 0; - } else { - const int code = keyboard.keyboard_key(); - if (code == 0x0D) { - return handle_directory_picker_command(state, input::UiCommand::confirm, update); - } - if (code == 0x08) { - return handle_directory_picker_command(state, input::UiCommand::delete_character, update); - } - if (code == 0x125 && picker.caret > 0) { - --picker.caret; - } else if (code == 0x127 && picker.caret < picker.name.size()) { - ++picker.caret; - } else if (code == 0x20) { - app::append_directory_name(state, " "); - keyboard.key_sent(); - } else if (code > 0 && keyboard.key_label(keyboard.keyIndex).size() == 1) { - app::append_directory_name(state, keyboard.key_label(keyboard.keyIndex)); - keyboard.key_sent(); - } - } - } else if (command == input::UiCommand::confirm) { + break; + case input::UiCommand::confirm: if (platform::valid_directory_name(picker.name)) { update->requests.directoryCreateRequested = true; } else { picker.error = "Use 1-42 characters, without reserved characters or a trailing space or period."; } - } + break; + default: + break; + } + } + + /** + * @brief Keep folder browsing and local text editing separate from saved settings until acceptance. + * @param state Active client and picker state. + * @param command Controller or keyboard action. + * @param update Receives requested filesystem operations and persistence effects. + * @return True when the directory picker owns this command. + */ + bool handle_directory_picker_command(app::ClientState &state, input::UiCommand command, app::AppUpdate *update) { + if (state.modal.id != app::ModalId::directory_picker) { + return false; + } + auto &picker = state.directoryPicker; + if (picker.creating) { + handle_directory_name_command(state, command, update); return true; } if (command == input::UiCommand::back) { @@ -1670,8 +1711,7 @@ namespace { if (command != input::UiCommand::activate && command != input::UiCommand::confirm) { return true; } - const auto selected = state.modal.selectedActionIndex; - if (selected == 0 && picker.available && !picker.path.empty()) { + if (const auto selected = state.modal.selectedActionIndex; selected == 0 && picker.available && !picker.path.empty()) { update->requests.directorySelectRequested = true; } else if (selected == 1 && picker.available && !picker.path.empty()) { picker.creating = true; @@ -1694,6 +1734,37 @@ namespace { return true; } + /** + * @brief Handle screenshot browsing and deletion independently of other dialogs. + * @param state Screenshot viewer and modal state. + * @param command Navigation or deletion command. + * @return True while the screenshot viewer consumes input. + */ + bool handle_screenshot_viewer_command(app::ClientState &state, input::UiCommand command) { + if (state.modal.id != app::ModalId::screenshot_viewer) { + return false; + } + auto &viewer = state.screenshotViewer; + if (command == input::UiCommand::back) { + viewer = {}; + open_modal(state, app::ModalId::app_actions, 3U); + } else if (command == input::UiCommand::delete_character && !viewer.entries.empty()) { + viewer.selectedIndex = std::min(viewer.selectedIndex, viewer.entries.size() - 1); + const auto &entry = viewer.entries[viewer.selectedIndex]; + open_confirmation(state, app::ConfirmationAction::delete_screenshot, "Delete screenshot?", {"This permanently deletes the saved image.", app::screenshot_time_label(entry.capturedAt)}, entry.path); + state.confirmation.returnModal = app::ModalId::screenshot_viewer; + } else if (!viewer.entries.empty()) { + const auto count = viewer.entries.size(); + viewer.selectedIndex = std::min(viewer.selectedIndex, count - 1); + if (command == input::UiCommand::move_left || command == input::UiCommand::previous_page) { + viewer.selectedIndex = (viewer.selectedIndex + count - 1) % count; + } else if (command == input::UiCommand::move_right || command == input::UiCommand::next_page) { + viewer.selectedIndex = (viewer.selectedIndex + 1) % count; + } + } + return true; + } + /** * @brief Route local dialog input before allowing the underlying screen to navigate. * @param state Current dialog and shell state. @@ -1720,25 +1791,7 @@ namespace { return true; } - if (state.modal.id == app::ModalId::screenshot_viewer) { - auto &viewer = state.screenshotViewer; - if (command == input::UiCommand::back) { - viewer = {}; - open_modal(state, app::ModalId::app_actions, 3U); - } else if (command == input::UiCommand::delete_character && !viewer.entries.empty()) { - viewer.selectedIndex = std::min(viewer.selectedIndex, viewer.entries.size() - 1); - const auto &entry = viewer.entries[viewer.selectedIndex]; - open_confirmation(state, app::ConfirmationAction::delete_screenshot, "Delete screenshot?", {"This permanently deletes the saved image.", app::screenshot_time_label(entry.capturedAt)}, entry.path); - state.confirmation.returnModal = app::ModalId::screenshot_viewer; - } else if (!viewer.entries.empty()) { - const auto count = viewer.entries.size(); - viewer.selectedIndex = std::min(viewer.selectedIndex, count - 1); - if (command == input::UiCommand::move_left || command == input::UiCommand::previous_page) { - viewer.selectedIndex = (viewer.selectedIndex + count - 1) % count; - } else if (command == input::UiCommand::move_right || command == input::UiCommand::next_page) { - viewer.selectedIndex = (viewer.selectedIndex + 1) % count; - } - } + if (handle_screenshot_viewer_command(state, command)) { return true; } @@ -1844,8 +1897,8 @@ namespace app { state.settings.playAudioOnPc = false; state.settings.showPerformanceStats = false; state.settings.playAudioOnXbox = true; - state.settings.screenshotDirectory.clear(); - state.settings.keyboardDockTop = false; + state.settings.controls.screenshotDirectory.clear(); + state.settings.controls.keyboardDockTop = false; state.settings.dirty = false; state.settings.savedFilesDirty = true; return state; @@ -2477,7 +2530,7 @@ namespace app { } if (detailUpdate.activatedItemId == "screenshot-directory") { state.directoryPicker = {}; - state.directoryPicker.path = state.settings.screenshotDirectory.empty() ? startup::default_storage_path("screenshots") : state.settings.screenshotDirectory; + state.directoryPicker.path = state.settings.controls.screenshotDirectory.empty() ? startup::default_storage_path("screenshots") : state.settings.controls.screenshotDirectory; open_modal(state, ModalId::directory_picker); update->requests.directoryListRequested = true; update->navigation.modalOpened = true; @@ -2862,12 +2915,12 @@ namespace app { picker.error.clear(); } - void accept_screenshot_directory(ClientState &state, AppUpdate &update, const std::string &path) { + void accept_screenshot_directory(ClientState &state, AppUpdate &update, std::string_view path) { if (state.modal.id != ModalId::directory_picker) { return; } - if (state.settings.screenshotDirectory != path) { - state.settings.screenshotDirectory = path; + if (state.settings.controls.screenshotDirectory != path) { + state.settings.controls.screenshotDirectory = path; state.settings.dirty = true; update.persistence.settingsChanged = true; } @@ -2921,7 +2974,7 @@ namespace app { if (state.modal.id == ModalId::quit_progress) { close_modal(state); } - show_notification(state, succeeded ? NotificationKind::success : NotificationKind::error, succeeded ? "App closed" : "Couldn't quit app", message.empty() ? target.appName : std::move(message), succeeded ? 5000 : 0); + show_notification(state, succeeded ? NotificationKind::success : NotificationKind::error, succeeded ? "App closed" : "Couldn't quit app", message.empty() ? target.appName : message, succeeded ? 5000 : 0); state.appQuit = {}; } diff --git a/src/app/client_state.h b/src/app/client_state.h index cabdf8a..cdc7bfb 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -370,6 +370,14 @@ namespace app { input::StreamMenu keyboard; ///< Reuse the stream keyboard's pages, labels and focus geometry locally. }; + /** + * @brief Persisted storage and placement preferences for local streaming controls. + */ + struct LocalStreamPreferences { + std::string screenshotDirectory; ///< Screenshot folder; empty selects the title-scoped screenshots directory. + bool keyboardDockTop = false; ///< Dock the stream keyboard to the top instead of the bottom. + }; + /** * @brief State owned by the settings, log viewer, and saved-file workflows. */ @@ -390,8 +398,7 @@ namespace app { bool playAudioOnPc = false; ///< True when the host PC should continue local audio playback during streaming. bool showPerformanceStats = false; ///< Display live performance telemetry and include it in the stream summary. bool playAudioOnXbox = true; ///< True when the Xbox should decode and play streamed audio locally. - std::string screenshotDirectory; ///< Screenshot folder; empty selects the title-scoped screenshots directory. - bool keyboardDockTop = false; ///< Dock the stream keyboard to the top instead of the bottom. + LocalStreamPreferences controls; ///< Screenshot storage and keyboard placement preferences. bool dirty = false; ///< True when persisted TOML-backed settings changed and should be saved. std::vector savedFiles; ///< Saved-file catalog shown on the reset settings page. bool savedFilesDirty = true; ///< True when the saved-file catalog should be refreshed. @@ -751,6 +758,6 @@ namespace app { * @param update Receives persistence and modal closure flags. * @param path Selected folder; empty restores the title-scoped default. */ - void accept_screenshot_directory(ClientState &state, AppUpdate &update, const std::string &path); + void accept_screenshot_directory(ClientState &state, AppUpdate &update, std::string_view path); } // namespace app diff --git a/src/app/screenshot_library.cpp b/src/app/screenshot_library.cpp index 19fa48b..085afb7 100644 --- a/src/app/screenshot_library.cpp +++ b/src/app/screenshot_library.cpp @@ -17,6 +17,9 @@ #include // local includes +#ifdef NXDK + #include "src/_nxdk_compat/include/moonlight_time.h" +#endif #include "src/platform/filesystem_utils.h" #include "src/startup/storage_paths.h" @@ -33,7 +36,7 @@ namespace { for (char c : field) { switch (c) { case '\\': - result += "\\\\"; + result += R"(\\)"; break; case '\t': result += "\\t"; @@ -140,8 +143,7 @@ namespace app { ScreenshotRecord record; record.deleted = fields[0] == "D"; unsigned int port = 0; - std::istringstream numbers(fields[1] + " " + fields[2] + " " + fields[3]); - if (!(numbers >> record.capturedAt >> record.source.appId >> port) || !(numbers >> std::ws).eof() || port == 0 || port > 65535) { + if (std::istringstream numbers(fields[1] + " " + fields[2] + " " + fields[3]); !(numbers >> record.capturedAt >> record.source.appId >> port) || !(numbers >> std::ws).eof() || port == 0 || port > 65535) { return std::nullopt; } record.source.hostPort = static_cast(port); @@ -175,8 +177,7 @@ namespace app { // records are read, so an incomplete capture never poisons the remaining index. const std::string append = "\n" + line + "\n"; const bool written = std::fwrite(append.data(), 1, append.size(), file) == append.size(); - const bool closed = std::fclose(file) == 0; - if (!written || !closed) { + if (const bool closed = std::fclose(file) == 0; !written || !closed) { error = "Cannot finalize screenshot index."; return false; } @@ -196,8 +197,7 @@ namespace app { bool oversized = false; while (std::fgets(buffer.data(), static_cast(buffer.size()), file) != nullptr) { std::string_view line(buffer.data()); - const bool complete = !line.empty() && line.back() == '\n'; - if (!complete || oversized) { + if (const bool complete = !line.empty() && line.back() == '\n'; !complete || oversized) { oversized = !complete; result.error = "Some incomplete or damaged screenshot records were skipped."; continue; @@ -209,20 +209,22 @@ namespace app { if (line.empty()) { continue; } - if (auto record = parse_screenshot(line); record.has_value()) { - if (screenshot_source_matches(record->source, source)) { - if (record->deleted) { - auto &entries = result.entries; - entries.erase(std::remove_if(entries.begin(), entries.end(), [&](const ScreenshotRecord &entry) { - return entry.path == record->path; - }), - entries.end()); - } else { - result.entries.push_back(std::move(*record)); - } - } - } else { + auto record = parse_screenshot(line); + if (!record.has_value()) { result.error = "Some incomplete or damaged screenshot records were skipped."; + continue; + } + if (!screenshot_source_matches(record->source, source)) { + continue; + } + if (record->deleted) { + auto &entries = result.entries; + entries.erase(std::remove_if(entries.begin(), entries.end(), [&](const ScreenshotRecord &entry) { + return entry.path == record->path; + }), + entries.end()); + } else { + result.entries.push_back(std::move(*record)); } } if (std::ferror(file) != 0) { @@ -261,8 +263,7 @@ namespace app { } const std::string append = "\n" + line + "\n"; const bool written = std::fwrite(append.data(), 1, append.size(), file) == append.size(); - const bool closed = std::fclose(file) == 0; - if (!written || !closed) { + if (const bool closed = std::fclose(file) == 0; !written || !closed) { error = "Screenshot deleted, but its index could not be updated."; return false; } @@ -291,8 +292,18 @@ namespace app { std::string screenshot_time_label(std::int64_t capturedAt) { const auto time = static_cast(capturedAt); std::array label {}; - const std::tm *utc = capturedAt >= 0 && static_cast(time) == capturedAt ? std::gmtime(&time) : nullptr; - if (utc == nullptr || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", utc) == 0) { + if (capturedAt < 0 || static_cast(time) != capturedAt) { + return "Capture time unavailable"; + } + std::tm utc {}; +#ifdef NXDK + const bool converted = moonlight_gmtime(time, utc); +#elif defined(_WIN32) + const bool converted = gmtime_s(&utc, &time) == 0; +#else + const bool converted = gmtime_r(&time, &utc) != nullptr; +#endif + if (!converted || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", &utc) == 0) { return "Capture time unavailable"; } return label.data(); diff --git a/src/input/stream_input.cpp b/src/input/stream_input.cpp index 1284b76..812e497 100644 --- a/src/input/stream_input.cpp +++ b/src/input/stream_input.cpp @@ -133,7 +133,8 @@ namespace input { release_source(VIRTUAL_MOUSE_SOURCE); const int previous = mouseDevice_; mouseDevice_ = device; - mouseX_ = mouseY_ = 0; + mouseX_ = 0; + mouseY_ = 0; mouseElapsed_ = 0; for (int i = 0; i < 4; ++i) { if (slots_[i].device != -1 && (slots_[i].device == previous || slots_[i].device == device)) { @@ -164,15 +165,15 @@ namespace input { mouse_button(VIRTUAL_MOUSE_SOURCE, BUTTON_RIGHT, (state.buttonFlags & B_FLAG) != 0); mouseX_ += mouse_axis(state.leftStickX) * 900 * seconds; mouseY_ -= mouse_axis(state.leftStickY) * 900 * seconds; - const int x = static_cast(mouseX_); - const int y = static_cast(mouseY_); + const auto x = static_cast(mouseX_); + const auto y = static_cast(mouseY_); mouseX_ -= x; mouseY_ -= y; mouse_move(x, y); } } - void StreamInput::digital(StreamPacketType type, std::map> &held, int source, int code, bool pressed, int modifiers) { + void StreamInput::digital(StreamPacketType type, std::map> &held, int source, int code, bool pressed, int modifiers) const { if (code <= 0 || (captured_ && pressed)) { return; } @@ -181,7 +182,7 @@ namespace input { if (!pressed) { return; } - entry = held.emplace(code, std::set {}).first; + entry = held.try_emplace(code).first; } auto &sources = entry->second; const bool wasPressed = !sources.empty(); @@ -210,7 +211,7 @@ namespace input { digital(StreamPacketType::mouse_button, buttons_, source, button, pressed, 0); } - void StreamInput::mouse_move(int x, int y) { + void StreamInput::mouse_move(int x, int y) const { if (!captured_ && (x != 0 || y != 0)) { StreamPacket packet {StreamPacketType::mouse_move}; packet.x = std::clamp(x, -32768, 32767); @@ -219,7 +220,7 @@ namespace input { } } - void StreamInput::scroll(int x, int y) { + void StreamInput::scroll(int x, int y) const { if (!captured_ && (x != 0 || y != 0)) { StreamPacket packet {StreamPacketType::scroll}; packet.x = std::clamp(x, -127, 127); @@ -280,107 +281,61 @@ namespace input { if (usage >= 89 && usage <= 97) { return 0x61 + usage - 89; } - switch (usage) { - case 39: - return '0'; - case 40: - return 0x0D; - case 41: - return 0x1B; - case 42: - return 0x08; - case 43: - return 0x09; - case 44: - return 0x20; - case 45: - return 0xBD; - case 46: - return 0xBB; - case 47: - return 0xDB; - case 48: - return 0xDD; - case 49: - case 50: - case 100: - return 0xDC; - case 51: - return 0xBA; - case 52: - return 0xDE; - case 53: - return 0xC0; - case 54: - return 0xBC; - case 55: - return 0xBE; - case 56: - return 0xBF; - case 57: - return 0x14; - case 70: - return 0x12C; - case 71: - return 0x91; - case 72: - return 0x13; - case 73: - return 0x12D; - case 74: - return 0x124; - case 75: - return 0x121; - case 76: - return 0x12E; - case 77: - return 0x123; - case 78: - return 0x122; - case 79: - return 0x127; - case 80: - return 0x125; - case 81: - return 0x128; - case 82: - return 0x126; - case 83: - return 0x190; - case 84: - return 0x16F; - case 85: - return 0x6A; - case 86: - return 0x6D; - case 87: - return 0x6B; - case 88: - return 0x10D; - case 98: - return 0x60; - case 99: - return 0x6E; - case 101: - return 0x15D; - case 224: - return 0xA2; - case 225: - return 0xA0; - case 226: - return 0xA4; - case 227: - return 0x15B; - case 228: - return 0x1A3; - case 229: - return 0xA1; - case 230: - return 0x1A5; - case 231: - return 0x15C; - default: - return 0; - } + constexpr std::array, 50> overrides {{ + {39, '0'}, + {40, 0x0D}, + {41, 0x1B}, + {42, 0x08}, + {43, 0x09}, + {44, 0x20}, + {45, 0xBD}, + {46, 0xBB}, + {47, 0xDB}, + {48, 0xDD}, + {49, 0xDC}, + {50, 0xDC}, + {51, 0xBA}, + {52, 0xDE}, + {53, 0xC0}, + {54, 0xBC}, + {55, 0xBE}, + {56, 0xBF}, + {57, 0x14}, + {70, 0x12C}, + {71, 0x91}, + {72, 0x13}, + {73, 0x12D}, + {74, 0x124}, + {75, 0x121}, + {76, 0x12E}, + {77, 0x123}, + {78, 0x122}, + {79, 0x127}, + {80, 0x125}, + {81, 0x128}, + {82, 0x126}, + {83, 0x190}, + {84, 0x16F}, + {85, 0x6A}, + {86, 0x6D}, + {87, 0x6B}, + {88, 0x10D}, + {98, 0x60}, + {99, 0x6E}, + {100, 0xDC}, + {101, 0x15D}, + {224, 0xA2}, + {225, 0xA0}, + {226, 0xA4}, + {227, 0x15B}, + {228, 0x1A3}, + {229, 0xA1}, + {230, 0x1A5}, + {231, 0x15C}, + }}; + const auto entry = std::lower_bound(overrides.begin(), overrides.end(), usage, [](const auto &binding, int key) { + return binding.first < key; + }); + return entry != overrides.end() && entry->first == usage ? entry->second : 0; } } // namespace input diff --git a/src/input/stream_input.h b/src/input/stream_input.h index 32ddc0c..bd026ff 100644 --- a/src/input/stream_input.h +++ b/src/input/stream_input.h @@ -115,13 +115,13 @@ namespace input { * @param x Horizontal delta. * @param y Vertical delta. */ - void mouse_move(int x, int y); + void mouse_move(int x, int y) const; /** * @brief Forward wheel ticks on both axes. * @param x Horizontal ticks. * @param y Vertical ticks. */ - void scroll(int x, int y); + void scroll(int x, int y) const; /** * @brief Release every key and mouse button belonging to one source. * @param source Source that disconnected or changed mode. @@ -163,7 +163,7 @@ namespace input { * @param pressed Transition direction. * @param modifiers Keyboard modifiers. */ - void digital(StreamPacketType type, std::map> &held, int source, int code, bool pressed, int modifiers); + void digital(StreamPacketType type, std::map> &held, int source, int code, bool pressed, int modifiers) const; Sink sink_; ///< Ordered transport sink. std::array slots_ {}; ///< Stable slots, never compacted on removal. std::map> keys_; ///< Sources holding each host key. diff --git a/src/input/stream_menu.cpp b/src/input/stream_menu.cpp index 45b7c6a..55eaeb2 100644 --- a/src/input/stream_menu.cpp +++ b/src/input/stream_menu.cpp @@ -184,7 +184,12 @@ namespace input { if (page != StreamMenuPage::radial || std::max(std::abs(static_cast(x)), std::abs(static_cast(y))) < WHEEL_DEADZONE) { return false; } - const int sector = std::abs(static_cast(y)) >= std::abs(static_cast(x)) ? (y > 0 ? 0 : 2) : (x > 0 ? 1 : 3); + int sector; + if (std::abs(static_cast(y)) >= std::abs(static_cast(x))) { + sector = y > 0 ? 0 : 2; + } else { + sector = x > 0 ? 1 : 3; + } const auto action = static_cast((right ? 4 : 0) + sector); const bool changed = action != selected; selected = action; @@ -258,8 +263,10 @@ namespace input { std::string StreamMenu::key_label(int index) const { const int catalogIndex = keyboard_cells().at(index).key; if (catalogIndex == KEYBOARD_PAGE_KEY) { - return keyboardLayout == 0 ? "?123" : keyboardLayout == 1 ? "PC keys" : - "ABC"; + if (keyboardLayout == 0) { + return "?123"; + } + return keyboardLayout == 1 ? "PC keys" : "ABC"; } if (catalogIndex == KEYBOARD_CAPS_KEY) { return "Caps"; @@ -275,8 +282,7 @@ namespace input { } int StreamMenu::key_modifiers(int code) const { - const int key = keyboard_cells().at(keyIndex).key; - if (key < static_cast(KEYS.size()) && KEYS[key].code == code) { + if (const int key = keyboard_cells().at(keyIndex).key; key < static_cast(KEYS.size()) && KEYS[key].code == code) { return binding_modifiers(key); } return modifiers | ((shiftOnce != (caps && code >= 'A' && code <= 'Z')) ? MODIFIER_SHIFT : 0); @@ -439,8 +445,10 @@ namespace input { 116, 117, }}); - return keyboardLayout == 0 ? letters : keyboardLayout == 1 ? symbols : - desktop; + if (keyboardLayout == 0) { + return letters; + } + return keyboardLayout == 1 ? symbols : desktop; } void StreamMenu::toggle_keyboard_page() { diff --git a/src/input/stream_menu.h b/src/input/stream_menu.h index 61ee9c9..fd014e2 100644 --- a/src/input/stream_menu.h +++ b/src/input/stream_menu.h @@ -165,6 +165,8 @@ namespace input { * @return Effective modifiers for that binding. */ int binding_modifiers(int index) const; + + public: int keyboardColumn_ = 4; ///< Preferred column retained while traversing wide keys. int keyboardRow_ = 1; ///< Preferred row retained while traversing tall side controls. std::array previousSticks_ {}; ///< Previous sampled axes so a stationary stick cannot steal the other wheel's selection. diff --git a/src/input/ui_input.cpp b/src/input/ui_input.cpp index 8ea0df0..4b0dc47 100644 --- a/src/input/ui_input.cpp +++ b/src/input/ui_input.cpp @@ -10,10 +10,35 @@ #include namespace input { + UiCommand direction_command(int x, int y) { + if (x != 0) { + return x > 0 ? UiCommand::move_right : UiCommand::move_left; + } + if (y != 0) { + return y > 0 ? UiCommand::move_down : UiCommand::move_up; + } + return UiCommand::none; + } + + std::pair navigation_step(UiCommand command) { + switch (command) { + case UiCommand::move_left: + return {-1, 0}; + case UiCommand::move_right: + return {1, 0}; + case UiCommand::move_up: + return {0, -1}; + case UiCommand::move_down: + return {0, 1}; + default: + return {0, 0}; + } + } + UiCommand DirectionNavigation::update_stick(short x, short y, std::uint32_t now) { const int horizontal = std::abs(static_cast(x)); const int vertical = std::abs(static_cast(y)); - const auto direction = horizontal > vertical ? (x > 0 ? UiCommand::move_right : UiCommand::move_left) : (y > 0 ? UiCommand::move_up : UiCommand::move_down); + const auto direction = horizontal > vertical ? direction_command(x, 0) : direction_command(0, -static_cast(y)); const int threshold = direction == command_ ? STICK_NAVIGATION_RELEASE_THRESHOLD : STICK_NAVIGATION_THRESHOLD; return update(std::max(horizontal, vertical) >= threshold ? direction : UiCommand::none, now); } @@ -25,7 +50,8 @@ namespace input { } if (command != command_) { command_ = command; - pressedAt_ = repeatedAt_ = now; + pressedAt_ = now; + repeatedAt_ = now; repeated_ = false; return command; } diff --git a/src/input/ui_input.h b/src/input/ui_input.h index 218dc7b..7a55169 100644 --- a/src/input/ui_input.h +++ b/src/input/ui_input.h @@ -41,11 +41,56 @@ namespace input { */ bool is_repeatable_ui_command(UiCommand command); + /** + * @brief Map signed directional steps to a command, preferring horizontal movement. + * @param x Horizontal step, positive rightward. + * @param y Vertical step, positive downward. + * @return Directional command, or none for a centered input. + */ + UiCommand direction_command(int x, int y); + + /** + * @brief Convert a directional command to a single keyboard or grid step. + * @param command Navigation command. + * @return Horizontal and vertical steps; zero for other commands. + */ + std::pair navigation_step(UiCommand command); + /** * @brief Generates UI repeats from transitions, including USB keyboards without OS autorepeat. */ class KeyboardNavigation { public: + /** + * @brief Start with no held navigation keys. + */ + KeyboardNavigation() = default; + /** + * @brief Copy the current hold and repeat state. + * @param other Navigation state to copy. + */ + KeyboardNavigation(const KeyboardNavigation &other) = default; + /** + * @brief Replace held navigation keys and repeat state with a copy. + * @param other Navigation state to copy. + * @return This navigation state. + */ + KeyboardNavigation &operator=(const KeyboardNavigation &other) = default; + /** + * @brief Release retained navigation keys. + */ + ~KeyboardNavigation() = default; + /** + * @brief Transfer held navigation keys without a potentially throwing container move. + * @param other Navigation state being transferred. + */ + KeyboardNavigation(KeyboardNavigation &&other) noexcept = default; + /** + * @brief Transfer navigation state when replacing the active input owner. + * @param other Navigation state being transferred. + * @return This navigation state. + */ + KeyboardNavigation &operator=(KeyboardNavigation &&other) noexcept = default; /** * @brief Register an initial key press and issue its command exactly once. * @param key Physical key identity, such as an SDL scancode. diff --git a/src/input/usb_hid.cpp b/src/input/usb_hid.cpp index c76d827..dbd8fb7 100644 --- a/src/input/usb_hid.cpp +++ b/src/input/usb_hid.cpp @@ -20,7 +20,7 @@ namespace input { } } - void BootHidInput::update(Device &device, const std::bitset<256> &next) { + void BootHidInput::update(Device &device, const std::bitset<256> &next) const { const auto changed = device.held ^ next; const auto aggregate = [&]() { std::bitset<256> all; @@ -59,6 +59,45 @@ namespace input { } } + void BootHidInput::keyboard_report(Device &device, const std::uint8_t *bytes, std::size_t length) const { + if (length < 8) { + return; + } + auto held = device.held; + for (int bit = 0; bit < 8; ++bit) { + held[224 + bit] = (static_cast(bytes[0]) & (std::byte {1} << bit)) != std::byte {0}; + } + if (const bool rollover = std::any_of(bytes + 2, bytes + 8, [](std::uint8_t code) { + return code >= 1 && code <= 3; + }); + !rollover) { + for (int code = 0; code < 224; ++code) { + held[code] = false; + } + for (int i = 2; i < 8; ++i) { + if (bytes[i] >= 4 && bytes[i] < 224) { + held[bytes[i]] = true; + } + } + } + update(device, held); + } + + void BootHidInput::mouse_report(Device &device, const std::uint8_t *bytes) const { + std::bitset<256> held; + // Boot protocol guarantees exactly three buttons and two signed 8-bit axes. + // Wheel/report-ID layouts require descriptor parsing and must not be guessed. + for (int bit = 0; bit < 3; ++bit) { + held[bit + 1] = (static_cast(bytes[0]) & (std::byte {1} << bit)) != std::byte {0}; + } + update(device, held); + const int x = bytes[1] < 128 ? bytes[1] : bytes[1] - 256; + const int y = bytes[2] < 128 ? bytes[2] : bytes[2] - 256; + if (x != 0 || y != 0) { + sink_({BootHidEventType::motion, 0, false, x, y}); + } + } + void BootHidInput::report(int device, const std::uint8_t *bytes, std::size_t length) { const auto found = devices_.find(device); if (found == devices_.end() || bytes == nullptr) { @@ -66,40 +105,9 @@ namespace input { } auto &state = found->second; if (state.protocol == 1) { - if (length < 8) { - return; - } - auto held = state.held; - for (int bit = 0; bit < 8; ++bit) { - held[224 + bit] = (bytes[0] & (1U << bit)) != 0; - } - const bool rollover = std::any_of(bytes + 2, bytes + 8, [](std::uint8_t code) { - return code >= 1 && code <= 3; - }); - if (!rollover) { - for (int code = 0; code < 224; ++code) { - held[code] = false; - } - for (int i = 2; i < 8; ++i) { - if (bytes[i] >= 4 && bytes[i] < 224) { - held[bytes[i]] = true; - } - } - } - update(state, held); + keyboard_report(state, bytes, length); } else if (length >= 3) { - std::bitset<256> held; - // Boot protocol guarantees exactly three buttons and two signed 8-bit axes. - // Wheel/report-ID layouts require descriptor parsing and must not be guessed. - for (int bit = 0; bit < 3; ++bit) { - held[bit + 1] = (bytes[0] & (1U << bit)) != 0; - } - update(state, held); - const int x = bytes[1] < 128 ? bytes[1] : bytes[1] - 256; - const int y = bytes[2] < 128 ? bytes[2] : bytes[2] - 256; - if (x != 0 || y != 0) { - sink_({BootHidEventType::motion, 0, false, x, y}); - } + mouse_report(state, bytes); } } } // namespace input diff --git a/src/input/usb_hid.h b/src/input/usb_hid.h index 0d3d0ec..ca86189 100644 --- a/src/input/usb_hid.h +++ b/src/input/usb_hid.h @@ -80,7 +80,20 @@ namespace input { * @param device Device being updated. * @param next New held inputs. */ - void update(Device &device, const std::bitset<256> &next); + void update(Device &device, const std::bitset<256> &next) const; + /** + * @brief Decode keyboard modifiers and non-rollover key slots from a validated report pointer. + * @param device Retained keyboard interface. + * @param bytes Report data. + * @param length Available bytes, checked before reading a boot keyboard report. + */ + void keyboard_report(Device &device, const std::uint8_t *bytes, std::size_t length) const; + /** + * @brief Decode the three standard buttons and signed axes from a boot mouse report. + * @param device Retained mouse interface. + * @param bytes Validated report containing at least three bytes. + */ + void mouse_report(Device &device, const std::uint8_t *bytes) const; Sink sink_; ///< Validated event consumer. std::map devices_; ///< Per-interface state. }; diff --git a/src/main.cpp b/src/main.cpp index 6722720..5a1c9af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,8 +49,8 @@ namespace { state.settings.playAudioOnPc = settings.playAudioOnPc; state.settings.showPerformanceStats = settings.showPerformanceStats; state.settings.playAudioOnXbox = settings.playAudioOnXbox; - state.settings.screenshotDirectory = settings.screenshotDirectory; - state.settings.keyboardDockTop = settings.keyboardDockTop; + state.settings.controls.screenshotDirectory = settings.screenshotDirectory; + state.settings.controls.keyboardDockTop = settings.keyboardDockTop; state.settings.dirty = false; } diff --git a/src/network/host_pairing.cpp b/src/network/host_pairing.cpp index 78341c1..68c261a 100644 --- a/src/network/host_pairing.cpp +++ b/src/network/host_pairing.cpp @@ -2738,8 +2738,7 @@ namespace network { return false; } uint32_t status = 0; - std::string message; - if (response.statusCode != 200 || !extract_root_status(response.body, &status, &message) || status != 200) { + if (std::string message; response.statusCode != 200 || !extract_root_status(response.body, &status, &message) || status != 200) { return append_error(errorMessage, message.empty() ? "The host rejected the quit request. Another client may own this session" : message); } // Like the other Moonlight clients, verify /serverinfo: some hosts return diff --git a/src/platform/filesystem_utils.cpp b/src/platform/filesystem_utils.cpp index d6f91a3..4ccc14b 100644 --- a/src/platform/filesystem_utils.cpp +++ b/src/platform/filesystem_utils.cpp @@ -7,11 +7,13 @@ // standard includes #include +#include #include #include #include #include #include +#include // platform includes #if defined(_WIN32) || defined(NXDK) @@ -42,8 +44,8 @@ namespace { return; } constexpr std::string_view letters = "CEFGXYZ"; - constexpr int partitions[] = {2, 1, 6, 7, 3, 4, 5}; - const char letter = static_cast(std::toupper(static_cast(path[0]))); + constexpr std::array partitions {2, 1, 6, 7, 3, 4, 5}; + const auto letter = static_cast(std::toupper(static_cast(path[0]))); const auto index = letters.find(letter); if (index != std::string_view::npos && !nxIsDriveMounted(letter)) { const auto target = std::string("\\Device\\Harddisk0\\Partition") + std::to_string(partitions[index]) + "\\"; @@ -82,7 +84,7 @@ namespace { #endif if (errorMessage != nullptr) { - *errorMessage = "Failed to create directory '" + path + "': " + std::strerror(errno); + *errorMessage = "Failed to create directory '" + path + "': " + std::error_code(errno, std::generic_category()).message(); } return false; } @@ -123,8 +125,7 @@ namespace platform { } return result; } - const DWORD attributes = GetFileAttributesA(path.c_str()); - if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { + if (const DWORD attributes = GetFileAttributesA(path.c_str()); attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { result.error = "Folder is unavailable: " + path; return result; } @@ -155,7 +156,7 @@ namespace platform { } DIR *directory = opendir(path.c_str()); if (directory == nullptr) { - result.error = "Unable to read folder: " + std::string(std::strerror(errno)); + result.error = "Unable to read folder: " + std::string(std::error_code(errno, std::generic_category()).message()); return result; } errno = 0; @@ -173,7 +174,7 @@ namespace platform { const int readError = errno; closedir(directory); if (readError != 0) { - result.error = "Unable to finish reading folder: " + std::string(std::strerror(readError)); + result.error = "Unable to finish reading folder: " + std::string(std::error_code(readError, std::generic_category()).message()); } #endif std::sort(result.paths.begin(), result.paths.end()); @@ -196,7 +197,7 @@ namespace platform { bool valid_directory_name(std::string_view name) { return !name.empty() && name.size() <= 42 && name != "." && name != ".." && name.back() != ' ' && name.back() != '.' && std::none_of(name.begin(), name.end(), [](unsigned char character) { - return character < 32 || character >= 127 || std::string_view("<>:\"/\\|?*+=,;[]").find(static_cast(character)) != std::string_view::npos; + return character < 32 || character >= 127 || std::string_view(R"(<>:"/\|?*+=,;[])").find(static_cast(character)) != std::string_view::npos; }); } @@ -211,12 +212,11 @@ namespace platform { #endif const auto child = join_path(parent, name); #if defined(_WIN32) || defined(NXDK) - const int result = _mkdir(child.c_str()); + if (const int result = _mkdir(child.c_str()); result != 0) { #else - const int result = mkdir(child.c_str(), 0750); + if (const int result = mkdir(child.c_str(), 0750); result != 0) { #endif - if (result != 0) { - error = errno == EEXIST ? "A file or folder with this name already exists." : "Could not create folder: " + std::string(std::strerror(errno)); + error = errno == EEXIST ? "A file or folder with this name already exists." : "Could not create folder: " + std::string(std::error_code(errno, std::generic_category()).message()); return false; } return true; @@ -253,7 +253,7 @@ namespace platform { if (errno == EEXIST) { continue; } - error = "Cannot write to this folder: " + std::string(std::strerror(errno)); + error = "Cannot write to this folder: " + std::string(std::error_code(errno, std::generic_category()).message()); return false; } close(file); diff --git a/src/platform/usb_input.cpp b/src/platform/usb_input.cpp index 63b87b3..3551bd1 100644 --- a/src/platform/usb_input.cpp +++ b/src/platform/usb_input.cpp @@ -12,7 +12,11 @@ #include // nxdk includes -#include +#ifdef NXDK + #include +#else + #include +#endif #include // lib includes @@ -70,11 +74,33 @@ namespace { std::array bytes {}; ///< Maximum boot keyboard report. }; - std::array reports {}; ///< Single-CPU DPC-to-event-thread queue. - unsigned head = 0; ///< Next queue write index, guarded by DPC IRQL. - unsigned tail = 0; ///< Next queue read index, guarded by DPC IRQL. - bool overflow = false; ///< An overflow requires a full input release. - int nextDevice = 1; ///< Distinguishes reused USB interface addresses. + /** + * @brief Stable USB callback context; zero marks a reusable disconnected interface. + */ + struct UsbConnection { + int id = 0; ///< Unique generation assigned when the interface connects. + }; + + /** + * @brief Fixed storage shared by USB callbacks and the SDL event-thread consumer. + */ + struct UsbInputState { + std::array reports {}; ///< Single-CPU DPC-to-event-thread queue. + unsigned head = 0; ///< Next queue write index, guarded by DPC IRQL. + unsigned tail = 0; ///< Next queue read index, guarded by DPC IRQL. + bool overflow = false; ///< An overflow requires a full input release. + int nextDevice = 1; ///< Distinguishes reused USB interface addresses. + std::array connections {}; ///< Callback contexts never allocate at DPC level. + }; + + /** + * @brief Access the statically initialized single-CPU USB bridge state. + * @return Queue and callback contexts, guarded by DPC IRQL where required. + */ + UsbInputState &usb_state() { + static UsbInputState state {}; + return state; + } /** * @brief Copy an operation without allocation or SDL calls from an interrupt callback. @@ -82,12 +108,11 @@ namespace { */ void enqueue(const UsbReport &report) { const KIRQL previous = KeRaiseIrqlToDpcLevel(); - const unsigned next = (head + 1) % reports.size(); - if (next == tail) { - overflow = true; + if (const unsigned next = (usb_state().head + 1) % usb_state().reports.size(); next == usb_state().tail) { + usb_state().overflow = true; } else { - reports[head] = report; - head = next; + usb_state().reports[usb_state().head] = report; + usb_state().head = next; } KfLowerIrql(previous); } @@ -106,7 +131,7 @@ namespace { return; } UsbReport report; - report.device = static_cast(reinterpret_cast(device->user_data)); + report.device = static_cast(device->user_data)->id; if (status < 0) { report.operation = -1; } else { @@ -132,11 +157,20 @@ namespace { if (usbh_hid_set_protocol(device, 0) != 0) { return; } - const int id = nextDevice++; - device->user_data = reinterpret_cast(static_cast(id)); + auto &connections = usb_state().connections; + const auto context = std::find_if(connections.begin(), connections.end(), [](const UsbConnection &entry) { + return entry.id == 0; + }); + if (context == connections.end()) { + return; + } + const int id = usb_state().nextDevice++; + context->id = id; + device->user_data = &*context; enqueue({id, device->bProtocolCode}); if (usbh_hid_start_int_read(device, 0, receive) != 0) { enqueue({id, -1}); + context->id = 0; device->user_data = nullptr; } } @@ -149,7 +183,10 @@ namespace { void disconnected(HID_DEV_T *device, int status) { (void) status; if (device->user_data != nullptr) { - enqueue({static_cast(reinterpret_cast(device->user_data)), -1}); + auto *context = static_cast(device->user_data); + enqueue({context->id, -1}); + context->id = 0; + device->user_data = nullptr; } } @@ -163,11 +200,23 @@ namespace { } else if (event.type == input::BootHidEventType::motion) { SDL_SendMouseMotion(SDL_GetKeyboardFocus(), 0, 1, event.x, event.y); } else { - const Uint8 button = event.code == 1 ? SDL_BUTTON_LEFT : event.code == 2 ? SDL_BUTTON_RIGHT : - SDL_BUTTON_MIDDLE; + const Uint8 button = std::array {SDL_BUTTON_LEFT, SDL_BUTTON_RIGHT, SDL_BUTTON_MIDDLE}.at(event.code - 1); SDL_SendMouseButton(SDL_GetKeyboardFocus(), 0, event.pressed ? SDL_PRESSED : SDL_RELEASED, button); } } + + /** + * @brief Re-register connected HID interfaces after releasing an overflowed report queue. + * @param input Event-thread boot HID decoder. + */ + void reconnect_boot_devices(input::BootHidInput &input) { + for (auto *device = usbh_hid_get_device_list(); device != nullptr; device = device->next) { + if (device->user_data == nullptr) { + continue; + } + input.connect(static_cast(device->user_data)->id, device->bProtocolCode); + } + } } // namespace /** @@ -186,25 +235,21 @@ extern "C" void moonlight_usb_input_pump() { for (;;) { UsbReport report; const KIRQL previous = KeRaiseIrqlToDpcLevel(); - const bool reset = overflow; - overflow = false; + const bool reset = usb_state().overflow; + usb_state().overflow = false; if (reset) { - tail = head; + usb_state().tail = usb_state().head; } - const bool available = tail != head; + const bool available = usb_state().tail != usb_state().head; if (available) { - report = reports[tail]; - tail = (tail + 1) % reports.size(); + report = usb_state().reports[usb_state().tail]; + usb_state().tail = (usb_state().tail + 1) % usb_state().reports.size(); } KfLowerIrql(previous); // Keep device identities after overflow; release their old state and accept future reports. if (reset) { input.clear(); - for (auto *device = usbh_hid_get_device_list(); device != nullptr; device = device->next) { - if (device->user_data != nullptr) { - input.connect(static_cast(reinterpret_cast(device->user_data)), device->bProtocolCode); - } - } + reconnect_boot_devices(input); } if (!available) { break; diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index 7f9d1da..937cc70 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -370,7 +370,8 @@ namespace streaming { } void FfmpegStreamBackend::invalidate_overlay_texture() { - statisticsBitmapChanged_ = statisticsTextureChanged_ = true; + statisticsBitmapChanged_ = true; + statisticsTextureChanged_ = true; statisticsTextureDamage_ = {0, 0, statisticsOverlay_.width(), statisticsOverlay_.height()}; if (statisticsTexture_ != nullptr) { int width = 0; @@ -396,7 +397,8 @@ namespace streaming { } else { statisticsTextureDamage_ = region; } - statisticsBitmapChanged_ = statisticsTextureChanged_ = true; + statisticsBitmapChanged_ = true; + statisticsTextureChanged_ = true; } bool FfmpegStreamBackend::has_pending_overlay() const { @@ -415,7 +417,7 @@ namespace streaming { } } - bool FfmpegStreamBackend::request_screenshot(const std::string &directory, const app::ScreenshotSource &source) { + bool FfmpegStreamBackend::request_screenshot(std::string_view directory, const app::ScreenshotSource &source) { if (const auto result = take_screenshot_result(); !result.empty()) { logging::info("screenshots", result); } @@ -444,9 +446,13 @@ namespace streaming { screenshot_.directory = directory; screenshot_.capturedAt = static_cast(std::time(nullptr)); screenshot_.result.clear(); - screenshot_.progress.store(0, std::memory_order_relaxed); - screenshot_.complete.store(false, std::memory_order_relaxed); - screenshot_.thread = SDL_CreateThread(save_screenshot_worker, "save-screenshot", &screenshot_); + screenshot_.progress.store(0); + screenshot_.complete.store(false); + screenshot_.thread = SDL_CreateThread([](auto *context) { + return save_screenshot_worker(static_cast(context)); + }, + "save-screenshot", + &screenshot_); if (screenshot_.thread == nullptr) { screenshot_.frame = {}; logging::warn("screenshots", std::string("Cannot start screenshot worker: ") + SDL_GetError()); @@ -455,7 +461,7 @@ namespace streaming { } std::string FfmpegStreamBackend::take_screenshot_result() { - if (screenshot_.thread == nullptr || !screenshot_.complete.load(std::memory_order_acquire)) { + if (screenshot_.thread == nullptr || !screenshot_.complete.load()) { return {}; } SDL_WaitThread(screenshot_.thread, nullptr); @@ -464,7 +470,7 @@ namespace streaming { } int FfmpegStreamBackend::screenshot_progress() const { - return screenshot_.thread != nullptr ? screenshot_.progress.load(std::memory_order_relaxed) : -1; + return screenshot_.thread != nullptr ? screenshot_.progress.load() : -1; } /** @@ -504,60 +510,84 @@ namespace streaming { const auto written = SDL_RWwrite(writer.file, pixels, size, count); writer.position += static_cast(written * size); writer.written = std::max(writer.written, writer.position); - writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected), std::memory_order_relaxed); + writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected)); return written; } - int FfmpegStreamBackend::save_screenshot_worker(void *context) { - auto &job = *static_cast(context); - // Stay below the high-priority streaming event loop without starving behind - // ongoing USB/audio work on the Xbox's single CPU. + /** + * @brief Encode a BMP with byte-based progress, closing the file on every outcome. + * @param surface Converted BGR screenshot. + * @param path Destination filename. + * @param progress UI-readable completed percentage. + * @return True after both encoding and file finalization succeed. + */ + bool save_screenshot_bitmap(SDL_Surface &surface, const std::string &path, std::atomic &progress) { + SDL_RWops *file = SDL_RWFromFile(path.c_str(), "wb"); + if (file == nullptr) { + return false; + } + ScreenshotFileProgress writer {file, progress, 54 + static_cast((surface.w * 3 + 3) & ~3) * surface.h}; + SDL_RWops progressFile {}; + progressFile.hidden.unknown.data1 = &writer; + progressFile.seek = screenshot_file_seek; + progressFile.write = screenshot_file_write; + const bool encoded = SDL_SaveBMP_RW(&surface, &progressFile, 0) == 0; + const bool closed = SDL_RWclose(file) == 0; + const bool saved = encoded && closed; + if (!saved) { + std::remove(path.c_str()); + } + return saved; + } + + SDL_Surface *FfmpegStreamBackend::convert_screenshot_frame(const LatestVideoFrame &frame) { + SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, frame.width, frame.height, 24, SDL_PIXELFORMAT_BGR24); + if (surface == nullptr) { + return nullptr; + } + SwsContext *conversion = sws_getContext(frame.width, frame.height, AV_PIX_FMT_YUV420P, frame.width, frame.height, AV_PIX_FMT_BGR24, SWS_POINT, nullptr, nullptr, nullptr); + if (conversion == nullptr) { + SDL_FreeSurface(surface); + return nullptr; + } + const int *coefficients = sws_getCoefficients(frame.matrix == VideoColorMatrix::bt709 ? SWS_CS_ITU709 : SWS_CS_ITU601); + sws_setColorspaceDetails(conversion, coefficients, frame.fullRange, coefficients, 1, 0, 1 << 16, 1 << 16); + const std::array pixels {frame.yPlane.data(), frame.uPlane.data(), frame.vPlane.data(), nullptr}; + const std::array pitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; + std::array output {static_cast(surface->pixels), nullptr, nullptr, nullptr}; + const std::array outputPitch {surface->pitch, 0, 0, 0}; + const bool converted = sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height; + sws_freeContext(conversion); + if (!converted) { + SDL_FreeSurface(surface); + return nullptr; + } + return surface; + } + + int FfmpegStreamBackend::save_screenshot_worker(ScreenshotTask *context) { + auto &job = *context; + // Stay below the streaming event loop without starving behind USB/audio work. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_NORMAL); - job.progress.store(1, std::memory_order_relaxed); + job.progress.store(1); std::string error; const std::string path = app::next_screenshot_path(job.directory, job.capturedAt, error); - const auto &frame = job.frame; - SDL_Surface *surface = path.empty() ? nullptr : SDL_CreateRGBSurfaceWithFormat(0, frame.width, frame.height, 24, SDL_PIXELFORMAT_BGR24); - SwsContext *conversion = surface == nullptr ? nullptr : sws_getContext(frame.width, frame.height, AV_PIX_FMT_YUV420P, frame.width, frame.height, AV_PIX_FMT_BGR24, SWS_POINT, nullptr, nullptr, nullptr); + SDL_Surface *surface = path.empty() ? nullptr : convert_screenshot_frame(job.frame); + // Release the YUV snapshot before disk I/O so full-screen menus still fit in 64 MiB. + job.frame = {}; bool saved = false; - if (conversion != nullptr) { - const int *coefficients = sws_getCoefficients(frame.matrix == VideoColorMatrix::bt709 ? SWS_CS_ITU709 : SWS_CS_ITU601); - sws_setColorspaceDetails(conversion, coefficients, frame.fullRange, coefficients, 1, 0, 1 << 16, 1 << 16); - const std::array pixels {frame.yPlane.data(), frame.uPlane.data(), frame.vPlane.data(), nullptr}; - const std::array pitch {frame.yPitch, frame.uPitch, frame.vPitch, 0}; - std::array output {static_cast(surface->pixels), nullptr, nullptr, nullptr}; - const std::array outputPitch {surface->pitch, 0, 0, 0}; - const bool converted = sws_scale(conversion, pixels.data(), pitch.data(), 0, frame.height, output.data(), outputPitch.data()) == frame.height; - sws_freeContext(conversion); - // The BGR surface now owns the image. Return the YUV snapshot and scale - // buffers before disk I/O so opening a full-screen menu still fits in 64 MiB. - job.frame = {}; - if (converted) { - job.progress.store(10, std::memory_order_relaxed); - if (SDL_RWops *file = SDL_RWFromFile(path.c_str(), "wb"); file != nullptr) { - ScreenshotFileProgress writer {file, job.progress, 54 + static_cast((surface->w * 3 + 3) & ~3) * surface->h}; - SDL_RWops progressFile {}; - progressFile.hidden.unknown.data1 = &writer; - progressFile.seek = screenshot_file_seek; - progressFile.write = screenshot_file_write; - const bool encoded = SDL_SaveBMP_RW(surface, &progressFile, 0) == 0; - const bool closed = SDL_RWclose(file) == 0; - saved = encoded && closed; - if (!saved) { - std::remove(path.c_str()); - } - } - } + if (surface != nullptr) { + job.progress.store(10); + saved = save_screenshot_bitmap(*surface, path, job.progress); } SDL_FreeSurface(surface); if (saved) { - job.progress.store(99, std::memory_order_relaxed); + job.progress.store(99); job.result = app::append_screenshot({job.source, path, job.capturedAt}, app::screenshot_library_path(), error) ? "Saved " + path : "Saved " + path + "; could not update the screenshot library: " + error; } else { job.result = "Screenshot failed: " + (error.empty() ? std::string(SDL_GetError()) : error); } - job.frame = {}; - job.complete.store(true, std::memory_order_release); + job.complete.store(true); return 0; } @@ -820,8 +850,7 @@ namespace streaming { if (statisticsTextureChanged_) { const auto &pixels = statisticsOverlay_.argb_pixels(); const SDL_Rect damage {statisticsTextureDamage_.x, statisticsTextureDamage_.y, statisticsTextureDamage_.width, statisticsTextureDamage_.height}; - const auto offset = (static_cast(damage.y) * width + damage.x) * 4; - if (SDL_UpdateTexture(statisticsTexture_, &damage, pixels.data() + offset, width * 4) != 0) { + if (const auto offset = (static_cast(damage.y) * width + damage.x) * 4; SDL_UpdateTexture(statisticsTexture_, &damage, pixels.data() + offset, width * 4) != 0) { return; } statisticsTextureChanged_ = false; diff --git a/src/streaming/ffmpeg_stream_backend.h b/src/streaming/ffmpeg_stream_backend.h index 555e55c..260431d 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -9,6 +9,7 @@ #include #include #include +#include #include // lib includes @@ -150,7 +151,7 @@ namespace streaming { * @param source Server and app identity captured at request time. * @return True when queued; false before the first frame or while another capture is pending. */ - bool request_screenshot(const std::string &directory, const app::ScreenshotSource &source); + bool request_screenshot(std::string_view directory, const app::ScreenshotSource &source); /** * @brief Reap a completed screenshot worker without blocking the streaming event loop. * @return Completion or error detail, or empty when no result is ready. @@ -402,7 +403,7 @@ namespace streaming { */ struct ScreenshotTask { SDL_Thread *thread = nullptr; ///< Owned worker, joined after completion or at shutdown. - std::atomic complete = false; ///< Release/acquire publication of the completed result. + std::atomic complete = false; ///< Publishes the completed result before the UI can read it. std::atomic progress = 0; ///< Conversion, BMP write and index progress; completion is published separately. LatestVideoFrame frame; ///< Independently owned snapshot of the uncomposited YUV frame. app::ScreenshotSource source; ///< Original host and app identity. @@ -416,7 +417,13 @@ namespace streaming { * @param context ScreenshotTask owned by this backend until the worker is joined. * @return Zero after publishing a completion result. */ - static int save_screenshot_worker(void *context); + static int save_screenshot_worker(ScreenshotTask *context); + /** + * @brief Convert the owned YUV snapshot into a BGR screenshot without retaining conversion buffers. + * @param frame Stable screenshot snapshot. + * @return Owned BGR surface, or nullptr when conversion fails. + */ + static SDL_Surface *convert_screenshot_frame(const LatestVideoFrame &frame); /** * @brief Hold FFmpeg and SDL state used by the video path. diff --git a/src/streaming/screenshot_status.cpp b/src/streaming/screenshot_status.cpp index 4801872..e53254c 100644 --- a/src/streaming/screenshot_status.cpp +++ b/src/streaming/screenshot_status.cpp @@ -50,8 +50,10 @@ namespace streaming { } std::string ScreenshotNotification::title() const { - return saving ? "Saving screenshot... " + std::to_string(percent) + "%" : saved ? "Screenshot saved" : - "Screenshot failed"; + if (saving) { + return "Saving screenshot... " + std::to_string(percent) + "%"; + } + return saved ? "Screenshot saved" : "Screenshot failed"; } std::string_view ScreenshotNotification::body() const { @@ -59,8 +61,7 @@ namespace streaming { return {}; } std::string_view text = detail; - const std::string_view prefix = saved ? "Saved " : "Screenshot failed: "; - if (text.substr(0, prefix.size()) == prefix) { + if (const std::string_view prefix = saved ? "Saved " : "Screenshot failed: "; text.substr(0, prefix.size()) == prefix) { text.remove_prefix(prefix.size()); } return text; diff --git a/src/streaming/screenshot_status.h b/src/streaming/screenshot_status.h index 9061b74..c538e47 100644 --- a/src/streaming/screenshot_status.h +++ b/src/streaming/screenshot_status.h @@ -76,7 +76,6 @@ namespace streaming { int percent = 0; ///< Displayed progress, with 100 reserved for successful completion. std::string detail; ///< Completed path or an actionable failure description. - private: std::uint32_t completedAt_ = 0; ///< Tick at which completion was observed by the UI. }; } // namespace streaming diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index b376ff3..72e4bd1 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -1513,7 +1513,7 @@ namespace streaming { logging::error("stream", initializationError); return false; } - resources.controller.configure(settings.screenshotDirectory, settings.keyboardDockTop, app::screenshot_source(host, app)); + resources.controller.configure(settings.controls.screenshotDirectory, settings.controls.keyboardDockTop, app::screenshot_source(host, app)); ScopedThreadPriority streamThreadPriority(SDL_THREAD_PRIORITY_HIGH); startup::log_memory_statistics(); @@ -1640,8 +1640,8 @@ namespace streaming { logging::info("stream", line); } startup::log_memory_statistics(); - if (settings.keyboardDockTop != resources.controller.keyboard_docked_top()) { - settings.keyboardDockTop = resources.controller.keyboard_docked_top(); + if (settings.controls.keyboardDockTop != resources.controller.keyboard_docked_top()) { + settings.controls.keyboardDockTop = resources.controller.keyboard_docked_top(); settings.dirty = true; } show_stream_end_statistics(host, app, startContext, finalStatistics, finalMessage, &resources); diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 53b9cf4..5566592 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -193,16 +193,17 @@ namespace { namespace streaming { void StreamControls::clear_panel() { - text_.clear(); - textBytes_ = 0; - iconTextures_.clear(); - panelRenderer_.reset(); - panel_.reset(); - panelContent_.reset(); - panelDamage_.clear(); + panel_.text.clear(); + panel_.textBytes = 0; + panel_.iconTextures.clear(); + panel_.renderer.reset(); + panel_.surface.reset(); + panel_.content.reset(); + panel_.damage.clear(); } - void StreamControls::label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading, bool background, bool fitSingleLine, SDL_Color foreground) { + void StreamControls::label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, const StreamLabelStyle &style) { + const auto &[leading, background, fitSingleLine, foreground] = style; if (background) { SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); SDL_RenderFillRect(renderer, &bounds); @@ -210,32 +211,33 @@ namespace streaming { if (text.empty()) { return; } - const Uint32 color = foreground.r | (static_cast(foreground.g) << 8) | (static_cast(foreground.b) << 16) | (static_cast(foreground.a) << 24); + const Uint32 color = static_cast(foreground.r) | (static_cast(foreground.g) << 8) | (static_cast(foreground.b) << 16) | (static_cast(foreground.a) << 24); const auto key = std::make_tuple(font, text, fitSingleLine ? 0 : std::max(1, bounds.w - 4), color); - auto found = text_.find(key); - if (found == text_.end()) { - std::unique_ptr surface(fitSingleLine ? TTF_RenderUTF8_Blended(font, text.c_str(), foreground) : TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), foreground, std::max(1, bounds.w - 4)), SDL_FreeSurface); + auto found = panel_.text.find(key); + if (found == panel_.text.end()) { + std::unique_ptr surface(fitSingleLine ? TTF_RenderUTF8_Blended(font, text.c_str(), foreground) : TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), foreground, std::max(1, bounds.w - 4)), &SDL_FreeSurface); if (!surface) { return; } const auto bytes = static_cast(surface->w) * surface->h * 4; - if (textBytes_ + bytes > 1024U * 1024U) { + if (panel_.textBytes + bytes > 1024U * 1024U) { SDL_RenderFlush(renderer); - text_.clear(); - textBytes_ = 0; + panel_.text.clear(); + panel_.textBytes = 0; } std::shared_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); if (!texture) { return; } - found = text_.emplace(key, CachedText {surface->w, surface->h, std::move(texture)}).first; - textBytes_ += bytes; + found = panel_.text.try_emplace(key, CachedText {surface->w, surface->h, std::move(texture)}).first; + panel_.textBytes += bytes; } const auto &raster = found->second; // nxdk SDL 2.0.9 requires source cropping rather than standalone clip commands. auto layout = layout_overlay_label(raster.width, raster.height, {bounds.x, bounds.y, bounds.w, bounds.h}, fitSingleLine); if (leading) { - layout.source.x = layout.source.y = 0; + layout.source.x = 0; + layout.source.y = 0; layout.destination.x = bounds.x; layout.destination.y = bounds.y; } @@ -246,32 +248,31 @@ namespace streaming { void StreamControls::navigate_controller(const input::ControllerState &sample, Uint32 now) { if (menu_.page != input::StreamMenuPage::keyboard && menu_.page != input::StreamMenuPage::logs) { - controllerNavigation_.reset(); + navigation_.controller.reset(); return; } - int x = (sample.buttonFlags & RIGHT_FLAG) != 0 ? 1 : (sample.buttonFlags & LEFT_FLAG) != 0 ? -1 : - 0; - int y = (sample.buttonFlags & DOWN_FLAG) != 0 ? 1 : (sample.buttonFlags & UP_FLAG) != 0 ? -1 : - 0; - if (navigationNeutralRequired_) { + int x = (sample.buttonFlags & RIGHT_FLAG) != 0; + int y = (sample.buttonFlags & DOWN_FLAG) != 0; + if (x == 0 && (sample.buttonFlags & LEFT_FLAG) != 0) { + x = -1; + } + if (y == 0 && (sample.buttonFlags & UP_FLAG) != 0) { + y = -1; + } + if (navigation_.neutralRequired) { if (x == 0 && y == 0 && std::max(std::abs(static_cast(sample.leftStickX)), std::abs(static_cast(sample.leftStickY))) < input::STICK_NAVIGATION_RELEASE_THRESHOLD) { - navigationNeutralRequired_ = false; + navigation_.neutralRequired = false; } return; } - const auto command = x > 0 ? input::UiCommand::move_right : x < 0 ? input::UiCommand::move_left : - y > 0 ? input::UiCommand::move_down : - y < 0 ? input::UiCommand::move_up : - input::UiCommand::none; - const auto step = x == 0 && y == 0 ? controllerNavigation_.update_stick(sample.leftStickX, sample.leftStickY, now) : controllerNavigation_.update(command, now); + const auto command = input::direction_command(x, y); + const auto step = x == 0 && y == 0 ? navigation_.controller.update_stick(sample.leftStickX, sample.leftStickY, now) : navigation_.controller.update(command, now); const auto key = menu_navigation_key(step); - if (key != SDLK_UNKNOWN && !controllerNavigationApplied_) { - controllerNavigationApplied_ = true; - menu_.navigate(key == SDLK_RIGHT ? 1 : key == SDLK_LEFT ? -1 : - 0, - key == SDLK_DOWN ? 1 : key == SDLK_UP ? -1 : - 0); - dirty_ = true; + if (key != SDLK_UNKNOWN && !navigation_.applied) { + navigation_.applied = true; + const auto [dx, dy] = input::navigation_step(menu_navigation_command(key)); + menu_.navigate(dx, dy); + panel_.dirty = true; } } @@ -330,7 +331,7 @@ namespace streaming { void StreamControls::close() { clear_panel(); - icons_.clear(); + panel_.icons.clear(); for (auto &device : devices_) { if (device.handle != nullptr) { SDL_GameControllerClose(device.handle); @@ -369,7 +370,7 @@ namespace streaming { } if (menu_.owner == instance) { menu_.owner = -1; - armed_ = true; + navigation_.armed = true; } for (auto &device : devices_) { if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == instance) { @@ -433,16 +434,16 @@ namespace streaming { return; } menu_.open(device); - keyboardNavigation_.reset(); - controllerNavigation_.reset(); - navigationNeutralRequired_ = false; - keyboardPageTriggerDown_ = false; - keyboardDockTriggerDown_ = false; - inputDevice_ = device < 0 ? input::UiInputDevice::keyboard : input::UiInputDevice::gamepad; + navigation_.keyboard.reset(); + navigation_.controller.reset(); + navigation_.neutralRequired = false; + navigation_.pageTriggerDown = false; + navigation_.dockTriggerDown = false; + navigation_.inputDevice = device < 0 ? input::UiInputDevice::keyboard : input::UiInputDevice::gamepad; router_.set_captured(true); - armed_ = false; - dirty_ = true; - message_.clear(); + navigation_.armed = false; + panel_.dirty = true; + panel_.message.clear(); logging::info("input", "Opened stream menu from " + (device < 0 ? std::string("keyboard") : "gamepad " + std::to_string(device))); } @@ -465,102 +466,74 @@ namespace streaming { router_.release_source(-3); router_.set_captured(true); menu_.key_sent(); - dirty_ = true; + panel_.dirty = true; } - bool StreamControls::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) { - if (menu_.page == input::StreamMenuPage::quit_confirmation) { - if (key == SDLK_RETURN) { - return menu_.confirm_disconnect(input::StreamDisconnectAction::disconnect); - } - if (key == SDLK_q) { - return menu_.confirm_disconnect(input::StreamDisconnectAction::quit_app); - } - if (key == SDLK_ESCAPE) { - dirty_ = true; - return menu_.confirm_disconnect(input::StreamDisconnectAction::cancel); - } - return false; + bool StreamControls::confirm_exit_key(SDL_Keycode key) { + if (key == SDLK_RETURN) { + return menu_.confirm_disconnect(input::StreamDisconnectAction::disconnect); + } + if (key == SDLK_q) { + return menu_.confirm_disconnect(input::StreamDisconnectAction::quit_app); } - dirty_ = true; if (key == SDLK_ESCAPE) { - if (menu_.page == input::StreamMenuPage::radial) { - menu_.close(); - } else { - menu_.page = input::StreamMenuPage::radial; - menu_.modifiers = 0; - menu_.shiftOnce = false; - } - return false; + panel_.dirty = true; + return menu_.confirm_disconnect(input::StreamDisconnectAction::cancel); } - if (menu_.page == input::StreamMenuPage::keyboard) { - if (key == SDLK_TAB) { - menu_.toggle_keyboard_page(); - return false; - } - if (key == SDLK_LSHIFT || key == SDLK_RSHIFT) { - menu_.toggle_shift(); - return false; - } - if (key == SDLK_CAPSLOCK) { - menu_.caps = !menu_.caps; - return false; - } - if (key == SDLK_F2) { - keyboardTop_ = !keyboardTop_; - return false; - } - if (key == SDLK_BACKSPACE || key == SDLK_SPACE) { - if (active) { - tap_key(key == SDLK_SPACE ? 0x20 : 0x08); - } - return false; - } + return false; + } + + bool StreamControls::handle_keyboard_shortcut(SDL_Keycode key, bool active) { + if (key == SDLK_TAB) { + menu_.toggle_keyboard_page(); + return true; } - if (key != SDLK_RETURN && key != SDLK_SPACE) { - if (key == SDLK_TAB && menu_.page == input::StreamMenuPage::radial) { - menu_.selected = menu_.selected == input::StreamAction::none ? input::StreamAction::keyboard : static_cast((static_cast(menu_.selected) + 1) % 8); - } else { - menu_.navigate(key == SDLK_RIGHT ? 1 : key == SDLK_LEFT ? -1 : - 0, - key == SDLK_DOWN ? 1 : key == SDLK_UP ? -1 : - 0); - } - return false; + if (key == SDLK_LSHIFT || key == SDLK_RSHIFT) { + menu_.toggle_shift(); + return true; } - if (menu_.page == input::StreamMenuPage::keyboard) { + if (key == SDLK_CAPSLOCK) { + menu_.caps = !menu_.caps; + return true; + } + if (key == SDLK_F2) { + keyboardTop_ = !keyboardTop_; + return true; + } + if (key == SDLK_BACKSPACE || key == SDLK_SPACE) { if (active) { - tap_key(menu_.keyboard_key()); + tap_key(key == SDLK_SPACE ? 0x20 : 0x08); } - return false; - } - if (menu_.page != input::StreamMenuPage::radial) { - return false; + return true; } + return false; + } + + void StreamControls::activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats) { switch (menu_.selected) { case input::StreamAction::quit: menu_.page = input::StreamMenuPage::quit_confirmation; break; case input::StreamAction::keyboard: menu_.page = input::StreamMenuPage::keyboard; - controllerNavigation_.reset(); - navigationNeutralRequired_ = true; + navigation_.controller.reset(); + navigation_.neutralRequired = true; break; case input::StreamAction::logs: menu_.page = input::StreamMenuPage::logs; - controllerNavigation_.reset(); - navigationNeutralRequired_ = true; - logs_.clear(); + navigation_.controller.reset(); + navigation_.neutralRequired = true; + panel_.logs.clear(); for (const auto &entry : logging::snapshot()) { - logs_.push_back(logging::format_entry(entry)); + panel_.logs.push_back(logging::format_entry(entry)); } - if (logs_.empty()) { - logs_ = logging::load_log_file().lines; + if (panel_.logs.empty()) { + panel_.logs = logging::load_log_file().lines; } break; case input::StreamAction::mouse: if (mouseDevice_ < 0 && menu_.owner < 0) { - message_ = "Open the menu with a controller to enable mouse mode."; + panel_.message = "Open the menu with a controller to enable mouse mode."; break; } mouseDevice_ = mouseDevice_ < 0 ? menu_.owner : -1; @@ -578,8 +551,8 @@ namespace streaming { } else if (backend.screenshot_progress() < 0) { screenshotNotification_.finish(false, "Could not start capture. Check the stream log for details.", SDL_GetTicks()); } - message_ = screenshotNotification_.title(); - logging::info("stream", message_); + panel_.message = screenshotNotification_.title(); + logging::info("stream", panel_.message); menu_.close(); break; } @@ -588,7 +561,7 @@ namespace streaming { { const int slot = active ? router_.connect(menu_.owner) : -1; if (slot < 0) { - message_ = "A connected controller and active stream are required."; + panel_.message = "A connected controller and active stream are required."; break; } release_pulse(); @@ -599,341 +572,466 @@ namespace streaming { packet.controller = latestHostController_[slot]; packet.controller.buttonFlags |= pulseButtons_; send_packet(packet); - message_ = menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; + panel_.message = menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; menu_.close(); break; } case input::StreamAction::none: break; } + } + + bool StreamControls::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) { + if (menu_.page == input::StreamMenuPage::quit_confirmation) { + return confirm_exit_key(key); + } + panel_.dirty = true; + if (key == SDLK_ESCAPE) { + if (menu_.page == input::StreamMenuPage::radial) { + menu_.close(); + } else { + menu_.page = input::StreamMenuPage::radial; + menu_.modifiers = 0; + menu_.shiftOnce = false; + } + return false; + } + if (menu_.page == input::StreamMenuPage::keyboard && handle_keyboard_shortcut(key, active)) { + return false; + } + if (key != SDLK_RETURN && key != SDLK_SPACE) { + if (key == SDLK_TAB && menu_.page == input::StreamMenuPage::radial) { + menu_.selected = menu_.selected == input::StreamAction::none ? input::StreamAction::keyboard : static_cast((static_cast(menu_.selected) + 1) % 8); + } else { + const auto [dx, dy] = input::navigation_step(menu_navigation_command(key)); + menu_.navigate(dx, dy); + } + return false; + } + if (menu_.page == input::StreamMenuPage::keyboard) { + if (active) { + tap_key(menu_.keyboard_key()); + } + return false; + } + if (menu_.page != input::StreamMenuPage::radial) { + return false; + } + activate_stream_action(backend, active, stats); return false; } - bool StreamControls::pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled) { - bool screenshotChanged = false; - if (const auto result = backend.take_screenshot_result(); !result.empty()) { - logging::info("screenshots", result); - screenshotNotification_.finish(result.rfind("Saved ", 0) == 0, result, SDL_GetTicks()); - screenshotChanged = true; - } else { - screenshotChanged = screenshotNotification_.update(backend.screenshot_progress()); + /** + * @brief Translate the four directional controller buttons to local navigation keys. + * @param button SDL controller button. + * @return Corresponding arrow key, or unknown for other buttons. + */ + SDL_Keycode controller_navigation_key(Uint8 button) { + switch (button) { + case SDL_CONTROLLER_BUTTON_DPAD_UP: + return SDLK_UP; + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + return SDLK_DOWN; + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + return SDLK_LEFT; + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + return SDLK_RIGHT; + default: + return SDLK_UNKNOWN; } - // Sampling and transport enqueueing share one owner so capture cannot race a stale gameplay packet. - controllerNavigationApplied_ = false; - SDL_PumpEvents(); - bool stop = false; - const bool wasVisible = visible(); - SDL_Event event {}; - const Uint32 eventStart = SDL_GetTicks(); - for (int count = 0; count < 128 && (count == 0 || SDL_GetTicks() - eventStart < 4) && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0; ++count) { - const bool triggerAxis = event.type == SDL_CONTROLLERAXISMOTION && (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT); - const bool gamepadActivity = event.type == SDL_CONTROLLERBUTTONDOWN || (event.type == SDL_CONTROLLERAXISMOTION && (triggerAxis ? event.caxis.value >= 16000 : std::abs(static_cast(event.caxis.value)) >= input::StreamMenu::WHEEL_DEADZONE)); - if (visible() && gamepadActivity && menu_.owner < 0) { - menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; + } + + void StreamControls::note_gamepad_activity(const SDL_Event &event) { + const bool triggerAxis = event.type == SDL_CONTROLLERAXISMOTION && (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT); + const bool gamepadActivity = event.type == SDL_CONTROLLERBUTTONDOWN || (event.type == SDL_CONTROLLERAXISMOTION && (triggerAxis ? event.caxis.value >= 16000 : std::abs(static_cast(event.caxis.value)) >= input::StreamMenu::WHEEL_DEADZONE)); + if (visible() && gamepadActivity && menu_.owner < 0) { + menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; + } + if (visible() && gamepadActivity && navigation_.inputDevice != input::UiInputDevice::gamepad) { + navigation_.inputDevice = input::UiInputDevice::gamepad; + navigation_.keyboard.reset(); + panel_.dirty = true; + } + if (event.type == SDL_CONTROLLERAXISMOTION) { + keyboard_trigger(event.caxis); + } + } + + void StreamControls::keyboard_trigger(const SDL_ControllerAxisEvent &axis) { + if (!visible() || menu_.page != input::StreamMenuPage::keyboard || axis.which != menu_.owner) { + return; + } + const bool pressed = axis.value >= 16000; + if (axis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT) { + if (pressed && !navigation_.pageTriggerDown && navigation_.armed) { + menu_.toggle_keyboard_page(); + panel_.dirty = true; } - if (visible() && gamepadActivity && inputDevice_ != input::UiInputDevice::gamepad) { - inputDevice_ = input::UiInputDevice::gamepad; - keyboardNavigation_.reset(); - dirty_ = true; + navigation_.pageTriggerDown = pressed; + } else if (axis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { + if (pressed && !navigation_.dockTriggerDown && navigation_.armed) { + keyboardTop_ = !keyboardTop_; + panel_.dirty = true; } - if (visible() && menu_.page == input::StreamMenuPage::keyboard && event.type == SDL_CONTROLLERAXISMOTION && event.caxis.which == menu_.owner) { - const bool pressed = event.caxis.value >= 16000; - if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT) { - if (pressed && !keyboardPageTriggerDown_ && armed_) { - menu_.toggle_keyboard_page(); - dirty_ = true; - } - keyboardPageTriggerDown_ = pressed; - } else if (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { - if (pressed && !keyboardDockTriggerDown_ && armed_) { - keyboardTop_ = !keyboardTop_; - dirty_ = true; - } - keyboardDockTriggerDown_ = pressed; + navigation_.dockTriggerDown = pressed; + } + } + + void StreamControls::joystick_button(const SDL_JoyButtonEvent &button) { + for (auto &device : devices_) { + if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != button.which) { + continue; + } + const int flag = joystick_chord_flag(device.handle, button.button); + if (flag != 0) { + const bool down = device.chord.update_button(flag, button.state == SDL_PRESSED); + if (down && !device.chordDown) { + open_menu(button.which); } + device.chordDown = down; } - if (event.type == SDL_CONTROLLERDEVICEADDED) { - open_if_needed(event.cdevice.which); - } else if (event.type == SDL_CONTROLLERDEVICEREMOVED) { - close_if_removed(event.cdevice.which); - } else if (event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP) { - for (auto &device : devices_) { - if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != event.jbutton.which) { - continue; - } - const int flag = joystick_chord_flag(device.handle, event.jbutton.button); - if (flag != 0) { - const bool down = device.chord.update_button(flag, event.jbutton.state == SDL_PRESSED); - if (down && !device.chordDown) { - open_menu(event.jbutton.which); - } - device.chordDown = down; - } + } + } + + void StreamControls::route_controller_button(const SDL_ControllerButtonEvent &button, bool active) { + for (auto &device : devices_) { + if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != button.which) { + continue; + } + const int flag = controller_flag(button.button); + const bool down = device.chord.update_button(flag, button.state == SDL_PRESSED); + if (down && !device.chordDown) { + open_menu(button.which); + } + device.chordDown = down; + if (button.state == SDL_PRESSED) { + device.sample.buttonFlags |= flag; + } else { + device.sample.buttonFlags &= ~flag; + } + if (active) { + router_.connect(button.which); + router_.controller(button.which, device.sample); + } + } + } + + void StreamControls::navigate_owner_controller() { + for (const auto &device : devices_) { + if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == menu_.owner) { + navigate_controller(device.sample, SDL_GetTicks()); + } + } + } + + bool StreamControls::controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + route_controller_button(button, active); + if (visible() && navigation_.armed && button.state == SDL_RELEASED && button.which == menu_.owner && menu_.page != input::StreamMenuPage::radial) { + navigate_owner_controller(); + } + if (visible() && navigation_.armed && button.state == SDL_PRESSED) { + if (menu_.owner < 0) { + menu_.owner = button.which; + } + if (button.which != menu_.owner) { + return false; + } + return activate_controller_button(button.button, backend, active, statisticsEnabled); + } + return false; + } + + bool StreamControls::activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + bool stop = false; + switch (button) { + case SDL_CONTROLLER_BUTTON_A: + stop = menu_key(SDLK_RETURN, backend, active, statisticsEnabled) || stop; + break; + case SDL_CONTROLLER_BUTTON_B: + stop = menu_key(SDLK_ESCAPE, backend, active, statisticsEnabled) || stop; + break; + case SDL_CONTROLLER_BUTTON_DPAD_UP: + case SDL_CONTROLLER_BUTTON_DPAD_DOWN: + case SDL_CONTROLLER_BUTTON_DPAD_LEFT: + case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: + if (menu_.page == input::StreamMenuPage::radial) { + menu_key(controller_navigation_key(button), backend, active, statisticsEnabled); + } else { + navigate_owner_controller(); } - } else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) { - const auto &button = event.cbutton; - for (auto &device : devices_) { - if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != button.which) { - continue; - } - const int flag = controller_flag(button.button); - const bool down = device.chord.update_button(flag, button.state == SDL_PRESSED); - if (down && !device.chordDown) { - open_menu(button.which); - } - device.chordDown = down; - if (button.state == SDL_PRESSED) { - device.sample.buttonFlags |= flag; - } else { - device.sample.buttonFlags &= ~flag; - } - if (active) { - router_.connect(button.which); - router_.controller(button.which, device.sample); - } + break; + case SDL_CONTROLLER_BUTTON_X: + if (menu_.page == input::StreamMenuPage::quit_confirmation) { + stop = menu_key(SDLK_q, backend, active, statisticsEnabled) || stop; + } else if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x08); } - if (visible() && armed_ && button.state == SDL_RELEASED && button.which == menu_.owner && menu_.page != input::StreamMenuPage::radial) { - for (const auto &device : devices_) { - if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == button.which) { - navigate_controller(device.sample, SDL_GetTicks()); - } - } + break; + case SDL_CONTROLLER_BUTTON_Y: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x20); } - if (visible() && armed_ && button.state == SDL_PRESSED) { - if (menu_.owner < 0) { - menu_.owner = button.which; - } - if (button.which != menu_.owner) { - continue; - } - switch (button.button) { - case SDL_CONTROLLER_BUTTON_A: - stop = menu_key(SDLK_RETURN, backend, active, statisticsEnabled) || stop; - break; - case SDL_CONTROLLER_BUTTON_B: - stop = menu_key(SDLK_ESCAPE, backend, active, statisticsEnabled) || stop; - break; - case SDL_CONTROLLER_BUTTON_DPAD_UP: - case SDL_CONTROLLER_BUTTON_DPAD_DOWN: - case SDL_CONTROLLER_BUTTON_DPAD_LEFT: - case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: - if (menu_.page == input::StreamMenuPage::radial) { - menu_key(button.button == SDL_CONTROLLER_BUTTON_DPAD_UP ? SDLK_UP : button.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ? SDLK_DOWN : - button.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ? SDLK_LEFT : - SDLK_RIGHT, - backend, - active, - statisticsEnabled); - } else { - for (const auto &device : devices_) { - if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == button.which) { - navigate_controller(device.sample, SDL_GetTicks()); - } - } - } - break; - case SDL_CONTROLLER_BUTTON_X: - if (menu_.page == input::StreamMenuPage::quit_confirmation) { - stop = menu_key(SDLK_q, backend, active, statisticsEnabled) || stop; - } else if (active && menu_.page == input::StreamMenuPage::keyboard) { - tap_key(0x08); - } - break; - case SDL_CONTROLLER_BUTTON_Y: - if (active && menu_.page == input::StreamMenuPage::keyboard) { - tap_key(0x20); - } - break; - case SDL_CONTROLLER_BUTTON_LEFTSTICK: - if (menu_.page == input::StreamMenuPage::keyboard) { - menu_.caps = !menu_.caps; - dirty_ = true; - } - break; - case SDL_CONTROLLER_BUTTON_RIGHTSTICK: - if (menu_.page == input::StreamMenuPage::keyboard) { - menu_.toggle_shift(); - dirty_ = true; - } - break; - case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: - case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: - if (active && menu_.page == input::StreamMenuPage::keyboard) { - tap_key(button.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ? 0x125 : 0x127); - } - break; - case SDL_CONTROLLER_BUTTON_START: - if (active && menu_.page == input::StreamMenuPage::keyboard) { - tap_key(0x0D); - } - break; - default: - break; - } + break; + case SDL_CONTROLLER_BUTTON_LEFTSTICK: + if (menu_.page == input::StreamMenuPage::keyboard) { + menu_.caps = !menu_.caps; + panel_.dirty = true; } - } else if (event.type == SDL_CONTROLLERAXISMOTION && visible() && armed_ && event.caxis.which == menu_.owner) { - for (auto &device : devices_) { - if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != event.caxis.which) { - continue; - } - // Preserve the event's deflection even when a queued center event follows it. - auto &sample = device.sample; - switch (event.caxis.axis) { - case SDL_CONTROLLER_AXIS_LEFTX: - sample.leftStickX = event.caxis.value; - break; - case SDL_CONTROLLER_AXIS_LEFTY: - sample.leftStickY = input::invert_stick_axis(event.caxis.value); - break; - case SDL_CONTROLLER_AXIS_RIGHTX: - sample.rightStickX = event.caxis.value; - break; - case SDL_CONTROLLER_AXIS_RIGHTY: - sample.rightStickY = input::invert_stick_axis(event.caxis.value); - break; - default: - break; - } - if (event.caxis.axis <= SDL_CONTROLLER_AXIS_RIGHTY) { - if (menu_.page == input::StreamMenuPage::radial) { - dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; - } else { - navigate_controller(sample, SDL_GetTicks()); - } - } + break; + case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + if (menu_.page == input::StreamMenuPage::keyboard) { + menu_.toggle_shift(); + panel_.dirty = true; + } + break; + case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: + case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ? 0x125 : 0x127); + } + break; + case SDL_CONTROLLER_BUTTON_START: + if (active && menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x0D); + } + break; + default: + break; + } + return stop; + } + + void StreamControls::controller_axis(const SDL_ControllerAxisEvent &axis) { + if (!visible() || !navigation_.armed || axis.which != menu_.owner) { + return; + } + for (auto &device : devices_) { + if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != axis.which) { + continue; + } + // Preserve the event's deflection even when a queued center event follows it. + auto &sample = device.sample; + switch (axis.axis) { + case SDL_CONTROLLER_AXIS_LEFTX: + sample.leftStickX = axis.value; + break; + case SDL_CONTROLLER_AXIS_LEFTY: + sample.leftStickY = input::invert_stick_axis(axis.value); + break; + case SDL_CONTROLLER_AXIS_RIGHTX: + sample.rightStickX = axis.value; + break; + case SDL_CONTROLLER_AXIS_RIGHTY: + sample.rightStickY = input::invert_stick_axis(axis.value); + break; + default: + break; + } + if (axis.axis <= SDL_CONTROLLER_AXIS_RIGHTY) { + if (menu_.page == input::StreamMenuPage::radial) { + panel_.dirty = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || panel_.dirty; + } else { + navigate_controller(sample, SDL_GetTicks()); } - } else if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { - const bool down = event.type == SDL_KEYDOWN; - const auto &key = event.key; - const bool shortcut = key.keysym.scancode == SDL_SCANCODE_Q && (key.keysym.mod & KMOD_CTRL) != 0 && (key.keysym.mod & KMOD_ALT) != 0 && (key.keysym.mod & KMOD_SHIFT) != 0; - if (down && key.repeat == 0 && shortcut) { - open_menu(-1); - armed_ = true; - } else if (visible()) { - if (!down) { - keyboardNavigation_.release(key.keysym.scancode); + } + } + } + + /** + * @brief Translate physical keyboard modifier flags to the host protocol. + * @param flags SDL modifier bits. + * @return Moonlight Shift, Ctrl, Alt and Meta modifier mask. + */ + int host_keyboard_modifiers(Uint16 flags) { + return ((flags & KMOD_SHIFT) != 0 ? MODIFIER_SHIFT : 0) | ((flags & KMOD_CTRL) != 0 ? MODIFIER_CTRL : 0) | ((flags & KMOD_ALT) != 0 ? MODIFIER_ALT : 0) | ((flags & KMOD_GUI) != 0 ? MODIFIER_META : 0); + } + + bool StreamControls::keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + bool stop = false; + const bool down = key.type == SDL_KEYDOWN; + const bool shortcut = key.keysym.scancode == SDL_SCANCODE_Q && (key.keysym.mod & KMOD_CTRL) != 0 && (key.keysym.mod & KMOD_ALT) != 0 && (key.keysym.mod & KMOD_SHIFT) != 0; + if (down && key.repeat == 0 && shortcut) { + open_menu(-1); + navigation_.armed = true; + return false; + } + if (visible()) { + if (!down) { + navigation_.keyboard.release(key.keysym.scancode); + } + if (!down || key.repeat != 0) { + return false; + } + { + if (navigation_.inputDevice != input::UiInputDevice::keyboard) { + navigation_.inputDevice = input::UiInputDevice::keyboard; + panel_.dirty = true; + } + const auto command = menu_navigation_command(key.keysym.sym); + if (command != input::UiCommand::none) { + if (navigation_.keyboard.press(key.keysym.scancode, command, SDL_GetTicks()) != input::UiCommand::none) { + menu_key(key.keysym.sym, backend, active, statisticsEnabled); } - if (down && key.repeat == 0) { - if (inputDevice_ != input::UiInputDevice::keyboard) { - inputDevice_ = input::UiInputDevice::keyboard; - dirty_ = true; - } - const auto command = menu_navigation_command(key.keysym.sym); - if (command != input::UiCommand::none) { - if (keyboardNavigation_.press(key.keysym.scancode, command, SDL_GetTicks()) != input::UiCommand::none) { - menu_key(key.keysym.sym, backend, active, statisticsEnabled); - } - } else { - if (key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_SPACE || key.keysym.sym == SDLK_ESCAPE) { - keyboardNavigation_.reset(); - } - stop = menu_key(key.keysym.sym, backend, active, statisticsEnabled) || stop; - } + } else { + if (key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_SPACE || key.keysym.sym == SDLK_ESCAPE) { + navigation_.keyboard.reset(); } - } else if (active && key.repeat == 0) { - const int mods = ((key.keysym.mod & KMOD_SHIFT) != 0 ? MODIFIER_SHIFT : 0) | ((key.keysym.mod & KMOD_CTRL) != 0 ? MODIFIER_CTRL : 0) | ((key.keysym.mod & KMOD_ALT) != 0 ? MODIFIER_ALT : 0) | ((key.keysym.mod & KMOD_GUI) != 0 ? MODIFIER_META : 0); - router_.key(0, input::keyboard_virtual_key(key.keysym.scancode), down, mods); + stop = menu_key(key.keysym.sym, backend, active, statisticsEnabled) || stop; } - } else if (active && event.type == SDL_MOUSEMOTION) { - router_.mouse_move(event.motion.xrel, event.motion.yrel); - } else if (active && (event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)) { - const int button = event.button.button; - const int hostButton = button == SDL_BUTTON_LEFT ? BUTTON_LEFT : button == SDL_BUTTON_RIGHT ? BUTTON_RIGHT : - button == SDL_BUTTON_MIDDLE ? BUTTON_MIDDLE : - button == SDL_BUTTON_X1 ? BUTTON_X1 : - button == SDL_BUTTON_X2 ? BUTTON_X2 : - 0; - router_.mouse_button(1, hostButton, event.type == SDL_MOUSEBUTTONDOWN); - } else if (active && event.type == SDL_MOUSEWHEEL) { - const int direction = event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1; - router_.scroll(event.wheel.x * direction, event.wheel.y * direction); - } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { - focused_ = false; - keyboardNavigation_.reset(); - router_.set_captured(true); - } else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { - focused_ = true; - router_.set_captured(visible()); - } else if (event.type == SDL_QUIT) { - stop = true; } + return stop; } - if (stop) { - router_.set_captured(true); - return true; + if (active && key.repeat == 0) { + const int mods = host_keyboard_modifiers(key.keysym.mod); + router_.key(0, input::keyboard_virtual_key(key.keysym.scancode), down, mods); } - poll(); - const Uint32 now = SDL_GetTicks(); - screenshotChanged = screenshotNotification_.tick(now) || screenshotChanged; - if (menu_.page == input::StreamMenuPage::logs && now - logRefreshTick_ >= 1000) { - logRefreshTick_ = now; + return stop; + } + + /** + * @brief Translate supported physical mouse buttons to host protocol buttons. + * @param button SDL mouse button. + * @return Host button, or zero for an unsupported button. + */ + int host_mouse_button(Uint8 button) { + switch (button) { + case SDL_BUTTON_LEFT: + return BUTTON_LEFT; + case SDL_BUTTON_RIGHT: + return BUTTON_RIGHT; + case SDL_BUTTON_MIDDLE: + return BUTTON_MIDDLE; + case SDL_BUTTON_X1: + return BUTTON_X1; + case SDL_BUTTON_X2: + return BUTTON_X2; + default: + return 0; + } + } + + bool StreamControls::handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + note_gamepad_activity(event); + switch (event.type) { + case SDL_CONTROLLERDEVICEADDED: + open_if_needed(event.cdevice.which); + break; + case SDL_CONTROLLERDEVICEREMOVED: + close_if_removed(event.cdevice.which); + break; + case SDL_JOYBUTTONDOWN: + case SDL_JOYBUTTONUP: + joystick_button(event.jbutton); + break; + case SDL_CONTROLLERBUTTONDOWN: + case SDL_CONTROLLERBUTTONUP: + return controller_button(event.cbutton, backend, active, statisticsEnabled); + case SDL_CONTROLLERAXISMOTION: + controller_axis(event.caxis); + break; + case SDL_KEYDOWN: + case SDL_KEYUP: + return keyboard_event(event.key, backend, active, statisticsEnabled); + case SDL_MOUSEMOTION: + if (active) { + router_.mouse_move(event.motion.xrel, event.motion.yrel); + } + break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + if (active) { + router_.mouse_button(1, host_mouse_button(event.button.button), event.type == SDL_MOUSEBUTTONDOWN); + } + break; + case SDL_MOUSEWHEEL: + if (active) { + const int direction = event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1; + router_.scroll(event.wheel.x * direction, event.wheel.y * direction); + } + break; + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { + focused_ = false; + navigation_.keyboard.reset(); + router_.set_captured(true); + } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { + focused_ = true; + router_.set_captured(visible()); + } + break; + case SDL_QUIT: + return true; + default: + break; + } + return false; + } + + void StreamControls::refresh_logs(Uint32 now) { + if (menu_.page == input::StreamMenuPage::logs && now - panel_.logRefreshTick >= 1000) { + panel_.logRefreshTick = now; std::vector lines; for (const auto &entry : logging::snapshot()) { lines.push_back(logging::format_entry(entry)); } - if (!lines.empty() && lines != logs_) { + if (!lines.empty() && lines != panel_.logs) { // Preserve the same entries when reading older output; offset zero follows live output. if (menu_.logOffset > 0) { - menu_.logOffset += std::max(0, static_cast(lines.size()) - static_cast(logs_.size())); + menu_.logOffset += std::max(0, static_cast(lines.size()) - static_cast(panel_.logs.size())); } - logs_ = std::move(lines); - dirty_ = true; + panel_.logs = std::move(lines); + panel_.dirty = true; } } - if (visible() && inputDevice_ == input::UiInputDevice::keyboard) { - const auto repeatedKey = menu_navigation_key(keyboardNavigation_.poll(now)); - if (repeatedKey != SDLK_UNKNOWN) { - menu_key(repeatedKey, backend, active, statisticsEnabled); - } + } + + void StreamControls::update_device(Device &device, bool active, Uint32 now) { + if (device.handle == nullptr) { + return; } - for (auto &device : devices_) { - if (device.handle == nullptr) { - continue; - } - const auto instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)); - const bool present = SDL_GameControllerGetAttached(device.handle) != SDL_FALSE; - const bool down = device.chord.update(device.sample.buttonFlags, present); - if (down && !device.chordDown) { - open_menu(instance); - } - device.chordDown = down; - if (active) { - if (present) { - router_.connect(instance); - router_.controller(instance, device.sample); - } else { - router_.disconnect(instance); - } - } - if (!visible() || instance != menu_.owner || inputDevice_ != input::UiInputDevice::gamepad) { - continue; - } - const auto &sample = device.sample; - if (sample.buttonFlags == 0 && sample.leftTrigger == 0 && sample.rightTrigger == 0) { - armed_ = true; - } - if (!armed_) { - continue; - } - if (menu_.page == input::StreamMenuPage::radial) { - // Poll on every pump as well as consuming events: Xbox input can change - // between queued axis events, and a selected item must not freeze polling. - dirty_ = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || dirty_; + const auto instance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)); + const bool present = SDL_GameControllerGetAttached(device.handle) != SDL_FALSE; + const bool down = device.chord.update(device.sample.buttonFlags, present); + if (down && !device.chordDown) { + open_menu(instance); + } + device.chordDown = down; + if (active) { + if (present) { + router_.connect(instance); + router_.controller(instance, device.sample); } else { - navigate_controller(sample, now); + router_.disconnect(instance); } } - if (pulseSlot_ >= 0 && (static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { - release_pulse(); + if (!visible() || instance != menu_.owner || navigation_.inputDevice != input::UiInputDevice::gamepad) { + return; } - if (!visible() && (wasVisible || dirty_)) { - keyboardNavigation_.reset(); + const auto &sample = device.sample; + if (sample.buttonFlags == 0 && sample.leftTrigger == 0 && sample.rightTrigger == 0) { + navigation_.armed = true; + } + if (!navigation_.armed) { + return; + } + if (menu_.page == input::StreamMenuPage::radial) { + // Poll on every pump as well as consuming events: Xbox input can change + // between queued axis events, and a selected item must not freeze polling. + panel_.dirty = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || panel_.dirty; + } else { + navigate_controller(sample, now); + } + } + + void StreamControls::refresh_overlay(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool wasVisible, bool screenshotChanged, bool stats) { + if (!visible() && (wasVisible || panel_.dirty)) { + navigation_.keyboard.reset(); router_.set_captured(!focused_); if (!screenshotNotification_.visible) { backend.set_statistics_bitmap(0, 0, {}); } clear_panel(); - dirty_ = false; + panel_.dirty = false; screenshotChanged = true; } backend.local_overlay_size(width, height); @@ -945,18 +1043,60 @@ namespace streaming { clear_panel(); } } - if (visible() && screenshotNotification_.visible && message_ != screenshotNotification_.title()) { - message_ = screenshotNotification_.title(); - dirty_ = true; + if (visible() && screenshotNotification_.visible && panel_.message != screenshotNotification_.title()) { + panel_.message = screenshotNotification_.title(); + panel_.dirty = true; + } + if (visible() && (width != panel_.width || height != panel_.height)) { + panel_.dirty = true; + panel_.width = width; + panel_.height = height; + } + if (visible() && panel_.dirty) { + render_menu(backend, font, width, height, stats); + } + } + + bool StreamControls::pump(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool active, bool &statisticsEnabled) { + bool screenshotChanged = false; + if (const auto result = backend.take_screenshot_result(); !result.empty()) { + logging::info("screenshots", result); + screenshotNotification_.finish(result.rfind("Saved ", 0) == 0, result, SDL_GetTicks()); + screenshotChanged = true; + } else { + screenshotChanged = screenshotNotification_.update(backend.screenshot_progress()); + } + // Sampling and transport enqueueing share one owner so capture cannot race a stale gameplay packet. + navigation_.applied = false; + SDL_PumpEvents(); + bool stop = false; + const bool wasVisible = visible(); + SDL_Event event; // SDL_PeepEvents fills the event before each use. + const Uint32 eventStart = SDL_GetTicks(); + for (int count = 0; count < 128 && (count == 0 || SDL_GetTicks() - eventStart < 4) && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0; ++count) { + stop = handle_event(event, backend, active, statisticsEnabled) || stop; + } + if (stop) { + router_.set_captured(true); + return true; } - if (visible() && (width != menuWidth_ || height != menuHeight_)) { - dirty_ = true; - menuWidth_ = width; - menuHeight_ = height; + poll(); + const Uint32 now = SDL_GetTicks(); + screenshotChanged = screenshotNotification_.tick(now) || screenshotChanged; + refresh_logs(now); + if (visible() && navigation_.inputDevice == input::UiInputDevice::keyboard) { + const auto repeatedKey = menu_navigation_key(navigation_.keyboard.poll(now)); + if (repeatedKey != SDLK_UNKNOWN) { + menu_key(repeatedKey, backend, active, statisticsEnabled); + } } - if (visible() && dirty_) { - render_menu(backend, font, width, height, statisticsEnabled); + for (auto &device : devices_) { + update_device(device, active, now); } + if (pulseSlot_ >= 0 && (static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { + release_pulse(); + } + refresh_overlay(backend, font, width, height, wasVisible, screenshotChanged, statisticsEnabled); if (active) { router_.tick(now - lastTick_); } @@ -988,24 +1128,24 @@ namespace streaming { if (panelWidth <= 0 || panelHeight <= 0) { return; } - if (!panel_ || panel_->w != panelWidth || panel_->h != panelHeight) { + if (!panel_.surface || panel_.surface->w != panelWidth || panel_.surface->h != panelHeight) { clear_panel(); - panel_.reset(SDL_CreateRGBSurfaceWithFormat(0, panelWidth, panelHeight, 32, SDL_PIXELFORMAT_ARGB8888)); - if (panel_) { - panelRenderer_.reset(SDL_CreateSoftwareRenderer(panel_.get())); + panel_.surface.reset(SDL_CreateRGBSurfaceWithFormat(0, panelWidth, panelHeight, 32, SDL_PIXELFORMAT_ARGB8888)); + if (panel_.surface) { + panel_.renderer.reset(SDL_CreateSoftwareRenderer(panel_.surface.get())); } } - if (!panelRenderer_) { + if (!panel_.renderer) { return; } - auto *renderer = panelRenderer_.get(); + auto *renderer = panel_.renderer.get(); SDL_SetRenderDrawColor(renderer, 22, 31, 46, 170); SDL_RenderClear(renderer); - label(renderer, font, title, {12, 8, panelWidth - 24, line}, false, false, true); + label(renderer, font, title, {12, 8, panelWidth - 24, line}, {false, false, true}); if (!detail.empty()) { // SDL_ttf's legacy wrapper cannot reliably split an uninterrupted Xbox // path. Fit one complete line instead of clipping or overlapping it. - label(renderer, font, detail, {12, line + 12, panelWidth - 24, line}, false, false, true); + label(renderer, font, detail, {12, line + 12, panelWidth - 24, line}, {false, false, true}); } SDL_Rect bar {12, panelHeight - 12, panelWidth - 24, 4}; SDL_SetRenderDrawColor(renderer, 50, 65, 86, 255); @@ -1014,7 +1154,7 @@ namespace streaming { SDL_SetRenderDrawColor(renderer, screenshotNotification_.saving || screenshotNotification_.saved ? 130 : 255, screenshotNotification_.saving || screenshotNotification_.saved ? 180 : 110, screenshotNotification_.saving || screenshotNotification_.saved ? 255 : 110, 255); SDL_RenderFillRect(renderer, &bar); SDL_RenderPresent(renderer); - backend.set_color_overlay_surface(panelWidth, panelHeight, static_cast(panel_->pixels), static_cast(panel_->pitch) * panelHeight, panel_->pitch, VideoOverlayPlacement::bottom_center); + backend.set_color_overlay_surface(panelWidth, panelHeight, static_cast(panel_.surface->pixels), static_cast(panel_.surface->pitch) * panelHeight, panel_.surface->pitch, VideoOverlayPlacement::bottom_center); } bool StreamControls::render_icon(SDL_Renderer *renderer, const char *asset, SDL_Rect bounds, const SDL_Rect *clip) { @@ -1023,26 +1163,26 @@ namespace streaming { return false; } const auto key = std::make_pair(std::string(asset), size); - auto found = icons_.find(key); - if (found == icons_.end()) { + auto found = panel_.icons.find(key); + if (found == panel_.icons.end()) { const std::string path = ui::build_asset_path(std::string("icons/") + asset); std::shared_ptr raster(ui::load_svg_surface(path, size, size), SDL_FreeSurface); if (!raster) { logging::warn("ui", "Unable to load stream icon " + path + ": " + SDL_GetError()); } - found = icons_.emplace(key, std::move(raster)).first; + found = panel_.icons.try_emplace(key, std::move(raster)).first; } if (!found->second) { return false; } const auto &surface = found->second; - auto texture = iconTextures_.find(key); - if (texture == iconTextures_.end()) { + auto texture = panel_.iconTextures.find(key); + if (texture == panel_.iconTextures.end()) { auto loaded = std::shared_ptr(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); if (!loaded) { return false; } - texture = iconTextures_.emplace(key, std::move(loaded)).first; + texture = panel_.iconTextures.try_emplace(key, std::move(loaded)).first; } SDL_Rect destination {bounds.x + (bounds.w - surface->w) / 2, bounds.y + (bounds.h - surface->h) / 2, surface->w, surface->h}; if (clip != nullptr) { @@ -1060,7 +1200,7 @@ namespace streaming { if (bounds.w <= 0 || bounds.h <= 0) { return; } - const bool keyboard = inputDevice_ == input::UiInputDevice::keyboard; + const bool keyboard = navigation_.inputDevice == input::UiInputDevice::keyboard; const int size = std::min(bounds.h, compact ? std::max(12, TTF_FontLineSkip(font) * 3 / 4) : TTF_FontLineSkip(font) + 12); int textWidth = 0; int textHeight = 0; @@ -1072,11 +1212,84 @@ namespace streaming { if (keyboard) { SDL_SetRenderDrawColor(renderer, 85, 85, 85, 255); SDL_RenderDrawRect(renderer, &icon); - label(renderer, font, key, {icon.x + 2, icon.y + 2, icon.w - 4, icon.h - 4}, false, false, true); + label(renderer, font, key, {icon.x + 2, icon.y + 2, icon.w - 4, icon.h - 4}, {false, false, true}); } else { render_icon(renderer, asset, icon); } - label(renderer, font, text, {left + capWidth + 4, bounds.y, std::max(1, groupWidth - capWidth - 4), bounds.h}, false, false, true); + label(renderer, font, text, {left + capWidth + 4, bounds.y, std::max(1, groupWidth - capWidth - 4), bounds.h}, {false, false, true}); + } + + void StreamControls::render_keyboard_key(SDL_Renderer *renderer, TTF_Font *font, int i, SDL_Rect key) { + const auto &cell = menu_.keyboard_cells()[i]; + const bool keyboardInput = navigation_.inputDevice == input::UiInputDevice::keyboard; + const bool modifier = cell.key < input::StreamMenu::KEYBOARD_PAGE_KEY && input::StreamMenu::KEYS[cell.key].code < 0; + bool latched = modifier && (menu_.modifiers & -input::StreamMenu::KEYS[cell.key].code) != 0; + if (cell.key == input::StreamMenu::KEYBOARD_CAPS_KEY) { + latched = menu_.caps; + } else if (cell.key == 40) { + latched = menu_.shiftOnce; + } + int gray = cell.width > 2 ? 42 : 30; + if (latched) { + gray = 65; + } + if (i == menu_.keyIndex) { + gray = 54; + } + SDL_SetRenderDrawColor(renderer, gray, gray, gray, 255); + SDL_RenderFillRect(renderer, &key); + std::string text = modifier ? input::StreamMenu::KEYS[cell.key].label : menu_.key_label(i); + + const char *shortcutIcon = nullptr; + const char *shortcutKey = ""; + switch (cell.key) { + case input::StreamMenu::KEYBOARD_PAGE_KEY: + shortcutIcon = "button-lt.svg"; + shortcutKey = "Tab"; + break; + case 47: + shortcutIcon = "button-x.svg"; + text = "Bksp"; + break; + case 57: + shortcutIcon = "button-lb.svg"; + text = "<"; + break; + case 59: + shortcutIcon = "button-rb.svg"; + text = ">"; + break; + case input::StreamMenu::KEYBOARD_CAPS_KEY: + shortcutIcon = "button-left-stick.svg"; + shortcutKey = "Caps"; + break; + case 44: + shortcutIcon = "button-y.svg"; + break; + case 46: + shortcutIcon = "button-start.svg"; + text = "Enter"; + break; + default: + break; + } + if (cell.width > 2 && shortcutIcon != nullptr && (!keyboardInput || *shortcutKey != '\0')) { + if (cell.height > 1) { + label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h / 2}, {false, false, true}); + render_hint(renderer, font, shortcutIcon, shortcutKey, "", {key.x + 2, key.y + key.h / 2, key.w - 4, key.h / 2 - 2}, cell.key != input::StreamMenu::KEYBOARD_CAPS_KEY && cell.key != 46); + } else { + render_hint(renderer, font, shortcutIcon, shortcutKey, text.c_str(), {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, true); + } + } else { + label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, {false, false, true}); + } + if (i == menu_.keyIndex) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_Rect focus {key.x - 1, key.y - 1, key.w + 2, key.h + 2}; + SDL_RenderDrawRect(renderer, &focus); + focus = {key.x - 2, key.y - 2, key.w + 4, key.h + 4}; + SDL_RenderDrawRect(renderer, &focus); + } } void StreamControls::render_keyboard(SDL_Renderer *renderer, TTF_Font *font, int width, int height, int rowHeight, bool focusOnly) { @@ -1084,8 +1297,12 @@ namespace streaming { constexpr int padding = 8; const int header = TTF_FontLineSkip(font) + 4; const auto &focusedCell = cells[menu_.keyIndex]; - std::string description = menu_.keyboardLayout == 0 ? "Keyboard / ABC" : menu_.keyboardLayout == 1 ? "Keyboard / ?123" : - "Keyboard / PC keys"; + std::string description = "Keyboard / PC keys"; + if (menu_.keyboardLayout == 0) { + description = "Keyboard / ABC"; + } else if (menu_.keyboardLayout == 1) { + description = "Keyboard / ?123"; + } if (focusedCell.key >= 106 && focusedCell.key < static_cast(input::StreamMenu::KEYS.size())) { static const std::array shortcuts {"Copy: Ctrl+C", "Paste: Ctrl+V", "Cut: Ctrl+X", "Undo: Ctrl+Z", "Redo: Ctrl+Y", "Select all: Ctrl+A", "Switch app: Alt+Tab", "Close app: Alt+F4", "Ctrl+Alt+Delete", "Desktop: Win+D", "Explorer: Win+E", "Run: Win+R"}; description = shortcuts[focusedCell.key - 106]; @@ -1095,13 +1312,13 @@ namespace streaming { // touch the same gap pixels. No SDL renderer clipping commands are needed. if (focusOnly) { SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); - for (const int index : {panelSelection_, menu_.keyIndex}) { + for (const int index : {panel_.selection, menu_.keyIndex}) { const auto &cell = cells[index]; const int left = padding + cell.column * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; const int right = padding + (cell.column + cell.width) * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; SDL_Rect area {left - 1, padding + header + cell.row * rowHeight - 1, right - left + 1, cell.height * rowHeight + 1}; SDL_RenderFillRect(renderer, &area); - panelDamage_.push_back({area.x, area.y, area.w, area.h}); + panel_.damage.push_back({area.x, area.y, area.w, area.h}); } } if (!focusOnly || menu_.keyboardLayout == 2) { @@ -1109,79 +1326,19 @@ namespace streaming { if (focusOnly) { SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); SDL_RenderFillRect(renderer, &area); - panelDamage_.push_back({area.x, area.y, area.w, area.h}); + panel_.damage.push_back({area.x, area.y, area.w, area.h}); } - label(renderer, font, description, {padding, 2, width - padding * 2, header}, false, false, true); + label(renderer, font, description, {padding, 2, width - padding * 2, header}, {false, false, true}); } - const bool keyboardInput = inputDevice_ == input::UiInputDevice::keyboard; for (int i = 0; i < static_cast(cells.size()); ++i) { - if (focusOnly && i != panelSelection_ && i != menu_.keyIndex) { + if (focusOnly && i != panel_.selection && i != menu_.keyIndex) { continue; } const auto &cell = cells[i]; const int left = padding + cell.column * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; const int right = padding + (cell.column + cell.width) * gridWidth / input::StreamMenu::KEYBOARD_COLUMNS; SDL_Rect key {left + 1, padding + header + cell.row * rowHeight + 1, right - left - 3, cell.height * rowHeight - 3}; - const bool modifier = cell.key < input::StreamMenu::KEYBOARD_PAGE_KEY && input::StreamMenu::KEYS[cell.key].code < 0; - const bool latched = cell.key == input::StreamMenu::KEYBOARD_CAPS_KEY ? menu_.caps : cell.key == 40 ? menu_.shiftOnce : - modifier && (menu_.modifiers & -input::StreamMenu::KEYS[cell.key].code) != 0; - const int gray = i == menu_.keyIndex ? 54 : latched ? 65 : - cell.width > 2 ? 42 : - 30; - SDL_SetRenderDrawColor(renderer, gray, gray, gray, 255); - SDL_RenderFillRect(renderer, &key); - std::string text = modifier ? input::StreamMenu::KEYS[cell.key].label : menu_.key_label(i); - - const char *shortcutIcon = nullptr; - const char *shortcutKey = ""; - switch (cell.key) { - case input::StreamMenu::KEYBOARD_PAGE_KEY: - shortcutIcon = "button-lt.svg"; - shortcutKey = "Tab"; - break; - case 47: - shortcutIcon = "button-x.svg"; - text = "Bksp"; - break; - case 57: - shortcutIcon = "button-lb.svg"; - text = "<"; - break; - case 59: - shortcutIcon = "button-rb.svg"; - text = ">"; - break; - case input::StreamMenu::KEYBOARD_CAPS_KEY: - shortcutIcon = "button-left-stick.svg"; - shortcutKey = "Caps"; - break; - case 44: - shortcutIcon = "button-y.svg"; - break; - case 46: - shortcutIcon = "button-start.svg"; - text = "Enter"; - break; - default: - break; - } - if (cell.width > 2 && shortcutIcon != nullptr && (!keyboardInput || *shortcutKey != '\0')) { - if (cell.height > 1) { - label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h / 2}, false, false, true); - render_hint(renderer, font, shortcutIcon, shortcutKey, "", {key.x + 2, key.y + key.h / 2, key.w - 4, key.h / 2 - 2}, cell.key != input::StreamMenu::KEYBOARD_CAPS_KEY && cell.key != 46); - } else { - render_hint(renderer, font, shortcutIcon, shortcutKey, text.c_str(), {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, true); - } - } else { - label(renderer, font, text, {key.x + 2, key.y + 2, key.w - 4, key.h - 4}, false, false, true); - } - if (i == menu_.keyIndex) { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_Rect focus {key.x - 1, key.y - 1, key.w + 2, key.h + 2}; - SDL_RenderDrawRect(renderer, &focus); - focus = {key.x - 2, key.y - 2, key.w + 4, key.h + 4}; - SDL_RenderDrawRect(renderer, &focus); - } + render_keyboard_key(renderer, font, i, key); } if (focusOnly) { return; @@ -1197,6 +1354,189 @@ namespace streaming { } } + /** + * @brief Bound one wheel sector for incremental overlay publication. + * @param wheel Wheel geometry. + * @param sector Clockwise sector from up. + * @return Conservative sector rectangle. + */ + VideoRectangle wheel_sector_bounds(const MenuWheel &wheel, int sector) { + const int cx = wheel.centerX; + const int cy = wheel.centerY; + const int radius = wheel.radius; + switch (sector) { + case 0: + return {cx - radius, cy - radius, radius * 2 + 1, radius + 1}; + case 1: + return {cx, cy - radius, radius + 1, radius * 2 + 1}; + case 2: + return {cx - radius, cy, radius * 2 + 1, radius + 1}; + default: + return {cx - radius, cy - radius, radius + 1, radius * 2 + 1}; + } + } + + void StreamControls::render_wheel_sector(SDL_Renderer *renderer, TTF_Font *font, const WheelPaint &paint, int sector) { + const auto &panel = panel_.surface; + const auto &[cx, cy, radius, selected] = paint.geometry; + const int width = paint.target.width; + const int height = paint.target.height; + const int line = paint.line; + const bool focusOnly = paint.focusOnly; + const bool stats = paint.stats; + constexpr std::array dx {0, 1, 0, -1}; + constexpr std::array dy {-1, 0, 1, 0}; + const auto action = static_cast(paint.index * 4 + sector); + const int labelWidth = std::min(line * 6, width / 5); + const int x = std::clamp(cx + dx[sector] * radius * 2 / 3 - labelWidth / 2, 0, width - labelWidth); + const int iconSize = std::max(18, line); + const int groupY = cy + dy[sector] * radius * 2 / 3 - (iconSize + line * 2) / 2; + if (focusOnly) { + // Restore the label's complete background, including any pixels in + // the central hole, before blending its antialiased text again. + const SDL_Rect group {x, groupY, labelWidth, iconSize + 2 + line * 2}; + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 150); + SDL_RenderFillRect(renderer, &group); + SDL_RenderFlush(renderer); + rasterize_menu_wheel({static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height}, {cx, cy, radius, selected}, sector); + const VideoRectangle clip {group.x, group.y, group.w, group.h}; + rasterize_menu_wheel({static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height}, {cx, cy, radius, selected}, -1, &clip); + render_icon(renderer, paint.index == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}, &group); + const VideoRectangle area = wheel_sector_bounds(paint.geometry, sector); + const int left = std::min(area.x, clip.x); + const int top = std::min(area.y, clip.y); + panel_.damage.push_back({left, top, std::max(area.x + area.width, clip.x + clip.width) - left, std::max(area.y + area.height, clip.y + clip.height) - top}); + } + constexpr std::array icons {"keyboard.svg", "mouse.svg", "scroll-text.svg", "log-out.svg", "button-guide.svg", "button-share.svg", "camera.svg", "chart-no-axes-combined.svg"}; + render_icon(renderer, icons[paint.index * 4 + sector], {x + (labelWidth - iconSize) / 2, groupY, iconSize, iconSize}); + label(renderer, font, input::stream_action_label(action, mouseDevice_ >= 0, stats), {x, groupY + iconSize + 2, labelWidth, line * 2}, {false, false}); + } + + void StreamControls::render_wheels(SDL_Renderer *renderer, TTF_Font *font, int width, int height, bool stats, bool focusOnly) { + const auto &panel = panel_.surface; + const int line = TTF_FontLineSkip(font) + 8; + const int cy = height / 2; + const int radius = std::min(width / 5, (height - line * 4) / 2); + for (int wheel = 0; wheel < 2; ++wheel) { + const int cx = width * (wheel == 0 ? 1 : 3) / 4; + const int selected = static_cast(menu_.selected) / 4 == wheel ? static_cast(menu_.selected) % 4 : -1; + const WheelPaint paint {{static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height}, {cx, cy, radius, selected}, wheel, line, focusOnly, stats}; + if (!focusOnly) { + SDL_RenderFlush(renderer); + rasterize_menu_wheel(paint.target, paint.geometry); + render_icon(renderer, wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}); + } + for (int sector = 0; sector < 4; ++sector) { + if (focusOnly && wheel * 4 + sector != panel_.selection && wheel * 4 + sector != static_cast(menu_.selected)) { + continue; + } + render_wheel_sector(renderer, font, paint, sector); + } + } + const bool keyboardHints = navigation_.inputDevice == input::UiInputDevice::keyboard; + const std::array assets {"button-left-stick.svg", "button-right-stick.svg", "button-a.svg", "button-b.svg"}; + const std::array keys {"Arrows", "Tab", "Enter", "Esc"}; + const std::array actions {keyboardHints ? "Select" : "Left wheel", keyboardHints ? "Next" : "Right wheel", "Choose", "Resume"}; + for (int i = 0; !focusOnly && i < 4; ++i) { + render_hint(renderer, font, assets[i], keys[i], actions[i], {i * width / 4 + 4, height - line * 2, width / 4 - 8, line + 4}); + } + } + + /** + * @brief Choose a readable severity color for a formatted log entry. + * @param text Formatted log line. + * @return Error, warning or normal text color. + */ + SDL_Color stream_log_color(std::string_view text) { + if (text.find("[ERROR]") != std::string_view::npos || text.find("[FATAL]") != std::string_view::npos) { + return {255, 125, 125, 255}; + } + if (text.find("[WARN") != std::string_view::npos) { + return {255, 211, 115, 255}; + } + return {224, 232, 241, 255}; + } + + void StreamControls::render_logs(SDL_Renderer *renderer, TTF_Font *font, int width, int height) { + const int line = TTF_FontLineSkip(font) + 8; + const SDL_Rect card {width / 8, line / 2, width * 7 / 8 - line / 2, height - line}; + ui::draw_dialog_surface(renderer, card); + label(renderer, font, "Stream logs", {card.x + 20, card.y + 28, card.w - 40, line}, {false, false}); + const int row = line * 2; + const int count = std::max(1, (card.h - line * 4) / row); + menu_.logOffset = std::min(menu_.logOffset, std::max(0, static_cast(panel_.logs.size()) - count)); + const int end = static_cast(panel_.logs.size()) - menu_.logOffset; + const int start = std::max(0, end - count); + for (int i = start; i < end; ++i) { + const auto &text = panel_.logs[i]; + const SDL_Color color = stream_log_color(text); + label(renderer, font, text, {card.x + 16, card.y + line * 2 + (i - start) * row, card.w - 42, row}, {true, false, false, color}); + } + const int trackHeight = count * row; + const int thumbHeight = std::max(8, trackHeight * count / std::max(count, static_cast(panel_.logs.size()))); + SDL_Rect thumb {card.x + card.w - 12, card.y + line * 2 + (trackHeight - thumbHeight) * start / std::max(1, static_cast(panel_.logs.size()) - count), 3, thumbHeight}; + SDL_SetRenderDrawColor(renderer, 130, 180, 255, 255); + SDL_RenderFillRect(renderer, &thumb); + if (panel_.logs.empty()) { + label(renderer, font, "No retained log entries.", {card.x + 16, card.y + line * 2, card.w - 32, line}, {false, false}); + } + render_hint(renderer, font, "button-left-stick.svg", "Arrows", menu_.logOffset == 0 ? "Live / scroll" : "Scroll", {card.x + 8, card.y + card.h - line - 12, card.w / 2 - 16, line}); + render_hint(renderer, font, "button-b.svg", "Esc", "Back", {card.x + card.w / 2, card.y + card.h - line - 12, card.w / 2 - 16, line}); + } + + void StreamControls::render_exit_confirmation(SDL_Renderer *renderer, TTF_Font *font, int width, int height) { + const int line = TTF_FontLineSkip(font) + 8; + const int cardWidth = std::min(width - 32, std::max(560, width * 4 / 5)); + const int cardHeight = line * 7; + const SDL_Rect card {(width - cardWidth) / 2, (height - cardHeight) / 2, cardWidth, cardHeight}; + ui::draw_dialog_surface(renderer, card); + label(renderer, font, "Exit stream?", {card.x + 20, card.y + 28, card.w - 40, line}, {false, false}); + label(renderer, font, "Disconnect keeps the app running. Quit app closes it on the PC; unsaved progress may be lost.", {card.x + 20, card.y + 28 + line, card.w - 40, line * 2}, {false, false}); + constexpr std::array buttons {"button-b.svg", "button-a.svg", "button-x.svg"}; + constexpr std::array keys {"Esc", "Enter", "Q"}; + constexpr std::array labels {"Cancel", "Disconnect", "Quit app"}; + for (int i = 0; i < 3; ++i) { + SDL_Rect button {card.x + 16 + i * (card.w - 24) / 3, card.y + card.h - line * 2, (card.w - 48) / 3, line + 12}; + ui::draw_dialog_round_rect(renderer, button, {28, 40, 61, 255}, 6); + render_hint(renderer, font, buttons[i], keys[i], labels[i], {button.x + 4, button.y, button.w - 8, button.h}); + } + } + + bool StreamControls::prepare_panel(int width, int height) { + if (!panel_.surface || panel_.surface->w != width || panel_.surface->h != height) { + clear_panel(); + panel_.surface.reset(SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_ARGB8888)); + if (panel_.surface) { + panel_.renderer.reset(SDL_CreateSoftwareRenderer(panel_.surface.get())); + } + } + if (!panel_.renderer) { + return false; + } + if (panel_.text.size() > 256) { + panel_.text.clear(); + panel_.textBytes = 0; + } + return true; + } + + void StreamControls::publish_panel(FfmpegStreamBackend &backend, int width, int height, bool keyboard, bool focusOnly) const { + SDL_RenderPresent(panel_.renderer.get()); + if (focusOnly) { + for (const auto &area : panel_.damage) { + const auto offset = static_cast(area.y) * panel_.surface->pitch + area.x * 4; + const auto pixels = extract_overlay_bitmap(static_cast(panel_.surface->pixels) + offset, static_cast(panel_.surface->pitch) * height - offset, panel_.surface->pitch, area.width, area.height, keyboard); + backend.update_color_overlay(area, pixels); + } + } else { + VideoOverlayPlacement placement = VideoOverlayPlacement::full_screen; + if (keyboard) { + placement = keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom; + } + backend.set_color_overlay_surface(width, height, static_cast(panel_.surface->pixels), static_cast(panel_.surface->pitch) * height, panel_.surface->pitch, placement, keyboard); + } + } + void StreamControls::render_menu(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool stats) { if (font == nullptr) { return; @@ -1206,32 +1546,20 @@ namespace streaming { const int safeBottom = std::max(8, height / 40); width = keyboard ? width * 9 / 10 : width; height = keyboard ? std::min(height - VideoOverlay::MARGIN * 2, rowHeight * input::StreamMenu::KEYBOARD_ROWS + 28 + TTF_FontLineSkip(font) + (TTF_FontLineSkip(font) + 8) + safeBottom) : height; - if (width <= 0 || height <= 0 || font == nullptr) { + if (width <= 0 || height <= 0) { return; } - if (!panel_ || panel_->w != width || panel_->h != height) { - clear_panel(); - panel_.reset(SDL_CreateRGBSurfaceWithFormat(0, width, height, 32, SDL_PIXELFORMAT_ARGB8888)); - if (panel_) { - panelRenderer_.reset(SDL_CreateSoftwareRenderer(panel_.get())); - } - } - if (!panelRenderer_) { + if (!prepare_panel(width, height)) { return; } - if (text_.size() > 256) { - text_.clear(); - textBytes_ = 0; - } - auto &panel = panel_; - auto &renderer = panelRenderer_; - const PanelContent content {font, inputDevice_, menu_.page, menu_.keyboardLayout, menu_.modifiers, menu_.caps, menu_.shiftOnce, stats, mouseDevice_, keyboardTop_, message_}; - const bool focusOnly = panelContent_ && *panelContent_ == content && (keyboard || menu_.page == input::StreamMenuPage::radial); - if (focusOnly && panelSelection_ == (keyboard ? menu_.keyIndex : static_cast(menu_.selected))) { - dirty_ = false; + const auto &renderer = panel_.renderer; + const PanelContent content {font, navigation_.inputDevice, menu_.page, menu_.keyboardLayout, menu_.modifiers, menu_.caps, menu_.shiftOnce, stats, mouseDevice_, keyboardTop_, panel_.message}; + const bool focusOnly = panel_.content && *panel_.content == content && (keyboard || menu_.page == input::StreamMenuPage::radial); + if (focusOnly && panel_.selection == (keyboard ? menu_.keyIndex : static_cast(menu_.selected))) { + panel_.dirty = false; return; } - panelDamage_.clear(); + panel_.damage.clear(); const Uint8 background = keyboard ? 16 : 0; if (!focusOnly) { SDL_SetRenderDrawColor(renderer.get(), background, background, background, keyboard ? 255 : 150); @@ -1239,118 +1567,23 @@ namespace streaming { } const int line = TTF_FontLineSkip(font) + 8; if (!focusOnly && menu_.page == input::StreamMenuPage::radial) { - label(renderer.get(), font, "Stream controls", {8, 4, width - 16, line}, false, false); + label(renderer.get(), font, "Stream controls", {8, 4, width - 16, line}, {false, false}); } if (keyboard) { render_keyboard(renderer.get(), font, width, height, rowHeight, focusOnly); } else if (menu_.page == input::StreamMenuPage::radial) { - const int cy = height / 2; - const int radius = std::min(width / 5, (height - line * 4) / 2); - for (int wheel = 0; wheel < 2; ++wheel) { - const int cx = width * (wheel == 0 ? 1 : 3) / 4; - const int selected = static_cast(menu_.selected) / 4 == wheel ? static_cast(menu_.selected) % 4 : -1; - if (!focusOnly) { - SDL_RenderFlush(renderer.get()); - rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, selected); - render_icon(renderer.get(), wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}); - } - constexpr std::array dx {0, 1, 0, -1}; - constexpr std::array dy {-1, 0, 1, 0}; - for (int sector = 0; sector < 4; ++sector) { - if (focusOnly && wheel * 4 + sector != panelSelection_ && wheel * 4 + sector != static_cast(menu_.selected)) { - continue; - } - const auto action = static_cast(wheel * 4 + sector); - const int labelWidth = std::min(line * 6, width / 5); - const int x = std::clamp(cx + dx[sector] * radius * 2 / 3 - labelWidth / 2, 0, width - labelWidth); - const int iconSize = std::max(18, line); - const int groupY = cy + dy[sector] * radius * 2 / 3 - (iconSize + line * 2) / 2; - if (focusOnly) { - // Restore the label's complete background, including any pixels in - // the central hole, before blending its antialiased text again. - const SDL_Rect group {x, groupY, labelWidth, iconSize + 2 + line * 2}; - SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 150); - SDL_RenderFillRect(renderer.get(), &group); - SDL_RenderFlush(renderer.get()); - rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, selected, sector); - const VideoRectangle clip {group.x, group.y, group.w, group.h}; - rasterize_menu_wheel(static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, width, height, cx, cy, radius, selected, -1, &clip); - render_icon(renderer.get(), wheel == 0 ? "button-left-stick.svg" : "button-right-stick.svg", {cx - line, cy - line, line * 2, line * 2}, &group); - const VideoRectangle area = sector == 0 ? VideoRectangle {cx - radius, cy - radius, radius * 2 + 1, radius + 1} : sector == 1 ? VideoRectangle {cx, cy - radius, radius + 1, radius * 2 + 1} : - sector == 2 ? VideoRectangle {cx - radius, cy, radius * 2 + 1, radius + 1} : - VideoRectangle {cx - radius, cy - radius, radius + 1, radius * 2 + 1}; - const int left = std::min(area.x, clip.x); - const int top = std::min(area.y, clip.y); - panelDamage_.push_back({left, top, std::max(area.x + area.width, clip.x + clip.width) - left, std::max(area.y + area.height, clip.y + clip.height) - top}); - } - constexpr std::array icons {"keyboard.svg", "mouse.svg", "scroll-text.svg", "log-out.svg", "button-guide.svg", "button-share.svg", "camera.svg", "chart-no-axes-combined.svg"}; - render_icon(renderer.get(), icons[wheel * 4 + sector], {x + (labelWidth - iconSize) / 2, groupY, iconSize, iconSize}); - label(renderer.get(), font, input::stream_action_label(action, mouseDevice_ >= 0, stats), {x, groupY + iconSize + 2, labelWidth, line * 2}, false, false); - } - } - const bool keyboardHints = inputDevice_ == input::UiInputDevice::keyboard; - const std::array assets {"button-left-stick.svg", "button-right-stick.svg", "button-a.svg", "button-b.svg"}; - const std::array keys {"Arrows", "Tab", "Enter", "Esc"}; - const std::array actions {keyboardHints ? "Select" : "Left wheel", keyboardHints ? "Next" : "Right wheel", "Choose", "Resume"}; - for (int i = 0; !focusOnly && i < 4; ++i) { - render_hint(renderer.get(), font, assets[i], keys[i], actions[i], {i * width / 4 + 4, height - line * 2, width / 4 - 8, line + 4}); - } + render_wheels(renderer.get(), font, width, height, stats, focusOnly); } else if (menu_.page == input::StreamMenuPage::logs) { - const SDL_Rect card {width / 8, line / 2, width * 7 / 8 - line / 2, height - line}; - ui::draw_dialog_surface(renderer.get(), card); - label(renderer.get(), font, "Stream logs", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); - const int row = line * 2; - const int count = std::max(1, (card.h - line * 4) / row); - menu_.logOffset = std::min(menu_.logOffset, std::max(0, static_cast(logs_.size()) - count)); - const int end = static_cast(logs_.size()) - menu_.logOffset; - const int start = std::max(0, end - count); - for (int i = start; i < end; ++i) { - const auto &text = logs_[i]; - const SDL_Color color = text.find("[ERROR]") != std::string::npos || text.find("[FATAL]") != std::string::npos ? SDL_Color {255, 125, 125, 255} : text.find("[WARN") != std::string::npos ? SDL_Color {255, 211, 115, 255} : - SDL_Color {224, 232, 241, 255}; - label(renderer.get(), font, text, {card.x + 16, card.y + line * 2 + (i - start) * row, card.w - 42, row}, true, false, false, color); - } - const int trackHeight = count * row; - const int thumbHeight = std::max(8, trackHeight * count / std::max(count, static_cast(logs_.size()))); - SDL_Rect thumb {card.x + card.w - 12, card.y + line * 2 + (trackHeight - thumbHeight) * start / std::max(1, static_cast(logs_.size()) - count), 3, thumbHeight}; - SDL_SetRenderDrawColor(renderer.get(), 130, 180, 255, 255); - SDL_RenderFillRect(renderer.get(), &thumb); - if (logs_.empty()) { - label(renderer.get(), font, "No retained log entries.", {card.x + 16, card.y + line * 2, card.w - 32, line}, false, false); - } - render_hint(renderer.get(), font, "button-left-stick.svg", "Arrows", menu_.logOffset == 0 ? "Live / scroll" : "Scroll", {card.x + 8, card.y + card.h - line - 12, card.w / 2 - 16, line}); - render_hint(renderer.get(), font, "button-b.svg", "Esc", "Back", {card.x + card.w / 2, card.y + card.h - line - 12, card.w / 2 - 16, line}); + render_logs(renderer.get(), font, width, height); } else { - const int cardWidth = std::min(width - 32, std::max(560, width * 4 / 5)); - const int cardHeight = line * 7; - const SDL_Rect card {(width - cardWidth) / 2, (height - cardHeight) / 2, cardWidth, cardHeight}; - ui::draw_dialog_surface(renderer.get(), card); - label(renderer.get(), font, "Exit stream?", {card.x + 20, card.y + 28, card.w - 40, line}, false, false); - label(renderer.get(), font, "Disconnect keeps the app running. Quit app closes it on the PC; unsaved progress may be lost.", {card.x + 20, card.y + 28 + line, card.w - 40, line * 2}, false, false); - constexpr std::array buttons {"button-b.svg", "button-a.svg", "button-x.svg"}; - constexpr std::array keys {"Esc", "Enter", "Q"}; - constexpr std::array labels {"Cancel", "Disconnect", "Quit app"}; - for (int i = 0; i < 3; ++i) { - SDL_Rect button {card.x + 16 + i * (card.w - 24) / 3, card.y + card.h - line * 2, (card.w - 48) / 3, line + 12}; - ui::draw_dialog_round_rect(renderer.get(), button, {28, 40, 61, 255}, 6); - render_hint(renderer.get(), font, buttons[i], keys[i], labels[i], {button.x + 4, button.y, button.w - 8, button.h}); - } + render_exit_confirmation(renderer.get(), font, width, height); } - if (!focusOnly && !message_.empty() && menu_.page == input::StreamMenuPage::radial) { - label(renderer.get(), font, message_, {4, line + 4, width - 8, line * 2}, false, false); - } - SDL_RenderPresent(renderer.get()); - if (focusOnly) { - for (const auto &area : panelDamage_) { - const auto offset = static_cast(area.y) * panel->pitch + area.x * 4; - const auto pixels = extract_overlay_bitmap(static_cast(panel->pixels) + offset, static_cast(panel->pitch) * height - offset, panel->pitch, area.width, area.height, keyboard); - backend.update_color_overlay(area, pixels); - } - } else { - backend.set_color_overlay_surface(width, height, static_cast(panel->pixels), static_cast(panel->pitch) * height, panel->pitch, keyboard ? (keyboardTop_ ? VideoOverlayPlacement::dock_top : VideoOverlayPlacement::dock_bottom) : VideoOverlayPlacement::full_screen, keyboard); + if (!focusOnly && !panel_.message.empty() && menu_.page == input::StreamMenuPage::radial) { + label(renderer.get(), font, panel_.message, {4, line + 4, width - 8, line * 2}, {false, false}); } - panelContent_ = content; - panelSelection_ = keyboard ? menu_.keyIndex : static_cast(menu_.selected); - dirty_ = false; + publish_panel(backend, width, height, keyboard, focusOnly); + panel_.content = content; + panel_.selection = keyboard ? menu_.keyIndex : static_cast(menu_.selected); + panel_.dirty = false; } } // namespace streaming diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index 435c9eb..db10a09 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -30,6 +30,16 @@ namespace streaming { class FfmpegStreamBackend; + /** + * @brief Text alignment, background and color for a cached stream-control label. + */ + struct StreamLabelStyle { + bool leading = false; ///< Align text to its top-left corner. + bool background = true; ///< Restore an opaque label background before drawing. + bool fitSingleLine = false; ///< Shrink the label to fit on one line. + SDL_Color foreground {255, 255, 255, 255}; ///< Original text color. + }; + /** * @brief Owns four gamepads, local stream controls and ordered host input. */ @@ -147,12 +157,9 @@ namespace streaming { * @param font Label font. * @param text UTF-8 label. * @param bounds Label bounds. - * @param leading Align console output to its top-left corner. - * @param background Paint an opaque label background. - * @param fitSingleLine Shrink a single-line key label to fit. - * @param foreground Text color. + * @param style Text alignment, background and color. */ - void label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, bool leading = false, bool background = true, bool fitSingleLine = false, SDL_Color foreground = {255, 255, 255, 255}); + void label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, const StreamLabelStyle &style = {}); /** * @brief Apply sampled or queued digital/stick navigation with shared repeat timing. * @param sample Controller state, with positive stick Y upward. @@ -170,6 +177,95 @@ namespace streaming { bool chordDown = false; ///< Chord edge latch. }; + /** + * @brief Track deliberate gamepad activity and keyboard trigger shortcuts. + * @param event Queued SDL event. + */ + void note_gamepad_activity(const SDL_Event &event); + /** + * @brief Apply trigger edges while the keyboard owns this controller. + * @param axis Queued controller axis transition. + */ + void keyboard_trigger(const SDL_ControllerAxisEvent &axis); + /** + * @brief Recognize the original Xbox raw-button shortcut. + * @param button Queued joystick button transition. + */ + void joystick_button(const SDL_JoyButtonEvent &button); + /** + * @brief Route controller transitions before local menu activation. + * @param button Queued controller button transition. + * @param active Transport readiness. + */ + void route_controller_button(const SDL_ControllerButtonEvent &button, bool active); + /** + * @brief Navigate from the current sample of the controller owning the local menu. + */ + void navigate_owner_controller(); + /** + * @brief Handle controller release gates and local button activation. + * @param button Queued controller button transition. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit. + */ + bool controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); + /** + * @brief Apply one local controller button action. + * @param button SDL controller button. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit. + */ + bool activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); + /** + * @brief Apply queued stick movement before the next sampled state. + * @param axis Queued controller axis transition. + */ + void controller_axis(const SDL_ControllerAxisEvent &axis); + /** + * @brief Route physical keyboard input to local controls or the host. + * @param key Queued keyboard transition. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit. + */ + bool keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); + /** + * @brief Dispatch a single SDL event without delaying the bounded input batch. + * @param event Queued SDL event. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit or window close. + */ + bool handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); + /** + * @brief Refresh live log entries while preserving a scrolled reading position. + * @param now Monotonic millisecond tick. + */ + void refresh_logs(Uint32 now); + /** + * @brief Route and navigate one sampled controller. + * @param device Owned controller slot. + * @param active Transport readiness. + * @param now Monotonic millisecond tick. + */ + void update_device(Device &device, bool active, Uint32 now); + /** + * @brief Publish local controls or capture feedback through the shared overlay. + * @param backend Streaming backend. + * @param font Overlay font. + * @param width Output width. + * @param height Output height. + * @param wasVisible Controls were visible before this input batch. + * @param screenshotChanged Capture feedback needs repainting. + * @param stats Session statistics setting. + */ + void refresh_overlay(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool wasVisible, bool screenshotChanged, bool stats); /** * @brief Open the wheels from gameplay or any local page and release host inputs. * @param device Controller instance ID, or -1 for keyboard control. @@ -192,6 +288,77 @@ namespace streaming { * @param height Available video height. */ void render_screenshot_notification(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height); + + /** + * @brief Rasterization context shared by the sectors of one menu wheel. + */ + struct WheelPaint { + OverlayBitmap target; ///< Writable panel pixels and geometry. + MenuWheel geometry; ///< Wheel center, radius and selection. + int index; ///< Left or right action wheel. + int line; ///< Text line height including spacing. + bool focusOnly; ///< Restore only changed sectors and labels. + bool stats; ///< Current session statistics setting. + }; + + /** + * @brief Draw one keyboard cell, its shortcut glyph and focus outline. + * @param renderer Panel renderer. + * @param font Keyboard font. + * @param i Cell index. + * @param key Cell pixel bounds. + */ + void render_keyboard_key(SDL_Renderer *renderer, TTF_Font *font, int i, SDL_Rect key); + /** + * @brief Draw or restore one wheel sector and its action label. + * @param renderer Panel renderer. + * @param font Wheel font. + * @param paint Wheel rasterization context. + * @param sector Clockwise sector from up. + */ + void render_wheel_sector(SDL_Renderer *renderer, TTF_Font *font, const WheelPaint &paint, int sector); + /** + * @brief Draw both menu wheels and their footer controls. + * @param renderer Panel renderer. + * @param font Wheel font. + * @param width Panel width. + * @param height Panel height. + * @param stats Current statistics setting. + * @param focusOnly Repaint only changed sectors. + */ + void render_wheels(SDL_Renderer *renderer, TTF_Font *font, int width, int height, bool stats, bool focusOnly); + /** + * @brief Draw the live log panel and scroll position. + * @param renderer Panel renderer. + * @param font Log font. + * @param width Panel width. + * @param height Panel height. + */ + void render_logs(SDL_Renderer *renderer, TTF_Font *font, int width, int height); + /** + * @brief Draw direct cancel, disconnect and quit-app actions. + * @param renderer Panel renderer. + * @param font Dialog font. + * @param width Panel width. + * @param height Panel height. + */ + void render_exit_confirmation(SDL_Renderer *renderer, TTF_Font *font, int width, int height); + /** + * @brief Allocate panel geometry when needed and enforce the raster cache budget. + * @param width Panel width. + * @param height Panel height. + * @return True when a panel renderer is available. + */ + bool prepare_panel(int width, int height); + /** + * @brief Publish changed panel regions through the shared GPU and software overlay. + * @param backend Overlay backend. + * @param width Panel width. + * @param height Panel height. + * @param keyboard Opaque docked keyboard placement. + * @param focusOnly Publish only damaged regions. + */ + void publish_panel(FfmpegStreamBackend &backend, int width, int height, bool keyboard, bool focusOnly) const; /** * @brief Draw the keyboard, optionally repainting only the old and new focus cells. * @param renderer Software panel renderer. @@ -222,6 +389,26 @@ namespace streaming { * @param compact Use smaller glyphs inside keyboard keys, preserving footer sizing. */ void render_hint(SDL_Renderer *renderer, TTF_Font *font, const char *asset, const char *key, const char *text, SDL_Rect bounds, bool compact = false); + /** + * @brief Apply direct cancel, disconnect or quit-app input on the exit confirmation page. + * @param key SDL key code. + * @return True for a confirmed stream exit. + */ + bool confirm_exit_key(SDL_Keycode key); + /** + * @brief Apply a layout, modifier, docking or editing shortcut on the keyboard page. + * @param key SDL key code. + * @param active Transport readiness for host key taps. + * @return True when the keyboard consumes this shortcut. + */ + bool handle_keyboard_shortcut(SDL_Keycode key, bool active); + /** + * @brief Execute the selected wheel action or open its local page. + * @param backend Overlay and screenshot backend. + * @param active Transport readiness. + * @param stats Mutable session statistics setting. + */ + void activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats); /** * @brief Process a local navigation or activation key. * @param key SDL key code. @@ -243,13 +430,6 @@ namespace streaming { std::array devices_ {}; ///< Stable local controller slots. input::StreamInput router_; ///< Ordered event-thread host input. input::StreamMenu menu_; ///< Local menu state. - input::KeyboardNavigation keyboardNavigation_; ///< Local keyboard repeats, independent of gameplay key forwarding. - input::DirectionNavigation controllerNavigation_; ///< Immediate direction changes with the shared repeat interval. - bool controllerNavigationApplied_ = false; ///< Queued events and the polling sample can advance focus only once per input pass. - bool navigationNeutralRequired_ = false; ///< Release wheel navigation before moving around a newly opened page. - input::UiInputDevice inputDevice_ = input::UiInputDevice::gamepad; ///< Device used by local helper text. - bool armed_ = false; ///< Opening buttons have been released. - bool dirty_ = false; ///< Overlay requires rasterization. bool focused_ = true; ///< Window focus state. int mouseDevice_ = -1; ///< Virtual mouse controller, or disabled. Uint32 lastTick_ = 0; ///< Pointer integration clock. @@ -257,29 +437,52 @@ namespace streaming { Uint32 pulseUntil_ = 0; ///< Guide/share pulse deadline. std::uint32_t pulseButtons_ = 0; ///< Button held across normal packets after the wheel closes. std::array latestHostController_ {}; ///< Latest unmodified transport state for pulse release. - std::string message_; ///< Last action result. - std::vector logs_; ///< Snapshot of runtime log lines. - int menuWidth_ = 0; ///< Last video rectangle width used for local controls. - int menuHeight_ = 0; ///< Last video rectangle height used for local controls. - bool keyboardPageTriggerDown_ = false; ///< Edge latch for the left-trigger keyboard layout shortcut. - bool keyboardDockTriggerDown_ = false; ///< Edge latch for right-trigger keyboard docking. bool keyboardTop_ = false; ///< Dock the keyboard flush with the top edge. std::string screenshotDirectory_; ///< Configured screenshot folder, empty for the default. ScreenshotNotification screenshotNotification_; ///< Progress and timed completion feedback independent of menu capture. app::ScreenshotSource screenshotSource_; ///< Stream identity retained with each capture. - Uint32 logRefreshTick_ = 0; ///< Last live log refresh. - std::map, std::shared_ptr> icons_; ///< Cached SVG rasters survive panel renderer replacement. - std::unique_ptr panel_ {nullptr, SDL_FreeSurface}; ///< Retained software composition target. - std::unique_ptr panelRenderer_ {nullptr, SDL_DestroyRenderer}; ///< Renderer reused at a stable panel size. - std::map, CachedText> text_; ///< Bounded cache of unchanged label rasters. - std::size_t textBytes_ = 0; ///< Estimated text texture bytes, bounded to protect the Xbox memory budget. - std::map, std::shared_ptr> iconTextures_; ///< Renderer-owned SVG textures. /** * @brief Content dependencies other than the focused key or wheel sector. */ using PanelContent = std::tuple; - std::optional panelContent_; ///< Last fully drawn content; changes invalidate incremental focus painting. - int panelSelection_ = -1; ///< Previously painted keyboard cell or wheel sector. - std::vector panelDamage_; ///< Regions repainted during the current input pass. + + /** + * @brief Input ownership, release gates and local navigation timing. + */ + struct NavigationState { + input::KeyboardNavigation keyboard; ///< Local keyboard repeats, independent of gameplay key forwarding. + input::DirectionNavigation controller; ///< Immediate direction changes with the shared repeat interval. + bool applied = false; ///< Queued events and the polling sample can advance focus only once per input pass. + bool neutralRequired = false; ///< Release wheel navigation before moving around a newly opened page. + input::UiInputDevice inputDevice = input::UiInputDevice::gamepad; ///< Device used by local helper text. + bool armed = false; ///< Opening buttons have been released. + bool pageTriggerDown = false; ///< Edge latch for the left-trigger keyboard layout shortcut. + bool dockTriggerDown = false; ///< Edge latch for right-trigger keyboard docking. + }; + + NavigationState navigation_; ///< Input ownership, release gates and local navigation timing. + + /** + * @brief Retained overlay surfaces, rasters and content invalidation. + */ + struct PanelCache { + bool dirty = false; ///< Overlay requires rasterization. + std::string message; ///< Last action result. + std::vector logs; ///< Snapshot of runtime log lines. + int width = 0; ///< Last video rectangle width used for local controls. + int height = 0; ///< Last video rectangle height used for local controls. + Uint32 logRefreshTick = 0; ///< Last live log refresh. + std::map, std::shared_ptr> icons; ///< Cached SVG rasters survive panel renderer replacement. + std::unique_ptr surface {nullptr, &SDL_FreeSurface}; ///< Retained software composition target. + std::unique_ptr renderer {nullptr, &SDL_DestroyRenderer}; ///< Renderer reused at a stable panel size. + std::map, CachedText> text; ///< Bounded cache of unchanged label rasters. + std::size_t textBytes = 0; ///< Estimated text texture bytes, bounded to protect the Xbox memory budget. + std::map, std::shared_ptr> iconTextures; ///< Renderer-owned SVG textures. + std::optional content; ///< Last fully drawn content; changes invalidate incremental focus painting. + int selection = -1; ///< Previously painted keyboard cell or wheel sector. + std::vector damage; ///< Regions repainted during the current input pass. + }; + + PanelCache panel_; ///< Retained overlay surfaces, rasters and content invalidation. }; } // namespace streaming diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index e21d5a5..8139df2 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -103,6 +103,48 @@ namespace { } return result; } + + /** + * @brief Bound changed ARGB rows and merge damage not yet presented. + * @param before Previous validated bitmap. + * @param after Replacement bitmap with matching geometry. + * @param width Bitmap width. + * @param height Bitmap height. + * @param pending Previously accumulated damage. + * @return Union of the changed pixel bounds and pending damage. + */ + streaming::VideoRectangle argb_damage(const std::vector &before, const std::vector &after, int width, int height, const streaming::VideoRectangle &pending) { + int left = width; + int right = 0; + int top = height; + int bottom = 0; + for (int row = 0; row < height; ++row) { + const auto offset = static_cast(row) * width * 4; + if (std::memcmp(before.data() + offset, after.data() + offset, static_cast(width) * 4) == 0) { + continue; + } + int first = 0; + while (first < width && std::memcmp(before.data() + offset + first * 4, after.data() + offset + first * 4, 4) == 0) { + ++first; + } + int last = width - 1; + while (last > first && std::memcmp(before.data() + offset + last * 4, after.data() + offset + last * 4, 4) == 0) { + --last; + } + left = std::min(left, first); + right = std::max(right, last + 1); + top = std::min(top, row); + bottom = row + 1; + } + if (pending.width > 0 && pending.height > 0) { + left = std::min(left, pending.x); + right = std::max(right, pending.x + pending.width); + top = std::min(top, pending.y); + bottom = std::max(bottom, pending.y + pending.height); + } + return {left, top, right - left, bottom - top}; + } + } // namespace namespace streaming { @@ -130,7 +172,9 @@ namespace streaming { continue; } if (opacity == 0) { - bitmap[index] = bitmap[index + 1] = bitmap[index + 2] = 0; + bitmap[index] = 0; + bitmap[index + 1] = 0; + bitmap[index + 2] = 0; continue; } for (int component = 0; component < 3; ++component) { @@ -143,13 +187,83 @@ namespace streaming { return bitmap; } - bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected, int sector, const VideoRectangle *clip) { + /** + * @brief Validate a wheel's destination, geometry and optional repaint bounds. + * @param target Writable ARGB bitmap. + * @param wheel Wheel geometry and selection. + * @param sector Requested sector, or minus one for the whole ring. + * @param clip Optional repaint rectangle. + * @return True when rasterization remains within the destination. + */ + bool valid_menu_wheel(OverlayBitmap target, const MenuWheel &wheel, int sector, const VideoRectangle *clip) { + const auto [pixels, capacity, pitch, width, height] = target; + const auto [centerX, centerY, radius, selected] = wheel; + if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048 || centerX < 0 || centerX >= width || centerY < 0 || centerY >= height || radius < 1 || radius > 2048 || sector < -1 || sector > 3) { return false; } if (clip != nullptr && (clip->x < 0 || clip->y < 0 || clip->width <= 0 || clip->height <= 0 || clip->width > width || clip->height > height || clip->x > width - clip->width || clip->y > height - clip->height)) { return false; } + return true; + } + + /** + * @brief Bound a single sector's columns on one wheel row. + * @param wheel Wheel geometry. + * @param y Row displacement from its center. + * @param first First visible column. + * @param last Last visible column. + * @param sector Requested sector, or minus one for the whole ring. + * @return Inclusive clipped column bounds. + */ + std::pair wheel_columns(const MenuWheel &wheel, int y, int first, int last, int sector) { + if (sector == 0 || sector == 2) { + return {std::max(first, wheel.centerX - std::abs(y)), std::min(last, wheel.centerX + std::abs(y))}; + } + if (sector == 1) { + first = std::max(first, wheel.centerX + std::abs(y) + 1); + } + if (sector == 3) { + last = std::min(last, wheel.centerX - std::abs(y) - 1); + } + return {first, last}; + } + + /** + * @brief Resolve a pixel's clockwise wheel sector with vertical diagonal ties. + * @param x Horizontal displacement. + * @param y Vertical displacement, positive downward. + * @return Up, right, down or left as zero through three. + */ + int wheel_sector(int x, int y) { + if (std::abs(y) >= std::abs(x)) { + return y < 0 ? 0 : 2; + } + return x > 0 ? 1 : 3; + } + + /** + * @brief Choose the separator or normal/selected wheel shade. + * @param x Horizontal displacement. + * @param y Vertical displacement. + * @param direction Pixel sector. + * @param selected Highlighted sector. + * @return Opaque grayscale component. + */ + unsigned wheel_shade(int x, int y, int direction, int selected) { + if (std::abs(std::abs(x) - std::abs(y)) < 2) { + return 110; + } + return direction == selected ? 85 : 36; + } + + bool rasterize_menu_wheel(OverlayBitmap target, const MenuWheel &wheel, int sector, const VideoRectangle *clip) { + const auto [pixels, capacity, pitch, width, height] = target; + const auto [centerX, centerY, radius, selected] = wheel; + if (!valid_menu_wheel(target, wheel, sector, clip)) { + return false; + } const int firstRow = std::max({0, centerY - radius, clip != nullptr ? clip->y : 0}); const int lastRow = std::min({height - 1, centerY + radius, clip != nullptr ? clip->y + clip->height - 1 : height - 1}); const int firstColumn = std::max({0, centerX - radius, clip != nullptr ? clip->x : 0}); @@ -159,24 +273,21 @@ namespace streaming { if ((sector == 0 && y > 0) || (sector == 2 && y < 0)) { continue; } - const int start = sector == 0 || sector == 2 ? std::max(firstColumn, centerX - std::abs(y)) : sector == 1 ? std::max(firstColumn, centerX + std::abs(y) + 1) : - firstColumn; - const int end = sector == 0 || sector == 2 ? std::min(lastColumn, centerX + std::abs(y)) : sector == 3 ? std::min(lastColumn, centerX - std::abs(y) - 1) : - lastColumn; + const auto [start, end] = wheel_columns(wheel, y, firstColumn, lastColumn, sector); for (int column = start; column <= end; ++column) { const int x = column - centerX; - const int distance = x * x + y * y; - if (distance > radius * radius || distance < radius * radius / 9) { + if (const int distance = x * x + y * y; distance > radius * radius || distance < radius * radius / 9) { continue; } - const int direction = std::abs(y) >= std::abs(x) ? (y < 0 ? 0 : 2) : (x > 0 ? 1 : 3); + const int direction = wheel_sector(x, y); if (sector >= 0 && sector != direction) { continue; } - const std::uint32_t gray = std::abs(std::abs(x) - std::abs(y)) < 2 ? 110 : direction == selected ? 85 : - 36; + const auto gray = wheel_shade(x, y, direction, selected); auto *pixel = pixels + static_cast(row) * pitch + column * 4; - pixel[0] = pixel[1] = pixel[2] = gray; + pixel[0] = gray; + pixel[1] = gray; + pixel[2] = gray; pixel[3] = 255; } } @@ -239,35 +350,7 @@ namespace streaming { const bool reuse = sameGeometry && opaque == opaque_; VideoRectangle changed {}; if (reuse) { - int left = width; - int right = 0; - int top = height; - int bottom = 0; - for (int row = 0; row < height; ++row) { - const auto offset = static_cast(row) * width * 4; - if (std::memcmp(argb_.data() + offset, pixels.data() + offset, static_cast(width) * 4) == 0) { - continue; - } - int first = 0; - while (std::memcmp(argb_.data() + offset + first * 4, pixels.data() + offset + first * 4, 4) == 0) { - ++first; - } - int last = width - 1; - while (std::memcmp(argb_.data() + offset + last * 4, pixels.data() + offset + last * 4, 4) == 0) { - --last; - } - left = std::min(left, first); - right = std::max(right, last + 1); - top = std::min(top, row); - bottom = row + 1; - } - if (damage_.width > 0 && damage_.height > 0) { - left = std::min(left, damage_.x); - right = std::max(right, damage_.x + damage_.width); - top = std::min(top, damage_.y); - bottom = std::max(bottom, damage_.y + damage_.height); - } - changed = {left, top, right - left, bottom - top}; + changed = argb_damage(argb_, pixels, width, height, damage_); } width_ = width; height_ = height; @@ -425,7 +508,9 @@ namespace streaming { const auto bitmapRow = static_cast(row) * width_; if (opaque_) { std::memcpy(destinationRow, converted.data() + bitmapRow * bytes, copyBytes); - } else { + continue; + } + { for (int column = 0; column < bounds.width; ++column) { const auto index = bitmapRow + column; const auto color = color_at(index); @@ -441,14 +526,8 @@ namespace streaming { return true; } - bool VideoOverlay::draw_yuy2(std::uint8_t *pixels, std::size_t capacity, int pitch, int frameWidth, int frameHeight, const VideoRectangle &destination, VideoColorMatrix matrix) { - if (width_ == 0) { - return true; - } - if (!valid_storage(pixels, capacity, pitch, frameWidth, frameHeight, 2) || (frameWidth & 1) != 0 || frameWidth > 2046 || frameHeight > 2047 || destination.width <= MARGIN || destination.height <= MARGIN || matrix == VideoColorMatrix::unsupported) { - return false; - } - const bool rebuild = frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height || matrix_ != matrix; + bool VideoOverlay::prepare_yuy2(int frameWidth, int frameHeight, const VideoRectangle &destination, VideoColorMatrix matrix, bool &rebuild) { + rebuild = frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height || matrix_ != matrix; if (rebuild) { matrix_ = matrix; frameWidth_ = frameWidth; @@ -458,7 +537,7 @@ namespace streaming { const auto bounds = rectangle({0, 0, destination.width, destination.height}); if (bounds.width == 0 || bounds.height == 0) { source_ = {}; - return true; + return false; } source_.x = (static_cast(static_cast(bounds.x) * frameWidth / destination.width) + 1) & ~1; source_.y = static_cast(static_cast(bounds.y) * frameHeight / destination.height); @@ -467,59 +546,84 @@ namespace streaming { source_.width = std::min(frameWidth - source_.x, ((visibleWidth * frameWidth + destination.width - 1) / destination.width + 1) & ~1); source_.height = std::min(frameHeight - source_.y, (visibleHeight * frameHeight + destination.height - 1) / destination.height); if (source_.width <= 0 || source_.height <= 0) { - return true; + return false; } yuy2_.resize(static_cast(source_.width) * source_.height * 2); if (!opaque_) { yuy2Opacity_.resize(yuy2_.size() / 2); } } - if (rebuild || (damage_.width > 0 && damage_.height > 0)) { - const auto bounds = rectangle({0, 0, destination.width, destination.height}); - const int visibleWidth = bounds.width; - const int visibleHeight = bounds.height; - if (visibleWidth == 0 || visibleHeight == 0) { - return true; - } - const int firstRow = rebuild ? 0 : std::min(source_.height, damage_.y * source_.height / visibleHeight); - const int lastRow = rebuild ? source_.height : std::min(source_.height, ((damage_.y + damage_.height) * source_.height + visibleHeight - 1) / visibleHeight); - const int firstColumn = rebuild ? 0 : std::min(source_.width, (damage_.x * source_.width / visibleWidth) & ~1); - const int lastColumn = rebuild ? source_.width : std::min(source_.width, ((((damage_.x + damage_.width) * source_.width + visibleWidth - 1) / visibleWidth) + 1) & ~1); - const bool nativeWidth = visibleWidth == source_.width; - for (int row = firstRow; row < lastRow; ++row) { - const int bitmapRow = std::min(visibleHeight - 1, row * visibleHeight / source_.height); - for (int column = firstColumn; column < lastColumn; column += 2) { - const int firstColumn = nativeWidth ? column : std::min(visibleWidth - 1, column * visibleWidth / source_.width); - const int secondColumn = nativeWidth ? column + 1 : std::min(visibleWidth - 1, (column + 1) * visibleWidth / source_.width); - const auto firstColor = color_at(static_cast(bitmapRow) * width_ + firstColumn); - const auto secondColor = color_at(static_cast(bitmapRow) * width_ + secondColumn); - const auto first = rgb_to_yuv(firstColor, matrix); - const auto second = rgb_to_yuv(secondColor, matrix); - const unsigned firstAlpha = firstColor >> 24; - const unsigned secondAlpha = secondColor >> 24; - const unsigned coverage = firstAlpha + secondAlpha; - const auto offset = (static_cast(row) * source_.width + column) * 2; - yuy2_[offset] = first[0]; - yuy2_[offset + 2] = second[0]; - // Weight shared chroma by coverage so a transparent colored pixel - // cannot tint its opaque neighbor or desaturate the host underneath. - // Most wheel/key pixels are gray. Equal chroma needs no coverage - // division, including antialiased text and partially dimmed video. - yuy2_[offset + 1] = coverage == 0 ? 128 : first[1] == second[1] ? first[1] : - (first[1] * firstAlpha + second[1] * secondAlpha + coverage / 2) / coverage; - yuy2_[offset + 3] = coverage == 0 ? 128 : first[2] == second[2] ? first[2] : - (first[2] * firstAlpha + second[2] * secondAlpha + coverage / 2) / coverage; - if (!opaque_) { - yuy2Opacity_[offset / 2] = firstAlpha; - yuy2Opacity_[offset / 2 + 1] = secondAlpha; - } - } + return true; + } + + /** + * @brief Weight shared chroma without allowing transparent pixels to tint their neighbors. + * @param first First chroma component. + * @param second Second chroma component. + * @param firstAlpha First pixel coverage. + * @param secondAlpha Second pixel coverage. + * @return Coverage-weighted chroma, or neutral chroma for a transparent pair. + */ + unsigned covered_chroma(unsigned first, unsigned second, unsigned firstAlpha, unsigned secondAlpha) { + const unsigned coverage = firstAlpha + secondAlpha; + if (coverage == 0) { + return 128; + } + if (first == second) { + return first; + } + return (first * firstAlpha + second * secondAlpha + coverage / 2) / coverage; + } + + void VideoOverlay::convert_yuy2_pair(int row, int column, int bitmapRow, int firstBitmapColumn, int secondBitmapColumn, VideoColorMatrix matrix) { + const auto firstColor = color_at(static_cast(bitmapRow) * width_ + firstBitmapColumn); + const auto secondColor = color_at(static_cast(bitmapRow) * width_ + secondBitmapColumn); + const auto first = rgb_to_yuv(firstColor, matrix); + const auto second = rgb_to_yuv(secondColor, matrix); + const unsigned firstAlpha = firstColor >> 24; + const unsigned secondAlpha = secondColor >> 24; + const auto offset = (static_cast(row) * source_.width + column) * 2; + yuy2_[offset] = first[0]; + yuy2_[offset + 2] = second[0]; + // Weight shared chroma by coverage so a transparent colored pixel + // cannot tint its opaque neighbor or desaturate the host underneath. + // Most wheel/key pixels are gray. Equal chroma needs no coverage + // division, including antialiased text and partially dimmed video. + yuy2_[offset + 1] = covered_chroma(first[1], second[1], firstAlpha, secondAlpha); + yuy2_[offset + 3] = covered_chroma(first[2], second[2], firstAlpha, secondAlpha); + if (!opaque_) { + yuy2Opacity_[offset / 2] = firstAlpha; + yuy2Opacity_[offset / 2 + 1] = secondAlpha; + } + } + + void VideoOverlay::update_yuy2(const VideoRectangle &destination, VideoColorMatrix matrix, bool rebuild) { + const auto bounds = rectangle({0, 0, destination.width, destination.height}); + const int visibleWidth = bounds.width; + const int visibleHeight = bounds.height; + if (visibleWidth == 0 || visibleHeight == 0) { + return; + } + const int firstRow = rebuild ? 0 : std::min(source_.height, damage_.y * source_.height / visibleHeight); + const int lastRow = rebuild ? source_.height : std::min(source_.height, ((damage_.y + damage_.height) * source_.height + visibleHeight - 1) / visibleHeight); + const int firstColumn = rebuild ? 0 : std::min(source_.width, (damage_.x * source_.width / visibleWidth) & ~1); + const int lastColumn = rebuild ? source_.width : std::min(source_.width, ((((damage_.x + damage_.width) * source_.width + visibleWidth - 1) / visibleWidth) + 1) & ~1); + const bool nativeWidth = visibleWidth == source_.width; + for (int row = firstRow; row < lastRow; ++row) { + const int bitmapRow = std::min(visibleHeight - 1, row * visibleHeight / source_.height); + for (int column = firstColumn; column < lastColumn; column += 2) { + const int firstBitmapColumn = nativeWidth ? column : std::min(visibleWidth - 1, column * visibleWidth / source_.width); + const int secondBitmapColumn = nativeWidth ? column + 1 : std::min(visibleWidth - 1, (column + 1) * visibleWidth / source_.width); + convert_yuy2_pair(row, column, bitmapRow, firstBitmapColumn, secondBitmapColumn, matrix); } } + } + + void VideoOverlay::blend_yuy2(std::uint8_t *pixels, int pitch) { damage_ = {}; if (source_.width <= 0 || source_.height <= 0) { - return true; + return; } for (int row = 0; row < source_.height; ++row) { auto *destinationRow = pixels + static_cast(source_.y + row) * pitch + source_.x * 2; @@ -527,7 +631,9 @@ namespace streaming { const auto *foreground = yuy2_.data() + rowOffset * 2; if (opaque_) { std::memcpy(destinationRow, foreground, source_.width * 2); - } else { + continue; + } + { for (int column = 0; column < source_.width; column += 2) { const unsigned left = yuy2Opacity_[rowOffset + column]; const unsigned right = yuy2Opacity_[rowOffset + column + 1]; @@ -552,6 +658,23 @@ namespace streaming { } } } + } + + bool VideoOverlay::draw_yuy2(std::uint8_t *pixels, std::size_t capacity, int pitch, int frameWidth, int frameHeight, const VideoRectangle &destination, VideoColorMatrix matrix) { + if (width_ == 0) { + return true; + } + if (!valid_storage(pixels, capacity, pitch, frameWidth, frameHeight, 2) || (frameWidth & 1) != 0 || frameWidth > 2046 || frameHeight > 2047 || destination.width <= MARGIN || destination.height <= MARGIN || matrix == VideoColorMatrix::unsupported) { + return false; + } + bool rebuild = false; + if (!prepare_yuy2(frameWidth, frameHeight, destination, matrix, rebuild)) { + return true; + } + if (rebuild || (damage_.width > 0 && damage_.height > 0)) { + update_yuy2(destination, matrix, rebuild); + } + blend_yuy2(pixels, pitch); return true; } } // namespace streaming diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index 7a9a2a9..b433da8 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -44,24 +44,35 @@ namespace streaming { OverlayLabelLayout layout_overlay_label(int width, int height, const VideoRectangle &bounds, bool shrinkToFit); /** - * @brief Rasterize a grayscale selection wheel directly into an ARGB panel. - * - * Avoid thousands of immediately flushed software-renderer commands per highlight. - * - * @param pixels ARGB8888 destination storage. - * @param capacity Accessible bytes. - * @param pitch Destination row stride. - * @param width Panel width, at most 2048. - * @param height Panel height, at most 2048. - * @param centerX Wheel center X inside the panel. - * @param centerY Wheel center Y inside the panel. - * @param radius Outer radius in pixels, between 1 and 2048. - * @param selected Selected clockwise sector starting at up, or -1 for none. - * @param sector Repaint only this clockwise sector, or -1 for the whole ring. - * @param clip Optional pixel rectangle restricting background restoration behind a sector label. - * @return False when storage or geometry is invalid, without writing pixels. + * @brief Writable ARGB bitmap view with validated row storage. */ - bool rasterize_menu_wheel(std::uint8_t *pixels, std::size_t capacity, int pitch, int width, int height, int centerX, int centerY, int radius, int selected, int sector = -1, const VideoRectangle *clip = nullptr); + struct OverlayBitmap { + std::uint8_t *pixels; ///< First destination byte. + std::size_t capacity; ///< Accessible destination byte count. + int pitch; ///< Byte stride between rows. + int width; ///< Pixel width. + int height; ///< Pixel height. + }; + + /** + * @brief Circular menu geometry and clockwise highlighted sector. + */ + struct MenuWheel { + int centerX; ///< Horizontal center in destination pixels. + int centerY; ///< Vertical center in destination pixels. + int radius; ///< Outer radius in pixels. + int selected; ///< Selected up/right/down/left sector, or minus one for none. + }; + + /** + * @brief Rasterize all or one sector of an opaque circular menu onto ARGB storage. + * @param target Destination bitmap, at most 2048 pixels in either dimension. + * @param wheel Geometry with a radius between 1 and 2048, and its highlighted sector. + * @param sector Repaint only this clockwise sector, or minus one for the whole ring. + * @param clip Optional repaint rectangle restricting background restoration. + * @return False for invalid storage or geometry, without changing destination pixels. + */ + bool rasterize_menu_wheel(OverlayBitmap target, const MenuWheel &wheel, int sector = -1, const VideoRectangle *clip = nullptr); /** * @brief Placement of a shared panel inside the presented video rectangle. @@ -176,6 +187,40 @@ namespace streaming { static constexpr int MARGIN = 8; ///< Panel inset in output pixels. private: + /** + * @brief Prepare geometry and storage for a YUY2 presentation cache. + * @param frameWidth Video width. + * @param frameHeight Video height. + * @param destination Output video rectangle. + * @param matrix Host color matrix. + * @param rebuild Receives whether geometry or matrix changed. + * @return False when no overlay pixels are visible. + */ + bool prepare_yuy2(int frameWidth, int frameHeight, const VideoRectangle &destination, VideoColorMatrix matrix, bool &rebuild); + /** + * @brief Convert one YUY2 pixel pair into the retained cache. + * @param row Cache row. + * @param column Cache pair column. + * @param bitmapRow Mapped source row. + * @param firstBitmapColumn First mapped source column. + * @param secondBitmapColumn Second mapped source column. + * @param matrix Host color matrix. + */ + void convert_yuy2_pair(int row, int column, int bitmapRow, int firstBitmapColumn, int secondBitmapColumn, VideoColorMatrix matrix); + /** + * @brief Refresh only damaged YUY2 cache rows and columns. + * @param destination Output video rectangle. + * @param matrix Host color matrix. + * @param rebuild True to convert the entire overlay. + */ + void update_yuy2(const VideoRectangle &destination, VideoColorMatrix matrix, bool rebuild); + /** + * @brief Blend the prepared YUY2 cache into validated framebuffer storage. + * @param pixels Framebuffer storage. + * @param pitch Framebuffer row stride. + */ + void blend_yuy2(std::uint8_t *pixels, int pitch); + /** * @brief Resolve either source format to one straight ARGB pixel. * @param index Packed source pixel index. diff --git a/src/ui/screenshot_image.cpp b/src/ui/screenshot_image.cpp index bbb59b1..a7e86c2 100644 --- a/src/ui/screenshot_image.cpp +++ b/src/ui/screenshot_image.cpp @@ -36,14 +36,12 @@ namespace ui { const auto height = little_endian(header, 22, 4); const auto planes = little_endian(header, 26, 2); const auto bits = little_endian(header, 28, 2); - const auto compression = little_endian(header, 30, 4); // Captures are 24-bit BI_RGB. Also accept ordinary 32-bit BI_RGB files, with // a two-megapixel ceiling so a damaged header cannot exhaust the Xbox heap. - if (dibSize < 40 || dibSize > 124 || offset < 14U + dibSize || width == 0 || width > 1920 || height == 0 || height > 1080 || planes != 1 || (bits != 24 && bits != 32) || compression != 0) { + if (const auto compression = little_endian(header, 30, 4); dibSize < 40 || dibSize > 124 || offset < 14U + dibSize || width == 0 || width > 1920 || height == 0 || height > 1080 || planes != 1 || (bits != 24 && bits != 32) || compression != 0) { return {}; } - const std::uint64_t stride = (static_cast(width) * bits + 31U) / 32U * 4U; - if (offset + stride * height > fileSize) { + if (const std::uint64_t stride = (static_cast(width) * bits + 31U) / 32U * 4U; offset + stride * height > fileSize) { return {}; } return {static_cast(width), static_cast(height)}; diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 30b6f1e..361d973 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -1448,8 +1448,7 @@ namespace { } const int hintHeight = measure_footer_actions_height(smallFont, viewModel.modal.footerActions, modalRect.w - 36); - const SDL_Rect hintRect {modalRect.x + 18, modalRect.y + 64 + pathHeight + 8, modalRect.w - 36, hintHeight}; - if (!render_footer_actions(renderer, smallFont, assetCache, viewModel.modal.footerActions, hintRect)) { + if (const SDL_Rect hintRect {modalRect.x + 18, modalRect.y + 64 + pathHeight + 8, modalRect.w - 36, hintHeight}; !render_footer_actions(renderer, smallFont, assetCache, viewModel.modal.footerActions, hintRect)) { return false; } @@ -1498,6 +1497,60 @@ namespace { return true; } + /** + * @brief Optional icon, focus and retained-cache settings for action rows. + */ + struct ActionRowsOptions { + AssetTextureCache *assetCache = nullptr; ///< Shared action icon cache. + bool active = true; ///< Whether the list owns controller focus. + FolderDialogCache *folder = nullptr; ///< Retained row-label and scroll state. + bool repaint = false; ///< Restore only rows affected by a focus change. + }; + + /** + * @brief Geometry and visible indices for a retained action-list repaint. + */ + struct ActionRowsLayout { + SDL_Rect bounds; ///< Complete list bounds. + int rowHeight; ///< Single row height. + int rowStep; ///< Distance between successive rows. + int contentWidth; ///< Width excluding the scrollbar. + std::size_t startIndex; ///< First visible row. + std::size_t endIndex; ///< One past the last visible row. + std::size_t selectedIndex; ///< Newly focused row. + bool windowChanged; ///< The visible row range moved. + }; + + /** + * @brief Erase stale focus backgrounds before repainting retained action rows. + * @param renderer Dialog renderer. + * @param folder Previous focus and scroll state. + * @param layout Current visible row geometry. + */ + void restore_action_row_backgrounds(SDL_Renderer *renderer, const FolderDialogCache &folder, const ActionRowsLayout &layout) { + if (layout.windowChanged) { + fill_rect(renderer, layout.bounds, 0x15, 0x1E, 0x2E); + } else { + for (const auto index : {folder.selected, layout.selectedIndex}) { + if (index >= layout.startIndex && index < layout.endIndex) { + fill_rect(renderer, {layout.bounds.x, layout.bounds.y + static_cast(index - layout.startIndex) * layout.rowStep, layout.contentWidth, layout.rowHeight}, 0x15, 0x1E, 0x2E); + } + } + } + } + + /** + * @brief Find the first focused action, using the first row when no focus is marked. + * @param rows Dialog actions. + * @return Focused row index, or zero. + */ + std::size_t selected_action_index(const std::vector &rows) { + const auto found = std::find_if(rows.begin(), rows.end(), [](const ui::ShellActionRow &row) { + return row.selected; + }); + return found == rows.end() ? 0 : static_cast(found - rows.begin()); + } + /** * @brief Draw scrollable action rows with rounded checkboxes, a filled selection and focus rail. * @@ -1512,17 +1565,8 @@ namespace { * @param repaint Repaint affected rows in the retained frame instead of the complete list. * @return True when visible labels rendered successfully. */ - bool render_action_rows( - SDL_Renderer *renderer, - TTF_Font *font, - const std::vector &rows, - const SDL_Rect &rect, - int rowHeight, - AssetTextureCache *assetCache = nullptr, - bool active = true, - FolderDialogCache *folder = nullptr, - bool repaint = false - ) { + bool render_action_rows(SDL_Renderer *renderer, TTF_Font *font, const std::vector &rows, const SDL_Rect &rect, int rowHeight, const ActionRowsOptions &options = {}) { + const auto &[assetCache, active, folder, repaint] = options; if (rows.empty()) { return true; } @@ -1560,15 +1604,7 @@ namespace { if (folder != nullptr) { if (repaint) { - if (windowChanged) { - fill_rect(renderer, rect, 0x15, 0x1E, 0x2E); - } else { - for (const auto index : {folder->selected, selectedIndex}) { - if (index >= startIndex && index < endIndex) { - fill_rect(renderer, {rect.x, rect.y + static_cast(index - startIndex) * rowStep, contentWidth, rowHeight}, 0x15, 0x1E, 0x2E); - } - } - } + restore_action_row_backgrounds(renderer, *folder, {rect, rowHeight, rowStep, contentWidth, startIndex, endIndex, selectedIndex, windowChanged}); } for (auto it = folder->labels.begin(); it != folder->labels.end();) { if (it->first < startIndex || it->first >= endIndex) { @@ -1707,10 +1743,14 @@ namespace { FooterActionChipLayout layout; const bool keyboard = !action.keyLabel.empty() || !action.secondaryKeyLabel.empty(); layout.iconSize = keyboard ? std::max(18, TTF_FontHeight(font) + 6) : std::max(18, chipHeight - 14); - layout.primaryIconWidth = !action.keyLabel.empty() ? measure_footer_action_label_width(font, action.keyLabel) + 12 : action.iconAssetPath.empty() ? 0 : - layout.iconSize; - layout.secondaryIconWidth = !action.secondaryKeyLabel.empty() ? measure_footer_action_label_width(font, action.secondaryKeyLabel) + 12 : action.secondaryIconAssetPath.empty() ? 0 : - layout.iconSize; + layout.primaryIconWidth = action.iconAssetPath.empty() ? 0 : layout.iconSize; + if (!action.keyLabel.empty()) { + layout.primaryIconWidth = measure_footer_action_label_width(font, action.keyLabel) + 12; + } + layout.secondaryIconWidth = action.secondaryIconAssetPath.empty() ? 0 : layout.iconSize; + if (!action.secondaryKeyLabel.empty()) { + layout.secondaryIconWidth = measure_footer_action_label_width(font, action.secondaryKeyLabel) + 12; + } layout.iconBlockWidth = layout.primaryIconWidth + layout.secondaryIconWidth + (layout.primaryIconWidth > 0 && layout.secondaryIconWidth > 0 ? 4 : 0); layout.horizontalPadding = compact ? 6 : 10; layout.iconLabelGap = compact ? 4 : 8; @@ -2410,10 +2450,8 @@ namespace { */ void process_directory_picker_requests(app::ClientState &state, app::AppUpdate &update) { auto &picker = state.directoryPicker; - if (update.requests.directorySelectRequested) { - if (platform::directory_is_writable(picker.path, picker.error)) { - app::accept_screenshot_directory(state, update, picker.path); - } + if (update.requests.directorySelectRequested && platform::directory_is_writable(picker.path, picker.error)) { + app::accept_screenshot_directory(state, update, picker.path); } if (update.requests.directoryCreateRequested && platform::create_child_directory(picker.path, picker.name, picker.error)) { picker.path = platform::join_path(picker.path, picker.name); @@ -2455,8 +2493,8 @@ namespace { state.settings.playAudioOnPc, state.settings.showPerformanceStats, state.settings.playAudioOnXbox, - state.settings.screenshotDirectory, - state.settings.keyboardDockTop, + state.settings.controls.screenshotDirectory, + state.settings.controls.keyboardDockTop, }; } @@ -3824,7 +3862,7 @@ namespace { const bool success = render_text_line_simple(renderer, smallFont, viewModel.content.selectedMenuRowLabel, {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, layout.title.x, layout.title.y, layout.title.w) && render_bounded_text(renderer, smallFont, viewModel.content.selectedMenuRowDescription, {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, sdl_rect(layout.description)); draw_line(renderer, layout.optionsHeader.x, layout.optionsHeader.y - 10, layout.optionsHeader.x + layout.optionsHeader.w, layout.optionsHeader.y - 10, MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0x30); - return success && render_text_line_simple(renderer, smallFont, "Options", {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, layout.optionsHeader.x, layout.optionsHeader.y, layout.optionsHeader.w) && render_action_rows(renderer, bodyFont, viewModel.content.detailMenuRows, sdl_rect(layout.options), rowHeight, nullptr, viewModel.content.rightPanelActive); + return success && render_text_line_simple(renderer, smallFont, "Options", {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, layout.optionsHeader.x, layout.optionsHeader.y, layout.optionsHeader.w) && render_action_rows(renderer, bodyFont, viewModel.content.detailMenuRows, sdl_rect(layout.options), rowHeight, {nullptr, viewModel.content.rightPanelActive}); } /** @@ -3925,6 +3963,15 @@ namespace { ); } + /** + * @brief Fonts shared by the title, content and actions of a shell dialog. + */ + struct DialogFonts { + TTF_Font *titleFont; ///< Dialog heading font. + TTF_Font *bodyFont; ///< Dialog body font. + TTF_Font *smallFont; ///< Helper and action font. + }; + /** * @brief Render a content-sized dialog with persistent guidance, scrolling actions or progress. * @@ -3939,7 +3986,8 @@ namespace { * @param folder Optional folder cache to populate for incremental navigation. * @return True when the dialog rendered successfully. */ - bool render_shell_dialog(SDL_Renderer *renderer, TTF_Font *titleFont, TTF_Font *bodyFont, TTF_Font *smallFont, AssetTextureCache *assetCache, const ui::ShellModalView &modal, int width, int height, FolderDialogCache *folder = nullptr) { + bool render_shell_dialog(SDL_Renderer *renderer, const DialogFonts &fonts, AssetTextureCache *assetCache, const ui::ShellModalView &modal, int width, int height, FolderDialogCache *folder = nullptr) { + const auto &[titleFont, bodyFont, smallFont] = fonts; const int padding = std::max(18, height / 40); const int dialogWidth = std::min(760, width * 3 / 4); const int innerWidth = dialogWidth - padding * 2; @@ -3978,7 +4026,7 @@ namespace { folder->rows = rows; folder->rowHeight = rowHeight; } - if (!modal.actions.empty() && !render_action_rows(renderer, bodyFont, modal.actions, rows, rowHeight, assetCache, true, folder)) { + if (!modal.actions.empty() && !render_action_rows(renderer, bodyFont, modal.actions, rows, rowHeight, {assetCache, true, folder})) { return false; } draw_line(renderer, bounds.x + padding, footerY - 6, bounds.x + bounds.w - padding, footerY - 6, MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0x25); @@ -4083,6 +4131,49 @@ namespace { return render_footer_actions(renderer, smallFont, assetCache, actions, {bounds.x + padding - 12, footerY, innerWidth + 24, footerHeight}, true); } +#ifdef NXDK + /** + * @brief Describe the Xbox framebuffer format and bytes per pixel. + * @param bpp Video depth. + * @return SDL format and byte count; zero bytes for an unsupported mode. + */ + std::pair folder_framebuffer_format(int bpp) { + if (bpp == 32) { + return {SDL_PIXELFORMAT_RGB888, 4}; + } + if (bpp == 16) { + return {SDL_PIXELFORMAT_RGB565, 2}; + } + return {SDL_PIXELFORMAT_RGB555, bpp == 15 ? 2 : 0}; + } + + /** + * @brief Copy damaged rows into Xbox scanout after validating the source and destination formats. + * @param surface Source software window surface. + * @param framebuffer Write-combined scanout storage. + * @param mode Current Xbox video mode. + * @param area Validated repaint bounds. + * @param bytes Bytes per pixel. + * @return False if locking failed and ordinary SDL presentation is required. + */ + bool copy_folder_framebuffer(SDL_Surface &surface, std::uint8_t *framebuffer, const VIDEO_MODE &mode, const ui::ShellRect &area, int bytes) { + const bool locked = SDL_MUSTLOCK(&surface) != 0; + if (locked && SDL_LockSurface(&surface) != 0) { + return false; + } + for (int y = area.y; y < area.y + area.h; ++y) { + const auto *source = static_cast(surface.pixels) + static_cast(y) * surface.pitch + area.x * bytes; + auto *destination = static_cast(framebuffer) + (static_cast(y) * mode.width + area.x) * bytes; + std::memcpy(destination, source, static_cast(area.w) * bytes); + } + if (locked) { + SDL_UnlockSurface(&surface); + } + XVideoFlushFB(); + return true; + } +#endif + /** * @brief Present changed folder rows without the Xbox SDL driver's full-screen copy, retaining SDL fallback. * @param renderer Software shell renderer to flush. @@ -4092,35 +4183,19 @@ namespace { */ void present_folder_damage(SDL_Renderer *renderer, SDL_Window *window, const SDL_Rect &logicalDamage, int logicalWidth) { #ifdef NXDK - SDL_RendererInfo info {}; - if (window != nullptr && SDL_GetRendererInfo(renderer, &info) == 0 && (info.flags & SDL_RENDERER_SOFTWARE) != 0) { + if (SDL_RendererInfo info {}; window != nullptr && SDL_GetRendererInfo(renderer, &info) == 0 && (info.flags & SDL_RENDERER_SOFTWARE) != 0) { SDL_RenderFlush(renderer); SDL_Surface *surface = SDL_GetWindowSurface(window); const VIDEO_MODE mode = XVideoGetMode(); void *framebuffer = XVideoGetFB(); - const Uint32 format = mode.bpp == 32 ? SDL_PIXELFORMAT_RGB888 : mode.bpp == 16 ? SDL_PIXELFORMAT_RGB565 : - SDL_PIXELFORMAT_RGB555; - const int bytes = mode.bpp == 32 ? 4 : mode.bpp == 15 || mode.bpp == 16 ? 2 : - 0; + const auto [format, bytes] = folder_framebuffer_format(mode.bpp); // The Xbox software window surface uses the video mode's RGB format. // Validate that relationship before copying into write-combined scanout. if (surface != nullptr && framebuffer != nullptr && bytes > 0 && surface->w == mode.width && surface->h == mode.height && surface->format->format == format && surface->format->BytesPerPixel == bytes && surface->pitch >= mode.width * bytes) { const auto area = ui::framebuffer_repaint_rect({logicalDamage.x, logicalDamage.y, logicalDamage.w, logicalDamage.h}, logicalWidth, mode.width, mode.height); - const bool locked = SDL_MUSTLOCK(surface) != 0; - if (locked && SDL_LockSurface(surface) != 0) { - SDL_RenderPresent(renderer); + if (copy_folder_framebuffer(*surface, static_cast(framebuffer), mode, area, bytes)) { return; } - for (int y = area.y; y < area.y + area.h; ++y) { - const auto *source = static_cast(surface->pixels) + static_cast(y) * surface->pitch + area.x * bytes; - auto *destination = static_cast(framebuffer) + (static_cast(y) * mode.width + area.x) * bytes; - std::memcpy(destination, source, static_cast(area.w) * bytes); - } - if (locked) { - SDL_UnlockSurface(surface); - } - XVideoFlushFB(); - return; } } #else @@ -4158,13 +4233,11 @@ namespace { if (file != nullptr) { std::fclose(file); } - const auto image = read ? ui::screenshot_bitmap_size({header.data(), header.size()}, size) : ui::ScreenshotImageSize {}; - if (image.width == 0) { + if (const auto image = read ? ui::screenshot_bitmap_size({header.data(), header.size()}, size) : ui::ScreenshotImageSize {}; image.width == 0) { cache.error = "Cannot open this screenshot. The bitmap is damaged or unsupported."; return; } - SDL_Surface *surface = SDL_LoadBMP(path.c_str()); - if (surface != nullptr) { + if (SDL_Surface *surface = SDL_LoadBMP(path.c_str()); surface != nullptr) { cache.texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); } @@ -4206,8 +4279,7 @@ namespace { return false; } } - const std::string details = !view.screenshot.status.empty() && !view.screenshot.path.empty() ? view.screenshot.status : view.screenshot.path; - if (!render_text_line_simple(renderer, smallFont, details, muted, bounds.x, picture.y + picture.h + 6, bounds.w) || !render_footer_actions(renderer, smallFont, assets, view.modal.footerActions, {bounds.x, bounds.y + bounds.h - footer, bounds.w, footer})) { + if (const std::string details = !view.screenshot.status.empty() && !view.screenshot.path.empty() ? view.screenshot.status : view.screenshot.path; !render_text_line_simple(renderer, smallFont, details, muted, bounds.x, picture.y + picture.h + 6, bounds.w) || !render_footer_actions(renderer, smallFont, assets, view.modal.footerActions, {bounds.x, bounds.y + bounds.h - footer, bounds.w, footer})) { return false; } SDL_RenderPresent(renderer); @@ -4254,22 +4326,15 @@ namespace { auto *folder = keypadModalLayoutCache != nullptr ? &keypadModalLayoutCache->folder : nullptr; const bool folderBrowser = viewModel.modal.visible && !viewModel.modal.busy && !viewModel.modal.actions.empty() && !viewModel.logViewer.visible && !viewModel.screenshot.visible && !viewModel.overlay.visible && !viewModel.keypad.visible; if (folder != nullptr) { - const bool reusable = folderBrowser && folder->valid && folder->logicalWidth == screenWidth && folder->framebufferWidth == framebufferWidth && folder->framebufferHeight == framebufferHeight && folder->titleFont == titleFont && folder->bodyFont == bodyFont && folder->smallFont == smallFont && ui::same_action_dialog_content(folder->content, viewModel.modal); - if (reusable) { - std::size_t selected = 0; - for (std::size_t i = 0; i < viewModel.modal.actions.size(); ++i) { - if (viewModel.modal.actions[i].selected) { - selected = i; - break; - } - } + if (const bool reusable = folderBrowser && folder->valid && folder->logicalWidth == screenWidth && folder->framebufferWidth == framebufferWidth && folder->framebufferHeight == framebufferHeight && folder->titleFont == titleFont && folder->bodyFont == bodyFont && folder->smallFont == smallFont && ui::same_action_dialog_content(folder->content, viewModel.modal); reusable) { + const auto selected = selected_action_index(viewModel.modal.actions); if (selected == folder->selected) { return true; } SDL_RenderSetScale(renderer, horizontalScale, 1.0f); const auto oldFirst = folder->first; const auto oldSelected = folder->selected; - const bool painted = render_action_rows(renderer, bodyFont, viewModel.modal.actions, folder->rows, folder->rowHeight, assetCache, true, folder, true); + const bool painted = render_action_rows(renderer, bodyFont, viewModel.modal.actions, folder->rows, folder->rowHeight, {assetCache, true, folder, true}); SDL_Rect damage = folder->rows; if (oldFirst == folder->first) { const auto top = std::min(oldSelected, selected) - folder->first; @@ -4368,15 +4433,7 @@ namespace { return false; } - if (!render_action_rows( - renderer, - bodyFont, - viewModel.content.menuRows, - {menuPanel.x + panelPadding, menuHeaderRect.y + menuHeaderRect.h + 12, menuPanel.w - (panelPadding * 2), menuPanel.h - (menuHeaderRect.h + (panelPadding * 2) + 12)}, - std::max(36, screenHeight / 13), - assetCache, - viewModel.content.leftPanelActive - )) { + if (!render_action_rows(renderer, bodyFont, viewModel.content.menuRows, {menuPanel.x + panelPadding, menuHeaderRect.y + menuHeaderRect.h + 12, menuPanel.w - (panelPadding * 2), menuPanel.h - (menuHeaderRect.h + (panelPadding * 2) + 12)}, std::max(36, screenHeight / 13), {assetCache, viewModel.content.leftPanelActive})) { return false; } @@ -4450,10 +4507,8 @@ namespace { render_text_line_simple(renderer, smallFont, "The full log viewer could not be rendered safely.", {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 54, modalRect.w - 32); render_text_line_simple(renderer, smallFont, (viewModel.modal.footerActions.empty() || viewModel.modal.footerActions.back().keyLabel.empty() ? "Press B to close." : "Press Esc to close."), {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, modalRect.x + 16, modalRect.y + 54 + TTF_FontLineSkip(smallFont) + 8, modalRect.w - 32); } - } else if (viewModel.modal.visible) { - if (!render_shell_dialog(renderer, titleFont, bodyFont, smallFont, assetCache, viewModel.modal, screenWidth, screenHeight, folderBrowser ? folder : nullptr)) { - return false; - } + } else if (viewModel.modal.visible && !render_shell_dialog(renderer, {titleFont, bodyFont, smallFont}, assetCache, viewModel.modal, screenWidth, screenHeight, folderBrowser ? folder : nullptr)) { + return false; } if (viewModel.keypad.visible && !render_keypad_dialog(renderer, bodyFont, smallFont, assetCache, viewModel, screenWidth, screenHeight, keypadModalLayoutCache)) { return false; @@ -4702,6 +4757,35 @@ namespace { return command; } + /** + * @brief Type printable physical keyboard input into a folder-name draft. + * @param state Active folder workflow. + * @param event Physical keyboard event. + * @return True when the folder-name keyboard consumes this key. + */ + bool handle_directory_key(app::ClientState &state, const SDL_KeyboardEvent &event) { + if (state.modal.id != app::ModalId::directory_picker || !state.directoryPicker.creating || event.keysym.sym < 32 || event.keysym.sym >= 127 || (event.keysym.mod & (KMOD_CTRL | KMOD_ALT | KMOD_GUI)) != 0) { + return false; + } + const int code = input::keyboard_virtual_key(event.keysym.scancode); + if (code == 0x20) { + app::append_directory_name(state, " "); + return true; + } + for (std::size_t i = 0; i < 60; ++i) { + const auto &key = input::StreamMenu::KEYS[i]; + if (key.code != code) { + continue; + } + const bool shifted = ((event.keysym.mod & KMOD_SHIFT) != 0) != (code >= 'A' && code <= 'Z' && (event.keysym.mod & KMOD_CAPS) != 0); + if (const std::string text = shifted ? key.shifted : key.label; text.size() == 1) { + app::append_directory_name(state, text); + } + break; + } + return true; + } + /** * @brief Route device transitions and maintain the input source used by contextual helpers. * @param state Reducer state and current input device. @@ -4749,24 +4833,7 @@ namespace { state.shell.shouldExit = true; return input::UiCommand::none; } - if (state.modal.id == app::ModalId::directory_picker && state.directoryPicker.creating && event.key.keysym.sym >= 32 && event.key.keysym.sym < 127 && (event.key.keysym.mod & (KMOD_CTRL | KMOD_ALT | KMOD_GUI)) == 0) { - const int code = input::keyboard_virtual_key(event.key.keysym.scancode); - if (code == 0x20) { - app::append_directory_name(state, " "); - } else { - for (std::size_t i = 0; i < 60; ++i) { - const auto &key = input::StreamMenu::KEYS[i]; - if (key.code != code) { - continue; - } - const bool shifted = ((event.key.keysym.mod & KMOD_SHIFT) != 0) != (code >= 'A' && code <= 'Z' && (event.key.keysym.mod & KMOD_CAPS) != 0); - const std::string text = shifted ? key.shifted : key.label; - if (text.size() == 1) { - app::append_directory_name(state, text); - } - break; - } - } + if (handle_directory_key(state, event.key)) { return input::UiCommand::none; } return inputState->keyboardNavigation.press(event.key.keysym.scancode, translate_keyboard_key(event.key.keysym.sym, event.key.keysym.mod), SDL_GetTicks()); @@ -4874,11 +4941,10 @@ namespace { */ int run_app_quit_task(void *context) { // NOSONAR(cpp:S5008) SDL thread callback ABI auto &task = *static_cast(context); - network::PairingIdentity identity {}; - if (load_saved_pairing_identity_for_streaming(&identity, &task.message)) { + if (network::PairingIdentity identity {}; load_saved_pairing_identity_for_streaming(&identity, &task.message)) { task.success = network::quit_host_app(task.target.address, task.target.port, identity, task.target.appId, &task.message); } - task.completed.store(true, std::memory_order_release); + task.completed.store(true); return 0; } @@ -4894,7 +4960,7 @@ namespace { task.target = state.appQuit; task.success = false; task.message.clear(); - task.completed.store(false, std::memory_order_relaxed); + task.completed.store(false); task.thread = SDL_CreateThread(run_app_quit_task, "quit-host-app", &task); if (task.thread == nullptr) { app::apply_app_quit_result(state, false, std::string("Could not start quit request: ") + SDL_GetError()); @@ -4907,7 +4973,7 @@ namespace { * @param task Runtime-owned quit worker. */ void finish_app_quit_task(app::ClientState &state, AppQuitTaskState &task) { - if (task.thread == nullptr || !task.completed.load(std::memory_order_acquire)) { + if (task.thread == nullptr || !task.completed.load()) { return; } SDL_WaitThread(task.thread, nullptr); @@ -5159,7 +5225,7 @@ namespace { } const auto ready = [](const auto &task) { - return task.thread != nullptr && task.completed.load(std::memory_order_acquire); + return task.thread != nullptr && task.completed.load(); }; bool changed = ready(runtime->connectionTestTask) || ready(runtime->appListTask) || ready(runtime->appArtTask) || ready(runtime->hostDiscoveryTask) || ready(runtime->appQuitTask); if (runtime->pairingTask.activeAttempt != nullptr) { diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index ac633b9..0c903a6 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -667,7 +667,7 @@ namespace { if (!picker.error.empty()) { view.lines.push_back(picker.error); } else if (!picker.path.empty() && picker.folders.empty()) { - view.lines.push_back("No subfolders here."); + view.lines.emplace_back("No subfolders here."); } const bool canUse = picker.available && !picker.path.empty(); view.actions = { @@ -831,7 +831,7 @@ namespace { * @param icon Controller icon path. * @return Keyboard keycap label, or an empty string for a non-controller icon. */ - std::string keyboard_hint_for_icon(const std::string &icon) { + std::string keyboard_hint_for_icon(std::string_view icon) { if (icon == "icons\\button-a.svg" || icon == "icons\\button-start.svg") { return "Enter"; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 66d37f3..541f231 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -45,6 +45,7 @@ file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/tests/*.cpp) list(FILTER TEST_SOURCES EXCLUDE REGEX "/tests/gpu/") list(FILTER TEST_SOURCES EXCLUDE REGEX "/tests/controller/") +list(FILTER TEST_SOURCES EXCLUDE REGEX "/tests/xbox/") add_executable(${PROJECT_NAME} ${TEST_SOURCES} @@ -132,3 +133,16 @@ target_link_options(test_shell_controller PRIVATE ${TEST_COVERAGE_LINK_OPTIONS}) gtest_discover_tests(test_shell_controller DISCOVERY_MODE PRE_TEST PROPERTIES ${TEST_RUNTIME_PROPERTIES}) + +# Exercise Xbox UTC conversion with deterministic kernel responses on the host. +add_executable(test_xbox_time "${CMAKE_SOURCE_DIR}/tests/xbox/time_test.cpp") +target_compile_features(test_xbox_time PRIVATE cxx_std_17) +target_include_directories(test_xbox_time PRIVATE + "${CMAKE_SOURCE_DIR}/tests/support/xbox_time" + "${CMAKE_SOURCE_DIR}") +target_link_libraries(test_xbox_time PRIVATE gtest_main lizardbyte::test_support) +target_compile_options(test_xbox_time PRIVATE ${TEST_COVERAGE_COMPILE_OPTIONS}) +target_link_options(test_xbox_time PRIVATE ${TEST_COVERAGE_LINK_OPTIONS}) +gtest_discover_tests(test_xbox_time + DISCOVERY_MODE PRE_TEST + PROPERTIES ${TEST_RUNTIME_PROPERTIES}) diff --git a/tests/controller/shell_controller_test.cpp b/tests/controller/shell_controller_test.cpp index 2cb8018..6c3014e 100644 --- a/tests/controller/shell_controller_test.cpp +++ b/tests/controller/shell_controller_test.cpp @@ -34,10 +34,24 @@ namespace { int closes = 0; ///< Close calls, including detached candidates. }; - std::array devices; ///< Devices survive removal until all open references are closed. - std::array handles; ///< Stable objects returned by successful opens. - std::vector available; ///< Current enumeration order; removal changes indices, not identities. - int enumerationCalls = 0; ///< Detect unnecessary rescans while the active pad remains attached. + /** + * @brief Mutable fake SDL data owned by the controller-test registry. + */ + struct ControllerRegistry { + std::array devices; ///< Devices survive removal until all open references are closed. + std::array handles; ///< Stable objects returned by successful opens. + std::vector available; ///< Current enumeration order; removal changes indices, not identities. + int enumerationCalls = 0; ///< Detect unnecessary rescans while the active pad remains attached. + }; + + /** + * @brief Access the registry shared with the C callback adapter. + * @return Stable test registry, reset before every scenario. + */ + ControllerRegistry ®istry() { + static ControllerRegistry state; + return state; + } /** * @brief Own an isolated SDL registry for one controller recovery scenario. @@ -49,45 +63,45 @@ namespace { */ void SetUp() override { BaseTest::SetUp(); - devices = {}; - handles = {}; - available.clear(); - enumerationCalls = 0; + registry().devices = {}; + registry().handles = {}; + registry().available.clear(); + registry().enumerationCalls = 0; } /** * @brief Add a device without delivering a device-added notification to the recovery code. * @param id Stable registry identity. */ - void attach(int id) { - handles[id].id = id; - available.push_back(id); + void attach(int id) const { + registry().handles[id].id = id; + registry().available.push_back(id); } /** * @brief Remove a device from enumeration while retaining its outstanding SDL handle. * @param id Stable registry identity. */ - void detach(int id) { - devices[id].attached = false; - available.erase(std::remove(available.begin(), available.end(), id), available.end()); + void detach(int id) const { + registry().devices[id].attached = false; + registry().available.erase(std::remove(registry().available.begin(), registry().available.end(), id), registry().available.end()); } }; } // namespace extern "C" { int SDL_NumJoysticks() { - ++enumerationCalls; - return static_cast(available.size()); + ++registry().enumerationCalls; + return static_cast(registry().available.size()); } SDL_bool SDL_IsGameController(int index) { - return devices[available[index]].mapped ? SDL_TRUE : SDL_FALSE; + return registry().devices[registry().available[index]].mapped ? SDL_TRUE : SDL_FALSE; } SDL_GameController *SDL_GameControllerOpen(int index) { - const int id = available[index]; - auto &device = devices[id]; + const int id = registry().available[index]; + auto &device = registry().devices[id]; if (!device.openable) { return nullptr; } @@ -96,18 +110,18 @@ extern "C" { if (device.removedWhileOpening) { device.attached = false; } - return &handles[id]; + return ®istry().handles[id]; } - void SDL_GameControllerClose(SDL_GameController *controller) { - auto &device = devices[controller->id]; + void SDL_GameControllerClose(const SDL_GameController *controller) { + auto &device = registry().devices[controller->id]; EXPECT_GT(device.references, 0); --device.references; ++device.closes; } - SDL_bool SDL_GameControllerGetAttached(SDL_GameController *controller) { - return devices[controller->id].attached ? SDL_TRUE : SDL_FALSE; + SDL_bool SDL_GameControllerGetAttached(const SDL_GameController *controller) { + return registry().devices[controller->id].attached ? SDL_TRUE : SDL_FALSE; } } @@ -119,13 +133,13 @@ namespace { attach(0); auto *active = SDL_GameControllerOpen(0); attach(1); - available = {1, 0}; + registry().available = {1, 0}; EXPECT_FALSE(input::recover_shell_controller(&active)); - EXPECT_EQ(active, &handles[0]); - EXPECT_EQ(devices[0].references, 1); - EXPECT_EQ(devices[0].closes, 0); - EXPECT_EQ(devices[1].opens, 0); - EXPECT_EQ(enumerationCalls, 0); + EXPECT_EQ(active, ®istry().handles[0]); + EXPECT_EQ(registry().devices[0].references, 1); + EXPECT_EQ(registry().devices[0].closes, 0); + EXPECT_EQ(registry().devices[1].opens, 0); + EXPECT_EQ(registry().enumerationCalls, 0); SDL_GameControllerClose(active); } @@ -138,12 +152,12 @@ namespace { auto *active = SDL_GameControllerOpen(0); detach(0); EXPECT_TRUE(input::recover_shell_controller(&active)); - EXPECT_EQ(active, &handles[1]); - EXPECT_EQ(devices[0].closes, 1); - EXPECT_EQ(devices[0].references, 0); - EXPECT_EQ(devices[1].references, 1); + EXPECT_EQ(active, ®istry().handles[1]); + EXPECT_EQ(registry().devices[0].closes, 1); + EXPECT_EQ(registry().devices[0].references, 0); + EXPECT_EQ(registry().devices[1].references, 1); EXPECT_FALSE(input::recover_shell_controller(&active)); - EXPECT_EQ(devices[1].opens, 1); + EXPECT_EQ(registry().devices[1].opens, 1); SDL_GameControllerClose(active); } @@ -158,8 +172,8 @@ namespace { SDL_GameControllerClose(active); active = nullptr; EXPECT_TRUE(input::recover_shell_controller(&active)); - EXPECT_EQ(active, &handles[1]); - EXPECT_EQ(devices[0].closes, 1); + EXPECT_EQ(active, ®istry().handles[1]); + EXPECT_EQ(registry().devices[0].closes, 1); SDL_GameControllerClose(active); } @@ -175,8 +189,8 @@ namespace { EXPECT_FALSE(input::recover_shell_controller(&active)); attach(1); EXPECT_TRUE(input::recover_shell_controller(&active)); - EXPECT_EQ(active, &handles[1]); - EXPECT_EQ(devices[0].closes, 1); + EXPECT_EQ(active, ®istry().handles[1]); + EXPECT_EQ(registry().devices[0].closes, 1); SDL_GameControllerClose(active); } @@ -187,16 +201,16 @@ namespace { for (int id = 0; id < 5; ++id) { attach(id); } - devices[0].mapped = false; - devices[1].openable = false; - devices[2].removedWhileOpening = true; + registry().devices[0].mapped = false; + registry().devices[1].openable = false; + registry().devices[2].removedWhileOpening = true; SDL_GameController *active = nullptr; EXPECT_TRUE(input::recover_shell_controller(&active)); - EXPECT_EQ(active, &handles[3]); - EXPECT_EQ(devices[0].opens + devices[1].opens, 0); - EXPECT_EQ(devices[2].closes, 1); - EXPECT_EQ(devices[2].references, 0); - EXPECT_EQ(devices[4].opens, 0); + EXPECT_EQ(active, ®istry().handles[3]); + EXPECT_EQ(registry().devices[0].opens + registry().devices[1].opens, 0); + EXPECT_EQ(registry().devices[2].closes, 1); + EXPECT_EQ(registry().devices[2].references, 0); + EXPECT_EQ(registry().devices[4].opens, 0); SDL_GameControllerClose(active); } @@ -205,15 +219,15 @@ namespace { */ TEST_F(ShellControllerTest, HandlesAbsentDevicesAndNullOwnershipSlots) { EXPECT_FALSE(input::recover_shell_controller(nullptr)); - EXPECT_EQ(enumerationCalls, 0); + EXPECT_EQ(registry().enumerationCalls, 0); SDL_GameController *active = nullptr; EXPECT_FALSE(input::recover_shell_controller(&active)); EXPECT_EQ(active, nullptr); attach(0); - devices[0].removedWhileOpening = true; + registry().devices[0].removedWhileOpening = true; EXPECT_FALSE(input::recover_shell_controller(&active)); EXPECT_EQ(active, nullptr); - EXPECT_EQ(devices[0].references, 0); - EXPECT_EQ(devices[0].closes, 1); + EXPECT_EQ(registry().devices[0].references, 0); + EXPECT_EQ(registry().devices[0].closes, 1); } } // namespace diff --git a/tests/support/shell_controller/SDL.h b/tests/support/shell_controller/SDL.h index f80bc83..4dbe108 100644 --- a/tests/support/shell_controller/SDL.h +++ b/tests/support/shell_controller/SDL.h @@ -7,15 +7,22 @@ /** * @brief SDL's boolean values used by the production recovery adapter. */ -enum SDL_bool { +enum class SDL_bool { SDL_FALSE = 0, ///< No or detached. SDL_TRUE = 1, ///< Yes or attached. }; +constexpr auto SDL_FALSE = SDL_bool::SDL_FALSE; ///< SDL-compatible false value. +constexpr auto SDL_TRUE = SDL_bool::SDL_TRUE; ///< SDL-compatible true value. + /** * @brief Opaque SDL controller handle implemented by the test device registry. */ -typedef struct _SDL_GameController SDL_GameController; +struct _SDL_GameController; +/** + * @brief SDL-compatible public controller handle name. + */ +using SDL_GameController = _SDL_GameController; extern "C" { /** @@ -39,11 +46,11 @@ extern "C" { * @brief Release one owned gamepad reference. * @param controller Owned handle being closed. */ - void SDL_GameControllerClose(SDL_GameController *controller); + void SDL_GameControllerClose(const SDL_GameController *controller); /** * @brief Check whether an open gamepad still has a connected device. * @param controller Open controller reference. * @return True while the device is attached. */ - SDL_bool SDL_GameControllerGetAttached(SDL_GameController *controller); + SDL_bool SDL_GameControllerGetAttached(const SDL_GameController *controller); } diff --git a/tests/support/xbox_time/xboxkrnl/xboxkrnl.h b/tests/support/xbox_time/xboxkrnl/xboxkrnl.h new file mode 100644 index 0000000..8ec036f --- /dev/null +++ b/tests/support/xbox_time/xboxkrnl/xboxkrnl.h @@ -0,0 +1,36 @@ +/** + * @file tests/support/xbox_time/xboxkrnl/xboxkrnl.h + * @brief Minimal kernel time types for native tests of the Xbox UTC adapter. + */ +#pragma once + +// standard includes +#include + +/** + * @brief Signed NT timestamp received by the kernel conversion. + */ +struct LARGE_INTEGER { + std::int64_t QuadPart = 0; ///< Ticks since the NT epoch. +}; + +/** + * @brief Calendar fields returned by the kernel conversion. + */ +struct TIME_FIELDS { + short Year = 1970; ///< Calendar year. + short Month = 1; ///< One-based month. + short Day = 1; ///< One-based day of month. + short Hour = 0; ///< Hour within the day. + short Minute = 0; ///< Minute within the hour. + short Second = 0; ///< Second within the minute. + short Milliseconds = 0; ///< Fractional seconds unused by the adapter. + short Weekday = 4; ///< Day of week, with Sunday as zero. +}; + +/** + * @brief Return deterministic calendar fields and record the supplied timestamp. + * @param time Signed NT timestamp. + * @param fields Receives the scripted calendar fields. + */ +void RtlTimeToTimeFields(const LARGE_INTEGER *time, TIME_FIELDS *fields); diff --git a/tests/unit/app/directory_picker_test.cpp b/tests/unit/app/directory_picker_test.cpp index 3815947..7bfd044 100644 --- a/tests/unit/app/directory_picker_test.cpp +++ b/tests/unit/app/directory_picker_test.cpp @@ -7,6 +7,7 @@ // standard includes #include +#include // lib includes #include @@ -21,9 +22,9 @@ namespace { * @param configured Existing screenshot destination. * @return State with the folder picker open and a pending directory read. */ - app::ClientState open_picker(const std::string &configured = {}) { + app::ClientState open_picker(std::string_view configured = {}) { auto state = app::create_initial_state(); - state.settings.screenshotDirectory = configured; + state.settings.controls.screenshotDirectory = configured; app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::move_left); app::handle_command(state, input::UiCommand::activate); @@ -67,7 +68,7 @@ TEST(DirectoryPickerTest, StagesConfiguredAndDefaultLocationsUntilAccepted) { EXPECT_FALSE(view.modal.actions[0].enabled); const auto update = app::handle_command(state, input::UiCommand::back); EXPECT_TRUE(update.navigation.modalClosed); - EXPECT_EQ(state.settings.screenshotDirectory, configured); + EXPECT_EQ(state.settings.controls.screenshotDirectory, configured); EXPECT_TRUE(state.directoryPicker.folders.empty()); EXPECT_FALSE(update.persistence.settingsChanged); } @@ -89,7 +90,7 @@ TEST(DirectoryPickerTest, BrowsesChildrenParentsAndDrives) { EXPECT_EQ(state.directoryPicker.path, "F:\\"); app::handle_command(state, input::UiCommand::previous_page); EXPECT_TRUE(state.directoryPicker.path.empty()); - EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Shots"); + EXPECT_EQ(state.settings.controls.screenshotDirectory, "F:\\Shots"); state.modal.selectedActionIndex = 0; EXPECT_FALSE(app::handle_command(state, input::UiCommand::activate).requests.directorySelectRequested); state.modal.selectedActionIndex = 1; @@ -108,9 +109,9 @@ TEST(DirectoryPickerTest, AcceptsValidatedSelectionAndRestoresTheDefault) { EXPECT_TRUE(app::handle_command(state, input::UiCommand::open_context_menu).requests.directorySelectRequested); auto update = app::handle_command(state, input::UiCommand::activate); EXPECT_TRUE(update.requests.directorySelectRequested); - EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Old"); + EXPECT_EQ(state.settings.controls.screenshotDirectory, "F:\\Old"); app::accept_screenshot_directory(state, update, state.directoryPicker.path); - EXPECT_EQ(state.settings.screenshotDirectory, "E:\\Screenshots"); + EXPECT_EQ(state.settings.controls.screenshotDirectory, "E:\\Screenshots"); EXPECT_TRUE(state.settings.dirty); EXPECT_TRUE(update.persistence.settingsChanged); EXPECT_TRUE(update.navigation.modalClosed); @@ -119,13 +120,13 @@ TEST(DirectoryPickerTest, AcceptsValidatedSelectionAndRestoresTheDefault) { app::handle_command(state, input::UiCommand::activate); state.modal.selectedActionIndex = 3; update = app::handle_command(state, input::UiCommand::confirm); - EXPECT_TRUE(state.settings.screenshotDirectory.empty()); + EXPECT_TRUE(state.settings.controls.screenshotDirectory.empty()); EXPECT_TRUE(update.persistence.settingsChanged); app::handle_command(state, input::UiCommand::activate); state.modal.selectedActionIndex = 3; EXPECT_FALSE(app::handle_command(state, input::UiCommand::activate).persistence.settingsChanged); app::accept_screenshot_directory(state, update, "ignored"); - EXPECT_TRUE(state.settings.screenshotDirectory.empty()); + EXPECT_TRUE(state.settings.controls.screenshotDirectory.empty()); } /** @@ -172,7 +173,7 @@ TEST(DirectoryPickerTest, TypesLocallyWithTheSharedKeyboardAndCaret) { app::handle_command(state, input::UiCommand::move_left); picker.keyboard.keyIndex = 46; EXPECT_TRUE(app::handle_command(state, input::UiCommand::activate).requests.directoryCreateRequested); - EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Shots"); + EXPECT_EQ(state.settings.controls.screenshotDirectory, "F:\\Shots"); EXPECT_FALSE(state.settings.dirty); } @@ -259,5 +260,5 @@ TEST(DirectoryPickerTest, KeepsStorageSeparateFromTheExistingDisplayCategory) { EXPECT_EQ(state.detailMenu.items().size(), 6); EXPECT_EQ(state.detailMenu.selected_item()->id, "cycle-stream-video-mode"); EXPECT_FALSE(state.detailMenu.select_item_by_id("screenshot-directory")); - EXPECT_EQ(state.settings.screenshotDirectory, "F:\\Screenshots"); + EXPECT_EQ(state.settings.controls.screenshotDirectory, "F:\\Screenshots"); } diff --git a/tests/unit/app/screenshot_library_test.cpp b/tests/unit/app/screenshot_library_test.cpp index a370dd0..18f1748 100644 --- a/tests/unit/app/screenshot_library_test.cpp +++ b/tests/unit/app/screenshot_library_test.cpp @@ -293,12 +293,12 @@ namespace { */ TEST_F(ScreenshotLibraryTest, ReportsDeletionFailuresWithoutLosingOtherCaptures) { std::string error; - const auto first = app::next_screenshot_path(root, 10, error); + const std::filesystem::path first = app::next_screenshot_path(root, 10, error); { std::ofstream image(first); image << "capture"; } - const app::ScreenshotRecord record {source, first, 10}; + const app::ScreenshotRecord record {source, first.string(), 10}; ASSERT_TRUE(app::append_screenshot(record, journal, error)); EXPECT_FALSE(app::delete_screenshot({}, journal, error)); EXPECT_FALSE(error.empty()); diff --git a/tests/unit/app/settings_storage_test.cpp b/tests/unit/app/settings_storage_test.cpp index b3880ce..bf8fd24 100644 --- a/tests/unit/app/settings_storage_test.cpp +++ b/tests/unit/app/settings_storage_test.cpp @@ -253,7 +253,7 @@ namespace { */ TEST_F(SettingsStorageTest, PreservesScreenshotDirectoryAndKeyboardDocking) { app::AppSettings settings; - settings.screenshotDirectory = "F:\\Screenshots\\Moonlight \"test\""; + settings.screenshotDirectory = R"(F:\Screenshots\Moonlight "test")"; settings.keyboardDockTop = true; ASSERT_TRUE(app::save_app_settings(settings, settingsPath).success); const auto loaded = app::load_app_settings(settingsPath); diff --git a/tests/unit/input/stream_input_test.cpp b/tests/unit/input/stream_input_test.cpp index db88e91..e9dc844 100644 --- a/tests/unit/input/stream_input_test.cpp +++ b/tests/unit/input/stream_input_test.cpp @@ -18,7 +18,7 @@ * @brief Captures ordered transport packets without a host connection. */ class StreamInputTest: public testing::Test { -protected: +public: std::vector packets; ///< Packets emitted by the router. input::StreamInput router {[this](const input::StreamPacket &packet) { packets.push_back(packet); @@ -307,7 +307,7 @@ TEST_F(StreamInputTest, BoundsMouseReportRateWithoutDelayingClicks) { TEST(StreamInputRateTest, SustainsMouseMovementWithBoundedTraffic) { int reports = 0; int distance = 0; - input::StreamInput router([&](const input::StreamPacket &packet) { + input::StreamInput router([&reports, &distance](const input::StreamPacket &packet) { if (packet.type == input::StreamPacketType::mouse_move) { ++reports; distance += packet.x; diff --git a/tests/unit/input/stream_menu_test.cpp b/tests/unit/input/stream_menu_test.cpp index efdc43f..11afc08 100644 --- a/tests/unit/input/stream_menu_test.cpp +++ b/tests/unit/input/stream_menu_test.cpp @@ -99,6 +99,21 @@ TEST(StreamMenuTest, NavigatesWideAndTallKeyboardCells) { EXPECT_EQ(menu.keyboard_key(), '0'); } +namespace { + /** + * @brief Count the grid units occupied by a single OSK key. + * @param coverage Coverage counts for the complete keyboard grid. + * @param cell Key geometry to include. + */ + void cover_keyboard_cell(std::array &coverage, const input::StreamKeyboardCell &cell) { + for (int y = cell.row; y < cell.row + cell.height; ++y) { + for (int x = cell.column; x < cell.column + cell.width; ++x) { + ++coverage.at(y * input::StreamMenu::KEYBOARD_COLUMNS + x); + } + } + } +} // namespace + /** * @brief Every host key remains accessible, with complete geometry on all three layouts. */ @@ -114,11 +129,7 @@ TEST(StreamMenuTest, PreservesHostKeysAcrossThreeLayouts) { if (cell.key < static_cast(input::StreamMenu::KEYS.size())) { codes.insert(input::StreamMenu::KEYS[cell.key].code); } - for (int y = cell.row; y < cell.row + cell.height; ++y) { - for (int x = cell.column; x < cell.column + cell.width; ++x) { - ++coverage.at(y * input::StreamMenu::KEYBOARD_COLUMNS + x); - } - } + cover_keyboard_cell(coverage, cell); } for (int count : coverage) { EXPECT_EQ(count, 1); diff --git a/tests/unit/input/ui_input_test.cpp b/tests/unit/input/ui_input_test.cpp index b997cc9..9035492 100644 --- a/tests/unit/input/ui_input_test.cpp +++ b/tests/unit/input/ui_input_test.cpp @@ -6,13 +6,31 @@ #include "src/input/ui_input.h" // standard includes +#include #include #include +#include #include // lib includes #include +/** + * @brief Shared grid commands preserve direction, horizontal priority and neutral input. + */ +TEST(UiInputTest, MapsDirectionalCommandsToGridSteps) { + const std::array commands {input::UiCommand::move_left, input::UiCommand::move_right, input::UiCommand::move_up, input::UiCommand::move_down}; + for (const auto command : commands) { + const auto [x, y] = input::navigation_step(command); + EXPECT_EQ(input::direction_command(x, y), command); + } + EXPECT_EQ(input::direction_command(2, -3), input::UiCommand::move_right); + EXPECT_EQ(input::direction_command(-2, 3), input::UiCommand::move_left); + EXPECT_EQ(input::direction_command(0, 0), input::UiCommand::none); + EXPECT_EQ(input::navigation_step(input::UiCommand::none), (std::pair {0, 0})); + EXPECT_EQ(input::navigation_step(input::UiCommand::activate), (std::pair {0, 0})); +} + /** * @brief Keyboard navigation uses the same initial delay and interval as controller navigation. */ @@ -110,14 +128,24 @@ TEST(ShellInputStateTest, ResumesNavigationAfterStreamingConsumesReleaseEvents) input::ShellInputState state; state.keyboardNavigation.press(1, input::UiCommand::move_down, 100); state.keyboardNavigation.press(2, input::UiCommand::confirm, 110); - state.leftTriggerPressed = state.rightTriggerPressed = true; - state.leftShoulderPressed = state.rightShoulderPressed = true; - state.controllerStartPressed = state.controllerBackPressed = true; - state.controllerExitComboArmed = state.controllerExitComboTriggered = true; - state.controllerStartDownTick = state.controllerBackDownTick = 120; - state.leftShoulderRepeatTick = state.rightShoulderRepeatTick = 120; - state.leftTriggerRepeatTick = state.rightTriggerRepeatTick = 120; - state.moveUpHoldState = state.moveDownHoldState = state.moveLeftHoldState = state.moveRightHoldState = {true, 100, 200}; + state.leftTriggerPressed = true; + state.rightTriggerPressed = true; + state.leftShoulderPressed = true; + state.rightShoulderPressed = true; + state.controllerStartPressed = true; + state.controllerBackPressed = true; + state.controllerExitComboArmed = true; + state.controllerExitComboTriggered = true; + state.controllerStartDownTick = 120; + state.controllerBackDownTick = 120; + state.leftShoulderRepeatTick = 120; + state.rightShoulderRepeatTick = 120; + state.leftTriggerRepeatTick = 120; + state.rightTriggerRepeatTick = 120; + state.moveUpHoldState = {true, 100, 200}; + state.moveDownHoldState = {true, 100, 200}; + state.moveLeftHoldState = {true, 100, 200}; + state.moveRightHoldState = {true, 100, 200}; state.reset_after_stream(); EXPECT_FALSE(state.leftTriggerPressed || state.rightTriggerPressed || state.leftShoulderPressed || state.rightShoulderPressed); EXPECT_FALSE(state.controllerStartPressed || state.controllerBackPressed || state.controllerExitComboArmed || state.controllerExitComboTriggered); @@ -139,10 +167,24 @@ TEST(ShellInputStateTest, ResumesNavigationAfterStreamingConsumesReleaseEvents) */ TEST(ShellInputStateTest, ControllerHandoffPreservesKeyboardAndResetsGamepadHolds) { input::ShellInputState state; - state.leftTriggerPressed = state.rightTriggerPressed = state.leftShoulderPressed = state.rightShoulderPressed = true; - state.controllerStartPressed = state.controllerBackPressed = state.controllerExitComboArmed = state.controllerExitComboTriggered = true; - state.controllerStartDownTick = state.controllerBackDownTick = state.leftShoulderRepeatTick = state.rightShoulderRepeatTick = state.leftTriggerRepeatTick = state.rightTriggerRepeatTick = 100; - state.moveUpHoldState = state.moveDownHoldState = state.moveLeftHoldState = state.moveRightHoldState = {true, 100, 200}; + state.leftTriggerPressed = true; + state.rightTriggerPressed = true; + state.leftShoulderPressed = true; + state.rightShoulderPressed = true; + state.controllerStartPressed = true; + state.controllerBackPressed = true; + state.controllerExitComboArmed = true; + state.controllerExitComboTriggered = true; + state.controllerStartDownTick = 100; + state.controllerBackDownTick = 100; + state.leftShoulderRepeatTick = 100; + state.rightShoulderRepeatTick = 100; + state.leftTriggerRepeatTick = 100; + state.rightTriggerRepeatTick = 100; + state.moveUpHoldState = {true, 100, 200}; + state.moveDownHoldState = {true, 100, 200}; + state.moveLeftHoldState = {true, 100, 200}; + state.moveRightHoldState = {true, 100, 200}; EXPECT_EQ(state.keyboardNavigation.press(1, input::UiCommand::move_right, 100), input::UiCommand::move_right); state.reset_after_controller_change(); EXPECT_FALSE(state.leftTriggerPressed || state.rightTriggerPressed || state.leftShoulderPressed || state.rightShoulderPressed); @@ -201,20 +243,18 @@ TEST(DirectionNavigationTest, ClearsOnNeutralOrResetAndHandlesClockWrap) { * @brief Both stick axes use the main menu dead zone, retain a noisy hold and release without a second tap. */ TEST(DirectionNavigationTest, StickFlicksStepOnceWithHysteresisOnBothAxes) { - for (const auto &axis : {std::pair {20000, 0}, {-20000, 0}, {0, 20000}, {0, -20000}}) { + const std::array, 4> axes {{{20000, 0, input::UiCommand::move_right}, {-20000, 0, input::UiCommand::move_left}, {0, 20000, input::UiCommand::move_up}, {0, -20000, input::UiCommand::move_down}}}; + for (const auto &[x, y, direction] : axes) { input::DirectionNavigation navigation; - const auto direction = axis.first > 0 ? input::UiCommand::move_right : axis.first < 0 ? input::UiCommand::move_left : - axis.second > 0 ? input::UiCommand::move_up : - input::UiCommand::move_down; - EXPECT_EQ(navigation.update_stick(axis.first / 2, axis.second / 2, 0), input::UiCommand::none); - EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 1), direction); - EXPECT_EQ(navigation.update_stick(axis.first * 3 / 5, axis.second * 3 / 5, 40), input::UiCommand::none); - EXPECT_EQ(navigation.update_stick(axis.first * 7 / 10, axis.second * 7 / 10, 80), input::UiCommand::none); - EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 149), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(x / 2, y / 2, 0), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(x, y, 1), direction); + EXPECT_EQ(navigation.update_stick(x * 3 / 5, y * 3 / 5, 40), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(x * 7 / 10, y * 7 / 10, 80), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(x, y, 149), input::UiCommand::none); EXPECT_EQ(navigation.update_stick(0, 0, 150), input::UiCommand::none); - EXPECT_EQ(navigation.update_stick(axis.first / 2, axis.second / 2, 151), input::UiCommand::none); - EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 152), direction); - EXPECT_EQ(navigation.update_stick(axis.first, axis.second, 302), direction); + EXPECT_EQ(navigation.update_stick(x / 2, y / 2, 151), input::UiCommand::none); + EXPECT_EQ(navigation.update_stick(x, y, 152), direction); + EXPECT_EQ(navigation.update_stick(x, y, 302), direction); } } diff --git a/tests/unit/input/usb_hid_test.cpp b/tests/unit/input/usb_hid_test.cpp index cc9f1d9..de35be8 100644 --- a/tests/unit/input/usb_hid_test.cpp +++ b/tests/unit/input/usb_hid_test.cpp @@ -16,7 +16,7 @@ * @brief Records decoded HID operations without USB hardware. */ class BootHidTest: public testing::Test { -protected: +public: std::vector events; ///< Decoded transitions. input::BootHidInput hid {[this](const input::BootHidEvent &event) { events.push_back(event); diff --git a/tests/unit/network/host_quit_test.cpp b/tests/unit/network/host_quit_test.cpp index cacefde..f2400a5 100644 --- a/tests/unit/network/host_quit_test.cpp +++ b/tests/unit/network/host_quit_test.cpp @@ -37,6 +37,45 @@ namespace { return "Test PC7.1.0.0server-one" + std::to_string(running) + "4798947990" + (paired ? "1" : "0") + ""; } + /** + * @brief Answer authenticated cancellation and server-info requests from the scripted host. + * @param request Host request under test. + * @param response Receives the scripted status and XML body. + * @param error Receives simulated transport errors when requested. + * @return True for a completed scripted response. + */ + bool handle_http_request(const network::testing::HostPairingHttpTestRequest &request, network::testing::HostPairingHttpTestResponse *response, std::string *error) { + if (request.pathAndQuery.rfind("/cancel?", 0) == 0) { + ++cancelRequests; + EXPECT_TRUE(request.useTls); + EXPECT_EQ(request.port, 47990U); + EXPECT_EQ(request.tlsClientIdentity, &identity); + EXPECT_NE(request.pathAndQuery.find("uniqueid=" + identity.uniqueId), std::string::npos); + if (cancelTransportFailure) { + if (error != nullptr) { + *error = "Cancel request timed out"; + } + return false; + } + response->statusCode = cancelStatus; + response->body = cancelBody; + if (!keepRunning && cancelStatus == 200 && cancelBody.find("status_code=\"200\"") != std::string::npos) { + running = 0; + } + return true; + } + EXPECT_EQ(request.pathAndQuery.rfind("/serverinfo?", 0), 0U); + if (serverTransportFailure || (failVerification && cancelRequests != 0)) { + if (error != nullptr) { + *error = "Serverinfo unavailable"; + } + return false; + } + response->statusCode = 200; + response->body = server_info(); + return true; + } + /** * @brief Install deterministic serverinfo and cancel responses. */ @@ -45,35 +84,7 @@ namespace { identity = network::create_pairing_identity(); ASSERT_TRUE(network::is_valid_pairing_identity(identity)); network::testing::set_host_pairing_http_test_handler([this](const auto &request, auto *response, std::string *error, const std::atomic *) { - if (request.pathAndQuery.rfind("/cancel?", 0) == 0) { - ++cancelRequests; - EXPECT_TRUE(request.useTls); - EXPECT_EQ(request.port, 47990U); - EXPECT_EQ(request.tlsClientIdentity, &identity); - EXPECT_NE(request.pathAndQuery.find("uniqueid=" + identity.uniqueId), std::string::npos); - if (cancelTransportFailure) { - if (error != nullptr) { - *error = "Cancel request timed out"; - } - return false; - } - response->statusCode = cancelStatus; - response->body = cancelBody; - if (!keepRunning && cancelStatus == 200 && cancelBody.find("status_code=\"200\"") != std::string::npos) { - running = 0; - } - return true; - } - EXPECT_EQ(request.pathAndQuery.rfind("/serverinfo?", 0), 0U); - if (serverTransportFailure || (failVerification && cancelRequests != 0)) { - if (error != nullptr) { - *error = "Serverinfo unavailable"; - } - return false; - } - response->statusCode = 200; - response->body = server_info(); - return true; + return handle_http_request(request, response, error); }); } @@ -139,7 +150,7 @@ namespace { cancelStatus = 403; EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); cancelStatus = 200; - cancelBody = ""; + cancelBody = R"()"; EXPECT_FALSE(network::quit_host_app("192.0.2.1", 47989, identity, 7, &error)); EXPECT_NE(error.find("Another client"), std::string::npos); cancelBody = "malformed response"; diff --git a/tests/unit/streaming/video_overlay_test.cpp b/tests/unit/streaming/video_overlay_test.cpp index 08ac802..c869f7a 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -149,7 +149,8 @@ TEST(VideoOverlayTest, WeightsGpuChromaByVisibleColorCoverage) { std::array frame; frame.fill(128); const int at = 8 * pitch + 8 * 2; - frame[at] = frame[at + 2] = 70; + frame[at] = 70; + frame[at + 2] = 70; ASSERT_TRUE(overlay.draw_yuy2(frame.data(), frame.size(), pitch, 24, 16, {0, 0, 24, 16})); EXPECT_EQ(frame[at], 81); EXPECT_EQ(frame[at + 1], 109); @@ -274,7 +275,8 @@ TEST(VideoOverlayTest, AlphaBlendsYuy2WithoutBakingTheVideoIntoItsCache) { for (int row = 0; row < 16; ++row) { for (int column = 0; column < 24; column += 2) { const int at = row * pitch + column * 2; - frame[at] = frame[at + 2] = 50; + frame[at] = 50; + frame[at + 2] = 50; frame[at + 1] = 90; frame[at + 3] = 170; } @@ -334,7 +336,7 @@ TEST(VideoOverlayTest, RasterizesWheelWithoutSoftwareRenderCommands) { const std::array y {5, 10, 15, 10}; for (int selected = 0; selected < 4; ++selected) { pixels.fill(0xcc); - ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), pitch, 20, 20, 10, 10, 6, selected)); + ASSERT_TRUE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), pitch, 20, 20}, {10, 10, 6, selected})); for (int sector = 0; sector < 4; ++sector) { EXPECT_EQ(pixels[y[sector] * pitch + x[sector] * 4], sector == selected ? 85 : 36); EXPECT_EQ(pixels[y[sector] * pitch + x[sector] * 4 + 3], 255); @@ -344,7 +346,7 @@ TEST(VideoOverlayTest, RasterizesWheelWithoutSoftwareRenderCommands) { EXPECT_EQ(pixels[0], 0xcc); EXPECT_EQ(pixels[10 * pitch + 20 * 4], 0xcc); } - ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), pitch, 20, 20, 0, 0, 4, -1)); + ASSERT_TRUE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), pitch, 20, 20}, {0, 0, 4, -1})); EXPECT_EQ(pixels[3 * 4], 36); } @@ -354,17 +356,17 @@ TEST(VideoOverlayTest, RasterizesWheelWithoutSoftwareRenderCommands) { TEST(VideoOverlayTest, RejectsInvalidWheelRasterStorage) { std::array pixels; pixels.fill(0xcc); - EXPECT_FALSE(streaming::rasterize_menu_wheel(nullptr, pixels.size(), 80, 20, 20, 10, 10, 6, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), 1, 80, 20, 20, 10, 10, 6, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, -1, 10, 6, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 20, 10, 6, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, -1, 6, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, 20, 6, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, 10, 0, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 80, 20, 20, 10, 10, 2049, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel({nullptr, pixels.size(), 80, 20, 20}, {10, 10, 6, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), 1, 80, 20, 20}, {10, 10, 6, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 80, 20, 20}, {-1, 10, 6, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 80, 20, 20}, {20, 10, 6, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 80, 20, 20}, {10, -1, 6, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 80, 20, 20}, {10, 20, 6, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 80, 20, 20}, {10, 10, 0, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 80, 20, 20}, {10, 10, 2049, 0})); std::array oversized {}; - EXPECT_FALSE(streaming::rasterize_menu_wheel(oversized.data(), oversized.size(), 2049 * 4, 2049, 1, 0, 0, 1, 0)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(oversized.data(), oversized.size(), 4, 1, 2049, 0, 0, 1, 0)); + EXPECT_FALSE(streaming::rasterize_menu_wheel({oversized.data(), oversized.size(), 2049 * 4, 2049, 1}, {0, 0, 1, 0})); + EXPECT_FALSE(streaming::rasterize_menu_wheel({oversized.data(), oversized.size(), 4, 1, 2049}, {0, 0, 1, 0})); EXPECT_TRUE(std::all_of(pixels.begin(), pixels.end(), [](std::uint8_t value) { return value == 0xcc; })); @@ -662,46 +664,57 @@ TEST(VideoOverlayTest, DimsTheStreamThroughBothPresentationPaths) { } } +namespace { + /** + * @brief Compare incremental and complete overlay conversion for one output scale and color matrix. + * @param scale Output size relative to the source panel. + * @param matrix Host color matrix. + */ + void compare_recolored_overlay(int scale, streaming::VideoColorMatrix matrix) { + streaming::VideoOverlay incremental; + std::vector pixels(16 * 12 * 4, 0); + for (std::size_t i = 3; i < pixels.size(); i += 4) { + pixels[i] = 150; + } + const streaming::VideoRectangle destination {0, 0, scale, scale * 3 / 4}; + for (int step = 0; step < 8; ++step) { + const auto index = static_cast((step * 7) % (16 * 12)) * 4; + pixels[index] = 200; + pixels[index + 1] = 40; + pixels[index + 2] = 70; + pixels[index + 3] = step % 2 ? 255 : 0; + incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + if (step % 2 == 0) { + const auto other = static_cast(16 * 12 - step - 1) * 4; + pixels[other + 1] = 240; + incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + } + // An identical update must preserve pending damage until presentation. + incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + streaming::VideoOverlay fresh; + fresh.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); + std::vector actual(64 * 48 * 2, 100); + auto expected = actual; + ASSERT_TRUE(incremental.draw_yuy2(actual.data(), actual.size(), 128, 64, 48, destination, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 128, 64, 48, destination, matrix)); + EXPECT_EQ(actual, expected) << "scale=" << scale << " step=" << step; + // A new host frame must still be composed with the cached UI. + actual.assign(actual.size(), 180); + expected = actual; + ASSERT_TRUE(incremental.draw_yuy2(actual.data(), actual.size(), 128, 64, 48, destination, matrix)); + ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 128, 64, 48, destination, matrix)); + EXPECT_EQ(actual, expected); + } + } +} // namespace + /** * @brief Cached UI damage produces the same colors, alpha and chroma as a fresh overlay at both scales. */ TEST(VideoOverlayTest, RecolorsChangedRegionsWithoutStaleGpuPixels) { for (const int scale : {32, 128}) { for (const auto matrix : {streaming::VideoColorMatrix::bt601, streaming::VideoColorMatrix::bt709}) { - streaming::VideoOverlay incremental; - std::vector pixels(16 * 12 * 4, 0); - for (std::size_t i = 3; i < pixels.size(); i += 4) { - pixels[i] = 150; - } - const streaming::VideoRectangle destination {0, 0, scale, scale * 3 / 4}; - for (int step = 0; step < 8; ++step) { - const auto index = static_cast((step * 7) % (16 * 12)) * 4; - pixels[index] = 200; - pixels[index + 1] = 40; - pixels[index + 2] = 70; - pixels[index + 3] = step % 2 ? 255 : 0; - incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); - if (step % 2 == 0) { - const auto other = static_cast(16 * 12 - step - 1) * 4; - pixels[other + 1] = 240; - incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); - } - // An identical update must preserve pending damage until presentation. - incremental.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); - streaming::VideoOverlay fresh; - fresh.set_argb_bitmap(16, 12, pixels, streaming::VideoOverlayPlacement::full_screen); - std::vector actual(64 * 48 * 2, 100); - auto expected = actual; - ASSERT_TRUE(incremental.draw_yuy2(actual.data(), actual.size(), 128, 64, 48, destination, matrix)); - ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 128, 64, 48, destination, matrix)); - EXPECT_EQ(actual, expected) << "scale=" << scale << " step=" << step; - // A new host frame must still be composed with the cached UI. - actual.assign(actual.size(), 180); - expected = actual; - ASSERT_TRUE(incremental.draw_yuy2(actual.data(), actual.size(), 128, 64, 48, destination, matrix)); - ASSERT_TRUE(fresh.draw_yuy2(expected.data(), expected.size(), 128, 64, 48, destination, matrix)); - EXPECT_EQ(actual, expected); - } + ASSERT_NO_FATAL_FAILURE(compare_recolored_overlay(scale, matrix)); } } } @@ -753,13 +766,12 @@ TEST(VideoOverlayTest, AppliesSmallPatchesAcrossBothPresentationPaths) { overlay.set_argb_bitmap(20, 16, pixels, streaming::VideoOverlayPlacement::full_screen); for (int step = 0; step < 6; ++step) { const streaming::VideoRectangle area {step * 3, step * 2, 3, 2}; - std::vector patch(area.width * area.height * 4, 0); + std::vector patch(static_cast(area.width) * area.height * 4, 0); for (std::size_t i = 0; i < patch.size(); i += 4) { patch[i] = step * 30; patch[i + 1] = 40; patch[i + 2] = 210; - patch[i + 3] = step % 3 == 0 ? 255 : step % 3 == 1 ? 150 : - 0; + patch[i + 3] = std::array {255, 150, 0}.at(step % 3); } for (int row = 0; row < area.height; ++row) { std::copy_n(patch.data() + row * area.width * 4, area.width * 4, pixels.data() + ((area.y + row) * 20 + area.x) * 4); @@ -820,13 +832,13 @@ TEST(VideoOverlayTest, RepaintsWheelSectorsAndClippedLabelBackgrounds) { int previous = -1; for (int selected : {0, 1, 2, 3, -1, 2, 0}) { if (previous >= 0) { - ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, selected, previous)); + ASSERT_TRUE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 160, 40, 40}, {20, 20, 18, selected}, previous)); } if (selected >= 0) { - ASSERT_TRUE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, selected, selected)); + ASSERT_TRUE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 160, 40, 40}, {20, 20, 18, selected}, selected)); } auto expected = backdrop; - ASSERT_TRUE(streaming::rasterize_menu_wheel(expected.data(), expected.size(), 160, 40, 40, 20, 20, 18, selected)); + ASSERT_TRUE(streaming::rasterize_menu_wheel({expected.data(), expected.size(), 160, 40, 40}, {20, 20, 18, selected})); // Initialize the unchanged sectors on the first frame. if (previous == -1 && selected == 0) { pixels = expected; @@ -834,7 +846,7 @@ TEST(VideoOverlayTest, RepaintsWheelSectorsAndClippedLabelBackgrounds) { EXPECT_EQ(pixels, expected); const streaming::VideoRectangle clip {17, 2, 6, 6}; auto clipped = backdrop; - ASSERT_TRUE(streaming::rasterize_menu_wheel(clipped.data(), clipped.size(), 160, 40, 40, 20, 20, 18, selected, -1, &clip)); + ASSERT_TRUE(streaming::rasterize_menu_wheel({clipped.data(), clipped.size(), 160, 40, 40}, {20, 20, 18, selected}, -1, &clip)); for (int y = 0; y < 40; ++y) { for (int x = 0; x < 40; ++x) { const int index = (y * 40 + x) * 4; @@ -843,9 +855,9 @@ TEST(VideoOverlayTest, RepaintsWheelSectorsAndClippedLabelBackgrounds) { } previous = selected; } - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, 0, -2)); - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, 0, 4)); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 160, 40, 40}, {20, 20, 18, 0}, -2)); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 160, 40, 40}, {20, 20, 18, 0}, 4)); for (const auto clip : {streaming::VideoRectangle {-1, 0, 1, 1}, {0, -1, 1, 1}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 41, 1}, {0, 0, 1, 41}, {40, 0, 1, 1}, {0, 40, 1, 1}}) { - EXPECT_FALSE(streaming::rasterize_menu_wheel(pixels.data(), pixels.size(), 160, 40, 40, 20, 20, 18, 0, -1, &clip)); + EXPECT_FALSE(streaming::rasterize_menu_wheel({pixels.data(), pixels.size(), 160, 40, 40}, {20, 20, 18, 0}, -1, &clip)); } } diff --git a/tests/xbox/time_test.cpp b/tests/xbox/time_test.cpp new file mode 100644 index 0000000..5bf9c7a --- /dev/null +++ b/tests/xbox/time_test.cpp @@ -0,0 +1,130 @@ +/** + * @file tests/xbox/time_test.cpp + * @brief Verifies Xbox UTC epoch conversion, calendar fields and overflow rejection. + */ +// test header include +#include "src/_nxdk_compat/include/moonlight_time.h" + +// standard includes +#include +#include + +// lib includes +#include + +namespace { + /** + * @brief Scripted kernel reply and observed timestamp for one conversion scenario. + */ + struct KernelTimeState { + TIME_FIELDS reply; ///< Calendar fields supplied by the kernel stub. + std::int64_t ticks = 0; ///< Most recently supplied NT timestamp. + int calls = 0; ///< Number of kernel conversions requested. + }; + + /** + * @brief Access the state shared with the kernel test callback. + * @return Registry reset before each scenario. + */ + KernelTimeState &kernel_time() { + static KernelTimeState state; + return state; + } + + /** + * @brief Reset kernel responses and observation state for each UTC conversion test. + */ + class XboxTimeTest: public BaseTest { + protected: + /** + * @brief Start each test with the Unix epoch as its kernel reply. + */ + void SetUp() override { + BaseTest::SetUp(); + kernel_time() = {}; + } + }; +} // namespace + +void RtlTimeToTimeFields(const LARGE_INTEGER *time, TIME_FIELDS *fields) { + auto &state = kernel_time(); + ++state.calls; + state.ticks = time->QuadPart; + *fields = state.reply; +} + +/** + * @brief The Unix epoch reaches the kernel as NT ticks and populates every standard calendar field. + */ +TEST_F(XboxTimeTest, ConvertsTheUnixEpochIntoCallerOwnedUtcFields) { + std::tm result {}; + result.tm_isdst = 1; + ASSERT_TRUE(moonlight_gmtime(0, result)); + EXPECT_EQ(kernel_time().calls, 1); + EXPECT_EQ(kernel_time().ticks, 116444736000000000LL); + EXPECT_EQ(result.tm_year, 70); + EXPECT_EQ(result.tm_mon, 0); + EXPECT_EQ(result.tm_mday, 1); + EXPECT_EQ(result.tm_hour, 0); + EXPECT_EQ(result.tm_min, 0); + EXPECT_EQ(result.tm_sec, 0); + EXPECT_EQ(result.tm_wday, 4); + EXPECT_EQ(result.tm_yday, 0); + EXPECT_EQ(result.tm_isdst, 0); +} + +/** + * @brief Leap day conversion preserves clock fields and a second conversion does not overwrite earlier output. + */ +TEST_F(XboxTimeTest, PreservesLeapDayAndIndependentResultStorage) { + kernel_time().reply = {2000, 2, 29, 12, 34, 56, 0, 2}; + std::tm first {}; + ASSERT_TRUE(moonlight_gmtime(951827696, first)); + EXPECT_EQ(kernel_time().ticks, 125963012960000000LL); + EXPECT_EQ(first.tm_year, 100); + EXPECT_EQ(first.tm_mon, 1); + EXPECT_EQ(first.tm_mday, 29); + EXPECT_EQ(first.tm_hour, 12); + EXPECT_EQ(first.tm_min, 34); + EXPECT_EQ(first.tm_sec, 56); + EXPECT_EQ(first.tm_wday, 2); + EXPECT_EQ(first.tm_yday, 59); + kernel_time().reply = {2000, 3, 1}; + std::tm second {}; + ASSERT_TRUE(moonlight_gmtime(951868800, second)); + EXPECT_EQ(second.tm_yday, 60); + EXPECT_EQ(first.tm_mday, 29); + EXPECT_EQ(first.tm_hour, 12); +} + +/** + * @brief Gregorian century exceptions and ordinary leap years produce the correct day of year. + */ +TEST_F(XboxTimeTest, AppliesGregorianCenturyAndLeapYearRules) { + kernel_time().reply = {2100, 3, 1}; + std::tm result {}; + ASSERT_TRUE(moonlight_gmtime(4107542400LL, result)); + EXPECT_EQ(kernel_time().ticks, 157520160000000000LL); + EXPECT_EQ(result.tm_year, 200); + EXPECT_EQ(result.tm_mon, 2); + EXPECT_EQ(result.tm_yday, 59); + kernel_time().reply = {2024, 3, 1}; + ASSERT_TRUE(moonlight_gmtime(1709251200, result)); + EXPECT_EQ(result.tm_yday, 60); +} + +/** + * @brief Invalid and overflowing timestamps never call the kernel or alter caller-owned output. + */ +TEST_F(XboxTimeTest, RejectsNegativeTimeAndNtTickOverflow) { + std::tm result {}; + result.tm_year = 123; + EXPECT_FALSE(moonlight_gmtime(-1, result)); + constexpr std::int64_t lastSecond = std::numeric_limits::max() / 10000000 - 11644473600LL; + EXPECT_FALSE(moonlight_gmtime(lastSecond + 1, result)); + EXPECT_EQ(kernel_time().calls, 0); + EXPECT_EQ(result.tm_year, 123); + EXPECT_TRUE(moonlight_gmtime(lastSecond, result)); + EXPECT_EQ(kernel_time().calls, 1); + EXPECT_LE(kernel_time().ticks, std::numeric_limits::max()); +} From 5e3157cbe2fd4a0f22e66968914db8141524ad38 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:30:58 -0500 Subject: [PATCH 19/22] docs: keep streaming instructions user focused --- README.md | 122 ++++++++++++++++-------------------------------------- 1 file changed, 35 insertions(+), 87 deletions(-) diff --git a/README.md b/README.md index 3f1d43a..c49fad9 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,8 @@ captions, and a blue focus ring shows which card will open. Running apps carry a Missing artwork uses Lucide desktop or gamepad placeholders. Use the D-pad or left stick to navigate, **A** to select, **B** to go back and **Y** for host or app actions. Move up from the first PC row to reach Settings, Support and Add Host. -Keyboard arrows support held navigation with the same initial delay and repeat rate as the gamepad. -Use **Enter** to select, **Esc** to go back, and **M** for host or app actions. Helper glyphs switch to -keyboard keycaps when a key is pressed and return to controller glyphs when a button or deliberate -stick movement is used. Small stick drift and device connection events do not change the helpers. +With a keyboard, hold the arrows to navigate, use **Enter** to select, **Esc** to go back, and **M** +for host or app actions. Button hints follow the keyboard or controller you are using. On the hosts page, **Alt + F4** exits the client. **PgUp / PgDn** scroll the log viewer; hold **Shift** for faster scrolling. **Backspace** dismisses a notification or changes the log pane. @@ -88,99 +86,58 @@ Press **Back/Select + Black + White (LB + RB)** together on any connected contro controls. Release the buttons, select a choice with either stick, then press **A** to choose it. **B** resumes playback. A USB keyboard can open the same menu with **Ctrl + Alt + Shift + Q**; use **Tab** to cycle through all choices, arrows to select the left wheel, **Enter** to choose, and -**Esc** to go back. Opening local controls releases held host input. Center the sticks and release -the buttons before resuming gameplay. The same shortcut returns to the wheels from the keyboard, -logs or quit confirmation. Lucide icons identify each action, and either stick updates the highlight -continuously as it moves. A translucent dark backdrop dims the live stream behind the wheels; the wheel sectors stay opaque -for readability. Guide and Share use Xbox Series-style symbols. +**Esc** to go back. Center the sticks and release the buttons before resuming gameplay. +The same shortcut returns to the wheels from the keyboard, logs or exit confirmation. | Stick | Up | Right | Down | Left | | ----- | -------- | ---------- | ---------- | ---------------------- | -| Left | Keyboard | Mouse mode | View logs | Quit stream | +| Left | Keyboard | Mouse mode | View logs | Exit | | Right | Guide | Share | Screenshot | Enable / disable stats | ![Stream controls](./docs/images/screenshots/12-stream-controls.png) **Exit** opens a confirmation dialog: **A / Enter** disconnects, **X / Q** quits the host app and **B / Esc** cancels. -The icons sit on their buttons; arrow navigation is not required. The client displays **Stopping stream...**, -then a stream summary. Release the buttons, then press a button to return to the library. -The host application keeps running when you choose Disconnect. - -The on-screen keyboard uses an Xbox One-style layout on a solid dark panel: a number row, -QWERTY keys, a wide space bar, tall side controls, and an outlined focus key. Use **D-pad / left stick** -to move, **A** to type, **X** for Backspace, **Y** for Space, and **Start** for host Enter. -**Right stick click** applies Shift to the next key; **left stick click** toggles persistent Caps. -**White / Black (LB / RB)** move the host text cursor left / right. **LT** cycles through letters, -symbols, and PC keys. The symbols page keeps its explicit numbers and punctuation unchanged by Shift; -PC keys include F1–F12, -navigation keys, Ctrl / Alt / Win toggles, and shortcuts for Copy, Paste, Cut, Undo, Redo, Select All, -Alt+Tab, Alt+F4, Ctrl+Alt+Delete, Win+D, Win+E and Win+R. Focus a shortcut to see its full combination -above the keys. Named shortcuts send their own modifier combination. **RT** moves the panel between -the top and bottom edges; the placement is saved when the stream ends. **B** returns to the wheels. -Leaving the keyboard clears held modifiers. Keys use the host's US keyboard layout. -Controller shortcut icons retain the same colors as the main menus. Caps and Enter use larger side-key -glyphs; the four footer helpers (Select, Back, Shift and Move) share one row. Held controller navigation -uses the same 150 ms initial delay and 45 ms repeat interval as keyboard navigation. +**Disconnect** leaves the application running on the PC; select its pause-marked poster to resume. +Its app action menu also offers **Quit app**. Quitting can lose unsaved progress. + +Use **D-pad / left stick** to move around the on-screen keyboard, **A** to type, **X** for Backspace, +**Y** for Space, and **Start** for host Enter. **Right stick click** applies Shift to the next key; +**left stick click** toggles Caps. **White / Black (LB / RB)** move the host text cursor left / right. +**LT** cycles through letters, symbols, and PC keys, including function keys and common shortcuts. +Focus a shortcut to see its combination above the keys. **RT** moves the keyboard between the top +and bottom of the screen; **B** returns to the wheels. Keys use the host's US keyboard layout. When using a physical keyboard, use **Tab** to change page, **Shift** for one-shot Shift, **Caps Lock** to toggle Caps, **F2** to move the panel, **Space / Backspace** to send those keys, and **Esc** to return. -In the numeric address/port keypad, **Space** types the focused key and **Enter** accepts the field; -**PgUp / PgDn** change the address section. ![On-screen keyboard](./docs/images/screenshots/13-stream-keyboard.png) -Stream logs appear in a live side panel with severity colors and a scrollbar. Scroll upward to read -older entries; return to the bottom to follow new entries. **Exit** opens a three-button dialog: -**B / Esc** cancels, **A / Enter** disconnects while leaving the app running on the PC, and **X / Q** -quits the app on the PC. Quitting can lose unsaved progress. An in-progress app has a pause icon on -its poster and can be selected to resume; its app action menu also offers **Quit app**, with confirmation. -The pause marker describes a resumable streaming session; the PC app continues running after disconnecting. -Returning from the stream resets local keyboard/controller holds before library navigation resumes. -If the menu's gamepad disconnects, the client automatically selects another connected gamepad without -requiring it to be unplugged or the client restarted. Keyboard navigation remains independent of this handoff. - -Screenshots default to `E:\UDATA\\screenshots` and omit local controls. Open -**Settings > Storage > Screenshot Folder** to browse drives and subfolders, select the current folder, -or create a new folder with the on-screen keyboard or a USB keyboard. **Use default location** restores -the title-scoped screenshot folder. **B / Esc** cancels without changing the saved destination. -New folder names use up to 42 printable characters; **Start / Enter** creates the folder. The destination -is checked for write access before it is saved. You can also change `screenshot_directory` in the -`[streaming]` section of `moonlight.toml`; a configured folder is created when the first screenshot is -saved, and an empty value restores the default. -Use a TOML literal string for Xbox paths, for example `screenshot_directory = 'F:\Screenshots'`. -The same section stores `keyboard_dock_top = false` (bottom) or `true` (top). +**View logs** shows live entries. Scroll upward to read older entries; return to the bottom to follow +new entries. **Guide** and **Share** send their controller buttons to the PC; their behavior depends +on the host's virtual controller and operating system. + +Screenshots are saved as BMP files, excluding local menus and statistics. The default folder is +`E:\UDATA\\screenshots`. Open **Settings > Storage > Screenshot Folder** to browse drives +and subfolders, select the current folder, or create a new folder with the on-screen keyboard or a +USB keyboard. Press **Start / Enter** to create a folder after entering its name. +**Use default location** restores the default folder. **B / Esc** cancels without changing the destination. Open a game or app's action menu and choose **Screenshots** to view its captures. The viewer works -offline and shows the newest capture first; use **LB / RB** or **PgUp / PgDn** to browse, and -**B / Esc** to return to the app's actions. Left and right navigation also changes images. -Use **X / Backspace / Delete** in the viewer to delete the selected image after confirmation. -New screenshots are linked to the server UUID and app ID in the title-scoped `screenshots.tsv` index; -servers without a UUID use their saved address and port. Renaming a server or app does not change its -library. Changing the screenshot folder preserves access to previously indexed locations. Keep the -index with your save data, and keep the bitmap files in their original locations. Moved or deleted -files display an unavailable message. Captures saved before this index was introduced remain on disk -and are not automatically assigned to an app. - -Taking a screenshot snapshots the currently displayed host frame and saves it in the background. -A progress popup remains visible while saving, then shows the saved path for five seconds. Capture errors -remain visible for eight seconds. Neither popup captures controller, mouse or keyboard input. -Only one capture saves at a time; completion and errors are written to the log. The wheels close -automatically after screenshot, Guide, Share, statistics and mouse-mode actions. +offline and shows the newest capture first; use **LB / RB**, left / right or **PgUp / PgDn** to browse, +and **B / Esc** to return. Use **X / Backspace / Delete** to delete the selected image after confirmation. + +Screenshots save in the background. A popup shows progress, then the saved path or an error. +The wheels close automatically after screenshot, Guide, Share, statistics and mouse-mode actions. ![Screenshot notification](./docs/images/screenshots/14-screenshot-notification.png) Mouse mode uses the invoking controller's **left stick** for movement and **A / B** for left / right -clicks. Stick-driven mouse movement is reported at up to 125 Hz, while button presses and releases -are sent immediately. That controller's gameplay input stays suppressed until mouse mode is disabled through the menu; -other gamepads continue to work. Physical keyboard and mouse input can be used alongside virtual input. +clicks. Disable mouse mode through the wheels to use that controller for gameplay again. +Other gamepads and physical keyboards and mice can be used at the same time. -Up to four gamepads are sent as separate controllers to Sunshine. Disconnecting a controller releases -its slot without renumbering the remaining players. Newly connected pads reuse free slots. +Up to four gamepads are supported as separate controllers in Sunshine. Standard USB boot keyboards and three-button boot mice are supported through Xbox-to-USB adapters -attached to the controller ports. Devices must support HID boot protocol; report-only devices and -nonstandard gaming-device reports are not supported. Boot mouse wheel and extra buttons are not -decoded. When SDL supplies wheel or extra mouse button events, they are forwarded to the host. -USB disconnects release held keys and mouse buttons. Console USB compatibility and host controller -creation should be checked with the actual devices and Sunshine configuration in use. +attached to the controller ports. Devices must support HID boot protocol; mouse wheels, extra buttons +and nonstandard gaming devices may not work. For xemu, open its Monitor with **~** and enter `device_add usb-kbd,id=kbd` or `device_add usb-mouse,id=mouse` to attach an emulated USB device to an available port. @@ -188,17 +145,8 @@ Remove them with `device_del kbd` and `device_del mouse`. See the [xemu keyboard setup guide](https://xemu.app/docs/keyboard/) for explicit controller-port selection. When Sunshine and xemu run on the same PC, prevent Sunshine's virtual controllers from being attached -to xemu. They can appear when streaming starts. If they are visible to xemu, disable automatic controller -binding in **Input**, manually assign the physical controller, and remove any saved virtual-controller -assignments. Leave keyboard/mouse ports unassigned. Otherwise xemu can attach a virtual gamepad to a -port already occupied by a USB keyboard or mouse and exit with -`usb_claim_port(): ... not found (in use?)`. - -**View logs** shows a snapshot of recent runtime entries; scroll with up/down and reopen it to refresh. -**Guide** and **Share** send controller button presses to the host. Share behavior depends on the host's -selected virtual controller and operating system. **Screenshot** saves the clean decoded frame as a BMP -under the title's `E:\UDATA\` save directory and displays the saved path. Screenshots exclude local menus -and statistics. Capture requires a decoded frame and writable save storage. +to xemu. Disable automatic controller binding in **Input**, manually assign the physical controller, +and leave ports used for keyboards or mice unassigned to controllers to avoid USB port conflicts. Enable **Settings → Display → Performance Stats** for a live panel during playback and additional performance details in the summary. The panel works with GPU and software presentation, refreshes From 1e3e85d0ab1030c1b07b067e75163e3f277d3637 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:00:34 -0500 Subject: [PATCH 20/22] refactor: finish stream controls quality cleanup --- src/app/client_state.cpp | 2 +- src/app/client_state.h | 2 +- src/app/screenshot_library.cpp | 25 +- src/platform/usb_input.cpp | 5 - src/streaming/ffmpeg_stream_backend.cpp | 4 +- src/streaming/stream_controls.cpp | 387 ++++++++++++------------ src/streaming/stream_controls.h | 259 ++++++++-------- src/streaming/video_overlay.cpp | 20 +- src/streaming/video_overlay.h | 2 +- src/ui/shell_screen.cpp | 233 +++++++------- 10 files changed, 489 insertions(+), 450 deletions(-) diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 7553da8..a9b1c31 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -2943,7 +2943,7 @@ namespace app { } } - void apply_app_quit_result(ClientState &state, bool succeeded, std::string message) { + void apply_app_quit_result(ClientState &state, bool succeeded, const std::string &message) { const auto target = state.appQuit; if (!target.inProgress) { return; diff --git a/src/app/client_state.h b/src/app/client_state.h index cdc7bfb..9326a35 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -736,7 +736,7 @@ namespace app { * @param succeeded True after the host confirmed that no app is running. * @param message Completion or actionable failure detail. */ - void apply_app_quit_result(ClientState &state, bool succeeded, std::string message); + void apply_app_quit_result(ClientState &state, bool succeeded, const std::string &message); /** * @brief Remove a deleted capture from the open viewer and keep its selection in range. * @param state Viewer that requested deletion. diff --git a/src/app/screenshot_library.cpp b/src/app/screenshot_library.cpp index 085afb7..6b505ef 100644 --- a/src/app/screenshot_library.cpp +++ b/src/app/screenshot_library.cpp @@ -66,6 +66,22 @@ namespace { return std::tolower(a) == std::tolower(b); }); } + + /** + * @brief Convert one timestamp using caller-owned calendar storage on every platform. + * @param time Unix timestamp representable by the platform clock type. + * @param utc Receives broken-down UTC calendar fields. + * @return True after a successful platform conversion. + */ + bool screenshot_utc_time(std::time_t time, std::tm &utc) { +#ifdef NXDK + return moonlight_gmtime(time, utc); +#elif defined(_WIN32) + return gmtime_s(&utc, &time) == 0; +#else + return gmtime_r(&time, &utc) != nullptr; +#endif + } } // namespace namespace app { @@ -296,14 +312,7 @@ namespace app { return "Capture time unavailable"; } std::tm utc {}; -#ifdef NXDK - const bool converted = moonlight_gmtime(time, utc); -#elif defined(_WIN32) - const bool converted = gmtime_s(&utc, &time) == 0; -#else - const bool converted = gmtime_r(&time, &utc) != nullptr; -#endif - if (!converted || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", &utc) == 0) { + if (!screenshot_utc_time(time, utc) || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", &utc) == 0) { return "Capture time unavailable"; } return label.data(); diff --git a/src/platform/usb_input.cpp b/src/platform/usb_input.cpp index 3551bd1..c997ba9 100644 --- a/src/platform/usb_input.cpp +++ b/src/platform/usb_input.cpp @@ -12,11 +12,6 @@ #include // nxdk includes -#ifdef NXDK - #include -#else - #include -#endif #include // lib includes diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index 937cc70..c529028 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -500,12 +500,12 @@ namespace streaming { /** * @brief Forward one BMP write and publish progress only for bytes actually written. * @param stream Progress-decorated SDL file. - * @param pixels Header or pixel bytes. + * @param pixels Header or pixel bytes supplied by the SDL C writer callback. * @param size Bytes per object. * @param count Requested object count. * @return Number of complete objects written. */ - std::size_t screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { + extern "C" std::size_t screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { auto &writer = *static_cast(stream->hidden.unknown.data1); const auto written = SDL_RWwrite(writer.file, pixels, size, count); writer.position += static_cast(written * size); diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 5566592..48840d6 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -192,6 +192,9 @@ namespace { } // namespace namespace streaming { + StreamControls::EventHandler::EventHandler(StreamControls &controls) noexcept: + controls_(controls) {} + void StreamControls::clear_panel() { panel_.text.clear(); panel_.textBytes = 0; @@ -246,9 +249,9 @@ namespace streaming { SDL_RenderCopy(renderer, raster.texture.get(), &source, &destination); } - void StreamControls::navigate_controller(const input::ControllerState &sample, Uint32 now) { - if (menu_.page != input::StreamMenuPage::keyboard && menu_.page != input::StreamMenuPage::logs) { - navigation_.controller.reset(); + void StreamControls::EventHandler::navigate_controller(const input::ControllerState &sample, Uint32 now) const { + if (controls_.menu_.page != input::StreamMenuPage::keyboard && controls_.menu_.page != input::StreamMenuPage::logs) { + controls_.navigation_.controller.reset(); return; } int x = (sample.buttonFlags & RIGHT_FLAG) != 0; @@ -259,20 +262,20 @@ namespace streaming { if (y == 0 && (sample.buttonFlags & UP_FLAG) != 0) { y = -1; } - if (navigation_.neutralRequired) { + if (controls_.navigation_.neutralRequired) { if (x == 0 && y == 0 && std::max(std::abs(static_cast(sample.leftStickX)), std::abs(static_cast(sample.leftStickY))) < input::STICK_NAVIGATION_RELEASE_THRESHOLD) { - navigation_.neutralRequired = false; + controls_.navigation_.neutralRequired = false; } return; } const auto command = input::direction_command(x, y); - const auto step = x == 0 && y == 0 ? navigation_.controller.update_stick(sample.leftStickX, sample.leftStickY, now) : navigation_.controller.update(command, now); + const auto step = x == 0 && y == 0 ? controls_.navigation_.controller.update_stick(sample.leftStickX, sample.leftStickY, now) : controls_.navigation_.controller.update(command, now); const auto key = menu_navigation_key(step); - if (key != SDLK_UNKNOWN && !navigation_.applied) { - navigation_.applied = true; + if (key != SDLK_UNKNOWN && !controls_.navigation_.applied) { + controls_.navigation_.applied = true; const auto [dx, dy] = input::navigation_step(menu_navigation_command(key)); - menu_.navigate(dx, dy); - panel_.dirty = true; + controls_.menu_.navigate(dx, dy); + controls_.panel_.dirty = true; } } @@ -319,12 +322,12 @@ namespace streaming { return visible() || screenshotNotification_.visible; } - void StreamControls::release_pulse() { - if (pulseSlot_ >= 0) { - input::StreamPacket packet {input::StreamPacketType::controller, pulseSlot_, router_.active_mask()}; - packet.controller = latestHostController_[pulseSlot_]; - pulseSlot_ = -1; - pulseButtons_ = 0; + void StreamControls::EventHandler::release_pulse() const { + if (controls_.pulseSlot_ >= 0) { + input::StreamPacket packet {input::StreamPacketType::controller, controls_.pulseSlot_, controls_.router_.active_mask()}; + packet.controller = controls_.latestHostController_[controls_.pulseSlot_]; + controls_.pulseSlot_ = -1; + controls_.pulseButtons_ = 0; send_packet(packet); } } @@ -363,7 +366,7 @@ namespace streaming { } void StreamControls::close_if_removed(int instance) { - release_pulse(); + events_.release_pulse(); router_.disconnect(instance); if (mouseDevice_ == instance) { mouseDevice_ = -1; @@ -429,75 +432,75 @@ namespace streaming { return menu_.page != input::StreamMenuPage::closed; } - void StreamControls::open_menu(int device) { - if (menu_.page == input::StreamMenuPage::radial) { + void StreamControls::EventHandler::open_menu(int device) const { + if (controls_.menu_.page == input::StreamMenuPage::radial) { return; } - menu_.open(device); - navigation_.keyboard.reset(); - navigation_.controller.reset(); - navigation_.neutralRequired = false; - navigation_.pageTriggerDown = false; - navigation_.dockTriggerDown = false; - navigation_.inputDevice = device < 0 ? input::UiInputDevice::keyboard : input::UiInputDevice::gamepad; - router_.set_captured(true); - navigation_.armed = false; - panel_.dirty = true; - panel_.message.clear(); + controls_.menu_.open(device); + controls_.navigation_.keyboard.reset(); + controls_.navigation_.controller.reset(); + controls_.navigation_.neutralRequired = false; + controls_.navigation_.pageTriggerDown = false; + controls_.navigation_.dockTriggerDown = false; + controls_.navigation_.inputDevice = device < 0 ? input::UiInputDevice::keyboard : input::UiInputDevice::gamepad; + controls_.router_.set_captured(true); + controls_.navigation_.armed = false; + controls_.panel_.dirty = true; + controls_.panel_.message.clear(); logging::info("input", "Opened stream menu from " + (device < 0 ? std::string("keyboard") : "gamepad " + std::to_string(device))); } - void StreamControls::tap_key(int code) { + void StreamControls::EventHandler::tap_key(int code) const { if (code == 0) { return; } - const int keyModifiers = menu_.key_modifiers(code); + const int keyModifiers = controls_.menu_.key_modifiers(code); // No gameplay sample is forwarded during this event-thread-only local key tap. - router_.set_captured(false); + controls_.router_.set_captured(false); constexpr std::array modifiers {MODIFIER_SHIFT, MODIFIER_CTRL, MODIFIER_ALT, MODIFIER_META}; constexpr std::array keys {0xA0, 0xA2, 0xA4, 0x15B}; for (std::size_t i = 0; i < modifiers.size(); ++i) { if ((keyModifiers & modifiers[i]) != 0) { - router_.key(-3, keys[i], true); + controls_.router_.key(-3, keys[i], true); } } - router_.key(-3, code, true, keyModifiers); - router_.key(-3, code, false, keyModifiers); - router_.release_source(-3); - router_.set_captured(true); - menu_.key_sent(); - panel_.dirty = true; + controls_.router_.key(-3, code, true, keyModifiers); + controls_.router_.key(-3, code, false, keyModifiers); + controls_.router_.release_source(-3); + controls_.router_.set_captured(true); + controls_.menu_.key_sent(); + controls_.panel_.dirty = true; } - bool StreamControls::confirm_exit_key(SDL_Keycode key) { + bool StreamControls::EventHandler::confirm_exit_key(SDL_Keycode key) const { if (key == SDLK_RETURN) { - return menu_.confirm_disconnect(input::StreamDisconnectAction::disconnect); + return controls_.menu_.confirm_disconnect(input::StreamDisconnectAction::disconnect); } if (key == SDLK_q) { - return menu_.confirm_disconnect(input::StreamDisconnectAction::quit_app); + return controls_.menu_.confirm_disconnect(input::StreamDisconnectAction::quit_app); } if (key == SDLK_ESCAPE) { - panel_.dirty = true; - return menu_.confirm_disconnect(input::StreamDisconnectAction::cancel); + controls_.panel_.dirty = true; + return controls_.menu_.confirm_disconnect(input::StreamDisconnectAction::cancel); } return false; } - bool StreamControls::handle_keyboard_shortcut(SDL_Keycode key, bool active) { + bool StreamControls::EventHandler::handle_keyboard_shortcut(SDL_Keycode key, bool active) const { if (key == SDLK_TAB) { - menu_.toggle_keyboard_page(); + controls_.menu_.toggle_keyboard_page(); return true; } if (key == SDLK_LSHIFT || key == SDLK_RSHIFT) { - menu_.toggle_shift(); + controls_.menu_.toggle_shift(); return true; } if (key == SDLK_CAPSLOCK) { - menu_.caps = !menu_.caps; + controls_.menu_.caps = !controls_.menu_.caps; return true; } if (key == SDLK_F2) { - keyboardTop_ = !keyboardTop_; + controls_.keyboardTop_ = !controls_.keyboardTop_; return true; } if (key == SDLK_BACKSPACE || key == SDLK_SPACE) { @@ -509,71 +512,71 @@ namespace streaming { return false; } - void StreamControls::activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats) { - switch (menu_.selected) { + void StreamControls::EventHandler::activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats) const { + switch (controls_.menu_.selected) { case input::StreamAction::quit: - menu_.page = input::StreamMenuPage::quit_confirmation; + controls_.menu_.page = input::StreamMenuPage::quit_confirmation; break; case input::StreamAction::keyboard: - menu_.page = input::StreamMenuPage::keyboard; - navigation_.controller.reset(); - navigation_.neutralRequired = true; + controls_.menu_.page = input::StreamMenuPage::keyboard; + controls_.navigation_.controller.reset(); + controls_.navigation_.neutralRequired = true; break; case input::StreamAction::logs: - menu_.page = input::StreamMenuPage::logs; - navigation_.controller.reset(); - navigation_.neutralRequired = true; - panel_.logs.clear(); + controls_.menu_.page = input::StreamMenuPage::logs; + controls_.navigation_.controller.reset(); + controls_.navigation_.neutralRequired = true; + controls_.panel_.logs.clear(); for (const auto &entry : logging::snapshot()) { - panel_.logs.push_back(logging::format_entry(entry)); + controls_.panel_.logs.push_back(logging::format_entry(entry)); } - if (panel_.logs.empty()) { - panel_.logs = logging::load_log_file().lines; + if (controls_.panel_.logs.empty()) { + controls_.panel_.logs = logging::load_log_file().lines; } break; case input::StreamAction::mouse: - if (mouseDevice_ < 0 && menu_.owner < 0) { - panel_.message = "Open the menu with a controller to enable mouse mode."; + if (controls_.mouseDevice_ < 0 && controls_.menu_.owner < 0) { + controls_.panel_.message = "Open the menu with a controller to enable mouse mode."; break; } - mouseDevice_ = mouseDevice_ < 0 ? menu_.owner : -1; - router_.set_mouse_device(mouseDevice_); - menu_.close(); + controls_.mouseDevice_ = controls_.mouseDevice_ < 0 ? controls_.menu_.owner : -1; + controls_.router_.set_mouse_device(controls_.mouseDevice_); + controls_.menu_.close(); break; case input::StreamAction::statistics: stats = !stats; - menu_.close(); + controls_.menu_.close(); break; case input::StreamAction::screenshot: { - if (backend.request_screenshot(screenshotDirectory_, screenshotSource_)) { - screenshotNotification_.begin(); + if (backend.request_screenshot(controls_.screenshotDirectory_, controls_.screenshotSource_)) { + controls_.screenshotNotification_.begin(); } else if (backend.screenshot_progress() < 0) { - screenshotNotification_.finish(false, "Could not start capture. Check the stream log for details.", SDL_GetTicks()); + controls_.screenshotNotification_.finish(false, "Could not start capture. Check the stream log for details.", SDL_GetTicks()); } - panel_.message = screenshotNotification_.title(); - logging::info("stream", panel_.message); - menu_.close(); + controls_.panel_.message = controls_.screenshotNotification_.title(); + logging::info("stream", controls_.panel_.message); + controls_.menu_.close(); break; } case input::StreamAction::guide: case input::StreamAction::share: { - const int slot = active ? router_.connect(menu_.owner) : -1; + const int slot = active ? controls_.router_.connect(controls_.menu_.owner) : -1; if (slot < 0) { - panel_.message = "A connected controller and active stream are required."; + controls_.panel_.message = "A connected controller and active stream are required."; break; } release_pulse(); - pulseSlot_ = slot; - pulseUntil_ = SDL_GetTicks() + 120; - pulseButtons_ = menu_.selected == input::StreamAction::guide ? SPECIAL_FLAG : MISC_FLAG; - input::StreamPacket packet {input::StreamPacketType::controller, slot, router_.active_mask()}; - packet.controller = latestHostController_[slot]; - packet.controller.buttonFlags |= pulseButtons_; + controls_.pulseSlot_ = slot; + controls_.pulseUntil_ = SDL_GetTicks() + 120; + controls_.pulseButtons_ = controls_.menu_.selected == input::StreamAction::guide ? SPECIAL_FLAG : MISC_FLAG; + input::StreamPacket packet {input::StreamPacketType::controller, slot, controls_.router_.active_mask()}; + packet.controller = controls_.latestHostController_[slot]; + packet.controller.buttonFlags |= controls_.pulseButtons_; send_packet(packet); - panel_.message = menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; - menu_.close(); + controls_.panel_.message = controls_.menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; + controls_.menu_.close(); break; } case input::StreamAction::none: @@ -581,40 +584,40 @@ namespace streaming { } } - bool StreamControls::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) { - if (menu_.page == input::StreamMenuPage::quit_confirmation) { + bool StreamControls::EventHandler::menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) const { + if (controls_.menu_.page == input::StreamMenuPage::quit_confirmation) { return confirm_exit_key(key); } - panel_.dirty = true; + controls_.panel_.dirty = true; if (key == SDLK_ESCAPE) { - if (menu_.page == input::StreamMenuPage::radial) { - menu_.close(); + if (controls_.menu_.page == input::StreamMenuPage::radial) { + controls_.menu_.close(); } else { - menu_.page = input::StreamMenuPage::radial; - menu_.modifiers = 0; - menu_.shiftOnce = false; + controls_.menu_.page = input::StreamMenuPage::radial; + controls_.menu_.modifiers = 0; + controls_.menu_.shiftOnce = false; } return false; } - if (menu_.page == input::StreamMenuPage::keyboard && handle_keyboard_shortcut(key, active)) { + if (controls_.menu_.page == input::StreamMenuPage::keyboard && handle_keyboard_shortcut(key, active)) { return false; } if (key != SDLK_RETURN && key != SDLK_SPACE) { - if (key == SDLK_TAB && menu_.page == input::StreamMenuPage::radial) { - menu_.selected = menu_.selected == input::StreamAction::none ? input::StreamAction::keyboard : static_cast((static_cast(menu_.selected) + 1) % 8); + if (key == SDLK_TAB && controls_.menu_.page == input::StreamMenuPage::radial) { + controls_.menu_.selected = controls_.menu_.selected == input::StreamAction::none ? input::StreamAction::keyboard : static_cast((static_cast(controls_.menu_.selected) + 1) % 8); } else { const auto [dx, dy] = input::navigation_step(menu_navigation_command(key)); - menu_.navigate(dx, dy); + controls_.menu_.navigate(dx, dy); } return false; } - if (menu_.page == input::StreamMenuPage::keyboard) { + if (controls_.menu_.page == input::StreamMenuPage::keyboard) { if (active) { - tap_key(menu_.keyboard_key()); + tap_key(controls_.menu_.keyboard_key()); } return false; } - if (menu_.page != input::StreamMenuPage::radial) { + if (controls_.menu_.page != input::StreamMenuPage::radial) { return false; } activate_stream_action(backend, active, stats); @@ -641,44 +644,44 @@ namespace streaming { } } - void StreamControls::note_gamepad_activity(const SDL_Event &event) { + void StreamControls::EventHandler::note_gamepad_activity(const SDL_Event &event) const { const bool triggerAxis = event.type == SDL_CONTROLLERAXISMOTION && (event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || event.caxis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT); const bool gamepadActivity = event.type == SDL_CONTROLLERBUTTONDOWN || (event.type == SDL_CONTROLLERAXISMOTION && (triggerAxis ? event.caxis.value >= 16000 : std::abs(static_cast(event.caxis.value)) >= input::StreamMenu::WHEEL_DEADZONE)); - if (visible() && gamepadActivity && menu_.owner < 0) { - menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; + if (controls_.visible() && gamepadActivity && controls_.menu_.owner < 0) { + controls_.menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; } - if (visible() && gamepadActivity && navigation_.inputDevice != input::UiInputDevice::gamepad) { - navigation_.inputDevice = input::UiInputDevice::gamepad; - navigation_.keyboard.reset(); - panel_.dirty = true; + if (controls_.visible() && gamepadActivity && controls_.navigation_.inputDevice != input::UiInputDevice::gamepad) { + controls_.navigation_.inputDevice = input::UiInputDevice::gamepad; + controls_.navigation_.keyboard.reset(); + controls_.panel_.dirty = true; } if (event.type == SDL_CONTROLLERAXISMOTION) { keyboard_trigger(event.caxis); } } - void StreamControls::keyboard_trigger(const SDL_ControllerAxisEvent &axis) { - if (!visible() || menu_.page != input::StreamMenuPage::keyboard || axis.which != menu_.owner) { + void StreamControls::EventHandler::keyboard_trigger(const SDL_ControllerAxisEvent &axis) const { + if (!controls_.visible() || controls_.menu_.page != input::StreamMenuPage::keyboard || axis.which != controls_.menu_.owner) { return; } const bool pressed = axis.value >= 16000; if (axis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT) { - if (pressed && !navigation_.pageTriggerDown && navigation_.armed) { - menu_.toggle_keyboard_page(); - panel_.dirty = true; + if (pressed && !controls_.navigation_.pageTriggerDown && controls_.navigation_.armed) { + controls_.menu_.toggle_keyboard_page(); + controls_.panel_.dirty = true; } - navigation_.pageTriggerDown = pressed; + controls_.navigation_.pageTriggerDown = pressed; } else if (axis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { - if (pressed && !navigation_.dockTriggerDown && navigation_.armed) { - keyboardTop_ = !keyboardTop_; - panel_.dirty = true; + if (pressed && !controls_.navigation_.dockTriggerDown && controls_.navigation_.armed) { + controls_.keyboardTop_ = !controls_.keyboardTop_; + controls_.panel_.dirty = true; } - navigation_.dockTriggerDown = pressed; + controls_.navigation_.dockTriggerDown = pressed; } } - void StreamControls::joystick_button(const SDL_JoyButtonEvent &button) { - for (auto &device : devices_) { + void StreamControls::EventHandler::joystick_button(const SDL_JoyButtonEvent &button) const { + for (auto &device : controls_.devices_) { if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != button.which) { continue; } @@ -693,8 +696,8 @@ namespace streaming { } } - void StreamControls::route_controller_button(const SDL_ControllerButtonEvent &button, bool active) { - for (auto &device : devices_) { + void StreamControls::EventHandler::route_controller_button(const SDL_ControllerButtonEvent &button, bool active) const { + for (auto &device : controls_.devices_) { if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != button.which) { continue; } @@ -710,30 +713,30 @@ namespace streaming { device.sample.buttonFlags &= ~flag; } if (active) { - router_.connect(button.which); - router_.controller(button.which, device.sample); + controls_.router_.connect(button.which); + controls_.router_.controller(button.which, device.sample); } } } - void StreamControls::navigate_owner_controller() { - for (const auto &device : devices_) { - if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == menu_.owner) { + void StreamControls::EventHandler::navigate_owner_controller() const { + for (const auto &device : controls_.devices_) { + if (device.handle != nullptr && SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) == controls_.menu_.owner) { navigate_controller(device.sample, SDL_GetTicks()); } } } - bool StreamControls::controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + bool StreamControls::EventHandler::controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { route_controller_button(button, active); - if (visible() && navigation_.armed && button.state == SDL_RELEASED && button.which == menu_.owner && menu_.page != input::StreamMenuPage::radial) { + if (controls_.visible() && controls_.navigation_.armed && button.state == SDL_RELEASED && button.which == controls_.menu_.owner && controls_.menu_.page != input::StreamMenuPage::radial) { navigate_owner_controller(); } - if (visible() && navigation_.armed && button.state == SDL_PRESSED) { - if (menu_.owner < 0) { - menu_.owner = button.which; + if (controls_.visible() && controls_.navigation_.armed && button.state == SDL_PRESSED) { + if (controls_.menu_.owner < 0) { + controls_.menu_.owner = button.which; } - if (button.which != menu_.owner) { + if (button.which != controls_.menu_.owner) { return false; } return activate_controller_button(button.button, backend, active, statisticsEnabled); @@ -741,71 +744,68 @@ namespace streaming { return false; } - bool StreamControls::activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { - bool stop = false; + bool StreamControls::EventHandler::activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { switch (button) { case SDL_CONTROLLER_BUTTON_A: - stop = menu_key(SDLK_RETURN, backend, active, statisticsEnabled) || stop; - break; + return menu_key(SDLK_RETURN, backend, active, statisticsEnabled); case SDL_CONTROLLER_BUTTON_B: - stop = menu_key(SDLK_ESCAPE, backend, active, statisticsEnabled) || stop; - break; + return menu_key(SDLK_ESCAPE, backend, active, statisticsEnabled); case SDL_CONTROLLER_BUTTON_DPAD_UP: case SDL_CONTROLLER_BUTTON_DPAD_DOWN: case SDL_CONTROLLER_BUTTON_DPAD_LEFT: case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: - if (menu_.page == input::StreamMenuPage::radial) { + if (controls_.menu_.page == input::StreamMenuPage::radial) { menu_key(controller_navigation_key(button), backend, active, statisticsEnabled); } else { navigate_owner_controller(); } break; case SDL_CONTROLLER_BUTTON_X: - if (menu_.page == input::StreamMenuPage::quit_confirmation) { - stop = menu_key(SDLK_q, backend, active, statisticsEnabled) || stop; - } else if (active && menu_.page == input::StreamMenuPage::keyboard) { + if (controls_.menu_.page == input::StreamMenuPage::quit_confirmation) { + return menu_key(SDLK_q, backend, active, statisticsEnabled); + } else if (active && controls_.menu_.page == input::StreamMenuPage::keyboard) { tap_key(0x08); } break; case SDL_CONTROLLER_BUTTON_Y: - if (active && menu_.page == input::StreamMenuPage::keyboard) { + if (active && controls_.menu_.page == input::StreamMenuPage::keyboard) { tap_key(0x20); } break; case SDL_CONTROLLER_BUTTON_LEFTSTICK: - if (menu_.page == input::StreamMenuPage::keyboard) { - menu_.caps = !menu_.caps; - panel_.dirty = true; + if (controls_.menu_.page == input::StreamMenuPage::keyboard) { + controls_.menu_.caps = !controls_.menu_.caps; + controls_.panel_.dirty = true; } break; case SDL_CONTROLLER_BUTTON_RIGHTSTICK: - if (menu_.page == input::StreamMenuPage::keyboard) { - menu_.toggle_shift(); - panel_.dirty = true; + if (controls_.menu_.page == input::StreamMenuPage::keyboard) { + controls_.menu_.toggle_shift(); + controls_.panel_.dirty = true; } break; case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: - if (active && menu_.page == input::StreamMenuPage::keyboard) { + if (active && controls_.menu_.page == input::StreamMenuPage::keyboard) { tap_key(button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ? 0x125 : 0x127); } break; case SDL_CONTROLLER_BUTTON_START: - if (active && menu_.page == input::StreamMenuPage::keyboard) { + if (active && controls_.menu_.page == input::StreamMenuPage::keyboard) { tap_key(0x0D); } break; default: break; } - return stop; + return false; } - void StreamControls::controller_axis(const SDL_ControllerAxisEvent &axis) { - if (!visible() || !navigation_.armed || axis.which != menu_.owner) { + void StreamControls::EventHandler::controller_axis(const SDL_ControllerAxisEvent &axis) const { + if (!controls_.visible() || !controls_.navigation_.armed || axis.which != controls_.menu_.owner) { return; } - for (auto &device : devices_) { + for (auto &device : controls_.devices_) { if (device.handle == nullptr || SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(device.handle)) != axis.which) { continue; } @@ -828,8 +828,8 @@ namespace streaming { break; } if (axis.axis <= SDL_CONTROLLER_AXIS_RIGHTY) { - if (menu_.page == input::StreamMenuPage::radial) { - panel_.dirty = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || panel_.dirty; + if (controls_.menu_.page == input::StreamMenuPage::radial) { + controls_.panel_.dirty = controls_.menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || controls_.panel_.dirty; } else { navigate_controller(sample, SDL_GetTicks()); } @@ -846,35 +846,34 @@ namespace streaming { return ((flags & KMOD_SHIFT) != 0 ? MODIFIER_SHIFT : 0) | ((flags & KMOD_CTRL) != 0 ? MODIFIER_CTRL : 0) | ((flags & KMOD_ALT) != 0 ? MODIFIER_ALT : 0) | ((flags & KMOD_GUI) != 0 ? MODIFIER_META : 0); } - bool StreamControls::keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + bool StreamControls::EventHandler::keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { bool stop = false; const bool down = key.type == SDL_KEYDOWN; - const bool shortcut = key.keysym.scancode == SDL_SCANCODE_Q && (key.keysym.mod & KMOD_CTRL) != 0 && (key.keysym.mod & KMOD_ALT) != 0 && (key.keysym.mod & KMOD_SHIFT) != 0; - if (down && key.repeat == 0 && shortcut) { + if (const bool shortcut = key.keysym.scancode == SDL_SCANCODE_Q && (key.keysym.mod & KMOD_CTRL) != 0 && (key.keysym.mod & KMOD_ALT) != 0 && (key.keysym.mod & KMOD_SHIFT) != 0; down && key.repeat == 0 && shortcut) { open_menu(-1); - navigation_.armed = true; + controls_.navigation_.armed = true; return false; } - if (visible()) { + if (controls_.visible()) { if (!down) { - navigation_.keyboard.release(key.keysym.scancode); + controls_.navigation_.keyboard.release(key.keysym.scancode); } if (!down || key.repeat != 0) { return false; } { - if (navigation_.inputDevice != input::UiInputDevice::keyboard) { - navigation_.inputDevice = input::UiInputDevice::keyboard; - panel_.dirty = true; + if (controls_.navigation_.inputDevice != input::UiInputDevice::keyboard) { + controls_.navigation_.inputDevice = input::UiInputDevice::keyboard; + controls_.panel_.dirty = true; } const auto command = menu_navigation_command(key.keysym.sym); if (command != input::UiCommand::none) { - if (navigation_.keyboard.press(key.keysym.scancode, command, SDL_GetTicks()) != input::UiCommand::none) { + if (controls_.navigation_.keyboard.press(key.keysym.scancode, command, SDL_GetTicks()) != input::UiCommand::none) { menu_key(key.keysym.sym, backend, active, statisticsEnabled); } } else { if (key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_SPACE || key.keysym.sym == SDLK_ESCAPE) { - navigation_.keyboard.reset(); + controls_.navigation_.keyboard.reset(); } stop = menu_key(key.keysym.sym, backend, active, statisticsEnabled) || stop; } @@ -883,7 +882,7 @@ namespace streaming { } if (active && key.repeat == 0) { const int mods = host_keyboard_modifiers(key.keysym.mod); - router_.key(0, input::keyboard_virtual_key(key.keysym.scancode), down, mods); + controls_.router_.key(0, input::keyboard_virtual_key(key.keysym.scancode), down, mods); } return stop; } @@ -910,14 +909,14 @@ namespace streaming { } } - bool StreamControls::handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) { + bool StreamControls::EventHandler::handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { note_gamepad_activity(event); switch (event.type) { case SDL_CONTROLLERDEVICEADDED: - open_if_needed(event.cdevice.which); + controls_.open_if_needed(event.cdevice.which); break; case SDL_CONTROLLERDEVICEREMOVED: - close_if_removed(event.cdevice.which); + controls_.close_if_removed(event.cdevice.which); break; case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: @@ -934,29 +933,29 @@ namespace streaming { return keyboard_event(event.key, backend, active, statisticsEnabled); case SDL_MOUSEMOTION: if (active) { - router_.mouse_move(event.motion.xrel, event.motion.yrel); + controls_.router_.mouse_move(event.motion.xrel, event.motion.yrel); } break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: if (active) { - router_.mouse_button(1, host_mouse_button(event.button.button), event.type == SDL_MOUSEBUTTONDOWN); + controls_.router_.mouse_button(1, host_mouse_button(event.button.button), event.type == SDL_MOUSEBUTTONDOWN); } break; case SDL_MOUSEWHEEL: if (active) { const int direction = event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1; - router_.scroll(event.wheel.x * direction, event.wheel.y * direction); + controls_.router_.scroll(event.wheel.x * direction, event.wheel.y * direction); } break; case SDL_WINDOWEVENT: if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { - focused_ = false; - navigation_.keyboard.reset(); - router_.set_captured(true); + controls_.focused_ = false; + controls_.navigation_.keyboard.reset(); + controls_.router_.set_captured(true); } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { - focused_ = true; - router_.set_captured(visible()); + controls_.focused_ = true; + controls_.router_.set_captured(controls_.visible()); } break; case SDL_QUIT: @@ -985,7 +984,7 @@ namespace streaming { } } - void StreamControls::update_device(Device &device, bool active, Uint32 now) { + void StreamControls::EventHandler::update_device(Device &device, bool active, Uint32 now) const { if (device.handle == nullptr) { return; } @@ -998,26 +997,26 @@ namespace streaming { device.chordDown = down; if (active) { if (present) { - router_.connect(instance); - router_.controller(instance, device.sample); + controls_.router_.connect(instance); + controls_.router_.controller(instance, device.sample); } else { - router_.disconnect(instance); + controls_.router_.disconnect(instance); } } - if (!visible() || instance != menu_.owner || navigation_.inputDevice != input::UiInputDevice::gamepad) { + if (!controls_.visible() || instance != controls_.menu_.owner || controls_.navigation_.inputDevice != input::UiInputDevice::gamepad) { return; } const auto &sample = device.sample; if (sample.buttonFlags == 0 && sample.leftTrigger == 0 && sample.rightTrigger == 0) { - navigation_.armed = true; + controls_.navigation_.armed = true; } - if (!navigation_.armed) { + if (!controls_.navigation_.armed) { return; } - if (menu_.page == input::StreamMenuPage::radial) { + if (controls_.menu_.page == input::StreamMenuPage::radial) { // Poll on every pump as well as consuming events: Xbox input can change // between queued axis events, and a selected item must not freeze polling. - panel_.dirty = menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || panel_.dirty; + controls_.panel_.dirty = controls_.menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || controls_.panel_.dirty; } else { navigate_controller(sample, now); } @@ -1074,7 +1073,7 @@ namespace streaming { SDL_Event event; // SDL_PeepEvents fills the event before each use. const Uint32 eventStart = SDL_GetTicks(); for (int count = 0; count < 128 && (count == 0 || SDL_GetTicks() - eventStart < 4) && SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) > 0; ++count) { - stop = handle_event(event, backend, active, statisticsEnabled) || stop; + stop = events_.handle_event(event, backend, active, statisticsEnabled) || stop; } if (stop) { router_.set_captured(true); @@ -1087,14 +1086,14 @@ namespace streaming { if (visible() && navigation_.inputDevice == input::UiInputDevice::keyboard) { const auto repeatedKey = menu_navigation_key(navigation_.keyboard.poll(now)); if (repeatedKey != SDLK_UNKNOWN) { - menu_key(repeatedKey, backend, active, statisticsEnabled); + events_.menu_key(repeatedKey, backend, active, statisticsEnabled); } } for (auto &device : devices_) { - update_device(device, active, now); + events_.update_device(device, active, now); } if (pulseSlot_ >= 0 && (static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { - release_pulse(); + events_.release_pulse(); } refresh_overlay(backend, font, width, height, wasVisible, screenshotChanged, statisticsEnabled); if (active) { @@ -1106,7 +1105,7 @@ namespace streaming { void StreamControls::release_host_input() { router_.release_all(); - release_pulse(); + events_.release_pulse(); } void StreamControls::render_screenshot_notification(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height) { diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index db10a09..ea44ab3 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -160,12 +160,6 @@ namespace streaming { * @param style Text alignment, background and color. */ void label(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, SDL_Rect bounds, const StreamLabelStyle &style = {}); - /** - * @brief Apply sampled or queued digital/stick navigation with shared repeat timing. - * @param sample Controller state, with positive stick Y upward. - * @param now Current SDL tick. - */ - void navigate_controller(const input::ControllerState &sample, Uint32 now); /** * @brief Independent controller state and shortcut latch. @@ -177,84 +171,11 @@ namespace streaming { bool chordDown = false; ///< Chord edge latch. }; - /** - * @brief Track deliberate gamepad activity and keyboard trigger shortcuts. - * @param event Queued SDL event. - */ - void note_gamepad_activity(const SDL_Event &event); - /** - * @brief Apply trigger edges while the keyboard owns this controller. - * @param axis Queued controller axis transition. - */ - void keyboard_trigger(const SDL_ControllerAxisEvent &axis); - /** - * @brief Recognize the original Xbox raw-button shortcut. - * @param button Queued joystick button transition. - */ - void joystick_button(const SDL_JoyButtonEvent &button); - /** - * @brief Route controller transitions before local menu activation. - * @param button Queued controller button transition. - * @param active Transport readiness. - */ - void route_controller_button(const SDL_ControllerButtonEvent &button, bool active); - /** - * @brief Navigate from the current sample of the controller owning the local menu. - */ - void navigate_owner_controller(); - /** - * @brief Handle controller release gates and local button activation. - * @param button Queued controller button transition. - * @param backend Streaming backend. - * @param active Transport readiness. - * @param statisticsEnabled Session statistics setting. - * @return True on a confirmed stream exit. - */ - bool controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); - /** - * @brief Apply one local controller button action. - * @param button SDL controller button. - * @param backend Streaming backend. - * @param active Transport readiness. - * @param statisticsEnabled Session statistics setting. - * @return True on a confirmed stream exit. - */ - bool activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); - /** - * @brief Apply queued stick movement before the next sampled state. - * @param axis Queued controller axis transition. - */ - void controller_axis(const SDL_ControllerAxisEvent &axis); - /** - * @brief Route physical keyboard input to local controls or the host. - * @param key Queued keyboard transition. - * @param backend Streaming backend. - * @param active Transport readiness. - * @param statisticsEnabled Session statistics setting. - * @return True on a confirmed stream exit. - */ - bool keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); - /** - * @brief Dispatch a single SDL event without delaying the bounded input batch. - * @param event Queued SDL event. - * @param backend Streaming backend. - * @param active Transport readiness. - * @param statisticsEnabled Session statistics setting. - * @return True on a confirmed stream exit or window close. - */ - bool handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled); /** * @brief Refresh live log entries while preserving a scrolled reading position. * @param now Monotonic millisecond tick. */ void refresh_logs(Uint32 now); - /** - * @brief Route and navigate one sampled controller. - * @param device Owned controller slot. - * @param active Transport readiness. - * @param now Monotonic millisecond tick. - */ - void update_device(Device &device, bool active, Uint32 now); /** * @brief Publish local controls or capture feedback through the shared overlay. * @param backend Streaming backend. @@ -266,11 +187,6 @@ namespace streaming { * @param stats Session statistics setting. */ void refresh_overlay(FfmpegStreamBackend &backend, TTF_Font *font, int width, int height, bool wasVisible, bool screenshotChanged, bool stats); - /** - * @brief Open the wheels from gameplay or any local page and release host inputs. - * @param device Controller instance ID, or -1 for keyboard control. - */ - void open_menu(int device); /** * @brief Draw cached colored controls with an opaque OSK and a translucent wheel backdrop. * @param backend Video backend. @@ -389,44 +305,145 @@ namespace streaming { * @param compact Use smaller glyphs inside keyboard keys, preserving footer sizing. */ void render_hint(SDL_Renderer *renderer, TTF_Font *font, const char *asset, const char *key, const char *text, SDL_Rect bounds, bool compact = false); + /** - * @brief Apply direct cancel, disconnect or quit-app input on the exit confirmation page. - * @param key SDL key code. - * @return True for a confirmed stream exit. - */ - bool confirm_exit_key(SDL_Keycode key); - /** - * @brief Apply a layout, modifier, docking or editing shortcut on the keyboard page. - * @param key SDL key code. - * @param active Transport readiness for host key taps. - * @return True when the keyboard consumes this shortcut. - */ - bool handle_keyboard_shortcut(SDL_Keycode key, bool active); - /** - * @brief Execute the selected wheel action or open its local page. - * @param backend Overlay and screenshot backend. - * @param active Transport readiness. - * @param stats Mutable session statistics setting. - */ - void activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats); - /** - * @brief Process a local navigation or activation key. - * @param key SDL key code. - * @param backend Video backend for screenshots. - * @param active Transport readiness. - * @param stats Mutable statistics toggle. - * @return True on confirmed quit. - */ - bool menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats); - /** - * @brief Tap a host key with the on-screen keyboard's modifier toggles. - * @param code Virtual key code. - */ - void tap_key(int code); - /** - * @brief End a Guide/Share pulse while restoring the latest gameplay state. - */ - void release_pulse(); + * @brief Process ordered input events using state owned by the enclosing stream session. + */ + class EventHandler { + public: + /** + * @brief Bind input processing to its owning session without copying SDL references. + * @param controls Session whose lifetime contains this handler. + */ + explicit EventHandler(StreamControls &controls) noexcept; + /** + * @brief Apply sampled or queued digital/stick navigation with shared repeat timing. + * @param sample Controller state, with positive stick Y upward. + * @param now Current SDL tick. + */ + void navigate_controller(const input::ControllerState &sample, Uint32 now) const; + /** + * @brief Track deliberate gamepad activity and keyboard trigger shortcuts. + * @param event Queued SDL event. + */ + void note_gamepad_activity(const SDL_Event &event) const; + /** + * @brief Apply trigger edges while the keyboard owns this controller. + * @param axis Queued controller axis transition. + */ + void keyboard_trigger(const SDL_ControllerAxisEvent &axis) const; + /** + * @brief Recognize the original Xbox raw-button shortcut. + * @param button Queued joystick button transition. + */ + void joystick_button(const SDL_JoyButtonEvent &button) const; + /** + * @brief Route controller transitions before local menu activation. + * @param button Queued controller button transition. + * @param active Transport readiness. + */ + void route_controller_button(const SDL_ControllerButtonEvent &button, bool active) const; + /** + * @brief Navigate from the current sample of the controller owning the local menu. + */ + void navigate_owner_controller() const; + /** + * @brief Handle controller release gates and local button activation. + * @param button Queued controller button transition. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit. + */ + bool controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const; + /** + * @brief Apply one local controller button action. + * @param button SDL controller button. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit. + */ + bool activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const; + /** + * @brief Apply queued stick movement before the next sampled state. + * @param axis Queued controller axis transition. + */ + void controller_axis(const SDL_ControllerAxisEvent &axis) const; + /** + * @brief Route physical keyboard input to local controls or the host. + * @param key Queued keyboard transition. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit. + */ + bool keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const; + /** + * @brief Dispatch a single SDL event without delaying the bounded input batch. + * @param event Queued SDL event. + * @param backend Streaming backend. + * @param active Transport readiness. + * @param statisticsEnabled Session statistics setting. + * @return True on a confirmed stream exit or window close. + */ + bool handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const; + /** + * @brief Route and navigate one sampled controller. + * @param device Owned controller slot. + * @param active Transport readiness. + * @param now Monotonic millisecond tick. + */ + void update_device(Device &device, bool active, Uint32 now) const; + /** + * @brief Open the wheels from gameplay or any local page and release host inputs. + * @param device Controller instance ID, or -1 for keyboard control. + */ + void open_menu(int device) const; + /** + * @brief Apply direct cancel, disconnect or quit-app input on the exit confirmation page. + * @param key SDL key code. + * @return True for a confirmed stream exit. + */ + bool confirm_exit_key(SDL_Keycode key) const; + /** + * @brief Apply a layout, modifier, docking or editing shortcut on the keyboard page. + * @param key SDL key code. + * @param active Transport readiness for host key taps. + * @return True when the keyboard consumes this shortcut. + */ + bool handle_keyboard_shortcut(SDL_Keycode key, bool active) const; + /** + * @brief Execute the selected wheel action or open its local page. + * @param backend Overlay and screenshot backend. + * @param active Transport readiness. + * @param stats Mutable session statistics setting. + */ + void activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats) const; + /** + * @brief Process a local navigation or activation key. + * @param key SDL key code. + * @param backend Video backend for screenshots. + * @param active Transport readiness. + * @param stats Mutable statistics toggle. + * @return True on confirmed quit. + */ + bool menu_key(SDL_Keycode key, FfmpegStreamBackend &backend, bool active, bool &stats) const; + /** + * @brief Tap a host key with the on-screen keyboard's modifier toggles. + * @param code Virtual key code. + */ + void tap_key(int code) const; + /** + * @brief End a Guide/Share pulse while restoring the latest gameplay state. + */ + void release_pulse() const; + + private: + StreamControls &controls_; ///< Session state updated by this handler's input operations. + }; + + EventHandler events_ {*this}; ///< Input processing bound to this session's stable lifetime. std::array devices_ {}; ///< Stable local controller slots. input::StreamInput router_; ///< Ordered event-thread host input. input::StreamMenu menu_; ///< Local menu state. diff --git a/src/streaming/video_overlay.cpp b/src/streaming/video_overlay.cpp index 8139df2..fef9e38 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -111,9 +111,15 @@ namespace { * @param width Bitmap width. * @param height Bitmap height. * @param pending Previously accumulated damage. - * @return Union of the changed pixel bounds and pending damage. + * @return Union of changed bounds and pending damage, or an empty rectangle for invalid storage. */ streaming::VideoRectangle argb_damage(const std::vector &before, const std::vector &after, int width, int height, const streaming::VideoRectangle &pending) { + if (width <= 0 || height <= 0 || width > 2048 || height > 2048) { + return {}; + } + if (const auto expected = static_cast(width) * height * 4; before.size() != expected || after.size() != expected) { + return {}; + } int left = width; int right = 0; int top = height; @@ -195,9 +201,9 @@ namespace streaming { * @param clip Optional repaint rectangle. * @return True when rasterization remains within the destination. */ - bool valid_menu_wheel(OverlayBitmap target, const MenuWheel &wheel, int sector, const VideoRectangle *clip) { - const auto [pixels, capacity, pitch, width, height] = target; - const auto [centerX, centerY, radius, selected] = wheel; + bool valid_menu_wheel(const OverlayBitmap &target, const MenuWheel &wheel, int sector, const VideoRectangle *clip) { + const auto &[pixels, capacity, pitch, width, height] = target; + const auto &[centerX, centerY, radius, selected] = wheel; if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048 || centerX < 0 || centerX >= width || centerY < 0 || centerY >= height || radius < 1 || radius > 2048 || sector < -1 || sector > 3) { return false; @@ -258,9 +264,9 @@ namespace streaming { return direction == selected ? 85 : 36; } - bool rasterize_menu_wheel(OverlayBitmap target, const MenuWheel &wheel, int sector, const VideoRectangle *clip) { - const auto [pixels, capacity, pitch, width, height] = target; - const auto [centerX, centerY, radius, selected] = wheel; + bool rasterize_menu_wheel(const OverlayBitmap &target, const MenuWheel &wheel, int sector, const VideoRectangle *clip) { + const auto &[pixels, capacity, pitch, width, height] = target; + const auto &[centerX, centerY, radius, selected] = wheel; if (!valid_menu_wheel(target, wheel, sector, clip)) { return false; } diff --git a/src/streaming/video_overlay.h b/src/streaming/video_overlay.h index b433da8..4a72e7e 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -72,7 +72,7 @@ namespace streaming { * @param clip Optional repaint rectangle restricting background restoration. * @return False for invalid storage or geometry, without changing destination pixels. */ - bool rasterize_menu_wheel(OverlayBitmap target, const MenuWheel &wheel, int sector = -1, const VideoRectangle *clip = nullptr); + bool rasterize_menu_wheel(const OverlayBitmap &target, const MenuWheel &wheel, int sector = -1, const VideoRectangle *clip = nullptr); /** * @brief Placement of a shared panel inside the presented video rectangle. diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 361d973..1033b58 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -1519,6 +1519,9 @@ namespace { std::size_t endIndex; ///< One past the last visible row. std::size_t selectedIndex; ///< Newly focused row. bool windowChanged; ///< The visible row range moved. + std::size_t visibleRowCount; ///< Maximum number of rows in the viewport. + bool overflow; ///< Whether the list needs a scrollbar. + int scrollBarWidth; ///< Scrollbar width, or zero when all rows fit. }; /** @@ -1552,128 +1555,141 @@ namespace { } /** - * @brief Draw scrollable action rows with rounded checkboxes, a filled selection and focus rail. - * - * @param renderer Active shell renderer. - * @param font Font for action labels. - * @param rows Actions and their focus state. - * @param rect Available list bounds. - * @param rowHeight Height of an action row. - * @param assetCache Optional cache for menu icons. - * @param active Whether this list currently owns controller focus. - * @param folder Optional retained folder viewport and visible label textures. - * @param repaint Repaint affected rows in the retained frame instead of the complete list. - * @return True when visible labels rendered successfully. + * @brief Calculate the visible action range while retaining folder-browser scroll position. + * @param rows Nonempty action list. + * @param rect List bounds. + * @param rowHeight Single row height. + * @param folder Previous folder viewport, or nullptr for centered ordinary menus. + * @return Row geometry, selected index and scrollbar state. */ - bool render_action_rows(SDL_Renderer *renderer, TTF_Font *font, const std::vector &rows, const SDL_Rect &rect, int rowHeight, const ActionRowsOptions &options = {}) { - const auto &[assetCache, active, folder, repaint] = options; - if (rows.empty()) { - return true; + ActionRowsLayout action_rows_layout(const std::vector &rows, const SDL_Rect &rect, int rowHeight, const FolderDialogCache *folder) { + constexpr int rowSpacing = 6; + const int rowStep = rowHeight + rowSpacing; + const auto visible = static_cast(std::max(1, (rect.h + rowSpacing) / std::max(1, rowStep))); + const auto selected = selected_action_index(rows); + std::size_t first = 0; + if (rows.size() > visible) { + first = selected > visible / 2 ? selected - visible / 2 : 0; + first = std::min(first, rows.size() - visible); } + if (folder != nullptr) { + first = ui::stable_list_first_row(rows.size(), selected, visible, folder->first); + } + const bool changed = folder != nullptr && first != folder->first; + const bool overflow = rows.size() > visible; + const int barWidth = overflow ? 8 : 0; + const int contentWidth = std::max(1, rect.w - (overflow ? barWidth + 10 : 0)); + return {rect, rowHeight, rowStep, contentWidth, first, std::min(rows.size(), first + visible), selected, changed, visible, overflow, barWidth}; + } - const int rowSpacing = 6; - const int rowStep = rowHeight + rowSpacing; - const auto visibleRowCount = static_cast(std::max(1, (rect.h + rowSpacing) / std::max(1, rowStep))); - std::size_t selectedIndex = 0U; - bool selectedIndexFound = false; - for (std::size_t index = 0; index < rows.size(); ++index) { - if (rows[index].selected) { - selectedIndex = index; - selectedIndexFound = true; - break; + /** + * @brief Release cached row labels that have scrolled out of view. + * @param folder Retained row textures. + * @param layout Current visible range. + */ + void prune_action_row_labels(FolderDialogCache &folder, const ActionRowsLayout &layout) { + for (auto it = folder.labels.begin(); it != folder.labels.end();) { + if (it->first < layout.startIndex || it->first >= layout.endIndex) { + clear_cached_text_texture(&it->second); + it = folder.labels.erase(it); + } else { + ++it; } } + } - std::size_t startIndex = 0U; - if (rows.size() > visibleRowCount && selectedIndexFound) { - const std::size_t centeredOffset = visibleRowCount / 2U; - startIndex = selectedIndex > centeredOffset ? selectedIndex - centeredOffset : 0U; - const std::size_t maxStartIndex = rows.size() - visibleRowCount; - if (startIndex > maxStartIndex) { - startIndex = maxStartIndex; + /** + * @brief Render one action's focus, icon, checkbox and retained or ordinary label. + * @param renderer Dialog renderer. + * @param font Action font. + * @param row Action content. + * @param index Row index in the complete list. + * @param layout Visible row geometry. + * @param options Icon, focus and cache settings. + * @return True when the label rendered successfully. + */ + bool render_action_row(SDL_Renderer *renderer, TTF_Font *font, const ui::ShellActionRow &row, std::size_t index, const ActionRowsLayout &layout, const ActionRowsOptions &options) { + auto *const assetCache = options.assetCache; + const bool active = options.active; + auto *const folder = options.folder; + const int y = layout.bounds.y + static_cast(index - layout.startIndex) * layout.rowStep; + const SDL_Rect rowRect {layout.bounds.x, y, layout.contentWidth, layout.rowHeight}; + if (row.selected) { + fill_rounded_rect(renderer, rowRect, {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, static_cast(active ? 0x28 : 0x12)}, 7); + if (active) { + fill_rect(renderer, {rowRect.x, rowRect.y + 8, 3, rowRect.h - 16}, ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE); + } + } + + int labelX = rowRect.x + 12; + if (assetCache != nullptr && !row.iconAssetPath.empty()) { + const int iconSize = std::min(24, rowRect.h - 16); + render_asset_icon(renderer, assetCache, row.iconAssetPath, {labelX, rowRect.y + (rowRect.h - iconSize) / 2, iconSize, iconSize}); + labelX += iconSize + 10; + } + const int checkboxSize = row.checkable ? std::min(22, rowRect.h - 16) : 0; + if (row.checkable) { + const SDL_Rect checkbox {rowRect.x + rowRect.w - checkboxSize - 14, rowRect.y + (rowRect.h - checkboxSize) / 2, checkboxSize, checkboxSize}; + if (row.checked) { + fill_rounded_rect(renderer, checkbox, {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}, 4); + for (int stroke = 0; stroke < 2; ++stroke) { + draw_line(renderer, checkbox.x + checkboxSize / 4, checkbox.y + checkboxSize / 2 + stroke, checkbox.x + checkboxSize * 5 / 12, checkbox.y + checkboxSize * 2 / 3 + stroke, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE); + draw_line(renderer, checkbox.x + checkboxSize * 5 / 12, checkbox.y + checkboxSize * 2 / 3 + stroke, checkbox.x + checkboxSize * 3 / 4, checkbox.y + checkboxSize / 3 + stroke, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE); + } + } else { + draw_rounded_rect_outline(renderer, checkbox, {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, 4); } } + const SDL_Color color = row.enabled ? SDL_Color {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF} : SDL_Color {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}; + const int labelWidth = rowRect.x + rowRect.w - labelX - 12 - (row.checkable ? checkboxSize + 16 : 0); + const int labelY = rowRect.y + (rowRect.h - TTF_FontLineSkip(font)) / 2; if (folder != nullptr) { - startIndex = ui::stable_list_first_row(rows.size(), selectedIndex, visibleRowCount, folder->first); + auto &label = folder->labels[index]; + if (!ensure_single_line_text_texture(renderer, font, row.label, color, labelWidth, &label) || !render_cached_text_texture(renderer, label, labelX, labelY)) { + return false; + } + } else if (!render_text_line_simple(renderer, font, row.label, color, labelX, labelY, labelWidth)) { + return false; } - const bool windowChanged = folder != nullptr && startIndex != folder->first; - const std::size_t endIndex = std::min(rows.size(), startIndex + visibleRowCount); - const bool overflow = rows.size() > visibleRowCount; - const int scrollBarWidth = overflow ? 8 : 0; - const int contentWidth = std::max(1, rect.w - (overflow ? scrollBarWidth + 10 : 0)); + return true; + } + /** + * @brief Render visible action rows using retained labels for folder navigation. + * @param renderer Dialog renderer. + * @param font Action font. + * @param rows Complete action list. + * @param rect List bounds. + * @param rowHeight Single row height. + * @param options Icon, focus and retained-cache settings. + * @return True when visible labels rendered successfully. + */ + bool render_action_rows(SDL_Renderer *renderer, TTF_Font *font, const std::vector &rows, const SDL_Rect &rect, int rowHeight, const ActionRowsOptions &options = {}) { + if (rows.empty()) { + return true; + } + const auto layout = action_rows_layout(rows, rect, rowHeight, options.folder); + auto *const folder = options.folder; if (folder != nullptr) { - if (repaint) { - restore_action_row_backgrounds(renderer, *folder, {rect, rowHeight, rowStep, contentWidth, startIndex, endIndex, selectedIndex, windowChanged}); - } - for (auto it = folder->labels.begin(); it != folder->labels.end();) { - if (it->first < startIndex || it->first >= endIndex) { - clear_cached_text_texture(&it->second); - it = folder->labels.erase(it); - } else { - ++it; - } + if (options.repaint) { + restore_action_row_backgrounds(renderer, *folder, layout); } + prune_action_row_labels(*folder, layout); } - for (std::size_t index = startIndex; index < endIndex; ++index) { - if (folder != nullptr && repaint && !windowChanged && index != folder->selected && index != selectedIndex) { + for (std::size_t index = layout.startIndex; index < layout.endIndex; ++index) { + if (folder != nullptr && options.repaint && !layout.windowChanged && index != folder->selected && index != layout.selectedIndex) { continue; } - const int y = rect.y + static_cast(index - startIndex) * rowStep; - const ui::ShellActionRow &row = rows[index]; - const SDL_Rect rowRect {rect.x, y, contentWidth, rowHeight}; - if (row.selected) { - fill_rounded_rect(renderer, rowRect, {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, static_cast(active ? 0x28 : 0x12)}, 7); - if (active) { - fill_rect(renderer, {rowRect.x, rowRect.y + 8, 3, rowRect.h - 16}, ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE); - } - } - - int labelX = rowRect.x + 12; - if (assetCache != nullptr && !row.iconAssetPath.empty()) { - const int iconSize = std::min(24, rowRect.h - 16); - render_asset_icon(renderer, assetCache, row.iconAssetPath, {labelX, rowRect.y + (rowRect.h - iconSize) / 2, iconSize, iconSize}); - labelX += iconSize + 10; - } - const int checkboxSize = row.checkable ? std::min(22, rowRect.h - 16) : 0; - if (row.checkable) { - const SDL_Rect checkbox {rowRect.x + rowRect.w - checkboxSize - 14, rowRect.y + (rowRect.h - checkboxSize) / 2, checkboxSize, checkboxSize}; - if (row.checked) { - fill_rounded_rect(renderer, checkbox, {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}, 4); - for (int stroke = 0; stroke < 2; ++stroke) { - draw_line(renderer, checkbox.x + checkboxSize / 4, checkbox.y + checkboxSize / 2 + stroke, checkbox.x + checkboxSize * 5 / 12, checkbox.y + checkboxSize * 2 / 3 + stroke, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE); - draw_line(renderer, checkbox.x + checkboxSize * 5 / 12, checkbox.y + checkboxSize * 2 / 3 + stroke, checkbox.x + checkboxSize * 3 / 4, checkbox.y + checkboxSize / 3 + stroke, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE); - } - } else { - draw_rounded_rect_outline(renderer, checkbox, {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, 4); - } - } - const SDL_Color color = row.enabled ? SDL_Color {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF} : SDL_Color {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}; - const int labelWidth = rowRect.x + rowRect.w - labelX - 12 - (row.checkable ? checkboxSize + 16 : 0); - const int labelY = rowRect.y + (rowRect.h - TTF_FontLineSkip(font)) / 2; - if (folder != nullptr) { - auto &label = folder->labels[index]; - if (!ensure_single_line_text_texture(renderer, font, row.label, color, labelWidth, &label) || !render_cached_text_texture(renderer, label, labelX, labelY)) { - return false; - } - } else if (!render_text_line_simple(renderer, font, row.label, color, labelX, labelY, labelWidth)) { + if (!render_action_row(renderer, font, rows[index], index, layout, options)) { return false; } } - - if (overflow && (!repaint || windowChanged)) { - render_vertical_scrollbar( - renderer, - {rect.x + rect.w - scrollBarWidth, rect.y, scrollBarWidth, rect.h}, - static_cast(rows.size()), - static_cast(visibleRowCount), - static_cast(startIndex) - ); + if (layout.overflow && (!options.repaint || layout.windowChanged)) { + render_vertical_scrollbar(renderer, {rect.x + rect.w - layout.scrollBarWidth, rect.y, layout.scrollBarWidth, rect.h}, static_cast(rows.size()), static_cast(layout.visibleRowCount), static_cast(layout.startIndex)); } - if (folder != nullptr) { - folder->first = startIndex; - folder->selected = selectedIndex; + folder->first = layout.startIndex; + folder->selected = layout.selectedIndex; } return true; } @@ -3973,17 +3989,14 @@ namespace { }; /** - * @brief Render a content-sized dialog with persistent guidance, scrolling actions or progress. - * - * @param renderer Active renderer. - * @param titleFont Dialog heading font. - * @param bodyFont Action label font. - * @param smallFont Supporting text font. - * @param assetCache Cached controller glyphs and action icons. + * @brief Render a shell modal and optionally retain its folder-list viewport. + * @param renderer Dialog renderer. + * @param fonts Heading, body and helper fonts. + * @param assetCache Cached action icons. * @param modal Dialog content. * @param width Logical display width. * @param height Framebuffer height. - * @param folder Optional folder cache to populate for incremental navigation. + * @param folder Optional retained folder state. * @return True when the dialog rendered successfully. */ bool render_shell_dialog(SDL_Renderer *renderer, const DialogFonts &fonts, AssetTextureCache *assetCache, const ui::ShellModalView &modal, int width, int height, FolderDialogCache *folder = nullptr) { @@ -4163,7 +4176,7 @@ namespace { } for (int y = area.y; y < area.y + area.h; ++y) { const auto *source = static_cast(surface.pixels) + static_cast(y) * surface.pitch + area.x * bytes; - auto *destination = static_cast(framebuffer) + (static_cast(y) * mode.width + area.x) * bytes; + auto *destination = framebuffer + (static_cast(y) * mode.width + area.x) * bytes; std::memcpy(destination, source, static_cast(area.w) * bytes); } if (locked) { From 1acadfd0873ab04b05a18b3695560c989ba1ed40 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:15:41 -0500 Subject: [PATCH 21/22] fix: clear remaining stream controls sonar findings --- src/app/screenshot_library.cpp | 3 +- src/platform/usb_input.cpp | 6 +- src/streaming/ffmpeg_stream_backend.cpp | 85 +++++++++++++------------ 3 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/app/screenshot_library.cpp b/src/app/screenshot_library.cpp index 6b505ef..dffaf55 100644 --- a/src/app/screenshot_library.cpp +++ b/src/app/screenshot_library.cpp @@ -311,8 +311,7 @@ namespace app { if (capturedAt < 0 || static_cast(time) != capturedAt) { return "Capture time unavailable"; } - std::tm utc {}; - if (!screenshot_utc_time(time, utc) || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", &utc) == 0) { + if (std::tm utc {}; !screenshot_utc_time(time, utc) || std::strftime(label.data(), label.size(), "%Y-%m-%d %H:%M:%S UTC", &utc) == 0) { return "Capture time unavailable"; } return label.data(); diff --git a/src/platform/usb_input.cpp b/src/platform/usb_input.cpp index c997ba9..2e78f09 100644 --- a/src/platform/usb_input.cpp +++ b/src/platform/usb_input.cpp @@ -153,15 +153,15 @@ namespace { return; } auto &connections = usb_state().connections; - const auto context = std::find_if(connections.begin(), connections.end(), [](const UsbConnection &entry) { + auto *context = std::find_if(connections.data(), connections.data() + connections.size(), [](const UsbConnection &entry) { return entry.id == 0; }); - if (context == connections.end()) { + if (context == connections.data() + connections.size()) { return; } const int id = usb_state().nextDevice++; context->id = id; - device->user_data = &*context; + device->user_data = context; enqueue({id, device->bProtocolCode}); if (usbh_hid_start_int_read(device, 0, receive) != 0) { enqueue({id, -1}); diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index c529028..d68fce4 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -337,8 +337,52 @@ namespace { } } + /** + * @brief Track actual completed BMP writes while retaining SDL's platform file implementation. + */ + struct ScreenshotFileProgress { + SDL_RWops *file; ///< Worker-owned file, closed after SDL finalizes the BMP header. + std::atomic &progress; ///< UI-readable percentage independent of file and surface lifetimes. + Sint64 expected; ///< Final 24-bit BMP length, including scanline padding and header. + Sint64 position = 0; ///< Current file position, including SDL's header fixup seeks. + Sint64 written = 0; ///< Highest written position, preventing header fixups from reducing progress. + }; + } // namespace +// SDL file callbacks require C linkage at global scope; static keeps them private to this file. +extern "C" { + /** + * @brief Delegate a BMP file seek while keeping progress accounting in sync. + * @param stream Progress-decorated SDL file. + * @param offset Byte offset relative to whence. + * @param whence SDL seek origin. + * @return New byte position or a negative value on error. + */ + static Sint64 screenshot_file_seek(SDL_RWops *stream, Sint64 offset, int whence) { + auto &writer = *static_cast(stream->hidden.unknown.data1); + writer.position = SDL_RWseek(writer.file, offset, whence); + return writer.position; + } + + /** + * @brief Forward one BMP write and publish progress only for bytes actually written. + * @param stream Progress-decorated SDL file. + * @param pixels Header or pixel bytes supplied by the SDL C writer callback. + * @param size Bytes per object. + * @param count Requested object count. + * @return Number of complete objects written. + */ + static std::size_t screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { + auto &writer = *static_cast(stream->hidden.unknown.data1); + const auto written = SDL_RWwrite(writer.file, pixels, size, count); + writer.position += static_cast(written * size); + writer.written = std::max(writer.written, writer.position); + writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected)); + return written; + } +} + namespace streaming { VideoAccelerationReport FfmpegStreamBackend::acceleration_report() const { @@ -473,47 +517,6 @@ namespace streaming { return screenshot_.thread != nullptr ? screenshot_.progress.load() : -1; } - /** - * @brief Track actual completed BMP writes while retaining SDL's platform file implementation. - */ - struct ScreenshotFileProgress { - SDL_RWops *file; ///< Worker-owned file, closed after SDL finalizes the BMP header. - std::atomic &progress; ///< UI-readable percentage independent of file and surface lifetimes. - Sint64 expected; ///< Final 24-bit BMP length, including scanline padding and header. - Sint64 position = 0; ///< Current file position, including SDL's header fixup seeks. - Sint64 written = 0; ///< Highest written position, preventing header fixups from reducing progress. - }; - - /** - * @brief Delegate a BMP file seek while keeping progress accounting in sync. - * @param stream Progress-decorated SDL file. - * @param offset Byte offset relative to whence. - * @param whence SDL seek origin. - * @return New byte position or a negative value on error. - */ - Sint64 screenshot_file_seek(SDL_RWops *stream, Sint64 offset, int whence) { - auto &writer = *static_cast(stream->hidden.unknown.data1); - writer.position = SDL_RWseek(writer.file, offset, whence); - return writer.position; - } - - /** - * @brief Forward one BMP write and publish progress only for bytes actually written. - * @param stream Progress-decorated SDL file. - * @param pixels Header or pixel bytes supplied by the SDL C writer callback. - * @param size Bytes per object. - * @param count Requested object count. - * @return Number of complete objects written. - */ - extern "C" std::size_t screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { - auto &writer = *static_cast(stream->hidden.unknown.data1); - const auto written = SDL_RWwrite(writer.file, pixels, size, count); - writer.position += static_cast(written * size); - writer.written = std::max(writer.written, writer.position); - writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected)); - return written; - } - /** * @brief Encode a BMP with byte-based progress, closing the file on every outcome. * @param surface Converted BGR screenshot. From 1b561483e3de7a195246e2a9467144bc333db3a4 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Tue, 17 Nov 2026 00:27:07 -0500 Subject: [PATCH 22/22] fix: declare SDL screenshot callbacks with C linkage --- src/streaming/ffmpeg_stream_backend.cpp | 62 ++++++++++++------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/streaming/ffmpeg_stream_backend.cpp b/src/streaming/ffmpeg_stream_backend.cpp index d68fce4..f4851ca 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -350,37 +350,35 @@ namespace { } // namespace -// SDL file callbacks require C linkage at global scope; static keeps them private to this file. -extern "C" { - /** - * @brief Delegate a BMP file seek while keeping progress accounting in sync. - * @param stream Progress-decorated SDL file. - * @param offset Byte offset relative to whence. - * @param whence SDL seek origin. - * @return New byte position or a negative value on error. - */ - static Sint64 screenshot_file_seek(SDL_RWops *stream, Sint64 offset, int whence) { - auto &writer = *static_cast(stream->hidden.unknown.data1); - writer.position = SDL_RWseek(writer.file, offset, whence); - return writer.position; - } +// SDL file callbacks use C linkage; project-prefixed names avoid global symbol collisions. +/** + * @brief Delegate a BMP file seek while keeping progress accounting in sync. + * @param stream Progress-decorated SDL file. + * @param offset Byte offset relative to whence. + * @param whence SDL seek origin. + * @return New byte position or a negative value on error. + */ +extern "C" Sint64 moonlight_screenshot_file_seek(SDL_RWops *stream, Sint64 offset, int whence) { + auto &writer = *static_cast(stream->hidden.unknown.data1); + writer.position = SDL_RWseek(writer.file, offset, whence); + return writer.position; +} - /** - * @brief Forward one BMP write and publish progress only for bytes actually written. - * @param stream Progress-decorated SDL file. - * @param pixels Header or pixel bytes supplied by the SDL C writer callback. - * @param size Bytes per object. - * @param count Requested object count. - * @return Number of complete objects written. - */ - static std::size_t screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { - auto &writer = *static_cast(stream->hidden.unknown.data1); - const auto written = SDL_RWwrite(writer.file, pixels, size, count); - writer.position += static_cast(written * size); - writer.written = std::max(writer.written, writer.position); - writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected)); - return written; - } +/** + * @brief Forward one BMP write and publish progress only for bytes actually written. + * @param stream Progress-decorated SDL file. + * @param pixels Header or pixel bytes supplied by the SDL C writer callback. + * @param size Bytes per object. + * @param count Requested object count. + * @return Number of complete objects written. + */ +extern "C" std::size_t moonlight_screenshot_file_write(SDL_RWops *stream, const void *pixels, std::size_t size, std::size_t count) { + auto &writer = *static_cast(stream->hidden.unknown.data1); + const auto written = SDL_RWwrite(writer.file, pixels, size, count); + writer.position += static_cast(written * size); + writer.written = std::max(writer.written, writer.position); + writer.progress.store(10 + static_cast(85 * std::min(writer.written, writer.expected) / writer.expected)); + return written; } namespace streaming { @@ -532,8 +530,8 @@ namespace streaming { ScreenshotFileProgress writer {file, progress, 54 + static_cast((surface.w * 3 + 3) & ~3) * surface.h}; SDL_RWops progressFile {}; progressFile.hidden.unknown.data1 = &writer; - progressFile.seek = screenshot_file_seek; - progressFile.write = screenshot_file_write; + progressFile.seek = moonlight_screenshot_file_seek; + progressFile.write = moonlight_screenshot_file_write; const bool encoded = SDL_SaveBMP_RW(&surface, &progressFile, 0) == 0; const bool closed = SDL_RWclose(file) == 0; const bool saved = encoded && closed;