summaryrefslogtreecommitdiff
path: root/src/c_client.py
blob: 469a7f5daebc9a597abb016f1ebbad0cb9867ab0 (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
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
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
#!/usr/bin/env python
from xml.etree.cElementTree import *
from os.path import basename
import getopt
import sys
import re

# Jump to the bottom of this file for the main routine

# Some hacks to make the API more readable, and to keep backwards compability
_cname_re = re.compile('([A-Z0-9][a-z]+|[A-Z0-9]+(?![a-z])|[a-z]+)')
_cname_special_cases = {'DECnet':'decnet'}

_extension_special_cases = ['XPrint', 'XCMisc', 'BigRequests']

_cplusplus_annoyances = {'class' : '_class',
                         'new'   : '_new',
                         'delete': '_delete'}

_hlines = []
_hlevel = 0
_clines = []
_clevel = 0
_ns = None

def _h(fmt, *args):
    '''
    Writes the given line to the header file.
    '''
    _hlines[_hlevel].append(fmt % args)
    
def _c(fmt, *args):
    '''
    Writes the given line to the source file.
    '''
    _clines[_clevel].append(fmt % args)
    
def _hc(fmt, *args):
    '''
    Writes the given line to both the header and source files.
    '''
    _h(fmt, *args)
    _c(fmt, *args)

# XXX See if this level thing is really necessary.
def _h_setlevel(idx):
    '''
    Changes the array that header lines are written to.
    Supports writing different sections of the header file.
    '''
    global _hlevel
    while len(_hlines) <= idx:
        _hlines.append([])
    _hlevel = idx
    
def _c_setlevel(idx):
    '''
    Changes the array that source lines are written to.
    Supports writing to different sections of the source file.
    '''
    global _clevel
    while len(_clines) <= idx:
        _clines.append([])
    _clevel = idx
    
def _n_item(str):
    '''
    Does C-name conversion on a single string fragment.
    Uses a regexp with some hard-coded special cases.
    '''
    if str in _cname_special_cases:
        return _cname_special_cases[str]
    else:
        split = _cname_re.finditer(str)
        name_parts = [match.group(0) for match in split]
        return '_'.join(name_parts)
    
def _cpp(str):
    '''
    Checks for certain C++ reserved words and fixes them.
    '''
    if str in _cplusplus_annoyances:
        return _cplusplus_annoyances[str]
    else:
        return str

def _ext(str):
    '''
    Does C-name conversion on an extension name.
    Has some additional special cases on top of _n_item.
    '''
    if str in _extension_special_cases:
        return _n_item(str).lower()
    else:
        return str.lower()
    
def _n(list):
    '''
    Does C-name conversion on a tuple of strings.
    Different behavior depending on length of tuple, extension/not extension, etc.
    Basically C-name converts the individual pieces, then joins with underscores.
    '''
    if len(list) == 1:
        parts = list
    elif len(list) == 2:
        parts = [list[0], _n_item(list[1])]
    elif _ns.is_ext:
        parts = [list[0], _ext(list[1])] + [_n_item(i) for i in list[2:]]
    else:
        parts = [list[0]] + [_n_item(i) for i in list[1:]]
    return '_'.join(parts).lower()

def _t(list):
    '''
    Does C-name conversion on a tuple of strings representing a type.
    Same as _n but adds a "_t" on the end.
    '''
    if len(list) == 1:
        parts = list
    elif len(list) == 2:
        parts = [list[0], _n_item(list[1]), 't']
    elif _ns.is_ext:
        parts = [list[0], _ext(list[1])] + [_n_item(i) for i in list[2:]] + ['t']
    else:
        parts = [list[0]] + [_n_item(i) for i in list[1:]] + ['t']
    return '_'.join(parts).lower()
        

def c_open(self):
    '''
    Exported function that handles module open.
    Opens the files and writes out the auto-generated comment, header file includes, etc.
    '''
    global _ns
    _ns = self.namespace
    _ns.c_ext_global_name = _n(_ns.prefix + ('id',))

    # Build the type-name collision avoidance table used by c_enum
    build_collision_table()

    _h_setlevel(0)
    _c_setlevel(0)

    _hc('/*')
    _hc(' * This file generated automatically from %s by c_client.py.', _ns.file)
    _hc(' * Edit at your peril.')
    _hc(' */')
    _hc('')

    _h('/**')
    _h(' * @defgroup XCB_%s_API XCB %s API', _ns.ext_name, _ns.ext_name)
    _h(' * @brief %s XCB Protocol Implementation.', _ns.ext_name)
    _h(' * @{')
    _h(' **/')
    _h('')
    _h('#ifndef __%s_H', _ns.header.upper())
    _h('#define __%s_H', _ns.header.upper())
    _h('')
    _h('#include "xcb.h"')

    _c('#include <string.h>')
    _c('#include <assert.h>')
    _c('#include "xcbext.h"')
    _c('#include "%s.h"', _ns.header)
        
    if _ns.is_ext:
        for (n, h) in self.imports:
            _hc('#include "%s.h"', h)

    _h('')
    _h('#ifdef __cplusplus')
    _h('extern "C" {')
    _h('#endif')

    if _ns.is_ext:
        _h('')
        _h('#define XCB_%s_MAJOR_VERSION %s', _ns.ext_name.upper(), _ns.major_version)
        _h('#define XCB_%s_MINOR_VERSION %s', _ns.ext_name.upper(), _ns.minor_version)
        _h('  ') #XXX
        _h('extern xcb_extension_t %s;', _ns.c_ext_global_name)

        _c('')
        _c('xcb_extension_t %s = { "%s", 0 };', _ns.c_ext_global_name, _ns.ext_xname)

def c_close(self):
    '''
    Exported function that handles module close.
    Writes out all the stored content lines, then closes the files.
    '''
    _h_setlevel(2)
    _c_setlevel(2)
    _hc('')

    _h('')
    _h('#ifdef __cplusplus')
    _h('}')
    _h('#endif')

    _h('')
    _h('#endif')
    _h('')
    _h('/**')
    _h(' * @}')
    _h(' */')

    # Write header file
    hfile = open('%s.h' % _ns.header, 'w')
    for list in _hlines:
        for line in list:
            hfile.write(line)
            hfile.write('\n')
    hfile.close()

    # Write source file
    cfile = open('%s.c' % _ns.header, 'w')
    for list in _clines:
        for line in list:
            cfile.write(line)
            cfile.write('\n')
    cfile.close()

def build_collision_table():
    global namecount
    namecount = {}

    for v in module.types.values():
        name = _t(v[0])
        namecount[name] = (namecount.get(name) or 0) + 1

def c_enum(self, name):
    '''
    Exported function that handles enum declarations.
    '''

    tname = _t(name)
    if namecount[tname] > 1:
        tname = _t(name + ('enum',))

    _h_setlevel(0)
    _h('')
    _h('typedef enum %s {', tname)

    count = len(self.values)

    for (enam, eval) in self.values:
        count = count - 1
        equals = ' = ' if eval != '' else ''
        comma = ',' if count > 0 else ''
        _h('    %s%s%s%s', _n(name + (enam,)).upper(), equals, eval, comma)

    _h('} %s;', tname)

def _c_type_setup(self, name, postfix):
    '''
    Sets up all the C-related state by adding additional data fields to
    all Field and Type objects.  Here is where we figure out most of our
    variable and function names.

    Recurses into child fields and list member types.
    '''
    # Do all the various names in advance
    self.c_type = _t(name + postfix)
    self.c_wiretype = 'char' if self.c_type == 'void' else self.c_type

    self.c_iterator_type = _t(name + ('iterator',))
    self.c_next_name = _n(name + ('next',))
    self.c_end_name = _n(name + ('end',))

    self.c_request_name = _n(name)
    self.c_checked_name = _n(name + ('checked',))
    self.c_unchecked_name = _n(name + ('unchecked',))
    self.c_reply_name = _n(name + ('reply',))
    self.c_reply_type = _t(name + ('reply',))
    self.c_cookie_type = _t(name + ('cookie',))

    self.c_aux_name = _n(name + ('aux',))
    self.c_aux_checked_name = _n(name + ('aux', 'checked'))
    self.c_aux_unchecked_name = _n(name + ('aux', 'unchecked'))
    self.c_serialize_name = _n(name + ('serialize',))
    self.c_unserialize_name = _n(name + ('unserialize',))

    # whether a request or reply has a switch field
    self.need_aux = False
    self.need_serialize = False
    if self.is_switch:
        self.need_serialize = True
        for bitcase in self.bitcases:
            _c_type_setup(bitcase.type, bitcase.field_type, ())

    if self.is_container:

        self.c_container = 'union' if self.is_union else 'struct'
        prev_varsized_field = None
        prev_varsized_offset = 0
        first_field_after_varsized = None

        for field in self.fields:
            # information about a fields anchestors
            if self.is_reply or hasattr(self, 'in_reply'):
                field.type.in_reply = True

            _c_type_setup(field.type, field.field_type, ())
            if field.type.is_list:
                _c_type_setup(field.type.member, field.field_type, ())

            field.c_field_type = _t(field.field_type)
            field.c_field_const_type = ('' if field.type.nmemb == 1 else 'const ') + field.c_field_type
            field.c_field_name = _cpp(field.field_name)
            field.c_subscript = '[%d]' % field.type.nmemb if (field.type.nmemb > 1) else ''
            field.c_pointer = ' ' if field.type.nmemb == 1 else '*'
            if field.type.is_switch:
                field.c_pointer = '*'
                field.c_field_const_type = 'const ' + field.c_field_type
                self.need_aux = True

            field.c_iterator_type = _t(field.field_type + ('iterator',))      # xcb_fieldtype_iterator_t
            field.c_iterator_name = _n(name + (field.field_name, 'iterator')) # xcb_container_field_iterator
            field.c_accessor_name = _n(name + (field.field_name,))            # xcb_container_field
            field.c_length_name = _n(name + (field.field_name, 'length'))     # xcb_container_field_length
            field.c_end_name = _n(name + (field.field_name, 'end'))           # xcb_container_field_end

            field.prev_varsized_field = prev_varsized_field
            field.prev_varsized_offset = prev_varsized_offset

            if prev_varsized_offset == 0:
                first_field_after_varsized = field
            field.first_field_after_varsized = first_field_after_varsized

            if field.type.fixed_size():
                prev_varsized_offset += field.type.size
            else:
                self.last_varsized_field = field
                prev_varsized_field = field
                prev_varsized_offset = 0

            # FIXME - structures with variable sized members, sort out when serialize() is needed

    # as switch does never appear at toplevel, 
    # continue here with type construction
    if self.is_switch:
        # special: switch C structs get pointer fields for variable-sized members
        _c_complex(self)
        # declare switch (un)packing functions
        _c_accessors(self, name, name)

    if self.need_serialize:
        if not hasattr(self, 'in_reply'):
            _c_serialize(self)
        _c_unserialize(self)
        
# _c_type_setup()

def get_request_fields(self):
    param_fields = []
    wire_fields = []

    for field in self.fields:
        if field.visible:
            # the field should appear as a parameter in the function call
            param_fields.append(field)
        if field.wire and not field.auto:
            if field.type.fixed_size() and not self.is_switch:
                # need to set the field up in the xcb_out structure
                wire_fields.append(field)
        # fields like 'pad0' are skipped!

    return (param_fields, wire_fields)
# get_request_fields()

def unserialize_fields(complex_type, code_lines=[], space='', prefix='', bitcase=False):
    prefix_str = prefix
    need_padding = False

    if prefix != '':
        prefix_str += "->"
    if hasattr(complex_type, 'type'):
        self = complex_type.type
        complex_name = complex_type.name
    else:
        self = complex_type
        complex_name = '_aux'

    if self.is_switch:
        switch_expr = _c_accessor_get_expr(self.expr)
        need_padding = True
        for b in self.bitcases:            
            bitcase_expr = _c_accessor_get_expr(b.type.expr, prefix)
            code_lines.append('    if(%s & %s) {' % (switch_expr, bitcase_expr))
            unserialize_fields(b.type, code_lines, space="%s    " % space, 
                               prefix="%s%s" % (prefix_str, complex_name), bitcase=True)
            code_lines.append('    }')

    else: 
        for field in self.fields:
            if not ((field.wire and not field.auto) or field.visible):
                continue

            length = "sizeof(%s)" % field.c_field_type
            
            # 1. fields with fixed size
            if field.type.fixed_size():
                need_padding = True
                value = '    _aux->%s = (%s) *xcb_tmp;' % (field.c_field_name, field.type.c_type) 
                # FIXME - lists

            # 2. fields with variable size
            else:
                #if need_padding:
                # unserialize: always calculate padding before variable sized fields
                code_lines.append('%s    if (0 != xcb_block_len) {' % space)
                code_lines.append('%s        xcb_block_len += -xcb_block_len & 3;' % space)
                code_lines.append('%s        xcb_tmp += xcb_block_len;' % space)
                code_lines.append('%s        xcb_buffer_len += xcb_block_len;' % space)
                code_lines.append('%s        xcb_block_len = 0;' % space)
                code_lines.append('%s    }' % space)

                # FIXME
                print("unserialize not yet implemented for variable size fields like %s" % field.c_field_name)

                value = '    xcb_parts[xcb_parts_idx].iov_base = (char *) %s%s;' % (prefix_str, field.c_field_name)
                if field.type.is_list:
                    # FIXME - list with variable-sized elements 
                    if field.type.size is None:
                        errmsg = '%s: warning: list object with variable-sized members not supported for field %s\n' 
                        sys.stderr.write(errmsg % (self.c_type, field.c_field_name))
                    length = '%s * sizeof(%s)' % (_c_accessor_get_expr(field.type.expr, prefix), field.type.member.c_wiretype)
                elif field.type.is_switch:
                    # switch is handled by this function as a special case
                    unserialize_fields(field.type, code_lines, space, prefix="%s%s" % (prefix_str, field.c_field_name))
                else:
                    # FIXME - variable sized field that is not a list
                    errmsg = '%s: warning: non-list object of variable size not supported for field %s\n' 
                    sys.stderr.write(errmsg % (self.c_type, field.c_field_name))
                    length = '%s * sizeof(%s)' % ('Uh oh', field.type.c_wiretype)

            # save serialization C code
            if value is not None:
                if field.type.fixed_size():
                    # field appears in the request structure
                    code_lines.append('%s    /* %s.%s */' % (space, self.c_type, field.c_field_name))
                else:
                    code_lines.append('%s    /* %s */' % (space, field.c_field_name))

                # _aux->XXX = 
                code_lines.append('%s%s' % (space, value))
                if field.type.fixed_size():
                    code_lines.append('%s    xcb_block_len += %s;' % (space, length))
                    code_lines.append('%s    xcb_tmp += %s;' % (space, length))
                else:
                    # take account of padding
                    code_lines.append('%s    xcb_block_len = %s;' % (space, length))
                    code_lines.append('%s    xcb_block_len += -xcb_block_len & 3;' % space)
                    code_lines.append('%s    xcb_tmp += xcb_block_len;' % space)
                    code_lines.append('%s    xcb_buffer_len += xcb_block_len;' % space)
                    code_lines.append('%s    xcb_block_len = 0;' % space)
        
    if not bitcase:
        code_lines.append('%s    xcb_block_len += -xcb_block_len & 3;' % space)
        #code_lines.append('%s    xcb_tmp += xcb_block_len;' % space)
        code_lines.append('%s    xcb_buffer_len += xcb_block_len;' % space)
            
# unserialize_fields()
    

def serialize_fields(complex_type, code_lines=[], temp_vars=set(), 
                     space='', prefix='', serialize_fixed_size_fields=False, 
                     bitcase=False):
    """
    helper routine to build up iovec arrays that will later be copied into a single buffer

    complex_type - encapsulating Type/Field
    code_lines, temp_vars - containers for generated code & variable declarations
    space - extra space to be inserted before any statement
    prefix - prefix to be used for struct members, needed for switch/bitcase mapping
    bitcase - flags whether fields are bitcase members 
    """
    
    # count -> no. of entries in xcb_parts array
    count = 0
    # flag to mark wether explicit padding needs to be inserted
    need_padding = False    
    prefix_str = prefix
    if prefix != '':
        prefix_str += "->"

    if hasattr(complex_type, 'type'):
        self = complex_type.type
        complex_name = complex_type.name
    else:
        self = complex_type
        complex_name = '_aux'

    def end_block(count):
        "end a block => insert padding"
        if need_padding and not bitcase:
            code_lines.append('    /* padding */')
            code_lines.append('    xcb_parts[xcb_parts_idx].iov_base = xcb_pad0;')
            code_lines.append('    xcb_pad = -xcb_block_len & 3;')
            code_lines.append('    xcb_parts[xcb_parts_idx].iov_len = xcb_pad;')
            code_lines.append('    xcb_parts_idx++;')
            code_lines.append('')
            code_lines.append('    xcb_buffer_len += xcb_pad + %s;' % 'xcb_block_len')
            code_lines.append('')
            count += 1
        return count
    # end_block()

    def insert_padding(count):
        code_lines.append('%s    /* implicit padding */' % space)
        code_lines.append('%s    xcb_pad = -xcb_block_len & 3;' % space)
        code_lines.append('%s    if (0 != xcb_pad) {' % space)
        code_lines.append('%s        xcb_parts[xcb_parts_idx].iov_base = xcb_pad0;' % space)
        code_lines.append('%s        xcb_parts[xcb_parts_idx].iov_len = xcb_pad;' % space)
        code_lines.append('%s        xcb_buffer_len += xcb_block_len + xcb_pad;' % space)
        code_lines.append('%s        xcb_pad = 0;' % space)
        code_lines.append('%s        xcb_parts_idx++;' % space)
        code_lines.append('%s    }' % space)
        return count + 1
    # insert_padding()
        
    # special case - if self.is_switch, all fields need to be serialized conditionally
    if self.is_switch:
        switch_expr = _c_accessor_get_expr(self.expr)
        need_padding = True

        for b in self.bitcases:
            bitcase_expr = _c_accessor_get_expr(b.type.expr, prefix)
            code_lines.append('    if(%s & %s) {' % (switch_expr, bitcase_expr))

            count += serialize_fields(b.type, code_lines, temp_vars, '%s    ' % space, 
                                      prefix="%s%s" % (prefix_str, complex_name), 
                                      serialize_fixed_size_fields=True, bitcase=True)
            code_lines.append('    }')
 
    else: 
        for field in self.fields:
            value = None
            
            # sort out invisible fields
            if not ((field.wire and not field.auto) or field.visible):
                continue
            # else
            length = "sizeof(%s)" % field.c_field_type

            # 1. fields with fixed size
            # fixed size fields are not always handled here, 
            # dependent on serialize_fixed_size_fields
            if field.type.fixed_size() and serialize_fixed_size_fields:
                value = '    xcb_parts[xcb_parts_idx].iov_base = (char *) ' 
                need_padding = True

                if field.type.is_expr:
                    # need to register a temporary variable for the expression
                    if field.type.c_type is None:
                        raise Exception("type for field '%s' (expression '%s') unkown" % 
                                        (field.field_name, _c_accessor_get_expr(field.type.expr)))
                    temp_vars.add('    %s xcb_expr_%s = %s;' % (field.type.c_type, field.field_name, 
                                                                _c_accessor_get_expr(field.type.expr, prefix)))
                    value += "&xcb_expr_%s;" % field.field_name 

                elif field.type.is_pad:
                    if field.type.nmemb == 1:
                        temp_vars.add('    unsigned int xcb_pad = 0;')
                        temp_vars.add('    char xcb_pad0[3] = {0, 0, 0};') 
                        value += "&xcb_pad;"
                    else:
                        value = '    memset(xcb_parts[xcb_parts_idx].iov_base, 0, %d);' % field.type.nmemb
                        length += "*%d" % field.type.nmemb
                        
                else:
                    # non-list type with fixed size
                    if field.type.nmemb == 1:
                        value += "&%s%s;" % (prefix_str, field.c_field_name)
                    # list with nmemb (fixed size) elements
                    else:
                        value += '%s%s;' % (prefix_str, field.c_field_name)
                        length = '%d' % field.type.nmemb

            # 2. fields with variable size
            elif not field.type.fixed_size():
                # always calculate padding before variable sized fields
                count = insert_padding(count)
                code_lines.append('%s    xcb_block_len = 0;' % space)        

                value = '    xcb_parts[xcb_parts_idx].iov_base = (char *) %s%s;' % (prefix_str, field.c_field_name)

                if field.type.is_list:
                    # FIXME - list of variable length with variable size elements 
                    if field.type.size is None:
                        errmsg = '%s: warning: list object with variable-sized members not supported for field %s\n' 
                        sys.stderr.write(errmsg % (self.c_type, field.c_field_name))
                        length = 'undefined'
                    # list of variable length with fixed size elements
                    else:
                        length = '%s * sizeof(%s)' % (_c_accessor_get_expr(field.type.expr, prefix), 
                                                      field.type.member.c_wiretype)

                elif field.type.is_switch:
                    # switch is handled at the beginning of this function as a special case
                    count += serialize_fields(field.type, code_lines, temp_vars, space,
                                              prefix="%s%s" % (prefix_str, field.c_field_name))
                    
                else:
                    # FIXME - variable sized field that is not a list
                    errmsg = '%s: warning: non-list object of variable size not supported for field %s\n' 
                    sys.stderr.write(errmsg % (self.c_type, field.c_field_name))
                    length = '%s * sizeof(%s)' % ('Uh oh', field.type.c_wiretype)

            # 3. save serialization C code
            if value is not None:
                # insert a comment so one can easily trace back to the XML
                if field.type.fixed_size():
                    # field belongs to some anchestor structure
                    code_lines.append('%s    /* %s.%s */' % (space, self.c_type, field.c_field_name))
                else:
                    code_lines.append('%s    /* %s */' % (space, field.c_field_name))

                # set xcb_parts[].iov_base and xcb_parts[].iov_len
                code_lines.append('%s%s' % (space, value))
                code_lines.append('%s    xcb_parts[xcb_parts_idx].iov_len  = %s;' % (space, length))
                # increase xcb_parts index
                code_lines.append('%s    xcb_parts_idx++;' % space)
                count += 1

                # record required memory
                if field.type.fixed_size():
                    code_lines.append('%s    xcb_block_len += %s;' % (space, length))
                else:
                    # FIXME
                    count = insert_padding(count)
                    # raise Exception("obsolete - should not be reached")
                    # code_lines.append('%s    xcb_unpadded = xcb_parts[xcb_parts_idx].iov_len;' %  space)

    count = end_block(count)
    if count > 0:
        temp_vars.add('    unsigned int xcb_parts_idx = 0;')
        temp_vars.add('    unsigned int xcb_block_len = 0;')
    if need_padding:
        temp_vars.add('    unsigned int xcb_pad = 0;')
        temp_vars.add('    char xcb_pad0[3] = {0, 0, 0};') 
    return count
# serialize_fields()

def _c_switch_aux_params(self):
    # get the fields referenced by the switch expression
    def get_expr_fields(expr):
        if expr.op is None:
            if expr.lenfield_name is not None:
                return [expr.lenfield_name]
        else:
            if expr.op == '~':
                return get_expr_fields(expr.rhs)
            elif expr.op == 'popcount':
                return get_expr_fields(expr.rhs)
            elif expr.op == 'sumof':
                return [expr.lenfield_name]
            elif expr.op == 'enumref':
                return []
            else: 
                return get_expr_fields(expr.lhs) + get_expr_fields(expr.rhs)
    # get_expr_fields()
    
    # resolve the field names with the parent structure(s)
    unresolved_fields = get_expr_fields(self.expr)
    expr_fields = dict.fromkeys(unresolved_fields)
    for p in reversed(self.parent):
        parent_fields = dict((f.field_name, f) for f in p.fields)
        if len(unresolved_fields) == 0:
            break
        for f in parent_fields.keys():
            if f in unresolved_fields:
                expr_fields[f] = parent_fields[f]
                unresolved_fields.remove(f)
                
    if None in expr_fields.values():
        raise Exception("could not resolve all fields for <switch> %s" % self.name)

    params = []
    for name, field in expr_fields.iteritems():
        params.append((field, name))

    return params
# _c_switch_aux_params()

def get_serialize_params(self, buffer_var='_buffer', aux_var='_aux', unserialize=False):
    param_fields, wire_fields = get_request_fields(self)
    if self.is_switch:
        switch_params = _c_switch_aux_params(self)
        param_fields += [s[0] for s in switch_params]

    # _serialize function parameters
    params = [('void', '**', buffer_var)]
    # parameter fields if any
    if self.is_switch:
        for p in switch_params:
            typespec = p[0].c_field_const_type
            pointerspec = p[0].c_pointer
            params.append((typespec, pointerspec, p[0].c_field_name))
    # aux argument - structure to be serialized
    if not unserialize:
        params.append(('const %s' % self.c_type, '*', aux_var))
    else:
        params.append(('%s' % self.c_type, '*', aux_var))
    if not self.is_switch:
        for p in param_fields:
            if not p.type.fixed_size():
                params.append((p.c_field_const_type, p.c_pointer, p.c_field_name))
    return (param_fields, wire_fields, params)
# get_serialize_params()

def _c_serialize(self):
    _h_setlevel(1)
    _c_setlevel(1)

    _hc('')
    # _serialize() returns the buffer size
    _hc('int')

    variable_size_fields = 0
    # maximum space required for type definition of function arguments
    maxtypelen = 0
    param_fields, wire_fields, params = get_serialize_params(self)

    # determine N(variable_fields) 
    for field in param_fields:
        # if self.is_switch, treat all fields as if they are variable sized
        if not field.type.fixed_size() or self.is_switch:
            variable_size_fields += 1
    # determine maxtypelen
    for p in params:
        maxtypelen = max(maxtypelen, len(p[0]) + len(p[1]))    

    # write to .c/.h
    for p in range(len(params)):
        line = ""
        typespec, pointerspec, field_name = params[p]
        indent = ' '*(len(self.c_serialize_name)+2)
        # p==0: function declaration
        if 0==p:
            line = "%s (" % self.c_serialize_name
            indent = ''
        spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec))
        line += "%s%s%s  %s%s  /**< */" % (indent, typespec, spacing, pointerspec, field_name)
        if p < len(params)-1:
            _hc("%s," % line)
        else:
            _h("%s);" % line)
            _c("%s)" % line)
                
    _c('{')
    if not self.is_switch:
        _c('    %s *xcb_out = *_buffer;', self.c_type)
        _c('    unsigned int xcb_buffer_len = sizeof(%s);', self.c_type)
    else:
        _c('    char *xcb_out = *_buffer;')
        _c('    unsigned int xcb_buffer_len = 0;')
    if variable_size_fields > 0:        
        code_lines = []
        temp_vars = set()
        count = serialize_fields(self, code_lines, temp_vars,
                                 serialize_fixed_size_fields=False)
        # update variable size fields 
        variable_size_fields = count
        _c('    struct iovec xcb_parts[%d];', count)
        for t in temp_vars:
            _c(t)
        _c('    char *xcb_tmp;')
        _c('    unsigned int i;')
    if not self.is_switch:
        _c('    unsigned int xcb_out_pad = -xcb_buffer_len & 3;')
        _c('    /* add size of padding */')
        _c('    xcb_buffer_len += xcb_out_pad;')

    _c('')
    
    if variable_size_fields > 0:        
        for l in code_lines:
            _c(l)

    # variable sized fields have been collected, now
    # allocate memory and copy everything into a continuous memory area
    _c('    if (NULL == xcb_out) {')
    _c('        /* allocate memory  */')
    _c('        *_buffer = malloc(xcb_buffer_len);')
    _c('        xcb_out = *_buffer;')
    _c('    }')
    _c('')

    # fill in struct members
    if not self.is_switch:
        if len(wire_fields)>0:
            _c('    *xcb_out = *_aux;')

    # copy variable size fields into the buffer
    if variable_size_fields > 0:
        # xcb_out padding
        if not self.is_switch:
            _c('    xcb_tmp = (char*)++xcb_out;')
            _c('    xcb_tmp += xcb_out_pad;')
        else:
            _c('    xcb_tmp = xcb_out;')
            
        # variable sized fields
        _c('    for(i=0; i<xcb_parts_idx; i++) {')
