Search
lxdream.org :: lxdream/src/gtkui/gtkui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtkui.c
changeset 1036:af7b0c5905dd
prev1019:87f191f92f8f
next1040:9e3e41eab2db
author nkeynes
date Wed Jun 24 06:06:40 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Support shell substitutions in config paths
Keep track of last folder in file dialogs
Fix out-of-dateness in GTK path dialog
file annotate diff log raw
nkeynes@435
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@435
     3
 *
nkeynes@435
     4
 * Core GTK-based user interface
nkeynes@435
     5
 *
nkeynes@435
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@435
     7
 *
nkeynes@435
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@435
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@435
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@435
    11
 * (at your option) any later version.
nkeynes@435
    12
 *
nkeynes@435
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@435
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@435
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@435
    16
 * GNU General Public License for more details.
nkeynes@435
    17
 */
nkeynes@435
    18
nkeynes@678
    19
#include <time.h>
nkeynes@678
    20
#include <stdlib.h>
nkeynes@678
    21
#include <unistd.h>
nkeynes@437
    22
#include <sys/time.h>
nkeynes@618
    23
#include <gtk/gtkversion.h>
nkeynes@678
    24
#include "lxdream.h"
nkeynes@435
    25
#include "dreamcast.h"
nkeynes@678
    26
#include "dream.h"
nkeynes@477
    27
#include "display.h"
nkeynes@464
    28
#include "gdrom/gdrom.h"
nkeynes@537
    29
#include "gtkui/gtkui.h"
nkeynes@435
    30
nkeynes@435
    31
void gtk_gui_start( void );
nkeynes@435
    32
void gtk_gui_stop( void );
nkeynes@435
    33
void gtk_gui_alloc_resources ( void );
nkeynes@435
    34
uint32_t gtk_gui_run_slice( uint32_t nanosecs );
nkeynes@435
    35
nkeynes@435
    36
struct dreamcast_module gtk_gui_module = { "gui", NULL,
nkeynes@736
    37
        gtk_gui_update, 
nkeynes@736
    38
        gtk_gui_start, 
nkeynes@736
    39
        gtk_gui_run_slice, 
nkeynes@736
    40
        gtk_gui_stop, 
nkeynes@736
    41
        NULL, NULL };
nkeynes@435
    42
nkeynes@435
    43
/**
nkeynes@435
    44
 * Single-instance windows (at most one)
nkeynes@435
    45
 */
nkeynes@435
    46
static main_window_t main_win = NULL;
nkeynes@435
    47
static debug_window_t debug_win = NULL;
nkeynes@435
    48
static mmio_window_t mmio_win = NULL;
nkeynes@435
    49
nkeynes@435
    50
/**
nkeynes@455
    51
 * UIManager and action helpers
nkeynes@455
    52
 */
nkeynes@455
    53
static GtkUIManager *global_ui_manager;
nkeynes@455
    54
static GtkActionGroup *global_action_group;
nkeynes@455
    55
nkeynes@455
    56
/**
nkeynes@435
    57
 * Count of running nanoseconds - used to cut back on the GUI runtime
nkeynes@435
    58
 */
nkeynes@435
    59
static uint32_t gtk_gui_nanos = 0;
nkeynes@622
    60
static uint32_t gtk_gui_ticks = 0;
nkeynes@437
    61
static struct timeval gtk_gui_lasttv;
nkeynes@435
    62
nkeynes@530
    63
static gboolean gtk_gui_init_ok = FALSE;
nkeynes@530
    64
nkeynes@455
    65
#define ENABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,TRUE)
nkeynes@455
    66
#define DISABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,FALSE)
nkeynes@455
    67
nkeynes@455
    68
// UI Actions
nkeynes@455
    69
