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