Search
lxdream.org :: lxdream/src/gtkui/gtkui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtkui.c
changeset 1098:4f2750753a6c
prev1040:9e3e41eab2db
next1123:b072c961c729
author nkeynes
date Sun Jan 31 18:36:06 2010 +1000 (10 years ago)
permissions -rw-r--r--
last change Show '<no disc>' in the title bar when there is no cdrom disc attached
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@1040
    77
        { "LoadState", GTK_STOCK_REVERT_TO_SAVED, N_("L_oad State..."), "F4", N_("Load an lxdream save state"), G_CALLBACK(load_state_action_callback) },
nkeynes@1040
    78
        { "SaveState", GTK_STOCK_SAVE_AS, N_("S_ave State..."), "F3", N_("Create an lxdream save state"), G_CALLBACK(save_state_action_callback) },
nkeynes@1040
    79
        { "QuickLoad", NULL, N_("_Load Quick State"), "F6", N_("Load the current quick save state"), G_CALLBACK(quick_load_action_callback) },
nkeynes@1040
    80
        { "QuickSave", NULL, N_("_Save Quick State..."), "F5", N_("Save to the current quick save state"), G_CALLBACK(quick_save_action_callback) },
nkeynes@1040
    81
        { "QuickStateMenu", NULL, N_("Select _Quick State"), NULL, N_("Set quick save state") },
nkeynes@736
    82
        { "Exit", GTK_STOCK_QUIT, N_("E_xit"), NULL, N_("Exit lxdream"), G_CALLBACK(exit_action_callback) },
nkeynes@773
    83
        { "GdromSettings", NULL, N_("_GD-Rom") },
nkeynes@736
    84
        { "GdromUnmount", NULL, N_("_Empty") },
nkeynes@736
    85
        { "GdromMount", GTK_STOCK_CDROM, N_("_Open Image..."), "<control>O", N_("Mount a cdrom disc"), G_CALLBACK(mount_action_callback) },
nkeynes@736
    86
        { "PathSettings", NULL, N_("_Paths..."), NULL, N_("Configure files and paths"), G_CALLBACK(path_settings_callback) }, 
nkeynes@736
    87
        { "AudioSettings", NULL, N_("_Audio..."), NULL, N_("Configure audio output"), G_CALLBACK(audio_settings_callback) },
nkeynes@736
    88
        { "ControllerSettings", NULL, N_("_Controllers..."), NULL, N_("Configure controllers"), G_CALLBACK(maple_settings_callback) },
nkeynes@736
    89
        { "NetworkSettings", NULL, N_("_Network..."), NULL, N_("Configure network settings"), G_CALLBACK(network_settings_callback) },
nkeynes@736
    90
        { "VideoSettings", NULL, N_("_Video..."), NULL,N_( "Configure video output"), G_CALLBACK(video_settings_callback) },
nkeynes@1015
    91
        { "HotkeySettings", NULL, N_("_Hotkeys..."), NULL,N_( "Configure hotkeys"), G_CALLBACK(hotkey_settings_callback) },
nkeynes@736
    92
        { "About", GTK_STOCK_ABOUT, N_("_About..."), NULL, N_("About lxdream"), G_CALLBACK(about_action_callback) },
nkeynes@736
    93
        { "DebugMenu", NULL, N_("_Debug") },
nkeynes@736
    94
        { "Debugger", NULL, N_("_Debugger"), NULL, N_("Open debugger window"), G_CALLBACK(debugger_action_callback) },
nkeynes@736
    95
        { "DebugMem", NULL, N_("View _Memory"), NULL, N_("View memory dump"), G_CALLBACK(debug_memory_action_callback) },
nkeynes@736
    96
        { "DebugMmio", NULL, N_("View IO _Registers"), NULL, N_("View MMIO Registers"), G_CALLBACK(debug_mmio_action_callback) },
nkeynes@736
    97
        { "SaveScene", NULL, N_("_Save Scene"), NULL, N_("Save next rendered scene"), G_CALLBACK(save_scene_action_callback) },
nkeynes@736
    98
        { "SingleStep", GTK_STOCK_REDO, N_("_Single Step"), NULL, N_("Single step"), G_CALLBACK(debug_step_action_callback) },
nkeynes@736
    99
        { "RunTo", GTK_STOCK_GOTO_LAST, N_("Run _To"), NULL, N_("Run to"), G_CALLBACK( debug_runto_action_callback) },
