1
/*
2
 * AT-SPI - Assistive Technology Service Provider Interface
3
 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4
 *
5
 * Copyright 2008, 2009 Codethink Ltd.
6
 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
7
 * Copyright 2001, 2002, 2003 Ximian, Inc.
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with this library; if not, write to the
21
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22
 * Boston, MA 02110-1301, USA.
23
 */
24

            
25
#include "config.h"
26

            
27
#include <atk/atk.h>
28
#include <stdarg.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include <strings.h>
33
#include <sys/stat.h>
34
#include <unistd.h>
35

            
36
#include <atk-bridge.h>
37
#include <atspi/atspi.h>
38
#include <droute/droute.h>
39

            
40
#include "accessible-stateset.h"
41
#include "adaptors.h"
42
#include "bridge.h"
43
#include "event.h"
44
#include "object.h"
45

            
46
#include "accessible-cache.h"
47
#include "accessible-leasing.h"
48
#include "accessible-register.h"
49

            
50
#include "spi-dbus.h"
51

            
52
/*---------------------------------------------------------------------------*/
53

            
54
static DBusHandlerResult
55
signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data);
56

            
57
SpiBridge *spi_global_app_data = NULL;
58

            
59
static gboolean inited = FALSE;
60
static gboolean atexit_added = FALSE;
61

            
62
/*---------------------------------------------------------------------------*/
63

            
64
static event_data *
65
161
add_event (const char *bus_name, const char *event)
66
{
67
  event_data *evdata;
68
  gchar **data;
69

            
70
161
  spi_atk_add_client (bus_name);
71
161
  evdata = g_new0 (event_data, 1);
72
161
  data = g_strsplit (event, ":", 3);
73
161
  if (!data)
74
    {
75
      g_free (evdata);
76
      return NULL;
77
    }
78
161
  evdata->bus_name = g_strdup (bus_name);
79
161
  evdata->data = data;
80
161
  spi_global_app_data->events = g_list_append (spi_global_app_data->events, evdata);
81
161
  return evdata;
82
}
83

            
84
static GSList *clients = NULL;
85

            
86
static void
87
322
tally_event_reply ()
88
{
89
322
  if (!spi_global_app_data)
90
    return;
91

            
92
322
  spi_global_app_data->replies_received++;
93
322
  if (spi_global_app_data->replies_received == 3)
94
    {
95
      if (!clients)
96
        spi_atk_deregister_event_listeners ();
97
      spi_global_app_data->events_initialized = TRUE;
98
    }
99
}
100

            
101
GType
102
_atk_bridge_type_from_iface (const char *iface)
103
{
104
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_ACCESSIBLE))
105
    return ATK_TYPE_OBJECT;
106
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_ACTION))
107
    return ATK_TYPE_ACTION;
108
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_COMPONENT))
109
    return ATK_TYPE_COMPONENT;
110
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_DOCUMENT))
111
    return ATK_TYPE_DOCUMENT;
112
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_HYPERTEXT))
113
    return ATK_TYPE_HYPERTEXT;
114
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_HYPERLINK))
115
    return ATK_TYPE_HYPERLINK;
116
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_IMAGE))
117
    return ATK_TYPE_IMAGE;
118
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_SELECTION))
119
    return ATK_TYPE_SELECTION;
120
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_TABLE))
121
    return ATK_TYPE_TABLE;
122
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_TEXT))
123
    return ATK_TYPE_TEXT;
124
  if (!strcmp (iface, ATSPI_DBUS_INTERFACE_VALUE))
125
    return ATK_TYPE_VALUE;
126
  return 0;