#        _c('        if (0 != xcb_parts[i].iov_base) {')
        _c('        memcpy(xcb_tmp, xcb_parts[i].iov_base, xcb_parts[i].iov_len);')
#        _c('        }')
        _c('        xcb_tmp += xcb_parts[i].iov_len;')
        _c('    }')
    _c('')
    _c('    return xcb_buffer_len;')
    _c('}')
# _c_serialize()

def _c_unserialize(self):
    _h_setlevel(1)
    _c_setlevel(1)

    # _unserialize()
    _hc('')
    # _unserialize() returns the buffer size as well
    _hc('int')


    variable_size_fields = 0
    # maximum space required for type definition of function arguments
    maxtypelen = 0
    param_fields, wire_fields, params = get_serialize_params(self, unserialize=True)

    # determine N(variable_fields) 
    for field in param_fields:
        # if self.is_switch, treat all fields as if they are variable sized
        if not field.type.fixed_size() or self.is_switch:
            variable_size_fields += 1
    # determine maxtypelen
    for p in params:
        maxtypelen = max(maxtypelen, len(p[0]) + len(p[1]))    

    # write to .c/.h
    for p in range(len(params)):
        line = ""
        typespec, pointerspec, field_name = params[p]
        indent = ' '*(len(self.c_unserialize_name)+2)
        # p==0: function declaration
        if 0==p:
            line = "%s (" % self.c_unserialize_name
            indent = ''
            pointerspec = '*'
        spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec))
        line += "%s%s%s %s%s  /**< */" % (indent, typespec, spacing, pointerspec, field_name)
        if p < len(params)-1:
            _hc("%s," % line)
        else:
            _h("%s);" % line)
            _c("%s)" % line)
                
    _c('{')
    _c('    char *xcb_tmp = _buffer;')
    _c('    unsigned int xcb_buffer_len = 0;')
    _c('    unsigned int xcb_block_len = 0;')
    _c('')
    code_lines = []
    unserialize_fields(self, code_lines)
    for l in code_lines:
        _c(l)
    _c('')
    _c('    return xcb_buffer_len;')
    _c('}')
