2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* This is the CD-audio control API for Simple DirectMedia Layer */
26 #include "SDL_cdrom.h"
27 #include "SDL_syscdrom.h"
29 #if !defined(__MACOS__)
30 #define CLIP_FRAMES 10 /* Some CD-ROMs won't go all the way */
33 static int SDL_cdinitted = 0;
34 static SDL_CD *default_cdrom;
36 /* The system level CD-ROM control functions */
37 struct CDcaps SDL_CDcaps = {
51 int SDL_CDROMInit(void)
56 retval = SDL_SYS_CDInit();
64 /* Check to see if the CD-ROM subsystem has been initialized */
65 static int CheckInit(int check_cdrom, SDL_CD **cdrom)
70 if ( check_cdrom && (*cdrom == NULL) ) {
71 *cdrom = default_cdrom;
72 if ( *cdrom == NULL ) {
73 SDL_SetError("CD-ROM not opened");
77 if ( ! SDL_cdinitted ) {
78 SDL_SetError("CD-ROM subsystem not initialized");
83 int SDL_CDNumDrives(void)
85 if ( ! CheckInit(0, NULL) ) {
91 const char *SDL_CDName(int drive)
93 if ( ! CheckInit(0, NULL) ) {
96 if ( drive >= SDL_numcds ) {
97 SDL_SetError("Invalid CD-ROM drive index");
100 if ( SDL_CDcaps.Name ) {
101 return(SDL_CDcaps.Name(drive));
107 SDL_CD *SDL_CDOpen(int drive)
109 struct SDL_CD *cdrom;
111 if ( ! CheckInit(0, NULL) ) {
114 if ( drive >= SDL_numcds ) {
115 SDL_SetError("Invalid CD-ROM drive index");
118 cdrom = (SDL_CD *)SDL_malloc(sizeof(*cdrom));
119 if ( cdrom == NULL ) {
123 SDL_memset(cdrom, 0, sizeof(*cdrom));
124 cdrom->id = SDL_CDcaps.Open(drive);
125 if ( cdrom->id < 0 ) {
129 default_cdrom = cdrom;
133 CDstatus SDL_CDStatus(SDL_CD *cdrom)
139 /* Check if the CD-ROM subsystem has been initialized */
140 if ( ! CheckInit(1, &cdrom) ) {
144 /* Get the current status of the drive */
145 cdrom->numtracks = 0;
146 cdrom->cur_track = 0;
147 cdrom->cur_frame = 0;
148 status = SDL_CDcaps.Status(cdrom, &i);
149 position = (Uint32)i;
150 cdrom->status = status;
152 /* Get the table of contents, if there's a CD available */
153 if ( CD_INDRIVE(status) ) {
154 if ( SDL_CDcaps.GetTOC(cdrom) < 0 ) {
157 /* If the drive is playing, get current play position */
158 if ( (status == CD_PLAYING) || (status == CD_PAUSED) ) {
159 for ( i=1; cdrom->track[i].offset <= position; ++i ) {
163 fprintf(stderr, "Current position: %d, track = %d (offset is %d)\n",
164 position, i-1, cdrom->track[i-1].offset);
166 cdrom->cur_track = i-1;
167 position -= cdrom->track[cdrom->cur_track].offset;
168 cdrom->cur_frame = position;
174 int SDL_CDPlayTracks(SDL_CD *cdrom,
175 int strack, int sframe, int ntracks, int nframes)
180 /* Check if the CD-ROM subsystem has been initialized */
181 if ( ! CheckInit(1, &cdrom) ) {
185 /* Determine the starting and ending tracks */
186 if ( (strack < 0) || (strack >= cdrom->numtracks) ) {
187 SDL_SetError("Invalid starting track");
190 if ( ! ntracks && ! nframes ) {
191 etrack = cdrom->numtracks;
194 etrack = strack+ntracks;
195 if ( etrack == strack ) {
196 eframe = sframe + nframes;
201 if ( etrack > cdrom->numtracks ) {
202 SDL_SetError("Invalid play length");
206 /* Skip data tracks and verify frame offsets */
207 while ( (strack <= etrack) &&
208 (cdrom->track[strack].type == SDL_DATA_TRACK) ) {
211 if ( sframe >= (int)cdrom->track[strack].length ) {
212 SDL_SetError("Invalid starting frame for track %d", strack);
215 while ( (etrack > strack) &&
216 (cdrom->track[etrack-1].type == SDL_DATA_TRACK) ) {
219 if ( eframe > (int)cdrom->track[etrack].length ) {
220 SDL_SetError("Invalid ending frame for track %d", etrack);
224 /* Determine start frame and play length */
225 start = (cdrom->track[strack].offset+sframe);
226 length = (cdrom->track[etrack].offset+eframe)-start;
228 /* I've never seen this necessary, but xmcd does it.. */
229 length -= CLIP_FRAMES; /* CLIP_FRAMES == 10 */
237 fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
239 return(SDL_CDcaps.Play(cdrom, start, length));
242 int SDL_CDPlay(SDL_CD *cdrom, int sframe, int length)
244 /* Check if the CD-ROM subsystem has been initialized */
245 if ( ! CheckInit(1, &cdrom) ) {
249 return(SDL_CDcaps.Play(cdrom, sframe, length));
252 int SDL_CDPause(SDL_CD *cdrom)
257 /* Check if the CD-ROM subsystem has been initialized */
258 if ( ! CheckInit(1, &cdrom) ) {
262 status = SDL_CDcaps.Status(cdrom, NULL);
265 retval = SDL_CDcaps.Pause(cdrom);
274 int SDL_CDResume(SDL_CD *cdrom)
279 /* Check if the CD-ROM subsystem has been initialized */
280 if ( ! CheckInit(1, &cdrom) ) {
284 status = SDL_CDcaps.Status(cdrom, NULL);
287 retval = SDL_CDcaps.Resume(cdrom);
295 int SDL_CDStop(SDL_CD *cdrom)
300 /* Check if the CD-ROM subsystem has been initialized */
301 if ( ! CheckInit(1, &cdrom) ) {
305 status = SDL_CDcaps.Status(cdrom, NULL);
309 retval = SDL_CDcaps.Stop(cdrom);
317 int SDL_CDEject(SDL_CD *cdrom)
319 /* Check if the CD-ROM subsystem has been initialized */
320 if ( ! CheckInit(1, &cdrom) ) {
323 return(SDL_CDcaps.Eject(cdrom));
326 void SDL_CDClose(SDL_CD *cdrom)
328 /* Check if the CD-ROM subsystem has been initialized */
329 if ( ! CheckInit(1, &cdrom) ) {
332 SDL_CDcaps.Close(cdrom);
334 default_cdrom = NULL;
337 void SDL_CDROMQuit(void)