1 /*
2 Copyright (C) 2004 Andreas Kirsch (used cd_null.c as template)
3 Copyright (C) 1996-1997 Id Software, Inc.
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 
14 See the GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 
20 */
21 
22 #include "quakedef.h"
23 #include "cdaudio.h"
24 
25 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
26 // SDL 1.2 has CD audio
27 
28 #include <SDL.h>
29 #include <time.h>
30 
31 // If one of the functions fails, it returns -1, if not 0
32 
33 // SDL supports multiple cd devices - so we are going to support this, too.
34 static void CDAudio_SDL_CDDrive_f( void );
35 
36 // we only support playing one CD at a time
37 static SDL_CD *cd;
38 
ValidateDrive(void)39 static int ValidateDrive( void )
40 {
41 	if( cd && SDL_CDStatus( cd ) > 0 )
42 		return cdValid = true;
43 
44 	return cdValid = false;
45 }
46 
CDAudio_SysEject(void)47 void CDAudio_SysEject (void)
48 {
49 	SDL_CDEject( cd );
50 }
51 
52 
CDAudio_SysCloseDoor(void)53 void CDAudio_SysCloseDoor (void)
54 {
55 	//NO SDL FUNCTION
56 }
57 
CDAudio_SysGetAudioDiskInfo(void)58 int CDAudio_SysGetAudioDiskInfo (void)
59 {
60 	if( ValidateDrive() ) // everything > 0 is ok, 0 is trayempty and -1 is error
61 		return cd->numtracks;
62 	return -1;
63 }
64 
CDAudio_SysGetVolume(void)65 float CDAudio_SysGetVolume (void)
66 {
67 	return -1.0f;
68 }
69 
CDAudio_SysSetVolume(float volume)70 void CDAudio_SysSetVolume (float volume)
71 {
72 	//NO SDL FUNCTION
73 }
74 
CDAudio_SysPlay(int track)75 int CDAudio_SysPlay (int track)
76 {
77 	return SDL_CDPlayTracks(cd, track-1, 0, 1, 0);
78 }
79 
CDAudio_SysStop(void)80 int CDAudio_SysStop (void)
81 {
82 	return SDL_CDStop( cd );
83 }
84 
CDAudio_SysPause(void)85 int CDAudio_SysPause (void)
86 {
87 	return SDL_CDPause( cd );
88 }
89 
CDAudio_SysResume(void)90 int CDAudio_SysResume (void)
91 {
92 	return SDL_CDResume( cd );
93 }
94 
CDAudio_SysUpdate(void)95 int CDAudio_SysUpdate (void)
96 {
97 	static time_t lastchk = 0;
98 
99 	if (cdPlaying && lastchk < time(NULL))
100 	{
101 		lastchk = time(NULL) + 2; //two seconds between chks
102 		if( !cd || cd->status <= 0 ) {
103 			cdValid = false;
104 			return -1;
105 		}
106 		if (SDL_CDStatus( cd ) == CD_STOPPED)
107 		{
108 			if( cdPlayLooping )
109 				CDAudio_SysPlay( cdPlayTrack );
110 			else
111 				cdPlaying = false;
112 		}
113 	}
114 	return 0;
115 }
116 
CDAudio_SysInit(void)117 void CDAudio_SysInit (void)
118 {
119 	if( SDL_InitSubSystem( SDL_INIT_CDROM ) == -1 )
120 		Con_Print( "Failed to init the CDROM SDL subsystem!\n" );
121 
122 	Cmd_AddCommand( "cddrive", CDAudio_SDL_CDDrive_f, "select an SDL-detected CD drive by number" );
123 }
124 
IsAudioCD(void)125 static int IsAudioCD( void )
126 {
127 	int i;
128 	for( i = 0 ; i < cd->numtracks ; i++ )
129 		if( cd->track[ i ].type == SDL_AUDIO_TRACK )
130 			return true;
131 	return false;
132 }
133 
CDAudio_SysStartup(void)134 int CDAudio_SysStartup (void)
135 {
136 	int i;
137 	int numdrives;
138 
139 	numdrives = SDL_CDNumDrives();
140 	if( numdrives == -1 ) // was the CDROM system initialized correctly?
141 		return -1;
142 
143 	Con_Printf( "Found %i cdrom drives.\n", numdrives );
144 
145 	for( i = 0 ; i < numdrives ; i++, cd = NULL ) {
146 		cd = SDL_CDOpen( i );
147 		if( !cd ) {
148 			Con_Printf( "CD drive %i is invalid.\n", i );
149 			continue;
150 		}
151 
152 		if( CD_INDRIVE( SDL_CDStatus( cd ) ) )
153 			if( IsAudioCD() )
154 				break;
155 			else
156 				Con_Printf( "The CD in drive %i is not an audio cd.\n", i );
157 		else
158 			Con_Printf( "No CD in drive %i.\n", i );
159 
160 		SDL_CDClose( cd );
161 	}
162 
163 	if( i == numdrives && !cd )
164 		return -1;
165 
166 	return 0;
167 }
168 
CDAudio_SysShutdown(void)169 void CDAudio_SysShutdown (void)
170 {
171 	if( cd )
172 		SDL_CDClose( cd );
173 }
174 
CDAudio_SDL_CDDrive_f(void)175 void CDAudio_SDL_CDDrive_f( void )
176 {
177 	int i;
178 	int numdrives = SDL_CDNumDrives();
179 
180 	if( Cmd_Argc() != 2 ) {
181 		Con_Print( "cddrive <drivenr>\n" );
182 		return;
183 	}
184 
185 	i = atoi( Cmd_Argv( 1 ) );
186 	if( i >= numdrives ) {
187 		Con_Printf("Only %i drives!\n", numdrives );
188 		return;
189 	}
190 
191 	if( cd )
192 		SDL_CDClose( cd );
193 
194 	cd = SDL_CDOpen( i );
195 	if( !cd ) {
196 		Con_Printf( "Couldn't open drive %i.\n", i );
197 		return;
198 	}
199 
200 	if( !CD_INDRIVE( SDL_CDStatus( cd ) ) )
201 		Con_Printf( "No cd in drive %i.\n", i );
202 	else if( !IsAudioCD() )
203 		Con_Printf( "The CD in drive %i is not an audio CD.\n", i );
204 
205 	ValidateDrive();
206 }
207 
208 
209 
210 
211 
212 #else
213 // SDL 1.3 does not have CD audio
214 
CDAudio_SysEject(void)215 void CDAudio_SysEject (void)
216 {
217 }
218 
219 
CDAudio_SysCloseDoor(void)220 void CDAudio_SysCloseDoor (void)
221 {
222 }
223 
224 
CDAudio_SysGetAudioDiskInfo(void)225 int CDAudio_SysGetAudioDiskInfo (void)
226 {
227 	return -1;
228 }
229 
230 
CDAudio_SysGetVolume(void)231 float CDAudio_SysGetVolume (void)
232 {
233 	return -1.0f;
234 }
235 
236 
CDAudio_SysSetVolume(float fvolume)237 void CDAudio_SysSetVolume (float fvolume)
238 {
239 }
240 
241 
CDAudio_SysPlay(int track)242 int CDAudio_SysPlay (int track)
243 {
244 	return -1;
245 }
246 
247 
CDAudio_SysStop(void)248 int CDAudio_SysStop (void)
249 {
250 	return -1;
251 }
252 
253 
CDAudio_SysPause(void)254 int CDAudio_SysPause (void)
255 {
256 	return -1;
257 }
258 
CDAudio_SysResume(void)259 int CDAudio_SysResume (void)
260 {
261 	return -1;
262 }
263 
CDAudio_SysUpdate(void)264 int CDAudio_SysUpdate (void)
265 {
266 	return -1;
267 }
268 
269 
CDAudio_SysInit(void)270 void CDAudio_SysInit (void)
271 {
272 }
273 
CDAudio_SysStartup(void)274 int CDAudio_SysStartup (void)
275 {
276 	return -1;
277 }
278 
CDAudio_SysShutdown(void)279 void CDAudio_SysShutdown (void)
280 {
281 }
282 #endif
283 
284