~ctwm/ctwm/trunk

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
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
/*
 * Copyright 1992 Claude Lecommandeur.
 */

#include "ctwm.h"

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

#include <X11/Xatom.h>

#include "ctwm_atoms.h"
#include "util.h"
#include "animate.h"
#include "screen.h"
#include "add_window.h"
#include "events.h"
#include "otp.h"
#include "cursor.h"
#include "image.h"
#include "drawing.h"
#include "list.h"
#include "occupation.h"
#include "vscreen.h"
#include "win_decorations.h"
#include "win_iconify.h"
#include "win_ops.h"
#include "win_utils.h"
#include "workspace_manager.h"
#include "workspace_utils.h"
#include "xparsegeometry.h"


#include "gram.tab.h"


// Temp; x-ref desc in workspace_utils
extern bool useBackgroundInfo;


static void CreateWorkSpaceManagerWindow(VirtualScreen *vs);
static void ResizeWorkSpaceManager(VirtualScreen *vs, TwmWindow *win);
static void PaintWorkSpaceManagerBorder(VirtualScreen *vs);

static void wmap_mapwin_backend(TwmWindow *win, bool handleraise);

static void WMapRedrawWindow(Window window, int width, int height,
                             ColorPair cp, const char *label);

static void InvertColorPair(ColorPair *cp);


static XContext MapWListContext = None;
static Cursor handCursor = None;



/*
 ****************************************************************
 *
 * First, functions related to general creation and drawing of the WSM
 * window and its backing structs
 *
 ****************************************************************
 */

/*
 * Allocate an X Context for WSM stuff.
 */
void
InitWorkSpaceManagerContext(void)
{
	if(MapWListContext == None) {
		MapWListContext = XUniqueContext();
	}
}


/**
 * Prep up structures for WSM windows in each VS.  Called (for each
 * Screen) in startup after InitVirtualScreens() has setup the VS stuff
 * (and after config file processing).  This also retrieves info for each
 * vs about which workspace is active, if available (from restarting
 * ourself, etc).
 *
 * XXX Passed param isn't quite complete, as we call some funcs that use
 * global Scr...
 */
void
ConfigureWorkSpaceManager(ScreenInfo *scr)
{
	WorkSpace *ws = Scr->workSpaceMgr.workSpaceList;
	char *vsmapbuf, *vsmap;

	// Get the workspace name that's up on this vscreen.  This is
	// where the startup process actually sets what workspace we're
	// [re]starting in.
	vsmapbuf = CtwmGetVScreenMap(dpy, Scr->Root);
	if(vsmapbuf != NULL) {
		// Property is a comma-separate list of the workspaces for
		// each vscreen, in magic order.  So we start by chopping off
		// the first and then re-chop in the loop below.
		vsmap = strtok(vsmapbuf, ",");
	}
	else {
		vsmap = NULL;
	}


	// Setup each vs
	for(VirtualScreen *vs = scr->vScreenList; vs != NULL; vs = vs->next) {
		WorkSpace *fws;

		/*
		 * Make sure this is all properly initialized to nothing.  Otherwise
		 * bad and undefined behavior can show up in certain cases (e.g.,
		 * with no Workspaces {} defined in .ctwmrc, the only defined
		 * workspace will be random memory bytes, which can causes crashes on
		 * e.g.  f.menu "TwmWindows".)
		 */
		WorkSpaceWindow *wsw = calloc(1, sizeof(WorkSpaceWindow));
		wsw->state = scr->workSpaceMgr.initialstate;

		// If we have a current ws for this vs, assign it in, and
		// loop onward to the ws for the next vs.  For any we don't
		// have a default for, the default ws is the first one we haven't
		// used yet.
		if((fws = GetWorkspace(vsmap)) != NULL) {
			wsw->currentwspc = fws;
			vsmap = strtok(NULL, ",");
		}
		else {
			wsw->currentwspc = ws;
			if(ws) {
				ws = ws->next;
			}
		}

		vs->wsw = wsw;
	}

	free(vsmapbuf);
}




/*
 * Create workspace manager windows for each vscreen.  Called (for each
 * screen) late in startup, after the preceeding funcs have run their
 * course.
 */
void
CreateWorkSpaceManager(void)
{
	if(! Scr->workSpaceManagerActive) {
		return;
	}

	/* Setup basic fonts/colors/cursors */
	Scr->workSpaceMgr.windowFont.basename =
	        "-adobe-courier-medium-r-normal--10-100-75-75-m-60-iso8859-1";
	Scr->workSpaceMgr.buttonFont = Scr->IconManagerFont;
	Scr->workSpaceMgr.cp         = Scr->IconManagerC;
	if(!Scr->BeNiceToColormap) {
		GetShadeColors(&Scr->workSpaceMgr.cp);
	}
	if(handCursor == None) {
		NewFontCursor(&handCursor, "top_left_arrow");
	}


	/*
	 * Create a WSM window for each vscreen.  We don't need one for each
	 * workspace (we just reuse the same one), but we do need one for
	 * each vscreen (since they have to be displayed simultaneously).
	 */
	for(VirtualScreen *vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		CreateWorkSpaceManagerWindow(vs);
	}


	/*
	 * Init background in the WSM workspace subwindow and potentially the
	 * root window to the settings for the active workspace
	 *
	 * XXX CTAG_BGDRAW This process is also done in similar fashion
	 * during CreateWorkSpaceManagerWindow(), and the two parts are done
	 * split well apart during GotoWorkSpace().  The details of the
	 * process should be factored out into helper functions instead of
	 * being reimplemented in each place.  That will require a little
	 * shuffling of code, and careful thinking on the apparent
	 * differences (which seem like they may be cosmetic).  Todo.
	 */
	for(VirtualScreen *vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		WorkSpaceWindow *wsw = vs->wsw;    // Our WSW
		WorkSpace *ws2 = wsw->currentwspc; // Active WS
		MapSubwindow *msw = wsw->mswl[ws2->number]; // Active WS's subwin

		/* Setup the background/border on the active workspace */
		if(Scr->workSpaceMgr.curImage == NULL) {
			if(Scr->workSpaceMgr.curPaint) {
				XSetWindowBackground(dpy, msw->w, Scr->workSpaceMgr.curColors.back);
			}
		}
		else {
			XSetWindowBackgroundPixmap(dpy, msw->w, Scr->workSpaceMgr.curImage->pixmap);
		}
		XSetWindowBorder(dpy, msw->w, Scr->workSpaceMgr.curBorderColor);
		XClearWindow(dpy, msw->w);

		/* Set the root window to the color/image of that WS if we should */
		if(useBackgroundInfo && ! Scr->DontPaintRootWindow) {
			if(ws2->image == NULL) {
				XSetWindowBackground(dpy, vs->window, ws2->backcp.back);
			}
			else {
				XSetWindowBackgroundPixmap(dpy, vs->window, ws2->image->pixmap);
			}
			XClearWindow(dpy, vs->window);
		}
	}


	/*
	 * Set the property we use to store the full list of workspaces.
	 *
	 * XXX This isn't really part of creating the WSM windows, so doesn't
	 * strictly belong here.  It does need to happen after the config
	 * file parsing setup the workspaces, so couldn't go into
	 * InitWorkSpaceManager().  It could probably move into
	 * ConfigureWorkSpaceManager() though, or could move into a separate
	 * hypotehtical ConfigureWorkSpaces() sort of thing...
	 */
	{
		char *wrkSpcList;
		int  len;

		len = GetPropertyFromMask(0xFFFFFFFFu, &wrkSpcList);
		XChangeProperty(dpy, Scr->Root, XA_WM_WORKSPACESLIST, XA_STRING, 8,
		                PropModeReplace, (unsigned char *) wrkSpcList, len);
		free(wrkSpcList);
	}
}


/*
 * Put together the actual window for the workspace manager.  Called as
 * part of CreateWorkSpaceManager() during startup, once per vscreen
 * (since there's a separate window for each).
 */
