src/cdrom/freebsd/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 Aug 2001 22:03:11 +0000
changeset 153 2839f45bdba0
parent 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Fixed IDE and SCSI CD-ROM detection on BeOS (thanks Caz!)
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
/* Functions for system-level CD-ROM audio control */
slouken@0
    29
slouken@0
    30
#include <sys/types.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <sys/stat.h>
slouken@0
    33
#include <fcntl.h>
slouken@0
    34
#include <stdio.h>
slouken@0
    35
#include <string.h>
slouken@0
    36
#include <errno.h>
slouken@0
    37
#include <unistd.h>
slouken@0
    38
#include <sys/cdio.h>
slouken@0
    39
slouken@0
    40
#include "SDL_error.h"
slouken@0
    41
#include "SDL_cdrom.h"
slouken@0
    42
#include "SDL_syscdrom.h"
slouken@0
    43
slouken@0
    44
slouken@0
    45
/* The maximum number of CD-ROM drives we'll detect */
slouken@0
    46
#define MAX_DRIVES	16	
slouken@0
    47
slouken@0
    48
/* A list of available CD-ROM drives */
slouken@0
    49
static char *SDL_cdlist[MAX_DRIVES];
slouken@0
    50
static dev_t SDL_cdmode[MAX_DRIVES];
slouken@0
    51
slouken@0
    52
/* The system-dependent CD control functions */
slouken@0
    53
static const char *SDL_SYS_CDName(int drive);
slouken@0
    54
static int SDL_SYS_CDOpen(int drive);
slouken@0
    55
static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
slouken@0
    56
static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
slouken@0
    57
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
slouken@0
    58
static int SDL_SYS_CDPause(SDL_CD *cdrom);
slouken@0
    59
static int SDL_SYS_CDResume(SDL_CD *cdrom);
slouken@0
    60
static int SDL_SYS_CDStop(SDL_CD *cdrom);
slouken@0
    61
static int SDL_SYS_CDEject(SDL_CD *cdrom);
slouken@0
    62
static void SDL_SYS_CDClose(SDL_CD *cdrom);
slouken@0
    63
slouken@0
    64
/* Some ioctl() errno values which occur when the tray is empty */
slouken@0
    65
#define ERRNO_TRAYEMPTY(errno)	\
slouken@0
    66
	((errno == EIO) || (errno == ENOENT) || (errno == EINVAL))
slouken@0
    67
slouken@0
    68
/* Check a drive to see if it is a CD-ROM */
slouken@0
    69
static int CheckDrive(char *drive, struct stat *stbuf)
slouken@0
    70
{
slouken@0
    71
	int is_cd, cdfd;
slouken@0
    72
	struct ioc_read_subchannel info;
slouken@0
    73
slouken@0
    74
	/* If it doesn't exist, return -1 */
slouken@0
    75
	if ( stat(drive, stbuf) < 0 ) {
slouken@0
    76
		return(-1);
slouken@0
    77
	}
slouken@0
    78
slouken@0
    79
	/* If it does exist, verify that it's an available CD-ROM */
slouken@0
    80
	is_cd = 0;
slouken@0
    81
	if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) {
slouken@0
    82
		cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
slouken@0
    83
		if ( cdfd >= 0 ) {
slouken@0
    84
			info.address_format = CD_MSF_FORMAT;
slouken@0
    85
			info.data_format = CD_CURRENT_POSITION;
slouken@0
    86
			info.data_len = 0;
slouken@0
    87
			info.data = NULL;
slouken@0
    88
			/* Under Linux, EIO occurs when a disk is not present.
slouken@0
    89
			   This isn't 100% reliable, so we use the USE_MNTENT
slouken@0
    90
			   code above instead.
slouken@0
    91
			 */
slouken@0
    92
			if ( (ioctl(cdfd, CDIOCREADSUBCHANNEL, &info) == 0) ||
slouken@0
    93
						ERRNO_TRAYEMPTY(errno) ) {
slouken@0
    94
				is_cd = 1;
slouken@0
    95
			}
slouken@0
    96
			close(cdfd);
slouken@0
    97
		}
slouken@0
    98
	}
slouken@0
    99
	return(is_cd);
