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