127
}
128

            
129
DRoutePropertyFunction
130
_atk_bridge_find_property_func (const char *property, GType *type)
131
{
132
  const char *iface;
133
  const char *member;
134
  DRouteProperty *dp;
135

            
136
  if (!strncasecmp (property, "action.", 7))
137
    {
138
      iface = ATSPI_DBUS_INTERFACE_ACTION;
139
      member = property + 7;
140
    }
141
  else if (!strncasecmp (property, "component.", 10))
142
    {
143
      iface = ATSPI_DBUS_INTERFACE_COMPONENT;
144
      member = property + 10;
145
    }
146
  else if (!strncasecmp (property, "selection.", 10))
147
    {
148
      iface = ATSPI_DBUS_INTERFACE_SELECTION;
149
      member = property + 10;
150
    }
151
  else if (!strncasecmp (property, "table.", 6))
152
    {
153
      iface = ATSPI_DBUS_INTERFACE_TABLE;
154
      member = property + 6;
155
    }
156
  else if (!strncasecmp (property, "text.", 5))
157
    {
158
      iface = ATSPI_DBUS_INTERFACE_TEXT;
159
      member = property + 5;
160
    }
161
  else if (!strncasecmp (property, "value.", 6))
162
    {
163
      iface = ATSPI_DBUS_INTERFACE_VALUE;
164
      member = property + 6;
165
    }
166
  else
167
    {
168
      iface = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
169
      member = property;
170
    }
171

            
172
  *type = _atk_bridge_type_from_iface (iface);
173

            
174
  dp = g_hash_table_lookup (spi_global_app_data->property_hash, iface);
175

            
176
  if (!dp)
177
    return NULL;
178

            
179
  for (; dp->name; dp++)
180
    {
181
      if (!strcasecmp (dp->name, member))
182
        {
183
          return dp->get;
184
        }
185
    }
186
  return NULL;
187
}
188

            
189
static void
190
add_property_to_event (event_data *evdata, const char *property)
191
{
192
  AtspiPropertyDefinition *prop = g_new0 (AtspiPropertyDefinition, 1);
193
  prop->func = _atk_bridge_find_property_func (property, &prop->type);
194
  if (!prop->func)
195
    {
196
      g_warning ("atk-bridge: Request for unknown property '%s'", property);
197
      g_free (prop);
198
      return;
199
    }
200

            
201
  prop->name = g_strdup (property);
202
  evdata->properties = g_slist_append (evdata->properties, prop);
203
}
204

            
205
static void
206
161
add_event_from_iter (DBusMessageIter *iter)
207
{
208
  const char *bus_name, *event;
209
  event_data *evdata;
210

            
211
161
  dbus_message_iter_get_basic (iter, &bus_name);
212
161
  dbus_message_iter_next (iter);
213
161
  dbus_message_iter_get_basic (iter, &event);
214
161
  dbus_message_iter_next (iter);
215
161
  evdata = add_event (bus_name, event);
216
161
  if (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY)
217
    {
218
      DBusMessageIter iter_sub_array;
219
      dbus_message_iter_recurse (iter, &iter_sub_array);
220
      while (dbus_message_iter_get_arg_type (&iter_sub_array) != DBUS_TYPE_INVALID)
221
        {
222
          const char *property;
223
          dbus_message_iter_get_basic (&iter_sub_array, &property);
224
          add_property_to_event (evdata, property);
225
          dbus_message_iter_next (&iter_sub_array);
226
        }
227
    }
228
161
}
229

            
230
static void
231
161
get_events_reply (DBusPendingCall *pending, void *user_data)
232
{
233
161
  DBusMessage *reply = dbus_pending_call_steal_reply (pending);
234
  DBusMessageIter iter, iter_array, iter_struct;
235

            
236
161
  if (!reply || !spi_global_app_data)
237
    goto done;
238

            
239
161
  if (strcmp (dbus_message_get_signature (reply), "a(ss)") != 0 &&
240
      strcmp (dbus_message_get_signature (reply), "a(ssas)") != 0)
241
    {
242
      g_warning ("atk-bridge: GetRegisteredEvents returned message with unknown signature");
243
      goto done;
244
    }
245

            
246
161
  dbus_message_iter_init (reply, &iter);
247
161
  dbus_message_iter_recurse (&iter, &iter_array);
248
322
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
249
    {
250
161
      dbus_message_iter_recurse (&iter_array, &iter_struct);
251
161
      add_event_from_iter (&iter_struct);
252
161
      dbus_message_iter_next (&iter_array);
253
    }
254

            
255
161
done:
256
161
  if (reply)
257
161
    dbus_message_unref (reply);
258
161
  if (pending)
259
161
    dbus_pending_call_unref (pending);
260

            
261
161
  tally_event_reply ();
262
161
}
263

            
264
static void
265
161
get_device_events_reply (DBusPendingCall *pending, void *user_data)
266
{
267
161
  DBusMessage *reply = dbus_pending_call_steal_reply (pending);
268
  DBusMessageIter iter, iter_array, iter_struct;
269

            
270
161
  if (!reply)
271
    goto done;
272

            
273
161
  if (strncmp (dbus_message_get_signature (reply), "a(s", 3) != 0)
274
    {
275
      g_warning ("atk-bridge: get_device_events_reply: unknown signature");
276
      goto done;
277
    }
278

            
279
161
  dbus_message_iter_init (reply, &iter);
280
161
  dbus_message_iter_recurse (&iter, &iter_array);
281
161
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
282
    {
283
      char *bus_name;
284
      dbus_message_iter_recurse (&iter_array, &iter_struct);
285
      dbus_message_iter_get_basic (&iter_struct, &bus_name);
286
      spi_atk_add_client (bus_name);
287
      dbus_message_iter_next (&iter_array);
288
    }
289

            
290
161
done:
291
161
  if (reply)
292
161
    dbus_message_unref (reply);
293
161
  if (pending)
294
161
    dbus_pending_call_unref (pending);
295

            
296
161
  tally_event_reply ();
297
161
}
298

            
299
static void
300
161
get_registered_event_listeners (SpiBridge *app)
301
{
302
  DBusMessage *message;
303
161
  DBusPendingCall *pending = NULL;
304

            
305
161
  message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
306
                                          ATSPI_DBUS_PATH_REGISTRY,
307
                                          ATSPI_DBUS_INTERFACE_REGISTRY,
308
                                          "GetRegisteredEvents");
309
161
  if (!message)
310
    return;
311

            
312
161
  dbus_connection_send_with_reply (app->bus, message, &pending, -1);
313
161
  dbus_message_unref (message);
314
161
  if (!pending)
315
    {
316
      spi_global_app_data->events_initialized = TRUE;
317
      return;
318
    }
319
161
  dbus_pending_call_set_notify (pending, get_events_reply, NULL, NULL);
320

            
321
161
  message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
322
                                          ATSPI_DBUS_PATH_DEC,
323
                                          ATSPI_DBUS_INTERFACE_DEC,
324
                                          "GetKeystrokeListeners");
325
161
  if (!message)
326
    return;
327
161
  pending = NULL;
328
161
  dbus_connection_send_with_reply (app->bus, message, &pending, -1);
329
161
  dbus_message_unref (message);
330
161
  if (!pending)
331
    {
332
      spi_global_app_data->events_initialized = TRUE;
333
      return;
334
    }
335
161
  dbus_pending_call_set_notify (pending, get_device_events_reply, NULL, NULL);
