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