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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
|
//========================================================================
// GLFW - An OpenGL framework
// File: x11_window.c
// Platform: X11 (Unix)
// API version: 2.6
// WWW: http://glfw.sourceforge.net
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Camilla Berglund
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
/* Defines some GLX FSAA tokens if not yet defined */
#ifndef GLX_SAMPLE_BUFFERS
# define GLX_SAMPLE_BUFFERS 100000
#endif
#ifndef GLX_SAMPLES
# define GLX_SAMPLES 100001
#endif
/* KDE decoration values */
enum {
KDE_noDecoration = 0,
KDE_normalDecoration = 1,
KDE_tinyDecoration = 2,
KDE_noFocus = 256,
KDE_standaloneMenuBar = 512,
KDE_desktopIcon = 1024 ,
KDE_staysOnTop = 2048
};
//************************************************************************
//**** GLFW internal functions ****
//************************************************************************
//========================================================================
// _glfwWaitForMapNotify()
//========================================================================
Bool _glfwWaitForMapNotify( Display *d, XEvent *e, char *arg )
{
return (e->type == MapNotify) && (e->xmap.window == (Window)arg);
}
//========================================================================
// _glfwWaitForUnmapNotify()
//========================================================================
Bool _glfwWaitForUnmapNotify( Display *d, XEvent *e, char *arg )
{
return (e->type == UnmapNotify) && (e->xmap.window == (Window)arg);
}
//========================================================================
// _glfwDisableDecorations() - Turn off window decorations
// Based on xawdecode: src/wmhooks.c
//========================================================================
#define MWM_HINTS_DECORATIONS (1L << 1)
static void _glfwDisableDecorations( void )
{
int RemovedDecorations;
Atom HintAtom;
XSetWindowAttributes attributes;
RemovedDecorations = 0;
// First try to set MWM hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "_MOTIF_WM_HINTS", True );
if ( HintAtom != None )
{
struct {
unsigned long flags;
unsigned long functions;
unsigned long decorations;
long input_mode;
unsigned long status;
} MWMHints = { MWM_HINTS_DECORATIONS, 0, 0, 0, 0 };
XChangeProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom, HintAtom,
32, PropModeReplace, (unsigned char *)&MWMHints,
sizeof(MWMHints)/4 );
RemovedDecorations = 1;
}
// Now try to set KWM hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "KWM_WIN_DECORATION", True );
if ( HintAtom != None )
{
long KWMHints = KDE_tinyDecoration;
XChangeProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom, HintAtom,
32, PropModeReplace, (unsigned char *)&KWMHints,
sizeof(KWMHints)/4 );
RemovedDecorations = 1;
}
// Now try to set GNOME hints
HintAtom = XInternAtom(_glfwLibrary.Dpy, "_WIN_HINTS", True );
if ( HintAtom != None )
{
long GNOMEHints = 0;
XChangeProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom, HintAtom,
32, PropModeReplace, (unsigned char *)&GNOMEHints,
sizeof(GNOMEHints)/4 );
RemovedDecorations = 1;
}
// Now try to set KDE NET_WM hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_WINDOW_TYPE", True );
if ( HintAtom != None )
{
Atom NET_WMHints[2];
NET_WMHints[0] = XInternAtom( _glfwLibrary.Dpy, "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE", True );
/* define a fallback... */
NET_WMHints[1] = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_WINDOW_TYPE_NORMAL", True );
XChangeProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom, XA_ATOM,
32, PropModeReplace, (unsigned char *)&NET_WMHints,
2 );
RemovedDecorations = 1;
}
// Set ICCCM fullscreen WM hint
HintAtom = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_STATE", True );
if ( HintAtom != None )
{
Atom NET_WMHints[1];
NET_WMHints[0] = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_STATE_FULLSCREEN", True );
XChangeProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom, XA_ATOM,
32, PropModeReplace, (unsigned char *)&NET_WMHints, 1 );
}
// Did we sucessfully remove the window decorations?
if( RemovedDecorations )
{
// Finally set the transient hints
XSetTransientForHint( _glfwLibrary.Dpy, _glfwWin.Win, RootWindow(_glfwLibrary.Dpy, _glfwWin.Scrn) );
XUnmapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
XMapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
}
else
{
// The Butcher way of removing window decorations
attributes.override_redirect = True;
XChangeWindowAttributes( _glfwLibrary.Dpy, _glfwWin.Win,
CWOverrideRedirect, &attributes );
_glfwWin.OverrideRedirect = GL_TRUE;
}
}
//========================================================================
// _glfwEnableDecorations() - Turn on window decorations
//========================================================================
static void _glfwEnableDecorations( void )
{
int ActivatedDecorations;
Atom HintAtom;
// If this is an override redirect window, skip it...
if( _glfwWin.OverrideRedirect )
{
return;
}
ActivatedDecorations = 0;
// First try to unset MWM hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "_MOTIF_WM_HINTS", True );
if ( HintAtom != None )
{
XDeleteProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom );
ActivatedDecorations = 1;
}
// Now try to unset KWM hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "KWM_WIN_DECORATION", True );
if ( HintAtom != None )
{
XDeleteProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom );
ActivatedDecorations = 1;
}
// Now try to unset GNOME hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "_WIN_HINTS", True );
if ( HintAtom != None )
{
XDeleteProperty( _glfwLibrary.Dpy, _glfwWin.Win, HintAtom );
ActivatedDecorations = 1;
}
// Now try to unset NET_WM hints
HintAtom = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_WINDOW_TYPE", True );
if ( HintAtom != None )
{
Atom NET_WMHints = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_WINDOW_TYPE_NORMAL", True);
if( NET_WMHints != None )
{
XChangeProperty( _glfwLibrary.Dpy, _glfwWin.Win,
HintAtom, XA_ATOM, 32, PropModeReplace,
(unsigned char *)&NET_WMHints, 1 );
ActivatedDecorations = 1;
}
}
// Finally unset the transient hints if necessary
if( ActivatedDecorations )
{
// NOTE: Does this work?
XSetTransientForHint( _glfwLibrary.Dpy, _glfwWin.Win, None);
XUnmapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
XMapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
}
}
//========================================================================
// _glfwChooseVisual() - We do our own function here, since
// glXChooseVisual does not behave as we want it to (not according to the
// GLFW specs)
//========================================================================
XVisualInfo * _glfwChooseVisual( Display *Dpy, int Screen, int r, int g,
int b, int a, int d, int s, int ar, int ag, int ab, int aa, int aux,
int fsaa, int stereo)
{
XVisualInfo *VI, *VI_list, VI_tmp;
int nitems_return, i;
int vi_gl, vi_rgba, vi_double, vi_stereo;
int vi_r, vi_g, vi_b, vi_a, vi_d, vi_s, vi_ar, vi_ag, vi_ab, vi_aa;
int vi_aux;
int color, accum, vi_accum;
int missing, color_diff, extra_diff;
int best_vis, best_missing, best_color_diff, best_extra_diff;
int samples, samplebuffers, vi_samples, vi_samplebuffers;
// Get list of visuals for this screen & display
VI_tmp.screen = Screen;
VI_list = XGetVisualInfo( Dpy, VisualScreenMask, &VI_tmp,
&nitems_return );
if( VI_list == NULL )
{
return NULL;
}
// Pick some prefered color depth if the user did not request a
// specific depth (note: if the user did not request a specific color
// depth, this will not be a driving demand, it's only here to avoid
// selection randomness)
color = (r > 0 || g > 0 || b > 0);
if( !color )
{
r = g = b = 8;
}
// Make sure that stereo is 1 or 0
stereo = stereo ? 1 : 0;
// Convenience pre-calculation
accum = (ar > 0 || ag > 0 || ab > 0 || aa > 0);
samples = fsaa;
samplebuffers = (fsaa > 0) ? 1 : 0;
// Loop through list of visuals to find best match
best_vis = -1;
best_missing = 0x7fffffff;
best_color_diff = 0x7fffffff;
best_extra_diff = 0x7fffffff;
for( i = 0; i < nitems_return; i ++ )
{
// We want GL, RGBA & DOUBLEBUFFER, and NOT STEREO / STEREO
glXGetConfig( Dpy, &VI_list[i], GLX_USE_GL, &vi_gl );
glXGetConfig( Dpy, &VI_list[i], GLX_RGBA, &vi_rgba );
glXGetConfig( Dpy, &VI_list[i], GLX_DOUBLEBUFFER, &vi_double );
glXGetConfig( Dpy, &VI_list[i], GLX_STEREO, &vi_stereo );
vi_stereo = vi_stereo ? 1 : 0;
if( vi_gl && vi_rgba && vi_double && (vi_stereo == stereo) )
{
// Get visual color parameters
glXGetConfig( Dpy, &VI_list[i], GLX_RED_SIZE, &vi_r );
glXGetConfig( Dpy, &VI_list[i], GLX_GREEN_SIZE, &vi_g );
glXGetConfig( Dpy, &VI_list[i], GLX_BLUE_SIZE, &vi_b );
// Get visual "extra" parameters
glXGetConfig( Dpy, &VI_list[i], GLX_ALPHA_SIZE, &vi_a );
glXGetConfig( Dpy, &VI_list[i], GLX_DEPTH_SIZE, &vi_d );
glXGetConfig( Dpy, &VI_list[i], GLX_STENCIL_SIZE, &vi_s );
glXGetConfig( Dpy, &VI_list[i], GLX_ACCUM_RED_SIZE, &vi_ar );
glXGetConfig( Dpy, &VI_list[i], GLX_ACCUM_GREEN_SIZE, &vi_ag );
glXGetConfig( Dpy, &VI_list[i], GLX_ACCUM_BLUE_SIZE, &vi_ab );
glXGetConfig( Dpy, &VI_list[i], GLX_ACCUM_ALPHA_SIZE, &vi_aa );
glXGetConfig( Dpy, &VI_list[i], GLX_AUX_BUFFERS, &vi_aux );
glXGetConfig( Dpy, &VI_list[i], GLX_SAMPLE_BUFFERS, &vi_samplebuffers );
glXGetConfig( Dpy, &VI_list[i], GLX_SAMPLES, &vi_samples );
vi_accum = (vi_ar > 0 || vi_ag > 0 || vi_ab > 0 || vi_aa > 0);
// Check how many buffers are missing
missing = 0;
if( a > 0 && vi_a == 0 ) missing ++;
if( d > 0 && vi_d == 0 ) missing ++;
if( s > 0 && vi_s == 0 ) missing ++;
if( accum && !vi_accum ) missing ++;
if( aux > 0 && vi_aux == 0 ) missing ++;
if( samplebuffers > 0 && vi_samplebuffers == 0 ) missing ++;
// Calculate color diff
color_diff = (r - vi_r) * (r - vi_r) +
(g - vi_g) * (g - vi_g) +
(b - vi_b) * (b - vi_b);
// Calculate "extra" diff
extra_diff = 0;
if( a > 0 )
{
extra_diff += (a - vi_a) * (a - vi_a);
}
if( d > 0 )
{
extra_diff += (d - vi_d) * (d - vi_d);
}
if( s > 0 )
{
extra_diff += (s - vi_s) * (s - vi_s);
}
if( accum )
{
extra_diff += (ar - vi_ar) * (ar - vi_ar) +
(ag - vi_ag) * (ag - vi_ag) +
(ab - vi_ab) * (ab - vi_ab) +
(aa - vi_aa) * (aa - vi_aa);
}
if( aux > 0 )
{
extra_diff += (aux - vi_aux) * (aux - vi_aux);
}
if( samples > 0 )
{
extra_diff += (samples - vi_samples) * (samples - vi_samples);
}
// Check if this is a better match. We implement some
// complicated rules, by prioritizing in this order:
// 1) Visuals with the least number of missing buffers always
// have priority
// 2a) If (r,g,b)!=(0,0,0), color depth has priority over
// other buffers
// 2b) If (r,g,b)==(0,0,0), other buffers have priority over
// color depth
if( missing < best_missing )
{
best_vis = i;
}
else if( missing == best_missing )
{
if( color )
{
if( (color_diff < best_color_diff) ||
(color_diff == best_color_diff &&
extra_diff < best_extra_diff) )
{
best_vis = i;
}
}
else
{
if( (extra_diff < best_extra_diff) ||
(extra_diff == best_extra_diff &&
color_diff < best_color_diff) )
{
best_vis = i;
}
}
}
if( best_vis == i )
{
best_missing = missing;
best_color_diff = color_diff;
best_extra_diff = extra_diff;
}
}
}
// Copy best visual to a visual to return
if( best_vis >= 0 )
{
VI = XGetVisualInfo( Dpy, VisualIDMask, &VI_list[ best_vis ],
&nitems_return );
}
else
{
VI = NULL;
}
// Free visuals list
XFree( VI_list );
return VI;
}
//========================================================================
// _glfwTranslateKey() - Translates an X Window key to internal coding
//========================================================================
static int _glfwTranslateKey( int keycode )
{
KeySym key, key_lc, key_uc;
// Try secondary keysym, for numeric keypad keys
// Note: This way we always force "NumLock = ON", which at least
// enables GLFW users to detect numeric keypad keys
key = XKeycodeToKeysym( _glfwLibrary.Dpy, keycode, 1 );
switch( key )
{
// Numeric keypad
case XK_KP_0: return GLFW_KEY_KP_0;
case XK_KP_1: return GLFW_KEY_KP_1;
case XK_KP_2: return GLFW_KEY_KP_2;
case XK_KP_3: return GLFW_KEY_KP_3;
case XK_KP_4: return GLFW_KEY_KP_4;
case XK_KP_5: return GLFW_KEY_KP_5;
case XK_KP_6: return GLFW_KEY_KP_6;
case XK_KP_7: return GLFW_KEY_KP_7;
case XK_KP_8: return GLFW_KEY_KP_8;
case XK_KP_9: return GLFW_KEY_KP_9;
case XK_KP_Separator:
case XK_KP_Decimal: return GLFW_KEY_KP_DECIMAL;
case XK_KP_Equal: return GLFW_KEY_KP_EQUAL;
case XK_KP_Enter: return GLFW_KEY_KP_ENTER;
default: break;
}
// Now try pimary keysym
key = XKeycodeToKeysym( _glfwLibrary.Dpy, keycode, 0 );
switch( key )
{
// Special keys (non character keys)
case XK_Escape: return GLFW_KEY_ESC;
case XK_Tab: return GLFW_KEY_TAB;
case XK_Shift_L: return GLFW_KEY_LSHIFT;
case XK_Shift_R: return GLFW_KEY_RSHIFT;
case XK_Control_L: return GLFW_KEY_LCTRL;
case XK_Control_R: return GLFW_KEY_RCTRL;
case XK_Meta_L:
case XK_Alt_L: return GLFW_KEY_LALT;
case XK_Mode_switch: // Mapped to Alt_R on many keyboards
case XK_Meta_R:
case XK_Alt_R: return GLFW_KEY_RALT;
case XK_KP_Delete:
case XK_Delete: return GLFW_KEY_DEL;
case XK_BackSpace: return GLFW_KEY_BACKSPACE;
case XK_Return: return GLFW_KEY_ENTER;
case XK_KP_Home:
case XK_Home: return GLFW_KEY_HOME;
case XK_KP_End:
case XK_End: return GLFW_KEY_END;
case XK_KP_Page_Up:
case XK_Page_Up: return GLFW_KEY_PAGEUP;
case XK_KP_Page_Down:
case XK_Page_Down: return GLFW_KEY_PAGEDOWN;
case XK_KP_Insert:
case XK_Insert: return GLFW_KEY_INSERT;
case XK_KP_Left:
case XK_Left: return GLFW_KEY_LEFT;
case XK_KP_Right:
case XK_Right: return GLFW_KEY_RIGHT;
case XK_KP_Down:
case XK_Down: return GLFW_KEY_DOWN;
case XK_KP_Up:
case XK_Up: return GLFW_KEY_UP;
case XK_F1: return GLFW_KEY_F1;
case XK_F2: return GLFW_KEY_F2;
case XK_F3: return GLFW_KEY_F3;
case XK_F4: return GLFW_KEY_F4;
case XK_F5: return GLFW_KEY_F5;
case XK_F6: return GLFW_KEY_F6;
case XK_F7: return GLFW_KEY_F7;
case XK_F8: return GLFW_KEY_F8;
case XK_F9: return GLFW_KEY_F9;
case XK_F10: return GLFW_KEY_F10;
case XK_F11: return GLFW_KEY_F11;
case XK_F12: return GLFW_KEY_F12;
case XK_F13: return GLFW_KEY_F13;
case XK_F14: return GLFW_KEY_F14;
case XK_F15: return GLFW_KEY_F15;
case XK_F16: return GLFW_KEY_F16;
case XK_F17: return GLFW_KEY_F17;
case XK_F18: return GLFW_KEY_F18;
case XK_F19: return GLFW_KEY_F19;
case XK_F20: return GLFW_KEY_F20;
case XK_F21: return GLFW_KEY_F21;
case XK_F22: return GLFW_KEY_F22;
case XK_F23: return GLFW_KEY_F23;
case XK_F24: return GLFW_KEY_F24;
case XK_F25: return GLFW_KEY_F25;
// Numeric keypad (should have been detected in secondary keysym!)
case XK_KP_Divide: return GLFW_KEY_KP_DIVIDE;
case XK_KP_Multiply: return GLFW_KEY_KP_MULTIPLY;
case XK_KP_Subtract: return GLFW_KEY_KP_SUBTRACT;
case XK_KP_Add: return GLFW_KEY_KP_ADD;
case XK_KP_Equal: return GLFW_KEY_KP_EQUAL;
case XK_KP_Enter: return GLFW_KEY_KP_ENTER;
// The rest (should be printable keys)
default:
// Make uppercase
XConvertCase( key, &key_lc, &key_uc );
key = key_uc;
// Valid ISO 8859-1 character?
if( (key >= 32 && key <= 126) ||
(key >= 160 && key <= 255) )
{
return (int) key;
}
return GLFW_KEY_UNKNOWN;
}
}
//========================================================================
// _glfwTranslateChar() - Translates an X Window event to Unicode
//========================================================================
static int _glfwTranslateChar( XKeyEvent *event )
{
KeySym keysym;
// Get X11 keysym
XLookupString( event, NULL, 0, &keysym, NULL );
// Convert to Unicode (see x11_keysym2unicode.c)
return (int) _glfwKeySym2Unicode( keysym );
}
//========================================================================
// Get next X event (called by glfwPollEvents)
//========================================================================
static int _glfwGetNextEvent( void )
{
XEvent event, next_event;
// Pull next event from event queue
XNextEvent( _glfwLibrary.Dpy, &event );
// Handle certain window messages
switch( event.type )
{
// Is a key being pressed?
case KeyPress:
{
// Translate and report key press
_glfwInputKey( _glfwTranslateKey( event.xkey.keycode ), GLFW_PRESS );
// Translate and report character input
if( _glfwWin.CharCallback )
{
_glfwInputChar( _glfwTranslateChar( &event.xkey ), GLFW_PRESS );
}
break;
}
// Is a key being released?
case KeyRelease:
{
// Do not report key releases for key repeats. For key repeats
// we will get KeyRelease/KeyPress pairs with identical time
// stamps. User selected key repeat filtering is handled in
// _glfwInputKey()/_glfwInputChar().
if( XEventsQueued( _glfwLibrary.Dpy, QueuedAfterReading ) )
{
XPeekEvent( _glfwLibrary.Dpy, &next_event );
if( next_event.type == KeyPress &&
next_event.xkey.window == event.xkey.window &&
next_event.xkey.keycode == event.xkey.keycode &&
next_event.xkey.time == event.xkey.time )
{
// Do not report anything for this event
break;
}
}
// Translate and report key release
_glfwInputKey( _glfwTranslateKey( event.xkey.keycode ), GLFW_RELEASE );
// Translate and report character input
if( _glfwWin.CharCallback )
{
_glfwInputChar( _glfwTranslateChar( &event.xkey ), GLFW_RELEASE );
}
break;
}
// Were any of the mouse-buttons pressed?
case ButtonPress:
{
if( event.xbutton.button == Button1 )
{
_glfwInputMouseClick( GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS );
}
else if( event.xbutton.button == Button2 )
{
_glfwInputMouseClick( GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS );
}
else if( event.xbutton.button == Button3 )
{
_glfwInputMouseClick( GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS );
}
// XFree86 3.3.2 and later translates mouse wheel up/down into
// mouse button 4 & 5 presses
else if( event.xbutton.button == Button4 )
{
_glfwInput.WheelPos++; // To verify: is this up or down?
if( _glfwWin.MouseWheelCallback )
{
_glfwWin.MouseWheelCallback( _glfwInput.WheelPos );
}
}
else if( event.xbutton.button == Button5 )
{
_glfwInput.WheelPos--;
if( _glfwWin.MouseWheelCallback )
{
_glfwWin.MouseWheelCallback( _glfwInput.WheelPos );
}
}
break;
}
// Were any of the mouse-buttons released?
case ButtonRelease:
{
if( event.xbutton.button == Button1 )
{
_glfwInputMouseClick( GLFW_MOUSE_BUTTON_LEFT,
GLFW_RELEASE );
}
else if( event.xbutton.button == Button2 )
{
_glfwInputMouseClick( GLFW_MOUSE_BUTTON_MIDDLE,
GLFW_RELEASE );
}
else if( event.xbutton.button == Button3 )
{
_glfwInputMouseClick( GLFW_MOUSE_BUTTON_RIGHT,
GLFW_RELEASE );
}
break;
}
// Was the mouse moved?
case MotionNotify:
{
if( event.xmotion.x != _glfwInput.CursorPosX ||
event.xmotion.y != _glfwInput.CursorPosY )
{
if( _glfwWin.MouseLock )
{
_glfwInput.MousePosX += event.xmotion.x -
_glfwInput.CursorPosX;
_glfwInput.MousePosY += event.xmotion.y -
_glfwInput.CursorPosY;
}
else
{
_glfwInput.MousePosX = event.xmotion.x;
_glfwInput.MousePosY = event.xmotion.y;
}
_glfwInput.CursorPosX = event.xmotion.x;
_glfwInput.CursorPosY = event.xmotion.y;
_glfwInput.MouseMoved = GL_TRUE;
// Call user callback function
if( _glfwWin.MousePosCallback )
{
_glfwWin.MousePosCallback( _glfwInput.MousePosX,
_glfwInput.MousePosY );
}
}
break;
}
// Was the window resized?
case ConfigureNotify:
{
if( event.xconfigure.width != _glfwWin.Width ||
event.xconfigure.height != _glfwWin.Height )
{
_glfwWin.Width = event.xconfigure.width;
_glfwWin.Height = event.xconfigure.height;
if( _glfwWin.WindowSizeCallback )
{
_glfwWin.WindowSizeCallback( _glfwWin.Width,
_glfwWin.Height );
}
}
break;
}
// Was the window closed by the window manager?
case ClientMessage:
{
if( (Atom) event.xclient.data.l[ 0 ] == _glfwWin.WMDeleteWindow )
{
return GL_TRUE;
}
if( (Atom) event.xclient.data.l[ 0 ] == _glfwWin.WMPing )
{
XSendEvent( _glfwLibrary.Dpy,
RootWindow( _glfwLibrary.Dpy, _glfwWin.VI->screen ),
False, SubstructureNotifyMask | SubstructureRedirectMask, &event );
}
break;
}
// Was the window mapped (un-iconified)?
case MapNotify:
_glfwWin.MapNotifyCount++;
break;
// Was the window unmapped (iconified)?
case UnmapNotify:
_glfwWin.MapNotifyCount--;
break;
// Was the window activated?
case FocusIn:
_glfwWin.FocusInCount++;
break;
// Was the window de-activated?
case FocusOut:
_glfwWin.FocusInCount--;
break;
// Was the window contents damaged?
case Expose:
{
// Call user callback function
if( _glfwWin.WindowRefreshCallback )
{
_glfwWin.WindowRefreshCallback();
}
break;
}
// Was the window destroyed?
case DestroyNotify:
return GL_TRUE;
default:
{
#if defined( _GLFW_HAS_XRANDR )
switch( event.type - _glfwLibrary.XRandR.EventBase )
{
case RRScreenChangeNotify:
{
// Show XRandR that we really care
XRRUpdateConfiguration( &event );
break;
}
}
#endif
break;
}
}
// The window was not destroyed
return GL_FALSE;
}
//========================================================================
// _glfwCreateNULLCursor() - Create a blank cursor (for locked mouse mode)
//========================================================================
Cursor _glfwCreateNULLCursor( Display *display, Window root )
{
Pixmap cursormask;
XGCValues xgc;
GC gc;
XColor col;
Cursor cursor;
cursormask = XCreatePixmap( display, root, 1, 1, 1 );
xgc.function = GXclear;
gc = XCreateGC( display, cursormask, GCFunction, &xgc );
XFillRectangle( display, cursormask, gc, 0, 0, 1, 1 );
col.pixel = 0;
col.red = 0;
col.flags = 4;
cursor = XCreatePixmapCursor( display, cursormask, cursormask,
&col,&col, 0,0 );
XFreePixmap( display, cursormask );
XFreeGC( display, gc );
return cursor;
}
//========================================================================
// _glfwInitGLXExtensions() - Initialize GLX-specific extensions
//========================================================================
static void _glfwInitGLXExtensions( void )
{
int has_swap_control;
// Initialize OpenGL extension: GLX_SGI_swap_control
has_swap_control = _glfwPlatformExtensionSupported(
"GLX_SGI_swap_control"
);
if( has_swap_control )
{
_glfwWin.SwapInterval = (GLXSWAPINTERVALSGI_T)
_glfw_glXGetProcAddress( (GLubyte*) "glXSwapIntervalSGI" );
}
else
{
_glfwWin.SwapInterval = NULL;
}
}
//************************************************************************
//**** Platform implementation functions ****
//************************************************************************
//========================================================================
// _glfwPlatformOpenWindow() - Here is where the window is created, and
// the OpenGL rendering context is created
//========================================================================
int _glfwPlatformOpenWindow( int width, int height, int redbits,
int greenbits, int bluebits, int alphabits, int depthbits,
int stencilbits, int mode, _GLFWhints* hints )
{
Colormap cmap;
XSetWindowAttributes wa;
XEvent event;
Atom protocols[2];
// Clear platform specific GLFW window state
_glfwWin.VI = NULL;
_glfwWin.CX = (GLXContext)0;
_glfwWin.Win = (Window)0;
_glfwWin.Hints = NULL;
_glfwWin.PointerGrabbed = GL_FALSE;
_glfwWin.KeyboardGrabbed = GL_FALSE;
_glfwWin.OverrideRedirect = GL_FALSE;
_glfwWin.FS.ModeChanged = GL_FALSE;
_glfwWin.Saver.Changed = GL_FALSE;
_glfwWin.RefreshRate = hints->RefreshRate;
// Fullscreen & screen saver settings
// Check if GLX is supported on this display
if( !glXQueryExtension( _glfwLibrary.Dpy, NULL, NULL ) )
{
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// Get screen ID for this window
_glfwWin.Scrn = _glfwLibrary.DefaultScreen;
// Get an appropriate visual
_glfwWin.VI = _glfwChooseVisual( _glfwLibrary.Dpy,
_glfwWin.Scrn,
redbits, greenbits, bluebits,
alphabits, depthbits, stencilbits,
hints->AccumRedBits, hints->AccumGreenBits,
hints->AccumBlueBits, hints->AccumAlphaBits,
hints->AuxBuffers, hints->Samples, hints->Stereo );
if( _glfwWin.VI == NULL )
{
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// Create a GLX context
_glfwWin.CX = glXCreateContext( _glfwLibrary.Dpy, _glfwWin.VI, 0, GL_TRUE );
if( _glfwWin.CX == NULL )
{
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// Create a colormap
cmap = XCreateColormap( _glfwLibrary.Dpy, RootWindow( _glfwLibrary.Dpy,
_glfwWin.VI->screen), _glfwWin.VI->visual, AllocNone );
// Do we want fullscreen?
if( mode == GLFW_FULLSCREEN )
{
// Change video mode
_glfwSetVideoMode( _glfwWin.Scrn, &_glfwWin.Width,
&_glfwWin.Height, &_glfwWin.RefreshRate );
// Remember old screen saver settings
XGetScreenSaver( _glfwLibrary.Dpy, &_glfwWin.Saver.Timeout,
&_glfwWin.Saver.Interval, &_glfwWin.Saver.Blanking,
&_glfwWin.Saver.Exposure );
// Disable screen saver
XSetScreenSaver( _glfwLibrary.Dpy, 0, 0, DontPreferBlanking,
DefaultExposures );
}
// Attributes for window
wa.colormap = cmap;
wa.border_pixel = 0;
wa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask |
PointerMotionMask | ButtonPressMask | ButtonReleaseMask |
ExposureMask | FocusChangeMask | VisibilityChangeMask;
// Create a window
_glfwWin.Win = XCreateWindow(
_glfwLibrary.Dpy,
RootWindow( _glfwLibrary.Dpy, _glfwWin.VI->screen ),
0, 0, // Upper left corner
_glfwWin.Width, _glfwWin.Height, // Width, height
0, // Borderwidth
_glfwWin.VI->depth, // Depth
InputOutput,
_glfwWin.VI->visual,
CWBorderPixel | CWColormap | CWEventMask,
&wa
);
if( !_glfwWin.Win )
{
_glfwPlatformCloseWindow();
return GL_FALSE;
}
// Get the delete window WM protocol atom
_glfwWin.WMDeleteWindow = XInternAtom( _glfwLibrary.Dpy,
"WM_DELETE_WINDOW",
False );
// Get the ping WM protocol atom
_glfwWin.WMPing = XInternAtom( _glfwLibrary.Dpy, "_NET_WM_PING", False );
protocols[0] = _glfwWin.WMDeleteWindow;
protocols[1] = _glfwWin.WMPing;
// Allow us to trap the Window Close protocol
XSetWMProtocols( _glfwLibrary.Dpy, _glfwWin.Win, protocols,
sizeof(protocols) / sizeof(Atom) );
// Remove window decorations for fullscreen windows
if( mode == GLFW_FULLSCREEN )
{
_glfwDisableDecorations();
}
_glfwWin.Hints = XAllocSizeHints();
if( hints->WindowNoResize )
{
_glfwWin.Hints->flags |= (PMinSize | PMaxSize);
_glfwWin.Hints->min_width = _glfwWin.Hints->max_width = _glfwWin.Width;
_glfwWin.Hints->min_height = _glfwWin.Hints->max_height = _glfwWin.Height;
}
if( mode == GLFW_FULLSCREEN )
{
_glfwWin.Hints->flags |= PPosition;
_glfwWin.Hints->x = 0;
_glfwWin.Hints->y = 0;
}
XSetWMNormalHints( _glfwLibrary.Dpy, _glfwWin.Win, _glfwWin.Hints );
// Map window
XMapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
// Wait for map notification
XIfEvent( _glfwLibrary.Dpy, &event, _glfwWaitForMapNotify,
(char*)_glfwWin.Win );
// Make sure that our window ends up on top of things
XRaiseWindow( _glfwLibrary.Dpy, _glfwWin.Win );
// Fullscreen mode "post processing"
if( mode == GLFW_FULLSCREEN )
{
#if defined( _GLFW_HAS_XRANDR )
// Request screen change notifications
if( _glfwLibrary.XRandR.Available )
{
XRRSelectInput( _glfwLibrary.Dpy,
_glfwWin.Win,
RRScreenChangeNotifyMask );
}
#endif
// Force window position/size (some WMs do their own window
// geometry, which we want to override)
XMoveWindow( _glfwLibrary.Dpy, _glfwWin.Win, 0, 0 );
XResizeWindow( _glfwLibrary.Dpy, _glfwWin.Win, _glfwWin.Width,
_glfwWin.Height );
// Grab keyboard
if( XGrabKeyboard( _glfwLibrary.Dpy, _glfwWin.Win, True,
GrabModeAsync, GrabModeAsync, CurrentTime ) ==
GrabSuccess )
{
_glfwWin.KeyboardGrabbed = GL_TRUE;
}
// Grab mouse cursor
if( XGrabPointer( _glfwLibrary.Dpy, _glfwWin.Win, True,
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask, GrabModeAsync, GrabModeAsync,
_glfwWin.Win, None, CurrentTime ) ==
GrabSuccess )
{
_glfwWin.PointerGrabbed = GL_TRUE;
}
// Try to get window inside viewport (for virtual displays) by
// moving the mouse cursor to the upper left corner (and then to
// the center) - this works for XFree86
XWarpPointer( _glfwLibrary.Dpy, None, _glfwWin.Win, 0,0,0,0, 0,0 );
XWarpPointer( _glfwLibrary.Dpy, None, _glfwWin.Win, 0,0,0,0,
_glfwWin.Width/2, _glfwWin.Height/2 );
}
// Set window & icon name
// teeworlds: set our own title early, workaround for some window-managers
_glfwPlatformSetWindowTitle( "Teeworlds" );
// Connect the context to the window
glXMakeCurrent( _glfwLibrary.Dpy, _glfwWin.Win, _glfwWin.CX );
// Start by clearing the front buffer to black (avoid ugly desktop
// remains in our OpenGL window)
glClear( GL_COLOR_BUFFER_BIT );
glXSwapBuffers( _glfwLibrary.Dpy, _glfwWin.Win );
// Initialize GLX-specific OpenGL extensions
_glfwInitGLXExtensions();
return GL_TRUE;
}
//========================================================================
// Properly kill the window/video display
//========================================================================
void _glfwPlatformCloseWindow( void )
{
#if defined( _GLFW_HAS_XRANDR )
XRRScreenConfiguration *sc;
Window root;
#endif
// Free WM size hints
if( _glfwWin.Hints )
{
XFree( _glfwWin.Hints );
_glfwWin.Hints = NULL;
}
// Do we have a rendering context?
if( _glfwWin.CX )
{
// Release the context
glXMakeCurrent( _glfwLibrary.Dpy, None, NULL );
// Delete the context
glXDestroyContext( _glfwLibrary.Dpy, _glfwWin.CX );
_glfwWin.CX = NULL;
}
// Ungrab pointer and/or keyboard?
if( _glfwWin.KeyboardGrabbed )
{
XUngrabKeyboard( _glfwLibrary.Dpy, CurrentTime );
_glfwWin.KeyboardGrabbed = GL_FALSE;
}
if( _glfwWin.PointerGrabbed )
{
XUngrabPointer( _glfwLibrary.Dpy, CurrentTime );
_glfwWin.PointerGrabbed = GL_FALSE;
}
// Do we have a window?
if( _glfwWin.Win )
{
// Unmap the window
XUnmapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
// Destroy the window
XDestroyWindow( _glfwLibrary.Dpy, _glfwWin.Win );
_glfwWin.Win = (Window) 0;
}
// Did we change the fullscreen resolution?
if( _glfwWin.FS.ModeChanged )
{
#if defined( _GLFW_HAS_XRANDR )
if( _glfwLibrary.XRandR.Available )
{
root = RootWindow( _glfwLibrary.Dpy, _glfwWin.Scrn );
sc = XRRGetScreenInfo( _glfwLibrary.Dpy, root );
XRRSetScreenConfig( _glfwLibrary.Dpy,
sc,
root,
_glfwWin.FS.OldSizeID,
_glfwWin.FS.OldRotation,
CurrentTime );
XRRFreeScreenConfigInfo( sc );
}
#elif defined( _GLFW_HAS_XF86VIDMODE )
if( _glfwLibrary.XF86VidMode.Available )
{
// Unlock mode switch
XF86VidModeLockModeSwitch( _glfwLibrary.Dpy,
_glfwWin.Scrn,
0 );
// Change the video mode back to the old mode
XF86VidModeSwitchToMode( _glfwLibrary.Dpy,
_glfwWin.Scrn, &_glfwWin.FS.OldMode );
}
#endif
_glfwWin.FS.ModeChanged = GL_FALSE;
}
// Did we change the screen saver setting?
if( _glfwWin.Saver.Changed )
{
// Restore old screen saver settings
XSetScreenSaver( _glfwLibrary.Dpy, _glfwWin.Saver.Timeout,
_glfwWin.Saver.Interval, _glfwWin.Saver.Blanking,
_glfwWin.Saver.Exposure );
_glfwWin.Saver.Changed = GL_FALSE;
}
XSync( _glfwLibrary.Dpy, True );
}
//========================================================================
// _glfwPlatformSetWindowTitle() - Set the window title.
//========================================================================
void _glfwPlatformSetWindowTitle( const char *title )
{
// Set window & icon title
XStoreName( _glfwLibrary.Dpy, _glfwWin.Win, title );
XSetIconName( _glfwLibrary.Dpy, _glfwWin.Win, title );
}
//========================================================================
// _glfwPlatformSetWindowSize() - Set the window size.
//========================================================================
void _glfwPlatformSetWindowSize( int width, int height )
{
int mode = 0, rate, sizechanged = GL_FALSE;
GLint drawbuffer;
GLfloat clearcolor[4];
rate = _glfwWin.RefreshRate;
// If we are in fullscreen mode, get some info about the current mode
if( _glfwWin.Fullscreen )
{
// Get closest match for target video mode
mode = _glfwGetClosestVideoMode( _glfwWin.Scrn, &width, &height, &rate );
}
if( _glfwWin.WindowNoResize )
{
_glfwWin.Hints->min_width = _glfwWin.Hints->max_width = width;
_glfwWin.Hints->min_height = _glfwWin.Hints->max_height = height;
}
XSetWMNormalHints( _glfwLibrary.Dpy, _glfwWin.Win, _glfwWin.Hints );
// Change window size before changing fullscreen mode?
if( _glfwWin.Fullscreen && (width > _glfwWin.Width) )
{
XResizeWindow( _glfwLibrary.Dpy, _glfwWin.Win, width, height );
sizechanged = GL_TRUE;
}
// Change fullscreen video mode?
if( _glfwWin.Fullscreen )
{
// Change video mode (keeping current rate)
_glfwSetVideoModeMODE( _glfwWin.Scrn, mode, _glfwWin.RefreshRate );
// Clear the front buffer to black (avoid ugly desktop remains in
// our OpenGL window)
glGetIntegerv( GL_DRAW_BUFFER, &drawbuffer );
glGetFloatv( GL_COLOR_CLEAR_VALUE, clearcolor );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
if( drawbuffer == GL_BACK )
{
glXSwapBuffers( _glfwLibrary.Dpy, _glfwWin.Win );
}
glClearColor( clearcolor[0], clearcolor[1], clearcolor[2],
clearcolor[3] );
}
// Set window size (if not already changed)
if( !sizechanged )
{
XResizeWindow( _glfwLibrary.Dpy, _glfwWin.Win, width, height );
}
}
//========================================================================
// _glfwPlatformSetWindowPos() - Set the window position.
//========================================================================
void _glfwPlatformSetWindowPos( int x, int y )
{
// Set window position
XMoveWindow( _glfwLibrary.Dpy, _glfwWin.Win, x, y );
}
//========================================================================
// _glfwPlatformIconfyWindow() - Window iconification
//========================================================================
void _glfwPlatformIconifyWindow( void )
{
// We can't do this for override redirect windows
if( _glfwWin.OverrideRedirect )
{
return;
}
// In fullscreen mode, we need to restore the desktop video mode
if( _glfwWin.Fullscreen )
{
#if defined( _GLFW_HAS_XF86VIDMODE )
if( _glfwLibrary.XF86VidMode.Available )
{
// Unlock mode switch
XF86VidModeLockModeSwitch( _glfwLibrary.Dpy,
_glfwWin.Scrn,
0 );
// Change the video mode back to the old mode
XF86VidModeSwitchToMode( _glfwLibrary.Dpy,
_glfwWin.Scrn, &_glfwWin.FS.OldMode );
}
#endif
_glfwWin.FS.ModeChanged = GL_FALSE;
}
// Show mouse pointer
if( _glfwWin.PointerHidden )
{
XUndefineCursor( _glfwLibrary.Dpy, _glfwWin.Win );
_glfwWin.PointerHidden = GL_FALSE;
}
// Un-grab mouse pointer
if( _glfwWin.PointerGrabbed )
{
XUngrabPointer( _glfwLibrary.Dpy, CurrentTime );
_glfwWin.PointerGrabbed = GL_FALSE;
}
// Iconify window
XIconifyWindow( _glfwLibrary.Dpy, _glfwWin.Win,
_glfwWin.Scrn );
// Window is now iconified
_glfwWin.Iconified = GL_TRUE;
}
//========================================================================
// Window un-iconification
//========================================================================
void _glfwPlatformRestoreWindow( void )
{
// We can't do this for override redirect windows
if( _glfwWin.OverrideRedirect )
{
return;
}
// In fullscreen mode, change back video mode to user selected mode
if( _glfwWin.Fullscreen )
{
_glfwSetVideoMode( _glfwWin.Scrn,
&_glfwWin.Width, &_glfwWin.Height, &_glfwWin.RefreshRate );
}
// Un-iconify window
XMapWindow( _glfwLibrary.Dpy, _glfwWin.Win );
// In fullscreen mode...
if( _glfwWin.Fullscreen )
{
// Make sure window is in upper left corner
XMoveWindow( _glfwLibrary.Dpy, _glfwWin.Win, 0, 0 );
// Get input focus
XSetInputFocus( _glfwLibrary.Dpy, _glfwWin.Win, RevertToParent,
CurrentTime );
}
// Lock mouse, if necessary
if( _glfwWin.MouseLock )
{
// Hide cursor
if( !_glfwWin.PointerHidden )
{
XDefineCursor( _glfwLibrary.Dpy, _glfwWin.Win,
_glfwCreateNULLCursor( _glfwLibrary.Dpy,
_glfwWin.Win ) );
_glfwWin.PointerHidden = GL_TRUE;
}
// Grab cursor
if( !_glfwWin.PointerGrabbed )
{
if( XGrabPointer( _glfwLibrary.Dpy, _glfwWin.Win, True,
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask, GrabModeAsync,
GrabModeAsync, _glfwWin.Win, None,
CurrentTime ) == GrabSuccess )
{
_glfwWin.PointerGrabbed = GL_TRUE;
}
}
}
// Window is no longer iconified
_glfwWin.Iconified = GL_FALSE;
}
//========================================================================
// _glfwPlatformSwapBuffers() - Swap buffers (double-buffering) and poll
// any new events.
//========================================================================
void _glfwPlatformSwapBuffers( void )
{
// Update display-buffer
glXSwapBuffers( _glfwLibrary.Dpy, _glfwWin.Win );
}
//========================================================================
// _glfwPlatformSwapInterval() - Set double buffering swap interval
//========================================================================
void _glfwPlatformSwapInterval( int interval )
{
if( _glfwWin.SwapInterval )
{
_glfwWin.SwapInterval( interval );
}
}
//========================================================================
// _glfwPlatformRefreshWindowParams()
//========================================================================
void _glfwPlatformRefreshWindowParams( void )
{
#if defined( _GLFW_HAS_XRANDR )
XRRScreenConfiguration *sc;
#elif defined( _GLFW_HAS_XF86VIDMODE )
XF86VidModeModeLine modeline;
int dotclock;
float pixels_per_second, pixels_per_frame;
#endif
int sample_buffers;
// AFAIK, there is no easy/sure way of knowing if OpenGL is hardware
// accelerated
_glfwWin.Accelerated = GL_TRUE;
// "Standard" window parameters
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_RED_SIZE,
&_glfwWin.RedBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_GREEN_SIZE,
&_glfwWin.GreenBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_BLUE_SIZE,
&_glfwWin.BlueBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_ALPHA_SIZE,
&_glfwWin.AlphaBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_DEPTH_SIZE,
&_glfwWin.DepthBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_STENCIL_SIZE,
&_glfwWin.StencilBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_ACCUM_RED_SIZE,
&_glfwWin.AccumRedBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_ACCUM_GREEN_SIZE,
&_glfwWin.AccumGreenBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_ACCUM_BLUE_SIZE,
&_glfwWin.AccumBlueBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_ACCUM_ALPHA_SIZE,
&_glfwWin.AccumAlphaBits );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_AUX_BUFFERS,
&_glfwWin.AuxBuffers );
// Get stereo rendering setting
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_STEREO,
&_glfwWin.Stereo );
_glfwWin.Stereo = _glfwWin.Stereo ? 1 : 0;
// Get multisample buffer samples
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_SAMPLES,
&_glfwWin.Samples );
glXGetConfig( _glfwLibrary.Dpy, _glfwWin.VI, GLX_SAMPLE_BUFFERS,
&sample_buffers );
if( sample_buffers == 0 )
_glfwWin.Samples = 0;
// Default to refresh rate unknown (=0 according to GLFW spec)
_glfwWin.RefreshRate = 0;
// Retrieve refresh rate, if possible
#if defined( _GLFW_HAS_XRANDR )
if( _glfwLibrary.XRandR.Available )
{
sc = XRRGetScreenInfo( _glfwLibrary.Dpy,
RootWindow( _glfwLibrary.Dpy, _glfwWin.Scrn ) );
_glfwWin.RefreshRate = XRRConfigCurrentRate( sc );
XRRFreeScreenConfigInfo( sc );
}
#elif defined( _GLFW_HAS_XF86VIDMODE )
if( _glfwLibrary.XF86VidMode.Available )
{
// Use the XF86VidMode extension to get current video mode
XF86VidModeGetModeLine( _glfwLibrary.Dpy, _glfwWin.Scrn,
&dotclock, &modeline );
pixels_per_second = 1000.0f * (float) dotclock;
pixels_per_frame = (float) modeline.htotal * modeline.vtotal;
_glfwWin.RefreshRate = (int)(pixels_per_second/pixels_per_frame+0.5);
}
#endif
}
//========================================================================
// _glfwPlatformPollEvents() - Poll for new window and input events
//========================================================================
void _glfwPlatformPollEvents( void )
{
int winclosed = GL_FALSE;
// Flag that the cursor has not moved
_glfwInput.MouseMoved = GL_FALSE;
// Clear MapNotify and FocusIn counts
_glfwWin.MapNotifyCount = 0;
_glfwWin.FocusInCount = 0;
// Use XSync to synchronise events to the X display.
// I don't know if this can have a serious performance impact. My
// benchmarks with a GeForce card under Linux shows no difference with
// or without XSync, but when the GL window is rendered over a slow
// network I have noticed bad event syncronisation problems when XSync
// is not used, so I decided to use it.
//
// teeworlds: don't synchronise
//XSync( _glfwLibrary.Dpy, False );
// Empty the window event queue
while( XPending( _glfwLibrary.Dpy ) )
{
if( _glfwGetNextEvent() )
{
winclosed = GL_TRUE;
}
}
// Did we get mouse movement in locked cursor mode?
if( _glfwInput.MouseMoved && _glfwWin.MouseLock )
{
int maxx, minx, maxy, miny;
// Calculate movement threshold
minx = _glfwWin.Width / 4;
maxx = (_glfwWin.Width * 3) / 4;
miny = _glfwWin.Height / 4;
maxy = (_glfwWin.Height * 3) / 4;
// Did the mouse cursor move beyond our movement threshold
if(_glfwInput.CursorPosX < minx || _glfwInput.CursorPosX > maxx ||
_glfwInput.CursorPosY < miny || _glfwInput.CursorPosY > maxy)
{
// Move the mouse pointer back to the window center so that it
// does not wander off...
_glfwPlatformSetMouseCursorPos( _glfwWin.Width/2,
_glfwWin.Height/2 );
XSync( _glfwLibrary.Dpy, False );
}
}
// Was the window (un)iconified?
if( _glfwWin.MapNotifyCount < 0 && !_glfwWin.Iconified )
{
// Show mouse pointer
if( _glfwWin.PointerHidden )
{
XUndefineCursor( _glfwLibrary.Dpy, _glfwWin.Win );
_glfwWin.PointerHidden = GL_FALSE;
}
// Un-grab mouse pointer
if( _glfwWin.PointerGrabbed )
{
XUngrabPointer( _glfwLibrary.Dpy, CurrentTime );
_glfwWin.PointerGrabbed = GL_FALSE;
}
_glfwWin.Iconified = GL_TRUE;
}
else if( _glfwWin.MapNotifyCount > 0 && _glfwWin.Iconified )
{
// Restore fullscreen mode properties
if( _glfwWin.Fullscreen )
{
// Change back video mode to user selected mode
_glfwSetVideoMode( _glfwWin.Scrn, &_glfwWin.Width,
&_glfwWin.Height, &_glfwWin.RefreshRate );
// Disable window manager decorations
_glfwEnableDecorations();
// Make sure window is in upper left corner
XMoveWindow( _glfwLibrary.Dpy, _glfwWin.Win, 0, 0 );
// Get input focus
XSetInputFocus( _glfwLibrary.Dpy, _glfwWin.Win,
RevertToParent, CurrentTime );
}
// Hide cursor if necessary
if( _glfwWin.MouseLock && !_glfwWin.PointerHidden )
{
if( !_glfwWin.PointerHidden )
{
XDefineCursor( _glfwLibrary.Dpy, _glfwWin.Win,
_glfwCreateNULLCursor( _glfwLibrary.Dpy,
_glfwWin.Win ) );
_glfwWin.PointerHidden = GL_TRUE;
}
}
// Grab cursor if necessary
if( (_glfwWin.MouseLock || _glfwWin.Fullscreen) &&
!_glfwWin.PointerGrabbed )
{
if( XGrabPointer( _glfwLibrary.Dpy, _glfwWin.Win, True,
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask, GrabModeAsync,
GrabModeAsync, _glfwWin.Win, None,
CurrentTime ) == GrabSuccess )
{
_glfwWin.PointerGrabbed = GL_TRUE;
}
}
_glfwWin.Iconified = GL_FALSE;
}
// Did the window get/lose focus
if( _glfwWin.FocusInCount > 0 && !_glfwWin.Active )
{
// If we are in fullscreen mode, restore window
if( _glfwWin.Fullscreen && _glfwWin.Iconified )
{
_glfwPlatformRestoreWindow();
}
// Window is now active
_glfwWin.Active = GL_TRUE;
}
else if( _glfwWin.FocusInCount < 0 && _glfwWin.Active )
{
// If we are in fullscreen mode, iconfify window
if( _glfwWin.Fullscreen )
{
_glfwPlatformIconifyWindow();
}
// Window is not active
_glfwWin.Active = GL_FALSE;
_glfwInputDeactivation();
}
// Was there a window close request?
if( winclosed && _glfwWin.WindowCloseCallback )
{
// Check if the program wants us to close the window
winclosed = _glfwWin.WindowCloseCallback();
}
if( winclosed )
{
glfwCloseWindow();
}
}
//========================================================================
// _glfwPlatformWaitEvents() - Wait for new window and input events
//========================================================================
void _glfwPlatformWaitEvents( void )
{
XEvent event;
// Wait for new events (blocking)
XNextEvent( _glfwLibrary.Dpy, &event );
XPutBackEvent( _glfwLibrary.Dpy, &event );
// Poll events from queue
_glfwPlatformPollEvents();
}
//========================================================================
// _glfwPlatformHideMouseCursor() - Hide mouse cursor (lock it)
//========================================================================
void _glfwPlatformHideMouseCursor( void )
{
// Hide cursor
if( !_glfwWin.PointerHidden )
{
XDefineCursor( _glfwLibrary.Dpy, _glfwWin.Win,
_glfwCreateNULLCursor( _glfwLibrary.Dpy,
_glfwWin.Win ) );
_glfwWin.PointerHidden = GL_TRUE;
}
// Grab cursor to user window
if( !_glfwWin.PointerGrabbed )
{
if( XGrabPointer( _glfwLibrary.Dpy, _glfwWin.Win, True,
ButtonPressMask | ButtonReleaseMask |
PointerMotionMask, GrabModeAsync, GrabModeAsync,
_glfwWin.Win, None, CurrentTime ) ==
GrabSuccess )
{
_glfwWin.PointerGrabbed = GL_TRUE;
}
}
}
//========================================================================
// _glfwPlatformShowMouseCursor() - Show mouse cursor (unlock it)
//========================================================================
void _glfwPlatformShowMouseCursor( void )
{
// Un-grab cursor (only in windowed mode: in fullscreen mode we still
// want the mouse grabbed in order to confine the cursor to the window
// area)
if( _glfwWin.PointerGrabbed && !_glfwWin.Fullscreen )
{
XUngrabPointer( _glfwLibrary.Dpy, CurrentTime );
_glfwWin.PointerGrabbed = GL_FALSE;
}
// Show cursor
if( _glfwWin.PointerHidden )
{
XUndefineCursor( _glfwLibrary.Dpy, _glfwWin.Win );
_glfwWin.PointerHidden = GL_FALSE;
}
}
//========================================================================
// _glfwPlatformSetMouseCursorPos() - Set physical mouse cursor position
//========================================================================
void _glfwPlatformSetMouseCursorPos( int x, int y )
{
// Change cursor position
_glfwInput.CursorPosX = x;
_glfwInput.CursorPosY = y;
XWarpPointer( _glfwLibrary.Dpy, None, _glfwWin.Win, 0,0,0,0, x, y );
}
|