src/cdrom/SDL_cdrom.c
author Ryan C. Gordon
Wed, 29 Nov 2006 10:30:05 +0000
branchSDL-1.2
changeset 3900 ce3a2bd11305
parent 1402 d910939febfa
child 1662 782fd950bd46
child 1895 c121d94672cb
child 4159 a1b03ba2fcd0
permissions -rw-r--r--
Wrapped some macro params in parentheses for alloca wrappers.
Thansk, Suzuki Masahiro.
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@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This is the CD-audio control API for Simple DirectMedia Layer */
slouken@0
    25
slouken@0
    26
#include "SDL_cdrom.h"
slouken@0
    27
#include "SDL_syscdrom.h"
slouken@0
    28
slouken@1402
    29
#if !defined(__MACOS__)
slouken@0
    30
#define CLIP_FRAMES	10	/* Some CD-ROMs won't go all the way */
slouken@0
    31
#endif
slouken@0
    32
slouken@0
    33
static int SDL_cdinitted = 0;
slouken@0
    34
static SDL_CD *default_cdrom;
slouken@0
    35
slouken@0
    36
/* The system level CD-ROM control functions */
slouken@0
    37
struct CDcaps SDL_CDcaps = {
slouken@0
    38
	NULL,					/* Name */
slouken@0
    39
	NULL,					/* Open */
slouken@0
    40
	NULL,					/* GetTOC */
slouken@0
    41
	NULL,					/* Status */
slouken@0
    42
	NULL,					/* Play */
slouken@0
    43
	NULL,					/* Pause */
slouken@0
    44
	NULL,					/* Resume */
slouken@0
    45
	NULL,					/* Stop */
slouken@0
    46
	NULL,					/* Eject */
slouken@0
    47
	NULL,					/* Close */
slouken@0
    48
};
slouken@0
    49
int SDL_numcds;
slouken@0
    50
slouken@0
    51
int SDL_CDROMInit(void)
slouken@0
    52
{
slouken@0
    53
	int retval;
slouken@0
    54
slouken@0
    55
	SDL_numcds = 0;
slouken@0
    56
	retval = SDL_SYS_CDInit();
slouken@0
    57
	if ( retval == 0 ) {
slouken@0
    58
		SDL_cdinitted = 1;
slouken@0
    59
	}
slouken@0
    60
	default_cdrom = NULL;
slouken@0
    61
	return(retval);
slouken@0
    62
}
slouken@0
    63
slouken@0
    64
/* Check to see if the CD-ROM subsystem has been initialized */
slouken@0
    65
static int CheckInit(int check_cdrom, SDL_CD **cdrom)
slouken@0
    66
{
slouken@0
    67
	int okay;
slouken@0
    68
slouken@0
    69
	okay = SDL_cdinitted;
slouken@0
    70
	if ( check_cdrom && (*cdrom == NULL) ) {
slouken@0
    71
		*cdrom = default_cdrom;
slouken@0
    72
		if ( *cdrom == NULL ) {
slouken@0
    73
			SDL_SetError("CD-ROM not opened");
slouken@0
    74
			okay = 0;
slouken@0
    75
		}
slouken@0
    76
	}
slouken@0
    77
	if ( ! SDL_cdinitted ) {
slouken@0
    78
		SDL_SetError("CD-ROM subsystem not initialized");
slouken@0
    79
	}
slouken@0
    80
	return(okay);
slouken@0
    81
}
slouken@0
    82
slouken@0
    83
int SDL_CDNumDrives(void)
slouken@0
    84
{
slouken@0
    85
	if ( ! CheckInit(0, NULL) ) {
slouken@0
    86
		return(-1);
slouken@0
    87
	}
slouken@0
    88
	return(SDL_numcds);
slouken@0
    89
}
slouken@0
    90
slouken@0
    91
