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