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
 * Copyright 2010, 2011 Novell, 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
/*
26
 *
27
 * Basic SPI initialization and event loop function prototypes
28
 *
29
 */
30

            
31
#include "atspi-private.h"
32
#ifdef HAVE_X11
33
#include "X11/Xlib.h"
34
#endif
35
#include "atspi-gmain.h"
36
#include <ctype.h>
37
#include <locale.h>
38
#include <stdio.h>
39
#include <string.h>
40

            
41
/**
42
 * AtspiMisc:
43
 *
44
 * Miscellaneous methods for using AT-SPI services.
45
 */
46

            
47
/* These are listed here for extraction by intltool */
48
#if 0
49
  N_("invalid")
50
  N_("accelerator label")
51
  N_("alert")
52
  N_("animation")
53
  N_("arrow")
54
  N_("calendar")
55
  N_("canvas")
56
  N_("check box")
57
  N_("check menu item")
58
  N_("color chooser")
59
  N_("column header")
60
  N_("combo box")
61
  N_("dateeditor")
62
  N_("desktop icon")
63
  N_("desktop frame")
64
  N_("dial")
65
  N_("dialog")
66
  N_("directory pane")
67
  N_("drawing area")
68
  N_("file chooser")
69
  N_("filler")
70
  /* I know it looks wrong but that is what Java returns */
71
  N_("fontchooser")
72
  N_("frame")
73
  N_("glass pane")
74
  N_("html container")
75
  N_("icon")
76
  N_("image")
77
  N_("internal frame")
78
  N_("label")
79
  N_("layered pane")
80
  N_("list")
81
  N_("list item")
82
  N_("menu")
83
  N_("menu bar")
84
  N_("menu button")
85
  N_("menu item")
86
  N_("option pane")
87
  N_("page tab")
88
  N_("page tab list")
89
  N_("panel")
90
  N_("password text")
91
  N_("popup menu")
92
  N_("progress bar")
93
  N_("button")
94
  N_("radio button")
95
  N_("radio menu item")
96
  N_("root pane")
97
  N_("row header")
98
  N_("scroll bar")
99
  N_("scroll pane")
100
  N_("separator")
101
  N_("slider")
102
  N_("split pane")
103
  N_("spin button")
104
  N_("statusbar")
105
  N_("switch")
106
  N_("table")
107
  N_("table cell")
108
  N_("table column header")
109
  N_("table row header")
110
  N_("tear off menu item")
111
  N_("terminal")
112
  N_("text")
113
  N_("toggle button")
114
  N_("tool bar")
115
  N_("tool tip")
116
  N_("tree")
117
  N_("tree table")
118
  N_("unknown")
119
  N_("viewport")
120
  N_("window")
121
  N_("header")
122
  N_("footer")
123
  N_("paragraph")
124
  N_("ruler")
125
  N_("application")
126
  N_("autocomplete")
127
  N_("edit bar")
128
  N_("embedded component")
129
  N_("entry")
130
  N_("chart")
131
  N_("caption")
132
  N_("document frame")
133
  N_("heading")
134
  N_("page")
135
  N_("section")
136
  N_("redundant object")
137
  N_("form")
138
  N_("link")
139
  N_("input method window")
140
  N_("table row")
141
  N_("tree item")
142
  N_("document spreadsheet")
143
  N_("document presentation")
144
  N_("document text")
145
  N_("document web")
146
  N_("document email")
147
  N_("comment")
148
  N_("list box")
149
  N_("grouping")
150
  N_("image map")
151
  N_("notification")
152
  N_("info bar")
153
  N_("level bar")
154
  N_("title bar")
155
  N_("block quote")
156
  N_("audio")
157
  N_("video")
158
  N_("definition")
159
  N_("article")
160
  N_("landmark")
161
  N_("log")
162
  N_("marquee")
163
  N_("math")
164
  N_("rating")
165
  N_("timer")
166
  N_("description list")
167
  N_("description term")
168
  N_("description value")
169
#endif /* 0 */
170

            
171
static void
172
1
_gettext_initialization (void)
173
{
174
  static gboolean gettext_initialized = FALSE;
175

            
176
1
  if (!gettext_initialized)
177
    {
178
1
      gettext_initialized = TRUE;
179
1
      setlocale (LC_ALL, "");
180
1
      bindtextdomain (GETTEXT_PACKAGE, ATSPI_LOCALEDIR);
181
1
      bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
182
    }
183
1
}
184

            
185
static void handle_get_items (DBusPendingCall *pending, void *user_data);
186

            
187
static DBusConnection *bus = NULL;
188
static GHashTable *live_refs = NULL;
189
static gint method_call_timeout = 800;
190
static gint app_startup_time = 15000;
191

            
192
GMainLoop *atspi_main_loop;
193
GMainContext *atspi_main_context;
194
gboolean atspi_no_cache;
195

            
196
const char *atspi_path_dec = ATSPI_DBUS_PATH_DEC;
197
const char *atspi_path_registry = ATSPI_DBUS_PATH_REGISTRY;
198
const char *atspi_path_root = ATSPI_DBUS_PATH_ROOT;
199
const char *atspi_bus_registry = ATSPI_DBUS_NAME_REGISTRY;
200
const char *atspi_interface_accessible = ATSPI_DBUS_INTERFACE_ACCESSIBLE;
201
const char *atspi_interface_action = ATSPI_DBUS_INTERFACE_ACTION;
202
const char *atspi_interface_application = ATSPI_DBUS_INTERFACE_APPLICATION;
203
const char *atspi_interface_collection = ATSPI_DBUS_INTERFACE_COLLECTION;
204
const char *atspi_interface_component = ATSPI_DBUS_INTERFACE_COMPONENT;
205
const char *atspi_interface_dec = ATSPI_DBUS_INTERFACE_DEC;
206
const char *atspi_interface_device_event_listener = ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER;
207
const char *atspi_interface_document = ATSPI_DBUS_INTERFACE_DOCUMENT;
208
const char *atspi_interface_editable_text = ATSPI_DBUS_INTERFACE_EDITABLE_TEXT;
209
const char *atspi_interface_event_object = ATSPI_DBUS_INTERFACE_EVENT_OBJECT;
210
const char *atspi_interface_hyperlink = ATSPI_DBUS_INTERFACE_HYPERLINK;
211
const char *atspi_interface_hypertext = ATSPI_DBUS_INTERFACE_HYPERTEXT;
212
const char *atspi_interface_image = ATSPI_DBUS_INTERFACE_IMAGE;
213
const char *atspi_interface_registry = ATSPI_DBUS_INTERFACE_REGISTRY;
214
const char *atspi_interface_selection = ATSPI_DBUS_INTERFACE_SELECTION;
215
const char *atspi_interface_table = ATSPI_DBUS_INTERFACE_TABLE;
216
const char *atspi_interface_table_cell = ATSPI_DBUS_INTERFACE_TABLE_CELL;
217
const char *atspi_interface_text = ATSPI_DBUS_INTERFACE_TEXT;
218
const char *atspi_interface_cache = ATSPI_DBUS_INTERFACE_CACHE;
219
const char *atspi_interface_value = ATSPI_DBUS_INTERFACE_VALUE;
220

            
221
static const char *interfaces[] = {
222
  ATSPI_DBUS_INTERFACE_ACCESSIBLE,
223
  ATSPI_DBUS_INTERFACE_ACTION,
224
  ATSPI_DBUS_INTERFACE_APPLICATION,
225
  ATSPI_DBUS_INTERFACE_COLLECTION,
226
  ATSPI_DBUS_INTERFACE_COMPONENT,
227
  ATSPI_DBUS_INTERFACE_DOCUMENT,
228
  ATSPI_DBUS_INTERFACE_EDITABLE_TEXT,
229
  ATSPI_DBUS_INTERFACE_HYPERLINK,
230
  ATSPI_DBUS_INTERFACE_HYPERTEXT,
231
  ATSPI_DBUS_INTERFACE_IMAGE,
232
  ATSPI_DBUS_INTERFACE_SELECTION,
233
  ATSPI_DBUS_INTERFACE_SOCKET,
234
  ATSPI_DBUS_INTERFACE_TABLE,
235
  ATSPI_DBUS_INTERFACE_TABLE_CELL,
236
  ATSPI_DBUS_INTERFACE_TEXT,
237
  ATSPI_DBUS_INTERFACE_VALUE,
238
  NULL
239
};
240

            
241
/* Holds a dbus object reference as a pair of app_name/path.  These have the lifetime
242
 * of the DBusMessage that is being processed.
243
 */
