src/cdrom/macosx/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Mar 2006 06:33:21 +0000
changeset 1487 dc6b59e925a2
parent 1402 d910939febfa
child 1635 92947e3a18db
permissions -rw-r--r--
Cleaning up warnings on MacOS X
slouken@616
     1
/*
slouken@616
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@616
     4
slouken@616
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@616
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@616
     9
slouken@616
    10
    This library is distributed in the hope that it will be useful,
slouken@616
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@616
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@616
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@616
    18
slouken@616
    19
    Sam Lantinga
slouken@616
    20
    slouken@libsdl.org
slouken@616
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@616
    23
slouken@616
    24
#include "SDL_syscdrom_c.h"
slouken@616
    25
slouken@616
    26
#pragma mark -- Globals --
slouken@616
    27
slouken@616
    28
static FSRef**         tracks;
slouken@616
    29
static FSVolumeRefNum* volumes;
slouken@616
    30
static CDstatus        status;
slouken@616
    31
static int             nextTrackFrame;
slouken@616
    32
static int             nextTrackFramesRemaining;
slouken@616
    33
static int             fakeCD;
slouken@616
    34
static int             currentTrack;
slouken@616
    35
static int             didReadTOC;
slouken@616
    36
static int             cacheTOCNumTracks;
slouken@616
    37
static int             currentDrive; /* Only allow 1 drive in use at a time */
slouken@616
    38
slouken@616
    39
#pragma mark -- Prototypes --
slouken@616
    40
slouken@616
    41
static const char *SDL_SYS_CDName   (int drive);
slouken@616
    42
static int         SDL_SYS_CDOpen   (int drive);
slouken@616
    43
static int         SDL_SYS_CDGetTOC (SDL_CD *cdrom);
slouken@616
    44
static CDstatus    SDL_SYS_CDStatus (SDL_CD *cdrom, int *position);
slouken@616
    45
static int         SDL_SYS_CDPlay   (SDL_CD *cdrom, int start, int length);
slouken@616
    46
static int         SDL_SYS_CDPause  (SDL_CD *cdrom);
slouken@616
    47
static int         SDL_SYS_CDResume (SDL_CD *cdrom);
slouken@616
    48
static int         SDL_SYS_CDStop   (SDL_CD *cdrom);
slouken@616
    49
static int         SDL_SYS_CDEject  (SDL_CD *cdrom);
slouken@616
    50
static void        SDL_SYS_CDClose  (SDL_CD *cdrom);
slouken@616
    51
slouken@616
    52
#pragma mark -- Helper Functions --
slouken@616
    53
slouken@616
    54
/* Read a list of tracks from the volume */
slouken@616
    55
static int LoadTracks (SDL_CD *cdrom)
slouken@616
    56
{
slouken@616
    57
    /* Check if tracks are already loaded */
slouken@616
    58
    if  ( tracks[cdrom->id] != NULL )
slouken@616
    59
        return 0;
slouken@616
    60
        
slouken@616
    61
    /* Allocate memory for tracks */
slouken@1338
    62
    tracks[cdrom->id] = (FSRef*) SDL_calloc (1, sizeof(**tracks) * cdrom->numtracks);
slouken@616
    63
    if (tracks[cdrom->id] == NULL) {
slouken@616
    64
        SDL_OutOfMemory ();
slouken@616
    65
        return -1;
slouken@616
    66
    }
slouken@616
    67
    
slouken@616
    68
    /* Load tracks */
slouken@616
    69
    if (ListTrackFiles (volumes[cdrom->id], tracks[cdrom->id], cdrom->numtracks) < 0)
slouken@616
    70
        return -1;
slouken@616
    71
slouken@616
    72
    return 0;
slouken@616
    73
}
slouken@616
    74
slouken@616
    75
/* Find a file for a given start frame and length */
slouken@616
    76