static void
CreateWorkSpaceManagerWindow(VirtualScreen *vs)
{
	unsigned int width, height;
	TwmWindow *tmp_win;
	int x, y, gravity;
	/* Shortcuts */
	const int vspace = Scr->workSpaceMgr.vspace;
	const int hspace = Scr->workSpaceMgr.hspace;
	const long count = Scr->workSpaceMgr.count;

	/* No workspaces?  Nothing to do. */
	if(count == 0) {
		return;
	}

	/*
	 * Work out grid.  wSM.columns will be filled if specified in
	 * WorkSpaceManageGeometry, or uninitialized (0) if not.
	 */
	{
		int lines, columns;
		columns = Scr->workSpaceMgr.columns;
		if(columns == 0) {
			lines = 2;
			columns = ((count - 1) / lines) + 1;
		}
		else {
			lines = ((count - 1) / columns) + 1;
		}
		Scr->workSpaceMgr.lines   = lines;
		Scr->workSpaceMgr.columns = columns;
	}


	/* Work out dimensions of stuff */
	{
		unsigned int bwidth, bheight;
		unsigned short strWid;
		WorkSpace *ws;
		const char *geometry = Scr->workSpaceMgr.geometry;
		const int lines      = Scr->workSpaceMgr.lines;
		const int columns    = Scr->workSpaceMgr.columns;

		/* Figure longest label */
		strWid = 0;
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			XRectangle inc_rect;
			XRectangle logical_rect;
			unsigned short wid;
			const MyFont font = Scr->workSpaceMgr.buttonFont;

			XmbTextExtents(font.font_set, ws->label, strlen(ws->label),
			               &inc_rect, &logical_rect);
			wid = logical_rect.width;
			if(wid > strWid) {
				strWid = wid;
			}
		}

		/*
		 * If WorkSpaceManagerGeometry is given, work from that.  Else,
		 * create a workable minimum ourselves.
		 * */
		if(geometry != NULL) {
			int mask;

			/* Base button/subwindow sizes */
			bwidth = strWid + 10;
			bheight = 22;

			/* Adjust to WSMGeometry if specified */
			mask = RLayoutXParseGeometry(Scr->Layout, geometry, &x, &y, &width, &height);
			if(mask & WidthValue) {
				bwidth = (width - (columns * hspace)) / columns;
			}
			if(mask & HeightValue) {
				bheight = (height - (lines * vspace)) / lines;
			}

			/* Size of the whole thing is based off those */
			width  = columns * (bwidth  + hspace);
			height = lines   * (bheight + vspace);

			/*
			 * If no Y given, put it at the bottom of the screen.  If one
			 * is, just accept it.  If it's a negative, we have to figure
			 * out where that actually is on this vscreen.
			 */
			if(!(mask & YValue)) {
				y = 0;
				mask |= YNegative;
			}
			if(mask & YNegative) {
				y += vs->h - height;
			}

			/*
			 * If X is given, tweak as necessary for the vscreen
			 * location.  Otherwise, put it in in something like the
			 * middle.
			 */
			if(mask & XValue) {
				if(mask & XNegative) {
					x += vs->w - width;
					gravity = (mask & YNegative) ? SouthEastGravity : NorthEastGravity;
				}
				else {
					gravity = (mask & YNegative) ? SouthWestGravity : NorthWestGravity;
				}
			}
			else {
				x = (vs->w - width) / 2;
				gravity = (mask & YValue) ? ((mask & YNegative) ?
				                             SouthGravity : NorthGravity) : SouthGravity;
			}
		}
		else {
			/* No geom specified, come up with one */
			bwidth  = strWid + 2 * Scr->WMgrButtonShadowDepth + 6;
			bheight = 22;
			width   = columns * (bwidth  + hspace);
			height  = lines   * (bheight + vspace);
			x       = (vs->w - width) / 2;
			y       = vs->h - height;
			gravity = NorthWestGravity;
		}
	}

	/* Set w/h to dummy values; ResizeWorkSpaceManager() writes real ones */
	vs->wsw->width  = 1;
	vs->wsw->height = 1;

	/* Allocate structs for map/button subwindows */
	vs->wsw->bswl = calloc(count, sizeof(ButtonSubwindow *));
	vs->wsw->mswl = calloc(count, sizeof(MapSubwindow *));

	/* Create main window */
	vs->wsw->w = XCreateSimpleWindow(dpy, Scr->Root, x, y, width, height, 0,
	                                 Scr->Black, Scr->workSpaceMgr.cp.back);


	/*
	 * Create the map and button subwindows for each workspace
	 */
	{
		WorkSpace *ws;

		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			MapSubwindow *msw;
			ButtonSubwindow *bsw;
			const int Dummy = 1;
			const unsigned long border = Scr->workSpaceMgr.defBorderColor;

			/* Alloc structs */
			vs->wsw->bswl[ws->number] = bsw = calloc(1, sizeof(ButtonSubwindow));
			vs->wsw->mswl[ws->number] = msw = calloc(1, sizeof(MapSubwindow));

			/*
			 * Create windows for button/map.  ResizeWorkSpaceManager()
			 * sets the real sizes and positions, so we dummy 'em.
			 */
			bsw->w = XCreateSimpleWindow(dpy, vs->wsw->w,
			                             Dummy, Dummy, Dummy, Dummy,
			                             0, Scr->Black, ws->cp.back);

			msw->w = XCreateSimpleWindow(dpy, vs->wsw->w,
			                             Dummy, Dummy, Dummy, Dummy,
			                             1, border, ws->cp.back);

			/* Map whichever is up by default */
			if(vs->wsw->state == WMS_buttons) {
				XMapWindow(dpy, bsw->w);
			}
			else {
				XMapWindow(dpy, msw->w);
			}

			/* Setup background on map-state window */
			/* XXX X-ref CTAG_BGDRAW in CreateWorkSpaceManager() */
			if(useBackgroundInfo) {
				if(ws->image == NULL || Scr->NoImagesInWorkSpaceManager) {
					XSetWindowBackground(dpy, msw->w, ws->backcp.back);
				}
				else {
					XSetWindowBackgroundPixmap(dpy, msw->w, ws->image->pixmap);
				}
			}
			else {
				if(Scr->workSpaceMgr.defImage == NULL || Scr->NoImagesInWorkSpaceManager) {
					XSetWindowBackground(dpy, msw->w, Scr->workSpaceMgr.defColors.back);
				}
				else {
					XSetWindowBackgroundPixmap(dpy, msw->w, Scr->workSpaceMgr.defImage->pixmap);
				}
			}

			/*
			 * Clear out button subwin; PaintWorkSpaceManager() fills it
			 * in.  Is this really necessary?
			 */
			XClearWindow(dpy, bsw->w);
		}
	}


	/* Set WM properties */
	{
		XSizeHints sizehints;
		XWMHints   wmhints;
		const int lines   = Scr->workSpaceMgr.lines;
		const int columns = Scr->workSpaceMgr.columns;
		const char *name      = Scr->workSpaceMgr.name;
		const char *icon_name = Scr->workSpaceMgr.icon_name;

		sizehints.flags       = USPosition | PBaseSize | PMinSize | PResizeInc
		                        | PWinGravity;
		sizehints.x           = x;
		sizehints.y           = y;
		sizehints.base_width  = columns * hspace;
		sizehints.base_height = lines   * vspace;
		sizehints.width_inc   = columns;
		sizehints.height_inc  = lines;
		sizehints.min_width   = columns  * (hspace + 2);
		sizehints.min_height  = lines    * (vspace + 2);
		sizehints.win_gravity = gravity;

		wmhints.flags         = InputHint | StateHint;
		wmhints.input         = True;
		wmhints.initial_state = NormalState;

		XmbSetWMProperties(dpy, vs->wsw->w, name, icon_name, NULL, 0,
		                   &sizehints, &wmhints, NULL);
	}


	/* Create our TwmWindow wrapping around it */
	tmp_win = AddWindow(vs->wsw->w, AWT_WORKSPACE_MANAGER,
	                    Scr->iconmgr, vs);
	if(! tmp_win) {
		fprintf(stderr, "cannot create workspace manager window, exiting...\n");
		exit(1);
	}
	tmp_win->occupation = fullOccupation;
	tmp_win->attr.width = width;
	tmp_win->attr.height = height;
	vs->wsw->twm_win = tmp_win;


	/* Do the figuring to size and internal-layout it */
	ResizeWorkSpaceManager(vs, tmp_win);


	/* Setup cursor/gravity and listen for events */
	{
		XWindowAttributes wattr;
		XSetWindowAttributes attr;
		unsigned long attrmask;

		attr.cursor = Scr->ButtonCursor;
		attr.win_gravity = gravity;
		attrmask = CWCursor | CWWinGravity;
		XChangeWindowAttributes(dpy, vs->wsw->w, attrmask, &attr);

		XGetWindowAttributes(dpy, vs->wsw->w, &wattr);
		attrmask = wattr.your_event_mask | KeyPressMask | KeyReleaseMask
		           | ExposureMask;
		XSelectInput(dpy, vs->wsw->w, attrmask);
	}


	/*
	 * Mark the buttons as listening to click and exposure events, and
	 * stash away some pointers in contexts.  We stash the overall WSM
	 * window in TwmContext, which means that when an event looks up the
	 * window, it finds the WSM rather than the subwindow, and then falls
	 * into the WMgrHandle*Event()'s, which then dig down into the event
	 * to find where it happened in there.
	 *
	 * The map window doesn't listen to expose events; it's just empty
	 * and background colored.  The individual subwindows in the map
	 * listen for exposes for drawing themselves.
	 *
	 * Dragging windows around to move or re-occupy in the map window
	 * does rely on motion events, but we don't listen for them here.
	 * That happens in WMgrHandleButtonEvent() after getting the initial
	 * click.  It changes the listen and runs through the action
	 * internally; those motions never run through our main event loop.
	 */
	for(WorkSpace *ws = Scr->workSpaceMgr.workSpaceList; ws != NULL;
	                ws = ws->next) {
		Window buttonw = vs->wsw->bswl[ws->number]->w;
		Window mapsubw = vs->wsw->mswl[ws->number]->w;

		XSelectInput(dpy, buttonw, ButtonPressMask | ButtonReleaseMask
		             | ExposureMask);
		XSaveContext(dpy, buttonw, TwmContext, (XPointer) tmp_win);
		XSaveContext(dpy, buttonw, ScreenContext, (XPointer) Scr);

		XSelectInput(dpy, mapsubw, ButtonPressMask | ButtonReleaseMask);
		XSaveContext(dpy, mapsubw, TwmContext, (XPointer) tmp_win);
		XSaveContext(dpy, mapsubw, ScreenContext, (XPointer) Scr);
	}


	/* Set WM_STATE prop */
	SetMapStateProp(tmp_win, WithdrawnState);


	/* Setup root window if necessary */
	/* XXX X-ref CTAG_BGDRAW in CreateWorkSpaceManager() */
	if(useBackgroundInfo && ! Scr->DontPaintRootWindow) {
		WorkSpace *ws = Scr->workSpaceMgr.workSpaceList;
		if(ws->image == NULL) {
			XSetWindowBackground(dpy, Scr->Root, ws->backcp.back);
		}
		else {
			XSetWindowBackgroundPixmap(dpy, Scr->Root, ws->image->pixmap);
		}
		XClearWindow(dpy, Scr->Root);
	}


	/*
	 * Don't have to PaintWorkSpaceManager(vs) here, because
	 * ResizeWorkSpaceManager() already called it for us.
	 */
}


