cd_win.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the included (GNU.txt) GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
  16. // rights reserved.
  17. #include <windows.h>
  18. #include "quakedef.h"
  19. extern HWND mainwindow;
  20. extern cvar_t bgmvolume;
  21. static qboolean cdValid = false;
  22. static qboolean playing = false;
  23. static qboolean wasPlaying = false;
  24. static qboolean initialized = false;
  25. static qboolean enabled = false;
  26. static qboolean playLooping = false;
  27. static float cdvolume;
  28. static byte remap[100];
  29. static byte cdrom;
  30. static byte playTrack;
  31. static byte maxTrack;
  32. UINT wDeviceID;
  33. static void CDAudio_Eject(void)
  34. {
  35. DWORD dwReturn;
  36. if (dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_DOOR_OPEN, (DWORD)NULL))
  37. Con_DPrintf("MCI_SET_DOOR_OPEN failed (%i)\n", dwReturn);
  38. }
  39. static void CDAudio_CloseDoor(void)
  40. {
  41. DWORD dwReturn;
  42. if (dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_DOOR_CLOSED, (DWORD)NULL))
  43. Con_DPrintf("MCI_SET_DOOR_CLOSED failed (%i)\n", dwReturn);
  44. }
  45. static int CDAudio_GetAudioDiskInfo(void)
  46. {
  47. DWORD dwReturn;
  48. MCI_STATUS_PARMS mciStatusParms;
  49. cdValid = false;
  50. mciStatusParms.dwItem = MCI_STATUS_READY;
  51. dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
  52. if (dwReturn)
  53. {
  54. Con_DPrintf("CDAudio: drive ready test - get status failed\n");
  55. return -1;
  56. }
  57. if (!mciStatusParms.dwReturn)
  58. {
  59. Con_DPrintf("CDAudio: drive not ready\n");
  60. return -1;
  61. }
  62. mciStatusParms.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
  63. dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
  64. if (dwReturn)
  65. {
  66. Con_DPrintf("CDAudio: get tracks - status failed\n");
  67. return -1;
  68. }
  69. if (mciStatusParms.dwReturn < 1)
  70. {
  71. Con_DPrintf("CDAudio: no music tracks\n");
  72. return -1;
  73. }
  74. cdValid = true;
  75. maxTrack = mciStatusParms.dwReturn;
  76. return 0;
  77. }
  78. void CDAudio_Play(byte track, qboolean looping)
  79. {
  80. DWORD dwReturn;
  81. MCI_PLAY_PARMS mciPlayParms;
  82. MCI_STATUS_PARMS mciStatusParms;
  83. if (!enabled)
  84. return;
  85. if (!cdValid)
  86. {
  87. CDAudio_GetAudioDiskInfo();
  88. if (!cdValid)
  89. return;
  90. }
  91. track = remap[track];
  92. if (track < 1 || track > maxTrack)
  93. {
  94. Con_DPrintf("CDAudio: Bad track number %u.\n", track);
  95. return;
  96. }
  97. // don't try to play a non-audio track
  98. mciStatusParms.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
  99. mciStatusParms.dwTrack = track;
  100. dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
  101. if (dwReturn)
  102. {
  103. Con_DPrintf("MCI_STATUS failed (%i)\n", dwReturn);
  104. return;
  105. }
  106. if (mciStatusParms.dwReturn != MCI_CDA_TRACK_AUDIO)
  107. {
  108. Con_Printf("CDAudio: track %i is not audio\n", track);
  109. return;
  110. }
  111. // get the length of the track to be played
  112. mciStatusParms.dwItem = MCI_STATUS_LENGTH;
  113. mciStatusParms.dwTrack = track;
  114. dwReturn = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT, (DWORD) (LPVOID) &mciStatusParms);
  115. if (dwReturn)
  116. {
  117. Con_DPrintf("MCI_STATUS failed (%i)\n", dwReturn);
  118. return;
  119. }
  120. if (playing)
  121. {
  122. if (playTrack == track)
  123. return;
  124. CDAudio_Stop();
  125. }
  126. mciPlayParms.dwFrom = MCI_MAKE_TMSF(track, 0, 0, 0);
  127. mciPlayParms.dwTo = (mciStatusParms.dwReturn << 8) | track;
  128. mciPlayParms.dwCallback = (DWORD)mainwindow;
  129. dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY | MCI_FROM | MCI_TO, (DWORD)(LPVOID) &mciPlayParms);
  130. if (dwReturn)
  131. {
  132. Con_DPrintf("CDAudio: MCI_PLAY failed (%i)\n", dwReturn);
  133. return;
  134. }
  135. playLooping = looping;
  136. playTrack = track;
  137. playing = true;
  138. if (cdvolume == 0.0)
  139. CDAudio_Pause ();
  140. }
  141. void CDAudio_Stop(void)
  142. {
  143. DWORD dwReturn;
  144. if (!enabled)
  145. return;
  146. if (!playing)
  147. return;
  148. if (dwReturn = mciSendCommand(wDeviceID, MCI_STOP, 0, (DWORD)NULL))
  149. Con_DPrintf("MCI_STOP failed (%i)", dwReturn);
  150. wasPlaying = false;
  151. playing = false;
  152. }
  153. void CDAudio_Pause(void)
  154. {
  155. DWORD dwReturn;
  156. MCI_GENERIC_PARMS mciGenericParms;
  157. if (!enabled)
  158. return;
  159. if (!playing)
  160. return;
  161. mciGenericParms.dwCallback = (DWORD)mainwindow;
  162. if (dwReturn = mciSendCommand(wDeviceID, MCI_PAUSE, 0, (DWORD)(LPVOID) &mciGenericParms))
  163. Con_DPrintf("MCI_PAUSE failed (%i)", dwReturn);
  164. wasPlaying = playing;
  165. playing = false;
  166. }
  167. void CDAudio_Resume(void)
  168. {
  169. DWORD dwReturn;
  170. MCI_PLAY_PARMS mciPlayParms;
  171. if (!enabled)
  172. return;
  173. if (!cdValid)
  174. return;
  175. if (!wasPlaying)
  176. return;
  177. mciPlayParms.dwFrom = MCI_MAKE_TMSF(playTrack, 0, 0, 0);
  178. mciPlayParms.dwTo = MCI_MAKE_TMSF(playTrack + 1, 0, 0, 0);
  179. mciPlayParms.dwCallback = (DWORD)mainwindow;
  180. dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_TO | MCI_NOTIFY, (DWORD)(LPVOID) &mciPlayParms);
  181. if (dwReturn)
  182. {
  183. Con_DPrintf("CDAudio: MCI_PLAY failed (%i)\n", dwReturn);
  184. return;
  185. }
  186. playing = true;
  187. }
  188. static void CD_f (void)
  189. {
  190. char *command;
  191. int ret;
  192. int n;
  193. int startAddress;
  194. if (Cmd_Argc() < 2)
  195. return;
  196. command = Cmd_Argv (1);
  197. if (Q_strcasecmp(command, "on") == 0)
  198. {
  199. enabled = true;
  200. return;
  201. }
  202. if (Q_strcasecmp(command, "off") == 0)
  203. {
  204. if (playing)
  205. CDAudio_Stop();
  206. enabled = false;
  207. return;
  208. }
  209. if (Q_strcasecmp(command, "reset") == 0)
  210. {
  211. enabled = true;
  212. if (playing)
  213. CDAudio_Stop();
  214. for (n = 0; n < 100; n++)
  215. remap[n] = n;
  216. CDAudio_GetAudioDiskInfo();
  217. return;
  218. }
  219. if (Q_strcasecmp(command, "remap") == 0)
  220. {
  221. ret = Cmd_Argc() - 2;
  222. if (ret <= 0)
  223. {
  224. for (n = 1; n < 100; n++)
  225. if (remap[n] != n)
  226. Con_Printf(" %u -> %u\n", n, remap[n]);
  227. return;
  228. }
  229. for (n = 1; n <= ret; n++)
  230. remap[n] = Q_atoi(Cmd_Argv (n+1));
  231. return;
  232. }
  233. if (Q_strcasecmp(command, "close") == 0)
  234. {
  235. CDAudio_CloseDoor();
  236. return;
  237. }
  238. if (!cdValid)
  239. {
  240. CDAudio_GetAudioDiskInfo();
  241. if (!cdValid)
  242. {
  243. Con_Printf("No CD in player.\n");
  244. return;
  245. }
  246. }
  247. if (Q_strcasecmp(command, "play") == 0)
  248. {
  249. CDAudio_Play((byte)Q_atoi(Cmd_Argv (2)), false);
  250. return;
  251. }
  252. if (Q_strcasecmp(command, "loop") == 0)
  253. {
  254. CDAudio_Play((byte)Q_atoi(Cmd_Argv (2)), true);
  255. return;
  256. }
  257. if (Q_strcasecmp(command, "stop") == 0)
  258. {
  259. CDAudio_Stop();
  260. return;
  261. }
  262. if (Q_strcasecmp(command, "pause") == 0)
  263. {
  264. CDAudio_Pause();
  265. return;
  266. }
  267. if (Q_strcasecmp(command, "resume") == 0)
  268. {
  269. CDAudio_Resume();
  270. return;
  271. }
  272. if (Q_strcasecmp(command, "eject") == 0)
  273. {
  274. if (playing)
  275. CDAudio_Stop();
  276. CDAudio_Eject();
  277. cdValid = false;
  278. return;
  279. }
  280. if (Q_strcasecmp(command, "info") == 0)
  281. {
  282. Con_Printf("%u tracks\n", maxTrack);
  283. if (playing)
  284. Con_Printf("Currently %s track %u\n", playLooping ? "looping" : "playing", playTrack);
  285. else if (wasPlaying)
  286. Con_Printf("Paused %s track %u\n", playLooping ? "looping" : "playing", playTrack);
  287. Con_Printf("Volume is %f\n", cdvolume);
  288. return;
  289. }
  290. }
  291. LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  292. {
  293. if (lParam != wDeviceID)
  294. return 1;
  295. switch (wParam)
  296. {
  297. case MCI_NOTIFY_SUCCESSFUL:
  298. if (playing)
  299. {
  300. playing = false;
  301. if (playLooping)
  302. CDAudio_Play(playTrack, true);
  303. }
  304. break;
  305. case MCI_NOTIFY_ABORTED:
  306. case MCI_NOTIFY_SUPERSEDED:
  307. break;
  308. case MCI_NOTIFY_FAILURE:
  309. Con_DPrintf("MCI_NOTIFY_FAILURE\n");
  310. CDAudio_Stop ();
  311. cdValid = false;
  312. break;
  313. default:
  314. Con_DPrintf("Unexpected MM_MCINOTIFY type (%i)\n", wParam);
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. void CDAudio_Update(void)
  320. {
  321. if (!enabled)
  322. return;
  323. if (bgmvolume.value != cdvolume)
  324. {
  325. if (cdvolume)
  326. {
  327. Cvar_SetValue ("bgmvolume", 0.0);
  328. cdvolume = bgmvolume.value;
  329. CDAudio_Pause ();
  330. }
  331. else
  332. {
  333. Cvar_SetValue ("bgmvolume", 1.0);
  334. cdvolume = bgmvolume.value;
  335. CDAudio_Resume ();
  336. }
  337. }
  338. }
  339. int CDAudio_Init(void)
  340. {
  341. DWORD dwReturn;
  342. MCI_OPEN_PARMS mciOpenParms;
  343. MCI_SET_PARMS mciSetParms;
  344. int n;
  345. #if 0 // QW
  346. if (cls.state == ca_dedicated)
  347. return -1;
  348. #endif
  349. if (COM_CheckParm("-nocdaudio"))
  350. return -1;
  351. mciOpenParms.lpstrDeviceType = "cdaudio";
  352. if (dwReturn = mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_SHAREABLE, (DWORD) (LPVOID) &mciOpenParms))
  353. {
  354. Con_Printf("CDAudio_Init: MCI_OPEN failed (%i)\n", dwReturn);
  355. return -1;
  356. }
  357. wDeviceID = mciOpenParms.wDeviceID;
  358. // Set the time format to track/minute/second/frame (TMSF).
  359. mciSetParms.dwTimeFormat = MCI_FORMAT_TMSF;
  360. if (dwReturn = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD)(LPVOID) &mciSetParms))
  361. {
  362. Con_Printf("MCI_SET_TIME_FORMAT failed (%i)\n", dwReturn);
  363. mciSendCommand(wDeviceID, MCI_CLOSE, 0, (DWORD)NULL);
  364. return -1;
  365. }
  366. for (n = 0; n < 100; n++)
  367. remap[n] = n;
  368. initialized = true;
  369. enabled = true;
  370. if (CDAudio_GetAudioDiskInfo())
  371. {
  372. Con_Printf("CDAudio_Init: No CD in player.\n");
  373. cdValid = false;
  374. enabled = false;
  375. }
  376. Cmd_AddCommand ("cd", CD_f);
  377. // Con_Printf("CD Audio Initialized\n");
  378. return 0;
  379. }
  380. void CDAudio_Shutdown(void)
  381. {
  382. if (!initialized)
  383. return;
  384. CDAudio_Stop();
  385. if (mciSendCommand(wDeviceID, MCI_CLOSE, MCI_WAIT, (DWORD)NULL))
  386. Con_DPrintf("CDAudio_Shutdown: MCI_CLOSE failed\n");
  387. }