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