/*
 * Size and layout a WSM.  Mostly an internal bit in the process of
 * setting it up.
 */
static void
ResizeWorkSpaceManager(VirtualScreen *vs, TwmWindow *win)
{
	WorkSpace *ws;
	int       i, j;
	/* Lots of shortcuts to ease reading */
	const int neww    = win->attr.width;
	const int newh    = win->attr.height;
	const int hspace  = Scr->workSpaceMgr.hspace;
	const int vspace  = Scr->workSpaceMgr.vspace;
	const int lines   = Scr->workSpaceMgr.lines;
	const int columns = Scr->workSpaceMgr.columns;
	const int bwidth  = (neww - (columns * hspace)) / columns;
	const int bheight = (newh - (lines   * vspace)) / lines;
	const int wwidth  = neww / columns;
	const int wheight = newh / lines;
	const float wf    = (float)(wwidth  - 2) / (float) vs->w;
	const float hf    = (float)(wheight - 2) / (float) vs->h;

	/* If nothing's changed since our last run, there's nothing to change */
	if(neww == vs->wsw->width && newh == vs->wsw->height) {
		return;
	}

	/* Set the new overall vals */
	vs->wsw->bwidth  = bwidth;
	vs->wsw->bheight = bheight;
	vs->wsw->width   = neww;
	vs->wsw->height  = newh;
	vs->wsw->wwidth  = wwidth;
	vs->wsw->wheight = wheight;

	/* Iterate over the WS's */
	i = 0;
	j = 0;
	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		MapSubwindow    *msw = vs->wsw->mswl[ws->number];
		ButtonSubwindow *bsw = vs->wsw->bswl[ws->number];

		/* Move button window to its place in the grid and size appropriately */
		XMoveResizeWindow(dpy, bsw->w,
		                  i * (bwidth  + hspace) + (hspace / 2),
		                  j * (bheight + vspace) + (vspace / 2),
		                  bwidth, bheight);

		/* Move the map window as well */
		msw->x = i * wwidth;
		msw->y = j * wheight;
		XMoveResizeWindow(dpy, msw->w, msw->x, msw->y, wwidth - 2, wheight - 2);

		/*
		 * Redo interior sizing and placement of all the windows in the
		 * WS in the map window
		 */
		for(WinList *wl = msw->wl; wl != NULL; wl = wl->next) {
			TwmWindow *tmp_win = wl->twm_win;
			wl->x      = (int)(tmp_win->frame_x * wf);
			wl->y      = (int)(tmp_win->frame_y * hf);
			wl->width  = (unsigned int)((tmp_win->frame_width  * wf) + 0.5);
			wl->height = (unsigned int)((tmp_win->frame_height * hf) + 0.5);
			XMoveResizeWindow(dpy, wl->w, wl->x, wl->y, wl->width, wl->height);
		}

		/* And around to the next WS */
		i++;
		if(i == columns) {
			i = 0;
			j++;
		};
	}


	/* Draw it */
	PaintWorkSpaceManager(vs);
}


/*
 * Draw up the button-state pieces of a WSM window.
 *
 * Note: this is currently stubbed out and does nothing.  Historically
 * it's been called during startup when the WSM window is put together,
 * and when the screen is unmasked.  However, the only apparent result is
 * that the border and buttons get drawn a little earlier; they already
 * get expose events that get picked up when we start the event loop.
 *
 * If we don't find any reason to reinstate it, we should remove this in
 * the future.
 */
void
PaintWorkSpaceManager(VirtualScreen *vs)
{
	WorkSpace *ws;

	/* x-ref header comment */
	return;

	PaintWorkSpaceManagerBorder(vs);

	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		Window buttonw = vs->wsw->bswl[ws->number]->w;
		ButtonState bs = (ws == vs->wsw->currentwspc) ? on : off;

		PaintWsButton(WSPCWINDOW, vs, buttonw, ws->label, ws->cp, bs);
	}
}


/*
 * Border around the WSM
 */
static void
PaintWorkSpaceManagerBorder(VirtualScreen *vs)
{
	int width, height;

	width  = vs->wsw->width;
	height = vs->wsw->height;
	Draw3DBorder(vs->wsw->w, 0, 0, width, height, 2, Scr->workSpaceMgr.cp, off,
	             true, false);
}


/*
 * Draw a workspace manager window on expose.  X-ref comment on
 * PaintWorkSpaceManager().
 */
void
WMgrHandleExposeEvent(VirtualScreen *vs, XEvent *event)
{
	if(vs->wsw->state == WMS_buttons) {
		Window buttonw;
		WorkSpace *ws;

		/* Find the button we're exposing */
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			buttonw = vs->wsw->bswl[ws->number]->w;
			if(event->xexpose.window == buttonw) {
				break;
			}
		}

		/* If none, just paint the border.  Else paint the button. */
		if(ws == NULL) {
			PaintWorkSpaceManagerBorder(vs);
		}
		else {
			ButtonState bs = (ws == vs->wsw->currentwspc) ? on : off;
			PaintWsButton(WSPCWINDOW, vs, buttonw, ws->label, ws->cp, bs);
		}
	}
	else {
		WinList *wl;

		/*
		 * This is presumably exposing some individual window in the WS
		 * subwindow; find it from the stashed context on the window, and
		 * redraw it.
		 */
		if(XFindContext(dpy, event->xexpose.window, MapWListContext,
		                (XPointer *) &wl) == XCNOENT) {
			return;
		}
		if(wl && wl->twm_win && wl->twm_win->mapped) {
			WMapRedrawName(vs, wl);
		}
	}
}



/*
 * Moving the WSM between button and map state
 */
void
WMgrToggleState(VirtualScreen *vs)
{
	if(vs->wsw->state == WMS_buttons) {
		WMgrSetMapState(vs);
	}
	else {
		WMgrSetButtonsState(vs);
	}
}

void
WMgrSetMapState(VirtualScreen *vs)
{
	WorkSpace *ws;

	if(vs->wsw->state == WMS_map) {
		return;
	}
	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		XUnmapWindow(dpy, vs->wsw->bswl [ws->number]->w);
		XMapWindow(dpy, vs->wsw->mswl [ws->number]->w);
	}
	vs->wsw->state = WMS_map;
	MaybeAnimate = true;
}

void
WMgrSetButtonsState(VirtualScreen *vs)
{
	WorkSpace *ws;

	if(vs->wsw->state == WMS_buttons) {
		return;
	}
	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		XUnmapWindow(dpy, vs->wsw->mswl [ws->number]->w);
		XMapWindow(dpy, vs->wsw->bswl [ws->number]->w);
	}
	vs->wsw->state = WMS_buttons;
}




/*
 ****************************************************************
 *
 * Handlers for mouse/key actions in the WSM
 *
 ****************************************************************
 */

/*
 * Key press/release events in the WSM.  A major use (and only for
 * release) is the Ctrl-key switching between map and button state.  The
 * other use is on-the-fly renaming of workspaces by typing in the
 * button-state WSM.
 */
