Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #!/usr/bin/python2
  2. # -*- Mode: Python; py-indent-offset: 8 -*-
  3.  
  4. # (C) Copyright Zack Rusin 2005
  5. # All Rights Reserved.
  6. #
  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:
  13. #
  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
  16. # Software.
  17. #
  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
  24. # IN THE SOFTWARE.
  25. #
  26. # Authors:
  27. #    Zack Rusin <zack@kde.org>
  28.  
  29. import license
  30. import gl_XML
  31. import sys, getopt
  32.  
  33. class PrintGlEnums(gl_XML.gl_print_base):
  34.  
  35.         def __init__(self):
  36.                 gl_XML.gl_print_base.__init__(self)
  37.  
  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")
  41.                 self.enum_table = {}
  42.  
  43.  
  44.         def printRealHeader(self):
  45.                 print '#include "main/glheader.h"'
  46.                 print '#include "main/mfeatures.h"'
  47.                 print '#include "main/enums.h"'
  48.                 print '#include "main/imports.h"'
  49.                 print ''
  50.                 print 'typedef struct {'
  51.                 print '   size_t offset;'
  52.                 print '   int n;'
  53.                 print '} enum_elt;'
  54.                 print ''
  55.                 return
  56.  
  57.         def print_code(self):
  58.                 print """
  59. typedef int (*cfunc)(const void *, const void *);
  60.  
  61. /**
  62. * Compare a key name to an element in the \c all_enums array.
  63. *
  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.
  67. *
  68. * \param a  Pointer to the desired enum name.
  69. * \param b  Pointer to an element of the \c all_enums array.
  70. */
  71. static int compar_name( const char *a, const enum_elt *b )
  72. {
  73.   return strcmp( a, & enum_string_table[ b->offset ] );
  74. }
  75.  
  76. /**
  77. * Compare a key enum value to an element in the \c all_enums array.
  78. *
  79. * \c bsearch always passes the key as the first parameter and the pointer
  80. * to the array element as the second parameter.  We can elimiate some
  81. * extra work by taking advantage of that fact.
  82. *
  83. * \param a  Pointer to the desired enum name.
  84. * \param b  Pointer to an index into the \c all_enums array.
  85. */
  86. static int compar_nr( const int *a, const unsigned *b )
  87. {
  88.   return a[0] - all_enums[*b].n;
  89. }
  90.  
  91.  
  92. static char token_tmp[20];
  93.  
  94. const char *_mesa_lookup_enum_by_nr( int nr )
  95. {
  96.   unsigned * i;
  97.  
  98.   i = (unsigned *) _mesa_bsearch(& nr, reduced_enums,
  99.                                  Elements(reduced_enums),
  100.                                  sizeof(reduced_enums[0]),
  101.                                  (cfunc) compar_nr);
  102.  
  103.   if ( i != NULL ) {
  104.      return & enum_string_table[ all_enums[ *i ].offset ];
  105.   }
  106.   else {
  107.      /* this is not re-entrant safe, no big deal here */
  108.      _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
  109.      token_tmp[sizeof(token_tmp) - 1] = '\\0';
  110.      return token_tmp;
  111.   }
  112. }
  113.  
  114. /* Get the name of an enum given that it is a primitive type.  Avoids
  115. * GL_FALSE/GL_POINTS ambiguity and others.
  116. */
  117. const char *_mesa_lookup_prim_by_nr( int nr )
  118. {
  119.   switch (nr) {
  120.   case GL_POINTS: return "GL_POINTS";
  121.   case GL_LINES: return "GL_LINES";
  122.   case GL_LINE_STRIP: return "GL_LINE_STRIP";
  123.   case GL_LINE_LOOP: return "GL_LINE_LOOP";
  124.   case GL_TRIANGLES: return "GL_TRIANGLES";
  125.   case GL_TRIANGLE_STRIP: return "GL_TRIANGLE_STRIP";
  126.   case GL_TRIANGLE_FAN: return "GL_TRIANGLE_FAN";
  127.   case GL_QUADS: return "GL_QUADS";
  128.   case GL_QUAD_STRIP: return "GL_QUAD_STRIP";
  129.   case GL_POLYGON: return "GL_POLYGON";
  130.   case GL_POLYGON+1: return "OUTSIDE_BEGIN_END";
  131.   default: return "<invalid>";
  132.   }
  133. }
  134.  
  135.  
  136.  
  137. int _mesa_lookup_enum_by_name( const char *symbol )
  138. {
  139.   enum_elt * f = NULL;
  140.  
  141.   if ( symbol != NULL ) {
  142.      f = (enum_elt *) _mesa_bsearch(symbol, all_enums,
  143.                                     Elements(all_enums),
  144.                                     sizeof( enum_elt ),
  145.                                     (cfunc) compar_name);
  146.   }
  147.  
  148.   return (f != NULL) ? f->n : -1;
  149. }
  150.  
  151. """
  152.                 return
  153.  
  154.  
  155.         def printBody(self, api_list):
  156.                 self.enum_table = {}
  157.                 for api in api_list:
  158.                         self.process_enums( api )
  159.  
  160.                 keys = self.enum_table.keys()
  161.                 keys.sort()
  162.  
  163.                 name_table = []
  164.                 enum_table = {}
  165.  
  166.                 for enum in keys:
  167.                         low_pri = 9
  168.                         for [name, pri] in self.enum_table[ enum ]:
  169.                                 name_table.append( [name, enum] )
  170.  
  171.                                 if pri < low_pri:
  172.                                         low_pri = pri
  173.                                         enum_table[enum] = name
  174.                                                
  175.  
  176.                 name_table.sort()
  177.  
  178.                 string_offsets = {}
  179.                 i = 0;
  180.                 print 'LONGSTRING static const char enum_string_table[] = '
  181.                 for [name, enum] in name_table:
  182.                         print '   "%s\\0"' % (name)
  183.                         string_offsets[ name ] = i
  184.                         i += len(name) + 1
  185.  
  186.                 print '   ;'
  187.                 print ''
  188.  
  189.  
  190.                 print 'static const enum_elt all_enums[%u] =' % (len(name_table))
  191.                 print '{'
  192.                 for [name, enum] in name_table:
  193.                         print '   { %5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name)
  194.                 print '};'
  195.                 print ''
  196.  
  197.                 print 'static const unsigned reduced_enums[%u] =' % (len(keys))
  198.                 print '{'
  199.                 for enum in keys:
  200.                         name = enum_table[ enum ]
  201.                         if [name, enum] not in name_table:
  202.                                 print '      /* Error! %s, 0x%04x */ 0,' % (name, enum)
  203.                         else:
  204.                                 i = name_table.index( [name, enum] )
  205.  
  206.                                 print '      %4u, /* %s */' % (i, name)
  207.                 print '};'
  208.  
  209.  
  210.                 self.print_code()
  211.                 return
  212.  
  213.  
  214.         def process_enums(self, api):
  215.                 for obj in api.enumIterateByName():
  216.                         if obj.value not in self.enum_table:
  217.                                 self.enum_table[ obj.value ] = []
  218.  
  219.  
  220.                         enum = self.enum_table[ obj.value ]
  221.                         name = "GL_" + obj.name
  222.                         priority = obj.priority()
  223.                         already_in = False;
  224.                         for n, p in enum:
  225.                                 if n == name:
  226.                                         already_in = True
  227.                         if not already_in:
  228.                                 enum.append( [name, priority] )
  229.  
  230.  
  231. def show_usage():
  232.         print "Usage: %s [-f input_file_name]" % sys.argv[0]
  233.         sys.exit(1)
  234.  
  235. if __name__ == '__main__':
  236.         try:
  237.                 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
  238.         except Exception,e:
  239.                 show_usage()
  240.  
  241.         api_list = []
  242.         for (arg,val) in args:
  243.                 if arg == "-f":
  244.                         api = gl_XML.parse_GL_API( val )
  245.                         api_list.append(api);
  246.  
  247.         printer = PrintGlEnums()
  248.         printer.Print( api_list )
  249.