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