Bug #5958: Also zero out the implicit pad byte in empty requests.
[free-sw/xcb/libxcb] / src / c-client.xsl
index c35fe33..5df5762 100644 (file)
@@ -26,7 +26,11 @@ authorization from the authors.
 -->
 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0"
-               xmlns:e="http://exslt.org/common">
+               xmlns:e="http://exslt.org/common"
+               xmlns:func="http://exslt.org/functions"
+               xmlns:str="http://exslt.org/strings"
+               xmlns:xcb="http://xcb.freedesktop.org"
+               extension-element-prefixes="func str xcb">
   
   <xsl:output method="text" />
 
@@ -43,12 +47,16 @@ authorization from the authors.
 
   <xsl:variable name="h" select="$mode = 'header'" />
   <xsl:variable name="c" select="$mode = 'source'" />
+
+  <xsl:variable name="need-string-h" select="//request/pad[@bytes != 1]" />
   
   <!-- String used to indent lines of code. -->
   <xsl:variable name="indent-string" select="'    '" />
 
   <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
   <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
+  <xsl:variable name="letters" select="concat($ucase, $lcase)" />
+  <xsl:variable name="digits" select="'0123456789'" />
 
   <xsl:variable name="header" select="/xcb/@header" />
   <xsl:variable name="ucase-header"
@@ -62,19 +70,6 @@ authorization from the authors.
     <xsl:for-each select="/xcb/import">
       <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
     </xsl:for-each>
-    <xsl:choose>
-      <xsl:when test="$header='xproto'">
-        <path><xsl:value-of select="concat($base-path,
-                                           'xcb_types.xml')" /></path>
-      </xsl:when>
-      <xsl:when test="$header='xcb_types'" />
-      <xsl:otherwise>
-        <path><xsl:value-of select="concat($base-path,
-                                           'xproto.xml')" /></path>
-        <path><xsl:value-of select="concat($base-path,
-                                           'xcb_types.xml')" /></path>
-      </xsl:otherwise>
-    </xsl:choose>
   </xsl:variable>
   <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>
 
@@ -90,19 +85,129 @@ authorization from the authors.
     <xcb>
       <xsl:copy-of select="@*" />
       <xsl:if test="$ext">
-        <constant type="XCBExtension" name="XCB{$ext}Id">
+        <constant type="xcb_extension_t" name="{xcb:xcb-prefix()}_id">
           <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
         </constant>
-        <function type="const XCBQueryExtensionRep *" name="XCB{$ext}Init">
-          <field type="XCBConnection *" name="c" />
-          <l>return XCBGetExtensionData(c, &amp;XCB<!--
-          --><xsl:value-of select="$ext" />Id);</l>
-        </function>
       </xsl:if>
       <xsl:apply-templates mode="pass1" />
     </xcb>
   </xsl:template>
 
+  <func:function name="xcb:xcb-prefix">
+    <xsl:param name="name" />
+    <func:result>
+      <xsl:text>xcb</xsl:text>
+      <xsl:choose>
+        <xsl:when test="/xcb/@extension-name = 'RandR'">
+          <xsl:text>_randr</xsl:text>
+        </xsl:when>
+        <xsl:when test="/xcb/@extension-name = 'ScreenSaver'">
+          <xsl:text>_screensaver</xsl:text>
+        </xsl:when>
+        <xsl:when test="/xcb/@extension-name = 'XF86Dri'">
+          <xsl:text>_xf86dri</xsl:text>
+        </xsl:when>
+        <xsl:when test="/xcb/@extension-name = 'XFixes'">
+          <xsl:text>_xfixes</xsl:text>
+        </xsl:when>
+        <xsl:when test="/xcb/@extension-name = 'XvMC'">
+          <xsl:text>_xvmc</xsl:text>
+        </xsl:when>
+        <xsl:when test="/xcb/@extension-name">
+          <xsl:text>_</xsl:text>
+          <xsl:call-template name="camelcase-to-underscore">
+            <xsl:with-param name="camelcase" select="/xcb/@extension-name" />
+          </xsl:call-template>
+        </xsl:when>
+        <xsl:otherwise>
+        </xsl:otherwise>
+      </xsl:choose>
+      <xsl:if test="$name">
+        <xsl:text>_</xsl:text>
+        <xsl:call-template name="camelcase-to-underscore">
+          <xsl:with-param name="camelcase" select="$name" />
+        </xsl:call-template>
+      </xsl:if>
+    </func:result>
+  </func:function>
+
+  <func:function name="xcb:lowercase">
+    <xsl:param name="name" />
+    <func:result>
+      <xsl:call-template name="camelcase-to-underscore">
+        <xsl:with-param name="camelcase" select="$name" />
+      </xsl:call-template>
+    </func:result>
+  </func:function>
+
+  <func:function name="xcb:get-char-void">
+    <xsl:param name="name" />
+    <xsl:variable name="ctype" select="substring-before($name, '_t')" />
+    <func:result>
+      <xsl:choose>
+        <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
+          <xsl:value-of select="$ctype" />    
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:value-of select="$name" />
+        </xsl:otherwise>
+      </xsl:choose>
+    </func:result>
+  </func:function>
+
+  <func:function name="xcb:remove-void">
+    <xsl:param name="name" />
+    <xsl:variable name="ctype" select="substring-before($name, '_t')" />
+    <func:result>
+      <xsl:choose>
+        <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
+          <xsl:choose>
+            <xsl:when test="$ctype = 'void'">
+              <xsl:text>char</xsl:text>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:value-of select="$ctype" />
+            </xsl:otherwise>
+          </xsl:choose>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:value-of select="$name" />
+        </xsl:otherwise>
+      </xsl:choose>
+    </func:result>
+  </func:function>
+
+  <!-- split camel case into words and insert underscore -->
+  <xsl:template name="camelcase-to-underscore">
+    <xsl:param name="camelcase"/>
+    <xsl:choose>
+      <xsl:when test="$camelcase='CHAR2B' or $camelcase='INT64'
+                      or $camelcase='FLOAT32' or $camelcase='FLOAT64'
+                      or $camelcase='BOOL32' or $camelcase='STRING8'
+                      or $camelcase='Family_DECnet'">
+        <xsl:value-of select="translate($camelcase, $ucase, $lcase)"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:for-each select="str:split($camelcase, '')">
+          <xsl:variable name="a" select="."/>
+          <xsl:variable name="b" select="following::*[1]"/>
+          <xsl:variable name="c" select="following::*[2]"/>
+          <xsl:value-of select="translate(., $ucase, $lcase)"/>
+          <xsl:if test="($b and contains($lcase, $a) and contains($ucase, $b))
+                        or ($b and contains($digits, $a)
+                            and contains($letters, $b))
+                        or ($b and contains($letters, $a)
+                            and contains($digits, $b))
+                        or ($c and contains($ucase, $a)
+                            and contains($ucase, $b)
+                            and contains($lcase, $c))">
+            <xsl:text>_</xsl:text>
+          </xsl:if>
+        </xsl:for-each>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
   <!-- Modify names that conflict with C++ keywords by prefixing them with an
        underscore.  If the name parameter is not specified, it defaults to the
        value of the name attribute on the context node. -->
