[Controller] Disallow multiple TildaWindows to bind to the same key
[tilda-gobject.git] / tilda.c
diff --git a/tilda.c b/tilda.c
index fc0497d..854520a 100644 (file)
--- a/tilda.c
+++ b/tilda.c
-#include <glib.h>
+#include <stdlib.h>
+#include <signal.h>
 
-#include "tilda-window.h"
-#include "tilda-terminal.h"
+#include "tilda.h"
+#include "tilda-controller.h"
+#include "tomboykeybinder.h"
+
+/* Project-global variables */
+DBusGConnection *dbus_connection;
+
+static void
+tilda_dbus_init ()
+{
+       debug_enter  ();
+
+       static const gchar service_name[] = "net.sourceforge.Tilda";
+       GError *error = NULL;
+       DBusGProxy *driver_proxy;
+       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\n"), error->message);
+               g_error_free (error);
+               return;
+       }
+
+       // Register the service name
+       driver_proxy = dbus_g_proxy_new_for_name (dbus_connection,
+                                                                                         DBUS_SERVICE_DBUS,
+                                                                                         DBUS_PATH_DBUS,
+                                                                                         DBUS_INTERFACE_DBUS);
+
+       ret = org_freedesktop_DBus_request_name (driver_proxy,
+                                                                                        service_name,
+                                                                                        DBUS_NAME_FLAG_DO_NOT_QUEUE,
+                                                                                        &request_ret,
+                                                                                        &error);
+
+       if (!ret)
+       {
+               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 void
+tilda_parse_command_line (gint argc, gchar *argv[])
+{
+       debug_enter ();
+
+       gboolean version = FALSE;
+
+       /* 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 },
+       };
+
+       /* Set up the command-line parser */
+       GError *error = NULL;
+       GOptionContext *context = g_option_context_new (NULL);
+       g_option_context_add_main_entries (context, cl_opts, NULL);
+       g_option_context_add_group (context, gtk_get_option_group (TRUE));
+       g_option_context_parse (context, &argc, &argv, &error);
+       g_option_context_free (context);
+
+       /* Check for unknown options, and give a nice message if there are some */
+       if (error)
+       {
+               g_printerr (_("Error parsing command-line options: %s\n"), error->message);
+               g_printerr (_("The command \"tilda --help\" will show all possible options\n"));
+               g_error_free (error);
+               exit (EXIT_FAILURE);
+       }
+
+       /* If we need to show the version, show it then exit normally */
+       if (version)
+       {
+               g_print ("%s\n\n", TILDA_VERSION);
+
+               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");
+
+               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");
+
+               exit (EXIT_SUCCESS);
+       }
+}
+
+static void
+tilda_termination_handler (gint signum)
+{
+       debug_enter  ();
+       debug_printf ("signum: %d\n", signum);
+
+       gtk_main_quit ();
+}
+
+/* Hook up all system signal handlers */
+static void
+tilda_signal_handlers_init ()
+{
+       struct sigaction sa;
+
+       /* Hook up signal handlers */
+       sa.sa_handler = tilda_termination_handler;
+       sigemptyset (&sa.sa_mask);
+       sa.sa_flags = 0;
+
+       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[])
 {
-       TildaWindow *tw;
+       debug_enter ();
+
+       TildaController *tilda;
+
+#if ENABLE_NLS
+       /* Gettext Initialization */
+       setlocale (LC_ALL, "");
+       bindtextdomain (PACKAGE, LOCALEDIR);
+       textdomain (PACKAGE);
+#endif
+
+       /* Parse the command-line options */
+       tilda_parse_command_line (argc, argv);
 
        /* Initialize GTK+ (and the GObject system) */
        gtk_init (&argc, &argv);
 
-       /* 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 = g_object_new (TILDA_TYPE_WINDOW, "number", 0, NULL);
+       /* Hook up the signal handlers */
+       tilda_signal_handlers_init ();
+
+       /* Initialize the keybinder */
+       tomboy_keybinder_init ();
+
+       /* Start our connection to DBus */
+       tilda_dbus_init ();
+
+       /* 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");
 
-       g_object_unref (G_OBJECT (tw));
+       /* Unref the TildaController that controls this whole operation */
+       g_object_unref (G_OBJECT(tilda));
 
        return 0;
 }