aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/OVR_CAPI.cpp
blob: e02151a0ea057e2fa390ddcfd0435fb9ccca3c85 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
/************************************************************************************

Filename    :   OVR_CAPI.cpp
Content     :   Experimental simple C interface to the HMD - version 1.
Created     :   November 30, 2013
Authors     :   Michael Antonov

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
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.

************************************************************************************/

#include "OVR_CAPI.h"
#include "OVR_Version.h"


#include "Kernel/OVR_Timer.h"
#include "Kernel/OVR_System.h"
#include "Kernel/OVR_DebugHelp.h"
#include "Extras/OVR_Math.h"
#include "OVR_Stereo.h"
#include "OVR_Profile.h"

#include "CAPI/CAPI_HMDState.h"

#include "Net/OVR_Session.h"
#include "Service/Service_NetClient.h"
#ifdef OVR_PRIVATE_FILE
#include "Service/Service_NetServer.h"
#endif

#include "Displays/OVR_Display.h"

#if !defined(HEADLESS_APP)
#if defined(OVR_OS_WIN32)
#include "Displays/OVR_Win32_ShimFunctions.h"
#include <VersionHelpers.h>
#endif
#endif /* !defined(HEADLESS_APP) */

// Forward decl to keep the callback static

// Note: Removed CaptureHmdDescTrace from non-Win32 due to build warning.
#if !defined(OVR_OS_MAC) && !defined(OVR_OS_LINUX)
static bool CaptureHmdDescTrace(const OVR::CAPI::HMDState *state);
#endif
#define TRACE_STATE_CAPTURE_FUNC OVR::CAPI::HMDState::EnumerateHMDStateList(CaptureHmdDescTrace)
#include "Tracing/Tracing.h"

#if !defined(OVR_OS_MAC) && !defined(OVR_OS_LINUX)
// EnumerateHMDStateList callback for tracing state capture
static bool CaptureHmdDescTrace(const OVR::CAPI::HMDState* state)
{
    TraceHmdDesc(*state->pHmdDesc);
    OVR_UNUSED(state); // Avoid potential compiler warnings.
    return true;
}
#endif

// Produce an invalid tracking state that will not mess up the application too badly.
static ovrTrackingState GetNullTrackingState()
{
    ovrTrackingState nullState = ovrTrackingState();
    nullState.HeadPose.ThePose.Orientation.w = 1.f; // Provide valid quaternions for head pose.
    return nullState;
}

// Produce a null frame timing structure that will not break the calling application.
static ovrFrameTiming GetNullFrameTiming()
{
    ovrFrameTiming nullTiming = ovrFrameTiming();
    nullTiming.DeltaSeconds = 0.013f; // Provide nominal value
    return nullTiming;
}


using namespace OVR;
using namespace OVR::Util::Render;
using namespace OVR::Vision;

//-------------------------------------------------------------------------------------
// Math
namespace OVR {


// ***** SensorDataType

SensorDataType::SensorDataType(const ovrSensorData& s)
{
    Acceleration        = s.Accelerometer;
    RotationRate        = s.Gyro;
    MagneticField       = s.Magnetometer;
    Temperature         = s.Temperature;
    AbsoluteTimeSeconds = s.TimeInSeconds;
}

SensorDataType::operator ovrSensorData () const
{
    ovrSensorData result;
    result.Accelerometer = Acceleration;
    result.Gyro          = RotationRate;
    result.Magnetometer  = MagneticField;
    result.Temperature   = Temperature;
    result.TimeInSeconds = (float)AbsoluteTimeSeconds;
    return result;
}




// ***** SensorState

TrackingState::TrackingState(const ovrTrackingState& s)
{
    HeadPose          = s.HeadPose;
    CameraPose        = s.CameraPose;
    LeveledCameraPose = s.LeveledCameraPose;
    RawSensorData     = s.RawSensorData;
    StatusFlags       = s.StatusFlags;
}

TrackingState::operator ovrTrackingState() const
{
    ovrTrackingState result;
    result.HeadPose          = HeadPose;
    result.CameraPose        = CameraPose;
    result.LeveledCameraPose = LeveledCameraPose;
    result.RawSensorData     = RawSensorData;
    result.StatusFlags       = StatusFlags;
    return result;
}


} // namespace OVR

//-------------------------------------------------------------------------------------

using namespace OVR::CAPI;


// Helper function to validate the HMD object provided by the API user.
static HMDState* GetHMDStateFromOvrHmd(ovrHmd hmddesc)
{
    if (!hmddesc || !hmddesc->Handle)
        return nullptr;

    return (HMDState*)hmddesc->Handle;
}


OVR_PUBLIC_FUNCTION(double) ovr_GetTimeInSeconds()
{
    return Timer::GetSeconds();
}


//-------------------------------------------------------------------------------------

// 1. Init/shutdown.

static ovrBool CAPI_ovrInitializeCalled = ovrFalse;
static OVR::Service::NetClient* CAPI_pNetClient = nullptr;


#if !defined(HEADLESS_APP)
ovrBool ovr_InitializeRenderingShim()
{
    OVR::Display::Initialize();

    return OVR::Display::GetDirectDisplayInitialized();
}

OVR_PUBLIC_FUNCTION(ovrBool) ovr_InitializeRenderingShimVersion(int requestedMinorVersion)
{
    // We ignore the patch and build versions here, as they aren't relevant to compatibility.
    // And we don't store them away here, as we do that in ovr_Initialize() instead.
    
    if (requestedMinorVersion > OVR_MINOR_VERSION)
        return ovrFalse;

    return ovr_InitializeRenderingShim();
}
#endif /* !defined(HEADLESS_APP) */

