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