X-Git-Url: https://irasnyder.com/gitweb/?a=blobdiff_plain;f=tilda.c;h=854520a4e5498e76d0ad8df7d4130d5e7808f53c;hb=e7ae1f9774ab8aac7379b5c6a1d78cb2ba9754cf;hp=c712fc7ac0289e3ca9c34f81f298bd219a139c05;hpb=cdfa28812023db7df374c63171cf4536ef5f78ba;p=tilda-gobject.git diff --git a/tilda.c b/tilda.c index c712fc7..854520a 100644 --- a/tilda.c +++ b/tilda.c @@ -1,27 +1,29 @@ -#include +#include +#include #include "tilda.h" -#include "tilda-window.h" -#include "tilda-terminal.h" +#include "tilda-controller.h" +#include "tomboykeybinder.h" +/* Project-global variables */ DBusGConnection *dbus_connection; -GPtrArray *windows; static void -tilda_initialize_dbus () +tilda_dbus_init () { debug_enter (); static const gchar service_name[] = "net.sourceforge.Tilda"; GError *error = NULL; DBusGProxy *driver_proxy; - int request_ret; + guint request_ret; + gboolean ret; // Initialize the DBus connection dbus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); if (dbus_connection == NULL) { - g_warning ("Unable to connect to dbus: %s", error->message); + g_warning (_("Unable to connect to DBus: %s\n"), error->message); g_error_free (error); return; } @@ -32,92 +34,107 @@ tilda_initialize_dbus () DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); - if (!org_freedesktop_DBus_request_name (driver_proxy, service_name, 0, &request_ret, &error)) + ret = org_freedesktop_DBus_request_name (driver_proxy, + service_name, + DBUS_NAME_FLAG_DO_NOT_QUEUE, + &request_ret, + &error); + + if (!ret) { - // FIXME: for whatever reason, this is wrong. The error message doesn't appear - // FIXME: when we were unable to register the service. Perhaps there's a more - // FIXME: GLib-y way of doing this? - g_warning ("Unable to register service: %s", error->message); + g_warning (_("Unable to communicate with DBus: %s\n"), error->message); g_error_free (error); } + if (request_ret == DBUS_REQUEST_NAME_REPLY_EXISTS) + { + g_critical (_("There is already an instance of Tilda running\n")); + exit (EXIT_FAILURE); + } + g_object_unref (driver_proxy); } -static gint -tilda_find_next_free_window_number () +static void +tilda_parse_command_line (gint argc, gchar *argv[]) { - debug_enter (); + debug_enter (); + + gboolean version = FALSE; - gint i, j; - gboolean found; + /* All of the various command-line options */ + const GOptionEntry cl_opts[] = { + { "version", 'V', 0, G_OPTION_ARG_NONE, &version, N_("Show version information"), NULL }, + { NULL }, + }; - for (i=0; imessage); + g_printerr (_("The command \"tilda --help\" will show all possible options\n")); + g_error_free (error); + exit (EXIT_FAILURE); + } - for (j=0; jlen; ++j) - { - TildaWindow *tw = g_ptr_array_index (windows, j); + /* If we need to show the version, show it then exit normally */ + if (version) + { + g_print ("%s\n\n", TILDA_VERSION); - if (tw->number == i) - { - found = TRUE; - break; - } - } + g_print ("Copyright (c) 2005-2008 Tristan Sloughter (sloutri@iit.edu)\n"); + g_print ("Copyright (c) 2005-2008 Ira W. Snyder (tilda@irasnyder.com)\n\n"); - if (!found) - return i; - } + g_print ("This program comes with ABSOLUTELY NO WARRANTY.\n"); + g_print ("This is free software, and you are welcome to redistribute it\n"); + g_print ("under certain conditions. See the file COPYING for details.\n"); - return 0; + exit (EXIT_SUCCESS); + } } -static TildaWindow * -tilda_add_window () +static void +tilda_termination_handler (gint signum) { - debug_enter (); - - TildaWindow *ret; - gint number; - - number = tilda_find_next_free_window_number (); - ret = g_object_new (TILDA_TYPE_WINDOW, "number", number, NULL); - - g_ptr_array_add (windows, ret); + debug_enter (); + debug_printf ("signum: %d\n", signum); - debug_printf ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, windows->len-1); - return ret; + gtk_main_quit (); } +/* Hook up all system signal handlers */ static void -tilda_del_window (gint number) +tilda_signal_handlers_init () { - debug_enter (); + struct sigaction sa; - gint i; - TildaWindow *win; + /* Hook up signal handlers */ + sa.sa_handler = tilda_termination_handler; + sigemptyset (&sa.sa_mask); + sa.sa_flags = 0; - for (i=0; ilen; ++i) - { - win = g_ptr_array_index (windows, i); - - if (win->number == number) - { - debug_printf ("Deleting window 0x%x (number %d of %d)\n", win, win->number, windows->len-1); - g_ptr_array_remove_index (windows, i); - g_object_unref (G_OBJECT(win)); - break; - } - } + sigaction (SIGINT, &sa, NULL); + sigaction (SIGQUIT, &sa, NULL); + sigaction (SIGABRT, &sa, NULL); + sigaction (SIGTERM, &sa, NULL); + + /* SIGKILL cannot be caught according to sigaction(2) and signal(7) */ + /* sigaction (SIGKILL, &sa, NULL); */ } int main (int argc, char *argv[]) { debug_enter (); - TildaWindow *tw; + TildaController *tilda; #if ENABLE_NLS /* Gettext Initialization */ @@ -126,29 +143,32 @@ int main (int argc, char *argv[]) textdomain (PACKAGE); #endif + /* Parse the command-line options */ + tilda_parse_command_line (argc, argv); + /* Initialize GTK+ (and the GObject system) */ gtk_init (&argc, &argv); + /* Hook up the signal handlers */ + tilda_signal_handlers_init (); + /* Initialize the keybinder */ tomboy_keybinder_init (); /* Start our connection to DBus */ - tilda_initialize_dbus (); - - /* Initialize the array of windows */ - windows = g_ptr_array_new (); + tilda_dbus_init (); - /* Create a TildaWindow, run it, and exit when it does, basically. - * - * This is nothing like what the real main() will be, but it's - * a good start for testing and integration of more of TildaWindow - * and TildaTerminal. */ - tw = tilda_add_window (); + /* Create a TildaController, which manages TildaWindows, which in turn manages + * TildaTerminals. Exit when it does. */ + tilda = g_object_new (TILDA_TYPE_CONTROLLER, NULL); debug_printf ("Starting gtk_main()!\n"); gtk_main (); debug_printf ("Out of gtk_main(), going down\n"); + /* Unref the TildaController that controls this whole operation */ + g_object_unref (G_OBJECT(tilda)); + return 0; }