nkeynes@736
   100
        { "SetBreakpoint", GTK_STOCK_CLOSE, N_("_Breakpoint"), NULL, N_("Toggle breakpoint"), G_CALLBACK( debug_breakpoint_action_callback) }
nkeynes@455
   101
};
nkeynes@455
   102
static const GtkToggleActionEntry ui_toggle_actions[] = {
nkeynes@736
   103
        { "FullScreen", NULL, "_Full Screen", "<alt>Return", "Toggle full screen video", G_CALLBACK(fullscreen_toggle_callback), 0 },
nkeynes@455
   104
};
nkeynes@1040
   105
static GtkRadioActionEntry ui_radio_actions[MAX_QUICK_STATE+1];
nkeynes@736
   106
nkeynes@455
   107
// Menus and toolbars
nkeynes@455
   108
static const char *ui_description =
nkeynes@455
   109
    "<ui>"
nkeynes@455
   110
    " <menubar name='MainMenu'>"
nkeynes@455
   111
    "  <menu action='FileMenu'>"
nkeynes@543
   112
    "   <menuitem action='LoadBinary'/>"
nkeynes@464
   113
    "   <menuitem action='GdromSettings'/>"
nkeynes@455
   114
    "   <separator/>"
nkeynes@455
   115
    "   <menuitem action='Reset'/>"
nkeynes@455
   116
    "   <menuitem action='Pause'/>"
nkeynes@455
   117
    "   <menuitem action='Run'/>"
nkeynes@455
   118
    "   <menuitem action='Debugger'/>"
nkeynes@455
   119
    "   <separator/>"
nkeynes@455
   120
    "   <menuitem action='LoadState'/>"
nkeynes@455
   121
    "   <menuitem action='SaveState'/>"
nkeynes@455
   122
    "   <separator/>"
nkeynes@1040
   123
    "   <menuitem action='QuickLoad'/>"
nkeynes@1040
   124
    "   <menuitem action='QuickSave'/>"
nkeynes@1040
   125
    "   <menu action='QuickStateMenu'>"
nkeynes@1040
   126
    "    <menuitem action='QuickState0'/>"
nkeynes@1040
   127
    "    <menuitem action='QuickState1'/>"
nkeynes@1040
   128
    "    <menuitem action='QuickState2'/>"
nkeynes@1040
   129
    "    <menuitem action='QuickState3'/>"
nkeynes@1040
   130
    "    <menuitem action='QuickState4'/>"
nkeynes@1040
   131
    "    <menuitem action='QuickState5'/>"
nkeynes@1040
   132
    "    <menuitem action='QuickState6'/>"
nkeynes@1040
   133
    "    <menuitem action='QuickState7'/>"
nkeynes@1040
   134
    "    <menuitem action='QuickState8'/>"
nkeynes@1040
   135
    "    <menuitem action='QuickState9'/>"
nkeynes@1040
   136
    "   </menu>"
nkeynes@1040
   137
    "   <separator/>"
nkeynes@455
   138
    "   <menuitem action='Exit'/>"
nkeynes@455
   139
    "  </menu>"
nkeynes@455
   140
    "  <menu action='SettingsMenu'>"
nkeynes@455
   141
    "   <menuitem action='PathSettings'/>"
nkeynes@455
   142
    "   <menuitem action='AudioSettings'/>"
nkeynes@455
   143
    "   <menuitem action='ControllerSettings'/>"
nkeynes@455
   144
    "   <menuitem action='NetworkSettings'/>"
nkeynes@455
   145
    "   <menuitem action='VideoSettings'/>"
nkeynes@1015
   146
    "   <menuitem action='HotkeySettings'/>"
nkeynes@455
   147
    "   <separator/>"
nkeynes@455
   148
    "   <menuitem action='FullScreen'/>"
nkeynes@455
   149
    "  </menu>"
nkeynes@455
   150
    "  <menu action='HelpMenu'>"
nkeynes@455
   151
    "   <menuitem action='About'/>"
nkeynes@455
   152
    "  </menu>"
nkeynes@455
   153
    " </menubar>"
nkeynes@455
   154
    " <toolbar name='MainToolbar'>"
nkeynes@464
   155
    "  <toolitem action='GdromMount'/>"
nkeynes@455
   156
    "  <toolitem action='Reset'/>"
nkeynes@455
   157
    "  <toolitem action='Pause'/>"
nkeynes@455
   158
    "  <toolitem action='Run'/>"
nkeynes@455
   159
    "  <separator/>"
nkeynes@455
   160
    "  <toolitem action='LoadState'/>"
