Update copyright and add some missing license info
[platform/upstream/at-spi2-core.git] / atspi / atspi-device-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 <stdio.h>
27
28 typedef struct
29 {
30   AtspiDeviceListenerCB    callback;
31   gpointer user_data;
32   GDestroyNotify callback_destroyed;
33 } DeviceEventHandler;
34
35 GObjectClass *device_parent_class;
36
37 /*
38  * Misc. helpers.
39  */
40
41 static DeviceEventHandler *
42 device_event_handler_new (AtspiDeviceListenerCB callback,
43                           GDestroyNotify callback_destroyed,
44                           gpointer user_data)
45 {
46   DeviceEventHandler *eh = g_new0 (DeviceEventHandler, 1);
47
48   eh->callback = callback;
49   eh->callback_destroyed = callback_destroyed;
50   eh->user_data = user_data;
51
52   return eh;
53 }
54
55 static gboolean
56 device_remove_datum (const AtspiDeviceEvent *event, void *user_data)
57 {
58   AtspiDeviceListenerSimpleCB cb = user_data;
59   return cb (event);
60 }
61   
62 static void
63 device_event_handler_free (DeviceEventHandler *eh)
64 {
65 #if 0
66   /* TODO; Test this; it will probably crash with pyatspi for unknown reasons */
67   if (eh->callback_destroyed)
68   {
69     gpointer rea_callback = (eh->callback == device_remove_datum ?
70                             eh->user_data : eh->callback);
71     (*eh->callback_destroyed) (real_callback);
72   }
73 #endif
74   g_free (eh);
75 }
76
77 static GList *
78 event_list_remove_by_cb (GList *list, AtspiDeviceListenerCB callback)
79 {
80   GList *l, *next;
81         
82   for (l = list; l; l = next)
83     {
84       DeviceEventHandler *eh = l->data;
85       next = l->next;
86
87       if (eh->callback == callback)
88       {
89         list = g_list_delete_link (list, l);
90         device_event_handler_free (eh);
91       }
92     }
93
94   return list;
95 }
96
97 /*
98  * Standard event dispatcher
99  */
100
101 static guint listener_id = 0;
102 static GList *device_listeners = NULL;
103
104 static gboolean
105 id_is_free (guint id)
106 {
107   GList *l;
108
109   for (l = device_listeners; l; l = g_list_next (l))
110   {
111     AtspiDeviceListener *listener = l->data;
112     if (listener->id == id) return FALSE;
113   }
114   return TRUE;
115 }
116
117 static AtspiDeviceEvent *
118 atspi_device_event_copy (AtspiDeviceEvent *src)
119 {
120   AtspiDeviceEvent *dst = g_new0 (AtspiDeviceEvent, 1);
121   if (!dst)
122     return NULL;
123   dst->type = src->type;
124   dst->id = src->id;
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;
131   return dst;
132 }
133
134 void
135 atspi_device_event_free (AtspiDeviceEvent *event)
136 {
137   if (event->event_string)
138     g_free (event->event_string);
139   g_free (event);
140 }
141
142 /* 
143  * Device event handler
144  */
145 static gboolean
146 atspi_device_event_dispatch (AtspiDeviceListener               *listener,
147                    const AtspiDeviceEvent *event)
148 {
149   GList *l;
150   gboolean handled = FALSE;
151
152   /* FIXME: re-enterancy hazard on this list */
153   for (l = listener->callbacks; l; l = l->next)
154     {
155       DeviceEventHandler *eh = l->data;
156
157       if ((handled = eh->callback (atspi_device_event_copy (event), eh->user_data)))
158         {
159           break;
160         }
161     }
162
163   return handled;
164 }
165
166 static void
167 atspi_device_listener_init (AtspiDeviceListener *listener)
168 {
169   GList *new_list;
170
171   do
172   {
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;
177 }
178
179 static void
180 atspi_device_listener_finalize (GObject *object)
181 {
182   AtspiDeviceListener *listener = (AtspiDeviceListener *) object;
183   GList *l;
184   
185   for (l = listener->callbacks; l; l = l->next)
186     {
187       device_event_handler_free (l->data);
188     }
189   
190   g_list_free (listener->callbacks);
191
192   device_parent_class->finalize (object);
193 }
194
195 static void
196 atspi_device_listener_class_init (AtspiDeviceListenerClass *klass)
197 {
198   GObjectClass *object_class = (GObjectClass *) klass;
199
200   device_parent_class = g_type_class_peek_parent (klass);
201   object_class->finalize = atspi_device_listener_finalize;
202
203   klass->device_event = atspi_device_event_dispatch;
204 }
205
206 G_DEFINE_TYPE (AtspiDeviceListener, atspi_device_listener,
207                           G_TYPE_OBJECT)
208
209 /**
210  * atspi_device_listener_new:
211  * @callback: (scope notify): an #AtspiDeviceListenerCB callback function,
212  *            or NULL.
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.
217  *
218  * Create a new #AtspiDeviceListener with a specified callback function.
219  *
220  * Returns: a pointer to a newly-created #AtspiDeviceListener.
221  *
222  **/
223 AtspiDeviceListener *
224 atspi_device_listener_new (AtspiDeviceListenerCB callback,
225                            GDestroyNotify callback_destroyed,
226                            void *user_data)
227 {
228   AtspiDeviceListener *listener = g_object_new (atspi_device_listener_get_type (), NULL);
229
230   if (callback)
231     atspi_device_listener_add_callback (listener, callback, callback_destroyed,
232                                        user_data);
233   return listener;
234 }
235
236 /**
237  * atspi_device_listener_new_simple:
238  * @callback: (scope notify): an #AtspiDeviceListenerCB callback function,
239  *            or NULL.
240  * @callback_destroyed: A #GDestroyNotify called when the listener is freed
241  * and data associated with the callback should be freed.  Can be NULL.
242  *
243  * Create a new #AtspiDeviceListener with a specified callback function.
244  * Like atspi_device_listener_new, but callback takes no user data.
245  *
246  * Returns: a pointer to a newly-created #AtspiDeviceListener.
247  *
248  **/
249 AtspiDeviceListener *
250 atspi_device_listener_new_simple (AtspiDeviceListenerSimpleCB callback,
251                            GDestroyNotify callback_destroyed)
252 {
253   return atspi_device_listener_new (device_remove_datum, callback_destroyed, callback);
254 }
255
256 /**
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.
264  *
265  * Add an in-process callback function to an existing #AtspiDeviceListener.
266  *
267  * Returns: #TRUE if successful, otherwise #FALSE.
268  *
269  **/
270 void
271 atspi_device_listener_add_callback (AtspiDeviceListener  *listener,
272                              AtspiDeviceListenerCB callback,
273                              GDestroyNotify callback_destroyed,
274                              void                      *user_data)
275 {
276   g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
277   DeviceEventHandler *new_handler;
278
279   new_handler = device_event_handler_new (callback,
280                                           callback_destroyed, user_data);
281
282   if (new_handler)
283   {
284     GList *new_list;
285     new_list = g_list_prepend (listener->callbacks, new_handler);
286     if (new_list)
287       listener->callbacks = new_list;
288   }
289 }
290
291 /**
292  * atspi_device_listener_remove_callback:
293  * @listener: the #AtspiDeviceListener instance to modify.
294  * @callback: (scope call): an #AtspiDeviceListenerCB function pointer.
295  *
296  * Remove an in-process callback function from an existing #AtspiDeviceListener.
297  *
298  * Returns: #TRUE if successful, otherwise #FALSE.
299  *
300  **/
301 void
302 atspi_device_listener_remove_callback (AtspiDeviceListener  *listener,
303                                 AtspiDeviceListenerCB callback)
304 {
305   g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
306
307   listener->callbacks = event_list_remove_by_cb (listener->callbacks, (void *) callback);
308 }
309
310 static void
311 read_device_event_from_iter (DBusMessageIter *iter, AtspiDeviceEvent *event)
312 {
313   dbus_uint32_t type;
314   dbus_int32_t id;
315   dbus_int16_t hw_code;
316   dbus_int16_t modifiers;
317   dbus_int32_t timestamp;
318   dbus_bool_t is_text;
319   DBusMessageIter iter_struct;
320
321   dbus_message_iter_recurse (iter, &iter_struct);
322
323   dbus_message_iter_get_basic (&iter_struct, &type);
324   event->type = type;
325   dbus_message_iter_next (&iter_struct);
326
327   dbus_message_iter_get_basic (&iter_struct, &id);
328   event->id = id;
329   dbus_message_iter_next (&iter_struct);
330
331   dbus_message_iter_get_basic (&iter_struct, &hw_code);
332   event->hw_code = hw_code;
333   dbus_message_iter_next (&iter_struct);
334
335   dbus_message_iter_get_basic (&iter_struct, &modifiers);
336   event->modifiers = modifiers;
337   dbus_message_iter_next (&iter_struct);
338
339   dbus_message_iter_get_basic (&iter_struct, &timestamp);
340   event->timestamp = timestamp;
341   dbus_message_iter_next (&iter_struct);
342
343   dbus_message_iter_get_basic (&iter_struct, &event->event_string);
344   dbus_message_iter_next (&iter_struct);
345
346   dbus_message_iter_get_basic (&iter_struct, &is_text);
347   event->is_text = is_text;
348 }
349
350 /*
351  * atspi_dbus_handle_DeviceEvent: (skip)
352  */
353 DBusHandlerResult
354 atspi_dbus_handle_DeviceEvent (DBusConnection *bus, DBusMessage *message, void *data)
355 {
356   const char *path = dbus_message_get_path (message);
357   int id;
358   AtspiDeviceEvent event;
359     AtspiDeviceListener *listener;
360   DBusMessageIter iter;
361   AtspiDeviceListenerClass *klass;
362   dbus_bool_t retval = FALSE;
363   GList *l;
364   DBusMessage *reply;
365
366   if (strcmp (dbus_message_get_signature (message), "(uinnisb)") != 0)
367   {
368     g_warning ("Atspi: Unknown signature for an event");
369     goto done;
370   }
371
372   if (sscanf (path, "/org/a11y/atspi/listeners/%d", &id) != 1)
373   {
374     g_warning ("Atspi: Bad listener path: %s\n", path);
375     goto done;
376   }
377
378   for (l = device_listeners; l; l = g_list_next (l))
379   {
380     listener = l->data;
381     if (listener->id == id) break;
382   }
383
384   if (!l)
385   {
386     goto done;
387   }
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)
392   {
393     retval = (*klass->device_event) (listener, &event);
394   }
395 done:
396   reply = dbus_message_new_method_return (message);
397   if (reply)
398   {
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);
402   }
403   return DBUS_HANDLER_RESULT_HANDLED;
404 }
405
406 gchar *
407 _atspi_device_listener_get_path (AtspiDeviceListener *listener)
408 {
409   return g_strdup_printf ("/org/a11y/atspi/listeners/%d", listener->id);
410 }
411
412 G_DEFINE_BOXED_TYPE (AtspiDeviceEvent,
413                      atspi_device_event,
414                      atspi_device_event_copy,
415                      atspi_device_event_free)