summaryrefslogtreecommitdiffstats
path: root/glib/dbus-gvalue.c
blob: bcda92592910620412e3d29d511b85f30849e0bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-gvalue.c GValue to-from DBusMessageIter
 *
 * Copyright (C) 2004 Ximian, Inc.
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * Licensed under the Academic Free License version 2.1
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <dbus-gvalue.h>
#include "dbus/dbus-signature.h"

/* This is slightly evil, we don't use g_value_set_foo() functions */
#define MAP_BASIC_INIT(d_t, g_t)                                     \
    case DBUS_TYPE_##d_t:                                       \
      g_value_init (value, G_TYPE_##g_t);                       \
      break

gboolean
dbus_gvalue_init      (int     type,
		       GValue *value)
{
  gboolean can_convert;

  can_convert = TRUE;

  switch (type)
    {
      MAP_BASIC_INIT (BOOLEAN, BOOLEAN);
      MAP_BASIC_INIT (BYTE,    UCHAR);
      MAP_BASIC_INIT (INT32,   INT);
      MAP_BASIC_INIT (UINT32,  UINT);
      MAP_BASIC_INIT (INT64,   INT64);
      MAP_BASIC_INIT (UINT64,  UINT64);
      MAP_BASIC_INIT (DOUBLE,  DOUBLE);

    case DBUS_TYPE_INT16:
        g_value_init (value, G_TYPE_INT);
      break;
    case DBUS_TYPE_UINT16:
        g_value_init (value, G_TYPE_UINT);
      break;
      
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
        g_value_init (value, G_TYPE_STRING);
      break;
      
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_ARRAY:
    case DBUS_TYPE_VARIANT:
    default:
      can_convert = FALSE;
    }
#undef MAP_BASIC_INIT
  return can_convert;
}

/* FIXME - broken for containers
 */
static int
base_type_from_signature  (const char *signature)
{
  DBusSignatureIter iter;

  dbus_signature_iter_init (&iter, signature);

  return dbus_signature_iter_get_current_type (&iter);
}

const char *
dbus_gvalue_genmarshal_name_from_type (const char *signature)
{
  int type;

  type = base_type_from_signature (signature);
  switch (type)
    {
    case DBUS_TYPE_BOOLEAN:
      return "BOOLEAN";
    case DBUS_TYPE_BYTE:
      return "UCHAR";
    case DBUS_TYPE_INT32:
      return "INT";
    case DBUS_TYPE_UINT32:
      return "UINT";
    case DBUS_TYPE_INT64:
      return "INT64";
    case DBUS_TYPE_UINT64:
      return "UINT64";
    case DBUS_TYPE_DOUBLE:
      return "DOUBLE";
    case DBUS_TYPE_INT16:
      return "INT";
      break;
    case DBUS_TYPE_UINT16:
      return "UINT";
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
      return "STRING";
      
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_ARRAY:
    case DBUS_TYPE_VARIANT:
      return NULL;
    }
  return NULL;
}

const char *
dbus_gvalue_binding_type_from_type (const char *signature)
{
  int type;

  type = base_type_from_signature (signature);

#define STRINGIFY(x) \
  case x: \
    return (#x)
  
  switch (type)
    {
      STRINGIFY(DBUS_TYPE_BOOLEAN);
      STRINGIFY(DBUS_TYPE_BYTE);
      STRINGIFY(DBUS_TYPE_INT32);
      STRINGIFY(DBUS_TYPE_UINT32);
      STRINGIFY(DBUS_TYPE_INT64);
      STRINGIFY(DBUS_TYPE_UINT64);
      STRINGIFY(DBUS_TYPE_DOUBLE);
    case DBUS_TYPE_INT16:
      return "DBUS_TYPE_INT32";
    case DBUS_TYPE_UINT16:
      return "DBUS_TYPE_UINT32";
    STRINGIFY(DBUS_TYPE_STRING);
    STRINGIFY(DBUS_TYPE_OBJECT_PATH);
    STRINGIFY(DBUS_TYPE_SIGNATURE);

    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_ARRAY:
    case DBUS_TYPE_VARIANT:
      return NULL;
    }
#undef STRINGIFY
  return NULL;
}

const char *
dbus_gvalue_ctype_from_type (const char *signature, gboolean in)
{
  int type;

  type = base_type_from_signature (signature);

  switch (type)
    {
    case DBUS_TYPE_BOOLEAN:
      return "gboolean";
    case DBUS_TYPE_BYTE:
      return "guchar";
    case DBUS_TYPE_INT32:
      return "gint32";
    case DBUS_TYPE_UINT32:
      return "guint32";
    case DBUS_TYPE_INT64:
      return "gint64";
    case DBUS_TYPE_UINT64:
      return "guint64";
    case DBUS_TYPE_DOUBLE:
      return "gdouble";
    case DBUS_TYPE_INT16:
      return "gint";
      break;
    case DBUS_TYPE_UINT16:
      return "guint";
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
      /* FIXME - kind of a hack */
      if (in)
	return "const char *";
      else
	return "char *";
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_ARRAY:
    case DBUS_TYPE_VARIANT:
      return NULL;
    }
  return NULL;
}

const char *
dbus_gtype_to_dbus_type (GType type)
{
  switch (type)
    {
    case G_TYPE_CHAR:
    case G_TYPE_UCHAR:
      return DBUS_TYPE_BYTE_AS_STRING;
      
    case G_TYPE_BOOLEAN:
      return DBUS_TYPE_BOOLEAN_AS_STRING;

      /* long gets cut to 32 bits so the remote API is consistent
       * on all architectures
       */
      
    case G_TYPE_LONG:
    case G_TYPE_INT:
      return DBUS_TYPE_INT32_AS_STRING;
    case G_TYPE_ULONG:
    case G_TYPE_UINT:
      return DBUS_TYPE_UINT32_AS_STRING;

    case G_TYPE_INT64:
      return DBUS_TYPE_INT64_AS_STRING;

    case G_TYPE_UINT64:
      return DBUS_TYPE_UINT64_AS_STRING;
      
    case G_TYPE_FLOAT:
    case G_TYPE_DOUBLE:
      return DBUS_TYPE_DOUBLE_AS_STRING;

    case G_TYPE_STRING:
      return DBUS_TYPE_STRING_AS_STRING;

    default:
      return NULL;
    }
}

gboolean
dbus_gvalue_demarshal (DBusMessageIter *iter, GValue *value)
{
  gboolean can_convert = TRUE;

  g_assert (sizeof (dbus_bool_t) == sizeof (value->data[0].v_int));

  dbus_gvalue_init (dbus_message_iter_get_arg_type (iter), value);
  
/* This is slightly evil, we don't use g_value_set_foo() functions */
#define MAP_BASIC(d_t, g_t)                                     \
    case DBUS_TYPE_##d_t:                                       \
      dbus_message_iter_get_basic (iter, &value->data[0]);      \
      break

  switch (dbus_message_iter_get_arg_type (iter))
    {
      MAP_BASIC (BOOLEAN, BOOLEAN);
      MAP_BASIC (BYTE,    UCHAR);
      MAP_BASIC (INT32,   INT);
      MAP_BASIC (UINT32,  UINT);
      MAP_BASIC (INT64,   INT64);
      MAP_BASIC (UINT64,  UINT64);
      MAP_BASIC (DOUBLE,  DOUBLE);

    case DBUS_TYPE_INT16:
      {
        dbus_int16_t v;
        dbus_message_iter_get_basic (iter, &v);
        g_value_set_int (value, v);
      }
      break;
    case DBUS_TYPE_UINT16:
      {
        dbus_uint16_t v;
        dbus_message_iter_get_basic (iter, &v);
        g_value_set_uint (value, v);
      }
      break;
      
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
      {
        const char *s;

        dbus_message_iter_get_basic (iter, &s);
        g_value_set_string (value, s);
      }
      break;
      
    case DBUS_TYPE_STRUCT:
    case DBUS_TYPE_ARRAY:
    case DBUS_TYPE_VARIANT:
    default:
      can_convert = FALSE;
    }
#undef MAP_BASIC
  return can_convert;
}
    
gboolean
dbus_gvalue_marshal (DBusMessageIter *iter, GValue *value)
{
  gboolean can_convert = TRUE;
  GType value_type = G_VALUE_TYPE (value);

  value_type = G_VALUE_TYPE (value);
  
  switch (value_type)
    {
    case G_TYPE_CHAR:
      {
        char b = g_value_get_char (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_BYTE,
                                             &b))
          goto nomem;
      }
      break;
    case G_TYPE_UCHAR:
      {
        unsigned char b = g_value_get_uchar (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_BYTE,
                                             &b))
          goto nomem;
      }
      break;
    case G_TYPE_BOOLEAN:
      {
        dbus_bool_t b = g_value_get_boolean (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_BOOLEAN,
                                             &b))
          goto nomem;
      }
      break;
    case G_TYPE_INT:
      {
        dbus_int32_t v = g_value_get_int (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_INT32,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_UINT:
      {
        dbus_uint32_t v = g_value_get_uint (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_UINT32,
                                             &v))
          goto nomem;
      }
      break;
      /* long gets cut to 32 bits so the remote API is consistent
       * on all architectures
       */
    case G_TYPE_LONG:
      {
        dbus_int32_t v = g_value_get_long (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_INT32,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_ULONG:
      {
        dbus_uint32_t v = g_value_get_ulong (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_UINT32,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_INT64:
      {
        gint64 v = g_value_get_int64 (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_INT64,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_UINT64:
      {
        guint64 v = g_value_get_uint64 (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_UINT64,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_FLOAT:
      {
        double v = g_value_get_float (value);
        
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_DOUBLE,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_DOUBLE:
      {
        double v = g_value_get_double (value);
        
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_DOUBLE,
                                             &v))
          goto nomem;
      }
      break;
    case G_TYPE_STRING:
      /* FIXME, the GValue string may not be valid UTF-8 */
      {
        const char *v = g_value_get_string (value);
        if (!dbus_message_iter_append_basic (iter,
                                             DBUS_TYPE_STRING,
                                             &v))
          goto nomem;
      }
      break;
      
    default:
      /* FIXME: we need to define custom boxed types for arrays
	 etc. so we can map them transparently / pleasantly */
      can_convert = FALSE;
      break;
    }

  return can_convert;

 nomem:
  g_error ("no memory");
  return FALSE;
}