Add debugging macros + use them
[tilda-gobject.git] / tilda.c
1 #include <glib.h>
2
3 #include "tilda.h"
4 #include "tilda-window.h"
5 #include "tilda-terminal.h"
6
7 DBusGConnection *dbus_connection;
8 GPtrArray *windows;
9
10 static void
11 tilda_initialize_dbus ()
12 {
13         debug_enter  ();
14
15         static const gchar service_name[] = "net.sourceforge.Tilda";
16         GError *error = NULL;
17         DBusGProxy *driver_proxy;
18         int request_ret;
19
20         // Initialize the DBus connection
21         dbus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
22         if (dbus_connection == NULL)
23         {
24                 g_warning ("Unable to connect to dbus: %s", error->message);
25                 g_error_free (error);
26                 return;
27         }
28
29         // Register the service name
30         driver_proxy = dbus_g_proxy_new_for_name (dbus_connection,
31                                                                                           DBUS_SERVICE_DBUS,
32                                                                                           DBUS_PATH_DBUS,
33                                                                                           DBUS_INTERFACE_DBUS);
34
35         if (!org_freedesktop_DBus_request_name (driver_proxy, service_name, 0, &request_ret, &error))
36         {
37                 // FIXME: for whatever reason, this is wrong. The error message doesn't appear
38                 // FIXME: when we were unable to register the service. Perhaps there's a more
39                 // FIXME: GLib-y way of doing this?
40                 g_warning ("Unable to register service: %s", error->message);
41                 g_error_free (error);
42         }
43
44         g_object_unref (driver_proxy);
45 }
46
47 static gint
48 tilda_find_next_free_window_number ()
49 {
50         debug_enter  ();
51
52         gint i, j;
53         gboolean found;
54
55         for (i=0; i<INT_MAX; ++i)
56         {
57                 found = FALSE;
58
59                 for (j=0; j<windows->len; ++j)
60                 {
61                         TildaWindow *tw = g_ptr_array_index (windows, j);
62
63                         if (tw->number == i)
64                         {
65                                 found = TRUE;
66                                 break;
67                         }
68                 }
69
70                 if (!found)
71                         return i;
72         }
73
74         return 0;
75 }
76
77 static TildaWindow *
78 tilda_add_window ()
79 {
80         debug_enter ();
81
82         TildaWindow *ret;
83         gint number;
84
85         number = tilda_find_next_free_window_number ();
86         ret = g_object_new (TILDA_TYPE_WINDOW, "number", number, NULL);
87
88         g_ptr_array_add (windows, ret);
89
90         debug_printf ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, windows->len-1);
91         return ret;
92 }
93
94 static void
95 tilda_del_window (gint number)
96 {
97         debug_enter ();
98
99         gint i;
100         TildaWindow *win;
101
102         for (i=0; i<windows->len; ++i)
103         {
104                 win = g_ptr_array_index (windows, i);
105
106                 if (win->number == number)
107                 {
108                         debug_printf ("Deleting window 0x%x (number %d of %d)\n", win, win->number, windows->len-1);
109                         g_ptr_array_remove_index (windows, i);
110                         g_object_unref (G_OBJECT(win));
111                         break;
112                 }
113         }
114 }
115
116 int main (int argc, char *argv[])
117 {
118         debug_enter ();
119
120         TildaWindow *tw;
121
122         /* Initialize GTK+ (and the GObject system) */
123         gtk_init (&argc, &argv);
124
125         /* Initialize the keybinder */
126         tomboy_keybinder_init ();
127
128         /* Start our connection to DBus */
129         tilda_initialize_dbus ();
130
131         /* Initialize the array of windows */
132         windows = g_ptr_array_new ();
133
134         /* Create a TildaWindow, run it, and exit when it does, basically.
135          *
136          * This is nothing like what the real main() will be, but it's
137          * a good start for testing and integration of more of TildaWindow
138          * and TildaTerminal. */
139         tw = tilda_add_window ();
140
141         debug_printf ("Starting gtk_main()!\n");
142         gtk_main ();
143         debug_printf ("Out of gtk_main(), going down\n");
144
145         return 0;
146 }
147
148 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */