1 <?xml version="1.0" encoding="utf-8"?>
3 Copyright (C) 2004 Josh Triplett. All Rights Reserved.
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:
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
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.
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.
27 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
29 xmlns:e="http://exslt.org/common">
31 <xsl:output method="text" />
33 <xsl:strip-space elements="*" />
35 <!-- "header" or "source" -->
36 <xsl:param name="mode" />
38 <!-- Path to the core protocol descriptions. -->
39 <xsl:param name="base-path" />
41 <!-- Path to the extension protocol descriptions. -->
42 <xsl:param name="extension-path" select="$base-path" />
44 <xsl:variable name="h" select="$mode = 'header'" />
45 <xsl:variable name="c" select="$mode = 'source'" />
47 <!-- String used to indent lines of code. -->
48 <xsl:variable name="indent-string" select="' '" />
50 <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
51 <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
53 <xsl:variable name="header" select="/xcb/@header" />
54 <xsl:variable name="ucase-header"
55 select="translate($header,$lcase,$ucase)" />
57 <xsl:variable name="ext" select="/xcb/@extension-name" />
59 <!-- Other protocol descriptions to search for types in, after checking the
60 current protocol description. -->
61 <xsl:variable name="search-path-rtf">
62 <xsl:for-each select="/xcb/import">
63 <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
65 <xsl:if test="not($header='xproto')">
66 <path><xsl:value-of select="concat($base-path, 'xproto.xml')" /></path>
69 <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>
71 <xsl:variable name="root" select="/" />
73 <!-- First pass: Store everything in a variable. -->
74 <xsl:variable name="pass1-rtf">
75 <xsl:apply-templates select="/" mode="pass1" />
77 <xsl:variable name="pass1" select="e:node-set($pass1-rtf)" />
79 <xsl:template match="xcb" mode="pass1">
81 <xsl:copy-of select="@*" />
83 <constant type="XCBExtension" name="XCB{$ext}Id">
84 <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
86 <function type="const XCBQueryExtensionRep *" name="XCB{$ext}Init">
87 <field type="XCBConnection *" name="c" />
88 <l>return XCBGetExtensionData(c, &XCB<!--
89 --><xsl:value-of select="$ext" />Id);</l>
92 <xsl:apply-templates mode="pass1" />
96 <!-- Modify names that conflict with C++ keywords by prefixing them with an
97 underscore. If the name parameter is not specified, it defaults to the
98 value of the name attribute on the context node. -->
99 <xsl:template name="canonical-var-name">
100 <xsl:param name="name" select="@name" />
101 <xsl:if test="$name='new' or $name='delete'
102 or $name='class' or $name='operator'">
103 <xsl:text>_</xsl:text>
105 <xsl:value-of select="$name" />
108 <!-- List of core types, for use in canonical-type-name. -->
109 <xsl:variable name="core-types-rtf">
112 <type name="CARD8" />
113 <type name="CARD16" />
114 <type name="CARD32" />
116 <type name="INT16" />
117 <type name="INT32" />
121 <type name="float" />
122 <type name="double" />
125 <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />
128 Output the canonical name for a type. This will be
129 XCB{extension-containing-Type-if-any}Type, wherever the type is found in
130 the search path, or just Type if not found. If the type parameter is not
131 specified, it defaults to the value of the type attribute on the context
134 <xsl:template name="canonical-type-name">
135 <xsl:param name="type" select="string(@type)" />
137 <xsl:variable name="is-unqualified" select="not(contains($type, ':'))"/>
138 <xsl:variable name="namespace" select="substring-before($type, ':')" />
139 <xsl:variable name="unqualified-type">
141 <xsl:when test="$is-unqualified">
142 <xsl:value-of select="$type" />
145 <xsl:value-of select="substring-after($type, ':')" />
151 <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
152 <xsl:value-of select="$type" />
155 <xsl:variable name="type-definitions"
156 select="(/xcb|document($search-path)/xcb
157 )[$is-unqualified or @header=$namespace]
158 /*[((self::struct or self::union
159 or self::xidtype or self::enum
160 or self::event or self::eventcopy
161 or self::error or self::errorcopy)
162 and @name=$unqualified-type)
164 and @newname=$unqualified-type)]" />
166 <xsl:when test="count($type-definitions) = 1">
167 <xsl:for-each select="$type-definitions">
168 <xsl:text>XCB</xsl:text>
169 <xsl:value-of select="concat(/xcb/@extension-name,
170 $unqualified-type)" />
173 <xsl:when test="count($type-definitions) > 1">
174 <xsl:message terminate="yes">
175 <xsl:text>Multiple definitions of type "</xsl:text>
176 <xsl:value-of select="$type" />
177 <xsl:text>" found.</xsl:text>
178 <xsl:if test="$is-unqualified">
179 <xsl:for-each select="$type-definitions">
182 <xsl:value-of select="concat(/xcb/@header, ':', $type)" />
188 <xsl:message terminate="yes">
189 <xsl:text>No definitions of type "</xsl:text>
190 <xsl:value-of select="$type" />
191 <xsl:text>" found</xsl:text>
192 <xsl:if test="$is-unqualified">
193 <xsl:text>, and it is not a known core type</xsl:text>
195 <xsl:text>.</xsl:text>
203 <!-- Helper template for requests, that outputs the cookie type. The
204 parameter "request" must be the request node, which defaults to the
206 <xsl:template name="cookie-type">
207 <xsl:param name="request" select="." />
208 <xsl:text>XCB</xsl:text>
210 <xsl:when test="$request/reply">
211 <xsl:value-of select="concat($ext, $request/@name)" />
214 <xsl:text>Void</xsl:text>
217 <xsl:text>Cookie</xsl:text>
220 <xsl:template name="request-function">
221 <xsl:param name="checked" />
222 <xsl:param name="req" />
224 <xsl:attribute name="name">
225 <xsl:text>XCB</xsl:text>
226 <xsl:value-of select="$ext" />
227 <xsl:value-of select="$req/@name" />
228 <xsl:if test="$checked='true' and not($req/reply)">Checked</xsl:if>
229 <xsl:if test="$checked='false' and $req/reply">Unchecked</xsl:if>
231 <xsl:attribute name="type">
232 <xsl:call-template name="cookie-type">
233 <xsl:with-param name="request" select="$req" />
236 <field type="XCBConnection *" name="c" />
237 <xsl:apply-templates select="$req/*[not(self::reply)]" mode="param" />
238 <do-request ref="XCB{$ext}{$req/@name}Req" opcode="{$req/@opcode}"
239 checked="{$checked}">
240 <xsl:if test="$req/reply">
241 <xsl:attribute name="has-reply">true</xsl:attribute>
247 <xsl:template match="request" mode="pass1">
248 <xsl:variable name="req" select="." />
249 <xsl:if test="reply">
250 <struct name="XCB{$ext}{@name}Cookie">
251 <field type="unsigned int" name="sequence" />
254 <struct name="XCB{$ext}{@name}Req">
255 <field type="CARD8" name="major_opcode" no-assign="true" />
257 <field type="CARD8" name="minor_opcode" no-assign="true" />
259 <xsl:apply-templates select="*[not(self::reply)]" mode="field" />
261 <field type="CARD16" name="length" no-assign="true" />
264 <xsl:call-template name="request-function">
265 <xsl:with-param name="checked" select="'true'" />
266 <xsl:with-param name="req" select="$req" />
268 <xsl:call-template name="request-function">
269 <xsl:with-param name="checked" select="'false'" />
270 <xsl:with-param name="req" select="$req" />
272 <xsl:if test="reply">
273 <struct name="XCB{$ext}{@name}Rep">
274 <field type="BYTE" name="response_type" />
275 <xsl:apply-templates select="reply/*" mode="field" />
277 <field type="CARD16" name="sequence" />
278 <field type="CARD32" name="length" />
281 <iterator-functions ref="XCB{$ext}{@name}" kind="Rep" />
282 <function type="XCB{$ext}{@name}Rep *" name="XCB{$ext}{@name}Reply">
283 <field type="XCBConnection *" name="c" />
284 <field name="cookie">
285 <xsl:attribute name="type">
286 <xsl:call-template name="cookie-type" />
289 <field type="XCBGenericError **" name="e" />
290 <l>return (XCB<xsl:value-of select="concat($ext, @name)" />Rep *)<!--
291 --> XCBWaitForReply(c, cookie.sequence, e);</l>
296 <xsl:template match="xidtype" mode="pass1">
297 <struct name="XCB{$ext}{@name}">
298 <field type="CARD32" name="xid" />
300 <iterator ref="XCB{$ext}{@name}" />
301 <iterator-functions ref="XCB{$ext}{@name}" />
302 <function type="XCB{$ext}{@name}" name="XCB{$ext}{@name}New">
303 <field type="XCBConnection *" name="c" />
304 <l>XCB<xsl:value-of select="concat($ext, @name)" /> ret;</l>
305 <l>ret.xid = XCBGenerateID(c);</l>
310 <xsl:template match="struct|union" mode="pass1">
311 <struct name="XCB{$ext}{@name}">
312 <xsl:if test="self::union">
313 <xsl:attribute name="kind">union</xsl:attribute>
315 <xsl:apply-templates select="*" mode="field" />
317 <iterator ref="XCB{$ext}{@name}" />
318 <iterator-functions ref="XCB{$ext}{@name}" />
321 <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
322 <xsl:variable name="suffix">
324 <xsl:when test="self::event|self::eventcopy">
325 <xsl:text>Event</xsl:text>
327 <xsl:when test="self::error|self::errorcopy">
328 <xsl:text>Error</xsl:text>
332 <constant type="number" name="XCB{$ext}{@name}" value="{@number}" />
334 <xsl:when test="self::event|self::error">
335 <struct name="XCB{$ext}{@name}{$suffix}">
336 <field type="BYTE" name="response_type" />
337 <xsl:if test="self::error">
338 <field type="BYTE" name="error_code" />
340 <xsl:apply-templates select="*" mode="field" />
341 <xsl:if test="not(self::event and boolean(@no-sequence-number))">
343 <field type="CARD16" name="sequence" />
348 <xsl:when test="self::eventcopy|self::errorcopy">
349 <typedef newname="XCB{$ext}{@name}{$suffix}">
350 <xsl:attribute name="oldname">
351 <xsl:call-template name="canonical-type-name">
352 <xsl:with-param name="type" select="@ref" />
354 <xsl:value-of select="$suffix" />
361 <xsl:template match="typedef" mode="pass1">
363 <xsl:attribute name="oldname">
364 <xsl:call-template name="canonical-type-name">
365 <xsl:with-param name="type" select="@oldname" />
368 <xsl:attribute name="newname">
369 <xsl:call-template name="canonical-type-name">
370 <xsl:with-param name="type" select="@newname" />
374 <iterator ref="XCB{$ext}{@newname}" />
375 <iterator-functions ref="XCB{$ext}{@newname}" />
378 <xsl:template match="enum" mode="pass1">
379 <enum name="XCB{$ext}{@name}">
380 <xsl:for-each select="item">
381 <item name="XCB{$ext}{../@name}{@name}">
382 <xsl:copy-of select="*" />
389 Templates for processing fields.
392 <xsl:template match="pad" mode="field">
393 <xsl:copy-of select="." />
396 <xsl:template match="field|exprfield" mode="field">
398 <xsl:attribute name="type">
399 <xsl:call-template name="canonical-type-name" />
401 <xsl:attribute name="name">
402 <xsl:call-template name="canonical-var-name" />
404 <xsl:copy-of select="*" />
408 <xsl:template match="list" mode="field">
409 <xsl:variable name="type"><!--
410 --><xsl:call-template name="canonical-type-name" /><!--
412 <list type="{$type}">
413 <xsl:attribute name="name">
414 <xsl:call-template name="canonical-var-name" />
416 <xsl:if test="not(parent::request) and node()
417 and not(.//*[not(self::value or self::op)])">
418 <xsl:attribute name="fixed">true</xsl:attribute>
420 <!-- Handle lists with no length expressions. -->
421 <xsl:if test="not(node())">
423 <!-- In a request, refer to an implicit localparam for length. -->
424 <xsl:when test="parent::request">
426 <xsl:value-of select="concat(@name, '_len')" />
429 <!-- In a reply, use the length of the reply to determine the length
431 <xsl:when test="parent::reply">
434 <fieldref>length</fieldref>
437 <function-call name="sizeof">
438 <param><xsl:value-of select="$type" /></param>
442 <!-- Other cases generate an error. -->
444 <xsl:message terminate="yes"><!--
445 -->Encountered a list with no length expresssion outside a<!--
446 --> request or reply.<!--
451 <xsl:copy-of select="*" />
455 <xsl:template match="valueparam" mode="field">
457 <xsl:attribute name="type">
458 <xsl:call-template name="canonical-type-name">
459 <xsl:with-param name="type" select="@value-mask-type" />
462 <xsl:attribute name="name">
463 <xsl:call-template name="canonical-var-name">
464 <xsl:with-param name="name" select="@value-mask-name" />
469 <xsl:attribute name="name">
470 <xsl:call-template name="canonical-var-name">
471 <xsl:with-param name="name" select="@value-list-name" />
474 <function-call name="XCBPopcount">
477 <xsl:call-template name="canonical-var-name">
478 <xsl:with-param name="name" select="@value-mask-name" />
486 <xsl:template match="field|localfield" mode="param">
488 <xsl:attribute name="type">
489 <xsl:call-template name="canonical-type-name" />
491 <xsl:attribute name="name">
492 <xsl:call-template name="canonical-var-name" />
497 <xsl:template match="list" mode="param">
498 <!-- If no length expression is provided, use a CARD32 localfield. -->
499 <xsl:if test="not(node())">
500 <field type="CARD32" name="{@name}_len" />
503 <xsl:attribute name="type">
504 <xsl:text>const </xsl:text>
505 <xsl:call-template name="canonical-type-name" />
506 <xsl:text> *</xsl:text>
508 <xsl:attribute name="name">
509 <xsl:call-template name="canonical-var-name" />
514 <xsl:template match="valueparam" mode="param">
516 <xsl:attribute name="type">
517 <xsl:call-template name="canonical-type-name">
518 <xsl:with-param name="type" select="@value-mask-type" />
521 <xsl:attribute name="name">
522 <xsl:call-template name="canonical-var-name">
523 <xsl:with-param name="name" select="@value-mask-name" />
527 <field type="const CARD32 *">
528 <xsl:attribute name="name">
529 <xsl:call-template name="canonical-var-name">
530 <xsl:with-param name="name" select="@value-list-name" />
536 <!-- Second pass: Process the variable. -->
537 <xsl:variable name="result-rtf">
538 <xsl:apply-templates select="$pass1/*" mode="pass2" />
540 <xsl:variable name="result" select="e:node-set($result-rtf)" />
542 <xsl:template match="xcb" mode="pass2">
544 <xsl:copy-of select="@*" />
545 <xsl:apply-templates mode="pass2"
546 select="constant|enum|struct|typedef|iterator" />
547 <xsl:apply-templates mode="pass2"
548 select="function|iterator-functions" />
552 <!-- Generic rules for nodes that don't need further processing: copy node
553 with attributes, and recursively process the child nodes. -->
554 <xsl:template match="*" mode="pass2">
556 <xsl:copy-of select="@*" />
557 <xsl:apply-templates mode="pass2" />
561 <xsl:template match="struct" mode="pass2">
562 <xsl:if test="@kind='union' and list[not(@fixed)]">
563 <xsl:message terminate="yes">Unions must be fixed length.</xsl:message>
565 <struct name="{@name}">
566 <xsl:if test="@kind">
567 <xsl:attribute name="kind">
568 <xsl:value-of select="@kind" />
571 <!-- FIXME: This should go by size, not number of fields. -->
572 <xsl:copy-of select="node()[not(self::middle)
573 and position() < 3]" />
574 <xsl:if test="middle and (count(*[not(self::middle)]) < 2)">
575 <pad bytes="{2 - count(*[not(self::middle)])}" />
577 <xsl:copy-of select="middle/*" />
578 <xsl:copy-of select="node()[not(self::middle) and (position() > 2)]" />
582 <xsl:template match="do-request" mode="pass2">
583 <xsl:variable name="struct"
584 select="$pass1/xcb/struct[@name=current()/@ref]" />
586 <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
588 <l>static const XCBProtocolRequest xcb_req = {</l>
590 <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
591 <l>/* ext */ <xsl:choose>
592 <xsl:when test="$ext">
593 <xsl:text>&XCB</xsl:text>
594 <xsl:value-of select="$ext" />
595 <xsl:text>Id</xsl:text>
597 <xsl:otherwise>0</xsl:otherwise>
599 <l>/* opcode */ <xsl:value-of select="@opcode" />,</l>
600 <l>/* isvoid */ <xsl:value-of select="1-boolean(@has-reply)" /></l>
605 <l>struct iovec xcb_parts[<xsl:value-of select="$num-parts+2" />];</l>
606 <l><xsl:value-of select="../@type" /> xcb_ret;</l>
607 <l><xsl:value-of select="@ref" /> xcb_out;</l>
610 <xsl:apply-templates select="$struct//*[(self::field or self::exprfield)
611 and not(boolean(@no-assign))]"
615 <l>xcb_parts[2].iov_base = (char *) &xcb_out;</l>
616 <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
617 <l>xcb_parts[3].iov_base = 0;</l>
618 <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;</l>
620 <xsl:for-each select="$struct/list">
621 <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
622 --><xsl:value-of select="@name" />;</l>
623 <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
624 --><xsl:apply-templates mode="output-expression" /><!--
625 --><xsl:if test="not(@type = 'void')">
626 <xsl:text> * sizeof(</xsl:text>
627 <xsl:value-of select="@type" />
628 <xsl:text>)</xsl:text>
630 <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
631 <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_len = -xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len & 3;</l>
634 <l>xcb_ret.sequence = XCBSendRequest(c, <!--
636 <xsl:when test="@checked='true'">XCB_REQUEST_CHECKED</xsl:when>
637 <xsl:otherwise>0</xsl:otherwise>
638 </xsl:choose>, xcb_parts + 2, &xcb_req);</l>
639 <l>return xcb_ret;</l>
642 <xsl:template match="field" mode="assign">
644 <xsl:text>xcb_out.</xsl:text>
645 <xsl:value-of select="@name" />
646 <xsl:text> = </xsl:text>
647 <xsl:value-of select="@name" />
648 <xsl:text>;</xsl:text>
652 <xsl:template match="exprfield" mode="assign">
654 <xsl:text>xcb_out.</xsl:text>
655 <xsl:value-of select="@name" />
656 <xsl:text> = </xsl:text>
657 <xsl:apply-templates mode="output-expression" />
658 <xsl:text>;</xsl:text>
662 <xsl:template match="iterator" mode="pass2">
663 <struct name="{@ref}Iter">
664 <field type="{@ref} *" name="data" />
665 <field type="int" name="rem" />
666 <field type="int" name="index" />
670 <!-- Change a_name_like_this to ANameLikeThis. If the parameter name is not
671 given, it defaults to the name attribute of the context node. -->
672 <xsl:template name="capitalize">
673 <xsl:param name="name" select="string(@name)" />
674 <xsl:if test="$name">
675 <xsl:value-of select="translate(substring($name,1,1), $lcase, $ucase)" />
677 <xsl:when test="contains($name, '_')">
678 <xsl:value-of select="substring(substring-before($name, '_'), 2)" />
679 <xsl:call-template name="capitalize">
680 <xsl:with-param name="name" select="substring-after($name, '_')" />
684 <xsl:value-of select="substring($name, 2)" />
690 <xsl:template match="iterator-functions" mode="pass2">
691 <xsl:variable name="ref" select="@ref" />
692 <xsl:variable name="kind" select="@kind" />
693 <xsl:variable name="struct"
694 select="$pass1/xcb/struct[@name=concat($ref,$kind)]" />
695 <xsl:variable name="nextfields-rtf">
696 <nextfield>R + 1</nextfield>
697 <xsl:for-each select="$struct/list[not(@fixed)]">
699 <xsl:when test="substring(@type, 1, 3) = 'XCB'">
700 <nextfield><xsl:value-of select="@type" />End(<!--
701 --><xsl:value-of select="$ref" /><!--
702 --><xsl:call-template name="capitalize" />Iter(R))</nextfield>
705 <nextfield><xsl:value-of select="$ref" /><!--
706 --><xsl:call-template name="capitalize" />End(R)</nextfield>
711 <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
712 <xsl:for-each select="$struct/list[not(@fixed)]">
713 <xsl:variable name="number"
714 select="1+count(preceding-sibling::list[not(@fixed)])" />
715 <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
716 <xsl:variable name="is-first"
717 select="not(preceding-sibling::list[not(@fixed)])" />
718 <xsl:variable name="field-name"><!--
719 --><xsl:call-template name="capitalize" /><!--
721 <xsl:variable name="is-variable"
722 select="$pass1/xcb/struct[@name=current()/@type]/list
723 or document($search-path)/xcb
724 /struct[concat('XCB',
725 ancestor::xcb/@extension-name,
726 @name) = current()/@type]
727 /*[self::valueparam or self::list]" />
728 <xsl:if test="not($is-variable)">
729 <function type="{@type} *" name="{$ref}{$field-name}">
730 <field type="const {$ref}{$kind} *" name="R" />
732 <xsl:when test="$is-first">
733 <l>return (<xsl:value-of select="@type" /> *) <!--
734 -->(<xsl:value-of select="$nextfield" />);</l>
737 <l>XCBGenericIter prev = <!--
738 --><xsl:value-of select="$nextfield" />;</l>
739 <l>return (<xsl:value-of select="@type" /> *) <!--
740 -->((char *) prev.data + XCB_TYPE_PAD(<!--
741 --><xsl:value-of select="@type" />, prev.index));</l>
746 <function type="int" name="{$ref}{$field-name}Length">
747 <field type="const {$ref}{$kind} *" name="R" />
748 <l>return <xsl:apply-templates mode="output-expression">
749 <xsl:with-param name="field-prefix" select="'R->'" />
750 </xsl:apply-templates>;</l>
753 <xsl:when test="substring(@type, 1, 3) = 'XCB'">
754 <function type="{@type}Iter" name="{$ref}{$field-name}Iter">
755 <field type="const {$ref}{$kind} *" name="R" />
756 <l><xsl:value-of select="@type" />Iter i;</l>
758 <xsl:when test="$is-first">
759 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
760 -->(<xsl:value-of select="$nextfield" />);</l>
763 <l>XCBGenericIter prev = <!--
764 --><xsl:value-of select="$nextfield" />;</l>
765 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
766 -->((char *) prev.data + XCB_TYPE_PAD(<!--
767 --><xsl:value-of select="@type" />, prev.index));</l>
770 <l>i.rem = <xsl:apply-templates mode="output-expression">
771 <xsl:with-param name="field-prefix" select="'R->'" />
772 </xsl:apply-templates>;</l>
773 <l>i.index = (char *) i.data - (char *) R;</l>
778 <xsl:variable name="cast">
780 <xsl:when test="@type='void'">char</xsl:when>
781 <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
784 <function type="XCBGenericIter" name="{$ref}{$field-name}End">
785 <field type="const {$ref}{$kind} *" name="R" />
786 <l>XCBGenericIter i;</l>
788 <xsl:when test="$is-first">
789 <l>i.data = ((<xsl:value-of select="$cast" /> *) <!--
790 -->(<xsl:value-of select="$nextfield" />)) + (<!--
791 --><xsl:apply-templates mode="output-expression">
792 <xsl:with-param name="field-prefix" select="'R->'" />
793 </xsl:apply-templates>);</l>
796 <l>XCBGenericIter child = <!--
797 --><xsl:value-of select="$nextfield" />;</l>
798 <l>i.data = ((<xsl:value-of select="$cast" /> *) <!--
799 -->child.data) + (<!--
800 --><xsl:apply-templates mode="output-expression">
801 <xsl:with-param name="field-prefix" select="'R->'" />
802 </xsl:apply-templates>);</l>
806 <l>i.index = (char *) i.data - (char *) R;</l>
812 <xsl:if test="not($kind)">
813 <function type="void" name="{$ref}Next">
814 <field type="{$ref}Iter *" name="i" />
816 <xsl:when test="$struct/list[not(@fixed)]">
817 <l><xsl:value-of select="$ref" /> *R = i->data;</l>
818 <l>XCBGenericIter child = <!--
819 --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
821 <l>i->data = (<xsl:value-of select="$ref" /> *) child.data;</l>
822 <l>i->index = child.index;</l>
827 <l>i->index += sizeof(<xsl:value-of select="$ref" />);</l>
831 <function type="XCBGenericIter" name="{$ref}End">
832 <field type="{$ref}Iter" name="i" />
833 <l>XCBGenericIter ret;</l>
835 <xsl:when test="$struct/list[not(@fixed)]">
836 <l>while(i.rem > 0)</l>
838 <l><xsl:value-of select="$ref" />Next(&i);</l>
840 <l>ret.data = i.data;</l>
841 <l>ret.rem = i.rem;</l>
842 <l>ret.index = i.index;</l>
845 <l>ret.data = i.data + i.rem;</l>
846 <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
855 <!-- Output the results. -->
856 <xsl:template match="/">
857 <xsl:if test="not(function-available('e:node-set'))">
858 <xsl:message terminate="yes"><!--
859 -->Error: This stylesheet requires the EXSL node-set extension.<!--
863 <xsl:if test="not($h) and not($c)">
864 <xsl:message terminate="yes"><!--
865 -->Error: Parameter "mode" must be "header" or "source".<!--
869 <xsl:apply-templates select="$result/*" mode="output" />
872 <xsl:template match="xcb" mode="output">
873 <xsl:variable name="guard"><!--
874 -->__<xsl:value-of select="$ucase-header" />_H<!--
878 * This file generated automatically from </xsl:text>
879 <xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
880 * Edit at your peril.
884 <xsl:if test="$h"><xsl:text>
885 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
886 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
889 <xsl:for-each select="$root/xcb/import">
890 <xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
897 <xsl:if test="$c"><xsl:text>
898 #include <assert.h>
900 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
904 <xsl:apply-templates mode="output" />
913 <xsl:template match="constant" mode="output">
915 <xsl:when test="@type = 'number'">
917 <xsl:text>#define </xsl:text>
918 <xsl:value-of select="@name" />
919 <xsl:text> </xsl:text>
920 <xsl:value-of select="@value" />
926 <xsl:when test="@type = 'string'">
928 <xsl:text>extern </xsl:text>
930 <xsl:text>const char </xsl:text>
931 <xsl:value-of select="@name" />
932 <xsl:text>[]</xsl:text>
934 <xsl:text> = "</xsl:text>
935 <xsl:value-of select="@value" />
936 <xsl:text>"</xsl:text>
944 <xsl:text>extern </xsl:text>
946 <xsl:call-template name="type-and-name" />
948 <xsl:text> = </xsl:text>
949 <xsl:value-of select="@value" />
958 <xsl:template match="typedef" mode="output">
960 <xsl:text>typedef </xsl:text>
961 <xsl:value-of select="@oldname" />
962 <xsl:text> </xsl:text>
963 <xsl:value-of select="@newname" />
970 <xsl:template match="struct" mode="output">
972 <xsl:variable name="type-lengths">
973 <xsl:call-template name="type-lengths">
974 <xsl:with-param name="items" select="field/@type" />
977 <xsl:text>typedef </xsl:text>
978 <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
981 <xsl:for-each select="exprfield|field|list[@fixed]|pad">
982 <xsl:text> </xsl:text>
983 <xsl:apply-templates select=".">
984 <xsl:with-param name="type-lengths" select="$type-lengths" />
985 </xsl:apply-templates>
989 <xsl:text>} </xsl:text>
990 <xsl:value-of select="@name" />
997 <xsl:template match="enum" mode="output">
999 <xsl:text>typedef enum {
1001 <xsl:call-template name="list">
1002 <xsl:with-param name="separator"><xsl:text>,
1003 </xsl:text></xsl:with-param>
1004 <xsl:with-param name="items">
1005 <xsl:for-each select="item">
1007 <xsl:value-of select="@name" />
1008 <xsl:if test="node()"> <!-- If there is an expression -->
1009 <xsl:text> = </xsl:text>
1010 <xsl:apply-templates mode="output-expression" />
1015 </xsl:call-template>
1017 } </xsl:text><xsl:value-of select="@name" /><xsl:text>;
1023 <xsl:template match="function" mode="output">
1024 <xsl:variable name="decl-open" select="concat(@name, ' (')" />
1025 <xsl:variable name="type-lengths">
1026 <xsl:call-template name="type-lengths">
1027 <xsl:with-param name="items" select="field/@type" />
1028 </xsl:call-template>
1030 <xsl:value-of select="@type" />
1033 <xsl:value-of select="$decl-open" />
1034 <xsl:call-template name="list">
1035 <xsl:with-param name="separator">
1038 <xsl:call-template name="repeat">
1039 <xsl:with-param name="count" select="string-length($decl-open)" />
1040 </xsl:call-template>
1042 <xsl:with-param name="items">
1043 <xsl:for-each select="field">
1045 <xsl:apply-templates select=".">
1046 <xsl:with-param name="type-lengths" select="$type-lengths" />
1047 </xsl:apply-templates>
1051 </xsl:call-template>
1052 <xsl:text>)</xsl:text>
1054 <xsl:if test="$h"><xsl:text>;
1056 </xsl:text></xsl:if>
1062 <xsl:apply-templates select="l|indent" mode="function-body">
1063 <xsl:with-param name="indent" select="$indent-string" />
1064 </xsl:apply-templates>
1071 <xsl:template match="l" mode="function-body">
1072 <xsl:param name="indent" />
1073 <xsl:value-of select="concat($indent, .)" /><xsl:text>
1077 <xsl:template match="indent" mode="function-body">
1078 <xsl:param name="indent" />
1079 <xsl:apply-templates select="l|indent" mode="function-body">
1080 <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
1081 </xsl:apply-templates>
1084 <xsl:template match="value" mode="output-expression">
1085 <xsl:value-of select="." />
1088 <xsl:template match="fieldref" mode="output-expression">
1089 <xsl:param name="field-prefix" />
1090 <xsl:value-of select="concat($field-prefix, .)" />
1093 <xsl:template match="op" mode="output-expression">
1094 <xsl:param name="field-prefix" />
1095 <xsl:text>(</xsl:text>
1096 <xsl:apply-templates select="node()[1]" mode="output-expression">
1097 <xsl:with-param name="field-prefix" select="$field-prefix" />
1098 </xsl:apply-templates>
1099 <xsl:text> </xsl:text>
1100 <xsl:value-of select="@op" />
1101 <xsl:text> </xsl:text>
1102 <xsl:apply-templates select="node()[2]" mode="output-expression">
1103 <xsl:with-param name="field-prefix" select="$field-prefix" />
1104 </xsl:apply-templates>
1105 <xsl:text>)</xsl:text>
1108 <xsl:template match="bit" mode="output-expression">
1109 <xsl:text>(1 << </xsl:text>
1110 <xsl:value-of select="." />
1111 <xsl:text>)</xsl:text>
1114 <xsl:template match="function-call" mode="output-expression">
1115 <xsl:param name="field-prefix" />
1116 <xsl:value-of select="@name" />
1117 <xsl:text>(</xsl:text>
1118 <xsl:call-template name="list">
1119 <xsl:with-param name="separator" select="', '" />
1120 <xsl:with-param name="items">
1121 <xsl:for-each select="param">
1122 <item><xsl:apply-templates mode="output-expression">
1123 <xsl:with-param name="field-prefix" select="$field-prefix" />
1124 </xsl:apply-templates></item>
1127 </xsl:call-template>
1128 <xsl:text>)</xsl:text>
1131 <!-- Catch invalid elements in expressions. -->
1132 <xsl:template match="*" mode="output-expression">
1133 <xsl:message terminate="yes">
1134 <xsl:text>Invalid element in expression: </xsl:text>
1135 <xsl:value-of select="name()" />
1139 <xsl:template match="field|exprfield">
1140 <xsl:param name="type-lengths" select="0" />
1141 <xsl:call-template name="type-and-name">
1142 <xsl:with-param name="type-lengths" select="$type-lengths" />
1143 </xsl:call-template>
1146 <xsl:template match="list[@fixed]">
1147 <xsl:param name="type-lengths" select="0" />
1148 <xsl:call-template name="type-and-name">
1149 <xsl:with-param name="type-lengths" select="$type-lengths" />
1150 </xsl:call-template>
1151 <xsl:text>[</xsl:text>
1152 <xsl:apply-templates mode="output-expression" />
1153 <xsl:text>]</xsl:text>
1156 <xsl:template match="pad">
1157 <xsl:param name="type-lengths" select="0" />
1159 <xsl:variable name="padnum"><xsl:number /></xsl:variable>
1161 <xsl:call-template name="type-and-name">
1162 <xsl:with-param name="type" select="'CARD8'" />
1163 <xsl:with-param name="name">
1164 <xsl:text>pad</xsl:text>
1165 <xsl:value-of select="$padnum - 1" />
1167 <xsl:with-param name="type-lengths" select="$type-lengths" />
1168 </xsl:call-template>
1169 <xsl:if test="@bytes > 1">
1170 <xsl:text>[</xsl:text>
1171 <xsl:value-of select="@bytes" />
1172 <xsl:text>]</xsl:text>
1176 <!-- Output the given type and name (defaulting to the corresponding
1177 attributes of the context node), with the appropriate spacing. The
1178 type must consist of a base type (which may contain spaces), then
1179 optionally a single space and a suffix of one or more '*' characters.
1180 If the type-lengths parameter is provided, use it to line up the base
1181 types and suffixs of the type declarations. -->
1182 <xsl:template name="type-and-name">
1183 <xsl:param name="type" select="@type" />
1184 <xsl:param name="name" select="@name" />
1185 <xsl:param name="type-lengths">
1186 <max-type-length>0</max-type-length>
1187 <max-suffix-length>0</max-suffix-length>
1190 <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
1191 <xsl:variable name="min-type-length"
1192 select="$type-lengths-ns/max-type-length" />
1193 <xsl:variable name="min-suffix-length"
1194 select="$type-lengths-ns/max-suffix-length" />
1196 <xsl:variable name="base-type">
1198 <xsl:when test="contains($type, ' *')">
1199 <xsl:value-of select="substring-before($type, ' *')" />
1202 <xsl:value-of select="$type" />
1206 <xsl:variable name="suffix">
1207 <xsl:if test="contains($type, ' *')">
1208 <xsl:text>*</xsl:text>
1209 <xsl:value-of select="substring-after($type, ' *')" />
1213 <xsl:value-of select="$base-type" />
1214 <xsl:if test="string-length($base-type) < $min-type-length">
1215 <xsl:call-template name="repeat">
1216 <xsl:with-param name="count" select="$min-type-length
1217 - string-length($base-type)" />
1218 </xsl:call-template>
1220 <xsl:text> </xsl:text>
1221 <xsl:if test="string-length($suffix) < $min-suffix-length">
1222 <xsl:call-template name="repeat">
1223 <xsl:with-param name="count" select="$min-suffix-length
1224 - string-length($suffix)" />
1225 </xsl:call-template>
1227 <xsl:value-of select="$suffix" />
1228 <xsl:value-of select="$name" />
1231 <!-- Output a list with a given separator. Empty items are skipped. -->
1232 <xsl:template name="list">
1233 <xsl:param name="separator" />
1234 <xsl:param name="items" />
1236 <xsl:for-each select="e:node-set($items)/*">
1237 <xsl:value-of select="." />
1238 <xsl:if test="not(position() = last())">
1239 <xsl:value-of select="$separator" />
1244 <!-- Repeat a string (space by default) a given number of times. -->
1245 <xsl:template name="repeat">
1246 <xsl:param name="str" select="' '" />
1247 <xsl:param name="count" />
1249 <xsl:if test="$count > 0">
1250 <xsl:value-of select="$str" />
1251 <xsl:call-template name="repeat">
1252 <xsl:with-param name="str" select="$str" />
1253 <xsl:with-param name="count" select="$count - 1" />
1254 </xsl:call-template>
1258 <!-- Record the maximum type lengths of a set of types for use as the
1259 max-type-lengths parameter of type-and-name. -->
1260 <xsl:template name="type-lengths">
1261 <xsl:param name="items" />
1262 <xsl:variable name="type-lengths-rtf">
1263 <xsl:for-each select="$items">
1266 <xsl:when test="contains(., ' *')">
1267 <xsl:value-of select="string-length(
1268 substring-before(., ' *'))" />
1271 <xsl:value-of select="string-length(.)" />
1277 <xsl:variable name="suffix-lengths-rtf">
1278 <xsl:for-each select="$items">
1281 <xsl:when test="contains(., ' *')">
1282 <xsl:value-of select="string-length(substring-after(., ' *'))
1286 <xsl:text>0</xsl:text>
1293 <xsl:call-template name="max">
1294 <xsl:with-param name="items"
1295 select="e:node-set($type-lengths-rtf)/*" />
1296 </xsl:call-template>
1299 <xsl:call-template name="max">
1300 <xsl:with-param name="items"
1301 select="e:node-set($suffix-lengths-rtf)/*" />
1302 </xsl:call-template>
1303 </max-suffix-length>
1306 <!-- Return the maximum number in a set of numbers. -->
1307 <xsl:template name="max">
1308 <xsl:param name="items" />
1310 <xsl:when test="count($items) = 0">
1311 <xsl:text>0</xsl:text>
1314 <xsl:variable name="head" select="number($items[1])" />
1315 <xsl:variable name="tail-max">
1316 <xsl:call-template name="max">
1317 <xsl:with-param name="items" select="$items[position() > 1]" />
1318 </xsl:call-template>
1321 <xsl:when test="$head > number($tail-max)">
1322 <xsl:value-of select="$head" />
1325 <xsl:value-of select="$tail-max" />