2 from xml.etree.cElementTree import *
3 from os.path import basename
8 # Jump to the bottom of this file for the main routine
10 # Some hacks to make the API more readable, and to keep backwards compability
11 _cname_re = re.compile('([A-Z0-9][a-z]+|[A-Z0-9]+(?![a-z])|[a-z]+)')
12 _cname_special_cases = {'DECnet':'decnet'}
14 _extension_special_cases = ['XPrint', 'XCMisc', 'BigRequests']
16 _cplusplus_annoyances = {'class' : '_class',
19 _c_keywords = {'default' : '_default'}
27 # global variable to keep track of serializers and
28 # switch data types due to weird dependencies
29 finished_serializers = []
35 Writes the given line to the header file.
37 _hlines[_hlevel].append(fmt % args)
41 Writes the given line to the source file.
43 _clines[_clevel].append(fmt % args)
47 Writes the given line to both the header and source files.
52 # XXX See if this level thing is really necessary.
55 Changes the array that header lines are written to.
56 Supports writing different sections of the header file.
59 while len(_hlines) <= idx:
65 Changes the array that source lines are written to.
66 Supports writing to different sections of the source file.
69 while len(_clines) <= idx:
75 Does C-name conversion on a single string fragment.
76 Uses a regexp with some hard-coded special cases.
78 if str in _cname_special_cases:
79 return _cname_special_cases[str]
81 split = _cname_re.finditer(str)
82 name_parts = [match.group(0) for match in split]
83 return '_'.join(name_parts)
87 Checks for certain C++ reserved words and fixes them.
89 if str in _cplusplus_annoyances:
90 return _cplusplus_annoyances[str]
91 elif str in _c_keywords:
92 return _c_keywords[str]
98 Does C-name conversion on an extension name.
99 Has some additional special cases on top of _n_item.
101 if str in _extension_special_cases:
102 return _n_item(str).lower()
108 Does C-name conversion on a tuple of strings.
109 Different behavior depending on length of tuple, extension/not extension, etc.
110 Basically C-name converts the individual pieces, then joins with underscores.
115 parts = [list[0], _n_item(list[1])]
117 parts = [list[0], _ext(list[1])] + [_n_item(i) for i in list[2:]]
119 parts = [list[0]] + [_n_item(i) for i in list[1:]]
120 return '_'.join(parts).lower()
124 Does C-name conversion on a tuple of strings representing a type.
125 Same as _n but adds a "_t" on the end.
130 parts = [list[0], _n_item(list[1]), 't']
132 parts = [list[0], _ext(list[1])] + [_n_item(i) for i in list[2:]] + ['t']
134 parts = [list[0]] + [_n_item(i) for i in list[1:]] + ['t']
135 return '_'.join(parts).lower()
140 Exported function that handles module open.
141 Opens the files and writes out the auto-generated comment, header file includes, etc.
145 _ns.c_ext_global_name = _n(_ns.prefix + ('id',))
147 # Build the type-name collision avoidance table used by c_enum
148 build_collision_table()
154 _hc(' * This file generated automatically from %s by c_client.py.', _ns.file)
155 _hc(' * Edit at your peril.')
160 _h(' * @defgroup XCB_%s_API XCB %s API', _ns.ext_name, _ns.ext_name)
161 _h(' * @brief %s XCB Protocol Implementation.', _ns.ext_name)
165 _h('#ifndef __%s_H', _ns.header.upper())
166 _h('#define __%s_H', _ns.header.upper())
168 _h('#include "xcb.h"')
170 _c('#include <stdlib.h>')
171 _c('#include <string.h>')
172 _c('#include <assert.h>')
173 _c('#include "xcbext.h"')
174 _c('#include "%s.h"', _ns.header)
177 for (n, h) in self.imports:
178 _hc('#include "%s.h"', h)
181 _h('#ifdef __cplusplus')
187 _h('#define XCB_%s_MAJOR_VERSION %s', _ns.ext_name.upper(), _ns.major_version)
188 _h('#define XCB_%s_MINOR_VERSION %s', _ns.ext_name.upper(), _ns.minor_version)
190 _h('extern xcb_extension_t %s;', _ns.c_ext_global_name)
193 _c('xcb_extension_t %s = { "%s", 0 };', _ns.c_ext_global_name, _ns.ext_xname)
197 Exported function that handles module close.
198 Writes out all the stored content lines, then closes the files.
205 _h('#ifdef __cplusplus')
217 hfile = open('%s.h' % _ns.header, 'w')
225 cfile = open('%s.c' % _ns.header, 'w')
232 def build_collision_table():
236 for v in module.types.values():
238 namecount[name] = (namecount.get(name) or 0) + 1
240 def c_enum(self, name):
242 Exported function that handles enum declarations.
246 if namecount[tname] > 1:
247 tname = _t(name + ('enum',))
251 _h('typedef enum %s {', tname)
253 count = len(self.values)
255 for (enam, eval) in self.values:
257 equals = ' = ' if eval != '' else ''
258 comma = ',' if count > 0 else ''
259 _h(' %s%s%s%s', _n(name + (enam,)).upper(), equals, eval, comma)
263 def _c_type_setup(self, name, postfix):
265 Sets up all the C-related state by adding additional data fields to
266 all Field and Type objects. Here is where we figure out most of our
267 variable and function names.
269 Recurses into child fields and list member types.
271 # Do all the various names in advance
272 self.c_type = _t(name + postfix)
273 self.c_wiretype = 'char' if self.c_type == 'void' else self.c_type
275 self.c_iterator_type = _t(name + ('iterator',))
276 self.c_next_name = _n(name + ('next',))
277 self.c_end_name = _n(name + ('end',))
279 self.c_request_name = _n(name)
280 self.c_checked_name = _n(name + ('checked',))
281 self.c_unchecked_name = _n(name + ('unchecked',))
282 self.c_reply_name = _n(name + ('reply',))
283 self.c_reply_type = _t(name + ('reply',))
284 self.c_cookie_type = _t(name + ('cookie',))
286 self.need_aux = False
287 self.need_serialize = False
288 self.need_sizeof = False
290 self.c_aux_name = _n(name + ('aux',))
291 self.c_aux_checked_name = _n(name + ('aux', 'checked'))
292 self.c_aux_unchecked_name = _n(name + ('aux', 'unchecked'))
293 self.c_serialize_name = _n(name + ('serialize',))
294 self.c_unserialize_name = _n(name + ('unserialize',))
295 self.c_unpack_name = _n(name + ('unpack',))
296 self.c_sizeof_name = _n(name + ('sizeof',))
298 # special case: structs where variable size fields are followed by fixed size fields
299 self.var_followed_by_fixed_fields = False
302 self.need_serialize = True
303 self.c_container = 'struct'
304 for bitcase in self.bitcases:
305 bitcase.c_field_name = _cpp(bitcase.field_name)
306 bitcase_name = bitcase.field_type if bitcase.type.has_name else name
307 _c_type_setup(bitcase.type, bitcase_name, ())
309 elif self.is_container:
311 self.c_container = 'union' if self.is_union else 'struct'
312 prev_varsized_field = None
313 prev_varsized_offset = 0
314 first_field_after_varsized = None
316 for field in self.fields:
317 _c_type_setup(field.type, field.field_type, ())
318 if field.type.is_list:
319 _c_type_setup(field.type.member, field.field_type, ())
320 if (field.type.nmemb is None):
321 self.need_sizeof = True
323 field.c_field_type = _t(field.field_type)
324 field.c_field_const_type = ('' if field.type.nmemb == 1 else 'const ') + field.c_field_type
325 field.c_field_name = _cpp(field.field_name)
326 field.c_subscript = '[%d]' % field.type.nmemb if (field.type.nmemb > 1) else ''
327 field.c_pointer = ' ' if field.type.nmemb == 1 else '*'
329 # correct the c_pointer field for variable size non-list types
330 if not field.type.fixed_size() and field.c_pointer == ' ':
331 field.c_pointer = '*'
332 if field.type.is_list and not field.type.member.fixed_size():
333 field.c_pointer = '*'
335 if field.type.is_switch:
336 field.c_pointer = '*'
337 field.c_field_const_type = 'const ' + field.c_field_type
339 elif not field.type.fixed_size() and not field.type.is_bitcase:
340 self.need_sizeof = True
342 field.c_iterator_type = _t(field.field_type + ('iterator',)) # xcb_fieldtype_iterator_t
343 field.c_iterator_name = _n(name + (field.field_name, 'iterator')) # xcb_container_field_iterator
344 field.c_accessor_name = _n(name + (field.field_name,)) # xcb_container_field
345 field.c_length_name = _n(name + (field.field_name, 'length')) # xcb_container_field_length
346 field.c_end_name = _n(name + (field.field_name, 'end')) # xcb_container_field_end
348 field.prev_varsized_field = prev_varsized_field
349 field.prev_varsized_offset = prev_varsized_offset
351 if prev_varsized_offset == 0:
352 first_field_after_varsized = field
353 field.first_field_after_varsized = first_field_after_varsized
355 if field.type.fixed_size():
356 prev_varsized_offset += field.type.size
357 # special case: intermixed fixed and variable size fields
358 if prev_varsized_field is not None and not field.type.is_pad and field.wire:
359 if not self.is_union:
360 self.need_serialize = True
361 self.var_followed_by_fixed_fields = True
363 self.last_varsized_field = field
364 prev_varsized_field = field
365 prev_varsized_offset = 0
367 if self.var_followed_by_fixed_fields:
368 if field.type.fixed_size():
369 field.prev_varsized_field = None
371 if self.need_serialize:
372 # when _unserialize() is wanted, create _sizeof() as well for consistency reasons
373 self.need_sizeof = True
375 # as switch does never appear at toplevel,
376 # continue here with type construction
378 if self.c_type not in finished_switch:
379 finished_switch.append(self.c_type)
380 # special: switch C structs get pointer fields for variable-sized members
382 for bitcase in self.bitcases:
383 bitcase_name = bitcase.type.name if bitcase.type.has_name else name
384 _c_accessors(bitcase.type, bitcase_name, bitcase_name)
385 # no list with switch as element, so no call to
386 # _c_iterator(field.type, field_name) necessary
388 if not self.is_bitcase:
389 if self.need_serialize:
390 if self.c_serialize_name not in finished_serializers:
391 finished_serializers.append(self.c_serialize_name)
392 _c_serialize('serialize', self)
394 # _unpack() and _unserialize() are only needed for special cases:
396 # special cases -> unserialize
397 if self.is_switch or self.var_followed_by_fixed_fields:
398 _c_serialize('unserialize', self)
401 if self.c_sizeof_name not in finished_sizeof:
402 if not module.namespace.is_ext or self.name[:2] == module.namespace.prefix:
403 finished_sizeof.append(self.c_sizeof_name)
404 _c_serialize('sizeof', self)
407 def _c_helper_absolute_name(prefix, field=None):
409 turn prefix, which is a list of tuples (name, separator, Type obj) into a string
410 representing a valid name in C (based on the context)
411 if field is not None, append the field name as well
414 for name, sep, obj in prefix:
418 if ((obj.is_bitcase and obj.has_name) or # named bitcase
419 (obj.is_switch and len(obj.parents)>1)):
422 if field is not None:
423 prefix_str += _cpp(field.field_name)
427 def _c_helper_field_mapping(complex_type, prefix, flat=False):
429 generate absolute names, based on prefix, for all fields starting from complex_type
430 if flat == True, nested complex types are not taken into account
433 if complex_type.is_switch:
434 for b in complex_type.bitcases:
436 switch_name, switch_sep, switch_type = prefix[-1]
437 bitcase_prefix = prefix + [(b.type.name[-1], '.', b.type)]
439 bitcase_prefix = prefix
441 if (True==flat and not b.type.has_name) or False==flat:
442 all_fields.update(_c_helper_field_mapping(b.type, bitcase_prefix, flat))
444 for f in complex_type.fields:
445 fname = _c_helper_absolute_name(prefix, f)
446 if all_fields.has_key(f.field_name):
447 raise Exception("field name %s has been registered before" % f.field_name)
449 all_fields[f.field_name] = (fname, f)
450 if f.type.is_container and flat==False:
451 if f.type.is_bitcase and not f.type.has_name:
453 elif f.type.is_switch and len(f.type.parents)>1:
454 # nested switch gets another separator
455 new_prefix = prefix+[(f.c_field_name, '.', f.type)]
457 new_prefix = prefix+[(f.c_field_name, '->', f.type)]
458 all_fields.update(_c_helper_field_mapping(f.type, new_prefix, flat))
463 def _c_helper_resolve_field_names (prefix):
465 get field names for all objects in the prefix array
469 # look for fields in the remaining containers
470 for idx, p in enumerate(prefix):
473 # sep can be preset in prefix, if not, make a sensible guess
474 sep = '.' if (obj.is_switch or obj.is_bitcase) else '->'
475 # exception: 'toplevel' object (switch as well!) always have sep '->'
476 sep = '->' if idx<1 else sep
477 if not obj.is_bitcase or (obj.is_bitcase and obj.has_name):
478 tmp_prefix.append((name, sep, obj))
479 all_fields.update(_c_helper_field_mapping(obj, tmp_prefix, flat=True))
482 # _c_helper_resolve_field_names
484 def get_expr_fields(self):
486 get the Fields referenced by switch or list expression
488 def get_expr_field_names(expr):
490 if expr.lenfield_name is not None:
491 return [expr.lenfield_name]
493 # constant value expr
497 return get_expr_field_names(expr.rhs)
498 elif expr.op == 'popcount':
499 return get_expr_field_names(expr.rhs)
500 elif expr.op == 'sumof':
501 # sumof expr references another list,
502 # we need that list's length field here
504 for f in expr.lenfield_parent.fields:
505 if f.field_name == expr.lenfield_name:
509 raise Exception("list field '%s' referenced by sumof not found" % expr.lenfield_name)
510 # referenced list + its length field
511 return [expr.lenfield_name] + get_expr_field_names(field.type.expr)
512 elif expr.op == 'enumref':
515 return get_expr_field_names(expr.lhs) + get_expr_field_names(expr.rhs)
516 # get_expr_field_names()
518 # resolve the field names with the parent structure(s)
519 unresolved_fields_names = get_expr_field_names(self.expr)
521 # construct prefix from self
522 prefix = [('', '', p) for p in self.parents]
523 if self.is_container:
524 prefix.append(('', '', self))
526 all_fields = _c_helper_resolve_field_names (prefix)
527 resolved_fields_names = filter(lambda x: x in all_fields.keys(), unresolved_fields_names)
528 if len(unresolved_fields_names) != len(resolved_fields_names):
529 raise Exception("could not resolve all fields for %s" % self.name)
531 resolved_fields = [all_fields[n][1] for n in resolved_fields_names]
532 return resolved_fields
535 def resolve_expr_fields(complex_obj):
537 find expr fields appearing in complex_obj and descendents that cannot be resolved within complex_obj
538 these are normally fields that need to be given as function parameters
544 for field in complex_obj.fields:
545 all_fields.append(field)
546 if field.type.is_switch or field.type.is_list:
547 expr_fields += get_expr_fields(field.type)
548 if field.type.is_container:
549 expr_fields += resolve_expr_fields(field.type)
551 # try to resolve expr fields
552 for e in expr_fields:
553 if e not in all_fields and e not in unresolved:
556 # resolve_expr_fields()
558 def get_serialize_params(context, self, buffer_var='_buffer', aux_var='_aux'):
560 functions like _serialize(), _unserialize(), and _unpack() sometimes need additional parameters:
561 E.g. in order to unpack switch, extra parameters might be needed to evaluate the switch
562 expression. This function tries to resolve all fields within a structure, and returns the
563 unresolved fields as the list of external parameters.
565 def add_param(params, param):
566 if param not in params:
569 # collect all fields into param_fields
573 for field in self.fields:
575 # the field should appear as a parameter in the function call
576 param_fields.append(field)
577 if field.wire and not field.auto:
578 if field.type.fixed_size() and not self.is_switch:
579 # field in the xcb_out structure
580 wire_fields.append(field)
581 # fields like 'pad0' are skipped!
583 # in case of switch, parameters always contain any fields referenced in the switch expr
584 # we do not need any variable size fields here, as the switch data type contains both
585 # fixed and variable size fields
587 param_fields = get_expr_fields(self)
589 # _serialize()/_unserialize()/_unpack() function parameters
590 # note: don't use set() for params, it is unsorted
593 # 1. the parameter for the void * buffer
594 if 'serialize' == context:
595 params.append(('void', '**', buffer_var))
596 elif context in ('unserialize', 'unpack', 'sizeof'):
597 params.append(('const void', '*', buffer_var))
599 # 2. any expr fields that cannot be resolved within self and descendants
600 unresolved_fields = resolve_expr_fields(self)
601 for f in unresolved_fields:
602 add_param(params, (f.c_field_type, '', f.c_field_name))
604 # 3. param_fields contain the fields necessary to evaluate the switch expr or any other fields
605 # that do not appear in the data type struct
606 for p in param_fields:
608 typespec = p.c_field_const_type
609 pointerspec = p.c_pointer
610 add_param(params, (typespec, pointerspec, p.c_field_name))
612 if p.visible and not p.wire and not p.auto:
613 typespec = p.c_field_type
615 add_param(params, (typespec, pointerspec, p.c_field_name))
618 if 'serialize' == context:
619 add_param(params, ('const %s' % self.c_type, '*', aux_var))
620 elif 'unserialize' == context:
621 add_param(params, ('%s' % self.c_type, '**', aux_var))
622 elif 'unpack' == context:
623 add_param(params, ('%s' % self.c_type, '*', aux_var))
625 # 5. switch contains all variable size fields as struct members
626 # for other data types though, these have to be supplied separately
627 # this is important for the special case of intermixed fixed and
628 # variable size fields
629 if not self.is_switch and 'serialize' == context:
630 for p in param_fields:
631 if not p.type.fixed_size():
632 add_param(params, (p.c_field_const_type, '*', p.c_field_name))
634 return (param_fields, wire_fields, params)
635 # get_serialize_params()
637 def _c_serialize_helper_insert_padding(context, code_lines, space, postpone):
638 code_lines.append('%s /* insert padding */' % space)
639 code_lines.append('%s xcb_pad = -xcb_block_len & 3;' % space)
640 # code_lines.append('%s printf("automatically inserting padding: %%%%d\\n", xcb_pad);' % space)
641 code_lines.append('%s xcb_buffer_len += xcb_block_len + xcb_pad;' % space)
644 code_lines.append('%s if (0 != xcb_pad) {' % space)
646 if 'serialize' == context:
647 code_lines.append('%s xcb_parts[xcb_parts_idx].iov_base = xcb_pad0;' % space)
648 code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = xcb_pad;' % space)
649 code_lines.append('%s xcb_parts_idx++;' % space)
650 elif context in ('unserialize', 'unpack', 'sizeof'):
651 code_lines.append('%s xcb_tmp += xcb_pad;' % space)
653 code_lines.append('%s xcb_pad = 0;' % space)
654 code_lines.append('%s }' % space)
656 code_lines.append('%s xcb_block_len = 0;' % space)
658 # keep tracking of xcb_parts entries for serialize
660 # _c_serialize_helper_insert_padding()
662 def _c_serialize_helper_switch(context, self, complex_name,
663 code_lines, temp_vars,
666 switch_expr = _c_accessor_get_expr(self.expr, None)
668 for b in self.bitcases:
669 bitcase_expr = _c_accessor_get_expr(b.type.expr, None)
670 code_lines.append(' if(%s & %s) {' % (switch_expr, bitcase_expr))
671 # code_lines.append(' printf("switch %s: entering bitcase section %s (mask=%%%%d)...\\n", %s);' %
672 # (self.name[-1], b.type.name[-1], bitcase_expr))
675 b_prefix = prefix + [(b.c_field_name, '.', b.type)]
677 count += _c_serialize_helper_fields(context, b.type,
678 code_lines, temp_vars,
682 code_lines.append(' }')
684 # if 'serialize' == context:
685 # count += _c_serialize_helper_insert_padding(context, code_lines, space, False)
686 # elif context in ('unserialize', 'unpack', 'sizeof'):
688 # code_lines.append('%s xcb_pad = -xcb_block_len & 3;' % space)
689 # code_lines.append('%s xcb_buffer_len += xcb_block_len + xcb_pad;' % space)
692 # _c_serialize_helper_switch
694 def _c_serialize_helper_switch_field(context, self, field, c_switch_variable, prefix):
696 handle switch by calling _serialize() or _unpack(), depending on context
698 # switch is handled by this function as a special case
699 param_fields, wire_fields, params = get_serialize_params(context, self)
700 field_mapping = _c_helper_field_mapping(self, prefix)
701 prefix_str = _c_helper_absolute_name(prefix)
703 # find the parameters that need to be passed to _serialize()/_unpack():
704 # all switch expr fields must be given as parameters
705 args = get_expr_fields(field.type)
706 # length fields for variable size types in switch, normally only some of need
707 # need to be passed as parameters
708 switch_len_fields = resolve_expr_fields(field.type)
710 # a switch field at this point _must_ be a bitcase field
711 # we require that bitcases are "self-contiguous"
712 bitcase_unresolved = resolve_expr_fields(self)
713 if len(bitcase_unresolved) != 0:
714 raise Exception('unresolved fields within bitcase is not supported at this point')
716 # get the C names for the parameters
718 for a in switch_len_fields:
719 c_field_names += "%s, " % field_mapping[a.c_field_name][0]
721 c_field_names += "%s, " % field_mapping[a.c_field_name][0]
723 # call _serialize()/_unpack() to determine the actual size
724 if 'serialize' == context:
725 length = "%s(&%s, %s&%s%s)" % (field.type.c_serialize_name, c_switch_variable,
726 c_field_names, prefix_str, field.c_field_name)
727 elif context in ('unserialize', 'unpack'):
728 length = "%s(xcb_tmp, %s&%s%s)" % (field.type.c_unpack_name,
729 c_field_names, prefix_str, field.c_field_name)
732 # _c_serialize_helper_switch_field()
734 def _c_serialize_helper_list_field(context, self, field,
735 code_lines, temp_vars,
738 helper function to cope with lists of variable length
740 expr = field.type.expr
741 prefix_str = _c_helper_absolute_name(prefix)
742 param_fields, wire_fields, params = get_serialize_params('sizeof', self)
743 param_names = [p[2] for p in params]
745 expr_fields_names = [f.field_name for f in get_expr_fields(field.type)]
746 resolved = filter(lambda x: x in param_names, expr_fields_names)
747 unresolved = filter(lambda x: x not in param_names, expr_fields_names)
751 field_mapping[r] = (r, None)
753 if len(unresolved)>0:
755 if len(tmp_prefix)==0:
756 raise Exception("found an empty prefix while resolving expr field names for list %s",
759 field_mapping.update(_c_helper_resolve_field_names(prefix))
760 resolved += filter(lambda x: x in field_mapping, unresolved)
761 unresolved = filter(lambda x: x not in field_mapping, unresolved)
762 if len(unresolved)>0:
763 raise Exception('could not resolve the length fields required for list %s' % field.c_field_name)
765 list_length = _c_accessor_get_expr(expr, field_mapping)
767 # default: list with fixed size elements
768 length = '%s * sizeof(%s)' % (list_length, field.type.member.c_wiretype)
770 # list with variable-sized elements
771 if not field.type.member.fixed_size():
773 if context in ('unserialize', 'sizeof', 'unpack'):
774 int_i = ' unsigned int i;'
775 xcb_tmp_len = ' unsigned int xcb_tmp_len;'
776 if int_i not in temp_vars:
777 temp_vars.append(int_i)
778 if xcb_tmp_len not in temp_vars:
779 temp_vars.append(xcb_tmp_len)
780 # loop over all list elements and call sizeof repeatedly
781 # this should be a bit faster than using the iterators
782 code_lines.append("%s for(i=0; i<%s; i++) {" % (space, list_length))
783 code_lines.append("%s xcb_tmp_len = %s(xcb_tmp);" %
784 (space, field.type.c_sizeof_name))
785 code_lines.append("%s xcb_block_len += xcb_tmp_len;" % space)
786 code_lines.append("%s xcb_tmp += xcb_tmp_len;" % space)
787 code_lines.append("%s }" % space)
789 elif 'serialize' == context:
790 code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = 0;' % space)
791 code_lines.append('%s xcb_tmp = (char *) %s%s;' % (space, prefix_str, field.c_field_name))
792 code_lines.append('%s for(i=0; i<%s; i++) { ' % (space, list_length))
793 code_lines.append('%s xcb_block_len = %s(xcb_tmp);' % (space, field.type.c_sizeof_name))
794 code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len += xcb_block_len;' % space)
795 code_lines.append('%s }' % space)
796 code_lines.append('%s xcb_block_len = xcb_parts[xcb_parts_idx].iov_len;' % space)
799 # _c_serialize_helper_list_field()
801 def _c_serialize_helper_fields_fixed_size(context, self, field,
802 code_lines, temp_vars,
804 # keep the C code a bit more readable by giving the field name
805 if not self.is_bitcase:
806 code_lines.append('%s /* %s.%s */' % (space, self.c_type, field.c_field_name))
808 scoped_name = [p[2].c_type if idx==0 else p[0] for idx, p in enumerate(prefix)]
809 typename = reduce(lambda x,y: "%s.%s" % (x, y), scoped_name)
810 code_lines.append('%s /* %s.%s */' % (space, typename, field.c_field_name))
812 abs_field_name = _c_helper_absolute_name(prefix, field)
813 # default for simple cases: call sizeof()
814 length = "sizeof(%s)" % field.c_field_type
816 if context in ('unserialize', 'unpack', 'sizeof'):
817 # default: simple cast
818 value = ' %s = *(%s *)xcb_tmp;' % (abs_field_name, field.c_field_type)
820 # padding - we could probably just ignore it
821 if field.type.is_pad and field.type.nmemb > 1:
823 for i in range(field.type.nmemb):
824 code_lines.append('%s %s[%d] = *(%s *)xcb_tmp;' %
825 (space, abs_field_name, i, field.c_field_type))
826 # total padding = sizeof(pad0) * nmemb
827 length += " * %d" % field.type.nmemb
829 if field.type.is_list:
830 # no such case in the protocol, cannot be tested and therefore ignored for now
831 raise Exception('list with fixed number of elemens unhandled in _unserialize()')
833 elif 'serialize' == context:
834 value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) '
836 if field.type.is_expr:
837 # need to register a temporary variable for the expression in case we know its type
838 if field.type.c_type is None:
839 raise Exception("type for field '%s' (expression '%s') unkown" %
840 (field.field_name, _c_accessor_get_expr(field.type.expr)))
842 temp_vars.append(' %s xcb_expr_%s = %s;' % (field.type.c_type, _cpp(field.field_name),
843 _c_accessor_get_expr(field.type.expr, prefix)))
844 value += "&xcb_expr_%s;" % _cpp(field.field_name)
846 elif field.type.is_pad:
847 if field.type.nmemb == 1:
850 # we could also set it to 0, see definition of xcb_send_request()
851 value = ' xcb_parts[xcb_parts_idx].iov_base = xcb_pad0;'
852 length += "*%d" % field.type.nmemb
855 # non-list type with fixed size
856 if field.type.nmemb == 1:
857 value += "&%s;" % (abs_field_name)
859 # list with nmemb (fixed size) elements
861 value += '%s;' % (abs_field_name)
862 length = '%d' % field.type.nmemb
864 return (value, length)
865 # _c_serialize_helper_fields_fixed_size()
867 def _c_serialize_helper_fields_variable_size(context, self, field,
868 code_lines, temp_vars,
870 prefix_str = _c_helper_absolute_name(prefix)
872 if context in ('unserialize', 'unpack', 'sizeof'):
874 var_field_name = 'xcb_tmp'
876 # special case: intermixed fixed and variable size fields
877 if self.var_followed_by_fixed_fields and 'unserialize' == context:
878 value = ' %s = xcb_tmp;' % field.c_field_name
879 temp_vars.append(' %s *%s;' % (field.type.c_type, field.c_field_name))
880 # special case: switch
881 if 'unpack' == context:
882 value = ' %s%s = (%s *)xcb_tmp;' % (prefix_str, field.c_field_name, field.c_field_type)
884 elif 'serialize' == context:
885 # variable size fields appear as parameters to _serialize() if the
886 # 'toplevel' container is not a switch
887 prefix_string = prefix_str if prefix[0][2].is_switch else ''
888 var_field_name = "%s%s" % (prefix_string, field.c_field_name)
889 value = ' xcb_parts[xcb_parts_idx].iov_base = (char *) %s;' % var_field_name
893 code_lines.append('%s /* %s */' % (space, field.c_field_name))
895 if field.type.is_list:
897 # in any context, list is already a pointer, so the default assignment is ok
898 code_lines.append("%s%s" % (space, value))
900 length = _c_serialize_helper_list_field(context, self, field,
901 code_lines, temp_vars,
904 elif field.type.is_switch:
906 if context == 'serialize':
907 # the _serialize() function allocates the correct amount memory if given a NULL pointer
908 value = ' xcb_parts[xcb_parts_idx].iov_base = (char *)0;'
909 length = _c_serialize_helper_switch_field(context, self, field,
910 'xcb_parts[xcb_parts_idx].iov_base',
914 # in all remaining special cases - call _sizeof()
915 length = "%s(%s)" % (field.type.c_sizeof_name, var_field_name)
917 return (value, length)
918 # _c_serialize_helper_fields_variable_size
920 def _c_serialize_helper_fields(context, self,
921 code_lines, temp_vars,
922 space, prefix, is_bitcase):
925 prev_field_was_variable = False
927 for field in self.fields:
928 if not ((field.wire and not field.auto) or field.visible):
931 # switch/bitcase: fixed size fields must be considered explicitly
932 if field.type.fixed_size():
933 if self.is_bitcase or self.var_followed_by_fixed_fields:
934 if prev_field_was_variable and need_padding:
936 # count += _c_serialize_helper_insert_padding(context, code_lines, space,
937 # self.var_followed_by_fixed_fields)
938 prev_field_was_variable = False
940 # prefix for fixed size fields
941 fixed_prefix = prefix
943 value, length = _c_serialize_helper_fields_fixed_size(context, self, field,
944 code_lines, temp_vars,
949 # fields with variable size
951 # switch/bitcase: always calculate padding before and after variable sized fields
952 if need_padding or is_bitcase:
953 count += _c_serialize_helper_insert_padding(context, code_lines, space,
954 self.var_followed_by_fixed_fields)
956 value, length = _c_serialize_helper_fields_variable_size(context, self, field,
957 code_lines, temp_vars,
959 prev_field_was_variable = True
961 # save (un)serialization C code
963 code_lines.append('%s%s' % (space, value))
965 if field.type.fixed_size():
966 if is_bitcase or self.var_followed_by_fixed_fields:
967 # keep track of (un)serialized object's size
968 code_lines.append('%s xcb_block_len += %s;' % (space, length))
969 if context in ('unserialize', 'unpack', 'sizeof'):
970 code_lines.append('%s xcb_tmp += %s;' % (space, length))
972 # variable size objects or bitcase:
973 # value & length might have been inserted earlier for special cases
975 # special case: intermixed fixed and variable size fields
976 if (not field.type.fixed_size() and
977 self.var_followed_by_fixed_fields and 'unserialize' == context):
978 temp_vars.append(' int %s_len;' % field.c_field_name)
979 code_lines.append('%s %s_len = %s;' % (space, field.c_field_name, length))
980 code_lines.append('%s xcb_block_len += %s_len;' % (space, field.c_field_name))
981 code_lines.append('%s xcb_tmp += %s_len;' % (space, field.c_field_name))
983 code_lines.append('%s xcb_block_len += %s;' % (space, length))
984 # increase pointer into the byte stream accordingly
985 if context in ('unserialize', 'sizeof', 'unpack'):
986 code_lines.append('%s xcb_tmp += xcb_block_len;' % space)
988 if 'serialize' == context:
990 code_lines.append('%s xcb_parts[xcb_parts_idx].iov_len = %s;' % (space, length))
991 code_lines.append('%s xcb_parts_idx++;' % space)
995 if self.var_followed_by_fixed_fields:
999 # _c_serialize_helper_fields()
1001 def _c_serialize_helper(context, complex_type,
1002 code_lines, temp_vars,
1003 space='', prefix=[]):
1004 # count tracks the number of fields to serialize
1007 if hasattr(complex_type, 'type'):
1008 self = complex_type.type
1009 complex_name = complex_type.name
1012 if self.var_followed_by_fixed_fields and 'unserialize' == context:
1013 complex_name = 'xcb_out'
1015 complex_name = '_aux'
1017 # special case: switch is serialized by evaluating each bitcase separately
1019 count += _c_serialize_helper_switch(context, self, complex_name,
1020 code_lines, temp_vars,
1023 # all other data types can be evaluated one field a time
1025 # unserialize & fixed size fields: simply cast the buffer to the respective xcb_out type
1026 if context in ('unserialize', 'unpack', 'sizeof') and not self.var_followed_by_fixed_fields:
1027 code_lines.append('%s xcb_block_len += sizeof(%s);' % (space, self.c_type))
1028 code_lines.append('%s xcb_tmp += xcb_block_len;' % space)
1029 # probably not needed
1030 #_c_serialize_helper_insert_padding(context, code_lines, space, False)
1032 count += _c_serialize_helper_fields(context, self,
1033 code_lines, temp_vars,
1034 space, prefix, False)
1036 count += _c_serialize_helper_insert_padding(context, code_lines, space, False)
1039 # _c_serialize_helper()
1041 def _c_serialize(context, self):
1043 depending on the context variable, generate _serialize(), _unserialize(), _unpack(), or _sizeof()
1044 for the ComplexType variable self
1050 # _serialize() returns the buffer size
1053 if self.is_switch and 'unserialize' == context:
1056 cases = { 'serialize' : self.c_serialize_name,
1057 'unserialize' : self.c_unserialize_name,
1058 'unpack' : self.c_unpack_name,
1059 'sizeof' : self.c_sizeof_name }
1060 func_name = cases[context]
1062 param_fields, wire_fields, params = get_serialize_params(context, self)
1063 variable_size_fields = 0
1064 # maximum space required for type definition of function arguments
1067 # determine N(variable_fields)
1068 for field in param_fields:
1069 # if self.is_switch, treat all fields as if they are variable sized
1070 if not field.type.fixed_size() or self.is_switch:
1071 variable_size_fields += 1
1072 # determine maxtypelen
1074 maxtypelen = max(maxtypelen, len(p[0]) + len(p[1]))
1077 indent = ' '*(len(func_name)+2)
1080 typespec, pointerspec, field_name = p
1081 spacing = ' '*(maxtypelen-len(typespec)-len(pointerspec))
1082 param_str.append("%s%s%s %s%s /**< */" % (indent, typespec, spacing, pointerspec, field_name))
1083 # insert function name
1084 param_str[0] = "%s (%s" % (func_name, param_str[0].strip())
1085 param_str = map(lambda x: "%s," % x, param_str)
1086 for s in param_str[:-1]:
1088 _h("%s);" % param_str[-1].rstrip(','))
1089 _c("%s)" % param_str[-1].rstrip(','))
1096 if 'serialize' == context:
1097 if not self.is_switch and not self.var_followed_by_fixed_fields:
1098 _c(' %s *xcb_out = *_buffer;', self.c_type)
1099 _c(' unsigned int xcb_out_pad = -sizeof(%s) & 3;', self.c_type)
1100 _c(' unsigned int xcb_buffer_len = sizeof(%s) + xcb_out_pad;', self.c_type)
1102 _c(' char *xcb_out = *_buffer;')
1103 _c(' unsigned int xcb_buffer_len = 0;')
1104 prefix = [('_aux', '->', self)]
1107 elif context in ('unserialize', 'unpack'):
1108 _c(' char *xcb_tmp = (char *)_buffer;')
1109 if not self.is_switch:
1110 if not self.var_followed_by_fixed_fields:
1111 _c(' const %s *_aux = (%s *)_buffer;', self.c_type, self.c_type)
1112 prefix = [('_aux', '->', self)]
1114 _c(' %s xcb_out;', self.c_type)
1115 prefix = [('xcb_out', '.', self)]
1117 aux_var = '_aux' # default for unpack: single pointer
1118 # note: unserialize not generated for switch
1119 if 'unserialize' == context:
1120 aux_var = '(*_aux)' # unserialize: double pointer (!)
1121 prefix = [(aux_var, '->', self)]
1123 _c(' unsigned int xcb_buffer_len = 0;')
1124 _c(' unsigned int xcb_block_len = 0;')
1125 _c(' unsigned int xcb_pad = 0;')
1127 elif 'sizeof' == context:
1128 param_names = [p[2] for p in params]
1130 # switch: call _unpack()
1131 _c(' %s _aux;', self.c_type)
1132 _c(' return %s(%s, &_aux);', self.c_unpack_name, reduce(lambda x,y: "%s, %s" % (x, y), param_names))
1135 elif self.var_followed_by_fixed_fields:
1136 # special case: call _unserialize()
1137 _c(' return %s(%s, NULL);', self.c_unserialize_name, reduce(lambda x,y: "%s, %s" % (x, y), param_names))
1141 _c(' char *xcb_tmp = (char *)_buffer;')
1142 prefix = [('_aux', '->', self)]
1144 count = _c_serialize_helper(context, self, code_lines, temp_vars, prefix=prefix)
1145 # update variable size fields (only important for context=='serialize'
1146 variable_size_fields = count
1147 if 'serialize' == context:
1148 temp_vars.append(' unsigned int xcb_pad = 0;')
1149 temp_vars.append(' char xcb_pad0[3] = {0, 0, 0};')
1150 temp_vars.append(' struct iovec xcb_parts[%d];' % count)
1151 temp_vars.append(' unsigned int xcb_parts_idx = 0;')
1152 temp_vars.append(' unsigned int xcb_block_len = 0;')
1153 temp_vars.append(' unsigned int i;')
1154 temp_vars.append(' char *xcb_tmp;')
1155 elif 'sizeof' == context:
1156 # neither switch nor intermixed fixed and variable size fields:
1157 # evaluate parameters directly
1158 if not (self.is_switch or self.var_followed_by_fixed_fields):
1160 # look if we have to declare an '_aux' variable at all
1161 if len(filter(lambda x: x.find('_aux')!=-1, code_lines))>0:
1162 if not self.var_followed_by_fixed_fields:
1163 _c(' const %s *_aux = (%s *)_buffer;', self.c_type, self.c_type)
1165 _c(' %s *_aux = malloc(sizeof(%s));', self.c_type, self.c_type)
1167 _c(' unsigned int xcb_buffer_len = 0;')
1168 _c(' unsigned int xcb_block_len = 0;')
1169 _c(' unsigned int xcb_pad = 0;')
1175 for l in code_lines:
1178 # variable sized fields have been collected, now
1179 # allocate memory and copy everything into a continuous memory area
1180 # note: this is not necessary in case of unpack
1181 if context in ('serialize', 'unserialize'):
1182 # unserialize: check for sizeof-only invocation
1183 if 'unserialize' == context:
1185 _c(' if (NULL == _aux)')
1186 _c(' return xcb_buffer_len;')
1189 _c(' if (NULL == %s) {', aux_ptr)
1190 _c(' /* allocate memory */')
1191 _c(' %s = malloc(xcb_buffer_len);', aux_ptr)
1192 if 'serialize' == context:
1193 _c(' *_buffer = xcb_out;')
1197 # serialize: handle variable size fields in a loop
1198 if 'serialize' == context:
1199 if not self.is_switch and not self.var_followed_by_fixed_fields:
1200 if len(wire_fields)>0:
1201 _c(' *xcb_out = *_aux;')
1202 # copy variable size fields into the buffer
1203 if variable_size_fields > 0:
1205 if not self.is_switch and not self.var_followed_by_fixed_fields:
1206 _c(' xcb_tmp = (char*)++xcb_out;')
1207 _c(' xcb_tmp += xcb_out_pad;')
1209 _c(' xcb_tmp = xcb_out;')
1211 # variable sized fields
1212 _c(' for(i=0; i<xcb_parts_idx; i++) {')
1213 _c(' if (0 != xcb_parts[i].iov_base && 0 != xcb_parts[i].iov_len)')
1214 _c(' memcpy(xcb_tmp, xcb_parts[i].iov_base, xcb_parts[i].iov_len);')
1215 _c(' if (0 != xcb_parts[i].iov_len)')
1216 _c(' xcb_tmp += xcb_parts[i].iov_len;')
1219 # unserialize: assign variable size fields individually
1220 if 'unserialize' == context:
1221 _c(' *%s = xcb_out;', aux_ptr)
1222 _c(' xcb_tmp = (char *)(*_aux+1);')
1223 for field in param_fields:
1224 if not field.type.fixed_size():
1225 _c(' memcpy(xcb_tmp, %s, %s_len);', field.c_field_name, field.c_field_name)
1226 _c(' xcb_tmp += %s_len;', field.c_field_name)
1229 _c(' return xcb_buffer_len;')
1233 def _c_iterator_get_end(field, accum):
1235 Figures out what C code is needed to find the end of a variable-length structure field.
1236 For nested structures, recurses into its last variable-sized field.
1237 For lists, calls the end function
1239 if field.type.is_container:
1240 accum = field.c_accessor_name + '(' + accum + ')'
1241 return _c_iterator_get_end(field.type.last_varsized_field, accum)
1242 if field.type.is_list:
1243 # XXX we can always use the first way
1244 if field.type.member.is_simple:
1245 return field.c_end_name + '(' + accum + ')'
1247 return field.type.member.c_end_name + '(' + field.c_iterator_name + '(' + accum + '))'
1249 def _c_iterator(self, name):
1251 Declares the iterator structure and next/end functions for a given type.
1256 _h(' * @brief %s', self.c_iterator_type)
1258 _h('typedef struct %s {', self.c_iterator_type)
1259 _h(' %s *data; /**< */', self.c_type)
1260 _h(' int%s rem; /**< */', ' ' * (len(self.c_type) - 2))
1261 _h(' int%s index; /**< */', ' ' * (len(self.c_type) - 2))
1262 _h('} %s;', self.c_iterator_type)
1268 _h(' * Get the next element of the iterator')
1269 _h(' * @param i Pointer to a %s', self.c_iterator_type)
1271 _h(' * Get the next element in the iterator. The member rem is')
1272 _h(' * decreased by one. The member data points to the next')
1273 _h(' * element. The member index is increased by sizeof(%s)', self.c_type)
1277 _hc('/*****************************************************************************')
1279 _hc(' ** void %s', self.c_next_name)
1281 _hc(' ** @param %s *i', self.c_iterator_type)
1282 _hc(' ** @returns void')
1284 _hc(' *****************************************************************************/')
1287 _h('%s (%s *i /**< */);', self.c_next_name, self.c_iterator_type)
1288 _c('%s (%s *i /**< */)', self.c_next_name, self.c_iterator_type)
1291 if not self.fixed_size():
1292 _c(' %s *R = i->data;', self.c_type)
1295 # FIXME - how to determine the size of a variable size union??
1296 _c(' /* FIXME - determine the size of the union %s */', self.c_type)
1298 if self.need_sizeof:
1299 _c(' xcb_generic_iterator_t child;')
1300 _c(' child.data = (%s *)(((char *)R) + %s(R));',
1301 self.c_type, self.c_sizeof_name)
1302 _c(' i->index = (char *) child.data - (char *) i->data;')
1304 _c(' xcb_generic_iterator_t child = %s;', _c_iterator_get_end(self.last_varsized_field, 'R'))
1305 _c(' i->index = child.index;')
1307 _c(' i->data = (%s *) child.data;', self.c_type)
1312 _c(' i->index += sizeof(%s);', self.c_type)
1318 _h(' * Return the iterator pointing to the last element')
1319 _h(' * @param i An %s', self.c_iterator_type)
1320 _h(' * @return The iterator pointing to the last element')
1322 _h(' * Set the current element in the iterator to the last element.')
1323 _h(' * The member rem is set to 0. The member data points to the')
1324 _h(' * last element.')
1328 _hc('/*****************************************************************************')
1330 _hc(' ** xcb_generic_iterator_t %s', self.c_end_name)
1332 _hc(' ** @param %s i', self.c_iterator_type)
1333 _hc(' ** @returns xcb_generic_iterator_t')
1335 _hc(' *****************************************************************************/')
1337 _hc('xcb_generic_iterator_t')
1338 _h('%s (%s i /**< */);', self.c_end_name, self.c_iterator_type)
1339 _c('%s (%s i /**< */)', self.c_end_name, self.c_iterator_type)
1341 _c(' xcb_generic_iterator_t ret;')
1343 if self.fixed_size():
1344 _c(' ret.data = i.data + i.rem;')
1345 _c(' ret.index = i.index + ((char *) ret.data - (char *) i.data);')
1348 _c(' while(i.rem > 0)')
1349 _c(' %s(&i);', self.c_next_name)
1350 _c(' ret.data = i.data;')
1351 _c(' ret.rem = i.rem;')
1352 _c(' ret.index = i.index;')
1357 def _c_accessor_get_length(expr, field_mapping=None):
1359 Figures out what C code is needed to get a length field.
1360 The field_mapping parameter can be used to change the absolute name of a length field.
1361 For fields that follow a variable-length field, use the accessor.
1362 Otherwise, just reference the structure field directly.
1365 lenfield_name = expr.lenfield_name
1366 if lenfield_name is not None:
1367 if field_mapping is not None:
1368 lenfield_name = field_mapping[lenfield_name][0]
1370 if expr.lenfield is not None and expr.lenfield.prev_varsized_field is not None:
1371 # special case: variable and fixed size fields are intermixed
1372 # if the lenfield is among the fixed size fields, there is no need
1373 # to call a special accessor function like <expr.lenfield.c_accessor_name + '(' + prefix + ')'>
1374 return field_mapping(expr.lenfield_name)
1375 elif expr.lenfield_name is not None:
1376 return lenfield_name
1378 return str(expr.nmemb)
1380 def _c_accessor_get_expr(expr, field_mapping):
1382 Figures out what C code is needed to get the length of a list field.
1383 The field_mapping parameter can be used to change the absolute name of a length field.
1384 Recurses for math operations.
1385 Returns bitcount for value-mask fields.
1386 Otherwise, uses the value of the length field.
1388 lenexp = _c_accessor_get_length(expr, field_mapping)
1391 return '(' + '~' + _c_accessor_get_expr(expr.rhs, field_mapping) + ')'
1392 elif expr.op == 'popcount':
1393 return 'xcb_popcount(' + _c_accessor_get_expr(expr.rhs, field_mapping) + ')'
1394 elif expr.op == 'enumref':
1395 enum_name = expr.lenfield_type.name
1396 constant_name = expr.lenfield_name
1397 c_name = _n(enum_name + (constant_name,)).upper()
1399 elif expr.op == 'sumof':
1400 # locate the referenced list object
1401 list_obj = expr.lenfield_type
1403 for f in expr.lenfield_parent.fields:
1404 if f.field_name == expr.lenfield_name:
1409 raise Exception("list field '%s' referenced by sumof not found" % expr.lenfield_name)
1410 list_name = field_mapping[field.c_field_name][0]
1411 c_length_func = "%s(%s)" % (field.c_length_name, list_name)
1412 # note: xcb_sumof() has only been defined for integers
1413 c_length_func = _c_accessor_get_expr(field.type.expr, field_mapping)
1414 return 'xcb_sumof(%s, %s)' % (list_name, c_length_func)
1415 elif expr.op != None:
1416 return ('(' + _c_accessor_get_expr(expr.lhs, field_mapping) +
1417 ' ' + expr.op + ' ' +
1418 _c_accessor_get_expr(expr.rhs, field_mapping) + ')')
1420 return 'xcb_popcount(' + lenexp + ')'
1424 def _c_accessors_field(self, field):
1426 Declares the accessor functions for a non-list field that follows a variable-length field.
1428 c_type = self.c_type
1430 # special case: switch
1431 switch_obj = self if self.is_switch else None
1433 switch_obj = self.parents[-1]
1434 if switch_obj is not None:
1435 c_type = switch_obj.c_type
1437 if field.type.is_simple:
1440 _hc('/*****************************************************************************')
1442 _hc(' ** %s %s', field.c_field_type, field.c_accessor_name)
1444 _hc(' ** @param const %s *R', c_type)
1445 _hc(' ** @returns %s', field.c_field_type)
1447 _hc(' *****************************************************************************/')
1449 _hc('%s', field.c_field_type)
1450 _h('%s (const %s *R /**< */);', field.c_accessor_name, c_type)
1451 _c('%s (const %s *R /**< */)', field.c_accessor_name, c_type)
1453 if field.prev_varsized_field is None:
1454 _c(' return (%s *) (R + 1);', field.c_field_type)
1456 _c(' xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
1457 _c(' return * (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index) + %d);',
1458 field.c_field_type, field.first_field_after_varsized.type.c_type, field.prev_varsized_offset)
1463 _hc('/*****************************************************************************')
1465 _hc(' ** %s * %s', field.c_field_type, field.c_accessor_name)
1467 _hc(' ** @param const %s *R', c_type)
1468 _hc(' ** @returns %s *', field.c_field_type)
1470 _hc(' *****************************************************************************/')
1472 if field.type.is_switch and switch_obj is None:
1473 return_type = 'void *'
1475 return_type = '%s *' % field.c_field_type
1478 _h('%s (const %s *R /**< */);', field.c_accessor_name, c_type)
1479 _c('%s (const %s *R /**< */)', field.c_accessor_name, c_type)
1481 if field.prev_varsized_field is None:
1482 _c(' return (%s) (R + 1);', return_type)
1483 # note: the special case 'variable fields followed by fixed size fields'
1484 # is not of any consequence here, since the ordering gets
1485 # 'corrected' in the reply function
1487 _c(' xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
1488 _c(' return (%s) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index) + %d);',
1489 return_type, field.first_field_after_varsized.type.c_type, field.prev_varsized_offset)
1493 def _c_accessors_list(self, field):
1495 Declares the accessor functions for a list field.
1496 Declares a direct-accessor function only if the list members are fixed size.
1497 Declares length and get-iterator functions always.
1500 c_type = self.c_type
1502 # special case: switch
1503 # in case of switch, 2 params have to be supplied to certain accessor functions:
1504 # 1. the anchestor object (request or reply)
1505 # 2. the (anchestor) switch object
1506 # the reason is that switch is either a child of a request/reply or nested in another switch,
1507 # so whenever we need to access a length field, we might need to refer to some anchestor type
1508 switch_obj = self if self.is_switch else None
1510 switch_obj = self.parents[-1]
1511 if switch_obj is not None:
1512 c_type = switch_obj.c_type
1516 parents = self.parents if hasattr(self, 'parents') else [self]
1517 # 'R': parents[0] is always the 'toplevel' container type
1518 params.append(('const %s *R' % parents[0].c_type, parents[0]))
1519 fields.update(_c_helper_field_mapping(parents[0], [('R', '->', parents[0])], flat=True))
1520 # auxiliary object for 'R' parameters
1523 if switch_obj is not None:
1524 # now look where the fields are defined that are needed to evaluate
1525 # the switch expr, and store the parent objects in accessor_params and
1526 # the fields in switch_fields
1528 # 'S': name for the 'toplevel' switch
1529 toplevel_switch = parents[1]
1530 params.append(('const %s *S' % toplevel_switch.c_type, toplevel_switch))
1531 fields.update(_c_helper_field_mapping(toplevel_switch, [('S', '->', toplevel_switch)], flat=True))
1533 # initialize prefix for everything "below" S
1534 prefix_str = '/* %s */ S' % toplevel_switch.name[-1]
1535 prefix = [(prefix_str, '->', toplevel_switch)]
1537 # look for fields in the remaining containers
1538 for p in parents[2:] + [self]:
1539 # the separator between parent and child is always '.' here,
1540 # because of nested switch statements
1541 if not p.is_bitcase or (p.is_bitcase and p.has_name):
1542 prefix.append((p.name[-1], '.', p))
1543 fields.update(_c_helper_field_mapping(p, prefix, flat=True))
1545 # auxiliary object for 'S' parameter
1550 if list.member.fixed_size():
1551 idx = 1 if switch_obj is not None else 0
1554 _hc('/*****************************************************************************')
1556 _hc(' ** %s * %s', field.c_field_type, field.c_accessor_name)
1558 _hc(' ** @param %s', params[idx][0])
1559 _hc(' ** @returns %s *', field.c_field_type)
1561 _hc(' *****************************************************************************/')
1563 _hc('%s *', field.c_field_type)
1565 _h('%s (%s /**< */);', field.c_accessor_name, params[idx][0])
1566 _c('%s (%s /**< */)', field.c_accessor_name, params[idx][0])
1569 if switch_obj is not None:
1570 _c(' return %s;', fields[field.c_field_name][0])
1571 elif field.prev_varsized_field is None:
1572 _c(' return (%s *) (R + 1);', field.c_field_type)
1574 _c(' xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
1575 _c(' return (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index) + %d);',
1576 field.c_field_type, field.first_field_after_varsized.type.c_type, field.prev_varsized_offset)
1581 _hc('/*****************************************************************************')
1583 _hc(' ** int %s', field.c_length_name)
1585 _hc(' ** @param const %s *R', c_type)
1586 _hc(' ** @returns int')
1588 _hc(' *****************************************************************************/')
1591 if switch_obj is not None:
1592 _hc('%s (const %s *R /**< */,', field.c_length_name, R_obj.c_type)
1593 spacing = ' '*(len(field.c_length_name)+2)
1594 _h('%sconst %s *S /**< */);', spacing, S_obj.c_type)
1595 _c('%sconst %s *S /**< */)', spacing, S_obj.c_type)
1596 length = _c_accessor_get_expr(field.type.expr, fields)
1598 _h('%s (const %s *R /**< */);', field.c_length_name, c_type)
1599 _c('%s (const %s *R /**< */)', field.c_length_name, c_type)
1600 length = _c_accessor_get_expr(field.type.expr, fields)
1602 _c(' return %s;', length)
1605 if field.type.member.is_simple:
1608 _hc('/*****************************************************************************')
1610 _hc(' ** xcb_generic_iterator_t %s', field.c_end_name)
1612 _hc(' ** @param const %s *R', c_type)
1613 _hc(' ** @returns xcb_generic_iterator_t')
1615 _hc(' *****************************************************************************/')
1617 _hc('xcb_generic_iterator_t')
1618 if switch_obj is not None:
1619 _hc('%s (const %s *R /**< */,', field.c_end_name, R_obj.c_type)
1620 spacing = ' '*(len(field.c_end_name)+2)
1621 _h('%sconst %s *S /**< */);', spacing, S_obj.c_type)
1622 _c('%sconst %s *S /**< */)', spacing, S_obj.c_type)
1624 _h('%s (const %s *R /**< */);', field.c_end_name, c_type)
1625 _c('%s (const %s *R /**< */)', field.c_end_name, c_type)
1627 _c(' xcb_generic_iterator_t i;')
1629 param = 'R' if switch_obj is None else 'S'
1630 if switch_obj is not None:
1631 _c(' i.data = %s + %s;', fields[field.c_field_name][0],
1632 _c_accessor_get_expr(field.type.expr, fields))
1633 elif field.prev_varsized_field == None:
1634 _c(' i.data = ((%s *) (R + 1)) + (%s);', field.type.c_wiretype,
1635 _c_accessor_get_expr(field.type.expr, fields))
1637 _c(' xcb_generic_iterator_t child = %s;',
1638 _c_iterator_get_end(field.prev_varsized_field, 'R'))
1639 _c(' i.data = ((%s *) child.data) + (%s);', field.type.c_wiretype,
1640 _c_accessor_get_expr(field.type.expr, fields))
1643 _c(' i.index = (char *) i.data - (char *) %s;', param)
1650 _hc('/*****************************************************************************')
1652 _hc(' ** %s %s', field.c_iterator_type, field.c_iterator_name)
1654 _hc(' ** @param const %s *R', c_type)
1655 _hc(' ** @returns %s', field.c_iterator_type)
1657 _hc(' *****************************************************************************/')
1660 _hc('%s', field.c_iterator_type)
1661 if switch_obj is not None:
1662 _hc('%s (const %s *R /**< */,', field.c_iterator_name, R_obj.c_type)
1663 spacing = ' '*(len(field.c_iterator_name)+2)
1664 _h('%sconst %s *S /**< */);', spacing, S_obj.c_type)
1665 _c('%sconst %s *S /**< */)', spacing, S_obj.c_type)
1667 _h('%s (const %s *R /**< */);', field.c_iterator_name, c_type)
1668 _c('%s (const %s *R /**< */)', field.c_iterator_name, c_type)
1670 _c(' %s i;', field.c_iterator_type)
1672 if switch_obj is not None:
1673 _c(' i.data = %s;', fields[field.c_field_name][0])
1674 _c(' i.rem = %s;', _c_accessor_get_expr(field.type.expr, fields))
1675 elif field.prev_varsized_field == None:
1676 _c(' i.data = (%s *) (R + 1);', field.c_field_type)
1678 _c(' xcb_generic_iterator_t prev = %s;', _c_iterator_get_end(field.prev_varsized_field, 'R'))
1679 _c(' i.data = (%s *) ((char *) prev.data + XCB_TYPE_PAD(%s, prev.index));',
1680 field.c_field_type, field.c_field_type)
1681 if switch_obj is None:
1682 _c(' i.rem = %s;', _c_accessor_get_expr(field.type.expr, fields))
1683 _c(' i.index = (char *) i.data - (char *) %s;', 'R' if switch_obj is None else 'S' )
1687 def _c_accessors(self, name, base):
1689 Declares the accessor functions for the fields of a structure.
1691 # no accessors for switch itself -
1692 # switch always needs to be unpacked explicitly
1693 # if self.is_switch:
1697 for field in self.fields:
1698 if field.type.is_list and not field.type.fixed_size():
1699 _c_accessors_list(self, field)
1700 elif field.prev_varsized_field is not None or not field.type.fixed_size():
1701 _c_accessors_field(self, field)
1703 def c_simple(self, name):
1705 Exported function that handles cardinal type declarations.
1706 These are types which are typedef'd to one of the CARDx's, char, float, etc.
1708 _c_type_setup(self, name, ())
1710 if (self.name != name):
1715 _h('typedef %s %s;', _t(self.name), my_name)
1718 _c_iterator(self, name)
1720 def _c_complex(self):
1722 Helper function for handling all structure types.
1723 Called for all structs, requests, replies, events, errors.
1728 _h(' * @brief %s', self.c_type)
1730 _h('typedef %s %s {', self.c_container, self.c_type)
1736 for field in self.fields:
1737 if not field.type.fixed_size() and not self.is_switch and not self.is_union:
1738 varfield = field.c_field_name
1741 struct_fields.append(field)
1743 for field in struct_fields:
1744 length = len(field.c_field_type)
1745 # account for '*' pointer_spec
1746 if not field.type.fixed_size():
1748 maxtypelen = max(maxtypelen, length)
1750 def _c_complex_field(self, field, space=''):
1751 if (field.type.fixed_size() or
1752 # in case of switch with switch children, don't make the field a pointer
1753 # necessary for unserialize to work
1754 (self.is_switch and field.type.is_switch)):
1755 spacing = ' ' * (maxtypelen - len(field.c_field_type))
1756 _h('%s %s%s %s%s; /**< */', space, field.c_field_type, spacing, field.c_field_name, field.c_subscript)
1758 spacing = ' ' * (maxtypelen - (len(field.c_field_type) + 1))
1759 _h('%s %s%s *%s%s; /**< */', space, field.c_field_type, spacing, field.c_field_name, field.c_subscript)
1761 if not self.is_switch:
1762 for field in struct_fields:
1763 _c_complex_field(self, field)
1765 for b in self.bitcases:
1768 _h(' struct _%s {', b.c_field_name)
1770 for field in b.type.fields:
1771 _c_complex_field(self, field, space)
1773 _h(' } %s;', b.c_field_name)
1775 _h('} %s;', self.c_type)
1777 def c_struct(self, name):
1779 Exported function that handles structure declarations.
1781 _c_type_setup(self, name, ())
1783 _c_accessors(self, name, name)
1784 _c_iterator(self, name)
1786 def c_union(self, name):
1788 Exported function that handles union declarations.
1790 _c_type_setup(self, name, ())
1792 _c_iterator(self, name)
1794 def _c_request_helper(self, name, cookie_type, void, regular, aux=False):
1796 Declares a request function.
1799 # Four stunningly confusing possibilities here:
1802 # ------------------------------
1804 # 0 flag CHECKED flag Normal Mode
1805 # void_cookie req_cookie
1806 # ------------------------------
1807 # "req_checked" "req_unchecked"
1808 # CHECKED flag 0 flag Abnormal Mode
1809 # void_cookie req_cookie
1810 # ------------------------------
1813 # Whether we are _checked or _unchecked
1814 checked = void and not regular
1815 unchecked = not void and not regular
1817 # What kind of cookie we return
1818 func_cookie = 'xcb_void_cookie_t' if void else self.c_cookie_type
1820 # What flag is passed to xcb_request
1821 func_flags = '0' if (void and regular) or (not void and not regular) else 'XCB_REQUEST_CHECKED'
1823 # Global extension id variable or NULL for xproto
1824 func_ext_global = '&' + _ns.c_ext_global_name if _ns.is_ext else '0'
1826 # What our function name is
1827 func_name = self.c_request_name if not aux else self.c_aux_name
1829 func_name = self.c_checked_name if not aux else self.c_aux_checked_name
1831 func_name = self.c_unchecked_name if not aux else self.c_aux_unchecked_name
1835 maxtypelen = len('xcb_connection_t')
1837 # special case: list with variable size elements
1838 list_with_var_size_elems = False
1840 for field in self.fields:
1842 # The field should appear as a call parameter
1843 param_fields.append(field)
1844 if field.wire and not field.auto:
1845 # We need to set the field up in the structure
1846 wire_fields.append(field)
1847 if field.type.need_serialize or field.type.need_sizeof:
1848 serial_fields.append(field)
1850 for field in param_fields:
1851 c_field_const_type = field.c_field_const_type
1852 if field.type.need_serialize and not aux:
1853 c_field_const_type = "const void"
1854 if len(c_field_const_type) > maxtypelen:
1855 maxtypelen = len(c_field_const_type)
1856 if field.type.is_list and not field.type.member.fixed_size():
1857 list_with_var_size_elems = True
1863 _h(' * Delivers a request to the X server')
1864 _h(' * @param c The connection')
1865 _h(' * @return A cookie')
1867 _h(' * Delivers a request to the X server.')
1870 _h(' * This form can be used only if the request will not cause')
1871 _h(' * a reply to be generated. Any returned error will be')
1872 _h(' * saved for handling by xcb_request_check().')
1874 _h(' * This form can be used only if the request will cause')
1875 _h(' * a reply to be generated. Any returned error will be')
1876 _h(' * placed in the event queue.')
1880 _hc('/*****************************************************************************')
1882 _hc(' ** %s %s', cookie_type, func_name)
1885 spacing = ' ' * (maxtypelen - len('xcb_connection_t'))
1886 _hc(' ** @param xcb_connection_t%s *c', spacing)
1888 for field in param_fields:
1889 c_field_const_type = field.c_field_const_type
1890 if field.type.need_serialize and not aux:
1891 c_field_const_type = "const void"
1892 spacing = ' ' * (maxtypelen - len(c_field_const_type))
1893 _hc(' ** @param %s%s %s%s', c_field_const_type, spacing, field.c_pointer, field.c_field_name)
1895 _hc(' ** @returns %s', cookie_type)
1897 _hc(' *****************************************************************************/')
1899 _hc('%s', cookie_type)
1901 spacing = ' ' * (maxtypelen - len('xcb_connection_t'))
1902 comma = ',' if len(param_fields) else ');'
1903 _h('%s (xcb_connection_t%s *c /**< */%s', func_name, spacing, comma)
1904 comma = ',' if len(param_fields) else ')'
1905 _c('%s (xcb_connection_t%s *c /**< */%s', func_name, spacing, comma)
1907 func_spacing = ' ' * (len(func_name) + 2)
1908 count = len(param_fields)
1909 for field in param_fields:
1911 c_field_const_type = field.c_field_const_type
1912 c_pointer = field.c_pointer
1913 if field.type.need_serialize and not aux:
1914 c_field_const_type = "const void"
1916 spacing = ' ' * (maxtypelen - len(c_field_const_type))
1917 comma = ',' if count else ');'
1918 _h('%s%s%s %s%s /**< */%s', func_spacing, c_field_const_type,
1919 spacing, c_pointer, field.c_field_name, comma)
1920 comma = ',' if count else ')'
1921 _c('%s%s%s %s%s /**< */%s', func_spacing, c_field_const_type,
1922 spacing, c_pointer, field.c_field_name, comma)
1925 if not self.var_followed_by_fixed_fields:
1926 for field in param_fields:
1927 if not field.type.fixed_size():
1929 if field.type.need_serialize:
1930 # _serialize() keeps track of padding automatically
1932 dimension = count + 2
1935 _c(' static const xcb_protocol_request_t xcb_req = {')
1936 _c(' /* count */ %d,', count)
1937 _c(' /* ext */ %s,', func_ext_global)
1938 _c(' /* opcode */ %s,', self.c_request_name.upper())
1939 _c(' /* isvoid */ %d', 1 if void else 0)
1943 _c(' struct iovec xcb_parts[%d];', dimension)
1944 _c(' %s xcb_ret;', func_cookie)
1945 _c(' %s xcb_out;', self.c_type)
1946 if self.var_followed_by_fixed_fields:
1947 _c(' /* in the protocol description, variable size fields are followed by fixed size fields */')
1948 _c(' void *xcb_aux = 0;')
1951 for idx, f in enumerate(serial_fields):
1953 _c(' void *xcb_aux%d = 0;' % (idx))
1954 if list_with_var_size_elems:
1955 _c(' unsigned int i;')
1956 _c(' unsigned int xcb_tmp_len;')
1957 _c(' char *xcb_tmp;')
1959 # simple request call tracing
1960 # _c(' printf("in function %s\\n");' % func_name)
1963 for field in wire_fields:
1964 if field.type.fixed_size():
1965 if field.type.is_expr:
1966 _c(' xcb_out.%s = %s;', field.c_field_name, _c_accessor_get_expr(field.type.expr, None))
1967 elif field.type.is_pad:
1968 if field.type.nmemb == 1:
1969 _c(' xcb_out.%s = 0;', field.c_field_name)
1971 _c(' memset(xcb_out.%s, 0, %d);', field.c_field_name, field.type.nmemb)
1973 if field.type.nmemb == 1:
1974 _c(' xcb_out.%s = %s;', field.c_field_name, field.c_field_name)
1976 _c(' memcpy(xcb_out.%s, %s, %d);', field.c_field_name, field.c_field_name, field.type.nmemb)
1978 def get_serialize_args(type_obj, c_field_name, aux_var, context='serialize'):
1979 serialize_args = get_serialize_params(context, type_obj,
1982 return reduce(lambda x,y: "%s, %s" % (x,y), [a[2] for a in serialize_args])
1984 # calls in order to free dyn. all. memory
1988 if not self.var_followed_by_fixed_fields:
1989 _c(' xcb_parts[2].iov_base = (char *) &xcb_out;')
1990 _c(' xcb_parts[2].iov_len = sizeof(xcb_out);')
1991 _c(' xcb_parts[3].iov_base = 0;')
1992 _c(' xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;')
1996 for field in param_fields:
1997 if not field.type.fixed_size():
1998 _c(' /* %s %s */', field.type.c_type, field.c_field_name)
1999 # default: simple cast to char *
2000 if not field.type.need_serialize and not field.type.need_sizeof:
2001 _c(' xcb_parts[%d].iov_base = (char *) %s;', count, field.c_field_name)
2002 if field.type.is_list:
2003 if field.type.member.fixed_size():
2004 _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count,
2005 _c_accessor_get_expr(field.type.expr, None),
2006 field.type.member.c_wiretype)
2008 list_length = _c_accessor_get_expr(field.type.expr, None)
2011 _c(" xcb_parts[%d].iov_len = 0;" % count)
2012 _c(" xcb_tmp = (char *)%s;", field.c_field_name)
2013 _c(" for(i=0; i<%s; i++) {" % list_length)
2014 _c(" xcb_tmp_len = %s(xcb_tmp);" %
2015 (field.type.c_sizeof_name))
2016 _c(" xcb_parts[%d].iov_len += xcb_tmp_len;" % count)
2017 _c(" xcb_tmp += xcb_tmp_len;")
2020 # not supposed to happen
2021 raise Exception("unhandled variable size field %s" % field.c_field_name)
2024 _c(' xcb_parts[%d].iov_base = (char *) %s;', count, field.c_field_name)
2025 idx = serial_fields.index(field)
2026 aux_var = '&xcb_aux%d' % idx
2027 context = 'serialize' if aux else 'sizeof'
2028 _c(' xcb_parts[%d].iov_len = ', count)
2030 serialize_args = get_serialize_args(field.type, aux_var, field.c_field_name, context)
2031 _c(' %s (%s);', field.type.c_serialize_name, serialize_args)
2032 _c(' xcb_parts[%d].iov_base = xcb_aux%d;' % (count, idx))
2033 free_calls.append(' free(xcb_aux%d);' % idx)
2035 serialize_args = get_serialize_args(field.type, field.c_field_name, aux_var, context)
2036 func_name = field.type.c_sizeof_name
2037 _c(' %s (%s);', func_name, serialize_args)
2040 if not (field.type.need_serialize or field.type.need_sizeof):
2041 # the _serialize() function keeps track of padding automatically
2042 _c(' xcb_parts[%d].iov_base = 0;', count)
2043 _c(' xcb_parts[%d].iov_len = -xcb_parts[%d].iov_len & 3;', count, count-1)
2046 # elif self.var_followed_by_fixed_fields:
2048 _c(' xcb_parts[2].iov_base = (char *) &xcb_out;')
2049 # request header: opcodes + length
2050 _c(' xcb_parts[2].iov_len = 2*sizeof(uint8_t) + sizeof(uint16_t);')
2053 buffer_var = '&xcb_aux'
2054 serialize_args = get_serialize_args(self, buffer_var, '&xcb_out', 'serialize')
2055 _c(' xcb_parts[%d].iov_len = %s (%s);', count, self.c_serialize_name, serialize_args)
2056 _c(' xcb_parts[%d].iov_base = (char *) xcb_aux;', count)
2057 free_calls.append(' free(xcb_aux);')
2058 # no padding necessary - _serialize() keeps track of padding automatically
2061 _c(' xcb_ret.sequence = xcb_send_request(c, %s, xcb_parts + 2, &xcb_req);', func_flags)
2063 # free dyn. all. data, if any
2064 for f in free_calls:
2066 _c(' return xcb_ret;')
2069 def _c_reply(self, name):
2071 Declares the function that returns the reply structure.
2073 spacing1 = ' ' * (len(self.c_cookie_type) - len('xcb_connection_t'))
2074 spacing2 = ' ' * (len(self.c_cookie_type) - len('xcb_generic_error_t'))
2075 spacing3 = ' ' * (len(self.c_reply_name) + 2)
2077 # check if _unserialize() has to be called for any field
2078 def look_for_special_cases(complex_obj):
2079 unserialize_fields = []
2080 # no unserialize call in case of switch
2081 if not complex_obj.is_switch:
2082 for field in complex_obj.fields:
2083 # three cases: 1. field with special case
2084 # 2. container that contains special case field
2085 # 3. list with special case elements
2086 if field.type.var_followed_by_fixed_fields:
2087 unserialize_fields.append(field)
2088 elif field.type.is_container:
2089 unserialize_fields += look_for_special_cases(field.type)
2090 elif field.type.is_list:
2091 if field.type.member.var_followed_by_fixed_fields:
2092 unserialize_fields.append(field)
2093 if field.type.member.is_container:
2094 unserialize_fields += look_for_special_cases(field.type.member)
2095 return unserialize_fields
2097 unserialize_fields = look_for_special_cases(self.reply)
2101 _h(' * Return the reply')
2102 _h(' * @param c The connection')
2103 _h(' * @param cookie The cookie')
2104 _h(' * @param e The xcb_generic_error_t supplied')
2106 _h(' * Returns the reply of the request asked by')
2108 _h(' * The parameter @p e supplied to this function must be NULL if')
2109 _h(' * %s(). is used.', self.c_unchecked_name)
2110 _h(' * Otherwise, it stores the error if any.')
2112 _h(' * The returned value must be freed by the caller using free().')
2116 _hc('/*****************************************************************************')
2118 _hc(' ** %s * %s', self.c_reply_type, self.c_reply_name)
2120 _hc(' ** @param xcb_connection_t%s *c', spacing1)
2121 _hc(' ** @param %s cookie', self.c_cookie_type)
2122 _hc(' ** @param xcb_generic_error_t%s **e', spacing2)
2123 _hc(' ** @returns %s *', self.c_reply_type)
2125 _hc(' *****************************************************************************/')
2127 _hc('%s *', self.c_reply_type)
2128 _hc('%s (xcb_connection_t%s *c /**< */,', self.c_reply_name, spacing1)
2129 _hc('%s%s cookie /**< */,', spacing3, self.c_cookie_type)
2130 _h('%sxcb_generic_error_t%s **e /**< */);', spacing3, spacing2)
2131 _c('%sxcb_generic_error_t%s **e /**< */)', spacing3, spacing2)
2134 if len(unserialize_fields)>0:
2135 # certain variable size fields need to be unserialized explicitly
2136 _c(' %s *reply = (%s *) xcb_wait_for_reply(c, cookie.sequence, e);',
2137 self.c_reply_type, self.c_reply_type)
2139 for field in unserialize_fields:
2140 if field.type.is_list:
2141 _c(' %s %s_iter = %s(reply);', field.c_iterator_type, field.c_field_name, field.c_iterator_name)
2142 _c(' int %s_len = %s(reply);', field.c_field_name, field.c_length_name)
2143 _c(' %s *%s_data;', field.c_field_type, field.c_field_name)
2145 raise Exception('not implemented: call _unserialize() in reply for non-list type %s', field.c_field_type)
2146 # call _unserialize(), using the reply as source and target buffer
2147 _c(' /* special cases: transform parts of the reply to match XCB data structures */')
2148 for field in unserialize_fields:
2149 if field.type.is_list:
2150 _c(' for(i=0; i<%s_len; i++) {', field.c_field_name)
2151 _c(' %s_data = %s_iter.data;', field.c_field_name, field.c_field_name)
2152 _c(' %s((const void *)%s_data, &%s_data);', field.type.c_unserialize_name,
2153 field.c_field_name, field.c_field_name)
2154 _c(' %s(&%s_iter);', field.type.c_next_name, field.c_field_name)
2156 # return the transformed reply
2157 _c(' return reply;')
2160 _c(' return (%s *) xcb_wait_for_reply(c, cookie.sequence, e);', self.c_reply_type)
2164 def _c_opcode(name, opcode):
2166 Declares the opcode define for requests, events, and errors.
2170 _h('/** Opcode for %s. */', _n(name))
2171 _h('#define %s %s', _n(name).upper(), opcode)
2173 def _c_cookie(self, name):
2175 Declares the cookie type for a non-void request.
2180 _h(' * @brief %s', self.c_cookie_type)
2182 _h('typedef struct %s {', self.c_cookie_type)
2183 _h(' unsigned int sequence; /**< */')
2184 _h('} %s;', self.c_cookie_type)
2186 def c_request(self, name):
2188 Exported function that handles request declarations.
2190 _c_type_setup(self, name, ('request',))
2193 # Cookie type declaration
2194 _c_cookie(self, name)
2197 _c_opcode(name, self.opcode)
2199 # Request structure declaration
2203 _c_type_setup(self.reply, name, ('reply',))
2204 # Reply structure definition
2205 _c_complex(self.reply)
2206 # Request prototypes
2207 _c_request_helper(self, name, self.c_cookie_type, False, True)
2208 _c_request_helper(self, name, self.c_cookie_type, False, False)
2210 _c_request_helper(self, name, self.c_cookie_type, False, True, True)
2211 _c_request_helper(self, name, self.c_cookie_type, False, False, True)
2213 _c_accessors(self.reply, name + ('reply',), name)
2214 _c_reply(self, name)
2216 # Request prototypes
2217 _c_request_helper(self, name, 'xcb_void_cookie_t', True, False)
2218 _c_request_helper(self, name, 'xcb_void_cookie_t', True, True)
2220 _c_request_helper(self, name, 'xcb_void_cookie_t', True, False, True)
2221 _c_request_helper(self, name, 'xcb_void_cookie_t', True, True, True)
2224 def c_event(self, name):
2226 Exported function that handles event declarations.
2228 _c_type_setup(self, name, ('event',))
2231 _c_opcode(name, self.opcodes[name])
2233 if self.name == name:
2234 # Structure definition
2239 _h('typedef %s %s;', _t(self.name + ('event',)), _t(name + ('event',)))
2241 def c_error(self, name):
2243 Exported function that handles error declarations.
2245 _c_type_setup(self, name, ('error',))
2248 _c_opcode(name, self.opcodes[name])
2250 if self.name == name:
2251 # Structure definition
2256 _h('typedef %s %s;', _t(self.name + ('error',)), _t(name + ('error',)))
2259 # Main routine starts here
2261 # Must create an "output" dictionary before any xcbgen imports.
2262 output = {'open' : c_open,
2264 'simple' : c_simple,
2266 'struct' : c_struct,
2268 'request' : c_request,
2273 # Boilerplate below this point
2275 # Check for the argument that specifies path to the xcbgen python package.
2277 opts, args = getopt.getopt(sys.argv[1:], 'p:')
2278 except getopt.GetoptError, err:
2280 print 'Usage: c_client.py [-p path] file.xml'
2283 for (opt, arg) in opts:
2285 sys.path.append(arg)
2287 # Import the module class
2289 from xcbgen.state import Module
2292 print 'Failed to load the xcbgen Python package!'
2293 print 'Make sure that xcb/proto installed it on your Python path.'
2294 print 'If not, you will need to create a .pth file or define $PYTHONPATH'
2295 print 'to extend the path.'
2296 print 'Refer to the README file in xcb/proto for more info.'
2300 # Parse the xml header
2301 module = Module(args[0], output)
2303 # Build type-registry and resolve type dependencies