static const GtkActionEntry ui_actions[] = {
nkeynes@736
    70
        { "FileMenu", NULL, N_("_File") },
nkeynes@736
    71
        { "SettingsMenu", NULL, N_("_Settings") },
nkeynes@736
    72
        { "HelpMenu", NULL, N_("_Help") },
nkeynes@736
    73
        { "LoadBinary", NULL, N_("Load _Binary..."), NULL, N_("Load and run a program binary"), G_CALLBACK(load_binary_action_callback) },
nkeynes@736
    74
        { "Reset", GTK_STOCK_REFRESH, N_("_Reset"), "<control>R", N_("Reset dreamcast"), G_CALLBACK(reset_action_callback) },
nkeynes@736
    75
        { "Pause", GTK_STOCK_MEDIA_PAUSE, N_("_Pause"), NULL, N_("Pause dreamcast"), G_CALLBACK(pause_action_callback) },
nkeynes@736
    76
        { "Run", GTK_STOCK_MEDIA_PLAY, N_("Resume"), NULL, N_("Resume"), G_CALLBACK(resume_action_callback) },
nkeynes@773
    77
        { "LoadState", GTK_STOCK_REVERT_TO_SAVED, N_("_Load State..."), "F4", N_("Load an lxdream save state"), G_CALLBACK(load_state_action_callback) },
nkeynes@773
    78
        { "SaveState", GTK_STOCK_SAVE_AS, N_("_Save State..."), "F3", N_("Create an lxdream save state"), G_CALLBACK(save_state_action_callback) },
nkeynes@736
    79
        { "Exit", GTK_STOCK_QUIT, N_("E_xit"), NULL, N_("Exit lxdream"), G_CALLBACK(exit_action_callback) },
nkeynes@773
    80
        { "GdromSettings", NULL, N_("_GD-Rom") },
nkeynes@736
    81
        { "GdromUnmount", NULL, N_("_Empty") },
nkeynes@736
    82
        { "GdromMount", GTK_STOCK_CDROM, N_("_Open Image..."), "<control>O", N_("Mount a cdrom disc"), G_CALLBACK(mount_action_callback) },
nkeynes@736
    83
        { "PathSettings", NULL, N_("_Paths..."), NULL, N_("Configure files and paths"), G_CALLBACK(path_settings_callback) }, 
nkeynes@736
    84
        { "AudioSettings", NULL, N_("_Audio..."), NULL, N_("Configure audio output"), G_CALLBACK(audio_settings_callback) },
nkeynes@736
    85
        { "ControllerSettings", NULL, N_("_Controllers..."), NULL, N_("Configure controllers"), G_CALLBACK(maple_settings_callback) },
nkeynes@736
    86
        { "NetworkSettings", NULL, N_("_Network..."), NULL, N_("Configure network settings"), G_CALLBACK(network_settings_callback) },
nkeynes@736
    87
        { "VideoSettings", NULL, N_("_Video..."), NULL,N_( "Configure video output"), G_CALLBACK(video_settings_callback) },
nkeynes@1015
    88
        { "HotkeySettings", NULL, N_("_Hotkeys..."), NULL,N_( "Configure hotkeys"), G_CALLBACK(hotkey_settings_callback) },
nkeynes@736
    89
        { "About", GTK_STOCK_ABOUT, N_("_About..."), NULL, N_("About lxdream"), G_CALLBACK(about_action_callback) },
nkeynes@736
    90
        { "DebugMenu", NULL, N_("_Debug") },
nkeynes@736
    91
        { "Debugger", NULL, N_("_Debugger"), NULL, N_("Open debugger window"), G_CALLBACK(debugger_action_callback) },
nkeynes@736
    92
        { "DebugMem", NULL, N_("View _Memory"), NULL, N_("View memory dump"), G_CALLBACK(debug_memory_action_callback) },
nkeynes@736
    93
        { "DebugMmio", NULL, N_("View IO _Registers"), NULL, N_("View MMIO Registers"), G_CALLBACK(debug_mmio_action_callback) },
nkeynes@736
    94
        { "SaveScene", NULL, N_("_Save Scene"), NULL, N_("Save next rendered scene"), G_CALLBACK(save_scene_action_callback) },
nkeynes@736
    95
        { "SingleStep", GTK_STOCK_REDO, N_("_Single Step"), NULL, N_("Single step"), G_CALLBACK(debug_step_action_callback) },
nkeynes@736
    96
        { "RunTo", GTK_STOCK_GOTO_LAST, N_("Run _To"), NULL, N_("Run to"), G_CALLBACK( debug_runto_action_callback) },
nkeynes@736
    97
        { "SetBreakpoint", GTK_STOCK_CLOSE, N_("_Breakpoint"), NULL, N_("Toggle breakpoint"), G_CALLBACK( debug_breakpoint_action_callback) }
nkeynes@455
    98
};
nkeynes@455
    99
static const GtkToggleActionEntry ui_toggle_actions[] = {
nkeynes@736
   100
        { "FullScreen", NULL, "_Full Screen", "<alt>Return", "Toggle full screen video", G_CALLBACK(fullscreen_toggle_callback), 0 },
nkeynes@455
   101
};
nkeynes@736
   102
nkeynes@455
   103
// Menus and toolbars
nkeynes@455
   104
