]> git.lizzy.rs Git - dragonfireclient.git/blob - src/porting.cpp
Command-line signal handling for Windows
[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
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         #include <signal.h>
79         #include <windows.h>
80         
81         BOOL WINAPI event_handler(DWORD sig)
82         {
83                 switch(sig)
84                 {
85                 case CTRL_C_EVENT:
86                 case CTRL_CLOSE_EVENT:
87                 case CTRL_LOGOFF_EVENT:
88                 case CTRL_SHUTDOWN_EVENT:
89
90                         if(g_killed == false)
91                         {
92                                 dstream<<DTIME<<"INFO: event_handler(): "
93                                                 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
94                                 dstream<<DTIME<<"INFO: event_handler(): "
95                                                 <<"Printing debug stacks"<<std::endl;
96                                 debug_stacks_print();
97
98                                 g_killed = true;
99                         }
100                         else
101                         {
102                                 (void)signal(SIGINT, SIG_DFL);
103                         }
104
105                         break;
106                 case CTRL_BREAK_EVENT:
107                         break;
108                 }
109                 
110                 return TRUE;
111         }
112         
113 void signal_handler_init(void)
114 {
115         dstream<<"signal_handler_init()"<<std::endl;
116         SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
117 }
118
119 #endif
120
121 /*
122         Path mangler
123 */
124
125 std::string path_data = ".." DIR_DELIM "data";
126 std::string path_userdata = "..";
127
128 void pathRemoveFile(char *path, char delim)
129 {
130         // Remove filename and path delimiter
131         int i;
132         for(i = strlen(path)-1; i>=0; i--)
133         {
134                 if(path[i] == delim)
135                         break;
136         }
137         path[i] = 0;
138 }
139
140 void initializePaths()
141 {
142 #ifdef RUN_IN_PLACE
143         /*
144                 Use relative paths if RUN_IN_PLACE
145         */
146
147         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
148
149         /*
150                 Windows
151         */
152         #if defined(_WIN32)
153                 #include <windows.h>
154
155         const DWORD buflen = 1000;
156         char buf[buflen];
157         DWORD len;
158         
159         // Find path of executable and set path_data relative to it
160         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
161         assert(len < buflen);
162         pathRemoveFile(buf, '\\');
163
164         // Use "./bin/../data"
165         path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
166                 
167         // Use "./bin/.."
168         path_userdata = std::string(buf) + DIR_DELIM "..";
169
170         /*
171                 Linux
172         */
173         #elif defined(linux)
174                 #include <unistd.h>
175         
176         char buf[BUFSIZ];
177         memset(buf, 0, BUFSIZ);
178         // Get path to executable
179         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
180         
181         pathRemoveFile(buf, '/');
182
183         // Use "./bin/../data"
184         path_data = std::string(buf) + "/../data";
185                 
186         // Use "./bin/../"
187         path_userdata = std::string(buf) + "/..";
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_data = std::string("../data");
198         path_userdata = std::string("..");
199
200         #endif
201
202 #else // RUN_IN_PLACE
203
204         /*
205                 Use platform-specific paths otherwise
206         */
207
208         dstream<<"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_data relative to it
221         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
222         assert(len < buflen);
223         pathRemoveFile(buf, '\\');
224         
225         // Use "./bin/../data"
226         path_data = std::string(buf) + DIR_DELIM ".." DIR_DELIM "data";
227         //path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
228                 
229         // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
230         len = GetEnvironmentVariable("APPDATA", buf, buflen);
231         assert(len < buflen);
232         path_userdata = std::string(buf) + DIR_DELIM + PROJECT_NAME;
233
234         /*
235                 Linux
236         */
237         #elif defined(linux)
238                 #include <unistd.h>
239         
240         char buf[BUFSIZ];
241         memset(buf, 0, BUFSIZ);
242         // Get path to executable
243         assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
244         
245         pathRemoveFile(buf, '/');
246
247         path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
248         //path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
249         if (!fs::PathExists(path_data)) {
250                 dstream<<"WARNING: data path " << path_data << " not found!";
251                 path_data = std::string(buf) + "/../data";
252                 dstream<<" Trying " << path_data << std::endl;
253         }
254         
255         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
256
257         /*
258                 OS X
259         */
260         #elif defined(__APPLE__)
261                 #include <unistd.h>
262
263     // Code based on
264     // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
265     CFBundleRef main_bundle = CFBundleGetMainBundle();
266     CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
267     char path[PATH_MAX];
268     if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
269         {
270                 dstream<<"Bundle resource path: "<<path<<std::endl;
271                 //chdir(path);
272                 path_data = std::string(path) + "/data";
273         }
274         else
275     {
276         // error!
277                 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
278     }
279     CFRelease(resources_url);
280         
281         path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
282
283         #elif defined(__FreeBSD__)
284
285         path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
286         path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
287     
288         #endif
289
290 #endif // RUN_IN_PLACE
291
292         dstream<<"path_data = "<<path_data<<std::endl;
293         dstream<<"path_userdata = "<<path_userdata<<std::endl;
294 }
295
296 } //namespace porting
297