Search
lxdream.org :: lxdream/src/gui/ctrl_dlg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/ctrl_dlg.c
changeset 508:ccd2c10edfe6
prev480:d28c2992f5ee
author nkeynes
date Tue Nov 20 08:32:05 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Drop default debugger window size down a notch
view annotate diff log raw
     1 /**
     2  * $Id: ctrl_dlg.c,v 1.6 2007-11-10 04:45:29 nkeynes Exp $
     3  *
     4  * Define the main (emu) GTK window, along with its menubars,
     5  * toolbars, etc.
     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 <assert.h>
    21 #include <gtk/gtk.h>
    22 #include <gdk/gdkkeysyms.h>
    24 #include "dream.h"
    25 #include "gui/gtkui.h"
    26 #include "maple/maple.h"
    28 #define MAX_DEVICES 4
    30 static void controller_device_configure(maple_device_t device);
    32 struct maple_config_class {
    33     const char *name;
    34     void (*config_func)(maple_device_t device);
    35 };
    37 typedef struct maple_slot_data {
    38     maple_device_t old_device;
    39     maple_device_t new_device;
    40     GtkWidget *button;
    41     GtkWidget *combo;
    42 } *maple_slot_data_t;
    44 static struct maple_config_class maple_device_config[] = {
    45     { "Sega Controller", controller_device_configure },
    46     { NULL, NULL } };
    48 static struct maple_slot_data maple_data[MAX_DEVICES];
    50 static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
    51 {
    52     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    53     if( !keypress_mode ) {
    54 	gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
    55 	g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
    56     }
    57     return FALSE;
    58 }
    60 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
    61 {
    62     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    63     if( keypress_mode ) {
    64 	if( event->keyval == GDK_Escape ) {
    65 	    gtk_entry_set_text( GTK_ENTRY(widget), "" );
    66 	    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
    67 	    return TRUE;
    68 	}
    69 	GdkKeymap *keymap = gdk_keymap_get_default();
    70 	guint keyval;
    72 	gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    73 					     NULL, NULL, NULL );
    74 	gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
    75 	g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
    76 	return TRUE;
    77     } else {
    78 	switch( event->keyval ) {
    79 	case GDK_Return:
    80 	case GDK_KP_Enter:
    81 	    gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
    82 	    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
    83 	    return TRUE;
    84 	case GDK_BackSpace:
    85 	case GDK_Delete:
    86 	    gtk_entry_set_text( GTK_ENTRY(widget), "" );
    87 	    return TRUE;
    88 	}
    89 	return FALSE;
    90     }
    92 }
    94 static void controller_config_done( GtkWidget *panel, gboolean isOK )
    95 {
    96     if( isOK ) {
    97 	maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
    98 	lxdream_config_entry_t conf = device->get_config(device);
    99 	int i;
   100 	for( i=0; conf[i].key != NULL; i++ ) {
   101 	    char buf[64];
   102 	    GtkWidget *entry1, *entry2;
   103 	    const gchar *key1, *key2;
   104 	    snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
   105 	    entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
   106 	    key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
   107 	    snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
   108 	    entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
   109 	    key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
   110 	    if( key1 == NULL || key1[0] == '\0') {
   111 		lxdream_set_config_value( &conf[i], key2 );
   112 	    } else if( key2 == NULL || key2[0] == '\0') {
   113 		lxdream_set_config_value( &conf[i], key1 );
   114 	    } else {
   115 		char buf[64];
   116 		snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
   117 		lxdream_set_config_value( &conf[i], buf );
   118 	    }
   119 	}
   120     }
   122 }
   124 static void controller_device_configure( maple_device_t device )
   125 {
   126     lxdream_config_entry_t conf = device->get_config(device);
   127     int count, i;
   128     for( count=0; conf[count].key != NULL; count++ );
   130     GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
   131     GList *focus_chain = NULL;
   132     gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
   133     for( i=0; i<count; i++ ) {
   134 	GtkWidget *text, *text2;
   135 	char buf[64];
   136 	int x=0;
   137 	int y=i;
   138 	if( i >= (count+1)>>1 ) {
   139 	    x = 3;
   140 	    y -= (count+1)>>1;
   141 	}
   142 	gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1, 
   143 			  GTK_SHRINK, GTK_SHRINK, 0, 0 );
   144 	text = gtk_entry_new();
   145 	gtk_entry_set_width_chars( GTK_ENTRY(text), 8 );
   146 	gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
   147 	g_signal_connect( text, "key_press_event", 
   148 			  G_CALLBACK(config_key_keypress), NULL );
   149 	g_signal_connect( text, "button_press_event",
   150 			  G_CALLBACK(config_key_buttonpress), NULL );
   151 	snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
   152 	g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
   153 	g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
   154 	gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
   155 	focus_chain = g_list_append( focus_chain, text );
   156 	text2 = gtk_entry_new();
   157 	gtk_entry_set_width_chars( GTK_ENTRY(text2), 8 );
   158 	gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
   159 	g_signal_connect( text2, "key_press_event", 
   160 			  G_CALLBACK(config_key_keypress), NULL );
   161 	g_signal_connect( text2, "button_press_event",
   162 			  G_CALLBACK(config_key_buttonpress), NULL );
   163 	snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
   164 	g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
   165 	g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
   166 	gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
   167 	focus_chain = g_list_append( focus_chain, text2 );
   168 	if( conf[i].value != NULL ) {
   169 	    gchar **parts = g_strsplit(conf[i].value,",",3);
   170 	    if( parts[0] != NULL ) {
   171 		gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
   172 		if( parts[1] != NULL ) {
   173 		    gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
   174 		}
   175 	    }
   176 	    g_strfreev(parts);
   177 	}
   178     }
   179     gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
   180     gtk_gui_run_property_dialog( _("Controller Configuration"), table, controller_config_done );
   181 }
   184 gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
   185 {
   186     maple_slot_data_t data = (maple_slot_data_t)user_data;
   187     if( data->new_device != NULL ) {
   188 	int i;
   189 	for( i=0; maple_device_config[i].name != NULL; i++ ) {
   190 	    if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
   191 		if( data->new_device == data->old_device ) {
   192 		    // Make a copy at this point if we haven't already
   193 		    data->new_device = data->old_device->clone(data->old_device);
   194 		}
   195 		maple_device_config[i].config_func(data->new_device);
   196 		break;
   197 	    }
   198 	}
   199 	if( maple_device_config[i].name == NULL ) {
   200 	    gui_error_dialog( _("No configuration page available for device type") );
   201 	}
   202     }
   203     return TRUE;
   204 }
   206 gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
   207 {
   208     maple_slot_data_t data = (maple_slot_data_t)user_data;
   209     int active = gtk_combo_box_get_active(combo);
   210     gtk_widget_set_sensitive(data->button, active != 0);
   211     if( active != 0 ) {
   212 	gchar *devname = gtk_combo_box_get_active_text(combo);
   213 	const maple_device_class_t devclz = maple_get_device_class(devname);
   214 	assert(devclz != NULL);
   215 	if( data->new_device != NULL ) {
   216 	    if( data->new_device->device_class != devclz ) {
   217 		data->new_device->destroy(data->new_device);
   218 		data->new_device = maple_new_device(devname);
   219 	    }
   220 	} else {
   221 	    data->new_device = maple_new_device(devname);
   222 	}
   223     } else {
   224 	if( data->new_device != NULL && data->new_device != data->old_device ) {
   225 	    data->new_device->destroy(data->new_device);
   226 	}
   227 	data->new_device = NULL;
   228     }
   229     return TRUE;
   230 }
   232 void maple_dialog_done( GtkWidget *panel, gboolean isOK )
   233 {
   234     if( isOK ) {
   235 	int i;
   236 	for( i=0; i<MAX_DEVICES; i++ ) {
   237 	    if( maple_data[i].new_device != maple_data[i].old_device ) {
   238 		if( maple_data[i].old_device != NULL ) {
   239 		    maple_detach_device(i,0);
   240 		}
   241 		if( maple_data[i].new_device != NULL ) {
   242 		    maple_attach_device(maple_data[i].new_device, i, 0 );
   243 		}
   244 	    }
   245 	}
   246 	lxdream_save_config();
   247     } else {
   248 	int i;
   249 	for( i=0; i<MAX_DEVICES; i++ ) {
   250 	    if( maple_data[i].new_device != NULL && 
   251 		maple_data[i].new_device != maple_data[i].old_device ) {
   252 		maple_data[i].new_device->destroy(maple_data[i].new_device);
   253 	    }
   254 	}
   255     }
   257 }
   259 GtkWidget *maple_panel_new()
   260 {
   261     GtkWidget *table = gtk_table_new(4, 3, TRUE);
   262     int i,j;
   263     const struct maple_device_class **devices = maple_get_device_classes();
   265     for( i=0; i< MAX_DEVICES; i++ ) {
   266 	char buf[12];
   267 	GtkWidget *combo, *button;
   268 	int active = 0;
   269 	maple_device_t device = maple_get_device(i,0);
   270 	sprintf( buf, _("Slot %d."), i );
   271 	gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
   272 	combo = gtk_combo_box_new_text();
   273 	gtk_combo_box_append_text( GTK_COMBO_BOX(combo), _("<empty>") );
   274 	for( j=0; devices[j] != NULL; j++ ) {
   275 	    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
   276 	    if( device != NULL && device->device_class == devices[j] ) {
   277 		active = j+1;
   278 	    }
   279 	}
   280 	gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
   281 	gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
   282 	button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
   283 	gtk_widget_set_sensitive(button, active != 0);
   284 	gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
   286 	maple_data[i].old_device = device;
   287 	maple_data[i].new_device = device;
   288 	maple_data[i].combo = combo;
   289 	maple_data[i].button = button;
   290 	g_signal_connect( button, "clicked", 
   291 			  G_CALLBACK( maple_properties_activated ), &maple_data[i] );
   292 	g_signal_connect( combo, "changed", 
   293 			  G_CALLBACK( maple_device_changed ), &maple_data[i] );
   295     }
   296     return table;
   297 }
   299 void maple_dialog_run( )
   300 {
   301     gtk_gui_run_property_dialog( _("Controller Settings"), maple_panel_new(), maple_dialog_done );
   302 }
.