From 884d8713eeb6c76d566c6dd02bac12fb88970970 Mon Sep 17 00:00:00 2001
From: Stephen Birarda <commit@birarda.com>
Date: Mon, 2 Feb 2015 10:04:04 -0800
Subject: [PATCH] actually enter and leave vr mode

---
 gvr-interface/src/GVRInterface.cpp | 42 +++++++++++++++++++++++++++---
 gvr-interface/src/GVRInterface.h   |  4 +++
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/gvr-interface/src/GVRInterface.cpp b/gvr-interface/src/GVRInterface.cpp
index d40845d675..51e2182775 100644
--- a/gvr-interface/src/GVRInterface.cpp
+++ b/gvr-interface/src/GVRInterface.cpp
@@ -83,8 +83,8 @@ JNIEXPORT void Java_io_highfidelity_gvrinterface_InterfaceActivity_handleHifiURL
 void GVRInterface::idle() {
 #if defined(ANDROID) && defined(HAVE_LIBOVR)
     if (!_inVRMode && ovr_IsHeadsetDocked()) {
-        qDebug() << "The headset just got docked - assume we are in VR mode.";
-        _inVRMode = true;
+        qDebug() << "The headset just got docked - enter VR mode.";
+        enterVRMode();
     } else if (_inVRMode) {
 
         if (ovr_IsHeadsetDocked()) {
@@ -109,8 +109,9 @@ void GVRInterface::idle() {
                     << ovrOrientation.x <<  ovrOrientation.y <<  ovrOrientation.z <<  ovrOrientation.w;
             }
         } else {
-            qDebug() << "The headset was undocked - assume we are no longer in VR mode.";
-            _inVRMode = false;
+            qDebug() << "The headset was undocked - leaving VR mode.";
+            
+            leaveVRMode();
         }
     } 
 #endif
@@ -128,3 +129,36 @@ void GVRInterface::handleApplicationStateChange(Qt::ApplicationState state) {
             break;
     }
 }
+
+void GVRInterface::enterVRMode() {
+#if defined(ANDROID) && defined(HAVE_LIBOVR)
+    // Default vrModeParms
+    ovrModeParms vrModeParms;
+    vrModeParms.AsynchronousTimeWarp = true;
+    vrModeParms.AllowPowerSave = true;
+    vrModeParms.DistortionFileName = NULL;
+    vrModeParms.EnableImageServer = false;
+    vrModeParms.CpuLevel = 2;
+    vrModeParms.GpuLevel = 2;
+    vrModeParms.GameThreadTid = 0;
+    
+    QAndroidJniEnvironment jniEnv;
+    
+    QPlatformNativeInterface* interface = QApplication::platformNativeInterface();
+    jobject activity = (jobject) interface->nativeResourceForIntegration("QtActivity");
+    
+    vrModeParms.ActivityObject = activity;
+    
+    ovrHmdInfo hmdInfo;
+    _ovr = ovr_EnterVrMode(vrModeParms, &hmdInfo);
+    
+    _inVRMode = true;
+#endif
+}
+
+void GVRInterface::leaveVRMode() {
+#if defined(ANDROID) && defined(HAVE_LIBOVR)
+    ovr_LeaveVrMode(_ovr);
+    _inVRMode = false;
+#endif
+}
diff --git a/gvr-interface/src/GVRInterface.h b/gvr-interface/src/GVRInterface.h
index d189c0b2af..b126dd1a45 100644
--- a/gvr-interface/src/GVRInterface.h
+++ b/gvr-interface/src/GVRInterface.h
@@ -37,6 +37,10 @@ private slots:
     void handleApplicationStateChange(Qt::ApplicationState state);
     void idle();
 private:
+    
+    void enterVRMode();
+    void leaveVRMode();
+    
     RenderingClient* _client;
     bool _inVRMode;