const char *SDL_CDName(int drive)
slouken@0
    92
{
slouken@0
    93
	if ( ! CheckInit(0, NULL) ) {
slouken@0
    94
		return(NULL);
slouken@0
    95
	}
slouken@0
    96
	if ( drive >= SDL_numcds ) {
slouken@0
    97
		SDL_SetError("Invalid CD-ROM drive index");
slouken@0
    98
		return(NULL);
slouken@0
    99
	}
slouken@0
   100
	if ( SDL_CDcaps.Name ) {
slouken@0
   101
		return(SDL_CDcaps.Name(drive));
slouken@0
   102
	} else {
slouken@0
   103
		return("");
slouken@0
   104
	}
slouken@0
   105
}
slouken@0
   106
slouken@0
   107
SDL_CD *SDL_CDOpen(int drive)
slouken@0
   108
{
slouken@0
   109
	struct SDL_CD *cdrom;
slouken@0
   110
slouken@0
   111
	if ( ! CheckInit(0, NULL) ) {
slouken@0
   112
		return(NULL);
slouken@0
   113
	}
slouken@0
   114
	if ( drive >= SDL_numcds ) {
slouken@0
   115
		SDL_SetError("Invalid CD-ROM drive index");
slouken@0
   116
		return(NULL);
slouken@0
   117
	}
slouken@1336
   118
	cdrom = (SDL_CD *)SDL_malloc(sizeof(*cdrom));
slouken@0
   119
	if ( cdrom == NULL ) {
slouken@0
   120
		SDL_OutOfMemory();
slouken@0
   121
		return(NULL);
slouken@0
   122
	}
slouken@1336
   123
	SDL_memset(cdrom, 0, sizeof(*cdrom));
slouken@0
   124
	cdrom->id = SDL_CDcaps.Open(drive);
slouken@0
   125
	if ( cdrom->id < 0 ) {
slouken@1336
   126
		SDL_free(cdrom);
slouken@0
   127
		return(NULL);
slouken@0
   128
	}
slouken@0
   129
	default_cdrom = cdrom;
slouken@0
   130
	return(cdrom);
slouken@0
   131
}
slouken@0
   132
slouken@0
   133
CDstatus SDL_CDStatus(SDL_CD *cdrom)
slouken@0
   134
{
slouken@0
   135
	CDstatus status;
slouken@0
   136
	int i;
slouken@0
   137
	Uint32 position;
slouken@0
   138
slouken@0
   139
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   140
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   141
		return(CD_ERROR);
slouken@0
   142
	}
slouken@0
   143
slouken@0
   144
	/* Get the current status of the drive */
slouken@0
   145
	cdrom->numtracks = 0;
slouken@0
   146
	cdrom->cur_track = 0;
slouken@0
   147
	cdrom->cur_frame = 0;
slouken@0
   148
	status = SDL_CDcaps.Status(cdrom, &i);
slouken@0
   149
	position = (Uint32)i;
slouken@0
   150
	cdrom->status = status;
slouken@0
   151
slouken@0
   152
	/* Get the table of contents, if there's a CD available */
slouken@0
   153
	if ( CD_INDRIVE(status) ) {
slouken@0
   154
		if ( SDL_CDcaps.GetTOC(cdrom) < 0 ) {
slouken@0
   155
			status = CD_ERROR;
slouken@0
   156
		}
slouken@0
   157
		/* If the drive is playing, get current play position */
slouken@0
   158
		if ( (status == CD_PLAYING) || (status == CD_PAUSED) ) {
slouken@0
   159
			for ( i=1; cdrom->track[i].offset <= position; ++i ) {
slouken@0
   160
				/* Keep looking */;
slouken@0
   161
			}
slouken@0
   162
#ifdef DEBUG_CDROM
slouken@0
   163
  fprintf(stderr, "Current position: %d, track = %d (offset is %d)\n",
slouken@0
   164
				position, i-1, cdrom->track[i-1].offset);
slouken@0
   165
#endif
slouken@0
   166
			cdrom->cur_track = i-1;
slouken@0
   167
			position -= cdrom->track[cdrom->cur_track].offset;
slouken@0
   168
			cdrom->cur_frame = position;
slouken@0
   169
		}
slouken@0
   170
	}
