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_MACOSX
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syscdrom_c.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark -- Globals --
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic FSRef**         tracks;
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic FSVolumeRefNum* volumes;
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus        status;
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             nextTrackFrame;
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             nextTrackFramesRemaining;
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             fakeCD;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             currentTrack;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             didReadTOC;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             cacheTOCNumTracks;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int             currentDrive; /* Only allow 1 drive in use at a time */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark -- Prototypes --
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName   (int drive);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDOpen   (int drive);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDGetTOC (SDL_CD *cdrom);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus    SDL_SYS_CDStatus (SDL_CD *cdrom, int *position);
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDPlay   (SDL_CD *cdrom, int start, int length);
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDPause  (SDL_CD *cdrom);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDResume (SDL_CD *cdrom);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDStop   (SDL_CD *cdrom);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDEject  (SDL_CD *cdrom);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void        SDL_SYS_CDClose  (SDL_CD *cdrom);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark -- Helper Functions --
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Read a list of tracks from the volume */
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadTracks (SDL_CD *cdrom)
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Check if tracks are already loaded */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if  ( tracks[cdrom->id] != NULL )
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allocate memory for tracks */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    tracks[cdrom->id] = (FSRef*) SDL_calloc (1, sizeof(**tracks) * cdrom->numtracks);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (tracks[cdrom->id] == NULL) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory ();
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Load tracks */
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ListTrackFiles (volumes[cdrom->id], tracks[cdrom->id], cdrom->numtracks) < 0)
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Find a file for a given start frame and length */
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic FSRef* GetFileForOffset (SDL_CD *cdrom, int start, int length,  int *outStartFrame, int *outStopFrame)
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i = 0; i < cdrom->numtracks; i++) {
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (cdrom->track[i].offset <= start &&
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            start < (cdrom->track[i].offset + cdrom->track[i].length))
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            break;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (i == cdrom->numtracks)
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return NULL;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    currentTrack = i;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    *outStartFrame = start - cdrom->track[i].offset;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((*outStartFrame + length) < cdrom->track[i].length) {
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *outStopFrame = *outStartFrame + length;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        length = 0;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        nextTrackFrame = -1;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        nextTrackFramesRemaining = -1;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *outStopFrame = -1;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        length -= cdrom->track[i].length - *outStartFrame;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        nextTrackFrame = cdrom->track[i+1].offset;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        nextTrackFramesRemaining = length;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return &tracks[cdrom->id][i];
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Setup another file for playback, or stop playback (called from another thread) */
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void CompletionProc (SDL_CD *cdrom)
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock ();
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (nextTrackFrame > 0 && nextTrackFramesRemaining > 0) {
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Load the next file to play */
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int startFrame, stopFrame;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FSRef *file;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        PauseFile ();
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ReleaseFile ();
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        file = GetFileForOffset (cdrom, nextTrackFrame,
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            nextTrackFramesRemaining, &startFrame, &stopFrame);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (file == NULL) {
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            status = CD_STOPPED;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            Unlock ();
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        LoadFile (file, startFrame, stopFrame);
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SetCompletionProc (CompletionProc, cdrom);
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        PlayFile ();
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else {
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Release the current file */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        PauseFile ();
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ReleaseFile ();
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        status = CD_STOPPED;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Unlock ();
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#pragma mark -- Driver Functions --
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialize */
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_CDInit (void)
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Initialize globals */
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    volumes = NULL;
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    tracks  = NULL;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status  = CD_STOPPED;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    nextTrackFrame = -1;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    nextTrackFramesRemaining = -1;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fakeCD  = SDL_FALSE;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    currentTrack = -1;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    didReadTOC = SDL_FALSE;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cacheTOCNumTracks = -1;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    currentDrive = -1;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Fill in function pointers */
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Name   = SDL_SYS_CDName;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Open   = SDL_SYS_CDOpen;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Status = SDL_SYS_CDStatus;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Play   = SDL_SYS_CDPlay;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Pause  = SDL_SYS_CDPause;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Resume = SDL_SYS_CDResume;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Stop   = SDL_SYS_CDStop;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Eject  = SDL_SYS_CDEject;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Close  = SDL_SYS_CDClose;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Read the list of "drives"
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        This is currently a hack that infers drives from
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        mounted audio CD volumes, rather than
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        actual CD-ROM devices - which means it may not
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        act as expected sometimes.
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    */
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Find out how many cd volumes are mounted */
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_numcds = DetectAudioCDVolumes (NULL, 0);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        If there are no volumes, fake a cd device
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        so tray empty can be reported.
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    */
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_numcds == 0) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fakeCD = SDL_TRUE;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_numcds = 1;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        status = CD_TRAYEMPTY;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allocate space for volumes */
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    volumes = (FSVolumeRefNum*) SDL_calloc (1, sizeof(*volumes) * SDL_numcds);
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (volumes == NULL) {
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory ();
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allocate space for tracks */
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    tracks = (FSRef**) SDL_calloc (1, sizeof(*tracks) * (SDL_numcds + 1));
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (tracks == NULL) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory ();
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Mark the end of the tracks array */
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    tracks[ SDL_numcds ] = (FSRef*)-1;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Redetect, now save all volumes for later
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Update SDL_numcds just in case it changed
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    */
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int numVolumes = SDL_numcds;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_numcds = DetectAudioCDVolumes (volumes, numVolumes);
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* If more cds suddenly show up, ignore them */
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (SDL_numcds > numVolumes) {
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_SetError ("Some CD's were added but they will be ignored");
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_numcds = numVolumes;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Shutdown and cleanup */
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_CDQuit(void)
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ReleaseFile();
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (volumes != NULL)
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        free (volumes);
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (tracks != NULL) {
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FSRef **ptr;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        for (ptr = tracks; *ptr != (FSRef*)-1; ptr++)
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if (*ptr != NULL)
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                free (*ptr);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        free (tracks);
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get the Unix disk name of the volume */
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName (int drive)
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * !!! FIXME: PBHGetVolParmsSync() is gone in 10.6,
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * !!! FIXME:  replaced with FSGetVolumeParms(), which
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * !!! FIXME:  isn't available before 10.5.  :/
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return "Mac OS X CD-ROM Device";
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSStatus     err = noErr;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HParamBlockRec  pb;
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    GetVolParmsInfoBuffer   volParmsInfo;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD)
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return "Fake CD-ROM Device";
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pb.ioParam.ioNamePtr = NULL;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pb.ioParam.ioVRefNum = volumes[drive];
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pb.ioParam.ioBuffer = (Ptr)&volParmsInfo;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pb.ioParam.ioReqCount = (SInt32)sizeof(volParmsInfo);
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    err = PBHGetVolParmsSync(&pb);
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (err != noErr) {
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError ("PBHGetVolParmsSync returned %d", err);
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return NULL;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return volParmsInfo.vMDeviceID;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Open the "device" */
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen (int drive)
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Only allow 1 device to be open */
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (currentDrive >= 0) {
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError ("Only one cdrom is supported");
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        currentDrive = drive;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return drive;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get the table of contents */
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC (SDL_CD *cdrom)
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD) {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError (kErrorFakeDevice);
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (didReadTOC) {
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        cdrom->numtracks = cacheTOCNumTracks;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ReadTOCData (volumes[cdrom->id], cdrom);
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    didReadTOC = SDL_TRUE;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cacheTOCNumTracks = cdrom->numtracks;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get CD-ROM status */
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus (SDL_CD *cdrom, int *position)
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (position) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        int trackFrame;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Lock ();
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        trackFrame = GetCurrentFrame ();
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        *position = cdrom->track[currentTrack].offset + trackFrame;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return status;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Start playback */
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int startFrame, stopFrame;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FSRef *ref;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD) {
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError (kErrorFakeDevice);
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock();
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (LoadTracks (cdrom) < 0)
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -2;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (PauseFile () < 0)
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -3;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ReleaseFile () < 0)
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -4;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ref = GetFileForOffset (cdrom, start, length, &startFrame, &stopFrame);
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ref == NULL) {
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError ("SDL_SYS_CDPlay: No file for start=%d, length=%d", start, length);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -5;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (LoadFile (ref, startFrame, stopFrame) < 0)
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -6;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SetCompletionProc (CompletionProc, cdrom);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (PlayFile () < 0)
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -7;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = CD_PLAYING;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Unlock();
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Pause playback */
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom)
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD) {
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError (kErrorFakeDevice);
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock ();
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (PauseFile () < 0) {
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -2;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = CD_PAUSED;
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Unlock ();
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Resume playback */
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom)
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD) {
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError (kErrorFakeDevice);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock ();
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (PlayFile () < 0) {
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -2;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = CD_PLAYING;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Unlock ();
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Stop playback */
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom)
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD) {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError (kErrorFakeDevice);
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock ();
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (PauseFile () < 0) {
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -2;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ReleaseFile () < 0) {
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -3;
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = CD_STOPPED;
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Unlock ();
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Eject the CD-ROM (Unmount the volume) */
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom)
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    OSStatus err;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pid_t dissenter;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (fakeCD) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError (kErrorFakeDevice);
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lock ();
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (PauseFile () < 0) {
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -2;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ReleaseFile () < 0) {
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -3;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = CD_STOPPED;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Eject the volume */
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	err = FSEjectVolumeSync(volumes[cdrom->id], kNilOptions, &dissenter);
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (err != noErr) {
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Unlock ();
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError ("PBUnmountVol returned %d", err);
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -4;
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    status = CD_TRAYEMPTY;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Invalidate volume and track info */
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    volumes[cdrom->id] = 0;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    free (tracks[cdrom->id]);
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    tracks[cdrom->id] = NULL;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Unlock ();
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Close the CD-ROM */
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom)
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    currentDrive = -1;
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_CDROM_MACOSX */
515