src/cdrom/SDL_cdrom.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Aug 2008 15:10:03 +0000
changeset 2735 204be4fc2726
parent 1978 542c78b6fb12
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Port SDL 1.3 to the Nintendo DS
by Darren Alton, mentored by Sam Lantinga
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@2735
    47
slouken@0
    48
int SDL_numcds;
slouken@0
    49
slouken@1895
    50
int
slouken@1895
    51
SDL_CDROMInit(void)
slouken@0
    52
{
slouken@1895
    53
    int retval;
slouken@0
    54
slouken@1895
    55
    SDL_numcds = 0;
slouken@1895
    56
    retval = SDL_SYS_CDInit();
slouken@1895
    57
    if (retval == 0) {
slouken@1895
    58
        SDL_cdinitted = 1;
slouken@1895
    59
    }
slouken@1895
    60
    default_cdrom = NULL;
slouken@1895
    61
    return (retval);
slouken@0
    62
}
slouken@0
    63
slouken@0
    64
/* Check to see if the CD-ROM subsystem has been initialized */
slouken@1895
    65
static int
slouken@1895
    66
CheckInit(int check_cdrom, SDL_CD ** cdrom)
slouken@0
    67
{
slouken@1895
    68
    int okay;
slouken@0
    69
slouken@1895
    70
    okay = SDL_cdinitted;
slouken@1895
    71
    if (check_cdrom && (*cdrom == NULL)) {
slouken@1895
    72
        *cdrom = default_cdrom;
slouken@1895
    73
        if (*cdrom == NULL) {
slouken@1895
    74
            SDL_SetError("CD-ROM not opened");
slouken@1895
    75
            okay = 0;
slouken@1895
    76
        }
slouken@1895
    77
    }
slouken@1895
    78
    if (!SDL_cdinitted) {
slouken@1895
    79
        SDL_SetError("CD-ROM subsystem not initialized");
slouken@1895
    80
    }
slouken@1895
    81
    return (okay);
slouken@0
    82
}
slouken@0
    83
slouken@1895
    84
int
slouken@1895
    85
SDL_CDNumDrives(void)
slouken@0
    86
{
slouken@1895
    87
    if (!CheckInit(0, NULL)) {
slouken@1895
    88
        return (-1);
slouken@1895
    89
    }
slouken@1895
    90
    return (SDL_numcds);
slouken@0
    91
}
slouken@0
    92
slouken@1895
    93
const char *
slouken@1895
    94
SDL_CDName(int drive)
slouken@0
    95
{
slouken@1895
    96
    if (!CheckInit(0, NULL)) {
slouken@1895
    97
        return (NULL);
slouken@1895
    98
    }
slouken@1895
    99
    if (drive >= SDL_numcds) {
slouken@1895
   100
        SDL_SetError("Invalid CD-ROM drive index");
slouken@1895
   101
        return (NULL);
slouken@1895
   102
    }
slouken@1895
   103
    if (SDL_CDcaps.Name) {
slouken@1895
   104
        return (SDL_CDcaps.Name(drive));
slouken@1895
   105
    } else {
slouken@1895
   106
        return ("");
slouken@1895
   107
    }
slouken@0
   108
}
slouken@0
   109
slouken@1895
   110
SDL_CD *
slouken@1895
   111
SDL_CDOpen(int drive)
slouken@0
   112
{
slouken@1895
   113
    struct SDL_CD *cdrom;
slouken@0
   114
slouken@1895
   115
    if (!CheckInit(0, NULL)) {
slouken@1895
   116
        return (NULL);
slouken@1895
   117
    }
slouken@1895
   118
    if (drive >= SDL_numcds) {
slouken@1895
   119
        SDL_SetError("Invalid CD-ROM drive index");
slouken@1895
   120
        return (NULL);
slouken@1895
   121
    }
slouken@1895
   122
    cdrom = (SDL_CD *) SDL_malloc(sizeof(*cdrom));
slouken@1895
   123
    if (cdrom == NULL) {
slouken@1895
   124
        SDL_OutOfMemory();
slouken@1895
   125
        return (NULL);
slouken@1895
   126
    }
slouken@1895
   127
    SDL_memset(cdrom, 0, sizeof(*cdrom));
slouken@1895
   128
    cdrom->id = SDL_CDcaps.Open(drive);
slouken@1895
   129
    if (cdrom->id < 0) {
slouken@1895
   130
        SDL_free(cdrom);
slouken@1895
   131
        return (NULL);
slouken@1895
   132
    }
slouken@1895
   133
    default_cdrom = cdrom;
slouken@1895
   134
    return (cdrom);
slouken@0
   135
}
slouken@0
   136
slouken@1895
   137
CDstatus
slouken@1895
   138
SDL_CDStatus(SDL_CD * cdrom)
slouken@0
   139
{
slouken@1895
   140
    CDstatus status;
slouken@1895
   141
    int i;
slouken@1895
   142
    Uint32 position;
slouken@0
   143
slouken@1895
   144
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   145
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   146
        return (CD_ERROR);
slouken@1895
   147
    }
slouken@0
   148
