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