static const char *ui_description =
nkeynes@455
   105
    "<ui>"
nkeynes@455
   106
    " <menubar name='MainMenu'>"
nkeynes@455
   107
    "  <menu action='FileMenu'>"
nkeynes@543
   108
    "   <menuitem action='LoadBinary'/>"
nkeynes@464
   109
    "   <menuitem action='GdromSettings'/>"
nkeynes@455
   110
    "   <separator/>"
nkeynes@455
   111
    "   <menuitem action='Reset'/>"
nkeynes@455
   112
    "   <menuitem action='Pause'/>"
nkeynes@455
   113
    "   <menuitem action='Run'/>"
nkeynes@455
   114
    "   <menuitem action='Debugger'/>"
nkeynes@455
   115
    "   <separator/>"
nkeynes@455
   116
    "   <menuitem action='LoadState'/>"
nkeynes@455
   117
    "   <menuitem action='SaveState'/>"
nkeynes@455
   118
    "   <separator/>"
nkeynes@455
   119
    "   <menuitem action='Exit'/>"
nkeynes@455
   120
    "  </menu>"
nkeynes@455
   121
    "  <menu action='SettingsMenu'>"
nkeynes@455
   122
    "   <menuitem action='PathSettings'/>"
nkeynes@455
   123
    "   <menuitem action='AudioSettings'/>"
nkeynes@455
   124
    "   <menuitem action='ControllerSettings'/>"
nkeynes@455
   125
    "   <menuitem action='NetworkSettings'/>"
nkeynes@455
   126
    "   <menuitem action='VideoSettings'/>"
nkeynes@1015
   127
    "   <menuitem action='HotkeySettings'/>"
nkeynes@455
   128
    "   <separator/>"
nkeynes@455
   129
    "   <menuitem action='FullScreen'/>"
nkeynes@455
   130
    "  </menu>"
nkeynes@455
   131
    "  <menu action='HelpMenu'>"
nkeynes@455
   132
    "   <menuitem action='About'/>"
nkeynes@455
   133
    "  </menu>"
nkeynes@455
   134
    " </menubar>"
nkeynes@455
   135
    " <toolbar name='MainToolbar'>"
nkeynes@464
   136
    "  <toolitem action='GdromMount'/>"
nkeynes@455
   137
    "  <toolitem action='Reset'/>"
nkeynes@455
   138
    "  <toolitem action='Pause'/>"
nkeynes@455
   139
    "  <toolitem action='Run'/>"
nkeynes@455
   140
    "  <separator/>"
nkeynes@455
   141
    "  <toolitem action='LoadState'/>"
nkeynes@455
   142
    "  <toolitem action='SaveState'/>"
nkeynes@455
   143
    " </toolbar>"
nkeynes@455
   144
    " <menubar name='DebugMenu'>"
nkeynes@455
   145
    "  <menu action='FileMenu'>"
nkeynes@477
   146
    "   <menuitem action='GdromSettings'/>"
nkeynes@455
   147
    "   <separator/>"
nkeynes@455
   148
    "   <menuitem action='Reset'/>"
nkeynes@455
   149
    "   <separator/>"
nkeynes@455
   150
    "   <menuitem action='LoadState'/>"
nkeynes@455
   151
    "   <menuitem action='SaveState'/>"
nkeynes@455
   152
    "   <separator/>"
nkeynes@455
   153
    "   <menuitem action='Exit'/>"
nkeynes@455
   154
    "  </menu>"
nkeynes@455
   155
    "  <menu action='DebugMenu'>"
nkeynes@455
   156
    "   <menuitem action='DebugMem'/>"
nkeynes@455
   157
    "   <menuitem action='DebugMmio'/>"
nkeynes@455
   158
    "   <menuitem action='SaveScene'/>"
nkeynes@455
   159
    "   <separator/>"
nkeynes@455
   160
    "   <menuitem action='SetBreakpoint'/>"
nkeynes@455
   161
    "   <menuitem action='Pause'/>"
nkeynes@455
   162
    "   <menuitem action='SingleStep'/>"
nkeynes@455
   163
    "   <menuitem action='RunTo'/>"
nkeynes@455
   164
    "   <menuitem action='Run'/>"
nkeynes@455
   165
    "  </menu>"
nkeynes@455
   166
    "  <menu action='SettingsMenu'>"
nkeynes@455
   167
    "   <menuitem action='PathSettings'/>"
nkeynes@455
   168
    "   <menuitem action='AudioSettings'/>"
