aldlist.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2006, Creative Labs Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice, this list of conditions and
  9. * the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
  11. * and the following disclaimer in the documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Creative Labs Inc. nor the names of its contributors may be used to endorse or
  13. * promote products derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  17. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  19. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  22. * POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "core/strings/stringFunctions.h"
  25. #include "aldlist.h"
  26. #if defined(TORQUE_OS_MAC)
  27. #include <OpenAL/alc.h>
  28. #elif defined(TORQUE_OS_LINUX)
  29. #include <AL/alc.h>
  30. #else
  31. #include <al/alc.h>
  32. #endif
  33. /*
  34. * Init call
  35. */
  36. ALDeviceList::ALDeviceList( const OPENALFNTABLE &oalft )
  37. {
  38. VECTOR_SET_ASSOCIATION( vDeviceInfo );
  39. ALDEVICEINFO ALDeviceInfo;
  40. char *devices;
  41. int index;
  42. const char *defaultDeviceName;
  43. const char *actualDeviceName;
  44. dMemcpy( &ALFunction, &oalft, sizeof( OPENALFNTABLE ) );
  45. // DeviceInfo vector stores, for each enumerated device, it's device name, selection status, spec version #, and extension support
  46. vDeviceInfo.clear();
  47. vDeviceInfo.reserve(10);
  48. defaultDeviceIndex = 0;
  49. // grab function pointers for 1.0-API functions, and if successful proceed to enumerate all devices
  50. const bool enumerationExtensionPresent = ALFunction.alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT");
  51. const bool enumerateAllExtensionPresent = ALFunction.alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT");
  52. if (enumerateAllExtensionPresent || enumerationExtensionPresent) {
  53. if (enumerateAllExtensionPresent)
  54. {
  55. devices = (char *)ALFunction.alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
  56. defaultDeviceName = (char *)ALFunction.alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER);
  57. }
  58. else
  59. {
  60. devices = (char *)ALFunction.alcGetString(NULL, ALC_DEVICE_SPECIFIER);
  61. defaultDeviceName = (char *)ALFunction.alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
  62. }
  63. index = 0;
  64. // go through device list (each device terminated with a single NULL, list terminated with double NULL)
  65. while (*devices != 0) {
  66. if (String::compare(defaultDeviceName, devices) == 0) {
  67. defaultDeviceIndex = index;
  68. }
  69. ALCdevice *device = ALFunction.alcOpenDevice(devices);
  70. if (device) {
  71. ALCcontext *context = ALFunction.alcCreateContext(device, NULL);
  72. if (context) {
  73. ALFunction.alcMakeContextCurrent(context);
  74. // if new actual device name isn't already in the list, then add it...
  75. actualDeviceName = devices;
  76. bool bNewName = true;
  77. for (int i = 0; i < GetNumDevices(); i++) {
  78. if (String::compare(GetDeviceName(i), actualDeviceName) == 0) {
  79. bNewName = false;
  80. }
  81. }
  82. if ((bNewName) && (actualDeviceName != NULL) && (dStrlen(actualDeviceName) > 0)) {
  83. dMemset(&ALDeviceInfo, 0, sizeof(ALDEVICEINFO));
  84. ALDeviceInfo.bSelected = true;
  85. dStrncpy(ALDeviceInfo.strDeviceName, actualDeviceName, sizeof(ALDeviceInfo.strDeviceName));
  86. ALFunction.alcGetIntegerv(device, ALC_MAJOR_VERSION, sizeof(int), &ALDeviceInfo.iMajorVersion);
  87. ALFunction.alcGetIntegerv(device, ALC_MINOR_VERSION, sizeof(int), &ALDeviceInfo.iMinorVersion);
  88. ALDeviceInfo.iCapsFlags = 0;
  89. // Check for ALC Extensions
  90. if (ALFunction.alcIsExtensionPresent(device, "ALC_EXT_CAPTURE") == AL_TRUE)
  91. ALDeviceInfo.iCapsFlags |= SFXALCapture;
  92. if (ALFunction.alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_TRUE)
  93. ALDeviceInfo.iCapsFlags |= SFXALEFX;
  94. // Check for AL Extensions
  95. if (ALFunction.alIsExtensionPresent("AL_EXT_OFFSET") == AL_TRUE)
  96. ALDeviceInfo.iCapsFlags |= SFXALOffset;
  97. if (ALFunction.alIsExtensionPresent("AL_EXT_LINEAR_DISTANCE") == AL_TRUE)
  98. ALDeviceInfo.iCapsFlags |= SFXALLinearDistance;
  99. if (ALFunction.alIsExtensionPresent("AL_EXT_EXPONENT_DISTANCE") == AL_TRUE)
  100. ALDeviceInfo.iCapsFlags |= SFXALExponentDistance;
  101. if (ALFunction.alIsExtensionPresent("EAX2.0") == AL_TRUE)
  102. ALDeviceInfo.iCapsFlags |= SFXALEAX2;
  103. if (ALFunction.alIsExtensionPresent("EAX3.0") == AL_TRUE)
  104. ALDeviceInfo.iCapsFlags |= SFXALEAX3;
  105. if (ALFunction.alIsExtensionPresent("EAX4.0") == AL_TRUE)
  106. ALDeviceInfo.iCapsFlags |= SFXALEAX4;
  107. if (ALFunction.alIsExtensionPresent("EAX5.0") == AL_TRUE)
  108. ALDeviceInfo.iCapsFlags |= SFXALEAX5;
  109. if (ALFunction.alIsExtensionPresent("EAX-RAM") == AL_TRUE)
  110. ALDeviceInfo.iCapsFlags |= SFXALEAXRAM;
  111. // Get Source Count
  112. ALDeviceInfo.uiSourceCount = GetMaxNumSources();
  113. vDeviceInfo.push_back(ALDeviceInfo);
  114. }
  115. ALFunction.alcMakeContextCurrent(NULL);
  116. ALFunction.alcDestroyContext(context);
  117. }
  118. ALFunction.alcCloseDevice(device);
  119. }
  120. devices += dStrlen(devices) + 1;
  121. index += 1;
  122. }
  123. }
  124. ResetFilters();
  125. }
  126. /*
  127. * Exit call
  128. */
  129. ALDeviceList::~ALDeviceList()
  130. {
  131. }
  132. /*
  133. * Returns the number of devices in the complete device list
  134. */
  135. int ALDeviceList::GetNumDevices()
  136. {
  137. return (int)vDeviceInfo.size();
  138. }
  139. /*
  140. * Returns the device name at an index in the complete device list
  141. */
  142. const char *ALDeviceList::GetDeviceName(int index)
  143. {
  144. if (index < GetNumDevices())
  145. return vDeviceInfo[index].strDeviceName;
  146. else
  147. return NULL;
  148. }
  149. /*
  150. * Returns the major and minor version numbers for a device at a specified index in the complete list
  151. */
  152. void ALDeviceList::GetDeviceVersion(int index, int *major, int *minor)
  153. {
  154. if (index < GetNumDevices()) {
  155. if (major)
  156. *major = vDeviceInfo[index].iMajorVersion;
  157. if (minor)
  158. *minor = vDeviceInfo[index].iMinorVersion;
  159. }
  160. return;
  161. }
  162. /*
  163. * Returns the maximum number of Sources that can be generate on the given device
  164. */
  165. U32 ALDeviceList::GetMaxNumSources(S32 index)
  166. {
  167. if (index < GetNumDevices())
  168. return vDeviceInfo[index].uiSourceCount;
  169. else
  170. return 0;
  171. }
  172. /*
  173. * Checks if the extension is supported on the given device
  174. */
  175. bool ALDeviceList::IsExtensionSupported(int index, SFXALCaps cap)
  176. {
  177. bool bReturn = false;
  178. if (index < GetNumDevices())
  179. bReturn = vDeviceInfo[index].iCapsFlags & cap;
  180. return bReturn;
  181. }
  182. /*
  183. * returns the index of the default device in the complete device list
  184. */
  185. int ALDeviceList::GetDefaultDevice()
  186. {
  187. return defaultDeviceIndex;
  188. }
  189. /*
  190. * Deselects devices which don't have the specified minimum version
  191. */
  192. void ALDeviceList::FilterDevicesMinVer(S32 major, S32 minor)
  193. {
  194. int dMajor, dMinor;
  195. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  196. GetDeviceVersion(i, &dMajor, &dMinor);
  197. if ((dMajor < major) || ((dMajor == major) && (dMinor < minor))) {
  198. vDeviceInfo[i].bSelected = false;
  199. }
  200. }
  201. }
  202. /*
  203. * Deselects devices which don't have the specified maximum version
  204. */
  205. void ALDeviceList::FilterDevicesMaxVer(S32 major, S32 minor)
  206. {
  207. S32 dMajor, dMinor;
  208. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  209. GetDeviceVersion(i, &dMajor, &dMinor);
  210. if ((dMajor > major) || ((dMajor == major) && (dMinor > minor))) {
  211. vDeviceInfo[i].bSelected = false;
  212. }
  213. }
  214. }
  215. /*
  216. * Deselects device which don't support the given extension name
  217. */
  218. void ALDeviceList::FilterDevicesExtension(SFXALCaps cap)
  219. {
  220. for (U32 i = 0; i < vDeviceInfo.size(); i++)
  221. vDeviceInfo[i].bSelected = vDeviceInfo[i].iCapsFlags & cap;
  222. }
  223. /*
  224. * Resets all filtering, such that all devices are in the list
  225. */
  226. void ALDeviceList::ResetFilters()
  227. {
  228. for (S32 i = 0; i < GetNumDevices(); i++) {
  229. vDeviceInfo[i].bSelected = true;
  230. }
  231. filterIndex = 0;
  232. }
  233. /*
  234. * Gets index of first filtered device
  235. */
  236. int ALDeviceList::GetFirstFilteredDevice()
  237. {
  238. int i;
  239. for (i = 0; i < GetNumDevices(); i++) {
  240. if (vDeviceInfo[i].bSelected == true) {
  241. break;
  242. }
  243. }
  244. filterIndex = i + 1;
  245. return i;
  246. }
  247. /*
  248. * Gets index of next filtered device
  249. */
  250. int ALDeviceList::GetNextFilteredDevice()
  251. {
  252. int i;
  253. for (i = filterIndex; i < GetNumDevices(); i++) {
  254. if (vDeviceInfo[i].bSelected == true) {
  255. break;
  256. }
  257. }
  258. filterIndex = i + 1;
  259. return i;
  260. }
  261. /*
  262. * Internal function to detemine max number of Sources that can be generated
  263. */
  264. unsigned int ALDeviceList::GetMaxNumSources()
  265. {
  266. ALuint uiSources[256];
  267. U32 iSourceCount = 0;
  268. // Clear AL Error Code
  269. ALFunction.alGetError();
  270. // Generate up to 256 Sources, checking for any errors
  271. for (iSourceCount = 0; iSourceCount < 256; iSourceCount++)
  272. {
  273. ALFunction.alGenSources(1, &uiSources[iSourceCount]);
  274. if (ALFunction.alGetError() != AL_NO_ERROR)
  275. break;
  276. }
  277. // Release the Sources
  278. ALFunction.alDeleteSources(iSourceCount, uiSources);
  279. if (ALFunction.alGetError() != AL_NO_ERROR)
  280. {
  281. for (U32 i = 0; i < 256; i++)
  282. {
  283. ALFunction.alDeleteSources(1, &uiSources[i]);
  284. }
  285. }
  286. return iSourceCount;
  287. }