3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 Random portability stuff
24 #ifndef PORTING_HEADER
25 #define PORTING_HEADER
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
38 #include "irrlichttypes.h" // u32
39 #include "irrlichttypes_extrabloated.h"
41 #include "constants.h"
46 #define SWPRINTF_CHARSTRING L"%S"
48 #define SWPRINTF_CHARSTRING L"%s"
51 //currently not needed
52 //template<typename T> struct alignment_trick { char c; T member; };
53 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
58 #define sleep_ms(x) Sleep(x)
61 #include <stdint.h> //for uintptr_t
63 #if (defined(linux) || defined(__linux) || defined(__GNU__)) && !defined(_GNU_SOURCE)
67 #define sleep_ms(x) usleep(x*1000)
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)
79 #define ALIGNOF(x) __alignof__(x)
83 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
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
91 #if defined(__FreeBSD__) || defined(__NetBSD__) || \
92 defined(__OpenBSD__) || defined(__DragonFly__) || \
93 defined(__APPLE__) || \
94 defined(__sun) || defined(sun) || \
95 defined(__QNX__) || defined(__QNXNTO__)
99 // So we need to define our own.
101 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
104 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
106 #if defined(__APPLE__)
107 #include <mach-o/dyld.h>
108 #include <CoreFoundation/CoreFoundation.h>
111 #ifndef _WIN32 // Posix
112 #include <sys/time.h>
114 #if defined(__MACH__) && defined(__APPLE__)
115 #include <mach/clock.h>
116 #include <mach/mach.h>
124 Signal handler (grabs Ctrl-C on POSIX systems)
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);
133 Path of static data directory.
135 extern std::string path_share;
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>"
143 extern std::string path_user;
146 Path to gettext locale files
148 extern std::string path_locale;
151 Get full path of stuff in data directory.
152 Example: "stone.png" -> "../data/stone.png"
154 std::string getDataPath(const char *subpath);
159 void initializePaths();
162 Return system information
163 e.g. "Linux/3.12.7 x86_64"
165 std::string get_sysinfo();
167 void initIrrlicht(irr::IrrlichtDevice * );
170 Resolution is 10-20ms.
171 Remember to check for overflows.
172 Overflow can occur at any value higher than 10000000.
174 #ifdef _WIN32 // Windows
176 inline u32 getTimeS()
178 return GetTickCount() / 1000;
181 inline u32 getTimeMs()
183 return GetTickCount();
186 inline u32 getTimeUs()
188 LARGE_INTEGER freq, t;
189 QueryPerformanceFrequency(&freq);
190 QueryPerformanceCounter(&t);
191 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000.0);
194 inline u32 getTimeNs()
196 LARGE_INTEGER freq, t;
197 QueryPerformanceFrequency(&freq);
198 QueryPerformanceCounter(&t);
199 return (double)(t.QuadPart) / ((double)(freq.QuadPart) / 1000000000.0);
204 inline u32 getTimeS()
207 gettimeofday(&tv, NULL);
211 inline u32 getTimeMs()
214 gettimeofday(&tv, NULL);
215 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
218 inline u32 getTimeUs()
221 gettimeofday(&tv, NULL);
222 return tv.tv_sec * 1000000 + tv.tv_usec;
225 inline u32 getTimeNs()
228 // from http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
229 #if defined(__MACH__) && defined(__APPLE__) // OS X does not have clock_gettime, use clock_get_time
232 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
233 clock_get_time(cclock, &mts);
234 mach_port_deallocate(mach_task_self(), cclock);
235 ts.tv_sec = mts.tv_sec;
236 ts.tv_nsec = mts.tv_nsec;
238 clock_gettime(CLOCK_REALTIME, &ts);
240 return ts.tv_sec * 1000000000 + ts.tv_nsec;
243 /*#include <sys/timeb.h>
244 inline u32 getTimeMs()
248 return tb.time * 1000 + tb.millitm;
252 inline u32 getTime(TimePrecision prec)
255 case PRECISION_SECONDS:
257 case PRECISION_MILLI:
259 case PRECISION_MICRO:
268 * Delta calculation function taking two 32bit arguments.
269 * @param old_time_ms old time for delta calculation (order is relevant!)
270 * @param new_time_ms new time for delta calculation (order is relevant!)
271 * @return positive 32bit delta value
273 inline u32 getDeltaMs(u32 old_time_ms, u32 new_time_ms)
275 if (new_time_ms >= old_time_ms) {
276 return (new_time_ms - old_time_ms);
278 return (old_time_ms - new_time_ms);
284 float getDisplayDensity();
286 v2u32 getDisplaySize();
287 v2u32 getWindowSize();
289 std::vector<core::vector3d<u32> > getSupportedVideoModes();
290 std::vector<irr::video::E_DRIVER_TYPE> getSupportedVideoDrivers();
291 const char *getVideoDriverName(irr::video::E_DRIVER_TYPE type);
292 const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type);
295 inline const char *getPlatformName()
300 #elif defined(linux) || defined(__linux) || defined(__linux__)
302 #elif defined(_WIN32) || defined(_WIN64)
304 #elif defined(__DragonFly__) || defined(__FreeBSD__) || \
305 defined(__NetBSD__) || defined(__OpenBSD__)
307 #elif defined(__APPLE__) && defined(__MACH__)
310 #elif TARGET_OS_IPHONE
317 #elif defined(__hpux)
319 #elif defined(__sun) || defined(sun)
325 #elif defined(__CYGWIN__)
327 #elif defined(__unix__) || defined(__unix)
328 #if defined(_POSIX_VERSION)
339 void setXorgClassHint(const video::SExposedVideoData &video_data,
340 const std::string &name);
342 // This only needs to be called at the start of execution, since all future
343 // threads in the process inherit this exception handler
344 void setWin32ExceptionHandler();
346 } // namespace porting
349 #include "porting_android.h"
352 #endif // PORTING_HEADER