hmd_openvr.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "hmd_openvr.h"
  6. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-variable")
  7. #include <openvr/openvr_capi.h>
  8. namespace bgfx
  9. {
  10. #if BX_PLATFORM_WINDOWS
  11. # define VR_CALLTYPE __cdecl
  12. #else
  13. # define VR_CALLTYPE
  14. #endif
  15. typedef uint32_t (VR_CALLTYPE *PFN_VR_INITINTERNAL)(EVRInitError* peError, EVRApplicationType eType);
  16. typedef void (VR_CALLTYPE *PFN_VR_SHUTDOWNINTERNAL)();
  17. typedef bool (VR_CALLTYPE *PFN_VR_ISHMDPRESENT)();
  18. typedef void* (VR_CALLTYPE *PFN_VR_GETGENERICINTERFACE)(const char* pchInterfaceVersion, EVRInitError* peError);
  19. typedef bool (VR_CALLTYPE *PFN_VR_ISRUNTIMEINSTALLED)();
  20. typedef bool (VR_CALLTYPE *PFN_VR_ISINTERFACEVERSIONVALID)(const char *pchInterfaceVersion);
  21. typedef uint32_t (VR_CALLTYPE *PFN_VR_GETINITTOKEN)();
  22. typedef const char* (VR_CALLTYPE *PFN_VR_GETVRINITERRORASSYMBOL)(EVRInitError error);
  23. typedef const char* (VR_CALLTYPE *PFN_VR_GETVRINITERRORASENGLISHDESCRIPTION)(EVRInitError error);
  24. PFN_VR_INITINTERNAL VR_InitInternal;
  25. PFN_VR_SHUTDOWNINTERNAL VR_ShutdownInternal;
  26. PFN_VR_ISHMDPRESENT VR_IsHmdPresent;
  27. PFN_VR_GETGENERICINTERFACE VR_GetGenericInterface;
  28. PFN_VR_ISRUNTIMEINSTALLED VR_IsRuntimeInstalled;
  29. PFN_VR_ISINTERFACEVERSIONVALID VR_IsInterfaceVersionValid;
  30. PFN_VR_GETINITTOKEN VR_GetInitToken;
  31. PFN_VR_GETVRINITERRORASSYMBOL VR_GetVRInitErrorAsSymbol;
  32. PFN_VR_GETVRINITERRORASENGLISHDESCRIPTION VR_GetVRInitErrorAsEnglishDescription;
  33. void* loadOpenVR()
  34. {
  35. void* openvrdll = bx::dlopen(
  36. #if BX_PLATFORM_LINUX
  37. "libopenvr_api.so"
  38. #elif BX_PLATFORM_OSX
  39. "libopenvr_api.dylib"
  40. #else
  41. "openvr_api.dll"
  42. #endif // BX_PLATFORM_*
  43. );
  44. if (NULL != openvrdll)
  45. {
  46. VR_InitInternal = (PFN_VR_INITINTERNAL )bx::dlsym(openvrdll, "VR_InitInternal");
  47. VR_ShutdownInternal = (PFN_VR_SHUTDOWNINTERNAL )bx::dlsym(openvrdll, "VR_ShutdownInternal");
  48. VR_IsHmdPresent = (PFN_VR_ISHMDPRESENT )bx::dlsym(openvrdll, "VR_IsHmdPresent");
  49. VR_GetGenericInterface = (PFN_VR_GETGENERICINTERFACE )bx::dlsym(openvrdll, "VR_GetGenericInterface");
  50. VR_IsRuntimeInstalled = (PFN_VR_ISRUNTIMEINSTALLED )bx::dlsym(openvrdll, "VR_IsRuntimeInstalled");
  51. VR_IsInterfaceVersionValid = (PFN_VR_ISINTERFACEVERSIONVALID)bx::dlsym(openvrdll, "VR_IsInterfaceVersionValid");
  52. VR_GetInitToken = (PFN_VR_GETINITTOKEN )bx::dlsym(openvrdll, "VR_GetInitToken");
  53. VR_GetVRInitErrorAsSymbol = (PFN_VR_GETVRINITERRORASSYMBOL )bx::dlsym(openvrdll, "VR_GetVRInitErrorAsSymbol");
  54. VR_GetVRInitErrorAsEnglishDescription = (PFN_VR_GETVRINITERRORASENGLISHDESCRIPTION)bx::dlsym(openvrdll, "VR_GetVRInitErrorAsEnglishDescription");
  55. if (NULL == VR_InitInternal
  56. || NULL == VR_ShutdownInternal
  57. || NULL == VR_IsHmdPresent
  58. || NULL == VR_GetGenericInterface
  59. || NULL == VR_IsRuntimeInstalled
  60. || NULL == VR_IsInterfaceVersionValid
  61. || NULL == VR_GetInitToken
  62. || NULL == VR_GetVRInitErrorAsSymbol
  63. || NULL == VR_GetVRInitErrorAsEnglishDescription)
  64. {
  65. bx::dlclose(openvrdll);
  66. return NULL;
  67. }
  68. EVRInitError err;
  69. uint32_t token = VR_InitInternal(&err, EVRApplicationType_VRApplication_Scene);
  70. BX_UNUSED(token);
  71. BX_TRACE("OpenVR: HMD is %spresent, Runtime is %sinstalled."
  72. , VR_IsHmdPresent() ? "" : "not "
  73. , VR_IsRuntimeInstalled() ? "" : "not "
  74. );
  75. }
  76. return openvrdll;
  77. }
  78. void unloadOpenVR(void* _openvrdll)
  79. {
  80. VR_ShutdownInternal();
  81. bx::dlclose(_openvrdll);
  82. }
  83. } // namespace bgfx