Support sending data with events
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / adaptors / value-adaptor.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 #include "bridge.h"
30
31 #include "spi-dbus.h"
32 #include "introspection.h"
33
34 static dbus_bool_t
35 impl_get_MinimumValue (DBusMessageIter * iter, void *user_data)
36 {
37   AtkValue *value = (AtkValue *) user_data;
38   GValue src = { 0 };
39   GValue dest = { 0 };
40   gdouble dub;
41
42   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
43
44   g_value_init (&src, G_TYPE_DOUBLE);
45   atk_value_get_minimum_value (value, &src);
46   g_value_init (&dest, G_TYPE_DOUBLE);
47
48   if (g_value_transform (&src, &dest))
49     {
50       dub = g_value_get_double (&dest);
51       return droute_return_v_double (iter, dub);
52     }
53   else
54     {
55       return FALSE;
56     }
57 }
58
59 static dbus_bool_t
60 impl_get_MaximumValue (DBusMessageIter * iter, void *user_data)
61 {
62   AtkValue *value = (AtkValue *) user_data;
63   GValue src = { 0 };
64   GValue dest = { 0 };
65   gdouble dub = 0;
66
67   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
68
69   g_value_init (&src, G_TYPE_DOUBLE);
70   atk_value_get_maximum_value (value, &src);
71   g_value_init (&dest, G_TYPE_DOUBLE);
72
73   if (g_value_transform (&src, &dest))
74     {
75       dub = g_value_get_double (&dest);
76     }
77   return droute_return_v_double (iter, dub);
78 }
79
80 static dbus_bool_t
81 impl_get_MinimumIncrement (DBusMessageIter * iter, void *user_data)
82 {
83   AtkValue *value = (AtkValue *) user_data;
84   GValue src = { 0 };
85   GValue dest = { 0 };
86   gdouble dub = 0;
87
88   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
89
90   g_value_init (&src, G_TYPE_DOUBLE);
91   atk_value_get_minimum_increment (value, &src);
92   g_value_init (&dest, G_TYPE_DOUBLE);
93
94   if (g_value_transform (&src, &dest))
95     {
96       dub = g_value_get_double (&dest);
97     }
98   return droute_return_v_double (iter, dub);
99 }
100
101 static dbus_bool_t
102 impl_get_CurrentValue (DBusMessageIter * iter, void *user_data)
103 {
104   AtkValue *value = (AtkValue *) user_data;
105   GValue src = { 0 };
106   GValue dest = { 0 };
107   gdouble dub = 0;
108
109   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
110
111   g_value_init (&src, G_TYPE_DOUBLE);
112   atk_value_get_current_value (value, &src);
113   g_value_init (&dest, G_TYPE_DOUBLE);
114
115   if (g_value_transform (&src, &dest))
116     {
117       dub = g_value_get_double (&dest);
118     }
119   return droute_return_v_double (iter, dub);
120 }
121
122 static dbus_bool_t
123 impl_set_CurrentValue (DBusMessageIter * iter, void *user_data)
124 {
125   AtkValue *value = (AtkValue *) user_data;
126   GValue src = { 0 };
127   GValue dest = { 0 };
128   gdouble dub;
129   DBusMessageIter iter_variant;
130
131   g_return_val_if_fail (ATK_IS_VALUE (user_data), FALSE);
132
133   dbus_message_iter_recurse (iter, &iter_variant);
134   if (dbus_message_iter_get_arg_type (&iter_variant) != DBUS_TYPE_DOUBLE)
135     {
136       g_warning ("TODO: Support setting value from a non-double");
137       return FALSE;
138     }
139   dbus_message_iter_get_basic (&iter_variant, &dub);
140   g_value_init (&src, G_TYPE_DOUBLE);
141   g_value_set_double (&src, dub);
142
143   atk_value_get_current_value (value, &dest);
144
145   if (g_value_transform (&src, &dest))
146     {
147       atk_value_set_current_value (value, &dest);
148       return TRUE;
149     }
150   else
151     {
152       return FALSE;
153     }
154 }
155
156 /* keeping this method around for backwards-compatibility for now; see
157  *  * BGO#652596 */
158 static DBusMessage *
159 impl_SetCurrentValue (DBusConnection * bus, DBusMessage * message,
160                        void *user_data)
161 {
162   AtkValue *value = (AtkValue *) user_data;
163   dbus_bool_t rv;
164   DBusMessage *reply;
165   gdouble dub = 0;
166   GValue new_value = { 0 };
167
168   g_return_val_if_fail (ATK_IS_VALUE (value),
169                         droute_not_yet_handled_error (message));
170
171   if (!dbus_message_get_args
172       (message, NULL, DBUS_TYPE_DOUBLE, &dub, DBUS_TYPE_INVALID))
173     {
174       return droute_invalid_arguments_error (message);
175     }
176
177   g_value_init (&new_value, G_TYPE_DOUBLE);
178   g_value_set_double (&new_value, dub);
179   rv = atk_value_set_current_value (value, &new_value);
180
181   reply = dbus_message_new_method_return (message);
182   if (reply)
183     {
184       dbus_message_append_args (reply, DBUS_TYPE_BOOLEAN, &rv,
185                                 DBUS_TYPE_INVALID);
186     }
187   return reply;
188 }
189
190 static DRouteMethod methods[] = {
191   {impl_SetCurrentValue, "SetCurrentValue"},
192   {NULL, NULL}
193 };
194
195 static DRouteProperty properties[] = {
196   {impl_get_MinimumValue, NULL, "MinimumValue"},
197   {impl_get_MaximumValue, NULL, "MaximumValue"},
198   {impl_get_MinimumIncrement, NULL, "MinimumIncrement"},
199   {impl_get_CurrentValue, impl_set_CurrentValue, "CurrentValue"},
200   {NULL, NULL, NULL}
201 };
202
203 void
204 spi_initialize_value (DRoutePath * path)
205 {
206   spi_atk_add_interface (path,
207                          ATSPI_DBUS_INTERFACE_VALUE, spi_org_a11y_atspi_Value, methods, properties);
208 };