src/cdrom/linux/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Dec 2001 13:13:20 +0000
changeset 254 4fc12b8edf74
parent 252 e8157fcb3114
child 297 f6ffac90895c
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Functions for system-level CD-ROM audio control */
    29 
    30 #include <sys/types.h>
    31 #include <stdlib.h>
    32 #include <sys/stat.h>
    33 #include <sys/ioctl.h>
    34 #include <fcntl.h>
    35 #include <stdio.h>
    36 #include <string.h>
    37 #include <errno.h>
    38 #include <unistd.h>
    39 #ifdef __linux__
    40 #include <linux/cdrom.h>
    41 #endif
    42 #ifdef __SVR4
    43 #include <sys/cdio.h>
    44 #endif
    45 
    46 /* Define this to use the alternative getmntent() code */
    47 #ifndef __SVR4
    48 #define USE_MNTENT
    49 #endif
    50 
    51 #ifdef USE_MNTENT
    52 #if defined(__USLC__)
    53 #include <sys/mntent.h>
    54 #else
    55 #include <mntent.h>
    56 #endif
    57 
    58 #ifndef _PATH_MNTTAB
    59 #ifdef MNTTAB
    60 #define _PATH_MNTTAB	MNTTAB
    61 #else
    62 #define _PATH_MNTTAB	"/etc/fstab"
    63 #endif
    64 #endif /* !_PATH_MNTTAB */
    65 
    66 #ifndef _PATH_MOUNTED
    67 #define _PATH_MOUNTED	"/etc/mtab"
    68 #endif /* !_PATH_MOUNTED */
    69 
    70 #ifndef MNTTYPE_CDROM
    71 #define MNTTYPE_CDROM	"iso9660"
    72 #endif
    73 #ifndef MNTTYPE_SUPER
    74 #define MNTTYPE_SUPER	"supermount"
    75 #endif
    76 #endif /* USE_MNTENT */
    77 
    78 #include "SDL_error.h"
    79 #include "SDL_cdrom.h"
    80 #include "SDL_syscdrom.h"
    81 
    82 
    83 /* The maximum number of CD-ROM drives we'll detect */
    84 #define MAX_DRIVES	16	
    85 
    86 /* A list of available CD-ROM drives */
    87 static char *SDL_cdlist[MAX_DRIVES];
    88 static dev_t SDL_cdmode[MAX_DRIVES];
    89 
    90 /* The system-dependent CD control functions */
    91 static const char *SDL_SYS_CDName(int drive);
    92 static int SDL_SYS_CDOpen(int drive);
    93 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
    94 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
    95 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
    96 static int SDL_SYS_CDPause(SDL_CD *cdrom);
    97 static int SDL_SYS_CDResume(SDL_CD *cdrom);
    98 static int SDL_SYS_CDStop(SDL_CD *cdrom);
    99 static int SDL_SYS_CDEject(SDL_CD *cdrom);
   100 static void SDL_SYS_CDClose(SDL_CD *cdrom);
   101 
   102 /* Some ioctl() errno values which occur when the tray is empty */
   103 #define ERRNO_TRAYEMPTY(errno)	\
   104 	((errno == EIO) || (errno == ENOENT) || (errno == EINVAL))
   105 
   106 /* Check a drive to see if it is a CD-ROM */
   107 static int CheckDrive(char *drive, char *mnttype, struct stat *stbuf)
   108 {
   109 	int is_cd, cdfd;
   110 	struct cdrom_subchnl info;
   111 
   112 	/* If it doesn't exist, return -1 */
   113 	if ( stat(drive, stbuf) < 0 ) {
   114 		return(-1);
   115 	}
   116 
   117 	/* If it does exist, verify that it's an available CD-ROM */
   118 	is_cd = 0;
   119 	if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) {
   120 		cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
   121 		if ( cdfd >= 0 ) {
   122 			info.cdsc_format = CDROM_MSF;
   123 			/* Under Linux, EIO occurs when a disk is not present.
   124 			 */
   125 			if ( (ioctl(cdfd, CDROMSUBCHNL, &info) == 0) ||
   126 						ERRNO_TRAYEMPTY(errno) ) {
   127 				is_cd = 1;
   128 			}
   129 			close(cdfd);
   130 		}
   131 #ifdef USE_MNTENT
   132 		/* Even if we can't read it, it might be mounted */
   133 		else if ( mnttype && (strcmp(mnttype, MNTTYPE_CDROM) == 0) ) {
   134 			is_cd = 1;
   135 		}
   136 #endif
   137 	}
   138 	return(is_cd);
   139 }
   140 
   141 /* Add a CD-ROM drive to our list of valid drives */
   142 static void AddDrive(char *drive, struct stat *stbuf)
   143 {
   144 	int i;
   145 
   146 	if ( SDL_numcds < MAX_DRIVES ) {
   147 		/* Check to make sure it's not already in our list.
   148 	 	   This can happen when we see a drive via symbolic link.
   149 		 */
   150 		for ( i=0; i<SDL_numcds; ++i ) {
   151 			if ( stbuf->st_rdev == SDL_cdmode[i] ) {
   152 #ifdef DEBUG_CDROM
   153   fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
   154 #endif
   155 				return;
   156 			}
   157 		}
   158 
   159 		/* Add this drive to our list */
   160 		i = SDL_numcds;
   161 		SDL_cdlist[i] = (char *)malloc(strlen(drive)+1);
   162 		if ( SDL_cdlist[i] == NULL ) {
   163 			SDL_OutOfMemory();
   164 			return;
   165 		}
   166 		strcpy(SDL_cdlist[i], drive);
   167 		SDL_cdmode[i] = stbuf->st_rdev;
   168 		++SDL_numcds;
   169 #ifdef DEBUG_CDROM
   170   fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
   171 #endif
   172 	}
   173 }
   174 
   175 #ifdef USE_MNTENT
   176 static void CheckMounts(const char *mtab)
   177 {
   178 	FILE *mntfp;
   179 	struct mntent *mntent;
   180 	struct stat stbuf;
   181 
   182 	mntfp = setmntent(mtab, "r");
   183 	if ( mntfp != NULL ) {
   184 		char *tmp;
   185 		char *mnt_type;
   186 		char *mnt_dev;
   187 
   188 		while ( (mntent=getmntent(mntfp)) != NULL ) {
   189 			mnt_type = malloc(strlen(mntent->mnt_type) + 1);
   190 			if (mnt_type == NULL)
   191 				continue;  /* maybe you'll get lucky next time. */
   192 
   193 			mnt_dev = malloc(strlen(mntent->mnt_fsname) + 1);
   194 			if (mnt_dev == NULL) {
   195 				free(mnt_type);
   196 				continue;
   197 			}
   198 
   199 			strcpy(mnt_type, mntent->mnt_type);
   200 			strcpy(mnt_dev, mntent->mnt_fsname);
   201 
   202 			/* Handle "supermount" filesystem mounts */
   203 			if ( strcmp(mnt_type, MNTTYPE_SUPER) == 0 ) {
   204 				tmp = strstr(mntent->mnt_opts, "fs=");
   205 				if ( tmp ) {
   206 					free(mnt_type);
   207 					mnt_type = strdup(tmp + strlen("fs="));
   208 					if ( mnt_type ) {
   209 						tmp = strchr(mnt_type, ',');
   210 						if ( tmp ) {
   211 							*tmp = '\0';
   212 						}
   213 					}
   214 				}
   215 				tmp = strstr(mntent->mnt_opts, "dev=");
   216 				if ( tmp ) {
   217 					free(mnt_dev);
   218 					mnt_dev = strdup(tmp + strlen("dev="));
   219 					if ( mnt_dev ) {
   220 						tmp = strchr(mnt_dev, ',');
   221 						if ( tmp ) {
   222 							*tmp = '\0';
   223 						}
   224 					}
   225 				}
   226 			}
   227 			if ( strcmp(mnt_type, MNTTYPE_CDROM) == 0 ) {
   228 #ifdef DEBUG_CDROM
   229   fprintf(stderr, "Checking mount path from %s: %s mounted on %s of %s\n",
   230 	mtab, mnt_dev, mntent->mnt_dir, mnt_type);
   231 #endif
   232 				if (CheckDrive(mnt_dev, mnt_type, &stbuf) > 0) {
   233 					AddDrive(mnt_dev, &stbuf);
   234 				}
   235 			}
   236 			free(mnt_dev);
   237 			free(mnt_type);
   238 		}
   239 		endmntent(mntfp);
   240 	}
   241 }
   242 #endif /* USE_MNTENT */
   243 
   244 int  SDL_SYS_CDInit(void)
   245 {
   246 	/* checklist: /dev/cdrom, /dev/hd?, /dev/scd? /dev/sr? */
   247 	static char *checklist[] = {
   248 		"cdrom", "?a hd?", "?0 scd?", "?0 sr?", NULL
   249 	};
   250 	char *SDLcdrom;
   251 	int i, j, exists;
   252 	char drive[32];
   253 	struct stat stbuf;
   254 
   255 	/* Fill in our driver capabilities */
   256 	SDL_CDcaps.Name = SDL_SYS_CDName;
   257 	SDL_CDcaps.Open = SDL_SYS_CDOpen;
   258 	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   259 	SDL_CDcaps.Status = SDL_SYS_CDStatus;
   260 	SDL_CDcaps.Play = SDL_SYS_CDPlay;
   261 	SDL_CDcaps.Pause = SDL_SYS_CDPause;
   262 	SDL_CDcaps.Resume = SDL_SYS_CDResume;
   263 	SDL_CDcaps.Stop = SDL_SYS_CDStop;
   264 	SDL_CDcaps.Eject = SDL_SYS_CDEject;
   265 	SDL_CDcaps.Close = SDL_SYS_CDClose;
   266 
   267 	/* Look in the environment for our CD-ROM drive list */
   268 	SDLcdrom = getenv("SDL_CDROM");	/* ':' separated list of devices */
   269 	if ( SDLcdrom != NULL ) {
   270 		char *cdpath, *delim;
   271 		cdpath = malloc(strlen(SDLcdrom)+1);
   272 		if ( cdpath != NULL ) {
   273 			strcpy(cdpath, SDLcdrom);
   274 			SDLcdrom = cdpath;
   275 			do {
   276 				delim = strchr(SDLcdrom, ':');
   277 				if ( delim ) {
   278 					*delim++ = '\0';
   279 				}
   280 #ifdef DEBUG_CDROM
   281   fprintf(stderr, "Checking CD-ROM drive from SDL_CDROM: %s\n", SDLcdrom);
   282 #endif
   283 				if ( CheckDrive(SDLcdrom, NULL, &stbuf) > 0 ) {
   284 					AddDrive(SDLcdrom, &stbuf);
   285 				}
   286 				if ( delim ) {
   287 					SDLcdrom = delim;
   288 				} else {
   289 					SDLcdrom = NULL;
   290 				}
   291 			} while ( SDLcdrom );
   292 			free(cdpath);
   293 		}
   294 
   295 		/* If we found our drives, there's nothing left to do */
   296 		if ( SDL_numcds > 0 ) {
   297 			return(0);
   298 		}
   299 	}
   300 
   301 #ifdef USE_MNTENT
   302 	/* Check /dev/cdrom first :-) */
   303 	if (CheckDrive("/dev/cdrom", NULL, &stbuf) > 0) {
   304 		AddDrive("/dev/cdrom", &stbuf);
   305 	}
   306 
   307 	/* Now check the currently mounted CD drives */
   308 	CheckMounts(_PATH_MOUNTED);
   309 
   310 	/* Finally check possible mountable drives in /etc/fstab */
   311 	CheckMounts(_PATH_MNTTAB);
   312 
   313 	/* If we found our drives, there's nothing left to do */
   314 	if ( SDL_numcds > 0 ) {
   315 		return(0);
   316 	}
   317 #endif /* USE_MNTENT */
   318 
   319 	/* Scan the system for CD-ROM drives.
   320 	   Not always 100% reliable, so use the USE_MNTENT code above first.
   321 	 */
   322 	for ( i=0; checklist[i]; ++i ) {
   323 		if ( checklist[i][0] == '?' ) {
   324 			char *insert;
   325 			exists = 1;
   326 			for ( j=checklist[i][1]; exists; ++j ) {
   327 				sprintf(drive, "/dev/%s", &checklist[i][3]);
   328 				insert = strchr(drive, '?');
   329 				if ( insert != NULL ) {
   330 					*insert = j;
   331 				}
   332 #ifdef DEBUG_CDROM
   333   fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive);
   334 #endif
   335 				switch (CheckDrive(drive, NULL, &stbuf)) {
   336 					/* Drive exists and is a CD-ROM */
   337 					case 1:
   338 						AddDrive(drive, &stbuf);
   339 						break;
   340 					/* Drive exists, but isn't a CD-ROM */
   341 					case 0:
   342 						break;
   343 					/* Drive doesn't exist */
   344 					case -1:
   345 						exists = 0;
   346 						break;
   347 				}
   348 			}
   349 		} else {
   350 			sprintf(drive, "/dev/%s", checklist[i]);
   351 #ifdef DEBUG_CDROM
   352   fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive);
   353 #endif
   354 			if ( CheckDrive(drive, NULL, &stbuf) > 0 ) {
   355 				AddDrive(drive, &stbuf);
   356 			}
   357 		}
   358 	}
   359 	return(0);
   360 }
   361 
   362 /* General ioctl() CD-ROM command function */
   363 static int SDL_SYS_CDioctl(int id, int command, void *arg)
   364 {
   365 	int retval;
   366 
   367 	retval = ioctl(id, command, arg);
   368 	if ( retval < 0 ) {
   369 		SDL_SetError("ioctl() error: %s", strerror(errno));
   370 	}
   371 	return(retval);
   372 }
   373 
   374 static const char *SDL_SYS_CDName(int drive)
   375 {
   376 	return(SDL_cdlist[drive]);
   377 }
   378 
   379 static int SDL_SYS_CDOpen(int drive)
   380 {
   381 	return(open(SDL_cdlist[drive], (O_RDONLY|O_EXCL|O_NONBLOCK), 0));
   382 }
   383 
   384 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
   385 {
   386 	struct cdrom_tochdr toc;
   387 	int i, okay;
   388 	struct cdrom_tocentry entry;
   389 
   390 	okay = 0;
   391 	if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc) == 0 ) {
   392 		cdrom->numtracks = toc.cdth_trk1-toc.cdth_trk0+1;
   393 		if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
   394 			cdrom->numtracks = SDL_MAX_TRACKS;
   395 		}
   396 		/* Read all the track TOC entries */
   397 		for ( i=0; i<=cdrom->numtracks; ++i ) {
   398 			if ( i == cdrom->numtracks ) {
   399 				cdrom->track[i].id = CDROM_LEADOUT;
   400 			} else {
   401 				cdrom->track[i].id = toc.cdth_trk0+i;
   402 			}
   403 			entry.cdte_track = cdrom->track[i].id;
   404 			entry.cdte_format = CDROM_MSF;
   405 			if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCENTRY,
   406 								&entry) < 0 ) {
   407 				break;
   408 			} else {
   409 				if ( entry.cdte_ctrl & CDROM_DATA_TRACK ) {
   410 					cdrom->track[i].type = SDL_DATA_TRACK;
   411 				} else {
   412 					cdrom->track[i].type = SDL_AUDIO_TRACK;
   413 				}
   414 				cdrom->track[i].offset = MSF_TO_FRAMES(
   415 						entry.cdte_addr.msf.minute,
   416 						entry.cdte_addr.msf.second,
   417 						entry.cdte_addr.msf.frame);
   418 				cdrom->track[i].length = 0;
   419 				if ( i > 0 ) {
   420 					cdrom->track[i-1].length =
   421 						cdrom->track[i].offset-
   422 						cdrom->track[i-1].offset;
   423 				}
   424 			}
   425 		}
   426 		if ( i == (cdrom->numtracks+1) ) {
   427 			okay = 1;
   428 		}
   429 	}
   430 	return(okay ? 0 : -1);
   431 }
   432 
   433 /* Get CD-ROM status */
   434 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
   435 {
   436 	CDstatus status;
   437 	struct cdrom_tochdr toc;
   438 	struct cdrom_subchnl info;
   439 
   440 	info.cdsc_format = CDROM_MSF;
   441 	if ( ioctl(cdrom->id, CDROMSUBCHNL, &info) < 0 ) {
   442 		if ( ERRNO_TRAYEMPTY(errno) ) {
   443 			status = CD_TRAYEMPTY;
   444 		} else {
   445 			status = CD_ERROR;
   446 		}
   447 	} else {
   448 		switch (info.cdsc_audiostatus) {
   449 			case CDROM_AUDIO_INVALID:
   450 			case CDROM_AUDIO_NO_STATUS:
   451 				/* Try to determine if there's a CD available */
   452 				if (ioctl(cdrom->id, CDROMREADTOCHDR, &toc)==0)
   453 					status = CD_STOPPED;
   454 				else
   455 					status = CD_TRAYEMPTY;
   456 				break;
   457 			case CDROM_AUDIO_COMPLETED:
   458 				status = CD_STOPPED;
   459 				break;
   460 			case CDROM_AUDIO_PLAY:
   461 				status = CD_PLAYING;
   462 				break;
   463 			case CDROM_AUDIO_PAUSED:
   464 				/* Workaround buggy CD-ROM drive */
   465 				if ( info.cdsc_trk == CDROM_LEADOUT ) {
   466 					status = CD_STOPPED;
   467 				} else {
   468 					status = CD_PAUSED;
   469 				}
   470 				break;
   471 			default:
   472 				status = CD_ERROR;
   473 				break;
   474 		}
   475 	}
   476 	if ( position ) {
   477 		if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
   478 			*position = MSF_TO_FRAMES(
   479 					info.cdsc_absaddr.msf.minute,
   480 					info.cdsc_absaddr.msf.second,
   481 					info.cdsc_absaddr.msf.frame);
   482 		} else {
   483 			*position = 0;
   484 		}
   485 	}
   486 	return(status);
   487 }
   488 
   489 /* Start play */
   490 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
   491 {
   492 	struct cdrom_msf playtime;
   493 
   494 	FRAMES_TO_MSF(start,
   495 	   &playtime.cdmsf_min0, &playtime.cdmsf_sec0, &playtime.cdmsf_frame0);
   496 	FRAMES_TO_MSF(start+length,
   497 	   &playtime.cdmsf_min1, &playtime.cdmsf_sec1, &playtime.cdmsf_frame1);
   498 #ifdef DEBUG_CDROM
   499   fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
   500 	playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
   501 	playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
   502 #endif
   503 	return(SDL_SYS_CDioctl(cdrom->id, CDROMPLAYMSF, &playtime));
   504 }
   505 
   506 /* Pause play */
   507 static int SDL_SYS_CDPause(SDL_CD *cdrom)
   508 {
   509 	return(SDL_SYS_CDioctl(cdrom->id, CDROMPAUSE, 0));
   510 }
   511 
   512 /* Resume play */
   513 static int SDL_SYS_CDResume(SDL_CD *cdrom)
   514 {
   515 	return(SDL_SYS_CDioctl(cdrom->id, CDROMRESUME, 0));
   516 }
   517 
   518 /* Stop play */
   519 static int SDL_SYS_CDStop(SDL_CD *cdrom)
   520 {
   521 	return(SDL_SYS_CDioctl(cdrom->id, CDROMSTOP, 0));
   522 }
   523 
   524 /* Eject the CD-ROM */
   525 static int SDL_SYS_CDEject(SDL_CD *cdrom)
   526 {
   527 	return(SDL_SYS_CDioctl(cdrom->id, CDROMEJECT, 0));
   528 }
   529 
   530 /* Close the CD-ROM handle */
   531 static void SDL_SYS_CDClose(SDL_CD *cdrom)
   532 {
   533 	close(cdrom->id);
   534 }
   535 
   536 void SDL_SYS_CDQuit(void)
   537 {
   538 	int i;
   539 
   540 	if ( SDL_numcds > 0 ) {
   541 		for ( i=0; i<SDL_numcds; ++i ) {
   542 			free(SDL_cdlist[i]);
   543 		}
   544 		SDL_numcds = 0;
   545 	}
   546 }
   547