1
0

DevEnum.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "DevEnum.h"
  2. #include "ds2.h"
  3. #include "strsafe.h"
  4. static int device_count=0;
  5. static const GUID NULL_GUID;
  6. BOOL WINAPI DsDevEnum::DSEnumCallback(LPGUID guid,LPCTSTR desc,LPCTSTR,DS_DEV *** var)
  7. {
  8. DS_DEV * dev=new DS_DEV;
  9. size_t length=lstrlen(desc);
  10. length+=4; // "##: "
  11. length+=1; // null terminator
  12. dev->name = (TCHAR *)calloc(sizeof(TCHAR),length);
  13. StringCchPrintf(dev->name,length, TEXT("%02d: %s"), device_count++, desc);
  14. dev->guid=guid ? *guid : NULL_GUID;
  15. **var = dev;
  16. *var=&dev->next;
  17. return 1;
  18. }
  19. DsDevEnum::DsDevEnum()
  20. {
  21. DS2::InitDLL();
  22. ds_devs=0;
  23. DS_DEV ** dev_ptr=&ds_devs;
  24. device_count = 1;
  25. if (DS2::pDirectSoundEnumerate)
  26. DS2::pDirectSoundEnumerate((LPDSENUMCALLBACK)DSEnumCallback,&dev_ptr);
  27. *dev_ptr=0;
  28. pDev=ds_devs;
  29. }
  30. DsDevEnum::~DsDevEnum()
  31. {
  32. DS_DEV* dev=ds_devs;
  33. while(dev)
  34. {
  35. DS_DEV * next=dev->next;
  36. free(dev->name);
  37. delete dev;
  38. dev=next;
  39. }
  40. // ds_devs=0;
  41. }
  42. bool DsDevEnum::FindGuid(const GUID & guid)
  43. {
  44. pDev=ds_devs;
  45. while(pDev)
  46. {
  47. if (pDev->guid== guid) return true;
  48. pDev=pDev->next;
  49. }
  50. return false;
  51. }
  52. bool DsDevEnum::FindDefault()
  53. {
  54. return FindGuid(NULL_GUID);
  55. }
  56. bool DsDevEnum::FindName(const TCHAR *deviceToFind)
  57. {
  58. pDev=ds_devs;
  59. if (!deviceToFind) return true;
  60. while(pDev)
  61. {
  62. if (!lstrcmpi(pDev->name,deviceToFind)) return true;
  63. pDev=pDev->next;
  64. }
  65. return false;
  66. }
  67. /*
  68. const GUID DsDevEnum::GuidFromName(const char* name)
  69. {
  70. return FindName(name) ? GetGuid() : NULL_GUID;
  71. }
  72. const char * DsDevEnum::NameFromGuid(const GUID & g, char * def)
  73. {
  74. return FindGuid(g) ? GetName() : def;
  75. }
  76. */
  77. const GUID DsDevEnum::GetGuid()
  78. {
  79. return pDev ? pDev->guid : NULL_GUID;
  80. }
  81. static bool _getcaps(IDirectSound * pDS,LPDSCAPS pCaps,DWORD * speakercfg)
  82. {
  83. bool rv = true;
  84. if (pCaps)
  85. {
  86. memset(pCaps,0,sizeof(*pCaps));
  87. pCaps->dwSize=sizeof(*pCaps);
  88. if (FAILED(pDS->GetCaps(pCaps))) rv = false;
  89. }
  90. if (speakercfg)
  91. {
  92. if (FAILED(pDS->GetSpeakerConfig(speakercfg))) rv = false;
  93. }
  94. return rv;
  95. }
  96. bool DsDevEnum::GetCapsFromGuid(const GUID *dev,LPDSCAPS pCaps,DWORD * speakercfg)
  97. {
  98. bool rv=false;
  99. if (!dev) dev=&NULL_GUID;
  100. if (DS2::TryGetDevCaps(dev,pCaps,speakercfg)) rv=true;
  101. else
  102. {
  103. IDirectSound * l_pDS=0;
  104. if (SUCCEEDED(DS2::myDirectSoundCreate(dev,&l_pDS)) && l_pDS)
  105. {
  106. rv=_getcaps(l_pDS,pCaps,speakercfg);
  107. l_pDS->Release();
  108. }
  109. }
  110. return rv;
  111. }