test/testcdrom.c
changeset 0 74212992fb08
child 568 0cd6b268193b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testcdrom.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,189 @@
     1.4 +
     1.5 +/* Test the SDL CD-ROM audio functions */
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <stdio.h>
     1.9 +#include <ctype.h>
    1.10 +
    1.11 +#include "SDL.h"
    1.12 +
    1.13 +
    1.14 +static void PrintStatus(int driveindex, SDL_CD *cdrom)
    1.15 +{
    1.16 +	CDstatus status;
    1.17 +	char *status_str;
    1.18 +
    1.19 +	status = SDL_CDStatus(cdrom);
    1.20 +	switch (status) {
    1.21 +		case CD_TRAYEMPTY:
    1.22 +			status_str = "tray empty";
    1.23 +			break;
    1.24 +		case CD_STOPPED:
    1.25 +			status_str = "stopped";
    1.26 +			break;
    1.27 +		case CD_PLAYING:
    1.28 +			status_str = "playing";
    1.29 +			break;
    1.30 +		case CD_PAUSED:
    1.31 +			status_str = "paused";
    1.32 +			break;
    1.33 +		case CD_ERROR:
    1.34 +			status_str = "error state";
    1.35 +			break;
    1.36 +	}
    1.37 +	printf("Drive %d status: %s\n", driveindex, status_str);
    1.38 +	if ( status >= CD_PLAYING ) {
    1.39 +		int m, s, f;
    1.40 +		FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
    1.41 +		printf("Currently playing track %d, %d:%2.2d\n",
    1.42 +			cdrom->track[cdrom->cur_track].id, m, s);
    1.43 +	}
    1.44 +}
    1.45 +
    1.46 +static void ListTracks(SDL_CD *cdrom)
    1.47 +{
    1.48 +	int i;
    1.49 +	int m, s, f;
    1.50 +
    1.51 +	SDL_CDStatus(cdrom);
    1.52 +	printf("Drive tracks: %d\n", cdrom->numtracks);
    1.53 +	for ( i=0; i<cdrom->numtracks; ++i ) {
    1.54 +		FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
    1.55 +		if ( f > 0 )
    1.56 +			++s;
    1.57 +		printf("\tTrack (index %d) %d: %d:%2.2d\n", i,
    1.58 +					cdrom->track[i].id, m, s);
    1.59 +	}
    1.60 +}
    1.61 +
    1.62 +static void PrintUsage(char *argv0)
    1.63 +{
    1.64 +	fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
    1.65 +	fprintf(stderr, "Where 'command' is one of:\n");
    1.66 +	fprintf(stderr, "	-status\n");
    1.67 +	fprintf(stderr, "	-list\n");
    1.68 +	fprintf(stderr, "	-play [first_track] [first_frame] [num_tracks] [num_frames]\n");
    1.69 +	fprintf(stderr, "	-pause\n");
    1.70 +	fprintf(stderr, "	-resume\n");
    1.71 +	fprintf(stderr, "	-stop\n");
    1.72 +	fprintf(stderr, "	-eject\n");
    1.73 +	fprintf(stderr, "	-sleep <milliseconds>\n");
    1.74 +}
    1.75 +
    1.76 +int main(int argc, char *argv[])
    1.77 +{
    1.78 +	int drive;
    1.79 +	int i;
    1.80 +	SDL_CD *cdrom;
    1.81 +
    1.82 +	/* Initialize SDL first */
    1.83 +	if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
    1.84 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.85 +		exit(1);
    1.86 +	}
    1.87 +	atexit(SDL_Quit);
    1.88 +
    1.89 +	/* Find out how many CD-ROM drives are connected to the system */
    1.90 +	if ( SDL_CDNumDrives() == 0 ) {
    1.91 +		printf("No CD-ROM devices detected\n");
    1.92 +		exit(0);
    1.93 +	}
    1.94 +	printf("Drives available: %d\n", SDL_CDNumDrives());
    1.95 +	for ( i=0; i<SDL_CDNumDrives(); ++i ) {
    1.96 +		printf("Drive %d:  \"%s\"\n", i, SDL_CDName(i));
    1.97 +	}
    1.98 +
    1.99 +	/* Open the CD-ROM */
   1.100 +	drive = 0;
   1.101 +	i=1;
   1.102 +	if ( argv[i] && isdigit(argv[i][0]) ) {
   1.103 +		drive = atoi(argv[i++]);
   1.104 +	}
   1.105 +	cdrom = SDL_CDOpen(drive);
   1.106 +	if ( cdrom == NULL ) {
   1.107 +		fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
   1.108 +							SDL_GetError());
   1.109 +		exit(2);
   1.110 +	}
   1.111 +#ifdef TEST_NULLCD
   1.112 +	cdrom = NULL;
   1.113 +#endif
   1.114 +	
   1.115 +	/* Find out which function to perform */
   1.116 +	for ( ; argv[i]; ++i ) {
   1.117 +		if ( strcmp(argv[i], "-status") == 0 ) {
   1.118 +			/* PrintStatus(drive, cdrom); */
   1.119 +		} else
   1.120 +		if ( strcmp(argv[i], "-list") == 0 ) {
   1.121 +			ListTracks(cdrom);
   1.122 +		} else
   1.123 +		if ( strcmp(argv[i], "-play") == 0 ) {
   1.124 +			int strack, sframe;
   1.125 +			int ntrack, nframe;
   1.126 +
   1.127 +			strack = 0;
   1.128 +			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.129 +				strack = atoi(argv[++i]);
   1.130 +			}
   1.131 +			sframe = 0;
   1.132 +			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.133 +				sframe = atoi(argv[++i]);
   1.134 +			}
   1.135 +			ntrack = 0;
   1.136 +			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.137 +				ntrack = atoi(argv[++i]);
   1.138 +			}
   1.139 +			nframe = 0;
   1.140 +			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.141 +				nframe = atoi(argv[++i]);
   1.142 +			}
   1.143 +			if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) {
   1.144 +				if ( SDL_CDPlayTracks(cdrom, strack, sframe,
   1.145 +							ntrack, nframe) < 0 ) {
   1.146 +					fprintf(stderr,
   1.147 +			"Couldn't play tracks %d/%d for %d/%d: %s\n",
   1.148 +				strack, sframe, ntrack, nframe, SDL_GetError());
   1.149 +				}
   1.150 +			} else {
   1.151 +				fprintf(stderr, "No CD in drive!\n");
   1.152 +			}
   1.153 +		} else
   1.154 +		if ( strcmp(argv[i], "-pause") == 0 ) {
   1.155 +			if ( SDL_CDPause(cdrom) < 0 ) {
   1.156 +				fprintf(stderr, "Couldn't pause CD: %s\n",
   1.157 +								SDL_GetError());
   1.158 +			}
   1.159 +		} else
   1.160 +		if ( strcmp(argv[i], "-resume") == 0 ) {
   1.161 +			if ( SDL_CDResume(cdrom) < 0 ) {
   1.162 +				fprintf(stderr, "Couldn't resume CD: %s\n",
   1.163 +								SDL_GetError());
   1.164 +			}
   1.165 +		} else
   1.166 +		if ( strcmp(argv[i], "-stop") == 0 ) {
   1.167 +			if ( SDL_CDStop(cdrom) < 0 ) {
   1.168 +				fprintf(stderr, "Couldn't eject CD: %s\n",
   1.169 +								SDL_GetError());
   1.170 +			}
   1.171 +		} else
   1.172 +		if ( strcmp(argv[i], "-eject") == 0 ) {
   1.173 +			if ( SDL_CDEject(cdrom) < 0 ) {
   1.174 +				fprintf(stderr, "Couldn't eject CD: %s\n",
   1.175 +								SDL_GetError());
   1.176 +			}
   1.177 +		} else
   1.178 +		if ( (strcmp(argv[i], "-sleep") == 0) &&
   1.179 +				(argv[i+1] && isdigit(argv[i+1][0])) ) {
   1.180 +			SDL_Delay(atoi(argv[++i]));
   1.181 +			printf("Delayed %d milliseconds\n", atoi(argv[i]));
   1.182 +		} else {
   1.183 +			PrintUsage(argv[0]);
   1.184 +			SDL_CDClose(cdrom);
   1.185 +			exit(1);
   1.186 +		}
   1.187 +	}
   1.188 +	PrintStatus(drive, cdrom);
   1.189 +	SDL_CDClose(cdrom);
   1.190 +
   1.191 +	return(0);
   1.192 +}