19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_CDROM_LINUX
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions for system-level CD-ROM audio control */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <string.h>	/* For strerror() */
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __LINUX__
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_LINUX_VERSION_H
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* linux 2.6.9 workaround */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <linux/version.h>
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,9)
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <asm/types.h>
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __le64 __u64
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __le32 __u32
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __le16 __u16
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __be64 __u64
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __be32 __u32
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __be16 __u16
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* linux 2.6.9 workaround */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* HAVE_LINUX_VERSION_H */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <linux/cdrom.h>
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __SVR4
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/cdio.h>
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Define this to use the alternative getmntent() code */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef __SVR4
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define USE_MNTENT
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MNTENT
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__USLC__)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mntent.h>
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mntent.h>
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _PATH_MNTTAB
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef MNTTAB
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _PATH_MNTTAB	MNTTAB
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _PATH_MNTTAB	"/etc/fstab"
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !_PATH_MNTTAB */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef _PATH_MOUNTED
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define _PATH_MOUNTED	"/etc/mtab"
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* !_PATH_MOUNTED */
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef MNTTYPE_CDROM
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MNTTYPE_CDROM	"iso9660"
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef MNTTYPE_SUPER
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MNTTYPE_SUPER	"supermount"
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_MNTENT */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_cdrom.h"
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_syscdrom.h"
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The maximum number of CD-ROM drives we'll detect */
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_DRIVES	16
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* A list of available CD-ROM drives */
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *SDL_cdlist[MAX_DRIVES];
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic dev_t SDL_cdmode[MAX_DRIVES];
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The system-dependent CD control functions */
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive);
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Some ioctl() errno values which occur when the tray is empty */
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef ENOMEDIUM
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ENOMEDIUM ENOENT
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ERRNO_TRAYEMPTY(errno)	\
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	((errno == EIO)    || (errno == ENOENT) || \
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 (errno == EINVAL) || (errno == ENOMEDIUM))
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Check a drive to see if it is a CD-ROM */
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckDrive(char *drive, char *mnttype, struct stat *stbuf)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int is_cd, cdfd;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_subchnl info;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If it doesn't exist, return -1 */
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( stat(drive, stbuf) < 0 ) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If it does exist, verify that it's an available CD-ROM */
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	is_cd = 0;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) {
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cdfd >= 0 ) {
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			info.cdsc_format = CDROM_MSF;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Under Linux, EIO occurs when a disk is not present.
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 */
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (ioctl(cdfd, CDROMSUBCHNL, &info) == 0) ||
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						ERRNO_TRAYEMPTY(errno) ) {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				is_cd = 1;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			close(cdfd);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MNTENT
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Even if we can't read it, it might be mounted */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else if ( mnttype && (SDL_strcmp(mnttype, MNTTYPE_CDROM) == 0) ) {
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			is_cd = 1;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(is_cd);
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Add a CD-ROM drive to our list of valid drives */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AddDrive(char *drive, struct stat *stbuf)
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numcds < MAX_DRIVES ) {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Check to make sure it's not already in our list.
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 	   This can happen when we see a drive via symbolic link.
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_numcds; ++i ) {
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( stbuf->st_rdev == SDL_cdmode[i] ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Add this drive to our list */
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		i = SDL_numcds;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_cdlist[i] = SDL_strdup(drive);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_cdlist[i] == NULL ) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_cdmode[i] = stbuf->st_rdev;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++SDL_numcds;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MNTENT
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CheckMounts(const char *mtab)
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FILE *mntfp;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct mntent *mntent;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct stat stbuf;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mntfp = setmntent(mtab, "r");
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mntfp != NULL ) {
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *tmp;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *mnt_type;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		size_t mnt_type_len;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *mnt_dev;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		size_t mnt_dev_len;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while ( (mntent=getmntent(mntfp)) != NULL ) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mnt_type_len = SDL_strlen(mntent->mnt_type) + 1;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mnt_type = SDL_stack_alloc(char, mnt_type_len);
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (mnt_type == NULL)
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				continue;  /* maybe you'll get lucky next time. */
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mnt_dev_len = SDL_strlen(mntent->mnt_fsname) + 1;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mnt_dev = SDL_stack_alloc(char, mnt_dev_len);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (mnt_dev == NULL) {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_stack_free(mnt_type);
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				continue;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_strlcpy(mnt_type, mntent->mnt_type, mnt_type_len);
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_strlcpy(mnt_dev, mntent->mnt_fsname, mnt_dev_len);
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Handle "supermount" filesystem mounts */
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_strcmp(mnt_type, MNTTYPE_SUPER) == 0 ) {
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				tmp = SDL_strstr(mntent->mnt_opts, "fs=");
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( tmp ) {
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_stack_free(mnt_type);
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					mnt_type = SDL_strdup(tmp + SDL_strlen("fs="));
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( mnt_type ) {
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						tmp = SDL_strchr(mnt_type, ',');
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( tmp ) {
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							*tmp = '\0';
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				tmp = SDL_strstr(mntent->mnt_opts, "dev=");
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( tmp ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_stack_free(mnt_dev);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					mnt_dev = SDL_strdup(tmp + SDL_strlen("dev="));
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if ( mnt_dev ) {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						tmp = SDL_strchr(mnt_dev, ',');
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if ( tmp ) {
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							*tmp = '\0';
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_strcmp(mnt_type, MNTTYPE_CDROM) == 0 ) {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Checking mount path from %s: %s mounted on %s of %s\n",
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mtab, mnt_dev, mntent->mnt_dir, mnt_type);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (CheckDrive(mnt_dev, mnt_type, &stbuf) > 0) {
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					AddDrive(mnt_dev, &stbuf);
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_stack_free(mnt_dev);
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_stack_free(mnt_type);
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		endmntent(mntfp);
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_MNTENT */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint  SDL_SYS_CDInit(void)
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* checklist: /dev/cdrom, /dev/hd?, /dev/scd? /dev/sr? */
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static char *checklist[] = {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		"cdrom", "?a hd?", "?0 scd?", "?0 sr?", NULL
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	};
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *SDLcdrom;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, j, exists;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char drive[32];
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct stat stbuf;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in our driver capabilities */
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Name = SDL_SYS_CDName;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Open = SDL_SYS_CDOpen;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Status = SDL_SYS_CDStatus;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Play = SDL_SYS_CDPlay;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Pause = SDL_SYS_CDPause;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Resume = SDL_SYS_CDResume;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Stop = SDL_SYS_CDStop;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Eject = SDL_SYS_CDEject;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Close = SDL_SYS_CDClose;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Look in the environment for our CD-ROM drive list */
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDLcdrom != NULL ) {
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char *cdpath, *delim;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		size_t len = SDL_strlen(SDLcdrom)+1;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdpath = SDL_stack_alloc(char, len);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cdpath != NULL ) {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_strlcpy(cdpath, SDLcdrom, len);
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDLcdrom = cdpath;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			do {
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				delim = SDL_strchr(SDLcdrom, ':');
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( delim ) {
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*delim++ = '\0';
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Checking CD-ROM drive from SDL_CDROM: %s\n", SDLcdrom);
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( CheckDrive(SDLcdrom, NULL, &stbuf) > 0 ) {
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					AddDrive(SDLcdrom, &stbuf);
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( delim ) {
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDLcdrom = delim;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDLcdrom = NULL;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} while ( SDLcdrom );
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_stack_free(cdpath);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* If we found our drives, there's nothing left to do */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_numcds > 0 ) {
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(0);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_MNTENT
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check /dev/cdrom first :-) */
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (CheckDrive("/dev/cdrom", NULL, &stbuf) > 0) {
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		AddDrive("/dev/cdrom", &stbuf);
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Now check the currently mounted CD drives */
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CheckMounts(_PATH_MOUNTED);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Finally check possible mountable drives in /etc/fstab */
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CheckMounts(_PATH_MNTTAB);
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we found our drives, there's nothing left to do */
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numcds > 0 ) {
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_MNTENT */
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Scan the system for CD-ROM drives.
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   Not always 100% reliable, so use the USE_MNTENT code above first.
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; checklist[i]; ++i ) {
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( checklist[i][0] == '?' ) {
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			char *insert;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			exists = 1;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( j=checklist[i][1]; exists; ++j ) {
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s", &checklist[i][3]);
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				insert = SDL_strchr(drive, '?');
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( insert != NULL ) {
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*insert = j;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch (CheckDrive(drive, NULL, &stbuf)) {
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Drive exists and is a CD-ROM */
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case 1:
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						AddDrive(drive, &stbuf);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Drive exists, but isn't a CD-ROM */
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case 0:
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* Drive doesn't exist */
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case -1:
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						exists = 0;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s", checklist[i]);
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive);
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( CheckDrive(drive, NULL, &stbuf) > 0 ) {
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				AddDrive(drive, &stbuf);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* General ioctl() CD-ROM command function */
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDioctl(int id, int command, void *arg)
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = ioctl(id, command, arg);
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retval < 0 ) {
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("ioctl() error: %s", strerror(errno));
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive)
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_cdlist[drive]);
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive)
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(open(SDL_cdlist[drive], (O_RDONLY|O_NONBLOCK), 0));
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_tochdr toc;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, okay;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_tocentry entry;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	okay = 0;
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc) == 0 ) {
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdrom->numtracks = toc.cdth_trk1-toc.cdth_trk0+1;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->numtracks = SDL_MAX_TRACKS;
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Read all the track TOC entries */
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<=cdrom->numtracks; ++i ) {
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( i == cdrom->numtracks ) {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].id = CDROM_LEADOUT;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].id = toc.cdth_trk0+i;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry.cdte_track = cdrom->track[i].id;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			entry.cdte_format = CDROM_MSF;
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCENTRY,
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								&entry) < 0 ) {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( entry.cdte_ctrl & CDROM_DATA_TRACK ) {
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cdrom->track[i].type = SDL_DATA_TRACK;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cdrom->track[i].type = SDL_AUDIO_TRACK;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].offset = MSF_TO_FRAMES(
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						entry.cdte_addr.msf.minute,
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						entry.cdte_addr.msf.second,
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						entry.cdte_addr.msf.frame);
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].length = 0;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( i > 0 ) {
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cdrom->track[i-1].length =
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						cdrom->track[i].offset-
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						cdrom->track[i-1].offset;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( i == (cdrom->numtracks+1) ) {
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			okay = 1;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(okay ? 0 : -1);
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get CD-ROM status */
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_tochdr toc;
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_subchnl info;
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	info.cdsc_format = CDROM_MSF;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(cdrom->id, CDROMSUBCHNL, &info) < 0 ) {
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ERRNO_TRAYEMPTY(errno) ) {
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			status = CD_TRAYEMPTY;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			status = CD_ERROR;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (info.cdsc_audiostatus) {
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_INVALID:
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_NO_STATUS:
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Try to determine if there's a CD available */
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (ioctl(cdrom->id, CDROMREADTOCHDR, &toc)==0)
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_STOPPED;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				else
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_TRAYEMPTY;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_COMPLETED:
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_STOPPED;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_PLAY:
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_PLAYING;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_PAUSED:
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Workaround buggy CD-ROM drive */
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( info.cdsc_trk == CDROM_LEADOUT ) {
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_STOPPED;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_PAUSED;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_ERROR;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( position ) {
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*position = MSF_TO_FRAMES(
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					info.cdsc_absaddr.msf.minute,
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					info.cdsc_absaddr.msf.second,
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					info.cdsc_absaddr.msf.frame);
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*position = 0;
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(status);
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Start play */
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_msf playtime;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FRAMES_TO_MSF(start,
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   &playtime.cdmsf_min0, &playtime.cdmsf_sec0, &playtime.cdmsf_frame0);
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FRAMES_TO_MSF(start+length,
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   &playtime.cdmsf_min1, &playtime.cdmsf_sec1, &playtime.cdmsf_frame1);
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, CDROMPLAYMSF, &playtime));
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Pause play */
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom)
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, CDROMPAUSE, 0));
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Resume play */
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom)
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, CDROMRESUME, 0));
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Stop play */
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom)
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, CDROMSTOP, 0));
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Eject the CD-ROM */
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom)
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, CDROMEJECT, 0));
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Close the CD-ROM handle */
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom)
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	close(cdrom->id);
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_CDQuit(void)
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numcds > 0 ) {
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_numcds; ++i ) {
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_cdlist[i]);
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_numcds = 0;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_CDROM_LINUX */
565