Mercurial > sdl-ios-xcode
comparison src/cdrom/SDL_cdrom.c @ 1662:782fd950bd46 SDL-1.3
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.
WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.
The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce
The headers are being converted to automatically generate doxygen documentation.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 28 May 2006 13:04:16 +0000 |
parents | d910939febfa |
children | 4da1ee79c9af |
comparison
equal
deleted
inserted
replaced
1661:281d3f4870e5 | 1662:782fd950bd46 |
---|---|
25 | 25 |
26 #include "SDL_cdrom.h" | 26 #include "SDL_cdrom.h" |
27 #include "SDL_syscdrom.h" | 27 #include "SDL_syscdrom.h" |
28 | 28 |
29 #if !defined(__MACOS__) | 29 #if !defined(__MACOS__) |
30 #define CLIP_FRAMES 10 /* Some CD-ROMs won't go all the way */ | 30 #define CLIP_FRAMES 10 /* Some CD-ROMs won't go all the way */ |
31 #endif | 31 #endif |
32 | 32 |
33 static int SDL_cdinitted = 0; | 33 static int SDL_cdinitted = 0; |
34 static SDL_CD *default_cdrom; | 34 static SDL_CD *default_cdrom; |
35 | 35 |
36 /* The system level CD-ROM control functions */ | 36 /* The system level CD-ROM control functions */ |
37 struct CDcaps SDL_CDcaps = { | 37 struct CDcaps SDL_CDcaps = { |
38 NULL, /* Name */ | 38 NULL, /* Name */ |
39 NULL, /* Open */ | 39 NULL, /* Open */ |
40 NULL, /* GetTOC */ | 40 NULL, /* GetTOC */ |
41 NULL, /* Status */ | 41 NULL, /* Status */ |
42 NULL, /* Play */ | 42 NULL, /* Play */ |
43 NULL, /* Pause */ | 43 NULL, /* Pause */ |
44 NULL, /* Resume */ | 44 NULL, /* Resume */ |
45 NULL, /* Stop */ | 45 NULL, /* Stop */ |
46 NULL, /* Eject */ | 46 NULL, /* Eject */ |
47 NULL, /* Close */ | 47 NULL, /* Close */ |
48 }; | 48 }; |
49 int SDL_numcds; | 49 int SDL_numcds; |
50 | 50 |
51 int SDL_CDROMInit(void) | 51 int |
52 { | 52 SDL_CDROMInit (void) |
53 int retval; | 53 { |
54 | 54 int retval; |
55 SDL_numcds = 0; | 55 |
56 retval = SDL_SYS_CDInit(); | 56 SDL_numcds = 0; |
57 if ( retval == 0 ) { | 57 retval = SDL_SYS_CDInit (); |
58 SDL_cdinitted = 1; | 58 if (retval == 0) { |
59 } | 59 SDL_cdinitted = 1; |
60 default_cdrom = NULL; | 60 } |
61 return(retval); | 61 default_cdrom = NULL; |
62 return (retval); | |
62 } | 63 } |
63 | 64 |
64 /* Check to see if the CD-ROM subsystem has been initialized */ | 65 /* Check to see if the CD-ROM subsystem has been initialized */ |
65 static int CheckInit(int check_cdrom, SDL_CD **cdrom) | 66 static int |
66 { | 67 CheckInit (int check_cdrom, SDL_CD ** cdrom) |
67 int okay; | 68 { |
68 | 69 int okay; |
69 okay = SDL_cdinitted; | 70 |
70 if ( check_cdrom && (*cdrom == NULL) ) { | 71 okay = SDL_cdinitted; |
71 *cdrom = default_cdrom; | 72 if (check_cdrom && (*cdrom == NULL)) { |
72 if ( *cdrom == NULL ) { | 73 *cdrom = default_cdrom; |
73 SDL_SetError("CD-ROM not opened"); | 74 if (*cdrom == NULL) { |
74 okay = 0; | 75 SDL_SetError ("CD-ROM not opened"); |
75 } | 76 okay = 0; |
76 } | 77 } |
77 if ( ! SDL_cdinitted ) { | 78 } |
78 SDL_SetError("CD-ROM subsystem not initialized"); | 79 if (!SDL_cdinitted) { |
79 } | 80 SDL_SetError ("CD-ROM subsystem not initialized"); |
80 return(okay); | 81 } |
81 } | 82 return (okay); |
82 | 83 } |
83 int SDL_CDNumDrives(void) | 84 |
84 { | 85 int |
85 if ( ! CheckInit(0, NULL) ) { | 86 SDL_CDNumDrives (void) |
86 return(-1); | 87 { |
87 } | 88 if (!CheckInit (0, NULL)) { |
88 return(SDL_numcds); | 89 return (-1); |
89 } | 90 } |
90 | 91 return (SDL_numcds); |
91 const char *SDL_CDName(int drive) | 92 } |
92 { | 93 |
93 if ( ! CheckInit(0, NULL) ) { | 94 const char * |
94 return(NULL); | 95 SDL_CDName (int drive) |
95 } | 96 { |
96 if ( drive >= SDL_numcds ) { | 97 if (!CheckInit (0, NULL)) { |
97 SDL_SetError("Invalid CD-ROM drive index"); | 98 return (NULL); |
98 return(NULL); | 99 } |
99 } | 100 if (drive >= SDL_numcds) { |
100 if ( SDL_CDcaps.Name ) { | 101 SDL_SetError ("Invalid CD-ROM drive index"); |
101 return(SDL_CDcaps.Name(drive)); | 102 return (NULL); |
102 } else { | 103 } |
103 return(""); | 104 if (SDL_CDcaps.Name) { |
104 } | 105 return (SDL_CDcaps.Name (drive)); |
105 } | 106 } else { |
106 | 107 return (""); |
107 SDL_CD *SDL_CDOpen(int drive) | 108 } |
108 { | 109 } |
109 struct SDL_CD *cdrom; | 110 |
110 | 111 SDL_CD * |
111 if ( ! CheckInit(0, NULL) ) { | 112 SDL_CDOpen (int drive) |
112 return(NULL); | 113 { |
113 } | 114 struct SDL_CD *cdrom; |
114 if ( drive >= SDL_numcds ) { | 115 |
115 SDL_SetError("Invalid CD-ROM drive index"); | 116 if (!CheckInit (0, NULL)) { |
116 return(NULL); | 117 return (NULL); |
117 } | 118 } |
118 cdrom = (SDL_CD *)SDL_malloc(sizeof(*cdrom)); | 119 if (drive >= SDL_numcds) { |
119 if ( cdrom == NULL ) { | 120 SDL_SetError ("Invalid CD-ROM drive index"); |
120 SDL_OutOfMemory(); | 121 return (NULL); |
121 return(NULL); | 122 } |
122 } | 123 cdrom = (SDL_CD *) SDL_malloc (sizeof (*cdrom)); |
123 SDL_memset(cdrom, 0, sizeof(*cdrom)); | 124 if (cdrom == NULL) { |
124 cdrom->id = SDL_CDcaps.Open(drive); | 125 SDL_OutOfMemory (); |
125 if ( cdrom->id < 0 ) { | 126 return (NULL); |
126 SDL_free(cdrom); | 127 } |
127 return(NULL); | 128 SDL_memset (cdrom, 0, sizeof (*cdrom)); |
128 } | 129 cdrom->id = SDL_CDcaps.Open (drive); |
129 default_cdrom = cdrom; | 130 if (cdrom->id < 0) { |
130 return(cdrom); | 131 SDL_free (cdrom); |
131 } | 132 return (NULL); |
132 | 133 } |
133 CDstatus SDL_CDStatus(SDL_CD *cdrom) | 134 default_cdrom = cdrom; |
134 { | 135 return (cdrom); |
135 CDstatus status; | 136 } |
136 int i; | 137 |
137 Uint32 position; | 138 CDstatus |
138 | 139 SDL_CDStatus (SDL_CD * cdrom) |
139 /* Check if the CD-ROM subsystem has been initialized */ | 140 { |
140 if ( ! CheckInit(1, &cdrom) ) { | 141 CDstatus status; |
141 return(CD_ERROR); | 142 int i; |
142 } | 143 Uint32 position; |
143 | 144 |
144 /* Get the current status of the drive */ | 145 /* Check if the CD-ROM subsystem has been initialized */ |
145 cdrom->numtracks = 0; | 146 if (!CheckInit (1, &cdrom)) { |
146 cdrom->cur_track = 0; | 147 return (CD_ERROR); |
147 cdrom->cur_frame = 0; | 148 } |
148 status = SDL_CDcaps.Status(cdrom, &i); | 149 |
149 position = (Uint32)i; | 150 /* Get the current status of the drive */ |
150 cdrom->status = status; | 151 cdrom->numtracks = 0; |
151 | 152 cdrom->cur_track = 0; |
152 /* Get the table of contents, if there's a CD available */ | 153 cdrom->cur_frame = 0; |
153 if ( CD_INDRIVE(status) ) { | 154 status = SDL_CDcaps.Status (cdrom, &i); |
154 if ( SDL_CDcaps.GetTOC(cdrom) < 0 ) { | 155 position = (Uint32) i; |
155 status = CD_ERROR; | 156 cdrom->status = status; |
156 } | 157 |
157 /* If the drive is playing, get current play position */ | 158 /* Get the table of contents, if there's a CD available */ |
158 if ( (status == CD_PLAYING) || (status == CD_PAUSED) ) { | 159 if (CD_INDRIVE (status)) { |
159 for ( i=1; cdrom->track[i].offset <= position; ++i ) { | 160 if (SDL_CDcaps.GetTOC (cdrom) < 0) { |
160 /* Keep looking */; | 161 status = CD_ERROR; |
161 } | 162 } |
163 /* If the drive is playing, get current play position */ | |
164 if ((status == CD_PLAYING) || (status == CD_PAUSED)) { | |
165 for (i = 1; cdrom->track[i].offset <= position; ++i) { | |
166 /* Keep looking */ ; | |
167 } | |
162 #ifdef DEBUG_CDROM | 168 #ifdef DEBUG_CDROM |
163 fprintf(stderr, "Current position: %d, track = %d (offset is %d)\n", | 169 fprintf (stderr, |
164 position, i-1, cdrom->track[i-1].offset); | 170 "Current position: %d, track = %d (offset is %d)\n", |
171 position, i - 1, cdrom->track[i - 1].offset); | |
165 #endif | 172 #endif |
166 cdrom->cur_track = i-1; | 173 cdrom->cur_track = i - 1; |
167 position -= cdrom->track[cdrom->cur_track].offset; | 174 position -= cdrom->track[cdrom->cur_track].offset; |
168 cdrom->cur_frame = position; | 175 cdrom->cur_frame = position; |
169 } | 176 } |
170 } | 177 } |
171 return(status); | 178 return (status); |
172 } | 179 } |
173 | 180 |
174 int SDL_CDPlayTracks(SDL_CD *cdrom, | 181 int |
175 int strack, int sframe, int ntracks, int nframes) | 182 SDL_CDPlayTracks (SDL_CD * cdrom, |
176 { | 183 int strack, int sframe, int ntracks, int nframes) |
177 int etrack, eframe; | 184 { |
178 int start, length; | 185 int etrack, eframe; |
179 | 186 int start, length; |
180 /* Check if the CD-ROM subsystem has been initialized */ | 187 |
181 if ( ! CheckInit(1, &cdrom) ) { | 188 /* Check if the CD-ROM subsystem has been initialized */ |
182 return(CD_ERROR); | 189 if (!CheckInit (1, &cdrom)) { |
183 } | 190 return (CD_ERROR); |
184 | 191 } |
185 /* Determine the starting and ending tracks */ | 192 |
186 if ( (strack < 0) || (strack >= cdrom->numtracks) ) { | 193 /* Determine the starting and ending tracks */ |
187 SDL_SetError("Invalid starting track"); | 194 if ((strack < 0) || (strack >= cdrom->numtracks)) { |
188 return(CD_ERROR); | 195 SDL_SetError ("Invalid starting track"); |
189 } | 196 return (CD_ERROR); |
190 if ( ! ntracks && ! nframes ) { | 197 } |
191 etrack = cdrom->numtracks; | 198 if (!ntracks && !nframes) { |
192 eframe = 0; | 199 etrack = cdrom->numtracks; |
193 } else { | 200 eframe = 0; |
194 etrack = strack+ntracks; | 201 } else { |
195 if ( etrack == strack ) { | 202 etrack = strack + ntracks; |
196 eframe = sframe + nframes; | 203 if (etrack == strack) { |
197 } else { | 204 eframe = sframe + nframes; |
198 eframe = nframes; | 205 } else { |
199 } | 206 eframe = nframes; |
200 } | 207 } |
201 if ( etrack > cdrom->numtracks ) { | 208 } |
202 SDL_SetError("Invalid play length"); | 209 if (etrack > cdrom->numtracks) { |
203 return(CD_ERROR); | 210 SDL_SetError ("Invalid play length"); |
204 } | 211 return (CD_ERROR); |
205 | 212 } |
206 /* Skip data tracks and verify frame offsets */ | 213 |
207 while ( (strack <= etrack) && | 214 /* Skip data tracks and verify frame offsets */ |
208 (cdrom->track[strack].type == SDL_DATA_TRACK) ) { | 215 while ((strack <= etrack) && |
209 ++strack; | 216 (cdrom->track[strack].type == SDL_DATA_TRACK)) { |
210 } | 217 ++strack; |
211 if ( sframe >= (int)cdrom->track[strack].length ) { | 218 } |
212 SDL_SetError("Invalid starting frame for track %d", strack); | 219 if (sframe >= (int) cdrom->track[strack].length) { |
213 return(CD_ERROR); | 220 SDL_SetError ("Invalid starting frame for track %d", strack); |
214 } | 221 return (CD_ERROR); |
215 while ( (etrack > strack) && | 222 } |
216 (cdrom->track[etrack-1].type == SDL_DATA_TRACK) ) { | 223 while ((etrack > strack) && |
217 --etrack; | 224 (cdrom->track[etrack - 1].type == SDL_DATA_TRACK)) { |
218 } | 225 --etrack; |
219 if ( eframe > (int)cdrom->track[etrack].length ) { | 226 } |
220 SDL_SetError("Invalid ending frame for track %d", etrack); | 227 if (eframe > (int) cdrom->track[etrack].length) { |
221 return(CD_ERROR); | 228 SDL_SetError ("Invalid ending frame for track %d", etrack); |
222 } | 229 return (CD_ERROR); |
223 | 230 } |
224 /* Determine start frame and play length */ | 231 |
225 start = (cdrom->track[strack].offset+sframe); | 232 /* Determine start frame and play length */ |
226 length = (cdrom->track[etrack].offset+eframe)-start; | 233 start = (cdrom->track[strack].offset + sframe); |
234 length = (cdrom->track[etrack].offset + eframe) - start; | |
227 #ifdef CLIP_FRAMES | 235 #ifdef CLIP_FRAMES |
228 /* I've never seen this necessary, but xmcd does it.. */ | 236 /* I've never seen this necessary, but xmcd does it.. */ |
229 length -= CLIP_FRAMES; /* CLIP_FRAMES == 10 */ | 237 length -= CLIP_FRAMES; /* CLIP_FRAMES == 10 */ |
230 #endif | 238 #endif |
231 if ( length < 0 ) { | 239 if (length < 0) { |
232 return(0); | 240 return (0); |
233 } | 241 } |
234 | 242 |
235 /* Play! */ | 243 /* Play! */ |
236 #ifdef DEBUG_CDROM | 244 #ifdef DEBUG_CDROM |
237 fprintf(stderr, "Playing %d frames at offset %d\n", length, start); | 245 fprintf (stderr, "Playing %d frames at offset %d\n", length, start); |
238 #endif | 246 #endif |
239 return(SDL_CDcaps.Play(cdrom, start, length)); | 247 return (SDL_CDcaps.Play (cdrom, start, length)); |
240 } | 248 } |
241 | 249 |
242 int SDL_CDPlay(SDL_CD *cdrom, int sframe, int length) | 250 int |
243 { | 251 SDL_CDPlay (SDL_CD * cdrom, int sframe, int length) |
244 /* Check if the CD-ROM subsystem has been initialized */ | 252 { |
245 if ( ! CheckInit(1, &cdrom) ) { | 253 /* Check if the CD-ROM subsystem has been initialized */ |
246 return(CD_ERROR); | 254 if (!CheckInit (1, &cdrom)) { |
247 } | 255 return (CD_ERROR); |
248 | 256 } |
249 return(SDL_CDcaps.Play(cdrom, sframe, length)); | 257 |
250 } | 258 return (SDL_CDcaps.Play (cdrom, sframe, length)); |
251 | 259 } |
252 int SDL_CDPause(SDL_CD *cdrom) | 260 |
253 { | 261 int |
254 CDstatus status; | 262 SDL_CDPause (SDL_CD * cdrom) |
255 int retval; | 263 { |
256 | 264 CDstatus status; |
257 /* Check if the CD-ROM subsystem has been initialized */ | 265 int retval; |
258 if ( ! CheckInit(1, &cdrom) ) { | 266 |
259 return(CD_ERROR); | 267 /* Check if the CD-ROM subsystem has been initialized */ |
260 } | 268 if (!CheckInit (1, &cdrom)) { |
261 | 269 return (CD_ERROR); |
262 status = SDL_CDcaps.Status(cdrom, NULL); | 270 } |
263 switch (status) { | 271 |
264 case CD_PLAYING: | 272 status = SDL_CDcaps.Status (cdrom, NULL); |
265 retval = SDL_CDcaps.Pause(cdrom); | 273 switch (status) { |
266 break; | 274 case CD_PLAYING: |
267 default: | 275 retval = SDL_CDcaps.Pause (cdrom); |
268 retval = 0; | 276 break; |
269 break; | 277 default: |
270 } | 278 retval = 0; |
271 return(retval); | 279 break; |
272 } | 280 } |
273 | 281 return (retval); |
274 int SDL_CDResume(SDL_CD *cdrom) | 282 } |
275 { | 283 |
276 CDstatus status; | 284 int |
277 int retval; | 285 SDL_CDResume (SDL_CD * cdrom) |
278 | 286 { |
279 /* Check if the CD-ROM subsystem has been initialized */ | 287 CDstatus status; |
280 if ( ! CheckInit(1, &cdrom) ) { | 288 int retval; |
281 return(CD_ERROR); | 289 |
282 } | 290 /* Check if the CD-ROM subsystem has been initialized */ |
283 | 291 if (!CheckInit (1, &cdrom)) { |
284 status = SDL_CDcaps.Status(cdrom, NULL); | 292 return (CD_ERROR); |
285 switch (status) { | 293 } |
286 case CD_PAUSED: | 294 |
287 retval = SDL_CDcaps.Resume(cdrom); | 295 status = SDL_CDcaps.Status (cdrom, NULL); |
288 default: | 296 switch (status) { |
289 retval = 0; | 297 case CD_PAUSED: |
290 break; | 298 retval = SDL_CDcaps.Resume (cdrom); |
291 } | 299 default: |
292 return(retval); | 300 retval = 0; |
293 } | 301 break; |
294 | 302 } |
295 int SDL_CDStop(SDL_CD *cdrom) | 303 return (retval); |
296 { | 304 } |
297 CDstatus status; | 305 |
298 int retval; | 306 int |
299 | 307 SDL_CDStop (SDL_CD * cdrom) |
300 /* Check if the CD-ROM subsystem has been initialized */ | 308 { |
301 if ( ! CheckInit(1, &cdrom) ) { | 309 CDstatus status; |
302 return(CD_ERROR); | 310 int retval; |
303 } | 311 |
304 | 312 /* Check if the CD-ROM subsystem has been initialized */ |
305 status = SDL_CDcaps.Status(cdrom, NULL); | 313 if (!CheckInit (1, &cdrom)) { |
306 switch (status) { | 314 return (CD_ERROR); |
307 case CD_PLAYING: | 315 } |
308 case CD_PAUSED: | 316 |
309 retval = SDL_CDcaps.Stop(cdrom); | 317 status = SDL_CDcaps.Status (cdrom, NULL); |
310 default: | 318 switch (status) { |
311 retval = 0; | 319 case CD_PLAYING: |
312 break; | 320 case CD_PAUSED: |
313 } | 321 retval = SDL_CDcaps.Stop (cdrom); |
314 return(retval); | 322 default: |
315 } | 323 retval = 0; |
316 | 324 break; |
317 int SDL_CDEject(SDL_CD *cdrom) | 325 } |
318 { | 326 return (retval); |
319 /* Check if the CD-ROM subsystem has been initialized */ | 327 } |
320 if ( ! CheckInit(1, &cdrom) ) { | 328 |
321 return(CD_ERROR); | 329 int |
322 } | 330 SDL_CDEject (SDL_CD * cdrom) |
323 return(SDL_CDcaps.Eject(cdrom)); | 331 { |
324 } | 332 /* Check if the CD-ROM subsystem has been initialized */ |
325 | 333 if (!CheckInit (1, &cdrom)) { |
326 void SDL_CDClose(SDL_CD *cdrom) | 334 return (CD_ERROR); |
327 { | 335 } |
328 /* Check if the CD-ROM subsystem has been initialized */ | 336 return (SDL_CDcaps.Eject (cdrom)); |
329 if ( ! CheckInit(1, &cdrom) ) { | 337 } |
330 return; | 338 |
331 } | 339 void |
332 SDL_CDcaps.Close(cdrom); | 340 SDL_CDClose (SDL_CD * cdrom) |
333 SDL_free(cdrom); | 341 { |
334 default_cdrom = NULL; | 342 /* Check if the CD-ROM subsystem has been initialized */ |
335 } | 343 if (!CheckInit (1, &cdrom)) { |
336 | 344 return; |
337 void SDL_CDROMQuit(void) | 345 } |
338 { | 346 SDL_CDcaps.Close (cdrom); |
339 SDL_SYS_CDQuit(); | 347 SDL_free (cdrom); |
340 SDL_cdinitted = 0; | 348 default_cdrom = NULL; |
341 } | 349 } |
350 | |
351 void | |
352 SDL_CDROMQuit (void) | |
353 { | |
354 SDL_SYS_CDQuit (); | |
355 SDL_cdinitted = 0; | |
356 } | |
357 | |
358 /* vi: set ts=4 sw=4 expandtab: */ |