@@ -117,27 +222,26 @@ authorization from the authors.
 
   <!-- List of core types, for use in canonical-type-name. -->
   <xsl:variable name="core-types-rtf">
-    <type name="BOOL" />
-    <type name="BYTE" />
-    <type name="CARD8" />
-    <type name="CARD16" />
-    <type name="CARD32" />
-    <type name="INT8" />
-    <type name="INT16" />
-    <type name="INT32" />
-
-    <type name="char" />
-    <type name="void" />
-    <type name="float" />
-    <type name="double" />
-    <type name="XID" />
+    <type name="BOOL" newname="uint8" />
+    <type name="BYTE" newname="uint8" />
+    <type name="CARD8" newname="uint8" />
+    <type name="CARD16" newname="uint16" />
+    <type name="CARD32" newname="uint32" />
+    <type name="INT8" newname="int8" />
+    <type name="INT16" newname="int16" />
+    <type name="INT32" newname="int32" />
+
+    <type name="char" newname="char" />
+    <type name="void" newname="void" />
+    <type name="float" newname="float" />
+    <type name="double" newname="double" />
   </xsl:variable>
   <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />
 
   <!--
     Output the canonical name for a type.  This will be
-    XCB{extension-containing-Type-if-any}Type, wherever the type is found in
-    the search path, or just Type if not found.  If the type parameter is not
+    xcb_{extension-containing-type-if-any}_type, wherever the type is found in
+    the search path, or just type if not found.  If the type parameter is not
     specified, it defaults to the value of the type attribute on the context
     node.
   -->
@@ -159,14 +263,14 @@ authorization from the authors.
 
     <xsl:choose>
       <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
-        <xsl:value-of select="$type" />
+        <xsl:value-of select="$core-types/type[@name=$type]/@newname" />
       </xsl:when>
       <xsl:otherwise>
         <xsl:variable name="type-definitions"
                       select="(/xcb|document($search-path)/xcb
                               )[$is-unqualified or @header=$namespace]
