Search
lxdream.org :: lxdream/src/gtkui/gtk_ctrl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtk_ctrl.c
changeset 849:bbe26d798fc2
prev770:429ff505c450
next852:2b4a5e3575b9
author nkeynes
date Mon Sep 08 05:13:51 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Refactor mouse event management - button events are now usable for controllers
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <string.h>
    22 #include <gtk/gtk.h>
    23 #include <gdk/gdkkeysyms.h>
    25 #include "lxdream.h"
    26 #include "display.h"
    27 #include "gtkui/gtkui.h"
    28 #include "maple/maple.h"
    30 #define MAX_DEVICES 4
    32 static void controller_device_configure(maple_device_t device);
    34 struct maple_config_class {
    35     const char *name;
    36     void (*config_func)(maple_device_t device);
    37 };
    39 typedef struct maple_slot_data {
    40     maple_device_t old_device;
    41     maple_device_t new_device;
    42     GtkWidget *button;
    43     GtkWidget *combo;
    44 } *maple_slot_data_t;
    46 static struct maple_config_class maple_device_config[] = {
    47         { "Sega Controller", controller_device_configure },
    48         { NULL, NULL } };
    50 static struct maple_slot_data maple_data[MAX_DEVICES];
    52 static void config_keysym_hook( void *data, const gchar *keysym )
    53 {
    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);
    58 }
    60 static gboolean config_key_buttonpress( GtkWidget *widget, GdkEventButton *event, gpointer user_data )
    61 {
    62     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    63     if( keypress_mode ) {
    64         gchar *keysym = input_keycode_to_keysym( &system_mouse_driver, event->button);
    65         if( keysym != NULL ) {
    66             config_keysym_hook( widget, keysym );
    67             g_free(keysym);
    68         }
    69         return TRUE;
    70     } else {
    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);
    74     }
    75     return FALSE;
    76 }
    78 static gboolean config_key_keypress( GtkWidget *widget, GdkEventKey *event, gpointer user_data )
    79 {
    80     gboolean keypress_mode = GPOINTER_TO_INT(g_object_get_data( G_OBJECT(widget), "keypress_mode"));
    81     if( 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) );
    85             return TRUE;
    86         }
    87         GdkKeymap *keymap = gdk_keymap_get_default();
    88         guint keyval;
    90         gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
    91                                              NULL, NULL, NULL );
    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);
    95         return TRUE;
    96     } else {
    97         switch( event->keyval ) {
    98         case GDK_Return:
    99         case GDK_KP_Enter:
   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);
   103             return TRUE;
   104         case GDK_BackSpace:
   105         case GDK_Delete:
   106             gtk_entry_set_text( GTK_ENTRY(widget), "" );
   107             return TRUE;
   108         }
   109         return FALSE;
   110     }
   112 }
   114 static void controller_config_done( GtkWidget *panel, gboolean isOK )
   115 {
   116     if( 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);
   119         int i;
   120         for( i=0; conf[i].key != NULL; i++ ) {
   121             char buf[64];
   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 );
   134             } else {
   135                 char buf[64];
   136                 snprintf( buf, sizeof(buf), "%s, %s", key1, key2 );
   137                 lxdream_set_config_value( &conf[i], buf );
   138             }
   139         }
   140     }
   141     input_set_keysym_hook(NULL, NULL);
   142 }
   144 static void controller_device_configure( maple_device_t device )
   145 {
   146     lxdream_config_entry_t conf = maple_get_device_config(device);
   147     int count, i;
   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;
   155         char buf[64];
   156         int x=0;
   157         int y=i;
   158         if( i >= (count+1)>>1 ) {
   159             x = 3;
   160             y -= (count+1)>>1;
   161         }
   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]) );
   194                 }
   195             }
   196             g_strfreev(parts);
   197         }
   198     }
   199     gtk_container_set_focus_chain( GTK_CONTAINER(table), focus_chain );
   200     gtk_gui_run_property_dialog( _("Controller Configuration"), table, controller_config_done );
   201 }
   203 gboolean maple_properties_activated( GtkButton *button, gpointer user_data )
   204 {
   205     maple_slot_data_t data = (maple_slot_data_t)user_data;
   206     if( data->new_device != NULL ) {
   207         int i;
   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);
   213                 }
   214                 maple_device_config[i].config_func(data->new_device);
   215                 break;
   216             }
   217         }
   218         if( maple_device_config[i].name == NULL ) {
   219             gui_error_dialog( _("No configuration page available for device type") );
   220         }
   221     }
   222     return TRUE;
   223 }
   225 gboolean maple_device_changed( GtkComboBox *combo, gpointer user_data )
   226 {
   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;
   230     if( active != 0 ) {
   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);
   238                 }
   239                 data->new_device = maple_new_device(devname);
   240             }
   241         } else {
   242             data->new_device = maple_new_device(devname);
   243         }
   244         has_config = data->new_device != NULL && data->new_device->get_config != NULL;
   245     } else {
   246         if( data->new_device != NULL && data->new_device != data->old_device ) {
   247             data->new_device->destroy(data->new_device);
   248         }
   249         data->new_device = NULL;
   250     }
   251     gtk_widget_set_sensitive(data->button, has_config);
   252     return TRUE;
   253 }
   255 void maple_dialog_done( GtkWidget *panel, gboolean isOK )
   256 {
   257     if( isOK ) {
   258         int i;
   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);
   263                 }
   264                 if( maple_data[i].new_device != NULL ) {
   265                     maple_attach_device(maple_data[i].new_device, i, 0 );
   266                 }
   267             }
   268         }
   269         lxdream_save_config();
   270     } else {
   271         int i;
   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);
   276             }
   277         }
   278     }
   280 }
   282 GtkWidget *maple_panel_new()
   283 {
   284     GtkWidget *table = gtk_table_new(4, 3, TRUE);
   285     int i,j;
   286     const struct maple_device_class **devices = maple_get_device_classes();
   288     for( i=0; i< MAX_DEVICES; i++ ) {
   289         char buf[16];
   290         GtkWidget *combo, *button;
   291         int active = 0;
   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] ) {
   300                 active = j+1;
   301             }
   302         }
   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] );
   318     }
   319     return table;
   320 }
   322 void maple_dialog_run( )
   323 {
   324     gtk_gui_run_property_dialog( _("Controller Settings"), maple_panel_new(), maple_dialog_done );
   325 }
.