]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_linux.c
minor cleanup/simplification (though a bit of a de-optimization) of explosion shading...
[divverent/darkplaces.git] / cd_linux.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.  
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
21 // rights reserved.
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <sys/ioctl.h>
27 #include <sys/file.h>
28 #include <sys/types.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <time.h>
32 #include <errno.h>
33
34 #include <linux/cdrom.h>
35
36 #include "quakedef.h"
37
38 // used by menu to ghost CD audio slider
39 qboolean cdaudioinitialized = false;
40 static qboolean cdValid = false;
41 static qboolean playing = false;
42 static qboolean wasPlaying = false;
43 static qboolean initialized = false;
44 static qboolean enabled = true;
45 static qboolean playLooping = false;
46 static float cdvolume;
47 static qbyte remap[100];
48 static qbyte playTrack;
49 static qbyte maxTrack;
50
51 static int cdfile = -1;
52 static char cd_dev[64] = "/dev/cdrom";
53
54 static void CDAudio_Eject(void)
55 {
56         if (cdfile == -1 || !enabled)
57                 return; // no cd init'd
58
59         if ( ioctl(cdfile, CDROMEJECT) == -1 ) 
60                 Con_DPrintf("ioctl cdromeject failed\n");
61 }
62
63
64 static void CDAudio_CloseDoor(void)
65 {
66         if (cdfile == -1 || !enabled)
67                 return; // no cd init'd
68
69         if ( ioctl(cdfile, CDROMCLOSETRAY) == -1 ) 
70                 Con_DPrintf("ioctl cdromclosetray failed\n");
71 }
72
73 static int CDAudio_GetAudioDiskInfo(void)
74 {
75         struct cdrom_tochdr tochdr;
76
77         cdValid = false;
78
79         if ( ioctl(cdfile, CDROMREADTOCHDR, &tochdr) == -1 ) 
80     {
81       Con_DPrintf("ioctl cdromreadtochdr failed\n");
82           return -1;
83     }
84
85         if (tochdr.cdth_trk0 < 1)
86         {
87                 Con_DPrintf("CDAudio: no music tracks\n");
88                 return -1;
89         }
90
91         cdValid = true;
92         maxTrack = tochdr.cdth_trk1;
93
94         return 0;
95 }
96
97
98 void CDAudio_Play(qbyte track, qboolean looping)
99 {
100         struct cdrom_tocentry entry;
101         struct cdrom_ti ti;
102
103         if (cdfile == -1 || !enabled)
104                 return;
105         
106         if (!cdValid)
107         {
108                 CDAudio_GetAudioDiskInfo();
109                 if (!cdValid)
110                         return;
111         }
112
113         track = remap[track];
114
115         if (track < 1 || track > maxTrack)
116         {
117                 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
118                 return;
119         }
120
121         // don't try to play a non-audio track
122         entry.cdte_track = track;
123         entry.cdte_format = CDROM_MSF;
124     if ( ioctl(cdfile, CDROMREADTOCENTRY, &entry) == -1 )
125         {
126                 Con_DPrintf("ioctl cdromreadtocentry failed\n");
127                 return;
128         }
129         if (entry.cdte_ctrl == CDROM_DATA_TRACK)
130         {
131                 Con_Printf("CDAudio: track %i is not audio\n", track);
132                 return;
133         }
134
135         if (playing)
136         {
137                 if (playTrack == track)
138                         return;
139                 CDAudio_Stop();
140         }
141
142         ti.cdti_trk0 = track;
143         ti.cdti_trk1 = track;
144         ti.cdti_ind0 = 1;
145         ti.cdti_ind1 = 99;
146
147         if ( ioctl(cdfile, CDROMPLAYTRKIND, &ti) == -1 ) 
148     {
149                 Con_DPrintf("ioctl cdromplaytrkind failed\n");
150                 return;
151     }
152
153         if ( ioctl(cdfile, CDROMRESUME) == -1 ) 
154                 Con_DPrintf("ioctl cdromresume failed\n");
155
156         playLooping = looping;
157         playTrack = track;
158         playing = true;
159
160         if (cdvolume == 0.0)
161                 CDAudio_Pause ();
162 }
163
164
165 void CDAudio_Stop(void)
166 {
167         if (cdfile == -1 || !enabled)
168                 return;
169         
170         if (!playing)
171                 return;
172
173         if ( ioctl(cdfile, CDROMSTOP) == -1 )
174                 Con_DPrintf("ioctl cdromstop failed (%d)\n", errno);
175
176         wasPlaying = false;
177         playing = false;
178 }
179
180 void CDAudio_Pause(void)
181 {
182         if (cdfile == -1 || !enabled)
183                 return;
184
185         if (!playing)
186                 return;
187
188         if ( ioctl(cdfile, CDROMPAUSE) == -1 ) 
189                 Con_DPrintf("ioctl cdrompause failed\n");
190
191         wasPlaying = playing;
192         playing = false;
193 }
194
195
196 void CDAudio_Resume(void)
197 {
198         if (cdfile == -1 || !enabled)
199                 return;
200         
201         if (!cdValid)
202                 return;
203
204         if (!wasPlaying)
205                 return;
206         
207         if ( ioctl(cdfile, CDROMRESUME) == -1 )
208                 Con_DPrintf("ioctl cdromresume failed\n");
209         playing = true;
210 }
211
212 static void CD_f (void)
213 {
214         char    *command;
215         int             ret;
216         int             n;
217
218         if (Cmd_Argc() < 2)
219                 return;
220
221         command = Cmd_Argv (1);
222
223         if (Q_strcasecmp(command, "on") == 0)
224         {
225                 enabled = true;
226                 return;
227         }
228
229         if (Q_strcasecmp(command, "off") == 0)
230         {
231                 if (playing)
232                         CDAudio_Stop();
233                 enabled = false;
234                 return;
235         }
236
237         if (Q_strcasecmp(command, "reset") == 0)
238         {
239                 enabled = true;
240                 if (playing)
241                         CDAudio_Stop();
242                 for (n = 0; n < 100; n++)
243                         remap[n] = n;
244                 CDAudio_GetAudioDiskInfo();
245                 return;
246         }
247
248         if (Q_strcasecmp(command, "remap") == 0)
249         {
250                 ret = Cmd_Argc() - 2;
251                 if (ret <= 0)
252                 {
253                         for (n = 1; n < 100; n++)
254                                 if (remap[n] != n)
255                                         Con_Printf("  %u -> %u\n", n, remap[n]);
256                         return;
257                 }
258                 for (n = 1; n <= ret; n++)
259                         remap[n] = atoi(Cmd_Argv (n+1));
260                 return;
261         }
262
263         if (Q_strcasecmp(command, "close") == 0)
264         {
265                 CDAudio_CloseDoor();
266                 return;
267         }
268
269         if (!cdValid)
270         {
271                 CDAudio_GetAudioDiskInfo();
272                 if (!cdValid)
273                 {
274                         Con_Printf("No CD in player.\n");
275                         return;
276                 }
277         }
278
279         if (Q_strcasecmp(command, "play") == 0)
280         {
281                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
282                 return;
283         }
284
285         if (Q_strcasecmp(command, "loop") == 0)
286         {
287                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
288                 return;
289         }
290
291         if (Q_strcasecmp(command, "stop") == 0)
292         {
293                 CDAudio_Stop();
294                 return;
295         }
296
297         if (Q_strcasecmp(command, "pause") == 0)
298         {
299                 CDAudio_Pause();
300                 return;
301         }
302
303         if (Q_strcasecmp(command, "resume") == 0)
304         {
305                 CDAudio_Resume();
306                 return;
307         }
308
309         if (Q_strcasecmp(command, "eject") == 0)
310         {
311                 if (playing)
312                         CDAudio_Stop();
313                 CDAudio_Eject();
314                 cdValid = false;
315                 return;
316         }
317
318         if (Q_strcasecmp(command, "info") == 0)
319         {
320                 Con_Printf("%u tracks\n", maxTrack);
321                 if (playing)
322                         Con_Printf("Currently %s track %u\n", playLooping ? "looping" : "playing", playTrack);
323                 else if (wasPlaying)
324                         Con_Printf("Paused %s track %u\n", playLooping ? "looping" : "playing", playTrack);
325                 Con_Printf("Volume is %f\n", cdvolume);
326                 return;
327         }
328 }
329
330 void CDAudio_Update(void)
331 {
332         struct cdrom_subchnl subchnl;
333         static time_t lastchk;
334
335         if (!enabled)
336                 return;
337
338         if (bgmvolume.value != cdvolume)
339         {
340                 if (cdvolume)
341                 {
342                         Cvar_SetValueQuick (&bgmvolume, 0.0);
343                         cdvolume = bgmvolume.value;
344                         CDAudio_Pause ();
345                 }
346                 else
347                 {
348                         Cvar_SetValueQuick (&bgmvolume, 1.0);
349                         cdvolume = bgmvolume.value;
350                         CDAudio_Resume ();
351                 }
352         }
353
354         if (playing && lastchk < time(NULL)) {
355                 lastchk = time(NULL) + 2; //two seconds between chks
356                 subchnl.cdsc_format = CDROM_MSF;
357                 if (ioctl(cdfile, CDROMSUBCHNL, &subchnl) == -1 ) {
358                         Con_DPrintf("ioctl cdromsubchnl failed\n");
359                         playing = false;
360                         return;
361                 }
362                 if (subchnl.cdsc_audiostatus != CDROM_AUDIO_PLAY &&
363                         subchnl.cdsc_audiostatus != CDROM_AUDIO_PAUSED) {
364                         playing = false;
365                         if (playLooping)
366                                 CDAudio_Play(playTrack, true);
367                 }
368         }
369 }
370
371 int CDAudio_Init(void)
372 {
373         int i;
374
375         if (cls.state == ca_dedicated)
376                 return -1;
377
378         if (COM_CheckParm("-nocdaudio"))
379                 return -1;
380
381         if ((i = COM_CheckParm("-cddev")) != 0 && i < com_argc - 1) {
382                 strncpy(cd_dev, com_argv[i + 1], sizeof(cd_dev));
383                 cd_dev[sizeof(cd_dev) - 1] = 0;
384         }
385
386         if ((cdfile = open(cd_dev, O_RDONLY)) == -1) {
387                 Con_Printf("CDAudio_Init: open of \"%s\" failed (%i)\n", cd_dev, errno);
388                 cdfile = -1;
389                 return -1;
390         }
391
392         for (i = 0; i < 100; i++)
393                 remap[i] = i;
394         cdaudioinitialized = true;
395         initialized = true;
396         enabled = true;
397
398         if (CDAudio_GetAudioDiskInfo())
399         {
400                 Con_Printf("CDAudio_Init: No CD in player.\n");
401                 cdValid = false;
402         }
403
404         Cmd_AddCommand ("cd", CD_f);
405
406         Con_Printf("CD Audio Initialized\n");
407
408         return 0;
409 }
410
411
412 void CDAudio_Shutdown(void)
413 {
414         if (!initialized)
415                 return;
416         CDAudio_Stop();
417         close(cdfile);
418         cdfile = -1;
419 }