src/cdrom/osf/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 26 Feb 2006 04:54:01 +0000
changeset 1438 1f4f09641645
parent 1402 d910939febfa
child 1635 92947e3a18db
permissions -rw-r--r--
Date: Sun, 26 Feb 2006 11:25:09 +0900
From: Hayashi Naoyuki
Subject: Re: [SDL] CVS stable again, please update SDL ports

Some problems are caused on Tru64 UNIX.
If applying SDL12-osf1.path, these problems are fixed.


1. configure-script say "recursive mutexes... no" and "pthread
semaphores... no".
checking for pthreads... yes
checking for recursive mutexes... no
checking for pthread semaphores... no

This is because it compiled without pthread_cflags and pthread_lib when
checking recursive mutexes and pthread semaphores.


2. Compiling src/audio/mme/SDL_mmeaudio.c fails.
cc: Severe: ./src/audio/mme/SDL_mmeaudio.c, line 25: Cannot find file
<mme_api.h> specified in #include directive. (noinclfilef)
#include <mme_api.h>
-^

This is because BUILD_CFLAGS is wrong.


3. Compiling src/cdrom/osf/SDL_syscdrom.c fails.
cc: Warning: ./src/cdrom/osf/SDL_syscdrom.c, line 176: Too few actual
parameters in the invocation of the macro "SDL_stack_alloc". (toofewactuals)
cdpath = SDL_stack_alloc(len);
------------------------------------^
cc: Error: ./src/cdrom/osf/SDL_syscdrom.c, line 176: Invalid expression.
(badexpr)
cdpath = SDL_stack_alloc(len);
-----------------^

SDL_stack_alloc is defined in include/SDL_stdinc.h.
#define SDL_stack_alloc(type, count) (type*)alloca(sizeof(type)*count)


4. Linking fails if running configure with --enable-x11-shared=yes.
/usr/ccs/bin/ld:
Warning: Unresolved:
p_XData32


