Search
lxdream.org :: lxdream/src/gtkui/gtkui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtkui.c
changeset 630:50addb748df5
prev622:3554650afb26
next658:f5926310bfbe
author nkeynes
date Tue Feb 12 08:36:29 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Bug #55: Fix relative path handling
Canonicalize paths before passing to the file 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@508
    19
#include "lxdream.h"
nkeynes@437
    20
#include <sys/time.h>
nkeynes@437
    21
#include <time.h>
nkeynes@508
    22
#include <glib/gi18n.h>
nkeynes@618
    23
#include <gtk/gtkversion.h>
nkeynes@435
    24
#include "dreamcast.h"
nkeynes@477
    25
#include "display.h"
nkeynes@464
    26
#include "gdrom/gdrom.h"
nkeynes@537
    27
#include "gtkui/gtkui.h"
nkeynes@435
    28
nkeynes@622
    29
/* Base GUI clock is 10ms */
nkeynes@622
    30
#define GUI_TICK_PERIOD 10000000
nkeynes@435
    31
nkeynes@435
    32
void gtk_gui_start( void );
nkeynes@435
    33
void gtk_gui_stop( void );
nkeynes@435
    34
void gtk_gui_alloc_resources ( void );
nkeynes@435
    35
uint32_t gtk_gui_run_slice( uint32_t nanosecs );
nkeynes@435
    36
nkeynes@435
    37
struct dreamcast_module gtk_gui_module = { "gui", NULL,
nkeynes@435
    38
					   gtk_gui_update, 
nkeynes@435
    39
					   gtk_gui_start, 
nkeynes@435
    40
					   gtk_gui_run_slice, 
nkeynes@435
    41
					   gtk_gui_stop, 
nkeynes@435
    42
					   NULL, NULL };
nkeynes@435
    43
nkeynes@435
    44
/**
nkeynes@435
    45
 * Single-instance windows (at most one)
nkeynes@435
    46
 */
nkeynes@435
    47
static main_window_t main_win = NULL;
nkeynes@435
    48
static debug_window_t debug_win = NULL;
nkeynes@435
    49
static mmio_window_t mmio_win = NULL;
nkeynes@435
    50
nkeynes@435
    51
/**
nkeynes@455
    52
 * UIManager and action helpers
nkeynes@455
    53
 */
nkeynes@455
    54
static GtkUIManager *global_ui_manager;
nkeynes@455
    55
static GtkActionGroup *global_action_group;
nkeynes@455
    56
nkeynes@455
    57
/**
nkeynes@435
    58
 * Count of running nanoseconds - used to cut back on the GUI runtime
nkeynes@435
    59
 */
nkeynes@435
    60
static uint32_t gtk_gui_nanos = 0;
nkeynes@622
    61
static uint32_t gtk_gui_ticks = 0;
nkeynes@437
    62
static struct timeval gtk_gui_lasttv;
nkeynes@435
    63
nkeynes@530
    64
static gboolean gtk_gui_init_ok = FALSE;
nkeynes@530
    65
nkeynes@455
    66
#define ENABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,TRUE)
nkeynes@455
    67
#define DISABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,FALSE)
nkeynes@455
    68
nkeynes@455
    69
// UI Actions
nkeynes@455
    70
