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#include <QtGlobal>
9#if QT_VERSION >= QT_VERSION_CHECK(6,5,0)
10#include <QtSystemDetection>
11#endif
12
13#ifdef Q_OS_ANDROID
14# ifndef S_IREAD
15# define S_IREAD S_IRUSR
16# endif
17# ifndef S_IWRITE
18# define S_IWRITE S_IRUSR
19# endif
20#endif
21
22#ifndef Q_OS_WINDOWS
23# include <sys/time.h> // Mac OS X needs this before sys/resource
24# include <sys/resource.h> // for setpriority
25# include <sys/socket.h>
26# include <sys/wait.h> // For WIFEXITED on Mac OS X
27#else // Q_OS_WINDOWS
28# define close wsock_close
29
30# ifndef NOMINMAX
31# define NOMINMAX
32# endif
33
34# include <windows.h>
35
36# undef DialogBox
37# undef LoadImage
38# undef LoadIcon
39# undef GetObject
40# undef DrawText
41# undef CreateDialog
42# undef CreateFont
43# undef DeleteFile
44# undef GetCurrentTime
45# undef SetJob
46# undef SendMessage
47
48# include <winsock2.h>
49# include <ws2tcpip.h>
50
51# undef close
52
53# undef CopyFile
54# undef MoveFile
55
56# define fsync(FD) 0
57//used in videodevice only - that code is not windows-compatible anyway
58# define minor(X) 0
59
60 using uint = unsigned int;
61
62# define setenv(x, y, z) ::SetEnvironmentVariableA(x, y)
63# define unsetenv(x) 0
64
65#define lstat stat
66#define nice(x) ((int)!::SetPriorityClass(\
67 ::GetCurrentProcess(), ((x) < -10) ? \
68 HIGH_PRIORITY_CLASS : (((x) < 0) ? \
69 ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
70 IDLE_PRIORITY_CLASS : (((x) > 0) ? \
71 BELOW_NORMAL_PRIORITY_CLASS : \
72 NORMAL_PRIORITY_CLASS)))))
73#define PRIO_PROCESS 0
74#define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
75
76
77 //signals: not tested
78# define SIGHUP 1
79# define SIGQUIT 3
80# define SIGKILL 9
81# define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
82# define SIGUSR2 12 // used to restart LIRC as required
83# define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
84# define SIGALRM 14
85# define SIGCONT 18
86# define SIGSTOP 19
87
88# define O_SYNC 0
89
90 // Success: mkfifo returns zero but CreateNamedPipeA returns a
91 // file handle. Failure: both return -1.
92 #define mkfifo(path, mode) \
93 (CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
94 PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
95 1024, 1024, 10000, nullptr) == INVALID_HANDLE_VALUE ? -1 : 0)
96
97# define RTLD_LAZY 0
98# define dlopen(x, y) LoadLibraryA((x))
99# define dlclose(x) !FreeLibrary((HMODULE)(x))
100# define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
101
102# include <cstdio>
103 inline const char *dlerror(void)
104 {
105 #define DLERR_MAX 512
106 static char errStr[DLERR_MAX];
107 DWORD errCode = GetLastError();
108
109 if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
110 FORMAT_MESSAGE_IGNORE_INSERTS |
111 FORMAT_MESSAGE_MAX_WIDTH_MASK,
112 nullptr, errCode,
113 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
114 errStr, DLERR_MAX - 1, nullptr))
115 snprintf(errStr, DLERR_MAX - 1,
116 "dlopen()/dlsym() caused error %d", (int)errCode);
117
118 return errStr;
119 }
120
121 // getuid/geteuid/setuid - not implemented
122# define getuid() 0
123# define geteuid() 0
124# define setuid(x) 0
125# define seteuid(x) 0
126
127
128// TODO this stuff is not implemented yet
129# define daemon(x, y) -1
130# define getloadavg(x, y) -1
131
132// this stuff is untested
133# define WIFEXITED(w) (((w) & 0xff) == 0)
134# define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
135# define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
136# define WEXITSTATUS(w) (((w) >> 8) & 0xff)
137# define WTERMSIG(w) ((w) & 0x7f)
138
139#endif // Q_OS_WINDOWS
140
141#ifndef O_NONBLOCK
142# define O_NONBLOCK 04000 /* NOLINT(cppcoreguidelines-macro-usage) */
143#endif
144
145#endif // COMPAT_H
unsigned int uint
Definition: compat.h:60
#define DLERR_MAX
const char * dlerror(void)
Definition: compat.h:103