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:31:07 2007 +0000 (16 years ago)
permissions -rw-r--r--
last change Rename mmr_win.c to mmio_win.c
view annotate diff log raw
     1 /**
     2  * $Id: ctrl_dlg.c,v 1.3 2007-10-21 05:21:35 nkeynes Exp $
     3  *
     4  * Define the main (emu) GTK window, along with its menubars,
     5  * toolbars, etc.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     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.
    13  *
    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.
    18  */
    20 #include <assert.h>
    21 #include <gtk/gtk.h>
    23 #include "dream.h"
    24 #include "gui/gtkui.h"
    25 #include "maple/maple.h"
    27 #define MAX_DEVICES 4
    29 static void controller_device_configure(maple_device_t device);
    31 struct maple_config_class {
    32     const char *name;
    33     void (*config_func)(maple_device_t device);
    34 };
    36 typedef struct maple_slot_data {
    37     maple_device_t old_device;
    38     maple_device_t new_device;
    39     GtkWidget *button;
    40     GtkWidget *combo;
    41 } *maple_slot_data_t;
    43 static struct maple_config_class maple_device_config[] = {
    44     { "Sega Controller", controller_device_configure },
    45     { NULL, NULL } };
    47 static struct maple_slot_data maple_data[MAX_DEVICES];
    49 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
    50 {
    51     GdkKeymap *keymap = gdk_keymap_get_default();
    52     guint keyval;
    54     gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    55 					 NULL, NULL, NULL );
    56     gtk_entry_set_text( GTK_ENTRY(widget), gdk_keyval_name(keyval) );
    57     return TRUE;
    58 }
    60 static void controller_config_done( GtkWidget *panel, gboolean isOK )
    61 {
    64 }
    66 static void controller_device_configure( maple_device_t device )
    67 {
    68     lxdream_config_entry_t conf = device->get_config(device);
    69     int count, i;
    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;
    75 	int x=0;
    76 	int y=i;
    77 	if( i >= (count+1)>>1 ) {
    78 	    x = 3;
    79 	    y -= (count+1)>>1;
    80 	}
    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]) );
   100 	    }
   101 	}
   102 	g_strfreev(parts);
   103     }
   104     gtk_gui_run_property_dialog( "Controller Configuration", table, controller_config_done );
   105 }
   108 gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
   109 {
   110     maple_slot_data_t data = (maple_slot_data_t)user_data;
   111     if( data->new_device != NULL ) {
   112 	int i;
   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);
   116 		break;
   117 	    }
   118 	}
   119 	if( maple_device_config[i].name == NULL ) {
   120 	    gui_error_dialog( "No configuration page available for device type" );
   121 	}
   122     }
   123     return TRUE;
   124 }
   126 gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
   127 {
   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);
   131     if( 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);
   139 	    }
   140 	} else {
   141 	    data->new_device = maple_new_device(devname);
   142 	}
   143     } else {
   144 	if( data->new_device != NULL && data->new_device != data->old_device ) {
   145 	    data->new_device->destroy(data->new_device);
   146 	}
   147 	data->new_device = NULL;
   148     }
   149     return TRUE;
   150 }
   152 void maple_commit_changes( )
   153 {
   154     int i;
   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);
   159 	    }
   160 	    if( maple_data[i].new_device != NULL ) {
   161 		maple_attach_device(maple_data[i].new_device, i, 0 );
   162 	    }
   163 	}
   164     }
   165     lxdream_save_config();
   166 }
   168 void maple_cancel_changes( )
   169 {
   170     int i;
   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);
   175 	}
   176     }
   177 }
   179 GtkWidget *maple_panel_new()
   180 {
   181     GtkWidget *table = gtk_table_new(4, 3, TRUE);
   182     GtkTreeIter iter;
   183     int i,j;
   184     const struct maple_device_class **devices = maple_get_device_classes();
   186     for( i=0; i< MAX_DEVICES; i++ ) {
   187 	char buf[12];
   188 	GtkWidget *combo, *button;
   189 	int active = 0;
   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] ) {
   198 		active = j+1;
   199 	    }
   200 	}
   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] );
   216     }
   217     return table;
   218 }
   220 void maple_dialog_run( GtkWindow *parent )
   221 {
   222     gint result = gtk_gui_run_property_dialog( "Controller Settings", maple_panel_new(), NULL );
   223     if( result == GTK_RESPONSE_ACCEPT ) {
   224 	maple_commit_changes();
   225     } else {
   226 	maple_cancel_changes();
   227     }
   228 }
.