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 Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 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
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the
20
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
 * Boston, MA 02110-1301, USA.
22
 */
23

            
24
#include <config.h>
25
#include <dlfcn.h>
26
#include <gio/gio.h>
27
#include <glib.h>
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31

            
32
#include <dbus/dbus.h>
33

            
34
#include "atspi/atspi.h"
35
#include "deviceeventcontroller.h"
36
#include "paths.h"
37
#include "registry.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
  { "dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL },
45
  { "use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL },
46
  { NULL }
47
};
48

            
49
static GDBusProxy *sm_proxy = NULL;
50
static char *client_id = NULL;
51
static GDBusProxy *client_proxy = NULL;
52

            
53
#define SM_DBUS_NAME "org.gnome.SessionManager"
54
#define SM_DBUS_PATH "/org/gnome/SessionManager"
55
#define SM_DBUS_INTERFACE "org.gnome.SessionManager"
56

            
57
#define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate"
58

            
59
static gboolean register_client (void);
60

            
61
static void
62
on_session_signal (GDBusProxy *proxy,
63
                   gchar *sender_name,
64
                   gchar *signal_name,
65
                   GVariant *parameters,
66
                   gpointer user_data)
67
{
68
  if (g_strcmp0 (signal_name, "SessionOver") == 0)
69
    {
70
      g_main_loop_quit (mainloop);
71
    }
72
  else if (g_strcmp0 (signal_name, "SessionRunning") == 0)
73
    {
74
      if (!register_client ())
75
        g_warning ("Unable to register client with session manager");
76
    }
77
}
78

            
79
static gboolean
80
28
session_manager_connect (void)
81
{
82
  GVariant *res;
83
  gboolean is_running;
84

            
85
28
  sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
86
                                            SM_DBUS_NAME,
87
                                            SM_DBUS_PATH,
88
                                            SM_DBUS_INTERFACE, NULL, NULL);
89

            
90
28
  res = g_dbus_proxy_call_sync (sm_proxy,
91
                                "IsSessionRunning", NULL,
92
                                0, 1000, NULL, NULL);
93

            
94
28
  if (res)
95
    {
96
28
      g_variant_get (res, "(b)", &is_running);
97
28
      g_variant_unref (res);
98
28
      if (is_running)
99
        {
100
28
          if (!register_client ())
101
            g_warning ("Unable to register client with session manager");
102
        }
103
    }
104

            
105
28
  g_signal_connect (G_OBJECT (sm_proxy), "g-signal",
106
                    G_CALLBACK (on_session_signal), NULL);
107

            
108
28
  return (sm_proxy != NULL);
109
}
110

            
111
static gboolean
112
28
end_session_response (gboolean is_okay, const gchar *reason)
113
{
114
  GVariant *ret;
115
28
  GError *error = NULL;
116

            
117
28
  if (!reason)
118
28
    reason = "";
119

            
120
28
  ret = g_dbus_proxy_call_sync (client_proxy, "EndSessionResponse",
121
                                g_variant_new ("(bs)", is_okay, reason),
122
                                0, 1000, NULL, &error);
123

            
124
28
  if (!ret)
125
    {
126
1
      g_warning ("Failed to send session response %s", error->message);
127
1
      g_error_free (error);
128
1
      return FALSE;
129
    }
130
  else
131
27
    g_variant_unref (ret);
132

            
133
27
  return TRUE;
134
}
135

            
136
static void
137
35
client_proxy_signal_cb (GDBusProxy *proxy,
138
                        gchar *sender_name,
139
                        gchar *signal_name,
140
                        GVariant *parameters,
141
                        gpointer user_data)
