From a3bf90f5a3fe050ffae353f80bc5608c7c9a1244 Mon Sep 17 00:00:00 2001
From: Philip Rebohle <philip.rebohle@tu-dortmund.de>
Date: Thu, 4 Oct 2018 12:30:26 +0200
Subject: [PATCH] [vr] Cosmetic code cleanup

---
 src/dxvk/dxvk_openvr.cpp | 89 ++++++++++++++++++++++------------------
 src/dxvk/dxvk_openvr.h   | 18 +++++---
 2 files changed, 62 insertions(+), 45 deletions(-)

diff --git a/src/dxvk/dxvk_openvr.cpp b/src/dxvk/dxvk_openvr.cpp
index 3e4fbe20..255c3fc1 100644
--- a/src/dxvk/dxvk_openvr.cpp
+++ b/src/dxvk/dxvk_openvr.cpp
@@ -57,10 +57,10 @@ namespace dxvk {
   void VrInstance::initInstanceExtensions() {
     std::lock_guard<std::mutex> lock(m_mutex);
 
-    if (m_compositor == nullptr)
+    if (!m_compositor)
       m_compositor = this->getCompositor();
 
-    if (m_compositor == nullptr || m_initializedInsExt)
+    if (!m_compositor || m_initializedInsExt)
       return;
     
     m_insExtensions = this->queryInstanceExtensions();
@@ -71,7 +71,7 @@ namespace dxvk {
   void VrInstance::initDeviceExtensions(const DxvkInstance* instance) {
     std::lock_guard<std::mutex> lock(m_mutex);
 
-    if (m_compositor == nullptr || m_initializedDevExt)
+    if (!m_compositor || m_initializedDevExt)
       return;
     
     for (uint32_t i = 0; instance->enumAdapters(i) != nullptr; i++) {
@@ -121,40 +121,20 @@ namespace dxvk {
     // Locate the OpenVR DLL if loaded by the process. Some
     // applications may not have OpenVR loaded at the time
     // they create the DXGI instance, so we try our own DLL.
-#ifdef __WINE__
-    // on winelib, load native openvr_api directly
-    m_ovrApi = ::dlopen("libopenvr_api.so", RTLD_LAZY | RTLD_NOLOAD);
-
-    if (m_ovrApi == nullptr) {
-      m_ovrApi = ::dlopen("libopenvr_api_dxvk.so", RTLD_LAZY | RTLD_LOCAL);
-      m_loadedOvrApi = m_ovrApi != nullptr;
-    }
-#else
-    m_ovrApi = ::GetModuleHandle("openvr_api.dll");
-
-    if (m_ovrApi == nullptr) {
-      m_ovrApi = ::LoadLibrary("openvr_api_dxvk.dll");
-      m_loadedOvrApi = m_ovrApi != nullptr;
-    }
-#endif
+    m_ovrApi = this->loadLibrary();
+    m_loadedOvrApi = m_ovrApi != nullptr;
     
-    if (m_ovrApi == nullptr) {
+    if (!m_ovrApi) {
       Logger::warn("OpenVR: Failed to locate module");
       return nullptr;
     }
     
     // Load method used to retrieve the IVRCompositor interface
-#ifdef __WINE__
-    g_vrFunctions.initInternal        = reinterpret_cast<VR_InitInternalProc>       (::dlsym(m_ovrApi, "VR_InitInternal"));
-    g_vrFunctions.shutdownInternal    = reinterpret_cast<VR_ShutdownInternalProc>   (::dlsym(m_ovrApi, "VR_ShutdownInternal"));
-    g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(::dlsym(m_ovrApi, "VR_GetGenericInterface"));
-#else
-    g_vrFunctions.initInternal        = reinterpret_cast<VR_InitInternalProc>       (::GetProcAddress(m_ovrApi, "VR_InitInternal"));
-    g_vrFunctions.shutdownInternal    = reinterpret_cast<VR_ShutdownInternalProc>   (::GetProcAddress(m_ovrApi, "VR_ShutdownInternal"));
-    g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(::GetProcAddress(m_ovrApi, "VR_GetGenericInterface"));
-#endif
+    g_vrFunctions.initInternal        = reinterpret_cast<VR_InitInternalProc>       (this->getSym("VR_InitInternal"));
+    g_vrFunctions.shutdownInternal    = reinterpret_cast<VR_ShutdownInternalProc>   (this->getSym("VR_ShutdownInternal"));
+    g_vrFunctions.getGenericInterface = reinterpret_cast<VR_GetGenericInterfaceProc>(this->getSym("VR_GetGenericInterface"));
     
-    if (g_vrFunctions.getGenericInterface == nullptr) {
+    if (!g_vrFunctions.getGenericInterface) {
       Logger::warn("OpenVR: VR_GetGenericInterface not found");
       return nullptr;
     }
@@ -165,9 +145,9 @@ namespace dxvk {
     vr::IVRCompositor* compositor = reinterpret_cast<vr::IVRCompositor*>(
       g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error));
     
-    if (error != vr::VRInitError_None || compositor == nullptr) {
-      if (g_vrFunctions.initInternal     == nullptr
-       || g_vrFunctions.shutdownInternal == nullptr) {
+    if (error != vr::VRInitError_None || !compositor) {
+      if (!g_vrFunctions.initInternal
+       || !g_vrFunctions.shutdownInternal) {
         Logger::warn("OpenVR: VR_InitInternal or VR_ShutdownInternal not found");
         return nullptr;
       }
@@ -185,7 +165,7 @@ namespace dxvk {
       compositor = reinterpret_cast<vr::IVRCompositor*>(
         g_vrFunctions.getGenericInterface(vr::IVRCompositor_Version, &error));
       
-      if (error != vr::VRInitError_None || compositor == nullptr) {
+      if (error != vr::VRInitError_None || !compositor) {
         Logger::warn("OpenVR: Failed to query compositor interface");
         this->shutdown();
         return nullptr;
@@ -201,16 +181,45 @@ namespace dxvk {
     if (m_initializedOpenVr)
       g_vrFunctions.shutdownInternal();
     
-#if __WINE__
     if (m_loadedOvrApi)
-      ::dlclose(m_ovrApi);
-#else
-    if (m_loadedOvrApi)
-      ::FreeLibrary(m_ovrApi);
-#endif
+      this->freeLibrary();
     
     m_initializedOpenVr = false;
     m_loadedOvrApi      = false;
   }
+
+
+  SoHandle VrInstance::loadLibrary() {
+    SoHandle handle = nullptr;
+    #ifdef __WINE__
+    // on winelib, load native openvr_api directly
+    if (!(handle = ::dlopen("libopenvr_api.so", RTLD_LAZY | RTLD_NOLOAD)))
+      handle = ::dlopen("libopenvr_api_dxvk.so", RTLD_LAZY | RTLD_LOCAL);
+    #else
+    if (!(handle = ::GetModuleHandle("openvr_api.dll")))
+      handle = ::LoadLibrary("openvr_api_dxvk.dll");
+    #endif
+    return handle;
+  }
+
+
+  void VrInstance::freeLibrary() {
+    #ifdef __WINE__
+    ::dlclose(m_ovrApi);
+    #else
+    ::FreeLibrary(m_ovrApi);
+    #endif
+  }
+
+  
+  void* VrInstance::getSym(const char* sym) {
+    #ifdef __WINE__
+    return reinterpret_cast<void*>(
+      ::dlsym(m_ovrApi, sym));
+    #else
+    return reinterpret_cast<void*>(
+      ::GetProcAddress(m_ovrApi, sym));
+    #endif
+  }
   
 }
\ No newline at end of file
diff --git a/src/dxvk/dxvk_openvr.h b/src/dxvk/dxvk_openvr.h
index 858f57c3..f5058c87 100644
--- a/src/dxvk/dxvk_openvr.h
+++ b/src/dxvk/dxvk_openvr.h
@@ -5,6 +5,12 @@
 
 #include "dxvk_include.h"
 
+#ifdef __WINE__
+using SoHandle = void*;
+#else
+using SoHandle = HMODULE;
+#endif
+
 namespace vr {
   class IVRCompositor;
   class IVRSystem;
@@ -66,11 +72,7 @@ namespace dxvk {
 
     std::mutex            m_mutex;
     vr::IVRCompositor*    m_compositor = nullptr;
-#ifdef __WINE__
-    void*                 m_ovrApi     = nullptr;
-#else
-    HMODULE               m_ovrApi     = nullptr;
-#endif
+    SoHandle              m_ovrApi     = nullptr;
 
     bool m_loadedOvrApi      = false;
     bool m_initializedOpenVr = false;
@@ -91,6 +93,12 @@ namespace dxvk {
     vr::IVRCompositor* getCompositor();
 
     void shutdown();
+
+    SoHandle loadLibrary();
+
+    void freeLibrary();
+
+    void* getSym(const char* sym);
     
   };