2008-05-16 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / spi-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 DBusMessage *
33 spi_dbus_general_error (DBusMessage * message)
34 {
35   return dbus_message_new_error (message,
36                                  "org.freedesktop.atspi.GeneralError",
37                                  "General error");
38 }
39
40
41 DBusMessage *
42 spi_dbus_return_rect (DBusMessage * message, gint ix, gint iy, gint iwidth,
43                       gint iheight)
44 {
45   DBusMessage *reply;
46   dbus_uint32_t x, y, width, height;
47
48   x = ix;
49   y = iy;
50   width = iwidth;
51   height = iheight;
52   reply = dbus_message_new_method_return (message);
53   if (reply)
54     {
55       DBusMessageIter iter, sub;
56       dbus_message_iter_init_append (reply, &iter);
57       if (!dbus_message_iter_open_container
58           (&iter, DBUS_TYPE_STRUCT, NULL, &sub))
59         goto oom;
60       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &x);
61       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &y);
62       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &width);
63       dbus_message_iter_append_basic (&sub, DBUS_TYPE_INT32, &height);
64       if (!dbus_message_iter_close_container (&iter, &sub))
65         goto oom;
66     }
67   return reply;
68 oom:
69   /* todo: return an error */
70   return reply;
71 }
72
73 void spi_dbus_emit_valist(DBusConnection *bus, const char *path, const char *interface, const char *name, int first_arg_type, va_list args)
74 {
75   DBusMessage *sig;
76
77   sig = dbus_message_new_signal(path, interface, name);
78   if (first_arg_type != DBUS_TYPE_INVALID)
79   {
80     dbus_message_append_args_valist(sig, first_arg_type, args);
81   }
82   dbus_connection_send(bus, sig, NULL);
83   dbus_message_unref(sig);
84 }
85
86 dbus_bool_t spi_dbus_message_iter_get_struct(DBusMessageIter *iter, ...)
87 {
88   va_list args;
89   DBusMessageIter iter_struct;
90   int type;
91   void *ptr;
92
93   dbus_message_iter_recurse(iter, &iter_struct);
94   va_start(args, iter);
95   for (;;)
96   {
97     type = va_arg(args, int);
98     if (type == DBUS_TYPE_INVALID) break;
99     if (type != dbus_message_iter_get_arg_type(&iter_struct))
100     {
101       va_end(args);
102       return FALSE;
103     }
104     ptr = va_arg(args, void *);
105     dbus_message_iter_get_basic(&iter_struct, ptr);
106     dbus_message_iter_next(&iter_struct);
107   }
108   dbus_message_iter_next(iter);
109   va_end(args);
110   return TRUE;
111 }
112
113 dbus_bool_t spi_dbus_message_iter_append_struct(DBusMessageIter *iter, ...)
114 {
115   va_list args;
116   DBusMessageIter iter_struct;
117   int type;
118   void *ptr;
119
120   if (!dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &iter_struct)) return FALSE;
121   va_start(args, iter);
122   for (;;)
123   {
124     type = va_arg(args, int);
125     if (type == DBUS_TYPE_INVALID) break;
126     ptr = va_arg(args, void *);
127     dbus_message_iter_append_basic(&iter_struct, type, ptr);
128   }
129   if (!dbus_message_iter_close_container(iter, &iter_struct)) return FALSE;
130   va_end(args);
131   return TRUE;
132 }
133
134 dbus_bool_t spi_dbus_marshall_deviceEvent(DBusMessage *message, const Accessibility_DeviceEvent *e)
135 {
136   DBusMessageIter iter;
137
138   if (!message) return FALSE;
139   dbus_message_iter_init_append(message, &iter);
140   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);
141 }
142
143 dbus_bool_t spi_dbus_demarshall_deviceEvent(DBusMessage *message, Accessibility_DeviceEvent *e)
144 {
145   DBusMessageIter iter;
146
147   dbus_message_iter_init(message, &iter);
148   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);
149 }