Mercurial > sdl-ios-xcode
annotate src/cdrom/SDL_cdrom.c @ 1359:1e4ba2e063b4
*** empty log message ***
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 10 Feb 2006 07:03:22 +0000 |
parents | c71e05b4dc2e |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* This is the CD-audio control API for Simple DirectMedia Layer */ | |
24 | |
25 #include "SDL_cdrom.h" | |
26 #include "SDL_syscdrom.h" | |
27 | |
28 #if !defined(macintosh) | |
29 #define CLIP_FRAMES 10 /* Some CD-ROMs won't go all the way */ | |
30 #endif | |
31 | |
32 static int SDL_cdinitted = 0; | |
33 static SDL_CD *default_cdrom; | |
34 | |
35 /* The system level CD-ROM control functions */ | |
36 struct CDcaps SDL_CDcaps = { | |
37 NULL, /* Name */ | |
38 NULL, /* Open */ | |
39 NULL, /* GetTOC */ | |
40 NULL, /* Status */ | |
41 NULL, /* Play */ | |
42 NULL, /* Pause */ | |
43 NULL, /* Resume */ | |
44 NULL, /* Stop */ | |
45 NULL, /* Eject */ | |
46 NULL, /* Close */ | |
47 }; | |
48 int SDL_numcds; | |
49 | |
50 int SDL_CDROMInit(void) | |
51 { | |
52 int retval; | |
53 | |
54 SDL_numcds = 0; | |
55 retval = SDL_SYS_CDInit(); | |
56 if ( retval == 0 ) { | |
57 SDL_cdinitted = 1; | |
58 } | |
59 default_cdrom = NULL; | |
60 return(retval); | |
61 } | |
62 | |
63 /* Check to see if the CD-ROM subsystem has been initialized */ | |
64 static int CheckInit(int check_cdrom, SDL_CD **cdrom) | |
65 { | |
66 int okay; | |
67 | |
68 okay = SDL_cdinitted; | |
69 if ( check_cdrom && (*cdrom == NULL) ) { | |
70 *cdrom = default_cdrom; | |
71 if ( *cdrom == NULL ) { | |
72 SDL_SetError("CD-ROM not opened"); | |
73 okay = 0; | |
74 } | |
75 } | |
76 if ( ! SDL_cdinitted ) { | |
77 SDL_SetError("CD-ROM subsystem not initialized"); | |
78 } | |
79 return(okay); | |
80 } | |
81 | |
82 int SDL_CDNumDrives(void) | |
83 { | |
84 if ( ! CheckInit(0, NULL) ) { | |
85 return(-1); | |
86 } | |
87 return(SDL_numcds); | |
88 } | |
89 | |
90 const char *SDL_CDName(int drive) | |
91 { | |
92 if ( ! CheckInit(0, NULL) ) { | |
93 return(NULL); | |
94 } | |
95 if ( drive >= SDL_numcds ) { | |
96 SDL_SetError("Invalid CD-ROM drive index"); | |
97 return(NULL); | |
98 } | |
99 if ( SDL_CDcaps.Name ) { | |
100 return(SDL_CDcaps.Name(drive)); | |
101 } else { | |
102 return(""); | |
103 } | |
104 } | |
105 | |
106 SDL_CD *SDL_CDOpen(int drive) | |
107 { | |
108 struct SDL_CD *cdrom; | |
109 | |
110 if ( ! CheckInit(0, NULL) ) { | |
111 return(NULL); | |
112 } | |
113 if ( drive >= SDL_numcds ) { | |
114 SDL_SetError("Invalid CD-ROM drive index"); | |
115 return(NULL); | |
116 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
117 cdrom = (SDL_CD *)SDL_malloc(sizeof(*cdrom)); |
0 | 118 if ( cdrom == NULL ) { |
119 SDL_OutOfMemory(); | |
120 return(NULL); | |
121 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
122 SDL_memset(cdrom, 0, sizeof(*cdrom)); |
0 | 123 cdrom->id = SDL_CDcaps.Open(drive); |
124 if ( cdrom->id < 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
125 SDL_free(cdrom); |
0 | 126 return(NULL); |
127 } | |
128 default_cdrom = cdrom; | |
129 return(cdrom); | |
130 } | |
131 | |
132 CDstatus SDL_CDStatus(SDL_CD *cdrom) | |
133 { | |
134 CDstatus status; | |
135 int i; | |
136 Uint32 position; | |
137 | |
138 /* Check if the CD-ROM subsystem has been initialized */ | |
139 if ( ! CheckInit(1, &cdrom) ) { | |
140 return(CD_ERROR); | |
141 } | |
142 | |
143 /* Get the current status of the drive */ | |
144 cdrom->numtracks = 0; | |
145 cdrom->cur_track = 0; | |
146 cdrom->cur_frame = 0; | |
147 status = SDL_CDcaps.Status(cdrom, &i); | |
148 position = (Uint32)i; | |
149 cdrom->status = status; | |
150 | |
151 /* Get the table of contents, if there's a CD available */ | |
152 if ( CD_INDRIVE(status) ) { | |
153 if ( SDL_CDcaps.GetTOC(cdrom) < 0 ) { | |
154 status = CD_ERROR; | |
155 } | |
156 /* If the drive is playing, get current play position */ | |
157 if ( (status == CD_PLAYING) || (status == CD_PAUSED) ) { | |
158 for ( i=1; cdrom->track[i].offset <= position; ++i ) { | |
159 /* Keep looking */; | |
160 } | |
161 #ifdef DEBUG_CDROM | |
162 fprintf(stderr, "Current position: %d, track = %d (offset is %d)\n", | |
163 position, i-1, cdrom->track[i-1].offset); | |
164 #endif | |
165 cdrom->cur_track = i-1; | |
166 position -= cdrom->track[cdrom->cur_track].offset; | |
167 cdrom->cur_frame = position; | |
168 } | |
169 } | |
170 return(status); | |
171 } | |
172 | |
173 int SDL_CDPlayTracks(SDL_CD *cdrom, | |
174 int strack, int sframe, int ntracks, int nframes) | |
175 { | |
176 int etrack, eframe; | |
177 int start, length; | |
178 | |
179 /* Check if the CD-ROM subsystem has been initialized */ | |
180 if ( ! CheckInit(1, &cdrom) ) { | |
181 return(CD_ERROR); | |
182 } | |
183 | |
184 /* Determine the starting and ending tracks */ | |
185 if ( (strack < 0) || (strack >= cdrom->numtracks) ) { | |
186 SDL_SetError("Invalid starting track"); | |
187 return(CD_ERROR); | |
188 } | |
189 if ( ! ntracks && ! nframes ) { | |
190 etrack = cdrom->numtracks; | |
191 eframe = 0; | |
192 } else { | |
193 etrack = strack+ntracks; | |
194 if ( etrack == strack ) { | |
195 eframe = sframe + nframes; | |
196 } else { | |
197 eframe = nframes; | |
198 } | |
199 } | |
200 if ( etrack > cdrom->numtracks ) { | |
201 SDL_SetError("Invalid play length"); | |
202 return(CD_ERROR); | |
203 } | |
204 | |
205 /* Skip data tracks and verify frame offsets */ | |
206 while ( (strack <= etrack) && | |
207 (cdrom->track[strack].type == SDL_DATA_TRACK) ) { | |
208 ++strack; | |
209 } | |
210 if ( sframe >= (int)cdrom->track[strack].length ) { | |
211 SDL_SetError("Invalid starting frame for track %d", strack); | |
212 return(CD_ERROR); | |
213 } | |
214 while ( (etrack > strack) && | |
215 (cdrom->track[etrack-1].type == SDL_DATA_TRACK) ) { | |
216 --etrack; | |
217 } | |
218 if ( eframe > (int)cdrom->track[etrack].length ) { | |
219 SDL_SetError("Invalid ending frame for track %d", etrack); | |
220 return(CD_ERROR); | |
221 } | |
222 | |
223 /* Determine start frame and play length */ | |
224 start = (cdrom->track[strack].offset+sframe); | |
225 length = (cdrom->track[etrack].offset+eframe)-start; | |
226 #ifdef CLIP_FRAMES | |
227 /* I've never seen this necessary, but xmcd does it.. */ | |
228 length -= CLIP_FRAMES; /* CLIP_FRAMES == 10 */ | |
229 #endif | |
230 if ( length < 0 ) { | |
231 return(0); | |
232 } | |
233 | |
234 /* Play! */ | |
235 #ifdef DEBUG_CDROM | |
236 fprintf(stderr, "Playing %d frames at offset %d\n", length, start); | |
237 #endif | |
238 return(SDL_CDcaps.Play(cdrom, start, length)); | |
239 } | |
240 | |
241 int SDL_CDPlay(SDL_CD *cdrom, int sframe, int length) | |
242 { | |
243 /* Check if the CD-ROM subsystem has been initialized */ | |
244 if ( ! CheckInit(1, &cdrom) ) { | |
245 return(CD_ERROR); | |
246 } | |
247 | |
248 return(SDL_CDcaps.Play(cdrom, sframe, length)); | |
249 } | |
250 | |
251 int SDL_CDPause(SDL_CD *cdrom) | |
252 { | |
253 CDstatus status; | |
254 int retval; | |
255 | |
256 /* Check if the CD-ROM subsystem has been initialized */ | |
257 if ( ! CheckInit(1, &cdrom) ) { | |
258 return(CD_ERROR); | |
259 } | |
260 | |
261 status = SDL_CDcaps.Status(cdrom, NULL); | |
262 switch (status) { | |
263 case CD_PLAYING: | |
264 retval = SDL_CDcaps.Pause(cdrom); | |
265 break; | |
266 default: | |
267 retval = 0; | |
268 break; | |
269 } | |
270 return(retval); | |
271 } | |
272 | |
273 int SDL_CDResume(SDL_CD *cdrom) | |
274 { | |
275 CDstatus status; | |
276 int retval; | |
277 | |
278 /* Check if the CD-ROM subsystem has been initialized */ | |
279 if ( ! CheckInit(1, &cdrom) ) { | |
280 return(CD_ERROR); | |
281 } | |
282 | |
283 status = SDL_CDcaps.Status(cdrom, NULL); | |
284 switch (status) { | |
285 case CD_PAUSED: | |
286 retval = SDL_CDcaps.Resume(cdrom); | |
287 default: | |
288 retval = 0; | |
289 break; | |
290 } | |
291 return(retval); | |
292 } | |
293 | |
294 int SDL_CDStop(SDL_CD *cdrom) | |
295 { | |
296 CDstatus status; | |
297 int retval; | |
298 | |
299 /* Check if the CD-ROM subsystem has been initialized */ | |
300 if ( ! CheckInit(1, &cdrom) ) { | |
301 return(CD_ERROR); | |
302 } | |
303 | |
304 status = SDL_CDcaps.Status(cdrom, NULL); | |
305 switch (status) { | |
306 case CD_PLAYING: | |
307 case CD_PAUSED: | |
308 retval = SDL_CDcaps.Stop(cdrom); | |
309 default: | |
310 retval = 0; | |
311 break; | |
312 } | |
313 return(retval); | |
314 } | |
315 | |
316 int SDL_CDEject(SDL_CD *cdrom) | |
317 { | |
318 /* Check if the CD-ROM subsystem has been initialized */ | |
319 if ( ! CheckInit(1, &cdrom) ) { | |
320 return(CD_ERROR); | |
321 } | |
322 return(SDL_CDcaps.Eject(cdrom)); | |
323 } | |
324 | |
325 void SDL_CDClose(SDL_CD *cdrom) | |
326 { | |
327 /* Check if the CD-ROM subsystem has been initialized */ | |
328 if ( ! CheckInit(1, &cdrom) ) { | |
329 return; | |
330 } | |
331 SDL_CDcaps.Close(cdrom); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
332 SDL_free(cdrom); |
0 | 333 default_cdrom = NULL; |
334 } | |
335 | |
336 void SDL_CDROMQuit(void) | |
337 { | |
338 SDL_SYS_CDQuit(); | |
339 SDL_cdinitted = 0; | |
340 } |