comparison src/cdrom/aix/SDL_syscdrom.c @ 0:74212992fb08

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