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