filename | src/gdrom/gdrom.c |
changeset | 691:ad3356543392 |
prev | 678:35eb00945316 |
next | 695:2bb59940ff35 |
author | nkeynes |
date | Thu Jun 19 04:40:37 2008 +0000 (14 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. |
view | annotate | diff | log | raw |
2 /**
3 * $Id$
4 *
5 * GD-Rom access functions.
6 *
7 * Copyright (c) 2005 Nathan Keynes.
8 *
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.
13 *
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.
18 */
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <errno.h>
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"
28 #include "dream.h"
30 extern gdrom_disc_t gdrom_disc;
32 DEFINE_HOOK( gdrom_disc_change_hook, gdrom_disc_change_hook_t )
34 gdrom_fire_disc_changed( gdrom_disc_t disc )
35 {
36 CALL_HOOKS( gdrom_disc_change_hook, disc, disc == NULL ? NULL : disc->name );
37 }
39 gdrom_image_class_t gdrom_image_classes[] = { &cdrom_device_class,
40 &nrg_image_class,
41 &cdi_image_class,
42 &gdi_image_class,
43 NULL };
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 *filename )
50 {
51 const gchar *ext = strrchr(filename, '.');
52 gdrom_disc_t disc = NULL;
54 int fd = open( filename, O_RDONLY | O_NONBLOCK );
55 FILE *f;
56 int i;
57 gdrom_image_class_t extclz = NULL;
59 if( fd == -1 ) {
60 return NULL;
61 }
63 f = fdopen(fd, "ro");
66 /* try extensions */
67 if( ext != NULL ) {
68 ext++; /* Skip the '.' */
69 for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
70 if( gdrom_image_classes[i]->extension != NULL &&
71 strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
72 extclz = gdrom_image_classes[i];
73 if( extclz->is_valid_file(f) ) {
74 disc = extclz->open_image_file(filename, f);
75 if( disc != NULL )
76 return disc;
77 }
78 break;
79 }
80 }
81 }
83 /* Okay, fall back to magic */
84 gboolean recognized = FALSE;
85 for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
86 if( gdrom_image_classes[i] != extclz &&
87 gdrom_image_classes[i]->is_valid_file(f) ) {
88 recognized = TRUE;
89 disc = gdrom_image_classes[i]->open_image_file(filename, f);
90 if( disc != NULL )
91 return disc;
92 }
93 }
95 fclose(f);
96 return NULL;
97 }
99 void gdrom_mount_disc( gdrom_disc_t disc )
100 {
101 if( disc != gdrom_disc ) {
102 gdrom_unmount_disc();
103 gdrom_disc = disc;
104 gdrom_image_dump_info( disc );
105 gdrom_fire_disc_changed( disc );
106 }
107 }
109 gboolean gdrom_mount_image( const gchar *filename )
110 {
111 gdrom_disc_t disc = gdrom_image_open(filename);
112 if( disc != NULL ) {
113 gdrom_mount_disc( disc );
114 return TRUE;
115 }
116 return FALSE;
117 }
119 void gdrom_unmount_disc( )
120 {
121 if( gdrom_disc != NULL ) {
122 gdrom_disc->close(gdrom_disc);
123 gdrom_fire_disc_changed(NULL);
124 gdrom_disc = NULL;
125 }
126 }
128 gdrom_disc_t gdrom_get_current_disc()
129 {
130 return gdrom_disc;
131 }
133 const gchar *gdrom_get_current_disc_name()
134 {
135 if( gdrom_disc == NULL ) {
136 return NULL;
137 } else {
138 return gdrom_disc->name;
139 }
140 }
142 gchar *gdrom_get_relative_filename( const gchar *base_name, const gchar *rel_name )
143 {
144 gchar *dirname = g_path_get_dirname(base_name);
145 gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, rel_name );
146 g_free(dirname);
147 return pathname;
148 }
.