Initial commit.
[platform/core/uifw/at-spi2-atk.git] / libspi / component.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  * Copyright 2001, 2002 Sun Microsystems Inc.,
7  * Copyright 2001, 2002 Ximian, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include "accessible.h"
26
27 static AtkComponent *
28 get_component (DBusMessage * message)
29 {
30   AtkObject *obj = spi_dbus_get_object (dbus_message_get_path (message));
31   if (!obj)
32     return NULL;
33   return ATK_COMPONENT (obj);
34 }
35
36 static AtkComponent *
37 get_component_from_path (const char *path, void *user_data)
38 {
39   AtkObject *obj = spi_dbus_get_object (path);
40   if (!obj)
41     return NULL;
42   return ATK_COMPONENT (obj);
43 }
44
45 static DBusMessage *
46 impl_contains (DBusConnection * bus, DBusMessage * message, void *user_data)
47 {
48   AtkComponent *component = get_component (message);
49   dbus_int32_t x, y;
50   dbus_uint32_t coord_type;
51   DBusError error;
52   dbus_bool_t retval;
53   DBusMessage *reply;
54
55   if (!component)
56     return spi_dbus_general_error (message);
57   dbus_error_init (&error);
58   if (!dbus_message_get_args
59       (message, &error, DBUS_TYPE_INT32, &x, DBUS_TYPE_UINT32, &y,
60        DBUS_TYPE_INT32, &coord_type, DBUS_TYPE_INVALID))
61     {
62       return SPI_DBUS_RETURN_ERROR (message, &error);
63     }
64   retval =
65     atk_component_contains (component, x, y, (AtkCoordType) coord_type);
66   reply = dbus_message_new_method_return (message);
67   if (reply)
68     {
69       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &retval,
70                                 DBUS_TYPE_INVALID);
71     }
72   return reply;
73 }
74
75 static DBusMessage *
76 impl_getAccessibleAtPoint (DBusConnection * bus, DBusMessage * message,
77                            void *user_data)
78 {
79   AtkComponent *component = get_component (message);
80   dbus_int32_t x, y;
81   dbus_uint32_t coord_type;
82   DBusError error;
83   AtkObject *child;
84
85   if (!component)
86     return spi_dbus_general_error (message);
87   dbus_error_init (&error);
88   if (!dbus_message_get_args
89       (message, &error, DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32, &y,
90        DBUS_TYPE_UINT32, &coord_type, DBUS_TYPE_INVALID))
91     {
92       return SPI_DBUS_RETURN_ERROR (message, &error);
93     }
94   child =
95     atk_component_ref_accessible_at_point (component, x, y,
96                                            (AtkCoordType) coord_type);
97   return spi_dbus_return_object (message, child, TRUE);
98 }
99
100 static DBusMessage *
101 impl_getExtents (DBusConnection * bus, DBusMessage * message, void *user_data)
102 {
103   AtkComponent *component = get_component (message);
104   DBusError error;
105   dbus_uint32_t coord_type;
106   gint ix, iy, iwidth, iheight;
107
108   if (!component)
109     return spi_dbus_general_error (message);
110   dbus_error_init (&error);
111   if (!dbus_message_get_args
112       (message, &error, DBUS_TYPE_UINT32, &coord_type, DBUS_TYPE_INVALID))
113     {
114       return SPI_DBUS_RETURN_ERROR (message, &error);
115     }
116   atk_component_get_extents (component, &ix, &iy, &iwidth, &iheight,
117                              (AtkCoordType) coord_type);
118   return spi_dbus_return_rect (message, ix, iy, iwidth, iheight);
119 }
120
121 static DBusMessage *
122 impl_getPosition (DBusConnection * bus, DBusMessage * message,
123                   void *user_data)
124 {
125   AtkComponent *component = get_component (message);
126   DBusError error;
127   dbus_uint32_t coord_type;
128   gint ix = 0, iy = 0;
129   dbus_int32_t x, y;
130   DBusMessage *reply;
131
132   if (!component)
133     return spi_dbus_general_error (message);
134   dbus_error_init (&error);
135   if (!dbus_message_get_args
136       (message, &error, DBUS_TYPE_UINT32, &coord_type, DBUS_TYPE_INVALID))
137     {
138       return SPI_DBUS_RETURN_ERROR (message, &error);
139     }
140   atk_component_get_position (component, &ix, &iy, (AtkCoordType) coord_type);
141   x = ix;
142   y = iy;
143   reply = dbus_message_new_method_return (message);
144   if (reply)
145     {
146       dbus_message_append_args (reply, DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32,
147                                 &y, DBUS_TYPE_INVALID);
148     }
149   return reply;
150 }
151
152 static DBusMessage *
153 impl_getSize (DBusConnection * bus, DBusMessage * message, void *user_data)
154 {
155   AtkComponent *component = get_component (message);
156   gint iwidth = 0, iheight = 0;
157   dbus_int32_t width, height;
158   DBusMessage *reply;
159
160   if (!component)
161     return spi_dbus_general_error (message);
162   atk_component_get_size (component, &iwidth, &iheight);
163   width = iwidth;
164   height = iheight;
165   reply = dbus_message_new_method_return (message);
166   if (reply)
167     {
168       dbus_message_append_args (reply, DBUS_TYPE_INT32, &width,
169                                 DBUS_TYPE_INT32, &height, DBUS_TYPE_INVALID);
170     }
171   return reply;
172 }
173
174 static DBusMessage *
175 impl_getLayer (DBusConnection * bus, DBusMessage * message, void *user_data)
176 {
177   AtkComponent *component = get_component (message);
178   AtkLayer atklayer;
179   dbus_uint32_t rv;
180   DBusMessage *reply;
181
182   if (!component)
183     return spi_dbus_general_error (message);
184   atklayer = atk_component_get_layer (component);
185
186   switch (atklayer)
187     {
188     case ATK_LAYER_BACKGROUND:
189       rv = Accessibility_LAYER_BACKGROUND;
190       break;
191     case ATK_LAYER_CANVAS:
192       rv = Accessibility_LAYER_CANVAS;
193       break;
194     case ATK_LAYER_WIDGET:
195       rv = Accessibility_LAYER_WIDGET;
196       break;
197     case ATK_LAYER_MDI:
198       rv = Accessibility_LAYER_MDI;
199       break;
200     case ATK_LAYER_POPUP:
201       rv = Accessibility_LAYER_POPUP;
202       break;
203     case ATK_LAYER_OVERLAY:
204       rv = Accessibility_LAYER_OVERLAY;
205       break;
206     case ATK_LAYER_WINDOW:
207       rv = Accessibility_LAYER_WINDOW;
208       break;
209     default:
210       rv = Accessibility_LAYER_INVALID;
211       break;
212     }
213   reply = dbus_message_new_method_return (message);
214   if (reply)
215     {
216       dbus_message_append_args (reply, DBUS_TYPE_UINT32, &rv,
217                                 DBUS_TYPE_INVALID);
218     }
219   return reply;
220 }
221
222 static DBusMessage *
223 impl_getMDIZOrder (DBusConnection * bus, DBusMessage * message,
224                    void *user_data)
225 {
226   AtkComponent *component = get_component (message);
227   dbus_int16_t rv;
228   DBusMessage *reply;
229
230   if (!component)
231     return spi_dbus_general_error (message);
232   rv = atk_component_get_mdi_zorder (component);
233   reply = dbus_message_new_method_return (message);
234   if (reply)
235     {
236       dbus_message_append_args (reply, DBUS_TYPE_UINT32, &rv,
237                                 DBUS_TYPE_INVALID);
238     }
239   return reply;
240 }
241
242 static DBusMessage *
243 impl_grabFocus (DBusConnection * bus, DBusMessage * message, void *user_data)
244 {
245   AtkComponent *component = get_component (message);
246   dbus_bool_t rv;
247   DBusMessage *reply;
248
249   if (!component)
250     return spi_dbus_general_error (message);
251   rv = atk_component_grab_focus (component);
252   reply = dbus_message_new_method_return (message);
253   if (reply)
254     {
255       dbus_message_append_args (reply, DBUS_TYPE_UINT32, &rv,
256                                 DBUS_TYPE_INVALID);
257     }
258   return reply;
259 }
260
261 #if 0
262 static DBusMessage *
263 impl_registerFocusHandler (DBusConnection * bus, DBusMessage * message,
264                            void *user_data)
265 {
266 }
267
268 static DBusMessage *
269 impl_deregisterFocusHandler (DBusConnection * bus, DBusMessage * message,
270                              void *user_data)
271 {
272 }
273 #endif
274
275 static DBusMessage *
276 impl_getAlpha (DBusConnection * bus, DBusMessage * message, void *user_data)
277 {
278   AtkComponent *component = get_component (message);
279   double rv;
280   DBusMessage *reply;
281
282   if (!component)
283     return spi_dbus_general_error (message);
284   rv = atk_component_get_alpha (component);
285   reply = dbus_message_new_method_return (message);
286   if (reply)
287     {
288       dbus_message_append_args (reply, DBUS_TYPE_DOUBLE, &rv,
289                                 DBUS_TYPE_INVALID);
290     }
291   return reply;
292 }
293
294 static DRouteMethod methods[] = {
295   {DROUTE_METHOD, impl_contains, "contains",
296    "i,x,i:i,y,i:u,coord_type,i:b,,o"},
297   {DROUTE_METHOD, impl_getAccessibleAtPoint, "getAccessibleAtPoint",
298    "i,x,i:i,y,i:u,coord_type,i:o,,o"},
299   {DROUTE_METHOD, impl_getExtents, "getExtents", "u,coord_type,i:(uuuu),,o"},
300   {DROUTE_METHOD, impl_getPosition, "getPosition",
301    "i,x,o:i,y,o:u,coord_type,i"},
302   {DROUTE_METHOD, impl_getSize, "getSize", "i,width,o:i,height,o"},
303   {DROUTE_METHOD, impl_getLayer, "getLayer", "g,,o"},
304   {DROUTE_METHOD, impl_getMDIZOrder, "getMDIZOrder", "n,,o"},
305   {DROUTE_METHOD, impl_grabFocus, "grabFocus", "b,,o"},
306   //{ DROUTE_METHOD, impl_registerFocusHandler, "registerFocusHandler", "o,handler,i" },
307   //{ DROUTE_METHOD, impl_deregisterFocusHandler, "deregisterFocusHandler", "o,handler,i" },
308   {DROUTE_METHOD, impl_getAlpha, "getAlpha", "d,,o"},
309   {0, NULL, NULL, NULL}
310 };
311
312 void
313 spi_initialize_component (DRouteData * data)
314 {
315   droute_add_interface (data, "org.freedesktop.accessibility.Component",
316                         methods, NULL,
317                         (DRouteGetDatumFunction) get_component_from_path,
318                         NULL);
319 };