--- BasiliskII/src/Unix/prefs_editor_gtk.cpp 2005/06/19 15:52:09 1.28 +++ BasiliskII/src/Unix/prefs_editor_gtk.cpp 2010/01/15 01:53:31 1.41 @@ -1,7 +1,7 @@ /* * prefs_editor_gtk.cpp - Preferences editor, Unix implementation using GTK+ * - * Basilisk II (C) 1997-2005 Christian Bauer + * Basilisk II (C) 1997-2008 Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -39,10 +39,13 @@ #include "prefs.h" #include "prefs_editor.h" +#define DEBUG 0 +#include "debug.h" + // Global variables static GtkWidget *win; // Preferences window -static bool start_clicked = true; // Return value of PrefsEditor() function +static bool start_clicked = false; // Return value of PrefsEditor() function // Prototypes @@ -60,6 +63,12 @@ static void read_settings(void); * Utility functions */ +#if ! GLIB_CHECK_VERSION(2,0,0) +#define G_OBJECT(obj) GTK_OBJECT(obj) +#define g_object_get_data(obj, key) gtk_object_get_data((obj), (key)) +#define g_object_set_data(obj, key, data) gtk_object_set_data((obj), (key), (data)) +#endif + struct opt_desc { int label_id; GtkSignalFunc func; @@ -69,6 +78,39 @@ struct combo_desc { int label_id; }; +struct file_req_assoc { + file_req_assoc(GtkWidget *r, GtkWidget *e) : req(r), entry(e) {} + GtkWidget *req; + GtkWidget *entry; +}; + +static void cb_browse_ok(GtkWidget *button, file_req_assoc *assoc) +{ + gchar *file = (char *)gtk_file_selection_get_filename(GTK_FILE_SELECTION(assoc->req)); + gtk_entry_set_text(GTK_ENTRY(assoc->entry), file); + gtk_widget_destroy(assoc->req); + delete assoc; +} + +static void cb_browse(GtkWidget *widget, void *user_data) +{ + GtkWidget *req = gtk_file_selection_new(GetString(STR_BROWSE_TITLE)); + gtk_signal_connect_object(GTK_OBJECT(req), "delete_event", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(req)); + gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(req)->ok_button), "clicked", GTK_SIGNAL_FUNC(cb_browse_ok), new file_req_assoc(req, (GtkWidget *)user_data)); + gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(req)->cancel_button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(req)); + gtk_widget_show(req); +} + +static GtkWidget *make_browse_button(GtkWidget *entry) +{ + GtkWidget *button; + + button = gtk_button_new_with_label(GetString(STR_BROWSE_CTRL)); + gtk_widget_show(button); + gtk_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc)cb_browse, (void *)entry); + return button; +} + static void add_menu_item(GtkWidget *menu, int label_id, GtkSignalFunc func) { GtkWidget *item = gtk_menu_item_new_with_label(GetString(label_id)); @@ -82,16 +124,16 @@ static GtkWidget *make_pane(GtkWidget *n GtkWidget *frame, *label, *box; frame = gtk_frame_new(NULL); - gtk_widget_show(frame); - gtk_container_border_width(GTK_CONTAINER(frame), 4); - - label = gtk_label_new(GetString(title_id)); - gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label); + gtk_container_set_border_width(GTK_CONTAINER(frame), 4); box = gtk_vbox_new(FALSE, 4); - gtk_widget_show(box); gtk_container_set_border_width(GTK_CONTAINER(box), 4); gtk_container_add(GTK_CONTAINER(frame), box); + + gtk_widget_show_all(frame); + + label = gtk_label_new(GetString(title_id)); + gtk_notebook_append_page(GTK_NOTEBOOK(notebook), frame, label); return box; } @@ -132,6 +174,86 @@ static GtkWidget *make_table(GtkWidget * return table; } +static GtkWidget *table_make_option_menu(GtkWidget *table, int row, int label_id, const opt_desc *options, int active) +{ + GtkWidget *label, *opt, *menu; + + label = gtk_label_new(GetString(label_id)); + gtk_widget_show(label); + gtk_table_attach(GTK_TABLE(table), label, 0, 1, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4); + + opt = gtk_option_menu_new(); + gtk_widget_show(opt); + menu = gtk_menu_new(); + + while (options->label_id) { + add_menu_item(menu, options->label_id, options->func); + options++; + } + gtk_menu_set_active(GTK_MENU(menu), active); + + gtk_option_menu_set_menu(GTK_OPTION_MENU(opt), menu); + gtk_table_attach(GTK_TABLE(table), opt, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4); + return menu; +} + +static GtkWidget *table_make_combobox(GtkWidget *table, int row, int label_id, const char *default_value, GList *glist) +{ + GtkWidget *label, *combo; + char str[32]; + + label = gtk_label_new(GetString(label_id)); + gtk_widget_show(label); + gtk_table_attach(GTK_TABLE(table), label, 0, 1, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4); + + combo = gtk_combo_new(); + gtk_widget_show(combo); + gtk_combo_set_popdown_strings(GTK_COMBO(combo), glist); + + gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(combo)->entry), default_value); + gtk_table_attach(GTK_TABLE(table), combo, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4); + + return combo; +} + +static GtkWidget *table_make_combobox(GtkWidget *table, int row, int label_id, const char *default_value, const combo_desc *options) +{ + GList *glist = NULL; + while (options->label_id) { + glist = g_list_append(glist, (void *)GetString(options->label_id)); + options++; + } + + return table_make_combobox(table, row, label_id, default_value, glist); +} + +static GtkWidget *table_make_file_entry(GtkWidget *table, int row, int label_id, const char *prefs_item, bool only_dirs = false) +{ + GtkWidget *box, *label, *entry, *button; + + label = gtk_label_new(GetString(label_id)); + gtk_widget_show(label); + gtk_table_attach(GTK_TABLE(table), label, 0, 1, row, row + 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 4, 4); + + const char *str = PrefsFindString(prefs_item); + if (str == NULL) + str = ""; + + box = gtk_hbox_new(FALSE, 4); + gtk_widget_show(box); + gtk_table_attach(GTK_TABLE(table), box, 1, 2, row, row + 1, (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)0, 4, 4); + + entry = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(entry), str); + gtk_widget_show(entry); + gtk_box_pack_start(GTK_BOX(box), entry, TRUE, TRUE, 0); + + button = make_browse_button(entry); + gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); + g_object_set_data(G_OBJECT(entry), "chooser_button", button); + return entry; +} + static GtkWidget *make_option_menu(GtkWidget *top, int label_id, const opt_desc *options, int active) { GtkWidget *box, *label, *opt, *menu; @@ -306,7 +428,7 @@ static void mn_about(...) dialog = gnome_about_new( "Basilisk II", version, - "Copyright (C) 1997-2004 Christian Bauer", + "Copyright (C) 1997-2008 Christian Bauer", authors, "Basilisk II comes with ABSOLUTELY NO WARRANTY." "This is free software, and you are welcome to redistribute it" @@ -322,7 +444,7 @@ static void mn_about(...) char str[512]; sprintf(str, "Basilisk II\nVersion %d.%d\n\n" - "Copyright (C) 1997-2004 Christian Bauer et al.\n" + "Copyright (C) 1997-2008 Christian Bauer et al.\n" "E-mail: Christian.Bauer@uni-mainz.de\n" "http://www.uni-mainz.de/~bauec002/B2Main.html\n\n" "Basilisk II comes with ABSOLUTELY NO\n" @@ -363,7 +485,7 @@ static void mn_zap_pram(...) // Menu item descriptions static GtkItemFactoryEntry menu_items[] = { {(gchar *)GetString(STR_PREFS_MENU_FILE_GTK), NULL, NULL, 0, ""}, - {(gchar *)GetString(STR_PREFS_ITEM_START_GTK), NULL, GTK_SIGNAL_FUNC(cb_start), 0, NULL}, + {(gchar *)GetString(STR_PREFS_ITEM_START_GTK), "S", GTK_SIGNAL_FUNC(cb_start), 0, NULL}, {(gchar *)GetString(STR_PREFS_ITEM_ZAP_PRAM_GTK), NULL, GTK_SIGNAL_FUNC(mn_zap_pram), 0, NULL}, {(gchar *)GetString(STR_PREFS_ITEM_SEPL_GTK), NULL, NULL, 0, ""}, {(gchar *)GetString(STR_PREFS_ITEM_QUIT_GTK), "Q", GTK_SIGNAL_FUNC(cb_quit), 0, NULL}, @@ -397,10 +519,10 @@ bool PrefsEditor(void) gtk_box_pack_start(GTK_BOX(box), menu_bar, FALSE, TRUE, 0); GtkWidget *notebook = gtk_notebook_new(); - gtk_widget_show(notebook); gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP); gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), FALSE); gtk_box_pack_start(GTK_BOX(box), notebook, TRUE, TRUE, 0); + gtk_widget_realize(notebook); create_volumes_pane(notebook); create_scsi_pane(notebook); @@ -409,6 +531,7 @@ bool PrefsEditor(void) create_serial_pane(notebook); create_memory_pane(notebook); create_jit_pane(notebook); + gtk_widget_show(notebook); static const opt_desc buttons[] = { {STR_START_BUTTON, GTK_SIGNAL_FUNC(cb_start)}, @@ -437,12 +560,6 @@ static void cl_selected(GtkWidget *list, selected_volume = row; } -struct file_req_assoc { - file_req_assoc(GtkWidget *r, GtkWidget *e) : req(r), entry(e) {} - GtkWidget *req; - GtkWidget *entry; -}; - // Volume selected for addition static void add_volume_ok(GtkWidget *button, file_req_assoc *assoc) { @@ -595,6 +712,27 @@ static GtkWidget *w_jit_cache_size; static GtkWidget *w_jit_lazy_flush; static GtkWidget *w_jit_follow_const_jumps; +// Are we running a JIT capable CPU? +static bool is_jit_capable(void) +{ +#if USE_JIT && (defined __i386__ || defined __x86_64__) + return true; +#elif defined __APPLE__ && defined __MACH__ + // XXX run-time detect so that we can use a PPC GUI prefs editor + static char cpu[10]; + if (cpu[0] == 0) { + FILE *fp = popen("uname -p", "r"); + if (fp == NULL) + return false; + fgets(cpu, sizeof(cpu) - 1, fp); + fclose(fp); + } + if (cpu[0] == 'i' && cpu[2] == '8' && cpu[3] == '6') // XXX assuming i?86 + return true; +#endif + return false; +} + // Set sensitivity of widgets static void set_jit_sensitive(void) { @@ -633,19 +771,19 @@ static void tb_jit_follow_const_jumps(Gt // Read settings from widgets and set preferences static void read_jit_settings(void) { -#if USE_JIT - bool jit_enabled = PrefsFindBool("jit"); + bool jit_enabled = is_jit_capable() && PrefsFindBool("jit"); if (jit_enabled) { const char *str = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(w_jit_cache_size)->entry)); PrefsReplaceInt32("jitcachesize", atoi(str)); } -#endif } // Create "JIT Compiler" pane static void create_jit_pane(GtkWidget *top) { -#if USE_JIT + if (!is_jit_capable()) + return; + GtkWidget *box, *table, *label, *menu; char str[32]; @@ -671,7 +809,6 @@ static void create_jit_pane(GtkWidget *t w_jit_follow_const_jumps = make_checkbox(box, STR_JIT_FOLLOW_CONST_JUMPS, "jitinline", GTK_SIGNAL_FUNC(tb_jit_follow_const_jumps)); set_jit_sensitive(); -#endif } /* @@ -994,7 +1131,9 @@ static GtkWidget *w_mouse_wheel_lines; // Set sensitivity of widgets static void set_input_sensitive(void) { - gtk_widget_set_sensitive(w_keycode_file, PrefsFindBool("keycodes")); + const bool use_keycodes = PrefsFindBool("keycodes"); + gtk_widget_set_sensitive(w_keycode_file, use_keycodes); + gtk_widget_set_sensitive(GTK_WIDGET(g_object_get_data(G_OBJECT(w_keycode_file), "chooser_button")), use_keycodes); gtk_widget_set_sensitive(w_mouse_wheel_lines, PrefsFindInt32("mousewheelmode") == 1); } @@ -1024,13 +1163,33 @@ static void read_input_settings(void) // Create "Input" pane static void create_input_pane(GtkWidget *top) { - GtkWidget *box, *hbox, *menu, *label; + GtkWidget *box, *hbox, *menu, *label, *button; GtkObject *adj; box = make_pane(top, STR_INPUT_PANE_TITLE); make_checkbox(box, STR_KEYCODES_CTRL, "keycodes", GTK_SIGNAL_FUNC(tb_keycodes)); - w_keycode_file = make_file_entry(box, STR_KEYCODE_FILE_CTRL, "keycodefile"); + + hbox = gtk_hbox_new(FALSE, 4); + gtk_widget_show(hbox); + gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0); + + label = gtk_label_new(GetString(STR_KEYCODES_CTRL)); + gtk_widget_show(label); + gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); + + const char *str = PrefsFindString("keycodefile"); + if (str == NULL) + str = ""; + + w_keycode_file = gtk_entry_new(); + gtk_entry_set_text(GTK_ENTRY(w_keycode_file), str); + gtk_widget_show(w_keycode_file); + gtk_box_pack_start(GTK_BOX(hbox), w_keycode_file, TRUE, TRUE, 0); + + button = make_browse_button(w_keycode_file); + gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); + g_object_set_data(G_OBJECT(w_keycode_file), "chooser_button", button); make_separator(box); @@ -1271,9 +1430,15 @@ static void create_serial_pane(GtkWidget * "Memory/Misc" pane */ -static GtkObject *w_ramsize_adj; +static GtkWidget *w_ramsize; static GtkWidget *w_rom_file; +// Don't use CPU when idle? +static void tb_idlewait(GtkWidget *widget) +{ + PrefsReplaceBool("idlewait", GTK_TOGGLE_BUTTON(widget)->active); +} + // "Ignore SEGV" button toggled #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION static void tb_ignoresegv(GtkWidget *widget) @@ -1296,9 +1461,10 @@ static void mn_cpu_68040(...) {PrefsRepl // Read settings from widgets and set preferences static void read_memory_settings(void) { - PrefsReplaceInt32("ramsize", int(GTK_ADJUSTMENT(w_ramsize_adj)->value) << 20); + const char *str = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(w_ramsize)->entry)); + PrefsReplaceInt32("ramsize", atoi(str) << 20); - const char *str = get_file_entry_path(w_rom_file); + str = get_file_entry_path(w_rom_file); if (str && strlen(str)) PrefsReplaceString("rom", str); else @@ -1309,47 +1475,27 @@ static void read_memory_settings(void) // Create "Memory/Misc" pane static void create_memory_pane(GtkWidget *top) { - GtkWidget *box, *hbox, *vbox, *hbox2, *label, *scale; + GtkWidget *box, *hbox, *table, *label, *menu; box = make_pane(top, STR_MEMORY_MISC_PANE_TITLE); + table = make_table(box, 2, 5); - hbox = gtk_hbox_new(FALSE, 4); - gtk_widget_show(hbox); - - label = gtk_label_new(GetString(STR_RAMSIZE_SLIDER)); - gtk_widget_show(label); - gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); - - vbox = gtk_vbox_new(FALSE, 4); - gtk_widget_show(vbox); - - gfloat min, max; - min = 1; - max = 1024; - w_ramsize_adj = gtk_adjustment_new(min, min, max, 1, 16, 0); - gtk_adjustment_set_value(GTK_ADJUSTMENT(w_ramsize_adj), PrefsFindInt32("ramsize") >> 20); - - scale = gtk_hscale_new(GTK_ADJUSTMENT(w_ramsize_adj)); - gtk_widget_show(scale); - gtk_scale_set_digits(GTK_SCALE(scale), 0); - gtk_box_pack_start(GTK_BOX(vbox), scale, TRUE, TRUE, 0); - - hbox2 = gtk_hbox_new(FALSE, 4); - gtk_widget_show(hbox2); - - char val[32]; - sprintf(val, GetString(STR_RAMSIZE_FMT), int(min)); - label = gtk_label_new(val); - gtk_widget_show(label); - gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0); - - sprintf(val, GetString(STR_RAMSIZE_FMT), int(max)); - label = gtk_label_new(val); - gtk_widget_show(label); - gtk_box_pack_end(GTK_BOX(hbox2), label, FALSE, FALSE, 0); - gtk_box_pack_start(GTK_BOX(vbox), hbox2, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0); - gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0); + static const combo_desc options[] = { + STR_RAMSIZE_2MB_LAB, + STR_RAMSIZE_4MB_LAB, + STR_RAMSIZE_8MB_LAB, + STR_RAMSIZE_16MB_LAB, + STR_RAMSIZE_32MB_LAB, + STR_RAMSIZE_64MB_LAB, + STR_RAMSIZE_128MB_LAB, + STR_RAMSIZE_256MB_LAB, + STR_RAMSIZE_512MB_LAB, + STR_RAMSIZE_1024MB_LAB, + 0 + }; + char default_ramsize[10]; + sprintf(default_ramsize, "%d", PrefsFindInt32("ramsize") >> 20); + w_ramsize = table_make_combobox(table, 0, STR_RAMSIZE_CTRL, default_ramsize, options); static const opt_desc model_options[] = { {STR_MODELID_5_LAB, GTK_SIGNAL_FUNC(mn_modelid_5)}, @@ -1361,7 +1507,7 @@ static void create_memory_pane(GtkWidget case 5: active = 0; break; case 14: active = 1; break; } - make_option_menu(box, STR_MODELID_CTRL, model_options, active); + table_make_option_menu(table, 2, STR_MODELID_CTRL, model_options, active); #if EMULATED_68K static const opt_desc cpu_options[] = { @@ -1380,11 +1526,12 @@ static void create_memory_pane(GtkWidget case 3: active = fpu ? 3 : 2; break; case 4: active = 4; } - make_option_menu(box, STR_CPU_CTRL, cpu_options, active); + table_make_option_menu(table, 3, STR_CPU_CTRL, cpu_options, active); #endif - w_rom_file = make_file_entry(box, STR_ROM_FILE_CTRL, "rom"); + w_rom_file = table_make_file_entry(table, 4, STR_ROM_FILE_CTRL, "rom"); + make_checkbox(box, STR_IDLEWAIT_CTRL, "idlewait", GTK_SIGNAL_FUNC(tb_idlewait)); #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION make_checkbox(box, STR_IGNORESEGV_CTRL, "ignoresegv", GTK_SIGNAL_FUNC(tb_ignoresegv)); #endif @@ -1405,3 +1552,280 @@ static void read_settings(void) read_memory_settings(); read_jit_settings(); } + + +#ifdef STANDALONE_GUI +#include +#include +#include "rpc.h" + +/* + * Fake unused data and functions + */ + +uint8 XPRAM[XPRAM_SIZE]; +void MountVolume(void *fh) { } +void FileDiskLayout(loff_t size, uint8 *data, loff_t &start_byte, loff_t &real_size) { } + +#if defined __APPLE__ && defined __MACH__ +void DarwinSysInit(void) { } +void DarwinSysExit(void) { } +void DarwinAddFloppyPrefs(void) { } +void DarwinAddSerialPrefs(void) { } +bool DarwinCDReadTOC(char *, uint8 *) { } +#endif + + +/* + * Display alert + */ + +static void dl_destroyed(void) +{ + gtk_main_quit(); +} + +static void display_alert(int title_id, int prefix_id, int button_id, const char *text) +{ + char str[256]; + sprintf(str, GetString(prefix_id), text); + + GtkWidget *dialog = gtk_dialog_new(); + gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id)); + gtk_container_border_width(GTK_CONTAINER(dialog), 5); + gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150); + gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL); + + GtkWidget *label = gtk_label_new(str); + gtk_widget_show(label); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0); + + GtkWidget *button = gtk_button_new_with_label(GetString(button_id)); + gtk_widget_show(button); + gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog)); + gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0); + GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT); + gtk_widget_grab_default(button); + gtk_widget_show(dialog); + + gtk_main(); +} + + +/* + * Display error alert + */ + +void ErrorAlert(const char *text) +{ + display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text); +} + + +/* + * Display warning alert + */ + +void WarningAlert(const char *text) +{ + display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text); +} + + +/* + * RPC handlers + */ + +static GMainLoop *g_gui_loop; + +static int handle_ErrorAlert(rpc_connection_t *connection) +{ + D(bug("handle_ErrorAlert\n")); + + int error; + char *str; + if ((error = rpc_method_get_args(connection, RPC_TYPE_STRING, &str, RPC_TYPE_INVALID)) < 0) + return error; + + ErrorAlert(str); + free(str); + return RPC_ERROR_NO_ERROR; +} + +static int handle_WarningAlert(rpc_connection_t *connection) +{ + D(bug("handle_WarningAlert\n")); + + int error; + char *str; + if ((error = rpc_method_get_args(connection, RPC_TYPE_STRING, &str, RPC_TYPE_INVALID)) < 0) + return error; + + WarningAlert(str); + free(str); + return RPC_ERROR_NO_ERROR; +} + +static int handle_Exit(rpc_connection_t *connection) +{ + D(bug("handle_Exit\n")); + + g_main_quit(g_gui_loop); + return RPC_ERROR_NO_ERROR; +} + + +/* + * SIGCHLD handler + */ + +static char g_app_path[PATH_MAX]; +static rpc_connection_t *g_gui_connection = NULL; + +static void sigchld_handler(int sig, siginfo_t *sip, void *) +{ + D(bug("Child %d exitted with status = %x\n", sip->si_pid, sip->si_status)); + + // XXX perform a new wait because sip->si_status is sometimes not + // the exit _value_ on MacOS X but rather the usual status field + // from waitpid() -- we could arrange this code in some other way... + int status; + if (waitpid(sip->si_pid, &status, 0) < 0) + status = sip->si_status; + if (WIFEXITED(status)) + status = WEXITSTATUS(status); + if (status & 0x80) + status |= -1 ^0xff; + + if (status < 0) { // negative -> execlp/-errno + char str[256]; + sprintf(str, GetString(STR_NO_B2_EXE_FOUND), g_app_path, strerror(-status)); + ErrorAlert(str); + status = 1; + } + + if (status != 0) { + if (g_gui_connection) + rpc_exit(g_gui_connection); + exit(status); + } +} + + +/* + * Start standalone GUI + */ + +int main(int argc, char *argv[]) +{ +#ifdef HAVE_GNOMEUI + // Init GNOME/GTK + char version[16]; + sprintf(version, "%d.%d", VERSION_MAJOR, VERSION_MINOR); + gnome_init("Basilisk II", version, argc, argv); +#else + // Init GTK + gtk_set_locale(); + gtk_init(&argc, &argv); +#endif + + // Read preferences + PrefsInit(NULL, argc, argv); + + // Show preferences editor + bool start = PrefsEditor(); + + // Exit preferences + PrefsExit(); + + // Transfer control to the executable + if (start) { + char gui_connection_path[64]; + sprintf(gui_connection_path, "/org/BasiliskII/GUI/%d", getpid()); + + // Catch exits from the child process + struct sigaction sigchld_sa, old_sigchld_sa; + sigemptyset(&sigchld_sa.sa_mask); + sigchld_sa.sa_sigaction = sigchld_handler; + sigchld_sa.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; + if (sigaction(SIGCHLD, &sigchld_sa, &old_sigchld_sa) < 0) { + char str[256]; + sprintf(str, GetString(STR_SIG_INSTALL_ERR), SIGCHLD, strerror(errno)); + ErrorAlert(str); + return 1; + } + + // Search and run the BasiliskII executable + char *p; + strcpy(g_app_path, argv[0]); + if ((p = strstr(g_app_path, "BasiliskIIGUI.app/Contents/MacOS")) != NULL) { + strcpy(p, "BasiliskII.app/Contents/MacOS/BasiliskII"); + if (access(g_app_path, X_OK) < 0) { + char str[256]; + sprintf(str, GetString(STR_NO_B2_EXE_FOUND), g_app_path, strerror(errno)); + WarningAlert(str); + strcpy(g_app_path, "/Applications/BasiliskII.app/Contents/MacOS/BasiliskII"); + } + } else { + p = strrchr(g_app_path, '/'); + p = p ? p + 1 : g_app_path; + strcpy(p, "BasiliskII"); + } + + int pid = fork(); + if (pid == 0) { + D(bug("Trying to execute %s\n", g_app_path)); + execlp(g_app_path, g_app_path, "--gui-connection", gui_connection_path, (char *)NULL); +#ifdef _POSIX_PRIORITY_SCHEDULING + // XXX get a chance to run the parent process so that to not confuse/upset GTK... + sched_yield(); +#endif + _exit(-errno); + } + + // Establish a connection to Basilisk II + if ((g_gui_connection = rpc_init_server(gui_connection_path)) == NULL) { + printf("ERROR: failed to initialize GUI-side RPC server connection\n"); + return 1; + } + static const rpc_method_descriptor_t vtable[] = { + { RPC_METHOD_ERROR_ALERT, handle_ErrorAlert }, + { RPC_METHOD_WARNING_ALERT, handle_WarningAlert }, + { RPC_METHOD_EXIT, handle_Exit } + }; + if (rpc_method_add_callbacks(g_gui_connection, vtable, sizeof(vtable) / sizeof(vtable[0])) < 0) { + printf("ERROR: failed to setup GUI method callbacks\n"); + return 1; + } + int socket; + if ((socket = rpc_listen_socket(g_gui_connection)) < 0) { + printf("ERROR: failed to initialize RPC server thread\n"); + return 1; + } + + g_gui_loop = g_main_new(TRUE); + while (g_main_is_running(g_gui_loop)) { + + // Process a few events pending + const int N_EVENTS_DISPATCH = 10; + for (int i = 0; i < N_EVENTS_DISPATCH; i++) { + if (!g_main_iteration(FALSE)) + break; + } + + // Check for RPC events (100 ms timeout) + int ret = rpc_wait_dispatch(g_gui_connection, 100000); + if (ret == 0) + continue; + if (ret < 0) + break; + rpc_dispatch(g_gui_connection); + } + + rpc_exit(g_gui_connection); + return 0; + } + + return 0; +} +#endif