-                               /*[((self::struct or self::union
-                                    or self::xidtype or self::enum
+                               /*[((self::struct or self::union or self::enum
+                                    or self::xidtype or self::xidunion
                                     or self::event or self::eventcopy
                                     or self::error or self::errorcopy)
                                    and @name=$unqualified-type)
@@ -175,9 +279,7 @@ authorization from the authors.
         <xsl:choose>
           <xsl:when test="count($type-definitions) = 1">
             <xsl:for-each select="$type-definitions">
-              <xsl:text>XCB</xsl:text>
-              <xsl:value-of select="concat(/xcb/@extension-name,
-                                           $unqualified-type)" />
+              <xsl:value-of select="xcb:xcb-prefix($unqualified-type)" />
             </xsl:for-each>
           </xsl:when>
           <xsl:when test="count($type-definitions) > 1">
@@ -211,126 +313,142 @@ authorization from the authors.
   </xsl:template>
   
   <!-- Helper template for requests, that outputs the cookie type.  The
-       context node must be the request. -->
+       parameter "request" must be the request node, which defaults to the
+       context node. -->
   <xsl:template name="cookie-type">
-    <xsl:text>XCB</xsl:text>
+    <xsl:param name="request" select="." />
     <xsl:choose>
-      <xsl:when test="reply">
-        <xsl:value-of select="concat($ext, @name)" />
+      <xsl:when test="$request/reply">
+        <xsl:value-of select="xcb:xcb-prefix($request/@name)" />
       </xsl:when>
       <xsl:otherwise>
-        <xsl:text>Void</xsl:text>
+        <xsl:text>xcb_void</xsl:text>
       </xsl:otherwise>
     </xsl:choose>
-    <xsl:text>Cookie</xsl:text>
+    <xsl:text>_cookie_t</xsl:text>
   </xsl:template>
 
+  <xsl:template name="request-function">
+    <xsl:param name="checked" />
+    <xsl:param name="req" />
+    <function>
+      <xsl:attribute name="name">
+        <xsl:value-of select="xcb:xcb-prefix($req/@name)" />
+        <xsl:if test="$checked='true' and not($req/reply)">_checked</xsl:if>
+        <xsl:if test="$checked='false' and $req/reply">_unchecked</xsl:if>
+      </xsl:attribute>
+      <xsl:attribute name="type">
+        <xsl:call-template name="cookie-type">
+          <xsl:with-param name="request" select="$req" />
+        </xsl:call-template>
+      </xsl:attribute>
+      <field type="xcb_connection_t *" name="c" />
+      <xsl:apply-templates select="$req/*[not(self::reply)]" mode="param" />
+      <do-request ref="{xcb:xcb-prefix($req/@name)}_request_t" opcode="{translate(xcb:xcb-prefix($req/@name), $lcase, $ucase)}"
+                  checked="{$checked}">
+        <xsl:if test="$req/reply">
+          <xsl:attribute name="has-reply">true</xsl:attribute>
+        </xsl:if>
+      </do-request>
+    </function>
+  </xsl:template>
+  
   <xsl:template match="request" mode="pass1">
+    <xsl:variable name="req" select="." />
     <xsl:if test="reply">
-      <struct name="XCB{$ext}{@name}Cookie">
+      <struct name="{xcb:xcb-prefix(@name)}_cookie_t">
         <field type="unsigned int" name="sequence" />
       </struct>
     </xsl:if>
-    <struct name="XCB{$ext}{@name}Req">
-      <field type="CARD8" name="major_opcode" no-assign="true" />
+    <constant type="number" name="{xcb:xcb-prefix($req/@name)}" value="{$req/@opcode}" />
+    <struct name="{xcb:xcb-prefix(@name)}_request_t">
+      <field type="uint8_t" name="major_opcode" no-assign="true" />
       <xsl:if test="$ext">
-        <field type="CARD8" name="minor_opcode" no-assign="true" />
+        <field type="uint8_t" name="minor_opcode" no-assign="true" />
       </xsl:if>
       <xsl:apply-templates select="*[not(self::reply)]" mode="field" />
       <middle>
-        <field type="CARD16" name="length" no-assign="true" />
+        <field type="uint16_t" name="length" no-assign="true" />
       </middle>
     </struct>
-    <function name="XCB{$ext}{@name}">
-      <xsl:attribute name="type">
-        <xsl:call-template name="cookie-type" />
-      </xsl:attribute>
-      <field type="XCBConnection *" name="c" />
-      <xsl:apply-templates select="*[not(self::reply)]" mode="param" />
-      <do-request ref="XCB{$ext}{@name}Req" opcode="{@opcode}">
-        <xsl:if test="reply">
-          <xsl:attribute name="has-reply">true</xsl:attribute>
-        </xsl:if>
-      </do-request>
-    </function>
+    <xsl:call-template name="request-function">
+      <xsl:with-param name="checked" select="'true'" />
+      <xsl:with-param name="req" select="$req" />
+    </xsl:call-template>
+    <xsl:call-template name="request-function">
+      <xsl:with-param name="checked" select="'false'" />
+      <xsl:with-param name="req" select="$req" />
+    </xsl:call-template>
     <xsl:if test="reply">
-      <struct name="XCB{$ext}{@name}Rep">
-        <field type="BYTE" name="response_type" />
+      <struct name="{xcb:xcb-prefix(@name)}_reply_t">
+        <field type="uint8_t" name="response_type" />
         <xsl:apply-templates select="reply/*" mode="field" />
         <middle>
-          <field type="CARD16" name="sequence" />
-          <field type="CARD32" name="length" />
+          <field type="uint16_t" name="sequence" />
+          <field type="uint32_t" name="length" />
         </middle>
       </struct>
-      <iterator-functions ref="XCB{$ext}{@name}" kind="Rep" />
-      <function type="XCB{$ext}{@name}Rep *" name="XCB{$ext}{@name}Reply">
-        <field type="XCBConnection *" name="c" />
+      <iterator-functions ref="{xcb:xcb-prefix(@name)}" kind="_reply" />
+      <function type="{xcb:xcb-prefix(@name)}_reply_t *" name="{xcb:xcb-prefix(@name)}_reply">
+        <field type="xcb_connection_t *" name="c" />
         <field name="cookie">
           <xsl:attribute name="type">
             <xsl:call-template name="cookie-type" />
           </xsl:attribute>
         </field>
-        <field type="XCBGenericError **" name="e" />
-        <l>return (XCB<xsl:value-of select="concat($ext, @name)" />Rep *)<!--
-        --> XCBWaitForReply(c, cookie.sequence, e);</l>
+        <field type="xcb_generic_error_t **" name="e" />
+        <l>return (<xsl:value-of select="xcb:xcb-prefix(@name)" />_reply_t *)<!--
+        --> xcb_wait_for_reply(c, cookie.sequence, e);</l>
       </function>
     </xsl:if>
   </xsl:template>
 
-  <xsl:template match="xidtype" mode="pass1">
-    <struct name="XCB{$ext}{@name}">
-      <field type="CARD32" name="xid" />
-    </struct>
-    <iterator ref="XCB{$ext}{@name}" />
-    <iterator-functions ref="XCB{$ext}{@name}" />
-    <function type="XCB{$ext}{@name}" name="XCB{$ext}{@name}New">
-      <field type="XCBConnection *" name="c" />
-      <l>XCB<xsl:value-of select="concat($ext, @name)" /> ret;</l>
-      <l>ret.xid = XCBGenerateID(c);</l>
-      <l>return ret;</l>
-    </function>
+  <xsl:template match="xidtype|xidunion" mode="pass1">
+    <typedef oldname="uint32_t" newname="{xcb:xcb-prefix(@name)}_t" />
+    <iterator ref="{xcb:xcb-prefix(@name)}" />
+    <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
   </xsl:template>
 
   <xsl:template match="struct|union" mode="pass1">
-    <struct name="XCB{$ext}{@name}">
+    <struct name="{xcb:xcb-prefix(@name)}_t">
       <xsl:if test="self::union">
         <xsl:attribute name="kind">union</xsl:attribute>
       </xsl:if>
       <xsl:apply-templates select="*" mode="field" />
     </struct>
-    <iterator ref="XCB{$ext}{@name}" />
-    <iterator-functions ref="XCB{$ext}{@name}" />
+    <iterator ref="{xcb:xcb-prefix(@name)}" />
+    <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
   </xsl:template>
 
   <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
     <xsl:variable name="suffix">
       <xsl:choose>
         <xsl:when test="self::event|self::eventcopy">
-          <xsl:text>Event</xsl:text>
+          <xsl:text>_event_t</xsl:text>
         </xsl:when>
         <xsl:when test="self::error|self::errorcopy">
-          <xsl:text>Error</xsl:text>
+          <xsl:text>_error_t</xsl:text>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>
-    <constant type="number" name="XCB{$ext}{@name}" value="{@number}" />
+    <constant type="number" name="{xcb:xcb-prefix(@name)}" value="{@number}" />
     <xsl:choose>
       <xsl:when test="self::event|self::error">
-        <struct name="XCB{$ext}{@name}{$suffix}">
-          <field type="BYTE" name="response_type" />
+        <struct name="{xcb:xcb-prefix(@name)}{$suffix}">
+          <field type="uint8_t" name="response_type" />
           <xsl:if test="self::error">
-            <field type="BYTE" name="error_code" />
+            <field type="uint8_t" name="error_code" />
           </xsl:if>
           <xsl:apply-templates select="*" mode="field" />
           <xsl:if test="not(self::event and boolean(@no-sequence-number))">
             <middle>
-              <field type="CARD16" name="sequence" />
+              <field type="uint16_t" name="sequence" />
             </middle>
           </xsl:if>
         </struct>
       </xsl:when>
       <xsl:when test="self::eventcopy|self::errorcopy">
-        <typedef newname="XCB{$ext}{@name}{$suffix}">
+        <typedef newname="{xcb:xcb-prefix(@name)}{$suffix}">
           <xsl:attribute name="oldname">
             <xsl:call-template name="canonical-type-name">
               <xsl:with-param name="type" select="@ref" />
@@ -348,21 +466,23 @@ authorization from the authors.
         <xsl:call-template name="canonical-type-name">
           <xsl:with-param name="type" select="@oldname" />
         </xsl:call-template>
+        <xsl:text>_t</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="newname">
         <xsl:call-template name="canonical-type-name">
           <xsl:with-param name="type" select="@newname" />
         </xsl:call-template>
+        <xsl:text>_t</xsl:text>
       </xsl:attribute>
     </typedef>
-    <iterator ref="XCB{$ext}{@newname}" />
-    <iterator-functions ref="XCB{$ext}{@newname}" />
+    <iterator ref="{xcb:xcb-prefix(@newname)}" />
+    <iterator-functions ref="{xcb:xcb-prefix(@newname)}" />
   </xsl:template>
 
   <xsl:template match="enum" mode="pass1">
-    <enum name="XCB{$ext}{@name}">
+    <enum name="{xcb:xcb-prefix(@name)}_t">
       <xsl:for-each select="item">
-        <item name="XCB{$ext}{../@name}{@name}">
+        <item name="{translate(xcb:xcb-prefix(concat(../@name, concat('_', @name))), $lcase, $ucase)}">
           <xsl:copy-of select="*" />
         </item>
       </xsl:for-each>
@@ -381,6 +501,7 @@ authorization from the authors.
     <xsl:copy>
       <xsl:attribute name="type">
         <xsl:call-template name="canonical-type-name" />
+        <xsl:text>_t</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
         <xsl:call-template name="canonical-var-name" />
@@ -391,7 +512,8 @@ authorization from the authors.
 
   <xsl:template match="list" mode="field">
     <xsl:variable name="type"><!--
-      --><xsl:call-template name="canonical-type-name" /><!--
+      --><xsl:call-template name="canonical-type-name" />
+        <xsl:text>_t</xsl:text><!--
     --></xsl:variable>
     <list type="{$type}">
       <xsl:attribute name="name">
@@ -442,6 +564,7 @@ authorization from the authors.
         <xsl:call-template name="canonical-type-name">
           <xsl:with-param name="type" select="@value-mask-type" />
         </xsl:call-template>
+        <xsl:text>_t</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
         <xsl:call-template name="canonical-var-name">
@@ -449,13 +572,13 @@ authorization from the authors.
         </xsl:call-template>
       </xsl:attribute>
     </field>
-    <list type="CARD32">
+    <list type="uint32_t">
       <xsl:attribute name="name">
         <xsl:call-template name="canonical-var-name">
           <xsl:with-param name="name" select="@value-list-name" />
         </xsl:call-template>
       </xsl:attribute>
-      <function-call name="XCBPopcount">
+      <function-call name="xcb_popcount">
         <param>
           <fieldref>
             <xsl:call-template name="canonical-var-name">
@@ -467,10 +590,11 @@ authorization from the authors.
     </list>
   </xsl:template>
 
-  <xsl:template match="field|localfield" mode="param">
+  <xsl:template match="field" mode="param">
     <field>
       <xsl:attribute name="type">
         <xsl:call-template name="canonical-type-name" />
+        <xsl:text>_t</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
         <xsl:call-template name="canonical-var-name" />
@@ -481,12 +605,18 @@ authorization from the authors.
   <xsl:template match="list" mode="param">
     <!-- If no length expression is provided, use a CARD32 localfield. -->
     <xsl:if test="not(node())">
-      <field type="CARD32" name="{@name}_len" />
+      <field type="uint32_t" name="{@name}_len" />
     </xsl:if>
     <field>
+      <xsl:variable name="ctype">
+        <xsl:call-template name="canonical-type-name" />
+      </xsl:variable>
       <xsl:attribute name="type">
         <xsl:text>const </xsl:text>
         <xsl:call-template name="canonical-type-name" />
+        <xsl:if test="not($ctype='char') and not($ctype='void')">
+          <xsl:text>_t</xsl:text>
+        </xsl:if>
         <xsl:text> *</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
@@ -501,6 +631,7 @@ authorization from the authors.
         <xsl:call-template name="canonical-type-name">
           <xsl:with-param name="type" select="@value-mask-type" />
         </xsl:call-template>
+        <xsl:text>_t</xsl:text>
       </xsl:attribute>
       <xsl:attribute name="name">
         <xsl:call-template name="canonical-var-name">
@@ -508,7 +639,7 @@ authorization from the authors.
         </xsl:call-template>
       </xsl:attribute>
     </field>
-    <field type="const CARD32 *">
+    <field type="const uint32_t *">
       <xsl:attribute name="name">
         <xsl:call-template name="canonical-var-name">
           <xsl:with-param name="name" select="@value-list-name" />
@@ -569,14 +700,14 @@ authorization from the authors.
 
     <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
 
-    <l>static const XCBProtocolRequest xcb_req = {</l>
+    <l>static const xcb_protocol_request_t xcb_req = {</l>
     <indent>
       <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
       <l>/* ext */ <xsl:choose>
                      <xsl:when test="$ext">
