Clean up threading
[oweals/minetest.git] / src / porting.h
1 /*
2 Minetest
3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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
24 #ifndef PORTING_HEADER
25 #define PORTING_HEADER
26
27 #ifdef _WIN32
28         #ifdef _WIN32_WINNT
29                 #undef _WIN32_WINNT
30         #endif
31         #define _WIN32_WINNT 0x0501 // We need to do this before any other headers
32                 // because those might include sdkddkver.h which defines _WIN32_WINNT if not already set
33 #endif
34
35 #include <string>
36 #include <vector>
37 #include "irrlicht.h"
38 #include "irrlichttypes.h" // u32
39 #include "irrlichttypes_extrabloated.h"
40 #include "debug.h"
41 #include "constants.h"
42 #include "gettime.h"
43 #include "threads.h"
44
45 #ifdef _MSC_VER
46         #define SWPRINTF_CHARSTRING L"%S"
47 #else
48         #define SWPRINTF_CHARSTRING L"%s"
49 #endif
50
51 //currently not needed
52 //template<typename T> struct alignment_trick { char c; T member; };
53 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
54
55 #ifdef _WIN32
56         #include <windows.h>
57
58         #define sleep_ms(x) Sleep(x)
59 #else
60         #include <unistd.h>
61         #include <stdint.h> //for uintptr_t
62
63         #if (defined(linux) || defined(__linux) || defined(__GNU__)) && !defined(_GNU_SOURCE)
64                 #define _GNU_SOURCE
65         #endif
66
67         #define sleep_ms(x) usleep(x*1000)
68 #endif
69
70 #ifdef _MSC_VER
71         #define ALIGNOF(x) __alignof(x)
72         #define strtok_r(x, y, z) strtok_s(x, y, z)
73         #define strtof(x, y) (float)strtod(x, y)
74         #define strtoll(x, y, z) _strtoi64(x, y, z)
75         #define strtoull(x, y, z) _strtoui64(x, y, z)
76         #define strcasecmp(x, y) stricmp(x, y)
77         #define strncasecmp(x, y, n) strnicmp(x, y, n)
78 #else
79         #define ALIGNOF(x) __alignof__(x)
80 #endif
81
82 #ifdef __MINGW32__
83         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
84 #endif
85
86 // strlcpy is missing from glibc.  thanks a lot, drepper.
87 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
88 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
89 // default raises an assertion error and aborts the program if the buffer is
90 // too small.
91 #if defined(__FreeBSD__) || defined(__NetBSD__)    || \
92         defined(__OpenBSD__) || defined(__DragonFly__) || \
93         defined(__APPLE__)   ||                           \
94         defined(__sun)       || defined(sun)           || \
95         defined(__QNX__)     || defined(__QNXNTO__)
96         #define HAVE_STRLCPY
97 #endif
98
99 // So we need to define our own.
100 #ifndef HAVE_STRLCPY
101         #define strlcpy(d, s, n) mystrlcpy(d, s, n)
102 #endif
103
104 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
105
106 #if defined(__APPLE__)
107         #include <mach-o/dyld.h>
108         #include <CoreFoundation/CoreFoundation.h>
109 #endif
110
111 namespace porting
112 {
113
114 /*
115         Signal handler (grabs Ctrl-C on POSIX systems)
116 */
117
118 void signal_handler_init(void);
119 // Returns a pointer to a bool.
120 // When the bool is true, program should quit.
121 bool * signal_handler_killstatus(void);
122
123 /*
124         Path of static data directory.
125 */
126 extern std::string path_share;
127
128 /*
129         Directory for storing user data. Examples:
130         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
131         Linux: "~/.<PROJECT_NAME>"
132         Mac: "~/Library/Application Support/<PROJECT_NAME>"
133 */
134 extern std::string path_user;
135
136 /*
137         Get full path of stuff in data directory.
138         Example: "stone.png" -> "../data/stone.png"
139 */
140 std::string getDataPath(const char *subpath);
141
142 /*
143         Initialize path_share and path_user.
144 */
145 void initializePaths();
146
147 /*
148         Return system information
149         e.g. "Linux/3.12.7 x86_64"
150 */
151 std::string get_sysinfo();
152
153 void initIrrlicht(irr::IrrlichtDevice * );
154
155 /*
156         Resolution is 10-20ms.
157         Remember to check for overflows.
158         Overflow can occur at any value higher than 10000000.
159 */
160 #ifdef _WIN32 // Windows
161 #ifndef _WIN32_WINNT
162         #define _WIN32_WINNT 0x0501
163 #endif
164         #include <windows.h>
165
166         inline u32 getTimeS()
167         {
168                 return GetTickCount() / 1000;
169         }
170
171         inline u32 getTimeMs()
172         {
173                 return GetTickCount();
174         }
175
176         inline u32 getTimeUs()
177         {
178                 LARGE_INTEGER freq, t;
179                 QueryPerformanceFrequency(&freq);
180                 QueryPerformanceCounter(&t);
181                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
182         }
183
184         inline u32 getTimeNs()
185         {
186                 LARGE_INTEGER freq, t;
187                 QueryPerformanceFrequency(&freq);
188                 QueryPerformanceCounter(&t);
189                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
190         }
191
192 #else // Posix
193 #include <sys/time.h>
194 #include <time.h>
195 #if defined(__MACH__) && defined(__APPLE__)
196 #include <mach/clock.h>
197 #include <mach/mach.h>
198 #endif
199
200         inline u32 getTimeS()
201         {
202                 struct timeval tv;
203                 gettimeofday(&tv, NULL);
204                 return tv.tv_sec;
205         }
206
207         inline u32 getTimeMs()
208         {
209                 struct timeval tv;
210                 gettimeofday(&tv, NULL);
211                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
212         }
213
214         inline u32 getTimeUs()
215         {
216                 struct timeval tv;
217                 gettimeofday(&tv, NULL);
218                 return tv.tv_sec * 1000000 + tv.tv_usec;
219         }
220
221         inline u32 getTimeNs()
222         {
223                 struct timespec ts;
224                 // from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
225 #if defined(__MACH__) && defined(__APPLE__) // OS X does not have clock_gettime, use clock_get_time
226                 clock_serv_t cclock;
227                 mach_timespec_t mts;
228                 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
229                 clock_get_time(cclock, &mts);
230                 mach_port_deallocate(mach_task_self(), cclock);
231                 ts.tv_sec = mts.tv_sec;
232                 ts.tv_nsec = mts.tv_nsec;
233 #else
234                 clock_gettime(CLOCK_REALTIME, &ts);
235 #endif
236                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
237         }
238
239         /*#include <sys/timeb.h>
240         inline u32 getTimeMs()
241         {
242                 struct timeb tb;
243                 ftime(&tb);
244                 return tb.time * 1000 + tb.millitm;
245         }*/
246 #endif
247
248 inline u32 getTime(TimePrecision prec)
249 {
250         switch (prec) {
251                 case PRECISION_SECONDS:
252                         return getTimeS();
253                 case PRECISION_MILLI:
254                         return getTimeMs();
255                 case PRECISION_MICRO:
256                         return getTimeUs();
257                 case PRECISION_NANO:
258                         return getTimeNs();
259         }
260         return 0;
261 }
262
263 /**
264  * Delta calculation function taking two 32bit arguments.
265  * @param old_time_ms old time for delta calculation (order is relevant!)
266  * @param new_time_ms new time for delta calculation (order is relevant!)
267  * @return positive 32bit delta value
268  */
269 inline u32 getDeltaMs(u32 old_time_ms, u32 new_time_ms)
270 {
271         if (new_time_ms >= old_time_ms) {
272                 return (new_time_ms - old_time_ms);
273         } else {
274                 return (old_time_ms - new_time_ms);
275         }
276 }
277
278
279 #ifndef SERVER
280 float getDisplayDensity();
281
282 v2u32 getDisplaySize();
283 v2u32 getWindowSize();
284
285 std::vector<core::vector3d<u32> > getSupportedVideoModes();
286 std::vector<irr::video::E_DRIVER_TYPE> getSupportedVideoDrivers();
287 const char *getVideoDriverName(irr::video::E_DRIVER_TYPE type);
288 const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type);
289 #endif
290
291 inline const char *getPlatformName()
292 {
293         return
294 #if defined(ANDROID)
295         "Android"
296 #elif defined(linux) || defined(__linux) || defined(__linux__)
297         "Linux"
298 #elif defined(_WIN32) || defined(_WIN64)
299         "Windows"
300 #elif defined(__DragonFly__) || defined(__FreeBSD__) || \
301                 defined(__NetBSD__) || defined(__OpenBSD__)
302         "BSD"
303 #elif defined(__APPLE__) && defined(__MACH__)
304         #if TARGET_OS_MAC
305                 "OSX"
306         #elif TARGET_OS_IPHONE
307                 "iOS"
308         #else
309                 "Apple"
310         #endif
311 #elif defined(_AIX)
312         "AIX"
313 #elif defined(__hpux)
314         "HP-UX"
315 #elif defined(__sun) || defined(sun)
316         #if defined(__SVR4)
317                 "Solaris"
318         #else
319                 "SunOS"
320         #endif
321 #elif defined(__CYGWIN__)
322         "Cygwin"
323 #elif defined(__unix__) || defined(__unix)
324         #if defined(_POSIX_VERSION)
325                 "Posix"
326         #else
327                 "Unix"
328         #endif
329 #else
330         "?"
331 #endif
332         ;
333 }
334
335 void setXorgClassHint(const video::SExposedVideoData &video_data,
336         const std::string &name);
337
338 // This only needs to be called at the start of execution, since all future
339 // threads in the process inherit this exception handler
340 void setWin32ExceptionHandler();
341
342 } // namespace porting
343
344 #ifdef __ANDROID__
345 #include "porting_android.h"
346 #endif
347
348 #endif // PORTING_HEADER
349