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