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