Merge branch 'mdoff' of ssh://git.codethink.co.uk/git/atspi-dbus into mgorse
[platform/core/uifw/at-spi2-atk.git] / libspi / dbus.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008 Novell, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "accessible.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static GHashTable *path2ptr;
29 static guint objindex;
30
31 static void
32 deregister_object (gpointer data, GObject *obj)
33 {
34   spi_dbus_notify_remove(ATK_OBJECT(obj), NULL);
35   g_hash_table_remove (path2ptr, &obj);
36 }
37
38 static guint
39 register_object (AtkObject * obj)
40 {
41   gint *new_int;
42
43   if (!path2ptr)
44     {
45       path2ptr = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL);
46       if (!path2ptr)
47         return ++objindex;
48     }
49   objindex++;
50   while (g_hash_table_lookup (path2ptr, &objindex))
51     {
52       objindex++;
53       /* g_object_get_data returning 0 means no data, so handle wrap-around */
54       if (objindex == 0)
55         objindex++;
56     }
57   new_int = (gint *)g_malloc(sizeof(gint));
58   if (new_int)
59   {
60     *new_int = objindex;
61     g_hash_table_insert (path2ptr, new_int, obj);
62   }
63   g_object_set_data (G_OBJECT (obj), "dbus-id", (gpointer) objindex);
64   g_object_weak_ref(G_OBJECT(obj), deregister_object, NULL);
65   spi_dbus_notify_change(obj, TRUE, NULL);
66   return objindex;
67 }
68
69 AtkObject *
70 spi_dbus_get_object (const char *path)
71 {
72   guint index;
73   void *data;
74
75   g_assert (path);
76   if (strncmp(path, "/org/freedesktop/atspi/accessible", 33) != 0) return NULL;
77   path += 33;   /* skip over preamble */
78   if (path[0] == '\0') return atk_get_root();
79   if (path[0] != '/') return NULL;
80   path++;
81   index = atoi (path);
82   data = g_hash_table_lookup (path2ptr, &index);
83   if (data)
84     return ATK_OBJECT (data);
85   return NULL;
86 }
87
88 gchar *
89 spi_dbus_get_path (AtkObject * obj)
90 {
91   if (!obj) return NULL;
92   guint index = (guint) g_object_get_data (G_OBJECT (obj), "dbus-id");
93   if (!index)
94     index = register_object (obj);
95   return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", index);
96 }
97
98 DBusMessage *
99 spi_dbus_general_error (DBusMessage * message)
100 {
101   return dbus_message_new_error (message,
102                                  "org.freedesktop.atspi.GeneralError",
103                                  "General error");
104 }
105
106 /* Reply with the given object and dereference it if unref is TRUE */
107 DBusMessage *
108 spi_dbus_return_object (DBusMessage * message, AtkObject * obj, int unref)
109 {
110   DBusMessage *reply;
111   const char *path = spi_dbus_get_path (obj);
112   if (unref)
113     g_object_unref (obj);
114   if (!path)
115     {
116       /* Should we have a more specific error for this? */
117       return spi_dbus_general_error (message);
118     }
119   reply = dbus_message_new_method_return (message);
120   if (reply)
121     {
122       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
123                                 DBUS_TYPE_INVALID);
124     }
125   return reply;
126 }
127
128 DBusMessage *
129 spi_dbus_return_rect (DBusMessage * message, gint ix, gint iy, gint iwidth,
130                       gint iheight)
131 {
132   DBusMessage *reply;
133   dbus_uint32_t x, y, width, height;
134
135   x = ix;
136   y = iy;
137   width = iwidth;
138   height = iheight;
139   reply = dbus_message_new_method_return (message);
140   if (reply)
141     {
142       DBusMessageIter iter, sub;
143       dbus_message_iter_init_append (reply, &iter);
144       if (!dbus_message_iter_open_container
145           (&iter, DBUS_TYPE_STRUCT, NULL, &sub))
146         goto oom;
147       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &x);
148       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &y);
149       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &width);
150       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &height);
151       if (!dbus_message_iter_close_container (&iter, &sub))
152         goto oom;
153     }
154   return reply;
155 oom:
156   /* todo: return an error */
157   return reply;
158 }
159
160 dbus_bool_t
161 spi_dbus_return_v_object (DBusMessageIter * iter, AtkObject * obj, int unref)
162 {
163   const char *path = spi_dbus_get_path (obj);
164   if (unref)
165     g_object_unref (obj);
166   if (!path)
167     return FALSE;
168   return droute_return_v_object (iter, path);
169 }
170
171
172 void
173 spi_dbus_initialize (DRouteData * data)
174 {
175   spi_initialize_accessible (data);
176   spi_initialize_action(data);
177   spi_initialize_component (data);
178   spi_initialize_document (data);
179   spi_initialize_editabletext (data);
180   spi_initialize_hyperlink (data);
181   spi_initialize_hypertext (data);
182   spi_initialize_image (data);
183   spi_initialize_selection (data);
184   spi_initialize_table (data);
185   spi_initialize_text (data);
186   spi_initialize_value (data);
187   spi_initialize_introspectable(data);
188 }
189
190 void spi_dbus_emit_valist(DBusConnection *bus, const char *path, const char *interface, const char *name, int first_arg_type, va_list args)
191 {
192   DBusMessage *sig;
193
194   sig = dbus_message_new_signal(path, interface, name);
195   if (first_arg_type != DBUS_TYPE_INVALID)
196   {
197     dbus_message_append_args_valist(sig, first_arg_type, args);
198   }
199   dbus_connection_send(bus, sig, NULL);
200   dbus_message_unref(sig);
201 }
202
203 static GString *
204 spi_get_tree (AtkObject * obj, GString * str, DRouteData * data)
205 {
206   int role;
207   const char *name;
208   gchar *path;
209   GSList *l;
210   gint childcount;
211   gint i;
212
213   if (!obj)
214     return NULL;
215   role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));;
216   name = atk_object_get_name (obj);
217   if (!name)
218     name = "";
219   path = spi_dbus_get_path (obj);
220   g_string_append_printf (str,
221                           "<object path=\"%s\" name=\"%s\" role=\"%d\">\n",
222                           path, name, role);
223   for (l = data->interfaces; l; l = g_slist_next (l))
224     {
225       DRouteInterface *iface_def = (DRouteInterface *) l->data;
226       void *datum = NULL;
227       if (iface_def->get_datum)
228         datum = (*iface_def->get_datum) (path, data->user_data);
229       if (datum)
230         {
231           g_string_append_printf (str, "<interface name=\"%s\"/>\n",
232                                   iface_def->name);
233           if (iface_def->free_datum)
234             (*iface_def->free_datum) (datum);
235         }
236     }
237   childcount = atk_object_get_n_accessible_children (obj);
238   for (i = 0; i < childcount; i++)
239     {
240       AtkObject *child = atk_object_ref_accessible_child (obj, i);
241       str = spi_get_tree (child, str, data);
242       g_object_unref (child);
243     }
244   str = g_string_append (str, "</object>\n");
245   return str;
246 }
247
248 dbus_bool_t spi_dbus_message_iter_get_struct(DBusMessageIter *iter, ...)
249 {
250   va_list args;
251   DBusMessageIter iter_struct;
252   int type;
253   void *ptr;
254
255   dbus_message_iter_recurse(iter, &iter_struct);
256   va_start(args, iter);
257   for (;;)
258   {
259     type = va_arg(args, int);
260     if (type == DBUS_TYPE_INVALID) break;
261     if (type != dbus_message_iter_get_arg_type(&iter_struct))
262     {
263       va_end(args);
264       return FALSE;
265     }
266     ptr = va_arg(args, void *);
267     dbus_message_iter_get_basic(&iter_struct, ptr);
268     dbus_message_iter_next(&iter_struct);
269   }
270   dbus_message_iter_next(iter);
271   va_end(args);
272   return TRUE;
273 }
274
275 dbus_bool_t spi_dbus_message_iter_append_struct(DBusMessageIter *iter, ...)
276 {
277   va_list args;
278   DBusMessageIter iter_struct;
279   int type;
280   void *ptr;
281
282   if (!dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &iter_struct)) return FALSE;
283   va_start(args, iter);
284   for (;;)
285   {
286     type = va_arg(args, int);
287     if (type == DBUS_TYPE_INVALID) break;
288     ptr = va_arg(args, void *);
289     dbus_message_iter_append_basic(&iter_struct, type, ptr);
290   }
291   if (!dbus_message_iter_close_container(iter, &iter_struct)) return FALSE;
292   va_end(args);
293   return TRUE;
294 }
295
296 dbus_bool_t spi_dbus_marshall_deviceEvent(DBusMessage *message, const Accessibility_DeviceEvent *e)
297 {
298   DBusMessageIter iter;
299
300   if (!message) return FALSE;
301   dbus_message_iter_init_append(message, &iter);
302   return spi_dbus_message_iter_append_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_INT16, &e->hw_code, DBUS_TYPE_INT16, &e->modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID);
303 }
304
305 dbus_bool_t spi_dbus_demarshall_deviceEvent(DBusMessage *message, Accessibility_DeviceEvent *e)
306 {
307   DBusMessageIter iter;
308
309   dbus_message_iter_init(message, &iter);
310   return spi_dbus_message_iter_get_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_INT16, &e->hw_code, DBUS_TYPE_INT16, &e->modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID);
311 }