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 <QtGlobal> // for Q_OS_XXX
10#endif
11
12#include <sys/param.h> // Defines BSD on FreeBSD, Mac OS X
13
14#include "mythconfig.h"
15
16// Libdvdnav now uses off64_t lseek64(), which BSD/Darwin doesn't have.
17// Luckily, its lseek() is already 64bit compatible
18#ifdef BSD
19 typedef off_t off64_t; //NOLINT(modernize-use-using) included from dvdnav C code
20# define lseek64(f,o,w) lseek(f,o,w)
21#endif
22
23#ifdef Q_OS_ANDROID
24# ifndef S_IREAD
25# define S_IREAD S_IRUSR
26# endif
27# ifndef S_IWRITE
28# define S_IWRITE S_IRUSR
29# endif
30#endif
31
32#ifndef _WIN32
33# include <sys/time.h> // Mac OS X needs this before sys/resource
34# include <sys/resource.h> // for setpriority
35# include <sys/socket.h>
36# include <sys/wait.h> // For WIFEXITED on Mac OS X
37#else // _WIN32
38# ifndef _MSC_VER
39# define close wsock_close
40# endif
41
42# ifndef NOMINMAX
43# define NOMINMAX
44# endif
45
46# include <windows.h>
47
48# undef DialogBox
49# undef LoadImage
50# undef LoadIcon
51# undef GetObject
52# undef DrawText
53# undef CreateDialog
54# undef CreateFont
55# undef DeleteFile
56# undef GetCurrentTime
57# undef SetJob
58# undef SendMessage
59
60# ifndef _MSC_VER
61# include <winsock2.h>
62# include <ws2tcpip.h>
63# else
64# include <io.h>
65# endif
66
67# undef close
68
69# undef CopyFile
70# undef MoveFile
71
72# define fsync(FD) 0
73//used in videodevice only - that code is not windows-compatible anyway
74# define minor(X) 0
75
76 #if defined(__cplusplus)
77 using uint = unsigned int;
78 #else
79 typedef unsigned int uint;
80 #endif
81
82
83# if defined(__cplusplus)
84
85# define setenv(x, y, z) ::SetEnvironmentVariableA(x, y)
86# define unsetenv(x) 0
87
88
89 struct statfs {
90 // long f_type; /* type of filesystem */
91 long f_bsize; /* optimal transfer block size */
92 long f_blocks; /* total data blocks in file system */
93 // long f_bfree; /* free blocks in fs */
94 long f_bavail; /* free blocks avail to non-superuser */
95 // long f_files; /* total file nodes in file system */
96 // long f_ffree; /* free file nodes in fs */
97 // long f_fsid; /* file system id */
98 // long f_namelen; /* maximum length of filenames */
99 // long f_spare[6]; /* spare for later */
100 };
101#pragma GCC diagnostic push
102#pragma GCC diagnostic ignored "-Wshadow"
103 inline int statfs(const char* path, struct statfs* buffer)
104 {
105 DWORD spc = 0, bps = 0, fc = 0, c = 0;
106
107 if (buffer && GetDiskFreeSpaceA(path, &spc, &bps, &fc, &c))
108 {
109 buffer->f_bsize = bps;
110 buffer->f_blocks = spc * c;
111 buffer->f_bavail = spc * fc;
112 return 0;
113 }
114
115 return -1;
116 }
117#pragma GCC diagnostic pop
118# endif
119
120#define lstat stat
121#define nice(x) ((int)!::SetPriorityClass(\
122 ::GetCurrentProcess(), ((x) < -10) ? \
123 HIGH_PRIORITY_CLASS : (((x) < 0) ? \
124 ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
125 IDLE_PRIORITY_CLASS : (((x) > 0) ? \
126 BELOW_NORMAL_PRIORITY_CLASS : \
127 NORMAL_PRIORITY_CLASS)))))
128#define PRIO_PROCESS 0
129#define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
130
131
132 //signals: not tested
133# define SIGHUP 1
134# define SIGQUIT 3
135# define SIGKILL 9
136# define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
137# define SIGUSR2 12 // used to restart LIRC as required
138# define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
139# define SIGALRM 14
140# define SIGCONT 18
141# define SIGSTOP 19
142
143# define O_SYNC 0
144
145 // Success: mkfifo returns zero but CreateNamedPipeA returns a
146 // file handle. Failure: both return -1.
147 #define mkfifo(path, mode) \
148 (CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
149 PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
150 1024, 1024, 10000, nullptr) == INVALID_HANDLE_VALUE ? -1 : 0)
151
152# define RTLD_LAZY 0
153# define dlopen(x, y) LoadLibraryA((x))
154# define dlclose(x) !FreeLibrary((HMODULE)(x))
155# define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
156
157# ifdef __cplusplus
158# include <cstdio>
159 inline const char *dlerror(void)
160 {
161 #define DLERR_MAX 512
162 static char errStr[DLERR_MAX];
163 DWORD errCode = GetLastError();
164
165 if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
166 FORMAT_MESSAGE_IGNORE_INSERTS |
167 FORMAT_MESSAGE_MAX_WIDTH_MASK,
168 nullptr, errCode,
169 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
170 errStr, DLERR_MAX - 1, nullptr))
171 snprintf(errStr, DLERR_MAX - 1,
172 "dlopen()/dlsym() caused error %d", (int)errCode);
173
174 return errStr;
175 }
176# else // __cplusplus
177# include <stdio.h>
178# define dlerror() "dlerror() is unimplemented."
179# endif // __cplusplus
180
181 // getuid/geteuid/setuid - not implemented
182# define getuid() 0
183# define geteuid() 0
184# define setuid(x) 0
185# define seteuid(x) 0
186
187
188// TODO this stuff is not implemented yet
189# define daemon(x, y) -1
190# define getloadavg(x, y) -1
191
192// this stuff is untested
193# define WIFEXITED(w) (((w) & 0xff) == 0)
194# define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
195# define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
196# define WEXITSTATUS(w) (((w) >> 8) & 0xff)
197# define WTERMSIG(w) ((w) & 0x7f)
198
199#endif // _WIN32
200
201
202#ifdef _MSC_VER
203 #include <cstdlib> // for rand()
204 #include <sys/time.h>
205
206 // Turn off the visual studio warnings (identifier was truncated)
207 #pragma warning(disable:4786)
208
209 #ifdef restrict
210 #undef restrict
211 #endif
212
213 #include <cinttypes>
214 #include <direct.h>
215 #include <process.h>
216
217 #define strtoll _strtoi64
218 #define strncasecmp _strnicmp
219 #define snprintf _snprintf
220
221 #ifdef _WIN64
222 using ssize_t = __int64;
223 #else
224 using ssize_t = int;
225 #endif
226
227 // Check for execute, only checking existance in MSVC
228 #define X_OK 0
229
230 #if (_MSC_VER < 1800)
231 #define rint( x ) floor(x + 0.5)
232 #define round( x ) floor(x + 0.5)
233
234 #if ( _MSC_VER < 1700)
235 #define signbit( x ) ( x < 0 )
236 #endif
237
238 #undef M_PI
239 #define M_PI 3.14159265358979323846
240 #endif
241
242 #define getpid() _getpid()
243 #define ftruncate( fd, fsize ) _chsize( fd, fsize )
244
245 #ifndef S_ISCHR
246 # ifdef S_IFCHR
247 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
248 # else
249 # define S_ISCHR(m) 0
250 # endif
251 #endif /* !S_ISCHR */
252
253 #ifndef S_ISBLK
254 # define S_ISBLK(m) 0
255 #endif
256
257 #ifndef S_ISREG
258 # define S_ISREG(m) 1
259 #endif
260
261 #ifndef S_ISDIR
262 # ifdef S_IFDIR
263 # define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR )
264 # else
265 # define S_ISDIR(m) 0
266 # endif
267 #endif
268
269 using mode_t = uint32_t;
270
271 #if !defined(__cplusplus) && !defined( inline )
272 # define inline __inline
273 #endif
274
275 #if !defined(__func__) // C99 & C++11
276 # define __func__ __FUNCTION__
277 #endif
278
279# define SIGTRAP SIGBREAK
280# define STDERR_FILENO (int)GetStdHandle( STD_ERROR_HANDLE )
281
282#include <sys/stat.h> // S_IREAD/WRITE on MinGW
283# define S_IRUSR _S_IREAD
284# ifndef lseek64
285# define lseek64( f, o, w ) _lseeki64( f, o, w )
286# endif
287
288#endif // _MSC_VER
289
290
291#ifndef O_NONBLOCK
292# define O_NONBLOCK 04000 /* NOLINT(cppcoreguidelines-macro-usage) */
293#endif
294
295#endif // COMPAT_H
unsigned int uint
Definition: compat.h:77
#define DLERR_MAX
int statfs(const char *path, struct statfs *buffer)
Definition: compat.h:103
const char * dlerror(void)
Definition: compat.h:159
unsigned int uint
Definition: freesurround.h:24
#define off_t
Definition: compat.h:89
long f_blocks
Definition: compat.h:92
long f_bsize
Definition: compat.h:91
long f_bavail
Definition: compat.h:94