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
      !strncmp (spi_global_app_data->app_bus_addr, "unix:path=", 10))
461
    {
462
      unlink (spi_global_app_data->app_bus_addr + 10);
463
      g_free (spi_global_app_data->app_bus_addr);
464
      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
#ifndef DISABLE_P2P
634
static guint
635
get_ancestral_uid (guint pid)
636
{
637
  FILE *fp;
638
  char buf[80];
639
  int ppid = 0;
640
  int uid = 0;
641
  gboolean got_ppid = 0;
642
  gboolean got_uid = 0;
643

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

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

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

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

            
688
  spi_global_app_data->direct_connections = g_list_append (spi_global_app_data->direct_connections, con);
689
}
690
#endif
691

            
692
static gchar *atspi_dbus_name = NULL;
693
static gboolean atspi_no_register = FALSE;
694

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

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

            
712
  if (!obj)
713
    return;
714

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

            
720
  if (ATK_IS_SOCKET (obj))
721
    return;
722

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

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

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

            
750
  return NULL;
751
}
752

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

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

            
766
  dbus_message_iter_init (message, &iter);
767
  add_event_from_iter (&iter);
768
}
769

            
770
static void
771
free_property_definition (void *data)
772
{
773
  AtspiPropertyDefinition *pd = data;
774

            
775
  g_free (pd->name);
776
  g_free (pd);
777
}
778

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

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

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

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

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

            
815
  g_strfreev (remove_data);
816
}
817

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

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

            
828
  remove_events (sender, name);
829
}
830

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

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

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

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

            
857
1459
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
858
1137
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
859

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

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

            
904
322
  return result;
905
}
906

            
907
int
908
161
spi_atk_create_socket (SpiBridge *app)
909
{
910
#ifndef DISABLE_P2P
911
  DBusServer *server;
912
  DBusError error;
913
  const gchar *user_runtime_dir = g_get_user_runtime_dir ();
914
  char *socket_path;
915
  char *escaped_socket_path;
916

            
917
  if (g_mkdir_with_parents (user_runtime_dir, 0700) != 0)
918
    return -1;
919

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

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

            
942
  escaped_socket_path = dbus_address_escape_value (socket_path);
943
  g_free (socket_path);
944

            
945
  app->app_bus_addr = g_strconcat ("unix:path=", escaped_socket_path, NULL);
946
  dbus_free (escaped_socket_path);
947

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

            
958
  atspi_dbus_server_setup_with_g_main (server, spi_context);
959
  dbus_server_set_new_connection_function (server, new_connection_cb, NULL, NULL);
960

            
961
  app->server = server;
962
#endif
963

            
964
161
  return 0;
965
}
966

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

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

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

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

            
993
161
  spi_atk_register_event_listeners ();
994
161
  if (!spi_global_cache)
995
    {
996
161
      spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
997
161
      treepath = droute_add_one (spi_global_app_data->droute,
998
                                 "/org/a11y/atspi/cache", spi_global_cache);
999

            
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;
    }
161
  for (l = spi_global_app_data->direct_connections; l; l = l->next)
    {
      DBusConnection *connection;
      connection = l->data;
      droute_context_unregister (spi_global_app_data->droute, connection);
      droute_unintercept_dbus (connection);
      dbus_connection_close (connection);
      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
479
spi_atk_add_client (const char *bus_name)
{
  GSList *l;
  gchar *match;
479
  for (l = clients; l; l = l->next)
    {
318
      if (!g_strcmp0 (l->data, bus_name))
318
        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------------------------------------------------------------------------*/