19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is the CD-audio control API for Simple DirectMedia Layer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_cdrom.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syscdrom.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !defined(__MACOS__)
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define CLIP_FRAMES	10	/* Some CD-ROMs won't go all the way */
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_cdinitted = 0;
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_CD *default_cdrom;
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The system level CD-ROM control functions */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct CDcaps SDL_CDcaps = {
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Name */
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Open */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* GetTOC */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Status */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Play */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Pause */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Resume */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Stop */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Eject */
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	NULL,					/* Close */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_numcds;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDROMInit(void)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_numcds = 0;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = SDL_SYS_CDInit();
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retval == 0 ) {
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_cdinitted = 1;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	default_cdrom = NULL;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Check to see if the CD-ROM subsystem has been initialized */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckInit(int check_cdrom, SDL_CD **cdrom)
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int okay;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	okay = SDL_cdinitted;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( check_cdrom && (*cdrom == NULL) ) {
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*cdrom = default_cdrom;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( *cdrom == NULL ) {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("CD-ROM not opened");
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			okay = 0;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! SDL_cdinitted ) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("CD-ROM subsystem not initialized");
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(okay);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDNumDrives(void)
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(0, NULL) ) {
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_numcds);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char *SDL_CDName(int drive)
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(0, NULL) ) {
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( drive >= SDL_numcds ) {
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Invalid CD-ROM drive index");
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_CDcaps.Name ) {
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(SDL_CDcaps.Name(drive));
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return("");
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_CD *SDL_CDOpen(int drive)
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct SDL_CD *cdrom;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(0, NULL) ) {
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( drive >= SDL_numcds ) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Invalid CD-ROM drive index");
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom = (SDL_CD *)SDL_malloc(sizeof(*cdrom));
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cdrom == NULL ) {
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(cdrom, 0, sizeof(*cdrom));
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->id = SDL_CDcaps.Open(drive);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cdrom->id < 0 ) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(cdrom);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	default_cdrom = cdrom;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(cdrom);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallCDstatus SDL_CDStatus(SDL_CD *cdrom)
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 position;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the current status of the drive */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->numtracks = 0;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->cur_track = 0;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->cur_frame = 0;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	status = SDL_CDcaps.Status(cdrom, &i);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	position = (Uint32)i;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->status = status;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the table of contents, if there's a CD available */
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( CD_INDRIVE(status) ) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_CDcaps.GetTOC(cdrom) < 0 ) {
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			status = CD_ERROR;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If the drive is playing, get current play position */
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (status == CD_PLAYING) || (status == CD_PAUSED) ) {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=1; cdrom->track[i].offset <= position; ++i ) {
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Keep looking */;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Current position: %d, track = %d (offset is %d)\n",
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				position, i-1, cdrom->track[i-1].offset);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->cur_track = i-1;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			position -= cdrom->track[cdrom->cur_track].offset;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->cur_frame = position;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(status);
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDPlayTracks(SDL_CD *cdrom,
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int strack, int sframe, int ntracks, int nframes)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int etrack, eframe;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int start, length;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the starting and ending tracks */
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (strack < 0) || (strack >= cdrom->numtracks) ) {
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Invalid starting track");
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! ntracks && ! nframes ) {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		etrack = cdrom->numtracks;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		eframe = 0;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		etrack = strack+ntracks;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( etrack == strack ) {
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			eframe = sframe + nframes;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			eframe = nframes;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( etrack > cdrom->numtracks ) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Invalid play length");
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Skip data tracks and verify frame offsets */
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( (strack <= etrack) &&
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(cdrom->track[strack].type == SDL_DATA_TRACK) ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++strack;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( sframe >= (int)cdrom->track[strack].length ) {
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Invalid starting frame for track %d", strack);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( (etrack > strack) &&
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(cdrom->track[etrack-1].type == SDL_DATA_TRACK) ) {
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		--etrack;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( eframe > (int)cdrom->track[etrack].length ) {
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Invalid ending frame for track %d", etrack);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine start frame and play length */
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	start = (cdrom->track[strack].offset+sframe);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	length = (cdrom->track[etrack].offset+eframe)-start;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef CLIP_FRAMES
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* I've never seen this necessary, but xmcd does it.. */
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	length -= CLIP_FRAMES;	/* CLIP_FRAMES == 10 */
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( length < 0 ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Play! */
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_CDcaps.Play(cdrom, start, length));
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDPlay(SDL_CD *cdrom, int sframe, int length)
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_CDcaps.Play(cdrom, sframe, length));
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDPause(SDL_CD *cdrom)
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	status = SDL_CDcaps.Status(cdrom, NULL);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (status) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case CD_PLAYING:
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = SDL_CDcaps.Pause(cdrom);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = 0;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDResume(SDL_CD *cdrom)
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	status = SDL_CDcaps.Status(cdrom, NULL);
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (status) {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case CD_PAUSED:
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = SDL_CDcaps.Resume(cdrom);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = 0;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDStop(SDL_CD *cdrom)
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	status = SDL_CDcaps.Status(cdrom, NULL);
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (status) {
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case CD_PLAYING:
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case CD_PAUSED:
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = SDL_CDcaps.Stop(cdrom);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			retval = 0;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_CDEject(SDL_CD *cdrom)
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(CD_ERROR);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_CDcaps.Eject(cdrom));
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_CDClose(SDL_CD *cdrom)
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check if the CD-ROM subsystem has been initialized */
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! CheckInit(1, &cdrom) ) {
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Close(cdrom);
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(cdrom);
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	default_cdrom = NULL;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_CDROMQuit(void)
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SYS_CDQuit();
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_cdinitted = 0;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
342