Revert part of last commit
[platform/upstream/at-spi2-core.git] / registryd / de-marshaller.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 <stdlib.h>
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <dbus/dbus.h>
27
28 #include "de-types.h"
29
30 dbus_bool_t spi_dbus_message_iter_get_struct(DBusMessageIter *iter, ...)
31 {
32   va_list args;
33   DBusMessageIter iter_struct;
34   int type;
35   void *ptr;
36
37   dbus_message_iter_recurse(iter, &iter_struct);
38   va_start(args, iter);
39   for (;;)
40   {
41     type = va_arg(args, int);
42     if (type == DBUS_TYPE_INVALID) break;
43     if (type != dbus_message_iter_get_arg_type(&iter_struct))
44     {
45       va_end(args);
46       return FALSE;
47     }
48     ptr = va_arg(args, void *);
49     dbus_message_iter_get_basic(&iter_struct, ptr);
50     dbus_message_iter_next(&iter_struct);
51   }
52   dbus_message_iter_next(iter);
53   va_end(args);
54   return TRUE;
55 }
56
57 dbus_bool_t spi_dbus_message_iter_append_struct(DBusMessageIter *iter, ...)
58 {
59   va_list args;
60   DBusMessageIter iter_struct;
61   int type;
62   void *ptr;
63
64   if (!dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &iter_struct)) return FALSE;
65   va_start(args, iter);
66   for (;;)
67   {
68     type = va_arg(args, int);
69     if (type == DBUS_TYPE_INVALID) break;
70     ptr = va_arg(args, void *);
71     dbus_message_iter_append_basic(&iter_struct, type, ptr);
72   }
73   if (!dbus_message_iter_close_container(iter, &iter_struct)) return FALSE;
74   va_end(args);
75   return TRUE;
76 }
77
78 dbus_bool_t spi_dbus_marshal_deviceEvent(DBusMessage *message, const Accessibility_DeviceEvent *e)
79 {
80   DBusMessageIter iter;
81
82   if (!message) return FALSE;
83   dbus_message_iter_init_append(message, &iter);
84   return spi_dbus_message_iter_append_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_UINT32, &e->hw_code, DBUS_TYPE_UINT32, &e->modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID);
85 }
86
87 dbus_bool_t spi_dbus_demarshal_deviceEvent(DBusMessage *message, Accessibility_DeviceEvent *e)
88 {
89   DBusMessageIter iter;
90   dbus_uint16_t hw_code;
91   dbus_uint16_t modifiers;
92
93   dbus_message_iter_init(message, &iter);
94   if (spi_dbus_message_iter_get_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_INT32, &e->hw_code, DBUS_TYPE_INT32, &e->modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID))
95     return TRUE;
96   /* TODO: Perhaps remove the below code for 2.1 */
97   if (!spi_dbus_message_iter_get_struct(&iter, DBUS_TYPE_UINT32, &e->type, DBUS_TYPE_INT32, &e->id, DBUS_TYPE_INT16, &hw_code, DBUS_TYPE_INT16, &modifiers, DBUS_TYPE_INT32, &e->timestamp, DBUS_TYPE_STRING, &e->event_string, DBUS_TYPE_BOOLEAN, &e->is_text, DBUS_TYPE_INVALID))
98     return FALSE;
99   e->hw_code = hw_code;
100   e->modifiers = modifiers;
101   return TRUE;
102 }
103
104 /*
105  * This is a rather annoying function needed to replace
106  * NULL values of strings with the empty string. Null string
107  * values can be created by the atk_object_get_name or text selection
108  */
109 static const void *
110 provide_defaults(const gint type,
111                  const void *val)
112 {
113   switch (type)
114     {
115       case DBUS_TYPE_STRING:
116       case DBUS_TYPE_OBJECT_PATH:
117            if (!val)
118               return "";
119            else
120               return val;
121       default:
122            return val;
123     }
124 }