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
+3
View File
@@ -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)
+185
View File
@@ -0,0 +1,185 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 JR Lanteigne <root@dnim.dev>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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
+61
View File
@@ -0,0 +1,61 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 JR Lanteigne <root@dnim.dev>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "wivrn_packets.h"
#include "xrt/xrt_defines.h"
#include "xrt/xrt_device.h"
#include "xrt/xrt_tracking.h"
#include <array>
#include <mutex>
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<xrt_input, 18> inputs_array;
std::array<xrt_input, 18> inputs_staging; // written from the network thread, published by update_inputs
std::array<xrt_output, 4> 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
+112
View File
@@ -0,0 +1,112 @@
/*
* WiVRn VR streaming
* Copyright (C) 2026 JR Lanteigne <root@dnim.dev>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#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
+11
View File
@@ -126,6 +126,11 @@ wivrn::wivrn_session::wivrn_session(std::unique_ptr<wivrn_connection> 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)
+2
View File
@@ -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<wivrn_eye_tracker> eye_tracker;
std::optional<wivrn_gamepad> gamepad_device;
std::optional<wivrn_android_face_tracker> android_face_tracker;
std::optional<wivrn_fb_face2_tracker> fb_face2_tracker;
std::optional<wivrn_htc_face_tracker> htc_face_tracker;