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