Merge branch 'master' of https://github.com/erlehmann/minetest-delta.git into upstrea...
[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
30 #ifdef __APPLE__
31         #include "CoreFoundation/CoreFoundation.h"
32 #endif
33
34 namespace porting
35 {
36
37 /*
38         Signal handler (grabs Ctrl-C on POSIX systems)
39 */
40
41 bool g_killed = false;
42
43 bool * signal_handler_killstatus(void)
44 {
45         return &g_killed;
46 }
47
48 #if !defined(_WIN32) // POSIX
49         #include <signal.h>
50
51 void sigint_handler(int sig)
52 {
53         if(g_killed == false)
54         {
55                 dstream<<DTIME<<"INFO: sigint_handler(): "
56                                 <<"Ctrl-C pressed, shutting down."<<std::endl;
57                 
58                 dstream<<DTIME<<"INFO: sigint_handler(): "
59                                 <<"Printing debug stacks"<<std::endl;
60                 debug_stacks_print();
61
62                 g_killed = true;
63         }
64         else
65         {
66                 (void)signal(SIGINT, SIG_DFL);
67         }
68 }
69
70 void signal_handler_init(void)
71 {
72         dstream<<"signal_handler_init()"<<std::endl;
73         (void)signal(SIGINT, sigint_handler);
74 }
75
76 #else // _WIN32
77
78 void signal_handler_init(void)
79 {
80         // No-op
81 }
82
83 #endif
84
85 /*
86         Path mangler
87 */
88
89 std::string path_data = "../data";
90 std::string path_userdata = "../";
91
92 void pathRemoveFile(char *path, char delim)
93 {
94         // Remove filename and path delimiter
95         int i;
96         for(i = strlen(path)-1; i>=0; i--)
97         {
98                 if(path[i] == delim)
99                         break;
100         }
101         path[i] = 0;
102 }
103
104 void initializePaths()
105 {
106 #ifdef RUN_IN_PLACE
107         /*
108                 Use relative paths if RUN_IN_PLACE
109         */
110
111         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
112
113         /*
114                 Windows
115         */
116         #if defined(_WIN32)
117                 #include <windows.h>
118
119         const DWORD buflen = 1000;
120         char buf[buflen];
121         DWORD len;
122         
123         // Find path of executable and set path_data relative to it
124         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
125         assert(len < buflen);
126         pathRemoveFile(buf, '\\');
127
128         // Use "./bin/../data"
129         path_data = std::string(buf) + "/../data";
130                 
131         // Use "./bin/../"
132         path_userdata = std::string(buf) + "/../";
133
134         /*
135                 Linux
136         */
137         #elif defined(linux)
138                 #include <unistd.h>
139         
140         char buf[BUFSIZ];
141         memset(buf, 0, BUFSIZ);
142         // Get path to executable
143         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
144         
145         pathRemoveFile(buf, '/');
146
147         // Use "./bin/../data"
148         path_data = std::string(buf) + "/../data";
149                 
150         // Use "./bin/../"
151         path_userdata = std::string(buf) + "/../";
152         
153         /*
154                 OS X
155         */
156         #elif defined(__APPLE__)
157         
158         //TODO: Get path of executable. This assumes working directory is bin/
159         dstream<<"WARNING: Relative path not properly supported on OS X"
160                         <<std::endl;
161         path_data = std::string("../data");
162         path_userdata = std::string("../");
163
164         #endif
165
166 #else // RUN_IN_PLACE
167
168         /*
169                 Use platform-specific paths otherwise
170         */
171
172         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
173
174         /*
175                 Windows
176         */
177         #if defined(_WIN32)
178                 #include <windows.h>
179
180         const DWORD buflen = 1000;
181         char buf[buflen];
182         DWORD len;
183         
184         // Find path of executable and set path_data relative to it
185         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
186         assert(len < buflen);
187         pathRemoveFile(buf, '\\');
188         
189         // Use "./bin/../data"
190         path_data = std::string(buf) + "/../data";
191         //path_data = std::string(buf) + "/../share/" + APPNAME;
192                 
193         // Use "C:\Documents and Settings\user\Application Data\<APPNAME>"
194         len = GetEnvironmentVariable("APPDATA", buf, buflen);
195         assert(len < buflen);
196         path_userdata = std::string(buf) + "/" + APPNAME;
197
198         /*
199                 Linux
200         */
201         #elif defined(linux)
202                 #include <unistd.h>
203         
204         char buf[BUFSIZ];
205         memset(buf, 0, BUFSIZ);
206         // Get path to executable
207         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
208         
209         pathRemoveFile(buf, '/');
210
211         path_data = std::string(buf) + "/../share/" + APPNAME;
212         //path_data = std::string(INSTALL_PREFIX) + "/share/" + APPNAME;
213         
214         path_userdata = std::string(getenv("HOME")) + "/." + APPNAME;
215
216         /*
217                 OS X
218         */
219         #elif defined(__APPLE__)
220                 #include <unistd.h>
221
222     // Code based on
223     // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
224     CFBundleRef main_bundle = CFBundleGetMainBundle();
225     CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
226     char path[PATH_MAX];
227     if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
228         {
229                 dstream<<"Bundle resource path: "<<path<<std::endl;
230                 //chdir(path);
231                 path_data = std::string(path) + "/data";
232         }
233         else
234     {
235         // error!
236                 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
237     }
238     CFRelease(resources_url);
239         
240         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + APPNAME;
241     
242         #endif
243
244 #endif // RUN_IN_PLACE
245
246         dstream<<"path_data = "<<path_data<<std::endl;
247         dstream<<"path_userdata = "<<path_userdata<<std::endl;
248 }
249
250 } //namespace porting
251