src/cdrom/linux/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
/*
slouken@0
     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
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1635
    24
#ifdef SDL_CDROM_LINUX
slouken@1635
    25
slouken@0
    26
/* Functions for system-level CD-ROM audio control */
slouken@0
    27
slouken@1662
    28
#include <string.h>             /* For strerror() */
slouken@0
    29
#include <sys/types.h>
slouken@0
    30
#include <sys/stat.h>
slouken@0
    31
#include <sys/ioctl.h>
slouken@0
    32
#include <fcntl.h>
slouken@0
    33
#include <errno.h>
slouken@0
    34
#include <unistd.h>
slouken@1402
    35
#ifdef __LINUX__
slouken@1062
    36
#ifdef HAVE_LINUX_VERSION_H
slouken@998
    37
/* linux 2.6.9 workaround */
slouken@998
    38
#include <linux/version.h>
slouken@998
    39
#if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,9)
slouken@998
    40
#include <asm/types.h>
slouken@998
    41
#define __le64 __u64
slouken@998
    42
#define __le32 __u32
slouken@998
    43
#define __le16 __u16
slouken@998
    44
#define __be64 __u64
slouken@998
    45
#define __be32 __u32
slouken@998
    46
#define __be16 __u16
slouken@998
    47
#endif /* linux 2.6.9 workaround */
slouken@1062
    48
#endif /* HAVE_LINUX_VERSION_H */
slouken@0
    49
#include <linux/cdrom.h>
slouken@0
    50
#endif
slouken@0
    51
#ifdef __SVR4
slouken@0
    52
#include <sys/cdio.h>
slouken@0
    53
#endif
slouken@0
    54
slouken@0
    55
/* Define this to use the alternative getmntent() code */
slouken@0
    56
#ifndef __SVR4
slouken@0
    57
#define USE_MNTENT
slouken@0
    58
#endif
slouken@0
    59
slouken@0
    60
#ifdef USE_MNTENT
slouken@0
    61
#if defined(__USLC__)
slouken@0
    62
#include <sys/mntent.h>
slouken@0
    63
#else
slouken@0
    64
#include <mntent.h>
slouken@0
    65
#endif
slouken@0
    66
slouken@0
    67
#ifndef _PATH_MNTTAB
slouken@0
    68
#ifdef MNTTAB
slouken@0
    69
#define _PATH_MNTTAB	MNTTAB
slouken@0
    70
#else
slouken@0
    71
#define _PATH_MNTTAB	"/etc/fstab"
slouken@0
    72
#endif
slouken@0
    73
#endif /* !_PATH_MNTTAB */
slouken@0
    74
slouken@0
    75
#ifndef _PATH_MOUNTED
slouken@0
    76
#define _PATH_MOUNTED	"/etc/mtab"
slouken@0
    77
#endif /* !_PATH_MOUNTED */
slouken@0
    78
slouken@0
    79
#ifndef MNTTYPE_CDROM
slouken@0
    80
#define MNTTYPE_CDROM	"iso9660"
slouken@0
    81
#endif
slouken@0
    82
#ifndef MNTTYPE_SUPER
slouken@0
    83
#define MNTTYPE_SUPER	"supermount"
slouken@0
    84
#endif
slouken@0
    85
#endif /* USE_MNTENT */
slouken@0
    86
slouken@0
    87
#include "SDL_cdrom.h"
slouken@1361
    88
#include "../SDL_syscdrom.h"
slouken@0
    89
slouken@0
    90
slouken@0
    91
/* The maximum number of CD-ROM drives we'll detect */
slouken@1662
    92
#define MAX_DRIVES	16
slouken@0
    93
slouken@0
    94
/* A list of available CD-ROM drives */
slouken@0
    95
static char *SDL_cdlist[MAX_DRIVES];
slouken@0
    96
static dev_t SDL_cdmode[MAX_DRIVES];
slouken@0
    97
slouken@0
    98
/* The system-dependent CD control functions */
slouken@1668
    99
static const char *SDL_SYS_CDName(int drive);
slouken@1668
   100
