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_WIN32
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions for system-level CD-ROM audio control */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WIN32_LEAN_AND_MEAN
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <windows.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mmsystem.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_cdrom.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_syscdrom.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This really broken?? */
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BROKEN_MCI_PAUSE	/* Pausing actually stops play -- Doh! */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The maximum number of CD-ROM drives we'll detect (Don't change!) */
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_DRIVES	26
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* A list of available CD-ROM drives */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *SDL_cdlist[MAX_DRIVES];
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic MCIDEVICEID SDL_mciID[MAX_DRIVES];
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BROKEN_MCI_PAUSE
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_paused[MAX_DRIVES];
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_CD_end_position;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The system-dependent CD control functions */
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Add a CD-ROM drive to our list of valid drives */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AddDrive(char *drive)
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numcds < MAX_DRIVES ) {
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Add this drive to our list */
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		i = SDL_numcds;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_cdlist[i] = SDL_strdup(drive);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_cdlist[i] == NULL ) {
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_OutOfMemory();
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++SDL_numcds;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef CDROM_DEBUG
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint  SDL_SYS_CDInit(void)
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* checklist: Drive 'A' - 'Z' */
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char drive[4];
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in our driver capabilities */
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Name = SDL_SYS_CDName;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Open = SDL_SYS_CDOpen;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Status = SDL_SYS_CDStatus;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Play = SDL_SYS_CDPlay;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Pause = SDL_SYS_CDPause;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Resume = SDL_SYS_CDResume;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Stop = SDL_SYS_CDStop;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Eject = SDL_SYS_CDEject;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CDcaps.Close = SDL_SYS_CDClose;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Scan the system for CD-ROM drives */
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i='A'; i<='Z'; ++i ) {
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_snprintf(drive, SDL_arraysize(drive), "%c:\\", i);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( GetDriveType(drive) == DRIVE_CDROM ) {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			AddDrive(drive);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(SDL_mciID, 0, sizeof(SDL_mciID));
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* General ioctl() CD-ROM command function */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDioctl(int id, UINT msg, DWORD flags, void *arg)
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCIERROR mci_error;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_error = mciSendCommand(SDL_mciID[id], msg, flags, (DWORD_PTR)arg);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mci_error ) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		char error[256];
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mciGetErrorString(mci_error, error, 256);
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("mciSendCommand() error: %s", error);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(!mci_error ? 0 : -1);
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_cdlist[drive]);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive)
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCI_OPEN_PARMS mci_open;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCI_SET_PARMS mci_set;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char device[3];
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD flags;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Open the requested device */
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_open.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device[0] = *SDL_cdlist[drive];
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device[1] = ':';
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device[2] = '\0';
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_open.lpstrElementName = device;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flags =
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  (MCI_OPEN_TYPE|MCI_OPEN_SHAREABLE|MCI_OPEN_TYPE_ID|MCI_OPEN_ELEMENT);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_SYS_CDioctl(0, MCI_OPEN, flags, &mci_open) < 0 ) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flags &= ~MCI_OPEN_SHAREABLE;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_SYS_CDioctl(0, MCI_OPEN, flags, &mci_open) < 0 ) {
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_mciID[drive] = mci_open.wDeviceID;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the minute-second-frame time format */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_set.dwTimeFormat = MCI_FORMAT_MSF;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SYS_CDioctl(drive, MCI_SET, MCI_SET_TIME_FORMAT, &mci_set);
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BROKEN_MCI_PAUSE
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_paused[drive] = 0;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(drive);
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCI_STATUS_PARMS mci_status;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, okay;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD flags;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	okay = 0;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_status.dwItem = MCI_STATUS_NUMBER_OF_TRACKS;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flags = MCI_STATUS_ITEM | MCI_WAIT;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, &mci_status) == 0 ) {
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdrom->numtracks = mci_status.dwReturn;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->numtracks = SDL_MAX_TRACKS;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Read all the track TOC entries */
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flags = MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<cdrom->numtracks; ++i ) {
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].id = i+1;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mci_status.dwTrack = cdrom->track[i].id;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef MCI_CDA_STATUS_TYPE_TRACK
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mci_status.dwItem = MCI_CDA_STATUS_TYPE_TRACK;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags,
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							&mci_status) < 0 ) {
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( mci_status.dwReturn == MCI_CDA_TRACK_AUDIO ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].type = SDL_AUDIO_TRACK;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].type = SDL_DATA_TRACK;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].type = SDL_AUDIO_TRACK;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mci_status.dwItem = MCI_STATUS_POSITION;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags,
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							&mci_status) < 0 ) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].offset = MSF_TO_FRAMES(
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_MINUTE(mci_status.dwReturn),
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_SECOND(mci_status.dwReturn),
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_FRAME(mci_status.dwReturn));
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			cdrom->track[i].length = 0;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( i > 0 ) {
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i-1].length =
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						cdrom->track[i].offset-
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						cdrom->track[i-1].offset;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( i == cdrom->numtracks ) {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mci_status.dwTrack = cdrom->track[i - 1].id;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mci_status.dwItem = MCI_STATUS_LENGTH;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags,
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							&mci_status) == 0 ) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i - 1].length = MSF_TO_FRAMES(
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_MINUTE(mci_status.dwReturn),
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_SECOND(mci_status.dwReturn),
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_FRAME(mci_status.dwReturn));
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* compute lead-out offset */
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].offset = cdrom->track[i - 1].offset +
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					cdrom->track[i - 1].length;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				cdrom->track[i].length = 0;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				okay = 1;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(okay ? 0 : -1);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get CD-ROM status */
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CDstatus status;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCI_STATUS_PARMS mci_status;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD flags;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flags = MCI_STATUS_ITEM | MCI_WAIT;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_status.dwItem = MCI_STATUS_MODE;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, &mci_status) < 0 ) {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_ERROR;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (mci_status.dwReturn) {
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case MCI_MODE_NOT_READY:
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case MCI_MODE_OPEN:
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_TRAYEMPTY;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case MCI_MODE_STOP:
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BROKEN_MCI_PAUSE
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( SDL_paused[cdrom->id] ) {
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_PAUSED;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_STOPPED;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_STOPPED;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* BROKEN_MCI_PAUSE */
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case MCI_MODE_PLAY:
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BROKEN_MCI_PAUSE
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( SDL_paused[cdrom->id] ) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_PAUSED;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					status = CD_PLAYING;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_PLAYING;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* BROKEN_MCI_PAUSE */
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case MCI_MODE_PAUSE:
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_PAUSED;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				status = CD_ERROR;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( position ) {
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mci_status.dwItem = MCI_STATUS_POSITION;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags,
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							&mci_status) == 0 ) {
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*position = MSF_TO_FRAMES(
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_MINUTE(mci_status.dwReturn),
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_SECOND(mci_status.dwReturn),
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					MCI_MSF_FRAME(mci_status.dwReturn));
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*position = 0;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*position = 0;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(status);
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Start play */
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCI_PLAY_PARMS mci_play;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int m, s, f;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DWORD flags;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flags = MCI_FROM | MCI_TO | MCI_NOTIFY;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_play.dwCallback = 0;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FRAMES_TO_MSF(start, &m, &s, &f);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_play.dwFrom = MCI_MAKE_MSF(m, s, f);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	FRAMES_TO_MSF(start+length, &m, &s, &f);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_play.dwTo = MCI_MAKE_MSF(m, s, f);
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CD_end_position = mci_play.dwTo;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, MCI_PLAY, flags, &mci_play));
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Pause play */
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom)
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BROKEN_MCI_PAUSE
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_paused[cdrom->id] = 1;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, MCI_PAUSE, MCI_WAIT, NULL));
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Resume play */
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom)
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef BROKEN_MCI_PAUSE
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MCI_STATUS_PARMS mci_status;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int okay;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int flags;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	okay = 0;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Play from the current play position to the end position set earlier */
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	flags = MCI_STATUS_ITEM | MCI_WAIT;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mci_status.dwItem = MCI_STATUS_POSITION;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, &mci_status) == 0 ) {
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		MCI_PLAY_PARMS mci_play;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flags = MCI_FROM | MCI_TO | MCI_NOTIFY;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mci_play.dwCallback = 0;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mci_play.dwFrom = mci_status.dwReturn;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mci_play.dwTo = SDL_CD_end_position;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_SYS_CDioctl(cdrom->id,MCI_PLAY,flags,&mci_play) == 0) {
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			okay = 1;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_paused[cdrom->id] = 0;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(okay ? 0 : -1);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, MCI_RESUME, MCI_WAIT, NULL));
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* BROKEN_MCI_PAUSE */
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Stop play */
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom)
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, MCI_STOP, MCI_WAIT, NULL));
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Eject the CD-ROM */
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom)
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_SYS_CDioctl(cdrom->id, MCI_SET, MCI_SET_DOOR_OPEN, NULL));
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Close the CD-ROM handle */
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom)
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SYS_CDioctl(cdrom->id, MCI_CLOSE, MCI_WAIT, NULL);
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_CDQuit(void)
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numcds > 0 ) {
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_numcds; ++i ) {
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(SDL_cdlist[i]);
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_cdlist[i] = NULL;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_numcds = 0;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_CDROM_WIN32 */
387