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