aldlist.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 deviceExternal[256];
  76. dStrcpy(deviceExternal, devices, 256);
  77. char* openFind = dStrchr(deviceExternal, '(');
  78. if (openFind)
  79. {
  80. char* deviceName = openFind + 1;
  81. char* closeFind = dStrchr(deviceName, ')');
  82. if (closeFind)
  83. (*closeFind) = '\0';
  84. dStrncpy(ALDeviceInfo.strDeviceName, deviceName, sizeof(ALDeviceInfo.strDeviceName));
  85. }
  86. else
  87. {
  88. dStrncpy(ALDeviceInfo.strDeviceName, devices, sizeof(ALDeviceInfo.strDeviceName));
  89. }
  90. vDeviceInfo.push_back(ALDeviceInfo);
  91. }
  92. devices += dStrlen(devices) + 1;
  93. index += 1;
  94. }
  95. ResetFilters();
  96. }
  97. /*
  98. * Exit call
  99. */
  100. ALDeviceList::~ALDeviceList()
  101. {
  102. }
  103. /*
  104. * Returns the number of devices in the complete device list
  105. */
  106. int ALDeviceList::GetNumDevices()
  107. {
  108. return (int)vDeviceInfo.size();
  109. }
  110. /*
  111. * Returns the device name at an index in the complete device list
  112. */
  113. const char *ALDeviceList::GetInternalDeviceName(int index)
  114. {
  115. if (index < GetNumDevices())
  116. return vDeviceInfo[index].strInternalDeviceName;
  117. else
  118. return NULL;
  119. }
  120. const char* ALDeviceList::GetDeviceName(int index)
  121. {
  122. if (index < GetNumDevices())
  123. return vDeviceInfo[index].strDeviceName;
  124. else
  125. return NULL;
  126. }
  127. /*
  128. * Returns the major and minor version numbers for a device at a specified index in the complete list
  129. */
  130. void ALDeviceList::GetDeviceVersion(int index, int *major, int *minor)
  131. {
  132. if (index < GetNumDevices()) {
  133. if (major)
  134. *major = vDeviceInfo[index].iMajorVersion;
  135. if (minor)
  136. *minor = vDeviceInfo[index].iMinorVersion;
  137. }
  138. return;
  139. }
  140. /*
  141. * Returns the maximum number of Sources that can be generate on the given device
  142. */
  143. U32 ALDeviceList::GetMaxNumSources(S32 index)
  144. {
  145. if (index < GetNumDevices())
  146. return vDeviceInfo[index].uiSourceCount;
  147. else
  148. return 0;
  149. }
  150. /*
  151. * Checks if the extension is supported on the given device
  152. */
  153. bool ALDeviceList::IsExtensionSupported(int index, SFXALCaps cap)
  154. {
  155. bool bReturn = false;
  156. if (index < GetNumDevices())
  157. bReturn = vDeviceInfo[index].iCapsFlags & cap;
  158. return bReturn;
  159. }
  160. /*
  161. * returns the index of the default device in the complete device list
  162. */
  163. int ALDeviceList::GetDefaultDevice()
  164. {
  165. return defaultDeviceIndex;
  166. }
  167. /*
  168. * Deselects devices which don't have the specified minimum version
  169. */
  170. void ALDeviceList::FilterDevicesMinVer(S32 major, S32 minor)
  171. {
  172. int dMajor, dMinor;
  173. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  174. GetDeviceVersion(i, &dMajor, &dMinor);
  175. if ((dMajor < major) || ((dMajor == major) && (dMinor < minor))) {
  176. vDeviceInfo[i].bSelected = false;
  177. }
  178. }
  179. }
  180. /*
  181. * Deselects devices which don't have the specified maximum version
  182. */
  183. void ALDeviceList::FilterDevicesMaxVer(S32 major, S32 minor)
  184. {
  185. S32 dMajor, dMinor;
  186. for (U32 i = 0; i < vDeviceInfo.size(); i++) {
  187. GetDeviceVersion(i, &dMajor, &dMinor);
  188. if ((dMajor > major) || ((dMajor == major) && (dMinor > minor))) {
  189. vDeviceInfo[i].bSelected = false;
  190. }
  191. }
  192. }
  193. /*
  194. * Deselects device which don't support the given extension name
  195. */
  196. void ALDeviceList::FilterDevicesExtension(SFXALCaps cap)
  197. {
  198. for (U32 i = 0; i < vDeviceInfo.size(); i++)
  199. vDeviceInfo[i].bSelected = vDeviceInfo[i].iCapsFlags & cap;
  200. }
  201. /*
  202. * Resets all filtering, such that all devices are in the list
  203. */
  204. void ALDeviceList::ResetFilters()
  205. {
  206. for (S32 i = 0; i < GetNumDevices(); i++) {
  207. vDeviceInfo[i].bSelected = true;
  208. }
  209. filterIndex = 0;
  210. }
  211. /*
  212. * Gets index of first filtered device
  213. */
  214. int ALDeviceList::GetFirstFilteredDevice()
  215. {
  216. int i;
  217. for (i = 0; i < GetNumDevices(); i++) {
  218. if (vDeviceInfo[i].bSelected == true) {
  219. break;
  220. }
  221. }
  222. filterIndex = i + 1;
  223. return i;
  224. }
  225. /*
  226. * Gets index of next filtered device
  227. */
  228. int ALDeviceList::GetNextFilteredDevice()
  229. {
  230. int i;
  231. for (i = filterIndex; i < GetNumDevices(); i++) {
  232. if (vDeviceInfo[i].bSelected == true) {
  233. break;
  234. }
  235. }
  236. filterIndex = i + 1;
  237. return i;
  238. }
  239. /*
  240. * Internal function to detemine max number of Sources that can be generated
  241. */
  242. unsigned int ALDeviceList::GetMaxNumSources()
  243. {
  244. ALuint uiSources[256];
  245. U32 iSourceCount = 0;
  246. // Clear AL Error Code
  247. ALFunction.alGetError();
  248. // Generate up to 256 Sources, checking for any errors
  249. for (iSourceCount = 0; iSourceCount < 256; iSourceCount++)
  250. {
  251. ALFunction.alGenSources(1, &uiSources[iSourceCount]);
  252. if (ALFunction.alGetError() != AL_NO_ERROR)
  253. break;
  254. }
  255. // Release the Sources
  256. ALFunction.alDeleteSources(iSourceCount, uiSources);
  257. if (ALFunction.alGetError() != AL_NO_ERROR)
  258. {
  259. for (U32 i = 0; i < 256; i++)
  260. {
  261. ALFunction.alDeleteSources(1, &uiSources[i]);
  262. }
  263. }
  264. return iSourceCount;
  265. }