static int SDL_SYS_CDOpen(int drive);
slouken@1668
   101
static int SDL_SYS_CDGetTOC(SDL_CD * cdrom);
slouken@1668
   102
static CDstatus SDL_SYS_CDStatus(SDL_CD * cdrom, int *position);
slouken@1668
   103
static int SDL_SYS_CDPlay(SDL_CD * cdrom, int start, int length);
slouken@1668
   104
static int SDL_SYS_CDPause(SDL_CD * cdrom);
slouken@1668
   105
static int SDL_SYS_CDResume(SDL_CD * cdrom);
slouken@1668
   106
static int SDL_SYS_CDStop(SDL_CD * cdrom);
slouken@1668
   107
static int SDL_SYS_CDEject(SDL_CD * cdrom);
slouken@1668
   108
static void SDL_SYS_CDClose(SDL_CD * cdrom);
slouken@0
   109
slouken@0
   110
/* Some ioctl() errno values which occur when the tray is empty */
slouken@396
   111
#ifndef ENOMEDIUM
slouken@396
   112
#define ENOMEDIUM ENOENT
slouken@396
   113
#endif
slouken@0
   114
#define ERRNO_TRAYEMPTY(errno)	\
slouken@396
   115
	((errno == EIO)    || (errno == ENOENT) || \
slouken@396
   116
	 (errno == EINVAL) || (errno == ENOMEDIUM))
slouken@0
   117
slouken@0
   118
/* Check a drive to see if it is a CD-ROM */
slouken@1662
   119
static int
slouken@1668
   120
CheckDrive(char *drive, char *mnttype, struct stat *stbuf)
slouken@0
   121
{
slouken@1662
   122
    int is_cd, cdfd;
slouken@1662
   123
    struct cdrom_subchnl info;
slouken@0
   124
slouken@1662
   125
    /* If it doesn't exist, return -1 */
slouken@1668
   126
    if (stat(drive, stbuf) < 0) {
slouken@1662
   127
        return (-1);
slouken@1662
   128
    }
slouken@0
   129
slouken@1662
   130
    /* If it does exist, verify that it's an available CD-ROM */
slouken@1662
   131
    is_cd = 0;
slouken@1668
   132
    if (S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode)) {
slouken@1668
   133
        cdfd = open(drive, (O_RDONLY | O_NONBLOCK), 0);
slouken@1662
   134
        if (cdfd >= 0) {
slouken@1662
   135
            info.cdsc_format = CDROM_MSF;
slouken@1662
   136
            /* Under Linux, EIO occurs when a disk is not present.
slouken@1662
   137
             */
slouken@1668
   138
            if ((ioctl(cdfd, CDROMSUBCHNL, &info) == 0) ||
slouken@1668
   139
                ERRNO_TRAYEMPTY(errno)) {
slouken@1662
   140
                is_cd = 1;
slouken@1662
   141
            }
slouken@1668
   142
            close(cdfd);
slouken@1662
   143
        }
slouken@0
   144
#ifdef USE_MNTENT
slouken@1662
   145
        /* Even if we can't read it, it might be mounted */
slouken@1668
   146
        else if (mnttype && (SDL_strcmp(mnttype, MNTTYPE_CDROM) == 0)) {
slouken@1662
   147
            is_cd = 1;
slouken@1662
   148
        }
slouken@0
   149
#endif
slouken@1662
   150
    }
slouken@1662
   151
    return (is_cd);
slouken@0
   152
}
slouken@0
   153
slouken@0
   154
/* Add a CD-ROM drive to our list of valid drives */
slouken@1662
   155
static void
slouken@1668
   156