-                       <xsl:text>&amp;XCB</xsl:text>
-                       <xsl:value-of select="$ext" />
-                       <xsl:text>Id</xsl:text>
+                       <xsl:text>&amp;</xsl:text>
+                       <xsl:value-of select="xcb:xcb-prefix()" />
+                       <xsl:text>_id</xsl:text>
                      </xsl:when>
                      <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>,</l>
@@ -591,33 +722,44 @@ authorization from the authors.
     <l><xsl:value-of select="@ref" /> xcb_out;</l>
 
     <l />
-    <xsl:apply-templates select="$struct//*[(self::field or self::exprfield)
+    <xsl:if test="not ($ext) and not($struct//*[(self::field or self::exprfield or self::pad)
+                                                and not(boolean(@no-assign))])">
+      <l>xcb_out.pad0 = 0;</l>
+    </xsl:if>
+    <xsl:apply-templates select="$struct//*[(self::field or self::exprfield or self::pad)
                                             and not(boolean(@no-assign))]"
                          mode="assign" />
 
     <l />
-    <l>xcb_parts[2].iov_base = &amp;xcb_out;</l>
+    <l>xcb_parts[2].iov_base = (char *) &amp;xcb_out;</l>
     <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
     <l>xcb_parts[3].iov_base = 0;</l>
     <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len &amp; 3;</l>
 
     <xsl:for-each select="$struct/list">
