Imported version 2.7.91
[platform/core/uifw/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 (const AtspiDeviceEvent *src)
119 {
120   AtspiDeviceEvent *dst = g_new0 (AtspiDeviceEvent, 1);
121   dst->type = src->type;
122   dst->id = src->id;
123   dst->hw_code = src->hw_code;
124   dst->modifiers = src->modifiers;
125   dst->timestamp = src->timestamp;
126   if (src->event_string)
127     dst->event_string = g_strdup (src->event_string);
128   dst->is_text = src->is_text;
129   return dst;
130 }
131
132 void
133 atspi_device_event_free (AtspiDeviceEvent *event)
134 {
135   if (event->event_string)
136     g_free (event->event_string);
137   g_free (event);
138 }
139
140 /* 
141  * Device event handler
142  */
143 static gboolean
144 atspi_device_event_dispatch (AtspiDeviceListener               *listener,
145                    const AtspiDeviceEvent *event)
146 {
147   GList *l;
148   gboolean handled = FALSE;
149
150   /* FIXME: re-enterancy hazard on this list */
151   for (l = listener->callbacks; l; l = l->next)
152     {
153       DeviceEventHandler *eh = l->data;
154
155       if ((handled = eh->callback (atspi_device_event_copy (event), eh->user_data)))
156         {
157           break;
158         }
159     }
160
161   return handled;
162 }
163
164 static void
165 atspi_device_listener_init (AtspiDeviceListener *listener)
166 {
167
168   do
169   {
170     listener->id = listener_id++;
171   } while (!id_is_free (listener->id));
172   device_listeners = g_list_append (device_listeners, listener);
173 }
174
175 static void
176 atspi_device_listener_finalize (GObject *object)
177 {
178   AtspiDeviceListener *listener = (AtspiDeviceListener *) object;
179   GList *l;
180   
181   for (l = listener->callbacks; l; l = l->next)
182     {
183       device_event_handler_free (l->data);
184     }
185   
186   g_list_free (listener->callbacks);
187
188   device_parent_class->finalize (object);
189 }
190
191 static void
192 atspi_device_listener_class_init (AtspiDeviceListenerClass *klass)
193 {
194   GObjectClass *object_class = (GObjectClass *) klass;
195
196   device_parent_class = g_type_class_peek_parent (klass);
197   object_class->finalize = atspi_device_listener_finalize;
198
199   klass->device_event = atspi_device_event_dispatch;
200 }
201
202 G_DEFINE_TYPE (AtspiDeviceListener, atspi_device_listener,
203                           G_TYPE_OBJECT)
204
205 /**
206  * atspi_device_listener_new:
207  * @callback: (scope notified): an #AtspiDeviceListenerCB callback function,
208  *            or NULL.
209  * @user_data: (closure): a pointer to data which will be passed to the
210  * callback when invoked.
211  * @callback_destroyed: A #GDestroyNotify called when the listener is freed
212  * and data associated with the callback should be freed. It can be NULL.
213  *
214  * Creates a new #AtspiDeviceListener with a specified callback function.
215  *
216  * Returns: (transfer full): a pointer to a newly-created #AtspiDeviceListener.
217  *
218  **/
219 AtspiDeviceListener *
220 atspi_device_listener_new (AtspiDeviceListenerCB callback,
221                            void *user_data,
222                            GDestroyNotify callback_destroyed)
223 {
224   AtspiDeviceListener *listener = g_object_new (atspi_device_listener_get_type (), NULL);
225
226   if (callback)
227     atspi_device_listener_add_callback (listener, callback, callback_destroyed,
228                                        user_data);
229   return listener;
230 }
231
232 /**
233  * atspi_device_listener_new_simple:
234  * @callback: (scope notified): an #AtspiDeviceListenerCB callback function,
235  *            or NULL.
236  * @callback_destroyed: A #GDestroyNotify called when the listener is freed
237  * and data associated with the callback should be freed.  It an be NULL.
238  *
239  * Creates a new #AtspiDeviceListener with a specified callback function.
240  * This method is similar to #atspi_device_listener_new, but callback
241  * takes no user data.
242  *
243  * Returns: a pointer to a newly-created #AtspiDeviceListener.
244  *
245  **/
246 AtspiDeviceListener *
247 atspi_device_listener_new_simple (AtspiDeviceListenerSimpleCB callback,
248                            GDestroyNotify callback_destroyed)
249 {
250   return atspi_device_listener_new (device_remove_datum, callback, callback_destroyed);
251 }
252
253 /**
254  * atspi_device_listener_add_callback:
255  * @listener: the #AtspiDeviceListener instance to modify.
256  * @callback: (scope notified): an #AtspiDeviceListenerCB function pointer.
257  * @callback_destroyed: A #GDestroyNotify called when the listener is freed
258  * and data associated with the callback should be freed. It can be NULL.
259  * @user_data: (closure): a pointer to data which will be passed to the
260  *             callback when invoked. 
261  *
262  * Adds an in-process callback function to an existing #AtspiDeviceListener.
263  *
264  * Returns: #TRUE if successful, otherwise #FALSE.
265  *
266  **/
267 void
268 atspi_device_listener_add_callback (AtspiDeviceListener  *listener,
269                              AtspiDeviceListenerCB callback,
270                              GDestroyNotify callback_destroyed,
271                              void                      *user_data)
272 {
273   g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
274   DeviceEventHandler *new_handler;
275
276   new_handler = device_event_handler_new (callback,
277                                           callback_destroyed, user_data);
278
279   listener->callbacks = g_list_prepend (listener->callbacks, new_handler);
280 }
281
282 /**
283  * atspi_device_listener_remove_callback:
284  * @listener: the #AtspiDeviceListener instance to modify.
285  * @callback: (scope call): an #AtspiDeviceListenerCB function pointer.
286  *
287  * Removes an in-process callback function from an existing 
288  * #AtspiDeviceListener.
289  *
290  * Returns: #TRUE if successful, otherwise #FALSE.
291  *
292  **/
293 void
294 atspi_device_listener_remove_callback (AtspiDeviceListener  *listener,
295                                 AtspiDeviceListenerCB callback)
296 {
297   g_return_if_fail (ATSPI_IS_DEVICE_LISTENER (listener));
298
299   listener->callbacks = event_list_remove_by_cb (listener->callbacks, (void *) callback);
300 }
301
302 static void
303 read_device_event_from_iter (DBusMessageIter *iter, AtspiDeviceEvent *event)
304 {
305   dbus_uint32_t type;
306   dbus_int32_t id;
307   dbus_int32_t hw_code;
308   dbus_int32_t modifiers;
309   dbus_int32_t timestamp;
310   dbus_bool_t is_text;
311   DBusMessageIter iter_struct;
312
313   dbus_message_iter_recurse (iter, &iter_struct);
314
315   dbus_message_iter_get_basic (&iter_struct, &type);
316   event->type = type;
317   dbus_message_iter_next (&iter_struct);
318
319   dbus_message_iter_get_basic (&iter_struct, &id);
320   event->id = id;
321   dbus_message_iter_next (&iter_struct);
322
323   /* TODO: Remove cast from next two on ABI break */
324   dbus_message_iter_get_basic (&iter_struct, &hw_code);
325   event->hw_code = (gushort) hw_code;
326   dbus_message_iter_next (&iter_struct);
327
328   dbus_message_iter_get_basic (&iter_struct, &modifiers);
329   event->modifiers = (gushort) modifiers;
330   dbus_message_iter_next (&iter_struct);
331
332   dbus_message_iter_get_basic (&iter_struct, &timestamp);
333   event->timestamp = timestamp;
334   dbus_message_iter_next (&iter_struct);
335
336   dbus_message_iter_get_basic (&iter_struct, &event->event_string);
337   dbus_message_iter_next (&iter_struct);
338
339   dbus_message_iter_get_basic (&iter_struct, &is_text);
340   event->is_text = is_text;
341 }
342
343 DBusHandlerResult
344 _atspi_dbus_handle_DeviceEvent (DBusConnection *bus, DBusMessage *message, void *data)
345 {
346   const char *path = dbus_message_get_path (message);
347   int id;
348   AtspiDeviceEvent event;
349     AtspiDeviceListener *listener;
350   DBusMessageIter iter;
351   AtspiDeviceListenerClass *klass;
352   dbus_bool_t retval = FALSE;
353   GList *l;
354   DBusMessage *reply;
355
356   if (strcmp (dbus_message_get_signature (message), "(uiuuisb)") != 0)
357   {
358     g_warning ("Atspi: Unknown signature for an event");
359     goto done;
360   }
361
362   if (sscanf (path, "/org/a11y/atspi/listeners/%d", &id) != 1)
363   {
364     g_warning ("Atspi: Bad listener path: %s\n", path);
365     goto done;
366   }
367
368   for (l = device_listeners; l; l = g_list_next (l))
369   {
370     listener = l->data;
371     if (listener->id == id) break;
372   }
373
374   if (!l)
375   {
376     goto done;
377   }
378   dbus_message_iter_init (message, &iter);
379   read_device_event_from_iter (&iter, &event);
380   klass = ATSPI_DEVICE_LISTENER_GET_CLASS (listener);
381   if (klass->device_event)
382   {
383     retval = (*klass->device_event) (listener, &event);
384     if (retval != 0 && retval != 1)
385     {
386       g_warning ("at-spi: device event handler returned %d; should be 0 or 1", retval);
387       retval = 0;
388     }
389   }
390 done:
391   reply = dbus_message_new_method_return (message);
392   if (reply)
393   {
394     dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &retval, DBUS_TYPE_INVALID);
395     dbus_connection_send (_atspi_bus(), reply, NULL);
396     dbus_message_unref (reply);
397   }
398   return DBUS_HANDLER_RESULT_HANDLED;
399 }
400
401 gchar *
402 _atspi_device_listener_get_path (AtspiDeviceListener *listener)
403 {  return g_strdup_printf ("/org/a11y/atspi/listeners/%d", listener->id);
404 }
405
406 G_DEFINE_BOXED_TYPE (AtspiDeviceEvent,
407                      atspi_device_event,
408                      atspi_device_event_copy,
409                      atspi_device_event_free)