AddDrive(char *drive, struct stat *stbuf)
slouken@0
   157
{
slouken@1662
   158
    int i;
slouken@0
   159
slouken@1662
   160
    if (SDL_numcds < MAX_DRIVES) {
slouken@1662
   161
        /* Check to make sure it's not already in our list.
slouken@1662
   162
           This can happen when we see a drive via symbolic link.
slouken@1662
   163
         */
slouken@1662
   164
        for (i = 0; i < SDL_numcds; ++i) {
slouken@1662
   165
            if (stbuf->st_rdev == SDL_cdmode[i]) {
slouken@0
   166
#ifdef DEBUG_CDROM
slouken@1668
   167
                fprintf(stderr, "Duplicate drive detected: %s == %s\n",
slouken@1668
   168
                        drive, SDL_cdlist[i]);
slouken@0
   169
#endif
slouken@1662
   170
                return;
slouken@1662
   171
            }
slouken@1662
   172
        }
slouken@0
   173
slouken@1662
   174
        /* Add this drive to our list */
slouken@1662
   175
        i = SDL_numcds;
slouken@1668
   176
        SDL_cdlist[i] = SDL_strdup(drive);
slouken@1662
   177
        if (SDL_cdlist[i] == NULL) {
slouken@1668
   178
            SDL_OutOfMemory();
slouken@1662
   179
            return;
slouken@1662
   180
        }
slouken@1662
   181
        SDL_cdmode[i] = stbuf->st_rdev;
slouken@1662
   182
        ++SDL_numcds;
slouken@0
   183
#ifdef DEBUG_CDROM
slouken@1668
   184
        fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
slouken@0
   185
#endif
slouken@1662
   186
    }
slouken@0
   187
}
slouken@0
   188
slouken@0
   189
#ifdef USE_MNTENT
slouken@1662
   190
static void
slouken@1668
   191
CheckMounts(const char *mtab)
slouken@0
   192
{
slouken@1662
   193
    FILE *mntfp;
slouken@1662
   194
    struct mntent *mntent;
slouken@1662
   195
    struct stat stbuf;
slouken@0
   196
slouken@1668
   197
    mntfp = setmntent(mtab, "r");
slouken@1662
   198
    if (mntfp != NULL) {
slouken@1662
   199
        char *tmp;
slouken@1662
   200
        char *mnt_type;
slouken@1662
   201
        size_t mnt_type_len;
slouken@1662
   202
        char *mnt_dev;
slouken@1662
   203
        size_t mnt_dev_len;
slouken@0
   204
slouken@1668
   205
        while ((mntent = getmntent(mntfp)) != NULL) {
slouken@1668
   206
            mnt_type_len = SDL_strlen(mntent->mnt_type) + 1;
slouken@1668
   207
            mnt_type = SDL_stack_alloc(char, mnt_type_len);
slouken@1662
   208
            if (mnt_type == NULL)
slouken@1662
   209
                continue;       /* maybe you'll get lucky next time. */
slouken@139
   210
slouken@1668
   211
            mnt_dev_len = SDL_strlen(mntent->mnt_fsname) + 1;
slouken@1668
   212
            mnt_dev = SDL_stack_alloc(char, mnt_dev_len);
slouken@1662
   213
            if (mnt_dev == NULL) {
slouken@1668
   214
                SDL_stack_free(mnt_type);
slouken@1662
   215
                continue;
slouken@1662
   216
            }
slouken@139
   217
slouken@1668
   218
            SDL_strlcpy(mnt_type, mntent->mnt_type, mnt_type_len);
slouken@1668
   219
            SDL_strlcpy(mnt_dev, mntent->mnt_fsname, mnt_dev_len);
slouken@0
   220
slouken@1662
   221
            /* Handle "supermount" filesystem mounts */
slouken@1668
   222
            if (SDL_strcmp(mnt_type, MNTTYPE_SUPER) == 0) {
slouken@1668
   223
                tmp = SDL_strstr(mntent->mnt_opts, "fs=");
slouken@1662
   224
                if (tmp) {
slouken@1668
   225
                    SDL_free(mnt_type);
slouken@1668
   226
                    mnt_type = SDL_strdup(tmp + SDL_strlen("fs="));
slouken@1662
   227
                    if (mnt_type) {
slouken@1668
   228
                        tmp = SDL_strchr(mnt_type, ',');
slouken@1662
   229
                        if (tmp) {
slouken@1662
   230
                            *tmp = '\0';
slouken@1662
   231
                        }
slouken@1662
   232
                    }
slouken@1662
   233
                }
slouken@1668
   234
                tmp = SDL_strstr(mntent->mnt_opts, "dev=");
slouken@1662
   235
                if (tmp) {
slouken@1668
   236
                    SDL_free(mnt_dev);
slouken@1668
   237
                    mnt_dev = SDL_strdup(tmp + SDL_strlen("dev="));
slouken@1662
   238
                    if (mnt_dev) {
slouken@1668
   239
                        tmp = SDL_strchr(mnt_dev, ',');
slouken@1662
   240
                        if (tmp) {
slouken@1662
   241
                            *tmp = '\0';
slouken@1662
   242
                        }
slouken@1662
   243
                    }
slouken@1662
   244
                }
slouken@1662
   245
            }
slouken@1668
   246
            if (SDL_strcmp(mnt_type, MNTTYPE_CDROM) == 0) {
slouken@0
   247
#ifdef DEBUG_CDROM
slouken@1668
   248
                fprintf(stderr,
slouken@1668
   249
                        "Checking mount path from %s: %s mounted on %s of %s\n",
slouken@1668
   250
                        mtab, mnt_dev, mntent->mnt_dir, mnt_type);
slouken@0
   251
#endif
slouken@1668
   252
                if (CheckDrive(mnt_dev, mnt_type, &stbuf) > 0) {
slouken@1668
   253
                    AddDrive(mnt_dev, &stbuf);
slouken@1662
   254
                }
slouken@1662
   255
            }
slouken@1668
   256
            SDL_stack_free(mnt_dev);
slouken@1668
   257
            SDL_stack_free(mnt_type);
slouken@1662
   258
        }
slouken@1668
   259
        endmntent(mntfp);
slouken@1662
   260
    }
slouken@0
   261
}
slouken@0
   262