5. Compiling src/video/x11/SDL_x11dyn.c fails if running configure with
--enable-x11-shared=no.
cc: Error: ./src/video/x11/SDL_x11sym.h, line 149: In this statement,
"_SmtBufferOverflow" is not declared. (undeclared)
SDL_X11_SYM(1,void,_SmtBufferOverflow,(Display *dpy,register smtDisplayPtr))
^
cc: Error: ./src/video/x11/SDL_x11sym.h, line 150: In this statement,
"_SmtBufferOverflow" is not declared. (undeclared)
SDL_X11_SYM(1,void,_SmtBufferOverflow,(Display *dpy,register smtDisplayPtr))
^
cc: Error: ./src/video/x11/SDL_x11sym.h, line 150: In this statement,
"_SmtIpError" is not declared. (undeclared)
SDL_X11_SYM(1,void,_SmtIpError,(Display *dpy,register smtDisplayPtr, int))
^
     1 /*
     2     Tru64 audio module for SDL (Simple DirectMedia Layer)
     3     Copyright (C) 2003
     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 
    20 */
    21 #include "SDL_config.h"
    22 
    23 /* Functions for system-level CD-ROM audio control */
    24 
    25 /* #define DEBUG_CDROM 1 */
    26 
    27 #include <sys/types.h>
    28 #include <dirent.h>
    29 #include <sys/stat.h>
    30 #include <fcntl.h>
    31 #include <io/cam/cdrom.h>
    32 #include <io/cam/rzdisk.h>
    33 #include <io/common/devgetinfo.h>
    34 
    35 #include "SDL_cdrom.h"
    36 #include "../SDL_syscdrom.h"
    37 
    38 /* The maximum number of CD-ROM drives we'll detect */
    39 #define MAX_DRIVES 16
    40 
    41 /* A list of available CD-ROM drives */
    42 static char *SDL_cdlist[MAX_DRIVES];
    43 static dev_t SDL_cdmode[MAX_DRIVES];
    44 
    45 /* The system-dependent CD control functions */
    46 static const char *SDL_SYS_CDName(int drive);
    47 static int         SDL_SYS_CDOpen(int drive);
    48 static int         SDL_SYS_CDGetTOC(SDL_CD *cdrom);
    49 static CDstatus    SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
    50 static int         SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
    51 static int         SDL_SYS_CDPause(SDL_CD *cdrom);
    52 static int         SDL_SYS_CDResume(SDL_CD *cdrom);
    53 static int         SDL_SYS_CDStop(SDL_CD *cdrom);
    54 static int         SDL_SYS_CDEject(SDL_CD *cdrom);
    55 static void        SDL_SYS_CDClose(SDL_CD *cdrom);
    56 
    57 /* Check a drive to see if it is a CD-ROM */
    58 /* Caution!! Not tested. */ 
    59 static int CheckDrive(char *drive, struct stat *stbuf)
    60 {
    61     int cdfd, is_cd = 0;
    62     struct mode_sel_sns_params msp;
    63     struct inquiry_info inq;
    64 
    65 #ifdef DEBUG_CDROM
    66     char *devtype[] = {"Disk", "Tape", "Printer", "Processor", "WORM",
    67 	"CD-ROM", "Scanner", "Optical", "Changer", "Comm", "Unknown"};
    68 #endif
    69 
    70     bzero(&msp, sizeof(msp));
    71     bzero(&inq, sizeof(inq));
    72 
    73     /* If it doesn't exist, return -1 */
    74     if ( stat(drive, stbuf) < 0 ) {
    75 	return(-1);
    76     }
    77 
    78     if ( (cdfd = open(drive, (O_RDWR|O_NDELAY), 0)) >= 0 ) {
    79 	msp.msp_addr   =   (caddr_t) &inq;
    80 	msp.msp_pgcode =                0;
    81 	msp.msp_pgctrl =                0;
    82 	msp.msp_length =      sizeof(inq);
    83 	msp.msp_setps  =                0;
    84 
    85 	if ( ioctl(cdfd, SCSI_GET_INQUIRY_DATA, &msp) )
    86 	    return (0);
    87 
    88 #ifdef DEBUG_CDROM
    89 	fprintf(stderr, "Device Type: %s\n", devtype[inq.perfdt]);
    90 	fprintf(stderr, "Vendor: %.8s\n", inq.vndrid);
    91 	fprintf(stderr, "Product: %.8s\n", inq.prodid);
    92 	fprintf(stderr, "Revision: %.8s\n", inq.revlvl);
    93 #endif
    94 	if ( inq.perfdt == DTYPE_RODIRECT )
    95 	    is_cd = 1;
    96     }
    97 
    98     return(is_cd);
    99 }
   100 
   101 /* Add a CD-ROM drive to our list of valid drives */
   102 static void AddDrive(char *drive, struct stat *stbuf)
   103 {
   104     int i;
   105 
   106     if ( SDL_numcds < MAX_DRIVES ) {
   107 	/* Check to make sure it's not already in our list.
   108 	 * This can happen when we see a drive via symbolic link.
   109 	 *
   110 	 */
   111 	for ( i=0; i<SDL_numcds; ++i ) {
   112 	    if ( stbuf->st_rdev == SDL_cdmode[i] ) {
   113 #ifdef DEBUG_CDROM
   114   fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
   115 #endif
   116 	    return;
   117 	    }
   118 	}
   119 
   120 	/* Add this drive to our list */
   121 	i = SDL_numcds;
   122 	SDL_cdlist[i] = SDL_strdup(drive);
   123 	if ( SDL_cdlist[i] == NULL ) {
   124 	    SDL_OutOfMemory();
   125 	    return;
   126 	}
   127 	SDL_cdmode[i] = stbuf->st_rdev;
   128 	++SDL_numcds;
   129 #ifdef DEBUG_CDROM
   130   fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
   131 #endif
   132     }
   133 }
   134 
   135 int  SDL_SYS_CDInit(void)
   136 {
   137     /* checklist:
   138      *
   139      * Tru64 5.X (/dev/rdisk/cdrom?c)
   140      * dir: /dev/rdisk, name: cdrom
   141      *
   142      * Digital UNIX 4.0X (/dev/rrz?c)
   143      * dir: /dev, name: rrz
   144      *
   145      */
   146     struct {
   147 	char *dir;
   148 	char *name;
   149     } checklist[] = {
   150 	{"/dev/rdisk", "cdrom"},
   151 	{"/dev", "rrz"},
   152 	{NULL, NULL}};
   153     char drive[32];
   154     char *SDLcdrom;
   155     int i, j, exists;
   156     struct stat stbuf;
   157 
   158     /* Fill in our driver capabilities */
   159     SDL_CDcaps.Name   = SDL_SYS_CDName;
   160     SDL_CDcaps.Open   = SDL_SYS_CDOpen;
   161     SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   162     SDL_CDcaps.Status = SDL_SYS_CDStatus;
   163     SDL_CDcaps.Play   = SDL_SYS_CDPlay;
   164     SDL_CDcaps.Pause  = SDL_SYS_CDPause;
   165     SDL_CDcaps.Resume = SDL_SYS_CDResume;
   166     SDL_CDcaps.Stop   = SDL_SYS_CDStop;
   167     SDL_CDcaps.Eject  = SDL_SYS_CDEject;
   168     SDL_CDcaps.Close  = SDL_SYS_CDClose;
   169 
   170 
   171     /* Look in the environment for our CD-ROM drive list */
   172     SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
   173     if ( SDLcdrom != NULL ) {
   174 	char *cdpath, *delim;
   175 	size_t len = SDL_strlen(SDLcdrom)+1;
   176 	cdpath = SDL_stack_alloc(char, len);
   177 	if ( cdpath != NULL ) {
   178 	    SDL_strlcpy(cdpath, SDLcdrom, len);
   179 	    SDLcdrom = cdpath;
   180 	    do {
   181 		delim = SDL_strchr(SDLcdrom, ':');
   182 		if ( delim ) {
   183 		    *delim++ = '\0';
   184 		}
   185 		if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
   186 		    AddDrive(SDLcdrom, &stbuf);
   187 		}
   188 		if ( delim ) {
   189 		    SDLcdrom = delim;
   190 		} else {
   191 		    SDLcdrom = NULL;
   192 		}
   193 	    } while ( SDLcdrom );
   194 	    SDL_stack_free(cdpath);
   195 	}
   196 
   197 	/* If we found our drives, there's nothing left to do */
   198 	if ( SDL_numcds > 0 ) {
   199 	    return(0);
   200 	}
   201     }
   202     /* Scan the system for CD-ROM drives */
   203     for ( i = 0; checklist[i].dir; ++i) {
   204 	DIR *devdir;
   205 	struct dirent *devent;
   206 	int name_len;
   207 
   208 	devdir = opendir(checklist[i].dir);
   209 	if (devdir) {
   210 	    name_len = SDL_strlen(checklist[i].name);
   211 	    while (devent = readdir(devdir))
   212 		if (SDL_memcmp(checklist[i].name, devent->d_name, name_len) == 0)
   213 		    if (devent->d_name[devent->d_namlen-1] == 'c') {
   214 			SDL_snprintf(drive, SDL_arraysize(drive), "%s/%s", checklist[i].dir, devent->d_name);
   215 #ifdef DEBUG_CDROM
   216 			fprintf(stderr, "Try to add drive: %s\n", drive);
   217 #endif
   218 			if ( CheckDrive(drive, &stbuf) > 0 )
   219 			    AddDrive(drive, &stbuf);
   220 		    }
   221 	    closedir(devdir);
   222 	} else {
   223 #ifdef DEBUG_CDROM
   224 	    fprintf(stderr, "cannot open dir: %s\n", checklist[i].dir);
   225 #endif
   226 	}
   227     }
   228     return (0);
   229 }
   230 
   231 static const char *SDL_SYS_CDName(int drive)
   232 {
   233     return(SDL_cdlist[drive]);
   234 }
   235 
   236 static int SDL_SYS_CDOpen(int drive)
   237 {
   238     /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
   239     return(open(SDL_cdlist[drive], (O_RDWR|O_NDELAY), 0));
   240 }
   241 
   242 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
   243 {
   244     struct cd_toc                  toc;
   245     struct cd_toc_header           hdr;
   246     struct cd_toc_entry          *cdte;
   247     int i;
   248     int okay = 0;
   249     if ( ioctl(cdrom->id, CDROM_TOC_HEADER, &hdr) ) {
   250 	fprintf(stderr,"ioctl error CDROM_TOC_HEADER\n");
   251 	return -1;
   252     }
   253     cdrom->numtracks = hdr.th_ending_track - hdr.th_starting_track + 1;
   254     if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
   255 	cdrom->numtracks = SDL_MAX_TRACKS;
   256     }
   257 #ifdef DEBUG_CDROM
   258   fprintf(stderr,"hdr.th_data_len1 = %d\n", hdr.th_data_len1);
   259   fprintf(stderr,"hdr.th_data_len0 = %d\n", hdr.th_data_len0);
   260   fprintf(stderr,"hdr.th_starting_track = %d\n", hdr.th_starting_track);
   261   fprintf(stderr,"hdr.th_ending_track = %d\n", hdr.th_ending_track);
   262   fprintf(stderr,"cdrom->numtracks = %d\n", cdrom->numtracks);
   263 #endif
   264     toc.toc_address_format = CDROM_LBA_FORMAT;
   265     toc.toc_starting_track = 0;
   266     toc.toc_alloc_length = (hdr.th_data_len1 << 8) +
   267 			    hdr.th_data_len0 + sizeof(hdr);
   268     if ( (toc.toc_buffer = alloca(toc.toc_alloc_length)) == NULL) {
   269 	fprintf(stderr,"cannot allocate toc.toc_buffer\n");
   270 	return -1;
   271     }
   272 
   273     bzero (toc.toc_buffer, toc.toc_alloc_length);
   274     if (ioctl(cdrom->id, CDROM_TOC_ENTRYS, &toc)) {
   275 	fprintf(stderr,"ioctl error CDROM_TOC_ENTRYS\n");
   276 	return -1;
   277     }
   278 
   279     cdte =(struct cd_toc_entry *) ((char *) toc.toc_buffer + sizeof(hdr));
   280     for (i=0; i <= cdrom->numtracks; ++i) {
   281 	if (i == cdrom->numtracks ) {
   282 	    cdrom->track[i].id = 0xAA;;
   283 	} else {
   284 	    cdrom->track[i].id = hdr.th_starting_track + i;
   285 	}
   286 
   287 	cdrom->track[i].type =
   288 	    cdte[i].te_control & CDROM_DATA_TRACK;
   289 	cdrom->track[i].offset =
   290 	    cdte[i].te_absaddr.lba.addr3 << 24 |
   291 	    cdte[i].te_absaddr.lba.addr2 << 16 |
   292 	    cdte[i].te_absaddr.lba.addr1 << 8  |
   293 	    cdte[i].te_absaddr.lba.addr0;
   294 	cdrom->track[i].length = 0;
   295 	if ( i > 0 ) {
   296 	    cdrom->track[i - 1].length =
   297 		cdrom->track[i].offset -
   298 		cdrom->track[i - 1].offset;
   299 	}
   300     }
   301 #ifdef DEBUG_CDROM
   302   for (i = 0; i <= cdrom->numtracks; i++) {
   303     fprintf(stderr,"toc_entry[%d].te_track_number = %d\n",
   304 	    i,cdte[i].te_track_number);
   305     fprintf(stderr,"cdrom->track[%d].id = %d\n", i,cdrom->track[i].id);
   306     fprintf(stderr,"cdrom->track[%d].type = %x\n", i,cdrom->track[i].type);
   307     fprintf(stderr,"cdrom->track[%d].offset = %d\n", i,cdrom->track[i].offset);
   308     fprintf(stderr,"cdrom->track[%d].length = %d\n", i,cdrom->track[i].length);
   309   }
   310 #endif
   311     if ( i == (cdrom->numtracks+1) ) {
   312 	okay = 1;
   313     }
   314 
   315     return(okay ? 0 : -1);
   316 }
   317 
   318 /* Get CD-ROM status */
   319 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
   320 {
   321     CDstatus                     status;
   322     struct cd_sub_channel            sc;
   323     struct cd_subc_channel_data     scd;
   324 
   325     sc.sch_address_format = CDROM_LBA_FORMAT;
   326     sc.sch_data_format    = CDROM_CURRENT_POSITION;
   327     sc.sch_track_number   = 0;
   328     sc.sch_alloc_length   = sizeof(scd);
   329     sc.sch_buffer         = (caddr_t)&scd;
   330     if ( ioctl(cdrom->id, CDROM_READ_SUBCHANNEL, &sc) ) {
   331 	status = CD_ERROR;
   332 	fprintf(stderr,"ioctl error CDROM_READ_SUBCHANNEL \n");
   333     } else {
   334 	switch (scd.scd_header.sh_audio_status) {
   335 	    case AS_AUDIO_INVALID:
   336 		status = CD_STOPPED;
   337 		break;
   338 	    case AS_PLAY_IN_PROGRESS:
   339 		status = CD_PLAYING;
   340 		break;
   341 	    case AS_PLAY_PAUSED:
   342 		status = CD_PAUSED;
   343 		break;
   344 	    case AS_PLAY_COMPLETED:
   345 		status = CD_STOPPED;
   346 		break;
   347 	    case AS_PLAY_ERROR:
   348 		status = CD_ERROR;
   349 		break;
   350 	    case AS_NO_STATUS:
   351 		status = CD_STOPPED;
   352 		break;
   353 	    default:
   354 		status = CD_ERROR;
   355 		break;
   356 	}
   357 #ifdef DEBUG_CDROM
   358   fprintf(stderr,"scd.scd_header.sh_audio_status = %x\n",
   359 	scd.scd_header.sh_audio_status);
   360 #endif
   361     }
   362     if (position) {
   363 	if (status == CD_PLAYING || (status == CD_PAUSED) ) {
   364 	    *position =
   365 		scd.scd_position_data.scp_absaddr.lba.addr3 << 24 |
   366 		scd.scd_position_data.scp_absaddr.lba.addr2 << 16 |
   367 		scd.scd_position_data.scp_absaddr.lba.addr1 << 8  |
   368 		scd.scd_position_data.scp_absaddr.lba.addr0;
   369 	} else {
   370 	    *position = 0;
   371 	}
   372     }
   373 
   374     return status;
   375 }
   376 
   377 /* Start play */
   378 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
   379 {
   380 /*
   381  * Play MSF
   382  */
   383     struct cd_play_audio_msf msf;
   384     int end;
   385 
   386     bzero(&msf, sizeof(msf));
   387     end = start +length;
   388     FRAMES_TO_MSF(start + 150, /* LBA = 4500*M + 75*S + F - 150 */
   389 		  &msf.msf_starting_M_unit,
   390 		  &msf.msf_starting_S_unit,
   391 		  &msf.msf_starting_F_unit);
   392     FRAMES_TO_MSF(end + 150, /* LBA = 4500*M + 75*S + F - 150 */
   393 		  &msf.msf_ending_M_unit,
   394 		  &msf.msf_ending_S_unit,
   395 		  &msf.msf_ending_F_unit);
   396 
   397     return(ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
   398 }
   399 
   400 /* Pause play */
   401 static int SDL_SYS_CDPause(SDL_CD *cdrom)
   402 {
   403     return(ioctl(cdrom->id, CDROM_PAUSE_PLAY));
   404 }
   405 
   406 /* Resume play */
   407 static int SDL_SYS_CDResume(SDL_CD *cdrom)
   408 {
   409     return(ioctl(cdrom->id, CDROM_RESUME_PLAY));
   410 }
   411 
   412 /* Stop play */
   413 static int SDL_SYS_CDStop(SDL_CD *cdrom)
   414 {
   415     return(ioctl(cdrom->id, SCSI_STOP_UNIT));
   416 }
   417 
   418 /* Eject the CD-ROM */
   419 static int SDL_SYS_CDEject(SDL_CD *cdrom)
   420 {
   421     return(ioctl(cdrom->id, CDROM_EJECT_CADDY));
   422 }
   423 
   424 /* Close the CD-ROM handle */
   425 static void SDL_SYS_CDClose(SDL_CD *cdrom)
   426 {
   427     close(cdrom->id);
   428 }
   429 
   430 void SDL_SYS_CDQuit(void)
   431 {
   432     int i;
   433 
   434     if ( SDL_numcds > 0 ) {
   435 	for ( i=0; i<SDL_numcds; ++i ) {
   436 	    SDL_free(SDL_cdlist[i]);
   437 	}
   438 	SDL_numcds = 0;
   439     }
   440 }
   441 
   442