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