MythTV  master
compat.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 // Simple header which encapsulates platform incompatibilities, so we
3 // do not need to litter the codebase with ifdefs.
4 
5 #ifndef COMPAT_H
6 #define COMPAT_H
7 
8 #ifdef __cplusplus
9 # include <cstdio> // for snprintf(), used by inline dlerror()
10 # include <QtGlobal> // for Q_OS_XXX
11 #else
12 # include <stdio.h> // for snprintf(), used by inline dlerror()
13 #endif
14 
15 #ifdef _WIN32
16 # ifndef _MSC_VER
17 # define close wsock_close
18 # endif
19 
20 # ifndef NOMINMAX
21 # define NOMINMAX
22 # endif
23 
24 # include <windows.h>
25 
26 # undef DialogBox
27 # undef LoadImage
28 # undef LoadIcon
29 # undef GetObject
30 # undef DrawText
31 # undef CreateDialog
32 # undef CreateFont
33 # undef DeleteFile
34 # undef GetCurrentTime
35 # undef SetJob
36 # undef SendMessage
37 
38 # ifndef _MSC_VER
39 # include <winsock2.h>
40 # include <ws2tcpip.h>
41 # else
42 # include <io.h>
43 # endif
44 
45 # undef close
46 #else
47 # include <sys/time.h> // Mac OS X needs this before sys/resource
48 # include <sys/resource.h> // for setpriority
49 # include <sys/socket.h>
50 # include <sys/wait.h> // For WIFEXITED on Mac OS X
51 #endif
52 
53 #ifdef _MSC_VER
54  #include <cstdlib> // for rand()
55  #include <ctime>
56  #include <sys/time.h>
57 #endif
58 #if defined(USING_MINGW)
59  #include <time.h>
60 #endif
61 
62 #ifdef _MSC_VER
63  // Turn off the visual studio warnings (identifier was truncated)
64  #pragma warning(disable:4786)
65 
66  #ifdef restrict
67  #undef restrict
68  #endif
69 
70  #include <cinttypes>
71  #include <direct.h>
72  #include <process.h>
73 
74  #define strtoll _strtoi64
75  #define strncasecmp _strnicmp
76  #define snprintf _snprintf
77 
78  #ifdef _WIN64
79  using ssize_t = __int64;
80  #else
81  using ssize_t = int;
82  #endif
83 
84  // Check for execute, only checking existance in MSVC
85  #define X_OK 0
86 
87  #if (_MSC_VER < 1800)
88  #define rint( x ) floor(x + 0.5)
89  #define round( x ) floor(x + 0.5)
90 
91  #if ( _MSC_VER < 1700)
92  #define signbit( x ) ( x < 0 )
93  #endif
94 
95  #undef M_PI
96  #define M_PI 3.14159265358979323846
97  #endif
98 
99  #define getpid() _getpid()
100  #define ftruncate( fd, fsize ) _chsize( fd, fsize )
101 
102  #ifndef S_ISCHR
103  # ifdef S_IFCHR
104  # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
105  # else
106  # define S_ISCHR(m) 0
107  # endif
108  #endif /* !S_ISCHR */
109 
110  #ifndef S_ISBLK
111  # define S_ISBLK(m) 0
112  #endif
113 
114  #ifndef S_ISREG
115  # define S_ISREG(m) 1
116  #endif
117 
118  #ifndef S_ISDIR
119  # ifdef S_IFDIR
120  # define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR )
121  # else
122  # define S_ISDIR(m) 0
123  # endif
124  #endif
125 
126  using mode_t = uint32_t;
127 
128  #if !defined(__cplusplus) && !defined( inline )
129  # define inline __inline
130  #endif
131 
132  #if !defined(__func__) // C99 & C++11
133  # define __func__ __FUNCTION__
134  #endif
135 
136 #endif
137 
138 #ifdef _WIN32
139 # define fsync(FD) 0
140 //used in videodevice only - that code is not windows-compatible anyway
141 # define minor(X) 0
142 
143  #if defined(__cplusplus)
144  using uint = unsigned int;
145  #else
146  typedef unsigned int uint;
147  #endif
148 #endif
149 
150 #if defined(__cplusplus) && defined(_WIN32)
151 # include <QtGlobal>
152 
153 #if QT_VERSION >= QT_VERSION_CHECK(5,10,0) && !defined(USING_MINGW)
154  #include <QRandomGenerator>
155  static inline void srandom(unsigned int /*seed*/) { }
156  static inline long int random(void)
157  { return QRandomGenerator::global()->generate64(); }
158 #else
159  // cppcheck-suppress qsrandCalled
160  static inline void srandom(unsigned int seed) { qsrand(seed); }
161  static inline long int random(void) { return qrand(); }
162 #endif
163 
164 # define setenv(x, y, z) ::SetEnvironmentVariableA(x, y)
165 # define unsetenv(x) 0
166 
167 
168  struct statfs {
169  // long f_type; /* type of filesystem */
170  long f_bsize; /* optimal transfer block size */
171  long f_blocks; /* total data blocks in file system */
172  // long f_bfree; /* free blocks in fs */
173  long f_bavail; /* free blocks avail to non-superuser */
174  // long f_files; /* total file nodes in file system */
175  // long f_ffree; /* free file nodes in fs */
176  // long f_fsid; /* file system id */
177  // long f_namelen; /* maximum length of filenames */
178  // long f_spare[6]; /* spare for later */
179  };
180  inline int statfs(const char* path, struct statfs* buffer)
181  {
182  DWORD spc = 0, bps = 0, fc = 0, c = 0;
183 
184  if (buffer && GetDiskFreeSpaceA(path, &spc, &bps, &fc, &c))
185  {
186  buffer->f_bsize = bps;
187  buffer->f_blocks = spc * c;
188  buffer->f_bavail = spc * fc;
189  return 0;
190  }
191 
192  return -1;
193  }
194 #endif
195 
196 #ifdef _WIN32
197 #define lstat stat
198 #define nice(x) ((int)!::SetPriorityClass(\
199  ::GetCurrentProcess(), ((x) < -10) ? \
200  HIGH_PRIORITY_CLASS : (((x) < 0) ? \
201  ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
202  IDLE_PRIORITY_CLASS : (((x) > 0) ? \
203  BELOW_NORMAL_PRIORITY_CLASS : \
204  NORMAL_PRIORITY_CLASS)))))
205 #define PRIO_PROCESS 0
206 #define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
207 #endif // _WIN32
208 
209 #ifdef _MSC_VER
210 # define SIGTRAP SIGBREAK
211 # define STDERR_FILENO (int)GetStdHandle( STD_ERROR_HANDLE )
212 #endif
213 
214 #ifdef _WIN32
215  //signals: not tested
216 # define SIGHUP 1
217 # define SIGQUIT 3
218 # define SIGKILL 9
219 # define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
220 # define SIGUSR2 12 // used to restart LIRC as required
221 # define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
222 # define SIGALRM 14
223 # define SIGCONT 18
224 # define SIGSTOP 19
225 
226 # define S_IRGRP 0
227 # define S_IROTH 0
228 # define O_SYNC 0
229 
230  #define mkfifo(path, mode) \
231  (int)CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
232  PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
233  1024, 1024, 10000, nullptr)
234 
235 # define RTLD_LAZY 0
236 # define dlopen(x, y) LoadLibraryA((x))
237 # define dlclose(x) !FreeLibrary((HMODULE)(x))
238 # define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
239 
240 # ifdef __cplusplus
241  inline const char *dlerror(void)
242  {
243  #define DLERR_MAX 512
244  static char errStr[DLERR_MAX];
245  DWORD errCode = GetLastError();
246 
247  if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
248  FORMAT_MESSAGE_IGNORE_INSERTS |
249  FORMAT_MESSAGE_MAX_WIDTH_MASK,
250  nullptr, errCode,
251  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
252  errStr, DLERR_MAX - 1, nullptr))
253  snprintf(errStr, DLERR_MAX - 1,
254  "dlopen()/dlsym() caused error %d", (int)errCode);
255 
256  return errStr;
257  }
258 # else // __cplusplus
259 # define dlerror() "dlerror() is unimplemented."
260 # endif // __cplusplus
261 
262  // getuid/geteuid/setuid - not implemented
263 # define getuid() 0
264 # define geteuid() 0
265 # define setuid(x) 0
266 # define seteuid(x) 0
267 #endif // _WIN32
268 
269 #if _MSC_VER && !defined(gmtime_r)
270 // FFmpeg libs already have a workaround, use it if the headers are included,
271 // use this otherwise.
272 static __inline struct tm *gmtime_r(const time_t *timep, struct tm *result)
273 {
274  // this is safe on windows, where gmtime uses a thread local variable.
275  // using _gmtime_s() would be better, but needs to be tested on windows.
276  struct tm *tmp = gmtime(timep);
277  if (tmp)
278  {
279  *result = *tmp;
280  return result;
281  }
282  return nullptr;
283 }
284 #endif
285 
286 #if _MSC_VER && !defined(localtime_r)
287 // FFmpeg libs already have a workaround, use it if the headers are included,
288 // use this otherwise.
289 static __inline struct tm *localtime_r(const time_t *timep, struct tm *result)
290 {
291  // this is safe, windows uses a thread local variable for localtime().
292  if (timep && result)
293  {
294  struct tm *win_tmp = localtime(timep);
295  memcpy(result, win_tmp, sizeof(struct tm));
296  return result;
297  }
298  return nullptr;
299 }
300 #endif
301 
302 #ifdef _WIN32
303 // TODO this stuff is not implemented yet
304 # define daemon(x, y) -1
305 # define getloadavg(x, y) -1
306 
307 // this stuff is untested
308 # define WIFEXITED(w) (((w) & 0xff) == 0)
309 # define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
310 # define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
311 # define WEXITSTATUS(w) (((w) >> 8) & 0xff)
312 # define WTERMSIG(w) ((w) & 0x7f)
313 
314 #endif // _WIN32
315 
316 #include <sys/param.h> // Defines BSD on FreeBSD, Mac OS X
317 
318 #include "mythconfig.h"
319 
320 // Libdvdnav now uses off64_t lseek64(), which BSD/Darwin doesn't have.
321 // Luckily, its lseek() is already 64bit compatible
322 #ifdef BSD
323  typedef off_t off64_t; //NOLINT(modernize-use-using) included from dvdnav C code
324  #define lseek64(f,o,w) lseek(f,o,w)
325 #endif
326 
327 #if defined(_MSC_VER)
328 #include <sys/stat.h> // S_IREAD/WRITE on MinGW
329 # define S_IRUSR _S_IREAD
330 # ifndef lseek64
331 # define lseek64( f, o, w ) _lseeki64( f, o, w )
332 # endif
333 #endif
334 
335 #ifdef _WIN32
336 # define fseeko(stream, offset, whence) fseeko64(stream, offset, whence)
337 # define ftello(stream) ftello64(stream)
338 #endif
339 
340 #ifdef Q_OS_ANDROID
341 #ifndef S_IREAD
342 #define S_IREAD S_IRUSR
343 #endif
344 #ifndef S_IWRITE
345 #define S_IWRITE S_IRUSR
346 #endif
347 #endif
348 
349 #ifdef _WIN32
350 # ifdef LZO_COMPILE_TIME_ASSERT_HEADER
351 # undef LZO_COMPILE_TIME_ASSERT_HEADER
352 # endif
353 
354 # define LZO_COMPILE_TIME_ASSERT_HEADER( a )
355 
356 # ifdef LZO_COMPILE_TIME_ASSERT
357 # undef LZO_COMPILE_TIME_ASSERT
358 # endif
359 
360 # define LZO_COMPILE_TIME_ASSERT( a )
361 #endif
362 
363 #ifndef O_NONBLOCK
364 # define O_NONBLOCK 04000
365 #endif
366 
367 #endif // COMPAT_H
srandom
static void srandom(unsigned int seed)
Definition: compat.h:160
tmp
static guint32 * tmp
Definition: goom_core.cpp:31
statfs
int statfs(const char *path, struct statfs *buffer)
Definition: compat.h:180
dlerror
const char * dlerror(void)
Definition: compat.h:241
uint
unsigned int uint
Definition: compat.h:144
off_t
#define off_t
Definition: mythiowrapper.cpp:240
statfs::f_blocks
long f_blocks
Definition: compat.h:171
random
static long int random(void)
Definition: compat.h:161
statfs
Definition: compat.h:168
statfs::f_bsize
long f_bsize
Definition: compat.h:170
DLERR_MAX
#define DLERR_MAX
statfs::f_bavail
long f_bavail
Definition: compat.h:173