Fix a few memory leaks in at-spi2-registryd
[platform/upstream/at-spi2-core.git] / registryd / registry-main.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  * Copyright 2001, 2002 Ximian, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <stdlib.h>
25 #include <config.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <stdio.h>
29 #include <dlfcn.h>
30
31 #include <dbus/dbus.h>
32 #include <dbus/dbus-glib.h>
33
34 #include "paths.h"
35 #include "registry.h"
36 #include "deviceeventcontroller.h"
37
38 #define CORBA_GCONF_KEY  "/desktop/gnome/interface/at-spi-corba"
39
40 static gboolean need_to_quit ();
41
42 static GMainLoop *mainloop;
43 static gchar *dbus_name = NULL;
44 static gboolean use_gnome_session = FALSE;
45
46 static GOptionEntry optentries[] =
47 {
48   {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
49   {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
50   {NULL}
51 };
52
53 static DBusGConnection *bus_connection = NULL;
54 static DBusGProxy      *sm_proxy = NULL;
55 static char            *client_id = NULL;
56 static DBusGProxy      *client_proxy = NULL;
57
58 #define SM_DBUS_NAME      "org.gnome.SessionManager"
59 #define SM_DBUS_PATH      "/org/gnome/SessionManager"
60 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
61
62 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
63
64 static void registry_session_init (const char *previous_client_id, const char *exe);
65
66 static gboolean
67 session_manager_connect (void)
68 {
69
70         if (bus_connection == NULL) {
71                 GError *error;
72
73                 error = NULL;
74                 bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
75                 if (bus_connection == NULL) {
76                         g_message ("Failed to connect to the session bus: %s",
77                                    error->message);
78                         g_error_free (error);
79                         exit (1);
80                 }
81         }
82
83         sm_proxy = dbus_g_proxy_new_for_name (bus_connection,
84                                               SM_DBUS_NAME,
85                                               SM_DBUS_PATH,
86                                               SM_DBUS_INTERFACE);
87         return (sm_proxy != NULL);
88 }
89
90 static void
91 stop_cb (gpointer data)
92 {
93         g_main_loop_quit (mainloop);
94 }
95
96 static gboolean
97 end_session_response (gboolean is_okay, const gchar *reason)
98 {
99         gboolean ret;
100         GError *error = NULL;
101
102         ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
103                                  &error,
104                                  G_TYPE_BOOLEAN, is_okay,
105                                  G_TYPE_STRING, reason,
106                                  G_TYPE_INVALID,
107                                  G_TYPE_INVALID);
108
109         if (!ret) {
110                 g_warning ("Failed to send session response %s", error->message);
111                 g_error_free (error);
112         }
113
114         return ret;
115 }
116
117 static void
118 query_end_session_cb (guint flags, gpointer data)
119 {
120         end_session_response (TRUE, NULL);
121 }
122
123 static void
124 end_session_cb (guint flags, gpointer data)
125 {
126         end_session_response (TRUE, NULL);
127         g_main_loop_quit (mainloop);
128 }
129 static gboolean
130 register_client (void)
131 {
132         GError     *error;
133         gboolean    res;
134         const char *startup_id;
135         const char *app_id;
136
137         startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
138         app_id = "at-spi-registryd.desktop";
139
140         error = NULL;
141         res = dbus_g_proxy_call (sm_proxy,
142                                  "RegisterClient",
143                                  &error,
144                                  G_TYPE_STRING, app_id,
145                                  G_TYPE_STRING, startup_id,
146                                  G_TYPE_INVALID,
147                                  DBUS_TYPE_G_OBJECT_PATH, &client_id,
148                                  G_TYPE_INVALID);
149         if (! res) {
150                 g_warning ("Failed to register client: %s", error->message);
151                 g_error_free (error);
152                 return FALSE;
153         }
154
155         client_proxy = dbus_g_proxy_new_for_name (bus_connection,
156                                                   SM_DBUS_NAME,
157                                                   client_id,
158                                                   SM_CLIENT_DBUS_INTERFACE);
159
160         dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
161         dbus_g_proxy_connect_signal (client_proxy, "Stop",
162                                      G_CALLBACK (stop_cb), NULL, NULL);
163
164         dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
165         dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
166                                      G_CALLBACK (query_end_session_cb), NULL, NULL);
167
168         dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
169         dbus_g_proxy_connect_signal (client_proxy, "EndSession",
170                                      G_CALLBACK (end_session_cb), NULL, NULL);
171
172         g_unsetenv ("DESKTOP_AUTOSTART_ID");
173
174         return TRUE;
175 }
176
177 /*---------------------------------------------------------------------------*/
178
179 /*
180  * Returns a 'canonicalized' value for DISPLAY,
181  * with the screen number stripped off if present.
182  *
183  */
184 static const gchar*
185 spi_display_name (void)
186 {
187     static const char *canonical_display_name = NULL;
188     if (!canonical_display_name)
189       {
190         const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
191         if (!display_env)
192           {
193             display_env = g_getenv ("DISPLAY");
194             if (!display_env || !display_env[0]) 
195                 canonical_display_name = ":0";
196             else
197               {
198                 gchar *display_p, *screen_p;
199                 canonical_display_name = g_strdup (display_env);
200                 display_p = strrchr (canonical_display_name, ':');
201                 screen_p = strrchr (canonical_display_name, '.');
202                 if (screen_p && display_p && (screen_p > display_p))
203                   {
204                     *screen_p = '\0';
205                   }
206               }
207           }
208         else
209           {
210             canonical_display_name = display_env;
211           }
212       }
213     return canonical_display_name;
214 }
215
216 /*---------------------------------------------------------------------------*/
217
218 /*
219  * Gets the IOR from the XDisplay.
220  * Not currently used in D-Bus version, but something similar
221  * may be employed in the future for accessing the registry daemon
222  * bus name.
223  */
224
225 static DBusConnection *
226 spi_get_bus (void)
227 {
228      Atom AT_SPI_BUS;
229      Atom actual_type;
230      Display *bridge_display;
231      int actual_format;
232      unsigned char *data = NULL;  
233      unsigned long nitems;
234      unsigned long leftover;
235
236      DBusConnection *bus = NULL;
237      DBusError       error;
238
239      bridge_display = XOpenDisplay (spi_display_name ());
240      if (!bridge_display)
241         g_error ("AT_SPI: Could not get the display");
242
243      AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", FALSE); 
244      XGetWindowProperty(bridge_display, 
245                         XDefaultRootWindow (bridge_display),
246                         AT_SPI_BUS, 0L,
247                         (long)BUFSIZ, False,
248                         (Atom) 31, &actual_type, &actual_format,
249                         &nitems, &leftover, &data);
250
251      dbus_error_init (&error);
252
253      if (data == NULL)
254      {
255          g_warning ("AT-SPI: Accessibility bus bus not found - Using session bus.\n");
256          bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
257          if (!bus)
258              g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
259      }
260      else
261      {
262          bus = dbus_connection_open (data, &error);
263          XFree (data);
264          if (!bus)
265          {
266              g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
267          }
268          else
269          {
270              if (!dbus_bus_register (bus, &error))
271                  g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
272          } 
273      }
274
275   XCloseDisplay (bridge_display);
276      return bus;
277 }
278
279 /*---------------------------------------------------------------------------*/
280
281 typedef GObject *(*gconf_client_get_default_t) ();
282 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
283
284 int
285 main (int argc, char **argv)
286 {
287   SpiRegistry *registry;
288   SpiDEController *dec;
289
290   DBusConnection *bus = NULL;
291
292   GOptionContext *opt;
293
294   GError *err = NULL;
295   DBusError error;
296   int ret;
297
298   if (need_to_quit ())
299     return 0;
300
301   g_type_init();
302
303   /*Parse command options*/
304   opt = g_option_context_new(NULL);
305   g_option_context_add_main_entries(opt, optentries, NULL);
306
307   if (!g_option_context_parse(opt, &argc, &argv, &err))
308       g_error("Option parsing failed: %s\n", err->message);
309
310   if (dbus_name == NULL)
311       dbus_name = SPI_DBUS_NAME_REGISTRY;
312
313   dbus_error_init (&error);
314   bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
315   bus = spi_get_bus ();
316   if (!bus)
317   {
318     return 0;
319   }
320
321   mainloop = g_main_loop_new (NULL, FALSE);
322   dbus_connection_setup_with_g_main(bus, NULL);
323
324   ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
325   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
326     {
327       g_error("Could not obtain D-Bus name - %s\n", dbus_name);
328     }
329   else
330     {
331       g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
332     }
333
334   registry = spi_registry_new (bus);
335   dec = spi_registry_dec_new (registry, bus);
336
337   if (use_gnome_session)
338     {
339       if (!session_manager_connect ())
340           g_warning ("Unable to connect to session manager");
341
342       if (!register_client ())
343           g_warning ("Unable to register client with session manager");
344     }
345
346   g_main_loop_run (mainloop);
347   return 0;
348 }
349
350 static gboolean
351 need_to_quit ()
352 {
353   void *gconf = NULL;
354   gconf_client_get_default_t gconf_client_get_default = NULL;
355   gconf_client_get_bool_t gconf_client_get_bool = NULL;
356   GObject *gconf_client;        /* really a GConfClient */
357   gboolean ret;
358
359   g_type_init ();
360
361   gconf = dlopen ("libgconf-2.so", RTLD_LAZY);
362   if (gconf)
363     {
364       gconf_client_get_default = dlsym (gconf, "gconf_client_get_default");
365       gconf_client_get_bool = dlsym (gconf, "gconf_client_get_bool");
366   }
367
368   if (!gconf_client_get_default || !gconf_client_get_bool)
369     {
370       if (gconf)
371         dlclose (gconf);
372       return FALSE;
373     }
374
375   /* If we've been relocated, we will exit if the at-spi-corba gconf key
376  * has been set.  If we have not been relocated, we will only run if the
377  * at-spi-dbus gconf key has been set.
378    */
379   gconf_client = gconf_client_get_default ();
380   ret = gconf_client_get_bool (gconf_client, CORBA_GCONF_KEY, NULL);
381   g_object_unref (gconf_client);
382
383   return ret;
384 }