3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Random portability stuff
23 See comments in porting.h
33 #include "CoreFoundation/CoreFoundation.h"
40 Signal handler (grabs Ctrl-C on POSIX systems)
43 bool g_killed = false;
45 bool * signal_handler_killstatus(void)
50 #if !defined(_WIN32) // POSIX
53 void sigint_handler(int sig)
57 dstream<<DTIME<<"INFO: sigint_handler(): "
58 <<"Ctrl-C pressed, shutting down."<<std::endl;
60 // Comment out for less clutter when testing scripts
61 /*dstream<<DTIME<<"INFO: sigint_handler(): "
62 <<"Printing debug stacks"<<std::endl;
63 debug_stacks_print();*/
69 (void)signal(SIGINT, SIG_DFL);
73 void signal_handler_init(void)
75 (void)signal(SIGINT, sigint_handler);
82 BOOL WINAPI event_handler(DWORD sig)
87 case CTRL_CLOSE_EVENT:
88 case CTRL_LOGOFF_EVENT:
89 case CTRL_SHUTDOWN_EVENT:
93 dstream<<DTIME<<"INFO: event_handler(): "
94 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
95 // Comment out for less clutter when testing scripts
96 /*dstream<<DTIME<<"INFO: event_handler(): "
97 <<"Printing debug stacks"<<std::endl;
98 debug_stacks_print();*/
104 (void)signal(SIGINT, SIG_DFL);
108 case CTRL_BREAK_EVENT:
115 void signal_handler_init(void)
117 SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
126 std::string path_share = ".." DIR_DELIM "share";
127 std::string path_user = ".." DIR_DELIM "user";
129 std::string getDataPath(const char *subpath)
131 return path_share + DIR_DELIM + subpath;
134 void pathRemoveFile(char *path, char delim)
136 // Remove filename and path delimiter
138 for(i = strlen(path)-1; i>=0; i--)
146 void initializePaths()
150 Use relative paths if RUN_IN_PLACE
153 infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
161 const DWORD buflen = 1000;
165 // Find path of executable and set path_share relative to it
166 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
167 assert(len < buflen);
168 pathRemoveFile(buf, '\\');
170 path_share = std::string(buf) + "\\..\\share";
171 path_user = std::string(buf) + "\\..\\user";
180 memset(buf, 0, BUFSIZ);
181 // Get path to executable
182 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
184 pathRemoveFile(buf, '/');
186 path_share = std::string(buf) + "/../share";
187 path_user = std::string(buf) + "/../user";
192 #elif defined(__APPLE__) || defined(__FreeBSD__)
194 //TODO: Get path of executable. This assumes working directory is bin/
195 dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
197 path_share = std::string("../share");
198 path_user = std::string("../user");
202 #else // RUN_IN_PLACE
205 Use platform-specific paths otherwise
208 infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
216 const DWORD buflen = 1000;
220 // Find path of executable and set path_share relative to it
221 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
222 assert(len < buflen);
223 pathRemoveFile(buf, '\\');
225 // Use ".\bin\..\share"
226 path_share = std::string(buf) + "\\..\\share";
228 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
229 len = GetEnvironmentVariable("APPDATA", buf, buflen);
230 assert(len < buflen);
231 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
240 memset(buf, 0, BUFSIZ);
241 // Get path to executable
242 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
244 pathRemoveFile(buf, '/');
246 path_share = std::string(buf) + "/../share/" + PROJECT_NAME;
247 //path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
248 if (!fs::PathExists(path_share)) {
249 dstream<<"WARNING: data path " << path_share << " not found!";
250 path_share = std::string(buf) + "/../data";
251 dstream<<" Trying " << path_share << std::endl;
254 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
259 #elif defined(__APPLE__)
263 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
264 CFBundleRef main_bundle = CFBundleGetMainBundle();
265 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
267 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
269 dstream<<"Bundle resource path: "<<path<<std::endl;
271 path_share = std::string(path) + "/share";
276 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
278 CFRelease(resources_url);
280 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
282 #elif defined(__FreeBSD__)
284 path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
285 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
289 #endif // RUN_IN_PLACE
292 } //namespace porting