| 1 |
/* |
| 2 |
Copyright (C) 2009-2013,2015 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <wx/wx.h> |
| 20 |
#include <wx/dynlib.h> |
| 21 |
#include "generated/configure_launcher.h" |
| 22 |
#include "apis/FlagListManager.h" |
| 23 |
#include "apis/OpenALManager.h" |
| 24 |
#include "apis/ProfileManager.h" |
| 25 |
#include "global/ProfileKeys.h" |
| 26 |
|
| 27 |
#if USE_OPENAL |
| 28 |
/* Include al.h because the wxWidgets FindPackage searches for al.h |
| 29 |
* because otherwise it moves around on different platforms. |
| 30 |
* See the FindOpenAL.cmake file in your CMake distribution for |
| 31 |
* more information. */ |
| 32 |
#include <al.h> |
| 33 |
#include <alc.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "global/MemoryDebugging.h" |
| 37 |
|
| 38 |
// from FSO, code/sound/openal.cpp, SVN r8840 |
| 39 |
// enumeration extension |
| 40 |
#ifndef ALC_DEFAULT_ALL_DEVICES_SPECIFIER |
| 41 |
#define ALC_DEFAULT_ALL_DEVICES_SPECIFIER 0x1012 |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifndef ALC_ALL_DEVICES_SPECIFIER |
| 45 |
#define ALC_ALL_DEVICES_SPECIFIER 0x1013 |
| 46 |
#endif |
| 47 |
|
| 48 |
const wxByte BUILD_CAP_NEW_SND = 1<<2; |
| 49 |
|
| 50 |
#if USE_OPENAL |
| 51 |
namespace OpenALMan { |
| 52 |
wxDynamicLibrary OpenALLib; |
| 53 |
bool isInitialized = false; |
| 54 |
}; |
| 55 |
using namespace OpenALMan; |
| 56 |
#endif |
| 57 |
|
| 58 |
bool OpenALMan::Initialize() { |
| 59 |
#if USE_OPENAL |
| 60 |
if ( isInitialized ) { |
| 61 |
return true; |
| 62 |
#if IS_APPLE |
| 63 |
} else if ( OpenALLib.Load(_T("/System/Library/Frameworks/OpenAL.framework/OpenAL"), |
| 64 |
wxDL_VERBATIM) ) { |
| 65 |
isInitialized = true; |
| 66 |
return true; |
| 67 |
} else if ( OpenALLib.Load(_T("/Library/Frameworks/OpenAL.framework/OpenAL"), |
| 68 |
wxDL_VERBATIM) ) { |
| 69 |
isInitialized = true; |
| 70 |
return true; |
| 71 |
#elif IS_WIN32 |
| 72 |
} else if ( OpenALLib.Load(_T("OpenAL32")) ) { |
| 73 |
isInitialized = true; |
| 74 |
return true; |
| 75 |
#else |
| 76 |
} else if ( OpenALLib.Load(_T("libopenal")) ) { |
| 77 |
isInitialized = true; |
| 78 |
return true; |
| 79 |
#endif |
| 80 |
} else if ( OpenALLib.Load(_T("OpenAL")) ) { |
| 81 |
isInitialized = true; |
| 82 |
return true; |
| 83 |
} else { |
| 84 |
return false; |
| 85 |
} |
| 86 |
#else |
| 87 |
return false; |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
bool OpenALMan::DeInitialize() { |
| 92 |
#if USE_OPENAL |
| 93 |
OpenALLib.Unload(); |
| 94 |
return true; |
| 95 |
#else |
| 96 |
return false; |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
bool OpenALMan::IsInitialized() { |
| 101 |
#if USE_OPENAL |
| 102 |
return isInitialized; |
| 103 |
#else |
| 104 |
return false; |
| 105 |
#endif |
| 106 |
} |
| 107 |
|
| 108 |
bool OpenALMan::WasCompiledIn() { |
| 109 |
#if USE_OPENAL |
| 110 |
return true; |
| 111 |
#else |
| 112 |
return false; |
| 113 |
#endif |
| 114 |
} |
| 115 |
|
| 116 |
#if USE_OPENAL |
| 117 |
typedef const ALCchar* (ALC_APIENTRY *alcGetStringType)(ALCdevice*, ALenum); |
| 118 |
typedef ALCboolean (ALC_APIENTRY *alcIsExtensionPresentType)(ALCdevice*, const ALchar*); |
| 119 |
typedef const ALchar* (AL_APIENTRY *alGetStringType)(ALenum); |
| 120 |
typedef ALenum (AL_APIENTRY *alGetErrorType)(void); |
| 121 |
typedef ALCdevice* (ALC_APIENTRY *alcOpenDeviceType)(const ALCchar *); |
| 122 |
typedef ALCboolean (ALC_APIENTRY *alcCloseDeviceType)(ALCdevice *); |
| 123 |
typedef ALCcontext* (ALC_APIENTRY *alcCreateContextType)(const ALCdevice*, const ALCint*); |
| 124 |
typedef ALCboolean (ALC_APIENTRY *alcMakeContextCurrentType)(ALCcontext*); |
| 125 |
typedef void (ALC_APIENTRY *alcDestroyContextType)(ALCcontext*); |
| 126 |
|
| 127 |
namespace OpenALMan { |
| 128 |
template< typename funcPtrType> |
| 129 |
funcPtrType GetOpenALFunctionPointer(const wxString& name, size_t line); |
| 130 |
bool checkForALError_(size_t line); |
| 131 |
}; |
| 132 |
#define ___GetOALFuncPtr(type, name, line) GetOpenALFunctionPointer<type>(_T(name), line) |
| 133 |
#define GetOALFuncPtr(type, name) ___GetOALFuncPtr(type, #name, __LINE__) |
| 134 |
#define checkForALError() OpenALMan::checkForALError_(__LINE__) |
| 135 |
|
| 136 |
template< typename funcPtrType> |
| 137 |
funcPtrType |
| 138 |
OpenALMan::GetOpenALFunctionPointer(const wxString& name, size_t line) { |
| 139 |
if ( !OpenALLib.HasSymbol(name) ) { |
| 140 |
wxLogError( |
| 141 |
_T("OpenAL does not have %s() for function containing line %d"), |
| 142 |
name.c_str(), line); |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
funcPtrType pointer = NULL; |
| 147 |
|
| 148 |
pointer = reinterpret_cast<funcPtrType>( |
| 149 |
OpenALLib.GetSymbol(name)); |
| 150 |
|
| 151 |
if ( pointer == NULL ) { |
| 152 |
wxLogError(_T("Unable to get %s() function from OpenAL, even though it apparently exists for function containing line %d"), name.c_str(), line); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
return pointer; |
| 157 |
} |
| 158 |
|
| 159 |
bool OpenALMan::checkForALError_(size_t line) { |
| 160 |
alGetErrorType getError = GetOALFuncPtr(alGetErrorType, alGetError); |
| 161 |
if ( getError == NULL ) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
ALenum errorcode = (*getError)(); |
| 165 |
if ( errorcode == AL_NO_ERROR ) { |
| 166 |
return true; |
| 167 |
} else if ( errorcode == AL_INVALID_NAME ) { |
| 168 |
wxLogError(_T("OpenAL:%ld: a bad name (ID) was passed to an OpenAL function"), line); |
| 169 |
} else if ( errorcode == AL_INVALID_ENUM ) { |
| 170 |
wxLogError(_T("OpenAL:%ld: an invalid enum value was passed to an OpenAL function"), line); |
| 171 |
} else { |
| 172 |
wxLogError(_T("OpenAL:%ld: Unknown error number 0x%08x"), line, errorcode); |
| 173 |
} |
| 174 |
return false; |
| 175 |
} |
| 176 |
#endif |
| 177 |
|
| 178 |
#if USE_OPENAL |
| 179 |
wxArrayString GetAvailableDevices(const ALenum deviceType) { |
| 180 |
wxArrayString arr; |
| 181 |
wxCHECK_MSG(OpenALMan::IsInitialized(), arr, |
| 182 |
_T("GetAvailableDevices called but OpenALMan not initialized")); |
| 183 |
|
| 184 |
wxCHECK_MSG(deviceType == ALC_DEVICE_SPECIFIER || |
| 185 |
deviceType == ALC_CAPTURE_DEVICE_SPECIFIER, arr, |
| 186 |
wxString::Format(_T("GetAvailableDevices given invalid specifier %d"), |
| 187 |
deviceType)); |
| 188 |
|
| 189 |
ALenum adjustedDeviceType = deviceType; |
| 190 |
|
| 191 |
alcIsExtensionPresentType isExtensionPresent = |
| 192 |
GetOALFuncPtr(alcIsExtensionPresentType, alcIsExtensionPresent); |
| 193 |
|
| 194 |
if ( isExtensionPresent != NULL ) { |
| 195 |
if ( (*isExtensionPresent)(NULL, "ALC_ENUMERATION_EXT") != AL_TRUE ) { |
| 196 |
wxLogFatalError(_T("OpenAL does not seem to support device enumeration.")); |
| 197 |
return arr; |
| 198 |
} |
| 199 |
} else { |
| 200 |
return arr; |
| 201 |
} |
| 202 |
|
| 203 |
if ((deviceType == ALC_DEVICE_SPECIFIER) && |
| 204 |
(*isExtensionPresent)(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE) { |
| 205 |
adjustedDeviceType = ALC_ALL_DEVICES_SPECIFIER; |
| 206 |
} |
| 207 |
|
| 208 |
alcGetStringType GetString = GetOALFuncPtr(alcGetStringType, alcGetString); |
| 209 |
|
| 210 |
if ( GetString != NULL ) { |
| 211 |
const ALCchar* devices = (*GetString)(NULL, adjustedDeviceType); |
| 212 |
if ( devices != NULL ) { |
| 213 |
size_t len; |
| 214 |
size_t offset = 0; |
| 215 |
do { |
| 216 |
len = strlen(devices+offset); |
| 217 |
if ( len > 0 ) { |
| 218 |
wxString device(devices+offset, *wxConvCurrent); |
| 219 |
arr.Add(device); |
| 220 |
} |
| 221 |
offset += len+1; |
| 222 |
} while ( len != 0 ); |
| 223 |
} else { |
| 224 |
wxLogError(_T("OpenAL gave NULL for list of devices.")); |
| 225 |
return arr; |
| 226 |
} |
| 227 |
} else { |
| 228 |
return arr; |
| 229 |
} |
| 230 |
return arr; |
| 231 |
} |
| 232 |
#endif |
| 233 |
|
| 234 |
wxArrayString OpenALMan::GetAvailablePlaybackDevices() { |
| 235 |
#if USE_OPENAL |
| 236 |
wxCHECK_MSG(OpenALMan::IsInitialized(), wxArrayString(), |
| 237 |
_T("GetAvailablePlaybackDevices called but OpenALMan not initialized")); |
| 238 |
return GetAvailableDevices(ALC_DEVICE_SPECIFIER); |
| 239 |
#else |
| 240 |
return wxArrayString(); |
| 241 |
#endif |
| 242 |
} |
| 243 |
|
| 244 |
wxArrayString OpenALMan::GetAvailableCaptureDevices() { |
| 245 |
#if USE_OPENAL |
| 246 |
wxCHECK_MSG(OpenALMan::IsInitialized(), wxArrayString(), |
| 247 |
_T("GetAvailableCaptureDevices called but OpenALMan not initialized")); |
| 248 |
return GetAvailableDevices(ALC_CAPTURE_DEVICE_SPECIFIER); |
| 249 |
#else |
| 250 |
return wxArrayString(); |
| 251 |
#endif |
| 252 |
} |
| 253 |
|
| 254 |
#if USE_OPENAL |
| 255 |
wxString GetSystemDefaultDevice(const ALenum deviceType) { |
| 256 |
wxCHECK_MSG( OpenALMan::IsInitialized(), wxEmptyString, |
| 257 |
_T("GetSystemDefaultDevice called but OpenALMan not initialized")); |
| 258 |
wxCHECK_MSG(deviceType == ALC_DEFAULT_DEVICE_SPECIFIER || |
| 259 |
deviceType == ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER, wxEmptyString, |
| 260 |
wxString::Format(_T("GetSystemDefaultDevice given invalid specifier %d"), |
| 261 |
deviceType)); |
| 262 |
|
| 263 |
ALenum adjustedDeviceType = deviceType; |
| 264 |
|
| 265 |
alcIsExtensionPresentType isExtensionPresent = |
| 266 |
GetOALFuncPtr(alcIsExtensionPresentType, alcIsExtensionPresent); |
| 267 |
|
| 268 |
if ((isExtensionPresent != NULL) && |
| 269 |
(deviceType == ALC_DEFAULT_DEVICE_SPECIFIER) && |
| 270 |
((*isExtensionPresent)(NULL, "ALC_ENUMERATE_ALL_EXT") == AL_TRUE)) { |
| 271 |
adjustedDeviceType = ALC_DEFAULT_ALL_DEVICES_SPECIFIER; |
| 272 |
} |
| 273 |
|
| 274 |
alcGetStringType GetString = GetOALFuncPtr(alcGetStringType, alcGetString); |
| 275 |
|
| 276 |
if ( GetString == NULL ) { |
| 277 |
return wxEmptyString; |
| 278 |
} else { |
| 279 |
const ALCchar* defaultDevice = (*GetString)(NULL, adjustedDeviceType); |
| 280 |
if ( defaultDevice == NULL ) { |
| 281 |
wxLogError(_("Unable to get system default OpenAL %sdevice"), |
| 282 |
(deviceType == ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER ? |
| 283 |
_T("capture ") : wxEmptyString)); |
| 284 |
return wxEmptyString; |
| 285 |
} else { |
| 286 |
wxString defaultDeviceStr(defaultDevice, *wxConvCurrent); |
| 287 |
wxLogDebug(_("System default OpenAL %sdevice: %s"), |
| 288 |
(deviceType == ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER ? |
| 289 |
_T("capture ") : wxEmptyString), |
| 290 |
defaultDeviceStr.c_str()); |
| 291 |
return defaultDeviceStr; |
| 292 |
} |
| 293 |
} |
| 294 |
return wxEmptyString; |
| 295 |
} |
| 296 |
#endif |
| 297 |
|
| 298 |
wxString OpenALMan::GetSystemDefaultPlaybackDevice() { |
| 299 |
#if USE_OPENAL |
| 300 |
wxCHECK_MSG(OpenALMan::IsInitialized(), wxEmptyString, |
| 301 |
_T("GetSystemDefaultPlaybackDevice called but OpenALMan not initialized")); |
| 302 |
return GetSystemDefaultDevice(ALC_DEFAULT_DEVICE_SPECIFIER); |
| 303 |
#else |
| 304 |
return wxEmptyString; |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
wxString OpenALMan::GetSystemDefaultCaptureDevice() { |
| 309 |
#if USE_OPENAL |
| 310 |
wxCHECK_MSG(OpenALMan::IsInitialized(), wxEmptyString, |
| 311 |
_T("GetSystemDefaultCaptureDevice called but OpenALMan not initialized")); |
| 312 |
return GetSystemDefaultDevice(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER); |
| 313 |
#else |
| 314 |
return wxEmptyString; |
| 315 |
#endif |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
wxString OpenALMan::GetCurrentVersion() { |
| 320 |
#if USE_OPENAL |
| 321 |
alGetStringType GetString = GetOALFuncPtr(alGetStringType,alGetString); |
| 322 |
|
| 323 |
if ( GetString == NULL ) { |
| 324 |
return _("Unknown version"); |
| 325 |
} |
| 326 |
|
| 327 |
wxString selectedDevice; |
| 328 |
ProMan::GetProfileManager()->ProfileRead(PRO_CFG_OPENAL_DEVICE, &selectedDevice); |
| 329 |
|
| 330 |
alcOpenDeviceType OpenDevice = |
| 331 |
GetOALFuncPtr(alcOpenDeviceType,alcOpenDevice); |
| 332 |
if ( OpenDevice == NULL) { |
| 333 |
return _("Unable to get open device function"); |
| 334 |
} |
| 335 |
|
| 336 |
ALCdevice* device = (*OpenDevice)(selectedDevice.char_str()); |
| 337 |
if ( device == NULL) { |
| 338 |
wxLogError(_T("alcOpenDevice returned NULL for selected device '%s'"), |
| 339 |
selectedDevice.c_str()); |
| 340 |
return _("Error opening device"); |
| 341 |
} |
| 342 |
|
| 343 |
alcCreateContextType CreateContext = |
| 344 |
GetOALFuncPtr(alcCreateContextType,alcCreateContext); |
| 345 |
if ( CreateContext == NULL) { |
| 346 |
return _("Unable to get open context on device function"); |
| 347 |
} |
| 348 |
|
| 349 |
ALCint attributes = 0; |
| 350 |
ALCcontext* context = (*CreateContext)(device,NULL); |
| 351 |
if ( context == NULL) { |
| 352 |
return _("Error in opening context"); |
| 353 |
} |
| 354 |
|
| 355 |
alcMakeContextCurrentType MakeContextCurrent = |
| 356 |
GetOALFuncPtr(alcMakeContextCurrentType,alcMakeContextCurrent); |
| 357 |
if ( MakeContextCurrent == NULL) { |
| 358 |
return _("Unable to get the set context as current function"); |
| 359 |
} |
| 360 |
|
| 361 |
if ( (*MakeContextCurrent)(context) != ALC_TRUE || checkForALError() == false ) { |
| 362 |
return _("Error in setting context as current"); |
| 363 |
} |
| 364 |
|
| 365 |
const ALCchar* version = (*GetString)(AL_VERSION); |
| 366 |
if ( !checkForALError() || version == NULL ) { |
| 367 |
wxLogError(_T("OpenAL: Unable to retrieve Version String")); |
| 368 |
return _("Unknown version"); |
| 369 |
} |
| 370 |
wxString Version(version, wxConvUTF8); |
| 371 |
|
| 372 |
// unset the current context |
| 373 |
(*MakeContextCurrent)(NULL); |
| 374 |
|
| 375 |
alcDestroyContextType DestroyContext = |
| 376 |
GetOALFuncPtr(alcDestroyContextType,alcDestroyContext); |
| 377 |
if ( DestroyContext == NULL ) { |
| 378 |
return _("Unable to destroy context"); |
| 379 |
} |
| 380 |
|
| 381 |
(*DestroyContext)(context); |
| 382 |
context = NULL; |
| 383 |
|
| 384 |
alcCloseDeviceType CloseDevice = |
| 385 |
GetOALFuncPtr(alcCloseDeviceType,alcCloseDevice); |
| 386 |
if ( CloseDevice == NULL ) { |
| 387 |
return _("Unable to close device"); |
| 388 |
} |
| 389 |
|
| 390 |
(*CloseDevice)(device); |
| 391 |
|
| 392 |
return wxString::Format(_("Detected OpenAL version: %s"), Version.c_str()); |
| 393 |
#else |
| 394 |
return wxEmptyString; |
| 395 |
#endif |
| 396 |
} |
| 397 |
|
| 398 |
// bits are adapted from GetCurrentVersion() and FSO, sound/ds.cpp, ds_init() |
| 399 |
bool OpenALMan::IsEFXSupported(const wxString& playbackDeviceName) { |
| 400 |
#if USE_OPENAL |
| 401 |
wxCHECK_MSG( OpenALMan::IsInitialized(), false, |
| 402 |
_T("IsEFXSupported called but OpenALMan not initialized")); |
| 403 |
|
| 404 |
if (playbackDeviceName.IsEmpty()) { |
| 405 |
wxLogError(_T("IsEFXSupported: playback device name is empty")); |
| 406 |
return false; |
| 407 |
} |
| 408 |
|
| 409 |
alcOpenDeviceType OpenDevice = |
| 410 |
GetOALFuncPtr(alcOpenDeviceType, alcOpenDevice); |
| 411 |
|
| 412 |
if (OpenDevice == NULL) { |
| 413 |
wxLogError(_T("IsEFXSupported: Unable to open device.")); |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
ALCdevice* playbackDevice = (*OpenDevice)(playbackDeviceName.char_str()); |
| 418 |
|
| 419 |
if (playbackDevice == NULL) { |
| 420 |
wxLogError( |
| 421 |
_T("IsEFXSupported: alcOpenDevice returned NULL when opening device '%s'"), |
| 422 |
playbackDeviceName.c_str()); |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
alcIsExtensionPresentType isExtensionPresent = |
| 427 |
GetOALFuncPtr(alcIsExtensionPresentType, alcIsExtensionPresent); |
| 428 |
|
| 429 |
if (isExtensionPresent == NULL) { |
| 430 |
wxLogError( |
| 431 |
_T("IsEFXSupported: Could not get alcIsExtensionPresent function.")); |
| 432 |
return false; |
| 433 |
} |
| 434 |
|
| 435 |
bool hasEFX = (*isExtensionPresent)(playbackDevice, "ALC_EXT_EFX") == AL_TRUE; |
| 436 |
|
| 437 |
alcCloseDeviceType CloseDevice = |
| 438 |
GetOALFuncPtr(alcCloseDeviceType, alcCloseDevice); |
| 439 |
|
| 440 |
if (CloseDevice == NULL) { |
| 441 |
wxLogError(_T("IsEFXSupported: Unable to close device.")); |
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
(*CloseDevice)(playbackDevice); |
| 446 |
|
| 447 |
return hasEFX; |
| 448 |
#else |
| 449 |
return false; |
| 450 |
#endif |
| 451 |
} |
| 452 |
|
| 453 |
bool OpenALMan::BuildHasNewSoundCode() { |
| 454 |
wxCHECK_MSG(OpenALMan::IsInitialized(), false, |
| 455 |
_T("OpenALMan has not been initialized.")); |
| 456 |
wxCHECK_MSG(FlagListManager::IsInitialized(), false, |
| 457 |
_T("FlagListManager has not been initialized.")); |
| 458 |
wxCHECK_MSG(FlagListManager::GetFlagListManager()->IsProcessingOK(), false, |
| 459 |
_T("Flag file processing has not (yet) succeeded.")); |
| 460 |
|
| 461 |
return (FlagListManager::GetFlagListManager()->GetBuildCaps() & BUILD_CAP_NEW_SND) > 0; |
| 462 |
} |