aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/OpenGL_X11.c
blob: 610611d099a020c32de52fbc31bbc4e78329a9fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*
 * Leo Chan -- 1995
 *
 * This file takes care of the C implementation of opening up an
 * X window, assigning an OpenGL graphics context to it, storing that
 * graphics context in the java class data structure.
 *
 * also contains the use() and swap() functions for double buffering
 *
 * Adam King -- 1997
 *
 * Modified Creation window code to use the currently showing Java Frame
 * window instead of creating a new window.
 * ---------------
 *
 * Many improvements and changes are made !
 *
 * Sven Goethel
 *
 * September 1997
 *
 * some X stuff is also modified/debugged to run on aix ...
 */

/**
 * Pointer Semantics of GLContext:
 * ==============================

   "displayHandle"	:= (Display *)
   	
   "windowHandle" 	:= (Window)
   	if("offScreenRenderer" == FALSE && "createOwnWindow" == TRUE )
		"windowHandle" contains the new created Window 
		               (by XCreateWindow)!
	else
		"windowHandle" contains the new Java-Native Window 

   "pixmapHandle"	:= (Pixmap)
   	if("offScreenRenderer" == TRUE)
		"pixmapHandle" contains the new Pixmap (by XCreatePixmap)!
		"windowHandle" contains the new created OffScreenWindow 
			       (by glXCreateGLXPixmap)!
        else
		"pixmapHandle" is unused !

   "sharedGLContextNative" := (GLXContext)
   	This is the optional shared GLContext !

   "glContext" := (GLXContext)
   	This is THE used GLContext !

   "pData    " := (jlong)
   	This is used as the "not initialized" flag !
	It is reset to zero while Destroy Method
 */

#include <stdio.h>
#include <stdlib.h>
#include <jni.h>

/*
 * the next thing to include are special headers that were created by
 * JAVAH.  They include the C structure definitions for the JAVA classes
 */
#include "gl4java_GLContext.h"

/*--------------------------------------------------------------------------
 * here on in is just regular apple pie C
 */

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include <GL/glx.h>
#include <X11/Xatom.h>

#include <stdlib.h>

#include "OpenGL_X11_common.h"

/*
 * STATIC FLAGS FOR gl4java behavior ...
 */
