Line data Source code
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 *pReturnCode);
24 :
25 : static uint32_t g_nVRToken = 0;
26 :
27 0 : uint32_t VR_GetInitToken()
28 : {
29 0 : return g_nVRToken;
30 : }
31 :
32 : EVRInitError VR_LoadHmdSystemInternal();
33 : void CleanupInternalInterfaces();
34 :
35 :
36 0 : uint32_t VR_InitInternal( EVRInitError *peError, vr::EVRApplicationType eApplicationType )
37 : {
38 0 : EVRInitError err = VR_LoadHmdSystemInternal();
39 0 : if (err != vr::VRInitError_None)
40 : {
41 0 : SharedLib_Unload(g_pVRModule);
42 0 : g_pHmdSystem = NULL;
43 0 : g_pVRModule = NULL;
44 :
45 0 : if (peError)
46 0 : *peError = err;
47 :
48 0 : return 0;
49 : }
50 :
51 0 : err = g_pHmdSystem->Init(eApplicationType);
52 0 : if (err != VRInitError_None)
53 : {
54 0 : SharedLib_Unload(g_pVRModule);
55 0 : g_pHmdSystem = NULL;
56 0 : g_pVRModule = NULL;
57 :
58 0 : if (peError)
59 0 : *peError = err;
60 :
61 0 : return 0;
62 : }
63 :
64 0 : if (peError)
65 0 : *peError = VRInitError_None;
66 :
67 0 : return ++g_nVRToken;
68 : }
69 :
70 0 : void VR_ShutdownInternal()
71 : {
72 0 : if (g_pHmdSystem)
73 : {
74 0 : g_pHmdSystem->Cleanup();
75 0 : g_pHmdSystem = NULL;
76 : }
77 0 : if (g_pVRModule)
78 : {
79 0 : SharedLib_Unload(g_pVRModule);
80 0 : g_pVRModule = NULL;
81 : }
82 :
83 : #if !defined( VR_API_PUBLIC )
84 : CleanupInternalInterfaces();
85 : #endif
86 :
87 0 : ++g_nVRToken;
88 0 : }
89 :
90 0 : EVRInitError VR_LoadHmdSystemInternal()
91 : {
92 0 : std::string sRuntimePath, sConfigPath, sLogPath;
93 :
94 0 : bool bReadPathRegistry = CVRPathRegistry_Public::GetPaths( &sRuntimePath, &sConfigPath, &sLogPath, NULL, NULL );
95 0 : if( !bReadPathRegistry )
96 : {
97 0 : 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 0 : if( !Path_IsDirectory( sRuntimePath ) )
103 : {
104 0 : return vr::VRInitError_Init_InstallationNotFound;
105 : }
106 :
107 : // Because we don't have a way to select debug vs. release yet we'll just
108 : // use debug if it's there
109 : #if defined( LINUX64 )
110 0 : std::string sTestPath = Path_Join( sRuntimePath, "bin", PLATSUBDIR );
111 : #else
112 : std::string sTestPath = Path_Join( sRuntimePath, "bin" );
113 : #endif
114 0 : if( !Path_IsDirectory( sTestPath ) )
115 : {
116 0 : 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 0 : std::string sDLLPath = Path_Join( sTestPath, "vrclient" DYNAMIC_LIB_EXT );
123 : #endif
124 :
125 : // only look in the override
126 0 : void *pMod = SharedLib_Load( sDLLPath.c_str() );
127 : // nothing more to do if we can't load the DLL
128 0 : if( !pMod )
129 : {
130 0 : return vr::VRInitError_Init_VRClientDLLNotFound;
131 : }
132 :
133 0 : VRClientCoreFactoryFn fnFactory = ( VRClientCoreFactoryFn )( SharedLib_GetFunction( pMod, "VRClientCoreFactory" ) );
134 0 : if( !fnFactory )
135 : {
136 0 : SharedLib_Unload( pMod );
137 0 : return vr::VRInitError_Init_FactoryNotFound;
138 : }
139 :
140 0 : int nReturnCode = 0;
141 0 : g_pHmdSystem = static_cast< IVRClientCore * > ( fnFactory( vr::IVRClientCore_Version, &nReturnCode ) );
142 0 : if( !g_pHmdSystem )
143 : {
144 0 : SharedLib_Unload( pMod );
145 0 : return vr::VRInitError_Init_InterfaceNotFound;
146 : }
147 :
148 0 : g_pVRModule = pMod;
149 0 : return VRInitError_None;
150 : }
151 :
152 :
153 0 : void *VR_GetGenericInterface(const char *pchInterfaceVersion, EVRInitError *peError)
154 : {
155 0 : if (!g_pHmdSystem)
156 : {
157 0 : if (peError)
158 0 : *peError = vr::VRInitError_Init_NotInitialized;
159 0 : return NULL;
160 : }
161 :
162 0 : return g_pHmdSystem->GetGenericInterface(pchInterfaceVersion, peError);
163 : }
164 :
165 0 : bool VR_IsInterfaceVersionValid(const char *pchInterfaceVersion)
166 : {
167 0 : if (!g_pHmdSystem)
168 : {
169 0 : return false;
170 : }
171 :
172 0 : return g_pHmdSystem->IsInterfaceVersionValid(pchInterfaceVersion) == VRInitError_None;
173 : }
174 :
175 0 : bool VR_IsHmdPresent()
176 : {
177 0 : if( g_pHmdSystem )
178 : {
179 : // if we're already initialized, just call through
180 0 : return g_pHmdSystem->BIsHmdPresent();
181 : }
182 : else
183 : {
184 : // otherwise we need to do a bit more work
185 0 : EVRInitError err = VR_LoadHmdSystemInternal();
186 0 : if( err != VRInitError_None )
187 0 : return false;
188 :
189 0 : bool bHasHmd = g_pHmdSystem->BIsHmdPresent();
190 :
191 0 : g_pHmdSystem = NULL;
192 0 : SharedLib_Unload( g_pVRModule );
193 0 : g_pVRModule = NULL;
194 :
195 0 : return bHasHmd;
196 : }
197 : }
198 :
199 : /** Returns true if the OpenVR runtime is installed. */
200 0 : bool VR_IsRuntimeInstalled()
201 : {
202 0 : if( g_pHmdSystem )
203 : {
204 : // if we're already initialized, OpenVR is obviously installed
205 0 : return true;
206 : }
207 : else
208 : {
209 : // otherwise we need to do a bit more work
210 0 : std::string sRuntimePath, sConfigPath, sLogPath;
211 :
212 0 : bool bReadPathRegistry = CVRPathRegistry_Public::GetPaths( &sRuntimePath, &sConfigPath, &sLogPath, NULL, NULL );
213 0 : if( !bReadPathRegistry )
214 : {
215 0 : 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 0 : if( !Path_IsDirectory( sRuntimePath ) )
221 : {
222 0 : return false;
223 : }
224 :
225 : // the installation may be corrupt in some way, but it certainly looks installed
226 0 : return true;
227 : }
228 : }
229 :
230 :
231 : /** Returns where OpenVR runtime is installed. */
232 0 : const char *VR_RuntimePath()
233 : {
234 : // otherwise we need to do a bit more work
235 0 : static std::string sRuntimePath;
236 0 : std::string sConfigPath, sLogPath;
237 :
238 0 : bool bReadPathRegistry = CVRPathRegistry_Public::GetPaths( &sRuntimePath, &sConfigPath, &sLogPath, NULL, NULL );
239 0 : if ( !bReadPathRegistry )
240 : {
241 0 : 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 0 : if ( !Path_IsDirectory( sRuntimePath ) )
247 : {
248 0 : return nullptr;
249 : }
250 :
251 0 : return sRuntimePath.c_str();
252 : }
253 :
254 :
255 : /** Returns the symbol version of an HMD error. */
256 0 : const char *VR_GetVRInitErrorAsSymbol( EVRInitError error )
257 : {
258 0 : if( g_pHmdSystem )
259 0 : return g_pHmdSystem->GetIDForVRInitError( error );
260 : else
261 0 : return GetIDForVRInitError( error );
262 : }
263 :
264 :
265 : /** Returns the english string version of an HMD error. */
266 0 : const char *VR_GetVRInitErrorAsEnglishDescription( EVRInitError error )
267 : {
268 0 : if ( g_pHmdSystem )
269 0 : return g_pHmdSystem->GetEnglishStringForHmdError( error );
270 : else
271 0 : return GetEnglishStringForHmdError( error );
272 : }
273 :
274 :
275 : VR_INTERFACE const char *VR_CALLTYPE VR_GetStringForHmdError( vr::EVRInitError error );
276 :
277 : /** Returns the english string version of an HMD error. */
278 0 : const char *VR_GetStringForHmdError( EVRInitError error )
279 : {
280 0 : return VR_GetVRInitErrorAsEnglishDescription( error );
281 : }
282 :
283 : }
284 :
|