Update changelog
[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
79 void signal_handler_init(void)
80 {
81         // No-op
82 }
83
84 #endif
85
86 /*
87         Path mangler
88 */
89
90 std::string path_data = "../data";
91 std::string path_userdata = "../";
92
93 void pathRemoveFile(char *path, char delim)
94 {
95         // Remove filename and path delimiter
96         int i;
97         for(i = strlen(path)-1; i>=0; i--)
98         {
99                 if(path[i] == delim)
100                         break;
101         }
102         path[i] = 0;
103 }
104
105 void initializePaths()
106 {
107 #ifdef RUN_IN_PLACE
108         /*
109                 Use relative paths if RUN_IN_PLACE
110         */
111
112         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
113
114         /*
115                 Windows
116         */
117         #if defined(_WIN32)
118                 #include <windows.h>
119
120         const DWORD buflen = 1000;
121         char buf[buflen];
122         DWORD len;
123         
124         // Find path of executable and set path_data relative to it
125         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
126         assert(len < buflen);
127         pathRemoveFile(buf, '\\');
128
129         // Use "./bin/../data"
130         path_data = std::string(buf) + "/../data";
131                 
132         // Use "./bin/../"
133         path_userdata = std::string(buf) + "/../";
134
135         /*
136                 Linux
137         */
138         #elif defined(linux)
139                 #include <unistd.h>
140         
141         char buf[BUFSIZ];
142         memset(buf, 0, BUFSIZ);
143         // Get path to executable
144         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
145         
146         pathRemoveFile(buf, '/');
147
148         // Use "./bin/../data"
149         path_data = std::string(buf) + "/../data";
150                 
151         // Use "./bin/../"
152         path_userdata = std::string(buf) + "/../";
153         
154         /*
155                 OS X
156         */
157         #elif defined(__APPLE__) || defined(__FreeBSD__)
158         
159         //TODO: Get path of executable. This assumes working directory is bin/
160         dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
161                         <<std::endl;
162         path_data = std::string("../data");
163         path_userdata = std::string("../");
164
165         #endif
166
167 #else // RUN_IN_PLACE
168
169         /*
170                 Use platform-specific paths otherwise
171         */
172
173         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
174
175         /*
176                 Windows
177         */
178         #if defined(_WIN32)
179                 #include <windows.h>
180
181         const DWORD buflen = 1000;
182         char buf[buflen];
183         DWORD len;
184         
185         // Find path of executable and set path_data relative to it
186         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
187         assert(len < buflen);
188         pathRemoveFile(buf, '\\');
189         
190         // Use "./bin/../data"
191         path_data = std::string(buf) + "/../data";
192         //path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
193                 
194         // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
195         len = GetEnvironmentVariable("APPDATA", buf, buflen);
196         assert(len < buflen);
197         path_userdata = std::string(buf) + "/" + PROJECT_NAME;
198
199         /*
200                 Linux
201         */
202         #elif defined(linux)
203                 #include <unistd.h>
204         
205         char buf[BUFSIZ];
206         memset(buf, 0, BUFSIZ);
207         // Get path to executable
208         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
209         
210         pathRemoveFile(buf, '/');
211
212         path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
213         //path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
214         if (!fs::PathExists(path_data)) {
215                 dstream<<"WARNING: data path " << path_data << " not found!";
216                 path_data = std::string(buf) + "/../data";
217                 dstream<<" Trying " << path_data << std::endl;
218         }
219         
220         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
221
222         /*
223                 OS X
224         */
225         #elif defined(__APPLE__)
226                 #include <unistd.h>
227
228     // Code based on
229     // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
230     CFBundleRef main_bundle = CFBundleGetMainBundle();
231     CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
232     char path[PATH_MAX];
233     if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
234         {
235                 dstream<<"Bundle resource path: "<<path<<std::endl;
236                 //chdir(path);
237                 path_data = std::string(path) + "/data";
238         }
239         else
240     {
241         // error!
242                 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
243     }
244     CFRelease(resources_url);
245         
246         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
247
248         #elif defined(__FreeBSD__)
249
250         path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
251         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
252     
253         #endif
254
255 #endif // RUN_IN_PLACE
256
257         dstream<<"path_data = "<<path_data<<std::endl;
258         dstream<<"path_userdata = "<<path_userdata<<std::endl;
259 }
260
261 } //namespace porting
262