Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 613:c2dd87f947b2
prev561:533f6b478071
next644:ccae4bfa5f82
author nkeynes
date Fri Feb 08 11:30:04 2008 +0000 (16 years ago)
permissions -rw-r--r--
last change Bail out properly on read errors during a DMA cycle
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 "gdrom/ide.h"
    24 #include "gdrom/gdrom.h"
    25 #include "gdrom/packet.h"
    26 #include "dream.h"
    28 extern gdrom_disc_t gdrom_disc;
    30 gdrom_image_class_t gdrom_image_classes[] = { &cdrom_device_class, 
    31 					      &nrg_image_class, 
    32 					      &cdi_image_class, 
    33 					      &gdi_image_class, 
    34 					      NULL };
    36 char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
    37 uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
    39 gdrom_disc_t gdrom_image_open( const gchar *filename )
    40 {
    41     const gchar *ext = strrchr(filename, '.');
    42     gdrom_disc_t disc = NULL;
    44     int fd = open( filename, O_RDONLY | O_NONBLOCK );
    45     FILE *f;
    46     int i;
    47     gdrom_image_class_t extclz = NULL;
    49     if( fd == -1 ) {
    50 	return NULL;
    51     }
    53     f = fdopen(fd, "ro");
    56     /* try extensions */
    57     if( ext != NULL ) {
    58 	ext++; /* Skip the '.' */
    59 	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    60 	    if( gdrom_image_classes[i]->extension != NULL &&
    61 		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
    62 		extclz = gdrom_image_classes[i];
    63 		if( extclz->is_valid_file(f) ) {
    64 		    disc = extclz->open_image_file(filename, f);
    65 		    if( disc != NULL )
    66 			return disc;
    67 		}
    68 		break;
    69 	    }
    70 	}
    71     }
    73     /* Okay, fall back to magic */
    74     gboolean recognized = FALSE;
    75     for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    76 	if( gdrom_image_classes[i] != extclz &&
    77 	    gdrom_image_classes[i]->is_valid_file(f) ) {
    78 	    recognized = TRUE;
    79 	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
    80 	    if( disc != NULL )
    81 		return disc;
    82 	}
    83     }
    85     fclose(f);
    86     return NULL;
    87 }
    89 void gdrom_mount_disc( gdrom_disc_t disc ) 
    90 {
    91     gdrom_unmount_disc();
    92     gdrom_disc = disc;
    93     gdrom_image_dump_info( disc );
    94 }
    96 gboolean gdrom_mount_image( const gchar *filename )
    97 {
    98     gdrom_disc_t disc = gdrom_image_open(filename);
    99     if( disc != NULL ) {
   100 	gdrom_mount_disc( disc );
   101 	return TRUE;
   102     }
   103     return FALSE;
   104 }
   106 void gdrom_unmount_disc( ) 
   107 {
   108     if( gdrom_disc != NULL ) {
   109 	gdrom_disc->close(gdrom_disc);
   110     }
   111     gdrom_disc = NULL;
   113 }
   115 gdrom_disc_t gdrom_get_current_disc()
   116 {
   117     return gdrom_disc;
   118 }
   120 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
   121 {
   122     gchar *dirname = g_path_get_dirname(base_name);
   123     gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
   124     g_free(dirname);
   125     return pathname;
   126 }
.