slouken@1895
   149
    /* Get the current status of the drive */
slouken@1895
   150
    cdrom->numtracks = 0;
slouken@1895
   151
    cdrom->cur_track = 0;
slouken@1895
   152
    cdrom->cur_frame = 0;
slouken@1895
   153
    status = SDL_CDcaps.Status(cdrom, &i);
slouken@1895
   154
    position = (Uint32) i;
slouken@1895
   155
    cdrom->status = status;
slouken@0
   156
slouken@1895
   157
    /* Get the table of contents, if there's a CD available */
slouken@1895
   158
    if (CD_INDRIVE(status)) {
slouken@1895
   159
        if (SDL_CDcaps.GetTOC(cdrom) < 0) {
slouken@1895
   160
            status = CD_ERROR;
slouken@1895
   161
        }
slouken@1895
   162
        /* If the drive is playing, get current play position */
slouken@1895
   163
        if ((status == CD_PLAYING) || (status == CD_PAUSED)) {
slouken@1895
   164
            for (i = 1; cdrom->track[i].offset <= position; ++i) {
slouken@1895
   165
                /* Keep looking */ ;
slouken@1895
   166
            }
slouken@0
   167
#ifdef DEBUG_CDROM
slouken@1895
   168
            fprintf(stderr,
slouken@1895
   169
                    "Current position: %d, track = %d (offset is %d)\n",
slouken@1895
   170
                    position, i - 1, cdrom->track[i - 1].offset);
slouken@0
   171
#endif
slouken@1895
   172
            cdrom->cur_track = i - 1;
slouken@1895
   173
            position -= cdrom->track[cdrom->cur_track].offset;
slouken@1895
   174
            cdrom->cur_frame = position;
slouken@1895
   175
        }
slouken@1895
   176
    }
slouken@1895
   177
    return (status);
slouken@0
   178
}
slouken@0
   179
slouken@1895
   180
int
slouken@1895
   181
SDL_CDPlayTracks(SDL_CD * cdrom,
slouken@1895
   182
                 int strack, int sframe, int ntracks, int nframes)
slouken@0
   183
{
slouken@1895
   184
    int etrack, eframe;
slouken@1895
   185
    int start, length;
slouken@0
   186
slouken@1895
   187
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   188
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   189
        return (CD_ERROR);
slouken@1895
   190
    }
slouken@0
   191
slouken@1895
   192
    /* Determine the starting and ending tracks */
slouken@1895
   193
    if ((strack < 0) || (strack >= cdrom->numtracks)) {
slouken@1895
   194
        SDL_SetError("Invalid starting track");
slouken@1895
   195
        return (CD_ERROR);
slouken@1895
   196
    }
slouken@1895
   197
    if (!ntracks && !nframes) {
slouken@1895
   198
        etrack = cdrom->numtracks;
slouken@1895
   199
        eframe = 0;
slouken@1895
   200
    } else {
slouken@1895
   201
        etrack = strack + ntracks;
slouken@1895
   202
        if (etrack == strack) {
slouken@1895
   203
            eframe = sframe + nframes;
slouken@1895
   204
        } else {
slouken@1895
   205
            eframe = nframes;
slouken@1895
   206
        }
slouken@1895
   207
    }
slouken@1895
   208
    if (etrack > cdrom->numtracks) {
slouken@1895
   209
        SDL_SetError("Invalid play length");
slouken@1895
   210
        return (CD_ERROR);
slouken@1895
   211
    }
slouken@0
   212
slouken@1895
   213
    /* Skip data tracks and verify frame offsets */
slouken@1895
   214
    while ((strack <= etrack) &&
slouken@1895
   215
           (cdrom->track[strack].type == SDL_DATA_TRACK)) {
slouken@1895
   216
        ++strack;
slouken@1895
   217
    }
slouken@1895
   218
    if (sframe >= (int) cdrom->track[strack].length) {
slouken@1895
   219
        SDL_SetError("Invalid starting frame for track %d", strack);
slouken@1895
   220
        return (CD_ERROR);
slouken@1895
   221
    }
slouken@1895
   222
    while ((etrack > strack) &&
slouken@1895
   223
           (cdrom->track[etrack - 1].type == SDL_DATA_TRACK)) {
slouken@1895
   224
        --etrack;
slouken@1895
   225
    }
slouken@1895
   226
    if (eframe > (int) cdrom->track[etrack].length) {
slouken@1895
   227
        SDL_SetError("Invalid ending frame for track %d", etrack);
slouken@1895
   228
        return (CD_ERROR);
slouken@1895
   229
    }
slouken@0
   230
slouken@1895
   231
    /* Determine start frame and play length */
slouken@1895
   232
    start = (cdrom->track[strack].offset + sframe);
slouken@1895
   233
    length = (cdrom->track[etrack].offset + eframe) - start;
slouken@0
   234
#ifdef CLIP_FRAMES
slouken@1895
   235
    /* I've never seen this necessary, but xmcd does it.. */
slouken@1895
   236
    length -= CLIP_FRAMES;      /* CLIP_FRAMES == 10 */
