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