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 // Use standard Posix macro for Linux
64 #if (defined(linux) || defined(__linux)) && !defined(__linux__)
67 #if (defined(__linux__) || defined(__GNU__)) && !defined(_GNU_SOURCE)
71 #define sleep_ms(x) usleep(x*1000)
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)
83 #define ALIGNOF(x) __alignof__(x)
87 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
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
95 #if defined(__FreeBSD__) || defined(__NetBSD__) || \
96 defined(__OpenBSD__) || defined(__DragonFly__) || \
97 defined(__APPLE__) || \
98 defined(__sun) || defined(sun) || \
99 defined(__QNX__) || defined(__QNXNTO__)
103 // So we need to define our own.
105 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
108 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
110 #if defined(__APPLE__)
111 #include <mach-o/dyld.h>
112 #include <CoreFoundation/CoreFoundation.h>
115 #ifndef _WIN32 // Posix
116 #include <sys/time.h>
118 #if defined(__MACH__) && defined(__APPLE__)
119 #include <mach/clock.h>
120 #include <mach/mach.h>
128 Signal handler (grabs Ctrl-C on POSIX systems)
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);
137 Path of static data directory.
139 extern std::string path_share;
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>"
147 extern std::string path_user;
150 Path to gettext locale files
152 extern std::string path_locale;
155 Path to directory for storing caches.
157 extern std::string path_cache;
160 Get full path of stuff in data directory.
161 Example: "stone.png" -> "../data/stone.png"
163 std::string getDataPath(const char *subpath);
166 Move cache folder from path_user to the
167 system cache location if possible.
169 void migrateCachePath();
174 void initializePaths();
177 Return system information
178 e.g. "Linux/3.12.7 x86_64"
180 std::string get_sysinfo();
182 void initIrrlicht(irr::IrrlichtDevice * );
185 // Monotonic counter getters.
187 #ifdef _WIN32 // Windows
189 extern double perf_freq;
191 inline u64 os_get_time(double mult)
194 QueryPerformanceCounter(&t);
195 return static_cast<double>(t.QuadPart) / (perf_freq / mult);
198 // Resolution is <1us.
199 inline u64 getTimeS() { return os_get_time(1); }
200 inline u64 getTimeMs() { return os_get_time(1000); }
201 inline u64 getTimeUs() { return os_get_time(1000*1000); }
202 inline u64 getTimeNs() { return os_get_time(1000*1000*1000); }
206 inline void os_get_clock(struct timespec *ts)
208 #if defined(__MACH__) && defined(__APPLE__)
209 // From http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
210 // OS X does not have clock_gettime, use clock_get_time
213 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
214 clock_get_time(cclock, &mts);
215 mach_port_deallocate(mach_task_self(), cclock);
216 ts->tv_sec = mts.tv_sec;
217 ts->tv_nsec = mts.tv_nsec;
218 #elif defined(CLOCK_MONOTONIC_RAW)
219 clock_gettime(CLOCK_MONOTONIC_RAW, ts);
220 #elif defined(_POSIX_MONOTONIC_CLOCK)
221 clock_gettime(CLOCK_MONOTONIC, ts);
224 gettimeofday(&tv, NULL);
225 TIMEVAL_TO_TIMESPEC(&tv, ts);
229 inline u64 getTimeS()
236 inline u64 getTimeMs()
240 return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
243 inline u64 getTimeUs()
247 return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
250 inline u64 getTimeNs()
254 return ts.tv_sec * 1000000000 + ts.tv_nsec;
259 inline u64 getTime(TimePrecision prec)
262 case PRECISION_SECONDS: return getTimeS();
263 case PRECISION_MILLI: return getTimeMs();
264 case PRECISION_MICRO: return getTimeUs();
265 case PRECISION_NANO: return getTimeNs();
267 FATAL_ERROR("Called getTime with invalid time precision");
271 * Delta calculation function arguments.
272 * @param old_time_ms old time for delta calculation
273 * @param new_time_ms new time for delta calculation
274 * @return positive delta value
276 inline u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
278 if (new_time_ms >= old_time_ms) {
279 return (new_time_ms - old_time_ms);
281 return (old_time_ms - new_time_ms);
287 float getDisplayDensity();
289 v2u32 getDisplaySize();
290 v2u32 getWindowSize();
292 std::vector<core::vector3d<u32> > getSupportedVideoModes();
293 std::vector<irr::video::E_DRIVER_TYPE> getSupportedVideoDrivers();
294 const char *getVideoDriverName(irr::video::E_DRIVER_TYPE type);
295 const char *getVideoDriverFriendlyName(irr::video::E_DRIVER_TYPE type);
298 inline const char *getPlatformName()
303 #elif defined(__linux__)
305 #elif defined(_WIN32) || defined(_WIN64)
307 #elif defined(__DragonFly__) || defined(__FreeBSD__) || \
308 defined(__NetBSD__) || defined(__OpenBSD__)
310 #elif defined(__APPLE__) && defined(__MACH__)
313 #elif TARGET_OS_IPHONE
320 #elif defined(__hpux)
322 #elif defined(__sun) || defined(sun)
328 #elif defined(__CYGWIN__)
330 #elif defined(__unix__) || defined(__unix)
331 #if defined(_POSIX_VERSION)
342 void setXorgClassHint(const video::SExposedVideoData &video_data,
343 const std::string &name);
345 bool setWindowIcon(IrrlichtDevice *device);
347 bool setXorgWindowIconFromPath(IrrlichtDevice *device,
348 const std::string &icon_file);
350 // This only needs to be called at the start of execution, since all future
351 // threads in the process inherit this exception handler
352 void setWin32ExceptionHandler();
354 bool secure_rand_fill_buf(void *buf, size_t len);
356 // This attaches to the parents process console, or creates a new one if it doesnt exist.
357 void attachOrCreateConsole(void);
358 } // namespace porting
361 #include "porting_android.h"
364 #endif // PORTING_HEADER