2020-05-21 20:37:25 +00:00
|
|
|
#include "console_window.h"
|
2021-01-07 13:36:46 +00:00
|
|
|
#include "../util.h"
|
2020-05-21 20:37:25 +00:00
|
|
|
|
2021-01-02 15:05:14 +00:00
|
|
|
struct Selection {
|
|
|
|
gint start;
|
|
|
|
gint end;
|
|
|
|
};
|
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
struct _ConsoleWindow {
|
|
|
|
GtkWindowClass parent_class;
|
|
|
|
|
|
|
|
GtkEntry *input;
|
|
|
|
GtkTextView *output;
|
|
|
|
GtkTextView *sidebar_input;
|
|
|
|
GtkTextView *sidebar_output;
|
|
|
|
|
|
|
|
GAsyncQueue *input_queue;
|
|
|
|
GAsyncQueue *output_queue;
|
|
|
|
|
|
|
|
bool should_clear;
|
|
|
|
bool log_to_sidebar;
|
|
|
|
bool clear_sidebar;
|
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
GtkEntryCompletion *command_completion;
|
|
|
|
guint command_history_len;
|
|
|
|
gint command_history_index;
|
2021-01-02 15:05:14 +00:00
|
|
|
|
|
|
|
struct Selection auto_complete_range;
|
|
|
|
uintptr_t auto_complete_context;
|
|
|
|
bool ignore_auto_complete_context_reset;
|
|
|
|
|
2021-01-02 18:11:36 +00:00
|
|
|
bool developer_mode;
|
|
|
|
|
2021-01-02 15:05:14 +00:00
|
|
|
GB_gameboy_t *gb;
|
2020-05-21 20:37:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(ConsoleWindow, console_window, GTK_TYPE_WINDOW);
|
|
|
|
|
2021-01-02 15:05:14 +00:00
|
|
|
typedef enum {
|
|
|
|
PROP_GB_PTR = 1,
|
|
|
|
|
|
|
|
N_PROPERTIES
|
|
|
|
} ConsoleWindowProperty;
|
|
|
|
|
|
|
|
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
|
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *message;
|
|
|
|
GB_log_attributes attributes;
|
|
|
|
bool sidebar;
|
|
|
|
} AttributedMessage;
|
|
|
|
|
2021-01-02 15:05:14 +00:00
|
|
|
static void console_window_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) {
|
|
|
|
ConsoleWindow *self = (ConsoleWindow *) object;
|
|
|
|
|
|
|
|
switch ((ConsoleWindowProperty) property_id) {
|
|
|
|
case PROP_GB_PTR: self->gb = g_value_get_pointer(value); break;
|
|
|
|
default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void console_window_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
|
|
|
|
ConsoleWindow *self = (ConsoleWindow *) object;
|
|
|
|
|
|
|
|
switch ((ConsoleWindowProperty) property_id) {
|
|
|
|
case PROP_GB_PTR: g_value_set_pointer(value, self->gb); break;
|
|
|
|
default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
static gboolean on_input_key_press(GtkEntry *input, GdkEventKey *event, ConsoleWindow *self) {
|
|
|
|
switch (event->keyval) {
|
|
|
|
case GDK_KEY_Up:
|
|
|
|
if (event->type == GDK_KEY_PRESS) {
|
|
|
|
if (self->command_history_index + 1 == self->command_history_len) {
|
|
|
|
self->command_history_index = self->command_history_len - 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->command_history_index += 1;
|
|
|
|
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model = gtk_entry_completion_get_model(self->command_completion);
|
|
|
|
|
|
|
|
if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, self->command_history_index)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *entry = NULL;
|
|
|
|
gtk_tree_model_get(model, &iter, 0, &entry, -1);
|
2021-01-02 15:25:43 +00:00
|
|
|
gtk_entry_set_text(input, entry);
|
2020-05-24 13:20:16 +00:00
|
|
|
gtk_editable_set_position(GTK_EDITABLE(input), -1);
|
|
|
|
}
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
return true;
|
|
|
|
break;
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
case GDK_KEY_Down:
|
|
|
|
if (event->type == GDK_KEY_PRESS) {
|
|
|
|
if (self->command_history_index <= 0) {
|
2021-01-02 15:25:43 +00:00
|
|
|
gtk_entry_set_text(input, "");
|
2020-05-24 13:20:16 +00:00
|
|
|
self->command_history_index = -1;
|
|
|
|
}
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
if (self->command_history_index == -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->command_history_index -= 1;
|
|
|
|
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkTreeModel *model = gtk_entry_completion_get_model(self->command_completion);
|
|
|
|
|
|
|
|
if (!gtk_tree_model_iter_nth_child(model, &iter, NULL, self->command_history_index)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *entry = NULL;
|
|
|
|
gtk_tree_model_get(model, &iter, 0, &entry, -1);
|
2021-01-02 15:25:43 +00:00
|
|
|
gtk_entry_set_text(input, entry);
|
2020-05-24 13:20:16 +00:00
|
|
|
gtk_editable_set_position(GTK_EDITABLE(input), -1);
|
|
|
|
}
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GDK_KEY_Tab:
|
|
|
|
if (event->type == GDK_KEY_PRESS) {
|
2021-01-02 15:05:14 +00:00
|
|
|
if (self->auto_complete_context == 0) {
|
|
|
|
gint start_pos;
|
|
|
|
gint end_pos;
|
|
|
|
gtk_editable_get_selection_bounds(GTK_EDITABLE(input), &start_pos, &end_pos);
|
|
|
|
|
|
|
|
if (start_pos != end_pos) {
|
|
|
|
self->ignore_auto_complete_context_reset = true;
|
|
|
|
gtk_editable_delete_text(GTK_EDITABLE(input), start_pos, end_pos);
|
|
|
|
self->ignore_auto_complete_context_reset = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->auto_complete_range = (struct Selection){
|
|
|
|
.start = start_pos,
|
|
|
|
.end = start_pos
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *substring = gtk_editable_get_chars(GTK_EDITABLE(input), 0, self->auto_complete_range.start);
|
|
|
|
|
|
|
|
uintptr_t context = self->auto_complete_context;
|
|
|
|
char *completion = GB_debugger_complete_substring(self->gb, substring, &context);
|
|
|
|
g_free(substring);
|
|
|
|
|
|
|
|
if (completion) {
|
|
|
|
self->ignore_auto_complete_context_reset = true;
|
|
|
|
|
|
|
|
gtk_editable_select_region(GTK_EDITABLE(input), self->auto_complete_range.start, self->auto_complete_range.end);
|
|
|
|
gint new_end = self->auto_complete_range.start;
|
|
|
|
gtk_editable_delete_text(GTK_EDITABLE(input), self->auto_complete_range.start, self->auto_complete_range.end);
|
|
|
|
gtk_editable_insert_text(GTK_EDITABLE(input), completion, -1, &new_end);
|
|
|
|
self->auto_complete_range.end = new_end;
|
|
|
|
|
|
|
|
gtk_editable_set_position(GTK_EDITABLE(input), self->auto_complete_range.end);
|
|
|
|
|
|
|
|
self->ignore_auto_complete_context_reset = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
g_debug("BEEP (no completion found)");
|
|
|
|
gdk_display_beep(gdk_display_get_default());
|
|
|
|
}
|
|
|
|
|
|
|
|
self->auto_complete_context = context;
|
2020-05-24 13:20:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-02 15:05:14 +00:00
|
|
|
static void reset_auto_completion_context(GtkEntry *input, ConsoleWindow *self) {
|
|
|
|
if (self->ignore_auto_complete_context_reset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->auto_complete_context = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void on_delete_text(GtkEditable *editable, int start_pos, int end_pos, ConsoleWindow *self) {
|
|
|
|
reset_auto_completion_context(GTK_ENTRY(editable), self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void on_insert_text(GtkEditable *editable, char*new_text, int new_text_length, gpointer position, ConsoleWindow *self) {
|
|
|
|
reset_auto_completion_context(GTK_ENTRY(editable), self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void on_move_cursor(GtkEntry *input, GtkMovementStep step, int count, gboolean extend_selection, ConsoleWindow *self) {
|
|
|
|
reset_auto_completion_context(input, self);
|
|
|
|
}
|
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
static void console_window_init(ConsoleWindow *self) {
|
|
|
|
gtk_widget_init_template(GTK_WIDGET(self));
|
|
|
|
|
|
|
|
GtkTextBuffer *text_buf = gtk_text_view_get_buffer(self->output);
|
|
|
|
GtkTextTagTable *tag_table = 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);
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
gtk_text_view_set_buffer(
|
|
|
|
self->sidebar_output,
|
|
|
|
gtk_text_buffer_new(tag_table)
|
|
|
|
);
|
|
|
|
|
|
|
|
gtk_text_buffer_set_text(gtk_text_view_get_buffer(self->sidebar_input), "registers\nbacktrace\n", -1);
|
|
|
|
|
|
|
|
self->input_queue = g_async_queue_new();
|
|
|
|
self->output_queue = g_async_queue_new();
|
|
|
|
self->log_to_sidebar = false;
|
|
|
|
self->clear_sidebar = false;
|
2020-05-24 13:20:16 +00:00
|
|
|
self->command_history_index = -1;
|
|
|
|
|
|
|
|
GtkTreeIter iter;
|
|
|
|
GtkListStore *command_list_store = gtk_list_store_new(1, G_TYPE_STRING);
|
|
|
|
self->command_completion = gtk_entry_completion_new();
|
|
|
|
gtk_entry_completion_set_model(self->command_completion, GTK_TREE_MODEL(command_list_store));
|
|
|
|
gtk_entry_completion_set_text_column(self->command_completion, 0);
|
|
|
|
gtk_entry_completion_set_popup_completion(self->command_completion, false);
|
2021-01-02 15:05:14 +00:00
|
|
|
gtk_entry_completion_set_inline_completion(self->command_completion, false);
|
2020-05-24 13:20:16 +00:00
|
|
|
gtk_entry_set_completion(self->input, self->command_completion);
|
|
|
|
gtk_entry_set_input_hints(self->input, GTK_INPUT_HINT_NO_SPELLCHECK | GTK_INPUT_HINT_NO_EMOJI);
|
|
|
|
|
|
|
|
gtk_widget_add_events(GTK_WIDGET(self), GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
|
|
|
|
|
|
|
|
g_signal_connect(self->input, "key-press-event", G_CALLBACK(on_input_key_press), self);
|
|
|
|
g_signal_connect(self->input, "key-release-event", G_CALLBACK(on_input_key_press), self);
|
2021-01-02 15:05:14 +00:00
|
|
|
g_signal_connect(self->input, "delete-text", G_CALLBACK(on_delete_text), self);
|
|
|
|
g_signal_connect(self->input, "insert-text", G_CALLBACK(on_insert_text), self);
|
|
|
|
g_signal_connect(self->input, "move-cursor", G_CALLBACK(on_move_cursor), self);
|
2021-01-02 15:25:43 +00:00
|
|
|
|
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(self->input));
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void console_window_realize(GtkWidget *widget) {
|
|
|
|
ConsoleWindow *self = (ConsoleWindow *)widget;
|
|
|
|
|
|
|
|
GTK_WIDGET_CLASS(console_window_parent_class)->realize(widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes ownership of message
|
|
|
|
static void log_simple(ConsoleWindow *self, const char *message) {
|
|
|
|
AttributedMessage *attr_msg = g_new(AttributedMessage, 1);
|
|
|
|
attr_msg->message = message;
|
|
|
|
attr_msg->attributes = 0;
|
|
|
|
attr_msg->sidebar = false;
|
|
|
|
|
|
|
|
g_async_queue_push(self->output_queue, attr_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Use command history (arrow key (↑, ↓) events)
|
2020-05-24 13:20:16 +00:00
|
|
|
static void on_input_enter(GtkEntry *input, ConsoleWindow *self) {
|
2020-05-21 20:37:25 +00:00
|
|
|
const gchar *text_ptr = gtk_entry_get_text(input);
|
|
|
|
const gchar *text = NULL;
|
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
GtkTreeModel *model = gtk_entry_completion_get_model(self->command_completion);
|
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
if (g_strcmp0("", text_ptr) == 0) {
|
2020-05-24 13:20:16 +00:00
|
|
|
const char *last = NULL;
|
|
|
|
GtkTreeIter iter;
|
|
|
|
if (gtk_tree_model_get_iter_first(model, &iter)) {
|
|
|
|
gtk_tree_model_get(model, &iter, 0, &last, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last) text = last;
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
text = text_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text) {
|
2020-05-24 13:20:16 +00:00
|
|
|
const char *last = NULL;
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
GtkTreeIter iter;
|
|
|
|
if (gtk_tree_model_get_iter_first(model, &iter)) {
|
|
|
|
gtk_tree_model_get(model, &iter, 0, &last, -1);
|
|
|
|
}
|
2020-05-21 20:37:25 +00:00
|
|
|
|
|
|
|
// Add command to queue unless it was the last command issued
|
2020-05-24 13:20:16 +00:00
|
|
|
if (!last || g_strcmp0(last, text) != 0) {
|
|
|
|
gtk_list_store_prepend(GTK_LIST_STORE(model), &iter);
|
|
|
|
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, text, -1);
|
|
|
|
self->command_history_len += 1;
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_async_queue_push(self->input_queue, (gpointer) g_strdup(text));
|
|
|
|
gtk_entry_set_text(self->input, "");
|
2020-05-24 13:20:16 +00:00
|
|
|
|
|
|
|
self->command_history_index = -1;
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_sidebar(ConsoleWindow *self, GB_gameboy_t *gb) {
|
|
|
|
if (!GB_debugger_is_stopped(gb)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkTextBuffer *text_buf = gtk_text_view_get_buffer(self->sidebar_input);
|
|
|
|
gint line_count = gtk_text_buffer_get_line_count(text_buf);
|
|
|
|
|
|
|
|
self->log_to_sidebar = true;
|
|
|
|
self->clear_sidebar = true;
|
|
|
|
|
|
|
|
for (unsigned line = 0; line < line_count; ++line) {
|
|
|
|
GtkTextIter start_iter;
|
|
|
|
GtkTextIter end_iter;
|
|
|
|
gunichar ch;
|
|
|
|
|
|
|
|
gtk_text_buffer_get_iter_at_line(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(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
self->log_to_sidebar = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean console_window_draw(GtkWidget *widget, cairo_t *cr) {
|
|
|
|
ConsoleWindow *self = (ConsoleWindow *)widget;
|
|
|
|
GtkTextBuffer *main_text_buf = gtk_text_view_get_buffer(self->output);
|
|
|
|
GtkTextBuffer *sidebar_text_buf = gtk_text_view_get_buffer(self->sidebar_output);
|
|
|
|
|
|
|
|
if (self->should_clear) {
|
|
|
|
gtk_text_buffer_set_text(main_text_buf, "", -1);
|
|
|
|
gtk_text_buffer_set_text(sidebar_text_buf, "", -1);
|
|
|
|
|
|
|
|
// clear pending log messages
|
|
|
|
while (g_async_queue_try_pop(self->output_queue));
|
|
|
|
|
|
|
|
self->should_clear = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
GtkTextIter iter;
|
|
|
|
GtkTextIter start;
|
|
|
|
AttributedMessage *attr_msg = NULL;
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
GtkTextIter main_scroll_iter;
|
|
|
|
bool scroll_main = false;
|
|
|
|
|
|
|
|
GtkTextIter sidebar_scroll_iter;
|
|
|
|
bool scroll_sidebar = false;
|
|
|
|
|
|
|
|
if (self->clear_sidebar) {
|
|
|
|
gtk_text_buffer_set_text(sidebar_text_buf, "", -1);
|
|
|
|
self->clear_sidebar = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((attr_msg = g_async_queue_try_pop(self->output_queue))) {
|
|
|
|
GtkTextBuffer *text_buf = attr_msg->sidebar? sidebar_text_buf : main_text_buf;
|
|
|
|
|
|
|
|
gtk_text_buffer_get_end_iter(text_buf, &iter);
|
|
|
|
GtkTextMark *start_mark = gtk_text_buffer_create_mark(text_buf, NULL, &iter, true);
|
|
|
|
|
|
|
|
// give ownership of message to the text buffer
|
|
|
|
gtk_text_buffer_insert(text_buf, &iter, attr_msg->message, -1);
|
|
|
|
gtk_text_buffer_get_iter_at_mark(text_buf, &start, start_mark);
|
|
|
|
|
|
|
|
if (attr_msg->attributes & GB_LOG_BOLD) {
|
|
|
|
gtk_text_buffer_apply_tag_by_name(text_buf, "bold", &start, &iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr_msg->attributes & GB_LOG_DASHED_UNDERLINE) {
|
|
|
|
gtk_text_buffer_apply_tag_by_name(text_buf, "dashed_underline", &start, &iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attr_msg->attributes & GB_LOG_UNDERLINE) {
|
|
|
|
gtk_text_buffer_apply_tag_by_name(text_buf, "underline", &start, &iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_text_buffer_delete_mark(text_buf, start_mark);
|
|
|
|
|
|
|
|
if (attr_msg->sidebar) {
|
|
|
|
scroll_sidebar = true;
|
|
|
|
sidebar_scroll_iter = iter;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scroll_main = true;
|
|
|
|
main_scroll_iter = iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(attr_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scroll_main) {
|
2021-01-07 14:18:50 +00:00
|
|
|
text_view_scroll_to_bottom(self->output, gtk_text_buffer_create_mark(main_text_buf, NULL, &main_scroll_iter, true));
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (scroll_sidebar) {
|
2021-01-07 14:18:50 +00:00
|
|
|
text_view_scroll_to_bottom(self->sidebar_output, gtk_text_buffer_create_mark(sidebar_text_buf, NULL, &sidebar_scroll_iter, true));
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GTK_WIDGET_CLASS(console_window_parent_class)->draw(widget, cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void console_window_class_init(ConsoleWindowClass *class) {
|
|
|
|
gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(class), RESOURCE_PREFIX "ui/console_window.ui");
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), ConsoleWindow, input);
|
|
|
|
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), ConsoleWindow, output);
|
|
|
|
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), ConsoleWindow, sidebar_input);
|
|
|
|
gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(class), ConsoleWindow, sidebar_output);
|
|
|
|
|
2020-05-24 13:20:16 +00:00
|
|
|
gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(class), on_input_enter);
|
2020-05-21 20:37:25 +00:00
|
|
|
|
|
|
|
GTK_WIDGET_CLASS(class)->realize = console_window_realize;
|
|
|
|
GTK_WIDGET_CLASS(class)->draw = console_window_draw;
|
2021-01-02 15:05:14 +00:00
|
|
|
|
|
|
|
obj_properties[PROP_GB_PTR] = g_param_spec_pointer(
|
|
|
|
"gb", "SameBoy core pointer", "SameBoy Core pointer (GB_gameboy_t)",
|
|
|
|
G_PARAM_CONSTRUCT | G_PARAM_READWRITE
|
|
|
|
);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS(class)->set_property = console_window_set_property;
|
|
|
|
G_OBJECT_CLASS(class)->get_property = console_window_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_properties(G_OBJECT_CLASS(class), N_PROPERTIES, obj_properties);
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 15:05:14 +00:00
|
|
|
ConsoleWindow *console_window_new(GB_gameboy_t *gb) {
|
|
|
|
return g_object_new(CONSOLE_WINDOW_TYPE, "gb", gb, NULL);
|
2020-05-21 20:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function gets called every VBlank while the emulation is running.
|
2021-01-07 21:35:41 +00:00
|
|
|
char *console_window_get_async_input(ConsoleWindow *self, GB_gameboy_t *gb) {
|
2020-05-21 20:37:25 +00:00
|
|
|
self->clear_sidebar = true;
|
|
|
|
|
|
|
|
char *command = (char *)g_async_queue_try_pop(self->input_queue);
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
if (command) {
|
|
|
|
gchar *msg = g_strdup_printf("> %s\n", command);
|
|
|
|
log_simple(self, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will only be called if the debugger is in stopped mode (after a breakpoint hit for example),
|
|
|
|
// thus we block the emulation thread until input is available.
|
2021-01-07 21:35:41 +00:00
|
|
|
char *console_window_get_sync_input(ConsoleWindow *self, GB_gameboy_t *gb) {
|
2020-05-21 20:37:25 +00:00
|
|
|
update_sidebar(self, gb);
|
|
|
|
|
|
|
|
char *command = (char *)g_async_queue_pop(self->input_queue);
|
2021-01-02 15:05:14 +00:00
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
if (command) {
|
|
|
|
gchar *msg = g_strdup_printf("> %s\n", command);
|
|
|
|
log_simple(self, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return command;
|
|
|
|
}
|
|
|
|
|
2021-01-07 21:35:41 +00:00
|
|
|
void console_window_focus(ConsoleWindow *self) {
|
2021-01-02 18:11:36 +00:00
|
|
|
gtk_window_present_with_time(GTK_WINDOW(self), time(NULL));
|
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(self->input));
|
|
|
|
}
|
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
// Queues a message to be logged to the console
|
2021-01-07 21:35:41 +00:00
|
|
|
void console_window_log(ConsoleWindow *self, const char *message, GB_log_attributes attributes) {
|
2020-05-21 20:37:25 +00:00
|
|
|
if (!message || g_str_equal("", message)) return;
|
|
|
|
|
2021-01-02 18:11:36 +00:00
|
|
|
if (self->developer_mode) {
|
2021-01-07 21:35:41 +00:00
|
|
|
console_window_focus(self);
|
2021-01-02 18:11:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 20:37:25 +00:00
|
|
|
AttributedMessage *attr_msg = g_new(AttributedMessage, 1);
|
|
|
|
attr_msg->message = g_strdup(message);
|
|
|
|
attr_msg->attributes = attributes;
|
|
|
|
attr_msg->sidebar = self->log_to_sidebar;
|
|
|
|
|
|
|
|
g_async_queue_push(self->output_queue, attr_msg);
|
|
|
|
|
|
|
|
// mark as dirty
|
|
|
|
gtk_widget_queue_draw(GTK_WIDGET(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marks the console as to be cleared on the next redraw
|
2021-01-07 21:35:41 +00:00
|
|
|
void console_window_clear(ConsoleWindow *self) {
|
2020-05-21 20:37:25 +00:00
|
|
|
self->should_clear = true;
|
|
|
|
|
|
|
|
// mark as dirty
|
|
|
|
gtk_widget_queue_draw(GTK_WIDGET(self));
|
|
|
|
}
|
2021-01-02 15:25:43 +00:00
|
|
|
|
2021-01-07 21:35:41 +00:00
|
|
|
void console_window_break_debugger(ConsoleWindow *self, bool forced) {
|
2021-01-02 21:53:58 +00:00
|
|
|
if (!forced && !self->developer_mode) return;
|
|
|
|
|
2021-01-02 15:25:43 +00:00
|
|
|
GB_debugger_break(self->gb);
|
2021-01-07 21:35:41 +00:00
|
|
|
console_window_focus(self);
|
2021-01-02 15:25:43 +00:00
|
|
|
}
|
2021-01-02 16:40:42 +00:00
|
|
|
|
|
|
|
// Hack to avoid deadlocking on queue reads ...
|
2021-01-07 21:35:41 +00:00
|
|
|
void console_window_abort_debugger(ConsoleWindow *self) {
|
2021-01-02 16:40:42 +00:00
|
|
|
g_async_queue_push(self->input_queue, g_strdup("c\0"));
|
|
|
|
g_async_queue_push(self->output_queue, g_strdup("c\0"));
|
2021-01-07 21:35:41 +00:00
|
|
|
console_window_clear(self);
|
2021-01-02 16:40:42 +00:00
|
|
|
}
|
2021-01-02 18:11:36 +00:00
|
|
|
|
2021-01-07 21:35:41 +00:00
|
|
|
void console_window_set_developer_mode(ConsoleWindow *self, bool value) {
|
2021-01-02 18:11:36 +00:00
|
|
|
self->developer_mode = value;
|
|
|
|
}
|