-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminiglut.c
2855 lines (2462 loc) · 64.6 KB
/
miniglut.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
/*
MiniGLUT - minimal GLUT subset without dependencies
Copyright (C) 2020-2024 John Tsiombikas <nuclear@member.fsf.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#if defined(unix) || defined(__unix__)
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
#include <GL/glx.h>
#define BUILD_X11
#ifndef GLX_SAMPLE_BUFFERS_ARB
#define GLX_SAMPLE_BUFFERS_ARB 100000
#define GLX_SAMPLES_ARB 100001
#endif
#ifndef GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB
#define GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20b2
#endif
static Display *dpy;
static Window win, root;
static Colormap cmap;
static int cmap_size;
static int scr;
static GLXContext ctx;
static Atom xa_wm_proto, xa_wm_del_win;
static Atom xa_net_wm_state, xa_net_wm_state_fullscr;
static Atom xa_motif_wm_hints;
static Atom xa_motion_event, xa_button_press_event, xa_button_release_event, xa_command_event;
static unsigned int evmask;
static Cursor blank_cursor;
static int have_netwm_fullscr(void);
#elif defined(_WIN32)
#include <windows.h>
#define BUILD_WIN32
static LRESULT CALLBACK handle_message(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam);
static HINSTANCE hinst;
static HWND win;
static HDC dc;
static HGLRC ctx;
static HPALETTE cmap;
static int cmap_size;
#else
#error unsupported platform
#endif
#include <GL/gl.h>
#include "miniglut.h"
#ifdef _MSC_VER
#pragma warning (disable: 4244 4305)
#endif
struct ctx_info {
int rsize, gsize, bsize, asize;
int zsize, ssize;
int dblbuf;
int samples;
int stereo;
int srgb;
};
static void cleanup(void);
static void create_window(const char *title);
static void get_window_pos(int *x, int *y);
static void get_window_size(int *w, int *h);
static void get_screen_size(int *scrw, int *scrh);
static long get_msec(void);
static void panic(const char *msg);
static void sys_exit(int status);
static int sys_write(int fd, const void *buf, int count);
static int init_x = -1, init_y, init_width = 256, init_height = 256;
static unsigned int init_mode;
static struct ctx_info ctx_info;
static int cur_cursor = GLUT_CURSOR_INHERIT;
static int ignore_key_repeat;
static glut_cb cb_display;
static glut_cb cb_idle;
static glut_cb_reshape cb_reshape;
static glut_cb_state cb_vis, cb_entry;
static glut_cb_keyb cb_keydown, cb_keyup;
static glut_cb_special cb_skeydown, cb_skeyup;
static glut_cb_mouse cb_mouse;
static glut_cb_motion cb_motion, cb_passive;
static glut_cb_sbmotion cb_sball_motion, cb_sball_rotate;
static glut_cb_sbbutton cb_sball_button;
static int fullscreen;
static int prev_win_x, prev_win_y, prev_win_width, prev_win_height;
static int win_width, win_height;
static int mapped;
static int quit;
static int upd_pending;
static int modstate;
static int has_sball, sball_nbuttons;
void glutInit(int *argc, char **argv)
{
#ifdef BUILD_X11
Pixmap blankpix = 0;
XColor xcol;
if(!(dpy = XOpenDisplay(0))) {
panic("Failed to connect to the X server\n");
}
scr = DefaultScreen(dpy);
root = RootWindow(dpy, scr);
xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", False);
xa_wm_del_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
xa_motif_wm_hints = XInternAtom(dpy, "_MOTIF_WM_HINTS", False);
xa_net_wm_state_fullscr = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
if(have_netwm_fullscr()) {
xa_net_wm_state = XInternAtom(dpy, "_NET_WM_STATE", False);
}
xa_motion_event = XInternAtom(dpy, "MotionEvent", True);
xa_button_press_event = XInternAtom(dpy, "ButtonPressEvent", True);
xa_button_release_event = XInternAtom(dpy, "ButtonReleaseEvent", True);
xa_command_event = XInternAtom(dpy, "CommandEvent", True);
evmask = ExposureMask | StructureNotifyMask;
if((blankpix = XCreateBitmapFromData(dpy, root, (char*)&blankpix, 1, 1))) {
blank_cursor = XCreatePixmapCursor(dpy, blankpix, blankpix, &xcol, &xcol, 0, 0);
XFreePixmap(dpy, blankpix);
}
#endif
#ifdef BUILD_WIN32
WNDCLASSEX wc = {0};
hinst = GetModuleHandle(0);
wc.cbSize = sizeof wc;
wc.hbrBackground = GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hIcon = wc.hIconSm = LoadIcon(0, IDI_APPLICATION);
wc.hInstance = hinst;
wc.lpfnWndProc = handle_message;
wc.lpszClassName = "MiniGLUT";
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
if(!RegisterClassEx(&wc)) {
panic("Failed to register \"MiniGLUT\" window class\n");
}
if(init_x == -1) {
get_screen_size(&init_x, &init_y);
init_x >>= 3;
init_y >>= 3;
}
#endif
}
void glutInitWindowPosition(int x, int y)
{
init_x = x;
init_y = y;
}
void glutInitWindowSize(int xsz, int ysz)
{
init_width = xsz;
init_height = ysz;
}
void glutInitDisplayMode(unsigned int mode)
{
init_mode = mode;
}
void glutCreateWindow(const char *title)
{
create_window(title);
}
void glutExit(void)
{
quit = 1;
}
void glutMainLoop(void)
{
while(!quit) {
glutMainLoopEvent();
}
}
void glutPostRedisplay(void)
{
upd_pending = 1;
}
void glutIgnoreKeyRepeat(int ignore)
{
ignore_key_repeat = ignore;
}
#define UPD_EVMASK(x) \
do { \
if(func) { \
evmask |= x; \
} else { \
evmask &= ~(x); \
} \
if(win) XSelectInput(dpy, win, evmask); \
} while(0)
void glutIdleFunc(glut_cb func)
{
cb_idle = func;
}
void glutDisplayFunc(glut_cb func)
{
cb_display = func;
}
void glutReshapeFunc(glut_cb_reshape func)
{
cb_reshape = func;
}
void glutVisibilityFunc(glut_cb_state func)
{
cb_vis = func;
#ifdef BUILD_X11
UPD_EVMASK(VisibilityChangeMask);
#endif
}
void glutEntryFunc(glut_cb_state func)
{
cb_entry = func;
#ifdef BUILD_X11
UPD_EVMASK(EnterWindowMask | LeaveWindowMask);
#endif
}
void glutKeyboardFunc(glut_cb_keyb func)
{
cb_keydown = func;
#ifdef BUILD_X11
UPD_EVMASK(KeyPressMask);
#endif
}
void glutKeyboardUpFunc(glut_cb_keyb func)
{
cb_keyup = func;
#ifdef BUILD_X11
UPD_EVMASK(KeyReleaseMask);
#endif
}
void glutSpecialFunc(glut_cb_special func)
{
cb_skeydown = func;
#ifdef BUILD_X11
UPD_EVMASK(KeyPressMask);
#endif
}
void glutSpecialUpFunc(glut_cb_special func)
{
cb_skeyup = func;
#ifdef BUILD_X11
UPD_EVMASK(KeyReleaseMask);
#endif
}
void glutMouseFunc(glut_cb_mouse func)
{
cb_mouse = func;
#ifdef BUILD_X11
UPD_EVMASK(ButtonPressMask | ButtonReleaseMask);
#endif
}
void glutMotionFunc(glut_cb_motion func)
{
cb_motion = func;
#ifdef BUILD_X11
UPD_EVMASK(ButtonMotionMask);
#endif
}
void glutPassiveMotionFunc(glut_cb_motion func)
{
cb_passive = func;
#ifdef BUILD_X11
UPD_EVMASK(PointerMotionMask);
#endif
}
void glutSpaceballMotionFunc(glut_cb_sbmotion func)
{
cb_sball_motion = func;
}
void glutSpaceballRotateFunc(glut_cb_sbmotion func)
{
cb_sball_rotate = func;
}
void glutSpaceballButtonFunc(glut_cb_sbbutton func)
{
cb_sball_button = func;
}
int glutGet(unsigned int s)
{
int x, y;
switch(s) {
case GLUT_WINDOW_X:
get_window_pos(&x, &y);
return x;
case GLUT_WINDOW_Y:
get_window_pos(&x, &y);
return y;
case GLUT_WINDOW_WIDTH:
get_window_size(&x, &y);
return x;
case GLUT_WINDOW_HEIGHT:
get_window_size(&x, &y);
return y;
case GLUT_WINDOW_BUFFER_SIZE:
return ctx_info.rsize + ctx_info.gsize + ctx_info.bsize + ctx_info.asize;
case GLUT_WINDOW_STENCIL_SIZE:
return ctx_info.ssize;
case GLUT_WINDOW_DEPTH_SIZE:
return ctx_info.zsize;
case GLUT_WINDOW_RED_SIZE:
return ctx_info.rsize;
case GLUT_WINDOW_GREEN_SIZE:
return ctx_info.gsize;
case GLUT_WINDOW_BLUE_SIZE:
return ctx_info.bsize;
case GLUT_WINDOW_ALPHA_SIZE:
return ctx_info.asize;
case GLUT_WINDOW_DOUBLEBUFFER:
return ctx_info.dblbuf;
case GLUT_WINDOW_RGBA:
return 1;
case GLUT_WINDOW_NUM_SAMPLES:
return ctx_info.samples;
case GLUT_WINDOW_STEREO:
return ctx_info.stereo;
case GLUT_WINDOW_SRGB:
return ctx_info.srgb;
case GLUT_WINDOW_CURSOR:
return cur_cursor;
case GLUT_WINDOW_COLORMAP_SIZE:
return cmap_size;
case GLUT_SCREEN_WIDTH:
get_screen_size(&x, &y);
return x;
case GLUT_SCREEN_HEIGHT:
get_screen_size(&x, &y);
return y;
case GLUT_INIT_DISPLAY_MODE:
return init_mode;
case GLUT_INIT_WINDOW_X:
return init_x;
case GLUT_INIT_WINDOW_Y:
return init_y;
case GLUT_INIT_WINDOW_WIDTH:
return init_width;
case GLUT_INIT_WINDOW_HEIGHT:
return init_height;
case GLUT_ELAPSED_TIME:
return get_msec();
default:
break;
}
return 0;
}
int glutDeviceGet(unsigned int x)
{
switch(x) {
case GLUT_HAS_KEYBOARD:
case GLUT_HAS_MOUSE:
return 1;
case GLUT_NUM_MOUSE_BUTTONS:
return 3;
case GLUT_HAS_SPACEBALL:
return has_sball;
case GLUT_NUM_SPACEBALL_BUTTONS:
return sball_nbuttons;
case GLUT_HAS_DIAL_AND_BUTTON_BOX:
case GLUT_HAS_TABLET:
case GLUT_NUM_BUTTON_BOX_BUTTONS:
case GLUT_NUM_DIALS:
case GLUT_NUM_TABLET_BUTTONS:
return 0;
default:
break;
}
return -1;
}
int glutGetModifiers(void)
{
return modstate;
}
static int is_space(int c)
{
return c == ' ' || c == '\t' || c == '\v' || c == '\n' || c == '\r';
}
static const char *skip_space(const char *s)
{
while(*s && is_space(*s)) s++;
return s;
}
int glutExtensionSupported(char *ext)
{
const char *str, *eptr;
if(!(str = (const char*)glGetString(GL_EXTENSIONS))) {
return 0;
}
while(*str) {
str = skip_space(str);
eptr = skip_space(ext);
while(*str && !is_space(*str) && *eptr && *str == *eptr) {
str++;
eptr++;
}
if((!*str || is_space(*str)) && !*eptr) {
return 1;
}
while(*str && !is_space(*str)) str++;
}
return 0;
}
/* --------------- UNIX/X11 implementation ----------------- */
#ifdef BUILD_X11
enum {
SPNAV_EVENT_ANY, /* used by spnav_remove_events() */
SPNAV_EVENT_MOTION,
SPNAV_EVENT_BUTTON /* includes both press and release */
};
struct spnav_event_motion {
int type;
int x, y, z;
int rx, ry, rz;
unsigned int period;
int *data;
};
struct spnav_event_button {
int type;
int press;
int bnum;
};
union spnav_event {
int type;
struct spnav_event_motion motion;
struct spnav_event_button button;
};
static void handle_event(XEvent *ev);
static int spnav_window(Window win);
static int spnav_event(const XEvent *xev, union spnav_event *event);
static int spnav_remove_events(int type);
void glutMainLoopEvent(void)
{
XEvent ev;
if(!cb_display) {
panic("display callback not set");
}
if(!upd_pending && !cb_idle) {
XNextEvent(dpy, &ev);
handle_event(&ev);
if(quit) goto end;
}
while(XPending(dpy)) {
XNextEvent(dpy, &ev);
handle_event(&ev);
if(quit) goto end;
}
if(cb_idle) {
cb_idle();
}
if(upd_pending && mapped) {
upd_pending = 0;
cb_display();
}
end:
if(quit) {
cleanup();
}
}
static void cleanup(void)
{
if(win) {
spnav_window(root);
glXMakeCurrent(dpy, 0, 0);
XDestroyWindow(dpy, win);
}
}
static KeySym translate_keysym(KeySym sym)
{
switch(sym) {
case XK_Escape:
return 27;
case XK_BackSpace:
return '\b';
case XK_Linefeed:
return '\r';
case XK_Return:
return '\r';
case XK_Delete:
return 127;
case XK_Tab:
return '\t';
default:
break;
}
return sym;
}
static void handle_event(XEvent *ev)
{
KeySym sym;
union spnav_event sev;
switch(ev->type) {
case MapNotify:
mapped = 1;
break;
case UnmapNotify:
mapped = 0;
break;
case ConfigureNotify:
if(cb_reshape && (ev->xconfigure.width != win_width || ev->xconfigure.height != win_height)) {
win_width = ev->xconfigure.width;
win_height = ev->xconfigure.height;
cb_reshape(ev->xconfigure.width, ev->xconfigure.height);
}
break;
case ClientMessage:
if(ev->xclient.message_type == xa_wm_proto) {
if(ev->xclient.data.l[0] == xa_wm_del_win) {
quit = 1;
}
}
if(spnav_event(ev, &sev)) {
switch(sev.type) {
case SPNAV_EVENT_MOTION:
if(cb_sball_motion) {
cb_sball_motion(sev.motion.x, sev.motion.y, sev.motion.z);
}
if(cb_sball_rotate) {
cb_sball_rotate(sev.motion.rx, sev.motion.ry, sev.motion.rz);
}
spnav_remove_events(SPNAV_EVENT_MOTION);
break;
case SPNAV_EVENT_BUTTON:
if(cb_sball_button) {
cb_sball_button(sev.button.bnum + 1, sev.button.press ? GLUT_DOWN : GLUT_UP);
}
break;
default:
break;
}
}
break;
case Expose:
upd_pending = 1;
break;
case KeyPress:
if(0) {
case KeyRelease:
if(ignore_key_repeat && XEventsQueued(dpy, QueuedAfterReading)) {
XEvent next;
XPeekEvent(dpy, &next);
if(next.type == KeyPress && next.xkey.keycode == ev->xkey.keycode &&
next.xkey.time == ev->xkey.time) {
/* this is a key-repeat event, ignore the release and consume
* the following press
*/
XNextEvent(dpy, &next);
break;
}
}
}
modstate = ev->xkey.state & (ShiftMask | ControlMask | Mod1Mask);
if(!(sym = XLookupKeysym(&ev->xkey, 0))) {
break;
}
sym = translate_keysym(sym);
if(sym < 256) {
if(ev->type == KeyPress) {
if(cb_keydown) cb_keydown((unsigned char)sym, ev->xkey.x, ev->xkey.y);
} else {
if(cb_keyup) cb_keyup((unsigned char)sym, ev->xkey.x, ev->xkey.y);
}
} else {
if(ev->type == KeyPress) {
if(cb_skeydown) cb_skeydown(sym, ev->xkey.x, ev->xkey.y);
} else {
if(cb_skeyup) cb_skeyup(sym, ev->xkey.x, ev->xkey.y);
}
}
break;
case ButtonPress:
case ButtonRelease:
modstate = ev->xbutton.state & (ShiftMask | ControlMask | Mod1Mask);
if(cb_mouse) {
int bn = ev->xbutton.button - Button1;
cb_mouse(bn, ev->type == ButtonPress ? GLUT_DOWN : GLUT_UP,
ev->xbutton.x, ev->xbutton.y);
}
break;
case MotionNotify:
if(ev->xmotion.state & (Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask)) {
if(cb_motion) cb_motion(ev->xmotion.x, ev->xmotion.y);
} else {
if(cb_passive) cb_passive(ev->xmotion.x, ev->xmotion.y);
}
break;
case VisibilityNotify:
if(cb_vis) {
cb_vis(ev->xvisibility.state == VisibilityFullyObscured ? GLUT_NOT_VISIBLE : GLUT_VISIBLE);
}
break;
case EnterNotify:
if(cb_entry) cb_entry(GLUT_ENTERED);
break;
case LeaveNotify:
if(cb_entry) cb_entry(GLUT_LEFT);
break;
}
}
void glutSwapBuffers(void)
{
glXSwapBuffers(dpy, win);
}
/* BUG:
* set_fullscreen_mwm removes the decorations with MotifWM hints, and then it
* needs to resize the window to make it fullscreen. The way it does this is by
* querying the size of the root window (see get_screen_size), which in the
* case of multi-monitor setups will be the combined size of all monitors.
* This is problematic; the way to solve it is to use the XRandR extension, or
* the Xinerama extension, to figure out the dimensions of the correct video
* output, which would add potentially two extension support libraries to our
* dependencies list.
* Moreover, any X installation modern enough to support XR&R will almost
* certainly be running a window manager supporting the EHWM
* _NET_WM_STATE_FULLSCREEN method (set_fullscreen_ewmh), which does not rely
* on manual resizing, and is used in preference if available, making this
* whole endeavor pointless.
* So I'll just leave it with set_fullscreen_mwm covering the entire
* multi-monitor area for now.
*/
struct mwm_hints {
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long input_mode;
unsigned long status;
};
#define MWM_HINTS_DECORATIONS 2
#define MWM_DECOR_ALL 1
static void set_fullscreen_mwm(int fs)
{
struct mwm_hints hints;
int scr_width, scr_height;
if(fs) {
get_window_pos(&prev_win_x, &prev_win_y);
get_window_size(&prev_win_width, &prev_win_height);
get_screen_size(&scr_width, &scr_height);
hints.decorations = 0;
hints.flags = MWM_HINTS_DECORATIONS;
XChangeProperty(dpy, win, xa_motif_wm_hints, xa_motif_wm_hints, 32,
PropModeReplace, (unsigned char*)&hints, 5);
XMoveResizeWindow(dpy, win, 0, 0, scr_width, scr_height);
} else {
XDeleteProperty(dpy, win, xa_motif_wm_hints);
XMoveResizeWindow(dpy, win, prev_win_x, prev_win_y, prev_win_width, prev_win_height);
}
}
static int have_netwm_fullscr(void)
{
int fmt;
long offs = 0;
unsigned long i, count, rem;
Atom *prop, type;
Atom xa_net_supported = XInternAtom(dpy, "_NET_SUPPORTED", False);
do {
XGetWindowProperty(dpy, root, xa_net_supported, offs, 8, False, AnyPropertyType,
&type, &fmt, &count, &rem, (unsigned char**)&prop);
for(i=0; i<count; i++) {
if(prop[i] == xa_net_wm_state_fullscr) {
XFree(prop);
return 1;
}
}
XFree(prop);
offs += count;
} while(rem > 0);
return 0;
}
static void set_fullscreen_ewmh(int fs)
{
XClientMessageEvent msg = {0};
msg.type = ClientMessage;
msg.window = win;
msg.message_type = xa_net_wm_state; /* _NET_WM_STATE */
msg.format = 32;
msg.data.l[0] = fs ? 1 : 0;
msg.data.l[1] = xa_net_wm_state_fullscr; /* _NET_WM_STATE_FULLSCREEN */
msg.data.l[2] = 0;
msg.data.l[3] = 1; /* source regular application */
XSendEvent(dpy, root, False, SubstructureNotifyMask | SubstructureRedirectMask, (XEvent*)&msg);
}
static void set_fullscreen(int fs)
{
if(fullscreen == fs) return;
if(xa_net_wm_state && xa_net_wm_state_fullscr) {
set_fullscreen_ewmh(fs);
fullscreen = fs;
} else if(xa_motif_wm_hints) {
set_fullscreen_mwm(fs);
fullscreen = fs;
}
}
void glutPositionWindow(int x, int y)
{
set_fullscreen(0);
XMoveWindow(dpy, win, x, y);
}
void glutReshapeWindow(int xsz, int ysz)
{
set_fullscreen(0);
XResizeWindow(dpy, win, xsz, ysz);
}
void glutFullScreen(void)
{
set_fullscreen(1);
}
void glutSetWindowTitle(const char *title)
{
XTextProperty tprop;
if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
return;
}
XSetWMName(dpy, win, &tprop);
XFree(tprop.value);
}
void glutSetIconTitle(const char *title)
{
XTextProperty tprop;
if(!XStringListToTextProperty((char**)&title, 1, &tprop)) {
return;
}
XSetWMIconName(dpy, win, &tprop);
XFree(tprop.value);
}
void glutSetCursor(int cidx)
{
Cursor cur = None;
switch(cidx) {
case GLUT_CURSOR_LEFT_ARROW:
cur = XCreateFontCursor(dpy, XC_left_ptr);
break;
case GLUT_CURSOR_INHERIT:
break;
case GLUT_CURSOR_NONE:
cur = blank_cursor;
break;
default:
return;
}
XDefineCursor(dpy, win, cur);
cur_cursor = cidx;
}
void glutWarpPointer(int x, int y)
{
XWarpPointer(dpy, None, win, 0, 0, 0, 0, x, y);
}
void glutSetColor(int idx, float r, float g, float b)
{
XColor color;
if(idx >= 0 && idx < cmap_size) {
color.pixel = idx;
color.red = (unsigned short)(r * 65535.0f);
color.green = (unsigned short)(g * 65535.0f);
color.blue = (unsigned short)(b * 65535.0f);
color.flags = DoRed | DoGreen | DoBlue;
XStoreColor(dpy, cmap, &color);
}
}
float glutGetColor(int idx, int comp)
{
XColor color;
if(idx < 0 || idx >= cmap_size) {
return -1.0f;
}
color.pixel = idx;
XQueryColor(dpy, cmap, &color);
switch(comp) {
case GLUT_RED:
return color.red / 65535.0f;
case GLUT_GREEN:
return color.green / 65535.0f;
case GLUT_BLUE:
return color.blue / 65535.0f;
default:
break;
}
return -1.0f;
}
void glutSetKeyRepeat(int repmode)
{
if(repmode) {
XAutoRepeatOn(dpy);
} else {
XAutoRepeatOff(dpy);
}
}
static XVisualInfo *choose_visual(unsigned int mode)
{
XVisualInfo *vi;
int attr[32];
int *aptr = attr;
int *samples = 0;
if(mode & GLUT_DOUBLE) {
*aptr++ = GLX_DOUBLEBUFFER;
}
if(mode & GLUT_INDEX) {
*aptr++ = GLX_BUFFER_SIZE;
*aptr++ = 1;
} else {
*aptr++ = GLX_RGBA;
*aptr++ = GLX_RED_SIZE; *aptr++ = 1;
*aptr++ = GLX_GREEN_SIZE; *aptr++ = 1;
*aptr++ = GLX_BLUE_SIZE; *aptr++ = 1;
}
if(mode & GLUT_ALPHA) {
*aptr++ = GLX_ALPHA_SIZE;
*aptr++ = 4;
}
if(mode & GLUT_DEPTH) {
*aptr++ = GLX_DEPTH_SIZE;
*aptr++ = 8;
}
if(mode & GLUT_STENCIL) {
*aptr++ = GLX_STENCIL_SIZE;
*aptr++ = 1;
}
if(mode & GLUT_ACCUM) {
*aptr++ = GLX_ACCUM_RED_SIZE; *aptr++ = 1;
*aptr++ = GLX_ACCUM_GREEN_SIZE; *aptr++ = 1;
*aptr++ = GLX_ACCUM_BLUE_SIZE; *aptr++ = 1;
}
if(mode & GLUT_STEREO) {
*aptr++ = GLX_STEREO;
}
if(mode & GLUT_SRGB) {
*aptr++ = GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB;
}
if(mode & GLUT_MULTISAMPLE) {
*aptr++ = GLX_SAMPLE_BUFFERS_ARB;
*aptr++ = 1;
*aptr++ = GLX_SAMPLES_ARB;
samples = aptr;
*aptr++ = 32;
}
*aptr++ = None;
if(!samples) {
return glXChooseVisual(dpy, scr, attr);
}
while(!(vi = glXChooseVisual(dpy, scr, attr)) && *samples) {
*samples >>= 1;
if(!*samples) {
aptr[-3] = None;
}
}
return vi;
}
static void create_window(const char *title)
{
XSetWindowAttributes xattr = {0};
XVisualInfo *vi;
unsigned int xattr_mask;
unsigned int mode = init_mode;
if(!(vi = choose_visual(mode))) {
mode &= ~GLUT_SRGB;
if(!(vi = choose_visual(mode))) {
panic("Failed to find compatible visual\n");
}
}
if(!(ctx = glXCreateContext(dpy, vi, 0, True))) {
XFree(vi);
panic("Failed to create OpenGL context\n");
}
glXGetConfig(dpy, vi, GLX_RED_SIZE, &ctx_info.rsize);
glXGetConfig(dpy, vi, GLX_GREEN_SIZE, &ctx_info.gsize);
glXGetConfig(dpy, vi, GLX_BLUE_SIZE, &ctx_info.bsize);
glXGetConfig(dpy, vi, GLX_ALPHA_SIZE, &ctx_info.asize);
glXGetConfig(dpy, vi, GLX_DEPTH_SIZE, &ctx_info.zsize);
glXGetConfig(dpy, vi, GLX_STENCIL_SIZE, &ctx_info.ssize);