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