diff --git a/runtime/src/active_input.h b/runtime/src/active_input.h index c0f7451b1d..55b67d3bbf 100644 --- a/runtime/src/active_input.h +++ b/runtime/src/active_input.h @@ -95,7 +95,8 @@ class ActiveInputTracker { const InputEdge edge = edge_between(it->second, now); it->second = now; if (edge == InputEdge::None || family == tracked_) return InputEdge::None; - if (changed_once_ && time_us - changed_at_ < thresholds_.min_switch_us) return InputEdge::None; + if (changed_once_ && (time_us < changed_at_ || time_us - changed_at_ < thresholds_.min_switch_us)) + return InputEdge::None; const InputFamily before = active(); tracked_ = family; changed_at_ = time_us; @@ -125,11 +126,17 @@ class ActiveInputTracker { InputEdge edge_between(const InputSample& was, const InputSample& now) const { if (now.buttons & ~was.buttons) return InputEdge::Button; const float t = thresholds_.trigger; - if ((was.left_trigger <= t && now.left_trigger > t) || (was.right_trigger <= t && now.right_trigger > t)) + const auto trigger_rose = [t](float prev, float cur) { + return std::isfinite(prev) && std::isfinite(cur) && prev <= t && cur > t; + }; + if (trigger_rose(was.left_trigger, now.left_trigger) || trigger_rose(was.right_trigger, now.right_trigger)) return InputEdge::Trigger; - const auto out = [this](float x, float y) { return std::hypot(x, y) > thresholds_.stick; }; - if ((!out(was.left_x, was.left_y) && out(now.left_x, now.left_y)) || - (!out(was.right_x, was.right_y) && out(now.right_x, now.right_y))) + const auto stick_left = [this](float px, float py, float cx, float cy) { + if (!std::isfinite(px) || !std::isfinite(py) || !std::isfinite(cx) || !std::isfinite(cy)) return false; + return std::hypot(px, py) <= thresholds_.stick && std::hypot(cx, cy) > thresholds_.stick; + }; + if (stick_left(was.left_x, was.left_y, now.left_x, now.left_y) || + stick_left(was.right_x, was.right_y, now.right_x, now.right_y)) return InputEdge::Stick; return InputEdge::None; } diff --git a/runtime/test/active_input_test.cpp b/runtime/test/active_input_test.cpp index 11c6283201..64b7f1b869 100644 --- a/runtime/test/active_input_test.cpp +++ b/runtime/test/active_input_test.cpp @@ -4,6 +4,7 @@ #include "../src/active_input.h" #include +#include #include #include @@ -201,6 +202,41 @@ int test_forced() { return 0; } +int test_out_of_order_timestamp_and_non_finite_axes() { + ActiveInputTracker t(InputFamily::Xbox); + EXPECT(t.observe(1, InputFamily::PlayStation5, InputSample{}, 1000000) == InputEdge::None); + EXPECT(t.observe(2, InputFamily::Keyboard, InputSample{}, 1000000) == InputEdge::None); + + InputSample press; + press.buttons = 1; + EXPECT(t.observe(1, InputFamily::PlayStation5, press, 2000000) == InputEdge::Button); + EXPECT(t.active() == InputFamily::PlayStation5 && t.generation() == 1); + + // An earlier timestamp must not underflow (time_us - changed_at_) and bypass min_switch_us. + EXPECT(t.observe(2, InputFamily::Keyboard, press, 1999000) == InputEdge::None); + EXPECT(t.active() == InputFamily::PlayStation5 && t.generation() == 1); + + // Non-finite stick/trigger samples (NaN baseline or Inf deflection) must not trigger a family switch. + const float nan = std::numeric_limits::quiet_NaN(); + const float inf = std::numeric_limits::infinity(); + ActiveInputTracker t2(InputFamily::Xbox); + InputSample nan_stick; + nan_stick.left_x = nan; + EXPECT(t2.observe(3, InputFamily::PlayStation5, nan_stick, 1000000) == InputEdge::None); + InputSample deflected; + deflected.left_x = 0.9f; + EXPECT(t2.observe(3, InputFamily::PlayStation5, deflected, 2000000) == InputEdge::None); + EXPECT(t2.active() == InputFamily::Xbox && t2.generation() == 0); + + ActiveInputTracker t3(InputFamily::Xbox); + EXPECT(t3.observe(4, InputFamily::PlayStation5, InputSample{}, 1000000) == InputEdge::None); + InputSample inf_trig; + inf_trig.left_trigger = inf; + EXPECT(t3.observe(4, InputFamily::PlayStation5, inf_trig, 2000000) == InputEdge::None); + EXPECT(t3.active() == InputFamily::Xbox && t3.generation() == 0); + return 0; +} + int test_names() { EXPECT(std::string_view(input_family_name(InputFamily::PlayStation5)) == "PlayStation 5"); EXPECT(std::string_view(input_family_name(InputFamily::Keyboard)) == "keyboard and mouse"); @@ -222,6 +258,7 @@ int main() { {"one family, held input", test_same_family_and_holds}, {"reconnect", test_reconnect}, {"forced family", test_forced}, + {"out-of-order timestamp and non-finite axes", test_out_of_order_timestamp_and_non_finite_axes}, {"names", test_names}, }; int failures = 0;