336
}
337

            
338
static void
339
161
register_reply (DBusPendingCall *pending, void *user_data)
340
{
341
  DBusMessage *reply;
342
161
  SpiBridge *app = user_data;
343

            
344
161
  app->register_pending = NULL;
345

            
346
161
  reply = dbus_pending_call_steal_reply (pending);
347
161
  dbus_pending_call_unref (pending);
348

            
349
161
  if (!spi_global_app_data)
350
    {
351
      if (reply)
352
        dbus_message_unref (reply);
353
      return;
354
    }
355

            
356
161
  if (reply)
357
    {
358
      gchar *app_name, *obj_path;
359

            
360
161
      if (strcmp (dbus_message_get_signature (reply), "(so)") != 0)
361
        {
362
          g_warning ("AT-SPI: Could not obtain desktop path or name\n");
363
        }
364
      else
365
        {
366
          DBusMessageIter iter, iter_struct;
367
161
          dbus_message_iter_init (reply, &iter);
368
161
          dbus_message_iter_recurse (&iter, &iter_struct);
369
161
          dbus_message_iter_get_basic (&iter_struct, &app_name);
370
161
          dbus_message_iter_next (&iter_struct);
371
161
          dbus_message_iter_get_basic (&iter_struct, &obj_path);
372

            
373
161
          g_free (app->desktop_name);
374
161
          app->desktop_name = g_strdup (app_name);
375
161
          g_free (app->desktop_path);
376
161
          app->desktop_path = g_strdup (obj_path);
377
        }
378
    }
379
  else
380
    {
381
      g_warning ("AT-SPI: Could not embed inside desktop");
382
      return;
383
    }
384
161
  dbus_message_unref (reply);
385

            
386
161
  if (!spi_global_app_data->events_initialized)
387
161
    get_registered_event_listeners (spi_global_app_data);
388
}
389

            
390
static gboolean
391
161
register_application (gpointer data)
392
{
393
161
  SpiBridge *app = data;
394
  DBusMessage *message;
395
  DBusMessageIter iter;
396
  DBusPendingCall *pending;
397

            
398
161
  spi_global_app_data->registration_pending = 0;
399

            
400
161
  message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
401
                                          ATSPI_DBUS_PATH_ROOT,
402
                                          ATSPI_DBUS_INTERFACE_SOCKET,
403
                                          "Embed");
404

            
405
161
  dbus_message_iter_init_append (message, &iter);
406
161
  spi_object_append_reference (&iter, app->root);
407

            
408
161
  if (!dbus_connection_send_with_reply (app->bus, message, &pending, -1) || !pending)
409
    {
410
      if (pending)
411
        dbus_pending_call_unref (pending);
412

            
413
      dbus_message_unref (message);
414
      return FALSE;
415
    }
416

            
417
161
  app->register_pending = pending;
418
161
  dbus_pending_call_set_notify (pending, register_reply, app, NULL);
419

            
420
161
  if (message)
421
161
    dbus_message_unref (message);
422

            
423
161
  return FALSE;
424
}
425

            
426
void
427
161
_atk_bridge_schedule_application_registration (SpiBridge *app)
428
{
429
  /* We need the callback to be called first thing, before any other of ours
430
   * (and possibly of client apps), so use a high priority and a short timeout
431
   * to try and be called first by the main loop. */
432
161
  if (!app->registration_pending)
433
161
    app->registration_pending = spi_timeout_add_full (G_PRIORITY_HIGH, 0,
434
                                                      register_application,
435
                                                      app, NULL);
436
161
}
437

            
438
gboolean
439
161
_atk_bridge_remove_pending_application_registration (SpiBridge *app)
440
{
441
161
  if (app->registration_pending)
442
    {
443
      g_source_remove (app->registration_pending);
444
      app->registration_pending = 0;
445
      return TRUE;
446
    }
447

            
448
161
  return FALSE;
449
}
450

            
451
/*---------------------------------------------------------------------------*/
452

            
453
static void
454
322
remove_socket ()
455
{
456
322
  if (!spi_global_app_data)
457
161
    return;
458

            
459
161
  if (spi_global_app_data->app_bus_addr &&
460
161
      !strncmp (spi_global_app_data->app_bus_addr, "unix:path=", 10))
461
    {
462
161
      unlink (spi_global_app_data->app_bus_addr + 10);
463
161
      g_free (spi_global_app_data->app_bus_addr);
464
161
      spi_global_app_data->app_bus_addr = NULL;
465
    }
466

            
467
161
  if (spi_global_app_data->app_tmp_dir)
468
    {
469
      rmdir (spi_global_app_data->app_tmp_dir);
470
      g_free (spi_global_app_data->app_tmp_dir);
471
      spi_global_app_data->app_tmp_dir = NULL;
472
    }
473
}
474

            
475
static void
476
161
deregister_application (SpiBridge *app)
477
{
478
  DBusMessage *message;
479
  DBusMessageIter iter;
480
  const char *uname;
481

            
482
161
  if (_atk_bridge_remove_pending_application_registration (spi_global_app_data))
483
    return;
484

            
485
161
  message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
486
                                          ATSPI_DBUS_PATH_REGISTRY,
487
                                          ATSPI_DBUS_INTERFACE_REGISTRY,
488
                                          "DeregisterApplication");
489
161
  dbus_message_set_no_reply (message, TRUE);
490

            
491
161
  uname = dbus_bus_get_unique_name (app->bus);
492

            
493
161
  dbus_message_iter_init_append (message, &iter);
494
161
  dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
495
161
  dbus_connection_send (app->bus, message, NULL);
496
161
  if (message)
497
161
    dbus_message_unref (message);
498

            
499
161
  remove_socket ();
500

            
501
161
  g_free (app->desktop_name);
502
161
  app->desktop_name = NULL;
503
161
  g_free (app->desktop_path);
504
161
  app->desktop_path = NULL;
