From 8f9d24c1e8cf4ca7488f6ef7fd03072836262e45 Mon Sep 17 00:00:00 2001 From: Maximilian Mader Date: Fri, 10 Apr 2020 04:10:28 +0200 Subject: [PATCH] [GTK3] Implement debugger console sidebar --- .gitignore | 1 + gtk3/main.c | 239 +++++++++++++++++++++++++++--------- gtk3/main.h | 3 + gtk3/resources/ui/window.ui | 121 +++++++----------- 4 files changed, 231 insertions(+), 133 deletions(-) diff --git a/.gitignore b/.gitignore index ab1a14e..648570f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ build # intermediate source files generated at build time gtk3/sameboy-gtk3-resources.c gtk3/resources/gtk3/ +gtk3/resources/ui/#*# # temporary backup file *.*~ diff --git a/gtk3/main.c b/gtk3/main.c index b9dd877..155212e 100644 --- a/gtk3/main.c +++ b/gtk3/main.c @@ -23,6 +23,8 @@ static SDL_GameController *controller = NULL; static SDL_AudioSpec want_aspec, have_aspec; static SDL_AudioDeviceID device_id; +static const GThread *main_thread; + static GtkApplication *main_application; static GtkBuilder *builder; static GtkGLArea *gl_area; @@ -72,12 +74,15 @@ static uint8_t oamHeight; static uint8_t pressed_buttons; +static GtkTextBuffer *pending_console_output = NULL; static gboolean in_sync_input = false; static gchar *last_console_input = NULL; +static gboolean log_to_sidebar = false; +static gboolean should_clear_sidebar = false; static GMutex debugger_input_mutex; static GCond debugger_input_cond; -static GMutex console_output_lock; +static GRecMutex console_output_lock; static GPtrArray *debugger_input_queue; // List of GActions for the `app` prefix @@ -97,9 +102,12 @@ static const GActionEntry app_entries[] = { { "change_model", NULL, "s", "@s 'CGB'", on_model_changed }, { "toggle_mute", NULL, NULL, "false", on_mute_changed }, { "pause", NULL, NULL, "false", on_pause_changed }, + { "clear_console", activate_clear_console, NULL, NULL, NULL }, }; int main(int argc, char *argv[]) { + main_thread = g_thread_self(); + // initialize GB_model_t to invalid value gui_data.cli_options.model = -1; gui_data.prev_model = -1; @@ -245,7 +253,7 @@ void gl_check_realize(GtkWidget *w, gpointer user_data_gptr) { else { gdk_gl_context_make_current(context); int version = epoxy_gl_version(); - + g_object_run_dispose(G_OBJECT(context)); g_object_unref(context); context = NULL; @@ -278,7 +286,7 @@ static gboolean init_controllers() { if (val < 0) { g_warning("Failed to load controller mappings: %s", SDL_GetError()); } - + g_bytes_unref(db_f); } @@ -363,7 +371,7 @@ static GB_model_t get_model() { else if (g_strcmp0(family, "SGB") == 0) { return get_sgb_model(); } - + return get_cgb_model(); } @@ -386,7 +394,13 @@ static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample) { SDL_QueueAudio(device_id, sample, sizeof(*sample)); } +// Console TODO: +// TODO: clear sidebar when switching to async mode +// TODO: Command history (up / down arrow in input) +// TODO: reverse search of commands +// TODO: search in output static char *sync_console_input(GB_gameboy_t *gb) { + update_debugger_sidebar(gb); console_log(gb, "> ", 0); in_sync_input = true; @@ -403,11 +417,6 @@ static char *sync_console_input(GB_gameboy_t *gb) { in_sync_input = false; - // clear sidebar - GtkTextView *sidebar_output = builder_get(GTK_TEXT_VIEW, "console_sidebar_output"); - GtkTextBuffer *sidebar_text_buf = gtk_text_view_get_buffer(sidebar_output); - gtk_text_buffer_set_text(sidebar_text_buf, "", -1); - return input; } @@ -429,50 +438,140 @@ static char *async_console_input(GB_gameboy_t *gb) { return input; } -static void on_console_log(gpointer user_data_gptr) { - LogData *log_data = (LogData *)user_data_gptr; - GB_gameboy_t *gb = log_data->gb; - GB_log_attributes attributes = log_data->attributes; +static void update_debugger_sidebar(GB_gameboy_t *gb) { + if (!GB_debugger_is_stopped(gb)) { + return; + } - g_mutex_lock(&console_output_lock); - - GtkTextView *text_view = builder_get(GTK_TEXT_VIEW, "console_screen"); - GtkTextBuffer *text_buf = gtk_text_view_get_buffer(text_view); + if (main_thread != g_thread_self()) { + g_idle_add((GSourceFunc) update_debugger_sidebar, gb); + return; + } + + g_rec_mutex_lock(&console_output_lock); + should_clear_sidebar = true; + append_pending_output(); + log_to_sidebar = true; + g_rec_mutex_unlock(&console_output_lock); + + GtkTextView *sidebar_input = builder_get(GTK_TEXT_VIEW, "console_sidebar_input"); + GtkTextBuffer *sidebar_input_text_buf = gtk_text_view_get_buffer(sidebar_input); + + gint line_count = gtk_text_buffer_get_line_count(sidebar_input_text_buf); + + for (unsigned line = 0; line < line_count; ++line) { + GtkTextIter start_iter; + GtkTextIter end_iter; + gunichar ch; + + gtk_text_buffer_get_iter_at_line(sidebar_input_text_buf, &start_iter, line); + end_iter = start_iter; + + do { + ch = gtk_text_iter_get_char(&end_iter); + if (!gtk_text_iter_forward_char(&end_iter)) { + break; + } + } + while (ch != '\n'); + + gchar *cmd = gtk_text_buffer_get_text(sidebar_input_text_buf, &start_iter, &end_iter, false); + g_strchug(cmd); // trim leading whitespace + g_strchomp(cmd); // trim trailing whitespace + + if (g_strcmp0("", cmd) != 0) { + char *duped = g_strdup(cmd); + GB_attributed_log(gb, GB_LOG_BOLD, "%s:\n", duped); + GB_debugger_execute_command(gb, duped); + GB_log(gb, "\n"); + g_free(duped); + } + + g_free(cmd); + } + + g_rec_mutex_lock(&console_output_lock); + append_pending_output(); + log_to_sidebar = false; + g_rec_mutex_unlock(&console_output_lock); +} + +static gboolean scroll_to_bottom(GtkTextView *textview, GtkTextMark *mark) { + GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview); GtkTextIter iter; - GtkTextIter start; - - gtk_text_buffer_get_end_iter(text_buf, &iter); - GtkTextMark *start_mark = gtk_text_buffer_create_mark(text_buf, NULL, &iter, TRUE); - gtk_text_buffer_insert(text_buf, &iter, g_strdup(log_data->string), -1); - gtk_text_buffer_get_iter_at_mark(text_buf, &start, start_mark); - if (attributes & GB_LOG_BOLD) { - gtk_text_buffer_apply_tag_by_name(text_buf, "bold", &start, &iter); + gtk_text_buffer_get_end_iter(buffer, &iter); + gtk_text_iter_set_line_offset(&iter, 0); + + gtk_text_buffer_move_mark(buffer, mark, &iter); + gtk_text_view_scroll_to_mark(textview, mark, 0.0, TRUE, 0.0, 0.10); + + gtk_text_buffer_delete_mark(buffer, mark); + + return TRUE; +} + +static void append_pending_output(void) { + g_rec_mutex_lock(&console_output_lock); + + if (should_clear_sidebar) { + GtkTextView *sidebar_output = builder_get(GTK_TEXT_VIEW, "console_sidebar_output"); + GtkTextBuffer *sidebar_output_text_buf = gtk_text_view_get_buffer(sidebar_output); + gtk_text_buffer_set_text(sidebar_output_text_buf, "", -1); + should_clear_sidebar = FALSE; } - if (attributes & GB_LOG_DASHED_UNDERLINE) { - gtk_text_buffer_apply_tag_by_name(text_buf, "dashed_underline", &start, &iter); + if (gtk_text_buffer_get_char_count(pending_console_output) > 0) { + GtkTextView *text_view = builder_get(GTK_TEXT_VIEW, log_to_sidebar? "console_sidebar_output" : "console_screen"); + GtkTextBuffer *text_buf = gtk_text_view_get_buffer(text_view); + GtkTextIter start; + GtkTextIter end; + gtk_text_buffer_get_start_iter(pending_console_output, &start); + gtk_text_buffer_get_end_iter(pending_console_output, &end); + + GtkTextIter iter; + gtk_text_buffer_get_end_iter(text_buf, &iter); + gtk_text_buffer_insert_range(text_buf, &iter, &start, &end); + + scroll_to_bottom(text_view, gtk_text_buffer_create_mark(text_buf, NULL, &iter, TRUE)); + + gtk_text_buffer_set_text(pending_console_output, "", -1); } - if (attributes & GB_LOG_UNDERLINE) { - gtk_text_buffer_apply_tag_by_name(text_buf, "underline", &start, &iter); - } - - g_free((gpointer)log_data->string); - g_free(log_data); - gtk_text_buffer_delete_mark(text_buf, start_mark); - g_mutex_unlock(&console_output_lock); + g_rec_mutex_unlock(&console_output_lock); } static void console_log(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes) { - if (string != NULL && !g_str_equal("", string)) { - LogData *log_data = g_malloc(sizeof(LogData)); - log_data->gb = gb; - log_data->string = g_strdup(string); - log_data->attributes = attributes; + g_rec_mutex_lock(&console_output_lock); - g_idle_add((GSourceFunc) on_console_log, log_data); + if (string != NULL && !g_str_equal("", string)) { + GtkTextIter iter; + GtkTextIter start; + + // Append attributed text to "pending_console_output" GtkTextBuffer + gtk_text_buffer_get_end_iter(pending_console_output, &iter); + GtkTextMark *start_mark = gtk_text_buffer_create_mark(pending_console_output, NULL, &iter, TRUE); + gtk_text_buffer_insert(pending_console_output, &iter, g_strdup(string), -1); + gtk_text_buffer_get_iter_at_mark(pending_console_output, &start, start_mark); + + if (attributes & GB_LOG_BOLD) { + gtk_text_buffer_apply_tag_by_name(pending_console_output, "bold", &start, &iter); + } + + if (attributes & GB_LOG_DASHED_UNDERLINE) { + gtk_text_buffer_apply_tag_by_name(pending_console_output, "dashed_underline", &start, &iter); + } + + if (attributes & GB_LOG_UNDERLINE) { + gtk_text_buffer_apply_tag_by_name(pending_console_output, "underline", &start, &iter); + } + + gtk_text_buffer_delete_mark(pending_console_output, start_mark); + + g_idle_add((GSourceFunc) append_pending_output, NULL); } + + g_rec_mutex_unlock(&console_output_lock); } // Returns a `GApplication`s `GMenuModel` by ID @@ -494,7 +593,7 @@ static void create_fallback_canvas(void) { } // Create our application’s menu. -// +// // This function tries to stick to the desktop environment’s conventions. // For the GNOME Shell it uses a hamburger menu, otherwise it either lets // the desktop environment shell handle the menu if it signals support for it @@ -512,7 +611,7 @@ static void setup_menu(GApplication *app) { const gchar *xdg_current_desktop = g_getenv("XDG_CURRENT_DESKTOP"); const gchar *gdm_session = g_getenv("GDMSESSION"); const gchar *desktop_session = g_getenv("DESKTOP_SESSION"); - + gchar *desktop = (gchar *)xdg_current_desktop; if (desktop == NULL || g_str_equal(desktop, "")) desktop = (gchar *)gdm_session; if (desktop == NULL || g_str_equal(desktop, "")) desktop = (gchar *)desktop_session; @@ -764,7 +863,7 @@ static void activate(GApplication *app, gpointer gui_data_gptr) { g_signal_connect(memory_viewer, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL); g_signal_connect(console, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL); g_signal_connect(printer, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL); - + g_signal_connect(get_object("vram_viewer_tileset_canvas"), "draw", G_CALLBACK(on_draw_vram_viewer_tileset), NULL); g_signal_connect(get_object("vram_viewer_tilemap_canvas"), "draw", G_CALLBACK(on_draw_vram_viewer_tilemap), NULL); @@ -798,6 +897,12 @@ static void activate(GApplication *app, gpointer gui_data_gptr) { GtkTextView *text_view = builder_get(GTK_TEXT_VIEW, "console_screen"); GtkTextBuffer *text_buf = gtk_text_view_get_buffer(text_view); + + gtk_text_view_set_buffer( + builder_get(GTK_TEXT_VIEW, "console_sidebar_output"), + gtk_text_buffer_new(gtk_text_buffer_get_tag_table(text_buf)) + ); + gtk_text_buffer_create_tag(text_buf, "bold", "weight", PANGO_WEIGHT_BOLD, NULL); gtk_text_buffer_create_tag(text_buf, "underline", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL); gtk_text_buffer_create_tag(text_buf, "dashed_underline", "underline", PANGO_UNDERLINE_DOUBLE, "underline-set", TRUE, NULL); @@ -811,12 +916,16 @@ static void activate(GApplication *app, gpointer gui_data_gptr) { g_mutex_init(&debugger_input_mutex); g_cond_init(&debugger_input_cond); - g_mutex_init(&console_output_lock); + g_rec_mutex_init(&console_output_lock); if (!debugger_input_queue) { debugger_input_queue = g_ptr_array_sized_new(4); } + if (!pending_console_output) { + pending_console_output = gtk_text_buffer_new(gtk_text_buffer_get_tag_table(text_buf)); + } + // Start the emulation thread run(gui_data); } @@ -861,7 +970,7 @@ static void open(GApplication *app, GFile **files, gint n_files, const gchar *hi // Tell our application to quit. // After this functions has been called the `shutdown` signal will be issued. -// +// // TODO: Make sure we have a way to quit our emulation loop before `shutdown` gets called static void quit(GApplication *app) { // Tell our own main loop to quit. @@ -884,10 +993,10 @@ static gboolean on_key_press(GtkWidget *w, GdkEventKey *event, gpointer data) { case GDK_KEY_a: mask = BUTTON_MASK_LEFT; break; case GDK_KEY_s: mask = BUTTON_MASK_DOWN; break; case GDK_KEY_d: mask = BUTTON_MASK_RIGHT; break; - + case GDK_KEY_g: mask = BUTTON_MASK_SELECT; break; case GDK_KEY_h: mask = BUTTON_MASK_START; break; - + case GDK_KEY_k: mask = BUTTON_MASK_B; break; case GDK_KEY_l: mask = BUTTON_MASK_A; break; @@ -923,7 +1032,7 @@ static gboolean on_key_press(GtkWidget *w, GdkEventKey *event, gpointer data) { } break; } } - + if (event->type == GDK_KEY_PRESS) { pressed_buttons |= mask; } @@ -981,7 +1090,7 @@ static void activate_open_vram_viewer(GSimpleAction *action, GVariant *parameter static void activate_open(GSimpleAction *action, GVariant *parameter, gpointer app) { GtkFileChooserNative *native = gtk_file_chooser_native_new("Open File", GTK_WINDOW(main_window), GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", "_Cancel"); gint res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native)); - + if (res == GTK_RESPONSE_ACCEPT) { // TODO: Emit an event for our emulation loop g_message("%s", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native))); @@ -1015,6 +1124,18 @@ static void activate_reset(GSimpleAction *action, GVariant *parameter, gpointer } } +// app.clear_console GAction +// Clears the debugger console +static void activate_clear_console(GSimpleAction *action, GVariant *parameter, gpointer app) { + g_rec_mutex_lock(&console_output_lock); + + GtkTextView *text_view = builder_get(GTK_TEXT_VIEW, "console_screen"); + GtkTextBuffer *text_buf = gtk_text_view_get_buffer(text_view); + gtk_text_buffer_set_text(text_buf, "", -1); + + g_rec_mutex_unlock(&console_output_lock); +} + static void on_model_changed(GSimpleAction *action, GVariant *value, gpointer user_data) { if (!GB_is_inited(&gb)) { g_simple_action_set_state(action, value); @@ -1092,7 +1213,7 @@ static void gl_init(GtkWidget *w) { g_debug("GL Context: %p", gtk_gl_area_get_context(gl_area)); gtk_gl_area_make_current(gl_area); - + if (gtk_gl_area_get_error(gl_area) != NULL) { goto error; } @@ -1181,7 +1302,7 @@ static gboolean on_draw_vram_viewer_tileset(GtkWidget *widget, cairo_t *cr, gpoi height = gtk_widget_get_allocated_height(widget); gtk_render_background(context, cr, 0, 0, width, height); - + cairo_surface_t *surface = cairo_image_surface_create_for_data( (unsigned char *) tileset_buffer, CAIRO_FORMAT_RGB24, @@ -1405,7 +1526,7 @@ G_MODULE_EXPORT void on_boot_rom_location_changed(GtkWidget *w, gpointer user_da if (g_strcmp0(id, "other") == 0) { GtkFileChooserNative *native = gtk_file_chooser_native_new("Select Folder", preferences, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, "_Select", "_Cancel"); gint res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native)); - + if (res == GTK_RESPONSE_ACCEPT) { config.boot_rom_path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(native)); update_boot_rom_selector(builder); @@ -1537,7 +1658,7 @@ static void render_texture(void *pixels, void *previous) { glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - + GB_frame_blending_mode_t mode = get_frame_blending_mode(); if (!previous) { mode = GB_FRAME_BLENDING_MODE_DISABLED; @@ -1600,7 +1721,7 @@ static void update_window_geometry() { GdkGeometry hints; hints.min_width = GB_get_screen_width(&gb); hints.min_height = GB_get_screen_height(&gb); - + gtk_window_set_geometry_hints( GTK_WINDOW(main_window), NULL, @@ -1734,7 +1855,7 @@ static void on_vblank(gpointer data) { g_bytes_new(oamInfo[row].image, 128 * sizeof(uint32_t)), GDK_COLORSPACE_RGB, true, 8, 8, oamHeight, 8 * sizeof(uint32_t) ); - + GdkPixbuf *dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, 8 * 2, oamHeight * 2); gdk_pixbuf_scale(pixbuf, dest, @@ -2017,13 +2138,13 @@ static void load_boot_rom(GuiData *gui_data) { if (config.boot_rom_path != NULL && g_strcmp0(config.boot_rom_path, "other") != 0 && g_strcmp0(config.boot_rom_path, "auto") != 0) { boot_rom_path = g_build_filename(config.boot_rom_path, boot_rom_name, NULL); g_message("Trying to load boot ROM from %s", boot_rom_path); - + if (GB_load_boot_rom(&gb, boot_rom_path)) { g_free(boot_rom_path); g_warning("Falling back to internal boot ROM"); goto internal_boot_rom; } - + g_free(boot_rom_path); } else { internal_boot_rom: @@ -2039,7 +2160,7 @@ static void load_boot_rom(GuiData *gui_data) { boot_rom_data = g_bytes_get_data(boot_rom_f, &boot_rom_size); GB_load_boot_rom_from_buffer(&gb, boot_rom_data, boot_rom_size); - + g_bytes_unref(boot_rom_f); } } diff --git a/gtk3/main.h b/gtk3/main.h index 900b1a2..cb22790 100644 --- a/gtk3/main.h +++ b/gtk3/main.h @@ -70,6 +70,8 @@ static gboolean init_audio(); static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample); static char *sync_console_input(GB_gameboy_t *gb); static char *async_console_input(GB_gameboy_t *gb); +static void update_debugger_sidebar(GB_gameboy_t *gb); +static void append_pending_output(); static void on_console_log(gpointer user_data_gptr); static void console_log(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes); @@ -107,6 +109,7 @@ static void activate_open(GSimpleAction *action, GVariant *parameter, gpointer a static void activate_preferences(GSimpleAction *action, GVariant *parameter, gpointer app); static void activate_quit(GSimpleAction *action, GVariant *parameter, gpointer app); static void activate_reset(GSimpleAction *action, GVariant *parameter, gpointer app); +static void activate_clear_console(GSimpleAction *action, GVariant *parameter, gpointer app); static void on_model_changed(GSimpleAction *action, GVariant *value, gpointer user_data); static void on_mute_changed(GSimpleAction *action, GVariant *value, gpointer user_data); diff --git a/gtk3/resources/ui/window.ui b/gtk3/resources/ui/window.ui index 59d0c32..42f430c 100644 --- a/gtk3/resources/ui/window.ui +++ b/gtk3/resources/ui/window.ui @@ -140,27 +140,18 @@ Maximilian Mader https://github.com/max-m 600 376 - + True - False - - - True - True - False - word - 5 - 5 - 5 - 5 - False - False - True - - - + True + False + word + 5 + 5 + 5 + 5 + False + False + True @@ -191,25 +182,16 @@ Maximilian Mader https://github.com/max-m 320 80 - + True - False - - - True - True - word - 5 - 5 - 5 - 5 - False - True - - - + True + word + 5 + 5 + 5 + 5 + False + True @@ -232,26 +214,17 @@ Maximilian Mader https://github.com/max-m True in - + True - False - - - True - True - False - word - 5 - 5 - 5 - 5 - False - True - - - + True + False + word + 5 + 5 + 5 + 5 + False + True @@ -656,23 +629,6 @@ Maximilian Mader https://github.com/max-m 3 - - - Use Integer Scaling - True - True - False - 5 - 10 - True - - - - False - True - 4 - - Keep Aspect Ratio @@ -690,6 +646,23 @@ Maximilian Mader https://github.com/max-m 4 + + + Use Integer Scaling + True + True + False + 5 + 10 + True + + + + False + True + 4 + + False