slouken@0
   100
}
slouken@0
   101
slouken@0
   102
/* Add a CD-ROM drive to our list of valid drives */
slouken@0
   103
static void AddDrive(char *drive, struct stat *stbuf)
slouken@0
   104
{
slouken@0
   105
	int i;
slouken@0
   106
slouken@0
   107
	if ( SDL_numcds < MAX_DRIVES ) {
slouken@0
   108
		/* Check to make sure it's not already in our list.
slouken@0
   109
	 	   This can happen when we see a drive via symbolic link.
slouken@0
   110
		 */
slouken@0
   111
		for ( i=0; i<SDL_numcds; ++i ) {
slouken@0
   112
			if ( stbuf->st_rdev == SDL_cdmode[i] ) {
slouken@0
   113
#ifdef DEBUG_CDROM
slouken@0
   114
  fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
slouken@0
   115
#endif
slouken@0
   116
				return;
slouken@0
   117
			}
slouken@0
   118
		}
slouken@0
   119
slouken@0
   120
		/* Add this drive to our list */
slouken@0
   121
		i = SDL_numcds;
slouken@0
   122
		SDL_cdlist[i] = (char *)malloc(strlen(drive)+1);
slouken@0
   123
		if ( SDL_cdlist[i] == NULL ) {
slouken@0
   124
			SDL_OutOfMemory();
slouken@0
   125
			return;
slouken@0
   126
		}
slouken@0
   127
		strcpy(SDL_cdlist[i], drive);
slouken@0
   128
		SDL_cdmode[i] = stbuf->st_rdev;
slouken@0
   129
		++SDL_numcds;
slouken@0
   130
#ifdef DEBUG_CDROM
slouken@0
   131
  fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
slouken@0
   132
#endif
slouken@0
   133
	}
slouken@0
   134
}
slouken@0
   135
slouken@0
   136
int  SDL_SYS_CDInit(void)
slouken@0
   137
{
slouken@0
   138
	/* checklist: /dev/cdrom,/dev/cd?c /dev/acd?c
slouken@0
   139
			/dev/matcd?c /dev/mcd?c /dev/scd?c */
slouken@0
   140
	static char *checklist[] = {
slouken@0
   141
	"cdrom", "?0 cd?", "?0 acd?", "?0 matcd?", "?0 mcd?", "?0 scd?",NULL
slouken@0
   142
	};
slouken@0
   143
	char *SDLcdrom;
slouken@0
   144
	int i, j, exists;
slouken@0
   145
	char drive[32];
slouken@0
   146
	struct stat stbuf;
slouken@0
   147
slouken@0
   148
	/* Fill in our driver capabilities */
slouken@0
   149
	SDL_CDcaps.Name = SDL_SYS_CDName;
slouken@0
   150
	SDL_CDcaps.Open = SDL_SYS_CDOpen;
slouken@0
   151
	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
slouken@0
   152
	SDL_CDcaps.Status = SDL_SYS_CDStatus;
slouken@0
   153
	SDL_CDcaps.Play = SDL_SYS_CDPlay;
slouken@0
   154
	SDL_CDcaps.Pause = SDL_SYS_CDPause;
slouken@0
   155
	SDL_CDcaps.Resume = SDL_SYS_CDResume;
slouken@0
   156
	SDL_CDcaps.Stop = SDL_SYS_CDStop;
slouken@0
   157
	SDL_CDcaps.Eject = SDL_SYS_CDEject;
slouken@0
   158
	SDL_CDcaps.Close = SDL_SYS_CDClose;
slouken@0
   159
slouken@0
   160
	/* Look in the environment for our CD-ROM drive list */
slouken@0
   161
	SDLcdrom = getenv("SDL_CDROM");	/* ':' separated list of devices */
slouken@0
   162
	if ( SDLcdrom != NULL ) {
slouken@0
   163
		char *cdpath, *delim;
slouken@0
   164
		cdpath = malloc(strlen(SDLcdrom)+1);
slouken@0
   165
		if ( cdpath != NULL ) {
slouken@0
   166
			strcpy(cdpath, SDLcdrom);
slouken@0
   167
			SDLcdrom = cdpath;
slouken@0
   168
			do {
slouken@0
   169
				delim = strchr(SDLcdrom, ':');
slouken@0
   170
				if ( delim ) {
slouken@0
   171
					*delim++ = '\0';
slouken@0
   172
				}
slouken@0
   173
				if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
slouken@0
   174
					AddDrive(SDLcdrom, &stbuf);
slouken@0
   175
				}
slouken@0
   176
				if ( delim ) {
slouken@0
   177
					SDLcdrom = delim;
slouken@0
   178
				} else {
slouken@0
   179
					SDLcdrom = NULL;
slouken@0
   180
				}
slouken@0
   181
			} while ( SDLcdrom );
slouken@0
   182
			free(cdpath);
slouken@0
   183
		}
slouken@0
   184
slouken@0
   185
		/* If we found our drives, there's nothing left to do */
slouken@0
   186
		if ( SDL_numcds > 0 ) {
slouken@0
   187
			return(0);
slouken@0
   188
		}
slouken@0
   189
	}
