Initial commit.
[platform/core/uifw/at-spi2-atk.git] / libspi / image.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 AtkImage *
28 get_image (DBusMessage * message)
29 {
30   AtkObject *obj = spi_dbus_get_object (dbus_message_get_path (message));
31   if (!obj)
32     return NULL;
33   return ATK_IMAGE (obj);
34 }
35
36 static AtkImage *
37 get_image_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_IMAGE (obj);
43 }
44
45 static dbus_bool_t
46 impl_get_imageDescription (const char *path, DBusMessageIter * iter,
47                            void *user_data)
48 {
49   AtkImage *image = get_image_from_path (path, user_data);
50   if (!image)
51     return FALSE;
52   return droute_return_v_string (iter,
53                                  atk_image_get_image_description (image));
54 }
55
56 static char *
57 impl_get_imageDescription_str (void *datum)
58 {
59   AtkImage *image = (AtkImage *) datum;
60   g_assert (ATK_IS_IMAGE (datum));
61   return g_strdup (atk_image_get_image_description (image));
62 }
63
64 static dbus_bool_t
65 impl_get_imageLocale (const char *path, DBusMessageIter * iter,
66                       void *user_data)
67 {
68   AtkImage *image = get_image_from_path (path, user_data);
69   if (!image)
70     return FALSE;
71   return droute_return_v_string (iter, atk_image_get_image_locale (image));
72 }
73
74 static char *
75 impl_get_imageLocale_str (void *datum)
76 {
77   AtkImage *image = (AtkImage *) datum;
78   g_assert (ATK_IS_IMAGE (datum));
79   return g_strdup (atk_image_get_image_locale (image));
80 }
81
82 static DBusMessage *
83 impl_getImageExtents (DBusConnection * bus, DBusMessage * message,
84                       void *user_data)
85 {
86   AtkImage *image = get_image (message);
87   DBusError error;
88   dbus_uint32_t coordType;
89   gint ix, iy, iwidth, iheight;
90
91   if (!image)
92     return spi_dbus_general_error (message);
93   dbus_error_init (&error);
94   if (!dbus_message_get_args
95       (message, &error, DBUS_TYPE_UINT32, &coordType, DBUS_TYPE_INVALID))
96     {
97       return SPI_DBUS_RETURN_ERROR (message, &error);
98     }
99   atk_image_get_image_size (image, &iwidth, &iheight);
100   atk_image_get_image_position (image, &ix, &iy, (AtkCoordType) coordType);
101   return spi_dbus_return_rect (message, ix, iy, iwidth, iheight);
102 }
103
104 static DBusMessage *
105 impl_getImagePosition (DBusConnection * bus, DBusMessage * message,
106                        void *user_data)
107 {
108   AtkImage *image = get_image (message);
109   DBusError error;
110   dbus_uint32_t coord_type;
111   gint ix = 0, iy = 0;
112   dbus_int32_t x, y;
113   DBusMessage *reply;
114
115   if (!image)
116     return spi_dbus_general_error (message);
117   dbus_error_init (&error);
118   if (!dbus_message_get_args
119       (message, &error, DBUS_TYPE_UINT32, &coord_type, DBUS_TYPE_INVALID))
120     {
121       return SPI_DBUS_RETURN_ERROR (message, &error);
122     }
123   atk_image_get_image_position (image, &ix, &iy, (AtkCoordType) coord_type);
124   x = ix;
125   y = iy;
126   reply = dbus_message_new_method_return (message);
127   if (reply)
128     {
129       dbus_message_append_args (reply, DBUS_TYPE_INT32, &x, DBUS_TYPE_INT32,
130                                 &y, DBUS_TYPE_INVALID);
131     }
132   return reply;
133 }
134
135 static DBusMessage *
136 impl_getImageSize (DBusConnection * bus, DBusMessage * message,
137                    void *user_data)
138 {
139   AtkImage *image = get_image (message);
140   gint iwidth = 0, iheight = 0;
141   dbus_int32_t width, height;
142   DBusMessage *reply;
143
144   if (!image)
145     return spi_dbus_general_error (message);
146   atk_image_get_image_size (image, &iwidth, &iheight);
147   width = iwidth;
148   height = iheight;
149   reply = dbus_message_new_method_return (message);
150   if (reply)
151     {
152       dbus_message_append_args (reply, DBUS_TYPE_INT32, &width,
153                                 DBUS_TYPE_INT32, &height, DBUS_TYPE_INVALID);
154     }
155   return reply;
156 }
157
158 static DRouteMethod methods[] = {
159   {DROUTE_METHOD, impl_getImageExtents, "getImageExtents",
160    "n,coordType,i:(uuuu),,o"},
161   {DROUTE_METHOD, impl_getImagePosition, "getImagePosition",
162    "i,x,o:i,y,o:n,coordType,i"},
163   {DROUTE_METHOD, impl_getImageSize, "getImageSize", "i,width,o:i,height,o"},
164   {0, NULL, NULL, NULL}
165 };
166
167 static DRouteProperty properties[] = {
168   {impl_get_imageDescription, impl_get_imageDescription_str, NULL, NULL,
169    "imageDescription", "s"},
170   {impl_get_imageLocale, impl_get_imageLocale_str, NULL, NULL, "imageLocale",
171    "s"},
172   {NULL, NULL, NULL, NULL, NULL, NULL}
173 };
174
175 void
176 spi_initialize_image (DRouteData * data)
177 {
178   droute_add_interface (data, "org.freedesktop.accessibility.Image", methods,
179                         properties,
180                         (DRouteGetDatumFunction) get_image_from_path, NULL);
181 };