-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathloudbus.c
1600 lines (1384 loc) · 48.2 KB
/
loudbus.c
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* loudbus.c
* A D-Bus Client for Racket.
*
* Copyright (c) 2012-15 Zarni Htet, Alexandra Greenberg, Mark Lewis,
* Evan Manuella, Samuel A. Rebelsky, Hart Russell, Mani Tiwaree,
* and Christine Tran. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published
* by the Free Software Foundation, either version 3 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, see <http://www.gnu.org/licenses/>.
*/
// +-------+----------------------------------------------------------
// | Notes |
// +-------+
/*
* Since Racket has moved exclusively to 3M-style garbage collection
(or so it seems, since it no longer ships with the CGC libraries,
this needs to be annotated for 3M, either manually or automatically.
It's hard to debug automatically annotated code, so we've done our
best to annotate it manually.
* This implementation is incomplete. We expect to add other methods
(and support for other types) in the future.
*/
// +---------+--------------------------------------------------------
// | Headers |
// +---------+
#include <stdlib.h> // For malloc, random and such
#include <stdio.h> // We use fprintf for error messages during
// development.
#include <time.h> // For seeing our random number generator
#include <glib.h> // For various glib stuff.
#include <gio/gio.h> // For the GDBus functions.
#include <escheme.h> // For all the fun Scheme stuff
#include <scheme.h> // For more fun Scheme stuff
// +--------+---------------------------------------------------------
// | Macros |
// +--------+
#ifdef VERBOSE
#define LOG(FORMAT, ARGS...) \
do { \
fprintf (stderr, "\t *** "); \
fprintf (stderr, FORMAT, ## ARGS); \
fprintf (stderr, "\n"); \
} while (0)
#define SCHEME_LOG(MSG,OBJ) loudbus_log_scheme_object (MSG, OBJ)
#else
#define LOG(FORMAT, ARGS...) do { } while (0)
#define SCHEME_LOG(MSG,OBJ) do { } while (0)
#endif
// +-------+----------------------------------------------------------
// | Types |
// +-------+
/**
* The information we store for a proxy. In addition to the main
* proxy, we also need information on the proxy, so that we can
* look up information.
*/
struct LouDBusProxy
{
int signature; // Identifies this as a proxy
GDBusProxy *proxy; // The real proxy
GDBusNodeInfo *ninfo; // Information on the proxy
GDBusInterfaceInfo *iinfo; // Information on the interace, used
// to extract info about param. types
};
typedef struct LouDBusProxy LouDBusProxy;
// +---------+--------------------------------------------------------
// | Globals |
// +---------+
/**
* A Scheme object to tag proxies.
*/
static Scheme_Object *LOUDBUS_PROXY_TAG;
// +--------------------------+---------------------------------------
// | Selected Predeclarations |
// +--------------------------+
static Scheme_Object *loudbus_call_with_closure (int argc,
Scheme_Object **argv,
Scheme_Object *prim);
static void loudbus_proxy_free (LouDBusProxy *proxy);
static int g_dbus_method_info_num_formals (GDBusMethodInfo *method);
static GVariant *scheme_object_to_parameter (Scheme_Object *obj, gchar *type);
static LouDBusProxy *scheme_object_to_proxy (Scheme_Object *obj);
static char *scheme_object_to_string (Scheme_Object *scmval);
static int loudbus_proxy_validate (LouDBusProxy *proxy);
// +---------------------------------------+--------------------------
// | Bridges Between LouDBusProxy and Racket |
// +---------------------------------------+
/**
* Finalize a loudbus_proxy.
*/
static void
loudbus_proxy_finalize (void *p, void *data)
{
LouDBusProxy *proxy;
LOG ("loudbus_proxy_finalize (%p,%p)", p, data);
proxy = scheme_object_to_proxy (p);
loudbus_proxy_free (proxy);
} // loudbus_proxy_finalize
// +-----------------+------------------------------------------------
// | Local Utilities |
// +-----------------+
/**
* Convert underscores to dashes in a string.
*/
static void
dash_it_all (gchar *str)
{
while (*str != '\0')
{
if (*str == '_')
*str = '-';
str++;
} // while
} // dash_it_all
/**
* Log a message and a scheme object.
*/
static void
loudbus_log_scheme_object (gchar *msg, Scheme_Object *obj)
{
gchar *rendered;
rendered = scheme_display_to_string (obj, NULL);
fprintf (stderr, "%s: %s\n", msg, rendered);
} // loudbus_log_scheme_object
/**
* Get the signature used to identify LouDBusProxy objects.
*/
static int
loudbus_proxy_signature (void)
{
static int signature = 0; // The signature.
// Get a non-zero signature.
while (signature == 0)
{
signature = random ();
} // while (signature == 0)
return signature;
} // loudbus_proxy_signature
/**
* Get information on the proxy.
*/
static GDBusNodeInfo *
g_dbus_proxy_get_node_info (GDBusProxy *proxy)
{
GError *error; // Error returned by various functions.
GVariant *response; // The response from the proxy call.
GDBusNodeInfo *info; // Information on the node.
const gchar *xml; // XML code for the proxy interface.
// Get the introspection data
error = NULL;
response =
g_dbus_proxy_call_sync (proxy,
"org.freedesktop.DBus.Introspectable.Introspect",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (response == NULL)
{
return NULL;
} // if (response == NULL)
// Get the XML from the introspection data
g_variant_get (response, "(&s)", &xml);
// Build an object that lets us explore the introspection data.
error = NULL;
info = g_dbus_node_info_new_for_xml (xml, &error);
g_variant_unref (response);
if (info == NULL)
{
return NULL;
} // if (info == NULL)
// And return that object
return info;
} // g_dbus_proxy_get_node_info
/**
* Determine the length of a null-terminated array of pointers.
*/
static int
parray_len (gpointer *arr)
{
int i; // Index into the array
if (arr == NULL)
return 0;
for (i = 0; arr[i] != NULL; i++)
;
return i;
} // parray_len
/**
* Register a scheme function. Provides a slightly more concise interface
* to a few lines that we type regularly.
*/
static void
register_function (Scheme_Prim *prim, gchar *name,
int minarity, int maxarity,
Scheme_Env *menv)
{
Scheme_Object *proc =
scheme_make_prim_w_arity (prim, name, minarity, maxarity);
scheme_add_global (name, proc, menv);
} // register_function
/**
* Convert dashes to underscores in a string.
*/
static void
score_it_all (gchar *str)
{
while (*str != '\0')
{
if (*str == '-')
*str = '_';
str++;
} // while
} // score_it_all
// +-----------------+------------------------------------------------
// | Proxy Functions |
// +-----------------+
/**
* Free one of the allocated proxies.
*/
void
loudbus_proxy_free (LouDBusProxy *proxy)
{
// Sanity check 1. Make sure that it's not NULL.
if (proxy == NULL)
return;
// Sanity check 2. Make sure that it's really an LouDBusProxy.
if (! loudbus_proxy_validate (proxy))
return;
// Clear the signature (so that we don't identify this as a
// LouDBusProxy in the future).
proxy->signature = 0;
// Clear the proxy.
if (proxy->proxy != NULL)
{
g_object_unref (proxy->proxy);
proxy->proxy = NULL;
} // if (proxy->proxy != NULL)
// Clear the node information.
if (proxy->ninfo != NULL)
{
g_dbus_node_info_unref (proxy->ninfo);
proxy->ninfo = NULL;
} // if (proxy->ninfo != NULL)
// Clear the interface information
proxy->iinfo = NULL; // Part of the node info, so not freed separately.
// And free the enclosing structure
g_free (proxy);
} // loudbus_proxy_free
LouDBusProxy *
loudbus_proxy_new (gchar *service, gchar *object, gchar *interface,
GError **errorp)
{
LouDBusProxy *proxy; // The proxy we're creating
// Allocate space for the struct.
proxy = g_malloc0 (sizeof (LouDBusProxy));
if (proxy == NULL)
{
LOG ("loudbus_proxy_new: Could not allocate proxy.");
return NULL;
} // if (proxy == NULL)
LOG ("Creating proxy for (%s,%s,%s)", service, object, interface);
proxy->proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
service,
object,
interface,
NULL,
errorp);
if (proxy->proxy == NULL)
{
LOG ("loudbus_proxy_new: Could not build proxy.");
g_free (proxy);
return NULL;
} // if we failed to create the proxy.
// Get the node information
proxy->ninfo = g_dbus_proxy_get_node_info (proxy->proxy);
if (proxy->ninfo == NULL)
{
LOG ("loudbus_proxy_new: Could not get node info.");
g_object_unref (proxy->proxy);
g_free (proxy);
return NULL;
} // if we failed to get node information
// Get the interface information
proxy->iinfo = g_dbus_node_info_lookup_interface(proxy->ninfo, interface);
if (proxy->iinfo == NULL)
{
LOG ("loudbus_proxy_new: Could not get interface info.");
g_object_unref (proxy->proxy);
g_dbus_node_info_unref (proxy->ninfo);
g_free (proxy);
return NULL;
} // if we failed to get interface information
// We will be looking stuff up in the interface, so build a cache
g_dbus_interface_info_cache_build (proxy->iinfo);
// Set the signature
proxy->signature = loudbus_proxy_signature ();
// And we seem to be done
return proxy;
} // loudbus_proxy_new
int
loudbus_proxy_validate (LouDBusProxy *proxy)
{
// Sanity check. We don't want segfaults.
if (proxy == NULL)
return 0;
// Things are only proxies if they contain the magic signature.
return (proxy->signature == loudbus_proxy_signature ());
} // loudbus_proxy_validate
// +-----------------+------------------------------------------------
// | Type Conversion |
// +-----------------+
/**
* Convert a D-Bus signature to a human-readable string.
*/
static gchar *
dbus_signature_to_string (gchar *signature)
{
switch (signature[0])
{
case 'a':
switch (signature[1])
{
case 'i':
return "list/vector of integers";
case 's':
return "list/vector of strings";
case 'y':
return "bytes";
default:
return signature;
} // inner switch
case 'i':
return "integer";
case 's':
return "string";
case 'y':
return "byte";
default:
return signature;
} // switch
} // dbus_signature_to_string
/**
* Convert a GVariant to a Scheme object. Returns NULL if there's a
* problem.
*/
static Scheme_Object *
g_variant_to_scheme_object (GVariant *gv)
{
const GVariantType *type; // The type of the GVariant
const gchar *typestring; // A string that describes the type
int i; // A counter variable
int len; // Length of arrays and tuples
Scheme_Object *lst = NULL; // A list that we build as a result
Scheme_Object *sval = NULL; // One value
Scheme_Object *result = NULL; // One result to return.
// Special case: We'll treat NULL as void.
if (gv == NULL)
{
return scheme_void;
} // if (gv == NULL)
// Get the type
type = g_variant_get_type (gv);
typestring = g_variant_get_type_string (gv);
// ** Handle most of the basic types **
// Integer
if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32))
{
// We don't refer to any Scheme objects across allocating calls,
// so no need for GC code.
int i;
i = g_variant_get_int32 (gv);
result = scheme_make_integer (i);
return result;
} // if it's an integer
// Double
if (g_variant_type_equal (type, G_VARIANT_TYPE_DOUBLE))
{
double d;
d = g_variant_get_double (gv);
result = scheme_make_double (d);
return result;
} // if it's a double
// String
if (g_variant_type_equal (type, G_VARIANT_TYPE_STRING))
{
// We don't refer to any Scheme objects across allocating calls,
// so no need for GC code.
const gchar *str;
str = g_variant_get_string (gv, NULL);
result = scheme_make_locale_string (str);
return result;
} // if it's a string
// ** Handle some special cases **
// We treat arrays of bytes as bytestrings
if (g_strcmp0 (typestring, "ay") == 0)
{
gsize size;
guchar *data;
data = (guchar *) g_variant_get_fixed_array (gv, &size, sizeof (guchar));
return scheme_make_sized_byte_string ((char *) data, size, 1);
} // if it's an array of bytes
// ** Handle the compound types **
// Tuple or Array
if ( (g_variant_type_is_tuple (type))
|| (g_variant_type_is_array (type)) )
{
// Find out how many values to put into the list.
len = g_variant_n_children (gv);
// Here, we are referring to stuff across allocating calls, so we
// need to be careful.
MZ_GC_DECL_REG (2);
MZ_GC_VAR_IN_REG (0, lst);
MZ_GC_VAR_IN_REG (1, sval);
MZ_GC_REG ();
// Start with the empty list.
lst = scheme_null;
// Step through the items, right to left, adding them to the list.
for (i = len-1; i >= 0; i--)
{
sval = g_variant_to_scheme_object (g_variant_get_child_value (gv, i));
lst = scheme_make_pair (sval, lst);
} // for
// Okay, we've made it through the list, now we can clean up.
MZ_GC_UNREG ();
if ((g_variant_type_is_array (type)))
{
//If type is array, convert to vector
scheme_list_to_vector ((char*)lst);
}//If array
// And we're done.
return lst;
} // if it's a tuple or an array
// Unknown. Give up.
scheme_signal_error ("Unknown type %s", typestring);
return scheme_void;
} // g_variant_to_scheme_object
/**
* Convert a Scheme list or vector to a GVariant that represents an array.
*/
static GVariant *
scheme_object_to_array (Scheme_Object *lv, gchar *type)
{
Scheme_Object *sval; // One element of the list/array
GVariant *gval; // The converted element
GVariantBuilder *builder;
// Special case: The empty list gives the empty array.
if (SCHEME_NULLP (lv))
{
// Note: For individual objects, D-Bus type signatures are acceptable
// as GVariant type strings.
builder = g_variant_builder_new ((GVariantType *) type);
if (builder == NULL)
return NULL;
return g_variant_builder_end (builder);
} // if it's null
// A list, or so we think.
if (SCHEME_PAIRP (lv))
{
builder = g_variant_builder_new ((GVariantType *) type);
if (builder == NULL)
return NULL;
// Follow the cons cells through the list
while (SCHEME_PAIRP (lv))
{
sval = SCHEME_CAR (lv);
gval = scheme_object_to_parameter (sval, type+1);
if (gval == NULL)
{
g_variant_builder_unref (builder);
return NULL;
} // if (gval == NULL)
g_variant_builder_add_value (builder, gval);
lv = SCHEME_CDR (lv);
} // while
// We've reached the end. Was it really a list?
if (! SCHEME_NULLP (lv))
{
g_variant_builder_unref (builder);
return NULL;
} // If the list does not end in null, so it's not a list.
// We've hit the null at the end of the list.
return g_variant_builder_end (builder);
} // if it's a list
// A vector
else if (SCHEME_VECTORP (lv))
{
int len = SCHEME_VEC_SIZE (lv);
int i;
LOG ("scheme_object_to_array: Handling a vector of length %d", len);
builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
if (builder == NULL)
return NULL;
for (i = 0; i < len; i++)
{
sval = SCHEME_VEC_ELS(lv)[i];
gval = scheme_object_to_parameter (sval, type + 1);
if (gval == NULL)
{
g_variant_builder_unref (builder);
return NULL;
} // if we could not convert the object
g_variant_builder_add_value (builder, gval);
} // for each index
return g_variant_builder_end (builder);
} // if it's a vector
// Can only convert lists and vectors.
else
return NULL;
} // scheme_object_to_array
/**
* Convert a Scheme object to a GVariant that will serve as one of
* the parameters of a call go g_dbus_proxy_call_.... Returns NULL
* if it is unable to do the conversion.
*/
static GVariant *
scheme_object_to_parameter (Scheme_Object *obj, gchar *type)
{
gchar *str; // A temporary string
// Special case: Array of bytes
if (g_strcmp0 (type, "ay") == 0)
{
if (SCHEME_BYTE_STRINGP (obj))
{
return g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE,
SCHEME_BYTE_STR_VAL (obj),
SCHEME_BYTE_STRLEN_VAL (obj),
sizeof (guchar));
} // if it's a byte string
} // array of bytes
// Handle normal cases
switch (type[0])
{
// Arrays
case 'a':
return scheme_object_to_array (obj, type);
// Doubles
case 'd':
if (SCHEME_DBLP (obj))
return g_variant_new ("d", SCHEME_DBL_VAL (obj));
else if (SCHEME_FLTP (obj))
return g_variant_new ("d", (double) SCHEME_FLT_VAL (obj));
else if (SCHEME_INTP (obj))
return g_variant_new ("d", (double) SCHEME_INT_VAL (obj));
else if (SCHEME_RATIONALP (obj))
return g_variant_new ("d", (double) scheme_rational_to_double (obj));
else
return NULL;
// 32 bit integers
case 'i':
if (SCHEME_INTP (obj))
return g_variant_new ("i", (int) SCHEME_INT_VAL (obj));
else if (SCHEME_DBLP (obj))
return g_variant_new ("i", (int) SCHEME_DBL_VAL (obj));
else if (SCHEME_FLTP (obj))
return g_variant_new ("i", (int) SCHEME_FLT_VAL (obj));
else if (SCHEME_RATIONALP (obj))
return g_variant_new ("i", (int) scheme_rational_to_double (obj));
else
return NULL;
// Strings
case 's':
str = scheme_object_to_string (obj);
if (str == NULL)
return NULL;
return g_variant_new ("s", str);
// 32 bit unsigned integers
case 'u':
if (SCHEME_INTP (obj))
return g_variant_new ("u", (unsigned int) SCHEME_INT_VAL (obj));
else
return NULL;
// Everything else is currently unsupported
default:
return NULL;
} // switch
} // scheme_object_to_parameter
/**
* Convert a Scheme object representing an LouDBusProxy to the proxy.
* Returns NULL if it cannot convert.
*/
static LouDBusProxy *
scheme_object_to_proxy (Scheme_Object *obj)
{
LouDBusProxy *proxy;
LOG ("scheme_object_to_proxy (%p)", obj);
// Make sure that we have a pointer.
if (! SCHEME_CPTRP (obj))
{
LOG ("scheme_object_to_proxy: not a pointer");
return NULL;
} // if the object is not a pointer
#ifdef CHECK_POINTER_TYPE
/*
Note: I don't seem to have the right tag on the object, which
suggests to me that the scheme_make_cptr doesn't quite work
the way I expect.
*/
// Make sure that Scheme thinks it's the right kind of pointer.
if (SCHEME_CPTR_TYPE (obj) == LOUDBUS_PROXY_TAG)
{
LOG ("scheme_object_to_proxy: wrong type of pointer");
return NULL;
} // if the object has the wrong type
#endif
// Get the pointer
proxy = SCHEME_CPTR_VAL (obj);
LOG ("scheme_object_to_proxy: potential proxy is %p", proxy);
// Make sure that we also think that it's a proxy.
if (! loudbus_proxy_validate (proxy))
{
LOG ("scheme_object_to_proxy: not really a proxy");
return NULL;
} // if it's not really a proxy
LOG ("scheme_object_to_proxy: validated.");
// And that's it
return proxy;
} // scheme_object_to_proxy
/**
* Given some kind of Scheme string value, convert it to a C string
* If scmval is not a string value, returns NULL.
*/
static char *
scheme_object_to_string (Scheme_Object *scmval)
{
char *str = NULL;
// Char strings are the normal Scheme strings. They need to be
// converted to byte strings.
if (SCHEME_CHAR_STRINGP (scmval))
{
scmval = scheme_char_string_to_byte_string_locale (scmval);
str = SCHEME_BYTE_STR_VAL (scmval);
} // if it's a char string
// Byte strings are easy, but not the typical Scheme strings.
else if (SCHEME_BYTE_STRINGP (scmval))
{
str = SCHEME_BYTE_STR_VAL (scmval);
} // if it's a byte string
// A design decision: We'll treat symbols as strings. (It certainly
// makes things easier for the client.)
else if (SCHEME_SYMBOLP (scmval))
{
str = SCHEME_SYM_VAL (scmval);
} // if it's a symbol
// Everything else is not a string
else
{
// Signal an error by setting the return value to NULL.
str = NULL;
} // if it's not a string
return str;
} // scheme_object_to_string
/**
* Convert an array of Scheme objects to a GVariant that serves as
* the primary parameter to g_dbus_proxy_call.
*/
static GVariant *
scheme_objects_to_parameter_tuple (gchar *fun,
int arity,
Scheme_Object **objects,
GDBusArgInfo *formals[])
{
int i; // Counter variable
GVariantBuilder *builder;
// Something to let us build tuples
GVariant *result; // The GVariant we build
GVariant *actual; // One actual
builder = g_variant_builder_new (G_VARIANT_TYPE_TUPLE);
// Annotations for garbage collector.
// Since we're converting Scheme_Object values to GVariants, it should
// not be the case that we have an "allocating call". However, I am
// worried that conversion to a string, which requires
// scheme_char_string_to_byte_string_locale, might be considered an
// allocating call. So let's be in the safe side. The sample code suggests
// that we can put an array of GObjects in a single variable (see
// the supplied makeadder3m.c for more details).
MZ_GC_DECL_REG (1);
MZ_GC_VAR_IN_REG (0, objects);
MZ_GC_REG ();
// Process all the parameters
for (i = 0; i < arity; i++)
{
actual = scheme_object_to_parameter (objects[i], formals[i]->signature);
// If we can't convert the parameter, we give up.
if (actual == NULL)
{
// Early exit - Clean up for garbage collection
MZ_GC_UNREG ();
// Get rid of the builder
g_variant_builder_unref (builder);
// And return an arror message.
scheme_wrong_type (fun,
dbus_signature_to_string (formals[i]->signature),
i,
arity,
objects);
} // If we could not convert
// Otherwise, we add the value to the builder and go on
g_variant_builder_add_value (builder, actual);
} // for
// Clean up garbage collection info.
MZ_GC_UNREG ();
// And we're done.
result = g_variant_builder_end (builder);
return result;
} // scheme_objects_to_parameter_tuple
// +-----------------------+------------------------------------------
// | Other Local Functions |
// +-----------------------+
/**
* Add one of the procedures that the proxy provides on the D-Bus.
*/
static void
loudbus_add_dbus_proc (Scheme_Env *env,
Scheme_Object *proxy,
gchar *dbus_name,
gchar *external_name,
int arity)
{
Scheme_Object *vals[3];
Scheme_Object *proc;
vals[0] = NULL;
vals[1] = NULL;
vals[2] = NULL;
// Prepare for potential garbage collection during allocating calls
// (e.g., scheme_make_locale_string).
MZ_GC_DECL_REG (3);
MZ_GC_VAR_IN_REG (0, vals[0]);
MZ_GC_VAR_IN_REG (1, vals[1]);
MZ_GC_VAR_IN_REG (2, vals[2]);
MZ_GC_REG ();
// Fill in the closure with the object.
vals[0] = proxy;
vals[1] = scheme_make_locale_string (dbus_name);
vals[2] = scheme_make_locale_string (external_name);
// Build the procedure. Note that we need to duplicate the
// external name because scheme_make_prim_closure_w_arity seems
// to retain a pointer to the string. (At least, it seems that way
// to me.)
proc = scheme_make_prim_closure_w_arity (loudbus_call_with_closure,
3, vals,
g_strdup (external_name),
arity, arity);
// And add it to the environment.
scheme_add_global (external_name, proc, env);
// And update the GC info.
MZ_GC_UNREG ();
} // loudbus_add_dbus_proc
/**
* The kernel of the various mechanisms for calling D-Bus functions.
*/
static Scheme_Object *
dbus_call_kernel (LouDBusProxy *proxy,
gchar *dbus_name,
gchar *external_name,
int argc,
Scheme_Object **argv)
{
GDBusMethodInfo *method;
// Information on the actual method
int arity; // The arity of that method
GVariant *actuals; // The actual parameters
GVariant *gresult; // The result from the function call as a GVariant
Scheme_Object *sresult;
// That Scheme result as a Scheme object
GError *error; // Possible error from call
// Grab the method information.
method = g_dbus_interface_info_lookup_method (proxy->iinfo, dbus_name);
if (method == NULL)
{
scheme_signal_error ("no such method: %s", dbus_name);
} // if the method is invalid
// Get the arity
arity = g_dbus_method_info_num_formals (method);
if (arity != argc)
{
scheme_signal_error ("%s expected %d params, received %d",
external_name, arity, argc);
} // if the arity is incorrect
// Build the actuals
actuals = scheme_objects_to_parameter_tuple (external_name,
argc,
argv,
method->in_args);
if (actuals == NULL)
{
scheme_signal_error ("%s: could not convert parameters",
external_name);
} // if (actuals == NULL)
// Call the function.
error = NULL;
gresult = g_dbus_proxy_call_sync (proxy->proxy,
dbus_name,
actuals,
0,
-1,
NULL,
&error);
if (gresult == NULL)
{
if (error != NULL)
{
scheme_signal_error ("%s: call failed because %s",
external_name, error->message);
} // if (error != NULL)
else
{
scheme_signal_error ("%s: call failed for unknown reason",
external_name);
} // if something went wrong, but there's no error
} // if (gresult == NULL)
// Convert to Scheme form
sresult = g_variant_to_scheme_object (gresult);
if (sresult == NULL)
{
scheme_signal_error ("%s: could not convert return values",
external_name);
} // if (sresult == NULL)
// And we're done.
return sresult;
} // dbus_call_kernel
/**
* Get a count of the number of methods in an interface.
*/
int
g_dbus_interface_info_num_methods (GDBusInterfaceInfo *info)
{
int m; // Counter variable
// Special case: No methods
if (info->methods == NULL)
return 0;
// Normal case: Iterate until we find the NULL.
for (m = 0; info->methods[m] != NULL; m++)
;
// And we're done.
return m;
} // g_dbus_interface_info_num_methods
/**
* Get a count of the number of formal parameters to a method.
*/
static int
g_dbus_method_info_num_formals (GDBusMethodInfo *method)
{
int i; // Counter variable
// Special case?: No formals
if (method->in_args == NULL)
return 0;
// Normal case: Iterate until we find the NULL.