Search
lxdream.org :: lxdream/src/gtkui/gdrom_menu.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gdrom_menu.c
changeset 678:35eb00945316
prev561:533f6b478071
next691:ad3356543392
author nkeynes
date Thu May 29 11:00:26 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Split gdrom.h into public and private gddriver.h
Reorganize gdrom mount to use a disc change hook
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Creates and manages the GD-Rom attachment menu.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <assert.h>
    20 #include <libgen.h>
    21 #include <glib/gi18n.h>
    23 #include "dream.h"
    24 #include "dreamcast.h"
    25 #include "config.h"
    26 #include "gdrom/gdrom.h"
    27 #include "gtkui/gtkui.h"
    29 #define MAX_RECENT_ITEMS 5
    31 static GList *gdrom_menu_list = NULL;
    32 static gboolean gdrom_menu_adjusting = FALSE;
    33 static GList *gdrom_device_list = NULL;
    34 static GList *gdrom_recent_list = NULL;
    36 void gdrom_menu_rebuild_all();
    39 gint gdrom_menu_find_item( const gchar *name )
    40 {
    41     gint posn = 0;
    42     GList *ptr;
    44     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    45 	gchar *device = (gchar *)ptr->data;
    46 	posn++;
    47 	if( strcmp(device, name) == 0 ) {
    48 	    return posn;
    49 	}
    50     }
    51     posn++;
    52     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    53 	gchar *file = (gchar *)ptr->data;
    54 	posn++;
    55 	if( strcmp(file, name) == 0 ) {
    56 	    return posn;
    57 	}
    58     }
    59     return -1;
    60 }
    62 gint gdrom_menu_add_recent_item( const gchar *name )
    63 {
    64     gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
    65     if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
    66 	GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
    67 	g_free( ptr->data );
    68 	gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
    69     }
    71     GList *ptr;
    72     int size = 0;
    73     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    74 	size += strlen( (gchar *)ptr->data ) + 1;
    75     }
    76     char buf[size];
    77     strcpy( buf, (gchar *)gdrom_recent_list->data );
    78     ptr = g_list_next(gdrom_recent_list);
    79     while( ptr != NULL ) {
    80 	strcat( buf, ":" );
    81 	strcat( buf, (gchar *)ptr->data );
    82 	ptr = g_list_next(ptr);
    83     }
    84     lxdream_set_global_config_value( CONFIG_RECENT, buf );
    85     lxdream_save_config();
    87     return g_list_length( gdrom_device_list ) + 2; // menu posn of new item
    88 }
    90 void gdrom_menu_update_all()
    91 {
    92     const gchar *disc_name = gdrom_get_current_disc_name();
    93     gint posn = 0;
    94     GList *ptr;
    96     gdrom_menu_adjusting = TRUE;
    98     if( disc_name != NULL ) {
    99         posn = gdrom_menu_find_item( disc_name );
   100         if( posn == -1 ) {
   101             posn = gdrom_menu_add_recent_item( disc_name );
   102             gdrom_menu_rebuild_all();
   103         }
   104     }
   106     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   107         GtkWidget *menu = GTK_WIDGET(ptr->data);
   108         GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   109         GList *item = g_list_nth( children, posn );
   110         assert( item != NULL );
   111         gtk_check_menu_item_set_active( GTK_CHECK_MENU_ITEM(item->data), TRUE );
   112         g_list_free(children);
   113     }    
   115     gdrom_menu_adjusting = FALSE;
   116 }
   118 void gdrom_menu_empty_callback( GtkWidget *widget, gpointer user_data )
   119 {
   120     if( !gdrom_menu_adjusting ) {
   121         gdrom_unmount_disc();
   122         lxdream_set_global_config_value( CONFIG_GDROM, NULL );
   123         lxdream_save_config();
   124     }
   125 }
   127 void gdrom_menu_disc_changed( gdrom_disc_t disc, const gchar *name, void *user_data )
   128 {
   129     if( !gdrom_menu_adjusting ) {
   130         gdrom_menu_update_all();
   131     }
   132 }
   134 gboolean gdrom_menu_open_file( const char *filename )
   135 {
   136     gboolean result = FALSE;
   137     if( filename != NULL ) {
   138         result = gdrom_mount_image(filename);
   139     }
   140     if( result ) {
   141         lxdream_set_global_config_value( CONFIG_GDROM, filename );
   142         lxdream_save_config();
   143     }
   144     return result;
   145 }
   147 void gdrom_menu_open_image_callback( GtkWidget *widget, gpointer user_data )
   148 {
   149     if( !gdrom_menu_adjusting ) {
   150         const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   151         open_file_dialog( _("Open..."), gdrom_menu_open_file, NULL, NULL, dir );
   152     }
   153 }
   156 void gdrom_menu_open_specified_callback( GtkWidget *widget, gpointer user_data )
   157 {
   158     if( !gdrom_menu_adjusting ) {
   159 	gdrom_menu_open_file( (gchar *)user_data );
   160     }
   161 }
   163 void gdrom_menu_build( GtkWidget *menu ) 
   164 {
   165     GSList *group = NULL;
   166     GtkWidget *empty = gtk_radio_menu_item_new_with_label( group, _("Empty") );
   167     group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(empty) );
   168     g_signal_connect_after( empty, "activate", G_CALLBACK(gdrom_menu_empty_callback), NULL );
   169     gtk_menu_shell_append( GTK_MENU_SHELL(menu), empty );
   171     GList *ptr;
   172     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   173 	gchar *name = (gchar *)ptr->data;
   174 	GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name);
   175 	gtk_widget_set_name( item, name );
   176 	group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   177 	g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   178 			  name );
   179 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   180     }
   182     if( gdrom_recent_list != NULL ) {
   183 	gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   184 	for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   185 	    gchar *path = (gchar *)ptr->data;
   186 	    gchar *name = basename(path);
   187 	    GtkWidget *item = gtk_radio_menu_item_new_with_label( group, name );
   188 	    gtk_widget_set_name( item, path );
   189 	    group = gtk_radio_menu_item_get_group( GTK_RADIO_MENU_ITEM(item) );
   190 	    g_signal_connect_after( item, "activate", G_CALLBACK(gdrom_menu_open_specified_callback),
   191 				    path );
   192 	    gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
   194 	}
   195     }
   196     gtk_menu_shell_append( GTK_MENU_SHELL(menu), gtk_separator_menu_item_new() );
   197     GtkWidget *open = gtk_image_menu_item_new_with_label( _("Open image file...") );
   198     g_signal_connect_after( open, "activate", G_CALLBACK(gdrom_menu_open_image_callback), NULL );
   199     gtk_menu_shell_append( GTK_MENU_SHELL(menu), open );
   200     gtk_widget_show_all(menu);
   201 }
   203 GtkWidget *gdrom_menu_new()
   204 {
   205     GtkWidget *menu = gtk_menu_new();
   206     gtk_menu_set_title( GTK_MENU(menu), _("GD-Rom Settings") );
   208     gdrom_menu_build(menu);
   210     gdrom_menu_list = g_list_append(gdrom_menu_list, menu);
   211     gtk_widget_show_all(menu);
   212     gdrom_menu_update_all();
   213     return menu;
   214 }
   216 void gdrom_menu_rebuild_all()
   217 {
   218     GList *ptr;
   220     for( ptr = gdrom_menu_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   221 	GtkWidget *menu = GTK_WIDGET(ptr->data);
   222 	GList *children = gtk_container_get_children( GTK_CONTAINER(menu) );
   223 	GList *listptr;
   224 	for( listptr = children; listptr != NULL; listptr = g_list_next(listptr) ) {
   225 	    gtk_widget_destroy( GTK_WIDGET(listptr->data) );
   226 	}
   227 	g_list_free(children);
   228 	gdrom_menu_build(menu);
   229     }
   230     gdrom_menu_update_all();
   231 }
   233 void gdrom_menu_init()
   234 {
   235     const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
   236     register_gdrom_disc_change_hook( gdrom_menu_disc_changed, NULL );
   237     gdrom_device_list = gdrom_get_native_devices();
   238     if( recent != NULL ) {
   239 	gchar **list = g_strsplit(recent, ":", 5);
   240 	int i;
   241 	for( i=0; list[i] != NULL; i++ ) {
   242 	    gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
   243 	}
   244 	g_strfreev(list);
   245     }
   246 }
.