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

            
25
#define _GNU_SOURCE
26
#include "config.h"
27

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

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

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

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

            
51
#include "spi-dbus.h"
52

            
53
/*---------------------------------------------------------------------------*/
54

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

            
58
SpiBridge *spi_global_app_data = NULL;
59

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

            
63
/*---------------------------------------------------------------------------*/
64

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

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

            
85
static GSList *clients = NULL;
86

            
87
static void
88
322
tally_event_reply ()
89
{
90
  static int replies_received = 0;
91

            
92
322
  if (!spi_global_app_data)
93
    return;
94

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

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

            
132
DRoutePropertyFunction
133
_atk_bridge_find_property_func (const char *property, GType *type)
134
{
135
  const char *iface;
136
  const char *member;
137
  DRouteProperty *dp;
138

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

            
175
  *type = _atk_bridge_type_from_iface (iface);
176

            
177
  dp = g_hash_table_lookup (spi_global_app_data->property_hash, iface);
178

            
179
  if (!dp)
180
    return NULL;
181

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

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

            
204
  prop->name = g_strdup (property);
205
  evdata->properties = g_slist_append (evdata->properties, prop);
206
}
207

            
208
static void
209
161
add_event_from_iter (DBusMessageIter *iter)
210
{
211
  const char *bus_name, *event;
212
  event_data *evdata;
213

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

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

            
239
161
  if (!reply || !spi_global_app_data)
240
    goto done;
241

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

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

            
258
161
done:
259
161
  if (reply)
260
161
    dbus_message_unref (reply);
261
161
  if (pending)
262
161
    dbus_pending_call_unref (pending);
263

            
264
161
  tally_event_reply ();
265
161
}
266

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

            
273
161
  if (!reply)
274
    goto done;
275

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

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

            
293
161
done:
294
161
  if (reply)
295
161
    dbus_message_unref (reply);
296
161
  if (pending)
297
161
    dbus_pending_call_unref (pending);
298

            
299
161
  tally_event_reply ();
300
161
}
301

            
302
static void
303
161
get_registered_event_listeners (SpiBridge *app)
304
{
305
  DBusMessage *message;
306
161
  DBusPendingCall *pending = NULL;
307

            
308
161
  message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
309
                                          ATSPI_DBUS_PATH_REGISTRY,
310
                                          ATSPI_DBUS_INTERFACE_REGISTRY,
311
                                          "GetRegisteredEvents");
312
161
  if (!message)
313
    return;
314

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

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

            
341
static void
342
161
register_reply (DBusPendingCall *pending, void *user_data)
343
{
344
  DBusMessage *reply;
345
161
  SpiBridge *app = user_data;
346

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

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

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

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

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

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

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

            
399
161
  spi_global_app_data->registration_pending = 0;
400

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

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

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

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

            
418
161
  dbus_pending_call_set_notify (pending, register_reply, app, NULL);
419

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

            
423
161
  return FALSE;
424
}
425

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

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

            
448
161
  return FALSE;
449
}
450

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

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

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

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

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

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

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

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

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

            
499
161
  remove_socket ();
