Skip to content

Commit 97615b8

Browse files
committed
Added activate debug mode and direct read freetrack
1 parent c914768 commit 97615b8

File tree

5 files changed

+158
-105
lines changed

5 files changed

+158
-105
lines changed

OpenVR/FreeTrack/samples/driver_sample/driver_sample.cpp

Lines changed: 121 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include <thread>
99
#include <chrono>
1010

11-
#include <atlbase.h>
12-
//#include <Windows.h>
11+
#include <Windows.h>
1312
//#include "Shlwapi.h"
1413

1514
using namespace vr;
@@ -68,41 +67,93 @@ static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
6867
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
6968
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
7069
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";
70+
static const char * const k_pch_Sample_DebugMode_Bool = "DebugMode";
71+
72+
#define FREETRACK_HEAP "FT_SharedMem"
73+
#define FREETRACK_MUTEX "FT_Mutext"
74+
75+
/* only 6 headpose floats and the data id are filled -sh */
76+
typedef struct FTData__ {
77+
uint32_t DataID;
78+
int32_t CamWidth;
79+
int32_t CamHeight;
80+
/* virtual pose */
81+
float Yaw; /* positive yaw to the left */
82+
float Pitch; /* positive pitch up */
83+
float Roll; /* positive roll to the left */
84+
float X;
85+
float Y;
86+
float Z;
87+
/* raw pose with no smoothing, sensitivity, response curve etc. */
88+
float RawYaw;
89+
float RawPitch;
90+
float RawRoll;
91+
float RawX;
92+
float RawY;
93+
float RawZ;
94+
/* raw points, sorted by Y, origin top left corner */
95+
float X1;
96+
float Y1;
97+
float X2;
98+
float Y2;
99+
float X3;
100+
float Y3;
101+
float X4;
102+
float Y4;
103+
} volatile FTData;
104+
105+
typedef struct FTHeap__ {
106+
FTData data;
107+
int32_t GameID;
108+
union
109+
{
110+
unsigned char table[8];
111+
int32_t table_ints[2];
112+
};
113+
int32_t GameID2;
114+
} volatile FTHeap;
71115

72-
typedef struct _FreeTrack
73-
{
74-
unsigned long int dataID;
75-
long int camWidth;
76-
long int camHeight;
77-
78-
float yaw;
79-
float pitch;
80-
float roll;
81-
float x;
82-
float y;
83-
float z;
84-
85-
float rawyaw;
86-
float rawpitch;
87-
float rawroll;
88-
float rawx;
89-
float rawy;
90-
float rawz;
91-
92-
float x1;
93-
float y1;
94-
float x2;
95-
float y2;
96-
float x3;
97-
float y3;
98-
float x4;
99-
float y4;
100-
} TFreeTrack, *PFreeTrack;
101-
typedef bool(__stdcall *_GetData)(__out TFreeTrack *FreeTrack);
102-
_GetData GetData;
103-
HMODULE FreeTrackLib;
104-
TFreeTrack FreeTrack;
116+
static HANDLE hFTMemMap = 0;
117+
static FTHeap *ipc_heap = 0;
118+
static HANDLE ipc_mutex = 0;
119+
120+
FTData *FreeTrack;
105121
bool HMDConnected = false;
122+
std::thread *pFTthread = NULL;
123+
124+
//FreeTrack implementation from https://github.com/opentrack/opentrack/tree/unstable/freetrackclient
125+
static BOOL impl_create_mapping(void)
126+
{
127+
if (ipc_heap != NULL)
128+
return TRUE;
129+
130+
hFTMemMap = CreateFileMappingA(INVALID_HANDLE_VALUE,
131+
NULL,
132+
PAGE_READWRITE,
133+
0,
134+
sizeof(FTHeap),
135+
(LPCSTR)FREETRACK_HEAP);
136+
137+
if (hFTMemMap == NULL)
138+
return (ipc_heap = NULL), FALSE;
139+
140+
ipc_heap = (FTHeap*)MapViewOfFile(hFTMemMap, FILE_MAP_WRITE, 0, 0, sizeof(FTHeap));
141+
ipc_mutex = CreateMutexA(NULL, FALSE, FREETRACK_MUTEX);
142+
143+
return TRUE;
144+
}
145+
146+
void FTRead()
147+
{
148+
while (HMDConnected) {
149+
if (ipc_mutex && WaitForSingleObject(ipc_mutex, 16) == WAIT_OBJECT_0) {
150+
memcpy(&FreeTrack, &ipc_heap, sizeof(FreeTrack));
151+
if (ipc_heap->data.DataID > (1 << 29))
152+
ipc_heap->data.DataID = 0;
153+
ReleaseMutex(ipc_mutex);
154+
}
155+
}
156+
}
106157

