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