2008-12-17 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / value.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 <math.h>
26
27 #include <atk/atk.h>
28 #include <droute/droute.h>
29
30 #include "spi-common/spi-dbus.h"
31
32 static double
33 get_double_from_gvalue (GValue * gvalue)
34 {
35   double retval = 0;
36   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
37     {
38       switch (gvalue->g_type)
39         {
40         case G_TYPE_DOUBLE:
41           retval = g_value_get_double (gvalue);
42           break;
43         case G_TYPE_FLOAT:
44           retval = g_value_get_float (gvalue);
45           break;
46         case G_TYPE_ULONG:
47           retval = g_value_get_ulong (gvalue);
48           break;
49         case G_TYPE_LONG:
50           retval = g_value_get_long (gvalue);
51           break;
52         case G_TYPE_UINT:
53           retval = g_value_get_uint (gvalue);
54           break;
55         case G_TYPE_INT:
56           retval = g_value_get_int (gvalue);
57           break;
58         case G_TYPE_UCHAR:
59           retval = g_value_get_uchar (gvalue);
60           break;
61         case G_TYPE_CHAR:
62           retval = g_value_get_char (gvalue);
63           break;
64         case G_TYPE_BOOLEAN:
65           retval = g_value_get_boolean (gvalue);
66           break;
67         }
68     }
69   else
70     {
71       g_warning ("SpiValue requested from a non-fundamental type\n");
72     }
73   return retval;
74 }
75
76 static gboolean
77 get_double_from_variant (DBusMessageIter * iter, double *out)
78 {
79   DBusMessageIter sub;
80
81   dbus_message_iter_recurse (iter, &sub);
82   switch (dbus_message_iter_get_arg_type (&sub))
83     {
84     case DBUS_TYPE_DOUBLE:
85       {
86         dbus_message_iter_get_basic (&sub, out);
87         return TRUE;
88       }
89     case DBUS_TYPE_UINT32:
90       {
91         dbus_uint32_t v;
92         dbus_message_iter_get_basic (&sub, &v);
93         *out = (double) v;
94         return TRUE;
95       }
96     case DBUS_TYPE_INT32:
97       {
98         dbus_int32_t v;
99         dbus_message_iter_get_basic (&sub, &v);
100         *out = (double) v;
101         return TRUE;
102       }
103     case DBUS_TYPE_UINT16:
104       {
105         dbus_uint16_t v;
106         dbus_message_iter_get_basic (&sub, &v);
107         *out = (double) v;
108         return TRUE;
109       }
110     case DBUS_TYPE_INT16:
111       {
112         dbus_int16_t v;
113         dbus_message_iter_get_basic (&sub, &v);
114         *out = (double) v;
115         return TRUE;
116       }
117     case DBUS_TYPE_BYTE:
118       {
119         char v;
120         dbus_message_iter_get_basic (&sub, &v);
121         *out = (double) v;
122         return TRUE;
123       }
124     case DBUS_TYPE_BOOLEAN:
125       {
126         dbus_bool_t v;
127         dbus_message_iter_get_basic (&sub, &v);
128         *out = (double) v;
129         return TRUE;
130       }
131     default:
132       return FALSE;
133     }
134 }
135
136 static void
137 gvalue_set_from_double (GValue * gvalue, double value)
138 {
139   if (G_TYPE_IS_FUNDAMENTAL (G_VALUE_TYPE (gvalue)))
140     {
141       switch (gvalue->g_type)
142         {
143         case G_TYPE_DOUBLE:
144           g_value_set_double (gvalue, value);
145           break;
146         case G_TYPE_FLOAT:
147           g_value_set_float (gvalue, value);
148           break;
149         case G_TYPE_ULONG:
150           g_value_set_ulong (gvalue, value);
151           break;
152         case G_TYPE_LONG:
153           g_value_set_long (gvalue, value);
154           break;
155         case G_TYPE_UINT:
156           g_value_set_uint (gvalue, value);
157           break;
158         case G_TYPE_INT:
159           g_value_set_int (gvalue, value);
160           break;
161         case G_TYPE_UCHAR:
162           g_value_set_uchar (gvalue, value);
163           break;
164         case G_TYPE_CHAR:
165           g_value_set_char (gvalue, value);
166           break;
167         case G_TYPE_BOOLEAN:
168           g_value_set_boolean (gvalue, ((fabs (value) > 0.5) ? 1 : 0));
169           break;
170         }
171     }
172   else
173     {
174       g_warning ("SpiValue change requested for a non-fundamental type\n");
175     }
176 }
177
178 static dbus_bool_t
179 impl_get_minimumValue (DBusMessageIter * iter,
180                        void *user_data)
181 {
182   AtkValue *value = (AtkValue *) user_data;
183   GValue gvalue = { 0, };
184   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
185   atk_value_get_minimum_value (value, &gvalue);
186   return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
187 }
188
189 static dbus_bool_t
190 impl_get_maximumValue (DBusMessageIter * iter,
191                        void *user_data)
192 {
193   AtkValue *value = (AtkValue *) user_data;
194   GValue gvalue = { 0, };
195   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
196   atk_value_get_maximum_value (value, &gvalue);
197   return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
198 }
199
200 static dbus_bool_t
201 impl_get_minimumIncrement (DBusMessageIter * iter,
202                            void *user_data)
203 {
204   AtkValue *value = (AtkValue *) user_data;
205   GValue gvalue = { 0, };
206   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
207   atk_value_get_minimum_value (value, &gvalue);
208   return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
209 }
210
211 static dbus_bool_t
212 impl_get_currentValue (DBusMessageIter * iter,
213                        void *user_data)
214 {
215   AtkValue *value = (AtkValue *) user_data;
216   GValue gvalue = { 0, };
217   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
218   atk_value_get_current_value (value, &gvalue);
219   return droute_return_v_double (iter, get_double_from_gvalue (&gvalue));
220 }
221
222 static dbus_bool_t
223 impl_set_currentValue (DBusMessageIter * iter,
224                        void *user_data)
225 {
226   AtkValue *value = (AtkValue *) user_data;
227   GValue gvalue = { 0, };
228   double dbl;
229
230   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
231   if (!get_double_from_variant (iter, &dbl))
232     return FALSE;
233   atk_value_get_current_value (value, &gvalue);
234   gvalue_set_from_double (&gvalue, dbl);
235   return TRUE;
236 }
237
238 static DRouteProperty properties[] = {
239   {impl_get_minimumValue, NULL, "minimumValue"},
240   {impl_get_maximumValue, NULL, "maximumValue"},
241   {impl_get_minimumIncrement, NULL, "minimumIncrement"},
242   {impl_get_currentValue, impl_set_currentValue, "currentValue"},
243   {NULL, NULL, NULL}
244 };
245
246 void
247 spi_initialize_value (DRoutePath *path)
248 {
249   droute_path_add_interface (path,
250                              SPI_DBUS_INTERFACE_VALUE,
251                              NULL,
252                              properties);
253 };