diff options
author | marha <marha@users.sourceforge.net> | 2010-05-16 20:50:58 +0000 |
---|---|---|
committer | marha <marha@users.sourceforge.net> | 2010-05-16 20:50:58 +0000 |
commit | 1c94119ae26b94a60bb2c2b33494ed43c3b8a52f (patch) | |
tree | cfe0c736c95314edac7d9f1065be9c13026ed0c1 /libxcb | |
parent | 6b29aa4559aeb6f795caee047561654bfa0a1954 (diff) | |
download | vcxsrv-1c94119ae26b94a60bb2c2b33494ed43c3b8a52f.tar.gz vcxsrv-1c94119ae26b94a60bb2c2b33494ed43c3b8a52f.tar.bz2 vcxsrv-1c94119ae26b94a60bb2c2b33494ed43c3b8a52f.zip |
svn merge -r588:HEAD ^/branches/released .
Diffstat (limited to 'libxcb')
-rw-r--r-- | libxcb/src/c_client.py | 2086 | ||||
-rw-r--r-- | libxcb/src/xcb_util.c | 854 | ||||
-rw-r--r-- | libxcb/xcb-proto/src/dri2.xml | 318 | ||||
-rw-r--r-- | libxcb/xcb-proto/xcbgen/expr.py | 183 | ||||
-rw-r--r-- | libxcb/xcb-proto/xcbgen/xtypes.py | 942 |
5 files changed, 2206 insertions, 2177 deletions
diff --git a/libxcb/src/c_client.py b/libxcb/src/c_client.py index a18072258..ad7a6ccd2 100644 --- a/libxcb/src/c_client.py +++ b/libxcb/src/c_client.py @@ -1,1042 +1,1044 @@ -#!/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) - _c('#include <X11/Xtrans/Xtrans.h>') - - 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',)) - - 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: - _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 '*' - - 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 - -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 != 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(): - 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: - if len(field.c_field_type) > maxtypelen: - maxtypelen = len(field.c_field_type) - - for field in struct_fields: - spacing = ' ' * (maxtypelen - len(field.c_field_type)) - _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): - ''' - 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 checked: - func_name = self.c_checked_name - if unchecked: - func_name = self.c_unchecked_name - - param_fields = [] - wire_fields = [] - maxtypelen = len('xcb_connection_t') - - 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) - - for field in param_fields: - if len(field.c_field_const_type) > maxtypelen: - maxtypelen = len(field.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: - spacing = ' ' * (maxtypelen - len(field.c_field_const_type)) - _hc(' ** @param %s%s %s%s', field.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 - spacing = ' ' * (maxtypelen - len(field.c_field_const_type)) - comma = ',' if count else ');' - _h('%s%s%s %s%s /**< */%s', func_spacing, field.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, field.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 - - _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) - _c(' ') - - 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;') - - 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.is_list: - _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, _c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype) - else: - _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, 'Uh oh', field.type.c_wiretype) - _c(' xcb_parts[%d].iov_base = 0;', count + 1) - _c(' xcb_parts[%d].iov_len = -xcb_parts[%d].iov_len & 3;', count + 1, count) - count = count + 2 - - _c(' xcb_ret.sequence = xcb_send_request(c, %s, xcb_parts + 2, &xcb_req);', func_flags) - _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) - # 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) - -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.insert(0,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() +#!/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)
+ _c('#include <X11/Xtrans/Xtrans.h>')
+
+ 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',))
+
+ 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:
+ _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 '*'
+
+ 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
+
+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 != 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():
+ 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:
+ if len(field.c_field_type) > maxtypelen:
+ maxtypelen = len(field.c_field_type)
+
+ for field in struct_fields:
+ spacing = ' ' * (maxtypelen - len(field.c_field_type))
+ _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):
+ '''
+ 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 checked:
+ func_name = self.c_checked_name
+ if unchecked:
+ func_name = self.c_unchecked_name
+
+ param_fields = []
+ wire_fields = []
+ maxtypelen = len('xcb_connection_t')
+
+ 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)
+
+ for field in param_fields:
+ if len(field.c_field_const_type) > maxtypelen:
+ maxtypelen = len(field.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:
+ spacing = ' ' * (maxtypelen - len(field.c_field_const_type))
+ _hc(' ** @param %s%s %s%s', field.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
+ spacing = ' ' * (maxtypelen - len(field.c_field_const_type))
+ comma = ',' if count else ');'
+ _h('%s%s%s %s%s /**< */%s', func_spacing, field.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, field.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
+
+ _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)
+ _c(' ')
+
+ 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;')
+
+ 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.is_list:
+ _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, _c_accessor_get_expr(field.type.expr), field.type.member.c_wiretype)
+ else:
+ _c(' xcb_parts[%d].iov_len = %s * sizeof(%s);', count, 'Uh oh', field.type.c_wiretype)
+ _c(' xcb_parts[%d].iov_base = 0;', count + 1)
+ _c(' xcb_parts[%d].iov_len = -xcb_parts[%d].iov_len & 3;', count + 1, count)
+ count = count + 2
+
+ _c(' xcb_ret.sequence = xcb_send_request(c, %s, xcb_parts + 2, &xcb_req);', func_flags)
+ _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)
+ # 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)
+
+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.insert(0,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()
diff --git a/libxcb/src/xcb_util.c b/libxcb/src/xcb_util.c index 101532be3..cd41b98f9 100644 --- a/libxcb/src/xcb_util.c +++ b/libxcb/src/xcb_util.c @@ -1,424 +1,430 @@ -/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - * Except as contained in this notice, the names of the authors or their - * institutions shall not be used in advertising or otherwise to promote the - * sale, use or other dealings in this Software without prior written - * authorization from the authors. - */ - -/* Utility functions implementable using only public APIs. */ - -#include <assert.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <limits.h> -#include <sys/un.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#ifdef DNETCONN -#include <netdnet/dnetdb.h> -#include <netdnet/dn.h> -#endif -#include <netdb.h> -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <stddef.h> -#include <unistd.h> -#include <string.h> - -#include "xcb.h" -#include "xcbext.h" -#include "xcbint.h" - -#ifdef _MSC_VER -#define close(fd) closesocket(fd) -#endif - -static const int error_connection = 1; - -int xcb_popcount(uint32_t mask) -{ - uint32_t y; - y = (mask >> 1) & 033333333333; - y = mask - y - ((y >> 1) & 033333333333); - return ((y + (y >> 3)) & 030707070707) % 077; -} - -static int _xcb_parse_display(const char *name, char **host, char **protocol, - int *displayp, int *screenp) -{ - int len, display, screen; - char *slash, *colon, *dot, *end; - if(!name || !*name) - name = getenv("DISPLAY"); - if(!name) - return 0; - -#ifdef HAVE_LAUNCHD - if(strncmp(name, "/tmp/launch", 11) == 0) - slash = NULL; - else -#endif - slash = strrchr(name, '/'); - - if (slash) { - len = slash - name; - if (protocol) { - *protocol = malloc(len + 1); - if(!*protocol) - return 0; - memcpy(*protocol, name, len); - (*protocol)[len] = '\0'; - } - name = slash + 1; - } else - if (protocol) - *protocol = NULL; - - colon = strrchr(name, ':'); - if(!colon) - return 0; - len = colon - name; - ++colon; - display = strtoul(colon, &dot, 10); - if(dot == colon) - return 0; - if(*dot == '\0') - screen = 0; - else - { - if(*dot != '.') - return 0; - ++dot; - screen = strtoul(dot, &end, 10); - if(end == dot || *end != '\0') - return 0; - } - /* At this point, the display string is fully parsed and valid, but - * the caller's memory is untouched. */ - - *host = malloc(len + 1); - if(!*host) - return 0; - memcpy(*host, name, len); - (*host)[len] = '\0'; - *displayp = display; - if(screenp) - *screenp = screen; - return 1; -} - -int xcb_parse_display(const char *name, char **host, int *displayp, - int *screenp) -{ - return _xcb_parse_display(name, host, NULL, displayp, screenp); -} - -static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port); -static int _xcb_open_unix(char *protocol, const char *file); -#ifdef DNETCONN -static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port); -#endif -#ifdef HAVE_ABSTRACT_SOCKETS -static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen); -#endif - -static int _xcb_open(char *host, char *protocol, const int display) -{ - int fd; - static const char unix_base[] = "/tmp/.X11-unix/X"; - const char *base = unix_base; - size_t filelen; - char *file = NULL; - int actual_filelen; - - if(*host) - { -#ifdef HAVE_LAUNCHD - if(strncmp(host, "/tmp/launch", 11) == 0) { - base = host; - } else { -#endif - -#ifdef DNETCONN - /* DECnet displays have two colons, so _xcb_parse_display will have - left one at the end. However, an IPv6 address can end with *two* - colons, so only treat this as a DECnet display if host ends with - exactly one colon. */ - char *colon = strchr(host, ':'); - if(colon && *(colon+1) == '\0') - { - *colon = '\0'; - return _xcb_open_decnet(host, protocol, display); - } - else -#endif - if (protocol - || strcmp("unix",host)) { /* follow the old unix: rule */ - - /* display specifies TCP */ - unsigned short port = X_TCP_PORT + display; - return _xcb_open_tcp(host, protocol, port); - } -#ifdef HAVE_LAUNCHD - } -#endif - } - - filelen = strlen(base) + 1 + sizeof(display) * 3 + 1; - file = malloc(filelen); - if(file == NULL) - return -1; - - /* display specifies Unix socket */ -#ifdef HAVE_LAUNCHD - if(base == host) - actual_filelen = snprintf(file, filelen, "%s:%d", base, display); - else -#endif - actual_filelen = snprintf(file, filelen, "%s%d", base, display); - if(actual_filelen < 0) - { - free(file); - return -1; - } - /* snprintf may truncate the file */ - filelen = MIN(actual_filelen, filelen - 1); -#ifdef HAVE_ABSTRACT_SOCKETS - fd = _xcb_open_abstract(protocol, file, filelen); - if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED)) - { - free(file); - return fd; - } - -#endif - fd = _xcb_open_unix(protocol, file); - free(file); - - return fd; -} - -#ifdef DNETCONN -static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port) -{ - int fd; - struct sockaddr_dn addr; - struct accessdata_dn accessdata; - struct nodeent *nodeaddr = getnodebyname(host); - - if(!nodeaddr) - return -1; - if (protocol && strcmp("dnet",protocol)) - return -1; - addr.sdn_family = AF_DECnet; - - addr.sdn_add.a_len = nodeaddr->n_length; - memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len); - - addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port); - if(addr.sdn_objnamel < 0) - return -1; - addr.sdn_objnum = 0; - - fd = socket(PF_DECnet, SOCK_STREAM, 0); - if(fd == -1) - return -1; - - memset(&accessdata, 0, sizeof(accessdata)); - accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid()); - if(accessdata.acc_accl < 0) - return -1; - setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata)); - - if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { - close(fd); - return -1; - } - return fd; -} -#endif - -#ifdef WIN32 -int InitWSA(void) -{ - static WSADATA wsadata; - - if (!wsadata.wVersion) - { - ptw32_processInitialize(); - if (WSAStartup(0x0202, &wsadata)) - return -1; - } - return 0; -} -#else -#define InitWSA() -#endif - -static int _xcb_open_tcp(char *host, char *protocol, const unsigned short port) -{ - int fd = -1; - struct addrinfo hints; - char service[6]; /* "65535" with the trailing '\0' */ - struct addrinfo *results, *addr; - char *bracket; - - if (protocol && strcmp("tcp",protocol)) - return -1; - - memset(&hints, 0, sizeof(hints)); -#ifdef AI_ADDRCONFIG - hints.ai_flags |= AI_ADDRCONFIG; -#endif -#ifdef AI_NUMERICSERV - hints.ai_flags |= AI_NUMERICSERV; -#endif - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - -#ifdef AF_INET6 - /* Allow IPv6 addresses enclosed in brackets. */ - if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0') - { - *bracket = '\0'; - ++host; - hints.ai_flags |= AI_NUMERICHOST; - hints.ai_family = AF_INET6; - } -#endif - -#ifdef WIN32 - if (InitWSA()<0) - return -1; -#endif - - snprintf(service, sizeof(service), "%hu", port); - if(getaddrinfo(host, service, &hints, &results)) - /* FIXME: use gai_strerror, and fill in error connection */ - return -1; - - for(addr = results; addr; addr = addr->ai_next) - { - fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if(fd >= 0) { - char on = 1; - setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); - setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)); - - if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0) - break; - close(fd); - fd = -1; - } - } - freeaddrinfo(results); - return fd; -} - -static int _xcb_open_unix(char *protocol, const char *file) -{ - int fd; - struct sockaddr_un addr; - - if (protocol && strcmp("unix",protocol)) - return -1; - - strcpy(addr.sun_path, file); - addr.sun_family = AF_UNIX; -#ifdef HAVE_SOCKADDR_SUN_LEN - addr.sun_len = SUN_LEN(&addr); -#endif - fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(fd == -1) - return -1; - if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) { - close(fd); - return -1; - } - return fd; -} - -#ifdef HAVE_ABSTRACT_SOCKETS -static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen) -{ - int fd; - struct sockaddr_un addr = {0}; - socklen_t namelen; - - if (protocol && strcmp("unix",protocol)) - return -1; - - strcpy(addr.sun_path + 1, file); - addr.sun_family = AF_UNIX; - namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen; -#ifdef HAVE_SOCKADDR_SUN_LEN - addr.sun_len = 1 + filelen; -#endif - fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (fd == -1) - return -1; - if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) { - close(fd); - return -1; - } - return fd; -} -#endif - -xcb_connection_t *xcb_connect(const char *displayname, int *screenp) -{ - return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp); -} - -xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp) -{ - int fd, display = 0; - char *host; - char *protocol; - xcb_auth_info_t ourauth; - xcb_connection_t *c; - - int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp); - - if(!parsed) - return (xcb_connection_t *) &error_connection; - else - fd = _xcb_open(host, protocol, display); - free(host); - - if(fd == -1) - return (xcb_connection_t *) &error_connection; - - if(auth) - return xcb_connect_to_fd(fd, auth); - - if(_xcb_get_auth_info(fd, &ourauth, display)) - { - c = xcb_connect_to_fd(fd, &ourauth); - free(ourauth.name); - free(ourauth.data); - } - else - c = xcb_connect_to_fd(fd, 0); - - return c; -} +/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the names of the authors or their
+ * institutions shall not be used in advertising or otherwise to promote the
+ * sale, use or other dealings in this Software without prior written
+ * authorization from the authors.
+ */
+
+/* Utility functions implementable using only public APIs. */
+
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <limits.h>
+#include <sys/un.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#ifdef DNETCONN
+#include <netdnet/dnetdb.h>
+#include <netdnet/dn.h>
+#endif
+#include <netdb.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "xcb.h"
+#include "xcbext.h"
+#include "xcbint.h"
+
+#ifdef _MSC_VER
+#define close(fd) closesocket(fd)
+#endif
+
+static const int error_connection = 1;
+
+int xcb_popcount(uint32_t mask)
+{
+ uint32_t y;
+ y = (mask >> 1) & 033333333333;
+ y = mask - y - ((y >> 1) & 033333333333);
+ return ((y + (y >> 3)) & 030707070707) % 077;
+}
+
+static int _xcb_parse_display(const char *name, char **host, char **protocol,
+ int *displayp, int *screenp)
+{
+ int len, display, screen;
+ char *slash, *colon, *dot, *end;
+ if(!name || !*name)
+ name = getenv("DISPLAY");
+ if(!name)
+ return 0;
+
+#ifdef HAVE_LAUNCHD
+ if(strncmp(name, "/tmp/launch", 11) == 0)
+ slash = NULL;
+ else
+#endif
+ slash = strrchr(name, '/');
+
+ if (slash) {
+ len = slash - name;
+ if (protocol) {
+ *protocol = malloc(len + 1);
+ if(!*protocol)
+ return 0;
+ memcpy(*protocol, name, len);
+ (*protocol)[len] = '\0';
+ }
+ name = slash + 1;
+ } else
+ if (protocol)
+ *protocol = NULL;
+
+ colon = strrchr(name, ':');
+ if(!colon)
+ return 0;
+ len = colon - name;
+ ++colon;
+ display = strtoul(colon, &dot, 10);
+ if(dot == colon)
+ return 0;
+ if(*dot == '\0')
+ screen = 0;
+ else
+ {
+ if(*dot != '.')
+ return 0;
+ ++dot;
+ screen = strtoul(dot, &end, 10);
+ if(end == dot || *end != '\0')
+ return 0;
+ }
+ /* At this point, the display string is fully parsed and valid, but
+ * the caller's memory is untouched. */
+
+ *host = malloc(len + 1);
+ if(!*host)
+ return 0;
+ memcpy(*host, name, len);
+ (*host)[len] = '\0';
+ *displayp = display;
+ if(screenp)
+ *screenp = screen;
+ return 1;
+}
+
+int xcb_parse_display(const char *name, char **host, int *displayp,
+ int *screenp)
+{
+ return _xcb_parse_display(name, host, NULL, displayp, screenp);
+}
+
+static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port);
+static int _xcb_open_unix(char *protocol, const char *file);
+#ifdef DNETCONN
+static int _xcb_open_decnet(const char *host, char *protocol, const unsigned short port);
+#endif
+#ifdef HAVE_ABSTRACT_SOCKETS
+static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen);
+#endif
+
+static int _xcb_open(const char *host, char *protocol, const int display)
+{
+ int fd;
+ static const char unix_base[] = "/tmp/.X11-unix/X";
+ const char *base = unix_base;
+ size_t filelen;
+ char *file = NULL;
+ int actual_filelen;
+
+#ifdef HAVE_LAUNCHD
+ if(strncmp(host, "/tmp/launch", 11) == 0) {
+ base = host;
+ host = "";
+ protocol = NULL;
+ }
+#endif
+
+ if(*host || protocol)
+ {
+#ifdef DNETCONN
+ /* DECnet displays have two colons, so _xcb_parse_display will have
+ left one at the end. However, an IPv6 address can end with *two*
+ colons, so only treat this as a DECnet display if host ends with
+ exactly one colon. */
+ char *colon = strchr(host, ':');
+ if(colon && *(colon+1) == '\0')
+ {
+ *colon = '\0';
+ return _xcb_open_decnet(host, protocol, display);
+ }
+ else
+#endif
+ if (protocol
+ || strcmp("unix",host)) { /* follow the old unix: rule */
+
+ /* display specifies TCP */
+ unsigned short port = X_TCP_PORT + display;
+ return _xcb_open_tcp(host, protocol, port);
+ }
+ }
+
+ filelen = strlen(base) + 1 + sizeof(display) * 3 + 1;
+ file = malloc(filelen);
+ if(file == NULL)
+ return -1;
+
+ /* display specifies Unix socket */
+#ifdef HAVE_LAUNCHD
+ if(strncmp(base, "/tmp/launch", 11) == 0)
+ actual_filelen = snprintf(file, filelen, "%s:%d", base, display);
+ else
+#endif
+ actual_filelen = snprintf(file, filelen, "%s%d", base, display);
+ if(actual_filelen < 0)
+ {
+ free(file);
+ return -1;
+ }
+ /* snprintf may truncate the file */
+ filelen = MIN(actual_filelen, filelen - 1);
+#ifdef HAVE_ABSTRACT_SOCKETS
+ fd = _xcb_open_abstract(protocol, file, filelen);
+ if (fd >= 0 || (errno != ENOENT && errno != ECONNREFUSED))
+ {
+ free(file);
+ return fd;
+ }
+
+#endif
+ fd = _xcb_open_unix(protocol, file);
+ free(file);
+
+ return fd;
+}
+
+#ifdef DNETCONN
+static int _xcb_open_decnet(const char *host, const char *protocol, const unsigned short port)
+{
+ int fd;
+ struct sockaddr_dn addr;
+ struct accessdata_dn accessdata;
+ struct nodeent *nodeaddr = getnodebyname(host);
+
+ if(!nodeaddr)
+ return -1;
+ if (protocol && strcmp("dnet",protocol))
+ return -1;
+ addr.sdn_family = AF_DECnet;
+
+ addr.sdn_add.a_len = nodeaddr->n_length;
+ memcpy(addr.sdn_add.a_addr, nodeaddr->n_addr, addr.sdn_add.a_len);
+
+ addr.sdn_objnamel = sprintf((char *)addr.sdn_objname, "X$X%d", port);
+ if(addr.sdn_objnamel < 0)
+ return -1;
+ addr.sdn_objnum = 0;
+
+ fd = socket(PF_DECnet, SOCK_STREAM, 0);
+ if(fd == -1)
+ return -1;
+
+ memset(&accessdata, 0, sizeof(accessdata));
+ accessdata.acc_accl = sprintf((char*)accessdata.acc_acc, "%d", getuid());
+ if(accessdata.acc_accl < 0)
+ return -1;
+ setsockopt(fd, DNPROTO_NSP, SO_CONACCESS, &accessdata, sizeof(accessdata));
+
+ if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+#endif
+
+#ifdef WIN32
+int InitWSA(void)
+{
+ static WSADATA wsadata;
+
+ if (!wsadata.wVersion)
+ {
+ ptw32_processInitialize();
+ if (WSAStartup(0x0202, &wsadata))
+ return -1;
+ }
+ return 0;
+}
+#else
+#define InitWSA()
+#endif
+
+static int _xcb_open_tcp(const char *host, char *protocol, const unsigned short port)
+{
+ int fd = -1;
+ struct addrinfo hints;
+ char service[6]; /* "65535" with the trailing '\0' */
+ struct addrinfo *results, *addr;
+ char *bracket;
+
+ if (protocol && strcmp("tcp",protocol) && strcmp("inet",protocol)
+#ifdef AF_INET6
+ && strcmp("inet6",protocol)
+#endif
+ )
+ return -1;
+
+ if (*host == '\0')
+ host = "localhost";
+
+ memset(&hints, 0, sizeof(hints));
+#ifdef AI_ADDRCONFIG
+ hints.ai_flags |= AI_ADDRCONFIG;
+#endif
+#ifdef AI_NUMERICSERV
+ hints.ai_flags |= AI_NUMERICSERV;
+#endif
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+#ifdef AF_INET6
+ /* Allow IPv6 addresses enclosed in brackets. */
+ if(host[0] == '[' && (bracket = strrchr(host, ']')) && bracket[1] == '\0')
+ {
+ *bracket = '\0';
+ ++host;
+ hints.ai_flags |= AI_NUMERICHOST;
+ hints.ai_family = AF_INET6;
+ }
+#endif
+
+#ifdef WIN32
+ if (InitWSA()<0)
+ return -1;
+#endif
+
+ snprintf(service, sizeof(service), "%hu", port);
+ if(getaddrinfo(host, service, &hints, &results))
+ /* FIXME: use gai_strerror, and fill in error connection */
+ return -1;
+
+ for(addr = results; addr; addr = addr->ai_next)
+ {
+ fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
+ if(fd >= 0) {
+ char on = 1;
+ setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
+ setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on));
+
+ if (connect(fd, addr->ai_addr, addr->ai_addrlen) >= 0)
+ break;
+ close(fd);
+ fd = -1;
+ }
+ }
+ freeaddrinfo(results);
+ return fd;
+}
+
+static int _xcb_open_unix(char *protocol, const char *file)
+{
+ int fd;
+ struct sockaddr_un addr;
+
+ if (protocol && strcmp("unix",protocol))
+ return -1;
+
+ strcpy(addr.sun_path, file);
+ addr.sun_family = AF_UNIX;
+#ifdef HAVE_SOCKADDR_SUN_LEN
+ addr.sun_len = SUN_LEN(&addr);
+#endif
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if(fd == -1)
+ return -1;
+ if(connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+#ifdef HAVE_ABSTRACT_SOCKETS
+static int _xcb_open_abstract(char *protocol, const char *file, size_t filelen)
+{
+ int fd;
+ struct sockaddr_un addr = {0};
+ socklen_t namelen;
+
+ if (protocol && strcmp("unix",protocol))
+ return -1;
+
+ strcpy(addr.sun_path + 1, file);
+ addr.sun_family = AF_UNIX;
+ namelen = offsetof(struct sockaddr_un, sun_path) + 1 + filelen;
+#ifdef HAVE_SOCKADDR_SUN_LEN
+ addr.sun_len = 1 + filelen;
+#endif
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd == -1)
+ return -1;
+ if (connect(fd, (struct sockaddr *) &addr, namelen) == -1) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+#endif
+
+xcb_connection_t *xcb_connect(const char *displayname, int *screenp)
+{
+ return xcb_connect_to_display_with_auth_info(displayname, NULL, screenp);
+}
+
+xcb_connection_t *xcb_connect_to_display_with_auth_info(const char *displayname, xcb_auth_info_t *auth, int *screenp)
+{
+ int fd, display = 0;
+ char *host;
+ char *protocol;
+ xcb_auth_info_t ourauth;
+ xcb_connection_t *c;
+
+ int parsed = _xcb_parse_display(displayname, &host, &protocol, &display, screenp);
+
+ if(!parsed)
+ return (xcb_connection_t *) &error_connection;
+ else
+ fd = _xcb_open(host, protocol, display);
+ free(host);
+
+ if(fd == -1)
+ return (xcb_connection_t *) &error_connection;
+
+ if(auth)
+ return xcb_connect_to_fd(fd, auth);
+
+ if(_xcb_get_auth_info(fd, &ourauth, display))
+ {
+ c = xcb_connect_to_fd(fd, &ourauth);
+ free(ourauth.name);
+ free(ourauth.data);
+ }
+ else
+ c = xcb_connect_to_fd(fd, 0);
+
+ return c;
+}
diff --git a/libxcb/xcb-proto/src/dri2.xml b/libxcb/xcb-proto/src/dri2.xml index 7fea94839..62e16ed17 100644 --- a/libxcb/xcb-proto/src/dri2.xml +++ b/libxcb/xcb-proto/src/dri2.xml @@ -1,152 +1,166 @@ -<?xml version="1.0" encoding="utf-8"?> -<!-- -Copyright (C) 2005 Jeremy Kolb. -Copyright © 2009 Intel Corporation -All Rights Reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -Except as contained in this notice, the names of the authors or their -institutions shall not be used in advertising or otherwise to promote the -sale, use or other dealings in this Software without prior written -authorization from the authors. ---> - -<xcb header="dri2" extension-xname="DRI2" extension-name="DRI2" - major-version="1" minor-version="1"> - <import>xproto</import> - - <enum name="Attachment"> - <item name="BufferFrontLeft" /> - <item name="BufferBackLeft" /> - <item name="BufferFrontRight" /> - <item name="BufferBackRight" /> - <item name="BufferDepth" /> - <item name="BufferStencil" /> - <item name="BufferAccum" /> - <item name="BufferFakeFrontLeft" /> - <item name="BufferFakeFrontRight" /> - <item name="BufferDepthStencil" /> - </enum> - - <enum name="DriverType"> - <item name="DRI" /> - </enum> - - <!-- Types --> - - <struct name="DRI2Buffer"> - <field type="CARD32" name="attachment" enum="Attachment" /> - <field type="CARD32" name="name" /> - <field type="CARD32" name="pitch" /> - <field type="CARD32" name="cpp" /> - <field type="CARD32" name="flags" /> - </struct> - - <struct name="AttachFormat"> - <field type="CARD32" name="attachment" enum="Attachment" /> - <field type="CARD32" name="format" /> - </struct> - - <!-- Requests --> - <request name="QueryVersion" opcode="0"> - <field type="CARD32" name="major_version" /> - <field type="CARD32" name="minor_version" /> - <reply> - <pad bytes="1" /> - <field type="CARD32" name="major_version" /> - <field type="CARD32" name="minor_version" /> - </reply> - </request> - - <request name="Connect" opcode="1"> - <field type="WINDOW" name="window" /> - <field type="CARD32" name="driver_type" enum="DriverType" /> - <reply> - <pad bytes="1" /> - <field type="CARD32" name="driver_name_length" /> - <field type="CARD32" name="device_name_length" /> - <pad bytes="16" /> - <list type="char" name="driver_name"> - <fieldref>driver_name_length</fieldref> - </list> - <list type="char" name="device_name"> - <fieldref>device_name_length</fieldref> - </list> - </reply> - </request> - - <request name="Authenticate" opcode="2"> - <field type="WINDOW" name="window" /> - <field type="CARD32" name="magic" /> - <reply> - <pad bytes="1" /> - <field type="CARD32" name="authenticated" /> - </reply> - </request> - - <request name="CreateDrawable" opcode="3"> - <field type="DRAWABLE" name="drawable" /> - </request> - - <request name="DestroyDrawable" opcode="4"> - <field type="DRAWABLE" name="drawable" /> - </request> - - <request name="GetBuffers" opcode="5"> - <field type="DRAWABLE" name="drawable" /> - <field type="CARD32" name="count" /> - <list type="CARD32" name="attachments" /> - <reply> - <pad bytes="1" /> - <field type="CARD32" name="width" /> - <field type="CARD32" name="height" /> - <field type="CARD32" name="count" /> - <pad bytes="12" /> - <list type="DRI2Buffer" name="buffers"> - <fieldref>count</fieldref> - </list> - </reply> - </request> - - <request name="CopyRegion" opcode="6"> - <field type="DRAWABLE" name="drawable" /> - <field type="CARD32" name="region" /> - <field type="CARD32" name="dest" /> - <field type="CARD32" name="src" /> - <reply> - <pad bytes="1" /> - </reply> - </request> - - <request name="GetBuffersWithFormat" opcode="7"> - <field type="DRAWABLE" name="drawable" /> - <field type="CARD32" name="count" /> - <list type="AttachFormat" name="attachments" /> - <reply> - <pad bytes="1" /> - <field type="CARD32" name="width" /> - <field type="CARD32" name="height" /> - <field type="CARD32" name="count" /> - <pad bytes="12" /> - <list type="DRI2Buffer" name="buffers"> - <fieldref>count</fieldref> - </list> - </reply> - </request> -</xcb> +<?xml version="1.0" encoding="utf-8"?>
+<!--
+Copyright (C) 2005 Jeremy Kolb.
+Copyright © 2009 Intel Corporation
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Except as contained in this notice, the names of the authors or their
+institutions shall not be used in advertising or otherwise to promote the
+sale, use or other dealings in this Software without prior written
+authorization from the authors.
+-->
+
+<xcb header="dri2" extension-xname="DRI2" extension-name="DRI2"
+ major-version="1" minor-version="1">
+ <import>xproto</import>
+
+ <enum name="Attachment">
+ <item name="BufferFrontLeft" />
+ <item name="BufferBackLeft" />
+ <item name="BufferFrontRight" />
+ <item name="BufferBackRight" />
+ <item name="BufferDepth" />
+ <item name="BufferStencil" />
+ <item name="BufferAccum" />
+ <item name="BufferFakeFrontLeft" />
+ <item name="BufferFakeFrontRight" />
+ <item name="BufferDepthStencil" />
+ </enum>
+
+ <enum name="DriverType">
+ <item name="DRI" />
+ </enum>
+
+ <!-- Types -->
+
+ <struct name="DRI2Buffer">
+ <field type="CARD32" name="attachment" enum="Attachment" />
+ <field type="CARD32" name="name" />
+ <field type="CARD32" name="pitch" />
+ <field type="CARD32" name="cpp" />
+ <field type="CARD32" name="flags" />
+ </struct>
+
+ <struct name="AttachFormat">
+ <field type="CARD32" name="attachment" enum="Attachment" />
+ <field type="CARD32" name="format" />
+ </struct>
+
+ <!-- Requests -->
+ <request name="QueryVersion" opcode="0">
+ <field type="CARD32" name="major_version" />
+ <field type="CARD32" name="minor_version" />
+ <reply>
+ <pad bytes="1" />
+ <field type="CARD32" name="major_version" />
+ <field type="CARD32" name="minor_version" />
+ </reply>
+ </request>
+
+ <request name="Connect" opcode="1">
+ <field type="WINDOW" name="window" />
+ <field type="CARD32" name="driver_type" enum="DriverType" />
+ <reply>
+ <pad bytes="1" />
+ <field type="CARD32" name="driver_name_length" />
+ <field type="CARD32" name="device_name_length" />
+ <pad bytes="16" />
+ <list type="char" name="driver_name">
+ <fieldref>driver_name_length</fieldref>
+ </list>
+ <list type="void" name="alignment_pad">
+ <op op="-">
+ <op op="&">
+ <op op="+">
+ <fieldref>driver_name_length</fieldref>
+ <value> 3 </value>
+ </op>
+ <unop op="~">
+ <value>3</value>
+ </unop>
+ </op>
+ <fieldref>driver_name_length</fieldref>
+ </op>
+ </list>
+ <list type="char" name="device_name">
+ <fieldref>device_name_length</fieldref>
+ </list>
+ </reply>
+ </request>
+
+ <request name="Authenticate" opcode="2">
+ <field type="WINDOW" name="window" />
+ <field type="CARD32" name="magic" />
+ <reply>
+ <pad bytes="1" />
+ <field type="CARD32" name="authenticated" />
+ </reply>
+ </request>
+
+ <request name="CreateDrawable" opcode="3">
+ <field type="DRAWABLE" name="drawable" />
+ </request>
+
+ <request name="DestroyDrawable" opcode="4">
+ <field type="DRAWABLE" name="drawable" />
+ </request>
+
+ <request name="GetBuffers" opcode="5">
+ <field type="DRAWABLE" name="drawable" />
+ <field type="CARD32" name="count" />
+ <list type="CARD32" name="attachments" />
+ <reply>
+ <pad bytes="1" />
+ <field type="CARD32" name="width" />
+ <field type="CARD32" name="height" />
+ <field type="CARD32" name="count" />
+ <pad bytes="12" />
+ <list type="DRI2Buffer" name="buffers">
+ <fieldref>count</fieldref>
+ </list>
+ </reply>
+ </request>
+
+ <request name="CopyRegion" opcode="6">
+ <field type="DRAWABLE" name="drawable" />
+ <field type="CARD32" name="region" />
+ <field type="CARD32" name="dest" />
+ <field type="CARD32" name="src" />
+ <reply>
+ <pad bytes="1" />
+ </reply>
+ </request>
+
+ <request name="GetBuffersWithFormat" opcode="7">
+ <field type="DRAWABLE" name="drawable" />
+ <field type="CARD32" name="count" />
+ <list type="AttachFormat" name="attachments" />
+ <reply>
+ <pad bytes="1" />
+ <field type="CARD32" name="width" />
+ <field type="CARD32" name="height" />
+ <field type="CARD32" name="count" />
+ <pad bytes="12" />
+ <list type="DRI2Buffer" name="buffers">
+ <fieldref>count</fieldref>
+ </list>
+ </reply>
+ </request>
+</xcb>
diff --git a/libxcb/xcb-proto/xcbgen/expr.py b/libxcb/xcb-proto/xcbgen/expr.py index 522e17d09..9b1855425 100644 --- a/libxcb/xcb-proto/xcbgen/expr.py +++ b/libxcb/xcb-proto/xcbgen/expr.py @@ -1,88 +1,95 @@ -''' -This module contains helper classes for structure fields and length expressions. -''' -class Field(object): - ''' - Represents a field of a structure. - - type is the datatype object for the field. - field_type is the name of the type (string tuple) - field_name is the name of the structure field. - visible is true iff the field should be in the request API. - wire is true iff the field should be in the request structure. - auto is true iff the field is on the wire but not in the request API (e.g. opcode) - ''' - def __init__(self, type, field_type, field_name, visible, wire, auto): - self.type = type - self.field_type = field_type - self.field_name = field_name - self.visible = visible - self.wire = wire - self.auto = auto - - -class Expression(object): - ''' - Represents a mathematical expression for a list length or exprfield. - - Public fields: - op is the operation (text +,*,/,<<) or None. - lhs and rhs are the sub-Expressions if op is set. - lenfield_name is the name of the length field, or None for request lists. - lenfield is the Field object for the length field, or None. - bitfield is True if the length field is a bitmask instead of a number. - nmemb is the fixed size (value)of the expression, or None - ''' - def __init__(self, elt, parent): - self.parent = parent - - self.nmemb = None - - self.lenfield_name = None - self.lenfield_type = None - self.lenfield = None - self.lenwire = False - self.bitfield = False - - self.op = None - self.lhs = None - self.rhs = None - - if elt.tag == 'list': - # List going into a request, which has no length field (inferred by server) - self.lenfield_name = elt.get('name') + '_len' - self.lenfield_type = 'CARD32' - - elif elt.tag == 'fieldref': - # Standard list with a fieldref - self.lenfield_name = elt.text - - elif elt.tag == 'valueparam': - # Value-mask. The length bitmask is described by attributes. - self.lenfield_name = elt.get('value-mask-name') - self.lenfield_type = elt.get('value-mask-type') - self.lenwire = True - self.bitfield = True - - elif elt.tag == 'op': - # Op field. Need to recurse. - self.op = elt.get('op') - self.lhs = Expression(list(elt)[0], parent) - self.rhs = Expression(list(elt)[1], parent) - - # Hopefully we don't have two separate length fields... - self.lenfield_name = self.lhs.lenfield_name - if self.lenfield_name == None: - self.lenfield_name = self.rhs.lenfield_name - - elif elt.tag == 'value': - # Constant expression - self.nmemb = int(elt.text) - - else: - # Notreached - raise Exception('XXX') - - - def fixed_size(self): - return self.nmemb != None +'''
+This module contains helper classes for structure fields and length expressions.
+'''
+class Field(object):
+ '''
+ Represents a field of a structure.
+
+ type is the datatype object for the field.
+ field_type is the name of the type (string tuple)
+ field_name is the name of the structure field.
+ visible is true iff the field should be in the request API.
+ wire is true iff the field should be in the request structure.
+ auto is true iff the field is on the wire but not in the request API (e.g. opcode)
+ '''
+ def __init__(self, type, field_type, field_name, visible, wire, auto):
+ self.type = type
+ self.field_type = field_type
+ self.field_name = field_name
+ self.visible = visible
+ self.wire = wire
+ self.auto = auto
+
+
+class Expression(object):
+ '''
+ Represents a mathematical expression for a list length or exprfield.
+
+ Public fields:
+ op is the operation (text +,*,/,<<,~) or None.
+ lhs and rhs are the sub-Expressions if op is set.
+ lenfield_name is the name of the length field, or None for request lists.
+ lenfield is the Field object for the length field, or None.
+ bitfield is True if the length field is a bitmask instead of a number.
+ nmemb is the fixed size (value)of the expression, or None
+ '''
+ def __init__(self, elt, parent):
+ self.parent = parent
+
+ self.nmemb = None
+
+ self.lenfield_name = None
+ self.lenfield_type = None
+ self.lenfield = None
+ self.lenwire = False
+ self.bitfield = False
+
+ self.op = None
+ self.lhs = None
+ self.rhs = None
+
+ if elt.tag == 'list':
+ # List going into a request, which has no length field (inferred by server)
+ self.lenfield_name = elt.get('name') + '_len'
+ self.lenfield_type = 'CARD32'
+
+ elif elt.tag == 'fieldref':
+ # Standard list with a fieldref
+ self.lenfield_name = elt.text
+
+ elif elt.tag == 'valueparam':
+ # Value-mask. The length bitmask is described by attributes.
+ self.lenfield_name = elt.get('value-mask-name')
+ self.lenfield_type = elt.get('value-mask-type')
+ self.lenwire = True
+ self.bitfield = True
+
+ elif elt.tag == 'op':
+ # Op field. Need to recurse.
+ self.op = elt.get('op')
+ self.lhs = Expression(list(elt)[0], parent)
+ self.rhs = Expression(list(elt)[1], parent)
+
+ # Hopefully we don't have two separate length fields...
+ self.lenfield_name = self.lhs.lenfield_name
+ if self.lenfield_name == None:
+ self.lenfield_name = self.rhs.lenfield_name
+
+ elif elt.tag == 'unop':
+ # Op field. Need to recurse.
+ self.op = elt.get('op')
+ self.rhs = Expression(list(elt)[0], parent)
+
+ self.lenfield_name = self.rhs.lenfield_name
+
+ elif elt.tag == 'value':
+ # Constant expression
+ self.nmemb = int(elt.text, 0)
+
+ else:
+ # Notreached
+ raise Exception('XXX')
+
+
+ def fixed_size(self):
+ return self.nmemb != None
diff --git a/libxcb/xcb-proto/xcbgen/xtypes.py b/libxcb/xcb-proto/xcbgen/xtypes.py index 01d765e59..dae5102b3 100644 --- a/libxcb/xcb-proto/xcbgen/xtypes.py +++ b/libxcb/xcb-proto/xcbgen/xtypes.py @@ -1,471 +1,471 @@ -''' -This module contains the classes which represent XCB data types. -''' -from expr import Field, Expression -import __main__ - -class Type(object): - ''' - Abstract base class for all XCB data types. - Contains default fields, and some abstract methods. - ''' - def __init__(self, name): - ''' - Default structure initializer. Sets up default fields. - - Public fields: - name is a tuple of strings specifying the full type name. - size is the size of the datatype in bytes, or None if variable-sized. - nmemb is 1 for non-list types, None for variable-sized lists, otherwise number of elts. - booleans for identifying subclasses, because I can't figure out isinstance(). - ''' - self.name = name - self.size = None - self.nmemb = None - self.resolved = False - - # Screw isinstance(). - self.is_simple = False - self.is_list = False - self.is_expr = False - self.is_container = False - self.is_reply = False - self.is_union = False - self.is_pad = False - - def resolve(self, module): - ''' - Abstract method for resolving a type. - This should make sure any referenced types are already declared. - ''' - raise Exception('abstract resolve method not overridden!') - - def out(self, name): - ''' - Abstract method for outputting code. - These are declared in the language-specific modules, and - there must be a dictionary containing them declared when this module is imported! - ''' - raise Exception('abstract out method not overridden!') - - def fixed_size(self): - ''' - Abstract method for determining if the data type is fixed-size. - ''' - raise Exception('abstract fixed_size method not overridden!') - - def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto): - ''' - Default method for making a data type a member of a structure. - Extend this if the data type needs to add an additional length field or something. - - module is the global module object. - complex_type is the structure object. - see Field for the meaning of the other parameters. - ''' - new_field = Field(self, field_type, field_name, visible, wire, auto) - - # We dump the _placeholder_byte if any fields are added. - for (idx, field) in enumerate(complex_type.fields): - if field == _placeholder_byte: - complex_type.fields[idx] = new_field - return - - complex_type.fields.append(new_field) - -class SimpleType(Type): - ''' - Derived class which represents a cardinal type like CARD32 or char. - Any type which is typedef'ed to cardinal will be one of these. - - Public fields added: - none - ''' - def __init__(self, name, size): - Type.__init__(self, name) - self.is_simple = True - self.size = size - self.nmemb = 1 - - def resolve(self, module): - self.resolved = True - - def fixed_size(self): - return True - - out = __main__.output['simple'] - - -# Cardinal datatype globals. See module __init__ method. -tcard8 = SimpleType(('uint8_t',), 1) -tcard16 = SimpleType(('uint16_t',), 2) -tcard32 = SimpleType(('uint32_t',), 4) -tint8 = SimpleType(('int8_t',), 1) -tint16 = SimpleType(('int16_t',), 2) -tint32 = SimpleType(('int32_t',), 4) -tchar = SimpleType(('char',), 1) -tfloat = SimpleType(('float',), 4) -tdouble = SimpleType(('double',), 8) - - -class Enum(SimpleType): - ''' - Derived class which represents an enum. Fixed-size. - - Public fields added: - values contains a list of (name, value) tuples. value is empty, or a number. - bits contains a list of (name, bitnum) tuples. items only appear if specified as a bit. bitnum is a number. - ''' - def __init__(self, name, elt): - SimpleType.__init__(self, name, 4) - self.values = [] - self.bits = [] - for item in list(elt): - # First check if we're using a default value - if len(list(item)) == 0: - self.values.append((item.get('name'), '')) - continue - - # An explicit value or bit was specified. - value = list(item)[0] - if value.tag == 'value': - self.values.append((item.get('name'), value.text)) - elif value.tag == 'bit': - self.values.append((item.get('name'), '%u' % (1 << int(value.text)))) - self.bits.append((item.get('name'), value.text)) - - def resolve(self, module): - self.resolved = True - - def fixed_size(self): - return True - - out = __main__.output['enum'] - - -class ListType(Type): - ''' - Derived class which represents a list of some other datatype. Fixed- or variable-sized. - - Public fields added: - member is the datatype of the list elements. - parent is the structure type containing the list. - expr is an Expression object containing the length information, for variable-sized lists. - ''' - def __init__(self, elt, member, parent): - Type.__init__(self, member.name) - self.is_list = True - self.member = member - self.parent = parent - - if elt.tag == 'list': - elts = list(elt) - self.expr = Expression(elts[0] if len(elts) else elt, self) - elif elt.tag == 'valueparam': - self.expr = Expression(elt, self) - - self.size = member.size if member.fixed_size() else None - self.nmemb = self.expr.nmemb if self.expr.fixed_size() else None - - def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto): - if not self.fixed_size(): - # We need a length field. - # Ask our Expression object for it's name, type, and whether it's on the wire. - lenfid = self.expr.lenfield_type - lenfield_name = self.expr.lenfield_name - lenwire = self.expr.lenwire - needlen = True - - # See if the length field is already in the structure. - for field in self.parent.fields: - if field.field_name == lenfield_name: - needlen = False - - # It isn't, so we need to add it to the structure ourself. - if needlen: - type = module.get_type(lenfid) - lenfield_type = module.get_type_name(lenfid) - type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False) - - # Add ourself to the structure by calling our original method. - Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto) - - def resolve(self, module): - if self.resolved: - return - self.member.resolve(module) - - # Find my length field again. We need the actual Field object in the expr. - # This is needed because we might have added it ourself above. - if not self.fixed_size(): - for field in self.parent.fields: - if field.field_name == self.expr.lenfield_name and field.wire: - self.expr.lenfield = field - break - - self.resolved = True - - def fixed_size(self): - return self.member.fixed_size() and self.expr.fixed_size() - -class ExprType(Type): - ''' - Derived class which represents an exprfield. Fixed size. - - Public fields added: - expr is an Expression object containing the value of the field. - ''' - def __init__(self, elt, member, parent): - Type.__init__(self, member.name) - self.is_expr = True - self.member = member - self.parent = parent - - self.expr = Expression(list(elt)[0], self) - - self.size = member.size - self.nmemb = 1 - - def resolve(self, module): - if self.resolved: - return - self.member.resolve(module) - self.resolved = True - - def fixed_size(self): - return True - -class PadType(Type): - ''' - Derived class which represents a padding field. - ''' - def __init__(self, elt): - Type.__init__(self, tcard8.name) - self.is_pad = True - self.size = 1 - self.nmemb = 1 if (elt == None) else int(elt.get('bytes')) - - def resolve(self, module): - self.resolved = True - - def fixed_size(self): - return True - - -class ComplexType(Type): - ''' - Derived class which represents a structure. Base type for all structure types. - - Public fields added: - fields is an array of Field objects describing the structure fields. - ''' - def __init__(self, name, elt): - Type.__init__(self, name) - self.is_container = True - self.elt = elt - self.fields = [] - self.nmemb = 1 - self.size = 0 - - def resolve(self, module): - if self.resolved: - return - pads = 0 - - # Resolve all of our field datatypes. - for child in list(self.elt): - if child.tag == 'pad': - field_name = 'pad' + str(pads) - fkey = 'CARD8' - type = PadType(child) - pads = pads + 1 - visible = False - elif child.tag == 'field': - field_name = child.get('name') - fkey = child.get('type') - type = module.get_type(fkey) - visible = True - elif child.tag == 'exprfield': - field_name = child.get('name') - fkey = child.get('type') - type = ExprType(child, module.get_type(fkey), self) - visible = False - elif child.tag == 'list': - field_name = child.get('name') - fkey = child.get('type') - type = ListType(child, module.get_type(fkey), self) - visible = True - elif child.tag == 'valueparam': - field_name = child.get('value-list-name') - fkey = 'CARD32' - type = ListType(child, module.get_type(fkey), self) - visible = True - else: - # Hit this on Reply - continue - - # Get the full type name for the field - field_type = module.get_type_name(fkey) - # Add the field to ourself - type.make_member_of(module, self, field_type, field_name, visible, True, False) - # Recursively resolve the type (could be another structure, list) - type.resolve(module) - - self.calc_size() # Figure out how big we are - self.resolved = True - - def calc_size(self): - self.size = 0 - for m in self.fields: - if not m.wire: - continue - if m.type.fixed_size(): - self.size = self.size + (m.type.size * m.type.nmemb) - else: - self.size = None - break - - def fixed_size(self): - for m in self.fields: - if not m.type.fixed_size(): - return False - return True - - -class Struct(ComplexType): - ''' - Derived class representing a struct data type. - ''' - out = __main__.output['struct'] - - -class Union(ComplexType): - ''' - Derived class representing a union data type. - ''' - def __init__(self, name, elt): - ComplexType.__init__(self, name, elt) - self.is_union = True - - out = __main__.output['union'] - - -class Reply(ComplexType): - ''' - Derived class representing a reply. Only found as a field of Request. - ''' - def __init__(self, name, elt): - ComplexType.__init__(self, name, elt) - self.is_reply = True - - def resolve(self, module): - if self.resolved: - return - # Add the automatic protocol fields - self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True)) - self.fields.append(_placeholder_byte) - self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True)) - self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True)) - ComplexType.resolve(self, module) - - -class Request(ComplexType): - ''' - Derived class representing a request. - - Public fields added: - reply contains the reply datatype or None for void requests. - opcode contains the request number. - ''' - def __init__(self, name, elt): - ComplexType.__init__(self, name, elt) - self.reply = None - self.opcode = elt.get('opcode') - - for child in list(elt): - if child.tag == 'reply': - self.reply = Reply(name, child) - - def resolve(self, module): - if self.resolved: - return - # Add the automatic protocol fields - if module.namespace.is_ext: - self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True)) - self.fields.append(Field(tcard8, tcard8.name, 'minor_opcode', False, True, True)) - self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True)) - ComplexType.resolve(self, module) - else: - self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True)) - self.fields.append(_placeholder_byte) - self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True)) - ComplexType.resolve(self, module) - - if self.reply: - self.reply.resolve(module) - - out = __main__.output['request'] - - -class Event(ComplexType): - ''' - Derived class representing an event data type. - - Public fields added: - opcodes is a dictionary of name -> opcode number, for eventcopies. - ''' - def __init__(self, name, elt): - ComplexType.__init__(self, name, elt) - self.opcodes = {} - - tmp = elt.get('no-sequence-number') - self.has_seq = (tmp == None or tmp.lower() == 'false' or tmp == '0') - - def add_opcode(self, opcode, name, main): - self.opcodes[name] = opcode - if main: - self.name = name - - def resolve(self, module): - if self.resolved: - return - - # Add the automatic protocol fields - self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True)) - if self.has_seq: - self.fields.append(_placeholder_byte) - self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True)) - ComplexType.resolve(self, module) - - out = __main__.output['event'] - - -class Error(ComplexType): - ''' - Derived class representing an error data type. - - Public fields added: - opcodes is a dictionary of name -> opcode number, for errorcopies. - ''' - def __init__(self, name, elt): - ComplexType.__init__(self, name, elt) - self.opcodes = {} - - def add_opcode(self, opcode, name, main): - self.opcodes[name] = opcode - if main: - self.name = name - - def resolve(self, module): - if self.resolved: - return - - # Add the automatic protocol fields - self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True)) - self.fields.append(Field(tcard8, tcard8.name, 'error_code', False, True, True)) - self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True)) - ComplexType.resolve(self, module) - - out = __main__.output['error'] - -_placeholder_byte = Field(PadType(None), tcard8.name, 'pad0', False, True, False) +'''
+This module contains the classes which represent XCB data types.
+'''
+from expr import Field, Expression
+import __main__
+
+class Type(object):
+ '''
+ Abstract base class for all XCB data types.
+ Contains default fields, and some abstract methods.
+ '''
+ def __init__(self, name):
+ '''
+ Default structure initializer. Sets up default fields.
+
+ Public fields:
+ name is a tuple of strings specifying the full type name.
+ size is the size of the datatype in bytes, or None if variable-sized.
+ nmemb is 1 for non-list types, None for variable-sized lists, otherwise number of elts.
+ booleans for identifying subclasses, because I can't figure out isinstance().
+ '''
+ self.name = name
+ self.size = None
+ self.nmemb = None
+ self.resolved = False
+
+ # Screw isinstance().
+ self.is_simple = False
+ self.is_list = False
+ self.is_expr = False
+ self.is_container = False
+ self.is_reply = False
+ self.is_union = False
+ self.is_pad = False
+
+ def resolve(self, module):
+ '''
+ Abstract method for resolving a type.
+ This should make sure any referenced types are already declared.
+ '''
+ raise Exception('abstract resolve method not overridden!')
+
+ def out(self, name):
+ '''
+ Abstract method for outputting code.
+ These are declared in the language-specific modules, and
+ there must be a dictionary containing them declared when this module is imported!
+ '''
+ raise Exception('abstract out method not overridden!')
+
+ def fixed_size(self):
+ '''
+ Abstract method for determining if the data type is fixed-size.
+ '''
+ raise Exception('abstract fixed_size method not overridden!')
+
+ def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto):
+ '''
+ Default method for making a data type a member of a structure.
+ Extend this if the data type needs to add an additional length field or something.
+
+ module is the global module object.
+ complex_type is the structure object.
+ see Field for the meaning of the other parameters.
+ '''
+ new_field = Field(self, field_type, field_name, visible, wire, auto)
+
+ # We dump the _placeholder_byte if any fields are added.
+ for (idx, field) in enumerate(complex_type.fields):
+ if field == _placeholder_byte:
+ complex_type.fields[idx] = new_field
+ return
+
+ complex_type.fields.append(new_field)
+
+class SimpleType(Type):
+ '''
+ Derived class which represents a cardinal type like CARD32 or char.
+ Any type which is typedef'ed to cardinal will be one of these.
+
+ Public fields added:
+ none
+ '''
+ def __init__(self, name, size):
+ Type.__init__(self, name)
+ self.is_simple = True
+ self.size = size
+ self.nmemb = 1
+
+ def resolve(self, module):
+ self.resolved = True
+
+ def fixed_size(self):
+ return True
+
+ out = __main__.output['simple']
+
+
+# Cardinal datatype globals. See module __init__ method.
+tcard8 = SimpleType(('uint8_t',), 1)
+tcard16 = SimpleType(('uint16_t',), 2)
+tcard32 = SimpleType(('uint32_t',), 4)
+tint8 = SimpleType(('int8_t',), 1)
+tint16 = SimpleType(('int16_t',), 2)
+tint32 = SimpleType(('int32_t',), 4)
+tchar = SimpleType(('char',), 1)
+tfloat = SimpleType(('float',), 4)
+tdouble = SimpleType(('double',), 8)
+
+
+class Enum(SimpleType):
+ '''
+ Derived class which represents an enum. Fixed-size.
+
+ Public fields added:
+ values contains a list of (name, value) tuples. value is empty, or a number.
+ bits contains a list of (name, bitnum) tuples. items only appear if specified as a bit. bitnum is a number.
+ '''
+ def __init__(self, name, elt):
+ SimpleType.__init__(self, name, 4)
+ self.values = []
+ self.bits = []
+ for item in list(elt):
+ # First check if we're using a default value
+ if len(list(item)) == 0:
+ self.values.append((item.get('name'), ''))
+ continue
+
+ # An explicit value or bit was specified.
+ value = list(item)[0]
+ if value.tag == 'value':
+ self.values.append((item.get('name'), value.text))
+ elif value.tag == 'bit':
+ self.values.append((item.get('name'), '%u' % (1 << int(value.text, 0))))
+ self.bits.append((item.get('name'), value.text))
+
+ def resolve(self, module):
+ self.resolved = True
+
+ def fixed_size(self):
+ return True
+
+ out = __main__.output['enum']
+
+
+class ListType(Type):
+ '''
+ Derived class which represents a list of some other datatype. Fixed- or variable-sized.
+
+ Public fields added:
+ member is the datatype of the list elements.
+ parent is the structure type containing the list.
+ expr is an Expression object containing the length information, for variable-sized lists.
+ '''
+ def __init__(self, elt, member, parent):
+ Type.__init__(self, member.name)
+ self.is_list = True
+ self.member = member
+ self.parent = parent
+
+ if elt.tag == 'list':
+ elts = list(elt)
+ self.expr = Expression(elts[0] if len(elts) else elt, self)
+ elif elt.tag == 'valueparam':
+ self.expr = Expression(elt, self)
+
+ self.size = member.size if member.fixed_size() else None
+ self.nmemb = self.expr.nmemb if self.expr.fixed_size() else None
+
+ def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto):
+ if not self.fixed_size():
+ # We need a length field.
+ # Ask our Expression object for it's name, type, and whether it's on the wire.
+ lenfid = self.expr.lenfield_type
+ lenfield_name = self.expr.lenfield_name
+ lenwire = self.expr.lenwire
+ needlen = True
+
+ # See if the length field is already in the structure.
+ for field in self.parent.fields:
+ if field.field_name == lenfield_name:
+ needlen = False
+
+ # It isn't, so we need to add it to the structure ourself.
+ if needlen:
+ type = module.get_type(lenfid)
+ lenfield_type = module.get_type_name(lenfid)
+ type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False)
+
+ # Add ourself to the structure by calling our original method.
+ Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto)
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+ self.member.resolve(module)
+
+ # Find my length field again. We need the actual Field object in the expr.
+ # This is needed because we might have added it ourself above.
+ if not self.fixed_size():
+ for field in self.parent.fields:
+ if field.field_name == self.expr.lenfield_name and field.wire:
+ self.expr.lenfield = field
+ break
+
+ self.resolved = True
+
+ def fixed_size(self):
+ return self.member.fixed_size() and self.expr.fixed_size()
+
+class ExprType(Type):
+ '''
+ Derived class which represents an exprfield. Fixed size.
+
+ Public fields added:
+ expr is an Expression object containing the value of the field.
+ '''
+ def __init__(self, elt, member, parent):
+ Type.__init__(self, member.name)
+ self.is_expr = True
+ self.member = member
+ self.parent = parent
+
+ self.expr = Expression(list(elt)[0], self)
+
+ self.size = member.size
+ self.nmemb = 1
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+ self.member.resolve(module)
+ self.resolved = True
+
+ def fixed_size(self):
+ return True
+
+class PadType(Type):
+ '''
+ Derived class which represents a padding field.
+ '''
+ def __init__(self, elt):
+ Type.__init__(self, tcard8.name)
+ self.is_pad = True
+ self.size = 1
+ self.nmemb = 1 if (elt == None) else int(elt.get('bytes'), 0)
+
+ def resolve(self, module):
+ self.resolved = True
+
+ def fixed_size(self):
+ return True
+
+
+class ComplexType(Type):
+ '''
+ Derived class which represents a structure. Base type for all structure types.
+
+ Public fields added:
+ fields is an array of Field objects describing the structure fields.
+ '''
+ def __init__(self, name, elt):
+ Type.__init__(self, name)
+ self.is_container = True
+ self.elt = elt
+ self.fields = []
+ self.nmemb = 1
+ self.size = 0
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+ pads = 0
+
+ # Resolve all of our field datatypes.
+ for child in list(self.elt):
+ if child.tag == 'pad':
+ field_name = 'pad' + str(pads)
+ fkey = 'CARD8'
+ type = PadType(child)
+ pads = pads + 1
+ visible = False
+ elif child.tag == 'field':
+ field_name = child.get('name')
+ fkey = child.get('type')
+ type = module.get_type(fkey)
+ visible = True
+ elif child.tag == 'exprfield':
+ field_name = child.get('name')
+ fkey = child.get('type')
+ type = ExprType(child, module.get_type(fkey), self)
+ visible = False
+ elif child.tag == 'list':
+ field_name = child.get('name')
+ fkey = child.get('type')
+ type = ListType(child, module.get_type(fkey), self)
+ visible = True
+ elif child.tag == 'valueparam':
+ field_name = child.get('value-list-name')
+ fkey = 'CARD32'
+ type = ListType(child, module.get_type(fkey), self)
+ visible = True
+ else:
+ # Hit this on Reply
+ continue
+
+ # Get the full type name for the field
+ field_type = module.get_type_name(fkey)
+ # Add the field to ourself
+ type.make_member_of(module, self, field_type, field_name, visible, True, False)
+ # Recursively resolve the type (could be another structure, list)
+ type.resolve(module)
+
+ self.calc_size() # Figure out how big we are
+ self.resolved = True
+
+ def calc_size(self):
+ self.size = 0
+ for m in self.fields:
+ if not m.wire:
+ continue
+ if m.type.fixed_size():
+ self.size = self.size + (m.type.size * m.type.nmemb)
+ else:
+ self.size = None
+ break
+
+ def fixed_size(self):
+ for m in self.fields:
+ if not m.type.fixed_size():
+ return False
+ return True
+
+
+class Struct(ComplexType):
+ '''
+ Derived class representing a struct data type.
+ '''
+ out = __main__.output['struct']
+
+
+class Union(ComplexType):
+ '''
+ Derived class representing a union data type.
+ '''
+ def __init__(self, name, elt):
+ ComplexType.__init__(self, name, elt)
+ self.is_union = True
+
+ out = __main__.output['union']
+
+
+class Reply(ComplexType):
+ '''
+ Derived class representing a reply. Only found as a field of Request.
+ '''
+ def __init__(self, name, elt):
+ ComplexType.__init__(self, name, elt)
+ self.is_reply = True
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+ # Add the automatic protocol fields
+ self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
+ self.fields.append(_placeholder_byte)
+ self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
+ self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True))
+ ComplexType.resolve(self, module)
+
+
+class Request(ComplexType):
+ '''
+ Derived class representing a request.
+
+ Public fields added:
+ reply contains the reply datatype or None for void requests.
+ opcode contains the request number.
+ '''
+ def __init__(self, name, elt):
+ ComplexType.__init__(self, name, elt)
+ self.reply = None
+ self.opcode = elt.get('opcode')
+
+ for child in list(elt):
+ if child.tag == 'reply':
+ self.reply = Reply(name, child)
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+ # Add the automatic protocol fields
+ if module.namespace.is_ext:
+ self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
+ self.fields.append(Field(tcard8, tcard8.name, 'minor_opcode', False, True, True))
+ self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
+ ComplexType.resolve(self, module)
+ else:
+ self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
+ self.fields.append(_placeholder_byte)
+ self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
+ ComplexType.resolve(self, module)
+
+ if self.reply:
+ self.reply.resolve(module)
+
+ out = __main__.output['request']
+
+
+class Event(ComplexType):
+ '''
+ Derived class representing an event data type.
+
+ Public fields added:
+ opcodes is a dictionary of name -> opcode number, for eventcopies.
+ '''
+ def __init__(self, name, elt):
+ ComplexType.__init__(self, name, elt)
+ self.opcodes = {}
+
+ tmp = elt.get('no-sequence-number')
+ self.has_seq = (tmp == None or tmp.lower() == 'false' or tmp == '0')
+
+ def add_opcode(self, opcode, name, main):
+ self.opcodes[name] = opcode
+ if main:
+ self.name = name
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+
+ # Add the automatic protocol fields
+ self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
+ if self.has_seq:
+ self.fields.append(_placeholder_byte)
+ self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
+ ComplexType.resolve(self, module)
+
+ out = __main__.output['event']
+
+
+class Error(ComplexType):
+ '''
+ Derived class representing an error data type.
+
+ Public fields added:
+ opcodes is a dictionary of name -> opcode number, for errorcopies.
+ '''
+ def __init__(self, name, elt):
+ ComplexType.__init__(self, name, elt)
+ self.opcodes = {}
+
+ def add_opcode(self, opcode, name, main):
+ self.opcodes[name] = opcode
+ if main:
+ self.name = name
+
+ def resolve(self, module):
+ if self.resolved:
+ return
+
+ # Add the automatic protocol fields
+ self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
+ self.fields.append(Field(tcard8, tcard8.name, 'error_code', False, True, True))
+ self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
+ ComplexType.resolve(self, module)
+
+ out = __main__.output['error']
+
+_placeholder_byte = Field(PadType(None), tcard8.name, 'pad0', False, True, False)
|