2013-05-13 19:53:28 +00:00
|
|
|
#include "platform.h"
|
2013-05-13 20:06:18 +00:00
|
|
|
#include <stdlib.h>
|
2013-07-03 12:27:06 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
2016-01-17 04:24:28 +00:00
|
|
|
#include <SDL.h>
|
2014-04-05 17:48:38 +00:00
|
|
|
#include <iostream>
|
2016-01-17 04:24:28 +00:00
|
|
|
#include <fcntl.h>
|
2015-02-22 03:35:50 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2015-02-21 22:48:56 +00:00
|
|
|
#include <codecvt>
|
2015-02-22 03:35:50 +00:00
|
|
|
#endif
|
2013-05-13 19:53:28 +00:00
|
|
|
|
|
|
|
std::string getHomePath()
|
|
|
|
{
|
2013-05-16 19:29:41 +00:00
|
|
|
std::string homePath;
|
|
|
|
|
2014-03-14 03:14:49 +00:00
|
|
|
// this should give you something like "/home/YOUR_USERNAME" on Linux and "C:\Users\YOUR_USERNAME\" on Windows
|
2013-05-16 19:29:41 +00:00
|
|
|
const char * envHome = getenv("HOME");
|
2014-03-14 03:14:49 +00:00
|
|
|
if(envHome != nullptr)
|
|
|
|
{
|
2013-05-16 19:29:41 +00:00
|
|
|
homePath = envHome;
|
|
|
|
}
|
2014-03-14 03:14:49 +00:00
|
|
|
|
2013-05-16 19:29:41 +00:00
|
|
|
#ifdef WIN32
|
2014-06-05 19:56:41 +00:00
|
|
|
// but does not seem to work for Windows XP or Vista, so try something else
|
2013-05-16 19:29:41 +00:00
|
|
|
if (homePath.empty()) {
|
|
|
|
const char * envDir = getenv("HOMEDRIVE");
|
|
|
|
const char * envPath = getenv("HOMEPATH");
|
|
|
|
if (envDir != nullptr && envPath != nullptr) {
|
|
|
|
homePath = envDir;
|
|
|
|
homePath += envPath;
|
2013-06-14 15:16:16 +00:00
|
|
|
|
|
|
|
for(unsigned int i = 0; i < homePath.length(); i++)
|
|
|
|
if(homePath[i] == '\\')
|
|
|
|
homePath[i] = '/';
|
2013-05-16 19:29:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-14 03:14:49 +00:00
|
|
|
// convert path to generic directory seperators
|
2013-07-03 12:27:06 +00:00
|
|
|
boost::filesystem::path genericPath(homePath);
|
|
|
|
return genericPath.generic_string();
|
2013-05-13 19:53:28 +00:00
|
|
|
}
|
2014-08-02 19:19:57 +00:00
|
|
|
|
|
|
|
int runShutdownCommand()
|
|
|
|
{
|
|
|
|
#ifdef WIN32 // windows
|
|
|
|
return system("shutdown -s -t 0");
|
|
|
|
#else // osx / linux
|
|
|
|
return system("sudo shutdown -h now");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int runRestartCommand()
|
|
|
|
{
|
|
|
|
#ifdef WIN32 // windows
|
|
|
|
return system("shutdown -r -t 0");
|
|
|
|
#else // osx / linux
|
|
|
|
return system("sudo shutdown -r now");
|
|
|
|
#endif
|
2015-02-21 22:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int runSystemCommand(const std::string& cmd_utf8)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
// on Windows we use _wsystem to support non-ASCII paths
|
|
|
|
// which requires converting from utf8 to a wstring
|
|
|
|
typedef std::codecvt_utf8<wchar_t> convert_type;
|
|
|
|
std::wstring_convert<convert_type, wchar_t> converter;
|
|
|
|
std::wstring wchar_str = converter.from_bytes(cmd_utf8);
|
|
|
|
return _wsystem(wchar_str.c_str());
|
|
|
|
#else
|
|
|
|
return system(cmd_utf8.c_str());
|
|
|
|
#endif
|
2016-01-17 04:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int quitES(const std::string& filename)
|
|
|
|
{
|
|
|
|
touch(filename);
|
|
|
|
SDL_Event* quit = new SDL_Event();
|
|
|
|
quit->type = SDL_QUIT;
|
|
|
|
SDL_PushEvent(quit);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void touch(const std::string& filename)
|
|
|
|
{
|
|
|
|
int fd = open(filename.c_str(), O_CREAT|O_WRONLY, 0644);
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2014-08-02 19:19:57 +00:00
|
|
|
}
|