505
}
506

            
507
/*---------------------------------------------------------------------------*/
508

            
509
/*---------------------------------------------------------------------------*/
510

            
511
static AtkPlugClass *plug_class;
512
static AtkSocketClass *socket_class;
513

            
514
static gchar *
515
get_plug_id (AtkPlug *plug)
516
{
517
  const char *uname;
518
  gchar *path;
519
  GString *str;
520

            
521
  if (!spi_global_app_data || !spi_global_app_data->bus)
522
    return NULL;
523

            
524
  str = g_string_new (NULL);
525
  uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
526
  path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
527
  g_string_printf (str, "%s:%s", uname, path);
528
  g_free (path);
529
  return g_string_free (str, FALSE);
530
}
531

            
532
AtkStateSet *
533
socket_ref_state_set (AtkObject *accessible)
534
{
535
  char *child_name, *child_path;
536
  AtkSocket *socket = ATK_SOCKET (accessible);
537
  int count = 0;
538
  int j;
539
  int v;
540
  DBusMessage *message, *reply;
541
  DBusMessageIter iter, iter_array;
542
  AtkStateSet *set;
543

            
544
  set = atk_state_set_new ();
545

            
546
  if (!socket->embedded_plug_id)
547
    return set;
548

            
549
  child_name = g_strdup (socket->embedded_plug_id);
550
  if (!child_name)
551
    return set;
552
  child_path = g_utf8_strchr (child_name + 1, -1, ':');
553
  if (!child_path)
554
    {
555
      g_free (child_name);
556
      return set;
557
    }
558
  *(child_path++) = '\0';
559
  message = dbus_message_new_method_call (child_name, child_path, ATSPI_DBUS_INTERFACE_ACCESSIBLE, "GetState");
560
  g_free (child_name);
561
  reply = dbus_connection_send_with_reply_and_block (spi_global_app_data->bus, message, 1, NULL);
562
  dbus_message_unref (message);
563
  if (reply == NULL)
564
    return set;
565
  if (strcmp (dbus_message_get_signature (reply), "au") != 0)
566
    {
567
      dbus_message_unref (reply);
568
      return set;
569
    }
570

            
571
  dbus_message_iter_init (reply, &iter);
572
  dbus_message_iter_recurse (&iter, &iter_array);
573
  do
574
    {
575
      dbus_message_iter_get_basic (&iter_array, &v);
576
      for (j = 0; j < 32; j++)
577
        {
578
          if (v & (1u << j))
579
            {
580
              AtkState state = spi_atk_state_from_spi_state ((count << 5) + j);
581
              atk_state_set_add_state (set, state);
582
            }
583
        }
584
      count++;
585
    }
586
  while (dbus_message_iter_next (&iter_array));
587
  dbus_message_unref (reply);
588
  return set;
589
}
590

            
591
static void
592
socket_embed_hook (AtkSocket *socket, const gchar *plug_id)
593
{
594
  g_return_if_fail (spi_global_register != NULL);
595

            
596
  AtkObject *accessible = ATK_OBJECT (socket);
597
  gchar *plug_name, *plug_path;
598
  AtkObjectClass *klass;
599

            
600
  /* Force registration */
601
  gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
602
  /* Let the plug know that it has been embedded */
603
  plug_name = g_strdup (plug_id);
604
  if (!plug_name)
605
    {
606
      g_free (path);
607
      return;
608
    }
609
  plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
610
  if (plug_path)
611
    {
612
      DBusMessage *message;
613
      *(plug_path++) = '\0';
614
      message = dbus_message_new_method_call (plug_name, plug_path, ATSPI_DBUS_INTERFACE_SOCKET, "Embedded");
615
      dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
616
      dbus_connection_send (spi_global_app_data->bus, message, NULL);
617
    }
618
  g_free (plug_name);
619
  g_free (path);
620

            
621
  klass = ATK_OBJECT_GET_CLASS (accessible);
622
  klass->ref_state_set = socket_ref_state_set;
623
}
624

            
625
static void
626
161
install_plug_hooks ()
627
{
628
  gpointer data;
629

            
630
161
  data = g_type_class_ref (ATK_TYPE_PLUG);
631
161
  plug_class = ATK_PLUG_CLASS (data);
632
161
  data = g_type_class_ref (ATK_TYPE_SOCKET);
633
161
  socket_class = ATK_SOCKET_CLASS (data);
634
161
  plug_class->get_object_id = get_plug_id;
635
161
  socket_class->embed = socket_embed_hook;
636
161
}
637

            
638
static guint
639
get_ancestral_uid (guint pid)
640
{
641
  FILE *fp;
642
  char buf[80];
643
  int ppid = 0;
644
  int uid = 0;
645
  gboolean got_ppid = 0;
646
  gboolean got_uid = 0;
647

            
648
  sprintf (buf, "/proc/%d/status", pid);
649
  fp = fopen (buf, "r");
650
  if (!fp)
651
    return 0;
652
  while ((!got_ppid || !got_uid) && fgets (buf, sizeof (buf), fp))
653
    {
654
      if (sscanf (buf, "PPid:\t%d", &ppid) == 1)
655
        got_ppid = TRUE;
656
      else if (sscanf (buf, "Uid:\t%d", &uid) == 1)
657
        got_uid = TRUE;
658
    }
659
  fclose (fp);
660

            
661
  if (!got_ppid || !got_uid)
662
    return 0;
663
  if (uid != 0)
664
    return uid;
665
  if (ppid == 0 || ppid == 1)
666
    return 0;
667
  return get_ancestral_uid (ppid);
668
}
669

            
670
static dbus_bool_t
671
134
user_check (DBusConnection *bus, unsigned long uid, void *data)
672
{
673
134
  if (uid == getuid () || uid == geteuid ())
674
134
    return TRUE;
675
  if (getuid () == 0)
676
    {
677
      guint ancestor = get_ancestral_uid (getpid ());
678
      return (ancestor == uid || ancestor == 1 || ancestor == 0);
679
    }
680
  return FALSE;
681
}
682

            
683
static void
684
159
remove_direct_connection (DBusConnection *connection)
685
{
686
  GList *link;
687

            
688
159
  link = g_list_find (spi_global_app_data->direct_connections, connection);
689
159
  if (!link)
690
    return;
691

            
692
159
  droute_context_unregister (spi_global_app_data->droute, connection);
693
159
  droute_unintercept_dbus (connection);
694
318
  spi_global_app_data->direct_connections =
695
159
      g_list_delete_link (spi_global_app_data->direct_connections, link);
696
159
  dbus_connection_unref (connection);
697
}
698

            
699
static DBusHandlerResult
700
481
direct_connection_filter (DBusConnection *connection,
701
                          DBusMessage *message,
702
                          void *user_data)
