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