# _c_unserialize()

def _c_iterator_get_end(field, accum):
    '''
    Figures out what C code is needed to find the end of a variable-length structure field.
    For nested structures, recurses into its last variable-sized field.
    For lists, calls the end function
    '''
    if field.type.is_container:
        accum = field.c_accessor_name + '(' + accum + ')'
        # XXX there could be fixed-length fields at the end
        return _c_iterator_get_end(field.type.last_varsized_field, accum)
    if field.type.is_list:
        # XXX we can always use the first way
        if field.type.member.is_simple:
            return field.c_end_name + '(' + accum + ')'
        else:
            return field.type.member.c_end_name + '(' + field.c_iterator_name + '(' + accum + '))'

def _c_iterator(self, name):
    '''
    Declares the iterator structure and next/end functions for a given type.
    '''
    _h_setlevel(0)
    _h('')
    _h('/**')
    _h(' * @brief %s', self.c_iterator_type)
    _h(' **/')
    _h('typedef struct %s {', self.c_iterator_type)
    _h('    %s *data; /**<  */', self.c_type)
    _h('    int%s rem; /**<  */', ' ' * (len(self.c_type) - 2))
    _h('    int%s index; /**<  */', ' ' * (len(self.c_type) - 2))
    _h('} %s;', self.c_iterator_type)

    _h_setlevel(1)
    _c_setlevel(1)
    _h('')
    _h('/**')
    _h(' * Get the next element of the iterator')
    _h(' * @param i Pointer to a %s', self.c_iterator_type)
    _h(' *')
    _h(' * Get the next element in the iterator. The member rem is')
    _h(' * decreased by one. The member data points to the next')
    _h(' * element. The member index is increased by sizeof(%s)', self.c_type)
    _h(' */')
    _c('')
    _hc('')
    _hc('/*****************************************************************************')
    _hc(' **')
    _hc(' ** void %s', self.c_next_name)
    _hc(' ** ')
    _hc(' ** @param %s *i', self.c_iterator_type)
    _hc(' ** @returns void')
    _hc(' **')
    _hc(' *****************************************************************************/')
    _hc(' ')
    _hc('void')
    _h('%s (%s *i  /**< */);', self.c_next_name, self.c_iterator_type)
    _c('%s (%s *i  /**< */)', self.c_next_name, self.c_iterator_type)
    _c('{')

    if not self.fixed_size():
        _c('    %s *R = i->data;', self.c_type)
        _c('    xcb_generic_iterator_t child = %s;', _c_iterator_get_end(self.last_varsized_field, 'R'))
        _c('    --i->rem;')
        _c('    i->data = (%s *) child.data;', self.c_type)
        _c('    i->index = child.index;')
    else:
        _c('    --i->rem;')
        _c('    ++i->data;')
        _c('    i->index += sizeof(%s);', self.c_type)

    _c('}')

    _h('')
    _h('/**')
    _h(' * Return the iterator pointing to the last element')
    _h(' * @param i An %s', self.c_iterator_type)
    _h(' * @return  The iterator pointing to the last element')
    _h(' *')
    _h(' * Set the current element in the iterator to the last element.')
    _h(' * The member rem is set to 0. The member data points to the')
    _h(' * last element.')
    _h(' */')
    _c('')
    _hc('')
    _hc('/*****************************************************************************')
    _hc(' **')
    _hc(' ** xcb_generic_iterator_t %s', self.c_end_name)
    _hc(' ** ')
    _hc(' ** @param %s i', self.c_iterator_type)
    _hc(' ** @returns xcb_generic_iterator_t')
    _hc(' **')
    _hc(' *****************************************************************************/')
    _hc(' ')
    _hc('xcb_generic_iterator_t')
    _h('%s (%s i  /**< */);', self.c_end_name, self.c_iterator_type)
    _c('%s (%s i  /**< */)', self.c_end_name, self.c_iterator_type)
    _c('{')
    _c('    xcb_generic_iterator_t ret;')

    if self.fixed_size():
        _c('    ret.data = i.data + i.rem;')
        _c('    ret.index = i.index + ((char *) ret.data - (char *) i.data);')
        _c('    ret.rem = 0;')
    else:
        _c('    while(i.rem > 0)')
        _c('        %s(&i);', self.c_next_name)
        _c('    ret.data = i.data;')
        _c('    ret.rem = i.rem;')
        _c('    ret.index = i.index;')

    _c('    return ret;')
    _c('}')

