filename | src/gtkui/gtkui.c |
changeset | 889:5baaea6d9722 |
prev | 853:9a0aa8217a31 |
next | 1015:ad448bedc48a |
author | nkeynes |
date | Fri Dec 26 14:25:23 2008 +0000 (15 years ago) |
branch | lxdream-mem |
permissions | -rw-r--r-- |
last change | Change RAM regions to use static arrays rather than mmap regions, for a 2-3% performance gain. General mem cleanups, including some save state fixes that break states again. |
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 <time.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/time.h>
23 #include <gtk/gtkversion.h>
24 #include "lxdream.h"
25 #include "dreamcast.h"
26 #include "dream.h"
27 #include "display.h"
28 #include "gdrom/gdrom.h"
29 #include "gtkui/gtkui.h"
31 void gtk_gui_start( void );
32 void gtk_gui_stop( void );
33 void gtk_gui_alloc_resources ( void );
34 uint32_t gtk_gui_run_slice( uint32_t nanosecs );
36 struct dreamcast_module gtk_gui_module = { "gui", NULL,
37 gtk_gui_update,
38 gtk_gui_start,
39 gtk_gui_run_slice,
40 gtk_gui_stop,
41 NULL, NULL };
43 /**
44 * Single-instance windows (at most one)
45 */
46 static main_window_t main_win = NULL;
47 static debug_window_t debug_win = NULL;
48 static mmio_window_t mmio_win = NULL;
50 /**
51 * UIManager and action helpers
52 */
53 static GtkUIManager *global_ui_manager;
54 static GtkActionGroup *global_action_group;
56 /**
57 * Count of running nanoseconds - used to cut back on the GUI runtime
58 */
59 static uint32_t gtk_gui_nanos = 0;
60 static uint32_t gtk_gui_ticks = 0;
61 static struct timeval gtk_gui_lasttv;
63 static gboolean gtk_gui_init_ok = FALSE;
65 #define ENABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,TRUE)
66 #define DISABLE_ACTION(win,name) SET_ACTION_ENABLED(win,name,FALSE)
68 // UI Actions
69 static const GtkActionEntry ui_actions[] = {
70 { "FileMenu", NULL, N_("_File") },
71 { "SettingsMenu", NULL, N_("_Settings") },
72 { "HelpMenu", NULL, N_("_Help") },
73 { "LoadBinary", NULL, N_("Load _Binary..."), NULL, N_("Load and run a program binary"), G_CALLBACK(load_binary_action_callback) },
74 { "Reset", GTK_STOCK_REFRESH, N_("_Reset"), "<control>R", N_("Reset dreamcast"), G_CALLBACK(reset_action_callback) },
75 { "Pause", GTK_STOCK_MEDIA_PAUSE, N_("_Pause"), NULL, N_("Pause dreamcast"), G_CALLBACK(pause_action_callback) },
76 { "Run", GTK_STOCK_MEDIA_PLAY, N_("Resume"), NULL, N_("Resume"), G_CALLBACK(resume_action_callback) },
77 { "LoadState", GTK_STOCK_REVERT_TO_SAVED, N_("_Load State..."), "F4", N_("Load an lxdream save state"), G_CALLBACK(load_state_action_callback) },
78 { "SaveState", GTK_STOCK_SAVE_AS, N_("_Save State..."), "F3", N_("Create an lxdream save state"), G_CALLBACK(save_state_action_callback) },
79 { "Exit", GTK_STOCK_QUIT, N_("E_xit"), NULL, N_("Exit lxdream"), G_CALLBACK(exit_action_callback) },
80 { "GdromSettings", NULL, N_("_GD-Rom") },
81 { "GdromUnmount", NULL, N_("_Empty") },
82 { "GdromMount", GTK_STOCK_CDROM, N_("_Open Image..."), "<control>O", N_("Mount a cdrom disc"), G_CALLBACK(mount_action_callback) },
83 { "PathSettings", NULL, N_("_Paths..."), NULL, N_("Configure files and paths"), G_CALLBACK(path_settings_callback) },
84 { "AudioSettings", NULL, N_("_Audio..."), NULL, N_("Configure audio output"), G_CALLBACK(audio_settings_callback) },
85 { "ControllerSettings", NULL, N_("_Controllers..."), NULL, N_("Configure controllers"), G_CALLBACK(maple_settings_callback) },
86 { "NetworkSettings", NULL, N_("_Network..."), NULL, N_("Configure network settings"), G_CALLBACK(network_settings_callback) },
87 { "VideoSettings", NULL, N_("_Video..."), NULL,N_( "Configure video output"), G_CALLBACK(video_settings_callback) },
88 { "About", GTK_STOCK_ABOUT, N_("_About..."), NULL, N_("About lxdream"), G_CALLBACK(about_action_callback) },
89 { "DebugMenu", NULL, N_("_Debug") },
90 { "Debugger", NULL, N_("_Debugger"), NULL, N_("Open debugger window"), G_CALLBACK(debugger_action_callback) },
91 { "DebugMem", NULL, N_("View _Memory"), NULL, N_("View memory dump"), G_CALLBACK(debug_memory_action_callback) },
92 { "DebugMmio", NULL, N_("View IO _Registers"), NULL, N_("View MMIO Registers"), G_CALLBACK(debug_mmio_action_callback) },
93 { "SaveScene", NULL, N_("_Save Scene"), NULL, N_("Save next rendered scene"), G_CALLBACK(save_scene_action_callback) },
94 { "SingleStep", GTK_STOCK_REDO, N_("_Single Step"), NULL, N_("Single step"), G_CALLBACK(debug_step_action_callback) },
95 { "RunTo", GTK_STOCK_GOTO_LAST, N_("Run _To"), NULL, N_("Run to"), G_CALLBACK( debug_runto_action_callback) },
96 { "SetBreakpoint", GTK_STOCK_CLOSE, N_("_Breakpoint"), NULL, N_("Toggle breakpoint"), G_CALLBACK( debug_breakpoint_action_callback) }
97 };
98 static const GtkToggleActionEntry ui_toggle_actions[] = {
99 { "FullScreen", NULL, "_Full Screen", "<alt>Return", "Toggle full screen video", G_CALLBACK(fullscreen_toggle_callback), 0 },
100 };
102 // Menus and toolbars
103 static const char *ui_description =
104 "<ui>"
105 " <menubar name='MainMenu'>"
106 " <menu action='FileMenu'>"
107 " <menuitem action='LoadBinary'/>"
108 " <menuitem action='GdromSettings'/>"
109 " <separator/>"
110 " <menuitem action='Reset'/>"
111 " <menuitem action='Pause'/>"
112 " <menuitem action='Run'/>"
113 " <menuitem action='Debugger'/>"
114 " <separator/>"
115 " <menuitem action='LoadState'/>"
116 " <menuitem action='SaveState'/>"
117 " <separator/>"
118 " <menuitem action='Exit'/>"
119 " </menu>"
120 " <menu action='SettingsMenu'>"
121 " <menuitem action='PathSettings'/>"
122 " <menuitem action='AudioSettings'/>"
123 " <menuitem action='ControllerSettings'/>"
124 " <menuitem action='NetworkSettings'/>"
125 " <menuitem action='VideoSettings'/>"
126 " <separator/>"
127 " <menuitem action='FullScreen'/>"
128 " </menu>"
129 " <menu action='HelpMenu'>"
130 " <menuitem action='About'/>"
131 " </menu>"
132 " </menubar>"
133 " <toolbar name='MainToolbar'>"
134 " <toolitem action='GdromMount'/>"
135 " <toolitem action='Reset'/>"
136 " <toolitem action='Pause'/>"
137 " <toolitem action='Run'/>"
138 " <separator/>"
139 " <toolitem action='LoadState'/>"
140 " <toolitem action='SaveState'/>"
141 " </toolbar>"
142 " <menubar name='DebugMenu'>"
143 " <menu action='FileMenu'>"
144 " <menuitem action='GdromSettings'/>"
145 " <separator/>"
146 " <menuitem action='Reset'/>"
147 " <separator/>"
148 " <menuitem action='LoadState'/>"
149 " <menuitem action='SaveState'/>"
150 " <separator/>"
151 " <menuitem action='Exit'/>"
152 " </menu>"
153 " <menu action='DebugMenu'>"
154 " <menuitem action='DebugMem'/>"
155 " <menuitem action='DebugMmio'/>"
156 " <menuitem action='SaveScene'/>"
157 " <separator/>"
158 " <menuitem action='SetBreakpoint'/>"
159 " <menuitem action='Pause'/>"
160 " <menuitem action='SingleStep'/>"
161 " <menuitem action='RunTo'/>"
162 " <menuitem action='Run'/>"
163 " </menu>"
164 " <menu action='SettingsMenu'>"
165 " <menuitem action='PathSettings'/>"
166 " <menuitem action='AudioSettings'/>"
167 " <menuitem action='ControllerSettings'/>"
168 " <menuitem action='NetworkSettings'/>"
169 " <menuitem action='VideoSettings'/>"
170 " <separator/>"
171 " <menuitem action='FullScreen'/>"
172 " </menu>"
173 " <menu action='HelpMenu'>"
174 " <menuitem action='About'/>"
175 " </menu>"
176 " </menubar>"
177 " <toolbar name='DebugToolbar'>"
178 " <toolitem action='GdromMount'/>"
179 " <toolitem action='Reset'/>"
180 " <toolitem action='Pause'/>"
181 " <separator/>"
182 " <toolitem action='SingleStep'/>"
183 " <toolitem action='RunTo'/>"
184 " <toolitem action='Run'/>"
185 " <toolitem action='SetBreakpoint'/>"
186 " <separator/>"
187 " <toolitem action='LoadState'/>"
188 " <toolitem action='SaveState'/>"
189 " </toolbar>"
190 "</ui>";
192 gboolean gui_parse_cmdline( int *argc, char **argv[] )
193 {
194 gtk_gui_init_ok = gtk_init_check( argc, argv );
195 return gtk_gui_init_ok;
196 }
198 gboolean gtk_gui_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *ptr )
199 {
200 main_window_update_title( main_win );
201 return TRUE;
202 }
204 gboolean gui_init( gboolean withDebug )
205 {
206 if( gtk_gui_init_ok ) {
207 GError *error = NULL;
208 dreamcast_register_module( >k_gui_module );
209 gtk_gui_alloc_resources();
211 global_action_group = gtk_action_group_new("MenuActions");
212 gtk_action_group_set_translation_domain( global_action_group, NULL );
213 gtk_action_group_add_actions( global_action_group, ui_actions, G_N_ELEMENTS(ui_actions), NULL );
214 gtk_action_group_add_toggle_actions( global_action_group, ui_toggle_actions, G_N_ELEMENTS(ui_toggle_actions), NULL );
215 gtk_gui_enable_action("AudioSettings", FALSE);
216 gtk_gui_enable_action("NetworkSettings", FALSE);
217 gtk_gui_enable_action("VideoSettings", FALSE);
219 global_ui_manager = gtk_ui_manager_new();
220 gtk_ui_manager_set_add_tearoffs(global_ui_manager, TRUE);
221 gtk_ui_manager_insert_action_group( global_ui_manager, global_action_group, 0 );
223 if (!gtk_ui_manager_add_ui_from_string (global_ui_manager, ui_description, -1, &error)) {
224 g_message ("building menus failed: %s", error->message);
225 g_error_free (error);
226 exit(1);
227 }
228 GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
229 GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu");
230 GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/MainToolbar");
232 GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/MainMenu/FileMenu/GdromSettings");
233 GtkWidget *gdrommenu = gdrom_menu_new();
234 gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
235 main_win = main_window_new( lxdream_package_name, menubar, toolbar, accel_group );
236 if( withDebug ) {
237 gtk_gui_show_debugger();
238 }
239 register_gdrom_disc_change_hook( gtk_gui_disc_changed, NULL );
241 return TRUE;
242 } else {
243 return FALSE;
244 }
245 }
247 void gui_main_loop( gboolean run )
248 {
249 gtk_gui_update();
250 if( run ) {
251 dreamcast_run();
252 gtk_main();
253 } else {
254 gtk_main();
255 }
256 }
258 void gui_update_state(void)
259 {
260 gtk_gui_update();
261 }
263 void gui_set_use_grab( gboolean flag )
264 {
265 if( main_win != NULL ) {
266 main_window_set_use_grab(main_win, flag);
267 }
268 }
270 gboolean gui_error_dialog( const char *msg, ... )
271 {
272 if( main_win != NULL ) {
273 va_list args;
274 GtkWidget *dialog =
275 gtk_message_dialog_new( main_window_get_frame(main_win), GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
276 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, NULL );
277 va_start(args, msg);
278 gchar *markup = g_markup_vprintf_escaped( msg, args );
279 va_end( args );
280 gtk_message_dialog_set_markup( GTK_MESSAGE_DIALOG(dialog), markup );
281 g_free(markup);
282 gtk_dialog_run(GTK_DIALOG(dialog));
283 gtk_widget_destroy(dialog);
284 return TRUE;
285 }
286 return FALSE;
287 }
289 void gui_update_io_activity( io_activity_type io, gboolean active )
290 {
292 }
294 void gtk_gui_show_debugger()
295 {
296 if( debug_win ) {
297 debug_window_show(debug_win, TRUE);
298 } else {
299 GtkAccelGroup *accel_group = gtk_ui_manager_get_accel_group (global_ui_manager);
300 GtkWidget *menubar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu");
301 GtkWidget *toolbar = gtk_ui_manager_get_widget(global_ui_manager, "/DebugToolbar");
302 GtkWidget *gdrommenuitem = gtk_ui_manager_get_widget(global_ui_manager, "/DebugMenu/FileMenu/GdromSettings");
303 GtkWidget *gdrommenu = gdrom_menu_new();
304 gtk_menu_item_set_submenu( GTK_MENU_ITEM(gdrommenuitem), gdrommenu );
305 gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("Debugger"));
306 debug_win = debug_window_new( title, menubar, toolbar, accel_group );
307 g_free(title);
308 }
309 }
311 void gtk_gui_show_mmio()
312 {
313 if( mmio_win ) {
314 mmio_window_show(mmio_win, TRUE);
315 } else {
316 gchar *title = g_strdup_printf( "%s :: %s", lxdream_package_name, _("MMIO Registers"));
317 mmio_win = mmio_window_new( title );
318 g_free(title);
319 }
320 }
323 main_window_t gtk_gui_get_main()
324 {
325 return main_win;
326 }
328 debug_window_t gtk_gui_get_debugger()
329 {
330 return debug_win;
331 }
333 mmio_window_t gtk_gui_get_mmio()
334 {
335 return mmio_win;
336 }
338 /**
339 * Hook called when DC starts running. Just disables the run/step buttons
340 * and enables the stop button.
341 */
342 void gtk_gui_start( void )
343 {
344 main_window_set_running( main_win, TRUE );
345 if( debug_win != NULL ) {
346 debug_window_set_running( debug_win, TRUE );
347 }
348 gtk_gui_nanos = 0;
349 gettimeofday(>k_gui_lasttv,NULL);
350 }
352 /**
353 * Hook called when DC stops running. Enables the run/step buttons
354 * and disables the stop button.
355 */
356 void gtk_gui_stop( void )
357 {
358 main_window_set_running( main_win, FALSE );
359 gtk_gui_update();
360 }
362 void gtk_gui_update( void )
363 {
364 if( global_action_group ) {
365 gtk_gui_enable_action("Run", !dreamcast_is_running() );
366 gtk_gui_enable_action("Pause", dreamcast_is_running() );
367 }
368 if( debug_win ) {
369 debug_window_set_running( debug_win, FALSE );
370 debug_window_update(debug_win);
371 }
372 if( mmio_win ) {
373 mmio_window_update(mmio_win);
374 }
375 dump_window_update_all();
376 }
378 /**
379 * Module run-slice. Run the event loop 100 times/second (doesn't really need to be
380 * any more often than this), and update the speed display 10 times/second.
381 *
382 * Also detect if we're running too fast here and yield for a bit
383 */
384 uint32_t gtk_gui_run_slice( uint32_t nanosecs )
385 {
386 gtk_gui_nanos += nanosecs;
387 if( gtk_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
388 gtk_gui_nanos -= GUI_TICK_PERIOD;
389 gtk_gui_ticks ++;
390 uint32_t current_period = gtk_gui_ticks * GUI_TICK_PERIOD;
392 // Run the event loop
393 while( gtk_events_pending() )
394 gtk_main_iteration();
396 struct timeval tv;
397 gettimeofday(&tv,NULL);
398 uint32_t ns = ((tv.tv_sec - gtk_gui_lasttv.tv_sec) * 1000000000) +
399 (tv.tv_usec - gtk_gui_lasttv.tv_usec)*1000;
400 if( (ns * 1.05) < current_period ) {
401 // We've gotten ahead - sleep for a little bit
402 struct timespec tv;
403 tv.tv_sec = 0;
404 tv.tv_nsec = current_period - ns;
405 nanosleep(&tv, &tv);
406 }
408 /* Update the display every 10 ticks (ie 10 times a second) and
409 * save the current tv value */
410 if( gtk_gui_ticks > 10 ) {
411 gtk_gui_ticks -= 10;
413 double speed = (float)( (double)current_period * 100.0 / ns );
414 gtk_gui_lasttv.tv_sec = tv.tv_sec;
415 gtk_gui_lasttv.tv_usec = tv.tv_usec;
416 main_window_set_speed( main_win, speed );
417 }
418 }
419 return nanosecs;
420 }
423 PangoFontDescription *gui_fixed_font;
424 GdkColor gui_colour_normal, gui_colour_changed, gui_colour_error;
425 GdkColor gui_colour_warn, gui_colour_pc, gui_colour_debug;
426 GdkColor gui_colour_trace, gui_colour_break, gui_colour_temp_break;
427 GdkColor gui_colour_white;
429 void gtk_gui_alloc_resources() {
430 GdkColormap *map;
432 gui_colour_normal.red = gui_colour_normal.green = gui_colour_normal.blue = 0;
433 gui_colour_changed.red = gui_colour_changed.green = 64*256;
434 gui_colour_changed.blue = 154*256;
435 gui_colour_error.red = 65535;
436 gui_colour_error.green = gui_colour_error.blue = 64*256;
437 gui_colour_pc.red = 32*256;
438 gui_colour_pc.green = 170*256;
439 gui_colour_pc.blue = 52*256;
440 gui_colour_warn = gui_colour_changed;
441 gui_colour_trace.red = 156*256;
442 gui_colour_trace.green = 78*256;
443 gui_colour_trace.blue = 201*256;
444 gui_colour_debug = gui_colour_pc;
445 gui_colour_break.red = 65535;
446 gui_colour_break.green = gui_colour_break.blue = 192*256;
447 gui_colour_temp_break.red = gui_colour_temp_break.green = 128*256;
448 gui_colour_temp_break.blue = 32*256;
449 gui_colour_white.red = gui_colour_white.green = gui_colour_white.blue = 65535;
451 map = gdk_colormap_new(gdk_visual_get_best(), TRUE);
452 gdk_colormap_alloc_color(map, &gui_colour_normal, TRUE, TRUE);
453 gdk_colormap_alloc_color(map, &gui_colour_changed, TRUE, TRUE);
454 gdk_colormap_alloc_color(map, &gui_colour_error, TRUE, TRUE);
455 gdk_colormap_alloc_color(map, &gui_colour_warn, TRUE, TRUE);
456 gdk_colormap_alloc_color(map, &gui_colour_pc, TRUE, TRUE);
457 gdk_colormap_alloc_color(map, &gui_colour_debug, TRUE, TRUE);
458 gdk_colormap_alloc_color(map, &gui_colour_trace, TRUE, TRUE);
459 gdk_colormap_alloc_color(map, &gui_colour_break, TRUE, TRUE);
460 gdk_colormap_alloc_color(map, &gui_colour_temp_break, TRUE, TRUE);
461 gdk_colormap_alloc_color(map, &gui_colour_white, TRUE, TRUE);
462 gui_fixed_font = pango_font_description_from_string("Courier 10");
463 }
465 gint gtk_gui_run_property_dialog( const gchar *title, GtkWidget *panel, gtk_dialog_done_fn fn )
466 {
467 GtkWidget *dialog =
468 gtk_dialog_new_with_buttons(title, main_window_get_frame(main_win),
469 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
470 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
471 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
472 NULL);
473 gint result;
474 gtk_widget_show_all(panel);
475 gtk_container_add( GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panel );
476 result = gtk_dialog_run( GTK_DIALOG(dialog) );
477 if( fn != NULL ) {
478 fn(panel, result == GTK_RESPONSE_ACCEPT);
479 }
480 gtk_widget_destroy( dialog );
481 return result;
482 }
484 void gtk_gui_enable_action( const gchar *action, gboolean enable )
485 {
486 gtk_action_set_sensitive( gtk_action_group_get_action( global_action_group, action), enable);
487 }
489 static void delete_frame_buffer( guchar *pixels, gpointer buffer )
490 {
491 if( buffer != NULL ) {
492 g_free(buffer);
493 }
494 }
496 GdkPixbuf *gdk_pixbuf_new_from_frame_buffer( frame_buffer_t buffer )
497 {
498 return gdk_pixbuf_new_from_data( (unsigned char *)buffer->data,
499 GDK_COLORSPACE_RGB,
500 (buffer->colour_format == COLFMT_BGRA8888),
501 8,
502 buffer->width,
503 buffer->height,
504 buffer->rowstride,
505 delete_frame_buffer,
506 buffer );
507 }
509 /**
510 * Extract the keyval of the key event if no modifier keys were pressed -
511 * in other words get the keyval of the key by itself. The other way around
512 * would be to use the hardware keysyms directly rather than the keyvals,
513 * but the mapping looks to be messier.
514 */
515 uint16_t gtk_get_unmodified_keyval( GdkEventKey *event )
516 {
517 GdkKeymap *keymap = gdk_keymap_get_default();
518 guint keyval;
520 gdk_keymap_translate_keyboard_state( keymap, event->hardware_keycode, 0, 0, &keyval,
521 NULL, NULL, NULL );
522 return keyval;
523 }
525 gchar *get_absolute_path( const gchar *in_path )
526 {
527 char tmp[PATH_MAX];
528 if( in_path == NULL ) {
529 return NULL;
530 }
531 if( in_path[0] == '/' || in_path[0] == 0 ) {
532 return g_strdup(in_path);
533 } else {
534 getcwd(tmp, sizeof(tmp));
535 return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
536 }
537 }
.