void
WMgrHandleKeyReleaseEvent(VirtualScreen *vs, XEvent *event)
{
	KeySym keysym;

	keysym = XLookupKeysym((XKeyEvent *) event, 0);
	if(! keysym) {
		return;
	}
	if(keysym == XK_Control_L || keysym == XK_Control_R) {
		/* DontToggleWorkSpaceManagerState added 20040607 by dl*/
		if(!Scr->DontToggleWorkspaceManagerState) {
			WMgrToggleState(vs);
		}
		return;
	}
}

void
WMgrHandleKeyPressEvent(VirtualScreen *vs, XEvent *event)
{
	WorkSpace *ws;

	/* Check if we're using Control to toggle the state */
	{
		KeySym keysym = XLookupKeysym((XKeyEvent *) event, 0);
		if(! keysym) {
			return;
		}
		if(keysym == XK_Control_L || keysym == XK_Control_R) {
			/* DontToggleWorkSpaceManagerState added 20040607 by dl*/
			if(!Scr->DontToggleWorkspaceManagerState) {
				WMgrToggleState(vs);
			}
			return;
		}
	}

	/* Otherwise, key presses do nothing in map state */
	if(vs->wsw->state == WMS_map) {
		return;
	}

	/*
	 * If we're typing in a button-state WSM, and the mouse is on one of
	 * the buttons, that means we're changing the name, so do that dance.
	 */
	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		if(vs->wsw->bswl[ws->number]->w == event->xkey.subwindow) {
			break;
		}
	}
	if(ws == NULL) {
		/* Not on a button, nothing to do */
		return;
	}


	/*
	 * Edit the label.
	 */
	{
		int    nkeys;
		char   keys[16];
		size_t nlen;
		char   *newname;

		/* Look up what keystrokes are queued.  Arbitrary buf size */
		nkeys = XLookupString(&(event->xkey), keys, 16, NULL, NULL);

		/* Label length can't grow to more than cur+nkeys */
		nlen = strlen(ws->label);
		newname = malloc(nlen + nkeys + 1);
		strcpy(newname, ws->label);

		/* Iterate over the passed keystrokes */
		for(int i = 0 ; i < nkeys ; i++) {
			unsigned char k = keys[i];

			if(isprint(k)) {
				/* Printable chars append to the string */
				newname[nlen++] = k;
			}
			else if((k == 127) || (k == 8)) {
				/*
				 * DEL or BS back up a char.
				 *
				 * XXX Would it be more generally correct to do this via
				 * keysyms, in the face of changed keyboard mappings or
				 * significantly differing locales?
				 */
				if(nlen != 0) {
					nlen--;
				}
			}
			else {
				/* Any other char stops the process dead */
				break;
			}
		}
		/* Now ends where it ends */
		newname[nlen] = '\0';

		/* Swap it in */
		free(ws->label);
		ws->label = newname;
	}


	/* Redraw the button with the new label */
	{
		ButtonState bs = (ws == vs->wsw->currentwspc) ? on : off;

		PaintWsButton(WSPCWINDOW, vs, vs->wsw->bswl[ws->number]->w, ws->label,
		              ws->cp, bs);
	}
}


/*
 * Mouse clicking in WSM.  Gets called on button press (not release).  In
 * the simple case, that's just switching workspaces.  In the more
 * complex, it's changing window occupation in various different ways, or
 * even moving windows.  When that's happening, it internally hijacks
 * button/motion/exposure events and implements them for the moving, with
 * magic escapes if it gets them for something else.  Ew.
 */