static FSRef* GetFileForOffset (SDL_CD *cdrom, int start, int length,  int *outStartFrame, int *outStopFrame)
slouken@616
    77
{
slouken@616
    78
    int i;
slouken@616
    79
    
slouken@616
    80
    for (i = 0; i < cdrom->numtracks; i++) {
slouken@616
    81
    
slouken@616
    82
        if (cdrom->track[i].offset <= start &&
slouken@616
    83
            start < (cdrom->track[i].offset + cdrom->track[i].length))
slouken@616
    84
            break;
slouken@616
    85
    }
slouken@616
    86
    
slouken@616
    87
    if (i == cdrom->numtracks)
slouken@616
    88
        return NULL;
slouken@616
    89
        
slouken@616
    90
    currentTrack = i;
slouken@616
    91
slouken@616
    92
    *outStartFrame = start - cdrom->track[i].offset;
slouken@616
    93
    
slouken@616
    94
    if ((*outStartFrame + length) < cdrom->track[i].length) {
slouken@616
    95
        *outStopFrame = *outStartFrame + length;
slouken@616
    96
        length = 0;
slouken@616
    97
        nextTrackFrame = -1;
slouken@616
    98
        nextTrackFramesRemaining = -1;
slouken@616
    99
    }
slouken@616
   100
    else {
slouken@616
   101
        *outStopFrame = -1;
slouken@616
   102
        length -= cdrom->track[i].length - *outStartFrame;
slouken@616
   103
        nextTrackFrame = cdrom->track[i+1].offset;
slouken@616
   104
        nextTrackFramesRemaining = length;
slouken@616
   105
    }
slouken@616
   106
    
slouken@616
   107
    return &tracks[cdrom->id][i];
slouken@616
   108
}
slouken@616
   109
slouken@616
   110
/* Setup another file for playback, or stop playback (called from another thread) */
slouken@616
   111
static void CompletionProc (SDL_CD *cdrom)
slouken@616
   112
{
slouken@616
   113
    
slouken@616
   114
    Lock ();
slouken@616
   115
    
slouken@616
   116
    if (nextTrackFrame > 0 && nextTrackFramesRemaining > 0) {
slouken@616
   117
    
slouken@616
   118
        /* Load the next file to play */
slouken@616
   119
        int startFrame, stopFrame;
slouken@616
   120
        FSRef *file;
slouken@616
   121
        
slouken@616
   122
        PauseFile ();
slouken@616
   123
        ReleaseFile ();
slouken@616
   124
                
slouken@616
   125
        file = GetFileForOffset (cdrom, nextTrackFrame, 
slouken@616
   126
            nextTrackFramesRemaining, &startFrame, &stopFrame);
slouken@616
   127
        
slouken@616
   128
        if (file == NULL) {
slouken@616
   129
            status = CD_STOPPED;
slouken@616
   130
            Unlock ();
slouken@616
   131
            return;
slouken@616
   132
        }
slouken@616
   133
        
slouken@616
   134
        LoadFile (file, startFrame, stopFrame);
slouken@616
   135
        
slouken@616
   136
        SetCompletionProc (CompletionProc, cdrom);
slouken@616
   137
        
slouken@616
   138
        PlayFile ();
slouken@616
   139
    }
slouken@616
   140
    else {
slouken@616
   141
    
slouken@616
   142
        /* Release the current file */
slouken@616
   143
        PauseFile ();
slouken@616
   144
        ReleaseFile ();
slouken@616
   145
        status = CD_STOPPED;
slouken@616
   146
    }
slouken@616
   147
    
slouken@616
   148
    Unlock ();
slouken@616
   149
}
slouken@616
   150
slouken@616
   151
slouken@616
   152
#pragma mark -- Driver Functions --
slouken@616
   153
slouken@616
   154
/* Initialize */
slouken@616
   155
