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"
30 AtspiDeviceListenerCB callback;
32 GDestroyNotify callback_destroyed;
35 GObjectClass *device_parent_class;
41 static DeviceEventHandler *
42 device_event_handler_new (AtspiDeviceListenerCB callback,
43 GDestroyNotify callback_destroyed,
46 DeviceEventHandler *eh = g_new0 (DeviceEventHandler, 1);
48 eh->callback = callback;
49 eh->callback_destroyed = callback_destroyed;
50 eh->user_data = user_data;
56 device_remove_datum (const AtspiDeviceEvent *event, void *user_data)
58 AtspiDeviceListenerSimpleCB cb = user_data;
63 device_event_handler_free (DeviceEventHandler *eh)
66 /* TODO; Test this; it will probably crash with pyatspi for unknown reasons */
67 if (eh->callback_destroyed)
69 gpointer rea_callback = (eh->callback == device_remove_datum ?
70 eh->user_data : eh->callback);
71 (*eh->callback_destroyed) (real_callback);
78 event_list_remove_by_cb (GList *list, AtspiDeviceListenerCB callback)
82 for (l = list; l; l = next)
84 DeviceEventHandler *eh = l->data;
87 if (eh->callback == callback)
89 list = g_list_delete_link (list, l);
90 device_event_handler_free (eh);
98 * Standard event dispatcher
101 static guint listener_id = 0;
102 static GList *device_listeners = NULL;
105 id_is_free (guint id)
109 for (l = device_listeners; l; l = g_list_next (l))
111 AtspiDeviceListener *listener = l->data;
112 if (listener->id == id) return FALSE;
117 static AtspiDeviceEvent *
118 atspi_device_event_copy (AtspiDeviceEvent *src)
120 AtspiDeviceEvent *dst = g_new0 (AtspiDeviceEvent, 1);
123 dst->type = src->type;
125 dst->hw_code = src->hw_code;
126 dst->modifiers = src->modifiers;
127 dst->timestamp = src->timestamp;
128 if (src->event_string)
129 dst->event_string = g_strdup (src->event_string);
130 dst->is_text = src->is_text;
135 atspi_device_event_free (AtspiDeviceEvent *event)
137 if (event->event_string)
138 g_free (event->event_string);
143 * Device event handler
146 atspi_device_event_dispatch (AtspiDeviceListener *listener,
147 const AtspiDeviceEvent *event)
150 gboolean handled = FALSE;
152 /* FIXME: re-enterancy hazard on this list */
153 for (l = listener->callbacks; l; l = l->next)
155 DeviceEventHandler *eh = l->data;
157 if ((handled = eh->callback (atspi_device_event_copy (event), eh->user_data)))
167 atspi_device_listener_init (AtspiDeviceListener *listener)
173 listener->id = listener_id++;
174 } while (!id_is_free (listener->id));
175 new_list = g_list_append (device_listeners, listener);
176 if (new_list) device_listeners = new_list;
180 atspi_device_listener_finalize (GObject *object)
182 AtspiDeviceListener *listener = (AtspiDeviceListener *) object;
185 for (l = listener->callbacks; l; l = l->next)
187 device_event_handler_free (l->data);
190 g_list_free (listener->callbacks);
192 device_parent_class->finalize (object);
196 atspi_device_listener_class_init (AtspiDeviceListenerClass *klass)
198 GObjectClass *object_class = (GObjectClass *) klass;
200 device_parent_class = g_type_class_peek_parent (klass);
201 object_class->finalize = atspi_device_listener_finalize;
203 klass->device_event = atspi_device_event_dispatch;
206 G_DEFINE_TYPE (AtspiDeviceListener, atspi_device_listener,
210 * atspi_device_listener_new:
211 * @callback: (scope notify): an #AtspiDeviceListenerCB callback function,
213 * @callback_destroyed: A #GDestroyNotify called when the listener is freed
214 * and data associated with the callback should be freed. Can be NULL.
215 * @user_data: (closure): a pointer to data which will be passed to the
216 * callback when invoked.
218 * Create a new #AtspiDeviceListener with a specified callback function.
220 * Returns: a pointer to a newly-created #AtspiDeviceListener.
223 AtspiDeviceListener *
224 atspi_device_listener_new (AtspiDeviceListenerCB callback,
225 GDestroyNotify callback_destroyed,
228 AtspiDeviceListener *listener = g_object_new (atspi_device_listener_get_type (), NULL);
231 atspi_device_listener_add_callback (listener, callback, callback_destroyed,
237 * atspi_device_listener_new_simple:
238 * @callback: (scope notify): an #AtspiDeviceListenerCB callback function,
240 * @callback_destroyed: A #GDestroyNotify called when the listener is freed
241 * and data associated with the callback should be freed. Can be NULL.
243 * Create a new #AtspiDeviceListener with a specified callback function.
244 * Like atspi_device_listener_new, but callback takes no user data.
246 * Returns: a pointer to a newly-created #AtspiDeviceListener.
249 AtspiDeviceListener *
250 atspi_device_listener_new_simple (AtspiDeviceListenerSimpleCB callback,
251 GDestroyNotify callback_destroyed)
253 return atspi_device_listener_new (device_remove_datum, callback_destroyed, callback);
257 * atspi_device_listener_add_callback:
258 * @listener: the #AtspiDeviceListener instance to modify.
259 * @callback: (scope notify): an #AtspiDeviceListenerCB function pointer.
260 * @user_data: (closure): a pointer to data which will be passed to the
261 * callback when invoked.
262 * @callback_destroyed: A #GDestroyNotify called when the listener is freed
263 * and data associated with the callback should be freed. Can be NULL.
265 * Add an in-process callback function to an existing #AtspiDeviceListener.
267 * Returns: #TRUE if successful, otherwise #FALSE.
271 atspi_device_listener_add_callback (AtspiDeviceListener *listener,
272 AtspiDeviceListenerCB callback,
273 GDestroyNotify callback_destroyed,
276 g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
277 DeviceEventHandler *new_handler;
279 new_handler = device_event_handler_new (callback,
280 callback_destroyed, user_data);
285 new_list = g_list_prepend (listener->callbacks, new_handler);
287 listener->callbacks = new_list;
292 * atspi_device_listener_remove_callback:
293 * @listener: the #AtspiDeviceListener instance to modify.
294 * @callback: (scope call): an #AtspiDeviceListenerCB function pointer.
296 * Remove an in-process callback function from an existing #AtspiDeviceListener.
298 * Returns: #TRUE if successful, otherwise #FALSE.
302 atspi_device_listener_remove_callback (AtspiDeviceListener *listener,
303 AtspiDeviceListenerCB callback)
305 g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
307 listener->callbacks = event_list_remove_by_cb (listener->callbacks, (void *) callback);
311 read_device_event_from_iter (DBusMessageIter *iter, AtspiDeviceEvent *event)
315 dbus_int16_t hw_code;
316 dbus_int16_t modifiers;
317 dbus_int32_t timestamp;
319 DBusMessageIter iter_struct;
321 dbus_message_iter_recurse (iter, &iter_struct);
323 dbus_message_iter_get_basic (&iter_struct, &type);
325 dbus_message_iter_next (&iter_struct);
327 dbus_message_iter_get_basic (&iter_struct, &id);
329 dbus_message_iter_next (&iter_struct);
331 dbus_message_iter_get_basic (&iter_struct, &hw_code);
332 event->hw_code = hw_code;
333 dbus_message_iter_next (&iter_struct);
335 dbus_message_iter_get_basic (&iter_struct, &modifiers);
336 event->modifiers = modifiers;
337 dbus_message_iter_next (&iter_struct);
339 dbus_message_iter_get_basic (&iter_struct, ×tamp);
340 event->timestamp = timestamp;
341 dbus_message_iter_next (&iter_struct);
343 dbus_message_iter_get_basic (&iter_struct, &event->event_string);
344 dbus_message_iter_next (&iter_struct);
346 dbus_message_iter_get_basic (&iter_struct, &is_text);
347 event->is_text = is_text;
351 * atspi_dbus_handle_DeviceEvent: (skip)
354 atspi_dbus_handle_DeviceEvent (DBusConnection *bus, DBusMessage *message, void *data)
356 const char *path = dbus_message_get_path (message);
358 AtspiDeviceEvent event;
359 AtspiDeviceListener *listener;
360 DBusMessageIter iter;
361 AtspiDeviceListenerClass *klass;
362 dbus_bool_t retval = FALSE;
366 if (strcmp (dbus_message_get_signature (message), "(uinnisb)") != 0)
368 g_warning ("Atspi: Unknown signature for an event");
372 if (sscanf (path, "/org/a11y/atspi/listeners/%d", &id) != 1)
374 g_warning ("Atspi: Bad listener path: %s\n", path);
378 for (l = device_listeners; l; l = g_list_next (l))
381 if (listener->id == id) break;
388 dbus_message_iter_init (message, &iter);
389 read_device_event_from_iter (&iter, &event);
390 klass = ATSPI_DEVICE_LISTENER_GET_CLASS (listener);
391 if (klass->device_event)
393 retval = (*klass->device_event) (listener, &event);
396 reply = dbus_message_new_method_return (message);
399 dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &retval, DBUS_TYPE_INVALID);
400 dbus_connection_send (_atspi_bus(), reply, NULL);
401 dbus_message_unref (reply);
403 return DBUS_HANDLER_RESULT_HANDLED;
407 _atspi_device_listener_get_path (AtspiDeviceListener *listener)
409 return g_strdup_printf ("/org/a11y/atspi/listeners/%d", listener->id);
412 G_DEFINE_BOXED_TYPE (AtspiDeviceEvent,
414 atspi_device_event_copy,
415 atspi_device_event_free)