test/testcdrom.c
changeset 1895 c121d94672cb
parent 1484 b2b476a4a73c
     1.1 --- a/test/testcdrom.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/test/testcdrom.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -9,201 +9,195 @@
     1.4  #include "SDL.h"
     1.5  
     1.6  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
     1.7 -static void quit(int rc)
     1.8 +static void
     1.9 +quit(int rc)
    1.10  {
    1.11 -	SDL_Quit();
    1.12 -	exit(rc);
    1.13 +    SDL_Quit();
    1.14 +    exit(rc);
    1.15  }
    1.16  
    1.17 -static void PrintStatus(int driveindex, SDL_CD *cdrom)
    1.18 +static void
    1.19 +PrintStatus(int driveindex, SDL_CD * cdrom)
    1.20  {
    1.21 -	CDstatus status;
    1.22 -	char *status_str;
    1.23 +    CDstatus status;
    1.24 +    char *status_str;
    1.25  
    1.26 -	status = SDL_CDStatus(cdrom);
    1.27 -	switch (status) {
    1.28 -		case CD_TRAYEMPTY:
    1.29 -			status_str = "tray empty";
    1.30 -			break;
    1.31 -		case CD_STOPPED:
    1.32 -			status_str = "stopped";
    1.33 -			break;
    1.34 -		case CD_PLAYING:
    1.35 -			status_str = "playing";
    1.36 -			break;
    1.37 -		case CD_PAUSED:
    1.38 -			status_str = "paused";
    1.39 -			break;
    1.40 -		case CD_ERROR:
    1.41 -			status_str = "error state";
    1.42 -			break;
    1.43 -	}
    1.44 -	printf("Drive %d status: %s\n", driveindex, status_str);
    1.45 -	if ( status >= CD_PLAYING ) {
    1.46 -		int m, s, f;
    1.47 -		FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
    1.48 -		printf("Currently playing track %d, %d:%2.2d\n",
    1.49 -			cdrom->track[cdrom->cur_track].id, m, s);
    1.50 -	}
    1.51 +    status = SDL_CDStatus(cdrom);
    1.52 +    switch (status) {
    1.53 +    case CD_TRAYEMPTY:
    1.54 +        status_str = "tray empty";
    1.55 +        break;
    1.56 +    case CD_STOPPED:
    1.57 +        status_str = "stopped";
    1.58 +        break;
    1.59 +    case CD_PLAYING:
    1.60 +        status_str = "playing";
    1.61 +        break;
    1.62 +    case CD_PAUSED:
    1.63 +        status_str = "paused";
    1.64 +        break;
    1.65 +    case CD_ERROR:
    1.66 +        status_str = "error state";
    1.67 +        break;
    1.68 +    }
    1.69 +    printf("Drive %d status: %s\n", driveindex, status_str);
    1.70 +    if (status >= CD_PLAYING) {
    1.71 +        int m, s, f;
    1.72 +        FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
    1.73 +        printf("Currently playing track %d, %d:%2.2d\n",
    1.74 +               cdrom->track[cdrom->cur_track].id, m, s);
    1.75 +    }
    1.76  }
    1.77  
    1.78 -static void ListTracks(SDL_CD *cdrom)
    1.79 +static void
    1.80 +ListTracks(SDL_CD * cdrom)
    1.81  {
    1.82 -	int i;
    1.83 -	int m, s, f;
    1.84 -	char* trtype;
    1.85 +    int i;
    1.86 +    int m, s, f;
    1.87 +    char *trtype;
    1.88  
    1.89 -	SDL_CDStatus(cdrom);
    1.90 -	printf("Drive tracks: %d\n", cdrom->numtracks);
    1.91 -	for ( i=0; i<cdrom->numtracks; ++i ) {
    1.92 -		FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
    1.93 -		if ( f > 0 )
    1.94 -			++s;
    1.95 -		switch(cdrom->track[i].type)
    1.96 -		{
    1.97 -		    case SDL_AUDIO_TRACK:
    1.98 -			trtype="audio";
    1.99 -			break;
   1.100 -		    case SDL_DATA_TRACK:
   1.101 -			trtype="data";
   1.102 -			break;
   1.103 -		    default:
   1.104 -			trtype="unknown";
   1.105 -			break;
   1.106 -		}
   1.107 -		printf("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
   1.108 -					cdrom->track[i].id, m, s, cdrom->track[i].length, trtype);
   1.109 -	}
   1.110 +    SDL_CDStatus(cdrom);
   1.111 +    printf("Drive tracks: %d\n", cdrom->numtracks);
   1.112 +    for (i = 0; i < cdrom->numtracks; ++i) {
   1.113 +        FRAMES_TO_MSF(cdrom->track[i].length, &m, &s, &f);
   1.114 +        if (f > 0)
   1.115 +            ++s;
   1.116 +        switch (cdrom->track[i].type) {
   1.117 +        case SDL_AUDIO_TRACK:
   1.118 +            trtype = "audio";
   1.119 +            break;
   1.120 +        case SDL_DATA_TRACK:
   1.121 +            trtype = "data";
   1.122 +            break;
   1.123 +        default:
   1.124 +            trtype = "unknown";
   1.125 +            break;
   1.126 +        }
   1.127 +        printf("\tTrack (index %d) %d: %d:%2.2d / %d [%s track]\n", i,
   1.128 +               cdrom->track[i].id, m, s, cdrom->track[i].length, trtype);
   1.129 +    }
   1.130  }
   1.131  
   1.132 -static void PrintUsage(char *argv0)
   1.133 +static void
   1.134 +PrintUsage(char *argv0)
   1.135  {
   1.136 -	fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
   1.137 -	fprintf(stderr, "Where 'command' is one of:\n");
   1.138 -	fprintf(stderr, "	-status\n");
   1.139 -	fprintf(stderr, "	-list\n");
   1.140 -	fprintf(stderr, "	-play [first_track] [first_frame] [num_tracks] [num_frames]\n");
   1.141 -	fprintf(stderr, "	-pause\n");
   1.142 -	fprintf(stderr, "	-resume\n");
   1.143 -	fprintf(stderr, "	-stop\n");
   1.144 -	fprintf(stderr, "	-eject\n");
   1.145 -	fprintf(stderr, "	-sleep <milliseconds>\n");
   1.146 +    fprintf(stderr, "Usage: %s [drive#] [command] [command] ...\n", argv0);
   1.147 +    fprintf(stderr, "Where 'command' is one of:\n");
   1.148 +    fprintf(stderr, "	-status\n");
   1.149 +    fprintf(stderr, "	-list\n");
   1.150 +    fprintf(stderr,
   1.151 +            "	-play [first_track] [first_frame] [num_tracks] [num_frames]\n");
   1.152 +    fprintf(stderr, "	-pause\n");
   1.153 +    fprintf(stderr, "	-resume\n");
   1.154 +    fprintf(stderr, "	-stop\n");
   1.155 +    fprintf(stderr, "	-eject\n");
   1.156 +    fprintf(stderr, "	-sleep <milliseconds>\n");
   1.157  }
   1.158  
   1.159 -int main(int argc, char *argv[])
   1.160 +int
   1.161 +main(int argc, char *argv[])
   1.162  {
   1.163 -	int drive;
   1.164 -	int i;
   1.165 -	SDL_CD *cdrom;
   1.166 +    int drive;
   1.167 +    int i;
   1.168 +    SDL_CD *cdrom;
   1.169  
   1.170 -	/* Initialize SDL first */
   1.171 -	if ( SDL_Init(SDL_INIT_CDROM) < 0 ) {
   1.172 -		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   1.173 -		return(1);
   1.174 -	}
   1.175 +    /* Initialize SDL first */
   1.176 +    if (SDL_Init(SDL_INIT_CDROM) < 0) {
   1.177 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.178 +        return (1);
   1.179 +    }
   1.180  
   1.181 -	/* Find out how many CD-ROM drives are connected to the system */
   1.182 -	if ( SDL_CDNumDrives() == 0 ) {
   1.183 -		printf("No CD-ROM devices detected\n");
   1.184 -		quit(0);
   1.185 -	}
   1.186 -	printf("Drives available: %d\n", SDL_CDNumDrives());
   1.187 -	for ( i=0; i<SDL_CDNumDrives(); ++i ) {
   1.188 -		printf("Drive %d:  \"%s\"\n", i, SDL_CDName(i));
   1.189 -	}
   1.190 +    /* Find out how many CD-ROM drives are connected to the system */
   1.191 +    if (SDL_CDNumDrives() == 0) {
   1.192 +        printf("No CD-ROM devices detected\n");
   1.193 +        quit(0);
   1.194 +    }
   1.195 +    printf("Drives available: %d\n", SDL_CDNumDrives());
   1.196 +    for (i = 0; i < SDL_CDNumDrives(); ++i) {
   1.197 +        printf("Drive %d:  \"%s\"\n", i, SDL_CDName(i));
   1.198 +    }
   1.199  
   1.200 -	/* Open the CD-ROM */
   1.201 -	drive = 0;
   1.202 -	i=1;
   1.203 -	if ( argv[i] && isdigit(argv[i][0]) ) {
   1.204 -		drive = atoi(argv[i++]);
   1.205 -	}
   1.206 -	cdrom = SDL_CDOpen(drive);
   1.207 -	if ( cdrom == NULL ) {
   1.208 -		fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
   1.209 -							SDL_GetError());
   1.210 -		quit(2);
   1.211 -	}
   1.212 +    /* Open the CD-ROM */
   1.213 +    drive = 0;
   1.214 +    i = 1;
   1.215 +    if (argv[i] && isdigit(argv[i][0])) {
   1.216 +        drive = atoi(argv[i++]);
   1.217 +    }
   1.218 +    cdrom = SDL_CDOpen(drive);
   1.219 +    if (cdrom == NULL) {
   1.220 +        fprintf(stderr, "Couldn't open drive %d: %s\n", drive,
   1.221 +                SDL_GetError());
   1.222 +        quit(2);
   1.223 +    }
   1.224  #ifdef TEST_NULLCD
   1.225 -	cdrom = NULL;
   1.226 +    cdrom = NULL;
   1.227  #endif
   1.228 -	
   1.229 -	/* Find out which function to perform */
   1.230 -	for ( ; argv[i]; ++i ) {
   1.231 -		if ( strcmp(argv[i], "-status") == 0 ) {
   1.232 -			/* PrintStatus(drive, cdrom); */
   1.233 -		} else
   1.234 -		if ( strcmp(argv[i], "-list") == 0 ) {
   1.235 -			ListTracks(cdrom);
   1.236 -		} else
   1.237 -		if ( strcmp(argv[i], "-play") == 0 ) {
   1.238 -			int strack, sframe;
   1.239 -			int ntrack, nframe;
   1.240 +
   1.241 +    /* Find out which function to perform */
   1.242 +    for (; argv[i]; ++i) {
   1.243 +        if (strcmp(argv[i], "-status") == 0) {
   1.244 +            /* PrintStatus(drive, cdrom); */
   1.245 +        } else if (strcmp(argv[i], "-list") == 0) {
   1.246 +            ListTracks(cdrom);
   1.247 +        } else if (strcmp(argv[i], "-play") == 0) {
   1.248 +            int strack, sframe;
   1.249 +            int ntrack, nframe;
   1.250  
   1.251 -			strack = 0;
   1.252 -			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.253 -				strack = atoi(argv[++i]);
   1.254 -			}
   1.255 -			sframe = 0;
   1.256 -			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.257 -				sframe = atoi(argv[++i]);
   1.258 -			}
   1.259 -			ntrack = 0;
   1.260 -			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.261 -				ntrack = atoi(argv[++i]);
   1.262 -			}
   1.263 -			nframe = 0;
   1.264 -			if ( argv[i+1] && isdigit(argv[i+1][0]) ) {
   1.265 -				nframe = atoi(argv[++i]);
   1.266 -			}
   1.267 -			if ( CD_INDRIVE(SDL_CDStatus(cdrom)) ) {
   1.268 -				if ( SDL_CDPlayTracks(cdrom, strack, sframe,
   1.269 -							ntrack, nframe) < 0 ) {
   1.270 -					fprintf(stderr,
   1.271 -			"Couldn't play tracks %d/%d for %d/%d: %s\n",
   1.272 -				strack, sframe, ntrack, nframe, SDL_GetError());
   1.273 -				}
   1.274 -			} else {
   1.275 -				fprintf(stderr, "No CD in drive!\n");
   1.276 -			}
   1.277 -		} else
   1.278 -		if ( strcmp(argv[i], "-pause") == 0 ) {
   1.279 -			if ( SDL_CDPause(cdrom) < 0 ) {
   1.280 -				fprintf(stderr, "Couldn't pause CD: %s\n",
   1.281 -								SDL_GetError());
   1.282 -			}
   1.283 -		} else
   1.284 -		if ( strcmp(argv[i], "-resume") == 0 ) {
   1.285 -			if ( SDL_CDResume(cdrom) < 0 ) {
   1.286 -				fprintf(stderr, "Couldn't resume CD: %s\n",
   1.287 -								SDL_GetError());
   1.288 -			}
   1.289 -		} else
   1.290 -		if ( strcmp(argv[i], "-stop") == 0 ) {
   1.291 -			if ( SDL_CDStop(cdrom) < 0 ) {
   1.292 -				fprintf(stderr, "Couldn't eject CD: %s\n",
   1.293 -								SDL_GetError());
   1.294 -			}
   1.295 -		} else
   1.296 -		if ( strcmp(argv[i], "-eject") == 0 ) {
   1.297 -			if ( SDL_CDEject(cdrom) < 0 ) {
   1.298 -				fprintf(stderr, "Couldn't eject CD: %s\n",
   1.299 -								SDL_GetError());
   1.300 -			}
   1.301 -		} else
   1.302 -		if ( (strcmp(argv[i], "-sleep") == 0) &&
   1.303 -				(argv[i+1] && isdigit(argv[i+1][0])) ) {
   1.304 -			SDL_Delay(atoi(argv[++i]));
   1.305 -			printf("Delayed %d milliseconds\n", atoi(argv[i]));
   1.306 -		} else {
   1.307 -			PrintUsage(argv[0]);
   1.308 -			SDL_CDClose(cdrom);
   1.309 -			quit(1);
   1.310 -		}
   1.311 -	}
   1.312 -	PrintStatus(drive, cdrom);
   1.313 -	SDL_CDClose(cdrom);
   1.314 -	SDL_Quit();
   1.315 +            strack = 0;
   1.316 +            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
   1.317 +                strack = atoi(argv[++i]);
   1.318 +            }
   1.319 +            sframe = 0;
   1.320 +            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
   1.321 +                sframe = atoi(argv[++i]);
   1.322 +            }
   1.323 +            ntrack = 0;
   1.324 +            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
   1.325 +                ntrack = atoi(argv[++i]);
   1.326 +            }
   1.327 +            nframe = 0;
   1.328 +            if (argv[i + 1] && isdigit(argv[i + 1][0])) {
   1.329 +                nframe = atoi(argv[++i]);
   1.330 +            }
   1.331 +            if (CD_INDRIVE(SDL_CDStatus(cdrom))) {
   1.332 +                if (SDL_CDPlayTracks(cdrom, strack, sframe,
   1.333 +                                     ntrack, nframe) < 0) {
   1.334 +                    fprintf(stderr,
   1.335 +                            "Couldn't play tracks %d/%d for %d/%d: %s\n",
   1.336 +                            strack, sframe, ntrack, nframe, SDL_GetError());
   1.337 +                }
   1.338 +            } else {
   1.339 +                fprintf(stderr, "No CD in drive!\n");
   1.340 +            }
   1.341 +        } else if (strcmp(argv[i], "-pause") == 0) {
   1.342 +            if (SDL_CDPause(cdrom) < 0) {
   1.343 +                fprintf(stderr, "Couldn't pause CD: %s\n", SDL_GetError());
   1.344 +            }
   1.345 +        } else if (strcmp(argv[i], "-resume") == 0) {
   1.346 +            if (SDL_CDResume(cdrom) < 0) {
   1.347 +                fprintf(stderr, "Couldn't resume CD: %s\n", SDL_GetError());
   1.348 +            }
   1.349 +        } else if (strcmp(argv[i], "-stop") == 0) {
   1.350 +            if (SDL_CDStop(cdrom) < 0) {
   1.351 +                fprintf(stderr, "Couldn't eject CD: %s\n", SDL_GetError());
   1.352 +            }
   1.353 +        } else if (strcmp(argv[i], "-eject") == 0) {
   1.354 +            if (SDL_CDEject(cdrom) < 0) {
   1.355 +                fprintf(stderr, "Couldn't eject CD: %s\n", SDL_GetError());
   1.356 +            }
   1.357 +        } else if ((strcmp(argv[i], "-sleep") == 0) &&
   1.358 +                   (argv[i + 1] && isdigit(argv[i + 1][0]))) {
   1.359 +            SDL_Delay(atoi(argv[++i]));
   1.360 +            printf("Delayed %d milliseconds\n", atoi(argv[i]));
   1.361 +        } else {
   1.362 +            PrintUsage(argv[0]);
   1.363 +            SDL_CDClose(cdrom);
   1.364 +            quit(1);
   1.365 +        }
   1.366 +    }
   1.367 +    PrintStatus(drive, cdrom);
   1.368 +    SDL_CDClose(cdrom);
   1.369 +    SDL_Quit();
   1.370  
   1.371 -	return(0);
   1.372 +    return (0);
   1.373  }