nkeynes@455
   161
    "  <toolitem action='SaveState'/>"
nkeynes@455
   162
    " </toolbar>"
nkeynes@455
   163
    " <menubar name='DebugMenu'>"
nkeynes@455
   164
    "  <menu action='FileMenu'>"
nkeynes@477
   165
    "   <menuitem action='GdromSettings'/>"
nkeynes@455
   166
    "   <separator/>"
nkeynes@455
   167
    "   <menuitem action='Reset'/>"
nkeynes@455
   168
    "   <separator/>"
nkeynes@455
   169
    "   <menuitem action='LoadState'/>"
nkeynes@455
   170
    "   <menuitem action='SaveState'/>"
nkeynes@455
   171
    "   <separator/>"
nkeynes@1040
   172
    "   <menuitem action='QuickLoad'/>"
nkeynes@1040
   173
    "   <menuitem action='QuickSave'/>"
nkeynes@1040
   174
    "   <menu action='QuickStateMenu'>"
nkeynes@1040
   175
    "    <menuitem action='QuickState0'/>"
nkeynes@1040
   176
    "    <menuitem action='QuickState1'/>"
nkeynes@1040
   177
    "    <menuitem action='QuickState2'/>"
nkeynes@1040
   178
    "    <menuitem action='QuickState3'/>"
nkeynes@1040
   179
    "    <menuitem action='QuickState4'/>"
nkeynes@1040
   180
    "    <menuitem action='QuickState5'/>"
nkeynes@1040
   181
    "    <menuitem action='QuickState6'/>"
nkeynes@1040
   182
    "    <menuitem action='QuickState7'/>"
nkeynes@1040
   183
    "    <menuitem action='QuickState8'/>"
nkeynes@1040
   184
    "    <menuitem action='QuickState9'/>"
nkeynes@1040
   185
    "   </menu>"
nkeynes@1040
   186
    "   <separator/>"
nkeynes@455
   187
    "   <menuitem action='Exit'/>"
nkeynes@455
   188
    "  </menu>"
nkeynes@455
   189
    "  <menu action='DebugMenu'>"
nkeynes@455
   190
    "   <menuitem action='DebugMem'/>"
nkeynes@455
   191
    "   <menuitem action='DebugMmio'/>"
nkeynes@455
   192
    "   <menuitem action='SaveScene'/>"
nkeynes@455
   193
    "   <separator/>"
nkeynes@455
   194
    "   <menuitem action='SetBreakpoint'/>"
nkeynes@455
   195
    "   <menuitem action='Pause'/>"
nkeynes@455
   196
    "   <menuitem action='SingleStep'/>"
nkeynes@455
   197
    "   <menuitem action='RunTo'/>"
nkeynes@455
   198
    "   <menuitem action='Run'/>"
nkeynes@455
   199
    "  </menu>"
nkeynes@455
   200
    "  <menu action='SettingsMenu'>"
nkeynes@455
   201
    "   <menuitem action='PathSettings'/>"
nkeynes@455
   202
    "   <menuitem action='AudioSettings'/>"
nkeynes@455
   203
    "   <menuitem action='ControllerSettings'/>"
nkeynes@455
   204
    "   <menuitem action='NetworkSettings'/>"
nkeynes@455
   205
    "   <menuitem action='VideoSettings'/>"
nkeynes@1015
   206
    "   <menuitem action='HotkeySettings'/>"
nkeynes@455
   207
    "   <separator/>"
nkeynes@455
   208
    "   <menuitem action='FullScreen'/>"
nkeynes@455
   209
    "  </menu>"
nkeynes@455
   210
    "  <menu action='HelpMenu'>"
nkeynes@455
   211
    "   <menuitem action='About'/>"
nkeynes@455
   212
    "  </menu>"
nkeynes@455
   213
    " </menubar>"
nkeynes@455
   214
    " <toolbar name='DebugToolbar'>"
nkeynes@464
   215
    "  <toolitem action='GdromMount'/>"
nkeynes@455
   216
    "  <toolitem action='Reset'/>"
nkeynes@455
   217
    "  <toolitem action='Pause'/>"
nkeynes@455
   218
    "  <separator/>"
nkeynes@455
   219
    "  <toolitem action='SingleStep'/>"
nkeynes@455
   220
    "  <toolitem action='RunTo'/>"
nkeynes@455
   221
    "  <toolitem action='Run'/>"
nkeynes@455
   222
    "  <toolitem action='SetBreakpoint'/>"
