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