src/cdrom/SDL_cdrom.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 29 Oct 2006 14:45:46 +0000
changeset 2063 dea73e1d07b0
parent 1978 542c78b6fb12
child 2698 e1da92da346c
permissions -rw-r--r--
Merged Ryan's fix, in case we need it later.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     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@0
     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@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    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@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* This is the CD-audio control API for Simple DirectMedia Layer */
slouken@0
    25
slouken@0
    26
#include "SDL_cdrom.h"
slouken@0
    27
#include "SDL_syscdrom.h"
slouken@0
    28
slouken@1895
    29
#define CLIP_FRAMES	10      /* Some CD-ROMs won't go all the way */
slouken@0
    30
slouken@0
    31
static int SDL_cdinitted = 0;
slouken@0
    32
static SDL_CD *default_cdrom;
slouken@0
    33
slouken@0
    34
/* The system level CD-ROM control functions */
slouken@0
    35
struct CDcaps SDL_CDcaps = {
slouken@1895
    36
    NULL,                       /* Name */
slouken@1895
    37
    NULL,                       /* Open */
slouken@1895
    38
    NULL,                       /* GetTOC */
slouken@1895
    39
    NULL,                       /* Status */
slouken@1895
    40
    NULL,                       /* Play */
slouken@1895
    41
    NULL,                       /* Pause */
slouken@1895
    42
    NULL,                       /* Resume */
slouken@1895
    43
    NULL,                       /* Stop */
slouken@1895
    44
    NULL,                       /* Eject */
slouken@1895
    45
    NULL,                       /* Close */
slouken@0
    46
};
slouken@0
    47
int SDL_numcds;
slouken@0
    48
slouken@1895
    49
int
slouken@1895
    50
SDL_CDROMInit(void)
slouken@0
    51
{
slouken@1895
    52
    int retval;
slouken@0
    53
slouken@1895
    54
    SDL_numcds = 0;
slouken@1895
    55
    retval = SDL_SYS_CDInit();
slouken@1895
    56
    if (retval == 0) {
slouken@1895
    57
        SDL_cdinitted = 1;
slouken@1895
    58
    }
slouken@1895
    59
    default_cdrom = NULL;
slouken@1895
    60
    return (retval);
slouken@0
    61
}
slouken@0
    62
slouken@0
    63
/* Check to see if the CD-ROM subsystem has been initialized */
slouken@1895
    64
static int
slouken@1895
    65
CheckInit(int check_cdrom, SDL_CD ** cdrom)
slouken@0
    66
{
slouken@1895
    67
    int okay;
slouken@0
    68
slouken@1895
    69
    okay = SDL_cdinitted;
slouken@1895
    70
    if (check_cdrom && (*cdrom == NULL)) {
slouken@1895
    71
        *cdrom = default_cdrom;
slouken@1895
    72
        if (*cdrom == NULL) {
slouken@1895
    73
            SDL_SetError("CD-ROM not opened");
slouken@1895
    74
            okay = 0;
slouken@1895
    75
        }
slouken@1895
    76
    }
slouken@1895
    77
    if (!SDL_cdinitted) {
slouken@1895
    78
        SDL_SetError("CD-ROM subsystem not initialized");
slouken@1895
    79
    }
slouken@1895
    80
    return (okay);
slouken@0
    81
}
slouken@0
    82
slouken@1895
    83
int
slouken@1895
    84
SDL_CDNumDrives(void)
slouken@0
    85
{
slouken@1895
    86
    if (!CheckInit(0, NULL)) {
slouken@1895
    87
        return (-1);
slouken@1895
    88
    }
slouken@1895
    89
    return (SDL_numcds);
slouken@0
    90
}
slouken@0
    91
slouken@1895
    92
const char *
slouken@1895
    93
