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
32 #include "CoreFoundation/CoreFoundation.h"
39 Signal handler (grabs Ctrl-C on POSIX systems)
42 bool g_killed = false;
44 bool * signal_handler_killstatus(void)
49 #if !defined(_WIN32) // POSIX
52 void sigint_handler(int sig)
56 dstream<<DTIME<<"INFO: sigint_handler(): "
57 <<"Ctrl-C pressed, shutting down."<<std::endl;
59 // Comment out for less clutter when testing scripts
60 /*dstream<<DTIME<<"INFO: sigint_handler(): "
61 <<"Printing debug stacks"<<std::endl;
62 debug_stacks_print();*/
68 (void)signal(SIGINT, SIG_DFL);
72 void signal_handler_init(void)
74 (void)signal(SIGINT, sigint_handler);
81 BOOL WINAPI event_handler(DWORD sig)
86 case CTRL_CLOSE_EVENT:
87 case CTRL_LOGOFF_EVENT:
88 case CTRL_SHUTDOWN_EVENT:
92 dstream<<DTIME<<"INFO: event_handler(): "
93 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
94 // Comment out for less clutter when testing scripts
95 /*dstream<<DTIME<<"INFO: event_handler(): "
96 <<"Printing debug stacks"<<std::endl;
97 debug_stacks_print();*/
103 (void)signal(SIGINT, SIG_DFL);
107 case CTRL_BREAK_EVENT:
114 void signal_handler_init(void)
116 SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
125 std::string path_share = ".." DIR_DELIM "share";
126 std::string path_user = ".." DIR_DELIM "user";
128 std::string getDataPath(const char *subpath)
130 return path_share + DIR_DELIM + subpath;
133 void pathRemoveFile(char *path, char delim)
135 // Remove filename and path delimiter
137 for(i = strlen(path)-1; i>=0; i--)
145 void initializePaths()
149 Use relative paths if RUN_IN_PLACE
152 dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
160 const DWORD buflen = 1000;
164 // Find path of executable and set path_share relative to it
165 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
166 assert(len < buflen);
167 pathRemoveFile(buf, '\\');
169 path_share = std::string(buf) + "\\..\\share";
170 path_user = std::string(buf) + "\\..\\user";
179 memset(buf, 0, BUFSIZ);
180 // Get path to executable
181 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
183 pathRemoveFile(buf, '/');
185 path_share = std::string(buf) + "/../share";
186 path_user = std::string(buf) + "/../user";
191 #elif defined(__APPLE__) || defined(__FreeBSD__)
193 //TODO: Get path of executable. This assumes working directory is bin/
194 dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
196 path_share = std::string("../share");
197 path_user = std::string("../user");
201 #else // RUN_IN_PLACE
204 Use platform-specific paths otherwise
207 dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
215 const DWORD buflen = 1000;
219 // Find path of executable and set path_share relative to it
220 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
221 assert(len < buflen);
222 pathRemoveFile(buf, '\\');
224 // Use ".\bin\..\share"
225 path_share = std::string(buf) + "\\..\\share";
227 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
228 len = GetEnvironmentVariable("APPDATA", buf, buflen);
229 assert(len < buflen);
230 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
239 memset(buf, 0, BUFSIZ);
240 // Get path to executable
241 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
243 pathRemoveFile(buf, '/');
245 path_share = std::string(buf) + "/../share/" + PROJECT_NAME;
246 //path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
247 if (!fs::PathExists(path_share)) {
248 dstream<<"WARNING: data path " << path_share << " not found!";
249 path_share = std::string(buf) + "/../data";
250 dstream<<" Trying " << path_share << std::endl;
253 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
258 #elif defined(__APPLE__)
262 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
263 CFBundleRef main_bundle = CFBundleGetMainBundle();
264 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
266 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
268 dstream<<"Bundle resource path: "<<path<<std::endl;
270 path_share = std::string(path) + "/share";
275 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
277 CFRelease(resources_url);
279 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
281 #elif defined(__FreeBSD__)
283 path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
284 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
288 #endif // RUN_IN_PLACE
291 } //namespace porting