nkeynes@455
   169
    "   <menuitem action='ControllerSettings'/>"
nkeynes@455
   170
    "   <menuitem action='NetworkSettings'/>"
nkeynes@455
   171
    "   <menuitem action='VideoSettings'/>"
nkeynes@1015
   172
    "   <menuitem action='HotkeySettings'/>"
nkeynes@455
   173
    "   <separator/>"
nkeynes@455
   174
    "   <menuitem action='FullScreen'/>"
nkeynes@455
   175
    "  </menu>"
nkeynes@455
   176
    "  <menu action='HelpMenu'>"
nkeynes@455
   177
    "   <menuitem action='About'/>"
nkeynes@455
   178
    "  </menu>"
nkeynes@455
   179
    " </menubar>"
nkeynes@455
   180
    " <toolbar name='DebugToolbar'>"
nkeynes@464
   181
    "  <toolitem action='GdromMount'/>"
nkeynes@455
   182
    "  <toolitem action='Reset'/>"
nkeynes@455
   183
    "  <toolitem action='Pause'/>"
nkeynes@455
   184
    "  <separator/>"
nkeynes@455
   185
    "  <toolitem action='SingleStep'/>"
nkeynes@455
   186
    "  <toolitem action='RunTo'/>"
nkeynes@455
   187
    "  <toolitem action='Run'/>"
nkeynes@455
   188
    "  <toolitem action='SetBreakpoint'/>"
nkeynes@455
   189
    "  <separator/>"
nkeynes@455
   190
    "  <toolitem action='LoadState'/>"
nkeynes@455
   191
    "  <toolitem action='SaveState'/>"
nkeynes@455
   192
    " </toolbar>"
nkeynes@455
   193
    "</ui>";
nkeynes@455
   194
nkeynes@435
   195
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@435
   196
{
nkeynes@530
   197
    gtk_gui_init_ok = gtk_init_check( argc, argv );
nkeynes@530
   198
    return gtk_gui_init_ok;
nkeynes@435
   199
}
nkeynes@435
   200
nkeynes@837
   201
gboolean gtk_gui_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *ptr )
nkeynes@837
   202
{
nkeynes@837
   203
    main_window_update_title( main_win );
nkeynes@839
   204
    return TRUE;
nkeynes@837
   205
}
nkeynes@837
   206
nkeynes@1015
   207
gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
nkeynes@435
   208
{
nkeynes@530
   209
    if( gtk_gui_init_ok ) {
nkeynes@736
   210
        GError *error = NULL;
nkeynes@736
   211
        dreamcast_register_module( &gtk_gui_module );
nkeynes@736
   212
        gtk_gui_alloc_resources();
nkeynes@464
   213
nkeynes@736
   214
        global_action_group = gtk_action_group_new("MenuActions");
nkeynes@736
   215
        gtk_action_group_set_translation_domain( global_action_group, NULL );
nkeynes@736
   216
        gtk_action_group_add_actions( global_action_group, ui_actions, G_N_ELEMENTS(ui_actions), NULL );
nkeynes@736
   217
        gtk_action_group_add_toggle_actions( global_action_group, ui_toggle_actions, G_N_ELEMENTS(ui_toggle_actions), NULL );
nkeynes@736
   218
        gtk_gui_enable_action("AudioSettings", FALSE);
nkeynes@736
   219
        gtk_gui_enable_action("NetworkSettings", FALSE);
nkeynes@736
   220
        gtk_gui_enable_action("VideoSettings", FALSE);
nkeynes@736
   221
nkeynes@736
   222
        global_ui_manager = gtk_ui_manager_new();
nkeynes@736
   223
        gtk_ui_manager_set_add_tearoffs(global_ui_manager, TRUE);
nkeynes@736
   224
        gtk_ui_manager_insert_action_group( global_ui_manager, global_action_group, 0 );
nkeynes@736
   225
nkeynes@736
   226
        if (!gtk_ui_manager_add_ui_from_string (global_ui_manager, ui_description, -1, &error)) {
nkeynes@736
   227
            g_message ("building menus failed: %s", error->message);
nkeynes@736
   228
            g_error_free (error);
nkeynes@736
   229
            exit(1);
nkeynes@736
   230
        }
nkeynes@736
   231
        GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
nkeynes@736
   232
        GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu");
nkeynes@736
   233
        GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/MainToolbar");
nkeynes@736
   234
nkeynes@736
   235
        GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu/FileMenu/GdromSettings");
nkeynes@736
   236
        GtkWidget *gdrommenu = gdrom_menu_new();
nkeynes@736
   237
        gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
nkeynes@738
   238
        main_win = main_window_new( lxdream_package_name, menubar, toolbar, accel_group  );
nkeynes@736
   239
        if( withDebug ) {
nkeynes@736
   240
            gtk_gui_show_debugger();
nkeynes@736
   241
        }
nkeynes@1015
   242
        
nkeynes@1015
   243
        if (withFullscreen) {
nkeynes@1015
   244
            main_window_set_fullscreen(main_win, TRUE);
nkeynes@1015
   245
            //manually call full-screen state code for non-compliant window managers
nkeynes@1015
   246
            main_window_show_gui(main_win, TRUE);
nkeynes@1015
   247
        }
nkeynes@1015
   248
        
nkeynes@837
   249
        register_gdrom_disc_change_hook( gtk_gui_disc_changed, NULL );
nkeynes@736
   250
nkeynes@736
   251
        return TRUE;
nkeynes@530
   252
    } else {
nkeynes@736
   253
        return FALSE;
nkeynes@455
   254
    }
nkeynes@435
   255
}
nkeynes@435
   256
