Search
lxdream.org :: lxdream/src/gdlist.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdlist.c
changeset 1097:d4807997e450
prev1036:af7b0c5905dd
next1109:700c5ab26a63
author nkeynes
date Sun Jan 31 18:35:06 2010 +1000 (12 years ago)
permissions -rw-r--r--
last change Refactor CDROM host support
- Completely separate GDROM hardware (in gdrom/gdrom.c) from generic CDROM
support (now in drivers/cdrom)
- Add concept of 'sector sources' that can be mixed and matched to create
cdrom discs (makes support of arbitrary disc types much simpler)
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 )
   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);
   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);
   166     }
   168     return FALSE;
   169 }
   171 gint gdrom_list_get_selection( )
   172 {
   173     const char *name = gdrom_get_current_disc_name();
   174     if( name == NULL ) {
   175         return 0;
   176     } else {
   177         return gdrom_list_find(name);
   178     }
   179 }
   181 int gdrom_list_size()
   182 {
   183     return gdrom_device_count + gdrom_recent_count + 2;
   184 }
   186 const gchar *gdrom_list_get_display_name( int posn )
   187 {
   188     if( posn == 0 ) {
   189         return _("Empty");
   190     }
   192     if( posn <= gdrom_device_count ) {
   193         cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
   194         return device->display_name;
   195     }
   197     if( posn == gdrom_device_count + 1) {
   198         return "";
   199     }
   201     if( posn < 0 || posn > gdrom_list_size() ) {
   202         return NULL;
   203     }
   205     gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   206     return basename(entry);
   207 }
   209 const gchar *gdrom_list_get_filename( int posn )
   210 {
   211     if( posn == 0 ) {
   212         return _("Empty");
   213     }
   215     if( posn <= gdrom_device_count ) {
   216         cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
   217         return device->name;
   218     }
   220     if( posn == gdrom_device_count + 1) {
   221         return "";
   222     }
   224     if( posn < 0 || posn > gdrom_list_size() ) {
   225         return NULL;
   226     }
   228     return g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   229 }
.