Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: third_party/openvr/src/src/openvr_api_public.cpp

Issue 2754253003: Add OpenVR SDK to third_party (Closed)
Patch Set: rebase and fix copyright Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //========= Copyright Valve Corporation ============//
2 #define VR_API_EXPORT 1
3 #include "openvr.h"
4 #include "ivrclientcore.h"
5 #include "pathtools_public.h"
6 #include "sharedlibtools_public.h"
7 #include "envvartools_public.h"
8 #include "hmderrors_public.h"
9 #include "vrpathregistry_public.h"
10
11 using vr::EVRInitError;
12 using vr::IVRSystem;
13 using vr::IVRClientCore;
14 using vr::VRInitError_None;
15
16 namespace vr
17 {
18
19 static void *g_pVRModule = NULL;
20 static IVRClientCore *g_pHmdSystem = NULL;
21
22
23 typedef void* (*VRClientCoreFactoryFn)(const char *pInterfaceName, int *pReturnC ode);
24
25 static uint32_t g_nVRToken = 0;
26
27 uint32_t VR_GetInitToken()
28 {
29 return g_nVRToken;
30 }
31
32 EVRInitError VR_LoadHmdSystemInternal();
33 void CleanupInternalInterfaces();
34
35
36 uint32_t VR_InitInternal( EVRInitError *peError, vr::EVRApplicationType eApplica tionType )
37 {
38 EVRInitError err = VR_LoadHmdSystemInternal();
39 if (err != vr::VRInitError_None)
40 {
41 SharedLib_Unload(g_pVRModule);
42 g_pHmdSystem = NULL;
43 g_pVRModule = NULL;
44
45 if (peError)
46 *peError = err;
47
48 return 0;
49 }
50
51 err = g_pHmdSystem->Init(eApplicationType);
52 if (err != VRInitError_None)
53 {
54 SharedLib_Unload(g_pVRModule);
55 g_pHmdSystem = NULL;
56 g_pVRModule = NULL;
57
58 if (peError)
59 *peError = err;
60
61 return 0;
62 }
63
64 if (peError)
65 *peError = VRInitError_None;
66
67 return ++g_nVRToken;
68 }
69
70 void VR_ShutdownInternal()
71 {
72 if (g_pHmdSystem)
73 {
74 g_pHmdSystem->Cleanup();
75 g_pHmdSystem = NULL;
76 }
77 if (g_pVRModule)
78 {
79 SharedLib_Unload(g_pVRModule);
80 g_pVRModule = NULL;
81 }
82
83 #if !defined( VR_API_PUBLIC )
84 CleanupInternalInterfaces();
85 #endif
86
87 ++g_nVRToken;
88 }
89
90 EVRInitError VR_LoadHmdSystemInternal()
91 {
92 std::string sRuntimePath, sConfigPath, sLogPath;
93
94 bool bReadPathRegistry = CVRPathRegistry_Public::GetPaths( &sRuntimePath , &sConfigPath, &sLogPath, NULL, NULL );
95 if( !bReadPathRegistry )
96 {
97 return vr::VRInitError_Init_PathRegistryNotFound;
98 }
99
100 // figure out where we're going to look for vrclient.dll
101 // see if the specified path actually exists.
102 if( !Path_IsDirectory( sRuntimePath ) )
103 {
104 return vr::VRInitError_Init_InstallationNotFound;
105 }
106
107 // Because we don't have a way to select debug vs. release yet we'll jus t
108 // use debug if it's there
109 #if defined( LINUX64 )
110 std::string sTestPath = Path_Join( sRuntimePath, "bin", PLATSUBDIR );
111 #else
112 std::string sTestPath = Path_Join( sRuntimePath, "bin" );
113 #endif
114 if( !Path_IsDirectory( sTestPath ) )
115 {
116 return vr::VRInitError_Init_InstallationCorrupt;
117 }
118
119 #if defined( WIN64 )
120 std::string sDLLPath = Path_Join( sTestPath, "vrclient_x64" DYNAMIC_LIB_ EXT );
121 #else
122 std::string sDLLPath = Path_Join( sTestPath, "vrclient" DYNAMIC_LIB_EXT );
123 #endif
124
125 // only look in the override
126 void *pMod = SharedLib_Load( sDLLPath.c_str() );
127 // nothing more to do if we can't load the DLL
128 if( !pMod )
129 {
130 return vr::VRInitError_Init_VRClientDLLNotFound;
131 }
132
133 VRClientCoreFactoryFn fnFactory = ( VRClientCoreFactoryFn )( SharedLib_G etFunction( pMod, "VRClientCoreFactory" ) );
134 if( !fnFactory )
135 {
136 SharedLib_Unload( pMod );
137 return vr::VRInitError_Init_FactoryNotFound;
138 }
139
140 int nReturnCode = 0;
141 g_pHmdSystem = static_cast< IVRClientCore * > ( fnFactory( vr::IVRClient Core_Version, &nReturnCode ) );
142 if( !g_pHmdSystem )
143 {
144 SharedLib_Unload( pMod );
145 return vr::VRInitError_Init_InterfaceNotFound;
146 }
147
148 g_pVRModule = pMod;
149 return VRInitError_None;
150 }
151
152
153 void *VR_GetGenericInterface(const char *pchInterfaceVersion, EVRInitError *peEr ror)
154 {
155 if (!g_pHmdSystem)
156 {
157 if (peError)
158 *peError = vr::VRInitError_Init_NotInitialized;
159 return NULL;
160 }
161
162 return g_pHmdSystem->GetGenericInterface(pchInterfaceVersion, peError);
163 }
164
165 bool VR_IsInterfaceVersionValid(const char *pchInterfaceVersion)
166 {
167 if (!g_pHmdSystem)
168 {
169 return false;
170 }
171
172 return g_pHmdSystem->IsInterfaceVersionValid(pchInterfaceVersion) == VRI nitError_None;
173 }
174
175 bool VR_IsHmdPresent()
176 {
177 if( g_pHmdSystem )
178 {
179 // if we're already initialized, just call through
180 return g_pHmdSystem->BIsHmdPresent();
181 }
182 else
183 {
184 // otherwise we need to do a bit more work
185 EVRInitError err = VR_LoadHmdSystemInternal();
186 if( err != VRInitError_None )
187 return false;
188
189 bool bHasHmd = g_pHmdSystem->BIsHmdPresent();
190
191 g_pHmdSystem = NULL;
192 SharedLib_Unload( g_pVRModule );
193 g_pVRModule = NULL;
194
195 return bHasHmd;
196 }
197 }
198
199 /** Returns true if the OpenVR runtime is installed. */
200 bool VR_IsRuntimeInstalled()
201 {
202 if( g_pHmdSystem )
203 {
204 // if we're already initialized, OpenVR is obviously installed
205 return true;
206 }
207 else
208 {
209 // otherwise we need to do a bit more work
210 std::string sRuntimePath, sConfigPath, sLogPath;
211
212 bool bReadPathRegistry = CVRPathRegistry_Public::GetPaths( &sRun timePath, &sConfigPath, &sLogPath, NULL, NULL );
213 if( !bReadPathRegistry )
214 {
215 return false;
216 }
217
218 // figure out where we're going to look for vrclient.dll
219 // see if the specified path actually exists.
220 if( !Path_IsDirectory( sRuntimePath ) )
221 {
222 return false;
223 }
224
225 // the installation may be corrupt in some way, but it certainly looks installed
226 return true;
227 }
228 }
229
230
231 /** Returns where OpenVR runtime is installed. */
232 const char *VR_RuntimePath()
233 {
234 // otherwise we need to do a bit more work
235 static std::string sRuntimePath;
236 std::string sConfigPath, sLogPath;
237
238 bool bReadPathRegistry = CVRPathRegistry_Public::GetPaths( &sRuntimePath , &sConfigPath, &sLogPath, NULL, NULL );
239 if ( !bReadPathRegistry )
240 {
241 return nullptr;
242 }
243
244 // figure out where we're going to look for vrclient.dll
245 // see if the specified path actually exists.
246 if ( !Path_IsDirectory( sRuntimePath ) )
247 {
248 return nullptr;
249 }
250
251 return sRuntimePath.c_str();
252 }
253
254
255 /** Returns the symbol version of an HMD error. */
256 const char *VR_GetVRInitErrorAsSymbol( EVRInitError error )
257 {
258 if( g_pHmdSystem )
259 return g_pHmdSystem->GetIDForVRInitError( error );
260 else
261 return GetIDForVRInitError( error );
262 }
263
264
265 /** Returns the english string version of an HMD error. */
266 const char *VR_GetVRInitErrorAsEnglishDescription( EVRInitError error )
267 {
268 if ( g_pHmdSystem )
269 return g_pHmdSystem->GetEnglishStringForHmdError( error );
270 else
271 return GetEnglishStringForHmdError( error );
272 }
273
274
275 VR_INTERFACE const char *VR_CALLTYPE VR_GetStringForHmdError( vr::EVRInitError e rror );
276
277 /** Returns the english string version of an HMD error. */
278 const char *VR_GetStringForHmdError( EVRInitError error )
279 {
280 return VR_GetVRInitErrorAsEnglishDescription( error );
281 }
282
283 }
284
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698