void
WMgrHandleButtonEvent(VirtualScreen *vs, XEvent *event)
{
	WorkSpace    *oldws, *newws;
	WinList      *wl;
	TwmWindow    *win;
	unsigned int W0, H0;
	XEvent       lastev;
	Window       w = 0;
	Position     newX = 0, newY = 0, winX = 0, winY = 0;
	bool         alreadyvivible, realmovemode;
	const WorkSpaceWindow *mw = vs->wsw;

	/* Shortcuts into the event */
	const Window parent = event->xbutton.window;    // Map/button for WS
	const Window sw     = event->xbutton.subwindow; // Map mini-win
	const Time etime    = event->xbutton.time;
	const unsigned int button   = event->xbutton.button;
	const unsigned int modifier = event->xbutton.state;


	/* If we're in button state, we're just clicking to change */
	if(vs->wsw->state == WMS_buttons) {
		WorkSpace *ws;
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			if(vs->wsw->bswl[ws->number]->w == parent) {
				GotoWorkSpace(vs, ws);
				break;
			}
		}
		return;
	}

	/*
	 * If we get this far, we're in map state, where things are more
	 * complicated.  A simple click/release means change here too, but
	 * there's also the possibility of dragging a subwindow around to
	 * change its window's occupation.
	 */

	/* Find what workspace we're clicking in */
	for(oldws = Scr->workSpaceMgr.workSpaceList ; oldws != NULL ;
	                oldws = oldws->next) {
		if(vs->wsw->mswl[oldws->number]->w == parent) {
			break;
		}
	}
	if(oldws == NULL) {
		/* None?  We're done here. */
		return;
	}

	/*
	 * If clicked in the workspace but outside a window, we can only be
	 * switching workspaces.  So just do that, and we're done.
	 */
	if(sw == (Window) 0) {
		GotoWorkSpace(vs, oldws);
		return;
	}

	/* Use the context to find the winlist entry for this window */
	if(XFindContext(dpy, sw, MapWListContext, (XPointer *) &wl) == XCNOENT) {
		return;
	}
	win = wl->twm_win;

	/*
	 * Sometimes we skip transients, so do so.  XXX Should this
	 * GotoWorkSpace()?
	 */
	if((! Scr->TransientHasOccupation) && win->istransient) {
		return;
	}

	/*
	 * Are we trying to actually move the window, by moving its avatar in
	 * the WSM?  If ReallyMoveInWorkspaceManager is set, we're moving on
	 * click, but not in shift-click.  If it's not, it's the reverse.
	 *
	 * XXX This interacts really oddly and badly when you're also moving
	 * the window from WS to WS.
	 */
	realmovemode = false;
	if(Scr->ReallyMoveInWorkspaceManager) {
		if(!(modifier & ShiftMask)) {
			realmovemode = true;
		}
	}
	else if(modifier & ShiftMask) {
		realmovemode = true;
	}

	/*
	 * Frob screen-wide OpaqueMove as necessary for this window's
	 * details.
	 * XXX Really?
	 */
	if(win->OpaqueMove) {
		if(Scr->OpaqueMoveThreshold >= 200) {
			Scr->OpaqueMove = true;
		}
		else {
			const unsigned long winsz = win->frame_width * win->frame_height;
			const unsigned long scrsz = vs->w * vs->h;
			const float sf = Scr->OpaqueMoveThreshold / 100.0;
			if(winsz > (scrsz * sf)) {
				Scr->OpaqueMove = false;
			}
			else {
				Scr->OpaqueMove = true;
			}
		}
	}
	else {
		Scr->OpaqueMove = false;
	}

	/*
	 * Buttons inside the workspace manager, when clicking on the
	 * representation of a window:
	 * 1: drag window to a different workspace
	 * 2: drag a copy of the window to a different workspace
	 *    (show it in both workspaces)
	 * 3: remove the window from this workspace (if it isn't the last).
	 *
	 * XXX If you move between workspaces while also doing realmovemode,
	 * really messed up things happen.
	 */
	switch(button) {
		case 1 : {
			/*
			 * Moving from one to another; get rid of the old location,
			 * then fall through to the "duplicating" case below.
			 */
			XUnmapWindow(dpy, sw);
			/* FALLTHRU */
		}

		case 2 : {
			/*
			 * Duplicating window to another WS.  We create a copy of the
			 * window, and start moving that.  The 'moving' case above
			 * falls through to us after unmapping that old window,
			 * leaving just our copy, which is good enough.  This is
			 * really just setting up the visual stuff for the move; the
			 * actual occupation changes etc. come at the end of the
			 * motion, much lower down.
			 */
			int X0, Y0, X1, Y1;
			unsigned int bw;
			XSetWindowAttributes attrs;
			Window junkW;

			/* [XYWH]0 = size/location of the avatar in the map */
			XGetGeometry(dpy, sw, &junkW, &X0, &Y0, &W0, &H0, &bw, &JunkDepth);

			/*
			 * [XY]0 are the coordinates of the avatar subwindow inside
			 * the individual workspace window in the map.  Turn those
			 * into [XY]1 as the coordinates of it relative to the whole
			 * WSM window.
			 */
			XTranslateCoordinates(dpy, vs->wsw->mswl[oldws->number]->w,
			                      mw->w, X0, Y0, &X1, &Y1, &junkW);

			/*
			 * Create the copy window to drag around, as a child of the
			 * whole WSM (so we can move it between workspaces), and map
			 * it.
			 */
			attrs.event_mask       = ExposureMask;
			attrs.background_pixel = wl->cp.back;
			attrs.border_pixel     = wl->cp.back;
			w = XCreateWindow(dpy, mw->w, X1, Y1, W0, H0, bw,
			                  CopyFromParent, CopyFromParent, CopyFromParent,
			                  CWEventMask | CWBackPixel | CWBorderPixel,
			                  &attrs);
			XMapRaised(dpy, w);

			/* Do our dance on it to draw the name/color/etc */
			WMapRedrawWindow(w, W0, H0, wl->cp, wl->twm_win->icon_name);

			/*
			 * If we're moving the real window and
			 * AlwaysShowWindowWhenMovingFromWorkspaceManager is set in
			 * config, we need to be sure the real window is visible
			 * while we move it.
			 */
			if(realmovemode && Scr->ShowWinWhenMovingInWmgr) {
				if(Scr->OpaqueMove) {
					DisplayWin(vs, win);
				}
				else {
					MoveOutline(Scr->Root,
					            win->frame_x - win->frame_bw,
					            win->frame_y - win->frame_bw,
					            win->frame_width  + 2 * win->frame_bw,
					            win->frame_height + 2 * win->frame_bw,
					            win->frame_bw,
					            win->title_height + win->frame_bw3D);
				}
			}

			/* Move onward */
			break;
		}

		/*
		 * For the button 3 or anything else case, there's no dragging or
		 * anything, so they do their thing and just immediately return.
		 */
		case 3 : {
			int newocc = win->occupation & ~(1 << oldws->number);
			if(newocc != 0) {
				ChangeOccupation(win, newocc);
			}
			return;
		}

		default :
			return;
	}

	/*
	 * Keep dragging the representation of the window
	 *
	 * XXX Look back at this and see if we can move it to an inner
	 * function for readability...
	 */
	{
		const float wf = (float)(mw->wwidth  - 1) / (float) vs->w;
		const float hf = (float)(mw->wheight - 1) / (float) vs->h;
		int XW, YW;
		bool cont;
		Window junkW;

		/* Figure where in the subwindow the click was, and stash in XW/YW */
		XTranslateCoordinates(dpy, Scr->Root, sw, event->xbutton.x_root,
		                      event->xbutton.y_root,
		                      &XW, &YW, &junkW);

		/*
		 * Grab the pointer, lock it into the WSM, and get the events
		 * related to buttons and movement.  We don't need
		 * PointerMotionMask, since that would only happen after buttons
		 * are released, and we'll be done by then.
		 */
		XGrabPointer(dpy, mw->w, False,
		             ButtonPressMask | ButtonMotionMask | ButtonReleaseMask,
		             GrabModeAsync, GrabModeAsync, mw->w, Scr->MoveCursor,
		             CurrentTime);

		/* Start handling events until we're done */
		alreadyvivible = false;
		cont = true;
		while(cont) {
			XEvent ev;

			/* Grab the next event and handle */
			XMaskEvent(dpy, ButtonPressMask | ButtonMotionMask |
			           ButtonReleaseMask | ExposureMask, &ev);
			switch(ev.xany.type) {
				case Expose : {
					/* Something got exposed */
					if(ev.xexpose.window == w) {
						/*
						 * The win we're working with?  We know how to do
						 * that.
						 */
						WMapRedrawWindow(w, W0, H0, wl->cp,
						                 wl->twm_win->icon_name);
						break;
					}

					/* Else, delegate to our global dispatcher */
					Event = ev;
					DispatchEvent();
					break;
				}

				case ButtonPress :
				case ButtonRelease : {
					/*
					 * Events for buttons other than the one whose press
					 * started this activity are totally ignored.
					 */
					if(ev.xbutton.button != button) {
						break;
					}

					/*
					 * Otherwise, this is a press/release of the button
					 * that started things.  Though I'm not sure how it
					 * could be a press, since it was already pressed.
					 * Regardless, this is our exit condition.  Fall
					 * through into the Motion case to handle any
					 * remaining movement.
					 */
					lastev = ev;
					cont = false;
					newX = ev.xbutton.x;
					newY = ev.xbutton.y;
				}

				/* Everything remaining is motion handling */
				case MotionNotify : {
					/* If we fell through from above, no new movement */
					if(cont) {
						newX = ev.xmotion.x;
						newY = ev.xmotion.y;
					}

					/* Lots to do if we're moving the window for real */
					if(realmovemode) {
						int XSW, YSW;
						WorkSpace *cws;
						MapSubwindow *msw;

						/* Did the move start in the currently visible WS? */
						bool startincurrent = (oldws == vs->wsw->currentwspc);

						/* Find the workspace we wound up in */
						for(cws = Scr->workSpaceMgr.workSpaceList ;
						                cws != NULL ;
						                cws = cws->next) {
							msw = vs->wsw->mswl [cws->number];
							if((newX >= msw->x)
							                && (newX <  msw->x + mw->wwidth)
							                && (newY >= msw->y)
							                && (newY <  msw->y + mw->wheight)) {
								break;
							}
						}
						if(!cws) {
							/* None?  Ignore. */
							break;
						}

						/*
						 * Mouse is wherever it started inside the
						 * subwindow, so figure the X/Y of the top left
						 * of the subwindow from there.  (coords relative
						 * to the whole WSM because of grab)
						 */
						winX = newX - XW;
						winY = newY - YW;

						/* XXX redundant w/previous? */
						msw = vs->wsw->mswl[cws->number];

						/*
						 * Figure where those coords are relative to the
						 * per-workspace window.
						 */
						XTranslateCoordinates(dpy, mw->w, msw->w,
						                      winX, winY, &XSW, &YSW, &junkW);

						/*
						 * Now rework the win[XY] to be the coordinates
						 * the window would be in the whole screen, based
						 * on the [XY]SW inside the map, using our scale
						 * factors.
						 */
						winX = (int)(XSW / wf);
						winY = (int)(YSW / hf);

						/*
						 * Clip to the screen if DontMoveOff is set.
						 *
						 * XXX Can we use the Constrain*() functions for
						 * this, instead of implementing icky magic
						 * internally?
						 */
						if(Scr->DontMoveOff) {
							const int width = win->frame_width;
							const int height = win->frame_height;

							if((winX < Scr->BorderLeft) && ((Scr->MoveOffResistance < 0) ||
							                                (winX > Scr->BorderLeft - Scr->MoveOffResistance))) {
								winX = Scr->BorderLeft;
								newX = msw->x + XW + Scr->BorderLeft * mw->wwidth / vs->w;
							}
							if(((winX + width) > vs->w - Scr->BorderRight) &&
							                ((Scr->MoveOffResistance < 0) ||
							                 ((winX + width) < vs->w - Scr->BorderRight + Scr->MoveOffResistance))) {
								winX = vs->w - Scr->BorderRight - width;
								newX = msw->x + mw->wwidth *
								       (1 - Scr->BorderRight / (double) vs->w) - wl->width + XW - 2;
							}
							if((winY < Scr->BorderTop) && ((Scr->MoveOffResistance < 0) ||
							                               (winY > Scr->BorderTop - Scr->MoveOffResistance))) {
								winY = Scr->BorderTop;
								newY = msw->y + YW + Scr->BorderTop * mw->height / vs->h;
							}
							if(((winY + height) > vs->h - Scr->BorderBottom) &&
							                ((Scr->MoveOffResistance < 0) ||
							                 ((winY + height) < vs->h - Scr->BorderBottom + Scr->MoveOffResistance))) {
								winY = vs->h - Scr->BorderBottom - height;
								newY = msw->y + mw->wheight
								       * (1 - Scr->BorderBottom / (double) vs->h)
								       * - wl->height + YW - 2;
							}
						}


						/* Setup the avatar for the new position of win */
						WMapSetupWindow(win, winX, winY, -1, -1);

						/*
						 * If SWWMIW, we already made sure above the
						 * window is always visible on the screen.  So we
						 * don't need to do any of the following steps to
						 * maybe show it or maybe un-show it, since we
						 * know it's already always shown.
						 */
						if(Scr->ShowWinWhenMovingInWmgr) {
							goto movewin;
						}

						/*
						 * If we wound up in the same workspace as is
						 * being displayed on the screen, we need to make
						 * sure that window is showing up on the screen
						 * as a "about to be occupied here if you release
						 * the button" indication.
						 */
						if(cws == vs->wsw->currentwspc) {
							if(alreadyvivible) {
								/* Unless it's already there */
								goto movewin;
							}
							if(Scr->OpaqueMove) {
								XMoveWindow(dpy, win->frame, winX, winY);
								DisplayWin(vs, win);
							}
							else {
								MoveOutline(Scr->Root,
								            winX - win->frame_bw,
								            winY - win->frame_bw,
								            win->frame_width  + 2 * win->frame_bw,
								            win->frame_height + 2 * win->frame_bw,
								            win->frame_bw,
								            win->title_height + win->frame_bw3D);
							}
							alreadyvivible = true;

							/*
							 * We already moved it, skip past the other
							 * code trying to move it in other cases.
							 */
							goto move;
						}

						/*
						 * If it's for whatever reason not being shown on
						 * the screen, then we don't need to move it; hop
						 * straight to moving the avatar.
						 */
						if(!alreadyvivible) {
							goto move;
						}

						/*
						 * So it _is_ being shown.  If it's not supposed
						 * to be here, hide it away (don't need to worry
						 * about AlwaysShow, it would have skipped past
						 * us from ab0ve).  Also, if we started moving
						 * the window out of the visible workspace with
						 * button 1, it's gonna not be here if you
						 * release, so hide it away.
						 */
						if(!OCCUPY(win, vs->wsw->currentwspc) ||
						                (startincurrent && (button == 1))) {
							if(Scr->OpaqueMove) {
								Vanish(vs, win);
								XMoveWindow(dpy, win->frame, winX, winY);
							}
							else {
								MoveOutline(Scr->Root, 0, 0, 0, 0, 0, 0);
							}
							alreadyvivible = false;
							goto move;
						}

movewin:
						/*
						 * However we get here, the real window is
						 * visible and needs to be moved.  So move it.
						 */
						if(Scr->OpaqueMove) {
							XMoveWindow(dpy, win->frame, winX, winY);
						}
						else {
							MoveOutline(Scr->Root,
							            winX - win->frame_bw,
							            winY - win->frame_bw,
							            win->frame_width  + 2 * win->frame_bw,
							            win->frame_height + 2 * win->frame_bw,
							            win->frame_bw,
							            win->title_height + win->frame_bw3D);
						}
					}

move:
					/*
					 * Just move the subwindow in the map to the new
					 * location.
					 */
					XMoveWindow(dpy, w, newX - XW, newY - YW);

					/* And we're done.  Next event! */
					break;
				}
			}
		}
	}

	/*
	 * Finished with dragging (button released).
	 */
	if(realmovemode) {
		/* Moving the real window?  Move the real window. */
		if(Scr->ShowWinWhenMovingInWmgr || alreadyvivible) {
			if(Scr->OpaqueMove && !OCCUPY(win, vs->wsw->currentwspc)) {
				Vanish(vs, win);
			}
			if(!Scr->OpaqueMove) {
				MoveOutline(Scr->Root, 0, 0, 0, 0, 0, 0);
				WMapRedrawName(vs, wl);
			}
		}
		SetupWindow(win, winX, winY, win->frame_width, win->frame_height, -1);
	}

	/*
	 * Last event that caused us to escape is other code's
	 * responsibility, put it back in the queue.
	 */
	lastev.xbutton.subwindow = (Window) 0;
	lastev.xbutton.window = parent;
	XPutBackEvent(dpy, &lastev);

	/* End our grab */
	XUngrabPointer(dpy, CurrentTime);

	/*
	 * If you wanted to change workspaces, and clicked _outside_ a
	 * window, it would have just switched way up near the top of the
	 * function.  But if you clicked _in_ a window [in the WSM map], it
	 * would have to go through the whole fun to find out whether you
	 * wanted to move/reoccupy the window, or were just wanting to change
	 * WSen.
	 *
	 * So if it's been <250ms (completely arbitrary and non-configgable,
	 * probably should be rethought) since you started, assume you just
	 * wanted to switch workspaces.  Don't do any occupation change, And
	 * just switch.  Also do some magic related to the map/button
	 * toggling.
	 *
	 * XXX This still leaves any window _moves_ done.  It seems like it
	 * should probably revert those too?
	 */
	if((lastev.xbutton.time - etime) < 250) {
		KeyCode control_L_code, control_R_code;
		char keys [32];

		/* Re-show old miniwindow, destroy the temp, and warp to WS */
		XMapWindow(dpy, sw);
		XDestroyWindow(dpy, w);
		GotoWorkSpace(vs, oldws);
		if(!Scr->DontWarpCursorInWMap) {
			WarpToWindow(win, Scr->RaiseOnWarp);
		}

		/*
		 * The control keys toggle between map and button state.  If we
		 * did a short move, and ctrl is being held at the end, flip the
		 * state.  This has several possible causes and effects.
		 *
		 * One is that _during_ a move, we don't do look through KeyPress
		 * events, so we wouldn't see it happen yet.  But after we're
		 * done and return back into the event loop, that press will come
		 * in and cause the state to change.  It may be weird to the user
		 * to see that happen, not when they hit ctrl, but _later_, after
		 * they release the mouse button.  The "best" solution may be to
		 * find that press in the event queue and empty it out, but a
		 * cheap solution is just to pre-flip it and then let the event
		 * code flip it back.  Flickery maybe, but easy.  Now, _should_ we be
		 * doing that?  I'm a little doubtful...
		 *
		 * A second is that if the WSM is "naturally" in button mode, and
		 * you temporarily ctrl-flip it into map mode and then click in a
		 * window.  This code will cause it to automatically flip back
		 * after you release the mouse if you haven't released ctrl yet.
		 * This is apparently needed because, unless you have
		 * DontWarpCursorInWMap set, the previous few lines of code would
		 * have shifted the cursor to the window you clicked, which means
		 * you don't get a chance to release it in the WSM and flip the
		 * state back.  It seems a reasonable assumption that the user
		 * wanted a temporary change of state just for the purposes of
		 * the change.
		 *
		 * (if you had released the ctrl before the mouse, the release
		 * event would already be queued up up on the WSM, so would flip
		 * it back after we return)
		 *
		 * XXX Should we be checking DontToggleWorkSpaceManagerState
		 * here?
		 */
		control_L_code = XKeysymToKeycode(dpy, XStringToKeysym("Control_L"));
		control_R_code = XKeysymToKeycode(dpy, XStringToKeysym("Control_R"));
		XQueryKeymap(dpy, keys);
		if((keys [control_L_code / 8] & ((char) 0x80 >> (control_L_code % 8))) ||
		                keys [control_R_code / 8] & ((char) 0x80 >> (control_R_code % 8))) {
			WMgrToggleState(vs);
		}
		return;
	}


	/* Find the workspace we finally found up in */
	for(newws = Scr->workSpaceMgr.workSpaceList ; newws != NULL ;
	                newws = newws->next) {
		MapSubwindow *msw = vs->wsw->mswl[newws->number];
		if((newX >= msw->x) && (newX < msw->x + mw->wwidth) &&
		                (newY >= msw->y) && (newY < msw->y + mw->wheight)) {
			break;
		}
	}

	/* And finish off whatever we're supposed to be doing */
	switch(button) {
		case 1 : { /* moving to another workspace */
			int occupation;

			/* If nothing to change, re-map avatar and we're done */
			if((newws == NULL) || (newws == oldws) ||
			                OCCUPY(wl->twm_win, newws)) {
				XMapWindow(dpy, sw);
				break;
			}

			/* Out of the old, into the new */
			occupation = win->occupation;
			occupation |= (1 << newws->number);
			occupation &= ~(1 << oldws->number);
			ChangeOccupation(win, occupation);

			/*
			 * Raise it to the top if it's in our current ws, and
			 * properly slot it into the WSM map stack if not.
			 */
			if(newws == vs->wsw->currentwspc) {
				OtpRaise(win, WinWin);
				WMapRaise(win);
			}
			else {
				WMapRestack(newws);
			}

			break;
		}

		case 2 : { /* putting in extra workspace */
			int occupation;

			/* Nothing to do if it's going nowhere or places it already is */
			if((newws == NULL) || (newws == oldws) ||
			                OCCUPY(wl->twm_win, newws)) {
				break;
			}

			/* Move */
			occupation = win->occupation | (1 << newws->number);
			ChangeOccupation(win, occupation);

			/* Raise/stack */
			if(newws == vs->wsw->currentwspc) {
				OtpRaise(win, WinWin);
				WMapRaise(win);
			}
			else {
				WMapRestack(newws);
			}
			break;
		}

		/*
		 * Should actually never hit this state; only 1/2 would have
		 * gotten this far into the code...
		 */
		default :
			return;
	}

	/* Clean up our temporary moving-around-in the WSM window */
	XDestroyWindow(dpy, w);
}