703
{
704
481
  if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected"))
705
    {
706
4
      dbus_connection_remove_filter (connection,
707
                                     direct_connection_filter,
708
                                     NULL);
709
4
      remove_direct_connection (connection);
710
    }
711

            
712
481
  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
713
}
714

            
715
static void
716
159
new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
717
{
718
159
  dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
719
159
  if (!dbus_connection_add_filter (con, direct_connection_filter, NULL, NULL))
720
    return;
721

            
722
159
  dbus_connection_ref (con);
723
159
  atspi_dbus_connection_setup_with_g_main (con, spi_context);
724
159
  droute_intercept_dbus (con);
725
159
  droute_context_register (spi_global_app_data->droute, con);
726

            
727
159
  spi_global_app_data->direct_connections =
728
159
      g_list_append (spi_global_app_data->direct_connections, con);
729
}
730

            
731
static gchar *atspi_dbus_name = NULL;
732
static gboolean atspi_no_register = FALSE;
733

            
734
static GOptionEntry atspi_option_entries[] = {
735
  { "atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
736
    "D-Bus bus name to register as", NULL },
737
  { "atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
738
    "Do not register with Registry Daemon", NULL },
739
  { NULL }
740
};
741

            
742
static void
743
add_objects_for_introspection (AtkObject *obj, GString *str)
744
{
745
  gchar *path;
746
  AtkStateSet *set;
747
  char *p;
748
  gint i;
749
  gint count;
750

            
751
  if (!obj)
752
    return;
753

            
754
  path = spi_register_object_to_path (spi_global_register, G_OBJECT (obj));
755
  p = strrchr (path, '/') + 1;
756
  g_string_append_printf (str, "<node name=\"%s\"/>\n", p);
757
  g_free (path);
758

            
759
  if (ATK_IS_SOCKET (obj))
760
    return;
761

            
762
  set = atk_object_ref_state_set (obj);
763
  if (atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
764
    {
765
      g_object_unref (set);
766
      return;
767
    }
768
  g_object_unref (set);
769

            
770
  count = atk_object_get_n_accessible_children (obj);
771
  for (i = 0; i < count; i++)
772
    {
773
      AtkObject *child = atk_object_ref_accessible_child (obj, i);
774
      add_objects_for_introspection (child, str);
775
      g_object_unref (child);
776
    }
777
}
778

            
779
static gchar *
780
introspect_children_cb (const char *path, void *data)
781
{
782
  if (!strcmp (path, "/org/a11y/atspi/accessible"))
783
    {
784
      GString *str = g_string_new (NULL);
785
      add_objects_for_introspection (spi_global_app_data->root, str);
786
      return g_string_free (str, FALSE);
787
    }
788

            
789
  return NULL;
790
}
791

            
792
static void
793
handle_event_listener_registered (DBusConnection *bus, DBusMessage *message, void *user_data)
794
{
795
  DBusMessageIter iter;
796
  const char *signature = dbus_message_get_signature (message);
797

            
798
  if (strcmp (signature, "ssas") != 0 &&
799
      strcmp (signature, "ss") != 0)
800
    {
801
      g_warning ("got RegisterEvent with invalid signature '%s'", signature);
802
      return;
803
    }
804

            
805
  dbus_message_iter_init (message, &iter);
806
  add_event_from_iter (&iter);
807
}
808

            
809
static void
810
free_property_definition (void *data)
811
{
812
  AtspiPropertyDefinition *pd = data;
813

            
814
  g_free (pd->name);
815
  g_free (pd);
816
}
817

            
818
static void
819
321
remove_events (const char *bus_name, const char *event)
820
{
821
  gchar **remove_data;
822
  GList *list;
823

            
824
321
  remove_data = g_strsplit (event, ":", 3);
825
321
  if (!remove_data)
826
    {
827
      return;
828
    }
829

            
830
642
  for (list = spi_global_app_data->events; list;)
831
    {
832
321
      event_data *evdata = list->data;
833
322
      if (!g_strcmp0 (evdata->bus_name, bus_name) &&
834
1
          spi_event_is_subtype (evdata->data, remove_data))
835
1
        {
836
          GList *next;
837
1
          GList *events = spi_global_app_data->events;
838

            
839
1
          g_strfreev (evdata->data);
840
1
          g_free (evdata->bus_name);
841
1
          g_slist_free_full (evdata->properties, free_property_definition);
842
1
          g_free (evdata);
843

            
844
1
          next = list->next;
845
1
          spi_global_app_data->events = g_list_delete_link (events, list);
846
1
          list = next;
847
        }
848
      else
849
        {
850
320
          list = list->next;
851
        }
852
    }
853

            
854
321
  g_strfreev (remove_data);
855
}
856

            
857
static void
858
321
handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message, void *user_data)
859
{
860
  gchar *name;
861
  char *sender;
862

            
863
321
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
864
                              DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
865
    return;
866

            
867
321
  remove_events (sender, name);
868
}
869

            
870
static void
871
handle_device_listener_registered (DBusConnection *bus, DBusMessage *message, void *user_data)
872
{
873
  char *sender;
874
  DBusMessageIter iter, iter_struct;
875

            
876
  if (strncmp (dbus_message_get_signature (message), "(s", 2) != 0)
877
    {
878
      g_warning ("atk-bridge: handle_device_listener_register: unknown signature");
879
      return;
880
    }
881

            
882
  dbus_message_iter_init (message, &iter);
883
  dbus_message_iter_recurse (&iter, &iter_struct);
884
  dbus_message_iter_get_basic (&iter_struct, &sender);
885
  spi_atk_add_client (sender);
886
}
887

            
888
static DBusHandlerResult
889
1285
signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
890
{
891
1285
  const char *interface = dbus_message_get_interface (message);
892
1285
  const char *member = dbus_message_get_member (message);
893
1285
  DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
894
  static gboolean registry_lost = FALSE;
895

            
896
1285
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
897
641
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
898

            
899
644
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_REGISTRY))
900
    {
901
321
      result = DBUS_HANDLER_RESULT_HANDLED;
902
321
      if (!strcmp (member, "EventListenerRegistered"))
903
        handle_event_listener_registered (bus, message, user_data);
904
321
      else if (!strcmp (member, "EventListenerDeregistered"))
905
321
        handle_event_listener_deregistered (bus, message, user_data);
906
      else
907
        result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
908
    }
