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
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
|
/*
* Copyright 2014 Olaf Seibert
*/
/*
* Implements some of the Extended Window Manager Hints, as (extremely
* poorly) documented at
* http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html .
* In fact, the wiki page that refers to that as being the current version
* (http://www.freedesktop.org/wiki/Specifications/wm-spec/)
* neglects to tell us there are newer versions 1.4 and 1.5 at
* http://standards.freedesktop.org/wm-spec/wm-spec-1.5.html
* (which has a listable directory that I discovered by accident).
* The same wiki page also has lots of dead links to a CVS repository.
* Nevertheless, it is where one ends up if one starts at
* http://www.freedesktop.org/wiki/Specifications/ .
*
* EWMH is an extension to the ICCCM (Inter-Client Communication
* Conventions Manual).
* http://tronche.com/gui/x/icccm/
*
* To fill in lots of details, the source code of other window managers
* has been consulted.
*/
#include "ctwm.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <assert.h>
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include "ctwm_atoms.h"
#include "ctwm_shutdown.h"
#include "ewmh_atoms.h"
#include "screen.h"
#include "events.h"
#include "event_handlers.h"
#include "functions_defs.h"
#include "icons.h"
#include "otp.h"
#include "image.h"
#include "list.h"
#include "functions.h"
#include "occupation.h"
#include "r_layout.h"
#include "util.h"
#include "vscreen.h"
#include "win_iconify.h"
#include "win_ops.h"
#include "win_resize.h"
#include "win_utils.h"
#include "workspace_utils.h"
/* #define DEBUG_EWMH */
Atom XEWMHAtom[NUM_EWMH_XATOMS];
#define NET_WM_STATE_REMOVE 0 /* remove/unset property */
#define NET_WM_STATE_ADD 1 /* add/set property */
#define NET_WM_STATE_TOGGLE 2 /* toggle property */
#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
#define _NET_WM_MOVERESIZE_SIZE_TOP 1
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
#define _NET_WM_MOVERESIZE_SIZE_LEFT 7
#define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
#define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
#define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
#define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
static Image *ExtractIcon(ScreenInfo *scr, unsigned long *prop, int width,
int height);
static void EwmhClientMessage_NET_WM_DESKTOP(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_WM_STATE(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_ACTIVE_WINDOW(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_WM_MOVERESIZE(XClientMessageEvent *msg);
static void EwmhClientMessage_NET_CLOSE_WINDOW(XClientMessageEvent *msg);
static XEvent synth_btnevent_for_moveresize(TwmWindow *twm_win);
static unsigned long EwmhGetWindowProperty(Window w, Atom name, Atom type);
static void EwmhGetStrut(TwmWindow *twm_win, bool update);
static void EwmhRemoveStrut(TwmWindow *twm_win);
static void EwmhSet_NET_WORKAREA(ScreenInfo *scr);
static int EwmhGet_NET_WM_STATE(TwmWindow *twm_win);
static void EwmhClientMessage_NET_WM_STATEchange(TwmWindow *twm_win, int change,
int newVal);
#define ALL_WORKSPACES 0xFFFFFFFFU
static void SendPropertyMessage(Window to, Window about,
Atom messagetype,
long l0, long l1, long l2, long l3, long l4,
long mask)
{
XEvent e;
e.xclient.type = ClientMessage;
e.xclient.message_type = messagetype;
e.xclient.display = dpy;
e.xclient.window = about;
e.xclient.format = 32;
e.xclient.data.l[0] = l0;
e.xclient.data.l[1] = l1;
e.xclient.data.l[2] = l2;
e.xclient.data.l[3] = l3;
e.xclient.data.l[4] = l4;
XSendEvent(dpy, to, False, mask, &e);
}
static void EwmhInitAtoms(void)
{
XInternAtoms(dpy, XEWMHAtomNames, NUM_EWMH_XATOMS, False, XEWMHAtom);
}
static bool caughtError;
static int CatchError(Display *display, XErrorEvent *event)
{
caughtError = true;
return 0;
}
void EwmhInit(void)
{
EwmhInitAtoms();
}
/*
* Force-generate some event, so that we know the current time.
*
* Suggested in the ICCCM:
* http://tronche.com/gui/x/icccm/sec-2.html#s-2.1
*/
static void GenerateTimestamp(ScreenInfo *scr)
{
XEvent event;
struct timespec tosleep;
int timeout = 200; /* 0.2 seconds in ms */
int found;
/* Sleep in 10ms chunks */
tosleep.tv_sec = 0;
tosleep.tv_nsec = (10 * 1000 * 1000);
if(EventTime > 0) {
return;
}
XChangeProperty(dpy, scr->icccm_Window,
XA_WM_CLASS, XA_STRING,
8, PropModeAppend, NULL, 0);
while(timeout > 0) {
found = XCheckTypedWindowEvent(dpy, scr->icccm_Window, PropertyNotify, &event);
if(found) {
break;
}
nanosleep(&tosleep, NULL);
timeout -= 10;
}
if(found) {
#ifdef DEBUG_EWMH
fprintf(stderr, "GenerateTimestamp: time = %ld, timeout left = %d\n",
event.xproperty.time, timeout);
#endif /* DEBUG_EWMH */
if(EventTime < event.xproperty.time) {
EventTime = event.xproperty.time;
}
}
}
/*
* Perform the "replace the window manager" protocol, as vaguely hinted
* at by the ICCCM section 4.3.
* http://tronche.com/gui/x/icccm/sec-4.html#s-4.3
*
* We do want to run through this even if we're not running with
* --replace ourselves, because it also sets up the bits for other
* invocations to --replace us.
*
* TODO: convert the selection to atom VERSION.
*/
static bool EwmhReplaceWM(ScreenInfo *scr)
{
char atomname[32];
Atom wmAtom;
Window selectionOwner;
snprintf(atomname, sizeof(atomname), "WM_S%d", scr->screen);
wmAtom = XInternAtom(dpy, atomname, False);
selectionOwner = XGetSelectionOwner(dpy, wmAtom);
if(selectionOwner == scr->icccm_Window) {
selectionOwner = None;
}
if(selectionOwner != None) {
XErrorHandler oldHandler;
/*
* Check if that owner still exists, and if it does, we want
* StructureNotify-kind events from it.
*/
caughtError = false;
oldHandler = XSetErrorHandler(CatchError);
XSelectInput(dpy, selectionOwner, StructureNotifyMask);
XSync(dpy, False);
XSetErrorHandler(oldHandler);
if(caughtError) {
selectionOwner = None;
}
}
/*
* If something else is owning things and we're not running
* --replace, give up now and return failure.
*/
if(selectionOwner != None && !CLarg.ewmh_replace) {
#ifdef DEBUG_EWMH
fprintf(stderr, "A window manager is already running on screen %d\n",
scr->screen);
#endif
return false;
}
/* We're asked to --replace, so try to do so */
XSetSelectionOwner(dpy, wmAtom, scr->icccm_Window, CurrentTime);
if(XGetSelectionOwner(dpy, wmAtom) != scr->icccm_Window) {
fprintf(stderr, "Did not get window manager selection on screen %d\n",
scr->screen);
return false;
}
/*
* If there was a previous selection owner, wait for it
* to go away.
*/
if(selectionOwner != None) {
int timeout = 10 * 1000; /* 10 seconds in ms */
XEvent event;
struct timespec tosleep;
/* Sleep in 100ms chunks */
tosleep.tv_sec = 0;
tosleep.tv_nsec = (100 * 1000 * 1000);
while(timeout > 0) {
int found = XCheckTypedWindowEvent(dpy, selectionOwner, DestroyNotify, &event);
if(found) {
break;
}
nanosleep(&tosleep, NULL);
timeout -= 100;
}
if(timeout <= 0) {
fprintf(stderr, "Timed out waiting for other window manager "
"on screen %d to quit\n",
scr->screen);
return false;
}
}
/*
* Send a message to confirm we're now managing the screen.
* ICCCM, end of chapter 2, section "Manager Selections".
* http://tronche.com/gui/x/icccm/sec-2.html#s-2.8
*
* ICCCM says StructureNotifyMask,
* OpenBox says SubstructureNotifyMask.
*/
GenerateTimestamp(scr);
SendPropertyMessage(scr->XineramaRoot, scr->XineramaRoot,
XA_MANAGER, EventTime, wmAtom, scr->icccm_Window, 0, 0,
StructureNotifyMask);
return true;
}
/*
* This function is called very early in initialisation.
*
* Only scr->screen and scr->XineramaRoot are valid: we want to know if
* it makes sense to continue with the full initialisation.
*
* Create the ICCCM window that owns the WM_Sn selection.
*/
bool EwmhInitScreenEarly(ScreenInfo *scr)
{
XSetWindowAttributes attrib;
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: XCreateWindow\n");
#endif
attrib.event_mask = PropertyChangeMask;
attrib.override_redirect = True;
scr->icccm_Window = XCreateWindow(dpy, scr->XineramaRoot,
-100, -100, 1, 1, 0,
CopyFromParent, InputOutput,
CopyFromParent,
CWEventMask | CWOverrideRedirect,
&attrib);
XMapWindow(dpy, scr->icccm_Window);
XLowerWindow(dpy, scr->icccm_Window);
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: call EwmhReplaceWM\n");
#endif
if(!EwmhReplaceWM(scr)) {
XDestroyWindow(dpy, scr->icccm_Window);
scr->icccm_Window = None;
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: return false\n");
#endif
return false;
}
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhInitScreenEarly: return true\n");
#endif
return true;
}
/*
* This initialisation is called late, when scr has been set up
* completely.
*/
void EwmhInitScreenLate(ScreenInfo *scr)
{
long data[2];
/* Set _NET_SUPPORTING_WM_CHECK on root window */
data[0] = scr->icccm_Window;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_SUPPORTING_WM_CHECK, XA_WINDOW,
32, PropModeReplace,
(unsigned char *)data, 1);
/*
* Set properties on the window;
* this also belongs with _NET_SUPPORTING_WM_CHECK
*/
XChangeProperty(dpy, scr->icccm_Window,
XA__NET_WM_NAME, XA_UTF8_STRING,
8, PropModeReplace,
(unsigned char *)"ctwm", 4);
data[0] = scr->icccm_Window;
XChangeProperty(dpy, scr->icccm_Window,
XA__NET_SUPPORTING_WM_CHECK, XA_WINDOW,
32, PropModeReplace,
(unsigned char *)data, 1);
/*
* Add supported properties to the root window.
*/
data[0] = 0;
data[1] = 0;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_DESKTOP_VIEWPORT, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 2);
data[0] = scr->rootw;
data[1] = scr->rooth;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_DESKTOP_GEOMETRY, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 2);
EwmhSet_NET_WORKAREA(scr);
if(scr->workSpaceManagerActive) {
data[0] = scr->workSpaceMgr.count;
}
else {
data[0] = 1;
}
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_NUMBER_OF_DESKTOPS, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 1);
if(scr->workSpaceManagerActive) {
/* TODO: this is for the first Virtual Screen only... */
/*data[0] = scr->workSpaceMgr.workSpaceWindowList->currentwspc->number; */
data[0] = 0;
}
else {
data[0] = 0;
}
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_CURRENT_DESKTOP, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 1);
EwmhSet_NET_SHOWING_DESKTOP(0);
long supported[30];
int i = 0;
supported[i++] = XA__NET_SUPPORTING_WM_CHECK;
supported[i++] = XA__NET_DESKTOP_VIEWPORT;
supported[i++] = XA__NET_NUMBER_OF_DESKTOPS;
supported[i++] = XA__NET_CURRENT_DESKTOP;
supported[i++] = XA__NET_DESKTOP_GEOMETRY;
supported[i++] = XA__NET_WM_ICON;
supported[i++] = XA__NET_WM_DESKTOP;
supported[i++] = XA__NET_CLIENT_LIST;
supported[i++] = XA__NET_CLIENT_LIST_STACKING;
supported[i++] = XA__NET_WM_WINDOW_TYPE;
supported[i++] = XA__NET_WM_WINDOW_TYPE_NORMAL;
supported[i++] = XA__NET_WM_WINDOW_TYPE_DESKTOP;
supported[i++] = XA__NET_WM_WINDOW_TYPE_DOCK;
supported[i++] = XA__NET_WM_STRUT;
supported[i++] = XA__NET_WM_STRUT_PARTIAL;
supported[i++] = XA__NET_SHOWING_DESKTOP;
supported[i++] = XA__NET_WM_STATE;
supported[i++] = XA__NET_WM_STATE_MAXIMIZED_VERT;
supported[i++] = XA__NET_WM_STATE_MAXIMIZED_HORZ;
supported[i++] = XA__NET_WM_STATE_FULLSCREEN;
supported[i++] = XA__NET_ACTIVE_WINDOW;
supported[i++] = XA__NET_WORKAREA;
supported[i++] = XA__NET_WM_MOVERESIZE;
supported[i++] = XA__NET_WM_STATE_SHADED;
supported[i++] = XA__NET_WM_STATE_ABOVE;
supported[i++] = XA__NET_WM_STATE_BELOW;
supported[i++] = XA__NET_CLOSE_WINDOW;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_SUPPORTED, XA_ATOM,
32, PropModeReplace,
(unsigned char *)supported, i);
}
#ifdef VSCREEN
/*
* Set up the _NET_VIRTUAL_ROOTS property, which indicates that we're
* using virtual root windows.
* This applies only if we have multiple virtual screens.
*
* Also record this as a supported atom in _NET_SUPPORTED.
*
* Really, our virtual screens (with their virtual root windows) don't quite
* fit in the EWMH idiom. Several root window properties (such as
* _NET_CURRENT_DESKTOP) are more appropriate on the virtual root windows. But
* that is not where other clients would look for them.
*
* The idea seems to be that the virtual roots as used for workspaces (desktops
* in EWMH terminology) are only mapped one at a time.
*/
void EwmhInitVirtualRoots(ScreenInfo *scr)
{
int numVscreens = scr->numVscreens;
if(numVscreens > 1) {
long *data;
long d0;
VirtualScreen *vs;
int i;
data = calloc(numVscreens, sizeof(long));
for(vs = scr->vScreenList, i = 0;
vs != NULL && i < numVscreens;
vs = vs->next, i++) {
data[i] = vs->window;
}
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_VIRTUAL_ROOTS, XA_WINDOW,
32, PropModeReplace,
(unsigned char *)data, numVscreens);
/* This might fail, but what can we do about it? */
free(data);
d0 = XA__NET_VIRTUAL_ROOTS;
XChangeProperty(dpy, scr->XineramaRoot,
XA__NET_SUPPORTED, XA_ATOM,
32, PropModeAppend,
(unsigned char *)&d0, 1);
}
}
#endif
static void EwmhTerminateScreen(ScreenInfo *scr)
{
XDeleteProperty(dpy, scr->XineramaRoot, XA__NET_SUPPORTED);
/*
* Don't delete scr->icccm_Window; let it be deleted automatically
* when we terminate the X server connection. A replacement window
* manager may want to start working immediately after it has
* disappeared.
*/
}
/*
* Clear everything that needs to be cleared before we exit.
*/
void EwmhTerminate(void)
{
int scrnum;
ScreenInfo *scr;
for(scrnum = 0; scrnum < NumScreens; scrnum++) {
if((scr = ScreenList[scrnum]) == NULL) {
continue;
}
EwmhTerminateScreen(scr);
}
}
/*
* Event handler: lost the WM_Sn selection
* (that's the only selection we have).
*/
void EwmhSelectionClear(XSelectionClearEvent *sev)
{
#ifdef DEBUG_EWMH
fprintf(stderr, "sev->window = %x\n", (unsigned)sev->window);
#endif
DoShutdown();
}
/*
* When accepting client messages to the root window,
* be accepting and accept both the real root window and the
* current virtual screen.
*
* Should perhaps also accept any other virtual screen.
*/
bool EwmhClientMessage(XClientMessageEvent *msg)
{
if(msg->format != 32) {
return false;
}
/* Messages regarding any window */
if(msg->message_type == XA__NET_WM_DESKTOP) {
EwmhClientMessage_NET_WM_DESKTOP(msg);
return true;
}
else if(msg->message_type == XA__NET_WM_STATE) {
EwmhClientMessage_NET_WM_STATE(msg);
return true;
}
else if(msg->message_type == XA__NET_ACTIVE_WINDOW) {
EwmhClientMessage_NET_ACTIVE_WINDOW(msg);
return true;
}
else if(msg->message_type == XA__NET_WM_MOVERESIZE) {
EwmhClientMessage_NET_WM_MOVERESIZE(msg);
return true;
}
else if(msg->message_type == XA__NET_CLOSE_WINDOW) {
EwmhClientMessage_NET_CLOSE_WINDOW(msg);
return true;
}
/* Messages regarding the root window */
if(msg->window != Scr->XineramaRoot &&
msg->window != Scr->Root) {
#ifdef DEBUG_EWMH
fprintf(stderr, "Received unrecognized client message: %s\n",
XGetAtomName(dpy, msg->message_type));
#endif
return false;
}
if(msg->message_type == XA__NET_CURRENT_DESKTOP) {
GotoWorkSpaceByNumber(Scr->currentvs, msg->data.l[0]);
return true;
}
else if(msg->message_type == XA__NET_SHOWING_DESKTOP) {
ShowBackground(Scr->currentvs, msg->data.l[0] ? 1 : 0);
return true;
}
else {
#ifdef DEBUG_EWMH
fprintf(stderr, "Received unrecognized client message about root window: %s\n",
XGetAtomName(dpy, msg->message_type));
#endif
}
return false;
}
/*
* The format of the _NET_WM_ICON property is
*
* [0] width
* [1] height
* height repetitions of
* row, which is
* width repetitions of
* pixel: ARGB
* repeat for next size.
*
* Some icons can be 256x256 CARDINALs which is 65536 CARDINALS!
* Therefore we fetch in pieces and skip the pixels of large icons
* until needed.
*
* First scan all sizes. Keep a record of the closest smaller and larger
* size. At the end, choose from one of those.
* Finally, go and fetch the pixel data.
*/
Image *EwmhGetIcon(ScreenInfo *scr, TwmWindow *twm_win)
{
int fetch_offset;
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned long *prop;
int wanted_area;
int smaller, larger;
int offset;
int smaller_offset, larger_offset;
int i;
int width, height;
fetch_offset = 0;
if(XGetWindowProperty(dpy, twm_win->w, XA__NET_WM_ICON,
fetch_offset, 8 * 1024, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success || nitems == 0) {
return NULL;
}
if(actual_format != 32) {
XFree(prop);
return NULL;
}
#ifdef DEBUG_EWMH
fprintf(stderr, "_NET_WM_ICON data fetched\n");
#endif
/*
* Usually the icons are square, but that is not a rule.
* So we measure the area instead.
*
* Approach wanted size from both directions and at the end,
* choose the "nearest".
*/
wanted_area = Scr->PreferredIconWidth * Scr->PreferredIconHeight;
smaller = 0;
larger = 999999;
smaller_offset = -1;
larger_offset = -1;
i = 0;
for(;;) {
offset = i;
int w = prop[i++];
int h = prop[i++];
int size = w * h;
const int area = w * h;
#ifdef DEBUG_EWMH
fprintf(stderr, "[%d+%d] w=%d h=%d\n", fetch_offset, offset, w, h);
#endif
if(area == wanted_area) {
#ifdef DEBUG_EWMH
fprintf(stderr, "exact match [%d+%d=%d] w=%d h=%d\n", fetch_offset, offset,
fetch_offset + offset, w, h);
#endif /* DEBUG_EWMH */
smaller_offset = fetch_offset + offset;
smaller = area;
larger_offset = -1;
break;
}
else if(area < wanted_area) {
if(area > smaller) {
#ifdef DEBUG_EWMH
fprintf(stderr, "increase smaller, was [%d]\n", smaller_offset);
#endif /* DEBUG_EWMH */
smaller = area;
smaller_offset = fetch_offset + offset;
}
}
else { /* area > wanted_area */
if(area < larger) {
#ifdef DEBUG_EWMH
fprintf(stderr, "decrease larger, was [%d]\n", larger_offset);
#endif /* DEBUG_EWMH */
larger = area;
larger_offset = fetch_offset + offset;
}
}
if(i + size + 2 > nitems) {
#ifdef DEBUG_EWMH
fprintf(stderr, "not enough data: %d + %d > %ld \n", i, size, nitems);
#endif /* DEBUG_EWMH */
if(i + size + 2 <= nitems + bytes_after / 4) {
/* we can fetch some more... */
XFree(prop);
fetch_offset += i + size;
if(XGetWindowProperty(dpy, twm_win->w, XA__NET_WM_ICON,
fetch_offset, 8 * 1024, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success) {
continue;
}
i = 0;
continue;
}
break;
}
i += size;
}
/*
* Choose which icon approximates our desired size best.
*/
int area = 0;
ALLOW_DEAD_STORE(area); // all branches below init it
if(smaller_offset >= 0) {
if(larger_offset >= 0) {
/* choose the nearest */
#ifdef DEBUG_EWMH
fprintf(stderr, "choose nearest %d %d\n", smaller, larger);
#endif /* DEBUG_EWMH */
if((double)larger / wanted_area > (double)wanted_area / smaller) {
offset = smaller_offset;
area = smaller;
}
else {
offset = larger_offset;
area = larger;
}
}
else {
/* choose smaller */
#ifdef DEBUG_EWMH
fprintf(stderr, "choose smaller (only) %d\n", smaller);
#endif /* DEBUG_EWMH */
offset = smaller_offset;
area = smaller;
}
}
else if(larger_offset >= 0) {
/* choose larger */
#ifdef DEBUG_EWMH
fprintf(stderr, "choose larger (only) %d\n", larger);
#endif /* DEBUG_EWMH */
offset = larger_offset;
area = larger;
}
else {
/* no icons found at all? */
#ifdef DEBUG_EWMH
fprintf(stderr, "nothing to choose from\n");
#endif /* DEBUG_EWMH */
XFree(prop);
return NULL;
}
/*
* Now fetch the pixels.
*/
#ifdef DEBUG_EWMH
fprintf(stderr, "offset = %d fetch_offset = %d\n", offset, fetch_offset);
fprintf(stderr, "offset + 2 + area = %d fetch_offset + nitems = %ld\n",
offset + 2 + area, fetch_offset + nitems);
#endif /* DEBUG_EWMH */
if(offset < fetch_offset ||
offset + 2 + area > fetch_offset + nitems) {
XFree(prop);
fetch_offset = offset;
#ifdef DEBUG_EWMH
fprintf(stderr, "refetching from %d\n", fetch_offset);
#endif /* DEBUG_EWMH */
if(XGetWindowProperty(dpy, twm_win->w, XA__NET_WM_ICON,
fetch_offset, 2 + area, False, XA_CARDINAL,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success) {
return NULL;
}
}
i = offset - fetch_offset;
width = prop[i++];
height = prop[i++];
#ifdef DEBUG_EWMH
fprintf(stderr, "Chosen [%d] w=%d h=%d area=%d\n", offset, width, height, area);
#endif /* DEBUG_EWMH */
assert(width * height == area);
Image *image = ExtractIcon(scr, &prop[i], width, height);
XFree(prop);
return image;
}
static uint16_t *buffer_16bpp;
static uint32_t *buffer_32bpp;
static void convert_for_16(int w, int x, int y, int argb)
{
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = (argb >> 0) & 0xFF;
buffer_16bpp [y * w + x] = ((r >> 3) << 11) + ((g >> 2) << 5) + (b >> 3);
}
static void convert_for_32(int w, int x, int y, int argb)
{
buffer_32bpp [y * w + x] = argb & 0x00FFFFFF;
}
static Image *ExtractIcon(ScreenInfo *scr, unsigned long *prop, int width,
int height)
{
XImage *ximage;
void (*store_data)(int w, int x, int y, int argb);
int x, y, transparency;
int rowbytes;
unsigned char *maskbits;
GC gc;
Pixmap pixret;
Pixmap mask;
Image *image;
int i;
ximage = NULL;
/** XXX sort of duplicated from util.c:LoadJpegImage() */
if(scr->d_depth == 16) {
store_data = convert_for_16;
buffer_16bpp = malloc(width * height * sizeof(buffer_16bpp[0]));
buffer_32bpp = NULL;
ximage = XCreateImage(dpy, CopyFromParent, scr->d_depth, ZPixmap, 0,
(char *) buffer_16bpp, width, height, 16, width * 2);
}
else if(scr->d_depth == 24 || scr->d_depth == 32) {
store_data = convert_for_32;
buffer_32bpp = malloc(width * height * sizeof(buffer_32bpp[0]));
buffer_16bpp = NULL;
ximage = XCreateImage(dpy, CopyFromParent, scr->d_depth, ZPixmap, 0,
(char *) buffer_32bpp, width, height, 32, width * 4);
}
else {
#ifdef DEBUG_EWMH
fprintf(stderr, "Screen unsupported depth for 32-bit icon: %d\n", scr->d_depth);
#endif /* DEBUG_EWMH */
XFree(prop);
return NULL;
}
if(ximage == NULL) {
#ifdef DEBUG_EWMH
fprintf(stderr, "cannot create image for icon\n");
#endif /* DEBUG_EWMH */
XFree(prop);
return NULL;
}
transparency = 0;
rowbytes = (width + 7) / 8;
maskbits = calloc(height, rowbytes);
/*
* Copy all ARGB pixels to the pixmap (the RGB part), and the bitmap (the
* Alpha, or opaqueness part). If any pixels are transparent, we're going
* to need a shape.
*/
i = 0;
for(y = 0; y < height; y++) {
for(x = 0; x < width; x++) {
unsigned long argb = prop[i++];
store_data(width, x, y, argb);
int opaque = ((argb >> 24) & 0xFF) >= 0x80; /* arbitrary cutoff */
if(opaque) {
maskbits [rowbytes * y + (x / 8)] |= 0x01 << (x % 8);
}
else {
transparency = 1;
}
}
}
gc = DefaultGC(dpy, scr->screen);
pixret = XCreatePixmap(dpy, scr->Root, width, height, scr->d_depth);
XPutImage(dpy, pixret, gc, ximage, 0, 0, 0, 0, width, height);
XDestroyImage(ximage); /* also frees buffer_{16,32}bpp */
ximage = NULL;
mask = None;
if(transparency) {
mask = XCreatePixmapFromBitmapData(dpy, scr->Root, (char *)maskbits,
width, height, 1, 0, 1);
}
free(maskbits);
image = AllocImage();
image->width = width;
image->height = height;
image->pixmap = pixret;
image->mask = mask;
return image;
}
/*
* Handle a PropertyNotify on _NET_WM_ICON.
*/
static void EwmhHandle_NET_WM_ICONNotify(XPropertyEvent *event,
TwmWindow *twm_win)
{
unsigned long valuemask; /* mask for create windows */
XSetWindowAttributes attributes; /* attributes for create windows */
Icon *icon = twm_win->icon;
int x;
#ifdef DEBUG_EWMH
fprintf(stderr, "EwmhHandlePropertyNotify: NET_WM_ICON\n");
#endif /* DEBUG_EWMH */
/*
* If there is no icon yet, we'll look at this property
* later, if and when we do create an icon.
*/
if(!icon || icon->match != match_net_wm_icon) {
#ifdef DEBUG_EWMH
fprintf(stderr, "no icon, or not match_net_wm_icon\n");
#endif /* DEBUG_EWMH */
return;
}
Image *image = EwmhGetIcon(Scr, twm_win);
/* TODO: de-duplicate with handling of XA_WM_HINTS */
{
Image *old_image = icon->image;
icon->image = image;
FreeImage(old_image);
}
if(twm_win->icon->bm_w) {
XDestroyWindow(dpy, twm_win->icon->bm_w);
}
valuemask = CWBackPixmap;
attributes.background_pixmap = image->pixmap;
x = GetIconOffset(twm_win->icon);
twm_win->icon->bm_w =
XCreateWindow(dpy, twm_win->icon->w, x, 0,
twm_win->icon->width,
twm_win->icon->height,
0, Scr->d_depth,
CopyFromParent, Scr->d_visual,
valuemask, &attributes);
if(image->mask) {
XShapeCombineMask(dpy, twm_win->icon->bm_w, ShapeBounding, 0, 0, image->mask,
ShapeSet);
XShapeCombineMask(dpy, twm_win->icon->w, ShapeBounding, x, 0, image->mask,
ShapeSet);
}
else {
XRectangle rect;
rect.x = x;
rect.y = 0;
rect.width = twm_win->icon->width;
rect.height = twm_win->icon->height;
XShapeCombineRectangles(dpy, twm_win->icon->w, ShapeBounding, 0,
0, &rect, 1, ShapeUnion, 0);
}
XMapSubwindows(dpy, twm_win->icon->w);
RedoIconName(twm_win);
}
/*
* Handle a PropertyNotify on _NET_WM_STRUT(PARTIAL).
*/
static void EwmhHandle_NET_WM_STRUTNotify(XPropertyEvent *event,
TwmWindow *twm_win)
{
EwmhGetStrut(twm_win, true);
}
/*
* Handle a _NET_WM_STATE ClientMessage.
*/
static int atomToFlag(Atom a)
{
#ifdef DEBUG_EWMH
# define CRWARN(x) fprintf(stderr, "atomToFlag: ignoring " #x "\n")
#else
# define CRWARN(x) (void)0
#endif
#define CHKNRET(st) \
if(a == XA__NET_WM_##st) { \
if(LookInNameList(Scr->EWMHIgnore, #st)) { \
CRWARN(st); \
return 0; \
} \
return EWMH_##st; \
}
/* Check (potentially ignoring) various flags we know */
CHKNRET(STATE_MAXIMIZED_VERT);
CHKNRET(STATE_MAXIMIZED_HORZ);
CHKNRET(STATE_FULLSCREEN);
CHKNRET(STATE_SHADED);
CHKNRET(STATE_ABOVE);
CHKNRET(STATE_BELOW);
#undef CHKNRET
#undef CRWARN
/* Else we don't recognize it */
return 0;
}
/*
* Handle the NET_WM_STATE client message.
* It can change 1 or 2 values represented in NET_WM_STATE.
* The second change is allowed
* specifically for (re)setting horizontal and vertical maximalisation in
* one go. Treat that as a special case.
*/
static void EwmhClientMessage_NET_WM_STATE(XClientMessageEvent *msg)
{
Window w = msg->window;
TwmWindow *twm_win;
int change, change1, change2, newValue;
twm_win = GetTwmWindow(w);
if(twm_win == NULL) {
return;
}
/*
* Due to EWMHIgnore, it's possible to wind up with change1=0 and
* change2=something, so swap 'em if that happens.
*/
change1 = atomToFlag(msg->data.l[1]);
change2 = atomToFlag(msg->data.l[2]);
if(change1 == 0 && change2 != 0) {
change1 = change2;
change2 = 0;
}
change = change1 | change2;
switch(msg->data.l[0]) {
case NET_WM_STATE_REMOVE:
#ifdef DEBUG_EWMH
printf("NET_WM_STATE_REMOVE: ");
#endif
newValue = 0;
break;
case NET_WM_STATE_ADD:
#ifdef DEBUG_EWMH
printf("NET_WM_STATE_ADD: ");
#endif
newValue = change;
break;
case NET_WM_STATE_TOGGLE:
#ifdef DEBUG_EWMH
printf("NET_WM_STATE_TOGGLE: ");
#endif
newValue = ~twm_win->ewmhFlags & change;
break;
default:
#ifdef DEBUG_EWMH
printf("invalid operation in NET_WM_STATE: %ld\n", msg->data.l[0]);
#endif
return;
}
#ifdef DEBUG_EWMH
printf("%s and %s\n", XGetAtomName(dpy, msg->data.l[1]),
XGetAtomName(dpy, msg->data.l[2]));
#endif
/*
* Special-case the horizontal and vertical zoom.
* You can turn them both on or both off, but no other combinations
* are done as a unit.
*/
if(change == (EWMH_STATE_MAXIMIZED_VERT | EWMH_STATE_MAXIMIZED_HORZ) &&
(newValue == 0 || newValue == change)) {
EwmhClientMessage_NET_WM_STATEchange(twm_win, change, newValue);
}
else {
EwmhClientMessage_NET_WM_STATEchange(twm_win, change1, newValue & change1);
if(change2 != 0) {
EwmhClientMessage_NET_WM_STATEchange(twm_win, change2, newValue & change2);
}
}
}
/*
* change - bitmask of settings that possibly change. Only one bit is
* set in this, with the possible exception of
* *MAXIMIZED_{VERT,HORIZ} which can be set together.
* newValue - bits with the new values; only valid bits are the ones
* in change.
*/
static void EwmhClientMessage_NET_WM_STATEchange(TwmWindow *twm_win, int change,
int newValue)
{
/* Now check what we need to change */
if(change & (EWMH_STATE_MAXIMIZED_VERT | EWMH_STATE_MAXIMIZED_HORZ |
EWMH_STATE_FULLSCREEN)) {
int newZoom = ZOOM_NONE;
switch(newValue) {
case 0:
newZoom = twm_win->zoomed; /* turn off whatever zoom */
break;
case EWMH_STATE_MAXIMIZED_VERT:
newZoom = F_ZOOM;
break;
case EWMH_STATE_MAXIMIZED_HORZ:
newZoom = F_HORIZOOM;
break;
case EWMH_STATE_MAXIMIZED_HORZ | EWMH_STATE_MAXIMIZED_VERT:
newZoom = F_FULLZOOM;
break;
case EWMH_STATE_FULLSCREEN:
newZoom = F_FULLSCREENZOOM;
break;
}
fullzoom(twm_win, newZoom);
}
else if(change & EWMH_STATE_SHADED) {
#ifdef DEBUG_EWMH
printf("EWMH_STATE_SHADED: newValue: %d old: %d\n", newValue,
twm_win->ewmhFlags & EWMH_STATE_SHADED);
#endif
if((twm_win->ewmhFlags & EWMH_STATE_SHADED) ^ newValue) {
/* Toggle the shade/squeeze state */
#ifdef DEBUG_EWMH
printf("EWMH_STATE_SHADED: change it\n");
#endif
Squeeze(twm_win);
}
}
else if(change & (EWMH_STATE_ABOVE | EWMH_STATE_BELOW)) {
/*
* Other changes call into ctwm code, which in turn calls back to
* EWMH code to update the ewmhFlags and the property. This one
* we handle completely internally.
*/
unsigned omask = 0, oval = 0;
const int prepri = OtpEffectivePriority(twm_win);
/* Which bits are we changing and what to? */
#define DOBIT(fld) do { \
if(change & EWMH_STATE_##fld) { omask |= OTP_AFLAG_##fld; } \
if(newValue & EWMH_STATE_##fld) { oval |= OTP_AFLAG_##fld; } \
} while(0)
DOBIT(ABOVE);
DOBIT(BELOW);
#undef DOBIT
/* Update OTP as necessary */
OtpSetAflagMask(twm_win, omask, oval);
if(OtpEffectivePriority(twm_win) != prepri) {
OtpRestackWindow(twm_win);
}
/* Set the EWMH property back on the window */
EwmhSet_NET_WM_STATE(twm_win, change);
}
}
/*
* Handle the _NET_ACTIVE_WINDOW client message.
* Pagers would send such a message to "activate" a window.
*
* What does "activate" really mean? It isn't properly described.
*
* Let's presume that it means that the window is de-iconified and gets
* focus. The mouse may be moved to it (but not all window button apps
* do that). But is it always raised or should that depend on the
* RaiseOnWarp option?
*/
static void EwmhClientMessage_NET_ACTIVE_WINDOW(XClientMessageEvent *msg)
{
Window w = msg->window;
TwmWindow *twm_win;
twm_win = GetTwmWindow(w);
if(twm_win == NULL) {
return;
}
if(!twm_win->mapped) {
DeIconify(twm_win);
}
#if 0
WarpToWindow(twm_win, Scr->RaiseOnWarp /* True ? */);
#else
/*
* Keep the mouse pointer where it is (typically the dock).
* WarpToWindow() would change the current workspace if needed to go
* to the window. But pagers would only send this message for
* windows in the current workspace, I expect.
*/
if(Scr->RaiseOnWarp) {
AutoRaiseWindow(twm_win);
}
SetFocus(twm_win, msg->data.l[1]);
#endif
}
/*
* Ugly implementation of _NET_WM_MOVERESIZE.
*
* window = window to be moved or resized
* message_type = _NET_WM_MOVERESIZE
* format = 32
* data.l[0] = x_root
* data.l[1] = y_root
* data.l[2] = direction
* data.l[3] = button
* data.l[4] = source indication
*/
static void EwmhClientMessage_NET_WM_MOVERESIZE(XClientMessageEvent *msg)
{
TwmWindow *twm_win;
twm_win = GetTwmWindow(msg->window);
if(twm_win == NULL) {
return;
}
if(!twm_win->mapped) {
DeIconify(twm_win);
}
switch(msg->data.l[2]) {
case _NET_WM_MOVERESIZE_SIZE_TOPLEFT:
case _NET_WM_MOVERESIZE_SIZE_TOP:
case _NET_WM_MOVERESIZE_SIZE_TOPRIGHT:
case _NET_WM_MOVERESIZE_SIZE_RIGHT:
case _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT:
case _NET_WM_MOVERESIZE_SIZE_BOTTOM:
case _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT:
case _NET_WM_MOVERESIZE_SIZE_LEFT:
case _NET_WM_MOVERESIZE_SIZE_KEYBOARD: {
/* Fake up as if we triggered f.resize on it */
XEvent xevent = synth_btnevent_for_moveresize(twm_win);
/*
* The resize won't actually start until we cross the cursor
* over a border. Perhaps we should find the nearest corner,
* and pre-warp the cursor there? That may be less friendly
* for the user, since it might not be as predictable, and
* having the cursor zoom off without warning is probably a
* little surprising...
*/
ExecuteFunction(F_RESIZE, "", twm_win->frame, twm_win,
&xevent, C_WINDOW, false);
/*
* It's not guaranteed that this actually began as a button
* press, but our implementation is expecting the button to
* be held down all through the move, so that
* ExecuteFunction() won't actually end until there's a
* button release of some kind, which will trigger
* HandleButtonRelease() properly. So we don't need to call
* it ourselves.
*/
break;
}
case _NET_WM_MOVERESIZE_MOVE:
case _NET_WM_MOVERESIZE_MOVE_KEYBOARD: {
/* Fake up as if we triggered f.move on it */
XEvent xevent = synth_btnevent_for_moveresize(twm_win);
ExecuteFunction(F_MOVE, "", twm_win->frame, twm_win,
&xevent, C_WINDOW, false);
/* X-ref HandleButtonRelease() discussion above */
break;
}
case _NET_WM_MOVERESIZE_CANCEL:
/*
* TODO: check if the twm_win is the same.
* TODO: check how to make this actually work.
*
* As currently implemented, I don't believe we ever need to
* do anything here. All the needed cleanup should happen in
* our ButtonRelease handler.
*/
break;
}
}
static XEvent
synth_btnevent_for_moveresize(TwmWindow *twm_win)
{
XEvent xevent;
Window root = twm_win->parent_vs->window;
Window child = twm_win->w;
int x_root = twm_win->frame_x;
int y_root = twm_win->frame_y;
int x_win = 0;
int y_win = 0;
unsigned int dummy_mask;
/* Find the pointer */
XQueryPointer(dpy, twm_win->frame, &root, &child, &x_root, &y_root,
&x_win, &y_win, &dummy_mask);
/* Synthesize a button event */
xevent.type = ButtonPress;
xevent.xbutton.root = root;
xevent.xbutton.window = child;
xevent.xbutton.x_root = x_root;
xevent.xbutton.y_root = y_root;
xevent.xbutton.x = x_win;
xevent.xbutton.y = y_win;
xevent.xbutton.time = EventTime;
return xevent;
}
/*
* Implementation of _NET_CLOSE_WINDOW
*
* window = window to be closed
* message_type = _NET_CLOSE_WINDOW
* format = 32
* data.l[0] = timestamp
* data.l[1] = source indication:
* 0 Clients that support only older version of this spec
* 1 for normal applications, and
* 2 for pagers and other Clients that represent direct user actions
* data.l[2] = 0
* data.l[3] = 0
* data.l[4] = 0
*/
static void EwmhClientMessage_NET_CLOSE_WINDOW(XClientMessageEvent *msg)
{
TwmWindow *tmp_win;
tmp_win = GetTwmWindow(msg->window);
if(tmp_win != NULL) {
ButtonPressed = -1;
ExecuteFunction(F_DELETE, NULL, msg->window, tmp_win,
(XEvent *)msg, C_NO_CONTEXT, 0);
}
}
/*
* Handle any PropertyNotify.
*/
int EwmhHandlePropertyNotify(XPropertyEvent *event, TwmWindow *twm_win)
{
if(event->atom == XA__NET_WM_ICON) {
EwmhHandle_NET_WM_ICONNotify(event, twm_win);
return 1;
}
else if(event->atom == XA__NET_WM_STRUT_PARTIAL ||
event->atom == XA__NET_WM_STRUT) {
EwmhHandle_NET_WM_STRUTNotify(event, twm_win);
return 1;
}
else if(event->atom == XA__NET_WM_NAME) {
char *prop = GetWMPropertyString(twm_win->w, XA__NET_WM_NAME);
if(prop == NULL) {
FreeWMPropertyString(twm_win->names.net_wm_name);
twm_win->names.net_wm_name = NULL;
apply_window_name(twm_win);
return 1;
}
if(twm_win->names.net_wm_name != NULL
&& strcmp(twm_win->names.net_wm_name, prop) == 0) {
/* No change, just free and skip out */
free(prop);
return 1;
}
/* It's changing, free the old and bring in the new */
FreeWMPropertyString(twm_win->names.net_wm_name);
twm_win->names.net_wm_name = prop;
/* Kick the reset process */
apply_window_name(twm_win);
return 1;
}
else if(event->atom == XA__NET_WM_ICON_NAME) {
char *prop = GetWMPropertyString(twm_win->w, XA__NET_WM_ICON_NAME);
if(prop == NULL) {
FreeWMPropertyString(twm_win->names.net_wm_icon_name);
twm_win->names.net_wm_icon_name = NULL;
apply_window_icon_name(twm_win);
return 1;
}
if(twm_win->names.net_wm_icon_name != NULL
&& strcmp(twm_win->names.net_wm_icon_name, prop) == 0) {
/* No change, just free and skip out */
free(prop);
return 1;
}
/* It's changing, free the old and bring in the new */
FreeWMPropertyString(twm_win->names.net_wm_icon_name);
twm_win->names.net_wm_icon_name = prop;
/* Kick the reset process */
apply_window_icon_name(twm_win);
return 1;
}
return 0;
}
/*
* Set the _NET_WM_DESKTOP property for the current workspace.
*/
void EwmhSet_NET_WM_DESKTOP(TwmWindow *twm_win)
{
WorkSpace *ws;
VirtualScreen *vs = twm_win->vs;
if(vs != NULL) {
ws = vs->wsw->currentwspc;
}
else {
ws = NULL;
}
EwmhSet_NET_WM_DESKTOP_ws(twm_win, ws);
}
/*
* Set the _NET_WM_DESKTOP property for the given workspace.
*/
void EwmhSet_NET_WM_DESKTOP_ws(TwmWindow *twm_win, WorkSpace *ws)
{
unsigned long workspaces[MAXWORKSPACE];
int n = 0;
if(!Scr->workSpaceManagerActive) {
workspaces[n++] = 0;
}
else if(twm_win->occupation == fullOccupation) {
workspaces[n++] = ALL_WORKSPACES;
}
else {
/*
* Our windows can occupy multiple workspaces ("virtual desktops" in
* EWMH terminology) at once. Extend the _NET_WM_DESKTOP property
* by setting it to multiple CARDINALs if this occurs.
* Put the currently visible workspace (if any) first, since typical
* pager apps don't know about this.
*/
int occupation = twm_win->occupation;
/*
* Set visible workspace number.
*/
if(ws != NULL) {
int wsn = ws->number;
workspaces[n++] = wsn;
occupation &= ~(1 << wsn);
}
/*
* Set any other workspace numbers.
*/
if(occupation != 0) {
int i;
int mask = 1;
for(i = 0; i < MAXWORKSPACE; i++) {
if(occupation & mask) {
workspaces[n++] = i;
}
mask <<= 1;
}
}
}
XChangeProperty(dpy, twm_win->w,
XA__NET_WM_DESKTOP, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)workspaces, n);
}
static unsigned long EwmhGetWindowProperty(Window w, Atom name, Atom type)
{
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned long *prop;
unsigned long value;
if(XGetWindowProperty(dpy, w, name,
0, 1, False, type,
&actual_type, &actual_format, &nitems,
&bytes_after, (unsigned char **)&prop) != Success) {
return 0;
}
if(actual_format == 32) {
if(nitems >= 1) {
value = prop[0];
}
else {
value = 0;
}
}
else {
value = 0;
}
XFree(prop);
return value;
}
/*
* Simple function to get multiple properties of format 32.
* If it fails, returns NULL, and *nitems_return == 0.
*/
static unsigned long *EwmhGetWindowProperties(Window w, Atom name, Atom type,
unsigned long *nitems_return)
{
Atom actual_type;
int actual_format;
unsigned long bytes_after;
unsigned long *prop;
if(XGetWindowProperty(dpy, w, name,
0, 8192, False, type,
&actual_type, &actual_format, nitems_return,
&bytes_after, (unsigned char **)&prop) != Success) {
*nitems_return = 0;
return NULL;
}
if(actual_format != 32) {
XFree(prop);
prop = NULL;
*nitems_return = 0;
}
return prop;
}
int EwmhGetOccupation(TwmWindow *twm_win)
{
unsigned long nitems;
unsigned long *prop;
int occupation;
occupation = 0;
prop = EwmhGetWindowProperties(twm_win->w,
XA__NET_WM_DESKTOP, XA_CARDINAL, &nitems);
if(prop) {
int i;
for(i = 0; i < nitems; i++) {
unsigned int val = prop[i];
if(val == ALL_WORKSPACES) {
occupation = fullOccupation;
}
else if(val < Scr->workSpaceMgr.count) {
occupation |= 1 << val;
}
else {
occupation |= 1 << (Scr->workSpaceMgr.count - 1);
}
}
occupation &= fullOccupation;
XFree(prop);
}
return occupation;
}
/*
* The message to change the desktop of a window doesn't recognize
* that it may be in more than one.
*
* Therefore the following heuristic is applied:
* - the window is removed from the workspace where it is visible (if any);
* - it is added to the given workspace;
* - other occupation bits are left unchanged.
*
* If asked to put it on a too high numbered workspace, put it on
* the highest possible.
*/
static void EwmhClientMessage_NET_WM_DESKTOP(XClientMessageEvent *msg)
{
Window w = msg->window;
TwmWindow *twm_win;
int occupation;
VirtualScreen *vs;
unsigned int val;
twm_win = GetTwmWindow(w);
if(twm_win == NULL) {
return;
}
occupation = twm_win->occupation;
/* Remove from visible workspace */
if((vs = twm_win->vs) != NULL) {
occupation &= ~(1 << vs->wsw->currentwspc->number);
}
val = (unsigned int)msg->data.l[0];
/* Add to requested workspace (or to all) */
if(val == ALL_WORKSPACES) {
occupation = fullOccupation;
}
else if(val < Scr->workSpaceMgr.count) {
occupation |= 1 << val;
}
else {
occupation |= 1 << (Scr->workSpaceMgr.count - 1);
}
ChangeOccupation(twm_win, occupation);
}
/*
* Delete all properties that should be removed from a withdrawn
* window.
*/
void EwmhUnmapNotify(TwmWindow *twm_win)
{
XDeleteProperty(dpy, twm_win->w, XA__NET_WM_DESKTOP);
}
/*
* Add a new window to _NET_CLIENT_LIST.
* Newer windows are always added at the end.
*
* Look at new_win->iconmanagerlist as an optimization for
* !LookInList(Scr->IconMgrNoShow, new_win->name, &new_win->class)).
*/
void EwmhAddClientWindow(TwmWindow *new_win)
{
if(Scr->ewmh_CLIENT_LIST_size == 0) {
return;
}
if(new_win->iconmanagerlist != NULL &&
!new_win->iswspmgr &&
!new_win->isiconmgr) {
Scr->ewmh_CLIENT_LIST_used++;
if(Scr->ewmh_CLIENT_LIST_used > Scr->ewmh_CLIENT_LIST_size) {
long *tp;
int tsz = Scr->ewmh_CLIENT_LIST_size;
Scr->ewmh_CLIENT_LIST_size *= 2;
tp = realloc(Scr->ewmh_CLIENT_LIST,
sizeof(long) * Scr->ewmh_CLIENT_LIST_size);
if(tp == NULL) {
Scr->ewmh_CLIENT_LIST_size = tsz;
fprintf(stderr, "Unable to allocate memory for EWMH client list.\n");
return;
}
Scr->ewmh_CLIENT_LIST = tp;
}
if(Scr->ewmh_CLIENT_LIST) {
Scr->ewmh_CLIENT_LIST[Scr->ewmh_CLIENT_LIST_used - 1] = new_win->w;
}
else {
Scr->ewmh_CLIENT_LIST_size = 0;
fprintf(stderr, "Unable to allocate memory for EWMH client list.\n");
return;
}
XChangeProperty(dpy, Scr->Root, XA__NET_CLIENT_LIST, XA_WINDOW, 32,
PropModeReplace, (unsigned char *)Scr->ewmh_CLIENT_LIST,
Scr->ewmh_CLIENT_LIST_used);
}
}
void EwmhDeleteClientWindow(TwmWindow *old_win)
{
int i;
if(old_win->ewmhFlags & EWMH_HAS_STRUT) {
EwmhRemoveStrut(old_win);
}
/*
* Remove the window from _NET_CLIENT_LIST.
*/
if(Scr->ewmh_CLIENT_LIST_size == 0) {
return;
}
for(i = Scr->ewmh_CLIENT_LIST_used - 1; i >= 0; i--) {
if(Scr->ewmh_CLIENT_LIST[i] == old_win->w) {
memmove(&Scr->ewmh_CLIENT_LIST[i],
&Scr->ewmh_CLIENT_LIST[i + 1],
(Scr->ewmh_CLIENT_LIST_used - 1 - i) * sizeof(Scr->ewmh_CLIENT_LIST[0]));
Scr->ewmh_CLIENT_LIST_used--;
if(Scr->ewmh_CLIENT_LIST_used &&
(Scr->ewmh_CLIENT_LIST_used * 3) < Scr->ewmh_CLIENT_LIST_size) {
Scr->ewmh_CLIENT_LIST_size /= 2;
Scr->ewmh_CLIENT_LIST = realloc(Scr->ewmh_CLIENT_LIST,
sizeof((Scr->ewmh_CLIENT_LIST[0])) * Scr->ewmh_CLIENT_LIST_size);
/* memory shrinking, shouldn't have problems */
}
break;
}
}
/* If window was not found, there is no need to update the property. */
if(i >= 0) {
XChangeProperty(dpy, Scr->Root, XA__NET_CLIENT_LIST, XA_WINDOW, 32,
PropModeReplace, (unsigned char *)Scr->ewmh_CLIENT_LIST,
Scr->ewmh_CLIENT_LIST_used);
}
}
/*
* Similar to EwmhAddClientWindow() and EwmhDeleteClientWindow(),
* but the windows are in stacking order.
* Therefore we look at the OTPs, which are by definition in the correct order.
*/
void EwmhSet_NET_CLIENT_LIST_STACKING(void)
{
int size;
unsigned long *prop;
TwmWindow *twm_win;
int i;
/* Expect the same number of windows as in the _NET_CLIENT_LIST */
size = Scr->ewmh_CLIENT_LIST_used + 10;
prop = calloc(size, sizeof(unsigned long));
if(prop == NULL) {
return;
}
i = 0;
for(twm_win = OtpBottomWin();
twm_win != NULL;
twm_win = OtpNextWinUp(twm_win)) {
if(twm_win->iconmanagerlist != NULL &&
!twm_win->iswspmgr &&
!twm_win->isiconmgr) {
prop[i] = twm_win->w;
i++;
if(i > size) {
fprintf(stderr, "Too many stacked windows\n");
break;
}
}
}
if(i != Scr->ewmh_CLIENT_LIST_used) {
fprintf(stderr, "Incorrect number of stacked windows: %d (expected %d)\n",
i, Scr->ewmh_CLIENT_LIST_used);
}
XChangeProperty(dpy, Scr->Root, XA__NET_CLIENT_LIST_STACKING, XA_WINDOW, 32,
PropModeReplace, (unsigned char *)prop, i);
free(prop);
}
void EwmhSet_NET_ACTIVE_WINDOW(Window w)
{
unsigned long prop[1];
prop[0] = w;
XChangeProperty(dpy, Scr->Root, XA__NET_ACTIVE_WINDOW, XA_WINDOW, 32,
PropModeReplace, (unsigned char *)prop, 1);
}
/*
* Get window properties as relevant when the window is initially mapped.
*
* So far, only NET_WM_WINDOW_TYPE and _NET_WM_STRUT_PARTIAL.
* In particular, most of the initial value of _NET_WM_STATE is ignored. TODO.
*
* Also do any generic initialisation needed to EWMH-specific fields
* in a TwmWindow.
*/
void EwmhGetProperties(TwmWindow *twm_win)
{
twm_win->ewmhFlags = 0;
Atom type = EwmhGetWindowProperty(twm_win->w, XA__NET_WM_WINDOW_TYPE, XA_ATOM);
if(type == XA__NET_WM_WINDOW_TYPE_DESKTOP) {
twm_win->ewmhWindowType = wt_Desktop;
}
else if(type == XA__NET_WM_WINDOW_TYPE_DOCK) {
twm_win->ewmhWindowType = wt_Dock;
}
else {
twm_win->ewmhWindowType = wt_Normal;
}
EwmhGetStrut(twm_win, false);
/* Only the 3 listed states are supported for now */
twm_win->ewmhFlags |= EwmhGet_NET_WM_STATE(twm_win) &
(EWMH_STATE_ABOVE | EWMH_STATE_BELOW | EWMH_STATE_SHADED);
}
/* Only used in initially mapping a window */
int EwmhGetInitPriority(TwmWindow *twm_win)
{
switch(twm_win->ewmhWindowType) {
case wt_Desktop:
return EWMH_PRI_DESKTOP;
case wt_Dock:
return EWMH_PRI_DOCK;
default:
return 0;
}
}
bool EwmhHasBorder(TwmWindow *twm_win)
{
switch(twm_win->ewmhWindowType) {
case wt_Desktop:
case wt_Dock:
return false;
default:
return true;
}
}
bool EwmhHasTitle(TwmWindow *twm_win)
{
switch(twm_win->ewmhWindowType) {
case wt_Desktop:
case wt_Dock:
return false;
default:
return true;
}
}
bool EwmhOnWindowRing(TwmWindow *twm_win)
{
switch(twm_win->ewmhWindowType) {
case wt_Desktop:
case wt_Dock:
return false;
default:
return true;
}
}
/*
* Recalculate the effective border values from the remembered struts.
* Interestingly it is not documented how to do that.
* Usually only one dock is present on each side, so it shouldn't matter
* too much, but I presume that maximizing the values is the thing to do.
*/
static void EwmhRecalculateStrut(void)
{
int left = 0;
int right = 0;
int top = 0;
int bottom = 0;
EwmhStrut *strut = Scr->ewmhStruts;
while(strut != NULL) {
left = max(left, strut->left);
right = max(right, strut->right);
top = max(top, strut->top);
bottom = max(bottom, strut->bottom);
strut = strut->next;
}
Scr->BorderLeft = left;
Scr->BorderRight = right;
Scr->BorderTop = top;
Scr->BorderBottom = bottom;
// Bordered layout may have changed
Scr->BorderedLayout = RLayoutCopyCropped(Scr->Layout,
left, right, top, bottom);
if(Scr->BorderedLayout == NULL) {
Scr->BorderedLayout = Scr->Layout; // nothing to crop
}
EwmhSet_NET_WORKAREA(Scr);
}
/*
* Check _NET_WM_STRUT_PARTIAL or _NET_WM_STRUT.
* These are basically automatic settings for Border{Left,Right,Top,Bottom}.
*
* If any values are found, collect them in a list of strut values
* belonging to Scr. When a window is added or removed that has struts,
* the new effective value must be calculated. The expectation is that
* at most a handful of windows will have struts.
*
* If update is true, this is called as an update for an existing window.
*/
static void EwmhGetStrut(TwmWindow *twm_win, bool update)
{
unsigned long nitems;
unsigned long *prop;
EwmhStrut *strut;
prop = EwmhGetWindowProperties(twm_win->w,
XA__NET_WM_STRUT_PARTIAL, XA_CARDINAL,
&nitems);
if(prop == NULL) {
prop = EwmhGetWindowProperties(twm_win->w,
XA__NET_WM_STRUT, XA_CARDINAL, &nitems);
if(prop == NULL) {
return;
}
}
if(nitems < 4) {
#ifdef DEBUG_EWMH
/* This happens a lot, despite returning Success ??? */
printf("struts: prop = %p, nitems = %ld\n", prop, nitems);
#endif
XFree(prop);
return;
}
#ifdef DEBUG_EWMH
printf("struts: left %ld, right %ld, top %ld, bottom %ld\n",
prop[0], prop[1], prop[2], prop[3]);
#endif
/*
* If there were no struts before, the user configured margins are set
* in Border{Left,Right,Top,Bottom}. In order not to lose those values
* when recalculating them, convert them to struts for a dummy window.
*/
if(Scr->ewmhStruts == NULL &&
(Scr->BorderLeft |
Scr->BorderRight |
Scr->BorderTop |
Scr->BorderBottom) != 0) {
strut = calloc(1, sizeof(EwmhStrut));
if(strut == NULL) {
XFree(prop);
return;
}
strut->next = NULL;
strut->win = NULL;
strut->left = Scr->BorderLeft;
strut->right = Scr->BorderRight;
strut->top = Scr->BorderTop;
strut->bottom = Scr->BorderBottom;
Scr->ewmhStruts = strut;
}
strut = NULL;
/*
* Find the struts of the window that we're supposed to be updating.
* If not found, there is no problem: we'll just allocate a new
* record.
*/
if(update) {
strut = Scr->ewmhStruts;
while(strut != NULL) {
if(strut->win == twm_win) {
break;
}
strut = strut->next;
}
}
/*
* If needed, allocate a new struts record and link it in.
*/
if(strut == NULL) {
strut = calloc(1, sizeof(EwmhStrut));
if(strut == NULL) {
XFree(prop);
return;
}
strut->next = Scr->ewmhStruts;
Scr->ewmhStruts = strut;
}
strut->win = twm_win;
strut->left = prop[0];
strut->right = prop[1];
strut->top = prop[2];
strut->bottom = prop[3];
XFree(prop);
/*
* Mark this window as having contributed some struts.
* This can be checked and undone when the window is deleted.
*/
twm_win->ewmhFlags |= EWMH_HAS_STRUT;
EwmhRecalculateStrut();
}
/*
* Remove the struts associated with the given window from the
* remembered list. If found, recalculate the effective borders.
*/
static void EwmhRemoveStrut(TwmWindow *twm_win)
{
EwmhStrut **prev = &Scr->ewmhStruts;
EwmhStrut *strut = Scr->ewmhStruts;
while(strut != NULL) {
if(strut->win == twm_win) {
twm_win->ewmhFlags &= ~EWMH_HAS_STRUT;
*prev = strut->next;
free(strut);
EwmhRecalculateStrut();
break;
}
prev = &strut->next;
strut = strut->next;
}
}
/**
* Set _NET_FRAME_EXTENTS property.
* This tells the client how much space is being taken up by the window
* decorations. Some clients may need this information to position other
* windows on top of themselves. e.g., Firefox's form autofill and
* context menu will be positioned a bit wrong (high, by the height of
* the titlebar) without this.
*/
void EwmhSet_NET_FRAME_EXTENTS(TwmWindow *twm_win)
{
long data[4];
const long w = twm_win->frame_bw3D + twm_win->frame_bw;
data[0] = w; // left
data[1] = w; // right
data[2] = twm_win->title_height + w; // top
data[3] = w; // bottom
XChangeProperty(dpy, twm_win->w,
XA__NET_FRAME_EXTENTS, XA_CARDINAL,
32, PropModeReplace,
(unsigned char *)data, 4);
}
void EwmhSet_NET_SHOWING_DESKTOP(int state)
{
unsigned long prop[1];
prop[0] = state;
XChangeProperty(dpy, Scr->XineramaRoot, XA__NET_SHOWING_DESKTOP, XA_CARDINAL,
32,
PropModeReplace, (unsigned char *)prop, 1);
}
/*
* Set _NET_WM_STATE.
*
* TwmWindow.ewmhFlags keeps track of the atoms that should be in
* the list, so that we don't have to fetch or recalculate them all.
*
* XXX It's not clear that the theoretical performance gain and edge-case
* bug avoidance of the 'changes' arg is worth the complexity and
* edge-case bug creation it brings. Consider removing it.
*/
void EwmhSet_NET_WM_STATE(TwmWindow *twm_win, int changes)
{
unsigned long prop[10];
int flags;
int i;
if(changes & EWMH_STATE_MAXIMIZED_VERT) {
int newFlags = 0;
switch(twm_win->zoomed) {
case F_FULLZOOM:
newFlags = EWMH_STATE_MAXIMIZED_VERT |
EWMH_STATE_MAXIMIZED_HORZ;
break;
case F_ZOOM:
case F_LEFTZOOM:
case F_RIGHTZOOM:
newFlags = EWMH_STATE_MAXIMIZED_VERT;
break;
case F_HORIZOOM:
case F_TOPZOOM:
case F_BOTTOMZOOM:
newFlags = EWMH_STATE_MAXIMIZED_HORZ;
break;
case F_FULLSCREENZOOM:
newFlags = EWMH_STATE_FULLSCREEN;
break;
}
twm_win->ewmhFlags &= ~(EWMH_STATE_MAXIMIZED_VERT |
EWMH_STATE_MAXIMIZED_HORZ |
EWMH_STATE_FULLSCREEN);
twm_win->ewmhFlags |= newFlags;
}
if(changes & EWMH_STATE_SHADED) {
if(twm_win->squeezed) {
twm_win->ewmhFlags |= EWMH_STATE_SHADED;
}
else {
twm_win->ewmhFlags &= ~EWMH_STATE_SHADED;
}
}
if(changes & (EWMH_STATE_ABOVE | EWMH_STATE_BELOW)) {
int pri = OtpEffectiveDisplayPriority(twm_win);
twm_win->ewmhFlags &= ~(EWMH_STATE_ABOVE | EWMH_STATE_BELOW);
/*
* If it's a DOCK or DESKTOP, and where we expect those to be, we
* consider that there's nothing to tell it. Otherwise, we tell
* it ABOVE/BELOW based on where it effectively is.
*/
if(twm_win->ewmhWindowType == wt_Dock && pri == EWMH_PRI_DOCK) {
pri = 0;
}
if(twm_win->ewmhWindowType == wt_Desktop && pri == EWMH_PRI_DESKTOP) {
pri = 0;
}
if(pri > 0) {
twm_win->ewmhFlags |= EWMH_STATE_ABOVE;
}
else if(pri < 0) {
twm_win->ewmhFlags |= EWMH_STATE_BELOW;
}
}
flags = twm_win->ewmhFlags;
i = 0;
if(flags & EWMH_STATE_MAXIMIZED_VERT) {
prop[i++] = XA__NET_WM_STATE_MAXIMIZED_VERT;
}
if(flags & EWMH_STATE_MAXIMIZED_HORZ) {
prop[i++] = XA__NET_WM_STATE_MAXIMIZED_HORZ;
}
if(flags & EWMH_STATE_FULLSCREEN) {
prop[i++] = XA__NET_WM_STATE_FULLSCREEN;
}
if(flags & EWMH_STATE_SHADED) {
prop[i++] = XA__NET_WM_STATE_SHADED;
}
if(flags & EWMH_STATE_ABOVE) {
prop[i++] = XA__NET_WM_STATE_ABOVE;
}
if(flags & EWMH_STATE_BELOW) {
prop[i++] = XA__NET_WM_STATE_BELOW;
}
XChangeProperty(dpy, twm_win->w, XA__NET_WM_STATE, XA_ATOM, 32,
PropModeReplace, (unsigned char *)prop, i);
}
/*
* Get the initial state of _NET_WM_STATE.
*
* Only some of the flags are supported when initially creating a window.
*/
static int EwmhGet_NET_WM_STATE(TwmWindow *twm_win)
{
int flags = 0;
unsigned long *prop;
unsigned long nitems;
int i;
prop = EwmhGetWindowProperties(twm_win->w, XA__NET_WM_STATE, XA_ATOM, &nitems);
if(prop) {
for(i = 0; i < nitems; i++) {
flags |= atomToFlag(prop[i]);
}
XFree(prop);
}
return flags;
}
/*
* Set _NET_WORKAREA.
*/
static void EwmhSet_NET_WORKAREA(ScreenInfo *scr)
{
unsigned long prop[4];
/* x */ prop[0] = scr->BorderLeft;
/* y */ prop[1] = scr->BorderTop;
/* w */ prop[2] = scr->rootw - scr->BorderLeft - scr->BorderRight;
/* h */ prop[3] = scr->rooth - scr->BorderTop - scr->BorderBottom;
XChangeProperty(dpy, Scr->XineramaRoot, XA__NET_WORKAREA, XA_CARDINAL, 32,
PropModeReplace, (unsigned char *)prop, 4);
}
|