/*
 ****************************************************************
 *
 * Functions for doing things with subwindows in the WSM in map state
 *
 ****************************************************************
 */

/*
 * Backend for mapping up windows in the WSM map.
 */
static void
wmap_mapwin_backend(TwmWindow *win, bool handleraise)
{
	VirtualScreen *vs;
	WorkSpace *ws;
	WinList *wl;

	for(vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			for(wl = vs->wsw->mswl[ws->number]->wl; wl != NULL; wl = wl->next) {
				if(win == wl->twm_win) {
					/*
					 * When called via deiconify, we might have to do
					 * stuff related to auto-raising the window while we
					 * de-iconify.  When called via a map request, the
					 * window is always wherever it previously was in the
					 * stack.
					 */
					if(!handleraise || Scr->NoRaiseDeicon) {
						XMapWindow(dpy, wl->w);
					}
					else {
						XMapRaised(dpy, wl->w);
					}
					WMapRedrawName(vs, wl);
					break;
				}
			}
		}
	}
}

/*
 * Map up a window's subwindow in the map-mode WSM.  Happens as a result
 * of getting (or faking) a Map request event.  Notably, _not_ in the
 * process of de-iconifying a window; mostly as a result of _creating_
 * windows, or when a client maps itself without a request from us.
 *
 * x-ref comment on WMapDeIconify() for some subtle distinctions between
 * the two...
 */
