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 628:bd46e2c4c479
prev616:527ab167712b
next669:ab344e42bca9
author nkeynes
date Fri Feb 08 00:59:15 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix premature deletion of the old device when changing devices
file annotate diff log raw
nkeynes@447
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@447
     3
 *
nkeynes@447
     4
 * Define the main (emu) GTK window, along with its menubars,
nkeynes@447
     5
 * toolbars, etc.
nkeynes@447
     6
 *
nkeynes@447
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@447
     8
 *
nkeynes@447
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@447
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@447
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@447
    12
 * (at your option) any later version.
nkeynes@447
    13
 *
nkeynes@447
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@447
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@447
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@447
    17
 * GNU General Public License for more details.
nkeynes@447
    18
 */
nkeynes@447
    19
nkeynes@447
    20
#include <assert.h>
nkeynes@447
    21
#include <gtk/gtk.h>
nkeynes@460
    22
#include <gdk/gdkkeysyms.h>
nkeynes@447
    23
nkeynes@614
    24
#include "lxdream.h"
nkeynes@614
    25
#include "display.h"
nkeynes@537
    26
#include "gtkui/gtkui.h"
nkeynes@447
    27
#include "maple/maple.h"
nkeynes@447
    28
nkeynes@447
    29
#define MAX_DEVICES 4
nkeynes@447
    30
nkeynes@455
    31
static void controller_device_configure(maple_device_t device);
nkeynes@455
    32
nkeynes@455
    33
struct maple_config_class {
nkeynes@455
    34
    const char *name;
nkeynes@455
    35
    void (*config_func)(maple_device_t device);
nkeynes@455
    36
};
nkeynes@455
    37
nkeynes@447
    38
typedef struct maple_slot_data {
nkeynes@447
    39
    maple_device_t old_device;
nkeynes@447
    40
    maple_device_t new_device;
nkeynes@447
    41
    GtkWidget *button;
nkeynes@447
    42
    GtkWidget *combo;
nkeynes@447
    43
} *maple_slot_data_t;
nkeynes@447
    44
nkeynes@455
    45
static struct maple_config_class maple_device_config[] = {
nkeynes@455
    46
    { "Sega Controller", controller_device_configure },
nkeynes@455
    47
    { NULL, NULL } };
nkeynes@455
    48
nkeynes@447
    49
static struct maple_slot_data maple_data[MAX_DEVICES];
nkeynes@447
    50
nkeynes@614
    51
static void config_keysym_hook( void *data, const gchar *keysym )
nkeynes@614
    52
{
nkeynes@614
    53
    GtkWidget *widget = (GtkWidget *)data;
nkeynes@614
    54
    gtk_entry_set_text( GTK_ENTRY(widget), keysym );
nkeynes@614
    55
    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
nkeynes@614
    56
    input_set_keysym_hook(NULL, NULL);
nkeynes@614
    57
}
nkeynes@614
    58
nkeynes@460
    59
static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
nkeynes@460
    60
{
nkeynes@460
    61
    gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
nkeynes@460
    62
    if( !keypress_mode ) {
nkeynes@508
    63
	gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
nkeynes@460
    64
	g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
nkeynes@614
    65
	input_set_keysym_hook(config_keysym_hook, widget);
nkeynes@460
    66
    }
nkeynes@460
    67
    return FALSE;
nkeynes@460
    68
}
nkeynes@460
    69
nkeynes@455
    70
static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
nkeynes@455
    71
{
nkeynes@460
    72
    gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
nkeynes@460
    73
    if( keypress_mode ) {
nkeynes@460
    74
	if( event->keyval == GDK_Escape ) {
nkeynes@460
    75
	    gtk_entry_set_text( GTK_ENTRY(widget), "" );
nkeynes@460
    76
	    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
nkeynes@460
    77
	    return TRUE;
nkeynes@460
    78
	}
nkeynes@460
    79
	GdkKeymap *keymap = gdk_keymap_get_default();
nkeynes@460
    80
	guint keyval;
nkeynes@460
    81
	
nkeynes@460
    82
	gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
nkeynes@460
    83
					     NULL, NULL, NULL );
nkeynes@460
    84
	gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
nkeynes@460
    85
	g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(FALSE) );
nkeynes@614
    86
	input_set_keysym_hook(NULL, NULL);
nkeynes@460
    87
	return TRUE;