nkeynes@455
   223
    "  <separator/>"
nkeynes@455
   224
    "  <toolitem action='LoadState'/>"
nkeynes@455
   225
    "  <toolitem action='SaveState'/>"
nkeynes@455
   226
    " </toolbar>"
nkeynes@455
   227
    "</ui>";
nkeynes@455
   228
nkeynes@435
   229
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@435
   230
{
nkeynes@530
   231
    gtk_gui_init_ok = gtk_init_check( argc, argv );
nkeynes@530
   232
    return gtk_gui_init_ok;
nkeynes@435
   233
}
nkeynes@435
   234
nkeynes@1098
   235
gboolean gtk_gui_disc_changed( cdrom_disc_t disc, const gchar *disc_name, void *ptr )
nkeynes@837
   236
{
nkeynes@837
   237
    main_window_update_title( main_win );
nkeynes@839
   238
    return TRUE;
nkeynes@837
   239
}
nkeynes@837
   240
nkeynes@1015
   241
gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
nkeynes@435
   242
{
nkeynes@530
   243
    if( gtk_gui_init_ok ) {
nkeynes@1040
   244
        int i;
nkeynes@736
   245
        GError *error = NULL;
nkeynes@736
   246
        dreamcast_register_module( &gtk_gui_module );
nkeynes@736
   247
        gtk_gui_alloc_resources();
nkeynes@464
   248
nkeynes@736
   249
        global_action_group = gtk_action_group_new("MenuActions");
nkeynes@736
   250
        gtk_action_group_set_translation_domain( global_action_group, NULL );
nkeynes@736
   251
        gtk_action_group_add_actions( global_action_group, ui_actions, G_N_ELEMENTS(ui_actions), NULL );
nkeynes@736
   252
        gtk_action_group_add_toggle_actions( global_action_group, ui_toggle_actions, G_N_ELEMENTS(ui_toggle_actions), NULL );
nkeynes@1040
   253
nkeynes@1040
   254
        for( i=0; i<=MAX_QUICK_STATE; i++ ) {
nkeynes@1040
   255
            ui_radio_actions[i].name = g_strdup_printf("QuickState%d", i);
nkeynes@1040
   256
            ui_radio_actions[i].stock_id = NULL;
nkeynes@1040
   257
            ui_radio_actions[i].label = g_strdup_printf(_("State _%d"), i );
nkeynes@1040
   258
            ui_radio_actions[i].accelerator = NULL;
nkeynes@1040
   259
            ui_radio_actions[i].tooltip = g_strdup_printf(_("Use quick save state %d"),i);
nkeynes@1040
   260
            ui_radio_actions[i].value = i;
nkeynes@1040
   261
        }
nkeynes@1040
   262
        gtk_action_group_add_radio_actions( global_action_group, ui_radio_actions, G_N_ELEMENTS(ui_radio_actions), 
nkeynes@1040
   263
                                            dreamcast_get_quick_state(), G_CALLBACK(quick_state_action_callback), NULL );
nkeynes@736
   264
        gtk_gui_enable_action("AudioSettings", FALSE);
nkeynes@736
   265
        gtk_gui_enable_action("NetworkSettings", FALSE);
nkeynes@736
   266
        gtk_gui_enable_action("VideoSettings", FALSE);
nkeynes@736
   267
nkeynes@736
   268
        global_ui_manager = gtk_ui_manager_new();
nkeynes@736
   269
        gtk_ui_manager_set_add_tearoffs(global_ui_manager, TRUE);
nkeynes@736
   270
        gtk_ui_manager_insert_action_group( global_ui_manager, global_action_group, 0 );
nkeynes@736
   271
nkeynes@736
   272
        if (!gtk_ui_manager_add_ui_from_string (global_ui_manager, ui_description, -1, &error)) {
nkeynes@736
   273
            g_message ("building menus failed: %s", error->message);
nkeynes@736
   274
            g_error_free (error);
nkeynes@736
   275
            exit(1);
nkeynes@736
   276
        }
nkeynes@736
   277
        GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
nkeynes@736
   278
        GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu");
nkeynes@736
   279
        GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/MainToolbar");
nkeynes@736
   280
nkeynes@736
   281
        GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu/FileMenu/GdromSettings");
nkeynes@736
   282
        GtkWidget *gdrommenu = gdrom_menu_new();
nkeynes@736
   283
        gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
nkeynes@738
   284
        main_win = main_window_new( lxdream_package_name, menubar, toolbar, accel_group  );
nkeynes@736
   285
        if( withDebug ) {
nkeynes@736
   286
            gtk_gui_show_debugger();
nkeynes@736
   287
        }
nkeynes@1015
   288
        
nkeynes@1015
   289
        if (withFullscreen) {
nkeynes@1015
   290
            main_window_set_fullscreen(main_win, TRUE);
nkeynes@1015
   291
            //manually call full-screen state code for non-compliant window managers
nkeynes@1015
   292
            main_window_show_gui(main_win, TRUE);
nkeynes@1015
   293
        }
nkeynes@1015
   294
        
nkeynes@837
   295
        register_gdrom_disc_change_hook( gtk_gui_disc_changed, NULL );
nkeynes@736
   296
nkeynes@736
   297
        return TRUE;
nkeynes@530
   298
    } else {
nkeynes@736
   299
        return FALSE;
nkeynes@455
   300
    }
nkeynes@435
   301
}
nkeynes@435
   302
