Search
lxdream.org :: lxdream/src/gtkui/ctrl_dlg.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/ctrl_dlg.c
changeset 537:d924be49e192
prev508:ccd2c10edfe6
next561:533f6b478071
author nkeynes
date Thu Nov 22 11:10:15 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Re-add "Load Binary" menu item (misplaced in GUI rewrite)
Prevent running with no code loaded
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gtkui/ctrl_dlg.c Thu Nov 22 11:10:15 2007 +0000
1.3 @@ -0,0 +1,302 @@
1.4 +/**
1.5 + * $Id: ctrl_dlg.c,v 1.6 2007-11-10 04:45:29 nkeynes Exp $
1.6 + *
1.7 + * Define the main (emu) GTK window, along with its menubars,
1.8 + * toolbars, etc.
1.9 + *
1.10 + * Copyright (c) 2005 Nathan Keynes.
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + */
1.22 +
1.23 +#include <assert.h>
1.24 +#include <gtk/gtk.h>
1.25 +#include <gdk/gdkkeysyms.h>
1.26 +
1.27 +#include "dream.h"
1.28 +#include "gtkui/gtkui.h"
1.29 +#include "maple/maple.h"
1.30 +
1.31 +#define MAX_DEVICES 4
1.32 +
1.33 +static void controller_device_configure(maple_device_t device);
1.34 +
1.35 +struct maple_config_class {
1.36 + const char *name;
1.37 + void (*config_func)(maple_device_t device);
1.38 +};
1.39 +
1.40 +typedef struct maple_slot_data {
1.41 + maple_device_t old_device;
1.42 + maple_device_t new_device;
1.43 + GtkWidget *button;
1.44 + GtkWidget *combo;
1.45 +} *maple_slot_data_t;
1.46 +
1.47 +static struct maple_config_class maple_device_config[] = {
1.48 + { "Sega Controller", controller_device_configure },
1.49 + { NULL, NULL } };
1.50 +
1.51 +static struct maple_slot_data maple_data[MAX_DEVICES];
1.52 +
1.53 +static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
1.54 +{
1.55 + gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
1.56 + if( !keypress_mode ) {
1.57 + gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
1.58 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
1.59 + }
1.60 + return FALSE;
1.61 +}
1.62 +
1.63 +static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
1.64 +{
1.65 + gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
1.66 + if( keypress_mode ) {
1.67 + if( event->keyval == GDK_Escape ) {
1.68 + gtk_entry_set_text( GTK_ENTRY(widget), "" );
1.69 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.70 + return TRUE;
1.71 + }
1.72 + GdkKeymap *keymap = gdk_keymap_get_default();
1.73 + guint keyval;
1.74 +
1.75 + gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
1.76 + NULL, NULL, NULL );
1.77 + gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
1.78 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.79 + return TRUE;
1.80 + } else {
1.81 + switch( event->keyval ) {
1.82 + case GDK_Return:
1.83 + case GDK_KP_Enter:
1.84 + gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
1.85 + g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
1.86 + return TRUE;
1.87 + case GDK_BackSpace:
1.88 + case GDK_Delete:
1.89 + gtk_entry_set_text( GTK_ENTRY(widget), "" );
1.90 + return TRUE;
1.91 + }
1.92 + return FALSE;
1.93 + }
1.94 +
1.95 +}
1.96 +
1.97 +static void controller_config_done( GtkWidget *panel, gboolean isOK )
1.98 +{
1.99 + if( isOK ) {
1.100 + maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
1.101 + lxdream_config_entry_t conf = device->get_config(device);
1.102 + int i;
1.103 + for( i=0; conf[i].key != NULL; i++ ) {
1.104 + char buf[64];
1.105 + GtkWidget *entry1, *entry2;
1.106 + const gchar *key1, *key2;
1.107 + snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
1.108 + entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
1.109 + key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
1.110 + snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
1.111 + entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
1.112 + key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
1.113 + if( key1 == NULL || key1[0] == '\0') {
1.114 + lxdream_set_config_value( &conf[i], key2 );
1.115 + } else if( key2 == NULL || key2[0] == '\0') {
1.116 + lxdream_set_config_value( &conf[i], key1 );
1.117 + } else {
1.118 + char buf[64];
1.119 + snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
1.120 + lxdream_set_config_value( &conf[i], buf );
1.121 + }
1.122 + }
1.123 + }
1.124 +
1.125 +}
1.126 +
1.127 +static void controller_device_configure( maple_device_t device )
1.128 +{
1.129 + lxdream_config_entry_t conf = device->get_config(device);
1.130 + int count, i;
1.131 + for( count=0; conf[count].key != NULL; count++ );
1.132 +
1.133 + GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
1.134 + GList *focus_chain = NULL;
1.135 + gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
1.136 + for( i=0; i<count; i++ ) {
1.137 + GtkWidget *text, *text2;
1.138 + char buf[64];
1.139 + int x=0;
1.140 + int y=i;
1.141 + if( i >= (count+1)>>1 ) {
1.142 + x = 3;
1.143 + y -= (count+1)>>1;
1.144 + }
1.145 + gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1,
1.146 + GTK_SHRINK, GTK_SHRINK, 0, 0 );
1.147 + text = gtk_entry_new();
1.148 + gtk_entry_set_width_chars( GTK_ENTRY(text), 8 );
1.149 + gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
1.150 + g_signal_connect( text, "key_press_event",
1.151 + G_CALLBACK(config_key_keypress), NULL );
1.152 + g_signal_connect( text, "button_press_event",
1.153 + G_CALLBACK(config_key_buttonpress), NULL );
1.154 + snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
1.155 + g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.156 + g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
1.157 + gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
1.158 + focus_chain = g_list_append( focus_chain, text );
1.159 + text2 = gtk_entry_new();
1.160 + gtk_entry_set_width_chars( GTK_ENTRY(text2), 8 );
1.161 + gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
1.162 + g_signal_connect( text2, "key_press_event",
1.163 + G_CALLBACK(config_key_keypress), NULL );
1.164 + g_signal_connect( text2, "button_press_event",
1.165 + G_CALLBACK(config_key_buttonpress), NULL );
1.166 + snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
1.167 + g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
1.168 + g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
1.169 + gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
1.170 + focus_chain = g_list_append( focus_chain, text2 );
1.171 + if( conf[i].value != NULL ) {
1.172 + gchar **parts = g_strsplit(conf[i].value,",",3);
1.173 + if( parts[0] != NULL ) {
1.174 + gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
1.175 + if( parts[1] != NULL ) {
1.176 + gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
1.177 + }
1.178 + }
1.179 + g_strfreev(parts);
1.180 + }
1.181 + }
1.182 + gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
1.183 + gtk_gui_run_property_dialog( _("Controller Configuration"), table, controller_config_done );
1.184 +}
1.185 +
1.186 +
1.187 +gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
1.188 +{
1.189 + maple_slot_data_t data = (maple_slot_data_t)user_data;
1.190 + if( data->new_device != NULL ) {
1.191 + int i;
1.192 + for( i=0; maple_device_config[i].name != NULL; i++ ) {
1.193 + if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
1.194 + if( data->new_device == data->old_device ) {
1.195 + // Make a copy at this point if we haven't already
1.196 + data->new_device = data->old_device->clone(data->old_device);
1.197 + }
1.198 + maple_device_config[i].config_func(data->new_device);
1.199 + break;
1.200 + }
1.201 + }
1.202 + if( maple_device_config[i].name == NULL ) {
1.203 + gui_error_dialog( _("No configuration page available for device type") );
1.204 + }
1.205 + }
1.206 + return TRUE;
1.207 +}
1.208 +
1.209 +gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
1.210 +{
1.211 + maple_slot_data_t data = (maple_slot_data_t)user_data;
1.212 + int active = gtk_combo_box_get_active(combo);
1.213 + gtk_widget_set_sensitive(data->button, active != 0);
1.214 + if( active != 0 ) {
1.215 + gchar *devname = gtk_combo_box_get_active_text(combo);
1.216 + const maple_device_class_t devclz = maple_get_device_class(devname);
1.217 + assert(devclz != NULL);
1.218 + if( data->new_device != NULL ) {
1.219 + if( data->new_device->device_class != devclz ) {
1.220 + data->new_device->destroy(data->new_device);
1.221 + data->new_device = maple_new_device(devname);
1.222 + }
1.223 + } else {
1.224 + data->new_device = maple_new_device(devname);
1.225 + }
1.226 + } else {
1.227 + if( data->new_device != NULL && data->new_device != data->old_device ) {
1.228 + data->new_device->destroy(data->new_device);
1.229 + }
1.230 + data->new_device = NULL;
1.231 + }
1.232 + return TRUE;
1.233 +}
1.234 +
1.235 +void maple_dialog_done( GtkWidget *panel, gboolean isOK )
1.236 +{
1.237 + if( isOK ) {
1.238 + int i;
1.239 + for( i=0; i<MAX_DEVICES; i++ ) {
1.240 + if( maple_data[i].new_device != maple_data[i].old_device ) {
1.241 + if( maple_data[i].old_device != NULL ) {
1.242 + maple_detach_device(i,0);
1.243 + }
1.244 + if( maple_data[i].new_device != NULL ) {
1.245 + maple_attach_device(maple_data[i].new_device, i, 0 );
1.246 + }
1.247 + }
1.248 + }
1.249 + lxdream_save_config();
1.250 + } else {
1.251 + int i;
1.252 + for( i=0; i<MAX_DEVICES; i++ ) {
1.253 + if( maple_data[i].new_device != NULL &&
1.254 + maple_data[i].new_device != maple_data[i].old_device ) {
1.255 + maple_data[i].new_device->destroy(maple_data[i].new_device);
1.256 + }
1.257 + }
1.258 + }
1.259 +
1.260 +}
1.261 +
1.262 +GtkWidget *maple_panel_new()
1.263 +{
1.264 + GtkWidget *table = gtk_table_new(4, 3, TRUE);
1.265 + int i,j;
1.266 + const struct maple_device_class **devices = maple_get_device_classes();
1.267 +
1.268 + for( i=0; i< MAX_DEVICES; i++ ) {
1.269 + char buf[12];
1.270 + GtkWidget *combo, *button;
1.271 + int active = 0;
1.272 + maple_device_t device = maple_get_device(i,0);
1.273 + sprintf( buf, _("Slot %d."), i );
1.274 + gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
1.275 + combo = gtk_combo_box_new_text();
1.276 + gtk_combo_box_append_text( GTK_COMBO_BOX(combo), _("<empty>") );
1.277 + for( j=0; devices[j] != NULL; j++ ) {
1.278 + gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
1.279 + if( device != NULL && device->device_class == devices[j] ) {
1.280 + active = j+1;
1.281 + }
1.282 + }
1.283 + gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
1.284 + gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
1.285 + button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
1.286 + gtk_widget_set_sensitive(button, active != 0);
1.287 + gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
1.288 +
1.289 + maple_data[i].old_device = device;
1.290 + maple_data[i].new_device = device;
1.291 + maple_data[i].combo = combo;
1.292 + maple_data[i].button = button;
1.293 + g_signal_connect( button, "clicked",
1.294 + G_CALLBACK( maple_properties_activated ), &maple_data[i] );
1.295 + g_signal_connect( combo, "changed",
1.296 + G_CALLBACK( maple_device_changed ), &maple_data[i] );
1.297 +
1.298 + }
1.299 + return table;
1.300 +}
1.301 +
1.302 +void maple_dialog_run( )
1.303 +{
1.304 + gtk_gui_run_property_dialog( _("Controller Settings"), maple_panel_new(), maple_dialog_done );
1.305 +}
.