summaryrefslogtreecommitdiff
path: root/src/myfrobnicator.c
blob: 9936e6218a5fb2bd0bf7c3a7152ff27f71a7a123 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * Copyright (C) 2008-2010 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz@redhat.com>
 */

#include "config.h"

#include <stdlib.h>

#include "gdbusgi.h"

#include "myfrobnicator.h"

struct _MyFrobnicatorPrivate
{
  guint foo;
};

enum
{
  LASER_FIRED_SIGNAL,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};

G_DEFINE_TYPE (MyFrobnicator, my_frobnicator, G_TYPE_OBJECT);

static void
my_frobnicator_init (MyFrobnicator *frobnicator)
{
}

static void
my_frobnicator_class_init (MyFrobnicatorClass *klass)
{
  /**
   * MyFrobnicator::laser-fired:
   * @frobnicator: A #MyFrobnicator.
   * @str: A string.
   * @carray_as: (array) (element-type utf8):
   * @garray_ai: (array zero-terminated="0") (element-type gint):
   *
   * Check that signal parameters are marshalled correctly onto the
   * bus. Btw, Hmm, apparently (array length=carray_ai_len) doesn't
   * work yet. TODO: file bug
   *
   * Attributes: (gdbus.signal LaserFired)
   */
  signals[LASER_FIRED_SIGNAL] = g_signal_new ("laser-fired",
                                              MY_TYPE_FROBNICATOR,
                                              G_SIGNAL_RUN_LAST,
                                              G_STRUCT_OFFSET (MyFrobnicatorClass, laser_fired),
                                              NULL, /* accumulator */
                                              NULL, /* accu_data */
                                              NULL, /* we don't need a C marshaller if we're only using D-Bus */
                                              G_TYPE_NONE,
                                              3,
                                              G_TYPE_STRING,
                                              G_TYPE_STRV,
                                              G_TYPE_ARRAY);
}

MyFrobnicator *
my_frobnicator_new (void)
{
  return MY_FROBNICATOR (g_object_new (MY_TYPE_FROBNICATOR, NULL));
}

/**
 * my_frobnicator_hello_world:
 * @frobnicator: A #MyFrobnicator.
 * @greeting: A greeting!
 *
 * Computes a response to @greeting.
 *
 * Returns: The response. Free with g_free().
 *
 * Attributes: (gdbus.method HelloWorld) (foo.bar baz)
 */
gchar *
my_frobnicator_hello_world (MyFrobnicator *frobnicator,
                            const gchar   *greeting)
{
  gchar *response;

  g_return_val_if_fail (MY_IS_FROBNICATOR (frobnicator), NULL);
  g_return_val_if_fail (greeting != NULL, NULL);

  response = NULL;

  if (g_strcmp0 (greeting, "fail") == 0)
    {
      GDBusMethodInvocation *invocation;
      invocation = g_dbus_method_invocation_takeover_for_gobject (G_OBJECT (frobnicator));
      g_dbus_method_invocation_return_error (invocation,
                                             G_IO_ERROR,
                                             G_IO_ERROR_FAILED,
                                             "fail boat!");
    }
  else
    {
      response = g_strdup_printf ("Hey %s - you greeted me `%s'",
                                  g_dbus_method_invocation_get_sender (g_dbus_method_invocation_get_for_gobject (G_OBJECT (frobnicator))),
                                  greeting);
    }
  return response;
}

/**
 * my_frobnicator_hello_world_with_gerror:
 *
 * Like my_frobnicator_hello_world() but takes a #GError.
 *
 * Attributes: (gdbus.method HelloWorldWithGError)
 */
gchar *
my_frobnicator_hello_world_with_gerror (MyFrobnicator  *frobnicator,
                                        const gchar    *greeting,
                                        GError        **error)
{
  gchar *response;

  g_return_val_if_fail (MY_IS_FROBNICATOR (frobnicator), NULL);
  g_return_val_if_fail (greeting != NULL, NULL);

  response = NULL;

  if (g_strcmp0 (greeting, "fail") == 0)
    {
      g_set_error (error,
                   G_DBUS_ERROR,
                   G_DBUS_ERROR_FILE_NOT_FOUND,
                   "fail boat with GError!");
    }
  else
    {
      response = g_strdup_printf ("Hey ya'll with GError - you greeted me `%s'",
                                  greeting);
    }

  return response;
}

/**
 * my_frobnicator_simple_types:
 * @frobnicator: A #MyFrobnicator.
 * @v_boolean: Value.
 * @v_int16: Value.
 * @v_uint16: Value.
 * @v_int32: Value.
 * @v_uint32: Value.
 * @v_int64: Value.
 * @v_uint64: Value.
 * @v_int: Value.
 * @v_uint: Value.
 * @v_double: Value.
 * @v_string: Value.
 * @v_object_path: (gdbus.signature o) (foo.bar baz): Value.
 * @v_signature: (gdbus.signature g): Value.
 * @out_boolean: (out): Return location for value.
 * @out_int16: (out): Return location for value.
 * @out_uint16: (out): Return location for value.
 * @out_int32: (out): Return location for value.
 * @out_uint32: (out): Return location for value.
 * @out_int64: (out): Return location for value.
 * @out_uint64: (out): Return location for value.
 * @out_int: (out): Return location for value.
 * @out_uint: (out): Return location for value.
 * @out_double: (out): Return location for value.
 * @out_string: (out): Return location for value.
 * @out_object_path: (out) (gdbus.signature o): Return location for value.
 * @out_signature: (out) (gdbus.signature g): Return location for value.
 *
 * Method demonstrating simple types.
 *
 * Attributes: (gdbus.method SimpleTypes)
 */