909
323
  else if (!strcmp (interface, ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER))
910
    {
911
      result = DBUS_HANDLER_RESULT_HANDLED;
912
      if (!strcmp (member, "KeystrokeListenerRegistered"))
913
        handle_device_listener_registered (bus, message, user_data);
914
      else
915
        result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
916
    }
917

            
918
967
  if (!g_strcmp0 (interface, DBUS_INTERFACE_DBUS) &&
919
323
      !g_strcmp0 (member, "NameOwnerChanged"))
920
    {
921
      char *name, *old, *new;
922
1
      if (dbus_message_get_args (message, NULL,
923
                                 DBUS_TYPE_STRING, &name,
924
                                 DBUS_TYPE_STRING, &old,
925
                                 DBUS_TYPE_STRING, &new,
926
                                 DBUS_TYPE_INVALID))
927
        {
928
1
          if (!strcmp (name, "org.a11y.atspi.Registry"))
929
            {
930
              if (registry_lost && !old[0])
931
                {
932
                  register_application (spi_global_app_data);
933
                  registry_lost = FALSE;
934
                }
935
              else if (!new[0])
936
                registry_lost = TRUE;
937
            }
938
1
          else if (*old != '\0' && *new == '\0')
939
1
            spi_atk_remove_client (old);
940
        }
941
    }
942

            
943
644
  return result;
944
}
945

            
946
int
947
161
spi_atk_create_socket (SpiBridge *app)
948
{
949
  DBusServer *server;
950
  DBusError error;
951
161
  const gchar *user_runtime_dir = g_get_user_runtime_dir ();
952
  char *socket_path;
953
  char *escaped_socket_path;
954
  const char *disable_p2p;
955

            
956
161
  disable_p2p = g_getenv ("ATSPI_DISABLE_P2P");
957
161
  if (disable_p2p && atoi (disable_p2p) > 0)
958
    return 0;
959

            
960
161
  if (g_mkdir_with_parents (user_runtime_dir, 0700) != 0)
961
    return -1;
962

            
963
161
  if (getuid () != 0)
964
    {
965
      app->app_tmp_dir = g_build_filename (user_runtime_dir,
966
                                           "at-spi2-XXXXXX", NULL);
967
      if (!g_mkdtemp (app->app_tmp_dir))
968
        {
969
          g_free (app->app_tmp_dir);
970
          app->app_tmp_dir = NULL;
971
          return -1;
972
        }
973
    }
974

            
975
161
  if (app->app_tmp_dir)
976
    {
977
      socket_path = g_strdup_printf ("%s/socket", app->app_tmp_dir);
978
    }
979
  else
980
    {
981
161
      socket_path = g_strdup_printf ("%s/at-spi2-socket-%d",
982
                                     user_runtime_dir, getpid ());
983
    }
984

            
985
161
  escaped_socket_path = dbus_address_escape_value (socket_path);
986
161
  g_free (socket_path);
987

            
988
161
  app->app_bus_addr = g_strconcat ("unix:path=", escaped_socket_path, NULL);
989
161
  dbus_free (escaped_socket_path);
990

            
991
161
  dbus_error_init (&error);
992
161
  server = dbus_server_listen (app->app_bus_addr, &error);
993
161
  if (server == NULL)
994
    {
995
      g_warning ("atk-bridge: Couldn't listen on dbus server: %s", error.message);
996
      dbus_error_free (&error);
997
      app->app_bus_addr[0] = '\0';
998
      return -1;
999
    }
161
  atspi_dbus_server_setup_with_g_main (server, spi_context);
161
  dbus_server_set_new_connection_function (server, new_connection_cb, NULL, NULL);
161
  app->server = server;
161
  return 0;
}
/*
 * Checks the status of the environment variables
 *
 * At this moment it only checks NO_AT_BRIDGE
 *
 * Returns TRUE if there isn't anything on the environment preventing
 * you to load the bridge, FALSE otherwise
 */
