src/cdrom/beos/SDL_syscdrom.cc
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 12:30:25 +0000
changeset 249 e3d0d44f6f2e
parent 153 2839f45bdba0
child 252 e8157fcb3114
permissions -rw-r--r--
*** empty log message ***
slouken@153
     1
/*
slouken@153
     2
    SDL - Simple DirectMedia Layer
slouken@153
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
slouken@153
     4
slouken@153
     5
    This library is free software; you can redistribute it and/or
slouken@153
     6
    modify it under the terms of the GNU Library General Public
slouken@153
     7
    License as published by the Free Software Foundation; either
slouken@153
     8
    version 2 of the License, or (at your option) any later version.
slouken@153
     9
slouken@153
    10
    This library is distributed in the hope that it will be useful,
slouken@153
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@153
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@153
    13
    Library General Public License for more details.
slouken@153
    14
slouken@153
    15
    You should have received a copy of the GNU Library General Public
slouken@153
    16
    License along with this library; if not, write to the Free
slouken@153
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@153
    18
slouken@153
    19
    Sam Lantinga
slouken@153
    20
    slouken@devolution.com
slouken@153
    21
*/
slouken@153
    22
slouken@153
    23
#ifdef SAVE_RCSID
slouken@153
    24
static char rcsid =
slouken@153
    25
 "@(#) $Id$";
slouken@153
    26
#endif
slouken@153
    27
slouken@153
    28
/* Functions for system-level CD-ROM audio control on BeOS
slouken@153
    29
   (not completely implemented yet)
slouken@153
    30
 */
slouken@153
    31
slouken@153
    32
#include <sys/types.h>
slouken@153
    33
#include <stdlib.h>
slouken@153
    34
#include <sys/stat.h>
slouken@153
    35
#include <stdio.h>
slouken@153
    36
#include <string.h>
slouken@153
    37
#include <unistd.h>
slouken@153
    38
slouken@153
    39
#include <scsi.h>
slouken@153
    40
#include <Directory.h>
slouken@153
    41
#include <Entry.h>
slouken@153
    42
#include <Path.h>
slouken@153
    43
slouken@153
    44
#include "SDL_error.h"
slouken@153
    45
#include "SDL_cdrom.h"
slouken@153
    46
extern "C" {
slouken@153
    47
#include "SDL_syscdrom.h"
slouken@153
    48
}
slouken@153
    49
slouken@153
    50
/* Constants to help us get at the SCSI table-of-contents info */
slouken@153
    51
#define CD_NUMTRACKS(toc)	toc.toc_data[3]
slouken@153
    52
#define CD_TRACK(toc, track)	(&toc.toc_data[6+(track)*8])
slouken@153
    53
#define CD_TRACK_N(toc, track)	CD_TRACK(toc, track)[0]
slouken@153
    54
#define CD_TRACK_M(toc, track)	CD_TRACK(toc, track)[3]
slouken@153
    55
#define CD_TRACK_S(toc, track)	CD_TRACK(toc, track)[4]
slouken@153
    56
#define CD_TRACK_F(toc, track)	CD_TRACK(toc, track)[5]
slouken@153
    57
slouken@153
    58
/* Constants to help us get at the SCSI position info */
slouken@153
    59
#define POS_TRACK(pos)	pos.position[6]
slouken@153
    60
#define POS_ABS_M(pos)	pos.position[9]
slouken@153
    61
#define POS_ABS_S(pos)	pos.position[10]
slouken@153
    62
#define POS_ABS_F(pos)	pos.position[11]
slouken@153
    63
#define POS_REL_M(pos)	pos.position[13]
slouken@153
    64
#define POS_REL_S(pos)	pos.position[14]
slouken@153
    65
#define POS_REL_F(pos)	pos.position[15]
slouken@153
    66
slouken@153
    67
/* The maximum number of CD-ROM drives we'll detect */
slouken@153
    68
#define MAX_DRIVES	16	
slouken@153
    69
slouken@153
    70
/* A list of available CD-ROM drives */
slouken@153
    71
static char *SDL_cdlist[MAX_DRIVES];
slouken@153
    72
slouken@153
    73
/* The system-dependent CD control functions */
slouken@153
    74
static const char *SDL_SYS_CDName(int drive);
slouken@153
    75
static int SDL_SYS_CDOpen(int drive);
slouken@153
    76
static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
slouken@153
    77
