winRedbook.cpp 13 KB

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