int SDL_SYS_CDInit (void) 
slouken@616
   156
{
slouken@616
   157
    /* Initialize globals */
slouken@616
   158
    volumes = NULL;
slouken@616
   159
    tracks  = NULL;
slouken@616
   160
    status  = CD_STOPPED;
slouken@616
   161
    nextTrackFrame = -1;
slouken@616
   162
    nextTrackFramesRemaining = -1;
slouken@616
   163
    fakeCD  = SDL_FALSE;
slouken@616
   164
    currentTrack = -1;
slouken@616
   165
    didReadTOC = SDL_FALSE;
slouken@616
   166
    cacheTOCNumTracks = -1;
slouken@616
   167
    currentDrive = -1;
slouken@616
   168
    
slouken@616
   169
    /* Fill in function pointers */
slouken@616
   170
    SDL_CDcaps.Name   = SDL_SYS_CDName;
slouken@616
   171
    SDL_CDcaps.Open   = SDL_SYS_CDOpen;
slouken@616
   172
    SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
slouken@616
   173
    SDL_CDcaps.Status = SDL_SYS_CDStatus;
slouken@616
   174
    SDL_CDcaps.Play   = SDL_SYS_CDPlay;
slouken@616
   175
    SDL_CDcaps.Pause  = SDL_SYS_CDPause;
slouken@616
   176
    SDL_CDcaps.Resume = SDL_SYS_CDResume;
slouken@616
   177
    SDL_CDcaps.Stop   = SDL_SYS_CDStop;
slouken@616
   178
    SDL_CDcaps.Eject  = SDL_SYS_CDEject;
slouken@616
   179
    SDL_CDcaps.Close  = SDL_SYS_CDClose;
slouken@616
   180
slouken@616
   181
    /* 
slouken@616
   182
        Read the list of "drives"
slouken@616
   183
        
slouken@616
   184
        This is currently a hack that infers drives from
slouken@616
   185
        mounted audio CD volumes, rather than
slouken@616
   186
        actual CD-ROM devices - which means it may not
slouken@616
   187
        act as expected sometimes.
slouken@616
   188
    */
slouken@616
   189
    
slouken@616
   190
    /* Find out how many cd volumes are mounted */
slouken@616
   191
    SDL_numcds = DetectAudioCDVolumes (NULL, 0);
slouken@616
   192
slouken@616
   193
    /*
slouken@616
   194
        If there are no volumes, fake a cd device
slouken@616
   195
        so tray empty can be reported.
slouken@616
   196
    */
slouken@616
   197
    if (SDL_numcds == 0) {
slouken@616
   198
    
slouken@616
   199
        fakeCD = SDL_TRUE;
slouken@616
   200
        SDL_numcds = 1;
slouken@616
   201
        status = CD_TRAYEMPTY;
slouken@616
   202
        
slouken@616
   203
        return 0;
slouken@616
   204
    }
slouken@616
   205
    
slouken@616
   206
    /* Allocate space for volumes */
slouken@1338
   207
    volumes = (FSVolumeRefNum*) SDL_calloc (1, sizeof(*volumes) * SDL_numcds);
slouken@616
   208
    if (volumes == NULL) {
slouken@616
   209
        SDL_OutOfMemory ();
slouken@616
   210
        return -1;
slouken@616
   211
    }
slouken@616
   212
    
slouken@616
   213
    /* Allocate space for tracks */
slouken@1338
   214
    tracks = (FSRef**) SDL_calloc (1, sizeof(*tracks) * (SDL_numcds + 1));
slouken@616
   215
    if (tracks == NULL) {
slouken@616
   216
        SDL_OutOfMemory ();
slouken@616
   217
        return -1;
slouken@616
   218
    }
slouken@616
   219
    
slouken@616
   220
    /* Mark the end of the tracks array */
slouken@616
   221
    tracks[ SDL_numcds ] = (FSRef*)-1;
slouken@616
   222
    
slouken@616
   223
    /* 
slouken@616
   224
        Redetect, now save all volumes for later
slouken@616
   225
        Update SDL_numcds just in case it changed
slouken@616
   226
    */
slouken@616
   227
    {
slouken@616
   228
        int numVolumes = SDL_numcds;
slouken@616
   229
        
slouken@616
   230
        SDL_numcds = DetectAudioCDVolumes (volumes, numVolumes);
slouken@616
   231
        
slouken@616
   232
        /* If more cds suddenly show up, ignore them */
slouken@616
   233
        if (SDL_numcds > numVolumes) {
slouken@616
   234
            SDL_SetError ("Some CD's were added but they will be ignored");
slouken@616
   235
            SDL_numcds = numVolumes;
slouken@616
   236
        }
slouken@616
   237
    }
slouken@616
   238
    
slouken@616
   239
    return 0;
slouken@616
   240
}
slouken@616
   241
