Oops...
authorSam Lantinga <slouken@libsdl.org>
Sat, 26 Apr 2003 06:40:01 +0000
changeset 61612c746afbc27
parent 615 7ec821f3cbd0
child 617 86e0385b8f96
Oops...
src/cdrom/macosx/SDL_syscdrom.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cdrom/macosx/SDL_syscdrom.c	Sat Apr 26 06:40:01 2003 +0000
     1.3 @@ -0,0 +1,484 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +#include "SDL_syscdrom_c.h"
    1.32 +
    1.33 +#pragma mark -- Globals --
    1.34 +
    1.35 +static FSRef**         tracks;
    1.36 +static FSVolumeRefNum* volumes;
    1.37 +static CDstatus        status;
    1.38 +static int             nextTrackFrame;
    1.39 +static int             nextTrackFramesRemaining;
    1.40 +static int             fakeCD;
    1.41 +static int             currentTrack;
    1.42 +static int             didReadTOC;
    1.43 +static int             cacheTOCNumTracks;
    1.44 +static int             currentDrive; /* Only allow 1 drive in use at a time */
    1.45 +
    1.46 +#pragma mark -- Prototypes --
    1.47 +
    1.48 +static const char *SDL_SYS_CDName   (int drive);
    1.49 +static int         SDL_SYS_CDOpen   (int drive);
    1.50 +static int         SDL_SYS_CDGetTOC (SDL_CD *cdrom);
    1.51 +static CDstatus    SDL_SYS_CDStatus (SDL_CD *cdrom, int *position);
    1.52 +static int         SDL_SYS_CDPlay   (SDL_CD *cdrom, int start, int length);
    1.53 +static int         SDL_SYS_CDPause  (SDL_CD *cdrom);
    1.54 +static int         SDL_SYS_CDResume (SDL_CD *cdrom);
    1.55 +static int         SDL_SYS_CDStop   (SDL_CD *cdrom);
    1.56 +static int         SDL_SYS_CDEject  (SDL_CD *cdrom);
    1.57 +static void        SDL_SYS_CDClose  (SDL_CD *cdrom);
    1.58 +
    1.59 +#pragma mark -- Helper Functions --
    1.60 +
    1.61 +/* Read a list of tracks from the volume */
    1.62 +static int LoadTracks (SDL_CD *cdrom)
    1.63 +{
    1.64 +    /* Check if tracks are already loaded */
    1.65 +    if  ( tracks[cdrom->id] != NULL )
    1.66 +        return 0;
    1.67 +        
    1.68 +    /* Allocate memory for tracks */
    1.69 +    tracks[cdrom->id] = (FSRef*) calloc (1, sizeof(**tracks) * cdrom->numtracks);
    1.70 +    if (tracks[cdrom->id] == NULL) {
    1.71 +        SDL_OutOfMemory ();
    1.72 +        return -1;
    1.73 +    }
    1.74 +    
    1.75 +    /* Load tracks */
    1.76 +    if (ListTrackFiles (volumes[cdrom->id], tracks[cdrom->id], cdrom->numtracks) < 0)
    1.77 +        return -1;
    1.78 +
    1.79 +    return 0;
    1.80 +}
    1.81 +
    1.82 +/* Find a file for a given start frame and length */
    1.83 +static FSRef* GetFileForOffset (SDL_CD *cdrom, int start, int length,  int *outStartFrame, int *outStopFrame)
    1.84 +{
    1.85 +    int i;
    1.86 +    
    1.87 +    for (i = 0; i < cdrom->numtracks; i++) {
    1.88 +    
    1.89 +        if (cdrom->track[i].offset <= start &&
    1.90 +            start < (cdrom->track[i].offset + cdrom->track[i].length))
    1.91 +            break;
    1.92 +    }
    1.93 +    
    1.94 +    if (i == cdrom->numtracks)
    1.95 +        return NULL;
    1.96 +        
    1.97 +    currentTrack = i;
    1.98 +
    1.99 +    *outStartFrame = start - cdrom->track[i].offset;
   1.100 +    
   1.101 +    if ((*outStartFrame + length) < cdrom->track[i].length) {
   1.102 +        *outStopFrame = *outStartFrame + length;
   1.103 +        length = 0;
   1.104 +        nextTrackFrame = -1;
   1.105 +        nextTrackFramesRemaining = -1;
   1.106 +    }
   1.107 +    else {
   1.108 +        *outStopFrame = -1;
   1.109 +        length -= cdrom->track[i].length - *outStartFrame;
   1.110 +        nextTrackFrame = cdrom->track[i+1].offset;
   1.111 +        nextTrackFramesRemaining = length;
   1.112 +    }
   1.113 +    
   1.114 +    return &tracks[cdrom->id][i];
   1.115 +}
   1.116 +
   1.117 +/* Setup another file for playback, or stop playback (called from another thread) */
   1.118 +static void CompletionProc (SDL_CD *cdrom)
   1.119 +{
   1.120 +    
   1.121 +    Lock ();
   1.122 +    
   1.123 +    if (nextTrackFrame > 0 && nextTrackFramesRemaining > 0) {
   1.124 +    
   1.125 +        /* Load the next file to play */
   1.126 +        int startFrame, stopFrame;
   1.127 +        FSRef *file;
   1.128 +        
   1.129 +        PauseFile ();
   1.130 +        ReleaseFile ();
   1.131 +                
   1.132 +        file = GetFileForOffset (cdrom, nextTrackFrame, 
   1.133 +            nextTrackFramesRemaining, &startFrame, &stopFrame);
   1.134 +        
   1.135 +        if (file == NULL) {
   1.136 +            status = CD_STOPPED;
   1.137 +            Unlock ();
   1.138 +            return;
   1.139 +        }
   1.140 +        
   1.141 +        LoadFile (file, startFrame, stopFrame);
   1.142 +        
   1.143 +        SetCompletionProc (CompletionProc, cdrom);
   1.144 +        
   1.145 +        PlayFile ();
   1.146 +    }
   1.147 +    else {
   1.148 +    
   1.149 +        /* Release the current file */
   1.150 +        PauseFile ();
   1.151 +        ReleaseFile ();
   1.152 +        status = CD_STOPPED;
   1.153 +    }
   1.154 +    
   1.155 +    Unlock ();
   1.156 +}
   1.157 +
   1.158 +
   1.159 +#pragma mark -- Driver Functions --
   1.160 +
   1.161 +/* Initialize */
   1.162 +int SDL_SYS_CDInit (void) 
   1.163 +{
   1.164 +    /* Initialize globals */
   1.165 +    volumes = NULL;
   1.166 +    tracks  = NULL;
   1.167 +    status  = CD_STOPPED;
   1.168 +    nextTrackFrame = -1;
   1.169 +    nextTrackFramesRemaining = -1;
   1.170 +    fakeCD  = SDL_FALSE;
   1.171 +    currentTrack = -1;
   1.172 +    didReadTOC = SDL_FALSE;
   1.173 +    cacheTOCNumTracks = -1;
   1.174 +    currentDrive = -1;
   1.175 +    
   1.176 +    /* Fill in function pointers */
   1.177 +    SDL_CDcaps.Name   = SDL_SYS_CDName;
   1.178 +    SDL_CDcaps.Open   = SDL_SYS_CDOpen;
   1.179 +    SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   1.180 +    SDL_CDcaps.Status = SDL_SYS_CDStatus;
   1.181 +    SDL_CDcaps.Play   = SDL_SYS_CDPlay;
   1.182 +    SDL_CDcaps.Pause  = SDL_SYS_CDPause;
   1.183 +    SDL_CDcaps.Resume = SDL_SYS_CDResume;
   1.184 +    SDL_CDcaps.Stop   = SDL_SYS_CDStop;
   1.185 +    SDL_CDcaps.Eject  = SDL_SYS_CDEject;
   1.186 +    SDL_CDcaps.Close  = SDL_SYS_CDClose;
   1.187 +
   1.188 +    /* 
   1.189 +        Read the list of "drives"
   1.190 +        
   1.191 +        This is currently a hack that infers drives from
   1.192 +        mounted audio CD volumes, rather than
   1.193 +        actual CD-ROM devices - which means it may not
   1.194 +        act as expected sometimes.
   1.195 +    */
   1.196 +    
   1.197 +    /* Find out how many cd volumes are mounted */
   1.198 +    SDL_numcds = DetectAudioCDVolumes (NULL, 0);
   1.199 +
   1.200 +    /*
   1.201 +        If there are no volumes, fake a cd device
   1.202 +        so tray empty can be reported.
   1.203 +    */
   1.204 +    if (SDL_numcds == 0) {
   1.205 +    
   1.206 +        fakeCD = SDL_TRUE;
   1.207 +        SDL_numcds = 1;
   1.208 +        status = CD_TRAYEMPTY;
   1.209 +        
   1.210 +        return 0;
   1.211 +    }
   1.212 +    
   1.213 +    /* Allocate space for volumes */
   1.214 +    volumes = (FSVolumeRefNum*) calloc (1, sizeof(*volumes) * SDL_numcds);
   1.215 +    if (volumes == NULL) {
   1.216 +        SDL_OutOfMemory ();
   1.217 +        return -1;
   1.218 +    }
   1.219 +    
   1.220 +    /* Allocate space for tracks */
   1.221 +    tracks = (FSRef**) calloc (1, sizeof(*tracks) * (SDL_numcds + 1));
   1.222 +    if (tracks == NULL) {
   1.223 +        SDL_OutOfMemory ();
   1.224 +        return -1;
   1.225 +    }
   1.226 +    
   1.227 +    /* Mark the end of the tracks array */
   1.228 +    tracks[ SDL_numcds ] = (FSRef*)-1;
   1.229 +    
   1.230 +    /* 
   1.231 +        Redetect, now save all volumes for later
   1.232 +        Update SDL_numcds just in case it changed
   1.233 +    */
   1.234 +    {
   1.235 +        int numVolumes = SDL_numcds;
   1.236 +        
   1.237 +        SDL_numcds = DetectAudioCDVolumes (volumes, numVolumes);
   1.238 +        
   1.239 +        /* If more cds suddenly show up, ignore them */
   1.240 +        if (SDL_numcds > numVolumes) {
   1.241 +            SDL_SetError ("Some CD's were added but they will be ignored");
   1.242 +            SDL_numcds = numVolumes;
   1.243 +        }
   1.244 +    }
   1.245 +    
   1.246 +    return 0;
   1.247 +}
   1.248 +
   1.249 +/* Shutdown and cleanup */
   1.250 +void SDL_SYS_CDQuit(void)
   1.251 +{
   1.252 +    ReleaseFile();
   1.253 +    
   1.254 +    if (volumes != NULL)
   1.255 +        free (volumes);
   1.256 +        
   1.257 +    if (tracks != NULL) {
   1.258 +    
   1.259 +        FSRef **ptr;
   1.260 +        for (ptr = tracks; *ptr != (FSRef*)-1; ptr++)
   1.261 +            if (*ptr != NULL)
   1.262 +                free (*ptr);
   1.263 +            
   1.264 +        free (tracks);
   1.265 +    }
   1.266 +}
   1.267 +
   1.268 +/* Get the Unix disk name of the volume */
   1.269 +static const char *SDL_SYS_CDName (int drive)
   1.270 +{
   1.271 +    CFStringRef  diskID;
   1.272 +    OSStatus     err = noErr;
   1.273 +    
   1.274 +    if (fakeCD)
   1.275 +        return "Fake CD-ROM Device";
   1.276 +        
   1.277 +    err = FSCopyDiskIDForVolume (volumes[drive], &diskID);
   1.278 +    if (err != noErr) {
   1.279 +        SDL_SetError ("FSCopyDiskIDForVolume returned %d", err);
   1.280 +        return NULL;
   1.281 +    }
   1.282 +    
   1.283 +    return CFStringGetCStringPtr (diskID, 0);
   1.284 +}
   1.285 +
   1.286 +/* Open the "device" */
   1.287 +static int SDL_SYS_CDOpen (int drive)
   1.288 +{
   1.289 +    /* Only allow 1 device to be open */
   1.290 +    if (currentDrive >= 0) {
   1.291 +        SDL_SetError ("Only one cdrom is supported");
   1.292 +        return -1;
   1.293 +    }
   1.294 +    else
   1.295 +        currentDrive = drive;
   1.296 +
   1.297 +    return drive;
   1.298 +}
   1.299 +
   1.300 +/* Get the table of contents */
   1.301 +static int SDL_SYS_CDGetTOC (SDL_CD *cdrom)
   1.302 +{
   1.303 +    if (fakeCD) {
   1.304 +        SDL_SetError (kErrorFakeDevice);
   1.305 +        return -1;
   1.306 +    }
   1.307 +    
   1.308 +    if (didReadTOC) {
   1.309 +        cdrom->numtracks = cacheTOCNumTracks;
   1.310 +        return 0;
   1.311 +    }
   1.312 +    
   1.313 +    
   1.314 +    ReadTOCData (volumes[cdrom->id], cdrom);
   1.315 +    didReadTOC = SDL_TRUE;
   1.316 +    cacheTOCNumTracks = cdrom->numtracks;
   1.317 +    
   1.318 +    return 0;
   1.319 +}
   1.320 +
   1.321 +/* Get CD-ROM status */
   1.322 +static CDstatus SDL_SYS_CDStatus (SDL_CD *cdrom, int *position)
   1.323 +{
   1.324 +    int trackFrame;
   1.325 +    
   1.326 +    Lock ();
   1.327 +    trackFrame = GetCurrentFrame ();
   1.328 +    Unlock ();
   1.329 +    
   1.330 +    if (position)
   1.331 +	    *position = cdrom->track[currentTrack].offset + trackFrame;
   1.332 +    
   1.333 +    return status;
   1.334 +}
   1.335 +
   1.336 +/* Start playback */
   1.337 +static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
   1.338 +{
   1.339 +    int startFrame, stopFrame;
   1.340 +    FSRef *ref;
   1.341 +    
   1.342 +    if (fakeCD) {
   1.343 +        SDL_SetError (kErrorFakeDevice);
   1.344 +        return -1;
   1.345 +    }
   1.346 +    
   1.347 +    Lock();
   1.348 +    
   1.349 +    if (LoadTracks (cdrom) < 0)
   1.350 +        return -2;
   1.351 +    
   1.352 +    if (PauseFile () < 0)
   1.353 +        return -3;
   1.354 +        
   1.355 +    if (ReleaseFile () < 0)
   1.356 +        return -4;
   1.357 +    
   1.358 +    ref = GetFileForOffset (cdrom, start, length, &startFrame, &stopFrame);
   1.359 +    if (ref == NULL) {
   1.360 +        SDL_SetError ("SDL_SYS_CDPlay: No file for start=%d, length=%d", start, length);
   1.361 +        return -5;
   1.362 +    }
   1.363 +    
   1.364 +    if (LoadFile (ref, startFrame, stopFrame) < 0)
   1.365 +        return -6;
   1.366 +    
   1.367 +    SetCompletionProc (CompletionProc, cdrom);
   1.368 +    
   1.369 +    if (PlayFile () < 0)
   1.370 +        return -7;
   1.371 +    
   1.372 +    status = CD_PLAYING;
   1.373 +    
   1.374 +    Unlock();
   1.375 +    
   1.376 +    return 0;
   1.377 +}
   1.378 +
   1.379 +/* Pause playback */
   1.380 +static int SDL_SYS_CDPause(SDL_CD *cdrom)
   1.381 +{
   1.382 +    if (fakeCD) {
   1.383 +        SDL_SetError (kErrorFakeDevice);
   1.384 +        return -1;
   1.385 +    }
   1.386 +    
   1.387 +    Lock ();
   1.388 +    
   1.389 +    if (PauseFile () < 0)
   1.390 +        return -2;
   1.391 +    
   1.392 +    status = CD_PAUSED;
   1.393 +    
   1.394 +    Unlock ();
   1.395 +    
   1.396 +    return 0;
   1.397 +}
   1.398 +
   1.399 +/* Resume playback */
   1.400 +static int SDL_SYS_CDResume(SDL_CD *cdrom)
   1.401 +{
   1.402 +    if (fakeCD) {
   1.403 +        SDL_SetError (kErrorFakeDevice);
   1.404 +        return -1;
   1.405 +    }
   1.406 +    
   1.407 +    Lock ();
   1.408 +    
   1.409 +    if (PlayFile () < 0)
   1.410 +        return -2;
   1.411 +        
   1.412 +    status = CD_PLAYING;
   1.413 +    
   1.414 +    Unlock ();
   1.415 +    
   1.416 +    return 0;
   1.417 +}
   1.418 +
   1.419 +/* Stop playback */
   1.420 +static int SDL_SYS_CDStop(SDL_CD *cdrom)
   1.421 +{
   1.422 +    if (fakeCD) {
   1.423 +        SDL_SetError (kErrorFakeDevice);
   1.424 +        return -1;
   1.425 +    }
   1.426 +    
   1.427 +    Lock ();
   1.428 +    
   1.429 +    if (PauseFile () < 0)
   1.430 +        return -2;
   1.431 +        
   1.432 +    if (ReleaseFile () < 0)
   1.433 +        return -3;
   1.434 +        
   1.435 +    status = CD_STOPPED;
   1.436 +    
   1.437 +    Unlock ();
   1.438 +    
   1.439 +    return 0;
   1.440 +}
   1.441 +
   1.442 +/* Eject the CD-ROM (Unmount the volume) */
   1.443 +static int SDL_SYS_CDEject(SDL_CD *cdrom)
   1.444 +{
   1.445 +    OSStatus err;
   1.446 +    
   1.447 +    if (fakeCD) {
   1.448 +        SDL_SetError (kErrorFakeDevice);
   1.449 +        return -1;
   1.450 +    }
   1.451 +    
   1.452 +    Lock ();
   1.453 +    
   1.454 +    if (PauseFile () < 0)
   1.455 +        return -2;
   1.456 +        
   1.457 +    if (ReleaseFile () < 0)
   1.458 +        return -3;
   1.459 +    
   1.460 +    status = CD_STOPPED;
   1.461 +    
   1.462 +    err = FSEjectVolumeSync (volumes[cdrom->id], 0, NULL);
   1.463 +    
   1.464 +    if (err != noErr) {
   1.465 +        SDL_SetError ("FSEjectVolumeSync returned %d", err);
   1.466 +        return -4;
   1.467 +    }
   1.468 +    
   1.469 +    status = CD_TRAYEMPTY;
   1.470 +
   1.471 +    /* Invalidate volume and track info */
   1.472 +    volumes[cdrom->id] = 0;
   1.473 +    free (tracks[cdrom->id]);
   1.474 +    tracks[cdrom->id] = NULL;
   1.475 +    
   1.476 +    Unlock ();
   1.477 +    
   1.478 +    return 0;
   1.479 +}
   1.480 +
   1.481 +/* Close the CD-ROM */
   1.482 +static void SDL_SYS_CDClose(SDL_CD *cdrom)
   1.483 +{
   1.484 +    currentDrive = -1;
   1.485 +    return;
   1.486 +}
   1.487 +