244
typedef struct
245
{
246
  const char *app_name;
247
  const char *path;
248
} ReferenceFromMessage;
249

            
250
static void
251
3443
get_reference_from_iter (DBusMessageIter *iter, ReferenceFromMessage *ref)
252
{
253
  DBusMessageIter iter_struct;
254

            
255
3443
  dbus_message_iter_recurse (iter, &iter_struct);
256
3443
  dbus_message_iter_get_basic (&iter_struct, &ref->app_name);
257
3443
  dbus_message_iter_next (&iter_struct);
258
3443
  dbus_message_iter_get_basic (&iter_struct, &ref->path);
259
3443
  dbus_message_iter_next (iter);
260
3443
}
261

            
262
gint
263
3852
_atspi_get_iface_num (const char *iface)
264
{
265
  /* TODO: Use a binary search or hash to improve performance */
266
  int i;
267

            
268
17328
  for (i = 0; interfaces[i]; i++)
269
    {
270
17328
      if (!strcmp (iface, interfaces[i]))
271
3852
        return i;
272
    }
273
  return -1;
274
}
275

            
276
GHashTable *
277
2
_atspi_get_live_refs (void)
278
{
279
2
  if (!live_refs)
280
    {
281
2
      live_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
282
    }
283
2
  return live_refs;
284
}
285

            
286
/* TODO: Add an application parameter */
287
DBusConnection *
288
337
_atspi_bus ()
289
{
290
337
  if (!bus)
291
    atspi_init ();
292
337
  if (!bus)
293
    g_error ("AT-SPI: Couldn't connect to accessibility bus. Is at-spi-bus-launcher running?");
294
337
  return bus;
295
}
296

            
297
#define APP_IS_REGISTRY(app) (!strcmp (app->bus_name, atspi_bus_registry))
298

            
299
static AtspiAccessible *desktop;
300

            
301
static void cleanup_deferred_message (void);
302

            
303
static void
304
2
cleanup ()
305
{
306
  GHashTable *refs;
307
  gint i;
308

            
309
2
  refs = live_refs;
310
2
  live_refs = NULL;
311
2
  if (refs)
312
    {
313
2
      g_hash_table_destroy (refs);
314
    }
315

            
316
2
  if (!desktop)
317
    goto end;
318

            
319
  /* TODO: Do we need this code, or should we just dispose the desktop? */
320
326
  for (i = desktop->children->len - 1; i >= 0; i--)
321
    {
322
324
      AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
323
324
      if (child->parent.app)
324
54
        g_object_run_dispose (G_OBJECT (child->parent.app));
325
324
      g_object_run_dispose (G_OBJECT (child));
326
    }
327

            
328
2
  g_object_run_dispose (G_OBJECT (desktop->parent.app));
329
2
  g_object_unref (desktop);
330
2
  desktop = NULL;
331

            
332
2
end:
333
2
  if (bus)
334
    {
335
2
      dbus_connection_close (bus);
336
2
      dbus_connection_unref (bus);
337
2
      bus = NULL;
338
    }
339

            
340
2
  cleanup_deferred_message ();
341
2
}
342

            
343
static gboolean atspi_inited = FALSE;
344

            
345
static GHashTable *app_hash = NULL;
346

            
347
static void
348
166
handle_get_bus_address (DBusPendingCall *pending, void *user_data)
349
{
350
166
  AtspiApplication *app = user_data;
351
166
  DBusMessage *reply = dbus_pending_call_steal_reply (pending);
352
  DBusMessage *message;
353
  const char *address;
354
  DBusPendingCall *new_pending;
355

            
356
166
  if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
357
    {
358
161
      if (dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &address,
359
161
                                 DBUS_TYPE_INVALID) &&
360
161
          address[0])
361
        {
362
          DBusError error;
363
          DBusConnection *bus;
364

            
365
161
          dbus_error_init (&error);
366
161
          bus = dbus_connection_open_private (address, &error);
367
161
          if (bus)
368
            {
369
157
              if (app->bus)
370
                {
371
157
                  dbus_connection_unref (app->bus);
372
                }
373
157
              app->bus = bus;
374
157
              atspi_dbus_connection_setup_with_g_main (bus, g_main_context_default ());
375
            }
376
          else
377
            {
378
8
              if (!strcmp (error.name, DBUS_ERROR_FILE_NOT_FOUND) &&
379
4
                  !g_getenv ("ATSPI_IN_TESTS"))
380
                g_warning ("AT-SPI: Unable to open bus connection: %s", error.message);
381
4
              dbus_error_free (&error);
382
            }
383
        }
384
    }
385
166
  dbus_message_unref (reply);
386
166
  dbus_pending_call_unref (pending);
387

            
388
166
  if (!app->bus)
389
    return; /* application has gone away / been disposed */
390

            
391
166
  message = dbus_message_new_method_call (app->bus_name,
392
                                          "/org/a11y/atspi/cache",
393
                                          atspi_interface_cache, "GetItems");
394

            
395
166
  dbus_connection_send_with_reply (app->bus, message, &new_pending, 2000);
396
166
  dbus_message_unref (message);
397
166
  if (!new_pending)
398
    return;
399
166
  dbus_pending_call_set_notify (new_pending, handle_get_items, app, NULL);
400
}
401

            
402
static AtspiApplication *
403
7358
get_application (const char *bus_name)
404
{
405
7358
  AtspiApplication *app = NULL;
406
  char *bus_name_dup;
407
  DBusMessage *message;
408
7358
  DBusPendingCall *pending = NULL;
409

            
410
7358
  if (!app_hash)
411
    {
412
2
      app_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_object_unref);
413
    }
414
7358
  app = g_hash_table_lookup (app_hash, bus_name);
415
7358
  if (app)
416
7192
    return app;
417
166
  bus_name_dup = g_strdup (bus_name);
418

            
419
  // TODO: change below to something that will send state-change:defunct notification if necessary */
420
166
  app = _atspi_application_new (bus_name);
421
166
  app->bus = dbus_connection_ref (_atspi_bus ());
422
166
  gettimeofday (&app->time_added, NULL);
423
166
  app->cache = ATSPI_CACHE_UNDEFINED;
424
166
  g_hash_table_insert (app_hash, bus_name_dup, app);
425
166
  message = dbus_message_new_method_call (bus_name, atspi_path_root,
426
                                          atspi_interface_application, "GetApplicationBusAddress");
427

            
428
166
  dbus_connection_send_with_reply (app->bus, message, &pending, 2000);
429
166
  dbus_message_unref (message);
430
166
  if (!pending)
431
    {
432
      g_hash_table_remove (app_hash, bus_name_dup);
433
      return NULL;
434
    }
435
166
  dbus_pending_call_set_notify (pending, handle_get_bus_address, app, NULL);
436
166
  return app;