static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
slouken@153
    78
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
slouken@153
    79
static int SDL_SYS_CDPause(SDL_CD *cdrom);
slouken@153
    80
static int SDL_SYS_CDResume(SDL_CD *cdrom);
slouken@153
    81
static int SDL_SYS_CDStop(SDL_CD *cdrom);
slouken@153
    82
static int SDL_SYS_CDEject(SDL_CD *cdrom);
slouken@153
    83
static void SDL_SYS_CDClose(SDL_CD *cdrom);
slouken@153
    84
int try_dir(const char *directory);
slouken@153
    85
slouken@153
    86
slouken@153
    87
/* Check a drive to see if it is a CD-ROM */
slouken@153
    88
static int CheckDrive(char *drive)
slouken@153
    89
{
slouken@153
    90
	struct stat stbuf;
slouken@153
    91
	int is_cd, cdfd;
slouken@153
    92
	device_geometry info;
slouken@153
    93
slouken@153
    94
	/* If it doesn't exist, return -1 */
slouken@153
    95
	if ( stat(drive, &stbuf) < 0 ) {
slouken@153
    96
		return(-1);
slouken@153
    97
	}
slouken@153
    98
slouken@153
    99
	/* If it does exist, verify that it's an available CD-ROM */
slouken@153
   100
	is_cd = 0;
slouken@153
   101
	cdfd = open(drive, 0);
slouken@153
   102
	if ( cdfd >= 0 ) {
slouken@153
   103
		if ( ioctl(cdfd, B_GET_GEOMETRY, &info) == B_NO_ERROR ) {
slouken@153
   104
			if ( info.device_type == B_CD ) {
slouken@153
   105
				is_cd = 1;
slouken@153
   106
			}
slouken@153
   107
		}
slouken@153
   108
		close(cdfd);
slouken@153
   109
	} else {
slouken@153
   110
		/* This can happen when the drive is open .. (?) */;
slouken@153
   111
		is_cd = 1;
slouken@153
   112
	}
slouken@153
   113
	return(is_cd);
slouken@153
   114
}
slouken@153
   115
slouken@153
   116
/* Add a CD-ROM drive to our list of valid drives */
slouken@153
   117
static void AddDrive(char *drive)
slouken@153
   118
{
slouken@153
   119
	int i;
slouken@153
   120
slouken@153
   121
	if ( SDL_numcds < MAX_DRIVES ) {
slouken@153
   122
		/* Add this drive to our list */
slouken@153
   123
		i = SDL_numcds;
slouken@153
   124
		SDL_cdlist[i] = (char *)malloc(strlen(drive)+1);
slouken@153
   125
		if ( SDL_cdlist[i] == NULL ) {
slouken@153
   126
			SDL_OutOfMemory();
slouken@153
   127
			return;
slouken@153
   128
		}
slouken@153
   129
		strcpy(SDL_cdlist[i], drive);
slouken@153
   130
		++SDL_numcds;
slouken@153
   131
#ifdef CDROM_DEBUG
slouken@153
   132
  fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
slouken@153
   133
#endif
slouken@153
   134
	}
slouken@153
   135
}
slouken@153
   136
slouken@153
   137
/* IDE bus scanning magic */
slouken@153
   138
enum {
slouken@153
   139
	IDE_GET_DEVICES_INFO = B_DEVICE_OP_CODES_END + 50,
slouken@153
   140
};
slouken@153
   141
struct ide_ctrl_info {
slouken@153
   142
	bool	ide_0_present;
slouken@153
   143
	bool	ide_0_master_present;
slouken@153
   144
	bool	ide_0_slave_present;
slouken@153
   145
	int	ide_0_master_type;
slouken@153
   146
	int	ide_0_slave_type;
slouken@153
   147
	bool	ide_1_present;
slouken@153
   148
	bool	ide_1_master_present;
slouken@153
   149
	bool	ide_1_slave_present;
slouken@153
   150
	int	ide_1_master_type;
slouken@153
   151
	int	ide_1_slave_type;
slouken@153
   152
};
slouken@153
   153
slouken@153
   154