void
WMapMapWindow(TwmWindow *win)
{
	/* We don't do raise handling */
	wmap_mapwin_backend(win, false);
}


/*
 * De-iconify a window in the WSM map.  The opposite of WMapIconify(),
 * and different from WMapMapWindow() in complicated ways.  This function
 * winds up getting called when a window is de-iconified via a ctwm
 * function.
 */
void
WMapDeIconify(TwmWindow *win)
{
	/* If it's not showing anywhere, nothing to do.  Is this possible? */
	if(!win->vs) {
		return;
	}

	/* Loop and map, handling raises if necessary */
	wmap_mapwin_backend(win, true);
}


/*
 * Hide away a window in the WSM map.  Happens when win is iconified;
 * different from destruction.
 */
void
WMapIconify(TwmWindow *win)
{
	VirtualScreen *vs;
	WorkSpace *ws;
	WinList *wl;

	if(!win->vs) {
		return;
	}

	for(vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			for(wl = vs->wsw->mswl[ws->number]->wl; wl != NULL; wl = wl->next) {
				if(win == wl->twm_win) {
					XUnmapWindow(dpy, wl->w);
					break;
				}
			}
		}
	}
}


/*
 * Position a window in the WSM.  Happens as a result of moving things.
 */
void
WMapSetupWindow(TwmWindow *win, int x, int y, int w, int h)
{
	VirtualScreen *vs;

	/* If it's an icon manager, or not on a vscreen, nothing to do */
	if(win->isiconmgr || !win->vs) {
		return;
	}

	/* If it's a WSM, we may be resetting size/position, but that's it */
	if(win->iswspmgr) {
		if(w == -1) {
			return;
		}
		ResizeWorkSpaceManager(win->vs, win);
		return;
	}

	/* If it's an occupy window, ditto */
	if(win == Scr->workSpaceMgr.occupyWindow->twm_win) {
		if(w == -1) {
			return;
		}
		ResizeOccupyWindow(win);
		return;
	}


	/* For anything else, we're potentially showing something */
	for(vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		WorkSpaceWindow *wsw = vs->wsw;
		WorkSpace *ws;

		/* Scale factors for windows in the map */
		float wf = (float)(wsw->wwidth  - 2) / (float) vs->w;
		float hf = (float)(wsw->wheight - 2) / (float) vs->h;

		/*
		 * Loop around windows in each WS to find all the places the
		 * requested window should show up.
		 */
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			MapSubwindow *msw = wsw->mswl[ws->number];

			for(WinList *wl = msw->wl; wl != NULL; wl = wl->next) {
				if(win == wl->twm_win) {
					/* New positions */
					wl->x = (int)(x * wf);
					wl->y = (int)(y * hf);

					/* Rescale if necessary and move */
					if(w == -1) {
						XMoveWindow(dpy, wl->w, wl->x, wl->y);
					}
					else {
						wl->width  = (unsigned int)((w * wf) + 0.5);
						wl->height = (unsigned int)((h * hf) + 0.5);
						if(!Scr->use3Dwmap) {
							wl->width  -= 2;
							wl->height -= 2;
						}
						if(wl->width < 1) {
							wl->width = 1;
						}
						if(wl->height < 1) {
							wl->height = 1;
						}
						XMoveResizeWindow(dpy, wl->w, wl->x, wl->y,
						                  wl->width, wl->height);
					}
					break;
				}
			}
		}
	}
}


/*
 * Frontends for changing the stacking of windows in the WSM.
 *
 * Strictly speaker, we have no ability to raise or lower a window in the
 * map; we only draw the whole stack.  And these functions don't actually
 * change the stacking of a window, they're called as a _result_ of
 * changing the stacking, to notify the WSM to re-check and update
 * itself.  So while conceptually we maintain a separation for our
 * callers between various reasons this is being called, the
 * implementations are identical.
 */
void
WMapRaiseLower(TwmWindow *win)
{
	WorkSpace *ws;

	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		if(OCCUPY(win, ws)) {
			WMapRestack(ws);
		}
	}
}

void
WMapLower(TwmWindow *win)
{
	WMapRaiseLower(win);
}

void
WMapRaise(TwmWindow *win)
{
	WMapRaiseLower(win);
}


/*
 * Backend for redoing the stacking of a window in the WSM.
 *
 * XXX Since this tends to get called iteratively, there's probably
 * something better we can do than doing all this relatively expensive
 * stuff over and over...
 */
void
WMapRestack(WorkSpace *ws)
{
	WinList *wl;
	Window  root, parent; // Dummy
	Window  *children, *smallws;
	unsigned int nchildren;

	/* Get a whole list of the windows on the screen */
	nchildren = 0;
	XQueryTree(dpy, Scr->Root, &root, &parent, &children, &nchildren);

	/*
	 * We're presumably dealing with a [often very small] subset of them,
	 * but just allocate space for the whole list; easier than trying to
	 * shrink down, and really, how big can it possibly be?
	 */
	smallws = calloc(nchildren, sizeof(Window));

	/* Work it up per vscreen */
	for(VirtualScreen *vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		int j = 0;
		const MapSubwindow *msw = vs->wsw->mswl[ws->number];

		/* Loop backward (from top to bottom of stack) */
		for(int i = nchildren - 1; i >= 0; i--) {
			TwmWindow *win = GetTwmWindow(children[i]);

			/*
			 * Only care about certain windows.  If it's not a window we
			 * know about, or not a frame (e.g., an icon), or doesn't
			 * occupy this workspace, skip it.
			 */
			if(win == NULL || win->frame != children[i] || !OCCUPY(win, ws)) {
				continue;
			}

			/* Debug */
			if(tracefile) {
				fprintf(tracefile, "WMapRestack : w = %lx, win = %p\n",
				        children[i], (void *)win);
				fflush(tracefile);
			}

			/* Find this window in the list for the map of this WS */
			for(wl = msw->wl; wl != NULL; wl = wl->next) {
				/* Debug */
				if(tracefile) {
					fprintf(tracefile, "WMapRestack : wl = %p, twm_win = %p\n",
					        (void *)wl, (void *)wl->twm_win);
					fflush(tracefile);
				}

				if(win == wl->twm_win) {
					/* There you are.  Add into our list to restack. */
					smallws[j++] = wl->w;
					break;
				}
			}
		}

		/*
		 * Restack the windows in the map.  Note that the order is
		 * reversed from earlier; XQueryTree() returns bottom->top,
		 * XRestackWindows() is passed top->bottom.
		 */
		XRestackWindows(dpy, smallws, j);
	}

	/* Cleanup */
	XFree(children);
	free(smallws);
	return;
}




/*
 ****************************************************************
 *
 * Bits related to the actual drawing of the windows in the map.
 *
 ****************************************************************
 */

/*
 * Update stuff in the WSM when win's icon name changes
 */
void
WMapUpdateIconName(TwmWindow *win)
{
	VirtualScreen *vs;
	WorkSpace *ws;
	WinList *wl;

	for(vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
			for(wl = vs->wsw->mswl[ws->number]->wl; wl != NULL; wl = wl->next) {
				if(win == wl->twm_win) {
					WMapRedrawName(vs, wl);
					break;
				}
			}
		}
	}
}


/*
 * Draw a window name into the window's representation in the map-state
 * WSM.
 */
void
WMapRedrawName(VirtualScreen *vs, WinList *wl)
{
	ColorPair cp = wl->cp;

	if(Scr->ReverseCurrentWorkspace && wl->wlist == vs->wsw->currentwspc) {
		InvertColorPair(&cp);
	}
	WMapRedrawWindow(wl->w, wl->width, wl->height, cp, wl->twm_win->icon_name);
}


/*
 * Draw up a window's representation in the map-state WSM, with the
 * window name.
 *
 * The drawing of the window name could probably be done a bit better.
 * The font size is based on a tiny fraction of the window's height, so
 * is probably usually too small to be useful, and often appears just as
 * some odd colored pixels at the top of the window.
 */
