src/cdrom/SDL_cdrom.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:30:25 +0000
changeset 249 e3d0d44f6f2e
parent 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
*** empty log message ***
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    slouken@devolution.com
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* This is the CD-audio control API for Simple DirectMedia Layer */
slouken@0
    29
slouken@0
    30
#include <stdio.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <string.h>
slouken@0
    33
slouken@0
    34
#include "SDL_error.h"
slouken@0
    35
#include "SDL_cdrom.h"
slouken@0
    36
#include "SDL_syscdrom.h"
slouken@0
    37
slouken@0
    38
#if !defined(macintosh)
slouken@0
    39
#define CLIP_FRAMES	10	/* Some CD-ROMs won't go all the way */
slouken@0
    40
#endif
slouken@0
    41
slouken@0
    42
static int SDL_cdinitted = 0;
slouken@0
    43
static SDL_CD *default_cdrom;
slouken@0
    44
slouken@0
    45
/* The system level CD-ROM control functions */
slouken@0
    46
struct CDcaps SDL_CDcaps = {
slouken@0
    47
	NULL,					/* Name */
slouken@0
    48
	NULL,					/* Open */
slouken@0
    49
	NULL,					/* GetTOC */
slouken@0
    50
	NULL,					/* Status */
slouken@0
    51
	NULL,					/* Play */
slouken@0
    52
	NULL,					/* Pause */
slouken@0
    53
	NULL,					/* Resume */
slouken@0
    54
	NULL,					/* Stop */
slouken@0
    55
	NULL,					/* Eject */
slouken@0
    56
	NULL,					/* Close */
slouken@0
    57
};
slouken@0
    58
int SDL_numcds;
slouken@0
    59
slouken@0
    60
int SDL_CDROMInit(void)
slouken@0
    61
{
slouken@0
    62
	int retval;
slouken@0
    63
slouken@0
    64
	SDL_numcds = 0;
slouken@0
    65
	retval = SDL_SYS_CDInit();
slouken@0
    66
	if ( retval == 0 ) {
slouken@0
    67
		SDL_cdinitted = 1;
slouken@0
    68
	}
slouken@0
    69
	default_cdrom = NULL;
slouken@0
    70
	return(retval);
slouken@0
    71
}
slouken@0
    72
slouken@0
    73
/* Check to see if the CD-ROM subsystem has been initialized */
slouken@0
    74
static int CheckInit(int check_cdrom, SDL_CD **cdrom)
slouken@0
    75
{
slouken@0
    76
	int okay;
slouken@0
    77
slouken@0
    78
	okay = SDL_cdinitted;
slouken@0
    79
	if ( check_cdrom && (*cdrom == NULL) ) {
slouken@0
    80
		*cdrom = default_cdrom;
slouken@0
    81
		if ( *cdrom == NULL ) {
slouken@0
    82
			SDL_SetError("CD-ROM not opened");
slouken@0
    83
			okay = 0;
slouken@0
    84
		}
slouken@0
    85
	}
slouken@0
    86
	if ( ! SDL_cdinitted ) {
slouken@0
    87
		SDL_SetError("CD-ROM subsystem not initialized");
slouken@0
    88
	}
slouken@0
    89
	return(okay);
slouken@0
    90
}
slouken@0
    91
slouken@0
    92
int SDL_CDNumDrives(void)
slouken@0
    93
{
slouken@0
    94
	if ( ! CheckInit(0, NULL) ) {
slouken@0
    95
		return(-1);
slouken@0
    96
	}
slouken@0
    97
	return(SDL_numcds);
slouken@0
    98
}
slouken@0
    99
slouken@0
   100
