Clean up names of private functions
[platform/upstream/at-spi2-core.git] / atspi / atspi-event-listener.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2002 Ximian Inc.
6  * Copyright 2002 Sun Microsystems, Inc.
7  * Copyright 2010, 2011 Novell, 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 "atspi-private.h"
26 #include <string.h>
27 #include <ctype.h>
28
29 typedef struct
30 {
31   AtspiEventListenerCB callback;
32   void *user_data;
33   GDestroyNotify callback_destroyed;
34   char *category;
35   char *name;
36   char *detail;
37 } EventListenerEntry;
38
39 G_DEFINE_TYPE (AtspiEventListener, atspi_event_listener, G_TYPE_OBJECT)
40
41 void
42 atspi_event_listener_init (AtspiEventListener *listener)
43 {
44 }
45
46 void
47 atspi_event_listener_class_init (AtspiEventListenerClass *klass)
48 {
49 }
50
51 static void
52 remove_datum (const AtspiEvent *event, void *user_data)
53 {
54   AtspiEventListenerSimpleCB cb = user_data;
55   cb (event);
56 }
57
58 typedef struct
59 {
60   gpointer callback;
61   GDestroyNotify callback_destroyed;
62   gint ref_count;
63 } CallbackInfo;
64 static GHashTable *callbacks;
65
66 void
67 callback_ref (void *callback, GDestroyNotify callback_destroyed)
68 {
69   CallbackInfo *info;
70
71   if (!callbacks)
72   {
73     callbacks = g_hash_table_new (g_direct_hash, g_direct_equal);
74     if (!callbacks)
75       return;
76   }
77
78   info = g_hash_table_lookup (callbacks, callback);
79   if (!info)
80   {
81     info = g_new (CallbackInfo, 1);
82     info->callback = callback;
83     info->callback_destroyed = callback_destroyed;
84     info->ref_count = 1;
85     g_hash_table_insert (callbacks, callback, info);
86   }
87   else
88     info->ref_count++;
89 }
90
91 void
92 callback_unref (gpointer callback)
93 {
94   CallbackInfo *info;
95
96   if (!callbacks)
97     return;
98   info = g_hash_table_lookup (callbacks, callback);
99   if (!info)
100   {
101     g_warning ("Atspi: Dereferencing invalid callback %p\n", callback);
102     return;
103   }
104   info->ref_count--;
105   if (info->ref_count == 0)
106   {
107 #if 0
108     /* TODO: Figure out why this seg faults from Python */
109     if (info->callback_destroyed)
110       (*info->callback_destroyed) (info->callback);
111 #endif
112     g_free (info);
113     g_hash_table_remove (callbacks, callback);
114   }
115 }
116
117 /**
118  * atspi_event_listener_new:
119  * @callback: (scope notified): An #AtspiEventListenerSimpleCB to be called
120  * when an event is fired.
121  * @user_data: (closure): data to pass to the callback.
122  * @callback_destroyed: A #GDestroyNotify called when the listener is freed
123  * and data associated with the callback should be freed.  Can be NULL.
124  *
125  * Returns: (transfer full): A new #AtspiEventListener.
126  */
127 AtspiEventListener *
128 atspi_event_listener_new (AtspiEventListenerCB callback,
129                                  gpointer user_data,
130                                  GDestroyNotify callback_destroyed)
131 {
132   AtspiEventListener *listener = g_object_new (ATSPI_TYPE_EVENT_LISTENER, NULL);
133   listener->callback = callback;
134   callback_ref (callback, callback_destroyed);
135   listener->user_data = user_data;
136   listener->cb_destroyed = callback_destroyed;
137   return listener;
138 }
139
140 /**
141  * atspi_event_listener_new_simple: (skip)
142  * @callback: (scope notified): An #AtspiEventListenerSimpleCB to be called
143  * when an event is fired.
144  * @callback_destroyed: A #GDestroyNotify called when the listener is freed
145  * and data associated with the callback should be freed.  Can be NULL.
146  *
147  * Returns: (transfer full): A new #AtspiEventListener.
148  */
149 AtspiEventListener *
150 atspi_event_listener_new_simple (AtspiEventListenerSimpleCB callback,
151                                  GDestroyNotify callback_destroyed)
152 {
153   AtspiEventListener *listener = g_object_new (ATSPI_TYPE_EVENT_LISTENER, NULL);
154   listener->callback = remove_datum;
155   callback_ref (remove_datum, callback_destroyed);
156   listener->user_data = callback;
157   listener->cb_destroyed = callback_destroyed;
158   return listener;
159 }
160
161 static GList *event_listeners = NULL;
162
163 static gchar *
164 convert_name_from_dbus (const char *name)
165 {
166   gchar *ret = g_malloc (g_utf8_strlen (name, -1) * 2 + 1);
167   const char *p = name;
168   gchar *q = ret;
169
170   if (!ret)
171     return NULL;
172
173   while (*p)
174   {
175     if (isupper (*p))
176     {
177       if (q > ret)
178         *q++ = '-';
179       *q++ = tolower (*p++);
180     }
181     else
182       *q++ = *p++;
183   }
184   *q = '\0';
185   return ret;
186 }
187
188 static void
189 cache_process_children_changed (AtspiEvent *event)
190 {
191   AtspiAccessible *child;
192
193   if (!G_VALUE_HOLDS (&event->any_data, ATSPI_TYPE_ACCESSIBLE) ||
194       !(event->source->cached_properties & ATSPI_CACHE_CHILDREN) ||
195       atspi_state_set_contains (event->source->states, ATSPI_STATE_MANAGES_DESCENDANTS))
196     return;
197
198   child = g_value_get_object (&event->any_data);
199
200   if (!strncmp (event->type, "object:children-changed:add", 27))
201   {
202     if (g_list_find (event->source->children, child))
203       return;
204     GList *new_list = g_list_insert (event->source->children, g_object_ref (child), event->detail1);
205     if (new_list)
206       event->source->children = new_list;
207   }
208   else if (g_list_find (event->source->children, child))
209   {
210     event->source->children = g_list_remove (event->source->children, child);
211     if (child == child->parent.app->root)
212       g_object_run_dispose (G_OBJECT (child->parent.app));
213     g_object_unref (child);
214   }
215 }
216
217 static void
218 cache_process_property_change (AtspiEvent *event)
219 {
220   if (!strcmp (event->type, "object:property-change:accessible-parent"))
221   {
222     if (event->source->accessible_parent)
223       g_object_unref (event->source->accessible_parent);
224     if (G_VALUE_HOLDS (&event->any_data, ATSPI_TYPE_ACCESSIBLE))
225     {
226       event->source->accessible_parent = g_value_dup_object (&event->any_data);
227       _atspi_accessible_add_cache (event->source, ATSPI_CACHE_PARENT);
228     }
229     else
230     {
231       event->source->accessible_parent = NULL;
232       event->source->cached_properties &= ~ATSPI_CACHE_PARENT;
233     }
234   }
235   else if (!strcmp (event->type, "object:property-change:accessible-name"))
236   {
237     if (event->source->name)
238       g_free (event->source->name);
239     if (G_VALUE_HOLDS_STRING (&event->any_data))
240     {
241       event->source->name = g_value_dup_string (&event->any_data);
242       _atspi_accessible_add_cache (event->source, ATSPI_CACHE_NAME);
243     }
244     else
245     {
246       event->source->name = NULL;
247       event->source->cached_properties &= ~ATSPI_CACHE_NAME;
248     }
249   }
250   else if (!strcmp (event->type, "object:property-change:accessible-description"))
251   {
252     if (event->source->description)
253       g_free (event->source->description);
254     if (G_VALUE_HOLDS_STRING (&event->any_data))
255     {
256       event->source->description = g_value_dup_string (&event->any_data);
257       _atspi_accessible_add_cache (event->source, ATSPI_CACHE_DESCRIPTION);
258     }
259     else
260     {
261       event->source->description = NULL;
262       event->source->cached_properties &= ~ATSPI_CACHE_DESCRIPTION;
263     }
264   }
265 }
266
267 static void
268 cache_process_state_changed (AtspiEvent *event)
269 {
270   if (event->source->states)
271     atspi_state_set_set_by_name (event->source->states, event->type + 21,
272                                  event->detail1);
273 }
274
275 static dbus_bool_t
276 demarshal_rect (DBusMessageIter *iter, AtspiRect *rect)
277 {
278   dbus_int32_t x, y, width, height;
279   DBusMessageIter iter_struct;
280
281   dbus_message_iter_recurse (iter, &iter_struct);
282   if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
283   dbus_message_iter_get_basic (&iter_struct, &x);
284   dbus_message_iter_next (&iter_struct);
285   if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
286   dbus_message_iter_get_basic (&iter_struct, &y);
287   dbus_message_iter_next (&iter_struct);
288   if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
289   dbus_message_iter_get_basic (&iter_struct, &width);
290   dbus_message_iter_next (&iter_struct);
291   if (dbus_message_iter_get_arg_type (&iter_struct) != DBUS_TYPE_INT32) return FALSE;
292   dbus_message_iter_get_basic (&iter_struct, &height);
293   rect->x = x;
294   rect->y = y;
295   rect->width = width;
296   rect->height = height;
297   return TRUE;
298 }
299
300 static gchar *
301 strdup_and_adjust_for_dbus (const char *s)
302 {
303   gchar *d = g_strdup (s);
304   gchar *p;
305   int parts = 0;
306
307   if (!d)
308     return NULL;
309
310   for (p = d; *p; p++)
311   {
312     if (*p == '-')
313     {
314       memmove (p, p + 1, g_utf8_strlen (p, -1));
315       *p = toupper (*p);
316     }
317     else if (*p == ':')
318     {
319       parts++;
320       if (parts == 2)
321         break;
322       p [1] = toupper (p [1]);
323     }
324   }
325
326   d [0] = toupper (d [0]);
327   return d;
328 }
329
330 static gboolean
331 convert_event_type_to_dbus (const char *eventType, char **categoryp, char **namep, char **detailp, char **matchrule)
332 {
333   gchar *tmp = strdup_and_adjust_for_dbus (eventType);
334   char *category = NULL, *name = NULL, *detail = NULL;
335   char *saveptr = NULL;
336
337   if (tmp == NULL) return FALSE;
338   category = strtok_r (tmp, ":", &saveptr);
339   if (category) category = g_strdup (category);
340   if (!category) goto oom;
341   name = strtok_r (NULL, ":", &saveptr);
342   if (name)
343   {
344     name = g_strdup (name);
345     if (!name) goto oom;
346     detail = strtok_r (NULL, ":", &saveptr);
347     if (detail) detail = g_strdup (detail);
348   }
349   if (matchrule)
350   {
351     *matchrule = g_strdup_printf ("type='signal',interface='org.a11y.atspi.Event.%s'", category);
352     if (!*matchrule) goto oom;
353     if (name && name [0])
354     {
355       gchar *new_str = g_strconcat (*matchrule, ",member='", name, "'", NULL);
356       g_free (*matchrule);
357       *matchrule = new_str;
358     }
359     if (detail && detail [0])
360     {
361       gchar *new_str = g_strconcat (*matchrule, ",arg0='", detail, "'", NULL);
362       g_free (*matchrule);
363       *matchrule = new_str;
364     }
365   }
366   if (categoryp) *categoryp = category;
367   else g_free (category);
368   if (namep) *namep = name;
369   else if (name) g_free (name);
370   if (detailp) *detailp = detail;
371   else if (detail) g_free (detail);
372   g_free (tmp);
373   return TRUE;
374 oom:
375   if (tmp) g_free (tmp);
376   if (category) g_free (category);
377   if (name) g_free (name);
378   if (detail) g_free (detail);
379   return FALSE;
380 }
381
382 static void
383 listener_entry_free (EventListenerEntry *e)
384 {
385   gpointer callback = (e->callback == remove_datum ? e->user_data : e->callback);
386   g_free (e->category);
387   g_free (e->name);
388   if (e->detail) g_free (e->detail);
389   callback_unref (callback);
390   g_free (e);
391 }
392
393 /**
394  * atspi_event_listener_register:
395  * @listener: The #AtspiEventListener to register against an event type.
396  * @event_type: a character string indicating the type of events for which
397  *            notification is requested.  Format is
398  *            EventClass:major_type:minor_type:detail
399  *            where all subfields other than EventClass are optional.
400  *            EventClasses include "object", "window", "mouse",
401  *            and toolkit events (e.g. "Gtk", "AWT").
402  *            Examples: "focus:", "Gtk:GtkWidget:button_press_event".
403  *
404  * Legal object event types:
405  *
406  *    (property change events)
407  *
408  *            object:property-change
409  *            object:property-change:accessible-name
410  *            object:property-change:accessible-description
411  *            object:property-change:accessible-parent
412  *            object:property-change:accessible-value
413  *            object:property-change:accessible-role
414  *            object:property-change:accessible-table-caption
415  *            object:property-change:accessible-table-column-description
416  *            object:property-change:accessible-table-column-header
417  *            object:property-change:accessible-table-row-description
418  *            object:property-change:accessible-table-row-header
419  *            object:property-change:accessible-table-summary
420  *
421  *    (other object events)
422  *
423  *            object:state-changed 
424  *            object:children-changed
425  *            object:visible-data-changed
426  *            object:selection-changed
427  *            object:text-selection-changed
428  *            object:text-changed
429  *            object:text-caret-moved
430  *            object:row-inserted
431  *            object:row-reordered
432  *            object:row-deleted
433  *            object:column-inserted
434  *            object:column-reordered
435  *            object:column-deleted
436  *            object:model-changed
437  *            object:active-descendant-changed
438  *
439  *  (window events)
440  *
441  *            window:minimize
442  *            window:maximize
443  *            window:restore
444  *            window:close
445  *            window:create
446  *            window:reparent
447  *            window:desktop-create
448  *            window:desktop-destroy
449  *            window:activate
450  *            window:deactivate
451  *            window:raise
452  *            window:lower
453  *            window:move
454  *            window:resize
455  *            window:shade
456  *            window:unshade
457  *            window:restyle
458  *
459  *  (other events)
460  *
461  *            focus:
462  *            mouse:abs
463  *            mouse:rel
464  *            mouse:b1p
465  *            mouse:b1r
466  *            mouse:b2p
467  *            mouse:b2r
468  *            mouse:b3p
469  *            mouse:b3r
470  *
471  * NOTE: this string may be UTF-8, but should not contain byte value 56
472  *            (ascii ':'), except as a delimiter, since non-UTF-8 string
473  *            delimiting functions are used internally.
474  *            In general, listening to
475  *            toolkit-specific events is not recommended.
476  *
477  * Add an in-process callback function to an existing AtspiEventListener.
478  *
479  * Returns: #TRUE if successful, otherwise #FALSE.
480  **/
481 gboolean
482 atspi_event_listener_register (AtspiEventListener *listener,
483                                              const gchar              *event_type,
484                                              GError **error)
485 {
486   /* TODO: Keep track of which events have been registered, so that we
487  * deregister all of them when the event listener is destroyed */
488
489   return atspi_event_listener_register_from_callback (listener->callback,
490                                                       listener->user_data,
491                                                       listener->cb_destroyed,
492                                                       event_type, error);
493 }
494
495 /**
496  * atspi_event_listener_register_from_callback:
497  * @callback: (scope notified): the #AtspiEventListenerCB to be registered against
498  *            an event type.
499  * @user_data: (closure): User data to be passed to the callback.
500  * @callback_destroyed: A #GDestroyNotify called when the callback is destroyed.
501  * @event_type: a character string indicating the type of events for which
502  *            notification is requested.  See #atspi_event_listener_register
503  * for a description of the format.
504  */
505 gboolean
506 atspi_event_listener_register_from_callback (AtspiEventListenerCB callback,
507                                              void *user_data,
508                                              GDestroyNotify callback_destroyed,
509                                              const gchar              *event_type,
510                                              GError **error)
511 {
512   EventListenerEntry *e;
513   char *matchrule;
514   DBusError d_error;
515   GList *new_list;
516   DBusMessage *message, *reply;
517
518   if (!callback)
519     {
520       return FALSE;
521     }
522
523   if (!event_type)
524   {
525     g_warning ("called atspi_event_listener_register_from_callback with a NULL event_type");
526     return FALSE;
527   }
528
529   e = g_new (EventListenerEntry, 1);
530   e->callback = callback;
531   e->user_data = user_data;
532   e->callback_destroyed = callback_destroyed;
533   callback_ref (callback == remove_datum ? user_data : callback,
534                 callback_destroyed);
535   if (!convert_event_type_to_dbus (event_type, &e->category, &e->name, &e->detail, &matchrule))
536   {
537     g_free (e);
538     return FALSE;
539   }
540   new_list = g_list_prepend (event_listeners, e);
541   if (!new_list)
542   {
543     listener_entry_free (e);
544     return FALSE;
545   }
546   event_listeners = new_list;
547   dbus_error_init (&d_error);
548   dbus_bus_add_match (_atspi_bus(), matchrule, &d_error);
549   if (d_error.message)
550   {
551     g_warning ("Atspi: Adding match: %s", d_error.message);
552     /* TODO: Set error */
553   }
554
555   dbus_error_init (&d_error);
556   message = dbus_message_new_method_call (atspi_bus_registry,
557         atspi_path_registry,
558         atspi_interface_registry,
559         "RegisterEvent");
560   if (!message)
561     return FALSE;
562   dbus_message_append_args (message, DBUS_TYPE_STRING, &event_type, DBUS_TYPE_INVALID);
563   reply = _atspi_dbus_send_with_reply_and_block (message, error);
564   if (reply)
565     dbus_message_unref (reply);
566
567   return TRUE;
568 }
569
570 /**
571  * atspi_event_listener_register_no_data: (skip)
572  * @callback: (scope notified): the #AtspiEventListenerSimpleCB to be
573  *            registered against an event type.
574  * @callback_destroyed: A #GDestroyNotify called when the callback is destroyed.
575  * @event_type: a character string indicating the type of events for which
576  *            notification is requested.  Format is
577  *            EventClass:major_type:minor_type:detail
578  *            where all subfields other than EventClass are optional.
579  *            EventClasses include "object", "window", "mouse",
580  *            and toolkit events (e.g. "Gtk", "AWT").
581  *            Examples: "focus:", "Gtk:GtkWidget:button_press_event".
582  *
583  * Like atspi_event_listener_register, but callback takes no user_data.
584  **/
585 gboolean
586 atspi_event_listener_register_no_data (AtspiEventListenerSimpleCB callback,
587                                  GDestroyNotify callback_destroyed,
588                                  const gchar              *event_type,
589                                  GError **error)
590 {
591   return atspi_event_listener_register_from_callback (remove_datum, callback,
592                                                       callback_destroyed,
593                                                       event_type, error);
594 }
595
596 static gboolean
597 is_superset (const gchar *super, const gchar *sub)
598 {
599   if (!super || !super [0])
600     return TRUE;
601   return (strcmp (super, sub) == 0);
602 }
603
604 /**
605  * atspi_event_listener_deregister:
606  * @listener: The #AtspiEventListener to deregister.
607  * @event_type: a string specifying the event type for which this
608  *             listener is to be deregistered.
609  *
610  * deregisters an #AtspiEventListener from the registry, for a specific
611  *             event type.
612  *
613  * Returns: #TRUE if successful, otherwise #FALSE.
614  **/
615 gboolean
616 atspi_event_listener_deregister (AtspiEventListener *listener,
617                                                const gchar              *event_type,
618                                                GError **error)
619 {
620   return atspi_event_listener_deregister_from_callback (listener->callback,
621                                                         listener->user_data,
622                                                         event_type, error);
623 }
624
625 /**
626  * atspi_event_listener_deregister_from_callback:
627  * @callback: (scope call): the #AtspiEventListenerCB registered against an
628  *            event type.
629  * @user_data: (closure): User data that was passed in for this callback.
630  * @event_type: a string specifying the event type for which this
631  *             listener is to be deregistered.
632  *
633  * deregisters an #AtspiEventListenerCB from the registry, for a specific
634  *             event type.
635  *
636  * Returns: #TRUE if successful, otherwise #FALSE.
637  **/
638 gboolean
639 atspi_event_listener_deregister_from_callback (AtspiEventListenerCB callback,
640                                                void *user_data,
641                                                const gchar              *event_type,
642                                                GError **error)
643 {
644   char *category, *name, *detail, *matchrule;
645   GList *l;
646
647   if (!convert_event_type_to_dbus (event_type, &category, &name, &detail, &matchrule))
648   {
649     return FALSE;
650   }
651   if (!callback)
652     {
653       return FALSE;
654     }
655
656   for (l = event_listeners; l;)
657   {
658     EventListenerEntry *e = l->data;
659     if (e->callback == callback &&
660         e->user_data == user_data &&
661         is_superset (category, e->category) &&
662         is_superset (name, e->name) &&
663         is_superset (detail, e->detail))
664     {
665       gboolean need_replace;
666       DBusError d_error;
667       DBusMessage *message, *reply;
668       need_replace = (l == event_listeners);
669       l = g_list_remove (l, e);
670       if (need_replace)
671         event_listeners = l;
672       dbus_error_init (&d_error);
673       dbus_bus_remove_match (_atspi_bus(), matchrule, &d_error);
674       dbus_error_init (&d_error);
675       message = dbus_message_new_method_call (atspi_bus_registry,
676             atspi_path_registry,
677             atspi_interface_registry,
678             "RegisterEvent");
679       if (!message)
680       return FALSE;
681       dbus_message_append_args (message, DBUS_TYPE_STRING, &event_type, DBUS_TYPE_INVALID);
682       reply = _atspi_dbus_send_with_reply_and_block (message, error);
683       dbus_message_unref (reply);
684
685       listener_entry_free (e);
686     }
687     else l = g_list_next (l);
688   }
689   g_free (category);
690   g_free (name);
691   if (detail) g_free (detail);
692   g_free (matchrule);
693   return TRUE;
694 }
695
696 /**
697  * atspi_event_listener_deregister_no_data:
698  * @callback: (scope call): the #AtspiEventListenerSimpleCB registered against
699  *            an event type.
700  * @event_type: a string specifying the event type for which this
701  *             listener is to be deregistered.
702  *
703  * deregisters an #AtspiEventListenerSimpleCB from the registry, for a specific
704  *             event type.
705  *
706  * Returns: #TRUE if successful, otherwise #FALSE.
707  **/
708 gboolean
709 atspi_event_listener_deregister_no_data (AtspiEventListenerSimpleCB callback,
710                                    const gchar              *event_type,
711                                    GError **error)
712 {
713   return atspi_event_listener_deregister_from_callback (remove_datum, callback,
714                                                         event_type,
715                                                         error);
716 }
717
718 static AtspiEvent *
719 atspi_event_copy (AtspiEvent *src)
720 {
721   AtspiEvent *dst = g_new0 (AtspiEvent, 1);
722   dst->type = g_strdup (src->type);
723   dst->source = g_object_ref (src->source);
724   dst->detail1 = src->detail1;
725   dst->detail2 = src->detail2;
726   g_value_init (&dst->any_data, G_VALUE_TYPE (&src->any_data));
727   g_value_copy (&src->any_data, &dst->any_data);
728   return dst;
729 }
730
731 static void
732 atspi_event_free (AtspiEvent *event)
733 {
734   g_object_unref (event->source);
735   g_free (event->type);
736   g_value_unset (&event->any_data);
737   g_free (event);
738 }
739
740 void
741 _atspi_send_event (AtspiEvent *e)
742 {
743   char *category, *name, *detail;
744   GList *l;
745
746   /* Ensure that the value is set to avoid a Python exception */
747   /* TODO: Figure out how to do this without using a private field */
748   if (e->any_data.g_type == 0)
749   {
750     g_value_init (&e->any_data, G_TYPE_INT);
751     g_value_set_int (&e->any_data, 0);
752   }
753
754   if (!convert_event_type_to_dbus (e->type, &category, &name, &detail, NULL))
755   {
756     g_warning ("Atspi: Couldn't parse event: %s\n", e->type);
757     return;
758   }
759   for (l = event_listeners; l; l = g_list_next (l))
760   {
761     EventListenerEntry *entry = l->data;
762     if (!strcmp (category, entry->category) &&
763         (entry->name == NULL || !strcmp (name, entry->name)) &&
764         (entry->detail == NULL || !strcmp (detail, entry->detail)))
765     {
766         entry->callback (atspi_event_copy (e), entry->user_data);
767     }
768   }
769   if (detail) g_free (detail);
770   g_free (name);
771   g_free (category);
772 }
773
774 DBusHandlerResult
775 _atspi_dbus_handle_event (DBusConnection *bus, DBusMessage *message, void *data)
776 {
777   char *detail = NULL;
778   const char *category = dbus_message_get_interface (message);
779   const char *member = dbus_message_get_member (message);
780   const char *signature = dbus_message_get_signature (message);
781   gchar *name;
782   gchar *converted_type;
783   DBusMessageIter iter, iter_variant;
784   dbus_message_iter_init (message, &iter);
785   AtspiEvent e;
786   dbus_int32_t detail1, detail2;
787   char *p;
788
789   if (strcmp (signature, "siiv(so)") != 0)
790   {
791     g_warning ("Got invalid signature %s for signal %s from interface %s\n", signature, member, category);
792     return DBUS_HANDLER_RESULT_HANDLED;
793   }
794
795   memset (&e, 0, sizeof (e));
796
797   if (category)
798   {
799     category = g_utf8_strrchr (category, -1, '.');
800     if (category == NULL)
801     {
802       // TODO: Error
803       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
804     }
805     category++;
806   }
807   dbus_message_iter_get_basic (&iter, &detail);
808   dbus_message_iter_next (&iter);
809   dbus_message_iter_get_basic (&iter, &detail1);
810   e.detail1 = detail1;
811   dbus_message_iter_next (&iter);
812   dbus_message_iter_get_basic (&iter, &detail2);
813   e.detail2 = detail2;
814   dbus_message_iter_next (&iter);
815
816   converted_type = convert_name_from_dbus (category);
817   name = convert_name_from_dbus (member);
818   detail = convert_name_from_dbus (detail);
819
820   if (strcasecmp  (category, name) != 0)
821   {
822     p = g_strconcat (converted_type, ":", name, NULL);
823     g_free (converted_type);
824     converted_type = p;
825   }
826   else if (detail [0] == '\0')
827   {
828     p = g_strconcat (converted_type, ":",  NULL);
829     g_free (converted_type);
830     converted_type = p;
831   }
832
833   if (detail[0] != '\0')
834   {
835     p = g_strconcat (converted_type, ":", detail, NULL);
836     g_free (converted_type);
837     converted_type = p;
838   }
839   e.type = converted_type;
840   e.source = _atspi_ref_accessible (dbus_message_get_sender(message), dbus_message_get_path(message));
841
842   dbus_message_iter_recurse (&iter, &iter_variant);
843   switch (dbus_message_iter_get_arg_type (&iter_variant))
844   {
845     case DBUS_TYPE_STRUCT:
846     {
847       AtspiRect rect;
848       if (demarshal_rect (&iter_variant, &rect))
849       {
850         g_value_init (&e.any_data, ATSPI_TYPE_RECT);
851         g_value_set_boxed (&e.any_data, &rect);
852       }
853       else
854       {
855         AtspiAccessible *accessible;
856         accessible = _atspi_dbus_return_accessible_from_iter (&iter_variant);
857         g_value_init (&e.any_data, ATSPI_TYPE_ACCESSIBLE);
858         g_value_set_instance (&e.any_data, accessible);
859         g_object_unref (accessible);    /* value now owns it */
860       }
861       break;
862     }
863     case DBUS_TYPE_STRING:
864     {
865       dbus_message_iter_get_basic (&iter_variant, &p);
866       g_value_init (&e.any_data, G_TYPE_STRING);
867       g_value_set_string (&e.any_data, p);
868       break;
869     }
870   default:
871     break;
872   }
873
874   if (!strncmp (e.type, "object:children-changed", 23))
875   {
876     cache_process_children_changed (&e);
877   }
878   else if (!strncmp (e.type, "object:property-change", 22))
879   {
880     cache_process_property_change (&e);
881   }
882   else if (!strncmp (e.type, "object:state-changed", 20))
883   {
884     cache_process_state_changed (&e);
885   }
886
887   _atspi_send_event (&e);
888
889   g_free (converted_type);
890   g_free (name);
891   g_free (detail);
892   g_object_unref (e.source);
893   g_value_unset (&e.any_data);
894   return DBUS_HANDLER_RESULT_HANDLED;
895 }
896
897 G_DEFINE_BOXED_TYPE (AtspiEvent, atspi_event, atspi_event_copy, atspi_event_free)