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 Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 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 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <glib-object.h>
28 #include <dbus/dbus.h>
29
30 #include <atspi/atspi.h>
31
32 DBusMessage *
spi_dbus_general_error(DBusMessage * message)33 spi_dbus_general_error (DBusMessage * message)
34 {
35 return dbus_message_new_error (message,
36 "org.a11y.atspi.GeneralError",
37 "General error");
38 }
39
40 DBusMessage *
spi_dbus_return_rect(DBusMessage * message,gint ix,gint iy,gint iwidth,gint iheight)41 spi_dbus_return_rect (DBusMessage * message, gint ix, gint iy, gint iwidth,
42 gint iheight)
43 {
44 DBusMessage *reply;
45 dbus_uint32_t x, y, width, height;
46
47 x = ix;
48 y = iy;
49 width = iwidth;
50 height = iheight;
51 reply = dbus_message_new_method_return (message);
52 if (reply)
53 {
54 DBusMessageIter iter, sub;
55 dbus_message_iter_init_append (reply, &iter);
56 if (!dbus_message_iter_open_container
57 (&iter, DBUS_TYPE_STRUCT, NULL, &sub))
58 goto oom;
59 dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &x);
60 dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &y);
61 dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &width);
62 dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &height);
63 if (!dbus_message_iter_close_container (&iter, &sub))
64 goto oom;
65 }
66 return reply;
67 oom:
68 /* todo: return an error */
69 return reply;
70 }
71
spi_dbus_emit_valist(DBusConnection * bus,const char * path,const char * interface,const char * name,int first_arg_type,va_list args)72 void spi_dbus_emit_valist(DBusConnection *bus, const char *path, const char *interface, const char *name, int first_arg_type, va_list args)
73 {
74 DBusMessage *sig;
75
76 sig = dbus_message_new_signal(path, interface, name);
77 if (first_arg_type != DBUS_TYPE_INVALID)
78 {
79 dbus_message_append_args_valist(sig, first_arg_type, args);
80 }
81 dbus_connection_send(bus, sig, NULL);
82 dbus_message_unref(sig);
83 }
84
spi_dbus_message_iter_get_struct(DBusMessageIter * iter,...)85 dbus_bool_t spi_dbus_message_iter_get_struct(DBusMessageIter *iter, ...)
86 {
87 va_list args;
88 DBusMessageIter iter_struct;
89 int type;
90 void *ptr;
91
92 dbus_message_iter_recurse(iter, &iter_struct);
93 va_start(args, iter);
94 for (;;)
95 {
96 type = va_arg(args, int);
97 if (type == DBUS_TYPE_INVALID) break;
98 if (type != dbus_message_iter_get_arg_type(&iter_struct))
99 {
100 va_end(args);
101 return FALSE;
102 }
103 ptr = va_arg(args, void *);
104 dbus_message_iter_get_basic(&iter_struct, ptr);
105 dbus_message_iter_next(&iter_struct);
106 }
107 dbus_message_iter_next(iter);
108 va_end(args);
109 return TRUE;
110 }
111
spi_dbus_message_iter_append_struct(DBusMessageIter * iter,...)112 dbus_bool_t spi_dbus_message_iter_append_struct(DBusMessageIter *iter, ...)
113 {
114 va_list args;
115 DBusMessageIter iter_struct;
116 int type;
117 void *ptr;
118
119 if (!dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &iter_struct)) return FALSE;
120 va_start(args, iter);
121 for (;;)
122 {
123 type = va_arg(args, int);
124 if (type == DBUS_TYPE_INVALID) break;
125 ptr = va_arg(args, void *);
126 dbus_message_iter_append_basic(&iter_struct, type, ptr);
127 }
128 va_end(args);
129 if (!dbus_message_iter_close_container(iter, &iter_struct)) return FALSE;
130 return TRUE;
131 }
132
spi_dbus_marshal_deviceEvent(DBusMessage * message,const AtspiDeviceEvent * e)133 dbus_bool_t spi_dbus_marshal_deviceEvent(DBusMessage *message, const AtspiDeviceEvent *e)
134 {
135 DBusMessageIter iter;
136
137 if (!message) return FALSE;
138 dbus_message_iter_init_append(message, &iter);
139 return spi_dbus_message_iter_append_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_INT16, &e->hw_code, DBUS_TYPE_INT16, &e->modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID);
140 }
141
spi_dbus_demarshal_deviceEvent(DBusMessage * message,AtspiDeviceEvent * e)142 dbus_bool_t spi_dbus_demarshal_deviceEvent(DBusMessage *message, AtspiDeviceEvent *e)
143 {
144 DBusMessageIter iter;
145
146 dbus_message_iter_init(message, &iter);
147 return spi_dbus_message_iter_get_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_INT16, &e->hw_code, DBUS_TYPE_INT16, &e->modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID);
148 }
149
150 /*
151 * This is a rather annoying function needed to replace
152 * NULL values of strings with the empty string. Null string
153 * values can be created by the atk_object_get_name or text selection
154 */
155 static const void *
provide_defaults(const gint type,const void * val)156 provide_defaults(const gint type,
157 const void *val)
158 {
159 switch (type)
160 {
161 case DBUS_TYPE_STRING:
162 case DBUS_TYPE_OBJECT_PATH:
163 if (!val)
164 return "";
165 else
166 return val;
167 default:
168 return val;
169 }
170 }
171
172 /*
173 * Appends all the standard parameters to an AT-SPI event.
174 */
175 void
spi_dbus_signal_new(const char * path,const char * klass,const char * major,const char * minor,dbus_int32_t detail1,dbus_int32_t detail2)176 spi_dbus_signal_new (const char *path,
177 const char *klass,
178 const char *major,
179 const char *minor,
180 dbus_int32_t detail1,
181 dbus_int32_t detail2)
182 {
183 DBusMessage *sig;
184 DBusMessageIter iter;
185 gchar *cname, *t;
186
187 if (!klass) klass = "";
188 if (!major) major = "";
189 if (!minor) minor = "";
190
191 /*
192 * This is very annoying, but as '-' isn't a legal signal
193 * name in D-Bus (Why not??!?) The names need converting
194 * on this side, and again on the client side.
195 */
196 cname = g_strdup(major);
197 while ((t = strchr(cname, '-')) != NULL) *t = '_';
198
199 sig = dbus_message_new_signal(path, klass, cname);
200 g_free(cname);
201
202 dbus_message_iter_init_append(sig, &iter);
203
204 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
205 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail1);
206 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail2);
207 }
208
209 void
spi_dbus_emit_signal(DBusConnection * bus,const char * path,const char * klass,const char * major,const char * minor,dbus_int32_t detail1,dbus_int32_t detail2,const char * type,const void * val)210 spi_dbus_emit_signal(DBusConnection *bus, const char *path,
211 const char *klass,
212 const char *major,
213 const char *minor,
214 dbus_int32_t detail1,
215 dbus_int32_t detail2,
216 const char *type,
217 const void *val)
218 {
219 gchar *cname, *t;
220 DBusMessage *sig;
221 DBusMessageIter iter, sub;
222 if (!klass) klass = "";
223 if (!major) major = "";
224 if (!minor) minor = "";
225 if (!type) type = "u";
226
227 /*
228 * This is very annoying, but as '-' isn't a legal signal
229 * name in D-Bus (Why not??!?) The names need converting
230 * on this side, and again on the client side.
231 */
232 cname = g_strdup(major);
233 while ((t = strchr(cname, '-')) != NULL) *t = '_';
234
235 sig = dbus_message_new_signal(path, klass, cname);
236 g_free(cname);
237
238 dbus_message_iter_init_append(sig, &iter);
239
240 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
241 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail1);
242 dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail2);
243
244 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, type, &sub);
245 /*
246 * I need to convert the string signature to an integer type signature.
247 * DBUS_TYPE_INT32 is defined as 'i' whereas the string is "i".
248 * I should just be able to cast the first character of the string to an
249 * integer.
250 */
251 val = provide_defaults((int) *type, val);
252 dbus_message_iter_append_basic(&sub, (int) *type, &val);
253 dbus_message_iter_close_container(&iter, &sub);
254
255 dbus_connection_send(bus, sig, NULL);
256 dbus_message_unref(sig);
257 }
258
259
260 /*
261 dbus_bool_t spi_dbus_get_simple_property (DBusConnection *bus, const char *dest, const char *path, const char *interface, const char *prop, int *type, void *ptr, DBusError *error)
262 {
263 DBusMessage *message, *reply;
264 DBusMessageIter iter, iter_variant;
265 int typ;
266
267 dbus_error_init (error);
268 message = dbus_message_new_method_call (dest, path, "org.freedesktop.DBus.Properties", "get");
269 if (!message) return FALSE;
270 if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &prop, DBUS_TYPE_INVALID))
271 {
272 return FALSE;
273 }
274 reply = dbus_connection_send_with_reply_and_block (bus, message, 1000, error);
275 dbus_message_unref (message);
276 if (!reply) return FALSE;
277 dbus_message_iter_init (reply, &iter);
278 dbus_message_iter_recurse (&iter, &iter_variant);
279 typ = dbus_message_iter_get_arg_type (&iter_variant);
280 if (type) *type = typ;
281 if (typ == DBUS_TYPE_INVALID || typ == DBUS_TYPE_STRUCT || typ == DBUS_TYPE_ARRAY)
282 {
283 return FALSE;
284 }
285 dbus_message_iter_get_basic (&iter_variant, ptr);
286 dbus_message_unref (reply);
287 return TRUE;
288 }
289 */
290