void
my_frobnicator_simple_types (MyFrobnicator *frobnicator,
                             gboolean       v_boolean,
                             gint16         v_int16,
                             guint16        v_uint16,
                             gint32         v_int32,
                             guint32        v_uint32,
                             gint64         v_int64,
                             guint64        v_uint64,
                             gint           v_int,
                             guint          v_uint,
                             gdouble        v_double,
                             const gchar   *v_string,
                             const gchar   *v_object_path,
                             const gchar   *v_signature,
                             gboolean      *out_boolean,
                             gint16        *out_int16,
                             guint16       *out_uint16,
                             gint32        *out_int32,
                             guint32       *out_uint32,
                             gint64        *out_int64,
                             guint64       *out_uint64,
                             gint          *out_int,
                             guint         *out_uint,
                             gdouble       *out_double,
                             gchar        **out_string,
                             gchar        **out_object_path,
                             gchar        **out_signature)
{
  g_return_if_fail (MY_IS_FROBNICATOR (frobnicator));
  *out_boolean     = !v_boolean;
  *out_int16       = v_int16  + 100;
  *out_uint16      = v_uint16 + 101;
  *out_int32       = v_int32  + 102;
  *out_uint32      = v_uint32 + 103;
  *out_int64       = v_int64  + 104;
  *out_uint64      = v_uint64 + 105;
  *out_int         = v_int    + 106;
  *out_uint        = v_uint   + 107;
  *out_double      = v_double + 108;
  *out_string      = g_strdup_printf ("You passed `%s'", v_string);
  *out_object_path = g_strdup_printf ("/you/passed%s", v_object_path);
  *out_signature   = g_strdup_printf ("assgit%s", v_signature);
}

/**
 * my_frobnicator_strip:
 * @frobnicator: A #MyFrobnicator.
 * @str: (inout): A string that will be stripped using g_strstrip().
 *
 * An example of a method with an inout parameter.
 *
 * Attributes: (gdbus.method Strip)
 */
void
my_frobnicator_strip (MyFrobnicator  *frobnicator,
                      gchar          *str)
{
  g_return_if_fail (MY_IS_FROBNICATOR (frobnicator));
  g_return_if_fail (str != NULL);
  g_strstrip (str);
}

/**
 * my_frobnicator_return_const_str:
 * @frobnicator: A #MyFrobnicator.
 * @num: A number.
 *
 * An method that returns a const object.
 *
 * Returns: A constant string that should not be freed.
 *
 * Attributes: (gdbus.method ReturnConstStr)
 */
const gchar *
my_frobnicator_return_const_str (MyFrobnicator  *frobnicator,
                                 guint           num)
{
  const gchar *ret;
  switch (num)
    {
    case 0:
      ret = "zero";
      break;
    case 1:
      ret = "one";
      break;
    case 2:
      ret = "two";
      break;
    default:
      ret = "bigger than two!";
      break;
    }
  return ret;
}

/**
 * my_frobnicator_poke_path:
 * @frobnicator: A #MyFrobnicator
 * @object_path: (gdbus.signature o) (foo.bar bat): An object path.
 *
 * Manipulate an object path.
 *
 * Returns: (gdbus.signature o): A new object path. Free with g_free().
 *
 * Attributes: (gdbus.method PokePath)
 */
gchar *
my_frobnicator_poke_path (MyFrobnicator *frobnicator,
                          const gchar   *object_path)
{
  return g_strdup_printf ("/another/object/path/here%s", object_path);
}


/**
 * my_frobnicator_test_arrays:
 * @frobnicator:
 * @carray_ai: (array length=carray_ai_len):
 * @carray_ai_len:
 * @carray_as:
 * @gbytearray_ay: (element-type guint8):
 * @garray_ai: (element-type gint):
 * @garray_ad: (element-type gdouble):
 * @gptrarray_as: (element-type utf8):
 * @carray_as_nzt_len:
 * @carray_as_nzt: (element-type utf8) (array length=carray_as_nzt_len):
 *
 * Returns: The sum of all elements.
 *
 * Attributes: (gdbus.method TestArrays)
 */