107158
//-----------------------------------------------------------------------------
108159
// Purpose:
@@ -170,16 +221,21 @@ EVRInitError CWatchdogDriver_Sample::Init( vr::IVRDriverContext *pDriverContext
170221

171222
void CWatchdogDriver_Sample::Cleanup()
172223
{
224+
if (HMDConnected) {
225+
HMDConnected = false;
226+
if (pFTthread) {
227+
pFTthread->join();
228+
delete pFTthread;
229+
pFTthread = nullptr;
230+
}
231+
}
173232
g_bExiting = true;
174233
if ( m_pWatchdogThread )
175234
{
176235
m_pWatchdogThread->join();
177236
delete m_pWatchdogThread;
178237
m_pWatchdogThread = nullptr;
179238
}
180-
181-
if (FreeTrackLib != NULL) FreeLibrary(FreeTrackLib);
182-
FreeTrackLib = nullptr;
183239
}
184240

185241

@@ -217,6 +273,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
217273
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
218274
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
219275
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);
276+
m_bDebugMode = vr::VRSettings()->GetBool(k_pch_Sample_Section, k_pch_Sample_DebugMode_Bool);
220277

221278
//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
222279
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
@@ -226,35 +283,14 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
226283
//DriverLog( "driver_null: Display Frequency: %f\n", m_flDisplayFrequency );
227284
//DriverLog( "driver_null: IPD: %f\n", m_flIPD );
228285

229-
CRegKey key;
230-
TCHAR libPath[MAX_PATH];
231-
232-
LONG status = key.Open(HKEY_CURRENT_USER, _T("Software\\OpenVR-OpenTrack"));
233-
if (status == ERROR_SUCCESS)
234-
{
235-
ULONG libPathSize = sizeof(libPath);
236-
237-
#ifdef _WIN64
238-
status = key.QueryStringValue(_T("FreeTrack64"), libPath, &libPathSize);
239-
#else
240-
status = key.QueryStringValue(_T("FreeTrack"), libPath, &libPathSize);
241-
#endif
242-
243-
244-
if (status == ERROR_SUCCESS)
245-
{
246-
if (PathFileExists(libPath)) {
247-
HMDConnected = true;
248-
FreeTrackLib = LoadLibrary(libPath);
249-
GetData = (_GetData)GetProcAddress(FreeTrackLib, "FTGetData");
250-
251-
if (GetData == NULL) HMDConnected = false;
252-
}
253-
}
254-
}
255-
256-
key.Close();
257286

287+
if (impl_create_mapping() == false) {
288+
HMDConnected = false;
289+
}
290+
else {
291+
HMDConnected = true;
292+
pFTthread = new std::thread(FTRead);
293+
}
258294
}
259295

260296
virtual ~CSampleDeviceDriver()
@@ -281,6 +317,8 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
281317
// avoid "not fullscreen" warnings from vrmonitor
282318
vr::VRProperties()->SetBoolProperty( m_ulPropertyContainer, Prop_IsOnDesktop_Bool, false );
283319

