Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
porting.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5/*
6 Random portability stuff
7*/
8
9#pragma once
10
11#if (defined(__linux__) || defined(__GNU__)) && !defined(_GNU_SOURCE)
12 #define _GNU_SOURCE
13#endif
14
15// Be mindful of what you include here!
16#include <string>
17#include "config.h"
18#include "irrlichttypes.h" // u64
19#include "debug.h"
20#include "constants.h"
21#include "util/timetaker.h" // TimePrecision
22
23#ifdef _MSC_VER
24 #define SWPRINTF_CHARSTRING L"%S"
25#else
26 #define SWPRINTF_CHARSTRING L"%s"
27#endif
28
29#ifdef _WIN32
30 #include <windows.h>
31
32 #define sleep_ms(x) Sleep(x)
33 #define sleep_us(x) Sleep((x)/1000)
34
35 #define setenv(n,v,o) _putenv_s(n,v)
36 #define unsetenv(n) _putenv_s(n,"")
37#else
38 #include <unistd.h>
39 #include <cstdlib> // setenv
40
41 #define sleep_ms(x) usleep((x)*1000)
42 #define sleep_us(x) usleep(x)
43#endif
44
45#ifdef _MSC_VER
46 #define strtok_r(x, y, z) strtok_s(x, y, z)
47 #define strtof(x, y) (float)strtod(x, y)
48 #define strtoll(x, y, z) _strtoi64(x, y, z)
49 #define strtoull(x, y, z) _strtoui64(x, y, z)
50 #define strcasecmp(x, y) stricmp(x, y)
51 #define strncasecmp(x, y, n) strnicmp(x, y, n)
52#endif
53
54#ifdef __MINGW32__
55 // was broken in 2013, unclear if still needed
56 #define strtok_r(x, y, z) mystrtok_r(x, y, z)
57#endif
58
59#if !HAVE_STRLCPY
60 #define strlcpy(d, s, n) mystrlcpy(d, s, n)
61#endif
62
63#ifndef _WIN32 // POSIX
64 #include <sys/time.h>
65 #include <ctime>
66 #if defined(__MACH__) && defined(__APPLE__)
67 #include <TargetConditionals.h>
68 #endif
69#endif
70
71namespace porting
72{
73
74/*
75 Signal handler (grabs Ctrl-C on POSIX systems)
76*/
77
79// Returns a pointer to a bool.
80// When the bool is true, program should quit.
82
83/*
84 Path of static data directory.
85*/
86extern std::string path_share;
87
88/*
89 Directory for storing user data. Examples:
90 Windows: "C:\Documents and Settings\user\Application Data<PROJECT_NAME>"
91 Linux: "~/.<PROJECT_NAME>"
92 Mac: "~/Library/Application Support/<PROJECT_NAME>"
93*/
94extern std::string path_user;
95
96/*
97 Path to gettext locale files
98*/
99extern std::string path_locale;
100
101/*
102 Path to directory for storing caches.
103*/
104extern std::string path_cache;
105
106/*
107 Gets the path of our executable.
108*/
109bool getCurrentExecPath(char *buf, size_t len);
110
111/*
112 Get full path of stuff in data directory.
113 Example: "stone.png" -> "../data/stone.png"
114*/
115std::string getDataPath(const char *subpath);
116
117/*
118 Initialize path_*.
119*/
120void initializePaths();
121
122/*
123 Return system information
124 e.g. "Linux/3.12.7 x86_64"
125*/
126const std::string &get_sysinfo();
127
128
129// Monotonic timer
130
131#ifdef _WIN32 // Windows
132
133extern double perf_freq;
134
135inline u64 os_get_time(double mult)
136{
137 LARGE_INTEGER t;
138 QueryPerformanceCounter(&t);
139 return static_cast<double>(t.QuadPart) / (perf_freq / mult);
140}
141
142// Resolution is <1us.
143inline u64 getTimeS() { return os_get_time(1); }
144inline u64 getTimeMs() { return os_get_time(1000); }
145inline u64 getTimeUs() { return os_get_time(1000*1000); }
146inline u64 getTimeNs() { return os_get_time(1000*1000*1000); }
147
148#else // POSIX
149
150inline void os_get_clock(struct timespec *ts)
151{
152#if defined(CLOCK_MONOTONIC_RAW)
153 clock_gettime(CLOCK_MONOTONIC_RAW, ts);
154#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK > 0
155 clock_gettime(CLOCK_MONOTONIC, ts);
156#else
157# if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK == 0
158 // zero means it might be supported at runtime
159 if (clock_gettime(CLOCK_MONOTONIC, ts) == 0)
160 return;
161# endif
162 struct timeval tv;
163 gettimeofday(&tv, NULL);
164 TIMEVAL_TO_TIMESPEC(&tv, ts);
165#endif
166}
167
168inline u64 getTimeS()
169{
170 struct timespec ts;
171 os_get_clock(&ts);
172 return ts.tv_sec;
173}
174
175inline u64 getTimeMs()
176{
177 struct timespec ts;
178 os_get_clock(&ts);
179 return ((u64) ts.tv_sec) * 1000LL + ((u64) ts.tv_nsec) / 1000000LL;
180}
181
182inline u64 getTimeUs()
183{
184 struct timespec ts;
185 os_get_clock(&ts);
186 return ((u64) ts.tv_sec) * 1000000LL + ((u64) ts.tv_nsec) / 1000LL;
187}
188
189inline u64 getTimeNs()
190{
191 struct timespec ts;
192 os_get_clock(&ts);
193 return ((u64) ts.tv_sec) * 1000000000LL + ((u64) ts.tv_nsec);
194}
195
196#endif
197
198inline u64 getTime(TimePrecision prec)
199{
200 switch (prec) {
201 case PRECISION_SECONDS: return getTimeS();
202 case PRECISION_MILLI: return getTimeMs();
203 case PRECISION_MICRO: return getTimeUs();
204 case PRECISION_NANO: return getTimeNs();
205 }
206 FATAL_ERROR("Called getTime with invalid time precision");
207}
208
215inline u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
216{
217 if (new_time_ms >= old_time_ms) {
218 return (new_time_ms - old_time_ms);
219 }
220
221 return (old_time_ms - new_time_ms);
222}
223
224inline const char *getPlatformName()
225{
226 return
227#if defined(ANDROID)
228 "Android"
229#elif defined(__linux__)
230 "Linux"
231#elif defined(_WIN32) || defined(_WIN64)
232 "Windows"
233#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
234 defined(__NetBSD__) || defined(__OpenBSD__)
235 "BSD"
236#elif defined(__APPLE__) && defined(__MACH__)
237 #if TARGET_OS_MAC
238 "OSX"
239 #elif TARGET_OS_IPHONE
240 "iOS"
241 #else
242 "Apple"
243 #endif
244#elif defined(_AIX)
245 "AIX"
246#elif defined(__hpux)
247 "HP-UX"
248#elif defined(__sun) || defined(sun)
249 #if defined(__SVR4)
250 "Solaris"
251 #else
252 "SunOS"
253 #endif
254#elif defined(__HAIKU__)
255 "Haiku"
256#elif defined(__CYGWIN__)
257 "Cygwin"
258#elif defined(__unix__) || defined(__unix)
259 #if defined(_POSIX_VERSION)
260 "POSIX"
261 #else
262 "Unix"
263 #endif
264#else
265 "?"
266#endif
267 ;
268}
269
270bool secure_rand_fill_buf(void *buf, size_t len);
271
272// Call once near beginning of main function.
273void osSpecificInit();
274
275// This attaches to the parents process console, or creates a new one if it doesnt exist.
277
278#if HAVE_MALLOC_TRIM
284void TrackFreedMemory(size_t amount);
285
290void TriggerMemoryTrim();
291#else
292static inline void TrackFreedMemory(size_t amount) { (void)amount; }
293static inline void TriggerMemoryTrim() { (void)0; }
294#endif
295
296#ifdef _WIN32
297// Quotes an argument for use in a CreateProcess() commandline (not cmd.exe!!)
298std::string QuoteArgv(const std::string &arg);
299
300// Convert an error code (e.g. from GetLastError()) into a string.
301std::string ConvertError(DWORD error_code);
302#endif
303
304// snprintf wrapper
305int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...);
306
315bool open_url(const std::string &url);
316
325bool open_directory(const std::string &path);
326
327} // namespace porting
328
329#ifdef __ANDROID__
330#include "porting_android.h"
331#endif
#define FATAL_ERROR(msg)
Definition debug.h:33
Definition porting.cpp:78
u64 getDeltaMs(u64 old_time_ms, u64 new_time_ms)
Delta calculation function arguments.
Definition porting.h:215
std::string path_locale
Definition porting.cpp:161
bool secure_rand_fill_buf(void *buf, size_t len)
Definition porting.cpp:745
std::string path_share
Definition porting.cpp:159
int mt_snprintf(char *buf, const size_t buf_size, const char *fmt,...)
Definition porting.cpp:841
void os_get_clock(struct timespec *ts)
Definition porting.h:150
void signal_handler_init(void)
Definition porting.cpp:115
void osSpecificInit()
Definition porting.cpp:766
static void TrackFreedMemory(size_t amount)
Definition porting.h:292
bool open_directory(const std::string &path)
Opens a directory in the default file manager.
Definition porting.cpp:900
u64 getTimeS()
Definition porting.h:168
std::string path_cache
Definition porting.cpp:162
std::string path_user
Definition porting.cpp:160
u64 getTime(TimePrecision prec)
Definition porting.h:198
bool * signal_handler_killstatus()
Definition porting.cpp:86
u64 getTimeMs()
Definition porting.h:175
bool open_url(const std::string &url)
Opens URL in default web browser.
Definition porting.cpp:890
u64 getTimeNs()
Definition porting.h:189
const char * getPlatformName()
Definition porting.h:224
bool getCurrentExecPath(char *buf, size_t len)
Definition porting.cpp:405
std::string getDataPath(const char *subpath)
Definition porting.cpp:165
void initializePaths()
Definition porting.cpp:614
void attachOrCreateConsole()
Definition porting.cpp:779
static void TriggerMemoryTrim()
Definition porting.h:293
u64 getTimeUs()
Definition porting.h:182
const std::string & get_sysinfo()
Definition porting.cpp:266
TimePrecision
Definition timetaker.h:11
@ PRECISION_SECONDS
Definition timetaker.h:12
@ PRECISION_NANO
Definition timetaker.h:15
@ PRECISION_MICRO
Definition timetaker.h:14
@ PRECISION_MILLI
Definition timetaker.h:13