refactor chord_state

This commit is contained in:
galister
2026-10-27 22:08:23 +01:00
committed by xytovl
parent 528f8e7246
commit 1f8e5afcf0
3 changed files with 58 additions and 58 deletions
+10 -23
View File
@@ -565,8 +565,8 @@ void scenes::stream::on_focused()
plots_toggle_1 = get_action("plots_toggle_1").first;
plots_toggle_2 = get_action("plots_toggle_2").first;
gamepad_mode_1 = get_action("gamepad_mode_1").first;
gamepad_mode_2 = get_action("gamepad_mode_2").first;
gamepad_chord.a = get_action("gamepad_mode_1").first;
gamepad_chord.b = get_action("gamepad_mode_2").first;
recenter_left = get_action("recenter_left").first;
recenter_right = get_action("recenter_right").first;
gui_distance_left = get_action("gui_distance_left").first;
@@ -1263,29 +1263,16 @@ void scenes::stream::render(const XrFrameState & frame_state)
show_gamepad_toast(false);
spdlog::info("Controller gamepad emulation disabled due to setting change");
}
gamepad_chord = {};
gamepad_chord.reset();
}
else
else if (gamepad_chord.check(instance.now()))
{
XrActionStateGetInfo get_info{
.type = XR_TYPE_ACTION_STATE_GET_INFO,
.action = gamepad_mode_1,
};
XrActionStateBoolean state_1{XR_TYPE_ACTION_STATE_BOOLEAN};
CHECK_XR(xrGetActionStateBoolean(session, &get_info, &state_1));
get_info.action = gamepad_mode_2;
XrActionStateBoolean state_2{XR_TYPE_ACTION_STATE_BOOLEAN};
CHECK_XR(xrGetActionStateBoolean(session, &get_info, &state_2));
if (check_gamepad_mode_chord(state_1, state_2))
{
const bool enabled = not gamepad_mode.load();
gamepad_mode = enabled;
if (not enabled)
scheduled_derived_pose = instance.now();
show_gamepad_toast(enabled);
spdlog::info("Controller gamepad mode {}", enabled ? "enabled" : "disabled");
}
const bool enabled = not gamepad_mode.load();
gamepad_mode = enabled;
if (not enabled)
scheduled_derived_pose = instance.now();
show_gamepad_toast(enabled);
spdlog::info("Controller gamepad mode {}", enabled ? "enabled" : "disabled");
}
read_actions();
+18 -7
View File
@@ -83,13 +83,27 @@ private:
std::array<std::atomic<interaction_profile>, 3> interaction_profiles; // left hand, right hand, gamepad
std::atomic<bool> gamepad_mode = false;
struct gamepad_chord_state
struct chord_state
{
XrAction a = XR_NULL_HANDLE;
XrAction b = XR_NULL_HANDLE;
uint32_t clicks_needed = 1;
bool both_released = false;
bool waiting_for_second_tap = false;
XrTime first_tap_time = 0;
uint32_t clicks_done = 0;
XrTime first_click_time = 0;
void reset()
{
both_released = false;
clicks_done = 0;
first_click_time = 0;
}
// Read actions a and b and determine if they were activated together clicks_needed times in a row
bool check(XrTime now);
};
gamepad_chord_state gamepad_chord = {};
chord_state gamepad_chord = {.clicks_needed = 2};
bool using_frame_gamepad() const
{
@@ -97,7 +111,6 @@ private:
interaction_profiles[0].load() == interaction_profile::valve_frame_controller_valve and
interaction_profiles[1].load() == interaction_profile::valve_frame_controller_valve;
}
bool check_gamepad_mode_chord(XrActionStateBoolean state_1, XrActionStateBoolean state_2);
std::atomic<bool> interaction_profile_changed = false;
std::atomic<XrTime> scheduled_derived_pose = 0; // Tracking thread will compute derived pose when time is reached
std::atomic<bool> recenter_requested = false;
@@ -192,8 +205,6 @@ private:
XrAction plots_toggle_1 = XR_NULL_HANDLE;
XrAction plots_toggle_2 = XR_NULL_HANDLE;
XrAction gamepad_mode_1 = XR_NULL_HANDLE;
XrAction gamepad_mode_2 = XR_NULL_HANDLE;
XrAction recenter_left = XR_NULL_HANDLE;
XrAction recenter_right = XR_NULL_HANDLE;
XrAction gui_distance_left = XR_NULL_HANDLE;
+30 -28
View File
@@ -98,56 +98,58 @@ constexpr std::array frame_gamepad_ids = {
};
} // namespace
bool scenes::stream::check_gamepad_mode_chord(XrActionStateBoolean state_1, XrActionStateBoolean state_2)
bool scenes::stream::chord_state::check(XrTime now)
{
constexpr XrDuration gamepad_chord_press_tolerance = 150'000'000; // 150ms between grip presses
constexpr XrDuration gamepad_chord_timeout = 400'000'000; // 400ms between chord taps
constexpr XrDuration press_tolerance = 150'000'000; // 150ms between button presses
constexpr XrDuration click_timeout = 400'000'000; // 400ms between chord clicks
if (not state_1.isActive or not state_2.isActive)
auto state_1 = application::read_action_bool(a);
auto state_2 = application::read_action_bool(b);
if (not state_1 or not state_2)
{
gamepad_chord = {};
reset();
return false;
}
const bool down_1 = state_1.currentState;
const bool down_2 = state_2.currentState;
if (clicks_done != 0 and now - first_click_time > click_timeout)
clicks_done = 0;
const bool down_1 = state_1->second;
const bool down_2 = state_2->second;
if (not down_1 and not down_2)
{
gamepad_chord.both_released = true;
both_released = true;
return false;
}
// require both inputs to be released between the double-tap
// require both inputs to be released between taps
// to avoid accidental activation
if (not(down_1 and down_2) or not gamepad_chord.both_released)
if (not(down_1 and down_2) or not both_released)
return false;
// both grips are down
gamepad_chord.both_released = false;
const XrTime press_1 = state_1.lastChangeTime;
const XrTime press_2 = state_2.lastChangeTime;
// both inputs are down
both_released = false;
const XrTime press_1 = state_1->first;
const XrTime press_2 = state_2->first;
const XrDuration press_separation = press_1 >= press_2 ? press_1 - press_2 : press_2 - press_1;
// grips were not pressed together
if (press_separation > gamepad_chord_press_tolerance)
// inputs were not pressed together
if (press_separation > press_tolerance)
{
gamepad_chord.waiting_for_second_tap = false;
clicks_done = 0;
return false;
}
const XrTime tap_time = std::max(press_1, press_2);
if (gamepad_chord.waiting_for_second_tap and
tap_time > gamepad_chord.first_tap_time and
tap_time - gamepad_chord.first_tap_time <= gamepad_chord_timeout)
{
gamepad_chord.waiting_for_second_tap = false;
return true;
}
if (clicks_done == 0)
first_click_time = std::max(press_1, press_2);
gamepad_chord.first_tap_time = tap_time;
gamepad_chord.waiting_for_second_tap = true;
return false;
++clicks_done;
if (clicks_done < clicks_needed)
return false;
reset();
return true;
}
void scenes::stream::read_actions()