3 # (C) Copyright IBM Corporation 2004, 2005
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 # Ian Romanick <idr@us.ibm.com>
28 from xml
.sax
import saxutils
29 from xml
.sax
import make_parser
30 from xml
.sax
.handler
import feature_namespaces
38 class glXDocItemFactory(glX_XML
.glXItemFactory
):
39 """Factory to create GLX protocol documentation oriented objects derived from glItem."""
41 def create(self
, context
, name
, attrs
):
43 return glXDocParameter(context
, name
, attrs
)
45 return glX_XML
.glXItemFactory
.create(self
, context
, name
, attrs
)
47 class glXDocParameter(gl_XML
.glParameter
):
48 def __init__(self
, context
, name
, attrs
):
50 gl_XML
.glParameter
.__init
__(self
, context
, name
, attrs
);
53 def packet_type(self
):
54 """Get the type string for the packet header
56 GLX protocol documentation uses type names like CARD32,
57 FLOAT64, LISTofCARD8, and ENUM. This function converts the
58 type of the parameter to one of these names."""
64 if self
.p_type
.glx_name
== "":
67 type_name
= self
.p_type
.glx_name
69 return "%s%s" % (list_of
, type_name
)
71 def packet_size(self
):
76 b_prod
= self
.p_type
.size
78 if not self
.count_parameter_list
and self
.counter
:
80 elif self
.count_parameter_list
and not self
.counter
or self
.is_output
:
82 elif self
.count_parameter_list
and self
.counter
:
85 raise RuntimeError("Parameter '%s' to function '%s' has size 0." % (self
.name
, self
.context
.name
))
87 ss
= "%s*%s" % (a_prod
, b_prod
)
96 class PrintGlxProtoText(glX_XML
.GlxProto
):
98 glX_XML
.GlxProto
.__init
__(self
)
99 self
.factory
= glXDocItemFactory()
100 self
.last_category
= ""
103 def printHeader(self
):
106 def body_size(self
, f
):
107 # At some point, refactor this function and
108 # glXFunction::command_payload_length.
114 for p
in f
.parameterIterator(1, 2):
115 [s
, pad
] = p
.packet_size()
119 size_str
+= "%s%s" % (plus
, s
)
125 return [size
, size_str
, pad_str
]
127 def print_render_header(self
, f
):
128 [size
, size_str
, pad_str
] = self
.body_size(f
)
132 s
= "%u" % ((size
+ 3) & ~
3)
134 s
= "%u+%s+%s" % (size
, size_str
, pad_str
)
136 s
= "%u+%s" % (size
, size_str
)
138 print ' 2 %-15s rendering command length' % (s
)
139 print ' 2 %-4u rendering command opcode' % (f
.glx_rop
)
143 def print_single_header(self
, f
):
144 [size
, size_str
, pad_str
] = self
.body_size(f
)
145 size
= ((size
+ 3) / 4) + 2;
147 if f
.glx_vendorpriv
!= 0:
150 print ' 1 CARD8 opcode (X assigned)'
151 print ' 1 %-4u GLX opcode (%s)' % (f
.opcode_real_value(), f
.opcode_real_name())
156 s
= "%u+((%s+%s)/4)" % (size
, size_str
, pad_str
)
158 s
= "%u+((%s)/4)" % (size
, size_str
)
160 print ' 2 %-15s request length' % (s
)
162 if f
.glx_vendorpriv
!= 0:
163 print ' 4 %-4u vendor specific opcode' % (f
.opcode_value())
165 print ' 4 GLX_CONTEXT_TAG context tag'
169 def print_reply(self
, f
):
173 print ' 2 CARD16 sequence number'
176 print ' 4 0 reply length'
177 elif f
.reply_always_array
:
178 print ' 4 m reply length'
180 print ' 4 m reply length, m = (n == 1 ? 0 : n)'
184 if f
.fn_return_type
!= 'void':
185 print ' 4 %-15s return value' % (f
.fn_return_type
)
187 elif f
.output
!= None:
196 if not f
.reply_always_array
:
198 print ' if (n = 1) this follows:'
200 print ' 4 CARD32 %s' % (f
.output
.name
)
201 print ' %-2u unused' % (unused
- 4)
203 print ' otherwise this follows:'
206 print ' %-2u unused' % (unused
)
208 [s
, pad
] = p
.packet_size()
209 print ' %-8s %-15s %s' % (s
, p
.packet_type(), p
.name
)
213 bytes
= 4 - (bytes
& 3)
214 print ' %-8u %-15s unused' % (bytes
, "")
216 print ' %-8s %-15s unused, %s=pad(%s)' % (pad
, "", pad
, s
)
218 print ' %-2u unused' % (unused
)
221 def print_body(self
, f
):
222 for p
in f
.parameterIterator(1, 2):
223 [s
, pad
] = p
.packet_size()
224 print ' %-8s %-15s %s' % (s
, p
.packet_type(), p
.name
)
228 bytes
= 4 - (bytes
& 3)
229 print ' %-8u %-15s unused' % (bytes
, "")
231 print ' %-8s %-15s unused, %s=pad(%s)' % (pad
, "", pad
, s
)
233 def printFunction(self
, f
):
234 # At some point this should be expanded to support pixel
235 # functions, but I'm not going to lose any sleep over it now.
237 if f
.client_handcode
or f
.server_handcode
or f
.vectorequiv
or f
.image
:
240 print ' %s' % (f
.name
)
243 self
.print_render_header(f
)
245 self
.print_single_header(f
)
256 if __name__
== '__main__':
257 file_name
= "gl_API.xml"
260 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:")
264 for (arg
,val
) in args
:
268 dh
= PrintGlxProtoText()
270 parser
= make_parser()
271 parser
.setFeature(feature_namespaces
, 1)
272 parser
.setContentHandler(dh
)