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/README.md b/README.md index 47be3c5..c49fad9 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,10 @@ 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. +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. 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 +82,71 @@ 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. 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 | 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. +**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. + +![On-screen keyboard](./docs/images/screenshots/13-stream-keyboard.png) + +**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**, 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. 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 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; 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. +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. 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 @@ -89,6 +155,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..47036e3 100644 --- a/cmake/nxdk.cmake +++ b/cmake/nxdk.cmake @@ -230,10 +230,44 @@ function(_moonlight_prepare_nxdk_tools nxdk_dir cxbe_path source_dir build_dir o ) endfunction() +# 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) + 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) + # 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() + # 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_sdl_hooks("${nxdk_dir}" input_signature) + file(READ "${nxdk_dir}/.moonlight-source.signature" nxdk_revision) set(signature_inputs "NXDK_DIR=${nxdk_dir}" @@ -241,6 +275,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 +328,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..f1b74c8 100644 --- a/cmake/sources.cmake +++ b/cmake/sources.cmake @@ -14,12 +14,16 @@ 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" "${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..514bedc 100644 --- a/cmake/stage-lucide.cmake +++ b/cmake/stage-lucide.cmake @@ -5,8 +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) + activity + camera + chart-no-axes-combined + circle-check + circle-plus + circle-question-mark + circle-x + gamepad-2 + hash + info + key-round + keyboard + loader-circle + log-out + monitor + mouse + network + panels-top-left + pause + power + rotate-ccw + save + scroll-text + settings + share-2 +) set(_icon_source "${LUCIDE_SOURCE_DIR}/icons/${icon}.svg") if(NOT EXISTS "${_icon_source}") message(FATAL_ERROR "Required Lucide icon not found: ${_icon_source}") @@ -39,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) + add-host + circle-help + ethernet-port + gear + host-monitor-offline + host-monitor-online + host-monitor-pairing + life-buoy + monitor-check + monitor-dot + monitor-off + question-mark + support +) file(REMOVE "${LUCIDE_ASSET_DIR}/icons/${legacy_icon}.svg") endforeach() 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/docs/images/screenshots/12-stream-controls.png b/docs/images/screenshots/12-stream-controls.png new file mode 100644 index 0000000..9bdabad Binary files /dev/null and b/docs/images/screenshots/12-stream-controls.png differ diff --git a/docs/images/screenshots/13-stream-keyboard.png b/docs/images/screenshots/13-stream-keyboard.png new file mode 100644 index 0000000..1d10ade Binary files /dev/null and b/docs/images/screenshots/13-stream-keyboard.png differ diff --git a/docs/images/screenshots/14-screenshot-notification.png b/docs/images/screenshots/14-screenshot-notification.png new file mode 100644 index 0000000..9532163 Binary files /dev/null and b/docs/images/screenshots/14-screenshot-notification.png differ 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 20c1668..a9b1c31 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 { @@ -78,6 +80,11 @@ 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: @@ -86,6 +93,8 @@ namespace { 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"; } @@ -93,6 +102,11 @@ 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::display)) { return app::SettingsCategory::display; @@ -100,20 +114,30 @@ namespace { 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::display: - return "Tune streaming video resolution, frame rate, bitrate, audio playback, and the in-stream diagnostics overlay."; + 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."; } @@ -493,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,6 +529,7 @@ namespace { {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::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: @@ -536,6 +561,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 {}; @@ -587,6 +617,10 @@ namespace { true, }, }; + case app::SettingsCategory::storage: + return { + {"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 { {"input-placeholder", "Input settings are not implemented yet", "Input-specific options are planned, but there are no adjustable controller settings in this build yet.", true}, @@ -637,7 +671,17 @@ 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 = {}; + } + if (state.modal.id == app::ModalId::screenshot_viewer) { + state.screenshotViewer = {}; + } state.modal = {}; reset_confirmation(state); } @@ -713,20 +757,29 @@ 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: return 4U; case app::ModalId::app_actions: - return 3U; + return app::selected_app(state) != nullptr && app::selected_app(state)->running ? 5U : 4U; 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: case app::ModalId::app_details: case app::ModalId::log_viewer: case app::ModalId::connection_test: + case app::ModalId::screenshot_viewer: + case app::ModalId::quit_progress: return 0U; } return 0U; @@ -738,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; } @@ -1139,7 +1192,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. @@ -1150,7 +1203,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; @@ -1166,8 +1219,14 @@ 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; + 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; @@ -1358,7 +1417,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; @@ -1374,6 +1438,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; } @@ -1391,7 +1463,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) { @@ -1444,7 +1516,7 @@ namespace { } app::HostAppRecord &appRecord = mutableHost->apps[indices[state.apps.selectedAppIndex]]; - switch (state.modal.selectedActionIndex % 3U) { + switch (state.modal.selectedActionIndex) { case 0: appRecord.hidden = !appRecord.hidden; state.hosts.dirty = true; @@ -1465,6 +1537,24 @@ 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; + 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; } @@ -1486,6 +1576,9 @@ namespace { close_modal_and_mark_closed(state, update); return true; 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); @@ -1500,6 +1593,185 @@ namespace { return false; } + /** + * @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. + */ + 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; + 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(); + 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; + break; + case input::UiCommand::open_context_menu: + keyboard.toggle_shift(); + break; + case input::UiCommand::delete_character: + if (picker.caret > 0) { + picker.name.erase(--picker.caret, 1); + } + 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) { + 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; + } + 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; + 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 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. + * @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; @@ -1508,6 +1780,24 @@ 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 (handle_screenshot_viewer_command(state, command)) { + return true; + } + + if (handle_directory_picker_command(state, command, update)) { + return true; + } if (handle_log_viewer_modal_command(state, command, update)) { return true; @@ -1607,6 +1897,8 @@ namespace app { state.settings.playAudioOnPc = false; state.settings.showPerformanceStats = false; state.settings.playAudioOnXbox = true; + state.settings.controls.screenshotDirectory.clear(); + state.settings.controls.keyboardDockTop = false; state.settings.dirty = false; state.settings.savedFilesDirty = true; return state; @@ -2156,7 +2448,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. @@ -2236,6 +2528,14 @@ namespace app { rebuild_menu(state, "toggle-show-performance-stats"); return; } + if (detailUpdate.activatedItemId == "screenshot-directory") { + state.directoryPicker = {}; + 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; + return; + } if (detailUpdate.activatedItemId == "factory-reset") { open_confirmation( state, @@ -2601,6 +2901,83 @@ 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, std::string_view path) { + if (state.modal.id != ModalId::directory_picker) { + return; + } + if (state.settings.controls.screenshotDirectory != path) { + state.settings.controls.screenshotDirectory = path; + state.settings.dirty = true; + update.persistence.settingsChanged = true; + } + close_modal_and_mark_closed(state, &update); + 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, const 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 : 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 a601dfa..9326a35 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -9,15 +9,19 @@ #include #include #include +#include #include // 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" #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" #include "src/ui/menu_model.h" @@ -57,6 +61,9 @@ 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. + screenshot_viewer, ///< Local captures associated with the selected server and app. + quit_progress, ///< Authenticated host-app quit operation running in the background. }; /** @@ -81,8 +88,9 @@ namespace app { */ enum class SettingsCategory { logging, ///< Logging and diagnostics options. - display, ///< Display and video presentation 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. }; @@ -93,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. }; /** @@ -165,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. }; /** @@ -192,6 +204,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. @@ -343,6 +356,28 @@ 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 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. */ @@ -363,13 +398,35 @@ 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. + 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. }; /** - * @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 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. @@ -382,6 +439,9 @@ 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. + 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. }; @@ -414,6 +474,12 @@ 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. + 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. }; /** @@ -664,4 +730,34 @@ 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, 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. + * @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. + * @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, std::string_view path); + } // namespace app diff --git a/src/app/screenshot_library.cpp b/src/app/screenshot_library.cpp new file mode 100644 index 0000000..dffaf55 --- /dev/null +++ b/src/app/screenshot_library.cpp @@ -0,0 +1,319 @@ +/** + * @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 +#ifdef NXDK + #include "src/_nxdk_compat/include/moonlight_time.h" +#endif +#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 += R"(\\)"; + 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); + }); + } + + /** + * @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 { + 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 = 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 {}; + } + 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[0] != "D") || fields[9] != "end") { + return std::nullopt; + } + ScreenshotRecord record; + record.deleted = fields[0] == "D"; + unsigned int port = 0; + 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); + 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(); + if (const bool closed = std::fclose(file) == 0; !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()); + if (const bool complete = !line.empty() && line.back() == '\n'; !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; + } + 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) { + 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; + } + + 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(); + if (const bool closed = std::fclose(file) == 0; !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, {}); + 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 {}; + if (capturedAt < 0 || static_cast(time) != capturedAt) { + return "Capture time unavailable"; + } + 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(); + } +} // namespace app diff --git a/src/app/screenshot_library.h b/src/app/screenshot_library.h new file mode 100644 index 0000000..e7a9e6a --- /dev/null +++ b/src/app/screenshot_library.h @@ -0,0 +1,116 @@ +/** + * @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. + bool deleted = false; ///< Journal tombstone; never returned as a visible capture. + }; + + /** + * @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 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. + * @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/app/settings_storage.cpp b/src/app/settings_storage.cpp index ffa244c..c97b25e 100644 --- a/src/app/settings_storage.cpp +++ b/src/app/settings_storage.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,7 @@ extern "C" FILE *_wfopen(const wchar_t *path, const wchar_t *mode); #endif +// lib includes #include // local includes @@ -349,6 +351,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 +399,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 +500,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/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/stream_exit.cpp b/src/input/stream_exit.cpp index d3175c1..d08fbd7 100644 --- a/src/input/stream_exit.cpp +++ b/src/input/stream_exit.cpp @@ -1,9 +1,11 @@ /** * @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. */ +// 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 f1652b1..67ce003 100644 --- a/src/input/stream_exit.h +++ b/src/input/stream_exit.h @@ -1,16 +1,17 @@ /** * @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 +// standard includes #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 +20,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..812e497 --- /dev/null +++ b/src/input/stream_input.cpp @@ -0,0 +1,341 @@ +/** + * @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" + +// 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. + + /** + * @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_ = 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)) { + if (!equal(slots_[i].last, {})) { + send_controller(i, {}); + } + slots_[i].blocked = true; + } + } + } + + void StreamInput::tick(unsigned milliseconds) { + 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; + } + 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); + mouseX_ += mouse_axis(state.leftStickX) * 900 * seconds; + mouseY_ -= mouse_axis(state.leftStickY) * 900 * seconds; + 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) const { + if (code <= 0 || (captured_ && pressed)) { + return; + } + auto entry = held.find(code); + if (entry == held.end()) { + if (!pressed) { + return; + } + entry = held.try_emplace(code).first; + } + auto &sources = entry->second; + 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) const { + 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) const { + 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() { + mouseElapsed_ = 0; + 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; + } + 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 new file mode 100644 index 0000000..bd026ff --- /dev/null +++ b/src/input/stream_input.h @@ -0,0 +1,190 @@ +/** + * @file src/input/stream_input.h + * @brief Platform-neutral stream input routing and host input lifetime management. + */ +#pragma once + +// standard includes +#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 up to 125 reports/second, preserving fractional motion and immediate clicks. + * @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) const; + /** + * @brief Forward wheel ticks on both axes. + * @param x Horizontal ticks. + * @param y Vertical ticks. + */ + 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. + */ + 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) const; + 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. + unsigned mouseElapsed_ = 0; ///< Milliseconds accumulated toward the next 8 ms pointer report. + }; + + /** + * @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..55eaeb2 --- /dev/null +++ b/src/input/stream_menu.cpp @@ -0,0 +1,481 @@ +/** + * @file src/input/stream_menu.cpp + * @brief Implements radial selection and host keyboard navigation. + */ +// class header include +#include "src/input/stream_menu.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. + * @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) { + disconnectAction = StreamDisconnectAction::cancel; + owner = device; + page = StreamMenuPage::radial; + selected = StreamAction::none; + modifiers = 0; + logOffset = 0; + keyIndex = 10; + keyboardLayout = 0; + shiftOnce = false; + caps = 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; + } + 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; + 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::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) { + if (keyboardLayout == 0) { + return "?123"; + } + return keyboardLayout == 1 ? "PC keys" : "ABC"; + } + if (catalogIndex == KEYBOARD_CAPS_KEY) { + return "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 (binding_modifiers(catalogIndex) & MODIFIER_SHIFT) != 0 ? key.shifted : key.label; + } + + int StreamMenu::key_modifiers(int code) const { + 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); + } + + int StreamMenu::binding_modifiers(int index) const { + const auto &key = KEYS.at(index); + if (index >= 106) { + return key.modifiers; + } + // 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); + } + + bool StreamMenu::confirm_disconnect(StreamDisconnectAction action) { + if (page != StreamMenuPage::quit_confirmation) { + return false; + } + disconnectAction = action; + if (action == StreamDisconnectAction::cancel) { + page = StreamMenuPage::radial; + } + return action != StreamDisconnectAction::cancel; + } + + 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, + }}); + if (keyboardLayout == 0) { + return letters; + } + return 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 "Exit"; + 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..fd014e2 --- /dev/null +++ b/src/input/stream_menu.h @@ -0,0 +1,183 @@ +/** + * @file src/input/stream_menu.h + * @brief Testable dual-stick stream menu and on-screen keyboard navigation. + */ +#pragma once + +// standard includes +#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 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. + */ + 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 host modifiers, preserving explicit symbols-page bindings regardless of 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(); + /** + * @brief Complete the disconnect dialog directly, without directional selection. + * @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(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. + 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. + 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: + /** + * @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. + */ + 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. + }; + + /** + * @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..4b0dc47 --- /dev/null +++ b/src/input/ui_input.cpp @@ -0,0 +1,128 @@ +/** + * @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 + +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 ? 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); + } + + 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_ = now; + 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; + } + + 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..7a55169 --- /dev/null +++ b/src/input/ui_input.h @@ -0,0 +1,218 @@ +/** + * @file src/input/ui_input.h + * @brief Shared UI navigation timing and keyboard hold tracking. + */ +#pragma once + +// 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. + 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. + */ + 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 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. + * @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 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 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. + */ + 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. + */ + 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; + } + + /** + * @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/input/usb_hid.cpp b/src/input/usb_hid.cpp new file mode 100644 index 0000000..dbd8fb7 --- /dev/null +++ b/src/input/usb_hid.cpp @@ -0,0 +1,113 @@ +/** + * @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 + +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 { + 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::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) { + return; + } + auto &state = found->second; + if (state.protocol == 1) { + keyboard_report(state, bytes, length); + } else if (length >= 3) { + mouse_report(state, bytes); + } + } +} // namespace input diff --git a/src/input/usb_hid.h b/src/input/usb_hid.h new file mode 100644 index 0000000..ca86189 --- /dev/null +++ b/src/input/usb_hid.h @@ -0,0 +1,100 @@ +/** + * @file src/input/usb_hid.h + * @brief Safe USB boot keyboard and mouse report decoding. + */ +#pragma once + +// standard includes +#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) 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. + }; +} // namespace input diff --git a/src/main.cpp b/src/main.cpp index d889c44..5a1c9af 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" @@ -47,6 +49,8 @@ namespace { state.settings.playAudioOnPc = settings.playAudioOnPc; state.settings.showPerformanceStats = settings.showPerformanceStats; state.settings.playAudioOnXbox = settings.playAudioOnXbox; + 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 9bbc68d..68c261a 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 @@ -2712,6 +2711,44 @@ 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; + 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 + // 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/platform/filesystem_utils.cpp b/src/platform/filesystem_utils.cpp index a4a5f70..4ccc14b 100644 --- a/src/platform/filesystem_utils.cpp +++ b/src/platform/filesystem_utils.cpp @@ -6,10 +6,14 @@ #include "src/platform/filesystem_utils.h" // standard includes +#include +#include #include #include +#include #include #include +#include // platform includes #if defined(_WIN32) || defined(NXDK) @@ -18,12 +22,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 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]) + "\\"; + 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] == ':'; @@ -54,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; } @@ -71,6 +101,173 @@ 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; + } + 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; + } + 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::error_code(errno, std::generic_category()).message()); + 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::error_code(readError, std::generic_category()).message()); + } +#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(R"(<>:"/\|?*+=,;[])").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) + if (const int result = _mkdir(child.c_str()); result != 0) { +#else + if (const int result = mkdir(child.c_str(), 0750); result != 0) { +#endif + 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; + } + + 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::error_code(errno, std::generic_category()).message()); + 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; } @@ -149,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 994a85b..be4346a 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. @@ -61,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/platform/usb_input.cpp b/src/platform/usb_input.cpp new file mode 100644 index 0000000..2e78f09 --- /dev/null +++ b/src/platform/usb_input.cpp @@ -0,0 +1,260 @@ +/** + * @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 + +// 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 +// 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. + }; + + /** + * @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. + * @param report Boot report or lifecycle operation. + */ + void enqueue(const UsbReport &report) { + const KIRQL previous = KeRaiseIrqlToDpcLevel(); + if (const unsigned next = (usb_state().head + 1) % usb_state().reports.size(); next == usb_state().tail) { + usb_state().overflow = true; + } else { + usb_state().reports[usb_state().head] = report; + usb_state().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(device->user_data)->id; + 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; + } + auto &connections = usb_state().connections; + auto *context = std::find_if(connections.data(), connections.data() + connections.size(), [](const UsbConnection &entry) { + return entry.id == 0; + }); + if (context == connections.data() + connections.size()) { + 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; + } + } + + /** + * @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) { + auto *context = static_cast(device->user_data); + enqueue({context->id, -1}); + context->id = 0; + device->user_data = nullptr; + } + } + + /** + * @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 = 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 + +/** + * @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 = usb_state().overflow; + usb_state().overflow = false; + if (reset) { + usb_state().tail = usb_state().head; + } + const bool available = usb_state().tail != usb_state().head; + if (available) { + 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(); + reconnect_boot_devices(input); + } + 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..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 @@ -45,4 +49,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..f4851ca 100644 --- a/src/streaming/ffmpeg_stream_backend.cpp +++ b/src/streaming/ffmpeg_stream_backend.cpp @@ -2,13 +2,16 @@ * @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 #include #include #include +#include #include +#include #include #include #include @@ -31,7 +34,9 @@ extern "C" { // local includes #include "src/logging/logger.h" +#include "src/platform/filesystem_utils.h" +// nxdk includes #ifdef NXDK #include #endif @@ -332,8 +337,50 @@ 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 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. + */ +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 { VideoAccelerationReport FfmpegStreamBackend::acceleration_report() const { @@ -348,13 +395,201 @@ 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)); - statisticsBitmapChanged_ = statisticsTextureChanged_ = true; + 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::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_ = true; + statisticsTextureChanged_ = true; + statisticsTextureDamage_ = {0, 0, statisticsOverlay_.width(), statisticsOverlay_.height()}; 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; + } + } + } + + 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_ = true; + statisticsTextureChanged_ = true; + } + + 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::request_screenshot(std::string_view directory, const app::ScreenshotSource &source) { + if (const auto result = take_screenshot_result(); !result.empty()) { + logging::info("screenshots", result); + } + 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; + } + // 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_.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()); + } + return screenshot_.thread != nullptr; + } + + std::string FfmpegStreamBackend::take_screenshot_result() { + if (screenshot_.thread == nullptr || !screenshot_.complete.load()) { + return {}; + } + SDL_WaitThread(screenshot_.thread, nullptr); + screenshot_.thread = nullptr; + return std::exchange(screenshot_.result, {}); + } + + int FfmpegStreamBackend::screenshot_progress() const { + return screenshot_.thread != nullptr ? screenshot_.progress.load() : -1; + } + + /** + * @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 = 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; + 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::string error; + const std::string path = app::next_screenshot_path(job.directory, job.capturedAt, error); + 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 (surface != nullptr) { + job.progress.store(10); + saved = save_screenshot_bitmap(*surface, path, job.progress); + } + SDL_FreeSurface(surface); + if (saved) { + 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.complete.store(true); + return 0; } FfmpegStreamBackend::~FfmpegStreamBackend() { @@ -395,6 +630,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 { @@ -598,20 +839,27 @@ 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; + 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}; + 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; } - 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..260431d 100644 --- a/src/streaming/ffmpeg_stream_backend.h +++ b/src/streaming/ffmpeg_stream_backend.h @@ -9,16 +9,19 @@ #include #include #include +#include #include // lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + #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" -#include "third-party/moonlight-common-c/src/Limelight.h" struct AVCodecContext; struct AVFrame; @@ -71,7 +74,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(); @@ -97,9 +100,76 @@ 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, 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 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. + * @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. + * @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 set_statistics_bitmap(int width, int height, std::vector grayscale); + void render_local_overlay(SDL_Renderer *renderer, int width, int height); + + /** + * @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 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. + */ + 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. + * @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 +306,10 @@ namespace streaming { * @param destination Output video rectangle. */ void render_sdl_statistics(SDL_Renderer *renderer, const SDL_Rect &destination); + /** + * @brief Mark pixels changed, retaining the SDL texture when its dimensions still match. + */ + void invalidate_overlay_texture(); /** * @brief Record a retrieved frame before any client discard. * @@ -324,6 +398,33 @@ 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; ///< 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. + 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(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. */ @@ -394,12 +495,14 @@ 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. 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/pvideo_presenter.cpp b/src/streaming/pvideo_presenter.cpp index 8c927d6..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 @@ -269,7 +273,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..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" @@ -49,7 +50,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/screenshot_status.cpp b/src/streaming/screenshot_status.cpp new file mode 100644 index 0000000..e53254c --- /dev/null +++ b/src/streaming/screenshot_status.cpp @@ -0,0 +1,69 @@ +/** + * @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 +#include +#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; + 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 { + if (saving) { + return "Saving screenshot... " + std::to_string(percent) + "%"; + } + return saved ? "Screenshot saved" : "Screenshot failed"; + } + + std::string_view ScreenshotNotification::body() const { + if (saving) { + return {}; + } + std::string_view text = detail; + if (const std::string_view prefix = saved ? "Saved " : "Screenshot failed: "; 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 new file mode 100644 index 0000000..c538e47 --- /dev/null +++ b/src/streaming/screenshot_status.h @@ -0,0 +1,81 @@ +/** + * @file src/streaming/screenshot_status.h + * @brief Tracks screenshot notification progress and completion lifetime without SDL dependencies. + */ +#pragma once + +// 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. + */ + 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; + /** + * @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. + 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. + + 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 6c94ca8..72e4bd1 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -2,16 +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 "third-party/moonlight-common-c/src/Limelight.h" - +// standard includes #include #include #include @@ -22,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. @@ -64,7 +73,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 +84,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 +92,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 +152,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 +685,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 +1028,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.has_overlay(); + 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 (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); + if (wasVisible && !resources->controller.has_overlay()) { + resources->statisticsSampler = {}; } - - return 0; } /** @@ -1532,7 +1105,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.has_overlay() || !resources->statisticsEnabled || resources->statisticsFont == nullptr || !resources->mediaBackend.has_decoded_video()) { return false; } const auto now = PltGetMicroseconds(); @@ -1677,6 +1250,9 @@ namespace { render_text_lines(resources->renderer, resources->bodyFont, lines, screenWidth, &cursorY); + if (resources->controller.has_overlay() && mediaBackend != nullptr) { + mediaBackend->render_local_overlay(resources->renderer, screenWidth, screenHeight); + } SDL_RenderPresent(resources->renderer); render_stream_input_diagnostics(resources); return true; @@ -1792,9 +1368,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 +1416,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 +1433,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 +1490,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 +1513,7 @@ namespace streaming { logging::error("stream", initializationError); return false; } + resources.controller.configure(settings.controls.screenshotDirectory, settings.controls.keyboardDockTop, app::screenshot_source(host, app)); ScopedThreadPriority streamThreadPriority(SDL_THREAD_PRIORITY_HIGH); startup::log_memory_statistics(); @@ -2086,9 +1621,17 @@ 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 (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"; } @@ -2097,6 +1640,10 @@ namespace streaming { logging::info("stream", line); } startup::log_memory_statistics(); + 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); 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..48840d6 --- /dev/null +++ b/src/streaming/stream_controls.cpp @@ -0,0 +1,1588 @@ +/** + * @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" + +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; + } + } + +} // namespace + +namespace streaming { + StreamControls::EventHandler::EventHandler(StreamControls &controls) noexcept: + controls_(controls) {} + + void StreamControls::clear_panel() { + 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, const StreamLabelStyle &style) { + const auto &[leading, background, fitSingleLine, foreground] = style; + if (background) { + SDL_SetRenderDrawColor(renderer, 16, 16, 16, 255); + SDL_RenderFillRect(renderer, &bounds); + } + if (text.empty()) { + return; + } + 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 = 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 (panel_.textBytes + bytes > 1024U * 1024U) { + SDL_RenderFlush(renderer); + panel_.text.clear(); + panel_.textBytes = 0; + } + std::shared_ptr texture(SDL_CreateTextureFromSurface(renderer, surface.get()), SDL_DestroyTexture); + if (!texture) { + return; + } + 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 = 0; + 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, raster.texture.get(), &source, &destination); + } + + 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; + 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 (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) { + controls_.navigation_.neutralRequired = false; + } + return; + } + const auto command = input::direction_command(x, y); + 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 && !controls_.navigation_.applied) { + controls_.navigation_.applied = true; + const auto [dx, dy] = input::navigation_step(menu_navigation_command(key)); + controls_.menu_.navigate(dx, dy); + controls_.panel_.dirty = true; + } + } + + StreamControls::StreamControls(): + 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(); + } + + 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, app::ScreenshotSource source) { + screenshotDirectory_ = std::move(screenshotDirectory); + keyboardTop_ = keyboardTop; + screenshotSource_ = std::move(source); + } + + bool StreamControls::keyboard_docked_top() const { + return keyboardTop_; + } + + bool StreamControls::quit_app_requested() const { + return menu_.disconnectAction == input::StreamDisconnectAction::quit_app; + } + + bool StreamControls::has_overlay() const { + return visible() || screenshotNotification_.visible; + } + + 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); + } + } + + void StreamControls::close() { + clear_panel(); + panel_.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) { + events_.release_pulse(); + router_.disconnect(instance); + if (mouseDevice_ == instance) { + mouseDevice_ = -1; + } + if (menu_.owner == instance) { + menu_.owner = -1; + navigation_.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::EventHandler::open_menu(int device) const { + if (controls_.menu_.page == input::StreamMenuPage::radial) { + return; + } + 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::EventHandler::tap_key(int code) const { + if (code == 0) { + return; + } + const int keyModifiers = controls_.menu_.key_modifiers(code); + // No gameplay sample is forwarded during this event-thread-only local key tap. + 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) { + controls_.router_.key(-3, keys[i], 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::EventHandler::confirm_exit_key(SDL_Keycode key) const { + if (key == SDLK_RETURN) { + return controls_.menu_.confirm_disconnect(input::StreamDisconnectAction::disconnect); + } + if (key == SDLK_q) { + return controls_.menu_.confirm_disconnect(input::StreamDisconnectAction::quit_app); + } + if (key == SDLK_ESCAPE) { + controls_.panel_.dirty = true; + return controls_.menu_.confirm_disconnect(input::StreamDisconnectAction::cancel); + } + return false; + } + + bool StreamControls::EventHandler::handle_keyboard_shortcut(SDL_Keycode key, bool active) const { + if (key == SDLK_TAB) { + controls_.menu_.toggle_keyboard_page(); + return true; + } + if (key == SDLK_LSHIFT || key == SDLK_RSHIFT) { + controls_.menu_.toggle_shift(); + return true; + } + if (key == SDLK_CAPSLOCK) { + controls_.menu_.caps = !controls_.menu_.caps; + return true; + } + if (key == SDLK_F2) { + controls_.keyboardTop_ = !controls_.keyboardTop_; + return true; + } + if (key == SDLK_BACKSPACE || key == SDLK_SPACE) { + if (active) { + tap_key(key == SDLK_SPACE ? 0x20 : 0x08); + } + return true; + } + return false; + } + + void StreamControls::EventHandler::activate_stream_action(FfmpegStreamBackend &backend, bool active, bool &stats) const { + switch (controls_.menu_.selected) { + case input::StreamAction::quit: + controls_.menu_.page = input::StreamMenuPage::quit_confirmation; + break; + case input::StreamAction::keyboard: + controls_.menu_.page = input::StreamMenuPage::keyboard; + controls_.navigation_.controller.reset(); + controls_.navigation_.neutralRequired = true; + break; + case input::StreamAction::logs: + controls_.menu_.page = input::StreamMenuPage::logs; + controls_.navigation_.controller.reset(); + controls_.navigation_.neutralRequired = true; + controls_.panel_.logs.clear(); + for (const auto &entry : logging::snapshot()) { + controls_.panel_.logs.push_back(logging::format_entry(entry)); + } + if (controls_.panel_.logs.empty()) { + controls_.panel_.logs = logging::load_log_file().lines; + } + break; + case input::StreamAction::mouse: + if (controls_.mouseDevice_ < 0 && controls_.menu_.owner < 0) { + controls_.panel_.message = "Open the menu with a controller to enable mouse mode."; + break; + } + 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; + controls_.menu_.close(); + break; + case input::StreamAction::screenshot: + { + if (backend.request_screenshot(controls_.screenshotDirectory_, controls_.screenshotSource_)) { + controls_.screenshotNotification_.begin(); + } else if (backend.screenshot_progress() < 0) { + controls_.screenshotNotification_.finish(false, "Could not start capture. Check the stream log for details.", SDL_GetTicks()); + } + 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 ? controls_.router_.connect(controls_.menu_.owner) : -1; + if (slot < 0) { + controls_.panel_.message = "A connected controller and active stream are required."; + break; + } + release_pulse(); + 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); + controls_.panel_.message = controls_.menu_.selected == input::StreamAction::guide ? "Guide sent" : "Share sent (requires host support)"; + controls_.menu_.close(); + break; + } + case input::StreamAction::none: + break; + } + } + + 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); + } + controls_.panel_.dirty = true; + if (key == SDLK_ESCAPE) { + if (controls_.menu_.page == input::StreamMenuPage::radial) { + controls_.menu_.close(); + } else { + controls_.menu_.page = input::StreamMenuPage::radial; + controls_.menu_.modifiers = 0; + controls_.menu_.shiftOnce = false; + } + return false; + } + 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 && 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)); + controls_.menu_.navigate(dx, dy); + } + return false; + } + if (controls_.menu_.page == input::StreamMenuPage::keyboard) { + if (active) { + tap_key(controls_.menu_.keyboard_key()); + } + return false; + } + if (controls_.menu_.page != input::StreamMenuPage::radial) { + return false; + } + activate_stream_action(backend, active, stats); + return false; + } + + /** + * @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; + } + } + + 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 (controls_.visible() && gamepadActivity && controls_.menu_.owner < 0) { + controls_.menu_.owner = event.type == SDL_CONTROLLERBUTTONDOWN ? event.cbutton.which : event.caxis.which; + } + 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::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 && !controls_.navigation_.pageTriggerDown && controls_.navigation_.armed) { + controls_.menu_.toggle_keyboard_page(); + controls_.panel_.dirty = true; + } + controls_.navigation_.pageTriggerDown = pressed; + } else if (axis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) { + if (pressed && !controls_.navigation_.dockTriggerDown && controls_.navigation_.armed) { + controls_.keyboardTop_ = !controls_.keyboardTop_; + controls_.panel_.dirty = true; + } + controls_.navigation_.dockTriggerDown = pressed; + } + } + + 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; + } + 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; + } + } + } + + 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; + } + 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) { + controls_.router_.connect(button.which); + controls_.router_.controller(button.which, device.sample); + } + } + } + + 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::EventHandler::controller_button(const SDL_ControllerButtonEvent &button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { + route_controller_button(button, active); + 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 (controls_.visible() && controls_.navigation_.armed && button.state == SDL_PRESSED) { + if (controls_.menu_.owner < 0) { + controls_.menu_.owner = button.which; + } + if (button.which != controls_.menu_.owner) { + return false; + } + return activate_controller_button(button.button, backend, active, statisticsEnabled); + } + return false; + } + + bool StreamControls::EventHandler::activate_controller_button(Uint8 button, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { + switch (button) { + case SDL_CONTROLLER_BUTTON_A: + return menu_key(SDLK_RETURN, backend, active, statisticsEnabled); + case SDL_CONTROLLER_BUTTON_B: + 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 (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 (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 && controls_.menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x20); + } + break; + case SDL_CONTROLLER_BUTTON_LEFTSTICK: + if (controls_.menu_.page == input::StreamMenuPage::keyboard) { + controls_.menu_.caps = !controls_.menu_.caps; + controls_.panel_.dirty = true; + } + break; + case SDL_CONTROLLER_BUTTON_RIGHTSTICK: + 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 && controls_.menu_.page == input::StreamMenuPage::keyboard) { + tap_key(button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER ? 0x125 : 0x127); + } + break; + case SDL_CONTROLLER_BUTTON_START: + if (active && controls_.menu_.page == input::StreamMenuPage::keyboard) { + tap_key(0x0D); + } + break; + default: + break; + } + return false; + } + + 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 : controls_.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 (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()); + } + } + } + } + + /** + * @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::EventHandler::keyboard_event(const SDL_KeyboardEvent &key, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { + bool stop = false; + const bool down = key.type == SDL_KEYDOWN; + 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); + controls_.navigation_.armed = true; + return false; + } + if (controls_.visible()) { + if (!down) { + controls_.navigation_.keyboard.release(key.keysym.scancode); + } + if (!down || key.repeat != 0) { + return false; + } + { + 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 (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) { + controls_.navigation_.keyboard.reset(); + } + stop = menu_key(key.keysym.sym, backend, active, statisticsEnabled) || stop; + } + } + return stop; + } + if (active && key.repeat == 0) { + const int mods = host_keyboard_modifiers(key.keysym.mod); + controls_.router_.key(0, input::keyboard_virtual_key(key.keysym.scancode), down, mods); + } + 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::EventHandler::handle_event(const SDL_Event &event, FfmpegStreamBackend &backend, bool active, bool &statisticsEnabled) const { + note_gamepad_activity(event); + switch (event.type) { + case SDL_CONTROLLERDEVICEADDED: + controls_.open_if_needed(event.cdevice.which); + break; + case SDL_CONTROLLERDEVICEREMOVED: + controls_.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) { + controls_.router_.mouse_move(event.motion.xrel, event.motion.yrel); + } + break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + if (active) { + 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; + controls_.router_.scroll(event.wheel.x * direction, event.wheel.y * direction); + } + break; + case SDL_WINDOWEVENT: + if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { + controls_.focused_ = false; + controls_.navigation_.keyboard.reset(); + controls_.router_.set_captured(true); + } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { + controls_.focused_ = true; + controls_.router_.set_captured(controls_.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 != 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(panel_.logs.size())); + } + panel_.logs = std::move(lines); + panel_.dirty = true; + } + } + } + + void StreamControls::EventHandler::update_device(Device &device, bool active, Uint32 now) const { + if (device.handle == nullptr) { + return; + } + 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) { + controls_.router_.connect(instance); + controls_.router_.controller(instance, device.sample); + } else { + controls_.router_.disconnect(instance); + } + } + 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) { + controls_.navigation_.armed = true; + } + if (!controls_.navigation_.armed) { + return; + } + 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. + controls_.panel_.dirty = controls_.menu_.sample_sticks(sample.leftStickX, sample.leftStickY, sample.rightStickX, sample.rightStickY) || controls_.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(); + 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 && 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 = events_.handle_event(event, backend, active, statisticsEnabled) || stop; + } + if (stop) { + router_.set_captured(true); + return true; + } + 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) { + events_.menu_key(repeatedKey, backend, active, statisticsEnabled); + } + } + for (auto &device : devices_) { + events_.update_device(device, active, now); + } + if (pulseSlot_ >= 0 && (static_cast(now - pulseUntil_) >= 0 || (router_.active_mask() & (1U << pulseSlot_)) == 0)) { + events_.release_pulse(); + } + refresh_overlay(backend, font, width, height, wasVisible, screenshotChanged, statisticsEnabled); + if (active) { + router_.tick(now - lastTick_); + } + lastTick_ = now; + return stop; + } + + void StreamControls::release_host_input() { + router_.release_all(); + events_.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 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; + } + if (!panel_.surface || panel_.surface->w != panelWidth || panel_.surface->h != panelHeight) { + clear_panel(); + panel_.surface.reset(SDL_CreateRGBSurfaceWithFormat(0, panelWidth, panelHeight, 32, SDL_PIXELFORMAT_ARGB8888)); + if (panel_.surface) { + panel_.renderer.reset(SDL_CreateSoftwareRenderer(panel_.surface.get())); + } + } + if (!panel_.renderer) { + return; + } + 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}); + 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); + 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); + 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) { + 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 = 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 = panel_.icons.try_emplace(key, std::move(raster)).first; + } + if (!found->second) { + return false; + } + const auto &surface = found->second; + 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 = 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) { + 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; + } + + 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 = 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; + 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_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) { + 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 = "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]; + } + 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 : {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); + panel_.damage.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); + 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}); + } + for (int i = 0; i < static_cast(cells.size()); ++i) { + 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}; + render_keyboard_key(renderer, font, i, key); + } + 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); + 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}); + } + } + + /** + * @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; + } + 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) + safeBottom) : height; + if (width <= 0 || height <= 0) { + return; + } + if (!prepare_panel(width, height)) { + return; + } + 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; + } + panel_.damage.clear(); + const Uint8 background = keyboard ? 16 : 0; + if (!focusOnly) { + SDL_SetRenderDrawColor(renderer.get(), background, background, background, keyboard ? 255 : 150); + SDL_RenderClear(renderer.get()); + } + 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}); + } + if (keyboard) { + render_keyboard(renderer.get(), font, width, height, rowHeight, focusOnly); + } else if (menu_.page == input::StreamMenuPage::radial) { + render_wheels(renderer.get(), font, width, height, stats, focusOnly); + } else if (menu_.page == input::StreamMenuPage::logs) { + render_logs(renderer.get(), font, width, height); + } else { + render_exit_confirmation(renderer.get(), font, width, height); + } + 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}); + } + 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 new file mode 100644 index 0000000..ea44ab3 --- /dev/null +++ b/src/streaming/stream_controls.h @@ -0,0 +1,505 @@ +/** + * @file src/streaming/stream_controls.h + * @brief SDL input devices and local controls for an active stream. + */ +#pragma once + +// standard includes +#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; + + /** + * @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. + */ + 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. + * @param source Stable host and app metadata for saved screenshots. + */ + void configure(std::string screenshotDirectory, bool keyboardTop, app::ScreenshotSource source = {}); + /** + * @brief Read the placement selected while 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. + */ + 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 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. + * @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 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. + */ + 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 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 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 Refresh live log entries while preserving a scrolled reading position. + * @param now Monotonic millisecond tick. + */ + void refresh_logs(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 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. + * @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 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 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. + * @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, 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, const SDL_Rect *clip = nullptr); + /** + * @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 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. + bool focused_ = true; ///< Window focus state. + int mouseDevice_ = -1; ///< Virtual mouse controller, or disabled. + 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. + 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. + /** + * @brief Content dependencies other than the focused key or wheel sector. + */ + using PanelContent = std::tuple; + + /** + * @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 be8aea3..fef9e38 100644 --- a/src/streaming/video_overlay.cpp +++ b/src/streaming/video_overlay.cpp @@ -1,10 +1,13 @@ /** * @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. */ +// class header include #include "src/streaming/video_overlay.h" +// standard includes #include +#include #include #include @@ -19,24 +22,425 @@ 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; + } + + /** + * @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 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; + 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 { - 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, std::vector bitmap) { + if (!valid_storage(pixels, capacity, pitch, width, height, 4) || width > 2048 || height > 2048) { + return {}; + } + 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; + const unsigned opacity = pixel[3]; + const auto index = (static_cast(row) * width + column) * 4; + bitmap[index + 3] = opacity; + 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] = opaqueBackground ? 255 : opacity; + continue; + } + if (opacity == 0) { + bitmap[index] = 0; + bitmap[index + 1] = 0; + bitmap[index + 2] = 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; + } + + /** + * @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(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; + } + 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(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; + } + 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; + if ((sector == 0 && y > 0) || (sector == 2 && y < 0)) { + continue; + } + const auto [start, end] = wheel_columns(wheel, y, firstColumn, lastColumn, sector); + for (int column = start; column <= end; ++column) { + const int x = column - centerX; + if (const int distance = x * x + y * y; distance > radius * radius || distance < radius * radius / 9) { + continue; + } + const int direction = wheel_sector(x, y); + if (sector >= 0 && sector != direction) { + continue; + } + const auto gray = wheel_shade(x, y, direction, selected); + auto *pixel = pixels + static_cast(row) * pitch + column * 4; + pixel[0] = gray; + pixel[1] = gray; + pixel[2] = gray; + pixel[3] = 255; + } + } + 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; + } + 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) { + changed = argb_damage(argb_, pixels, width, height, damage_); + } + width_ = width; + height_ = height; + placement_ = placement; + argb_ = std::move(pixels); + std::vector().swap(grayscale_); + std::vector().swap(opacity_); + opaque_ = opaque; + if (reuse) { + rgb_.clear(); + rgbBpp_ = 0; + damage_ = changed; + } else { + invalidate_caches(); + } + } + + 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; + } + + 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; + 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; + } + + void VideoOverlay::invalidate_caches() { + damage_ = {0, 0, width_, height_}; 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 +452,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,56 +504,183 @@ 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); + continue; + } + { + 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()) { - return true; - } - if (!valid_storage(pixels, capacity, pitch, frameWidth, frameHeight, 2) || (frameWidth & 1) != 0 || frameWidth > 2046 || frameHeight > 2047 || destination.width <= MARGIN || destination.height <= MARGIN) { - return false; - } - if (frameWidth_ != frameWidth || frameHeight_ != frameHeight || outputWidth_ != destination.width || outputHeight_ != destination.height) { + 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; 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 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); + 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; + return false; } yuy2_.resize(static_cast(source_.width) * source_.height * 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; - } + if (!opaque_) { + yuy2Opacity_.resize(yuy2_.size() / 2); + } + } + 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) { - 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); + continue; + } + { + 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) { + 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); + 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); + } + } + } + } + + 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 596bc3f..4a72e7e 100644 --- a/src/streaming/video_overlay.h +++ b/src/streaming/video_overlay.h @@ -1,24 +1,141 @@ /** * @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 +// local includes #include "src/streaming/video_acceleration.h" 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. + * @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 bitmap = {}); + + /** + * @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 Writable ARGB bitmap view with validated row storage. + */ + 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(const OverlayBitmap &target, const MenuWheel &wheel, int sector = -1, const VideoRectangle *clip = nullptr); + + /** + * @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); + void set_bitmap(int width, int height, std::vector pixels, VideoOverlayPlacement placement = VideoOverlayPlacement::top_left, std::vector opacity = {}); + /** + * @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. + * @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. + * @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. + * @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 +149,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 +188,72 @@ namespace streaming { private: /** - * @brief Convert the grayscale bitmap only when its pixel depth changes. + * @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. + * @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. + 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. + 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..642e5bb --- /dev/null +++ b/src/ui/asset_path.cpp @@ -0,0 +1,21 @@ +/** + * @file src/ui/asset_path.cpp + * @brief Resolves bundled asset paths consistently across UI screens. + */ +// class header include +#include "src/ui/asset_path.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; + // 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..88c12ce --- /dev/null +++ b/src/ui/asset_path.h @@ -0,0 +1,17 @@ +/** + * @file src/ui/asset_path.h + * @brief Shared Xbox asset paths for shell and streaming UI. + */ +#pragma once + +// standard includes +#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..727ead6 --- /dev/null +++ b/src/ui/dialog_surface.h @@ -0,0 +1,51 @@ +/** + * @file src/ui/dialog_surface.h + * @brief Shared dialog surface for shell and in-stream modals on both SDL render paths. + */ +#pragma once + +// standard includes +#include +#include + +// lib includes +#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/screenshot_image.cpp b/src/ui/screenshot_image.cpp new file mode 100644 index 0000000..a7e86c2 --- /dev/null +++ b/src/ui/screenshot_image.cpp @@ -0,0 +1,59 @@ +/** + * @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); + // 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 (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 {}; + } + 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)}; + } + + 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_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 eae1fc5..1033b58 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -12,24 +12,27 @@ #include #include #include +#include #include #include #include #include #include -// nxdk includes -#include -#include -#include +// lib includes #include #include #include + +// nxdk includes +#include #include // NOSONAR(cpp:S3806) nxdk requires lowercase header names // 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" #include "src/network/host_discovery.h" @@ -44,9 +47,13 @@ #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/screenshot_image.h" #include "src/ui/shell_layout.h" #include "src/ui/shell_view.h" +#include "src/ui/svg_assets.h" namespace { @@ -75,20 +82,18 @@ 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 = 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 +130,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 +160,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; @@ -356,9 +241,40 @@ 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 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; ///< 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. std::vector lines; ///< Last body lines used to populate the cache. @@ -367,6 +283,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, @@ -440,7 +366,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. */ @@ -449,6 +387,9 @@ namespace { return; } + 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); @@ -635,7 +576,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 +718,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 @@ -1214,6 +1153,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; @@ -1225,14 +1172,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; } @@ -1355,7 +1295,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); @@ -1501,15 +1447,8 @@ namespace { return false; } - const int hintHeight = std::max(30, TTF_FontLineSkip(smallFont) + 10); - 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)) { + const int hintHeight = measure_footer_actions_height(smallFont, viewModel.modal.footerActions, modalRect.w - 36); + 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; } @@ -1559,104 +1498,199 @@ 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. - * @return True when visible labels rendered successfully. + * @brief Optional icon, focus and retained-cache settings for action rows. */ - 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 - ) { - if (rows.empty()) { - return true; + 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. + 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. + }; + + /** + * @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); + } + } } + } - const int rowSpacing = 6; + /** + * @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 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. + */ + 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 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; - } + 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}; + } - 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 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; } } - 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; - for (std::size_t index = startIndex; index < endIndex; ++index) { - 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); - } + /** + * @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); + 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); } - 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; } - - y += rowStep; + } else if (!render_text_line_simple(renderer, font, row.label, color, labelX, labelY, labelWidth)) { + return false; } + return true; + } - if (overflow) { - render_vertical_scrollbar( - renderer, - {rect.x + rect.w - scrollBarWidth, rect.y, scrollBarWidth, rect.h}, - static_cast(rows.size()), - static_cast(visibleRowCount), - static_cast(startIndex) - ); + /** + * @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 (options.repaint) { + restore_action_row_backgrounds(renderer, *folder, layout); + } + prune_action_row_labels(*folder, layout); + } + 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; + } + if (!render_action_row(renderer, font, rows[index], index, layout, options)) { + return false; + } + } + 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 = layout.startIndex; + folder->selected = layout.selectedIndex; } - return true; } @@ -1695,11 +1729,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 +1756,80 @@ 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.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; + 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 +1852,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 +1877,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 +2087,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 +2459,44 @@ 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 && 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, @@ -2407,6 +2509,8 @@ namespace { state.settings.playAudioOnPc, state.settings.showPerformanceStats, state.settings.playAudioOnXbox, + state.settings.controls.screenshotDirectory, + state.settings.controls.keyboardDockTop, }; } @@ -2762,6 +2866,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; @@ -3154,6 +3269,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 {}; @@ -3758,7 +3878,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}); } /** @@ -3860,19 +3980,27 @@ namespace { } /** - * @brief Render a content-sized dialog with a status icon and contained 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 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 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 retained folder state. * @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, 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; @@ -3894,7 +4022,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; @@ -3903,7 +4034,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); @@ -3917,7 +4053,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. @@ -3934,12 +4070,13 @@ 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); 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)); @@ -3990,8 +4127,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; @@ -4002,8 +4144,163 @@ 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 = 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. + * @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 + 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 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); + if (copy_folder_framebuffer(*surface, static_cast(framebuffer), mode, area, bytes)) { + return; + } + } + } +#else + (void) window; + (void) logicalDamage; + (void) logicalWidth; +#endif + SDL_RenderPresent(renderer); + } + + /** + * @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); + } + 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; + } + if (SDL_Surface *surface = SDL_LoadBMP(path.c_str()); 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 Compose a television-safe shell frame from compact navigation and page content. + * @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; + } + } + 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); + 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. @@ -4039,6 +4336,31 @@ 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.visible && !viewModel.modal.busy && !viewModel.modal.actions.empty() && !viewModel.logViewer.visible && !viewModel.screenshot.visible && !viewModel.overlay.visible && !viewModel.keypad.visible; + if (folder != nullptr) { + 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}); + 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); @@ -4053,6 +4375,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); @@ -4120,15 +4446,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; } @@ -4200,18 +4518,26 @@ 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); - } - } else if (viewModel.modal.visible) { - if (!render_shell_dialog(renderer, titleFont, bodyFont, smallFont, assetCache, viewModel.modal, screenWidth, screenHeight)) { - return false; + 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 && !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; } 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; } @@ -4246,34 +4572,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 +4585,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 +4633,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; } @@ -4340,13 +4655,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"); } } @@ -4358,7 +4681,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"); } @@ -4445,6 +4770,43 @@ 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. + * @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, @@ -4456,12 +4818,13 @@ 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); 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 +4833,33 @@ 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; + } + 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()); + 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; } @@ -4484,7 +4871,8 @@ namespace { SDL_GameController **controller, ShellInputState *inputState, const ProcessCommand &processCommand, - bool *skipPolledControllerNavigation + bool *skipPolledControllerNavigation, + bool *redrawRequested = nullptr ) { SDL_Event event; if (skipPolledControllerNavigation != nullptr) { @@ -4495,6 +4883,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); @@ -4557,7 +4948,57 @@ 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); + 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); + 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); + 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()) { + 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. @@ -4578,7 +5019,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 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. @@ -4586,6 +5027,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. @@ -4622,21 +5064,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; } /** @@ -4711,6 +5143,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(); @@ -4753,6 +5186,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); @@ -4803,9 +5237,22 @@ namespace { return; } + const auto ready = [](const auto &task) { + 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) { + 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); @@ -4902,6 +5349,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..."; @@ -4936,7 +5386,30 @@ 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); + 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); + 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); @@ -4964,13 +5437,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) { + 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, notification lifetimes, background tasks and redraws. + * @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. @@ -4985,7 +5458,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) { @@ -4998,16 +5473,35 @@ 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); + 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 { + 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()); - 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)) { + if ((runtime->keypadRedrawRequested || ui::shell_requires_animation(state)) && !draw_current_shell_frame(videoMode != nullptr ? *videoMode : VIDEO_MODE {}, state, resources, runtime)) { return false; } @@ -5038,6 +5532,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 a7d1354..0c903a6 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -7,9 +7,13 @@ // standard includes #include +#include #include #include +// local includes +#include "src/platform/filesystem_utils.h" + namespace { std::string format_file_size(std::uint64_t sizeBytes) { @@ -158,7 +162,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) { @@ -201,6 +205,11 @@ 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: @@ -209,6 +218,8 @@ namespace { return "Display"; case app::SettingsCategory::input: return "Input"; + case app::SettingsCategory::storage: + return "Storage"; case app::SettingsCategory::reset: return "Reset"; } @@ -342,6 +353,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), @@ -368,7 +384,12 @@ namespace { return lines; } if (state.settings.selectedCategory == app::SettingsCategory::display) { - lines.emplace_back("Display options will be added here."); + 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; } @@ -410,6 +431,9 @@ namespace { 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"; } @@ -540,19 +564,24 @@ 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. */ 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}, + {"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"}); + } } } @@ -593,6 +622,67 @@ 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"; + 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()) { + view.lines.push_back(picker.error); + } else if (!picker.path.empty() && picker.folders.empty()) { + view.lines.emplace_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. * @@ -621,12 +711,43 @@ 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; 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::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}, + {"delete-screenshot", "Delete", "icons\\button-x.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"); @@ -668,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}); @@ -705,9 +826,127 @@ 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(std::string_view 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 { + 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) { + 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. @@ -835,6 +1074,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..6340815 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. }; /** @@ -90,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. }; /** @@ -151,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; ///< 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. @@ -158,6 +165,20 @@ namespace ui { std::vector footerActions; ///< Footer actions displayed while a modal is open. }; + /** + * @brief Check whether any action dialog can retain its pixels while list selection changes. + * @param previous Previously painted dialog. + * @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_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. */ @@ -170,7 +191,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. @@ -182,9 +203,20 @@ 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. }; + /** + * @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. */ @@ -196,6 +228,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/src/ui/svg_assets.cpp b/src/ui/svg_assets.cpp new file mode 100644 index 0000000..81c9726 --- /dev/null +++ b/src/ui/svg_assets.cpp @@ -0,0 +1,105 @@ +/** + * @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 + +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..7d8e10e --- /dev/null +++ b/src/ui/svg_assets.h @@ -0,0 +1,21 @@ +/** + * @file src/ui/svg_assets.h + * @brief Shared SVG rasterization for the shell and streaming controls. + */ +#pragma once + +// standard includes +#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/CMakeLists.txt b/tests/CMakeLists.txt index d344033..541f231 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -44,6 +44,8 @@ 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/") +list(FILTER TEST_SOURCES EXCLUDE REGEX "/tests/xbox/") add_executable(${PROJECT_NAME} ${TEST_SOURCES} @@ -114,3 +116,33 @@ 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}) + +# 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 new file mode 100644 index 0000000..6c3014e --- /dev/null +++ b/tests/controller/shell_controller_test.cpp @@ -0,0 +1,233 @@ +/** + * @file tests/controller/shell_controller_test.cpp + * @brief Verifies recovery from removed virtual gamepads and preservation of live menu-controller references. + */ +// test header include +#include "src/input/shell_controller.h" + +// standard includes +#include +#include +#include + +// lib includes +#include + +/** + * @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. + }; + + /** + * @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. + */ + 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(); + 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) 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) 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() { + ++registry().enumerationCalls; + return static_cast(registry().available.size()); + } + + SDL_bool SDL_IsGameController(int index) { + return registry().devices[registry().available[index]].mapped ? SDL_TRUE : SDL_FALSE; + } + + SDL_GameController *SDL_GameControllerOpen(int index) { + const int id = registry().available[index]; + auto &device = registry().devices[id]; + if (!device.openable) { + return nullptr; + } + ++device.references; + ++device.opens; + if (device.removedWhileOpening) { + device.attached = false; + } + return ®istry().handles[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(const SDL_GameController *controller) { + return registry().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); + registry().available = {1, 0}; + EXPECT_FALSE(input::recover_shell_controller(&active)); + 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); + } + + /** + * @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, ®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(registry().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, ®istry().handles[1]); + EXPECT_EQ(registry().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, ®istry().handles[1]); + EXPECT_EQ(registry().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); + } + 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, ®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); + } + + /** + * @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(registry().enumerationCalls, 0); + SDL_GameController *active = nullptr; + EXPECT_FALSE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, nullptr); + attach(0); + registry().devices[0].removedWhileOpening = true; + EXPECT_FALSE(input::recover_shell_controller(&active)); + EXPECT_EQ(active, nullptr); + 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 new file mode 100644 index 0000000..4dbe108 --- /dev/null +++ b/tests/support/shell_controller/SDL.h @@ -0,0 +1,56 @@ +/** + * @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 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. + */ +struct _SDL_GameController; +/** + * @brief SDL-compatible public controller handle name. + */ +using 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(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(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/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index b536b5a..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 { @@ -842,6 +843,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 +870,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); @@ -1509,6 +1512,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 new file mode 100644 index 0000000..7bfd044 --- /dev/null +++ b/tests/unit/app/directory_picker_test.cpp @@ -0,0 +1,264 @@ +/** + * @file tests/unit/app/directory_picker_test.cpp + * @brief Verifies staged screenshot destination selection and the local folder-name keyboard. + */ +// test header include +#include "src/app/client_state.h" + +// standard includes +#include +#include + +// lib includes +#include + +// local includes +#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(std::string_view configured = {}) { + auto state = app::create_initial_state(); + 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); + 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); + 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); + 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.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"); + 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.controls.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.controls.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.controls.screenshotDirectory, "F:\\Old"); + app::accept_screenshot_directory(state, update, state.directoryPicker.path); + EXPECT_EQ(state.settings.controls.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.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.controls.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.controls.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"); +} + +/** + * @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.controls.screenshotDirectory, "F:\\Screenshots"); +} diff --git a/tests/unit/app/menu_actions_test.cpp b/tests/unit/app/menu_actions_test.cpp new file mode 100644 index 0000000..8baa252 --- /dev/null +++ b/tests/unit/app/menu_actions_test.cpp @@ -0,0 +1,224 @@ +/** + * @file tests/unit/app/menu_actions_test.cpp + * @brief Verifies retained action menus, confirmed capture deletion and host-app termination. + */ +// test header include +#include "src/app/client_state.h" + +// standard includes +#include + +// lib includes +#include + +// local includes +#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 new file mode 100644 index 0000000..18f1748 --- /dev/null +++ b/tests/unit/app/screenshot_library_test.cpp @@ -0,0 +1,318 @@ +/** + * @file tests/unit/app/screenshot_library_test.cpp + * @brief Verifies capture provenance, journal recovery, unique filenames and app-scoped navigation. + */ +// 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/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. + */ + 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(), 4U); + EXPECT_EQ(view.modal.footerActions[0].keyLabel, "PgUp"); + EXPECT_EQ(view.modal.footerActions[1].keyLabel, "PgDn"); + 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); + 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); + } + + /** + * @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 std::filesystem::path first = app::next_screenshot_path(root, 10, error); + { + std::ofstream image(first); + image << "capture"; + } + 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()); + 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/app/settings_storage_test.cpp b/tests/unit/app/settings_storage_test.cpp index c0b15c9..bf8fd24 100644 --- a/tests/unit/app/settings_storage_test.cpp +++ b/tests/unit/app/settings_storage_test.cpp @@ -247,3 +247,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 = 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); + 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..d5be02d 100644 --- a/tests/unit/input/navigation_input_test.cpp +++ b/tests/unit/input/navigation_input_test.cpp @@ -2,7 +2,7 @@ * @file tests/unit/input/navigation_input_test.cpp * @brief Verifies controller navigation input handling. */ -// class header include +// test header include #include "src/input/navigation_input.h" // lib includes @@ -10,6 +10,9 @@ 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..97b4a17 100644 --- a/tests/unit/input/stream_exit_test.cpp +++ b/tests/unit/input/stream_exit_test.cpp @@ -1,20 +1,21 @@ /** * @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 +// test header include +#include "src/input/stream_exit.h" // standard includes #include #include -// local includes -#include "src/input/stream_exit.h" +// lib includes #include "third-party/moonlight-common-c/src/Limelight.h" +#include + /** - * @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..e9dc844 --- /dev/null +++ b/tests/unit/input/stream_input_test.cpp @@ -0,0 +1,328 @@ +/** + * @file tests/unit/input/stream_input_test.cpp + * @brief Verifies independent gamepads, capture lifetimes and merged pointer/keyboard input. + */ +// test header include +#include "src/input/stream_input.h" + +// standard includes +#include +#include + +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include + +/** + * @brief Captures ordered transport packets without a host connection. + */ +class StreamInputTest: public testing::Test { +public: + 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; + } +} + +/** + * @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([&reports, &distance](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 new file mode 100644 index 0000000..11afc08 --- /dev/null +++ b/tests/unit/input/stream_menu_test.cpp @@ -0,0 +1,329 @@ +/** + * @file tests/unit/input/stream_menu_test.cpp + * @brief Verifies dual-stick sectors, dead zones, keyboard modifiers and log navigation. + */ +// test header include +#include "src/input/stream_menu.h" + +// standard includes +#include +#include + +// lib includes +#include "third-party/moonlight-common-c/src/Limelight.h" + +#include + +/** + * @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'); +} + +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. + */ +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); + } + cover_keyboard_cell(coverage, cell); + } + 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 is accepted directly and arrows cannot change A/B semantics. + */ +TEST(StreamMenuTest, RequiresExplicitDisconnectAndCancelsDirectly) { + input::StreamMenu menu; + EXPECT_FALSE(menu.confirm_disconnect(input::StreamDisconnectAction::disconnect)); + menu.open(1); + 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(input::StreamDisconnectAction::cancel)); + EXPECT_EQ(menu.page, input::StreamMenuPage::radial); + menu.page = input::StreamMenuPage::quit_confirmation; + 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"); +} + +/** + * @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"); +} + +/** + * @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. + */ +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..9035492 --- /dev/null +++ b/tests/unit/input/ui_input_test.cpp @@ -0,0 +1,273 @@ +/** + * @file tests/unit/input/ui_input_test.cpp + * @brief Verifies keyboard navigation holds, input-source arbitration and repeat-safe commands. + */ +// test header include +#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. + */ +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 = 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); + 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); +} + +/** + * @brief A gamepad replacement clears stale controller input while an independently held keyboard key keeps repeating. + */ +TEST(ShellInputStateTest, ControllerHandoffPreservesKeyboardAndResetsGamepadHolds) { + input::ShellInputState state; + 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); + 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. + */ +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); +} + +/** + * @brief Both stick axes use the main menu dead zone, retain a noisy hold and release without a second tap. + */ +TEST(DirectionNavigationTest, StickFlicksStepOnceWithHysteresisOnBothAxes) { + 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; + 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(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); + } +} + +/** + * @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/input/usb_hid_test.cpp b/tests/unit/input/usb_hid_test.cpp new file mode 100644 index 0000000..de35be8 --- /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 header include +#include "src/input/usb_hid.h" + +// standard includes +#include +#include + +// lib includes +#include + +/** + * @brief Records decoded HID operations without USB hardware. + */ +class BootHidTest: public testing::Test { +public: + 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/network/host_quit_test.cpp b/tests/unit/network/host_quit_test.cpp new file mode 100644 index 0000000..f2400a5 --- /dev/null +++ b/tests/unit/network/host_quit_test.cpp @@ -0,0 +1,172 @@ +/** + * @file tests/unit/network/host_quit_test.cpp + * @brief Verifies authenticated cancellation, stale-target protection and host quit verification. + */ +// test header include +#include "src/network/host_pairing.h" + +// standard includes +#include +#include + +// lib includes +#include + +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 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. + */ + 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 *) { + return handle_http_request(request, response, error); + }); + } + + /** + * @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 = 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"; + 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/platform/filesystem_utils_test.cpp b/tests/unit/platform/filesystem_utils_test.cpp index 659a495..0892d72 100644 --- a/tests/unit/platform/filesystem_utils_test.cpp +++ b/tests/unit/platform/filesystem_utils_test.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include // lib includes @@ -84,4 +85,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 diff --git a/tests/unit/startup/storage_paths_test.cpp b/tests/unit/startup/storage_paths_test.cpp new file mode 100644 index 0000000..9404cd5 --- /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 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. + */ +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/screenshot_status_test.cpp b/tests/unit/streaming/screenshot_status_test.cpp new file mode 100644 index 0000000..86e4a3f --- /dev/null +++ b/tests/unit/streaming/screenshot_status_test.cpp @@ -0,0 +1,113 @@ +/** + * @file tests/unit/streaming/screenshot_status_test.cpp + * @brief Verifies persistent save progress and timed screenshot completion feedback. + */ +// test header include +#include "src/streaming/screenshot_status.h" + +// standard includes +#include +#include + +// lib includes +#include + +/** + * @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)); +} + +/** + * @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 ea5ac28..c869f7a 100644 --- a/tests/unit/streaming/video_overlay_test.cpp +++ b/tests/unit/streaming/video_overlay_test.cpp @@ -2,16 +2,432 @@ * @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 #include #include #include +#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. + */ +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. + */ +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] = 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); + 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] = 50; + 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 +448,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 +609,255 @@ 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); + } +} + +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}) { + ASSERT_NO_FATAL_FAILURE(compare_recolored_overlay(scale, matrix)); + } + } +} + +/** + * @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); +} + +/** + * @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(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] = 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); + } + 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)); + } +} diff --git a/tests/unit/ui/asset_path_test.cpp b/tests/unit/ui/asset_path_test.cpp new file mode 100644 index 0000000..1e88fe6 --- /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 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. + */ +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..4ac9383 --- /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 header include +#include "src/ui/shell_view.h" + +// standard includes +#include +#include + +// lib includes +#include + +/** + * @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/tests/unit/ui/screenshot_image_test.cpp b/tests/unit/ui/screenshot_image_test.cpp new file mode 100644 index 0000000..a014a73 --- /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 header include +#include "src/ui/screenshot_image.h" + +// standard includes +#include +#include +#include + +// lib includes +#include + +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_layout_test.cpp b/tests/unit/ui/shell_layout_test.cpp index 30d1385..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 { @@ -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..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 { @@ -150,7 +151,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 +159,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"); } /** @@ -510,7 +513,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); @@ -522,7 +525,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"); } @@ -550,12 +553,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"); } @@ -875,3 +878,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_action_dialog_content(original, changed)); + const auto reject = [&](auto mutate) { + auto other = original; + mutate(other); + EXPECT_FALSE(ui::same_action_dialog_content(original, other)); + EXPECT_FALSE(ui::same_action_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_action_dialog_content(original, ui::build_shell_view_model(state, {}).modal)); +} 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()); +} diff --git a/xbe/assets/README.md b/xbe/assets/README.md index 2685e6d..2c064c0 100644 --- a/xbe/assets/README.md +++ b/xbe/assets/README.md @@ -15,3 +15,15 @@ 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 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 new file mode 100644 index 0000000..af696c4 --- /dev/null +++ b/xbe/assets/icons/button-guide.svg @@ -0,0 +1 @@ +Xbox diff --git a/xbe/assets/icons/button-left-stick.svg b/xbe/assets/icons/button-left-stick.svg new file mode 100644 index 0000000..b40f232 --- /dev/null +++ b/xbe/assets/icons/button-left-stick.svg @@ -0,0 +1 @@ + diff --git a/xbe/assets/icons/button-right-stick.svg b/xbe/assets/icons/button-right-stick.svg new file mode 100644 index 0000000..623006f --- /dev/null +++ b/xbe/assets/icons/button-right-stick.svg @@ -0,0 +1 @@ + diff --git a/xbe/assets/icons/button-share.svg b/xbe/assets/icons/button-share.svg new file mode 100644 index 0000000..caf60c5 --- /dev/null +++ b/xbe/assets/icons/button-share.svg @@ -0,0 +1 @@ + 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/.