00595b06ca0c8ce0dd922c8f0283fbbb37f47cde
[oweals/minetest.git] / src / porting.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 /*
21         Random portability stuff
22
23         See comments in porting.h
24 */
25
26 #include "porting.h"
27 #include "config.h"
28 #include "debug.h"
29 #include "filesys.h"
30
31 #ifdef __APPLE__
32         #include "CoreFoundation/CoreFoundation.h"
33 #endif
34
35 namespace porting
36 {
37
38 /*
39         Signal handler (grabs Ctrl-C on POSIX systems)
40 */
41
42 bool g_killed = false;
43
44 bool * signal_handler_killstatus(void)
45 {
46         return &g_killed;
47 }
48
49 #if !defined(_WIN32) // POSIX
50         #include <signal.h>
51
52 void sigint_handler(int sig)
53 {
54         if(g_killed == false)
55         {
56                 dstream<<DTIME<<"INFO: sigint_handler(): "
57                                 <<"Ctrl-C pressed, shutting down."<<std::endl;
58                 
59                 dstream<<DTIME<<"INFO: sigint_handler(): "
60                                 <<"Printing debug stacks"<<std::endl;
61                 debug_stacks_print();
62
63                 g_killed = true;
64         }
65         else
66         {
67                 (void)signal(SIGINT, SIG_DFL);
68         }
69 }
70
71 void signal_handler_init(void)
72 {
73         dstream<<"signal_handler_init()"<<std::endl;
74         (void)signal(SIGINT, sigint_handler);
75 }
76
77 #else // _WIN32
78         #include <signal.h>
79         #include <windows.h>
80         
81         BOOL WINAPI event_handler(DWORD sig)
82         {
83                 switch(sig)
84                 {
85                 case CTRL_C_EVENT:
86                 case CTRL_CLOSE_EVENT:
87                 case CTRL_LOGOFF_EVENT:
88                 case CTRL_SHUTDOWN_EVENT:
89
90                         if(g_killed == false)
91                         {
92                                 dstream<<DTIME<<"INFO: event_handler(): "
93                                                 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
94                                 dstream<<DTIME<<"INFO: event_handler(): "
95                                                 <<"Printing debug stacks"<<std::endl;
96                                 debug_stacks_print();
97
98                                 g_killed = true;
99                         }
100                         else
101                         {
102                                 (void)signal(SIGINT, SIG_DFL);
103                         }
104
105                         break;
106                 case CTRL_BREAK_EVENT:
107                         break;
108                 }
109                 
110                 return TRUE;
111         }
112         
113 void signal_handler_init(void)
114 {
115         dstream<<"signal_handler_init()"<<std::endl;
116         SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
117 }
118
119 #endif
120
121 /*
122         Path mangler
123 */
124
125 std::string path_data = ".." DIR_DELIM "data";
126 std::string path_userdata = "..";
127
128 std::string getDataPath(const char *subpath)
129 {
130         return path_data + DIR_DELIM + subpath;
131 }
132
133 void pathRemoveFile(char *path, char delim)
134 {
135         // Remove filename and path delimiter
136         int i;
137         for(i = strlen(path)-1; i>=0; i--)
138         {
139                 if(path[i] == delim)
140                         break;
141         }
142         path[i] = 0;
143 }
144
145 void initializePaths()
146 {
147 #ifdef RUN_IN_PLACE
148         /*
149                 Use relative paths if RUN_IN_PLACE
150         */
151
152         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
153
154         /*
155                 Windows
156         */
157         #if defined(_WIN32)
158                 #include <windows.h>
159
160         const DWORD buflen = 1000;
161         char buf[buflen];
162         DWORD len;
163         
164         // Find path of executable and set path_data relative to it
165         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
166         assert(len < buflen);
167         pathRemoveFile(buf, '\\');
168
169         // Use "./bin/../data"
170         path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
171                 
172         // Use "./bin/.."
173         path_userdata = std::string(buf) + DIR_DELIM "..";
174
175         /*
176                 Linux
177         */
178         #elif defined(linux)
179                 #include <unistd.h>
180         
181         char buf[BUFSIZ];
182         memset(buf, 0, BUFSIZ);
183         // Get path to executable
184         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
185         
186         pathRemoveFile(buf, '/');
187
188         // Use "./bin/../data"
189         path_data = std::string(buf) + "/../data";
190                 
191         // Use "./bin/../"
192         path_userdata = std::string(buf) + "/..";
193         
194         /*
195                 OS X
196         */
197         #elif defined(__APPLE__) || defined(__FreeBSD__)
198         
199         //TODO: Get path of executable. This assumes working directory is bin/
200         dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
201                         <<std::endl;
202         path_data = std::string("../data");
203         path_userdata = std::string("..");
204
205         #endif
206
207 #else // RUN_IN_PLACE
208
209         /*
210                 Use platform-specific paths otherwise
211         */
212
213         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
214
215         /*
216                 Windows
217         */
218         #if defined(_WIN32)
219                 #include <windows.h>
220
221         const DWORD buflen = 1000;
222         char buf[buflen];
223         DWORD len;
224         
225         // Find path of executable and set path_data relative to it
226         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
227         assert(len < buflen);
228         pathRemoveFile(buf, '\\');
229         
230         // Use "./bin/../data"
231         path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
232         //path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
233                 
234         // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
235         len = GetEnvironmentVariable("APPDATA", buf, buflen);
236         assert(len < buflen);
237         path_userdata = std::string(buf) + DIR_DELIM + PROJECT_NAME;
238
239         /*
240                 Linux
241         */
242         #elif defined(linux)
243                 #include <unistd.h>
244         
245         char buf[BUFSIZ];
246         memset(buf, 0, BUFSIZ);
247         // Get path to executable
248         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
249         
250         pathRemoveFile(buf, '/');
251
252         path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
253         //path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
254         if (!fs::PathExists(path_data)) {
255                 dstream<<"WARNING: data path " << path_data << " not found!";
256                 path_data = std::string(buf) + "/../data";
257                 dstream<<" Trying " << path_data << std::endl;
258         }
259         
260         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
261
262         /*
263                 OS X
264         */
265         #elif defined(__APPLE__)
266                 #include <unistd.h>
267
268     // Code based on
269     // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
270     CFBundleRef main_bundle = CFBundleGetMainBundle();
271     CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
272     char path[PATH_MAX];
273     if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
274         {
275                 dstream<<"Bundle resource path: "<<path<<std::endl;
276                 //chdir(path);
277                 path_data = std::string(path) + "/data";
278         }
279         else
280     {
281         // error!
282                 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
283     }
284     CFRelease(resources_url);
285         
286         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
287
288         #elif defined(__FreeBSD__)
289
290         path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
291         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
292     
293         #endif
294
295 #endif // RUN_IN_PLACE
296
297         dstream<<"path_data = "<<path_data<<std::endl;
298         dstream<<"path_userdata = "<<path_userdata<<std::endl;
299 }
300
301 } //namespace porting
302