#endif /* USE_MNTENT */
slouken@0
   263
slouken@1662
   264
int
slouken@1668
   265
SDL_SYS_CDInit(void)
slouken@0
   266
{
slouken@1662
   267
    /* checklist: /dev/cdrom, /dev/hd?, /dev/scd? /dev/sr? */
slouken@1662
   268
    static char *checklist[] = {
slouken@1662
   269
        "cdrom", "?a hd?", "?0 scd?", "?0 sr?", NULL
slouken@1662
   270
    };
slouken@1662
   271
    char *SDLcdrom;
slouken@1662
   272
    int i, j, exists;
slouken@1662
   273
    char drive[32];
slouken@1662
   274
    struct stat stbuf;
slouken@0
   275
slouken@1662
   276
    /* Fill in our driver capabilities */
slouken@1662
   277
    SDL_CDcaps.Name = SDL_SYS_CDName;
slouken@1662
   278
    SDL_CDcaps.Open = SDL_SYS_CDOpen;
slouken@1662
   279
    SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
slouken@1662
   280
    SDL_CDcaps.Status = SDL_SYS_CDStatus;
slouken@1662
   281
    SDL_CDcaps.Play = SDL_SYS_CDPlay;
slouken@1662
   282
    SDL_CDcaps.Pause = SDL_SYS_CDPause;
slouken@1662
   283
    SDL_CDcaps.Resume = SDL_SYS_CDResume;
slouken@1662
   284
    SDL_CDcaps.Stop = SDL_SYS_CDStop;
slouken@1662
   285
    SDL_CDcaps.Eject = SDL_SYS_CDEject;
slouken@1662
   286
    SDL_CDcaps.Close = SDL_SYS_CDClose;
slouken@0
   287
slouken@1662
   288
    /* Look in the environment for our CD-ROM drive list */
slouken@1668
   289
    SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */
slouken@1662
   290
    if (SDLcdrom != NULL) {
slouken@1662
   291
        char *cdpath, *delim;
slouken@1668
   292
        size_t len = SDL_strlen(SDLcdrom) + 1;
slouken@1668
   293
        cdpath = SDL_stack_alloc(char, len);
slouken@1662
   294
        if (cdpath != NULL) {
slouken@1668
   295
            SDL_strlcpy(cdpath, SDLcdrom, len);
slouken@1662
   296
            SDLcdrom = cdpath;
slouken@1662
   297
            do {
slouken@1668
   298
                delim = SDL_strchr(SDLcdrom, ':');
slouken@1662
   299
                if (delim) {
slouken@1662
   300
                    *delim++ = '\0';
slouken@1662
   301
                }
slouken@0
   302
#ifdef DEBUG_CDROM
slouken@1668
   303
                fprintf(stderr,
slouken@1668
   304
                        "Checking CD-ROM drive from SDL_CDROM: %s\n",
slouken@1668
   305
                        SDLcdrom);
slouken@0
   306
#endif
slouken@1668
   307
                if (CheckDrive(SDLcdrom, NULL, &stbuf) > 0) {
slouken@1668
   308
                    AddDrive(SDLcdrom, &stbuf);
slouken@1662
   309
                }
slouken@1662
   310
                if (delim) {
slouken@1662
   311
                    SDLcdrom = delim;
slouken@1662
   312
                } else {
slouken@1662
   313
                    SDLcdrom = NULL;
slouken@1662
   314
                }
slouken@1662
   315
            }
slouken@1662
   316
            while (SDLcdrom);
slouken@1668
   317
            SDL_stack_free(cdpath);
slouken@1662
   318
        }
slouken@0
   319
slouken@1662
   320
        /* If we found our drives, there's nothing left to do */
slouken@1662
   321
        if (SDL_numcds > 0) {
slouken@1662
   322
            return (0);
slouken@1662
   323
        }
slouken@1662
   324
    }
slouken@1662
   325
#ifdef USE_MNTENT
slouken@1662
   326
    /* Check /dev/cdrom first :-) */
slouken@1668
   327
    if (CheckDrive("/dev/cdrom", NULL, &stbuf) > 0) {
slouken@1668
   328
        AddDrive("/dev/cdrom", &stbuf);
slouken@1662
   329
    }
slouken@0
   330
slouken@1662
   331
    /* Now check the currently mounted CD drives */
slouken@1668
   332
    CheckMounts(_PATH_MOUNTED);
slouken@0
   333
slouken@1662
   334
    /* Finally check possible mountable drives in /etc/fstab */
slouken@1668
   335
    CheckMounts(_PATH_MNTTAB);
slouken@0
   336
slouken@1662
   337
    /* If we found our drives, there's nothing left to do */
slouken@1662
   338
    if (SDL_numcds > 0) {
slouken@1662
   339
        return (0);
slouken@1662
   340
    }
slouken@0
   341
#endif /* USE_MNTENT */
slouken@0
   342
slouken@1662
   343
    /* Scan the system for CD-ROM drives.
slouken@1662
   344
       Not always 100% reliable, so use the USE_MNTENT code above first.
slouken@1662
   345
     */
slouken@1662
   346
    for (i = 0; checklist[i]; ++i) {
slouken@1662
   347
        if (checklist[i][0] == '?') {
slouken@1662
   348
            char *insert;
slouken@1662
   349
            exists = 1;
slouken@1662
   350
            for (j = checklist[i][1]; exists; ++j) {
slouken@1668
   351
                SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s",
slouken@1668
   352
                             &checklist[i][3]);
slouken@1668
   353
                insert = SDL_strchr(drive, '?');
slouken@1662
   354
                if (insert != NULL) {
slouken@1662
   355
                    *insert = j;
slouken@1662
   356
                }
slouken@0
   357
#ifdef DEBUG_CDROM
slouken@1668
   358
                fprintf(stderr, "Checking possible CD-ROM drive: %s\n",
slouken@1668
   359
                        drive);
slouken@0
   360
#endif
slouken@1668
   361
                switch (CheckDrive(drive, NULL, &stbuf)) {
slouken@1662
   362
                    /* Drive exists and is a CD-ROM */
slouken@1662
   363
                case 1:
slouken@1668
   364
                    AddDrive(drive, &stbuf);
slouken@1662
   365
                    break;
slouken@1662
   366
                    /* Drive exists, but isn't a CD-ROM */
slouken@1662
   367
                case 0:
slouken@1662
   368
                    break;
slouken@1662
   369
                    /* Drive doesn't exist */
slouken@1662
   370
                case -1:
slouken@1662
   371
                    exists = 0;
slouken@1662
   372
                    break;
slouken@1662
   373
                }
slouken@1662
   374
            }
slouken@1662
   375
        } else {
slouken@1668
   376
            SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s",
slouken@1668
   377
                         checklist[i]);
slouken@0
   378
#ifdef DEBUG_CDROM
slouken@1668
   379
            fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive);
