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