Search
lxdream.org :: lxdream/src/gtkui/gtkui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtkui.c
changeset 561:533f6b478071
prev546:7d01e597a066
next608:4f588e52bce0
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
view annotate diff log raw
     1 /**
     2  * $Id$
     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 "lxdream.h"
    20 #include <sys/time.h>
    21 #include <time.h>
    22 #include <glib/gi18n.h>
    23 #include "dreamcast.h"
    24 #include "display.h"
    25 #include "gdrom/gdrom.h"
    26 #include "gtkui/gtkui.h"
    29 void gtk_gui_start( void );
    30 void gtk_gui_stop( void );
    31 void gtk_gui_alloc_resources ( void );
    32 uint32_t gtk_gui_run_slice( uint32_t nanosecs );
    34 struct dreamcast_module gtk_gui_module = { "gui", NULL,
    35 					   gtk_gui_update, 
    36 					   gtk_gui_start, 
    37 					   gtk_gui_run_slice, 
    38 					   gtk_gui_stop, 
    39 					   NULL, NULL };
    41 /**
    42  * Single-instance windows (at most one)
    43  */
    44 static main_window_t main_win = NULL;
    45 static debug_window_t debug_win = NULL;
    46 static mmio_window_t mmio_win = NULL;
    48 /**
    49  * UIManager and action helpers
    50  */
    51 static GtkUIManager *global_ui_manager;
    52 static GtkActionGroup *global_action_group;
    54 /**
    55  * Count of running nanoseconds - used to cut back on the GUI runtime
    56  */
    57 static uint32_t gtk_gui_nanos = 0;
    58 static struct timeval gtk_gui_lasttv;
    60 static gboolean gtk_gui_init_ok = FALSE;
    62 #define ENABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,TRUE)
    63 #define DISABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,FALSE)
    65 // UI Actions
    66 static const GtkActionEntry ui_actions[] = {
    67     { "FileMenu", NULL, N_("_File") },
    68     { "SettingsMenu", NULL, N_("_Settings") },
    69     { "HelpMenu", NULL, N_("_Help") },
    70     { "LoadBinary", NULL, N_("Load _Binary..."), NULL, N_("Load and run a program binary"), G_CALLBACK(load_binary_action_callback) },
    71     { "Reset", GTK_STOCK_REFRESH, N_("_Reset"), "<control>R", N_("Reset dreamcast"), G_CALLBACK(reset_action_callback) },
    72     { "Pause", GTK_STOCK_MEDIA_PAUSE, N_("_Pause"), NULL, N_("Pause dreamcast"), G_CALLBACK(pause_action_callback) },
    73     { "Run", GTK_STOCK_MEDIA_PLAY, N_("Resume"), NULL, N_("Resume"), G_CALLBACK(resume_action_callback) },
    74     { "LoadState", GTK_STOCK_REVERT_TO_SAVED, N_("_Load state..."), "F4", N_("Load an lxdream save state"), G_CALLBACK(load_state_action_callback) },
    75     { "SaveState", GTK_STOCK_SAVE_AS, N_("_Save state..."), "F3", N_("Create an lxdream save state"), G_CALLBACK(save_state_action_callback) },
    76     { "Exit", GTK_STOCK_QUIT, N_("E_xit"), NULL, N_("Exit lxdream"), G_CALLBACK(exit_action_callback) },
    77     { "GdromSettings", NULL, N_("_GD-Rom...") },
    78     { "GdromUnmount", NULL, N_("_Empty") },
    79     { "GdromMount", GTK_STOCK_CDROM, N_("_Open Image..."), "<control>O", N_("Mount a cdrom disc"), G_CALLBACK(mount_action_callback) },
    80     { "PathSettings", NULL, N_("_Paths..."), NULL, N_("Configure files and paths"), G_CALLBACK(path_settings_callback) }, 
    81     { "AudioSettings", NULL, N_("_Audio..."), NULL, N_("Configure audio output"), G_CALLBACK(audio_settings_callback) },
    82     { "ControllerSettings", NULL, N_("_Controllers..."), NULL, N_("Configure controllers"), G_CALLBACK(maple_settings_callback) },
    83     { "NetworkSettings", NULL, N_("_Network..."), NULL, N_("Configure network settings"), G_CALLBACK(network_settings_callback) },
    84     { "VideoSettings", NULL, N_("_Video..."), NULL,N_( "Configure video output"), G_CALLBACK(video_settings_callback) },
    85     { "About", GTK_STOCK_ABOUT, N_("_About..."), NULL, N_("About lxdream"), G_CALLBACK(about_action_callback) },
    86     { "DebugMenu", NULL, N_("_Debug") },
    87     { "Debugger", NULL, N_("_Debugger"), NULL, N_("Open debugger window"), G_CALLBACK(debugger_action_callback) },
    88     { "DebugMem", NULL, N_("View _Memory"), NULL, N_("View memory dump"), G_CALLBACK(debug_memory_action_callback) },
    89     { "DebugMmio", NULL, N_("View IO _Registers"), NULL, N_("View MMIO Registers"), G_CALLBACK(debug_mmio_action_callback) },
    90     { "SaveScene", NULL, N_("_Save Scene"), NULL, N_("Save next rendered scene"), G_CALLBACK(save_scene_action_callback) },
    91     { "SingleStep", GTK_STOCK_REDO, N_("_Single Step"), NULL, N_("Single step"), G_CALLBACK(debug_step_action_callback) },
    92     { "RunTo", GTK_STOCK_GOTO_LAST, N_("Run _To"), NULL, N_("Run to"), G_CALLBACK( debug_runto_action_callback) },
    93     { "SetBreakpoint", GTK_STOCK_CLOSE, N_("_Breakpoint"), NULL, N_("Toggle breakpoint"), G_CALLBACK( debug_breakpoint_action_callback) }
    94 };
    95 static const GtkToggleActionEntry ui_toggle_actions[] = {
    96     { "FullScreen", NULL, "_Full Screen", "<alt>Return", "Toggle full screen video", G_CALLBACK(fullscreen_toggle_callback), 0 },
    97 };
    99 // Menus and toolbars
   100 static const char *ui_description =
   101     "<ui>"
   102     " <menubar name='MainMenu'>"
   103     "  <menu action='FileMenu'>"
   104     "   <menuitem action='LoadBinary'/>"
   105     "   <menuitem action='GdromSettings'/>"
   106     "   <separator/>"
   107     "   <menuitem action='Reset'/>"
   108     "   <menuitem action='Pause'/>"
   109     "   <menuitem action='Run'/>"
   110     "   <menuitem action='Debugger'/>"
   111     "   <separator/>"
   112     "   <menuitem action='LoadState'/>"
   113     "   <menuitem action='SaveState'/>"
   114     "   <separator/>"
   115     "   <menuitem action='Exit'/>"
   116     "  </menu>"
   117     "  <menu action='SettingsMenu'>"
   118     "   <menuitem action='PathSettings'/>"
   119     "   <menuitem action='AudioSettings'/>"
   120     "   <menuitem action='ControllerSettings'/>"
   121     "   <menuitem action='NetworkSettings'/>"
   122     "   <menuitem action='VideoSettings'/>"
   123     "   <separator/>"
   124     "   <menuitem action='FullScreen'/>"
   125     "  </menu>"
   126     "  <menu action='HelpMenu'>"
   127     "   <menuitem action='About'/>"
   128     "  </menu>"
   129     " </menubar>"
   130     " <toolbar name='MainToolbar'>"
   131     "  <toolitem action='GdromMount'/>"
   132     "  <toolitem action='Reset'/>"
   133     "  <toolitem action='Pause'/>"
   134     "  <toolitem action='Run'/>"
   135     "  <separator/>"
   136     "  <toolitem action='LoadState'/>"
   137     "  <toolitem action='SaveState'/>"
   138     " </toolbar>"
   139     " <menubar name='DebugMenu'>"
   140     "  <menu action='FileMenu'>"
   141     "   <menuitem action='GdromSettings'/>"
   142     "   <separator/>"
   143     "   <menuitem action='Reset'/>"
   144     "   <separator/>"
   145     "   <menuitem action='LoadState'/>"
   146     "   <menuitem action='SaveState'/>"
   147     "   <separator/>"
   148     "   <menuitem action='Exit'/>"
   149     "  </menu>"
   150     "  <menu action='DebugMenu'>"
   151     "   <menuitem action='DebugMem'/>"
   152     "   <menuitem action='DebugMmio'/>"
   153     "   <menuitem action='SaveScene'/>"
   154     "   <separator/>"
   155     "   <menuitem action='SetBreakpoint'/>"
   156     "   <menuitem action='Pause'/>"
   157     "   <menuitem action='SingleStep'/>"
   158     "   <menuitem action='RunTo'/>"
   159     "   <menuitem action='Run'/>"
   160     "  </menu>"
   161     "  <menu action='SettingsMenu'>"
   162     "   <menuitem action='PathSettings'/>"
   163     "   <menuitem action='AudioSettings'/>"
   164     "   <menuitem action='ControllerSettings'/>"
   165     "   <menuitem action='NetworkSettings'/>"
   166     "   <menuitem action='VideoSettings'/>"
   167     "   <separator/>"
   168     "   <menuitem action='FullScreen'/>"
   169     "  </menu>"
   170     "  <menu action='HelpMenu'>"
   171     "   <menuitem action='About'/>"
   172     "  </menu>"
   173     " </menubar>"
   174     " <toolbar name='DebugToolbar'>"
   175     "  <toolitem action='GdromMount'/>"
   176     "  <toolitem action='Reset'/>"
   177     "  <toolitem action='Pause'/>"
   178     "  <separator/>"
   179     "  <toolitem action='SingleStep'/>"
   180     "  <toolitem action='RunTo'/>"
   181     "  <toolitem action='Run'/>"
   182     "  <toolitem action='SetBreakpoint'/>"
   183     "  <separator/>"
   184     "  <toolitem action='LoadState'/>"
   185     "  <toolitem action='SaveState'/>"
   186     " </toolbar>"
   187     "</ui>";
   189 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   190 {
   191     gtk_gui_init_ok = gtk_init_check( argc, argv );
   192     return gtk_gui_init_ok;
   193 }
   195 gboolean gui_init( gboolean withDebug )
   196 {
   197     if( gtk_gui_init_ok ) {
   198 	GError *error = NULL;
   199 	dreamcast_register_module( &gtk_gui_module );
   200 	gtk_gui_alloc_resources();
   202 	global_action_group = gtk_action_group_new("MenuActions");
   203 	gtk_action_group_set_translation_domain( global_action_group, NULL );
   204 	gtk_action_group_add_actions( global_action_group, ui_actions, G_N_ELEMENTS(ui_actions), NULL );
   205 	gtk_action_group_add_toggle_actions( global_action_group, ui_toggle_actions, G_N_ELEMENTS(ui_toggle_actions), NULL );
   206 	gtk_gui_enable_action("AudioSettings", FALSE);
   207 	gtk_gui_enable_action("NetworkSettings", FALSE);
   208 	gtk_gui_enable_action("VideoSettings", FALSE);
   210 	global_ui_manager = gtk_ui_manager_new();
   211 	gtk_ui_manager_set_add_tearoffs(global_ui_manager, TRUE);
   212 	gtk_ui_manager_insert_action_group( global_ui_manager, global_action_group, 0 );
   214 	if (!gtk_ui_manager_add_ui_from_string (global_ui_manager, ui_description, -1, &error)) {
   215 	    g_message ("building menus failed: %s", error->message);
   216 	    g_error_free (error);
   217 	    exit(1);
   218 	}
   219 	GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
   220 	GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu");
   221 	GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/MainToolbar");
   223 	GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu/FileMenu/GdromSettings");
   224 	gdrom_menu_init();
   225 	GtkWidget *gdrommenu = gdrom_menu_new();
   226 	gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
   227 	main_win = main_window_new( APP_NAME " " APP_VERSION, menubar, toolbar, accel_group  );
   228 	if( withDebug ) {
   229 	    gtk_gui_show_debugger();
   230 	}
   232 	return TRUE;
   233     } else {
   234 	return FALSE;
   235     }
   236 }
   238 void gui_main_loop(void)
   239 {
   240     gtk_gui_update();
   241     gtk_main();
   242 }
   244 void gui_update_state(void)
   245 {
   246     gtk_gui_update();
   247 }
   249 gboolean gui_error_dialog( const char *msg, ... )
   250 {
   251     if( main_win != NULL ) {
   252 	va_list args;
   253 	GtkWidget *dialog = 
   254 	    gtk_message_dialog_new( main_window_get_frame(main_win), GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
   255 				    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL );
   256 	va_start(args, msg);
   257 	gchar *markup = g_markup_vprintf_escaped( msg, args );
   258 	va_end( args );
   259 	gtk_message_dialog_set_markup( GTK_MESSAGE_DIALOG(dialog), markup );
   260 	g_free(markup);
   261 	gtk_dialog_run(GTK_DIALOG(dialog));
   262 	gtk_widget_destroy(dialog);
   263 	return TRUE;
   264     }
   265     return FALSE;
   266 }
   268 void gui_update_io_activity( io_activity_type io, gboolean active )
   269 {
   271 }
   273 void gtk_gui_show_debugger()
   274 {
   275     if( debug_win ) {
   276 	debug_window_show(debug_win, TRUE);
   277     } else {
   278 	GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
   279 	GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu");
   280 	GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugToolbar");
   281 	GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu/FileMenu/GdromSettings");
   282 	GtkWidget *gdrommenu = gdrom_menu_new();
   283 	gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
   284 	gchar *title = g_strdup_printf( APP_NAME " " APP_VERSION " :: %s", _("Debugger"));
   285 	debug_win = debug_window_new( title, menubar, toolbar, accel_group  );
   286 	g_free(title);
   287     }
   288 }
   290 void gtk_gui_show_mmio()
   291 {
   292     if( mmio_win ) {
   293 	mmio_window_show(mmio_win, TRUE);
   294     } else {
   295 	gchar *title = g_strdup_printf( APP_NAME " " APP_VERSION " :: %s", _("MMIO Registers"));
   296 	mmio_win = mmio_window_new( title );
   297 	g_free(title);
   298     }
   299 }
   302 main_window_t gtk_gui_get_main()
   303 {
   304     return main_win;
   305 }
   307 debug_window_t gtk_gui_get_debugger()
   308 {
   309     return debug_win;
   310 }
   312 mmio_window_t gtk_gui_get_mmio()
   313 {
   314     return mmio_win;
   315 }
   317 GtkWidget *gtk_gui_get_renderarea()
   318 {
   319     if( main_win == NULL ) {
   320 	return NULL;
   321     } else {
   322 	return main_window_get_renderarea(main_win);
   323     }
   324 }
   326 /**
   327  * Hook called when DC starts running. Just disables the run/step buttons
   328  * and enables the stop button.
   329  */
   330 void gtk_gui_start( void )
   331 {
   332     main_window_set_running( main_win, TRUE );
   333     if( debug_win != NULL ) {
   334 	debug_window_set_running( debug_win, TRUE );
   335     }
   336     gtk_gui_nanos = 0;
   337     gettimeofday(&gtk_gui_lasttv,NULL);
   338 }
   340 /**
   341  * Hook called when DC stops running. Enables the run/step buttons
   342  * and disables the stop button.
   343  */
   344 void gtk_gui_stop( void )
   345 {
   346     main_window_set_running( main_win, FALSE );
   347     gtk_gui_update();
   348 }
   350 void gtk_gui_update( void )
   351 {
   352     if( global_action_group ) {
   353 	gtk_gui_enable_action("Run", dreamcast_can_run() && !dreamcast_is_running() );
   354 	gtk_gui_enable_action("Pause", dreamcast_is_running() );
   355     }
   356     if( debug_win ) {
   357 	debug_window_set_running( debug_win, FALSE );
   358 	debug_window_update(debug_win);
   359     }
   360     if( mmio_win ) {
   361 	mmio_window_update(mmio_win);
   362     }
   363     dump_window_update_all();
   364 }
   366 /**
   367  * Module run-slice. Because UI processing is fairly expensive, only 
   368  * run the processing about 10 times a second while we're emulating.
   369  */
   370 uint32_t gtk_gui_run_slice( uint32_t nanosecs ) 
   371 {
   372     gtk_gui_nanos += nanosecs;
   373     if( gtk_gui_nanos > 100000000 ) { 
   374 	struct timeval tv;
   375 	while( gtk_events_pending() )
   376 	    gtk_main_iteration();
   378 	gettimeofday(&tv,NULL);
   379 	double ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000.0) +
   380 	    ((tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000.0);
   381 	double speed = (float)( (double)gtk_gui_nanos * 100.0 / ns );
   382 	gtk_gui_lasttv.tv_sec = tv.tv_sec;
   383 	gtk_gui_lasttv.tv_usec = tv.tv_usec;
   384 	main_window_set_speed( main_win, speed );
   385 	gtk_gui_nanos = 0;
   386     }
   387     return nanosecs;
   388 }
   391 PangoFontDescription *gui_fixed_font;
   392 GdkColor gui_colour_normal, gui_colour_changed, gui_colour_error;
   393 GdkColor gui_colour_warn, gui_colour_pc, gui_colour_debug;
   394 GdkColor gui_colour_trace, gui_colour_break, gui_colour_temp_break;
   395 GdkColor gui_colour_white;
   397 void gtk_gui_alloc_resources() {
   398     GdkColormap *map;
   400     gui_colour_normal.red = gui_colour_normal.green = gui_colour_normal.blue = 0;
   401     gui_colour_changed.red = gui_colour_changed.green = 64*256;
   402     gui_colour_changed.blue = 154*256;
   403     gui_colour_error.red = 65535;
   404     gui_colour_error.green = gui_colour_error.blue = 64*256;
   405     gui_colour_pc.red = 32*256;
   406     gui_colour_pc.green = 170*256;
   407     gui_colour_pc.blue = 52*256;
   408     gui_colour_warn = gui_colour_changed;
   409     gui_colour_trace.red = 156*256;
   410     gui_colour_trace.green = 78*256;
   411     gui_colour_trace.blue = 201*256;
   412     gui_colour_debug = gui_colour_pc;
   413     gui_colour_break.red = 65535;
   414     gui_colour_break.green = gui_colour_break.blue = 192*256;
   415     gui_colour_temp_break.red = gui_colour_temp_break.green = 128*256;
   416     gui_colour_temp_break.blue = 32*256;
   417     gui_colour_white.red = gui_colour_white.green = gui_colour_white.blue = 65535;
   419     map = gdk_colormap_new(gdk_visual_get_best(), TRUE);
   420     gdk_colormap_alloc_color(map, &gui_colour_normal, TRUE, TRUE);
   421     gdk_colormap_alloc_color(map, &gui_colour_changed, TRUE, TRUE);
   422     gdk_colormap_alloc_color(map, &gui_colour_error, TRUE, TRUE);
   423     gdk_colormap_alloc_color(map, &gui_colour_warn, TRUE, TRUE);
   424     gdk_colormap_alloc_color(map, &gui_colour_pc, TRUE, TRUE);
   425     gdk_colormap_alloc_color(map, &gui_colour_debug, TRUE, TRUE);
   426     gdk_colormap_alloc_color(map, &gui_colour_trace, TRUE, TRUE);
   427     gdk_colormap_alloc_color(map, &gui_colour_break, TRUE, TRUE);
   428     gdk_colormap_alloc_color(map, &gui_colour_temp_break, TRUE, TRUE);
   429     gdk_colormap_alloc_color(map, &gui_colour_white, TRUE, TRUE);
   430     gui_fixed_font = pango_font_description_from_string("Courier 10");
   431 }
   433 gint gtk_gui_run_property_dialog( const gchar *title, GtkWidget *panel, gtk_dialog_done_fn fn )
   434 {
   435     GtkWidget *dialog =
   436 	gtk_dialog_new_with_buttons(title, main_window_get_frame(main_win), 
   437 				    GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
   438 				    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
   439 				    GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
   440 				    NULL);
   441     gint result;
   442     gtk_widget_show_all(panel);
   443     gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
   444     result = gtk_dialog_run( GTK_DIALOG(dialog) );
   445     if( fn != NULL ) {
   446 	fn(panel, result == GTK_RESPONSE_ACCEPT);
   447     }
   448     gtk_widget_destroy( dialog );
   449     return result;
   450 }
   452 void gtk_gui_enable_action( const gchar *action, gboolean enable )
   453 {
   454     gtk_action_set_sensitive( gtk_action_group_get_action( global_action_group, action), enable);
   455 }
   457 static void delete_frame_buffer( guchar *pixels, gpointer buffer )
   458 {
   459     if( buffer != NULL ) {
   460 	g_free(buffer);
   461     }
   462 }
   464 GdkPixbuf *gdk_pixbuf_new_from_frame_buffer( frame_buffer_t buffer )
   465 {
   466     return gdk_pixbuf_new_from_data( (unsigned char *)buffer->data, 
   467 				     GDK_COLORSPACE_RGB,
   468 				     (buffer->colour_format == COLFMT_BGRA8888),
   469 				     8,
   470 				     buffer->width,
   471 				     buffer->height,
   472 				     buffer->rowstride,
   473 				     delete_frame_buffer,
   474 				     buffer );
   475 }
.