nkeynes@681
   257
void gui_main_loop( gboolean run )
nkeynes@435
   258
{
nkeynes@543
   259
    gtk_gui_update();
nkeynes@681
   260
    if( run ) {
nkeynes@681
   261
        dreamcast_run();
nkeynes@682
   262
        gtk_main();
nkeynes@681
   263
    } else {
nkeynes@681
   264
        gtk_main();
nkeynes@681
   265
    }
nkeynes@435
   266
}
nkeynes@435
   267
nkeynes@537
   268
void gui_update_state(void)
nkeynes@537
   269
{
nkeynes@537
   270
    gtk_gui_update();
nkeynes@537
   271
}
nkeynes@537
   272
nkeynes@839
   273
void gui_set_use_grab( gboolean flag )
nkeynes@839
   274
{
nkeynes@845
   275
    if( main_win != NULL ) {
nkeynes@853
   276
        main_window_set_use_grab(main_win, flag);
nkeynes@845
   277
    }
nkeynes@1015
   278
}
nkeynes@839
   279
nkeynes@447
   280
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@447
   281
{
nkeynes@447
   282
    if( main_win != NULL ) {
nkeynes@736
   283
        va_list args;
nkeynes@736
   284
        GtkWidget *dialog = 
nkeynes@736
   285
            gtk_message_dialog_new( main_window_get_frame(main_win), GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
nkeynes@736
   286
                    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL );
nkeynes@736
   287
        va_start(args, msg);
nkeynes@736
   288
        gchar *markup = g_markup_vprintf_escaped( msg, args );
nkeynes@736
   289
        va_end( args );
nkeynes@736
   290
        gtk_message_dialog_set_markup( GTK_MESSAGE_DIALOG(dialog), markup );
nkeynes@736
   291
        g_free(markup);
nkeynes@736
   292
        gtk_dialog_run(GTK_DIALOG(dialog));
nkeynes@736
   293
        gtk_widget_destroy(dialog);
nkeynes@736
   294
        return TRUE;
nkeynes@447
   295
    }
nkeynes@447
   296
    return FALSE;
nkeynes@447
   297
}
nkeynes@447
   298
nkeynes@435
   299
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@435
   300
{
nkeynes@435
   301
nkeynes@435
   302
}
nkeynes@435
   303
nkeynes@1019
   304
static gboolean gtk_do_later_callback( gpointer ptr )
nkeynes@1015
   305
{
nkeynes@1019
   306
    do_later_callback_t func = (do_later_callback_t)ptr;
nkeynes@1019
   307
    func();
nkeynes@1015
   308
    return FALSE;
nkeynes@1015
   309
}
nkeynes@1015
   310
nkeynes@1019
   311
void gui_do_later( do_later_callback_t func )
nkeynes@1015
   312
{
nkeynes@1019
   313
    g_timeout_add_seconds(0, gtk_do_later_callback, func);
nkeynes@1015
   314
}
nkeynes@1015
   315
nkeynes@435
   316
void gtk_gui_show_debugger()
nkeynes@435
   317
{
nkeynes@435
   318
    if( debug_win ) {
nkeynes@736
   319
        debug_window_show(debug_win, TRUE);
nkeynes@435
   320
    } else {
nkeynes@736
   321
        GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
nkeynes@736
   322
        GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu");
nkeynes@736
   323
        GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugToolbar");
nkeynes@736
   324
        GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu/FileMenu/GdromSettings");
nkeynes@736
   325
        GtkWidget *gdrommenu = gdrom_menu_new();
nkeynes@736
   326
        gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
nkeynes@738
   327
        gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("Debugger"));
nkeynes@736
   328
        debug_win = debug_window_new( title, menubar, toolbar, accel_group  );
nkeynes@736
   329
        g_free(title);
nkeynes@435
   330
    }