// Write out to the log where the current running module is located on disk.
static void LogLocationOfThisModule()
{
#if defined (OVR_OS_WIN32)
    // Log out the DLL file path on startup.
    {
        bool success = false;

        HMODULE hModule = nullptr;
        GetModuleHandleEx(
            GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
            (LPCTSTR)&ovr_Initialize,
            &hModule);
        if (hModule)
        {
            wchar_t filename[_MAX_PATH];
            DWORD len = GetModuleFileNameW(hModule, filename, OVR_ARRAY_COUNT(filename));
            if (len > 0 && filename[0])
            {
                success = true;
                LogText("[CAPI] LibOVR module is located at %ws\n", filename);
            }
        }

        if (!success)
        {
            LogError("[CAPI] WARNING: Unable to find LibOVR module.");
        }
    }
#endif // OVR_OS_WIN32
}

// These defaults are also in OVR_CAPIShim.c
static const ovrInitParams DefaultParams = {
    ovrInit_RequestVersion, // Flags
    OVR_MINOR_VERSION,      // RequestedMinorVersion
    0,                      // LogCallback
    0                       // ConnectionTimeoutSeconds
};

// Precondition: params is not null
OVR_PUBLIC_FUNCTION(ovrBool) ovr_Initialize(ovrInitParams const* params)
{
    // TBD: Should we check if the version requested changed and fail here?
    if (CAPI_ovrInitializeCalled) // If already initialized...
        return ovrTrue;

    TraceInit();
    TraceCall(0);

    if (!params)
    {
        params = &DefaultParams;
    }

    bool DebugMode = (params->Flags & ovrInit_Debug) != 0;

#if defined(OVR_BUILD_DEBUG)
    // If no debug setting is provided,
    if (!(params->Flags & (ovrInit_Debug | ovrInit_ForceNoDebug)))
    {
        DebugMode = true;
    }
#endif

    // We ignore the requested patch version and build version, as they are not currently relevant to
    // the library compatibility. Our test for minor version compatibility is currently simple: we support
    // only older or equal minor versions, and don't change our behavior if the requested minor version
    // is older than than OVR_MINOR_VERSION.

    if ((params->Flags & ovrInit_RequestVersion) != 0 &&
        (params->RequestedMinorVersion > OVR_MINOR_VERSION))
    {
        goto Abort;
    }

#if defined(OVR_OS_WIN32)
    // Older than Windows 7 SP1?
    if (!IsWindows7SP1OrGreater())
    {
        MessageBoxA(nullptr, "This software depends on features available starting with \nWindows 7 Service Pack 1, and it cannot start.", "LibOVR: Cannot start", 0);
        OVR_ASSERT(false);
        goto Abort;
    }
#endif // OVR_OS_WIN32

    OVR::Net::RuntimeSDKVersion.SetCurrent();  // Fill in the constant parts of this struct.
    OVR::Net::RuntimeSDKVersion.RequestedMinorVersion = (uint16_t)params->RequestedMinorVersion;

    // Initialize display subsystem regardless of Allocator initialization.
    OVR::Display::Initialize();

    // We must set up the system for the plugin to work
    if (!OVR::System::IsInitialized())
    {
        // TBD: Base this on registry setting?
        Allocator::SetLeakTracking(DebugMode);

        OVR::Log* logger = OVR::Log::ConfigureDefaultLog(OVR::LogMask_All);

        // Set the CAPI logger callback
        logger->SetCAPICallback(params->LogCallback);

        OVR::System::Init(logger);
    }

    if (!OVR::Display::GetDirectDisplayInitialized() && !OVR::Display::InCompatibilityMode(true))
    {
        OVR_ASSERT(false);
        goto Abort;
    }

    CAPI_pNetClient = NetClient::GetInstance();

    // Store off the initialization parameters from ovr_Initialize()
    CAPI_pNetClient->ApplyParameters(params);

    // Mark as initialized regardless of whether or not we can connect to the server.
    CAPI_ovrInitializeCalled = 1;

    // Log the location of the module after most of the bring-up, as the game
    // could do almost anything in response to a log message callback.
    LogLocationOfThisModule();

    // If unable to connect to server and we are not in a debug mode,
    if (!CAPI_pNetClient->Connect(true) && !DebugMode)
    {
        // Then it's a failure when the server is unreachable.
        // This means that a DebugHMD cannot be created unless the ovrInit_Debug flag is set.
        goto Abort;
    }

    // everything is okay
    TraceReturn(0);
    return ovrTrue;

Abort:
    // clean up and return failure
    TraceReturn(0);
    TraceFini();
    return ovrFalse;
}


OVR_PUBLIC_FUNCTION(void) ovr_Shutdown()
{  
    if (CAPI_ovrInitializeCalled)
    {
        OVR::Display::Shutdown();
        TraceCall(0);
        TraceFini();
        CAPI_ovrInitializeCalled = 0;
    }

    if (OVR::System::IsInitialized())
    {
        OVR::System::Destroy();
    }

    OVR::Net::RuntimeSDKVersion.Reset();
    CAPI_pNetClient = nullptr;  // Not strictly necessary, but useful for debugging and cleanliness.
}


// There is a thread safety issue with ovrHmd_Detect in that multiple calls from different
// threads can corrupt the global array state. This would lead to two problems:
//  a) Create(index) enumerator may miss or overshoot items. Probably not a big deal
//     as game logic can easily be written to only do Detect(s)/Creates in one place.
//     The alternative would be to return list handle.
//  b) TBD: Un-mutexed Detect access from two threads could lead to crash. We should
//         probably check this.
//

