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