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