slouken@0
   380
#endif
slouken@1668
   381
            if (CheckDrive(drive, NULL, &stbuf) > 0) {
slouken@1668
   382
                AddDrive(drive, &stbuf);
slouken@1662
   383
            }
slouken@1662
   384
        }
slouken@1662
   385
    }
slouken@1662
   386
    return (0);
slouken@0
   387
}
slouken@0
   388
slouken@0
   389
/* General ioctl() CD-ROM command function */
slouken@1662
   390
static int
slouken@1668
   391
SDL_SYS_CDioctl(int id, int command, void *arg)
slouken@0
   392
{
slouken@1662
   393
    int retval;
slouken@0
   394
slouken@1668
   395
    retval = ioctl(id, command, arg);
slouken@1662
   396
    if (retval < 0) {
slouken@1668
   397
        SDL_SetError("ioctl() error: %s", strerror(errno));
slouken@1662
   398
    }
slouken@1662
   399
    return (retval);
slouken@0
   400
}
slouken@0
   401
slouken@1662
   402
static const char *
slouken@1668
   403
SDL_SYS_CDName(int drive)
slouken@0
   404
{
slouken@1662
   405
    return (SDL_cdlist[drive]);
slouken@0
   406
}
slouken@0
   407
slouken@1662
   408
static int
slouken@1668
   409