320+
//Debug mode activate Windowed Mode (borderless fullscreen) on "Headset Window" and you can move window to second screen with buttons (Shift + Win + Right or Left), but lock to 30 FPS
321+
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, Prop_DisplayDebugMode_Bool, m_bDebugMode);
284322
// Icons can be configured in code or automatically configured by an external file "drivername\resources\driver.vrresources".
285323
// Icon properties NOT configured in code (post Activate) are then auto-configured by the optional presence of a driver's "drivername\resources\driver.vrresources".
286324
// In this manner a driver can configure their icons in a flexible data driven fashion by using an external file.
@@ -406,7 +444,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
406444
{
407445
DistortionCoordinates_t coordinates;
408446

409-
//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
447+
//Distortion for lens implementation from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
410448
float hX;
411449
float hY;
412450
double rr;
@@ -447,18 +485,16 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
447485
pose.qDriverFromHeadRotation = HmdQuaternion_Init(1, 0, 0, 0);
448486

449487
if (HMDConnected) {
450-
GetData(&FreeTrack);
451-
452488
//Set head tracking rotation
453-
pose.qRotation.w = cos(FreeTrack.yaw * 0.5) * cos(FreeTrack.roll * 0.5) * cos(FreeTrack.pitch * 0.5) + sin(FreeTrack.yaw * 0.5) * sin(FreeTrack.roll * 0.5) * sin(FreeTrack.pitch * 0.5);
454-
pose.qRotation.x = cos(FreeTrack.yaw * 0.5) * sin(FreeTrack.roll * 0.5) * cos(FreeTrack.pitch * 0.5) - sin(FreeTrack.yaw * 0.5) * cos(FreeTrack.roll * 0.5) * sin(FreeTrack.pitch * 0.5);
455-
pose.qRotation.y = cos(FreeTrack.yaw * 0.5) * cos(FreeTrack.roll * 0.5) * sin(FreeTrack.pitch * 0.5) + sin(FreeTrack.yaw * 0.5) * sin(FreeTrack.roll * 0.5) * cos(FreeTrack.pitch * 0.5);
456-
pose.qRotation.z = sin(FreeTrack.yaw * 0.5) * cos(FreeTrack.roll * 0.5) * cos(FreeTrack.pitch * 0.5) - cos(FreeTrack.yaw * 0.5) * sin(FreeTrack.roll * 0.5) * sin(FreeTrack.pitch * 0.5);
489+
pose.qRotation.w = cos(FreeTrack->Yaw * 0.5) * cos(FreeTrack->Roll * 0.5) * cos(FreeTrack->Pitch * 0.5) + sin(FreeTrack->Yaw * 0.5) * sin(FreeTrack->Roll * 0.5) * sin(FreeTrack->Pitch * 0.5);
490+
pose.qRotation.x = cos(FreeTrack->Yaw * 0.5) * sin(FreeTrack->Roll * 0.5) * cos(FreeTrack->Pitch * 0.5) - sin(FreeTrack->Yaw * 0.5) * cos(FreeTrack->Roll * 0.5) * sin(FreeTrack->Pitch * 0.5);
491+
pose.qRotation.y = cos(FreeTrack->Yaw * 0.5) * cos(FreeTrack->Roll * 0.5) * sin(FreeTrack->Pitch * 0.5) + sin(FreeTrack->Yaw * 0.5) * sin(FreeTrack->Roll * 0.5) * cos(FreeTrack->Pitch * 0.5);
492+
pose.qRotation.z = sin(FreeTrack->Yaw * 0.5) * cos(FreeTrack->Roll * 0.5) * cos(FreeTrack->Pitch * 0.5) - cos(FreeTrack->Yaw * 0.5) * sin(FreeTrack->Roll * 0.5) * sin(FreeTrack->Pitch * 0.5);
457493

458494
//Set position tracking
459-
pose.vecPosition[0] = FreeTrack.x; ///?
460-
pose.vecPosition[1] = FreeTrack.y; ///?
461-
pose.vecPosition[2] = FreeTrack.z; ///?
495+
pose.vecPosition[0] = FreeTrack->X; ///?
496+
pose.vecPosition[1] = FreeTrack->Y; ///?
497+
pose.vecPosition[2] = FreeTrack->Z; ///?
462498
}
463499

464500
return pose;
@@ -498,6 +534,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
498534
float m_fDistortionK2;
499535
float m_fZoomWidth;
500536
float m_fZoomHeight;
537+
bool m_bDebugMode;
501538
};
502539

503540
//-----------------------------------------------------------------------------
@@ -541,9 +578,14 @@ EVRInitError CServerDriver_Sample::Init( vr::IVRDriverContext *pDriverContext )
541578

