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