static const GtkActionEntry ui_actions[] = {
nkeynes@508
    71
    { "FileMenu", NULL, N_("_File") },
nkeynes@508
    72
    { "SettingsMenu", NULL, N_("_Settings") },
nkeynes@508
    73
    { "HelpMenu", NULL, N_("_Help") },
nkeynes@543
    74
    { "LoadBinary", NULL, N_("Load _Binary..."), NULL, N_("Load and run a program binary"), G_CALLBACK(load_binary_action_callback) },
nkeynes@508
    75
    { "Reset", GTK_STOCK_REFRESH, N_("_Reset"), "<control>R", N_("Reset dreamcast"), G_CALLBACK(reset_action_callback) },
nkeynes@508
    76
    { "Pause", GTK_STOCK_MEDIA_PAUSE, N_("_Pause"), NULL, N_("Pause dreamcast"), G_CALLBACK(pause_action_callback) },
nkeynes@508
    77
    { "Run", GTK_STOCK_MEDIA_PLAY, N_("Resume"), NULL, N_("Resume"), G_CALLBACK(resume_action_callback) },
nkeynes@508
    78
    { "LoadState", GTK_STOCK_REVERT_TO_SAVED, N_("_Load state..."), "F4", N_("Load an lxdream save state"), G_CALLBACK(load_state_action_callback) },
nkeynes@508
    79
    { "SaveState", GTK_STOCK_SAVE_AS, N_("_Save state..."), "F3", N_("Create an lxdream save state"), G_CALLBACK(save_state_action_callback) },
nkeynes@508
    80
    { "Exit", GTK_STOCK_QUIT, N_("E_xit"), NULL, N_("Exit lxdream"), G_CALLBACK(exit_action_callback) },
nkeynes@508
    81
    { "GdromSettings", NULL, N_("_GD-Rom...") },
nkeynes@508
    82
    { "GdromUnmount", NULL, N_("_Empty") },
nkeynes@508
    83
    { "GdromMount", GTK_STOCK_CDROM, N_("_Open Image..."), "<control>O", N_("Mount a cdrom disc"), G_CALLBACK(mount_action_callback) },
nkeynes@508
    84
    { "PathSettings", NULL, N_("_Paths..."), NULL, N_("Configure files and paths"), G_CALLBACK(path_settings_callback) }, 
nkeynes@508
    85
    { "AudioSettings", NULL, N_("_Audio..."), NULL, N_("Configure audio output"), G_CALLBACK(audio_settings_callback) },
nkeynes@508
    86
    { "ControllerSettings", NULL, N_("_Controllers..."), NULL, N_("Configure controllers"), G_CALLBACK(maple_settings_callback) },
nkeynes@508
    87
    { "NetworkSettings", NULL, N_("_Network..."), NULL, N_("Configure network settings"), G_CALLBACK(network_settings_callback) },
nkeynes@508
    88
    { "VideoSettings", NULL, N_("_Video..."), NULL,N_( "Configure video output"), G_CALLBACK(video_settings_callback) },
nkeynes@508
    89
    { "About", GTK_STOCK_ABOUT, N_("_About..."), NULL, N_("About lxdream"), G_CALLBACK(about_action_callback) },
nkeynes@508
    90
    { "DebugMenu", NULL, N_("_Debug") },
nkeynes@508
    91
    { "Debugger", NULL, N_("_Debugger"), NULL, N_("Open debugger window"), G_CALLBACK(debugger_action_callback) },
nkeynes@508
    92
    { "DebugMem", NULL, N_("View _Memory"), NULL, N_("View memory dump"), G_CALLBACK(debug_memory_action_callback) },
nkeynes@508
    93
    { "DebugMmio", NULL, N_("View IO _Registers"), NULL, N_("View MMIO Registers"), G_CALLBACK(debug_mmio_action_callback) },
nkeynes@508
    94
    { "SaveScene", NULL, N_("_Save Scene"), NULL, N_("Save next rendered scene"), G_CALLBACK(save_scene_action_callback) },
nkeynes@508
    95
    { "SingleStep", GTK_STOCK_REDO, N_("_Single Step"), NULL, N_("Single step"), G_CALLBACK(debug_step_action_callback) },
nkeynes@508
    96
    { "RunTo", GTK_STOCK_GOTO_LAST, N_("Run _To"), NULL, N_("Run to"), G_CALLBACK( debug_runto_action_callback) },
nkeynes@508
    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@455
   100
    { "FullScreen", NULL, "_Full Screen", "<alt>Return", "Toggle full screen video", G_CALLBACK(fullscreen_toggle_callback), 0 },
nkeynes@455
   101
};
nkeynes@455
   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@455
   127
    "   <separator/>"
nkeynes@455
   128
    "   <menuitem action='FullScreen'/>"
nkeynes@455
   129
    "  </menu>"
nkeynes@455
   130
    "  <menu action='HelpMenu'>"
nkeynes@455
   131
    "   <menuitem action='About'/>"
nkeynes@455
   132
    "  </menu>"
nkeynes@455
   133
    " </menubar>"
nkeynes@455
   134
    " <toolbar name='MainToolbar'>"
nkeynes@464
   135
    "  <toolitem action='GdromMount'/>"
nkeynes@455
   136
    "  <toolitem action='Reset'/>"
nkeynes@455
   137
    "  <toolitem action='Pause'/>"
nkeynes@455
   138
    "  <toolitem action='Run'/>"
nkeynes@455
   139
    "  <separator/>"
nkeynes@455
   140
    "  <toolitem action='LoadState'/>"
nkeynes@455
   141
    "  <toolitem action='SaveState'/>"
nkeynes@455
   142
    " </toolbar>"
nkeynes@455
   143
    " <menubar name='DebugMenu'>"
nkeynes@455
   144
    "  <menu action='FileMenu'>"
