test/testcdrom.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1484 b2b476a4a73c
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    13
quit (int rc)
icculus@1151
    14
{
slouken@1662
    15
    SDL_Quit ();
slouken@1662
    16
    exit (rc);
icculus@1151
    17
}
slouken@0
    18
slouken@1662
    19
static void
slouken@1662
    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@1662
    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@1662
    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@1662
    46
        FRAMES_TO_MSF (cdrom->cur_frame, &m, &s, &f);
slouken@1662
    47
        printf ("Currently playing track %d, %d:%2.2d\n",
slouken@1662
    48
                cdrom->track[cdrom->cur_track].id, m, s);
slouken@1662
    49
    }
slouken@0
    50
}
slouken@0
    51
slouken@1662
    52
static void
slouken@1662
    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@1662
    59
    SDL_CDStatus (cdrom);
slouken@1662
    60
    printf ("Drive tracks: %d\n", cdrom->numtracks);
slouken@1662
    61
    for (i = 0; i < cdrom->numtracks; ++i) {
slouken@1662
    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@1662
    76
        printf ("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
slouken@1662
    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@1662
    82
PrintUsage (char *argv0)
slouken@0
    83
{
slouken@1662
    84
    fprintf (stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
slouken@1662
    85
    fprintf (stderr, "Where 'command' is one of:\n");
slouken@1662
    86
    fprintf (stderr, "	-status\n");
slouken@1662
    87
    fprintf (stderr, "	-list\n");
slouken@1662
    88
    fprintf (stderr,
slouken@1662
    89
             "	-play [first_track] [first_frame] [num_tracks] [num_frames]\n");
slouken@1662
    90
    fprintf (stderr, "	-pause\n");
slouken@1662
    91
    fprintf (stderr, "	-resume\n");
slouken@1662
    92
    fprintf (stderr, "	-stop\n");
slouken@1662
    93
    fprintf (stderr, "	-eject\n");
slouken@1662
    94
    fprintf (stderr, "	-sleep <milliseconds>\n");
slouken@0
    95
}
slouken@0
    96
slouken@1662
    97
int
slouken@1662
    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@1662
   105
    if (SDL_Init (SDL_INIT_CDROM) < 0) {
slouken@1662
   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@1662
   111
    if (SDL_CDNumDrives () == 0) {
slouken@1662
   112
        printf ("No CD-ROM devices detected\n");
slouken@1662
   113
        quit (0);
slouken@1662
   114
    }
slouken@1662
   115
    printf ("Drives available: %d\n", SDL_CDNumDrives ());
slouken@1662
   116
    for (i = 0; i < SDL_CDNumDrives (); ++i) {
slouken@1662
   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@1662
   123
    if (argv[i] && isdigit (argv[i][0])) {
slouken@1662
   124
        drive = atoi (argv[i++]);
slouken@1662
   125
    }
slouken@1662
   126
    cdrom = SDL_CDOpen (drive);
slouken@1662
   127
    if (cdrom == NULL) {
slouken@1662
   128
        fprintf (stderr, "Couldn't open drive %d: %s\n", drive,
slouken@1662
   129
                 SDL_GetError ());
slouken@1662
   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@1662
   138
        if (strcmp (argv[i], "-status") == 0) {
slouken@1662
   139
            /* PrintStatus(drive, cdrom); */
slouken@1662
   140
        } else if (strcmp (argv[i], "-list") == 0) {
slouken@1662
   141
            ListTracks (cdrom);
slouken@1662
   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@1662
   147
            if (argv[i + 1] && isdigit (argv[i + 1][0])) {
slouken@1662
   148
                strack = atoi (argv[++i]);
slouken@1662
   149
            }
slouken@1662
   150
            sframe = 0;
slouken@1662
   151
            if (argv[i + 1] && isdigit (argv[i + 1][0])) {
slouken@1662
   152
                sframe = atoi (argv[++i]);
slouken@1662
   153
            }
slouken@1662
   154
            ntrack = 0;
slouken@1662
   155
            if (argv[i + 1] && isdigit (argv[i + 1][0])) {
slouken@1662
   156
                ntrack = atoi (argv[++i]);
slouken@1662
   157
            }
slouken@1662
   158
            nframe = 0;
slouken@1662
   159
            if (argv[i + 1] && isdigit (argv[i + 1][0])) {
slouken@1662
   160
                nframe = atoi (argv[++i]);
slouken@1662
   161
            }
slouken@1662
   162
            if (CD_INDRIVE (SDL_CDStatus (cdrom))) {
slouken@1662
   163
                if (SDL_CDPlayTracks (cdrom, strack, sframe,
slouken@1662
   164
                                      ntrack, nframe) < 0) {
slouken@1662
   165
                    fprintf (stderr,
slouken@1662
   166
                             "Couldn't play tracks %d/%d for %d/%d: %s\n",
slouken@1662
   167
                             strack, sframe, ntrack, nframe, SDL_GetError ());
slouken@1662
   168
                }
slouken@1662
   169
            } else {
slouken@1662
   170
                fprintf (stderr, "No CD in drive!\n");
slouken@1662
   171
            }
slouken@1662
   172
        } else if (strcmp (argv[i], "-pause") == 0) {
slouken@1662
   173
            if (SDL_CDPause (cdrom) < 0) {
slouken@1662
   174
                fprintf (stderr, "Couldn't pause CD: %s\n", SDL_GetError ());
slouken@1662
   175
            }
slouken@1662
   176
        } else if (strcmp (argv[i], "-resume") == 0) {
slouken@1662
   177
            if (SDL_CDResume (cdrom) < 0) {
slouken@1662
   178
                fprintf (stderr, "Couldn't resume CD: %s\n", SDL_GetError ());
slouken@1662
   179
            }
slouken@1662
   180
        } else if (strcmp (argv[i], "-stop") == 0) {
slouken@1662
   181
            if (SDL_CDStop (cdrom) < 0) {
slouken@1662
   182
                fprintf (stderr, "Couldn't eject CD: %s\n", SDL_GetError ());
slouken@1662
   183
            }
slouken@1662
   184
        } else if (strcmp (argv[i], "-eject") == 0) {
slouken@1662
   185
            if (SDL_CDEject (cdrom) < 0) {
slouken@1662
   186
                fprintf (stderr, "Couldn't eject CD: %s\n", SDL_GetError ());
slouken@1662
   187
            }
slouken@1662
   188
        } else if ((strcmp (argv[i], "-sleep") == 0) &&
slouken@1662
   189
                   (argv[i + 1] && isdigit (argv[i + 1][0]))) {
slouken@1662
   190
            SDL_Delay (atoi (argv[++i]));
slouken@1662
   191
            printf ("Delayed %d milliseconds\n", atoi (argv[i]));
slouken@1662
   192
        } else {
slouken@1662
   193
            PrintUsage (argv[0]);
slouken@1662
   194
            SDL_CDClose (cdrom);
slouken@1662
   195
            quit (1);
slouken@1662
   196
        }
slouken@1662
   197
    }
slouken@1662
   198
    PrintStatus (drive, cdrom);
slouken@1662
   199
    SDL_CDClose (cdrom);
slouken@1662
   200
    SDL_Quit ();
slouken@0
   201
slouken@1662
   202
    return (0);
slouken@0
   203
}