437
}
438

            
439
static AtspiAccessible *
440
3436
ref_accessible (ReferenceFromMessage *ref)
441
{
442
  AtspiApplication *app;
443
  AtspiAccessible *a;
444

            
445
3436
  if (!strcmp (ref->path, ATSPI_DBUS_PATH_NULL))
446
133
    return NULL;
447

            
448
3303
  app = get_application (ref->app_name);
449

            
450
3303
  if (!strcmp (ref->path, "/org/a11y/atspi/accessible/root"))
451
    {
452
865
      if (!app->root)
453
        {
454
324
          app->root = _atspi_accessible_new (app, atspi_path_root);
455
324
          app->root->accessible_parent = atspi_get_desktop (0);
456
324
          g_ptr_array_add (app->root->accessible_parent->children, g_object_ref (app->root));
457
        }
458
865
      return g_object_ref (app->root);
459
    }
460

            
461
2438
  a = g_hash_table_lookup (app->hash, ref->path);
462
2438
  if (a)
463
    {
464
1184
      return g_object_ref (a);
465
    }
466
1254
  a = _atspi_accessible_new (app, ref->path);
467
2508
  g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
468
1254
  return a;
469
}
470

            
471
static AtspiHyperlink *
472
9
ref_hyperlink (const char *app_name, const char *path)
473
{
474
9
  AtspiApplication *app = get_application (app_name);
475
  AtspiHyperlink *hyperlink;
476

            
477
9
  if (!strcmp (path, ATSPI_DBUS_PATH_NULL))
478
    return NULL;
479

            
480
9
  hyperlink = g_hash_table_lookup (app->hash, path);
481
9
  if (hyperlink)
482
    {
483
      return g_object_ref (hyperlink);
484
    }
485
9
  hyperlink = _atspi_hyperlink_new (app, path);
486
18
  g_hash_table_insert (app->hash, g_strdup (hyperlink->parent.path), hyperlink);
487
  /* TODO: This should be a weak ref */
488
9
  g_object_ref (hyperlink); /* for the hash */
489
9
  return hyperlink;
490
}
491

            
492
typedef struct
493
{
494
  char *path;
495
  char *parent;
496
  GArray *children;
497
  GArray *interfaces;
498
  char *name;
499
  dbus_uint32_t role;
500
  char *description;
501
  GArray *state_bitflags;
502
} CACHE_ADDITION;
503

            
504
static DBusHandlerResult
505
handle_remove_accessible (DBusConnection *bus, DBusMessage *message)
506
{
507
  ReferenceFromMessage ref;
508
  AtspiApplication *app;
509
  DBusMessageIter iter;
510
  const char *signature = dbus_message_get_signature (message);
511
  AtspiAccessible *a;
512

            
513
  if (strcmp (signature, "(so)") != 0)
514
    {
515
      g_warning ("AT-SPI: Unknown signature %s for RemoveAccessible", signature);
516
      return DBUS_HANDLER_RESULT_HANDLED;
517
    }
518

            
519
  dbus_message_iter_init (message, &iter);
520

            
521
  get_reference_from_iter (&iter, &ref);
522
  app = get_application (ref.app_name);
523
  a = ref_accessible (&ref);
524
  if (!a)
525
    return DBUS_HANDLER_RESULT_HANDLED;
526
  g_object_run_dispose (G_OBJECT (a));
527
  g_hash_table_remove (app->hash, a->parent.path);
528
  g_object_unref (a); /* unref our own ref */
529
  return DBUS_HANDLER_RESULT_HANDLED;
530
}
531

            
532
static DBusHandlerResult
533
646
handle_name_owner_changed (DBusConnection *bus, DBusMessage *message)
534
{
535
  const char *name, *new, *old;
536
  static gboolean registry_lost = FALSE;
537

            
538
646
  if (!dbus_message_get_args (message, NULL,
539
                              DBUS_TYPE_STRING, &name,
540
                              DBUS_TYPE_STRING, &old,
541
                              DBUS_TYPE_STRING, &new,
542
                              DBUS_TYPE_INVALID))
543
    {
544
      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
545
    }
546

            
547
646
  if (!strcmp (name, "org.a11y.atspi.Registry"))
548
    {
549
1
      if (registry_lost && !old[0])
550
        {
551
          _atspi_reregister_event_listeners ();
552
          _atspi_reregister_device_listeners ();
553
          registry_lost = FALSE;
554
        }
555
1
      else if (!new[0])
556
        registry_lost = TRUE;
557
    }
558
645
  else if (app_hash)
559
    {
560
641
      AtspiApplication *app = g_hash_table_lookup (app_hash, old);
561
641
      if (app && !strcmp (app->bus_name, old))
562
321
        g_object_run_dispose (G_OBJECT (app));
563
    }
564
646
  return DBUS_HANDLER_RESULT_HANDLED;
565
}
566

            
567
static gboolean
568
2
add_app_to_desktop (AtspiAccessible *a, const char *bus_name)
569
{
570
2
  ReferenceFromMessage ref = {
571
    .app_name = bus_name,
572
    .path = atspi_path_root,
573
  };
574
2
  AtspiAccessible *obj = ref_accessible (&ref);
575
  /* The app will be added to the desktop as a side-effect of calling
576
   * ref_accessible */
577
2
  g_object_unref (obj);
578
2
  return (obj != NULL);
579
}
580

            
581
static void
582
1384
add_accessible_from_iter (DBusMessageIter *iter)
583
{
584
  DBusMessageIter iter_struct, iter_array;
585
  AtspiAccessible *accessible;
586
  AtspiAccessible *parent;
587
  const char *name, *description;
588
  dbus_uint32_t role;
589
1384
  gboolean children_cached = FALSE;
590
  dbus_int32_t count, index;
591

            
592
1384
  dbus_message_iter_recurse (iter, &iter_struct);
593

            
594
  /* get accessible */
595
1384
  accessible = _atspi_dbus_consume_accessible (&iter_struct);
596
1384
  if (!accessible)
597
    return;
598

            
599
  /* Get application: TODO */
600
1384
  dbus_message_iter_next (&iter_struct);
601

            
602
  /* get parent */
603
1384
  parent = _atspi_dbus_consume_accessible (&iter_struct);
604
1384
  if (accessible->accessible_parent)
605
130
    g_object_unref (accessible->accessible_parent);
606
1384
  if (parent == accessible)
607
    {
608
      guint pid = atspi_accessible_get_process_id (accessible, NULL);
609
      if (!g_getenv ("ATSPI_IN_TESTS"))
610
        g_warning ("Process %d sent an accessible with itself as its parent. This shouldn't happen.", pid);
611
      accessible->accessible_parent = NULL;
612
    }
613
  else
614
1384
    accessible->accessible_parent = parent;
615

            
616
1384
  if (dbus_message_iter_get_arg_type (&iter_struct) == 'i')
617
    {
618
      /* Get index in parent */
619
1384
      dbus_message_iter_get_basic (&iter_struct, &index);
620
1384
      if (index >= 0 && accessible->accessible_parent)
621
        {
622
1253
          AtspiAccessible *old_child = NULL;
623
1253
          if (index >= accessible->accessible_parent->children->len)
624
            {
625
              /* There is no room for this object */
626
357
              g_ptr_array_set_size (accessible->accessible_parent->children, index + 1);
627
            }
628
          else
629
            {
630
              /* This place is already taken - let's free this place with dignity */
631
896
              if (g_ptr_array_index (accessible->accessible_parent->children, index))
632
                old_child = g_ptr_array_index (accessible->accessible_parent->children, index);
633
            }
634
1253
          g_ptr_array_index (accessible->accessible_parent->children, index) = g_object_ref (accessible);
635
1253
          g_clear_object (&old_child);
636
        }
637

            
638
      /* get child count */
639
1384
      dbus_message_iter_next (&iter_struct);
640
1384
      dbus_message_iter_get_basic (&iter_struct, &count);
641
1384
      if (count >= 0)
642
        {
643
1384
          g_ptr_array_set_size (accessible->children, count);
644
1384
          children_cached = TRUE;
645
        }
646
    }
647
  else if (dbus_message_iter_get_arg_type (&iter_struct) == 'a')
648
    {
649
      /* It's the old API with a list of children */
650
      /* TODO: Perhaps remove this code eventually */
651
      dbus_message_iter_recurse (&iter_struct, &iter_array);
652
      while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
653
        {
654
          AtspiAccessible *child;
655
          child = _atspi_dbus_consume_accessible (&iter_array);
656
          g_ptr_array_remove (accessible->children, child);
657
          g_ptr_array_add (accessible->children, child);
658
        }
659
      children_cached = TRUE;
660
    }
661

            
662
  /* interfaces */
663
1384
  dbus_message_iter_next (&iter_struct);
664
1384
  _atspi_dbus_set_interfaces (accessible, &iter_struct);
665
1384
  dbus_message_iter_next (&iter_struct);
666

            
667
  /* name */
668
1384
  if (accessible->name)
669
131
    g_free (accessible->name);
670
1384
  dbus_message_iter_get_basic (&iter_struct, &name);
671
1384
  accessible->name = g_strdup (name);
672
1384
  dbus_message_iter_next (&iter_struct);
673

            
674
  /* role */
675
1384
  dbus_message_iter_get_basic (&iter_struct, &role);
676
1384
  accessible->role = role;
677
1384
  dbus_message_iter_next (&iter_struct);
678

            
679
  /* description */
680
1384
  if (accessible->description)
681
    g_free (accessible->description);
682
1384
  dbus_message_iter_get_basic (&iter_struct, &description);
683
1384
  accessible->description = g_strdup (description);
684
1384
  dbus_message_iter_next (&iter_struct);
685

            
686
1384
  _atspi_dbus_set_state (accessible, &iter_struct);
687
1384
  dbus_message_iter_next (&iter_struct);
688

            
689
1384
  _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
690
                                               ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
691
1384
  if (!atspi_state_set_contains (accessible->states,
692
1384
                                 ATSPI_STATE_MANAGES_DESCENDANTS) &&
693
      children_cached)
694
1384
    _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
695

            
696
  /* This is a bit of a hack since the cache holds a ref, so we don't need
697
   * the one provided for us anymore */
698
1384
  g_object_unref (accessible);
699
}
700

            
701
static void
702
140
handle_get_items (DBusPendingCall *pending, void *user_data)
703
{
704
140
  DBusMessage *reply = dbus_pending_call_steal_reply (pending);
705
  DBusMessageIter iter, iter_array;
706

            
707
140
  if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
708
    {
709
5
      const char *sender = dbus_message_get_sender (reply);
710
5
      const char *error = NULL;
711
5
      const char *error_name = dbus_message_get_error_name (reply);
712
5
      if (!strcmp (error_name, DBUS_ERROR_SERVICE_UNKNOWN) || !strcmp (error_name, DBUS_ERROR_NO_REPLY))
713
        {
714
        }
715
      else
716
        {
717
          dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &error,
718
                                 DBUS_TYPE_INVALID);
719
          g_warning ("AT-SPI: Error in GetItems, sender=%s, error=%s", sender, error);
720
        }
721
5
      dbus_message_unref (reply);
722
5
      dbus_pending_call_unref (pending);
723
5
      return;
724
    }
725

            
726
135
  dbus_message_iter_init (reply, &iter);
727
135
  dbus_message_iter_recurse (&iter, &iter_array);
728
1519
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
729
    {
730
1384
      add_accessible_from_iter (&iter_array);
731
1384
      dbus_message_iter_next (&iter_array);
732
    }
733
135
  dbus_message_unref (reply);
734
135
  dbus_pending_call_unref (pending);
