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