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
30 #define _WIN32_WINNT 0x0501 // We need to do this before any other headers
31 // because those might include sdkddkver.h which defines _WIN32_WINNT if not already set
37 #include "irrlichttypes.h" // u32
38 #include "irrlichttypes_extrabloated.h"
40 #include "constants.h"
44 #define SWPRINTF_CHARSTRING L"%S"
46 #define SWPRINTF_CHARSTRING L"%s"
49 //currently not needed
50 //template<typename T> struct alignment_trick { char c; T member; };
51 //#define ALIGNOF(type) offsetof (alignment_trick<type>, member)
56 #define sleep_ms(x) Sleep(x)
59 #include <cstdint> //for uintptr_t
61 // Use standard Posix macro for Linux
62 #if (defined(linux) || defined(__linux)) && !defined(__linux__)
65 #if (defined(__linux__) || defined(__GNU__)) && !defined(_GNU_SOURCE)
69 #define sleep_ms(x) usleep(x*1000)
73 #define ALIGNOF(x) __alignof(x)
74 #define strtok_r(x, y, z) strtok_s(x, y, z)
75 #define strtof(x, y) (float)strtod(x, y)
76 #define strtoll(x, y, z) _strtoi64(x, y, z)
77 #define strtoull(x, y, z) _strtoui64(x, y, z)
78 #define strcasecmp(x, y) stricmp(x, y)
79 #define strncasecmp(x, y, n) strnicmp(x, y, n)
81 #define ALIGNOF(x) __alignof__(x)
85 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
88 // strlcpy is missing from glibc. thanks a lot, drepper.
89 // strlcpy is also missing from AIX and HP-UX because they aim to be weird.
90 // We can't simply alias strlcpy to MSVC's strcpy_s, since strcpy_s by
91 // default raises an assertion error and aborts the program if the buffer is
93 #if defined(__FreeBSD__) || defined(__NetBSD__) || \
94 defined(__OpenBSD__) || defined(__DragonFly__) || \
95 defined(__APPLE__) || \
96 defined(__sun) || defined(sun) || \
97 defined(__QNX__) || defined(__QNXNTO__)
101 // So we need to define our own.
103 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
106 #define PADDING(x, y) ((ALIGNOF(y) - ((uintptr_t)(x) & (ALIGNOF(y) - 1))) & (ALIGNOF(y) - 1))
108 #if defined(__APPLE__)
109 #include <mach-o/dyld.h>
110 #include <CoreFoundation/CoreFoundation.h>
113 #ifndef _WIN32 // Posix
114 #include <sys/time.h>
117 #if defined(__MACH__) && defined(__APPLE__)
118 #include <mach/clock.h>
119 #include <mach/mach.h>
127 Signal handler (grabs Ctrl-C on POSIX systems)
130 void signal_handler_init();
131 // Returns a pointer to a bool.
132 // When the bool is true, program should quit.
133 bool * signal_handler_killstatus();
136 Path of static data directory.
138 extern std::string path_share;
141 Directory for storing user data. Examples:
142 Windows: "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
143 Linux: "~/.<PROJECT_NAME>"
144 Mac: "~/Library/Application Support/<PROJECT_NAME>"
146 extern std::string path_user;
149 Path to gettext locale files
151 extern std::string path_locale;
154 Path to directory for storing caches.
156 extern std::string path_cache;
159 Gets the path of our executable.
161 bool getCurrentExecPath(char *buf, size_t len);
164 Get full path of stuff in data directory.
165 Example: "stone.png" -> "../data/stone.png"
167 std::string getDataPath(const char *subpath);
170 Move cache folder from path_user to the
171 system cache location if possible.
173 void migrateCachePath();
178 void initializePaths();
181 Return system information
182 e.g. "Linux/3.12.7 x86_64"
184 std::string get_sysinfo();
187 // Monotonic counter getters.
189 #ifdef _WIN32 // Windows
191 extern double perf_freq;
193 inline u64 os_get_time(double mult)
196 QueryPerformanceCounter(&t);
197 return static_cast<double>(t.QuadPart) / (perf_freq / mult);
200 // Resolution is <1us.
201 inline u64 getTimeS() { return os_get_time(1); }
202 inline u64 getTimeMs() { return os_get_time(1000); }
203 inline u64 getTimeUs() { return os_get_time(1000*1000); }
204 inline u64 getTimeNs() { return os_get_time(1000*1000*1000); }
208 inline void os_get_clock(struct timespec *ts)
210 #if defined(__MACH__) && defined(__APPLE__)
211 // From http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x
212 // OS X does not have clock_gettime, use clock_get_time
215 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
216 clock_get_time(cclock, &mts);
217 mach_port_deallocate(mach_task_self(), cclock);
218 ts->tv_sec = mts.tv_sec;
219 ts->tv_nsec = mts.tv_nsec;
220 #elif defined(CLOCK_MONOTONIC_RAW)
221 clock_gettime(CLOCK_MONOTONIC_RAW, ts);
222 #elif defined(_POSIX_MONOTONIC_CLOCK)
223 clock_gettime(CLOCK_MONOTONIC, ts);
226 gettimeofday(&tv, NULL);
227 TIMEVAL_TO_TIMESPEC(&tv, ts);
231 inline u64 getTimeS()
238 inline u64 getTimeMs()
242 return ((u64) ts.tv_sec) * 1000LL + ((u64) ts.tv_nsec) / 1000000LL;
245 inline u64 getTimeUs()
249 return ((u64) ts.tv_sec) * 1000000LL + ((u64) ts.tv_nsec) / 1000LL;
252 inline u64 getTimeNs()
256 return ((u64) ts.tv_sec) * 1000000000LL + ((u64) ts.tv_nsec);
261 inline u64 getTime(TimePrecision prec)
264 case PRECISION_SECONDS: return getTimeS();
265 case PRECISION_MILLI: return getTimeMs();
266 case PRECISION_MICRO: return getTimeUs();
267 case PRECISION_NANO: return getTimeNs();
269 FATAL_ERROR("Called getTime with invalid time precision");
273 * Delta calculation function arguments.
274 * @param old_time_ms old time for delta calculation
275 * @param new_time_ms new time for delta calculation
276 * @return positive delta value
278 inline u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
280 if (new_time_ms >= old_time_ms) {
281 return (new_time_ms - old_time_ms);
284 return (old_time_ms - new_time_ms);
287 inline const char *getPlatformName()
292 #elif defined(__linux__)
294 #elif defined(_WIN32) || defined(_WIN64)
296 #elif defined(__DragonFly__) || defined(__FreeBSD__) || \
297 defined(__NetBSD__) || defined(__OpenBSD__)
299 #elif defined(__APPLE__) && defined(__MACH__)
302 #elif TARGET_OS_IPHONE
309 #elif defined(__hpux)
311 #elif defined(__sun) || defined(sun)
317 #elif defined(__HAIKU__)
319 #elif defined(__CYGWIN__)
321 #elif defined(__unix__) || defined(__unix)
322 #if defined(_POSIX_VERSION)
333 bool secure_rand_fill_buf(void *buf, size_t len);
335 // This attaches to the parents process console, or creates a new one if it doesnt exist.
336 void attachOrCreateConsole();
339 // Quotes an argument for use in a CreateProcess() commandline (not cmd.exe!!)
340 std::string QuoteArgv(const std::string &arg);
343 int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
346 * Opens URL in default web browser
348 * Must begin with http:// or https://, and not contain any new lines
351 * @return true on success, false on failure
353 bool open_url(const std::string &url);
356 * Opens a directory in the default file manager
358 * The directory must exist.
360 * @param path Path to directory
361 * @return true on success, false on failure
363 bool open_directory(const std::string &path);
365 } // namespace porting
368 #include "porting_android.h"