diff --git a/src/input.cpp b/src/input.cpp index a12514d4e..4c1660835 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -177,6 +177,26 @@ namespace input { static std::function sink; return sink; } + + /** + * @brief Return the optional callback for observing queued packet dispatch in tests. + * + * @return Mutable test callback. + */ + std::function &input_packet_hook() { + static std::function hook; + return hook; + } + + /** + * @brief Return the optional callback for observing input task scheduling in tests. + * + * @return Mutable test callback. + */ + std::function)> &input_task_sink() { + static std::function)> sink; + return sink; + } #endif static std::bitset gamepadMask {}; @@ -298,6 +318,7 @@ namespace input { std::list> input_queue; ///< Validated input packets waiting for processing. std::mutex input_queue_lock; ///< Input queue lock. + bool input_task_scheduled {}; ///< Whether one worker task owns packet dispatch for this stream; guarded by input_queue_lock. thread_pool_util::ThreadPool::task_id_t mouse_left_button_timeout; ///< Mouse left button timeout. @@ -2020,10 +2041,34 @@ namespace input { } /** - * @brief Called on a thread pool thread to process an input message. - * @param input The input context pointer. + * @brief Process a bounded batch of queued packets for one stream. + * + * @param input Retained stream input state. */ - void passthrough_next_message(std::shared_ptr input) { + void passthrough_queued_messages(std::shared_ptr input); + + /** + * @brief Queue one packet-processing task for a stream. + * + * @param input Retained stream input state. + */ + void schedule_input_packet_task(const std::shared_ptr &input) { +#ifdef SUNSHINE_TESTS + if (const auto &sink = input_task_sink(); sink) { + sink(input); + return; + } +#endif + task_pool.push(passthrough_queued_messages, input); + } + + /** + * @brief Process one queued input packet for a stream. + * + * @param input Retained stream input state. + * @return True if a packet was processed, or false when the stream queue is empty. + */ + bool passthrough_next_message(std::shared_ptr &input) { // 'entry' backs the 'payload' pointer, so they must remain in scope together std::vector entry; PNV_INPUT_HEADER payload; @@ -2036,7 +2081,8 @@ namespace input { // If all entries have already been processed, nothing to do if (input->input_queue.empty()) { - return; + input->input_task_scheduled = false; + return false; } // Pop off the first entry, which we will send @@ -2067,6 +2113,12 @@ namespace input { // Print the final input packet input::print((void *) payload); +#ifdef SUNSHINE_TESTS + if (const auto &hook = input_packet_hook(); hook) { + hook(util::endian::little(payload->magic)); + } +#endif + // Send the batched input to the OS switch (util::endian::little(payload->magic)) { case MOUSE_MOVE_REL_MAGIC_GEN5: @@ -2114,6 +2166,31 @@ namespace input { passthrough(input, (PSS_CONTROLLER_BATTERY_PACKET) payload); break; } + return true; + } + + void passthrough_queued_messages(std::shared_ptr input) { + // Bound each turn so other task-pool work can run during sustained input. + constexpr std::size_t packets_per_task = 32; + for (std::size_t processed = 0; processed < packets_per_task; ++processed) { + if (!passthrough_next_message(input)) { + return; + } + } + + // Keep ownership until the next task is queued. New packets arriving meanwhile + // see input_task_scheduled and do not create a competing worker. + bool has_more; + { + std::lock_guard lock {input->input_queue_lock}; + has_more = !input->input_queue.empty(); + if (!has_more) { + input->input_task_scheduled = false; + } + } + if (has_more) { + schedule_input_packet_task(input); + } } /** @@ -2136,11 +2213,18 @@ namespace input { return; } + bool schedule_task = false; { - std::lock_guard lg(input->input_queue_lock); + std::lock_guard lock {input->input_queue_lock}; input->input_queue.push_back(std::move(input_data)); + if (!input->input_task_scheduled) { + input->input_task_scheduled = true; + schedule_task = true; + } + } + if (schedule_task) { + schedule_input_packet_task(input); } - task_pool.push(passthrough_next_message, input); } /** @@ -2360,6 +2444,18 @@ namespace input { return input->gamepads[client_index].id; } + void set_input_packet_hook(std::function hook) { + input_packet_hook() = std::move(hook); + } + + void set_input_task_sink(std::function)> sink) { + input_task_sink() = std::move(sink); + } + + void process_queued_messages(std::shared_ptr input) { + ::input::passthrough_queued_messages(std::move(input)); + } + void set_keyboard_sink(std::function sink) { keyboard_sink() = std::move(sink); } diff --git a/src/input.h b/src/input.h index d383e425b..6d78447e5 100644 --- a/src/input.h +++ b/src/input.h @@ -112,6 +112,26 @@ namespace input { */ int gamepad_id(const std::shared_ptr &input, std::uint8_t client_index); + /** + * @brief Install a callback called just before dispatching a queued input packet in a test. + * @param hook Callback receiving the packet magic, or empty to remove it. + */ + void set_input_packet_hook(std::function hook); + + /** + * @brief Redirect input task scheduling to a test callback. + * + * @param sink Callback receiving the stream whose packet task was queued, or empty to restore the worker pool. + */ + void set_input_task_sink(std::function)> sink); + + /** + * @brief Process queued input packets on the calling thread in a test. + * + * @param input Retained stream input state. + */ + void process_queued_messages(std::shared_ptr input); + /** * @brief Keyboard event Sunshine emitted toward the platform backend. */ diff --git a/tests/unit/test_input.cpp b/tests/unit/test_input.cpp index e88547218..10c5461ac 100644 --- a/tests/unit/test_input.cpp +++ b/tests/unit/test_input.cpp @@ -9,11 +9,14 @@ // standard includes #include #include +#include #include #include +#include #include #include #include +#include #include // moonlight-common-c includes @@ -83,6 +86,8 @@ namespace { * @brief Destroy retained test sessions and restore configuration. */ void TearDown() override { + input::testing::set_input_packet_hook({}); + input::testing::set_input_task_sink({}); input::terminate_gamepads(); input::testing::set_platform_input({}); context_ = nullptr; @@ -250,6 +255,112 @@ TEST_F(InputGamepadSessionTest, RejectsMalformedBatchablePacketsAtQueueIngress) } } +TEST_F(InputGamepadSessionTest, SerializesArrivalAndStatePacketsForOneController) { + ASSERT_FALSE(task_pool.running()); + config::input.gamepad = "auto"; + + auto stream_input = input::alloc(std::make_shared(), "simultaneous-controller-packets"); + ASSERT_NE(stream_input, nullptr); + std::vector> scheduled; + input::testing::set_input_task_sink([&](std::shared_ptr input) { + scheduled.push_back(std::move(input)); + }); + + SS_CONTROLLER_ARRIVAL_PACKET arrival {}; + arrival.header.size = util::endian::big(sizeof(arrival) - sizeof(arrival.header.size)); + arrival.header.magic = util::endian::little(SS_CONTROLLER_ARRIVAL_MAGIC); + arrival.controllerNumber = 0; + arrival.type = LI_CTYPE_NINTENDO; + + NV_MULTI_CONTROLLER_PACKET state {}; + state.header.size = util::endian::big(sizeof(state) - sizeof(state.header.size)); + state.header.magic = util::endian::little(MULTI_CONTROLLER_MAGIC_GEN5); + state.controllerNumber = 0; + state.activeGamepadMask = 1; + + std::vector arrival_bytes(sizeof(arrival)); + std::memcpy(arrival_bytes.data(), &arrival, sizeof(arrival)); + input::passthrough(stream_input, std::move(arrival_bytes)); + ASSERT_EQ(scheduled.size(), 1); + EXPECT_EQ(scheduled.front(), stream_input); + + std::promise arrival_started; + std::promise release_arrival; + const auto release_signal = release_arrival.get_future().share(); + input::testing::set_input_packet_hook([&](std::uint32_t magic) { + if (magic == SS_CONTROLLER_ARRIVAL_MAGIC) { + arrival_started.set_value(); + release_signal.wait(); + } + }); + + std::jthread worker {input::testing::process_queued_messages, stream_input}; + const auto arrival_status = arrival_started.get_future().wait_for(std::chrono::seconds {5}); + if (arrival_status != std::future_status::ready) { + release_arrival.set_value(); + worker.join(); + FAIL() << "Controller arrival packet did not reach the dispatch hook"; + } + + std::vector state_bytes(sizeof(state)); + std::memcpy(state_bytes.data(), &state, sizeof(state)); + input::passthrough(stream_input, std::move(state_bytes)); + EXPECT_EQ(scheduled.size(), 1); + EXPECT_EQ(input::testing::queued_input_packet_count(stream_input), 1); + + auto other_input = input::alloc(std::make_shared(), "independent-input-stream"); + if (!other_input) { + release_arrival.set_value(); + worker.join(); + FAIL() << "Could not create independent input stream"; + } + input::passthrough(other_input, make_input_packet(0x12345678, sizeof(std::uint32_t), sizeof(NV_INPUT_HEADER))); + EXPECT_EQ(scheduled.size(), 2); + if (scheduled.size() == 2) { + EXPECT_EQ(scheduled.back(), other_input); + } + const auto active_before_gamepad = runtime().active_device_count(); + release_arrival.set_value(); + worker.join(); + input::testing::set_input_packet_hook({}); + input::testing::process_queued_messages(other_input); + + EXPECT_EQ(input::testing::queued_input_packet_count(stream_input), 0); + EXPECT_EQ(input::testing::queued_input_packet_count(other_input), 0); + EXPECT_EQ(runtime().active_device_count(), active_before_gamepad + 1); + const auto gamepad_id = input::testing::gamepad_id(stream_input, 0); + ASSERT_GE(gamepad_id, 0); + const auto *adapter = platf::virtualhid::gamepad_adapter_for_testing(context(), gamepad_id); + ASSERT_NE(adapter, nullptr); + ASSERT_NE(adapter->gamepad(), nullptr); + EXPECT_EQ(adapter->gamepad()->profile().gamepad_kind, lvh::GamepadProfileKind::switch_pro); +} + +TEST_F(InputGamepadSessionTest, YieldsAfterBoundedPacketBatch) { + ASSERT_FALSE(task_pool.running()); + auto stream_input = input::alloc(std::make_shared(), "bounded-input-batch"); + ASSERT_NE(stream_input, nullptr); + + std::vector> scheduled; + input::testing::set_input_task_sink([&](std::shared_ptr input) { + scheduled.push_back(std::move(input)); + }); + + constexpr std::uint32_t unknown_magic = 0x12345678; + constexpr std::size_t packet_count = 33; + for (std::size_t n = 0; n < packet_count; ++n) { + input::passthrough(stream_input, make_input_packet(unknown_magic, sizeof(std::uint32_t), sizeof(NV_INPUT_HEADER))); + } + ASSERT_EQ(scheduled.size(), 1); + + input::testing::process_queued_messages(scheduled.front()); + EXPECT_EQ(input::testing::queued_input_packet_count(stream_input), 1); + ASSERT_EQ(scheduled.size(), 2); + input::testing::process_queued_messages(scheduled.back()); + EXPECT_EQ(input::testing::queued_input_packet_count(stream_input), 0); + EXPECT_EQ(scheduled.size(), 2); +} + TEST_F(InputGamepadSessionTest, ReusesGamepadsAcrossPauseAndDestroysThemOnTermination) { const std::string session_id = "paired-client-certificate"; auto first_mail = std::make_shared();