mshtml/htmltextcont: Initialize value (Coverity).
[wine] / dlls / opengl32 / make_opengl
1 #!/usr/bin/perl -w
2 use strict;
3
4 # This script is called thus :
5 #
6 #   make_opengl path_to_spec_file opengl_version
7 #
8 #     - path_to_spec_file is the path to the directory where the OpenGL
9 #       spec files are located. These files are hosted in the OpenGL
10 #       extension registry at opengl.org:
11 #       http://www.opengl.org/registry/api/gl.spec
12 #       http://www.opengl.org/registry/api/gl.tm
13 #
14 #       The files used to be hosted and maintained by SGI. You can still find
15 #       find them in the sample implementation CVS tree which is located at
16 #       CVS_ROOT/projects/ogl-sample/main/doc/registry/specs.
17 #       You can also find them on the web at the following URL :
18 #         http://oss.sgi.com/cgi-bin/cvsweb.cgi/projects/ogl-sample/main/doc/registry/specs/
19 #
20 #     - opengl_version is the OpenGL version emulated by the library
21 #       (can be 1.0 to 1.5).
22 #
23 # This script generates the three following files :
24 #
25 #     - opengl32.spec : the spec file giving all the exported functions
26 #       of the OpenGL32.DLL library. These functions are the one an
27 #       application can directly link to (and are all the functions
28 #       defined in the OpenGL core for the version defined by
29 #       'opengl_version').
30 #
31 #     - opengl_norm.c : this file contains the thunks for all OpenGL
32 #       functions that are defined in 'opengl32.spec'. The corresponding
33 #       functions NEED to be defined in Linux's libGL or the library
34 #       won't be able to be linked in.
35 #
36 #     - opengl_ext.c : in this file are stored thunks for ALL possible
37 #       OpenGL extensions (at least, all the extensions that are defined
38 #       in the OpenGL extension registry). Contrary to 'opengl_norm.c',
39 #       you do not need to have these extensions in your libGL to have
40 #       OpenGL work (as they are resolved at run-time using
41 #       glXGetProcAddressARB).
42 #
43 # Copyright 2000 Lionel Ulmer
44 #
45 # This library is free software; you can redistribute it and/or
46 # modify it under the terms of the GNU Lesser General Public
47 # License as published by the Free Software Foundation; either
48 # version 2.1 of the License, or (at your option) any later version.
49 #
50 # This library is distributed in the hope that it will be useful,
51 # but WITHOUT ANY WARRANTY; without even the implied warranty of
52 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
53 # Lesser General Public License for more details.
54 #
55 # You should have received a copy of the GNU Lesser General Public
56 # License along with this library; if not, write to the Free Software
57 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
58 #
59
60 #
61 # Files to generate
62 #
63 my $spec_file = "opengl32.spec";
64 my $norm_file = "opengl_norm.c";
65 my $ext_file  = "opengl_ext.c";
66
67 # Set to 0 for removing the ENTER / LEAVE GL calls
68 my $gen_thread_safe = 1;
69 # Prefix used for the local variables
70 my $ext_prefix = "func_";
71 # If set to 1, generate TRACEs for each OpenGL function
72 my $gen_traces = 1;
73
74 #
75 # List of categories to put in the 'opengl_norm.c' file
76 #
77 my %cat_1_0 = ( "display-list" => 1,
78              "drawing" => 1,
79              "drawing-control" => 1,
80              "feedback" => 1,
81              "framebuf" => 1,
82              "misc" => 1,
83              "modeling" => 1,
84              "pixel-op" => 1,
85              "pixel-rw" => 1,
86              "state-req" => 1,
87              "xform" => 1 );
88 my %cat_1_1 = ( %cat_1_0,
89              "1_1" => 1 );
90 my %cat_1_2 = ( %cat_1_1,
91              "VERSION_1_2" => 1 );
92 my %cat_1_3 = ( %cat_1_2,
93              "VERSION_1_3" => 1 );
94 my %cat_1_4 = ( %cat_1_3,
95              "VERSION_1_4" => 1 );
96 my %cat_1_5 = ( %cat_1_4,
97              "VERSION_1_5" => 1 );
98
99 my %norm_categories = ();
100
101 #
102 # This hash table gives the conversion between OpenGL types and what
103 # is used by the TRACE printfs
104 #
105 my %debug_conv =
106     ("GLbitfield" => "%d",
107      "GLboolean" => "%d",
108      "GLbyte" => "%d",
109      "GLclampd" => "%f",
110      "GLclampf" => "%f",
111      "GLdouble" => "%f",
112      "GLenum" => "%d",
113      "GLfloat" => "%f",
114      "GLint" => "%d",
115      "GLshort" => "%d",
116      "GLsizei" => "%d",
117      "GLstring" => "%s",
118      "GLubyte" => "%d",
119      "GLuint" => "%d",
120      "GLushort" => "%d",
121      "GLhalfNV" => "%d",
122      "GLintptrARB" => "%d",
123      "GLsizeiptrARB" => "%d",
124      "GLintptr" => "%d",
125      "GLsizeiptr" => "%d",
126      "GLhandleARB" => "%d",
127      "GLcharARB" => "%c",
128      "GLvoid" => "(void)",
129      "_GLfuncptr" => "%p");
130
131 #
132 # This hash table gives the conversion between OpenGL types and what
133 # is used in the .spec file
134 #
135 my %arg_conv =
136     ("GLbitfield" => [ "long", 4 ],
137      "GLboolean" => [ "long", 4 ],
138      "GLbyte" => [ "long", 4 ],
139      "GLclampd" => [ "double", 8 ],
140      "GLclampf" => [ "long", 4 ],
141      "GLdouble" => [ "double", 8 ],
142      "GLenum" => [ "long", 4 ],
143      "GLfloat" => [ "long", 4 ],
144      "GLint" => [ "long", 4 ],
145      "GLshort" => [ "long", 4 ],
146      "GLsizei" => [ "long", 4 ],
147      "GLstring" => [ "str", 4 ],
148      "GLubyte" => [ "long", 4 ],
149      "GLuint" => [ "long", 4 ],
150      "GLushort" => [ "long", 4 ],
151      "GLhalfNV" => [ "long", 4 ],
152      "GLintptrARB" => [ "long", 4 ],
153      "GLsizeiptrARB" => [ "long", 4 ],
154      "GLhandleARB" => [ "long", 4 ],
155      "GLcharARB" => [ "long", 4 ],
156      "GLintptr" => [ "long", 4 ],
157      "GLsizeiptr" => [ "long", 4 ],
158      "GLvoid" => [ "void", 4 ],
159      "_GLfuncptr" => [ "ptr", 4 ]);
160
161 #
162 # Used to convert some types
163 #
164 sub ConvertType($)
165 {
166     my ($type) = @_;
167
168     my %hash = ( "GLstring" => "const GLubyte *",
169               "GLintptrARB" => "ptrdiff_t",
170               "GLsizeiptrARB" => "ptrdiff_t",
171               "GLintptr" => "ptrdiff_t",
172               "GLsizeiptr" => "ptrdiff_t",
173               "GLhandleARB" => "unsigned int",
174               "GLcharARB" => "char",
175               "GLchar" => "char",
176               "GLhalfNV" => "unsigned short" );
177
178     foreach my $org (reverse sort keys %hash) {
179         if ($type =~ /$org/) {
180             my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
181             return "$before$hash{$org}$after";
182         }
183     }
184     return $type;
185 }
186
187 #
188 # Used to convert some variable names
189 #
190 sub ConvertVarName($)
191 {
192     my ($type) = @_;
193
194     my %hash = ( "near" => "nearParam",
195                  "far"  => "farParam" );
196
197     foreach my $org (keys %hash) {
198         if ($type =~ /$org/) {
199             my ($before, $after) = ($type =~ /^(.*)$org(.*)$/);
200             return "$before$hash{$org}$after";
201         }
202     }
203     return $type;
204 }
205
206 #
207 # This functions generates the thunk for a given function.
208 #
209 sub GenerateThunk($$$$$)
210 {
211     my ($func_ref, $comment, $prefix, $thread_safe, $local_var) = @_;
212     my $ret = "";
213     my $call_arg = "";
214     my $trace_arg = "";
215
216     return "" if $func_ref->[0] eq "glGetString";
217     return "" if $func_ref->[0] eq "glGetIntegerv";
218     return "" if $func_ref->[0] eq "glEnable";
219     return "" if $func_ref->[0] eq "glIsEnabled";
220     return "" if $func_ref->[0] eq "glDisable";
221     return "" if $func_ref->[0] eq "glScissor";
222     return "" if $func_ref->[0] eq "glViewport";
223
224     # If for opengl_norm.c, generate a nice heading otherwise Patrik won't be happy :-)
225     # Patrik says: Well I would be even happier if a (OPENGL32.@) was added as well. Done. :-)
226     if ($comment eq 1) {
227         $ret = "$ret/***********************************************************************\n";
228         $ret = "$ret *              $func_ref->[0] (OPENGL32.\@)\n";
229         $ret = "$ret */\n";
230     }
231     $ret = $ret . ConvertType($func_ref->[1]) . " WINAPI wine_$func_ref->[0]( ";
232     for (my $i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
233         ## Quick debug code :-)
234         ## print $func_ref->[2]->[$i]->[1] . "\n";
235         my $type = $func_ref->[2]->[$i]->[0];
236         my $name = ConvertVarName($func_ref->[2]->[$i]->[1]);
237         $ret = $ret . ConvertType($type) . " $name";
238         $call_arg = "$call_arg$name";
239         if ($type =~ /\*/) {
240             $trace_arg = "$trace_arg\%p";
241         } else {
242             $trace_arg = "$trace_arg$debug_conv{$type}";
243         }
244         if ($i != $#{@{$func_ref->[2]}}) {
245             $ret = "$ret, ";
246             $call_arg = "$call_arg, ";
247             $trace_arg = "$trace_arg, ";
248         } else {
249             $ret = "$ret ";
250             $call_arg = "$call_arg ";
251         }
252     }
253     $ret .= 'void ' if ($#{@{$func_ref->[2]}} < 0);
254     $ret = "$ret) {\n";
255     if ($func_ref->[1] ne "void") {
256         $ret = "$ret  " . ConvertType($func_ref->[1]) . " ret_value;\n";
257     }
258     $ret .= $local_var;
259     if ($gen_traces) {
260         $ret = "$ret  TRACE(\"($trace_arg)\\n\"";
261         if ($trace_arg ne "") {
262             $ret = "$ret, $call_arg";
263         }
264         $ret = "$ret);\n";
265     }
266     if ($thread_safe) {
267         $ret = "$ret  ENTER_GL();\n";
268     }
269     $ret = "$ret  ";
270     if ($func_ref->[1] ne "void") {
271         $ret = $ret . "ret_value = ";
272     }
273     $ret = "$ret$prefix$func_ref->[0]( $call_arg);\n";
274     if ($thread_safe) {
275         $ret = "$ret  LEAVE_GL();\n";
276     }
277     if ($func_ref->[1] ne "void") {
278         $ret = "$ret  return ret_value;\n"
279     }
280     $ret = "$ret}\n";
281
282     # Return this string....
283     return $ret;
284 }
285
286 #
287 # Extract and checks the number of arguments
288 #
289 if (@ARGV != 2) {
290     my $name0=$0;
291     $name0=~s%^.*/%%;
292     die "Usage: $name0 OpenGL_registry_location OpenGL_version\n";
293 }
294 my $registry_path = shift @ARGV;
295 my $version       = shift @ARGV;
296 if ($version eq "1.0") {
297     %norm_categories = %cat_1_0;
298 } elsif ($version eq "1.1") {
299     %norm_categories = %cat_1_1;
300 } elsif ($version eq "1.2") {
301     %norm_categories = %cat_1_2;
302 } elsif ($version eq "1.3") {
303     %norm_categories = %cat_1_3;
304 } elsif ($version eq "1.4") {
305     %norm_categories = %cat_1_4;
306 } elsif ($version eq "1.5") {
307     %norm_categories = %cat_1_5;
308 } else {
309     die "Incorrect OpenGL version.\n";
310 }
311
312 #
313 # Open the registry files
314 #
315 open(TYPES,    "$registry_path/gl.tm")   || die "Could not open 'gl.tm'. Please check your path the the registry files.\n";
316 open(REGISTRY, "$registry_path/gl.spec") || die "Could not open 'gl.spec'. Please check your path the the registry files.\n";
317
318 #
319 # First, create a mapping between the pseudo types used in the spec file
320 # and OpenGL types using the 'gl.tm' file.
321 #
322 my %pseudo_to_opengl = ();
323 while (my $line = <TYPES>) {
324     if ($line !~ /\w*\#/) {
325         my ($pseudo, $opengl) = ($line =~ /(\w*),\*,\*,\s*(.*),\*,\*/);
326         $pseudo_to_opengl{$pseudo} = $opengl;
327     }
328 }
329 # This is to override the 'void' -> '*' bogus conversion
330 $pseudo_to_opengl{"void"} = "void";
331
332 #
333 # Then, create the list of all OpenGL functions using the 'gl.spec'
334 # file. This will create two hash-tables, one with all the function
335 # whose category matches the one listed in '@norm_categories', the other
336 # with all other functions.
337 #
338 # An element of the hash table is a reference to an array with these
339 # elements :
340 #
341 #  - function name
342 #
343 #  - return type
344 #
345 #  - reference to an array giving the list of arguments (an empty array
346 #    for a 'void' function).
347 #
348 # The list of arguments is itself an array of reference to arrays. Each
349 # of these arrays represents the argument type and the argument name.
350 #
351 # An example :
352 #
353 # void glBitmap( GLsizei width, GLsizei height,
354 #                GLfloat xorig, GLfloat yorig,
355 #                GLfloat xmove, GLfloat ymove,
356 #                const GLubyte *bitmap );
357 #
358 # Would give something like that :
359 #
360 # [ "glBitmap",
361 #   "void",
362 #   [ [ "GLsizei", "width" ],
363 #     [ "GLsizei", "height" ],
364 #     [ "GLfloat", "xorig" ],
365 #     [ "GLfloat", "yorig" ],
366 #     [ "GLfloat", "xmove" ],
367 #     [ "GLfloat", "ymove" ],
368 #     [ "GLubyte *", "bitmap"] ] ];
369 #
370 my %norm_functions = ();
371
372 #
373 # This stores various extensions NOT part of the GL extension registry but still
374 # implemented by most OpenGL libraries out there...
375 #
376
377 my %ext_functions  =
378     ( "glDeleteBufferRegion" => [ "glDeleteBufferRegion", "void", [ [ "GLenum", "region" ] ], "glDeleteBufferRegion", "GL_KTX_buffer_region" ],
379       "glReadBufferRegion" => [ "glReadBufferRegion", "void", [ [ "GLenum", "region" ],
380                                                                 [ "GLint", "x" ],
381                                                                 [ "GLint", "y" ],
382                                                                 [ "GLsizei", "width" ],
383                                                                 [ "GLsizei", "height" ] ], "glReadBufferRegion", "GL_KTX_buffer_region" ],
384       "glDrawBufferRegion" => [ "glDrawBufferRegion", "void", [ [ "GLenum", "region" ],
385                                                                 [ "GLint", "x" ],
386                                                                 [ "GLint", "y" ],
387                                                                 [ "GLsizei", "width" ],
388                                                                 [ "GLsizei", "height" ],
389                                                                 [ "GLint", "xDest" ],
390                                                                 [ "GLint", "yDest" ] ], "glDrawBufferRegion", "GL_KTX_buffer_region" ],
391       "glBufferRegionEnabled" => [ "glBufferRegionEnabled", "GLuint", [ ], "glBufferRegionEnabled",  "GL_KTX_buffer_region" ],
392       "glNewBufferRegion" => [ "glNewBufferRegion", "GLuint", [ [ "GLenum", "type" ] ], "glNewBufferRegion", "GL_KTX_buffer_region" ],
393       "glMTexCoord2fSGIS" => [ "glMTexCoord2fSGIS", "void", [ [ "GLenum", "target" ],
394                                                               [ "GLfloat", "s" ],
395                                                               [ "GLfloat", "t" ] ], "glMTexCoord2fSGIS", "GL_SGIS_multitexture" ],
396       "glMTexCoord2fvSGIS" => [ "glMTexCoord2fvSGIS", "void", [ [ "GLenum", "target" ],
397                                                                 [ "GLfloat *", "v" ] ], "glMTexCoord2fvSGIS", "GL_SGIS_multitexture" ],
398       "glMultiTexCoord1dSGIS" => [ "glMultiTexCoord1dSGIS", "void", [ [ "GLenum", "target" ],
399                                                                       [ "GLdouble", "s" ] ],  "glMultiTexCoord1dSGIS", "GL_SGIS_multitexture" ],
400       "glMultiTexCoord1dvSGIS" => [ "glMultiTexCoord1dvSGIS", "void", [ [ "GLenum", "target" ],
401                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord1dvSGIS", "GL_SGIS_multitexture" ],
402       "glMultiTexCoord1fSGIS" => [ "glMultiTexCoord1fSGIS", "void", [ [ "GLenum", "target" ],
403                                                                       [ "GLfloat", "s" ] ], "glMultiTexCoord1fSGIS", "GL_SGIS_multitexture" ],
404       "glMultiTexCoord1fvSGIS" => [ "glMultiTexCoord1fvSGIS", "void", [ [ "GLenum", "target" ],
405                                                                         [ "const GLfloat *", "v" ] ], "glMultiTexCoord1fvSGIS", "GL_SGIS_multitexture" ],
406       "glMultiTexCoord1iSGIS" => [ "glMultiTexCoord1iSGIS", "void", [ [ "GLenum", "target" ],
407                                                                       [ "GLint", "s" ] ], "glMultiTexCoord1iSGIS", "GL_SGIS_multitexture" ],
408       "glMultiTexCoord1ivSGIS" => [ "glMultiTexCoord1ivSGIS", "void", [ [ "GLenum", "target" ],
409                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord1ivSGIS", "GL_SGIS_multitexture" ],
410       "glMultiTexCoord1sSGIS" => [ "glMultiTexCoord1sSGIS", "void", [ [ "GLenum", "target" ],
411                                                                       [ "GLshort", "s" ] ], "glMultiTexCoord1sSGIS", "GL_SGIS_multitexture" ],
412       "glMultiTexCoord1svSGIS" => [ "glMultiTexCoord1svSGIS", "void", [ [ "GLenum", "target" ],
413                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord1svSGIS", "GL_SGIS_multitexture" ],
414       "glMultiTexCoord2dSGIS" => [ "glMultiTexCoord2dSGIS", "void", [ [ "GLenum", "target" ],
415                                                                       [ "GLdouble", "s"],
416                                                                       [ "GLdouble", "t" ] ], "glMultiTexCoord2dSGIS", "GL_SGIS_multitexture" ],
417       "glMultiTexCoord2dvSGIS" => [ "glMultiTexCoord2dvSGIS", "void", [ [ "GLenum", "target" ],
418                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord2dvSGIS", "GL_SGIS_multitexture" ],
419       "glMultiTexCoord2fSGIS" => [ "glMultiTexCoord2fSGIS", "void", [ [ "GLenum", "target" ],
420                                                                       [ "GLfloat", "s" ],
421                                                                       [ "GLfloat", "t" ] ], "glMultiTexCoord2fSGIS", "GL_SGIS_multitexture" ],
422       "glMultiTexCoord2fvSGIS" => [ "glMultiTexCoord2fvSGIS", "void", [ [ "GLenum", "target" ],
423                                                                         [ "GLfloat *", "v" ] ], "glMultiTexCoord2fvSGIS", "GL_SGIS_multitexture" ],
424       "glMultiTexCoord2iSGIS" => [ "glMultiTexCoord2iSGIS", "void", [ [ "GLenum", "target" ],
425                                                                       [ "GLint", "s" ],
426                                                                       [ "GLint", "t" ] ], "glMultiTexCoord2iSGIS", "GL_SGIS_multitexture" ],
427       "glMultiTexCoord2ivSGIS" => [ "glMultiTexCoord2ivSGIS", "void", [ [ "GLenum", "target" ],
428                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord2ivSGIS", "GL_SGIS_multitexture" ],
429       "glMultiTexCoord2sSGIS" => [ "glMultiTexCoord2sSGIS", "void", [ [ "GLenum", "target" ],
430                                                                       [ "GLshort", "s" ],
431                                                                       [ "GLshort", "t" ] ], "glMultiTexCoord2sSGIS", "GL_SGIS_multitexture" ],
432       "glMultiTexCoord2svSGIS" => [ "glMultiTexCoord2svSGIS", "void", [ [ "GLenum", "target" ],
433                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord2svSGIS", "GL_SGIS_multitexture" ],
434       "glMultiTexCoord3dSGIS" => [ "glMultiTexCoord3dSGIS", "void", [ [ "GLenum", "target" ],
435                                                                       [ "GLdouble", "s" ],
436                                                                       [ "GLdouble", "t" ],
437                                                                       [ "GLdouble", "r" ] ], "glMultiTexCoord3dSGIS", "GL_SGIS_multitexture" ],
438       "glMultiTexCoord3dvSGIS" => [ "glMultiTexCoord3dvSGIS", "void", [ [ "GLenum", "target" ],
439                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord3dvSGIS", "GL_SGIS_multitexture" ],
440       "glMultiTexCoord3fSGIS" => [ "glMultiTexCoord3fSGIS", "void", [ [ "GLenum", "target" ],
441                                                                       [ "GLfloat", "s" ],
442                                                                       [ "GLfloat", "t" ],
443                                                                       [ "GLfloat", "r" ] ], "glMultiTexCoord3fSGIS", "GL_SGIS_multitexture" ],
444       "glMultiTexCoord3fvSGIS" => [ "glMultiTexCoord3fvSGIS", "void", [ [ "GLenum", "target" ],
445                                                                         [ "GLfloat *", "v" ] ], "glMultiTexCoord3fvSGIS", "GL_SGIS_multitexture" ],
446       "glMultiTexCoord3iSGIS" => [ "glMultiTexCoord3iSGIS", "void", [ [ "GLenum", "target" ],
447                                                                       [ "GLint", "s" ],
448                                                                       [ "GLint", "t" ],
449                                                                       [ "GLint", "r" ] ], "glMultiTexCoord3iSGIS", "GL_SGIS_multitexture" ],
450       "glMultiTexCoord3ivSGIS" => [ "glMultiTexCoord3ivSGIS", "void", [ [ "GLenum", "target" ],
451                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord3ivSGIS", "GL_SGIS_multitexture" ],
452       "glMultiTexCoord3sSGIS" => [ "glMultiTexCoord3sSGIS", "void", [ [ "GLenum", "target" ],
453                                                                       [ "GLshort", "s" ],
454                                                                       [ "GLshort", "t" ],
455                                                                       [ "GLshort", "r" ] ], "glMultiTexCoord3sSGIS", "GL_SGIS_multitexture" ],
456       "glMultiTexCoord3svSGIS" => [ "glMultiTexCoord3svSGIS", "void", [ [ "GLenum", "target" ],
457                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord3svSGIS", "GL_SGIS_multitexture" ],
458       "glMultiTexCoord4dSGIS" => [ "glMultiTexCoord4dSGIS", "void", [ [ "GLenum", "target" ],
459                                                                       [ "GLdouble", "s" ],
460                                                                       [ "GLdouble", "t" ],
461                                                                       [ "GLdouble", "r" ],
462                                                                       [ "GLdouble", "q" ] ], "glMultiTexCoord4dSGIS", "GL_SGIS_multitexture" ],
463       "glMultiTexCoord4dvSGIS" => [ "glMultiTexCoord4dvSGIS", "void", [ [ "GLenum", "target" ],
464                                                                         [ "GLdouble *", "v" ] ], "glMultiTexCoord4dvSGIS", "GL_SGIS_multitexture" ],
465       "glMultiTexCoord4fSGIS" => [ "glMultiTexCoord4fSGIS", "void", [ [ "GLenum", "target" ],
466                                                                       [ "GLfloat", "s" ],
467                                                                       [ "GLfloat", "t" ],
468                                                                       [ "GLfloat", "r" ],
469                                                                       [ "GLfloat", "q" ] ], "glMultiTexCoord4fSGIS", "GL_SGIS_multitexture" ],
470       "glMultiTexCoord4fvSGIS" => [ "glMultiTexCoord4fvSGIS", "void", [ [ "GLenum", "target" ],
471                                                                         [ "GLfloat *", "v" ] ], "glMultiTexCoord4fvSGIS", "GL_SGIS_multitexture" ],
472       "glMultiTexCoord4iSGIS" => [ "glMultiTexCoord4iSGIS", "void", [ [ "GLenum", "target" ],
473                                                                       [ "GLint", "s" ],
474                                                                       [ "GLint", "t" ],
475                                                                       [ "GLint", "r" ],
476                                                                       [ "GLint", "q" ] ], "glMultiTexCoord4iSGIS", "GL_SGIS_multitexture" ],
477       "glMultiTexCoord4ivSGIS" => [ "glMultiTexCoord4ivSGIS", "void", [ [ "GLenum", "target" ],
478                                                                         [ "GLint *", "v" ] ], "glMultiTexCoord4ivSGIS", "GL_SGIS_multitexture" ],
479       "glMultiTexCoord4sSGIS" => [ "glMultiTexCoord4sSGIS", "void", [ [ "GLenum", "target" ],
480                                                                       [ "GLshort", "s" ],
481                                                                       [ "GLshort", "t" ],
482                                                                       [ "GLshort", "r" ],
483                                                                       [ "GLshort", "q" ] ], "glMultiTexCoord4sSGIS", "GL_SGIS_multitexture" ],
484       "glMultiTexCoord4svSGIS" => [ "glMultiTexCoord4svSGIS", "void", [ [ "GLenum", "target" ],
485                                                                         [ "GLshort *", "v" ] ], "glMultiTexCoord4svSGIS", "GL_SGIS_multitexture" ],
486       "glMultiTexCoordPointerSGIS" => [ "glMultiTexCoordPointerSGIS", "void", [ [ "GLenum", "target" ],
487                                                                                 [ "GLint", "size" ],
488                                                                                 [ "GLenum", "type" ],
489                                                                                 [ "GLsizei", "stride" ],
490                                                                                 [ "GLvoid *", "pointer" ] ], "glMultiTexCoordPointerSGIS", "GL_SGIS_multitexture" ],
491       "glSelectTextureSGIS" => [ "glSelectTextureSGIS", "void", [ [ "GLenum", "target" ] ], "glSelectTextureSGIS", "GL_SGIS_multitexture" ],
492       "glSelectTextureCoordSetSGIS" => [ "glSelectTextureCoordSetSGIS", "void", [ [ "GLenum", "target" ] ], "glSelectTextureCoordSetSGIS", "GL_SGIS_multitexture" ],
493       "glDeleteObjectBufferATI" => [ "glDeleteObjectBufferATI", "void", [ [ "GLuint", "buffer" ] ], "glDeleteObjectBufferATI", "GL_ATI_vertex_array_object" ]
494       );
495
496 my @arg_names;
497 my %arg_types;
498 while (my $line = <REGISTRY>) {
499     if ($line =~ /^\w*\(.*\)/) {
500         # Get the function name (NOTE: the 'gl' prefix needs to be added later)
501         my ($funcname, $args) = ($line =~ /^(\w*)\((.*)\)/);
502         # and the argument names
503         @arg_names = split /\s*,\s*/, $args;
504
505         # After get :
506         #  - the return type
507         #  - category (the extension the function is part of)
508         #  - the argument types
509         #  - the category the function belongs
510         %arg_types = ();
511         my $category = "";
512         my $ret_type = "";
513         while (1) {
514             $line = <REGISTRY>;
515             unless (defined($line)) {
516                 last;
517             } elsif ($line =~ /^\s*$/) {
518                 if (($category eq "") || ($ret_type eq "")) {
519                     die "Missing 'category' line in function $funcname.\n";
520                 }
521                 last;
522             } elsif ($line =~ /\t*return\t*(\w*)/) {
523                 ($ret_type) = ($line =~ /\t*return\s*(\w*)/);
524                 $ret_type = $pseudo_to_opengl{$ret_type};
525                 unless (defined($ret_type)) {
526                     die "Unsupported return type in function $funcname\n";
527                 }
528             } elsif ($line =~ /^\t*category/) {
529                 ($category) = ($line =~ /^\t*category\s*([\w-]*)/);
530             } elsif ($line =~ /^\t*param/) {
531                 my ($name, $base_type, $ext) = ($line =~ /\t*param\s*(\w*)\s*(\w*) (.*)/);
532                 my $ptr = 0;
533                 unless (defined($name)) {
534                     chomp $line;
535                     die "Broken spec file line $line in function $funcname\n";
536                 }
537
538                 if ($ext =~ /array/) {
539                     # This is a pointer
540                     $ptr = 1;
541                 } elsif ($ext =~ /value/) {
542                     # And this a 'normal' value
543                     $ptr = 0;
544                 } else {
545                     chomp $line;
546                     die "Unsupported type : $line in function $funcname\n";
547                 }
548                 # Get the 'real' type and append a '*' in case of a pointer
549                 my $type = $pseudo_to_opengl{$base_type};
550                 unless (defined($type)) {
551                     chomp $line;
552                     die "Unsupported return type in function $funcname for type $base_type (line $line)\n";
553                 }
554                 if ($ptr) {
555                     $type = "$type*";
556                 }
557
558                 $arg_types{$name} = $type;
559             }
560         }
561
562         # Now, build the argument reference
563         my $arg_ref = [ ];
564         for (my $i = 0; $i <= $#arg_names; $i++) {
565             unless (defined($arg_types{$arg_names[$i]})) {
566                 print "@arg_names\n";
567                 foreach (sort keys %arg_types) {
568                     print "$_ => $arg_types{$_}\n";
569                 }
570                 die "Undefined type for $arg_names[$i] in function $funcname\n";
571             }
572
573             push @$arg_ref, [ $arg_types{$arg_names[$i]}, $arg_names[$i] ];
574         }
575         my $func_ref = [ "gl$funcname",
576                          $ret_type,
577                          $arg_ref,
578                          "gl$funcname",
579                          "GL_$category" ];
580
581         # Now, put in one or the other hash table
582         if ($norm_categories{$category}) {
583             $norm_functions{"gl$funcname"} = $func_ref;
584         } else {
585             $ext_functions{"gl$funcname"} = $func_ref;
586         }
587     }
588 }
589
590 #
591 # Clean up the input files
592 #
593 close(TYPES);
594 close(REGISTRY);
595
596 #
597 # Now, generate the output files. First, the spec file.
598 #
599 open(SPEC, ">$spec_file");
600
601 foreach (sort keys %norm_functions) {
602     my $func_name = $norm_functions{$_}->[0];
603     print SPEC "@  stdcall $func_name( ";
604     for (my $i = 0; $i <= $#{@{$norm_functions{$_}->[2]}}; $i++) {
605         my $type = $norm_functions{$_}->[2]->[$i]->[0];
606         if ($type =~ /\*/) {
607             print SPEC "ptr ";
608         } elsif (defined($arg_conv{$type})) {
609             print SPEC "$@$arg_conv{$type}[0] ";
610         } else {
611             die "No conversion for GL type $type...\n";
612         }
613     }
614     print SPEC ") wine_$func_name\n";
615 }
616
617 print SPEC "@  stub    glGetLevelParameterfv
618 @  stub    glGetLevelParameteriv
619 @  stdcall wglChoosePixelFormat(long ptr) gdi32.ChoosePixelFormat
620 @  stdcall wglCopyContext(long long long)
621 @  stdcall wglCreateContext(long) gdi32.wglCreateContext
622 @  stdcall wglCreateLayerContext(long long)
623 @  stdcall wglDeleteContext(long) gdi32.wglDeleteContext
624 @  stdcall wglDescribeLayerPlane(long long long long ptr)
625 @  stdcall wglDescribePixelFormat(long long long ptr) gdi32.DescribePixelFormat
626 @  stdcall wglGetCurrentContext() gdi32.wglGetCurrentContext
627 @  stdcall wglGetCurrentDC() gdi32.wglGetCurrentDC
628 @  stub    wglGetDefaultProcAddress
629 @  stdcall wglGetLayerPaletteEntries(long long long long ptr)
630 @  stdcall wglGetPixelFormat(long) gdi32.GetPixelFormat
631 @  stdcall wglGetProcAddress(str)
632 @  stdcall wglMakeCurrent(long long) gdi32.wglMakeCurrent
633 @  stdcall wglRealizeLayerPalette(long long long)
634 @  stdcall wglSetLayerPaletteEntries(long long long long ptr)
635 @  stdcall wglSetPixelFormat(long long ptr) gdi32.SetPixelFormat
636 @  stdcall wglShareLists(long long) gdi32.wglShareLists
637 @  stdcall wglSwapBuffers(long) gdi32.SwapBuffers
638 @  stdcall wglSwapLayerBuffers(long long)
639 @  stdcall wglUseFontBitmapsA(long long long long) gdi32.wglUseFontBitmapsA
640 @  stdcall wglUseFontBitmapsW(long long long long) gdi32.wglUseFontBitmapsW
641 @  stdcall wglUseFontOutlinesA(long long long long long long long ptr)
642 @  stdcall wglUseFontOutlinesW(long long long long long long long ptr)
643 ";
644
645 close(SPEC);
646
647 #
648 # After the spec file, the opengl_norm.c file
649 #
650 open(NORM, ">$norm_file");
651 print NORM "
652 /* Auto-generated file... Do not edit ! */
653
654 #include \"config.h\"
655 #include \"opengl_ext.h\"
656 #include \"wine/debug.h\"
657
658 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
659 ";
660 foreach (sort keys %norm_functions) {
661     my $string = GenerateThunk($norm_functions{$_}, 1, "", $gen_thread_safe, "");
662
663     print NORM "\n$string" if $string;
664 }
665 close(NORM);
666
667 #
668 # Finally, more complex, the opengl_ext.c file
669 #
670 open(EXT, ">$ext_file");
671 print EXT "
672 /* Auto-generated file... Do not edit ! */
673
674 #include \"config.h\"
675 #include \"opengl_ext.h\"
676 #include \"wine/debug.h\"
677
678 WINE_DEFAULT_DEBUG_CHANNEL(opengl);
679
680 ";
681
682 # The thunks themselves....
683 my $count = keys %ext_functions;
684 print EXT "const int extension_registry_size = $count;\n";
685 print EXT "void *extension_funcs[$count];\n";
686 print EXT "\n/* The thunks themselves....*/";
687 my $pos = 0;
688 foreach (sort keys %ext_functions) {
689     my $func_ref = $ext_functions{$_};
690     my $local_var = "  " . ConvertType($func_ref->[1]) . " (*$ext_prefix$func_ref->[0])( ";
691     for (my $i = 0; $i <= $#{@{$func_ref->[2]}}; $i++) {
692         my $type = ConvertType($func_ref->[2]->[$i]->[0]);
693         $local_var .= "$type";
694         if ($i != $#{@{$func_ref->[2]}}) {
695             $local_var .= ", ";
696         } else {
697             $local_var .= " ";
698         }
699     }
700     $local_var .= 'void ' if ($#{@{$func_ref->[2]}} < 0);
701     $local_var .= ") = extension_funcs[$pos];\n";
702     $pos++;
703     print EXT "\nstatic ", GenerateThunk($ext_functions{$_}, 0, $ext_prefix, $gen_thread_safe, $local_var);
704 }
705
706 # Then the table giving the string <-> function correspondance */
707 print EXT "\n\n/* The table giving the correspondance between names and functions */\n";
708 print EXT "const OpenGL_extension extension_registry[$count] = {\n";
709 my $i = 0;
710 foreach (sort keys %ext_functions) {
711     my $func_ref = $ext_functions{$_};
712     if ($func_ref->[0] eq $func_ref->[3])
713     {
714         print EXT "  { \"$func_ref->[0]\", \"$func_ref->[4]\", (void *) wine_$func_ref->[0] }";
715     }
716     if ($i != $count-1) {
717         print EXT ",";
718     }
719     $i++;
720     print EXT "\n";
721 }
722 print EXT "};\n";
723
724 close(EXT);