Browse Source

Windows ovr toggle (#67)

* Stop/Start OVRService when running OpenHMD on windows
TheOnlyJoey 8 years ago
parent
commit
31a27b9c4d
4 changed files with 46 additions and 1 deletions
  1. 4 0
      src/drv_oculus_rift/rift.c
  2. 5 1
      src/platform-posix.c
  3. 36 0
      src/platform-win32.c
  4. 1 0
      src/platform.h

+ 4 - 0
src/drv_oculus_rift/rift.c

@@ -377,6 +377,8 @@ static void destroy_driver(ohmd_driver* drv)
 	LOGD("shutting down driver");
 	hid_exit();
 	free(drv);
+
+	ohmd_toggle_ovr_service(1); //re-enable OVRService if previously running
 }
 
 ohmd_driver* ohmd_create_oculus_rift_drv(ohmd_context* ctx)
@@ -385,6 +387,8 @@ ohmd_driver* ohmd_create_oculus_rift_drv(ohmd_context* ctx)
 	if(drv == NULL)
 		return NULL;
 
+	ohmd_toggle_ovr_service(0); //disable OVRService if running
+
 	drv->get_device_list = get_device_list;
 	drv->open_device = open_device;
 	drv->destroy = destroy_driver;

+ 5 - 1
src/platform-posix.c

@@ -51,7 +51,6 @@ void ohmd_sleep(double seconds)
 }
 
 // threads
-
 struct ohmd_thread
 {
 	pthread_t thread;
@@ -125,4 +124,9 @@ void ohmd_unlock_mutex(ohmd_mutex* mutex)
 		pthread_mutex_unlock((pthread_mutex_t*)mutex);
 }
 
+/// Handling ovr service
+void ohmd_toggle_ovr_service(int state) //State is 0 for Disable, 1 for Enable
+{
+	//Empty implementation	
+}
 #endif

+ 36 - 0
src/platform-win32.c

@@ -105,4 +105,40 @@ void ohmd_unlock_mutex(ohmd_mutex* mutex)
 		ReleaseMutex(mutex->handle);
 }
 
+/// Handling ovr service
+static int _enable_ovr_service = 0;
+
+void ohmd_toggle_ovr_service(int state) //State is 0 for Disable, 1 for Enable
+{
+	SC_HANDLE serviceDbHandle = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
+	SC_HANDLE serviceHandle = OpenService(serviceDbHandle, 'OVRService', SC_MANAGER_ALL_ACCESS);
+
+	SERVICE_STATUS_PROCESS status;
+	DWORD bytesNeeded;
+	QueryServiceStatusEx(serviceHandle, SC_STATUS_PROCESS_INFO,(LPBYTE) &status,sizeof(SERVICE_STATUS_PROCESS), &bytesNeeded);
+
+	if (state == 0 || status.dwCurrentState == SERVICE_RUNNING)
+	{
+		// Stop it
+		BOOL b = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS) &status);
+		if (b)
+		{
+			printf("OVRService stopped\n");
+			_enable_ovr_service = 1;
+		}
+		else 
+			printf("Error: OVRService failed to stop, please try running with Administrator rights\n");
+	}
+	else if (state == 1 && _enable_ovr_service)
+	{
+		// Start it 
+		BOOL b = StartService(serviceHandle, NULL, NULL); 
+		if (b) 
+			printf("OVRService started\n");
+		else 
+			printf("Error: OVRService failed to start, please try running with Administrator rights\n");
+	} 
+	CloseServiceHandle(serviceHandle); 
+	CloseServiceHandle(serviceDbHandle); 
+}
 #endif

+ 1 - 0
src/platform.h

@@ -14,6 +14,7 @@
 
 double ohmd_get_tick();
 void ohmd_sleep(double seconds);
+void ohmd_toggle_ovr_service(int state);
 
 typedef struct ohmd_thread ohmd_thread;
 typedef struct ohmd_mutex ohmd_mutex;