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 #include <SDL.h>
25 #include <time.h>
26 
27 // If one of the functions fails, it returns -1, if not 0
28 
29 // SDL supports multiple cd devices - so we are going to support this, too.
30 static void CDAudio_SDL_CDDrive_f( void );
31 
32 // we only support playing one CD at a time
33 static SDL_CD *cd;
34 
ValidateDrive(void)35 static int ValidateDrive( void )
36 {
37 	if( cd && SDL_CDStatus( cd ) > 0 )
38 		return cdValid = true;
39 
40 	return cdValid = false;
41 }
42 
CDAudio_SysEject(void)43 void CDAudio_SysEject (void)
44 {
45 	SDL_CDEject( cd );
46 }
47 
48 
CDAudio_SysCloseDoor(void)49 void CDAudio_SysCloseDoor (void)
50 {
51 	//NO SDL FUNCTION
52 }
53 
CDAudio_SysGetAudioDiskInfo(void)54 int CDAudio_SysGetAudioDiskInfo (void)
55 {
56 	if( ValidateDrive() ) // everything > 0 is ok, 0 is trayempty and -1 is error
57 		return cd->numtracks;
58 	return -1;
59 }
60 
CDAudio_SysGetVolume(void)61 float CDAudio_SysGetVolume (void)
62 {
63 	return -1.0f;
64 }
65 
CDAudio_SysSetVolume(float volume)66 void CDAudio_SysSetVolume (float volume)
67 {
68 	//NO SDL FUNCTION
69 }
70 
CDAudio_SysPlay(int track)71 int CDAudio_SysPlay (int track)
72 {
73 	return SDL_CDPlayTracks(cd, track-1, 0, 1, 0);
74 }
75 
CDAudio_SysStop(void)76 int CDAudio_SysStop (void)
77 {
78 	return SDL_CDStop( cd );
79 }
80 
CDAudio_SysPause(void)81 int CDAudio_SysPause (void)
82 {
83 	return SDL_CDPause( cd );
84 }
85 
CDAudio_SysResume(void)86 int CDAudio_SysResume (void)
87 {
88 	return SDL_CDResume( cd );
89 }
90 
CDAudio_SysUpdate(void)91 int CDAudio_SysUpdate (void)
92 {
93 	static time_t lastchk = 0;
94 
95 	if (cdPlaying && lastchk < time(NULL))
96 	{
97 		lastchk = time(NULL) + 2; //two seconds between chks
98 		if( !cd || cd->status <= 0 ) {
99 			cdValid = false;
100 			return -1;
101 		}
102 		if (SDL_CDStatus( cd ) == CD_STOPPED)
103 		{
104 			if( cdPlayLooping )
105 				CDAudio_SysPlay( cdPlayTrack );
106 			else
107 				cdPlaying = false;
108 		}
109 	}
110 	return 0;
111 }
112 
CDAudio_SysInit(void)113 void CDAudio_SysInit (void)
114 {
115 	if( SDL_InitSubSystem( SDL_INIT_CDROM ) == -1 )
116 		Con_Print( "Failed to init the CDROM SDL subsystem!\n" );
117 
118 	Cmd_AddCommand( "cddrive", CDAudio_SDL_CDDrive_f, "select an SDL-detected CD drive by number" );
119 }
120 
IsAudioCD(void)121 static int IsAudioCD( void )
122 {
123 	int i;
124 	for( i = 0 ; i < cd->numtracks ; i++ )
125 		if( cd->track[ i ].type == SDL_AUDIO_TRACK )
126 			return true;
127 	return false;
128 }
129 
CDAudio_SysStartup(void)130 int CDAudio_SysStartup (void)
131 {
132 	int i;
133 	int numdrives;
134 
135 	numdrives = SDL_CDNumDrives();
136 	if( numdrives == -1 ) // was the CDROM system initialized correctly?
137 		return -1;
138 
139 	Con_Printf( "Found %i cdrom drives.\n", numdrives );
140 
141 	for( i = 0 ; i < numdrives ; i++, cd = NULL ) {
142 		cd = SDL_CDOpen( i );
143 		if( !cd ) {
144 			Con_Printf( "CD drive %i is invalid.\n", i );
145 			continue;
146 		}
147 
148 		if( CD_INDRIVE( SDL_CDStatus( cd ) ) )
149 			if( IsAudioCD() )
150 				break;
151 			else
152 				Con_Printf( "The CD in drive %i is not an audio cd.\n", i );
153 		else
154 			Con_Printf( "No CD in drive %i.\n", i );
155 
156 		SDL_CDClose( cd );
157 	}
158 
159 	if( i == numdrives && !cd )
160 		return -1;
161 
162 	return 0;
163 }
164 
CDAudio_SysShutdown(void)165 void CDAudio_SysShutdown (void)
166 {
167 	if( cd )
168 		SDL_CDClose( cd );
169 }
170 
CDAudio_SDL_CDDrive_f(void)171 void CDAudio_SDL_CDDrive_f( void )
172 {
173 	int i;
174 	int numdrives = SDL_CDNumDrives();
175 
176 	if( Cmd_Argc() != 2 ) {
177 		Con_Print( "cddrive <drivenr>\n" );
178 		return;
179 	}
180 
181 	i = atoi( Cmd_Argv( 1 ) );
182 	if( i >= numdrives ) {
183 		Con_Printf("Only %i drives!\n", numdrives );
184 		return;
185 	}
186 
187 	if( cd )
188 		SDL_CDClose( cd );
189 
190 	cd = SDL_CDOpen( i );
191 	if( !cd ) {
192 		Con_Printf( "Couldn't open drive %i.\n", i );
193 		return;
194 	}
195 
196 	if( !CD_INDRIVE( SDL_CDStatus( cd ) ) )
197 		Con_Printf( "No cd in drive %i.\n", i );
198 	else if( !IsAudioCD() )
199 		Con_Printf( "The CD in drive %i is not an audio CD.\n", i );
200 
201 	ValidateDrive();
202 }
203 
204 
205 
206 
207 
208