SDL_SYS_CDOpen(int drive)
slouken@0
   410
{
slouken@1668
   411
    return (open(SDL_cdlist[drive], (O_RDONLY | O_NONBLOCK), 0));
slouken@0
   412
}
slouken@0
   413
slouken@1662
   414
static int
slouken@1668
   415
SDL_SYS_CDGetTOC(SDL_CD * cdrom)
slouken@0
   416
{
slouken@1662
   417
    struct cdrom_tochdr toc;
slouken@1662
   418
    int i, okay;
slouken@1662
   419
    struct cdrom_tocentry entry;
slouken@0
   420
slouken@1662
   421
    okay = 0;
slouken@1668
   422
    if (SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc) == 0) {
slouken@1662
   423
        cdrom->numtracks = toc.cdth_trk1 - toc.cdth_trk0 + 1;
slouken@1662
   424
        if (cdrom->numtracks > SDL_MAX_TRACKS) {
slouken@1662
   425
            cdrom->numtracks = SDL_MAX_TRACKS;
slouken@1662
   426
        }
slouken@1662
   427
        /* Read all the track TOC entries */
slouken@1662
   428
        for (i = 0; i <= cdrom->numtracks; ++i) {
slouken@1662
   429
            if (i == cdrom->numtracks) {
slouken@1662
   430
                cdrom->track[i].id = CDROM_LEADOUT;
slouken@1662
   431
            } else {
slouken@1662
   432
                cdrom->track[i].id = toc.cdth_trk0 + i;
slouken@1662
   433
            }
slouken@1662
   434
            entry.cdte_track = cdrom->track[i].id;
slouken@1662
   435
            entry.cdte_format = CDROM_MSF;
slouken@1668
   436
            if (SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCENTRY, &entry) < 0) {
slouken@1662
   437
                break;
slouken@1662
   438
            } else {
slouken@1662
   439
                if (entry.cdte_ctrl & CDROM_DATA_TRACK) {
slouken@1662
   440
                    cdrom->track[i].type = SDL_DATA_TRACK;
slouken@1662
   441
                } else {
slouken@1662
   442
                    cdrom->track[i].type = SDL_AUDIO_TRACK;
slouken@1662
   443
                }
slouken@1662
   444
                cdrom->track[i].offset =
slouken@1668
   445
                    MSF_TO_FRAMES(entry.cdte_addr.msf.minute,
slouken@1668
   446
                                  entry.cdte_addr.msf.second,
slouken@1668
   447
                                  entry.cdte_addr.msf.frame);
slouken@1662
   448
                cdrom->track[i].length = 0;
slouken@1662
   449
                if (i > 0) {
slouken@1662
   450
                    cdrom->track[i - 1].length =
slouken@1662
   451
                        cdrom->track[i].offset - cdrom->track[i - 1].offset;
slouken@1662
   452
                }
slouken@1662
   453
            }
slouken@1662
   454
        }
