OS X compatibility fixes
[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 "irrlicht.h"
37 #include "irrlichttypes.h" // u32
38 #include "irrlichttypes_extrabloated.h"
39 #include "debug.h"
40 #include "constants.h"
41 #include "gettime.h"
42 #include "threads.h"
43
44 #ifdef _MSC_VER
45         #define SWPRINTF_CHARSTRING L"%S"
46 #else
47         #define SWPRINTF_CHARSTRING L"%s"
48 #endif
49
50 //currently not needed
51 //template<typename T> struct alignment_trick { char c; T member; };
52 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
53
54 #ifdef _WIN32
55         #include <windows.h>
56         
57         #define sleep_ms(x) Sleep(x)
58 #else
59         #include <unistd.h>
60         #include <stdint.h> //for uintptr_t
61         
62         #if (defined(linux) || defined(__linux)) && !defined(_GNU_SOURCE)
63                 #define _GNU_SOURCE
64         #endif
65
66         #include <sched.h>
67
68         #ifdef __FreeBSD__
69                 #include <pthread_np.h>
70                 typedef cpuset_t cpu_set_t;
71         #elif defined(__sun) || defined(sun)
72                 #include <sys/types.h>
73                 #include <sys/processor.h>
74         #elif defined(_AIX)
75                 #include <sys/processor.h>
76         #elif __APPLE__
77                 #include <mach/mach_init.h>
78                 #include <mach/thread_policy.h>
79         #endif
80
81         #define sleep_ms(x) usleep(x*1000)
82         
83         #define THREAD_PRIORITY_LOWEST       0
84         #define THREAD_PRIORITY_BELOW_NORMAL 1
85         #define THREAD_PRIORITY_NORMAL       2
86         #define THREAD_PRIORITY_ABOVE_NORMAL 3
87         #define THREAD_PRIORITY_HIGHEST      4
88 #endif
89
90 #ifdef _MSC_VER
91         #define ALIGNOF(x) __alignof(x)
92         #define strtok_r(x, y, z) strtok_s(x, y, z)
93         #define strtof(x, y) (float)strtod(x, y)
94         #define strtoll(x, y, z) _strtoi64(x, y, z)
95         #define strtoull(x, y, z) _strtoui64(x, y, z)
96         #define strcasecmp(x, y) stricmp(x, y)
97         #define strncasecmp(x, y, n) strnicmp(x, y, n)
98 #else
99         #define ALIGNOF(x) __alignof__(x)
100 #endif
101
102 #ifdef __MINGW32__
103         #define strtok_r(x, y, z) mystrtok_r(x, y, z)
104 #endif
105
106 // strlcpy is missing from glibc.  thanks a lot, drepper.
107 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
108 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
109 // default raises an assertion error and aborts the program if the buffer is
110 // too small.
111 #if defined(__FreeBSD__) || defined(__NetBSD__)    || \
112         defined(__OpenBSD__) || defined(__DragonFly__) || \
113         defined(__APPLE__)   ||                           \
114         defined(__sun)       || defined(sun)           || \
115         defined(__QNX__)     || defined(__QNXNTO__)
116         #define HAVE_STRLCPY
117 #endif
118
119 // So we need to define our own.
120 #ifndef HAVE_STRLCPY
121         #define strlcpy(d, s, n) mystrlcpy(d, s, n)
122 #endif
123
124 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
125
126 #if defined(__APPLE__)
127         #include <mach-o/dyld.h>
128         #include <CoreFoundation/CoreFoundation.h>
129 #endif
130
131 namespace porting
132 {
133
134 /*
135         Signal handler (grabs Ctrl-C on POSIX systems)
136 */
137
138 void signal_handler_init(void);
139 // Returns a pointer to a bool.
140 // When the bool is true, program should quit.
141 bool * signal_handler_killstatus(void);
142
143 /*
144         Path of static data directory.
145 */
146 extern std::string path_share;
147
148 /*
149         Directory for storing user data. Examples:
150         Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
151         Linux: "~/.<PROJECT_NAME>"
152         Mac: "~/Library/Application Support/<PROJECT_NAME>"
153 */
154 extern std::string path_user;
155
156 /*
157         Get full path of stuff in data directory.
158         Example: "stone.png" -> "../data/stone.png"
159 */
160 std::string getDataPath(const char *subpath);
161
162 /*
163         Initialize path_share and path_user.
164 */
165 void initializePaths();
166
167 /*
168         Get number of online processors in the system.
169 */
170 int getNumberOfProcessors();
171
172 /*
173         Set a thread's affinity to a particular processor.
174 */
175 bool threadBindToProcessor(threadid_t tid, int pnumber);
176
177 /*
178         Set a thread's priority.
179 */
180 bool threadSetPriority(threadid_t tid, int prio);
181
182 /*
183         Return system information
184         e.g. "Linux/3.12.7 x86_64"
185 */
186 std::string get_sysinfo();
187
188 void initIrrlicht(irr::IrrlichtDevice * );
189
190 /*
191         Resolution is 10-20ms.
192         Remember to check for overflows.
193         Overflow can occur at any value higher than 10000000.
194 */
195 #ifdef _WIN32 // Windows
196 #ifndef _WIN32_WINNT
197         #define _WIN32_WINNT 0x0501
198 #endif
199         #include <windows.h>
200         
201         inline u32 getTimeS()
202         {
203                 return GetTickCount() / 1000;
204         }
205         
206         inline u32 getTimeMs()
207         {
208                 return GetTickCount();
209         }
210         
211         inline u32 getTimeUs()
212         {
213                 LARGE_INTEGER freq, t;
214                 QueryPerformanceFrequency(&freq);
215                 QueryPerformanceCounter(&t);
216                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
217         }
218         
219         inline u32 getTimeNs()
220         {
221                 LARGE_INTEGER freq, t;
222                 QueryPerformanceFrequency(&freq);
223                 QueryPerformanceCounter(&t);
224                 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
225         }
226         
227 #else // Posix
228 #include <sys/time.h>
229 #include <time.h>
230 #ifdef __MACH__
231 #include <mach/clock.h>
232 #include <mach/mach.h>
233 #endif
234
235         inline u32 getTimeS()
236         {
237                 struct timeval tv;
238                 gettimeofday(&tv, NULL);
239                 return tv.tv_sec;
240         }
241         
242         inline u32 getTimeMs()
243         {
244                 struct timeval tv;
245                 gettimeofday(&tv, NULL);
246                 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
247         }
248         
249         inline u32 getTimeUs()
250         {
251                 struct timeval tv;
252                 gettimeofday(&tv, NULL);
253                 return tv.tv_sec * 1000000 + tv.tv_usec;
254         }
255         
256         inline u32 getTimeNs()
257         {
258                 struct timespec ts;
259                 // from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
260 #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
261                 clock_serv_t cclock;
262                 mach_timespec_t mts;
263                 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
264                 clock_get_time(cclock, &mts);
265                 mach_port_deallocate(mach_task_self(), cclock);
266                 ts.tv_sec = mts.tv_sec;
267                 ts.tv_nsec = mts.tv_nsec;
268 #else
269                 clock_gettime(CLOCK_REALTIME, &ts);
270 #endif
271                 return ts.tv_sec * 1000000000 + ts.tv_nsec;
272         }
273         
274         /*#include <sys/timeb.h>
275         inline u32 getTimeMs()
276         {
277                 struct timeb tb;
278                 ftime(&tb);
279                 return tb.time * 1000 + tb.millitm;
280         }*/
281 #endif
282
283 inline u32 getTime(TimePrecision prec)
284 {
285         switch (prec) {
286                 case PRECISION_SECONDS:
287                         return getTimeS();
288                 case PRECISION_MILLI:
289                         return getTimeMs();
290                 case PRECISION_MICRO:
291                         return getTimeUs();
292                 case PRECISION_NANO:
293                         return getTimeNs();
294         }
295         return 0;
296 }
297
298 /**
299  * Delta calculation function taking two 32bit arguments.
300  * @param old_time_ms old time for delta calculation (order is relevant!)
301  * @param new_time_ms new time for delta calculation (order is relevant!)
302  * @return positive 32bit delta value
303  */
304 inline u32 getDeltaMs(u32 old_time_ms, u32 new_time_ms)
305 {
306         if (new_time_ms >= old_time_ms) {
307                 return (new_time_ms - old_time_ms);
308         } else {
309                 return (old_time_ms - new_time_ms);
310         }
311 }
312
313 #if defined(linux) || defined(__linux)
314         #include <sys/prctl.h>
315
316         inline void setThreadName(const char *name) {
317                 /* It would be cleaner to do this with pthread_setname_np,
318                  * which was added to glibc in version 2.12, but some major
319                  * distributions are still runing 2.11 and previous versions.
320                  */
321                 prctl(PR_SET_NAME, name);
322         }
323 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
324         #include <pthread.h>
325         #include <pthread_np.h>
326
327         inline void setThreadName(const char *name) {
328                 pthread_set_name_np(pthread_self(), name);
329         }
330 #elif defined(__NetBSD__)
331         #include <pthread.h>
332
333         inline void setThreadName(const char *name) {
334                 pthread_setname_np(pthread_self(), name);
335         }
336 #elif defined(_MSC_VER)
337         typedef struct tagTHREADNAME_INFO {
338                 DWORD dwType; // must be 0x1000
339                 LPCSTR szName; // pointer to name (in user addr space)
340                 DWORD dwThreadID; // thread ID (-1=caller thread)
341                 DWORD dwFlags; // reserved for future use, must be zero
342         } THREADNAME_INFO;
343
344         inline void setThreadName(const char *name) {
345                 THREADNAME_INFO info;
346                 info.dwType = 0x1000;
347                 info.szName = name;
348                 info.dwThreadID = -1;
349                 info.dwFlags = 0;
350                 __try {
351                         RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), (ULONG_PTR *) &info);
352                 } __except (EXCEPTION_CONTINUE_EXECUTION) {}
353         }
354 #elif defined(__APPLE__)
355         #include <pthread.h>
356
357         inline void setThreadName(const char *name) {
358                 pthread_setname_np(name);
359         }
360 #elif defined(_WIN32)
361         inline void setThreadName(const char* name) {}
362 #else
363         #warning "Unrecognized platform, thread names will not be available."
364         inline void setThreadName(const char* name) {}
365 #endif
366
367 #ifndef SERVER
368 float getDisplayDensity();
369
370 v2u32 getDisplaySize();
371 v2u32 getWindowSize();
372 #endif
373
374 } // namespace porting
375
376 #endif // PORTING_HEADER
377