Search
lxdream.org :: lxdream/src/gtkui/ctrl_dlg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/ctrl_dlg.c
changeset 669:ab344e42bca9
prev628:bd46e2c4c479
next736:a02d1475ccfd
author nkeynes
date Mon May 12 10:00:13 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Cleanup most of the -Wall warnings (getting a bit sloppy...)
Convert FP code to use fixed banks rather than indirect pointer
(3-4% faster this way now)
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <string.h>
    22 #include <gtk/gtk.h>
    23 #include <gdk/gdkkeysyms.h>
    25 #include "lxdream.h"
    26 #include "display.h"
    27 #include "gtkui/gtkui.h"
    28 #include "maple/maple.h"
    30 #define MAX_DEVICES 4
    32 static void controller_device_configure(maple_device_t device);
    34 struct maple_config_class {
    35     const char *name;
    36     void (*config_func)(maple_device_t device);
    37 };
    39 typedef struct maple_slot_data {
    40     maple_device_t old_device;
    41     maple_device_t new_device;
    42     GtkWidget *button;
    43     GtkWidget *combo;
    44 } *maple_slot_data_t;
    46 static struct maple_config_class maple_device_config[] = {
    47     { "Sega Controller", controller_device_configure },
    48     { NULL, NULL } };
    50 static struct maple_slot_data maple_data[MAX_DEVICES];
    52 static void config_keysym_hook( void *data, const gchar *keysym )
    53 {
    54     GtkWidget *widget = (GtkWidget *)data;
    55     gtk_entry_set_text( GTK_ENTRY(widget), keysym );
    56     g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
    57     input_set_keysym_hook(NULL, NULL);
    58 }
    60 static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *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 	gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
    65 	g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
    66 	input_set_keysym_hook(config_keysym_hook, widget);
    67     }
    68     return FALSE;
    69 }
    71 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
    72 {
    73     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    74     if( keypress_mode ) {
    75 	if( event->keyval == GDK_Escape ) {
    76 	    gtk_entry_set_text( GTK_ENTRY(widget), "" );
    77 	    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
    78 	    return TRUE;
    79 	}
    80 	GdkKeymap *keymap = gdk_keymap_get_default();
    81 	guint keyval;
    83 	gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    84 					     NULL, NULL, NULL );
    85 	gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
    86 	g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
    87 	input_set_keysym_hook(NULL, NULL);
    88 	return TRUE;
    89     } else {
    90 	switch( event->keyval ) {
    91 	case GDK_Return:
    92 	case GDK_KP_Enter:
    93 	    gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
    94 	    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
    95 	    input_set_keysym_hook(config_keysym_hook, widget);
    96 	    return TRUE;
    97 	case GDK_BackSpace:
    98 	case GDK_Delete:
    99 	    gtk_entry_set_text( GTK_ENTRY(widget), "" );
   100 	    return TRUE;
   101 	}
   102 	return FALSE;
   103     }
   105 }
   107 static void controller_config_done( GtkWidget *panel, gboolean isOK )
   108 {
   109     if( isOK ) {
   110 	maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
   111 	lxdream_config_entry_t conf = device->get_config(device);
   112 	int i;
   113 	for( i=0; conf[i].key != NULL; i++ ) {
   114 	    char buf[64];
   115 	    GtkWidget *entry1, *entry2;
   116 	    const gchar *key1, *key2;
   117 	    snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
   118 	    entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
   119 	    key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
   120 	    snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
   121 	    entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
   122 	    key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
   123 	    if( key1 == NULL || key1[0] == '\0') {
   124 		lxdream_set_config_value( &conf[i], key2 );
   125 	    } else if( key2 == NULL || key2[0] == '\0') {
   126 		lxdream_set_config_value( &conf[i], key1 );
   127 	    } else {
   128 		char buf[64];
   129 		snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
   130 		lxdream_set_config_value( &conf[i], buf );
   131 	    }
   132 	}
   133     }
   134     input_set_keysym_hook(NULL, NULL);
   135 }
   137 static void controller_device_configure( maple_device_t device )
   138 {
   139     lxdream_config_entry_t conf = device->get_config(device);
   140     int count, i;
   141     for( count=0; conf[count].key != NULL; count++ );
   143     GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
   144     GList *focus_chain = NULL;
   145     gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
   146     for( i=0; i<count; i++ ) {
   147 	GtkWidget *text, *text2;
   148 	char buf[64];
   149 	int x=0;
   150 	int y=i;
   151 	if( i >= (count+1)>>1 ) {
   152 	    x = 3;
   153 	    y -= (count+1)>>1;
   154 	}
   155 	gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1, 
   156 			  GTK_SHRINK, GTK_SHRINK, 0, 0 );
   157 	text = gtk_entry_new();
   158 	gtk_entry_set_width_chars( GTK_ENTRY(text), 11 );
   159 	gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
   160 	g_signal_connect( text, "key_press_event", 
   161 			  G_CALLBACK(config_key_keypress), NULL );
   162 	g_signal_connect( text, "button_press_event",
   163 			  G_CALLBACK(config_key_buttonpress), NULL );
   164 	snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
   165 	g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
   166 	g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
   167 	gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
   168 	focus_chain = g_list_append( focus_chain, text );
   169 	text2 = gtk_entry_new();
   170 	gtk_entry_set_width_chars( GTK_ENTRY(text2), 11 );
   171 	gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
   172 	g_signal_connect( text2, "key_press_event", 
   173 			  G_CALLBACK(config_key_keypress), NULL );
   174 	g_signal_connect( text2, "button_press_event",
   175 			  G_CALLBACK(config_key_buttonpress), NULL );
   176 	snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
   177 	g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
   178 	g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
   179 	gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
   180 	focus_chain = g_list_append( focus_chain, text2 );
   181 	if( conf[i].value != NULL ) {
   182 	    gchar **parts = g_strsplit(conf[i].value,",",3);
   183 	    if( parts[0] != NULL ) {
   184 		gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
   185 		if( parts[1] != NULL ) {
   186 		    gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
   187 		}
   188 	    }
   189 	    g_strfreev(parts);
   190 	}
   191     }
   192     gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
   193     gtk_gui_run_property_dialog( _("Controller Configuration"), table, controller_config_done );
   194 }
   196 gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
   197 {
   198     maple_slot_data_t data = (maple_slot_data_t)user_data;
   199     if( data->new_device != NULL ) {
   200 	int i;
   201 	for( i=0; maple_device_config[i].name != NULL; i++ ) {
   202 	    if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
   203 		if( data->new_device == data->old_device ) {
   204 		    // Make a copy at this point if we haven't already
   205 		    data->new_device = data->old_device->clone(data->old_device);
   206 		}
   207 		maple_device_config[i].config_func(data->new_device);
   208 		break;
   209 	    }
   210 	}
   211 	if( maple_device_config[i].name == NULL ) {
   212 	    gui_error_dialog( _("No configuration page available for device type") );
   213 	}
   214     }
   215     return TRUE;
   216 }
   218 gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
   219 {
   220     maple_slot_data_t data = (maple_slot_data_t)user_data;
   221     int active = gtk_combo_box_get_active(combo);
   222     gboolean has_config = FALSE;
   223     if( active != 0 ) {
   224 	gchar *devname = gtk_combo_box_get_active_text(combo);
   225 	const maple_device_class_t devclz = maple_get_device_class(devname);
   226 	assert(devclz != NULL);
   227 	if( data->new_device != NULL ) {
   228 	    if( data->new_device->device_class != devclz ) {
   229 		if( data->new_device != data->old_device ) {
   230 		    data->new_device->destroy(data->new_device);
   231 		}
   232 		data->new_device = maple_new_device(devname);
   233 	    }
   234 	} else {
   235 	    data->new_device = maple_new_device(devname);
   236 	}
   237 	has_config = data->new_device != NULL && data->new_device->get_config != NULL;
   238     } else {
   239 	if( data->new_device != NULL && data->new_device != data->old_device ) {
   240 	    data->new_device->destroy(data->new_device);
   241 	}
   242 	data->new_device = NULL;
   243     }
   244     gtk_widget_set_sensitive(data->button, has_config);
   245     return TRUE;
   246 }
   248 void maple_dialog_done( GtkWidget *panel, gboolean isOK )
   249 {
   250     if( isOK ) {
   251 	int i;
   252 	for( i=0; i<MAX_DEVICES; i++ ) {
   253 	    if( maple_data[i].new_device != maple_data[i].old_device ) {
   254 		if( maple_data[i].old_device != NULL ) {
   255 		    maple_detach_device(i,0);
   256 		}
   257 		if( maple_data[i].new_device != NULL ) {
   258 		    maple_attach_device(maple_data[i].new_device, i, 0 );
   259 		}
   260 	    }
   261 	}
   262 	lxdream_save_config();
   263     } else {
   264 	int i;
   265 	for( i=0; i<MAX_DEVICES; i++ ) {
   266 	    if( maple_data[i].new_device != NULL && 
   267 		maple_data[i].new_device != maple_data[i].old_device ) {
   268 		maple_data[i].new_device->destroy(maple_data[i].new_device);
   269 	    }
   270 	}
   271     }
   273 }
   275 GtkWidget *maple_panel_new()
   276 {
   277     GtkWidget *table = gtk_table_new(4, 3, TRUE);
   278     int i,j;
   279     const struct maple_device_class **devices = maple_get_device_classes();
   281     for( i=0; i< MAX_DEVICES; i++ ) {
   282 	char buf[12];
   283 	GtkWidget *combo, *button;
   284 	int active = 0;
   285 	maple_device_t device = maple_get_device(i,0);
   286 	sprintf( buf, _("Slot %d."), i );
   287 	gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
   288 	combo = gtk_combo_box_new_text();
   289 	gtk_combo_box_append_text( GTK_COMBO_BOX(combo), _("<empty>") );
   290 	for( j=0; devices[j] != NULL; j++ ) {
   291 	    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
   292 	    if( device != NULL && device->device_class == devices[j] ) {
   293 		active = j+1;
   294 	    }
   295 	}
   296 	gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
   297 	gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
   298 	button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
   299 	gtk_widget_set_sensitive(button, active != 0 && device->get_config != NULL);
   300 	gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
   302 	maple_data[i].old_device = device;
   303 	maple_data[i].new_device = device;
   304 	maple_data[i].combo = combo;
   305 	maple_data[i].button = button;
   306 	g_signal_connect( button, "clicked", 
   307 			  G_CALLBACK( maple_properties_activated ), &maple_data[i] );
   308 	g_signal_connect( combo, "changed", 
   309 			  G_CALLBACK( maple_device_changed ), &maple_data[i] );
   311     }
   312     return table;
   313 }
   315 void maple_dialog_run( )
   316 {
   317     gtk_gui_run_property_dialog( _("Controller Settings"), maple_panel_new(), maple_dialog_done );
   318 }
.