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.
23 #include <gdk/gdkkeysyms.h>
27 #include "gtkui/gtkui.h"
28 #include "maple/maple.h"
32 static void controller_device_configure(maple_device_t device);
34 struct maple_config_class {
36 void (*config_func)(maple_device_t device);
39 typedef struct maple_slot_data {
40 maple_device_t old_device;
41 maple_device_t new_device;
46 static struct maple_config_class maple_device_config[] = {
47 { "Sega Controller", controller_device_configure },
50 static struct maple_slot_data maple_data[MAX_DEVICES];
52 static void config_keysym_hook( void *data, const gchar *keysym )
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);
60 static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
62 gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
64 gchar *keysym = input_keycode_to_keysym( &system_mouse_driver, event->button);
65 if( keysym != NULL ) {
66 config_keysym_hook( widget, keysym );
71 gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
72 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
73 input_set_keysym_hook(config_keysym_hook, widget);
78 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
80 gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
82 if( event->keyval == GDK_Escape ) {
83 gtk_entry_set_text( GTK_ENTRY(widget), "" );
84 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
87 GdkKeymap *keymap = gdk_keymap_get_default();
90 gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
92 gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
93 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
94 input_set_keysym_hook(NULL, NULL);
97 switch( event->keyval ) {
100 gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
101 g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
102 input_set_keysym_hook(config_keysym_hook, widget);
106 gtk_entry_set_text( GTK_ENTRY(widget), "" );
114 static void controller_config_done( GtkWidget *panel, gboolean isOK )
117 maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
118 lxdream_config_entry_t conf = device->get_config(device);
120 for( i=0; conf[i].key != NULL; i++ ) {
122 GtkWidget *entry1, *entry2;
123 const gchar *key1, *key2;
124 snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
125 entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
126 key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
127 snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
128 entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
129 key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
130 if( key1 == NULL || key1[0] == '\0') {
131 lxdream_set_config_value( &conf[i], key2 );
132 } else if( key2 == NULL || key2[0] == '\0') {
133 lxdream_set_config_value( &conf[i], key1 );
136 snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
137 lxdream_set_config_value( &conf[i], buf );
141 input_set_keysym_hook(NULL, NULL);
144 static void controller_device_configure( maple_device_t device )
146 lxdream_config_entry_t conf = maple_get_device_config(device);
148 for( count=0; conf[count].key != NULL; count++ );
150 GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
151 GList *focus_chain = NULL;
152 gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
153 for( i=0; i<count; i++ ) {
154 GtkWidget *text, *text2;
158 if( i >= (count+1)>>1 ) {
162 gtk_table_attach( GTK_TABLE(table), gtk_label_new(gettext(conf[i].label)), x, x+1, y, y+1,
163 GTK_SHRINK, GTK_SHRINK, 0, 0 );
164 text = gtk_entry_new();
165 gtk_entry_set_width_chars( GTK_ENTRY(text), 11 );
166 gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
167 g_signal_connect( text, "key_press_event",
168 G_CALLBACK(config_key_keypress), NULL );
169 g_signal_connect( text, "button_press_event",
170 G_CALLBACK(config_key_buttonpress), NULL );
171 snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
172 g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
173 g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
174 gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
175 focus_chain = g_list_append( focus_chain, text );
176 text2 = gtk_entry_new();
177 gtk_entry_set_width_chars( GTK_ENTRY(text2), 11 );
178 gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
179 g_signal_connect( text2, "key_press_event",
180 G_CALLBACK(config_key_keypress), NULL );
181 g_signal_connect( text2, "button_press_event",
182 G_CALLBACK(config_key_buttonpress), NULL );
183 snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
184 g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
185 g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
186 gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
187 focus_chain = g_list_append( focus_chain, text2 );
188 if( conf[i].value != NULL ) {
189 gchar **parts = g_strsplit(conf[i].value,",",3);
190 if( parts[0] != NULL ) {
191 gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
192 if( parts[1] != NULL ) {
193 gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
199 gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
200 gtk_gui_run_property_dialog( _("Controller Configuration"), table, controller_config_done );
203 gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
205 maple_slot_data_t data = (maple_slot_data_t)user_data;
206 if( data->new_device != NULL ) {
208 for( i=0; maple_device_config[i].name != NULL; i++ ) {
209 if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
210 if( data->new_device == data->old_device ) {
211 // Make a copy at this point if we haven't already
212 data->new_device = data->old_device->clone(data->old_device);
214 maple_device_config[i].config_func(data->new_device);
218 if( maple_device_config[i].name == NULL ) {
219 gui_error_dialog( _("No configuration page available for device type") );
225 gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
227 maple_slot_data_t data = (maple_slot_data_t)user_data;
228 int active = gtk_combo_box_get_active(combo);
229 gboolean has_config = FALSE;
231 gchar *devname = gtk_combo_box_get_active_text(combo);
232 const maple_device_class_t devclz = maple_get_device_class(devname);
233 assert(devclz != NULL);
234 if( data->new_device != NULL ) {
235 if( data->new_device->device_class != devclz ) {
236 if( data->new_device != data->old_device ) {
237 data->new_device->destroy(data->new_device);
239 data->new_device = maple_new_device(devname);
242 data->new_device = maple_new_device(devname);
244 has_config = data->new_device != NULL && data->new_device->get_config != NULL;
246 if( data->new_device != NULL && data->new_device != data->old_device ) {
247 data->new_device->destroy(data->new_device);
249 data->new_device = NULL;
251 gtk_widget_set_sensitive(data->button, has_config);
255 void maple_dialog_done( GtkWidget *panel, gboolean isOK )
259 for( i=0; i<MAX_DEVICES; i++ ) {
260 if( maple_data[i].new_device != maple_data[i].old_device ) {
261 if( maple_data[i].old_device != NULL ) {
262 maple_detach_device(i,0);
264 if( maple_data[i].new_device != NULL ) {
265 maple_attach_device(maple_data[i].new_device, i, 0 );
269 lxdream_save_config();
272 for( i=0; i<MAX_DEVICES; i++ ) {
273 if( maple_data[i].new_device != NULL &&
274 maple_data[i].new_device != maple_data[i].old_device ) {
275 maple_data[i].new_device->destroy(maple_data[i].new_device);
282 GtkWidget *maple_panel_new()
284 GtkWidget *table = gtk_table_new(4, 3, TRUE);
286 const struct maple_device_class **devices = maple_get_device_classes();
288 for( i=0; i< MAX_DEVICES; i++ ) {
290 GtkWidget *combo, *button;
292 maple_device_t device = maple_get_device(i,0);
293 snprintf( buf, sizeof(buf), _("Slot %d."), i );
294 gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
295 combo = gtk_combo_box_new_text();
296 gtk_combo_box_append_text( GTK_COMBO_BOX(combo), _("<empty>") );
297 for( j=0; devices[j] != NULL; j++ ) {
298 gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
299 if( device != NULL && device->device_class == devices[j] ) {
303 gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
304 gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
305 button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
306 gtk_widget_set_sensitive(button, active != 0 && device->get_config != NULL);
307 gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
309 maple_data[i].old_device = device;
310 maple_data[i].new_device = device;
311 maple_data[i].combo = combo;
312 maple_data[i].button = button;
313 g_signal_connect( button, "clicked",
314 G_CALLBACK( maple_properties_activated ), &maple_data[i] );
315 g_signal_connect( combo, "changed",
316 G_CALLBACK( maple_device_changed ), &maple_data[i] );
322 void maple_dialog_run( )
324 gtk_gui_run_property_dialog( _("Controller Settings"), maple_panel_new(), maple_dialog_done );
.