735
}
736

            
737
/* TODO: Do we stil need this function? */
738
static AtspiAccessible *
739
2
ref_accessible_desktop (AtspiApplication *app)
740
{
741
  GError *error;
742
  DBusMessage *message, *reply;
743
  DBusMessageIter iter, iter_array;
744
  gchar *bus_name_dup;
745

            
746
2
  if (desktop)
747
    {
748
      g_object_ref (desktop);
749
      return desktop;
750
    }
751
2
  desktop = _atspi_accessible_new (app, atspi_path_root);
752
4
  g_hash_table_insert (app->hash, g_strdup (desktop->parent.path),
753
                       g_object_ref (desktop));
754
2
  app->root = g_object_ref (desktop);
755
2
  desktop->name = g_strdup ("main");
756
2
  message = dbus_message_new_method_call (atspi_bus_registry,
757
                                          atspi_path_root,
758
                                          atspi_interface_accessible,
759
                                          "GetChildren");
760
2
  if (!message)
761
    return NULL;
762
2
  error = NULL;
763
2
  reply = _atspi_dbus_send_with_reply_and_block (message, &error);
764
2
  if (!reply || strcmp (dbus_message_get_signature (reply), "a(so)") != 0)
765
    {
766
      if (error != NULL)
767
        {
768
          g_warning ("Couldn't get application list: %s", error->message);
769
          g_clear_error (&error);
770
        }
771
      if (reply)
772
        dbus_message_unref (reply);
773
      return NULL;
774
    }
775
2
  dbus_message_iter_init (reply, &iter);
776
2
  dbus_message_iter_recurse (&iter, &iter_array);
777
4
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
778
    {
779
      ReferenceFromMessage ref;
780

            
781
2
      get_reference_from_iter (&iter_array, &ref);
782
2
      add_app_to_desktop (desktop, ref.app_name);
783
    }
784

            
785
  /* Record the alternate name as an alias for org.a11y.atspi.Registry */
786
2
  bus_name_dup = g_strdup (dbus_message_get_sender (reply));
787
2
  if (bus_name_dup)
788
2
    g_hash_table_insert (app_hash, bus_name_dup, app);
789

            
790
2
  dbus_message_unref (reply);
791

            
792
2
  return g_object_ref (desktop);
793
}
794

            
795
AtspiAccessible *
796
4030
_atspi_ref_accessible (const char *app, const char *path)
797
{
798
4030
  ReferenceFromMessage ref = {
799
    .app_name = app,
800
    .path = path,
801
  };
802
4030
  AtspiApplication *a = get_application (app);
803
4030
  if (!a)
804
    return NULL;
805
4030
  if (APP_IS_REGISTRY (a))
806
    {
807
4028
      if (!a->root)
808
2
        g_object_unref (ref_accessible_desktop (a)); /* sets a->root */
809
4028
      return g_object_ref (a->root);
810
    }
811
2
  return ref_accessible (&ref);
812
}
813

            
814
AtspiAccessible *
815
312
_atspi_dbus_return_accessible_from_message (DBusMessage *message)
816
{
817
  DBusMessageIter iter;
818
312
  AtspiAccessible *retval = NULL;
819
  const char *signature;
820

            
821
312
  if (!message)
822
    return NULL;
823

            
824
312
  signature = dbus_message_get_signature (message);
825
312
  if (!strcmp (signature, "(so)"))
826
    {
827
312
      dbus_message_iter_init (message, &iter);
828
312
      retval = _atspi_dbus_consume_accessible (&iter);
829
    }
830
  else
831
    {
832
      g_warning ("AT-SPI: Called _atspi_dbus_return_accessible_from_message with strange signature %s", signature);
833
    }
834
312
  dbus_message_unref (message);
835
312
  return retval;
836
}
837

            
838
/* Enters an iter which must be already pointing to a (so) and returns the accessible for it */
839
AtspiAccessible *
840
3432
_atspi_dbus_consume_accessible (DBusMessageIter *iter)
841
{
842
  ReferenceFromMessage ref;
843

            
844
3432
  get_reference_from_iter (iter, &ref);
845
3432
  return ref_accessible (&ref);
846
}
847

            
848
AtspiHyperlink *
849
9
_atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
850
{
851
  DBusMessageIter iter;
852
9
  AtspiHyperlink *retval = NULL;
853
  const char *signature;
854

            
855
9
  if (!message)
856
    return NULL;
857

            
858
9
  signature = dbus_message_get_signature (message);
859
9
  if (!strcmp (signature, "(so)"))
860
    {
861
9
      dbus_message_iter_init (message, &iter);
862
9
      retval = _atspi_dbus_return_hyperlink_from_iter (&iter);
863
    }
864
  else
865
    {
866
      g_warning ("AT-SPI: Called _atspi_dbus_return_hyperlink_from_message with strange signature %s", signature);
867
    }
868
9
  dbus_message_unref (message);
869
9
  return retval;
870
}
871

            
872
AtspiHyperlink *
873
9
_atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
874
{
875
  ReferenceFromMessage ref;
876

            
877
9
  get_reference_from_iter (iter, &ref);
878
9
  return ref_hyperlink (ref.app_name, ref.path);
879
}
880

            
881
const char *cache_signal_type = "((so)(so)(so)iiassusau)";
882
const char *old_cache_signal_type = "((so)(so)(so)a(so)assusau)";
883

            
884
static DBusHandlerResult
885
handle_add_accessible (DBusConnection *bus, DBusMessage *message)
886
{
887
  DBusMessageIter iter;
888
  const char *signature = dbus_message_get_signature (message);
889

            
890
  if (strcmp (signature, cache_signal_type) != 0 &&
891
      strcmp (signature, old_cache_signal_type) != 0)
892
    {
893
      g_warning ("AT-SPI: AddAccessible with unknown signature %s\n", signature);
894
      return DBUS_HANDLER_RESULT_HANDLED;
895
    }
896

            
897
  dbus_message_iter_init (message, &iter);
898
  add_accessible_from_iter (&iter);
899
  return DBUS_HANDLER_RESULT_HANDLED;
900
}
901

            
902
typedef struct
903
{
904
  DBusConnection *bus;
905
  DBusMessage *message;
906
} BusDataClosure;
907

            
908
static GSource *process_deferred_messages_source = NULL;
909

            
910
static void
911
969
process_deferred_message (BusDataClosure *closure)
912
{
913
969
  int type = dbus_message_get_type (closure->message);
914
969
  const char *interface = dbus_message_get_interface (closure->message);
915

            
916
969
  if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
917
969
      !strncmp (interface, "org.a11y.atspi.Event.", 21))
918
    {
919
323
      _atspi_dbus_handle_event (closure->message);
920
    }
921
646
  else if (dbus_message_is_method_call (closure->message, atspi_interface_device_event_listener, "NotifyEvent"))
922
    {
923
      _atspi_dbus_handle_DeviceEvent (closure->bus, closure->message);
924
    }
925
646
  else if (dbus_message_is_signal (closure->message, atspi_interface_cache, "AddAccessible"))
926
    {
927
      handle_add_accessible (closure->bus, closure->message);
928
    }
929
646
  else if (dbus_message_is_signal (closure->message, atspi_interface_cache, "RemoveAccessible"))
930
    {
931
      handle_remove_accessible (closure->bus, closure->message);
932
    }
933
646
  else if (dbus_message_is_signal (closure->message, "org.freedesktop.DBus", "NameOwnerChanged"))
934
    {
935
646
      handle_name_owner_changed (closure->bus, closure->message);
936
    }