gdouble
my_frobnicator_test_arrays (MyFrobnicator       *frobnicator,
                            gint                *carray_ai,
                            guint                carray_ai_len,
                            const gchar *const  *carray_as,
                            GByteArray          *gbytearray_ay,
                            GArray              *garray_ai,
                            GArray              *garray_ad,
                            GPtrArray           *gptrarray_as,
                            guint                carray_as_nzt_len,
                            const gchar *const  *carray_as_nzt)
{
  gdouble ret;
  guint n;

  ret = 0.0;

  for (n = 0; n < carray_ai_len; n++)
    ret += carray_ai[n];

  for (n = 0; carray_as[n] != NULL; n++)
    ret += atof (carray_as[n]);

  for (n = 0; n < gbytearray_ay->len; n++)
    ret += gbytearray_ay->data[n];

  for (n = 0; n < garray_ai->len; n++)
    ret += g_array_index (garray_ai, gint, n);

  for (n = 0; n < garray_ad->len; n++)
    ret += g_array_index (garray_ad, gdouble, n);

  for (n = 0; n < gptrarray_as->len; n++)
    ret += atof (gptrarray_as->pdata[n]);

  for (n = 0; n < carray_as_nzt_len; n++)
    ret += atof (carray_as_nzt[n]);

  return ret;
}

/**
 * my_frobnicator_return_arrays:
 * @frobnicator:
 * @out_garray_ai: (out) (element-type gint):
 * @out_garray_ad: (out)(element-type gdouble):
 * @out_gptrarray_as: (out) (element-type utf8):
 * @out_gbytearray_ay: (out) (element-type guint8):
 * @out_carray_ai: (out) (array length=out_carray_ai_len):
 * @out_carray_ai_len:
 * @out_carray_as_nzt_len:
 * @out_carray_as_nzt: (out) (element-type utf8) (array length=out_carray_as_nzt_len):
 * @out_carray_as: (out) (array) (element-type utf8):
 *
 * Attributes: (gdbus.method ReturnArrays)
 */
void
my_frobnicator_return_arrays (MyFrobnicator       *frobnicator,
                              GArray             **out_garray_ai,
                              GArray             **out_garray_ad,
                              GPtrArray          **out_gptrarray_as,
                              GByteArray         **out_gbytearray_ay,
                              gint               **out_carray_ai,
                              guint               *out_carray_ai_len,
                              guint               *out_carray_as_nzt_len,
                              gchar             ***out_carray_as_nzt,
                              gchar             ***out_carray_as)
{
  gint *ai;
  gchar **as;

  *out_garray_ai = g_array_new (FALSE, FALSE, sizeof (gint));
  g_array_set_size (*out_garray_ai, 3);
  ((gint *) (*out_garray_ai)->data)[0] = 10000;
  ((gint *) (*out_garray_ai)->data)[1] = 20000;
  ((gint *) (*out_garray_ai)->data)[2] = 30000;

  *out_garray_ad = g_array_new (FALSE, FALSE, sizeof (gdouble));
  g_array_set_size (*out_garray_ad, 3);
  ((gdouble *) (*out_garray_ad)->data)[0] = 1000.0;
  ((gdouble *) (*out_garray_ad)->data)[1] = 2000.0;
  ((gdouble *) (*out_garray_ad)->data)[2] = 3000.0;

  *out_gptrarray_as = g_ptr_array_new ();
  g_ptr_array_add (*out_gptrarray_as, "100");
  g_ptr_array_add (*out_gptrarray_as, "200");
  g_ptr_array_add (*out_gptrarray_as, "300");

  *out_gbytearray_ay = g_byte_array_new ();
  g_byte_array_set_size (*out_gbytearray_ay, 3);
  (*out_gbytearray_ay)->data[0] = 10;
  (*out_gbytearray_ay)->data[1] = 20;
  (*out_gbytearray_ay)->data[2] = 30;

  ai = g_new0 (gint, 3);
  ai[0] = 1;
  ai[1] = 2;
  ai[2] = 3;
  *out_carray_ai = ai;
  *out_carray_ai_len = 3;

  as = g_new0 (gchar *, 3);
  as[0] = g_strdup ("0.1");
  as[1] = g_strdup ("0.2");
  as[2] = g_strdup ("0.3");
  *out_carray_as_nzt = as;
  *out_carray_as_nzt_len = 3;

  as = g_new0 (gchar *, 4);
  as[0] = g_strdup ("0.01");
  as[1] = g_strdup ("0.02");
  as[2] = g_strdup ("0.03");
  *out_carray_as = as;
}

/**
 * my_frobnicator_fire_laser:
 * @frobnicator:
 *
 * Emits the laser-fired signal.
 *
 * Attributes: (gdbus.method FireLaser)
 */
void
my_frobnicator_fire_laser (MyFrobnicator *frobnicator)
{
  const gchar *strv[4];
  GArray *garray_ai;

  strv[0] = "zero";
  strv[1] = "two";
  strv[2] = "three";
  strv[3] = NULL;

  garray_ai = g_array_new (FALSE, FALSE, sizeof (gint));
  g_array_set_size (garray_ai, 3);
  ((gint *) (garray_ai)->data)[0] = 10;
  ((gint *) (garray_ai)->data)[1] = 20;
  ((gint *) (garray_ai)->data)[2] = 30;

  g_signal_emit (frobnicator,
                 signals[LASER_FIRED_SIGNAL],
                 0,
                 "woot",
                 strv,
                 garray_ai);

  g_array_unref (garray_ai);
}