static void
WMapRedrawWindow(Window window, int width, int height,
                 ColorPair cp, const char *label)
{
	int x, y;
	const MyFont font = Scr->workSpaceMgr.windowFont;

	/* Blank out window background color */
	XClearWindow(dpy, window);

	/* Figure out where to position the name */
	{
		XRectangle inc_rect;
		XRectangle logical_rect;
		int strhei, strwid;
		int i, descent;
		XFontStruct **xfonts;
		char **font_names;
		int fnum;

		XmbTextExtents(font.font_set, label, strlen(label),
		               &inc_rect, &logical_rect);
		strwid = logical_rect.width;
		strhei = logical_rect.height;

		/*
		 * If it's too tall to fit, just give up now.
		 * XXX Probably should still do border stuff below...
		 */
		if(strhei > height) {
			return;
		}

		x = (width - strwid) / 2;
		if(x < 1) {
			x = 1;
		}

		fnum = XFontsOfFontSet(font.font_set, &xfonts, &font_names);
		for(i = 0, descent = 0; i < fnum; i++) {
			/* xf = xfonts[i]; */
			descent = ((descent < (xfonts[i]->max_bounds.descent)) ?
			           (xfonts[i]->max_bounds.descent) : descent);
		}

		y = ((height + strhei) / 2) - descent;
	}

	/* Draw up borders around the win */
	if(Scr->use3Dwmap) {
		Draw3DBorder(window, 0, 0, width, height, 1, cp, off, true, false);
		FB(cp.fore, cp.back);
	}
	else {
		FB(cp.back, cp.fore);
		XFillRectangle(dpy, window, Scr->NormalGC, 0, 0, width, height);
		FB(cp.fore, cp.back);
	}

	/* Write in the name */
	if(Scr->Monochrome != COLOR) {
		XmbDrawImageString(dpy, window, font.font_set, Scr->NormalGC, x, y,
		                   label, strlen(label));
	}
	else {
		XmbDrawString(dpy, window, font.font_set, Scr->NormalGC, x, y,
		              label, strlen(label));
	}
}




/*
 * Processes for adding/removing windows from the WSM
 */

/*
 * Add a window into any appropriate WSMs' maps.  Called during
 * AddWindow().
 */
void
WMapAddWindow(TwmWindow *win)
{
	WorkSpace *ws;

	if(!WMapWindowMayBeAdded(win)) {
		return;
	}

	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		if(OCCUPY(win, ws)) {
			WMapAddWindowToWorkspace(win, ws);
		}
	}
}


/*
 * Create WSM representation of a given window in a given WS.  Called
 * when windows get added to a workspace, either via WMapAddWindow()
 * during the AddWindow() process, or via an occupation change.
 *
 * (previously: WMapAddToList())
 */
void
WMapAddWindowToWorkspace(TwmWindow *win, WorkSpace *ws)
{
	ColorPair cp;

	/* Setup coloring for the window */
	cp.back = win->title.back;
	cp.fore = win->title.fore;
	if(Scr->workSpaceMgr.windowcpgiven) {
		cp.back = Scr->workSpaceMgr.windowcp.back;
		GetColorFromList(Scr->workSpaceMgr.windowBackgroundL,
		                 win->name, &win->class, &cp.back);
		cp.fore = Scr->workSpaceMgr.windowcp.fore;
		GetColorFromList(Scr->workSpaceMgr.windowForegroundL,
		                 win->name, &win->class, &cp.fore);
	}
	if(Scr->use3Dwmap && !Scr->BeNiceToColormap) {
		GetShadeColors(&cp);
	}

	/* We need a copy in each VS */
	for(VirtualScreen *vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		unsigned int bw;
		WinList *wl;
		const float wf = (float)(vs->wsw->wwidth  - 2) / (float) vs->w;
		const float hf = (float)(vs->wsw->wheight - 2) / (float) vs->h;
		MapSubwindow *msw = vs->wsw->mswl[ws->number];

		/* Put together its winlist entry */
		wl = malloc(sizeof(struct winList));
		wl->wlist  = ws;
		wl->x      = (int)(win->frame_x * wf);
		wl->y      = (int)(win->frame_y * hf);
		wl->width  = (unsigned int)((win->frame_width  * wf) + 0.5);
		wl->height = (unsigned int)((win->frame_height * hf) + 0.5);
		wl->cp     = cp;
		wl->twm_win = win;

		/* Size the window bits */
		bw = 0;
		if(!Scr->use3Dwmap) {
			bw = 1;
			wl->width  -= 2;
			wl->height -= 2;
		}
		if(wl->width < 1) {
			wl->width  = 1;
		}
		if(wl->height < 1) {
			wl->height = 1;
		}

		/* Create its little window */
		wl->w = XCreateSimpleWindow(dpy, msw->w, wl->x, wl->y,
		                            wl->width, wl->height, bw,
		                            Scr->Black, cp.back);

		/* Setup cursor and attributes for it */
		{
			XSetWindowAttributes attr;
			unsigned long attrmask;

			attr.cursor = handCursor;
			attrmask = CWCursor;

			if(Scr->BackingStore) {
				attr.backing_store = WhenMapped;
				attrmask |= CWBackingStore;
			}

			XChangeWindowAttributes(dpy, wl->w, attrmask, &attr);
		}

		/* Setup events and stash context bits */
		XSelectInput(dpy, wl->w, ExposureMask);
		XSaveContext(dpy, wl->w, TwmContext, (XPointer) vs->wsw->twm_win);
		XSaveContext(dpy, wl->w, ScreenContext, (XPointer) Scr);
		XSaveContext(dpy, wl->w, MapWListContext, (XPointer) wl);

		/* Link it onto the front of the list */
		wl->next = msw->wl;
		msw->wl  = wl;

		/*
		 * And map it, if its window is mapped.  That'll kick an expose
		 * event, which will work its way down to WMapRedrawWindow(), and
		 * fill things in.
		 */
		if(win->mapped) {
			XMapWindow(dpy, wl->w);
		}
	} // And around to the next vscreen
}


/*
 * Remove a window from any WSM maps it's in.  Called during window
 * destruction process.
 *
 * (previously: WMapDestroyWindow())
 */
void
WMapRemoveWindow(TwmWindow *win)
{
	WorkSpace *ws;

	for(ws = Scr->workSpaceMgr.workSpaceList; ws != NULL; ws = ws->next) {
		if(OCCUPY(win, ws)) {
			WMapRemoveWindowFromWorkspace(win, ws);
		}
	}

	/*
	 * If it's a mapped occupy window, manually hide aways its bits in
	 * here.
	 *
	 * XXX Better belongs inline in caller or separate func?  This is the
	 * only thing exposing occupyWin out of occupation.c.
	 */
	if(win == occupyWin) {
		OccupyWindow *occwin = Scr->workSpaceMgr.occupyWindow;
		XUnmapWindow(dpy, occwin->twm_win->frame);
		occwin->twm_win->mapped = false;
		occwin->twm_win->occupation = 0;
		occupyWin = NULL;
	}
}


/*
 * Remove window's WSM representation.  Happens from WMapRemoveWindow()
 * as part of the window destruction process, and in the occupation
 * change process.
 *
 * (previously: WMapRemoveFromList())
 */
void
WMapRemoveWindowFromWorkspace(TwmWindow *win, WorkSpace *ws)
{
	VirtualScreen *vs;

	for(vs = Scr->vScreenList; vs != NULL; vs = vs->next) {
		WinList **prev = &vs->wsw->mswl[ws->number]->wl;

		/* Pull it out of the list and destroy it */
		for(WinList *wl = *prev ; wl != NULL ; wl = wl->next) {
			if(win != wl->twm_win) {
				/* Not it */
				prev = &wl->next;
				continue;
			}

			/* There you are.  Unlink and kill */
			*prev = wl->next;

			XDeleteContext(dpy, wl->w, TwmContext);
			XDeleteContext(dpy, wl->w, ScreenContext);
			XDeleteContext(dpy, wl->w, MapWListContext);
			XDestroyWindow(dpy, wl->w);
			free(wl);

			/* Around to the next vscreen */
			break;
		}
	}
}




/*
 ****************************************************************
 *
 * Utils-ish funcs
 *
 ****************************************************************
 */

/*
 * This is really more util.c fodder, but leaving it here for now because
 * it's only used once in WMapRedrawName().  If we start finding external
 * uses for it, it should be moved.
 */
static void
InvertColorPair(ColorPair *cp)
{
	Pixel save;

	save = cp->fore;
	cp->fore = cp->back;
	cp->back = save;
	save = cp->shadc;
	cp->shadc = cp->shadd;
	cp->shadd = save;
}


/*
 * Verify if a window may be added to the workspace map.
 * This is not allowed for
 * - icon managers
 * - the occupy window
 * - workspace manager windows
 * - or, optionally, windows with full occupation.
 */
bool
WMapWindowMayBeAdded(TwmWindow *win)
{
	if(win->isiconmgr) {
		return false;
	}
	if(win == Scr->workSpaceMgr.occupyWindow->twm_win) {
		return false;
	}
	if(win->iswspmgr) {
		return false;
	}
	if(Scr->workSpaceMgr.noshowoccupyall &&
	                win->occupation == fullOccupation) {
		return false;
	}
	return true;
}