Search
lxdream.org :: lxdream :: r460:a0c865b74c63
lxdream 0.9.1
released Jun 29
Download Now
changeset460:a0c865b74c63
parent459:21041c9634e4
child461:63d4de8dcec6
authornkeynes
dateTue Oct 23 10:47:17 2007 +0000 (16 years ago)
Make controller config actually work
src/config.c
src/config.h
src/gui/ctrl_dlg.c
src/maple/controller.c
src/maple/maple.h
1.1 --- a/src/config.c Tue Oct 23 10:40:46 2007 +0000
1.2 +++ b/src/config.c Tue Oct 23 10:47:17 2007 +0000
1.3 @@ -1,5 +1,5 @@
1.4 /**
1.5 - * $Id: config.c,v 1.2 2007-10-22 21:12:54 nkeynes Exp $
1.6 + * $Id: config.c,v 1.3 2007-10-23 10:47:17 nkeynes Exp $
1.7 *
1.8 * User configuration support
1.9 *
1.10 @@ -108,31 +108,39 @@
1.11 return global_config[key].value;
1.12 }
1.13
1.14 -void lxdream_set_config_value( int key, const gchar *value )
1.15 +void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
1.16 {
1.17 - struct lxdream_config_entry *param = &global_config[key];
1.18 if( param->value != param->default_value && param->value != NULL ) {
1.19 free( param->value );
1.20 }
1.21 param->value = g_strdup(value);
1.22 }
1.23
1.24 +void lxdream_set_global_config_value( int key, const gchar *value )
1.25 +{
1.26 + lxdream_set_config_value(&global_config[key], value);
1.27 +}
1.28 +
1.29 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value )
1.30 {
1.31 int i;
1.32 for( i=0; group->params[i].key != NULL; i++ ) {
1.33 if( strcasecmp( group->params[i].key, key ) == 0 ) {
1.34 - if( group->params[i].value != group->params[i].default_value &&
1.35 - group->params[i].value != NULL ) {
1.36 - free( group->params[i].value );
1.37 - }
1.38 - group->params[i].value = g_strdup( value );
1.39 + lxdream_set_config_value( &group->params[i], value );
1.40 return TRUE;
1.41 }
1.42 }
1.43 return FALSE;
1.44 }
1.45
1.46 +void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src )
1.47 +{
1.48 + int i;
1.49 + for( i=0; src[i].key != NULL; i++ ) {
1.50 + lxdream_set_config_value( &dest[i], src[i].value );
1.51 + }
1.52 +}
1.53 +
1.54 gboolean lxdream_load_config( )
1.55 {
1.56 if( lxdream_config_load_filename == NULL ) {
2.1 --- a/src/config.h Tue Oct 23 10:40:46 2007 +0000
2.2 +++ b/src/config.h Tue Oct 23 10:47:17 2007 +0000
2.3 @@ -1,5 +1,5 @@
2.4 /**
2.5 - * $Id: config.h,v 1.2 2007-10-22 21:12:54 nkeynes Exp $
2.6 + * $Id: config.h,v 1.3 2007-10-23 10:47:17 nkeynes Exp $
2.7 *
2.8 * User configuration support
2.9 *
2.10 @@ -51,8 +51,11 @@
2.11 /* Global config values */
2.12 const gchar *lxdream_get_config_value( int key );
2.13
2.14 -void lxdream_set_config_value( int key, const gchar *value );
2.15 +void lxdream_set_global_config_value( int key, const gchar *value );
2.16 +void lxdream_set_config_value( lxdream_config_entry_t entry, const gchar *value );
2.17 gboolean lxdream_set_group_value( lxdream_config_group_t group, const gchar *key, const gchar *value );
2.18 +void lxdream_copy_config_list( lxdream_config_entry_t dest, lxdream_config_entry_t src );
2.19 +
2.20 /**
2.21 * Search the standard locations for the configuration file:
2.22 * $HOME/.lxdreamrc
3.1 --- a/src/gui/ctrl_dlg.c Tue Oct 23 10:40:46 2007 +0000
3.2 +++ b/src/gui/ctrl_dlg.c Tue Oct 23 10:47:17 2007 +0000
3.3 @@ -1,5 +1,5 @@
3.4 /**
3.5 - * $Id: ctrl_dlg.c,v 1.3 2007-10-21 05:21:35 nkeynes Exp $
3.6 + * $Id: ctrl_dlg.c,v 1.4 2007-10-23 10:47:17 nkeynes Exp $
3.7 *
3.8 * Define the main (emu) GTK window, along with its menubars,
3.9 * toolbars, etc.
3.10 @@ -19,6 +19,7 @@
3.11
3.12 #include <assert.h>
3.13 #include <gtk/gtk.h>
3.14 +#include <gdk/gdkkeysyms.h>
3.15
3.16 #include "dream.h"
3.17 #include "gui/gtkui.h"
3.18 @@ -46,21 +47,78 @@
3.19
3.20 static struct maple_slot_data maple_data[MAX_DEVICES];
3.21
3.22 +static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
3.23 +{
3.24 + gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
3.25 + if( !keypress_mode ) {
3.26 + gtk_entry_set_text( GTK_ENTRY(widget), "<press key>" );
3.27 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
3.28 + }
3.29 + return FALSE;
3.30 +}
3.31 +
3.32 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
3.33 {
3.34 - GdkKeymap *keymap = gdk_keymap_get_default();
3.35 - guint keyval;
3.36 -
3.37 - gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
3.38 - NULL, NULL, NULL );
3.39 - gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
3.40 - return TRUE;
3.41 + gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
3.42 + if( keypress_mode ) {
3.43 + if( event->keyval == GDK_Escape ) {
3.44 + gtk_entry_set_text( GTK_ENTRY(widget), "" );
3.45 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
3.46 + return TRUE;
3.47 + }
3.48 + GdkKeymap *keymap = gdk_keymap_get_default();
3.49 + guint keyval;
3.50 +
3.51 + gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
3.52 + NULL, NULL, NULL );
3.53 + gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
3.54 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
3.55 + return TRUE;
3.56 + } else {
3.57 + switch( event->keyval ) {
3.58 + case GDK_Return:
3.59 + case GDK_KP_Enter:
3.60 + gtk_entry_set_text( GTK_ENTRY(widget), "<press key>" );
3.61 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
3.62 + return TRUE;
3.63 + case GDK_BackSpace:
3.64 + case GDK_Delete:
3.65 + gtk_entry_set_text( GTK_ENTRY(widget), "" );
3.66 + return TRUE;
3.67 + }
3.68 + return FALSE;
3.69 + }
3.70 +
3.71 }
3.72
3.73 static void controller_config_done( GtkWidget *panel, gboolean isOK )
3.74 {
3.75 -
3.76 -
3.77 + if( isOK ) {
3.78 + maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
3.79 + lxdream_config_entry_t conf = device->get_config(device);
3.80 + int i;
3.81 + for( i=0; conf[i].key != NULL; i++ ) {
3.82 + char buf[64];
3.83 + GtkWidget *entry1, *entry2;
3.84 + const gchar *key1, *key2;
3.85 + snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
3.86 + entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
3.87 + key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
3.88 + snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
3.89 + entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
3.90 + key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
3.91 + if( key1 == NULL || key1[0] == '\0') {
3.92 + lxdream_set_config_value( &conf[i], key2 );
3.93 + } else if( key2 == NULL || key2[0] == '\0') {
3.94 + lxdream_set_config_value( &conf[i], key1 );
3.95 + } else {
3.96 + char buf[64];
3.97 + snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
3.98 + lxdream_set_config_value( &conf[i], buf );
3.99 + }
3.100 + }
3.101 + }
3.102 +
3.103 }
3.104
3.105 static void controller_device_configure( maple_device_t device )
3.106 @@ -70,8 +128,12 @@
3.107 for( count=0; conf[count].key != NULL; count++ );
3.108
3.109 GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
3.110 + GList *focus_chain = NULL;
3.111 + gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
3.112 for( i=0; i<count; i++ ) {
3.113 GtkWidget *text, *text2;
3.114 + GQuark quark;
3.115 + char buf[64];
3.116 int x=0;
3.117 int y=i;
3.118 if( i >= (count+1)>>1 ) {
3.119 @@ -80,27 +142,42 @@
3.120 }
3.121 gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1,
3.122 GTK_SHRINK, GTK_SHRINK, 0, 0 );
3.123 - gchar **parts = g_strsplit(conf[i].value,",",3);
3.124 -
3.125 text = gtk_entry_new();
3.126 gtk_entry_set_width_chars( GTK_ENTRY(text), 8 );
3.127 + gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
3.128 g_signal_connect( text, "key_press_event",
3.129 G_CALLBACK(config_key_keypress), NULL );
3.130 + g_signal_connect( text, "button_press_event",
3.131 + G_CALLBACK(config_key_buttonpress), NULL );
3.132 + snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
3.133 + g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
3.134 + g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
3.135 gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
3.136 -
3.137 + focus_chain = g_list_append( focus_chain, text );
3.138 text2 = gtk_entry_new();
3.139 gtk_entry_set_width_chars( GTK_ENTRY(text2), 8 );
3.140 + gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
3.141 g_signal_connect( text2, "key_press_event",
3.142 G_CALLBACK(config_key_keypress), NULL );
3.143 + g_signal_connect( text2, "button_press_event",
3.144 + G_CALLBACK(config_key_buttonpress), NULL );
3.145 + snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
3.146 + g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
3.147 + g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
3.148 gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
3.149 - if( parts[0] != NULL ) {
3.150 - gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
3.151 - if( parts[1] != NULL ) {
3.152 - gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
3.153 + focus_chain = g_list_append( focus_chain, text2 );
3.154 + if( conf[i].value != NULL ) {
3.155 + gchar **parts = g_strsplit(conf[i].value,",",3);
3.156 + if( parts[0] != NULL ) {
3.157 + gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
3.158 + if( parts[1] != NULL ) {
3.159 + gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
3.160 + }
3.161 }
3.162 + g_strfreev(parts);
3.163 }
3.164 - g_strfreev(parts);
3.165 }
3.166 + gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
3.167 gtk_gui_run_property_dialog( "Controller Configuration", table, controller_config_done );
3.168 }
3.169
3.170 @@ -112,6 +189,10 @@
3.171 int i;
3.172 for( i=0; maple_device_config[i].name != NULL; i++ ) {
3.173 if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
3.174 + if( data->new_device == data->old_device ) {
3.175 + // Make a copy at this point if we haven't already
3.176 + data->new_device = data->old_device->clone(data->old_device);
3.177 + }
3.178 maple_device_config[i].config_func(data->new_device);
3.179 break;
3.180 }
3.181 @@ -149,31 +230,31 @@
3.182 return TRUE;
3.183 }
3.184
3.185 -void maple_commit_changes( )
3.186 +void maple_dialog_done( GtkWidget *panel, gboolean isOK )
3.187 {
3.188 - int i;
3.189 - for( i=0; i<MAX_DEVICES; i++ ) {
3.190 - if( maple_data[i].new_device != maple_data[i].old_device ) {
3.191 - if( maple_data[i].old_device != NULL ) {
3.192 - maple_detach_device(i,0);
3.193 + if( isOK ) {
3.194 + int i;
3.195 + for( i=0; i<MAX_DEVICES; i++ ) {
3.196 + if( maple_data[i].new_device != maple_data[i].old_device ) {
3.197 + if( maple_data[i].old_device != NULL ) {
3.198 + maple_detach_device(i,0);
3.199 + }
3.200 + if( maple_data[i].new_device != NULL ) {
3.201 + maple_attach_device(maple_data[i].new_device, i, 0 );
3.202 + }
3.203 }
3.204 - if( maple_data[i].new_device != NULL ) {
3.205 - maple_attach_device(maple_data[i].new_device, i, 0 );
3.206 + }
3.207 + lxdream_save_config();
3.208 + } else {
3.209 + int i;
3.210 + for( i=0; i<MAX_DEVICES; i++ ) {
3.211 + if( maple_data[i].new_device != NULL &&
3.212 + maple_data[i].new_device != maple_data[i].old_device ) {
3.213 + maple_data[i].new_device->destroy(maple_data[i].new_device);
3.214 }
3.215 }
3.216 }
3.217 - lxdream_save_config();
3.218 -}
3.219
3.220 -void maple_cancel_changes( )
3.221 -{
3.222 - int i;
3.223 - for( i=0; i<MAX_DEVICES; i++ ) {
3.224 - if( maple_data[i].new_device != NULL &&
3.225 - maple_data[i].new_device != maple_data[i].old_device ) {
3.226 - maple_data[i].new_device->destroy(maple_data[i].new_device);
3.227 - }
3.228 - }
3.229 }
3.230
3.231 GtkWidget *maple_panel_new()
3.232 @@ -217,12 +298,7 @@
3.233 return table;
3.234 }
3.235
3.236 -void maple_dialog_run( GtkWindow *parent )
3.237 +void maple_dialog_run( )
3.238 {
3.239 - gint result = gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), NULL );
3.240 - if( result == GTK_RESPONSE_ACCEPT ) {
3.241 - maple_commit_changes();
3.242 - } else {
3.243 - maple_cancel_changes();
3.244 - }
3.245 + gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), maple_dialog_done );
3.246 }
4.1 --- a/src/maple/controller.c Tue Oct 23 10:40:46 2007 +0000
4.2 +++ b/src/maple/controller.c Tue Oct 23 10:47:17 2007 +0000
4.3 @@ -1,5 +1,5 @@
4.4 /**
4.5 - * $Id: controller.c,v 1.8 2007-10-21 05:15:56 nkeynes Exp $
4.6 + * $Id: controller.c,v 1.9 2007-10-23 10:47:17 nkeynes Exp $
4.7 *
4.8 * Implements the standard dreamcast controller
4.9 *
4.10 @@ -28,6 +28,7 @@
4.11 void controller_attach( maple_device_t dev );
4.12 void controller_detach( maple_device_t dev );
4.13 void controller_destroy( maple_device_t dev );
4.14 +maple_device_t controller_clone( maple_device_t dev );
4.15 maple_device_t controller_new();
4.16 lxdream_config_entry_t controller_get_config( maple_device_t dev );
4.17 int controller_get_cond( maple_device_t dev, int function, unsigned char *outbuf,
4.18 @@ -44,7 +45,7 @@
4.19 static struct controller_device base_controller = {
4.20 { MAPLE_DEVICE_TAG, &controller_class, CONTROLLER_IDENT, CONTROLLER_VERSION,
4.21 controller_get_config, controller_attach, controller_detach, controller_destroy,
4.22 - NULL, NULL, controller_get_cond, NULL, NULL, NULL },
4.23 + controller_clone, NULL, NULL, controller_get_cond, NULL, NULL, NULL },
4.24 {0x0000FFFF, 0x80808080},
4.25 {{ "dpad left", CONFIG_TYPE_KEY },
4.26 { "dpad right", CONFIG_TYPE_KEY },
4.27 @@ -72,6 +73,15 @@
4.28 return MAPLE_DEVICE(dev);
4.29 }
4.30
4.31 +maple_device_t controller_clone( maple_device_t srcdevice )
4.32 +{
4.33 + controller_device_t src = (controller_device_t)srcdevice;
4.34 + controller_device_t dev = (controller_device_t)controller_new();
4.35 + lxdream_copy_config_list( dev->config, src->config );
4.36 + memcpy( dev->condition, src->condition, sizeof(src->condition) );
4.37 + return MAPLE_DEVICE(dev);
4.38 +}
4.39 +
4.40 /**
4.41 * Input callback
4.42 */
5.1 --- a/src/maple/maple.h Tue Oct 23 10:40:46 2007 +0000
5.2 +++ b/src/maple/maple.h Tue Oct 23 10:47:17 2007 +0000
5.3 @@ -1,5 +1,5 @@
5.4 /**
5.5 - * $Id: maple.h,v 1.7 2007-10-17 11:26:45 nkeynes Exp $
5.6 + * $Id: maple.h,v 1.8 2007-10-23 10:47:17 nkeynes Exp $
5.7 *
5.8 * Maple bus definitions
5.9 *
5.10 @@ -75,7 +75,7 @@
5.11 void (*attach)(struct maple_device *dev);
5.12 void (*detach)(struct maple_device *dev);
5.13 void (*destroy)(struct maple_device *dev);
5.14 -
5.15 + struct maple_device * (*clone)(struct maple_device *dev);
5.16 int (*reset)(struct maple_device *dev);
5.17 int (*shutdown)(struct maple_device *dev);
5.18 int (*get_condition)(struct maple_device *dev,
.