]> icculus.org git repositories - divverent/darkplaces.git/blob - cd_shared.c
changed nexuiz hud to display a different (more minimal) sbar pic when viewsize is 110
[divverent/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 int CDAudio_SysPlay (qbyte track);
30 extern int CDAudio_SysStop (void);
31 extern int CDAudio_SysPause (void);
32 extern int CDAudio_SysResume (void);
33 extern int CDAudio_SysUpdate (void);
34 extern void CDAudio_SysInit (void);
35 extern int CDAudio_SysStartup (void);
36 extern void CDAudio_SysShutdown (void);
37
38 // used by menu to ghost CD audio slider
39 cvar_t  cdaudioinitialized = {CVAR_READONLY,"cdaudioinitialized","0"};
40
41 static qboolean wasPlaying = false;
42 static qboolean initialized = false;
43 static qboolean enabled = false;
44 static float cdvolume;
45 static qbyte remap[100];
46 static qbyte maxTrack;
47
48 // exported variables
49 qboolean cdValid = false;
50 qboolean cdPlaying = false;
51 qboolean cdPlayLooping = false;
52 qbyte cdPlayTrack;
53
54
55 static void CDAudio_Eject (void)
56 {
57         if (!enabled)
58                 return;
59
60         CDAudio_SysEject();
61 }
62
63
64 static void CDAudio_CloseDoor (void)
65 {
66         if (!enabled)
67                 return;
68
69         CDAudio_SysCloseDoor();
70 }
71
72 static int CDAudio_GetAudioDiskInfo (void)
73 {
74         int ret;
75
76         cdValid = false;
77
78         ret = CDAudio_SysGetAudioDiskInfo();
79         if (ret < 1)
80                 return -1;
81
82         cdValid = true;
83         maxTrack = ret;
84
85         return 0;
86 }
87
88
89 void CDAudio_Play (qbyte track, qboolean looping)
90 {
91         if (!enabled)
92                 return;
93
94         if (!cdValid)
95         {
96                 CDAudio_GetAudioDiskInfo();
97                 if (!cdValid)
98                         return;
99         }
100
101         track = remap[track];
102         if (track < 1 || track > maxTrack)
103         {
104                 Con_DPrintf("CDAudio: Bad track number %u.\n", track);
105                 return;
106         }
107
108         if (cdPlaying && cdPlayTrack == track)
109                 return;
110
111         if (CDAudio_SysPlay(track) == -1)
112                 return;
113
114         cdPlayLooping = looping;
115         cdPlayTrack = track;
116         cdPlaying = true;
117
118         if (cdvolume == 0.0)
119                 CDAudio_Pause ();
120 }
121
122
123 void CDAudio_Stop (void)
124 {
125         if (!enabled || !cdPlaying)
126                 return;
127
128         if (CDAudio_SysStop() == -1)
129                 return;
130
131         wasPlaying = false;
132         cdPlaying = false;
133 }
134
135 void CDAudio_Pause (void)
136 {
137         if (!enabled || !cdPlaying)
138                 return;
139
140         if (CDAudio_SysPause() == -1)
141                 return;
142
143         wasPlaying = cdPlaying;
144         cdPlaying = false;
145 }
146
147
148 void CDAudio_Resume (void)
149 {
150         if (!enabled || !cdValid || !wasPlaying)
151                 return;
152
153         if (CDAudio_SysResume() == -1)
154                 return;
155         cdPlaying = true;
156 }
157
158 static void CD_f (void)
159 {
160         const char *command;
161         int ret;
162         int n;
163
164         if (Cmd_Argc() < 2)
165                 return;
166
167         command = Cmd_Argv (1);
168
169         if (strcasecmp(command, "on") == 0)
170         {
171                 enabled = true;
172                 return;
173         }
174
175         if (strcasecmp(command, "off") == 0)
176         {
177                 if (cdPlaying)
178                         CDAudio_Stop();
179                 enabled = false;
180                 return;
181         }
182
183         if (strcasecmp(command, "reset") == 0)
184         {
185                 enabled = true;
186                 if (cdPlaying)
187                         CDAudio_Stop();
188                 for (n = 0; n < 100; n++)
189                         remap[n] = n;
190                 CDAudio_GetAudioDiskInfo();
191                 return;
192         }
193
194         if (strcasecmp(command, "remap") == 0)
195         {
196                 ret = Cmd_Argc() - 2;
197                 if (ret <= 0)
198                 {
199                         for (n = 1; n < 100; n++)
200                                 if (remap[n] != n)
201                                         Con_Printf("  %u -> %u\n", n, remap[n]);
202                         return;
203                 }
204                 for (n = 1; n <= ret; n++)
205                         remap[n] = atoi(Cmd_Argv (n+1));
206                 return;
207         }
208
209         if (strcasecmp(command, "close") == 0)
210         {
211                 CDAudio_CloseDoor();
212                 return;
213         }
214
215         if (!cdValid)
216         {
217                 CDAudio_GetAudioDiskInfo();
218                 if (!cdValid)
219                 {
220                         Con_Printf("No CD in player.\n");
221                         return;
222                 }
223         }
224
225         if (strcasecmp(command, "play") == 0)
226         {
227                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), false);
228                 return;
229         }
230
231         if (strcasecmp(command, "loop") == 0)
232         {
233                 CDAudio_Play((qbyte)atoi(Cmd_Argv (2)), true);
234                 return;
235         }
236
237         if (strcasecmp(command, "stop") == 0)
238         {
239                 CDAudio_Stop();
240                 return;
241         }
242
243         if (strcasecmp(command, "pause") == 0)
244         {
245                 CDAudio_Pause();
246                 return;
247         }
248
249         if (strcasecmp(command, "resume") == 0)
250         {
251                 CDAudio_Resume();
252                 return;
253         }
254
255         if (strcasecmp(command, "eject") == 0)
256         {
257                 if (cdPlaying)
258                         CDAudio_Stop();
259                 CDAudio_Eject();
260                 cdValid = false;
261                 return;
262         }
263
264         if (strcasecmp(command, "info") == 0)
265         {
266                 Con_Printf("%u tracks\n", maxTrack);
267                 if (cdPlaying)
268                         Con_Printf("Currently %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
269                 else if (wasPlaying)
270                         Con_Printf("Paused %s track %u\n", cdPlayLooping ? "looping" : "playing", cdPlayTrack);
271                 Con_Printf("Volume is %f\n", cdvolume);
272                 return;
273         }
274 }
275
276 void CDAudio_Update (void)
277 {
278         if (!enabled)
279                 return;
280
281         if (bgmvolume.value != cdvolume)
282         {
283                 if (cdvolume)
284                 {
285                         Cvar_SetValueQuick (&bgmvolume, 0.0);
286                         cdvolume = bgmvolume.value;
287                         CDAudio_Pause ();
288                 }
289                 else
290                 {
291                         Cvar_SetValueQuick (&bgmvolume, 1.0);
292                         cdvolume = bgmvolume.value;
293                         CDAudio_Resume ();
294                 }
295         }
296
297         CDAudio_SysUpdate();
298 }
299
300 int CDAudio_Init (void)
301 {
302         int i;
303
304         if (cls.state == ca_dedicated)
305                 return -1;
306
307         if (COM_CheckParm("-nocdaudio") || COM_CheckParm("-safe"))
308                 return -1;
309
310         CDAudio_SysInit();
311
312         for (i = 0; i < 100; i++)
313                 remap[i] = i;
314
315         Cvar_RegisterVariable(&cdaudioinitialized);
316         Cvar_SetValueQuick(&cdaudioinitialized, true);
317         enabled = true;
318
319         Cmd_AddCommand("cd", CD_f);
320
321         return 0;
322 }
323
324 int CDAudio_Startup (void)
325 {
326         if (CDAudio_SysStartup() == -1)
327                 return -1;
328
329         if (CDAudio_GetAudioDiskInfo())
330         {
331                 Con_DPrintf("CDAudio_Init: No CD in player.\n");
332                 cdValid = false;
333         }
334
335         initialized = true;
336
337         Con_DPrintf("CD Audio Initialized\n");
338
339         return 0;
340 }
341
342 void CDAudio_Shutdown (void)
343 {
344         if (!initialized)
345                 return;
346         CDAudio_Stop();
347         CDAudio_SysShutdown();
348         initialized = false;
349 }