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.
This commit is contained in:
JR Lanteigne
2026-07-08 19:10:50 +02:00
committed by xytovl
parent 9935203cfb
commit aeecc857de
14 changed files with 580 additions and 103 deletions
+8 -1
View File
@@ -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;
+43 -7
View File
@@ -39,6 +39,7 @@
#include <boost/locale.hpp>
#include <boost/url/parse.hpp>
#include <chrono>
#include <cstring>
#include <ctype.h>
#include <exception>
#include <magic_enum.hpp>
@@ -602,6 +603,35 @@ static std::vector<interaction_profile> 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<std::string_view, XrActionType> action_suffixes[] =
@@ -634,11 +664,15 @@ static const std::pair<std::string_view, XrActionType> 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)
+5
View File
@@ -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()
+27 -1
View File
@@ -138,6 +138,27 @@ static const std::unordered_map<std::string, device_id> 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> scenes::stream::create(std::unique_ptr<wivrn_ses
std::tuple(device_id::LEFT_THUMB_HAPTIC, "/user/hand/left", "/output/haptic_thumb"),
std::tuple(device_id::RIGHT_THUMB_HAPTIC, "/user/hand/right", "/output/haptic_thumb"),
std::tuple(device_id::LEFT_THUMB_HAPTIC, "/user/hand/left", "/output/haptic_thumb_fb"),
std::tuple(device_id::RIGHT_THUMB_HAPTIC, "/user/hand/right", "/output/haptic_thumb_fb")})
std::tuple(device_id::RIGHT_THUMB_HAPTIC, "/user/hand/right", "/output/haptic_thumb_fb"),
std::tuple(device_id::GAMEPAD_HAPTIC_LEFT, "/user/gamepad", "/output/haptic_left"),
std::tuple(device_id::GAMEPAD_HAPTIC_RIGHT, "/user/gamepad", "/output/haptic_right"),
std::tuple(device_id::GAMEPAD_HAPTIC_LEFT_TRIGGER, "/user/gamepad", "/output/haptic_left_trigger"),
std::tuple(device_id::GAMEPAD_HAPTIC_RIGHT_TRIGGER, "/user/gamepad", "/output/haptic_right_trigger")})
{
if (auto action = application::get_action(std::string(path) + output); action.first)
{
+1 -1
View File
@@ -76,7 +76,7 @@ private:
std::unique_ptr<wivrn_session> network_session;
std::thread network_thread;
thread_safe<to_headset::tracking_control> tracking_control{};
std::array<std::atomic<interaction_profile>, 2> interaction_profiles; // left and right hand
std::array<std::atomic<interaction_profile>, 3> interaction_profiles; // left hand, right hand, gamepad
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;
+3 -1
View File
@@ -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
{