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 lstat stat
63#define nice(x) ((int)!::SetPriorityClass(\
64 ::GetCurrentProcess(), ((x) < -10) ? \
65 HIGH_PRIORITY_CLASS : (((x) < 0) ? \
66 ABOVE_NORMAL_PRIORITY_CLASS : (((x) > 10) ? \
67 IDLE_PRIORITY_CLASS : (((x) > 0) ? \
68 BELOW_NORMAL_PRIORITY_CLASS : \
69 NORMAL_PRIORITY_CLASS)))))
70#define PRIO_PROCESS 0
71#define setpriority(x, y, z) ((x) == PRIO_PROCESS && y == 0 ? nice(z) : -1)
72
73
74 //signals: not tested
75# define SIGHUP 1
76# define SIGQUIT 3
77# define SIGKILL 9
78# define SIGUSR1 10 // used to force UPnP mediamap rebuild in the backend
79# define SIGUSR2 12 // used to restart LIRC as required
80# define SIGPIPE 13 // not implemented in MINGW, will produce "unable to ignore sigpipe"
81# define SIGALRM 14
82# define SIGCONT 18
83# define SIGSTOP 19
84
85# define O_SYNC 0
86
87 // Success: mkfifo returns zero but CreateNamedPipeA returns a
88 // file handle. Failure: both return -1.
89 #define mkfifo(path, mode) \
90 (CreateNamedPipeA(path, PIPE_ACCESS_DUPLEX | WRITE_DAC, \
91 PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, \
92 1024, 1024, 10000, nullptr) == INVALID_HANDLE_VALUE ? -1 : 0)
93
94# define RTLD_LAZY 0
95# define dlopen(x, y) LoadLibraryA((x))
96# define dlclose(x) !FreeLibrary((HMODULE)(x))
97# define dlsym(x, y) GetProcAddress((HMODULE)(x), (y))
98
99# include <cstdio>
100 inline const char *dlerror(void)
101 {
102 #define DLERR_MAX 512
103 static char errStr[DLERR_MAX];
104 DWORD errCode = GetLastError();
105
106 if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
107 FORMAT_MESSAGE_IGNORE_INSERTS |
108 FORMAT_MESSAGE_MAX_WIDTH_MASK,
109 nullptr, errCode,
110 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
111 errStr, DLERR_MAX - 1, nullptr))
112 snprintf(errStr, DLERR_MAX - 1,
113 "dlopen()/dlsym() caused error %d", (int)errCode);
114
115 return errStr;
116 }
117
118 // getuid/geteuid/setuid - not implemented
119# define getuid() 0
120# define geteuid() 0
121# define setuid(x) 0
122# define seteuid(x) 0
123
124
125// TODO this stuff is not implemented yet
126# define daemon(x, y) -1
127# define getloadavg(x, y) -1
128
129// this stuff is untested
130# define WIFEXITED(w) (((w) & 0xff) == 0)
131# define WIFSIGNALED(w) (((w) & 0x7f) > 0 && (((w) & 0x7f) < 0x7f))
132# define WIFSTOPPED(w) (((w) & 0xff) == 0x7f)
133# define WEXITSTATUS(w) (((w) >> 8) & 0xff)
134# define WTERMSIG(w) ((w) & 0x7f)
135
136#endif // Q_OS_WINDOWS
137
138#ifndef O_NONBLOCK
139# define O_NONBLOCK 04000 /* NOLINT(cppcoreguidelines-macro-usage) */
140#endif
141
142#endif // COMPAT_H
unsigned int uint
Definition: compat.h:60
#define DLERR_MAX
const char * dlerror(void)
Definition: compat.h:100