mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
feat: SteamVR controller types, phone controls on glasses, flat games as VR panels (#1268)
This commit is contained in:
1 parent
2111197bea
commit
e5b54f6248
54 files changed
+908
-86
No files matched your search
@@ -1060,6 +1060,24 @@ def main(argv):
|
||||
return 3
|
||||
print("droiddeck-agent: Steam overview is ready for shortcut app %d" % prepared["appId"])
|
||||
return 0
|
||||
if argv and argv[0] == "--steam-supports-vr":
|
||||
if len(argv) != 2 or not argv[1].isascii() or not argv[1].isdecimal():
|
||||
print("usage: droiddeck-agent --steam-supports-vr <appid>", file=sys.stderr)
|
||||
return 2
|
||||
# Optional-VR games (Thumper, VRChat) carry vr_supported without vr_only.
|
||||
expression = ("(o => o ? (o.vr_supported === true || o.vr_only === true) : null)"
|
||||
"(window.appStore?.GetAppOverviewByGameID?.(%s))" % json.dumps(argv[1]))
|
||||
value = None
|
||||
deadline = time.monotonic() + 12 # Steam's UI is busiest while a game starts.
|
||||
while value is None and time.monotonic() < deadline:
|
||||
try:
|
||||
value = cdp_eval(Path(base) / "agent", expression, await_promise=False, timeout=8).get("value")
|
||||
except (AgentError, OSError, ValueError):
|
||||
pass
|
||||
if value is None:
|
||||
time.sleep(0.5)
|
||||
print({True: "yes", False: "no"}.get(value, "unknown"))
|
||||
return {True: 0, False: 5}.get(value, 1)
|
||||
if argv and argv[0] == "--steam-asleep":
|
||||
expression = """(() => {
|
||||
const store = globalThis.SuspendResumeStore;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
# Exit 0 when this game run should open as a SteamVR panel: SteamVR is running and Steam
|
||||
# lists no VR support for the app, optional or required. Under SteamVR, Steam stacks a flat game's window behind
|
||||
# its own, so without the panel the game plays unseen.
|
||||
[ "${1:-}" = waitforexitandrun ] || exit 1
|
||||
[ -z "${DROIDDECK_STEAMVR_PANEL:-}" ] || exit 1
|
||||
# Non-Steam shortcuts run with SteamAppId=0 and their CGameID in SteamGameId.
|
||||
app=${SteamAppId:-}
|
||||
[ "$app" = 0 ] && app=${SteamGameId:-}
|
||||
case $app in '' | 0 | *[!0-9]*) exit 1 ;; esac
|
||||
pgrep -x vrserver >/dev/null 2>&1 || exit 1
|
||||
/usr/local/bin/droiddeck-agent --steam-supports-vr "$app" >/dev/null 2>&1
|
||||
[ $? -eq 5 ]
|
||||
@@ -6,6 +6,8 @@ pgrep -x vrserver >/dev/null 2>&1 || {
|
||||
echo 'Start SteamVR before launching a game with droiddeck-steamvr-game.' >&2
|
||||
exit 1
|
||||
}
|
||||
# droiddeck-steamvr-flat skips a run that a launch option already wrapped.
|
||||
export DROIDDECK_STEAMVR_PANEL=1
|
||||
vrdir=${DROIDDECK_VR_DIR:-/usr/local/lib/droiddeck-vr}
|
||||
export VK_LAYER_PATH="$vrdir/layer${VK_LAYER_PATH:+:$VK_LAYER_PATH}"
|
||||
export VK_INSTANCE_LAYERS="VK_LAYER_DROIDDECK_vr${VK_INSTANCE_LAYERS:+:$VK_INSTANCE_LAYERS}"
|
||||
|
||||
@@ -74,6 +74,9 @@ bl_store_launch() {
|
||||
}
|
||||
|
||||
bl_run() {
|
||||
if [ -x /usr/local/bin/droiddeck-steamvr-flat ] && /usr/local/bin/droiddeck-steamvr-flat "${verb:-}"; then
|
||||
set -- /usr/local/bin/droiddeck-steamvr-game "$@"
|
||||
fi
|
||||
if [ -n "${BL_STORE_EXIT:-}" ]; then
|
||||
"$@"
|
||||
rc=$?
|
||||
@@ -463,6 +466,9 @@ verb=$1; shift
|
||||
case $verb in
|
||||
getcompatpath | getnativepath) printf '%s\\n' "$1"; exit 0 ;;
|
||||
esac
|
||||
if [ -x /usr/local/bin/droiddeck-steamvr-flat ] && /usr/local/bin/droiddeck-steamvr-flat "$verb"; then
|
||||
exec /usr/local/bin/droiddeck-steamvr-game /usr/local/bin/droiddeck-fex steam "$verb" "$@"
|
||||
fi
|
||||
exec /usr/local/bin/droiddeck-fex steam "$verb" "$@"
|
||||
"""
|
||||
|
||||
|
||||
@@ -412,6 +412,18 @@ class AgentTest(unittest.TestCase):
|
||||
with patch.object(agent, "cdp_eval", side_effect=agent.AgentError("NO_DEVTOOLS", "down")):
|
||||
self.assertEqual(1, agent.main(["--steam-asleep"]))
|
||||
|
||||
def test_supports_vr_command_separates_vr_flat_and_unknown_apps(self):
|
||||
with patch.dict(os.environ, {"BL_LAUNCH_DIR": str(self.dir.parent)}):
|
||||
for value, code in ((True, 0), (False, 5), (None, 1)):
|
||||
with self.subTest(value=value), patch.object(agent, "cdp_eval", return_value={"value": value}) as ask:
|
||||
self.assertEqual(code, agent.main(["--steam-supports-vr", "204360"]))
|
||||
self.assertIn('GetAppOverviewByGameID?.("204360")', ask.call_args.args[1])
|
||||
self.assertIn("vr_supported", ask.call_args.args[1])
|
||||
with patch.object(agent, "cdp_eval", side_effect=agent.AgentError("NO_DEVTOOLS", "down")):
|
||||
self.assertEqual(1, agent.main(["--steam-supports-vr", "204360"]))
|
||||
for bad in ([], ["1;x"], ["-1"], ["1", "2"]):
|
||||
self.assertEqual(2, agent.main(["--steam-supports-vr"] + bad))
|
||||
|
||||
def test_idle_inhibit_command_reports_whether_steam_reached_the_state(self):
|
||||
with patch.dict(os.environ, {"BL_LAUNCH_DIR": str(self.dir.parent)}):
|
||||
for value, code in ((True, 0), (False, 1), (None, 1)):
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "handcam_shm.h"
|
||||
#include "hand_skeleton.h"
|
||||
#include "index_pose.h"
|
||||
#include "pad_controller_config.h"
|
||||
#include "joycon_shm.h"
|
||||
#include "presenter.h"
|
||||
#include "spatial_tracking.h"
|
||||
@@ -1027,45 +1028,57 @@ public:
|
||||
pose_.qWorldFromDriverRotation.w = pose_.qDriverFromHeadRotation.w = pose_.qRotation.w = 1;
|
||||
}
|
||||
|
||||
void SetProfile(PadProfile profile) { profile_ = profile; }
|
||||
|
||||
EVRInitError Activate(uint32_t id) override {
|
||||
id_ = id;
|
||||
PropertyContainerHandle_t c = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
XrTrackingFile tracking;
|
||||
DroidDeckXrTracking packet{};
|
||||
xrMode_ = tracking.Read(&packet) && XrTrackingFile::Fresh(packet);
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, xrMode_ ? "DroidDeck Quest Touch" : "DroidDeck Pad Hand");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, xrMode_ ? "Meta" : "DroidDeck");
|
||||
const PadProfile profile = xrMode_ ? PadProfile::Touch : profile_;
|
||||
touch_ = profile == PadProfile::Touch;
|
||||
index_ = profile == PadProfile::Index;
|
||||
const char *model = touch_ ? "DroidDeck Quest Touch" : index_ ? "DroidDeck Index Hand" : "DroidDeck Pad Hand";
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, model);
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, touch_ ? "Meta" : index_ ? "Valve" : "DroidDeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, xrMode_ ?
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, touch_ ?
|
||||
(right_ ? "oculus_quest_plus_controller_right" : "oculus_quest_plus_controller_left") :
|
||||
(right_ ? "{indexcontroller}valve_controller_knu_1_0_right" : "{indexcontroller}valve_controller_knu_1_0_left"));
|
||||
VRProperties()->SetStringProperty(c, Prop_ControllerType_String, xrMode_ ? "oculus_touch" : "vive_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_InputProfilePath_String, xrMode_ ?
|
||||
"{oculus}/input/touch_profile.json" : "{htc}/input/vive_controller_profile.json");
|
||||
VRProperties()->SetStringProperty(c, Prop_ControllerType_String, touch_ ? "oculus_touch" : index_ ? "knuckles" : "vive_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_InputProfilePath_String, touch_ ? "{oculus}/input/touch_profile.json" :
|
||||
index_ ? "{indexcontroller}/input/index_controller_profile.json" : "{htc}/input/vive_controller_profile.json");
|
||||
VRProperties()->SetInt32Property(c, Prop_ControllerRoleHint_Int32,
|
||||
right_ ? TrackedControllerRole_RightHand : TrackedControllerRole_LeftHand);
|
||||
VRProperties()->SetInt32Property(c, Prop_DeviceClass_Int32, TrackedDeviceClass_Controller);
|
||||
auto in = VRDriverInput();
|
||||
const bool stick = touch_ || index_;
|
||||
const char *stickPath = touch_ ? "/input/joystick" : "/input/thumbstick";
|
||||
const auto path = [](const char *base, const char *leaf) { return std::string(base) + leaf; };
|
||||
in->CreateBooleanComponent(c, "/input/system/click", &system_);
|
||||
in->CreateBooleanComponent(c, "/input/application_menu/click", &menu_);
|
||||
in->CreateBooleanComponent(c, "/input/grip/click", &grip_);
|
||||
if (!index_) in->CreateBooleanComponent(c, "/input/application_menu/click", &menu_);
|
||||
in->CreateBooleanComponent(c, stick ? "/input/grip/touch" : "/input/grip/click", &grip_);
|
||||
in->CreateBooleanComponent(c, "/input/trigger/click", &triggerClick_);
|
||||
in->CreateScalarComponent(c, "/input/trigger/value", &trigger_, VRScalarType_Absolute, VRScalarUnits_NormalizedOneSided);
|
||||
in->CreateScalarComponent(c, xrMode_ ? "/input/joystick/x" : "/input/trackpad/x", &padX_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateScalarComponent(c, xrMode_ ? "/input/joystick/y" : "/input/trackpad/y", &padY_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateBooleanComponent(c, xrMode_ ? "/input/joystick/click" : "/input/trackpad/click", &padClick_);
|
||||
in->CreateBooleanComponent(c, xrMode_ ? "/input/joystick/touch" : "/input/trackpad/touch", &padTouch_);
|
||||
if (xrMode_) {
|
||||
const char *axisBase = stick ? stickPath : "/input/trackpad";
|
||||
in->CreateScalarComponent(c, path(axisBase, "/x").c_str(), &padX_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateScalarComponent(c, path(axisBase, "/y").c_str(), &padY_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateBooleanComponent(c, path(axisBase, "/click").c_str(), &padClick_);
|
||||
in->CreateBooleanComponent(c, path(axisBase, "/touch").c_str(), &padTouch_);
|
||||
if (stick) {
|
||||
const bool letters = touch_ && !right_;
|
||||
in->CreateScalarComponent(c, "/input/grip/value", &gripValue_, VRScalarType_Absolute, VRScalarUnits_NormalizedOneSided);
|
||||
if (index_) in->CreateScalarComponent(c, "/input/grip/force", &gripForce_, VRScalarType_Absolute, VRScalarUnits_NormalizedOneSided);
|
||||
in->CreateBooleanComponent(c, "/input/trigger/touch", &triggerTouch_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/a/click" : "/input/x/click", &primaryClick_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/b/click" : "/input/y/click", &secondaryClick_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/a/touch" : "/input/x/touch", &primaryTouch_);
|
||||
in->CreateBooleanComponent(c, right_ ? "/input/b/touch" : "/input/y/touch", &secondaryTouch_);
|
||||
in->CreateBooleanComponent(c, "/input/thumbrest/touch", &thumbrestTouch_);
|
||||
in->CreateBooleanComponent(c, letters ? "/input/x/click" : "/input/a/click", &primaryClick_);
|
||||
in->CreateBooleanComponent(c, letters ? "/input/y/click" : "/input/b/click", &secondaryClick_);
|
||||
in->CreateBooleanComponent(c, letters ? "/input/x/touch" : "/input/a/touch", &primaryTouch_);
|
||||
in->CreateBooleanComponent(c, letters ? "/input/y/touch" : "/input/b/touch", &secondaryTouch_);
|
||||
if (touch_) in->CreateBooleanComponent(c, "/input/thumbrest/touch", &thumbrestTouch_);
|
||||
}
|
||||
in->CreateHapticComponent(c, "/output/haptic", &haptic_);
|
||||
Log("%s hand activated (%s)", right_ ? "right" : "left", xrMode_ ? "Quest Touch" : "game pad");
|
||||
Log("%s hand activated (%s)", right_ ? "right" : "left", xrMode_ ? "Quest Touch" : model);
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Deactivate() override { ClearInputs(); id_ = k_unTrackedDeviceIndexInvalid; }
|
||||
@@ -1085,11 +1098,14 @@ public:
|
||||
if (id_ == k_unTrackedDeviceIndexInvalid) return;
|
||||
auto in = VRDriverInput();
|
||||
EVRInputError error = VRInputError_None;
|
||||
for (VRInputComponentHandle_t h : {system_, menu_, grip_, triggerClick_, padClick_, padTouch_}) {
|
||||
for (VRInputComponentHandle_t h : {system_, menu_, grip_, triggerClick_, padClick_, padTouch_, triggerTouch_,
|
||||
primaryClick_, secondaryClick_, primaryTouch_, secondaryTouch_}) {
|
||||
if (!h) continue;
|
||||
const auto result = in->UpdateBooleanComponent(h, false, 0);
|
||||
if (result != VRInputError_None) error = result;
|
||||
}
|
||||
for (VRInputComponentHandle_t h : {trigger_, padX_, padY_}) {
|
||||
for (VRInputComponentHandle_t h : {trigger_, padX_, padY_, gripValue_, gripForce_}) {
|
||||
if (!h) continue;
|
||||
const auto result = in->UpdateScalarComponent(h, 0.0f, 0);
|
||||
if (result != VRInputError_None) error = result;
|
||||
}
|
||||
@@ -1183,16 +1199,14 @@ public:
|
||||
p.result = valid ? TrackingResult_Running_OK : TrackingResult_Running_OutOfRange;
|
||||
auto in = VRDriverInput();
|
||||
const uint32_t b = pad ? pad->buttons : 0;
|
||||
in->UpdateBooleanComponent(system_, right_ && (b & PAD_GUIDE), 0);
|
||||
in->UpdateBooleanComponent(menu_, (b & (right_ ? PAD_START : PAD_SELECT)) != 0, 0);
|
||||
const auto gripError = in->UpdateBooleanComponent(grip_, opticalInputs_.grip, 0);
|
||||
if (gripValue_) in->UpdateScalarComponent(gripValue_, opticalInputs_.grip ? 1.0f : 0.0f, 0);
|
||||
const auto clickError = in->UpdateBooleanComponent(triggerClick_, opticalInputs_.click, 0);
|
||||
const auto triggerError = in->UpdateScalarComponent(trigger_, opticalInputs_.trigger, 0);
|
||||
if (triggerTouch_) in->UpdateBooleanComponent(triggerTouch_, opticalInputs_.trigger > 0.05f, 0);
|
||||
const float x = pad ? pad->axes[right_ ? 2 : 0]/32767.0f : 0;
|
||||
const float y = pad ? -pad->axes[right_ ? 3 : 1]/32767.0f : 0;
|
||||
in->UpdateScalarComponent(padX_, x, 0); in->UpdateScalarComponent(padY_, y, 0);
|
||||
in->UpdateBooleanComponent(padTouch_, x*x+y*y > 0.01f || (b & (right_ ? PAD_A : PAD_X)), 0);
|
||||
in->UpdateBooleanComponent(padClick_, (b & (right_ ? (PAD_A|PAD_R3) : PAD_X)) != 0, 0);
|
||||
UpdatePadButtons(b, x, y, true);
|
||||
LogOpticalInputs(hand, inputValid, valid, now, connection, origin, inputEpochs, gripError, clickError, triggerError);
|
||||
pose_ = p;
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, p, sizeof(p));
|
||||
@@ -1251,19 +1265,42 @@ public:
|
||||
auto in = VRDriverInput();
|
||||
const uint32_t b = pad->buttons;
|
||||
const float trig = pad->axes[right_ ? 4 : 5] / 255.0f;
|
||||
const bool touch = !sticksMoveHands && stickX * stickX + stickY * stickY > 0.01f;
|
||||
in->UpdateBooleanComponent(system_, right_ && (b & PAD_GUIDE), 0);
|
||||
in->UpdateBooleanComponent(menu_, (b & (right_ ? PAD_START : PAD_SELECT)) != 0, 0);
|
||||
in->UpdateBooleanComponent(grip_, (b & (right_ ? PAD_RB : PAD_LB)) != 0, 0);
|
||||
const bool grip = (b & (right_ ? PAD_RB : PAD_LB)) != 0;
|
||||
in->UpdateScalarComponent(trigger_, trig, 0);
|
||||
in->UpdateBooleanComponent(triggerClick_, trig > 0.6f, 0);
|
||||
in->UpdateScalarComponent(padX_, touch ? stickX : 0.0f, 0);
|
||||
in->UpdateScalarComponent(padY_, touch ? stickY : 0.0f, 0);
|
||||
in->UpdateBooleanComponent(padTouch_, touch || (b & (right_ ? PAD_A : PAD_X)), 0);
|
||||
in->UpdateBooleanComponent(padClick_, (b & (right_ ? (PAD_A | PAD_R3) : PAD_X)) != 0, 0);
|
||||
if (triggerTouch_) in->UpdateBooleanComponent(triggerTouch_, trig > 0.05f, 0);
|
||||
in->UpdateBooleanComponent(grip_, grip, 0);
|
||||
if (gripValue_) in->UpdateScalarComponent(gripValue_, grip ? 1.0f : 0.0f, 0);
|
||||
if (gripForce_) in->UpdateScalarComponent(gripForce_, grip ? 1.0f : 0.0f, 0);
|
||||
UpdatePadButtons(b, stickX, stickY, !sticksMoveHands);
|
||||
}
|
||||
|
||||
private:
|
||||
// Face buttons, menus and the stick, for both the game pad and optical hands.
|
||||
void UpdatePadButtons(uint32_t b, float stickX, float stickY, bool stickToGame) {
|
||||
auto in = VRDriverInput();
|
||||
const bool touch = stickToGame && stickX * stickX + stickY * stickY > 0.01f;
|
||||
in->UpdateBooleanComponent(system_, right_ && (b & PAD_GUIDE), 0);
|
||||
in->UpdateScalarComponent(padX_, touch ? stickX : 0.0f, 0);
|
||||
in->UpdateScalarComponent(padY_, touch ? stickY : 0.0f, 0);
|
||||
if (!touch_ && !index_) {
|
||||
in->UpdateBooleanComponent(menu_, (b & (right_ ? PAD_START : PAD_SELECT)) != 0, 0);
|
||||
in->UpdateBooleanComponent(padTouch_, touch || (b & (right_ ? PAD_A : PAD_X)), 0);
|
||||
in->UpdateBooleanComponent(padClick_, (b & (right_ ? (PAD_A | PAD_R3) : PAD_X)) != 0, 0);
|
||||
return;
|
||||
}
|
||||
// Touch and Index: face buttons are their own inputs and the stick is a thumbstick.
|
||||
const bool primary = b & (right_ ? PAD_A : PAD_X), secondary = b & (right_ ? PAD_B : PAD_Y);
|
||||
const bool both = (b & PAD_L3) && (b & PAD_R3);
|
||||
if (menu_) in->UpdateBooleanComponent(menu_, !right_ && (b & (PAD_START | PAD_SELECT)), 0);
|
||||
in->UpdateBooleanComponent(primaryClick_, primary, 0);
|
||||
in->UpdateBooleanComponent(secondaryClick_, secondary, 0);
|
||||
in->UpdateBooleanComponent(primaryTouch_, primary, 0);
|
||||
in->UpdateBooleanComponent(secondaryTouch_, secondary, 0);
|
||||
in->UpdateBooleanComponent(padTouch_, touch, 0);
|
||||
in->UpdateBooleanComponent(padClick_, !both && (b & (right_ ? PAD_R3 : PAD_L3)), 0);
|
||||
}
|
||||
|
||||
void LogOpticalInputs(const dd_spatial_hand &hand, bool inputValid, bool poseValid, int64_t now,
|
||||
uint64_t connection, uint64_t origin, uint16_t inputEpochs, EVRInputError gripError,
|
||||
EVRInputError clickError, EVRInputError triggerError) {
|
||||
@@ -1297,14 +1334,15 @@ private:
|
||||
static constexpr int16_t kGrabTrigger = 153;
|
||||
static constexpr float kStickDeadzone = 0.12f, kHandRangeX = 0.40f, kHandRangeUp = 0.50f, kHandRangeDown = 0.30f, kHandSmoothing = 0.05f, kHandPivot = 0.45f;
|
||||
bool right_;
|
||||
bool xrMode_ = false;
|
||||
bool xrMode_ = false, touch_ = false, index_ = false;
|
||||
PadProfile profile_ = PadProfile::Index;
|
||||
uint32_t id_ = k_unTrackedDeviceIndexInvalid;
|
||||
DriverPose_t pose_ = {};
|
||||
float pull_ = 0.0f, handX_ = 0.0f, handY_ = 0.0f;
|
||||
std::chrono::steady_clock::time_point lastUpdate_ = std::chrono::steady_clock::now();
|
||||
VRInputComponentHandle_t system_ = 0, menu_ = 0, grip_ = 0, triggerClick_ = 0, trigger_ = 0, padX_ = 0, padY_ = 0,
|
||||
padClick_ = 0, padTouch_ = 0, haptic_ = 0;
|
||||
VRInputComponentHandle_t gripValue_ = 0, triggerTouch_ = 0, primaryClick_ = 0, secondaryClick_ = 0,
|
||||
VRInputComponentHandle_t gripValue_ = 0, gripForce_ = 0, triggerTouch_ = 0, primaryClick_ = 0, secondaryClick_ = 0,
|
||||
primaryTouch_ = 0, secondaryTouch_ = 0, thumbrestTouch_ = 0;
|
||||
};
|
||||
|
||||
@@ -1552,9 +1590,9 @@ private:
|
||||
if (fresh) PublishChaperone(xr);
|
||||
DriverPose_t head = GetPose();
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(head));
|
||||
for (int hand = 0; hand < 2; hand++) if (hands_[hand]) {
|
||||
for (int hand = 0; hand < 2; hand++) if (CardboardController *pad = ActiveHand(hand)) {
|
||||
DroidDeckXrHand state = fresh ? xr.hands[hand] : DroidDeckXrHand{};
|
||||
hands_[hand]->UpdateXr(state, fresh ? xr.predicted_ns : 0);
|
||||
pad->UpdateXr(state, fresh ? xr.predicted_ns : 0);
|
||||
}
|
||||
VRDriverInput()->UpdateBooleanComponent(proximity_, fresh, 0);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
||||
@@ -1594,9 +1632,10 @@ private:
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(DriverPose_t));
|
||||
const bool lumaJoyCons = (spatial.flags & DD_SPATIAL_ACTIVE) && (spatial.flags & DD_SPATIAL_JOYCONS);
|
||||
const bool optical = !lumaJoyCons && (spatial.flags & DD_SPATIAL_ACTIVE) && (spatial.flags & DD_SPATIAL_HANDS);
|
||||
if (!optical && havePad && (pad.buttons & PAD_L3) && !(lastPad_.buttons & PAD_L3)) {
|
||||
sticksMoveHands_ = !sticksMoveHands_;
|
||||
Log("sticks now move %s", sticksMoveHands_ ? "the hands" : "in the game");
|
||||
const uint32_t chord = PAD_L3 | PAD_R3;
|
||||
if (!optical && havePad && (pad.buttons & chord) == chord && (lastPad_.buttons & chord) != chord) {
|
||||
sticksMoveHands_.store(!sticksMoveHands_.load());
|
||||
Log("sticks now move %s", sticksMoveHands_.load() ? "the hands" : "in the game");
|
||||
}
|
||||
const float dt = std::min(std::chrono::duration<float>(now - lastReach_).count(), 0.1f);
|
||||
lastReach_ = now;
|
||||
@@ -1638,7 +1677,7 @@ private:
|
||||
if (joyCon || bare) wantHand_[side] = true;
|
||||
const bool covered = handControllers_[side] && handControllers_[side]->Update(joyCon, bare, head.qRotation, nowNs,
|
||||
lumaJoyCons ? &spatial : nullptr);
|
||||
CardboardController *hand = hands_[side];
|
||||
CardboardController *hand = ActiveHand(side);
|
||||
if (!hand) continue;
|
||||
if (otherControllers_ || covered || bare || lumaJoyCons) {
|
||||
if (!padHandOff_[side]) hand->Disconnect();
|
||||
@@ -1650,7 +1689,7 @@ private:
|
||||
havePad ? &pad : nullptr, SpatialNow(), spatial.connection, spatial.origin,
|
||||
uint16_t(spatial.input_epochs >> (side*16)),
|
||||
spatial.flags & DD_SPATIAL_STEADY_AIM ? &spatial.head : nullptr);
|
||||
else hand->Update(head, havePad ? &pad : nullptr, sticksMoveHands_, reach_);
|
||||
else hand->Update(head, havePad ? &pad : nullptr, sticksMoveHands_.load(), reach_);
|
||||
}
|
||||
if (havePad) lastPad_ = pad;
|
||||
lastSent = now;
|
||||
@@ -1713,6 +1752,18 @@ private:
|
||||
PropertyContainerHandle_t container_ = k_ulInvalidPropertyContainer;
|
||||
public:
|
||||
void SetHands(CardboardController *left, CardboardController *right) { hands_[0] = left; hands_[1] = right; }
|
||||
void SetSticksMoveHands(bool on) { sticksMoveHands_ = on; }
|
||||
|
||||
// Called only from the pose thread: a replaced pair is disconnected there, so it never races an update.
|
||||
CardboardController *ActiveHand(int side) {
|
||||
CardboardController *wanted = hands_[side].load();
|
||||
if (wanted != active_[side]) {
|
||||
if (active_[side]) active_[side]->Disconnect();
|
||||
active_[side] = wanted;
|
||||
padHandOff_[side] = false;
|
||||
}
|
||||
return wanted;
|
||||
}
|
||||
void SetHandControllers(HandController *left, HandController *right) { handControllers_[0] = left; handControllers_[1] = right; }
|
||||
bool WantsHand(int side) const { return wantHand_[side].load(); }
|
||||
|
||||
@@ -1729,12 +1780,13 @@ private:
|
||||
PadState lastPad_;
|
||||
VRInputComponentHandle_t proximity_ = 0;
|
||||
bool worn_ = true;
|
||||
bool sticksMoveHands_ = true;
|
||||
std::atomic<bool> sticksMoveHands_{false};
|
||||
float reach_ = 0.0f;
|
||||
std::chrono::steady_clock::time_point lastReach_ = std::chrono::steady_clock::now();
|
||||
std::chrono::steady_clock::time_point lastScan_{};
|
||||
bool otherControllers_ = false;
|
||||
CardboardController *hands_[2] = {nullptr, nullptr};
|
||||
std::atomic<CardboardController *> hands_[2] = {nullptr, nullptr};
|
||||
CardboardController *active_[2] = {nullptr, nullptr};
|
||||
bool padHandOff_[2] = {false, false};
|
||||
ShmFile<droiddeck_joycon> joyConFile_{kJoyConPath, DROIDDECK_JOYCON_MAGIC, DROIDDECK_JOYCON_VERSION, "Joy-Con"};
|
||||
uint32_t lastJoyConSeq_ = 0;
|
||||
@@ -1767,11 +1819,8 @@ public:
|
||||
Log("DroidDeck Cardboard driver loaded");
|
||||
if (!VRServerDriverHost()->TrackedDeviceAdded("droiddeck-cardboard-hmd", TrackedDeviceClass_HMD, &hmd_))
|
||||
Log("TrackedDeviceAdded failed");
|
||||
if (VRSettings()->GetBool(kSettingsSection, "gamepadControllers")) {
|
||||
VRServerDriverHost()->TrackedDeviceAdded("droiddeck-pad-left", TrackedDeviceClass_Controller, &left_);
|
||||
VRServerDriverHost()->TrackedDeviceAdded("droiddeck-pad-right", TrackedDeviceClass_Controller, &right_);
|
||||
hmd_.SetHands(&left_, &right_);
|
||||
}
|
||||
padsEnabled_ = VRSettings()->GetBool(kSettingsSection, "gamepadControllers");
|
||||
ApplyPadConfig(true);
|
||||
hmd_.SetHandControllers(&handLeft_, &handRight_);
|
||||
return VRInitError_None;
|
||||
}
|
||||
@@ -1782,6 +1831,11 @@ public:
|
||||
}
|
||||
const char *const *GetInterfaceVersions() override { return k_InterfaceVersions; }
|
||||
void RunFrame() override {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
if (now - lastConfigCheck_ > std::chrono::seconds(1)) {
|
||||
lastConfigCheck_ = now;
|
||||
ApplyPadConfig(false);
|
||||
}
|
||||
for (int side = 0; side < 2; side++) {
|
||||
if (handAdded_[side] || !hmd_.WantsHand(side)) continue;
|
||||
handAdded_[side] = true;
|
||||
@@ -1803,9 +1857,11 @@ public:
|
||||
if (!h.componentHandle) continue;
|
||||
if (h.componentHandle == handLeft_.Haptic()) SendRumble(DROIDDECK_JOYCON_RUMBLE_SLOT, h);
|
||||
else if (h.componentHandle == handRight_.Haptic()) SendRumble(DROIDDECK_JOYCON_RUMBLE_SLOT + 1, h);
|
||||
else if (h.componentHandle == left_.Haptic() || h.componentHandle == right_.Haptic()) {
|
||||
if (left_.XrMode()) SendHaptic(h.componentHandle == right_.Haptic() ? 1 : 0, h);
|
||||
else for (int p = 0; p < kPadProfiles; p++) {
|
||||
if (!padAdded_[p] || (h.componentHandle != pads_[p][0].Haptic() && h.componentHandle != pads_[p][1].Haptic())) continue;
|
||||
if (pads_[p][0].XrMode()) SendHaptic(h.componentHandle == pads_[p][1].Haptic() ? 1 : 0, h);
|
||||
else SendRumble(0, h);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1852,8 +1908,46 @@ private:
|
||||
target.frequency = std::max(pulse.fFrequency, 0.f);
|
||||
__atomic_store_n(&haptics_->seq, (sequence | 1u) + 1u, __ATOMIC_RELEASE);
|
||||
}
|
||||
// SteamVR fixes a device's input profile when it activates, so each type gets its own pair,
|
||||
// added the first time it is chosen; devices cannot be removed, only disconnected.
|
||||
void ApplyPadConfig(bool initial) {
|
||||
// The file is a few bytes, so compare what it says; two quick writes can share an mtime.
|
||||
const PadControllerConfig pads = read_pad_controller_config("/run/droiddeck/" DROIDDECK_VR_CONTROLLERS_FILE);
|
||||
if (!initial && pads.profile == configProfile_ && pads.sticksMoveHands == configSticks_) return;
|
||||
configProfile_ = pads.profile;
|
||||
if (initial || pads.sticksMoveHands != configSticks_) hmd_.SetSticksMoveHands(pads.sticksMoveHands);
|
||||
configSticks_ = pads.sticksMoveHands;
|
||||
const PadProfile profile = padsEnabled_ ? pads.profile : PadProfile::Off;
|
||||
if (!initial && profile == profile_) return;
|
||||
profile_ = profile;
|
||||
Log("game pad hands: %s, sticks start in %s", pad_profile_name(profile), pads.sticksMoveHands ? "the hands" : "the game");
|
||||
if (profile == PadProfile::Off) { hmd_.SetHands(nullptr, nullptr); return; }
|
||||
const int p = int(profile) - 1;
|
||||
if (!padAdded_[p]) {
|
||||
static const char *const kNames[kPadProfiles][2] = {
|
||||
{"droiddeck-pad-left", "droiddeck-pad-right"},
|
||||
{"droiddeck-pad-touch-left", "droiddeck-pad-touch-right"},
|
||||
{"droiddeck-pad-vive-left", "droiddeck-pad-vive-right"},
|
||||
};
|
||||
for (int side = 0; side < 2; side++) {
|
||||
pads_[p][side].SetProfile(profile);
|
||||
VRServerDriverHost()->TrackedDeviceAdded(kNames[p][side], TrackedDeviceClass_Controller, &pads_[p][side]);
|
||||
}
|
||||
padAdded_[p] = true;
|
||||
}
|
||||
hmd_.SetHands(&pads_[p][0], &pads_[p][1]);
|
||||
}
|
||||
|
||||
static constexpr int kPadProfiles = 3;
|
||||
CardboardHmd hmd_;
|
||||
CardboardController left_{false}, right_{true};
|
||||
CardboardController pads_[kPadProfiles][2] = {{CardboardController(false), CardboardController(true)},
|
||||
{CardboardController(false), CardboardController(true)},
|
||||
{CardboardController(false), CardboardController(true)}};
|
||||
bool padAdded_[kPadProfiles] = {false, false, false};
|
||||
bool padsEnabled_ = true, configSticks_ = false;
|
||||
PadProfile profile_ = PadProfile::Off;
|
||||
PadProfile configProfile_ = PadProfile::Index;
|
||||
std::chrono::steady_clock::time_point lastConfigCheck_{};
|
||||
HandController handLeft_{false}, handRight_{true};
|
||||
bool handAdded_[2] = {false, false};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#define DROIDDECK_VR_CONTROLLERS_FILE "droiddeck-vr-controllers"
|
||||
|
||||
enum class PadProfile { Off, Index, Touch, Vive };
|
||||
|
||||
struct PadControllerConfig {
|
||||
PadProfile profile = PadProfile::Index;
|
||||
bool sticksMoveHands = false;
|
||||
};
|
||||
|
||||
// One key=value per line, written by the app before SteamVR starts.
|
||||
inline PadControllerConfig pad_controller_config(const char *text) {
|
||||
PadControllerConfig config;
|
||||
if (!text) return config;
|
||||
for (const char *line = text; *line;) {
|
||||
const char *end = std::strchr(line, '\n');
|
||||
const size_t length = end ? size_t(end - line) : std::strlen(line);
|
||||
const auto is = [&](const char *expected) {
|
||||
return std::strlen(expected) == length && !std::strncmp(line, expected, length);
|
||||
};
|
||||
if (is("emulate=off")) config.profile = PadProfile::Off;
|
||||
else if (is("emulate=index")) config.profile = PadProfile::Index;
|
||||
else if (is("emulate=touch")) config.profile = PadProfile::Touch;
|
||||
else if (is("emulate=vive")) config.profile = PadProfile::Vive;
|
||||
else if (is("sticks=hands")) config.sticksMoveHands = true;
|
||||
else if (is("sticks=game")) config.sticksMoveHands = false;
|
||||
if (!end) break;
|
||||
line = end + 1;
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
inline PadControllerConfig read_pad_controller_config(const char *path) {
|
||||
char text[256] = {};
|
||||
if (FILE *f = std::fopen(path, "r")) {
|
||||
const size_t n = std::fread(text, 1, sizeof(text) - 1, f);
|
||||
text[n] = 0;
|
||||
std::fclose(f);
|
||||
}
|
||||
return pad_controller_config(text);
|
||||
}
|
||||
|
||||
inline const char *pad_profile_name(PadProfile profile) {
|
||||
switch (profile) {
|
||||
case PadProfile::Off: return "off";
|
||||
case PadProfile::Index: return "Index";
|
||||
case PadProfile::Touch: return "Quest Touch";
|
||||
case PadProfile::Vive: return "Vive";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
@@ -80,6 +80,7 @@ int main() {
|
||||
Context context;
|
||||
VRDriverContext() = &context;
|
||||
CardboardController controller(false);
|
||||
controller.SetProfile(PadProfile::Vive);
|
||||
assert(controller.Activate(1) == VRInitError_None);
|
||||
const auto gripHandle=context.input.handles.at("/input/grip/click");
|
||||
const auto clickHandle=context.input.handles.at("/input/trigger/click");
|
||||
@@ -260,4 +261,46 @@ int main() {
|
||||
joyCon.Update(nullptr, false, {1,0,0,0}, now, &spatial);
|
||||
assert(!joyCon.GetPose().deviceIsConnected && context.input.values.at(joyTrigger) == 0);
|
||||
puts("PASS: optical controller recovery and Luma Joy-Con camera, fallback, head loss and origin changes");
|
||||
|
||||
for (const PadProfile profile : {PadProfile::Index, PadProfile::Touch}) {
|
||||
auto &padContext = context;
|
||||
padContext.input.handles.clear();
|
||||
CardboardController pad(true);
|
||||
pad.SetProfile(profile);
|
||||
assert(pad.Activate(2) == VRInitError_None);
|
||||
const auto &h = padContext.input.handles;
|
||||
const std::string stick = profile == PadProfile::Touch ? "/input/joystick" : "/input/thumbstick";
|
||||
assert(!h.count("/input/trackpad/x") && h.count("/input/b/click") && h.count("/input/grip/value"));
|
||||
assert(h.count("/input/application_menu/click") == (profile == PadProfile::Touch));
|
||||
DriverPose_t head{};
|
||||
head.qRotation.w = 1;
|
||||
PadState state{};
|
||||
state.buttons = PAD_A | PAD_RB;
|
||||
state.axes[2] = 32767;
|
||||
state.axes[4] = 255;
|
||||
pad.Update(head, &state, false, 0);
|
||||
const auto value = [&](const std::string &name) { return padContext.input.values.at(h.at(name)); };
|
||||
assert(value("/input/a/click") == 1 && value("/input/b/click") == 0);
|
||||
assert(value(stick + "/x") == 1 && value(stick + "/touch") == 1 && value(stick + "/click") == 0);
|
||||
assert(value("/input/grip/value") == 1 && value("/input/trigger/value") == 1);
|
||||
pad.Update(head, &state, true, 0);
|
||||
assert(value(stick + "/x") == 0);
|
||||
state = {};
|
||||
state.buttons = PAD_R3;
|
||||
pad.Update(head, &state, false, 0);
|
||||
assert(value(stick + "/click") == 1);
|
||||
state.buttons = PAD_L3 | PAD_R3;
|
||||
pad.Update(head, &state, false, 0);
|
||||
assert(value(stick + "/click") == 0);
|
||||
dd_spatial_hand optical{};
|
||||
state = {};
|
||||
state.buttons = PAD_A;
|
||||
pad.UpdateOptical(optical, false, &state, 1000000000, 1, 1);
|
||||
assert(value("/input/a/click") == 1 && value(stick + "/click") == 0 && value(stick + "/touch") == 0);
|
||||
state.buttons = PAD_B | PAD_R3;
|
||||
pad.UpdateOptical(optical, false, &state, 1000000001, 1, 1);
|
||||
assert(value("/input/b/click") == 1 && value("/input/a/click") == 0 && value(stick + "/click") == 1);
|
||||
assert(!padContext.input.values.count(0));
|
||||
}
|
||||
puts("PASS: game pad hands as Index and Quest Touch thumbstick controllers");
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "../src/pad_controller_config.h"
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
|
||||
int main() {
|
||||
auto config = pad_controller_config(nullptr);
|
||||
assert(config.profile == PadProfile::Index && !config.sticksMoveHands);
|
||||
config = pad_controller_config("emulate=touch\nsticks=hands\n");
|
||||
assert(config.profile == PadProfile::Touch && config.sticksMoveHands);
|
||||
config = pad_controller_config("emulate=off");
|
||||
assert(config.profile == PadProfile::Off);
|
||||
config = pad_controller_config("emulate=vive\nsticks=game");
|
||||
assert(config.profile == PadProfile::Vive && !config.sticksMoveHands);
|
||||
config = pad_controller_config("emulate=offline\nsticks=handsome\n");
|
||||
assert(config.profile == PadProfile::Index && !config.sticksMoveHands);
|
||||
assert(read_pad_controller_config("/nonexistent/droiddeck").profile == PadProfile::Index);
|
||||
std::puts("pad controller config ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user