Mercurial > sdl-ios-xcode
annotate src/cdrom/aix/SDL_syscdrom.c @ 1379:c0a74f199ecf
Use only safe string functions
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 19 Feb 2006 23:46:34 +0000 |
parents | 19418e4422cb |
children | d910939febfa |
rev | line source |
---|---|
0 | 1 /* |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
2 SDL - Simple DirectMedia Layer |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
0
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:
0
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:
0
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:
0
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:
0
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:
0
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:
0
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Carsten Griwodz | |
20 griff@kom.tu-darmstadt.de | |
21 | |
22 based on linux/SDL_syscdrom.c by Sam Lantinga | |
23 */ | |
24 | |
25 /* Functions for system-level CD-ROM audio control */ | |
26 | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
27 /*#define DEBUG_CDROM 1*/ |
0 | 28 |
29 #include <sys/types.h> | |
30 #include <sys/stat.h> | |
31 #include <fcntl.h> | |
32 #include <errno.h> | |
33 #include <unistd.h> | |
34 | |
35 #include <sys/ioctl.h> | |
36 #include <sys/devinfo.h> | |
37 #include <sys/mntctl.h> | |
38 #include <sys/statfs.h> | |
39 #include <sys/vmount.h> | |
40 #include <fstab.h> | |
41 #include <sys/scdisk.h> | |
42 | |
43 #include "SDL_cdrom.h" | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
44 #include "../SDL_syscdrom.h" |
0 | 45 |
46 /* The maximum number of CD-ROM drives we'll detect */ | |
47 #define MAX_DRIVES 16 | |
48 | |
49 /* A list of available CD-ROM drives */ | |
50 static char *SDL_cdlist[MAX_DRIVES]; | |
51 static dev_t SDL_cdmode[MAX_DRIVES]; | |
52 | |
53 /* The system-dependent CD control functions */ | |
54 static const char *SDL_SYS_CDName(int drive); | |
55 static int SDL_SYS_CDOpen(int drive); | |
56 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom); | |
57 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position); | |
58 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length); | |
59 static int SDL_SYS_CDPause(SDL_CD *cdrom); | |
60 static int SDL_SYS_CDResume(SDL_CD *cdrom); | |
61 static int SDL_SYS_CDStop(SDL_CD *cdrom); | |
62 static int SDL_SYS_CDEject(SDL_CD *cdrom); | |
63 static void SDL_SYS_CDClose(SDL_CD *cdrom); | |
64 static int SDL_SYS_CDioctl(int id, int command, void *arg); | |
65 | |
66 /* Check a drive to see if it is a CD-ROM */ | |
67 static int CheckDrive(char *drive, struct stat *stbuf) | |
68 { | |
69 int is_cd; | |
70 int cdfd; | |
71 int ret; | |
72 struct devinfo info; | |
73 | |
74 /* If it doesn't exist, return -1 */ | |
75 if ( stat(drive, stbuf) < 0 ) { | |
76 return -1; | |
77 } | |
78 | |
79 /* If it does exist, verify that it's an available CD-ROM */ | |
80 is_cd = 0; | |
81 if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) { | |
82 cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0); | |
83 if ( cdfd >= 0 ) { | |
84 ret = SDL_SYS_CDioctl( cdfd, IOCINFO, &info ); | |
85 if ( ret < 0 ) { | |
86 /* Some kind of error */ | |
87 is_cd = 0; | |
88 } else { | |
89 if ( info.devtype == DD_CDROM ) { | |
90 is_cd = 1; | |
91 } else { | |
92 is_cd = 0; | |
93 } | |
94 } | |
95 close(cdfd); | |
96 } | |
97 #ifdef DEBUG_CDROM | |
98 else | |
99 { | |
100 fprintf(stderr, "Could not open drive %s (%s)\n", drive, strerror(errno)); | |
101 } | |
102 #endif | |
103 } | |
104 return is_cd; | |
105 } | |
106 | |
107 /* Add a CD-ROM drive to our list of valid drives */ | |
108 static void AddDrive(char *drive, struct stat *stbuf) | |
109 { | |
110 int i; | |
111 | |
112 if ( SDL_numcds < MAX_DRIVES ) { | |
113 /* Check to make sure it's not already in our list. | |
114 This can happen when we see a drive via symbolic link. | |
115 */ | |
116 for ( i=0; i<SDL_numcds; ++i ) { | |
117 if ( stbuf->st_rdev == SDL_cdmode[i] ) { | |
118 #ifdef DEBUG_CDROM | |
119 fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]); | |
120 #endif | |
121 return; | |
122 } | |
123 } | |
124 | |
125 /* Add this drive to our list */ | |
126 i = SDL_numcds; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
127 SDL_cdlist[i] = SDL_strdup(drive); |
0 | 128 if ( SDL_cdlist[i] == NULL ) { |
129 SDL_OutOfMemory(); | |
130 return; | |
131 } | |
132 SDL_cdmode[i] = stbuf->st_rdev; | |
133 ++SDL_numcds; | |
134 #ifdef DEBUG_CDROM | |
135 fprintf(stderr, "Added CD-ROM drive: %s\n", drive); | |
136 #endif | |
137 } | |
138 } | |
139 | |
140 static void CheckMounts() | |
141 { | |
142 char* buffer; | |
143 int bufsz; | |
144 struct vmount* ptr; | |
145 int ret; | |
146 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
147 buffer = (char*)SDL_malloc(10); |
0 | 148 bufsz = 10; |
149 if ( buffer==NULL ) | |
150 { | |
151 fprintf(stderr, "Could not allocate 10 bytes in aix/SDL_syscdrom.c:CheckMounts\n" ); | |
152 exit ( -10 ); | |
153 } | |
154 | |
155 do | |
156 { | |
157 /* mntctrl() returns an array of all mounted filesystems */ | |
158 ret = mntctl ( MCTL_QUERY, bufsz, buffer ); | |
159 if ( ret == 0 ) | |
160 { | |
161 /* Buffer was too small, realloc. */ | |
162 bufsz = *(int*)buffer; /* Required size is in first word. */ | |
163 /* (whatever a word is in AIX 4.3.3) */ | |
164 /* int seems to be OK in 32bit mode. */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
165 SDL_free(buffer); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
166 buffer = (char*)SDL_malloc(bufsz); |
0 | 167 if ( buffer==NULL ) |
168 { | |
169 fprintf(stderr, | |
170 "Could not allocate %d bytes in aix/SDL_syscdrom.c:CheckMounts\n", | |
171 bufsz ); | |
172 exit ( -10 ); | |
173 } | |
174 } | |
175 else if ( ret < 0 ) | |
176 { | |
177 #ifdef DEBUG_CDROM | |
178 fprintf(stderr, "Error reading vmount structures\n"); | |
179 #endif | |
180 return; | |
181 } | |
182 } | |
183 while ( ret == 0 ); | |
184 | |
185 #ifdef DEBUG_CDROM | |
186 fprintf ( stderr, "Read %d vmount structures\n",ret ); | |
187 #endif | |
188 ptr = (struct vmount*)buffer; | |
189 do | |
190 { | |
191 switch(ptr->vmt_gfstype) | |
192 { | |
193 case MNT_CDROM : | |
194 { | |
195 struct stat stbuf; | |
196 char* text; | |
197 | |
198 text = (char*)ptr + ptr->vmt_data[VMT_OBJECT].vmt_off; | |
199 #ifdef DEBUG_CDROM | |
200 fprintf(stderr, "Checking mount path: %s mounted on %s\n", | |
201 text, (char*)ptr + ptr->vmt_data[VMT_STUB].vmt_off ); | |
202 #endif | |
203 if ( CheckDrive( text, &stbuf) > 0) | |
204 { | |
205 AddDrive( text, &stbuf); | |
206 } | |
207 } | |
208 break; | |
209 default : | |
210 break; | |
211 } | |
212 ptr = (struct vmount*)((char*)ptr + ptr->vmt_length); | |
213 ret--; | |
214 } | |
215 while ( ret > 0 ); | |
216 | |
217 free ( buffer ); | |
218 } | |
219 | |
220 static int CheckNonmounts() | |
221 { | |
222 #ifdef _THREAD_SAFE | |
223 AFILE_t fsFile = NULL; | |
224 int passNo = 0; | |
225 int ret; | |
226 struct fstab entry; | |
227 struct stat stbuf; | |
228 | |
229 ret = setfsent_r( &fsFile, &passNo ); | |
230 if ( ret != 0 ) return -1; | |
231 do | |
232 { | |
233 ret = getfsent_r ( &entry, &fsFile, &passNo ); | |
234 if ( ret == 0 ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
235 char* l = SDL_strrchr(entry.fs_spec,'/'); |
0 | 236 if ( l != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
237 if ( !SDL_strncmp("cd",++l,2) ) { |
0 | 238 #ifdef DEBUG_CDROM |
239 fprintf(stderr, | |
240 "Found unmounted CD ROM drive with device name %s\n", | |
241 entry.fs_spec); | |
242 #endif | |
243 if ( CheckDrive( entry.fs_spec, &stbuf) > 0) | |
244 { | |
245 AddDrive( entry.fs_spec, &stbuf); | |
246 } | |
247 } | |
248 } | |
249 } | |
250 } | |
251 while ( ret == 0 ); | |
252 ret = endfsent_r ( &fsFile ); | |
253 if ( ret != 0 ) return -1; | |
254 return 0; | |
255 #else | |
256 struct fstab* entry; | |
257 struct stat stbuf; | |
258 | |
259 setfsent(); | |
260 do | |
261 { | |
262 entry = getfsent(); | |
263 if ( entry != NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
264 char* l = SDL_strrchr(entry->fs_spec,'/'); |
0 | 265 if ( l != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
266 if ( !SDL_strncmp("cd",++l,2) ) { |
0 | 267 #ifdef DEBUG_CDROM |
268 fprintf(stderr,"Found unmounted CD ROM drive with device name %s", entry->fs_spec); | |
269 #endif | |
270 if ( CheckDrive( entry->fs_spec, &stbuf) > 0) | |
271 { | |
272 AddDrive( entry->fs_spec, &stbuf); | |
273 } | |
274 } | |
275 } | |
276 } | |
277 } | |
278 while ( entry != NULL ); | |
279 endfsent(); | |
280 #endif | |
281 } | |
282 | |
283 int SDL_SYS_CDInit(void) | |
284 { | |
285 char *SDLcdrom; | |
286 struct stat stbuf; | |
287 | |
288 /* Fill in our driver capabilities */ | |
289 SDL_CDcaps.Name = SDL_SYS_CDName; | |
290 SDL_CDcaps.Open = SDL_SYS_CDOpen; | |
291 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC; | |
292 SDL_CDcaps.Status = SDL_SYS_CDStatus; | |
293 SDL_CDcaps.Play = SDL_SYS_CDPlay; | |
294 SDL_CDcaps.Pause = SDL_SYS_CDPause; | |
295 SDL_CDcaps.Resume = SDL_SYS_CDResume; | |
296 SDL_CDcaps.Stop = SDL_SYS_CDStop; | |
297 SDL_CDcaps.Eject = SDL_SYS_CDEject; | |
298 SDL_CDcaps.Close = SDL_SYS_CDClose; | |
299 | |
300 /* Look in the environment for our CD-ROM drive list */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
301 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */ |
0 | 302 if ( SDLcdrom != NULL ) { |
303 char *cdpath, *delim; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
304 size_t len = SDL_strlen(SDLcdrom)+1; |
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
305 cdpath = SDL_stack_alloc(char, len); |
0 | 306 if ( cdpath != NULL ) { |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
307 SDL_strlcpy(cdpath, SDLcdrom, len); |
0 | 308 SDLcdrom = cdpath; |
309 do { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
310 delim = SDL_strchr(SDLcdrom, ':'); |
0 | 311 if ( delim ) { |
312 *delim++ = '\0'; | |
313 } | |
314 #ifdef DEBUG_CDROM | |
315 fprintf(stderr, "Checking CD-ROM drive from SDL_CDROM: %s\n", SDLcdrom); | |
316 #endif | |
317 if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) { | |
318 AddDrive(SDLcdrom, &stbuf); | |
319 } | |
320 if ( delim ) { | |
321 SDLcdrom = delim; | |
322 } else { | |
323 SDLcdrom = NULL; | |
324 } | |
325 } while ( SDLcdrom ); | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
326 SDL_stack_free(cdpath); |
0 | 327 } |
328 | |
329 /* If we found our drives, there's nothing left to do */ | |
330 if ( SDL_numcds > 0 ) { | |
331 return(0); | |
332 } | |
333 } | |
334 | |
335 CheckMounts(); | |
336 CheckNonmounts(); | |
337 | |
338 return 0; | |
339 } | |
340 | |
341 /* General ioctl() CD-ROM command function */ | |
342 static int SDL_SYS_CDioctl(int id, int command, void *arg) | |
343 { | |
344 int retval; | |
345 | |
346 retval = ioctl(id, command, arg); | |
347 if ( retval < 0 ) { | |
348 SDL_SetError("ioctl() error: %s", strerror(errno)); | |
349 } | |
350 return retval; | |
351 } | |
352 | |
353 static const char *SDL_SYS_CDName(int drive) | |
354 { | |
355 return(SDL_cdlist[drive]); | |
356 } | |
357 | |
358 static int SDL_SYS_CDOpen(int drive) | |
359 { | |
360 int fd; | |
361 char* lastsl; | |
362 char* cdromname; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
363 size_t len; |
0 | 364 |
365 /* | |
366 * We found /dev/cd? drives and that is in our list. But we can | |
367 * open only the /dev/rcd? versions of those devices for Audio CD. | |
368 */ | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
369 len = SDL_strlen(SDL_cdlist[drive])+2; |
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
370 cdromname = (char*)SDL_malloc(len); |
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
371 SDL_strlcpy(cdromname,SDL_cdlist[drive],len); |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
372 lastsl = SDL_strrchr(cdromname,'/'); |
0 | 373 if (lastsl) { |
374 *lastsl = 0; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
375 SDL_strlcat(cdromname,"/r",len); |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
376 lastsl = SDL_strrchr(SDL_cdlist[drive],'/'); |
0 | 377 if (lastsl) { |
378 lastsl++; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
379 SDL_strlcat(cdromname,lastsl,len); |
0 | 380 } |
381 } | |
382 | |
383 #ifdef DEBUG_CDROM | |
384 fprintf(stderr, "Should open drive %s, opening %s\n", SDL_cdlist[drive], cdromname); | |
385 #endif | |
386 | |
387 /* | |
388 * Use exclusive access. Don't use SC_DIAGNOSTICS as xmcd does because they | |
389 * require root priviledges, and we don't want that. SC_SINGLE provides | |
390 * exclusive access with less trouble. | |
391 */ | |
392 fd = openx(cdromname, O_RDONLY, NULL, SC_SINGLE); | |
393 if ( fd < 0 ) | |
394 { | |
395 #ifdef DEBUG_CDROM | |
396 fprintf(stderr, "Could not open drive %s (%s)\n", cdromname, strerror(errno)); | |
397 #endif | |
398 } | |
399 else | |
400 { | |
401 struct mode_form_op cdMode; | |
402 int ret; | |
403 #ifdef DEBUG_CDROM | |
404 cdMode.action = CD_GET_MODE; | |
405 ret = SDL_SYS_CDioctl(fd, DK_CD_MODE, &cdMode); | |
406 if ( ret < 0 ) { | |
407 fprintf(stderr, | |
408 "Could not get drive mode for %s (%s)\n", | |
409 cdromname, strerror(errno)); | |
410 } else { | |
411 switch(cdMode.cd_mode_form) { | |
412 case CD_MODE1 : | |
413 fprintf(stderr, | |
414 "Drive mode for %s is %s\n", | |
415 cdromname, "CD-ROM Data Mode 1"); | |
416 break; | |
417 case CD_MODE2_FORM1 : | |
418 fprintf(stderr, | |
419 "Drive mode for %s is %s\n", | |
420 cdromname, "CD-ROM XA Data Mode 2 Form 1"); | |
421 break; | |
422 case CD_MODE2_FORM2 : | |
423 fprintf(stderr, | |
424 "Drive mode for %s is %s\n", | |
425 cdromname, "CD-ROM XA Data Mode 2 Form 2"); | |
426 break; | |
427 case CD_DA : | |
428 fprintf(stderr, | |
429 "Drive mode for %s is %s\n", | |
430 cdromname, "CD-DA"); | |
431 break; | |
432 default : | |
433 fprintf(stderr, | |
434 "Drive mode for %s is %s\n", | |
435 cdromname, "unknown"); | |
436 break; | |
437 } | |
438 } | |
439 #endif | |
440 | |
441 cdMode.action = CD_CHG_MODE; | |
442 cdMode.cd_mode_form = CD_DA; | |
443 ret = SDL_SYS_CDioctl(fd, DK_CD_MODE, &cdMode); | |
444 if ( ret < 0 ) { | |
445 #ifdef DEBUG_CDROM | |
446 fprintf(stderr, | |
447 "Could not set drive mode for %s (%s)\n", | |
448 cdromname, strerror(errno)); | |
449 #endif | |
450 SDL_SetError("ioctl() error: Could not set CD drive mode, %s", | |
451 strerror(errno)); | |
452 } else { | |
453 #ifdef DEBUG_CDROM | |
454 fprintf(stderr, | |
455 "Drive mode for %s set to CD_DA\n", | |
456 cdromname); | |
457 #endif | |
458 } | |
459 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
460 SDL_free(cdromname); |
0 | 461 return fd; |
462 } | |
463 | |
464 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom) | |
465 { | |
466 struct cd_audio_cmd cmd; | |
467 struct cd_audio_cmd entry; | |
468 int i; | |
469 int okay; | |
470 | |
471 cmd.audio_cmds = CD_TRK_INFO_AUDIO; | |
472 cmd.msf_flag = FALSE; | |
473 if ( SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd) < 0 ) { | |
474 return -1; | |
475 } | |
476 | |
477 okay = 0; | |
478 cdrom->numtracks = cmd.indexing.track_index.last_track | |
479 - cmd.indexing.track_index.first_track+1; | |
480 if ( cdrom->numtracks > SDL_MAX_TRACKS ) { | |
481 cdrom->numtracks = SDL_MAX_TRACKS; | |
482 } | |
483 | |
484 /* Read all the track TOC entries */ | |
485 for ( i=0; i<=cdrom->numtracks; ++i ) { | |
486 if ( i == cdrom->numtracks ) { | |
487 cdrom->track[i].id = 0xAA;; | |
488 } else { | |
489 cdrom->track[i].id = cmd.indexing.track_index.first_track+i; | |
490 } | |
491 entry.audio_cmds = CD_GET_TRK_MSF; | |
492 entry.indexing.track_msf.track = cdrom->track[i].id; | |
493 if ( SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &entry) < 0 ) { | |
494 break; | |
495 } else { | |
496 cdrom->track[i].type = 0; /* don't know how to detect 0x04 data track */ | |
497 cdrom->track[i].offset = MSF_TO_FRAMES( | |
498 entry.indexing.track_msf.mins, | |
499 entry.indexing.track_msf.secs, | |
500 entry.indexing.track_msf.frames); | |
501 cdrom->track[i].length = 0; | |
502 if ( i > 0 ) { | |
503 cdrom->track[i-1].length = cdrom->track[i].offset | |
504 - cdrom->track[i-1].offset; | |
505 } | |
506 } | |
507 } | |
508 if ( i == (cdrom->numtracks+1) ) { | |
509 okay = 1; | |
510 } | |
511 return(okay ? 0 : -1); | |
512 } | |
513 | |
514 /* Get CD-ROM status */ | |
515 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position) | |
516 { | |
517 CDstatus status; | |
518 struct cd_audio_cmd cmd; | |
519 cmd.audio_cmds = CD_INFO_AUDIO; | |
520 | |
521 if ( SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd) < 0 ) { | |
522 #ifdef DEBUG_CDROM | |
523 fprintf(stderr, "ioctl failed in SDL_SYS_CDStatus (%s)\n", SDL_GetError()); | |
524 #endif | |
525 status = CD_ERROR; | |
526 } else { | |
527 switch (cmd.status) { | |
528 case CD_NO_AUDIO: | |
529 case CD_COMPLETED: | |
530 status = CD_STOPPED; | |
531 break; | |
532 case CD_PLAY_AUDIO: | |
533 status = CD_PLAYING; | |
534 break; | |
535 case CD_PAUSE_AUDIO: | |
536 status = CD_PAUSED; | |
537 break; | |
538 case CD_NOT_VALID: | |
539 #ifdef DEBUG_CDROM | |
540 fprintf(stderr, "cdStatus failed with CD_NOT_VALID\n"); | |
541 #endif | |
542 status = CD_ERROR; | |
543 break; | |
544 case CD_STATUS_ERROR: | |
545 #ifdef DEBUG_CDROM | |
546 fprintf(stderr, "cdStatus failed with CD_STATUS_ERROR\n"); | |
547 #endif | |
548 status = CD_ERROR; | |
549 break; | |
550 default: | |
551 #ifdef DEBUG_CDROM | |
552 fprintf(stderr, "cdStatus failed with unknown error\n"); | |
553 #endif | |
554 status = CD_ERROR; | |
555 break; | |
556 } | |
557 } | |
558 if ( position ) { | |
559 if ( status == CD_PLAYING || (status == CD_PAUSED) ) { | |
560 *position = MSF_TO_FRAMES( cmd.indexing.info_audio.current_mins, | |
561 cmd.indexing.info_audio.current_secs, | |
562 cmd.indexing.info_audio.current_frames); | |
563 } else { | |
564 *position = 0; | |
565 } | |
566 } | |
567 return status; | |
568 } | |
569 | |
570 /* Start play */ | |
571 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length) | |
572 { | |
573 struct cd_audio_cmd cmd; | |
574 | |
575 /* | |
576 * My CD Rom is muted by default. I think I read that this is new with | |
577 * AIX 4.3. SDL does not change the volume, so I need a kludge. Maybe | |
578 * its better to do this elsewhere? | |
579 */ | |
580 cmd.audio_cmds = CD_PLAY_AUDIO | CD_SET_VOLUME; | |
581 cmd.msf_flag = TRUE; | |
582 FRAMES_TO_MSF(start, | |
583 &cmd.indexing.msf.first_mins, | |
584 &cmd.indexing.msf.first_secs, | |
585 &cmd.indexing.msf.first_frames); | |
586 FRAMES_TO_MSF(start+length, | |
587 &cmd.indexing.msf.last_mins, | |
588 &cmd.indexing.msf.last_secs, | |
589 &cmd.indexing.msf.last_frames); | |
590 cmd.volume_type = CD_VOLUME_ALL; | |
591 cmd.all_channel_vol = 255; /* This is a uchar. What is a good value? No docu! */ | |
592 cmd.out_port_0_sel = CD_AUDIO_CHNL_0; | |
593 cmd.out_port_1_sel = CD_AUDIO_CHNL_1; | |
594 cmd.out_port_2_sel = CD_AUDIO_CHNL_2; | |
595 cmd.out_port_3_sel = CD_AUDIO_CHNL_3; | |
596 | |
597 #ifdef DEBUG_CDROM | |
598 fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n", | |
599 cmd.indexing.msf.first_mins, | |
600 cmd.indexing.msf.first_secs, | |
601 cmd.indexing.msf.first_frames, | |
602 cmd.indexing.msf.last_mins, | |
603 cmd.indexing.msf.last_secs, | |
604 cmd.indexing.msf.last_frames); | |
605 #endif | |
606 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd)); | |
607 } | |
608 | |
609 /* Pause play */ | |
610 static int SDL_SYS_CDPause(SDL_CD *cdrom) | |
611 { | |
612 struct cd_audio_cmd cmd; | |
613 cmd.audio_cmds = CD_PAUSE_AUDIO; | |
614 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd)); | |
615 } | |
616 | |
617 /* Resume play */ | |
618 static int SDL_SYS_CDResume(SDL_CD *cdrom) | |
619 { | |
620 struct cd_audio_cmd cmd; | |
621 cmd.audio_cmds = CD_RESUME_AUDIO; | |
622 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd)); | |
623 } | |
624 | |
625 /* Stop play */ | |
626 static int SDL_SYS_CDStop(SDL_CD *cdrom) | |
627 { | |
628 struct cd_audio_cmd cmd; | |
629 cmd.audio_cmds = CD_STOP_AUDIO; | |
630 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd)); | |
631 } | |
632 | |
633 /* Eject the CD-ROM */ | |
634 static int SDL_SYS_CDEject(SDL_CD *cdrom) | |
635 { | |
636 return(SDL_SYS_CDioctl(cdrom->id, DKEJECT, 0)); | |
637 } | |
638 | |
639 /* Close the CD-ROM handle */ | |
640 static void SDL_SYS_CDClose(SDL_CD *cdrom) | |
641 { | |
642 close(cdrom->id); | |
643 } | |
644 | |
645 void SDL_SYS_CDQuit(void) | |
646 { | |
647 int i; | |
648 | |
649 if ( SDL_numcds > 0 ) { | |
650 for ( i=0; i<SDL_numcds; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
651 SDL_free(SDL_cdlist[i]); |
0 | 652 } |
653 SDL_numcds = 0; | |
654 } | |
655 } | |
656 |