Change the keystroke delivery methid re-entrancy from
[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 typedef struct _SpiReentrantCallClosure 
50 {
51   GMainLoop   *loop;
52   DBusMessage *reply;
53 } SpiReentrantCallClosure;
54
55 static void
56 set_reply (DBusPendingCall * pending, void *user_data)
57 {
58   SpiReentrantCallClosure* closure = (SpiReentrantCallClosure *) user_data; 
59
60   closure->reply = dbus_pending_call_steal_reply (pending);
61   g_main_loop_quit (closure->loop);
62 }
63
64 static DBusMessage *
65 send_and_allow_reentry (DBusConnection * bus, DBusMessage * message)
66 {
67   DBusPendingCall *pending;
68   SpiReentrantCallClosure closure;
69
70   if (!dbus_connection_send_with_reply (bus, message, &pending, -1))
71       return NULL;
72   dbus_pending_call_set_notify (pending, set_reply, (void *) &closure, NULL);
73   closure.loop = g_main_loop_new (NULL, FALSE);
74
75   g_main_loop_run  (closure.loop);
76   
77   g_main_loop_unref (closure.loop);
78   return closure.reply;
79 }
80
81 /*---------------------------------------------------------------------------*/
82
83 /*
84  * Functionality related to sending device events from the application.
85  *
86  * This is used for forwarding key events on to the registry daemon.
87  */
88
89 static gboolean
90 Accessibility_DeviceEventController_NotifyListenersSync (const
91                                                          Accessibility_DeviceEvent
92                                                          * key_event)
93 {
94   DBusMessage *message;
95   DBusError error;
96   dbus_bool_t consumed = FALSE;
97
98   message =
99     dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
100                                   SPI_DBUS_PATH_DEC,
101                                   SPI_DBUS_INTERFACE_DEC,
102                                   "NotifyListenersSync");
103
104   dbus_error_init (&error);
105   if (spi_dbus_marshal_deviceEvent (message, key_event))
106     {
107       DBusMessage *reply =
108         send_and_allow_reentry (spi_global_app_data->bus, message);
109       if (reply)
110         {
111           DBusError error;
112           dbus_error_init (&error);
113           dbus_message_get_args (reply, &error, DBUS_TYPE_BOOLEAN, &consumed,
114                                  DBUS_TYPE_INVALID);
115           dbus_message_unref (reply);
116         }
117     }
118   dbus_message_unref (message);
119   return consumed;
120 }
121
122 static void
123 spi_init_keystroke_from_atk_key_event (Accessibility_DeviceEvent * keystroke,
124                                        AtkKeyEventStruct * event)
125 {
126   keystroke->id = (dbus_int32_t) event->keyval;
127   keystroke->hw_code = (dbus_int16_t) event->keycode;
128   keystroke->timestamp = (dbus_uint32_t) event->timestamp;
129   keystroke->modifiers = (dbus_uint16_t) (event->state & 0xFFFF);
130   if (event->string)
131     {
132       gunichar c;
133
134       keystroke->event_string = g_strdup (event->string);
135       c = g_utf8_get_char_validated (event->string, -1);
136       if (c > 0 && g_unichar_isprint (c))
137         keystroke->is_text = TRUE;
138       else
139         keystroke->is_text = FALSE;
140     }
141   else
142     {
143       keystroke->event_string = g_strdup ("");
144       keystroke->is_text = FALSE;
145     }
146   switch (event->type)
147     {
148     case (ATK_KEY_EVENT_PRESS):
149       keystroke->type = Accessibility_KEY_PRESSED_EVENT;
150       break;
151     case (ATK_KEY_EVENT_RELEASE):
152       keystroke->type = Accessibility_KEY_RELEASED_EVENT;
153       break;
154     default:
155       keystroke->type = 0;
156       break;
157     }
158 #if 0
159   g_print
160     ("key_event type %d; val=%d code=%d modifiers=%x name=%s is_text=%d, time=%lx\n",
161      (int) keystroke->type, (int) keystroke->id, (int) keystroke->hw_code,
162      (int) keystroke->modifiers, keystroke->event_string,
163      (int) keystroke->is_text, (unsigned long) keystroke->timestamp);
164 #endif
165 }
166
167
168 static gint
169 spi_atk_bridge_key_listener (AtkKeyEventStruct * event, gpointer data)
170 {
171   gboolean result;
172   Accessibility_DeviceEvent key_event;
173
174   spi_init_keystroke_from_atk_key_event (&key_event, event);
175
176   result =
177     Accessibility_DeviceEventController_NotifyListenersSync (&key_event);
178
179   if (key_event.event_string)
180     g_free (key_event.event_string);
181
182   return result;
183 }
184
185 /*---------------------------------------------------------------------------*/
186
187 static gchar *
188 convert_signal_name (const gchar * s)
189 {
190   gchar *ret = g_strdup (s);
191   gchar *t;
192
193   if (!ret)
194     return NULL;
195   ret[0] = toupper (ret[0]);
196   while ((t = strchr (ret, '-')) != NULL)
197     {
198       memmove (t, t + 1, strlen (t));
199       *t = toupper (*t);
200     }
201   return ret;
202 }
203
204 static const void *
205 replace_null (const gint type,
206               const void *val)
207 {
208   switch (type)
209     {
210       case DBUS_TYPE_STRING:
211       case DBUS_TYPE_OBJECT_PATH:
212            if (!val)
213               return "";
214            else
215               return val;
216       default:
217            return val;
218     }
219 }
220
221 static void
222 append_basic (DBusMessageIter *iter,
223               const char *type,
224               const void *val)
225 {
226   DBusMessageIter sub;
227
228   dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, type, &sub);
229
230     val = replace_null ((int) *type, val);
231     dbus_message_iter_append_basic(&sub, (int) *type, &val);
232
233   dbus_message_iter_close_container(iter, &sub);
234 }
235
236 static void
237 append_rect (DBusMessageIter *iter,
238              const char *type,
239              const void *val)
240 {
241   DBusMessageIter variant, sub;
242   const AtkRectangle *rect = (const AtkRectangle *) val;
243
244   dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, type, &variant);
245
246     dbus_message_iter_open_container (&variant, DBUS_TYPE_STRUCT, NULL, &sub);
247
248       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->x));
249       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->y));
250       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->width));
251       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &(rect->height));
252
253     dbus_message_iter_close_container (&variant, &sub);
254
255   dbus_message_iter_close_container(iter, &variant);
256 }
257
258 static void
259 append_object (DBusMessageIter *iter,
260                const char *type,
261                const void *val)
262 {
263   spi_object_append_v_reference (iter, ATK_OBJECT (val));
264 }
265
266 static gchar *
267 signal_name_to_dbus (const gchar *s)
268 {
269   gchar *ret = g_strdup (s);
270   gchar *t;
271
272   if (!ret)
273     return NULL;
274   ret [0] = toupper (ret [0]);
275   while ((t = strchr (ret, '-')) != NULL)
276   {
277     memmove (t, t + 1, strlen (t));
278     *t = toupper (*t);
279   }
280   return ret;
281 }
282
283 /*
284  * Emits an AT-SPI event.
285  * AT-SPI events names are split into three parts:
286  * class:major:minor
287  * This is mapped onto D-Bus events as:
288  * D-Bus Interface:Signal Name:Detail argument
289  *
290  * Marshals a basic type into the 'any_data' attribute of
291  * the AT-SPI event.
292  */
293 static void 
294 emit_event (AtkObject  *obj,
295             const char *klass,
296             const char *major,
297             const char *minor,
298             dbus_int32_t detail1,
299             dbus_int32_t detail2,
300             const char *type,
301             const void *val,
302             void (*append_variant) (DBusMessageIter *, const char *, const void *))
303 {
304   DBusConnection *bus = spi_global_app_data->bus;
305   const char *path =  spi_register_object_to_path (spi_global_register,
306                                                    G_OBJECT (obj));
307
308   gchar *cname, *t;
309   DBusMessage *sig;
310   DBusMessageIter iter, iter_struct;
311   
312   if (!klass) klass = "";
313   if (!major) major = "";
314   if (!minor) minor = "";
315   if (!type) type = "u";
316
317   /*
318    * This is very annoying, but as '-' isn't a legal signal
319    * name in D-Bus (Why not??!?) The names need converting
320    * on this side, and again on the client side.
321    */
322   cname = signal_name_to_dbus (major);
323   sig = dbus_message_new_signal(path, klass, cname);
324   g_free(cname);
325
326   dbus_message_iter_init_append(sig, &iter);
327
328   spi_object_append_reference (&iter, spi_global_app_data->root);
329   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
330   dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail1);
331   dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail2);
332
333   append_variant (&iter, type, val);
334
335   dbus_connection_send(bus, sig, NULL);
336   dbus_message_unref(sig);
337 }
338
339 /*---------------------------------------------------------------------------*/
340
341 /*
342  * The focus listener handles the ATK 'focus' signal and forwards it
343  * as the AT-SPI event, 'focus:'
344  */
345 static void
346 focus_tracker (AtkObject * accessible)
347 {
348   emit_event (accessible, ITF_EVENT_FOCUS, "focus", "", 0, 0,
349               DBUS_TYPE_INT32_AS_STRING, 0, append_basic);
350 }
351
352 /*---------------------------------------------------------------------------*/
353
354 #define PCHANGE "property_change"
355
356 /* 
357  * This handler handles the following ATK signals and
358  * converts them to AT-SPI events:
359  *  
360  * Gtk:AtkObject:property-change -> object:property-change:(property-name)
361  *
362  * The property-name is part of the ATK property-change signal.
363  */
364 static gboolean
365 property_event_listener (GSignalInvocationHint * signal_hint,
366                          guint n_param_values,
367                          const GValue * param_values, gpointer data)
368 {
369   AtkObject *accessible;
370   AtkPropertyValues *values;
371
372   const gchar *pname = NULL;
373
374   AtkObject *otemp;
375   const gchar *s1, s2;
376   gint i;
377
378   accessible = g_value_get_object (&param_values[0]);
379   values = (AtkPropertyValues *) g_value_get_pointer (&param_values[1]);
380
381   pname = values[0].property_name;
382
383   /* TODO Could improve this control statement by matching
384    * on only the end of the signal names,
385    */
386   if (strcmp (pname, "accessible-name") == 0)
387     {
388       s1 = atk_object_get_name (accessible);
389       if (s1 != NULL)
390         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
391                     DBUS_TYPE_STRING_AS_STRING, s1, append_basic);
392     }
393   if (strcmp (pname, "accessible-description") == 0)
394     {
395       s1 = atk_object_get_description (accessible);
396       if (s1 != NULL)
397         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
398                     DBUS_TYPE_STRING_AS_STRING, s1, append_basic);
399     }
400   if (strcmp (pname, "accessible-parent") == 0)
401     {
402       otemp = atk_object_get_parent (accessible);
403       if (otemp != NULL)
404         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
405                     "(so)", otemp, append_object);
406     }
407   if (strcmp (pname, "accessible-table-summary") == 0)
408     {
409       otemp = atk_table_get_summary (ATK_TABLE (accessible));
410       if (otemp != NULL)
411         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
412                     "(so)", otemp, append_object);
413     }
414   else if (strcmp (pname, "accessible-table-column-header") == 0)
415     {
416       i = g_value_get_int (&(values->new_value));
417       otemp = atk_table_get_column_header (ATK_TABLE (accessible), i);
418       if (otemp != NULL)
419         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
420                     "(so)", otemp, append_object);
421     }
422   else if (strcmp (pname, "accessible-table-row-header") == 0)
423     {
424       i = g_value_get_int (&(values->new_value));
425       otemp = atk_table_get_row_header (ATK_TABLE (accessible), i);
426       if (otemp != NULL)
427         emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
428                     "(so)", otemp, append_object);
429     }
430   else if (strcmp (pname, "accessible-table-row-description") == 0)
431     {
432       i = g_value_get_int (&(values->new_value));
433       s1 = atk_table_get_row_description (ATK_TABLE (accessible), i);
434       emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
435                   DBUS_TYPE_STRING_AS_STRING, s1, append_basic);
436     }
437   else if (strcmp (pname, "accessible-table-column-description") == 0)
438     {
439       i = g_value_get_int (&(values->new_value));
440       s1 = atk_table_get_column_description (ATK_TABLE (accessible), i);
441       emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
442                   DBUS_TYPE_STRING_AS_STRING, s1, append_basic);
443     }
444   else if (strcmp (pname, "accessible-table-caption-object") == 0)
445     {
446       otemp = atk_table_get_caption (ATK_TABLE (accessible));
447       emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
448                   "(so)", otemp, append_object);
449     }
450   else
451     {
452       emit_event (accessible, ITF_EVENT_OBJECT, PCHANGE, pname, 0, 0,
453             DBUS_TYPE_INT32_AS_STRING, 0, append_basic);
454     }
455   return TRUE;
456 }
457
458 /*---------------------------------------------------------------------------*/
459
460 #define STATE_CHANGED "state-changed"
461
462 /*
463  * The state event listener handles 'Gtk:AtkObject:state-change' ATK signals
464  * and forwards them as object:state-changed:(param-name) AT-SPI events. Where
465  * the param-name is part of the ATK state-change signal.
466  */
467 static gboolean
468 state_event_listener (GSignalInvocationHint * signal_hint,
469                       guint n_param_values,
470                       const GValue * param_values, gpointer data)
471 {
472   AtkObject *accessible;
473   gchar *pname;
474   guint detail1;
475
476   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
477   pname = g_strdup (g_value_get_string (&param_values[1]));
478
479   /* TODO - Possibly ignore a change to the 'defunct' state.
480    * This is because without reference counting defunct objects should be removed.
481    */
482   detail1 = (g_value_get_boolean (&param_values[2])) ? 1 : 0;
483   emit_event (accessible, ITF_EVENT_OBJECT, STATE_CHANGED, pname, detail1, 0,
484               DBUS_TYPE_INT32_AS_STRING, 0, append_basic);
485   g_free (pname);
486   return TRUE;
487 }
488
489 /*---------------------------------------------------------------------------*/
490
491 /*
492  * The window event listener handles the following ATK signals and forwards
493  * them as AT-SPI events:
494  *
495  * window:create     -> window:create
496  * window:destroy    -> window:destroy
497  * window:minimize   -> window:minimize
498  * window:maximize   -> window:maximize
499  * window:activate   -> window:activate
500  * window:deactivate -> window:deactivate
501  */
502 static gboolean
503 window_event_listener (GSignalInvocationHint * signal_hint,
504                        guint n_param_values,
505                        const GValue * param_values, gpointer data)
506 {
507   AtkObject *accessible;
508   GSignalQuery signal_query;
509   const gchar *name, *s;
510
511   g_signal_query (signal_hint->signal_id, &signal_query);
512   name = signal_query.signal_name;
513
514   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
515   s = atk_object_get_name (accessible);
516   emit_event (accessible, ITF_EVENT_WINDOW, name, "", 0, 0,
517               DBUS_TYPE_STRING_AS_STRING, s, append_basic);
518
519   return TRUE;
520 }
521
522 /*---------------------------------------------------------------------------*/
523
524 /* 
525  * The document event listener handles the following ATK signals
526  * and converts them to AT-SPI events:
527  *
528  * Gtk:AtkDocument:load-complete ->  document:load-complete
529  * Gtk:AtkDocument:load-stopped  ->  document:load-stopped
530  * Gtk:AtkDocument:reload        ->  document:reload
531  */
532 static gboolean
533 document_event_listener (GSignalInvocationHint * signal_hint,
534                          guint n_param_values,
535                          const GValue * param_values, gpointer data)
536 {
537   AtkObject *accessible;
538   GSignalQuery signal_query;
539   const gchar *name, *s;
540
541   g_signal_query (signal_hint->signal_id, &signal_query);
542   name = signal_query.signal_name;
543
544   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
545   s = atk_object_get_name (accessible);
546   emit_event (accessible, ITF_EVENT_DOCUMENT, name, "", 0, 0,
547               DBUS_TYPE_STRING_AS_STRING, s, append_basic);
548
549   return TRUE;
550 }
551
552 /*---------------------------------------------------------------------------*/
553
554 /*
555  * Signal handler for  "Gtk:AtkComponent:bounds-changed". Converts
556  * this to an AT-SPI event - "object:bounds-changed".
557  */
558 static gboolean
559 bounds_event_listener (GSignalInvocationHint * signal_hint,
560                        guint n_param_values,
561                        const GValue * param_values, gpointer data)
562 {
563   AtkObject *accessible;
564   AtkRectangle *atk_rect;
565   GSignalQuery signal_query;
566   const gchar *name, *s;
567
568   g_signal_query (signal_hint->signal_id, &signal_query);
569   name = signal_query.signal_name;
570
571   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
572
573   if (G_VALUE_HOLDS_BOXED (param_values + 1))
574   {
575     atk_rect = g_value_get_boxed (param_values + 1);
576
577     emit_event (accessible, ITF_EVENT_OBJECT, name, "", 0, 0,
578                 "(iiii)", atk_rect, append_rect);
579   }
580   return TRUE;
581 }
582
583 /*---------------------------------------------------------------------------*/
584
585 /* 
586  * Handles the ATK signal 'Gtk:AtkObject:active-descendant-changed' and 
587  * converts it to the AT-SPI signal - 'object:active-descendant-changed'.
588  *
589  */
590 static gboolean
591 active_descendant_event_listener (GSignalInvocationHint * signal_hint,
592                                   guint n_param_values,
593                                   const GValue * param_values, gpointer data)
594 {
595   AtkObject *accessible;
596   AtkObject *child;
597   GSignalQuery signal_query;
598   const gchar *name, *minor;
599   gchar *s;
600   gint detail1;
601
602   g_signal_query (signal_hint->signal_id, &signal_query);
603   name = signal_query.signal_name;
604
605   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
606   child = ATK_OBJECT (g_value_get_pointer (&param_values[1]));
607   g_return_val_if_fail (ATK_IS_OBJECT (child), TRUE);
608   minor = g_quark_to_string (signal_hint->detail);
609
610   detail1 = atk_object_get_index_in_parent (child);
611
612   emit_event (accessible, ITF_EVENT_OBJECT, name, "", detail1, 0,
613               "(so)", child, append_object);
614   g_free (s);
615   return TRUE;
616 }
617
618 /*---------------------------------------------------------------------------*/
619
620 /* 
621  * Handles the ATK signal 'Gtk:AtkHypertext:link-selected' and
622  * converts it to the AT-SPI signal - 'object:link-selected'
623  *
624  */
625 static gboolean
626 link_selected_event_listener (GSignalInvocationHint * signal_hint,
627                               guint n_param_values,
628                               const GValue * param_values, gpointer data)
629 {
630   AtkObject *accessible;
631   GSignalQuery signal_query;
632   const gchar *name, *minor;
633   gint detail1;
634
635   g_signal_query (signal_hint->signal_id, &signal_query);
636   name = signal_query.signal_name;
637
638   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
639   minor = g_quark_to_string (signal_hint->detail);
640
641   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
642     detail1 = g_value_get_int (&param_values[1]);
643
644   emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, 0,
645               DBUS_TYPE_INT32_AS_STRING, 0, append_basic);
646   return TRUE;
647 }
648
649 /*---------------------------------------------------------------------------*/
650
651 /* 
652  * Handles the ATK signal 'Gtk:AtkText:text-changed' and
653  * converts it to the AT-SPI signal - 'object:text-changed'
654  *
655  */
656 static gboolean
657 text_changed_event_listener (GSignalInvocationHint * signal_hint,
658                              guint n_param_values,
659                              const GValue * param_values, gpointer data)
660 {
661   AtkObject *accessible;
662   GSignalQuery signal_query;
663   const gchar *name, *minor;
664   gchar *selected;
665   gint detail1, detail2;
666
667   g_signal_query (signal_hint->signal_id, &signal_query);
668   name = signal_query.signal_name;
669
670   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
671   minor = g_quark_to_string (signal_hint->detail);
672
673   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
674     detail1 = g_value_get_int (&param_values[1]);
675
676   if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
677     detail2 = g_value_get_int (&param_values[2]);
678
679   selected =
680     atk_text_get_text (ATK_TEXT (accessible), detail1, detail1 + detail2);
681
682   emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
683               DBUS_TYPE_STRING_AS_STRING, selected, append_basic);
684   return TRUE;
685 }
686
687 /*---------------------------------------------------------------------------*/
688
689 /* 
690  * Handles the ATK signal 'Gtk:AtkText:text-selection-changed' and
691  * converts it to the AT-SPI signal - 'object:text-selection-changed'
692  *
693  */
694 static gboolean
695 text_selection_changed_event_listener (GSignalInvocationHint * signal_hint,
696                                        guint n_param_values,
697                                        const GValue * param_values,
698                                        gpointer data)
699 {
700   AtkObject *accessible;
701   GSignalQuery signal_query;
702   const gchar *name, *minor;
703   gint detail1, detail2;
704
705   g_signal_query (signal_hint->signal_id, &signal_query);
706   name = signal_query.signal_name;
707
708   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
709   minor = g_quark_to_string (signal_hint->detail);
710
711   if (G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
712     detail1 = g_value_get_int (&param_values[1]);
713
714   if (G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
715     detail2 = g_value_get_int (&param_values[2]);
716
717   emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
718               DBUS_TYPE_STRING_AS_STRING, "", append_basic);
719   return TRUE;
720 }
721
722 /*---------------------------------------------------------------------------*/
723
724 /*
725  * Children changed signal converter and forwarder.
726  *
727  * Klass (Interface) org.freedesktop.atspi.Event.Object
728  * Major is the signal name.
729  * Minor is 'add' or 'remove'
730  * detail1 is the index.
731  * detail2 is 0.
732  * any_data is the child reference.
733  */
734 static gboolean
735 children_changed_event_listener (GSignalInvocationHint * signal_hint,
736                                  guint n_param_values,
737                                  const GValue * param_values, gpointer data)
738 {
739   GSignalQuery signal_query;
740   const gchar *name, *minor;
741   gint detail1, detail2 = 0;
742
743   AtkObject *accessible, *ao;
744   gpointer child;
745
746   g_signal_query (signal_hint->signal_id, &signal_query);
747   name = signal_query.signal_name;
748
749   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
750   minor = g_quark_to_string (signal_hint->detail);
751
752   detail1 = g_value_get_uint (param_values + 1);
753   child = g_value_get_pointer (param_values + 2);
754
755   if (ATK_IS_OBJECT (child))
756     {
757       ao = ATK_OBJECT (child);
758       emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
759                   "(so)", ao, append_object);
760     }
761   else if ((minor != NULL) && (strcmp (minor, "add") == 0))
762     {
763       ao = atk_object_ref_accessible_child (accessible, 
764                                             detail1);
765       emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
766                   "(so)", ao, append_object);
767     }
768   else
769     {
770       emit_event (accessible, ITF_EVENT_OBJECT, name, minor, detail1, detail2,
771                   "(so)", ao, append_object);
772     }
773  
774   return TRUE;
775 }
776
777 /*---------------------------------------------------------------------------*/
778
779 static void
780 toplevel_added_event_listener (AtkObject * accessible,
781                                guint index, AtkObject * child)
782 {
783   emit_event (accessible, ITF_EVENT_OBJECT, "children-changed", "add", index, 0,
784               "(so)", child, append_object);
785 }
786
787 static void
788 toplevel_removed_event_listener (AtkObject * accessible,
789                                  guint index, AtkObject * child)
790 {
791   emit_event (accessible, ITF_EVENT_OBJECT, "children-changed", "remove", index, 0,
792               "(so)", child, append_object);
793 }
794
795 /*---------------------------------------------------------------------------*/
796
797 /*
798  * Generic signal converter and forwarder.
799  *
800  * Klass (Interface) org.freedesktop.atspi.Event.Object
801  * Major is the signal name.
802  * Minor is NULL.
803  * detail1 is 0.
804  * detail2 is 0.
805  * any_data is NULL.
806  */
807 static gboolean
808 generic_event_listener (GSignalInvocationHint * signal_hint,
809                         guint n_param_values,
810                         const GValue * param_values, gpointer data)
811 {
812   AtkObject *accessible;
813   GSignalQuery signal_query;
814   const gchar *name;
815   int detail1 = 0, detail2 = 0;
816
817   g_signal_query (signal_hint->signal_id, &signal_query);
818   name = signal_query.signal_name;
819
820   accessible = ATK_OBJECT (g_value_get_object (&param_values[0]));
821
822   if (n_param_values > 1 && G_VALUE_TYPE (&param_values[1]) == G_TYPE_INT)
823     detail1 = g_value_get_int (&param_values[1]);
824
825   if (n_param_values > 2 && G_VALUE_TYPE (&param_values[2]) == G_TYPE_INT)
826     detail2 = g_value_get_int (&param_values[2]);
827
828   emit_event (accessible, ITF_EVENT_OBJECT, name, "", detail1, detail2,
829               DBUS_TYPE_INT32_AS_STRING, 0, append_basic);
830   return TRUE;
831 }
832
833 /*---------------------------------------------------------------------------*/
834
835 /*
836  * Registers the provided function as a handler for the given signal name
837  * and stores the signal id returned so that the function may be
838  * de-registered later.
839  */
840 static void
841 add_signal_listener (GSignalEmissionHook listener, const char *signal_name)
842 {
843   guint id;
844
845   id = atk_add_global_event_listener (listener, signal_name);
846   g_array_append_val (listener_ids, id);
847 }
848
849 /*
850  * Initialization for the signal handlers.
851  *
852  * Registers all required signal handlers.
853  */
854 void
855 spi_atk_register_event_listeners (void)
856 {
857   /*
858    * Kludge to make sure the Atk interface types are registered, otherwise
859    * the AtkText signal handlers below won't get registered
860    */
861   GObject *ao = g_object_new (ATK_TYPE_OBJECT, NULL);
862   AtkObject *bo = atk_no_op_object_new (ao);
863
864   g_object_unref (G_OBJECT (bo));
865   g_object_unref (ao);
866
867   /* Register for focus event notifications, and register app with central registry  */
868   listener_ids = g_array_sized_new (FALSE, TRUE, sizeof (guint), 16);
869
870   atk_bridge_focus_tracker_id = atk_add_focus_tracker (focus_tracker);
871
872   add_signal_listener (property_event_listener,
873                        "Gtk:AtkObject:property-change");
874   add_signal_listener (window_event_listener, "window:create");
875   add_signal_listener (window_event_listener, "window:destroy");
876   add_signal_listener (window_event_listener, "window:minimize");
877   add_signal_listener (window_event_listener, "window:maximize");
878   add_signal_listener (window_event_listener, "window:restore");
879   add_signal_listener (window_event_listener, "window:activate");
880   add_signal_listener (window_event_listener, "window:deactivate");
881   add_signal_listener (document_event_listener,
882                        "Gtk:AtkDocument:load-complete");
883   add_signal_listener (document_event_listener, "Gtk:AtkDocument:reload");
884   add_signal_listener (document_event_listener,
885                        "Gtk:AtkDocument:load-stopped");
886   /* TODO Fake this event on the client side */
887   add_signal_listener (state_event_listener, "Gtk:AtkObject:state-change");
888   /* TODO */
889   add_signal_listener (active_descendant_event_listener,
890                        "Gtk:AtkObject:active-descendant-changed");
891   add_signal_listener (bounds_event_listener,
892                        "Gtk:AtkComponent:bounds-changed");
893   add_signal_listener (text_selection_changed_event_listener,
894                        "Gtk:AtkText:text-selection-changed");
895   add_signal_listener (text_changed_event_listener,
896                        "Gtk:AtkText:text-changed");
897   add_signal_listener (link_selected_event_listener,
898                        "Gtk:AtkHypertext:link-selected");
899   add_signal_listener (generic_event_listener,
900                        "Gtk:AtkObject:visible-data-changed");
901   add_signal_listener (generic_event_listener,
902                        "Gtk:AtkSelection:selection-changed");
903   add_signal_listener (generic_event_listener,
904                        "Gtk:AtkText:text-attributes-changed");
905   add_signal_listener (generic_event_listener,
906                        "Gtk:AtkText:text-caret-moved");
907   add_signal_listener (generic_event_listener, "Gtk:AtkTable:row-inserted");
908   add_signal_listener (generic_event_listener, "Gtk:AtkTable:row-reordered");
909   add_signal_listener (generic_event_listener, "Gtk:AtkTable:row-deleted");
910   add_signal_listener (generic_event_listener,
911                        "Gtk:AtkTable:column-inserted");
912   add_signal_listener (generic_event_listener,
913                        "Gtk:AtkTable:column-reordered");
914   add_signal_listener (generic_event_listener, "Gtk:AtkTable:column-deleted");
915   add_signal_listener (generic_event_listener, "Gtk:AtkTable:model-changed");
916
917   /* Children signal listeners */
918   atk_add_global_event_listener (children_changed_event_listener,
919                                  "Gtk:AtkObject:children-changed");
920
921 #if 0
922   g_signal_connect (G_OBJECT (spi_global_app_data->root),
923                     "children-changed::add",
924                     (GCallback) toplevel_added_event_listener, NULL);
925
926   g_signal_connect (G_OBJECT (spi_global_app_data->root),
927                     "children-changed::remove",
928                     (GCallback) toplevel_removed_event_listener, NULL);
929 #endif
930
931   /*
932    * May add the following listeners to implement preemptive key listening for GTK+
933    *
934    * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-press-event");
935    * atk_add_global_event_listener (spi_atk_bridge_widgetkey_listener, "Gtk:GtkWidget:key-release-event");
936    */
937   atk_bridge_key_event_listener_id =
938     atk_add_key_event_listener (spi_atk_bridge_key_listener, NULL);
939 }
940
941 /*---------------------------------------------------------------------------*/
942
943 /* 
944  * De-registers all ATK signal handlers.
945  */
946 void
947 spi_atk_deregister_event_listeners (void)
948 {
949   gint i;
950   GArray *ids = listener_ids;
951   listener_ids = NULL;
952
953   if (atk_bridge_focus_tracker_id)
954     atk_remove_focus_tracker (atk_bridge_focus_tracker_id);
955
956   for (i = 0; ids && i < ids->len; i++)
957     {
958       atk_remove_global_event_listener (g_array_index (ids, guint, i));
959     }
960
961   if (atk_bridge_key_event_listener_id)
962     atk_remove_key_event_listener (atk_bridge_key_event_listener_id);
963 }
964
965 /*---------------------------------------------------------------------------*/
966
967 /*
968  * TODO This function seems out of place here.
969  *
970  * Emits fake deactivate signals on all top-level windows.
971  * Used when shutting down AT-SPI, ensuring that all
972  * windows have been removed on the client side.
973  */
974 void
975 spi_atk_tidy_windows (void)
976 {
977   AtkObject *root;
978   gint n_children;
979   gint i;
980
981   root = atk_get_root ();
982   n_children = atk_object_get_n_accessible_children (root);
983   for (i = 0; i < n_children; i++)
984     {
985       AtkObject *child;
986       AtkStateSet *stateset;
987       const gchar *name;
988
989       child = atk_object_ref_accessible_child (root, i);
990       stateset = atk_object_ref_state_set (child);
991
992       name = atk_object_get_name (child);
993       if (atk_state_set_contains_state (stateset, ATK_STATE_ACTIVE))
994         {
995           emit_event (child, ITF_EVENT_WINDOW, "deactivate", NULL, 0, 0,
996                       DBUS_TYPE_STRING_AS_STRING, name, append_basic);
997         }
998       g_object_unref (stateset);
999
1000       emit_event (child, ITF_EVENT_WINDOW, "destroy", NULL, 0, 0,
1001                   DBUS_TYPE_STRING_AS_STRING, name, append_basic);
1002       g_object_unref (child);
1003     }
1004 }
1005
1006 /*END------------------------------------------------------------------------*/