winRedbook.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platformWin32/platformWin32.h"
  23. #include "platform/platformRedBook.h"
  24. #include "core/strings/unicode.h"
  25. #include "core/strings/stringFunctions.h"
  26. class Win32RedBookDevice : public RedBookDevice
  27. {
  28. private:
  29. typedef RedBookDevice Parent;
  30. U32 mDeviceId;
  31. void setLastError(const char *);
  32. void setLastError(U32);
  33. MIXERCONTROLDETAILS mMixerVolumeDetails;
  34. MIXERCONTROLDETAILS_UNSIGNED mMixerVolumeValue;
  35. union {
  36. HMIXEROBJ mVolumeDeviceId;
  37. UINT mAuxVolumeDeviceId;
  38. };
  39. U32 mOriginalVolume;
  40. bool mVolumeInitialized;
  41. bool mUsingMixer;
  42. void openVolume();
  43. void closeVolume();
  44. public:
  45. Win32RedBookDevice();
  46. ~Win32RedBookDevice();
  47. U32 getDeviceId();
  48. bool open();
  49. bool close();
  50. bool play(U32);
  51. bool stop();
  52. bool getTrackCount(U32 *);
  53. bool getVolume(F32 *);
  54. bool setVolume(F32);
  55. };
  56. //------------------------------------------------------------------------------
  57. // Win32 specific
  58. //------------------------------------------------------------------------------
  59. void installRedBookDevices()
  60. {
  61. U32 bufSize = ::GetLogicalDriveStrings(0,0);
  62. char * buf = new char[bufSize];
  63. ::GetLogicalDriveStringsA(bufSize, buf);
  64. char * str = buf;
  65. while(*str)
  66. {
  67. if(::GetDriveTypeA(str) == DRIVE_CDROM)
  68. {
  69. Win32RedBookDevice * device = new Win32RedBookDevice;
  70. device->mDeviceName = new char[dStrlen(str) + 1];
  71. dStrcpy(device->mDeviceName, str);
  72. RedBook::installDevice(device);
  73. }
  74. str += dStrlen(str) + 1;
  75. }
  76. delete [] buf;
  77. }
  78. void handleRedBookCallback(U32 code, U32 deviceId)
  79. {
  80. if(code != MCI_NOTIFY_SUCCESSFUL)
  81. return;
  82. Win32RedBookDevice * device = dynamic_cast<Win32RedBookDevice*>(RedBook::getCurrentDevice());
  83. if(!device)
  84. return;
  85. if(device->getDeviceId() != deviceId)
  86. return;
  87. // only installed callback on play (no callback if play is aborted)
  88. RedBook::handleCallback(RedBook::PlayFinished);
  89. }
  90. //------------------------------------------------------------------------------
  91. // Class: Win32RedBookDevice
  92. //------------------------------------------------------------------------------
  93. Win32RedBookDevice::Win32RedBookDevice()
  94. {
  95. mVolumeInitialized = false;
  96. }
  97. Win32RedBookDevice::~Win32RedBookDevice()
  98. {
  99. close();
  100. }
  101. U32 Win32RedBookDevice::getDeviceId()
  102. {
  103. return(mDeviceId);
  104. }
  105. bool Win32RedBookDevice::open()
  106. {
  107. if(mAcquired)
  108. {
  109. setLastError("Device is already open.");
  110. return(false);
  111. }
  112. U32 error;
  113. // open the device
  114. MCI_OPEN_PARMS openParms;
  115. #ifdef UNICODE
  116. openParms.lpstrDeviceType = (LPCWSTR)MCI_DEVTYPE_CD_AUDIO;
  117. UTF16 buf[512];
  118. convertUTF8toUTF16((UTF8 *)mDeviceName, buf);
  119. openParms.lpstrElementName = buf;
  120. #else
  121. openParms.lpstrDeviceType = (LPCSTR)MCI_DEVTYPE_CD_AUDIO;
  122. openParms.lpstrElementName = mDeviceName;
  123. #endif
  124. error = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID, (DWORD_PTR)(LPMCI_OPEN_PARMS)&openParms);
  125. if(error)
  126. {
  127. // attempt to open as a shared device
  128. error = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID|MCI_OPEN_SHAREABLE, (DWORD_PTR)(LPMCI_OPEN_PARMS)&openParms);
  129. if(error)
  130. {
  131. setLastError(error);
  132. return(false);
  133. }
  134. }
  135. // set time mode to milliseconds
  136. MCI_SET_PARMS setParms;
  137. setParms.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
  138. error = mciSendCommand(openParms.wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD_PTR)(LPMCI_SET_PARMS)&setParms);
  139. if(error)
  140. {
  141. setLastError(error);
  142. return(false);
  143. }
  144. //
  145. mDeviceId = openParms.wDeviceID;
  146. mAcquired = true;
  147. openVolume();
  148. setLastError("");
  149. return(true);
  150. }
  151. bool Win32RedBookDevice::close()
  152. {
  153. if(!mAcquired)
  154. {
  155. setLastError("Device has not been acquired");
  156. return(false);
  157. }
  158. stop();
  159. U32 error;
  160. MCI_GENERIC_PARMS closeParms;
  161. error = mciSendCommand(mDeviceId, MCI_CLOSE, 0, (DWORD_PTR)(LPMCI_GENERIC_PARMS)&closeParms);
  162. if(error)
  163. {
  164. setLastError(error);
  165. return(false);
  166. }
  167. mAcquired = false;
  168. closeVolume();
  169. setLastError("");
  170. return(true);
  171. }
  172. bool Win32RedBookDevice::play(U32 track)
  173. {
  174. if(!mAcquired)
  175. {
  176. setLastError("Device has not been acquired");
  177. return(false);
  178. }
  179. U32 numTracks;
  180. if(!getTrackCount(&numTracks))
  181. return(false);
  182. if(track >= numTracks)
  183. {
  184. setLastError("Track index is out of range");
  185. return(false);
  186. }
  187. MCI_STATUS_PARMS statusParms;
  188. // get track start time
  189. statusParms.dwItem = MCI_STATUS_POSITION;
  190. statusParms.dwTrack = track + 1;
  191. U32 error;
  192. error = mciSendCommand(mDeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK|MCI_WAIT,
  193. (DWORD_PTR)(LPMCI_STATUS_PARMS)&statusParms);
  194. if(error)
  195. {
  196. setLastError(error);
  197. return(false);
  198. }
  199. MCI_PLAY_PARMS playParms;
  200. playParms.dwFrom = statusParms.dwReturn;
  201. // get track end time
  202. statusParms.dwItem = MCI_STATUS_LENGTH;
  203. error = mciSendCommand(mDeviceId, MCI_STATUS, MCI_STATUS_ITEM|MCI_TRACK|MCI_WAIT,
  204. (DWORD_PTR)(LPMCI_STATUS_PARMS)&statusParms);
  205. if(error)
  206. {
  207. setLastError(error);
  208. return(false);
  209. }
  210. playParms.dwTo = playParms.dwFrom + statusParms.dwReturn;
  211. // play the track
  212. playParms.dwCallback = MAKELONG(getWin32WindowHandle(), 0);
  213. error = mciSendCommand(mDeviceId, MCI_PLAY, MCI_FROM|MCI_TO|MCI_NOTIFY,
  214. (DWORD_PTR)(LPMCI_PLAY_PARMS)&playParms);
  215. if(error)
  216. {
  217. setLastError(error);
  218. return(false);
  219. }
  220. setLastError("");
  221. return(true);
  222. }
  223. bool Win32RedBookDevice::stop()
  224. {
  225. if(!mAcquired)
  226. {
  227. setLastError("Device has not been acquired");
  228. return(false);
  229. }
  230. MCI_GENERIC_PARMS genParms;
  231. U32 error = mciSendCommand(mDeviceId, MCI_STOP, 0, (DWORD_PTR)(LPMCI_GENERIC_PARMS)&genParms);
  232. if(error)
  233. {
  234. setLastError(error);
  235. return(false);
  236. }
  237. setLastError("");
  238. return(true);
  239. }
  240. bool Win32RedBookDevice::getTrackCount(U32 * numTracks)
  241. {
  242. if(!mAcquired)
  243. {
  244. setLastError("Device has not been acquired");
  245. return(false);
  246. }
  247. MCI_STATUS_PARMS statusParms;
  248. statusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
  249. U32 error = mciSendCommand(mDeviceId, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD_PTR)(LPMCI_STATUS_PARMS)&statusParms);
  250. if(error)
  251. {
  252. setLastError(error);
  253. return(false);
  254. }
  255. *numTracks = statusParms.dwReturn;
  256. return(true);
  257. }
  258. bool Win32RedBookDevice::getVolume(F32 * volume)
  259. {
  260. if(!mAcquired)
  261. {
  262. setLastError("Device has not been acquired");
  263. return(false);
  264. }
  265. if(!mVolumeInitialized)
  266. {
  267. setLastError("Volume failed to initialize");
  268. return(false);
  269. }
  270. U32 vol = 0;
  271. if(mUsingMixer)
  272. {
  273. mixerGetControlDetails(mVolumeDeviceId, &mMixerVolumeDetails, MIXER_GETCONTROLDETAILSF_VALUE);
  274. vol = mMixerVolumeValue.dwValue;
  275. }
  276. else
  277. auxGetVolume(mAuxVolumeDeviceId, (unsigned long *)&vol);
  278. vol &= 0xffff;
  279. *volume = F32(vol) / 65535.f;
  280. setLastError("");
  281. return(true);
  282. }
  283. bool Win32RedBookDevice::setVolume(F32 volume)
  284. {
  285. if(!mAcquired)
  286. {
  287. setLastError("Device has not been acquired");
  288. return(false);
  289. }
  290. if(!mVolumeInitialized)
  291. {
  292. setLastError("Volume failed to initialize");
  293. return(false);
  294. }
  295. // move into a U32 - left/right U16 volumes
  296. U32 vol = U32(volume * 65536.f);
  297. if(vol > 0xffff)
  298. vol = 0xffff;
  299. if(mUsingMixer)
  300. {
  301. mMixerVolumeValue.dwValue = vol;
  302. mixerSetControlDetails(mVolumeDeviceId, &mMixerVolumeDetails, MIXER_SETCONTROLDETAILSF_VALUE);
  303. }
  304. else
  305. {
  306. vol |= vol << 16;
  307. auxSetVolume(mAuxVolumeDeviceId, vol);
  308. }
  309. setLastError("");
  310. return(true);
  311. }
  312. //------------------------------------------------------------------------------
  313. void Win32RedBookDevice::openVolume()
  314. {
  315. setLastError("");
  316. // first attempt to get the volume control through the mixer API
  317. S32 i;
  318. for(i = mixerGetNumDevs() - 1; i >= 0; i--)
  319. {
  320. // open the mixer
  321. if(mixerOpen((HMIXER*)&mVolumeDeviceId, i, 0, 0, 0) == MMSYSERR_NOERROR)
  322. {
  323. MIXERLINE lineInfo;
  324. memset(&lineInfo, 0, sizeof(lineInfo));
  325. lineInfo.cbStruct = sizeof(lineInfo);
  326. lineInfo.dwComponentType = MIXERLINE_COMPONENTTYPE_SRC_COMPACTDISC;
  327. // get the cdaudio line
  328. if(mixerGetLineInfo(mVolumeDeviceId, &lineInfo, MIXER_GETLINEINFOF_COMPONENTTYPE) == MMSYSERR_NOERROR)
  329. {
  330. MIXERLINECONTROLS lineControls;
  331. MIXERCONTROL volumeControl;
  332. memset(&lineControls, 0, sizeof(lineControls));
  333. lineControls.cbStruct = sizeof(lineControls);
  334. lineControls.dwLineID = lineInfo.dwLineID;
  335. lineControls.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
  336. lineControls.cControls = 1;
  337. lineControls.cbmxctrl = sizeof(volumeControl);
  338. lineControls.pamxctrl = &volumeControl;
  339. memset(&volumeControl, 0, sizeof(volumeControl));
  340. volumeControl.cbStruct = sizeof(volumeControl);
  341. // get the volume control
  342. if(mixerGetLineControls(mVolumeDeviceId, &lineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE) == MMSYSERR_NOERROR)
  343. {
  344. memset(&mMixerVolumeDetails, 0, sizeof(mMixerVolumeDetails));
  345. mMixerVolumeDetails.cbStruct = sizeof(mMixerVolumeDetails);
  346. mMixerVolumeDetails.dwControlID = volumeControl.dwControlID;
  347. mMixerVolumeDetails.cChannels = 1;
  348. mMixerVolumeDetails.cbDetails = sizeof(mMixerVolumeValue);
  349. mMixerVolumeDetails.paDetails = &mMixerVolumeValue;
  350. memset(&mMixerVolumeValue, 0, sizeof(mMixerVolumeValue));
  351. // query the current value
  352. if(mixerGetControlDetails(mVolumeDeviceId, &mMixerVolumeDetails, MIXER_GETCONTROLDETAILSF_VALUE) == MMSYSERR_NOERROR)
  353. {
  354. mUsingMixer = true;
  355. mVolumeInitialized = true;
  356. mOriginalVolume = mMixerVolumeValue.dwValue;
  357. return;
  358. }
  359. }
  360. }
  361. }
  362. mixerClose((HMIXER)mVolumeDeviceId);
  363. }
  364. // try aux
  365. for(i = auxGetNumDevs() - 1; i >= 0; i--)
  366. {
  367. AUXCAPS caps;
  368. auxGetDevCaps(i, &caps, sizeof(AUXCAPS));
  369. if((caps.wTechnology == AUXCAPS_CDAUDIO) && (caps.dwSupport & AUXCAPS_VOLUME))
  370. {
  371. mAuxVolumeDeviceId = i;
  372. mVolumeInitialized = true;
  373. mUsingMixer = false;
  374. auxGetVolume(i, (unsigned long *)&mOriginalVolume);
  375. return;
  376. }
  377. }
  378. setLastError("Volume failed to initialize");
  379. }
  380. void Win32RedBookDevice::closeVolume()
  381. {
  382. setLastError("");
  383. if(!mVolumeInitialized)
  384. return;
  385. if(mUsingMixer)
  386. {
  387. mMixerVolumeValue.dwValue = mOriginalVolume;
  388. mixerSetControlDetails(mVolumeDeviceId, &mMixerVolumeDetails, MIXER_SETCONTROLDETAILSF_VALUE);
  389. mixerClose((HMIXER)mVolumeDeviceId);
  390. }
  391. else
  392. auxSetVolume(mAuxVolumeDeviceId, mOriginalVolume);
  393. mVolumeInitialized = false;
  394. }
  395. //------------------------------------------------------------------------------
  396. void Win32RedBookDevice::setLastError(const char * error)
  397. {
  398. RedBook::setLastError(error);
  399. }
  400. void Win32RedBookDevice::setLastError(U32 errorId)
  401. {
  402. char buffer[256];
  403. if(!mciGetErrorStringA(errorId, buffer, sizeof(buffer) - 1))
  404. setLastError("Failed to get MCI error string!");
  405. else
  406. setLastError(buffer);
  407. }