]> git.lizzy.rs Git - dragonfireclient.git/blob - src/porting.cpp
Last set of minor cleanups
[dragonfireclient.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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 #if defined(_WIN32)
27         #include <windows.h>
28 #elif defined(linux)
29         #include <unistd.h>
30 #elif defined(__APPLE__)
31         #include <unistd.h>
32         #include <mach-o/dyld.h>
33 #elif defined(__FreeBSD__)
34         #include <unistd.h>
35         #include <sys/types.h>
36         #include <sys/sysctl.h>
37 #endif
38
39 #include "porting.h"
40 #include "config.h"
41 #include "debug.h"
42 #include "filesys.h"
43 #include "log.h"
44 #include "util/string.h"
45 #include <list>
46
47 #ifdef __APPLE__
48         #include "CoreFoundation/CoreFoundation.h"
49 #endif
50
51 namespace porting
52 {
53
54 /*
55         Signal handler (grabs Ctrl-C on POSIX systems)
56 */
57
58 bool g_killed = false;
59
60 bool * signal_handler_killstatus(void)
61 {
62         return &g_killed;
63 }
64
65 #if !defined(_WIN32) // POSIX
66         #include <signal.h>
67
68 void sigint_handler(int sig)
69 {
70         if(g_killed == false)
71         {
72                 dstream<<DTIME<<"INFO: sigint_handler(): "
73                                 <<"Ctrl-C pressed, shutting down."<<std::endl;
74
75                 // Comment out for less clutter when testing scripts
76                 /*dstream<<DTIME<<"INFO: sigint_handler(): "
77                                 <<"Printing debug stacks"<<std::endl;
78                 debug_stacks_print();*/
79
80                 g_killed = true;
81         }
82         else
83         {
84                 (void)signal(SIGINT, SIG_DFL);
85         }
86 }
87
88 void signal_handler_init(void)
89 {
90         (void)signal(SIGINT, sigint_handler);
91 }
92
93 #else // _WIN32
94         #include <signal.h>
95         #include <windows.h>
96
97         BOOL WINAPI event_handler(DWORD sig)
98         {
99                 switch(sig)
100                 {
101                 case CTRL_C_EVENT:
102                 case CTRL_CLOSE_EVENT:
103                 case CTRL_LOGOFF_EVENT:
104                 case CTRL_SHUTDOWN_EVENT:
105
106                         if(g_killed == false)
107                         {
108                                 dstream<<DTIME<<"INFO: event_handler(): "
109                                                 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
110                                 // Comment out for less clutter when testing scripts
111                                 /*dstream<<DTIME<<"INFO: event_handler(): "
112                                                 <<"Printing debug stacks"<<std::endl;
113                                 debug_stacks_print();*/
114
115                                 g_killed = true;
116                         }
117                         else
118                         {
119                                 (void)signal(SIGINT, SIG_DFL);
120                         }
121
122                         break;
123                 case CTRL_BREAK_EVENT:
124                         break;
125                 }
126
127                 return TRUE;
128         }
129
130 void signal_handler_init(void)
131 {
132         SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
133 }
134
135 #endif
136
137 /*
138         Path mangler
139 */
140
141 // Default to RUN_IN_PLACE style relative paths
142 std::string path_share = "..";
143 std::string path_user = "..";
144
145 std::string getDataPath(const char *subpath)
146 {
147         return path_share + DIR_DELIM + subpath;
148 }
149
150 void pathRemoveFile(char *path, char delim)
151 {
152         // Remove filename and path delimiter
153         int i;
154         for(i = strlen(path)-1; i>=0; i--)
155         {
156                 if(path[i] == delim)
157                         break;
158         }
159         path[i] = 0;
160 }
161
162 bool detectMSVCBuildDir(char *c_path)
163 {
164         std::string path(c_path);
165         const char *ends[] = {"bin\\Release", "bin\\Build", NULL};
166         return (removeStringEnd(path, ends) != "");
167 }
168
169 void initializePaths()
170 {
171 #if RUN_IN_PLACE
172         /*
173                 Use relative paths if RUN_IN_PLACE
174         */
175
176         infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
177
178         /*
179                 Windows
180         */
181         #if defined(_WIN32)
182
183         const DWORD buflen = 1000;
184         char buf[buflen];
185         DWORD len;
186
187         // Find path of executable and set path_share relative to it
188         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
189         assert(len < buflen);
190         pathRemoveFile(buf, '\\');
191
192         if(detectMSVCBuildDir(buf)){
193                 infostream<<"MSVC build directory detected"<<std::endl;
194                 path_share = std::string(buf) + "\\..\\..";
195                 path_user = std::string(buf) + "\\..\\..";
196         }
197         else{
198                 path_share = std::string(buf) + "\\..";
199                 path_user = std::string(buf) + "\\..";
200         }
201
202         /*
203                 Linux
204         */
205         #elif defined(linux)
206
207         char buf[BUFSIZ];
208         memset(buf, 0, BUFSIZ);
209         // Get path to executable
210         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
211
212         pathRemoveFile(buf, '/');
213
214         path_share = std::string(buf) + "/..";
215         path_user = std::string(buf) + "/..";
216
217         /*
218                 OS X
219         */
220         #elif defined(__APPLE__)
221
222         //https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dyld.3.html
223         //TODO: Test this code
224         char buf[BUFSIZ];
225         uint32_t len = sizeof(buf);
226         assert(_NSGetExecutablePath(buf, &len) != 0);
227
228         pathRemoveFile(buf, '/');
229
230         path_share = std::string(buf) + "/..";
231         path_user = std::string(buf) + "/..";
232
233         /*
234                 FreeBSD
235         */
236         #elif defined(__FreeBSD__)
237
238         int mib[4];
239         char buf[BUFSIZ];
240         size_t len = sizeof(buf);
241
242         mib[0] = CTL_KERN;
243         mib[1] = KERN_PROC;
244         mib[2] = KERN_PROC_PATHNAME;
245         mib[3] = -1;
246         assert(sysctl(mib, 4, buf, &len, NULL, 0) != -1);
247
248         pathRemoveFile(buf, '/');
249
250         path_share = std::string(buf) + "/..";
251         path_user = std::string(buf) + "/..";
252
253         #else
254
255         //TODO: Get path of executable. This assumes working directory is bin/
256         dstream<<"WARNING: Relative path not properly supported on this platform"
257                         <<std::endl;
258         path_share = std::string("..");
259         path_user = std::string("..");
260
261         #endif
262
263 #else // RUN_IN_PLACE
264
265         /*
266                 Use platform-specific paths otherwise
267         */
268
269         infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
270
271         /*
272                 Windows
273         */
274         #if defined(_WIN32)
275
276         const DWORD buflen = 1000;
277         char buf[buflen];
278         DWORD len;
279
280         // Find path of executable and set path_share relative to it
281         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
282         assert(len < buflen);
283         pathRemoveFile(buf, '\\');
284
285         // Use ".\bin\.."
286         path_share = std::string(buf) + "\\..";
287
288         // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
289         len = GetEnvironmentVariable("APPDATA", buf, buflen);
290         assert(len < buflen);
291         path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
292
293         /*
294                 Linux
295         */
296         #elif defined(linux)
297
298         // Get path to executable
299         std::string bindir = "";
300         {
301                 char buf[BUFSIZ];
302                 memset(buf, 0, BUFSIZ);
303                 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
304                 pathRemoveFile(buf, '/');
305                 bindir = buf;
306         }
307
308         // Find share directory from these.
309         // It is identified by containing the subdirectory "builtin".
310         std::list<std::string> trylist;
311         std::string static_sharedir = STATIC_SHAREDIR;
312         if(static_sharedir != "" && static_sharedir != ".")
313                 trylist.push_back(static_sharedir);
314         trylist.push_back(bindir + "/../share/" + PROJECT_NAME);
315         trylist.push_back(bindir + "/..");
316
317         for(std::list<std::string>::const_iterator i = trylist.begin();
318                         i != trylist.end(); i++)
319         {
320                 const std::string &trypath = *i;
321                 if(!fs::PathExists(trypath) || !fs::PathExists(trypath + "/builtin")){
322                         dstream<<"WARNING: system-wide share not found at \""
323                                         <<trypath<<"\""<<std::endl;
324                         continue;
325                 }
326                 // Warn if was not the first alternative
327                 if(i != trylist.begin()){
328                         dstream<<"WARNING: system-wide share found at \""
329                                         <<trypath<<"\""<<std::endl;
330                 }
331                 path_share = trypath;
332                 break;
333         }
334
335         path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
336
337         /*
338                 OS X
339         */
340         #elif defined(__APPLE__)
341
342     // Code based on
343     // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
344     CFBundleRef main_bundle = CFBundleGetMainBundle();
345     CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
346     char path[PATH_MAX];
347     if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
348         {
349                 dstream<<"Bundle resource path: "<<path<<std::endl;
350                 //chdir(path);
351                 path_share = std::string(path) + "/share";
352         }
353         else
354     {
355         // error!
356                 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
357     }
358     CFRelease(resources_url);
359
360         path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
361
362         #elif defined(__FreeBSD__)
363
364         path_share = STATIC_SHAREDIR;
365         path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
366
367         #endif
368
369 #endif // RUN_IN_PLACE
370 }
371
372 } //namespace porting
373