Moved all Platform functions to the utility namespace.

This commit is contained in:
Leon Styhre 2022-01-10 18:43:17 +01:00
parent 5a085c585e
commit 2d149c5161
8 changed files with 263 additions and 256 deletions

View file

@ -1107,10 +1107,10 @@ void FileData::launchGame(Window* window)
// Possibly keep ES-DE running in the background while the game is launched.
#if defined(_WIN64)
returnValue =
launchGameWindows(Utils::String::stringToWideString(command), runInBackground, hideWindow);
returnValue = Utils::Platform::launchGameWindows(Utils::String::stringToWideString(command),
runInBackground, hideWindow);
#else
returnValue = launchGameUnix(command, runInBackground);
returnValue = Utils::Platform::launchGameUnix(command, runInBackground);
#endif
// Notify the user in case of a failed game launch using a popup window.
if (returnValue != 0) {

View file

@ -1135,7 +1135,7 @@ void GuiMenu::openQuitMenu()
[this] {
Scripting::fireEvent("quit");
close(true);
quitES();
Utils::Platform::quitES();
},
"NO", nullptr));
}
@ -1153,7 +1153,7 @@ void GuiMenu::openQuitMenu()
[this] {
Scripting::fireEvent("quit");
close(true);
quitES();
Utils::Platform::quitES();
},
"NO", nullptr));
});
@ -1170,7 +1170,7 @@ void GuiMenu::openQuitMenu()
[] {
Scripting::fireEvent("quit", "reboot");
Scripting::fireEvent("reboot");
if (quitES(QuitMode::REBOOT) != 0) {
if (Utils::Platform::quitES(Utils::Platform::QuitMode::REBOOT) != 0) {
LOG(LogWarning) << "Reboot terminated with non-zero result!";
}
},
@ -1189,7 +1189,7 @@ void GuiMenu::openQuitMenu()
[] {
Scripting::fireEvent("quit", "poweroff");
Scripting::fireEvent("poweroff");
if (quitES(QuitMode::POWEROFF) != 0) {
if (Utils::Platform::quitES(Utils::Platform::QuitMode::POWEROFF) != 0) {
LOG(LogWarning) << "Power off terminated with non-zero result!";
}
},

View file

@ -563,8 +563,8 @@ int main(int argc, char* argv[])
if (Settings::getInstance()->getBool("HideTaskbar")) {
taskbarStateChanged = true;
taskbarState = getTaskbarState();
hideTaskbar();
taskbarState = Utils::Platform::getTaskbarState();
Utils::Platform::hideTaskbar();
}
#endif
@ -692,10 +692,10 @@ int main(int argc, char* argv[])
#if defined(_WIN64)
// If the taskbar state was changed (taskbar was hidden), then revert it.
if (taskbarStateChanged)
revertTaskbarState(taskbarState);
Utils::Platform::revertTaskbarState(taskbarState);
#endif
processQuitMode();
Utils::Platform::processQuitMode();
LOG(LogInfo) << "EmulationStation cleanly shutting down";

View file

@ -239,7 +239,7 @@ void InputManager::doOnFinish()
LOG(LogInfo) << " " << tocall;
std::cout << "==============================================\n"
"input config finish command:\n";
int exitCode = runSystemCommand(tocall);
int exitCode = Utils::Platform::runSystemCommand(tocall);
std::cout << "==============================================\n";
if (exitCode != 0) {

View file

@ -3,7 +3,7 @@
// EmulationStation Desktop Edition
// Platform.cpp
//
// Platform-specific functions.
// Platform utility functions.
//
#include "Platform.h"
@ -25,8 +25,12 @@
#endif
#include <fcntl.h>
int runRebootCommand()
namespace Utils
{
namespace Platform
{
int runRebootCommand()
{
#if defined(_WIN64)
return system("shutdown -r -t 0");
#elif defined(__APPLE__)
@ -35,10 +39,10 @@ int runRebootCommand()
#else
return system("shutdown --reboot now");
#endif
}
}
int runPoweroffCommand()
{
int runPoweroffCommand()
{
#if defined(_WIN64)
return system("shutdown -s -t 0");
#elif defined(__APPLE__)
@ -47,10 +51,10 @@ int runPoweroffCommand()
#else
return system("shutdown --poweroff now");
#endif
}
}
int runSystemCommand(const std::string& cmd_utf8)
{
int runSystemCommand(const std::string& cmd_utf8)
{
#if defined(_WIN64)
// On Windows we use _wsystem to support non-ASCII paths
// which requires converting from UTF-8 to a wstring.
@ -59,19 +63,19 @@ int runSystemCommand(const std::string& cmd_utf8)
#else
return system(cmd_utf8.c_str());
#endif
}
}
int runSystemCommand(const std::wstring& cmd_utf16)
{
int runSystemCommand(const std::wstring& cmd_utf16)
{
#if defined(_WIN64)
return _wsystem(cmd_utf16.c_str());
#else
return 0;
#endif
}
}
int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
{
int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
{
#if defined(__unix__) || defined(__APPLE__)
std::string command = std::string(cmd_utf8) + " 2>&1 &";
@ -79,7 +83,8 @@ int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
// instance no output from the command is captured and no real error handling is
// implemented. It should therefore only be used when absolutely necessary.
if (runInBackground) {
LOG(LogDebug) << "Platform::launchGameUnix(): Launching game while keeping ES-DE running "
LOG(LogDebug)
<< "Platform::launchGameUnix(): Launching game while keeping ES-DE running "
"in the background, no command output will be written to the log file";
return system(command.c_str());
}
@ -101,8 +106,8 @@ int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
returnValue = pclose(commandPipe);
#if defined(_RPI_)
// Hack to avoid that the application window occasionally loses focus when returning from
// a game, which only seems to happen on Raspberry Pi OS 10.
// Hack to avoid that the application window occasionally loses focus when returning
// from a game, which only seems to happen on Raspberry Pi OS 10.
SDL_Delay(50);
SDL_SetWindowInputFocus(Renderer::getSDLWindow());
#endif
@ -117,7 +122,8 @@ int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
}
if (returnValue) {
LOG(LogError) << "launchGameUnix - return value " << std::to_string(returnValue) + ":";
LOG(LogError) << "launchGameUnix - return value "
<< std::to_string(returnValue) + ":";
if (commandOutput.size())
LOG(LogError) << commandOutput;
else
@ -133,18 +139,18 @@ int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
#else // __unix__
return 0;
#endif
}
}
int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool hideWindow)
{
int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool hideWindow)
{
#if defined(_WIN64)
STARTUPINFOW si{};
PROCESS_INFORMATION pi;
si.cb = sizeof(si);
if (hideWindow) {
// Optionally hide the window. This is intended primarily for hiding console windows when
// launching scripts (used for example by Steam games and source ports).
// Optionally hide the window. This is intended primarily for hiding console windows
// when launching scripts (used for example by Steam games and source ports).
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
}
@ -169,12 +175,12 @@ int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool
int width{};
int height{};
// Hack to make the emulator window render correctly when launching games while running in
// full screen mode. If not done, the emulator window will simply be black although the
// game actually works and outputs sounds, accepts input etc. There is sometimes a white
// flash the first time an emulator is started during the program session and a white
// single-pixel line will be visible at the bottom of the screen while the game is loading.
// But it's at least a tolerable workaround.
// Hack to make the emulator window render correctly when launching games while
// running in full screen mode. If not done, the emulator window will simply be
// black although the game actually works and outputs sounds, accepts input etc.
// There is sometimes a white flash the first time an emulator is started during the
// program session and a white single-pixel line will be visible at the bottom of
// the screen while the game is loading. But it's at least a tolerable workaround.
SDL_GetWindowSize(Renderer::getSDLWindow(), &width, &height);
SDL_SetWindowSize(Renderer::getSDLWindow(), width, height - 1);
SDL_Delay(100);
@ -221,10 +227,10 @@ int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool
#else // _WIN64
return 0;
#endif
}
}
unsigned int getTaskbarState()
{
unsigned int getTaskbarState()
{
#if defined(_WIN64)
APPBARDATA barData;
barData.cbSize = sizeof(APPBARDATA);
@ -232,66 +238,50 @@ unsigned int getTaskbarState()
#else
return 0;
#endif
}
}
void hideTaskbar()
{
void hideTaskbar()
{
#if defined(_WIN64)
APPBARDATA barData;
barData.cbSize = sizeof(APPBARDATA);
barData.lParam = ABS_AUTOHIDE;
SHAppBarMessage(ABM_SETSTATE, &barData);
#endif
}
}
void revertTaskbarState(unsigned int& state)
{
void revertTaskbarState(unsigned int& state)
{
#if defined(_WIN64)
APPBARDATA barData;
barData.cbSize = sizeof(APPBARDATA);
barData.lParam = state;
SHAppBarMessage(ABM_SETSTATE, &barData);
#endif
}
}
QuitMode quitMode = QuitMode::QUIT;
int quitES(QuitMode mode)
{
int quitES(QuitMode mode)
{
quitMode = mode;
SDL_Event quit;
quit.type = SDL_QUIT;
SDL_PushEvent(&quit);
return 0;
}
}
void emergencyShutdown()
{
void emergencyShutdown()
{
LOG(LogError) << "Critical - Performing emergency shutdown...";
Window::getInstance()->deinit();
Log::flush();
exit(EXIT_FAILURE);
}
}
void touch(const std::string& filename)
{
#if defined(_WIN64)
FILE* fp;
fopen_s(&fp, filename.c_str(), "ab+");
if (fp != nullptr)
fclose(fp);
#else
int fd = open(filename.c_str(), O_CREAT | O_WRONLY, 0644);
if (fd >= 0)
close(fd);
#endif
}
void processQuitMode()
{
void processQuitMode()
{
switch (quitMode) {
case QuitMode::REBOOT: {
LOG(LogInfo) << "Rebooting system";
@ -307,4 +297,8 @@ void processQuitMode()
break;
}
}
}
}
} // namespace Platform
} // namespace Utils