OVR_PUBLIC_FUNCTION(int) ovrHmd_Detect()
{
    if (!CAPI_ovrInitializeCalled)
        return -2;

    return CAPI_pNetClient->Hmd_Detect();
}


// ovrHmd_Create us explicitly separated from ConfigureTracking and ConfigureRendering to allow creation of 
// a relatively light-weight handle that would reference the device going forward and would 
// survive future ovrHmd_Detect calls. That is once ovrHMD is returned, index is no longer
// necessary and can be changed by a ovrHmd_Detect call.
OVR_PUBLIC_FUNCTION(ovrHmd) ovrHmd_Create(int index)
{
    if (!CAPI_ovrInitializeCalled)
        return 0;

    double t0 = Timer::GetSeconds();
    HMDNetworkInfo netInfo;

    // There may be some delay before the HMD is fully detected.
    // Since we are also trying to create the HMD immediately it may lose this race and
    // get "NO HMD DETECTED."  Wait a bit longer to avoid this.
    while (!CAPI_pNetClient->Hmd_Create(index, &netInfo) ||
           netInfo.NetId == InvalidVirtualHmdId)
    {
        double waitTime = 2.0; // Default wait time

        if (NetClient::GetInstance()->IsConnected(false, false))
        {
            NetClient::GetInstance()->SetLastError("No HMD Detected");

            // If in single process mode,
            if (Net::Session::IsSingleProcess())
            {
                // Wait 8 seconds for HMD to be detected, as this is a single process
                // build and we expect that the operator has the system set up properly.
                waitTime = 8.0;
            }
            else
            {
                // Wait 1/2 second for HMD to be detected.
                waitTime = 0.5;
            }
        }
        else
        {
            NetClient::GetInstance()->SetLastError("Not connected to service");

            // Wait the default amount of time for the service to start up.
        }

        // If two seconds elapse and still no HMD detected,
        if (Timer::GetSeconds() - t0 > waitTime)
        {
            return 0;
        }
    }

    // Create HMD State object
    HMDState* hmds = HMDState::CreateHMDState(CAPI_pNetClient, netInfo);
    if (!hmds)
    {
        CAPI_pNetClient->Hmd_Release(netInfo.NetId);

        NetClient::GetInstance()->SetLastError("Unable to create HMD state");
        return 0;
    }

    // Reset frame timing so that FrameTimeManager values are properly initialized in AppRendered mode.
    ovrHmd_ResetFrameTiming(hmds->pHmdDesc, 0);

    TraceHmdDesc(*hmds->pHmdDesc);

    return hmds->pHmdDesc;
}

#if !defined(HEADLESS_APP)

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_AttachToWindow(ovrHmd hmddesc, void* window,
                                                   const ovrRecti* destMirrorRect,
                                                   const ovrRecti* sourceRenderTargetRect)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    OVR_UNUSED3(destMirrorRect, sourceRenderTargetRect, hmds);

    if (!CAPI_ovrInitializeCalled)
        return ovrFalse;

#ifndef OVR_OS_MAC
    CAPI_pNetClient->Hmd_AttachToWindow(hmds->GetNetId(), window);
    hmds->pWindow = window;
#endif
#ifdef OVR_OS_WIN32
    Win32::DisplayShim::GetInstance().hWindow = (HWND)window;
#endif
#ifdef OVR_OS_MAC
    OVR_UNUSED(window);
#endif

    return ovrTrue;
}

#endif /* !defined(HEADLESS_APP) */

OVR_PUBLIC_FUNCTION(ovrHmd) ovrHmd_CreateDebug(ovrHmdType type)
{
    if (!CAPI_ovrInitializeCalled)
        return 0;

    HMDState* hmds = HMDState::CreateDebugHMDState(type);
    if (!hmds)
        return nullptr;

    return hmds->pHmdDesc;
}

OVR_PUBLIC_FUNCTION(void) ovrHmd_Destroy(ovrHmd hmddesc)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    {   // Thread checker in its own scope, to avoid access after 'delete'.
        // Essentially just checks that no other RenderAPI function is executing.
        ThreadChecker::Scope checkScope(&hmds->RenderAPIThreadChecker, "ovrHmd_Destroy");
    }    

#if !defined(HEADLESS_APP)
#ifdef OVR_OS_WIN32
    if (hmds->pWindow)
    {
        // ? ok to call
        //CAPI_pNetClient->Hmd_AttachToWindow(hmds->GetNetId(), 0);
        hmds->pWindow = 0;
        Win32::DisplayShim::GetInstance().hWindow = (HWND)0;
    }    
#endif
#endif /* !defined(HEADLESS_APP) */

    delete (HMDState*)hmddesc->Handle;
}


OVR_PUBLIC_FUNCTION(const char*) ovrHmd_GetLastError(ovrHmd hmddesc)
{
    if (!CAPI_ovrInitializeCalled)
        return "System initialize not called";

    VirtualHmdId netId = InvalidVirtualHmdId;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (hmds)
        netId = hmds->GetNetId();

    return CAPI_pNetClient->Hmd_GetLastError(netId);
}

#define OVR_VERSION_LIBOVR_PFX "libOVR:"

// Returns version string representing libOVR version. Static, so
// string remains valid for app lifespan
OVR_PUBLIC_FUNCTION(const char*) ovr_GetVersionString()
{
	static const char* version = OVR_VERSION_LIBOVR_PFX OVR_VERSION_STRING;
    return version + sizeof(OVR_VERSION_LIBOVR_PFX) - 1;
}



//-------------------------------------------------------------------------------------

