diff --git a/client/configuration.cpp b/client/configuration.cpp index 1afcdccb..2009bf7f 100644 --- a/client/configuration.cpp +++ b/client/configuration.cpp @@ -332,6 +332,7 @@ const std::vector & configuration::config_fields() scalar("forward_keyboard", &configuration::forward_keyboard), scalar("forward_mouse", &configuration::forward_mouse), scalar("forward_gamepad", &configuration::forward_gamepad), + scalar("emulated_gamepad", &configuration::emulated_gamepad), scalar("virtual_keyboard_layout", &configuration::virtual_keyboard_layout), scalar("override_foveation_enable", &configuration::override_foveation_enable), scalar("override_foveation_pitch", &configuration::override_foveation_pitch), diff --git a/client/configuration.h b/client/configuration.h index f5b51c83..62efdbf8 100644 --- a/client/configuration.h +++ b/client/configuration.h @@ -86,6 +86,7 @@ public: bool forward_keyboard = false; bool forward_mouse = false; bool forward_gamepad = false; + bool emulated_gamepad = false; std::underlying_type_t body_part_mask = ~0; diff --git a/client/scenes/gui_settings.cpp b/client/scenes/gui_settings.cpp index ca1ec440..dc7b828a 100644 --- a/client/scenes/gui_settings.cpp +++ b/client/scenes/gui_settings.cpp @@ -574,6 +574,19 @@ void settings_devices(const settings_context & ctx) .default_bool = default_config.forward_gamepad, }); + if (ctx.instance.has_extension("XR_VALVE_frame_controller_interaction")) + { + list.push_back({ + .id = "##emulated_gamepad", + .label = _C("setting name", "Emulated Gamepad"), + .description = _("Double-click both grips to start using your controllers as a gamepad."), + .ui = ui_kind::toggle, + .get_bool = [&config] { return config.emulated_gamepad; }, + .set_bool = [&config](bool v) { config.emulated_gamepad = v; config.save(); }, + .default_bool = default_config.emulated_gamepad, + }); + } + ui::page_header(_cS("page header title", "Devices"), _cS("page header subtitle", "Forward input devices to the PC.")); render_settings(ctx, "##devices", list); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 3a32f508..a5991b77 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -36,6 +36,7 @@ #include "decoder/shard_accumulator.h" #include "inplace_vector.hpp" #include "spdlog/spdlog.h" +#include "utils/i18n.h" #include "utils/named_thread.h" #include "utils/ranges.h" #include "wivrn_packets.h" @@ -564,6 +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; recenter_left = get_action("recenter_left").first; recenter_right = get_action("recenter_right").first; gui_distance_left = get_action("gui_distance_left").first; @@ -1241,6 +1244,50 @@ void scenes::stream::render(const XrFrameState & frame_state) } } + auto show_gamepad_toast = [this](bool enabled) { + { + auto toast = gui_toast.lock(); + toast->emplace(enabled + ? _("Now emulating a gamepad. Double-click both grips to go back to using VR controllers.") + : _("No longer emulating a gamepad."), + false); + } + gui_status_last_change = instance.now(); + }; + + if (not application::get_config().emulated_gamepad) + { + if (gamepad_mode.exchange(false)) + { + scheduled_derived_pose = instance.now(); + show_gamepad_toast(false); + spdlog::info("Controller gamepad emulation disabled due to setting change"); + } + gamepad_chord = {}; + } + else + { + 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"); + } + } + read_actions(); if (application::get_config().enable_stream_gui) @@ -1372,6 +1419,8 @@ scene::meta & scenes::stream::get_meta_scene() {"plots_toggle_1", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"plots_toggle_2", XR_ACTION_TYPE_BOOLEAN_INPUT}, + {"gamepad_mode_1", XR_ACTION_TYPE_BOOLEAN_INPUT}, + {"gamepad_mode_2", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"recenter_left", XR_ACTION_TYPE_BOOLEAN_INPUT}, {"recenter_right", XR_ACTION_TYPE_BOOLEAN_INPUT}, @@ -1423,6 +1472,15 @@ scene::meta & scenes::stream::get_meta_scene() {"plots_toggle_2", "/user/hand/right/input/thumbstick/click"}, }, }, + suggested_binding{ + { + "/interaction_profiles/valve/frame_controller_valve", + }, + { + {"gamepad_mode_1", "/user/hand/left/input/squeeze/click"}, + {"gamepad_mode_2", "/user/hand/right/input/squeeze/click"}, + }, + }, suggested_binding{ { "/interaction_profiles/khr/simple_controller", diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 0e134124..7bef581b 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -81,6 +81,23 @@ private: std::thread network_thread; thread_safe tracking_control{}; std::array, 3> interaction_profiles; // left hand, right hand, gamepad + std::atomic gamepad_mode = false; + + struct gamepad_chord_state + { + bool both_released = false; + bool waiting_for_second_tap = false; + XrTime first_tap_time = 0; + }; + gamepad_chord_state gamepad_chord = {}; + + bool using_frame_gamepad() const + { + return gamepad_mode.load() and + 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 interaction_profile_changed = false; std::atomic scheduled_derived_pose = 0; // Tracking thread will compute derived pose when time is reached std::atomic recenter_requested = false; @@ -175,6 +192,8 @@ 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; diff --git a/client/scenes/stream_actions.cpp b/client/scenes/stream_actions.cpp index cf543bf3..1a68e94f 100644 --- a/client/scenes/stream_actions.cpp +++ b/client/scenes/stream_actions.cpp @@ -19,29 +19,168 @@ #include "application.h" #include "stream.h" +#include +#include +#include +#include #include +namespace +{ +using wivrn::device_id; + +std::optional frame_gamepad_input(device_id id) +{ + switch (id) + { + case device_id::VIEW_CLICK: + return device_id::GAMEPAD_VIEW_CLICK; + case device_id::RIGHT_MENU_CLICK: + return device_id::GAMEPAD_MENU_CLICK; + case device_id::A_CLICK: + return device_id::GAMEPAD_A_CLICK; + case device_id::B_CLICK: + return device_id::GAMEPAD_B_CLICK; + case device_id::RIGHT_X_CLICK: + return device_id::GAMEPAD_X_CLICK; + case device_id::RIGHT_Y_CLICK: + return device_id::GAMEPAD_Y_CLICK; + case device_id::DPAD_UP_CLICK: + return device_id::GAMEPAD_DPAD_UP_CLICK; + case device_id::LEFT_X_CLICK: + return device_id::GAMEPAD_DPAD_DOWN_CLICK; + case device_id::DPAD_LEFT_CLICK: + return device_id::GAMEPAD_DPAD_LEFT_CLICK; + case device_id::LEFT_Y_CLICK: + return device_id::GAMEPAD_DPAD_RIGHT_CLICK; + case device_id::LEFT_BUMPER_CLICK: + return device_id::GAMEPAD_SHOULDER_LEFT_CLICK; + case device_id::RIGHT_BUMPER_CLICK: + return device_id::GAMEPAD_SHOULDER_RIGHT_CLICK; + case device_id::LEFT_THUMBSTICK_CLICK: + return device_id::GAMEPAD_THUMBSTICK_LEFT_CLICK; + case device_id::RIGHT_THUMBSTICK_CLICK: + return device_id::GAMEPAD_THUMBSTICK_RIGHT_CLICK; + case device_id::LEFT_TRIGGER_VALUE: + return device_id::GAMEPAD_TRIGGER_LEFT_VALUE; + case device_id::RIGHT_TRIGGER_VALUE: + return device_id::GAMEPAD_TRIGGER_RIGHT_VALUE; + case device_id::LEFT_THUMBSTICK_X: + return device_id::GAMEPAD_THUMBSTICK_LEFT_X; + case device_id::RIGHT_THUMBSTICK_X: + return device_id::GAMEPAD_THUMBSTICK_RIGHT_X; + default: + return std::nullopt; + } +} + +constexpr std::array frame_gamepad_ids = { + device_id::GAMEPAD_MENU_CLICK, + device_id::GAMEPAD_VIEW_CLICK, + device_id::GAMEPAD_A_CLICK, + device_id::GAMEPAD_B_CLICK, + device_id::GAMEPAD_X_CLICK, + device_id::GAMEPAD_Y_CLICK, + device_id::GAMEPAD_DPAD_UP_CLICK, + device_id::GAMEPAD_DPAD_DOWN_CLICK, + device_id::GAMEPAD_DPAD_LEFT_CLICK, + device_id::GAMEPAD_DPAD_RIGHT_CLICK, + device_id::GAMEPAD_SHOULDER_LEFT_CLICK, + device_id::GAMEPAD_SHOULDER_RIGHT_CLICK, + device_id::GAMEPAD_THUMBSTICK_LEFT_CLICK, + device_id::GAMEPAD_THUMBSTICK_RIGHT_CLICK, + device_id::GAMEPAD_TRIGGER_LEFT_VALUE, + device_id::GAMEPAD_TRIGGER_RIGHT_VALUE, + device_id::GAMEPAD_THUMBSTICK_LEFT_X, + device_id::GAMEPAD_THUMBSTICK_LEFT_Y, + device_id::GAMEPAD_THUMBSTICK_RIGHT_X, + device_id::GAMEPAD_THUMBSTICK_RIGHT_Y, +}; +} // namespace + +bool scenes::stream::check_gamepad_mode_chord(XrActionStateBoolean state_1, XrActionStateBoolean state_2) +{ + 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 + + if (not state_1.isActive or not state_2.isActive) + { + gamepad_chord = {}; + return false; + } + + const bool down_1 = state_1.currentState; + const bool down_2 = state_2.currentState; + + if (not down_1 and not down_2) + { + gamepad_chord.both_released = true; + return false; + } + + // require both inputs to be released between the double-tap + // to avoid accidental activation + if (not(down_1 and down_2) or not gamepad_chord.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; + 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) + { + gamepad_chord.waiting_for_second_tap = false; + 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; + } + + gamepad_chord.first_tap_time = tap_time; + gamepad_chord.waiting_for_second_tap = true; + return false; +} + void scenes::stream::read_actions() { from_headset::inputs inputs; + const bool emulating_gamepad = using_frame_gamepad(); if (not is_gui_interactable()) { for (const auto & [id, action, action_type]: input_actions) { + device_id output_id = id; + if (emulating_gamepad) + { + auto mapped_id = frame_gamepad_input(id); + if (not mapped_id) + continue; // No VR inputs in gamepad mode + output_id = *mapped_id; + } + switch (action_type) { case XR_ACTION_TYPE_BOOLEAN_INPUT: { auto value = application::read_action_bool(action); if (value) - inputs.values.push_back({id, (float)value->second, value->first}); + inputs.values.push_back({output_id, (float)value->second, value->first}); } break; case XR_ACTION_TYPE_FLOAT_INPUT: { auto value = application::read_action_float(action); if (value) - inputs.values.push_back({id, value->second, value->first}); + inputs.values.push_back({output_id, value->second, value->first}); } break; @@ -49,8 +188,12 @@ void scenes::stream::read_actions() auto value = application::read_action_vec2(action); if (value) { - inputs.values.push_back({id, value->second.x, value->first}); - inputs.values.push_back({(device_id)((int)id + 1), value->second.y, value->first}); + inputs.values.push_back({output_id, value->second.x, value->first}); + float y = value->second.y; + if (emulating_gamepad && + (id == device_id::LEFT_THUMBSTICK_X || id == device_id::RIGHT_THUMBSTICK_X)) + y = -y; + inputs.values.push_back({(device_id)((int)output_id + 1), y, value->first}); } } break; @@ -62,6 +205,15 @@ void scenes::stream::read_actions() } } + if (emulating_gamepad) + { + for (auto id: frame_gamepad_ids) + { + if (std::ranges::none_of(inputs.values, [id](const auto & value) { return value.id == id; })) + inputs.values.push_back({id, 0, 0}); + } + } + try { network_session->send_stream(std::move(inputs)); @@ -74,6 +226,22 @@ void scenes::stream::read_actions() void scenes::stream::operator()(to_headset::haptics && haptics) { + if (using_frame_gamepad()) + { + switch (haptics.id) + { + case device_id::GAMEPAD_HAPTIC_LEFT: + case device_id::GAMEPAD_HAPTIC_LEFT_TRIGGER: + haptics.id = device_id::LEFT_CONTROLLER_HAPTIC; + break; + case device_id::GAMEPAD_HAPTIC_RIGHT: + case device_id::GAMEPAD_HAPTIC_RIGHT_TRIGGER: + haptics.id = device_id::RIGHT_CONTROLLER_HAPTIC; + break; + default: + break; + } + } auto range = haptics_actions.equal_range(haptics.id); for (auto it = range.first; it != range.second; ++it) { diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index 33992fb0..0cbfe466 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -470,11 +470,19 @@ void scenes::stream::tracking() if (auto htc = std::get_if(&body_tracker)) htc->update_active(); + const bool emulating_gamepad = using_frame_gamepad(); tracking.interaction_profiles = { interaction_profiles[0].load(), interaction_profiles[1].load(), interaction_profiles[2].load(), }; + if (emulating_gamepad) + { + // The server must see only a gamepad, no VR controllers + tracking.interaction_profiles[0] = interaction_profile::none; + tracking.interaction_profiles[1] = interaction_profile::none; + tracking.interaction_profiles[2] = interaction_profile::microsoft_xbox_controller; + } tracking.production_timestamp = t0; tracking.timestamp = t0 + pattern[pattern_position].prediction_ns; @@ -521,7 +529,8 @@ void scenes::stream::tracking() case wivrn::device_id::LEFT_POKE: case wivrn::device_id::RIGHT_PINCH_POSE: case wivrn::device_id::RIGHT_POKE: - locate_spaces.add_space(item.device, spaces[item.device], tracking.timestamp, tracking.device_poses); + if (not emulating_gamepad) + locate_spaces.add_space(item.device, spaces[item.device], tracking.timestamp, tracking.device_poses); break; case wivrn::device_id::EYE_GAZE: if (eye_gaze_enabled) @@ -538,19 +547,19 @@ void scenes::stream::tracking() break; case wivrn::device_id::LEFT_HAND: if (left_hand) - hands.emplace_back(locate_hands( - *left_hand, - height_offset_space, - t0, - at_time)); + { + auto hand = locate_hands(*left_hand, height_offset_space, t0, at_time); + if (not emulating_gamepad or hand.hand != from_headset::hand_tracking::hand_id::left_controller) + hands.emplace_back(std::move(hand)); + } break; case wivrn::device_id::RIGHT_HAND: if (right_hand) - hands.emplace_back(locate_hands( - *right_hand, - height_offset_space, - t0, - at_time)); + { + auto hand = locate_hands(*right_hand, height_offset_space, t0, at_time); + if (not emulating_gamepad or hand.hand != from_headset::hand_tracking::hand_id::right_controller) + hands.emplace_back(std::move(hand)); + } break; case wivrn::device_id::BODY: std::visit(utils::overloaded{ @@ -633,7 +642,7 @@ void scenes::stream::tracking() network_session->send_stream(std::span(packets.data(), packet_count)); XrTime old = scheduled_derived_pose; - if (old and now > old) + if (old and now > old and not emulating_gamepad) { send_derived_pose(); scheduled_derived_pose.compare_exchange_strong(old, 0);