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_("push 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_("table")
106
  N_("table cell")
107
  N_("table column header")
108
  N_("table row header")
109
  N_("tear off menu item")
110
  N_("terminal")
111
  N_("text")
112
  N_("toggle button")
113
  N_("tool bar")
114
  N_("tool tip")
115
  N_("tree")
116
  N_("tree table")
117
  N_("unknown")
118
  N_("viewport")
119
  N_("window")
120
  N_("header")
121
  N_("footer")
122
  N_("paragraph")
123
  N_("ruler")
124
  N_("application")
125
  N_("autocomplete")
126
  N_("edit bar")
127
  N_("embedded component")
128
  N_("entry")
129
  N_("chart")
130
  N_("caption")
131
  N_("document frame")
132
  N_("heading")
133
  N_("page")
134
  N_("section")
135
  N_("redundant object")
136
  N_("form")
137
  N_("link")
138
  N_("input method window")
139
  N_("table row")
140
  N_("tree item")
141
  N_("document spreadsheet")
142
  N_("document presentation")
143
  N_("document text")
144
  N_("document web")
145
  N_("document email")
146
  N_("comment")
147
  N_("list box")
148
  N_("grouping")
149
  N_("image map")
150
  N_("notification")
151
  N_("info bar")
152
  N_("level bar")
153
  N_("title bar")
154
  N_("block quote")
155
  N_("audio")
156
  N_("video")
157
  N_("definition")
158
  N_("article")
159
  N_("landmark")
160
  N_("log")
161
  N_("marquee")
162
  N_("math")
163
  N_("rating")
164
  N_("timer")
165
  N_("description list")
166
  N_("description term")
167
  N_("description value")
168
#endif /* 0 */
169

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
298
static AtspiAccessible *desktop;
299

            
300
static void cleanup_deferred_message (void);
301

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

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

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

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

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

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

            
339
2
  cleanup_deferred_message ();
340
2
}
341

            
342
static gboolean atspi_inited = FALSE;
343

            
344
static GHashTable *app_hash = NULL;
345

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

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

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

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

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

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

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

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

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

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

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

            
444
3509
  if (!strcmp (ref->path, ATSPI_DBUS_PATH_NULL))
445
142
    return NULL;
446

            
447
3367
  app = get_application (ref->app_name);
448

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

            
460
2485
  a = g_hash_table_lookup (app->hash, ref->path);
461
2485
  if (a)
462
    {
463
1201
      return g_object_ref (a);
464
    }
465
1284
  a = _atspi_accessible_new (app, ref->path);
466
2568
  g_hash_table_insert (app->hash, g_strdup (a->parent.path), g_object_ref (a));
467
1284
  return a;
468
}
469

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

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

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

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

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

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

            
518
  dbus_message_iter_init (message, &iter);
519

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

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

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

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

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

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

            
591
1419
  dbus_message_iter_recurse (iter, &iter_struct);
592

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

            
598
  /* Get application: TODO */
599
1419
  dbus_message_iter_next (&iter_struct);
600

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

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

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

            
659
  /* interfaces */
660
1419
  dbus_message_iter_next (&iter_struct);
661
1419
  _atspi_dbus_set_interfaces (accessible, &iter_struct);
662
1419
  dbus_message_iter_next (&iter_struct);
663

            
664
  /* name */
665
1419
  if (accessible->name)
666
136
    g_free (accessible->name);
667
1419
  dbus_message_iter_get_basic (&iter_struct, &name);
668
1419
  accessible->name = g_strdup (name);
669
1419
  dbus_message_iter_next (&iter_struct);
670

            
671
  /* role */
672
1419
  dbus_message_iter_get_basic (&iter_struct, &role);
673
1419
  accessible->role = role;
674
1419
  dbus_message_iter_next (&iter_struct);
675

            
676
  /* description */
677
1419
  if (accessible->description)
678
1
    g_free (accessible->description);
679
1419
  dbus_message_iter_get_basic (&iter_struct, &description);
680
1419
  accessible->description = g_strdup (description);
681
1419
  dbus_message_iter_next (&iter_struct);
682

            
683
1419
  _atspi_dbus_set_state (accessible, &iter_struct);
684
1419
  dbus_message_iter_next (&iter_struct);
685

            
686
1419
  _atspi_accessible_add_cache (accessible, ATSPI_CACHE_NAME | ATSPI_CACHE_ROLE |
687
                                               ATSPI_CACHE_PARENT | ATSPI_CACHE_DESCRIPTION);
