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