SDL_CDName(int drive)
slouken@0
    94
{
slouken@1895
    95
    if (!CheckInit(0, NULL)) {
slouken@1895
    96
        return (NULL);
slouken@1895
    97
    }
slouken@1895
    98
    if (drive >= SDL_numcds) {
slouken@1895
    99
        SDL_SetError("Invalid CD-ROM drive index");
slouken@1895
   100
        return (NULL);
slouken@1895
   101
    }
slouken@1895
   102
    if (SDL_CDcaps.Name) {
slouken@1895
   103
        return (SDL_CDcaps.Name(drive));
slouken@1895
   104
    } else {
slouken@1895
   105
        return ("");
slouken@1895
   106
    }
slouken@0
   107
}
slouken@0
   108
slouken@1895
   109
SDL_CD *
slouken@1895
   110
SDL_CDOpen(int drive)
slouken@0
   111
{
slouken@1895
   112
    struct SDL_CD *cdrom;
slouken@0
   113
slouken@1895
   114
    if (!CheckInit(0, NULL)) {
slouken@1895
   115
        return (NULL);
slouken@1895
   116
    }
slouken@1895
   117
    if (drive >= SDL_numcds) {
slouken@1895
   118
        SDL_SetError("Invalid CD-ROM drive index");
slouken@1895
   119
        return (NULL);
slouken@1895
   120
    }
slouken@1895
   121
    cdrom = (SDL_CD *) SDL_malloc(sizeof(*cdrom));
slouken@1895
   122
    if (cdrom == NULL) {
slouken@1895
   123
        SDL_OutOfMemory();
slouken@1895
   124
        return (NULL);
slouken@1895
   125
    }
slouken@1895
   126
    SDL_memset(cdrom, 0, sizeof(*cdrom));
slouken@1895
   127
    cdrom->id = SDL_CDcaps.Open(drive);
slouken@1895
   128
    if (cdrom->id < 0) {
slouken@1895
   129
        SDL_free(cdrom);
slouken@1895
   130
        return (NULL);
slouken@1895
   131
    }
slouken@1895
   132
    default_cdrom = cdrom;
slouken@1895
   133
    return (cdrom);
slouken@0
   134
}
slouken@0
   135
slouken@1895
   136
CDstatus
slouken@1895
   137
SDL_CDStatus(SDL_CD * cdrom)
slouken@0
   138
{
slouken@1895
   139
    CDstatus status;
slouken@1895
   140
    int i;
slouken@1895
   141
    Uint32 position;
slouken@0
   142
slouken@1895
   143
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   144
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   145
        return (CD_ERROR);
slouken@1895
   146
    }
slouken@0
   147
slouken@1895
   148
    /* Get the current status of the drive */
slouken@1895
   149
    cdrom->numtracks = 0;
slouken@1895
   150
    cdrom->cur_track = 0;
slouken@1895
   151
    cdrom->cur_frame = 0;
slouken@1895
   152
    status = SDL_CDcaps.Status(cdrom, &i);
slouken@1895
   153
    position = (Uint32) i;
slouken@1895
   154
    cdrom->status = status;
slouken@0
   155
slouken@1895
   156
    /* Get the table of contents, if there's a CD available */
slouken@1895
   157
    if (CD_INDRIVE(status)) {
slouken@1895
   158
        if (SDL_CDcaps.GetTOC(cdrom) < 0) {
slouken@1895
   159
            status = CD_ERROR;
slouken@1895
   160
        }
slouken@1895
   161
        /* If the drive is playing, get current play position */
slouken@1895
   162
        if ((status == CD_PLAYING) || (status == CD_PAUSED)) {
slouken@1895
   163
            for (i = 1; cdrom->track[i].offset <= position; ++i) {
slouken@1895
   164
                /* Keep looking */ ;
slouken@1895
   165
            }
slouken@0
   166
#ifdef DEBUG_CDROM
slouken@1895
   167
            fprintf(stderr,
slouken@1895
   168
                    "Current position: %d, track = %d (offset is %d)\n",
slouken@1895
   169
                    position, i - 1, cdrom->track[i - 1].offset);
slouken@0
   170
#endif
slouken@1895
   171
            cdrom->cur_track = i - 1;
slouken@1895
   172
            position -= cdrom->track[cdrom->cur_track].offset;
slouken@1895
   173
            cdrom->cur_frame = position;
slouken@1895
   174
        }
slouken@1895
   175
    }
slouken@1895
   176
    return (status);
