aldlist.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #else
  29. #include <al/alc.h>
  30. #endif
  31. /*
  32. * Init call
  33. */
  34. ALDeviceList::ALDeviceList( const OPENALFNTABLE &oalft )
  35. {
  36. VECTOR_SET_ASSOCIATION( vDeviceInfo );
  37. ALDEVICEINFO ALDeviceInfo;
  38. char *devices;
  39. int index;
  40. const char *defaultDeviceName;
  41. const char *actualDeviceName;
  42. dMemcpy( &ALFunction, &oalft, sizeof( OPENALFNTABLE ) );
  43. // DeviceInfo vector stores, for each enumerated device, it's device name, selection status, spec version #, and extension support
  44. vDeviceInfo.clear();
  45. vDeviceInfo.reserve(10);
  46. defaultDeviceIndex = 0;
  47. // grab function pointers for 1.0-API functions, and if successful proceed to enumerate all devices
  48. if (ALFunction.alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT")) {
  49. devices = (char *)ALFunction.alcGetString(NULL, ALC_DEVICE_SPECIFIER);
  50. defaultDeviceName = (char *)ALFunction.alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
  51. index = 0;
  52. // go through device list (each device terminated with a single NULL, list terminated with double NULL)
  53. while (*devices != 0) {
  54. if (dStrcmp(defaultDeviceName, devices) == 0) {
  55. defaultDeviceIndex = index;
  56. }
  57. ALCdevice *device = ALFunction.alcOpenDevice(devices);
  58. if (device) {
  59. ALCcontext *context = ALFunction.alcCreateContext(device, NULL);
  60. if (context) {
  61. ALFunction.alcMakeContextCurrent(context);
  62. // if new actual device name isn't already in the list, then add it...
  63. actualDeviceName = ALFunction.alcGetString(device, ALC_DEVICE_SPECIFIER);
  64. bool bNewName = true;
  65. for (int i = 0; i < GetNumDevices(); i++) {
  66. if (dStrcmp(GetDeviceName(i), actualDeviceName) == 0) {
  67. bNewName = false;
  68. }
  69. }
  70. if ((bNewName) && (actualDeviceName != NULL) && (dStrlen(actualDeviceName) > 0)) {
  71. dMemset(&ALDeviceInfo, 0, sizeof(ALDEVICEINFO));
  72. ALDeviceInfo.bSelected = true;
  73. dStrncpy(ALDeviceInfo.strDeviceName, actualDeviceName, sizeof(ALDeviceInfo.strDeviceName));
  74. ALFunction.alcGetIntegerv(device, ALC_MAJOR_VERSION, sizeof(int), &ALDeviceInfo.iMajorVersion);
  75. ALFunction.alcGetIntegerv(device, ALC_MINOR_VERSION, sizeof(int), &ALDeviceInfo.iMinorVersion);
  76. ALDeviceInfo.iCapsFlags = 0;
  77. // Check for ALC Extensions
  78. if (ALFunction.alcIsExtensionPresent(device, "ALC_EXT_CAPTURE") == AL_TRUE)
  79. ALDeviceInfo.iCapsFlags |= SFXALCapture;
  80. if (ALFunction.alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_TRUE)
  81. ALDeviceInfo.iCapsFlags |= SFXALEFX;
  82. // Check for AL Extensions
  83. if (ALFunction.alIsExtensionPresent("AL_EXT_OFFSET") == AL_TRUE)
  84. ALDeviceInfo.iCapsFlags |= SFXALOffset;
  85. if (ALFunction.alIsExtensionPresent("AL_EXT_LINEAR_DISTANCE") == AL_TRUE)
  86. ALDeviceInfo.iCapsFlags |= SFXALLinearDistance;
  87. if (ALFunction.alIsExtensionPresent("AL_EXT_EXPONENT_DISTANCE") == AL_TRUE)
  88. ALDeviceInfo.iCapsFlags |= SFXALExponentDistance;
  89. if (ALFunction.alIsExtensionPresent("EAX2.0") == AL_TRUE)
  90. ALDeviceInfo.iCapsFlags |= SFXALEAX2;
  91. if (ALFunction.alIsExtensionPresent("EAX3.0") == AL_TRUE)
  92. ALDeviceInfo.iCapsFlags |= SFXALEAX3;
  93. if (ALFunction.alIsExtensionPresent("EAX4.0") == AL_TRUE)
  94. ALDeviceInfo.iCapsFlags |= SFXALEAX4;
  95. if (ALFunction.alIsExtensionPresent("EAX5.0") == AL_TRUE)
  96. ALDeviceInfo.iCapsFlags |= SFXALEAX5;
  97. if (ALFunction.alIsExtensionPresent("EAX-RAM") == AL_TRUE)
  98. ALDeviceInfo.iCapsFlags |= SFXALEAXRAM;
  99. // Get Source Count
  100. ALDeviceInfo.uiSourceCount = GetMaxNumSources();
  101. vDeviceInfo.push_back(ALDeviceInfo);
  102. }
  103. ALFunction.alcMakeContextCurrent(NULL);
  104. ALFunction.alcDestroyContext(context);
  105. }
  106. ALFunction.alcCloseDevice(device);
  107. }
  108. devices += dStrlen(devices) + 1;
  109. index += 1;
  110. }
  111. }
  112. ResetFilters();
  113. }
  114. /*
  115. * Exit call
  116. */
  117. ALDeviceList::~ALDeviceList()
  118. {
  119. }
  120. /*
  121. * Returns the number of devices in the complete device list
  122. */
  123. int ALDeviceList::GetNumDevices()
  124. {
  125. return (int)vDeviceInfo.size();
  126. }
  127. /*
  128. * Returns the device name at an index in the complete device list
  129. */
  130. const char *ALDeviceList::GetDeviceName(int index)
  131. {
  132. if (index < GetNumDevices())
  133. return vDeviceInfo[index].strDeviceName;
  134. else
  135. return NULL;
  136. }
  137. /*
  138. * Returns the major and minor version numbers for a device at a specified index in the complete list
  139. */
  140. void ALDeviceList::GetDeviceVersion(int index, int *major, int *minor)
  141. {
  142. if (index < GetNumDevices()) {
  143. if (major)
  144. *major = vDeviceInfo[index].iMajorVersion;
  145. if (minor)
  146. *minor = vDeviceInfo[index].iMinorVersion;
  147. }
  148. return;
  149. }
  150. /*
  151. * Returns the maximum number of Sources that can be generate on the given device
  152. */
  153. U32 ALDeviceList::GetMaxNumSources(S32 index)
  154. {
  155. if (index < GetNumDevices())
  156. return vDeviceInfo[index].uiSourceCount;
  157. else
  158. return 0;
  159. }
  160. /*
  161. * Checks if the extension is supported on the given device
  162. */
  163. bool ALDeviceList::IsExtensionSupported(int index, SFXALCaps cap)
  164. {
  165. bool bReturn = false;
  166. if (index < GetNumDevices())
  167. bReturn = vDeviceInfo[index].iCapsFlags & cap;
  168. return bReturn;
  169. }
  170. /*
  171. * returns the index of the default device in the complete device list
  172. */
  173. int ALDeviceList::GetDefaultDevice()
  174. {
  175. return defaultDeviceIndex;
  176. }
  177. /*
  178. * Deselects devices which don't have the specified minimum version
  179. */
  180. void ALDeviceList::FilterDevicesMinVer(S32 major, S32 minor)
  181. {
  182. int dMajor, dMinor;
  183. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  184. GetDeviceVersion(i, &dMajor, &dMinor);
  185. if ((dMajor < major) || ((dMajor == major) && (dMinor < minor))) {
  186. vDeviceInfo[i].bSelected = false;
  187. }
  188. }
  189. }
  190. /*
  191. * Deselects devices which don't have the specified maximum version
  192. */
  193. void ALDeviceList::FilterDevicesMaxVer(S32 major, S32 minor)
  194. {
  195. S32 dMajor, dMinor;
  196. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  197. GetDeviceVersion(i, &dMajor, &dMinor);
  198. if ((dMajor > major) || ((dMajor == major) && (dMinor > minor))) {
  199. vDeviceInfo[i].bSelected = false;
  200. }
  201. }
  202. }
  203. /*
  204. * Deselects device which don't support the given extension name
  205. */
  206. void ALDeviceList::FilterDevicesExtension(SFXALCaps cap)
  207. {
  208. for (U32 i = 0; i < vDeviceInfo.size(); i++)
  209. vDeviceInfo[i].bSelected = vDeviceInfo[i].iCapsFlags & cap;
  210. }
  211. /*
  212. * Resets all filtering, such that all devices are in the list
  213. */
  214. void ALDeviceList::ResetFilters()
  215. {
  216. for (S32 i = 0; i < GetNumDevices(); i++) {
  217. vDeviceInfo[i].bSelected = true;
  218. }
  219. filterIndex = 0;
  220. }
  221. /*
  222. * Gets index of first filtered device
  223. */
  224. int ALDeviceList::GetFirstFilteredDevice()
  225. {
  226. int i;
  227. for (i = 0; i < GetNumDevices(); i++) {
  228. if (vDeviceInfo[i].bSelected == true) {
  229. break;
  230. }
  231. }
  232. filterIndex = i + 1;
  233. return i;
  234. }
  235. /*
  236. * Gets index of next filtered device
  237. */
  238. int ALDeviceList::GetNextFilteredDevice()
  239. {
  240. int i;
  241. for (i = filterIndex; i < GetNumDevices(); i++) {
  242. if (vDeviceInfo[i].bSelected == true) {
  243. break;
  244. }
  245. }
  246. filterIndex = i + 1;
  247. return i;
  248. }
  249. /*
  250. * Internal function to detemine max number of Sources that can be generated
  251. */
  252. unsigned int ALDeviceList::GetMaxNumSources()
  253. {
  254. ALuint uiSources[256];
  255. U32 iSourceCount = 0;
  256. // Clear AL Error Code
  257. ALFunction.alGetError();
  258. // Generate up to 256 Sources, checking for any errors
  259. for (iSourceCount = 0; iSourceCount < 256; iSourceCount++)
  260. {
  261. ALFunction.alGenSources(1, &uiSources[iSourceCount]);
  262. if (ALFunction.alGetError() != AL_NO_ERROR)
  263. break;
  264. }
  265. // Release the Sources
  266. ALFunction.alDeleteSources(iSourceCount, uiSources);
  267. if (ALFunction.alGetError() != AL_NO_ERROR)
  268. {
  269. for (U32 i = 0; i < 256; i++)
  270. {
  271. ALFunction.alDeleteSources(1, &uiSources[i]);
  272. }
  273. }
  274. return iSourceCount;
  275. }