Fix missing NULL argument to g_strconcat.
[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 kill_accessibility_bus ()
92 {
93   FILE *fp;
94   const char *home;
95   char *name;
96   int pid;
97
98   home = getenv ("HOME");
99   if (!home)
100     return;
101   name = g_strconcat (home, "/", ".atspi-dbus-bus.pid", NULL);
102   if (!name)
103     return;
104
105   fp = fopen (name, "r");
106   if (fp)
107   {
108     if (fscanf (fp, "%d", &pid) == 1)
109     {
110       kill (&pid, SIGTERM);
111     }
112     fclose (fp);
113   }
114   g_free (name);
115 }
116
117 static void
118 stop_cb (gpointer data)
119 {
120         kill_accessibility_bus ();
121         g_main_loop_quit (mainloop);
122 }
123
124 static gboolean
125 end_session_response (gboolean is_okay, const gchar *reason)
126 {
127         gboolean ret;
128         GError *error = NULL;
129
130         ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse",
131                                  &error,
132                                  G_TYPE_BOOLEAN, is_okay,
133                                  G_TYPE_STRING, reason,
134                                  G_TYPE_INVALID,
135                                  G_TYPE_INVALID);
136
137         if (!ret) {
138                 g_warning ("Failed to send session response %s", error->message);
139                 g_error_free (error);
140         }
141
142         return ret;
143 }
144
145 static void
146 query_end_session_cb (guint flags, gpointer data)
147 {
148         end_session_response (TRUE, NULL);
149 }
150
151 static void
152 end_session_cb (guint flags, gpointer data)
153 {
154         kill_accessibility_bus ();
155         end_session_response (TRUE, NULL);
156         g_main_loop_quit (mainloop);
157 }
158 static gboolean
159 register_client (void)
160 {
161         GError     *error;
162         gboolean    res;
163         const char *startup_id;
164         const char *app_id;
165
166         startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
167         app_id = "at-spi-registryd.desktop";
168
169         error = NULL;
170         res = dbus_g_proxy_call (sm_proxy,
171                                  "RegisterClient",
172                                  &error,
173                                  G_TYPE_STRING, app_id,
174                                  G_TYPE_STRING, startup_id,
175                                  G_TYPE_INVALID,
176                                  DBUS_TYPE_G_OBJECT_PATH, &client_id,
177                                  G_TYPE_INVALID);
178         if (! res) {
179                 g_warning ("Failed to register client: %s", error->message);
180                 g_error_free (error);
181                 return FALSE;
182         }
183
184         client_proxy = dbus_g_proxy_new_for_name (bus_connection,
185                                                   SM_DBUS_NAME,
186                                                   client_id,
187                                                   SM_CLIENT_DBUS_INTERFACE);
188
189         dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID);
190         dbus_g_proxy_connect_signal (client_proxy, "Stop",
191                                      G_CALLBACK (stop_cb), NULL, NULL);
192
193         dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID);
194         dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession",
195                                      G_CALLBACK (query_end_session_cb), NULL, NULL);
196
197         dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID);
198         dbus_g_proxy_connect_signal (client_proxy, "EndSession",
199                                      G_CALLBACK (end_session_cb), NULL, NULL);
200
201         g_unsetenv ("DESKTOP_AUTOSTART_ID");
202
203         return TRUE;
204 }
205
206 /*---------------------------------------------------------------------------*/
207
208 /*
209  * Returns a 'canonicalized' value for DISPLAY,
210  * with the screen number stripped off if present.
211  *
212  */
213 static const gchar*
214 spi_display_name (void)
215 {
216     static const char *canonical_display_name = NULL;
217     if (!canonical_display_name)
218       {
219         const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
220         if (!display_env)
221           {
222             display_env = g_getenv ("DISPLAY");
223             if (!display_env || !display_env[0]) 
224                 canonical_display_name = ":0";
225             else
226               {
227                 gchar *display_p, *screen_p;
228                 canonical_display_name = g_strdup (display_env);
229                 display_p = strrchr (canonical_display_name, ':');
230                 screen_p = strrchr (canonical_display_name, '.');
231                 if (screen_p && display_p && (screen_p > display_p))
232                   {
233                     *screen_p = '\0';
234                   }
235               }
236           }
237         else
238           {
239             canonical_display_name = display_env;
240           }
241       }
242     return canonical_display_name;
243 }
244
245 /*---------------------------------------------------------------------------*/
246
247 /*
248  * Gets the IOR from the XDisplay.
249  * Not currently used in D-Bus version, but something similar
250  * may be employed in the future for accessing the registry daemon
251  * bus name.
252  */
253
254 static DBusConnection *
255 spi_get_bus (void)
256 {
257      Atom AT_SPI_BUS;
258      Atom actual_type;
259      Display *bridge_display;
260      int actual_format;
261      unsigned char *data = NULL;  
262      unsigned long nitems;
263      unsigned long leftover;
264
265      DBusConnection *bus = NULL;
266      DBusError       error;
267
268      bridge_display = XOpenDisplay (spi_display_name ());
269      if (!bridge_display)
270         g_error ("AT_SPI: Could not get the display");
271
272      AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", FALSE); 
273      XGetWindowProperty(bridge_display, 
274                         XDefaultRootWindow (bridge_display),
275                         AT_SPI_BUS, 0L,
276                         (long)BUFSIZ, False,
277                         (Atom) 31, &actual_type, &actual_format,
278                         &nitems, &leftover, &data);
279
280      dbus_error_init (&error);
281
282      if (data == NULL)
283      {
284          g_warning ("AT-SPI: Accessibility bus bus not found - Using session bus.\n");
285          bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
286          if (!bus)
287              g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
288      }
289      else
290      {
291          bus = dbus_connection_open (data, &error);
292          XFree (data);
293          if (!bus)
294          {
295              g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
296          }
297          else
298          {
299              if (!dbus_bus_register (bus, &error))
300                  g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
301          } 
302      }
303
304   XCloseDisplay (bridge_display);
305      return bus;
306 }
307
308 /*---------------------------------------------------------------------------*/
309
310 typedef GObject *(*gconf_client_get_default_t) ();
311 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
312
313 int
314 main (int argc, char **argv)
315 {
316   SpiRegistry *registry;
317   SpiDEController *dec;
318
319   DBusConnection *bus = NULL;
320
321   GOptionContext *opt;
322
323   GError *err = NULL;
324   DBusError error;
325   int ret;
326
327   if (need_to_quit ())
328     return 0;
329
330   g_type_init();
331
332   /*Parse command options*/
333   opt = g_option_context_new(NULL);
334   g_option_context_add_main_entries(opt, optentries, NULL);
335
336   if (!g_option_context_parse(opt, &argc, &argv, &err))
337       g_error("Option parsing failed: %s\n", err->message);
338
339   if (dbus_name == NULL)
340       dbus_name = SPI_DBUS_NAME_REGISTRY;
341
342   dbus_error_init (&error);
343   bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
344   bus = spi_get_bus ();
345   if (!bus)
346   {
347     return 0;
348   }
349
350   mainloop = g_main_loop_new (NULL, FALSE);
351   dbus_connection_setup_with_g_main(bus, NULL);
352
353   ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
354   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
355     {
356       g_error("Could not obtain D-Bus name - %s\n", dbus_name);
357     }
358   else
359     {
360       g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
361     }
362
363   registry = spi_registry_new (bus);
364   dec = spi_registry_dec_new (registry, bus);
365
366   if (use_gnome_session)
367     {
368       if (!session_manager_connect ())
369           g_warning ("Unable to connect to session manager");
370
371       if (!register_client ())
372           g_warning ("Unable to register client with session manager");
373     }
374
375   g_main_loop_run (mainloop);
376   return 0;
377 }
378
379 static gboolean
380 need_to_quit ()
381 {
382   void *gconf = NULL;
383   gconf_client_get_default_t gconf_client_get_default = NULL;
384   gconf_client_get_bool_t gconf_client_get_bool = NULL;
385   GObject *gconf_client;        /* really a GConfClient */
386   gboolean ret;
387
388   g_type_init ();
389
390   gconf = dlopen ("libgconf-2.so", RTLD_LAZY);
391   if (gconf)
392     {
393       gconf_client_get_default = dlsym (gconf, "gconf_client_get_default");
394       gconf_client_get_bool = dlsym (gconf, "gconf_client_get_bool");
395   }
396
397   if (!gconf_client_get_default || !gconf_client_get_bool)
398     {
399       if (gconf)
400         dlclose (gconf);
401       return FALSE;
402     }
403
404   /* If we've been relocated, we will exit if the at-spi-corba gconf key
405  * has been set.  If we have not been relocated, we will only run if the
406  * at-spi-dbus gconf key has been set.
407    */
408   gconf_client = gconf_client_get_default ();
409   ret = gconf_client_get_bool (gconf_client, CORBA_GCONF_KEY, NULL);
410   g_object_unref (gconf_client);
411
412   return ret;
413 }