-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinxlib.c
1380 lines (1241 loc) · 52 KB
/
inxlib.c
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
INXlib v0.6
A simple skeleton framework for building X11 windowed applications with XLib.
It includes an OpenGL context for 3D graphics.
Copyright 2016-2024 Ioannis Nompelis
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "inxlib.h"
/**
// @details
//
// Function to initialize all callbacks to NULL first, and to invoke a user
// function that set the callbacks based on custom (user-defined) functions.
// @author Ioannis Nompelis <[email protected]>
*/
int xwindow_callbacks( struct my_xwin_vars *xvars )
{
int iret;
xvars->callback_Expose = NULL;
xvars->callback_ConfigureNotify = NULL;
xvars->callback_MapNotify = NULL;
xvars->callback_KeyPress = NULL;
xvars->callback_KeyRelease = NULL;
xvars->callback_ButtonPress = NULL;
xvars->callback_ButtonRelease = NULL;
xvars->callback_MotionNotify = NULL;
xvars->callback_EnterNotify = NULL;
xvars->callback_FocusIn = NULL;
xvars->callback_FocusOut = NULL;
//
// call the function that the user provides
//
iret = xwindow_user( xvars );
return( iret );
}
/**
// @details
//
// Function to create and bring up an X window with an associated OpenGL
// rendering context
//
// @author Ioannis Nompelis <[email protected]>
*/
int xwindow_setup( struct my_xwin_vars *xvars,
int width, int height, int xpos, int ypos,
int ithread, int irender, int iframe )
{
int ierr;
XVisualInfo *visinfo;
#ifndef _OLDSTYLE_
GLXFBConfig *fbconfig;
#endif
XSetWindowAttributes win_attr;
unsigned long attr_mask;
// the following array will be used for initialization
#ifdef _OLDSTYLE_
int glx_attr[] ={ GLX_RGBA,
GLX_RED_SIZE, 8, // number signifies bits
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, True,
None }; // this line terminates the list
#else
int glx_attr[] ={
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, True,
// GLX_SAMPLE_BUFFERS , 1,
// GLX_SAMPLES , 4,
None }; // this line terminates the list
#endif
//
// Initialize Xlib in multi-thread mode if requested
// (this seems to be necessary if mutliple threads will use Xlib, but if
// the library is used by an application that has potentially made this
// call before, we cannot make this call. We give a choice to use users.)
//
if( ithread == 0 ) {
ierr = XInitThreads();
if( ierr == 0 ) {
fprintf( stderr, " [Error] Could not start in multi-thread mode\n" );
return 1;
}
}
//
// make a connection to the X server
// (we use the user's default display from the environment variable)
//
xvars->xdisplay = XOpenDisplay( getenv("DISPLAY") );
if( xvars->xdisplay == NULL ) {
fprintf( stderr, " [Error] Failed to open display.\n" );
return 2;
}
//
// echo the number of screens on this X display
//
fprintf( stderr, " [INFO] The display has %d available screen(s)\n",
ScreenCount( xvars->xdisplay ) );
//
// get the default screen number (we will open our window in it) and get
// the root window
//
xvars->xscreen = DefaultScreen( xvars->xdisplay );
xvars->xroot = RootWindow( xvars->xdisplay, xvars->xscreen );
#ifndef _OLDSTYLE_
//
// retrieve a framebuffer configuration
//
int fbcount;
fbconfig = glXChooseFBConfig( xvars->xdisplay, xvars->xscreen, glx_attr,
&fbcount );
if( !fbconfig ) {
fprintf( stderr, " [Error] Failed to retrieve a framebuffer config\n" );
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 3;
} else {
fprintf( stderr, " [INFO] Number of FB config: %d \n", fbcount );
}
#endif
//
// allocate a structure to store the X display's visual info
// from the attributes we requested
//
#ifdef _OLDSTYLE_
visinfo = glXChooseVisual( xvars->xdisplay, xvars->xscreen, glx_attr );
#else
visinfo = glXGetVisualFromFBConfig( xvars->xdisplay, fbconfig[0] );
#endif
if( !visinfo ) {
fprintf( stderr, " [Error] Unable to find RGB, double-buffer visual\n" );
// close connection to the Xserver
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 4;
}
//
// set position and size of the X window (use sizes from the screen as guide)
//
if( width == -1 ) { // user has no preference
xvars->win_width = WidthOfScreen(ScreenOfDisplay( xvars->xdisplay,
xvars->xscreen )) * 3/4;
xvars->win_height = HeightOfScreen(ScreenOfDisplay( xvars->xdisplay,
xvars->xscreen )) * 3/4;
xvars->win_xpos = xvars->win_width / 4;
xvars->win_ypos = xvars->win_height / 4;
} else {
xvars->win_width = (unsigned int) width;
xvars->win_height = (unsigned int) height;
xvars->win_xpos = xpos;
xvars->win_ypos = ypos;
}
//
// Set attributes of window and the attribute mask
// Attributes include the choice of a movable window or frameless/static
// one, and X events to be trapped by the window (mouse clicks, motion,
// etc.) The attribute mask lets the API know of which of the attributes
// we provided.
//
win_attr.background_pixmap = None;
win_attr.background_pixel = 0;
win_attr.border_pixel = 2;
win_attr.colormap =
XCreateColormap( xvars->xdisplay, xvars->xroot, visinfo->visual,
AllocNone );
win_attr.cursor = None; // May point to an allocated special cursor
win_attr.override_redirect = False;
if( iframe == 1 ) {
win_attr.override_redirect = True; // this makes the window solid!
}
win_attr.event_mask = StructureNotifyMask |
ExposureMask |
FocusChangeMask |
PointerMotionMask |
ButtonPressMask |
ButtonReleaseMask |
KeyPressMask |
KeyReleaseMask;
attr_mask = CWBackPixel |
CWBorderPixel |
CWColormap |
CWEventMask |
CWOverrideRedirect;
//
// create the X window
//
xvars->xwindow = XCreateWindow( xvars->xdisplay, xvars->xroot,
xvars->win_xpos, xvars->win_ypos,
xvars->win_width, xvars->win_height, 0,
visinfo->depth, InputOutput,
visinfo->visual, attr_mask, &win_attr );
if( xvars->xwindow <= 0 ) {
fprintf( stderr, " [Error] Unable to create window \n" );
#ifndef _OLDSTYLE_
// drop FB configuration object(s)
XFree( fbconfig );
#endif
// drop visual info
XFree( visinfo );
// close connection to the Xserver
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 5;
}
XStoreName( xvars->xdisplay, xvars->xwindow, xvars->window_name );
//
// Specify events to be sent to the program from the window
//
XSelectInput( xvars->xdisplay, xvars->xwindow,
ExposureMask |
StructureNotifyMask |
FocusChangeMask |
PointerMotionMask |
ButtonPressMask |
ButtonReleaseMask |
KeyPressMask |
KeyReleaseMask);
//
// Create the window and bring it up
// (An X window can be created and not be displayed until needed.)
//
XMapWindow( xvars->xdisplay, xvars->xwindow );
XMapRaised( xvars->xdisplay, xvars->xwindow );
XFlush( xvars->xdisplay );
//
// Associate this window with an OpenGL context (create the context)
// Here we make the selection of whether we want direct rendering (with
// hardware acceleration) or indirect rendering (with software).
//
#ifdef _OLDSTYLE_
if( irender == 1 ) {
xvars->glxc = glXCreateContext( xvars->xdisplay, visinfo, NULL, True );
} else {
xvars->glxc = glXCreateContext( xvars->xdisplay, visinfo, NULL, False );
}
#else
//
// Create a modern OpenGL context
//
typedef GLXContext (*my_func)( Display*, GLXFBConfig, GLXContext,
Bool, const int* );
my_func glXCreateContextAttribsARB = NULL;
glXCreateContextAttribsARB = (my_func)
glXGetProcAddressARB( (const GLubyte*)"glXCreateContextAttribsARB" );
// This library does _not_ use the latest versions of the "core" profile
// because it uses display lists. This compromise allows for the modern
// programmable pipeline to be used _and_ the fixed pipeline rendering!
int context_attribs[] = {
// GLX_CONTEXT_MAJOR_VERSION_ARB, 4, // Core profile (DO NOT USE)
// GLX_CONTEXT_MINOR_VERSION_ARB, 5,
// GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_MAJOR_VERSION_ARB, 3, // Compatibility profile
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
None
};
xvars->glxc = glXCreateContextAttribsARB( xvars->xdisplay,
fbconfig[0], NULL, True,
context_attribs );
#endif
if( xvars->glxc == NULL ) {
fprintf( stderr, " [Error] Could not create GLX context!\n" );
#ifndef _OLDSTYLE_
XFree( fbconfig );
#endif
XFree( visinfo );
XDestroyWindow( xvars->xdisplay, xvars->xwindow );
xvars->xwindow = 0;
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 6;
}
// make sure the second GLX context is unuseable
xvars->glxc2 = NULL;
#ifndef _OLDSTYLE_
//
// make a GLX window (this is for high-performance applications)
//
xvars->glxwin = glXCreateWindow( xvars->xdisplay, fbconfig[0],
xvars->xwindow, NULL );
if( !(xvars->glxwin) ) {
fprintf( stderr, " [Error] Could not create a GLX area! \n" );
XFree( fbconfig );
XFree( visinfo );
glXDestroyContext( xvars->xdisplay, xvars->glxc );
glXDestroyContext( xvars->xdisplay, xvars->glxc2 );
XDestroyWindow( xvars->xdisplay, xvars->xwindow );
xvars->xwindow = 0;
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 8;
}
#endif
//
// make this the current OpenGL context (does not harm)
//
#ifndef _OLDSTYLE_
glXMakeContextCurrent( xvars->xdisplay, xvars->glxwin, xvars->glxwin,
xvars->glxc );
#else
glXMakeCurrent( xvars->xdisplay, xvars->xwindow, xvars->glxc );
#endif
//
// Drop structures we no longer need
//
#ifndef _OLDSTYLE_
XFree( fbconfig );
#endif
XFree( visinfo );
//
// Set some variables that may be unsued
//
#ifndef _OLDSTYLE_
xvars->pbuffer = 0;
xvars->glxcoff = 0;
#endif
//
// Set the internal variable for user-guided termination of the library
//
xvars->iterm_loop = 0;
return 0;
}
/**
// @details
//
// Function to create and bring up an X window with two associated OpenGL
// rendering contexts that share resources
//
// @author Ioannis Nompelis <[email protected]>
*/
int xwindow_setup_dualglx( struct my_xwin_vars *xvars,
int width, int height, int xpos, int ypos,
int iframe )
{
int ierr;
XVisualInfo *visinfo;
#ifndef _OLDSTYLE_
GLXFBConfig *fbconfig;
#endif
XSetWindowAttributes win_attr;
unsigned long attr_mask;
// the following array will be used for initialization
#ifdef _OLDSTYLE_
int glx_attr[] ={ GLX_RGBA,
GLX_RED_SIZE, 8, // number signifies bits
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, True,
None }; // this line terminates the list
#else
int glx_attr[] ={
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE,
GLX_WINDOW_BIT,
GLX_RENDER_TYPE,
GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE,
GLX_TRUE_COLOR,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
GLX_DOUBLEBUFFER, True,
// GLX_SAMPLE_BUFFERS , 1,
// GLX_SAMPLES , 4,
None }; // this line terminates the list
#endif
//
// Initialize Xlib in multi-thread mode
// (This is absolutely needed if the user is to use this as intended.)
//
ierr = XInitThreads();
if( ierr == 0 ) {
fprintf( stderr, " [Error] Could not start in multi-thread mode\n" );
return 1;
}
//
// make a connection to the X server
// (we use the user's default display from the environment variable)
//
xvars->xdisplay = XOpenDisplay( getenv("DISPLAY") );
if( xvars->xdisplay == NULL ) {
fprintf( stderr, " [Error] Failed to open display.\n" );
return 2;
}
//
// echo the number of screens on this X display
//
fprintf( stderr, " [INFO] The display has %d available screen(s)\n",
ScreenCount( xvars->xdisplay ) );
//
// get the default screen number (we will open our window in it) and get
// the root window
//
xvars->xscreen = DefaultScreen( xvars->xdisplay );
xvars->xroot = RootWindow( xvars->xdisplay, xvars->xscreen );
#ifndef _OLDSTYLE_
//
// retrieve a framebuffer configuration
//
int fbcount;
fbconfig = glXChooseFBConfig( xvars->xdisplay, xvars->xscreen, glx_attr,
&fbcount );
if( !fbconfig ) {
fprintf( stderr, " [Error] Failed to retrieve a framebuffer config\n" );
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 3;
} else {
fprintf( stderr, " [INFO] Number of FB config: %d \n", fbcount );
}
#endif
//
// allocate a structure to store the X display's visual info
// from the attributes we requested
//
#ifdef _OLDSTYLE_
visinfo = glXChooseVisual( xvars->xdisplay, xvars->xscreen, glx_attr );
#else
visinfo = glXGetVisualFromFBConfig( xvars->xdisplay, fbconfig[0] );
#endif
if( !visinfo ) {
fprintf( stderr, " [Error] Unable to find RGB, double-buffer visual\n" );
// close connection to the Xserver
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 4;
}
//
// set position and size of the X window (use sizes from the screen as guide)
//
if( width == -1 ) { // user has no preference
xvars->win_width = WidthOfScreen(ScreenOfDisplay( xvars->xdisplay,
xvars->xscreen )) * 3/4;
xvars->win_height = HeightOfScreen(ScreenOfDisplay( xvars->xdisplay,
xvars->xscreen )) * 3/4;
xvars->win_xpos = xvars->win_width / 4;
xvars->win_ypos = xvars->win_height / 4;
} else {
xvars->win_width = (unsigned int) width;
xvars->win_height = (unsigned int) height;
xvars->win_xpos = xpos;
xvars->win_ypos = ypos;
}
//
// Set attributes of window and the attribute mask
// Attributes include the choice of a movable window or frameless/static
// one, and X events to be trapped by the window (mouse clicks, motion,
// etc.) The attribute mask lets the API know of which of the attributes
// we provided.
//
win_attr.background_pixmap = None;
win_attr.background_pixel = 0;
win_attr.border_pixel = 2;
win_attr.colormap =
XCreateColormap( xvars->xdisplay, xvars->xroot, visinfo->visual,
AllocNone );
win_attr.cursor = None; // May point to an allocated special cursor
win_attr.override_redirect = False;
if( iframe == 1 ) {
win_attr.override_redirect = True; // this makes the window solid!
}
win_attr.event_mask = StructureNotifyMask |
ExposureMask |
FocusChangeMask |
PointerMotionMask |
ButtonPressMask |
ButtonReleaseMask |
KeyPressMask |
KeyReleaseMask;
attr_mask = CWBackPixel |
CWBorderPixel |
CWColormap |
CWEventMask |
CWOverrideRedirect;
//
// create the X window
//
xvars->xwindow = XCreateWindow( xvars->xdisplay, xvars->xroot,
xvars->win_xpos, xvars->win_ypos,
xvars->win_width, xvars->win_height, 0,
visinfo->depth, InputOutput,
visinfo->visual, attr_mask, &win_attr );
if( xvars->xwindow <= 0 ) {
fprintf( stderr, " [Error] Unable to create window \n" );
#ifndef _OLDSTYLE_
// drop FB configuration object(s)
XFree( fbconfig );
#endif
// drop visual info
XFree( visinfo );
// close connection to the Xserver
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 5;
}
XStoreName( xvars->xdisplay, xvars->xwindow, xvars->window_name );
//
// Specify events to be sent to the program from the window
//
XSelectInput( xvars->xdisplay, xvars->xwindow,
ExposureMask |
StructureNotifyMask |
FocusChangeMask |
PointerMotionMask |
ButtonPressMask |
ButtonReleaseMask |
KeyPressMask |
KeyReleaseMask);
//
// Create the window and bring it up
// (An X window can be created and not be displayed until needed.)
//
XMapWindow( xvars->xdisplay, xvars->xwindow );
XMapRaised( xvars->xdisplay, xvars->xwindow );
XFlush( xvars->xdisplay );
//
// Associate this window with an OpenGL context (create the context)
// Here we make the selection of whether we want direct rendering (with
// hardware acceleration) or indirect rendering (with software).
//
#ifdef _OLDSTYLE_
xvars->glxc = glXCreateContext( xvars->xdisplay, visinfo, NULL, True );
#else
//
// Create a modern OpenGL context
//
typedef GLXContext (*my_func)( Display*, GLXFBConfig, GLXContext,
Bool, const int* );
my_func glXCreateContextAttribsARB = NULL;
glXCreateContextAttribsARB = (my_func)
glXGetProcAddressARB( (const GLubyte*)"glXCreateContextAttribsARB" );
// This library does _not_ use the latest versions of the "core" profile
// because it uses display lists. This compromise allows for the modern
// programmable pipeline to be used _and_ the fixed pipeline rendering!
int context_attribs[] = {
// GLX_CONTEXT_MAJOR_VERSION_ARB, 4, // Core profile (DO NOT USE)
// GLX_CONTEXT_MINOR_VERSION_ARB, 5,
// GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
GLX_CONTEXT_MAJOR_VERSION_ARB, 3, // Compatibility profile
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
None
};
xvars->glxc = glXCreateContextAttribsARB( xvars->xdisplay,
fbconfig[0], NULL, True,
context_attribs );
#endif
if( xvars->glxc == NULL ) {
fprintf( stderr, " [Error] Could not create GLX context!\n" );
#ifndef _OLDSTYLE_
XFree( fbconfig );
#endif
XFree( visinfo );
XDestroyWindow( xvars->xdisplay, xvars->xwindow );
xvars->xwindow = 0;
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 6;
}
#ifdef _OLDSTYLE_
xvars->glxc2 = glXCreateContext( xvars->xdisplay, visinfo, xvars->glxc,
True );
#else
xvars->glxc2 = glXCreateContextAttribsARB( xvars->xdisplay,
fbconfig[0], xvars->glxc, True,
context_attribs );
#endif
if( xvars->glxc2 == NULL ) {
fprintf( stderr, " [Error] Could not create 2nd GLX context!\n" );
#ifndef _OLDSTYLE_
XFree( fbconfig );
#endif
XFree( visinfo );
glXDestroyContext( xvars->xdisplay, xvars->glxc );
XDestroyWindow( xvars->xdisplay, xvars->xwindow );
xvars->xwindow = 0;
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 7;
}
#ifndef _OLDSTYLE_
//
// make a GLX window (this is for high-performance applications)
//
xvars->glxwin = glXCreateWindow( xvars->xdisplay, fbconfig[0],
xvars->xwindow, NULL );
if( !(xvars->glxwin) ) {
fprintf( stderr, " [Error] Could not create a GLX area! \n" );
XFree( fbconfig );
XFree( visinfo );
glXDestroyContext( xvars->xdisplay, xvars->glxc );
glXDestroyContext( xvars->xdisplay, xvars->glxc2 );
XDestroyWindow( xvars->xdisplay, xvars->xwindow );
xvars->xwindow = 0;
XCloseDisplay( xvars->xdisplay );
xvars->xdisplay = NULL;
return 8;
}
#endif
//
// make this the current OpenGL context (does not harm)
//
#ifndef _OLDSTYLE_
glXMakeContextCurrent( xvars->xdisplay, xvars->glxwin, xvars->glxwin,
xvars->glxc );
#else
glXMakeCurrent( xvars->xdisplay, xvars->xwindow, xvars->glxc );
#endif
//
// Drop structures we no longer need
//
#ifndef _OLDSTYLE_
XFree( fbconfig );
#endif
XFree( visinfo );
//
// Set some variables that may be unsued
//
#ifndef _OLDSTYLE_
xvars->pbuffer = 0;
xvars->glxcoff = 0;
#endif
//
// Set the internal variable for user-guided termination of the library
//
xvars->iterm_loop = 0;
return 0;
}
#ifndef _OLDSTYLE_
/**
// @details
//
// Function to create an off-screen rendering drawable with associated OpenGL
// rendering context
//
// @author Ioannis Nompelis <[email protected]>
*/
int xwindow_setup_offscreen( struct my_xwin_vars *xvars,
unsigned int width, unsigned int height )
{
// for requesting a framebuffer configuration
int fboff_attr[] ={
GLX_X_RENDERABLE, True,
GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8,
GLX_BLUE_SIZE, 8,
GLX_ALPHA_SIZE, 8,
GLX_DEPTH_SIZE, 24,
GLX_STENCIL_SIZE, 8,
None }; // this line terminates the list
//
// retrieve a framebuffer configuration
//
int fbcount;
GLXFBConfig *fbconfig = glXChooseFBConfig( xvars->xdisplay, xvars->xscreen,
fboff_attr, &fbcount );
if( !fbconfig ) {
fprintf( stderr, " [Error] Failed to retrieve a framebuffer config\n" );
return 3;
} else {
fprintf( stderr, " [INFO] Number of FB config: %d \n", fbcount );
}
//
// create a pbuffer
//
static int pbuffer_attribs[] = {
GLX_PBUFFER_WIDTH, 800,
GLX_PBUFFER_HEIGHT, 600,
None
};
// over-write with non-constant numbers
pbuffer_attribs[1] = width;
pbuffer_attribs[3] = height;
xvars->pbuffer = glXCreatePbuffer( xvars->xdisplay, fbconfig[0],
pbuffer_attribs );
if( !(xvars->pbuffer) ) {
fprintf( stderr, " [Error] Unable to create Pbuffer \n" );
return 4;
}
//
// create a GLX context just for the pbuffer
//
xvars->glxcoff = glXCreateNewContext( xvars->xdisplay, fbconfig[0],
GLX_RGBA_TYPE,
xvars->glxc, True );
if( xvars->glxcoff == NULL ) {
fprintf( stderr, " [Error] Could not create GLX context!\n" );
XFree( fbconfig );
return 6;
}
//
// Drop structures we no longer need
//
XFree( fbconfig );
xvars->pb_width = width;
xvars->pb_height = height;
#ifdef _DEBUG_
fprintf( stderr, " [INFO] Off-screen FB: %d x %d (handle %ld) \n",
xvars->pb_width, xvars->pb_height,
xvars->pbuffer );
#endif
return 0;
}
#endif
void xwindow_query_glxversion( struct my_xwin_vars *xvars )
{
int glxMajor, glxMinor;
if( glXQueryVersion( xvars->xdisplay, &glxMajor, &glxMinor) ) {
fprintf( stdout, " [INFO] GLX Version: %d.%d\n", glxMajor, glxMinor );
} else {
fprintf( stdout, " [Error] Failed to query GLX version\n" );
}
}
/**
// @details
//
// Function to close an X window with an OpenGL context and close the connection
// to the X server
//
// @author Ioannis Nompelis <[email protected]>
*/
int xwindow_close( struct my_xwin_vars *xvars )
{
#ifndef _OLDSTYLE_
//
// Destroy the Pbuffer and its OpenGL context if they exist
//
if( xvars->pbuffer != 0 ) {
glXDestroyPbuffer( xvars->xdisplay, xvars->pbuffer );
glXDestroyContext( xvars->xdisplay, xvars->glxcoff );
fprintf( stderr, " [INFO] Released Pbuffer and its GLX context \n" );
}
#endif
//
// Destroy the OpenGL context
//
glXMakeCurrent( xvars->xdisplay, None, NULL );
if( xvars->glxc2 != NULL ) {
glXDestroyContext( xvars->xdisplay, xvars->glxc2 );
fprintf( stderr, " [INFO] Released 2nd GLX context \n" );
}
glXDestroyContext( xvars->xdisplay, xvars->glxc );
fprintf( stderr, " [INFO] Released GLX context \n" );
xvars->glxc = NULL;
#ifndef _OLDSTYLE_
//
// Destroy GLX area
//
glXDestroyWindow( xvars->xdisplay, xvars->glxwin );
fprintf( stderr, " [INFO] Destroyed GLX area \n" );
xvars->glxwin = 0;
#endif
//
// Destroy window
//
XDestroyWindow( xvars->xdisplay, xvars->xwindow );
fprintf( stderr, " [INFO] Destroyed X window \n" );
xvars->xwindow = 0;
//
// Close connection to X server
//
XCloseDisplay( xvars->xdisplay );
fprintf( stderr, " [INFO] Closed connection to the X server \n" );
xvars->xdisplay = NULL;
return 0;
}
/**
// @details
//
// Function that is up to the user to write. It decides termination in this case
//
// @author Ioannis Nompelis <[email protected]>
*/
int react_to_key_press(int ikey, int ishift, int ictrl, int ialt ) {
// return a termination flag if CTRL-Escape is pressed
if( ikey == XK_Escape && ictrl > 0 ) {
fprintf( stderr, " [INFO] The \"Escape\" key is pressed.\n" );
return -1;
} else {
return 0;
}
}
/**
// @details
//
// Function to handle X events that are trapped by an appropriately setup
// X window. It contains a mechanism for graceful exit. This function is meant
// to trap events, pass them to handler functions, and assume that they will be
// handled; handling is done by the software that uses this library as a front
// end. This function can act on its own on top of "sending" all events for
// handling, and this is the case here for illustration purposes. The function
// keeps track of key-presses, etc, as needed, but all events pass to the
// functions that have been assigned to the function-pointers by the user.
// This function can terminate the loop on its own, or through a variable that
// can be set in the back-end by the underlying software; the latter way should
// be the proper behaviour.
//
// @author Ioannis Nompelis <[email protected]>
*/
int xwindow_eventtrap( struct my_xwin_vars *xvars )
{
Display *xdisplay = xvars->xdisplay;
int iend = 0;
int ishift_key = 0, ialt_key = 0, ictrl_key = 0;
int ileft_button = 0, iright_button = 0, imiddle_button = 0;
while(iend == 0) {
// time to sleep if there are no events
int isleep = 25000; // about 1/40 of a second
int iresult;
// We need to start a timer here to use a time-interval to maintain a
// constant framerate. (We do not do that at present.)
// [start timer call goes here]
// perform pre-event, pre-drawing operations
if( xvars->callback_FrameEntry != NULL ) {
xvars->callback_FrameEntry( xvars );
}
while(XPending(xdisplay) > 0) {
XEvent event;
KeySym ikey;
XNextEvent(xdisplay, &event);
// decision making based on X event retreived
switch(event.type) {
case Expose:
fprintf( stderr, " [INFO] Got \"Expose\" event.\n" );
// This function can act on its own here...
// ...and/or dispatch the event to user-assigned functions for handling
if(xvars->callback_Expose != NULL) {
xvars->callback_Expose( xvars, &event );
}
break;
case ConfigureNotify:
fprintf( stderr, " [INFO] Got \"ConfigureNotify\" event.\n" );
// variables to track are:
// event.xconfigure.width;
// event.xconfigure.height;
// This function can act on its own here...
// ...and/or dispatch the event to user-assigned functions for handling
if(xvars->callback_ConfigureNotify != NULL)
xvars->callback_ConfigureNotify( xvars, &event );
break;
case MapNotify:
fprintf( stderr, " [INFO] Got \"MapNotify\" event.\n" );
// This function can act on its own here...
// ...and/or dispatch the event to user-assigned functions for handling
if(xvars->callback_MapNotify != NULL) {
xvars->callback_MapNotify( xvars, &event );
}
break;
case KeyPress:
fprintf( stderr, " [INFO] Got \"KeyPress\" event.\n" );
ikey = XLookupKeysym(&event.xkey, 0);
// This function can act on its own here...
// ...and/or dispatch the event to user-assigned functions for handling
// first trap control / alt / shift
if(ikey == XK_Shift_L) {
fprintf( stderr, " [INFO] The \"SHIFT L\" key is pressed.\n" );
ishift_key = 1;
} else
if(ikey == XK_Shift_R) {
fprintf( stderr, " [INFO] The \"SHIFT R\" key is pressed.\n" );
ishift_key = 2;
} else
if(ikey == XK_Control_L) {
fprintf( stderr, " [INFO] The \"CTRL L\" key is pressed.\n" );
ictrl_key = 1;
} else
if(ikey == XK_Control_R) {
fprintf( stderr, " [INFO] The \"CTRL R\" key is pressed.\n" );
ictrl_key = 2;
} else
if(ikey == XK_Alt_L) {
fprintf( stderr, " [INFO] The \"ALT L\" key is pressed.\n" );
ialt_key = 1;
} else
if(ikey == XK_Alt_R) {
fprintf( stderr, " [INFO] The \"ALT R\" key is pressed.\n" );
ialt_key = 2;
}
if(ikey == XK_Return) {
fprintf( stderr, " [INFO] The \"ENTER\" key is pressed.\n" );
} else
if(ikey == XK_Up) {
fprintf( stderr, " [INFO] The \"Up Arrow\" key is pressed.\n" );