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# endif
80
81#define lstat stat
82#define nice(x) ((int)!::SetPriorityClass(\
83 ::GetCurrentProcess(), ((x) < -10) ? \
84 HIGH_PRIORITY_CLASS : (((x) < 0) ? \
85 ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
86 IDLE_PRIORITY_CLASS : (((x) > 0) ? \
87 BELOW_NORMAL_PRIORITY_CLASS : \
88 NORMAL_PRIORITY_CLASS)))))
89#define PRIO_PROCESS 0
90#define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
91
92
93 //signals: not tested
94# define SIGHUP 1
95# define SIGQUIT 3
96# define SIGKILL 9
97# define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
98# define SIGUSR2 12 // used to restart LIRC as required
99# define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
100# define SIGALRM 14
101# define SIGCONT 18
102# define SIGSTOP 19
103
104# define O_SYNC 0
105
106 // Success: mkfifo returns zero but CreateNamedPipeA returns a
107 // file handle. Failure: both return -1.
108 #define mkfifo(path, mode) \
109 (CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
110 PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
111 1024, 1024, 10000, nullptr) == INVALID_HANDLE_VALUE ? -1 : 0)
112
113# define RTLD_LAZY 0
114# define dlopen(x, y) LoadLibraryA((x))
115# define dlclose(x) !FreeLibrary((HMODULE)(x))
116# define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
117
118# ifdef __cplusplus
119# include <cstdio>
120 inline const char *dlerror(void)
121 {
122 #define DLERR_MAX 512
123 static char errStr[DLERR_MAX];
124 DWORD errCode = GetLastError();
125
126 if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
127 FORMAT_MESSAGE_IGNORE_INSERTS |
128 FORMAT_MESSAGE_MAX_WIDTH_MASK,
129 nullptr, errCode,
130 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
131 errStr, DLERR_MAX - 1, nullptr))
132 snprintf(errStr, DLERR_MAX - 1,
133 "dlopen()/dlsym() caused error %d", (int)errCode);
134
135 return errStr;
136 }
137# else // __cplusplus
138# include <stdio.h>
139# define dlerror() "dlerror() is unimplemented."
140# endif // __cplusplus
141
142 // getuid/geteuid/setuid - not implemented
143# define getuid() 0
144# define geteuid() 0
145# define setuid(x) 0
146# define seteuid(x) 0
147
148
149// TODO this stuff is not implemented yet
150# define daemon(x, y) -1
151# define getloadavg(x, y) -1
152
153// this stuff is untested
154# define WIFEXITED(w) (((w) & 0xff) == 0)
155# define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
156# define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
157# define WEXITSTATUS(w) (((w) >> 8) & 0xff)
158# define WTERMSIG(w) ((w) & 0x7f)
159
160#endif // _WIN32
161
162
163#ifdef _MSC_VER
164 #include <cstdlib> // for rand()
165 #include <sys/time.h>
166
167 // Turn off the visual studio warnings (identifier was truncated)
168 #pragma warning(disable:4786)
169
170 #include <cinttypes>
171 #include <direct.h>
172 #include <process.h>
173
174 #define strtoll _strtoi64
175 #define strncasecmp _strnicmp
176 #define snprintf _snprintf
177
178 #ifdef _WIN64
179 using ssize_t = __int64;
180 #else
181 using ssize_t = int;
182 #endif
183
184 // Check for execute, only checking existance in MSVC
185 #define X_OK 0
186
187 #define getpid() _getpid()
188 #define ftruncate( fd, fsize ) _chsize( fd, fsize )
189
190 #ifndef S_ISCHR
191 # ifdef S_IFCHR
192 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
193 # else
194 # define S_ISCHR(m) 0
195 # endif
196 #endif /* !S_ISCHR */
197
198 #ifndef S_ISBLK
199 # define S_ISBLK(m) 0
200 #endif
201
202 #ifndef S_ISREG
203 # define S_ISREG(m) 1
204 #endif
205
206 #ifndef S_ISDIR
207 # ifdef S_IFDIR
208 # define S_ISDIR(m) (((m) & S_IFDIR) == S_IFDIR )
209 # else
210 # define S_ISDIR(m) 0
211 # endif
212 #endif
213
214 using mode_t = uint32_t;
215
216# define SIGTRAP SIGBREAK
217# define STDERR_FILENO (int)GetStdHandle( STD_ERROR_HANDLE )
218
219#include <sys/stat.h> // S_IREAD/WRITE on MinGW
220# define S_IRUSR _S_IREAD
221
222# define lseek _lseeki64
223# define off_t __int64
224#endif // _MSC_VER
225
226
227#ifndef O_NONBLOCK
228# define O_NONBLOCK 04000 /* NOLINT(cppcoreguidelines-macro-usage) */
229#endif
230
231#endif // COMPAT_H
unsigned int uint
Definition: compat.h:68
#define DLERR_MAX
const char * dlerror(void)
Definition: compat.h:120