slouken@0
   171
	return(status);
slouken@0
   172
}
slouken@0
   173
slouken@0
   174
int SDL_CDPlayTracks(SDL_CD *cdrom,
slouken@0
   175
			int strack, int sframe, int ntracks, int nframes)
slouken@0
   176
{
slouken@0
   177
	int etrack, eframe;
slouken@0
   178
	int start, length;
slouken@0
   179
slouken@0
   180
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   181
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   182
		return(CD_ERROR);
slouken@0
   183
	}
slouken@0
   184
slouken@0
   185
	/* Determine the starting and ending tracks */
slouken@0
   186
	if ( (strack < 0) || (strack >= cdrom->numtracks) ) {
slouken@0
   187
		SDL_SetError("Invalid starting track");
slouken@0
   188
		return(CD_ERROR);
slouken@0
   189
	}
slouken@0
   190
	if ( ! ntracks && ! nframes ) {
slouken@0
   191
		etrack = cdrom->numtracks;
slouken@0
   192
		eframe = 0;
slouken@0
   193
	} else {
slouken@0
   194
		etrack = strack+ntracks;
slouken@0
   195
		if ( etrack == strack ) {
slouken@0
   196
			eframe = sframe + nframes;
slouken@0
   197
		} else {
slouken@0
   198
			eframe = nframes;
slouken@0
   199
		}
slouken@0
   200
	}
slouken@0
   201
	if ( etrack > cdrom->numtracks ) {
slouken@0
   202
		SDL_SetError("Invalid play length");
slouken@0
   203
		return(CD_ERROR);
slouken@0
   204
	}
slouken@0
   205
slouken@0
   206
	/* Skip data tracks and verify frame offsets */
slouken@0
   207
	while ( (strack <= etrack) &&
slouken@0
   208
			(cdrom->track[strack].type == SDL_DATA_TRACK) ) {
slouken@0
   209
		++strack;
slouken@0
   210
	}
slouken@0
   211
	if ( sframe >= (int)cdrom->track[strack].length ) {
slouken@0
   212
		SDL_SetError("Invalid starting frame for track %d", strack);
slouken@0
   213
		return(CD_ERROR);
slouken@0
   214
	}
slouken@0
   215
	while ( (etrack > strack) &&
slouken@0
   216
			(cdrom->track[etrack-1].type == SDL_DATA_TRACK) ) {
slouken@0
   217
		--etrack;
slouken@0
   218
	}
slouken@0
   219
	if ( eframe > (int)cdrom->track[etrack].length ) {
slouken@0
   220
		SDL_SetError("Invalid ending frame for track %d", etrack);
slouken@0
   221
		return(CD_ERROR);
slouken@0
   222
	}
slouken@0
   223
slouken@0
   224
	/* Determine start frame and play length */
slouken@0
   225
	start = (cdrom->track[strack].offset+sframe);
slouken@0
   226
	length = (cdrom->track[etrack].offset+eframe)-start;
slouken@0
   227
#ifdef CLIP_FRAMES
slouken@0
   228
	/* I've never seen this necessary, but xmcd does it.. */
slouken@0
   229
	length -= CLIP_FRAMES;	/* CLIP_FRAMES == 10 */
slouken@0
   230
#endif
slouken@0
   231
	if ( length < 0 ) {
slouken@0
   232
		return(0);
slouken@0
   233
	}
slouken@0
   234
slouken@0
   235
	/* Play! */
slouken@0
   236
#ifdef DEBUG_CDROM
slouken@0
   237
  fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
slouken@0
   238
#endif
slouken@0
   239
	return(SDL_CDcaps.Play(cdrom, start, length));
slouken@0
   240
}
slouken@0
   241
slouken@0
   242