937
969
}
938

            
939
static GQueue *deferred_messages = NULL;
940

            
941
static void
942
969
destroy_deferred_message_item (gpointer ptr)
943
{
944
969
  BusDataClosure *c = ptr;
945
969
  dbus_message_unref (c->message);
946
969
  dbus_connection_unref (c->bus);
947
969
  g_free (c);
948
969
}
949

            
950
static void
951
2
cleanup_deferred_message (void)
952
{
953
2
  if (deferred_messages)
954
    {
955
2
      g_queue_free_full (deferred_messages, destroy_deferred_message_item);
956
2
      deferred_messages = NULL;
957
    }
958
2
}
959

            
960
static gboolean
961
1651
process_deferred_messages (void)
962
{
963
  static int in_process_deferred_messages = 0;
964
  BusDataClosure *closure;
965

            
966
1651
  if (in_process_deferred_messages)
967
493
    return TRUE;
968
1158
  in_process_deferred_messages = 1;
969
2127
  while ((closure = g_queue_pop_head (deferred_messages)))
970
    {
971
969
      process_deferred_message (closure);
972
969
      destroy_deferred_message_item (closure);
973
    }
974
1158
  in_process_deferred_messages = 0;
975
1158
  return FALSE;
976
}
977

            
978
static gboolean
979
658
process_deferred_messages_callback (gpointer data)
980
{
981
658
  if (process_deferred_messages ())
982
    return G_SOURCE_CONTINUE;
983

            
984
658
  process_deferred_messages_source = NULL;
985
658
  return G_SOURCE_REMOVE;
986
}
987

            
988
static DBusHandlerResult
989
969
defer_message (DBusConnection *connection, DBusMessage *message)
990
{
991
969
  BusDataClosure *closure = g_new (BusDataClosure, 1);
992

            
993
969
  closure->bus = dbus_connection_ref (bus);
994
969
  closure->message = dbus_message_ref (message);
995

            
996
969
  g_queue_push_tail (deferred_messages, closure);
997

            
998
969
  if (process_deferred_messages_source == NULL)
999
    {
658
      process_deferred_messages_source = g_idle_source_new ();
658
      g_source_set_callback (process_deferred_messages_source,
                             process_deferred_messages_callback, NULL, NULL);
658
      g_source_attach (process_deferred_messages_source, atspi_main_context);
658
      g_source_unref (process_deferred_messages_source);
    }
969
  return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult
972
atspi_dbus_filter (DBusConnection *bus, DBusMessage *message, void *data)
{
  /* Check that we don't start passing stuff from whatever calls dbus_connection_add_filter() */
972
  g_assert (data == NULL);
972
  int type = dbus_message_get_type (message);
972
  const char *interface = dbus_message_get_interface (message);
972
  if (type == DBUS_MESSAGE_TYPE_SIGNAL &&
971
      !strncmp (interface, "org.a11y.atspi.Event.", 21))
    {
323
      return defer_message (bus, message);
    }
649
  if (dbus_message_is_method_call (message, atspi_interface_device_event_listener, "NotifyEvent"))
    {
      return defer_message (bus, message);
    }
649
  if (dbus_message_is_signal (message, atspi_interface_cache, "AddAccessible"))
    {
      return defer_message (bus, message);
    }
649
  if (dbus_message_is_signal (message, atspi_interface_cache, "RemoveAccessible"))
    {
      return defer_message (bus, message);
    }
649
  if (dbus_message_is_signal (message, "org.freedesktop.DBus", "NameOwnerChanged"))
    {
646
      defer_message (bus, message);
646
      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    }
3
  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
/**
 * atspi_init:
 *
 * Connects to the accessibility registry and initializes the SPI.
 *
 * Returns: 0 on success, 1 if already initialized, or an integer error code.
 **/
int
2
atspi_init (void)
{
  char *match;
  const gchar *no_cache;
2
  if (atspi_inited)
    {
      return 1;
    }
2
  atspi_inited = TRUE;
2
  _atspi_get_live_refs ();
2
  bus = atspi_get_a11y_bus ();
2
  if (!bus)
    return 2;
2
  dbus_bus_register (bus, NULL);
2
  atspi_dbus_connection_setup_with_g_main (bus, g_main_context_default ());
2
  dbus_connection_add_filter (bus, atspi_dbus_filter, NULL, NULL);
2
  match = g_strdup_printf ("type='signal',interface='%s',member='AddAccessible'", atspi_interface_cache);
2
  dbus_bus_add_match (bus, match, NULL);
2
  g_free (match);
2
  match = g_strdup_printf ("type='signal',interface='%s',member='RemoveAccessible'", atspi_interface_cache);
2
  dbus_bus_add_match (bus, match, NULL);
2
  g_free (match);
2
  match = g_strdup_printf ("type='signal',interface='%s',member='ChildrenChanged'", atspi_interface_event_object);
2
  dbus_bus_add_match (bus, match, NULL);
2
  g_free (match);
2
  match = g_strdup_printf ("type='signal',interface='%s',member='PropertyChange'", atspi_interface_event_object);
2
  dbus_bus_add_match (bus, match, NULL);
2
  g_free (match);
2
  match = g_strdup_printf ("type='signal',interface='%s',member='StateChanged'", atspi_interface_event_object);
2
  dbus_bus_add_match (bus, match, NULL);
2
  g_free (match);
2
  dbus_bus_add_match (bus,
                      "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged'",
                      NULL);
2
  no_cache = g_getenv ("ATSPI_NO_CACHE");
2
  if (no_cache && g_strcmp0 (no_cache, "0") != 0)
    atspi_no_cache = TRUE;
2
  deferred_messages = g_queue_new ();
2
  return 0;
}
/**
 * atspi_is_initialized:
 *
 * Indicates whether AT-SPI has been initialized.
 *
 * Returns: %True if initialized; %False otherwise.
 */
gboolean
161
atspi_is_initialized ()
{
161
  return atspi_inited;
}
/**
 * atspi_event_main:
 *
 * Starts/enters the main event loop for the AT-SPI services.
 *
 * NOTE: This method does not return control; it is exited via a call to
 * #atspi_event_quit from within an event handler.
 *
 **/
void
162
atspi_event_main (void)
{
162
  atspi_main_loop = g_main_loop_new (NULL, FALSE);
162
  g_main_loop_run (atspi_main_loop);
161
  g_main_loop_unref (atspi_main_loop);
161
  atspi_main_loop = NULL;
161
}
/**
 * atspi_event_quit:
 *
 * Quits the last main event loop for the AT-SPI services,
 * See: #atspi_event_main
 **/
void
162
atspi_event_quit (void)
{
162
  if (atspi_main_loop)
162
    g_main_loop_quit (atspi_main_loop);
162
}
/**
 * atspi_exit:
 *
 * Disconnects from #AtspiRegistry instances and releases
 * any floating resources. Call only once at exit.
 *
 * Returns: 0 if there were no leaks, otherwise other integer values.
 **/
int
2
atspi_exit (void)
{
  int leaked;
2
  if (!atspi_inited)
    {
      return 0;
    }
2
  atspi_inited = FALSE;
2
  if (live_refs)
    {
2
      leaked = g_hash_table_size (live_refs);
    }
  else
    {
      leaked = 0;
    }
2
  cleanup ();
2
  return leaked;
}
static GSList *hung_processes;
static void
remove_hung_process (DBusPendingCall *pending, void *data)
{
  hung_processes = g_slist_remove (hung_processes, data);
  g_free (data);
  dbus_pending_call_unref (pending);
}
static void
977
check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
{
977
  if (!message && error->name &&
      !strcmp (error->name, "org.freedesktop.DBus.Error.NoReply"))
    {
      GSList *l;
      DBusMessage *message;
      gchar *bus_name_dup;
      DBusPendingCall *pending = NULL;
      for (l = hung_processes; l; l = l->next)
        if (!strcmp (l->data, bus_name))
          return;
      message = dbus_message_new_method_call (bus_name, "/",
                                              "org.freedesktop.DBus.Peer",
                                              "Ping");
      if (!message)
        return;
      dbus_connection_send_with_reply (bus, message, &pending, -1);
      dbus_message_unref (message);
      if (!pending)
        return;
      bus_name_dup = g_strdup (bus_name);
      hung_processes = g_slist_append (hung_processes, bus_name_dup);
      dbus_pending_call_set_notify (pending, remove_hung_process, bus_name_dup, NULL);
    }
}
static gboolean
490
connection_is_hung (const char *bus_name)
{
  GSList *l;
490
  for (l = hung_processes; l; l = l->next)
    if (!strcmp (l->data, bus_name))
      return TRUE;
490
  return FALSE;
}
static gboolean
977
check_app (AtspiApplication *app, GError **error)
{
977
  if (!app || !app->bus)
    {
      g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
                           _ ("The application no longer exists"));
      return FALSE;
    }
977
  if (atspi_main_loop && connection_is_hung (app->bus_name))
    {
      g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC,
                           "The process appears to be hung.");
      return FALSE;
    }
977
  return TRUE;
}
static void
993
set_timeout (AtspiApplication *app)
{
  struct timeval tv;
  int diff;
993
  if (app && app_startup_time > 0)
    {
993
      gettimeofday (&tv, NULL);
993
      diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
993
      dbind_set_timeout (MAX (method_call_timeout, app_startup_time - diff));
    }
  else
    dbind_set_timeout (method_call_timeout);
993
}
/* Makes a DBus call and returns a success value.  Simple return values can be demarshaled automatically
 * by passing their types after a "=>" marker in the @type argument (e.g. s=>i for a call that takes a string
 * and returns an int).
 */