slouken@0
   237
#endif
slouken@1895
   238
    if (length < 0) {
slouken@1895
   239
        return (0);
slouken@1895
   240
    }
slouken@0
   241
slouken@1895
   242
    /* Play! */
slouken@0
   243
#ifdef DEBUG_CDROM
slouken@1895
   244
    fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
slouken@0
   245
#endif
slouken@1895
   246
    return (SDL_CDcaps.Play(cdrom, start, length));
slouken@0
   247
}
slouken@0
   248
slouken@1895
   249
int
slouken@1895
   250
SDL_CDPlay(SDL_CD * cdrom, int sframe, int length)
slouken@0
   251
{
slouken@1895
   252
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   253
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   254
        return (CD_ERROR);
slouken@1895
   255
    }
slouken@0
   256
slouken@1895
   257
    return (SDL_CDcaps.Play(cdrom, sframe, length));
slouken@0
   258
}
slouken@0
   259
slouken@1895
   260
int
slouken@1895
   261
SDL_CDPause(SDL_CD * cdrom)
slouken@0
   262
{
slouken@1895
   263
    CDstatus status;
slouken@1895
   264
    int retval;
slouken@0
   265
slouken@1895
   266
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   267
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   268
        return (CD_ERROR);
slouken@1895
   269
    }
slouken@0
   270
slouken@1895
   271
    status = SDL_CDcaps.Status(cdrom, NULL);
slouken@1895
   272
    switch (status) {
slouken@1895
   273
    case CD_PLAYING:
slouken@1895
   274
        retval = SDL_CDcaps.Pause(cdrom);
slouken@1895
   275
        break;
slouken@1895
   276
    default:
slouken@1895
   277
        retval = 0;
slouken@1895
   278
        break;
slouken@1895
   279
    }
slouken@1895
   280
    return (retval);
slouken@0
   281
}
slouken@0
   282
slouken@1895
   283
int
slouken@1895
   284
SDL_CDResume(SDL_CD * cdrom)
slouken@0
   285
{
slouken@1895
   286
    CDstatus status;
slouken@1895
   287
    int retval;
slouken@0
   288
slouken@1895
   289
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   290
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   291
        return (CD_ERROR);
slouken@1895
   292
    }
slouken@0
   293
slouken@1895
   294
    status = SDL_CDcaps.Status(cdrom, NULL);
slouken@1895
   295
    switch (status) {
slouken@1895
   296
    case CD_PAUSED:
slouken@1895
   297
        retval = SDL_CDcaps.Resume(cdrom);
slouken@1895
   298
    default:
slouken@1895
   299
        retval = 0;
slouken@1895
   300
        break;
slouken@1895
   301
    }
slouken@1895
   302
    return (retval);
slouken@0
   303
}
slouken@0
   304
slouken@1895
   305
int
slouken@1895
   306
SDL_CDStop(SDL_CD * cdrom)
slouken@0
   307
{
slouken@1895
   308
    CDstatus status;
slouken@1895
   309
    int retval;
slouken@0
   310
slouken@1895
   311
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   312
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   313
        return (CD_ERROR);
slouken@1895
   314
    }
slouken@0
   315
slouken@1895
   316
    status = SDL_CDcaps.Status(cdrom, NULL);
slouken@1895
   317
    switch (status) {
slouken@1895
   318
    case CD_PLAYING:
slouken@1895
   319
    case CD_PAUSED:
slouken@1895
   320
        retval = SDL_CDcaps.Stop(cdrom);
slouken@1895
   321
    default:
slouken@1895
   322
        retval = 0;
slouken@1895
   323
        break;
slouken@1895
   324
    }
slouken@1895
   325
    return (retval);
slouken@0
   326
}
slouken@0
   327
slouken@1895
   328
int
slouken@1895
   329
SDL_CDEject(SDL_CD * cdrom)
slouken@0
   330
{
slouken@1895
   331
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   332
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   333
        return (CD_ERROR);
slouken@1895
   334
    }
slouken@1895
   335
    return (SDL_CDcaps.Eject(cdrom));
slouken@0
   336
}
slouken@0
   337
slouken@1895
   338
void
slouken@1895
   339
SDL_CDClose(SDL_CD * cdrom)
slouken@0
   340
{
slouken@1895
   341
    /* Check if the CD-ROM subsystem has been initialized */
slouken@1895
   342
    if (!CheckInit(1, &cdrom)) {
slouken@1895
   343
        return;
slouken@1895
   344
    }
slouken@1895
   345
    SDL_CDcaps.Close(cdrom);
slouken@1895
   346
    SDL_free(cdrom);
slouken@1895
   347
    default_cdrom = NULL;
slouken@0
   348
}
slouken@0
   349
slouken@1895
   350
void
slouken@1895
   351
SDL_CDROMQuit(void)
slouken@0
   352
{
slouken@1895
   353
    SDL_SYS_CDQuit();
slouken@1895
   354
    SDL_cdinitted = 0;
slouken@0
   355
}
slouken@1895
   356
slouken@1895
   357
/* vi: set ts=4 sw=4 expandtab: */