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