// Returns capability bits that are enabled at this time; described by ovrHmdCapBits.
// Note that this value is different font ovrHmdDesc::Caps, which describes what
// capabilities are available.
OVR_PUBLIC_FUNCTION(unsigned int) ovrHmd_GetEnabledCaps(ovrHmd hmddesc)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return 0;

    return hmds->EnabledHmdCaps;
}

// Modifies capability bits described by ovrHmdCapBits that can be modified,
// such as ovrHmdCap_LowPersistance.
OVR_PUBLIC_FUNCTION(void) ovrHmd_SetEnabledCaps(ovrHmd hmddesc, unsigned int capsBits)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    hmds->SetEnabledHmdCaps(capsBits);
}


//-------------------------------------------------------------------------------------
// *** Sensor

// Sensor APIs are separated from Create & Configure for several reasons:
//  - They need custom parameters that control allocation of heavy resources
//    such as Vision tracking, which you don't want to create unless necessary.
//  - A game may want to switch some sensor settings based on user input, 
//    or at lease enable/disable features such as Vision for debugging.
//  - The same or syntactically similar sensor interface is likely to be used if we 
//    introduce controllers.
//
//  - Sensor interface functions are all Thread-safe, unlike the frame/render API
//    functions that have different rules (all frame access functions
//    must be on render thread)

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_ConfigureTracking(ovrHmd hmddesc, unsigned int supportedCaps,
                                                      unsigned int requiredCaps)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    return hmds->ConfigureTracking(supportedCaps, requiredCaps) ? ovrTrue : ovrFalse;
}


OVR_PUBLIC_FUNCTION(void) ovrHmd_RecenterPose(ovrHmd hmddesc)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    hmds->RecenterPose();
}

OVR_PUBLIC_FUNCTION(ovrTrackingState) ovrHmd_GetTrackingState(ovrHmd hmddesc, double absTime)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return GetNullTrackingState();

    return hmds->PredictedTrackingState(absTime);
}





//-------------------------------------------------------------------------------------
// *** General Setup

// Per HMD -> calculateIdealPixelSize
OVR_PUBLIC_FUNCTION(ovrSizei) ovrHmd_GetFovTextureSize(ovrHmd hmddesc, ovrEyeType eye,
                                                       ovrFovPort fov, float pixelsPerDisplayPixel)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return Sizei(0);

    return hmds->RenderState.GetFOVTextureSize(eye, fov, pixelsPerDisplayPixel);
}


//-------------------------------------------------------------------------------------


OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_ConfigureRendering(ovrHmd hmddesc,
                                                       const ovrRenderAPIConfig* apiConfig,
                                                       unsigned int distortionCaps,
                                                       const ovrFovPort eyeFovIn[2],
                                                       ovrEyeRenderDesc eyeRenderDescOut[2])
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    return hmds->ConfigureRendering(eyeRenderDescOut, eyeFovIn,
                                    apiConfig, distortionCaps);
}

OVR_PUBLIC_FUNCTION(ovrFrameTiming) ovrHmd_BeginFrame(ovrHmd hmddesc, unsigned int frameIndex)
{
    // NOTE: frameIndex == 0 is handled inside ovrHmd_BeginFrameTiming()

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return GetNullFrameTiming();

    TraceCall(frameIndex);

    // Check: Proper configure and threading state for the call.
    hmds->checkRenderingConfigured("ovrHmd_BeginFrame");
    OVR_DEBUG_LOG_COND(hmds->BeginFrameCalled, ("ovrHmd_BeginFrame called multiple times."));
    ThreadChecker::Scope checkScope(&hmds->RenderAPIThreadChecker, "ovrHmd_BeginFrame");

    hmds->BeginFrameCalled   = true;
    hmds->BeginFrameThreadId = OVR::GetCurrentThreadId();

    ovrFrameTiming timing = ovrHmd_BeginFrameTiming(hmddesc, frameIndex);

    TraceReturn(frameIndex);

    return timing;
}


// Renders textures to frame buffer
OVR_PUBLIC_FUNCTION(void) ovrHmd_EndFrame(ovrHmd hmddesc,
                                          const ovrPosef renderPose[2],
                                          const ovrTexture eyeTexture[2])
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    const ovrTexture*        eyeDepthTexture = nullptr;
    ovrPositionTimewarpDesc* posTimewarpDesc = nullptr;

    TraceCall(hmds->BeginFrameIndex);

    hmds->SubmitEyeTextures(renderPose, eyeTexture, eyeDepthTexture);

    // Debug state checks: Must be in BeginFrame, on the same thread.
    hmds->checkBeginFrameScope("ovrHmd_EndFrame");
    ThreadChecker::Scope checkScope(&hmds->RenderAPIThreadChecker, "ovrHmd_EndFrame");  
    
#if !defined(HEADLESS_APP)
    if (hmds->pRenderer)
    {
        hmds->pRenderer->SetLatencyTestColor(hmds->LatencyTestActive ? hmds->LatencyTestDrawColor : nullptr);
    }