nkeynes@477
   145
    "   <menuitem action='GdromSettings'/>"
nkeynes@455
   146
    "   <separator/>"
nkeynes@455
   147
    "   <menuitem action='Reset'/>"
nkeynes@455
   148
    "   <separator/>"
nkeynes@455
   149
    "   <menuitem action='LoadState'/>"
nkeynes@455
   150
    "   <menuitem action='SaveState'/>"
nkeynes@455
   151
    "   <separator/>"
nkeynes@455
   152
    "   <menuitem action='Exit'/>"
nkeynes@455
   153
    "  </menu>"
nkeynes@455
   154
    "  <menu action='DebugMenu'>"
nkeynes@455
   155
    "   <menuitem action='DebugMem'/>"
nkeynes@455
   156
    "   <menuitem action='DebugMmio'/>"
nkeynes@455
   157
    "   <menuitem action='SaveScene'/>"
nkeynes@455
   158
    "   <separator/>"
nkeynes@455
   159
    "   <menuitem action='SetBreakpoint'/>"
nkeynes@455
   160
    "   <menuitem action='Pause'/>"
nkeynes@455
   161
    "   <menuitem action='SingleStep'/>"
nkeynes@455
   162
    "   <menuitem action='RunTo'/>"
nkeynes@455
   163
    "   <menuitem action='Run'/>"
nkeynes@455
   164
    "  </menu>"
nkeynes@455
   165
    "  <menu action='SettingsMenu'>"
nkeynes@455
   166
    "   <menuitem action='PathSettings'/>"
nkeynes@455
   167
    "   <menuitem action='AudioSettings'/>"
nkeynes@455
   168
    "   <menuitem action='ControllerSettings'/>"
nkeynes@455
   169
    "   <menuitem action='NetworkSettings'/>"
nkeynes@455
   170
    "   <menuitem action='VideoSettings'/>"
nkeynes@455
   171
    "   <separator/>"
nkeynes@455
   172
    "   <menuitem action='FullScreen'/>"
nkeynes@455
   173
    "  </menu>"
nkeynes@455
   174
    "  <menu action='HelpMenu'>"
nkeynes@455
   175
    "   <menuitem action='About'/>"
nkeynes@455
   176
    "  </menu>"
nkeynes@455
   177
    " </menubar>"
nkeynes@455
   178
    " <toolbar name='DebugToolbar'>"
nkeynes@464
   179
    "  <toolitem action='GdromMount'/>"
nkeynes@455
   180
    "  <toolitem action='Reset'/>"
nkeynes@455
   181
    "  <toolitem action='Pause'/>"
nkeynes@455
   182
    "  <separator/>"
nkeynes@455
   183
    "  <toolitem action='SingleStep'/>"
nkeynes@455
   184
    "  <toolitem action='RunTo'/>"
nkeynes@455
   185
    "  <toolitem action='Run'/>"
nkeynes@455
   186
    "  <toolitem action='SetBreakpoint'/>"
nkeynes@455
   187
    "  <separator/>"
nkeynes@455
   188
    "  <toolitem action='LoadState'/>"
nkeynes@455
   189
    "  <toolitem action='SaveState'/>"
nkeynes@455
   190
    " </toolbar>"
nkeynes@455
   191
    "</ui>";
nkeynes@455
   192
nkeynes@435
   193
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@435
   194
{
nkeynes@530
   195
    gtk_gui_init_ok = gtk_init_check( argc, argv );
nkeynes@530
   196
    return gtk_gui_init_ok;
nkeynes@435
   197
}
nkeynes@435
   198
nkeynes@435
   199