int  SDL_SYS_CDInit(void)
slouken@153
   155
{
slouken@153
   156
	char *SDLcdrom;
slouken@153
   157
	int raw_fd;
slouken@153
   158
	struct ide_ctrl_info info;
slouken@153
   159
slouken@153
   160
	/* Fill in our driver capabilities */
slouken@153
   161
	SDL_CDcaps.Name = SDL_SYS_CDName;
slouken@153
   162
	SDL_CDcaps.Open = SDL_SYS_CDOpen;
slouken@153
   163
	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
slouken@153
   164
	SDL_CDcaps.Status = SDL_SYS_CDStatus;
slouken@153
   165
	SDL_CDcaps.Play = SDL_SYS_CDPlay;
slouken@153
   166
	SDL_CDcaps.Pause = SDL_SYS_CDPause;
slouken@153
   167
	SDL_CDcaps.Resume = SDL_SYS_CDResume;
slouken@153
   168
	SDL_CDcaps.Stop = SDL_SYS_CDStop;
slouken@153
   169
	SDL_CDcaps.Eject = SDL_SYS_CDEject;
slouken@153
   170
	SDL_CDcaps.Close = SDL_SYS_CDClose;
slouken@153
   171
slouken@153
   172
	/* Look in the environment for our CD-ROM drive list */
slouken@153
   173
	SDLcdrom = getenv("SDL_CDROM");	/* ':' separated list of devices */
slouken@153
   174
	if ( SDLcdrom != NULL ) {
slouken@153
   175
		char *cdpath, *delim;
slouken@153
   176
		cdpath = (char *)malloc(strlen(SDLcdrom)+1);
slouken@153
   177
		if ( cdpath != NULL ) {
slouken@153
   178
			strcpy(cdpath, SDLcdrom);
slouken@153
   179
			SDLcdrom = cdpath;
slouken@153
   180
			do {
slouken@153
   181
				delim = strchr(SDLcdrom, ':');
slouken@153
   182
				if ( delim ) {
slouken@153
   183
					*delim++ = '\0';
slouken@153
   184
				}
slouken@153
   185
				if ( CheckDrive(SDLcdrom) > 0 ) {
slouken@153
   186
					AddDrive(SDLcdrom);
slouken@153
   187
				}
slouken@153
   188
				if ( delim ) {
slouken@153
   189
					SDLcdrom = delim;
slouken@153
   190
				} else {
slouken@153
   191
					SDLcdrom = NULL;
slouken@153
   192
				}
slouken@153
   193
			} while ( SDLcdrom );
slouken@153
   194
			free(cdpath);
slouken@153
   195
		}
slouken@153
   196
slouken@153
   197
		/* If we found our drives, there's nothing left to do */
slouken@153
   198
		if ( SDL_numcds > 0 ) {
slouken@153
   199
			return(0);
slouken@153
   200
		}
slouken@153
   201
	}
slouken@153
   202
	
slouken@153
   203
	/* Scan the system for CD-ROM drives */
slouken@153
   204
	try_dir("/dev/disk");
slouken@153
   205
	return 0;
slouken@153
   206
}
slouken@153
   207
slouken@153
   208
slouken@153
   209
int try_dir(const char *directory)
slouken@153
   210
{ 
slouken@153
   211
	BDirectory dir; 
slouken@153
   212
	dir.SetTo(directory); 
slouken@153
   213
	if(dir.InitCheck() != B_NO_ERROR) { 
slouken@153
   214
		return false; 
slouken@153
   215
	} 
slouken@153
   216
	dir.Rewind(); 
slouken@153
   217
	BEntry entry; 
slouken@153
   218
	while(dir.GetNextEntry(&entry) >= 0) { 
slouken@153
   219
		BPath path; 
slouken@153
   220
		const char *name; 
slouken@153
   221
		entry_ref e; 
slouken@153
   222
		
slouken@153
   223
		if(entry.GetPath(&path) != B_NO_ERROR) 
slouken@153
   224
			continue; 
slouken@153
   225
		name = path.Path(); 
slouken@153
   226
		
slouken@153
   227
		if(entry.GetRef(&e) != B_NO_ERROR) 
slouken@153
   228
			continue; 
slouken@153
   229
slouken@153
   230
		if(entry.IsDirectory()) { 
slouken@153
   231
			if(strcmp(e.name, "floppy") == 0) 
slouken@153
   232
				continue; /* ignore floppy (it is not silent)  */
slouken@153
   233
			int devfd = try_dir(name);
slouken@153
   234
			if(devfd >= 0)
slouken@153
   235
				return devfd;
slouken@153
   236
		} 
slouken@153
   237
		else { 
slouken@153
   238
			int devfd; 
slouken@153
   239
			device_geometry g; 
slouken@153
   240
slouken@153
   241
			if(strcmp(e.name, "raw") != 0) 
slouken@153
   242
				continue; /* ignore partitions */
slouken@153
   243
slouken@153
   244
			devfd = open(name, O_RDONLY); 
slouken@153
   245
			if(devfd < 0) 
slouken@153
   246
				continue; 
slouken@153
   247
slouken@153
   248
			if(ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0) {
slouken@153
   249
				if(g.device_type == B_CD)
slouken@153
   250
				{
slouken@153
   251
				AddDrive(strdup(name));
slouken@153
   252
				}
slouken@153
   253
			}
slouken@153
   254
			close(devfd);
slouken@153
   255
		} 
slouken@153
   256
	}
slouken@153
   257
	return B_ERROR;
slouken@153
   258
}
slouken@153
   259
