2020-09-16 20:14:35 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
2020-09-16 20:14:35 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// Platform.cpp
|
|
|
|
//
|
|
|
|
// Platform-specific functions.
|
|
|
|
//
|
|
|
|
|
2020-06-21 10:26:21 +00:00
|
|
|
#include "Platform.h"
|
2020-07-08 15:01:47 +00:00
|
|
|
|
|
|
|
#include "renderers/Renderer.h"
|
2020-07-07 19:25:15 +00:00
|
|
|
#include "utils/StringUtil.h"
|
2020-07-08 15:01:47 +00:00
|
|
|
#include "AudioManager.h"
|
|
|
|
#include "Log.h"
|
|
|
|
#include "MameNames.h"
|
2020-07-19 20:08:14 +00:00
|
|
|
#include "Settings.h"
|
2015-02-22 03:35:50 +00:00
|
|
|
|
2020-06-26 16:03:55 +00:00
|
|
|
#include <SDL2/SDL_events.h>
|
|
|
|
|
2020-08-19 20:02:42 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <array>
|
|
|
|
#endif
|
|
|
|
|
2020-12-28 22:23:01 +00:00
|
|
|
#if !defined(_WIN64)
|
2018-01-27 20:04:08 +00:00
|
|
|
#include <unistd.h>
|
2020-12-28 22:23:01 +00:00
|
|
|
#endif
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <fcntl.h>
|
2013-05-13 19:53:28 +00:00
|
|
|
|
2020-05-15 15:58:25 +00:00
|
|
|
int runRebootCommand()
|
2014-08-02 19:19:57 +00:00
|
|
|
{
|
2020-08-23 17:17:06 +00:00
|
|
|
#if defined(_WIN64)
|
2020-06-21 12:25:28 +00:00
|
|
|
return system("shutdown -r -t 0");
|
2020-08-23 17:17:06 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
// This will probably never be used as macOS requires root privileges to reboot.
|
|
|
|
return system("shutdown -r now");
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
return system("shutdown --reboot now");
|
2014-08-02 19:19:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:58:25 +00:00
|
|
|
int runPoweroffCommand()
|
2014-08-02 19:19:57 +00:00
|
|
|
{
|
2020-08-23 17:17:06 +00:00
|
|
|
#if defined(_WIN64)
|
2020-06-21 12:25:28 +00:00
|
|
|
return system("shutdown -s -t 0");
|
2020-08-23 17:17:06 +00:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
// This will probably never be used as macOS requires root privileges to power off.
|
|
|
|
return system("shutdown now");
|
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
return system("shutdown --poweroff now");
|
2014-08-02 19:19:57 +00:00
|
|
|
#endif
|
2015-02-21 22:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int runSystemCommand(const std::string& cmd_utf8)
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-06-21 12:25:28 +00:00
|
|
|
// On Windows we use _wsystem to support non-ASCII paths
|
2020-07-07 19:25:15 +00:00
|
|
|
// which requires converting from UTF-8 to a wstring.
|
2020-07-10 16:32:23 +00:00
|
|
|
std::wstring wchar_str = Utils::String::stringToWideString(cmd_utf8);
|
2020-06-21 12:25:28 +00:00
|
|
|
return _wsystem(wchar_str.c_str());
|
2020-07-07 19:25:15 +00:00
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
return system(cmd_utf8.c_str());
|
2020-07-07 19:25:15 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int runSystemCommand(const std::wstring& cmd_utf16)
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-07 19:25:15 +00:00
|
|
|
return _wsystem(cmd_utf16.c_str());
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-24 19:13:33 +00:00
|
|
|
int launchGameUnix(const std::string& cmd_utf8, bool runInBackground)
|
2020-07-07 19:25:15 +00:00
|
|
|
{
|
2020-08-19 20:02:42 +00:00
|
|
|
#if defined(__unix__) || defined (__APPLE__)
|
2021-03-24 19:13:33 +00:00
|
|
|
std::string command = std::string(cmd_utf8) + " 2>&1 &";
|
|
|
|
|
|
|
|
// Launching games while keeping ES-DE running in the background is very crude as for
|
|
|
|
// 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 in the background, no command output will be written to the log file";
|
|
|
|
return system(command.c_str());
|
|
|
|
}
|
2020-07-18 21:07:02 +00:00
|
|
|
|
|
|
|
FILE* commandPipe;
|
|
|
|
std::array<char, 128> buffer;
|
|
|
|
std::string commandOutput;
|
|
|
|
int returnValue;
|
|
|
|
|
2021-01-17 23:19:27 +00:00
|
|
|
if (!(commandPipe = reinterpret_cast<FILE*>(popen(command.c_str(), "r")))) {
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "Couldn't open pipe to command.";
|
2020-07-18 21:07:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(buffer.data(), buffer.size(), commandPipe) != nullptr) {
|
|
|
|
commandOutput += buffer.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
returnValue = pclose(commandPipe);
|
|
|
|
// We need to shift the return value as it contains some flags (which we don't need).
|
|
|
|
returnValue >>= 8;
|
|
|
|
|
|
|
|
// Remove any trailing newline from the command output.
|
|
|
|
if (commandOutput.size()) {
|
|
|
|
if (commandOutput.back() == '\n')
|
|
|
|
commandOutput.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (returnValue) {
|
2021-03-24 19:13:33 +00:00
|
|
|
LOG(LogError) << "launchGameUnix - return value " <<
|
2020-07-18 21:07:02 +00:00
|
|
|
std::to_string(returnValue) + ":";
|
|
|
|
if (commandOutput.size())
|
|
|
|
LOG(LogError) << commandOutput;
|
|
|
|
else
|
2021-03-24 19:13:33 +00:00
|
|
|
LOG(LogError) << "No error output provided by game or emulator";
|
2020-07-18 21:07:02 +00:00
|
|
|
}
|
|
|
|
else if (commandOutput.size()) {
|
2021-03-24 19:13:33 +00:00
|
|
|
LOG(LogDebug) << "Platform::launchGameUnix():";
|
2020-07-18 21:07:02 +00:00
|
|
|
LOG(LogDebug) << "Output from launched game:\n" << commandOutput;
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
|
|
|
|
#else // __unix__
|
2020-07-07 19:25:15 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-03-24 19:13:33 +00:00
|
|
|
int launchGameWindows(const std::wstring& cmd_utf16, bool runInBackground)
|
2020-07-07 19:25:15 +00:00
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-07 19:25:15 +00:00
|
|
|
STARTUPINFOW si {};
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
bool processReturnValue = true;
|
|
|
|
DWORD errorCode = 0;
|
|
|
|
|
|
|
|
processReturnValue = CreateProcessW(
|
2020-09-16 20:14:35 +00:00
|
|
|
nullptr, // No application name (use command line).
|
|
|
|
const_cast<wchar_t*>(cmd_utf16.c_str()), // Command line.
|
|
|
|
nullptr, // Process attributes.
|
|
|
|
nullptr, // Thread attributes.
|
|
|
|
FALSE, // Handles inheritance.
|
|
|
|
0, // Creation flags.
|
|
|
|
nullptr, // Use parent's environment block.
|
|
|
|
nullptr, // Use parent's starting directory.
|
|
|
|
&si, // Pointer to the STARTUPINFOW structure.
|
|
|
|
&pi); // Pointer to the PROCESS_INFORMATION structure.
|
2020-07-07 19:25:15 +00:00
|
|
|
|
2021-03-24 19:13:33 +00:00
|
|
|
// Unfortunately suspending ES-DE and resuming when the game/emulator process has exited
|
|
|
|
// doesn't work reliably on Windows, so we may need to keep ES-DE running in the background
|
|
|
|
// while the game is launched. I'm not sure if there is a workaround for this, but on most
|
|
|
|
// Windows installations it seems to work fine so we'll let the user choose via a menu option.
|
|
|
|
// Possibly the issue is specific to Windows 8.
|
|
|
|
// Running in the background is also required for Steam games as ES-DE would otherwise
|
|
|
|
// wait forever for Steam to exit unless it was already running when the game was launched.
|
|
|
|
if (!runInBackground) {
|
2020-07-19 20:08:14 +00:00
|
|
|
// Wait for the child process to exit.
|
|
|
|
WaitForSingleObject(pi.hThread, INFINITE);
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
}
|
2020-07-18 21:07:02 +00:00
|
|
|
|
2020-07-07 19:25:15 +00:00
|
|
|
// If the return value is false, then something failed.
|
|
|
|
if (!processReturnValue) {
|
|
|
|
LPWSTR pBuffer = nullptr;
|
|
|
|
|
|
|
|
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
|
|
|
nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2020-09-16 20:14:35 +00:00
|
|
|
reinterpret_cast<LPWSTR>(&pBuffer), 0, nullptr);
|
2020-07-07 19:25:15 +00:00
|
|
|
|
|
|
|
errorCode = GetLastError();
|
|
|
|
|
2020-07-10 16:32:23 +00:00
|
|
|
std::string errorMessage = Utils::String::wideStringToString(pBuffer);
|
2020-07-07 19:25:15 +00:00
|
|
|
// Remove trailing newline from the error message.
|
2020-07-18 21:07:02 +00:00
|
|
|
if (errorMessage.size()) {
|
|
|
|
if (errorMessage.back() == '\n')
|
|
|
|
errorMessage.pop_back();
|
|
|
|
if (errorMessage.size()) {
|
|
|
|
if (errorMessage.back() == '\r')
|
|
|
|
errorMessage.pop_back();
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 19:25:15 +00:00
|
|
|
|
2021-03-24 19:13:33 +00:00
|
|
|
LOG(LogError) << "launchGameWindows - system error code " <<
|
2020-07-07 19:25:15 +00:00
|
|
|
errorCode << ": " << errorMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close process and thread handles.
|
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
|
|
|
|
|
|
|
return errorCode;
|
2020-07-18 21:07:02 +00:00
|
|
|
|
2020-07-07 19:25:15 +00:00
|
|
|
#else // _WIN64
|
|
|
|
return 0;
|
|
|
|
#endif
|
2016-01-17 04:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 11:21:44 +00:00
|
|
|
unsigned int getTaskbarState()
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-18 11:21:44 +00:00
|
|
|
APPBARDATA barData;
|
|
|
|
barData.cbSize = sizeof(APPBARDATA);
|
2020-09-16 20:14:35 +00:00
|
|
|
return static_cast<UINT>(SHAppBarMessage(ABM_GETSTATE, &barData));
|
2020-07-18 11:21:44 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void hideTaskbar()
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-18 11:21:44 +00:00
|
|
|
APPBARDATA barData;
|
|
|
|
barData.cbSize = sizeof(APPBARDATA);
|
|
|
|
barData.lParam = ABS_AUTOHIDE;
|
|
|
|
SHAppBarMessage(ABM_SETSTATE, &barData);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void revertTaskbarState(unsigned int& state)
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2020-07-18 11:21:44 +00:00
|
|
|
APPBARDATA barData;
|
|
|
|
barData.cbSize = sizeof(APPBARDATA);
|
|
|
|
barData.lParam = state;
|
|
|
|
SHAppBarMessage(ABM_SETSTATE, &barData);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-08-14 23:50:23 +00:00
|
|
|
QuitMode quitMode = QuitMode::QUIT;
|
|
|
|
|
|
|
|
int quitES(QuitMode mode)
|
2016-01-17 04:24:28 +00:00
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
quitMode = mode;
|
2019-08-14 23:50:23 +00:00
|
|
|
|
2020-10-17 12:32:08 +00:00
|
|
|
SDL_Event quit;
|
|
|
|
quit.type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(&quit);
|
2020-06-21 12:25:28 +00:00
|
|
|
return 0;
|
2016-01-17 04:24:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 15:01:47 +00:00
|
|
|
void emergencyShutdown()
|
|
|
|
{
|
2020-07-26 21:30:45 +00:00
|
|
|
LOG(LogError) << "Critical - Performing emergency shutdown...";
|
2020-07-08 15:01:47 +00:00
|
|
|
|
|
|
|
MameNames::deinit();
|
|
|
|
// Most of the SDL deinitialization is done in Renderer.
|
|
|
|
Renderer::deinit();
|
|
|
|
Log::flush();
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:24:28 +00:00
|
|
|
void touch(const std::string& filename)
|
|
|
|
{
|
2020-08-23 15:04:30 +00:00
|
|
|
#if defined(_WIN64)
|
2021-03-15 16:22:45 +00:00
|
|
|
FILE* fp;
|
|
|
|
fopen_s(&fp, filename.c_str(), "ab+");
|
2020-06-28 16:39:18 +00:00
|
|
|
if (fp != nullptr)
|
2020-06-21 12:25:28 +00:00
|
|
|
fclose(fp);
|
2016-11-17 20:37:44 +00:00
|
|
|
#else
|
2020-06-21 12:25:28 +00:00
|
|
|
int fd = open(filename.c_str(), O_CREAT|O_WRONLY, 0644);
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2016-11-17 20:37:44 +00:00
|
|
|
#endif
|
2019-08-14 23:50:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void processQuitMode()
|
|
|
|
{
|
2020-06-21 12:25:28 +00:00
|
|
|
switch (quitMode) {
|
|
|
|
case QuitMode::REBOOT:
|
|
|
|
LOG(LogInfo) << "Rebooting system";
|
|
|
|
runRebootCommand();
|
|
|
|
break;
|
|
|
|
case QuitMode::POWEROFF:
|
|
|
|
LOG(LogInfo) << "Powering off system";
|
|
|
|
runPoweroffCommand();
|
|
|
|
break;
|
2020-06-25 17:52:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
2020-06-21 12:25:28 +00:00
|
|
|
}
|
2019-08-14 23:50:23 +00:00
|
|
|
}
|