slouken@0
   177
}
slouken@0
   178
slouken@1895
   179
int
slouken@1895
   180
SDL_CDPlayTracks(SDL_CD * cdrom,
slouken@1895
   181
                 int strack, int sframe, int ntracks, int nframes)
slouken@0
   182
{
slouken@1895
   183
    int etrack, eframe;
slouken@1895
   184
    int start, length;
slouken@0
   185
slouken@1895
   186
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   187
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   188
        return (CD_ERROR);
slouken@1895
   189
    }
slouken@0
   190
slouken@1895
   191
    /* Determine the starting and ending tracks */
slouken@1895
   192
    if ((strack < 0) || (strack >= cdrom->numtracks)) {
slouken@1895
   193
        SDL_SetError("Invalid starting track");
slouken@1895
   194
        return (CD_ERROR);
slouken@1895
   195
    }
slouken@1895
   196
    if (!ntracks && !nframes) {
slouken@1895
   197
        etrack = cdrom->numtracks;
slouken@1895
   198
        eframe = 0;
slouken@1895
   199
    } else {
slouken@1895
   200
        etrack = strack + ntracks;
slouken@1895
   201
        if (etrack == strack) {
slouken@1895
   202
            eframe = sframe + nframes;
slouken@1895
   203
        } else {
slouken@1895
   204
            eframe = nframes;
slouken@1895
   205
        }
slouken@1895
   206
    }
slouken@1895
   207
    if (etrack > cdrom->numtracks) {
slouken@1895
   208
        SDL_SetError("Invalid play length");
slouken@1895
   209
        return (CD_ERROR);
slouken@1895
   210
    }
slouken@0
   211
slouken@1895
   212
    /* Skip data tracks and verify frame offsets */
slouken@1895
   213
    while ((strack <= etrack) &&
slouken@1895
   214
           (cdrom->track[strack].type == SDL_DATA_TRACK)) {
slouken@1895
   215
        ++strack;
slouken@1895
   216
    }
slouken@1895
   217
    if (sframe >= (int) cdrom->track[strack].length) {
slouken@1895
   218
        SDL_SetError("Invalid starting frame for track %d", strack);
slouken@1895
   219
        return (CD_ERROR);
slouken@1895
   220
    }
slouken@1895
   221
    while ((etrack > strack) &&
slouken@1895
   222
           (cdrom->track[etrack - 1].type == SDL_DATA_TRACK)) {
slouken@1895
   223
        --etrack;
slouken@1895
   224
    }
slouken@1895
   225
    if (eframe > (int) cdrom->track[etrack].length) {
slouken@1895
   226
        SDL_SetError("Invalid ending frame for track %d", etrack);
slouken@1895
   227
        return (CD_ERROR);
slouken@1895
   228
    }
slouken@0
   229
slouken@1895
   230
    /* Determine start frame and play length */
slouken@1895
   231
    start = (cdrom->track[strack].offset + sframe);
slouken@1895
   232
    length = (cdrom->track[etrack].offset + eframe) - start;
slouken@0
   233
#ifdef CLIP_FRAMES
slouken@1895
   234
    /* I've never seen this necessary, but xmcd does it.. */
slouken@1895
   235
    length -= CLIP_FRAMES;      /* CLIP_FRAMES == 10 */
slouken@0
   236
#endif
slouken@1895
   237
    if (length < 0) {
slouken@1895
   238
        return (0);
slouken@1895
   239
    }
slouken@0
   240
slouken@1895
   241
    /* Play! */
slouken@0
   242
#ifdef DEBUG_CDROM
slouken@1895
   243
    fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
slouken@0
   244
#endif
slouken@1895
   245
    return (SDL_CDcaps.Play(cdrom, start, length));
slouken@0
   246
}
slouken@0
   247
slouken@1895
   248
int
slouken@1895
   249
SDL_CDPlay(SDL_CD * cdrom, int sframe, int length)
slouken@0
   250
{
slouken@1895
   251
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   252
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   253
        return (CD_ERROR);
slouken@1895
   254
    }
slouken@0
   255
slouken@1895
   256
    return (SDL_CDcaps.Play(cdrom, sframe, length));
