Add comment noting the requirement to free replies when finished.
[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         <doc> * The returned value must be freed by the caller using free().</doc>
412         <doc> */</doc>
413         <field type="xcb_connection_t *" name="c" />
414         <field name="cookie">
415           <xsl:attribute name="type">
416             <xsl:call-template name="cookie-type" />
417           </xsl:attribute>
418         </field>
419         <field type="xcb_generic_error_t **" name="e" />
420         <l>return (<xsl:value-of select="xcb:xcb-prefix(@name)" />_reply_t *)<!--
421         --> xcb_wait_for_reply(c, cookie.sequence, e);</l>
422       </function>
423     </xsl:if>
424   </xsl:template>
425
426   <xsl:template match="xidtype|xidunion" mode="pass1">
427     <typedef oldname="uint32_t" newname="{xcb:xcb-prefix(@name)}_t" />
428     <iterator ref="{xcb:xcb-prefix(@name)}" />
429     <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
430   </xsl:template>
431
432   <xsl:template match="struct|union" mode="pass1">
433     <struct name="{xcb:xcb-prefix(@name)}_t">
434       <xsl:if test="self::union">
435         <xsl:attribute name="kind">union</xsl:attribute>
436       </xsl:if>
437       <xsl:apply-templates select="*" mode="field" />
438     </struct>
439     <iterator ref="{xcb:xcb-prefix(@name)}" />
440     <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
441   </xsl:template>
442
443   <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
444     <xsl:variable name="suffix">
445       <xsl:choose>
446         <xsl:when test="self::event|self::eventcopy">
447           <xsl:text>_event_t</xsl:text>
448         </xsl:when>
449         <xsl:when test="self::error|self::errorcopy">
450           <xsl:text>_error_t</xsl:text>
451         </xsl:when>
452       </xsl:choose>
453     </xsl:variable>
454     <constant type="number" name="{xcb:xcb-prefix(@name)}" value="{@number}" />
455     <xsl:choose>
456       <xsl:when test="self::event|self::error">
457         <struct name="{xcb:xcb-prefix(@name)}{$suffix}">
458           <field type="uint8_t" name="response_type" />
459           <xsl:if test="self::error">
460             <field type="uint8_t" name="error_code" />
461           </xsl:if>
462           <xsl:apply-templates select="*" mode="field" />
463           <xsl:if test="not(self::event and boolean(@no-sequence-number))">
464             <middle>
465               <field type="uint16_t" name="sequence" />
466             </middle>
467           </xsl:if>
468         </struct>
469       </xsl:when>
470       <xsl:when test="self::eventcopy|self::errorcopy">
471         <typedef newname="{xcb:xcb-prefix(@name)}{$suffix}">
472           <xsl:attribute name="oldname">
473             <xsl:call-template name="canonical-type-name">
474               <xsl:with-param name="type" select="@ref" />
475             </xsl:call-template>
476             <xsl:value-of select="$suffix" />
477           </xsl:attribute>
478         </typedef>
479       </xsl:when>
480     </xsl:choose>
481   </xsl:template>
482
483   <xsl:template match="typedef" mode="pass1">
484     <typedef>
485       <xsl:attribute name="oldname">
486         <xsl:call-template name="canonical-type-name">
487           <xsl:with-param name="type" select="@oldname" />
488         </xsl:call-template>
489         <xsl:text>_t</xsl:text>
490       </xsl:attribute>
491       <xsl:attribute name="newname">
492         <xsl:call-template name="canonical-type-name">
493           <xsl:with-param name="type" select="@newname" />
494         </xsl:call-template>
495         <xsl:text>_t</xsl:text>
496       </xsl:attribute>
497     </typedef>
498     <iterator ref="{xcb:xcb-prefix(@newname)}" />
499     <iterator-functions ref="{xcb:xcb-prefix(@newname)}" />
500   </xsl:template>
501
502   <xsl:template match="enum" mode="pass1">
503     <enum name="{xcb:xcb-prefix(@name)}_t">
504       <xsl:for-each select="item">
505         <item name="{translate(xcb:xcb-prefix(concat(../@name, concat('_', @name))), $lcase, $ucase)}">
506           <xsl:copy-of select="*" />
507         </item>
508       </xsl:for-each>
509     </enum>
510   </xsl:template>
511
512   <!--
513     Templates for processing fields.
514   -->
515
516   <xsl:template match="pad" mode="field">
517     <xsl:copy-of select="." />
518   </xsl:template>
519   
520   <xsl:template match="field|exprfield" mode="field">
521     <xsl:copy>
522       <xsl:attribute name="type">
523         <xsl:call-template name="canonical-type-name" />
524         <xsl:text>_t</xsl:text>
525       </xsl:attribute>
526       <xsl:attribute name="name">
527         <xsl:call-template name="canonical-var-name" />
528       </xsl:attribute>
529       <xsl:copy-of select="*" />
530     </xsl:copy>
531   </xsl:template>
532
533   <xsl:template match="list" mode="field">
534     <xsl:variable name="type"><!--
535       --><xsl:call-template name="canonical-type-name" />
536         <xsl:text>_t</xsl:text><!--
537     --></xsl:variable>
538     <list type="{$type}">
539       <xsl:attribute name="name">
540         <xsl:call-template name="canonical-var-name" />
541       </xsl:attribute>
542       <xsl:if test="not(parent::request) and node()
543                     and not(.//*[not(self::value or self::op)])">
544         <xsl:attribute name="fixed">true</xsl:attribute>
545       </xsl:if>
546       <!-- Handle lists with no length expressions. -->
547       <xsl:if test="not(node())">
548         <xsl:choose>
549           <!-- In a request, refer to an implicit localparam for length. -->
550           <xsl:when test="parent::request">
551             <fieldref>
552               <xsl:value-of select="concat(@name, '_len')" />
553             </fieldref>
554           </xsl:when>
555           <!-- In a reply, use the length of the reply to determine the length
556                of the list. -->
557           <xsl:when test="parent::reply">
558             <op op="/">
559               <op op="&lt;&lt;">
560                 <fieldref>length</fieldref>
561                 <value>2</value>
562               </op>
563               <function-call name="sizeof">
564                 <param><xsl:value-of select="$type" /></param>
565               </function-call>
566             </op>
567           </xsl:when>
568           <!-- Other cases generate an error. -->
569           <xsl:otherwise>
570             <xsl:message terminate="yes"><!--
571               -->Encountered a list with no length expresssion outside a<!--
572               --> request or reply.<!--
573             --></xsl:message>
574           </xsl:otherwise>
575         </xsl:choose>
576       </xsl:if>
577       <xsl:copy-of select="*" />
578     </list>
579   </xsl:template>
580
581   <xsl:template match="valueparam" mode="field">
582     <field>
583       <xsl:attribute name="type">
584         <xsl:call-template name="canonical-type-name">
585           <xsl:with-param name="type" select="@value-mask-type" />
586         </xsl:call-template>
587         <xsl:text>_t</xsl:text>
588       </xsl:attribute>
589       <xsl:attribute name="name">
590         <xsl:call-template name="canonical-var-name">
591           <xsl:with-param name="name" select="@value-mask-name" />
592         </xsl:call-template>
593       </xsl:attribute>
594     </field>
595     <list type="uint32_t">
596       <xsl:attribute name="name">
597         <xsl:call-template name="canonical-var-name">
598           <xsl:with-param name="name" select="@value-list-name" />
599         </xsl:call-template>
600       </xsl:attribute>
601       <function-call name="xcb_popcount">
602         <param>
603           <fieldref>
604             <xsl:call-template name="canonical-var-name">
605               <xsl:with-param name="name" select="@value-mask-name" />
606             </xsl:call-template>
607           </fieldref>
608         </param>
609       </function-call>
610     </list>
611   </xsl:template>
612
613   <xsl:template match="field" mode="param">
614     <field>
615       <xsl:attribute name="type">
616         <xsl:call-template name="canonical-type-name" />
617         <xsl:text>_t</xsl:text>
618       </xsl:attribute>
619       <xsl:attribute name="name">
620         <xsl:call-template name="canonical-var-name" />
621       </xsl:attribute>
622     </field>
623   </xsl:template>
624
625   <xsl:template match="list" mode="param">
626     <!-- If no length expression is provided, use a CARD32 localfield. -->
627     <xsl:if test="not(node())">
628       <field type="uint32_t" name="{@name}_len" />
629     </xsl:if>
630     <field>
631       <xsl:variable name="ctype">
632         <xsl:call-template name="canonical-type-name" />
633       </xsl:variable>
634       <xsl:attribute name="type">
635         <xsl:text>const </xsl:text>
636         <xsl:call-template name="canonical-type-name" />
637         <xsl:if test="not($ctype='char') and not($ctype='void')">
638           <xsl:text>_t</xsl:text>
639         </xsl:if>
640         <xsl:text> *</xsl:text>
641       </xsl:attribute>
642       <xsl:attribute name="name">
643         <xsl:call-template name="canonical-var-name" />
644       </xsl:attribute>
645     </field>
646   </xsl:template>
647
648   <xsl:template match="valueparam" mode="param">
649     <field>
650       <xsl:attribute name="type">
651         <xsl:call-template name="canonical-type-name">
652           <xsl:with-param name="type" select="@value-mask-type" />
653         </xsl:call-template>
654         <xsl:text>_t</xsl:text>
655       </xsl:attribute>
656       <xsl:attribute name="name">
657         <xsl:call-template name="canonical-var-name">
658           <xsl:with-param name="name" select="@value-mask-name" />
659         </xsl:call-template>
660       </xsl:attribute>
661     </field>
662     <field type="const uint32_t *">
663       <xsl:attribute name="name">
664         <xsl:call-template name="canonical-var-name">
665           <xsl:with-param name="name" select="@value-list-name" />
666         </xsl:call-template>
667       </xsl:attribute>
668     </field>
669   </xsl:template>
670
671   <!-- Second pass: Process the variable. -->
672   <xsl:variable name="result-rtf">
673     <xsl:apply-templates select="$pass1/*" mode="pass2" />
674   </xsl:variable>
675   <xsl:variable name="result" select="e:node-set($result-rtf)" />
676
677   <xsl:template match="xcb" mode="pass2">
678     <xcb>
679       <xsl:copy-of select="@*" />
680       <xsl:apply-templates mode="pass2"
681                            select="constant|enum|struct|typedef|iterator" />
682       <xsl:apply-templates mode="pass2"
683                            select="function|iterator-functions" />
684     </xcb>
685   </xsl:template>
686
687   <!-- Generic rules for nodes that don't need further processing: copy node
688        with attributes, and recursively process the child nodes. -->
689   <xsl:template match="*" mode="pass2">
690     <xsl:copy>
691       <xsl:copy-of select="@*" />
692       <xsl:apply-templates mode="pass2" />
693     </xsl:copy>
694   </xsl:template>
695
696   <xsl:template match="struct" mode="pass2">
697     <xsl:if test="@kind='union' and list[not(@fixed)]">
698       <xsl:message terminate="yes">Unions must be fixed length.</xsl:message>
699     </xsl:if>
700     <struct name="{@name}">
701       <xsl:if test="@kind">
702         <xsl:attribute name="kind">
703           <xsl:value-of select="@kind" />
704         </xsl:attribute>
705       </xsl:if>
706       <!-- FIXME: This should go by size, not number of fields. -->
707       <xsl:copy-of select="node()[not(self::middle)
708                    and position() &lt; 3]" />
709       <xsl:if test="middle and (count(*[not(self::middle)]) &lt; 2)">
710         <pad bytes="{2 - count(*[not(self::middle)])}" />
711       </xsl:if>
712       <xsl:copy-of select="middle/*" />
713       <xsl:copy-of select="node()[not(self::middle) and (position() > 2)]" />
714     </struct>
715   </xsl:template>
716
717   <xsl:template match="do-request" mode="pass2">
718     <xsl:variable name="struct"
719                   select="$pass1/xcb/struct[@name=current()/@ref]" />
720
721     <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
722
723     <l>static const xcb_protocol_request_t xcb_req = {</l>
724     <indent>
725       <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
726       <l>/* ext */ <xsl:choose>
727                      <xsl:when test="$ext">
728                        <xsl:text>&amp;</xsl:text>
729                        <xsl:value-of select="xcb:xcb-prefix()" />
730                        <xsl:text>_id</xsl:text>
731                      </xsl:when>
732                      <xsl:otherwise>0</xsl:otherwise>
733                    </xsl:choose>,</l>
734       <l>/* opcode */ <xsl:value-of select="@opcode" />,</l>
735       <l>/* isvoid */ <xsl:value-of select="1-boolean(@has-reply)" /></l>
736     </indent>
737     <l>};</l>
738
739     <l />
740     <l>struct iovec xcb_parts[<xsl:value-of select="$num-parts+2" />];</l>
741     <l><xsl:value-of select="../@type" /> xcb_ret;</l>
742     <l><xsl:value-of select="@ref" /> xcb_out;</l>
743
744     <l />
745     <xsl:if test="not ($ext) and not($struct//*[(self::field or self::exprfield or self::pad)
746                                                 and not(boolean(@no-assign))])">
747       <l>xcb_out.pad0 = 0;</l>
748     </xsl:if>
749     <xsl:apply-templates select="$struct//*[(self::field or self::exprfield or self::pad)
750                                             and not(boolean(@no-assign))]"
751                          mode="assign" />
752
753     <l />
754     <l>xcb_parts[2].iov_base = (char *) &amp;xcb_out;</l>
755     <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
756     <l>xcb_parts[3].iov_base = 0;</l>
757     <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len &amp; 3;</l>
758
759     <xsl:for-each select="$struct/list">
760       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
761       --><xsl:value-of select="@name" />;</l>
762       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
763       --><xsl:apply-templates mode="output-expression" />
764       <xsl:if test="not(@type = 'void_t')">
765         <xsl:text> * sizeof(</xsl:text>
766           <xsl:choose>
767           <xsl:when test="@type='char_t'">
768             <xsl:text>char</xsl:text>
769           </xsl:when>
770           <xsl:otherwise>
771             <xsl:value-of select="@type" />
772           </xsl:otherwise>
773         </xsl:choose>
774         <xsl:text>)</xsl:text>
775       </xsl:if>;</l>
776       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
777       <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>
778     </xsl:for-each>
779
780     <l>xcb_ret.sequence = xcb_send_request(c, <!--
781     --><xsl:choose>
782          <xsl:when test="@checked='true'">XCB_REQUEST_CHECKED</xsl:when>
783          <xsl:otherwise>0</xsl:otherwise>
784        </xsl:choose>, xcb_parts + 2, &amp;xcb_req);</l>
785     <l>return xcb_ret;</l>
786   </xsl:template>
787
788   <xsl:template match="field" mode="assign">
789     <l>
790       <xsl:text>xcb_out.</xsl:text>
791       <xsl:value-of select="@name" />
792       <xsl:text> = </xsl:text>
793       <xsl:value-of select="@name" />
794       <xsl:text>;</xsl:text>
795     </l>
796   </xsl:template>
797
798   <xsl:template match="exprfield" mode="assign">
799     <l>
800       <xsl:text>xcb_out.</xsl:text>
801       <xsl:value-of select="@name" />
802       <xsl:text> = </xsl:text>
803       <xsl:apply-templates mode="output-expression" />
804       <xsl:text>;</xsl:text>
805     </l>
806   </xsl:template>
807
808   <xsl:template match="pad" mode="assign">
809     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
810     <l><xsl:choose>
811         <xsl:when test="@bytes = 1">xcb_out.pad<xsl:value-of select="$padnum - 1" /> = 0;</xsl:when>
812         <xsl:otherwise>memset(xcb_out.pad<xsl:value-of select="$padnum - 1" />, 0, <xsl:value-of select="@bytes" />);</xsl:otherwise>
813     </xsl:choose></l>
814   </xsl:template>
815
816   <xsl:template match="iterator" mode="pass2">
817     <struct name="{@ref}_iterator_t">
818       <field type="{@ref}_t *" name="data" />
819       <field type="int" name="rem" />
820       <field type="int" name="index" />
821     </struct>
822   </xsl:template>
823
824   <xsl:template match="iterator-functions" mode="pass2">
825     <xsl:variable name="ref" select="@ref" />
826     <xsl:variable name="kind" select="@kind" />
827     <xsl:variable name="struct"
828                   select="$pass1/xcb/struct[@name=concat($ref, $kind, '_t')]" />
829     <xsl:variable name="nextfields-rtf">
830       <nextfield>R + 1</nextfield>
831       <xsl:for-each select="$struct/list[not(@fixed)]">
832         <xsl:choose>
833           <xsl:when test="substring(@type, 1, 3) = 'xcb'">
834             <nextfield><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_end(<!--
835             --><xsl:value-of select="$ref" />_<!--
836             --><xsl:value-of select="string(@name)" />_iterator(R))</nextfield>
837           </xsl:when>
838           <xsl:otherwise>
839             <nextfield><xsl:value-of select="$ref" />_<!--
840             --><xsl:value-of select="string(@name)" />_end(R)</nextfield>
841           </xsl:otherwise>
842         </xsl:choose>
843       </xsl:for-each>
844     </xsl:variable>
845     <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
846     <xsl:for-each select="$struct/list[not(@fixed)]">
847       <xsl:variable name="number"
848                     select="1+count(preceding-sibling::list[not(@fixed)])" />
849       <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
850       <xsl:variable name="is-first"
851                     select="not(preceding-sibling::list[not(@fixed)])" />
852       <xsl:variable name="field-name" select="@name" />
853       <xsl:variable name="is-variable"
854                     select="$pass1/xcb/struct[@name=current()/@type]/list
855                             or document($search-path)/xcb
856                                /struct[concat(xcb:xcb-prefix(@name), '_t')
857                                        = current()/@type]
858                                /*[self::valueparam
859                                   or self::list[.//*[not(self::value
860                                                          or self::op)]]]" />
861       <xsl:if test="not($is-variable)">
862         <function type="{xcb:get-char-void(@type)} *" name="{$ref}_{xcb:lowercase($field-name)}">
863           <field type="const {$ref}{$kind}_t *" name="R" />
864           <xsl:choose>
865             <xsl:when test="$is-first">
866               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
867               -->(<xsl:value-of select="$nextfield" />);</l>
868             </xsl:when>
869             <xsl:otherwise>
870               <l>xcb_generic_iterator_t prev = <!--
871               --><xsl:value-of select="$nextfield" />;</l>
872               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
873               -->((char *) prev.data + XCB_TYPE_PAD(<!--
874               --><xsl:value-of select="xcb:get-char-void(@type)" />, prev.index));</l>
875             </xsl:otherwise>
876           </xsl:choose>
877         </function>
878       </xsl:if>
879       <function type="int" name="{$ref}_{xcb:lowercase($field-name)}_length">
880         <field type="const {$ref}{$kind}_t *" name="R" />
881         <l>return <xsl:apply-templates mode="output-expression">
882                     <xsl:with-param name="field-prefix" select="'R->'" />
883                   </xsl:apply-templates>;</l>
884       </function>
885       <xsl:choose>
886         <xsl:when test="substring(@type, 1, 3) = 'xcb'">
887           <function type="{substring(@type, 1, string-length(@type)-2)}_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_iterator">
888             <field type="const {$ref}{$kind}_t *" name="R" />
889             <l><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_iterator_t i;</l>
890             <xsl:choose>
891               <xsl:when test="$is-first">
892                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
893                 -->(<xsl:value-of select="$nextfield" />);</l>
894               </xsl:when>
895               <xsl:otherwise>
896                 <l>xcb_generic_iterator_t prev = <!--
897                 --><xsl:value-of select="$nextfield" />;</l>
898                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
899                 -->((char *) prev.data + XCB_TYPE_PAD(<!--
900                 --><xsl:value-of select="@type" />, prev.index));</l>
901               </xsl:otherwise>
902             </xsl:choose>
903             <l>i.rem = <xsl:apply-templates mode="output-expression">
904                          <xsl:with-param name="field-prefix" select="'R->'" />
905                        </xsl:apply-templates>;</l>
906             <l>i.index = (char *) i.data - (char *) R;</l>
907             <l>return i;</l>
908           </function>
909         </xsl:when>
910         <xsl:otherwise>
911           <xsl:variable name="cast">
912             <xsl:choose>
913               <xsl:when test="@type='void'">char</xsl:when>
914               <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
915             </xsl:choose>
916           </xsl:variable>
917           <function type="xcb_generic_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_end">
918             <field type="const {$ref}{$kind}_t *" name="R" />
919             <l>xcb_generic_iterator_t i;</l>
920             <xsl:choose>
921               <xsl:when test="$is-first">
922                 <l>i.data = ((<xsl:value-of select="xcb:remove-void($cast)" /> *) <!--
923                 -->(<xsl:value-of select="$nextfield" />)) + (<!--
924                 --><xsl:apply-templates mode="output-expression">
925                      <xsl:with-param name="field-prefix" select="'R->'" />
926                    </xsl:apply-templates>);</l>
927               </xsl:when>
928               <xsl:otherwise>
929                 <l>xcb_generic_iterator_t child = <!--
930                 --><xsl:value-of select="$nextfield" />;</l>
931                 <l>i.data = ((<xsl:value-of select="xcb:get-char-void($cast)" /> *) <!--
932                 -->child.data) + (<!--
933                 --><xsl:apply-templates mode="output-expression">
934                      <xsl:with-param name="field-prefix" select="'R->'" />
935                    </xsl:apply-templates>);</l>
936               </xsl:otherwise>
937             </xsl:choose>
938             <l>i.rem = 0;</l>
939             <l>i.index = (char *) i.data - (char *) R;</l>
940             <l>return i;</l>
941           </function>
942         </xsl:otherwise>
943       </xsl:choose>
944     </xsl:for-each>
945     <xsl:if test="not($kind)">
946       <function type="void" name="{$ref}_next">
947         <doc>/**</doc>
948         <doc> * Get the next element of the iterator</doc>
949         <doc> * @param i Pointer to a <xsl:value-of select="$ref" />_iterator_t</doc>
950         <doc> *</doc>
951         <doc> * Get the next element in the iterator. The member rem is</doc>
952         <doc> * decreased by one. The member data points to the next</doc>
953         <doc> * element. The member index is increased by sizeof(<xsl:value-of select="$ref" />_t)</doc>
954         <doc> */</doc>
955         <field type="{$ref}_iterator_t *" name="i" />
956         <xsl:choose>
957           <xsl:when test="$struct/list[not(@fixed)]">
958             <l><xsl:value-of select="$ref" />_t *R = i->data;</l>
959             <l>xcb_generic_iterator_t child = <!--
960             --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
961             <l>--i->rem;</l>
962             <l>i->data = (<xsl:value-of select="$ref" />_t *) child.data;</l>
963             <l>i->index = child.index;</l>
964           </xsl:when>
965           <xsl:otherwise>
966             <l>--i->rem;</l>
967             <l>++i->data;</l>
968             <l>i->index += sizeof(<xsl:value-of select="$ref" />_t);</l>
969           </xsl:otherwise>
970         </xsl:choose>
971       </function>
972       <function type="xcb_generic_iterator_t" name="{$ref}_end">
973         <doc>/**</doc>
974         <doc> * Return the iterator pointing to the last element</doc>
975         <doc> * @param i An <xsl:value-of select="$ref" />_iterator_t</doc>
976         <doc> * @return  The iterator pointing to the last element</doc>
977         <doc> *</doc>
978         <doc> * Set the current element in the iterator to the last element.</doc>
979         <doc> * The member rem is set to 0. The member data points to the</doc>
980         <doc> * last element.</doc>
981         <doc> */</doc>
982         <field type="{$ref}_iterator_t" name="i" />
983         <l>xcb_generic_iterator_t ret;</l>
984         <xsl:choose>
985           <xsl:when test="$struct/list[not(@fixed)]">
986             <l>while(i.rem > 0)</l>
987             <indent>
988               <l><xsl:value-of select="$ref" />_next(&amp;i);</l>
989             </indent>
990             <l>ret.data = i.data;</l>
991             <l>ret.rem = i.rem;</l>
992             <l>ret.index = i.index;</l>
993           </xsl:when>
994           <xsl:otherwise>
995             <l>ret.data = i.data + i.rem;</l>
996             <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
997             <l>ret.rem = 0;</l>
998           </xsl:otherwise>
999         </xsl:choose>
1000         <l>return ret;</l>
1001       </function>
1002     </xsl:if>
1003   </xsl:template>
1004
1005   <!-- Output the results. -->
1006   <xsl:template match="/">
1007     <xsl:if test="not(function-available('e:node-set'))">
1008       <xsl:message terminate="yes"><!--
1009         -->Error: This stylesheet requires the EXSL node-set extension.<!--
1010       --></xsl:message>
1011     </xsl:if>
1012
1013     <xsl:if test="not($h) and not($c)">
1014       <xsl:message terminate="yes"><!--
1015         -->Error: Parameter "mode" must be "header" or "source".<!--
1016       --></xsl:message>
1017     </xsl:if>
1018
1019     <xsl:apply-templates select="$result/*" mode="output" />
1020   </xsl:template>
1021
1022   <xsl:template match="xcb" mode="output">
1023     <xsl:variable name="guard"><!--
1024       -->__<xsl:value-of select="$ucase-header" />_H<!--
1025     --></xsl:variable>
1026
1027 <xsl:text>/*
1028  * This file generated automatically from </xsl:text>
1029 <xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
1030  * Edit at your peril.
1031  */
1032 </xsl:text>
1033 <xsl:if test="$h"><xsl:text>
1034 /**
1035  * @defgroup XCB_</xsl:text><xsl:value-of select="$ext" /><xsl:text>_API XCB </xsl:text><xsl:value-of select="$ext" /><xsl:text> API
1036  * @brief </xsl:text><xsl:value-of select="$ext" /><xsl:text> XCB Protocol Implementation.</xsl:text>
1037 <xsl:text>
1038  * @{
1039  **/
1040 </xsl:text>
1041
1042 <xsl:text>
1043 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
1044 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
1045 </xsl:text>
1046 #include "xcb.h"
1047 <xsl:for-each select="$root/xcb/import">
1048 <xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
1049 </xsl:text>
1050 </xsl:for-each>
1051 <xsl:text>
1052 </xsl:text>
1053 </xsl:if>
1054 <xsl:if test="$h">
1055     <xsl:choose>
1056         <xsl:when test="string($ext)">
1057   <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>
1058 </xsl:text>
1059   <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" />
1060   <xsl:text>
1061   
1062 </xsl:text>
1063     </xsl:when>
1064   </xsl:choose>
1065 </xsl:if>
1066
1067 <xsl:if test="$c">
1068 <xsl:if test="$need-string-h">
1069 #include &lt;string.h&gt;</xsl:if>
1070 <xsl:text>
1071 #include &lt;assert.h&gt;
1072 #include "xcbext.h"
1073 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
1074
1075 </xsl:text></xsl:if>
1076
1077     <xsl:apply-templates mode="output" />
1078
1079 <xsl:if test="$h">
1080 <xsl:text>
1081 #endif
1082
1083 /**
1084  * @}
1085  */
1086 </xsl:text>
1087 </xsl:if>
1088   </xsl:template>
1089
1090   <xsl:template match="constant" mode="output">
1091     <xsl:choose>
1092       <xsl:when test="@type = 'number'">
1093         <xsl:if test="$h">
1094           <xsl:text>/** Opcode for </xsl:text><xsl:value-of select="@name"/><xsl:text>. */
1095 </xsl:text>
1096           <xsl:text>#define </xsl:text>
1097           <xsl:value-of select="translate(@name, $lcase, $ucase)" />
1098           <xsl:text> </xsl:text>
1099           <xsl:value-of select="@value" />
1100           <xsl:text>
1101
1102 </xsl:text>
1103         </xsl:if>
1104       </xsl:when>
1105       <xsl:when test="@type = 'string'">
1106         <xsl:if test="$h">
1107           <xsl:text>extern </xsl:text>
1108         </xsl:if>
1109         <xsl:text>const char </xsl:text>
1110         <xsl:value-of select="@name" />
1111         <xsl:text>[]</xsl:text>
1112         <xsl:if test="$c">
1113           <xsl:text> = "</xsl:text>
1114           <xsl:value-of select="@value" />
1115           <xsl:text>"</xsl:text>
1116         </xsl:if>
1117         <xsl:text>;
1118
1119 </xsl:text>
1120       </xsl:when>
1121       <xsl:otherwise>
1122         <xsl:if test="$h">
1123           <xsl:text>extern </xsl:text>
1124         </xsl:if>
1125         <xsl:call-template name="type-and-name" />
1126         <xsl:if test="$c">
1127           <xsl:text> = </xsl:text>
1128           <xsl:value-of select="@value" />
1129         </xsl:if>
1130         <xsl:text>;
1131
1132 </xsl:text>
1133       </xsl:otherwise>
1134     </xsl:choose>
1135   </xsl:template>
1136
1137   <xsl:template match="typedef" mode="output">
1138     <xsl:if test="$h">
1139       <xsl:text>typedef </xsl:text>
1140       <xsl:value-of select="xcb:get-char-void(@oldname)" />
1141       <xsl:text> </xsl:text>
1142       <xsl:value-of select="@newname" />
1143       <xsl:text>;
1144
1145 </xsl:text>
1146     </xsl:if>
1147   </xsl:template>
1148
1149   <xsl:template match="struct" mode="output">
1150     <xsl:if test="$h">
1151       <xsl:variable name="type-lengths">
1152         <xsl:call-template name="type-lengths">
1153           <xsl:with-param name="items" select="field/@type" />
1154         </xsl:call-template>
1155       </xsl:variable>
1156       <xsl:text>/**
1157  * @brief </xsl:text><xsl:value-of select="@name" /><xsl:text>
1158  **/
1159 </xsl:text>
1160       <xsl:text>typedef </xsl:text>
1161       <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
1162       <xsl:text> </xsl:text>
1163       <xsl:value-of select="@name" />
1164       <xsl:text> {
1165 </xsl:text>
1166       <xsl:for-each select="exprfield|field|list[@fixed]|pad">
1167         <xsl:text>    </xsl:text>
1168         <xsl:apply-templates select=".">
1169           <xsl:with-param name="type-lengths" select="$type-lengths" />
1170         </xsl:apply-templates>
1171         <xsl:text>; /**&lt; </xsl:text><xsl:text> */
1172 </xsl:text>
1173       </xsl:for-each>
1174       <xsl:text>} </xsl:text>
1175       <xsl:value-of select="@name" />
1176       <xsl:text>;
1177
1178 </xsl:text>
1179     </xsl:if>
1180   </xsl:template>
1181
1182   <xsl:template match="enum" mode="output">
1183     <xsl:if test="$h">
1184       <xsl:text>typedef enum </xsl:text>
1185       <xsl:value-of select="@name" />
1186       <xsl:text> {
1187     </xsl:text>
1188       <xsl:call-template name="list">
1189         <xsl:with-param name="separator"><xsl:text>,
1190     </xsl:text></xsl:with-param>
1191         <xsl:with-param name="items">
1192           <xsl:for-each select="item">
1193             <item>
1194               <xsl:value-of select="@name" />
1195               <xsl:if test="node()"> <!-- If there is an expression -->
1196                 <xsl:text> = </xsl:text>
1197                 <xsl:apply-templates mode="output-expression" />
1198               </xsl:if>
1199             </item>
1200           </xsl:for-each>
1201         </xsl:with-param>
1202       </xsl:call-template>
1203       <xsl:text>
1204 } </xsl:text><xsl:value-of select="@name" /><xsl:text>;
1205
1206 </xsl:text>
1207     </xsl:if>
1208   </xsl:template>
1209
1210   <xsl:template match="function" mode="output">
1211     <xsl:variable name="decl-open" select="concat(@name, ' (')" />
1212     <xsl:variable name="type-lengths">
1213       <xsl:call-template name="type-lengths">
1214         <xsl:with-param name="items" select="field/@type" />
1215       </xsl:call-template>
1216   </xsl:variable>
1217   <!-- Doxygen for functions in header. -->
1218     <xsl:if test="$h">
1219       <xsl:apply-templates select="doc" mode="function-doc">
1220       </xsl:apply-templates>
1221     </xsl:if>
1222 /*****************************************************************************
1223  **
1224  ** <xsl:value-of select="@type" />
1225  <xsl:text> </xsl:text>
1226  <xsl:value-of select="@name" />
1227  ** <xsl:call-template name="list">
1228      <xsl:with-param name="items">
1229          <xsl:for-each select="field">
1230              <item>
1231                  <xsl:text>
1232  ** @param </xsl:text>
1233                  <xsl:apply-templates select=".">
1234                      <xsl:with-param name="type-lengths" select="$type-lengths" />
1235                  </xsl:apply-templates>
1236              </item>
1237          </xsl:for-each>
1238      </xsl:with-param>
1239  </xsl:call-template>
1240  ** @returns <xsl:value-of select="@type" />
1241  **
1242  *****************************************************************************/
1243  
1244 <xsl:value-of select="@type" />
1245     <xsl:text>
1246 </xsl:text>
1247     <xsl:value-of select="$decl-open" />
1248     <xsl:call-template name="list">
1249       <xsl:with-param name="separator">
1250         <xsl:text>,
1251 </xsl:text>
1252         <xsl:call-template name="repeat">
1253           <xsl:with-param name="count" select="string-length($decl-open)" />
1254         </xsl:call-template>
1255       </xsl:with-param>
1256       <xsl:with-param name="items">
1257         <xsl:for-each select="field">
1258           <item>
1259             <xsl:apply-templates select=".">
1260               <xsl:with-param name="type-lengths" select="$type-lengths" />
1261             </xsl:apply-templates>
1262             <xsl:text>  /**&lt; */</xsl:text>
1263           </item>
1264         </xsl:for-each>
1265       </xsl:with-param>
1266     </xsl:call-template>
1267     <xsl:text>)</xsl:text>
1268
1269     <xsl:if test="$h"><xsl:text>;
1270
1271 </xsl:text></xsl:if>
1272
1273     <xsl:if test="$c">
1274       <xsl:text>
1275 {
1276 </xsl:text>
1277       <xsl:apply-templates select="l|indent" mode="function-body">
1278         <xsl:with-param name="indent" select="$indent-string" />
1279       </xsl:apply-templates>
1280       <xsl:text>}
1281
1282 </xsl:text>
1283     </xsl:if>
1284   </xsl:template>
1285
1286   <xsl:template match="doc" mode="function-doc">
1287     <xsl:value-of select="." /><xsl:text>
1288 </xsl:text>
1289   </xsl:template>
1290
1291   <xsl:template match="l" mode="function-body">
1292     <xsl:param name="indent" />
1293     <xsl:value-of select="concat($indent, .)" /><xsl:text>
1294 </xsl:text>
1295   </xsl:template>
1296
1297   <xsl:template match="indent" mode="function-body">
1298     <xsl:param name="indent" />
1299     <xsl:apply-templates select="l|indent" mode="function-body">
1300       <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
1301     </xsl:apply-templates>
1302   </xsl:template>
1303
1304   <xsl:template match="value" mode="output-expression">
1305     <xsl:value-of select="." />
1306   </xsl:template>
1307
1308   <xsl:template match="fieldref" mode="output-expression">
1309     <xsl:param name="field-prefix" />
1310     <xsl:value-of select="concat($field-prefix, .)" />
1311   </xsl:template>
1312
1313   <xsl:template match="op" mode="output-expression">
1314     <xsl:param name="field-prefix" />
1315     <xsl:text>(</xsl:text>
1316     <xsl:apply-templates select="node()[1]" mode="output-expression">
1317       <xsl:with-param name="field-prefix" select="$field-prefix" />
1318     </xsl:apply-templates>
1319     <xsl:text> </xsl:text>
1320     <xsl:value-of select="@op" />
1321     <xsl:text> </xsl:text>
1322     <xsl:apply-templates select="node()[2]" mode="output-expression">
1323       <xsl:with-param name="field-prefix" select="$field-prefix" />
1324     </xsl:apply-templates>
1325     <xsl:text>)</xsl:text>
1326   </xsl:template>
1327
1328   <xsl:template match="bit" mode="output-expression">
1329     <xsl:text>(1 &lt;&lt; </xsl:text>
1330     <xsl:value-of select="." />
1331     <xsl:text>)</xsl:text>
1332   </xsl:template>
1333
1334   <xsl:template match="function-call" mode="output-expression">
1335     <xsl:param name="field-prefix" />
1336     <xsl:value-of select="@name" />
1337     <xsl:text>(</xsl:text>
1338     <xsl:call-template name="list">
1339       <xsl:with-param name="separator" select="', '" />
1340       <xsl:with-param name="items">
1341         <xsl:for-each select="param">
1342           <item><xsl:apply-templates mode="output-expression">
1343             <xsl:with-param name="field-prefix" select="$field-prefix" />
1344           </xsl:apply-templates></item>
1345         </xsl:for-each>
1346       </xsl:with-param>
1347     </xsl:call-template>
1348     <xsl:text>)</xsl:text>
1349   </xsl:template>
1350
1351   <!-- Catch invalid elements in expressions. -->
1352   <xsl:template match="*" mode="output-expression">
1353     <xsl:message terminate="yes">
1354       <xsl:text>Invalid element in expression: </xsl:text>
1355       <xsl:value-of select="name()" />
1356     </xsl:message>
1357   </xsl:template>
1358
1359   <xsl:template match="field|exprfield">
1360     <xsl:param name="type-lengths" select="0" />
1361     <xsl:call-template name="type-and-name">
1362       <xsl:with-param name="type-lengths" select="$type-lengths" />
1363     </xsl:call-template>
1364   </xsl:template>
1365
1366   <xsl:template match="list[@fixed]">
1367     <xsl:param name="type-lengths" select="0" />
1368     <xsl:call-template name="type-and-name">
1369       <xsl:with-param name="type-lengths" select="$type-lengths" />
1370     </xsl:call-template>
1371     <xsl:text>[</xsl:text>
1372     <xsl:apply-templates mode="output-expression" />
1373     <xsl:text>]</xsl:text>
1374   </xsl:template>
1375
1376   <xsl:template match="pad">
1377     <xsl:param name="type-lengths" select="0" />
1378
1379     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
1380
1381     <xsl:call-template name="type-and-name">
1382       <xsl:with-param name="type" select="'uint8_t'" />
1383       <xsl:with-param name="name">
1384         <xsl:text>pad</xsl:text>
1385         <xsl:value-of select="$padnum - 1" />
1386       </xsl:with-param>
1387       <xsl:with-param name="type-lengths" select="$type-lengths" />
1388     </xsl:call-template>
1389     <xsl:if test="@bytes > 1">
1390       <xsl:text>[</xsl:text>
1391       <xsl:value-of select="@bytes" />
1392       <xsl:text>]</xsl:text>
1393     </xsl:if>
1394   </xsl:template>
1395
1396   <!-- Output the given type and name (defaulting to the corresponding
1397        attributes of the context node), with the appropriate spacing.  The
1398        type must consist of a base type (which may contain spaces), then
1399        optionally a single space and a suffix of one or more '*' characters.
1400        If the type-lengths parameter is provided, use it to line up the base
1401        types and suffixs of the type declarations. -->
1402   <xsl:template name="type-and-name">
1403     <xsl:param name="type" select="@type" />
1404     <xsl:param name="name" select="@name" />
1405     <xsl:param name="type-lengths">
1406       <max-type-length>0</max-type-length>
1407       <max-suffix-length>0</max-suffix-length>
1408     </xsl:param>
1409     
1410     <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
1411     <xsl:variable name="min-type-length"
1412                   select="$type-lengths-ns/max-type-length" />
1413     <xsl:variable name="min-suffix-length"
1414                   select="$type-lengths-ns/max-suffix-length" />
1415
1416     <xsl:variable name="base-type">
1417       <xsl:choose>
1418         <xsl:when test="contains($type, ' *')">
1419           <xsl:value-of select="substring-before($type, ' *')" />
1420         </xsl:when>
1421         <xsl:otherwise>
1422           <xsl:value-of select="$type" />
1423         </xsl:otherwise>
1424       </xsl:choose>
1425     </xsl:variable>
1426     <xsl:variable name="suffix">
1427       <xsl:if test="contains($type, ' *')">
1428         <xsl:text>*</xsl:text>
1429         <xsl:value-of select="substring-after($type, ' *')" />
1430       </xsl:if>
1431     </xsl:variable>
1432
1433     <xsl:value-of select="$base-type" />
1434     <xsl:if test="string-length($base-type) &lt; $min-type-length">
1435       <xsl:call-template name="repeat">
1436         <xsl:with-param name="count" select="$min-type-length
1437                                              - string-length($base-type)" />
1438       </xsl:call-template>
1439     </xsl:if>
1440     <xsl:text> </xsl:text>
1441     <xsl:if test="string-length($suffix) &lt; $min-suffix-length">
1442       <xsl:call-template name="repeat">
1443         <xsl:with-param name="count" select="$min-suffix-length
1444                                              - string-length($suffix)" />
1445       </xsl:call-template>
1446     </xsl:if>
1447     <xsl:value-of select="$suffix" />
1448     <xsl:value-of select="$name" />
1449   </xsl:template>
1450
1451   <!-- Output a list with a given separator.  Empty items are skipped. -->
1452   <xsl:template name="list">
1453     <xsl:param name="separator" />
1454     <xsl:param name="items" />
1455
1456     <xsl:for-each select="e:node-set($items)/*">
1457       <xsl:value-of select="." />
1458       <xsl:if test="not(position() = last())">
1459         <xsl:value-of select="$separator" />
1460       </xsl:if>
1461     </xsl:for-each>
1462   </xsl:template>
1463
1464   <!-- Repeat a string (space by default) a given number of times. -->
1465   <xsl:template name="repeat">
1466     <xsl:param name="str" select="' '" />
1467     <xsl:param name="count" />
1468
1469     <xsl:if test="$count &gt; 0">
1470       <xsl:value-of select="$str" />
1471       <xsl:call-template name="repeat">
1472         <xsl:with-param name="str"   select="$str" />
1473         <xsl:with-param name="count" select="$count - 1" />
1474       </xsl:call-template>
1475     </xsl:if>
1476   </xsl:template>
1477
1478   <!-- Record the maximum type lengths of a set of types for use as the
1479        max-type-lengths parameter of type-and-name. -->
1480   <xsl:template name="type-lengths">
1481     <xsl:param name="items" />
1482     <xsl:variable name="type-lengths-rtf">
1483       <xsl:for-each select="$items">
1484         <item>
1485           <xsl:choose>
1486             <xsl:when test="contains(., ' *')">
1487               <xsl:value-of select="string-length(
1488                                     substring-before(., ' *'))" />
1489             </xsl:when>
1490             <xsl:otherwise>
1491               <xsl:value-of select="string-length(.)" />
1492             </xsl:otherwise>
1493           </xsl:choose>
1494         </item>
1495       </xsl:for-each>
1496     </xsl:variable>
1497     <xsl:variable name="suffix-lengths-rtf">
1498       <xsl:for-each select="$items">
1499         <item>
1500           <xsl:choose>
1501             <xsl:when test="contains(., ' *')">
1502               <xsl:value-of select="string-length(substring-after(., ' *'))
1503                                     + 1" />
1504             </xsl:when>
1505             <xsl:otherwise>
1506               <xsl:text>0</xsl:text>
1507             </xsl:otherwise>
1508           </xsl:choose>
1509         </item>
1510       </xsl:for-each>
1511     </xsl:variable>
1512     <max-type-length>
1513       <xsl:call-template name="max">
1514         <xsl:with-param name="items"
1515                         select="e:node-set($type-lengths-rtf)/*" />
1516       </xsl:call-template>
1517     </max-type-length>
1518     <max-suffix-length>
1519       <xsl:call-template name="max">
1520         <xsl:with-param name="items"
1521                         select="e:node-set($suffix-lengths-rtf)/*" />
1522       </xsl:call-template>
1523     </max-suffix-length>
1524   </xsl:template>
1525
1526   <!-- Return the maximum number in a set of numbers. -->
1527   <xsl:template name="max">
1528     <xsl:param name="items" />
1529     <xsl:choose>
1530       <xsl:when test="count($items) = 0">
1531         <xsl:text>0</xsl:text>
1532       </xsl:when>
1533       <xsl:otherwise>
1534         <xsl:variable name="head" select="number($items[1])" />
1535         <xsl:variable name="tail-max">
1536           <xsl:call-template name="max">
1537             <xsl:with-param name="items" select="$items[position() > 1]" />
1538           </xsl:call-template>
1539         </xsl:variable>
1540         <xsl:choose>
1541           <xsl:when test="$head > number($tail-max)">
1542             <xsl:value-of select="$head" />
1543           </xsl:when>
1544           <xsl:otherwise>
1545             <xsl:value-of select="$tail-max" />
1546           </xsl:otherwise>
1547         </xsl:choose>
1548       </xsl:otherwise>
1549     </xsl:choose>
1550   </xsl:template>
1551 </xsl:transform>