-
Notifications
You must be signed in to change notification settings - Fork 57
/
driver_osvr.cpp
150 lines (119 loc) · 3.49 KB
/
driver_osvr.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
/** @file
@brief OSVR driver provider
@date 2015
@author
Sensics, Inc.
<http://sensics.com>
*/
// Copyright 2015 Sensics, Inc.
//
// All rights reserved.
//
// (Final version intended to be licensed under
// the Apache License, Version 2.0)
// Internal Includes
#include "ihmddriverprovider.h"
#include "ihmddriver.h"
#include "steamvr.h"
#include "osvr_compiler_detection.h"
#include "osvr_hmd.h"
#include "stringhasprefix.h"
#include "osvr_dll_export.h"
// Library/third-party includes
#include <osvr/ClientKit/Context.h>
#include <osvr/ClientKit/Interface.h>
// Standard includes
#include <vector>
#include <cstring>
class CDriver_OSVR : public vr::IHmdDriverProvider
{
public:
/**
* Initializes the driver.
*
* This is called when the driver is first loaded.
*
* @param user_config_dir the absoluate path of the directory where the
* driver should store any user configuration files.
* @param driver_install_dir the absolute path of the driver's root
* directory.
*
* If Init() returns anything other than \c HmdError_None the driver will be
* unloaded.
*
* @returns HmdError_None on success.
*/
virtual vr::HmdError Init(const char* pchUserConfigDir, const char* pchDriverInstallDir) OSVR_OVERRIDE;
/**
* Performs any cleanup prior to the driver being unloaded.
*/
virtual void Cleanup() OSVR_OVERRIDE;
/**
* Returns the number of detect HMDs.
*/
virtual uint32_t GetHmdCount() OSVR_OVERRIDE;
/**
* Returns a single HMD by its index.
*
* @param index the index of the HMD to return.
*/
virtual vr::IHmdDriver* GetHmd(uint32_t index) OSVR_OVERRIDE;
/**
* Returns a single HMD by its name.
*
* @param hmd_id the C string name of the HMD.
*/
virtual vr::IHmdDriver* FindHmd(const char* hmd_id) OSVR_OVERRIDE;
private:
std::vector<std::unique_ptr<OSVRHmd>> hmds_;
std::unique_ptr<osvr::clientkit::ClientContext> context_;
std::unique_ptr<ClientMainloopThread> client_;
};
static CDriver_OSVR g_driverOSVR;
vr::HmdError CDriver_OSVR::Init(const char* pchUserConfigDir, const char* pchDriverInstallDir)
{
context_ = std::make_unique<osvr::clientkit::ClientContext>("com.osvr.SteamVR");
client_ = std::make_unique<ClientMainloopThread>(*context_);
const std::string display_description = context_->getStringParameter("/display");
hmds_.emplace_back(std::make_unique<OSVRHmd>(display_description, *(context_.get())));
client_->start();
return vr::HmdError_None;
}
void CDriver_OSVR::Cleanup()
{
client_.reset();
hmds_.clear();
context_.reset();
}
uint32_t CDriver_OSVR::GetHmdCount()
{
return hmds_.size();
}
vr::IHmdDriver* CDriver_OSVR::GetHmd(uint32_t index)
{
if (index >= hmds_.size())
return NULL;
return hmds_[index].get();
}
vr::IHmdDriver* CDriver_OSVR::FindHmd(const char* hmd_id)
{
for (auto& hmd : hmds_) {
if (0 == std::strcmp(hmd_id, hmd->GetId()))
return hmd.get();
}
return NULL;
}
static const char* IHmdDriverProvider_Prefix = "IHmdDriverProvider_";
OSVR_DLL_EXPORT void* HmdDriverFactory(const char* pInterfaceName, int* pReturnCode)
{
if (!StringHasPrefix(pInterfaceName, IHmdDriverProvider_Prefix)) {
*pReturnCode = vr::HmdError_Init_InvalidInterface;
return NULL;
}
if (0 != strcmp(vr::IHmdDriverProvider_Version, pInterfaceName)) {
if (pReturnCode)
*pReturnCode = vr::HmdError_Init_InterfaceNotFound;
return NULL;
}
return &g_driverOSVR;
}