def _c_accessor_get_length(expr, prefix=''):
    '''
    Figures out what C code is needed to get a length field.
    For fields that follow a variable-length field, use the accessor.
    Otherwise, just reference the structure field directly.
    '''
    prefarrow = '' if prefix == '' else prefix + '->'

    if expr.lenfield != None and expr.lenfield.prev_varsized_field != None:
        return expr.lenfield.c_accessor_name + '(' + prefix + ')'
    elif expr.lenfield_name != None:
        return prefarrow + expr.lenfield_name
    else:
        return str(expr.nmemb)

def _c_accessor_get_expr(expr, prefix=''):
    '''
    Figures out what C code is needed to get the length of a list field.
    Recurses for math operations.
    Returns bitcount for value-mask fields.
    Otherwise, uses the value of the length field.
    '''
    lenexp = _c_accessor_get_length(expr, prefix)

    if expr.op == '~':
        return '(' + '~' + _c_accessor_get_expr(expr.rhs, prefix) + ')'
    elif expr.op == 'popcount':
        return 'xcb_popcount(' + _c_accessor_get_expr(expr.rhs, prefix) + ')'
    elif expr.op == 'enumref':
        enum_name = expr.lenfield_type.name
        constant_name = expr.lenfield_name
        c_name = _n(enum_name + (constant_name,)).upper()
        return c_name
    elif expr.op == 'sumof':
        # 1. locate the referenced list object
        list_obj = expr.lenfield_type
        field = None
        for f in expr.lenfield_parent.fields:
            if f.field_name == expr.lenfield_name:
                field = f
                break
        if field is None:
            raise Exception("list field '%s' referenced by sumof not found" % expr.lenfield_name)
        if prefix != '':
            prefix = "%s->" % prefix
        list_name = "%s%s" % (prefix, field.c_field_name)
        c_length_func = "%s(%s%s)" % (field.c_length_name, prefix, field.c_field_name)
        return 'xcb_sumof(%s, %s)' % (list_name, c_length_func)
    elif expr.op != None:
        return '(' + _c_accessor_get_expr(expr.lhs, prefix) + ' ' + expr.op + ' ' + _c_accessor_get_expr(expr.rhs, prefix) + ')'
    elif expr.bitfield:
        return 'xcb_popcount(' + lenexp + ')'
    else:
        return lenexp