142
{
143
35
  if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
144
    {
145
      g_debug ("Got QueryEndSession signal");
146
      end_session_response (TRUE, NULL);
147
    }
148
35
  else if (g_strcmp0 (signal_name, "EndSession") == 0)
149
    {
150
28
      g_debug ("Got EndSession signal");
151
28
      end_session_response (TRUE, NULL);
152
28
      g_main_loop_quit (mainloop);
153
    }
154
7
  else if (g_strcmp0 (signal_name, "Stop") == 0)
155
    {
156
7
      g_debug ("Got Stop signal");
157
7
      g_main_loop_quit (mainloop);
158
    }
159
35
}
160

            
161
static gboolean
162
28
register_client (void)
163
{
164
  GError *error;
165
  GVariant *res;
166
  const char *startup_id;
167
  const char *app_id;
168

            
169
28
  if (client_proxy)
170
    return TRUE;
171

            
172
28
  startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
173
28
  if (!startup_id)
174
28
    startup_id = "";
175
28
  app_id = "at-spi-registryd.desktop";
176

            
177
28
  error = NULL;
178
28
  res = g_dbus_proxy_call_sync (sm_proxy,
179
                                "RegisterClient",
180
                                g_variant_new ("(ss)", app_id,
181
                                               startup_id),
182
                                0, 1000, NULL, &error);
183
28
  if (!res)
184
    {
185
      const char *message = (error && error->message ? error->message
186
                                                     : "no error");
187
      g_warning ("Failed to register client: %s", message);
188
      if (error)
189
        g_error_free (error);
190
      return FALSE;
191
    }
192
28
  g_variant_get (res, "(o)", &client_id);
193
28
  g_variant_unref (res);
194

            
195
28
  client_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
196
                                                SM_DBUS_NAME,
197
                                                client_id,
198
                                                SM_CLIENT_DBUS_INTERFACE,
199
                                                NULL, NULL);
200

            
201
28
  g_signal_connect (client_proxy, "g-signal",
202
                    G_CALLBACK (client_proxy_signal_cb), NULL);
203

            
204
28
  g_unsetenv ("DESKTOP_AUTOSTART_ID");
205

            
206
28
  return TRUE;
207
}
208

            
209
/*---------------------------------------------------------------------------*/
210

            
211
/*---------------------------------------------------------------------------*/
212

            
213
typedef GObject *(*gconf_client_get_default_t) ();
214
typedef gboolean (*gconf_client_get_bool_t) (GObject *, const char *, void *);
215

            
216
int
217
28
main (int argc, char **argv)
218
{
219
  SpiRegistry *registry;
220
  SpiDEController *dec;
221

            
222
28
  DBusConnection *bus = NULL;
223

            
224
  GOptionContext *opt;
225

            
226
28
  GError *err = NULL;
227
  int ret;
228

            
229
  /*Parse command options*/
230
28
  opt = g_option_context_new (NULL);
231
28
  g_option_context_add_main_entries (opt, optentries, NULL);
232

            
233
28
  if (!g_option_context_parse (opt, &argc, &argv, &err))
234
    {
235
      g_error ("Option parsing failed: %s\n", err->message);
236
      g_clear_error (&err);
237
    }
238

            
239
28
  if (dbus_name == NULL)
240
28
    dbus_name = SPI_DBUS_NAME_REGISTRY;
241

            
242
28
  bus = atspi_get_a11y_bus ();
243
28
  if (!bus)
244
    {
245
      return 0;
246
    }
247

            
248
28
  mainloop = g_main_loop_new (NULL, FALSE);
249
28
  atspi_dbus_connection_setup_with_g_main (bus, NULL);
250

            
251
28
  ret = dbus_bus_request_name (bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
252
28
  if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
253
    {
254
      exit (0); /* most likely already running */
255
    }
256
  else
257
    {
258
28
      g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
259
    }
260

            
261
28
  dec = spi_registry_dec_new (bus);
262
28
  registry = spi_registry_new (bus, dec);
263

            
264
28
  if (use_gnome_session)
265
    {
266
28
      if (!session_manager_connect ())
267
        g_warning ("Unable to connect to session manager");
268
    }
269

            
270
28
  g_main_loop_run (mainloop);
271

            
272
28
  dbus_connection_close (bus);
273
28
  dbus_connection_unref (bus);
274
28
  g_object_unref (dec);
275
28
  g_object_unref (registry);
276

            
277
28
  return 0;
278
}