-      <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (void *) <!--
+      <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
       --><xsl:value-of select="@name" />;</l>
       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
-      --><xsl:apply-templates mode="output-expression" /><!--
-      --><xsl:if test="not(@type = 'void')">
+      --><xsl:apply-templates mode="output-expression" />
+      <xsl:if test="not(@type = 'void_t')">
         <xsl:text> * sizeof(</xsl:text>
-        <xsl:value-of select="@type" />
+          <xsl:choose>
+          <xsl:when test="@type='char_t'">
+            <xsl:text>char</xsl:text>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:value-of select="@type" />
+          </xsl:otherwise>
+        </xsl:choose>
         <xsl:text>)</xsl:text>
       </xsl:if>;</l>
       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_len = -xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len &amp; 3;</l>
     </xsl:for-each>
 
-    <l>xcb_ret.sequence = XCBSendRequest(c, <!--
+    <l>xcb_ret.sequence = xcb_send_request(c, <!--
     --><xsl:choose>
-         <xsl:when test="@has-reply">XCB_REQUEST_CHECKED</xsl:when>
+         <xsl:when test="@checked='true'">XCB_REQUEST_CHECKED</xsl:when>
          <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>, xcb_parts + 2, &amp;xcb_req);</l>
     <l>return xcb_ret;</l>
@@ -643,51 +785,39 @@ authorization from the authors.
     </l>
   </xsl:template>
 
+  <xsl:template match="pad" mode="assign">
+    <xsl:variable name="padnum"><xsl:number /></xsl:variable>
+    <l><xsl:choose>
+        <xsl:when test="@bytes = 1">xcb_out.pad<xsl:value-of select="$padnum - 1" /> = 0;</xsl:when>
+        <xsl:otherwise>memset(xcb_out.pad<xsl:value-of select="$padnum - 1" />, 0, <xsl:value-of select="@bytes" />);</xsl:otherwise>
+    </xsl:choose></l>
+  </xsl:template>
+
   <xsl:template match="iterator" mode="pass2">
-    <struct name="{@ref}Iter">
-      <field type="{@ref} *" name="data" />
+    <struct name="{@ref}_iterator_t">
+      <field type="{@ref}_t *" name="data" />
       <field type="int" name="rem" />
       <field type="int" name="index" />
     </struct>
   </xsl:template>
 
-  <!-- Change a_name_like_this to ANameLikeThis.  If the parameter name is not
-       given, it defaults to the name attribute of the context node. -->
-  <xsl:template name="capitalize">
-    <xsl:param name="name" select="string(@name)" />
-    <xsl:if test="$name">
-      <xsl:value-of select="translate(substring($name,1,1), $lcase, $ucase)" />
-      <xsl:choose>
-        <xsl:when test="contains($name, '_')">
-          <xsl:value-of select="substring(substring-before($name, '_'), 2)" />
-          <xsl:call-template name="capitalize">
-            <xsl:with-param name="name" select="substring-after($name, '_')" />
-          </xsl:call-template>
-        </xsl:when>
-        <xsl:otherwise>
-          <xsl:value-of select="substring($name, 2)" />
-        </xsl:otherwise>
-      </xsl:choose>
-    </xsl:if>
-  </xsl:template>
-
   <xsl:template match="iterator-functions" mode="pass2">
     <xsl:variable name="ref" select="@ref" />
     <xsl:variable name="kind" select="@kind" />
     <xsl:variable name="struct"
-                  select="$pass1/xcb/struct[@name=concat($ref,$kind)]" />
+                  select="$pass1/xcb/struct[@name=concat($ref, $kind, '_t')]" />
     <xsl:variable name="nextfields-rtf">
       <nextfield>R + 1</nextfield>
       <xsl:for-each select="$struct/list[not(@fixed)]">
         <xsl:choose>
-          <xsl:when test="substring(@type, 1, 3) = 'XCB'">
-            <nextfield><xsl:value-of select="@type" />End(<!--
-            --><xsl:value-of select="$ref" /><!--
-            --><xsl:call-template name="capitalize" />Iter(R))</nextfield>
+          <xsl:when test="substring(@type, 1, 3) = 'xcb'">
+            <nextfield><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_end(<!--
+            --><xsl:value-of select="$ref" />_<!--
+            --><xsl:value-of select="string(@name)" />_iterator(R))</nextfield>
           </xsl:when>
           <xsl:otherwise>
-            <nextfield><xsl:value-of select="$ref" /><!--
-            --><xsl:call-template name="capitalize" />End(R)</nextfield>
+            <nextfield><xsl:value-of select="$ref" />_<!--
+            --><xsl:value-of select="string(@name)" />_end(R)</nextfield>
           </xsl:otherwise>
         </xsl:choose>
       </xsl:for-each>
@@ -699,52 +829,51 @@ authorization from the authors.
       <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
       <xsl:variable name="is-first"
                     select="not(preceding-sibling::list[not(@fixed)])" />
-      <xsl:variable name="field-name"><!--
-        --><xsl:call-template name="capitalize" /><!--
-      --></xsl:variable>
+      <xsl:variable name="field-name" select="@name" />
       <xsl:variable name="is-variable"
                     select="$pass1/xcb/struct[@name=current()/@type]/list
                             or document($search-path)/xcb
-                               /struct[concat('XCB',
-                                              ancestor::xcb/@extension-name,
-                                              @name) = current()/@type]
-                               /*[self::valueparam or self::list]" />
+                               /struct[concat(xcb:xcb-prefix(@name), '_t')
+                                       = current()/@type]
+                               /*[self::valueparam
+                                  or self::list[.//*[not(self::value
+                                                         or self::op)]]]" />
       <xsl:if test="not($is-variable)">
-        <function type="{@type} *" name="{$ref}{$field-name}">
-          <field type="{$ref}{$kind} *" name="R" />
+        <function type="{xcb:get-char-void(@type)} *" name="{$ref}_{xcb:lowercase($field-name)}">
+          <field type="const {$ref}{$kind}_t *" name="R" />
           <xsl:choose>
             <xsl:when test="$is-first">
-              <l>return (<xsl:value-of select="@type" /> *) <!--
+              <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
               -->(<xsl:value-of select="$nextfield" />);</l>
             </xsl:when>
             <xsl:otherwise>
-              <l>XCBGenericIter prev = <!--
+              <l>xcb_generic_iterator_t prev = <!--
               --><xsl:value-of select="$nextfield" />;</l>
-              <l>return (<xsl:value-of select="@type" /> *) <!--
+              <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
               -->((char *) prev.data + XCB_TYPE_PAD(<!--
-              --><xsl:value-of select="@type" />, prev.index));</l>
+              --><xsl:value-of select="xcb:get-char-void(@type)" />, prev.index));</l>
             </xsl:otherwise>
           </xsl:choose>
         </function>
       </xsl:if>
-      <function type="int" name="{$ref}{$field-name}Length">
-        <field type="{$ref}{$kind} *" name="R" />
+      <function type="int" name="{$ref}_{xcb:lowercase($field-name)}_length">
+        <field type="const {$ref}{$kind}_t *" name="R" />
         <l>return <xsl:apply-templates mode="output-expression">
                     <xsl:with-param name="field-prefix" select="'R->'" />
                   </xsl:apply-templates>;</l>
       </function>
       <xsl:choose>
-        <xsl:when test="substring(@type, 1, 3) = 'XCB'">
-          <function type="{@type}Iter" name="{$ref}{$field-name}Iter">
-            <field type="{$ref}{$kind} *" name="R" />
-            <l><xsl:value-of select="@type" />Iter i;</l>
+        <xsl:when test="substring(@type, 1, 3) = 'xcb'">
+          <function type="{substring(@type, 1, string-length(@type)-2)}_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_iterator">
+            <field type="const {$ref}{$kind}_t *" name="R" />
+            <l><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_iterator_t i;</l>
             <xsl:choose>
               <xsl:when test="$is-first">
                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
                 -->(<xsl:value-of select="$nextfield" />);</l>
               </xsl:when>
               <xsl:otherwise>
-                <l>XCBGenericIter prev = <!--
+                <l>xcb_generic_iterator_t prev = <!--
                 --><xsl:value-of select="$nextfield" />;</l>
                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
                 -->((char *) prev.data + XCB_TYPE_PAD(<!--
@@ -765,21 +894,21 @@ authorization from the authors.
               <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
             </xsl:choose>
           </xsl:variable>
-          <function type="XCBGenericIter" name="{$ref}{$field-name}End">
-            <field type="{$ref}{$kind} *" name="R" />
-            <l>XCBGenericIter i;</l>
+          <function type="xcb_generic_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_end">
+            <field type="const {$ref}{$kind}_t *" name="R" />
+            <l>xcb_generic_iterator_t i;</l>
             <xsl:choose>
               <xsl:when test="$is-first">
-                <l>i.data = ((<xsl:value-of select="$cast" /> *) <!--
+                <l>i.data = ((<xsl:value-of select="xcb:remove-void($cast)" /> *) <!--
                 -->(<xsl:value-of select="$nextfield" />)) + (<!--
                 --><xsl:apply-templates mode="output-expression">
                      <xsl:with-param name="field-prefix" select="'R->'" />
                    </xsl:apply-templates>);</l>
               </xsl:when>
               <xsl:otherwise>
-                <l>XCBGenericIter child = <!--
+                <l>xcb_generic_iterator_t child = <!--
                 --><xsl:value-of select="$nextfield" />;</l>
-                <l>i.data = ((<xsl:value-of select="$cast" /> *) <!--
+                <l>i.data = ((<xsl:value-of select="xcb:get-char-void($cast)" /> *) <!--
                 -->child.data) + (<!--
                 --><xsl:apply-templates mode="output-expression">
                      <xsl:with-param name="field-prefix" select="'R->'" />
@@ -794,32 +923,32 @@ authorization from the authors.
       </xsl:choose>
     </xsl:for-each>
     <xsl:if test="not($kind)">
-      <function type="void" name="{$ref}Next">
-        <field type="{$ref}Iter *" name="i" />
+      <function type="void" name="{$ref}_next">
+        <field type="{$ref}_iterator_t *" name="i" />
         <xsl:choose>
           <xsl:when test="$struct/list[not(@fixed)]">
-            <l><xsl:value-of select="$ref" /> *R = i->data;</l>
-            <l>XCBGenericIter child = <!--
+            <l><xsl:value-of select="$ref" />_t *R = i->data;</l>
+            <l>xcb_generic_iterator_t child = <!--
             --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
             <l>--i->rem;</l>
-            <l>i->data = (<xsl:value-of select="$ref" /> *) child.data;</l>
+            <l>i->data = (<xsl:value-of select="$ref" />_t *) child.data;</l>
             <l>i->index = child.index;</l>
           </xsl:when>
           <xsl:otherwise>
             <l>--i->rem;</l>
             <l>++i->data;</l>
-            <l>i->index += sizeof(<xsl:value-of select="$ref" />);</l>
+            <l>i->index += sizeof(<xsl:value-of select="$ref" />_t);</l>
           </xsl:otherwise>
         </xsl:choose>
       </function>
-      <function type="XCBGenericIter" name="{$ref}End">
-        <field type="{$ref}Iter" name="i" />
-        <l>XCBGenericIter ret;</l>
+      <function type="xcb_generic_iterator_t" name="{$ref}_end">
+        <field type="{$ref}_iterator_t" name="i" />
+        <l>xcb_generic_iterator_t ret;</l>
         <xsl:choose>
           <xsl:when test="$struct/list[not(@fixed)]">
             <l>while(i.rem > 0)</l>
             <indent>
-              <l><xsl:value-of select="$ref" />Next(&amp;i);</l>
+              <l><xsl:value-of select="$ref" />_next(&amp;i);</l>
             </indent>
             <l>ret.data = i.data;</l>
             <l>ret.rem = i.rem;</l>
@@ -864,8 +993,16 @@ authorization from the authors.
  * Edit at your peril.
  */
 </xsl:text>
-
 <xsl:if test="$h"><xsl:text>
+/**
+ * @defgroup XCB_</xsl:text><xsl:value-of select="$ext" /><xsl:text>_API XCB </xsl:text><xsl:value-of select="$ext" /><xsl:text> API
+ * @brief </xsl:text><xsl:value-of select="$ext" /><xsl:text> XCB Protocol Implementation.</xsl:text>
+<xsl:text>
+ * @{
+ **/
+</xsl:text>
+
+<xsl:text>
 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
 </xsl:text>
@@ -877,8 +1014,23 @@ authorization from the authors.
 <xsl:text>
 </xsl:text>
 </xsl:if>
+<xsl:if test="$h">
+    <xsl:choose>
+        <xsl:when test="string($ext)">
+  <xsl:text>#define XCB_</xsl:text><xsl:value-of select="translate($ext, $lcase, $ucase)"/><xsl:text>_MAJOR_VERSION </xsl:text><xsl:value-of select="/xcb/@major-version" /><xsl:text>
+</xsl:text>
+  <xsl:text>#define XCB_</xsl:text><xsl:value-of select="translate($ext, $lcase, $ucase)"/><xsl:text>_MINOR_VERSION </xsl:text><xsl:value-of select="/xcb/@minor-version" />
+  <xsl:text>
+  
+</xsl:text>
+    </xsl:when>
+  </xsl:choose>
+</xsl:if>
 
-<xsl:if test="$c"><xsl:text>
+<xsl:if test="$c">
+<xsl:if test="$need-string-h">
+#include &lt;string.h&gt;</xsl:if>
+<xsl:text>
 #include &lt;assert.h&gt;
 #include "xcbext.h"
 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
@@ -890,6 +1042,10 @@ authorization from the authors.
 <xsl:if test="$h">
 <xsl:text>
 #endif
+
+/**
+ * @}
+ */
 </xsl:text>
 </xsl:if>
   </xsl:template>
@@ -898,8 +1054,10 @@ authorization from the authors.
     <xsl:choose>
       <xsl:when test="@type = 'number'">
         <xsl:if test="$h">
+          <xsl:text>/** Opcode for </xsl:text><xsl:value-of select="@name"/><xsl:text>. */
+</xsl:text>
           <xsl:text>#define </xsl:text>
-          <xsl:value-of select="@name" />
+          <xsl:value-of select="translate(@name, $lcase, $ucase)" />
           <xsl:text> </xsl:text>
           <xsl:value-of select="@value" />
           <xsl:text>
@@ -942,7 +1100,7 @@ authorization from the authors.
   <xsl:template match="typedef" mode="output">
     <xsl:if test="$h">
       <xsl:text>typedef </xsl:text>
-      <xsl:value-of select="@oldname" />
+      <xsl:value-of select="xcb:get-char-void(@oldname)" />
       <xsl:text> </xsl:text>
       <xsl:value-of select="@newname" />
       <xsl:text>;
@@ -958,8 +1116,14 @@ authorization from the authors.
           <xsl:with-param name="items" select="field/@type" />
         </xsl:call-template>
       </xsl:variable>
+      <xsl:text>/**
+ * @brief </xsl:text><xsl:value-of select="@name" /><xsl:text>
+ **/
+</xsl:text>
       <xsl:text>typedef </xsl:text>
       <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
+      <xsl:text> </xsl:text>
+      <xsl:value-of select="@name" />
       <xsl:text> {
 </xsl:text>
       <xsl:for-each select="exprfield|field|list[@fixed]|pad">
@@ -967,7 +1131,7 @@ authorization from the authors.
         <xsl:apply-templates select=".">
           <xsl:with-param name="type-lengths" select="$type-lengths" />
         </xsl:apply-templates>
-        <xsl:text>;
+        <xsl:text>; /**&lt; </xsl:text><xsl:text> */
 </xsl:text>
       </xsl:for-each>
       <xsl:text>} </xsl:text>
@@ -980,7 +1144,9 @@ authorization from the authors.
 
   <xsl:template match="enum" mode="output">
     <xsl:if test="$h">
-      <xsl:text>typedef enum {
+      <xsl:text>typedef enum </xsl:text>
+      <xsl:value-of select="@name" />
+      <xsl:text> {
     </xsl:text>
       <xsl:call-template name="list">
         <xsl:with-param name="separator"><xsl:text>,
@@ -1010,8 +1176,31 @@ authorization from the authors.
       <xsl:call-template name="type-lengths">
         <xsl:with-param name="items" select="field/@type" />
       </xsl:call-template>
-    </xsl:variable>
-    <xsl:value-of select="@type" />
+  </xsl:variable>
+  <!-- Doxygen for functions in header. -->
+/*****************************************************************************
+ **
+ ** <xsl:value-of select="@type" />
+ <xsl:text> </xsl:text>
+ <xsl:value-of select="@name" />
+ ** <xsl:call-template name="list">
+     <xsl:with-param name="items">
+         <xsl:for-each select="field">
+             <item>
+                 <xsl:text>
+ ** @param </xsl:text>
+                 <xsl:apply-templates select=".">
+                     <xsl:with-param name="type-lengths" select="$type-lengths" />
+                 </xsl:apply-templates>
+             </item>
+         </xsl:for-each>
+     </xsl:with-param>
+ </xsl:call-template>
+ ** @returns <xsl:value-of select="@type" />
+ **
+ *****************************************************************************/
+<xsl:value-of select="@type" />
     <xsl:text>
 </xsl:text>
     <xsl:value-of select="$decl-open" />
@@ -1029,6 +1218,7 @@ authorization from the authors.
             <xsl:apply-templates select=".">
               <xsl:with-param name="type-lengths" select="$type-lengths" />
             </xsl:apply-templates>
+            <xsl:text>  /**&lt; */</xsl:text>
           </item>
         </xsl:for-each>
       </xsl:with-param>
@@ -1089,6 +1279,12 @@ authorization from the authors.
     <xsl:text>)</xsl:text>
   </xsl:template>
 
+  <xsl:template match="bit" mode="output-expression">
+    <xsl:text>(1 &lt;&lt; </xsl:text>
+    <xsl:value-of select="." />
+    <xsl:text>)</xsl:text>
+  </xsl:template>
+
   <xsl:template match="function-call" mode="output-expression">
     <xsl:param name="field-prefix" />
     <xsl:value-of select="@name" />
@@ -1137,7 +1333,7 @@ authorization from the authors.
     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
 
     <xsl:call-template name="type-and-name">
-      <xsl:with-param name="type" select="'CARD8'" />
+      <xsl:with-param name="type" select="'uint8_t'" />
       <xsl:with-param name="name">
         <xsl:text>pad</xsl:text>
         <xsl:value-of select="$padnum - 1" />