OCC22105 Suspicious call to XOpenDisplay() in OSD_FontMgr class
[occt.git] / src / OpenGl / OpenGl_txgl.cxx
1 /***********************************************************************
2
3 FONCTION :
4 ----------
5 File OpenGl_txgl :
6
7
8 REMARQUES:
9 ---------- 
10
11
12 HISTORIQUE DES MODIFICATIONS   :
13 --------------------------------
14 xx-xx-xx : xxx ; Creation.
15 07-02-96 : FMN ; Suppression code inutile:
16 - TxglLink() et TxglUnlink()
17 08-03-96 : FMN ; Suppression variables globales
18 Ajout cmn_delete_from_htbl() dans TxglDestroyWindow()
19 21-03-96 : CAL ; test sur previous_ctx dans TxglDestroyWindow()
20 et dans TxglSetDbuff()
21 01-04-96 : CAL ; Integration MINSK portage WNT
22 15-04-96 : CAL ; Integration travail PIXMAP de Jim ROTH
23 26-04-96 : FMN ; Correction warning de compilation
24 20-06-96 : CAL ; Retrait du XDestroyWindow dans TxglDestroyWindow
25 18-07-96 : FMN ; Suppression code inutile: TxglSetWindow().
26 27-09-96 : CAL ; Portage WNT
27 16-10-96 : GG  ; Coder le parametre de GLX_DEPTH_SIZE a 1 plutot
28 que 0 si l'on souhaite accroitre les performances
29 de 50% en utilisant le ZBuffer hardware !!!
30 Si la fenetre fournie a deja le bon visual pas
31 la peine de creer une sous-fenetre.
32 16-10-96 : GG  ; Le dithering doit etre active aussi avec 12 plans
33 de maniere a ameliorer la qualite
34 17-10-96 : FMN ; Ajout fonction printVisualInfo()
35 06-11-96 : CAL ; Remise a True du BackDitherProp pour < 12 plans
36 12-11-96 : CAL ; BackDitherProp = True pour <= 8 plans
37 BackDitherProp = False pour > 8 plans
38 29-01-97 : FMN ; Amelioration du tests pour le dithering
39 DitherProp = True pour <= 8 plans red
40 DitherProp = False pour > 8 plans red
41 Suppression de TxglSetDbuff()
42 06-06-97 : FMN ; Meilleure gestion glXMakeCurrent (pb avec LightWoks)
43 Suppression de previous_win
44 02-07-97 : FMN ; Suppression variable ESSAI
45 07-10-97 : FMN ; Simplification WNT 
46 13-10-97 : FMN ; Ajout wglShareLists
47 06-02-98 : FMN ; PRO11674: Suppression XSetErrorHandler(0) inutile
48 23-11-98 : CAL ; PRO16603: previous_ctx jamais remis a 0 pour eviter
49 la perte des lists.
50 07-12-98 : CAL ; PRO 16311 et PRO 11821
51 02.14.100 : JR : Warnings on WNT
52 14.07.06 : SAN : OCC12977: update previous_ctx properly in TxglDestroyWindow.
53 Old code resulted in crashes on some ATI Radeon cards under Linux.
54
55 ************************************************************************/
56
57 #define BUC60691  /*GG 06/06/00 Due to a severe bug in secondary 
58 //      table hash-code computation not yet solve,
59 //      It's necessary to compute the primary hash-key key
60 //      correctly under WNT/W98. The actual method is wrong
61 //      because a size 4 is used for this table instead
62 //      a conventional prime number as under UNIX system (23).
63 //       Under W98 sometimes the function wglMakeContext() does
64 //      not work for an UNKNOWN reason, the number of DC
65 //      seems limited to 5 but nothing tell that the limit is
66 //      reached !
67 //      We try right now to recover this error by creating a new DC.
68 */
69
70 #define RIC120303 /*GG Add new function TxglSetWindow using
71 //      the GLXContext defined by the user
72 //      Add new function TxglGetContext.
73 */
74
75 #define OCC954  /*SAV: 13/11/02 - check GLRC before deleting it*/
76
77 /*----------------------------------------------------------------------*/
78 /*
79 * Includes
80 */
81 #include <OpenGl_tgl_all.hxx>
82 #include <OpenGl_telem_util.hxx>
83 #include <stdio.h>
84
85 #include <OpenGl_Memory.hxx>
86
87
88 #ifdef WNT
89 struct HTBL_ENTRY {
90   HDC   hDC;
91   HGLRC hGLRC;
92   int   nUsed;
93   IMPLEMENT_MEMORY_OPERATORS
94 };
95
96
97 int call_util_osd_getenv ( char*, char*, int );
98 #endif  /* WNT */
99
100 #include <OpenGl_txgl.hxx>
101 int call_util_osd_getenv( char * , char * , int ) ;
102
103 /*----------------------------------------------------------------------*/
104 /*
105 * Variables statiques
106 */
107
108 #ifndef WNT
109 typedef NCollection_DataMap<Tint, GLCONTEXT> GLContextMap;
110 #else
111 typedef NCollection_DataMap<Tint, HTBL_ENTRY*> GLContextMap;
112 #endif
113
114 static GLContextMap _Txgl_Map;
115
116
117 #ifndef WNT
118 static  int BackDitherProp = False; /* Dithering pour le background */
119 static  int DitherProp = True;    /* Dithering pour le trace  */
120 static  GLXContext previous_ctx = 0;  /* Use for share display list */
121 static  GLXContext dead_ctx;            /* Context to be destroyed */
122 static  Display *dead_dpy;              /* Display associated with dead_ctx */
123 #else
124 static  int BackDitherProp = FALSE; /* Dithering pour le background */
125 static  int DitherProp = TRUE;    /* Dithering pour le trace  */
126 static  BOOL s_sysPalInUse;             /* Flag to check system colors usage */ 
127 static  HGLRC previous_ctx = 0;   /* Use for share display list */
128 #endif /* WNT */
129
130 /*----------------------------------------------------------------------*/
131 /*
132 * Constantes
133 */
134
135 #define NO_TRACE
136
137 #define CALL_DEF_STRING_LENGTH 132
138
139 #define WIN_HTBL_SIZE 23
140
141 /*----------------------------------------------------------------------*/
142 /*
143 * Fonctions statiques
144 */
145
146 #ifndef WNT
147 #ifdef TRACE
148 static GLvoid printVisualInfo( Display *, XVisualInfo *glxVisual );
149 #endif
150 #else
151 #ifdef BUC60691
152 static BOOL win95 = FALSE;
153 #endif
154
155 static int find_pixel_format(HTBL_ENTRY * hte, PIXELFORMATDESCRIPTOR * pfd);
156
157 __declspec( dllexport ) int __fastcall __OpenGl_INIT__ ( 
158   unsigned hInstance, unsigned long reason_for_call
159   ) {
160     if ( reason_for_call == DLL_PROCESS_ATTACH ) {
161
162     }
163     return 1;
164
165   }  /* end __OpenGl_INIT__ */
166 #endif  /* WNT */
167
168   /*----------------------------------------------------------------------*/
169
170   WINDOW
171     TxglCreateWindow( DISPLAY  *disp, WINDOW par,
172     Tint x, Tint y, Tint w, Tint h, Tint bw,
173     Tfloat bgcolr, Tfloat bgcolg, Tfloat bgcolb )
174   {
175
176 #ifndef WNT
177
178     GLCONTEXT ctx;
179     static int sdesc[11];
180     Colormap cmap;
181     XVisualInfo* vis=NULL;
182     /*    XVisualInfo tmplt;*/
183     XSetWindowAttributes cwa;
184     XColor color;
185     /*    Tint i, n, nret;*/
186     Tint n;
187     Tint scr;
188     int value;
189     char string[CALL_DEF_STRING_LENGTH];
190     int DBuffer = True;
191     XWindowAttributes wattr;
192
193     WINDOW win;
194
195     unsigned long mask = 0;
196     /*    unsigned long background_pixel = 0;*/
197
198     if (call_util_osd_getenv("CALL_OPENGL_NO_DBF", string, CALL_DEF_STRING_LENGTH))
199       DBuffer    = False;
200
201     if (call_util_osd_getenv("JWR_PIXMAP_DB", string, CALL_DEF_STRING_LENGTH))
202       TelSetPixmapDB(1);
203
204     XGetWindowAttributes( disp , par , &wattr );
205
206     n = 0;
207     sdesc[n] = GLX_RGBA;n++;
208
209     sdesc[n] = GLX_DEPTH_SIZE;n++;
210     sdesc[n] = 1;n++;
211
212     sdesc[n] = GLX_RED_SIZE;n++;
213     sdesc[n] = (wattr.depth <= 8) ? 0 : 1;n++;
214
215     sdesc[n] = GLX_GREEN_SIZE;n++;
216     sdesc[n] = (wattr.depth <= 8) ? 0 : 1;n++;
217
218     sdesc[n] = GLX_BLUE_SIZE;n++;
219     sdesc[n] = (wattr.depth <= 8) ? 0 : 1;n++;
220
221     if (DBuffer) {
222       sdesc[n] = GLX_DOUBLEBUFFER;n++;
223     }
224
225     sdesc[n] = None;n++;
226
227     scr = DefaultScreen( disp );
228
229 #if defined(__linux) || defined(Linux)
230     {
231       XVisualInfo vinfo;
232       int ninfo;
233       unsigned long vmask = VisualIDMask |  VisualScreenMask;
234       vinfo.visualid = wattr.visual->visualid;
235       vinfo.screen = DefaultScreen( disp );
236       vis = XGetVisualInfo( disp, vmask, &vinfo, &ninfo);
237     }
238 #endif
239
240     if( !vis )
241       vis = glXChooseVisual( disp, scr, sdesc );
242     if( !vis) return TFailure;
243
244 #ifdef TRACE
245     printf ("TxglCreateWindow \n");
246     printf ("Informations sur le visual\n");
247     printf ("par visualid %x%x %d\n", wattr.visual->visualid, wattr.visual->visualid);
248     printf ("vis visualid 0x%x %d\n", vis->visualid, vis->visualid);
249     printf ("vis depth %d\n", vis->depth);
250     printf ("vis class %d\n", vis->class);
251     printf ("vis red_mask %ld\n", vis->red_mask);
252     printf ("vis green_mask %ld\n", vis->green_mask);
253     printf ("vis blue_mask %ld\n", vis->blue_mask);
254     printf ("vis colormap_size %d\n", vis->colormap_size);
255     printf ("vis bits_per_rgb %d\n", vis->bits_per_rgb);
256     printVisualInfo( disp, vis );
257 #endif
258
259     /*
260     * Le BackDitherProp est utilise pour le clear du background
261     * Pour eviter une difference de couleurs avec la couleur choisie
262     * par l'application (XWindow) il faut desactiver le dithering
263     * au dessus de 8 plans.
264     * 
265     * Pour le DitherProp:
266     * On cherchera a activer le Dithering que si le Visual a au moins
267     * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
268     * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
269     * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
270     */
271
272     glXGetConfig( disp, vis, GLX_RED_SIZE, &value );
273
274     if ( value < 8 ) {
275       DitherProp = True;
276     }
277     else
278     {
279       DitherProp = False;
280     }
281
282     if ( vis->depth <= 8 ) {
283       BackDitherProp = True;
284     }
285     else
286     {
287       BackDitherProp = False;
288     }
289
290 #ifdef TRACE
291     printf("Dithering %d BackDithering %d \n",DitherProp,BackDitherProp);
292 #endif
293
294     if (call_util_osd_getenv ("CALL_OPENGL_NO_DITHER", string, CALL_DEF_STRING_LENGTH))
295       DitherProp = False;
296
297     if (call_util_osd_getenv ("CALL_OPENGL_NO_BACKDITHER", string, CALL_DEF_STRING_LENGTH))
298       BackDitherProp = False;
299
300     if (dead_ctx) {
301       /* recover display lists from dead_ctx, then destroy it */
302       ctx = glXCreateContext( disp, vis, dead_ctx, GL_TRUE );
303       glXDestroyContext(dead_dpy, dead_ctx);
304       dead_ctx = 0;
305     } else if (previous_ctx == 0) {
306       ctx = glXCreateContext( disp, vis, NULL, GL_TRUE );
307     } else {
308       /* ctx est une copie du previous */
309       ctx = glXCreateContext( disp, vis, previous_ctx, GL_TRUE );
310     }
311     previous_ctx = ctx;
312
313     if( !ctx) return TFailure;
314
315     cmap = XCreateColormap( disp,  par, vis->visual, AllocNone );
316
317     color.red = (unsigned short) (bgcolr * 0xFFFF);
318     color.green = (unsigned short) (bgcolg * 0xFFFF);
319     color.blue  = (unsigned short) (bgcolb * 0xFFFF);
320     color.flags = DoRed | DoGreen | DoBlue;
321     XAllocColor( disp, cmap, &color );
322
323     cwa.colormap  = cmap;
324     cwa.event_mask  = StructureNotifyMask;
325     cwa.border_pixel  = color.pixel;
326     cwa.background_pixel = color.pixel;
327
328     mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
329
330     if( vis->visualid == wattr.visual->visualid ) {
331       win = par;
332     } 
333     else 
334     {
335       win = XCreateWindow( disp, par, x, y, w, h, bw,
336         vis->depth, InputOutput, vis->visual,
337         mask, &cwa );
338     }
339
340 #ifdef TRACE
341     printf ("TxglCreateWindow win %x par %x \n", win, par);
342 #endif
343
344     XSetWindowBackground( disp, win, cwa.background_pixel );
345     XClearWindow( disp, win );
346
347     /* if in Pixmap double buffering mode, set up pixmap */
348
349     if (TelTestPixmapDB())
350     {
351       GC gc;
352       Pixmap pixmap;
353       GLXPixmap glxpixmap;
354
355       printf("setting up pixmap double buffering\n");
356
357       gc = XCreateGC(disp, win, 0, NULL);
358
359       pixmap = XCreatePixmap(disp, win, w, h, vis->depth);
360
361       glxpixmap = glXCreateGLXPixmap(disp, vis, pixmap);
362
363       glXMakeCurrent(disp, glxpixmap, ctx);
364
365       glDrawBuffer(GL_FRONT);
366
367       TelSetPixmapDBParams(disp, win, w, h, vis->depth, gc, pixmap, glxpixmap, ctx);
368     }
369
370     XFree((char*)vis);  
371
372     _Txgl_Map.Bind( (Tint)win, ctx );
373
374     return win;
375
376 #else /* WNT */
377
378     HTBL_ENTRY*           hte;
379     PIXELFORMATDESCRIPTOR pfd;
380     BOOL                  DBuffer = TRUE;
381     int                   iPixelFormat;
382     char                  string[ CALL_DEF_STRING_LENGTH ];
383
384 #ifdef BUC60691
385     OSVERSIONINFO os;
386     os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
387     GetVersionEx(&os);
388     if( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) win95 = TRUE; 
389 #endif
390
391     if ( _Txgl_Map.IsBound( (Tint)par ) ) {
392       hte = _Txgl_Map.ChangeFind( (Tint)par );
393       if ( hte ) 
394       {
395         ++hte -> nUsed;
396         printf("*TxglCreateWindow.window %d is alreday created\n",par);
397         return par;
398       }
399     }
400
401     hte = new HTBL_ENTRY();
402
403     if ( !hte ) return 0;
404     hte -> hDC   = GetDC ( par );
405
406     iPixelFormat = find_pixel_format(hte, &pfd);
407
408     if ( !iPixelFormat ) 
409     {
410       printf ("*OpenGL interface: ChoosePixelFormat failed. Error code: %d\n",GetLastError ());
411
412       ReleaseDC ( par, hte -> hDC );
413       delete hte;
414
415       return 0;
416     }  
417
418     if ( pfd.dwFlags & PFD_NEED_PALETTE ) 
419     {
420 #ifndef _WIN64
421       WINDOW_DATA* wd = ( WINDOW_DATA* )GetWindowLong ( par, GWL_USERDATA );
422 #else
423       WINDOW_DATA* wd = ( WINDOW_DATA* )GetWindowLong ( par, GWLP_USERDATA );
424 #endif
425
426       InterfaceGraphic_RealizePalette (hte -> hDC, wd -> hPal, FALSE,
427         s_sysPalInUse = pfd.dwFlags & PFD_NEED_SYSTEM_PALETTE ? TRUE : FALSE); 
428     }  
429
430     if ( pfd.cColorBits <= 8 ) 
431     { 
432       DitherProp     = TRUE;
433       BackDitherProp = TRUE;    
434     }  
435
436     if (call_util_osd_getenv ("CALL_OPENGL_NO_DITHER", string, CALL_DEF_STRING_LENGTH))
437       DitherProp = FALSE;
438
439     if (call_util_osd_getenv ("CALL_OPENGL_NO_BACKDITHER", string, CALL_DEF_STRING_LENGTH))
440       BackDitherProp = FALSE;
441
442     if (  !SetPixelFormat ( hte -> hDC, iPixelFormat, &pfd )  ) 
443     {
444       printf ("*OpenGL interface: SetPixelFormat failed. Error code %d\n",GetLastError ()); 
445       ReleaseDC ( par, hte -> hDC );
446       delete hte;
447       return 0; 
448     }  
449
450     hte -> hGLRC = wglCreateContext ( hte -> hDC );
451
452     if ( !hte -> hGLRC ) 
453     { 
454       printf ("*OpenGL interface: wglCreateContext failed. Error code: %d\n",GetLastError ());    
455       return 0;     
456     }  
457
458     if (previous_ctx == 0 )    
459     {
460       previous_ctx = hte -> hGLRC;
461     } else
462       wglShareLists(previous_ctx, hte -> hGLRC);
463
464     _Txgl_Map.Bind( (Tint)par, hte );
465
466     return par;
467
468 #endif  /* WNT */
469
470   }
471
472 #ifdef RIC120302
473   WINDOW
474     TxglSetWindow( DISPLAY  *disp, WINDOW par, GLCONTEXT ctx)
475   {
476 #ifndef WNT
477     XVisualInfo* vis;
478     char string[CALL_DEF_STRING_LENGTH];
479     XWindowAttributes wattr;
480
481     XGetWindowAttributes( disp , par , &wattr );
482     {
483       unsigned long vmask = VisualIDMask |  VisualScreenMask;
484       XVisualInfo vinfo;
485       int ninfo;
486       vinfo.visualid = wattr.visual->visualid;
487       vinfo.screen = DefaultScreen( disp );
488       vis = XGetVisualInfo( disp, vmask, &vinfo, &ninfo);
489     }
490
491     if( !vis) return TFailure;
492
493 #ifdef TRACE
494     printf ("TxglSetWindow \n");
495     printf ("Informations sur le visual\n");
496     printf ("par visualid %x%x %d\n", wattr.visual->visualid, wattr.visual->visualid);
497     printf ("vis visualid 0x%x %d\n", vis->visualid, vis->visualid);
498     printf ("vis depth %d\n", vis->depth);
499     printf ("vis class %d\n", vis->class);
500     printf ("vis red_mask %ld\n", vis->red_mask);
501     printf ("vis green_mask %ld\n", vis->green_mask);
502     printf ("vis blue_mask %ld\n", vis->blue_mask);
503     printf ("vis colormap_size %d\n", vis->colormap_size);
504     printf ("vis bits_per_rgb %d\n", vis->bits_per_rgb);
505     printVisualInfo( disp, vis );
506 #endif
507
508     /*
509     * Le BackDitherProp est utilise pour le clear du background
510     * Pour eviter une difference de couleurs avec la couleur choisie
511     * par l'application (XWindow) il faut desactiver le dithering
512     * au dessus de 8 plans.
513     * 
514     * Pour le DitherProp:
515     * On cherchera a activer le Dithering que si le Visual a au moins
516     * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
517     * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
518     * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
519     */
520
521     {
522       int value;
523       glXGetConfig( disp, vis, GLX_RED_SIZE, &value );
524
525       if ( value < 8 ) {
526         DitherProp = True;
527       } else {
528         DitherProp = False;
529       }
530
531       if ( vis->depth <= 8 ) {
532         BackDitherProp = True;
533       } else {
534         BackDitherProp = False;
535       }
536     }
537
538 #ifdef TRACE
539     printf("Dithering %d BackDithering %d \n",DitherProp,BackDitherProp);
540 #endif
541
542     if (call_util_osd_getenv ("CALL_OPENGL_NO_DITHER", string, CALL_DEF_STRING_LENGTH))
543       DitherProp = False;
544
545     if (call_util_osd_getenv ("CALL_OPENGL_NO_BACKDITHER", string, CALL_DEF_STRING_LENGTH))
546       BackDitherProp = False;
547
548     previous_ctx = ctx;
549
550     XFree((char*)vis);  
551
552     _Txgl_Map.Bind( ( Tint )par, ctx );
553 #else /* WNT */
554
555     /* abd
556     cmn_htbl_elem         rec;*/
557     HTBL_ENTRY*           hte;
558     PIXELFORMATDESCRIPTOR pfd;
559     BOOL                  DBuffer = TRUE;
560     int                   iPixelFormat;
561     char                  string[ CALL_DEF_STRING_LENGTH ];
562
563 #ifdef BUC60691
564     OSVERSIONINFO os;
565     os.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
566     GetVersionEx(&os);
567     if( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) win95 = TRUE; 
568 #endif
569
570     if ( _Txgl_Map.IsBound( ( Tint )par ) )
571     {
572       hte = _Txgl_Map.ChangeFind( ( Tint )par );
573       ++hte -> nUsed;
574       printf("*TxglSetWindow.window %d is alreday created\n",par);
575       return par;
576     }
577
578     hte = new HTBL_ENTRY();
579
580     if ( !hte ) return 0;
581
582     hte -> hDC   = GetDC ( par );
583     iPixelFormat = find_pixel_format(hte, &pfd);
584
585     if ( !iPixelFormat ) 
586     {
587       printf ("*OpenGL interface: ChoosePixelFormat failed. Error code: %d\n",GetLastError ());
588
589       ReleaseDC ( par, hte -> hDC );
590       delete hte;
591
592       return 0;
593     }  
594
595     if ( pfd.dwFlags & PFD_NEED_PALETTE ) 
596     {
597 #ifndef _WIN64
598       WINDOW_DATA* wd = ( WINDOW_DATA* )GetWindowLong ( par, GWL_USERDATA );
599 #else
600       WINDOW_DATA* wd = ( WINDOW_DATA* )GetWindowLong ( par, GWLP_USERDATA );
601 #endif
602
603       InterfaceGraphic_RealizePalette (hte -> hDC, wd -> hPal, FALSE,
604         s_sysPalInUse = pfd.dwFlags & PFD_NEED_SYSTEM_PALETTE ? TRUE : FALSE); 
605     }  
606
607     if ( pfd.cColorBits <= 8 ) 
608     { 
609       DitherProp     = TRUE;
610       BackDitherProp = TRUE;    
611     }  
612
613     if (call_util_osd_getenv ("CALL_OPENGL_NO_DITHER", string, CALL_DEF_STRING_LENGTH))
614       DitherProp = FALSE;
615
616     if (call_util_osd_getenv ("CALL_OPENGL_NO_BACKDITHER", string, CALL_DEF_STRING_LENGTH))
617       BackDitherProp = FALSE;
618
619     if (  !SetPixelFormat ( hte -> hDC, iPixelFormat, &pfd )  ) 
620     {
621       printf ("*OpenGL interface: SetPixelFormat failed. Error code %d\n",GetLastError ()); 
622       ReleaseDC ( par, hte -> hDC );
623       delete hte;
624       return 0; 
625     }  
626
627     hte -> hGLRC = previous_ctx = ctx;
628
629     _Txgl_Map.Bind( ( Tint )par, hte );
630 #endif  /* WNT */
631
632     return par;
633
634   }
635 #endif /*RIC120302*/
636
637   /*----------------------------------------------------------------------*/
638
639   TStatus
640     TxglWinset( DISPLAY *disp, WINDOW win )
641   {
642
643 #ifndef WNT
644
645     Bool  i;
646     GLXContext ctx;
647     GLenum errorcode;
648     const GLubyte *errorstring;
649
650     if ( !_Txgl_Map.IsBound( win ) )
651       return TFailure;
652
653     ctx = _Txgl_Map.ChangeFind( win );
654
655
656 #ifdef TRACE
657     printf ("TxglWinset::glXMakeCurrent %x \n", win);
658 #endif
659     if (TelTestPixmapDB())
660     {
661       i = glXMakeCurrent(disp, TelGetGLXPixmap(), ctx);
662     }
663     else
664     {
665       i = glXMakeCurrent(disp, win, ctx);  /* TRUE/FALSE */
666     }
667     if (!i)
668     {
669       errorcode = glGetError();
670       errorstring = gluErrorString(errorcode);
671       printf("glXMakeCurrent failed: %d %s\n", errorcode, errorstring);
672     }
673
674     return  i == True ? TSuccess : TFailure;
675
676 #else /* WNT */
677
678     HTBL_ENTRY*   hte;
679     HDC    hdc    = NULL;
680     HGLRC  hglrc  = NULL;
681     TStatus       retVal = TFailure;
682
683     __try {
684
685       if ( !_Txgl_Map.IsBound( ( Tint )win ) ) {
686         printf("OpenGL interface:  TxglWinset failed.UNKNOWN win %x\n",win);
687         __leave;
688       }
689       hte = _Txgl_Map.ChangeFind( ( Tint )win );
690
691 #ifdef BUC60691
692       if( win95 ) {
693         retVal = (TStatus)ReleaseDC ( win, hte -> hDC );
694         hte -> hDC   = GetDC ( win );
695       }   
696 #endif
697       if (  !wglMakeCurrent ( hte -> hDC, hte -> hGLRC )  ) 
698       {
699 #ifdef BUC60691
700         GLenum errorcode;
701         const GLubyte *errorstring;
702
703         errorcode = glGetError();
704         errorstring = gluErrorString(errorcode);
705         printf("wglMakeCurrent failed: %d %s\n", errorcode, errorstring);
706 #else
707         printf ("OpenGL interface: wglMakeCurrent failed. Error code: %d\n",GetLastError ());
708 #endif
709         retVal = TFailure;  
710       } else retVal = TSuccess;
711
712     }  /* end __try */
713
714
715     __finally 
716     {    
717     }  
718
719     return retVal;
720
721 #endif  /* WNT */
722
723   }
724
725   /*----------------------------------------------------------------------*/
726
727   WINDOW
728     TxglGetSubWindow( DISPLAY *disp, WINDOW win )
729     /* This function assumes that there is only
730     one child for the parent */
731   {
732
733 #ifndef WNT
734
735     Window root, parent, *child, w;
736     unsigned int num;
737
738     if( XQueryTree( disp, win, &root, &parent, &child, &num ) )
739     {
740       if (! num) return win;
741       w = child[0];
742       XFree( (char *)child );
743       return w;
744     }
745     else
746       return 0;
747
748 #else /* WNT */
749
750     return win;
751
752 #endif  /* WNT */
753
754   }
755
756   /*----------------------------------------------------------------------*/
757
758   void
759     TxglDestroyWindow( DISPLAY *disp, WINDOW win )
760   {
761
762 #ifndef WNT
763
764     GLXContext ctx;
765     Tint dummy;
766
767     if ( !_Txgl_Map.IsBound(win) )
768       return;
769     ctx = _Txgl_Map.ChangeFind(win);
770
771     /* FSXXX sync necessary if non-direct rendering */
772     glXWaitGL();
773
774     _Txgl_Map.UnBind( win );
775
776     if (previous_ctx == ctx) {
777       /* san -- OCC12977: it's important to put some valid GLXContext or null into
778       previous_ctx here, otherwise next glxCreateContext() will crash on some ATI Radeon cards
779       */
780       previous_ctx = NULL;
781       if ( _Txgl_Map.Size() > 0 ) {
782         GLContextMap::Iterator it(_Txgl_Map);
783         previous_ctx = it.Value();
784       }
785       /*
786       * if this is the last remaining context, do not destroy it yet, to avoid
787       * losing any shared display lists (fonts...)
788       */
789       if (previous_ctx) {
790         glXDestroyContext(disp, ctx);
791       } else {
792         dead_ctx = ctx;
793         dead_dpy = disp;
794       }
795     } else {
796       glXDestroyContext(disp, ctx);
797     }
798
799 #else /* WNT */
800
801     HTBL_ENTRY*   hte;
802 #ifdef _DEBUG 
803     WINDOW_DATA*  wd;
804 #endif  /* _DEBUG */
805     if ( !_Txgl_Map.IsBound(( Tint )win) )
806       return;
807     hte = _Txgl_Map.ChangeFind(( Tint )win);
808
809 #ifdef _DEBUG 
810     /* In release version of application we need to process    */
811     /*  palette messages in the main application message loop. */
812     /*  In debug version we don't have message loop for most   */
813     /*  cases. So, let's restore system colors here now.       */
814 #ifndef _WIN64
815     wd = ( WINDOW_DATA* )GetWindowLong ( win, GWL_USERDATA );
816 #else
817     wd = ( WINDOW_DATA* )GetWindowLong ( win, GWLP_USERDATA );
818 #endif
819
820     if ( wd != NULL ) InterfaceGraphic_RealizePalette (
821       hte -> hDC, wd -> hPal, TRUE, s_sysPalInUse);
822 #endif  /* _DEBUG */
823
824     if ( --hte -> nUsed == 0 ) 
825     { 
826 #ifdef OCC954    
827       if ( wglGetCurrentContext() != NULL )
828 #endif
829         wglDeleteContext ( hte -> hGLRC );
830       ReleaseDC ( win, hte -> hDC );
831       _Txgl_Map.UnBind( (Tint ) win );
832       delete hte;
833     }  
834
835 #endif  /* WNT */
836
837   }
838
839   /*----------------------------------------------------------------------*/
840
841   int
842     TxglGetDither(void)
843   {
844     return DitherProp;
845   }
846
847   /*----------------------------------------------------------------------*/
848
849   int
850     TxglGetBackDither(void)
851   {
852     return BackDitherProp;
853   }
854
855
856   /*----------------------------------------------------------------------*/
857   /*RIC120302*/
858   GLCONTEXT 
859     TxglGetContext( WINDOW win )
860   {
861     GLCONTEXT ctx = NULL;
862     if ( _Txgl_Map.IsBound( (Tint) win ) )
863       ctx = 
864 #ifdef WNT    
865       _Txgl_Map.Find( (Tint) win )->hGLRC;
866 #else
867       _Txgl_Map.Find( win );
868 #endif
869
870     return ctx;  
871   }
872   /*RIC120302*/
873
874   /*----------------------------------------------------------------------*/
875   enum { ZERO = 0, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN };
876
877   /* Unused :*/
878 #ifdef TRACE
879   static const char*
880     className( int class )
881   {
882     static char      *classes[] = {
883       "StaticGray",
884         "GrayScale",
885         "StaticColor",
886         "PseudoColor",
887         "TrueColor",
888         "DirectColor",
889     };
890
891     if ( class < ZERO || class > FIVE )
892       return "unknown";
893     else
894       return classes[class];
895   }
896 #endif
897   /*----------------------------------------------------------------------*/
898 #ifndef WNT
899
900 #ifdef TRACE
901   static GLvoid printVisualInfo( DISPLAY  *display, XVisualInfo *glxVisual )
902   {
903
904 #define TrueFalse(x)  ( x ? "True" : "False" )
905
906     int        tmp;
907
908     printf("\n" );
909     printf("   X Visual Information ...\n\n" );
910     printf("\tvisualid : 0x%x\n", glxVisual->visualid );
911     printf("\tclass    : %s\n\n", className( glxVisual->class ) );
912
913     glXGetConfig(display, glxVisual, GLX_USE_GL, &tmp);
914     printf( "\tSupport GL ( GLX_USE_GL ) : %s\n", TrueFalse(tmp) );
915
916     glXGetConfig(display, glxVisual, GLX_LEVEL, &tmp);
917     printf( "\tFramebuffer ( GLX_LEVEL ) : %s\n\n",
918       (tmp < ZERO) ? "Underlay" : (tmp == ZERO ? "Normal"  : tmp > ONE ? "Overlay"  : "Popup") );
919
920     glXGetConfig(display, glxVisual, GLX_BUFFER_SIZE, &tmp);
921     printf( "\tFramebuffer depth ( GLX_BUFFER_SIZE )     : %d\n", tmp );
922
923     glXGetConfig(display, glxVisual, GLX_DOUBLEBUFFER, &tmp);
924     printf( "\tDoublebuffer ( GLX_DOUBLEBUFFER )         : %s\n",
925       TrueFalse(tmp) );
926
927     glXGetConfig(display, glxVisual, GLX_DEPTH_SIZE, &tmp);
928     printf( "\tDepth buffer depth ( GLX_DEPTH_SIZE )     : %d\n", tmp );
929
930     glXGetConfig(display, glxVisual, GLX_STENCIL_SIZE, &tmp);
931     printf( "\tStencil buffer depth ( GLX_STENCIL_SIZE ) : %d\n", tmp );
932
933     glXGetConfig(display, glxVisual, GLX_STEREO, &tmp);
934     printf( "\tStereo Buffer ( GLX_STEREO )              : %s\n",
935       TrueFalse(tmp) );
936
937     glXGetConfig(display, glxVisual, GLX_AUX_BUFFERS, &tmp);
938     printf( "\tAuxillary Buffers ( GLX_AUX_BUFFERS)      : %d\n\n", tmp );
939
940     glXGetConfig(display, glxVisual, GLX_RGBA, &tmp);
941     printf( "\tColor mode ( GLX_RGBA )       : %s\n", tmp ? "RGBA" :
942     "Color Index" );
943
944     glXGetConfig(display, glxVisual, GLX_RED_SIZE, &tmp);
945     printf( "\tRed Bits ( GLX_RED_SIZE )     : %d\n", tmp );
946
947     glXGetConfig(display, glxVisual, GLX_GREEN_SIZE, &tmp);
948     printf( "\tGreen Bits ( GLX_GREEN_SIZE ) : %d\n", tmp );
949
950     glXGetConfig(display, glxVisual, GLX_BLUE_SIZE, &tmp);
951     printf( "\tBlue Bits ( GLX_BLUE_SIZE )   : %d\n", tmp );
952
953     glXGetConfig(display, glxVisual, GLX_ALPHA_SIZE, &tmp);
954     printf( "\tAlpha Bits ( GLX_ALPHA_SIZE ) : %d\n\n", tmp );
955
956     glXGetConfig(display, glxVisual, GLX_ACCUM_RED_SIZE, &tmp);
957     printf( "\tRed Accumulation Bits ( GLX_ACCUM_RED_SIZE )     : %d\n", tmp );
958
959     glXGetConfig(display, glxVisual, GLX_ACCUM_GREEN_SIZE, &tmp);
960     printf( "\tGreen Accumulation Bits ( GLX_ACCUM_GREEN_SIZE ) : %d\n", tmp );
961
962     glXGetConfig(display, glxVisual, GLX_ACCUM_BLUE_SIZE, &tmp);
963     printf( "\tBlue Accumulation Bits ( GLX_ACCUM_BLUE_SIZE )   : %d\n", tmp );
964
965     glXGetConfig(display, glxVisual, GLX_ACCUM_ALPHA_SIZE, &tmp);
966     printf( "\tAlpha Accumulation Bits ( GLX_ACCUM_ALPHA_SIZE ) : %d\n\n", tmp );
967   }
968 #endif //TRACE
969 #else //WNT
970   int find_pixel_format(HTBL_ENTRY * hte, PIXELFORMATDESCRIPTOR * pfd)
971   {
972     int                   iPixelFormat = 0;
973     int                   iGood = 0;
974     int                   i, j;
975     PIXELFORMATDESCRIPTOR pfd0;
976     char                  string[ CALL_DEF_STRING_LENGTH ];
977     BOOL                  DBuffer = TRUE;
978     const int             cBits[] = { 32, 24 };
979     const int             dBits[] = { 32, 24, 16 };
980
981     if (call_util_osd_getenv ("CALL_OPENGL_NO_DBF",
982       string, CALL_DEF_STRING_LENGTH)) 
983       DBuffer = FALSE;
984
985     pfd0.nSize           = sizeof ( PIXELFORMATDESCRIPTOR );
986     pfd0.nVersion        = 1;
987     pfd0.dwFlags         = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
988     pfd0.dwFlags        |= ( DBuffer ) ? PFD_DOUBLEBUFFER : PFD_SUPPORT_GDI;
989     pfd0.iPixelType      = PFD_TYPE_RGBA;
990     pfd0.cRedBits        = 0;
991     pfd0.cRedShift       = 0;
992     pfd0.cGreenBits      = 0;
993     pfd0.cGreenShift     = 0;
994     pfd0.cBlueBits       = 0;
995     pfd0.cBlueShift      = 0;
996     pfd0.cAlphaBits      = 0;
997     pfd0.cAlphaShift     = 0;
998     pfd0.cAccumBits      = 0;
999     pfd0.cAccumRedBits   = 0;
1000     pfd0.cAccumGreenBits = 0;
1001     pfd0.cAccumBlueBits  = 0;
1002     pfd0.cAccumAlphaBits = 0;
1003     pfd0.cStencilBits    = 0;
1004     pfd0.cAuxBuffers     = 0;
1005     pfd0.iLayerType      = PFD_MAIN_PLANE;
1006     pfd0.bReserved       = 0;
1007     pfd0.dwLayerMask     = 0;
1008     pfd0.dwVisibleMask   = 0;
1009     pfd0.dwDamageMask    = 0;
1010
1011     hte -> nUsed = 1;    
1012
1013     for (i = 0; i < sizeof(dBits) / sizeof(int); i++) {
1014
1015       pfd0.cDepthBits = dBits[i];
1016       iGood = 0;
1017       for (j = 0; j < sizeof(cBits) / sizeof(int); j++) {
1018
1019         pfd0.cColorBits = cBits[j];
1020         iPixelFormat = ChoosePixelFormat ( hte -> hDC, &pfd0 );
1021
1022         if (iPixelFormat) {
1023           pfd->cDepthBits = 0;
1024           pfd->cColorBits = 0;
1025           DescribePixelFormat (hte -> hDC, iPixelFormat,
1026             sizeof ( PIXELFORMATDESCRIPTOR ), pfd);
1027           if (pfd->cColorBits >= cBits[j] && pfd->cDepthBits >= dBits[i])
1028             break;
1029           if (iGood == 0)
1030             iGood = iPixelFormat;
1031         }
1032       }
1033       if (j < sizeof(cBits) / sizeof(int))
1034         break;
1035     }
1036
1037     if ( !iPixelFormat )
1038       iPixelFormat = iGood;
1039
1040     return iPixelFormat;
1041   }
1042
1043 #endif  /* WNT */
1044   /*----------------------------------------------------------------------*/