test/testcdrom.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
slouken@0
     2
/* Test the SDL CD-ROM audio functions */
slouken@0
     3
slouken@0
     4
#include <stdlib.h>
slouken@1484
     5
#include <stdio.h>
slouken@1484
     6
#include <string.h>
slouken@0
     7
#include <ctype.h>
slouken@0
     8
slouken@0
     9
#include "SDL.h"
slouken@0
    10
icculus@1151
    11
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1662
    12
static void
slouken@1668
    13
quit(int rc)
icculus@1151
    14
{
slouken@1668
    15
    SDL_Quit();
slouken@1668
    16
    exit(rc);
icculus@1151
    17
}
slouken@0
    18
slouken@1662
    19
static void
slouken@1668
    20
PrintStatus(int driveindex, SDL_CD * cdrom)
slouken@0
    21
{
slouken@1662
    22
    CDstatus status;
slouken@1662
    23
    char *status_str;
slouken@0
    24
slouken@1668
    25
    status = SDL_CDStatus(cdrom);
slouken@1662
    26
    switch (status) {
slouken@1662
    27
    case CD_TRAYEMPTY:
slouken@1662
    28
        status_str = "tray empty";
slouken@1662
    29
        break;
slouken@1662
    30
    case CD_STOPPED:
slouken@1662
    31
        status_str = "stopped";
slouken@1662
    32
        break;
slouken@1662
    33
    case CD_PLAYING:
slouken@1662
    34
        status_str = "playing";
slouken@1662
    35
        break;
slouken@1662
    36
    case CD_PAUSED:
slouken@1662
    37
        status_str = "paused";
slouken@1662
    38
        break;
slouken@1662
    39
    case CD_ERROR:
slouken@1662
    40
        status_str = "error state";
slouken@1662
    41
        break;
slouken@1662
    42
    }
slouken@1668
    43
    printf("Drive %d status: %s\n", driveindex, status_str);
slouken@1662
    44
    if (status >= CD_PLAYING) {
slouken@1662
    45
        int m, s, f;
slouken@1668
    46
        FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
slouken@1668
    47
        printf("Currently playing track %d, %d:%2.2d\n",
slouken@1668
    48
               cdrom->track[cdrom->cur_track].id, m, s);
slouken@1662
    49
    }
slouken@0
    50
}
slouken@0
    51
slouken@1662
    52
static void
slouken@1668
    53
ListTracks(SDL_CD * cdrom)
slouken@0
    54
{
slouken@1662
    55
    int i;
slouken@1662
    56
    int m, s, f;
slouken@1662
    57
    char *trtype;
slouken@0
    58
slouken@1668
    59
    SDL_CDStatus(cdrom);
slouken@1668
    60
    printf("Drive tracks: %d\n", cdrom->numtracks);
slouken@1662
    61
    for (i = 0; i < cdrom->numtracks; ++i) {
slouken@1668
    62
        FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
slouken@1662
    63
        if (f > 0)
slouken@1662
    64
            ++s;
slouken@1662
    65
        switch (cdrom->track[i].type) {
slouken@1662
    66
        case SDL_AUDIO_TRACK:
slouken@1662
    67
            trtype = "audio";
slouken@1662
    68
            break;
slouken@1662
    69
        case SDL_DATA_TRACK:
slouken@1662
    70
            trtype = "data";
slouken@1662
    71
            break;
slouken@1662
    72
        default:
slouken@1662
    73
            trtype = "unknown";
slouken@1662
    74
            break;
slouken@1662
    75
        }
slouken@1668
    76
        printf("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
slouken@1668
    77
               cdrom->track[i].id, m, s, cdrom->track[i].length, trtype);
slouken@1662
    78
    }
slouken@0
    79
}
slouken@0
    80
slouken@1662
    81
static void
slouken@1668
    82
PrintUsage(char *argv0)
slouken@0
    83
{
slouken@1668
    84
    fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
slouken@1668
    85
    fprintf(stderr, "Where 'command' is one of:\n");
slouken@1668
    86
    fprintf(stderr, "	-status\n");
slouken@1668
    87
    fprintf(stderr, "	-list\n");
slouken@1668
    88
    fprintf(stderr,
slouken@1668
    89
            "	-play [first_track] [first_frame] [num_tracks] [num_frames]\n");
slouken@1668
    90
    fprintf(stderr, "	-pause\n");
slouken@1668
    91
    fprintf(stderr, "	-resume\n");
slouken@1668
    92
    fprintf(stderr, "	-stop\n");
slouken@1668
    93
    fprintf(stderr, "	-eject\n");
slouken@1668
    94
    fprintf(stderr, "	-sleep <milliseconds>\n");
slouken@0
    95
}
slouken@0
    96
slouken@1662
    97
int
slouken@1668
    98
