2 Tru64 audio module for SDL (Simple DirectMedia Layer)
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.
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.
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
21 #include "SDL_config.h"
25 /* Functions for system-level CD-ROM audio control */
27 /* #define DEBUG_CDROM 1 */
29 #include <sys/types.h>
33 #include <io/cam/cdrom.h>
34 #include <io/cam/rzdisk.h>
35 #include <io/common/devgetinfo.h>
37 #include "SDL_cdrom.h"
38 #include "../SDL_syscdrom.h"
40 /* The maximum number of CD-ROM drives we'll detect */
43 /* A list of available CD-ROM drives */
44 static char *SDL_cdlist[MAX_DRIVES];
45 static dev_t SDL_cdmode[MAX_DRIVES];
47 /* The system-dependent CD control functions */
48 static const char *SDL_SYS_CDName(int drive);
49 static int SDL_SYS_CDOpen(int drive);
50 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
51 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
52 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
53 static int SDL_SYS_CDPause(SDL_CD *cdrom);
54 static int SDL_SYS_CDResume(SDL_CD *cdrom);
55 static int SDL_SYS_CDStop(SDL_CD *cdrom);
56 static int SDL_SYS_CDEject(SDL_CD *cdrom);
57 static void SDL_SYS_CDClose(SDL_CD *cdrom);
59 /* Check a drive to see if it is a CD-ROM */
60 /* Caution!! Not tested. */
61 static int CheckDrive(char *drive, struct stat *stbuf)
64 struct mode_sel_sns_params msp;
65 struct inquiry_info inq;
68 char *devtype[] = {"Disk", "Tape", "Printer", "Processor", "WORM",
69 "CD-ROM", "Scanner", "Optical", "Changer", "Comm", "Unknown"};
72 bzero(&msp, sizeof(msp));
73 bzero(&inq, sizeof(inq));
75 /* If it doesn't exist, return -1 */
76 if ( stat(drive, stbuf) < 0 ) {
80 if ( (cdfd = open(drive, (O_RDWR|O_NDELAY), 0)) >= 0 ) {
81 msp.msp_addr = (caddr_t) &inq;
84 msp.msp_length = sizeof(inq);
87 if ( ioctl(cdfd, SCSI_GET_INQUIRY_DATA, &msp) )
91 fprintf(stderr, "Device Type: %s\n", devtype[inq.perfdt]);
92 fprintf(stderr, "Vendor: %.8s\n", inq.vndrid);
93 fprintf(stderr, "Product: %.8s\n", inq.prodid);
94 fprintf(stderr, "Revision: %.8s\n", inq.revlvl);
96 if ( inq.perfdt == DTYPE_RODIRECT )
103 /* Add a CD-ROM drive to our list of valid drives */
104 static void AddDrive(char *drive, struct stat *stbuf)
108 if ( SDL_numcds < MAX_DRIVES ) {
109 /* Check to make sure it's not already in our list.
110 * This can happen when we see a drive via symbolic link.
113 for ( i=0; i<SDL_numcds; ++i ) {
114 if ( stbuf->st_rdev == SDL_cdmode[i] ) {
116 fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
122 /* Add this drive to our list */
124 SDL_cdlist[i] = SDL_strdup(drive);
125 if ( SDL_cdlist[i] == NULL ) {
129 SDL_cdmode[i] = stbuf->st_rdev;
132 fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
137 int SDL_SYS_CDInit(void)
141 * Tru64 5.X (/dev/rdisk/cdrom?c)
142 * dir: /dev/rdisk, name: cdrom
144 * Digital UNIX 4.0X (/dev/rrz?c)
145 * dir: /dev, name: rrz
152 {"/dev/rdisk", "cdrom"},
160 /* Fill in our driver capabilities */
161 SDL_CDcaps.Name = SDL_SYS_CDName;
162 SDL_CDcaps.Open = SDL_SYS_CDOpen;
163 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
164 SDL_CDcaps.Status = SDL_SYS_CDStatus;
165 SDL_CDcaps.Play = SDL_SYS_CDPlay;
166 SDL_CDcaps.Pause = SDL_SYS_CDPause;
167 SDL_CDcaps.Resume = SDL_SYS_CDResume;
168 SDL_CDcaps.Stop = SDL_SYS_CDStop;
169 SDL_CDcaps.Eject = SDL_SYS_CDEject;
170 SDL_CDcaps.Close = SDL_SYS_CDClose;
173 /* Look in the environment for our CD-ROM drive list */
174 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */
175 if ( SDLcdrom != NULL ) {
176 char *cdpath, *delim;
177 size_t len = SDL_strlen(SDLcdrom)+1;
178 cdpath = SDL_stack_alloc(char, len);
179 if ( cdpath != NULL ) {
180 SDL_strlcpy(cdpath, SDLcdrom, len);
183 delim = SDL_strchr(SDLcdrom, ':');
187 if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
188 AddDrive(SDLcdrom, &stbuf);
195 } while ( SDLcdrom );
196 SDL_stack_free(cdpath);
199 /* If we found our drives, there's nothing left to do */
200 if ( SDL_numcds > 0 ) {
204 /* Scan the system for CD-ROM drives */
205 for ( i = 0; checklist[i].dir; ++i) {
207 struct dirent *devent;
210 devdir = opendir(checklist[i].dir);
212 name_len = SDL_strlen(checklist[i].name);
213 while (devent = readdir(devdir))
214 if (SDL_memcmp(checklist[i].name, devent->d_name, name_len) == 0)
215 if (devent->d_name[devent->d_namlen-1] == 'c') {
216 SDL_snprintf(drive, SDL_arraysize(drive), "%s/%s", checklist[i].dir, devent->d_name);
218 fprintf(stderr, "Try to add drive: %s\n", drive);
220 if ( CheckDrive(drive, &stbuf) > 0 )
221 AddDrive(drive, &stbuf);
226 fprintf(stderr, "cannot open dir: %s\n", checklist[i].dir);
233 static const char *SDL_SYS_CDName(int drive)
235 return(SDL_cdlist[drive]);
238 static int SDL_SYS_CDOpen(int drive)
240 /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
241 return(open(SDL_cdlist[drive], (O_RDWR|O_NDELAY), 0));
244 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
247 struct cd_toc_header hdr;
248 struct cd_toc_entry *cdte;
251 if ( ioctl(cdrom->id, CDROM_TOC_HEADER, &hdr) ) {
252 fprintf(stderr,"ioctl error CDROM_TOC_HEADER\n");
255 cdrom->numtracks = hdr.th_ending_track - hdr.th_starting_track + 1;
256 if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
257 cdrom->numtracks = SDL_MAX_TRACKS;
260 fprintf(stderr,"hdr.th_data_len1 = %d\n", hdr.th_data_len1);
261 fprintf(stderr,"hdr.th_data_len0 = %d\n", hdr.th_data_len0);
262 fprintf(stderr,"hdr.th_starting_track = %d\n", hdr.th_starting_track);
263 fprintf(stderr,"hdr.th_ending_track = %d\n", hdr.th_ending_track);
264 fprintf(stderr,"cdrom->numtracks = %d\n", cdrom->numtracks);
266 toc.toc_address_format = CDROM_LBA_FORMAT;
267 toc.toc_starting_track = 0;
268 toc.toc_alloc_length = (hdr.th_data_len1 << 8) +
269 hdr.th_data_len0 + sizeof(hdr);
270 if ( (toc.toc_buffer = alloca(toc.toc_alloc_length)) == NULL) {
271 fprintf(stderr,"cannot allocate toc.toc_buffer\n");
275 bzero (toc.toc_buffer, toc.toc_alloc_length);
276 if (ioctl(cdrom->id, CDROM_TOC_ENTRYS, &toc)) {
277 fprintf(stderr,"ioctl error CDROM_TOC_ENTRYS\n");
281 cdte =(struct cd_toc_entry *) ((char *) toc.toc_buffer + sizeof(hdr));
282 for (i=0; i <= cdrom->numtracks; ++i) {
283 if (i == cdrom->numtracks ) {
284 cdrom->track[i].id = 0xAA;;
286 cdrom->track[i].id = hdr.th_starting_track + i;
289 cdrom->track[i].type =
290 cdte[i].te_control & CDROM_DATA_TRACK;
291 cdrom->track[i].offset =
292 cdte[i].te_absaddr.lba.addr3 << 24 |
293 cdte[i].te_absaddr.lba.addr2 << 16 |
294 cdte[i].te_absaddr.lba.addr1 << 8 |
295 cdte[i].te_absaddr.lba.addr0;
296 cdrom->track[i].length = 0;
298 cdrom->track[i - 1].length =
299 cdrom->track[i].offset -
300 cdrom->track[i - 1].offset;
304 for (i = 0; i <= cdrom->numtracks; i++) {
305 fprintf(stderr,"toc_entry[%d].te_track_number = %d\n",
306 i,cdte[i].te_track_number);
307 fprintf(stderr,"cdrom->track[%d].id = %d\n", i,cdrom->track[i].id);
308 fprintf(stderr,"cdrom->track[%d].type = %x\n", i,cdrom->track[i].type);
309 fprintf(stderr,"cdrom->track[%d].offset = %d\n", i,cdrom->track[i].offset);
310 fprintf(stderr,"cdrom->track[%d].length = %d\n", i,cdrom->track[i].length);
313 if ( i == (cdrom->numtracks+1) ) {
317 return(okay ? 0 : -1);
320 /* Get CD-ROM status */
321 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
324 struct cd_sub_channel sc;
325 struct cd_subc_channel_data scd;
327 sc.sch_address_format = CDROM_LBA_FORMAT;
328 sc.sch_data_format = CDROM_CURRENT_POSITION;
329 sc.sch_track_number = 0;
330 sc.sch_alloc_length = sizeof(scd);
331 sc.sch_buffer = (caddr_t)&scd;
332 if ( ioctl(cdrom->id, CDROM_READ_SUBCHANNEL, &sc) ) {
334 fprintf(stderr,"ioctl error CDROM_READ_SUBCHANNEL \n");
336 switch (scd.scd_header.sh_audio_status) {
337 case AS_AUDIO_INVALID:
340 case AS_PLAY_IN_PROGRESS:
346 case AS_PLAY_COMPLETED:
360 fprintf(stderr,"scd.scd_header.sh_audio_status = %x\n",
361 scd.scd_header.sh_audio_status);
365 if (status == CD_PLAYING || (status == CD_PAUSED) ) {
367 scd.scd_position_data.scp_absaddr.lba.addr3 << 24 |
368 scd.scd_position_data.scp_absaddr.lba.addr2 << 16 |
369 scd.scd_position_data.scp_absaddr.lba.addr1 << 8 |
370 scd.scd_position_data.scp_absaddr.lba.addr0;
380 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
385 struct cd_play_audio_msf msf;
388 bzero(&msf, sizeof(msf));
390 FRAMES_TO_MSF(start + 150, /* LBA = 4500*M + 75*S + F - 150 */
391 &msf.msf_starting_M_unit,
392 &msf.msf_starting_S_unit,
393 &msf.msf_starting_F_unit);
394 FRAMES_TO_MSF(end + 150, /* LBA = 4500*M + 75*S + F - 150 */
395 &msf.msf_ending_M_unit,
396 &msf.msf_ending_S_unit,
397 &msf.msf_ending_F_unit);
399 return(ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
403 static int SDL_SYS_CDPause(SDL_CD *cdrom)
405 return(ioctl(cdrom->id, CDROM_PAUSE_PLAY));
409 static int SDL_SYS_CDResume(SDL_CD *cdrom)
411 return(ioctl(cdrom->id, CDROM_RESUME_PLAY));
415 static int SDL_SYS_CDStop(SDL_CD *cdrom)
417 return(ioctl(cdrom->id, SCSI_STOP_UNIT));
420 /* Eject the CD-ROM */
421 static int SDL_SYS_CDEject(SDL_CD *cdrom)
423 return(ioctl(cdrom->id, CDROM_EJECT_CADDY));
426 /* Close the CD-ROM handle */
427 static void SDL_SYS_CDClose(SDL_CD *cdrom)
432 void SDL_SYS_CDQuit(void)
436 if ( SDL_numcds > 0 ) {
437 for ( i=0; i<SDL_numcds; ++i ) {
438 SDL_free(SDL_cdlist[i]);
444 #endif /* SDL_CDROM_OSF */