slouken@1662
   455
        if (i == (cdrom->numtracks + 1)) {
slouken@1662
   456
            okay = 1;
slouken@1662
   457
        }
slouken@1662
   458
    }
slouken@1662
   459
    return (okay ? 0 : -1);
slouken@0
   460
}
slouken@0
   461
slouken@0
   462
/* Get CD-ROM status */
slouken@1662
   463
static CDstatus
slouken@1668
   464
SDL_SYS_CDStatus(SDL_CD * cdrom, int *position)
slouken@0
   465
{
slouken@1662
   466
    CDstatus status;
slouken@1662
   467
    struct cdrom_tochdr toc;
slouken@1662
   468
    struct cdrom_subchnl info;
slouken@0
   469
slouken@1662
   470
    info.cdsc_format = CDROM_MSF;
slouken@1668
   471
    if (ioctl(cdrom->id, CDROMSUBCHNL, &info) < 0) {
slouken@1668
   472
        if (ERRNO_TRAYEMPTY(errno)) {
slouken@1662
   473
            status = CD_TRAYEMPTY;
slouken@1662
   474
        } else {
slouken@1662
   475
            status = CD_ERROR;
slouken@1662
   476
        }
slouken@1662
   477
    } else {
slouken@1662
   478
        switch (info.cdsc_audiostatus) {
slouken@1662
   479
        case CDROM_AUDIO_INVALID:
slouken@1662
   480
        case CDROM_AUDIO_NO_STATUS:
slouken@1662
   481
            /* Try to determine if there's a CD available */
slouken@1668
   482
            if (ioctl(cdrom->id, CDROMREADTOCHDR, &toc) == 0)
slouken@1662
   483
                status = CD_STOPPED;
slouken@1662
   484
            else
slouken@1662
   485
                status = CD_TRAYEMPTY;
slouken@1662
   486
            break;
slouken@1662
   487
        case CDROM_AUDIO_COMPLETED:
slouken@1662
   488
            status = CD_STOPPED;
slouken@1662
   489
            break;
slouken@1662
   490
        case CDROM_AUDIO_PLAY:
slouken@1662
   491
            status = CD_PLAYING;
slouken@1662
   492
            break;
slouken@1662
   493
        case CDROM_AUDIO_PAUSED:
slouken@1662
   494
            /* Workaround buggy CD-ROM drive */
slouken@1662
   495
            if (info.cdsc_trk == CDROM_LEADOUT) {
slouken@1662
   496
                status = CD_STOPPED;
slouken@1662
   497
            } else {
slouken@1662
   498
                status = CD_PAUSED;
slouken@1662
   499
            }
slouken@1662
   500
            break;
slouken@1662
   501
        default:
slouken@1662
   502
            status = CD_ERROR;
slouken@1662
   503
            break;
slouken@1662
   504
        }
slouken@1662
   505
    }
slouken@1662
   506
    if (position) {
slouken@1662
   507
        if (status == CD_PLAYING || (status == CD_PAUSED)) {
slouken@1668
   508
            *position = MSF_TO_FRAMES(info.cdsc_absaddr.msf.minute,
slouken@1668
   509
                                      info.cdsc_absaddr.msf.second,
slouken@1668
   510
                                      info.cdsc_absaddr.msf.frame);
slouken@1662
   511
        } else {
slouken@1662
   512
            *position = 0;
slouken@1662
   513
        }
slouken@1662
   514
    }
slouken@1662
   515
    return (status);
slouken@0
   516
}
slouken@0
   517