static jboolean verbose = JNI_FALSE;

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_useJAWT( JNIEnv *env, jobject obj )
{
    (void)env;
    (void)obj;
    return JNI_FALSE;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_hasJAWTSurfaceChanged( JNIEnv *env, jobject obj,
					      jlong thisWin )
{
    (void)env;
    (void)obj;
    (void)thisWin;
    return JNI_FALSE;
}

/*
 * OpenGL_GLFrame_openOpenGL
 *
 * the function name is created by JAVAH to reflect the package and class
 * names of the JAVA object that called this native code.
 *
 * the parameter is a handle to the OpenGL_OpenGLFrame object -- 
 * the data portion
 * followed by the method table for the class.
 */
JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_openOpenGLNative( JNIEnv *env, jobject obj,
					 jobject canvas)
{
    int screen = 0;

    /* found matching visual and gc */
    VisualGC vgc = { NULL, 0, 0 };

    jboolean ret = JNI_TRUE;

    jclass cls = 0;
    jfieldID fdisplayHandle=0, fwindowHandle=0, fpixmapHandle=0;
    jfieldID fverbose=0;
    jfieldID fglContext=0;
    jfieldID fstereoView=0, frgba=0, fstencilBits=0, 
             faccumSize=0, fownwind=0;
    jfieldID fdoubleBuffer=0, foffScreenRenderer;

    jfieldID fcreatewinw = 0, fcreatewinh = 0;
    jfieldID fshareWith = 0;
    jboolean joffScreenRenderer=JNI_FALSE;
    jboolean jdoubleBuffer=JNI_TRUE;
    jboolean jstereoView=JNI_FALSE;
    jboolean jrgba=JNI_TRUE;
    jint jstencilBits=0;
    jint jaccumSize=0;
    jboolean jownwind = JNI_FALSE ;
    jint jcreatewinw = 0, jcreatewinh = 0;
    GLXContext jshareWith = 0;

    /* these variables will be mapped in the java-object ! */
    Window thisWin=0;
    Display * display = 0;
    Window rootwini  = 0;
    Pixmap pix;

    int iValue, iValue1, iValue2, iValue3;

    (void)canvas;

    cls = (*env)->GetObjectClass(env, obj);
    if(cls==0) 
    {
        fprintf(stderr,"GL4Java ERROR: clazz not accessible\n");
	fflush(stderr);
	ret=JNI_FALSE;
    }

    if(ret==JNI_TRUE) {
	    fverbose =(*env)->GetStaticFieldID(env, cls, "gljNativeDebug", "Z");
	    if (fverbose == 0) 
	    {
                fprintf(stderr,"GL4Java: fverbose==0\n");
                fflush(stderr);
		ret=JNI_FALSE;
	    } else {
		verbose = (*env)->GetStaticBooleanField(env, cls, fverbose);
	    }
    }

    if(JNI_TRUE==verbose)
    {
	fprintf(stderr, "GL4Java: sizes:\n jint=%d\n (Display *)=%d\n GLXContext=%d\n",
	  sizeof(jint), sizeof(Display *), sizeof(GLXContext) );
	fflush(stderr);
    }

    /* FIRST OF ALL CHECK IF A NATIVE POINTER OR X11-TYPE FITS IN �jlong� */
    ret = testX11Java();
    ret = testJavaGLTypes(verbose);

    if(ret==JNI_TRUE) {
	    foffScreenRenderer = 
	    		(*env)->GetFieldID(env, cls, "offScreenRenderer", "Z");
            if (foffScreenRenderer == 0) ret= JNI_FALSE;
	    else joffScreenRenderer =(*env)->GetBooleanField(env, obj, foffScreenRenderer);
    }

    if(ret==JNI_TRUE) {
	    fdoubleBuffer = (*env)->GetFieldID(env, cls, "doubleBuffer", "Z");
	    if (fdoubleBuffer == 0) ret= JNI_FALSE;
	    else jdoubleBuffer =(*env)->GetBooleanField(env, obj, fdoubleBuffer);
    }

    if(ret==JNI_TRUE) {
	    fstereoView = (*env)->GetFieldID(env, cls, "stereoView", "Z");
	    if (fstereoView == 0) ret= JNI_FALSE;
	    else jstereoView =(*env)->GetBooleanField(env, obj, fstereoView);
    }

    if(ret==JNI_TRUE) {
	    frgba = (*env)->GetFieldID(env, cls, "rgba", "Z");
	    if (frgba == 0) ret= JNI_FALSE;
	    else jrgba =(*env)->GetBooleanField(env, obj, frgba);
    }

    if(ret==JNI_TRUE) {
	    fstencilBits = (*env)->GetFieldID(env, cls, "stencilBits", "I");
	    if (fstencilBits == 0) ret= JNI_FALSE;
	    else jstencilBits =(*env)->GetIntField(env, obj, fstencilBits);
    }

    if(ret==JNI_TRUE) {
	    faccumSize = (*env)->GetFieldID(env, cls, "accumSize", "I");
	    if (faccumSize == 0) ret= JNI_FALSE;
	    else jaccumSize =(*env)->GetIntField(env, obj, faccumSize);
    }

    if(ret==JNI_TRUE) {
	    fcreatewinw = (*env)->GetFieldID(env, cls, "createwinw", "I");
	    if (fcreatewinw == 0) ret= JNI_FALSE;
	    else jcreatewinw =(*env)->GetIntField(env, obj, fcreatewinw);
    }

    if(ret==JNI_TRUE) {
	    fcreatewinh = (*env)->GetFieldID(env, cls, "createwinh", "I");
	    if (fcreatewinh == 0) ret= JNI_FALSE;
	    else jcreatewinh =(*env)->GetIntField(env, obj, fcreatewinh);
    }

    if(ret==JNI_TRUE) {
	    fownwind = (*env)->GetFieldID(env, cls, "createOwnWindow", "Z");
	    if (fownwind == 0) ret= JNI_FALSE;
	    else jownwind =(*env)->GetBooleanField(env, obj, fownwind);
    }

    if(ret==JNI_TRUE) {
	    fshareWith = (*env)->GetFieldID(env, cls, "sharedGLContextNative", "J");
	    if (fshareWith == 0) ret= JNI_FALSE;
	    else jshareWith = (GLXContext)
		( (PointerHolder) (*env)->GetLongField(env, obj, fshareWith) );
    }


    if(ret==JNI_TRUE) {
	    fwindowHandle = (*env)->GetFieldID(env, cls, "windowHandle", "J");
	    if (fwindowHandle == 0) ret= JNI_FALSE;
	    else 
	      thisWin =(Window) 
	      ( (PointerHolder) (*env)->GetLongField(env, obj, fwindowHandle) );
    }

    if(ret==JNI_TRUE) {
	    fpixmapHandle = (*env)->GetFieldID(env, cls, "pixmapHandle", "J");
	    if (fpixmapHandle == 0) ret= JNI_FALSE;
    }

    if(joffScreenRenderer==JNI_TRUE)
    {
	    jownwind = JNI_FALSE;
	    thisWin=0;
    }

    if(thisWin==0 && joffScreenRenderer==JNI_FALSE)
    {
	  fprintf(stderr,"\nGL4Java ERROR: given native window == NULL !\n");
	  fflush(stderr);
	  ret=JNI_FALSE;
    }

    if(JNI_TRUE==verbose)
    {
	fprintf(stderr,"\nGL4Java:  (JAVA FOUND WINDOW HANDLE 0x%p)\n", 
	    (void *)((PointerHolder)thisWin));
	if(joffScreenRenderer==JNI_TRUE)
	{
	  fprintf(stderr,"\nGL4Java:  (USING OFFSCREEN GLPIXMAP BUFFER,\n\t forced: !ownWindow, window=NULL)\n");
	}
	fflush(stderr);
    }

    if(JNI_TRUE==verbose)
    {
	fprintf(stderr,"\nGL4Java:  (try to use visuals: doubleBuffer=%d, stereoView=%d, rgba=%d, stencilBits=%d, accumSize=%d, ownWindow=%d)\n)\n",
	    (int)jdoubleBuffer, (int)jstereoView, (int)jrgba, (int)jstencilBits, (int)jaccumSize,
	    (int)jownwind );
	fflush(stderr);
    }

    if(ret==JNI_TRUE) {
	    fdisplayHandle = (*env)->GetFieldID(env, cls, "displayHandle", "J");
	    if (fdisplayHandle == 0) ret= JNI_FALSE;
    }

    if(ret==JNI_TRUE) {
	    fglContext=(*env)->GetFieldID(env, cls, "glContext", "J");
	    if (fglContext == 0) ret= JNI_FALSE;
    }

	/*
	    if(ret==JNI_TRUE)
		XSetErrorHandler(x11gl_myErrorHandler);

	    if(ret==JNI_TRUE)
		XSetIOErrorHandler(x11gl_myIOErrorHandler);
	*/

    if(ret==JNI_TRUE)
	    display = XOpenDisplay( NULL );

    if(ret==JNI_TRUE && display == NULL ) {
        /* return FALSE to tell JAVA that we couldn't open.  could also use
         * SignalError() here if we would prefer to throw an exception
         */
	 fprintf(stderr, "GL4Java ERROR: Can not open Display\n");
	 fflush(stderr);
         ret = JNI_FALSE;
    }

    /* Check to see if the Xserver supports OpenGL */
    if(ret==JNI_TRUE) {
      if( !glXQueryExtension(display, (int *) 0, (int *) 0) ) {
	fprintf(stderr, "GL4Java ERROR: Can not query glx extension -> Server does not support OpenGL\n");
	fflush(stderr);
        ret = JNI_FALSE;
      } else {
	if (JNI_TRUE==verbose) {
		fprintf(stdout, "GLX by %s Version %s\n", 
			glXGetClientString(display, GLX_VENDOR),
			glXGetClientString(display, GLX_VERSION));
	}
      }
    }

    /* initialize the x stuff */
    if(ret==JNI_TRUE)
    {
	    screen = DefaultScreen( display );
            rootwini  = RootWindow(display,screen) ;

	    glXMakeCurrent(display, None, NULL);

            vgc = findVisualGlX( display, rootwini, &thisWin,
	                         (int)jcreatewinw, (int)jcreatewinh, 
                                 jdoubleBuffer, jstereoView, jrgba, 
				 jstencilBits, jaccumSize,
				 &jownwind, jshareWith,
				 joffScreenRenderer, &pix, verbose);

	    if(vgc.success == 0 && jrgba==JNI_TRUE)
	    {
	        jrgba=JNI_FALSE;
                vgc = findVisualGlX( display, rootwini, &thisWin,
	                             (int)jcreatewinw, (int)jcreatewinh, 
                                     jdoubleBuffer, jstereoView, jrgba, 
				     jstencilBits, jaccumSize,
				     &jownwind, jshareWith,
				     joffScreenRenderer, &pix, verbose);
	    }

	    if(vgc.success == 0)
	    {
	         fprintf(stderr,"GL4Java ERROR: GETTING GC FAILED\n");
	         fflush(stderr);

		 if(jownwind==JNI_TRUE && thisWin!=0)
			 XDestroyWindow( display, thisWin );
		 if(joffScreenRenderer==JNI_TRUE && pix!=0)
			 XFreePixmap(display, pix);
		 XCloseDisplay( display );
		 ret = JNI_FALSE;
	    } else {
	    	 if(glXGetConfig( display, vgc.visual, 
		                  GLX_DOUBLEBUFFER, &iValue)==0)
		 {
	                if (JNI_TRUE==verbose) {
				fprintf(stdout,"doubleBuffer: %d\n", iValue);
				fflush(stdout);
			}
			jdoubleBuffer=iValue?JNI_TRUE:JNI_FALSE;
		        if(ret==JNI_TRUE && fdoubleBuffer!=0) {
			    (*env)->SetBooleanField(env, obj, fdoubleBuffer, 
			                            jdoubleBuffer);
		        }

		 } else {
		 	fprintf(stderr,"GL4Java: fetching doubleBuffer state failed\n");
		        fflush(stderr);
		 }
	    	 if(glXGetConfig( display, vgc.visual, 
		                  GLX_STEREO, &iValue)==0)
		 {
	                if (JNI_TRUE==verbose) {
				fprintf(stdout,"stereoView: %d\n", iValue);
				fflush(stdout);
			}
			jstereoView=iValue?JNI_TRUE:JNI_FALSE;
		        if(ret==JNI_TRUE && fstereoView!=0) {
			    (*env)->SetBooleanField(env, obj, fstereoView, 
			                            jstereoView);
		        }

		 } else {
		 	fprintf(stderr,"GL4Java: fetching stereoView state failed\n");
		        fflush(stderr);
		 }
	    	 if(glXGetConfig( display, vgc.visual, 
		                  GLX_RGBA, &iValue)==0)
		 {
	                if (JNI_TRUE==verbose) {
				fprintf(stdout,"rgba: %d\n", iValue);
				fflush(stdout);
			}
			jrgba=iValue?JNI_TRUE:JNI_FALSE;
		        if(ret==JNI_TRUE && frgba!=0) {
			    (*env)->SetBooleanField(env, obj, frgba, 
			                            jrgba);
		        }

		 } else {
		 	fprintf(stderr,"GL4Java: fetching rgba state failed\n");
		        fflush(stderr);
		 }
	    	 if(glXGetConfig( display, vgc.visual, 
		                  GLX_STENCIL_SIZE, &iValue)==0)
		 {
	                if (JNI_TRUE==verbose) {
				fprintf(stdout,"stencilBits: %d\n", iValue);
				fflush(stdout);
			}
			jstencilBits=iValue;
		        if(ret==JNI_TRUE && fstencilBits!=0) {
	                    (*env)->SetIntField(env, obj, 
	                                        fstencilBits, (jint)jstencilBits);
		        }

		 } else {
		 	fprintf(stderr,"GL4Java: fetching stencilBits state failed\n");
		        fflush(stderr);
		 }
	    	 if(glXGetConfig( display, vgc.visual,GLX_ACCUM_RED_SIZE, &iValue)==0 &&
	    	    glXGetConfig( display, vgc.visual,GLX_ACCUM_GREEN_SIZE, &iValue1)==0 &&
	    	    glXGetConfig( display, vgc.visual,GLX_ACCUM_BLUE_SIZE, &iValue2)==0 &&
	    	    glXGetConfig( display, vgc.visual,GLX_ACCUM_ALPHA_SIZE, &iValue3)==0 )
		 {
	                if (JNI_TRUE==verbose) {
				fprintf(stdout,"accumSize(red): %d\n", iValue);
				fprintf(stdout,"accumSize(green): %d\n", iValue1);
				fprintf(stdout,"accumSize(blue): %d\n", iValue2);
				fprintf(stdout,"accumSize(alpha): %d\n", iValue3);
				fflush(stdout);
			}
			jaccumSize=iValue+iValue1+iValue2+iValue3;
		        if(ret==JNI_TRUE && faccumSize!=0) {
	                    (*env)->SetIntField(env, obj, 
	                                        faccumSize, (jint)jaccumSize);
		        }

		 } else {
		 	fprintf(stderr,"GL4Java: fetching accumSize(red) state failed\n");
		        fflush(stderr);
		 }
		 if(JNI_TRUE==verbose)
		 {
			fprintf(stderr,"\nGL4Java: (using visuals: doubleBuffer=%d, stereoView=%d, rgba=%d, stencilBits=%d, accumSize=%d, ownWindow=%d)\n)\n",
			    (int)jdoubleBuffer, (int)jstereoView, (int)jrgba,
			    (int)jstencilBits, (int)jaccumSize, (int)jownwind);
			fflush(stderr);
		 }

	    }
    }

    /* we are now done with the visual so we should free the storage */

    if(ret==JNI_TRUE)
    {
	    XClearWindow( display, thisWin );
	    XMapWindow( display, thisWin );
	    XFlush( display ); 
    }

    if(ret==JNI_TRUE && fwindowHandle!=0) {
	    (*env)->SetLongField(env, obj, fwindowHandle, (jlong)((PointerHolder)thisWin));
    }

    if(ret==JNI_TRUE && fpixmapHandle!=0) {
	    (*env)->SetLongField(env, obj, fpixmapHandle, (jlong)((PointerHolder)pix));
    }

    if(ret==JNI_TRUE && fdisplayHandle) {
	    (*env)->SetLongField(env, obj, fdisplayHandle, (jlong)((PointerHolder)display));
    }

    if(ret==JNI_TRUE && fglContext) {
	    (*env)->SetLongField(env, obj, fglContext, (jlong)((PointerHolder)vgc.gc));
    }

    if(ret==JNI_TRUE && fownwind) {
	    (*env)->SetBooleanField(env, obj, fownwind, jownwind);
    }

    return ret;
}

JNIEXPORT void JNICALL
Java_gl4java_GLContext_gljResizeNative( JNIEnv *env, jobject obj,
	                                jboolean isOwnWindow,
				        jlong disp, jlong thisWin,
				        jint width, jint height)
{
    /* perform a X11 synchronise, because rendering could be done
     * by a native Thread .... So we have to try avoid gl* reentrance
     * on the same GL-Context
     */
    glXWaitGL();

    if(isOwnWindow)
    {
	   XResizeWindow((Display *)((PointerHolder)disp), 
	                 (Window)((PointerHolder)thisWin), width, height);
	   /* if(JNI_TRUE==verbose)
	   {
		fprintf(stderr, "XResizeWindow -> %d x %d\n", 
			(int)width, (int)height);
		fflush(stderr);
	   } */
    }

    return;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljMakeCurrentNative( JNIEnv *env, jobject obj,
					     jobject canvas,
					     jlong disp,
					     jlong thisWin,
					     jlong glContext
                                           )
{
    jboolean ret = JNI_TRUE;
    GLXContext ctx = NULL;

    (void)canvas;

    if(glContext==0)
    {
	fprintf(stderr, "GL4Java ERROR: gljUse NO actual GC was created ...\n");
	fflush(stderr);
	return JNI_FALSE;
    }

    ctx = glXGetCurrentContext();

    if(ctx==(GLXContext)((PointerHolder)glContext))
    	return JNI_TRUE;

    if(ret==JNI_TRUE)
    {
	    if( !glXMakeCurrent( (Display *)((PointerHolder)disp), 
	                         (Window)((PointerHolder)thisWin), 
	                         (GLXContext)((PointerHolder)glContext) ) ) 
	    {
   extern GLenum glGetError ( void ) ;
		fprintf(stderr, "GL4Java: gljMakeCurrent failed with GC\n  Another thread may be use it now ...\n");
		fflush(stderr);
		ret = JNI_FALSE;
	    } 
    }
    
    return ret;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljFreeNative( JNIEnv *env, jobject obj,
			              jobject canvas,
				      jlong disp,
				      jlong thisWin,
				      jlong glContext
				    )
{
    jboolean ret = JNI_TRUE;

    (void)thisWin;
    (void)glContext;
    (void)canvas;

    if(ret==JNI_TRUE)
    {
	    if( !glXMakeCurrent( (Display *)((PointerHolder)disp), None, NULL)) 
	    {
		fprintf(stderr, "GL4Java: gljFree failed\n");
		fflush(stderr);
		ret = JNI_FALSE;
	    }  
    }
    return ret;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljIsContextCurrentNative( JNIEnv *env, jobject obj,
				      jlong glContext
				    )
{
    GLXContext ctx = glXGetCurrentContext();

    if(ctx==(GLXContext)((PointerHolder)glContext))
    	return JNI_TRUE;

    return JNI_FALSE;
}


JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljDestroyNative( JNIEnv *env, jobject obj,
					 jobject canvas )
{
    jclass cls = 0;
    jfieldID fdisplayHandle=0, fwindowHandle=0, fglContext=0;
    jfieldID fpixmapHandle=0;
    jfieldID fpData=0;
    jfieldID fownwind=0;

    jboolean jownwind = JNI_FALSE ;
    Display *disp=0;
    GLXContext gc=0;
    Window win=0;
    jlong pData=0;
    Pixmap pix=0;

    jboolean ret = JNI_TRUE;

    (void) canvas;

    cls = (*env)->GetObjectClass(env, obj);
    if(cls==0) ret=JNI_FALSE;

    if(ret==JNI_TRUE) {
	    fwindowHandle = (*env)->GetFieldID(env, cls, "windowHandle", "J");
	    if (fwindowHandle == 0) ret= JNI_FALSE;
	    else win = (Window) 
	      ( (PointerHolder) (*env)->GetLongField(env, obj, fwindowHandle) );
    }

    if(ret==JNI_TRUE) {
	    fdisplayHandle = (*env)->GetFieldID(env, cls, "displayHandle", "J");
	    if (fdisplayHandle == 0) ret= JNI_FALSE;
	    else disp=(Display *)
	      ( (PointerHolder) (*env)->GetLongField(env, obj, fdisplayHandle));
    }

    if(ret==JNI_TRUE) {
	    fglContext=(*env)->GetFieldID(env, cls, "glContext", "J");
	    if (fglContext == 0) ret= JNI_FALSE;
	    else gc =(GLXContext)
	      ( (PointerHolder) (*env)->GetLongField(env, obj, fglContext) );
    }

    if(ret==JNI_TRUE) {
	fpData = (*env)->GetFieldID(env, cls, "pData", "J");
	if (fpData == 0) ret= JNI_FALSE;
	else pData =(*env)->GetLongField(env, obj, fpData);
    }

    if(ret==JNI_TRUE) {
	    fpixmapHandle = (*env)->GetFieldID(env, cls, "pixmapHandle", "J");
	    if (fpixmapHandle == 0) ret= JNI_FALSE;
	    else pix = (Pixmap) 
	     ( (PointerHolder) (*env)->GetLongField(env, obj, fpixmapHandle));
    }

    if(ret==JNI_TRUE) {
	    fownwind = (*env)->GetFieldID(env, cls, "createOwnWindow", "Z");
	    if (fownwind == 0) ret= JNI_FALSE;
	    else jownwind =(*env)->GetBooleanField(env, obj, fownwind);
    }

    glXWaitGL();

    if(ret==JNI_TRUE)
    {
	    if ( gc == 0 )
	    {
	        if(JNI_TRUE==verbose)
		{
			fprintf(stderr, "GL4Java: gljDestroy failed, GL context is 0\n");
			fflush(stderr);
		}
	    }
	    glXMakeCurrent( disp, None, NULL );

	    if(ret==JNI_TRUE) 
	    {
	    	if(gc!=0)
			glXDestroyContext(disp, gc);
	        if(pix!=0)
	        {
		    if(win!=0)
			    glXDestroyGLXPixmap(disp, win);
		    win=0;
		    XFreePixmap(disp, pix);
	        }
		if(jownwind && win!=0)
		{
		    XDestroyWindow(disp, win);
		    win=0;
		}
	    }
    }

    if(ret==JNI_TRUE) 
    {
	    gc = 0;
	    disp=0;
	    win = 0;
	    pix = 0;
	    pData=0; /* remark the class, we must reinit GL-Context ! */
    }

    if(ret==JNI_TRUE && fpixmapHandle!=0) {
	    (*env)->SetLongField(env, obj, fpixmapHandle, (jlong)((PointerHolder)pix));
    }

    if(ret==JNI_TRUE && fwindowHandle!=0) {
	    (*env)->SetLongField(env, obj, fwindowHandle, (jlong)((PointerHolder)win));
    }

    if(ret==JNI_TRUE && fdisplayHandle) {
	    (*env)->SetLongField(env, obj, fdisplayHandle, (jlong)((PointerHolder)disp));
    }

    if(ret==JNI_TRUE && fglContext) {
	    (*env)->SetLongField(env, obj, fglContext, (jlong)((PointerHolder)gc));
    }


    if(ret==JNI_TRUE && fpData) {
	    (*env)->SetLongField(env, obj, fpData, pData);
    }

    if(ret==JNI_TRUE && fownwind) {
	    (*env)->SetBooleanField(env, obj, fownwind, jownwind);
    }
    return ret;
}

JNIEXPORT jboolean JNICALL
Java_gl4java_GLContext_gljSwapNative( JNIEnv *env, jobject obj,
				      jlong disp,
				      jlong thisWin,
				      jlong glContext,
				      jboolean doubleBuffer
				    )
{
    (void)glContext;

    if( doubleBuffer == JNI_FALSE ) {
	/* don't double buffer */
	glXWaitGL();
    } else {
	glXSwapBuffers( (Display *)((PointerHolder)disp), 
	                (Window)((PointerHolder)thisWin) );
    }

    return JNI_TRUE;
}