2009-10-08 Mike Gorse <mgorse@novell.com>
[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 "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.freedesktop.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   /* TODO this is a hack, used becuase child-added events are not guaranteed.
191    * On recieving an event from a non-registered object we check if it can be safely 
192    * registered before sending the event.
193    */
194   path = atk_dbus_object_attempt_registration (accessible);
195
196   /* Tough decision here
197    * We won't send events from accessible
198    * objects that have not yet been added to the accessible tree.
199    */
200   if (path == NULL)
201   {
202 #ifdef SPI_ATK_DEBUG
203       g_debug ("AT-SPI: Event recieved from non-registered object");
204 #endif
205       return;
206   }
207
208   spi_dbus_emit_signal (atk_adaptor_app_data->bus, path, klass, major, minor, detail1, detail2, type, val);
209   g_free(path);
210 }
211
212 /*---------------------------------------------------------------------------*/
213
214 /*
215  * Emits an AT-SPI event, marshalling a BoundingBox structure into the 
216  * 'any_data' variant of the event.
217  */
218 static void
219 emit_rect(AtkObject  *accessible,
220           const char *klass,
221           const char *major,
222           const char *minor,
223           AtkRectangle *rect)
224 {
225   DBusMessage *sig;
226   DBusMessageIter iter, variant, sub;
227   gchar *path, *cname, *t;
228   dbus_int32_t dummy = 0;
229
230   path = atk_dbus_object_to_path (accessible, FALSE);
231
232   /* Tough decision here
233    * We won't send events from accessible
234    * objects that have not yet been added to the accessible tree.
235    */
236   if (path == NULL)
237       return;
238
239   if (!klass) klass = "";
240   if (!major) major = "";
241   if (!minor) minor = "";
242
243   /*
244    * This is very annoying, but as '-' isn't a legal signal
245    * name in D-Bus (Why not??!?) The names need converting
246    * on this side, and again on the client side.
247    */
248   cname = g_strdup(major);
249   while ((t = strchr(cname, '-')) != NULL) *t = '_';
250
251   sig = dbus_message_new_signal(path, klass, cname);
252   g_free(path);
253   g_free(cname);
254
255   dbus_message_iter_init_append (sig, &iter);
256   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
257   dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &dummy);
258   dbus_message_iter_append_basic (&iter, DBUS_TYPE_INT32, &dummy);
259
260   dbus_message_iter_open_container (&iter, DBUS_TYPE_VARIANT, "(iiii)", &variant);
261     dbus_message_iter_open_container (&variant, DBUS_TYPE_STRUCT, NULL, &sub);
262       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->x));
263       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->y));
264       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->width));
265       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->height));
266     dbus_message_iter_close_container (&variant, &sub);
267   dbus_message_iter_close_container (&iter, &variant);
268
269   dbus_connection_send(atk_adaptor_app_data->bus, sig, NULL);
270
271   dbus_message_unref (sig);
272 }
273
274 /*---------------------------------------------------------------------------*/
275
276 /*
277  * The focus listener handles the ATK 'focus' signal and forwards it
278  * as the AT-SPI event, 'focus:'
279  */
280 static void
281 focus_tracker (AtkObject *accessible)
282 {
283   emit(accessible, ITF_EVENT_FOCUS, "focus", "", 0, 0, DBUS_TYPE_INT32_AS_STRING, 0);
284 }
285
286 /*---------------------------------------------------------------------------*/
287
288 #define PCHANGE "property-change"
289
290 /* 
291  * This handler handles the following ATK signals and
292  * converts them to AT-SPI events:
293  *  
294  * Gtk:AtkObject:property-change -> object:property-change:(property-name)
295  *
296  * The property-name is part of the ATK property-change signal.
297  */
298 static gboolean
299 property_event_listener (GSignalInvocationHint *signal_hint,
300                          guint                  n_param_values,
301                          const GValue          *param_values,
302                          gpointer               data)
303 {
304   AtkObject *accessible;
305   AtkPropertyValues *values;
306
307   const gchar *pname = NULL;
308
309   AtkObject *otemp;
310   const gchar *stemp;
311   gint i;
312
313   accessible = g_value_get_object (&param_values[0]);
314   values = (AtkPropertyValues*) g_value_get_pointer (&param_values[1]);
315
316   pname = values[0].property_name;
317   if (strcmp (pname, "accessible-name") == 0 ||
318       strcmp (pname, "accessible-description") == 0 ||
319       strcmp (pname, "accessible-parent") == 0)
320   {
321       return TRUE;
322   }
323
324   /* TODO Could improve this control statement by matching
325    * on only the end of the signal names,
326    */
327   if (strcmp (pname, "accessible-table-summary") == 0)
328     {
329       otemp = atk_table_get_summary(ATK_TABLE (accessible));
330       stemp = atk_dbus_object_to_path (otemp, FALSE);
331       if (stemp != NULL)
332           emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
333     }
334   else if (strcmp (pname, "accessible-table-column-header") == 0)
335     {
336       i = g_value_get_int (&(values->new_value));
337       otemp = atk_table_get_column_header(ATK_TABLE (accessible), i);
338       stemp = atk_dbus_object_to_path (otemp, FALSE);
339       if (stemp != NULL)
340           emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
341     }
342   else if (strcmp (pname, "accessible-table-row-header") == 0)
343     {
344       i = g_value_get_int (&(values->new_value));
345       otemp = atk_table_get_row_header(ATK_TABLE (accessible), i);
346       stemp = atk_dbus_object_to_path (otemp, FALSE);
347       if (stemp != NULL)
348           emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, stemp);
349     }
350   else if (strcmp (pname, "accessible-table-row-description") == 0)
351     {
352       i = g_value_get_int (&(values->new_value));
353       stemp = atk_table_get_row_description(ATK_TABLE (accessible), i);
354       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_STRING_AS_STRING, stemp);
355     }
356   else if (strcmp (pname, "accessible-table-column-description") == 0)
357     {
358       i = g_value_get_int (&(values->new_value));
359       stemp = atk_table_get_column_description(ATK_TABLE (accessible), i);
360       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_STRING_AS_STRING, stemp);
361     }
362   else if (strcmp (pname, "accessible-table-caption-object") == 0)
363     {
364       otemp = atk_table_get_caption(ATK_TABLE(accessible));
365       stemp = atk_object_get_name(otemp);
366       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_STRING_AS_STRING, stemp);
367     }
368   else
369     {
370       emit(accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0, DBUS_TYPE_INT32_AS_STRING, 0);
371     }
372   return TRUE;
373 }
374
375 /*---------------------------------------------------------------------------*/
376
377 #define STATE_CHANGED "state-changed"
378
379 /*
380  * The state event listener handles 'Gtk:AtkObject:state-change' ATK signals
381  * and forwards them as object:state-changed:(param-name) AT-SPI events. Where
382  * the param-name is part of the ATK state-change signal.
383  */
384 static gboolean
385 state_event_listener (GSignalInvocationHint *signal_hint,
386                       guint n_param_values,
387                       const GValue *param_values,
388                       gpointer data)
389 {
390   AtkObject *accessible;
391   gchar *pname;
392   guint detail1;
393
394   accessible = ATK_OBJECT(g_value_get_object (&param_values[0]));
395   pname = g_strdup (g_value_get_string (&param_values[1]));
396
397   /* TODO - Possibly ignore a change to the 'defunct' state.
398    * This is because without reference counting defunct objects should be removed.
399    */
400   detail1 = (g_value_get_boolean (&param_values[2])) ? 1 : 0;
401   emit(accessible, ITF_EVENT_OBJECT, STATE_CHANGED, pname, detail1, 0, DBUS_TYPE_INT32_AS_STRING, 0);
402   g_free (pname);
403   return TRUE;
404 }
405
406 /*---------------------------------------------------------------------------*/
407
408 /*
409  * The window event listener handles the following ATK signals and forwards
410  * them as AT-SPI events:
411  *
412  * window:create     -> window:create
413  * window:destroy    -> window:destroy
414  * window:minimize   -> window:minimize
415  * window:maximize   -> window:maximize
416  * window:activate   -> window:activate
417  * window:deactivate -> window:deactivate
418  */
419 static gboolean
420 window_event_listener (GSignalInvocationHint *signal_hint,
421                        guint n_param_values,
422                        const GValue *param_values,
423                        gpointer data)
424 {
425   AtkObject *accessible;
426   GSignalQuery signal_query;
427   const gchar *name, *s;
428   
429   g_signal_query (signal_hint->signal_id, &signal_query);
430   name = signal_query.signal_name;
431
432   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
433   s = atk_object_get_name (accessible);
434   emit(accessible, ITF_EVENT_WINDOW, name, "", 0, 0, DBUS_TYPE_STRING_AS_STRING, s);
435
436   return TRUE;
437 }
438
439 /*---------------------------------------------------------------------------*/
440
441 /* 
442  * The document event listener handles the following ATK signals
443  * and converts them to AT-SPI events:
444  *
445  * Gtk:AtkDocument:load-complete ->  document:load-complete
446  * Gtk:AtkDocument:load-stopped  ->  document:load-stopped
447  * Gtk:AtkDocument:reload        ->  document:reload
448  */
449 static gboolean
450 document_event_listener (GSignalInvocationHint *signal_hint,
451                          guint n_param_values,
452                          const GValue *param_values,
453                          gpointer data)
454 {
455   AtkObject *accessible;
456   GSignalQuery signal_query;
457   const gchar *name, *s;
458
459   g_signal_query (signal_hint->signal_id, &signal_query);
460   name = signal_query.signal_name;
461
462   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
463   s = atk_object_get_name (accessible);
464   emit(accessible, ITF_EVENT_DOCUMENT, name, "", 0, 0, DBUS_TYPE_STRING_AS_STRING, s);
465
466   return TRUE;
467 }
468
469 /*---------------------------------------------------------------------------*/
470
471 /*
472  * Signal handler for  "Gtk:AtkComponent:bounds-changed". Converts
473  * this to an AT-SPI event - "object:bounds-changed".
474  */
475 static gboolean
476 bounds_event_listener (GSignalInvocationHint *signal_hint,
477                        guint n_param_values,
478                        const GValue *param_values,
479                        gpointer data)
480 {
481   AtkObject *accessible;
482   AtkRectangle *atk_rect;
483   GSignalQuery signal_query;
484   const gchar *name, *s;
485
486   g_signal_query (signal_hint->signal_id, &signal_query);
487   name = signal_query.signal_name;
488
489   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
490
491   if (G_VALUE_HOLDS_BOXED (param_values + 1))
492     atk_rect = g_value_get_boxed (param_values + 1);
493
494   emit_rect(accessible, ITF_EVENT_OBJECT, name, "", atk_rect);
495   return TRUE;
496 }
497
498 /*---------------------------------------------------------------------------*/
499
500 /* 
501  * Handles the ATK signal 'Gtk:AtkObject:active-descendant-changed' and 
502  * converts it to the AT-SPI signal - 'object:active-descendant-changed'.
503  *
504  */
505 static gboolean
506 active_descendant_event_listener (GSignalInvocationHint *signal_hint,
507                                   guint n_param_values,
508                                   const GValue *param_values,
509                                   gpointer data)
510 {
511   AtkObject *accessible;
512   AtkObject *child;
513   GSignalQuery signal_query;
514   const gchar *name, *minor;
515   gchar *s;
516   gint detail1;
517
518   g_signal_query (signal_hint->signal_id, &signal_query);
519   name = signal_query.signal_name;
520
521   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
522   child = ATK_OBJECT(g_value_get_pointer (&param_values[1]));
523   g_return_val_if_fail (ATK_IS_OBJECT (child), TRUE);
524   minor = g_quark_to_string (signal_hint->detail);
525
526   detail1 = atk_object_get_index_in_parent (child);
527   s = atk_dbus_object_to_path (child, FALSE);
528   if (s == NULL)
529     {
530       g_free (s);
531       return TRUE;
532     }
533
534   emit(accessible, ITF_EVENT_OBJECT, name, "", detail1, 0, DBUS_TYPE_OBJECT_PATH_AS_STRING, s);
535   g_free(s);
536   return TRUE;
537 }
538
539 /*---------------------------------------------------------------------------*/
540
541 /* 
542  * Handles the ATK signal 'Gtk:AtkHypertext:link-selected' and
543  * converts it to the AT-SPI signal - 'object:link-selected'
544  *
545  */
546 static gboolean
547 link_selected_event_listener (GSignalInvocationHint *signal_hint,
548                               guint n_param_values,
549                               const GValue *param_values,
550                               gpointer data)
551 {
552   AtkObject *accessible;
553   GSignalQuery signal_query;
554   const gchar *name, *minor;
555   gint detail1;
556
557   g_signal_query (signal_hint->signal_id, &signal_query);
558   name = signal_query.signal_name;
559
560   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
561   minor = g_quark_to_string (signal_hint->detail);
562
563   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
564         detail1 = g_value_get_int (&param_values[1]);
565
566   emit(accessible, ITF_EVENT_OBJECT, name, minor, detail1, 0, DBUS_TYPE_INT32_AS_STRING, 0);
567   return TRUE;
568 }
569
570 /*---------------------------------------------------------------------------*/
571
572 /* 
573  * Handles the ATK signal 'Gtk:AtkText:text-changed' and
574  * converts it to the AT-SPI signal - 'object:text-changed'
575  *
576  */
577 static gboolean
578 text_changed_event_listener (GSignalInvocationHint *signal_hint,
579                              guint n_param_values,
580                              const GValue *param_values,
581                              gpointer data)
582 {
583   AtkObject *accessible;
584   GSignalQuery signal_query;
585   const gchar *name, *minor;
586   gchar *selected;
587   gint detail1, detail2;
588
589   g_signal_query (signal_hint->signal_id, &signal_query);
590   name = signal_query.signal_name;
591
592   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
593   minor = g_quark_to_string (signal_hint->detail);
594
595   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
596         detail1 = g_value_get_int (&param_values[1]);
597
598   if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
599         detail2 = g_value_get_int (&param_values[2]);
600
601   selected = atk_text_get_text (ATK_TEXT (accessible), detail1, detail1+detail2);
602
603   emit(accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2, DBUS_TYPE_STRING_AS_STRING, selected);
604   return TRUE;
605 }
606
607 /*---------------------------------------------------------------------------*/
608
609 /* 
610  * Handles the ATK signal 'Gtk:AtkText:text-selection-changed' and
611  * converts it to the AT-SPI signal - 'object:text-selection-changed'
612  *
613  */
614 static gboolean
615 text_selection_changed_event_listener (GSignalInvocationHint *signal_hint,
616                                        guint n_param_values,
617                                        const GValue *param_values,
618                                        gpointer data)
619 {
620   AtkObject *accessible;
621   GSignalQuery signal_query;
622   const gchar *name, *minor;
623   gint detail1, detail2;
624
625   g_signal_query (signal_hint->signal_id, &signal_query);
626   name = signal_query.signal_name;
627
628   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
629   minor = g_quark_to_string (signal_hint->detail);
630
631   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
632         detail1 = g_value_get_int (&param_values[1]);
633
634   if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
635         detail2 = g_value_get_int (&param_values[2]);
636
637   emit(accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2, DBUS_TYPE_STRING_AS_STRING, "");
638   return TRUE;
639 }
640
641 /*---------------------------------------------------------------------------*/
642
643 /*
644  * Generic signal converter and forwarder.
645  *
646  * Klass (Interface) org.freedesktop.atspi.Event.Object
647  * Major is the signal name.
648  * Minor is NULL.
649  * detail1 is 0.
650  * detail2 is 0.
651  * any_data is NULL.
652  */
653 static gboolean
654 generic_event_listener (GSignalInvocationHint *signal_hint,
655                         guint n_param_values,
656                         const GValue *param_values,
657                         gpointer data)
658 {
659   AtkObject *accessible;
660   GSignalQuery signal_query;
661   const gchar *name;
662
663   g_signal_query (signal_hint->signal_id, &signal_query);
664   name = signal_query.signal_name;
665
666   accessible = ATK_OBJECT(g_value_get_object(&param_values[0]));
667   emit(accessible, ITF_EVENT_OBJECT, name, "", 0, 0, DBUS_TYPE_INT32_AS_STRING, 0);
668   return TRUE;
669 }
670
671 /*---------------------------------------------------------------------------*/
672
673 /*
674  * Registers the provided function as a handler for the given signal name
675  * and stores the signal id returned so that the function may be
676  * de-registered later.
677  */
678 static void
679 add_signal_listener (GSignalEmissionHook listener, const char *signal_name)
680 {
681   guint id;
682
683   id = atk_add_global_event_listener (listener, signal_name);
684   g_array_append_val (listener_ids, id);
685 }
686
687 /*
688  * Initialization for the signal handlers.
689  *
690  * Registers all required signal handlers.
691  */
692 void
693 spi_atk_register_event_listeners (void)
694 {
695   /*
696    * Kludge to make sure the Atk interface types are registered, otherwise
697    * the AtkText signal handlers below won't get registered
698    */
699   GObject   *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
700   AtkObject *bo = atk_no_op_object_new (ao);
701
702   g_object_unref (G_OBJECT (bo));
703   g_object_unref (ao);
704
705   /* Register for focus event notifications, and register app with central registry  */
706   listener_ids = g_array_sized_new (FALSE, TRUE, sizeof (guint), 16);
707
708   atk_bridge_focus_tracker_id = atk_add_focus_tracker (focus_tracker);
709
710   add_signal_listener (property_event_listener,               "Gtk:AtkObject:property-change");
711   add_signal_listener (window_event_listener,                 "window:create");
712   add_signal_listener (window_event_listener,                 "window:destroy");
713   add_signal_listener (window_event_listener,                 "window:minimize");
714   add_signal_listener (window_event_listener,                 "window:maximize");
715   add_signal_listener (window_event_listener,                 "window:restore");
716   add_signal_listener (window_event_listener,                 "window:activate");
717   add_signal_listener (window_event_listener,                 "window:deactivate");
718   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:load-complete");
719   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:reload");
720   add_signal_listener (document_event_listener,               "Gtk:AtkDocument:load-stopped");
721   /* TODO Fake this event on the client side */
722   add_signal_listener (state_event_listener,                  "Gtk:AtkObject:state-change");
723   /* TODO */
724   add_signal_listener (active_descendant_event_listener,      "Gtk:AtkObject:active-descendant-changed");
725   add_signal_listener (bounds_event_listener,                 "Gtk:AtkComponent:bounds-changed");
726   add_signal_listener (text_selection_changed_event_listener, "Gtk:AtkText:text-selection-changed");
727   add_signal_listener (text_changed_event_listener,           "Gtk:AtkText:text-changed");
728   add_signal_listener (link_selected_event_listener,          "Gtk:AtkHypertext:link-selected");
729   add_signal_listener (generic_event_listener,                "Gtk:AtkObject:visible-data-changed");
730   add_signal_listener (generic_event_listener,                "Gtk:AtkSelection:selection-changed");
731   add_signal_listener (generic_event_listener,                "Gtk:AtkText:text-caret-moved");
732   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:row-inserted");
733   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:row-reordered");
734   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:row-deleted");
735   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:column-inserted");
736   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:column-reordered");
737   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:column-deleted");
738   add_signal_listener (generic_event_listener,                "Gtk:AtkTable:model-changed");
739
740   /*
741    * May add the following listeners to implement preemptive key listening for GTK+
742    *
743    * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-press-event");
744    * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-release-event");
745    */
746   atk_bridge_key_event_listener_id = atk_add_key_event_listener (spi_atk_bridge_key_listener, NULL);
747 }
748
749 /*---------------------------------------------------------------------------*/
750
751 /* 
752  * De-registers all ATK signal handlers.
753  */
754 void
755 spi_atk_deregister_event_listeners (void)
756 {
757   gint i;
758   GArray *ids = listener_ids;
759   listener_ids = NULL;
760
761   if (atk_bridge_focus_tracker_id)
762         atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
763   
764   for (i = 0; ids && i < ids->len; i++)
765     {
766           atk_remove_global_event_listener (g_array_index (ids, guint, i));
767     }
768   
769   if (atk_bridge_key_event_listener_id)
770           atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
771 }
772
773 /*---------------------------------------------------------------------------*/
774
775 /*
776  * TODO This function seems out of place here.
777  *
778  * Emits fake deactivate signals on all top-level windows.
779  * Used when shutting down AT-SPI, ensuring that all
780  * windows have been removed on the client side.
781  */
782 void
783 spi_atk_tidy_windows (void)
784 {
785   AtkObject *root;
786   gint n_children;
787   gint i;
788
789   root = atk_get_root ();
790   n_children = atk_object_get_n_accessible_children (root);
791   for (i = 0; i < n_children; i++)
792     {
793       AtkObject *child;
794       AtkStateSet *stateset;
795       const gchar *name;
796      
797       child = atk_object_ref_accessible_child (root, i);
798       stateset = atk_object_ref_state_set (child);
799       
800       name = atk_object_get_name (child);
801       if (atk_state_set_contains_state (stateset, ATK_STATE_ACTIVE))
802         {
803           emit(child, ITF_EVENT_WINDOW, "deactivate", NULL, 0, 0, DBUS_TYPE_STRING_AS_STRING, name);
804         }
805       g_object_unref (stateset);
806
807       emit(child, ITF_EVENT_WINDOW, "destroy", NULL, 0, 0, DBUS_TYPE_STRING_AS_STRING, name);
808       g_object_unref (child);
809     }
810 }
811
812 /*END------------------------------------------------------------------------*/