nkeynes@460
    88
    } else {
nkeynes@460
    89
	switch( event->keyval ) {
nkeynes@460
    90
	case GDK_Return:
nkeynes@460
    91
	case GDK_KP_Enter:
nkeynes@508
    92
	    gtk_entry_set_text( GTK_ENTRY(widget), _("<press key>") );
nkeynes@460
    93
	    g_object_set_data( G_OBJECT(widget), "keypress_mode", GINT_TO_POINTER(TRUE) );
nkeynes@614
    94
	    input_set_keysym_hook(config_keysym_hook, widget);
nkeynes@460
    95
	    return TRUE;
nkeynes@460
    96
	case GDK_BackSpace:
nkeynes@460
    97
	case GDK_Delete:
nkeynes@460
    98
	    gtk_entry_set_text( GTK_ENTRY(widget), "" );
nkeynes@460
    99
	    return TRUE;
nkeynes@460
   100
	}
nkeynes@460
   101
	return FALSE;
nkeynes@460
   102
    }
nkeynes@460
   103
	    
nkeynes@455
   104
}
nkeynes@447
   105
nkeynes@455
   106
static void controller_config_done( GtkWidget *panel, gboolean isOK )
nkeynes@455
   107
{
nkeynes@460
   108
    if( isOK ) {
nkeynes@460
   109
	maple_device_t device = (maple_device_t)gtk_object_get_data( GTK_OBJECT(panel), "maple_device" );
nkeynes@460
   110
	lxdream_config_entry_t conf = device->get_config(device);
nkeynes@460
   111
	int i;
nkeynes@460
   112
	for( i=0; conf[i].key != NULL; i++ ) {
nkeynes@460
   113
	    char buf[64];
nkeynes@460
   114
	    GtkWidget *entry1, *entry2;
nkeynes@460
   115
	    const gchar *key1, *key2;
nkeynes@460
   116
	    snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
nkeynes@460
   117
	    entry1 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
nkeynes@460
   118
	    key1 = gtk_entry_get_text(GTK_ENTRY(entry1));
nkeynes@460
   119
	    snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
nkeynes@460
   120
	    entry2 = GTK_WIDGET(g_object_get_qdata( G_OBJECT(panel), g_quark_from_string(buf)));
nkeynes@460
   121
	    key2 = gtk_entry_get_text(GTK_ENTRY(entry2));
nkeynes@460
   122
	    if( key1 == NULL || key1[0] == '\0') {
nkeynes@460
   123
		lxdream_set_config_value( &conf[i], key2 );
nkeynes@460
   124
	    } else if( key2 == NULL || key2[0] == '\0') {
nkeynes@460
   125
		lxdream_set_config_value( &conf[i], key1 );
nkeynes@460
   126
	    } else {
nkeynes@460
   127
		char buf[64];
nkeynes@460
   128
		snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
nkeynes@460
   129
		lxdream_set_config_value( &conf[i], buf );
nkeynes@460
   130
	    }
nkeynes@460
   131
	}
nkeynes@460
   132
    }
nkeynes@614
   133
    input_set_keysym_hook(NULL, NULL);
nkeynes@455
   134
}
nkeynes@455
   135
nkeynes@455
   136
static void controller_device_configure( maple_device_t device )
nkeynes@455
   137
{
nkeynes@455
   138
    lxdream_config_entry_t conf = device->get_config(device);
nkeynes@455
   139
    int count, i;
nkeynes@455
   140
    for( count=0; conf[count].key != NULL; count++ );
nkeynes@455
   141
nkeynes@455
   142
    GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
nkeynes@460
   143
    GList *focus_chain = NULL;
nkeynes@460
   144
    gtk_object_set_data( GTK_OBJECT(table), "maple_device", device );
nkeynes@455
   145
    for( i=0; i<count; i++ ) {
nkeynes@455
   146
	GtkWidget *text, *text2;
nkeynes@460
   147
	char buf[64];
nkeynes@455
   148
	int x=0;
nkeynes@455
   149
	int y=i;
nkeynes@455
   150
	if( i >= (count+1)>>1 ) {
nkeynes@455
   151
	    x = 3;
nkeynes@455
   152
	    y -= (count+1)>>1;
nkeynes@455
   153
	}
nkeynes@455
   154
	gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1, 
nkeynes@455
   155
			  GTK_SHRINK, GTK_SHRINK, 0, 0 );
nkeynes@455
   156
	text = gtk_entry_new();
nkeynes@616
   157
	gtk_entry_set_width_chars( GTK_ENTRY(text), 11 );
nkeynes@460
   158
	gtk_entry_set_editable( GTK_ENTRY(text), FALSE );
nkeynes@455
   159
	g_signal_connect( text, "key_press_event", 
nkeynes@455
   160
			  G_CALLBACK(config_key_keypress), NULL );
nkeynes@460
   161
	g_signal_connect( text, "button_press_event",
nkeynes@460
   162
			  G_CALLBACK(config_key_buttonpress), NULL );
nkeynes@460
   163
	snprintf( buf, sizeof(buf), "%s.1", conf[i].key );
nkeynes@460
   164
	g_object_set_data( G_OBJECT(text), "keypress_mode", GINT_TO_POINTER(FALSE) );
nkeynes@460
   165
	g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text );
