Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 492:84e33e4dda1c
prev489:45c8ddcf52cb
next561:533f6b478071
author nkeynes
date Tue Nov 06 08:35:16 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Issue #37: Add nulldc GDI format
file annotate diff log raw
nkeynes@342
     1
nkeynes@138
     2
/**
nkeynes@492
     3
 * $Id: gdrom.c,v 1.19 2007-11-06 08:35:16 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@492
    30
gdrom_image_class_t gdrom_image_classes[] = { &cdrom_device_class, 
nkeynes@492
    31
					      &nrg_image_class, 
nkeynes@492
    32
					      &cdi_image_class, 
nkeynes@492
    33
					      &gdi_image_class, 
nkeynes@492
    34
					      NULL };
nkeynes@138
    35
nkeynes@138
    36
char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
nkeynes@138
    37
uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
nkeynes@138
    38
nkeynes@138
    39
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    40
{
nkeynes@168
    41
    const gchar *ext = strrchr(filename, '.');
nkeynes@168
    42
    gdrom_disc_t disc = NULL;
nkeynes@237
    43
nkeynes@237
    44
    int fd = open( filename, O_RDONLY | O_NONBLOCK );
nkeynes@237
    45
    FILE *f;
nkeynes@422
    46
    int i;
nkeynes@168
    47
    gdrom_image_class_t extclz = NULL;
nkeynes@168
    48
nkeynes@237
    49
    if( fd == -1 ) {
nkeynes@168
    50
	return NULL;
nkeynes@168
    51
    }
nkeynes@168
    52
nkeynes@237
    53
    f = fdopen(fd, "ro");
nkeynes@237
    54
nkeynes@237
    55
nkeynes@168
    56
    /* try extensions */
nkeynes@168
    57
    if( ext != NULL ) {
nkeynes@168
    58
	ext++; /* Skip the '.' */
nkeynes@168
    59
	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@237
    60
	    if( gdrom_image_classes[i]->extension != NULL &&
nkeynes@237
    61
		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
nkeynes@168
    62
		extclz = gdrom_image_classes[i];
nkeynes@168
    63
		if( extclz->is_valid_file(f) ) {
nkeynes@168
    64
		    disc = extclz->open_image_file(filename, f);
nkeynes@168
    65
		    if( disc != NULL )
nkeynes@168
    66
			return disc;
nkeynes@168
    67
		}
nkeynes@168
    68
		break;
nkeynes@168
    69
	    }
nkeynes@168
    70
	}
nkeynes@168
    71
    }
nkeynes@168
    72
nkeynes@168
    73
    /* Okay, fall back to magic */
nkeynes@237
    74
    gboolean recognized = FALSE;
nkeynes@168
    75
    for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@168
    76
	if( gdrom_image_classes[i] != extclz &&
nkeynes@168
    77
	    gdrom_image_classes[i]->is_valid_file(f) ) {
nkeynes@237
    78
	    recognized = TRUE;
nkeynes@168
    79
	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
nkeynes@168
    80
	    if( disc != NULL )
nkeynes@168
    81
		return disc;
nkeynes@168
    82
	}
nkeynes@168
    83
    }
nkeynes@168
    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@433
    96
gboolean gdrom_mount_image( const gchar *filename )
nkeynes@138
    97
{
nkeynes@138
    98
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@433
    99
    if( disc != NULL ) {
nkeynes@138
   100
	gdrom_mount_disc( disc );
nkeynes@433
   101
	return TRUE;
nkeynes@433
   102
    }
nkeynes@433
   103
    return FALSE;
nkeynes@138
   104
}
nkeynes@138
   105
nkeynes@138
   106
void gdrom_unmount_disc( ) 
nkeynes@138
   107
{
nkeynes@138
   108
    if( gdrom_disc != NULL ) {
nkeynes@138
   109
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   110
    }
nkeynes@138
   111
    gdrom_disc = NULL;
nkeynes@342
   112
nkeynes@138
   113
}
nkeynes@138
   114
nkeynes@464
   115
gdrom_disc_t gdrom_get_current_disc()
nkeynes@464
   116
{
nkeynes@464
   117
    return gdrom_disc;
nkeynes@464
   118
}
.