mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 04:46:08 +00:00
feat: add immersive Quest SteamVR to the standard app (#910)
This commit is contained in:
1 parent
559120313a
commit
679e3d8934
73 files changed
+5916
-289
No files matched your search
@@ -7,6 +7,7 @@
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
@@ -17,6 +18,11 @@
|
||||
|
||||
#include "../../../app/src/main/cpp/waylandcomp/src/headpose_shm.h"
|
||||
#include "presenter.h"
|
||||
#include "xr_tracking_file.h"
|
||||
#include "xr_frame_math.h"
|
||||
#include "xr_vsync_timing.h"
|
||||
#include "xr_touch_input.h"
|
||||
#include "xr_chaperone.h"
|
||||
|
||||
#define DD_EXPORT extern "C" __attribute__((visibility("default")))
|
||||
|
||||
@@ -87,6 +93,27 @@ HmdQuaternion_t QuatFromMatrix(const float m[9]) {
|
||||
return q;
|
||||
}
|
||||
|
||||
HmdMatrix34_t XrPoseMatrix(const DroidDeckXrPose &pose) {
|
||||
const float qx = pose.orientation[0], qy = pose.orientation[1], qz = pose.orientation[2], qw = pose.orientation[3];
|
||||
HmdMatrix34_t matrix{};
|
||||
matrix.m[0][0] = 1 - 2 * (qy * qy + qz * qz); matrix.m[0][1] = 2 * (qx * qy - qw * qz); matrix.m[0][2] = 2 * (qx * qz + qw * qy);
|
||||
matrix.m[1][0] = 2 * (qx * qy + qw * qz); matrix.m[1][1] = 1 - 2 * (qx * qx + qz * qz); matrix.m[1][2] = 2 * (qy * qz - qw * qx);
|
||||
matrix.m[2][0] = 2 * (qx * qz - qw * qy); matrix.m[2][1] = 2 * (qy * qz + qw * qx); matrix.m[2][2] = 1 - 2 * (qx * qx + qy * qy);
|
||||
for (int axis = 0; axis < 3; axis++) matrix.m[axis][3] = pose.position[axis];
|
||||
return matrix;
|
||||
}
|
||||
|
||||
HmdMatrix34_t XrEyeToHead(const DroidDeckXrPose &head, const DroidDeckXrPose &eye) {
|
||||
HmdMatrix34_t headMatrix = XrPoseMatrix(head), eyeMatrix = XrPoseMatrix(eye), result{};
|
||||
for (int row = 0; row < 3; row++) {
|
||||
for (int column = 0; column < 3; column++)
|
||||
for (int inner = 0; inner < 3; inner++) result.m[row][column] += headMatrix.m[inner][row] * eyeMatrix.m[inner][column];
|
||||
for (int inner = 0; inner < 3; inner++)
|
||||
result.m[row][3] += headMatrix.m[inner][row] * (eyeMatrix.m[inner][3] - headMatrix.m[inner][3]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
class VirtualDisplay : public IVRVirtualDisplay {
|
||||
public:
|
||||
void Present(const PresentInfo_t *info, uint32_t size) override {
|
||||
@@ -114,26 +141,32 @@ private:
|
||||
|
||||
class Vsync {
|
||||
public:
|
||||
static constexpr std::chrono::nanoseconds kInterval{16666667};
|
||||
static constexpr auto kInterval = XrVsyncTiming::kDefaultPeriod;
|
||||
static constexpr std::chrono::microseconds kLead{2000};
|
||||
|
||||
static void Start() {
|
||||
if (running_.exchange(true)) return;
|
||||
timing_.Reset();
|
||||
thread_ = std::thread([] {
|
||||
XrTrackingFile xrFile;
|
||||
auto next = std::chrono::steady_clock::now() + kInterval;
|
||||
while (running_) {
|
||||
auto interval = kInterval;
|
||||
DroidDeckXrTracking xr;
|
||||
if (xrFile.Read(&xr) && XrTrackingFile::Fresh(xr)) {
|
||||
interval = std::chrono::nanoseconds(xr.period_ns);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
next = timing_.Target(now, next, xr.predicted_ns, interval);
|
||||
}
|
||||
auto announce = next - kLead;
|
||||
std::this_thread::sleep_until(announce);
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
double offset = std::chrono::duration<double>(announce - now).count();
|
||||
double offset = std::chrono::duration<double>(next - now).count();
|
||||
timing_.Record(next, interval);
|
||||
VRServerDriverHost()->VsyncEvent(offset);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
last_ = next;
|
||||
}
|
||||
next += kInterval;
|
||||
next += interval;
|
||||
now = std::chrono::steady_clock::now();
|
||||
while (next <= now) next += kInterval;
|
||||
while (next <= now) next += interval;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -141,20 +174,22 @@ public:
|
||||
running_ = false;
|
||||
if (thread_.joinable()) thread_.join();
|
||||
}
|
||||
static std::chrono::steady_clock::time_point LastTick() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return last_;
|
||||
static std::chrono::steady_clock::time_point Deadline(uint32_t framesToThrottle) {
|
||||
return timing_.Deadline(framesToThrottle);
|
||||
}
|
||||
|
||||
private:
|
||||
static inline std::atomic<bool> running_{false};
|
||||
static inline std::thread thread_;
|
||||
static inline std::mutex mutex_;
|
||||
static inline std::chrono::steady_clock::time_point last_{};
|
||||
static inline XrVsyncTiming timing_;
|
||||
};
|
||||
|
||||
class DirectMode : public IVRDriverDirectModeComponent {
|
||||
public:
|
||||
void ConfigureXr(const DroidDeckXrTracking &tracking) {
|
||||
xrWidth_ = tracking.eyes[0].width * 2; xrHeight_ = tracking.eyes[0].height;
|
||||
for (int eye = 0; eye < 2; eye++) eyeToHead_[eye] = XrEyeToHead(tracking.head, tracking.eyes[eye].pose);
|
||||
}
|
||||
struct Set {
|
||||
SharedTextureHandle_t handles[3] = {};
|
||||
int fds[3] = {-1, -1, -1};
|
||||
@@ -187,7 +222,7 @@ public:
|
||||
out->unTextureFlags = 0;
|
||||
Log(" -> handles 0x%llx 0x%llx 0x%llx, fds %d %d %d", (unsigned long long)set.handles[0],
|
||||
(unsigned long long)set.handles[1], (unsigned long long)set.handles[2], set.fds[0], set.fds[1], set.fds[2]);
|
||||
droiddeck::Presenter::AddTextures(set.handles, set.fds, d->nFormat, d->nWidth, d->nHeight);
|
||||
droiddeck::Presenter::AddTextures(set.handles, set.fds, d->nFormat, d->nWidth, d->nHeight, pid);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
sets_.push_back(set);
|
||||
}
|
||||
@@ -223,10 +258,19 @@ public:
|
||||
void SubmitLayer(const SubmitLayerPerEye_t (&eyes)[2]) override {
|
||||
layers_++;
|
||||
if (layerInFrame_++ == 0) {
|
||||
frame_.render = {};
|
||||
if (xrWidth_) {
|
||||
frame_.render.magic = DROIDDECK_XR_FRAME_MAGIC; frame_.render.version = DROIDDECK_XR_FRAME_VERSION;
|
||||
frame_.render.width = xrWidth_; frame_.render.height = xrHeight_;
|
||||
frame_.render.bytes = xrWidth_ * xrHeight_ * 4;
|
||||
frame_.render.color_space = DROIDDECK_XR_COLOR_LINEAR_RGBA8;
|
||||
}
|
||||
for (int e = 0; e < 2; e++) {
|
||||
frame_.texture[e] = eyes[e].hTexture;
|
||||
frame_.bounds[e][0] = eyes[e].bounds.uMin; frame_.bounds[e][1] = eyes[e].bounds.vMin;
|
||||
frame_.bounds[e][2] = eyes[e].bounds.uMax; frame_.bounds[e][3] = eyes[e].bounds.vMax;
|
||||
std::memcpy(frame_.render.projection[e], eyes[e].mProjection.m, sizeof(eyes[e].mProjection.m));
|
||||
xr_frame_eye_pose(frame_.render.eye_pose[e], eyes[e].mHmdPose.m, eyeToHead_[e].m);
|
||||
}
|
||||
}
|
||||
if (frames_ < 3)
|
||||
@@ -237,6 +281,8 @@ public:
|
||||
|
||||
void Present(SharedTextureHandle_t sync) override {
|
||||
frames_++;
|
||||
frame_.render.frame_id = frames_; frame_.render.captured_ns = XrTrackingFile::Now();
|
||||
frame_.layers = layerInFrame_;
|
||||
if (layerInFrame_) droiddeck::Presenter::Submit(frame_);
|
||||
if (layerInFrame_ != lastLayerCount_) {
|
||||
static int said;
|
||||
@@ -250,8 +296,7 @@ public:
|
||||
}
|
||||
|
||||
void PostPresent(const Throttling_t *t) override {
|
||||
auto last = Vsync::LastTick();
|
||||
auto until = last + Vsync::kInterval * (1 + (t ? t->nFramesToThrottle : 0));
|
||||
auto until = Vsync::Deadline(t ? t->nFramesToThrottle : 0);
|
||||
if (until > std::chrono::steady_clock::now()) std::this_thread::sleep_until(until);
|
||||
}
|
||||
|
||||
@@ -277,6 +322,8 @@ private:
|
||||
uint64_t frames_ = 0, layers_ = 0;
|
||||
int layerInFrame_ = 0, lastLayerCount_ = -1;
|
||||
droiddeck::EyeFrame frame_{};
|
||||
uint32_t xrWidth_ = 0, xrHeight_ = 0;
|
||||
HmdMatrix34_t eyeToHead_[2]{};
|
||||
};
|
||||
|
||||
struct PadState {
|
||||
@@ -342,12 +389,17 @@ public:
|
||||
EVRInitError Activate(uint32_t id) override {
|
||||
id_ = id;
|
||||
PropertyContainerHandle_t c = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, "DroidDeck Pad Hand");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, "DroidDeck");
|
||||
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");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, "generic_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_ControllerType_String, "vive_controller");
|
||||
VRProperties()->SetStringProperty(c, Prop_InputProfilePath_String, "{htc}/input/vive_controller_profile.json");
|
||||
VRProperties()->SetStringProperty(c, Prop_RenderModelName_String, xrMode_ ?
|
||||
(right_ ? "oculus_quest_plus_controller_right" : "oculus_quest_plus_controller_left") : "generic_controller");
|
||||
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()->SetInt32Property(c, Prop_ControllerRoleHint_Int32,
|
||||
right_ ? TrackedControllerRole_RightHand : TrackedControllerRole_LeftHand);
|
||||
VRProperties()->SetInt32Property(c, Prop_DeviceClass_Int32, TrackedDeviceClass_Controller);
|
||||
@@ -357,12 +409,21 @@ public:
|
||||
in->CreateBooleanComponent(c, "/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, "/input/trackpad/x", &padX_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateScalarComponent(c, "/input/trackpad/y", &padY_, VRScalarType_Absolute, VRScalarUnits_NormalizedTwoSided);
|
||||
in->CreateBooleanComponent(c, "/input/trackpad/click", &padClick_);
|
||||
in->CreateBooleanComponent(c, "/input/trackpad/touch", &padTouch_);
|
||||
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_) {
|
||||
in->CreateScalarComponent(c, "/input/grip/value", &gripValue_, 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->CreateHapticComponent(c, "/output/haptic", &haptic_);
|
||||
Log("%s hand activated (game pad)", right_ ? "right" : "left");
|
||||
Log("%s hand activated (%s)", right_ ? "right" : "left", xrMode_ ? "Quest Touch" : "game pad");
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Deactivate() override { id_ = k_unTrackedDeviceIndexInvalid; }
|
||||
@@ -371,6 +432,44 @@ public:
|
||||
void DebugRequest(const char *, char *resp, uint32_t size) override { if (size) resp[0] = 0; }
|
||||
|
||||
DriverPose_t GetPose() override { return pose_; }
|
||||
VRInputComponentHandle_t HapticHandle() const { return haptic_; }
|
||||
|
||||
void UpdateXr(const DroidDeckXrHand &hand, int64_t predicted) {
|
||||
if (id_ == k_unTrackedDeviceIndexInvalid) return;
|
||||
DriverPose_t pose{};
|
||||
pose.qWorldFromDriverRotation.w = pose.qDriverFromHeadRotation.w = 1;
|
||||
pose.qRotation.w = 1;
|
||||
pose.deviceIsConnected = hand.active;
|
||||
pose.poseIsValid = hand.active && XrTrackingFile::Tracked(hand.pose);
|
||||
if (pose.poseIsValid) {
|
||||
pose.qRotation = {hand.pose.orientation[3], hand.pose.orientation[0], hand.pose.orientation[1], hand.pose.orientation[2]};
|
||||
for (int axis = 0; axis < 3; axis++) pose.vecPosition[axis] = hand.pose.position[axis];
|
||||
pose.poseTimeOffset = XrTrackingFile::PoseTimeOffset(predicted, XrTrackingFile::Now());
|
||||
}
|
||||
pose.result = pose.poseIsValid ? TrackingResult_Running_OK : TrackingResult_Running_OutOfRange;
|
||||
pose_ = pose;
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, pose, sizeof(pose));
|
||||
XrTouchInput input = xr_touch_input(hand, right_);
|
||||
auto driverInput = VRDriverInput();
|
||||
driverInput->UpdateScalarComponent(trigger_, input.trigger, 0);
|
||||
driverInput->UpdateBooleanComponent(triggerClick_, input.triggerClick, 0);
|
||||
driverInput->UpdateBooleanComponent(grip_, input.gripClick, 0);
|
||||
driverInput->UpdateScalarComponent(padX_, input.stickX, 0);
|
||||
driverInput->UpdateScalarComponent(padY_, input.stickY, 0);
|
||||
driverInput->UpdateBooleanComponent(padClick_, input.stickClick, 0);
|
||||
driverInput->UpdateBooleanComponent(padTouch_, input.stickTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(menu_, false, 0);
|
||||
driverInput->UpdateBooleanComponent(system_, input.systemClick, 0);
|
||||
if (xrMode_) {
|
||||
driverInput->UpdateScalarComponent(gripValue_, input.grip, 0);
|
||||
driverInput->UpdateBooleanComponent(triggerTouch_, input.triggerTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(primaryClick_, input.primaryClick, 0);
|
||||
driverInput->UpdateBooleanComponent(secondaryClick_, input.secondaryClick, 0);
|
||||
driverInput->UpdateBooleanComponent(primaryTouch_, input.primaryTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(secondaryTouch_, input.secondaryTouch, 0);
|
||||
driverInput->UpdateBooleanComponent(thumbrestTouch_, input.thumbrestTouch, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void Update(const HmdQuaternion_t &head, const PadState *pad) {
|
||||
if (id_ == k_unTrackedDeviceIndexInvalid) return;
|
||||
@@ -409,10 +508,13 @@ public:
|
||||
|
||||
private:
|
||||
bool right_;
|
||||
bool xrMode_ = false;
|
||||
uint32_t id_ = k_unTrackedDeviceIndexInvalid;
|
||||
DriverPose_t pose_ = {};
|
||||
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,
|
||||
primaryTouch_ = 0, secondaryTouch_ = 0, thumbrestTouch_ = 0;
|
||||
};
|
||||
|
||||
class CardboardHmd : public ITrackedDeviceServerDriver, public IVRDisplayComponent {
|
||||
@@ -426,6 +528,12 @@ public:
|
||||
if (height_ <= 0) height_ = 1080;
|
||||
float hz = VRSettings()->GetFloat(kSettingsSection, "displayFrequency");
|
||||
if (hz <= 0.0f) hz = 60.0f;
|
||||
DroidDeckXrTracking xr;
|
||||
xrMode_ = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
|
||||
if (xrMode_) {
|
||||
width_ = xr.eyes[0].width * 2; height_ = xr.eyes[0].height;
|
||||
hz = 1000000000.f / xr.period_ns;
|
||||
}
|
||||
tanHalf_ = 1.0f;
|
||||
eyeAspect_ = (width_ * 0.5f) / height_;
|
||||
droiddeck_headpose hp;
|
||||
@@ -433,17 +541,31 @@ public:
|
||||
tanHalf_ = hp.tan_half_fov;
|
||||
if (hp.eye_aspect > 0.0f) eyeAspect_ = hp.eye_aspect;
|
||||
}
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, "DroidDeck Cardboard");
|
||||
VRProperties()->SetStringProperty(c, Prop_ModelNumber_String, xrMode_ ? "DroidDeck Quest OpenXR" : "DroidDeck Cardboard");
|
||||
VRProperties()->SetStringProperty(c, Prop_ManufacturerName_String, "DroidDeck");
|
||||
VRProperties()->SetStringProperty(c, Prop_TrackingSystemName_String, "droiddeck");
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserIpdMeters_Float, 0.064f);
|
||||
if (xrMode_) {
|
||||
VRServerDriverHost()->SetDisplayEyeToHead(id_, XrEyeToHead(xr.head, xr.eyes[0].pose), XrEyeToHead(xr.head, xr.eyes[1].pose));
|
||||
dm_.ConfigureXr(xr);
|
||||
float distanceSquared = 0;
|
||||
for (int axis = 0; axis < 3; axis++) {
|
||||
float difference = xr.eyes[0].pose.position[axis] - xr.eyes[1].pose.position[axis];
|
||||
distanceSquared += difference * difference;
|
||||
}
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserIpdMeters_Float, std::sqrt(distanceSquared));
|
||||
}
|
||||
VRProperties()->SetFloatProperty(c, Prop_UserHeadToEyeDepthMeters_Float, 0.0f);
|
||||
VRProperties()->SetFloatProperty(c, Prop_DisplayFrequency_Float, hz);
|
||||
VRProperties()->SetFloatProperty(c, Prop_SecondsFromVsyncToPhotons_Float, 0.011f);
|
||||
VRProperties()->SetUint64Property(c, Prop_CurrentUniverseId_Uint64, 2);
|
||||
chaperone_.Reset();
|
||||
if (xrMode_) PublishChaperone(xr);
|
||||
VRProperties()->SetBoolProperty(c, Prop_IsOnDesktop_Bool, false);
|
||||
VRProperties()->SetBoolProperty(c, Prop_DisplayDebugMode_Bool, false);
|
||||
directMode_ = VRSettings()->GetBool(kSettingsSection, "directMode");
|
||||
const char *presentation = std::getenv("DROIDDECK_VR_PRESENT");
|
||||
if (xrMode_ && presentation && !std::strcmp(presentation, "questwsi")) directMode_ = false;
|
||||
VRProperties()->SetBoolProperty(c, Prop_HasDriverDirectModeComponent_Bool, directMode_);
|
||||
if (directMode_) {
|
||||
VRProperties()->SetBoolProperty(c, Prop_DriverDirectModeSendsVsyncEvents_Bool, true);
|
||||
@@ -488,6 +610,19 @@ public:
|
||||
p.qWorldFromDriverRotation.w = 1;
|
||||
p.qDriverFromHeadRotation.w = 1;
|
||||
p.qRotation.w = 1;
|
||||
DroidDeckXrTracking xr;
|
||||
bool freshXr = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
|
||||
if (freshXr || xrMode_) {
|
||||
p.deviceIsConnected = true;
|
||||
p.poseIsValid = freshXr && XrTrackingFile::Tracked(xr.head);
|
||||
p.result = p.poseIsValid ? TrackingResult_Running_OK : TrackingResult_Running_OutOfRange;
|
||||
if (p.poseIsValid) {
|
||||
p.qRotation = {xr.head.orientation[3], xr.head.orientation[0], xr.head.orientation[1], xr.head.orientation[2]};
|
||||
for (int axis = 0; axis < 3; axis++) p.vecPosition[axis] = xr.head.position[axis];
|
||||
p.poseTimeOffset = XrTrackingFile::PoseTimeOffset(xr.predicted_ns, XrTrackingFile::Now());
|
||||
}
|
||||
return p;
|
||||
}
|
||||
p.vecPosition[1] = 1.6;
|
||||
droiddeck_headpose hp;
|
||||
bool ok = pose_.Read(&hp);
|
||||
@@ -509,6 +644,7 @@ public:
|
||||
bool IsDisplayOnDesktop() override { return !directMode_; }
|
||||
bool IsDisplayRealDisplay() override { return false; }
|
||||
void GetRecommendedRenderTargetSize(uint32_t *w, uint32_t *h) override {
|
||||
if (xrMode_) { *w = width_ / 2; *h = height_; return; }
|
||||
*w = (uint32_t)(width_ / 2 * 1.4f);
|
||||
*h = (uint32_t)(height_ * 1.4f);
|
||||
}
|
||||
@@ -516,7 +652,13 @@ public:
|
||||
*y = 0; *w = (uint32_t)width_ / 2; *h = (uint32_t)height_;
|
||||
*x = eye == Eye_Left ? 0 : (uint32_t)width_ / 2;
|
||||
}
|
||||
void GetProjectionRaw(EVREye, float *left, float *right, float *top, float *bottom) override {
|
||||
void GetProjectionRaw(EVREye eye, float *left, float *right, float *top, float *bottom) override {
|
||||
DroidDeckXrTracking xr;
|
||||
if (xrMode_ && xr_.Read(&xr) && XrTrackingFile::Fresh(xr)) {
|
||||
const float *fov = xr.eyes[eye == Eye_Right ? 1 : 0].fov;
|
||||
xr_frame_projection_raw(fov, left, right, top, bottom);
|
||||
return;
|
||||
}
|
||||
*left = -tanHalf_ * eyeAspect_; *right = tanHalf_ * eyeAspect_;
|
||||
*top = -tanHalf_; *bottom = tanHalf_;
|
||||
}
|
||||
@@ -532,10 +674,34 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
void PublishChaperone(const DroidDeckXrTracking &tracking) {
|
||||
chaperone_.Update(tracking, [this](const DroidDeckXrPose &head) {
|
||||
char timestamp[32];
|
||||
std::time_t now = std::time(nullptr); std::tm utc{}; gmtime_r(&now, &utc);
|
||||
std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc);
|
||||
const auto json = xr_chaperone_json(head, timestamp);
|
||||
const auto container = VRProperties()->TrackedDeviceToPropertyContainer(id_);
|
||||
return VRProperties()->SetStringProperty(container, Prop_DriverProvidedChaperoneJson_String, json.c_str()) == TrackedProp_Success;
|
||||
});
|
||||
}
|
||||
void PoseLoop() {
|
||||
uint32_t lastSeq = 0;
|
||||
auto lastSent = std::chrono::steady_clock::now();
|
||||
while (running_) {
|
||||
DroidDeckXrTracking xr;
|
||||
if (xrMode_) {
|
||||
bool fresh = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
|
||||
if (fresh) PublishChaperone(xr);
|
||||
DriverPose_t head = GetPose();
|
||||
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(head));
|
||||
for (int hand = 0; hand < 2; hand++) if (hands_[hand]) {
|
||||
DroidDeckXrHand state = fresh ? xr.hands[hand] : DroidDeckXrHand{};
|
||||
hands_[hand]->UpdateXr(state, fresh ? xr.predicted_ns : 0);
|
||||
}
|
||||
VRDriverInput()->UpdateBooleanComponent(proximity_, fresh, 0);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2));
|
||||
continue;
|
||||
}
|
||||
droiddeck_headpose hp;
|
||||
bool fresh = pose_.Read(&hp) && hp.seq != lastSeq;
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
@@ -570,6 +736,9 @@ public:
|
||||
|
||||
private:
|
||||
PoseFile pose_;
|
||||
XrTrackingFile xr_;
|
||||
XrChaperoneOrigin chaperone_;
|
||||
bool xrMode_ = false;
|
||||
PadRing pad_;
|
||||
PadState lastPad_;
|
||||
VRInputComponentHandle_t proximity_ = 0;
|
||||
@@ -586,6 +755,7 @@ class Provider : public IServerTrackedDeviceProvider {
|
||||
public:
|
||||
EVRInitError Init(IVRDriverContext *ctx) override {
|
||||
VR_INIT_SERVER_DRIVER_CONTEXT(ctx);
|
||||
focusEnabled_ = access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0;
|
||||
Log("DroidDeck Cardboard driver loaded");
|
||||
if (!VRServerDriverHost()->TrackedDeviceAdded("droiddeck-cardboard-hmd", TrackedDeviceClass_HMD, &hmd_))
|
||||
Log("TrackedDeviceAdded failed");
|
||||
@@ -596,17 +766,72 @@ public:
|
||||
}
|
||||
return VRInitError_None;
|
||||
}
|
||||
void Cleanup() override { VR_CLEANUP_SERVER_DRIVER_CONTEXT(); }
|
||||
void Cleanup() override {
|
||||
if (focusEnabled_) unlink(kFocusPath);
|
||||
if (haptics_) { munmap(haptics_, sizeof(*haptics_)); haptics_ = nullptr; }
|
||||
VR_CLEANUP_SERVER_DRIVER_CONTEXT();
|
||||
}
|
||||
const char *const *GetInterfaceVersions() override { return k_InterfaceVersions; }
|
||||
void RunFrame() override {
|
||||
VREvent_t e;
|
||||
while (VRServerDriverHost()->PollNextEvent(&e, sizeof(e))) {}
|
||||
while (VRServerDriverHost()->PollNextEvent(&e, sizeof(e))) {
|
||||
if (focusEnabled_) {
|
||||
bool changed = false;
|
||||
if (e.eventType == VREvent_SceneApplicationChanged) { scenePid_ = e.data.process.pid; changed = true; }
|
||||
if (e.eventType == VREvent_DashboardActivated) { dashboard_ = true; changed = true; }
|
||||
if (e.eventType == VREvent_DashboardDeactivated) { dashboard_ = false; changed = true; }
|
||||
if (changed) PublishFocus();
|
||||
}
|
||||
if (e.eventType == VREvent_Input_HapticVibration) {
|
||||
const auto &pulse = e.data.hapticVibration;
|
||||
int hand = pulse.componentHandle == left_.HapticHandle() ? 0 : pulse.componentHandle == right_.HapticHandle() ? 1 : -1;
|
||||
if (hand >= 0) SendHaptic(hand, pulse);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool ShouldBlockStandbyMode() override { return false; }
|
||||
void EnterStandby() override {}
|
||||
void LeaveStandby() override {}
|
||||
|
||||
private:
|
||||
static constexpr const char *kFocusPath = "/run/droiddeck/droiddeck-steamvr/scene-focus.json";
|
||||
bool focusEnabled_ = false, dashboard_ = true;
|
||||
uint32_t scenePid_ = 0;
|
||||
void PublishFocus() {
|
||||
const std::string temporary = std::string(kFocusPath) + ".tmp";
|
||||
int fd = open(temporary.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600);
|
||||
if (fd < 0) return;
|
||||
char body[128];
|
||||
int length = snprintf(body, sizeof(body), "{\"serverPid\":%d,\"scenePid\":%u,\"dashboard\":%s}\n",
|
||||
getpid(), scenePid_, dashboard_ ? "true" : "false");
|
||||
bool written = write(fd, body, length) == length;
|
||||
close(fd);
|
||||
if (written) rename(temporary.c_str(), kFocusPath);
|
||||
else unlink(temporary.c_str());
|
||||
}
|
||||
DroidDeckXrHaptics *haptics_ = nullptr;
|
||||
void SendHaptic(int hand, const VREvent_HapticVibration_t &pulse) {
|
||||
if (!std::isfinite(pulse.fAmplitude) || !std::isfinite(pulse.fFrequency) || !std::isfinite(pulse.fDurationSeconds)) return;
|
||||
if (!haptics_) {
|
||||
int descriptor = open("/run/droiddeck/" DROIDDECK_XR_HAPTIC_FILE, O_RDWR | O_CLOEXEC);
|
||||
if (descriptor < 0) return;
|
||||
struct stat status{};
|
||||
if (fstat(descriptor, &status) != 0 || status.st_size != sizeof(*haptics_)) { close(descriptor); return; }
|
||||
void *mapping = mmap(nullptr, sizeof(*haptics_), PROT_READ | PROT_WRITE, MAP_SHARED, descriptor, 0);
|
||||
close(descriptor);
|
||||
if (mapping == MAP_FAILED) return;
|
||||
haptics_ = static_cast<DroidDeckXrHaptics *>(mapping);
|
||||
}
|
||||
uint32_t sequence = __atomic_load_n(&haptics_->seq, __ATOMIC_RELAXED);
|
||||
__atomic_store_n(&haptics_->seq, sequence | 1u, __ATOMIC_SEQ_CST);
|
||||
__atomic_thread_fence(__ATOMIC_SEQ_CST);
|
||||
auto &target = haptics_->hands[hand];
|
||||
target.id++;
|
||||
target.duration_ns = (int64_t)(std::clamp(pulse.fDurationSeconds, 0.001f, 1.f) * 1000000000.0);
|
||||
target.amplitude = std::clamp(pulse.fAmplitude, 0.f, 1.f);
|
||||
target.frequency = std::max(pulse.fFrequency, 0.f);
|
||||
__atomic_store_n(&haptics_->seq, (sequence | 1u) + 1u, __ATOMIC_RELEASE);
|
||||
}
|
||||
CardboardHmd hmd_;
|
||||
CardboardController left_{false}, right_{true};
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user