Use nanosleep instead of usleep
[openal-soft/openbsd.git] / OpenAL32 / Include / alMain.h
blobc238a3a95b43d29ff8216ee718d7de1bd78724a6
1 #ifndef AL_MAIN_H
2 #define AL_MAIN_H
4 #include <string.h>
6 #include "alu.h"
8 #ifdef _WIN32
9 #include <windows.h>
10 //#define strcasecmp _stricmp
12 #else
14 #include <assert.h>
15 #include <pthread.h>
16 #include <sys/time.h>
17 #include <time.h>
18 #include <errno.h>
20 #define IsBadWritePtr(a,b) (0)
22 typedef pthread_mutex_t CRITICAL_SECTION;
23 static inline void EnterCriticalSection(CRITICAL_SECTION *cs)
25 int ret;
26 ret = pthread_mutex_lock(cs);
27 assert(ret == 0);
29 static inline void LeaveCriticalSection(CRITICAL_SECTION *cs)
31 int ret;
32 ret = pthread_mutex_unlock(cs);
33 assert(ret == 0);
35 static inline void InitializeCriticalSection(CRITICAL_SECTION *cs)
37 pthread_mutexattr_t attrib;
38 int ret;
40 ret = pthread_mutexattr_init(&attrib);
41 assert(ret == 0);
43 ret = pthread_mutexattr_settype(&attrib, PTHREAD_MUTEX_RECURSIVE);
44 assert(ret == 0);
45 ret = pthread_mutex_init(cs, &attrib);
46 assert(ret == 0);
48 pthread_mutexattr_destroy(&attrib);
51 static inline void DeleteCriticalSection(CRITICAL_SECTION *cs)
53 int ret;
54 ret = pthread_mutex_destroy(cs);
55 assert(ret == 0);
58 /* NOTE: This wrapper isn't quite accurate as it returns an ALuint, as opposed
59 * to the expected DWORD. Both are defined as unsigned 32-bit types, however.
60 * Additionally, Win32 is supposed to measure the time since Windows started,
61 * as opposed to the actual time. */
62 static inline ALuint timeGetTime(void)
64 struct timeval tv;
65 int ret;
67 ret = gettimeofday(&tv, NULL);
68 assert(ret == 0);
70 return tv.tv_usec/1000 + tv.tv_sec*1000;
73 static inline void Sleep(ALuint t)
75 struct timespec tv, rem;
76 tv.tv_nsec = (t*1000000)%1000000000;
77 tv.tv_sec = t/1000;
79 while(nanosleep(&tv, &rem) == -1 && errno == EINTR)
80 tv = rem;
82 #define min(x,y) (((x)<(y))?(x):(y))
83 #define max(x,y) (((x)>(y))?(x):(y))
84 #endif
86 #include "alListener.h"
88 #ifdef __cplusplus
89 extern "C"
91 #endif
93 extern CRITICAL_SECTION _alMutex;
95 extern char _alDebug[256];
97 #define AL_PRINT(...) do { \
98 int _al_print_i; \
99 char *_al_print_fn = strrchr(__FILE__, '/'); \
100 if(!_al_print_fn) _al_print_fn = __FILE__; \
101 else _al_print_fn += 1; \
102 _al_print_i = snprintf(_alDebug, sizeof(_alDebug), "AL lib: %s:%d: ", _al_print_fn, __LINE__); \
103 if(_al_print_i < (int)sizeof(_alDebug) && _al_print_i > 0) \
104 snprintf(_alDebug+_al_print_i, sizeof(_alDebug)-_al_print_i, __VA_ARGS__); \
105 _alDebug[sizeof(_alDebug)-1] = 0; \
106 fprintf(stderr, "%s", _alDebug); \
107 } while(0)
110 #define AL_FORMAT_MONO_FLOAT32 0x10010
111 #define AL_FORMAT_STEREO_FLOAT32 0x10011
113 #define AL_FORMAT_MONO_IMA4 0x1300
114 #define AL_FORMAT_STEREO_IMA4 0x1301
116 #define AL_FORMAT_51CHN8 0x120A
117 #define AL_FORMAT_51CHN16 0x120B
118 #define AL_FORMAT_51CHN32 0x120C
119 #define AL_FORMAT_61CHN8 0x120D
120 #define AL_FORMAT_61CHN16 0x120E
121 #define AL_FORMAT_61CHN32 0x120F
122 #define AL_FORMAT_71CHN8 0x1210
123 #define AL_FORMAT_71CHN16 0x1211
124 #define AL_FORMAT_71CHN32 0x1212
125 #define AL_FORMAT_QUAD8 0x1204
126 #define AL_FORMAT_QUAD16 0x1205
127 #define AL_FORMAT_QUAD32 0x1206
128 #define AL_FORMAT_REAR8 0x1207
129 #define AL_FORMAT_REAR16 0x1208
130 #define AL_FORMAT_REAR32 0x1209
132 #define SWMIXER_OUTPUT_RATE 44100
134 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
136 typedef struct {
137 ALCboolean (*OpenPlayback)(ALCdevice*, const ALCchar*);
138 void (*ClosePlayback)(ALCdevice*);
140 ALCboolean (*OpenCapture)(ALCdevice*, const ALCchar*, ALCuint, ALCenum, ALCsizei);
141 void (*CloseCapture)(ALCdevice*);
142 void (*StartCapture)(ALCdevice*);
143 void (*StopCapture)(ALCdevice*);
144 void (*CaptureSamples)(ALCdevice*, void*, ALCuint);
145 ALCuint (*AvailableSamples)(ALCdevice*);
146 } BackendFuncs;
148 void alc_alsa_init(BackendFuncs *func_list);
149 void alc_oss_init(BackendFuncs *func_list);
150 void alcDSoundInit(BackendFuncs *func_list);
151 void alcWinMMInit(BackendFuncs *FuncList);
154 struct ALCdevice_struct
156 ALboolean InUse;
157 ALboolean IsCaptureDevice;
159 ALuint Frequency;
160 ALuint UpdateFreq;
161 ALuint FrameSize;
162 ALuint Channels;
163 ALenum Format;
165 ALCchar *szDeviceName;
167 // Maximum number of sources that can be created
168 ALuint MaxNoOfSources;
170 // Context created on this device
171 ALCcontext *Context;
173 BackendFuncs *Funcs;
174 void *ExtraData; // For the backend's use
177 #define ALCdevice_OpenPlayback(a,b) ((a)->Funcs->OpenPlayback((a), (b)))
178 #define ALCdevice_ClosePlayback(a) ((a)->Funcs->ClosePlayback((a)))
179 #define ALCdevice_OpenCapture(a,b,c,d,e) ((a)->Funcs->OpenCapture((a), (b), (c), (d), (e)))
180 #define ALCdevice_CloseCapture(a) ((a)->Funcs->CloseCapture((a)))
181 #define ALCdevice_StartCapture(a) ((a)->Funcs->StartCapture((a)))
182 #define ALCdevice_StopCapture(a) ((a)->Funcs->StopCapture((a)))
183 #define ALCdevice_CaptureSamples(a,b,c) ((a)->Funcs->CaptureSamples((a), (b), (c)))
184 #define ALCdevice_AvailableSamples(a) ((a)->Funcs->AvailableSamples((a)))
186 struct ALCcontext_struct
188 ALlistener Listener;
190 struct ALsource *Source;
191 ALuint SourceCount;
193 ALenum LastError;
194 ALboolean InUse;
196 ALuint Frequency;
198 ALenum DistanceModel;
200 ALfloat DopplerFactor;
201 ALfloat DopplerVelocity;
202 ALfloat flSpeedOfSound;
204 ALint lNumMonoSources;
205 ALint lNumStereoSources;
207 ALCdevice *Device;
208 ALCchar ExtensionList[1024];
210 struct bs2b *bs2b;
212 ALCcontext *next;
215 ALCchar *AppendDeviceList(char *name);
216 ALCchar *AppendAllDeviceList(char *name);
217 ALCchar *AppendCaptureDeviceList(char *name);
219 ALCvoid SetALCError(ALenum errorCode);
221 ALCvoid SuspendContext(ALCcontext *context);
222 ALCvoid ProcessContext(ALCcontext *context);
224 ALvoid *StartThread(ALuint (*func)(ALvoid*), ALvoid *ptr);
225 ALuint StopThread(ALvoid *thread);
227 typedef struct RingBuffer RingBuffer;
228 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length);
229 void DestroyRingBuffer(RingBuffer *ring);
230 ALsizei RingBufferSize(RingBuffer *ring);
231 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len);
232 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len);
234 void ReadALConfig(void);
235 void FreeALConfig(void);
236 const char *GetConfigValue(const char *blockName, const char *keyName, const char *def);
237 int GetConfigValueInt(const char *blockName, const char *keyName, int def);
238 float GetConfigValueFloat(const char *blockName, const char *keyName, float def);
240 #ifdef __cplusplus
242 #endif
244 #endif