4 * VMU management - maintains a list of all known VMUs
6 * Copyright (c) 2009 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 #include <glib/glist.h>
22 #include <glib/gstrfuncs.h>
26 DEFINE_HOOK(vmulist_change_hook, vmulist_change_hook_t);
28 typedef struct vmulist_entry {
29 const gchar *filename;
35 * Doubly-linked list of vmulist_entry_t maintained in sorted order by display name.
36 * Could be augmented with a hashtable if it gets too long
38 static GList *vmu_list;
40 #define ENTRY(it) ((vmulist_entry_t)(it)->data)
41 #define VOLUME(it) (ENTRY(it)->vol)
42 #define DISPLAY_NAME(it) vmulist_display_name(ENTRY(it))
44 static const char *vmulist_display_name(vmulist_entry_t ent)
46 if( ent->filename == NULL ) {
49 const char *s = strrchr(ent->filename, '/' );
50 if( s == NULL || *(s+1) == '\0' ) {
57 static void vmulist_update_config( void )
59 GList *temp = NULL, *it;
61 for( it = vmu_list; it != NULL; it = g_list_next(it) ) {
62 vmulist_entry_t entry = ENTRY(it);
63 temp = g_list_append( temp, (char *)entry->filename );
65 lxdream_set_global_config_list_value( CONFIG_VMU, temp );
69 static vmulist_entry_t vmulist_get_entry_by_name( const gchar *name )
72 for( it = vmu_list; it != NULL; it = g_list_next(it) ) {
73 const gchar *vmu_name = DISPLAY_NAME(it);
74 if( name == NULL ? vmu_name == NULL : vmu_name != NULL && strcmp( vmu_name, name ) == 0 ) {
78 return NULL; // not found
81 static vmulist_entry_t vmulist_get_entry_by_filename( const gchar *name )
84 for( it = vmu_list; it != NULL; it = g_list_next(it) ) {
85 const gchar *vmu_name = ENTRY(it)->filename;
86 if( name == NULL ? vmu_name == NULL : vmu_name != NULL && strcmp( vmu_name, name ) == 0 ) {
90 return NULL; // not found
93 static vmulist_entry_t vmulist_get_entry_by_volume( vmu_volume_t vol )
96 for( it = vmu_list; it != NULL; it = g_list_next(it) ) {
97 if( VOLUME(it) == vol ) {
101 return NULL; // not found
104 static vmulist_entry_t vmulist_get_entry_by_index( unsigned int index )
106 return (vmulist_entry_t)g_list_nth_data(vmu_list,index);
109 static gint vmulist_display_name_compare( gconstpointer a, gconstpointer b )
111 const char *aname = vmulist_display_name((vmulist_entry_t)a);
112 const char *bname = vmulist_display_name((vmulist_entry_t)b);
119 return strcmp(aname,bname);
123 * Add a new entry into the list, maintaining the sorted order.
124 * If the filename is already in the list, it is updated instead.
126 static vmulist_entry_t vmulist_add_entry( const gchar *filename, vmu_volume_t vol )
128 vmulist_entry_t entry = vmulist_get_entry_by_filename(filename);
129 if( entry == NULL ) {
130 entry = g_malloc( sizeof(struct vmulist_entry) );
131 entry->filename = g_strdup(filename);
133 vmu_list = g_list_insert_sorted(vmu_list, entry, vmulist_display_name_compare );
135 if( entry->vol != vol && entry->vol != NULL )
136 vmu_volume_destroy( entry->vol );
138 /* NOTE: at the moment this can't require a resort, but if we allow
139 * user-editable display names it will
142 entry->attach_count = 0;
144 vmulist_update_config();
145 CALL_HOOKS( vmulist_change_hook, VMU_ADDED, g_list_index(vmu_list,entry) );
149 static void vmulist_remove_entry( vmulist_entry_t entry )
151 int idx = g_list_index(vmu_list, entry);
152 vmu_list = g_list_remove( vmu_list, entry );
153 g_free( (char *)entry->filename );
155 vmulist_update_config();
156 CALL_HOOKS( vmulist_change_hook, VMU_REMOVED, idx );
159 static unsigned int vmulist_get_index( vmulist_entry_t entry )
161 return g_list_index( vmu_list, entry );
164 int vmulist_add_vmu( const gchar *filename, vmu_volume_t vol )
166 vmulist_entry_t entry = vmulist_add_entry( filename, vol );
167 return vmulist_get_index(entry);
170 void vmulist_remove_vmu( vmu_volume_t vol )
172 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
173 if( entry != NULL ) {
174 vmulist_remove_entry(entry);
178 const char *vmulist_get_name( unsigned int idx )
180 vmulist_entry_t entry = vmulist_get_entry_by_index(idx);
181 if( entry != NULL ) {
182 return vmulist_display_name(entry);
187 const char *vmulist_get_filename( unsigned int idx )
189 vmulist_entry_t entry = vmulist_get_entry_by_index(idx);
190 if( entry != NULL ) {
191 return entry->filename;
196 const char *vmulist_get_volume_name( vmu_volume_t vol )
198 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
199 if( entry != NULL ) {
200 return entry->filename;
205 vmu_volume_t vmulist_get_vmu( unsigned int idx )
207 vmulist_entry_t entry = vmulist_get_entry_by_index(idx);
208 if( entry != NULL ) {
209 if( entry->vol == NULL ) {
210 entry->vol = vmu_volume_load(entry->filename);
217 vmu_volume_t vmulist_get_vmu_by_name( const gchar *name )
219 vmulist_entry_t entry = vmulist_get_entry_by_name(name);
220 if( entry != NULL ) {
221 if( entry->vol == NULL ) {
222 entry->vol = vmu_volume_load(entry->filename);
229 vmu_volume_t vmulist_get_vmu_by_filename( const gchar *name )
231 vmulist_entry_t entry = vmulist_get_entry_by_filename(name);
232 if( entry != NULL ) {
233 if( entry->vol == NULL ) {
234 entry->vol = vmu_volume_load(entry->filename);
238 vmu_volume_t vol = vmu_volume_load( name );
239 vmulist_add_entry( name, vol );
244 int vmulist_get_index_by_filename( const gchar *name )
246 vmulist_entry_t entry = vmulist_get_entry_by_filename(name);
247 if( entry != NULL ) {
248 return g_list_index( vmu_list, entry );
254 int vmulist_create_vmu( const gchar *filename, gboolean create_only )
256 vmu_volume_t vol = vmu_volume_new_default(filename);
258 if( vmu_volume_save( filename, vol, create_only ) ) {
259 return vmulist_add_vmu( filename, vol );
261 vmu_volume_destroy(vol);
266 gboolean vmulist_attach_vmu( vmu_volume_t vol, const gchar *where )
268 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
269 if( entry == NULL ) {
272 entry->attach_count++;
276 void vmulist_detach_vmu( vmu_volume_t vol )
278 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
279 if( entry != NULL && entry->attach_count > 0 ) {
280 entry->attach_count--;
284 unsigned int vmulist_get_size(void)
286 return g_list_length(vmu_list);
289 void vmulist_init( void )
291 GList *filenames = lxdream_get_global_config_list_value( CONFIG_VMU );
293 for( ptr = filenames; ptr != NULL; ptr = g_list_next(ptr) ) {
294 vmulist_add_entry( (gchar *)ptr->data, NULL );
297 g_list_free( filenames );
300 void vmulist_save_all( void )
303 for( it = vmu_list; it != NULL; it = g_list_next(it) ) {
304 vmulist_entry_t entry = ENTRY(it);
305 if( entry->vol != NULL && vmu_volume_is_dirty(entry->vol) ) {
306 vmu_volume_save(entry->filename, entry->vol, FALSE);
311 void vmulist_shutdown( void )
.