const char *SDL_CDName(int drive)
slouken@0
   101
{
slouken@0
   102
	if ( ! CheckInit(0, NULL) ) {
slouken@0
   103
		return(NULL);
slouken@0
   104
	}
slouken@0
   105
	if ( drive >= SDL_numcds ) {
slouken@0
   106
		SDL_SetError("Invalid CD-ROM drive index");
slouken@0
   107
		return(NULL);
slouken@0
   108
	}
slouken@0
   109
	if ( SDL_CDcaps.Name ) {
slouken@0
   110
		return(SDL_CDcaps.Name(drive));
slouken@0
   111
	} else {
slouken@0
   112
		return("");
slouken@0
   113
	}
slouken@0
   114
}
slouken@0
   115
slouken@0
   116
SDL_CD *SDL_CDOpen(int drive)
slouken@0
   117
{
slouken@0
   118
	struct SDL_CD *cdrom;
slouken@0
   119
slouken@0
   120
	if ( ! CheckInit(0, NULL) ) {
slouken@0
   121
		return(NULL);
slouken@0
   122
	}
slouken@0
   123
	if ( drive >= SDL_numcds ) {
slouken@0
   124
		SDL_SetError("Invalid CD-ROM drive index");
slouken@0
   125
		return(NULL);
slouken@0
   126
	}
slouken@0
   127
	cdrom = (SDL_CD *)malloc(sizeof(*cdrom));
slouken@0
   128
	if ( cdrom == NULL ) {
slouken@0
   129
		SDL_OutOfMemory();
slouken@0
   130
		return(NULL);
slouken@0
   131
	}
slouken@0
   132
	memset(cdrom, 0, sizeof(*cdrom));
slouken@0
   133
	cdrom->id = SDL_CDcaps.Open(drive);
slouken@0
   134
	if ( cdrom->id < 0 ) {
slouken@0
   135
		free(cdrom);
slouken@0
   136
		return(NULL);
slouken@0
   137
	}
slouken@0
   138
	default_cdrom = cdrom;
slouken@0
   139
	return(cdrom);
slouken@0
   140
}
slouken@0
   141
slouken@0
   142
CDstatus SDL_CDStatus(SDL_CD *cdrom)
slouken@0
   143
{
slouken@0
   144
	CDstatus status;
slouken@0
   145
	int i;
slouken@0
   146
	Uint32 position;
slouken@0
   147
slouken@0
   148
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   149
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   150
		return(CD_ERROR);
slouken@0
   151
	}
slouken@0
   152
slouken@0
   153
	/* Get the current status of the drive */
slouken@0
   154
	cdrom->numtracks = 0;
slouken@0
   155
	cdrom->cur_track = 0;
slouken@0
   156
	cdrom->cur_frame = 0;
slouken@0
   157
	status = SDL_CDcaps.Status(cdrom, &i);
slouken@0
   158
	position = (Uint32)i;
slouken@0
   159
	cdrom->status = status;
slouken@0
   160
slouken@0
   161
	/* Get the table of contents, if there's a CD available */
slouken@0
   162
	if ( CD_INDRIVE(status) ) {
slouken@0
   163
		if ( SDL_CDcaps.GetTOC(cdrom) < 0 ) {
slouken@0
   164
			status = CD_ERROR;
slouken@0
   165
		}
slouken@0
   166
		/* If the drive is playing, get current play position */
slouken@0
   167
		if ( (status == CD_PLAYING) || (status == CD_PAUSED) ) {
slouken@0
   168
			for ( i=1; cdrom->track[i].offset <= position; ++i ) {
slouken@0
   169
				/* Keep looking */;
slouken@0
   170
			}
slouken@0
   171
#ifdef DEBUG_CDROM
slouken@0
   172
  fprintf(stderr, "Current position: %d, track = %d (offset is %d)\n",
slouken@0
   173
				position, i-1, cdrom->track[i-1].offset);
slouken@0
   174
#endif
slouken@0
   175
			cdrom->cur_track = i-1;
slouken@0
   176
			position -= cdrom->track[cdrom->cur_track].offset;
slouken@0
   177
			cdrom->cur_frame = position;
slouken@0
   178
		}
slouken@0
   179
	}