slouken@0
   190
slouken@0
   191
	/* Scan the system for CD-ROM drives */
slouken@0
   192
	for ( i=0; checklist[i]; ++i ) {
slouken@0
   193
		if ( checklist[i][0] == '?' ) {
slouken@0
   194
			char *insert;
slouken@0
   195
			exists = 1;
slouken@0
   196
			for ( j=checklist[i][1]; exists; ++j ) {
slouken@0
   197
				sprintf(drive, "/dev/%sc", &checklist[i][3]);
slouken@0
   198
				insert = strchr(drive, '?');
slouken@0
   199
				if ( insert != NULL ) {
slouken@0
   200
					*insert = j;
slouken@0
   201
				}
slouken@0
   202
				switch (CheckDrive(drive, &stbuf)) {
slouken@0
   203
					/* Drive exists and is a CD-ROM */
slouken@0
   204
					case 1:
slouken@0
   205
						AddDrive(drive, &stbuf);
slouken@0
   206
						break;
slouken@0
   207
					/* Drive exists, but isn't a CD-ROM */
slouken@0
   208
					case 0:
slouken@0
   209
						break;
slouken@0
   210
					/* Drive doesn't exist */
slouken@0
   211
					case -1:
slouken@0
   212
						exists = 0;
slouken@0
   213
						break;
slouken@0
   214
				}
slouken@0
   215
			}
slouken@0
   216
		} else {
slouken@0
   217
			sprintf(drive, "/dev/%s", checklist[i]);
slouken@0
   218
			if ( CheckDrive(drive, &stbuf) > 0 ) {
slouken@0
   219
				AddDrive(drive, &stbuf);
slouken@0
   220
			}
slouken@0
   221
		}
slouken@0
   222
	}
slouken@0
   223
	return(0);
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
/* General ioctl() CD-ROM command function */
slouken@0
   227
static int SDL_SYS_CDioctl(int id, int command, void *arg)
slouken@0
   228
{
slouken@0
   229
	int retval;
slouken@0
   230
slouken@0
   231
	retval = ioctl(id, command, arg);
slouken@0
   232
	if ( retval < 0 ) {
slouken@0
   233
		SDL_SetError("ioctl() error: %s", strerror(errno));
slouken@0
   234
	}
slouken@0
   235
	return(retval);
slouken@0
   236
}
slouken@0
   237
slouken@0
   238
static const char *SDL_SYS_CDName(int drive)
slouken@0
   239
{
slouken@0
   240
	return(SDL_cdlist[drive]);
slouken@0
   241
}
slouken@0
   242
slouken@0
   243