View file

@ -3,7 +3,7 @@
// EmulationStation Desktop Edition
// Platform.h
//
// Platform-specific functions.
// Platform utility functions.
//
#ifndef ES_CORE_PLATFORM_H
@ -17,29 +17,42 @@
#include <windows.h>
#endif
enum QuitMode {
namespace Utils
{
namespace Platform
{
enum QuitMode {
QUIT = 0, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
REBOOT = 1,
POWEROFF = 2
};
};
// Uses UTF-8 for Unix and does a UTF-16/wstring conversion for Windows.
int runSystemCommand(const std::string& cmd_utf8);
// Windows specific UTF-16/wstring function. (FOR FUTURE USE)
int runSystemCommand(const std::wstring& cmd_utf16);
int runRebootCommand();
int runPoweroffCommand();
int launchGameUnix(const std::string& cmd_utf8, bool runInBackground);
int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool hideWindow);
// Uses UTF-8 for Unix and does a UTF-16/wstring conversion for Windows.
int runSystemCommand(const std::string& cmd_utf8);
// Windows specific UTF-16/wstring function. (FOR FUTURE USE)
int runSystemCommand(const std::wstring& cmd_utf16);
unsigned int getTaskbarState();
void hideTaskbar();
void revertTaskbarState(unsigned int& state);
int launchGameUnix(const std::string& cmd_utf8, bool runInBackground);
int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground, bool hideWindow);
// Clean, normal shutdown.
int quitES(QuitMode mode = QuitMode::QUIT);
unsigned int getTaskbarState();
void hideTaskbar();
void revertTaskbarState(unsigned int& state);
// Immediately shut down the application as cleanly as possible.
void emergencyShutdown();
void processQuitMode();
// Clean, normal shutdown.
int quitES(QuitMode mode = QuitMode::QUIT);
// Immediately shut down the application as cleanly as possible.
void emergencyShutdown();
void processQuitMode();
inline static QuitMode quitMode = QuitMode::QUIT;
} // namespace Platform
} // namespace Utils
#endif // ES_CORE_PLATFORM_H

View file

@ -77,7 +77,7 @@ namespace Scripting
.append(arg4)
.append(arg4Quotation);
LOG(LogDebug) << "Executing: " << script;
runSystemCommand(script);
Utils::Platform::runSystemCommand(script);
}
}
}

View file

@ -79,7 +79,7 @@ std::string ResourceManager::getResourcePath(const std::string& path, bool termi
LOG(LogError) << testExePath;
LOG(LogError) << "Has EmulationStation been properly installed?";
Scripting::fireEvent("quit");
emergencyShutdown();
Utils::Platform::emergencyShutdown();
}
else {
return "";