Search
lxdream.org :: lxdream/src/gui/ctrl_dlg.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/ctrl_dlg.c
changeset 460:a0c865b74c63
prev455:3080881d00d4
next480:d28c2992f5ee
author nkeynes
date Tue Oct 23 10:47:17 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Make controller config actually work
file annotate diff log raw
1.1 --- a/src/gui/ctrl_dlg.c Sun Oct 21 05:21:35 2007 +0000
1.2 +++ b/src/gui/ctrl_dlg.c Tue Oct 23 10:47:17 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: ctrl_dlg.c,v 1.3 2007-10-21 05:21:35 nkeynes Exp $
1.6 + * $Id: ctrl_dlg.c,v 1.4 2007-10-23 10:47:17 nkeynes Exp $
1.7 *
1.8 * Define the main (emu) GTK window, along with its menubars,
1.9 * toolbars, etc.
1.10 @@ -19,6 +19,7 @@
1.11
1.12 #include <assert.h>
1.13 #include <gtk/gtk.h>
1.14 +#include <gdk/gdkkeysyms.h>
1.15
1.16 #include "dream.h"
1.17 #include "gui/gtkui.h"
1.18 @@ -46,21 +47,78 @@
1.19
1.20 static struct maple_slot_data maple_data[MAX_DEVICES];
1.21
1.22 +static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
1.23 +{
1.24 + gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
1.25 + if( !keypress_mode ) {
1.26 + gtk_entry_set_text( GTK_ENTRY(widget), "<press key>" );
1.27 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
1.28 + }
1.29 + return FALSE;
1.30 +}
1.31 +
1.32 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
1.33 {
1.34 - GdkKeymap *keymap = gdk_keymap_get_default();
1.35 - guint keyval;
1.36 -
1.37 - gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
1.38 - NULL, NULL, NULL );
1.39 - gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
1.40 - return TRUE;
1.41 + gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
1.42 + if( keypress_mode ) {
1.43 + if( event->keyval == GDK_Escape ) {
1.44 + gtk_entry_set_text( GTK_ENTRY(widget), "" );
1.45 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.46 + return TRUE;
1.47 + }
1.48 + GdkKeymap *keymap = gdk_keymap_get_default();
1.49 + guint keyval;
1.50 +
1.51 + gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
1.52 + NULL, NULL, NULL );
1.53 + gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
1.54 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.55 + return TRUE;
1.56 + } else {
1.57 + switch( event->keyval ) {
1.58 + case GDK_Return:
1.59 + case GDK_KP_Enter:
1.60 + gtk_entry_set_text( GTK_ENTRY(widget), "<press key>" );
1.61 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
1.62 + return TRUE;
1.63 + case GDK_BackSpace:
1.64 + case GDK_Delete:
1.65 + gtk_entry_set_text( GTK_ENTRY(widget), "" );
1.66 + return TRUE;
1.67 + }
1.68 + return FALSE;
1.69 + }
1.70 +
1.71 }
1.72
1.73 static void controller_config_done( GtkWidget *panel, gboolean isOK )
1.74 {
1.75 -
1.76 -
1.77 + if( isOK ) {
1.78 + maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
1.79 + lxdream_config_entry_t conf = device->get_config(device);
1.80 + int i;
1.81 + for( i=0; conf[i].key != NULL; i++ ) {
1.82 + char buf[64];
1.83 + GtkWidget *entry1, *entry2;
1.84 + const gchar *key1, *key2;
1.85 + snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
1.86 + entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
1.87 + key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
1.88 + snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
1.89 + entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
1.90 + key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
1.91 + if( key1 == NULL || key1[0] == '\0') {
1.92 + lxdream_set_config_value( &conf[i], key2 );
1.93 + } else if( key2 == NULL || key2[0] == '\0') {
1.94 + lxdream_set_config_value( &conf[i], key1 );
1.95 + } else {
1.96 + char buf[64];
1.97 + snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
1.98 + lxdream_set_config_value( &conf[i], buf );
1.99 + }
1.100 + }
1.101 + }
1.102 +
1.103 }
1.104
1.105 static void controller_device_configure( maple_device_t device )
1.106 @@ -70,8 +128,12 @@
1.107 for( count=0; conf[count].key != NULL; count++ );
1.108
1.109 GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
1.110 + GList *focus_chain = NULL;
1.111 + gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
1.112 for( i=0; i<count; i++ ) {
1.113 GtkWidget *text, *text2;
1.114 + GQuark quark;
1.115 + char buf[64];
1.116 int x=0;
1.117 int y=i;
1.118 if( i >= (count+1)>>1 ) {
1.119 @@ -80,27 +142,42 @@
1.120 }
1.121 gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1,
1.122 GTK_SHRINK, GTK_SHRINK, 0, 0 );
1.123 - gchar **parts = g_strsplit(conf[i].value,",",3);
1.124 -
1.125 text = gtk_entry_new();
1.126 gtk_entry_set_width_chars( GTK_ENTRY(text), 8 );
1.127 + gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
1.128 g_signal_connect( text, "key_press_event",
1.129 G_CALLBACK(config_key_keypress), NULL );
1.130 + g_signal_connect( text, "button_press_event",
1.131 + G_CALLBACK(config_key_buttonpress), NULL );
1.132 + snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
1.133 + g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.134 + g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
1.135 gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
1.136 -
1.137 + focus_chain = g_list_append( focus_chain, text );
1.138 text2 = gtk_entry_new();
1.139 gtk_entry_set_width_chars( GTK_ENTRY(text2), 8 );
1.140 + gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
1.141 g_signal_connect( text2, "key_press_event",
1.142 G_CALLBACK(config_key_keypress), NULL );
1.143 + g_signal_connect( text2, "button_press_event",
1.144 + G_CALLBACK(config_key_buttonpress), NULL );
1.145 + snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
1.146 + g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.147 + g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
1.148 gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
1.149 - if( parts[0] != NULL ) {
1.150 - gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
1.151 - if( parts[1] != NULL ) {
1.152 - gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
1.153 + focus_chain = g_list_append( focus_chain, text2 );
1.154 + if( conf[i].value != NULL ) {
1.155 + gchar **parts = g_strsplit(conf[i].value,",",3);
1.156 + if( parts[0] != NULL ) {
1.157 + gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
1.158 + if( parts[1] != NULL ) {
1.159 + gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
1.160 + }
1.161 }
1.162 + g_strfreev(parts);
1.163 }
1.164 - g_strfreev(parts);
1.165 }
1.166 + gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
1.167 gtk_gui_run_property_dialog( "Controller Configuration", table, controller_config_done );
1.168 }
1.169
1.170 @@ -112,6 +189,10 @@
1.171 int i;
1.172 for( i=0; maple_device_config[i].name != NULL; i++ ) {
1.173 if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
1.174 + if( data->new_device == data->old_device ) {
1.175 + // Make a copy at this point if we haven't already
1.176 + data->new_device = data->old_device->clone(data->old_device);
1.177 + }
1.178 maple_device_config[i].config_func(data->new_device);
1.179 break;
1.180 }
1.181 @@ -149,31 +230,31 @@
1.182 return TRUE;
1.183 }
1.184
1.185 -void maple_commit_changes( )
1.186 +void maple_dialog_done( GtkWidget *panel, gboolean isOK )
1.187 {
1.188 - int i;
1.189 - for( i=0; i<MAX_DEVICES; i++ ) {
1.190 - if( maple_data[i].new_device != maple_data[i].old_device ) {
1.191 - if( maple_data[i].old_device != NULL ) {
1.192 - maple_detach_device(i,0);
1.193 + if( isOK ) {
1.194 + int i;
1.195 + for( i=0; i<MAX_DEVICES; i++ ) {
1.196 + if( maple_data[i].new_device != maple_data[i].old_device ) {
1.197 + if( maple_data[i].old_device != NULL ) {
1.198 + maple_detach_device(i,0);
1.199 + }
1.200 + if( maple_data[i].new_device != NULL ) {
1.201 + maple_attach_device(maple_data[i].new_device, i, 0 );
1.202 + }
1.203 }
1.204 - if( maple_data[i].new_device != NULL ) {
1.205 - maple_attach_device(maple_data[i].new_device, i, 0 );
1.206 + }
1.207 + lxdream_save_config();
1.208 + } else {
1.209 + int i;
1.210 + for( i=0; i<MAX_DEVICES; i++ ) {
1.211 + if( maple_data[i].new_device != NULL &&
1.212 + maple_data[i].new_device != maple_data[i].old_device ) {
1.213 + maple_data[i].new_device->destroy(maple_data[i].new_device);
1.214 }
1.215 }
1.216 }
1.217 - lxdream_save_config();
1.218 -}
1.219
1.220 -void maple_cancel_changes( )
1.221 -{
1.222 - int i;
1.223 - for( i=0; i<MAX_DEVICES; i++ ) {
1.224 - if( maple_data[i].new_device != NULL &&
1.225 - maple_data[i].new_device != maple_data[i].old_device ) {
1.226 - maple_data[i].new_device->destroy(maple_data[i].new_device);
1.227 - }
1.228 - }
1.229 }
1.230
1.231 GtkWidget *maple_panel_new()
1.232 @@ -217,12 +298,7 @@
1.233 return table;
1.234 }
1.235
1.236 -void maple_dialog_run( GtkWindow *parent )
1.237 +void maple_dialog_run( )
1.238 {
1.239 - gint result = gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), NULL );
1.240 - if( result == GTK_RESPONSE_ACCEPT ) {
1.241 - maple_commit_changes();
1.242 - } else {
1.243 - maple_cancel_changes();
1.244 - }
1.245 + gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), maple_dialog_done );
1.246 }
.