Search
lxdream.org :: lxdream/src/gui/gtkui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/gtkui.c
changeset 457:af605fd32c0b
prev455:3080881d00d4
next464:8e099fad42a6
author nkeynes
date Sun Oct 21 11:38:02 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Finish updating debug gui window bits
view annotate diff log raw
     1 /**
     2  * $Id: gtkui.c,v 1.6 2007-10-21 11:38:02 nkeynes Exp $
     3  *
     4  * Core GTK-based user interface
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <sys/time.h>
    20 #include <time.h>
    21 #include "dream.h"
    22 #include "dreamcast.h"
    23 #include "gui/gtkui.h"
    26 void gtk_gui_update( void );
    27 void gtk_gui_start( void );
    28 void gtk_gui_stop( void );
    29 void gtk_gui_alloc_resources ( void );
    30 uint32_t gtk_gui_run_slice( uint32_t nanosecs );
    32 struct dreamcast_module gtk_gui_module = { "gui", NULL,
    33 					   gtk_gui_update, 
    34 					   gtk_gui_start, 
    35 					   gtk_gui_run_slice, 
    36 					   gtk_gui_stop, 
    37 					   NULL, NULL };
    39 /**
    40  * Single-instance windows (at most one)
    41  */
    42 static main_window_t main_win = NULL;
    43 static debug_window_t debug_win = NULL;
    44 static mmio_window_t mmio_win = NULL;
    46 /**
    47  * UIManager and action helpers
    48  */
    49 static GtkUIManager *global_ui_manager;
    50 static GtkAccelGroup *global_accel_group;
    51 static GtkActionGroup *global_action_group;
    53 /**
    54  * Count of running nanoseconds - used to cut back on the GUI runtime
    55  */
    56 static uint32_t gtk_gui_nanos = 0;
    57 static struct timeval gtk_gui_lasttv;
    60 #define ENABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,TRUE)
    61 #define DISABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,FALSE)
    63 // UI Actions
    64 static const GtkActionEntry ui_actions[] = {
    65     { "FileMenu", NULL, "_File" },
    66     { "SettingsMenu", NULL, "_Settings" },
    67     { "HelpMenu", NULL, "_Help" },
    68     { "Mount", GTK_STOCK_CDROM, "_Mount...", "<control>O", "Mount a cdrom disc", G_CALLBACK(mount_action_callback) },
    69     { "Reset", GTK_STOCK_REFRESH, "_Reset", "<control>R", "Reset dreamcast", G_CALLBACK(reset_action_callback) },
    70     { "Pause", GTK_STOCK_MEDIA_PAUSE, "_Pause", NULL, "Pause dreamcast", G_CALLBACK(pause_action_callback) },
    71     { "Run", GTK_STOCK_MEDIA_PLAY, "Resume", NULL, "Resume", G_CALLBACK(resume_action_callback) },
    72     { "LoadState", GTK_STOCK_REVERT_TO_SAVED, "_Load state...", "F4", "Load an lxdream save state", G_CALLBACK(load_state_action_callback) },
    73     { "SaveState", GTK_STOCK_SAVE_AS, "_Save state...", "F3", "Create an lxdream save state", G_CALLBACK(save_state_action_callback) },
    74     { "Exit", GTK_STOCK_QUIT, "E_xit", NULL, "Exit lxdream", G_CALLBACK(exit_action_callback) },
    75     { "PathSettings", NULL, "_Paths...", NULL, "Configure files and paths", G_CALLBACK(path_settings_callback) }, 
    76     { "AudioSettings", NULL, "_Audio...", NULL, "Configure audio output", G_CALLBACK(audio_settings_callback) },
    77     { "ControllerSettings", NULL, "_Controllers...", NULL, "Configure controllers", G_CALLBACK(maple_settings_callback) },
    78     { "NetworkSettings", NULL, "_Network...", NULL, "Configure network settings", G_CALLBACK(network_settings_callback) },
    79     { "VideoSettings", NULL, "_Video...", NULL, "Configure video output", G_CALLBACK(video_settings_callback) },
    80     { "About", GTK_STOCK_ABOUT, "_About...", NULL, "About lxdream", G_CALLBACK(about_action_callback) },
    81     { "DebugMenu", NULL, "_Debug" },
    82     { "Debugger", NULL, "_Debugger", NULL, "Open debugger window", G_CALLBACK(debugger_action_callback) },
    83     { "DebugMem", NULL, "View _Memory", NULL, "View memory dump", G_CALLBACK(debug_memory_action_callback) },
    84     { "DebugMmio", NULL, "View IO _Registers", NULL, "View MMIO Registers", G_CALLBACK(debug_mmio_action_callback) },
    85     { "SaveScene", NULL, "_Save Scene", NULL, "Save next rendered scene", G_CALLBACK(save_scene_action_callback) },
    86     { "SingleStep", GTK_STOCK_REDO, "_Single Step", NULL, "Single step", G_CALLBACK(debug_step_action_callback) },
    87     { "RunTo", GTK_STOCK_GOTO_LAST, "Run _To", NULL, "Run to", G_CALLBACK( debug_runto_action_callback) },
    88     { "SetBreakpoint", GTK_STOCK_CLOSE, "_Breakpoint", NULL, "Toggle breakpoint", G_CALLBACK( debug_breakpoint_action_callback) }
    89 };
    90 static const GtkToggleActionEntry ui_toggle_actions[] = {
    91     { "FullScreen", NULL, "_Full Screen", "<alt>Return", "Toggle full screen video", G_CALLBACK(fullscreen_toggle_callback), 0 },
    92 };
    94 // Menus and toolbars
    95 static const char *ui_description =
    96     "<ui>"
    97     " <menubar name='MainMenu'>"
    98     "  <menu action='FileMenu'>"
    99     "   <menuitem action='Mount'/>"
   100     "   <separator/>"
   101     "   <menuitem action='Reset'/>"
   102     "   <menuitem action='Pause'/>"
   103     "   <menuitem action='Run'/>"
   104     "   <menuitem action='Debugger'/>"
   105     "   <separator/>"
   106     "   <menuitem action='LoadState'/>"
   107     "   <menuitem action='SaveState'/>"
   108     "   <separator/>"
   109     "   <menuitem action='Exit'/>"
   110     "  </menu>"
   111     "  <menu action='SettingsMenu'>"
   112     "   <menuitem action='PathSettings'/>"
   113     "   <menuitem action='AudioSettings'/>"
   114     "   <menuitem action='ControllerSettings'/>"
   115     "   <menuitem action='NetworkSettings'/>"
   116     "   <menuitem action='VideoSettings'/>"
   117     "   <separator/>"
   118     "   <menuitem action='FullScreen'/>"
   119     "  </menu>"
   120     "  <menu action='HelpMenu'>"
   121     "   <menuitem action='About'/>"
   122     "  </menu>"
   123     " </menubar>"
   124     " <toolbar name='MainToolbar'>"
   125     "  <toolitem action='Mount'/>"
   126     "  <toolitem action='Reset'/>"
   127     "  <toolitem action='Pause'/>"
   128     "  <toolitem action='Run'/>"
   129     "  <separator/>"
   130     "  <toolitem action='LoadState'/>"
   131     "  <toolitem action='SaveState'/>"
   132     " </toolbar>"
   133     " <menubar name='DebugMenu'>"
   134     "  <menu action='FileMenu'>"
   135     "   <menuitem action='Mount'/>"
   136     "   <separator/>"
   137     "   <menuitem action='Reset'/>"
   138     "   <separator/>"
   139     "   <menuitem action='LoadState'/>"
   140     "   <menuitem action='SaveState'/>"
   141     "   <separator/>"
   142     "   <menuitem action='Exit'/>"
   143     "  </menu>"
   144     "  <menu action='DebugMenu'>"
   145     "   <menuitem action='DebugMem'/>"
   146     "   <menuitem action='DebugMmio'/>"
   147     "   <menuitem action='SaveScene'/>"
   148     "   <separator/>"
   149     "   <menuitem action='SetBreakpoint'/>"
   150     "   <menuitem action='Pause'/>"
   151     "   <menuitem action='SingleStep'/>"
   152     "   <menuitem action='RunTo'/>"
   153     "   <menuitem action='Run'/>"
   154     "  </menu>"
   155     "  <menu action='SettingsMenu'>"
   156     "   <menuitem action='PathSettings'/>"
   157     "   <menuitem action='AudioSettings'/>"
   158     "   <menuitem action='ControllerSettings'/>"
   159     "   <menuitem action='NetworkSettings'/>"
   160     "   <menuitem action='VideoSettings'/>"
   161     "   <separator/>"
   162     "   <menuitem action='FullScreen'/>"
   163     "  </menu>"
   164     "  <menu action='HelpMenu'>"
   165     "   <menuitem action='About'/>"
   166     "  </menu>"
   167     " </menubar>"
   168     " <toolbar name='DebugToolbar'>"
   169     "  <toolitem action='Mount'/>"
   170     "  <toolitem action='Reset'/>"
   171     "  <toolitem action='Pause'/>"
   172     "  <separator/>"
   173     "  <toolitem action='SingleStep'/>"
   174     "  <toolitem action='RunTo'/>"
   175     "  <toolitem action='Run'/>"
   176     "  <toolitem action='SetBreakpoint'/>"
   177     "  <separator/>"
   178     "  <toolitem action='LoadState'/>"
   179     "  <toolitem action='SaveState'/>"
   180     " </toolbar>"
   181     "</ui>";
   185 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   186 {
   187     return gtk_init_check( argc, argv );
   188 }
   190 gboolean gui_init( gboolean withDebug )
   191 {
   192     GError *error = NULL;
   193     dreamcast_register_module( &gtk_gui_module );
   194     gtk_gui_alloc_resources();
   196     global_action_group = gtk_action_group_new("MenuActions");
   197     gtk_action_group_add_actions( global_action_group, ui_actions, G_N_ELEMENTS(ui_actions), NULL );
   198     gtk_action_group_add_toggle_actions( global_action_group, ui_toggle_actions, G_N_ELEMENTS(ui_toggle_actions), NULL );
   199     gtk_gui_enable_action("AudioSettings", FALSE);
   200     gtk_gui_enable_action("NetworkSettings", FALSE);
   201     gtk_gui_enable_action("VideoSettings", FALSE);
   202     global_ui_manager = gtk_ui_manager_new();
   203     gtk_ui_manager_set_add_tearoffs(global_ui_manager, TRUE);
   204     gtk_ui_manager_insert_action_group( global_ui_manager, global_action_group, 0 );
   206     if (!gtk_ui_manager_add_ui_from_string (global_ui_manager, ui_description, -1, &error)) {
   207 	g_message ("building menus failed: %s", error->message);
   208 	g_error_free (error);
   209 	exit(1);
   210     }
   211     GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
   212     GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu");
   213     GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/MainToolbar");
   214     main_win = main_window_new( APP_NAME " " APP_VERSION, menubar, toolbar, accel_group  );
   216     if( withDebug ) {
   217 	gtk_gui_show_debugger();
   218     }
   220     return TRUE;
   221 }
   223 void gui_main_loop(void)
   224 {
   225     gtk_main();
   226 }
   228 gboolean gui_error_dialog( const char *msg, ... )
   229 {
   230     if( main_win != NULL ) {
   231 	va_list args;
   232 	GtkWidget *dialog = 
   233 	    gtk_message_dialog_new( main_window_get_frame(main_win), GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
   234 				    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL );
   235 	va_start(args, msg);
   236 	gchar *markup = g_markup_vprintf_escaped( msg, args );
   237 	va_end( args );
   238 	gtk_message_dialog_set_markup( GTK_MESSAGE_DIALOG(dialog), markup );
   239 	g_free(markup);
   240 	gtk_dialog_run(GTK_DIALOG(dialog));
   241 	gtk_widget_destroy(dialog);
   242 	return TRUE;
   243     }
   244     return FALSE;
   245 }
   247 void gui_update_io_activity( io_activity_type io, gboolean active )
   248 {
   250 }
   252 void gtk_gui_show_debugger()
   253 {
   254     if( debug_win ) {
   255 	debug_window_show(debug_win, TRUE);
   256     } else {
   257 	GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
   258 	GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu");
   259 	GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugToolbar");
   260 	debug_win = debug_window_new( APP_NAME " " APP_VERSION " :: Debugger", menubar, toolbar, accel_group  );
   261     }
   262 }
   264 void gtk_gui_show_mmio()
   265 {
   266     if( mmio_win ) {
   267 	mmio_window_show(mmio_win, TRUE);
   268     } else {
   269 	mmio_win = mmio_window_new( APP_NAME " " APP_VERSION " :: MMIO Registers" );
   270     }
   271 }
   274 main_window_t gtk_gui_get_main()
   275 {
   276     return main_win;
   277 }
   279 debug_window_t gtk_gui_get_debugger()
   280 {
   281     return debug_win;
   282 }
   284 mmio_window_t gtk_gui_get_mmio()
   285 {
   286     return mmio_win;
   287 }
   289 GtkWidget *gtk_gui_get_renderarea()
   290 {
   291     return main_window_get_renderarea(main_win);
   292 }
   294 /**
   295  * Hook called when DC starts running. Just disables the run/step buttons
   296  * and enables the stop button.
   297  */
   298 void gtk_gui_start( void )
   299 {
   300     main_window_set_running( main_win, TRUE );
   301     if( debug_win != NULL ) {
   302 	debug_window_set_running( debug_win, TRUE );
   303     }
   304     gtk_gui_nanos = 0;
   305     gettimeofday(&gtk_gui_lasttv,NULL);
   306 }
   308 /**
   309  * Hook called when DC stops running. Enables the run/step buttons
   310  * and disables the stop button.
   311  */
   312 void gtk_gui_stop( void )
   313 {
   314     main_window_set_running( main_win, FALSE );
   315     gtk_gui_update();
   316 }
   318 void gtk_gui_update( void )
   319 {
   320     if( debug_win ) {
   321 	debug_window_set_running( debug_win, FALSE );
   322 	debug_window_update(debug_win);
   323     }
   324     if( mmio_win ) {
   325 	mmio_window_update(mmio_win);
   326     }
   327     dump_window_update_all();
   328 }
   330 /**
   331  * Module run-slice. Because UI processing is fairly expensive, only 
   332  * run the processing about 10 times a second while we're emulating.
   333  */
   334 uint32_t gtk_gui_run_slice( uint32_t nanosecs ) 
   335 {
   336     gtk_gui_nanos += nanosecs;
   337     if( gtk_gui_nanos > 100000000 ) { 
   338 	struct timeval tv;
   339 	while( gtk_events_pending() )
   340 	    gtk_main_iteration();
   342 	gettimeofday(&tv,NULL);
   343 	double ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000.0) +
   344 	    ((tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000.0);
   345 	double speed = (float)( (double)gtk_gui_nanos * 100.0 / ns );
   346 	gtk_gui_lasttv.tv_sec = tv.tv_sec;
   347 	gtk_gui_lasttv.tv_usec = tv.tv_usec;
   348 	main_window_set_speed( main_win, speed );
   349 	gtk_gui_nanos = 0;
   350     }
   351     return nanosecs;
   352 }
   355 PangoFontDescription *gui_fixed_font;
   356 GdkColor gui_colour_normal, gui_colour_changed, gui_colour_error;
   357 GdkColor gui_colour_warn, gui_colour_pc, gui_colour_debug;
   358 GdkColor gui_colour_trace, gui_colour_break, gui_colour_temp_break;
   359 GdkColor gui_colour_white;
   361 void gtk_gui_alloc_resources() {
   362     GdkColormap *map;
   364     gui_colour_normal.red = gui_colour_normal.green = gui_colour_normal.blue = 0;
   365     gui_colour_changed.red = gui_colour_changed.green = 64*256;
   366     gui_colour_changed.blue = 154*256;
   367     gui_colour_error.red = 65535;
   368     gui_colour_error.green = gui_colour_error.blue = 64*256;
   369     gui_colour_pc.red = 32*256;
   370     gui_colour_pc.green = 170*256;
   371     gui_colour_pc.blue = 52*256;
   372     gui_colour_warn = gui_colour_changed;
   373     gui_colour_trace.red = 156*256;
   374     gui_colour_trace.green = 78*256;
   375     gui_colour_trace.blue = 201*256;
   376     gui_colour_debug = gui_colour_pc;
   377     gui_colour_break.red = 65535;
   378     gui_colour_break.green = gui_colour_break.blue = 192*256;
   379     gui_colour_temp_break.red = gui_colour_temp_break.green = 128*256;
   380     gui_colour_temp_break.blue = 32*256;
   381     gui_colour_white.red = gui_colour_white.green = gui_colour_white.blue = 65535;
   383     map = gdk_colormap_new(gdk_visual_get_best(), TRUE);
   384     gdk_colormap_alloc_color(map, &gui_colour_normal, TRUE, TRUE);
   385     gdk_colormap_alloc_color(map, &gui_colour_changed, TRUE, TRUE);
   386     gdk_colormap_alloc_color(map, &gui_colour_error, TRUE, TRUE);
   387     gdk_colormap_alloc_color(map, &gui_colour_warn, TRUE, TRUE);
   388     gdk_colormap_alloc_color(map, &gui_colour_pc, TRUE, TRUE);
   389     gdk_colormap_alloc_color(map, &gui_colour_debug, TRUE, TRUE);
   390     gdk_colormap_alloc_color(map, &gui_colour_trace, TRUE, TRUE);
   391     gdk_colormap_alloc_color(map, &gui_colour_break, TRUE, TRUE);
   392     gdk_colormap_alloc_color(map, &gui_colour_temp_break, TRUE, TRUE);
   393     gdk_colormap_alloc_color(map, &gui_colour_white, TRUE, TRUE);
   394     gui_fixed_font = pango_font_description_from_string("Courier 10");
   395 }
   397 gint gtk_gui_run_property_dialog( const gchar *title, GtkWidget *panel, gtk_dialog_done_fn fn )
   398 {
   399     GtkWidget *dialog =
   400 	gtk_dialog_new_with_buttons(title, main_window_get_frame(main_win), 
   401 				    GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
   402 				    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
   403 				    GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
   404 				    NULL);
   405     gint result;
   406     gtk_widget_show_all(panel);
   407     gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
   408     result = gtk_dialog_run( GTK_DIALOG(dialog) );
   409     if( fn != NULL ) {
   410 	fn(panel, result == GTK_RESPONSE_ACCEPT);
   411     }
   412     gtk_widget_destroy( dialog );
   413     return result;
   414 }
   416 void gtk_gui_enable_action( const gchar *action, gboolean enable )
   417 {
   418     gtk_action_set_sensitive( gtk_action_group_get_action( global_action_group, action), enable);
   419 }
.