5 * GD-Rom access functions.
7 * Copyright (c) 2005 Nathan Keynes.
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.
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.
23 #include <glib/gutils.h>
24 #include "gdrom/ide.h"
25 #include "gdrom/gdrom.h"
26 #include "gdrom/gddriver.h"
27 #include "gdrom/packet.h"
30 extern gdrom_disc_t gdrom_disc;
32 DEFINE_HOOK( gdrom_disc_change_hook, gdrom_disc_change_hook_t )
34 static void gdrom_fire_disc_changed( gdrom_disc_t disc )
36 CALL_HOOKS( gdrom_disc_change_hook, disc, disc == NULL ? NULL : disc->name );
39 gdrom_image_class_t gdrom_image_classes[] = { &cdrom_device_class,
45 char *gdrom_mode_names[] = { "Mode 0", "Mode 1", "Mode 2", "Mode 2 Form 1", "Mode 2 Form 2", "Audio",
46 "Mode 2 semiraw", "XA Raw", "Non-XA Raw" };
47 uint32_t gdrom_sector_size[] = { 0, 2048, 2336, 2048, 2324, 2352, 2336, 2352, 2352 };
49 gdrom_disc_t gdrom_image_open( const gchar *inFilename )
51 const gchar *filename = inFilename;
52 const gchar *ext = strrchr(filename, '.');
53 gdrom_disc_t disc = NULL;
57 gdrom_image_class_t extclz = NULL;
59 // Check for a url-style filename.
60 char *lizard_lips = strstr( filename, "://" );
61 if( lizard_lips != NULL ) {
62 gchar *path = lizard_lips + 3;
63 int method_len = (lizard_lips-filename);
64 gchar method[method_len + 1];
65 memcpy( method, filename, method_len );
66 method[method_len] = '\0';
68 if( strcasecmp( method, "file" ) == 0 ) {
70 } else if( strcasecmp( method, "dvd" ) == 0 ||
71 strcasecmp( method, "cd" ) == 0 ||
72 strcasecmp( method, "cdrom" ) ) {
73 return cdrom_open_device( method, path );
75 ERROR( "Unrecognized URL method '%s' in filename '%s'", method, filename );
80 fd = open( filename, O_RDONLY | O_NONBLOCK );
90 ext++; /* Skip the '.' */
91 for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
92 if( gdrom_image_classes[i]->extension != NULL &&
93 strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
94 extclz = gdrom_image_classes[i];
95 if( extclz->is_valid_file(f) ) {
96 disc = extclz->open_image_file(filename, f);
105 /* Okay, fall back to magic */
106 gboolean recognized = FALSE;
107 for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
108 if( gdrom_image_classes[i] != extclz &&
109 gdrom_image_classes[i]->is_valid_file(f) ) {
111 disc = gdrom_image_classes[i]->open_image_file(filename, f);
121 void gdrom_mount_disc( gdrom_disc_t disc )
123 if( disc != gdrom_disc ) {
124 if( gdrom_disc != NULL ) {
125 gdrom_disc->close(gdrom_disc);
128 gdrom_image_dump_info( disc );
129 gdrom_fire_disc_changed( disc );
133 gboolean gdrom_mount_image( const gchar *filename )
135 gdrom_disc_t disc = gdrom_image_open(filename);
137 gdrom_mount_disc( disc );
143 void gdrom_unmount_disc( )
145 if( gdrom_disc != NULL ) {
146 gdrom_disc->close(gdrom_disc);
147 gdrom_fire_disc_changed(NULL);
152 gdrom_disc_t gdrom_get_current_disc()
157 const gchar *gdrom_get_current_disc_name()
159 if( gdrom_disc == NULL ) {
162 return gdrom_disc->name;
166 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
168 gchar *dirname = g_path_get_dirname(base_name);
169 gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
.