]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cd_shared.c
changed main() argv parameter to non-const, and casting it to const on assignment...
[xonotic/darkplaces.git] / cd_shared.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 "quakedef.h"
24
25 // Prototypes of the system dependent functions
26 extern void CDAudio_SysEject (void);
27 extern void CDAudio_SysCloseDoor (void);
28 extern int CDAudio_SysGetAudioDiskInfo (void);
29 extern float CDAudio_SysGetVolume (void);
30 extern void CDAudio_SysSetVolume (float volume);
31 extern int CDAudio_SysPlay (qbyte track);
32 extern int CDAudio_SysStop (void);
33 extern int CDAudio_SysPause (void);
34 extern int CDAudio_SysResume (void);
35 extern int CDAudio_SysUpdate (void);
36 extern void CDAudio_SysInit (void);
37 extern int CDAudio_SysStartup (void);
38 extern void CDAudio_SysShutdown (void);
39
40 // used by menu to ghost CD audio slider
41 cvar_t  cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0"};
42
43 static qboolean wasPlaying = false;
44 static qboolean initialized = false;
45 static qboolean enabled = false;
46 static float cdvolume;
47 static qbyte remap[100];
48 static qbyte maxTrack;
49 static int faketrack = -1;
50
51 static float saved_vol = 1.0f;
52
53 // exported variables
54 qboolean cdValid = false;
55 qboolean cdPlaying = false;
56 qboolean cdPlayLooping = false;
57 qbyte cdPlayTrack;
58
59
60 static void CDAudio_Eject (void)
61 {
62         if (!enabled)
63                 return;
64
65         CDAudio_SysEject();
66 }
67
68
69 static void CDAudio_CloseDoor (void)
70 {
71         if (!enabled)
72                 return;
73
74         CDAudio_SysCloseDoor();
75 }
76
77 static int CDAudio_GetAudioDiskInfo (void)
78 {
79         int ret;
80
81         cdValid = false;
82
83         ret = CDAudio_SysGetAudioDiskInfo();
84         if (ret < 1)
85                 return -1;
86
87         cdValid = true;
88         maxTrack = ret;
89
90         return 0;
91 }
92
93
94 void CDAudio_Play (qbyte track, qboolean looping)
95 {
96         sfx_t* sfx;
97
98         if (!enabled)
99                 return;
100
101         track = remap[track];
102         if (track < 1)
103         {
104                 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
105                 return;
106         }
107
108         if (cdPlaying && cdPlayTrack == track)
109                 return;
110         CDAudio_Stop ();
111
112         // Try playing a fake track (sound file) first
113         sfx = S_PrecacheSound (va ("cdtracks/track%02u.wav", track), false, true, false);
114         if (sfx != NULL)
115         {
116                 faketrack = S_StartSound (-1, 0, sfx, vec3_origin, cdvolume, 0);
117                 if (faketrack != -1)
118                 {
119                         if (looping)
120                                 S_SetChannelFlag (faketrack, CHANNELFLAG_FORCELOOP, true);
121                         S_SetChannelFlag (faketrack, CHANNELFLAG_FULLVOLUME, true);
122                         Con_DPrintf ("Fake CD track %u playing...\n", track);
123                 }
124         }
125
126         // If we can't play a fake CD track, try the real one
127         if (faketrack == -1)
128         {
129                 if (!cdValid)
130                 {
131                         CDAudio_GetAudioDiskInfo();
132                         if (!cdValid)
133                         {
134                                 Con_Print ("No CD in player.\n");
135                                 return;
136                         }
137                 }
138
139                 if (track > maxTrack)
140                 {
141                         Con_Printf("CDAudio: Bad track number %u.\n", track);
142                         return;
143                 }
144
145                 if (CDAudio_SysPlay(track) == -1)
146                         return;
147         }
148
149         cdPlayLooping = looping;
150         cdPlayTrack = track;
151         cdPlaying = true;
152
153         if (cdvolume == 0.0)
154                 CDAudio_Pause ();
155 }
156
157
158 void CDAudio_Stop (void)
159 {
160         if (!enabled || !cdPlaying)
161                 return;
162
163         if (faketrack != -1)
164         {
165                 S_StopChannel (faketrack);
166                 faketrack = -1;
167         }
168         else if (CDAudio_SysStop() == -1)
169                 return;
170
171         wasPlaying = false;
172         cdPlaying = false;
173 }
174
175 void CDAudio_Pause (void)
176 {
177         if (!enabled || !cdPlaying)
178                 return;
179
180         if (faketrack != -1)
181                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, true);
182         else if (CDAudio_SysPause() == -1)
183                 return;
184
185         wasPlaying = cdPlaying;
186         cdPlaying = false;
187 }
188
189
190 void CDAudio_Resume (void)
191 {
192         if (!enabled || cdPlaying || !wasPlaying)
193                 return;
194
195         if (faketrack != -1)
196                 S_SetChannelFlag (faketrack, CHANNELFLAG_PAUSED, false);
197         else if (CDAudio_SysResume() == -1)
198                 return;
199         cdPlaying = true;
200 }
201
202 static void CD_f (void)
203 {
204         const char *command;
205         int ret;
206         int n;
207
208         if (Cmd_Argc() < 2)
209                 return;
210
211         command = Cmd_Argv (1);
212
213         if (strcasecmp(command, "on") == 0)
214         {
215                 enabled = true;
216                 return;
217         }
218
219         if (strcasecmp(command, "off") == 0)
220         {
221                 if (cdPlaying)
222                         CDAudio_Stop();
223                 enabled = false;
224                 return;
225         }
226
227         if (strcasecmp(command, "reset") == 0)
228         {
229                 enabled = true;
230                 if (cdPlaying)
231                         CDAudio_Stop();
232                 for (n = 0; n < 100; n++)
233                         remap[n] = n;
234                 CDAudio_GetAudioDiskInfo();
235                 return;
236         }
237
238         if (strcasecmp(command, "remap") == 0)
239         {
240                 ret = Cmd_Argc() - 2;
241                 if (ret <= 0)
242                 {
243                         for (n = 1; n < 100; n++)
244                                 if (remap[n] != n)
245                                         Con_Printf("  %u -> %u\n", n, remap[n]);
246                         return;
247                 }
248                 for (n = 1; n <= ret; n++)
249                         remap[n] = atoi(Cmd_Argv (n+1));
250                 return;
251         }
252
253         if (strcasecmp(command, "close") == 0)
254         {
255                 CDAudio_CloseDoor();
256                 return;
257         }
258
259         if (strcasecmp(command, "play") == 0)
260         {
261                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
262                 return;
263         }
264
265         if (strcasecmp(command, "loop") == 0)
266         {
267                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
268                 return;
269         }
270
271         if (strcasecmp(command, "stop") == 0)
272         {
273                 CDAudio_Stop();
274                 return;
275         }
276
277         if (strcasecmp(command, "pause") == 0)
278         {
279                 CDAudio_Pause();
280                 return;
281         }
282
283         if (strcasecmp(command, "resume") == 0)
284         {
285                 CDAudio_Resume();
286                 return;
287         }
288
289         if (strcasecmp(command, "eject") == 0)
290         {
291                 if (cdPlaying && faketrack == -1)
292                         CDAudio_Stop();
293                 CDAudio_Eject();
294                 cdValid = false;
295                 return;
296         }
297
298         if (strcasecmp(command, "info") == 0)
299         {
300                 CDAudio_GetAudioDiskInfo ();
301                 if (cdValid)
302                         Con_Printf("%u tracks on CD.\n", maxTrack);
303                 else
304                         Con_Print ("No CD in player.\n");
305                 if (cdPlaying)
306                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
307                 else if (wasPlaying)
308                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
309                 Con_Printf("Volume is %f\n", cdvolume);
310                 return;
311         }
312 }
313
314 void CDAudio_SetVolume (float newvol)
315 {
316         // If the volume hasn't changed
317         if (newvol == cdvolume)
318                 return;
319
320         // If the CD has been muted
321         if (newvol == 0.0f)
322                 CDAudio_Pause ();
323         else
324         {
325                 // If the CD has been unmuted
326                 if (cdvolume == 0.0f)
327                         CDAudio_Resume ();
328
329                 if (faketrack != -1)
330                         S_SetChannelVolume (faketrack, newvol);
331                 CDAudio_SysSetVolume (newvol);
332         }
333
334         cdvolume = newvol;
335 }
336
337 void CDAudio_Update (void)
338 {
339         if (!enabled)
340                 return;
341
342         CDAudio_SetVolume (bgmvolume.value);
343
344         if (faketrack == -1)
345                 CDAudio_SysUpdate();
346 }
347
348 int CDAudio_Init (void)
349 {
350         int i;
351
352         if (cls.state == ca_dedicated)
353                 return -1;
354
355 // COMMANDLINEOPTION: Sound: -nocdaudio disables CD audio support
356         if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
357                 return -1;
358
359         CDAudio_SysInit();
360
361         for (i = 0; i < 100; i++)
362                 remap[i] = i;
363
364         Cvar_RegisterVariable(&cdaudioinitialized);
365         Cvar_SetValueQuick(&cdaudioinitialized, true);
366         enabled = true;
367
368         Cmd_AddCommand("cd", CD_f);
369
370         return 0;
371 }
372
373 int CDAudio_Startup (void)
374 {
375         CDAudio_SysStartup ();
376
377         if (CDAudio_GetAudioDiskInfo())
378         {
379                 Con_DPrint("CDAudio_Init: No CD in player.\n");
380                 cdValid = false;
381         }
382
383         saved_vol = CDAudio_SysGetVolume ();
384         if (saved_vol < 0.0f)
385         {
386                 Con_DPrint ("Can't get initial CD volume\n");
387                 saved_vol = 1.0f;
388         }
389         else
390                 Con_DPrintf ("Initial CD volume: %g\n", saved_vol);
391
392         initialized = true;
393
394         Con_DPrint("CD Audio Initialized\n");
395
396         return 0;
397 }
398
399 void CDAudio_Shutdown (void)
400 {
401         if (!initialized)
402                 return;
403
404         CDAudio_SysSetVolume (saved_vol);
405
406         CDAudio_Stop();
407         CDAudio_SysShutdown();
408         initialized = false;
409 }