nkeynes@455
   166
	gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
nkeynes@460
   167
	focus_chain = g_list_append( focus_chain, text );
nkeynes@455
   168
	text2 = gtk_entry_new();
nkeynes@616
   169
	gtk_entry_set_width_chars( GTK_ENTRY(text2), 11 );
nkeynes@460
   170
	gtk_entry_set_editable( GTK_ENTRY(text2), FALSE );
nkeynes@455
   171
	g_signal_connect( text2, "key_press_event", 
nkeynes@455
   172
			  G_CALLBACK(config_key_keypress), NULL );
nkeynes@460
   173
	g_signal_connect( text2, "button_press_event",
nkeynes@460
   174
			  G_CALLBACK(config_key_buttonpress), NULL );
nkeynes@460
   175
	snprintf( buf, sizeof(buf), "%s.2", conf[i].key );
nkeynes@460
   176
	g_object_set_data( G_OBJECT(text2), "keypress_mode", GINT_TO_POINTER(FALSE) );
nkeynes@460
   177
	g_object_set_qdata( G_OBJECT(table), g_quark_from_string(buf), text2 );
nkeynes@455
   178
	gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
nkeynes@460
   179
	focus_chain = g_list_append( focus_chain, text2 );
nkeynes@460
   180
	if( conf[i].value != NULL ) {
nkeynes@460
   181
	    gchar **parts = g_strsplit(conf[i].value,",",3);
nkeynes@460
   182
	    if( parts[0] != NULL ) {
nkeynes@460
   183
		gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
nkeynes@460
   184
		if( parts[1] != NULL ) {
nkeynes@460
   185
		    gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
nkeynes@460
   186
		}
nkeynes@455
   187
	    }
nkeynes@460
   188
	    g_strfreev(parts);
nkeynes@455
   189
	}
nkeynes@455
   190
    }
nkeynes@460
   191
    gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
nkeynes@508
   192
    gtk_gui_run_property_dialog( _("Controller Configuration"), table, controller_config_done );
nkeynes@455
   193
}
nkeynes@455
   194
nkeynes@455
   195
gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
nkeynes@447
   196
{
nkeynes@447
   197
    maple_slot_data_t data = (maple_slot_data_t)user_data;
nkeynes@455
   198
    if( data->new_device != NULL ) {
nkeynes@455
   199
	int i;
nkeynes@455
   200
	for( i=0; maple_device_config[i].name != NULL; i++ ) {
nkeynes@455
   201
	    if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
nkeynes@460
   202
		if( data->new_device == data->old_device ) {
nkeynes@460
   203
		    // Make a copy at this point if we haven't already
nkeynes@460
   204
		    data->new_device = data->old_device->clone(data->old_device);
nkeynes@460
   205
		}
nkeynes@455
   206
		maple_device_config[i].config_func(data->new_device);
nkeynes@455
   207
		break;
nkeynes@455
   208
	    }
nkeynes@455
   209
	}
nkeynes@455
   210
	if( maple_device_config[i].name == NULL ) {
nkeynes@508
   211
	    gui_error_dialog( _("No configuration page available for device type") );
nkeynes@455
   212
	}
nkeynes@455
   213
    }
nkeynes@455
   214
    return TRUE;
nkeynes@447
   215
}
nkeynes@447
   216
nkeynes@455
   217
gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
nkeynes@447
   218
{
nkeynes@447
   219
    maple_slot_data_t data = (maple_slot_data_t)user_data;
nkeynes@447
   220
    int active = gtk_combo_box_get_active(combo);
nkeynes@608
   221
    gboolean has_config = FALSE;
nkeynes@447
   222
    if( active != 0 ) {
nkeynes@447
   223
	gchar *devname = gtk_combo_box_get_active_text(combo);
nkeynes@447
   224
	const maple_device_class_t devclz = maple_get_device_class(devname);
nkeynes@447
   225
	assert(devclz != NULL);
nkeynes@447
   226
	if( data->new_device != NULL ) {
nkeynes@447
   227
	    if( data->new_device->device_class != devclz ) {
nkeynes@628
   228
		if( data->new_device != data->old_device ) {
nkeynes@628
   229
		    data->new_device->destroy(data->new_device);
nkeynes@628
   230
		}
nkeynes@447
   231
		data->new_device = maple_new_device(devname);
nkeynes@447
   232
	    }
nkeynes@447
   233
	} else {
nkeynes@447
   234
	    data->new_device = maple_new_device(devname);
nkeynes@447
   235
	}
nkeynes@608
   236
	has_config = data->new_device != NULL && data->new_device->get_config != NULL;
nkeynes@447
   237
    } else {
nkeynes@447
   238
	if( data->new_device != NULL && data->new_device != data->old_device ) {
nkeynes@447
   239
	    data->new_device->destroy(data->new_device);
nkeynes@447
   240
	}
nkeynes@447
   241
	data->new_device = NULL;
nkeynes@447
   242
    }
nkeynes@608
   243
    gtk_widget_set_sensitive(data->button, has_config);
nkeynes@455
   244
    return TRUE;
nkeynes@447
   245
}
nkeynes@447
   246
