File: OpenALManager.cpp

package info (click to toggle)
freespace2-launcher-wxlauncher 0.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 2,372 kB
  • ctags: 1,443
  • sloc: cpp: 13,446; python: 797; makefile: 13; sh: 12
file content (462 lines) | stat: -rw-r--r-- 13,534 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
Copyright (C) 2009-2013,2015 wxLauncher Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <wx/wx.h>
#include <wx/dynlib.h>
#include "generated/configure_launcher.h"
#include "apis/FlagListManager.h"
#include "apis/OpenALManager.h"
#include "apis/ProfileManager.h"
#include "global/ProfileKeys.h"

#if USE_OPENAL
/* Include al.h because the wxWidgets FindPackage searches for al.h
 * because otherwise it moves around on different platforms.
 * See the FindOpenAL.cmake file in your CMake distribution for
 * more information. */
#include <AL/al.h>
#include <AL/alc.h>
#endif

#include "global/MemoryDebugging.h"

// from FSO, code/sound/openal.cpp, SVN r8840
// enumeration extension
#ifndef ALC_DEFAULT_ALL_DEVICES_SPECIFIER
#define ALC_DEFAULT_ALL_DEVICES_SPECIFIER        0x1012
#endif

#ifndef ALC_ALL_DEVICES_SPECIFIER
#define ALC_ALL_DEVICES_SPECIFIER                0x1013
#endif

const wxByte BUILD_CAP_NEW_SND = 1<<2;

#if USE_OPENAL
namespace OpenALMan {
wxDynamicLibrary OpenALLib;
bool isInitialized = false;
};
using namespace OpenALMan;
#endif

bool OpenALMan::Initialize() {
#if USE_OPENAL
	if ( isInitialized ) {
		return true;
#if IS_APPLE
	} else if ( OpenALLib.Load(_T("/System/Library/Frameworks/OpenAL.framework/OpenAL"),
							   wxDL_VERBATIM) ) {
		isInitialized = true;
		return true;
	} else if ( OpenALLib.Load(_T("/Library/Frameworks/OpenAL.framework/OpenAL"),
							   wxDL_VERBATIM) ) {
		isInitialized = true;
		return true;
#elif IS_WIN32
	} else if ( OpenALLib.Load(_T("OpenAL32")) ) {
		isInitialized = true;
		return true;
#else
	} else if ( OpenALLib.Load(_T("libopenal")) ) {
		isInitialized = true;
		return true;
#endif
	} else if ( OpenALLib.Load(_T("OpenAL")) ) {
		isInitialized = true;
		return true;
	} else {
		return false;
	}
#else
	return false;
#endif
}

bool OpenALMan::DeInitialize() {
#if USE_OPENAL
	OpenALLib.Unload();
	return true;
#else
	return false;
#endif
}

bool OpenALMan::IsInitialized() {
#if USE_OPENAL
	return isInitialized;
#else
	return false;
#endif
}

bool OpenALMan::WasCompiledIn() {
#if USE_OPENAL
	return true;
#else
	return false;
#endif
}

#if USE_OPENAL
typedef const ALCchar* (ALC_APIENTRY *alcGetStringType)(ALCdevice*, ALenum);
typedef ALCboolean (ALC_APIENTRY *alcIsExtensionPresentType)(ALCdevice*, const ALchar*);
typedef const ALchar* (AL_APIENTRY *alGetStringType)(ALenum);
typedef ALenum (AL_APIENTRY *alGetErrorType)(void);
typedef ALCdevice* (ALC_APIENTRY *alcOpenDeviceType)(const ALCchar *);
typedef ALCboolean (ALC_APIENTRY *alcCloseDeviceType)(ALCdevice *);
typedef ALCcontext* (ALC_APIENTRY *alcCreateContextType)(const ALCdevice*, const ALCint*);
typedef ALCboolean (ALC_APIENTRY *alcMakeContextCurrentType)(ALCcontext*);
typedef void (ALC_APIENTRY *alcDestroyContextType)(ALCcontext*);

