2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 griff@kom.tu-darmstadt.de
22 based on linux/SDL_syscdrom.c by Sam Lantinga
24 #include "SDL_config.h"
28 /* Functions for system-level CD-ROM audio control */
30 /*#define DEBUG_CDROM 1*/
32 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/devinfo.h>
40 #include <sys/mntctl.h>
41 #include <sys/statfs.h>
42 #include <sys/vmount.h>
44 #include <sys/scdisk.h>
46 #include "SDL_cdrom.h"
47 #include "../SDL_syscdrom.h"
49 /* The maximum number of CD-ROM drives we'll detect */
52 /* A list of available CD-ROM drives */
53 static char *SDL_cdlist[MAX_DRIVES];
54 static dev_t SDL_cdmode[MAX_DRIVES];
56 /* The system-dependent CD control functions */
57 static const char *SDL_SYS_CDName(int drive);
58 static int SDL_SYS_CDOpen(int drive);
59 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
60 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
61 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
62 static int SDL_SYS_CDPause(SDL_CD *cdrom);
63 static int SDL_SYS_CDResume(SDL_CD *cdrom);
64 static int SDL_SYS_CDStop(SDL_CD *cdrom);
65 static int SDL_SYS_CDEject(SDL_CD *cdrom);
66 static void SDL_SYS_CDClose(SDL_CD *cdrom);
67 static int SDL_SYS_CDioctl(int id, int command, void *arg);
69 /* Check a drive to see if it is a CD-ROM */
70 static int CheckDrive(char *drive, struct stat *stbuf)
77 /* If it doesn't exist, return -1 */
78 if ( stat(drive, stbuf) < 0 ) {
82 /* If it does exist, verify that it's an available CD-ROM */
84 if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) {
85 cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
87 ret = SDL_SYS_CDioctl( cdfd, IOCINFO, &info );
89 /* Some kind of error */
92 if ( info.devtype == DD_CDROM ) {
103 fprintf(stderr, "Could not open drive %s (%s)\n", drive, strerror(errno));
110 /* Add a CD-ROM drive to our list of valid drives */
111 static void AddDrive(char *drive, struct stat *stbuf)
115 if ( SDL_numcds < MAX_DRIVES ) {
116 /* Check to make sure it's not already in our list.
117 This can happen when we see a drive via symbolic link.
119 for ( i=0; i<SDL_numcds; ++i ) {
120 if ( stbuf->st_rdev == SDL_cdmode[i] ) {
122 fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
128 /* Add this drive to our list */
130 SDL_cdlist[i] = SDL_strdup(drive);
131 if ( SDL_cdlist[i] == NULL ) {
135 SDL_cdmode[i] = stbuf->st_rdev;
138 fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
143 static void CheckMounts()
150 buffer = (char*)SDL_malloc(10);
154 fprintf(stderr, "Could not allocate 10 bytes in aix/SDL_syscdrom.c:CheckMounts\n" );
160 /* mntctrl() returns an array of all mounted filesystems */
161 ret = mntctl ( MCTL_QUERY, bufsz, buffer );
164 /* Buffer was too small, realloc. */
165 bufsz = *(int*)buffer; /* Required size is in first word. */
166 /* (whatever a word is in AIX 4.3.3) */
167 /* int seems to be OK in 32bit mode. */
169 buffer = (char*)SDL_malloc(bufsz);
173 "Could not allocate %d bytes in aix/SDL_syscdrom.c:CheckMounts\n",
181 fprintf(stderr, "Error reading vmount structures\n");
189 fprintf ( stderr, "Read %d vmount structures\n",ret );
191 ptr = (struct vmount*)buffer;
194 switch(ptr->vmt_gfstype)
201 text = (char*)ptr + ptr->vmt_data[VMT_OBJECT].vmt_off;
203 fprintf(stderr, "Checking mount path: %s mounted on %s\n",
204 text, (char*)ptr + ptr->vmt_data[VMT_STUB].vmt_off );
206 if ( CheckDrive( text, &stbuf) > 0)
208 AddDrive( text, &stbuf);
215 ptr = (struct vmount*)((char*)ptr + ptr->vmt_length);
223 static int CheckNonmounts()
226 AFILE_t fsFile = NULL;
232 ret = setfsent_r( &fsFile, &passNo );
233 if ( ret != 0 ) return -1;
236 ret = getfsent_r ( &entry, &fsFile, &passNo );
238 char* l = SDL_strrchr(entry.fs_spec,'/');
240 if ( !SDL_strncmp("cd",++l,2) ) {
243 "Found unmounted CD ROM drive with device name %s\n",
246 if ( CheckDrive( entry.fs_spec, &stbuf) > 0)
248 AddDrive( entry.fs_spec, &stbuf);
255 ret = endfsent_r ( &fsFile );
256 if ( ret != 0 ) return -1;
266 if ( entry != NULL ) {
267 char* l = SDL_strrchr(entry->fs_spec,'/');
269 if ( !SDL_strncmp("cd",++l,2) ) {
271 fprintf(stderr,"Found unmounted CD ROM drive with device name %s", entry->fs_spec);
273 if ( CheckDrive( entry->fs_spec, &stbuf) > 0)
275 AddDrive( entry->fs_spec, &stbuf);
281 while ( entry != NULL );
286 int SDL_SYS_CDInit(void)
291 /* Fill in our driver capabilities */
292 SDL_CDcaps.Name = SDL_SYS_CDName;
293 SDL_CDcaps.Open = SDL_SYS_CDOpen;
294 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
295 SDL_CDcaps.Status = SDL_SYS_CDStatus;
296 SDL_CDcaps.Play = SDL_SYS_CDPlay;
297 SDL_CDcaps.Pause = SDL_SYS_CDPause;
298 SDL_CDcaps.Resume = SDL_SYS_CDResume;
299 SDL_CDcaps.Stop = SDL_SYS_CDStop;
300 SDL_CDcaps.Eject = SDL_SYS_CDEject;
301 SDL_CDcaps.Close = SDL_SYS_CDClose;
303 /* Look in the environment for our CD-ROM drive list */
304 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */
305 if ( SDLcdrom != NULL ) {
306 char *cdpath, *delim;
307 size_t len = SDL_strlen(SDLcdrom)+1;
308 cdpath = SDL_stack_alloc(char, len);
309 if ( cdpath != NULL ) {
310 SDL_strlcpy(cdpath, SDLcdrom, len);
313 delim = SDL_strchr(SDLcdrom, ':');
318 fprintf(stderr, "Checking CD-ROM drive from SDL_CDROM: %s\n", SDLcdrom);
320 if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
321 AddDrive(SDLcdrom, &stbuf);
328 } while ( SDLcdrom );
329 SDL_stack_free(cdpath);
332 /* If we found our drives, there's nothing left to do */
333 if ( SDL_numcds > 0 ) {
344 /* General ioctl() CD-ROM command function */
345 static int SDL_SYS_CDioctl(int id, int command, void *arg)
349 retval = ioctl(id, command, arg);
351 SDL_SetError("ioctl() error: %s", strerror(errno));
356 static const char *SDL_SYS_CDName(int drive)
358 return(SDL_cdlist[drive]);
361 static int SDL_SYS_CDOpen(int drive)
369 * We found /dev/cd? drives and that is in our list. But we can
370 * open only the /dev/rcd? versions of those devices for Audio CD.
372 len = SDL_strlen(SDL_cdlist[drive])+2;
373 cdromname = (char*)SDL_malloc(len);
374 SDL_strlcpy(cdromname,SDL_cdlist[drive],len);
375 lastsl = SDL_strrchr(cdromname,'/');
378 SDL_strlcat(cdromname,"/r",len);
379 lastsl = SDL_strrchr(SDL_cdlist[drive],'/');
382 SDL_strlcat(cdromname,lastsl,len);
387 fprintf(stderr, "Should open drive %s, opening %s\n", SDL_cdlist[drive], cdromname);
391 * Use exclusive access. Don't use SC_DIAGNOSTICS as xmcd does because they
392 * require root priviledges, and we don't want that. SC_SINGLE provides
393 * exclusive access with less trouble.
395 fd = openx(cdromname, O_RDONLY, NULL, SC_SINGLE);
399 fprintf(stderr, "Could not open drive %s (%s)\n", cdromname, strerror(errno));
404 struct mode_form_op cdMode;
407 cdMode.action = CD_GET_MODE;
408 ret = SDL_SYS_CDioctl(fd, DK_CD_MODE, &cdMode);
411 "Could not get drive mode for %s (%s)\n",
412 cdromname, strerror(errno));
414 switch(cdMode.cd_mode_form) {
417 "Drive mode for %s is %s\n",
418 cdromname, "CD-ROM Data Mode 1");
420 case CD_MODE2_FORM1 :
422 "Drive mode for %s is %s\n",
423 cdromname, "CD-ROM XA Data Mode 2 Form 1");
425 case CD_MODE2_FORM2 :
427 "Drive mode for %s is %s\n",
428 cdromname, "CD-ROM XA Data Mode 2 Form 2");
432 "Drive mode for %s is %s\n",
437 "Drive mode for %s is %s\n",
438 cdromname, "unknown");
444 cdMode.action = CD_CHG_MODE;
445 cdMode.cd_mode_form = CD_DA;
446 ret = SDL_SYS_CDioctl(fd, DK_CD_MODE, &cdMode);
450 "Could not set drive mode for %s (%s)\n",
451 cdromname, strerror(errno));
453 SDL_SetError("ioctl() error: Could not set CD drive mode, %s",
458 "Drive mode for %s set to CD_DA\n",
467 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
469 struct cd_audio_cmd cmd;
470 struct cd_audio_cmd entry;
474 cmd.audio_cmds = CD_TRK_INFO_AUDIO;
475 cmd.msf_flag = FALSE;
476 if ( SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd) < 0 ) {
481 cdrom->numtracks = cmd.indexing.track_index.last_track
482 - cmd.indexing.track_index.first_track+1;
483 if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
484 cdrom->numtracks = SDL_MAX_TRACKS;
487 /* Read all the track TOC entries */
488 for ( i=0; i<=cdrom->numtracks; ++i ) {
489 if ( i == cdrom->numtracks ) {
490 cdrom->track[i].id = 0xAA;;
492 cdrom->track[i].id = cmd.indexing.track_index.first_track+i;
494 entry.audio_cmds = CD_GET_TRK_MSF;
495 entry.indexing.track_msf.track = cdrom->track[i].id;
496 if ( SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &entry) < 0 ) {
499 cdrom->track[i].type = 0; /* don't know how to detect 0x04 data track */
500 cdrom->track[i].offset = MSF_TO_FRAMES(
501 entry.indexing.track_msf.mins,
502 entry.indexing.track_msf.secs,
503 entry.indexing.track_msf.frames);
504 cdrom->track[i].length = 0;
506 cdrom->track[i-1].length = cdrom->track[i].offset
507 - cdrom->track[i-1].offset;
511 if ( i == (cdrom->numtracks+1) ) {
514 return(okay ? 0 : -1);
517 /* Get CD-ROM status */
518 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
521 struct cd_audio_cmd cmd;
522 cmd.audio_cmds = CD_INFO_AUDIO;
524 if ( SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd) < 0 ) {
526 fprintf(stderr, "ioctl failed in SDL_SYS_CDStatus (%s)\n", SDL_GetError());
530 switch (cmd.status) {
543 fprintf(stderr, "cdStatus failed with CD_NOT_VALID\n");
547 case CD_STATUS_ERROR:
549 fprintf(stderr, "cdStatus failed with CD_STATUS_ERROR\n");
555 fprintf(stderr, "cdStatus failed with unknown error\n");
562 if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
563 *position = MSF_TO_FRAMES( cmd.indexing.info_audio.current_mins,
564 cmd.indexing.info_audio.current_secs,
565 cmd.indexing.info_audio.current_frames);
574 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
576 struct cd_audio_cmd cmd;
579 * My CD Rom is muted by default. I think I read that this is new with
580 * AIX 4.3. SDL does not change the volume, so I need a kludge. Maybe
581 * its better to do this elsewhere?
583 cmd.audio_cmds = CD_PLAY_AUDIO | CD_SET_VOLUME;
586 &cmd.indexing.msf.first_mins,
587 &cmd.indexing.msf.first_secs,
588 &cmd.indexing.msf.first_frames);
589 FRAMES_TO_MSF(start+length,
590 &cmd.indexing.msf.last_mins,
591 &cmd.indexing.msf.last_secs,
592 &cmd.indexing.msf.last_frames);
593 cmd.volume_type = CD_VOLUME_ALL;
594 cmd.all_channel_vol = 255; /* This is a uchar. What is a good value? No docu! */
595 cmd.out_port_0_sel = CD_AUDIO_CHNL_0;
596 cmd.out_port_1_sel = CD_AUDIO_CHNL_1;
597 cmd.out_port_2_sel = CD_AUDIO_CHNL_2;
598 cmd.out_port_3_sel = CD_AUDIO_CHNL_3;
601 fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
602 cmd.indexing.msf.first_mins,
603 cmd.indexing.msf.first_secs,
604 cmd.indexing.msf.first_frames,
605 cmd.indexing.msf.last_mins,
606 cmd.indexing.msf.last_secs,
607 cmd.indexing.msf.last_frames);
609 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd));
613 static int SDL_SYS_CDPause(SDL_CD *cdrom)
615 struct cd_audio_cmd cmd;
616 cmd.audio_cmds = CD_PAUSE_AUDIO;
617 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd));
621 static int SDL_SYS_CDResume(SDL_CD *cdrom)
623 struct cd_audio_cmd cmd;
624 cmd.audio_cmds = CD_RESUME_AUDIO;
625 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd));
629 static int SDL_SYS_CDStop(SDL_CD *cdrom)
631 struct cd_audio_cmd cmd;
632 cmd.audio_cmds = CD_STOP_AUDIO;
633 return(SDL_SYS_CDioctl(cdrom->id, DKAUDIO, &cmd));
636 /* Eject the CD-ROM */
637 static int SDL_SYS_CDEject(SDL_CD *cdrom)
639 return(SDL_SYS_CDioctl(cdrom->id, DKEJECT, 0));
642 /* Close the CD-ROM handle */
643 static void SDL_SYS_CDClose(SDL_CD *cdrom)
648 void SDL_SYS_CDQuit(void)
652 if ( SDL_numcds > 0 ) {
653 for ( i=0; i<SDL_numcds; ++i ) {
654 SDL_free(SDL_cdlist[i]);
660 #endif /* SDL_CDROM_AIX */