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 );
134 vmulist_update_config();
136 CALL_HOOKS( vmulist_change_hook, VMU_ADDED, g_list_index(vmu_list,entry) );
138 if( entry->vol != vol && entry->vol != NULL )
139 vmu_volume_destroy( entry->vol );
141 /* NOTE: at the moment this can't require a resort, but if we allow
142 * user-editable display names it will
145 entry->attach_count = 0;
150 static void vmulist_remove_entry( vmulist_entry_t entry )
152 int idx = g_list_index(vmu_list, entry);
153 vmu_list = g_list_remove( vmu_list, entry );
154 g_free( (char *)entry->filename );
156 vmulist_update_config();
157 CALL_HOOKS( vmulist_change_hook, VMU_REMOVED, idx );
160 static unsigned int vmulist_get_index( vmulist_entry_t entry )
162 return g_list_index( vmu_list, entry );
165 int vmulist_add_vmu( const gchar *filename, vmu_volume_t vol )
167 vmulist_entry_t entry = vmulist_add_entry( filename, vol );
168 return vmulist_get_index(entry);
171 void vmulist_remove_vmu( vmu_volume_t vol )
173 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
174 if( entry != NULL ) {
175 vmulist_remove_entry(entry);
179 const char *vmulist_get_name( unsigned int idx )
181 vmulist_entry_t entry = vmulist_get_entry_by_index(idx);
182 if( entry != NULL ) {
183 return vmulist_display_name(entry);
188 const char *vmulist_get_filename( unsigned int idx )
190 vmulist_entry_t entry = vmulist_get_entry_by_index(idx);
191 if( entry != NULL ) {
192 return entry->filename;
197 const char *vmulist_get_volume_name( vmu_volume_t vol )
199 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
200 if( entry != NULL ) {
201 return entry->filename;
206 vmu_volume_t vmulist_get_vmu( unsigned int idx )
208 vmulist_entry_t entry = vmulist_get_entry_by_index(idx);
209 if( entry != NULL ) {
210 if( entry->vol == NULL ) {
211 entry->vol = vmu_volume_load(entry->filename);
218 vmu_volume_t vmulist_get_vmu_by_name( const gchar *name )
220 vmulist_entry_t entry = vmulist_get_entry_by_name(name);
221 if( entry != NULL ) {
222 if( entry->vol == NULL ) {
223 entry->vol = vmu_volume_load(entry->filename);
230 vmu_volume_t vmulist_get_vmu_by_filename( const gchar *name )
232 vmulist_entry_t entry = vmulist_get_entry_by_filename(name);
233 if( entry != NULL ) {
234 if( entry->vol == NULL ) {
235 entry->vol = vmu_volume_load(entry->filename);
239 vmu_volume_t vol = vmu_volume_load( name );
240 vmulist_add_entry( name, vol );
245 int vmulist_get_index_by_filename( const gchar *name )
247 vmulist_entry_t entry = vmulist_get_entry_by_filename(name);
248 if( entry != NULL ) {
249 return g_list_index( vmu_list, entry );
255 int vmulist_create_vmu( const gchar *filename, gboolean create_only )
257 vmu_volume_t vol = vmu_volume_new_default(filename);
259 if( vmu_volume_save( filename, vol, create_only ) ) {
260 return vmulist_add_vmu( filename, vol );
262 vmu_volume_destroy(vol);
267 gboolean vmulist_attach_vmu( vmu_volume_t vol, const gchar *where )
269 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
270 if( entry == NULL ) {
273 entry->attach_count++;
277 void vmulist_detach_vmu( vmu_volume_t vol )
279 vmulist_entry_t entry = vmulist_get_entry_by_volume(vol);
280 if( entry != NULL && entry->attach_count > 0 ) {
281 entry->attach_count--;
285 unsigned int vmulist_get_size(void)
287 return g_list_length(vmu_list);
290 void vmulist_init( void )
292 GList *filenames = lxdream_get_global_config_list_value( CONFIG_VMU );
294 for( ptr = filenames; ptr != NULL; ptr = g_list_next(ptr) ) {
295 vmulist_add_entry( (gchar *)ptr->data, NULL );
298 g_list_free( filenames );
301 void vmulist_save_all( void )
304 for( it = vmu_list; it != NULL; it = g_list_next(it) ) {
305 vmulist_entry_t entry = ENTRY(it);
306 if( entry->vol != NULL && vmu_volume_is_dirty(entry->vol) ) {
307 vmu_volume_save(entry->filename, entry->vol, FALSE);
312 void vmulist_shutdown( void )
.