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 Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 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    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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_MINT
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Atari MetaDOS CD-ROM functions
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Patrice Mandin
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/cdromio.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mint/metados.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_cdrom.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_syscdrom.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Some ioctl() errno values which occur when the tray is empty */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef ENOMEDIUM
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ENOMEDIUM ENOENT
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ERRNO_TRAYEMPTY(errno)	\
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	((errno == EIO)    || (errno == ENOENT) || \
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 (errno == EINVAL) || (errno == ENOMEDIUM))
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The maximum number of CD-ROM drives we'll detect */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_DRIVES	32
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct {
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char		device[3];	/* Physical device letter + ':' + '\0' */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	metaopen_t	metaopen;		/* Infos on opened drive */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} metados_drive_t;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic metados_drive_t metados_drives[MAX_DRIVES];
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The system-dependent CD control functions */
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDioctl(int id, int command, void *arg);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_CDInit(void)
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	metainit_t	metainit={0,0,0,0};
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	metaopen_t	metaopen;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, handle;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_subchnl info;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Metainit(&metainit);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (metainit.version == NULL) {
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "MetaDOS not installed\n");
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (metainit.drives_map == 0) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "No MetaDOS devices present\n");
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_numcds = 0;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (i='A'; i<='Z'; i++) {
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		metados_drives[SDL_numcds].device[0] = 0;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		metados_drives[SDL_numcds].device[1] = ':';
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		metados_drives[SDL_numcds].device[2] = 0;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (metainit.drives_map & (1<<(i-'A'))) {
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			handle = Metaopen(i, &metaopen);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (handle == 0) {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				info.cdsc_format = CDROM_MSF;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( (Metaioctl(i, METADOS_IOCTL_MAGIC, CDROMSUBCHNL, &info) == 0) || ERRNO_TRAYEMPTY(errno) ) {
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					metados_drives[SDL_numcds].device[0] = i;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					++SDL_numcds;
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Metaclose(i);
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in our driver capabilities */
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Name = SDL_SYS_CDName;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Open = SDL_SYS_CDOpen;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Close = SDL_SYS_CDClose;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Status = SDL_SYS_CDStatus;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Play = SDL_SYS_CDPlay;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Pause = SDL_SYS_CDPause;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Resume = SDL_SYS_CDResume;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Stop = SDL_SYS_CDStop;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Eject = SDL_SYS_CDEject;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_CDQuit(void)
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_numcds = 0;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive)
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(metados_drives[drive].device);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive)
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int handle;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	handle = Metaopen(metados_drives[drive].device[0], &(metados_drives[drive].metaopen));
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (handle == 0) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return drive;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom)
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Metaclose(metados_drives[cdrom->id].device[0]);
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDioctl(int id, int command, void *arg)
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int retval;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	retval = Metaioctl(metados_drives[id].device[0], METADOS_IOCTL_MAGIC, command, arg);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( retval < 0 ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("ioctl() error: %s", strerror(errno));
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(retval);
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i,okay;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_tochdr toc;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_tocentry entry;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Use standard ioctl() */
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc)<0) {
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->numtracks = toc.cdth_trk1-toc.cdth_trk0+1;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdrom->numtracks = SDL_MAX_TRACKS;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Read all the track TOC entries */
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	okay=1;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<=cdrom->numtracks; ++i ) {
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( i == cdrom->numtracks ) {
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].id = CDROM_LEADOUT;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].id = toc.cdth_trk0+i;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		entry.cdte_track = cdrom->track[i].id;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		entry.cdte_format = CDROM_MSF;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCENTRY, &entry) < 0 ) {
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			okay=0;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( entry.cdte_ctrl & CDROM_DATA_TRACK ) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].type = SDL_DATA_TRACK;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].type = SDL_AUDIO_TRACK;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].offset = MSF_TO_FRAMES(
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				entry.cdte_addr.msf.minute,
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				entry.cdte_addr.msf.second,
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				entry.cdte_addr.msf.frame);
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].length = 0;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( i > 0 ) {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i-1].length = cdrom->track[i].offset-cdrom->track[i-1].offset;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(okay ? 0 : -1);
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get CD-ROM status */
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_tochdr toc;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_subchnl info;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	info.cdsc_format = CDROM_MSF;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_SYS_CDioctl(cdrom->id, CDROMSUBCHNL, &info) < 0 ) {
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ERRNO_TRAYEMPTY(errno) ) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			status = CD_TRAYEMPTY;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			status = CD_ERROR;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (info.cdsc_audiostatus) {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_INVALID:
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_NO_STATUS:
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Try to determine if there's a CD available */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc)==0) {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_STOPPED;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_TRAYEMPTY;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_COMPLETED:
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_STOPPED;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_PLAY:
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_PLAYING;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case CDROM_AUDIO_PAUSED:
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Workaround buggy CD-ROM drive */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( info.cdsc_trk == CDROM_LEADOUT ) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_STOPPED;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_PAUSED;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_ERROR;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( position ) {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*position = MSF_TO_FRAMES(
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					info.cdsc_absaddr.msf.minute,
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					info.cdsc_absaddr.msf.second,
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					info.cdsc_absaddr.msf.frame);
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*position = 0;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(status);
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Start play */
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct cdrom_msf playtime;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FRAMES_TO_MSF(start,
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   &playtime.cdmsf_min0, &playtime.cdmsf_sec0, &playtime.cdmsf_frame0);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FRAMES_TO_MSF(start+length,
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   &playtime.cdmsf_min1, &playtime.cdmsf_sec1, &playtime.cdmsf_frame1);
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_SYS_CDioctl(cdrom->id, CDROMPLAYMSF, &playtime);
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Pause play */
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom)
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_SYS_CDioctl(cdrom->id, CDROMPAUSE, 0);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Resume play */
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom)
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_SYS_CDioctl(cdrom->id, CDROMRESUME, 0);
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Stop play */
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom)
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_SYS_CDioctl(cdrom->id, CDROMSTOP, 0);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Eject the CD-ROM */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom)
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_SYS_CDioctl(cdrom->id, CDROMEJECT, 0);
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_CDROM_MINT */
318