slouken@153
   260
slouken@153
   261
/* General ioctl() CD-ROM command function */
slouken@153
   262
static int SDL_SYS_CDioctl(int index, int command, void *arg)
slouken@153
   263
{
slouken@153
   264
	int okay;
slouken@153
   265
	int fd;
slouken@153
   266
slouken@153
   267
	okay = 0;
slouken@153
   268
	fd = open(SDL_cdlist[index], 0);
slouken@153
   269
	if ( fd >= 0 ) {
slouken@153
   270
		if ( ioctl(fd, command, arg) == B_NO_ERROR ) {
slouken@153
   271
			okay = 1;
slouken@153
   272
		}
slouken@153
   273
		close(fd);
slouken@153
   274
	}
slouken@153
   275
	return(okay ? 0 : -1);
slouken@153
   276
}
slouken@153
   277
slouken@153
   278
static const char *SDL_SYS_CDName(int drive)
slouken@153
   279
{
slouken@153
   280
	return(SDL_cdlist[drive]);
slouken@153
   281
} 
slouken@153
   282
slouken@153
   283
static int SDL_SYS_CDOpen(int drive)
slouken@153
   284
{
slouken@153
   285
	return(drive);
slouken@153
   286
}
slouken@153
   287
slouken@153
   288
static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
slouken@153
   289
{
slouken@153
   290
	int i;
slouken@153
   291
	scsi_toc toc;
slouken@153
   292
slouken@153
   293
	if ( SDL_SYS_CDioctl(cdrom->id, B_SCSI_GET_TOC, &toc) == 0 ) {
slouken@153
   294
		cdrom->numtracks = CD_NUMTRACKS(toc);
slouken@153
   295
		if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
slouken@153
   296
			cdrom->numtracks = SDL_MAX_TRACKS;
slouken@153
   297
		}
slouken@153
   298
		for ( i=0; i<=cdrom->numtracks; ++i ) {
slouken@153
   299
			cdrom->track[i].id = CD_TRACK_N(toc, i);
slouken@153
   300
			/* FIXME:  How do we tell on BeOS? */
slouken@153
   301
			cdrom->track[i].type = SDL_AUDIO_TRACK;
slouken@153
   302
			cdrom->track[i].offset = MSF_TO_FRAMES(
slouken@153
   303
							CD_TRACK_M(toc, i),
slouken@153
   304
							CD_TRACK_S(toc, i),
slouken@153
   305
							CD_TRACK_F(toc, i));
slouken@153
   306
			cdrom->track[i].length = 0;
slouken@153
   307
			if ( i > 0 ) {
slouken@153
   308
				cdrom->track[i-1].length =
slouken@153
   309
						cdrom->track[i].offset-
slouken@153
   310
						cdrom->track[i-1].offset;
slouken@153
   311
			}
slouken@153
   312
		}
slouken@153
   313
		return(0);
slouken@153
   314
	} else {
slouken@153
   315
		return(-1);
slouken@153
   316
	}
slouken@153
   317
}
slouken@153
   318
slouken@153
   319
/* Get CD-ROM status */
slouken@153
   320