nkeynes@455
   331
}
nkeynes@455
   332
nkeynes@455
   333
void gtk_gui_show_mmio()
nkeynes@455
   334
{
nkeynes@455
   335
    if( mmio_win ) {
nkeynes@736
   336
        mmio_window_show(mmio_win, TRUE);
nkeynes@455
   337
    } else {
nkeynes@738
   338
        gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("MMIO Registers"));
nkeynes@736
   339
        mmio_win = mmio_window_new( title );
nkeynes@736
   340
        g_free(title);
nkeynes@455
   341
    }
nkeynes@455
   342
}
nkeynes@455
   343
nkeynes@455
   344
nkeynes@455
   345
main_window_t gtk_gui_get_main()
nkeynes@455
   346
{
nkeynes@455
   347
    return main_win;
nkeynes@455
   348
}
nkeynes@455
   349
nkeynes@455
   350
debug_window_t gtk_gui_get_debugger()
nkeynes@455
   351
{
nkeynes@455
   352
    return debug_win;
nkeynes@455
   353
}
nkeynes@455
   354
nkeynes@455
   355
mmio_window_t gtk_gui_get_mmio()
nkeynes@455
   356
{
nkeynes@455
   357
    return mmio_win;
nkeynes@455
   358
}
nkeynes@435
   359
nkeynes@435
   360
/**
nkeynes@435
   361
 * Hook called when DC starts running. Just disables the run/step buttons
nkeynes@435
   362
 * and enables the stop button.
nkeynes@435
   363
 */
nkeynes@435
   364
void gtk_gui_start( void )
nkeynes@435
   365
{
nkeynes@435
   366
    main_window_set_running( main_win, TRUE );
nkeynes@435
   367
    if( debug_win != NULL ) {
nkeynes@736
   368
        debug_window_set_running( debug_win, TRUE );
nkeynes@435
   369
    }
nkeynes@437
   370
    gtk_gui_nanos = 0;
nkeynes@437
   371
    gettimeofday(&gtk_gui_lasttv,NULL);
nkeynes@435
   372
}
nkeynes@435
   373
nkeynes@435
   374
/**
nkeynes@435
   375
 * Hook called when DC stops running. Enables the run/step buttons
nkeynes@435
   376
 * and disables the stop button.
nkeynes@435
   377
 */
nkeynes@435
   378
void gtk_gui_stop( void )
nkeynes@435
   379
{
nkeynes@435
   380
    main_window_set_running( main_win, FALSE );
nkeynes@435
   381
    gtk_gui_update();
nkeynes@435
   382
}
nkeynes@435
   383
nkeynes@435
   384
void gtk_gui_update( void )
nkeynes@435
   385
{
nkeynes@546
   386
    if( global_action_group ) {
nkeynes@889
   387
        gtk_gui_enable_action("Run", !dreamcast_is_running() );
nkeynes@736
   388
        gtk_gui_enable_action("Pause", dreamcast_is_running() );
nkeynes@546
   389
    }
nkeynes@435
   390
    if( debug_win ) {
nkeynes@736
   391
        debug_window_set_running( debug_win, FALSE );
nkeynes@736
   392
        debug_window_update(debug_win);
nkeynes@435
   393
    }
nkeynes@435
   394
    if( mmio_win ) {
nkeynes@736
   395
        mmio_window_update(mmio_win);
nkeynes@435
   396
    }
nkeynes@455
   397
    dump_window_update_all();
nkeynes@435
   398
}
nkeynes@435
   399
nkeynes@435
   400
/**
nkeynes@622
   401
 * Module run-slice. Run the event loop 100 times/second (doesn't really need to be
nkeynes@622
   402
 * any more often than this), and update the speed display 10 times/second. 
nkeynes@622
   403
 *
nkeynes@622
   404
 * Also detect if we're running too fast here and yield for a bit
nkeynes@435
   405
 */
nkeynes@435
   406