slouken@0
   180
	return(status);
slouken@0
   181
}
slouken@0
   182
slouken@0
   183
int SDL_CDPlayTracks(SDL_CD *cdrom,
slouken@0
   184
			int strack, int sframe, int ntracks, int nframes)
slouken@0
   185
{
slouken@0
   186
	int etrack, eframe;
slouken@0
   187
	int start, length;
slouken@0
   188
slouken@0
   189
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   190
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   191
		return(CD_ERROR);
slouken@0
   192
	}
slouken@0
   193
slouken@0
   194
	/* Determine the starting and ending tracks */
slouken@0
   195
	if ( (strack < 0) || (strack >= cdrom->numtracks) ) {
slouken@0
   196
		SDL_SetError("Invalid starting track");
slouken@0
   197
		return(CD_ERROR);
slouken@0
   198
	}
slouken@0
   199
	if ( ! ntracks && ! nframes ) {
slouken@0
   200
		etrack = cdrom->numtracks;
slouken@0
   201
		eframe = 0;
slouken@0
   202
	} else {
slouken@0
   203
		etrack = strack+ntracks;
slouken@0
   204
		if ( etrack == strack ) {
slouken@0
   205
			eframe = sframe + nframes;
slouken@0
   206
		} else {
slouken@0
   207
			eframe = nframes;
slouken@0
   208
		}
slouken@0
   209
	}
slouken@0
   210
	if ( etrack > cdrom->numtracks ) {
slouken@0
   211
		SDL_SetError("Invalid play length");
slouken@0
   212
		return(CD_ERROR);
slouken@0
   213
	}
slouken@0
   214
slouken@0
   215
	/* Skip data tracks and verify frame offsets */
slouken@0
   216
	while ( (strack <= etrack) &&
slouken@0
   217
			(cdrom->track[strack].type == SDL_DATA_TRACK) ) {
slouken@0
   218
		++strack;
slouken@0
   219
	}
slouken@0
   220
	if ( sframe >= (int)cdrom->track[strack].length ) {
slouken@0
   221
		SDL_SetError("Invalid starting frame for track %d", strack);
slouken@0
   222
		return(CD_ERROR);
slouken@0
   223
	}
slouken@0
   224
	while ( (etrack > strack) &&
slouken@0
   225
			(cdrom->track[etrack-1].type == SDL_DATA_TRACK) ) {
slouken@0
   226
		--etrack;
slouken@0
   227
	}
slouken@0
   228
	if ( eframe > (int)cdrom->track[etrack].length ) {
slouken@0
   229
		SDL_SetError("Invalid ending frame for track %d", etrack);
slouken@0
   230
		return(CD_ERROR);
slouken@0
   231
	}
slouken@0
   232
slouken@0
   233
	/* Determine start frame and play length */
slouken@0
   234
	start = (cdrom->track[strack].offset+sframe);
slouken@0
   235
	length = (cdrom->track[etrack].offset+eframe)-start;
slouken@0
   236
#ifdef CLIP_FRAMES
slouken@0
   237
	/* I've never seen this necessary, but xmcd does it.. */
slouken@0
   238
	length -= CLIP_FRAMES;	/* CLIP_FRAMES == 10 */
slouken@0
   239
#endif
slouken@0
   240
	if ( length < 0 ) {
slouken@0
   241
		return(0);
slouken@0
   242
	}
slouken@0
   243
slouken@0
   244
	/* Play! */
slouken@0
   245
#ifdef DEBUG_CDROM
slouken@0
   246
  fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
slouken@0
   247
#endif
slouken@0
   248
	return(SDL_CDcaps.Play(cdrom, start, length));
slouken@0
   249
}
slouken@0
   250
slouken@0
   251