def _c_accessors_field(self, field):
    '''
    Declares the accessor functions for a non-list field that follows a variable-length field.
    '''
    if field.type.is_simple:
        _hc('')
        _hc('')
        _hc('/*****************************************************************************')
        _hc(' **')
        _hc(' ** %s %s', field.c_field_type, field.c_accessor_name)
        _hc(' ** ')
        _hc(' ** @param const %s *R', self.c_type)
        _hc(' ** @returns %s', field.c_field_type)
        _hc(' **')
        _hc(' *****************************************************************************/')
        _hc(' ')
        _hc('%s', field.c_field_type)
        _h('%s (const %s *R  /**< */);', field.c_accessor_name, self.c_type)
        _c('%s (const %s *R  /**< */)', field.c_accessor_name, self.c_type)
        _c('{')
        _c('    xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
        _c('    return * (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index) + %d);', 
           field.c_field_type, field.first_field_after_varsized.type.c_type, field.prev_varsized_offset)
        _c('}')
    else:
        _hc('')
        _hc('')
        _hc('/*****************************************************************************')
        _hc(' **')
        _hc(' ** %s * %s', field.c_field_type, field.c_accessor_name)
        _hc(' ** ')
        _hc(' ** @param const %s *R', self.c_type)
        _hc(' ** @returns %s *', field.c_field_type)
        _hc(' **')
        _hc(' *****************************************************************************/')
        _hc(' ')
        _hc('%s *', field.c_field_type)
        _h('%s (const %s *R  /**< */);', field.c_accessor_name, self.c_type)
        _c('%s (const %s *R  /**< */)', field.c_accessor_name, self.c_type)
        _c('{')
        _c('    xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
        _c('    return (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index) + %d);', field.c_field_type, field.first_field_after_varsized.type.c_type, field.prev_varsized_offset)
        _c('}')
    
def _c_accessors_list(self, field):
    '''
    Declares the accessor functions for a list field.
    Declares a direct-accessor function only if the list members are fixed size.
    Declares length and get-iterator functions always.
    '''
    list = field.type

    _h_setlevel(1)
    _c_setlevel(1)
    if list.member.fixed_size():
        _hc('')
        _hc('')
        _hc('/*****************************************************************************')
        _hc(' **')
        _hc(' ** %s * %s', field.c_field_type, field.c_accessor_name)
        _hc(' ** ')
        _hc(' ** @param const %s *R', self.c_type)
        _hc(' ** @returns %s *', field.c_field_type)
        _hc(' **')
        _hc(' *****************************************************************************/')
        _hc(' ')
        _hc('%s *', field.c_field_type)
        _h('%s (const %s *R  /**< */);', field.c_accessor_name, self.c_type)
        _c('%s (const %s *R  /**< */)', field.c_accessor_name, self.c_type)
        _c('{')

        if field.prev_varsized_field == None:
            _c('    return (%s *) (R + 1);', field.c_field_type)
        else:
            _c('    xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
            _c('    return (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index) + %d);', field.c_field_type, field.first_field_after_varsized.type.c_type, field.prev_varsized_offset)

        _c('}')

    _hc('')
    _hc('')
    _hc('/*****************************************************************************')
    _hc(' **')
    _hc(' ** int %s', field.c_length_name)
    _hc(' ** ')
    _hc(' ** @param const %s *R', self.c_type)
    _hc(' ** @returns int')
    _hc(' **')
    _hc(' *****************************************************************************/')
    _hc(' ')
    _hc('int')
    _h('%s (const %s *R  /**< */);', field.c_length_name, self.c_type)
    _c('%s (const %s *R  /**< */)', field.c_length_name, self.c_type)
    _c('{')
    _c('    return %s;', _c_accessor_get_expr(field.type.expr, 'R'))
    _c('}')

    if field.type.member.is_simple:
        _hc('')
        _hc('')
        _hc('/*****************************************************************************')
        _hc(' **')
        _hc(' ** xcb_generic_iterator_t %s', field.c_end_name)
        _hc(' ** ')
        _hc(' ** @param const %s *R', self.c_type)
        _hc(' ** @returns xcb_generic_iterator_t')
        _hc(' **')
        _hc(' *****************************************************************************/')
        _hc(' ')
        _hc('xcb_generic_iterator_t')
        _h('%s (const %s *R  /**< */);', field.c_end_name, self.c_type)
        _c('%s (const %s *R  /**< */)', field.c_end_name, self.c_type)
        _c('{')
        _c('    xcb_generic_iterator_t i;')

        if field.prev_varsized_field == None:
            _c('    i.data = ((%s *) (R + 1)) + (%s);', field.type.c_wiretype, _c_accessor_get_expr(field.type.expr, 'R'))
        else:
            _c('    xcb_generic_iterator_t child = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
            _c('    i.data = ((%s *) child.data) + (%s);', field.type.c_wiretype, _c_accessor_get_expr(field.type.expr, 'R'))

        _c('    i.rem = 0;')
        _c('    i.index = (char *) i.data - (char *) R;')
        _c('    return i;')
        _c('}')

    else:
        _hc('')
        _hc('')
        _hc('/*****************************************************************************')
        _hc(' **')
        _hc(' ** %s %s', field.c_iterator_type, field.c_iterator_name)
        _hc(' ** ')
        _hc(' ** @param const %s *R', self.c_type)
        _hc(' ** @returns %s', field.c_iterator_type)
        _hc(' **')
        _hc(' *****************************************************************************/')
        _hc(' ')
        _hc('%s', field.c_iterator_type)
        _h('%s (const %s *R  /**< */);', field.c_iterator_name, self.c_type)
        _c('%s (const %s *R  /**< */)', field.c_iterator_name, self.c_type)
        _c('{')
        _c('    %s i;', field.c_iterator_type)

        if field.prev_varsized_field == None:
            _c('    i.data = (%s *) (R + 1);', field.c_field_type)
        else:
            _c('    xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
            _c('    i.data = (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index));', field.c_field_type, field.c_field_type)

        _c('    i.rem = %s;', _c_accessor_get_expr(field.type.expr, 'R'))
        _c('    i.index = (char *) i.data - (char *) R;')
        _c('    return i;')
        _c('}')

def _c_accessors(self, name, base):
    '''
    Declares the accessor functions for the fields of a structure.
    '''
    for field in self.fields:
        if field.type.is_list and not field.type.fixed_size():
            _c_accessors_list(self, field)
        elif field.prev_varsized_field != None:
            _c_accessors_field(self, field)

def c_simple(self, name):
    '''
    Exported function that handles cardinal type declarations.
    These are types which are typedef'd to one of the CARDx's, char, float, etc.
    '''
    _c_type_setup(self, name, ())

    if (self.name != name):
        # Typedef
        _h_setlevel(0)
        my_name = _t(name)
        _h('')
        _h('typedef %s %s;', _t(self.name), my_name)

        # Iterator
        _c_iterator(self, name)

def _c_complex(self):
    '''
    Helper function for handling all structure types.
    Called for all structs, requests, replies, events, errors.
    '''
    _h_setlevel(0)
    _h('')
    _h('/**')
    _h(' * @brief %s', self.c_type)
    _h(' **/')
    _h('typedef %s %s {', self.c_container, self.c_type)

    struct_fields = []
    maxtypelen = 0

    varfield = None
    for field in self.fields:
        if not field.type.fixed_size() and not self.is_switch:
            varfield = field.c_field_name
            continue
        if varfield != None and not field.type.is_pad and field.wire:
            errmsg = '%s: warning: variable field %s followed by fixed field %s\n' % (self.c_type, varfield, field.c_field_name)
            sys.stderr.write(errmsg)
            # sys.exit(1)
        if field.wire:
            struct_fields.append(field)
        
    for field in struct_fields:
        length = len(field.c_field_type)
        if field.type.fixed_size():
            length += 1
        if length > maxtypelen:
            maxtypelen = length

    for field in struct_fields:
        if field.type.fixed_size():
            spacing = ' ' * (maxtypelen - len(field.c_field_type))
            _h('    %s%s %s%s; /**<  */', field.c_field_type, spacing, field.c_field_name, field.c_subscript)
        else:
            spacing = ' ' * (maxtypelen - (len(field.c_field_type) - 1))
            _h('    %s%s *%s%s; /**<  */', field.c_field_type, spacing, field.c_field_name, field.c_subscript)

    _h('} %s;', self.c_type)

def c_struct(self, name):
    '''
    Exported function that handles structure declarations.
    '''
    _c_type_setup(self, name, ())
    _c_complex(self)
    _c_accessors(self, name, name)
    _c_iterator(self, name)

def c_union(self, name):
    '''
    Exported function that handles union declarations.
    '''
    _c_type_setup(self, name, ())
    _c_complex(self)
    _c_iterator(self, name)

def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
    '''
    Declares a request function.
    '''

    # Four stunningly confusing possibilities here:
    #
    #   Void            Non-void
    # ------------------------------
    # "req"            "req"
    # 0 flag           CHECKED flag   Normal Mode
    # void_cookie      req_cookie
    # ------------------------------
    # "req_checked"    "req_unchecked"
    # CHECKED flag     0 flag         Abnormal Mode
    # void_cookie      req_cookie
    # ------------------------------


    # Whether we are _checked or _unchecked
    checked = void and not regular
    unchecked = not void and not regular

    # What kind of cookie we return
    func_cookie = 'xcb_void_cookie_t' if void else self.c_cookie_type

    # What flag is passed to xcb_request
    func_flags = '0' if (void and regular) or (not void and not regular) else 'XCB_REQUEST_CHECKED'

    # Global extension id variable or NULL for xproto
    func_ext_global = '&' + _ns.c_ext_global_name if _ns.is_ext else '0'

    # What our function name is
    func_name = self.c_request_name if not aux else self.c_aux_name
    if checked:
        func_name = self.c_checked_name if not aux else self.c_aux_checked_name
    if unchecked:
        func_name = self.c_unchecked_name if not aux else self.c_aux_unchecked_name

    param_fields = []
    wire_fields = []
    maxtypelen = len('xcb_connection_t')
    serial_fields = []

    for field in self.fields:
        if field.visible:
            # The field should appear as a call parameter
            param_fields.append(field)
        if field.wire and not field.auto:
            # We need to set the field up in the structure
            wire_fields.append(field)
        if field.type.need_serialize:
            serial_fields.append(field)
        
    for field in param_fields:
        c_field_const_type = field.c_field_const_type 
        if field.type.need_serialize and not aux:
            c_field_const_type = "const void"
        if len(c_field_const_type) > maxtypelen:
            maxtypelen = len(c_field_const_type)

    _h_setlevel(1)
    _c_setlevel(1)
    _h('')
    _h('/**')
    _h(' * Delivers a request to the X server')
    _h(' * @param c The connection')
    _h(' * @return A cookie')
    _h(' *')
    _h(' * Delivers a request to the X server.')
    _h(' * ')
    if checked:
        _h(' * This form can be used only if the request will not cause')
        _h(' * a reply to be generated. Any returned error will be')
        _h(' * saved for handling by xcb_request_check().')
    if unchecked:
        _h(' * This form can be used only if the request will cause')
        _h(' * a reply to be generated. Any returned error will be')
        _h(' * placed in the event queue.')
    _h(' */')
    _c('')
    _hc('')
    _hc('/*****************************************************************************')
    _hc(' **')
    _hc(' ** %s %s', cookie_type, func_name)
    _hc(' ** ')

    spacing = ' ' * (maxtypelen - len('xcb_connection_t'))
    _hc(' ** @param xcb_connection_t%s *c', spacing)

    for field in param_fields:
        c_field_const_type = field.c_field_const_type 
        if field.type.need_serialize and not aux:
            c_field_const_type = "const void"
        spacing = ' ' * (maxtypelen - len(c_field_const_type))
        _hc(' ** @param %s%s %s%s', c_field_const_type, spacing, field.c_pointer, field.c_field_name)

    _hc(' ** @returns %s', cookie_type)
    _hc(' **')
    _hc(' *****************************************************************************/')
    _hc(' ')
    _hc('%s', cookie_type)

    spacing = ' ' * (maxtypelen - len('xcb_connection_t'))
    comma = ',' if len(param_fields) else ');'
    _h('%s (xcb_connection_t%s *c  /**< */%s', func_name, spacing, comma)
    comma = ',' if len(param_fields) else ')'
    _c('%s (xcb_connection_t%s *c  /**< */%s', func_name, spacing, comma)

    func_spacing = ' ' * (len(func_name) + 2)
    count = len(param_fields)
    for field in param_fields:
        count = count - 1
        c_field_const_type = field.c_field_const_type 
        if field.type.need_serialize and not aux:
            c_field_const_type = "const void"
        spacing = ' ' * (maxtypelen - len(c_field_const_type))
        comma = ',' if count else ');'
        _h('%s%s%s %s%s  /**< */%s', func_spacing, c_field_const_type, 
           spacing, field.c_pointer, field.c_field_name, comma)
        comma = ',' if count else ')'
        _c('%s%s%s %s%s  /**< */%s', func_spacing, c_field_const_type, 
           spacing, field.c_pointer, field.c_field_name, comma)

    count = 2
    for field in param_fields:
        if not field.type.fixed_size():
            count = count + 2
            if field.type.need_serialize:
                # _serialize() keeps track of padding automatically
                count -= 1

    _c('{')
    _c('    static const xcb_protocol_request_t xcb_req = {')
    _c('        /* count */ %d,', count)
    _c('        /* ext */ %s,', func_ext_global)
    _c('        /* opcode */ %s,', self.c_request_name.upper())
    _c('        /* isvoid */ %d', 1 if void else 0)
    _c('    };')
    _c('    ')

    _c('    struct iovec xcb_parts[%d];', count + 2)
    _c('    %s xcb_ret;', func_cookie)
    _c('    %s xcb_out;', self.c_type)
    for idx, f in enumerate(serial_fields):
        _c('    %s xcb_aux%d;', f.type.c_type, idx)
    _c('    ')
    _c('    printf("in function %s\\n");' % func_name)     
 
    # fixed size fields
    for field in wire_fields:
        if field.type.fixed_size():
            if field.type.is_expr:
                _c('    xcb_out.%s = %s;', field.c_field_name, _c_accessor_get_expr(field.type.expr))
            elif field.type.is_pad:
                if field.type.nmemb == 1:
                    _c('    xcb_out.%s = 0;', field.c_field_name)
                else:
                    _c('    memset(xcb_out.%s, 0, %d);', field.c_field_name, field.type.nmemb)
            else:
                if field.type.nmemb == 1:
                    _c('    xcb_out.%s = %s;', field.c_field_name, field.c_field_name)
                else:
                    _c('    memcpy(xcb_out.%s, %s, %d);', field.c_field_name, field.c_field_name, field.type.nmemb)

    _c('    ')
    _c('    xcb_parts[2].iov_base = (char *) &xcb_out;')
    _c('    xcb_parts[2].iov_len = sizeof(xcb_out);')
    _c('    xcb_parts[3].iov_base = 0;')
    _c('    xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;')

    # calls in order to free dyn. all. memory
    free_calls = []
    count = 4
    for field in param_fields:
        if not field.type.fixed_size():
            _c('    xcb_parts[%d].iov_base = (char *) %s;', count, field.c_field_name)
            if field.type.need_serialize:
                idx = serial_fields.index(field)
                serialize_args = get_serialize_params(field.type, 
                                                      field.c_field_name, 
                                                      '&xcb_aux%d' % idx,
                                                      )[2]
                serialize_args = reduce(lambda x,y: "%s, %s" % (x,y), [a[2] for a in serialize_args])
                _c('    xcb_parts[%d].iov_len = ', count)
                if aux:
                    _c('      %s (%s);', field.type.c_serialize_name, serialize_args)
                    free_calls.append('    free(xcb_parts[%d].iov_base);' % count)
                else:
                    _c('      %s (%s);', field.type.c_unserialize_name, serialize_args)
            if field.type.is_list:
                _c('    xcb_parts[%d].iov_len = %s * sizeof(%s);', count, 
                   _c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype)
            elif not field.type.need_serialize:
                # FIXME - _serialize()
                _c('    xcb_parts[%d].iov_len = %s * sizeof(%s);', 
                   count, 'Uh oh', field.type.c_wiretype)
            
            count += 1
            if not field.type.need_serialize:
                # the _serialize() function keeps track of padding automatically
                _c('    xcb_parts[%d].iov_base = 0;', count)
                _c('    xcb_parts[%d].iov_len = -xcb_parts[%d].iov_len & 3;', count, count-1)
                count += 1

    _c('    ')
    _c('    xcb_ret.sequence = xcb_send_request(c, %s, xcb_parts + 2, &xcb_req);', func_flags)
    
    # free dyn. all. data, if any
    for f in free_calls:
        _c(f)
    _c('    return xcb_ret;')
    _c('}')

def _c_reply(self, name):
    '''
    Declares the function that returns the reply structure.
    '''
    spacing1 = ' ' * (len(self.c_cookie_type) - len('xcb_connection_t'))
    spacing2 = ' ' * (len(self.c_cookie_type) - len('xcb_generic_error_t'))
    spacing3 = ' ' * (len(self.c_reply_name) + 2)

    _h('')
    _h('/**')
    _h(' * Return the reply')
    _h(' * @param c      The connection')
    _h(' * @param cookie The cookie')
    _h(' * @param e      The xcb_generic_error_t supplied')
    _h(' *')
    _h(' * Returns the reply of the request asked by')
    _h(' * ')
    _h(' * The parameter @p e supplied to this function must be NULL if')
    _h(' * %s(). is used.', self.c_unchecked_name)
    _h(' * Otherwise, it stores the error if any.')
    _h(' *')
    _h(' * The returned value must be freed by the caller using free().')
    _h(' */')
    _c('')
    _hc('')
    _hc('/*****************************************************************************')
    _hc(' **')
    _hc(' ** %s * %s', self.c_reply_type, self.c_reply_name)
    _hc(' ** ')
    _hc(' ** @param xcb_connection_t%s  *c', spacing1)
    _hc(' ** @param %s   cookie', self.c_cookie_type)
    _hc(' ** @param xcb_generic_error_t%s **e', spacing2)
    _hc(' ** @returns %s *', self.c_reply_type)
    _hc(' **')
    _hc(' *****************************************************************************/')
    _hc(' ')
    _hc('%s *', self.c_reply_type)
    _hc('%s (xcb_connection_t%s  *c  /**< */,', self.c_reply_name, spacing1)
    _hc('%s%s   cookie  /**< */,', spacing3, self.c_cookie_type)
    _h('%sxcb_generic_error_t%s **e  /**< */);', spacing3, spacing2)
    _c('%sxcb_generic_error_t%s **e  /**< */)', spacing3, spacing2)
    _c('{')
    _c('    return (%s *) xcb_wait_for_reply(c, cookie.sequence, e);', self.c_reply_type)
    _c('}')

def _c_opcode(name, opcode):
    '''
    Declares the opcode define for requests, events, and errors.
    '''
    _h_setlevel(0)
    _h('')
    _h('/** Opcode for %s. */', _n(name))
    _h('#define %s %s', _n(name).upper(), opcode)
    
def _c_cookie(self, name):
    '''
    Declares the cookie type for a non-void request.
    '''
    _h_setlevel(0)
    _h('')
    _h('/**')
    _h(' * @brief %s', self.c_cookie_type)
    _h(' **/')
    _h('typedef struct %s {', self.c_cookie_type)
    _h('    unsigned int sequence; /**<  */')
    _h('} %s;', self.c_cookie_type)

def c_request(self, name):
    '''
    Exported function that handles request declarations.
    '''
    _c_type_setup(self, name, ('request',))

    if self.reply:
        # Cookie type declaration
        _c_cookie(self, name)

    # Opcode define
    _c_opcode(name, self.opcode)

    # Request structure declaration
    _c_complex(self)

    if self.reply:
        _c_type_setup(self.reply, name, ('reply',))
        # Reply structure definition
        _c_complex(self.reply)
        # Request prototypes
        _c_request_helper(self, name, self.c_cookie_type, False, True)
        _c_request_helper(self, name, self.c_cookie_type, False, False)
        if self.need_aux:
            _c_request_helper(self, name, self.c_cookie_type, False, True, True)
            _c_request_helper(self, name, self.c_cookie_type, False, False, True)
        # Reply accessors
        _c_accessors(self.reply, name + ('reply',), name)
        _c_reply(self, name)
    else:
        # Request prototypes
        _c_request_helper(self, name, 'xcb_void_cookie_t', True, False)
        _c_request_helper(self, name, 'xcb_void_cookie_t', True, True)
        if self.need_aux:
            _c_request_helper(self, name, 'xcb_void_cookie_t', True, False, True)
            _c_request_helper(self, name, 'xcb_void_cookie_t', True, True, True)


def c_event(self, name):
    '''
    Exported function that handles event declarations.
    '''
    _c_type_setup(self, name, ('event',))

    # Opcode define
    _c_opcode(name, self.opcodes[name])

    if self.name == name:
        # Structure definition
        _c_complex(self)
    else:
        # Typedef
        _h('')
        _h('typedef %s %s;', _t(self.name + ('event',)), _t(name + ('event',)))

def c_error(self, name):
    '''
    Exported function that handles error declarations.
    '''
    _c_type_setup(self, name, ('error',))

    # Opcode define
    _c_opcode(name, self.opcodes[name])

    if self.name == name:
        # Structure definition
        _c_complex(self)
    else:
        # Typedef
        _h('')
        _h('typedef %s %s;', _t(self.name + ('error',)), _t(name + ('error',)))


# Main routine starts here

# Must create an "output" dictionary before any xcbgen imports.
output = {'open'    : c_open,
          'close'   : c_close,
          'simple'  : c_simple,
          'enum'    : c_enum,
          'struct'  : c_struct,
          'union'   : c_union,
          'request' : c_request,
          'event'   : c_event,
          'error'   : c_error, 
          }

# Boilerplate below this point

# Check for the argument that specifies path to the xcbgen python package.
try:
    opts, args = getopt.getopt(sys.argv[1:], 'p:')
except getopt.GetoptError, err:
    print str(err)
    print 'Usage: c_client.py [-p path] file.xml'
    sys.exit(1)

for (opt, arg) in opts:
    if opt == '-p':
        sys.path.append(arg)

# Import the module class
try:
    from xcbgen.state import Module
except ImportError:
    print ''
    print 'Failed to load the xcbgen Python package!'
    print 'Make sure that xcb/proto installed it on your Python path.'
    print 'If not, you will need to create a .pth file or define $PYTHONPATH'
    print 'to extend the path.'
    print 'Refer to the README file in xcb/proto for more info.'
    print ''
    raise

# Parse the xml header
module = Module(args[0], output)

# Build type-registry and resolve type dependencies
module.register()
module.resolve()

# Output the code
module.generate()