summaryrefslogtreecommitdiff
path: root/registryd/registry-main.c
blob: 7619c86aac58e08738b46cd90503559aeb6bf04e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * AT-SPI - Assistive Technology Service Provider Interface
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
 *
 * Copyright 2001, 2002 Sun Microsystems Inc.,
 * Copyright 2001, 2002 Ximian, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <dlfcn.h>
#include <gio/gio.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dbus/dbus.h>

#include "atspi/atspi.h"
#include "deviceeventcontroller.h"
#include "paths.h"
#include "registry.h"

static GMainLoop *mainloop;
static gchar *dbus_name = NULL;
static gboolean use_gnome_session = FALSE;

static GOptionEntry optentries[] = {
  { "dbus-name", 0, 0, G_OPTION_ARG_STRING, &dbus_name, "Well-known name to register with D-Bus", NULL },
  { "use-gnome-session", 0, 0, G_OPTION_ARG_NONE, &use_gnome_session, "Should register with gnome session manager", NULL },
  { NULL }
};

static GDBusProxy *sm_proxy = NULL;
static char *client_id = NULL;
static GDBusProxy *client_proxy = NULL;

#define SM_DBUS_NAME "org.gnome.SessionManager"
#define SM_DBUS_PATH "/org/gnome/SessionManager"
#define SM_DBUS_INTERFACE "org.gnome.SessionManager"

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

static gboolean register_client (void);

static void
on_session_signal (GDBusProxy *proxy,
                   gchar *sender_name,
                   gchar *signal_name,
                   GVariant *parameters,
                   gpointer user_data)
{
  if (g_strcmp0 (signal_name, "SessionOver") == 0)
    {
      g_main_loop_quit (mainloop);
    }
  else if (g_strcmp0 (signal_name, "SessionRunning") == 0)
    {
      if (!register_client ())
        g_warning ("Unable to register client with session manager");
    }
}

static gboolean
session_manager_connect (void)
{
  GVariant *res;
  gboolean is_running;

  sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
                                            SM_DBUS_NAME,
                                            SM_DBUS_PATH,
                                            SM_DBUS_INTERFACE, NULL, NULL);

  res = g_dbus_proxy_call_sync (sm_proxy,
                                "IsSessionRunning", NULL,
                                0, 1000, NULL, NULL);

  if (res)
    {
      g_variant_get (res, "(b)", &is_running);
      g_variant_unref (res);
      if (is_running)
        {
          if (!register_client ())
            g_warning ("Unable to register client with session manager");
        }
    }

  g_signal_connect (G_OBJECT (sm_proxy), "g-signal",
                    G_CALLBACK (on_session_signal), NULL);

  return (sm_proxy != NULL);
}

static gboolean
end_session_response (gboolean is_okay, const gchar *reason)
{
  GVariant *ret;
  GError *error = NULL;

  if (!reason)
    reason = "";

  ret = g_dbus_proxy_call_sync (client_proxy, "EndSessionResponse",
                                g_variant_new ("(bs)", is_okay, reason),
                                0, 1000, NULL, &error);

  if (!ret)
    {
      g_warning ("Failed to send session response %s", error->message);
      g_error_free (error);
      return FALSE;
    }
  else
    g_variant_unref (ret);

  return TRUE;
}

static void
client_proxy_signal_cb (GDBusProxy *proxy,
                        gchar *sender_name,
                        gchar *signal_name,
                        GVariant *parameters,
                        gpointer user_data)
{
  if (g_strcmp0 (signal_name, "QueryEndSession") == 0)
    {
      g_debug ("Got QueryEndSession signal");
      end_session_response (TRUE, NULL);
    }
  else if (g_strcmp0 (signal_name, "EndSession") == 0)
    {
      g_debug ("Got EndSession signal");
      end_session_response (TRUE, NULL);
      g_main_loop_quit (mainloop);
    }
  else if (g_strcmp0 (signal_name, "Stop") == 0)
    {
      g_debug ("Got Stop signal");
      g_main_loop_quit (mainloop);
    }
}

static gboolean
register_client (void)
{
  GError *error;
  GVariant *res;
  const char *startup_id;
  const char *app_id;

  if (client_proxy)
    return TRUE;

  startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
  if (!startup_id)
    startup_id = "";
  app_id = "at-spi-registryd.desktop";

  error = NULL;
  res = g_dbus_proxy_call_sync (sm_proxy,
                                "RegisterClient",
                                g_variant_new ("(ss)", app_id,
                                               startup_id),
                                0, 1000, NULL, &error);
  if (!res)
    {
      const char *message = (error && error->message ? error->message
                                                     : "no error");
      g_warning ("Failed to register client: %s", message);
      if (error)
        g_error_free (error);
      return FALSE;
    }
  g_variant_get (res, "(o)", &client_id);
  g_variant_unref (res);

  client_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION, 0, NULL,
                                                SM_DBUS_NAME,
                                                client_id,
                                                SM_CLIENT_DBUS_INTERFACE,
                                                NULL, NULL);

  g_signal_connect (client_proxy, "g-signal",
                    G_CALLBACK (client_proxy_signal_cb), NULL);

  g_unsetenv ("DESKTOP_AUTOSTART_ID");

  return TRUE;
}

/*---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*/

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

int
main (int argc, char **argv)
{
  SpiRegistry *registry;
  SpiDEController *dec;

  DBusConnection *bus = NULL;

  GOptionContext *opt;

  GError *err = NULL;
  int ret;

  /*Parse command options*/
  opt = g_option_context_new (NULL);
  g_option_context_add_main_entries (opt, optentries, NULL);

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

  if (dbus_name == NULL)
    dbus_name = SPI_DBUS_NAME_REGISTRY;

  bus = atspi_get_a11y_bus ();
  if (!bus)
    {
      return 0;
    }

  mainloop = g_main_loop_new (NULL, FALSE);
  atspi_dbus_connection_setup_with_g_main (bus, NULL);

  ret = dbus_bus_request_name (bus, dbus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE, NULL);
  if (ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
    {
      exit (0); /* most likely already running */
    }
  else
    {
      g_print ("SpiRegistry daemon is running with well-known name - %s\n", dbus_name);
    }

  dec = spi_registry_dec_new (bus);
  registry = spi_registry_new (bus, dec);

  if (use_gnome_session)
    {
      if (!session_manager_connect ())
        g_warning ("Unable to connect to session manager");
    }

  g_main_loop_run (mainloop);

  dbus_connection_close (bus);
  dbus_connection_unref (bus);
  g_object_unref (dec);
  g_object_unref (registry);

  return 0;
}