dbus_bool_t
137
_atspi_dbus_call (gpointer obj, const char *interface, const char *method, GError **error, const char *type, ...)
{
  va_list args;
  dbus_bool_t retval;
  DBusError err;
137
  AtspiObject *aobj = ATSPI_OBJECT (obj);
137
  if (!check_app (aobj->app, error))
    return FALSE;
137
  va_start (args, type);
137
  dbus_error_init (&err);
137
  set_timeout (aobj->app);
137
  retval = dbind_method_call_reentrant_va (aobj->app->bus, aobj->app->bus_name,
137
                                           aobj->path, interface, method, &err,
                                           type, args);
137
  va_end (args);
137
  check_for_hang (NULL, &err, aobj->app->bus, aobj->app->bus_name);
137
  process_deferred_messages ();
137
  if (dbus_error_is_set (&err))
    {
      g_set_error (error, ATSPI_ERROR, ATSPI_ERROR_IPC, "%s", err.message);
      dbus_error_free (&err);
    }
137
  return retval;
}
static DBusMessage *
465
_atspi_dbus_call_partial_va (gpointer obj,
                             const char *interface,
                             const char *method,
                             GError **error,
                             const char *type,
                             va_list args)
{
465
  AtspiObject *aobj = ATSPI_OBJECT (obj);
  DBusError err;
465
  DBusMessage *msg = NULL, *reply = NULL;
  DBusMessageIter iter;
  const char *p;
465
  dbus_error_init (&err);
465
  if (!check_app (aobj->app, error))
    goto out;
465
  msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
465
  if (!msg)
    goto out;
465
  p = type;
465
  dbus_message_iter_init_append (msg, &iter);
465
  dbind_any_marshal_va (&iter, &p, args);
465
  set_timeout (aobj->app);
465
  reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
465
  check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
465
out:
465
  if (msg)
465
    dbus_message_unref (msg);
465
  process_deferred_messages ();
465
  if (dbus_error_is_set (&err))
    {
      /* TODO: Set gerror */
      dbus_error_free (&err);
    }
465
  if (reply && dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
    {
      const char *err_str = NULL;
      dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
      if (err_str)
        g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
      dbus_message_unref (reply);
      return NULL;
    }
465
  return reply;
}
/* Makes a DBus call but returns the raw DBusMessage reply.  Use this
 * when you need to demarshal complex return values by hand.  Remember
 * to dbus_message_unref() the return value when you are done.
 */
DBusMessage *
465
_atspi_dbus_call_partial (gpointer obj,
                          const char *interface,
                          const char *method,
                          GError **error,
                          const char *type,
                          ...)
{
  DBusMessage *ret;
  va_list args;
465
  va_start (args, type);
465
  ret = _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
465
  va_end (args);
465
  return ret;
}
dbus_bool_t
375
_atspi_dbus_get_property (gpointer obj, const char *interface, const char *name, GError **error, const char *type, void *data)
{
  DBusMessage *message, *reply;
  DBusMessageIter iter, iter_variant;
  DBusError err;
375
  dbus_bool_t retval = FALSE;
375
  AtspiObject *aobj = ATSPI_OBJECT (obj);
375
  char expected_type = (type[0] == '(' ? 'r' : type[0]);
375
  if (!aobj)
    return FALSE;
375
  if (!check_app (aobj->app, error))
    return FALSE;
375
  message = dbus_message_new_method_call (aobj->app->bus_name,
375
                                          aobj->path,
                                          "org.freedesktop.DBus.Properties",
                                          "Get");
375
  if (!message)
    {
      // TODO: throw exception
      return FALSE;
    }
375
  dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
375
  dbus_error_init (&err);
375
  set_timeout (aobj->app);
375
  reply = dbind_send_and_allow_reentry (aobj->app->bus, message, &err);
375
  check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
375
  dbus_message_unref (message);
375
  process_deferred_messages ();
375
  if (!reply)
    {
      /* TODO: throw exception */
      goto done;
    }
375
  if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
    {
      const char *err_str = NULL;
      dbus_message_get_args (reply, NULL, DBUS_TYPE_STRING, &err_str, DBUS_TYPE_INVALID);
      if (err_str)
        g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err_str);
      goto done;
    }
375
  dbus_message_iter_init (reply, &iter);
375
  if (dbus_message_iter_get_arg_type (&iter) != 'v')
    {
      g_warning ("atspi_dbus_get_property: expected a variant when fetching %s:%s; got %s instead", interface, name, dbus_message_get_signature (reply));
      goto done;
    }
375
  dbus_message_iter_recurse (&iter, &iter_variant);
375
  if (dbus_message_iter_get_arg_type (&iter_variant) != expected_type)
    {
      g_warning ("atspi_dbus_get_property: Wrong type: expected %s when fetching %s:%s; got %c instead", type, interface, name, dbus_message_iter_get_arg_type (&iter_variant));
      goto done;
    }
375
  if (!strcmp (type, "(so)"))
    {
3
      *((AtspiAccessible **) data) = _atspi_dbus_consume_accessible (&iter_variant);
    }
  else
    {
372
      if (type[0] == 's')
        {
174
          *(char **) data = NULL;
        }
372
      dbus_message_iter_get_basic (&iter_variant, data);
372
      if (type[0] == 's')
348
        *(char **) data = g_strdup (*(char **) data);
    }
375
  retval = TRUE;
375
done:
375
  dbus_error_free (&err);
375
  if (reply)
375
    dbus_message_unref (reply);
375
  return retval;
}
DBusMessage *
16
_atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
{
  DBusMessage *reply;
  DBusError err;
  AtspiApplication *app;
  DBusConnection *bus;
16
  app = get_application (dbus_message_get_destination (message));
16
  if (app && !app->bus)
    return NULL; /* will fail anyway; app has been disposed */
16
  bus = (app ? app->bus : _atspi_bus ());
16
  dbus_error_init (&err);
16
  set_timeout (app);
16
  reply = dbind_send_and_allow_reentry (bus, message, &err);
16
  process_deferred_messages ();
16
  dbus_message_unref (message);
16
  if (dbus_error_is_set (&err))
    {
      if (error)
        g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
      dbus_error_free (&err);
    }
16
  return reply;
}
GHashTable *
3
_atspi_dbus_return_hash_from_message (DBusMessage *message)
{
  DBusMessageIter iter;
  GHashTable *ret;
3
  if (!message)
    return NULL;
3
  _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
3
  dbus_message_iter_init (message, &iter);
3
  ret = _atspi_dbus_hash_from_iter (&iter);
3
  dbus_message_unref (message);
3
  return ret;
}
GHashTable *
5
_atspi_dbus_hash_from_iter (DBusMessageIter *iter)
{
5
  GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
                                            (GDestroyNotify) g_free,
                                            (GDestroyNotify) g_free);
  DBusMessageIter iter_array, iter_dict;
5
  dbus_message_iter_recurse (iter, &iter_array);
16
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
    {
      const char *name, *value;
11
      dbus_message_iter_recurse (&iter_array, &iter_dict);
11
      dbus_message_iter_get_basic (&iter_dict, &name);
11
      dbus_message_iter_next (&iter_dict);
11
      dbus_message_iter_get_basic (&iter_dict, &value);
22
      g_hash_table_insert (hash, g_strdup (name), g_strdup (value));
11
      dbus_message_iter_next (&iter_array);
    }
5
  return hash;
}
GArray *
1
_atspi_dbus_return_attribute_array_from_message (DBusMessage *message)
{
  DBusMessageIter iter;
  GArray *ret;
1
  if (!message)
    return NULL;
1
  _ATSPI_DBUS_CHECK_SIG (message, "a{ss}", NULL, NULL);
1
  dbus_message_iter_init (message, &iter);
1
  ret = _atspi_dbus_attribute_array_from_iter (&iter);
1
  dbus_message_unref (message);
1
  return ret;
}
GArray *
1
_atspi_dbus_attribute_array_from_iter (DBusMessageIter *iter)
{
  DBusMessageIter iter_array, iter_dict;
1
  GArray *array = g_array_new (TRUE, TRUE, sizeof (gchar *));
1
  dbus_message_iter_recurse (iter, &iter_array);
3
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
    {
      const char *name, *value;
      gchar *str;
2
      dbus_message_iter_recurse (&iter_array, &iter_dict);
2
      dbus_message_iter_get_basic (&iter_dict, &name);
2
      dbus_message_iter_next (&iter_dict);
2
      dbus_message_iter_get_basic (&iter_dict, &value);
2
      str = g_strdup_printf ("%s:%s", name, value);
2
      array = g_array_append_val (array, str);
2
      dbus_message_iter_next (&iter_array);
      ;
    }
1
  return array;
}
typedef enum
{
  DEMARSHAL_STATUS_SUCCESS,
  DEMARSHAL_STATUS_INVALID_SIGNATURE,
  DEMARSHAL_STATUS_INVALID_VALUE,
} DemarshalStatus;
typedef struct
{
  /* array of (char *) */
  GPtrArray *names;
} InterfaceNames;
static DemarshalStatus
1506
interface_names_demarshal (DBusMessageIter *iter, InterfaceNames **out_interfaces)
{
1506
  char *sig = dbus_message_iter_get_signature (iter);
1506
  gboolean matches = strcmp (sig, "as") == 0;
1506
  dbus_free (sig);
1506
  *out_interfaces = NULL;
1506
  GPtrArray *names = g_ptr_array_new_with_free_func (g_free);
1506
  if (!matches)
    {
      return DEMARSHAL_STATUS_INVALID_SIGNATURE;
    }
  DBusMessageIter iter_array;
1506
  dbus_message_iter_recurse (iter, &iter_array);
5215
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
    {
      const char *iface;
3709
      dbus_message_iter_get_basic (&iter_array, &iface);
7418
      g_ptr_array_add (names, g_strdup (iface));
3709
      dbus_message_iter_next (&iter_array);
    }
1506
  InterfaceNames *ifaces = g_new0 (InterfaceNames, 1);
1506
  ifaces->names = names;
1506
  *out_interfaces = ifaces;
1506
  return DEMARSHAL_STATUS_SUCCESS;
}
/* Converts an array of interface names to a value suitable for AtspiAccessible.interfaces */
static gint
1506
interface_names_to_bitmask (const InterfaceNames *ifaces)
{
1506
  gint val = 0;
  guint i;
1506
  g_assert (ifaces->names != NULL);
5215
  for (i = 0; i < ifaces->names->len; i++)
    {
3709
      const char *name = g_ptr_array_index (ifaces->names, i);
3709
      gint iface_num = _atspi_get_iface_num (name);
3709
      if (iface_num == -1)
        {
          g_warning ("AT-SPI: Unknown interface %s", name);
        }
      else
        {
3709
          val |= (1 << iface_num);
        }
    }
1506
  return val;
}
static void
1506
interface_names_free (InterfaceNames *ifaces)
{
1506
  g_ptr_array_free (ifaces->names, TRUE);
1506
  g_free (ifaces);
1506
}
void
1506
_atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
{
  InterfaceNames *ifaces;
1506
  accessible->interfaces = 0;
1506
  if (interface_names_demarshal (iter, &ifaces) != DEMARSHAL_STATUS_SUCCESS)
    {
      g_warning ("Passed iterator with invalid signature");
      return;
    }
1506
  accessible->interfaces = interface_names_to_bitmask (ifaces);
1506
  interface_names_free (ifaces);
1506
  _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
}
void
1395
_atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
{
  DBusMessageIter iter_array;
  gint count;
  dbus_uint32_t *states;
1395
  dbus_message_iter_recurse (iter, &iter_array);
1395
  dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1395
  if (count != 2)
    {
      g_warning ("AT-SPI: expected 2 values in states array; got %d\n", count);
      if (!accessible->states)
        accessible->states = _atspi_state_set_new_internal (accessible, 0);
    }
  else
    {
1395
      guint64 val = ((guint64) states[1]) << 32;
1395
      val += states[0];
1395
      if (!accessible->states)
1384
        accessible->states = _atspi_state_set_new_internal (accessible, val);
      else
11
        accessible->states->states = val;
    }
1395
  _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1395
}
GQuark
_atspi_error_quark (void)
{
  return g_quark_from_static_string ("atspi_error");
}
/*
 * Gets the IOR from the XDisplay.
 */
