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