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