gboolean gui_init( gboolean withDebug )
nkeynes@435
   200
{
nkeynes@530
   201
    if( gtk_gui_init_ok ) {
nkeynes@530
   202
	GError *error = NULL;
nkeynes@530
   203
	dreamcast_register_module( &gtk_gui_module );
nkeynes@530
   204
	gtk_gui_alloc_resources();
nkeynes@530
   205
	
nkeynes@530
   206
	global_action_group = gtk_action_group_new("MenuActions");
nkeynes@530
   207
	gtk_action_group_set_translation_domain( global_action_group, NULL );
nkeynes@530
   208
	gtk_action_group_add_actions( global_action_group, ui_actions, G_N_ELEMENTS(ui_actions), NULL );
nkeynes@530
   209
	gtk_action_group_add_toggle_actions( global_action_group, ui_toggle_actions, G_N_ELEMENTS(ui_toggle_actions), NULL );
nkeynes@530
   210
	gtk_gui_enable_action("AudioSettings", FALSE);
nkeynes@530
   211
	gtk_gui_enable_action("NetworkSettings", FALSE);
nkeynes@530
   212
	gtk_gui_enable_action("VideoSettings", FALSE);
nkeynes@530
   213
	
nkeynes@530
   214
	global_ui_manager = gtk_ui_manager_new();
nkeynes@530
   215
	gtk_ui_manager_set_add_tearoffs(global_ui_manager, TRUE);
nkeynes@530
   216
	gtk_ui_manager_insert_action_group( global_ui_manager, global_action_group, 0 );
nkeynes@530
   217
	
nkeynes@530
   218
	if (!gtk_ui_manager_add_ui_from_string (global_ui_manager, ui_description, -1, &error)) {
nkeynes@530
   219
	    g_message ("building menus failed: %s", error->message);
nkeynes@530
   220
	    g_error_free (error);
nkeynes@530
   221
	    exit(1);
nkeynes@530
   222
	}
nkeynes@530
   223
	GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
nkeynes@530
   224
	GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu");
nkeynes@530
   225
	GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/MainToolbar");
nkeynes@530
   226
	
nkeynes@530
   227
	GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu/FileMenu/GdromSettings");
nkeynes@530
   228
	gdrom_menu_init();
nkeynes@530
   229
	GtkWidget *gdrommenu = gdrom_menu_new();
nkeynes@530
   230
	gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
nkeynes@530
   231
	main_win = main_window_new( APP_NAME " " APP_VERSION, menubar, toolbar, accel_group  );
nkeynes@608
   232
	main_window_set_use_grab(main_win, TRUE);
nkeynes@530
   233
	if( withDebug ) {
nkeynes@530
   234
	    gtk_gui_show_debugger();
nkeynes@530
   235
	}
nkeynes@464
   236
nkeynes@530
   237
	return TRUE;
nkeynes@530
   238
    } else {
nkeynes@530
   239
	return FALSE;
nkeynes@455
   240
    }
nkeynes@435
   241
}
nkeynes@435
   242
nkeynes@435
   243
void gui_main_loop(void)
nkeynes@435
   244
{
nkeynes@543
   245
    gtk_gui_update();
nkeynes@435
   246
    gtk_main();
nkeynes@435
   247
}
nkeynes@435
   248
nkeynes@537
   249
void gui_update_state(void)
nkeynes@537
   250
{
nkeynes@537
   251
    gtk_gui_update();
nkeynes@537
   252
}
nkeynes@537
   253
nkeynes@447
   254
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@447
   255
{
nkeynes@447
   256
    if( main_win != NULL ) {
nkeynes@447
   257
	va_list args;
nkeynes@447
   258
	GtkWidget *dialog = 
nkeynes@447
   259
	    gtk_message_dialog_new( main_window_get_frame(main_win), GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
nkeynes@447
   260
				    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL );
nkeynes@447
   261
	va_start(args, msg);
nkeynes@447
   262
	gchar *markup = g_markup_vprintf_escaped( msg, args );
nkeynes@447
   263
	va_end( args );
nkeynes@447
   264
	gtk_message_dialog_set_markup( GTK_MESSAGE_DIALOG(dialog), markup );
nkeynes@447
   265
	g_free(markup);
nkeynes@447
   266
	gtk_dialog_run(GTK_DIALOG(dialog));
nkeynes@447
   267
	gtk_widget_destroy(dialog);
nkeynes@447
   268
	return TRUE;
nkeynes@447
   269
    }
nkeynes@447
   270
    return FALSE;
nkeynes@447
   271
}
nkeynes@447
   272
nkeynes@435
   273
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@435
   274
{
nkeynes@435
   275
nkeynes@435
   276
}
nkeynes@435
   277
nkeynes@435
   278
void gtk_gui_show_debugger()
nkeynes@435
   279
{
nkeynes@435
   280
    if( debug_win ) {
nkeynes@435
   281
	debug_window_show(debug_win, TRUE);
nkeynes@435
   282
    } else {
nkeynes@455
   283
	GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
nkeynes@455
   284
	GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu");
nkeynes@455
   285
	GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugToolbar");
nkeynes@477
   286
	GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu/FileMenu/GdromSettings");
nkeynes@477
   287
	GtkWidget *gdrommenu = gdrom_menu_new();
nkeynes@477
   288
	gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
nkeynes@508
   289
	gchar *title = g_strdup_printf( APP_NAME " " APP_VERSION " :: %s", _("Debugger"));
nkeynes@508
   290
	debug_win = debug_window_new( title, menubar, toolbar, accel_group  );
nkeynes@508
   291
	g_free(title);
nkeynes@435
   292
    }
nkeynes@455
   293
}
nkeynes@455
   294
