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.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include "atspi-private.h"
28 AtspiDeviceListenerCB callback;
30 GDestroyNotify callback_destroyed;
33 GObjectClass *device_parent_class;
35 static guint32 _e_id = 0;
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;
118 remove_listener (GObject *obj, gpointer data)
120 device_listeners = g_list_remove (device_listeners, obj);
123 static AtspiDeviceEvent *
124 atspi_device_event_copy (AtspiDeviceEvent *src)
126 AtspiDeviceEvent *dst = g_new0 (AtspiDeviceEvent, 1);
129 dst->type = src->type;
131 dst->hw_code = src->hw_code;
132 dst->modifiers = src->modifiers;
133 dst->timestamp = src->timestamp;
134 if (src->event_string)
135 dst->event_string = g_strdup (src->event_string);
136 dst->is_text = src->is_text;
141 atspi_device_event_free (AtspiDeviceEvent *event)
143 if (event->event_string)
144 g_free (event->event_string);
149 * Device event handler
152 atspi_device_event_dispatch (AtspiDeviceListener *listener,
153 const AtspiDeviceEvent *event)
156 gboolean handled = FALSE;
158 /* FIXME: re-enterancy hazard on this list */
159 for (l = listener->callbacks; l; l = l->next)
161 DeviceEventHandler *eh = l->data;
163 if ((handled = eh->callback (atspi_device_event_copy (event), eh->user_data)))
173 atspi_device_listener_init (AtspiDeviceListener *listener)
179 listener->id = listener_id++;
180 } while (!id_is_free (listener->id));
181 new_list = g_list_append (device_listeners, listener);
182 if (new_list) device_listeners = new_list;
186 atspi_device_listener_finalize (GObject *object)
188 AtspiDeviceListener *listener = (AtspiDeviceListener *) object;
191 for (l = listener->callbacks; l; l = l->next)
193 device_event_handler_free (l->data);
196 g_list_free (listener->callbacks);
198 device_parent_class->finalize (object);
202 atspi_device_listener_class_init (AtspiDeviceListenerClass *klass)
204 GObjectClass *object_class = (GObjectClass *) klass;
206 device_parent_class = g_type_class_peek_parent (klass);
207 object_class->finalize = atspi_device_listener_finalize;
209 klass->device_event = atspi_device_event_dispatch;
212 G_DEFINE_TYPE (AtspiDeviceListener, atspi_device_listener,
216 * atspi_device_listener_new:
217 * @callback: (scope notify): an #AtspiDeviceListenerCB callback function,
219 * @callback_destroyed: A #GDestroyNotify called when the listener is freed
220 * and data associated with the callback should be freed. Can be NULL.
221 * @user_data: (closure): a pointer to data which will be passed to the
222 * callback when invoked.
224 * Create a new #AtspiDeviceListener with a specified callback function.
226 * Returns: a pointer to a newly-created #AtspiDeviceListener.
229 AtspiDeviceListener *
230 atspi_device_listener_new (AtspiDeviceListenerCB callback,
231 GDestroyNotify callback_destroyed,
234 AtspiDeviceListener *listener = g_object_new (atspi_device_listener_get_type (), NULL);
237 atspi_device_listener_add_callback (listener, callback, callback_destroyed,
243 * atspi_device_listener_new_simple:
244 * @callback: (scope notify): an #AtspiDeviceListenerCB callback function,
246 * @callback_destroyed: A #GDestroyNotify called when the listener is freed
247 * and data associated with the callback should be freed. Can be NULL.
249 * Create a new #AtspiDeviceListener with a specified callback function.
250 * Like atspi_device_listener_new, but callback takes no user data.
252 * Returns: a pointer to a newly-created #AtspiDeviceListener.
255 AtspiDeviceListener *
256 atspi_device_listener_new_simple (AtspiDeviceListenerSimpleCB callback,
257 GDestroyNotify callback_destroyed)
259 return atspi_device_listener_new (device_remove_datum, callback_destroyed, callback);
263 * atspi_device_listener_add_callback:
264 * @listener: the #AtspiDeviceListener instance to modify.
265 * @callback: (scope notify): an #AtspiDeviceListenerCB function pointer.
266 * @user_data: (closure): a pointer to data which will be passed to the
267 * callback when invoked.
268 * @callback_destroyed: A #GDestroyNotify called when the listener is freed
269 * and data associated with the callback should be freed. Can be NULL.
271 * Add an in-process callback function to an existing #AtspiDeviceListener.
273 * Returns: #TRUE if successful, otherwise #FALSE.
277 atspi_device_listener_add_callback (AtspiDeviceListener *listener,
278 AtspiDeviceListenerCB callback,
279 GDestroyNotify callback_destroyed,
282 g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
283 DeviceEventHandler *new_handler;
285 new_handler = device_event_handler_new (callback,
286 callback_destroyed, user_data);
291 new_list = g_list_prepend (listener->callbacks, new_handler);
293 listener->callbacks = new_list;
298 * atspi_device_listener_remove_callback:
299 * @listener: the #AtspiDeviceListener instance to modify.
300 * @callback: (scope call): an #AtspiDeviceListenerCB function pointer.
302 * Remove an in-process callback function from an existing #AtspiDeviceListener.
304 * Returns: #TRUE if successful, otherwise #FALSE.
308 atspi_device_listener_remove_callback (AtspiDeviceListener *listener,
309 AtspiDeviceListenerCB callback)
311 g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
313 listener->callbacks = event_list_remove_by_cb (listener->callbacks, (void *) callback);
316 static const char *device_event_type = "(uinnisb)";
319 read_device_event_from_iter (DBusMessageIter *iter, AtspiDeviceEvent *event)
323 dbus_int16_t hw_code;
324 dbus_int16_t modifiers;
325 dbus_int32_t timestamp;
328 DBusMessageIter iter_struct;
330 dbus_message_iter_recurse (iter, &iter_struct);
332 dbus_message_iter_get_basic (&iter_struct, &type);
334 dbus_message_iter_next (&iter_struct);
336 dbus_message_iter_get_basic (&iter_struct, &id);
338 dbus_message_iter_next (&iter_struct);
340 dbus_message_iter_get_basic (&iter_struct, &hw_code);
341 event->hw_code = hw_code;
342 dbus_message_iter_next (&iter_struct);
344 dbus_message_iter_get_basic (&iter_struct, &modifiers);
345 event->modifiers = modifiers;
346 dbus_message_iter_next (&iter_struct);
348 dbus_message_iter_get_basic (&iter_struct, ×tamp);
349 event->timestamp = timestamp;
350 dbus_message_iter_next (&iter_struct);
352 dbus_message_iter_get_basic (&iter_struct, &event->event_string);
353 dbus_message_iter_next (&iter_struct);
355 dbus_message_iter_get_basic (&iter_struct, &is_text);
356 event->is_text = is_text;
360 * atspi_dbus_handle_DeviceEvent: (skip)
363 atspi_dbus_handle_DeviceEvent (DBusConnection *bus, DBusMessage *message, void *data)
365 const char *path = dbus_message_get_path (message);
367 AtspiDeviceEvent event;
368 AtspiDeviceListener *listener;
369 DBusMessageIter iter;
370 AtspiDeviceListenerClass *klass;
371 dbus_bool_t retval = FALSE;
376 if (strcmp (dbus_message_get_signature (message), "(uinnisb)") != 0)
378 g_warning ("Atspi: Unknown signature for an event");
382 if (sscanf (path, "/org/a11y/atspi/listeners/%d", &id) != 1)
384 g_warning ("Atspi: Bad listener path: %s\n", path);
388 for (l = device_listeners; l; l = g_list_next (l))
391 if (listener->id == id) break;
398 dbus_message_iter_init (message, &iter);
399 read_device_event_from_iter (&iter, &event);
400 klass = ATSPI_DEVICE_LISTENER_GET_CLASS (listener);
401 if (klass->device_event)
403 retval = (*klass->device_event) (listener, &event);
406 reply = dbus_message_new_method_return (message);
409 dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &retval, DBUS_TYPE_INVALID);
410 dbus_connection_send (_atspi_bus(), reply, NULL);
411 dbus_message_unref (reply);
413 return DBUS_HANDLER_RESULT_HANDLED;
417 _atspi_device_listener_get_path (AtspiDeviceListener *listener)
419 return g_strdup_printf ("/org/a11y/atspi/listeners/%d", listener->id);
422 G_DEFINE_BOXED_TYPE (AtspiDeviceEvent,
424 atspi_device_event_copy,
425 atspi_device_event_free)