Skip to content

Commit

Permalink
refactoring: around button register and openvr prop
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinaS-64892 committed Jul 27, 2024
1 parent f8361b3 commit 8704c99
Show file tree
Hide file tree
Showing 8 changed files with 591 additions and 447 deletions.
30 changes: 26 additions & 4 deletions alvr/server_openvr/cpp/alvr_server/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ vr::ETrackedDeviceClass Controller::getControllerDeviceClass() {
return vr::TrackedDeviceClass_Controller;
}

Controller::Controller(uint64_t deviceID)
Controller::Controller(uint64_t deviceID, DEVICE_DESCRIPTION_TYPE deviceType)
: TrackedDevice(deviceID) {
m_pose = vr::DriverPose_t {};
m_pose.poseIsValid = false;
Expand All @@ -24,6 +24,8 @@ Controller::Controller(uint64_t deviceID)
m_pose.qDriverFromHeadRotation = HmdQuaternion_Init(1, 0, 0, 0);
m_pose.qWorldFromDriverRotation = HmdQuaternion_Init(1, 0, 0, 0);
m_pose.qRotation = HmdQuaternion_Init(1, 0, 0, 0);

m_deviceType = deviceType;
}

//
Expand All @@ -39,9 +41,9 @@ vr::EVRInitError Controller::Activate(vr::TrackedDeviceIndex_t unObjectId) {
this->object_id = unObjectId;
this->prop_container = vr_properties->TrackedDeviceToPropertyContainer(this->object_id);

SetOpenvrProps(this->device_id);
SetOpenVRProps();

RegisterButtons(this->device_id);
RegisterButtonAll();

vr_driver_input->CreateHapticComponent(this->prop_container, "/output/haptic", &m_compHaptic);

Expand Down Expand Up @@ -80,6 +82,7 @@ vr::EVRInitError Controller::Activate(vr::TrackedDeviceIndex_t unObjectId) {
"/input/skeleton/left",
"/skeleton/hand/left",
"/pose/raw",
// m_fullSkeletal ? vr::EVRSkeletalTrackingLevel::VRSkeletalTracking_Full :
vr::EVRSkeletalTrackingLevel::VRSkeletalTracking_Partial,
nullptr,
0U,
Expand All @@ -91,6 +94,7 @@ vr::EVRInitError Controller::Activate(vr::TrackedDeviceIndex_t unObjectId) {
"/input/skeleton/right",
"/skeleton/hand/right",
"/pose/raw",
// m_fullSkeletal ? vr::EVRSkeletalTrackingLevel::VRSkeletalTracking_Full :
vr::EVRSkeletalTrackingLevel::VRSkeletalTracking_Partial,
nullptr,
0U,
Expand Down Expand Up @@ -120,6 +124,24 @@ vr::EVRInitError Controller::Activate(vr::TrackedDeviceIndex_t unObjectId) {
return vr::VRInitError_None;
}

void Controller::RegisterButtonAll() {
int buttonLen = 0;
auto buttons = GetRegisterButtons(m_deviceType, &buttonLen);
for (int i = 0; buttonLen > i; i++) {
RegisterButton(buttons[i]);
}
FreeRegisterButtonArray(buttons, buttonLen);
}

void Controller::SetOpenVRProps() {
int propLen = 0;
auto props = GetOpenVrProps(m_deviceType, &propLen);
for (int i = 0; propLen > i; i++) {
set_prop(props[i]);
}
FreePropArray(props, propLen);
}

void Controller::Deactivate() {
Debug("RemoteController::Deactivate\n");
this->object_id = vr::k_unTrackedDeviceIndexInvalid;
Expand Down Expand Up @@ -149,7 +171,7 @@ vr::VRInputComponentHandle_t Controller::getHapticComponent() { return m_compHap

void Controller::RegisterButton(uint64_t id) {
ButtonInfo buttonInfo;
if (device_id == HAND_LEFT_ID) {
if (m_deviceType == DEVICE_DESCRIPTION_TYPE::LEFT_HAND) {
buttonInfo = LEFT_CONTROLLER_BUTTON_MAPPING[id];
} else {
buttonInfo = RIGHT_CONTROLLER_BUTTON_MAPPING[id];
Expand Down
7 changes: 6 additions & 1 deletion alvr/server_openvr/cpp/alvr_server/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class Controller : public TrackedDevice, public vr::ITrackedDeviceServerDriver {
public:
Controller(uint64_t deviceID);
Controller(uint64_t deviceID, DEVICE_DESCRIPTION_TYPE deviceType);

virtual ~Controller() {};

Expand Down Expand Up @@ -49,6 +49,9 @@ class Controller : public TrackedDevice, public vr::ITrackedDeviceServerDriver {
vr::ETrackedDeviceClass getControllerDeviceClass();

private:
void SetOpenVRProps();
void RegisterButtonAll();

static const int SKELETON_BONE_COUNT = 31;
static const int ANIMATION_FRAME_COUNT = 15;

Expand All @@ -59,6 +62,8 @@ class Controller : public TrackedDevice, public vr::ITrackedDeviceServerDriver {

vr::DriverPose_t m_pose;

DEVICE_DESCRIPTION_TYPE m_deviceType;

// These variables are used for controller hand animation
// todo: move to rust
float m_thumbTouchAnimationProgress = 0;
Expand Down
11 changes: 10 additions & 1 deletion alvr/server_openvr/cpp/alvr_server/HMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ vr::EVRInitError Hmd::Activate(vr::TrackedDeviceIndex_t unObjectId) {
this->object_id = unObjectId;
this->prop_container = vr_properties->TrackedDeviceToPropertyContainer(this->object_id);

SetOpenvrProps(this->device_id);
SetOpenVRProps();

vr_properties->SetFloatProperty(
this->prop_container,
Expand Down Expand Up @@ -181,6 +181,15 @@ vr::EVRInitError Hmd::Activate(vr::TrackedDeviceIndex_t unObjectId) {
return vr::VRInitError_None;
}

void Hmd::SetOpenVRProps() {
int propLen = 0;
auto props = GetOpenVrProps(DEVICE_DESCRIPTION_TYPE::HMD, &propLen);
for (int i = 0; propLen > i; i++) {
set_prop(props[i]);
}
FreePropArray(props, propLen);
}

void Hmd::Deactivate() {
this->object_id = vr::k_unTrackedDeviceIndexInvalid;
this->prop_container = vr::k_ulInvalidPropertyContainer;
Expand Down
2 changes: 2 additions & 0 deletions alvr/server_openvr/cpp/alvr_server/HMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Hmd : public TrackedDevice, public vr::ITrackedDeviceServerDriver, vr::IVR
std::shared_ptr<PoseHistory> m_poseHistory;

private:
void SetOpenVRProps();

FfiViewsConfig views_config;

bool m_baseComponentsInitialized;
Expand Down
14 changes: 10 additions & 4 deletions alvr/server_openvr/cpp/alvr_server/alvr_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ class DriverProvider : public vr::IServerTrackedDeviceProvider {
}

if (Settings::Instance().m_enableControllers) {
this->left_controller = std::make_unique<Controller>(HAND_LEFT_ID);
this->right_controller = std::make_unique<Controller>(HAND_RIGHT_ID);
this->left_controller
= std::make_unique<Controller>(HAND_LEFT_ID, DEVICE_DESCRIPTION_TYPE::LEFT_HAND);
this->right_controller
= std::make_unique<Controller>(HAND_RIGHT_ID, DEVICE_DESCRIPTION_TYPE::RIGHT_HAND);

this->tracked_devices.insert({ HAND_LEFT_ID,
(TrackedDevice*)this->left_controller.get() });
Expand Down Expand Up @@ -310,10 +312,14 @@ void (*ReportPresent)(unsigned long long timestamp_ns, unsigned long long offset
void (*ReportComposed)(unsigned long long timestamp_ns, unsigned long long offset_ns);
FfiDynamicEncoderParams (*GetDynamicEncoderParams)();
unsigned long long (*GetSerialNumber)(unsigned long long deviceID, char* outString);
void (*SetOpenvrProps)(unsigned long long deviceID);
void (*RegisterButtons)(unsigned long long deviceID);
void (*WaitForVSync)();

FfiOpenvrProperty* (*GetOpenVrProps)(DEVICE_DESCRIPTION_TYPE deviceType, int* count);
void (*FreePropArray)(FfiOpenvrProperty* ptr, int count);

uint64_t* (*GetRegisterButtons)(DEVICE_DESCRIPTION_TYPE deviceType, int* count);
void (*FreeRegisterButtonArray)(uint64_t* ptr, int count);

void CppInit() {
HookCrashHandler();

Expand Down
15 changes: 13 additions & 2 deletions alvr/server_openvr/cpp/alvr_server/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ struct FfiDynamicEncoderParams {
float framerate;
};

enum DEVICE_DESCRIPTION_TYPE {
HMD,
LEFT_HAND,
RIGHT_HAND,
OTHER,
};

extern "C" const unsigned char* FRAME_RENDER_VS_CSO_PTR;
extern "C" unsigned int FRAME_RENDER_VS_CSO_LEN;
extern "C" const unsigned char* FRAME_RENDER_PS_CSO_PTR;
Expand Down Expand Up @@ -128,10 +135,14 @@ extern "C" void (*ReportPresent)(unsigned long long timestamp_ns, unsigned long
extern "C" void (*ReportComposed)(unsigned long long timestamp_ns, unsigned long long offset_ns);
extern "C" FfiDynamicEncoderParams (*GetDynamicEncoderParams)();
extern "C" unsigned long long (*GetSerialNumber)(unsigned long long deviceID, char* outString);
extern "C" void (*SetOpenvrProps)(unsigned long long deviceID);
extern "C" void (*RegisterButtons)(unsigned long long deviceID);
extern "C" void (*WaitForVSync)();

extern "C" void (*FreePropArray)(FfiOpenvrProperty* ptr, int count);
extern "C" FfiOpenvrProperty* (*GetOpenVrProps)(DEVICE_DESCRIPTION_TYPE deviceType, int* count);

extern "C" unsigned long long* (*GetRegisterButtons)(DEVICE_DESCRIPTION_TYPE deviceType, int* count);
extern "C" void (*FreeRegisterButtonArray)(unsigned long long* ptr, int count);

extern "C" void CppInit();
extern "C" void* CppOpenvrEntryPoint(const char* pInterfaceName, int* pReturnCode);
extern "C" void InitializeStreaming();
Expand Down
55 changes: 52 additions & 3 deletions alvr/server_openvr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use alvr_server_core::{ServerCoreContext, ServerCoreEvent, REGISTERED_BUTTON_SET
use alvr_session::CodecType;
use std::{
ffi::{c_char, c_void, CString},
ptr,
mem, ptr, thread,
sync::{mpsc, Once},
thread,
time::{Duration, Instant},
Expand Down Expand Up @@ -253,6 +253,53 @@ pub extern "C" fn register_buttons(device_id: u64) {
}
}

pub unsafe extern "C" fn get_register_buttons(
device_type: DEVICE_DESCRIPTION_TYPE,
button_id_len: *mut std::os::raw::c_int,
) -> *mut u64 {
let device_id = if device_type == DEVICE_DESCRIPTION_TYPE_LEFT_HAND {
*HAND_LEFT_ID
} else {
*HAND_RIGHT_ID
};
let mut button_id_vec: Vec<u64> = REGISTERED_BUTTON_SET
.iter()
.filter_map(|id| {
if let Some(info) = BUTTON_INFO.get(id) {
if info.device_id == device_id {
Some(*id)
} else {
None
}
} else {
error!("Cannot register unrecognized button ID {id}");
None
}
})
.collect();

button_id_vec.shrink_to_fit();
assert!(button_id_vec.len() == button_id_vec.capacity());

let len = button_id_vec.len();
let ptr = button_id_vec.as_mut_ptr();

mem::forget(button_id_vec);
ptr::write(button_id_len, len as std::os::raw::c_int);

ptr
}

#[no_mangle]
pub unsafe extern "C" fn free_register_button_array(
button_id_array: *mut u64,
id_len: std::os::raw::c_int,
) {
let len = id_len as usize;
let vec = Vec::from_raw_parts(button_id_array, len, len);
mem::drop(vec);
}

extern "C" fn send_haptics(device_id: u64, duration_s: f32, frequency: f32, amplitude: f32) {
if let Some(context) = &*SERVER_CORE_CONTEXT.read() {
let haptics = Haptics {
Expand Down Expand Up @@ -383,8 +430,10 @@ pub unsafe extern "C" fn HmdDriverFactory(
LogPeriodically = Some(alvr_server_core::alvr_log_periodically);
PathStringToHash = Some(alvr_server_core::alvr_path_to_id);
GetSerialNumber = Some(props::get_serial_number);
SetOpenvrProps = Some(props::set_device_openvr_props);
RegisterButtons = Some(register_buttons);
GetOpenVrProps = Some(props::get_device_openvr_props);
FreePropArray = Some(props::free_prop_array);
GetRegisterButtons = Some(get_register_buttons);
FreeRegisterButtonArray = Some(free_register_button_array);
DriverReadyIdle = Some(driver_ready_idle);
HapticsSend = Some(send_haptics);
SetVideoConfigNals = Some(set_video_config_nals);
Expand Down
Loading

0 comments on commit 8704c99

Please sign in to comment.