From 13e8dc036c2898cf5adfb088764f2c0f9996ded9 Mon Sep 17 00:00:00 2001 From: dante ruiz Date: Mon, 30 Sep 2019 14:24:14 -0700 Subject: [PATCH] mac close interface and launcher and fix compile warnings --- launchers/qt/CMakeLists.txt | 1 + launchers/qt/src/Helper.h | 3 ++ launchers/qt/src/Helper_darwin.mm | 40 +++++++++++++++++++ launchers/qt/src/LauncherState.h | 6 +-- .../qt/src/NSTask+NSTaskExecveAdditions.m | 8 ++-- launchers/qt/src/Unzipper.cpp | 2 +- launchers/qt/src/main.cpp | 5 +++ 7 files changed, 56 insertions(+), 9 deletions(-) diff --git a/launchers/qt/CMakeLists.txt b/launchers/qt/CMakeLists.txt index d54189137c..57f900140c 100644 --- a/launchers/qt/CMakeLists.txt +++ b/launchers/qt/CMakeLists.txt @@ -35,6 +35,7 @@ include(ExternalProject) if (APPLE) set(CMAKE_EXE_LINKER_FLAGS "-framework Cocoa -framework CoreServices -framework Carbon -framework IOKit -framework Security -framework SystemConfiguration") + add_compile_options(-W -Wall -Wextra -Wpedantic) endif() if (WIN32) diff --git a/launchers/qt/src/Helper.h b/launchers/qt/src/Helper.h index d26cacfa5c..65f68ec41d 100644 --- a/launchers/qt/src/Helper.h +++ b/launchers/qt/src/Helper.h @@ -14,6 +14,9 @@ void swapLaunchers(const QString& oldLauncherPath = QString(), const QString& ne #ifdef Q_OS_MAC bool replaceDirectory(const QString& orginalDirectory, const QString& newDirectory); +void closeInterfaceIfRunning(); +void waitForInterfaceToClose(); +bool isLauncherAlreadyRunning(); #endif #ifdef Q_OS_WIN diff --git a/launchers/qt/src/Helper_darwin.mm b/launchers/qt/src/Helper_darwin.mm index 5ffde89caf..28b35193ba 100644 --- a/launchers/qt/src/Helper_darwin.mm +++ b/launchers/qt/src/Helper_darwin.mm @@ -70,3 +70,43 @@ bool replaceDirectory(const QString& orginalDirectory, const QString& newDirecto return (bool) [fileManager replaceItemAtURL:[UpdaterHelper NSStringToNSURL:orginalDirectory.toNSString()] withItemAtURL:[UpdaterHelper NSStringToNSURL:newDirectory.toNSString()] backupItemName:nil options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:&destinationUrl error:nil]; } + + +void waitForInterfaceToClose() { + bool interfaceRunning = true; + + while (interfaceRunning) { + interfaceRunning = false; + NSWorkspace* workspace = [NSWorkspace sharedWorkspace]; + NSArray* apps = [workspace runningApplications]; + for (NSRunningApplication* app in apps) { + if ([[app bundleIdentifier] isEqualToString:@"com.highfidelity.interface"] || + [[app bundleIdentifier] isEqualToString:@"com.highfidelity.interface-pr"]) { + interfaceRunning = true; + break; + } + } + } +} + +bool isLauncherAlreadyRunning() { + NSArray* apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.highfidelity.launcher"]; + NSLog(@"Count: %lu", [apps count]); + if ([apps count] > 1) { + NSLog(@"launcher is already running"); + return true; + } + + return false; +} + +void closeInterfaceIfRunning() { + NSWorkspace* workspace = [NSWorkspace sharedWorkspace]; + NSArray* apps = [workspace runningApplications]; + for (NSRunningApplication* app in apps) { + if ([[app bundleIdentifier] isEqualToString:@"com.highfidelity.interface"] || + [[app bundleIdentifier] isEqualToString:@"com.highfidelity.interface-pr"]) { + [app terminate]; + } + } +} diff --git a/launchers/qt/src/LauncherState.h b/launchers/qt/src/LauncherState.h index b96440c827..51e0399c1e 100644 --- a/launchers/qt/src/LauncherState.h +++ b/launchers/qt/src/LauncherState.h @@ -31,9 +31,9 @@ struct LoginResponse { class LauncherState : public QObject { Q_OBJECT - Q_PROPERTY(UIState uiState READ getUIState NOTIFY uiStateChanged); - Q_PROPERTY(ApplicationState applicationState READ getApplicationState NOTIFY applicationStateChanged); - Q_PROPERTY(float downloadProgress READ getDownloadProgress NOTIFY downloadProgressChanged); + Q_PROPERTY(UIState uiState READ getUIState NOTIFY uiStateChanged) + Q_PROPERTY(ApplicationState applicationState READ getApplicationState NOTIFY applicationStateChanged) + Q_PROPERTY(float downloadProgress READ getDownloadProgress NOTIFY downloadProgressChanged) public: LauncherState(); diff --git a/launchers/qt/src/NSTask+NSTaskExecveAdditions.m b/launchers/qt/src/NSTask+NSTaskExecveAdditions.m index 6cdf669247..a216b24e95 100644 --- a/launchers/qt/src/NSTask+NSTaskExecveAdditions.m +++ b/launchers/qt/src/NSTask+NSTaskExecveAdditions.m @@ -15,7 +15,7 @@ toCArray(NSArray *array) @throw exception; } char *str; - for (int i = 0; i < [array count]; i++) { + for (NSUInteger i = 0; i < [array count]; i++) { str = (char *) [array[i] UTF8String]; if (str == NULL) { NSException *exception = [NSException @@ -25,7 +25,7 @@ toCArray(NSArray *array) @throw exception; } if (asprintf(&cArray[i], "%s", str) == -1) { - for (int j = 0; j < i; j++) { + for (NSUInteger j = 0; j < i; j++) { free(cArray[j]); } free(cArray); @@ -54,13 +54,11 @@ toCArray(NSArray *array) char** envp = toCArray(env); // `execve` replaces the current process with `path`. // It will only return if it fails to replace the current process. - NSLog(@"------------>"); chdir(dirname(args[0])); execve(args[0], (char * const *)args, envp); - NSLog(@"----------- FAILED "); // If we're here `execve` failed. :( - for (int i = 0; i < [[self arguments] count]; i++) { + for (NSUInteger i = 0; i < [[self arguments] count]; i++) { free((void *) args[i]); } free((void *) args); diff --git a/launchers/qt/src/Unzipper.cpp b/launchers/qt/src/Unzipper.cpp index 5d08d3b25a..54a81f272e 100644 --- a/launchers/qt/src/Unzipper.cpp +++ b/launchers/qt/src/Unzipper.cpp @@ -49,7 +49,7 @@ void Unzipper::run() { uint64_t totalSize = 0; uint64_t totalCompressedSize = 0; - bool _shouldFail = false; + //bool _shouldFail = false; for (int i = 0; i < fileCount; i++) { if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) continue; diff --git a/launchers/qt/src/main.cpp b/launchers/qt/src/main.cpp index a1c7ab61ca..db69bfafe3 100644 --- a/launchers/qt/src/main.cpp +++ b/launchers/qt/src/main.cpp @@ -31,6 +31,11 @@ bool hasSuffix(const std::string path, const std::string suffix) { int main(int argc, char *argv[]) { Q_INIT_RESOURCE(resources); #ifdef Q_OS_MAC + if (isLauncherAlreadyRunning()) { + return 0; + } + closeInterfaceIfRunning(); + // waitForInterfaceToClose(); // auto updater if (argc == 3) { if (hasSuffix(argv[1], "app") && hasSuffix(argv[2], "app")) {