int SDL_CDPlay(SDL_CD *cdrom, int sframe, int length)
slouken@0
   252
{
slouken@0
   253
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   254
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   255
		return(CD_ERROR);
slouken@0
   256
	}
slouken@0
   257
slouken@0
   258
	return(SDL_CDcaps.Play(cdrom, sframe, length));
slouken@0
   259
}
slouken@0
   260
slouken@0
   261
int SDL_CDPause(SDL_CD *cdrom)
slouken@0
   262
{
slouken@0
   263
	CDstatus status;
slouken@0
   264
	int retval;
slouken@0
   265
slouken@0
   266
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   267
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   268
		return(CD_ERROR);
slouken@0
   269
	}
slouken@0
   270
slouken@0
   271
	status = SDL_CDcaps.Status(cdrom, NULL);
slouken@0
   272
	switch (status) {
slouken@0
   273
		case CD_PLAYING:
slouken@0
   274
			retval = SDL_CDcaps.Pause(cdrom);
slouken@0
   275
			break;
slouken@0
   276
		default:
slouken@0
   277
			retval = 0;
slouken@0
   278
			break;
slouken@0
   279
	}
slouken@0
   280
	return(retval);
slouken@0
   281
}
slouken@0
   282
slouken@0
   283
int SDL_CDResume(SDL_CD *cdrom)
slouken@0
   284
{
slouken@0
   285
	CDstatus status;
slouken@0
   286
	int retval;
slouken@0
   287
slouken@0
   288
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   289
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   290
		return(CD_ERROR);
slouken@0
   291
	}
slouken@0
   292
slouken@0
   293
	status = SDL_CDcaps.Status(cdrom, NULL);
slouken@0
   294
	switch (status) {
slouken@0
   295
		case CD_PAUSED:
slouken@0
   296
			retval = SDL_CDcaps.Resume(cdrom);
slouken@0
   297
		default:
slouken@0
   298
			retval = 0;
slouken@0
   299
			break;
slouken@0
   300
	}
slouken@0
   301
	return(retval);
slouken@0
   302
}
slouken@0
   303
slouken@0
   304
int SDL_CDStop(SDL_CD *cdrom)
slouken@0
   305
{
slouken@0
   306
	CDstatus status;
slouken@0
   307
	int retval;
slouken@0
   308
slouken@0
   309
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   310
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   311
		return(CD_ERROR);
slouken@0
   312
	}
slouken@0
   313
slouken@0
   314
	status = SDL_CDcaps.Status(cdrom, NULL);
slouken@0
   315
	switch (status) {
slouken@0
   316
		case CD_PLAYING:
slouken@0
   317
		case CD_PAUSED:
slouken@0
   318
			retval = SDL_CDcaps.Stop(cdrom);
slouken@0
   319
		default:
slouken@0
   320
			retval = 0;
slouken@0
   321
			break;
slouken@0
   322
	}
slouken@0
   323
	return(retval);
slouken@0
   324
}
slouken@0
   325
slouken@0
   326
int SDL_CDEject(SDL_CD *cdrom)
slouken@0
   327
{
slouken@0
   328
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   329
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   330
		return(CD_ERROR);
slouken@0
   331
	}
slouken@0
   332
	return(SDL_CDcaps.Eject(cdrom));
slouken@0
   333
}
slouken@0
   334
slouken@0
   335
void SDL_CDClose(SDL_CD *cdrom)
slouken@0
   336
{
slouken@0
   337
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   338
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   339
		return;
slouken@0
   340
	}
slouken@0
   341
	SDL_CDcaps.Close(cdrom);
slouken@0
   342
	free(cdrom);
slouken@0
   343
	default_cdrom = NULL;
slouken@0
   344
}
slouken@0
   345
slouken@0
   346
void SDL_CDROMQuit(void)
slouken@0
   347
{
slouken@0
   348
	SDL_SYS_CDQuit();
slouken@0
   349
	SDL_cdinitted = 0;
slouken@0
   350
}