cd_linux.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <stdio.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/file.h>
  22. #include <sys/types.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <errno.h>
  27. #include <linux/cdrom.h>
  28. #include "quakedef.h"
  29. static qboolean cdValid = false;
  30. static qboolean playing = false;
  31. static qboolean wasPlaying = false;
  32. static qboolean initialized = false;
  33. static qboolean enabled = true;
  34. static qboolean playLooping = false;
  35. static float cdvolume;
  36. static byte remap[100];
  37. static byte playTrack;
  38. static byte maxTrack;
  39. static int cdfile = -1;
  40. static char cd_dev[64] = "/dev/cdrom";
  41. static void CDAudio_Eject(void)
  42. {
  43. if (cdfile == -1 || !enabled)
  44. return; // no cd init'd
  45. if ( ioctl(cdfile, CDROMEJECT) == -1 )
  46. Con_DPrintf("ioctl cdromeject failed\n");
  47. }
  48. static void CDAudio_CloseDoor(void)
  49. {
  50. if (cdfile == -1 || !enabled)
  51. return; // no cd init'd
  52. if ( ioctl(cdfile, CDROMCLOSETRAY) == -1 )
  53. Con_DPrintf("ioctl cdromclosetray failed\n");
  54. }
  55. static int CDAudio_GetAudioDiskInfo(void)
  56. {
  57. struct cdrom_tochdr tochdr;
  58. cdValid = false;
  59. if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 )
  60. {
  61. Con_DPrintf("ioctl cdromreadtochdr failed\n");
  62. return -1;
  63. }
  64. if (tochdr.cdth_trk0 < 1)
  65. {
  66. Con_DPrintf("CDAudio: no music tracks\n");
  67. return -1;
  68. }
  69. cdValid = true;
  70. maxTrack = tochdr.cdth_trk1;
  71. return 0;
  72. }
  73. void CDAudio_Play(byte track, qboolean looping)
  74. {
  75. struct cdrom_tocentry entry;
  76. struct cdrom_ti ti;
  77. if (cdfile == -1 || !enabled)
  78. return;
  79. if (!cdValid)
  80. {
  81. CDAudio_GetAudioDiskInfo();
  82. if (!cdValid)
  83. return;
  84. }
  85. track = remap[track];
  86. if (track < 1 || track > maxTrack)
  87. {
  88. Con_DPrintf("CDAudio: Bad track number %u.\n", track);
  89. return;
  90. }
  91. // don't try to play a non-audio track
  92. entry.cdte_track = track;
  93. entry.cdte_format = CDROM_MSF;
  94. if ( ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1 )
  95. {
  96. Con_DPrintf("ioctl cdromreadtocentry failed\n");
  97. return;
  98. }
  99. if (entry.cdte_ctrl == CDROM_DATA_TRACK)
  100. {
  101. Con_Printf("CDAudio: track %i is not audio\n", track);
  102. return;
  103. }
  104. if (playing)
  105. {
  106. if (playTrack == track)
  107. return;
  108. CDAudio_Stop();
  109. }
  110. ti.cdti_trk0 = track;
  111. ti.cdti_trk1 = track;
  112. ti.cdti_ind0 = 1;
  113. ti.cdti_ind1 = 99;
  114. if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 )
  115. {
  116. Con_DPrintf("ioctl cdromplaytrkind failed\n");
  117. return;
  118. }
  119. if ( ioctl(cdfile, CDROMRESUME) == -1 )
  120. Con_DPrintf("ioctl cdromresume failed\n");
  121. playLooping = looping;
  122. playTrack = track;
  123. playing = true;
  124. if (cdvolume == 0.0)
  125. CDAudio_Pause ();
  126. }
  127. void CDAudio_Stop(void)
  128. {
  129. if (cdfile == -1 || !enabled)
  130. return;
  131. if (!playing)
  132. return;
  133. if ( ioctl(cdfile, CDROMSTOP) == -1 )
  134. Con_DPrintf("ioctl cdromstop failed (%d)\n", errno);
  135. wasPlaying = false;
  136. playing = false;
  137. }
  138. void CDAudio_Pause(void)
  139. {
  140. if (cdfile == -1 || !enabled)
  141. return;
  142. if (!playing)
  143. return;
  144. if ( ioctl(cdfile, CDROMPAUSE) == -1 )
  145. Con_DPrintf("ioctl cdrompause failed\n");
  146. wasPlaying = playing;
  147. playing = false;
  148. }
  149. void CDAudio_Resume(void)
  150. {
  151. if (cdfile == -1 || !enabled)
  152. return;
  153. if (!cdValid)
  154. return;
  155. if (!wasPlaying)
  156. return;
  157. if ( ioctl(cdfile, CDROMRESUME) == -1 )
  158. Con_DPrintf("ioctl cdromresume failed\n");
  159. playing = true;
  160. }
  161. static void CD_f (void)
  162. {
  163. char *command;
  164. int ret;
  165. int n;
  166. if (Cmd_Argc() < 2)
  167. return;
  168. command = Cmd_Argv (1);
  169. if (Q_strcasecmp(command, "on") == 0)
  170. {
  171. enabled = true;
  172. return;
  173. }
  174. if (Q_strcasecmp(command, "off") == 0)
  175. {
  176. if (playing)
  177. CDAudio_Stop();
  178. enabled = false;
  179. return;
  180. }
  181. if (Q_strcasecmp(command, "reset") == 0)
  182. {
  183. enabled = true;
  184. if (playing)
  185. CDAudio_Stop();
  186. for (n = 0; n < 100; n++)
  187. remap[n] = n;
  188. CDAudio_GetAudioDiskInfo();
  189. return;
  190. }
  191. if (Q_strcasecmp(command, "remap") == 0)
  192. {
  193. ret = Cmd_Argc() - 2;
  194. if (ret <= 0)
  195. {
  196. for (n = 1; n < 100; n++)
  197. if (remap[n] != n)
  198. Con_Printf(" %u -> %u\n", n, remap[n]);
  199. return;
  200. }
  201. for (n = 1; n <= ret; n++)
  202. remap[n] = Q_atoi(Cmd_Argv (n+1));
  203. return;
  204. }
  205. if (Q_strcasecmp(command, "close") == 0)
  206. {
  207. CDAudio_CloseDoor();
  208. return;
  209. }
  210. if (!cdValid)
  211. {
  212. CDAudio_GetAudioDiskInfo();
  213. if (!cdValid)
  214. {
  215. Con_Printf("No CD in player.\n");
  216. return;
  217. }
  218. }
  219. if (Q_strcasecmp(command, "play") == 0)
  220. {
  221. CDAudio_Play((byte)Q_atoi(Cmd_Argv (2)), false);
  222. return;
  223. }
  224. if (Q_strcasecmp(command, "loop") == 0)
  225. {
  226. CDAudio_Play((byte)Q_atoi(Cmd_Argv (2)), true);
  227. return;
  228. }
  229. if (Q_strcasecmp(command, "stop") == 0)
  230. {
  231. CDAudio_Stop();
  232. return;
  233. }
  234. if (Q_strcasecmp(command, "pause") == 0)
  235. {
  236. CDAudio_Pause();
  237. return;
  238. }
  239. if (Q_strcasecmp(command, "resume") == 0)
  240. {
  241. CDAudio_Resume();
  242. return;
  243. }
  244. if (Q_strcasecmp(command, "eject") == 0)
  245. {
  246. if (playing)
  247. CDAudio_Stop();
  248. CDAudio_Eject();
  249. cdValid = false;
  250. return;
  251. }
  252. if (Q_strcasecmp(command, "info") == 0)
  253. {
  254. Con_Printf("%u tracks\n", maxTrack);
  255. if (playing)
  256. Con_Printf("Currently %s track %u\n", playLooping ? "looping" : "playing", playTrack);
  257. else if (wasPlaying)
  258. Con_Printf("Paused %s track %u\n", playLooping ? "looping" : "playing", playTrack);
  259. Con_Printf("Volume is %f\n", cdvolume);
  260. return;
  261. }
  262. }
  263. void CDAudio_Update(void)
  264. {
  265. struct cdrom_subchnl subchnl;
  266. static time_t lastchk;
  267. if (!enabled)
  268. return;
  269. if (bgmvolume.value != cdvolume)
  270. {
  271. if (cdvolume)
  272. {
  273. Cvar_SetValue ("bgmvolume", 0.0);
  274. cdvolume = bgmvolume.value;
  275. CDAudio_Pause ();
  276. }
  277. else
  278. {
  279. Cvar_SetValue ("bgmvolume", 1.0);
  280. cdvolume = bgmvolume.value;
  281. CDAudio_Resume ();
  282. }
  283. }
  284. if (playing && lastchk < time(NULL)) {
  285. lastchk = time(NULL) + 2; //two seconds between chks
  286. subchnl.cdsc_format = CDROM_MSF;
  287. if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1 ) {
  288. Con_DPrintf("ioctl cdromsubchnl failed\n");
  289. playing = false;
  290. return;
  291. }
  292. if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
  293. subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED) {
  294. playing = false;
  295. if (playLooping)
  296. CDAudio_Play(playTrack, true);
  297. }
  298. }
  299. }
  300. int CDAudio_Init(void)
  301. {
  302. int i;
  303. #if 0
  304. if (cls.state == ca_dedicated)
  305. return -1;
  306. #endif
  307. if (COM_CheckParm("-nocdaudio"))
  308. return -1;
  309. if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1) {
  310. strncpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
  311. cd_dev[sizeof(cd_dev) - 1] = 0;
  312. }
  313. if ((cdfile = open(cd_dev, O_RDONLY)) == -1) {
  314. Con_Printf("CDAudio_Init: open of \"%s\" failed (%i)\n", cd_dev, errno);
  315. cdfile = -1;
  316. return -1;
  317. }
  318. for (i = 0; i < 100; i++)
  319. remap[i] = i;
  320. initialized = true;
  321. enabled = true;
  322. if (CDAudio_GetAudioDiskInfo())
  323. {
  324. Con_Printf("CDAudio_Init: No CD in player.\n");
  325. cdValid = false;
  326. }
  327. Cmd_AddCommand ("cd", CD_f);
  328. Con_Printf("CD Audio Initialized\n");
  329. return 0;
  330. }
  331. void CDAudio_Shutdown(void)
  332. {
  333. if (!initialized)
  334. return;
  335. CDAudio_Stop();
  336. close(cdfile);
  337. cdfile = -1;
  338. }