Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 342:850502f0e8de
prev245:a1d0655a88d3
next422:61a0598e07ff
author nkeynes
date Wed Jan 31 10:58:42 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Refactor gdrom module to be more conducive to real device support
file annotate diff log raw
nkeynes@342
     1
nkeynes@138
     2
/**
nkeynes@342
     3
 * $Id: gdrom.c,v 1.12 2007-01-31 10:58:42 nkeynes Exp $
nkeynes@138
     4
 *
nkeynes@138
     5
 * GD-Rom  access functions.
nkeynes@138
     6
 *
nkeynes@138
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@138
     8
 *
nkeynes@138
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@138
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@138
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@138
    12
 * (at your option) any later version.
nkeynes@138
    13
 *
nkeynes@138
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@138
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@138
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@138
    17
 * GNU General Public License for more details.
nkeynes@138
    18
 */
nkeynes@138
    19
nkeynes@138
    20
#include <stdio.h>
nkeynes@237
    21
#include <fcntl.h>
nkeynes@168
    22
#include <errno.h>
nkeynes@138
    23
#include "gdrom/ide.h"
nkeynes@138
    24
#include "gdrom/gdrom.h"
nkeynes@143
    25
#include "gdrom/packet.h"
nkeynes@138
    26
#include "dream.h"
nkeynes@138
    27
nkeynes@342
    28
extern gdrom_disc_t gdrom_disc;
nkeynes@138
    29
nkeynes@237
    30
gdrom_image_class_t gdrom_image_classes[] = { &linux_device_class, &nrg_image_class, &cdi_image_class, NULL };
nkeynes@138
    31
nkeynes@138
    32
char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
nkeynes@138
    33
uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
nkeynes@138
    34
nkeynes@138
    35
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    36
{
nkeynes@168
    37
    const gchar *ext = strrchr(filename, '.');
nkeynes@168
    38
    gdrom_disc_t disc = NULL;
nkeynes@237
    39
nkeynes@237
    40
    int fd = open( filename, O_RDONLY | O_NONBLOCK );
nkeynes@237
    41
    FILE *f;
nkeynes@168
    42
    int i,j;
nkeynes@168
    43
    gdrom_image_class_t extclz = NULL;
nkeynes@168
    44
nkeynes@237
    45
    if( fd == -1 ) {
nkeynes@168
    46
	ERROR("Unable to open file '%s': %s", filename, strerror(errno));
nkeynes@168
    47
	return NULL;
nkeynes@168
    48
    }
nkeynes@168
    49
nkeynes@237
    50
    f = fdopen(fd, "ro");
nkeynes@237
    51
nkeynes@237
    52
nkeynes@168
    53
    /* try extensions */
nkeynes@168
    54
    if( ext != NULL ) {
nkeynes@168
    55
	ext++; /* Skip the '.' */
nkeynes@168
    56
	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@237
    57
	    if( gdrom_image_classes[i]->extension != NULL &&
nkeynes@237
    58
		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
nkeynes@168
    59
		extclz = gdrom_image_classes[i];
nkeynes@168
    60
		if( extclz->is_valid_file(f) ) {
nkeynes@168
    61
		    disc = extclz->open_image_file(filename, f);
nkeynes@168
    62
		    if( disc != NULL )
nkeynes@168
    63
			return disc;
nkeynes@168
    64
		}
nkeynes@168
    65
		break;
nkeynes@168
    66
	    }
nkeynes@168
    67
	}
nkeynes@168
    68
    }
nkeynes@168
    69
nkeynes@168
    70
    /* Okay, fall back to magic */
nkeynes@237
    71
    gboolean recognized = FALSE;
nkeynes@168
    72
    for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@168
    73
	if( gdrom_image_classes[i] != extclz &&
nkeynes@168
    74
	    gdrom_image_classes[i]->is_valid_file(f) ) {
nkeynes@237
    75
	    recognized = TRUE;
nkeynes@168
    76
	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
nkeynes@168
    77
	    if( disc != NULL )
nkeynes@168
    78
		return disc;
nkeynes@168
    79
	}
nkeynes@168
    80
    }
nkeynes@168
    81
nkeynes@237
    82
    if( !recognized ) {
nkeynes@237
    83
	ERROR( "Unable to open disc %s: Unsupported format", filename );
nkeynes@237
    84
    }
nkeynes@168
    85
    fclose(f);
nkeynes@168
    86
    return NULL;
nkeynes@138
    87
}
nkeynes@138
    88
nkeynes@138
    89
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
    90
{
nkeynes@138
    91
    gdrom_unmount_disc();
nkeynes@138
    92
    gdrom_disc = disc;
nkeynes@342
    93
    gdrom_image_dump_info( disc );
nkeynes@138
    94
}
nkeynes@138
    95
nkeynes@138
    96
gdrom_disc_t gdrom_mount_image( const gchar *filename )
nkeynes@138
    97
{
nkeynes@138
    98
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@138
    99
    if( disc != NULL )
nkeynes@138
   100
	gdrom_mount_disc( disc );
nkeynes@138
   101
    return disc;
nkeynes@138
   102
}
nkeynes@138
   103
nkeynes@138
   104
void gdrom_unmount_disc( ) 
nkeynes@138
   105
{
nkeynes@138
   106
    if( gdrom_disc != NULL ) {
nkeynes@138
   107
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   108
    }
nkeynes@138
   109
    gdrom_disc = NULL;
nkeynes@342
   110
nkeynes@138
   111
}
nkeynes@138
   112
.