688
1419
  if (!atspi_state_set_contains (accessible->states,
689
1419
                                 ATSPI_STATE_MANAGES_DESCENDANTS) &&
690
      children_cached)
691
1419
    _atspi_accessible_add_cache (accessible, ATSPI_CACHE_CHILDREN);
692

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

            
698
static void
699
142
handle_get_items (DBusPendingCall *pending, void *user_data)
700
{
701
142
  DBusMessage *reply = dbus_pending_call_steal_reply (pending);
702
  DBusMessageIter iter, iter_array;
703

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

            
723
140
  dbus_message_iter_init (reply, &iter);
724
140
  dbus_message_iter_recurse (&iter, &iter_array);
725
1559
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
726
    {
727
1419
      add_accessible_from_iter (&iter_array);
728
1419
      dbus_message_iter_next (&iter_array);
729
    }
730
140
  dbus_message_unref (reply);
731
140
  dbus_pending_call_unref (pending);
732
}
733

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

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

            
778
2
      get_reference_from_iter (&iter_array, &ref);
779
2
      add_app_to_desktop (desktop, ref.app_name);
780
    }
781

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

            
787
2
  dbus_message_unref (reply);
788

            
789
2
  return g_object_ref (desktop);
790
}
791

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

            
811
AtspiAccessible *
812
313
_atspi_dbus_return_accessible_from_message (DBusMessage *message)
813
{
814
  DBusMessageIter iter;
815
313
  AtspiAccessible *retval = NULL;
816
  const char *signature;
817

            
818
313
  if (!message)
819
    return NULL;
820

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

            
835
/* Enters an iter which must be already pointing to a (so) and returns the accessible for it */
836
AtspiAccessible *
837
3505
_atspi_dbus_consume_accessible (DBusMessageIter *iter)
838
{
839
  ReferenceFromMessage ref;
840

            
841
3505
  get_reference_from_iter (iter, &ref);
842
3505
  return ref_accessible (&ref);
843
}
844

            
845
AtspiHyperlink *
846
9
_atspi_dbus_return_hyperlink_from_message (DBusMessage *message)
847
{
848
  DBusMessageIter iter;
849
9
  AtspiHyperlink *retval = NULL;
850
  const char *signature;
851

            
852
9
  if (!message)
853
    return NULL;
854

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

            
869
AtspiHyperlink *
870
9
_atspi_dbus_return_hyperlink_from_iter (DBusMessageIter *iter)
871
{
872
  ReferenceFromMessage ref;
873

            
874
9
  get_reference_from_iter (iter, &ref);
875
9
  return ref_hyperlink (ref.app_name, ref.path);
876
}
877

            
878
const char *cache_signal_type = "((so)(so)(so)iiassusau)";
879
const char *old_cache_signal_type = "((so)(so)(so)a(so)assusau)";
880

            
881
static DBusHandlerResult
882
handle_add_accessible (DBusConnection *bus, DBusMessage *message)
883
{
884
  DBusMessageIter iter;
885
  const char *signature = dbus_message_get_signature (message);
886

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

            
894
  dbus_message_iter_init (message, &iter);
895
  add_accessible_from_iter (&iter);
896
  return DBUS_HANDLER_RESULT_HANDLED;
897
}
898

            
899
typedef struct
900
{
901
  DBusConnection *bus;
902
  DBusMessage *message;
903
} BusDataClosure;
904

            
905
static GSource *process_deferred_messages_source = NULL;
906

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

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

            
936
static GQueue *deferred_messages = NULL;
937

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

            
947
static void
948
2
cleanup_deferred_message (void)
949
{
950
2
  if (deferred_messages)
951
    {
952
2
      g_queue_free_full (deferred_messages, destroy_deferred_message_item);
953
2
      deferred_messages = NULL;
954
    }
955
2
}
956

            
957
static gboolean
958
1708
process_deferred_messages (void)
959
{
960
  static int in_process_deferred_messages = 0;
961
  BusDataClosure *closure;
962

            
963
1708
  if (in_process_deferred_messages)
964
494
    return TRUE;
965
1214
  in_process_deferred_messages = 1;
966
2183
  while ((closure = g_queue_pop_head (deferred_messages)))
967
    {
968
969
      process_deferred_message (closure);
969
969
      destroy_deferred_message_item (closure);
970
    }
971
1214
  in_process_deferred_messages = 0;
972
1214
  return FALSE;
973
}
974

            
975
static gboolean
976
717
process_deferred_messages_callback (gpointer data)
977
{
978
717
  if (process_deferred_messages ())
979
    return G_SOURCE_CONTINUE;
980

            
981
717
  process_deferred_messages_source = NULL;
982
717
  return G_SOURCE_REMOVE;
983
}
984

            
985
static DBusHandlerResult
986
969
defer_message (DBusConnection *connection, DBusMessage *message)
987
{
988
969
  BusDataClosure *closure = g_new (BusDataClosure, 1);
989

            
990
969
  closure->bus = dbus_connection_ref (bus);
991
969
  closure->message = dbus_message_ref (message);
992

            
993
969
  g_queue_push_tail (deferred_messages, closure);
994

            
995
969
  if (process_deferred_messages_source == NULL)
996
    {
997
717
      process_deferred_messages_source = g_idle_source_new ();
998
717
      g_source_set_callback (process_deferred_messages_source,
999
                             process_deferred_messages_callback, NULL, NULL);
717
      g_source_attach (process_deferred_messages_source, atspi_main_context);
717
      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
  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
978
check_for_hang (DBusMessage *message, DBusError *error, DBusConnection *bus, const char *bus_name)
{
978
  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
491
connection_is_hung (const char *bus_name)
{
  GSList *l;
491
  for (l = hung_processes; l; l = l->next)
    if (!strcmp (l->data, bus_name))
      return TRUE;
491
  return FALSE;
}
static gboolean
978
check_app (AtspiApplication *app, GError **error)
{
978
  if (!app || !app->bus)
    {
      g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_APPLICATION_GONE,
                           _ ("The application no longer exists"));
      return FALSE;
    }
978
  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;
    }
978
  return TRUE;
}
static void
991
set_timeout (AtspiApplication *app)
{
  struct timeval tv;
  int diff;
991
  if (app && app_startup_time > 0)
    {
991
      gettimeofday (&tv, NULL);
991
      diff = (tv.tv_sec - app->time_added.tv_sec) * 1000 + (tv.tv_usec - app->time_added.tv_usec) / 1000;
991
      dbind_set_timeout (MAX (method_call_timeout, app_startup_time - diff));
    }
  else
    dbind_set_timeout (method_call_timeout);
991
}
/* 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 *
466
_atspi_dbus_call_partial_va (gpointer obj,
                             const char *interface,
                             const char *method,
                             GError **error,
                             const char *type,
                             va_list args)
{
466
  AtspiObject *aobj = ATSPI_OBJECT (obj);
  DBusError err;
466
  DBusMessage *msg = NULL, *reply = NULL;
  DBusMessageIter iter;
  const char *p;
466
  dbus_error_init (&err);
466
  if (!check_app (aobj->app, error))
    goto out;
466
  msg = dbus_message_new_method_call (aobj->app->bus_name, aobj->path, interface, method);
466
  if (!msg)
    goto out;
466
  p = type;
466
  dbus_message_iter_init_append (msg, &iter);
466
  dbind_any_marshal_va (&iter, &p, args);
466
  set_timeout (aobj->app);
466
  reply = dbind_send_and_allow_reentry (aobj->app->bus, msg, &err);
466
  check_for_hang (reply, &err, aobj->app->bus, aobj->app->bus_name);
466
out:
466
  if (msg)
466
    dbus_message_unref (msg);
466
  process_deferred_messages ();
466
  if (dbus_error_is_set (&err))
    {
      /* TODO: Set gerror */
      dbus_error_free (&err);
    }
466
  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;
    }
466
  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 *
466
_atspi_dbus_call_partial (gpointer obj,
                          const char *interface,
                          const char *method,
                          GError **error,
                          const char *type,
                          ...)
{
  DBusMessage *ret;
  va_list args;
466
  va_start (args, type);
466
  ret = _atspi_dbus_call_partial_va (obj, interface, method, error, type, args);
466
  va_end (args);
466
  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 from interface %s; got %s\n", name, interface, 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, got %c\n", type, 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 *
13
_atspi_dbus_send_with_reply_and_block (DBusMessage *message, GError **error)
{
  DBusMessage *reply;
  DBusError err;
  AtspiApplication *app;
  DBusConnection *bus;
13
  app = get_application (dbus_message_get_destination (message));
13
  if (app && !app->bus)
    return NULL; /* will fail anyway; app has been disposed */
13
  bus = (app ? app->bus : _atspi_bus ());
13
  dbus_error_init (&err);
13
  set_timeout (app);
13
  reply = dbind_send_and_allow_reentry (bus, message, &err);
13
  process_deferred_messages ();
13
  dbus_message_unref (message);
13
  if (dbus_error_is_set (&err))
    {
      if (error)
        g_set_error_literal (error, ATSPI_ERROR, ATSPI_ERROR_IPC, err.message);
      dbus_error_free (&err);
    }
13
  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
1541
interface_names_demarshal (DBusMessageIter *iter, InterfaceNames **out_interfaces)
{
1541
  char *sig = dbus_message_iter_get_signature (iter);
1541
  gboolean matches = strcmp (sig, "as") == 0;
1541
  dbus_free (sig);
1541
  *out_interfaces = NULL;
1541
  GPtrArray *names = g_ptr_array_new_with_free_func (g_free);
1541
  if (!matches)
    {
      return DEMARSHAL_STATUS_INVALID_SIGNATURE;
    }
  DBusMessageIter iter_array;
1541
  dbus_message_iter_recurse (iter, &iter_array);
5307
  while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
    {
      const char *iface;
3766
      dbus_message_iter_get_basic (&iter_array, &iface);
7532
      g_ptr_array_add (names, g_strdup (iface));
3766
      dbus_message_iter_next (&iter_array);
    }
1541
  InterfaceNames *ifaces = g_new0 (InterfaceNames, 1);
1541
  ifaces->names = names;
1541
  *out_interfaces = ifaces;
1541
  return DEMARSHAL_STATUS_SUCCESS;
}
/* Converts an array of interface names to a value suitable for AtspiAccessible.interfaces */
static gint
1541
interface_names_to_bitmask (const InterfaceNames *ifaces)
{
1541
  gint val = 0;
  guint i;
1541
  g_assert (ifaces->names != NULL);
5307
  for (i = 0; i < ifaces->names->len; i++)
    {
3766
      const char *name = g_ptr_array_index (ifaces->names, i);
3766
      gint iface_num = _atspi_get_iface_num (name);
3766
      if (iface_num == -1)
        {
          g_warning ("AT-SPI: Unknown interface %s", name);
        }
      else
        {
3766
          val |= (1 << iface_num);
        }
    }
1541
  return val;
}
static void
1541
interface_names_free (InterfaceNames *ifaces)
{
1541
  g_ptr_array_free (ifaces->names, TRUE);
1541
  g_free (ifaces);
1541
}
void
1541
_atspi_dbus_set_interfaces (AtspiAccessible *accessible, DBusMessageIter *iter)
{
  InterfaceNames *ifaces;
1541
  accessible->interfaces = 0;
1541
  if (interface_names_demarshal (iter, &ifaces) != DEMARSHAL_STATUS_SUCCESS)
    {
      g_warning ("Passed iterator with invalid signature");
      return;
    }
1541
  accessible->interfaces = interface_names_to_bitmask (ifaces);
1541
  interface_names_free (ifaces);
1541
  _atspi_accessible_add_cache (accessible, ATSPI_CACHE_INTERFACES);
}
void
1430
_atspi_dbus_set_state (AtspiAccessible *accessible, DBusMessageIter *iter)
{
  DBusMessageIter iter_array;
  gint count;
  dbus_uint32_t *states;
1430
  dbus_message_iter_recurse (iter, &iter_array);
1430
  dbus_message_iter_get_fixed_array (&iter_array, &states, &count);
1430
  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
    {
1430
      guint64 val = ((guint64) states[1]) << 32;
1430
      val += states[0];
1430
      if (!accessible->states)
1419
        accessible->states = _atspi_state_set_new_internal (accessible, val);
      else
11
        accessible->states->states = val;
    }
1430
  _atspi_accessible_add_cache (accessible, ATSPI_CACHE_STATES);
1430
}
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
190
a11y_bus_free (void *data)
{
190
  if (data == a11y_bus)
    {
190
      a11y_bus = NULL;
190
      dbus_connection_free_data_slot (&a11y_dbus_slot);
    }
190
}
/**
 * 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 *
3540
_atspi_strdup_and_adjust_for_dbus (const char *s)
{
3540
  gchar *d = g_strdup (s);
  gchar *p;
3540
  int parts = 0;
3540
  if (!d)
    return NULL;
71775
  for (p = d; *p; p++)
    {
71773
      if (*p == '-')
        {
3540
          memmove (p, p + 1, g_utf8_strlen (p, -1));
3540
          *p = toupper (*p);
        }
68233
      else if (*p == ':')
        {
7078
          parts++;
7078
          if (parts == 2)
3538
            break;
3540
          p[1] = toupper (p[1]);
        }
    }
3540
  d[0] = toupper (d[0]);
3540
  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;
    }
}