slouken@616
   242
/* Shutdown and cleanup */
slouken@616
   243
void SDL_SYS_CDQuit(void)
slouken@616
   244
{
slouken@616
   245
    ReleaseFile();
slouken@616
   246
    
slouken@616
   247
    if (volumes != NULL)
slouken@616
   248
        free (volumes);
slouken@616
   249
        
slouken@616
   250
    if (tracks != NULL) {
slouken@616
   251
    
slouken@616
   252
        FSRef **ptr;
slouken@616
   253
        for (ptr = tracks; *ptr != (FSRef*)-1; ptr++)
slouken@616
   254
            if (*ptr != NULL)
slouken@616
   255
                free (*ptr);
slouken@616
   256
            
slouken@616
   257
        free (tracks);
slouken@616
   258
    }
slouken@616
   259
}
slouken@616
   260
slouken@616
   261
/* Get the Unix disk name of the volume */
slouken@616
   262
static const char *SDL_SYS_CDName (int drive)
slouken@616
   263
{
slouken@616
   264
    OSStatus     err = noErr;
slouken@768
   265
    HParamBlockRec  pb;
slouken@768
   266
    GetVolParmsInfoBuffer   volParmsInfo;
slouken@768
   267
   
slouken@616
   268
    if (fakeCD)
slouken@616
   269
        return "Fake CD-ROM Device";
slouken@768
   270
slouken@768
   271
    pb.ioParam.ioNamePtr = NULL;
slouken@768
   272
    pb.ioParam.ioVRefNum = volumes[drive];
slouken@768
   273
    pb.ioParam.ioBuffer = (Ptr)&volParmsInfo;
slouken@768
   274
    pb.ioParam.ioReqCount = (SInt32)sizeof(volParmsInfo);
slouken@768
   275
    err = PBHGetVolParmsSync(&pb);
slouken@768
   276
slouken@616
   277
    if (err != noErr) {
slouken@768
   278
        SDL_SetError ("PBHGetVolParmsSync returned %d", err);
slouken@616
   279
        return NULL;
slouken@616
   280
    }
slouken@768
   281
slouken@768
   282
    return volParmsInfo.vMDeviceID;
slouken@616
   283
}
slouken@616
   284
slouken@616
   285
/* Open the "device" */
slouken@616
   286
static int SDL_SYS_CDOpen (int drive)
slouken@616
   287
{
slouken@616
   288
    /* Only allow 1 device to be open */
slouken@616
   289
    if (currentDrive >= 0) {
slouken@616
   290
        SDL_SetError ("Only one cdrom is supported");
slouken@616
   291
        return -1;
slouken@616
   292
    }
slouken@616
   293
    else
slouken@616
   294
        currentDrive = drive;
slouken@616
   295
slouken@616
   296
    return drive;
slouken@616
   297
}
slouken@616
   298
slouken@616
   299
/* Get the table of contents */
slouken@616
   300
static int SDL_SYS_CDGetTOC (SDL_CD *cdrom)
slouken@616
   301
{
slouken@616
   302
    if (fakeCD) {
slouken@616
   303
        SDL_SetError (kErrorFakeDevice);
slouken@616
   304
        return -1;
slouken@616
   305
    }
slouken@616
   306
    
slouken@616
   307
    if (didReadTOC) {
slouken@616
   308
        cdrom->numtracks = cacheTOCNumTracks;
slouken@616
   309
        return 0;
slouken@616
   310
    }
slouken@616
   311
    
slouken@616
   312
    
slouken@616
   313
    ReadTOCData (volumes[cdrom->id], cdrom);
slouken@616
   314
    didReadTOC = SDL_TRUE;
slouken@616
   315
    cacheTOCNumTracks = cdrom->numtracks;
slouken@616
   316
    
slouken@616
   317
    return 0;
slouken@616
   318
}
slouken@616
   319
