Add a third log output interface method
[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                 // Comment out for less clutter when testing scripts
60                 /*dstream<<DTIME<<"INFO: sigint_handler(): "
61                                 <<"Printing debug stacks"<<std::endl;
62                 debug_stacks_print();*/
63
64                 g_killed = true;
65         }
66         else
67         {
68                 (void)signal(SIGINT, SIG_DFL);
69         }
70 }
71
72 void signal_handler_init(void)
73 {
74         dstream<<"signal_handler_init()"<<std::endl;
75         (void)signal(SIGINT, sigint_handler);
76 }
77
78 #else // _WIN32
79         #include <signal.h>
80         #include <windows.h>
81         
82         BOOL WINAPI event_handler(DWORD sig)
83         {
84                 switch(sig)
85                 {
86                 case CTRL_C_EVENT:
87                 case CTRL_CLOSE_EVENT:
88                 case CTRL_LOGOFF_EVENT:
89                 case CTRL_SHUTDOWN_EVENT:
90
91                         if(g_killed == false)
92                         {
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();*/
99
100                                 g_killed = true;
101                         }
102                         else
103                         {
104                                 (void)signal(SIGINT, SIG_DFL);
105                         }
106
107                         break;
108                 case CTRL_BREAK_EVENT:
109                         break;
110                 }
111                 
112                 return TRUE;
113         }
114         
115 void signal_handler_init(void)
116 {
117         dstream<<"signal_handler_init()"<<std::endl;
118         SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
119 }
120
121 #endif
122
123 /*
124         Path mangler
125 */
126
127 std::string path_data = ".." DIR_DELIM "data";
128 std::string path_userdata = "..";
129
130 std::string getDataPath(const char *subpath)
131 {
132         return path_data + DIR_DELIM + subpath;
133 }
134
135 void pathRemoveFile(char *path, char delim)
136 {
137         // Remove filename and path delimiter
138         int i;
139         for(i = strlen(path)-1; i>=0; i--)
140         {
141                 if(path[i] == delim)
142                         break;
143         }
144         path[i] = 0;
145 }
146
147 void initializePaths()
148 {
149 #ifdef RUN_IN_PLACE
150         /*
151                 Use relative paths if RUN_IN_PLACE
152         */
153
154         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
155
156         /*
157                 Windows
158         */
159         #if defined(_WIN32)
160                 #include <windows.h>
161
162         const DWORD buflen = 1000;
163         char buf[buflen];
164         DWORD len;
165         
166         // Find path of executable and set path_data relative to it
167         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
168         assert(len < buflen);
169         pathRemoveFile(buf, '\\');
170
171         // Use "./bin/../data"
172         path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
173                 
174         // Use "./bin/.."
175         path_userdata = std::string(buf) + DIR_DELIM "..";
176
177         /*
178                 Linux
179         */
180         #elif defined(linux)
181                 #include <unistd.h>
182         
183         char buf[BUFSIZ];
184         memset(buf, 0, BUFSIZ);
185         // Get path to executable
186         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
187         
188         pathRemoveFile(buf, '/');
189
190         // Use "./bin/../data"
191         path_data = std::string(buf) + "/../data";
192                 
193         // Use "./bin/../"
194         path_userdata = std::string(buf) + "/..";
195         
196         /*
197                 OS X
198         */
199         #elif defined(__APPLE__) || defined(__FreeBSD__)
200         
201         //TODO: Get path of executable. This assumes working directory is bin/
202         dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
203                         <<std::endl;
204         path_data = std::string("../data");
205         path_userdata = std::string("..");
206
207         #endif
208
209 #else // RUN_IN_PLACE
210
211         /*
212                 Use platform-specific paths otherwise
213         */
214
215         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
216
217         /*
218                 Windows
219         */
220         #if defined(_WIN32)
221                 #include <windows.h>
222
223         const DWORD buflen = 1000;
224         char buf[buflen];
225         DWORD len;
226         
227         // Find path of executable and set path_data relative to it
228         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
229         assert(len < buflen);
230         pathRemoveFile(buf, '\\');
231         
232         // Use "./bin/../data"
233         path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
234         //path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
235                 
236         // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
237         len = GetEnvironmentVariable("APPDATA", buf, buflen);
238         assert(len < buflen);
239         path_userdata = std::string(buf) + DIR_DELIM + PROJECT_NAME;
240
241         /*
242                 Linux
243         */
244         #elif defined(linux)
245                 #include <unistd.h>
246         
247         char buf[BUFSIZ];
248         memset(buf, 0, BUFSIZ);
249         // Get path to executable
250         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
251         
252         pathRemoveFile(buf, '/');
253
254         path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
255         //path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
256         if (!fs::PathExists(path_data)) {
257                 dstream<<"WARNING: data path " << path_data << " not found!";
258                 path_data = std::string(buf) + "/../data";
259                 dstream<<" Trying " << path_data << std::endl;
260         }
261         
262         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
263
264         /*
265                 OS X
266         */
267         #elif defined(__APPLE__)
268                 #include <unistd.h>
269
270     // Code based on
271     // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
272     CFBundleRef main_bundle = CFBundleGetMainBundle();
273     CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
274     char path[PATH_MAX];
275     if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
276         {
277                 dstream<<"Bundle resource path: "<<path<<std::endl;
278                 //chdir(path);
279                 path_data = std::string(path) + "/data";
280         }
281         else
282     {
283         // error!
284                 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
285     }
286     CFRelease(resources_url);
287         
288         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
289
290         #elif defined(__FreeBSD__)
291
292         path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
293         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
294     
295         #endif
296
297 #endif // RUN_IN_PLACE
298
299         dstream<<"path_data = "<<path_data<<std::endl;
300         dstream<<"path_userdata = "<<path_userdata<<std::endl;
301 }
302
303 } //namespace porting
304