This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_syscdrom.c
523 lines (404 loc) · 10.9 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2009 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
if (tracks[cdrom->id] == NULL) {
68
SDL_OutOfMemory();
69
70
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
122
Lock();
123
if (nextTrackFrame > 0 && nextTrackFramesRemaining > 0) {
124
125
126
127
/* Load the next file to play */
int startFrame, stopFrame;
FSRef *file;
128
129
130
131
132
133
134
135
PauseFile();
ReleaseFile();
file = GetFileForOffset(cdrom, nextTrackFrame,
nextTrackFramesRemaining, &startFrame,
&stopFrame);
136
137
if (file == NULL) {
status = CD_STOPPED;
138
Unlock();
139
140
return;
}
141
142
143
144
145
146
147
148
LoadFile(file, startFrame, stopFrame);
SetCompletionProc(CompletionProc, cdrom);
PlayFile();
} else {
149
/* Release the current file */
150
151
PauseFile();
ReleaseFile();
152
153
status = CD_STOPPED;
}
154
155
Unlock();
156
157
158
159
160
161
}
#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
/* Find out how many cd volumes are mounted */
199
SDL_numcds = DetectAudioCDVolumes(NULL, 0);
200
201
/*
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
volumes = (FSVolumeRefNum *) SDL_calloc(1, sizeof(*volumes) * SDL_numcds);
216
if (volumes == NULL) {
217
SDL_OutOfMemory();
218
219
return -1;
}
220
221
/* Allocate space for tracks */
222
tracks = (FSRef **) SDL_calloc(1, sizeof(*tracks) * (SDL_numcds + 1));
223
if (tracks == NULL) {
224
SDL_OutOfMemory();
225
226
return -1;
}
227
228
/* Mark the end of the tracks array */
229
230
tracks[SDL_numcds] = (FSRef *) - 1;
231
/*
232
233
234
Redetect, now save all volumes for later
Update SDL_numcds just in case it changed
*/
235
236
{
int numVolumes = SDL_numcds;
237
238
239
SDL_numcds = DetectAudioCDVolumes(volumes, numVolumes);
240
241
/* If more cds suddenly show up, ignore them */
if (SDL_numcds > numVolumes) {
242
SDL_SetError("Some CD's were added but they will be ignored");
243
244
245
SDL_numcds = numVolumes;
}
}
246
247
248
249
250
return 0;
}
/* Shutdown and cleanup */
251
252
void
SDL_SYS_CDQuit(void)
253
254
{
ReleaseFile();
255
256
if (volumes != NULL)
257
258
free(volumes);
259
if (tracks != NULL) {
260
261
FSRef **ptr;
262
for (ptr = tracks; *ptr != (FSRef *) - 1; ptr++)
263
if (*ptr != NULL)
264
265
266
free(*ptr);
free(tracks);
267
268
269
270
}
}
/* Get the Unix disk name of the volume */
271
272
static const char *
SDL_SYS_CDName(int drive)
273
{
274
275
276
277
OSStatus err = noErr;
HParamBlockRec pb;
GetVolParmsInfoBuffer volParmsInfo;
278
279
if (fakeCD)
return "Fake CD-ROM Device";
280
281
282
pb.ioParam.ioNamePtr = NULL;
pb.ioParam.ioVRefNum = volumes[drive];
283
284
pb.ioParam.ioBuffer = (Ptr) & volParmsInfo;
pb.ioParam.ioReqCount = (SInt32) sizeof(volParmsInfo);
285
286
err = PBHGetVolParmsSync(&pb);
287
if (err != noErr) {
288
SDL_SetError("PBHGetVolParmsSync returned %d", err);
289
290
return NULL;
}
291
292
return volParmsInfo.vMDeviceID;
293
294
295
}
/* Open the "device" */
296
297
static int
SDL_SYS_CDOpen(int drive)
298
299
300
{
/* Only allow 1 device to be open */
if (currentDrive >= 0) {
301
SDL_SetError("Only one cdrom is supported");
302
return -1;
303
} else
304
305
306
307
308
309
currentDrive = drive;
return drive;
}
/* Get the table of contents */
310
311
static int
SDL_SYS_CDGetTOC(SDL_CD * cdrom)
312
313
{
if (fakeCD) {
314
SDL_SetError(kErrorFakeDevice);
315
316
return -1;
}
317
318
319
320
321
if (didReadTOC) {
cdrom->numtracks = cacheTOCNumTracks;
return 0;
}
322
323
324
ReadTOCData(volumes[cdrom->id], cdrom);
325
326
didReadTOC = SDL_TRUE;
cacheTOCNumTracks = cdrom->numtracks;
327
328
329
330
331
return 0;
}
/* Get CD-ROM status */
332
333
static CDstatus
SDL_SYS_CDStatus(SDL_CD * cdrom, int *position)
334
{
335
336
if (position) {
int trackFrame;
337
338
339
340
341
Lock();
trackFrame = GetCurrentFrame();
Unlock();
342
343
*position = cdrom->track[currentTrack].offset + trackFrame;
}
344
345
346
347
348
return status;
}
/* Start playback */
349
350
static int
SDL_SYS_CDPlay(SDL_CD * cdrom, int start, int length)
351
352
353
{
int startFrame, stopFrame;
FSRef *ref;
354
355
if (fakeCD) {
356
SDL_SetError(kErrorFakeDevice);
357
358
return -1;
}
359
360
Lock();
361
362
if (LoadTracks(cdrom) < 0)
363
return -2;
364
365
if (PauseFile() < 0)
366
return -3;
367
368
if (ReleaseFile() < 0)
369
return -4;
370
371
ref = GetFileForOffset(cdrom, start, length, &startFrame, &stopFrame);
372
if (ref == NULL) {
373
374
SDL_SetError("SDL_SYS_CDPlay: No file for start=%d, length=%d",
start, length);
375
376
return -5;
}
377
378
if (LoadFile(ref, startFrame, stopFrame) < 0)
379
return -6;
380
381
382
383
SetCompletionProc(CompletionProc, cdrom);
if (PlayFile() < 0)
384
return -7;
385
386
status = CD_PLAYING;
387
388
Unlock();
389
390
391
392
393
return 0;
}
/* Pause playback */
394
395
static int
SDL_SYS_CDPause(SDL_CD * cdrom)
396
397
{
if (fakeCD) {
398
SDL_SetError(kErrorFakeDevice);
399
400
return -1;
}
401
402
403
404
405
Lock();
if (PauseFile() < 0) {
Unlock();
406
return -2;
407
}
408
409
status = CD_PAUSED;
410
411
412
Unlock();
413
414
415
416
return 0;
}
/* Resume playback */
417
418
static int
SDL_SYS_CDResume(SDL_CD * cdrom)
419
420
{
if (fakeCD) {
421
SDL_SetError(kErrorFakeDevice);
422
423
return -1;
}
424
425
426
427
428
Lock();
if (PlayFile() < 0) {
Unlock();
429
return -2;
430
}
431
432
status = CD_PLAYING;
433
434
435
Unlock();
436
437
438
439
return 0;
}
/* Stop playback */
440
441
static int
SDL_SYS_CDStop(SDL_CD * cdrom)
442
443
{
if (fakeCD) {
444
SDL_SetError(kErrorFakeDevice);
445
446
return -1;
}
447
448
449
450
451
Lock();
if (PauseFile() < 0) {
Unlock();
452
return -2;
453
}
454
455
456
if (ReleaseFile() < 0) {
Unlock();
457
return -3;
458
}
459
460
status = CD_STOPPED;
461
462
463
Unlock();
464
465
466
467
return 0;
}
/* Eject the CD-ROM (Unmount the volume) */
468
469
static int
SDL_SYS_CDEject(SDL_CD * cdrom)
470
471
{
OSStatus err;
472
473
pid_t dissenter;
474
if (fakeCD) {
475
SDL_SetError(kErrorFakeDevice);
476
477
return -1;
}
478
479
480
481
482
Lock();
if (PauseFile() < 0) {
Unlock();
483
return -2;
484
}
485
486
487
if (ReleaseFile() < 0) {
Unlock();
488
return -3;
489
}
490
491
status = CD_STOPPED;
492
493
494
495
496
497
498
499
500
501
/* Eject the volume */
err = FSEjectVolumeSync(volumes[cdrom->id], kNilOptions, &dissenter);
if (err != noErr) {
Unlock();
SDL_SetError("PBUnmountVol returned %d", err);
return -4;
}
502
503
504
505
status = CD_TRAYEMPTY;
/* Invalidate volume and track info */
volumes[cdrom->id] = 0;
506
free(tracks[cdrom->id]);
507
tracks[cdrom->id] = NULL;
508
509
510
Unlock();
511
512
513
514
return 0;
}
/* Close the CD-ROM */
515
516
static void
SDL_SYS_CDClose(SDL_CD * cdrom)
517
518
519
520
521
{
currentDrive = -1;
return;
}
522
#endif /* SDL_CDROM_MACOSX */
523
/* vi: set ts=4 sw=4 expandtab: */