slouken@616
   320
/* Get CD-ROM status */
slouken@616
   321
static CDstatus SDL_SYS_CDStatus (SDL_CD *cdrom, int *position)
slouken@616
   322
{
slouken@768
   323
    if (position) {
slouken@768
   324
        int trackFrame;
slouken@768
   325
        
slouken@768
   326
        Lock ();
slouken@768
   327
        trackFrame = GetCurrentFrame ();
slouken@768
   328
        Unlock ();
slouken@616
   329
    
slouken@768
   330
        *position = cdrom->track[currentTrack].offset + trackFrame;
slouken@768
   331
    }
slouken@616
   332
    
slouken@616
   333
    return status;
slouken@616
   334
}
slouken@616
   335
slouken@616
   336
/* Start playback */
slouken@616
   337
static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
slouken@616
   338
{
slouken@616
   339
    int startFrame, stopFrame;
slouken@616
   340
    FSRef *ref;
slouken@616
   341
    
slouken@616
   342
    if (fakeCD) {
slouken@616
   343
        SDL_SetError (kErrorFakeDevice);
slouken@616
   344
        return -1;
slouken@616
   345
    }
slouken@616
   346
    
slouken@616
   347
    Lock();
slouken@616
   348
    
slouken@616
   349
    if (LoadTracks (cdrom) < 0)
slouken@616
   350
        return -2;
slouken@616
   351
    
slouken@616
   352
    if (PauseFile () < 0)
slouken@616
   353
        return -3;
slouken@616
   354
        
slouken@616
   355
    if (ReleaseFile () < 0)
slouken@616
   356
        return -4;
slouken@616
   357
    
slouken@616
   358
    ref = GetFileForOffset (cdrom, start, length, &startFrame, &stopFrame);
slouken@616
   359
    if (ref == NULL) {
slouken@616
   360
        SDL_SetError ("SDL_SYS_CDPlay: No file for start=%d, length=%d", start, length);
slouken@616
   361
        return -5;
slouken@616
   362
    }
slouken@616
   363
    
slouken@616
   364
    if (LoadFile (ref, startFrame, stopFrame) < 0)
slouken@616
   365
        return -6;
slouken@616
   366
    
slouken@616
   367
    SetCompletionProc (CompletionProc, cdrom);
slouken@616
   368
    
slouken@616
   369
    if (PlayFile () < 0)
slouken@616
   370
        return -7;
slouken@616
   371
    
slouken@616
   372
    status = CD_PLAYING;
slouken@616
   373
    
slouken@616
   374
    Unlock();
slouken@616
   375
    
slouken@616
   376
    return 0;
slouken@616
   377
}
slouken@616
   378
slouken@616
   379
/* Pause playback */
slouken@616
   380
static int SDL_SYS_CDPause(SDL_CD *cdrom)
slouken@616
   381
{
slouken@616
   382
    if (fakeCD) {
slouken@616
   383
        SDL_SetError (kErrorFakeDevice);
slouken@616
   384
        return -1;
slouken@616
   385
    }
slouken@616
   386
    
slouken@616
   387
    Lock ();
slouken@616
   388
    
slouken@768
   389
    if (PauseFile () < 0) {
slouken@768
   390
        Unlock ();
slouken@616
   391
        return -2;
slouken@768
   392
    }
slouken@616
   393
    
slouken@616
   394
    status = CD_PAUSED;
slouken@616
   395
    
slouken@616
   396
    Unlock ();
slouken@616
   397
    
slouken@616
   398
    return 0;
slouken@616
   399
}
slouken@616
   400
slouken@616
   401
/* Resume playback */
slouken@616
   402
static int SDL_SYS_CDResume(SDL_CD *cdrom)
slouken@616
   403
{
slouken@616
   404
    if (fakeCD) {
slouken@616
   405
        SDL_SetError (kErrorFakeDevice);
slouken@616
   406
        return -1;
slouken@616
   407
    }
slouken@616
   408
    
slouken@616
   409
    Lock ();
slouken@616
   410
    
icculus@1126
   411
    if (PlayFile () < 0) {
slouken@768
   412
        Unlock ();
slouken@616
   413
        return -2;
slouken@768
   414
    }
slouken@616
   415
        
slouken@616
   416
    status = CD_PLAYING;
slouken@616
   417
    
slouken@616
   418
    Unlock ();
slouken@616
   419
    
slouken@616
   420
    return 0;
slouken@616
   421
}
slouken@616
   422
