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