slouken@0
   257
}
slouken@0
   258
slouken@1895
   259
int
slouken@1895
   260
SDL_CDPause(SDL_CD * cdrom)
slouken@0
   261
{
slouken@1895
   262
    CDstatus status;
slouken@1895
   263
    int retval;
slouken@0
   264
slouken@1895
   265
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   266
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   267
        return (CD_ERROR);
slouken@1895
   268
    }
slouken@0
   269
slouken@1895
   270
    status = SDL_CDcaps.Status(cdrom, NULL);
slouken@1895
   271
    switch (status) {
slouken@1895
   272
    case CD_PLAYING:
slouken@1895
   273
        retval = SDL_CDcaps.Pause(cdrom);
slouken@1895
   274
        break;
slouken@1895
   275
    default:
slouken@1895
   276
        retval = 0;
slouken@1895
   277
        break;
slouken@1895
   278
    }
slouken@1895
   279
    return (retval);
slouken@0
   280
}
slouken@0
   281
slouken@1895
   282
int
slouken@1895
   283
SDL_CDResume(SDL_CD * cdrom)
slouken@0
   284
{
slouken@1895
   285
    CDstatus status;
slouken@1895
   286
    int retval;
slouken@0
   287
slouken@1895
   288
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   289
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   290
        return (CD_ERROR);
slouken@1895
   291
    }
slouken@0
   292
slouken@1895
   293
    status = SDL_CDcaps.Status(cdrom, NULL);
slouken@1895
   294
    switch (status) {
slouken@1895
   295
    case CD_PAUSED:
slouken@1895
   296
        retval = SDL_CDcaps.Resume(cdrom);
slouken@1895
   297
    default:
slouken@1895
   298
        retval = 0;
slouken@1895
   299
        break;
slouken@1895
   300
    }
slouken@1895
   301
    return (retval);
slouken@0
   302
}
slouken@0
   303
slouken@1895
   304
int
slouken@1895
   305
SDL_CDStop(SDL_CD * cdrom)
slouken@0
   306
{
slouken@1895
   307
    CDstatus status;
slouken@1895
   308
    int retval;
slouken@0
   309
slouken@1895
   310
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   311
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   312
        return (CD_ERROR);
slouken@1895
   313
    }
slouken@0
   314
slouken@1895
   315
    status = SDL_CDcaps.Status(cdrom, NULL);
slouken@1895
   316
    switch (status) {
slouken@1895
   317
    case CD_PLAYING:
slouken@1895
   318
    case CD_PAUSED:
slouken@1895
   319
        retval = SDL_CDcaps.Stop(cdrom);
slouken@1895
   320
    default:
slouken@1895
   321
        retval = 0;
slouken@1895
   322
        break;
slouken@1895
   323
    }
slouken@1895
   324
    return (retval);
slouken@0
   325
}
slouken@0
   326
slouken@1895
   327
int
slouken@1895
   328
SDL_CDEject(SDL_CD * cdrom)
slouken@0
   329
{
slouken@1895
   330
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   331
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   332
        return (CD_ERROR);
slouken@1895
   333
    }
slouken@1895
   334
    return (SDL_CDcaps.Eject(cdrom));
slouken@0
   335
}
slouken@0
   336
slouken@1895
   337
void
slouken@1895
   338
SDL_CDClose(SDL_CD * cdrom)
slouken@0
   339
{
slouken@1895
   340
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   341
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   342
        return;
slouken@1895
   343
    }
slouken@1895
   344
    SDL_CDcaps.Close(cdrom);
slouken@1895
   345
    SDL_free(cdrom);
slouken@1895
   346
    default_cdrom = NULL;
slouken@0
   347
}
slouken@0
   348
slouken@1895
   349
void
slouken@1895
   350
SDL_CDROMQuit(void)
slouken@0
   351
{
slouken@1895
   352
    SDL_SYS_CDQuit();
slouken@1895
   353
    SDL_cdinitted = 0;
slouken@0
   354
}
slouken@1895
   355
slouken@1895
   356
/* vi: set ts=4 sw=4 expandtab: */