static int SDL_SYS_CDOpen(int drive)
slouken@0
   244
{
slouken@0
   245
	return(open(SDL_cdlist[drive], (O_RDONLY|O_EXCL|O_NONBLOCK), 0));
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
slouken@0
   249
{
slouken@0
   250
	struct ioc_toc_header toc;
slouken@0
   251
	int i, okay;
slouken@0
   252
	struct ioc_read_toc_entry entry;
slouken@0
   253
	struct cd_toc_entry data;
slouken@0
   254
slouken@0
   255
	okay = 0;
slouken@0
   256
	if ( SDL_SYS_CDioctl(cdrom->id, CDIOREADTOCHEADER, &toc) == 0 ) {
slouken@0
   257
		cdrom->numtracks = toc.ending_track-toc.starting_track+1;
slouken@0
   258
		if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
slouken@0
   259
			cdrom->numtracks = SDL_MAX_TRACKS;
slouken@0
   260
		}
slouken@0
   261
		/* Read all the track TOC entries */
slouken@0
   262
		for ( i=0; i<=cdrom->numtracks; ++i ) {
slouken@0
   263
			if ( i == cdrom->numtracks ) {
slouken@0
   264
				cdrom->track[i].id = 0xAA; /* CDROM_LEADOUT */
slouken@0
   265
			} else {
slouken@0
   266
				cdrom->track[i].id = toc.starting_track+i;
slouken@0
   267
			}
slouken@0
   268
			entry.starting_track = cdrom->track[i].id;
slouken@0
   269
			entry.address_format = CD_MSF_FORMAT;
slouken@0
   270
			entry.data_len = sizeof(data);
slouken@0
   271
			entry.data = &data;
slouken@0
   272
			if ( SDL_SYS_CDioctl(cdrom->id, CDIOREADTOCENTRYS,
slouken@0
   273
								&entry) < 0 ) {
slouken@0
   274
				break;
slouken@0
   275
			} else {
slouken@0
   276
				cdrom->track[i].type = data.control;
slouken@0
   277
				cdrom->track[i].offset = MSF_TO_FRAMES(
slouken@0
   278
						data.addr.msf.minute,
slouken@0
   279
						data.addr.msf.second,
slouken@0
   280
						data.addr.msf.frame);
slouken@0
   281
				cdrom->track[i].length = 0;
slouken@0
   282
				if ( i > 0 ) {
slouken@0
   283
					cdrom->track[i-1].length =
slouken@0
   284
						cdrom->track[i].offset-
slouken@0
   285
						cdrom->track[i-1].offset;
slouken@0
   286
				}
slouken@0
   287
			}
slouken@0
   288
		}
slouken@0
   289
		if ( i == (cdrom->numtracks+1) ) {
slouken@0
   290
			okay = 1;
slouken@0
   291
		}
slouken@0
   292
	}
slouken@0
   293
	return(okay ? 0 : -1);
slouken@0
   294
}
slouken@0
   295
slouken@0
   296
/* Get CD-ROM status */
slouken@0
   297
static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
slouken@0
   298
{
slouken@0
   299
	CDstatus status;
slouken@0
   300
	struct ioc_toc_header toc;
slouken@0
   301
	struct ioc_read_subchannel info;
slouken@0
   302
	struct cd_sub_channel_info data;
slouken@0
   303
slouken@0
   304
	info.address_format = CD_MSF_FORMAT;
slouken@0
   305
	info.data_format = CD_CURRENT_POSITION;
slouken@0
   306
	info.track = 0;
slouken@0
   307
	info.data_len = sizeof(data);
slouken@0
   308
	info.data = &data;
slouken@0
   309
	if ( ioctl(cdrom->id, CDIOCREADSUBCHANNEL, &info) < 0 ) {
slouken@0
   310
		if ( ERRNO_TRAYEMPTY(errno) ) {
slouken@0
   311
			status = CD_TRAYEMPTY;
slouken@0
   312
		} else {
slouken@0
   313
			status = CD_ERROR;
slouken@0
   314
		}
slouken@0
   315
	} else {
slouken@0
   316
		switch (data.header.audio_status) {
slouken@0
   317
			case CD_AS_AUDIO_INVALID:
slouken@0
   318
			case CD_AS_NO_STATUS:
slouken@0
   319
				/* Try to determine if there's a CD available */
slouken@0
   320
				if (ioctl(cdrom->id,CDIOREADTOCHEADER,&toc)==0)
slouken@0
   321
					status = CD_STOPPED;
slouken@0
   322
				else
slouken@0
   323
					status = CD_TRAYEMPTY;
slouken@0
   324
				break;
slouken@0
   325
			case CD_AS_PLAY_COMPLETED:
slouken@0
   326
				status = CD_STOPPED;
slouken@0
   327
				break;
slouken@0
   328
			case CD_AS_PLAY_IN_PROGRESS:
slouken@0
   329
				status = CD_PLAYING;
slouken@0
   330
				break;
slouken@0
   331
			case CD_AS_PLAY_PAUSED:
slouken@0
   332
				status = CD_PAUSED;
slouken@0
   333
				break;
slouken@0
   334
			default:
slouken@0
   335
				status = CD_ERROR;
slouken@0
   336
				break;
slouken@0
   337
		}
slouken@0
   338
	}
slouken@0
   339
	if ( position ) {
slouken@0
   340
		if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
slouken@0
   341
			*position = MSF_TO_FRAMES(
slouken@0
   342
					data.what.position.absaddr.msf.minute,
slouken@0
   343
					data.what.position.absaddr.msf.second,
slouken@0
   344
					data.what.position.absaddr.msf.frame);
slouken@0
   345
		} else {
slouken@0
   346
			*position = 0;
slouken@0
   347
		}
slouken@0
   348
	}
