Search
lxdream.org :: lxdream/src/gui/ctrl_dlg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/ctrl_dlg.c
changeset 455:3080881d00d4
prev450:207461e79f21
next460:a0c865b74c63
author nkeynes
date Sun Oct 21 05:21:35 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change More GUI WIP
file annotate diff log raw
nkeynes@447
     1
/**
nkeynes@455
     2
 * $Id: ctrl_dlg.c,v 1.3 2007-10-21 05:21:35 nkeynes Exp $
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@447
    22
nkeynes@447
    23
#include "dream.h"
nkeynes@447
    24
#include "gui/gtkui.h"
nkeynes@447
    25
#include "maple/maple.h"
nkeynes@447
    26
nkeynes@447
    27
#define MAX_DEVICES 4
nkeynes@447
    28
nkeynes@455
    29
static void controller_device_configure(maple_device_t device);
nkeynes@455
    30
nkeynes@455
    31
struct maple_config_class {
nkeynes@455
    32
    const char *name;
nkeynes@455
    33
    void (*config_func)(maple_device_t device);
nkeynes@455
    34
};
nkeynes@455
    35
nkeynes@447
    36
typedef struct maple_slot_data {
nkeynes@447
    37
    maple_device_t old_device;
nkeynes@447
    38
    maple_device_t new_device;
nkeynes@447
    39
    GtkWidget *button;
nkeynes@447
    40
    GtkWidget *combo;
nkeynes@447
    41
} *maple_slot_data_t;
nkeynes@447
    42
nkeynes@455
    43
static struct maple_config_class maple_device_config[] = {
nkeynes@455
    44
    { "Sega Controller", controller_device_configure },
nkeynes@455
    45
    { NULL, NULL } };
nkeynes@455
    46
nkeynes@447
    47
static struct maple_slot_data maple_data[MAX_DEVICES];
nkeynes@447
    48
nkeynes@455
    49
static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
nkeynes@455
    50
{
nkeynes@455
    51
    GdkKeymap *keymap = gdk_keymap_get_default();
nkeynes@455
    52
    guint keyval;
nkeynes@455
    53
    
nkeynes@455
    54
    gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
nkeynes@455
    55
					 NULL, NULL, NULL );
nkeynes@455
    56
    gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
nkeynes@455
    57
    return TRUE;
nkeynes@455
    58
}
nkeynes@447
    59
nkeynes@455
    60
static void controller_config_done( GtkWidget *panel, gboolean isOK )
nkeynes@455
    61
{
nkeynes@455
    62
    
nkeynes@455
    63
nkeynes@455
    64
}
nkeynes@455
    65
nkeynes@455
    66
static void controller_device_configure( maple_device_t device )
nkeynes@455
    67
{
nkeynes@455
    68
    lxdream_config_entry_t conf = device->get_config(device);
nkeynes@455
    69
    int count, i;
nkeynes@455
    70
    for( count=0; conf[count].key != NULL; count++ );
nkeynes@455
    71
nkeynes@455
    72
    GtkWidget *table = gtk_table_new( (count+1)>>1, 6, FALSE );
nkeynes@455
    73
    for( i=0; i<count; i++ ) {
nkeynes@455
    74
	GtkWidget *text, *text2;
nkeynes@455
    75
	int x=0;
nkeynes@455
    76
	int y=i;
nkeynes@455
    77
	if( i >= (count+1)>>1 ) {
nkeynes@455
    78
	    x = 3;
nkeynes@455
    79
	    y -= (count+1)>>1;
nkeynes@455
    80
	}
nkeynes@455
    81
	gtk_table_attach( GTK_TABLE(table), gtk_label_new(conf[i].key), x, x+1, y, y+1, 
nkeynes@455
    82
			  GTK_SHRINK, GTK_SHRINK, 0, 0 );
nkeynes@455
    83
	gchar **parts = g_strsplit(conf[i].value,",",3);
nkeynes@455
    84
	
nkeynes@455
    85
	text = gtk_entry_new();
nkeynes@455
    86
	gtk_entry_set_width_chars( GTK_ENTRY(text), 8 );
nkeynes@455
    87
	g_signal_connect( text, "key_press_event", 
nkeynes@455
    88
			  G_CALLBACK(config_key_keypress), NULL );
nkeynes@455
    89
	gtk_table_attach_defaults( GTK_TABLE(table), text, x+1, x+2, y, y+1);
nkeynes@455
    90
nkeynes@455
    91
	text2 = gtk_entry_new();
nkeynes@455
    92
	gtk_entry_set_width_chars( GTK_ENTRY(text2), 8 );
nkeynes@455
    93
	g_signal_connect( text2, "key_press_event", 
nkeynes@455
    94
			  G_CALLBACK(config_key_keypress), NULL );
nkeynes@455
    95
	gtk_table_attach_defaults( GTK_TABLE(table), text2, x+2, x+3, y, y+1);
nkeynes@455
    96
	if( parts[0] != NULL ) {
nkeynes@455
    97
	    gtk_entry_set_text( GTK_ENTRY(text), g_strstrip(parts[0]) );
nkeynes@455
    98
	    if( parts[1] != NULL ) {
nkeynes@455
    99
		gtk_entry_set_text( GTK_ENTRY(text2), g_strstrip(parts[1]) );
nkeynes@455
   100
	    }
nkeynes@455
   101
	}
nkeynes@455
   102
	g_strfreev(parts);
nkeynes@455
   103
    }
nkeynes@455
   104
    gtk_gui_run_property_dialog( "Controller Configuration", table, controller_config_done );
nkeynes@455
   105
}
nkeynes@455
   106
nkeynes@455
   107
nkeynes@455
   108
gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
nkeynes@447
   109
{
nkeynes@447
   110
    maple_slot_data_t data = (maple_slot_data_t)user_data;
nkeynes@455
   111
    if( data->new_device != NULL ) {
nkeynes@455
   112
	int i;
nkeynes@455
   113
	for( i=0; maple_device_config[i].name != NULL; i++ ) {
nkeynes@455
   114
	    if( strcmp(data->new_device->device_class->name, maple_device_config[i].name) == 0 ) {
nkeynes@455
   115
		maple_device_config[i].config_func(data->new_device);
nkeynes@455
   116
		break;
nkeynes@455
   117
	    }
nkeynes@455
   118
	}
nkeynes@455
   119
	if( maple_device_config[i].name == NULL ) {
nkeynes@455
   120
	    gui_error_dialog( "No configuration page available for device type" );
nkeynes@455
   121
	}
nkeynes@455
   122
    }
nkeynes@455
   123
    return TRUE;
nkeynes@447
   124
}
nkeynes@447
   125
nkeynes@455
   126
gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
nkeynes@447
   127
{
nkeynes@447
   128
    maple_slot_data_t data = (maple_slot_data_t)user_data;
nkeynes@447
   129
    int active = gtk_combo_box_get_active(combo);
nkeynes@447
   130
    gtk_widget_set_sensitive(data->button, active != 0);
nkeynes@447
   131
    if( active != 0 ) {
nkeynes@447
   132
	gchar *devname = gtk_combo_box_get_active_text(combo);
nkeynes@447
   133
	const maple_device_class_t devclz = maple_get_device_class(devname);
nkeynes@447
   134
	assert(devclz != NULL);
nkeynes@447
   135
	if( data->new_device != NULL ) {
nkeynes@447
   136
	    if( data->new_device->device_class != devclz ) {
nkeynes@447
   137
		data->new_device->destroy(data->new_device);
nkeynes@447
   138
		data->new_device = maple_new_device(devname);
nkeynes@447
   139
	    }
nkeynes@447
   140
	} else {
nkeynes@447
   141
	    data->new_device = maple_new_device(devname);
nkeynes@447
   142
	}
nkeynes@447
   143
    } else {
nkeynes@447
   144
	if( data->new_device != NULL && data->new_device != data->old_device ) {
nkeynes@447
   145
	    data->new_device->destroy(data->new_device);
nkeynes@447
   146
	}
nkeynes@447
   147
	data->new_device = NULL;
nkeynes@447
   148
    }
nkeynes@455
   149
    return TRUE;
nkeynes@447
   150
}
nkeynes@447
   151
nkeynes@455
   152
void maple_commit_changes( )
nkeynes@447
   153
{
nkeynes@447
   154
    int i;
nkeynes@447
   155
    for( i=0; i<MAX_DEVICES; i++ ) {
nkeynes@447
   156
	if( maple_data[i].new_device != maple_data[i].old_device ) {
nkeynes@447
   157
	    if( maple_data[i].old_device != NULL ) {
nkeynes@447
   158
		maple_detach_device(i,0);
nkeynes@447
   159
	    }
nkeynes@447
   160
	    if( maple_data[i].new_device != NULL ) {
nkeynes@447
   161
		maple_attach_device(maple_data[i].new_device, i, 0 );
nkeynes@447
   162
	    }
nkeynes@447
   163
	}
nkeynes@447
   164
    }
nkeynes@450
   165
    lxdream_save_config();
nkeynes@447
   166
}
nkeynes@447
   167
nkeynes@455
   168
void maple_cancel_changes( )
nkeynes@447
   169
{
nkeynes@447
   170
    int i;
nkeynes@447
   171
    for( i=0; i<MAX_DEVICES; i++ ) {
nkeynes@447
   172
	if( maple_data[i].new_device != NULL && 
nkeynes@447
   173
	    maple_data[i].new_device != maple_data[i].old_device ) {
nkeynes@447
   174
	    maple_data[i].new_device->destroy(maple_data[i].new_device);
nkeynes@447
   175
	}
nkeynes@447
   176
    }
nkeynes@447
   177
}
nkeynes@447
   178
nkeynes@455
   179
GtkWidget *maple_panel_new()
nkeynes@447
   180
{
nkeynes@447
   181
    GtkWidget *table = gtk_table_new(4, 3, TRUE);
nkeynes@447
   182
    GtkTreeIter iter;
nkeynes@447
   183
    int i,j;
nkeynes@447
   184
    const struct maple_device_class **devices = maple_get_device_classes();
nkeynes@447
   185
nkeynes@447
   186
    for( i=0; i< MAX_DEVICES; i++ ) {
nkeynes@447
   187
	char buf[12];
nkeynes@447
   188
	GtkWidget *combo, *button;
nkeynes@447
   189
	int active = 0;
nkeynes@447
   190
	maple_device_t device = maple_get_device(i,0);
nkeynes@447
   191
	sprintf( buf, "Slot %d.", i );
nkeynes@447
   192
	gtk_table_attach_defaults( GTK_TABLE(table), gtk_label_new(buf), 0, 1, i, i+1 );
nkeynes@447
   193
	combo = gtk_combo_box_new_text();
nkeynes@447
   194
	gtk_combo_box_append_text( GTK_COMBO_BOX(combo), "<empty>" );
nkeynes@447
   195
	for( j=0; devices[j] != NULL; j++ ) {
nkeynes@447
   196
	    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), devices[j]->name);
nkeynes@447
   197
	    if( device != NULL && device->device_class == devices[j] ) {
nkeynes@447
   198
		active = j+1;
nkeynes@447
   199
	    }
nkeynes@447
   200
	}
nkeynes@447
   201
	gtk_combo_box_set_active(GTK_COMBO_BOX(combo), active);
nkeynes@447
   202
	gtk_table_attach_defaults( GTK_TABLE(table), combo, 1, 2, i, i+1 );
nkeynes@447
   203
	button = gtk_button_new_from_stock( GTK_STOCK_PROPERTIES );
nkeynes@447
   204
	gtk_widget_set_sensitive(button, active != 0);
nkeynes@447
   205
	gtk_table_attach_defaults( GTK_TABLE(table), button, 2, 3, i, i+1 );
nkeynes@447
   206
nkeynes@447
   207
	maple_data[i].old_device = device;
nkeynes@447
   208
	maple_data[i].new_device = device;
nkeynes@447
   209
	maple_data[i].combo = combo;
nkeynes@447
   210
	maple_data[i].button = button;
nkeynes@450
   211
	g_signal_connect( button, "clicked", 
nkeynes@455
   212
			  G_CALLBACK( maple_properties_activated ), &maple_data[i] );
nkeynes@447
   213
	g_signal_connect( combo, "changed", 
nkeynes@455
   214
			  G_CALLBACK( maple_device_changed ), &maple_data[i] );
nkeynes@447
   215
nkeynes@447
   216
    }
nkeynes@447
   217
    return table;
nkeynes@447
   218
}
nkeynes@447
   219
nkeynes@455
   220
void maple_dialog_run( GtkWindow *parent )
nkeynes@447
   221
{
nkeynes@455
   222
    gint result = gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), NULL );
nkeynes@447
   223
    if( result == GTK_RESPONSE_ACCEPT ) {
nkeynes@455
   224
	maple_commit_changes();
nkeynes@447
   225
    } else {
nkeynes@455
   226
	maple_cancel_changes();
nkeynes@447
   227
    }
nkeynes@447
   228
}
.