Modify new attribute from previous patch so that it is necessary only on
[free-sw/xcb/libxcb] / src / c-client.xsl
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 Copyright (C) 2004 Josh Triplett.  All Rights Reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
26 -->
27 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
28                version="1.0"
29                xmlns:e="http://exslt.org/common"
30                xmlns:func="http://exslt.org/functions"
31                xmlns:str="http://exslt.org/strings"
32                xmlns:xcb="http://xcb.freedesktop.org"
33                extension-element-prefixes="func str xcb">
34   
35   <xsl:output method="text" />
36
37   <xsl:strip-space elements="*" />
38
39   <!-- "header" or "source" -->
40   <xsl:param name="mode" />
41
42   <!-- Path to the core protocol descriptions. -->
43   <xsl:param name="base-path" />
44
45   <!-- Path to the extension protocol descriptions. -->
46   <xsl:param name="extension-path" select="$base-path" />
47
48   <xsl:variable name="h" select="$mode = 'header'" />
49   <xsl:variable name="c" select="$mode = 'source'" />
50
51   <xsl:variable name="need-string-h" select="//request/pad[@bytes != 1]" />
52   
53   <!-- String used to indent lines of code. -->
54   <xsl:variable name="indent-string" select="'    '" />
55
56   <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
57   <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
58   <xsl:variable name="letters" select="concat($ucase, $lcase)" />
59   <xsl:variable name="digits" select="'0123456789'" />
60
61   <xsl:variable name="header" select="/xcb/@header" />
62   <xsl:variable name="ucase-header"
63                 select="translate($header,$lcase,$ucase)" />
64
65   <xsl:variable name="ext" select="/xcb/@extension-name" />
66
67   <!-- Other protocol descriptions to search for types in, after checking the
68        current protocol description. -->
69   <xsl:variable name="search-path-rtf">
70     <xsl:for-each select="/xcb/import">
71       <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
72     </xsl:for-each>
73   </xsl:variable>
74   <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>
75
76   <xsl:variable name="root" select="/" />
77   
78   <!-- First pass: Store everything in a variable. -->
79   <xsl:variable name="pass1-rtf">
80     <xsl:apply-templates select="/" mode="pass1" />
81   </xsl:variable>
82   <xsl:variable name="pass1" select="e:node-set($pass1-rtf)" />
83   
84   <xsl:template match="xcb" mode="pass1">
85     <xcb>
86       <xsl:copy-of select="@*" />
87       <xsl:if test="$ext">
88         <constant type="xcb_extension_t" name="{xcb:xcb-prefix()}_id">
89           <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
90         </constant>
91       </xsl:if>
92       <xsl:apply-templates mode="pass1" />
93     </xcb>
94   </xsl:template>
95
96   <func:function name="xcb:xcb-prefix">
97     <xsl:param name="name" />
98     <func:result>
99       <xsl:text>xcb</xsl:text>
100       <xsl:if test="/xcb/@extension-name">
101         <xsl:text>_</xsl:text>
102         <xsl:choose>
103           <xsl:when test="/xcb/@extension-multiword = 'true' or /xcb/@extension-multiword = '1'">
104             <xsl:call-template name="camelcase-to-underscore">
105               <xsl:with-param name="camelcase" select="/xcb/@extension-name" />
106             </xsl:call-template>
107           </xsl:when>
108           <xsl:otherwise>
109             <xsl:value-of select="translate(/xcb/@extension-name, $ucase, $lcase)"/>
110           </xsl:otherwise>
111         </xsl:choose>
112       </xsl:if>
113       <xsl:if test="$name">
114         <xsl:text>_</xsl:text>
115         <xsl:call-template name="camelcase-to-underscore">
116           <xsl:with-param name="camelcase" select="$name" />
117         </xsl:call-template>
118       </xsl:if>
119     </func:result>
120   </func:function>
121
122   <func:function name="xcb:lowercase">
123     <xsl:param name="name" />
124     <func:result>
125       <xsl:call-template name="camelcase-to-underscore">
126         <xsl:with-param name="camelcase" select="$name" />
127       </xsl:call-template>
128     </func:result>
129   </func:function>
130
131   <func:function name="xcb:get-char-void">
132     <xsl:param name="name" />
133     <xsl:variable name="ctype" select="substring-before($name, '_t')" />
134     <func:result>
135       <xsl:choose>
136         <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
137           <xsl:value-of select="$ctype" />    
138         </xsl:when>
139         <xsl:otherwise>
140           <xsl:value-of select="$name" />
141         </xsl:otherwise>
142       </xsl:choose>
143     </func:result>
144   </func:function>
145
146   <func:function name="xcb:remove-void">
147     <xsl:param name="name" />
148     <xsl:variable name="ctype" select="substring-before($name, '_t')" />
149     <func:result>
150       <xsl:choose>
151         <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
152           <xsl:choose>
153             <xsl:when test="$ctype = 'void'">
154               <xsl:text>char</xsl:text>
155             </xsl:when>
156             <xsl:otherwise>
157               <xsl:value-of select="$ctype" />
158             </xsl:otherwise>
159           </xsl:choose>
160         </xsl:when>
161         <xsl:otherwise>
162           <xsl:value-of select="$name" />
163         </xsl:otherwise>
164       </xsl:choose>
165     </func:result>
166   </func:function>
167
168   <!-- split camel case into words and insert underscore -->
169   <xsl:template name="camelcase-to-underscore">
170     <xsl:param name="camelcase"/>
171     <xsl:choose>
172       <xsl:when test="$camelcase='CHAR2B' or $camelcase='INT64'
173                       or $camelcase='FLOAT32' or $camelcase='FLOAT64'
174                       or $camelcase='BOOL32' or $camelcase='STRING8'
175                       or $camelcase='Family_DECnet'">
176         <xsl:value-of select="translate($camelcase, $ucase, $lcase)"/>
177       </xsl:when>
178       <xsl:otherwise>
179         <xsl:for-each select="str:split($camelcase, '')">
180           <xsl:variable name="a" select="."/>
181           <xsl:variable name="b" select="following::*[1]"/>
182           <xsl:variable name="c" select="following::*[2]"/>
183           <xsl:value-of select="translate(., $ucase, $lcase)"/>
184           <xsl:if test="($b and contains($lcase, $a) and contains($ucase, $b))
185                         or ($b and contains($digits, $a)
186                             and contains($letters, $b))
187                         or ($b and contains($letters, $a)
188                             and contains($digits, $b))
189                         or ($c and contains($ucase, $a)
190                             and contains($ucase, $b)
191                             and contains($lcase, $c))">
192             <xsl:text>_</xsl:text>
193           </xsl:if>
194         </xsl:for-each>
195       </xsl:otherwise>
196     </xsl:choose>
197   </xsl:template>
198
199   <!-- Modify names that conflict with C++ keywords by prefixing them with an
200        underscore.  If the name parameter is not specified, it defaults to the
201        value of the name attribute on the context node. -->
202   <xsl:template name="canonical-var-name">
203     <xsl:param name="name" select="@name" />
204     <xsl:if test="$name='new' or $name='delete'
205                   or $name='class' or $name='operator'">
206       <xsl:text>_</xsl:text>
207     </xsl:if>
208     <xsl:value-of select="$name" />
209   </xsl:template>
210
211   <!-- List of core types, for use in canonical-type-name. -->
212   <xsl:variable name="core-types-rtf">
213     <type name="BOOL" newname="uint8" />
214     <type name="BYTE" newname="uint8" />
215     <type name="CARD8" newname="uint8" />
216     <type name="CARD16" newname="uint16" />
217     <type name="CARD32" newname="uint32" />
218     <type name="INT8" newname="int8" />
219     <type name="INT16" newname="int16" />
220     <type name="INT32" newname="int32" />
221
222     <type name="char" newname="char" />
223     <type name="void" newname="void" />
224     <type name="float" newname="float" />
225     <type name="double" newname="double" />
226   </xsl:variable>
227   <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />
228
229   <!--
230     Output the canonical name for a type.  This will be
231     xcb_{extension-containing-type-if-any}_type, wherever the type is found in
232     the search path, or just type if not found.  If the type parameter is not
233     specified, it defaults to the value of the type attribute on the context
234     node.
235   -->
236   <xsl:template name="canonical-type-name">
237     <xsl:param name="type" select="string(@type)" />
238
239     <xsl:variable name="is-unqualified" select="not(contains($type, ':'))"/>
240     <xsl:variable name="namespace" select="substring-before($type, ':')" />
241     <xsl:variable name="unqualified-type">
242       <xsl:choose>
243         <xsl:when test="$is-unqualified">
244           <xsl:value-of select="$type" />
245         </xsl:when>
246         <xsl:otherwise>
247           <xsl:value-of select="substring-after($type, ':')" />
248         </xsl:otherwise>
249       </xsl:choose>
250     </xsl:variable>
251
252     <xsl:choose>
253       <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
254         <xsl:value-of select="$core-types/type[@name=$type]/@newname" />
255       </xsl:when>
256       <xsl:otherwise>
257         <xsl:variable name="type-definitions"
258                       select="(/xcb|document($search-path)/xcb
259                               )[$is-unqualified or @header=$namespace]
260                                /*[((self::struct or self::union or self::enum
261                                     or self::xidtype or self::xidunion
262                                     or self::event or self::eventcopy
263                                     or self::error or self::errorcopy)
264                                    and @name=$unqualified-type)
265                                   or (self::typedef
266                                       and @newname=$unqualified-type)]" />
267         <xsl:choose>
268           <xsl:when test="count($type-definitions) = 1">
269             <xsl:for-each select="$type-definitions">
270               <xsl:value-of select="xcb:xcb-prefix($unqualified-type)" />
271             </xsl:for-each>
272           </xsl:when>
273           <xsl:when test="count($type-definitions) > 1">
274             <xsl:message terminate="yes">
275               <xsl:text>Multiple definitions of type "</xsl:text>
276               <xsl:value-of select="$type" />
277               <xsl:text>" found.</xsl:text>
278               <xsl:if test="$is-unqualified">
279                 <xsl:for-each select="$type-definitions">
280                   <xsl:text>
281     </xsl:text>
282                   <xsl:value-of select="concat(/xcb/@header, ':', $type)" />
283                 </xsl:for-each>
284               </xsl:if>
285             </xsl:message>
286           </xsl:when>
287           <xsl:otherwise>
288             <xsl:message terminate="yes">
289               <xsl:text>No definitions of type "</xsl:text>
290               <xsl:value-of select="$type" />
291               <xsl:text>" found</xsl:text>
292               <xsl:if test="$is-unqualified">
293                 <xsl:text>, and it is not a known core type</xsl:text>
294               </xsl:if>
295               <xsl:text>.</xsl:text>
296             </xsl:message>
297           </xsl:otherwise>
298         </xsl:choose>   
299       </xsl:otherwise>
300     </xsl:choose>
301   </xsl:template>
302   
303   <!-- Helper template for requests, that outputs the cookie type.  The
304        parameter "request" must be the request node, which defaults to the
305        context node. -->
306   <xsl:template name="cookie-type">
307     <xsl:param name="request" select="." />
308     <xsl:choose>
309       <xsl:when test="$request/reply">
310         <xsl:value-of select="xcb:xcb-prefix($request/@name)" />
311       </xsl:when>
312       <xsl:otherwise>
313         <xsl:text>xcb_void</xsl:text>
314       </xsl:otherwise>
315     </xsl:choose>
316     <xsl:text>_cookie_t</xsl:text>
317   </xsl:template>
318
319   <xsl:template name="request-function">
320     <xsl:param name="checked" />
321     <xsl:param name="req" />
322     <function>
323       <xsl:attribute name="name">
324         <xsl:value-of select="xcb:xcb-prefix($req/@name)" />
325         <xsl:if test="$checked='true' and not($req/reply)">_checked</xsl:if>
326         <xsl:if test="$checked='false' and $req/reply">_unchecked</xsl:if>
327       </xsl:attribute>
328       <xsl:attribute name="type">
329         <xsl:call-template name="cookie-type">
330           <xsl:with-param name="request" select="$req" />
331         </xsl:call-template>
332       </xsl:attribute>
333       <doc>/**</doc>
334       <doc> * Delivers a request to the X server</doc>
335       <doc> * @param c The connection</doc>
336       <doc> * @return A cookie</doc>
337       <doc> *</doc>
338       <doc> * Delivers a request to the X server.</doc>
339       <doc> * </doc>
340       <xsl:if test="$checked='true' and not($req/reply)">
341         <doc> * This form can be used only if the request will not cause</doc>
342         <doc> * a reply to be generated. Any returned error will be</doc>
343         <doc> * saved for handling by xcb_request_check().</doc>
344       </xsl:if>
345       <xsl:if test="$checked='false' and $req/reply">
346         <doc> * This form can be used only if the request will cause</doc>
347         <doc> * a reply to be generated. Any returned error will be</doc>
348         <doc> * placed in the event queue.</doc>
349       </xsl:if>
350       <doc> */</doc>
351       <field type="xcb_connection_t *" name="c" />
352       <xsl:apply-templates select="$req/*[not(self::reply)]" mode="param" />
353       <do-request ref="{xcb:xcb-prefix($req/@name)}_request_t" opcode="{translate(xcb:xcb-prefix($req/@name), $lcase, $ucase)}"
354                   checked="{$checked}">
355         <xsl:if test="$req/reply">
356           <xsl:attribute name="has-reply">true</xsl:attribute>
357         </xsl:if>
358       </do-request>
359     </function>
360   </xsl:template>
361   
362   <xsl:template match="request" mode="pass1">
363     <xsl:variable name="req" select="." />
364     <xsl:if test="reply">
365       <struct name="{xcb:xcb-prefix(@name)}_cookie_t">
366         <field type="unsigned int" name="sequence" />
367       </struct>
368     </xsl:if>
369     <constant type="number" name="{xcb:xcb-prefix($req/@name)}" value="{$req/@opcode}" />
370     <struct name="{xcb:xcb-prefix(@name)}_request_t">
371       <field type="uint8_t" name="major_opcode" no-assign="true" />
372       <xsl:if test="$ext">
373         <field type="uint8_t" name="minor_opcode" no-assign="true" />
374       </xsl:if>
375       <xsl:apply-templates select="*[not(self::reply)]" mode="field" />
376       <middle>
377         <field type="uint16_t" name="length" no-assign="true" />
378       </middle>
379     </struct>
380     <xsl:call-template name="request-function">
381       <xsl:with-param name="checked" select="'true'" />
382       <xsl:with-param name="req" select="$req" />
383     </xsl:call-template>
384     <xsl:call-template name="request-function">
385       <xsl:with-param name="checked" select="'false'" />
386       <xsl:with-param name="req" select="$req" />
387     </xsl:call-template>
388     <xsl:if test="reply">
389       <struct name="{xcb:xcb-prefix(@name)}_reply_t">
390         <field type="uint8_t" name="response_type" />
391         <xsl:apply-templates select="reply/*" mode="field" />
392         <middle>
393           <field type="uint16_t" name="sequence" />
394           <field type="uint32_t" name="length" />
395         </middle>
396       </struct>
397       <iterator-functions ref="{xcb:xcb-prefix(@name)}" kind="_reply" />
398       <function type="{xcb:xcb-prefix(@name)}_reply_t *" name="{xcb:xcb-prefix(@name)}_reply">
399         <doc>/**</doc>
400         <doc> * Return the reply</doc>
401         <doc> * @param c      The connection</doc>
402         <doc> * @param cookie The cookie</doc>
403         <doc> * @param e      The xcb_generic_error_t supplied</doc>
404         <doc> *</doc>
405         <doc> * Returns the reply of the request asked by</doc>
406         <doc> * </doc>
407         <doc> * The parameter @p e supplied to this function must be NULL if</doc>
408         <doc> * <xsl:value-of select="xcb:xcb-prefix(@name)" />_unchecked(). is used.</doc>
409         <doc> * Otherwise, it stores the error if any.</doc>
410         <doc> */</doc>
411         <field type="xcb_connection_t *" name="c" />
412         <field name="cookie">
413           <xsl:attribute name="type">
414             <xsl:call-template name="cookie-type" />
415           </xsl:attribute>
416         </field>
417         <field type="xcb_generic_error_t **" name="e" />
418         <l>return (<xsl:value-of select="xcb:xcb-prefix(@name)" />_reply_t *)<!--
419         --> xcb_wait_for_reply(c, cookie.sequence, e);</l>
420       </function>
421     </xsl:if>
422   </xsl:template>
423
424   <xsl:template match="xidtype|xidunion" mode="pass1">
425     <typedef oldname="uint32_t" newname="{xcb:xcb-prefix(@name)}_t" />
426     <iterator ref="{xcb:xcb-prefix(@name)}" />
427     <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
428   </xsl:template>
429
430   <xsl:template match="struct|union" mode="pass1">
431     <struct name="{xcb:xcb-prefix(@name)}_t">
432       <xsl:if test="self::union">
433         <xsl:attribute name="kind">union</xsl:attribute>
434       </xsl:if>
435       <xsl:apply-templates select="*" mode="field" />
436     </struct>
437     <iterator ref="{xcb:xcb-prefix(@name)}" />
438     <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
439   </xsl:template>
440
441   <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
442     <xsl:variable name="suffix">
443       <xsl:choose>
444         <xsl:when test="self::event|self::eventcopy">
445           <xsl:text>_event_t</xsl:text>
446         </xsl:when>
447         <xsl:when test="self::error|self::errorcopy">
448           <xsl:text>_error_t</xsl:text>
449         </xsl:when>
450       </xsl:choose>
451     </xsl:variable>
452     <constant type="number" name="{xcb:xcb-prefix(@name)}" value="{@number}" />
453     <xsl:choose>
454       <xsl:when test="self::event|self::error">
455         <struct name="{xcb:xcb-prefix(@name)}{$suffix}">
456           <field type="uint8_t" name="response_type" />
457           <xsl:if test="self::error">
458             <field type="uint8_t" name="error_code" />
459           </xsl:if>
460           <xsl:apply-templates select="*" mode="field" />
461           <xsl:if test="not(self::event and boolean(@no-sequence-number))">
462             <middle>
463               <field type="uint16_t" name="sequence" />
464             </middle>
465           </xsl:if>
466         </struct>
467       </xsl:when>
468       <xsl:when test="self::eventcopy|self::errorcopy">
469         <typedef newname="{xcb:xcb-prefix(@name)}{$suffix}">
470           <xsl:attribute name="oldname">
471             <xsl:call-template name="canonical-type-name">
472               <xsl:with-param name="type" select="@ref" />
473             </xsl:call-template>
474             <xsl:value-of select="$suffix" />
475           </xsl:attribute>
476         </typedef>
477       </xsl:when>
478     </xsl:choose>
479   </xsl:template>
480
481   <xsl:template match="typedef" mode="pass1">
482     <typedef>
483       <xsl:attribute name="oldname">
484         <xsl:call-template name="canonical-type-name">
485           <xsl:with-param name="type" select="@oldname" />
486         </xsl:call-template>
487         <xsl:text>_t</xsl:text>
488       </xsl:attribute>
489       <xsl:attribute name="newname">
490         <xsl:call-template name="canonical-type-name">
491           <xsl:with-param name="type" select="@newname" />
492         </xsl:call-template>
493         <xsl:text>_t</xsl:text>
494       </xsl:attribute>
495     </typedef>
496     <iterator ref="{xcb:xcb-prefix(@newname)}" />
497     <iterator-functions ref="{xcb:xcb-prefix(@newname)}" />
498   </xsl:template>
499
500   <xsl:template match="enum" mode="pass1">
501     <enum name="{xcb:xcb-prefix(@name)}_t">
502       <xsl:for-each select="item">
503         <item name="{translate(xcb:xcb-prefix(concat(../@name, concat('_', @name))), $lcase, $ucase)}">
504           <xsl:copy-of select="*" />
505         </item>
506       </xsl:for-each>
507     </enum>
508   </xsl:template>
509
510   <!--
511     Templates for processing fields.
512   -->
513
514   <xsl:template match="pad" mode="field">
515     <xsl:copy-of select="." />
516   </xsl:template>
517   
518   <xsl:template match="field|exprfield" mode="field">
519     <xsl:copy>
520       <xsl:attribute name="type">
521         <xsl:call-template name="canonical-type-name" />
522         <xsl:text>_t</xsl:text>
523       </xsl:attribute>
524       <xsl:attribute name="name">
525         <xsl:call-template name="canonical-var-name" />
526       </xsl:attribute>
527       <xsl:copy-of select="*" />
528     </xsl:copy>
529   </xsl:template>
530
531   <xsl:template match="list" mode="field">
532     <xsl:variable name="type"><!--
533       --><xsl:call-template name="canonical-type-name" />
534         <xsl:text>_t</xsl:text><!--
535     --></xsl:variable>
536     <list type="{$type}">
537       <xsl:attribute name="name">
538         <xsl:call-template name="canonical-var-name" />
539       </xsl:attribute>
540       <xsl:if test="not(parent::request) and node()
541                     and not(.//*[not(self::value or self::op)])">
542         <xsl:attribute name="fixed">true</xsl:attribute>
543       </xsl:if>
544       <!-- Handle lists with no length expressions. -->
545       <xsl:if test="not(node())">
546         <xsl:choose>
547           <!-- In a request, refer to an implicit localparam for length. -->
548           <xsl:when test="parent::request">
549             <fieldref>
550               <xsl:value-of select="concat(@name, '_len')" />
551             </fieldref>
552           </xsl:when>
553           <!-- In a reply, use the length of the reply to determine the length
554                of the list. -->
555           <xsl:when test="parent::reply">
556             <op op="/">
557               <op op="&lt;&lt;">
558                 <fieldref>length</fieldref>
559                 <value>2</value>
560               </op>
561               <function-call name="sizeof">
562                 <param><xsl:value-of select="$type" /></param>
563               </function-call>
564             </op>
565           </xsl:when>
566           <!-- Other cases generate an error. -->
567           <xsl:otherwise>
568             <xsl:message terminate="yes"><!--
569               -->Encountered a list with no length expresssion outside a<!--
570               --> request or reply.<!--
571             --></xsl:message>
572           </xsl:otherwise>
573         </xsl:choose>
574       </xsl:if>
575       <xsl:copy-of select="*" />
576     </list>
577   </xsl:template>
578
579   <xsl:template match="valueparam" mode="field">
580     <field>
581       <xsl:attribute name="type">
582         <xsl:call-template name="canonical-type-name">
583           <xsl:with-param name="type" select="@value-mask-type" />
584         </xsl:call-template>
585         <xsl:text>_t</xsl:text>
586       </xsl:attribute>
587       <xsl:attribute name="name">
588         <xsl:call-template name="canonical-var-name">
589           <xsl:with-param name="name" select="@value-mask-name" />
590         </xsl:call-template>
591       </xsl:attribute>
592     </field>
593     <list type="uint32_t">
594       <xsl:attribute name="name">
595         <xsl:call-template name="canonical-var-name">
596           <xsl:with-param name="name" select="@value-list-name" />
597         </xsl:call-template>
598       </xsl:attribute>
599       <function-call name="xcb_popcount">
600         <param>
601           <fieldref>
602             <xsl:call-template name="canonical-var-name">
603               <xsl:with-param name="name" select="@value-mask-name" />
604             </xsl:call-template>
605           </fieldref>
606         </param>
607       </function-call>
608     </list>
609   </xsl:template>
610
611   <xsl:template match="field" mode="param">
612     <field>
613       <xsl:attribute name="type">
614         <xsl:call-template name="canonical-type-name" />
615         <xsl:text>_t</xsl:text>
616       </xsl:attribute>
617       <xsl:attribute name="name">
618         <xsl:call-template name="canonical-var-name" />
619       </xsl:attribute>
620     </field>
621   </xsl:template>
622
623   <xsl:template match="list" mode="param">
624     <!-- If no length expression is provided, use a CARD32 localfield. -->
625     <xsl:if test="not(node())">
626       <field type="uint32_t" name="{@name}_len" />
627     </xsl:if>
628     <field>
629       <xsl:variable name="ctype">
630         <xsl:call-template name="canonical-type-name" />
631       </xsl:variable>
632       <xsl:attribute name="type">
633         <xsl:text>const </xsl:text>
634         <xsl:call-template name="canonical-type-name" />
635         <xsl:if test="not($ctype='char') and not($ctype='void')">
636           <xsl:text>_t</xsl:text>
637         </xsl:if>
638         <xsl:text> *</xsl:text>
639       </xsl:attribute>
640       <xsl:attribute name="name">
641         <xsl:call-template name="canonical-var-name" />
642       </xsl:attribute>
643     </field>
644   </xsl:template>
645
646   <xsl:template match="valueparam" mode="param">
647     <field>
648       <xsl:attribute name="type">
649         <xsl:call-template name="canonical-type-name">
650           <xsl:with-param name="type" select="@value-mask-type" />
651         </xsl:call-template>
652         <xsl:text>_t</xsl:text>
653       </xsl:attribute>
654       <xsl:attribute name="name">
655         <xsl:call-template name="canonical-var-name">
656           <xsl:with-param name="name" select="@value-mask-name" />
657         </xsl:call-template>
658       </xsl:attribute>
659     </field>
660     <field type="const uint32_t *">
661       <xsl:attribute name="name">
662         <xsl:call-template name="canonical-var-name">
663           <xsl:with-param name="name" select="@value-list-name" />
664         </xsl:call-template>
665       </xsl:attribute>
666     </field>
667   </xsl:template>
668
669   <!-- Second pass: Process the variable. -->
670   <xsl:variable name="result-rtf">
671     <xsl:apply-templates select="$pass1/*" mode="pass2" />
672   </xsl:variable>
673   <xsl:variable name="result" select="e:node-set($result-rtf)" />
674
675   <xsl:template match="xcb" mode="pass2">
676     <xcb>
677       <xsl:copy-of select="@*" />
678       <xsl:apply-templates mode="pass2"
679                            select="constant|enum|struct|typedef|iterator" />
680       <xsl:apply-templates mode="pass2"
681                            select="function|iterator-functions" />
682     </xcb>
683   </xsl:template>
684
685   <!-- Generic rules for nodes that don't need further processing: copy node
686        with attributes, and recursively process the child nodes. -->
687   <xsl:template match="*" mode="pass2">
688     <xsl:copy>
689       <xsl:copy-of select="@*" />
690       <xsl:apply-templates mode="pass2" />
691     </xsl:copy>
692   </xsl:template>
693
694   <xsl:template match="struct" mode="pass2">
695     <xsl:if test="@kind='union' and list[not(@fixed)]">
696       <xsl:message terminate="yes">Unions must be fixed length.</xsl:message>
697     </xsl:if>
698     <struct name="{@name}">
699       <xsl:if test="@kind">
700         <xsl:attribute name="kind">
701           <xsl:value-of select="@kind" />
702         </xsl:attribute>
703       </xsl:if>
704       <!-- FIXME: This should go by size, not number of fields. -->
705       <xsl:copy-of select="node()[not(self::middle)
706                    and position() &lt; 3]" />
707       <xsl:if test="middle and (count(*[not(self::middle)]) &lt; 2)">
708         <pad bytes="{2 - count(*[not(self::middle)])}" />
709       </xsl:if>
710       <xsl:copy-of select="middle/*" />
711       <xsl:copy-of select="node()[not(self::middle) and (position() > 2)]" />
712     </struct>
713   </xsl:template>
714
715   <xsl:template match="do-request" mode="pass2">
716     <xsl:variable name="struct"
717                   select="$pass1/xcb/struct[@name=current()/@ref]" />
718
719     <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
720
721     <l>static const xcb_protocol_request_t xcb_req = {</l>
722     <indent>
723       <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
724       <l>/* ext */ <xsl:choose>
725                      <xsl:when test="$ext">
726                        <xsl:text>&amp;</xsl:text>
727                        <xsl:value-of select="xcb:xcb-prefix()" />
728                        <xsl:text>_id</xsl:text>
729                      </xsl:when>
730                      <xsl:otherwise>0</xsl:otherwise>
731                    </xsl:choose>,</l>
732       <l>/* opcode */ <xsl:value-of select="@opcode" />,</l>
733       <l>/* isvoid */ <xsl:value-of select="1-boolean(@has-reply)" /></l>
734     </indent>
735     <l>};</l>
736
737     <l />
738     <l>struct iovec xcb_parts[<xsl:value-of select="$num-parts+2" />];</l>
739     <l><xsl:value-of select="../@type" /> xcb_ret;</l>
740     <l><xsl:value-of select="@ref" /> xcb_out;</l>
741
742     <l />
743     <xsl:if test="not ($ext) and not($struct//*[(self::field or self::exprfield or self::pad)
744                                                 and not(boolean(@no-assign))])">
745       <l>xcb_out.pad0 = 0;</l>
746     </xsl:if>
747     <xsl:apply-templates select="$struct//*[(self::field or self::exprfield or self::pad)
748                                             and not(boolean(@no-assign))]"
749                          mode="assign" />
750
751     <l />
752     <l>xcb_parts[2].iov_base = (char *) &amp;xcb_out;</l>
753     <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
754     <l>xcb_parts[3].iov_base = 0;</l>
755     <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len &amp; 3;</l>
756
757     <xsl:for-each select="$struct/list">
758       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
759       --><xsl:value-of select="@name" />;</l>
760       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
761       --><xsl:apply-templates mode="output-expression" />
762       <xsl:if test="not(@type = 'void_t')">
763         <xsl:text> * sizeof(</xsl:text>
764           <xsl:choose>
765           <xsl:when test="@type='char_t'">
766             <xsl:text>char</xsl:text>
767           </xsl:when>
768           <xsl:otherwise>
769             <xsl:value-of select="@type" />
770           </xsl:otherwise>
771         </xsl:choose>
772         <xsl:text>)</xsl:text>
773       </xsl:if>;</l>
774       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
775       <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>
776     </xsl:for-each>
777
778     <l>xcb_ret.sequence = xcb_send_request(c, <!--
779     --><xsl:choose>
780          <xsl:when test="@checked='true'">XCB_REQUEST_CHECKED</xsl:when>
781          <xsl:otherwise>0</xsl:otherwise>
782        </xsl:choose>, xcb_parts + 2, &amp;xcb_req);</l>
783     <l>return xcb_ret;</l>
784   </xsl:template>
785
786   <xsl:template match="field" mode="assign">
787     <l>
788       <xsl:text>xcb_out.</xsl:text>
789       <xsl:value-of select="@name" />
790       <xsl:text> = </xsl:text>
791       <xsl:value-of select="@name" />
792       <xsl:text>;</xsl:text>
793     </l>
794   </xsl:template>
795
796   <xsl:template match="exprfield" mode="assign">
797     <l>
798       <xsl:text>xcb_out.</xsl:text>
799       <xsl:value-of select="@name" />
800       <xsl:text> = </xsl:text>
801       <xsl:apply-templates mode="output-expression" />
802       <xsl:text>;</xsl:text>
803     </l>
804   </xsl:template>
805
806   <xsl:template match="pad" mode="assign">
807     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
808     <l><xsl:choose>
809         <xsl:when test="@bytes = 1">xcb_out.pad<xsl:value-of select="$padnum - 1" /> = 0;</xsl:when>
810         <xsl:otherwise>memset(xcb_out.pad<xsl:value-of select="$padnum - 1" />, 0, <xsl:value-of select="@bytes" />);</xsl:otherwise>
811     </xsl:choose></l>
812   </xsl:template>
813
814   <xsl:template match="iterator" mode="pass2">
815     <struct name="{@ref}_iterator_t">
816       <field type="{@ref}_t *" name="data" />
817       <field type="int" name="rem" />
818       <field type="int" name="index" />
819     </struct>
820   </xsl:template>
821
822   <xsl:template match="iterator-functions" mode="pass2">
823     <xsl:variable name="ref" select="@ref" />
824     <xsl:variable name="kind" select="@kind" />
825     <xsl:variable name="struct"
826                   select="$pass1/xcb/struct[@name=concat($ref, $kind, '_t')]" />
827     <xsl:variable name="nextfields-rtf">
828       <nextfield>R + 1</nextfield>
829       <xsl:for-each select="$struct/list[not(@fixed)]">
830         <xsl:choose>
831           <xsl:when test="substring(@type, 1, 3) = 'xcb'">
832             <nextfield><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_end(<!--
833             --><xsl:value-of select="$ref" />_<!--
834             --><xsl:value-of select="string(@name)" />_iterator(R))</nextfield>
835           </xsl:when>
836           <xsl:otherwise>
837             <nextfield><xsl:value-of select="$ref" />_<!--
838             --><xsl:value-of select="string(@name)" />_end(R)</nextfield>
839           </xsl:otherwise>
840         </xsl:choose>
841       </xsl:for-each>
842     </xsl:variable>
843     <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
844     <xsl:for-each select="$struct/list[not(@fixed)]">
845       <xsl:variable name="number"
846                     select="1+count(preceding-sibling::list[not(@fixed)])" />
847       <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
848       <xsl:variable name="is-first"
849                     select="not(preceding-sibling::list[not(@fixed)])" />
850       <xsl:variable name="field-name" select="@name" />
851       <xsl:variable name="is-variable"
852                     select="$pass1/xcb/struct[@name=current()/@type]/list
853                             or document($search-path)/xcb
854                                /struct[concat(xcb:xcb-prefix(@name), '_t')
855                                        = current()/@type]
856                                /*[self::valueparam
857                                   or self::list[.//*[not(self::value
858                                                          or self::op)]]]" />
859       <xsl:if test="not($is-variable)">
860         <function type="{xcb:get-char-void(@type)} *" name="{$ref}_{xcb:lowercase($field-name)}">
861           <field type="const {$ref}{$kind}_t *" name="R" />
862           <xsl:choose>
863             <xsl:when test="$is-first">
864               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
865               -->(<xsl:value-of select="$nextfield" />);</l>
866             </xsl:when>
867             <xsl:otherwise>
868               <l>xcb_generic_iterator_t prev = <!--
869               --><xsl:value-of select="$nextfield" />;</l>
870               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
871               -->((char *) prev.data + XCB_TYPE_PAD(<!--
872               --><xsl:value-of select="xcb:get-char-void(@type)" />, prev.index));</l>
873             </xsl:otherwise>
874           </xsl:choose>
875         </function>
876       </xsl:if>
877       <function type="int" name="{$ref}_{xcb:lowercase($field-name)}_length">
878         <field type="const {$ref}{$kind}_t *" name="R" />
879         <l>return <xsl:apply-templates mode="output-expression">
880                     <xsl:with-param name="field-prefix" select="'R->'" />
881                   </xsl:apply-templates>;</l>
882       </function>
883       <xsl:choose>
884         <xsl:when test="substring(@type, 1, 3) = 'xcb'">
885           <function type="{substring(@type, 1, string-length(@type)-2)}_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_iterator">
886             <field type="const {$ref}{$kind}_t *" name="R" />
887             <l><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_iterator_t i;</l>
888             <xsl:choose>
889               <xsl:when test="$is-first">
890                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
891                 -->(<xsl:value-of select="$nextfield" />);</l>
892               </xsl:when>
893               <xsl:otherwise>
894                 <l>xcb_generic_iterator_t prev = <!--
895                 --><xsl:value-of select="$nextfield" />;</l>
896                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
897                 -->((char *) prev.data + XCB_TYPE_PAD(<!--
898                 --><xsl:value-of select="@type" />, prev.index));</l>
899               </xsl:otherwise>
900             </xsl:choose>
901             <l>i.rem = <xsl:apply-templates mode="output-expression">
902                          <xsl:with-param name="field-prefix" select="'R->'" />
903                        </xsl:apply-templates>;</l>
904             <l>i.index = (char *) i.data - (char *) R;</l>
905             <l>return i;</l>
906           </function>
907         </xsl:when>
908         <xsl:otherwise>
909           <xsl:variable name="cast">
910             <xsl:choose>
911               <xsl:when test="@type='void'">char</xsl:when>
912               <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
913             </xsl:choose>
914           </xsl:variable>
915           <function type="xcb_generic_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_end">
916             <field type="const {$ref}{$kind}_t *" name="R" />
917             <l>xcb_generic_iterator_t i;</l>
918             <xsl:choose>
919               <xsl:when test="$is-first">
920                 <l>i.data = ((<xsl:value-of select="xcb:remove-void($cast)" /> *) <!--
921                 -->(<xsl:value-of select="$nextfield" />)) + (<!--
922                 --><xsl:apply-templates mode="output-expression">
923                      <xsl:with-param name="field-prefix" select="'R->'" />
924                    </xsl:apply-templates>);</l>
925               </xsl:when>
926               <xsl:otherwise>
927                 <l>xcb_generic_iterator_t child = <!--
928                 --><xsl:value-of select="$nextfield" />;</l>
929                 <l>i.data = ((<xsl:value-of select="xcb:get-char-void($cast)" /> *) <!--
930                 -->child.data) + (<!--
931                 --><xsl:apply-templates mode="output-expression">
932                      <xsl:with-param name="field-prefix" select="'R->'" />
933                    </xsl:apply-templates>);</l>
934               </xsl:otherwise>
935             </xsl:choose>
936             <l>i.rem = 0;</l>
937             <l>i.index = (char *) i.data - (char *) R;</l>
938             <l>return i;</l>
939           </function>
940         </xsl:otherwise>
941       </xsl:choose>
942     </xsl:for-each>
943     <xsl:if test="not($kind)">
944       <function type="void" name="{$ref}_next">
945         <doc>/**</doc>
946         <doc> * Get the next element of the iterator</doc>
947         <doc> * @param i Pointer to a <xsl:value-of select="$ref" />_iterator_t</doc>
948         <doc> *</doc>
949         <doc> * Get the next element in the iterator. The member rem is</doc>
950         <doc> * decreased by one. The member data points to the next</doc>
951         <doc> * element. The member index is increased by sizeof(<xsl:value-of select="$ref" />_t)</doc>
952         <doc> */</doc>
953         <field type="{$ref}_iterator_t *" name="i" />
954         <xsl:choose>
955           <xsl:when test="$struct/list[not(@fixed)]">
956             <l><xsl:value-of select="$ref" />_t *R = i->data;</l>
957             <l>xcb_generic_iterator_t child = <!--
958             --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
959             <l>--i->rem;</l>
960             <l>i->data = (<xsl:value-of select="$ref" />_t *) child.data;</l>
961             <l>i->index = child.index;</l>
962           </xsl:when>
963           <xsl:otherwise>
964             <l>--i->rem;</l>
965             <l>++i->data;</l>
966             <l>i->index += sizeof(<xsl:value-of select="$ref" />_t);</l>
967           </xsl:otherwise>
968         </xsl:choose>
969       </function>
970       <function type="xcb_generic_iterator_t" name="{$ref}_end">
971         <doc>/**</doc>
972         <doc> * Return the iterator pointing to the last element</doc>
973         <doc> * @param i An <xsl:value-of select="$ref" />_iterator_t</doc>
974         <doc> * @return  The iterator pointing to the last element</doc>
975         <doc> *</doc>
976         <doc> * Set the current element in the iterator to the last element.</doc>
977         <doc> * The member rem is set to 0. The member data points to the</doc>
978         <doc> * last element.</doc>
979         <doc> */</doc>
980         <field type="{$ref}_iterator_t" name="i" />
981         <l>xcb_generic_iterator_t ret;</l>
982         <xsl:choose>
983           <xsl:when test="$struct/list[not(@fixed)]">
984             <l>while(i.rem > 0)</l>
985             <indent>
986               <l><xsl:value-of select="$ref" />_next(&amp;i);</l>
987             </indent>
988             <l>ret.data = i.data;</l>
989             <l>ret.rem = i.rem;</l>
990             <l>ret.index = i.index;</l>
991           </xsl:when>
992           <xsl:otherwise>
993             <l>ret.data = i.data + i.rem;</l>
994             <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
995             <l>ret.rem = 0;</l>
996           </xsl:otherwise>
997         </xsl:choose>
998         <l>return ret;</l>
999       </function>
1000     </xsl:if>
1001   </xsl:template>
1002
1003   <!-- Output the results. -->
1004   <xsl:template match="/">
1005     <xsl:if test="not(function-available('e:node-set'))">
1006       <xsl:message terminate="yes"><!--
1007         -->Error: This stylesheet requires the EXSL node-set extension.<!--
1008       --></xsl:message>
1009     </xsl:if>
1010
1011     <xsl:if test="not($h) and not($c)">
1012       <xsl:message terminate="yes"><!--
1013         -->Error: Parameter "mode" must be "header" or "source".<!--
1014       --></xsl:message>
1015     </xsl:if>
1016
1017     <xsl:apply-templates select="$result/*" mode="output" />
1018   </xsl:template>
1019
1020   <xsl:template match="xcb" mode="output">
1021     <xsl:variable name="guard"><!--
1022       -->__<xsl:value-of select="$ucase-header" />_H<!--
1023     --></xsl:variable>
1024
1025 <xsl:text>/*
1026  * This file generated automatically from </xsl:text>
1027 <xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
1028  * Edit at your peril.
1029  */
1030 </xsl:text>
1031 <xsl:if test="$h"><xsl:text>
1032 /**
1033  * @defgroup XCB_</xsl:text><xsl:value-of select="$ext" /><xsl:text>_API XCB </xsl:text><xsl:value-of select="$ext" /><xsl:text> API
1034  * @brief </xsl:text><xsl:value-of select="$ext" /><xsl:text> XCB Protocol Implementation.</xsl:text>
1035 <xsl:text>
1036  * @{
1037  **/
1038 </xsl:text>
1039
1040 <xsl:text>
1041 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
1042 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
1043 </xsl:text>
1044 #include "xcb.h"
1045 <xsl:for-each select="$root/xcb/import">
1046 <xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
1047 </xsl:text>
1048 </xsl:for-each>
1049 <xsl:text>
1050 </xsl:text>
1051 </xsl:if>
1052 <xsl:if test="$h">
1053     <xsl:choose>
1054         <xsl:when test="string($ext)">
1055   <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>
1056 </xsl:text>
1057   <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" />
1058   <xsl:text>
1059   
1060 </xsl:text>
1061     </xsl:when>
1062   </xsl:choose>
1063 </xsl:if>
1064
1065 <xsl:if test="$c">
1066 <xsl:if test="$need-string-h">
1067 #include &lt;string.h&gt;</xsl:if>
1068 <xsl:text>
1069 #include &lt;assert.h&gt;
1070 #include "xcbext.h"
1071 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
1072
1073 </xsl:text></xsl:if>
1074
1075     <xsl:apply-templates mode="output" />
1076
1077 <xsl:if test="$h">
1078 <xsl:text>
1079 #endif
1080
1081 /**
1082  * @}
1083  */
1084 </xsl:text>
1085 </xsl:if>
1086   </xsl:template>
1087
1088   <xsl:template match="constant" mode="output">
1089     <xsl:choose>
1090       <xsl:when test="@type = 'number'">
1091         <xsl:if test="$h">
1092           <xsl:text>/** Opcode for </xsl:text><xsl:value-of select="@name"/><xsl:text>. */
1093 </xsl:text>
1094           <xsl:text>#define </xsl:text>
1095           <xsl:value-of select="translate(@name, $lcase, $ucase)" />
1096           <xsl:text> </xsl:text>
1097           <xsl:value-of select="@value" />
1098           <xsl:text>
1099
1100 </xsl:text>
1101         </xsl:if>
1102       </xsl:when>
1103       <xsl:when test="@type = 'string'">
1104         <xsl:if test="$h">
1105           <xsl:text>extern </xsl:text>
1106         </xsl:if>
1107         <xsl:text>const char </xsl:text>
1108         <xsl:value-of select="@name" />
1109         <xsl:text>[]</xsl:text>
1110         <xsl:if test="$c">
1111           <xsl:text> = "</xsl:text>
1112           <xsl:value-of select="@value" />
1113           <xsl:text>"</xsl:text>
1114         </xsl:if>
1115         <xsl:text>;
1116
1117 </xsl:text>
1118       </xsl:when>
1119       <xsl:otherwise>
1120         <xsl:if test="$h">
1121           <xsl:text>extern </xsl:text>
1122         </xsl:if>
1123         <xsl:call-template name="type-and-name" />
1124         <xsl:if test="$c">
1125           <xsl:text> = </xsl:text>
1126           <xsl:value-of select="@value" />
1127         </xsl:if>
1128         <xsl:text>;
1129
1130 </xsl:text>
1131       </xsl:otherwise>
1132     </xsl:choose>
1133   </xsl:template>
1134
1135   <xsl:template match="typedef" mode="output">
1136     <xsl:if test="$h">
1137       <xsl:text>typedef </xsl:text>
1138       <xsl:value-of select="xcb:get-char-void(@oldname)" />
1139       <xsl:text> </xsl:text>
1140       <xsl:value-of select="@newname" />
1141       <xsl:text>;
1142
1143 </xsl:text>
1144     </xsl:if>
1145   </xsl:template>
1146
1147   <xsl:template match="struct" mode="output">
1148     <xsl:if test="$h">
1149       <xsl:variable name="type-lengths">
1150         <xsl:call-template name="type-lengths">
1151           <xsl:with-param name="items" select="field/@type" />
1152         </xsl:call-template>
1153       </xsl:variable>
1154       <xsl:text>/**
1155  * @brief </xsl:text><xsl:value-of select="@name" /><xsl:text>
1156  **/
1157 </xsl:text>
1158       <xsl:text>typedef </xsl:text>
1159       <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
1160       <xsl:text> </xsl:text>
1161       <xsl:value-of select="@name" />
1162       <xsl:text> {
1163 </xsl:text>
1164       <xsl:for-each select="exprfield|field|list[@fixed]|pad">
1165         <xsl:text>    </xsl:text>
1166         <xsl:apply-templates select=".">
1167           <xsl:with-param name="type-lengths" select="$type-lengths" />
1168         </xsl:apply-templates>
1169         <xsl:text>; /**&lt; </xsl:text><xsl:text> */
1170 </xsl:text>
1171       </xsl:for-each>
1172       <xsl:text>} </xsl:text>
1173       <xsl:value-of select="@name" />
1174       <xsl:text>;
1175
1176 </xsl:text>
1177     </xsl:if>
1178   </xsl:template>
1179
1180   <xsl:template match="enum" mode="output">
1181     <xsl:if test="$h">
1182       <xsl:text>typedef enum </xsl:text>
1183       <xsl:value-of select="@name" />
1184       <xsl:text> {
1185     </xsl:text>
1186       <xsl:call-template name="list">
1187         <xsl:with-param name="separator"><xsl:text>,
1188     </xsl:text></xsl:with-param>
1189         <xsl:with-param name="items">
1190           <xsl:for-each select="item">
1191             <item>
1192               <xsl:value-of select="@name" />
1193               <xsl:if test="node()"> <!-- If there is an expression -->
1194                 <xsl:text> = </xsl:text>
1195                 <xsl:apply-templates mode="output-expression" />
1196               </xsl:if>
1197             </item>
1198           </xsl:for-each>
1199         </xsl:with-param>
1200       </xsl:call-template>
1201       <xsl:text>
1202 } </xsl:text><xsl:value-of select="@name" /><xsl:text>;
1203
1204 </xsl:text>
1205     </xsl:if>
1206   </xsl:template>
1207
1208   <xsl:template match="function" mode="output">
1209     <xsl:variable name="decl-open" select="concat(@name, ' (')" />
1210     <xsl:variable name="type-lengths">
1211       <xsl:call-template name="type-lengths">
1212         <xsl:with-param name="items" select="field/@type" />
1213       </xsl:call-template>
1214   </xsl:variable>
1215   <!-- Doxygen for functions in header. -->
1216     <xsl:if test="$h">
1217       <xsl:apply-templates select="doc" mode="function-doc">
1218       </xsl:apply-templates>
1219     </xsl:if>
1220 /*****************************************************************************
1221  **
1222  ** <xsl:value-of select="@type" />
1223  <xsl:text> </xsl:text>
1224  <xsl:value-of select="@name" />
1225  ** <xsl:call-template name="list">
1226      <xsl:with-param name="items">
1227          <xsl:for-each select="field">
1228              <item>
1229                  <xsl:text>
1230  ** @param </xsl:text>
1231                  <xsl:apply-templates select=".">
1232                      <xsl:with-param name="type-lengths" select="$type-lengths" />
1233                  </xsl:apply-templates>
1234              </item>
1235          </xsl:for-each>
1236      </xsl:with-param>
1237  </xsl:call-template>
1238  ** @returns <xsl:value-of select="@type" />
1239  **
1240  *****************************************************************************/
1241  
1242 <xsl:value-of select="@type" />
1243     <xsl:text>
1244 </xsl:text>
1245     <xsl:value-of select="$decl-open" />
1246     <xsl:call-template name="list">
1247       <xsl:with-param name="separator">
1248         <xsl:text>,
1249 </xsl:text>
1250         <xsl:call-template name="repeat">
1251           <xsl:with-param name="count" select="string-length($decl-open)" />
1252         </xsl:call-template>
1253       </xsl:with-param>
1254       <xsl:with-param name="items">
1255         <xsl:for-each select="field">
1256           <item>
1257             <xsl:apply-templates select=".">
1258               <xsl:with-param name="type-lengths" select="$type-lengths" />
1259             </xsl:apply-templates>
1260             <xsl:text>  /**&lt; */</xsl:text>
1261           </item>
1262         </xsl:for-each>
1263       </xsl:with-param>
1264     </xsl:call-template>
1265     <xsl:text>)</xsl:text>
1266
1267     <xsl:if test="$h"><xsl:text>;
1268
1269 </xsl:text></xsl:if>
1270
1271     <xsl:if test="$c">
1272       <xsl:text>
1273 {
1274 </xsl:text>
1275       <xsl:apply-templates select="l|indent" mode="function-body">
1276         <xsl:with-param name="indent" select="$indent-string" />
1277       </xsl:apply-templates>
1278       <xsl:text>}
1279
1280 </xsl:text>
1281     </xsl:if>
1282   </xsl:template>
1283
1284   <xsl:template match="doc" mode="function-doc">
1285     <xsl:value-of select="." /><xsl:text>
1286 </xsl:text>
1287   </xsl:template>
1288
1289   <xsl:template match="l" mode="function-body">
1290     <xsl:param name="indent" />
1291     <xsl:value-of select="concat($indent, .)" /><xsl:text>
1292 </xsl:text>
1293   </xsl:template>
1294
1295   <xsl:template match="indent" mode="function-body">
1296     <xsl:param name="indent" />
1297     <xsl:apply-templates select="l|indent" mode="function-body">
1298       <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
1299     </xsl:apply-templates>
1300   </xsl:template>
1301
1302   <xsl:template match="value" mode="output-expression">
1303     <xsl:value-of select="." />
1304   </xsl:template>
1305
1306   <xsl:template match="fieldref" mode="output-expression">
1307     <xsl:param name="field-prefix" />
1308     <xsl:value-of select="concat($field-prefix, .)" />
1309   </xsl:template>
1310
1311   <xsl:template match="op" mode="output-expression">
1312     <xsl:param name="field-prefix" />
1313     <xsl:text>(</xsl:text>
1314     <xsl:apply-templates select="node()[1]" mode="output-expression">
1315       <xsl:with-param name="field-prefix" select="$field-prefix" />
1316     </xsl:apply-templates>
1317     <xsl:text> </xsl:text>
1318     <xsl:value-of select="@op" />
1319     <xsl:text> </xsl:text>
1320     <xsl:apply-templates select="node()[2]" mode="output-expression">
1321       <xsl:with-param name="field-prefix" select="$field-prefix" />
1322     </xsl:apply-templates>
1323     <xsl:text>)</xsl:text>
1324   </xsl:template>
1325
1326   <xsl:template match="bit" mode="output-expression">
1327     <xsl:text>(1 &lt;&lt; </xsl:text>
1328     <xsl:value-of select="." />
1329     <xsl:text>)</xsl:text>
1330   </xsl:template>
1331
1332   <xsl:template match="function-call" mode="output-expression">
1333     <xsl:param name="field-prefix" />
1334     <xsl:value-of select="@name" />
1335     <xsl:text>(</xsl:text>
1336     <xsl:call-template name="list">
1337       <xsl:with-param name="separator" select="', '" />
1338       <xsl:with-param name="items">
1339         <xsl:for-each select="param">
1340           <item><xsl:apply-templates mode="output-expression">
1341             <xsl:with-param name="field-prefix" select="$field-prefix" />
1342           </xsl:apply-templates></item>
1343         </xsl:for-each>
1344       </xsl:with-param>
1345     </xsl:call-template>
1346     <xsl:text>)</xsl:text>
1347   </xsl:template>
1348
1349   <!-- Catch invalid elements in expressions. -->
1350   <xsl:template match="*" mode="output-expression">
1351     <xsl:message terminate="yes">
1352       <xsl:text>Invalid element in expression: </xsl:text>
1353       <xsl:value-of select="name()" />
1354     </xsl:message>
1355   </xsl:template>
1356
1357   <xsl:template match="field|exprfield">
1358     <xsl:param name="type-lengths" select="0" />
1359     <xsl:call-template name="type-and-name">
1360       <xsl:with-param name="type-lengths" select="$type-lengths" />
1361     </xsl:call-template>
1362   </xsl:template>
1363
1364   <xsl:template match="list[@fixed]">
1365     <xsl:param name="type-lengths" select="0" />
1366     <xsl:call-template name="type-and-name">
1367       <xsl:with-param name="type-lengths" select="$type-lengths" />
1368     </xsl:call-template>
1369     <xsl:text>[</xsl:text>
1370     <xsl:apply-templates mode="output-expression" />
1371     <xsl:text>]</xsl:text>
1372   </xsl:template>
1373
1374   <xsl:template match="pad">
1375     <xsl:param name="type-lengths" select="0" />
1376
1377     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
1378
1379     <xsl:call-template name="type-and-name">
1380       <xsl:with-param name="type" select="'uint8_t'" />
1381       <xsl:with-param name="name">
1382         <xsl:text>pad</xsl:text>
1383         <xsl:value-of select="$padnum - 1" />
1384       </xsl:with-param>
1385       <xsl:with-param name="type-lengths" select="$type-lengths" />
1386     </xsl:call-template>
1387     <xsl:if test="@bytes > 1">
1388       <xsl:text>[</xsl:text>
1389       <xsl:value-of select="@bytes" />
1390       <xsl:text>]</xsl:text>
1391     </xsl:if>
1392   </xsl:template>
1393
1394   <!-- Output the given type and name (defaulting to the corresponding
1395        attributes of the context node), with the appropriate spacing.  The
1396        type must consist of a base type (which may contain spaces), then
1397        optionally a single space and a suffix of one or more '*' characters.
1398        If the type-lengths parameter is provided, use it to line up the base
1399        types and suffixs of the type declarations. -->
1400   <xsl:template name="type-and-name">
1401     <xsl:param name="type" select="@type" />
1402     <xsl:param name="name" select="@name" />
1403     <xsl:param name="type-lengths">
1404       <max-type-length>0</max-type-length>
1405       <max-suffix-length>0</max-suffix-length>
1406     </xsl:param>
1407     
1408     <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
1409     <xsl:variable name="min-type-length"
1410                   select="$type-lengths-ns/max-type-length" />
1411     <xsl:variable name="min-suffix-length"
1412                   select="$type-lengths-ns/max-suffix-length" />
1413
1414     <xsl:variable name="base-type">
1415       <xsl:choose>
1416         <xsl:when test="contains($type, ' *')">
1417           <xsl:value-of select="substring-before($type, ' *')" />
1418         </xsl:when>
1419         <xsl:otherwise>
1420           <xsl:value-of select="$type" />
1421         </xsl:otherwise>
1422       </xsl:choose>
1423     </xsl:variable>
1424     <xsl:variable name="suffix">
1425       <xsl:if test="contains($type, ' *')">
1426         <xsl:text>*</xsl:text>
1427         <xsl:value-of select="substring-after($type, ' *')" />
1428       </xsl:if>
1429     </xsl:variable>
1430
1431     <xsl:value-of select="$base-type" />
1432     <xsl:if test="string-length($base-type) &lt; $min-type-length">
1433       <xsl:call-template name="repeat">
1434         <xsl:with-param name="count" select="$min-type-length
1435                                              - string-length($base-type)" />
1436       </xsl:call-template>
1437     </xsl:if>
1438     <xsl:text> </xsl:text>
1439     <xsl:if test="string-length($suffix) &lt; $min-suffix-length">
1440       <xsl:call-template name="repeat">
1441         <xsl:with-param name="count" select="$min-suffix-length
1442                                              - string-length($suffix)" />
1443       </xsl:call-template>
1444     </xsl:if>
1445     <xsl:value-of select="$suffix" />
1446     <xsl:value-of select="$name" />
1447   </xsl:template>
1448
1449   <!-- Output a list with a given separator.  Empty items are skipped. -->
1450   <xsl:template name="list">
1451     <xsl:param name="separator" />
1452     <xsl:param name="items" />
1453
1454     <xsl:for-each select="e:node-set($items)/*">
1455       <xsl:value-of select="." />
1456       <xsl:if test="not(position() = last())">
1457         <xsl:value-of select="$separator" />
1458       </xsl:if>
1459     </xsl:for-each>
1460   </xsl:template>
1461
1462   <!-- Repeat a string (space by default) a given number of times. -->
1463   <xsl:template name="repeat">
1464     <xsl:param name="str" select="' '" />
1465     <xsl:param name="count" />
1466
1467     <xsl:if test="$count &gt; 0">
1468       <xsl:value-of select="$str" />
1469       <xsl:call-template name="repeat">
1470         <xsl:with-param name="str"   select="$str" />
1471         <xsl:with-param name="count" select="$count - 1" />
1472       </xsl:call-template>
1473     </xsl:if>
1474   </xsl:template>
1475
1476   <!-- Record the maximum type lengths of a set of types for use as the
1477        max-type-lengths parameter of type-and-name. -->
1478   <xsl:template name="type-lengths">
1479     <xsl:param name="items" />
1480     <xsl:variable name="type-lengths-rtf">
1481       <xsl:for-each select="$items">
1482         <item>
1483           <xsl:choose>
1484             <xsl:when test="contains(., ' *')">
1485               <xsl:value-of select="string-length(
1486                                     substring-before(., ' *'))" />
1487             </xsl:when>
1488             <xsl:otherwise>
1489               <xsl:value-of select="string-length(.)" />
1490             </xsl:otherwise>
1491           </xsl:choose>
1492         </item>
1493       </xsl:for-each>
1494     </xsl:variable>
1495     <xsl:variable name="suffix-lengths-rtf">
1496       <xsl:for-each select="$items">
1497         <item>
1498           <xsl:choose>
1499             <xsl:when test="contains(., ' *')">
1500               <xsl:value-of select="string-length(substring-after(., ' *'))
1501                                     + 1" />
1502             </xsl:when>
1503             <xsl:otherwise>
1504               <xsl:text>0</xsl:text>
1505             </xsl:otherwise>
1506           </xsl:choose>
1507         </item>
1508       </xsl:for-each>
1509     </xsl:variable>
1510     <max-type-length>
1511       <xsl:call-template name="max">
1512         <xsl:with-param name="items"
1513                         select="e:node-set($type-lengths-rtf)/*" />
1514       </xsl:call-template>
1515     </max-type-length>
1516     <max-suffix-length>
1517       <xsl:call-template name="max">
1518         <xsl:with-param name="items"
1519                         select="e:node-set($suffix-lengths-rtf)/*" />
1520       </xsl:call-template>
1521     </max-suffix-length>
1522   </xsl:template>
1523
1524   <!-- Return the maximum number in a set of numbers. -->
1525   <xsl:template name="max">
1526     <xsl:param name="items" />
1527     <xsl:choose>
1528       <xsl:when test="count($items) = 0">
1529         <xsl:text>0</xsl:text>
1530       </xsl:when>
1531       <xsl:otherwise>
1532         <xsl:variable name="head" select="number($items[1])" />
1533         <xsl:variable name="tail-max">
1534           <xsl:call-template name="max">
1535             <xsl:with-param name="items" select="$items[position() > 1]" />
1536           </xsl:call-template>
1537         </xsl:variable>
1538         <xsl:choose>
1539           <xsl:when test="$head > number($tail-max)">
1540             <xsl:value-of select="$head" />
1541           </xsl:when>
1542           <xsl:otherwise>
1543             <xsl:value-of select="$tail-max" />
1544           </xsl:otherwise>
1545         </xsl:choose>
1546       </xsl:otherwise>
1547     </xsl:choose>
1548   </xsl:template>
1549 </xsl:transform>