#ifdef HAVE_X11
/*
 * Returns a 'canonicalized' value for DISPLAY,
 * with the screen number stripped off if present.
 *
 * TODO: Avoid having duplicate functions for this here and in at-spi2-atk
 */
static gchar *
191
spi_display_name (void)
{
191
  char *canonical_display_name = NULL;
191
  const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
191
  if (!display_env)
    {
191
      display_env = g_getenv ("DISPLAY");
191
      if (!display_env || !display_env[0])
        return NULL;
      else
        {
          gchar *display_p, *screen_p;
191
          canonical_display_name = g_strdup (display_env);
191
          display_p = g_utf8_strrchr (canonical_display_name, -1, ':');
191
          screen_p = g_utf8_strrchr (canonical_display_name, -1, '.');
191
          if (screen_p && display_p && (screen_p > display_p))
            {
              *screen_p = '\0';
            }
        }
    }
  else
    {
      canonical_display_name = g_strdup (display_env);
    }
191
  return canonical_display_name;
}
static char *
191
get_accessibility_bus_address_x11 (void)
{
  Atom AT_SPI_BUS;
  Atom actual_type;
191
  Display *bridge_display = NULL;
  int actual_format;
  char *data;
191
  unsigned char *data_x11 = NULL;
  unsigned long nitems;
  unsigned long leftover;
  char *display_name;
191
  display_name = spi_display_name ();
191
  if (!display_name)
    return NULL;
191
  bridge_display = XOpenDisplay (display_name);
191
  g_free (display_name);
191
  if (!bridge_display)
    {
      g_warning ("Could not open X display");
      return NULL;
    }
191
  AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
191
  XGetWindowProperty (bridge_display,
                      XDefaultRootWindow (bridge_display),
                      AT_SPI_BUS, 0L,
                      (long) BUFSIZ, False,
                      (Atom) 31, &actual_type, &actual_format,
                      &nitems, &leftover, &data_x11);
191
  XCloseDisplay (bridge_display);
191
  data = g_strdup ((gchar *) data_x11);
191
  XFree (data_x11);
191
  return data;
}
#endif
static char *
191
get_accessibility_bus_address_dbus (void)
{
191
  DBusConnection *session_bus = NULL;
  DBusMessage *message;
  DBusMessage *reply;
  DBusError error;
191
  char *address = NULL;
191
  session_bus = dbus_bus_get (DBUS_BUS_SESSION, NULL);
191
  if (!session_bus)
    return NULL;
191
  message = dbus_message_new_method_call ("org.a11y.Bus",
                                          "/org/a11y/bus",
                                          "org.a11y.Bus",
                                          "GetAddress");
191
  dbus_error_init (&error);
191
  reply = dbus_connection_send_with_reply_and_block (session_bus,
                                                     message,
                                                     -1,
                                                     &error);
191
  dbus_message_unref (message);
191
  if (!reply)
    {
      g_warning ("AT-SPI: Error retrieving accessibility bus address: %s: %s",
                 error.name, error.message);
      dbus_error_free (&error);
      goto out;
    }
  {
    const char *tmp_address;
191
    if (!dbus_message_get_args (reply,
                                NULL,
                                DBUS_TYPE_STRING,
                                &tmp_address,
                                DBUS_TYPE_INVALID))
      {
        dbus_message_unref (reply);
        goto out;
      }
191
    address = g_strdup (tmp_address);
191
    dbus_message_unref (reply);
  }
191
out:
191
  dbus_connection_unref (session_bus);
191
  return address;
}
static DBusConnection *a11y_bus;
static dbus_int32_t a11y_dbus_slot = -1;
static void
191
a11y_bus_free (void *data)
{
191
  if (data == a11y_bus)
    {
191
      a11y_bus = NULL;
191
      dbus_connection_free_data_slot (&a11y_dbus_slot);
    }
191
}
/**
 * atspi_get_a11y_bus: (skip)
 */
DBusConnection *
191
atspi_get_a11y_bus (void)
{
  DBusError error;
191
  char *address = NULL;
191
  const char *address_env = NULL;
191
  if (a11y_bus && dbus_connection_get_is_connected (a11y_bus))
    return a11y_bus;
191
  if (a11y_dbus_slot == -1)
191
    if (!dbus_connection_allocate_data_slot (&a11y_dbus_slot))
      g_warning ("AT-SPI: Unable to allocate D-Bus slot");
191
  address_env = g_getenv ("AT_SPI_BUS_ADDRESS");
191
  if (address_env != NULL && *address_env != 0)
    address = g_strdup (address_env);
#ifdef HAVE_X11
382
  if (!address && g_getenv ("DISPLAY") != NULL &&
191
      g_getenv ("WAYLAND_DISPLAY") == NULL)
191
    address = get_accessibility_bus_address_x11 ();
#endif
191
  if (!address)
191
    address = get_accessibility_bus_address_dbus ();
191
  if (!address)
    return NULL;
191
  dbus_error_init (&error);
191
  a11y_bus = dbus_connection_open_private (address, &error);
191
  g_free (address);
191
  if (!a11y_bus)
    {
      if (!g_getenv ("SSH_CONNECTION"))
        g_warning ("Couldn't connect to accessibility bus: %s", error.message);
      dbus_error_free (&error);
      return NULL;
    }
  else
    {
191
      if (!dbus_bus_register (a11y_bus, &error))
        {
          g_warning ("Couldn't register with accessibility bus: %s", error.message);
          dbus_error_free (&error);
          dbus_connection_close (a11y_bus);
          dbus_connection_unref (a11y_bus);
          a11y_bus = NULL;
          return NULL;
        }
    }
  /* Simulate a weak ref on the bus */
191
  dbus_connection_set_data (a11y_bus, a11y_dbus_slot, a11y_bus, a11y_bus_free);
191
  return a11y_bus;
}
/**
 * atspi_set_timeout:
 * @val: The timeout value, in milliseconds, or -1 to disable the timeout.
 * @startup_time: The amount of time, in milliseconds, to allow to pass
 * before enforcing timeouts on an application. Can be used to prevent
 * timeout exceptions if an application is likely to block for an extended
 * period of time on initialization. -1 can be passed to disable this
 * behavior.
 *
 * Set the timeout used for method calls. If this is not set explicitly,
 * a default of 800 ms is used.
 * Note that at-spi2-registryd currently uses a timeout of 3 seconds when
 * sending a keyboard event notification. This means that, if an AT makes
 * a call in response to the keyboard notification and the application
 * being called does not respond before the timeout is reached,
 * at-spi2-registryd will time out on the keyboard event notification and
 * pass the key onto the application (ie, reply to indicate that the key
 * was not consumed), so this may make it undesirable to set a timeout
 * larger than 3 seconds.
 *
 * By default, the normal timeout is set to 800 ms, and the application startup
 * timeout is set to 15 seconds.
 */
void
atspi_set_timeout (gint val, gint startup_time)
{
  method_call_timeout = val;
  app_startup_time = startup_time;
}
/**
 * atspi_set_main_context:
 * @cnx: The #GMainContext to use.
 *
 * Sets the main loop context that AT-SPI should assume is in use when
 * setting an idle callback.
 * This function should be called by application-side implementors (ie,
 * at-spi2-atk) when it is desirable to re-enter the main loop.
 */