nkeynes@455
   295
void gtk_gui_show_mmio()
nkeynes@455
   296
{
nkeynes@455
   297
    if( mmio_win ) {
nkeynes@455
   298
	mmio_window_show(mmio_win, TRUE);
nkeynes@455
   299
    } else {
nkeynes@508
   300
	gchar *title = g_strdup_printf( APP_NAME " " APP_VERSION " :: %s", _("MMIO Registers"));
nkeynes@508
   301
	mmio_win = mmio_window_new( title );
nkeynes@508
   302
	g_free(title);
nkeynes@455
   303
    }
nkeynes@455
   304
}
nkeynes@455
   305
nkeynes@455
   306
nkeynes@455
   307
main_window_t gtk_gui_get_main()
nkeynes@455
   308
{
nkeynes@455
   309
    return main_win;
nkeynes@455
   310
}
nkeynes@455
   311
nkeynes@455
   312
debug_window_t gtk_gui_get_debugger()
nkeynes@455
   313
{
nkeynes@455
   314
    return debug_win;
nkeynes@455
   315
}
nkeynes@455
   316
nkeynes@455
   317
mmio_window_t gtk_gui_get_mmio()
nkeynes@455
   318
{
nkeynes@455
   319
    return mmio_win;
nkeynes@455
   320
}
nkeynes@435
   321
nkeynes@435
   322
GtkWidget *gtk_gui_get_renderarea()
nkeynes@435
   323
{
nkeynes@530
   324
    if( main_win == NULL ) {
nkeynes@530
   325
	return NULL;
nkeynes@530
   326
    } else {
nkeynes@530
   327
	return main_window_get_renderarea(main_win);
nkeynes@530
   328
    }
nkeynes@435
   329
}
nkeynes@435
   330
nkeynes@435
   331
/**
nkeynes@435
   332
 * Hook called when DC starts running. Just disables the run/step buttons
nkeynes@435
   333
 * and enables the stop button.
nkeynes@435
   334
 */
nkeynes@435
   335
void gtk_gui_start( void )
nkeynes@435
   336
{
nkeynes@435
   337
    main_window_set_running( main_win, TRUE );
nkeynes@435
   338
    if( debug_win != NULL ) {
nkeynes@435
   339
	debug_window_set_running( debug_win, TRUE );
nkeynes@435
   340
    }
nkeynes@437
   341
    gtk_gui_nanos = 0;
nkeynes@437
   342
    gettimeofday(&gtk_gui_lasttv,NULL);
nkeynes@435
   343
}
nkeynes@435
   344
nkeynes@435
   345
/**
nkeynes@435
   346
 * Hook called when DC stops running. Enables the run/step buttons
nkeynes@435
   347
 * and disables the stop button.
nkeynes@435
   348
 */
nkeynes@435
   349
void gtk_gui_stop( void )
nkeynes@435
   350
{
nkeynes@435
   351
    main_window_set_running( main_win, FALSE );
nkeynes@435
   352
    gtk_gui_update();
nkeynes@435
   353
}
nkeynes@435
   354
nkeynes@435
   355
void gtk_gui_update( void )
nkeynes@435
   356
{
nkeynes@546
   357
    if( global_action_group ) {
nkeynes@546
   358
	gtk_gui_enable_action("Run", dreamcast_can_run() && !dreamcast_is_running() );
nkeynes@546
   359
	gtk_gui_enable_action("Pause", dreamcast_is_running() );
nkeynes@546
   360
    }
nkeynes@435
   361
    if( debug_win ) {
nkeynes@435
   362
	debug_window_set_running( debug_win, FALSE );
nkeynes@435
   363
	debug_window_update(debug_win);
nkeynes@435
   364
    }
nkeynes@435
   365
    if( mmio_win ) {
nkeynes@457
   366
	mmio_window_update(mmio_win);
nkeynes@435
   367
    }
nkeynes@455
   368
    dump_window_update_all();
nkeynes@435
   369
}
nkeynes@435
   370
nkeynes@435
   371
/**
nkeynes@622
   372
 * Module run-slice. Run the event loop 100 times/second (doesn't really need to be
nkeynes@622
   373
 * any more often than this), and update the speed display 10 times/second. 
nkeynes@622
   374
 *
nkeynes@622
   375
 * Also detect if we're running too fast here and yield for a bit
nkeynes@435
   376
 */
nkeynes@435
   377