nkeynes@460
   247
void maple_dialog_done( GtkWidget *panel, gboolean isOK )
nkeynes@447
   248
{
nkeynes@460
   249
    if( isOK ) {
nkeynes@460
   250
	int i;
nkeynes@460
   251
	for( i=0; i<MAX_DEVICES; i++ ) {
nkeynes@460
   252
	    if( maple_data[i].new_device != maple_data[i].old_device ) {
nkeynes@460
   253
		if( maple_data[i].old_device != NULL ) {
nkeynes@460
   254
		    maple_detach_device(i,0);
nkeynes@460
   255
		}
nkeynes@460
   256
		if( maple_data[i].new_device != NULL ) {
nkeynes@460
   257
		    maple_attach_device(maple_data[i].new_device, i, 0 );
nkeynes@460
   258
		}
nkeynes@447
   259
	    }
nkeynes@460
   260
	}
nkeynes@460
   261
	lxdream_save_config();
nkeynes@460
   262
    } else {
nkeynes@460
   263
	int i;
nkeynes@460
   264
	for( i=0; i<MAX_DEVICES; i++ ) {
nkeynes@460
   265
	    if( maple_data[i].new_device != NULL && 
nkeynes@460
   266
		maple_data[i].new_device != maple_data[i].old_device ) {
nkeynes@460
   267
		maple_data[i].new_device->destroy(maple_data[i].new_device);
nkeynes@447
   268
	    }
nkeynes@447
   269
	}
nkeynes@447
   270
    }
nkeynes@447
   271
nkeynes@447
   272
}
nkeynes@447
   273
nkeynes@455
   274
GtkWidget *maple_panel_new()
nkeynes@447
   275
{
nkeynes@447
   276
    GtkWidget *table = gtk_table_new(4, 3, TRUE);
nkeynes@447
   277
    int i,j;
nkeynes@447
   278
    const struct maple_device_class **devices = maple_get_device_classes();
nkeynes@447
   279
nkeynes@447
   280
    for( i=0; i< MAX_DEVICES; i++ ) {
nkeynes@447
   281
	char buf[12];
nkeynes@447
   282
	GtkWidget *combo, *button;
nkeynes@447
   283
	int active = 0;
nkeynes@447
   284
	maple_device_t device = maple_get_device(i,0);
nkeynes@508
   285
	sprintf( buf, _("Slot %d."), i );
nkeynes@447
   286
	gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
nkeynes@447
   287
	combo = gtk_combo_box_new_text();
nkeynes@508
   288
	gtk_combo_box_append_text( GTK_COMBO_BOX(combo), _("<empty>") );
nkeynes@447
   289
	for( j=0; devices[j] != NULL; j++ ) {
nkeynes@447
   290
	    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
nkeynes@447
   291
	    if( device != NULL && device->device_class == devices[j] ) {
nkeynes@447
   292
		active = j+1;
nkeynes@447
   293
	    }
nkeynes@447
   294
	}
nkeynes@447
   295
	gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
nkeynes@447
   296
	gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
nkeynes@447
   297
	button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
nkeynes@608
   298
	gtk_widget_set_sensitive(button, active != 0 && device->get_config != NULL);
nkeynes@447
   299
	gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
nkeynes@447
   300
nkeynes@447
   301
	maple_data[i].old_device = device;
nkeynes@447
   302
	maple_data[i].new_device = device;
nkeynes@447
   303
	maple_data[i].combo = combo;
nkeynes@447
   304
	maple_data[i].button = button;
nkeynes@450
   305
	g_signal_connect( button, "clicked", 
nkeynes@455
   306
			  G_CALLBACK( maple_properties_activated ), &maple_data[i] );
nkeynes@447
   307
	g_signal_connect( combo, "changed", 
nkeynes@455
   308
			  G_CALLBACK( maple_device_changed ), &maple_data[i] );
nkeynes@447
   309
nkeynes@447
   310
    }
nkeynes@447
   311
    return table;
nkeynes@447
   312
}
nkeynes@447
   313
nkeynes@460
   314
void maple_dialog_run( )
nkeynes@447
   315
{
nkeynes@508
   316
    gtk_gui_run_property_dialog( _("Controller Settings"), maple_panel_new(), maple_dialog_done );
nkeynes@447
   317
}
.