2 # -*- Mode: Python; py-indent-offset: 8 -*-
4 # (C) Copyright Zack Rusin 2005
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # on the rights to use, copy, modify, merge, publish, distribute, sub
11 # license, and/or sell copies of the Software, and to permit persons to whom
12 # the Software is furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 # Zack Rusin <zack@kde.org>
33 class PrintGlEnums(gl_XML
.gl_print_base
):
36 gl_XML
.gl_print_base
.__init
__(self
)
38 self
.name
= "gl_enums.py (from Mesa)"
39 self
.license
= license
.bsd_license_template
% ( \
40 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
44 def printRealHeader(self
):
45 print '#include "main/glheader.h"'
46 print '#include "main/enums.h"'
47 print '#include "main/imports.h"'
48 print '#include "main/mtypes.h"'
50 print 'typedef struct PACKED {'
51 print ' uint16_t offset;'
59 typedef int (*cfunc)(const void *, const void *);
62 * Compare a key enum value to an element in the \c enum_string_table_offsets array.
64 * \c bsearch always passes the key as the first parameter and the pointer
65 * to the array element as the second parameter. We can elimiate some
66 * extra work by taking advantage of that fact.
68 * \param a Pointer to the desired enum name.
69 * \param b Pointer into the \c enum_string_table_offsets array.
71 static int compar_nr( const int *a, enum_elt *b )
77 static char token_tmp[20];
79 const char *_mesa_lookup_enum_by_nr( int nr )
83 STATIC_ASSERT(sizeof(enum_string_table) < (1 << 16));
85 elt = bsearch(& nr, enum_string_table_offsets,
86 ARRAY_SIZE(enum_string_table_offsets),
87 sizeof(enum_string_table_offsets[0]),
91 return &enum_string_table[elt->offset];
94 /* this is not re-entrant safe, no big deal here */
95 _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
96 token_tmp[sizeof(token_tmp) - 1] = '\\0';
104 static const char *prim_names[PRIM_MAX+3] = {
115 "GL_LINES_ADJACENCY",
116 "GL_LINE_STRIP_ADJACENCY",
117 "GL_TRIANGLES_ADJACENCY",
118 "GL_TRIANGLE_STRIP_ADJACENCY",
124 /* Get the name of an enum given that it is a primitive type. Avoids
125 * GL_FALSE/GL_POINTS ambiguity and others.
128 _mesa_lookup_prim_by_nr(GLuint nr)
130 if (nr < ARRAY_SIZE(prim_names))
131 return prim_names[nr];
133 return "invalid mode";
141 def printBody(self
, api_list
):
144 self
.process_enums( api
)
148 for enum
in sorted(self
.enum_table
.keys()):
151 for [name
, pri
] in self
.enum_table
[ enum
]:
156 enum_table
.append((enum
, best_name
))
160 print '#if defined(__GNUC__)'
161 print '# define LONGSTRING __extension__'
163 print '# define LONGSTRING'
166 print 'LONGSTRING static const char enum_string_table[] = '
167 for enum
, name
in enum_table
:
168 print ' "%s\\0"' % (name
)
169 string_offsets
[ enum
] = i
176 print 'static const enum_elt enum_string_table_offsets[%u] =' % (len(enum_table
))
178 for enum
, name
in enum_table
:
179 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets
[enum
], enum
, name
)
187 def process_enums(self
, api
):
188 for obj
in api
.enumIterateByName():
189 if obj
.value
not in self
.enum_table
:
190 self
.enum_table
[ obj
.value
] = []
193 enum
= self
.enum_table
[ obj
.value
]
194 name
= "GL_" + obj
.name
195 priority
= obj
.priority()
201 enum
.append( [name
, priority
] )
205 print "Usage: %s [-f input_file_name]" % sys
.argv
[0]
208 if __name__
== '__main__':
210 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "f:")
215 for (arg
,val
) in args
:
217 api
= gl_XML
.parse_GL_API( val
)
218 api_list
.append(api
);
220 printer
= PrintGlEnums()
221 printer
.Print( api_list
)