uint32_t gtk_gui_run_slice( uint32_t nanosecs ) 
nkeynes@435
   378
{
nkeynes@435
   379
    gtk_gui_nanos += nanosecs;
nkeynes@622
   380
    if( gtk_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@622
   381
	gtk_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@622
   382
	gtk_gui_ticks ++;
nkeynes@622
   383
	uint32_t current_period = gtk_gui_ticks * GUI_TICK_PERIOD;
nkeynes@622
   384
nkeynes@622
   385
	// Run the event loop
nkeynes@622
   386
	while( gtk_events_pending() )
nkeynes@622
   387
	    gtk_main_iteration();	
nkeynes@622
   388
nkeynes@437
   389
	struct timeval tv;
nkeynes@437
   390
	gettimeofday(&tv,NULL);
nkeynes@622
   391
	uint32_t ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@622
   392
	    (tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000;
nkeynes@622
   393
	if( (ns * 1.05) < current_period ) {
nkeynes@622
   394
	    // We've gotten ahead - sleep for a little bit
nkeynes@622
   395
	    struct timespec tv;
nkeynes@622
   396
	    tv.tv_sec = 0;
nkeynes@622
   397
	    tv.tv_nsec = current_period - ns;
nkeynes@622
   398
	    nanosleep(&tv, &tv);
nkeynes@622
   399
	}
nkeynes@622
   400
nkeynes@622
   401
	/* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@622
   402
	 * save the current tv value */
nkeynes@622
   403
	if( gtk_gui_ticks > 10 ) {
nkeynes@622
   404
	    gtk_gui_ticks -= 10;
nkeynes@622
   405
	    
nkeynes@622
   406
	    double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@622
   407
	    gtk_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@622
   408
	    gtk_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@622
   409
	    main_window_set_speed( main_win, speed );
nkeynes@622
   410
	}
nkeynes@435
   411
    }
nkeynes@435
   412
    return nanosecs;
nkeynes@435
   413
}
nkeynes@435
   414
nkeynes@435
   415
nkeynes@435
   416
PangoFontDescription *gui_fixed_font;
nkeynes@435
   417
GdkColor gui_colour_normal, gui_colour_changed, gui_colour_error;
nkeynes@435
   418
GdkColor gui_colour_warn, gui_colour_pc, gui_colour_debug;
nkeynes@435
   419
GdkColor gui_colour_trace, gui_colour_break, gui_colour_temp_break;
nkeynes@435
   420
GdkColor gui_colour_white;
nkeynes@435
   421
nkeynes@435
   422
void gtk_gui_alloc_resources() {
nkeynes@435
   423
    GdkColormap *map;
nkeynes@435
   424
nkeynes@435
   425
    gui_colour_normal.red = gui_colour_normal.green = gui_colour_normal.blue = 0;
nkeynes@435
   426
    gui_colour_changed.red = gui_colour_changed.green = 64*256;
nkeynes@435
   427
    gui_colour_changed.blue = 154*256;
nkeynes@435
   428
    gui_colour_error.red = 65535;
nkeynes@435
   429
    gui_colour_error.green = gui_colour_error.blue = 64*256;
nkeynes@435
   430
    gui_colour_pc.red = 32*256;
nkeynes@435
   431
    gui_colour_pc.green = 170*256;
nkeynes@435
   432
    gui_colour_pc.blue = 52*256;
nkeynes@435
   433
    gui_colour_warn = gui_colour_changed;
nkeynes@435
   434
    gui_colour_trace.red = 156*256;
nkeynes@435
   435
    gui_colour_trace.green = 78*256;
nkeynes@435
   436
    gui_colour_trace.blue = 201*256;
nkeynes@435
   437
    gui_colour_debug = gui_colour_pc;
nkeynes@435
   438
    gui_colour_break.red = 65535;
nkeynes@435
   439
    gui_colour_break.green = gui_colour_break.blue = 192*256;
nkeynes@435
   440
    gui_colour_temp_break.red = gui_colour_temp_break.green = 128*256;
nkeynes@435
   441
    gui_colour_temp_break.blue = 32*256;
nkeynes@435
   442
    gui_colour_white.red = gui_colour_white.green = gui_colour_white.blue = 65535;
nkeynes@435
   443
nkeynes@435
   444
    map = gdk_colormap_new(gdk_visual_get_best(), TRUE);
nkeynes@435
   445
    gdk_colormap_alloc_color(map, &gui_colour_normal, TRUE, TRUE);
nkeynes@435
   446
    gdk_colormap_alloc_color(map, &gui_colour_changed, TRUE, TRUE);
nkeynes@435
   447
    gdk_colormap_alloc_color(map, &gui_colour_error, TRUE, TRUE);
nkeynes@435
   448
    gdk_colormap_alloc_color(map, &gui_colour_warn, TRUE, TRUE);
nkeynes@435
   449
    gdk_colormap_alloc_color(map, &gui_colour_pc, TRUE, TRUE);
nkeynes@435
   450
    gdk_colormap_alloc_color(map, &gui_colour_debug, TRUE, TRUE);
nkeynes@435
   451
    gdk_colormap_alloc_color(map, &gui_colour_trace, TRUE, TRUE);
nkeynes@435
   452
    gdk_colormap_alloc_color(map, &gui_colour_break, TRUE, TRUE);
nkeynes@435
   453
    gdk_colormap_alloc_color(map, &gui_colour_temp_break, TRUE, TRUE);
nkeynes@435
   454
    gdk_colormap_alloc_color(map, &gui_colour_white, TRUE, TRUE);
nkeynes@435
   455
    gui_fixed_font = pango_font_description_from_string("Courier 10");
nkeynes@435
   456
}
nkeynes@450
   457
nkeynes@455
   458
gint gtk_gui_run_property_dialog( const gchar *title, GtkWidget *panel, gtk_dialog_done_fn fn )
nkeynes@450
   459
{
nkeynes@450
   460
    GtkWidget *dialog =
nkeynes@450
   461
	gtk_dialog_new_with_buttons(title, main_window_get_frame(main_win), 
nkeynes@450
   462
				    GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
nkeynes@450
   463
				    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
nkeynes@450
   464
				    GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
nkeynes@450
   465
				    NULL);
nkeynes@450
   466
    gint result;
nkeynes@450
   467
    gtk_widget_show_all(panel);
nkeynes@450
   468
    gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
nkeynes@450
   469
    result = gtk_dialog_run( GTK_DIALOG(dialog) );
nkeynes@455
   470
    if( fn != NULL ) {
nkeynes@455
   471
	fn(panel, result == GTK_RESPONSE_ACCEPT);
nkeynes@455
   472
    }
nkeynes@450
   473
    gtk_widget_destroy( dialog );
nkeynes@450
   474
    return result;
nkeynes@450
   475
}
nkeynes@455
   476
nkeynes@455
   477
void gtk_gui_enable_action( const gchar *action, gboolean enable )
nkeynes@455
   478
{
nkeynes@455
   479
    gtk_action_set_sensitive( gtk_action_group_get_action( global_action_group, action), enable);
nkeynes@455
   480
}
nkeynes@477
   481
nkeynes@477
   482
static void delete_frame_buffer( guchar *pixels, gpointer buffer )
nkeynes@477
   483
{
nkeynes@477
   484
    if( buffer != NULL ) {
nkeynes@477
   485
	g_free(buffer);
nkeynes@477
   486
    }
nkeynes@477
   487
}
nkeynes@477
   488
nkeynes@477
   489
GdkPixbuf *gdk_pixbuf_new_from_frame_buffer( frame_buffer_t buffer )
nkeynes@477
   490
{
nkeynes@480
   491
    return gdk_pixbuf_new_from_data( (unsigned char *)buffer->data, 
nkeynes@477
   492
				     GDK_COLORSPACE_RGB,
nkeynes@477
   493
				     (buffer->colour_format == COLFMT_BGRA8888),
nkeynes@477
   494
				     8,
nkeynes@477
   495
				     buffer->width,
nkeynes@477
   496
				     buffer->height,
nkeynes@477
   497
				     buffer->rowstride,
nkeynes@477
   498
				     delete_frame_buffer,
nkeynes@477
   499
				     buffer );
nkeynes@477
   500
}
nkeynes@608
   501
nkeynes@608
   502
/**
nkeynes@608
   503
 * Extract the keyval of the key event if no modifier keys were pressed -
nkeynes@608
   504
 * in other words get the keyval of the key by itself. The other way around
nkeynes@608
   505
 * would be to use the hardware keysyms directly rather than the keyvals,
nkeynes@608
   506
 * but the mapping looks to be messier.
nkeynes@608
   507
 */
nkeynes@608
   508
uint16_t gtk_get_unmodified_keyval( GdkEventKey *event )
nkeynes@608
   509
{
nkeynes@608
   510
    GdkKeymap *keymap = gdk_keymap_get_default();
nkeynes@608
   511
    guint keyval;
nkeynes@608
   512
    
nkeynes@608
   513
    gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval, 
nkeynes@608
   514
					 NULL, NULL, NULL );
nkeynes@608
   515
    return keyval;
nkeynes@608
   516
}
nkeynes@608
   517