int SDL_CDPlay(SDL_CD *cdrom, int sframe, int length)
slouken@0
   243
{
slouken@0
   244
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   245
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   246
		return(CD_ERROR);
slouken@0
   247
	}
slouken@0
   248
slouken@0
   249
	return(SDL_CDcaps.Play(cdrom, sframe, length));
slouken@0
   250
}
slouken@0
   251
slouken@0
   252
int SDL_CDPause(SDL_CD *cdrom)
slouken@0
   253
{
slouken@0
   254
	CDstatus status;
slouken@0
   255
	int retval;
slouken@0
   256
slouken@0
   257
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   258
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   259
		return(CD_ERROR);
slouken@0
   260
	}
slouken@0
   261
slouken@0
   262
	status = SDL_CDcaps.Status(cdrom, NULL);
slouken@0
   263
	switch (status) {
slouken@0
   264
		case CD_PLAYING:
slouken@0
   265
			retval = SDL_CDcaps.Pause(cdrom);
slouken@0
   266
			break;
slouken@0
   267
		default:
slouken@0
   268
			retval = 0;
slouken@0
   269
			break;
slouken@0
   270
	}
slouken@0
   271
	return(retval);
slouken@0
   272
}
slouken@0
   273
slouken@0
   274
int SDL_CDResume(SDL_CD *cdrom)
slouken@0
   275
{
slouken@0
   276
	CDstatus status;
slouken@0
   277
	int retval;
slouken@0
   278
slouken@0
   279
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   280
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   281
		return(CD_ERROR);
slouken@0
   282
	}
slouken@0
   283
slouken@0
   284
	status = SDL_CDcaps.Status(cdrom, NULL);
slouken@0
   285
	switch (status) {
slouken@0
   286
		case CD_PAUSED:
slouken@0
   287
			retval = SDL_CDcaps.Resume(cdrom);
slouken@0
   288
		default:
slouken@0
   289
			retval = 0;
slouken@0
   290
			break;
slouken@0
   291
	}
slouken@0
   292
	return(retval);
slouken@0
   293
}
slouken@0
   294
slouken@0
   295
int SDL_CDStop(SDL_CD *cdrom)
slouken@0
   296
{
slouken@0
   297
	CDstatus status;
slouken@0
   298
	int retval;
slouken@0
   299
slouken@0
   300
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   301
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   302
		return(CD_ERROR);
slouken@0
   303
	}
slouken@0
   304
slouken@0
   305
	status = SDL_CDcaps.Status(cdrom, NULL);
slouken@0
   306
	switch (status) {
slouken@0
   307
		case CD_PLAYING:
slouken@0
   308
		case CD_PAUSED:
slouken@0
   309
			retval = SDL_CDcaps.Stop(cdrom);
slouken@0
   310
		default:
slouken@0
   311
			retval = 0;
slouken@0
   312
			break;
slouken@0
   313
	}
slouken@0
   314
	return(retval);
slouken@0
   315
}
slouken@0
   316
slouken@0
   317
int SDL_CDEject(SDL_CD *cdrom)
slouken@0
   318
{
slouken@0
   319
	/* Check if the CD-ROM subsystem has been initialized */
slouken@0
   320
	if ( ! CheckInit(1, &cdrom) ) {
slouken@0
   321
		return(CD_ERROR);
slouken@0
   322
	}
slouken@0
   323
	return(SDL_CDcaps.Eject(cdrom));
slouken@0
   324
}
slouken@0
   325
slouken@0
   326
void SDL_CDClose(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;
slouken@0
   331
	}
slouken@0
   332
	SDL_CDcaps.Close(cdrom);
slouken@1336
   333
	SDL_free(cdrom);
slouken@0
   334
	default_cdrom = NULL;
slouken@0
   335
}
slouken@0
   336
slouken@0
   337
void SDL_CDROMQuit(void)
slouken@0
   338
{
slouken@0
   339
	SDL_SYS_CDQuit();
slouken@0
   340
	SDL_cdinitted = 0;
slouken@0
   341
}