namespace OpenALMan {
	template< typename funcPtrType> 
	funcPtrType GetOpenALFunctionPointer(const wxString& name, size_t line);
	bool checkForALError_(size_t line);
};
#define ___GetOALFuncPtr(type, name, line) GetOpenALFunctionPointer<type>(_T(name), line)
#define GetOALFuncPtr(type, name) ___GetOALFuncPtr(type, #name, __LINE__)
#define checkForALError() OpenALMan::checkForALError_(__LINE__)

template< typename funcPtrType> 
funcPtrType 
OpenALMan::GetOpenALFunctionPointer(const wxString& name, size_t line) {
	if ( !OpenALLib.HasSymbol(name) ) {
		wxLogError(
			_T("OpenAL does not have %s() for function containing line %d"),
			name.c_str(), line);
		return NULL;
	}

	funcPtrType pointer = NULL;

	pointer = reinterpret_cast<funcPtrType>(
		OpenALLib.GetSymbol(name));

	if ( pointer == NULL ) {
		wxLogError(_T("Unable to get %s() function from OpenAL, even though it apparently exists for function containing line %d"), name.c_str(), line);
		return NULL;
	}

	return pointer;
}

bool OpenALMan::checkForALError_(size_t line) {
	alGetErrorType getError = GetOALFuncPtr(alGetErrorType, alGetError);
	if ( getError == NULL ) {
		return false;
	}
	ALenum errorcode = (*getError)();
	if ( errorcode == AL_NO_ERROR ) {
		return true;
	} else if ( errorcode == AL_INVALID_NAME ) {
		wxLogError(_T("OpenAL:%ld: a bad name (ID) was passed to an OpenAL function"), line);
	} else if ( errorcode == AL_INVALID_ENUM ) {
		wxLogError(_T("OpenAL:%ld: an invalid enum value was passed to an OpenAL function"), line);
	} else {
		wxLogError(_T("OpenAL:%ld: Unknown error number 0x%08x"), line, errorcode);
	}
	return false;
}
#endif