static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
slouken@153
   321
{
slouken@153
   322
	CDstatus status;
slouken@153
   323
	int fd;
slouken@153
   324
	int cur_frame;
slouken@153
   325
	scsi_position pos;
slouken@153
   326
slouken@153
   327
	fd = open(SDL_cdlist[cdrom->id], 0);
slouken@153
   328
	cur_frame = 0;
slouken@153
   329
	if ( fd >= 0 ) {
slouken@153
   330
		if ( ioctl(fd, B_SCSI_GET_POSITION, &pos) == B_NO_ERROR ) {
slouken@153
   331
			cur_frame = MSF_TO_FRAMES(
slouken@153
   332
				POS_ABS_M(pos), POS_ABS_S(pos), POS_ABS_F(pos));
slouken@153
   333
		}
slouken@153
   334
		if ( ! pos.position[1] || (pos.position[1] >= 0x13) ||
slouken@153
   335
			((pos.position[1] == 0x12) && (!pos.position[6])) ) {
slouken@153
   336
			status = CD_STOPPED;
slouken@153
   337
		} else
slouken@153
   338
		if ( pos.position[1] == 0x11 ) {
slouken@153
   339
			status = CD_PLAYING;
slouken@153
   340
		} else {
slouken@153
   341
			status = CD_PAUSED;
slouken@153
   342
		}
slouken@153
   343
		close(fd);
slouken@153
   344
	} else {
slouken@153
   345
		status = CD_TRAYEMPTY;
slouken@153
   346
	}
slouken@153
   347
	if ( position ) {
slouken@153
   348
		*position = cur_frame;
slouken@153
   349
	}
slouken@153
   350
	return(status);
slouken@153
   351
}
slouken@153
   352
slouken@153
   353
/* Start play */
slouken@153
   354
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
slouken@153
   355
{
slouken@153
   356
	int okay;
slouken@153
   357
	int fd;
slouken@153
   358
	scsi_play_position pos;
slouken@153
   359
slouken@153
   360
	okay = 0;
slouken@153
   361
	fd = open(SDL_cdlist[cdrom->id], 0);
slouken@153
   362
	if ( fd >= 0 ) {
slouken@153
   363
		FRAMES_TO_MSF(start, &pos.start_m, &pos.start_s, &pos.start_f);
slouken@153
   364
		FRAMES_TO_MSF(start+length, &pos.end_m, &pos.end_s, &pos.end_f);
slouken@153
   365
		if ( ioctl(fd, B_SCSI_PLAY_POSITION, &pos) == B_NO_ERROR ) {
slouken@153
   366
			okay = 1;
slouken@153
   367
		}
slouken@153
   368
		close(fd);
slouken@153
   369
	}
slouken@153
   370
	return(okay ? 0 : -1);
slouken@153
   371
}
slouken@153
   372
slouken@153
   373
/* Pause play */
slouken@153
   374
static int SDL_SYS_CDPause(SDL_CD *cdrom)
slouken@153
   375
{
slouken@153
   376
	return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_PAUSE_AUDIO, 0));
slouken@153
   377
}
slouken@153
   378
slouken@153
   379
/* Resume play */
slouken@153
   380
static int SDL_SYS_CDResume(SDL_CD *cdrom)
slouken@153
   381
{
slouken@153
   382
	return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_RESUME_AUDIO, 0));
slouken@153
   383
}
slouken@153
   384
slouken@153
   385
/* Stop play */
slouken@153
   386
static int SDL_SYS_CDStop(SDL_CD *cdrom)
slouken@153
   387
{
slouken@153
   388
	return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_STOP_AUDIO, 0));
slouken@153
   389
}
slouken@153
   390
slouken@153
   391
/* Eject the CD-ROM */
slouken@153
   392
static int SDL_SYS_CDEject(SDL_CD *cdrom)
slouken@153
   393
{
slouken@153
   394
	return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_EJECT, 0));
slouken@153
   395
}
slouken@153
   396
slouken@153
   397
/* Close the CD-ROM handle */
slouken@153
   398
static void SDL_SYS_CDClose(SDL_CD *cdrom)
slouken@153
   399
{
slouken@153
   400
	close(cdrom->id);
slouken@153
   401
}
slouken@153
   402
slouken@153
   403
void SDL_SYS_CDQuit(void)
slouken@153
   404
{
slouken@153
   405
	int i;
slouken@153
   406
slouken@153
   407
	if ( SDL_numcds > 0 ) {
slouken@153
   408
		for ( i=0; i<SDL_numcds; ++i ) {
slouken@153
   409
			free(SDL_cdlist[i]);
slouken@153
   410
		}
slouken@153
   411
		SDL_numcds = 0;
slouken@153
   412
	}
slouken@153
   413
}
slouken@153
   414