Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 691:ad3356543392
prev678:35eb00945316
next695:2bb59940ff35
author nkeynes
date Thu Jun 19 04:40:37 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Refactor the gd-rom list management out of the GUI (devices, recent files, etc).
Add gd-rom list to the cocoa UI.
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@678
    23
#include <glib/gutils.h>
nkeynes@138
    24
#include "gdrom/ide.h"
nkeynes@138
    25
#include "gdrom/gdrom.h"
nkeynes@678
    26
#include "gdrom/gddriver.h"
nkeynes@143
    27
#include "gdrom/packet.h"
nkeynes@138
    28
#include "dream.h"
nkeynes@138
    29
nkeynes@342
    30
extern gdrom_disc_t gdrom_disc;
nkeynes@138
    31
nkeynes@678
    32
DEFINE_HOOK( gdrom_disc_change_hook, gdrom_disc_change_hook_t )
nkeynes@678
    33
nkeynes@678
    34
gdrom_fire_disc_changed( gdrom_disc_t disc )
nkeynes@678
    35
{
nkeynes@678
    36
    CALL_HOOKS( gdrom_disc_change_hook, disc, disc == NULL ? NULL : disc->name );
nkeynes@678
    37
}
nkeynes@678
    38
nkeynes@492
    39
gdrom_image_class_t gdrom_image_classes[] = { &cdrom_device_class, 
nkeynes@492
    40
					      &nrg_image_class, 
nkeynes@492
    41
					      &cdi_image_class, 
nkeynes@492
    42
					      &gdi_image_class, 
nkeynes@492
    43
					      NULL };
nkeynes@138
    44
nkeynes@644
    45
char *gdrom_mode_names[] = { "Mode 0", "Mode 1", "Mode 2", "Mode 2 Form 1", "Mode 2 Form 2", "Audio", 
nkeynes@644
    46
			     "Mode 2 semiraw", "XA Raw", "Non-XA Raw" };
nkeynes@644
    47
uint32_t gdrom_sector_size[] = { 0, 2048, 2336, 2048, 2324, 2352, 2336, 2352, 2352 };
nkeynes@138
    48
nkeynes@138
    49
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    50
{
nkeynes@168
    51
    const gchar *ext = strrchr(filename, '.');
nkeynes@168
    52
    gdrom_disc_t disc = NULL;
nkeynes@237
    53
nkeynes@237
    54
    int fd = open( filename, O_RDONLY | O_NONBLOCK );
nkeynes@237
    55
    FILE *f;
nkeynes@422
    56
    int i;
nkeynes@168
    57
    gdrom_image_class_t extclz = NULL;
nkeynes@168
    58
nkeynes@237
    59
    if( fd == -1 ) {
nkeynes@168
    60
	return NULL;
nkeynes@168
    61
    }
nkeynes@168
    62
nkeynes@237
    63
    f = fdopen(fd, "ro");
nkeynes@237
    64
nkeynes@237
    65
nkeynes@168
    66
    /* try extensions */
nkeynes@168
    67
    if( ext != NULL ) {
nkeynes@168
    68
	ext++; /* Skip the '.' */
nkeynes@168
    69
	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@237
    70
	    if( gdrom_image_classes[i]->extension != NULL &&
nkeynes@237
    71
		strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
nkeynes@168
    72
		extclz = gdrom_image_classes[i];
nkeynes@168
    73
		if( extclz->is_valid_file(f) ) {
nkeynes@168
    74
		    disc = extclz->open_image_file(filename, f);
nkeynes@168
    75
		    if( disc != NULL )
nkeynes@168
    76
			return disc;
nkeynes@168
    77
		}
nkeynes@168
    78
		break;
nkeynes@168
    79
	    }
nkeynes@168
    80
	}
nkeynes@168
    81
    }
nkeynes@168
    82
nkeynes@168
    83
    /* Okay, fall back to magic */
nkeynes@237
    84
    gboolean recognized = FALSE;
nkeynes@168
    85
    for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@168
    86
	if( gdrom_image_classes[i] != extclz &&
nkeynes@168
    87
	    gdrom_image_classes[i]->is_valid_file(f) ) {
nkeynes@237
    88
	    recognized = TRUE;
nkeynes@168
    89
	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
nkeynes@168
    90
	    if( disc != NULL )
nkeynes@168
    91
		return disc;
nkeynes@168
    92
	}
nkeynes@168
    93
    }
nkeynes@168
    94
nkeynes@168
    95
    fclose(f);
nkeynes@168
    96
    return NULL;
nkeynes@138
    97
}
nkeynes@138
    98
nkeynes@138
    99
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
   100
{
nkeynes@678
   101
    if( disc != gdrom_disc ) {
nkeynes@678
   102
        gdrom_unmount_disc();
nkeynes@678
   103
        gdrom_disc = disc;
nkeynes@678
   104
        gdrom_image_dump_info( disc );
nkeynes@678
   105
        gdrom_fire_disc_changed( disc );
nkeynes@678
   106
    }
nkeynes@138
   107
}
nkeynes@138
   108
nkeynes@433
   109
gboolean gdrom_mount_image( const gchar *filename )
nkeynes@138
   110
{
nkeynes@138
   111
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@433
   112
    if( disc != NULL ) {
nkeynes@678
   113
        gdrom_mount_disc( disc );
nkeynes@678
   114
        return TRUE;
nkeynes@433
   115
    }
nkeynes@433
   116
    return FALSE;
nkeynes@138
   117
}
nkeynes@138
   118
nkeynes@138
   119
void gdrom_unmount_disc( ) 
nkeynes@138
   120
{
nkeynes@138
   121
    if( gdrom_disc != NULL ) {
nkeynes@678
   122
        gdrom_disc->close(gdrom_disc);
nkeynes@678
   123
        gdrom_fire_disc_changed(NULL);
nkeynes@691
   124
        gdrom_disc = NULL;
nkeynes@138
   125
    }
nkeynes@138
   126
}
nkeynes@138
   127
nkeynes@464
   128
gdrom_disc_t gdrom_get_current_disc()
nkeynes@464
   129
{
nkeynes@464
   130
    return gdrom_disc;
nkeynes@464
   131
}
nkeynes@613
   132
nkeynes@678
   133
const gchar *gdrom_get_current_disc_name()
nkeynes@678
   134
{
nkeynes@678
   135
    if( gdrom_disc == NULL ) {
nkeynes@678
   136
        return NULL;
nkeynes@678
   137
    } else {
nkeynes@678
   138
        return gdrom_disc->name;
nkeynes@678
   139
    }
nkeynes@678
   140
}
nkeynes@678
   141
nkeynes@613
   142
gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
nkeynes@613
   143
{
nkeynes@613
   144
    gchar *dirname = g_path_get_dirname(base_name);
nkeynes@613
   145
    gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
nkeynes@613
   146
    g_free(dirname);
nkeynes@613
   147
    return pathname;
nkeynes@613
   148
}
.