From aeecc857de5566494d326749ba3404e38794beeb Mon Sep 17 00:00:00 2001 From: JR Lanteigne Date: Mon, 6 Jul 2026 18:03:02 -0300 Subject: [PATCH] Add gamepad forwarding through OpenXR Forward a gamepad paired to the headset to the server. Useful when the controller is in range of the headset but not the server, such as a DualSense paired to a Quest over Bluetooth. The headset reads the gamepad through the OpenXR microsoft/xbox_controller profile and forwards it on the existing input rails: gamepad device ids in the inputs packet, plus a gamepad entry in the interaction profile array that signals connection and disconnection from the tracking thread. The server exposes a native OpenXR gamepad at /user/gamepad, connected while the headset reports a gamepad interaction profile. This needs no permission and is always available. Rumble is sent back to the headset and applied through OpenXR haptics. --- client/android/hid.cpp | 9 +- client/application.cpp | 50 ++++++- client/application.h | 5 + client/scenes/stream.cpp | 28 +++- client/scenes/stream.h | 2 +- client/scenes/stream_tracking.cpp | 4 +- common/wivrn_packets.h | 210 ++++++++++++++++------------- server/CMakeLists.txt | 1 + server/driver/wivrn_controller.cpp | 3 + server/driver/wivrn_gamepad.cpp | 185 +++++++++++++++++++++++++ server/driver/wivrn_gamepad.h | 61 +++++++++ server/driver/wivrn_gamepad_map.h | 112 +++++++++++++++ server/driver/wivrn_session.cpp | 11 ++ server/driver/wivrn_session.h | 2 + 14 files changed, 580 insertions(+), 103 deletions(-) create mode 100644 server/driver/wivrn_gamepad.cpp create mode 100644 server/driver/wivrn_gamepad.h create mode 100644 server/driver/wivrn_gamepad_map.h diff --git a/client/android/hid.cpp b/client/android/hid.cpp index 2f6bca3e..9b625e6d 100644 --- a/client/android/hid.cpp +++ b/client/android/hid.cpp @@ -249,10 +249,13 @@ bool android_hid::input_handler::handle_input(scene * current_scene, AInputEvent { const int32_t type = AInputEvent_getType(event); const int32_t src = AInputEvent_getSource(event); - const int32_t dev = AInputEvent_getDeviceId(event); if (type == AINPUT_EVENT_TYPE_KEY) { + // Gamepad input is read through the OpenXR microsoft/xbox_controller profile, not here. + if ((src & AINPUT_SOURCE_GAMEPAD) == AINPUT_SOURCE_GAMEPAD) + return false; + int32_t flags = AKeyEvent_getFlags(event); if ((flags & AKEY_EVENT_FLAG_SOFT_KEYBOARD) != 0) return false; // ignore soft keyboards @@ -288,6 +291,10 @@ bool android_hid::input_handler::handle_input(scene * current_scene, AInputEvent if (type == AINPUT_EVENT_TYPE_MOTION) { + // Gamepad axes are read through the OpenXR microsoft/xbox_controller profile, not here. + if ((src & AINPUT_SOURCE_JOYSTICK) == AINPUT_SOURCE_JOYSTICK) + return false; + // don't care about absolute mouse due to mapping difficulties if ((src & AINPUT_SOURCE_MOUSE_RELATIVE) == 0) return false; diff --git a/client/application.cpp b/client/application.cpp index 871b3598..69b3a1a5 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -602,6 +603,35 @@ static std::vector interaction_profiles{ "/user/eyes_ext/input/gaze_ext/pose", }, }, + interaction_profile{ + .profile_name = "/interaction_profiles/microsoft/xbox_controller", + .input_sources = { + "/user/gamepad/input/menu/click", + "/user/gamepad/input/view/click", + "/user/gamepad/input/a/click", + "/user/gamepad/input/b/click", + "/user/gamepad/input/x/click", + "/user/gamepad/input/y/click", + "/user/gamepad/input/dpad_up/click", + "/user/gamepad/input/dpad_down/click", + "/user/gamepad/input/dpad_left/click", + "/user/gamepad/input/dpad_right/click", + "/user/gamepad/input/shoulder_left/click", + "/user/gamepad/input/shoulder_right/click", + "/user/gamepad/input/thumbstick_left/click", + "/user/gamepad/input/thumbstick_right/click", + "/user/gamepad/input/trigger_left/value", + "/user/gamepad/input/trigger_right/value", + "/user/gamepad/input/thumbstick_left/x", + "/user/gamepad/input/thumbstick_left/y", + "/user/gamepad/input/thumbstick_right/x", + "/user/gamepad/input/thumbstick_right/y", + "/user/gamepad/output/haptic_left", + "/user/gamepad/output/haptic_right", + "/user/gamepad/output/haptic_left_trigger", + "/user/gamepad/output/haptic_right_trigger", + }, + }, }; static const std::pair action_suffixes[] = @@ -634,11 +664,15 @@ static const std::pair action_suffixes[] = {"/ready_ext", XR_ACTION_TYPE_BOOLEAN_INPUT}, // Output paths - {"/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, - {"/haptic_trigger", XR_ACTION_TYPE_VIBRATION_OUTPUT}, - {"/haptic_trigger_fb",XR_ACTION_TYPE_VIBRATION_OUTPUT}, - {"/haptic_thumb", XR_ACTION_TYPE_VIBRATION_OUTPUT}, - {"/haptic_thumb_fb", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_trigger", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_trigger_fb", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_thumb", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_thumb_fb", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_left", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_right", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_left_trigger", XR_ACTION_TYPE_VIBRATION_OUTPUT}, + {"/haptic_right_trigger",XR_ACTION_TYPE_VIBRATION_OUTPUT}, // clang-format on }; @@ -1717,8 +1751,10 @@ void application::run() while (ALooper_pollOnce(100, nullptr, &events, (void **)&source) >= 0) { // Process this event. - if (source != nullptr) - source->process(app_info.native_app, source); + if (source == nullptr) + continue; + + source->process(app_info.native_app, source); } if (app_info.native_app->destroyRequested) diff --git a/client/application.h b/client/application.h index d638dd7a..edd7c1e5 100644 --- a/client/application.h +++ b/client/application.h @@ -206,6 +206,11 @@ public: { return instance().app_info.native_app; } + + static android_hid::input_handler & get_input_handler() + { + return instance().input_handler; + } #endif static bool is_session_running() diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 3fc278b1..21e248ec 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -138,6 +138,27 @@ static const std::unordered_map device_ids = { {"/user/hand/right/input/aim_activate_ext/ready_ext",device_id::RIGHT_AIM_ACTIVATE_READY}, {"/user/hand/right/input/grasp_ext/value", device_id::RIGHT_GRASP_VALUE}, {"/user/hand/right/input/grasp_ext/ready_ext", device_id::RIGHT_GRASP_READY}, + + {"/user/gamepad/input/menu/click", device_id::GAMEPAD_MENU_CLICK}, + {"/user/gamepad/input/view/click", device_id::GAMEPAD_VIEW_CLICK}, + {"/user/gamepad/input/a/click", device_id::GAMEPAD_A_CLICK}, + {"/user/gamepad/input/b/click", device_id::GAMEPAD_B_CLICK}, + {"/user/gamepad/input/x/click", device_id::GAMEPAD_X_CLICK}, + {"/user/gamepad/input/y/click", device_id::GAMEPAD_Y_CLICK}, + {"/user/gamepad/input/dpad_down/click", device_id::GAMEPAD_DPAD_DOWN_CLICK}, + {"/user/gamepad/input/dpad_right/click", device_id::GAMEPAD_DPAD_RIGHT_CLICK}, + {"/user/gamepad/input/dpad_up/click", device_id::GAMEPAD_DPAD_UP_CLICK}, + {"/user/gamepad/input/dpad_left/click", device_id::GAMEPAD_DPAD_LEFT_CLICK}, + {"/user/gamepad/input/shoulder_left/click", device_id::GAMEPAD_SHOULDER_LEFT_CLICK}, + {"/user/gamepad/input/shoulder_right/click", device_id::GAMEPAD_SHOULDER_RIGHT_CLICK}, + {"/user/gamepad/input/thumbstick_left/click", device_id::GAMEPAD_THUMBSTICK_LEFT_CLICK}, + {"/user/gamepad/input/thumbstick_right/click", device_id::GAMEPAD_THUMBSTICK_RIGHT_CLICK}, + {"/user/gamepad/input/trigger_left/value", device_id::GAMEPAD_TRIGGER_LEFT_VALUE}, + {"/user/gamepad/input/trigger_right/value", device_id::GAMEPAD_TRIGGER_RIGHT_VALUE}, + {"/user/gamepad/input/thumbstick_left/x", device_id::GAMEPAD_THUMBSTICK_LEFT_X}, + {"/user/gamepad/input/thumbstick_left/y", device_id::GAMEPAD_THUMBSTICK_LEFT_Y}, + {"/user/gamepad/input/thumbstick_right/x", device_id::GAMEPAD_THUMBSTICK_RIGHT_X}, + {"/user/gamepad/input/thumbstick_right/y", device_id::GAMEPAD_THUMBSTICK_RIGHT_Y}, }; // clang-format on @@ -380,7 +401,12 @@ std::shared_ptr scenes::stream::create(std::unique_ptr network_session; std::thread network_thread; thread_safe tracking_control{}; - std::array, 2> interaction_profiles; // left and right hand + std::array, 3> interaction_profiles; // left hand, right hand, gamepad 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; diff --git a/client/scenes/stream_tracking.cpp b/client/scenes/stream_tracking.cpp index f3db34b0..5f9b0573 100644 --- a/client/scenes/stream_tracking.cpp +++ b/client/scenes/stream_tracking.cpp @@ -443,6 +443,7 @@ void scenes::stream::tracking() tracking.interaction_profiles = { interaction_profiles[0].load(), interaction_profiles[1].load(), + interaction_profiles[2].load(), }; tracking.production_timestamp = t0; @@ -664,6 +665,7 @@ void scenes::stream::on_interaction_profile_changed(const XrEventDataInteraction std::array path = { "/user/hand/left", "/user/hand/right", + "/user/gamepad", }; #define DO_PROFILE(vendor, name) \ if (profile == "/interaction_profiles/" #vendor "/" #name) \ @@ -672,7 +674,7 @@ void scenes::stream::on_interaction_profile_changed(const XrEventDataInteraction continue; \ } - for (size_t i = 0; i < 2; ++i) + for (size_t i = 0; i < path.size(); ++i) { try { diff --git a/common/wivrn_packets.h b/common/wivrn_packets.h index 69219db7..5b6b0ba4 100644 --- a/common/wivrn_packets.h +++ b/common/wivrn_packets.h @@ -43,96 +43,122 @@ static constexpr int protocol_revision = 2; enum class device_id : uint8_t { - HEAD, // /user/head - LEFT_CONTROLLER_HAPTIC, // /user/hand/left/output/haptic - RIGHT_CONTROLLER_HAPTIC, // /user/hand/right/output/haptic - LEFT_TRIGGER_HAPTIC, // /user/hand/left/output/haptic_trigger - RIGHT_TRIGGER_HAPTIC, // /user/hand/right/output/haptic_trigger - LEFT_THUMB_HAPTIC, // /user/hand/left/output/haptic_thumb - RIGHT_THUMB_HAPTIC, // /user/hand/right/output/haptic_thumb - LEFT_GRIP, // /user/hand/left/input/grip/pose - LEFT_AIM, // /user/hand/left/input/aim/pose - LEFT_PALM, // /user/hand/left/palm_ext/pose - RIGHT_GRIP, // /user/hand/right/input/grip/pose - RIGHT_AIM, // /user/hand/right/input/aim/pose - RIGHT_PALM, // /user/hand/right/palm_ext/pose - X_CLICK, // /user/hand/left/input/x/click - X_TOUCH, // /user/hand/left/input/x/touch - Y_CLICK, // /user/hand/left/input/y/click - Y_TOUCH, // /user/hand/left/input/y/touch - MENU_CLICK, // /user/hand/left/input/menu/click - LEFT_SQUEEZE_CLICK, // /user/hand/left/input/squeeze/click - LEFT_SQUEEZE_FORCE, // /user/hand/left/input/squeeze/force - LEFT_SQUEEZE_VALUE, // /user/hand/left/input/squeeze/value - LEFT_TRIGGER_CLICK, // /user/hand/left/input/trigger/click - LEFT_TRIGGER_VALUE, // /user/hand/left/input/trigger/value - LEFT_TRIGGER_TOUCH, // /user/hand/left/input/trigger/touch - LEFT_TRIGGER_PROXIMITY, // /user/hand/left/input/trigger/proximity - LEFT_TRIGGER_CURL, // /user/hand/left/input/trigger/curl_fb - LEFT_TRIGGER_SLIDE, // /user/hand/left/input/trigger/slide_fb - LEFT_TRIGGER_FORCE, // /user/hand/left/input/trigger/force - LEFT_THUMBSTICK_X, // /user/hand/left/input/thumbstick/x - LEFT_THUMBSTICK_Y, // /user/hand/left/input/thumbstick/y - LEFT_THUMBSTICK_CLICK, // /user/hand/left/input/thumbstick/click - LEFT_THUMBSTICK_TOUCH, // /user/hand/left/input/thumbstick/touch - LEFT_THUMBREST_TOUCH, // /user/hand/left/input/thumbrest/touch - LEFT_THUMBREST_FORCE, // /user/hand/left/input/thumbrest/force - LEFT_THUMB_PROXIMITY, // /user/hand/left/input/thumb_resting_surfaces/proximity - LEFT_TRACKPAD_X, // /user/hand/left/input/trackpad/x - LEFT_TRACKPAD_Y, // /user/hand/left/input/trackpad/y - LEFT_TRACKPAD_CLICK, // /user/hand/left/input/trackpad/click - LEFT_TRACKPAD_TOUCH, // /user/hand/left/input/trackpad/touch - LEFT_TRACKPAD_FORCE, // /user/hand/left/input/trackpad/force - LEFT_STYLUS_FORCE, // /user/hand/left/input/stylus_fb/force - LEFT_PINCH_POSE, // /user/hand/left/input/pinch_ext/pose - LEFT_PINCH_VALUE, // /user/hand/left/input/pinch_ext/value - LEFT_PINCH_READY, // /user/hand/left/input/pinch_ext/ready_ext - LEFT_POKE, // /user/hand/left/input/poke_ext/pose - LEFT_AIM_ACTIVATE_VALUE, // /user/hand/left/input/aim_activate_ext/value - LEFT_AIM_ACTIVATE_READY, // /user/hand/left/input/aim_activate_ext/ready_ext - LEFT_GRASP_VALUE, // /user/hand/left/input/grasp_ext/value - LEFT_GRASP_READY, // /user/hand/left/input/grasp_ext/ready_ext - A_CLICK, // /user/hand/right/input/a/click - A_TOUCH, // /user/hand/right/input/a/touch - B_CLICK, // /user/hand/right/input/b/click - B_TOUCH, // /user/hand/right/input/b/touch - SYSTEM_CLICK, // /user/hand/right/input/system/click - RIGHT_SQUEEZE_CLICK, // /user/hand/right/input/squeeze/click - RIGHT_SQUEEZE_FORCE, // /user/hand/right/input/squeeze/force - RIGHT_SQUEEZE_VALUE, // /user/hand/right/input/squeeze/value - RIGHT_TRIGGER_CLICK, // /user/hand/right/input/trigger/click - RIGHT_TRIGGER_VALUE, // /user/hand/right/input/trigger/value - RIGHT_TRIGGER_TOUCH, // /user/hand/right/input/trigger/touch - RIGHT_TRIGGER_PROXIMITY, // /user/hand/right/input/trigger/proximity - RIGHT_TRIGGER_CURL, // /user/hand/right/input/trigger/curl_fb - RIGHT_TRIGGER_SLIDE, // /user/hand/right/input/trigger/slide_fb - RIGHT_TRIGGER_FORCE, // /user/hand/right/input/trigger/force - RIGHT_THUMBSTICK_X, // /user/hand/right/input/thumbstick/x - RIGHT_THUMBSTICK_Y, // /user/hand/right/input/thumbstick/y - RIGHT_THUMBSTICK_CLICK, // /user/hand/right/input/thumbstick/click - RIGHT_THUMBSTICK_TOUCH, // /user/hand/right/input/thumbstick/touch - RIGHT_THUMBREST_TOUCH, // /user/hand/right/input/thumbrest/touch - RIGHT_THUMBREST_FORCE, // /user/hand/right/input/thumbrest/force - RIGHT_THUMB_PROXIMITY, // /user/hand/right/input/thumb_resting_surfaces/proximity - RIGHT_TRACKPAD_X, // /user/hand/right/input/trackpad/x - RIGHT_TRACKPAD_Y, // /user/hand/right/input/trackpad/y - RIGHT_TRACKPAD_CLICK, // /user/hand/right/input/trackpad/click - RIGHT_TRACKPAD_TOUCH, // /user/hand/right/input/trackpad/touch - RIGHT_TRACKPAD_FORCE, // /user/hand/right/input/trackpad/force - RIGHT_STYLUS_FORCE, // /user/hand/right/input/stylus_fb/force - RIGHT_PINCH_POSE, // /user/hand/right/input/pinch_ext/pose - RIGHT_PINCH_VALUE, // /user/hand/right/input/pinch_ext/value - RIGHT_PINCH_READY, // /user/hand/right/input/pinch_ext/ready_ext - RIGHT_POKE, // /user/hand/right/input/poke_ext/pose - RIGHT_AIM_ACTIVATE_VALUE, // /user/hand/right/input/aim_activate_ext/value - RIGHT_AIM_ACTIVATE_READY, // /user/hand/right/input/aim_activate_ext/ready_ext - RIGHT_GRASP_VALUE, // /user/hand/right/input/grasp_ext/value - RIGHT_GRASP_READY, // /user/hand/right/input/grasp_ext/ready_ext - EYE_GAZE, // /user/eyes_ext/input/gaze_ext/pose - LEFT_HAND, // identify hand tracking - RIGHT_HAND, // identify hand tracking - BODY, // identify body tracking - FACE, // identify face tracking + HEAD, // /user/head + LEFT_CONTROLLER_HAPTIC, // /user/hand/left/output/haptic + RIGHT_CONTROLLER_HAPTIC, // /user/hand/right/output/haptic + LEFT_TRIGGER_HAPTIC, // /user/hand/left/output/haptic_trigger + RIGHT_TRIGGER_HAPTIC, // /user/hand/right/output/haptic_trigger + LEFT_THUMB_HAPTIC, // /user/hand/left/output/haptic_thumb + RIGHT_THUMB_HAPTIC, // /user/hand/right/output/haptic_thumb + GAMEPAD_HAPTIC_LEFT, // /user/gamepad/output/haptic_left + GAMEPAD_HAPTIC_RIGHT, // /user/gamepad/output/haptic_right + GAMEPAD_HAPTIC_LEFT_TRIGGER, // /user/gamepad/output/haptic_left_trigger + GAMEPAD_HAPTIC_RIGHT_TRIGGER, // /user/gamepad/output/haptic_right_trigger + LEFT_GRIP, // /user/hand/left/input/grip/pose + LEFT_AIM, // /user/hand/left/input/aim/pose + LEFT_PALM, // /user/hand/left/palm_ext/pose + RIGHT_GRIP, // /user/hand/right/input/grip/pose + RIGHT_AIM, // /user/hand/right/input/aim/pose + RIGHT_PALM, // /user/hand/right/palm_ext/pose + X_CLICK, // /user/hand/left/input/x/click + X_TOUCH, // /user/hand/left/input/x/touch + Y_CLICK, // /user/hand/left/input/y/click + Y_TOUCH, // /user/hand/left/input/y/touch + MENU_CLICK, // /user/hand/left/input/menu/click + LEFT_SQUEEZE_CLICK, // /user/hand/left/input/squeeze/click + LEFT_SQUEEZE_FORCE, // /user/hand/left/input/squeeze/force + LEFT_SQUEEZE_VALUE, // /user/hand/left/input/squeeze/value + LEFT_TRIGGER_CLICK, // /user/hand/left/input/trigger/click + LEFT_TRIGGER_VALUE, // /user/hand/left/input/trigger/value + LEFT_TRIGGER_TOUCH, // /user/hand/left/input/trigger/touch + LEFT_TRIGGER_PROXIMITY, // /user/hand/left/input/trigger/proximity + LEFT_TRIGGER_CURL, // /user/hand/left/input/trigger/curl_fb + LEFT_TRIGGER_SLIDE, // /user/hand/left/input/trigger/slide_fb + LEFT_TRIGGER_FORCE, // /user/hand/left/input/trigger/force + LEFT_THUMBSTICK_X, // /user/hand/left/input/thumbstick/x + LEFT_THUMBSTICK_Y, // /user/hand/left/input/thumbstick/y + LEFT_THUMBSTICK_CLICK, // /user/hand/left/input/thumbstick/click + LEFT_THUMBSTICK_TOUCH, // /user/hand/left/input/thumbstick/touch + LEFT_THUMBREST_TOUCH, // /user/hand/left/input/thumbrest/touch + LEFT_THUMBREST_FORCE, // /user/hand/left/input/thumbrest/force + LEFT_THUMB_PROXIMITY, // /user/hand/left/input/thumb_resting_surfaces/proximity + LEFT_TRACKPAD_X, // /user/hand/left/input/trackpad/x + LEFT_TRACKPAD_Y, // /user/hand/left/input/trackpad/y + LEFT_TRACKPAD_CLICK, // /user/hand/left/input/trackpad/click + LEFT_TRACKPAD_TOUCH, // /user/hand/left/input/trackpad/touch + LEFT_TRACKPAD_FORCE, // /user/hand/left/input/trackpad/force + LEFT_STYLUS_FORCE, // /user/hand/left/input/stylus_fb/force + LEFT_PINCH_POSE, // /user/hand/left/input/pinch_ext/pose + LEFT_PINCH_VALUE, // /user/hand/left/input/pinch_ext/value + LEFT_PINCH_READY, // /user/hand/left/input/pinch_ext/ready_ext + LEFT_POKE, // /user/hand/left/input/poke_ext/pose + LEFT_AIM_ACTIVATE_VALUE, // /user/hand/left/input/aim_activate_ext/value + LEFT_AIM_ACTIVATE_READY, // /user/hand/left/input/aim_activate_ext/ready_ext + LEFT_GRASP_VALUE, // /user/hand/left/input/grasp_ext/value + LEFT_GRASP_READY, // /user/hand/left/input/grasp_ext/ready_ext + A_CLICK, // /user/hand/right/input/a/click + A_TOUCH, // /user/hand/right/input/a/touch + B_CLICK, // /user/hand/right/input/b/click + B_TOUCH, // /user/hand/right/input/b/touch + SYSTEM_CLICK, // /user/hand/right/input/system/click + RIGHT_SQUEEZE_CLICK, // /user/hand/right/input/squeeze/click + RIGHT_SQUEEZE_FORCE, // /user/hand/right/input/squeeze/force + RIGHT_SQUEEZE_VALUE, // /user/hand/right/input/squeeze/value + RIGHT_TRIGGER_CLICK, // /user/hand/right/input/trigger/click + RIGHT_TRIGGER_VALUE, // /user/hand/right/input/trigger/value + RIGHT_TRIGGER_TOUCH, // /user/hand/right/input/trigger/touch + RIGHT_TRIGGER_PROXIMITY, // /user/hand/right/input/trigger/proximity + RIGHT_TRIGGER_CURL, // /user/hand/right/input/trigger/curl_fb + RIGHT_TRIGGER_SLIDE, // /user/hand/right/input/trigger/slide_fb + RIGHT_TRIGGER_FORCE, // /user/hand/right/input/trigger/force + RIGHT_THUMBSTICK_X, // /user/hand/right/input/thumbstick/x + RIGHT_THUMBSTICK_Y, // /user/hand/right/input/thumbstick/y + RIGHT_THUMBSTICK_CLICK, // /user/hand/right/input/thumbstick/click + RIGHT_THUMBSTICK_TOUCH, // /user/hand/right/input/thumbstick/touch + RIGHT_THUMBREST_TOUCH, // /user/hand/right/input/thumbrest/touch + RIGHT_THUMBREST_FORCE, // /user/hand/right/input/thumbrest/force + RIGHT_THUMB_PROXIMITY, // /user/hand/right/input/thumb_resting_surfaces/proximity + RIGHT_TRACKPAD_X, // /user/hand/right/input/trackpad/x + RIGHT_TRACKPAD_Y, // /user/hand/right/input/trackpad/y + RIGHT_TRACKPAD_CLICK, // /user/hand/right/input/trackpad/click + RIGHT_TRACKPAD_TOUCH, // /user/hand/right/input/trackpad/touch + RIGHT_TRACKPAD_FORCE, // /user/hand/right/input/trackpad/force + RIGHT_STYLUS_FORCE, // /user/hand/right/input/stylus_fb/force + RIGHT_PINCH_POSE, // /user/hand/right/input/pinch_ext/pose + RIGHT_PINCH_VALUE, // /user/hand/right/input/pinch_ext/value + RIGHT_PINCH_READY, // /user/hand/right/input/pinch_ext/ready_ext + RIGHT_POKE, // /user/hand/right/input/poke_ext/pose + RIGHT_AIM_ACTIVATE_VALUE, // /user/hand/right/input/aim_activate_ext/value + RIGHT_AIM_ACTIVATE_READY, // /user/hand/right/input/aim_activate_ext/ready_ext + RIGHT_GRASP_VALUE, // /user/hand/right/input/grasp_ext/value + RIGHT_GRASP_READY, // /user/hand/right/input/grasp_ext/ready_ext + EYE_GAZE, // /user/eyes_ext/input/gaze_ext/pose + LEFT_HAND, // identify hand tracking + RIGHT_HAND, // identify hand tracking + BODY, // identify body tracking + FACE, // identify face tracking + + // Gamepad, microsoft/xbox_controller profile (/user/gamepad) + GAMEPAD_MENU_CLICK, // /user/gamepad/input/menu/click + GAMEPAD_VIEW_CLICK, // /user/gamepad/input/view/click + GAMEPAD_A_CLICK, // /user/gamepad/input/a/click + GAMEPAD_B_CLICK, // /user/gamepad/input/b/click + GAMEPAD_X_CLICK, // /user/gamepad/input/x/click + GAMEPAD_Y_CLICK, // /user/gamepad/input/y/click + GAMEPAD_DPAD_DOWN_CLICK, // /user/gamepad/input/dpad_down/click + GAMEPAD_DPAD_RIGHT_CLICK, // /user/gamepad/input/dpad_right/click + GAMEPAD_DPAD_UP_CLICK, // /user/gamepad/input/dpad_up/click + GAMEPAD_DPAD_LEFT_CLICK, // /user/gamepad/input/dpad_left/click + GAMEPAD_SHOULDER_LEFT_CLICK, // /user/gamepad/input/shoulder_left/click + GAMEPAD_SHOULDER_RIGHT_CLICK, // /user/gamepad/input/shoulder_right/click + GAMEPAD_THUMBSTICK_LEFT_CLICK, // /user/gamepad/input/thumbstick_left/click + GAMEPAD_THUMBSTICK_RIGHT_CLICK, // /user/gamepad/input/thumbstick_right/click + GAMEPAD_TRIGGER_LEFT_VALUE, // /user/gamepad/input/trigger_left/value + GAMEPAD_TRIGGER_RIGHT_VALUE, // /user/gamepad/input/trigger_right/value + GAMEPAD_THUMBSTICK_LEFT_X, // /user/gamepad/input/thumbstick_left/x + GAMEPAD_THUMBSTICK_LEFT_Y, // /user/gamepad/input/thumbstick_left/y + GAMEPAD_THUMBSTICK_RIGHT_X, // /user/gamepad/input/thumbstick_right/x + GAMEPAD_THUMBSTICK_RIGHT_Y, // /user/gamepad/input/thumbstick_right/y }; enum class interaction_profile : uint8_t @@ -319,8 +345,8 @@ struct tracking XrFovf fov; }; - // /user/hand/left and /user/hand/right - std::array interaction_profiles; + // /user/hand/left, /user/hand/right and /user/gamepad + std::array interaction_profiles; XrTime production_timestamp; XrTime timestamp; diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index bee76ba2..c19e4504 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -123,6 +123,7 @@ if (WIVRN_BUILD_SERVER) driver/wivrn_hmd.cpp driver/wivrn_controller.cpp driver/wivrn_eye_tracker.cpp + driver/wivrn_gamepad.cpp driver/wivrn_android_face_tracker.cpp driver/wivrn_fb_face2_tracker.cpp driver/wivrn_htc_face_tracker.cpp diff --git a/server/driver/wivrn_controller.cpp b/server/driver/wivrn_controller.cpp index c622e6aa..4c7f4e30 100644 --- a/server/driver/wivrn_controller.cpp +++ b/server/driver/wivrn_controller.cpp @@ -757,6 +757,9 @@ void wivrn_controller::set_inputs(const from_headset::inputs & inputs, const clo for (const auto & input: inputs.values) { + // Gamepad inputs are handled by wivrn_gamepad + if (input.id >= device_id::GAMEPAD_MENU_CLICK) + continue; int64_t last_change_time = input.last_change_time ? clock_offset.from_headset(input.last_change_time) : 0; auto [index, type, device] = map_input(input.id); if (device != device_type) diff --git a/server/driver/wivrn_gamepad.cpp b/server/driver/wivrn_gamepad.cpp new file mode 100644 index 00000000..261a928e --- /dev/null +++ b/server/driver/wivrn_gamepad.cpp @@ -0,0 +1,185 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 JR Lanteigne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "wivrn_gamepad.h" + +#include "wivrn_gamepad_map.h" +#include "wivrn_session.h" + +#include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" + +#include "os/os_time.h" +#include "util/u_logging.h" +#include "utils/method.h" + +namespace wivrn +{ + +static_assert(gp_count == 18); + +wivrn_gamepad::wivrn_gamepad(wivrn::wivrn_session & cnx) : + xrt_device{ + .name = XRT_DEVICE_XBOX_CONTROLLER, + .device_type = XRT_DEVICE_TYPE_GAMEPAD, + .str = "WiVRn Gamepad", + .serial = "WiVRn Gamepad", + .tracking_origin = &origin, + .update_inputs = method_pointer<&wivrn_gamepad::update_inputs>, + .get_tracked_pose = method_pointer<&wivrn_gamepad::get_tracked_pose>, + .set_output = method_pointer<&wivrn_gamepad::set_output>, + .destroy = [](xrt_device *) {}, + }, + origin{ + .type = XRT_TRACKING_TYPE_OTHER, + .initial_offset = XRT_POSE_IDENTITY, + }, + cnx(&cnx) +{ + inputs_array = {}; + inputs_staging = {}; + inputs = inputs_array.data(); + input_count = inputs_array.size(); + + inputs_array[gp_menu].name = XRT_INPUT_XBOX_MENU_CLICK; + inputs_array[gp_view].name = XRT_INPUT_XBOX_VIEW_CLICK; + inputs_array[gp_a].name = XRT_INPUT_XBOX_A_CLICK; + inputs_array[gp_b].name = XRT_INPUT_XBOX_B_CLICK; + inputs_array[gp_x].name = XRT_INPUT_XBOX_X_CLICK; + inputs_array[gp_y].name = XRT_INPUT_XBOX_Y_CLICK; + inputs_array[gp_dpad_down].name = XRT_INPUT_XBOX_DPAD_DOWN_CLICK; + inputs_array[gp_dpad_right].name = XRT_INPUT_XBOX_DPAD_RIGHT_CLICK; + inputs_array[gp_dpad_up].name = XRT_INPUT_XBOX_DPAD_UP_CLICK; + inputs_array[gp_dpad_left].name = XRT_INPUT_XBOX_DPAD_LEFT_CLICK; + inputs_array[gp_shoulder_left].name = XRT_INPUT_XBOX_SHOULDER_LEFT_CLICK; + inputs_array[gp_shoulder_right].name = XRT_INPUT_XBOX_SHOULDER_RIGHT_CLICK; + inputs_array[gp_thumbstick_left_click].name = XRT_INPUT_XBOX_THUMBSTICK_LEFT_CLICK; + inputs_array[gp_thumbstick_right_click].name = XRT_INPUT_XBOX_THUMBSTICK_RIGHT_CLICK; + inputs_array[gp_thumbstick_left].name = XRT_INPUT_XBOX_THUMBSTICK_LEFT; + inputs_array[gp_thumbstick_right].name = XRT_INPUT_XBOX_THUMBSTICK_RIGHT; + inputs_array[gp_trigger_left].name = XRT_INPUT_XBOX_LEFT_TRIGGER_VALUE; + inputs_array[gp_trigger_right].name = XRT_INPUT_XBOX_RIGHT_TRIGGER_VALUE; + + outputs_array = {{ + {.name = XRT_OUTPUT_NAME_XBOX_HAPTIC_LEFT}, + {.name = XRT_OUTPUT_NAME_XBOX_HAPTIC_RIGHT}, + {.name = XRT_OUTPUT_NAME_XBOX_HAPTIC_LEFT_TRIGGER}, + {.name = XRT_OUTPUT_NAME_XBOX_HAPTIC_RIGHT_TRIGGER}, + }}; + output_count = outputs_array.size(); + outputs = outputs_array.data(); +} + +xrt_result_t wivrn_gamepad::update_inputs() +{ + std::lock_guard lock(mutex); + + const int64_t timestamp = os_monotonic_get_ns(); + for (size_t i = 0; i < inputs_array.size(); ++i) + { + inputs_array[i].value = inputs_staging[i].value; + inputs_array[i].active = connected; + inputs_array[i].timestamp = timestamp; + } + + return XRT_SUCCESS; +} + +xrt_result_t wivrn_gamepad::get_tracked_pose(xrt_input_name name, int64_t, xrt_space_relation * out_relation) +{ + // A gamepad has no tracked pose. + *out_relation = {}; + U_LOG_XDEV_UNSUPPORTED_INPUT(this, u_log_get_global_level(), name); + return XRT_ERROR_INPUT_UNSUPPORTED; +} + +void wivrn_gamepad::set_inputs(const from_headset::inputs & inputs) +{ + std::lock_guard lock(mutex); + + for (const auto & value: inputs.values) + { + auto m = map_gamepad(value.id); + if (m.slot == gp_count) + continue; + auto & v = inputs_staging[m.slot].value; + switch (m.kind) + { + case gp_value::button: + case gp_value::dpad: + v.boolean = value.value != 0; + break; + case gp_value::trigger: + v.vec1.x = value.value; + break; + case gp_value::stick_x: + v.vec2.x = value.value; + break; + case gp_value::stick_y: + v.vec2.y = value.value; + break; + } + } +} + +xrt_result_t wivrn_gamepad::set_output(xrt_output_name name, const xrt_output_value * value) +{ + device_id id; + switch (name) + { + case XRT_OUTPUT_NAME_XBOX_HAPTIC_LEFT: + id = device_id::GAMEPAD_HAPTIC_LEFT; + break; + case XRT_OUTPUT_NAME_XBOX_HAPTIC_RIGHT: + id = device_id::GAMEPAD_HAPTIC_RIGHT; + break; + case XRT_OUTPUT_NAME_XBOX_HAPTIC_LEFT_TRIGGER: + id = device_id::GAMEPAD_HAPTIC_LEFT_TRIGGER; + break; + case XRT_OUTPUT_NAME_XBOX_HAPTIC_RIGHT_TRIGGER: + id = device_id::GAMEPAD_HAPTIC_RIGHT_TRIGGER; + break; + default: + return XRT_ERROR_OUTPUT_UNSUPPORTED; + } + + try + { + cnx->send_stream(to_headset::haptics{ + .id = id, + .duration = std::chrono::nanoseconds(value->vibration.duration_ns), + .frequency = value->vibration.frequency, + .amplitude = value->vibration.amplitude}); + } + catch (...) + { + return XRT_ERROR_OUTPUT_REQUEST_FAILURE; + } + return XRT_SUCCESS; +} + +void wivrn_gamepad::set_connected(bool value) +{ + std::lock_guard lock(mutex); + connected = value; + if (not connected) + for (auto & input: inputs_staging) + input.value = {}; +} + +} // namespace wivrn diff --git a/server/driver/wivrn_gamepad.h b/server/driver/wivrn_gamepad.h new file mode 100644 index 00000000..db122384 --- /dev/null +++ b/server/driver/wivrn_gamepad.h @@ -0,0 +1,61 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 JR Lanteigne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "wivrn_packets.h" + +#include "xrt/xrt_defines.h" +#include "xrt/xrt_device.h" +#include "xrt/xrt_tracking.h" + +#include +#include + +namespace wivrn +{ +class wivrn_session; + +// A gamepad forwarded from the headset, exposed as an OpenXR xbox_controller (/user/gamepad). +// Inputs stay inactive until the headset reports a gamepad is connected. +class wivrn_gamepad : public xrt_device +{ + std::mutex mutex; + bool connected = false; + + xrt_tracking_origin origin; + std::array inputs_array; + std::array inputs_staging; // written from the network thread, published by update_inputs + std::array outputs_array; + + wivrn::wivrn_session * cnx; + +public: + using base_t = xrt_device; + wivrn_gamepad(wivrn::wivrn_session & cnx); + + xrt_result_t update_inputs(); + xrt_result_t get_tracked_pose(xrt_input_name name, int64_t at_timestamp_ns, xrt_space_relation * out_relation); + xrt_result_t set_output(xrt_output_name name, const xrt_output_value * value); + + // Update from a forwarded inputs packet (gamepad device_ids). + void set_inputs(const from_headset::inputs &); + void set_connected(bool); +}; + +} // namespace wivrn diff --git a/server/driver/wivrn_gamepad_map.h b/server/driver/wivrn_gamepad_map.h new file mode 100644 index 00000000..3e5b8bbb --- /dev/null +++ b/server/driver/wivrn_gamepad_map.h @@ -0,0 +1,112 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2026 JR Lanteigne + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "wivrn_packets.h" + +namespace wivrn +{ +enum gp_input +{ + gp_menu, + gp_view, + gp_a, + gp_b, + gp_x, + gp_y, + gp_dpad_down, + gp_dpad_right, + gp_dpad_up, + gp_dpad_left, + gp_shoulder_left, + gp_shoulder_right, + gp_thumbstick_left_click, + gp_thumbstick_right_click, + gp_thumbstick_left, + gp_thumbstick_right, + gp_trigger_left, + gp_trigger_right, + gp_count, +}; + +enum class gp_value +{ + button, + dpad, + trigger, + stick_x, + stick_y, +}; + +struct gamepad_input +{ + gp_input slot; + gp_value kind; +}; + +inline gamepad_input map_gamepad(device_id id) +{ + switch (id) + { + case device_id::GAMEPAD_MENU_CLICK: + return {gp_menu, gp_value::button}; + case device_id::GAMEPAD_VIEW_CLICK: + return {gp_view, gp_value::button}; + case device_id::GAMEPAD_A_CLICK: + return {gp_a, gp_value::button}; + case device_id::GAMEPAD_B_CLICK: + return {gp_b, gp_value::button}; + case device_id::GAMEPAD_X_CLICK: + return {gp_x, gp_value::button}; + case device_id::GAMEPAD_Y_CLICK: + return {gp_y, gp_value::button}; + case device_id::GAMEPAD_SHOULDER_LEFT_CLICK: + return {gp_shoulder_left, gp_value::button}; + case device_id::GAMEPAD_SHOULDER_RIGHT_CLICK: + return {gp_shoulder_right, gp_value::button}; + case device_id::GAMEPAD_THUMBSTICK_LEFT_CLICK: + return {gp_thumbstick_left_click, gp_value::button}; + case device_id::GAMEPAD_THUMBSTICK_RIGHT_CLICK: + return {gp_thumbstick_right_click, gp_value::button}; + case device_id::GAMEPAD_DPAD_DOWN_CLICK: + return {gp_dpad_down, gp_value::dpad}; + case device_id::GAMEPAD_DPAD_RIGHT_CLICK: + return {gp_dpad_right, gp_value::dpad}; + case device_id::GAMEPAD_DPAD_UP_CLICK: + return {gp_dpad_up, gp_value::dpad}; + case device_id::GAMEPAD_DPAD_LEFT_CLICK: + return {gp_dpad_left, gp_value::dpad}; + case device_id::GAMEPAD_TRIGGER_LEFT_VALUE: + return {gp_trigger_left, gp_value::trigger}; + case device_id::GAMEPAD_TRIGGER_RIGHT_VALUE: + return {gp_trigger_right, gp_value::trigger}; + case device_id::GAMEPAD_THUMBSTICK_LEFT_X: + return {gp_thumbstick_left, gp_value::stick_x}; + case device_id::GAMEPAD_THUMBSTICK_LEFT_Y: + return {gp_thumbstick_left, gp_value::stick_y}; + case device_id::GAMEPAD_THUMBSTICK_RIGHT_X: + return {gp_thumbstick_right, gp_value::stick_x}; + case device_id::GAMEPAD_THUMBSTICK_RIGHT_Y: + return {gp_thumbstick_right, gp_value::stick_y}; + default: + return {gp_count, gp_value::button}; + } +} + +} // namespace wivrn diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index c97f67d3..d8b39f1b 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -126,6 +126,11 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr connection static_roles.hand_tracking.unobstructed.right = static_xdevs[right_controller_index] = &right_controller; static_xdevs[right_hand_interaction_index = static_xdev_count++] = &right_hand_interaction; + // Expose a gamepad forwarded from the headset as a native OpenXR device (/user/gamepad). + // Always present; inactive until the headset reports a connected gamepad. + roles.gamepad = static_xdev_count; + static_xdevs[static_xdev_count++] = &gamepad_device.emplace(*this); + #if WIVRN_FEATURE_STEAMVR_LIGHTHOUSE auto use_steamvr_lh = configuration().use_steamvr_lh || std::getenv("WIVRN_USE_STEAMVR_LH"); xrt_system_devices * lhdevs = NULL; @@ -543,6 +548,9 @@ static xrt_device_name get_name(interaction_profile profile) void wivrn_session::operator()(const from_headset::tracking & tracking) { + if (gamepad_device) + gamepad_device->set_connected(tracking.interaction_profiles[2] != interaction_profile::none); + auto left = (roles.left == -1 || roles.left == left_controller_index || roles.left == left_hand_interaction_index) ? get_name(tracking.interaction_profiles[0]) : XRT_DEVICE_INVALID; auto right = (roles.right == -1 || roles.right == right_controller_index || roles.right == right_hand_interaction_index) ? get_name(tracking.interaction_profiles[1]) : XRT_DEVICE_INVALID; if (left != roles.left_profile or right != roles.right_profile) @@ -673,6 +681,9 @@ void wivrn_session::operator()(from_headset::inputs && inputs) right_hand_interaction.set_inputs(inputs, offset); else if (roles.right == right_controller_index) right_controller.set_inputs(inputs, offset); + + if (gamepad_device) + gamepad_device->set_inputs(inputs); } void wivrn_session::operator()(from_headset::hid::input && e) diff --git a/server/driver/wivrn_session.h b/server/driver/wivrn_session.h index 366dfc75..29bfab18 100644 --- a/server/driver/wivrn_session.h +++ b/server/driver/wivrn_session.h @@ -30,6 +30,7 @@ #include "wivrn_controller.h" #include "wivrn_eye_tracker.h" #include "wivrn_fb_face2_tracker.h" +#include "wivrn_gamepad.h" #include "wivrn_generic_tracker.h" #include "wivrn_hmd.h" #include "wivrn_htc_face_tracker.h" @@ -95,6 +96,7 @@ class wivrn_session : public xrt_system_devices wivrn_controller right_hand_interaction; int32_t right_hand_interaction_index; std::optional eye_tracker; + std::optional gamepad_device; std::optional android_face_tracker; std::optional fb_face2_tracker; std::optional htc_face_tracker;