#if USE_OPENAL
wxArrayString GetAvailableDevices(const ALenum deviceType) {
	wxArrayString arr;
	wxCHECK_MSG(OpenALMan::IsInitialized(), arr,
		_T("GetAvailableDevices called but OpenALMan not initialized"));
	
	wxCHECK_MSG(deviceType == ALC_DEVICE_SPECIFIER ||
		deviceType == ALC_CAPTURE_DEVICE_SPECIFIER, arr,
		wxString::Format(_T("GetAvailableDevices given invalid specifier %d"),
			deviceType));

	ALenum adjustedDeviceType = deviceType;
	
	alcIsExtensionPresentType isExtensionPresent =
		GetOALFuncPtr(alcIsExtensionPresentType, alcIsExtensionPresent);

	if ( isExtensionPresent != NULL ) {
		if ( (*isExtensionPresent)(NULL, "ALC_ENUMERATION_EXT") != AL_TRUE ) {
			wxLogFatalError(_T("OpenAL does not seem to support device enumeration."));
			return arr;
		}
	} else {
		return arr;
	}

	if ((deviceType == ALC_DEVICE_SPECIFIER) &&
			(*isExtensionPresent)(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE) {
		adjustedDeviceType = ALC_ALL_DEVICES_SPECIFIER;
	}
	
	alcGetStringType GetString = GetOALFuncPtr(alcGetStringType, alcGetString);

	if ( GetString != NULL ) {
		const ALCchar* devices = (*GetString)(NULL, adjustedDeviceType);
		if ( devices != NULL ) {
			size_t len;
			size_t offset = 0;
			do {
				len = strlen(devices+offset);
				if ( len > 0 ) {
					wxString device(devices+offset, *wxConvCurrent);
					arr.Add(device);
				}
				offset += len+1;
			} while ( len != 0 );
		} else {
			wxLogError(_T("OpenAL gave NULL for list of devices."));
			return arr;
		}
	} else {
		return arr;
	}
	return arr;
}
#endif

wxArrayString OpenALMan::GetAvailablePlaybackDevices() {
#if USE_OPENAL
	wxCHECK_MSG(OpenALMan::IsInitialized(), wxArrayString(),
		_T("GetAvailablePlaybackDevices called but OpenALMan not initialized"));
	return GetAvailableDevices(ALC_DEVICE_SPECIFIER);
#else
	return wxArrayString();
#endif	
}

wxArrayString OpenALMan::GetAvailableCaptureDevices() {
#if USE_OPENAL
	wxCHECK_MSG(OpenALMan::IsInitialized(), wxArrayString(),
		_T("GetAvailableCaptureDevices called but OpenALMan not initialized"));
	return GetAvailableDevices(ALC_CAPTURE_DEVICE_SPECIFIER);
#else
	return wxArrayString();
#endif
}

#if USE_OPENAL
wxString GetSystemDefaultDevice(const ALenum deviceType) {
	wxCHECK_MSG( OpenALMan::IsInitialized(), wxEmptyString,
		_T("GetSystemDefaultDevice called but OpenALMan not initialized"));
	wxCHECK_MSG(deviceType == ALC_DEFAULT_DEVICE_SPECIFIER ||
		deviceType == ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER, wxEmptyString,
		wxString::Format(_T("GetSystemDefaultDevice given invalid specifier %d"),
			deviceType));
	
	ALenum adjustedDeviceType = deviceType;
	
	alcIsExtensionPresentType isExtensionPresent =
		GetOALFuncPtr(alcIsExtensionPresentType, alcIsExtensionPresent);
	
	if ((isExtensionPresent != NULL) &&
		(deviceType == ALC_DEFAULT_DEVICE_SPECIFIER) &&
		((*isExtensionPresent)(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE)) {
			adjustedDeviceType = ALC_DEFAULT_ALL_DEVICES_SPECIFIER;
	}
	
	alcGetStringType GetString = GetOALFuncPtr(alcGetStringType, alcGetString);

	if ( GetString == NULL ) {
		return wxEmptyString;
	} else {
		const ALCchar* defaultDevice = (*GetString)(NULL, adjustedDeviceType);
		if ( defaultDevice == NULL ) {
			wxLogError(_("Unable to get system default OpenAL %sdevice"),
				(deviceType == ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER ?
					_T("capture ") : wxEmptyString));
			return wxEmptyString;
		} else {
			wxString defaultDeviceStr(defaultDevice, *wxConvCurrent);
			wxLogDebug(_("System default OpenAL %sdevice: %s"),
				(deviceType == ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER ?
					_T("capture ") : wxEmptyString),
				defaultDeviceStr.c_str());
			return defaultDeviceStr;
		}
	}
	return wxEmptyString;
}
#endif

wxString OpenALMan::GetSystemDefaultPlaybackDevice() {
#if USE_OPENAL
	wxCHECK_MSG(OpenALMan::IsInitialized(), wxEmptyString,
		_T("GetSystemDefaultPlaybackDevice called but OpenALMan not initialized"));
	return GetSystemDefaultDevice(ALC_DEFAULT_DEVICE_SPECIFIER);
#else
	return wxEmptyString;
#endif
}

wxString OpenALMan::GetSystemDefaultCaptureDevice() {
#if USE_OPENAL
	wxCHECK_MSG(OpenALMan::IsInitialized(), wxEmptyString,
		_T("GetSystemDefaultCaptureDevice called but OpenALMan not initialized"));
	return GetSystemDefaultDevice(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
#else
	return wxEmptyString;
#endif
}


wxString OpenALMan::GetCurrentVersion() {
#if USE_OPENAL
	alGetStringType GetString = GetOALFuncPtr(alGetStringType,alGetString);

	if ( GetString == NULL ) {
		return _("Unknown version");
	}
	
	wxString selectedDevice;
	ProMan::GetProfileManager()->ProfileRead(PRO_CFG_OPENAL_DEVICE, &selectedDevice);

	alcOpenDeviceType OpenDevice = 
		GetOALFuncPtr(alcOpenDeviceType,alcOpenDevice);
	if ( OpenDevice == NULL) {
		return _("Unable to get open device function");
	}

	ALCdevice* device = (*OpenDevice)(selectedDevice.char_str());
	if ( device == NULL) {
		wxLogError(_T("alcOpenDevice returned NULL for selected device '%s'"),
			selectedDevice.c_str());
		return _("Error opening device");
	}

	alcCreateContextType CreateContext =
		GetOALFuncPtr(alcCreateContextType,alcCreateContext);
	if ( CreateContext == NULL) {
		return _("Unable to get open context on device function");
	}

	ALCint attributes = 0;
	ALCcontext* context = (*CreateContext)(device,NULL);
	if ( context == NULL) {
		return _("Error in opening context");
	}

	alcMakeContextCurrentType MakeContextCurrent =
		GetOALFuncPtr(alcMakeContextCurrentType,alcMakeContextCurrent);
	if ( MakeContextCurrent == NULL) {
		return _("Unable to get the set context as current function");
	}

	if ( (*MakeContextCurrent)(context) != ALC_TRUE || checkForALError() == false ) {
		return _("Error in setting context as current");
	}
	
	const ALCchar* version = (*GetString)(AL_VERSION);
	if ( !checkForALError() || version == NULL ) {
		wxLogError(_T("OpenAL: Unable to retrieve Version String"));
		return _("Unknown version");
	}
	wxString Version(version, wxConvUTF8);

	// unset the current context
	(*MakeContextCurrent)(NULL);

	alcDestroyContextType DestroyContext =
		GetOALFuncPtr(alcDestroyContextType,alcDestroyContext);
	if ( DestroyContext == NULL ) {
		return _("Unable to destroy context");
	}

	(*DestroyContext)(context);
	context = NULL;

	alcCloseDeviceType CloseDevice =
		GetOALFuncPtr(alcCloseDeviceType,alcCloseDevice);
	if ( CloseDevice == NULL ) {
		return _("Unable to close device");
	}

	(*CloseDevice)(device);

	return wxString::Format(_("Detected OpenAL version: %s"), Version.c_str());
#else
	return wxEmptyString;
#endif
}

// bits are adapted from GetCurrentVersion() and FSO, sound/ds.cpp, ds_init()
bool OpenALMan::IsEFXSupported(const wxString& playbackDeviceName) {
#if USE_OPENAL
	wxCHECK_MSG( OpenALMan::IsInitialized(), false,
		_T("IsEFXSupported called but OpenALMan not initialized"));
	
	if (playbackDeviceName.IsEmpty()) {
		wxLogError(_T("IsEFXSupported: playback device name is empty"));
		return false;
	}
	
	alcOpenDeviceType OpenDevice = 
		GetOALFuncPtr(alcOpenDeviceType, alcOpenDevice);

	if (OpenDevice == NULL) {
		wxLogError(_T("IsEFXSupported: Unable to open device."));
		return false;
	}

	ALCdevice* playbackDevice = (*OpenDevice)(playbackDeviceName.char_str());
	
	if (playbackDevice == NULL) {
		wxLogError(
			_T("IsEFXSupported: alcOpenDevice returned NULL when opening device '%s'"),
			playbackDeviceName.c_str());
		return false;
	}

	alcIsExtensionPresentType isExtensionPresent =
		GetOALFuncPtr(alcIsExtensionPresentType, alcIsExtensionPresent);
	
	if (isExtensionPresent == NULL) {
		wxLogError(
			_T("IsEFXSupported: Could not get alcIsExtensionPresent function."));
		return false;
	}
	
	bool hasEFX = (*isExtensionPresent)(playbackDevice, "ALC_EXT_EFX") == AL_TRUE;

	alcCloseDeviceType CloseDevice =
		GetOALFuncPtr(alcCloseDeviceType, alcCloseDevice);
	
	if (CloseDevice == NULL) {
		wxLogError(_T("IsEFXSupported: Unable to close device."));
		return false;
	}

	(*CloseDevice)(playbackDevice);

	return hasEFX;
#else
	return false;
#endif
}

bool OpenALMan::BuildHasNewSoundCode() {
	wxCHECK_MSG(OpenALMan::IsInitialized(), false,
		_T("OpenALMan has not been initialized."));
	wxCHECK_MSG(FlagListManager::IsInitialized(), false,
		_T("FlagListManager has not been initialized."));
	wxCHECK_MSG(FlagListManager::GetFlagListManager()->IsProcessingOK(), false,
		_T("Flag file processing has not (yet) succeeded."));
	
	return (FlagListManager::GetFlagListManager()->GetBuildCaps() & BUILD_CAP_NEW_SND) > 0;
}