f859156747387b68ac1b704c485bc3e1f111117c
[platform/core/uifw/at-spi2-atk.git] / common / spi-dbus.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  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 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  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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 "spi-types.h"
31
32 /*
33 DBusMessage *
34 spi_dbus_general_error (DBusMessage * message)
35 {
36   return dbus_message_new_error (message,
37                                  "org.freedesktop.atspi.GeneralError",
38                                  "General error");
39 }
40 */
41
42
43 DBusMessage *
44 spi_dbus_return_rect (DBusMessage * message, gint ix, gint iy, gint iwidth,
45                       gint iheight)
46 {
47   DBusMessage *reply;
48   dbus_uint32_t x, y, width, height;
49
50   x = ix;
51   y = iy;
52   width = iwidth;
53   height = iheight;
54   reply = dbus_message_new_method_return (message);
55   if (reply)
56     {
57       DBusMessageIter iter, sub;
58       dbus_message_iter_init_append (reply, &iter);
59       if (!dbus_message_iter_open_container
60           (&iter, DBUS_TYPE_STRUCT, NULL, &sub))
61         goto oom;
62       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &x);
63       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &y);
64       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &width);
65       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &height);
66       if (!dbus_message_iter_close_container (&iter, &sub))
67         goto oom;
68     }
69   return reply;
70 oom:
71   /* todo: return an error */
72   return reply;
73 }
74
75 void spi_dbus_emit_valist(DBusConnection *bus, const char *path, const char *interface, const char *name, int first_arg_type, va_list args)
76 {
77   DBusMessage *sig;
78
79   sig = dbus_message_new_signal(path, interface, name);
80   if (first_arg_type != DBUS_TYPE_INVALID)
81   {
82     dbus_message_append_args_valist(sig, first_arg_type, args);
83   }
84   dbus_connection_send(bus, sig, NULL);
85   dbus_message_unref(sig);
86 }
87
88 dbus_bool_t spi_dbus_message_iter_get_struct(DBusMessageIter *iter, ...)
89 {
90   va_list args;
91   DBusMessageIter iter_struct;
92   int type;
93   void *ptr;
94
95   dbus_message_iter_recurse(iter, &iter_struct);
96   va_start(args, iter);
97   for (;;)
98   {
99     type = va_arg(args, int);
100     if (type == DBUS_TYPE_INVALID) break;
101     if (type != dbus_message_iter_get_arg_type(&iter_struct))
102     {
103       va_end(args);
104       return FALSE;
105     }
106     ptr = va_arg(args, void *);
107     dbus_message_iter_get_basic(&iter_struct, ptr);
108     dbus_message_iter_next(&iter_struct);
109   }
110   dbus_message_iter_next(iter);
111   va_end(args);
112   return TRUE;
113 }
114
115 dbus_bool_t spi_dbus_message_iter_append_struct(DBusMessageIter *iter, ...)
116 {
117   va_list args;
118   DBusMessageIter iter_struct;
119   int type;
120   void *ptr;
121
122   if (!dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &iter_struct)) return FALSE;
123   va_start(args, iter);
124   for (;;)
125   {
126     type = va_arg(args, int);
127     if (type == DBUS_TYPE_INVALID) break;
128     ptr = va_arg(args, void *);
129     dbus_message_iter_append_basic(&iter_struct, type, ptr);
130   }
131   if (!dbus_message_iter_close_container(iter, &iter_struct)) return FALSE;
132   va_end(args);
133   return TRUE;
134 }
135
136 dbus_bool_t spi_dbus_marshal_deviceEvent(DBusMessage *message, const Accessibility_DeviceEvent *e)
137 {
138   DBusMessageIter iter;
139
140   if (!message) return FALSE;
141   dbus_message_iter_init_append(message, &iter);
142   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);
143 }
144
145 dbus_bool_t spi_dbus_demarshal_deviceEvent(DBusMessage *message, Accessibility_DeviceEvent *e)
146 {
147   DBusMessageIter iter;
148
149   dbus_message_iter_init(message, &iter);
150   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);
151 }
152
153 /*
154  * This is a rather annoying function needed to replace
155  * NULL values of strings with the empty string. Null string
156  * values can be created by the atk_object_get_name or text selection
157  */
158 static const void *
159 provide_defaults(const gint type,
160                  const void *val)
161 {
162   switch (type)
163     {
164       case DBUS_TYPE_STRING:
165       case DBUS_TYPE_OBJECT_PATH:
166            if (!val)
167               return "";
168            else
169               return val;
170       default:
171            return val;
172     }
173 }
174
175 void 
176 spi_dbus_emit_signal(DBusConnection *bus, 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      const char *type,
183      const void *val)
184 {
185   gchar *cname, *t;
186   DBusMessage *sig;
187   DBusMessageIter iter, sub;
188   if (!klass) klass = "";
189   if (!major) major = "";
190   if (!minor) minor = "";
191   if (!type) type = "u";
192
193   /*
194    * This is very annoying, but as '-' isn't a legal signal
195    * name in D-Bus (Why not??!?) The names need converting
196    * on this side, and again on the client side.
197    */
198   cname = g_strdup(major);
199   while ((t = strchr(cname, '-')) != NULL) *t = '_';
200
201   sig = dbus_message_new_signal(path, klass, cname);
202   g_free(cname);
203
204   dbus_message_iter_init_append(sig, &iter);
205
206   dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &minor);
207   dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail1);
208   dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &detail2);
209
210   dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, type, &sub);
211   /*
212    * I need to convert the string signature to an integer type signature.
213    * DBUS_TYPE_INT32 is defined as 'i' whereas the string is "i".
214    * I should just be able to cast the first character of the string to an
215    * integer.
216    */
217   val = provide_defaults((int) *type, val);
218   dbus_message_iter_append_basic(&sub, (int) *type, &val);
219   dbus_message_iter_close_container(&iter, &sub);
220
221   dbus_connection_send(bus, sig, NULL);
222   dbus_message_unref(sig);
223 }
224
225 /*
226 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)
227 {
228   DBusMessage *message, *reply;
229   DBusMessageIter iter, iter_variant;
230   int typ;
231
232   dbus_error_init (error);
233   message = dbus_message_new_method_call (dest, path, "org.freedesktop.DBus.Properties", "get");
234   if (!message) return FALSE;
235   if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &prop, DBUS_TYPE_INVALID))
236   {
237     return FALSE;
238   }
239   reply = dbus_connection_send_with_reply_and_block (bus, message, 1000, error);
240   dbus_message_unref (message);
241   if (!reply) return FALSE;
242   dbus_message_iter_init (reply, &iter);
243   dbus_message_iter_recurse (&iter, &iter_variant);
244   typ = dbus_message_iter_get_arg_type (&iter_variant);
245   if (type) *type = typ;
246   if (typ == DBUS_TYPE_INVALID || typ == DBUS_TYPE_STRUCT || typ == DBUS_TYPE_ARRAY)
247   {
248     return FALSE;
249   }
250   dbus_message_iter_get_basic (&iter_variant, ptr);
251   dbus_message_unref (reply);
252   return TRUE;
253 }
254 */