542579
void CServerDriver_Sample::Cleanup()
543580
{
544-
if (FreeTrackLib != NULL) FreeLibrary(FreeTrackLib);
545-
FreeTrackLib = nullptr;
546-
581+
if (HMDConnected) {
582+
HMDConnected = false;
583+
if (pFTthread) {
584+
pFTthread->join();
585+
delete pFTthread;
586+
pFTthread = nullptr;
587+
}
588+
}
547589
delete m_pNullHmdLatest;
548590
m_pNullHmdLatest = NULL;
549591
}

OpenVR/UDP/samples/driver_sample/driver_sample.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static const char * const k_pch_Sample_DistortionK1_Float = "DistortionK1";
6969
static const char * const k_pch_Sample_DistortionK2_Float = "DistortionK2";
7070
static const char * const k_pch_Sample_ZoomWidth_Float = "ZoomWidth";
7171
static const char * const k_pch_Sample_ZoomHeight_Float = "ZoomHeight";
72+
static const char * const k_pch_Sample_DebugMode_Bool = "DebugMode";
7273

7374
//OpenTrack vars
7475
double qW, qX, qY, qZ;
@@ -249,6 +250,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
249250
m_fDistortionK2 = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_DistortionK2_Float);
250251
m_fZoomWidth = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomWidth_Float);
251252
m_fZoomHeight = vr::VRSettings()->GetFloat(k_pch_Sample_Section, k_pch_Sample_ZoomHeight_Float);
253+
m_bDebugMode = vr::VRSettings()->GetBool(k_pch_Sample_Section, k_pch_Sample_DebugMode_Bool);
252254

253255
//DriverLog( "driver_null: Serial Number: %s\n", m_sSerialNumber.c_str() );
254256
//DriverLog( "driver_null: Model Number: %s\n", m_sModelNumber.c_str() );
@@ -326,8 +328,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
326328
vr::VRProperties()->SetBoolProperty( m_ulPropertyContainer, Prop_IsOnDesktop_Bool, false );
327329

328330
//Debug mode activate Windowed Mode (borderless fullscreen) on "Headset Window" and you can move window to second screen with buttons (Shift + Win + Right or Left), but lock to 30 FPS
329-
//vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, Prop_DisplayDebugMode_Bool, true);
330-
331+
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, Prop_DisplayDebugMode_Bool, m_bDebugMode);
331332
// Icons can be configured in code or automatically configured by an external file "drivername\resources\driver.vrresources".
332333
// Icon properties NOT configured in code (post Activate) are then auto-configured by the optional presence of a driver's "drivername\resources\driver.vrresources".
333334
// In this manner a driver can configure their icons in a flexible data driven fashion by using an external file.
@@ -453,7 +454,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
453454
{
454455
DistortionCoordinates_t coordinates;
455456

456-
//distortion for lens from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
457+
//Distortion for lens implementation from https://github.com/HelenXR/openvr_survivor/blob/master/src/head_mount_display_device.cc
457458
float hX;
458459
float hY;
459460
double rr;
@@ -544,6 +545,7 @@ class CSampleDeviceDriver : public vr::ITrackedDeviceServerDriver, public vr::IV
544545
float m_fDistortionK2;
545546
float m_fZoomWidth;
546547
float m_fZoomHeight;
548+
bool m_bDebugMode;
547549
};
548550

549551
//-----------------------------------------------------------------------------

SteamVR Settings/OpenVR/steamvr.vrsettings

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
"DistortionK1" : 0.91,
1717
"DistortionK2" : 0.93,
1818
"ZoomWidth" : 0.8,
19-
"ZoomHeight" : 0.8
19+
"ZoomHeight" : 0.8,
20+
"DebugMode" : <DEBUGMODE>
2021
},
2122
"steamvr" : {
2223
"activateMultipleDrivers" : true,
2324
"directMode" : false,
2425
"enableHomeApp" : false,
2526
"forcedDriver" : "null",
26-
"mirrorViewGeometry" : "0 0 960 540",
2727
"startMonitorFromAppLaunch" : false
2828
}
2929
}

0 commit comments

Comments
 (0)