Search
lxdream.org :: lxdream/src/gdlist.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdlist.c
changeset 1109:700c5ab26a63
prev1097:d4807997e450
next1296:30ecee61f811
author nkeynes
date Fri Mar 02 23:49:10 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android WIP:
* Rename gui_jni.c to gui_android.c - now quite android specific.
* Implement generic EGL driver with very minimal Java wrapper
* Run emulation in separate thread, and implement simple queue for
inter-thread communication.
* Add menu/action-bar items for start + reset
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GD-Rom list manager - maintains the list of recently accessed images and
     5  * available devices for the UI + config. 
     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 <string.h>
    21 #include <stdlib.h>
    22 #include <glib/gstrfuncs.h>
    23 #include <libgen.h>
    24 #include "gettext.h"
    25 #include "gdrom/gdrom.h"
    26 #include "drivers/cdrom/drive.h"
    27 #include "gdlist.h"
    28 #include "lxdream.h"
    29 #include "config.h"
    31 #define MAX_RECENT_ITEMS 5
    33 #define FIRST_RECENT_INDEX (gdrom_device_count+2)
    35 DEFINE_HOOK(gdrom_list_change_hook, gdrom_list_change_hook_t);
    37 static GList *gdrom_device_list = NULL;
    38 static GList *gdrom_recent_list = NULL;
    39 static unsigned int gdrom_device_count = 0, gdrom_recent_count = 0;
    41 gint gdrom_list_find( const gchar *name )
    42 {
    43     gint posn = 0;
    44     GList *ptr;
    46     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    47         cdrom_drive_t device = (cdrom_drive_t)ptr->data;
    48         posn++;
    49         if( strcmp(device->name, name) == 0 ) {
    50             return posn;
    51         }
    52     }
    53     posn++;
    54     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    55         gchar *file = (gchar *)ptr->data;
    56         posn++;
    57         if( strcmp(file, name) == 0 ) {
    58             return posn;
    59         }
    60     }
    61     return -1;
    62 }
    64 /**
    65  * Update the recent list in the lxdream config (but does not save)  
    66  */
    67 void gdrom_list_update_config()
    68 {
    69     lxdream_set_global_config_list_value( CONFIG_RECENT, gdrom_recent_list ); 
    70 }
    73 void gdrom_list_add_recent_item( const gchar *name )
    74 {
    75     gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
    76     if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
    77         GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
    78         g_free( ptr->data );
    79         gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
    80     } else {
    81         gdrom_recent_count ++;
    82     }
    83     gdrom_list_update_config();
    84 }
    86 void gdrom_list_move_to_front( const gchar *name )
    87 {
    88     GList *ptr;
    89     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    90         gchar *file = (gchar *)ptr->data;
    91         if( strcmp(file, name) == 0 ) {
    92             gdrom_recent_list = g_list_delete_link( gdrom_recent_list, ptr );
    93             gdrom_recent_list = g_list_prepend( gdrom_recent_list, file );
    94             gdrom_list_update_config();
    95             return;
    96         }
    97     }
    98 }
   100 /**
   101  * Disc-changed callback from the GD-Rom driver. Updates the list accordingly.
   102  */
   103 gboolean gdrom_list_disc_changed( cdrom_disc_t disc, const gchar *disc_name, void *user_data )
   104 {
   105     gboolean list_changed = FALSE;
   106     int posn = 0;
   107     if( disc != NULL ) {
   108         posn = gdrom_list_find( disc_name );
   109         if( posn == -1 ) {
   110             gdrom_list_add_recent_item( disc_name );
   111             posn = FIRST_RECENT_INDEX;
   112             list_changed = TRUE;
   113         } else if( posn > FIRST_RECENT_INDEX ) {
   114             gdrom_list_move_to_front( disc_name );
   115             posn = FIRST_RECENT_INDEX;
   116             list_changed = TRUE;
   117         }
   118     }
   120     lxdream_set_global_config_path_value( CONFIG_GDROM, disc_name );
   121     lxdream_save_config();   
   123     CALL_HOOKS( gdrom_list_change_hook, list_changed, posn );
   124     return TRUE;
   125 }
   127 /**
   128  * Drives-changed callback from the host CD-Rom drivers. Probably not likely to
   129  * happen too often unless you're adding/removing external drives...
   130  */
   131 void gdrom_list_drives_changed( GList *device_list )
   132 {
   133 }
   135 /************ Public interface ***********/
   137 void gdrom_list_init()
   138 {
   139     gdrom_recent_list = lxdream_get_global_config_list_value( CONFIG_RECENT );
   140     register_gdrom_disc_change_hook( gdrom_list_disc_changed, NULL );
   141     cdrom_drive_scan();
   142     gdrom_device_list = cdrom_drive_get_list();
   143     gdrom_device_count = g_list_length(gdrom_device_list);
   144     gdrom_recent_count = g_list_length(gdrom_recent_list);
   146     // Run the hooks in case anyone registered before the list was initialized
   147     CALL_HOOKS( gdrom_list_change_hook, TRUE, gdrom_list_get_selection() );
   148 }
   150 gboolean gdrom_list_set_selection( int posn, ERROR *err )
   151 {
   152     if( posn == 0 ) { // Always 'Empty'
   153         gdrom_unmount_disc();
   154         return TRUE;
   155     }
   157     if( posn <= gdrom_device_count ) {
   158         cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
   159         return gdrom_mount_image(device->name, err);
   160     }
   162     posn -= FIRST_RECENT_INDEX;
   163     if( posn >= 0 && posn < gdrom_recent_count ) {
   164         gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
   165         return gdrom_mount_image(entry, err);
   166     }
   168     /* Should never happen */
   169     SET_ERROR( err, LX_ERR_BUG, "Invalid selection: This is probably a bug" );
   170     return FALSE;
   171 }
   173 gint gdrom_list_get_selection( )
   174 {
   175     const char *name = gdrom_get_current_disc_name();
   176     if( name == NULL ) {
   177         return 0;
   178     } else {
   179         return gdrom_list_find(name);
   180     }
   181 }
   183 int gdrom_list_size()
   184 {
   185     return gdrom_device_count + gdrom_recent_count + 2;
   186 }
   188 const gchar *gdrom_list_get_display_name( int posn )
   189 {
   190     if( posn == 0 ) {
   191         return _("Empty");
   192     }
   194     if( posn <= gdrom_device_count ) {
   195         cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
   196         return device->display_name;
   197     }
   199     if( posn == gdrom_device_count + 1) {
   200         return "";
   201     }
   203     if( posn < 0 || posn > gdrom_list_size() ) {
   204         return NULL;
   205     }
   207     gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   208     return basename(entry);
   209 }
   211 const gchar *gdrom_list_get_filename( int posn )
   212 {
   213     if( posn == 0 ) {
   214         return _("Empty");
   215     }
   217     if( posn <= gdrom_device_count ) {
   218         cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
   219         return device->name;
   220     }
   222     if( posn == gdrom_device_count + 1) {
   223         return "";
   224     }
   226     if( posn < 0 || posn > gdrom_list_size() ) {
   227         return NULL;
   228     }
   230     return g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   231 }
.