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 // Default to RUN_IN_PLACE style relative paths
127 std::string path_share = "..";
128 std::string path_user = "..";
130 std::string getDataPath(const char *subpath)
132 return path_share + DIR_DELIM + subpath;
135 void pathRemoveFile(char *path, char delim)
137 // Remove filename and path delimiter
139 for(i = strlen(path)-1; i>=0; i--)
147 void initializePaths()
151 Use relative paths if RUN_IN_PLACE
154 infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
162 const DWORD buflen = 1000;
166 // Find path of executable and set path_share relative to it
167 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
168 assert(len < buflen);
169 pathRemoveFile(buf, '\\');
171 path_share = std::string(buf) + "\\..";
172 path_user = std::string(buf) + "\\..";
181 memset(buf, 0, BUFSIZ);
182 // Get path to executable
183 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
185 pathRemoveFile(buf, '/');
187 path_share = std::string(buf) + "/..";
188 path_user = std::string(buf) + "/..";
193 #elif defined(__APPLE__) || defined(__FreeBSD__)
195 //TODO: Get path of executable. This assumes working directory is bin/
196 dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
198 path_share = std::string("..");
199 path_user = std::string("..");
203 #else // RUN_IN_PLACE
206 Use platform-specific paths otherwise
209 infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
217 const DWORD buflen = 1000;
221 // Find path of executable and set path_share relative to it
222 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
223 assert(len < buflen);
224 pathRemoveFile(buf, '\\');
227 path_share = std::string(buf) + "\\..";
229 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
230 len = GetEnvironmentVariable("APPDATA", buf, buflen);
231 assert(len < buflen);
232 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
241 memset(buf, 0, BUFSIZ);
242 // Get path to executable
243 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
245 pathRemoveFile(buf, '/');
247 path_share = std::string(buf) + "/../share/" + PROJECT_NAME;
248 //path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
249 if (!fs::PathExists(path_share)) {
250 dstream<<"WARNING: system-wide share not found at \""<<path_share<<"\"";
251 path_share = std::string(buf) + "/..";
252 dstream<<"WARNING: Using \""<<path_share<<"\" instead."<<std::endl;
255 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
260 #elif defined(__APPLE__)
264 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
265 CFBundleRef main_bundle = CFBundleGetMainBundle();
266 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
268 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
270 dstream<<"Bundle resource path: "<<path<<std::endl;
272 path_share = std::string(path) + "/share";
277 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
279 CFRelease(resources_url);
281 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
283 #elif defined(__FreeBSD__)
285 path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
286 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
290 #endif // RUN_IN_PLACE
293 } //namespace porting