forked from OSVR/SteamVR-OSVR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSVRTrackedDevice.cpp
1151 lines (1003 loc) · 44.4 KB
/
OSVRTrackedDevice.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
/** @file
@brief OSVR tracked device
@date 2015
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2015 Sensics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Internal Includes
#include "OSVRTrackedDevice.h"
#include "Logging.h"
#include "AngVelTools.h"
#include "OSVRDisplay.h"
#include "ValveStrCpy.h"
#include "make_unique.h"
#include "matrix_cast.h"
#include "osvr_compiler_detection.h"
#include "osvr_device_properties.h"
#include "platform_fixes.h" // strcasecmp
// OpenVR includes
#include <openvr_driver.h>
// Library/third-party includes
#include <osvr/Client/RenderManagerConfig.h>
#include <osvr/ClientKit/Display.h>
#include <osvr/Display/DisplayEnumerator.h>
#include <osvr/RenderKit/DistortionCorrectTextureCoordinate.h>
#include <osvr/Util/EigenInterop.h>
#include <osvr/Util/PlatformConfig.h>
#include <osvr/ClientKit/InterfaceStateC.h>
#include <util/FixedLengthStringFunctions.h>
// Standard includes
#include <algorithm> // for std::find
#include <cstring>
#include <ctime>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
#include <tuple>
OSVRTrackedDevice::OSVRTrackedDevice(osvr::clientkit::ClientContext& context, vr::IServerDriverHost* driver_host, vr::IDriverLog* driver_log) : context_(context), driverHost_(driver_host), pose_(), deviceClass_(vr::TrackedDeviceClass_HMD)
{
OSVR_LOG(trace) << "OSVRTrackedDevice::OSVRTrackedDevice() called.";
OSVR_LOG(debug) << "Client context: " << (&context_);
settings_ = std::make_unique<Settings>(driver_host->GetSettings(vr::IVRSettings_Version));
if (driver_log) {
Logging::instance().setDriverLog(driver_log);
}
}
OSVRTrackedDevice::~OSVRTrackedDevice()
{
driverHost_ = nullptr;
}
vr::EVRInitError OSVRTrackedDevice::Activate(uint32_t object_id)
{
OSVR_LOG(trace) << "OSVRTrackedDevice::Activate() called.";
objectId_ = object_id;
// TODO use C++11 <chrono>
const std::time_t waitTime = settings_->getSetting<int32_t>("serverTimeout", 5);
// Register tracker callback
if (trackerInterface_.notEmpty()) {
trackerInterface_.free();
}
// Ensure context is fully started up
OSVR_LOG(trace) << "OSVRTrackedDevice::Activate(): Waiting for the context to fully start up...\n";
std::time_t startTime = std::time(nullptr);
while (!context_.checkStatus()) {
context_.update();
if (std::time(nullptr) > startTime + waitTime) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Context startup timed out!\n";
return vr::VRInitError_Driver_Failed;
}
}
displayConfig_ = osvr::clientkit::DisplayConfig(context_);
// Ensure display is fully started up
OSVR_LOG(trace) << "OSVRTrackedDevice::Activate(): Waiting for the display to fully start up, including receiving initial pose update...\n";
startTime = std::time(nullptr);
while (!displayConfig_.checkStartup()) {
context_.update();
if (std::time(nullptr) > startTime + waitTime) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Display startup timed out!\n";
return vr::VRInitError_Driver_Failed;
}
}
// Verify valid display config
if ((displayConfig_.getNumViewers() != 1) && (displayConfig_.getViewer(0).getNumEyes() != 2) && (displayConfig_.getViewer(0).getEye(0).getNumSurfaces() == 1) && (displayConfig_.getViewer(0).getEye(1).getNumSurfaces() != 1)) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Unexpected display parameters!\n";
if (displayConfig_.getNumViewers() < 1) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): At least one viewer must exist.\n";
return vr::VRInitError_Driver_HmdDisplayNotFound;
} else if (displayConfig_.getViewer(0).getNumEyes() < 2) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): At least two eyes must exist.\n";
return vr::VRInitError_Driver_HmdDisplayNotFound;
} else if ((displayConfig_.getViewer(0).getEye(0).getNumSurfaces() < 1) || (displayConfig_.getViewer(0).getEye(1).getNumSurfaces() < 1)) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): At least one surface must exist for each eye.\n";
return vr::VRInitError_Driver_HmdDisplayNotFound;
}
}
// Register tracker callback
trackerInterface_ = context_.getInterface("/me/head");
trackerInterface_.registerCallback(&OSVRTrackedDevice::HmdTrackerCallback, this);
auto configString = context_.getStringParameter("/renderManagerConfig");
// If the /renderManagerConfig parameter is missing from the configuration
// file, use an empty dictionary instead. This allows the render manager
// config to zero out its values.
if (configString.empty()) {
OSVR_LOG(info) << "OSVRTrackedDevice::Activate(): Render Manager config is empty, using default values.\n";
configString = "{}";
}
try {
renderManagerConfig_.parse(configString);
} catch (const std::exception& e) {
OSVR_LOG(err) << "OSVRTrackedDevice::Activate(): Exception parsing Render Manager config: " << e.what() << "\n";
}
configure();
configureDistortionParameters();
driverHost_->ProximitySensorState(objectId_, true);
OSVR_LOG(trace) << "OSVRTrackedDevice::Activate(): Activation complete.\n";
return vr::VRInitError_None;
}
void OSVRTrackedDevice::Deactivate()
{
OSVR_LOG(trace) << "OSVRTrackedDevice::Deactivate() called.";
/// Have to force freeing here
if (trackerInterface_.notEmpty()) {
trackerInterface_.free();
}
}
void OSVRTrackedDevice::EnterStandby()
{
// FIXME Implement
}
void* OSVRTrackedDevice::GetComponent(const char* component_name_and_version)
{
if (!strcasecmp(component_name_and_version, vr::IVRDisplayComponent_Version)) {
return static_cast<vr::IVRDisplayComponent*>(this);
}
// Override this to add a component to a driver
return nullptr;
}
void OSVRTrackedDevice::DebugRequest(const char* request, char* response_buffer, uint32_t response_buffer_size)
{
// TODO
// Log the requests just to see what info clients are looking for
OSVR_LOG(debug) << "Received debug request [" << request << "] with response buffer size of " << response_buffer_size << "].";
// make use of (from vrtypes.h) static const uint32_t k_unMaxDriverDebugResponseSize = 32768;
// return empty string for now
if (response_buffer_size > 0) {
response_buffer[0] = '\0';
}
}
void OSVRTrackedDevice::GetWindowBounds(int32_t* x, int32_t* y, uint32_t* width, uint32_t* height)
{
const auto bounds = getWindowBounds(display_, scanoutOrigin_);
*x = bounds.x;
*y = bounds.y;
*width = bounds.width;
*height = bounds.height;
}
bool OSVRTrackedDevice::IsDisplayOnDesktop()
{
// If the current display still appears in the active displays list,
// then it's attached to the desktop.
const auto displays = osvr::display::getDisplays();
const auto display_on_desktop = (end(displays) != std::find(begin(displays), end(displays), display_));
OSVR_LOG(trace) << "OSVRTrackedDevice::IsDisplayOnDesktop(): " << (display_on_desktop ? "yes" : "no");
return display_on_desktop;
}
bool OSVRTrackedDevice::IsDisplayRealDisplay()
{
// TODO get this info from display description?
return true;
}
void OSVRTrackedDevice::GetRecommendedRenderTargetSize(uint32_t* width, uint32_t* height)
{
//const double overfill_factor = renderManagerConfig_.getRenderOverfillFactor();
const double overfill_factor = 1.0;
const auto bounds = getWindowBounds(display_, scanoutOrigin_);
*width = static_cast<uint32_t>(bounds.width * overfill_factor);
*height = static_cast<uint32_t>(bounds.height * overfill_factor);
OSVR_LOG(trace) << "GetRecommendedRenderTargetSize(): width = " << *width << ", height = " << *height << ".";
}
void OSVRTrackedDevice::GetEyeOutputViewport(vr::EVREye eye, uint32_t* x, uint32_t* y, uint32_t* width, uint32_t* height)
{
const auto display_mode = displayConfiguration_.getDisplayMode();
const auto viewport = getEyeOutputViewport(eye, display_, scanoutOrigin_, display_mode);
*x = static_cast<uint32_t>(viewport.x);
*y = static_cast<uint32_t>(viewport.y);
*width = viewport.width;
*height = viewport.height;
}
void OSVRTrackedDevice::GetProjectionRaw(vr::EVREye eye, float* left, float* right, float* top, float* bottom)
{
// Reference: https://github.com/ValveSoftware/openvr/wiki/IVRSystem::GetProjectionRaw
// SteamVR expects top and bottom to be swapped!
osvr::clientkit::ProjectionClippingPlanes pl = displayConfig_.getViewer(0).getEye(eye).getSurface(0).getProjectionClippingPlanes();
*left = static_cast<float>(pl.left);
*right = static_cast<float>(pl.right);
*bottom = static_cast<float>(pl.top); // SWAPPED
*top = static_cast<float>(pl.bottom); // SWAPPED
}
vr::DistortionCoordinates_t OSVRTrackedDevice::ComputeDistortion(vr::EVREye eye, float u, float v)
{
// Rotate the texture coordinates to match the display orientation
const auto orientation = scanoutOrigin_ + display_.rotation;
const auto desired_orientation = osvr::display::DesktopOrientation::Landscape;
const auto rotation = desired_orientation - orientation;
std::tie(u, v) = rotate(u, v, rotation);
// Note that RenderManager expects the (0, 0) to be the lower-left corner
// and (1, 1) to be the upper-right corner while SteamVR assumes (0, 0) is
// upper-left and (1, 1) is lower-right. To accommodate this, we need to
// flip the y-coordinate before passing it to RenderManager and flip it
// again before returning the value to SteamVR.
using osvr::renderkit::DistortionCorrectTextureCoordinate;
static const size_t COLOR_RED = 0;
static const size_t COLOR_GREEN = 1;
static const size_t COLOR_BLUE = 2;
const auto osvr_eye = static_cast<size_t>(eye);
const auto distortion_parameters = distortionParameters_[osvr_eye];
const auto in_coords = osvr::renderkit::Float2{{u, 1.0f - v}}; // flip v-coordinate
const auto interpolators = (vr::Eye_Left == eye) ? &leftEyeInterpolators_ : &rightEyeInterpolators_;
auto coords_red = DistortionCorrectTextureCoordinate(
osvr_eye, in_coords, distortion_parameters,
COLOR_RED, overfillFactor_, *interpolators);
auto coords_green = DistortionCorrectTextureCoordinate(
osvr_eye, in_coords, distortion_parameters,
COLOR_GREEN, overfillFactor_, *interpolators);
auto coords_blue = DistortionCorrectTextureCoordinate(
osvr_eye, in_coords, distortion_parameters,
COLOR_BLUE, overfillFactor_, *interpolators);
vr::DistortionCoordinates_t coords;
// flip v-coordinates again
coords.rfRed[0] = coords_red[0];
coords.rfRed[1] = 1.0f - coords_red[1];
coords.rfGreen[0] = coords_green[0];
coords.rfGreen[1] = 1.0f - coords_green[1];
coords.rfBlue[0] = coords_blue[0];
coords.rfBlue[1] = 1.0f - coords_blue[1];
return coords;
}
vr::DriverPose_t OSVRTrackedDevice::GetPose()
{
return pose_;
}
bool OSVRTrackedDevice::GetBoolTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error)
{
const bool default_value = false;
const auto result = checkProperty(prop, bool());
if (vr::TrackedProp_Success != result) {
if (error)
*error = result;
return default_value;
}
#include "ignore-warning/push"
#include "ignore-warning/switch-enum"
// Prop_ContainsProximitySensor_Bool spams our log files. Ignoring it here.
OSVR_LOG(properties) << "OSVRTrackedDevice::GetBoolTrackedDeviceProperty(): Requested property: " << prop << "\n";
switch (prop) {
// Properties that apply to all device classes
case vr::Prop_WillDriftInYaw_Bool:
if (error)
*error = vr::TrackedProp_Success;
return true;
break;
case vr::Prop_DeviceIsWireless_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
case vr::Prop_DeviceIsCharging_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
case vr::Prop_Firmware_UpdateAvailable_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
case vr::Prop_Firmware_ManualUpdate_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
case vr::Prop_BlockServerShutdown_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
case vr::Prop_CanUnifyCoordinateSystemWithHmd_Bool: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
break;
case vr::Prop_ContainsProximitySensor_Bool:
if (error)
*error = vr::TrackedProp_Success;
return true;
break;
case vr::Prop_DeviceProvidesBatteryStatus_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
case vr::Prop_DeviceCanPowerOff_Bool:
if (error)
*error = vr::TrackedProp_Success;
return true;
break;
case vr::Prop_HasCamera_Bool:
if (error)
*error = vr::TrackedProp_Success;
return false;
break;
// Properties that apply to HMDs
case vr::Prop_ReportsTimeSinceVSync_Bool: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
break;
case vr::Prop_IsOnDesktop_Bool:
if (error)
*error = vr::TrackedProp_Success;
return this->IsDisplayOnDesktop();
break;
}
#include "ignore-warning/pop"
OSVR_LOG(warn) << "OSVRTrackedDevice::GetBoolTrackedDeviceProperty(): Unknown property " << prop << " requested.\n";
if (error)
*error = vr::TrackedProp_UnknownProperty;
return default_value;
}
float OSVRTrackedDevice::GetFloatTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error)
{
const float default_value = 0.0f;
const auto result = checkProperty(prop, float());
if (vr::TrackedProp_Success != result) {
if (error)
*error = result;
return default_value;
}
#include "ignore-warning/push"
#include "ignore-warning/switch-enum"
OSVR_LOG(properties) << "OSVRTrackedDevice::GetFloatTrackedDeviceProperty(): Requested property: " << prop << "\n";
switch (prop) {
// General properties that apply to all device classes
case vr::Prop_DeviceBatteryPercentage_Float:
if (error)
*error = vr::TrackedProp_Success;
return 1.0f; // full battery
// Properties that are unique to TrackedDeviceClass_HMD
case vr::Prop_SecondsFromVsyncToPhotons_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayFrequency_Float:
if (error)
*error = vr::TrackedProp_Success;
return static_cast<float>(display_.verticalRefreshRate);
case vr::Prop_UserIpdMeters_Float:
if (error)
*error = vr::TrackedProp_Success;
return GetIPD();
case vr::Prop_DisplayMCOffset_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayMCScale_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayGCBlackClamp_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayGCOffset_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayGCScale_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayGCPrescale_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_LensCenterLeftU_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_LensCenterLeftV_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_LensCenterRightU_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_LensCenterRightV_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_UserHeadToEyeDepthMeters_Float:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_TrackingReference
case vr::Prop_FieldOfViewLeftDegrees_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_FieldOfViewRightDegrees_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_FieldOfViewTopDegrees_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_FieldOfViewBottomDegrees_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_TrackingRangeMinimumMeters_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_TrackingRangeMaximumMeters_Float: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
}
#include "ignore-warning/pop"
OSVR_LOG(warn) << "OSVRTrackedDevice::GetFloatTrackedDeviceProperty(): Unknown property " << prop << " requested.\n";
if (error)
*error = vr::TrackedProp_UnknownProperty;
return default_value;
}
int32_t OSVRTrackedDevice::GetInt32TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error)
{
const int32_t default_value = 0;
const auto result = checkProperty(prop, int32_t());
if (vr::TrackedProp_Success != result) {
if (error)
*error = result;
return default_value;
}
#include "ignore-warning/push"
#include "ignore-warning/switch-enum"
OSVR_LOG(properties) << "OSVRTrackedDevice::GetInt32TrackedDeviceProperty(): Requested property: " << prop << "\n";
switch (prop) {
// General properties that apply to all device classes
case vr::Prop_DeviceClass_Int32:
if (error)
*error = vr::TrackedProp_Success;
return deviceClass_;
// Properties that are unique to TrackedDeviceClass_HMD
case vr::Prop_DisplayMCType_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_EdidVendorID_Int32:
if (error)
*error = vr::TrackedProp_Success;
return static_cast<int32_t>(display_.edidVendorId);
case vr::Prop_EdidProductID_Int32:
if (error)
*error = vr::TrackedProp_Success;
return static_cast<int32_t>(display_.edidProductId);
case vr::Prop_DisplayGCType_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_CameraCompatibilityMode_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_Controller
case vr::Prop_Axis0Type_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_Axis1Type_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_Axis2Type_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_Axis3Type_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_Axis4Type_Int32:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
}
#include "ignore-warning/pop"
OSVR_LOG(warn) << "OSVRTrackedDevice::GetInt32TrackedDeviceProperty(): Unknown property " << prop << " requested.\n";
if (error)
*error = vr::TrackedProp_UnknownProperty;
return default_value;
}
uint64_t OSVRTrackedDevice::GetUint64TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error)
{
const uint64_t default_value = 0;
const auto result = checkProperty(prop, uint64_t());
if (vr::TrackedProp_Success != result) {
if (error)
*error = result;
return default_value;
}
#include "ignore-warning/push"
#include "ignore-warning/switch-enum"
OSVR_LOG(properties) << "OSVRTrackedDevice::GetUint64TrackedDeviceProperty(): Requested property: " << prop << "\n";
switch (prop) {
// General properties that apply to all device classes
case vr::Prop_HardwareRevision_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_FirmwareVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_FPGAVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_VRCVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_RadioVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DongleVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_HMD
case vr::Prop_CurrentUniverseId_Uint64:
if (error)
*error = vr::TrackedProp_Success;
return 1;
case vr::Prop_PreviousUniverseId_Uint64:
if (error)
*error = vr::TrackedProp_Success;
return 1;
case vr::Prop_DisplayFirmwareVersion_Uint64:
/// @todo This really should be read from the server
if (error)
*error = vr::TrackedProp_Success;
return 192;
case vr::Prop_CameraFirmwareVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayFPGAVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayBootloaderVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayHardwareVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_AudioFirmwareVersion_Uint64:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_Controller
case vr::Prop_SupportedButtons_Uint64: // TODO
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
}
#include "ignore-warning/pop"
OSVR_LOG(warn) << "OSVRTrackedDevice::GetUint64TrackedDeviceProperty(): Unknown property " << prop << " requested.\n";
if (error)
*error = vr::TrackedProp_UnknownProperty;
return default_value;
}
vr::HmdMatrix34_t OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error)
{
// Default value is identity matrix
vr::HmdMatrix34_t default_value;
map(default_value) = Matrix34f::Identity();
const auto result = checkProperty(prop, vr::HmdMatrix34_t());
if (vr::TrackedProp_Success != result) {
if (error)
*error = result;
return default_value;
}
#include "ignore-warning/push"
#include "ignore-warning/switch-enum"
OSVR_LOG(properties) << "OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(): Requested property: " << prop << "\n";
switch (prop) {
// General properties that apply to all device classes
case vr::Prop_StatusDisplayTransform_Matrix34:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_HMD
case vr::Prop_CameraToHeadTransform_Matrix34:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
}
#include "ignore-warning/pop"
OSVR_LOG(warn) << "OSVRTrackedDevice::GetMatrix34TrackedDeviceProperty(): Unknown property " << prop << " requested.\n";
if (error)
*error = vr::TrackedProp_UnknownProperty;
return default_value;
}
uint32_t OSVRTrackedDevice::GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, char* value, uint32_t buffer_size, vr::ETrackedPropertyError* error)
{
uint32_t default_value = 0;
const auto result = checkProperty(prop, value);
if (vr::TrackedProp_Success != result) {
if (error)
*error = result;
return default_value;
}
OSVR_LOG(properties) << "OSVRTrackedDevice::GetStringTrackedDeviceProperty(): Requested property: " << prop << "\n";
std::string sValue = GetStringTrackedDeviceProperty(prop, error);
if (*error == vr::TrackedProp_Success) {
if (sValue.size() + 1 > buffer_size) {
*error = vr::TrackedProp_BufferTooSmall;
} else {
valveStrCpy(sValue, value, buffer_size);
}
return static_cast<uint32_t>(sValue.size()) + 1;
}
return 0;
}
// ------------------------------------
// Private Methods
// ------------------------------------
std::string OSVRTrackedDevice::GetStringTrackedDeviceProperty(vr::ETrackedDeviceProperty prop, vr::ETrackedPropertyError* error)
{
std::string default_value = "";
#include "ignore-warning/push"
#include "ignore-warning/switch-enum"
switch (prop) {
// General properties that apply to all device classes
case vr::Prop_TrackingSystemName_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_ModelNumber_String:
if (error)
*error = vr::TrackedProp_Success;
return settings_->getSetting<std::string>("modelNumber", displayConfiguration_.getModel() + " " + displayConfiguration_.getVersion());
case vr::Prop_SerialNumber_String:
if (error)
*error = vr::TrackedProp_Success;
return settings_->getSetting<std::string>("serialNumber", this->GetId());
case vr::Prop_RenderModelName_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_ManufacturerName_String:
if (error)
*error = vr::TrackedProp_Success;
return settings_->getSetting<std::string>("manufacturer", displayConfiguration_.getVendor());
case vr::Prop_TrackingFirmwareVersion_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_HardwareRevision_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_AllWirelessDongleDescriptions_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_ConnectedWirelessDongle_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_Firmware_ManualUpdateURL_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_Firmware_ProgrammingTarget_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DriverVersion_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_HMD
case vr::Prop_DisplayMCImageLeft_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayMCImageRight_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_DisplayGCImage_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
case vr::Prop_CameraFirmwareDescription_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_Controller
case vr::Prop_AttachedDeviceId_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
// Properties that are unique to TrackedDeviceClass_TrackingReference
case vr::Prop_ModeLabel_String:
if (error)
*error = vr::TrackedProp_ValueNotProvidedByDevice;
return default_value;
}
#include "ignore-warning/pop"
OSVR_LOG(warn) << "OSVRTrackedDevice::GetStringTrackedDeviceProperty(): Unknown property " << prop << " requested.\n";
if (error)
*error = vr::TrackedProp_UnknownProperty;
return default_value;
}
void OSVRTrackedDevice::HmdTrackerCallback(void* userdata, const OSVR_TimeValue*, const OSVR_PoseReport* report)
{
if (!userdata)
return;
auto* self = static_cast<OSVRTrackedDevice*>(userdata);
// Get angular velocity in correct format for SteamVR
OSVR_TimeValue timeval2;
OSVR_AngularVelocityState state;
osvrGetAngularVelocityState(self->trackerInterface_.get(), &timeval2, &state);
double dt = state.dt;
Eigen::Quaterniond r = osvr::util::fromQuat(report->pose.rotation);
Eigen::Quaterniond q = osvr::util::fromQuat(state.incrementalRotation);
q = r.inverse() * q * r;
// Convert invcremental rotation to angular velocity
Eigen::Vector3d angularvelocity = osvr::vbtracker::incRotToAngVelVec(q, dt);
vr::DriverPose_t pose;
pose.poseTimeOffset = 0; // close enough
Eigen::Vector3d::Map(pose.vecWorldFromDriverTranslation) = Eigen::Vector3d::Zero();
Eigen::Vector3d::Map(pose.vecDriverFromHeadTranslation) = Eigen::Vector3d::Zero();
map(pose.qWorldFromDriverRotation) = Eigen::Quaterniond::Identity();
map(pose.qDriverFromHeadRotation) = Eigen::Quaterniond::Identity();
// Position
Eigen::Vector3d::Map(pose.vecPosition) = osvr::util::vecMap(report->pose.translation);
// Position velocity and acceleration are not currently consistently provided
Eigen::Vector3d::Map(pose.vecVelocity) = Eigen::Vector3d::Zero();
Eigen::Vector3d::Map(pose.vecAcceleration) = Eigen::Vector3d::Zero();
// Orientation
map(pose.qRotation) = osvr::util::fromQuat(report->pose.rotation);
// Angular velocity and acceleration are not currently consistently provided
Eigen::Vector3d::Map(pose.vecAngularVelocity) = angularvelocity;
Eigen::Vector3d::Map(pose.vecAngularAcceleration) = Eigen::Vector3d::Zero();
pose.result = vr::TrackingResult_Running_OK;
pose.poseIsValid = true;
pose.willDriftInYaw = true;
pose.shouldApplyHeadModel = true;
pose.deviceIsConnected = true;
self->pose_ = pose;
self->driverHost_->TrackedDevicePoseUpdated(self->objectId_, self->pose_);
}
float OSVRTrackedDevice::GetIPD()
{
OSVR_Pose3 leftEye, rightEye;
if (displayConfig_.getViewer(0).getEye(0).getPose(leftEye) != true) {
OSVR_LOG(err) << "OSVRTrackedDevice::GetHeadFromEyePose(): Unable to get left eye pose!\n";
}
if (displayConfig_.getViewer(0).getEye(1).getPose(rightEye) != true) {
OSVR_LOG(err) << "OSVRTrackedDevice::GetHeadFromEyePose(): Unable to get right eye pose!\n";
}
float ipd = static_cast<float>((osvr::util::vecMap(leftEye.translation) - osvr::util::vecMap(rightEye.translation)).norm());
return ipd;
}
const char* OSVRTrackedDevice::GetId()
{
if (display_.name.empty()) {
display_.name = "OSVR HMD";
}
return display_.name.c_str();
}
void OSVRTrackedDevice::configure()
{
// Get settings from config file
const bool verbose_logging = settings_->getSetting<bool>("verbose", false);
if (verbose_logging) {
OSVR_LOG(info) << "Verbose logging enabled.";
Logging::instance().setLogLevel(trace);
} else {
OSVR_LOG(info) << "Verbose logging disabled.";
Logging::instance().setLogLevel(info);
}
// The name of the display we want to use
const auto display_name = settings_->getSetting<std::string>("displayName", "OSVR");
// Detect displays and find the one we're using as an HMD
bool display_found = false;
auto displays = osvr::display::getDisplays();
for (const auto& display : displays) {
if (std::string::npos == display.name.find(display_name)) {
OSVR_LOG(trace) << "Rejecting display [" << display.name << "] since it doesn't match [" << display_name << "].";
continue;
}
OSVR_LOG(trace) << "Found a match! Display [" << display.name << "] matches [" << display_name << "].";
display_ = display;
display_found = true;
// The scan-out origin of the display
const auto scan_out_origin_str = settings_->getSetting<std::string>("scanoutOrigin", "");
if (scan_out_origin_str.empty()) {
// Calculate the scan-out origin based on the display parameters
//scanoutOrigin_ = osvr::display::to_ScanOutOrigin(osvr::display::ScanOutOrigin::UpperLeft + display_.rotation);
const auto rot = renderManagerConfig_.getDisplayRotation();
scanoutOrigin_ = osvr::display::to_ScanOutOrigin(osvr::display::ScanOutOrigin::UpperLeft + osvr::display::to_Rotation(static_cast<int>(rot)));
OSVR_LOG(warn) << "Warning: scan-out origin unspecified. Defaulting to " << scanoutOrigin_ << ".";
} else {
scanoutOrigin_ = parseScanOutOrigin(scan_out_origin_str);
}
break;
}
if (!display_found) {
// If the desired display wasn't detected, use the settings from the
// display descriptor instead.
//
// This will most frequently occur when the HMD is in direct mode or if
// the HMD is disconnected.
displayDescription_ = context_.getStringParameter("/display");
displayConfiguration_ = OSVRDisplayConfiguration(displayDescription_);
const auto d = OSVRDisplayConfiguration(displayDescription_);
auto active_resolution = d.activeResolution();
const auto position_x = renderManagerConfig_.getWindowXPosition();
const auto position_y = renderManagerConfig_.getWindowYPosition();
// Rotation
// Adjust the scan-out orientation based rotation value. Also, when
// the rotation is 90 or 270, we need to swap the resolution values
// and zero out the rotation.. Otherwise, just ignore the rotation (as
// we compensate for it using the scan-out origin).
using osvr::display::Rotation;
auto rotation = Rotation::Zero;
const auto rot = renderManagerConfig_.getDisplayRotation();
if (90 == rot || 270 == rot) {
std::swap(active_resolution.width, active_resolution.height);
}
display_.adapter.description = "Unknown";
display_.name = displayConfiguration_.getVendor() + " " + displayConfiguration_.getModel() + " " + displayConfiguration_.getVersion();
display_.size.width = static_cast<uint32_t>(active_resolution.width);
display_.size.height = static_cast<uint32_t>(active_resolution.height);