slouken@0
   349
	return(status);
slouken@0
   350
}
slouken@0
   351
slouken@0
   352
/* Start play */
slouken@0
   353
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
slouken@0
   354
{
slouken@0
   355
	struct ioc_play_msf playtime;
slouken@0
   356
slouken@0
   357
	FRAMES_TO_MSF(start,
slouken@0
   358
		&playtime.start_m, &playtime.start_s, &playtime.start_f);
slouken@0
   359
	FRAMES_TO_MSF(start+length,
slouken@0
   360
		&playtime.end_m, &playtime.end_s, &playtime.end_f);
slouken@0
   361
#ifdef DEBUG_CDROM
slouken@0
   362
  fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
slouken@0
   363
	playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
slouken@0
   364
	playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
slouken@0
   365
#endif
slouken@0
   366
	ioctl(cdrom->id, CDIOCSTART, 0);
slouken@0
   367
	return(SDL_SYS_CDioctl(cdrom->id, CDIOCPLAYMSF, &playtime));
slouken@0
   368
}
slouken@0
   369
slouken@0
   370
/* Pause play */
slouken@0
   371
static int SDL_SYS_CDPause(SDL_CD *cdrom)
slouken@0
   372
{
slouken@0
   373
	return(SDL_SYS_CDioctl(cdrom->id, CDIOCPAUSE, 0));
slouken@0
   374
}
slouken@0
   375
slouken@0
   376
/* Resume play */
slouken@0
   377
static int SDL_SYS_CDResume(SDL_CD *cdrom)
slouken@0
   378
{
slouken@0
   379
	return(SDL_SYS_CDioctl(cdrom->id, CDIOCRESUME, 0));
slouken@0
   380
}
slouken@0
   381
slouken@0
   382
/* Stop play */
slouken@0
   383
static int SDL_SYS_CDStop(SDL_CD *cdrom)
slouken@0
   384
{
slouken@0
   385
	return(SDL_SYS_CDioctl(cdrom->id, CDIOCSTOP, 0));
slouken@0
   386
}
slouken@0
   387
slouken@0
   388
/* Eject the CD-ROM */
slouken@0
   389
static int SDL_SYS_CDEject(SDL_CD *cdrom)
slouken@0
   390
{
slouken@0
   391
	return(SDL_SYS_CDioctl(cdrom->id, CDIOCEJECT, 0));
slouken@0
   392
}
slouken@0
   393
slouken@0
   394
/* Close the CD-ROM handle */
slouken@0
   395
static void SDL_SYS_CDClose(SDL_CD *cdrom)
slouken@0
   396
{
slouken@0
   397
	close(cdrom->id);
slouken@0
   398
}
slouken@0
   399
slouken@0
   400
void SDL_SYS_CDQuit(void)
slouken@0
   401
{
slouken@0
   402
	int i;
slouken@0
   403
slouken@0
   404
	if ( SDL_numcds > 0 ) {
slouken@0
   405
		for ( i=0; i<SDL_numcds; ++i ) {
slouken@0
   406
			free(SDL_cdlist[i]);
slouken@0
   407
		}
slouken@0
   408
		SDL_numcds = 0;
slouken@0
   409
	}
slouken@0
   410
}
slouken@0
   411