Don't try to catch SIGKILL
[tilda-gobject.git] / tilda.c
diff --git a/tilda.c b/tilda.c
index f640eb1..02b6848 100644 (file)
--- a/tilda.c
+++ b/tilda.c
@@ -1,5 +1,6 @@
 #include <glib.h>
 #include <stdlib.h>
+#include <signal.h>
 
 #include "tilda.h"
 #include "tilda-window.h"
@@ -18,12 +19,13 @@ tilda_initialize_dbus ()
        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", error->message);
+               g_warning (_("Unable to connect to DBus: %s\n"), error->message);
                g_error_free (error);
                return;
        }
@@ -34,15 +36,24 @@ 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);
 }
 
@@ -93,7 +104,7 @@ tilda_add_window ()
        return ret;
 }
 
-static void
+void
 tilda_del_window (gint number)
 {
        debug_enter ();
@@ -110,6 +121,13 @@ tilda_del_window (gint 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));
+
+                       if (windows->len == 0)
+                       {
+                               debug_printf ("No windows left, exiting...\n");
+                               gtk_main_quit ();
+                       }
+
                        break;
                }
        }
@@ -161,6 +179,35 @@ tilda_parse_command_line (gint argc, gchar *argv[])
        }
 }
 
+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[])
 {
        debug_enter ();
@@ -180,6 +227,9 @@ int main (int argc, char *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 ();
 
@@ -200,6 +250,9 @@ int main (int argc, char *argv[])
        gtk_main ();
        debug_printf ("Out of gtk_main(), going down\n");
 
+       /* Free the pointer array we allocated earlier */
+       g_ptr_array_free (windows, TRUE);
+
        return 0;
 }