--- BasiliskII/src/Unix/prefs_editor_gtk.cpp 2005/11/21 21:39:08 1.29 +++ BasiliskII/src/Unix/prefs_editor_gtk.cpp 2008/01/01 09:40:33 1.40 @@ -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; @@ -115,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; } @@ -419,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" @@ -435,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" @@ -510,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); @@ -522,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)}, @@ -702,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) { @@ -740,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]; @@ -778,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 } /* @@ -1403,6 +1433,12 @@ static void create_serial_pane(GtkWidget 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) @@ -1495,6 +1531,7 @@ static void create_memory_pane(GtkWidget 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 @@ -1515,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(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