Clocksource: use a better clock if available.
[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 #ifndef _WIN32 // Posix
112         #include <sys/time.h>
113         #include <time.h>
114         #if defined(__MACH__) && defined(__APPLE__)
115                 #include <mach/clock.h>
116                 #include <mach/mach.h>
117         #endif
118 #endif
119
120 namespace porting
121 {
122
123 /*
124         Signal handler (grabs Ctrl-C on POSIX systems)
125 */
126
127 void signal_handler_init(void);
128 // Returns a pointer to a bool.
129 // When the bool is true, program should quit.
130 bool * signal_handler_killstatus(void);
131
132 /*
133         Path of static data directory.
134 */
135 extern std::string path_share;
136
137 /*
138         Directory for storing user data. Examples:
139         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
140         Linux: "~/.<PROJECT_NAME>"
141         Mac: "~/Library/Application Support/<PROJECT_NAME>"
142 */
143 extern std::string path_user;
144
145 /*
146         Path to gettext locale files
147 */
148 extern std::string path_locale;
149
150 /*
151         Path to directory for storing caches.
152 */
153 extern std::string path_cache;
154
155 /*
156         Get full path of stuff in data directory.
157         Example: "stone.png" -> "../data/stone.png"
158 */
159 std::string getDataPath(const char *subpath);
160
161 /*
162         Move cache folder from path_user to the
163         system cache location if possible.
164 */
165 void migrateCachePath();
166
167 /*
168         Initialize path_*.
169 */
170 void initializePaths();
171
172 /*
173         Return system information
174         e.g. "Linux/3.12.7 x86_64"
175 */
176 std::string get_sysinfo();
177
178 void initIrrlicht(irr::IrrlichtDevice * );
179
180 /*
181         Resolution is 10-20ms.
182         Remember to check for overflows.
183         Overflow can occur at any value higher than 10000000.
184 */
185 #ifdef _WIN32 // Windows
186
187         inline u32 getTimeS()
188         {
189                 return GetTickCount() / 1000;
190         }
191
192         inline u32 getTimeMs()
193         {
194                 return GetTickCount();
195         }
196
197         inline u32 getTimeUs()
198         {
199                 LARGE_INTEGER freq, t;
200                 QueryPerformanceFrequency(&freq);
201                 QueryPerformanceCounter(&t);
202                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
203         }
204
205         inline u32 getTimeNs()
206         {
207                 LARGE_INTEGER freq, t;
208                 QueryPerformanceFrequency(&freq);
209                 QueryPerformanceCounter(&t);
210                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
211         }
212
213 #else // Posix
214         inline void _os_get_clock(struct timespec *ts)
215         {
216 #if defined(__MACH__) && defined(__APPLE__)
217         // from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
218         // OS X does not have clock_gettime, use clock_get_time
219                 clock_serv_t cclock;
220                 mach_timespec_t mts;
221                 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
222                 clock_get_time(cclock, &mts);
223                 mach_port_deallocate(mach_task_self(), cclock);
224                 ts.tv_sec = mts.tv_sec;
225                 ts.tv_nsec = mts.tv_nsec;
226 #elif defined(CLOCK_MONOTONIC_RAW)
227                 clock_gettime(CLOCK_MONOTONIC_RAW, ts);
228 #elif defined(_POSIX_MONOTONIC_CLOCK)
229                 clock_gettime(CLOCK_MONOTONIC, ts);
230 #else
231                 struct timeval tv;
232                 gettimeofday(&tv, NULL);
233                 TIMEVAL_TO_TIMESPEC(&tv, ts);
234 #endif // defined(__MACH__) && defined(__APPLE__)
235         }
236
237         // Note: these clock functions do not return wall time, but
238         // generally a clock that starts at 0 when the process starts.
239         inline u32 getTimeS()
240         {
241                 struct timespec ts;
242                 _os_get_clock(&ts);
243                 return ts.tv_sec;
244         }
245
246         inline u32 getTimeMs()
247         {
248                 struct timespec ts;
249                 _os_get_clock(&ts);
250                 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
251         }
252
253         inline u32 getTimeUs()
254         {
255                 struct timespec ts;
256                 _os_get_clock(&ts);
257                 return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
258         }
259
260         inline u32 getTimeNs()
261         {
262                 struct timespec ts;
263                 _os_get_clock(&ts);
264                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
265         }
266
267         /*#include <sys/timeb.h>
268         inline u32 getTimeMs()
269         {
270                 struct timeb tb;
271                 ftime(&tb);
272                 return tb.time * 1000 + tb.millitm;
273         }*/
274 #endif
275
276 inline u32 getTime(TimePrecision prec)
277 {
278         switch (prec) {
279                 case PRECISION_SECONDS:
280                         return getTimeS();
281                 case PRECISION_MILLI:
282                         return getTimeMs();
283                 case PRECISION_MICRO:
284                         return getTimeUs();
285                 case PRECISION_NANO:
286                         return getTimeNs();
287         }
288         return 0;
289 }
290
291 /**
292  * Delta calculation function taking two 32bit arguments.
293  * @param old_time_ms old time for delta calculation (order is relevant!)
294  * @param new_time_ms new time for delta calculation (order is relevant!)
295  * @return positive 32bit delta value
296  */
297 inline u32 getDeltaMs(u32 old_time_ms, u32 new_time_ms)
298 {
299         if (new_time_ms >= old_time_ms) {
300                 return (new_time_ms - old_time_ms);
301         } else {
302                 return (old_time_ms - new_time_ms);
303         }
304 }
305
306
307 #ifndef SERVER
308 float getDisplayDensity();
309
310 v2u32 getDisplaySize();
311 v2u32 getWindowSize();
312
313 std::vector<core::vector3d<u32> > getSupportedVideoModes();
314 std::vector<irr::video::E_DRIVER_TYPE> getSupportedVideoDrivers();
315 const char *getVideoDriverName(irr::video::E_DRIVER_TYPE type);
316 const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type);
317 #endif
318
319 inline const char *getPlatformName()
320 {
321         return
322 #if defined(ANDROID)
323         "Android"
324 #elif defined(linux) || defined(__linux) || defined(__linux__)
325         "Linux"
326 #elif defined(_WIN32) || defined(_WIN64)
327         "Windows"
328 #elif defined(__DragonFly__) || defined(__FreeBSD__) || \
329                 defined(__NetBSD__) || defined(__OpenBSD__)
330         "BSD"
331 #elif defined(__APPLE__) && defined(__MACH__)
332         #if TARGET_OS_MAC
333                 "OSX"
334         #elif TARGET_OS_IPHONE
335                 "iOS"
336         #else
337                 "Apple"
338         #endif
339 #elif defined(_AIX)
340         "AIX"
341 #elif defined(__hpux)
342         "HP-UX"
343 #elif defined(__sun) || defined(sun)
344         #if defined(__SVR4)
345                 "Solaris"
346         #else
347                 "SunOS"
348         #endif
349 #elif defined(__CYGWIN__)
350         "Cygwin"
351 #elif defined(__unix__) || defined(__unix)
352         #if defined(_POSIX_VERSION)
353                 "Posix"
354         #else
355                 "Unix"
356         #endif
357 #else
358         "?"
359 #endif
360         ;
361 }
362
363 void setXorgClassHint(const video::SExposedVideoData &video_data,
364         const std::string &name);
365
366 // This only needs to be called at the start of execution, since all future
367 // threads in the process inherit this exception handler
368 void setWin32ExceptionHandler();
369
370 bool secure_rand_fill_buf(void *buf, size_t len);
371 } // namespace porting
372
373 #ifdef __ANDROID__
374 #include "porting_android.h"
375 #endif
376
377 #endif // PORTING_HEADER
378