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