nkeynes@681
   303
void gui_main_loop( gboolean run )
nkeynes@435
   304
{
nkeynes@543
   305
    gtk_gui_update();
nkeynes@681
   306
    if( run ) {
nkeynes@681
   307
        dreamcast_run();
nkeynes@682
   308
        gtk_main();
nkeynes@681
   309
    } else {
nkeynes@681
   310
        gtk_main();
nkeynes@681
   311
    }
nkeynes@435
   312
}
nkeynes@435
   313
nkeynes@537
   314
void gui_update_state(void)
nkeynes@537
   315
{
nkeynes@537
   316
    gtk_gui_update();
nkeynes@537
   317
}
nkeynes@537
   318
nkeynes@839
   319
void gui_set_use_grab( gboolean flag )
nkeynes@839
   320
{
nkeynes@845
   321
    if( main_win != NULL ) {
nkeynes@853
   322
        main_window_set_use_grab(main_win, flag);
nkeynes@845
   323
    }
nkeynes@1015
   324
}
nkeynes@839
   325
nkeynes@447
   326
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@447
   327
{
nkeynes@447
   328
    if( main_win != NULL ) {
nkeynes@736
   329
        va_list args;
nkeynes@736
   330
        GtkWidget *dialog = 
nkeynes@736
   331
            gtk_message_dialog_new( main_window_get_frame(main_win), GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
nkeynes@736
   332
                    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL );
nkeynes@736
   333
        va_start(args, msg);
nkeynes@736
   334
        gchar *markup = g_markup_vprintf_escaped( msg, args );
nkeynes@736
   335
        va_end( args );
nkeynes@736
   336
        gtk_message_dialog_set_markup( GTK_MESSAGE_DIALOG(dialog), markup );
nkeynes@736
   337
        g_free(markup);
nkeynes@736
   338
        gtk_dialog_run(GTK_DIALOG(dialog));
nkeynes@736
   339
        gtk_widget_destroy(dialog);
nkeynes@736
   340
        return TRUE;
nkeynes@447
   341
    }
nkeynes@447
   342
    return FALSE;
nkeynes@447
   343
}
nkeynes@447
   344
nkeynes@435
   345
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@435
   346
{
nkeynes@435
   347
nkeynes@435
   348
}
nkeynes@435
   349
nkeynes@1019
   350
static gboolean gtk_do_later_callback( gpointer ptr )
nkeynes@1015
   351
{
nkeynes@1019
   352
    do_later_callback_t func = (do_later_callback_t)ptr;
nkeynes@1019
   353
    func();
nkeynes@1015
   354
    return FALSE;
nkeynes@1015
   355
}
nkeynes@1015
   356
nkeynes@1019
   357
void gui_do_later( do_later_callback_t func )
nkeynes@1015
   358
{
nkeynes@1019
   359
    g_timeout_add_seconds(0, gtk_do_later_callback, func);
nkeynes@1015
   360
}
nkeynes@1015
   361
nkeynes@435
   362
void gtk_gui_show_debugger()
nkeynes@435
   363
{
nkeynes@435
   364
    if( debug_win ) {
nkeynes@736
   365
        debug_window_show(debug_win, TRUE);
nkeynes@435
   366
    } else {
nkeynes@736
   367
        GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
nkeynes@736
   368
        GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu");
nkeynes@736
   369
        GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugToolbar");
nkeynes@736
   370
        GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu/FileMenu/GdromSettings");
nkeynes@736
   371
        GtkWidget *gdrommenu = gdrom_menu_new();
nkeynes@736
   372
        gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
nkeynes@738
   373
        gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("Debugger"));
nkeynes@736
   374
        debug_win = debug_window_new( title, menubar, toolbar, accel_group  );
nkeynes@736
   375
        g_free(title);
nkeynes@435
   376
    }
