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