2
0

aldlist.cpp 7.8 KB

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