comparison src/cdrom/SDL_cdrom.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
47 NULL, /* Close */ 47 NULL, /* Close */
48 }; 48 };
49 int SDL_numcds; 49 int SDL_numcds;
50 50
51 int 51 int
52 SDL_CDROMInit (void) 52 SDL_CDROMInit(void)
53 { 53 {
54 int retval; 54 int retval;
55 55
56 SDL_numcds = 0; 56 SDL_numcds = 0;
57 retval = SDL_SYS_CDInit (); 57 retval = SDL_SYS_CDInit();
58 if (retval == 0) { 58 if (retval == 0) {
59 SDL_cdinitted = 1; 59 SDL_cdinitted = 1;
60 } 60 }
61 default_cdrom = NULL; 61 default_cdrom = NULL;
62 return (retval); 62 return (retval);
63 } 63 }
64 64
65 /* Check to see if the CD-ROM subsystem has been initialized */ 65 /* Check to see if the CD-ROM subsystem has been initialized */
66 static int 66 static int
67 CheckInit (int check_cdrom, SDL_CD ** cdrom) 67 CheckInit(int check_cdrom, SDL_CD ** cdrom)
68 { 68 {
69 int okay; 69 int okay;
70 70
71 okay = SDL_cdinitted; 71 okay = SDL_cdinitted;
72 if (check_cdrom && (*cdrom == NULL)) { 72 if (check_cdrom && (*cdrom == NULL)) {
73 *cdrom = default_cdrom; 73 *cdrom = default_cdrom;
74 if (*cdrom == NULL) { 74 if (*cdrom == NULL) {
75 SDL_SetError ("CD-ROM not opened"); 75 SDL_SetError("CD-ROM not opened");
76 okay = 0; 76 okay = 0;
77 } 77 }
78 } 78 }
79 if (!SDL_cdinitted) { 79 if (!SDL_cdinitted) {
80 SDL_SetError ("CD-ROM subsystem not initialized"); 80 SDL_SetError("CD-ROM subsystem not initialized");
81 } 81 }
82 return (okay); 82 return (okay);
83 } 83 }
84 84
85 int 85 int
86 SDL_CDNumDrives (void) 86 SDL_CDNumDrives(void)
87 { 87 {
88 if (!CheckInit (0, NULL)) { 88 if (!CheckInit(0, NULL)) {
89 return (-1); 89 return (-1);
90 } 90 }
91 return (SDL_numcds); 91 return (SDL_numcds);
92 } 92 }
93 93
94 const char * 94 const char *
95 SDL_CDName (int drive) 95 SDL_CDName(int drive)
96 { 96 {
97 if (!CheckInit (0, NULL)) { 97 if (!CheckInit(0, NULL)) {
98 return (NULL); 98 return (NULL);
99 } 99 }
100 if (drive >= SDL_numcds) { 100 if (drive >= SDL_numcds) {
101 SDL_SetError ("Invalid CD-ROM drive index"); 101 SDL_SetError("Invalid CD-ROM drive index");
102 return (NULL); 102 return (NULL);
103 } 103 }
104 if (SDL_CDcaps.Name) { 104 if (SDL_CDcaps.Name) {
105 return (SDL_CDcaps.Name (drive)); 105 return (SDL_CDcaps.Name(drive));
106 } else { 106 } else {
107 return (""); 107 return ("");
108 } 108 }
109 } 109 }
110 110
111 SDL_CD * 111 SDL_CD *
112 SDL_CDOpen (int drive) 112 SDL_CDOpen(int drive)
113 { 113 {
114 struct SDL_CD *cdrom; 114 struct SDL_CD *cdrom;
115 115
116 if (!CheckInit (0, NULL)) { 116 if (!CheckInit(0, NULL)) {
117 return (NULL); 117 return (NULL);
118 } 118 }
119 if (drive >= SDL_numcds) { 119 if (drive >= SDL_numcds) {
120 SDL_SetError ("Invalid CD-ROM drive index"); 120 SDL_SetError("Invalid CD-ROM drive index");
121 return (NULL); 121 return (NULL);
122 } 122 }
123 cdrom = (SDL_CD *) SDL_malloc (sizeof (*cdrom)); 123 cdrom = (SDL_CD *) SDL_malloc(sizeof(*cdrom));
124 if (cdrom == NULL) { 124 if (cdrom == NULL) {
125 SDL_OutOfMemory (); 125 SDL_OutOfMemory();
126 return (NULL); 126 return (NULL);
127 } 127 }
128 SDL_memset (cdrom, 0, sizeof (*cdrom)); 128 SDL_memset(cdrom, 0, sizeof(*cdrom));
129 cdrom->id = SDL_CDcaps.Open (drive); 129 cdrom->id = SDL_CDcaps.Open(drive);
130 if (cdrom->id < 0) { 130 if (cdrom->id < 0) {
131 SDL_free (cdrom); 131 SDL_free(cdrom);
132 return (NULL); 132 return (NULL);
133 } 133 }
134 default_cdrom = cdrom; 134 default_cdrom = cdrom;
135 return (cdrom); 135 return (cdrom);
136 } 136 }
137 137
138 CDstatus 138 CDstatus
139 SDL_CDStatus (SDL_CD * cdrom) 139 SDL_CDStatus(SDL_CD * cdrom)
140 { 140 {
141 CDstatus status; 141 CDstatus status;
142 int i; 142 int i;
143 Uint32 position; 143 Uint32 position;
144 144
145 /* Check if the CD-ROM subsystem has been initialized */ 145 /* Check if the CD-ROM subsystem has been initialized */
146 if (!CheckInit (1, &cdrom)) { 146 if (!CheckInit(1, &cdrom)) {
147 return (CD_ERROR); 147 return (CD_ERROR);
148 } 148 }
149 149
150 /* Get the current status of the drive */ 150 /* Get the current status of the drive */
151 cdrom->numtracks = 0; 151 cdrom->numtracks = 0;
152 cdrom->cur_track = 0; 152 cdrom->cur_track = 0;
153 cdrom->cur_frame = 0; 153 cdrom->cur_frame = 0;
154 status = SDL_CDcaps.Status (cdrom, &i); 154 status = SDL_CDcaps.Status(cdrom, &i);
155 position = (Uint32) i; 155 position = (Uint32) i;
156 cdrom->status = status; 156 cdrom->status = status;
157 157
158 /* Get the table of contents, if there's a CD available */ 158 /* Get the table of contents, if there's a CD available */
159 if (CD_INDRIVE (status)) { 159 if (CD_INDRIVE(status)) {
160 if (SDL_CDcaps.GetTOC (cdrom) < 0) { 160 if (SDL_CDcaps.GetTOC(cdrom) < 0) {
161 status = CD_ERROR; 161 status = CD_ERROR;
162 } 162 }
163 /* If the drive is playing, get current play position */ 163 /* If the drive is playing, get current play position */
164 if ((status == CD_PLAYING) || (status == CD_PAUSED)) { 164 if ((status == CD_PLAYING) || (status == CD_PAUSED)) {
165 for (i = 1; cdrom->track[i].offset <= position; ++i) { 165 for (i = 1; cdrom->track[i].offset <= position; ++i) {
166 /* Keep looking */ ; 166 /* Keep looking */ ;
167 } 167 }
168 #ifdef DEBUG_CDROM 168 #ifdef DEBUG_CDROM
169 fprintf (stderr, 169 fprintf(stderr,
170 "Current position: %d, track = %d (offset is %d)\n", 170 "Current position: %d, track = %d (offset is %d)\n",
171 position, i - 1, cdrom->track[i - 1].offset); 171 position, i - 1, cdrom->track[i - 1].offset);
172 #endif 172 #endif
173 cdrom->cur_track = i - 1; 173 cdrom->cur_track = i - 1;
174 position -= cdrom->track[cdrom->cur_track].offset; 174 position -= cdrom->track[cdrom->cur_track].offset;
175 cdrom->cur_frame = position; 175 cdrom->cur_frame = position;
176 } 176 }
177 } 177 }
178 return (status); 178 return (status);
179 } 179 }
180 180
181 int 181 int
182 SDL_CDPlayTracks (SDL_CD * cdrom, 182 SDL_CDPlayTracks(SDL_CD * cdrom,
183 int strack, int sframe, int ntracks, int nframes) 183 int strack, int sframe, int ntracks, int nframes)
184 { 184 {
185 int etrack, eframe; 185 int etrack, eframe;
186 int start, length; 186 int start, length;
187 187
188 /* Check if the CD-ROM subsystem has been initialized */ 188 /* Check if the CD-ROM subsystem has been initialized */
189 if (!CheckInit (1, &cdrom)) { 189 if (!CheckInit(1, &cdrom)) {
190 return (CD_ERROR); 190 return (CD_ERROR);
191 } 191 }
192 192
193 /* Determine the starting and ending tracks */ 193 /* Determine the starting and ending tracks */
194 if ((strack < 0) || (strack >= cdrom->numtracks)) { 194 if ((strack < 0) || (strack >= cdrom->numtracks)) {
195 SDL_SetError ("Invalid starting track"); 195 SDL_SetError("Invalid starting track");
196 return (CD_ERROR); 196 return (CD_ERROR);
197 } 197 }
198 if (!ntracks && !nframes) { 198 if (!ntracks && !nframes) {
199 etrack = cdrom->numtracks; 199 etrack = cdrom->numtracks;
200 eframe = 0; 200 eframe = 0;
205 } else { 205 } else {
206 eframe = nframes; 206 eframe = nframes;
207 } 207 }
208 } 208 }
209 if (etrack > cdrom->numtracks) { 209 if (etrack > cdrom->numtracks) {
210 SDL_SetError ("Invalid play length"); 210 SDL_SetError("Invalid play length");
211 return (CD_ERROR); 211 return (CD_ERROR);
212 } 212 }
213 213
214 /* Skip data tracks and verify frame offsets */ 214 /* Skip data tracks and verify frame offsets */
215 while ((strack <= etrack) && 215 while ((strack <= etrack) &&
216 (cdrom->track[strack].type == SDL_DATA_TRACK)) { 216 (cdrom->track[strack].type == SDL_DATA_TRACK)) {
217 ++strack; 217 ++strack;
218 } 218 }
219 if (sframe >= (int) cdrom->track[strack].length) { 219 if (sframe >= (int) cdrom->track[strack].length) {
220 SDL_SetError ("Invalid starting frame for track %d", strack); 220 SDL_SetError("Invalid starting frame for track %d", strack);
221 return (CD_ERROR); 221 return (CD_ERROR);
222 } 222 }
223 while ((etrack > strack) && 223 while ((etrack > strack) &&
224 (cdrom->track[etrack - 1].type == SDL_DATA_TRACK)) { 224 (cdrom->track[etrack - 1].type == SDL_DATA_TRACK)) {
225 --etrack; 225 --etrack;
226 } 226 }
227 if (eframe > (int) cdrom->track[etrack].length) { 227 if (eframe > (int) cdrom->track[etrack].length) {
228 SDL_SetError ("Invalid ending frame for track %d", etrack); 228 SDL_SetError("Invalid ending frame for track %d", etrack);
229 return (CD_ERROR); 229 return (CD_ERROR);
230 } 230 }
231 231
232 /* Determine start frame and play length */ 232 /* Determine start frame and play length */
233 start = (cdrom->track[strack].offset + sframe); 233 start = (cdrom->track[strack].offset + sframe);
240 return (0); 240 return (0);
241 } 241 }
242 242
243 /* Play! */ 243 /* Play! */
244 #ifdef DEBUG_CDROM 244 #ifdef DEBUG_CDROM
245 fprintf (stderr, "Playing %d frames at offset %d\n", length, start); 245 fprintf(stderr, "Playing %d frames at offset %d\n", length, start);
246 #endif 246 #endif
247 return (SDL_CDcaps.Play (cdrom, start, length)); 247 return (SDL_CDcaps.Play(cdrom, start, length));
248 } 248 }
249 249
250 int 250 int
251 SDL_CDPlay (SDL_CD * cdrom, int sframe, int length) 251 SDL_CDPlay(SDL_CD * cdrom, int sframe, int length)
252 { 252 {
253 /* Check if the CD-ROM subsystem has been initialized */ 253 /* Check if the CD-ROM subsystem has been initialized */
254 if (!CheckInit (1, &cdrom)) { 254 if (!CheckInit(1, &cdrom)) {
255 return (CD_ERROR); 255 return (CD_ERROR);
256 } 256 }
257 257
258 return (SDL_CDcaps.Play (cdrom, sframe, length)); 258 return (SDL_CDcaps.Play(cdrom, sframe, length));
259 } 259 }
260 260
261 int 261 int
262 SDL_CDPause (SDL_CD * cdrom) 262 SDL_CDPause(SDL_CD * cdrom)
263 { 263 {
264 CDstatus status; 264 CDstatus status;
265 int retval; 265 int retval;
266 266
267 /* Check if the CD-ROM subsystem has been initialized */ 267 /* Check if the CD-ROM subsystem has been initialized */
268 if (!CheckInit (1, &cdrom)) { 268 if (!CheckInit(1, &cdrom)) {
269 return (CD_ERROR); 269 return (CD_ERROR);
270 } 270 }
271 271
272 status = SDL_CDcaps.Status (cdrom, NULL); 272 status = SDL_CDcaps.Status(cdrom, NULL);
273 switch (status) { 273 switch (status) {
274 case CD_PLAYING: 274 case CD_PLAYING:
275 retval = SDL_CDcaps.Pause (cdrom); 275 retval = SDL_CDcaps.Pause(cdrom);
276 break; 276 break;
277 default: 277 default:
278 retval = 0; 278 retval = 0;
279 break; 279 break;
280 } 280 }
281 return (retval); 281 return (retval);
282 } 282 }
283 283
284 int 284 int
285 SDL_CDResume (SDL_CD * cdrom) 285 SDL_CDResume(SDL_CD * cdrom)
286 { 286 {
287 CDstatus status; 287 CDstatus status;
288 int retval; 288 int retval;
289 289
290 /* Check if the CD-ROM subsystem has been initialized */ 290 /* Check if the CD-ROM subsystem has been initialized */
291 if (!CheckInit (1, &cdrom)) { 291 if (!CheckInit(1, &cdrom)) {
292 return (CD_ERROR); 292 return (CD_ERROR);
293 } 293 }
294 294
295 status = SDL_CDcaps.Status (cdrom, NULL); 295 status = SDL_CDcaps.Status(cdrom, NULL);
296 switch (status) { 296 switch (status) {
297 case CD_PAUSED: 297 case CD_PAUSED:
298 retval = SDL_CDcaps.Resume (cdrom); 298 retval = SDL_CDcaps.Resume(cdrom);
299 default: 299 default:
300 retval = 0; 300 retval = 0;
301 break; 301 break;
302 } 302 }
303 return (retval); 303 return (retval);
304 } 304 }
305 305
306 int 306 int
307 SDL_CDStop (SDL_CD * cdrom) 307 SDL_CDStop(SDL_CD * cdrom)
308 { 308 {
309 CDstatus status; 309 CDstatus status;
310 int retval; 310 int retval;
311 311
312 /* Check if the CD-ROM subsystem has been initialized */ 312 /* Check if the CD-ROM subsystem has been initialized */
313 if (!CheckInit (1, &cdrom)) { 313 if (!CheckInit(1, &cdrom)) {
314 return (CD_ERROR); 314 return (CD_ERROR);
315 } 315 }
316 316
317 status = SDL_CDcaps.Status (cdrom, NULL); 317 status = SDL_CDcaps.Status(cdrom, NULL);
318 switch (status) { 318 switch (status) {
319 case CD_PLAYING: 319 case CD_PLAYING:
320 case CD_PAUSED: 320 case CD_PAUSED:
321 retval = SDL_CDcaps.Stop (cdrom); 321 retval = SDL_CDcaps.Stop(cdrom);
322 default: 322 default:
323 retval = 0; 323 retval = 0;
324 break; 324 break;
325 } 325 }
326 return (retval); 326 return (retval);
327 } 327 }
328 328
329 int 329 int
330 SDL_CDEject (SDL_CD * cdrom) 330 SDL_CDEject(SDL_CD * cdrom)
331 { 331 {
332 /* Check if the CD-ROM subsystem has been initialized */ 332 /* Check if the CD-ROM subsystem has been initialized */
333 if (!CheckInit (1, &cdrom)) { 333 if (!CheckInit(1, &cdrom)) {
334 return (CD_ERROR); 334 return (CD_ERROR);
335 } 335 }
336 return (SDL_CDcaps.Eject (cdrom)); 336 return (SDL_CDcaps.Eject(cdrom));
337 } 337 }
338 338
339 void 339 void
340 SDL_CDClose (SDL_CD * cdrom) 340 SDL_CDClose(SDL_CD * cdrom)
341 { 341 {
342 /* Check if the CD-ROM subsystem has been initialized */ 342 /* Check if the CD-ROM subsystem has been initialized */
343 if (!CheckInit (1, &cdrom)) { 343 if (!CheckInit(1, &cdrom)) {
344 return; 344 return;
345 } 345 }
346 SDL_CDcaps.Close (cdrom); 346 SDL_CDcaps.Close(cdrom);
347 SDL_free (cdrom); 347 SDL_free(cdrom);
348 default_cdrom = NULL; 348 default_cdrom = NULL;
349 } 349 }
350 350
351 void 351 void
352 SDL_CDROMQuit (void) 352 SDL_CDROMQuit(void)
353 { 353 {
354 SDL_SYS_CDQuit (); 354 SDL_SYS_CDQuit();
355 SDL_cdinitted = 0; 355 SDL_cdinitted = 0;
356 } 356 }
357 357
358 /* vi: set ts=4 sw=4 expandtab: */ 358 /* vi: set ts=4 sw=4 expandtab: */