This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_syscdrom.c
524 lines (405 loc) · 11.1 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
#ifdef SDL_CDROM_MACOSX
26
27
28
29
#include "SDL_syscdrom_c.h"
#pragma mark -- Globals --
30
31
32
33
34
35
36
37
38
39
static FSRef **tracks;
static FSVolumeRefNum *volumes;
static CDstatus status;
static int nextTrackFrame;
static int nextTrackFramesRemaining;
static int fakeCD;
static int currentTrack;
static int didReadTOC;
static int cacheTOCNumTracks;
static int currentDrive; /* Only allow 1 drive in use at a time */
40
41
42
#pragma mark -- Prototypes --
43
44
45
46
47
48
49
50
51
52
static const char *SDL_SYS_CDName (int drive);
static int SDL_SYS_CDOpen (int drive);
static int SDL_SYS_CDGetTOC (SDL_CD * cdrom);
static CDstatus SDL_SYS_CDStatus (SDL_CD * cdrom, int *position);
static int SDL_SYS_CDPlay (SDL_CD * cdrom, int start, int length);
static int SDL_SYS_CDPause (SDL_CD * cdrom);
static int SDL_SYS_CDResume (SDL_CD * cdrom);
static int SDL_SYS_CDStop (SDL_CD * cdrom);
static int SDL_SYS_CDEject (SDL_CD * cdrom);
static void SDL_SYS_CDClose (SDL_CD * cdrom);
53
54
55
56
#pragma mark -- Helper Functions --
/* Read a list of tracks from the volume */
57
58
static int
LoadTracks (SDL_CD * cdrom)
59
60
{
/* Check if tracks are already loaded */
61
if (tracks[cdrom->id] != NULL)
62
return 0;
63
64
/* Allocate memory for tracks */
65
66
tracks[cdrom->id] =
(FSRef *) SDL_calloc (1, sizeof (**tracks) * cdrom->numtracks);
67
68
69
70
if (tracks[cdrom->id] == NULL) {
SDL_OutOfMemory ();
return -1;
}
71
72
/* Load tracks */
73
74
if (ListTrackFiles
(volumes[cdrom->id], tracks[cdrom->id], cdrom->numtracks) < 0)
75
76
77
78
79
80
return -1;
return 0;
}
/* Find a file for a given start frame and length */
81
82
83
static FSRef *
GetFileForOffset (SDL_CD * cdrom, int start, int length, int *outStartFrame,
int *outStopFrame)
84
85
{
int i;
86
87
for (i = 0; i < cdrom->numtracks; i++) {
88
89
90
91
92
if (cdrom->track[i].offset <= start &&
start < (cdrom->track[i].offset + cdrom->track[i].length))
break;
}
93
94
95
if (i == cdrom->numtracks)
return NULL;
96
97
98
99
currentTrack = i;
*outStartFrame = start - cdrom->track[i].offset;
100
101
102
103
104
105
if ((*outStartFrame + length) < cdrom->track[i].length) {
*outStopFrame = *outStartFrame + length;
length = 0;
nextTrackFrame = -1;
nextTrackFramesRemaining = -1;
106
} else {
107
108
*outStopFrame = -1;
length -= cdrom->track[i].length - *outStartFrame;
109
nextTrackFrame = cdrom->track[i + 1].offset;
110
111
nextTrackFramesRemaining = length;
}
112
113
114
115
116
return &tracks[cdrom->id][i];
}
/* Setup another file for playback, or stop playback (called from another thread) */
117
118
static void
CompletionProc (SDL_CD * cdrom)
119
{
120
121
Lock ();
122
123
if (nextTrackFrame > 0 && nextTrackFramesRemaining > 0) {
124
125
126
127
/* Load the next file to play */
int startFrame, stopFrame;
FSRef *file;
128
129
130
PauseFile ();
ReleaseFile ();
131
132
133
134
135
file = GetFileForOffset (cdrom, nextTrackFrame,
nextTrackFramesRemaining, &startFrame,
&stopFrame);
136
137
138
139
140
if (file == NULL) {
status = CD_STOPPED;
Unlock ();
return;
}
141
142
LoadFile (file, startFrame, stopFrame);
143
144
SetCompletionProc (CompletionProc, cdrom);
145
146
PlayFile ();
147
148
} else {
149
150
151
152
153
/* Release the current file */
PauseFile ();
ReleaseFile ();
status = CD_STOPPED;
}
154
155
156
157
158
159
160
161
Unlock ();
}
#pragma mark -- Driver Functions --
/* Initialize */
162
163
int
SDL_SYS_CDInit (void)
164
165
166
{
/* Initialize globals */
volumes = NULL;
167
168
tracks = NULL;
status = CD_STOPPED;
169
170
nextTrackFrame = -1;
nextTrackFramesRemaining = -1;
171
fakeCD = SDL_FALSE;
172
173
174
175
currentTrack = -1;
didReadTOC = SDL_FALSE;
cacheTOCNumTracks = -1;
currentDrive = -1;
176
177
/* Fill in function pointers */
178
179
SDL_CDcaps.Name = SDL_SYS_CDName;
SDL_CDcaps.Open = SDL_SYS_CDOpen;
180
181
SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
SDL_CDcaps.Status = SDL_SYS_CDStatus;
182
183
SDL_CDcaps.Play = SDL_SYS_CDPlay;
SDL_CDcaps.Pause = SDL_SYS_CDPause;
184
SDL_CDcaps.Resume = SDL_SYS_CDResume;
185
186
187
SDL_CDcaps.Stop = SDL_SYS_CDStop;
SDL_CDcaps.Eject = SDL_SYS_CDEject;
SDL_CDcaps.Close = SDL_SYS_CDClose;
188
189
/*
190
191
192
193
194
195
196
197
Read the list of "drives"
This is currently a hack that infers drives from
mounted audio CD volumes, rather than
actual CD-ROM devices - which means it may not
act as expected sometimes.
*/
198
199
200
201
/* Find out how many cd volumes are mounted */
SDL_numcds = DetectAudioCDVolumes (NULL, 0);
/*
202
203
204
If there are no volumes, fake a cd device
so tray empty can be reported.
*/
205
if (SDL_numcds == 0) {
206
207
208
209
fakeCD = SDL_TRUE;
SDL_numcds = 1;
status = CD_TRAYEMPTY;
210
211
212
return 0;
}
213
214
/* Allocate space for volumes */
215
216
volumes =
(FSVolumeRefNum *) SDL_calloc (1, sizeof (*volumes) * SDL_numcds);
217
218
219
220
if (volumes == NULL) {
SDL_OutOfMemory ();
return -1;
}
221
222
/* Allocate space for tracks */
223
tracks = (FSRef **) SDL_calloc (1, sizeof (*tracks) * (SDL_numcds + 1));
224
225
226
227
if (tracks == NULL) {
SDL_OutOfMemory ();
return -1;
}
228
229
/* Mark the end of the tracks array */
230
231
tracks[SDL_numcds] = (FSRef *) - 1;
232
/*
233
234
235
Redetect, now save all volumes for later
Update SDL_numcds just in case it changed
*/
236
237
{
int numVolumes = SDL_numcds;
238
239
SDL_numcds = DetectAudioCDVolumes (volumes, numVolumes);
240
241
242
243
244
245
246
/* If more cds suddenly show up, ignore them */
if (SDL_numcds > numVolumes) {
SDL_SetError ("Some CD's were added but they will be ignored");
SDL_numcds = numVolumes;
}
}
247
248
249
250
251
return 0;
}
/* Shutdown and cleanup */
252
253
void
SDL_SYS_CDQuit (void)
254
{
255
256
ReleaseFile ();
257
258
if (volumes != NULL)
free (volumes);
259
260
if (tracks != NULL) {
261
262
FSRef **ptr;
263
for (ptr = tracks; *ptr != (FSRef *) - 1; ptr++)
264
265
if (*ptr != NULL)
free (*ptr);
266
267
268
269
270
271
free (tracks);
}
}
/* Get the Unix disk name of the volume */
272
273
static const char *
SDL_SYS_CDName (int drive)
274
{
275
276
277
278
OSStatus err = noErr;
HParamBlockRec pb;
GetVolParmsInfoBuffer volParmsInfo;
279
280
if (fakeCD)
return "Fake CD-ROM Device";
281
282
283
pb.ioParam.ioNamePtr = NULL;
pb.ioParam.ioVRefNum = volumes[drive];
284
285
286
pb.ioParam.ioBuffer = (Ptr) & volParmsInfo;
pb.ioParam.ioReqCount = (SInt32) sizeof (volParmsInfo);
err = PBHGetVolParmsSync (&pb);
287
288
if (err != noErr) {
289
SDL_SetError ("PBHGetVolParmsSync returned %d", err);
290
291
return NULL;
}
292
293
return volParmsInfo.vMDeviceID;
294
295
296
}
/* Open the "device" */
297
298
static int
SDL_SYS_CDOpen (int drive)
299
300
301
302
303
{
/* Only allow 1 device to be open */
if (currentDrive >= 0) {
SDL_SetError ("Only one cdrom is supported");
return -1;
304
} else
305
306
307
308
309
310
currentDrive = drive;
return drive;
}
/* Get the table of contents */
311
312
static int
SDL_SYS_CDGetTOC (SDL_CD * cdrom)
313
314
315
316
317
{
if (fakeCD) {
SDL_SetError (kErrorFakeDevice);
return -1;
}
318
319
320
321
322
if (didReadTOC) {
cdrom->numtracks = cacheTOCNumTracks;
return 0;
}
323
324
325
326
327
ReadTOCData (volumes[cdrom->id], cdrom);
didReadTOC = SDL_TRUE;
cacheTOCNumTracks = cdrom->numtracks;
328
329
330
331
332
return 0;
}
/* Get CD-ROM status */
333
334
static CDstatus
SDL_SYS_CDStatus (SDL_CD * cdrom, int *position)
335
{
336
337
if (position) {
int trackFrame;
338
339
340
341
Lock ();
trackFrame = GetCurrentFrame ();
Unlock ();
342
343
344
*position = cdrom->track[currentTrack].offset + trackFrame;
}
345
346
347
348
349
return status;
}
/* Start playback */
350
351
static int
SDL_SYS_CDPlay (SDL_CD * cdrom, int start, int length)
352
353
354
{
int startFrame, stopFrame;
FSRef *ref;
355
356
357
358
359
if (fakeCD) {
SDL_SetError (kErrorFakeDevice);
return -1;
}
360
361
362
Lock ();
363
364
if (LoadTracks (cdrom) < 0)
return -2;
365
366
367
if (PauseFile () < 0)
return -3;
368
369
370
if (ReleaseFile () < 0)
return -4;
371
372
373
ref = GetFileForOffset (cdrom, start, length, &startFrame, &stopFrame);
if (ref == NULL) {
374
375
SDL_SetError ("SDL_SYS_CDPlay: No file for start=%d, length=%d",
start, length);
376
377
return -5;
}
378
379
380
if (LoadFile (ref, startFrame, stopFrame) < 0)
return -6;
381
382
SetCompletionProc (CompletionProc, cdrom);
383
384
385
if (PlayFile () < 0)
return -7;
386
387
status = CD_PLAYING;
388
389
390
Unlock ();
391
392
393
394
return 0;
}
/* Pause playback */
395
396
static int
SDL_SYS_CDPause (SDL_CD * cdrom)
397
398
399
400
401
{
if (fakeCD) {
SDL_SetError (kErrorFakeDevice);
return -1;
}
402
403
Lock ();
404
405
406
if (PauseFile () < 0) {
Unlock ();
407
return -2;
408
}
409
410
status = CD_PAUSED;
411
412
Unlock ();
413
414
415
416
417
return 0;
}
/* Resume playback */
418
419
static int
SDL_SYS_CDResume (SDL_CD * cdrom)
420
421
422
423
424
{
if (fakeCD) {
SDL_SetError (kErrorFakeDevice);
return -1;
}
425
426
Lock ();
427
428
if (PlayFile () < 0) {
429
Unlock ();
430
return -2;
431
}
432
433
status = CD_PLAYING;
434
435
Unlock ();
436
437
438
439
440
return 0;
}
/* Stop playback */
441
442
static int
SDL_SYS_CDStop (SDL_CD * cdrom)
443
444
445
446
447
{
if (fakeCD) {
SDL_SetError (kErrorFakeDevice);
return -1;
}
448
449
Lock ();
450
451
452
if (PauseFile () < 0) {
Unlock ();
453
return -2;
454
}
455
456
457
if (ReleaseFile () < 0) {
Unlock ();
458
return -3;
459
}
460
461
status = CD_STOPPED;
462
463
Unlock ();
464
465
466
467
468
return 0;
}
/* Eject the CD-ROM (Unmount the volume) */
469
470
static int
SDL_SYS_CDEject (SDL_CD * cdrom)
471
472
{
OSStatus err;
473
474
pid_t dissenter;
475
476
477
478
if (fakeCD) {
SDL_SetError (kErrorFakeDevice);
return -1;
}
479
480
Lock ();
481
482
483
if (PauseFile () < 0) {
Unlock ();
484
return -2;
485
}
486
487
488
if (ReleaseFile () < 0) {
Unlock ();
489
return -3;
490
}
491
492
status = CD_STOPPED;
493
494
495
496
497
/* Eject the volume */
err = FSEjectVolumeSync (volumes[cdrom->id], kNilOptions, &dissenter);
if (err != noErr) {
498
Unlock ();
499
500
501
502
SDL_SetError ("PBUnmountVol returned %d", err);
return -4;
}
503
504
505
506
507
508
status = CD_TRAYEMPTY;
/* Invalidate volume and track info */
volumes[cdrom->id] = 0;
free (tracks[cdrom->id]);
tracks[cdrom->id] = NULL;
509
510
Unlock ();
511
512
513
514
515
return 0;
}
/* Close the CD-ROM */
516
517
static void
SDL_SYS_CDClose (SDL_CD * cdrom)
518
519
520
521
522
{
currentDrive = -1;
return;
}
523
#endif /* SDL_CDROM_MACOSX */
524
/* vi: set ts=4 sw=4 expandtab: */