#endif /* !defined(HEADLESS_APP) */

    ovrHmd_GetLatencyTest2DrawColor(hmddesc, nullptr); // We don't actually need to draw color, so send nullptr
    
    if (hmds->pRenderer)
    {
        hmds->pRenderer->SaveGraphicsState();

#if !defined(HEADLESS_APP)
        // See if we need to show the HSWDisplay.
        if (hmds->pHSWDisplay) // Until we know that these are valid, assume any of them can't be.
        {
            ovrHSWDisplayState hswDisplayState;
            hmds->pHSWDisplay->TickState(&hswDisplayState, true);  // This may internally call HASWarning::Display.

            if (hswDisplayState.Displayed)
            {
                hmds->pHSWDisplay->Render(ovrEye_Left, &eyeTexture[ovrEye_Left]);
                hmds->pHSWDisplay->Render(ovrEye_Right, &eyeTexture[ovrEye_Right]);
            }
        }
#endif /* !defined(HEADLESS_APP) */

        if (posTimewarpDesc)
            hmds->pRenderer->SetPositionTimewarpDesc(*posTimewarpDesc);

        hmds->pRenderer->EndFrame(hmds->BeginFrameIndex, true);
        hmds->pRenderer->RestoreGraphicsState();
    }

    // Call after present
    ovrHmd_EndFrameTiming(hmddesc);

    TraceReturn(hmds->BeginFrameIndex);

    // Out of BeginFrame
    hmds->BeginFrameThreadId = 0;
    hmds->BeginFrameCalled   = false;
    hmds->BeginFrameIndex++; // Set frame index to the next value in case 0 is passed.
}

#if !defined(HEADLESS_APP)
// Not exposed as part of public API
OVR_PUBLIC_FUNCTION(void) ovrHmd_RegisterPostDistortionCallback(ovrHmd hmddesc, PostDistortionCallback callback)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds || !hmds->pRenderer)
        return;

    hmds->pRenderer->RegisterPostDistortionCallback(callback);
}
#endif /* !defined(HEADLESS_APP) */



//-------------------------------------------------------------------------------------
// ***** Frame Timing logic

OVR_PUBLIC_FUNCTION(ovrFrameTiming) ovrHmd_GetFrameTiming(ovrHmd hmddesc, unsigned int frameIndex)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return GetNullFrameTiming();

    // If no frame index is provided,
    if (frameIndex == 0)
    {
        // Use the next one in the series.
        frameIndex = hmds->BeginFrameIndex;
    }

    return hmds->GetFrameTiming(frameIndex);
}

OVR_PUBLIC_FUNCTION(ovrFrameTiming) ovrHmd_BeginFrameTiming(ovrHmd hmddesc, unsigned int frameIndex)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return GetNullFrameTiming();

    // Check: Proper state for the call.    
    OVR_DEBUG_LOG_COND(hmds->BeginFrameTimingCalled,
                      ("ovrHmd_BeginFrameTiming called multiple times."));    
    hmds->BeginFrameTimingCalled = true;

    // If a frame index is specified,
    if (frameIndex != 0)
    {
        // Use the next one after the last BeginFrame() index.
        hmds->BeginFrameIndex = (uint32_t)frameIndex;
    }
    else
    {
        frameIndex = hmds->BeginFrameIndex;
    }

    // Update latency tester once per frame
    hmds->LatencyTestActive = hmds->ProcessLatencyTest(hmds->LatencyTestDrawColor);

    if (!hmds->BeginFrameCalled)
    {
        hmds->TimewarpTimer.CalculateTimewarpTiming(frameIndex);
    }

    return hmds->GetFrameTiming(frameIndex);
}

OVR_PUBLIC_FUNCTION(void) ovrHmd_EndFrameTiming(ovrHmd hmddesc)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    // Debug state checks: Must be in BeginFrameTiming, on the same thread.
    hmds->checkBeginFrameTimingScope("ovrHmd_EndTiming");
   // MA TBD: Correct check or not?
   // ThreadChecker::Scope checkScope(&hmds->RenderAPIThreadChecker, "ovrHmd_EndFrame");

    hmds->BeginFrameTimingCalled = false;

    hmds->endFrameRenderTiming();
}

OVR_PUBLIC_FUNCTION(void) ovrHmd_ResetFrameTiming(ovrHmd hmddesc, unsigned int frameIndex)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    OVR_UNUSED(frameIndex);

    // Clear timing-related state.
    hmds->BeginFrameIndex = frameIndex;
    hmds->TimewarpTimer.Reset();
}

OVR_PUBLIC_FUNCTION(void) ovrHmd_GetEyePoses(ovrHmd hmddesc, unsigned int frameIndex, const ovrVector3f hmdToEyeViewOffset[2],
                                             ovrPosef outEyePoses[2], ovrTrackingState* outHmdTrackingState)
{
    if (!hmdToEyeViewOffset || !outEyePoses)
    {
        OVR_ASSERT(false);
        return;
    }

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
    {
        outEyePoses[0] = ovrPosef(); outEyePoses[0].Orientation.w = 1;
        outEyePoses[1] = ovrPosef(); outEyePoses[1].Orientation.w = 1;
        if (outHmdTrackingState)
            *outHmdTrackingState = GetNullTrackingState();
        return;
    }

    // If no frame index is provided,
    if (frameIndex == 0)
    {
        // Use the next one in the series.
        frameIndex = hmds->BeginFrameIndex;
    }

    ovrTrackingState hmdTrackingState = hmds->GetMidpointPredictionTracking(frameIndex);
    TraceTrackingState(hmdTrackingState);
    ovrPosef hmdPose = hmdTrackingState.HeadPose.ThePose;

    // caller passed in a valid pointer, so copy to output
    if (outHmdTrackingState)
       *outHmdTrackingState = hmdTrackingState;

    // Currently HmdToEyeViewOffset is only a 3D vector
    // (Negate HmdToEyeViewOffset because offset is a view matrix offset and not a camera offset)
    outEyePoses[0] = Posef(hmdPose.Orientation, ((Posef)hmdPose).Apply(-((Vector3f)hmdToEyeViewOffset[0])));
    outEyePoses[1] = Posef(hmdPose.Orientation, ((Posef)hmdPose).Apply(-((Vector3f)hmdToEyeViewOffset[1])));
}

