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