Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OSystem.cxx
Go to the documentation of this file.
1 #include "OSystem.hxx"
2 #include "LOG.hxx"
3 #include "platform.hxx"
4 
5 #include <ctime>
6 
7 #if defined(CYTOPIA_PLATFORM_LINUX) || defined(CYTOPIA_PLATFORM_HAIKU)
8 #include <limits.h>
9 #include <unistd.h>
10 #include <sys/stat.h>
11 #endif
12 
13 #ifdef CYTOPIA_PLATFORM_LINUX
14 #include <cstdlib>
15 #include <string.h>
16 const char *getDialogCommand()
17 {
18  if (::system(nullptr))
19  {
20  if (::system("which gdialog") == 0)
21  return "gdialog";
22 
23  else if (::system("which kdialog") == 0)
24  return "kdialog";
25  }
26  return nullptr;
27 }
28 #elif defined(CYTOPIA_PLATFORM_MACOSX)
29 #include <cstdlib>
30 #elif defined(CYTOPIA_PLATFORM_WIN)
31 #include <windows.h>
32 #endif
33 
34 void OSystem::error(const std::string &title, const std::string &text)
35 {
36 #if defined(CYTOPIA_PLATFORM_LINUX)
37  const char *dialogCommand = getDialogCommand();
38  if (dialogCommand)
39  {
40  std::string command = dialogCommand;
41  command += " --title \"" + title + "\" --msgbox \"" + text + "\"";
42  int syserror = ::system(command.c_str());
43  if (syserror)
44  {
45  LOG(LOG_DEBUG) << "WARNING: Cant execute command " << command;
46  }
47  }
48 
49  // fail-safe method here, using stdio perhaps, depends on your application
50 #elif defined(CYTOPIA_PLATFORM_WIN)
51  MessageBox(nullptr, text.c_str(), title.c_str(), MB_OK | MB_ICONERROR);
52 #endif
53 }
54 
55 void OSystem::openUrl(const std::string &url, const std::string &prefix)
56 {
57 #ifdef CYTOPIA_PLATFORM_LINUX
58  std::string command = prefix + "xdg-open '" + url + "'";
59  LOG(LOG_DEBUG) << command;
60  ::system(command.c_str());
61 
62 #elif defined(CYTOPIA_PLATFORM_WIN)
63  ShellExecuteA(0, "Open", url.c_str(), 0, 0, SW_SHOW);
64 
65 #elif defined(CYTOPIA_PLATFORM_MACOSX)
66  std::string command = "open \"" + url + "\" &";
67  ::system(command.c_str());
68 #endif
69 }
70 
71 void OSystem::openDir(const std::string &path, const std::string &prefix)
72 {
73  std::string command;
74 
75 #ifdef CYTOPIA_PLATFORM_LINUX
76  command = prefix + "nautilus '" + path + "' &";
77  ::system(command.c_str());
78 #elif defined(CYTOPIA_PLATFORM_WIN)
79  ShellExecute(GetDesktopWindow(), "open", path.c_str(), nullptr, nullptr, SW_SHOWNORMAL);
80 
81 #elif defined(CYTOPIA_PLATFORM_MACOSX)
82  command = "open \"" + path + "\" &";
83  ::system(command.c_str());
84 
85 #endif
86 }
87 
89 {
90 #define RETURN_TRUE(t) \
91  if (type == t) \
92  return true;
93 
94 #ifdef CYTOPIA_PLATFORM_WIN
96 #endif
97 
98 #ifdef CYTOPIA_PLATFORM_LINUX
100 #endif
101 
102 #ifdef CYTOPIA_PLATFORM_UNIX
104 #endif
105 
106 #ifdef CYTOPIA_PLATFORM_ANDROID
108 #endif
109 
110 #ifdef CYTOPIA_PLATFORM_MACOSX
112 #endif
113 
114 #ifdef CYTOPIA_PLATFORM_XBSD
116 #endif
117 
118 #ifdef CYTOPIA_PLATFORM_HAIKU
120 #endif
121 
122 #ifdef CYTOPIA_PLATFORM_BEOS
124 #endif
125 
126  return false;
127 }
128 
130 bool OSystem::isLinux() { return is(Type::linux); }
131 bool OSystem::isUnix() { return is(Type::unix); }
132 bool OSystem::isMac() { return is(Type::macos); }
RETURN_TRUE
#define RETURN_TRUE(t)
OSystem::is
static bool is(Type type)
Definition: OSystem.cxx:88
OSystem::openDir
static void openDir(const std::string &path, const std::string &prefix="")
Definition: OSystem.cxx:71
LOG
Definition: LOG.hxx:32
OSystem::openUrl
static void openUrl(const std::string &url, const std::string &prefix="")
Definition: OSystem.cxx:55
OSystem::Type::macos
@ macos
LOG.hxx
OSystem::Type::android
@ android
OSystem::Type::bsd
@ bsd
OSystem::isMac
static bool isMac()
Definition: OSystem.cxx:132
LOG_DEBUG
@ LOG_DEBUG
Definition: LOG.hxx:26
OSystem::Type::beos
@ beos
OSystem.hxx
OSystem::isAndroid
static bool isAndroid()
Definition: OSystem.cxx:129
OSystem::isLinux
static bool isLinux()
Definition: OSystem.cxx:130
platform.hxx
OSystem::isUnix
static bool isUnix()
Definition: OSystem.cxx:131
OSystem::isWindows
static bool isWindows()
Definition: OSystem.cxx:133
OSystem::Type::unix
@ unix
OSystem::Type
Type
Definition: OSystem.hxx:8
OSystem::Type::windows
@ windows
OSystem::Type::linux
@ linux
OSystem::Type::haiku
@ haiku
string
std::string string
Definition: AudioConfig.hxx:14
OSystem::error
static void error(const std::string &title, const std::string &text)
Definition: OSystem.cxx:34