static gboolean
161
check_envvar (void)
{
  const gchar *envvar;
161
  envvar = g_getenv ("NO_AT_BRIDGE");
161
  if (envvar && atoi (envvar) == 1)
    return FALSE;
  else
161
    return TRUE;
}
void
161
spi_atk_activate ()
{
  DRoutePath *treepath;
161
  spi_atk_register_event_listeners ();
161
  if (!spi_global_cache)
    {
161
      spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
161
      treepath = droute_add_one (spi_global_app_data->droute,
                                 "/org/a11y/atspi/cache", spi_global_cache);
161
      if (!treepath)
        {
          g_warning ("atk-bridge: Error in droute_add_one().  Already running?");
          return;
        }
161
      spi_initialize_cache (treepath);
161
      if (spi_global_app_data->bus)
161
        droute_path_register (treepath, spi_global_app_data->bus);
    }
161
  spi_cache_emit_ready ();
}
static gboolean
spi_object_has_dbus_interface (void *obj, const char *interface)
{
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_ACCESSIBLE))
    return TRUE;
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_ACTION))
    return ATK_IS_ACTION (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_APPLICATION))
      return ATK_IS_OBJECT(obj) && (atk_object_get_role (obj) == ATK_ROLE_APPLICATION);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_COLLECTION))
    return TRUE;
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_COMPONENT))
    return ATK_IS_COMPONENT (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_DOCUMENT))
    return ATK_IS_DOCUMENT (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_EDITABLE_TEXT))
    return ATK_IS_EDITABLE_TEXT (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_HYPERLINK))
    return ATK_IS_HYPERLINK (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_HYPERTEXT))
    return ATK_IS_HYPERTEXT (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_IMAGE))
    return ATK_IS_IMAGE (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_SELECTION))
    return ATK_IS_SELECTION (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_SOCKET))
    return TRUE;
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_TABLE))
    return ATK_IS_TABLE (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_TABLE_CELL))
    return ATK_IS_TABLE_CELL (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_TEXT))
    return ATK_IS_TEXT (obj);
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_VALUE))
    return ATK_IS_VALUE (obj);
  return FALSE;
}
static gboolean
161
init_bus ()
{
  DBusError error;
  /* Set up D-Bus connection and register bus name */
161
  dbus_error_init (&error);
161
  spi_global_app_data->bus = atspi_get_a11y_bus ();
161
  if (!spi_global_app_data->bus)
    return FALSE;
161
  if (atspi_dbus_name != NULL)
    {
161
      if (dbus_bus_request_name (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
        {
161
          g_print ("AT-SPI Received D-Bus name - %s\n", atspi_dbus_name);
        }
      else
        {
          g_print ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
                   atspi_dbus_name);
        }
    }
161
  atspi_dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
161
  droute_context_register (spi_global_app_data->droute,
161
                           spi_global_app_data->bus);
  /* Set up filter and match rules to catch signals */
161
  dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
161
  dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.DeviceEventListener', sender='org.a11y.atspi.Registry'", NULL);
161
  dbus_bus_add_match (spi_global_app_data->bus, "type='signal', arg0='org.a11y.atspi.Registry', interface='org.freedesktop.DBus', member='NameOwnerChanged'", NULL);
161
  dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
                              NULL);
  /* Register this app by sending a signal out to AT-SPI registry daemon */
161
  if (!atspi_no_register && !ATK_IS_PLUG (spi_global_app_data->root))
161
    _atk_bridge_schedule_application_registration (spi_global_app_data);
  else
    get_registered_event_listeners (spi_global_app_data);
161
  dbus_error_free (&error);
161
  return TRUE;
}
static void
161
deactivate_bus ()
{
161
  if (!spi_global_app_data->bus)
    return;
161
  dbus_connection_remove_filter (spi_global_app_data->bus, signal_filter, NULL);
161
  droute_context_unregister (spi_global_app_data->droute, spi_global_app_data->bus);
161
  if (atspi_dbus_name != NULL)
    {
      DBusError error;
      int result;
161
      dbus_error_init (&error);
161
      result = dbus_bus_release_name (spi_global_app_data->bus, atspi_dbus_name, &error);
161
      if (result == -1)
        {
          g_warning ("atk-bridge: could not release dbus name: %s", error.message);
        }
      else
        {
161
          g_print ("bridge: released name, result %d\n", result);
        }
161
      dbus_error_free (&error);
    }
161
  if (spi_global_app_data->register_pending)
    {
      dbus_pending_call_cancel (spi_global_app_data->register_pending);
      spi_global_app_data->register_pending = NULL;
    }
161
  dbus_connection_close (spi_global_app_data->bus);
161
  dbus_connection_unref (spi_global_app_data->bus);
161
  spi_global_app_data->bus = NULL;
}
static void
on_session_name_owner_changed (GDBusConnection *connection,
                               const gchar *sender_name,
                               const gchar *object_path,
                               const gchar *interface_name,
                               const gchar *signal_name,
                               GVariant *parameters,
                               gpointer user_data)
{
  const gchar *old_owner;
  const gchar *new_owner;
  g_variant_get (parameters,
                 "(&s&s&s)",
                 NULL,
                 &old_owner,
                 &new_owner);
  if (strlen (new_owner) == 0)
    {
      spi_atk_deregister_event_listeners ();
      deactivate_bus ();
    }
  else
    {
      init_bus ();
      spi_atk_register_event_listeners ();
    }
}
/**
 * atk_bridge_adaptor_init: initializes the atk bridge adaptor
 *
 * The following needs to be initialized.
 *
 * - DRoute for routing message to their accessible objects.
 * - Event handlers for emmitting signals on specific ATK events.
 * - setup the bus for p2p communication
 * - Application registration with the AT-SPI registry.
 *
 * Returns: 0 if the bridge gets or was already initialized
 * succesfully, -1 otherwise
 */
