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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
|
/* valadbusclientmodule.vala
*
* Copyright (C) 2007-2009 Jürg Billeter
* Copyright (C) 2008 Philip Van Hoof
*
* 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
* Philip Van Hoof <pvanhoof@gnome.org>
*/
using GLib;
using Gee;
/**
* The link between a dynamic method and generated code.
*/
public class Vala.DBusClientModule : DBusModule {
int dynamic_property_id;
public DBusClientModule (CCodeGenerator codegen, CCodeModule? next) {
base (codegen, next);
}
string get_dynamic_dbus_name (string vala_name) {
// TODO switch default to no transformation as soon as we have static D-Bus client support
// keep transformation by default for static D-Bus client and server support
if (context.dbus_transformation) {
return Symbol.lower_case_to_camel_case (vala_name);
} else {
return vala_name;
}
}
public override void generate_dynamic_method_wrapper (DynamicMethod method) {
var dynamic_method = (DynamicMethod) method;
var func = new CCodeFunction (method.get_cname (), method.return_type.get_cname ());
var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
generate_cparameters (method, method.return_type, false, cparam_map, func);
var block = new CCodeBlock ();
if (dynamic_method.dynamic_type.data_type == dbus_object_type) {
generate_dbus_method_wrapper (method, block);
} else {
Report.error (method.source_reference, "dynamic methods are not supported for `%s'".printf (dynamic_method.dynamic_type.to_string ()));
}
// append to C source file
source_type_member_declaration.append (func.copy ());
func.block = block;
source_type_member_definition.append (func);
}
void generate_dbus_method_wrapper (Method method, CCodeBlock block) {
var dynamic_method = (DynamicMethod) method;
var expr = dynamic_method.invocation;
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_begin_call"));
ccall.add_argument (new CCodeIdentifier ("self"));
bool found_out = false;
Expression callback = null;
int callback_index = -1;
int arg_index = 1;
foreach (Expression arg in expr.get_argument_list ()) {
if (arg.symbol_reference is Method) {
// callback
if (callback != null) {
Report.error (expr.source_reference, "only one reply callback may be specified in invocation of DBus method");
expr.error = true;
return;
} else if (found_out) {
Report.error (expr.source_reference, "out argument and reply callback conflict in invocation of DBus method");
expr.error = true;
return;
}
callback = arg;
callback_index = arg_index;
} else if (arg is UnaryExpression && ((UnaryExpression) arg).operator == UnaryOperator.OUT) {
// out arg
if (callback != null) {
Report.error (expr.source_reference, "out argument and reply callback conflict in invocation of DBus method");
expr.error = true;
return;
}
found_out = true;
} else {
// in arg
if (callback != null || found_out) {
Report.error (expr.source_reference, "in argument must not follow out argument or reply callback in invocation of DBus method");
expr.error = true;
return;
}
}
arg_index++;
}
ccall.add_argument (new CCodeConstant ("\"%s\"".printf (get_dynamic_dbus_name (method.name))));
if (callback != null) {
var reply_method = (Method) callback.symbol_reference;
var cb_fun = new CCodeFunction ("_%s_cb".printf (reply_method.get_cname ()), "void");
cb_fun.modifiers = CCodeModifiers.STATIC;
cb_fun.add_parameter (new CCodeFormalParameter ("proxy", "DBusGProxy*"));
cb_fun.add_parameter (new CCodeFormalParameter ("call", "DBusGProxyCall*"));
cb_fun.add_parameter (new CCodeFormalParameter ("user_data", "void*"));
cb_fun.block = new CCodeBlock ();
var cerrdecl = new CCodeDeclaration ("GError*");
cerrdecl.add_declarator (new CCodeVariableDeclarator ("error", new CCodeConstant ("NULL")));
cb_fun.block.add_statement (cerrdecl);
var cend_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_end_call"));
cend_call.add_argument (new CCodeIdentifier ("proxy"));
cend_call.add_argument (new CCodeIdentifier ("call"));
cend_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("error")));
var creply_call = new CCodeFunctionCall ((CCodeExpression) callback.ccodenode);
creply_call.add_argument (new CCodeIdentifier ("user_data"));
int param_count = reply_method.get_parameters ().size;
int i = 0;
foreach (FormalParameter param in reply_method.get_parameters ()) {
if ((++i) == param_count) {
if (!(param.parameter_type is ErrorType)) {
Report.error (null, "DBus reply callbacks must end with GLib.Error argument");
return;
}
break;
}
if (param.parameter_type is ArrayType && ((ArrayType) param.parameter_type).element_type.data_type != string_type.data_type) {
var array_type = (ArrayType) param.parameter_type;
CCodeDeclaration cdecl;
if (dbus_use_ptr_array (array_type)) {
cdecl = new CCodeDeclaration ("GPtrArray*");
} else {
cdecl = new CCodeDeclaration ("GArray*");
}
cdecl.add_declarator (new CCodeVariableDeclarator (param.name));
cb_fun.block.add_statement (cdecl);
cend_call.add_argument (get_dbus_g_type (array_type));
cend_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (param.name)));
creply_call.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier (param.name), dbus_use_ptr_array (array_type) ? "pdata" : "data"));
creply_call.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier (param.name), "len"));
} else {
var cdecl = new CCodeDeclaration (param.parameter_type.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator (param.name));
cb_fun.block.add_statement (cdecl);
if (param.parameter_type is ArrayType && ((ArrayType) param.parameter_type).element_type.data_type == string_type.data_type) {
// special case string array
cend_call.add_argument (new CCodeIdentifier ("G_TYPE_STRV"));
} else {
cend_call.add_argument (get_dbus_g_type (param.parameter_type));
}
cend_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (param.name)));
creply_call.add_argument (new CCodeIdentifier (param.name));
if (param.parameter_type is ArrayType && ((ArrayType) param.parameter_type).element_type.data_type == string_type.data_type) {
var cstrvlen = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length"));
cstrvlen.add_argument (new CCodeIdentifier (param.name));
creply_call.add_argument (cstrvlen);
}
}
}
cend_call.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
cb_fun.block.add_statement (new CCodeExpressionStatement (cend_call));
creply_call.add_argument (new CCodeIdentifier ("error"));
cb_fun.block.add_statement (new CCodeExpressionStatement (creply_call));
source_type_member_definition.append (cb_fun);
ccall.add_argument (new CCodeIdentifier (cb_fun.name));
ccall.add_argument (new CCodeConstant ("param%d_target".printf (callback_index)));
ccall.add_argument (new CCodeConstant ("NULL"));
} else if (found_out || !(method.return_type is VoidType)) {
ccall.call = new CCodeIdentifier ("dbus_g_proxy_call");
ccall.add_argument (new CCodeIdentifier ("error"));
} else {
ccall.call = new CCodeIdentifier ("dbus_g_proxy_call_no_reply");
}
foreach (FormalParameter param in method.get_parameters ()) {
if (param.parameter_type is MethodType
|| param.parameter_type is DelegateType) {
// callback parameter
break;
}
if (param.direction != ParameterDirection.IN) {
continue;
}
var array_type = param.parameter_type as ArrayType;
if (array_type != null) {
// array parameter
if (array_type.element_type.data_type != string_type.data_type) {
// non-string arrays (use GArray)
ccall.add_argument (get_dbus_g_type (array_type));
var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
CCodeDeclaration cdecl;
CCodeFunctionCall array_construct;
if (dbus_use_ptr_array (array_type)) {
cdecl = new CCodeDeclaration ("GPtrArray*");
array_construct = new CCodeFunctionCall (new CCodeIdentifier ("g_ptr_array_sized_new"));
array_construct.add_argument (new CCodeIdentifier (head.get_array_length_cname (param.name, 1)));
} else {
cdecl = new CCodeDeclaration ("GArray*");
array_construct = new CCodeFunctionCall (new CCodeIdentifier ("g_array_new"));
array_construct.add_argument (new CCodeConstant ("TRUE"));
array_construct.add_argument (new CCodeConstant ("TRUE"));
array_construct.add_argument (sizeof_call);
}
cdecl.add_declarator (new CCodeVariableDeclarator ("dbus_%s".printf (param.name), array_construct));
block.add_statement (cdecl);
if (dbus_use_ptr_array (array_type)) {
var memcpy_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
memcpy_call.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("dbus_%s".printf (param.name)), "pdata"));
memcpy_call.add_argument (new CCodeIdentifier (param.name));
memcpy_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeIdentifier (head.get_array_length_cname (param.name, 1)), sizeof_call));
block.add_statement (new CCodeExpressionStatement (memcpy_call));
var len_assignment = new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("dbus_%s".printf (param.name)), "len"), new CCodeIdentifier (head.get_array_length_cname (param.name, 1)));
block.add_statement (new CCodeExpressionStatement (len_assignment));
} else {
var cappend_call = new CCodeFunctionCall (new CCodeIdentifier ("g_array_append_vals"));
cappend_call.add_argument (new CCodeIdentifier ("dbus_%s".printf (param.name)));
cappend_call.add_argument (new CCodeIdentifier (param.name));
cappend_call.add_argument (new CCodeIdentifier (head.get_array_length_cname (param.name, 1)));
block.add_statement (new CCodeExpressionStatement (cappend_call));
}
ccall.add_argument (new CCodeIdentifier ("dbus_%s".printf (param.name)));
} else {
// string arrays
ccall.add_argument (new CCodeIdentifier ("G_TYPE_STRV"));
ccall.add_argument (new CCodeIdentifier (param.name));
}
} else if (param.parameter_type.get_type_signature ().has_prefix ("(")) {
// struct parameter
var st = (Struct) param.parameter_type.data_type;
var array_construct = new CCodeFunctionCall (new CCodeIdentifier ("g_value_array_new"));
array_construct.add_argument (new CCodeConstant ("0"));
var cdecl = new CCodeDeclaration ("GValueArray*");
cdecl.add_declarator (new CCodeVariableDeclarator ("dbus_%s".printf (param.name), array_construct));
block.add_statement (cdecl);
var type_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_type_get_struct"));
type_call.add_argument (new CCodeConstant ("\"GValueArray\""));
foreach (Field f in st.get_fields ()) {
if (f.binding != MemberBinding.INSTANCE) {
continue;
}
string val_name = "val_%s_%s".printf (param.name, f.name);
// 0-initialize struct with struct initializer { 0 }
var cvalinit = new CCodeInitializerList ();
cvalinit.append (new CCodeConstant ("0"));
var cval_decl = new CCodeDeclaration ("GValue");
cval_decl.add_declarator (new CCodeVariableDeclarator (val_name, cvalinit));
block.add_statement (cval_decl);
var val_ptr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (val_name));
var cinit_call = new CCodeFunctionCall (new CCodeIdentifier ("g_value_init"));
cinit_call.add_argument (val_ptr);
cinit_call.add_argument (new CCodeIdentifier (f.field_type.data_type.get_type_id ()));
block.add_statement (new CCodeExpressionStatement (cinit_call));
var cset_call = new CCodeFunctionCall (new CCodeIdentifier (f.field_type.data_type.get_set_value_function ()));
cset_call.add_argument (val_ptr);
cset_call.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier (param.name), f.name));
block.add_statement (new CCodeExpressionStatement (cset_call));
var cappend_call = new CCodeFunctionCall (new CCodeIdentifier ("g_value_array_append"));
cappend_call.add_argument (new CCodeIdentifier ("dbus_%s".printf (param.name)));
cappend_call.add_argument (val_ptr);
block.add_statement (new CCodeExpressionStatement (cappend_call));
type_call.add_argument (new CCodeIdentifier (f.field_type.data_type.get_type_id ()));
}
type_call.add_argument (new CCodeConstant ("G_TYPE_INVALID"));
ccall.add_argument (type_call);
ccall.add_argument (new CCodeIdentifier ("dbus_%s".printf (param.name)));
} else {
ccall.add_argument (get_dbus_g_type (param.parameter_type));
ccall.add_argument (new CCodeIdentifier (param.name));
}
}
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
var out_marshalling_fragment = new CCodeFragment ();
foreach (FormalParameter param in method.get_parameters ()) {
if (param.parameter_type is MethodType) {
// callback parameter
break;
}
if (param.direction != ParameterDirection.OUT) {
continue;
}
if (param.parameter_type.get_type_signature ().has_prefix ("(")) {
// struct output parameter
var st = (Struct) param.parameter_type.data_type;
var cdecl = new CCodeDeclaration ("GValueArray*");
cdecl.add_declarator (new CCodeVariableDeclarator ("dbus_%s".printf (param.name)));
block.add_statement (cdecl);
var type_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_type_get_struct"));
type_call.add_argument (new CCodeConstant ("\"GValueArray\""));
int i = 0;
foreach (Field f in st.get_fields ()) {
if (f.binding != MemberBinding.INSTANCE) {
continue;
}
var cget_call = new CCodeFunctionCall (new CCodeIdentifier (f.field_type.data_type.get_get_value_function ()));
cget_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeElementAccess (new CCodeMemberAccess.pointer (new CCodeIdentifier ("dbus_%s".printf (param.name)), "values"), new CCodeConstant (i.to_string ()))));
var converted_value = cget_call;
if (requires_copy (f.field_type)) {
var dupexpr = get_dup_func_expression (f.field_type, expr.source_reference);
converted_value = new CCodeFunctionCall (dupexpr);
converted_value.add_argument (cget_call);
}
var assign = new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier (param.name), f.name), converted_value);
out_marshalling_fragment.append (new CCodeExpressionStatement (assign));
type_call.add_argument (new CCodeIdentifier (f.field_type.data_type.get_type_id ()));
i++;
}
type_call.add_argument (new CCodeConstant ("G_TYPE_INVALID"));
ccall.add_argument (type_call);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("dbus_%s".printf (param.name))));
} else {
ccall.add_argument (new CCodeIdentifier (param.parameter_type.data_type.get_type_id ()));
ccall.add_argument (new CCodeIdentifier (param.name));
}
}
if (!(method.return_type is VoidType)) {
// synchronous D-Bus method call with reply
var array_type = method.return_type as ArrayType;
if (array_type != null && array_type.element_type.data_type != string_type.data_type) {
// non-string arrays (use GArray)
ccall.add_argument (get_dbus_g_type (array_type));
CCodeDeclaration cdecl;
if (dbus_use_ptr_array (array_type)) {
cdecl = new CCodeDeclaration ("GPtrArray*");
} else {
cdecl = new CCodeDeclaration ("GArray*");
}
cdecl.add_declarator (new CCodeVariableDeclarator ("result"));
block.add_statement (cdecl);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("result")));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
block.add_statement (new CCodeExpressionStatement (ccall));
// don't access result when error occured
var creturnblock = new CCodeBlock ();
creturnblock.add_statement (new CCodeReturnStatement (default_value_for_type (method.return_type, false)));
var cerrorif = new CCodeIfStatement (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("error")), creturnblock);
block.add_statement (cerrorif);
block.add_statement (out_marshalling_fragment);
// *result_length1 = result->len;
var garray_length = new CCodeMemberAccess.pointer (new CCodeIdentifier ("result"), "len");
var result_length = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("result_length1"));
var assign = new CCodeAssignment (result_length, garray_length);
block.add_statement (new CCodeExpressionStatement (assign));
// return result->data;
block.add_statement (new CCodeReturnStatement (new CCodeCastExpression (new CCodeMemberAccess.pointer (new CCodeIdentifier ("result"), dbus_use_ptr_array (array_type) ? "pdata" : "data"), method.return_type.get_cname ())));
} else {
// string arrays or other datatypes
ccall.add_argument (get_dbus_g_type (method.return_type));
var cdecl = new CCodeDeclaration (method.return_type.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator ("result"));
block.add_statement (cdecl);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("result")));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
block.add_statement (new CCodeExpressionStatement (ccall));
// don't access result when error occured
var creturnblock = new CCodeBlock ();
creturnblock.add_statement (new CCodeReturnStatement (default_value_for_type (method.return_type, false)));
var cerrorif = new CCodeIfStatement (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("error")), creturnblock);
block.add_statement (cerrorif);
block.add_statement (out_marshalling_fragment);
if (array_type != null) {
// special case string array
// *result_length1 = g_strv_length (result);
var cstrvlen = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length"));
cstrvlen.add_argument (new CCodeIdentifier ("result"));
var result_length = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("result_length1"));
var assign = new CCodeAssignment (result_length, cstrvlen);
block.add_statement (new CCodeExpressionStatement (assign));
}
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("result")));
}
} else {
if (found_out) {
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
}
block.add_statement (new CCodeExpressionStatement (ccall));
// don't access result when error occured
var creturnblock = new CCodeBlock ();
creturnblock.add_statement (new CCodeReturnStatement ());
var cerrorif = new CCodeIfStatement (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("error")), creturnblock);
block.add_statement (cerrorif);
block.add_statement (out_marshalling_fragment);
}
}
public override CCodeExpression get_dbus_g_type (DataType data_type) {
if (data_type is ArrayType) {
var array_type = data_type as ArrayType;
if (array_type.element_type.data_type == string_type.data_type) {
return new CCodeIdentifier ("G_TYPE_STRV");
}
var carray_type = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_type_get_collection"));
if (dbus_use_ptr_array (array_type)) {
carray_type.add_argument (new CCodeConstant ("\"GPtrArray\""));
} else {
carray_type.add_argument (new CCodeConstant ("\"GArray\""));
}
carray_type.add_argument (get_dbus_g_type (array_type.element_type));
return carray_type;
} else if (data_type.data_type is Enum) {
var en = (Enum) data_type.data_type;
if (en.is_flags) {
return new CCodeIdentifier ("G_TYPE_UINT");
} else {
return new CCodeIdentifier ("G_TYPE_INT");
}
} else if (data_type.data_type == null) {
critical ("Internal error during DBus type generation with: %s", data_type.to_string ());
return new CCodeIdentifier ("G_TYPE_NONE");
} else if (data_type.data_type.get_full_name () == "GLib.HashTable") {
var cmap_type = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_type_get_map"));
var type_args = data_type.get_type_arguments ();
cmap_type.add_argument (new CCodeConstant ("\"GHashTable\""));
foreach (DataType type_arg in type_args) {
cmap_type.add_argument (get_dbus_g_type (type_arg));
}
return cmap_type;
} else if (data_type.data_type.get_type_signature ().has_prefix ("(")) {
// struct parameter
var st = (Struct) data_type.data_type;
var type_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_type_get_struct"));
type_call.add_argument (new CCodeConstant ("\"GValueArray\""));
foreach (Field f in st.get_fields ()) {
if (f.binding != MemberBinding.INSTANCE) {
continue;
}
type_call.add_argument (get_dbus_g_type (f.field_type));
}
type_call.add_argument (new CCodeConstant ("G_TYPE_INVALID"));
return type_call;
} else {
return new CCodeIdentifier (data_type.data_type.get_type_id ());
}
}
public bool dbus_use_ptr_array (ArrayType array_type) {
if (array_type.element_type.data_type == string_type.data_type) {
// use char**
return false;
} else if (array_type.element_type.data_type == bool_type.data_type
|| array_type.element_type.data_type == char_type.data_type
|| array_type.element_type.data_type == uchar_type.data_type
|| array_type.element_type.data_type == int_type.data_type
|| array_type.element_type.data_type == uint_type.data_type
|| array_type.element_type.data_type == long_type.data_type
|| array_type.element_type.data_type == ulong_type.data_type
|| array_type.element_type.data_type == int8_type.data_type
|| array_type.element_type.data_type == uint8_type.data_type
|| array_type.element_type.data_type == int32_type.data_type
|| array_type.element_type.data_type == uint32_type.data_type
|| array_type.element_type.data_type == int64_type.data_type
|| array_type.element_type.data_type == uint64_type.data_type
|| array_type.element_type.data_type == double_type.data_type) {
// use GArray
return false;
} else {
// use GPtrArray
return true;
}
}
public override string get_dynamic_property_getter_cname (DynamicProperty prop) {
if (prop.dynamic_type.data_type != dbus_object_type) {
return base.get_dynamic_property_getter_cname (prop);
}
string getter_cname = "_dynamic_get_%s%d".printf (prop.name, dynamic_property_id++);
var func = new CCodeFunction (getter_cname, prop.property_type.get_cname ());
func.modifiers |= CCodeModifiers.STATIC | CCodeModifiers.INLINE;
func.add_parameter (new CCodeFormalParameter ("obj", prop.dynamic_type.get_cname ()));
var block = new CCodeBlock ();
generate_dbus_property_getter_wrapper (prop, block);
// append to C source file
source_type_member_declaration.append (func.copy ());
func.block = block;
source_type_member_definition.append (func);
return getter_cname;
}
public override string get_dynamic_property_setter_cname (DynamicProperty prop) {
if (prop.dynamic_type.data_type != dbus_object_type) {
return base.get_dynamic_property_setter_cname (prop);
}
string setter_cname = "_dynamic_set_%s%d".printf (prop.name, dynamic_property_id++);
var func = new CCodeFunction (setter_cname, "void");
func.modifiers |= CCodeModifiers.STATIC | CCodeModifiers.INLINE;
func.add_parameter (new CCodeFormalParameter ("obj", prop.dynamic_type.get_cname ()));
func.add_parameter (new CCodeFormalParameter ("value", prop.property_type.get_cname ()));
var block = new CCodeBlock ();
generate_dbus_property_setter_wrapper (prop, block);
// append to C source file
source_type_member_declaration.append (func.copy ());
func.block = block;
source_type_member_definition.append (func);
return setter_cname;
}
void create_dbus_property_proxy (DynamicProperty node, CCodeBlock block) {
var prop_proxy_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_new_from_proxy"));
prop_proxy_call.add_argument (new CCodeIdentifier ("obj"));
prop_proxy_call.add_argument (new CCodeConstant ("DBUS_INTERFACE_PROPERTIES"));
prop_proxy_call.add_argument (new CCodeConstant ("NULL"));
var prop_proxy_decl = new CCodeDeclaration ("DBusGProxy*");
prop_proxy_decl.add_declarator (new CCodeVariableDeclarator ("property_proxy", prop_proxy_call));
block.add_statement (prop_proxy_decl);
}
void generate_dbus_property_getter_wrapper (DynamicProperty node, CCodeBlock block) {
create_dbus_property_proxy (node, block);
// initialize GValue
var cvalinit = new CCodeInitializerList ();
cvalinit.append (new CCodeConstant ("0"));
var cval_decl = new CCodeDeclaration ("GValue");
cval_decl.add_declarator (new CCodeVariableDeclarator ("gvalue", cvalinit));
block.add_statement (cval_decl);
var val_ptr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("gvalue"));
// call Get method on property proxy
var cdecl = new CCodeDeclaration (node.property_type.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator ("result"));
block.add_statement (cdecl);
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_call"));
ccall.add_argument (new CCodeIdentifier ("property_proxy"));
ccall.add_argument (new CCodeConstant ("\"Get\""));
ccall.add_argument (new CCodeConstant ("NULL"));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_STRING"));
var get_iface = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_get_interface"));
get_iface.add_argument (new CCodeIdentifier ("obj"));
ccall.add_argument (get_iface);
ccall.add_argument (new CCodeIdentifier ("G_TYPE_STRING"));
ccall.add_argument (new CCodeConstant ("\"%s\"".printf (get_dynamic_dbus_name (node.name))));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_VALUE"));
ccall.add_argument (val_ptr);
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
block.add_statement (new CCodeExpressionStatement (ccall));
// unref property proxy
var prop_proxy_unref = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
prop_proxy_unref.add_argument (new CCodeIdentifier ("property_proxy"));
block.add_statement (new CCodeExpressionStatement (prop_proxy_unref));
// assign value to result variable
var cget_call = new CCodeFunctionCall (new CCodeIdentifier (node.property_type.data_type.get_get_value_function ()));
cget_call.add_argument (val_ptr);
var assign = new CCodeAssignment (new CCodeIdentifier ("result"), cget_call);
block.add_statement (new CCodeExpressionStatement (assign));
// return result
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("result")));
}
void generate_dbus_property_setter_wrapper (DynamicProperty node, CCodeBlock block) {
create_dbus_property_proxy (node, block);
// initialize GValue
var cvalinit = new CCodeInitializerList ();
cvalinit.append (new CCodeConstant ("0"));
var cval_decl = new CCodeDeclaration ("GValue");
cval_decl.add_declarator (new CCodeVariableDeclarator ("gvalue", cvalinit));
block.add_statement (cval_decl);
var val_ptr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("gvalue"));
var cinit_call = new CCodeFunctionCall (new CCodeIdentifier ("g_value_init"));
cinit_call.add_argument (val_ptr);
cinit_call.add_argument (new CCodeIdentifier (node.property_type.data_type.get_type_id ()));
block.add_statement (new CCodeExpressionStatement (cinit_call));
var cset_call = new CCodeFunctionCall (new CCodeIdentifier (node.property_type.data_type.get_set_value_function ()));
cset_call.add_argument (val_ptr);
cset_call.add_argument (new CCodeIdentifier ("value"));
block.add_statement (new CCodeExpressionStatement (cset_call));
// call Set method on property proxy
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_call"));
ccall.add_argument (new CCodeIdentifier ("property_proxy"));
ccall.add_argument (new CCodeConstant ("\"Set\""));
ccall.add_argument (new CCodeConstant ("NULL"));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_STRING"));
var get_iface = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_get_interface"));
get_iface.add_argument (new CCodeIdentifier ("obj"));
ccall.add_argument (get_iface);
ccall.add_argument (new CCodeIdentifier ("G_TYPE_STRING"));
ccall.add_argument (new CCodeConstant ("\"%s\"".printf (get_dynamic_dbus_name (node.name))));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_VALUE"));
ccall.add_argument (val_ptr);
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
ccall.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
block.add_statement (new CCodeExpressionStatement (ccall));
// unref property proxy
var prop_proxy_unref = new CCodeFunctionCall (new CCodeIdentifier ("g_object_unref"));
prop_proxy_unref.add_argument (new CCodeIdentifier ("property_proxy"));
block.add_statement (new CCodeExpressionStatement (prop_proxy_unref));
}
public override string get_dynamic_signal_connect_wrapper_name (DynamicSignal sig) {
if (sig.dynamic_type.data_type != dbus_object_type) {
return base.get_dynamic_signal_connect_wrapper_name (sig);
}
string connect_wrapper_name = "_%sconnect".printf (get_dynamic_signal_cname (sig));
var func = new CCodeFunction (connect_wrapper_name, "void");
func.add_parameter (new CCodeFormalParameter ("obj", "gpointer"));
func.add_parameter (new CCodeFormalParameter ("signal_name", "const char *"));
func.add_parameter (new CCodeFormalParameter ("handler", "GCallback"));
func.add_parameter (new CCodeFormalParameter ("data", "gpointer"));
var block = new CCodeBlock ();
generate_dbus_connect_wrapper (sig, block);
// append to C source file
source_type_member_declaration.append (func.copy ());
func.block = block;
source_type_member_definition.append (func);
return connect_wrapper_name;
}
public override string get_dynamic_signal_disconnect_wrapper_name (DynamicSignal sig) {
if (sig.dynamic_type.data_type != dbus_object_type) {
return base.get_dynamic_signal_disconnect_wrapper_name (sig);
}
string disconnect_wrapper_name = "_%sdisconnect".printf (get_dynamic_signal_cname (sig));
var func = new CCodeFunction (disconnect_wrapper_name, "void");
func.add_parameter (new CCodeFormalParameter ("obj", "gpointer"));
func.add_parameter (new CCodeFormalParameter ("signal_name", "const char *"));
func.add_parameter (new CCodeFormalParameter ("handler", "GCallback"));
func.add_parameter (new CCodeFormalParameter ("data", "gpointer"));
var block = new CCodeBlock ();
generate_dbus_disconnect_wrapper (sig, block);
// append to C source file
source_type_member_declaration.append (func.copy ());
func.block = block;
source_type_member_definition.append (func);
return disconnect_wrapper_name;
}
void generate_dbus_connect_wrapper (DynamicSignal sig, CCodeBlock block) {
var m = (Method) sig.handler.symbol_reference;
sig.accept (codegen);
// FIXME should only be done once per marshaller
var register_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_object_register_marshaller"));
head.generate_marshaller (sig.get_parameters (), sig.return_type, true);
register_call.add_argument (new CCodeIdentifier (head.get_marshaller_function (sig.get_parameters (), sig.return_type, null, true)));
register_call.add_argument (new CCodeIdentifier ("G_TYPE_NONE"));
var add_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_add_signal"));
add_call.add_argument (new CCodeIdentifier ("obj"));
add_call.add_argument (new CCodeConstant ("\"%s\"".printf (get_dynamic_dbus_name (sig.name))));
bool first = true;
foreach (FormalParameter param in m.get_parameters ()) {
if (first) {
// skip sender parameter
first = false;
continue;
}
var array_type = param.parameter_type as ArrayType;
if (array_type != null) {
if (array_type.element_type.data_type == string_type.data_type) {
register_call.add_argument (new CCodeIdentifier ("G_TYPE_STRV"));
add_call.add_argument (new CCodeIdentifier ("G_TYPE_STRV"));
} else {
if (array_type.element_type.data_type.get_type_id () == null) {
Report.error (param.source_reference, "unsupported parameter type for D-Bus signals");
return;
}
var carray_type = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_type_get_collection"));
carray_type.add_argument (new CCodeConstant ("\"GArray\""));
carray_type.add_argument (new CCodeIdentifier (array_type.element_type.data_type.get_type_id ()));
register_call.add_argument (carray_type);
add_call.add_argument (carray_type);
}
} else {
if (param.parameter_type.get_type_id () == null) {
Report.error (param.source_reference, "unsupported parameter type for D-Bus signals");
return;
}
register_call.add_argument (new CCodeIdentifier (param.parameter_type.get_type_id ()));
add_call.add_argument (new CCodeIdentifier (param.parameter_type.get_type_id ()));
}
}
register_call.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
add_call.add_argument (new CCodeIdentifier ("G_TYPE_INVALID"));
block.add_statement (new CCodeExpressionStatement (register_call));
block.add_statement (new CCodeExpressionStatement (add_call));
var call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_connect_signal"));
call.add_argument (new CCodeIdentifier ("obj"));
call.add_argument (new CCodeIdentifier ("signal_name"));
call.add_argument (new CCodeIdentifier ("handler"));
call.add_argument (new CCodeIdentifier ("data"));
call.add_argument (new CCodeConstant ("NULL"));
block.add_statement (new CCodeExpressionStatement (call));
}
void generate_dbus_disconnect_wrapper (DynamicSignal sig, CCodeBlock block) {
var call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_disconnect_signal"));
call.add_argument (new CCodeIdentifier ("obj"));
call.add_argument (new CCodeIdentifier ("signal_name"));
call.add_argument (new CCodeIdentifier ("handler"));
call.add_argument (new CCodeIdentifier ("data"));
block.add_statement (new CCodeExpressionStatement (call));
}
public override void visit_cast_expression (CastExpression expr) {
// handles casting DBus.Object instances to DBus interfaces
var type = expr.type_reference as ObjectType;
var call = expr.inner as MethodCall;
if (type == null || !(type.type_symbol is Interface)
|| type.type_symbol.get_attribute ("DBus") == null || call == null) {
base.visit_cast_expression (expr);
return;
}
var mtype = call.call.value_type as MethodType;
if (mtype == null || mtype.method_symbol.get_cname () != "dbus_g_proxy_new_for_name") {
base.visit_cast_expression (expr);
return;
}
var args = call.get_argument_list ();
Expression connection = ((MemberAccess) call.call).inner;
Expression bus_name = args.get (0);
Expression object_path = args.get (1);
var ccall = new CCodeFunctionCall (new CCodeIdentifier (type.type_symbol.get_lower_case_cprefix () + "dbus_proxy_new"));
connection.accept (codegen);
ccall.add_argument ((CCodeExpression) connection.ccodenode);
bus_name.accept (codegen);
ccall.add_argument ((CCodeExpression) bus_name.ccodenode);
object_path.accept (codegen);
ccall.add_argument ((CCodeExpression) object_path.ccodenode);
expr.ccodenode = ccall;
}
public override void visit_interface (Interface iface) {
base.visit_interface (iface);
var dbus = iface.get_attribute ("DBus");
if (dbus == null) {
return;
}
string dbus_iface_name = dbus.get_string ("name");
if (dbus_iface_name == null) {
return;
}
// create proxy class
string cname = iface.get_cname () + "DBusProxy";
string lower_cname = iface.get_lower_case_cprefix () + "dbus_proxy";
CCodeFragment decl_frag;
CCodeFragment def_frag;
CCodeFragment member_decl_frag;
if (iface.access != SymbolAccessibility.PRIVATE) {
decl_frag = header_type_declaration;
def_frag = header_type_definition;
member_decl_frag = header_type_member_declaration;
dbus_glib_h_needed_in_header = true;
} else {
decl_frag = source_type_declaration;
def_frag = source_type_definition;
member_decl_frag = source_type_member_declaration;
dbus_glib_h_needed = true;
}
source_type_declaration.append (new CCodeTypeDefinition ("DBusGProxy", new CCodeVariableDeclarator (cname)));
source_type_declaration.append (new CCodeTypeDefinition ("DBusGProxyClass", new CCodeVariableDeclarator (cname + "Class")));
var implement = new CCodeFunctionCall (new CCodeIdentifier ("G_IMPLEMENT_INTERFACE"));
implement.add_argument (new CCodeIdentifier (iface.get_upper_case_cname ("TYPE_")));
implement.add_argument (new CCodeIdentifier (lower_cname + "_interface_init"));
var define_type = new CCodeFunctionCall (new CCodeIdentifier ("G_DEFINE_TYPE_EXTENDED"));
define_type.add_argument (new CCodeIdentifier (cname));
define_type.add_argument (new CCodeIdentifier (lower_cname));
define_type.add_argument (new CCodeIdentifier ("DBUS_TYPE_G_PROXY"));
define_type.add_argument (new CCodeConstant ("0"));
define_type.add_argument (implement);
source_type_member_definition.append (new CCodeExpressionStatement (define_type));
var proxy_new = new CCodeFunction (lower_cname + "_new", iface.get_cname () + "*");
proxy_new.add_parameter (new CCodeFormalParameter ("connection", "DBusGConnection*"));
proxy_new.add_parameter (new CCodeFormalParameter ("name", "const char*"));
proxy_new.add_parameter (new CCodeFormalParameter ("path", "const char*"));
var new_block = new CCodeBlock ();
var new_call = new CCodeFunctionCall (new CCodeIdentifier ("g_object_new"));
new_call.add_argument (new CCodeFunctionCall (new CCodeIdentifier (lower_cname + "_get_type")));
new_call.add_argument (new CCodeConstant ("\"connection\""));
new_call.add_argument (new CCodeIdentifier ("connection"));
new_call.add_argument (new CCodeConstant ("\"name\""));
new_call.add_argument (new CCodeIdentifier ("name"));
new_call.add_argument (new CCodeConstant ("\"path\""));
new_call.add_argument (new CCodeIdentifier ("path"));
new_call.add_argument (new CCodeConstant ("\"interface\""));
new_call.add_argument (new CCodeConstant ("\"%s\"".printf (dbus_iface_name)));
new_call.add_argument (new CCodeConstant ("NULL"));
new_block.add_statement (new CCodeReturnStatement (new_call));
member_decl_frag.append (proxy_new.copy ());
proxy_new.block = new_block;
source_type_member_definition.append (proxy_new);
var proxy_class_init = new CCodeFunction (lower_cname + "_class_init", "void");
proxy_class_init.add_parameter (new CCodeFormalParameter ("klass", cname + "Class*"));
proxy_class_init.modifiers = CCodeModifiers.STATIC;
proxy_class_init.block = new CCodeBlock ();
source_type_member_definition.append (proxy_class_init);
var proxy_instance_init = new CCodeFunction (lower_cname + "_init", "void");
proxy_instance_init.add_parameter (new CCodeFormalParameter ("self", cname + "*"));
proxy_instance_init.modifiers = CCodeModifiers.STATIC;
proxy_instance_init.block = new CCodeBlock ();
source_type_member_definition.append (proxy_instance_init);
var proxy_iface_init = new CCodeFunction (lower_cname + "_interface_init", "void");
proxy_iface_init.add_parameter (new CCodeFormalParameter ("iface", iface.get_cname () + "Iface*"));
var iface_block = new CCodeBlock ();
foreach (Method m in iface.get_methods ()) {
var vfunc_entry = new CCodeMemberAccess.pointer (new CCodeIdentifier ("iface"), m.vfunc_name);
iface_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (vfunc_entry, new CCodeIdentifier (generate_dbus_proxy_method (iface, m)))));
if (m.coroutine) {
vfunc_entry = new CCodeMemberAccess.pointer (new CCodeIdentifier ("iface"), m.vfunc_name + "_async");
iface_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (vfunc_entry, new CCodeIdentifier (generate_async_dbus_proxy_method (iface, m)))));
vfunc_entry = new CCodeMemberAccess.pointer (new CCodeIdentifier ("iface"), m.vfunc_name + "_finish");
iface_block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (vfunc_entry, new CCodeIdentifier (generate_finish_dbus_proxy_method (iface, m)))));
}
}
proxy_iface_init.modifiers = CCodeModifiers.STATIC;
source_type_member_declaration.append (proxy_iface_init.copy ());
proxy_iface_init.block = iface_block;
source_type_member_definition.append (proxy_iface_init);
}
void generate_marshalling (Method m, string dbus_iface_name, CCodeFragment prefragment, CCodeFragment postfragment) {
CCodeDeclaration cdecl;
var destination = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_get_bus_name"));
destination.add_argument (new CCodeCastExpression (new CCodeIdentifier ("self"), "DBusGProxy*"));
var path = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_proxy_get_path"));
path.add_argument (new CCodeCastExpression (new CCodeIdentifier ("self"), "DBusGProxy*"));
var msgcall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_new_method_call"));
msgcall.add_argument (destination);
msgcall.add_argument (path);
msgcall.add_argument (new CCodeConstant ("\"%s\"".printf (dbus_iface_name)));
msgcall.add_argument (new CCodeConstant ("\"%s\"".printf (Symbol.lower_case_to_camel_case (m.name))));
prefragment.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("_message"), msgcall)));
var iter_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_iter_init_append"));
iter_call.add_argument (new CCodeIdentifier ("_message"));
iter_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_iter")));
prefragment.append (new CCodeExpressionStatement (iter_call));
iter_call = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_iter_init"));
iter_call.add_argument (new CCodeIdentifier ("_reply"));
iter_call.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_iter")));
postfragment.append (new CCodeExpressionStatement (iter_call));
foreach (FormalParameter param in m.get_parameters ()) {
if (param.direction == ParameterDirection.IN) {
CCodeExpression expr = new CCodeIdentifier (param.name);
if (param.parameter_type.is_real_struct_type ()) {
expr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, expr);
}
write_expression (prefragment, param.parameter_type, new CCodeIdentifier ("_iter"), expr);
} else {
cdecl = new CCodeDeclaration (param.parameter_type.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator ("_" + param.name));
postfragment.append (cdecl);
var array_type = param.parameter_type as ArrayType;
if (array_type != null) {
for (int dim = 1; dim <= array_type.rank; dim++) {
cdecl = new CCodeDeclaration ("int");
cdecl.add_declarator (new CCodeVariableDeclarator ("_%s_length%d".printf (param.name, dim), new CCodeConstant ("0")));
postfragment.append (cdecl);
}
}
var target = new CCodeIdentifier ("_" + param.name);
var expr = read_expression (postfragment, param.parameter_type, new CCodeIdentifier ("_iter"), target);
postfragment.append (new CCodeExpressionStatement (new CCodeAssignment (target, expr)));
// TODO check that parameter is not NULL (out parameters are optional)
// free value if parameter is NULL
postfragment.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (param.name)), target)));
if (array_type != null) {
for (int dim = 1; dim <= array_type.rank; dim++) {
// TODO check that parameter is not NULL (out parameters are optional)
postfragment.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("%s_length%d".printf (param.name, dim))), new CCodeIdentifier ("_%s_length%d".printf (param.name, dim)))));
}
}
}
}
if (!(m.return_type is VoidType)) {
cdecl = new CCodeDeclaration (m.return_type.get_cname ());
cdecl.add_declarator (new CCodeVariableDeclarator ("_result"));
postfragment.append (cdecl);
var array_type = m.return_type as ArrayType;
if (array_type != null) {
for (int dim = 1; dim <= array_type.rank; dim++) {
cdecl = new CCodeDeclaration ("int");
cdecl.add_declarator (new CCodeVariableDeclarator ("_result_length%d".printf (dim), new CCodeConstant ("0")));
postfragment.append (cdecl);
}
}
var target = new CCodeIdentifier ("_result");
var expr = read_expression (postfragment, m.return_type, new CCodeIdentifier ("_iter"), target);
postfragment.append (new CCodeExpressionStatement (new CCodeAssignment (target, expr)));
if (array_type != null) {
for (int dim = 1; dim <= array_type.rank; dim++) {
// TODO check that parameter is not NULL (out parameters are optional)
postfragment.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("result_length%d".printf (dim))), new CCodeIdentifier ("_result_length%d".printf (dim)))));
}
}
}
}
string generate_dbus_proxy_method (Interface iface, Method m) {
string proxy_name = "%sdbus_proxy_%s".printf (iface.get_lower_case_cprefix (), m.name);
string dbus_iface_name = iface.get_attribute ("DBus").get_string ("name");
CCodeDeclaration cdecl;
var function = new CCodeFunction (proxy_name, m.return_type.get_cname ());
function.modifiers = CCodeModifiers.STATIC;
var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
generate_cparameters (m, m.return_type, false, cparam_map, function);
var block = new CCodeBlock ();
var prefragment = new CCodeFragment ();
var postfragment = new CCodeFragment ();
cdecl = new CCodeDeclaration ("DBusGConnection");
cdecl.add_declarator (new CCodeVariableDeclarator ("*_connection"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusMessage");
cdecl.add_declarator (new CCodeVariableDeclarator ("*_message"));
cdecl.add_declarator (new CCodeVariableDeclarator ("*_reply"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusMessageIter");
cdecl.add_declarator (new CCodeVariableDeclarator ("_iter"));
block.add_statement (cdecl);
block.add_statement (prefragment);
generate_marshalling (m, dbus_iface_name, prefragment, postfragment);
var gconnection = new CCodeFunctionCall (new CCodeIdentifier ("g_object_get"));
gconnection.add_argument (new CCodeIdentifier ("self"));
gconnection.add_argument (new CCodeConstant ("\"connection\""));
gconnection.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_connection")));
gconnection.add_argument (new CCodeConstant ("NULL"));
block.add_statement (new CCodeExpressionStatement (gconnection));
var connection = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_connection_get_connection"));
connection.add_argument (new CCodeIdentifier ("_connection"));
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_connection_send_with_reply_and_block"));
ccall.add_argument (connection);
ccall.add_argument (new CCodeIdentifier ("_message"));
ccall.add_argument (new CCodeConstant ("-1"));
ccall.add_argument (new CCodeConstant ("NULL"));
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("_reply"), ccall)));
var conn_unref = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_connection_unref"));
conn_unref.add_argument (new CCodeIdentifier ("_connection"));
block.add_statement (new CCodeExpressionStatement (conn_unref));
var message_unref = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_unref"));
message_unref.add_argument (new CCodeIdentifier ("_message"));
block.add_statement (new CCodeExpressionStatement (message_unref));
block.add_statement (postfragment);
var reply_unref = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_unref"));
reply_unref.add_argument (new CCodeIdentifier ("_reply"));
block.add_statement (new CCodeExpressionStatement (reply_unref));
if (!(m.return_type is VoidType)) {
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("_result")));
}
source_type_member_declaration.append (function.copy ());
function.block = block;
source_type_member_definition.append (function);
return proxy_name;
}
string generate_async_dbus_proxy_method (Interface iface, Method m) {
string proxy_name = "%sdbus_proxy_%s_async".printf (iface.get_lower_case_cprefix (), m.name);
string dbus_iface_name = iface.get_attribute ("DBus").get_string ("name");
CCodeDeclaration cdecl;
// generate data struct
string dataname = "%sDBusProxy%sData".printf (iface.get_cname (), Symbol.lower_case_to_camel_case (m.name));
var datastruct = new CCodeStruct ("_" + dataname);
datastruct.add_field ("GAsyncReadyCallback", "callback");
datastruct.add_field ("gpointer", "user_data");
datastruct.add_field ("DBusPendingCall*", "pending");
source_type_definition.append (datastruct);
source_type_declaration.append (new CCodeTypeDefinition ("struct _" + dataname, new CCodeVariableDeclarator (dataname)));
// generate async function
var function = new CCodeFunction (proxy_name, "void");
function.modifiers = CCodeModifiers.STATIC;
var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
cparam_map.set (get_param_pos (-1), new CCodeFormalParameter ("callback", "GAsyncReadyCallback"));
cparam_map.set (get_param_pos (-0.9), new CCodeFormalParameter ("user_data", "gpointer"));
generate_cparameters (m, m.return_type, false, cparam_map, function, null, null, null, 1);
var block = new CCodeBlock ();
var prefragment = new CCodeFragment ();
var postfragment = new CCodeFragment ();
cdecl = new CCodeDeclaration ("DBusGConnection");
cdecl.add_declarator (new CCodeVariableDeclarator ("*_connection"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusMessage");
cdecl.add_declarator (new CCodeVariableDeclarator ("*_message"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusPendingCall");
cdecl.add_declarator (new CCodeVariableDeclarator ("*_pending"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusMessageIter");
cdecl.add_declarator (new CCodeVariableDeclarator ("_iter"));
block.add_statement (cdecl);
block.add_statement (prefragment);
generate_marshalling (m, dbus_iface_name, prefragment, postfragment);
var gconnection = new CCodeFunctionCall (new CCodeIdentifier ("g_object_get"));
gconnection.add_argument (new CCodeIdentifier ("self"));
gconnection.add_argument (new CCodeConstant ("\"connection\""));
gconnection.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_connection")));
gconnection.add_argument (new CCodeConstant ("NULL"));
block.add_statement (new CCodeExpressionStatement (gconnection));
var connection = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_connection_get_connection"));
connection.add_argument (new CCodeIdentifier ("_connection"));
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_connection_send_with_reply"));
ccall.add_argument (connection);
ccall.add_argument (new CCodeIdentifier ("_message"));
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("_pending")));
ccall.add_argument (new CCodeConstant ("-1"));
block.add_statement (new CCodeExpressionStatement (ccall));
var conn_unref = new CCodeFunctionCall (new CCodeIdentifier ("dbus_g_connection_unref"));
conn_unref.add_argument (new CCodeIdentifier ("_connection"));
block.add_statement (new CCodeExpressionStatement (conn_unref));
var message_unref = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_unref"));
message_unref.add_argument (new CCodeIdentifier ("_message"));
block.add_statement (new CCodeExpressionStatement (message_unref));
var dataalloc = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
dataalloc.add_argument (new CCodeIdentifier (dataname));
var datadecl = new CCodeDeclaration (dataname + "*");
datadecl.add_declarator (new CCodeVariableDeclarator ("data"));
block.add_statement (datadecl);
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("data"), dataalloc)));
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "callback"), new CCodeIdentifier ("callback"))));
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "user_data"), new CCodeIdentifier ("user_data"))));
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "pending"), new CCodeIdentifier ("_pending"))));
var pending = new CCodeFunctionCall (new CCodeIdentifier ("dbus_pending_call_set_notify"));
pending.add_argument (new CCodeIdentifier ("_pending"));
pending.add_argument (new CCodeIdentifier ("%sdbus_proxy_%s_ready".printf (iface.get_lower_case_cprefix (), m.name)));
pending.add_argument (new CCodeIdentifier ("data"));
pending.add_argument (new CCodeConstant ("NULL"));
block.add_statement (new CCodeExpressionStatement (pending));
source_type_member_declaration.append (function.copy ());
function.block = block;
source_type_member_definition.append (function);
// generate ready function
function = new CCodeFunction ("%sdbus_proxy_%s_ready".printf (iface.get_lower_case_cprefix (), m.name), "void");
function.modifiers = CCodeModifiers.STATIC;
function.add_parameter (new CCodeFormalParameter ("pending", "DBusPendingCall*"));
function.add_parameter (new CCodeFormalParameter ("user_data", "void*"));
block = new CCodeBlock ();
datadecl = new CCodeDeclaration (dataname + "*");
datadecl.add_declarator (new CCodeVariableDeclarator ("data"));
block.add_statement (datadecl);
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("data"), new CCodeIdentifier ("user_data"))));
// complete async call by invoking callback
var object_creation = new CCodeFunctionCall (new CCodeIdentifier ("g_object_newv"));
object_creation.add_argument (new CCodeConstant ("G_TYPE_OBJECT"));
object_creation.add_argument (new CCodeConstant ("0"));
object_creation.add_argument (new CCodeConstant ("NULL"));
var async_result_creation = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_new"));
async_result_creation.add_argument (object_creation);
async_result_creation.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "callback"));
async_result_creation.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "user_data"));
async_result_creation.add_argument (new CCodeIdentifier ("data"));
var completecall = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_complete"));
completecall.add_argument (async_result_creation);
block.add_statement (new CCodeExpressionStatement (completecall));
source_type_member_declaration.append (function.copy ());
function.block = block;
source_type_member_definition.append (function);
return proxy_name;
}
string generate_finish_dbus_proxy_method (Interface iface, Method m) {
string proxy_name = "%sdbus_proxy_%s_finish".printf (iface.get_lower_case_cprefix (), m.name);
string dbus_iface_name = iface.get_attribute ("DBus").get_string ("name");
CCodeDeclaration cdecl;
var function = new CCodeFunction (proxy_name, m.return_type.get_cname ());
function.modifiers = CCodeModifiers.STATIC;
var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
cparam_map.set (get_param_pos (0.1), new CCodeFormalParameter ("res", "GAsyncResult*"));
generate_cparameters (m, m.return_type, false, cparam_map, function, null, null, null, 2);
var block = new CCodeBlock ();
var prefragment = new CCodeFragment ();
var postfragment = new CCodeFragment ();
string dataname = "%sDBusProxy%sData".printf (iface.get_cname (), Symbol.lower_case_to_camel_case (m.name));
cdecl = new CCodeDeclaration (dataname + "*");
cdecl.add_declarator (new CCodeVariableDeclarator ("data"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusMessage");
cdecl.add_declarator (new CCodeVariableDeclarator ("*_reply"));
block.add_statement (cdecl);
cdecl = new CCodeDeclaration ("DBusMessageIter");
cdecl.add_declarator (new CCodeVariableDeclarator ("_iter"));
block.add_statement (cdecl);
var get_source_tag = new CCodeFunctionCall (new CCodeIdentifier ("g_simple_async_result_get_source_tag"));
get_source_tag.add_argument (new CCodeCastExpression (new CCodeIdentifier ("res"), "GSimpleAsyncResult *"));
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("data"), get_source_tag)));
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("dbus_pending_call_steal_reply"));
ccall.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "pending"));
block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("_reply"), ccall)));
generate_marshalling (m, dbus_iface_name, prefragment, postfragment);
block.add_statement (postfragment);
var reply_unref = new CCodeFunctionCall (new CCodeIdentifier ("dbus_message_unref"));
reply_unref.add_argument (new CCodeIdentifier ("_reply"));
block.add_statement (new CCodeExpressionStatement (reply_unref));
if (!(m.return_type is VoidType)) {
block.add_statement (new CCodeReturnStatement (new CCodeIdentifier ("_result")));
}
source_type_member_declaration.append (function.copy ());
function.block = block;
source_type_member_definition.append (function);
return proxy_name;
}
}
|