forked from betavr/steamvr_driver_hydra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_hydra.cpp
1742 lines (1479 loc) · 79.8 KB
/
driver_hydra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//============ Copyright (c) Valve Corporation, All rights reserved. ============
#define WIN32_LEAN_AND_MEAN
#include "driver_hydra.h"
#include "driverlog.h"
#ifdef _WIN32
#include <windows.h>
#include <mmsystem.h> // for timeBeginPeriod()
#pragma comment(lib, "winmm.lib")
#endif
using namespace vr;
#if defined(_WIN32)
#define HMD_DLL_EXPORT extern "C" __declspec( dllexport )
#define HMD_DLL_IMPORT extern "C" __declspec( dllimport )
#elif defined(__GNUC__) || defined(COMPILER_GCC) || defined(__APPLE__)
#define HMD_DLL_EXPORT extern "C" __attribute__((visibility("default")))
#define HMD_DLL_IMPORT extern "C"
#else
#error "Unsupported Platform."
#endif
CServerDriver_Hydra g_serverDriverHydra;
//-----------------------------------------------------------------------------
// Purpose: HmdDriverFactory
//-----------------------------------------------------------------------------
HMD_DLL_EXPORT void *HmdDriverFactory(const char *pInterfaceName, int *pReturnCode)
{
if (0 == strcmp(IServerTrackedDeviceProvider_Version, pInterfaceName))
{
return &g_serverDriverHydra;
}
// watchdog causes problems with steamvr (endless startup loop), so it's disabled for now
/*
if (0 == strcmp(IVRWatchdogProvider_Version, pInterfaceName))
{
return &g_watchdogDriverHydra;
}
*/
if (pReturnCode)
*pReturnCode = VRInitError_Init_InterfaceNotFound;
return NULL;
}
inline HmdQuaternion_t HmdQuaternion_Init(double w, double x, double y, double z)
{
HmdQuaternion_t quat;
quat.w = w;
quat.x = x;
quat.y = y;
quat.z = z;
return quat;
}
// keys for use with the settings API
static const char * const k_pch_Hydra_Section = "hydra";
//static const char * const k_pch_Hydra_RenderModel_String = "rendermodel";
static const char * const k_pch_Hydra_EnableIMU_Bool = "EnableImu";
static const char * const k_pch_Hydra_JoystickDeadzone_Float = "JoyStickDeadZone";
static const char * const k_pch_Hydra_CrouchPressKey_String = "CrouchPressKey";
static const char * const k_pch_Hydra_CustomPressKey_String = "CustomPressKey";
static const char * const k_pch_Hydra_CrouchOffset_Float = "CrouchOffset";
static const char * const k_pch_Hydra_RecognizeAsIndexControllers_Bool = "IndexControllers";
static const char * const k_pch_Hydra_EnableCustomKey_Bool = "EnableCustomKey";
float PosZOffset = 0;
static void GenerateSerialNumber(char *p, int psize, int base, int controller)
{
_snprintf(p, psize, "hydra%d_controller%d", base, controller);
}
int KeyNameToKeyCode(std::string KeyName) {
std::transform(KeyName.begin(), KeyName.end(), KeyName.begin(), ::toupper);
if (KeyName == "NONE") return 0;
else if (KeyName == "MOUSE-LEFT-BTN") return VK_LBUTTON;
else if (KeyName == "MOUSE-RIGHT-BTN") return VK_RBUTTON;
else if (KeyName == "MOUSE-MIDDLE-BTN") return VK_MBUTTON;
else if (KeyName == "MOUSE-SIDE1-BTN") return VK_XBUTTON1;
else if (KeyName == "MOUSE-SIDE2-BTN") return VK_XBUTTON2;
else if (KeyName == "ESCAPE") return VK_ESCAPE;
else if (KeyName == "F1") return VK_F1;
else if (KeyName == "F2") return VK_F2;
else if (KeyName == "F3") return VK_F3;
else if (KeyName == "F4") return VK_F4;
else if (KeyName == "F5") return VK_F5;
else if (KeyName == "F6") return VK_F6;
else if (KeyName == "F7") return VK_F7;
else if (KeyName == "F8") return VK_F8;
else if (KeyName == "F9") return VK_F9;
else if (KeyName == "F10") return VK_F10;
else if (KeyName == "F11") return VK_F11;
else if (KeyName == "F12") return VK_F12;
else if (KeyName == "~") return 192;
else if (KeyName == "1") return '1';
else if (KeyName == "2") return '2';
else if (KeyName == "3") return '3';
else if (KeyName == "4") return '4';
else if (KeyName == "5") return '5';
else if (KeyName == "6") return '6';
else if (KeyName == "7") return '7';
else if (KeyName == "8") return '8';
else if (KeyName == "9") return '9';
else if (KeyName == "0") return '0';
else if (KeyName == "-") return 189;
else if (KeyName == "=") return 187;
else if (KeyName == "TAB") return VK_TAB;
else if (KeyName == "CAPS-LOCK") return VK_CAPITAL;
else if (KeyName == "SHIFT") return VK_SHIFT;
else if (KeyName == "CTRL") return VK_CONTROL;
else if (KeyName == "WIN") return VK_LWIN;
else if (KeyName == "ALT") return VK_MENU;
else if (KeyName == "SPACE") return VK_SPACE;
else if (KeyName == "ENTER") return VK_RETURN;
else if (KeyName == "BACKSPACE") return VK_BACK;
else if (KeyName == "Q") return 'Q';
else if (KeyName == "W") return 'W';
else if (KeyName == "E") return 'E';
else if (KeyName == "R") return 'R';
else if (KeyName == "T") return 'T';
else if (KeyName == "Y") return 'Y';
else if (KeyName == "U") return 'U';
else if (KeyName == "I") return 'I';
else if (KeyName == "O") return 'O';
else if (KeyName == "P") return 'P';
else if (KeyName == "[") return '[';
else if (KeyName == "]") return ']';
else if (KeyName == "A") return 'A';
else if (KeyName == "S") return 'S';
else if (KeyName == "D") return 'D';
else if (KeyName == "F") return 'F';
else if (KeyName == "G") return 'G';
else if (KeyName == "H") return 'H';
else if (KeyName == "J") return 'J';
else if (KeyName == "K") return 'K';
else if (KeyName == "L") return 'L';
else if (KeyName == ";") return 186;
else if (KeyName == "'") return 222;
else if (KeyName == "\\") return 220;
else if (KeyName == "Z") return 'Z';
else if (KeyName == "X") return 'X';
else if (KeyName == "C") return 'C';
else if (KeyName == "V") return 'V';
else if (KeyName == "B") return 'B';
else if (KeyName == "N") return 'N';
else if (KeyName == "M") return 'M';
else if (KeyName == "<") return 188;
else if (KeyName == ">") return 190;
else if (KeyName == "?") return 191;
else if (KeyName == "PRINTSCREEN") return VK_SNAPSHOT;
else if (KeyName == "SCROLL-LOCK") return VK_SCROLL;
else if (KeyName == "PAUSE") return VK_PAUSE;
else if (KeyName == "INSERT") return VK_INSERT;
else if (KeyName == "HOME") return VK_HOME;
else if (KeyName == "PAGE-UP") return VK_NEXT;
else if (KeyName == "DELETE") return VK_DELETE;
else if (KeyName == "END") return VK_END;
else if (KeyName == "PAGE-DOWN") return VK_PRIOR;
else if (KeyName == "UP") return VK_UP;
else if (KeyName == "DOWN") return VK_DOWN;
else if (KeyName == "LEFT") return VK_LEFT;
else if (KeyName == "RIGHT") return VK_RIGHT;
else if (KeyName == "NUM-LOCK") return VK_NUMLOCK;
else if (KeyName == "NUMPAD0") return VK_NUMPAD0;
else if (KeyName == "NUMPAD1") return VK_NUMPAD1;
else if (KeyName == "NUMPAD2") return VK_NUMPAD2;
else if (KeyName == "NUMPAD3") return VK_NUMPAD3;
else if (KeyName == "NUMPAD4") return VK_NUMPAD4;
else if (KeyName == "NUMPAD5") return VK_NUMPAD5;
else if (KeyName == "NUMPAD6") return VK_NUMPAD6;
else if (KeyName == "NUMPAD7") return VK_NUMPAD7;
else if (KeyName == "NUMPAD8") return VK_NUMPAD8;
else if (KeyName == "NUMPAD9") return VK_NUMPAD9;
else if (KeyName == "NUMPAD-DIVIDE") return VK_DIVIDE;
else if (KeyName == "NUMPAD-MULTIPLY") return VK_MULTIPLY;
else if (KeyName == "NUMPAD-MINUS") return VK_SUBTRACT;
else if (KeyName == "NUMPAD-PLUS") return VK_ADD;
else if (KeyName == "NUMPAD-DEL") return VK_DECIMAL;
else return 0;
}
//-----------------------------------------------------------------------------
// Purpose: Watchdog
//-----------------------------------------------------------------------------
/*
class CWatchdogDriver_Hydra : public IVRWatchdogProvider
{
public:
CWatchdogDriver_Hydra()
{
m_pWatchdogThread = nullptr;
}
virtual EVRInitError Init(vr::IVRDriverContext *pDriverContext);
virtual void Cleanup();
private:
std::thread *m_pWatchdogThread;
};
CWatchdogDriver_Hydra g_watchdogDriverHydra;
bool g_bExiting = false;
void WatchdogThreadFunction()
{
while (!g_bExiting)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
vr::VRWatchdogHost()->WatchdogWakeUp();
}
}
EVRInitError CWatchdogDriver_Hydra::Init(vr::IVRDriverContext *pDriverContext)
{
VR_INIT_WATCHDOG_DRIVER_CONTEXT(pDriverContext);
InitDriverLog(vr::VRDriverLog());
// Watchdog mode on Windows starts a thread that listens for the 'Y' key on the keyboard to
// be pressed. A real driver should wait for a system button event or something else from the
// the hardware that signals that the VR system should start up.
g_bExiting = false;
m_pWatchdogThread = new std::thread(WatchdogThreadFunction);
if (!m_pWatchdogThread)
{
DriverLog("Unable to create watchdog thread\n");
return VRInitError_Driver_Failed;
}
return VRInitError_None;
}
void CWatchdogDriver_Hydra::Cleanup()
{
g_bExiting = true;
if (m_pWatchdogThread)
{
m_pWatchdogThread->join();
delete m_pWatchdogThread;
m_pWatchdogThread = nullptr;
}
CleanupDriverLog();
}
*/
//-----------------------------------------------------------------------------
// Purpose: Controller Driver
//-----------------------------------------------------------------------------
class CHydraControllerDriver : public vr::ITrackedDeviceServerDriver
{
public:
int32_t sixenceControllerRole;
bool IsActivated() const
{
return m_unObjectId != vr::k_unTrackedDeviceIndexInvalid;
}
bool HasControllerId(int nBase, int nId)
{
return nBase == m_nBase && nId == m_nId;
}
/** Process sixenseControllerData. Return true if it's new to help caller manage sleep durations */
bool Update(sixenseControllerData & cd)
{
if (m_ucPoseSequenceNumber == cd.sequence_number || !IsActivated())
return false;
m_ucPoseSequenceNumber = cd.sequence_number;
UpdateTrackingState(cd);
// Block all buttons until initial press confirms hemisphere
if (WaitingForHemisphereTracking(cd))
return true;
DelaySystemButtonForChording(cd);
if ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0) {
if (RecognizeAsIndexCtrls) {
if ((GetAsyncKeyState('1') & 0x8000) != 0) IndexStickMode = 0;
if ((GetAsyncKeyState('2') & 0x8000) != 0) IndexStickMode = 1;
if ((GetAsyncKeyState('3') & 0x8000) != 0) IndexStickMode = 2;
if ((GetAsyncKeyState('4') & 0x8000) != 0) IndexStickMode = 3;
if ((GetAsyncKeyState('5') & 0x8000) != 0) IndexStickMode = 4;
} else {
if ((GetAsyncKeyState('1') & 0x8000) != 0) ViveStickMode = 0;
if ((GetAsyncKeyState('2') & 0x8000) != 0) ViveStickMode = 1;
if ((GetAsyncKeyState('3') & 0x8000) != 0) ViveStickMode = 2;
if ((GetAsyncKeyState('4') & 0x8000) != 0) ViveStickMode = 3;
}
if ((GetAsyncKeyState('9') & 0x8000) != 0) m_bCrouchEnable = true;
if ((GetAsyncKeyState('0') & 0x8000) != 0) m_bCrouchEnable = false;
}
UpdateControllerState(cd);
return true;
}
bool IsHoldingSystemButton() const
{
return m_eSystemButtonState == k_eWaiting;
}
void ConsumeSystemButtonPress()
{
if (m_eSystemButtonState == k_eWaiting)
{
m_eSystemButtonState = k_eBlocked;
}
}
// User initiated manual alignment of the coordinate system of driver_hydra with the HMD:
//
// The user has put two controllers on either side of her head, near the shoulders. We
// assume that the HMD is roughly in between them (so the exact distance apart doesn't
// matter as long as the pose is symmetrical) and we align the HMD's coordinate system
// using the line between the controllers (again, exact position is not important, only
// symmetry).
static void RealignCoordinates(CHydraControllerDriver * pHydraA, CHydraControllerDriver * pHydraB)
{
if (pHydraA->m_unObjectId == vr::k_unTrackedDeviceIndexInvalid)
return;
pHydraA->m_pAlignmentPartner = pHydraB;
pHydraB->m_pAlignmentPartner = pHydraA;
pHydraB->sixenceControllerRole = HydraLeftRole;
pHydraA->sixenceControllerRole = HydraRightRole;
// Ask hydra_monitor to tell us HMD pose
static vr::VREvent_Data_t nodata = { 0 };
vr::VRServerDriverHost()->VendorSpecificEvent(pHydraA->m_unObjectId,
(vr::EVREventType) (vr::VREvent_VendorSpecific_Reserved_Start + 0), nodata,
-std::chrono::duration_cast<std::chrono::seconds>(k_SystemButtonChordingDelay).count());
}
// hydra_monitor called us back with the HMD information
// (Note we should probably cache pose at the moment of the chording, but we just use current here)
void FinishRealignCoordinates(sixenseMath::Matrix3 & matHmdRotation, sixenseMath::Vector3 & vecHmdPosition)
{
using namespace sixenseMath;
CHydraControllerDriver * pHydraA = this;
CHydraControllerDriver * pHydraB = m_pAlignmentPartner;
if (!pHydraA || !pHydraB)
return;
// Assign left/right arbitrarily for a second
Vector3 posLeft(pHydraA->m_Pose.vecPosition[0], pHydraA->m_Pose.vecPosition[1], pHydraA->m_Pose.vecPosition[2]);
Vector3 posRight(pHydraB->m_Pose.vecPosition[0], pHydraB->m_Pose.vecPosition[1], pHydraB->m_Pose.vecPosition[2]);
Vector3 posCenter = (posLeft + posRight) * 0.5f;
Vector3 posDiff = posRight - posLeft;
// Choose arbitrary controller for hint about which one is on the right:
// Assume controllers are roughly upright, so +X vector points across body.
Quat q1(pHydraA->m_Pose.qRotation.x, pHydraA->m_Pose.qRotation.y, pHydraA->m_Pose.qRotation.z, pHydraA->m_Pose.qRotation.w);
Vector3 rightProbe = q1 * Vector3(1, 0, 0);
if (rightProbe * posDiff < 0) // * is dot product
{
std::swap(posLeft, posRight);
posDiff = posDiff * -1.0f;
}
// Find a vector pointing forward relative to the hands, so we can rotate
// that to match forward for the head. Use -Y by right hand rule.
Vector3 hydraFront = posDiff ^ Vector3(0, -1, 0); // ^ is cross product
Vector3 hmdFront = matHmdRotation * Vector3(0, 0, -1); // -Z implicitly forward
// Project both "front" vectors onto the XZ plane (we only care about yaw,
// because we assume the HMD space is Y up, and hydra space is also Y up,
// assuming the base is level).
hydraFront[1] = 0.0f;
hmdFront[1] = 0.0f;
// Rotation is what makes the hydraFront point toward hmdFront
Quat rotation = Quat::rotation(hydraFront, hmdFront);
// Adjust for the natural pose of HMD vs controllers
Vector3 vecAlignPosition = vecHmdPosition + Vector3(0, -0.100f, -0.100f);
Vector3 translation = vecAlignPosition - rotation * posCenter;
// Note that it is very common for all objects from a given driver to share
// the same world transforms, because they are in the same driver space and
// the same world space.
pHydraA->m_WorldFromDriverTranslation = translation;
pHydraA->m_WorldFromDriverRotation = rotation;
pHydraA->m_bCalibrated = true;
pHydraB->m_WorldFromDriverTranslation = translation;
pHydraB->m_WorldFromDriverRotation = rotation;
pHydraB->m_bCalibrated = true;
}
void DebugRequest(const char * pchRequest, char * pchResponseBuffer, uint32_t unResponseBufferSize)
{
std::istringstream ss(pchRequest);
std::string strCmd;
ss >> strCmd;
if (strCmd == "hydra:realign_coordinates")
{
// hydra_monitor is calling us back with HMD tracking information so we can
// finish realigning our coordinate system to the HMD's
float m[3][3], v[3];
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
// Note the transpose, because sixenseMath::Matrix3 and vr::HmdMatrix34_t disagree on row/col major
ss >> m[j][i];
}
ss >> v[i];
}
sixenseMath::Matrix3 matRot(m);
sixenseMath::Vector3 matPos(v);
FinishRealignCoordinates(matRot, matPos);
}
/*
if (unResponseBufferSize >= 1)
pchResponseBuffer[0] = 0;
*/
}
CHydraControllerDriver(int base, int n):
m_nBase(base),
m_nId(n),
m_ucPoseSequenceNumber(0),
m_eHemisphereTrackingState(k_eHemisphereTrackingDisabled),
m_bCalibrated(false),
m_pAlignmentPartner(NULL),
m_eSystemButtonState(k_eIdle),
m_unObjectId(vr::k_unTrackedDeviceIndexInvalid),
m_ulPropertyContainer(vr::k_ulInvalidPropertyContainer)
{
char buf[1024];
GenerateSerialNumber(buf, sizeof(buf), base, n);
m_sSerialNumber = buf;
m_sModelNumber = "Hydra";
m_sManufacturerName = "Razer";
memset(&m_ControllerState, 0, sizeof(m_ControllerState));
memset(&m_Pose, 0, sizeof(m_Pose));
m_Pose.result = vr::TrackingResult_Calibrating_InProgress;
sixenseControllerData cd;
sixenseGetNewestData(m_nId, &cd);
m_firmware_revision = cd.firmware_revision;
m_hardware_revision = cd.hardware_revision;
DriverLog("Using settings values\n");
// assign rendermodel
//vr::VRSettings()->GetString(k_pch_Hydra_Section, k_pch_Hydra_RenderModel_String, buf, sizeof(buf));
//m_sRenderModel = buf;
// Controller type
RecognizeAsIndexCtrls = vr::VRSettings()->GetBool(k_pch_Hydra_Section, k_pch_Hydra_RecognizeAsIndexControllers_Bool);
// Enable IMU emulation
m_bEnableIMUEmulation = vr::VRSettings()->GetBool(k_pch_Hydra_Section, k_pch_Hydra_EnableIMU_Bool);
m_bEnableAngularVelocity = true;
// Set joystick deadzone
m_fJoystickDeadzone = vr::VRSettings()->GetFloat(k_pch_Hydra_Section, k_pch_Hydra_JoystickDeadzone_Float);
// "Hold Thumbpad" mode (not user configurable)
m_bEnableHoldThumbpad = true;
// Crouch key code (for send to another HMD drivers)
vr::VRSettings()->GetString(k_pch_Hydra_Section, k_pch_Hydra_CrouchPressKey_String, buf, sizeof(buf));
m_nCrouchPressKey = KeyNameToKeyCode(buf);
// Crouch offset Z
m_fCrouchOffset = vr::VRSettings()->GetFloat(k_pch_Hydra_Section, k_pch_Hydra_CrouchOffset_Float);
// Cutom key code (for send to another apps)
vr::VRSettings()->GetString(k_pch_Hydra_Section, k_pch_Hydra_CustomPressKey_String, buf, sizeof(buf));
m_nCustomPressKey = KeyNameToKeyCode(buf);
// Enable custom key on left Index controller
EnabledCustomKey = vr::VRSettings()->GetBool(k_pch_Hydra_Section, k_pch_Hydra_EnableCustomKey_Bool);
}
virtual ~CHydraControllerDriver()
{
}
virtual EVRInitError Activate(vr::TrackedDeviceIndex_t unObjectId)
{
DriverLog("Activated device: %s (object id %d)\n", GetSerialNumber().c_str(), unObjectId);
m_unObjectId = unObjectId;
g_serverDriverHydra.LaunchHydraMonitor();
// Set properties
m_ulPropertyContainer = vr::VRProperties()->TrackedDeviceToPropertyContainer(m_unObjectId);
// Index controllers
if (RecognizeAsIndexCtrls) {
// Parameters taken from here https://github.com/HadesVR/HadesVR/blob/main/Software/Driver/src/samples/driver_HadesVR/include/devices.hpp
if (sixenceControllerRole == HydraLeftRole) {
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_ControllerRoleHint_Int32, vr::TrackedControllerRole_LeftHand);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ModelNumber_String, "Knuckles Left");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_RenderModelName_String, "{indexcontroller}valve_controller_knu_1_0_Left");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_Firmware_ProgrammingTarget_String, "LHR-E217CD00");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_RegisteredDeviceType_String, "valve/index_controllerLHR-E217CD00");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceOff_String, "{indexcontroller}/icons/left_controller_status_off.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceSearching_String, "{indexcontroller}/icons/left_controller_status_searching.gif");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceSearchingAlert_String, "{indexcontroller}/icons/left_controller_status_searching_alert.gif");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceReady_String, "{indexcontroller}/icons//left_controller_status_ready.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceReadyAlert_String, "{indexcontroller}/icons/left_controller_status_ready_alert.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceNotReady_String, "{indexcontroller}/icons/left_controller_status_error.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceStandby_String, "{indexcontroller}/icons/left_controller_status_off.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceAlertLow_String, "{indexcontroller}/icons/left_controller_status_ready_low.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_SerialNumber_String, "LHR-E217CD00");
vr::VRDriverInput()->CreateSkeletonComponent(m_ulPropertyContainer, "/input/skeleton/left", "/skeleton/hand/left", "/pose/raw", vr::VRSkeletalTracking_Partial, nullptr, 0U, &m_skeletonHandle); // Skeleton not work???
}
else {
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_ControllerRoleHint_Int32, vr::TrackedControllerRole_RightHand);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ModelNumber_String, "Knuckles Right");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_RenderModelName_String, "{indexcontroller}valve_controller_knu_1_0_right");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_Firmware_ProgrammingTarget_String, "LHR-E217CD01");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_RegisteredDeviceType_String, "valve/index_controllerLHR-E217CD01");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceOff_String, "{indexcontroller}/icons/right_controller_status_off.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceSearching_String, "{indexcontroller}/icons/right_controller_status_searching.gif");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceSearchingAlert_String, "{indexcontroller}/icons/right_controller_status_searching_alert.gif");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceReady_String, "{indexcontroller}/icons/right_controller_status_ready.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceReadyAlert_String, "{indexcontroller}/icons/right_controller_status_ready_alert.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceNotReady_String, "{indexcontroller}/icons/right_controller_status_error.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceStandby_String, "{indexcontroller}/icons/right_controller_status_off.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceAlertLow_String, "{indexcontroller}/icons/right_controller_status_ready_low.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_SerialNumber_String, "LHR-E217CD01");
vr::VRDriverInput()->CreateSkeletonComponent(m_ulPropertyContainer, "/input/skeleton/right", "/skeleton/hand/right", "/pose/raw", vr::VRSkeletalTracking_Partial, nullptr, 0U, &m_skeletonHandle); // Skeleton not work???
}
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/thumbstick/x", &m_thumbstickX, vr::VRScalarType_Absolute, vr::VRScalarUnits_NormalizedTwoSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/thumbstick/y", &m_thumbstickY, vr::VRScalarType_Absolute, vr::VRScalarUnits_NormalizedTwoSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trigger/value", &m_triggerValue, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trackpad/x", &m_trackpadX, vr::VRScalarType_Absolute, vr::VRScalarUnits_NormalizedTwoSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trackpad/y", &m_trackpadY, vr::VRScalarType_Absolute, vr::VRScalarUnits_NormalizedTwoSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trackpad/force", &m_trackpadForce, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/finger/index", &m_fingerIndex, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/finger/middle", &m_fingerMiddle, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/finger/ring", &m_fingerRing, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/finger/pinky", &m_fingerPinky, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/grip/force", &m_gripForce, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/grip/value", &m_gripValue, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
// Buttons handles
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/a/click", &m_aClick);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/a/touch", &m_aTouch);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/b/click", &m_bClick);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/b/touch", &m_bTouch);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/system/click", &m_systemClick);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/system/touch", &m_systemTouch);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/trackpad/touch", &m_trackpadTouch);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/trigger/click", &m_triggerClick);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/grip/touch", &m_gripTouch);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/thumbstick/click", &m_thumbstickClick);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/thumbstick/touch", &m_thumbstickTouch);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_Firmware_UpdateAvailable_Bool, false);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_DeviceProvidesBatteryStatus_Bool, false);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_DeviceCanPowerOff_Bool, true);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_DeviceClass_Int32, vr::TrackedDeviceClass_Controller);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_Firmware_ForceUpdateRequired_Bool, false);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_Identifiable_Bool, true);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_Firmware_RemindUpdate_Bool, false);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_Axis0Type_Int32, vr::k_eControllerAxis_TrackPad);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_Axis1Type_Int32, vr::k_eControllerAxis_Trigger);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_Axis2Type_Int32, vr::k_eControllerAxis_Trigger);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_HasDisplayComponent_Bool, false);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_HasCameraComponent_Bool, false);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_HasDriverDirectModeComponent_Bool, false);
vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, vr::Prop_HasVirtualDisplayComponent_Bool, false);
vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, vr::Prop_ControllerHandSelectionPriority_Int32, 0);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ManufacturerName_String, "Valve");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ResourceRoot_String, "indexcontroller");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_InputProfilePath_String, "{indexcontroller}/input/index_controller_profile.json");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ControllerType_String, "knuckles");
//vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_TrackingSystemName_String, "lighthouse");
} else {
// Vive controller
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ModelNumber_String, "ViveMV"); //m_sModelNumber.c_str()
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_SerialNumber_String, m_sSerialNumber.c_str());
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ControllerType_String, "vive_controller");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_RenderModelName_String, "vr_controller_vive_1_5"); //m_sRenderModel.c_str()
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_ManufacturerName_String, "HTC"); //m_sManufacturerName.c_str()
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_TrackingFirmwareVersion_String, "cd.firmware_revision=" + m_firmware_revision);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_HardwareRevision_String, "cd.hardware_revision=" + m_hardware_revision);
vr::VRProperties()->SetUint64Property(m_ulPropertyContainer, Prop_FirmwareVersion_Uint64, m_firmware_revision);
vr::VRProperties()->SetUint64Property(m_ulPropertyContainer, Prop_HardwareRevision_Uint64, m_hardware_revision);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceOff_String, "{htc}/icons/controller_status_off.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceSearching_String, "{htc}/icons/controller_status_searching.gif");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceSearchingAlert_String, "{htc}/icons/controller_status_searching_alert.gif");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceReady_String, "{htc}/icons/controller_status_ready.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceReadyAlert_String, "{htc}/icons/controller_status_ready_alert.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceNotReady_String, "{htc}/icons/controller_status_error.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceStandby_String, "{htc}/icons/controller_status_off.png");
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, vr::Prop_NamedIconPathDeviceAlertLow_String, "{htc}/icons/controller_status_ready_low.png");
// probably not needed
//vr::VRProperties()->SetUint64Property(m_ulPropertyContainer, Prop_CurrentUniverseId_Uint64, 2);
//vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_DeviceClass_Int32, TrackedDeviceClass_Controller);
//vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_Axis0Type_Int32, k_eControllerAxis_TrackPad);
//vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_Axis1Type_Int32, k_eControllerAxis_Trigger);
//vr::VRProperties()->SetBoolProperty(m_ulPropertyContainer, Prop_IsOnDesktop_Bool, false); // avoid "not fullscreen" warnings from vrmonitor
//vr::VRProperties()->SetInt32Property(m_ulPropertyContainer, Prop_ControllerRoleHint_Int32, TrackedControllerRole_RightHand);
vr::VRProperties()->SetStringProperty(m_ulPropertyContainer, Prop_InputProfilePath_String, "{htc}/input/vive_controller_profile.json");
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/system/click", &m_compSystem);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/grip/click", &m_compGrip);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/application_menu/click", &m_compAppMenu);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/trigger/click", &m_compTriggerClick);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trigger/value", &m_compTrigger, vr::EVRScalarType::VRScalarType_Absolute, vr::EVRScalarUnits::VRScalarUnits_NormalizedOneSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trackpad/x", &m_compJoystickAxisX, vr::VRScalarType_Absolute, vr::VRScalarUnits_NormalizedTwoSided);
vr::VRDriverInput()->CreateScalarComponent(m_ulPropertyContainer, "/input/trackpad/y", &m_compJoystickAxisY, vr::VRScalarType_Absolute, vr::VRScalarUnits_NormalizedTwoSided);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/trackpad/click", &m_compJoystickButton);
vr::VRDriverInput()->CreateBooleanComponent(m_ulPropertyContainer, "/input/trackpad/touch", &m_compJoystickTouch);
vr::VRDriverInput()->CreateHapticComponent(m_ulPropertyContainer, "/output/haptic", &m_compHaptic);
}
// create our haptic component
//vr::VRDriverInput()->CreateHapticComponent(m_ulPropertyContainer, "/output/haptic", &haptic);
return VRInitError_None;
}
virtual void Deactivate()
{
DriverLog("Deactivated device: %s (object id %d)\n", GetSerialNumber().c_str(), m_unObjectId);
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
}
virtual void EnterStandby()
{
}
void *GetComponent(const char *pchComponentNameAndVersion)
{
// override this to add a component to a driver
return NULL;
}
virtual void PowerOff()
{
}
virtual DriverPose_t GetPose()
{
DriverPose_t pose = { 0 };
pose.poseIsValid = false;
pose.result = TrackingResult_Calibrating_OutOfRange;
pose.deviceIsConnected = true;
pose.qWorldFromDriverRotation = HmdQuaternion_Init(1, 0, 0, 0);
pose.qDriverFromHeadRotation = HmdQuaternion_Init(1, 0, 0, 0);
return pose;
}
void RunFrame()
{
#if defined( _WIN32 )
// Your driver would read whatever hardware state is associated with its input components and pass that
// in to UpdateBooleanComponent. This could happen in RunFrame or on a thread of your own that's reading USB
// state. There's no need to update input state unless it changes, but it doesn't do any harm to do so.
#endif
}
void ProcessEvent(const vr::VREvent_t & vrEvent)
{
switch (vrEvent.eventType)
{
case vr::VREvent_Input_HapticVibration:
{
if (vrEvent.data.hapticVibration.componentHandle == m_compHaptic)
{
// This is where you would send a signal to your hardware to trigger actual haptic feedback
}
}
break;
}
}
std::string GetSerialNumber() const { return m_sSerialNumber; }
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
private:
vr::TrackedDeviceIndex_t m_unObjectId;
vr::PropertyContainerHandle_t m_ulPropertyContainer;
vr::ETrackedControllerRole m_eControllerRole;
// Index controller
vr::VRInputComponentHandle_t m_thumbstickX;
vr::VRInputComponentHandle_t m_thumbstickY;
vr::VRInputComponentHandle_t m_triggerValue;
vr::VRInputComponentHandle_t m_trackpadX;
vr::VRInputComponentHandle_t m_trackpadY;
vr::VRInputComponentHandle_t m_trackpadForce;
vr::VRInputComponentHandle_t m_fingerIndex;
vr::VRInputComponentHandle_t m_fingerMiddle;
vr::VRInputComponentHandle_t m_fingerRing;
vr::VRInputComponentHandle_t m_fingerPinky;
vr::VRInputComponentHandle_t m_gripForce;
vr::VRInputComponentHandle_t m_gripValue;
vr::VRInputComponentHandle_t m_aClick;
vr::VRInputComponentHandle_t m_aTouch;
vr::VRInputComponentHandle_t m_bClick;
vr::VRInputComponentHandle_t m_bTouch;
vr::VRInputComponentHandle_t m_systemClick;
vr::VRInputComponentHandle_t m_systemTouch;
vr::VRInputComponentHandle_t m_trackpadTouch;
vr::VRInputComponentHandle_t m_triggerClick;
vr::VRInputComponentHandle_t m_gripTouch;
vr::VRInputComponentHandle_t m_thumbstickClick;
vr::VRInputComponentHandle_t m_thumbstickTouch;
vr::VRInputComponentHandle_t m_skeletonHandle; //??
// Vive controller
vr::VRInputComponentHandle_t m_compSystem;
vr::VRInputComponentHandle_t m_compJoystickButton;
vr::VRInputComponentHandle_t m_compJoystickTouch;
vr::VRInputComponentHandle_t m_compJoystickAxisX;
vr::VRInputComponentHandle_t m_compJoystickAxisY;
vr::VRInputComponentHandle_t m_compTrigger;
vr::VRInputComponentHandle_t m_compTriggerClick;
vr::VRInputComponentHandle_t m_compGrip;
vr::VRInputComponentHandle_t m_compAppMenu;
// Etc
vr::VRInputComponentHandle_t m_compHaptic;
std::string m_sSerialNumber;
std::string m_sModelNumber;
std::string m_sManufacturerName;
//std::string m_sRenderModel;
// Which Hydra controller
int m_nBase;
int m_nId;
// Used to deduplicate state data from the sixense driver
uint8_t m_ucPoseSequenceNumber;
// To main structures for passing state to vrserver
vr::DriverPose_t m_Pose;
vr::VRControllerState_t m_ControllerState;
// Ancillary tracking state
sixenseMath::Vector3 m_WorldFromDriverTranslation;
sixenseMath::Quat m_WorldFromDriverRotation;
sixenseUtils::Derivatives m_Deriv;
enum { k_eHemisphereTrackingDisabled, k_eHemisphereTrackingButtonDown, k_eHemisphereTrackingEnabled } m_eHemisphereTrackingState;
bool m_bCalibrated;
// Other controller with from the last realignment
CHydraControllerDriver *m_pAlignmentPartner;
// Timeout for system button chording
std::chrono::steady_clock::time_point m_SystemButtonDelay;
enum { k_eIdle, k_eWaiting, k_eSent, k_ePulsed, k_eBlocked } m_eSystemButtonState;
// Cached for answering version queries from vrserver
unsigned short m_firmware_revision;
unsigned short m_hardware_revision;
static const float k_fScaleSixenseToMeters;
static const std::chrono::milliseconds k_SystemButtonChordingDelay;
static const std::chrono::milliseconds k_SystemButtonPulsingDuration;
typedef void (vr::IVRServerDriverHost::*ButtonUpdate)(uint32_t unWhichDevice, vr::EVRButtonId eButtonId, double eventTimeOffset);
//void SendButtonUpdates(ButtonUpdate ButtonEvent, uint64_t ulMask);
// IMU emulation things
std::chrono::steady_clock::time_point m_ControllerLastUpdateTime;
Eigen::Quaternionf m_ControllerLastRotation;
sixenseMath::Vector3 m_LastVelocity;
sixenseMath::Vector3 m_LastAcceleration;
Eigen::Vector3f m_LastAngularVelocity;
bool m_bHasUpdateHistory;
bool m_bEnableAngularVelocity;
bool m_bEnableHoldThumbpad;
// steamvr.vrsettings config values
bool m_bEnableIMUEmulation;
float m_fJoystickDeadzone;
int32_t m_nCrouchPressKey;
int32_t m_nCustomPressKey;
float m_fCrouchOffset;
void UpdateControllerState(sixenseControllerData & cd)
{
/**
* Handling button presses for the user switchable features go here,
* before the driver state is updated.
*
* 18/08/18 - Developer mode removed, because we want to forward all
* button presses to the new input system from now on
*/
/*
// Developer mode
if (m_bEnableDeveloperMode) {
// Button 1 toggles imu emulation
if (cd.buttons & SIXENSE_BUTTON_1) {
m_bEnableIMUEmulation = !m_bEnableIMUEmulation;
m_bHasUpdateHistory = false;
}
// Button 2 toggles angular velocity emulation
if (cd.buttons & SIXENSE_BUTTON_2) {
m_bEnableAngularVelocity = !m_bEnableAngularVelocity;
}
}
*/
// Update input components
// Crouch
if (m_bCrouchEnable && sixenceControllerRole == HydraRightRole)
m_bCrouchPressed = cd.buttons & SIXENSE_BUTTON_3;
if (m_bCrouchPressed) {
PosZOffset = m_fCrouchOffset;
keybd_event(m_nCrouchPressKey, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); // Key down
} else if (PosZOffset != 0) {
keybd_event(m_nCrouchPressKey, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); // Key up
PosZOffset = 0;
}
// Index controllers
if (RecognizeAsIndexCtrls) {
// System button
vr::VRDriverInput()->UpdateBooleanComponent(m_systemTouch, cd.buttons & SIXENSE_BUTTON_START, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_systemClick, cd.buttons & SIXENSE_BUTTON_START, 0);
if (sixenceControllerRole == HydraLeftRole) {
// A button
vr::VRDriverInput()->UpdateBooleanComponent(m_aTouch, cd.buttons & SIXENSE_BUTTON_1, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_aClick, cd.buttons & SIXENSE_BUTTON_1, 0);
// B button
vr::VRDriverInput()->UpdateBooleanComponent(m_bTouch, cd.buttons & SIXENSE_BUTTON_3, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_bClick, cd.buttons & SIXENSE_BUTTON_3, 0);
} else { // Right controller
// A button
vr::VRDriverInput()->UpdateBooleanComponent(m_aTouch, cd.buttons & SIXENSE_BUTTON_2, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_aClick, cd.buttons & SIXENSE_BUTTON_2, 0);
// B button
vr::VRDriverInput()->UpdateBooleanComponent(m_bTouch, cd.buttons & SIXENSE_BUTTON_4, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_bClick, cd.buttons & SIXENSE_BUTTON_4, 0);
}
// Joystick axis
vr::VRDriverInput()->UpdateBooleanComponent(m_thumbstickTouch, false, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_trackpadTouch, false, 0);
float joyStickX = 0, joyStickY = 0;
if (fabsf(cd.joystick_x) > m_fJoystickDeadzone || fabsf(cd.joystick_y) > m_fJoystickDeadzone)
{
joyStickX = cd.joystick_x;
joyStickY = cd.joystick_y;
if (IndexStickMode == 0)
vr::VRDriverInput()->UpdateBooleanComponent(m_thumbstickTouch, true, 0);
else if (IndexStickMode == 1)
vr::VRDriverInput()->UpdateBooleanComponent(m_trackpadTouch, true, 0);
else if (IndexStickMode == 2 || IndexStickMode == 3 || IndexStickMode == 4) {
vr::VRDriverInput()->UpdateBooleanComponent(m_thumbstickTouch, true, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_trackpadTouch, true, 0);
}
}
if (IndexStickMode == 0) {
vr::VRDriverInput()->UpdateScalarComponent(m_thumbstickX, joyStickX, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_thumbstickY, joyStickY, 0);
} else if (IndexStickMode == 1) {
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadX, joyStickX, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadY, joyStickY, 0);
} else if (IndexStickMode == 2 || IndexStickMode == 3 || IndexStickMode == 4) {
vr::VRDriverInput()->UpdateScalarComponent(m_thumbstickX, joyStickX, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_thumbstickY, joyStickY, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadX, joyStickX, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadY, joyStickY, 0);
}
// Joystick button
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, 0, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_thumbstickClick, cd.buttons & SIXENSE_BUTTON_JOYSTICK, 0);
if (IndexStickMode == 1) { // Touchpad mode
vr::VRDriverInput()->UpdateBooleanComponent(m_thumbstickClick, false, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 1.0f : 0, 0);
} else if (IndexStickMode == 2) { // Touchpad mirror
vr::VRDriverInput()->UpdateBooleanComponent(m_thumbstickClick, cd.buttons & SIXENSE_BUTTON_JOYSTICK, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 1.0f : 0, 0);
} else if (IndexStickMode == 3) { // Inverse dpad left and right on right controller
if (sixenceControllerRole == HydraLeftRole)
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 1.0f : 0, 0);
else {
if (joyStickX != 0 && joyStickY < 0.3f && joyStickY > -0.3f)
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 0 : 1.0f, 0);
else
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 1.0f : 0, 0);
}
} else if (IndexStickMode == 4) { // Always pressed except dpad up & down on second controler
if (sixenceControllerRole == HydraLeftRole)
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 0 : 1.0f, 0);
else {
if (joyStickX != 0 && joyStickY < 0.3f && joyStickY > -0.3f)
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 0 : 1.0f, 0);
else
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 1.0f : 0, 0);
}
}
else if (IndexStickMode == 5) { // Fully Inverse touchpad press
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, (cd.buttons & SIXENSE_BUTTON_JOYSTICK) ? 0 : 1.0f, 0);
}
// Trigger
vr::VRDriverInput()->UpdateScalarComponent(m_triggerValue, cd.trigger, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_triggerClick, cd.trigger > 0.9f, 0); //?
// Bumper button
if ((sixenceControllerRole == HydraLeftRole && cd.buttons & SIXENSE_BUTTON_2) || (sixenceControllerRole == HydraRightRole && cd.buttons & SIXENSE_BUTTON_1) || (cd.buttons & SIXENSE_BUTTON_BUMPER)) {
vr::VRDriverInput()->UpdateScalarComponent(m_fingerMiddle, 1.0f, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_fingerRing, 1.0f, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_fingerPinky, 1.0f, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_gripValue, 1.0f, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_gripForce, 1.0f, 0);
} else {
vr::VRDriverInput()->UpdateScalarComponent(m_fingerMiddle, 0, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_fingerRing, 0, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_fingerPinky, 0, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_gripValue, 0, 0);
vr::VRDriverInput()->UpdateScalarComponent(m_gripForce, 0, 0);
}
vr::VRDriverInput()->UpdateBooleanComponent(m_fingerIndex, cd.trigger > 0.1f ? 1.0f : 0, 0); //?
// Custom button "Touchpad click"
if (sixenceControllerRole == HydraLeftRole)
m_bIndexCustomKeyPressed = cd.buttons & SIXENSE_BUTTON_4;
if (sixenceControllerRole == HydraRightRole && m_bIndexCustomKeyPressed) {
if (EnabledCustomKey == false) {
if (m_bCrouchEnable) { // If crouch is off, then we do nothing here
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, 1.0f, 0);
vr::VRDriverInput()->UpdateBooleanComponent(m_trackpadTouch, true, 0);
}
} else {
keybd_event(m_nCustomPressKey, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); // Key down
m_bIndexKeyboardKeyPressed = true;
}
} else if (m_bIndexKeyboardKeyPressed && EnabledCustomKey) {
keybd_event(m_nCustomPressKey, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); // Key up
m_bIndexKeyboardKeyPressed = false;
}
// If crouch is disabled, then the touchpad click is emulated on controllers
if (!m_bCrouchEnable) {
if (!EnabledCustomKey && sixenceControllerRole == HydraLeftRole && cd.buttons & SIXENSE_BUTTON_4) {
vr::VRDriverInput()->UpdateScalarComponent(m_trackpadForce, 1.0f, 0);