2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
26 #include <sys/ioctl.h>
28 #include <sys/types.h>
34 #include <linux/cdrom.h>
38 static qboolean cdValid = false;
39 static qboolean playing = false;
40 static qboolean wasPlaying = false;
41 static qboolean initialized = false;
42 static qboolean enabled = true;
43 static qboolean playLooping = false;
44 static float cdvolume;
45 static byte remap[100];
46 static byte playTrack;
49 static int cdfile = -1;
50 static char cd_dev[64] = "/dev/cdrom";
52 static void CDAudio_Eject(void)
54 if (cdfile == -1 || !enabled)
55 return; // no cd init'd
57 if ( ioctl(cdfile, CDROMEJECT) == -1 )
58 Con_DPrintf("ioctl cdromeject failed\n");
62 static void CDAudio_CloseDoor(void)
64 if (cdfile == -1 || !enabled)
65 return; // no cd init'd
67 if ( ioctl(cdfile, CDROMCLOSETRAY) == -1 )
68 Con_DPrintf("ioctl cdromclosetray failed\n");
71 static int CDAudio_GetAudioDiskInfo(void)
73 struct cdrom_tochdr tochdr;
77 if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 )
79 Con_DPrintf("ioctl cdromreadtochdr failed\n");
83 if (tochdr.cdth_trk0 < 1)
85 Con_DPrintf("CDAudio: no music tracks\n");
90 maxTrack = tochdr.cdth_trk1;
96 void CDAudio_Play(byte track, qboolean looping)
98 struct cdrom_tocentry entry;
101 if (cdfile == -1 || !enabled)
106 CDAudio_GetAudioDiskInfo();
111 track = remap[track];
113 if (track < 1 || track > maxTrack)
115 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
119 // don't try to play a non-audio track
120 entry.cdte_track = track;
121 entry.cdte_format = CDROM_MSF;
122 if ( ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1 )
124 Con_DPrintf("ioctl cdromreadtocentry failed\n");
127 if (entry.cdte_ctrl == CDROM_DATA_TRACK)
129 Con_Printf("CDAudio: track %i is not audio\n", track);
135 if (playTrack == track)
140 ti.cdti_trk0 = track;
141 ti.cdti_trk1 = track;
145 if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 )
147 Con_DPrintf("ioctl cdromplaytrkind failed\n");
151 if ( ioctl(cdfile, CDROMRESUME) == -1 )
152 Con_DPrintf("ioctl cdromresume failed\n");
154 playLooping = looping;
163 void CDAudio_Stop(void)
165 if (cdfile == -1 || !enabled)
171 if ( ioctl(cdfile, CDROMSTOP) == -1 )
172 Con_DPrintf("ioctl cdromstop failed (%d)\n", errno);
178 void CDAudio_Pause(void)
180 if (cdfile == -1 || !enabled)
186 if ( ioctl(cdfile, CDROMPAUSE) == -1 )
187 Con_DPrintf("ioctl cdrompause failed\n");
189 wasPlaying = playing;
194 void CDAudio_Resume(void)
196 if (cdfile == -1 || !enabled)
205 if ( ioctl(cdfile, CDROMRESUME) == -1 )
206 Con_DPrintf("ioctl cdromresume failed\n");
210 static void CD_f (void)
219 command = Cmd_Argv (1);
221 if (Q_strcasecmp(command, "on") == 0)
227 if (Q_strcasecmp(command, "off") == 0)
235 if (Q_strcasecmp(command, "reset") == 0)
240 for (n = 0; n < 100; n++)
242 CDAudio_GetAudioDiskInfo();
246 if (Q_strcasecmp(command, "remap") == 0)
248 ret = Cmd_Argc() - 2;
251 for (n = 1; n < 100; n++)
253 Con_Printf(" %u -> %u\n", n, remap[n]);
256 for (n = 1; n <= ret; n++)
257 remap[n] = atoi(Cmd_Argv (n+1));
261 if (Q_strcasecmp(command, "close") == 0)
269 CDAudio_GetAudioDiskInfo();
272 Con_Printf("No CD in player.\n");
277 if (Q_strcasecmp(command, "play") == 0)
279 CDAudio_Play((byte)atoi(Cmd_Argv (2)), false);
283 if (Q_strcasecmp(command, "loop") == 0)
285 CDAudio_Play((byte)atoi(Cmd_Argv (2)), true);
289 if (Q_strcasecmp(command, "stop") == 0)
295 if (Q_strcasecmp(command, "pause") == 0)
301 if (Q_strcasecmp(command, "resume") == 0)
307 if (Q_strcasecmp(command, "eject") == 0)
316 if (Q_strcasecmp(command, "info") == 0)
318 Con_Printf("%u tracks\n", maxTrack);
320 Con_Printf("Currently %s track %u\n", playLooping ? "looping" : "playing", playTrack);
322 Con_Printf("Paused %s track %u\n", playLooping ? "looping" : "playing", playTrack);
323 Con_Printf("Volume is %f\n", cdvolume);
328 void CDAudio_Update(void)
330 struct cdrom_subchnl subchnl;
331 static time_t lastchk;
336 if (bgmvolume.value != cdvolume)
340 Cvar_SetValue ("bgmvolume", 0.0);
341 cdvolume = bgmvolume.value;
346 Cvar_SetValue ("bgmvolume", 1.0);
347 cdvolume = bgmvolume.value;
352 if (playing && lastchk < time(NULL)) {
353 lastchk = time(NULL) + 2; //two seconds between chks
354 subchnl.cdsc_format = CDROM_MSF;
355 if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1 ) {
356 Con_DPrintf("ioctl cdromsubchnl failed\n");
360 if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
361 subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED) {
364 CDAudio_Play(playTrack, true);
369 int CDAudio_Init(void)
373 if (cls.state == ca_dedicated)
376 if (COM_CheckParm("-nocdaudio"))
379 if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1) {
380 strncpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
381 cd_dev[sizeof(cd_dev) - 1] = 0;
384 if ((cdfile = open(cd_dev, O_RDONLY)) == -1) {
385 Con_Printf("CDAudio_Init: open of \"%s\" failed (%i)\n", cd_dev, errno);
390 for (i = 0; i < 100; i++)
395 if (CDAudio_GetAudioDiskInfo())
397 Con_Printf("CDAudio_Init: No CD in player.\n");
401 Cmd_AddCommand ("cd", CD_f);
403 Con_Printf("CD Audio Initialized\n");
409 void CDAudio_Shutdown(void)