aldlist.cpp 8.1 KB

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