nkeynes@630
   518
gchar *get_absolute_path( const gchar *in_path )
nkeynes@630
   519
{
nkeynes@630
   520
    char tmp[PATH_MAX];
nkeynes@630
   521
    if( in_path == NULL ) {
nkeynes@630
   522
	return NULL;
nkeynes@630
   523
    }
nkeynes@630
   524
    if( in_path[0] == '/' || in_path[0] == 0 ) {
nkeynes@630
   525
	return g_strdup(in_path);
nkeynes@630
   526
    } else {
nkeynes@630
   527
	getcwd(tmp, sizeof(tmp));
nkeynes@630
   528
	return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
nkeynes@630
   529
    }
nkeynes@630
   530
}
nkeynes@630
   531
nkeynes@618
   532
/************* X11-specificness **********/
nkeynes@618
   533
#include <gdk/gdkx.h>
nkeynes@618
   534
nkeynes@618
   535
guint gdk_keycode_to_modifier( GdkDisplay *display, guint keycode )
nkeynes@618
   536
{
nkeynes@618
   537
  int i;
nkeynes@618
   538
  int result = 0;
nkeynes@618
   539
  Display *xdisplay = GDK_DISPLAY_XDISPLAY (display);
nkeynes@618
   540
  XModifierKeymap *keymap = XGetModifierMapping( xdisplay );
nkeynes@618
   541
  for( i=0; i<8*keymap->max_keypermod; i++ ) {
nkeynes@618
   542
    if( keymap->modifiermap[i] == keycode ) {
nkeynes@618
   543
      result = 1 << (i/keymap->max_keypermod);
nkeynes@618
   544
      break;
nkeynes@618
   545
    }
nkeynes@618
   546
  }
nkeynes@618
   547
  XFreeModifiermap(keymap);
nkeynes@618
   548
  return result;
nkeynes@618
   549
}
nkeynes@618
   550
