Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 644:ccae4bfa5f82
prev613:c2dd87f947b2
next678:35eb00945316
author nkeynes
date Sun Mar 02 11:38:08 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Bug #59: Add provisional support for all CD read modes (not validated)
file annotate diff log raw
nkeynes@342
     1
nkeynes@138
     2
/**
nkeynes@561
     3
 * $Id$
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@644
    36
char *gdrom_mode_names[] = { "Mode 0", "Mode 1", "Mode 2", "Mode 2 Form 1", "Mode 2 Form 2", "Audio", 
nkeynes@644
    37
			     "Mode 2 semiraw", "XA Raw", "Non-XA Raw" };
nkeynes@644
    38
uint32_t gdrom_sector_size[] = { 0, 2048, 2336, 2048, 2324, 2352, 2336, 2352, 2352 };
nkeynes@138
    39
nkeynes@138
    40
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    41
{
nkeynes@168
    42
    const gchar *ext = strrchr(filename, '.');
nkeynes@168
    43
    gdrom_disc_t disc = NULL;
nkeynes@237
    44
nkeynes@237
    45
    int fd = open( filename, O_RDONLY | O_NONBLOCK );
nkeynes@237
    46
    FILE *f;
nkeynes@422
    47
    int i;
nkeynes@168
    48
    gdrom_image_class_t extclz = NULL;
nkeynes@168
    49
nkeynes@237
    50
    if( fd == -1 ) {
nkeynes@168
    51
	return NULL;
nkeynes@168
    52
    }
nkeynes@168
    53
nkeynes@237
    54
    f = fdopen(fd, "ro");
nkeynes@237
    55
nkeynes@237
    56
nkeynes@168
    57
    /* try extensions */
nkeynes@168
    58
    if( ext != NULL ) {
nkeynes@168
    59
	ext++; /* Skip the '.' */
nkeynes@168
    60
	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@237
    61
	    if( gdrom_image_classes[i]->extension != NULL &&
nkeynes@237
    62
		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
nkeynes@168
    63
		extclz = gdrom_image_classes[i];
nkeynes@168
    64
		if( extclz->is_valid_file(f) ) {
nkeynes@168
    65
		    disc = extclz->open_image_file(filename, f);
nkeynes@168
    66
		    if( disc != NULL )
nkeynes@168
    67
			return disc;
nkeynes@168
    68
		}
nkeynes@168
    69
		break;
nkeynes@168
    70
	    }
nkeynes@168
    71
	}
nkeynes@168
    72
    }
nkeynes@168
    73
nkeynes@168
    74
    /* Okay, fall back to magic */
nkeynes@237
    75
    gboolean recognized = FALSE;
nkeynes@168
    76
    for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@168
    77
	if( gdrom_image_classes[i] != extclz &&
nkeynes@168
    78
	    gdrom_image_classes[i]->is_valid_file(f) ) {
nkeynes@237
    79
	    recognized = TRUE;
nkeynes@168
    80
	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
nkeynes@168
    81
	    if( disc != NULL )
nkeynes@168
    82
		return disc;
nkeynes@168
    83
	}
nkeynes@168
    84
    }
nkeynes@168
    85
nkeynes@168
    86
    fclose(f);
nkeynes@168
    87
    return NULL;
nkeynes@138
    88
}
nkeynes@138
    89
nkeynes@138
    90
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
    91
{
nkeynes@138
    92
    gdrom_unmount_disc();
nkeynes@138
    93
    gdrom_disc = disc;
nkeynes@342
    94
    gdrom_image_dump_info( disc );
nkeynes@138
    95
}
nkeynes@138
    96
nkeynes@433
    97
gboolean gdrom_mount_image( const gchar *filename )
nkeynes@138
    98
{
nkeynes@138
    99
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@433
   100
    if( disc != NULL ) {
nkeynes@138
   101
	gdrom_mount_disc( disc );
nkeynes@433
   102
	return TRUE;
nkeynes@433
   103
    }
nkeynes@433
   104
    return FALSE;
nkeynes@138
   105
}
nkeynes@138
   106
nkeynes@138
   107
void gdrom_unmount_disc( ) 
nkeynes@138
   108
{
nkeynes@138
   109
    if( gdrom_disc != NULL ) {
nkeynes@138
   110
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   111
    }
nkeynes@138
   112
    gdrom_disc = NULL;
nkeynes@342
   113
nkeynes@138
   114
}
nkeynes@138
   115
nkeynes@464
   116
gdrom_disc_t gdrom_get_current_disc()
nkeynes@464
   117
{
nkeynes@464
   118
    return gdrom_disc;
nkeynes@464
   119
}
nkeynes@613
   120
nkeynes@613
   121
gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
nkeynes@613
   122
{
nkeynes@613
   123
    gchar *dirname = g_path_get_dirname(base_name);
nkeynes@613
   124
    gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
nkeynes@613
   125
    g_free(dirname);
nkeynes@613
   126
    return pathname;
nkeynes@613
   127
}
.