Don't leak errors
[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 <gio/gio.h>
29 #include <stdio.h>
30 #include <dlfcn.h>
31
32 #include <dbus/dbus.h>
33
34 #include "paths.h"
35 #include "registry.h"
36 #include "deviceeventcontroller.h"
37 #include "atspi/atspi.h"
38
39 static GMainLoop *mainloop;
40 static gchar *dbus_name = NULL;
41 static gboolean use_gnome_session = FALSE;
42
43 static GOptionEntry optentries[] =
44 {
45   {"dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL},
46   {"use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL},
47   {NULL}
48 };
49
50 static GDBusConnection *bus_connection = NULL;
51 static GDBusProxy      *sm_proxy = NULL;
52 static char            *client_id = NULL;
53 static GDBusProxy      *client_proxy = NULL;
54
55 #define SM_DBUS_NAME      "org.gnome.SessionManager"
56 #define SM_DBUS_PATH      "/org/gnome/SessionManager"
57 #define SM_DBUS_INTERFACE "org.gnome.SessionManager"
58
59 #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
60
61 static void registry_session_init (const char *previous_client_id, const char *exe);
62
63 static void
64 on_session_over (GDBusProxy *proxy,
65                  gchar      *sender_name,
66                  gchar      *signal_name,
67                  GVariant   *parameters,
68                  gpointer    user_data)
69 {
70         if (g_strcmp0 (signal_name, "SessionOver") == 0) {
71                 g_main_loop_quit (mainloop);
72         }
73 }
74
75 static gboolean
76 session_manager_connect (void)
77 {
78
79         sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
80                                               SM_DBUS_NAME,
81                                               SM_DBUS_PATH,
82                                               SM_DBUS_INTERFACE, NULL, NULL);
83
84         g_signal_connect (G_OBJECT (sm_proxy), "g-signal",
85                           G_CALLBACK (on_session_over), NULL);
86
87         return (sm_proxy != NULL);
88 }
89
90 static void
91 stop_cb (gpointer data)
92 {
93         g_main_loop_quit (mainloop);
94 }
95
96 static gboolean
97 end_session_response (gboolean is_okay, const gchar *reason)
98 {
99   GVariant *ret;
100         GError *error = NULL;
101
102         if (!reason)
103                 reason = "";
104
105         ret = g_dbus_proxy_call_sync (client_proxy, "EndSessionResponse",
106                                       g_variant_new ("(bs)", is_okay, reason),
107                                       0, 1000, NULL, &error);
108
109         if (!ret) {
110                 g_warning ("Failed to send session response %s", error->message);
111                 g_error_free (error);
112                 return FALSE;
113         }
114         else
115                 g_variant_unref (ret);
116
117         return TRUE;
118 }
119
120 static void
121 client_proxy_signal_cb (GDBusProxy *proxy,
122                         gchar *sender_name,
123                         gchar *signal_name,
124                         GVariant *parameters,
125                         gpointer user_data)
126 {
127         if (g_strcmp0 (signal_name, "QueryEndSession") == 0) {
128                 g_debug ("Got QueryEndSession signal");
129                 end_session_response (TRUE, NULL);
130         } else if (g_strcmp0 (signal_name, "EndSession") == 0) {
131                 g_debug ("Got EndSession signal");
132                 end_session_response (TRUE, NULL);
133                 g_main_loop_quit (mainloop);
134         } else if (g_strcmp0 (signal_name, "Stop") == 0) {
135                 g_debug ("Got Stop signal");
136                 g_main_loop_quit (mainloop);
137         }
138 }
139
140 static gboolean
141 register_client (void)
142 {
143         GError     *error;
144   GVariant *res;
145         const char *startup_id;
146         const char *app_id;
147
148         startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
149         if (!startup_id)
150                 startup_id = "";
151         app_id = "at-spi-registryd.desktop";
152
153         error = NULL;
154         res = g_dbus_proxy_call_sync (sm_proxy,
155                                  "RegisterClient",
156                                       g_variant_new ("(ss)", app_id,
157                                                      startup_id),
158                                       0, 1000, NULL, &error);
159         if (! res) {
160                 g_warning ("Failed to register client: %s", error->message);
161                 g_error_free (error);
162                 return FALSE;
163         }
164         g_variant_get (res, "(o)", &client_id);
165         g_variant_unref (res);
166
167         client_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
168                                                   SM_DBUS_NAME,
169                                                   client_id,
170                                                   SM_CLIENT_DBUS_INTERFACE,
171                                                   NULL, NULL);
172
173         g_signal_connect (client_proxy, "g-signal",
174                           G_CALLBACK (client_proxy_signal_cb), NULL);
175
176         g_unsetenv ("DESKTOP_AUTOSTART_ID");
177
178         return TRUE;
179 }
180
181 /*---------------------------------------------------------------------------*/
182
183
184 /*---------------------------------------------------------------------------*/
185
186 typedef GObject *(*gconf_client_get_default_t) ();
187 typedef gboolean (*gconf_client_get_bool_t)(GObject *, const char *, void *);
188
189 int
190 main (int argc, char **argv)
191 {
192   SpiRegistry *registry;
193   SpiDEController *dec;
194
195   DBusConnection *bus = NULL;
196
197   GOptionContext *opt;
198
199   GError *err = NULL;
200   int ret;
201
202   g_type_init();
203
204   /*Parse command options*/
205   opt = g_option_context_new(NULL);
206   g_option_context_add_main_entries(opt, optentries, NULL);
207
208   if (!g_option_context_parse(opt, &argc, &argv, &err))
209     {
210       g_error("Option parsing failed: %s\n", err->message);
211       g_clear_error (&err);
212     }
213
214   if (dbus_name == NULL)
215       dbus_name = SPI_DBUS_NAME_REGISTRY;
216
217   bus = atspi_get_a11y_bus ();
218   if (!bus)
219   {
220     return 0;
221   }
222
223   mainloop = g_main_loop_new (NULL, FALSE);
224   atspi_dbus_connection_setup_with_g_main(bus, NULL);
225
226   ret = dbus_bus_request_name(bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
227   if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
228     {
229       exit (0); /* most likely already running */
230     }
231   else
232     {
233       g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
234     }
235
236   registry = spi_registry_new (bus);
237   dec = spi_registry_dec_new (registry, bus);
238
239   if (use_gnome_session)
240     {
241       if (!session_manager_connect ())
242           g_warning ("Unable to connect to session manager");
243
244       if (!register_client ())
245           g_warning ("Unable to register client with session manager");
246     }
247
248   g_main_loop_run (mainloop);
249   return 0;
250 }