System.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006-2018 Chris Cannam and QMUL.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #ifndef SV_SYSTEM_H
17 #define SV_SYSTEM_H
18 
19 #include "base/Debug.h"
20 
21 #ifdef _WIN32
22 
23 #include <windows.h>
24 #include <malloc.h>
25 #include <process.h>
26 #include <math.h>
27 
28 #define MLOCK(a,b) 1
29 #define MUNLOCK(a,b) 1
30 #define MUNLOCK_SAMPLEBLOCK(a) 1
31 #define MUNLOCKALL() 1
32 
33 extern void SystemMemoryBarrier();
34 #define MBARRIER() SystemMemoryBarrier()
35 
36 #define DLOPEN(a,b) LoadLibrary((a).toStdWString().c_str())
37 #define DLSYM(a,b) GetProcAddress((HINSTANCE)(a),(b))
38 #define DLCLOSE(a) (!FreeLibrary((HINSTANCE)(a)))
39 #define DLERROR() ""
40 
41 #define PLUGIN_GLOB "*.dll"
42 #define PATH_SEPARATOR ';'
43 
44 // The default Vamp plugin path is obtained from a function in the
45 // Vamp SDK (Vamp::PluginHostAdapter::getPluginPath).
46 
47 // At the time of writing, at least, the vast majority of LADSPA
48 // plugins on Windows hosts will have been put there for use in
49 // Audacity. It's a bit of a shame that Audacity uses its own Program
50 // Files directory for plugins that any host may want to use... maybe
51 // they were just following the example of VSTs, which are usually
52 // found in Steinberg's Program Files directory. Anyway, we can
53 // greatly increase our chances of picking up some LADSPA plugins by
54 // default if we include the Audacity plugin location as well as an
55 // (imho) more sensible place.
56 
57 #define DEFAULT_LADSPA_PATH "%ProgramFiles%\\LADSPA Plugins;%ProgramFiles%\\Audacity\\Plug-Ins"
58 #define DEFAULT_DSSI_PATH "%ProgramFiles%\\DSSI Plugins"
59 
60 #define getpid _getpid
61 
62 #if defined(_MSC_VER)
63 #include <BaseTsd.h>
64 typedef SSIZE_T ssize_t;
65 #endif
66 
67 #ifdef _MSC_VER
68 extern "C" {
69 void usleep(unsigned long usec);
70 }
71 #else
72 #include <unistd.h>
73 #endif
74 
75 extern "C" {
76 int gettimeofday(struct timeval *p, void *tz);
77 }
78 
79 #define ISNAN std::isnan
80 #define ISINF std::isinf
81 
82 #else
83 
84 #include <sys/mman.h>
85 #include <dlfcn.h>
86 #include <stdio.h> // for perror
87 #include <cmath>
88 #include <unistd.h> // sleep + usleep primarily
89 
90 #define MLOCK(a,b) ::mlock((a),(b))
91 #define MUNLOCK(a,b) (::munlock((a),(b)) ? (::perror("munlock failed"), 0) : 0)
92 #define MUNLOCK_SAMPLEBLOCK(a) do { if (!(a).empty()) { const float &b = *(a).begin(); MUNLOCK(&b, (a).capacity() * sizeof(float)); } } while(0);
93 //#define MLOCK(a,b) 1
94 //#define MUNLOCK(a,b) 1
95 //#define MUNLOCK_SAMPLEBLOCK(a) 1
96 
97 #define DLOPEN(a,b) dlopen((a).toStdString().c_str(),(b))
98 #define DLSYM(a,b) dlsym((a),(b))
99 #define DLCLOSE(a) dlclose((a))
100 #define DLERROR() dlerror()
101 
102 #include <cmath>
103 #define ISNAN std::isnan
104 #define ISINF std::isinf
105 
106 #ifdef __APPLE__
107 
108 #define PLUGIN_GLOB "*.dylib *.so"
109 #define PATH_SEPARATOR ':'
110 
111 #define DEFAULT_LADSPA_PATH "$HOME/Library/Audio/Plug-Ins/LADSPA:/Library/Audio/Plug-Ins/LADSPA"
112 #define DEFAULT_DSSI_PATH "$HOME/Library/Audio/Plug-Ins/DSSI:/Library/Audio/Plug-Ins/DSSI"
113 
114 #define MUNLOCKALL() 1
115 
116 #include <libkern/OSAtomic.h>
117 #define MBARRIER() OSMemoryBarrier()
118 
119 #else
120 
121 #ifdef sun
122 #undef MLOCK
123 #undef MUNLOCK
124 #define MLOCK(a,b) ::mlock((char *)a,b)
125 #define MUNLOCK(a,b) ::munlock((char *)a,b)
126 #ifdef __SUNPRO_CC
127 #undef ISNAN
128 #undef ISINF
129 #define ISNAN(x) ((x)!=(x))
130 #define ISINF(x) 0
131 #endif
132 #endif
133 
134 #define PLUGIN_GLOB "*.so"
135 #define PATH_SEPARATOR ':'
136 
137 #define DEFAULT_LADSPA_PATH "$HOME/ladspa:$HOME/.ladspa:/usr/local/lib/ladspa:/usr/lib/ladspa"
138 #define DEFAULT_DSSI_PATH "$HOME/dssi:$HOME/.dssi:/usr/local/lib/dssi:/usr/lib/dssi"
139 
140 #define MUNLOCKALL() ::munlockall()
141 
142 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
143 #define MBARRIER() __sync_synchronize()
144 #else
145 extern void SystemMemoryBarrier();
146 #define MBARRIER() SystemMemoryBarrier()
147 #endif
148 
149 #endif /* ! __APPLE__ */
150 
151 #endif /* ! _WIN32 */
152 
154 extern ProcessStatus GetProcessStatus(int pid);
155 
156 // Return a vague approximation to the number of free megabytes of real memory.
157 // Return -1 if unknown. (Hence signed args.) Note that this could be more than
158 // is actually addressable, e.g. for a 32-bit process on a 64-bit system.
159 extern void GetRealMemoryMBAvailable(ssize_t &available, ssize_t &total);
160 
161 // Return a vague approximation to the number of free megabytes of
162 // disc space on the partition containing the given path. Return -1
163 // if unknown. (Hence signed return type)
164 extern ssize_t GetDiscSpaceMBAvailable(const char *path);
165 
166 extern "C" {
167 
168 // Return true if the OS desktop is set to use a dark mode
169 // theme. Return false if it is set to a light theme or if the theme
170 // is unknown.
171 extern bool OSReportsDarkThemeActive();
172 
173 // Return true if the OS desktop reports an accent colour to go with
174 // the current theme; if so, also return by reference the r, g, and b
175 // components of the colour (range 0-255). Return false if we can't
176 // query such a thing.
177 extern bool OSQueryAccentColour(int *r, int *g, int *b);
178 
179 }
180 
181 extern void StoreStartupLocale();
182 extern void RestoreStartupLocale();
183 
184 #include <cmath>
185 
186 #ifndef M_PI
187 #define M_PI 3.14159265358979323846
188 #endif
189 
190 extern double mod(double x, double y);
191 extern float modf(float x, float y);
192 
193 extern double princarg(double a);
194 extern float princargf(float a);
195 
196 #ifdef USE_POW_NO_F
197 #define powf pow
198 #endif
199 
204 extern bool getEnvUtf8(std::string variable, std::string &value);
205 
210 extern bool putEnvUtf8(std::string variable, std::string value);
211 
212 #endif /* ! _SYSTEM_H_ */
213 
214 
float princargf(float a)
Definition: System.cpp:341
ProcessStatus GetProcessStatus(int pid)
Definition: System.cpp:84
void StoreStartupLocale()
Definition: System.cpp:319
double princarg(double a)
Definition: System.cpp:340
ProcessStatus
Definition: System.h:153
void SystemMemoryBarrier()
bool putEnvUtf8(std::string variable, std::string value)
Set the value of the given environment variable.
Definition: System.cpp:407
ssize_t GetDiscSpaceMBAvailable(const char *path)
Definition: System.cpp:262
bool OSReportsDarkThemeActive()
Definition: os-other.cpp:5
bool getEnvUtf8(std::string variable, std::string &value)
Return the value of the given environment variable by reference.
Definition: System.cpp:344
void GetRealMemoryMBAvailable(ssize_t &available, ssize_t &total)
Definition: System.cpp:126
double mod(double x, double y)
Definition: System.cpp:337
void RestoreStartupLocale()
Definition: System.cpp:328
float modf(float x, float y)
Definition: System.cpp:338
bool OSQueryAccentColour(int *r, int *g, int *b)
Definition: os-win10.cpp:27