Add droute.c, droute.h, dbus.c, dbus.h, bitarray.h
[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 "dbus.h"
27
28 static GHashTable *path2ptr;
29 static guint objindex;
30
31 static void
32 deregister_object (gpointer obj)
33 {
34   g_hash_table_remove (path2ptr, obj);
35 }
36
37 static guint
38 register_object (AtkObject * obj)
39 {
40   if (!path2ptr)
41     {
42       path2ptr = g_hash_table_new (g_int_hash, g_int_equal);
43       if (!path2ptr)
44         return ++objindex;
45     }
46   while (g_hash_table_lookup (path2ptr, (gpointer)++ objindex))
47     {
48       /* g_object_get_data returning 0 means no data, so handle wrap-around */
49       if (objindex == 0)
50         objindex++;
51     }
52   g_hash_table_insert (path2ptr, (gpointer) objindex, obj);
53   g_object_set_data_full (G_OBJECT (obj), "dbus-id", (gpointer) objindex,
54                           deregister_object);
55   return objindex;
56 }
57
58 AtkObject *
59 spi_dbus_get_object (const char *path)
60 {
61   guint index;
62   void *data;
63
64   g_assert (path);
65   index = atoi (path);
66   data = g_hash_table_lookup (path2ptr, (gpointer) index);
67   if (data)
68     return ATK_OBJECT (data);
69   return NULL;
70 }
71
72 char *
73 spi_dbus_get_path (AtkObject * obj)
74 {
75   guint index = (guint) g_object_get_data (G_OBJECT (obj), "dbus-id");
76   if (!index)
77     index = register_object (obj);
78   return g_strdup_printf ("/org/freedesktop/atspi/accessible/%d", index);
79 }
80
81 DBusMessage *
82 spi_dbus_general_error (DBusMessage * message)
83 {
84   return dbus_message_new_error (message,
85                                  "org.freedesktop.accessibility.GeneralError",
86                                  "General error");
87 }
88
89 /* Reply with the given object and dereference it if unref is TRUE */
90 DBusMessage *
91 spi_dbus_return_object (DBusMessage * message, AtkObject * obj, int unref)
92 {
93   DBusMessage *reply;
94   const char *path = spi_dbus_get_path (obj);
95   if (unref)
96     g_object_unref (obj);
97   if (!path)
98     {
99       /* Should we have a more specific error for this? */
100       return spi_dbus_general_error (message);
101     }
102   reply = dbus_message_new_method_return (message);
103   if (reply)
104     {
105       dbus_message_append_args (reply, DBUS_TYPE_OBJECT_PATH, path,
106                                 DBUS_TYPE_INVALID);
107     }
108   return reply;
109 }
110
111 DBusMessage *
112 spi_dbus_return_rect (DBusMessage * message, gint ix, gint iy, gint iwidth,
113                       gint iheight)
114 {
115   DBusMessage *reply;
116   dbus_uint32_t x, y, width, height;
117
118   x = ix;
119   y = iy;
120   width = iwidth;
121   height = iheight;
122   reply = dbus_message_new_method_return (message);
123   if (reply)
124     {
125       DBusMessageIter iter, sub;
126       dbus_message_iter_init_append (reply, &iter);
127       if (!dbus_message_iter_open_container
128           (&iter, DBUS_TYPE_STRUCT, NULL, &sub))
129         goto oom;
130       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &x);
131       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &y);
132       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &width);
133       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &height);
134       if (!dbus_message_iter_close_container (&iter, &sub))
135         goto oom;
136     }
137   return reply;
138 oom:
139   /* todo: return an error */
140   return reply;
141 }
142
143 dbus_bool_t
144 spi_dbus_return_v_object (DBusMessageIter * iter, AtkObject * obj, int unref)
145 {
146   const char *path = spi_dbus_get_path (obj);
147   if (unref)
148     g_object_unref (obj);
149   if (!path)
150     return FALSE;
151   return droute_return_v_object (iter, path);
152 }
153
154
155 void
156 spi_dbus_initialize (DRouteData * data)
157 {
158   spi_initialize_accessible (data);
159   spi_initialize_action(data);
160   spi_initialize_component (data);
161   spi_initialize_document (data);
162   spi_initialize_editabletext (data);
163   spi_initialize_hyperlink (data);
164   spi_initialize_hypertext (data);
165   spi_initialize_image (data);
166   spi_initialize_selection (data);
167   spi_initialize_table (data);
168   spi_initialize_text (data);
169   spi_initialize_value (data);
170 }
171
172 static GString *
173 spi_get_tree (AtkObject * obj, GString * str, DRouteData * data)
174 {
175   int role;
176   const char *name;
177   gchar *path;
178   GSList *l;
179   gint childcount;
180   gint i;
181
182   if (!obj)
183     return NULL;
184   role = spi_accessible_role_from_atk_role (atk_object_get_role (obj));;
185   name = atk_object_get_name (obj);
186   if (!name)
187     name = "";
188   path = spi_dbus_get_path (obj);
189   g_string_append_printf (str,
190                           "<object path=\"%s\" name=\"%s\" role=\"%d\">\n",
191                           path, name, role);
192   for (l = data->interfaces; l; l = g_slist_next (l))
193     {
194       DRouteInterface *iface_def = (DRouteInterface *) l->data;
195       void *datum = NULL;
196       if (iface_def->get_datum)
197         datum = (*iface_def->get_datum) (path, data->user_data);
198       if (datum)
199         {
200           g_string_append_printf (str, "<interface name=\"%s\"/>\n",
201                                   iface_def->name);
202           if (iface_def->free_datum)
203             (*iface_def->free_datum) (datum);
204         }
205     }
206   childcount = atk_object_get_n_accessible_children (obj);
207   for (i = 0; i < childcount; i++)
208     {
209       AtkObject *child = atk_object_ref_accessible_child (obj, i);
210       str = spi_get_tree (child, str, data);
211       g_object_unref (child);
212     }
213   str = g_string_append (str, "</object>\n");
214   return str;
215 }