OVR_PUBLIC_FUNCTION(ovrPosef) ovrHmd_GetHmdPosePerEye(ovrHmd hmddesc, ovrEyeType eye)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
    {
        ovrPosef nullPose = ovrPosef();
        nullPose.Orientation.w = 1.0f; // Return a proper quaternion.
        return nullPose;
    }

    hmds->checkBeginFrameTimingScope("ovrHmd_GetEyePose");

    return hmds->GetEyePredictionPose(eye);
}

OVR_PUBLIC_FUNCTION(void) ovrHmd_AddDistortionTimeMeasurement(ovrHmd hmddesc, double distortionTimeSeconds)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    hmds->checkBeginFrameTimingScope("ovrHmd_GetTimewarpEyeMatrices");   

    hmds->TimewarpTimer.AddDistortionTimeMeasurement(distortionTimeSeconds);
}

OVR_PUBLIC_FUNCTION(void) ovrHmd_GetEyeTimewarpMatricesDebug(ovrHmd hmddesc, ovrEyeType eye, ovrPosef renderPose,
                                                             ovrQuatf playerTorsoMotion, ovrMatrix4f twmOut[2],
                                                             double debugTimingOffsetInSeconds)
{
    if (!twmOut)
    {
        OVR_ASSERT(false);
        return;
    }

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    // Debug checks: BeginFrame was called, on the same thread.
    hmds->checkBeginFrameTimingScope("ovrHmd_GetTimewarpEyeMatrices");   

    // TODO: Position input disabled for now
    bool calcPosition = false;
    ovrVector3f* hmdToEyeViewOffset = nullptr;

    // playerTorsoMotion can be fed in by the app to indicate player rotation,
    // i.e. renderPose is in torso space, and playerTorsoMotion says that torso space changed.
    Quatf playerTorsoMotionInv = Quatf(playerTorsoMotion).Inverted();
    Posef renderPoseTorso = (Posef)renderPose * Posef(playerTorsoMotionInv, Vector3f::Zero());
    hmds->GetTimewarpMatricesEx(eye, renderPoseTorso, calcPosition, hmdToEyeViewOffset, twmOut,
                                debugTimingOffsetInSeconds);
}


OVR_PUBLIC_FUNCTION(void) ovrHmd_GetEyeTimewarpMatrices(ovrHmd hmddesc, ovrEyeType eye, ovrPosef renderPose, ovrMatrix4f twmOut[2])
{
    if (!twmOut)
    {
        OVR_ASSERT(false);
        return;
    }

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    // Shortcut to doing orientation-only timewarp.

    hmds->GetTimewarpMatrices(eye, renderPose, twmOut);
}


OVR_PUBLIC_FUNCTION(ovrEyeRenderDesc) ovrHmd_GetRenderDesc(ovrHmd hmddesc,
                                                           ovrEyeType eyeType, ovrFovPort fov)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrEyeRenderDesc();

    return hmds->RenderState.CalcRenderDesc(eyeType, fov);
}


#if defined(OVR_CC_MSVC)
    #define OVR_OFFSET_OF(s, field) ((size_t)&((s*)0)->field)
#else
    #define OVR_OFFSET_OF(s, field) (1)
#endif


OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_CreateDistortionMeshDebug(ovrHmd hmddesc,
                                                              ovrEyeType eyeType, ovrFovPort fov,
                                                              unsigned int distortionCaps,
                                                              ovrDistortionMesh *meshData,
												              float debugEyeReliefOverrideInMetres)
{
    if (!meshData)
    {
        OVR_ASSERT(false);
        return ovrFalse;
    }

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    return hmds->CreateDistortionMesh(eyeType, fov,
                                      distortionCaps,
                                      meshData,
                                      debugEyeReliefOverrideInMetres)
           ? ovrTrue : ovrFalse;
}


OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_CreateDistortionMesh(ovrHmd hmddesc,
                                                         ovrEyeType eyeType, ovrFovPort fov,
                                                         unsigned int distortionCaps,
                                                         ovrDistortionMesh *meshData)
{
    if (!meshData)
    {
        OVR_ASSERT(false);
        return ovrFalse;
    }

    return ovrHmd_CreateDistortionMeshDebug(hmddesc, eyeType, fov, distortionCaps, meshData, 0);
}



// Frees distortion mesh allocated by ovrHmd_GenerateDistortionMesh. meshData elements
// are set to null and 0s after the call.
OVR_PUBLIC_FUNCTION(void) ovrHmd_DestroyDistortionMesh(ovrDistortionMesh* meshData)
{
    if (!meshData)
    {
        OVR_ASSERT(false);
        return;
    }

    if (meshData->pVertexData)
        DistortionMeshDestroy((DistortionMeshVertexData*)meshData->pVertexData,
                              meshData->pIndexData);
    meshData->pVertexData = 0;
    meshData->pIndexData  = 0;
    meshData->VertexCount = 0;
    meshData->IndexCount  = 0;
}



