Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 759:f16975739abc
prev736:a02d1475ccfd
next837:4eae2ddccf9c
author nkeynes
date Mon Jul 21 01:01:39 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix batch of -Wall warnings
view annotate diff log raw
     2 /**
     3  * $Id$
     4  *
     5  * GD-Rom  access functions.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <stdio.h>
    21 #include <fcntl.h>
    22 #include <errno.h>
    23 #include <glib/gutils.h>
    24 #include "gdrom/ide.h"
    25 #include "gdrom/gdrom.h"
    26 #include "gdrom/gddriver.h"
    27 #include "gdrom/packet.h"
    28 #include "dream.h"
    30 extern gdrom_disc_t gdrom_disc;
    32 DEFINE_HOOK( gdrom_disc_change_hook, gdrom_disc_change_hook_t )
    34 static void gdrom_fire_disc_changed( gdrom_disc_t disc )
    35 {
    36     CALL_HOOKS( gdrom_disc_change_hook, disc, disc == NULL ? NULL : disc->name );
    37 }
    39 gdrom_image_class_t gdrom_image_classes[] = { &cdrom_device_class, 
    40         &nrg_image_class, 
    41         &cdi_image_class, 
    42         &gdi_image_class, 
    43         NULL };
    45 char *gdrom_mode_names[] = { "Mode 0", "Mode 1", "Mode 2", "Mode 2 Form 1", "Mode 2 Form 2", "Audio", 
    46         "Mode 2 semiraw", "XA Raw", "Non-XA Raw" };
    47 uint32_t gdrom_sector_size[] = { 0, 2048, 2336, 2048, 2324, 2352, 2336, 2352, 2352 };
    49 gdrom_disc_t gdrom_image_open( const gchar *inFilename )
    50 {
    51     const gchar *filename = inFilename;
    52     const gchar *ext = strrchr(filename, '.');
    53     gdrom_disc_t disc = NULL;
    54     int fd;
    55     FILE *f;
    56     int i;
    57     gdrom_image_class_t extclz = NULL;
    59     // Check for a url-style filename.
    60     char *lizard_lips = strstr( filename, "://" );
    61     if( lizard_lips != NULL ) {
    62         gchar *path = lizard_lips + 3;
    63         int method_len = (lizard_lips-filename);
    64         gchar method[method_len + 1];
    65         memcpy( method, filename, method_len );
    66         method[method_len] = '\0';
    68         if( strcasecmp( method, "file" ) == 0 ) {
    69             filename = path;
    70         } else if( strcasecmp( method, "dvd" ) == 0 ||
    71                 strcasecmp( method, "cd" ) == 0 ||
    72                 strcasecmp( method, "cdrom" ) ) {
    73             return cdrom_open_device( method, path );
    74         } else {
    75             ERROR( "Unrecognized URL method '%s' in filename '%s'", method, filename );
    76             return NULL;
    77         }
    78     }
    80     fd = open( filename, O_RDONLY | O_NONBLOCK );
    81     if( fd == -1 ) {
    82         return NULL;
    83     }
    85     f = fdopen(fd, "ro");
    88     /* try extensions */
    89     if( ext != NULL ) {
    90         ext++; /* Skip the '.' */
    91         for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    92             if( gdrom_image_classes[i]->extension != NULL &&
    93                     strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
    94                 extclz = gdrom_image_classes[i];
    95                 if( extclz->is_valid_file(f) ) {
    96                     disc = extclz->open_image_file(filename, f);
    97                     if( disc != NULL )
    98                         return disc;
    99                 }
   100                 break;
   101             }
   102         }
   103     }
   105     /* Okay, fall back to magic */
   106     gboolean recognized = FALSE;
   107     for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
   108         if( gdrom_image_classes[i] != extclz &&
   109                 gdrom_image_classes[i]->is_valid_file(f) ) {
   110             recognized = TRUE;
   111             disc = gdrom_image_classes[i]->open_image_file(filename, f);
   112             if( disc != NULL )
   113                 return disc;
   114         }
   115     }
   117     fclose(f);
   118     return NULL;
   119 }
   121 void gdrom_mount_disc( gdrom_disc_t disc ) 
   122 {
   123     if( disc != gdrom_disc ) {
   124         if( gdrom_disc != NULL ) {
   125             gdrom_disc->close(gdrom_disc);
   126         }
   127         gdrom_disc = disc;
   128         gdrom_image_dump_info( disc );
   129         gdrom_fire_disc_changed( disc );
   130     }
   131 }
   133 gboolean gdrom_mount_image( const gchar *filename )
   134 {
   135     gdrom_disc_t disc = gdrom_image_open(filename);
   136     if( disc != NULL ) {         
   137         gdrom_mount_disc( disc );
   138         return TRUE;
   139     }
   140     return FALSE;
   141 }
   143 void gdrom_unmount_disc( ) 
   144 {
   145     if( gdrom_disc != NULL ) {
   146         gdrom_disc->close(gdrom_disc);
   147         gdrom_fire_disc_changed(NULL);
   148         gdrom_disc = NULL;
   149     }
   150 }
   152 gdrom_disc_t gdrom_get_current_disc()
   153 {
   154     return gdrom_disc;
   155 }
   157 const gchar *gdrom_get_current_disc_name()
   158 {
   159     if( gdrom_disc == NULL ) {
   160         return NULL;
   161     } else {
   162         return gdrom_disc->name;
   163     }
   164 }
   166 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
   167 {
   168     gchar *dirname = g_path_get_dirname(base_name);
   169     gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
   170     g_free(dirname);
   171     return pathname;
   172 }
.