main(int argc, char *argv[])
slouken@0
    99
{
slouken@1662
   100
    int drive;
slouken@1662
   101
    int i;
slouken@1662
   102
    SDL_CD *cdrom;
slouken@0
   103
slouken@1662
   104
    /* Initialize SDL first */
slouken@1668
   105
    if (SDL_Init(SDL_INIT_CDROM) < 0) {
slouken@1668
   106
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1662
   107
        return (1);
slouken@1662
   108
    }
slouken@0
   109
slouken@1662
   110
    /* Find out how many CD-ROM drives are connected to the system */
slouken@1668
   111
    if (SDL_CDNumDrives() == 0) {
slouken@1668
   112
        printf("No CD-ROM devices detected\n");
slouken@1668
   113
        quit(0);
slouken@1662
   114
    }
slouken@1668
   115
    printf("Drives available: %d\n", SDL_CDNumDrives());
slouken@1668
   116
    for (i = 0; i < SDL_CDNumDrives(); ++i) {
slouken@1668
   117
        printf("Drive %d:  \"%s\"\n", i, SDL_CDName(i));
slouken@1662
   118
    }
slouken@0
   119
slouken@1662
   120
    /* Open the CD-ROM */
slouken@1662
   121
    drive = 0;
slouken@1662
   122
    i = 1;
slouken@1668
   123
    if (argv[i] && isdigit(argv[i][0])) {
slouken@1668
   124
        drive = atoi(argv[i++]);
slouken@1662
   125
    }
slouken@1668
   126
    cdrom = SDL_CDOpen(drive);
slouken@1662
   127
    if (cdrom == NULL) {
slouken@1668
   128
        fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
slouken@1668
   129
                SDL_GetError());
slouken@1668
   130
        quit(2);
slouken@1662
   131
    }
slouken@0
   132
#ifdef TEST_NULLCD
slouken@1662
   133
    cdrom = NULL;
slouken@0
   134
#endif
slouken@1662
   135
slouken@1662
   136
    /* Find out which function to perform */
slouken@1662
   137
    for (; argv[i]; ++i) {
slouken@1668
   138
        if (strcmp(argv[i], "-status") == 0) {
slouken@1662
   139
            /* PrintStatus(drive, cdrom); */
slouken@1668
   140
        } else if (strcmp(argv[i], "-list") == 0) {
slouken@1668
   141
            ListTracks(cdrom);
slouken@1668
   142
        } else if (strcmp(argv[i], "-play") == 0) {
slouken@1662
   143
            int strack, sframe;
slouken@1662
   144
            int ntrack, nframe;
slouken@0
   145
slouken@1662
   146
            strack = 0;
slouken@1668
   147
            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
slouken@1668
   148
                strack = atoi(argv[++i]);
slouken@1662
   149
            }
slouken@1662
   150
            sframe = 0;
slouken@1668
   151
            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
slouken@1668
   152
                sframe = atoi(argv[++i]);
slouken@1662
   153
            }
slouken@1662
   154
            ntrack = 0;
slouken@1668
   155
            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
slouken@1668
   156
                ntrack = atoi(argv[++i]);
slouken@1662
   157
            }
slouken@1662
   158
            nframe = 0;
slouken@1668
   159
            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
slouken@1668
   160
                nframe = atoi(argv[++i]);
slouken@1662
   161
            }
slouken@1668
   162
            if (CD_INDRIVE(SDL_CDStatus(cdrom))) {
slouken@1668
   163
                if (SDL_CDPlayTracks(cdrom, strack, sframe,
slouken@1668
   164
                                     ntrack, nframe) < 0) {
slouken@1668
   165
                    fprintf(stderr,
slouken@1668
   166
                            "Couldn't play tracks %d/%d for %d/%d: %s\n",
slouken@1668
   167
                            strack, sframe, ntrack, nframe, SDL_GetError());
slouken@1662
   168
                }
slouken@1662
   169
            } else {
slouken@1668
   170
                fprintf(stderr, "No CD in drive!\n");
slouken@1662
   171
            }
slouken@1668
   172
        } else if (strcmp(argv[i], "-pause") == 0) {
slouken@1668
   173
            if (SDL_CDPause(cdrom) < 0) {
slouken@1668
   174
                fprintf(stderr, "Couldn't pause CD: %s\n", SDL_GetError());
slouken@1662
   175
            }
slouken@1668
   176
        } else if (strcmp(argv[i], "-resume") == 0) {
slouken@1668
   177
            if (SDL_CDResume(cdrom) < 0) {
slouken@1668
   178
                fprintf(stderr, "Couldn't resume CD: %s\n", SDL_GetError());
slouken@1662
   179
            }
slouken@1668
   180
        } else if (strcmp(argv[i], "-stop") == 0) {
slouken@1668
   181
            if (SDL_CDStop(cdrom) < 0) {
slouken@1668
   182
                fprintf(stderr, "Couldn't eject CD: %s\n", SDL_GetError());
slouken@1662
   183
            }
slouken@1668
   184
        } else if (strcmp(argv[i], "-eject") == 0) {
slouken@1668
   185
            if (SDL_CDEject(cdrom) < 0) {
slouken@1668
   186
                fprintf(stderr, "Couldn't eject CD: %s\n", SDL_GetError());
slouken@1662
   187
            }
slouken@1668
   188
        } else if ((strcmp(argv[i], "-sleep") == 0) &&
slouken@1668
   189
                   (argv[i + 1] && isdigit(argv[i + 1][0]))) {
slouken@1668
   190
            SDL_Delay(atoi(argv[++i]));
slouken@1668
   191
            printf("Delayed %d milliseconds\n", atoi(argv[i]));
slouken@1662
   192
        } else {
slouken@1668
   193
            PrintUsage(argv[0]);
slouken@1668
   194
            SDL_CDClose(cdrom);
slouken@1668
   195
            quit(1);
slouken@1662
   196
        }
slouken@1662
   197
    }
slouken@1668
   198
    PrintStatus(drive, cdrom);
slouken@1668
   199
    SDL_CDClose(cdrom);
slouken@1668
   200
    SDL_Quit();
slouken@0
   201
slouken@1662
   202
    return (0);
slouken@0
   203
}