void
atspi_set_main_context (GMainContext *cnx)
{
  if (atspi_main_context == cnx)
    return;
  if (process_deferred_messages_source != NULL)
    {
      g_source_destroy (process_deferred_messages_source);
      process_deferred_messages_source = g_idle_source_new ();
      g_source_set_callback (process_deferred_messages_source,
                             process_deferred_messages_callback, NULL, NULL);
      g_source_attach (process_deferred_messages_source, cnx);
      g_source_unref (process_deferred_messages_source);
    }
  atspi_main_context = cnx;
  atspi_dbus_connection_setup_with_g_main (atspi_get_a11y_bus (), cnx);
  if (desktop)
    {
      gint i;
      for (i = desktop->children->len - 1; i >= 0; i--)
        {
          AtspiAccessible *child = g_ptr_array_index (desktop->children, i);
          if (child->parent.app && child->parent.app->bus)
            atspi_dbus_connection_setup_with_g_main (child->parent.app->bus, cnx);
        }
    }
}
#ifdef DEBUG_REF_COUNTS
static void
print_disposed (gpointer key, gpointer value, gpointer data)
{
  AtspiAccessible *accessible = key;
  if (accessible->parent.app)
    return;
  g_print ("disposed: %s %d\n", accessible->name, accessible->role);
}
void
debug_disposed ()
{
  g_hash_table_foreach (live_refs, print_disposed, NULL);
}
#endif
gchar *
2
_atspi_name_compat (gchar *name)
{
2
  gchar *p = name;
36
  while (*p)
    {
34
      if (*p == '-')
2
        *p = ' ';
34
      p++;
    }
2
  return name;
}
/**
 * atspi_role_get_name:
 * @role: an #AtspiRole object to query.
 *
 * Gets a localizable string that indicates the name of an #AtspiRole.
 *
 * Returns: a localizable string name for an #AtspiRole enumerated type.
 **/
gchar *
2
atspi_role_get_name (AtspiRole role)
{
2
  gchar *retval = NULL;
  GTypeClass *type_class;
  GEnumValue *value;
2
  type_class = g_type_class_ref (ATSPI_TYPE_ROLE);
2
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
2
  value = g_enum_get_value (G_ENUM_CLASS (type_class), role);
2
  if (value)
    {
4
      retval = g_strdup (value->value_nick);
    }
2
  g_type_class_unref (type_class);
2
  if (retval)
2
    return _atspi_name_compat (retval);
  return NULL;
}
/**
 * atspi_role_get_localized_name:
 * @role: an #AtspiRole object to query.
 *
 * Gets the localized description string describing the #AtspiRole @role.
 *
 * Returns: the localized string describing the AtspiRole
 **/
gchar *
1
atspi_role_get_localized_name (AtspiRole role)
{
1
  gchar *raw_name = NULL;
  const char *translated_name;
1
  _gettext_initialization ();
1
  raw_name = atspi_role_get_name (role);
1
  translated_name = dgettext (GETTEXT_PACKAGE, raw_name);
1
  if (translated_name != raw_name)
    {
      g_free (raw_name);
      return g_strdup (translated_name);
    }
  else
1
    return raw_name;
}
GHashTable *
323
_atspi_dbus_update_cache_from_dict (AtspiAccessible *accessible, DBusMessageIter *iter)
{
323
  GHashTable *cache = _atspi_accessible_ref_cache (accessible);
  DBusMessageIter iter_dict, iter_dict_entry, iter_struct, iter_variant;
323
  dbus_message_iter_recurse (iter, &iter_dict);
323
  while (dbus_message_iter_get_arg_type (&iter_dict) != DBUS_TYPE_INVALID)
    {
      const char *key;
      GValue *val = NULL;
      dbus_message_iter_recurse (&iter_dict, &iter_dict_entry);
      dbus_message_iter_get_basic (&iter_dict_entry, &key);
      dbus_message_iter_next (&iter_dict_entry);
      dbus_message_iter_recurse (&iter_dict_entry, &iter_variant);
      if (!strcmp (key, "interfaces"))
        {
          _atspi_dbus_set_interfaces (accessible, &iter_variant);
        }
      else if (!strcmp (key, "Attributes"))
        {
          char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
          val = g_new0 (GValue, 1);
          ;
          g_value_init (val, G_TYPE_HASH_TABLE);
          if (strcmp (iter_sig, "a{ss}") != 0)
            {
              dbus_free (iter_sig);
              break;
            }
          dbus_free (iter_sig);
          g_value_take_boxed (val, _atspi_dbus_hash_from_iter (&iter_variant));
        }
      else if (!strcmp (key, "Component.ScreenExtents"))
        {
          dbus_int32_t d_int;
          AtspiRect extents;
          char *iter_sig = dbus_message_iter_get_signature (&iter_variant);
          val = g_new0 (GValue, 1);
          ;
          g_value_init (val, ATSPI_TYPE_RECT);
          if (strcmp (iter_sig, "(iiii)") != 0)
            {
              dbus_free (iter_sig);
              break;
            }
          dbus_free (iter_sig);
          dbus_message_iter_recurse (&iter_variant, &iter_struct);
          dbus_message_iter_get_basic (&iter_struct, &d_int);
          extents.x = d_int;
          dbus_message_iter_next (&iter_struct);
          dbus_message_iter_get_basic (&iter_struct, &d_int);
          extents.y = d_int;
          dbus_message_iter_next (&iter_struct);
          dbus_message_iter_get_basic (&iter_struct, &d_int);
          extents.width = d_int;
          dbus_message_iter_next (&iter_struct);
          dbus_message_iter_get_basic (&iter_struct, &d_int);
          extents.height = d_int;
          g_value_set_boxed (val, &extents);
        }
      if (val)
        g_hash_table_insert (cache, g_strdup (key), val);
      dbus_message_iter_next (&iter_dict);
    }
323
  return cache;
}
static const char *sr_introspection = "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
                                      "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
                                      "<node name=\"/org/a11y/atspi/screenreader\">\n"
                                      "  <interface name=\"org.a11y.Atspi.ScreenReader\">\n"
                                      "    <signal name=\"ReadingPosition\">\n"
                                      "      <arg type=\"i\"/>\n"
                                      "      <arg type=\"i\"/>\n"
                                      "    </signal>\n"
                                      "  </interface>\n"
                                      "</node>";
static DBusHandlerResult
screen_reader_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
{
  if (dbus_message_is_method_call (message, DBUS_INTERFACE_INTROSPECTABLE,
                                   "Introspect"))
    {
      DBusMessage *reply = dbus_message_new_method_return (message);
      dbus_message_append_args (reply, DBUS_TYPE_STRING, &sr_introspection,
                                DBUS_TYPE_INVALID);
      dbus_connection_send (bus, reply, NULL);
      dbus_message_unref (reply);
      return DBUS_HANDLER_RESULT_HANDLED;
    }
  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
gboolean
_atspi_prepare_screen_reader_interface ()
{
  static gint initialized = 0;
  DBusConnection *a11y_bus = _atspi_bus ();
  if (initialized)
    return (initialized > 0);
  if (dbus_bus_request_name (a11y_bus, "org.a11y.Atspi.ScreenReader", 0, NULL) < 0)
    {
      initialized = -1;
      return FALSE;
    }
  initialized = 1;
  dbus_connection_add_filter (a11y_bus, screen_reader_filter, NULL, NULL);
  return TRUE;
}
gchar *
3480
_atspi_strdup_and_adjust_for_dbus (const char *s)
{
3480
  gchar *d = g_strdup (s);
  gchar *p;
3480
  int parts = 0;
3480
  if (!d)
    return NULL;
70575
  for (p = d; *p; p++)
    {
70573
      if (*p == '-')
        {
3480
          memmove (p, p + 1, g_utf8_strlen (p, -1));
3480
          *p = toupper (*p);
        }
67093
      else if (*p == ':')
        {
6958
          parts++;
6958
          if (parts == 2)
3478
            break;
3480
          p[1] = toupper (p[1]);
        }
    }
3480
  d[0] = toupper (d[0]);
3480
  return d;
}
/**
 * atspi_get_version:
 * @major: (out): the major version.
 * @minor: (out): the minor version.
 * @micro: (out): the micro/patch version.
 *
 * Returns the version of the AT-SPI library being used at runtime.
* Since: 2.50
 */
void
atspi_get_version (gint *major, gint *minor, gint *micro)
{
  if (major)
    *major = ATSPI_MAJOR_VERSION;
  if (minor)
    *minor = ATSPI_MINOR_VERSION;
  if (micro)
    *micro = ATSPI_MICRO_VERSION;
}
gboolean
_atspi_key_is_on_keypad (gint keycode)
{
  switch (keycode)
    {
    case 106: /* / */
    case 63:  /* * */
    case 82:  /* - */
    case 79:  /* 7 */
    case 80:  /* 8 */
    case 91:  /* 9 */
    case 86:  /* + */
    case 83:  /* 4 */
    case 84:  /* 5 */
    case 85:  /* 6 */
    case 87:  /* 1 */
    case 88:  /* 2 */
    case 89:  /* 3 */
    case 104: /* enter */
    case 90:  /* 0 */
    case 81:  /* . */
      return TRUE;
    default:
      return FALSE;
    }
}