diff --git a/AutoStart/AutoStart-MacOS.cpp b/AutoStart/AutoStart-MacOS.cpp index 32bad731..9060b597 100644 --- a/AutoStart/AutoStart-MacOS.cpp +++ b/AutoStart/AutoStart-MacOS.cpp @@ -3,9 +3,12 @@ #include "filesystem.h" #include +#include #include #include +#include + /*-----------------------------------------------------*\ | MacOS AutoStart Implementation | | Public Methods | @@ -21,35 +24,35 @@ bool AutoStart::DisableAutoStart() std::error_code autostart_file_remove_errcode; bool success = false; - // /*-------------------------------------------------*\ - // | Check if the filename is valid | - // \*-------------------------------------------------*/ - // if(autostart_file != "") - // { - // /*---------------------------------------------*\ - // | If file doesn't exist, disable is successful | - // \*---------------------------------------------*/ - // if(!filesystem::exists(autostart_file)) - // { - // success = true; - // } - // /*---------------------------------------------*\ - // | Otherwise, delete the file | - // \*---------------------------------------------*/ - // else - // { - // success = filesystem::remove(autostart_file, autostart_file_remove_errcode); + /*-------------------------------------------------*\ + | Check if the filename is valid | + \*-------------------------------------------------*/ + if(autostart_file != "") + { + /*---------------------------------------------*\ + | If file doesn't exist, disable is successful | + \*---------------------------------------------*/ + if(!filesystem::exists(autostart_file)) + { + success = true; + } + /*---------------------------------------------*\ + | Otherwise, delete the file | + \*---------------------------------------------*/ + else + { + success = filesystem::remove(autostart_file, autostart_file_remove_errcode); - // if(!success) - // { - // LOG_ERROR("[AutoStart] An error occurred removing the auto start file."); - // } - // } - // } - // else - // { - // LOG_ERROR("Could not establish correct autostart file path."); - // } + if(!success) + { + LOG_ERROR("[AutoStart] An error occurred removing the auto start file."); + } + } + } + else + { + LOG_ERROR("Could not establish correct autostart file path."); + } return(success); } @@ -58,41 +61,41 @@ bool AutoStart::EnableAutoStart(AutoStartInfo autostart_info) { bool success = false; - // /*-------------------------------------------------*\ - // | Check if the filename is valid | - // \*-------------------------------------------------*/ - // if(autostart_file != "") - // { - // std::string desktop_file = GenerateDesktopFile(autostart_info); - // std::ofstream autostart_file_stream(autostart_file, std::ios::out | std::ios::trunc); + /*-------------------------------------------------*\ + | Check if the filename is valid | + \*-------------------------------------------------*/ + if(autostart_file != "") + { + std::string desktop_file = GenerateLaunchAgentFile(autostart_info); + std::ofstream autostart_file_stream(autostart_file, std::ios::out | std::ios::trunc); - // /*---------------------------------------------*\ - // | Error out if the file could not be opened | - // \*---------------------------------------------*/ - // if(!autostart_file_stream) - // { - // LOG_ERROR("Could not open %s for writing.", autostart_file.c_str()); - // success = false; - // } - // /*---------------------------------------------*\ - // | Otherwise, write the file | - // \*---------------------------------------------*/ - // else - // { - // autostart_file_stream << desktop_file; - // autostart_file_stream.close(); - // success = !autostart_file_stream.fail(); + /*---------------------------------------------*\ + | Error out if the file could not be opened | + \*---------------------------------------------*/ + if(!autostart_file_stream) + { + LOG_ERROR("Could not open %s for writing.", autostart_file.c_str()); + success = false; + } + /*---------------------------------------------*\ + | Otherwise, write the file | + \*---------------------------------------------*/ + else + { + autostart_file_stream << desktop_file; + autostart_file_stream.close(); + success = !autostart_file_stream.fail(); - // if (!success) - // { - // LOG_ERROR("An error occurred writing the auto start file."); - // } - // } - // } - // else - // { - // LOG_ERROR("Could not establish correct autostart file path."); - // } + if (!success) + { + LOG_ERROR("An error occurred writing the auto start file."); + } + } + } + else + { + LOG_ERROR("Could not establish correct autostart file path."); + } return(success); } @@ -117,11 +120,12 @@ std::string AutoStart::GetExePath() /*-------------------------------------------------*\ | Create the OpenRGB executable path | \*-------------------------------------------------*/ - // char exepath[ PATH_MAX ]; + char exepath[ PATH_MAX ]; + uint32_t exesize = PATH_MAX; - // ssize_t count = readlink("/proc/self/exe", exepath, PATH_MAX); - - // return(std::string(exepath, (count > 0) ? count : 0)); + int ret_val = _NSGetExecutablePath(exepath, &exesize); + + return(std::string(exepath, (ret_val == 0) ? strlen(exepath) : 0)); return(""); } @@ -131,51 +135,74 @@ std::string AutoStart::GetExePath() | Private Methods | \*-----------------------------------------------------*/ +std::string AutoStart::GenerateLaunchAgentFile(AutoStartInfo autostart_info) +{ + /*-------------------------------------------------*\ + | Generate a .plist file from the AutoStart | + | parameters | + \*-------------------------------------------------*/ + std::stringstream fileContents; + + fileContents << "" << std::endl; + fileContents << "" << std::endl; + fileContents << "" << std::endl; + fileContents << "" << std::endl; + fileContents << " Label" << std::endl; + fileContents << " org.openrgb" << std::endl; + fileContents << " ProgramArguments" << std::endl; + fileContents << " " << std::endl; + fileContents << " " << autostart_info.path << "" << std::endl; + + if(autostart_info.args != "") + { + std::istringstream arg_parser(autostart_info.args); + std::string arg; + + while(arg_parser >> arg) + { + fileContents << " " << arg << "" << std::endl; + } + } + + fileContents << " " << std::endl; + fileContents << " RunAtLoad" << std::endl; + fileContents << "" << std::endl; + fileContents << "" << std::endl; + + return(fileContents.str()); +} + void AutoStart::InitAutoStart(std::string name) { std::string autostart_dir; autostart_name = name; - // /*-------------------------------------------------*\ - // | Get home and config paths | - // \*-------------------------------------------------*/ - // const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); - // const char *home = getenv("HOME"); + /*-------------------------------------------------*\ + | Determine where the autostart .desktop files are | + | kept | + \*-------------------------------------------------*/ + autostart_dir = getenv("HOME"); + autostart_dir = autostart_dir + "/Library/LaunchAgents/"; - // /*-------------------------------------------------*\ - // | Determine where the autostart .desktop files are | - // | kept | - // \*-------------------------------------------------*/ - // if(xdg_config_home != NULL) - // { - // autostart_dir = xdg_config_home; - // autostart_dir = autostart_dir + "/autostart/"; - // } - // else if(home != NULL) - // { - // autostart_dir = home; - // autostart_dir = autostart_dir + "/.config/autostart/"; - // } + /*-------------------------------------------------*\ + | Check if the filename is valid | + \*-------------------------------------------------*/ + if(autostart_dir != "") + { + std::error_code ec; - // /*-------------------------------------------------*\ - // | Check if the filename is valid | - // \*-------------------------------------------------*/ - // if(autostart_dir != "") - // { - // std::error_code ec; - - // bool success = true; + bool success = true; - // if(!filesystem::exists(autostart_dir)) - // { - // success = filesystem::create_directories(autostart_dir, ec); - // } + if(!filesystem::exists(autostart_dir)) + { + success = filesystem::create_directories(autostart_dir, ec); + } - // if(success) - // { - // autostart_file = autostart_dir + autostart_name + ".desktop"; - // } - // } + if(success) + { + autostart_file = autostart_dir + autostart_name + ".plist"; + } + } } diff --git a/AutoStart/AutoStart-MacOS.h b/AutoStart/AutoStart-MacOS.h index 1c55e634..1a90ec31 100644 --- a/AutoStart/AutoStart-MacOS.h +++ b/AutoStart/AutoStart-MacOS.h @@ -15,4 +15,5 @@ public: private: void InitAutoStart(std::string name); + std::string GenerateLaunchAgentFile(AutoStartInfo autostart_info); }; diff --git a/qt/OpenRGBSettingsPage.cpp b/qt/OpenRGBSettingsPage.cpp index 56f9b1de..04994842 100644 --- a/qt/OpenRGBSettingsPage.cpp +++ b/qt/OpenRGBSettingsPage.cpp @@ -114,11 +114,6 @@ OpenRGBSettingsPage::OpenRGBSettingsPage(QWidget *parent) : ui->AutoStartStatusLabel->hide(); autostart_initialized = true; - -#ifdef __APPLE__ - SetAutoStartVisibility(false); - ui->CheckboxAutoStart->hide(); -#endif } OpenRGBSettingsPage::~OpenRGBSettingsPage()