slouken@0
   518
/* Start play */
slouken@1662
   519
static int
slouken@1668
   520
SDL_SYS_CDPlay(SDL_CD * cdrom, int start, int length)
slouken@0
   521
{
slouken@1662
   522
    struct cdrom_msf playtime;
slouken@0
   523
slouken@1668
   524
    FRAMES_TO_MSF(start,
slouken@1668
   525
                  &playtime.cdmsf_min0, &playtime.cdmsf_sec0,
slouken@1668
   526
                  &playtime.cdmsf_frame0);
slouken@1668
   527
    FRAMES_TO_MSF(start + length, &playtime.cdmsf_min1, &playtime.cdmsf_sec1,
slouken@1668
   528
                  &playtime.cdmsf_frame1);
slouken@0
   529
#ifdef DEBUG_CDROM
slouken@1668
   530
    fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
slouken@1668
   531
            playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
slouken@1668
   532
            playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
slouken@0
   533
#endif
slouken@1668
   534
    return (SDL_SYS_CDioctl(cdrom->id, CDROMPLAYMSF, &playtime));
slouken@0
   535
}
slouken@0
   536
slouken@0
   537
/* Pause play */
slouken@1662
   538
static int
slouken@1668
   539
SDL_SYS_CDPause(SDL_CD * cdrom)
slouken@0
   540
{
slouken@1668
   541
    return (SDL_SYS_CDioctl(cdrom->id, CDROMPAUSE, 0));
slouken@0
   542
}
slouken@0
   543
slouken@0
   544
/* Resume play */
slouken@1662
   545
static int
slouken@1668
   546
SDL_SYS_CDResume(SDL_CD * cdrom)
slouken@0
   547
{
slouken@1668
   548
    return (SDL_SYS_CDioctl(cdrom->id, CDROMRESUME, 0));
slouken@0
   549
}
slouken@0
   550
slouken@0
   551
/* Stop play */
slouken@1662
   552
static int
slouken@1668
   553
SDL_SYS_CDStop(SDL_CD * cdrom)
slouken@0
   554
{
slouken@1668
   555
    return (SDL_SYS_CDioctl(cdrom->id, CDROMSTOP, 0));
slouken@0
   556
}
slouken@0
   557
slouken@0
   558
/* Eject the CD-ROM */
slouken@1662
   559
static int
slouken@1668
   560
SDL_SYS_CDEject(SDL_CD * cdrom)
slouken@0
   561
{
slouken@1668
   562
    return (SDL_SYS_CDioctl(cdrom->id, CDROMEJECT, 0));
slouken@0
   563
}
slouken@0
   564
slouken@0
   565
/* Close the CD-ROM handle */
slouken@1662
   566
static void
slouken@1668
   567
SDL_SYS_CDClose(SDL_CD * cdrom)
slouken@0
   568
{
slouken@1668
   569
    close(cdrom->id);
slouken@0
   570
}
slouken@0
   571
slouken@1662
   572
void
slouken@1668
   573
SDL_SYS_CDQuit(void)
slouken@0
   574
{
slouken@1662
   575
    int i;
slouken@0
   576
slouken@1662
   577
    if (SDL_numcds > 0) {
slouken@1662
   578
        for (i = 0; i < SDL_numcds; ++i) {
slouken@1668
   579
            SDL_free(SDL_cdlist[i]);
slouken@1662
   580
        }
slouken@1662
   581
        SDL_numcds = 0;
slouken@1662
   582
    }
slouken@0
   583
}
slouken@0
   584
slouken@1635
   585
#endif /* SDL_CDROM_LINUX */
slouken@1662
   586
/* vi: set ts=4 sw=4 expandtab: */