2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2002 Ximian Inc.
6 * Copyright 2002 Sun Microsystems, Inc.
7 * Copyright 2010, 2011 Novell, Inc.
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.
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.
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.
25 #include "atspi-private.h"
31 AtspiEventListenerCB callback;
33 GDestroyNotify callback_destroyed;
39 G_DEFINE_TYPE (AtspiEventListener, atspi_event_listener, G_TYPE_OBJECT)
42 atspi_event_listener_init (AtspiEventListener *listener)
47 atspi_event_listener_class_init (AtspiEventListenerClass *klass)
52 remove_datum (const AtspiEvent *event, void *user_data)
54 AtspiEventListenerSimpleCB cb = user_data;
61 GDestroyNotify callback_destroyed;
64 static GHashTable *callbacks;
67 callback_ref (void *callback, GDestroyNotify callback_destroyed)
73 callbacks = g_hash_table_new (g_direct_hash, g_direct_equal);
78 info = g_hash_table_lookup (callbacks, callback);
81 info = g_new (CallbackInfo, 1);
82 info->callback = callback;
83 info->callback_destroyed = callback_destroyed;
85 g_hash_table_insert (callbacks, callback, info);
92 callback_unref (gpointer callback)
98 info = g_hash_table_lookup (callbacks, callback);
101 g_warning ("Atspi: Dereferencing invalid callback %p\n", callback);
105 if (info->ref_count == 0)
108 /* TODO: Figure out why this seg faults from Python */
109 if (info->callback_destroyed)
110 (*info->callback_destroyed) (info->callback);
113 g_hash_table_remove (callbacks, callback);
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.
125 * Returns: (transfer full): A new #AtspiEventListener.
128 atspi_event_listener_new (AtspiEventListenerCB callback,
130 GDestroyNotify callback_destroyed)
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;
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.
147 * Returns: (transfer full): A new #AtspiEventListener.
150 atspi_event_listener_new_simple (AtspiEventListenerSimpleCB callback,
151 GDestroyNotify callback_destroyed)
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;
161 static GList *event_listeners = NULL;
164 convert_name_from_dbus (const char *name)
166 gchar *ret = g_malloc (g_utf8_strlen (name, -1) * 2 + 1);
167 const char *p = name;
179 *q++ = tolower (*p++);
189 cache_process_children_changed (AtspiEvent *event)
191 AtspiAccessible *child;
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))
198 child = g_value_get_object (&event->any_data);
200 if (!strncmp (event->type, "object:children-changed:add", 27))
202 if (g_list_find (event->source->children, child))
204 GList *new_list = g_list_insert (event->source->children, g_object_ref (child), event->detail1);
206 event->source->children = new_list;
208 else if (g_list_find (event->source->children, child))
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);
218 cache_process_property_change (AtspiEvent *event)
220 if (!strcmp (event->type, "object:property-change:accessible-parent"))
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))
226 event->source->accessible_parent = g_value_dup_object (&event->any_data);
227 _atspi_accessible_add_cache (event->source, ATSPI_CACHE_PARENT);
231 event->source->accessible_parent = NULL;
232 event->source->cached_properties &= ~ATSPI_CACHE_PARENT;
235 else if (!strcmp (event->type, "object:property-change:accessible-name"))
237 if (event->source->name)
238 g_free (event->source->name);
239 if (G_VALUE_HOLDS_STRING (&event->any_data))
241 event->source->name = g_value_dup_string (&event->any_data);
242 _atspi_accessible_add_cache (event->source, ATSPI_CACHE_NAME);
246 event->source->name = NULL;
247 event->source->cached_properties &= ~ATSPI_CACHE_NAME;
250 else if (!strcmp (event->type, "object:property-change:accessible-description"))
252 if (event->source->description)
253 g_free (event->source->description);
254 if (G_VALUE_HOLDS_STRING (&event->any_data))
256 event->source->description = g_value_dup_string (&event->any_data);
257 _atspi_accessible_add_cache (event->source, ATSPI_CACHE_DESCRIPTION);
261 event->source->description = NULL;
262 event->source->cached_properties &= ~ATSPI_CACHE_DESCRIPTION;
268 cache_process_state_changed (AtspiEvent *event)
270 if (event->source->states)
271 atspi_state_set_set_by_name (event->source->states, event->type + 21,
276 demarshal_rect (DBusMessageIter *iter, AtspiRect *rect)
278 dbus_int32_t x, y, width, height;
279 DBusMessageIter iter_struct;
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);
296 rect->height = height;
301 strdup_and_adjust_for_dbus (const char *s)
303 gchar *d = g_strdup (s);
314 memmove (p, p + 1, g_utf8_strlen (p, -1));
322 p [1] = toupper (p [1]);
326 d [0] = toupper (d [0]);
331 convert_event_type_to_dbus (const char *eventType, char **categoryp, char **namep, char **detailp, char **matchrule)
333 gchar *tmp = strdup_and_adjust_for_dbus (eventType);
334 char *category = NULL, *name = NULL, *detail = NULL;
335 char *saveptr = NULL;
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);
344 name = g_strdup (name);
346 detail = strtok_r (NULL, ":", &saveptr);
347 if (detail) detail = g_strdup (detail);
351 *matchrule = g_strdup_printf ("type='signal',interface='org.a11y.atspi.Event.%s'", category);
352 if (!*matchrule) goto oom;
353 if (name && name [0])
355 gchar *new_str = g_strconcat (*matchrule, ",member='", name, "'", NULL);
357 *matchrule = new_str;
359 if (detail && detail [0])
361 gchar *new_str = g_strconcat (*matchrule, ",arg0='", detail, "'", NULL);
363 *matchrule = new_str;
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);
375 if (tmp) g_free (tmp);
376 if (category) g_free (category);
377 if (name) g_free (name);
378 if (detail) g_free (detail);
383 listener_entry_free (EventListenerEntry *e)
385 gpointer callback = (e->callback == remove_datum ? e->user_data : e->callback);
386 g_free (e->category);
388 if (e->detail) g_free (e->detail);
389 callback_unref (callback);
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".
404 * Legal object event types:
406 * (property change events)
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
421 * (other object events)
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
433 * object:column-inserted
434 * object:column-reordered
435 * object:column-deleted
436 * object:model-changed
437 * object:active-descendant-changed
447 * window:desktop-create
448 * window:desktop-destroy
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.
477 * Add an in-process callback function to an existing AtspiEventListener.
479 * Returns: #TRUE if successful, otherwise #FALSE.
482 atspi_event_listener_register (AtspiEventListener *listener,
483 const gchar *event_type,
486 /* TODO: Keep track of which events have been registered, so that we
487 * deregister all of them when the event listener is destroyed */
489 return atspi_event_listener_register_from_callback (listener->callback,
491 listener->cb_destroyed,
496 * atspi_event_listener_register_from_callback:
497 * @callback: (scope notified): the #AtspiEventListenerCB to be registered against
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.
506 atspi_event_listener_register_from_callback (AtspiEventListenerCB callback,
508 GDestroyNotify callback_destroyed,
509 const gchar *event_type,
512 EventListenerEntry *e;
516 DBusMessage *message, *reply;
525 g_warning ("called atspi_event_listener_register_from_callback with a NULL event_type");
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,
535 if (!convert_event_type_to_dbus (event_type, &e->category, &e->name, &e->detail, &matchrule))
540 new_list = g_list_prepend (event_listeners, e);
543 listener_entry_free (e);
546 event_listeners = new_list;
547 dbus_error_init (&d_error);
548 dbus_bus_add_match (_atspi_bus(), matchrule, &d_error);
551 g_warning ("Atspi: Adding match: %s", d_error.message);
552 /* TODO: Set error */
555 dbus_error_init (&d_error);
556 message = dbus_message_new_method_call (atspi_bus_registry,
558 atspi_interface_registry,
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);
565 dbus_message_unref (reply);
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".
583 * Like atspi_event_listener_register, but callback takes no user_data.
586 atspi_event_listener_register_no_data (AtspiEventListenerSimpleCB callback,
587 GDestroyNotify callback_destroyed,
588 const gchar *event_type,
591 return atspi_event_listener_register_from_callback (remove_datum, callback,
597 is_superset (const gchar *super, const gchar *sub)
599 if (!super || !super [0])
601 return (strcmp (super, sub) == 0);
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.
610 * deregisters an #AtspiEventListener from the registry, for a specific
613 * Returns: #TRUE if successful, otherwise #FALSE.
616 atspi_event_listener_deregister (AtspiEventListener *listener,
617 const gchar *event_type,
620 return atspi_event_listener_deregister_from_callback (listener->callback,
626 * atspi_event_listener_deregister_from_callback:
627 * @callback: (scope call): the #AtspiEventListenerCB registered against an
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.
633 * deregisters an #AtspiEventListenerCB from the registry, for a specific
636 * Returns: #TRUE if successful, otherwise #FALSE.
639 atspi_event_listener_deregister_from_callback (AtspiEventListenerCB callback,
641 const gchar *event_type,
644 char *category, *name, *detail, *matchrule;
647 if (!convert_event_type_to_dbus (event_type, &category, &name, &detail, &matchrule))
656 for (l = event_listeners; l;)
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))
665 gboolean need_replace;
667 DBusMessage *message, *reply;
668 need_replace = (l == event_listeners);
669 l = g_list_remove (l, e);
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,
677 atspi_interface_registry,
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);
685 listener_entry_free (e);
687 else l = g_list_next (l);
691 if (detail) g_free (detail);
697 * atspi_event_listener_deregister_no_data:
698 * @callback: (scope call): the #AtspiEventListenerSimpleCB registered against
700 * @event_type: a string specifying the event type for which this
701 * listener is to be deregistered.
703 * deregisters an #AtspiEventListenerSimpleCB from the registry, for a specific
706 * Returns: #TRUE if successful, otherwise #FALSE.
709 atspi_event_listener_deregister_no_data (AtspiEventListenerSimpleCB callback,
710 const gchar *event_type,
713 return atspi_event_listener_deregister_from_callback (remove_datum, callback,
719 atspi_event_copy (AtspiEvent *src)
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);
732 atspi_event_free (AtspiEvent *event)
734 g_object_unref (event->source);
735 g_free (event->type);
736 g_value_unset (&event->any_data);
741 _atspi_send_event (AtspiEvent *e)
743 char *category, *name, *detail;
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)
750 g_value_init (&e->any_data, G_TYPE_INT);
751 g_value_set_int (&e->any_data, 0);
754 if (!convert_event_type_to_dbus (e->type, &category, &name, &detail, NULL))
756 g_warning ("Atspi: Couldn't parse event: %s\n", e->type);
759 for (l = event_listeners; l; l = g_list_next (l))
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)))
766 entry->callback (atspi_event_copy (e), entry->user_data);
769 if (detail) g_free (detail);
775 _atspi_dbus_handle_event (DBusConnection *bus, DBusMessage *message, void *data)
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);
782 gchar *converted_type;
783 DBusMessageIter iter, iter_variant;
784 dbus_message_iter_init (message, &iter);
786 dbus_int32_t detail1, detail2;
789 if (strcmp (signature, "siiv(so)") != 0)
791 g_warning ("Got invalid signature %s for signal %s from interface %s\n", signature, member, category);
792 return DBUS_HANDLER_RESULT_HANDLED;
795 memset (&e, 0, sizeof (e));
799 category = g_utf8_strrchr (category, -1, '.');
800 if (category == NULL)
803 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
807 dbus_message_iter_get_basic (&iter, &detail);
808 dbus_message_iter_next (&iter);
809 dbus_message_iter_get_basic (&iter, &detail1);
811 dbus_message_iter_next (&iter);
812 dbus_message_iter_get_basic (&iter, &detail2);
814 dbus_message_iter_next (&iter);
816 converted_type = convert_name_from_dbus (category);
817 name = convert_name_from_dbus (member);
818 detail = convert_name_from_dbus (detail);
820 if (strcasecmp (category, name) != 0)
822 p = g_strconcat (converted_type, ":", name, NULL);
823 g_free (converted_type);
826 else if (detail [0] == '\0')
828 p = g_strconcat (converted_type, ":", NULL);
829 g_free (converted_type);
833 if (detail[0] != '\0')
835 p = g_strconcat (converted_type, ":", detail, NULL);
836 g_free (converted_type);
839 e.type = converted_type;
840 e.source = _atspi_ref_accessible (dbus_message_get_sender(message), dbus_message_get_path(message));
842 dbus_message_iter_recurse (&iter, &iter_variant);
843 switch (dbus_message_iter_get_arg_type (&iter_variant))
845 case DBUS_TYPE_STRUCT:
848 if (demarshal_rect (&iter_variant, &rect))
850 g_value_init (&e.any_data, ATSPI_TYPE_RECT);
851 g_value_set_boxed (&e.any_data, &rect);
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 */
863 case DBUS_TYPE_STRING:
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);
874 if (!strncmp (e.type, "object:children-changed", 23))
876 cache_process_children_changed (&e);
878 else if (!strncmp (e.type, "object:property-change", 22))
880 cache_process_property_change (&e);
882 else if (!strncmp (e.type, "object:state-changed", 20))
884 cache_process_state_changed (&e);
887 _atspi_send_event (&e);
889 g_free (converted_type);
892 g_object_unref (e.source);
893 g_value_unset (&e.any_data);
894 return DBUS_HANDLER_RESULT_HANDLED;
897 G_DEFINE_BOXED_TYPE (AtspiEvent, atspi_event, atspi_event_copy, atspi_event_free)