2 * $Id: ctrl_dlg.c,v 1.3 2007-10-21 05:21:35 nkeynes Exp $
4 * Define the main (emu) GTK window, along with its menubars,
7 * Copyright (c) 2005 Nathan Keynes.
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.
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.
24 #include "gui/gtkui.h"
25 #include "maple/maple.h"
29 static void controller_device_configure(maple_device_t device);
31 struct maple_config_class {
33 void (*config_func)(maple_device_t device);
36 typedef struct maple_slot_data {
37 maple_device_t old_device;
38 maple_device_t new_device;
43 static struct maple_config_class maple_device_config[] = {
44 { "Sega Controller", controller_device_configure },
47 static struct maple_slot_data maple_data[MAX_DEVICES];
49 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
51 GdkKeymap *keymap = gdk_keymap_get_default();
54 gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
56 gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
60 static void controller_config_done( GtkWidget *panel, gboolean isOK )
66 static void controller_device_configure( maple_device_t device )
68 lxdream_config_entry_t conf = device->get_config(device);
70 for( count=0; conf[count].key != NULL; count++ );
72 GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
73 for( i=0; i<count; i++ ) {
74 GtkWidget *text, *text2;
77 if( i >= (count+1)>>1 ) {
81 gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1,
82 GTK_SHRINK, GTK_SHRINK, 0, 0 );
83 gchar **parts = g_strsplit(conf[i].value,",",3);
85 text = gtk_entry_new();
86 gtk_entry_set_width_chars( GTK_ENTRY(text), 8 );
87 g_signal_connect( text, "key_press_event",
88 G_CALLBACK(config_key_keypress), NULL );
89 gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
91 text2 = gtk_entry_new();
92 gtk_entry_set_width_chars( GTK_ENTRY(text2), 8 );
93 g_signal_connect( text2, "key_press_event",
94 G_CALLBACK(config_key_keypress), NULL );
95 gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
96 if( parts[0] != NULL ) {
97 gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
98 if( parts[1] != NULL ) {
99 gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
104 gtk_gui_run_property_dialog( "Controller Configuration", table, controller_config_done );
108 gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
110 maple_slot_data_t data = (maple_slot_data_t)user_data;
111 if( data->new_device != NULL ) {
113 for( i=0; maple_device_config[i].name != NULL; i++ ) {
114 if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
115 maple_device_config[i].config_func(data->new_device);
119 if( maple_device_config[i].name == NULL ) {
120 gui_error_dialog( "No configuration page available for device type" );
126 gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
128 maple_slot_data_t data = (maple_slot_data_t)user_data;
129 int active = gtk_combo_box_get_active(combo);
130 gtk_widget_set_sensitive(data->button, active != 0);
132 gchar *devname = gtk_combo_box_get_active_text(combo);
133 const maple_device_class_t devclz = maple_get_device_class(devname);
134 assert(devclz != NULL);
135 if( data->new_device != NULL ) {
136 if( data->new_device->device_class != devclz ) {
137 data->new_device->destroy(data->new_device);
138 data->new_device = maple_new_device(devname);
141 data->new_device = maple_new_device(devname);
144 if( data->new_device != NULL && data->new_device != data->old_device ) {
145 data->new_device->destroy(data->new_device);
147 data->new_device = NULL;
152 void maple_commit_changes( )
155 for( i=0; i<MAX_DEVICES; i++ ) {
156 if( maple_data[i].new_device != maple_data[i].old_device ) {
157 if( maple_data[i].old_device != NULL ) {
158 maple_detach_device(i,0);
160 if( maple_data[i].new_device != NULL ) {
161 maple_attach_device(maple_data[i].new_device, i, 0 );
165 lxdream_save_config();
168 void maple_cancel_changes( )
171 for( i=0; i<MAX_DEVICES; i++ ) {
172 if( maple_data[i].new_device != NULL &&
173 maple_data[i].new_device != maple_data[i].old_device ) {
174 maple_data[i].new_device->destroy(maple_data[i].new_device);
179 GtkWidget *maple_panel_new()
181 GtkWidget *table = gtk_table_new(4, 3, TRUE);
184 const struct maple_device_class **devices = maple_get_device_classes();
186 for( i=0; i< MAX_DEVICES; i++ ) {
188 GtkWidget *combo, *button;
190 maple_device_t device = maple_get_device(i,0);
191 sprintf( buf, "Slot %d.", i );
192 gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
193 combo = gtk_combo_box_new_text();
194 gtk_combo_box_append_text( GTK_COMBO_BOX(combo), "<empty>" );
195 for( j=0; devices[j] != NULL; j++ ) {
196 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
197 if( device != NULL && device->device_class == devices[j] ) {
201 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
202 gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
203 button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
204 gtk_widget_set_sensitive(button, active != 0);
205 gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
207 maple_data[i].old_device = device;
208 maple_data[i].new_device = device;
209 maple_data[i].combo = combo;
210 maple_data[i].button = button;
211 g_signal_connect( button, "clicked",
212 G_CALLBACK( maple_properties_activated ), &maple_data[i] );
213 g_signal_connect( combo, "changed",
214 G_CALLBACK( maple_device_changed ), &maple_data[i] );
220 void maple_dialog_run( GtkWindow *parent )
222 gint result = gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), NULL );
223 if( result == GTK_RESPONSE_ACCEPT ) {
224 maple_commit_changes();
226 maple_cancel_changes();
.