mshtml: Use LONGLONG/ULONGLONG for PRInt64/PRUint64.
[wine] / dlls / opengl32 / wgl.c
1 /* Window-specific OpenGL functions implementation.
2  *
3  * Copyright (c) 1999 Lionel Ulmer
4  * Copyright (c) 2005 Raphael Junqueira
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "winreg.h"
32 #include "wingdi.h"
33 #include "winternl.h"
34 #include "winnt.h"
35
36 #include "opengl_ext.h"
37 #ifdef HAVE_GL_GLU_H
38 #undef far
39 #undef near
40 #include <GL/glu.h>
41 #endif
42 #include "wine/library.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(wgl);
46 WINE_DECLARE_DEBUG_CHANNEL(opengl);
47
48 typedef struct wine_wgl_s {
49     PROC WINAPI  (*p_wglGetProcAddress)(LPCSTR  lpszProc);
50
51     void WINAPI  (*p_wglGetIntegerv)(GLenum pname, GLint* params);
52     void WINAPI  (*p_wglFinish)(void);
53     void WINAPI  (*p_wglFlush)(void);
54 } wine_wgl_t;
55
56 /** global wgl object */
57 static wine_wgl_t wine_wgl;
58
59 #ifdef SONAME_LIBGLU
60 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
61 MAKE_FUNCPTR(gluNewTess)
62 MAKE_FUNCPTR(gluDeleteTess)
63 MAKE_FUNCPTR(gluTessBeginContour)
64 MAKE_FUNCPTR(gluTessBeginPolygon)
65 MAKE_FUNCPTR(gluTessCallback)
66 MAKE_FUNCPTR(gluTessEndContour)
67 MAKE_FUNCPTR(gluTessEndPolygon)
68 MAKE_FUNCPTR(gluTessVertex)
69 #undef MAKE_FUNCPTR
70 #endif /* SONAME_LIBGLU */
71
72 /* x11drv GDI escapes */
73 #define X11DRV_ESCAPE 6789
74 enum x11drv_escape_codes
75 {
76     X11DRV_GET_DISPLAY,         /* get X11 display for a DC */
77     X11DRV_GET_DRAWABLE,        /* get current drawable for a DC */
78     X11DRV_GET_FONT,            /* get current X font for a DC */
79     X11DRV_SET_DRAWABLE,        /* set current drawable for a DC */
80     X11DRV_START_EXPOSURES,     /* start graphics exposures */
81     X11DRV_END_EXPOSURES,       /* end graphics exposures */
82     X11DRV_GET_DCE,             /* get the DCE pointer */
83     X11DRV_SET_DCE,             /* set the DCE pointer */
84     X11DRV_GET_GLX_DRAWABLE,    /* get current glx drawable for a DC */
85     X11DRV_SYNC_PIXMAP          /* sync the dibsection to its pixmap */
86 };
87
88 void (*wine_tsx11_lock_ptr)(void) = NULL;
89 void (*wine_tsx11_unlock_ptr)(void) = NULL;
90
91 static HMODULE opengl32_handle;
92 static void* libglu_handle = NULL;
93
94 static char* internal_gl_disabled_extensions = NULL;
95 static char* internal_gl_extensions = NULL;
96
97 typedef struct wine_glcontext {
98   HDC hdc;
99   BOOL do_escape;
100   /* ... more stuff here */
101 } Wine_GLContext;
102
103 void enter_gl(void)
104 {
105     Wine_GLContext *curctx = NtCurrentTeb()->glContext;
106
107     if (curctx && curctx->do_escape)
108     {
109         enum x11drv_escape_codes escape = X11DRV_SYNC_PIXMAP;
110         ExtEscape(curctx->hdc, X11DRV_ESCAPE, sizeof(escape), (LPCSTR)&escape, 0, NULL);
111     }
112
113     wine_tsx11_lock_ptr();
114     return;
115 }
116
117 const GLubyte * WINAPI wine_glGetString( GLenum name );
118
119 /***********************************************************************
120  *              wglCreateLayerContext (OPENGL32.@)
121  */
122 HGLRC WINAPI wglCreateLayerContext(HDC hdc,
123                                    int iLayerPlane) {
124   TRACE("(%p,%d)\n", hdc, iLayerPlane);
125
126   if (iLayerPlane == 0) {
127       return wglCreateContext(hdc);
128   }
129   FIXME(" no handler for layer %d\n", iLayerPlane);
130
131   return NULL;
132 }
133
134 /***********************************************************************
135  *              wglDescribeLayerPlane (OPENGL32.@)
136  */
137 BOOL WINAPI wglDescribeLayerPlane(HDC hdc,
138                                   int iPixelFormat,
139                                   int iLayerPlane,
140                                   UINT nBytes,
141                                   LPLAYERPLANEDESCRIPTOR plpd) {
142   FIXME("(%p,%d,%d,%d,%p)\n", hdc, iPixelFormat, iLayerPlane, nBytes, plpd);
143
144   return FALSE;
145 }
146
147 /***********************************************************************
148  *              wglGetLayerPaletteEntries (OPENGL32.@)
149  */
150 int WINAPI wglGetLayerPaletteEntries(HDC hdc,
151                                      int iLayerPlane,
152                                      int iStart,
153                                      int cEntries,
154                                      const COLORREF *pcr) {
155   FIXME("(): stub !\n");
156
157   return 0;
158 }
159
160 static int compar(const void *elt_a, const void *elt_b) {
161   return strcmp(((const OpenGL_extension *) elt_a)->name,
162                 ((const OpenGL_extension *) elt_b)->name);
163 }
164
165 /* Check if a GL extension is supported */
166 static BOOL is_extension_supported(const char* extension)
167 {
168     const char *gl_ext_string = (const char*)wine_glGetString(GL_EXTENSIONS);
169
170     TRACE("Checking for extension '%s'\n", extension);
171
172     if(!gl_ext_string) {
173         ERR("No OpenGL extensions found, check if your OpenGL setup is correct!\n");
174         return FALSE;
175     }
176
177     /* We use the GetProcAddress function from the display driver to retrieve function pointers
178      * for OpenGL and WGL extensions. In case of winex11.drv the OpenGL extension lookup is done
179      * using glXGetProcAddress. This function is quite unreliable in the sense that its specs don't
180      * require the function to return NULL when a extension isn't found. For this reason we check
181      * if the OpenGL extension required for the function we are looking up is supported. */
182
183     /* Check if the extension is part of the GL extension string to see if it is supported. */
184     if(strstr(gl_ext_string, extension) != NULL)
185         return TRUE;
186
187     /* In general an OpenGL function starts as an ARB/EXT extension and at some stage
188      * it becomes part of the core OpenGL library and can be reached without the ARB/EXT
189      * suffix as well. In the extension table, these functions contain GL_VERSION_major_minor.
190      * Check if we are searching for a core GL function */
191     if(strncmp(extension, "GL_VERSION_", 11) == 0)
192     {
193         const GLubyte *gl_version = glGetString(GL_VERSION);
194         const char *version = extension + 11; /* Move past 'GL_VERSION_' */
195
196         if(!gl_version) {
197             ERR("Error no OpenGL version found,\n");
198             return FALSE;
199         }
200
201         /* Compare the major/minor version numbers of the native OpenGL library and what is required by the function.
202          * The gl_version string is guaranteed to have at least a major/minor and sometimes it has a release number as well. */
203         if( (gl_version[0] >= version[0]) || ((gl_version[0] == version[0]) && (gl_version[2] >= version[2])) ) {
204             return TRUE;
205         }
206         WARN("The function requires OpenGL version '%c.%c' while your drivers only provide '%c.%c'\n", version[0], version[2], gl_version[0], gl_version[2]);
207     }
208
209     return FALSE;
210 }
211
212 /***********************************************************************
213  *              wglGetProcAddress (OPENGL32.@)
214  */
215 PROC WINAPI wglGetProcAddress(LPCSTR  lpszProc) {
216   void *local_func;
217   OpenGL_extension  ext;
218   const OpenGL_extension *ext_ret;
219
220   TRACE("(%s)\n", lpszProc);
221
222   if(lpszProc == NULL)
223     return NULL;
224
225   /* First, look if it's not already defined in the 'standard' OpenGL functions */
226   if ((local_func = GetProcAddress(opengl32_handle, lpszProc)) != NULL) {
227     TRACE(" found function in 'standard' OpenGL functions (%p)\n", local_func);
228     return local_func;
229   }
230
231   /* After that, search in the thunks to find the real name of the extension */
232   ext.name = lpszProc;
233   ext_ret = bsearch(&ext, extension_registry, extension_registry_size,
234                     sizeof(OpenGL_extension), compar);
235
236   /* If nothing was found, we are looking for a WGL extension or an unknown GL extension. */
237   if (ext_ret == NULL) {
238     /* If the function name starts with a w it is a WGL extension */
239     if(lpszProc[0] == 'w')
240       return wine_wgl.p_wglGetProcAddress(lpszProc);
241
242     /* We are dealing with an unknown GL extension. */
243     WARN("Extension '%s' not defined in opengl32.dll's function table!\n", lpszProc);
244     return NULL;
245   } else { /* We are looking for an OpenGL extension */
246
247     /* Check if the GL extension required by the function is available */
248     if(!is_extension_supported(ext_ret->extension)) {
249         WARN("Extension '%s' required by function '%s' not supported!\n", ext_ret->extension, lpszProc);
250     }
251
252     local_func = wine_wgl.p_wglGetProcAddress(ext_ret->name);
253
254     /* After that, look at the extensions defined in the Linux OpenGL library */
255     if (local_func == NULL) {
256       char buf[256];
257       void *ret = NULL;
258
259       /* Remove the 3 last letters (EXT, ARB, ...).
260
261          I know that some extensions have more than 3 letters (MESA, NV,
262          INTEL, ...), but this is only a stop-gap measure to fix buggy
263          OpenGL drivers (moreover, it is only useful for old 1.0 apps
264          that query the glBindTextureEXT extension).
265       */
266       memcpy(buf, ext_ret->name, strlen(ext_ret->name) - 3);
267       buf[strlen(ext_ret->name) - 3] = '\0';
268       TRACE(" extension not found in the Linux OpenGL library, checking against libGL bug with %s..\n", buf);
269
270       ret = GetProcAddress(opengl32_handle, buf);
271       if (ret != NULL) {
272         TRACE(" found function in main OpenGL library (%p) !\n", ret);
273       } else {
274         WARN("Did not find function %s (%s) in your OpenGL library !\n", lpszProc, ext_ret->name);
275       }
276
277       return ret;
278     } else {
279       TRACE(" returning function  (%p)\n", ext_ret->func);
280       extension_funcs[ext_ret - extension_registry] = local_func;
281
282       return ext_ret->func;
283     }
284   }
285 }
286
287 /***********************************************************************
288  *              wglRealizeLayerPalette (OPENGL32.@)
289  */
290 BOOL WINAPI wglRealizeLayerPalette(HDC hdc,
291                                    int iLayerPlane,
292                                    BOOL bRealize) {
293   FIXME("()\n");
294
295   return FALSE;
296 }
297
298 /***********************************************************************
299  *              wglSetLayerPaletteEntries (OPENGL32.@)
300  */
301 int WINAPI wglSetLayerPaletteEntries(HDC hdc,
302                                      int iLayerPlane,
303                                      int iStart,
304                                      int cEntries,
305                                      const COLORREF *pcr) {
306   FIXME("(): stub !\n");
307
308   return 0;
309 }
310
311 /***********************************************************************
312  *              wglSwapLayerBuffers (OPENGL32.@)
313  */
314 BOOL WINAPI wglSwapLayerBuffers(HDC hdc,
315                                 UINT fuPlanes) {
316   TRACE_(opengl)("(%p, %08x)\n", hdc, fuPlanes);
317
318   if (fuPlanes & WGL_SWAP_MAIN_PLANE) {
319     if (!SwapBuffers(hdc)) return FALSE;
320     fuPlanes &= ~WGL_SWAP_MAIN_PLANE;
321   }
322
323   if (fuPlanes) {
324     WARN("Following layers unhandled : %08x\n", fuPlanes);
325   }
326
327   return TRUE;
328 }
329
330 #ifdef SONAME_LIBGLU
331
332 static void *load_libglu(void)
333 {
334     static int already_loaded;
335     void *handle;
336
337     if (already_loaded) return libglu_handle;
338     already_loaded = 1;
339
340     TRACE("Trying to load GLU library: %s\n", SONAME_LIBGLU);
341     handle = wine_dlopen(SONAME_LIBGLU, RTLD_NOW, NULL, 0);
342     if (!handle)
343     {
344         WARN("Failed to load %s\n", SONAME_LIBGLU);
345         return NULL;
346     }
347
348 #define LOAD_FUNCPTR(f) if((p##f = (void*)wine_dlsym(handle, #f, NULL, 0)) == NULL) goto sym_not_found;
349 LOAD_FUNCPTR(gluNewTess)
350 LOAD_FUNCPTR(gluDeleteTess)
351 LOAD_FUNCPTR(gluTessBeginContour)
352 LOAD_FUNCPTR(gluTessBeginPolygon)
353 LOAD_FUNCPTR(gluTessCallback)
354 LOAD_FUNCPTR(gluTessEndContour)
355 LOAD_FUNCPTR(gluTessEndPolygon)
356 LOAD_FUNCPTR(gluTessVertex)
357 #undef LOAD_FUNCPTR
358     libglu_handle = handle;
359     return handle;
360
361 sym_not_found:
362     WARN("Unable to load function ptrs from libGLU\n");
363     /* Close the library as we won't use it */
364     wine_dlclose(handle, NULL, 0);
365     return NULL;
366 }
367
368 static void fixed_to_double(POINTFX fixed, UINT em_size, GLdouble vertex[3])
369 {
370     vertex[0] = (fixed.x.value + (GLdouble)fixed.x.fract / (1 << 16)) / em_size;  
371     vertex[1] = (fixed.y.value + (GLdouble)fixed.y.fract / (1 << 16)) / em_size;  
372     vertex[2] = 0.0;
373 }
374
375 static void tess_callback_vertex(GLvoid *vertex)
376 {
377     GLdouble *dbl = vertex;
378     TRACE("%f, %f, %f\n", dbl[0], dbl[1], dbl[2]);
379     glVertex3dv(vertex);
380 }
381
382 static void tess_callback_begin(GLenum which)
383 {
384     TRACE("%d\n", which);
385     glBegin(which);
386 }
387
388 static void tess_callback_end(void)
389 {
390     TRACE("\n");
391     glEnd();
392 }
393
394 /***********************************************************************
395  *              wglUseFontOutlines_common
396  */
397 static BOOL wglUseFontOutlines_common(HDC hdc,
398                                       DWORD first,
399                                       DWORD count,
400                                       DWORD listBase,
401                                       FLOAT deviation,
402                                       FLOAT extrusion,
403                                       int format,
404                                       LPGLYPHMETRICSFLOAT lpgmf,
405                                       BOOL unicode)
406 {
407     UINT glyph;
408     const MAT2 identity = {{0,1},{0,0},{0,0},{0,1}};
409     GLUtesselator *tess;
410     LOGFONTW lf;
411     HFONT old_font, unscaled_font;
412     UINT em_size = 1024;
413     RECT rc;
414
415     TRACE("(%p, %d, %d, %d, %f, %f, %d, %p, %s)\n", hdc, first, count,
416           listBase, deviation, extrusion, format, lpgmf, unicode ? "W" : "A");
417
418     if (!load_libglu())
419     {
420         ERR("libGLU is required for this function but isn't loaded\n");
421         return FALSE;
422     }
423
424     ENTER_GL();
425     tess = pgluNewTess();
426     if(tess)
427     {
428         pgluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)tess_callback_vertex);
429         pgluTessCallback(tess, GLU_TESS_BEGIN, (_GLUfuncptr)tess_callback_begin);
430         pgluTessCallback(tess, GLU_TESS_END, tess_callback_end);
431     }
432     LEAVE_GL();
433
434     if(!tess) return FALSE;
435
436     GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
437     rc.left = rc.right = rc.bottom = 0;
438     rc.top = em_size;
439     DPtoLP(hdc, (POINT*)&rc, 2);
440     lf.lfHeight = -abs(rc.top - rc.bottom);
441     lf.lfOrientation = lf.lfEscapement = 0;
442     unscaled_font = CreateFontIndirectW(&lf);
443     old_font = SelectObject(hdc, unscaled_font);
444
445     for (glyph = first; glyph < first + count; glyph++)
446     {
447         DWORD needed;
448         GLYPHMETRICS gm;
449         BYTE *buf;
450         TTPOLYGONHEADER *pph;
451         TTPOLYCURVE *ppc;
452         GLdouble *vertices;
453
454         if(unicode)
455             needed = GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
456         else
457             needed = GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, 0, NULL, &identity);
458
459         if(needed == GDI_ERROR)
460             goto error;
461
462         buf = HeapAlloc(GetProcessHeap(), 0, needed);
463         vertices = HeapAlloc(GetProcessHeap(), 0, needed / sizeof(POINTFX) * 3 * sizeof(GLdouble));
464
465         if(unicode)
466             GetGlyphOutlineW(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
467         else
468             GetGlyphOutlineA(hdc, glyph, GGO_NATIVE, &gm, needed, buf, &identity);
469
470         TRACE("glyph %d\n", glyph);
471
472         if(lpgmf)
473         {
474             lpgmf->gmfBlackBoxX = (float)gm.gmBlackBoxX / em_size;
475             lpgmf->gmfBlackBoxY = (float)gm.gmBlackBoxY / em_size;
476             lpgmf->gmfptGlyphOrigin.x = (float)gm.gmptGlyphOrigin.x / em_size;
477             lpgmf->gmfptGlyphOrigin.y = (float)gm.gmptGlyphOrigin.y / em_size;
478             lpgmf->gmfCellIncX = (float)gm.gmCellIncX / em_size;
479             lpgmf->gmfCellIncY = (float)gm.gmCellIncY / em_size;
480
481             TRACE("%fx%f at %f,%f inc %f,%f\n", lpgmf->gmfBlackBoxX, lpgmf->gmfBlackBoxY,
482                   lpgmf->gmfptGlyphOrigin.x, lpgmf->gmfptGlyphOrigin.y, lpgmf->gmfCellIncX, lpgmf->gmfCellIncY); 
483             lpgmf++;
484         }
485
486         ENTER_GL();
487         glNewList(listBase++, GL_COMPILE);
488         pgluTessBeginPolygon(tess, NULL);
489
490         pph = (TTPOLYGONHEADER*)buf;
491         while((BYTE*)pph < buf + needed)
492         {
493             TRACE("\tstart %d, %d\n", pph->pfxStart.x.value, pph->pfxStart.y.value);
494
495             pgluTessBeginContour(tess);
496
497             fixed_to_double(pph->pfxStart, em_size, vertices);
498             pgluTessVertex(tess, vertices, vertices);
499             vertices += 3;
500
501             ppc = (TTPOLYCURVE*)((char*)pph + sizeof(*pph));
502             while((char*)ppc < (char*)pph + pph->cb)
503             {
504                 int i;
505
506                 switch(ppc->wType) {
507                 case TT_PRIM_LINE:
508                     for(i = 0; i < ppc->cpfx; i++)
509                     {
510                         TRACE("\t\tline to %d, %d\n", ppc->apfx[i].x.value, ppc->apfx[i].y.value);
511                         fixed_to_double(ppc->apfx[i], em_size, vertices); 
512                         pgluTessVertex(tess, vertices, vertices);
513                         vertices += 3;
514                     }
515                     break;
516
517                 case TT_PRIM_QSPLINE:
518                     for(i = 0; i < ppc->cpfx/2; i++)
519                     {
520                         /* FIXME just connecting the control points for now */
521                         TRACE("\t\tcurve  %d,%d %d,%d\n",
522                               ppc->apfx[i * 2].x.value,     ppc->apfx[i * 3].y.value,
523                               ppc->apfx[i * 2 + 1].x.value, ppc->apfx[i * 3 + 1].y.value);
524                         fixed_to_double(ppc->apfx[i * 2], em_size, vertices); 
525                         pgluTessVertex(tess, vertices, vertices);
526                         vertices += 3;
527                         fixed_to_double(ppc->apfx[i * 2 + 1], em_size, vertices); 
528                         pgluTessVertex(tess, vertices, vertices);
529                         vertices += 3;
530                     }
531                     break;
532                 default:
533                     ERR("\t\tcurve type = %d\n", ppc->wType);
534                     pgluTessEndContour(tess);
535                     goto error_in_list;
536                 }
537
538                 ppc = (TTPOLYCURVE*)((char*)ppc + sizeof(*ppc) +
539                                      (ppc->cpfx - 1) * sizeof(POINTFX));
540             }
541             pgluTessEndContour(tess);
542             pph = (TTPOLYGONHEADER*)((char*)pph + pph->cb);
543         }
544
545 error_in_list:
546         pgluTessEndPolygon(tess);
547         glTranslated((GLdouble)gm.gmCellIncX / em_size, (GLdouble)gm.gmCellIncY / em_size, 0.0);
548         glEndList();
549         LEAVE_GL();
550         HeapFree(GetProcessHeap(), 0, buf);
551         HeapFree(GetProcessHeap(), 0, vertices);
552     }
553
554  error:
555     DeleteObject(SelectObject(hdc, old_font));
556     pgluDeleteTess(tess);
557     return TRUE;
558
559 }
560
561 #else /* SONAME_LIBGLU */
562
563 static BOOL wglUseFontOutlines_common(HDC hdc,
564                                       DWORD first,
565                                       DWORD count,
566                                       DWORD listBase,
567                                       FLOAT deviation,
568                                       FLOAT extrusion,
569                                       int format,
570                                       LPGLYPHMETRICSFLOAT lpgmf,
571                                       BOOL unicode)
572 {
573     FIXME("Unable to compile in wglUseFontOutlines support without GL/glu.h\n");
574     return FALSE;
575 }
576
577 #endif /* SONAME_LIBGLU */
578
579 /***********************************************************************
580  *              wglUseFontOutlinesA (OPENGL32.@)
581  */
582 BOOL WINAPI wglUseFontOutlinesA(HDC hdc,
583                                 DWORD first,
584                                 DWORD count,
585                                 DWORD listBase,
586                                 FLOAT deviation,
587                                 FLOAT extrusion,
588                                 int format,
589                                 LPGLYPHMETRICSFLOAT lpgmf)
590 {
591     return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, FALSE);
592 }
593
594 /***********************************************************************
595  *              wglUseFontOutlinesW (OPENGL32.@)
596  */
597 BOOL WINAPI wglUseFontOutlinesW(HDC hdc,
598                                 DWORD first,
599                                 DWORD count,
600                                 DWORD listBase,
601                                 FLOAT deviation,
602                                 FLOAT extrusion,
603                                 int format,
604                                 LPGLYPHMETRICSFLOAT lpgmf)
605 {
606     return wglUseFontOutlines_common(hdc, first, count, listBase, deviation, extrusion, format, lpgmf, TRUE);
607 }
608
609 /***********************************************************************
610  *              glFinish (OPENGL32.@)
611  */
612 void WINAPI wine_glFinish( void )
613 {
614     TRACE("()\n");
615     wine_wgl.p_wglFinish();
616 }
617
618 /***********************************************************************
619  *              glFlush (OPENGL32.@)
620  */
621 void WINAPI wine_glFlush( void )
622 {
623     TRACE("()\n");
624     wine_wgl.p_wglFlush();
625 }
626
627 /***********************************************************************
628  *              glGetString (OPENGL32.@)
629  */
630 const GLubyte * WINAPI wine_glGetString( GLenum name )
631 {
632   const GLubyte *ret;
633   const char* GL_Extensions = NULL;
634
635   /* this is for buggy nvidia driver, crashing if called from a different
636      thread with no context */
637   if(wglGetCurrentContext() == NULL)
638     return NULL;
639
640   if (GL_EXTENSIONS != name) {
641     ENTER_GL();
642     ret = glGetString(name);
643     LEAVE_GL();
644     return ret;
645   }
646
647   if (NULL == internal_gl_extensions) {
648     ENTER_GL();
649     GL_Extensions = (const char *) glGetString(GL_EXTENSIONS);
650
651     if (GL_Extensions)
652     {
653       size_t len = strlen(GL_Extensions);
654       internal_gl_extensions = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len + 2);
655
656       TRACE("GL_EXTENSIONS reported:\n");
657       while (*GL_Extensions != 0x00) {
658         const char* Start = GL_Extensions;
659         char        ThisExtn[256];
660
661         while (*GL_Extensions != ' ' && *GL_Extensions != 0x00) {
662           GL_Extensions++;
663         }
664         memcpy(ThisExtn, Start, (GL_Extensions - Start));
665         ThisExtn[GL_Extensions - Start] = 0;
666         TRACE("- %s:", ThisExtn);
667         
668         /* test if supported API is disabled by config */
669         if (!internal_gl_disabled_extensions || !strstr(internal_gl_disabled_extensions, ThisExtn)) {
670           strcat(internal_gl_extensions, " ");
671           strcat(internal_gl_extensions, ThisExtn);
672           TRACE(" active\n");
673         } else {
674           TRACE(" deactived (by config)\n");
675         }
676
677         if (*GL_Extensions == ' ') GL_Extensions++;
678       }
679     }
680     LEAVE_GL();
681   }
682   return (const GLubyte *) internal_gl_extensions;
683 }
684
685 /***********************************************************************
686  *              glGetIntegerv (OPENGL32.@)
687  */
688 void WINAPI wine_glGetIntegerv( GLenum pname, GLint* params )
689 {
690     wine_wgl.p_wglGetIntegerv(pname, params);
691 }
692
693 /***********************************************************************
694  *              wglSwapBuffers (OPENGL32.@)
695  */
696 BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers( HDC hdc )
697 {
698     return SwapBuffers(hdc);
699 }
700
701 /* This is for brain-dead applications that use OpenGL functions before even
702    creating a rendering context.... */
703 static BOOL process_attach(void)
704 {
705   HMODULE mod_x11, mod_gdi32;
706   DWORD size;
707   HKEY hkey = 0;
708
709   GetDesktopWindow();  /* make sure winex11 is loaded (FIXME) */
710   mod_x11 = GetModuleHandleA( "winex11.drv" );
711   mod_gdi32 = GetModuleHandleA( "gdi32.dll" );
712
713   if (!mod_x11 || !mod_gdi32)
714   {
715       ERR("X11DRV or GDI32 not loaded. Cannot create default context.\n");
716       return FALSE;
717   }
718
719   wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod_x11, "wine_tsx11_lock" );
720   wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod_x11, "wine_tsx11_unlock" );
721
722   wine_wgl.p_wglGetProcAddress = (void *)GetProcAddress(mod_gdi32, "wglGetProcAddress");
723
724   /* Interal WGL function */
725   wine_wgl.p_wglGetIntegerv = (void *)wine_wgl.p_wglGetProcAddress("wglGetIntegerv");
726   wine_wgl.p_wglFinish = (void *)wine_wgl.p_wglGetProcAddress("wglFinish");
727   wine_wgl.p_wglFlush = (void *)wine_wgl.p_wglGetProcAddress("wglFlush");
728
729   if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\OpenGL", &hkey)) {
730     if (!RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, NULL, &size)) {
731       internal_gl_disabled_extensions = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
732       RegQueryValueExA( hkey, "DisabledExtensions", 0, NULL, (LPBYTE)internal_gl_disabled_extensions, &size);
733       TRACE("found DisabledExtensions=%s\n", debugstr_a(internal_gl_disabled_extensions));
734     }
735     RegCloseKey(hkey);
736   }
737
738   return TRUE;
739 }
740
741
742 /**********************************************************************/
743
744 static void process_detach(void)
745 {
746   if (libglu_handle) wine_dlclose(libglu_handle, NULL, 0);
747   HeapFree(GetProcessHeap(), 0, internal_gl_extensions);
748   HeapFree(GetProcessHeap(), 0, internal_gl_disabled_extensions);
749 }
750
751 /***********************************************************************
752  *           OpenGL initialisation routine
753  */
754 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
755 {
756     switch(reason)
757     {
758     case DLL_PROCESS_ATTACH:
759         opengl32_handle = hinst;
760         DisableThreadLibraryCalls(hinst);
761         return process_attach();
762     case DLL_PROCESS_DETACH:
763         process_detach();
764         break;
765     }
766     return TRUE;
767 }