// Computes updated 'uvScaleOffsetOut' to be used with a distortion if render target size or
// viewport changes after the fact. This can be used to adjust render size every frame, if desired.
OVR_PUBLIC_FUNCTION(void) ovrHmd_GetRenderScaleAndOffset(ovrFovPort fov,
                                                         ovrSizei textureSize, ovrRecti renderViewport,
                                                         ovrVector2f uvScaleOffsetOut[2] )
{        
    if (!uvScaleOffsetOut)
    {
        OVR_ASSERT(false);
        return;
    }

    // Find the mapping from TanAngle space to target NDC space.
    ScaleAndOffset2D  eyeToSourceNDC = CreateNDCScaleAndOffsetFromFov(fov);
    // Find the mapping from TanAngle space to textureUV space.
    ScaleAndOffset2D  eyeToSourceUV  = CreateUVScaleAndOffsetfromNDCScaleandOffset(
                                         eyeToSourceNDC,
                                         renderViewport, textureSize );

    if (uvScaleOffsetOut)
    {
        uvScaleOffsetOut[0] = eyeToSourceUV.Scale;
        uvScaleOffsetOut[1] = eyeToSourceUV.Offset;
    }
}


//-------------------------------------------------------------------------------------
// ***** Latency Test interface

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_GetLatencyTestDrawColor(ovrHmd hmddesc, unsigned char rgbColorOut[3])
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    if (rgbColorOut)
    {
        rgbColorOut[0] = hmds->LatencyTestDrawColor[0];
        rgbColorOut[1] = hmds->LatencyTestDrawColor[1];
        rgbColorOut[2] = hmds->LatencyTestDrawColor[2];
    }

    return hmds->LatencyTestActive;
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_ProcessLatencyTest(ovrHmd hmddesc, unsigned char rgbColorOut[3])
{
    OVR_UNUSED(hmddesc);

    if (!rgbColorOut)
    {
        OVR_ASSERT(false);
        return ovrFalse;
    }

    return NetClient::GetInstance()->LatencyUtil_ProcessInputs(Timer::GetSeconds(), rgbColorOut);
}

OVR_PUBLIC_FUNCTION(const char*) ovrHmd_GetLatencyTestResult(ovrHmd hmddesc)
{
    OVR_UNUSED(hmddesc);

    return NetClient::GetInstance()->LatencyUtil_GetResultsString();
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_GetLatencyTest2DrawColor(ovrHmd hmddesc, unsigned char rgbColorOut[3])
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    // TBD: Move directly into renderer
    bool dk2LatencyTest = (hmds->EnabledHmdCaps & ovrHmdCap_DynamicPrediction) != 0;
    if (dk2LatencyTest)
    {
        hmds->LatencyTest2DrawColor[0] = hmds->ScreenLatencyTracker.GetNextDrawColor();
        hmds->LatencyTest2DrawColor[1] = hmds->ScreenLatencyTracker.IsLatencyTimingAvailable() ? 255 : 0;
        hmds->LatencyTest2DrawColor[2] = hmds->ScreenLatencyTracker.IsLatencyTimingAvailable() ? 0 : 255;

        if (rgbColorOut)
        {
            rgbColorOut[0] = hmds->LatencyTest2DrawColor[0];
            rgbColorOut[1] = hmds->LatencyTest2DrawColor[1];
            rgbColorOut[2] = hmds->LatencyTest2DrawColor[2];
        }

#if !defined(HEADLESS_APP)
        if (hmds->pRenderer) {
            hmds->pRenderer->SetLatencyTest2Color(hmds->LatencyTest2DrawColor);
        }
#endif /* !defined(HEADLESS_APP) */
    }
    else
    {
#if !defined(HEADLESS_APP)
        if (hmds->pRenderer) {
            hmds->pRenderer->SetLatencyTest2Color(nullptr);
        }
#endif /* !defined(HEADLESS_APP) */
    }

    return dk2LatencyTest ? ovrTrue : ovrFalse;
}


OVR_PUBLIC_FUNCTION(double) ovrHmd_GetMeasuredLatencyTest2(ovrHmd hmddesc)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return 0.;

    double latencyPostPresent = 0.;
    hmds->ScreenLatencyTracker.GetVsyncToScanout(latencyPostPresent);
    return latencyPostPresent;
}


//-------------------------------------------------------------------------------------
// ***** Health and Safety Warning Display interface
//

#if !defined(HEADLESS_APP)

OVR_PUBLIC_FUNCTION(void) ovrHmd_GetHSWDisplayState(ovrHmd hmddesc, ovrHSWDisplayState *hswDisplayState)
{
    if (!hswDisplayState)
    {
        OVR_ASSERT(false);
        return;
    }

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return;

    OVR::CAPI::HSWDisplay* pHSWDisplay = hmds->pHSWDisplay;

    if (pHSWDisplay)
        pHSWDisplay->TickState(hswDisplayState); // This may internally call HSWDisplay::Display.
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_DismissHSWDisplay(ovrHmd hmddesc)
{
    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds)
        return ovrFalse;

    OVR::CAPI::HSWDisplay* pHSWDisplay = hmds->pHSWDisplay;

    if (pHSWDisplay && pHSWDisplay->Dismiss())
        return ovrTrue;

    return ovrFalse;
}

#endif /* !defined(HEADLESS_APP) */

// -----------------------------------------------------------------------------------
// ***** Property Access

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_GetBool(ovrHmd hmddesc,
                                            const char* propertyName,
                                            ovrBool defaultVal)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    bool success = false;
    if (hmds)
    {
        success = hmds->getBoolValue(propertyName, (defaultVal != ovrFalse));
    }
    else
    {
        success = NetClient::GetInstance()->GetBoolValue(InvalidVirtualHmdId, propertyName, (defaultVal != ovrFalse));
    }
    return success ? ovrTrue : ovrFalse;
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_SetBool(ovrHmd hmddesc,
                                            const char* propertyName,
                                            ovrBool value)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    bool retval = false;
    if (hmds)
    {
        retval = hmds->setBoolValue(propertyName, value != ovrFalse);
    }
    else
    {
        retval = NetClient::GetInstance()->SetBoolValue(InvalidVirtualHmdId, propertyName, (value != ovrFalse));
    }
    return retval ? ovrTrue : ovrFalse;
}

