2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2004 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "SDL_syscdrom_c.h"
30 #pragma mark -- Globals --
32 static FSRef** tracks;
33 static FSVolumeRefNum* volumes;
34 static CDstatus status;
35 static int nextTrackFrame;
36 static int nextTrackFramesRemaining;
38 static int currentTrack;
39 static int didReadTOC;
40 static int cacheTOCNumTracks;
41 static int currentDrive; /* Only allow 1 drive in use at a time */
43 #pragma mark -- Prototypes --
45 static const char *SDL_SYS_CDName (int drive);
46 static int SDL_SYS_CDOpen (int drive);
47 static int SDL_SYS_CDGetTOC (SDL_CD *cdrom);
48 static CDstatus SDL_SYS_CDStatus (SDL_CD *cdrom, int *position);
49 static int SDL_SYS_CDPlay (SDL_CD *cdrom, int start, int length);
50 static int SDL_SYS_CDPause (SDL_CD *cdrom);
51 static int SDL_SYS_CDResume (SDL_CD *cdrom);
52 static int SDL_SYS_CDStop (SDL_CD *cdrom);
53 static int SDL_SYS_CDEject (SDL_CD *cdrom);
54 static void SDL_SYS_CDClose (SDL_CD *cdrom);
56 #pragma mark -- Helper Functions --
58 /* Read a list of tracks from the volume */
59 static int LoadTracks (SDL_CD *cdrom)
61 /* Check if tracks are already loaded */
62 if ( tracks[cdrom->id] != NULL )
65 /* Allocate memory for tracks */
66 tracks[cdrom->id] = (FSRef*) calloc (1, sizeof(**tracks) * cdrom->numtracks);
67 if (tracks[cdrom->id] == NULL) {
73 if (ListTrackFiles (volumes[cdrom->id], tracks[cdrom->id], cdrom->numtracks) < 0)
79 /* Find a file for a given start frame and length */
80 static FSRef* GetFileForOffset (SDL_CD *cdrom, int start, int length, int *outStartFrame, int *outStopFrame)
84 for (i = 0; i < cdrom->numtracks; i++) {
86 if (cdrom->track[i].offset <= start &&
87 start < (cdrom->track[i].offset + cdrom->track[i].length))
91 if (i == cdrom->numtracks)
96 *outStartFrame = start - cdrom->track[i].offset;
98 if ((*outStartFrame + length) < cdrom->track[i].length) {
99 *outStopFrame = *outStartFrame + length;
102 nextTrackFramesRemaining = -1;
106 length -= cdrom->track[i].length - *outStartFrame;
107 nextTrackFrame = cdrom->track[i+1].offset;
108 nextTrackFramesRemaining = length;
111 return &tracks[cdrom->id][i];
114 /* Setup another file for playback, or stop playback (called from another thread) */
115 static void CompletionProc (SDL_CD *cdrom)
120 if (nextTrackFrame > 0 && nextTrackFramesRemaining > 0) {
122 /* Load the next file to play */
123 int startFrame, stopFrame;
129 file = GetFileForOffset (cdrom, nextTrackFrame,
130 nextTrackFramesRemaining, &startFrame, &stopFrame);
138 LoadFile (file, startFrame, stopFrame);
140 SetCompletionProc (CompletionProc, cdrom);
146 /* Release the current file */
156 #pragma mark -- Driver Functions --
159 int SDL_SYS_CDInit (void)
161 /* Initialize globals */
166 nextTrackFramesRemaining = -1;
169 didReadTOC = SDL_FALSE;
170 cacheTOCNumTracks = -1;
173 /* Fill in function pointers */
174 SDL_CDcaps.Name = SDL_SYS_CDName;
175 SDL_CDcaps.Open = SDL_SYS_CDOpen;
176 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
177 SDL_CDcaps.Status = SDL_SYS_CDStatus;
178 SDL_CDcaps.Play = SDL_SYS_CDPlay;
179 SDL_CDcaps.Pause = SDL_SYS_CDPause;
180 SDL_CDcaps.Resume = SDL_SYS_CDResume;
181 SDL_CDcaps.Stop = SDL_SYS_CDStop;
182 SDL_CDcaps.Eject = SDL_SYS_CDEject;
183 SDL_CDcaps.Close = SDL_SYS_CDClose;
186 Read the list of "drives"
188 This is currently a hack that infers drives from
189 mounted audio CD volumes, rather than
190 actual CD-ROM devices - which means it may not
191 act as expected sometimes.
194 /* Find out how many cd volumes are mounted */
195 SDL_numcds = DetectAudioCDVolumes (NULL, 0);
198 If there are no volumes, fake a cd device
199 so tray empty can be reported.
201 if (SDL_numcds == 0) {
205 status = CD_TRAYEMPTY;
210 /* Allocate space for volumes */
211 volumes = (FSVolumeRefNum*) calloc (1, sizeof(*volumes) * SDL_numcds);
212 if (volumes == NULL) {
217 /* Allocate space for tracks */
218 tracks = (FSRef**) calloc (1, sizeof(*tracks) * (SDL_numcds + 1));
219 if (tracks == NULL) {
224 /* Mark the end of the tracks array */
225 tracks[ SDL_numcds ] = (FSRef*)-1;
228 Redetect, now save all volumes for later
229 Update SDL_numcds just in case it changed
232 int numVolumes = SDL_numcds;
234 SDL_numcds = DetectAudioCDVolumes (volumes, numVolumes);
236 /* If more cds suddenly show up, ignore them */
237 if (SDL_numcds > numVolumes) {
238 SDL_SetError ("Some CD's were added but they will be ignored");
239 SDL_numcds = numVolumes;
246 /* Shutdown and cleanup */
247 void SDL_SYS_CDQuit(void)
254 if (tracks != NULL) {
257 for (ptr = tracks; *ptr != (FSRef*)-1; ptr++)
265 /* Get the Unix disk name of the volume */
266 static const char *SDL_SYS_CDName (int drive)
268 OSStatus err = noErr;
270 GetVolParmsInfoBuffer volParmsInfo;
273 return "Fake CD-ROM Device";
275 pb.ioParam.ioNamePtr = NULL;
276 pb.ioParam.ioVRefNum = volumes[drive];
277 pb.ioParam.ioBuffer = (Ptr)&volParmsInfo;
278 pb.ioParam.ioReqCount = (SInt32)sizeof(volParmsInfo);
279 err = PBHGetVolParmsSync(&pb);
282 SDL_SetError ("PBHGetVolParmsSync returned %d", err);
286 return volParmsInfo.vMDeviceID;
289 /* Open the "device" */
290 static int SDL_SYS_CDOpen (int drive)
292 /* Only allow 1 device to be open */
293 if (currentDrive >= 0) {
294 SDL_SetError ("Only one cdrom is supported");
298 currentDrive = drive;
303 /* Get the table of contents */
304 static int SDL_SYS_CDGetTOC (SDL_CD *cdrom)
307 SDL_SetError (kErrorFakeDevice);
312 cdrom->numtracks = cacheTOCNumTracks;
317 ReadTOCData (volumes[cdrom->id], cdrom);
318 didReadTOC = SDL_TRUE;
319 cacheTOCNumTracks = cdrom->numtracks;
324 /* Get CD-ROM status */
325 static CDstatus SDL_SYS_CDStatus (SDL_CD *cdrom, int *position)
331 trackFrame = GetCurrentFrame ();
334 *position = cdrom->track[currentTrack].offset + trackFrame;
341 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
343 int startFrame, stopFrame;
347 SDL_SetError (kErrorFakeDevice);
353 if (LoadTracks (cdrom) < 0)
356 if (PauseFile () < 0)
359 if (ReleaseFile () < 0)
362 ref = GetFileForOffset (cdrom, start, length, &startFrame, &stopFrame);
364 SDL_SetError ("SDL_SYS_CDPlay: No file for start=%d, length=%d", start, length);
368 if (LoadFile (ref, startFrame, stopFrame) < 0)
371 SetCompletionProc (CompletionProc, cdrom);
384 static int SDL_SYS_CDPause(SDL_CD *cdrom)
387 SDL_SetError (kErrorFakeDevice);
393 if (PauseFile () < 0) {
405 /* Resume playback */
406 static int SDL_SYS_CDResume(SDL_CD *cdrom)
409 SDL_SetError (kErrorFakeDevice);
415 if (PlayFile () < 0) {
428 static int SDL_SYS_CDStop(SDL_CD *cdrom)
431 SDL_SetError (kErrorFakeDevice);
437 if (PauseFile () < 0) {
442 if (ReleaseFile () < 0) {
454 /* Eject the CD-ROM (Unmount the volume) */
455 static int SDL_SYS_CDEject(SDL_CD *cdrom)
461 SDL_SetError (kErrorFakeDevice);
467 if (PauseFile () < 0) {
472 if (ReleaseFile () < 0) {
480 pb.ioParam.ioNamePtr = NULL;
481 pb.ioParam.ioVRefNum = volumes[cdrom->id];
482 err = PBUnmountVol((ParamBlockRec *) &pb);
486 SDL_SetError ("PBUnmountVol returned %d", err);
490 status = CD_TRAYEMPTY;
492 /* Invalidate volume and track info */
493 volumes[cdrom->id] = 0;
494 free (tracks[cdrom->id]);
495 tracks[cdrom->id] = NULL;
502 /* Close the CD-ROM */
503 static void SDL_SYS_CDClose(SDL_CD *cdrom)