uint32_t gtk_gui_run_slice( uint32_t nanosecs ) 
nkeynes@435
   407
{
nkeynes@435
   408
    gtk_gui_nanos += nanosecs;
nkeynes@622
   409
    if( gtk_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@678
   410
        gtk_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@678
   411
        gtk_gui_ticks ++;
nkeynes@678
   412
        uint32_t current_period = gtk_gui_ticks * GUI_TICK_PERIOD;
nkeynes@622
   413
nkeynes@678
   414
        // Run the event loop
nkeynes@678
   415
        while( gtk_events_pending() )
nkeynes@678
   416
            gtk_main_iteration();	
nkeynes@622
   417
nkeynes@678
   418
        struct timeval tv;
nkeynes@678
   419
        gettimeofday(&tv,NULL);
nkeynes@678
   420
        uint32_t ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@678
   421
        (tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000;
nkeynes@678
   422
        if( (ns * 1.05) < current_period ) {
nkeynes@678
   423
            // We've gotten ahead - sleep for a little bit
nkeynes@678
   424
            struct timespec tv;
nkeynes@678
   425
            tv.tv_sec = 0;
nkeynes@678
   426
            tv.tv_nsec = current_period - ns;
nkeynes@678
   427
            nanosleep(&tv, &tv);
nkeynes@678
   428
        }
nkeynes@622
   429
nkeynes@678
   430
        /* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@678
   431
         * save the current tv value */
nkeynes@678
   432
        if( gtk_gui_ticks > 10 ) {
nkeynes@678
   433
            gtk_gui_ticks -= 10;
nkeynes@678
   434
nkeynes@678
   435
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@678
   436
            gtk_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@678
   437
            gtk_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@678
   438
            main_window_set_speed( main_win, speed );
nkeynes@678
   439
        }
nkeynes@435
   440
    }
nkeynes@435
   441
    return nanosecs;
nkeynes@435
   442
}
nkeynes@435
   443
nkeynes@435
   444
nkeynes@435
   445
PangoFontDescription *gui_fixed_font;
nkeynes@435
   446
GdkColor gui_colour_normal, gui_colour_changed, gui_colour_error;
nkeynes@435
   447
GdkColor gui_colour_warn, gui_colour_pc, gui_colour_debug;
nkeynes@435
   448
GdkColor gui_colour_trace, gui_colour_break, gui_colour_temp_break;
nkeynes@435
   449
GdkColor gui_colour_white;
nkeynes@435
   450
nkeynes@435
   451
void gtk_gui_alloc_resources() {
nkeynes@435
   452
    GdkColormap *map;
nkeynes@435
   453
nkeynes@435
   454
    gui_colour_normal.red = gui_colour_normal.green = gui_colour_normal.blue = 0;
nkeynes@435
   455
    gui_colour_changed.red = gui_colour_changed.green = 64*256;
nkeynes@435
   456
    gui_colour_changed.blue = 154*256;
nkeynes@435
   457
    gui_colour_error.red = 65535;
nkeynes@435
   458
    gui_colour_error.green = gui_colour_error.blue = 64*256;
nkeynes@435
   459
    gui_colour_pc.red = 32*256;
nkeynes@435
   460
    gui_colour_pc.green = 170*256;
nkeynes@435
   461
    gui_colour_pc.blue = 52*256;
nkeynes@435
   462
    gui_colour_warn = gui_colour_changed;
nkeynes@435
   463
    gui_colour_trace.red = 156*256;
nkeynes@435
   464
    gui_colour_trace.green = 78*256;
nkeynes@435
   465
    gui_colour_trace.blue = 201*256;
nkeynes@435
   466
    gui_colour_debug = gui_colour_pc;
nkeynes@435
   467
    gui_colour_break.red = 65535;
nkeynes@435
   468
    gui_colour_break.green = gui_colour_break.blue = 192*256;
nkeynes@435
   469
    gui_colour_temp_break.red = gui_colour_temp_break.green = 128*256;
nkeynes@435
   470
    gui_colour_temp_break.blue = 32*256;
nkeynes@435
   471
    gui_colour_white.red = gui_colour_white.green = gui_colour_white.blue = 65535;
nkeynes@435
   472
nkeynes@435
   473
    map = gdk_colormap_new(gdk_visual_get_best(), TRUE);
nkeynes@435
   474
    gdk_colormap_alloc_color(map, &gui_colour_normal, TRUE, TRUE);
nkeynes@435
   475
    gdk_colormap_alloc_color(map, &gui_colour_changed, TRUE, TRUE);
nkeynes@435
   476
    gdk_colormap_alloc_color(map, &gui_colour_error, TRUE, TRUE);
nkeynes@435
   477
    gdk_colormap_alloc_color(map, &gui_colour_warn, TRUE, TRUE);
nkeynes@435
   478
    gdk_colormap_alloc_color(map, &gui_colour_pc, TRUE, TRUE);
nkeynes@435
   479
    gdk_colormap_alloc_color(map, &gui_colour_debug, TRUE, TRUE);
nkeynes@435
   480
    gdk_colormap_alloc_color(map, &gui_colour_trace, TRUE, TRUE);
nkeynes@435
   481
    gdk_colormap_alloc_color(map, &gui_colour_break, TRUE, TRUE);
nkeynes@435
   482
    gdk_colormap_alloc_color(map, &gui_colour_temp_break, TRUE, TRUE);
nkeynes@435
   483
    gdk_colormap_alloc_color(map, &gui_colour_white, TRUE, TRUE);
nkeynes@435
   484
    gui_fixed_font = pango_font_description_from_string("Courier 10");
nkeynes@435
   485
}
nkeynes@450
   486
nkeynes@455
   487
gint gtk_gui_run_property_dialog( const gchar *title, GtkWidget *panel, gtk_dialog_done_fn fn )
nkeynes@450
   488
{
nkeynes@450
   489
    GtkWidget *dialog =
nkeynes@736
   490
        gtk_dialog_new_with_buttons(title, main_window_get_frame(main_win), 
nkeynes@736
   491
                GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
nkeynes@736
   492
                GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
nkeynes@736
   493
                GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
nkeynes@736
   494
                NULL);
nkeynes@450
   495
    gint result;
nkeynes@450
   496
    gtk_widget_show_all(panel);
nkeynes@450
   497
    gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
nkeynes@450
   498
    result = gtk_dialog_run( GTK_DIALOG(dialog) );
nkeynes@455
   499
    if( fn != NULL ) {
nkeynes@736
   500
        fn(panel, result == GTK_RESPONSE_ACCEPT);
nkeynes@455
   501
    }
nkeynes@450
   502
    gtk_widget_destroy( dialog );
nkeynes@450
   503
    return result;
nkeynes@450
   504
}
nkeynes@455
   505
nkeynes@455
   506
void gtk_gui_enable_action( const gchar *action, gboolean enable )
nkeynes@455
   507
{
nkeynes@455
   508
    gtk_action_set_sensitive( gtk_action_group_get_action( global_action_group, action), enable);
nkeynes@455
   509
}
nkeynes@477
   510
nkeynes@477
   511
static void delete_frame_buffer( guchar *pixels, gpointer buffer )
nkeynes@477
   512
{
nkeynes@477
   513
    if( buffer != NULL ) {
nkeynes@736
   514
        g_free(buffer);
nkeynes@477
   515
    }
nkeynes@477
   516
}
nkeynes@477
   517
nkeynes@477
   518
GdkPixbuf *gdk_pixbuf_new_from_frame_buffer( frame_buffer_t buffer )
nkeynes@477
   519
{
nkeynes@480
   520
    return gdk_pixbuf_new_from_data( (unsigned char *)buffer->data, 
nkeynes@736
   521
            GDK_COLORSPACE_RGB,
nkeynes@736
   522
            (buffer->colour_format == COLFMT_BGRA8888),
nkeynes@736
   523
            8,
nkeynes@736
   524
            buffer->width,
nkeynes@736
   525
            buffer->height,
nkeynes@736
   526
            buffer->rowstride,
nkeynes@736
   527
            delete_frame_buffer,
nkeynes@736
   528
            buffer );
nkeynes@477
   529
}
nkeynes@608
   530
nkeynes@608
   531
/**
nkeynes@608
   532
 * Extract the keyval of the key event if no modifier keys were pressed -
nkeynes@608
   533
 * in other words get the keyval of the key by itself. The other way around
nkeynes@608
   534
 * would be to use the hardware keysyms directly rather than the keyvals,
nkeynes@608
   535
 * but the mapping looks to be messier.
nkeynes@608
   536
 */
nkeynes@608
   537
uint16_t gtk_get_unmodified_keyval( GdkEventKey *event )
nkeynes@608
   538
{
nkeynes@608
   539
    GdkKeymap *keymap = gdk_keymap_get_default();
nkeynes@608
   540
    guint keyval;
nkeynes@736
   541
nkeynes@608
   542
    gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
nkeynes@736
   543
                                         NULL, NULL, NULL );
nkeynes@608
   544
    return keyval;
nkeynes@608
   545
}
nkeynes@608
   546
nkeynes@630
   547
.