OVR_PUBLIC_FUNCTION(int) ovrHmd_GetInt(ovrHmd hmddesc,
                                       const char* propertyName,
                                       int defaultVal)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (hmds)
    {
        return hmds->getIntValue(propertyName, defaultVal);
    }
    else
    {
        return NetClient::GetInstance()->GetIntValue(InvalidVirtualHmdId, propertyName, defaultVal);
    }
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_SetInt(ovrHmd hmddesc,
                                           const char* propertyName,
                                           int value)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    bool success = false;
    if (hmds)
    {
        success = hmds->setIntValue(propertyName, value);
    }
    else
    {
        success = NetClient::GetInstance()->SetIntValue(InvalidVirtualHmdId, propertyName, value);
    }
    return success ? ovrTrue : ovrFalse;
}

OVR_PUBLIC_FUNCTION(float) ovrHmd_GetFloat(ovrHmd hmddesc,
                                           const char* propertyName,
                                           float defaultVal)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (hmds)
    {
        return hmds->getFloatValue(propertyName, defaultVal);
    }
    else
    {
        return (float)NetClient::GetInstance()->GetNumberValue(InvalidVirtualHmdId, propertyName, defaultVal);
    }
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_SetFloat(ovrHmd hmddesc,
                                             const char* propertyName,
                                             float value)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    bool success = false;
    if (hmds)
    {
        success = hmds->setFloatValue(propertyName, value);
    }
    else
    {
        success = NetClient::GetInstance()->SetNumberValue(InvalidVirtualHmdId, propertyName, value);
    }
    return success ? ovrTrue : ovrFalse;
}

OVR_PUBLIC_FUNCTION(unsigned int) ovrHmd_GetFloatArray(ovrHmd hmddesc,
                                                       const char* propertyName,
                                                       float values[],
                                                       unsigned int arraySize)
{
    OVR_ASSERT(propertyName != nullptr && values != nullptr);
    if (!propertyName || !values)
        return 0;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds) return 0;

    return hmds->getFloatArray(propertyName, values, arraySize);
}

// Modify float[] property; false if property doesn't exist or is readonly.
OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_SetFloatArray(ovrHmd hmddesc,
                                                  const char* propertyName,
                                                  float values[],
                                                  unsigned int arraySize)
{
    OVR_ASSERT(propertyName != nullptr && values != nullptr);
    if (!propertyName || !values)
        return ovrFalse;

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (!hmds) return ovrFalse;

    return hmds->setFloatArray(propertyName, values, arraySize) ? ovrTrue : ovrFalse;
}

OVR_PUBLIC_FUNCTION(const char*) ovrHmd_GetString(ovrHmd hmddesc,
                                                  const char* propertyName,
                                                  const char* defaultVal)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return "";

    // Replace null default with empty string
    if (!defaultVal)
        defaultVal = "";

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    if (hmds)
    {
        return hmds->getString(propertyName, defaultVal);
    }
    else
    {
        return NetClient::GetInstance()->GetStringValue(InvalidVirtualHmdId, propertyName, defaultVal);
    }
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_SetString(ovrHmd hmddesc,
                                              const char* propertyName,
                                              const char* value)
{
    OVR_ASSERT(propertyName != nullptr);
    if (!propertyName)
        return ovrFalse;

    // Replace null value with empty string
    if (!value)
        value = "";

    HMDState* hmds = GetHMDStateFromOvrHmd(hmddesc);
    bool success = false;
    if (hmds)
    {
        success = hmds->setString(propertyName, value);
    }
    else
    {
        success = NetClient::GetInstance()->SetStringValue(InvalidVirtualHmdId, propertyName, value);
    }
    return success ? ovrTrue : ovrFalse;
}


// -----------------------------------------------------------------------------------
// ***** Logging

// make sure OVR_Log.h's enum matches CAPI's
static_assert(
    ((ovrLogLevel_Debug == ovrLogLevel(LogLevel_Debug)) &&
    (ovrLogLevel_Info   == ovrLogLevel(LogLevel_Info)) &&
    (ovrLogLevel_Error  == ovrLogLevel(LogLevel_Error))),
    "mismatched LogLevel enums"
);

#define OVR_TRACEMSG_MAX_LEN 1024 /* in chars */

OVR_PUBLIC_FUNCTION(int) ovr_TraceMessage(int level, const char* message)
{
    OVR_ASSERT(message != nullptr);
    if (!message)
        return -1;

    // Keep traced messages to some reasonable maximum length
    int len = (int)strnlen(message, OVR_TRACEMSG_MAX_LEN);
    if (len >= OVR_TRACEMSG_MAX_LEN)
        return -1;

    switch (level)
    {
    case ovrLogLevel_Debug:
        TraceLogDebug(message);
        break;
    case ovrLogLevel_Info:
    default:
        TraceLogInfo(message);
        break;
    case ovrLogLevel_Error:
        TraceLogError(message);
        break;
    }

    return len;
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_StartPerfLog(ovrHmd hmddesc, const char* fileName, const char* userData1)
{
    // DEPRECATED
    OVR_UNUSED3(hmddesc, fileName, userData1);
    return ovrFalse;
}

OVR_PUBLIC_FUNCTION(ovrBool) ovrHmd_StopPerfLog(ovrHmd hmddesc)
{
    // DEPRECATED
    OVR_UNUSED(hmddesc);
    return ovrFalse;
}