nkeynes@618
   551
#if !(GTK_CHECK_VERSION(2,8,0))
nkeynes@618
   552
/* gdk_display_warp_pointer was added in GTK 2.8. If we're using an earlier
nkeynes@618
   553
 * version, include the code here. (Can't just set the dependency on 2.8 as
nkeynes@618
   554
 * it still hasn't been ported to OSX...) Original copyright statement belo
nkeynes@618
   555
 */
nkeynes@618
   556
nkeynes@618
   557
/* GDK - The GIMP Drawing Kit
nkeynes@618
   558
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
nkeynes@618
   559
 *
nkeynes@618
   560
 * This library is free software; you can redistribute it and/or
nkeynes@618
   561
 * modify it under the terms of the GNU Lesser General Public
nkeynes@618
   562
 * License as published by the Free Software Foundation; either
nkeynes@618
   563
 * version 2 of the License, or (at your option) any later version.
nkeynes@618
   564
 *
nkeynes@618
   565
 * This library is distributed in the hope that it will be useful,
nkeynes@618
   566
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@618
   567
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
nkeynes@618
   568
 * Lesser General Public License for more details.
nkeynes@618
   569
 *
nkeynes@618
   570
 * You should have received a copy of the GNU Lesser General Public
nkeynes@618
   571
 * License along with this library; if not, write to the
nkeynes@618
   572
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
nkeynes@618
   573
 * Boston, MA 02111-1307, USA.
nkeynes@618
   574
 */
nkeynes@618
   575
nkeynes@618
   576
/*
nkeynes@618
   577
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
nkeynes@618
   578
 * file for a list of people on the GTK+ Team.  See the ChangeLog
nkeynes@618
   579
 * files for a list of changes.  These files are distributed with
nkeynes@618
   580
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
nkeynes@618
   581
 */
nkeynes@618
   582
void gdk_display_warp_pointer (GdkDisplay *display,
nkeynes@618
   583
                          GdkScreen  *screen,
nkeynes@618
   584
                          gint        x,
nkeynes@618
   585
                          gint        y)
nkeynes@618
   586
{
nkeynes@618
   587
  Display *xdisplay;
nkeynes@618
   588
  Window dest;
nkeynes@618
   589
nkeynes@618
   590
  xdisplay = GDK_DISPLAY_XDISPLAY (display);
nkeynes@618
   591
  dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
nkeynes@618
   592
nkeynes@618
   593
  XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);  
nkeynes@618
   594
}
nkeynes@618
   595
nkeynes@618
   596
#endif
nkeynes@618
   597
.