<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2004 Josh Triplett.  All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of the authors or their
institutions shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Software without prior written
authorization from the authors.
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <!-- The root element -->
  <xsd:element name="xcb">
    <xsd:complexType>
      <xsd:group ref="macro" minOccurs="0" maxOccurs="unbounded" />
      <xsd:attribute name="header" type="xsd:string" use="required" />
      <xsd:attribute name="extension-xname" type="xsd:string" use="optional" />
      <xsd:attribute name="extension-name"  type="xsd:string" use="optional" />
      <xsd:attribute name="major-version"   type="xsd:integer" use="optional" />
      <xsd:attribute name="minor-version"   type="xsd:integer" use="optional" />
    </xsd:complexType>
  </xsd:element>

  <!-- Padding -->
  <xsd:element name="pad">
    <xsd:complexType>
      <xsd:attribute name="bytes" type="xsd:integer" use="required" />
    </xsd:complexType>
  </xsd:element>

  <!-- Type for fields or parameters with attributes "name" and "type" -->
  <xsd:complexType name="var">
    <xsd:attribute name="name" type="xsd:string" use="required" />
    <xsd:attribute name="type" type="xsd:string" use="required" />
  </xsd:complexType>

  <!-- field replaces FIELD, PARAM, and REPLY. -->
  <xsd:element name="field" type="var" />

  <!-- list replaces ARRAYFIELD, LISTPARAM, and ARRAYREPLY.  The name and type
       are specified as attributes.  The content is an expression giving the
       length. -->
  <xsd:element name="list">
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="var">
          <xsd:group ref="expression" minOccurs="0" maxOccurs="1" />
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>

  <!-- Expressions -->
  <xsd:group name="expression">
    <xsd:choice>
      <xsd:element name="op">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:group ref="expression" />
            <xsd:group ref="expression" />
          </xsd:sequence>
          <xsd:attribute name="op" use="required">
            <xsd:simpleType>
              <xsd:restriction base="xsd:string">
                <xsd:pattern value="\+|-|\*|/|&amp;|&lt;&lt;" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:attribute>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="fieldref" type="xsd:string" />
      <xsd:element name="value" type="dec-or-hex-integer" />
      <xsd:element name="bit" type="xsd:integer" />
    </xsd:choice>
  </xsd:group>
  
  <!-- Fields in requests that are calculated from other information, not
       supplied by the caller. -->
  <xsd:element name="exprfield" >
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="var">
          <xsd:group ref="expression" />
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>

  <!-- BITMASK/LISTofVALUE parameter pairs. -->
  <xsd:element name="valueparam">
    <xsd:complexType>
      <xsd:attribute name="value-mask-type" type="xsd:string" use="required" />
      <xsd:attribute name="value-mask-name" type="xsd:string" use="required" />
      <xsd:attribute name="value-list-name" type="xsd:string" use="required" />
    </xsd:complexType>
  </xsd:element>

  <xsd:group name="fields">
    <xsd:choice>
      <xsd:element ref="pad" />
      <xsd:element ref="field" />
      <xsd:element ref="list" />
    </xsd:choice>
  </xsd:group>

  <!-- Type for a structure -->
  <xsd:complexType name="struct">
    <xsd:group ref="fields" minOccurs="1" maxOccurs="unbounded" />
    <xsd:attribute name="name" type="xsd:string" use="required" />
  </xsd:complexType>

  <!-- Type for a packet structure -->
  <xsd:complexType name="packet-struct">
    <xsd:group ref="fields" minOccurs="0" maxOccurs="unbounded" />
    <xsd:attribute name="name" type="xsd:string" use="required" />
    <xsd:attribute name="number" type="xsd:integer" use="required" />
  </xsd:complexType>

  <!-- Type for a packet structure copy -->
  <xsd:complexType name="packet-struct-copy">
    <xsd:attribute name="name" type="xsd:string" use="required" />
    <xsd:attribute name="number" type="xsd:integer" use="required" />
    <xsd:attribute name="ref" type="xsd:string" use="required" />
  </xsd:complexType>

  <!-- Type for hex integers -->
  <xsd:simpleType name="hex-integer">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="0x[0-9a-fA-F]+" />
    </xsd:restriction>
  </xsd:simpleType>

  <!-- Type for integers in either decimal or hex -->
  <xsd:simpleType name="dec-or-hex-integer">
    <xsd:union memberTypes="xsd:integer hex-integer" />
  </xsd:simpleType>

  <xsd:group name="macro">
    <xsd:choice>
      <xsd:element name="request">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
              <xsd:group ref="fields" />
              <xsd:element ref="exprfield" />
              <xsd:element ref="valueparam" />
            </xsd:choice>
            <xsd:element name="reply" minOccurs="0" maxOccurs="1">
              <xsd:complexType>
                <xsd:choice minOccurs="1" maxOccurs="unbounded">
                  <xsd:group ref="fields" />
                  <xsd:element ref="valueparam" />
                </xsd:choice>
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name="name" type="xsd:string" use="required" />
          <xsd:attribute name="opcode" type="xsd:integer" use="required" />
          <xsd:attribute name="combine-adjacent" type="xsd:boolean"
                         use="optional"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="event">
        <xsd:complexType>
          <xsd:complexContent>
            <xsd:extension base="packet-struct">
              <xsd:attribute name="no-sequence-number" type="xsd:boolean"
                             use="optional" />
            </xsd:extension>
          </xsd:complexContent>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="eventcopy" type="packet-struct-copy" />
      <xsd:element name="error" type="packet-struct" />
      <xsd:element name="errorcopy" type="packet-struct-copy" />
      <xsd:element name="struct" type="struct" />
      <xsd:element name="union" type="struct" />
      <xsd:element name="xidtype">
        <xsd:complexType>
          <xsd:attribute name="name" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="xidunion">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="type" type="xsd:string"
                         minOccurs="1" maxOccurs="unbounded" />
          </xsd:sequence>
          <xsd:attribute name="name" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="enum">
        <xsd:complexType>
          <xsd:sequence minOccurs="1" maxOccurs="unbounded">
            <xsd:element name="item">
              <xsd:complexType>
                <xsd:group ref="expression" minOccurs="0" maxOccurs="1" />
                <xsd:attribute name="name" type="xsd:string" use="required" />
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name="name" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="typedef">
        <xsd:complexType>
          <xsd:attribute name="oldname" type="xsd:string" use="required" />
          <xsd:attribute name="newname" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
      <!-- The import element allows a protocol description to reference the
           declarations of another protocol description. -->
      <xsd:element name="import" type="xsd:string" />
    </xsd:choice>
  </xsd:group>
</xsd:schema>