slouken@616
   423
/* Stop playback */
slouken@616
   424
static int SDL_SYS_CDStop(SDL_CD *cdrom)
slouken@616
   425
{
slouken@616
   426
    if (fakeCD) {
slouken@616
   427
        SDL_SetError (kErrorFakeDevice);
slouken@616
   428
        return -1;
slouken@616
   429
    }
slouken@616
   430
    
slouken@616
   431
    Lock ();
slouken@616
   432
    
slouken@768
   433
    if (PauseFile () < 0) {
slouken@768
   434
        Unlock ();
slouken@616
   435
        return -2;
slouken@768
   436
    }
slouken@616
   437
        
slouken@768
   438
    if (ReleaseFile () < 0) {
slouken@768
   439
        Unlock ();
slouken@616
   440
        return -3;
slouken@768
   441
    }
slouken@616
   442
        
slouken@616
   443
    status = CD_STOPPED;
slouken@616
   444
    
slouken@616
   445
    Unlock ();
slouken@616
   446
    
slouken@616
   447
    return 0;
slouken@616
   448
}
slouken@616
   449
slouken@616
   450
/* Eject the CD-ROM (Unmount the volume) */
slouken@616
   451
static int SDL_SYS_CDEject(SDL_CD *cdrom)
slouken@616
   452
{
slouken@616
   453
    OSStatus err;
slouken@768
   454
	HParamBlockRec  pb;
slouken@616
   455
    
slouken@616
   456
    if (fakeCD) {
slouken@616
   457
        SDL_SetError (kErrorFakeDevice);
slouken@616
   458
        return -1;
slouken@616
   459
    }
slouken@616
   460
    
slouken@616
   461
    Lock ();
slouken@616
   462
    
slouken@768
   463
    if (PauseFile () < 0) {
slouken@768
   464
        Unlock ();
slouken@616
   465
        return -2;
slouken@768
   466
    }
slouken@616
   467
        
slouken@768
   468
    if (ReleaseFile () < 0) {
slouken@768
   469
        Unlock ();
slouken@616
   470
        return -3;
slouken@768
   471
    }
slouken@616
   472
    
slouken@616
   473
    status = CD_STOPPED;
slouken@616
   474
    
slouken@1487
   475
	/* Eject the volume */
slouken@768
   476
	pb.ioParam.ioNamePtr = NULL;
slouken@768
   477
	pb.ioParam.ioVRefNum = volumes[cdrom->id];
slouken@768
   478
	err = PBUnmountVol((ParamBlockRec *) &pb);
slouken@768
   479
slouken@768
   480
	if (err != noErr) {
slouken@768
   481
        Unlock ();
slouken@768
   482
		SDL_SetError ("PBUnmountVol returned %d", err);
slouken@768
   483
		return -4;
slouken@768
   484
	}
slouken@616
   485
    
slouken@616
   486
    status = CD_TRAYEMPTY;
slouken@616
   487
slouken@616
   488
    /* Invalidate volume and track info */
slouken@616
   489
    volumes[cdrom->id] = 0;
slouken@616
   490
    free (tracks[cdrom->id]);
slouken@616
   491
    tracks[cdrom->id] = NULL;
slouken@616
   492
    
slouken@616
   493
    Unlock ();
slouken@616
   494
    
slouken@616
   495
    return 0;
slouken@616
   496
}
slouken@616
   497
slouken@616
   498
/* Close the CD-ROM */
slouken@616
   499
static void SDL_SYS_CDClose(SDL_CD *cdrom)
slouken@616
   500
{
slouken@616
   501
    currentDrive = -1;
slouken@616
   502
    return;
slouken@616
   503
}
slouken@616
   504