nkeynes@455
   377
}
nkeynes@455
   378
nkeynes@455
   379
void gtk_gui_show_mmio()
nkeynes@455
   380
{
nkeynes@455
   381
    if( mmio_win ) {
nkeynes@736
   382
        mmio_window_show(mmio_win, TRUE);
nkeynes@455
   383
    } else {
nkeynes@738
   384
        gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("MMIO Registers"));
nkeynes@736
   385
        mmio_win = mmio_window_new( title );
nkeynes@736
   386
        g_free(title);
nkeynes@455
   387
    }
nkeynes@455
   388
}
nkeynes@455
   389
nkeynes@455
   390
nkeynes@455
   391
main_window_t gtk_gui_get_main()
nkeynes@455
   392
{
nkeynes@455
   393
    return main_win;
nkeynes@455
   394
}
nkeynes@455
   395
nkeynes@455
   396
debug_window_t gtk_gui_get_debugger()
nkeynes@455
   397
{
nkeynes@455
   398
    return debug_win;
nkeynes@455
   399
}
nkeynes@455
   400
nkeynes@455
   401
mmio_window_t gtk_gui_get_mmio()
nkeynes@455
   402
{
nkeynes@455
   403
    return mmio_win;
nkeynes@455
   404
}
nkeynes@435
   405
nkeynes@435
   406
/**
nkeynes@435
   407
 * Hook called when DC starts running. Just disables the run/step buttons
nkeynes@435
   408
 * and enables the stop button.
nkeynes@435
   409
 */
nkeynes@435
   410
void gtk_gui_start( void )
nkeynes@435
   411
{
nkeynes@435
   412
    main_window_set_running( main_win, TRUE );
nkeynes@435
   413
    if( debug_win != NULL ) {
nkeynes@736
   414
        debug_window_set_running( debug_win, TRUE );
nkeynes@435
   415
    }
nkeynes@437
   416
    gtk_gui_nanos = 0;
nkeynes@437
   417
    gettimeofday(&gtk_gui_lasttv,NULL);
nkeynes@435
   418
}
nkeynes@435
   419
nkeynes@435
   420
/**
nkeynes@435
   421
 * Hook called when DC stops running. Enables the run/step buttons
nkeynes@435
   422
 * and disables the stop button.
nkeynes@435
   423
 */
nkeynes@435
   424
void gtk_gui_stop( void )
nkeynes@435
   425
{
nkeynes@435
   426
    main_window_set_running( main_win, FALSE );
nkeynes@435
   427
    gtk_gui_update();
nkeynes@435
   428
}
nkeynes@435
   429
nkeynes@435
   430
void gtk_gui_update( void )
nkeynes@435
   431
{
nkeynes@546
   432
    if( global_action_group ) {
nkeynes@889
   433
        gtk_gui_enable_action("Run", !dreamcast_is_running() );
nkeynes@736
   434
        gtk_gui_enable_action("Pause", dreamcast_is_running() );
nkeynes@546
   435
    }
nkeynes@435
   436
    if( debug_win ) {
nkeynes@736
   437
        debug_window_set_running( debug_win, FALSE );
nkeynes@736
   438
        debug_window_update(debug_win);
nkeynes@435
   439
    }
nkeynes@435
   440
    if( mmio_win ) {
nkeynes@736
   441
        mmio_window_update(mmio_win);
nkeynes@435
   442
    }
nkeynes@455
   443
    dump_window_update_all();
nkeynes@435
   444
}
nkeynes@435
   445
nkeynes@435
   446
/**
nkeynes@622
   447
 * Module run-slice. Run the event loop 100 times/second (doesn't really need to be
nkeynes@622
   448
 * any more often than this), and update the speed display 10 times/second. 
nkeynes@622
   449
 *
nkeynes@622
   450
 * Also detect if we're running too fast here and yield for a bit
nkeynes@435
   451
 */
nkeynes@435
   452