500

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

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

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

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

            
514
static gchar *
515
get_plug_id (AtkPlug *plug)
516
{
517
  const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
518
  gchar *path;
519
  GString *str = g_string_new (NULL);
520

            
521
  path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
522
  g_string_printf (str, "%s:%s", uname, path);
523
  g_free (path);
524
  return g_string_free (str, FALSE);
525
}
526

            
527
AtkStateSet *
528
socket_ref_state_set (AtkObject *accessible)
529
{
530
  char *child_name, *child_path;
531
  AtkSocket *socket = ATK_SOCKET (accessible);
532
  int count = 0;
533
  int j;
534
  int v;
535
  DBusMessage *message, *reply;
536
  DBusMessageIter iter, iter_array;
537
  AtkStateSet *set;
538

            
539
  set = atk_state_set_new ();
540

            
541
  if (!socket->embedded_plug_id)
542
    return set;
543

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

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

            
586
static void
587
socket_embed_hook (AtkSocket *socket, const gchar *plug_id)
588
{
589
  g_return_if_fail (spi_global_register != NULL);
590

            
591
  AtkObject *accessible = ATK_OBJECT (socket);
592
  gchar *plug_name, *plug_path;
593
  AtkObjectClass *klass;
594

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

            
616
  klass = ATK_OBJECT_GET_CLASS (accessible);
617
  klass->ref_state_set = socket_ref_state_set;
618
}
619

            
620
static void
621
161
install_plug_hooks ()
622
{
623
  gpointer data;
624

            
625
161
  data = g_type_class_ref (ATK_TYPE_PLUG);
626
161
  plug_class = ATK_PLUG_CLASS (data);
627
161
  data = g_type_class_ref (ATK_TYPE_SOCKET);
628
161
  socket_class = ATK_SOCKET_CLASS (data);
629
161
  plug_class->get_object_id = get_plug_id;
630
161
  socket_class->embed = socket_embed_hook;
631
161
}
632

            
633
static guint
634
get_ancestral_uid (guint pid)
635
{
636
  FILE *fp;
637
  char buf[80];
638
  int ppid = 0;
639
  int uid = 0;
640
  gboolean got_ppid = 0;
641
  gboolean got_uid = 0;
642

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

            
656
  if (!got_ppid || !got_uid)
657
    return 0;
658
  if (uid != 0)
659
    return uid;
660
  if (ppid == 0 || ppid == 1)
661
    return 0;
662
  return get_ancestral_uid (ppid);
663
}
664

            
665
static dbus_bool_t
666
131
user_check (DBusConnection *bus, unsigned long uid, void *data)
667
{
668
131
  if (uid == getuid () || uid == geteuid ())
669
131
    return TRUE;
670
  if (getuid () == 0)
671
    {
672
      guint ancestor = get_ancestral_uid (getpid ());
673
      return (ancestor == uid || ancestor == 1 || ancestor == 0);
674
    }
675
  return FALSE;
676
}
677

            
678
static void
679
157
new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
680
{
681
157
  dbus_connection_set_unix_user_function (con, user_check, NULL, NULL);
682
157
  dbus_connection_ref (con);
683
157
  atspi_dbus_connection_setup_with_g_main (con, spi_context);
684
157
  droute_intercept_dbus (con);
685
157
  droute_context_register (spi_global_app_data->droute, con);
686

            
687
157
  spi_global_app_data->direct_connections = g_list_append (spi_global_app_data->direct_connections, con);
688
157
}
689

            
690
static gchar *atspi_dbus_name = NULL;
691
static gboolean atspi_no_register = FALSE;
692

            
693
static GOptionEntry atspi_option_entries[] = {
694
  { "atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
695
    "D-Bus bus name to register as", NULL },
696
  { "atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
697
    "Do not register with Registry Daemon", NULL },
698
  { NULL }
699
};
700

            
701
static void
702
add_objects_for_introspection (AtkObject *obj, GString *str)
703
{
704
  gchar *path;
705
  AtkStateSet *set;
706
  char *p;
707
  gint i;
708
  gint count;
709

            
710
  if (!obj)
711
    return;
712

            
713
  path = spi_register_object_to_path (spi_global_register, G_OBJECT (obj));
714
  p = strrchr (path, '/') + 1;
715
  g_string_append_printf (str, "<node name=\"%s\"/>\n", p);
716
  g_free (path);
717

            
718
  if (ATK_IS_SOCKET (obj))
719
    return;
720

            
721
  set = atk_object_ref_state_set (obj);
722
  if (atk_state_set_contains_state (set, ATK_STATE_MANAGES_DESCENDANTS))
723
    {
724
      g_object_unref (set);
725
      return;
726
    }
727
  g_object_unref (set);
728

            
729
  count = atk_object_get_n_accessible_children (obj);
730
  for (i = 0; i < count; i++)
731
    {
732
      AtkObject *child = atk_object_ref_accessible_child (obj, i);
733
      add_objects_for_introspection (child, str);
734
      g_object_unref (child);
735
    }
736
}
737

            
738
static gchar *
739
introspect_children_cb (const char *path, void *data)
740
{
741
  if (!strcmp (path, "/org/a11y/atspi/accessible"))
742
    {
743
      GString *str = g_string_new (NULL);
744
      add_objects_for_introspection (spi_global_app_data->root, str);
745
      return g_string_free (str, FALSE);
746
    }
747

            
748
  return NULL;
749
}
750

            
751
static void
752
handle_event_listener_registered (DBusConnection *bus, DBusMessage *message, void *user_data)
753
{
754
  DBusMessageIter iter;
755
  const char *signature = dbus_message_get_signature (message);
756

            
757
  if (strcmp (signature, "ssas") != 0 &&
758
      strcmp (signature, "ss") != 0)
759
    {
760
      g_warning ("got RegisterEvent with invalid signature '%s'", signature);
761
      return;
762
    }
763

            
764
  dbus_message_iter_init (message, &iter);
765
  add_event_from_iter (&iter);
766
}
767

            
768
static void
769
free_property_definition (void *data)
770
{
771
  AtspiPropertyDefinition *pd = data;
772

            
773
  g_free (pd->name);
774
  g_free (pd);
775
}
776

            
777
static void
778
remove_events (const char *bus_name, const char *event)
779
{
780
  gchar **remove_data;
781
  GList *list;
782

            
783
  remove_data = g_strsplit (event, ":", 3);
784
  if (!remove_data)
785
    {
786
      return;
787
    }
788

            
789
  for (list = spi_global_app_data->events; list;)
790
    {
791
      event_data *evdata = list->data;
792
      if (!g_strcmp0 (evdata->bus_name, bus_name) &&
793
          spi_event_is_subtype (evdata->data, remove_data))
794
        {
795
          GList *next;
796
          GList *events = spi_global_app_data->events;
797

            
798
          g_strfreev (evdata->data);
799
          g_free (evdata->bus_name);
800
          g_slist_free_full (evdata->properties, free_property_definition);
801
          g_free (evdata);
802

            
803
          next = list->next;
804
          spi_global_app_data->events = g_list_delete_link (events, list);
805
          list = next;
806
        }
807
      else
808
        {
809
          list = list->next;
810
        }
811
    }
812

            
813
  g_strfreev (remove_data);
814
}
815

            
816
static void
817
handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message, void *user_data)
818
{
819
  gchar *name;
820
  char *sender;
821

            
822
  if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
823
                              DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
824
    return;
825

            
826
  remove_events (sender, name);
827
}
828

            
829
static void
830
handle_device_listener_registered (DBusConnection *bus, DBusMessage *message, void *user_data)
831
{
832
  char *sender;
833
  DBusMessageIter iter, iter_struct;
834

            
835
  if (strncmp (dbus_message_get_signature (message), "(s", 2) != 0)
836
    {
837
      g_warning ("atk-bridge: handle_device_listener_register: unknown signature");
838
      return;
839
    }
840

            
841
  dbus_message_iter_init (message, &iter);
842
  dbus_message_iter_recurse (&iter, &iter_struct);
843
  dbus_message_iter_get_basic (&iter_struct, &sender);
844
  spi_atk_add_client (sender);
845
}
846

            
847
static DBusHandlerResult
848
962
signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
849
{
850
962
  const char *interface = dbus_message_get_interface (message);
851
962
  const char *member = dbus_message_get_member (message);
852
962
  DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
853
  static gboolean registry_lost = FALSE;
854

            
855
962
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
856
640
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
857

            
858
322
  if (!strcmp (interface, ATSPI_DBUS_INTERFACE_REGISTRY))
859
    {
860
      result = DBUS_HANDLER_RESULT_HANDLED;
861
      if (!strcmp (member, "EventListenerRegistered"))
862
        handle_event_listener_registered (bus, message, user_data);
863
      else if (!strcmp (member, "EventListenerDeregistered"))
864
        handle_event_listener_deregistered (bus, message, user_data);
865
      else
866
        result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
867
    }
868
322
  else if (!strcmp (interface, ATSPI_DBUS_INTERFACE_DEVICE_EVENT_LISTENER))
869
    {
870
      result = DBUS_HANDLER_RESULT_HANDLED;
871
      if (!strcmp (member, "KeystrokeListenerRegistered"))
872
        handle_device_listener_registered (bus, message, user_data);
873
      else
874
        result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
875
    }
876

            
877
644
  if (!g_strcmp0 (interface, DBUS_INTERFACE_DBUS) &&
878
322
      !g_strcmp0 (member, "NameOwnerChanged"))
879
    {
880
      char *name, *old, *new;
881
      if (dbus_message_get_args (message, NULL,
882
                                 DBUS_TYPE_STRING, &name,
883
                                 DBUS_TYPE_STRING, &old,
884
                                 DBUS_TYPE_STRING, &new,
885
                                 DBUS_TYPE_INVALID))
886
        {
887
          if (!strcmp (name, "org.a11y.atspi.Registry"))
888
            {
889
              if (registry_lost && !old[0])
890
                {
891
                  register_application (spi_global_app_data);
892
                  registry_lost = FALSE;
893
                }
894
              else if (!new[0])
895
                registry_lost = TRUE;
896
            }
897
          else if (*old != '\0' && *new == '\0')
898
            spi_atk_remove_client (old);
899
        }
900
    }
901

            
902
322
  return result;
903
}
904

            
905
int
906
161
spi_atk_create_socket (SpiBridge *app)
907
{
908
  DBusServer *server;
909
  DBusError error;
910
161
  const gchar *user_runtime_dir = g_get_user_runtime_dir ();
911
  char *socket_path;
912
  char *escaped_socket_path;
913
  const char *disable_p2p;
914

            
915
161
  disable_p2p = g_getenv ("ATSPI_DISABLE_P2P");
916
161
  if (disable_p2p && atoi (disable_p2p) > 0)
917
    return 0;
918

            
919
161
  if (g_mkdir_with_parents (user_runtime_dir, 0700) != 0)
920
    return -1;
921

            
922
161
  if (getuid () != 0)
923
    {
924
      app->app_tmp_dir = g_build_filename (user_runtime_dir,
925
                                           "at-spi2-XXXXXX", NULL);
926
      if (!g_mkdtemp (app->app_tmp_dir))
927
        {
928
          g_free (app->app_tmp_dir);
929
          app->app_tmp_dir = NULL;
930
          return -1;
931
        }
932
    }
933

            
934
161
  if (app->app_tmp_dir)
935
    {
936
      socket_path = g_strdup_printf ("%s/socket", app->app_tmp_dir);
937
    }
938
  else
939
    {
940
161
      socket_path = g_strdup_printf ("%s/at-spi2-socket-%d",
941
                                     user_runtime_dir, getpid ());
942
    }
943

            
944
161
  escaped_socket_path = dbus_address_escape_value (socket_path);
945
161
  g_free (socket_path);
946

            
947
161
  app->app_bus_addr = g_strconcat ("unix:path=", escaped_socket_path, NULL);
948
161
  dbus_free (escaped_socket_path);
949

            
950
161
  dbus_error_init (&error);
951
161
  server = dbus_server_listen (app->app_bus_addr, &error);
952
161
  if (server == NULL)
953
    {
954
      g_warning ("atk-bridge: Couldn't listen on dbus server: %s", error.message);
955
      dbus_error_free (&error);
956
      app->app_bus_addr[0] = '\0';
957
      return -1;
958
    }
959

            
960
161
  atspi_dbus_server_setup_with_g_main (server, spi_context);
961
161
  dbus_server_set_new_connection_function (server, new_connection_cb, NULL, NULL);
962

            
963
161
  app->server = server;
964

            
965
161
  return 0;
966
}
967

            
968
/*
969
 * Checks the status of the environment variables
970
 *
971
 * At this moment it only checks NO_AT_BRIDGE
972
 *
973
 * Returns TRUE if there isn't anything on the environment preventing
974
 * you to load the bridge, FALSE otherwise
975
 */
976
static gboolean
977
161
check_envvar (void)
978
{
979
  const gchar *envvar;
980

            
981
161
  envvar = g_getenv ("NO_AT_BRIDGE");
982

            
983
161
  if (envvar && atoi (envvar) == 1)
984
    return FALSE;
985
  else
986
161
    return TRUE;
987
}
988

            
989
void
990
161
spi_atk_activate ()
991
{
992
  DRoutePath *treepath;
993

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