+DBusGConnection *dbus_connection;
+GPtrArray *windows;
+
+static void
+tilda_initialize_dbus ()
+{
+ debug_enter ();
+
+ static const gchar service_name[] = "net.sourceforge.Tilda";
+ GError *error = NULL;
+ DBusGProxy *driver_proxy;
+ int request_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_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);
+
+ if (!org_freedesktop_DBus_request_name (driver_proxy, service_name, 0, &request_ret, &error))
+ {
+ // 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_error_free (error);
+ }
+
+ g_object_unref (driver_proxy);
+}
+
+static gint
+tilda_find_next_free_window_number ()
+{
+ debug_enter ();
+
+ gint i, j;
+ gboolean found;
+
+ for (i=0; i<INT_MAX; ++i)
+ {
+ found = FALSE;
+
+ for (j=0; j<windows->len; ++j)
+ {
+ TildaWindow *tw = g_ptr_array_index (windows, j);
+
+ if (tw->number == i)
+ {
+ found = TRUE;
+ break;
+ }
+ }
+
+ if (!found)
+ return i;
+ }
+
+ return 0;
+}
+
+static TildaWindow *
+tilda_add_window ()
+{
+ 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_printf ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, windows->len-1);
+ return ret;
+}
+
+static void
+tilda_del_window (gint number)
+{
+ debug_enter ();
+
+ gint i;
+ TildaWindow *win;
+
+ for (i=0; i<windows->len; ++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;
+ }
+ }
+}
+