uint32_t gtk_gui_run_slice( uint32_t nanosecs ) 
nkeynes@435
   453
{
nkeynes@435
   454
    gtk_gui_nanos += nanosecs;
nkeynes@622
   455
    if( gtk_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@678
   456
        gtk_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@678
   457
        gtk_gui_ticks ++;
nkeynes@678
   458
        uint32_t current_period = gtk_gui_ticks * GUI_TICK_PERIOD;
nkeynes@622
   459
nkeynes@678
   460
        // Run the event loop
nkeynes@678
   461
        while( gtk_events_pending() )
nkeynes@678
   462
            gtk_main_iteration();	
nkeynes@622
   463
nkeynes@678
   464
        struct timeval tv;
nkeynes@678
   465
        gettimeofday(&tv,NULL);
nkeynes@678
   466
        uint32_t ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@678
   467
        (tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000;
nkeynes@678
   468
        if( (ns * 1.05) < current_period ) {
nkeynes@678
   469
            // We've gotten ahead - sleep for a little bit
nkeynes@678
   470
            struct timespec tv;
nkeynes@678
   471
            tv.tv_sec = 0;
nkeynes@678
   472
            tv.tv_nsec = current_period - ns;
nkeynes@678
   473
            nanosleep(&tv, &tv);
nkeynes@678
   474
        }
nkeynes@622
   475
nkeynes@678
   476
        /* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@678
   477
         * save the current tv value */
nkeynes@678
   478
        if( gtk_gui_ticks > 10 ) {
nkeynes@678
   479
            gtk_gui_ticks -= 10;
nkeynes@678
   480
nkeynes@678
   481
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@678
   482
            gtk_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@678
   483
            gtk_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@678
   484
            main_window_set_speed( main_win, speed );
nkeynes@678
   485
        }
nkeynes@435
   486
    }
nkeynes@435
   487
    return nanosecs;
nkeynes@435
   488
}
nkeynes@435
   489
nkeynes@435
   490
nkeynes@435
   491
PangoFontDescription *gui_fixed_font;
nkeynes@435
   492
GdkColor gui_colour_normal, gui_colour_changed, gui_colour_error;
nkeynes@435
   493
GdkColor gui_colour_warn, gui_colour_pc, gui_colour_debug;
nkeynes@435
   494
GdkColor gui_colour_trace, gui_colour_break, gui_colour_temp_break;
nkeynes@435
   495
GdkColor gui_colour_white;
nkeynes@435
   496
nkeynes@435
   497
void gtk_gui_alloc_resources() {
nkeynes@435
   498
    GdkColormap *map;
nkeynes@435
   499
nkeynes@435
   500
    gui_colour_normal.red = gui_colour_normal.green = gui_colour_normal.blue = 0;
nkeynes@435
   501
    gui_colour_changed.red = gui_colour_changed.green = 64*256;
nkeynes@435
   502
    gui_colour_changed.blue = 154*256;
nkeynes@435
   503
    gui_colour_error.red = 65535;
nkeynes@435
   504
    gui_colour_error.green = gui_colour_error.blue = 64*256;
nkeynes@435
   505
    gui_colour_pc.red = 32*256;
nkeynes@435
   506
    gui_colour_pc.green = 170*256;
nkeynes@435
   507
    gui_colour_pc.blue = 52*256;
nkeynes@435
   508
    gui_colour_warn = gui_colour_changed;
nkeynes@435
   509
    gui_colour_trace.red = 156*256;
nkeynes@435
   510
    gui_colour_trace.green = 78*256;
nkeynes@435
   511
    gui_colour_trace.blue = 201*256;
nkeynes@435
   512
    gui_colour_debug = gui_colour_pc;
nkeynes@435
   513
    gui_colour_break.red = 65535;
nkeynes@435
   514
    gui_colour_break.green = gui_colour_break.blue = 192*256;
nkeynes@435
   515
    gui_colour_temp_break.red = gui_colour_temp_break.green = 128*256;
nkeynes@435
   516
    gui_colour_temp_break.blue = 32*256;
nkeynes@435
   517
    gui_colour_white.red = gui_colour_white.green = gui_colour_white.blue = 65535;
nkeynes@435
   518
nkeynes@435
   519
    map = gdk_colormap_new(gdk_visual_get_best(), TRUE);
nkeynes@435
   520
    gdk_colormap_alloc_color(map, &gui_colour_normal, TRUE, TRUE);
nkeynes@435
   521
    gdk_colormap_alloc_color(map, &gui_colour_changed, TRUE, TRUE);
nkeynes@435
   522
    gdk_colormap_alloc_color(map, &gui_colour_error, TRUE, TRUE);
nkeynes@435
   523
    gdk_colormap_alloc_color(map, &gui_colour_warn, TRUE, TRUE);
nkeynes@435
   524
    gdk_colormap_alloc_color(map, &gui_colour_pc, TRUE, TRUE);
nkeynes@435
   525
    gdk_colormap_alloc_color(map, &gui_colour_debug, TRUE, TRUE);
nkeynes@435
   526
    gdk_colormap_alloc_color(map, &gui_colour_trace, TRUE, TRUE);
nkeynes@435
   527
    gdk_colormap_alloc_color(map, &gui_colour_break, TRUE, TRUE);
nkeynes@435
   528
    gdk_colormap_alloc_color(map, &gui_colour_temp_break, TRUE, TRUE);
nkeynes@435
   529
    gdk_colormap_alloc_color(map, &gui_colour_white, TRUE, TRUE);
nkeynes@435
   530
    gui_fixed_font = pango_font_description_from_string("Courier 10");
nkeynes@435
   531
}
nkeynes@450
   532
nkeynes@455
   533
gint gtk_gui_run_property_dialog( const gchar *title, GtkWidget *panel, gtk_dialog_done_fn fn )
nkeynes@450
   534
{
nkeynes@450
   535
    GtkWidget *dialog =
nkeynes@736
   536
        gtk_dialog_new_with_buttons(title, main_window_get_frame(main_win), 
nkeynes@736
   537
                GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
nkeynes@736
   538
                GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
nkeynes@736
   539
                GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
nkeynes@736
   540
                NULL);
nkeynes@450
   541
    gint result;
nkeynes@450
   542
    gtk_widget_show_all(panel);
nkeynes@450
   543
    gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
nkeynes@450
   544
    result = gtk_dialog_run( GTK_DIALOG(dialog) );
nkeynes@455
   545
    if( fn != NULL ) {
nkeynes@736
   546
        fn(panel, result == GTK_RESPONSE_ACCEPT);
nkeynes@455
   547
    }
nkeynes@450
   548
    gtk_widget_destroy( dialog );
nkeynes@450
   549
    return result;
nkeynes@450
   550
}
nkeynes@455
   551
nkeynes@455
   552
void gtk_gui_enable_action( const gchar *action, gboolean enable )
nkeynes@455
   553
{
nkeynes@455
   554
    gtk_action_set_sensitive( gtk_action_group_get_action( global_action_group, action), enable);
nkeynes@455
   555
}
nkeynes@477
   556
nkeynes@477
   557
static void delete_frame_buffer( guchar *pixels, gpointer buffer )
nkeynes@477
   558
{
nkeynes@477
   559
    if( buffer != NULL ) {
nkeynes@736
   560
        g_free(buffer);
nkeynes@477
   561
    }
nkeynes@477
   562
}
nkeynes@477
   563
nkeynes@477
   564
GdkPixbuf *gdk_pixbuf_new_from_frame_buffer( frame_buffer_t buffer )
nkeynes@477
   565
{
nkeynes@480
   566
    return gdk_pixbuf_new_from_data( (unsigned char *)buffer->data, 
nkeynes@736
   567
            GDK_COLORSPACE_RGB,
nkeynes@736
   568
            (buffer->colour_format == COLFMT_BGRA8888),
nkeynes@736
   569
            8,
nkeynes@736
   570
            buffer->width,
nkeynes@736
   571
            buffer->height,
nkeynes@736
   572
            buffer->rowstride,
nkeynes@736
   573
            delete_frame_buffer,
nkeynes@736
   574
            buffer );
nkeynes@477
   575
}
nkeynes@608
   576
nkeynes@608
   577
/**
nkeynes@608
   578
 * Extract the keyval of the key event if no modifier keys were pressed -
nkeynes@608
   579
 * in other words get the keyval of the key by itself. The other way around
nkeynes@608
   580
 * would be to use the hardware keysyms directly rather than the keyvals,
nkeynes@608
   581
 * but the mapping looks to be messier.
nkeynes@608
   582
 */
nkeynes@608
   583
uint16_t gtk_get_unmodified_keyval( GdkEventKey *event )
nkeynes@608
   584
{
nkeynes@608
   585
    GdkKeymap *keymap = gdk_keymap_get_default();
nkeynes@608
   586
    guint keyval;
nkeynes@736
   587
nkeynes@608
   588
    gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
nkeynes@736
   589
                                         NULL, NULL, NULL );
nkeynes@608
   590
    return keyval;
nkeynes@608
   591
}
nkeynes@608
   592
nkeynes@630
   593
.