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