浏览代码

UI: Detect other instances of obs on macOS

Detect other instances of the obs by inspecting running applications
with same bundle ID.

fixes: #3053
Mikhail Kochegarov 5 年之前
父节点
当前提交
117d35433f
共有 3 个文件被更改,包括 29 次插入2 次删除
  1. 5 2
      UI/obs-app.cpp
  2. 23 0
      UI/platform-osx.mm
  3. 1 0
      UI/platform.hpp

+ 5 - 2
UI/obs-app.cpp

@@ -1929,13 +1929,17 @@ static int run_program(fstream &logFile, int argc, char *argv[])
 		OBSTranslator translator;
 		program.installTranslator(&translator);
 
-#ifdef _WIN32
 		/* --------------------------------------- */
 		/* check and warn if already running       */
 
 		bool cancel_launch = false;
 		bool already_running = false;
+
+#if defined(_WIN32)
 		RunOnceMutex rom = GetRunOnceMutex(already_running);
+#elif defined(__APPLE__)
+		CheckAppWithSameBundleID(already_running);
+#endif
 
 		if (!already_running) {
 			goto run;
@@ -1979,7 +1983,6 @@ static int run_program(fstream &logFile, int argc, char *argv[])
 
 		/* --------------------------------------- */
 	run:
-#endif
 
 #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__FreeBSD__)
 		// Mounted by termina during chromeOS linux container startup

+ 23 - 0
UI/platform-osx.mm

@@ -87,6 +87,29 @@ bool InitApplicationBundle()
 #endif
 }
 
+void CheckAppWithSameBundleID(bool &already_running)
+{
+	try {
+		NSBundle *bundle = [NSBundle mainBundle];
+		if (!bundle)
+			throw "Could not find main bundle";
+
+		NSString *bundleID = [bundle bundleIdentifier];
+		if (!bundleID)
+			throw "Could not find bundle identifier";
+
+		int app_count =
+			[NSRunningApplication
+				runningApplicationsWithBundleIdentifier:bundleID]
+				.count;
+
+		already_running = app_count > 1;
+
+	} catch (const char *error) {
+		blog(LOG_ERROR, "CheckAppWithSameBundleID: %s", error);
+	}
+}
+
 string GetDefaultVideoSavePath()
 {
 	NSFileManager *fm = [NSFileManager defaultManager];

+ 1 - 0
UI/platform.hpp

@@ -68,4 +68,5 @@ void EnableOSXVSync(bool enable);
 void EnableOSXDockIcon(bool enable);
 void InstallNSApplicationSubclass();
 void disableColorSpaceConversion(QWidget *window);
+void CheckAppWithSameBundleID(bool &already_running);
 #endif