src/cdrom/openbsd/SDL_syscdrom.c
author Sam Lantinga
Fri, 16 Oct 2009 05:05:21 +0000
branchSDL-1.2
changeset 4342 f2800721813d
parent 4159 a1b03ba2fcd0
child 6137 4720145f848b
permissions -rw-r--r--
patch-ab (from reinoud@NetBSD.org):
Apply patch to first unlock the CD before trying to eject it. Since
SDL doesn't seem to have lock/unlock primitives, this looks like the
right thing to do.

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