int
161
atk_bridge_adaptor_init (gint *argc, gchar **argv[])
{
  GOptionContext *opt;
161
  GError *err = NULL;
  AtkObject *root;
  gboolean load_bridge;
  DRoutePath *accpath;
161
  load_bridge = check_envvar ();
161
  if (inited && !load_bridge)
    g_warning ("ATK Bridge is disabled but a11y has already been enabled.");
161
  if (inited)
    return 0;
161
  if (!load_bridge)
    return -1;
161
  inited = TRUE;
161
  root = atk_get_root ();
161
  g_warn_if_fail (root);
161
  if (!root)
    {
      inited = FALSE;
      return -1;
    }
  /* Parse command line options */
161
  opt = g_option_context_new (NULL);
161
  g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
161
  g_option_context_set_ignore_unknown_options (opt, TRUE);
161
  if (!g_option_context_parse (opt, argc, argv, &err))
    {
      g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
      g_error_free (err);
    }
161
  g_option_context_free (opt);
  /* Allocate global data and do ATK initializations */
161
  spi_global_app_data = g_new0 (SpiBridge, 1);
161
  spi_global_app_data->root = g_object_ref (root);
161
  spi_global_app_data->desktop_name = g_strdup (ATSPI_DBUS_NAME_REGISTRY);
161
  spi_global_app_data->desktop_path = g_strdup (ATSPI_DBUS_PATH_ROOT);
  /* Hook our plug-and socket functions */
161
  install_plug_hooks ();
  /*
   * Create the leasing, register and cache objects.
   * The order is important here, the cache depends on the
   * register object.
   */
161
  spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
161
  spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
  /* Register droute for routing AT-SPI messages */
322
  spi_global_app_data->droute =
161
      droute_new ();
161
  accpath = droute_add_many (spi_global_app_data->droute,
                             "/org/a11y/atspi/accessible",
                             NULL,
                             introspect_children_cb,
                             NULL,
                             (DRouteGetDatumFunction)
                                 spi_global_register_path_to_object,
                             spi_object_has_dbus_interface);
  /* Register all interfaces with droute and set up application accessible db */
161
  spi_initialize_accessible (accpath);
161
  spi_initialize_application (accpath);
161
  spi_initialize_action (accpath);
161
  spi_initialize_collection (accpath);
161
  spi_initialize_component (accpath);
161
  spi_initialize_document (accpath);
161
  spi_initialize_editabletext (accpath);
161
  spi_initialize_hyperlink (accpath);
161
  spi_initialize_hypertext (accpath);
161
  spi_initialize_image (accpath);
161
  spi_initialize_selection (accpath);
161
  spi_initialize_socket (accpath);
161
  spi_initialize_table (accpath);
161
  spi_initialize_table_cell (accpath);
161
  spi_initialize_text (accpath);
161
  spi_initialize_value (accpath);
161
  if (!init_bus ())
    {
      g_object_unref (spi_global_app_data->root);
      g_free (spi_global_app_data->desktop_name);
      g_free (spi_global_app_data->desktop_path);
      droute_free (spi_global_app_data->droute);
      if (spi_global_app_data->property_hash)
        g_hash_table_destroy (spi_global_app_data->property_hash);
      g_free (spi_global_app_data);
      spi_global_app_data = NULL;
      inited = FALSE;
      return -1;
    }
161
  spi_global_app_data->main_context = g_main_context_new ();
  /* Register methods to send D-Bus signals on certain ATK events */
161
  if (clients)
    spi_atk_activate ();
161
  if (!atexit_added)
161
    atexit (remove_socket);
161
  atexit_added = TRUE;
161
  spi_global_app_data->session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
161
  if (spi_global_app_data->session_bus)
    {
161
      spi_global_app_data->name_owner_changed_subscription_id =
161
          g_dbus_connection_signal_subscribe (spi_global_app_data->session_bus,
                                              "org.freedesktop.DBus",  /* name */
                                              "org.freedesktop.DBus",  /* interface */
                                              "NameOwnerChanged",      /* signal name */
                                              "/org/freedesktop/DBus", /* path */
                                              "org.a11y.Bus",          /* arg0 */
                                              G_DBUS_SIGNAL_FLAGS_NONE,
                                              on_session_name_owner_changed,
                                              NULL,
                                              (GDestroyNotify) NULL);
    }
161
  return 0;
}
void
161
atk_bridge_adaptor_cleanup (void)
{
  GSList *ls;
161
  if (!inited)
    return;
161
  if (!spi_global_app_data)
    return;
161
  spi_atk_tidy_windows ();
161
  spi_atk_deregister_event_listeners ();
161
  deregister_application (spi_global_app_data);
161
  deactivate_bus ();
316
  while (spi_global_app_data->direct_connections)
    {
      DBusConnection *connection;
155
      connection = spi_global_app_data->direct_connections->data;
155
      dbus_connection_remove_filter (connection,
                                     direct_connection_filter,
                                     NULL);
155
      dbus_connection_close (connection);
155
      remove_direct_connection (connection);
    }
322
  for (ls = clients; ls; ls = ls->next)
161
    g_free (ls->data);
161
  g_slist_free (clients);
161
  clients = NULL;
161
  g_clear_object (&spi_global_cache);
161
  g_clear_object (&spi_global_leasing);
161
  g_clear_object (&spi_global_register);
161
  if (spi_global_app_data->main_context)
161
    g_main_context_unref (spi_global_app_data->main_context);
161
  droute_free (spi_global_app_data->droute);
161
  g_clear_object (&spi_global_app_data->session_bus);
161
  if (spi_global_app_data->property_hash)
161
    g_hash_table_destroy (spi_global_app_data->property_hash);
161
  g_free (spi_global_app_data);
161
  spi_global_app_data = NULL;
161
  inited = FALSE;
}
/*---------------------------------------------------------------------------*/
static gchar *name_match_tmpl =
    "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged', arg0='%s'";
void
323
spi_atk_add_client (const char *bus_name)
{
  GSList *l;
  gchar *match;
324
  for (l = clients; l; l = l->next)
    {
162
      if (!g_strcmp0 (l->data, bus_name))
161
        return;
    }
162
  if (!clients)
161
    spi_atk_activate ();
162
  clients = g_slist_append (clients, g_strdup (bus_name));
162
  match = g_strdup_printf (name_match_tmpl, bus_name);
162
  dbus_bus_add_match (spi_global_app_data->bus, match, NULL);
162
  g_free (match);
}
void
1
spi_atk_remove_client (const char *bus_name)
{
  GSList *l;
  GSList *next_node;
1
  l = clients;
1
  while (l)
    {
1
      next_node = l->next;
1
      if (!g_strcmp0 (l->data, bus_name))
        {
1
          gchar *match = g_strdup_printf (name_match_tmpl, l->data);
1
          dbus_bus_remove_match (spi_global_app_data->bus, match, NULL);
1
          g_free (match);
1
          g_free (l->data);
1
          clients = g_slist_delete_link (clients, l);
1
          if (!clients)
            spi_atk_deregister_event_listeners ();
1
          return;
        }
      l = next_node;
    }
}
void
1932
spi_atk_add_interface (DRoutePath *path,
                       const char *name,
                       const char *introspect,
                       const DRouteMethod *methods,
                       const DRouteProperty *properties)
{
1932
  droute_path_add_interface (path, name, introspect, methods, properties);
1932
  if (properties)
    {
1932
      if (!spi_global_app_data->property_hash)
161
        spi_global_app_data->property_hash = g_hash_table_new_full (g_str_hash,
                                                                    g_str_equal,
                                                                    g_free, NULL);
1932
      g_hash_table_insert (spi_global_app_data->property_hash, g_strdup (name),
                           (gpointer) properties);
    }
1932
}
/*END------------------------------------------------------------------------*/