2009-04-23 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / event.c
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 Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 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  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <string.h>
26
27 #include <atk/atk.h>
28 #include <droute/droute.h>
29
30 #include "bridge.h"
31 #include "accessible-register.h"
32
33 #include "spi-common/spi-dbus.h"
34
35 static GArray *listener_ids = NULL;
36
37 static gint atk_bridge_key_event_listener_id;
38 static gint atk_bridge_focus_tracker_id;
39
40 /*---------------------------------------------------------------------------*/
41
42 #define ITF_EVENT_OBJECT   "org.freedesktop.atspi.Event.Object"
43 #define ITF_EVENT_WINDOW   "org.freedesktop.atspi.Event.Window"
44 #define ITF_EVENT_DOCUMENT "org.freedekstop.atspi.Event.Document"
45 #define ITF_EVENT_FOCUS    "org.freedesktop.atspi.Event.Focus"
46
47 /*---------------------------------------------------------------------------*/
48
49 static void
50 set_reply (DBusPendingCall *pending, void *user_data)
51 {
52     void **replyptr = (void **)user_data;
53
54     *replyptr = dbus_pending_call_steal_reply (pending);
55 }
56
57 static DBusMessage *
58 send_and_allow_reentry (DBusConnection *bus, DBusMessage *message)
59 {
60     DBusPendingCall *pending;
61     DBusMessage *reply = NULL;
62
63     if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
64     {
65         return NULL;
66     }
67     dbus_pending_call_set_notify (pending, set_reply, (void *)&reply, NULL);
68     while (!reply)
69     {
70       if (!dbus_connection_read_write_dispatch (bus, -1)) return NULL;
71     }
72     return reply;
73 }
74
75 static gboolean
76 Accessibility_DeviceEventController_notifyListenersSync(const Accessibility_DeviceEvent *key_event)
77 {
78   DBusMessage *message;
79   DBusError error;
80   dbus_bool_t consumed = FALSE;
81
82   message =
83   dbus_message_new_method_call(SPI_DBUS_NAME_REGISTRY, 
84                                SPI_DBUS_PATH_DEC,
85                                SPI_DBUS_INTERFACE_DEC,
86                                "notifyListenersSync");
87
88   dbus_error_init(&error);
89   if (spi_dbus_marshal_deviceEvent(message, key_event))
90   {
91     DBusMessage *reply = send_and_allow_reentry (atk_adaptor_app_data->bus, message);
92     if (reply)
93     {
94       DBusError error;
95       dbus_error_init(&error);
96       dbus_message_get_args(reply, &error, DBUS_TYPE_BOOLEAN, &consumed, DBUS_TYPE_INVALID);
97       dbus_message_unref(reply);
98     }
99   }
100   dbus_message_unref(message);
101   return consumed;
102 }
103
104 static void
105 spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent  *keystroke,
106                                        AtkKeyEventStruct          *event)
107 {
108   keystroke->id        = (dbus_int32_t) event->keyval;
109   keystroke->hw_code   = (dbus_int16_t) event->keycode;
110   keystroke->timestamp = (dbus_uint32_t) event->timestamp;
111   keystroke->modifiers = (dbus_uint16_t) (event->state & 0xFFFF);
112   if (event->string)
113     {
114       gunichar c;
115
116       keystroke->event_string = g_strdup (event->string);
117       c = g_utf8_get_char_validated (event->string, -1);
118       if (c > 0 && g_unichar_isprint (c))
119         keystroke->is_text = TRUE;
120       else
121         keystroke->is_text = FALSE;
122     }
123   else
124     {
125       keystroke->event_string = g_strdup ("");
126       keystroke->is_text = FALSE;
127     }
128   switch (event->type)
129     {
130     case (ATK_KEY_EVENT_PRESS):
131       keystroke->type = Accessibility_KEY_PRESSED_EVENT;
132       break;
133     case (ATK_KEY_EVENT_RELEASE):
134       keystroke->type = Accessibility_KEY_RELEASED_EVENT;
135       break;
136     default:
137       keystroke->type = 0;
138       break;
139     }
140 #if 0  
141   g_print ("key_event type %d; val=%d code=%d modifiers=%x name=%s is_text=%d, time=%lx\n",
142            (int) keystroke->type, (int) keystroke->id, (int) keystroke->hw_code,
143            (int) keystroke->modifiers,
144            keystroke->event_string, (int) keystroke->is_text, (unsigned long) keystroke->timestamp);
145 #endif
146 }
147
148
149 static gint
150 spi_atk_bridge_key_listener (AtkKeyEventStruct *event, gpointer data)
151 {
152   gboolean             result;
153   Accessibility_DeviceEvent key_event;
154
155   spi_init_keystroke_from_atk_key_event (&key_event, event);
156
157   result = Accessibility_DeviceEventController_notifyListenersSync (&key_event);
158
159   if (key_event.event_string) g_free (key_event.event_string);
160
161   return result;
162 }
163
164
165 /*---------------------------------------------------------------------------*/
166
167 /*
168  * Emits an AT-SPI event.
169  * AT-SPI events names are split into three parts:
170  * class:major:minor
171  * This is mapped onto D-Bus events as:
172  * D-Bus Interface:Signal Name:Detail argument
173  *
174  * Marshals a basic type into the 'any_data' attribute of
175  * the AT-SPI event.
176  */
177
178 static void 
179 emit(AtkObject  *accessible,
180      const char *klass,
181      const char *major,
182      const char *minor,
183      dbus_int32_t detail1,
184      dbus_int32_t detail2,
185      const char *type,
186      const void *val)
187 {
188   gchar *path;
189
190   path = atk_dbus_object_to_path (accessible);
191
192   /* Tough decision here
193    * We won't send events from accessible
194    * objects that have not yet been added to the accessible tree.
195    */
196   if (path == NULL)
197       return;
198
199   spi_dbus_emit_signal (atk_adaptor_app_data->bus, path, klass, major, minor, detail1, detail2, type, val);
200   g_free(path);
201 }
202
203 /*---------------------------------------------------------------------------*/
204
205 /*
206  * Emits an AT-SPI event, marshalling a BoundingBox structure into the 
207  * 'any_data' variant of the event.
208  */
209 static void
210 emit_rect(AtkObject  *accessible,
211           const char *klass,
212           const char *major,
213           const char *minor,
214           AtkRectangle *rect)
215 {
216   DBusMessage *sig;
217   DBusMessageIter iter, variant, sub;
218   gchar *path, *cname, *t;
219   dbus_int32_t dummy = 0;
220
221   path = atk_dbus_object_to_path (accessible);
222
223   /* Tough decision here
224    * We won't send events from accessible
225    * objects that have not yet been added to the accessible tree.
226    */
227   if (path == NULL)
228       return;
229
230   if (!klass) klass = "";
231   if (!major) major = "";
232   if (!minor) minor = "";
233
234   /*
235    * This is very annoying, but as '-' isn't a legal signal
236    * name in D-Bus (Why not??!?) The names need converting
237    * on this side, and again on the client side.
238    */
239   cname = g_strdup(major);
240   while ((t = strchr(cname, '-')) != NULL) *t = '_';
241
242   sig = dbus_message_new_signal(path, klass, cname);
243   g_free(path);
244   g_free(cname);
245
246   dbus_message_iter_init_append (sig, &iter);
247   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
248   dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &dummy);
249   dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &dummy);
250
251   dbus_message_iter_open_container (&iter, DBUS_TYPE_VARIANT, "(iiii)", &variant);
252     dbus_message_iter_open_container (&variant, DBUS_TYPE_STRUCT, NULL, &sub);
253       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->x));
254       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->y));
255       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->width));
256       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->height));
257     dbus_message_iter_close_container (&variant, &sub);
258   dbus_message_iter_close_container (&iter, &variant);
259
260   dbus_connection_send(atk_adaptor_app_data->bus, sig, NULL);
261 }
262
263 /*---------------------------------------------------------------------------*/
264
265 /*
266  * The focus listener handles the ATK 'focus' signal and forwards it
267  * as the AT-SPI event, 'focus:'
268  */
269 static void
270 focus_tracker (AtkObject *accessible)
271 {
272   emit(accessible, ITF_EVENT_FOCUS, "focus", "", 0, 0, DBUS_TYPE_INT32_AS_STRING, 0);
273 }
274
275 /*---------------------------------------------------------------------------*/
276
277 #define PCHANGE "property-change"
278
279 /* 
280  * This handler handles the following ATK signals and
281  * converts them to AT-SPI events:
282  *  
283  * Gtk:AtkObject:property-change -> object:property-change:(property-name)
284  *
285  * The property-name is part of the ATK property-change signal.
286  */
287 static gboolean
288 property_event_listener (GSignalInvocationHint *signal_hint,
289                          guint                  n_param_values,
290                          const GValue          *param_values,
291                          gpointer               data)
292 {
293   AtkObject *accessible;
294   AtkPropertyValues *values;
295
296   const gchar *pname = NULL;
297
298   AtkObject *otemp;
299   const gchar *stemp;
300   gint i;
301   
302   accessible = g_value_get_object (&param_values[0]);
303   values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
304
305   pname = values[0].property_name;
306
307   /* TODO Could improve this control statement by matching
308    * on only the end of the signal names,
309    */
310   if (strcmp (pname, "accessible-table-summary") == 0)
311     {
312       otemp = atk_table_get_summary(ATK_TABLE (accessible));
313       stemp = atk_dbus_object_to_path (otemp);
314       if (stemp != NULL)
315           emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
316     }
317   else if (strcmp (pname, "accessible-table-column-header") == 0)
318     {
319       i = g_value_get_int (&(values->new_value));
320       otemp = atk_table_get_column_header(ATK_TABLE (accessible), i);
321       stemp = atk_dbus_object_to_path (otemp);
322       if (stemp != NULL)
323           emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
324     }
325   else if (strcmp (pname, "accessible-table-row-header") == 0)
326     {
327       i = g_value_get_int (&(values->new_value));
328       otemp = atk_table_get_row_header(ATK_TABLE (accessible), i);
329       stemp = atk_dbus_object_to_path (otemp);
330       if (stemp != NULL)
331           emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
332     }
333   else if (strcmp (pname, "accessible-table-row-description") == 0)
334     {
335       i = g_value_get_int (&(values->new_value));
336       stemp = atk_table_get_row_description(ATK_TABLE (accessible), i);
337       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_STRING_AS_STRING, stemp);
338     }
339   else if (strcmp (pname, "accessible-table-column-description") == 0)
340     {
341       i = g_value_get_int (&(values->new_value));
342       stemp = atk_table_get_column_description(ATK_TABLE (accessible), i);
343       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_STRING_AS_STRING, stemp);
344     }
345   else if (strcmp (pname, "accessible-table-caption-object") == 0)
346     {
347       otemp = atk_table_get_caption(ATK_TABLE(accessible));
348       stemp = atk_object_get_name(otemp);
349       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_STRING_AS_STRING, stemp);
350     }
351   else
352     {
353       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_INT32_AS_STRING, 0);
354     }
355   return TRUE;
356 }
357
358 /*---------------------------------------------------------------------------*/
359
360 #define STATE_CHANGED "state-changed"
361
362 /*
363  * The state event listener handles 'Gtk:AtkObject:state-change' ATK signals
364  * and forwards them as object:state-changed:(param-name) AT-SPI events. Where
365  * the param-name is part of the ATK state-change signal.
366  */
367 static gboolean
368 state_event_listener (GSignalInvocationHint *signal_hint,
369                       guint n_param_values,
370                       const GValue *param_values,
371                       gpointer data)
372 {
373   AtkObject *accessible;
374   gchar *pname;
375   guint detail1;
376
377   accessible = ATK_OBJECT(g_value_get_object (&param_values[0]));
378   pname = g_strdup (g_value_get_string (&param_values[1]));
379
380   /* TODO - Possibly ignore a change to the 'defunct' state.
381    * This is because without reference counting defunct objects should be removed.
382    */
383   detail1 = (g_value_get_boolean (&param_values[2])) ? 1 : 0;
384   emit(accessible, ITF_EVENT_OBJECT, STATE_CHANGED, pname, detail1, 0, DBUS_TYPE_INT32_AS_STRING, 0);
385   g_free (pname);
386   return TRUE;
387 }
388
389 /*---------------------------------------------------------------------------*/
390
391 /*
392  * The window event listener handles the following ATK signals and forwards
393  * them as AT-SPI events:
394  *
395  * window:create     -> window:create
396  * window:destroy    -> window:destroy
397  * window:minimize   -> window:minimize
398  * window:maximize   -> window:maximize
399  * window:activate   -> window:activate
400  * window:deactivate -> window:deactivate
401  */
402 static gboolean
403 window_event_listener (GSignalInvocationHint *signal_hint,
404                        guint n_param_values,
405                        const GValue *param_values,
406                        gpointer data)
407 {
408   AtkObject *accessible;
409   GSignalQuery signal_query;
410   const gchar *name, *s;
411   
412   g_signal_query (signal_hint->signal_id, &signal_query);
413   name = signal_query.signal_name;
414
415   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
416   s = atk_object_get_name (accessible);
417   emit(accessible, ITF_EVENT_WINDOW, name, "", 0, 0, DBUS_TYPE_STRING_AS_STRING, s);
418
419   return TRUE;
420 }
421
422 /*---------------------------------------------------------------------------*/
423
424 /* 
425  * The document event listener handles the following ATK signals
426  * and converts them to AT-SPI events:
427  *
428  * Gtk:AtkDocument:load-complete ->  document:load-complete
429  * Gtk:AtkDocument:load-stopped  ->  document:load-stopped
430  * Gtk:AtkDocument:reload        ->  document:reload
431  */
432 static gboolean
433 document_event_listener (GSignalInvocationHint *signal_hint,
434                          guint n_param_values,
435                          const GValue *param_values,
436                          gpointer data)
437 {
438   AtkObject *accessible;
439   GSignalQuery signal_query;
440   const gchar *name, *s;
441
442   g_signal_query (signal_hint->signal_id, &signal_query);
443   name = signal_query.signal_name;
444
445   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
446   s = atk_object_get_name (accessible);
447   emit(accessible, ITF_EVENT_DOCUMENT, name, "", 0, 0, DBUS_TYPE_STRING_AS_STRING, s);
448
449   return TRUE;
450 }
451
452 /*---------------------------------------------------------------------------*/
453
454 /*
455  * Signal handler for  "Gtk:AtkComponent:bounds-changed". Converts
456  * this to an AT-SPI event - "object:bounds-changed".
457  */
458 static gboolean
459 bounds_event_listener (GSignalInvocationHint *signal_hint,
460                        guint n_param_values,
461                        const GValue *param_values,
462                        gpointer data)
463 {
464   AtkObject *accessible;
465   AtkRectangle *atk_rect;
466   GSignalQuery signal_query;
467   const gchar *name, *s;
468
469   g_signal_query (signal_hint->signal_id, &signal_query);
470   name = signal_query.signal_name;
471
472   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
473
474   if (G_VALUE_HOLDS_BOXED (param_values + 1))
475     atk_rect = g_value_get_boxed (param_values + 1);
476
477   emit_rect(accessible, ITF_EVENT_OBJECT, name, "", atk_rect);
478   return TRUE;
479 }
480
481 /*---------------------------------------------------------------------------*/
482
483 /* 
484  * Handles the ATK signal 'Gtk:AtkObject:active-descendant-changed' and 
485  * converts it to the AT-SPI signal - 'object:active-descendant-changed'.
486  *
487  */
488 static gboolean
489 active_descendant_event_listener (GSignalInvocationHint *signal_hint,
490                                   guint n_param_values,
491                                   const GValue *param_values,
492                                   gpointer data)
493 {
494   AtkObject *accessible;
495   AtkObject *child;
496   GSignalQuery signal_query;
497   const gchar *name, *minor;
498   gchar *s;
499   gint detail1;
500
501   g_signal_query (signal_hint->signal_id, &signal_query);
502   name = signal_query.signal_name;
503
504   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
505   child = ATK_OBJECT(g_value_get_pointer (&param_values[1]));
506   g_return_val_if_fail (ATK_IS_OBJECT (child), TRUE);
507   minor = g_quark_to_string (signal_hint->detail);
508
509   detail1 = atk_object_get_index_in_parent (child);
510   s = atk_dbus_object_to_path (child);
511   if (s == NULL)
512     {
513       g_free (s);
514       return TRUE;
515     }
516
517   emit(accessible, ITF_EVENT_OBJECT, name, "", detail1, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, s);
518   g_free(s);
519   return TRUE;
520 }
521
522 /*---------------------------------------------------------------------------*/
523
524 /* 
525  * Handles the ATK signal 'Gtk:AtkHypertext:link-selected' and
526  * converts it to the AT-SPI signal - 'object:link-selected'
527  *
528  */
529 static gboolean
530 link_selected_event_listener (GSignalInvocationHint *signal_hint,
531                               guint n_param_values,
532                               const GValue *param_values,
533                               gpointer data)
534 {
535   AtkObject *accessible;
536   GSignalQuery signal_query;
537   const gchar *name, *minor;
538   gint detail1;
539
540   g_signal_query (signal_hint->signal_id, &signal_query);
541   name = signal_query.signal_name;
542
543   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
544   minor = g_quark_to_string (signal_hint->detail);
545
546   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
547         detail1 = g_value_get_int (&param_values[1]);
548
549   emit(accessible, ITF_EVENT_OBJECT, name, minor, detail1, 0, DBUS_TYPE_INT32_AS_STRING, 0);
550   return TRUE;
551 }
552
553 /*---------------------------------------------------------------------------*/
554
555 /* 
556  * Handles the ATK signal 'Gtk:AtkText:text-changed' and
557  * converts it to the AT-SPI signal - 'object:text-changed'
558  *
559  */
560 static gboolean
561 text_changed_event_listener (GSignalInvocationHint *signal_hint,
562                              guint n_param_values,
563                              const GValue *param_values,
564                              gpointer data)
565 {
566   AtkObject *accessible;
567   GSignalQuery signal_query;
568   const gchar *name, *minor;
569   gchar *selected;
570   gint detail1, detail2;
571
572   g_signal_query (signal_hint->signal_id, &signal_query);
573   name = signal_query.signal_name;
574
575   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
576   minor = g_quark_to_string (signal_hint->detail);
577
578   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
579         detail1 = g_value_get_int (&param_values[1]);
580
581   if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
582         detail2 = g_value_get_int (&param_values[2]);
583
584   selected = atk_text_get_text (ATK_TEXT (accessible), detail1, detail1+detail2);
585
586   emit(accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2, DBUS_TYPE_STRING_AS_STRING, selected);
587   return TRUE;
588 }
589
590 /*---------------------------------------------------------------------------*/
591
592 /* 
593  * Handles the ATK signal 'Gtk:AtkText:text-selection-changed' and
594  * converts it to the AT-SPI signal - 'object:text-selection-changed'
595  *
596  */
597 static gboolean
598 text_selection_changed_event_listener (GSignalInvocationHint *signal_hint,
599                                        guint n_param_values,
600                                        const GValue *param_values,
601                                        gpointer data)
602 {
603   AtkObject *accessible;
604   GSignalQuery signal_query;
605   const gchar *name, *minor;
606   gint detail1, detail2;
607
608   g_signal_query (signal_hint->signal_id, &signal_query);
609   name = signal_query.signal_name;
610
611   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
612   minor = g_quark_to_string (signal_hint->detail);
613
614   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
615         detail1 = g_value_get_int (&param_values[1]);
616
617   if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
618         detail2 = g_value_get_int (&param_values[2]);
619
620   emit(accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2, DBUS_TYPE_STRING_AS_STRING, "");
621   return TRUE;
622 }
623
624 /*---------------------------------------------------------------------------*/
625
626 /*
627  * Generic signal converter and forwarder.
628  *
629  * Klass (Interface) org.freedesktop.atspi.Event.Object
630  * Major is the signal name.
631  * Minor is NULL.
632  * detail1 is 0.
633  * detail2 is 0.
634  * any_data is NULL.
635  */
636 static gboolean
637 generic_event_listener (GSignalInvocationHint *signal_hint,
638                         guint n_param_values,
639                         const GValue *param_values,
640                         gpointer data)
641 {
642   AtkObject *accessible;
643   GSignalQuery signal_query;
644   const gchar *name;
645
646   g_signal_query (signal_hint->signal_id, &signal_query);
647   name = signal_query.signal_name;
648
649   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
650   emit(accessible, ITF_EVENT_OBJECT, name, "", 0, 0, DBUS_TYPE_INT32_AS_STRING, 0);
651   return TRUE;
652 }
653
654 /*---------------------------------------------------------------------------*/
655
656 /*
657  * Registers the provided function as a handler for the given signal name
658  * and stores the signal id returned so that the function may be
659  * de-registered later.
660  */
661 static void
662 add_signal_listener (GSignalEmissionHook listener, const char *signal_name)
663 {
664   guint id;
665
666   id = atk_add_global_event_listener (listener, signal_name);
667   g_array_append_val (listener_ids, id);
668 }
669
670 /*
671  * Initialization for the signal handlers.
672  *
673  * Registers all required signal handlers.
674  */
675 void
676 spi_atk_register_event_listeners (void)
677 {
678   /*
679    * Kludge to make sure the Atk interface types are registered, otherwise
680    * the AtkText signal handlers below won't get registered
681    */
682   GObject   *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
683   AtkObject *bo = atk_no_op_object_new (ao);
684
685   g_object_unref (G_OBJECT (bo));
686   g_object_unref (ao);
687
688   /* Register for focus event notifications, and register app with central registry  */
689   listener_ids = g_array_sized_new (FALSE, TRUE, sizeof (guint), 16);
690
691   atk_bridge_focus_tracker_id = atk_add_focus_tracker (focus_tracker);
692
693   add_signal_listener (property_event_listener,               "Gtk:AtkObject:property-change");
694   add_signal_listener (window_event_listener,                 "window:create");
695   add_signal_listener (window_event_listener,                 "window:destroy");
696   add_signal_listener (window_event_listener,                 "window:minimize");
697   add_signal_listener (window_event_listener,                 "window:maximize");
698   add_signal_listener (window_event_listener,                 "window:restore");
699   add_signal_listener (window_event_listener,                 "window:activate");
700   add_signal_listener (window_event_listener,                 "window:deactivate");
701   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:load-complete");
702   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:reload");
703   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:load-stopped");
704   /* TODO Fake this event on the client side */
705   add_signal_listener (state_event_listener,                  "Gtk:AtkObject:state-change");
706   /* TODO */
707   add_signal_listener (active_descendant_event_listener,      "Gtk:AtkObject:active-descendant-changed");
708   add_signal_listener (bounds_event_listener,                 "Gtk:AtkComponent:bounds-changed");
709   add_signal_listener (text_selection_changed_event_listener, "Gtk:AtkText:text-selection-changed");
710   add_signal_listener (text_changed_event_listener,           "Gtk:AtkText:text-changed");
711   add_signal_listener (link_selected_event_listener,          "Gtk:AtkHypertext:link-selected");
712   add_signal_listener (generic_event_listener,                "Gtk:AtkObject:visible-data-changed");
713   add_signal_listener (generic_event_listener,                "Gtk:AtkSelection:selection-changed");
714   add_signal_listener (generic_event_listener,                "Gtk:AtkText:text-caret-moved");
715   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:row-inserted");
716   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:row-reordered");
717   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:row-deleted");
718   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:column-inserted");
719   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:column-reordered");
720   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:column-deleted");
721   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:model-changed");
722
723   /*
724    * May add the following listeners to implement preemptive key listening for GTK+
725    *
726    * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-press-event");
727    * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-release-event");
728    */
729   atk_bridge_key_event_listener_id = atk_add_key_event_listener (spi_atk_bridge_key_listener, NULL);
730 }
731
732 /*---------------------------------------------------------------------------*/
733
734 /* 
735  * De-registers all ATK signal handlers.
736  */
737 void
738 spi_atk_deregister_event_listeners (void)
739 {
740   gint i;
741   GArray *ids = listener_ids;
742   listener_ids = NULL;
743
744   if (atk_bridge_focus_tracker_id)
745         atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
746   
747   for (i = 0; ids && i < ids->len; i++)
748     {
749           atk_remove_global_event_listener (g_array_index (ids, guint, i));
750     }
751   
752   if (atk_bridge_key_event_listener_id)
753           atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
754 }
755
756 /*---------------------------------------------------------------------------*/
757
758 /*
759  * TODO This function seems out of place here.
760  *
761  * Emits fake deactivate signals on all top-level windows.
762  * Used when shutting down AT-SPI, ensuring that all
763  * windows have been removed on the client side.
764  */
765 void
766 spi_atk_tidy_windows (void)
767 {
768   AtkObject *root;
769   gint n_children;
770   gint i;
771
772   root = atk_get_root ();
773   n_children = atk_object_get_n_accessible_children (root);
774   for (i = 0; i < n_children; i++)
775     {
776       AtkObject *child;
777       AtkStateSet *stateset;
778       const gchar *name;
779      
780       child = atk_object_ref_accessible_child (root, i);
781       stateset = atk_object_ref_state_set (child);
782       
783       name = atk_object_get_name (child);
784       if (atk_state_set_contains_state (stateset, ATK_STATE_ACTIVE))
785         {
786           emit(child, ITF_EVENT_WINDOW, "deactivate", NULL, 0, 0, DBUS_TYPE_STRING_AS_STRING, name);
787         }
788       g_object_unref (stateset);
789
790       emit(child, ITF_EVENT_WINDOW, "destroy", NULL, 0, 0, DBUS_TYPE_STRING_AS_STRING, name);
791       g_object_unref (child);
792     }
793 }
794
795 /*END------------------------------------------------------------------------*/