Publish LumaOps source
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-FreeBSD.cpp |
|
||||
| |
|
||||
| Autostart implementation for FreeBSD |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include "AutoStart-FreeBSD.h"
|
||||
#include "LogManager.h"
|
||||
#include "filesystem.h"
|
||||
|
||||
AutoStart::AutoStart(std::string name)
|
||||
{
|
||||
InitAutoStart(name);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if(!success)
|
||||
{
|
||||
LOG_ERROR("[AutoStart] An error occurred removing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
bool AutoStart::EnableAutoStart(AutoStartInfo autostart_info)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_file != "")
|
||||
{
|
||||
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("[AutoStart] Could not open %s for writing.", autostart_file.c_str());
|
||||
success = false;
|
||||
}
|
||||
/*-------------------------------------------------*\
|
||||
| Otherwise, write the file |
|
||||
\*-------------------------------------------------*/
|
||||
else
|
||||
{
|
||||
autostart_file_stream.close();
|
||||
success = !autostart_file_stream.fail();
|
||||
|
||||
if (!success)
|
||||
{
|
||||
LOG_ERROR("[AutoStart] An error occurred writing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
bool AutoStart::IsAutoStartEnabled()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_file != "")
|
||||
{
|
||||
return(filesystem::exists(autostart_file));
|
||||
}
|
||||
else
|
||||
{
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
std::string AutoStart::GetExePath()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Create the OpenRGB executable path |
|
||||
\*-----------------------------------------------------*/
|
||||
char exepath[ PATH_MAX ];
|
||||
|
||||
ssize_t count = readlink("/proc/self/exe", exepath, PATH_MAX);
|
||||
|
||||
return(std::string(exepath, (count > 0) ? count : 0));
|
||||
}
|
||||
|
||||
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 |
|
||||
\*-----------------------------------------------------*/
|
||||
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;
|
||||
|
||||
bool success = true;
|
||||
|
||||
if(!filesystem::exists(autostart_dir))
|
||||
{
|
||||
success = filesystem::create_directories(autostart_dir, ec);
|
||||
}
|
||||
|
||||
if(success)
|
||||
{
|
||||
autostart_file = autostart_dir + autostart_name + ".desktop";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-FreeBSD.h |
|
||||
| |
|
||||
| Autostart implementation for FreeBSD |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "AutoStart.h"
|
||||
|
||||
class AutoStart: public AutoStartInterface
|
||||
{
|
||||
public:
|
||||
AutoStart(std::string name);
|
||||
|
||||
bool DisableAutoStart();
|
||||
bool EnableAutoStart(AutoStartInfo autostart_info);
|
||||
bool IsAutoStartEnabled();
|
||||
std::string GetExePath();
|
||||
|
||||
private:
|
||||
void InitAutoStart(std::string name);
|
||||
std::string GenerateLaunchAgentFile(AutoStartInfo autostart_info);
|
||||
};
|
||||
@@ -0,0 +1,216 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-Linux.cpp |
|
||||
| |
|
||||
| Autostart implementation for Linux |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
#include <linux/limits.h>
|
||||
#include "AutoStart-Linux.h"
|
||||
#include "LogManager.h"
|
||||
#include "filesystem.h"
|
||||
|
||||
AutoStart::AutoStart(std::string name)
|
||||
{
|
||||
InitAutoStart(name);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if(!success)
|
||||
{
|
||||
LOG_ERROR("[AutoStart] An error occurred removing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Error out if the file could not be opened |
|
||||
\*-------------------------------------------------*/
|
||||
if(!autostart_file_stream)
|
||||
{
|
||||
LOG_ERROR("[AutoStart] 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("[AutoStart] An error occurred writing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
bool AutoStart::IsAutoStartEnabled()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_file != "")
|
||||
{
|
||||
return(filesystem::exists(autostart_file));
|
||||
}
|
||||
else
|
||||
{
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
std::string AutoStart::GetExePath()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Create the OpenRGB executable path |
|
||||
\*-----------------------------------------------------*/
|
||||
char exepath[ PATH_MAX ];
|
||||
|
||||
ssize_t count = readlink("/proc/self/exe", exepath, PATH_MAX);
|
||||
|
||||
return(std::string(exepath, (count > 0) ? count : 0));
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Linux AutoStart Implementation |
|
||||
| Private Methods |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
std::string AutoStart::GenerateDesktopFile(AutoStartInfo autostart_info)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Generate a .desktop file from the AutoStart |
|
||||
| parameters |
|
||||
\*-----------------------------------------------------*/
|
||||
std::stringstream fileContents;
|
||||
|
||||
fileContents << "[Desktop Entry]" << std::endl;
|
||||
fileContents << "Categories=" << autostart_info.category << std::endl;
|
||||
fileContents << "Comment=" << autostart_info.desc << std::endl;
|
||||
fileContents << "Icon=" << autostart_info.icon << std::endl;
|
||||
fileContents << "Name=" << GetAutoStartName() << std::endl;
|
||||
fileContents << "StartupNotify=true" << std::endl;
|
||||
fileContents << "Terminal=false" << std::endl;
|
||||
fileContents << "Type=Application" << std::endl;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Add the executable path and arguments |
|
||||
\*-----------------------------------------------------*/
|
||||
fileContents << "Exec=" << autostart_info.path;
|
||||
|
||||
if (autostart_info.args != "")
|
||||
{
|
||||
fileContents << " " << autostart_info.args;
|
||||
}
|
||||
|
||||
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 |
|
||||
\*-----------------------------------------------------*/
|
||||
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;
|
||||
|
||||
bool success = true;
|
||||
|
||||
if(!filesystem::exists(autostart_dir))
|
||||
{
|
||||
success = filesystem::create_directories(autostart_dir, ec);
|
||||
}
|
||||
|
||||
if(success)
|
||||
{
|
||||
autostart_file = autostart_dir + autostart_name + ".desktop";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-Linux.h |
|
||||
| |
|
||||
| Autostart implementation for Linux |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "AutoStart.h"
|
||||
|
||||
class AutoStart: public AutoStartInterface
|
||||
{
|
||||
public:
|
||||
AutoStart(std::string name);
|
||||
|
||||
bool DisableAutoStart();
|
||||
bool EnableAutoStart(AutoStartInfo autostart_info);
|
||||
bool IsAutoStartEnabled();
|
||||
std::string GetExePath();
|
||||
|
||||
private:
|
||||
void InitAutoStart(std::string name);
|
||||
std::string GenerateDesktopFile(AutoStartInfo autostart_info);
|
||||
};
|
||||
@@ -0,0 +1,210 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-MacOS.cpp |
|
||||
| |
|
||||
| Autostart implementation for MacOS |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <mach-o/dyld.h>
|
||||
#include "AutoStart-MacOS.h"
|
||||
#include "LogManager.h"
|
||||
#include "filesystem.h"
|
||||
|
||||
AutoStart::AutoStart(std::string name)
|
||||
{
|
||||
InitAutoStart(name);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if(!success)
|
||||
{
|
||||
LOG_ERROR("[AutoStart] An error occurred removing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
bool AutoStart::EnableAutoStart(AutoStartInfo autostart_info)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| 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("[AutoStart] 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("[AutoStart] An error occurred writing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
bool AutoStart::IsAutoStartEnabled()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_file != "")
|
||||
{
|
||||
return(filesystem::exists(autostart_file));
|
||||
}
|
||||
else
|
||||
{
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
std::string AutoStart::GetExePath()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Create the OpenRGB executable path |
|
||||
\*-----------------------------------------------------*/
|
||||
char exepath[ PATH_MAX ];
|
||||
uint32_t exesize = PATH_MAX;
|
||||
|
||||
int ret_val = _NSGetExecutablePath(exepath, &exesize);
|
||||
|
||||
return(std::string(exepath, (ret_val == 0) ? strlen(exepath) : 0));
|
||||
|
||||
return("");
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| MacOS AutoStart Implementation |
|
||||
| Private Methods |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
std::string AutoStart::GenerateLaunchAgentFile(AutoStartInfo autostart_info)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Generate a .plist file from the AutoStart |
|
||||
| parameters |
|
||||
\*-----------------------------------------------------*/
|
||||
std::stringstream fileContents;
|
||||
|
||||
fileContents << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
|
||||
fileContents << "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" << std::endl;
|
||||
fileContents << "<plist version=\"1.0\">" << std::endl;
|
||||
fileContents << "<dict>" << std::endl;
|
||||
fileContents << " <key>Label</key>" << std::endl;
|
||||
fileContents << " <string>org.openrgb</string>" << std::endl;
|
||||
fileContents << " <key>ProgramArguments</key>" << std::endl;
|
||||
fileContents << " <array>" << std::endl;
|
||||
fileContents << " <string>" << autostart_info.path << "</string>" << std::endl;
|
||||
|
||||
if(autostart_info.args != "")
|
||||
{
|
||||
std::istringstream arg_parser(autostart_info.args);
|
||||
std::string arg;
|
||||
|
||||
while(arg_parser >> arg)
|
||||
{
|
||||
fileContents << " <string>" << arg << "</string>" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
fileContents << " </array>" << std::endl;
|
||||
fileContents << " <key>RunAtLoad</key><true/>" << std::endl;
|
||||
fileContents << "</dict>" << std::endl;
|
||||
fileContents << "</plist>" << std::endl;
|
||||
|
||||
return(fileContents.str());
|
||||
}
|
||||
|
||||
void AutoStart::InitAutoStart(std::string name)
|
||||
{
|
||||
std::string autostart_dir;
|
||||
|
||||
autostart_name = name;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Determine where the autostart .desktop files are |
|
||||
| kept |
|
||||
\*-----------------------------------------------------*/
|
||||
autostart_dir = getenv("HOME");
|
||||
autostart_dir = autostart_dir + "/Library/LaunchAgents/";
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_dir != "")
|
||||
{
|
||||
std::error_code ec;
|
||||
|
||||
bool success = true;
|
||||
|
||||
if(!filesystem::exists(autostart_dir))
|
||||
{
|
||||
success = filesystem::create_directories(autostart_dir, ec);
|
||||
}
|
||||
|
||||
if(success)
|
||||
{
|
||||
autostart_file = autostart_dir + autostart_name + ".plist";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-MacOS.h |
|
||||
| |
|
||||
| Autostart implementation for MacOS |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "AutoStart.h"
|
||||
|
||||
class AutoStart: public AutoStartInterface
|
||||
{
|
||||
public:
|
||||
AutoStart(std::string name);
|
||||
|
||||
bool DisableAutoStart();
|
||||
bool EnableAutoStart(AutoStartInfo autostart_info);
|
||||
bool IsAutoStartEnabled();
|
||||
std::string GetExePath();
|
||||
|
||||
private:
|
||||
void InitAutoStart(std::string name);
|
||||
std::string GenerateLaunchAgentFile(AutoStartInfo autostart_info);
|
||||
};
|
||||
@@ -0,0 +1,206 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-Windows.cpp |
|
||||
| |
|
||||
| Autostart implementation for Windows |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <shlobj.h>
|
||||
#include "AutoStart-Windows.h"
|
||||
#include "LogManager.h"
|
||||
#include "filesystem.h"
|
||||
#include "windows.h"
|
||||
|
||||
AutoStart::AutoStart(std::string name)
|
||||
{
|
||||
InitAutoStart(name);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if(!success)
|
||||
{
|
||||
LOG_ERROR("[AutoStart] An error occurred removing the auto start file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return(success);
|
||||
}
|
||||
|
||||
bool AutoStart::EnableAutoStart(AutoStartInfo autostart_info)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_file != "")
|
||||
{
|
||||
bool weInitialised = false;
|
||||
HRESULT result;
|
||||
IShellLinkW* shellLink = NULL;
|
||||
|
||||
std::wstring exepathw = utf8_decode(autostart_info.path);
|
||||
std::wstring argumentsw = utf8_decode(autostart_info.args);
|
||||
std::wstring startupfilepathw = utf8_decode(autostart_file);
|
||||
std::wstring descriptionw = utf8_decode(autostart_info.desc);
|
||||
std::wstring iconw = utf8_decode(autostart_info.path);
|
||||
|
||||
result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&shellLink);
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| If not initialized, initialize |
|
||||
\*-------------------------------------------------*/
|
||||
if(result == CO_E_NOTINITIALIZED)
|
||||
{
|
||||
weInitialised = true;
|
||||
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
|
||||
result = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLinkW, (void**)&shellLink);
|
||||
}
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| If successfully initialized, save a shortcut |
|
||||
| from the AutoStart parameters |
|
||||
\*-------------------------------------------------*/
|
||||
if(SUCCEEDED(result))
|
||||
{
|
||||
shellLink->SetPath(exepathw.c_str());
|
||||
shellLink->SetArguments(argumentsw.c_str());
|
||||
shellLink->SetDescription(descriptionw.c_str());
|
||||
shellLink->SetIconLocation(iconw.c_str(), 0);
|
||||
|
||||
IPersistFile* persistFile;
|
||||
|
||||
result = shellLink->QueryInterface(IID_IPersistFile, (void**)&persistFile);
|
||||
|
||||
if(SUCCEEDED(result))
|
||||
{
|
||||
result = persistFile->Save(startupfilepathw.c_str(), TRUE);
|
||||
success = SUCCEEDED(result);
|
||||
persistFile->Release();
|
||||
}
|
||||
|
||||
shellLink->Release();
|
||||
}
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Uninitialize when done |
|
||||
\*-------------------------------------------------*/
|
||||
if(weInitialised)
|
||||
{
|
||||
CoUninitialize();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR("[AutoStart] Could not establish correct autostart file path.");
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool AutoStart::IsAutoStartEnabled()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Check if the filename is valid |
|
||||
\*-----------------------------------------------------*/
|
||||
if(autostart_file != "")
|
||||
{
|
||||
return(filesystem::exists(autostart_file));
|
||||
}
|
||||
else
|
||||
{
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
|
||||
std::string AutoStart::GetExePath()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Create the OpenRGB executable path |
|
||||
\*-----------------------------------------------------*/
|
||||
char exepath[MAX_PATH] = "";
|
||||
|
||||
DWORD count = GetModuleFileNameA(NULL, exepath, MAX_PATH);
|
||||
|
||||
return(std::string(exepath, (count > 0) ? count : 0));
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Windows AutoStart Implementation |
|
||||
| Private Methods |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
void AutoStart::InitAutoStart(std::string name)
|
||||
{
|
||||
char startMenuPath[MAX_PATH];
|
||||
|
||||
autostart_name = name;
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Get startup applications path |
|
||||
\*-----------------------------------------------------*/
|
||||
HRESULT result = SHGetFolderPathA(NULL, CSIDL_PROGRAMS, NULL, 0, startMenuPath);
|
||||
|
||||
if(SUCCEEDED(result))
|
||||
{
|
||||
autostart_file = std::string(startMenuPath);
|
||||
|
||||
autostart_file += "\\Startup\\" + autostart_name + ".lnk";
|
||||
}
|
||||
else
|
||||
{
|
||||
autostart_file.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------*\
|
||||
| Convert an UTF8 string to a wide Unicode String |
|
||||
| (from wmi.cpp) |
|
||||
\*---------------------------------------------------------*/
|
||||
std::wstring AutoStart::utf8_decode(const std::string& str)
|
||||
{
|
||||
if(str.empty())
|
||||
{
|
||||
return std::wstring();
|
||||
}
|
||||
|
||||
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), nullptr, 0);
|
||||
|
||||
std::wstring wstrTo(size_needed, 0);
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), &wstrTo[0], size_needed);
|
||||
|
||||
return(wstrTo);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart-Windows.h |
|
||||
| |
|
||||
| Autostart implementation for Windows |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "AutoStart.h"
|
||||
|
||||
class AutoStart: public AutoStartInterface
|
||||
{
|
||||
public:
|
||||
AutoStart(std::string name);
|
||||
|
||||
bool DisableAutoStart();
|
||||
bool EnableAutoStart(AutoStartInfo autostart_info);
|
||||
bool IsAutoStartEnabled();
|
||||
std::string GetExePath();
|
||||
|
||||
private:
|
||||
void InitAutoStart(std::string name);
|
||||
std::wstring utf8_decode(const std::string& str);
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart.cpp |
|
||||
| |
|
||||
| Autostart common implementation |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include "AutoStart.h"
|
||||
|
||||
std::string AutoStartInterface::GetAutoStartFile()
|
||||
{
|
||||
return(autostart_file);
|
||||
}
|
||||
|
||||
std::string AutoStartInterface::GetAutoStartName()
|
||||
{
|
||||
return(autostart_name);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*---------------------------------------------------------*\
|
||||
| AutoStart.h |
|
||||
| |
|
||||
| Autostart common implementation |
|
||||
| |
|
||||
| This file is part of the OpenRGB project |
|
||||
| SPDX-License-Identifier: GPL-2.0-or-later |
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
struct AutoStartInfo
|
||||
{
|
||||
std::string path;
|
||||
std::string args;
|
||||
std::string desc;
|
||||
std::string icon;
|
||||
std::string category;
|
||||
};
|
||||
|
||||
class AutoStartInterface
|
||||
{
|
||||
public:
|
||||
virtual bool DisableAutoStart() = 0;
|
||||
virtual bool EnableAutoStart(AutoStartInfo autostart_info) = 0;
|
||||
virtual bool IsAutoStartEnabled() = 0;
|
||||
virtual std::string GetExePath() = 0;
|
||||
|
||||
std::string GetAutoStartFile();
|
||||
std::string GetAutoStartName();
|
||||
|
||||
protected:
|
||||
std::string autostart_file;
|
||||
std::string autostart_name;
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "AutoStart-Windows.h"
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#include "AutoStart-Linux.h"
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include "AutoStart-MacOS.h"
|
||||
#endif
|
||||
|
||||
#ifdef __FreeBSD__
|
||||
#include "AutoStart-FreeBSD.h"
|
||||
#endif
|
||||
Reference in New Issue
Block a user