File: events.c

package info (click to toggle)
vtwm 5.4.7-5
  • links: PTS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 2,596 kB
  • ctags: 1,969
  • sloc: ansic: 20,457; yacc: 992; sh: 212; lex: 111; makefile: 70
file content (4272 lines) | stat: -rw-r--r-- 112,457 bytes parent folder | download | duplicates (2)
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
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
/*****************************************************************************/
/**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
/**                          Salt Lake City, Utah                           **/
/**  Portions Copyright 1989 by the Massachusetts Institute of Technology   **/
/**                        Cambridge, Massachusetts                         **/
/**                                                                         **/
/**                           All Rights Reserved                           **/
/**                                                                         **/
/**    Permission to use, copy, modify, and distribute this software and    **/
/**    its documentation  for  any  purpose  and  without  fee is hereby    **/
/**    granted, provided that the above copyright notice appear  in  all    **/
/**    copies and that both  that  copyright  notice  and  this  permis-    **/
/**    sion  notice appear in supporting  documentation,  and  that  the    **/
/**    names of Evans & Sutherland and M.I.T. not be used in advertising    **/
/**    in publicity pertaining to distribution of the  software  without    **/
/**    specific, written prior permission.                                  **/
/**                                                                         **/
/**    EVANS & SUTHERLAND AND M.I.T. DISCLAIM ALL WARRANTIES WITH REGARD    **/
/**    TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES  OF  MERCHANT-    **/
/**    ABILITY  AND  FITNESS,  IN  NO  EVENT SHALL EVANS & SUTHERLAND OR    **/
/**    M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL  DAM-    **/
/**    AGES OR  ANY DAMAGES WHATSOEVER  RESULTING FROM LOSS OF USE, DATA    **/
/**    OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER    **/
/**    TORTIOUS ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE    **/
/**    OR PERFORMANCE OF THIS SOFTWARE.                                     **/
/*****************************************************************************/


/***********************************************************************
 *
 * $XConsortium: events.c,v 1.182 91/07/17 13:59:14 dave Exp $
 *
 * twm event handling
 *
 * 17-Nov-87 Thomas E. LaStrange		File created
 *
 ***********************************************************************/

#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <poll.h>
#include <errno.h>
#include "twm.h"
#include <X11/Xatom.h>
#include "add_window.h"
#include "menus.h"
#include "events.h"
#include "resize.h"
#include "parse.h"
#include "gram.h"
#include "util.h"
#include "screen.h"
#include "iconmgr.h"
#include "version.h"
#include "desktop.h"
/* djhjr - 6/22/01 */
#ifndef NO_SOUND_SUPPORT
#include "sound.h"
#endif
/* Submitted by Takeharu Kato */
#ifdef NEED_SELECT_H
#include <sys/select.h>	/* RAISEDELAY */
#else
#include <sys/time.h>	/* RAISEDELAY */
#include <sys/types.h>	/* RAISEDELAY */
#include <unistd.h>
#endif
#include <unistd.h>
#include <assert.h>
#include <sys/fcntl.h>

extern void IconDown();
/* djhjr - 4/26/99 */
extern void AppletDown();

static void do_menu ();
void RedoIconName();

extern int iconifybox_width, iconifybox_height;
extern unsigned int mods_used;
extern int menuFromFrameOrWindowOrTitlebar;
/* djhjr - 6/22/01 */
#ifndef NO_SOUND_SUPPORT
extern int createSoundFromFunction;
extern int destroySoundFromFunction;
#endif

#define MAX_X_EVENT 256
event_proc EventHandler[MAX_X_EVENT]; /* event handler jump table */
char *Action;
int Context = C_NO_CONTEXT;	/* current button press context */
TwmWindow *ButtonWindow;	/* button press window structure */
XEvent ButtonEvent;		/* button press event */
XEvent Event;			/* the current event */
TwmWindow *Tmp_win;		/* the current twm window */

/* Used in HandleEnterNotify to remove border highlight from a window
 * that has not recieved a LeaveNotify event because of a pointer grab
 */
TwmWindow *UnHighLight_win = NULL;

Window DragWindow;		/* variables used in moving windows */
int origDragX;
int origDragY;
int DragX;
int DragY;
int DragWidth;
int DragHeight;
int CurrentDragX;
int CurrentDragY;

/* Vars to tell if the resize has moved. */
extern int ResizeOrigX;
extern int ResizeOrigY;

static int enter_flag;
static int ColortableThrashing;
static TwmWindow *enter_win, *raise_win;

ScreenInfo *FindScreenInfo();
int ButtonPressed = -1;
int Cancel = FALSE;
int GlobalFirstTime = True;
int GlobalMenuButton = False;

void HandleCreateNotify();

void HandleShapeNotify ();
extern int ShapeEventBase, ShapeErrorBase;

void AutoRaiseWindow (tmp)
	TwmWindow *tmp;
{
	XRaiseWindow (dpy, tmp->frame);
	XRaiseWindow (dpy, tmp->VirtualDesktopDisplayWindow);

	RaiseStickyAbove(); /* DSE */
	RaiseAutoPan();
	
	XSync (dpy, 0);
	enter_win = NULL;
	enter_flag = TRUE;
	raise_win = tmp;
}

void SetRaiseWindow (tmp)
	TwmWindow *tmp;
{
	enter_flag = TRUE;
	enter_win = NULL;
	raise_win = tmp;
	XSync (dpy, 0);
}



/***********************************************************************
 *
 *  Procedure:
 *	InitEvents - initialize the event jump table
 *
 ***********************************************************************
 */

void
InitEvents()
{
	int i;


	ResizeWindow = 0;
	DragWindow = 0;
	enter_flag = FALSE;
	enter_win = raise_win = NULL;

	for (i = 0; i < MAX_X_EVENT; i++)
	EventHandler[i] = HandleUnknown;

	EventHandler[Expose] = HandleExpose;
	EventHandler[CreateNotify] = HandleCreateNotify;
	EventHandler[DestroyNotify] = HandleDestroyNotify;
	EventHandler[MapRequest] = HandleMapRequest;
	EventHandler[MapNotify] = HandleMapNotify;
	EventHandler[UnmapNotify] = HandleUnmapNotify;
#if 0 /* functionality moved to menus.c:ExecuteFunction() - djhjr - 11/7/03 */
	EventHandler[MotionNotify] = HandleMotionNotify;
#endif
	EventHandler[ButtonRelease] = HandleButtonRelease;
	EventHandler[ButtonPress] = HandleButtonPress;
	EventHandler[EnterNotify] = HandleEnterNotify;
	EventHandler[LeaveNotify] = HandleLeaveNotify;
	EventHandler[ConfigureRequest] = HandleConfigureRequest;
	EventHandler[ClientMessage] = HandleClientMessage;
	EventHandler[PropertyNotify] = HandlePropertyNotify;
	EventHandler[KeyPress] = HandleKeyPress;
	EventHandler[ColormapNotify] = HandleColormapNotify;
	EventHandler[VisibilityNotify] = HandleVisibilityNotify;
	if (HasShape)
	EventHandler[ShapeEventBase+ShapeNotify] = HandleShapeNotify;
}




Time lastTimestamp = CurrentTime;	/* until Xlib does this for us */

Bool StashEventTime (ev)
	register XEvent *ev;
{
	switch (ev->type) {
	  case KeyPress:
	  case KeyRelease:
	lastTimestamp = ev->xkey.time;
	return True;
	  case ButtonPress:
	  case ButtonRelease:
	lastTimestamp = ev->xbutton.time;
	return True;
	  case MotionNotify:
	lastTimestamp = ev->xmotion.time;
	return True;
	  case EnterNotify:
	  case LeaveNotify:
	lastTimestamp = ev->xcrossing.time;
	return True;
	  case PropertyNotify:
	lastTimestamp = ev->xproperty.time;
	return True;
	  case SelectionClear:
	lastTimestamp = ev->xselectionclear.time;
	return True;
	  case SelectionRequest:
	lastTimestamp = ev->xselectionrequest.time;
	return True;
	  case SelectionNotify:
	lastTimestamp = ev->xselection.time;
	return True;
	}
	return False;
}



/*
 * WindowOfEvent - return the window about which this event is concerned; this
 * window may not be the same as XEvent.xany.window (the first window listed
 * in the structure).
 */
Window WindowOfEvent (e)
	XEvent *e;
{
	/*
	 * Each window subfield is marked with whether or not it is the same as
	 * XEvent.xany.window or is different (which is the case for some of the
	 * notify events).
	 */
	switch (e->type) {
	  case KeyPress:
	  case KeyRelease:  return e->xkey.window;			     /* same */
	  case ButtonPress:
	  case ButtonRelease:  return e->xbutton.window;		     /* same */
	  case MotionNotify:  return e->xmotion.window;		     /* same */
	  case EnterNotify:
	  case LeaveNotify:  return e->xcrossing.window;		     /* same */
	  case FocusIn:
	  case FocusOut:  return e->xfocus.window;			     /* same */
	  case KeymapNotify:  return e->xkeymap.window;		     /* same */
	  case Expose:  return e->xexpose.window;			     /* same */
	  case GraphicsExpose:  return e->xgraphicsexpose.drawable;	     /* same */
	  case NoExpose:  return e->xnoexpose.drawable;		     /* same */
	  case VisibilityNotify:  return e->xvisibility.window;	     /* same */
	  case CreateNotify:  return e->xcreatewindow.window;	     /* DIFF */
	  case DestroyNotify:  return e->xdestroywindow.window;	     /* DIFF */
	  case UnmapNotify:  return e->xunmap.window;		     /* DIFF */
	  case MapNotify:  return e->xmap.window;			     /* DIFF */
	  case MapRequest:  return e->xmaprequest.window;		     /* DIFF */
	  case ReparentNotify:  return e->xreparent.window;		     /* DIFF */
	  case ConfigureNotify:  return e->xconfigure.window;	     /* DIFF */
	  case ConfigureRequest:  return e->xconfigurerequest.window;    /* DIFF */
	  case GravityNotify:  return e->xgravity.window;		     /* DIFF */
	  case ResizeRequest:  return e->xresizerequest.window;	     /* same */
	  case CirculateNotify:  return e->xcirculate.window;	     /* DIFF */
	  case CirculateRequest:  return e->xcirculaterequest.window;    /* DIFF */
	  case PropertyNotify:  return e->xproperty.window;		     /* same */
	  case SelectionClear:  return e->xselectionclear.window;	     /* same */
	  case SelectionRequest: return e->xselectionrequest.requestor;  /* DIFF */
	  case SelectionNotify:  return e->xselection.requestor;	     /* same */
	  case ColormapNotify:  return e->xcolormap.window;		     /* same */
	  case ClientMessage:  return e->xclient.window;		     /* same */
	  case MappingNotify:  return None;
	}
	return None;
}



/***********************************************************************
 *
 *  Procedure:
 *	DispatchEvent2 -
 *      handle a single X event stored in global var Event
 *      this routine for is for a call during an f.move
 *
 **********************************************************************/
/*
 * Merged into DispatchEvent()
 * djhjr - 10/6/02
 */
#if 0
Bool DispatchEvent2 ()
{
	Window w = Event.xany.window;
	StashEventTime (&Event);

	if (XFindContext (dpy, w, TwmContext, (caddr_t *) &Tmp_win) == XCNOENT)
	  Tmp_win = NULL;

	if (XFindContext (dpy, w, ScreenContext, (caddr_t *)&Scr) == XCNOENT) {
	Scr = FindScreenInfo (WindowOfEvent (&Event));
	}

	if (!Scr) return False;

	if (menuFromFrameOrWindowOrTitlebar && Event.type == Expose)
	  HandleExpose();

	if (!menuFromFrameOrWindowOrTitlebar && Event.type>= 0 && Event.type < MAX_X_EVENT) {
	(*EventHandler[Event.type])();
	}

	return True;
}
#endif

/***********************************************************************
 *
 *  Procedure:
 *	DispatchEvent - handle a single X event stored in global var Event
 *
 ***********************************************************************
 */
Bool DispatchEvent ()
{
	Window w = Event.xany.window;
	StashEventTime (&Event);

	if (XFindContext (dpy, w, TwmContext, (caddr_t *) &Tmp_win) == XCNOENT)
		Tmp_win = NULL;

	if (XFindContext (dpy, w, ScreenContext, (caddr_t *)&Scr) == XCNOENT)
		Scr = FindScreenInfo (WindowOfEvent (&Event));

	if (!Scr) return False;

	if (MoveFunction != F_NOFUNCTION && menuFromFrameOrWindowOrTitlebar)
	{
		if (Event.type == Expose)
			HandleExpose();
	}
	else if (Event.type >= 0 && Event.type < MAX_X_EVENT)
		(*EventHandler[Event.type])();

	return True;
}



/***********************************************************************
 *
 *  Procedures:
 *      HandleSignal   use instead of signal
 *
 ***********************************************************************
 */

#define HANDLE_SIG_MAX 256 /* increase means changing type written to pipe */

static SigProc signaltable[HANDLE_SIG_MAX];
static int sigselfpipe[2] = { -1, -1 };

static void
SelfPipeHandler(sig)
	int sig;
{
	unsigned char sigchar = sig;
	int esave;

	esave = errno;
	write(sigselfpipe[1], &sigchar, 1);
	errno = esave;
}

void
HandleSignal(sig, func)
	int sig;
	SigProc func;
{
	struct sigaction sa;
	int r, i;

	if (sigselfpipe[0] == -1) {
		r = pipe(sigselfpipe);
		if (r) { perror("pipe for signals"); exit(-1); }
		for (i = 0; i < 2; i++) {
			r = fcntl(sigselfpipe[i], F_GETFL);
			if (r<0) { perror("fcntl get for pipe"); exit(-1); }
			r |= O_NONBLOCK;
			r = fcntl(sigselfpipe[i], F_SETFL, r);
			if (r<0) { perror("fcntl get for pipe"); exit(-1); }
		}
	}

	assert(sig < HANDLE_SIG_MAX);

	r = sigaction(sig, NULL, &sa);
	if (r) { perror("sigaction get"); exit(-1); }

	if (sa.sa_handler != SIG_DFL)
		return;

	signaltable[sig] = func;
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = SelfPipeHandler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	r = sigaction(sig, &sa, NULL);
	if (r) { perror("sigaction set"); exit(-1); }
}

/***********************************************************************
 *
 *  Procedure:
 *	HandleEvents - handle X events
 *
 ***********************************************************************
 */

void
HandleEvents()
{
	struct pollfd pfds[2];
	pfds[0].fd = ConnectionNumber(dpy);
	pfds[0].events = POLLIN|POLLPRI;
	pfds[1].fd = sigselfpipe[0];
	pfds[1].events = POLLIN|POLLPRI;
	sigset_t emptyset;
	sigemptyset(&emptyset);

	while (TRUE)
	{
	if (enter_flag && !QLength(dpy)) {
	    if (enter_win && enter_win != raise_win) {
		AutoRaiseWindow (enter_win);  /* sets enter_flag T */
	    } else {
		enter_flag = FALSE;
	    }
	}
	if (ColortableThrashing && !QLength(dpy) && Scr) {
	    InstallWindowColormaps(ColormapNotify, (TwmWindow *) NULL);
	}
	WindowMoved = FALSE;
	while (!XCheckMaskEvent(dpy, -1 /* wot no AllEventMask */, &Event)) {
		int r;
		unsigned char signum;
		r = poll(pfds, 2, -1);
		if (r<0 && errno!=EINTR) {
			perror("vtwm: poll failed");
			exit(-1);
		}
		for (;;) {
			r = read(sigselfpipe[0], &signum, 1);
			if (r < 0) {
				if (errno == EINTR) continue;
				if (errno == EAGAIN) break;
				if (errno == EWOULDBLOCK) break;
				perror("vtwm: read signal self-pipe");
				exit(-1);
			}
			if (!r) {
				fputs("signal self-pipe read EOF!",stderr);
				exit(-1);
			}
			assert(signum < HANDLE_SIG_MAX);
			assert(signaltable[signum]);
			signaltable[signum](signum);
		}
	}
	(void) DispatchEvent ();
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleColormapNotify - colormap notify event handler
 *
 * This procedure handles both a client changing its own colormap, and
 * a client explicitly installing its colormap itself (only the window
 * manager should do that, so we must set it correctly).
 *
 ***********************************************************************
 */

void
HandleColormapNotify()
{
	XColormapEvent *cevent = (XColormapEvent *) &Event;
	ColormapWindow *cwin, **cwins;
	TwmColormap *cmap;
	int lost, won, n, number_cwins;
	extern TwmColormap *CreateTwmColormap();

	if (XFindContext(dpy, cevent->window, ColormapContext, (caddr_t *)&cwin) == XCNOENT)
	return;
	cmap = cwin->colormap;

	if (cevent->new)
	{
	if (XFindContext(dpy, cevent->colormap, ColormapContext,
			 (caddr_t *)&cwin->colormap) == XCNOENT)
	    cwin->colormap = CreateTwmColormap(cevent->colormap);
	else
	    cwin->colormap->refcnt++;

	cmap->refcnt--;

	if (cevent->state == ColormapUninstalled)
	    cmap->state &= ~CM_INSTALLED;
	else
	    cmap->state |= CM_INSTALLED;

	if (cmap->state & CM_INSTALLABLE)
	    InstallWindowColormaps(ColormapNotify, (TwmWindow *) NULL);

	if (cmap->refcnt == 0)
	{
	    XDeleteContext(dpy, cmap->c, ColormapContext);
	    free((char *) cmap);
	}

	return;
	}

	if (cevent->state == ColormapUninstalled &&
	(cmap->state & CM_INSTALLABLE))
	{
	if (!(cmap->state & CM_INSTALLED))
	    return;
	cmap->state &= ~CM_INSTALLED;

	if (!ColortableThrashing)
	{
	    ColortableThrashing = TRUE;
	    XSync(dpy, 0);
	}

	if (cevent->serial >= Scr->cmapInfo.first_req)
	{
	    number_cwins = Scr->cmapInfo.cmaps->number_cwins;

	    /*
	     * Find out which colortables collided.
	     */

	    cwins = Scr->cmapInfo.cmaps->cwins;
	    for (lost = won = -1, n = 0;
		 (lost == -1 || won == -1) && n < number_cwins;
		 n++)
	    {
		if (lost == -1 && cwins[n] == cwin)
		{
		    lost = n;	/* This is the window which lost its colormap */
		    continue;
		}

		if (won == -1 &&
		    cwins[n]->colormap->install_req == cevent->serial)
		{
		    won = n;	/* This is the window whose colormap caused */
		    continue;	/* the de-install of the previous colormap */
		}
	    }

	    /*
	    ** Cases are:
	    ** Both the request and the window were found:
	    **		One of the installs made honoring the WM_COLORMAP
	    **		property caused another of the colormaps to be
	    **		de-installed, just mark the scoreboard.
	    **
	    ** Only the request was found:
	    **		One of the installs made honoring the WM_COLORMAP
	    **		property caused a window not in the WM_COLORMAP
	    **		list to lose its map.  This happens when the map
	    **		it is losing is one which is trying to be installed,
	    **		but is getting getting de-installed by another map
	    **		in this case, we'll get a scoreable event later,
	    **		this one is meaningless.
	    **
	    ** Neither the request nor the window was found:
	    **		Somebody called installcolormap, but it doesn't
	    **		affect the WM_COLORMAP windows.  This case will
	    **		probably never occur.
	    **
	    ** Only the window was found:
	    **		One of the WM_COLORMAP windows lost its colormap
	    **		but it wasn't one of the requests known.  This is
	    **		probably because someone did an "InstallColormap".
	    **		The colormap policy is "enforced" by re-installing
	    **		the colormaps which are believed to be correct.
	    */

	    if (won != -1)
		if (lost != -1)
		{
		    /* lower diagonal index calculation */
		    if (lost > won)
			n = lost*(lost-1)/2 + won;
		    else
			n = won*(won-1)/2 + lost;
		    Scr->cmapInfo.cmaps->scoreboard[n] = 1;
		} else
		{
		    /*
		    ** One of the cwin installs caused one of the cwin
		    ** colormaps to be de-installed, so I'm sure to get an
		    ** UninstallNotify for the cwin I know about later.
		    ** I haven't got it yet, or the test of CM_INSTALLED
		    ** above would have failed.  Turning the CM_INSTALLED
		    ** bit back on makes sure we get back here to score
		    ** the collision.
		    */
		    cmap->state |= CM_INSTALLED;
		}
	    else if (lost != -1)
		InstallWindowColormaps(ColormapNotify, (TwmWindow *) NULL);
	}
	}

	else if (cevent->state == ColormapUninstalled)
	cmap->state &= ~CM_INSTALLED;

	else if (cevent->state == ColormapInstalled)
	cmap->state |= CM_INSTALLED;
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleVisibilityNotify - visibility notify event handler
 *
 * This routine keeps track of visibility events so that colormap
 * installation can keep the maximum number of useful colormaps
 * installed at one time.
 *
 ***********************************************************************
 */

void
HandleVisibilityNotify()
{
	XVisibilityEvent *vevent = (XVisibilityEvent *) &Event;
	ColormapWindow *cwin;
	TwmColormap *cmap;

	if (XFindContext(dpy, vevent->window, ColormapContext, (caddr_t *)&cwin) == XCNOENT)
	return;

	/*
	 * when Saber complains about retreiving an <int> from an <unsigned int>
	 * just type "touch vevent->state" and "cont"
	 */
	cmap = cwin->colormap;
	if ((cmap->state & CM_INSTALLABLE) &&
	vevent->state != cwin->visibility &&
	(vevent->state == VisibilityFullyObscured ||
	 cwin->visibility == VisibilityFullyObscured) &&
	cmap->w == cwin->w) {
	cwin->visibility = vevent->state;
	InstallWindowColormaps(VisibilityNotify, (TwmWindow *) NULL);
	} else
	cwin->visibility = vevent->state;
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleKeyPress - key press event handler
 *
 ***********************************************************************
 */

int MovedFromKeyPress = False;

void
HandleKeyPress()
{
	FuncKey *key;
	int len;
	unsigned int modifier;
	TwmWindow *tmp_win;

	/* djhjr - 6/5/98 */
	int have_ScrFocus = 0;

#if 0
	if (InfoLines)
	{	XUnmapWindow(dpy, Scr->InfoWindow);
RFB july 28 1993 this code was wrong anyway because
InfoLines should have been set to 0.
Simply remove it...
	}
#endif
	Context = C_NO_CONTEXT;

	if (Event.xany.window == Scr->Root)
	Context = C_ROOT;
	if ((Event.xany.window == Scr->VirtualDesktopDisplay) ||
	(Event.xany.window == Scr->VirtualDesktopDisplayOuter))
	{
		if (Event.xkey.subwindow &&
		    (XFindContext(dpy, Event.xkey.subwindow, VirtualContext, (caddr_t *) &tmp_win)
		     != XCNOENT)) {
			Tmp_win = tmp_win;
			Context = C_VIRTUAL_WIN;
		} else {
			Context = C_VIRTUAL;
			Tmp_win = Scr->VirtualDesktopDisplayTwin;
		}
	}
	if (Tmp_win)
	{
	if (Event.xany.window == Tmp_win->title_w)
	    Context = C_TITLE;
	if (Event.xany.window == Tmp_win->w)
	    Context = C_WINDOW;
	if (Event.xany.window == Tmp_win->icon_w)
	    Context = C_ICON;
	if (Event.xany.window == Tmp_win->frame)
	    Context = C_FRAME;
	if (Tmp_win->list && Event.xany.window == Tmp_win->list->w)
	    Context = C_ICONMGR;
	if (Tmp_win->list && Event.xany.window == Tmp_win->list->icon)
	    Context = C_ICONMGR;
	}

	/*
	 * Now HERE'S a fine little kludge: Make an icon manager's frame or
	 * the virtual desktop's frame or a door and it's frame context-
	 * sensitive to key bindings, and make the frames of windows without
	 * titlebars forward key events.
	 *
	 * djhjr - 6/5/98 7/2/98 7/14/98
	 */
	if (Scr->Focus && (Context == C_NO_CONTEXT || Context == C_ROOT))
	{
		/* ugly, but it works! see also iconmgr.c:RemoveIconManager() */
		if (Scr->Focus->iconmgr)
		{
#ifdef NEVER /* warps to icon managers uniquely handled in menus.c:WarpToWindow() */
			if (!Scr->Focus->iconmgrp->active)
			{
				ActiveIconManager(Scr->Focus->iconmgrp->last);
				Tmp_win = Scr->Focus;
			}
			else
				Tmp_win = Scr->Focus->iconmgrp->active->twm;
#endif

			have_ScrFocus = 1;
		}
		else if (Scr->VirtualDesktopDisplayTwin == Scr->Focus)
		{
			Tmp_win = Scr->Focus;
			Context = C_VIRTUAL;
		}
		/* XFindContext() doesn't seem to work here!?! */
		else if (Scr->Doors)
		{
			TwmDoor *door_win;

			for (door_win = Scr->Doors; door_win != NULL;
					door_win = door_win->next)
				if (door_win->twin == Scr->Focus)
				{
					Tmp_win = Scr->Focus;
					Context = C_DOOR;

					break;
				}
		}
		else if (Scr->Focus->frame && !Scr->Focus->title_w)
		{
			Tmp_win = Scr->Focus;
			Event.xany.window = Tmp_win->frame;
			Context = C_FRAME;
		}
	}

	modifier = (Event.xkey.state & mods_used);
	for (key = Scr->FuncKeyRoot.next; key != NULL; key = key->next)
	{
	if (key->keycode == Event.xkey.keycode &&
	    key->mods == modifier &&
	    (key->cont == Context || key->cont == C_NAME))
	{
	    /* it doesn't make sense to resize from a key press? */
	    if (key->func == F_RESIZE)
			return;

		/*
		 * Exceptions for warps from icon managers (see the above kludge)
		 *
		 * djhjr - 6/5/98 7/2/98 7/14/98
		 */
		switch (key->func)
		{
			case F_WARP:
				if (have_ScrFocus && Context == C_ROOT)
					return;

				break;
			case F_WARPCLASSNEXT:
			case F_WARPCLASSPREV:
			case F_WARPRING:
				if (Context == C_ICONMGR)
					Scr->Focus = Tmp_win = Tmp_win->list->iconmgr->twm_win;

				if (have_ScrFocus)
				{
					Tmp_win = Scr->Focus;
					Context = C_ICONMGR;
				}

				break;
/*			case F_WARPTO:*/
/*			case F_WARPTOICONMGR:*/
/*			case F_WARPTONEWEST:*/
			default:
				break;
		}

		/* special case for moves */
		if (key->func == F_MOVE || key->func == F_FORCEMOVE)
			MovedFromKeyPress = True;

	    if (key->cont != C_NAME)
	    {
		ExecuteFunction(key->func, key->action, Event.xany.window,
		    Tmp_win, &Event, Context, FALSE);

		/*
		 * Added this 'if ()' for deferred keyboard events (see also menus.c)
		 * Submitted by Michel Eyckmans
		 */
		if (!(Context = C_ROOT && RootFunction != F_NOFUNCTION))
		  XUngrabPointer(dpy, CurrentTime);

		return;
	    }
	    else
	    {
		int matched = FALSE;
		len = strlen(key->win_name);

		/* try and match the name first */
		for (Tmp_win = Scr->TwmRoot.next; Tmp_win != NULL;
		    Tmp_win = Tmp_win->next)
		{
		    if (!strncmp(key->win_name, Tmp_win->name, len))
		    {
			matched = TRUE;
			ExecuteFunction(key->func, key->action, Tmp_win->frame,
			    Tmp_win, &Event, C_FRAME, FALSE);
			XUngrabPointer(dpy, CurrentTime);
		    }
		}

		/* now try the res_name */
		if (!matched)
		for (Tmp_win = Scr->TwmRoot.next; Tmp_win != NULL;
		    Tmp_win = Tmp_win->next)
		{
		    if (!strncmp(key->win_name, Tmp_win->class.res_name, len))
		    {
			matched = TRUE;
			ExecuteFunction(key->func, key->action, Tmp_win->frame,
			    Tmp_win, &Event, C_FRAME, FALSE);
			XUngrabPointer(dpy, CurrentTime);
		    }
		}

		/* now try the res_class */
		if (!matched)
		for (Tmp_win = Scr->TwmRoot.next; Tmp_win != NULL;
		    Tmp_win = Tmp_win->next)
		{
		    if (!strncmp(key->win_name, Tmp_win->class.res_class, len))
		    {
			matched = TRUE;
			ExecuteFunction(key->func, key->action, Tmp_win->frame,
			    Tmp_win, &Event, C_FRAME, FALSE);
			XUngrabPointer(dpy, CurrentTime);
		    }
		}
		if (matched)
		    return;
	    }
	}
	}

	/*
	 * If we get here, no function was bound to the key.  Send it
	 * to the client if it was in a window we know about.
	 */
	if (Tmp_win)
	{
	    if (Event.xany.window == Tmp_win->icon_w ||
	    Event.xany.window == Tmp_win->frame ||
	    Event.xany.window == Tmp_win->title_w ||
	    (Tmp_win->list && (Event.xany.window == Tmp_win->list->w)))
	    {
	        Event.xkey.window = Tmp_win->w;
	        XSendEvent(dpy, Tmp_win->w, False, KeyPressMask, &Event);
	    }
	}

}



static void free_window_names (tmp, nukefull, nukename, nukeicon)
	TwmWindow *tmp;
	Bool nukefull, nukename, nukeicon;
{
	/*  the other two "free()"s were "XFree()"s - djhjr - 9/14/03 */
/*

 * XXX - are we sure that nobody ever sets these to another constant (check
 * twm windows)?
 */
	if (tmp->name == tmp->full_name) nukefull = False;

/* this test is never true anymore... - djhjr - 2/20/99
	if (tmp->name == tmp->icon_name) nukename = False;
*/

#define isokay(v) ((v) && (v) != NoName)

	if (nukefull && isokay(tmp->full_name)) free (tmp->full_name);
	if (nukename && isokay(tmp->name)) free (tmp->name);

/* ...because the icon name is now alloc()'d locally - djhjr - 2/20/99
	if (nukeicon && isokay(tmp->icon_name)) XFree (tmp->icon_name);
*/
	if (nukeicon && tmp->icon_name) free(tmp->icon_name);

#undef isokay
	return;
}



void free_cwins (tmp)
	TwmWindow *tmp;
{
	int i;
	TwmColormap *cmap;

	if (tmp->cmaps.number_cwins) {
	for (i = 0; i < tmp->cmaps.number_cwins; i++) {
	     if (--tmp->cmaps.cwins[i]->refcnt == 0) {
		cmap = tmp->cmaps.cwins[i]->colormap;
		if (--cmap->refcnt == 0) {
		    XDeleteContext(dpy, cmap->c, ColormapContext);
		    free((char *) cmap);
		}
		XDeleteContext(dpy, tmp->cmaps.cwins[i]->w, ColormapContext);
		free((char *) tmp->cmaps.cwins[i]);
	    }
	}
	free((char *) tmp->cmaps.cwins);
	if (tmp->cmaps.number_cwins > 1) {
	    free(tmp->cmaps.scoreboard);
	    tmp->cmaps.scoreboard = NULL;
	}
	tmp->cmaps.number_cwins = 0;
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	HandlePropertyNotify - property notify event handler
 *
 ***********************************************************************
 */

void
HandlePropertyNotify()
{
	char *prop = NULL;
#ifdef NO_I18N_SUPPORT
	Atom actual = None;
	int actual_format;
	unsigned long nitems, bytesafter;
#endif
	unsigned long valuemask;		/* mask for create windows */
	XSetWindowAttributes attributes;	/* attributes for create windows */
	Pixmap pm;

	/* watch for standard colormap changes */
	if (Event.xproperty.window == Scr->Root) {
	XStandardColormap *maps = NULL;
	int nmaps;

	switch (Event.xproperty.state) {
	  case PropertyNewValue:
	    if (XGetRGBColormaps (dpy, Scr->Root, &maps, &nmaps,
				  Event.xproperty.atom)) {
		/* if got one, then replace any existing entry */
		InsertRGBColormap (Event.xproperty.atom, maps, nmaps, True);
	    }
	    return;

	  case PropertyDelete:
	    RemoveRGBColormap (Event.xproperty.atom);
	    return;
	}
	}

	if (!Tmp_win) return;		/* unknown window */

#define MAX_NAME_LEN 200L		/* truncate to this many */
#define MAX_ICON_NAME_LEN 200L		/* ditto */

	switch (Event.xproperty.atom) {
	  case XA_WM_NAME:
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	if (!I18N_FetchName(dpy, Tmp_win->w, &prop))
#else
	if (XGetWindowProperty (dpy, Tmp_win->w, Event.xproperty.atom, 0L,
			MAX_NAME_LEN, False, XA_STRING, &actual,
			&actual_format, &nitems, &bytesafter,
			(unsigned char **) &prop) != Success || actual == None)
#endif
		return;

	free_window_names (Tmp_win, True, True, False);
	Tmp_win->full_name = (prop) ? strdup(prop) : NoName;
	Tmp_win->name = (prop) ? strdup(prop) : NoName;
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	if (prop) free(prop);
#else
	if (prop) XFree(prop);
#endif

/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	Tmp_win->name_width = MyFont_TextWidth (&Scr->TitleBarFont,
#else
	Tmp_win->name_width = XTextWidth (Scr->TitleBarFont.font,
#endif
					  Tmp_win->name,
					  strlen (Tmp_win->name));

	SetupWindow (Tmp_win, Tmp_win->frame_x, Tmp_win->frame_y,
		     Tmp_win->frame_width, Tmp_win->frame_height, -1);

	if (Tmp_win->title_w) XClearArea(dpy, Tmp_win->title_w, 0,0,0,0, True);

	/*
	 * if the icon name is NoName, set the name of the icon to be
	 * the same as the window
	 */
/* see that the icon name is it's own memory - djhjr - 2/20/99
	if (Tmp_win->icon_name == NoName) {
	    Tmp_win->icon_name = Tmp_win->name;
*/
	if (!strcmp(Tmp_win->icon_name, NoName)) {
	    free(Tmp_win->icon_name);
	    Tmp_win->icon_name = strdup(Tmp_win->name);

	    RedoIconName();
	}
	break;

	  case XA_WM_ICON_NAME:
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	if (!I18N_GetIconName(dpy, Tmp_win->w, &prop))
#else
	if (XGetWindowProperty (dpy, Tmp_win->w, Event.xproperty.atom, 0,
			MAX_ICON_NAME_LEN, False, XA_STRING, &actual,
			&actual_format, &nitems, &bytesafter,
			(unsigned char **) &prop) != Success || actual == None)
#endif
		return;

/* see that the icon name is it's own memory - djhjr - 2/20/99
	if (!prop) prop = NoName;
	free_window_names (Tmp_win, False, False, True);
	Tmp_win->icon_name = prop;
*/
	free_window_names (Tmp_win, False, False, True);
	Tmp_win->icon_name = (prop) ? strdup(prop) : NoName;
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	if (prop) free(prop);
#else
	if (prop) XFree(prop);
#endif

	RedoIconName();

	break;

	  case XA_WM_HINTS:
	if (Tmp_win->wmhints) XFree ((char *) Tmp_win->wmhints);
	Tmp_win->wmhints = XGetWMHints(dpy, Event.xany.window);

	if (Tmp_win->wmhints && (Tmp_win->wmhints->flags & WindowGroupHint))
	  Tmp_win->group = Tmp_win->wmhints->window_group;

	if (!Tmp_win->forced && Tmp_win->wmhints &&
	    Tmp_win->wmhints->flags & IconWindowHint) {
	    if (Tmp_win->icon_w) {
	    	int icon_x, icon_y;

		/*
		 * There's already an icon window.
		 * Try to find out where it is; if we succeed, move the new
		 * window to where the old one is.
		 */
		if (XGetGeometry (dpy, Tmp_win->icon_w, &JunkRoot, &icon_x,
		  &icon_y, &JunkWidth, &JunkHeight, &JunkBW, &JunkDepth)) {
		    /*
		     * Move the new icon window to where the old one was.
		     */
		    XMoveWindow(dpy, Tmp_win->wmhints->icon_window, icon_x,
		      icon_y);
		}

		/*
		 * If the window is iconic, map the new icon window.
		 */
		if (Tmp_win->icon)
		    XMapWindow(dpy, Tmp_win->wmhints->icon_window);

		/*
		 * Now, if the old window isn't ours, unmap it, otherwise
		 * just get rid of it completely.
		 */
		if (Tmp_win->icon_not_ours) {
		    if (Tmp_win->icon_w != Tmp_win->wmhints->icon_window)
			XUnmapWindow(dpy, Tmp_win->icon_w);
		} else
		    XDestroyWindow(dpy, Tmp_win->icon_w);

		/*
		 * The new icon window isn't our window, so note that fact
		 * so that we don't treat it as ours.
		 */
		Tmp_win->icon_not_ours = TRUE;

		/*
		 * Now make the new window the icon window for this window,
		 * and set it up to work as such (select for key presses
		 * and button presses/releases, set up the contexts for it,
		 * and define the cursor for it).
		 */
		Tmp_win->icon_w = Tmp_win->wmhints->icon_window;
		XSelectInput (dpy, Tmp_win->icon_w,
		  KeyPressMask | ButtonPressMask | ButtonReleaseMask);
		XSaveContext(dpy, Tmp_win->icon_w, TwmContext, (caddr_t)Tmp_win);
		XSaveContext(dpy, Tmp_win->icon_w, ScreenContext, (caddr_t)Scr);
		XDefineCursor(dpy, Tmp_win->icon_w, Scr->IconCursor);
	    }
	}

	if (Tmp_win->icon_w && !Tmp_win->forced && Tmp_win->wmhints &&
	    (Tmp_win->wmhints->flags & IconPixmapHint)) {
	    if (!XGetGeometry (dpy, Tmp_win->wmhints->icon_pixmap, &JunkRoot,
			       &JunkX, &JunkY, (unsigned int *)&Tmp_win->icon_width,
			       (unsigned int *)&Tmp_win->icon_height, &JunkBW, &JunkDepth)) {
		return;
	    }

	    pm = XCreatePixmap (dpy, Scr->Root, Tmp_win->icon_width,
				Tmp_win->icon_height, Scr->d_depth);
	    if (!pm) return;

	    FB(Tmp_win->iconc.fore, Tmp_win->iconc.back);

/*
 * adapted from CTWM-3.5 - djhjr - 9/4/98
 */
#ifdef ORIGINAL_PIXMAPS
	    XCopyPlane(dpy, Tmp_win->wmhints->icon_pixmap, pm,
		Scr->NormalGC,
		0,0, Tmp_win->icon_width, Tmp_win->icon_height, 0, 0, 1 );
#else
	    if (JunkDepth == Scr->d_depth)
			XCopyArea  (dpy, Tmp_win->wmhints->icon_pixmap, pm, Scr->NormalGC,
					0,0, Tmp_win->icon_width, Tmp_win->icon_height, 0, 0);
	    else
			XCopyPlane(dpy, Tmp_win->wmhints->icon_pixmap, pm, Scr->NormalGC,
					0,0, Tmp_win->icon_width, Tmp_win->icon_height, 0, 0, 1 );
#endif

	    valuemask = CWBackPixmap;
	    attributes.background_pixmap = pm;

	    if (Tmp_win->icon_bm_w)
		XDestroyWindow(dpy, Tmp_win->icon_bm_w);

	    Tmp_win->icon_bm_w =
	      XCreateWindow (dpy, Tmp_win->icon_w, 0, 0,
			     (unsigned int) Tmp_win->icon_width,
			     (unsigned int) Tmp_win->icon_height,
			     (unsigned int) 0, Scr->d_depth,
			     (unsigned int) CopyFromParent, Scr->d_visual,
			     valuemask, &attributes);

/*
 * adapted from CTWM-3.5 - djhjr - 9/4/98
 */
#ifndef ORIGINAL_PIXMAPS
	    if (! (Tmp_win->wmhints->flags & IconMaskHint)) {
			XRectangle rect;

			rect.x = rect.y = 0;
			rect.width  = Tmp_win->icon_width;
			rect.height = Tmp_win->icon_height;
			XShapeCombineRectangles (dpy, Tmp_win->icon_w, ShapeBounding,
					0, 0, &rect, 1, ShapeUnion, 0);
	    }
#endif

	    XFreePixmap (dpy, pm);
	    RedoIconName();
	}

/*
 * adapted from CTWM-3.5 - djhjr - 9/4/98
 */
#ifndef ORIGINAL_PIXMAPS
	if (Tmp_win->icon_w && !Tmp_win->forced && Tmp_win->wmhints &&
	    (Tmp_win->wmhints->flags & IconMaskHint)) {
	    GC gc;

	    if (!XGetGeometry (dpy, Tmp_win->wmhints->icon_mask, &JunkRoot,
			       &JunkX, &JunkY, &JunkWidth, &JunkHeight, &JunkBW,
			       &JunkDepth)) {
		return;
	    }
	    if (JunkDepth != 1) return;

	    pm = XCreatePixmap (dpy, Scr->Root, JunkWidth, JunkHeight, 1);
	    if (!pm) return;

	    gc = XCreateGC (dpy, pm, 0, NULL);
	    if (!gc) return;

	    XCopyArea (dpy, Tmp_win->wmhints->icon_mask, pm, gc,
		       0, 0, JunkWidth, JunkHeight, 0, 0);
	    XFreeGC (dpy, gc);

	    XFreePixmap (dpy, pm);
		RedoIconName();
	}
#endif

	break;

	  case XA_WM_NORMAL_HINTS:
	GetWindowSizeHints (Tmp_win);
	break;

	  default:
	if (Event.xproperty.atom == _XA_WM_COLORMAP_WINDOWS) {
	    FetchWmColormapWindows (Tmp_win);	/* frees old data */
	    break;
	} else if (Event.xproperty.atom == _XA_WM_PROTOCOLS) {
	    FetchWmProtocols (Tmp_win);
	    break;
	}
	break;
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	RedoIconName - procedure to re-position the icon window and name
 *
 ***********************************************************************
 */

void RedoIconName()
{
	int x, y;

	if (Tmp_win->list)
	{
	/* let the expose event cause the repaint */
	XClearArea(dpy, Tmp_win->list->w, 0,0,0,0, True);

	if (Scr->SortIconMgr)
	    SortIconManager(Tmp_win->list->iconmgr);
	}

	if (Scr->Virtual &&
 	Scr->NamesInVirtualDesktop &&
 	Tmp_win->VirtualDesktopDisplayWindow)
 	    XClearArea(dpy, Tmp_win->VirtualDesktopDisplayWindow,
 		       0, 0, 0, 0, True);

	if ( ! Tmp_win->icon_w ) return;

	if (Tmp_win->icon_not_ours)
	return;

/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	Tmp_win->icon_w_width = MyFont_TextWidth(&Scr->IconFont,
#else
	Tmp_win->icon_w_width = XTextWidth(Scr->IconFont.font,
#endif
			Tmp_win->icon_name, strlen(Tmp_win->icon_name));

/* djhjr - 6/11/96
	Tmp_win->icon_w_width += 6;
	if (Tmp_win->icon_w_width < Tmp_win->icon_width)
	{
	Tmp_win->icon_x = (Tmp_win->icon_width - Tmp_win->icon_w_width)/2;
	Tmp_win->icon_x += 3;
	Tmp_win->icon_w_width = Tmp_win->icon_width;
	}
	else
	{
	Tmp_win->icon_x = 3;
	}
*/
    Tmp_win->icon_w_width += 8;
    if (Tmp_win->icon_w_width < Tmp_win->icon_width + 8)
    {
		Tmp_win->icon_x = (((Tmp_win->icon_width + 8) - Tmp_win->icon_w_width)/2) + 4;
		Tmp_win->icon_w_width = Tmp_win->icon_width + 8;
    }
    else
		Tmp_win->icon_x = 4;

	if (Tmp_win->icon_w_width == Tmp_win->icon_width)
	x = 0;
	else
	x = (Tmp_win->icon_w_width - Tmp_win->icon_width)/2;

/* djhjr - 6/11/96
	y = 0;
*/
	y = 4;

/* djhjr - 6/11/96
	Tmp_win->icon_w_height = Tmp_win->icon_height + Scr->IconFont.height + 4;
	Tmp_win->icon_y = Tmp_win->icon_height + Scr->IconFont.height;
*/
	Tmp_win->icon_w_height = Tmp_win->icon_height + Scr->IconFont.height + 8;
	Tmp_win->icon_y = Tmp_win->icon_height + Scr->IconFont.height + 2;

	XResizeWindow(dpy, Tmp_win->icon_w, Tmp_win->icon_w_width,
	Tmp_win->icon_w_height);
	if (Tmp_win->icon_bm_w)
	{
	XMoveWindow(dpy, Tmp_win->icon_bm_w, x, y);
	XMapWindow(dpy, Tmp_win->icon_bm_w);
	}
	if (Tmp_win->icon)
	{
	XClearArea(dpy, Tmp_win->icon_w, 0, 0, 0, 0, True);
	}
}

/*
 * RedoDoorName - Redraw the contents of a door's window
 *
 * djhjr - 2/10/99 2/28/99
 */
void
RedoDoorName(twin, door)
TwmWindow *twin;
TwmDoor *door;
{
	TwmWindow *tmp_win;

	/* font was font.font->fid - djhjr - 9/14/03 */
	FBF(door->colors.fore, door->colors.back, Scr->DoorFont);

	/* find it's twm window to get the current width, etc. */
/*
 * The TWM window is passed from Do*Resize(),
 * as it may be undeterminable in HandleExpose()!?
 *
 * djhjr - 2/28/99
 *
	if (XFindContext(dpy, Event.xany.window, TwmContext,
			(caddr_t *)&tmp_win) != XCNOENT)
*/
	if (twin)
		tmp_win = twin;
	else
		XFindContext(dpy, Event.xany.window, TwmContext, (caddr_t *)&tmp_win);

	if (tmp_win)
	{
		int tw, bw;

/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		tw = MyFont_TextWidth(&Scr->DoorFont,
#else
		tw = XTextWidth(Scr->DoorFont.font,
#endif
				door->name, strlen(door->name));

		/* djhjr - 4/26/96 */
/* djhjr - 8/11/98
		* was 'Scr->use3Dborders' - djhjr - 8/11/98 *
		bw = (Scr->BorderBevelWidth > 0) ? Scr->ThreeDBorderWidth : 0;
*/
		bw = (Scr->BorderBevelWidth > 0) ? Scr->BorderWidth : 0;

		/* change the little internal one to fit the external */
		XResizeWindow(dpy, door->w,
			  tmp_win->frame_width,
			  tmp_win->frame_height);

		/* draw the text in the right place */
/* And it IS the right place.
** If your font has its characters starting 20 pixels
** over to the right, it just looks wrong!
** For example grog-9 from ISC's X11R3 distribution.
*/
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		MyFont_DrawString(dpy, door->w, &Scr->DoorFont,
#else
		XDrawString(dpy, door->w,
#endif
			Scr->NormalGC,
/* gets 'SIZE_VINDENT' out of here... djhjr - 5/14/96
			(tmp_win->frame_width - tw)/2,
			tmp_win->frame_height - SIZE_VINDENT -
			(tmp_win->frame_height - Scr->DoorFont.height)/2,
** ...and NOW it's in the right place! */
			(tmp_win->frame_width - tw - 2 * bw) / 2,
			(tmp_win->frame_height - tmp_win->title_height -
					Scr->DoorFont.height - 2 * bw) / 2 +
/* djhjr - 9/14/03
					Scr->DoorFont.font->ascent,
*/
					Scr->DoorFont.ascent,
			door->name, strlen(door->name));

		/* djhjr - 2/7/99 */
		if (Scr->DoorBevelWidth > 0)
			Draw3DBorder(door->w, 0, 0, tmp_win->frame_width - (bw * 2),
					tmp_win->frame_height - (bw * 2),
					Scr->DoorBevelWidth, Scr->DoorC, off, False, False);
	} else {
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		MyFont_DrawString(dpy, door->w, &Scr->DoorFont,
#else
		XDrawString(dpy, door->w,
#endif
			Scr->NormalGC,
			SIZE_HINDENT/2, 0/*Scr->DoorFont.height*/,
			door->name, strlen(door->name));
	}
}

/*
 * RedoListWindow - Redraw the contents of an icon manager's entry
 *
 * djhjr - 3/1/99
 */
void
RedoListWindow(twin)
TwmWindow *twin;
{
/* djhjr - 4/19/96
	* font was font.font->fid - djhjr - 9/14/03 *
	FBF(twin->list->fore, twin->list->back, Scr->IconManagerFont);
* djhjr - 9/14/03 *
#ifndef NO_I18N_SUPPORT
	MyFont_DrawString (dpy, Event.xany.window, &Scr->IconManagerFont,
#else
	XDrawString (dpy, Event.xany.window,
#endif
		Scr->NormalGC,
		iconmgr_textx, Scr->IconManagerFont.y+4,
		twin->icon_name, strlen(twin->icon_name));
	DrawIconManagerBorder(twin->list);
*/
	/* made static - djhjr - 6/18/99 */
	static int en = 0, dots = 0;

	/* djhjr - 3/29/98 */
	int i, j, slen = strlen(twin->icon_name);
	char *a = NULL;

	/* djhjr - 10/2/01 */
	if (!twin->list) return;

	/*
	 * clip the title a couple of characters less than the width of the
	 * icon window plus padding, and tack on ellipses - this is a little
	 * different than the titlebar's...
	 *
	 * djhjr - 3/29/98
	 */
	if (Scr->NoPrettyTitles == FALSE) /* for rader - djhjr - 2/9/99 */
	{
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		i = MyFont_TextWidth(&Scr->IconManagerFont,
#else
		i = XTextWidth(Scr->IconManagerFont.font,
#endif
				twin->icon_name, slen);

/* DUH! - djhjr - 6/18/99
		j = twin->list->width - iconmgr_textx - en;
*/
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		if (!en) en = MyFont_TextWidth(&Scr->IconManagerFont, "n", 1);
		if (!dots) dots = MyFont_TextWidth(&Scr->IconManagerFont, "...", 3);
#else
		if (!en) en = XTextWidth(Scr->IconManagerFont.font, "n", 1);
		if (!dots) dots = XTextWidth(Scr->IconManagerFont.font, "...", 3);
#endif
		j = twin->list->width - iconmgr_textx - dots;

		/* djhjr - 5/5/98 */
		/* was 'Scr->use3Diconmanagers' - djhjr - 8/11/98 */
		if (Scr->IconMgrBevelWidth > 0)
			j -= Scr->IconMgrBevelWidth;
		else
			j -= Scr->BorderWidth;

/* djhjr - 6/18/99
		if (2 * en >= j)
*/
		if (en >= j)
			slen = 0;
		else if (i >= j)
		{
			for (i = slen; i >= 0; i--)

/* djhjr - 6/18/99
				if (XTextWidth(Scr->IconManagerFont.font, twin->icon_name, i) + 2 * en < j)
*/
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
				if (MyFont_TextWidth(&Scr->IconManagerFont,
#else
				if (XTextWidth(Scr->IconManagerFont.font,
#endif
						twin->icon_name, i) + en < j)
				{
					slen = i;
					break;
				}

			a = (char *)malloc(slen + 4);
			memcpy(a, twin->icon_name, slen);
			strcpy(a + slen, "...");
			slen += 3;
		}
	}

	/* font was font.font->fid - djhjr - 9/14/03 */
	FBF(twin->list->cp.fore, twin->list->cp.back, Scr->IconManagerFont);

/* what's the point of this? - djhjr - 5/2/98
	if (Scr->use3Diconmanagers && (Scr->Monochrome != COLOR))
* djhjr - 9/14/03 *
#ifndef NO_I18N_SUPPORT
		MyFont_DrawImageString (dpy, twin->list->w,
				&Scr->IconManagerFont,
#else
		XDrawImageString (dpy, twin->list->w,
#endif
		Scr->NormalGC, iconmgr_textx,

* djhjr - 5/2/98
		Scr->IconManagerFont.y+4,
*
		(twin->list->height - Scr->IconManagerFont.height) / 2 +
			Scr->IconManagerFont.y,

		(a) ? a : twin->icon_name, slen);
	else
*/
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		MyFont_DrawString (dpy, twin->list->w,
				&Scr->IconManagerFont,
#else
		XDrawString (dpy, twin->list->w,
#endif
		Scr->NormalGC, iconmgr_textx,

/* djhjr - 5/2/98
		Scr->IconManagerFont.y+4,
*/
		(twin->list->height - Scr->IconManagerFont.height) / 2 +
			Scr->IconManagerFont.y,

		(a) ? a : twin->icon_name, slen);

	/* free the clipped title - djhjr - 3/29/98 */
	if (a) free(a);

	DrawIconManagerBorder(twin->list, False);
}


/***********************************************************************
 *
 *  Procedure:
 *	HandleClientMessage - client message event handler
 *
 ***********************************************************************
 */

void
HandleClientMessage()
{
	extern void RestartVtwm();

	if (Event.xclient.message_type == _XA_WM_CHANGE_STATE)
	{
		if (Tmp_win != NULL)
		{
			if (Event.xclient.data.l[0] == IconicState && !Tmp_win->icon)
			{
			XEvent button;

			XQueryPointer( dpy, Scr->Root, &JunkRoot, &JunkChild,
					  &(button.xmotion.x_root),
					  &(button.xmotion.y_root),
					  &JunkX, &JunkY, &JunkMask);

			ExecuteFunction(F_ICONIFY, NULLSTR, Event.xany.window,
				Tmp_win, &button, FRAME, FALSE);
			XUngrabPointer(dpy, CurrentTime);
			}
		}
	}
	/* djhjr - 7/31/98 */
	else if (Event.xclient.message_type == _XA_TWM_RESTART)
		RestartVtwm(CurrentTime);
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleExpose - expose event handler
 *
 ***********************************************************************
 */

static void flush_expose();

void
HandleExpose()
{
	MenuRoot *tmp;
	TwmDoor *door = NULL;
	int j;

	if (XFindContext(dpy, Event.xany.window, MenuContext, (caddr_t *)&tmp) == 0)
	{
		PaintMenu(tmp, &Event);
		return;
	}

	if (XFindContext(dpy, Event.xany.window, DoorContext, (caddr_t *)&door) != XCNOENT)
	{
		/* see also resize.c - djhjr - 2/28/99 */
		RedoDoorName(NULL, door);
		flush_expose(Event.xany.window);
		return;
	}

	if (Event.xexpose.count != 0)
	return;

	if (Event.xany.window == Scr->InfoWindow && InfoLines)
	{
	int i, k;
	int height;

	/* font was font.font->fid - djhjr - 9/14/03 */
	FBF(Scr->DefaultC.fore, Scr->DefaultC.back, Scr->InfoFont);

	/* djhjr - 5/10/96 */
	XGetGeometry (dpy, Scr->InfoWindow, &JunkRoot, &JunkX, &JunkY,
				&JunkWidth, &JunkHeight, &JunkBW, &JunkDepth);

	height = Scr->InfoFont.height+2;
	for (i = 0; i < InfoLines; i++)
	{
		/* djhjr - 5/10/96 */
		j = strlen(Info[i]);

		/* djhjr - 4/29/98 */
		k = 5;
		/* was 'Scr->use3Dborders' - djhjr - 8/11/98 */
		if (!i && Scr->BorderBevelWidth > 0) k += Scr->InfoBevelWidth;

/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
	    MyFont_DrawString(dpy, Scr->InfoWindow, &Scr->InfoFont,
#else
	    XDrawString(dpy, Scr->InfoWindow,
#endif
		Scr->NormalGC,
/* centers the lines... djhjr - 5/10/96
		10,
*/
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
		(JunkWidth - MyFont_TextWidth(&Scr->InfoFont, Info[i], j)) / 2,
#else
		(JunkWidth - XTextWidth(Scr->InfoFont.font, Info[i], j)) / 2,
#endif

		/* 'k' was a hard-coded '5' - djhjr - 4/29/98 */
		(i*height) + Scr->InfoFont.y + k, Info[i], j);
	}

	/* djhjr - 5/9/96 */
	/* was 'Scr->use3Dborders' - djhjr - 8/11/98 */
	if (Scr->InfoBevelWidth > 0)
	    Draw3DBorder(Scr->InfoWindow, 0, 0, JunkWidth, JunkHeight,
/* djhjr - 4/29/98
				BW, Scr->DefaultC, off, False, False);
*/
				Scr->InfoBevelWidth, Scr->DefaultC, off, False, False);

	flush_expose (Event.xany.window);
	}

	/* see that the desktop's bevel gets redrawn - djhjr - 2/10/99 */
	else if (Event.xany.window == Scr->VirtualDesktopDisplay)
	{
		Draw3DBorder(Scr->VirtualDesktopDisplayOuter, 0, 0,
				Scr->VirtualDesktopMaxWidth + (Scr->VirtualDesktopBevelWidth * 2),
				Scr->VirtualDesktopMaxHeight + (Scr->VirtualDesktopBevelWidth * 2),
				Scr->VirtualDesktopBevelWidth, Scr->VirtualC, off, False, False);
		flush_expose (Event.xany.window);
		return;
	}

	else if (Tmp_win != NULL)
	{
	/* djhjr - 4/20/96 */
	/* was 'Scr->use3Dborders' - djhjr - 8/11/98 */
	if (Scr->BorderBevelWidth > 0 && (Event.xany.window == Tmp_win->frame)) {
	    PaintBorders (Tmp_win, ((Tmp_win == Scr->Focus) ? True : False));
	    flush_expose (Event.xany.window);
	    return;
	}
	else

	if (Event.xany.window == Tmp_win->title_w)
	{
/* djhjr - 4/20/96
	    * font was font.font->fid - djhjr - 9/14/03 *
	    FBF(Tmp_win->title.fore, Tmp_win->title.back, Scr->TitleBarFont);

* djhjr - 9/14/03 *
#ifndef NO_I18N_SUPPORT
	    MyFont_DrawString (dpy, Tmp_win->title_w, &Scr->TitleBarFont,
#else
	    XDrawString (dpy, Tmp_win->title_w,
#endif
			 Scr->NormalGC,
			 Scr->TBInfo.titlex, Scr->TitleBarFont.y,
			 Tmp_win->name, strlen(Tmp_win->name));
*/
		PaintTitle (Tmp_win);

		/* djhjr - 10/25/02 */
		PaintTitleHighlight(Tmp_win, (Tmp_win == Scr->Focus) ? on : off);

	    flush_expose (Event.xany.window);
		return;
	}
	else if (Event.xany.window == Tmp_win->icon_w)
	{

/* djhjr - 4/21/96
	    * font was font.font->fid - djhjr - 9/14/03 *
	    FBF(Tmp_win->iconc.fore, Tmp_win->iconc.back, Scr->IconFont);

* djhjr - 9/14/03 *
#ifndef NO_I18N_SUPPORT
	    MyFont_DrawString (dpy, Tmp_win->icon_w, &Scr->IconManagerFont,
#else
	    XDrawString (dpy, Tmp_win->icon_w,
#endif
		Scr->NormalGC,
		Tmp_win->icon_x, Tmp_win->icon_y,
		Tmp_win->icon_name, strlen(Tmp_win->icon_name));
*/
		PaintIcon(Tmp_win);

	    flush_expose (Event.xany.window);
	    return;
	} else if (Tmp_win->titlebuttons) {
	    int i;
	    Window w = Event.xany.window;
	    TBWindow *tbw;
	    int nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;

	    for (i = 0, tbw = Tmp_win->titlebuttons; i < nb; i++, tbw++) {
			if (w == tbw->window) {
/* djhjr - 4/19/96
			    register TitleButton *tb = tbw->info;

			    FB(Tmp_win->title.fore, Tmp_win->title.back);
			    XCopyPlane (dpy, tb->bitmap, w, Scr->NormalGC,
					tb->srcx, tb->srcy, tb->width, tb->height,
					tb->dstx, tb->dsty, 1);
*/
				/* djhjr - 11/17/97 8/10/98 */
				/* added the test for window highlighting - djhjr - 3/14/98 */
				/* collapsed two functions - djhjr - 8/10/98 */
				if (Scr->ButtonColorIsFrame && Tmp_win->highlight)
					PaintTitleButton(Tmp_win, tbw, (Scr->Focus == Tmp_win) ? 2 : 1);
				else
					PaintTitleButton(Tmp_win, tbw, 0);

			    flush_expose (w);
		    	return;
			}
	    }
	}
	if (Tmp_win->list) {
	    if (Event.xany.window == Tmp_win->list->w)
	    {
			/* see also resize.c - djhjr - 3/1/99 */
			RedoListWindow(Tmp_win);
			flush_expose (Event.xany.window);
			return;
	    }
	    if (Event.xany.window == Tmp_win->list->icon)
	    {
/* djhjr - 4/19/96
		FB(Tmp_win->list->fore, Tmp_win->list->back);
		XCopyPlane(dpy, Scr->siconifyPm, Tmp_win->list->icon,
		    Scr->NormalGC,
		    0,0, iconifybox_width, iconifybox_height, 0, 0, 1);
*/
/* djhjr - 10/30/02
		* was 'Scr->use3Diconmanagers' - djhjr - 8/11/98 *
		if (Scr->IconMgrBevelWidth > 0 && Tmp_win->list->iconifypm)
		    XCopyArea (dpy, Tmp_win->list->iconifypm, Tmp_win->list->icon,
				Scr->NormalGC, 0, 0,
				iconifybox_width, iconifybox_height, 0, 0);
		else {
		    FB(Tmp_win->list->cp.fore, Tmp_win->list->cp.back);
		    XCopyPlane(dpy, Scr->siconifyPm->pixmap, Tmp_win->list->icon, Scr->NormalGC,
			0,0, iconifybox_width, iconifybox_height, 0, 0, 1);
		}
*/
		XCopyArea(dpy, Tmp_win->list->iconifypm->pixmap,
			  Tmp_win->list->icon, Scr->NormalGC, 0, 0,
			  iconifybox_width, iconifybox_height, 0, 0);

		flush_expose (Event.xany.window);
		return;
	    }
	}
	}

	/* update the virtual desktop display names */
	if (Scr->Virtual && Scr->NamesInVirtualDesktop) {
	    TwmWindow *tmp_win;
	    char *name = NULL;

	    if (XFindContext(dpy, Event.xany.window, VirtualContext,
			     (caddr_t *)&tmp_win) != XCNOENT) {
		    /* font was font.font->fid - djhjr - 9/14/03 */
		    FBF(tmp_win->virtual.fore, tmp_win->virtual.back,
			Scr->VirtualFont);
		    if (tmp_win->icon_name)
			    name = tmp_win->icon_name;
		    else if (tmp_win->name)
			    name = tmp_win->name;
		    if (name)
/* djhjr - 9/14/03 */
#ifndef NO_I18N_SUPPORT
			    MyFont_DrawImageString(dpy, Event.xany.window,
					     &Scr->VirtualFont,
#else
			    XDrawImageString(dpy, Event.xany.window,
#endif
					     Scr->NormalGC,
					     0, Scr->VirtualFont.height,
					     name, strlen(name));
	    }
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleDestroyNotify - DestroyNotify event handler
 *
 ***********************************************************************
 */

void
HandleDestroyNotify()
{
	int i;

	/*
	 * Warning, this is also called by HandleUnmapNotify; if it ever needs to
	 * look at the event, HandleUnmapNotify will have to mash the UnmapNotify
	 * into a DestroyNotify.
	 */

	if (Tmp_win == NULL)
	return;

/* djhjr - 6/22/01 */
#ifndef NO_SOUND_SUPPORT
	if (destroySoundFromFunction == FALSE)
		PlaySound(S_CUNMAP);
	else
		destroySoundFromFunction = FALSE;
#endif

	if (Tmp_win == Scr->Focus)
	{
	FocusOnRoot();
	}

	if (Tmp_win == Scr->Newest) /* PF */
		Scr->Newest = NULL; /* PF */

	/* djhjr - 5/16/98 */
	if (Tmp_win == UnHighLight_win) UnHighLight_win = NULL;

	XDeleteContext(dpy, Tmp_win->w, TwmContext);
	XDeleteContext(dpy, Tmp_win->w, ScreenContext);
	XDeleteContext(dpy, Tmp_win->frame, TwmContext);
	XDeleteContext(dpy, Tmp_win->frame, ScreenContext);
	XDeleteContext(dpy, Tmp_win->VirtualDesktopDisplayWindow, VirtualContext);
	if (Tmp_win->icon_w)
	{
	XDeleteContext(dpy, Tmp_win->icon_w, TwmContext);
	XDeleteContext(dpy, Tmp_win->icon_w, ScreenContext);
	}
	if (Tmp_win->title_height)
	{
	int nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;
	XDeleteContext(dpy, Tmp_win->title_w, TwmContext);
	XDeleteContext(dpy, Tmp_win->title_w, ScreenContext);
	if (Tmp_win->hilite_w)
	{
	    XDeleteContext(dpy, Tmp_win->hilite_w, TwmContext);
	    XDeleteContext(dpy, Tmp_win->hilite_w, ScreenContext);
	}
	if (Tmp_win->titlebuttons) {
	    for (i = 0; i < nb; i++) {
		XDeleteContext (dpy, Tmp_win->titlebuttons[i].window,
				TwmContext);
		XDeleteContext (dpy, Tmp_win->titlebuttons[i].window,
				ScreenContext);
	    }
	    }
	}

	if (Scr->cmapInfo.cmaps == &Tmp_win->cmaps)
	InstallWindowColormaps(DestroyNotify, &Scr->TwmRoot);

	/*
	 * TwmWindows contain the following pointers
	 *
	 *     1.  full_name
	 *     2.  name
	 *     3.  icon_name
	 *     4.  wmhints
	 *     5.  class.res_name
	 *     6.  class.res_class
	 *     7.  list
	 *     8.  iconmgrp
	 *     9.  cwins
	 *     10. titlebuttons
	 *     11. window ring
	 *     12. virtual desktop display window
	 */
	if (Tmp_win->gray) XFreePixmap (dpy, Tmp_win->gray);

	/* djhjr - 4/26/99 */
	AppletDown(Tmp_win);

	XDestroyWindow(dpy, Tmp_win->frame);
	if (Tmp_win->icon_w && !Tmp_win->icon_not_ours) {
	XDestroyWindow(dpy, Tmp_win->icon_w);
	IconDown (Tmp_win);
	}
	XDestroyWindow(dpy, Tmp_win->VirtualDesktopDisplayWindow);	/* 12 */
	RemoveIconManager(Tmp_win);					/* 7 */
	Tmp_win->prev->next = Tmp_win->next;
	if (Tmp_win->next != NULL)
	Tmp_win->next->prev = Tmp_win->prev;
	if (Tmp_win->auto_raise) Scr->NumAutoRaises--;

	free_window_names (Tmp_win, True, True, True);		/* 1, 2, 3 */
	if (Tmp_win->wmhints)					/* 4 */
	  XFree ((char *)Tmp_win->wmhints);
	if (Tmp_win->class.res_name && Tmp_win->class.res_name != NoName)  /* 5 */
	  XFree ((char *)Tmp_win->class.res_name);
	if (Tmp_win->class.res_class && Tmp_win->class.res_class != NoName) /* 6 */
	  XFree ((char *)Tmp_win->class.res_class);
	free_cwins (Tmp_win);				/* 9 */
	if (Tmp_win->titlebuttons)					/* 10 */
	  free ((char *) Tmp_win->titlebuttons);
	/*
	 * 11a through 11c was handled in a local function, but
	 * is now broken out (11a & 11b), and uses a public function
	 * in menus.c (11c) - djhjr - 10/27/02
	 */
	if (enter_win == Tmp_win) {			/* 11a */
	  enter_flag = FALSE;
	  enter_win = NULL;
	}
	if (raise_win == Tmp_win) raise_win = NULL;	/* 11b */
	RemoveWindowFromRing(Tmp_win);			/* 11c */

	free((char *)Tmp_win);
}



void
HandleCreateNotify()
{
#ifdef DEBUG_EVENTS
	fprintf(stderr, "CreateNotify w = 0x%x\n", Event.xcreatewindow.window);
	fflush(stderr);
	XBell(dpy, 0);
	XSync(dpy, 0);
#endif
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleMapRequest - MapRequest event handler
 *
 ***********************************************************************
 */

void
HandleMapRequest()
{

	int stat;
	int zoom_save;

	Event.xany.window = Event.xmaprequest.window;
	stat = XFindContext(dpy, Event.xany.window, TwmContext, (caddr_t *)&Tmp_win);
	if (stat == XCNOENT)
	Tmp_win = NULL;

	/* If the window has never been mapped before ... */
	if (Tmp_win == NULL)
	{
	/* Add decorations. */
	Tmp_win = AddWindow(Event.xany.window, FALSE, (IconMgr *) NULL);
	if (Tmp_win == NULL)
	    return;

/* djhjr - 6/22/01 */
#ifndef NO_SOUND_SUPPORT
	if (createSoundFromFunction == FALSE)
		PlaySound(S_CMAP);
	else
		createSoundFromFunction = FALSE;
#endif

	}
	else
	{
	/*
	 * If the window has been unmapped by the client, it won't be listed
	 * in the icon manager.  Add it again, if requested.
	 */
	if (Tmp_win->list == NULL)
	    (void) AddIconManager (Tmp_win);
	}

	/* If it's not merely iconified, and we have hints, use them. */
	if ((! Tmp_win->icon) &&
	Tmp_win->wmhints && (Tmp_win->wmhints->flags & StateHint))
	{
	int state;
	Window icon;

	/* use WM_STATE if enabled */
	if (!(RestartPreviousState && GetWMState(Tmp_win->w, &state, &icon) &&
	      (state == NormalState || state == IconicState)))
	  state = Tmp_win->wmhints->initial_state;

	switch (state)
	{
	    case DontCareState:
	    case NormalState:
	    case ZoomState:
	    case InactiveState:
		XMapWindow(dpy, Tmp_win->w);
		XMapWindow(dpy, Tmp_win->frame);
		SetMapStateProp(Tmp_win, NormalState);
		SetRaiseWindow (Tmp_win);

		/* djhjr - 10/2/01 */
		if (Scr->StrictIconManager)
		    if (Tmp_win->list)
			RemoveIconManager(Tmp_win);

		break;

	    case IconicState:
		zoom_save = Scr->DoZoom;
		Scr->DoZoom = FALSE;
		Iconify(Tmp_win, 0, 0);
		Scr->DoZoom = zoom_save;
		break;
	}
	}
	/* If no hints, or currently an icon, just "deiconify" */
	else
	{
	DeIconify(Tmp_win);
	SetRaiseWindow (Tmp_win);
	}

	RaiseStickyAbove(); /* DSE */
	RaiseAutoPan(); /* DSE */

}



void SimulateMapRequest (w)
	Window w;
{
	Event.xmaprequest.window = w;
	HandleMapRequest ();
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleMapNotify - MapNotify event handler
 *
 ***********************************************************************
 */

void
HandleMapNotify()
{
	if (Tmp_win == NULL)
	return;

	/*
	 * Need to do the grab to avoid race condition of having server send
	 * MapNotify to client before the frame gets mapped; this is bad because
	 * the client would think that the window has a chance of being viewable
	 * when it really isn't.
	 */
	XGrabServer (dpy);
	if (Tmp_win->icon_w)
	XUnmapWindow(dpy, Tmp_win->icon_w);
	if (Tmp_win->title_w)
	XMapSubwindows(dpy, Tmp_win->title_w);
	XMapSubwindows(dpy, Tmp_win->frame);

/* djhjr - 4/25/96
	if (Scr->Focus != Tmp_win && Tmp_win->hilite_w)
	XUnmapWindow(dpy, Tmp_win->hilite_w);
*/
	if (Scr->Focus != Tmp_win)
		PaintTitleHighlight(Tmp_win, off);

	XMapWindow(dpy, Tmp_win->frame);
	XUngrabServer (dpy);
	XFlush (dpy);
	Tmp_win->mapped = TRUE;
	Tmp_win->icon = FALSE;
	Tmp_win->icon_on = FALSE;

	/* Race condition if in menus.c:DeIconify() - djhjr - 10/2/01 */
	if (Scr->StrictIconManager)
	    if (Tmp_win->list)
		RemoveIconManager(Tmp_win);
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleUnmapNotify - UnmapNotify event handler
 *
 ***********************************************************************
 */

void
HandleUnmapNotify()
{
	int dstx, dsty;
	Window dumwin;

	/*
	 * The July 27, 1988 ICCCM spec states that a client wishing to switch
	 * to WithdrawnState should send a synthetic UnmapNotify with the
	 * event field set to (pseudo-)root, in case the window is already
	 * unmapped (which is the case for twm for IconicState).  Unfortunately,
	 * we looked for the TwmContext using that field, so try the window
	 * field also.
	 */
	if (Tmp_win == NULL)
	{
	Event.xany.window = Event.xunmap.window;
	if (XFindContext(dpy, Event.xany.window,
	    TwmContext, (caddr_t *)&Tmp_win) == XCNOENT)
	    Tmp_win = NULL;
	}

	if (Tmp_win == NULL || (!Tmp_win->mapped && !Tmp_win->icon))
	return;

	/*
	 * The program may have unmapped the client window, from either
	 * NormalState or IconicState.  Handle the transition to WithdrawnState.
	 *
	 * We need to reparent the window back to the root (so that twm exiting
	 * won't cause it to get mapped) and then throw away all state (pretend
	 * that we've received a DestroyNotify).
	 */

	XGrabServer (dpy);
	if (XTranslateCoordinates (dpy, Event.xunmap.window, Tmp_win->attr.root,
			       0, 0, &dstx, &dsty, &dumwin)) {
	XEvent ev;
	Bool reparented = XCheckTypedWindowEvent (dpy, Event.xunmap.window,
						  ReparentNotify, &ev);
	SetMapStateProp (Tmp_win, WithdrawnState);
	if (reparented) {
	    if (Tmp_win->old_bw) XSetWindowBorderWidth (dpy,
							Event.xunmap.window,
							Tmp_win->old_bw);
	    if (Tmp_win->wmhints && (Tmp_win->wmhints->flags & IconWindowHint))
	      XUnmapWindow (dpy, Tmp_win->wmhints->icon_window);
	} else {
	    XReparentWindow (dpy, Event.xunmap.window, Tmp_win->attr.root,
			     dstx, dsty);
	    RestoreWithdrawnLocation (Tmp_win);
	}
	XRemoveFromSaveSet (dpy, Event.xunmap.window);
	XSelectInput (dpy, Event.xunmap.window, NoEventMask);
	HandleDestroyNotify ();		/* do not need to mash event before */
	} /* else window no longer exists and we'll get a destroy notify */
	XUngrabServer (dpy);
	XFlush (dpy);
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleMotionNotify - MotionNotify event handler
 *
 ***********************************************************************
 */

#if 0 /* functionality moved to menus.c:ExecuteFunction() - djhjr - 11/7/03 */
void
HandleMotionNotify()
{
#if 0 /* done in menus.c:ExecuteFunction() now - djhjr - 11/4/03 */
	if (moving_window) {
	    DoMoveWindowOnDesktop(Event.xmotion.x, Event.xmotion.y);
	}
#endif

#if 0 /* done in menus.c:ExecuteFunction() now - djhjr - 5/27/03 */
	if ( ResizeWindow )
	{
	XQueryPointer( dpy, Event.xany.window,
	    &(Event.xmotion.root), &JunkChild,
	    &(Event.xmotion.x_root), &(Event.xmotion.y_root),
	    &(Event.xmotion.x), &(Event.xmotion.y),
	    &JunkMask);

	/* Set WindowMoved appropriately so that f.deltastop will
	   work with resize as well as move. */
	if (abs (Event.xmotion.x - ResizeOrigX) >= Scr->MoveDelta
	    || abs (Event.xmotion.y - ResizeOrigY) >= Scr->MoveDelta)
	{
	  /* djhjr - 9/5/98 */
	  resizing_window = 1;

	  WindowMoved = TRUE;
	}

	/* added this 'if ()' for applying MoveDelta - djhjr - 9/5/98 */
	if (resizing_window)
	{
		XFindContext(dpy, ResizeWindow, TwmContext, (caddr_t *)&Tmp_win);
		DoResize(Event.xmotion.x_root, Event.xmotion.y_root, Tmp_win);
	}
	}
#endif
}
#endif



/***********************************************************************
 *
 *  Procedure:
 *	HandleButtonRelease - ButtonRelease event handler
 *
 ***********************************************************************
 */
void
HandleButtonRelease()
{
/* djhjr - 10/6/02
	int xl, xr, yt, yb, w, h;
*/
	unsigned mask;

	if (Scr->StayUpMenus)
	{
	    if (GlobalFirstTime == True && GlobalMenuButton == True )
	    {
	        ButtonPressed = -1;
	        GlobalFirstTime = False;
	        return;
	    } /* end if  */

	    GlobalFirstTime = True;
	} /* end if  */

#if 0
0 For StayUpMenus, delete infobox after buttonpress!
0	if (InfoLines) 		/* delete info box on 2nd button release  */
0	  /* if (Context == C_IDENTIFY)  */
0		/* This would force you to click on the box itself */
0	  {
0fprintf( stderr, "Kill info B\n" );
0	XUnmapWindow(dpy, Scr->InfoWindow);
0	InfoLines = 0;
0	Context = C_NO_CONTEXT;
0	  }
#endif

#if 0 /* done in menus.c:ExecuteFunction() now - djhjr - 11/4/03 */
	if (moving_window)
	{	EndMoveWindowOnDesktop();
	}
#endif

	if (DragWindow != None)
	{
/*
 * Most all of this is redundant (see menus.c:ExecuteFunction()),
 * and I don't see why. Everything except local functionality is
 * '#if 0'd out, with just a few lines moved (copied) to menus.c.
 * djhjr - 10/6/02
 */
#if 0
		MoveOutline(Scr->Root, 0, 0, 0, 0, 0, 0);

		XFindContext(dpy, DragWindow, TwmContext, (caddr_t *)&Tmp_win);
		if (DragWindow == Tmp_win->frame)
		{
			xl = Event.xbutton.x_root - DragX - Tmp_win->frame_bw;
			yt = Event.xbutton.y_root - DragY - Tmp_win->frame_bw;
			w = DragWidth + 2 * Tmp_win->frame_bw;
			h = DragHeight + 2 * Tmp_win->frame_bw;
		}
		else
		{
/*
 * Deskset/Openwin apps change the icon's border width attribute.
 * Submitted by Caveh Frank Jalali
 *
			xl = Event.xbutton.x_root - DragX - Scr->IconBorderWidth;
			yt = Event.xbutton.y_root - DragY - Scr->IconBorderWidth;
			w = DragWidth + 2 * Scr->IconBorderWidth;
			h = DragHeight + 2 * Scr->IconBorderWidth;
*/
 			XWindowAttributes wat;
 
			XGetWindowAttributes(dpy, DragWindow, &wat);
			xl = Event.xbutton.x_root - DragX - wat.border_width;
			yt = Event.xbutton.y_root - DragY - wat.border_width;
			w = DragWidth + 2 * wat.border_width;
			h = DragHeight + 2 * wat.border_width;
		}

		if (ConstMove)
		{
			if (ConstMoveDir == MOVE_HORIZ)
			yt = ConstMoveY;

			if (ConstMoveDir == MOVE_VERT)
			xl = ConstMoveX;

			if (ConstMoveDir == MOVE_NONE)
			{
			yt = ConstMoveY;
			xl = ConstMoveX;
			}
		}

		if (Scr->DontMoveOff && MoveFunction != F_FORCEMOVE)
		{
			xr = xl + w;
			yb = yt + h;

			if (xl < 0)
			xl = 0;
			if (xr > Scr->MyDisplayWidth)
			xl = Scr->MyDisplayWidth - w;

			if (yt < 0)
			yt = 0;
			if (yb > Scr->MyDisplayHeight)
			yt = Scr->MyDisplayHeight - h;
		}

		CurrentDragX = xl;
		CurrentDragY = yt;
		if (DragWindow == Tmp_win->frame)
		  SetupWindow (Tmp_win, xl, yt,
			       Tmp_win->frame_width, Tmp_win->frame_height, -1);
		else
			XMoveWindow (dpy, DragWindow, xl, yt);

/* djhjr - 4/7/98
		if (!Scr->NoRaiseMove && !Scr->OpaqueMove)    * opaque already did *
			XRaiseWindow(dpy, DragWindow);
*/
		if (!Scr->NoRaiseMove)
			/* opaque already did, so test the individual window, methinks */
			if (DragWindow == Tmp_win->frame)
			{
				if (!Tmp_win->opaque_move)
					XRaiseWindow(dpy, DragWindow);
			}
			else if (!Scr->OpaqueMove)
				XRaiseWindow(dpy, DragWindow);

		RaiseStickyAbove(); /* DSE */
		RaiseAutoPan();

		if (!Scr->OpaqueMove)
			UninstallRootColormap();
		else
			XSync(dpy, 0);

		if (Scr->NumAutoRaises) {
			enter_flag = TRUE;
			enter_win = NULL;
			raise_win = ((DragWindow == Tmp_win->frame && !Scr->NoRaiseMove)
				 ? Tmp_win : NULL);
		}
#endif

		DragWindow = None;
		ConstMove = FALSE;
	}

#ifdef NEVER /* djhjr - 5/27/03 */
	if ( ResizeWindow )
	{
		EndResize();
	}
#endif

	if ( ActiveMenu && RootFunction == F_NOFUNCTION )
	{
		if ( ActiveItem )
		{
			int func = ActiveItem->func;
			Action = ActiveItem->action;
			switch (func)
			{	case F_MOVE:
				case F_FORCEMOVE:
				ButtonPressed = -1;
				break;
#if (0)
0			case F_IDENTIFY:
0	      case F_CIRCLEUP:
0	      case F_CIRCLEDOWN:
0	      case F_REFRESH:
0	      case F_WARPTOSCREEN:
0		case F_AUTOPAN: /*RFB */
0		case F_SNAPREALSCREEN:/*RFB*/
0		PopDownMenu();
0		break;
#endif
				default:
					break;
			}
			ExecuteFunction(func, Action,
				ButtonWindow ? ButtonWindow->frame : None,
				ButtonWindow, &Event/*&ButtonEvent*/, Context, TRUE);
			Context = C_NO_CONTEXT;
			ButtonWindow = NULL;

/* djhjr - 9/15/99
			* if we are not executing a defered command, then take down the
			 * menu
			 *
			if (RootFunction == F_NOFUNCTION)
			{
				PopDownMenu();
			}
*/
		}
/* djhjr - 9/15/99
		else
*/
			PopDownMenu();
	}

	mask = (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask);
	switch (Event.xbutton.button)
	{
		case Button1: mask &= ~Button1Mask; break;
		case Button2: mask &= ~Button2Mask; break;
		case Button3: mask &= ~Button3Mask; break;
		case Button4: mask &= ~Button4Mask; break;
		case Button5: mask &= ~Button5Mask; break;
	}

	if (RootFunction != F_NOFUNCTION ||
	ResizeWindow != None ||
	moving_window != None ||
	DragWindow != None)
	{	ButtonPressed = -1;
	}

	if (RootFunction == F_NOFUNCTION &&
	(Event.xbutton.state & mask) == 0 &&
	DragWindow == None &&
	moving_window == None &&
	ResizeWindow == None)
	{
		XUngrabPointer(dpy, CurrentTime);
		XUngrabServer(dpy);
		XFlush(dpy);
		EventHandler[EnterNotify] = HandleEnterNotify;
		EventHandler[LeaveNotify] = HandleLeaveNotify;
	menuFromFrameOrWindowOrTitlebar = FALSE;
		ButtonPressed = -1;
		if (DownIconManager)
		{
			DownIconManager->down = FALSE;

/* djhjr - 4/19/96
			if (Scr->Highlight) DrawIconManagerBorder(DownIconManager);
*/
			if (Scr->Highlight) DrawIconManagerBorder(DownIconManager, False);

			DownIconManager = NULL;
		}
		Cancel = FALSE;
	}
}



static void do_menu (menu, wnd)
	MenuRoot *menu;			/* menu to pop up */
	Window wnd;				/* invoking window or None */
{
	int x = Event.xbutton.x_root;
	int y = Event.xbutton.y_root;
	Bool center = True;

	if (Scr->StayUpMenus)
	{	GlobalMenuButton = True;
	}

	if (!Scr->NoGrabServer)
	XGrabServer(dpy);
	if (wnd) {
	Window child;
	/* djhjr - 1/20/98 */
	int w = Scr->TBInfo.width / 2;
/* djhjr - 1/20/98
	int h = Scr->TBInfo.width - Scr->TBInfo.border;
*/
	int h = Scr->TBInfo.width;

/* djhjr - 1/20/98
	(void) XTranslateCoordinates (dpy, w, Scr->Root, 0, h, &x, &y, &child);
*/
	(void) XTranslateCoordinates (dpy, wnd, Scr->Root, w, h, &x, &y, &child);

/* djhjr - 1/20/98
	* djhjr - 3/12/97 *
	y -= Scr->TitleHeight;
*/
	y -= Scr->TitleHeight / 2;

/* djhjr - 1/20/98
	center = False;
*/
	}
	if (PopUpMenu (menu, x, y, center)) {
	UpdateMenu();
	} else {
	DoAudible(); /* was 'XBell()' - djhjr - 6/22/01 */
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleButtonPress - ButtonPress event handler
 *
 ***********************************************************************
 */
void
HandleButtonPress()
{
	unsigned int modifier;
	Cursor cur;
	TwmDoor *door = NULL;

	/* Submitted by Jennifer Elaan */
	if (Event.xbutton.button > MAX_BUTTONS)
		return;

	if (Scr->StayUpMenus)
	{
		/* added '&& ButtonPressed == -1' - Submitted by Steve Ratcliffe */
		if (GlobalFirstTime == False && GlobalMenuButton == True
				&& ButtonPressed == -1)
		{
			return;
		}
	}
	else
	{	/* pop down the menu, if any */
		if (ActiveMenu != NULL) PopDownMenu();
	}

	if ( InfoLines ) 	/* StayUpMenus */
	{
/* djhjr - 6/22/01 */
#ifndef NO_SOUND_SUPPORT
		PlaySound(S_IUNMAP);
#endif

	  	XUnmapWindow(dpy, Scr->InfoWindow);
		InfoLines = 0;
	}

	XSync(dpy, 0);			/* XXX - remove? */

	if (ButtonPressed != -1
	&& !InfoLines /* want menus if we have info box */
	)
	{	/* we got another butt press in addition to one still held
		* down, we need to cancel the operation we were doing
		*/
		Cancel = TRUE;
	    if (DragWindow != None)
	    {
		CurrentDragX = origDragX;
		CurrentDragY = origDragY;
		if (!menuFromFrameOrWindowOrTitlebar)
		{
			/* added this 'if ... else' - djhjr - 4/7/98 */
			if (Tmp_win && DragWindow == Tmp_win->frame && Tmp_win->opaque_move)
				XMoveWindow (dpy, DragWindow, origDragX, origDragY);
			else
			if (Scr->OpaqueMove && DragWindow != None)
				XMoveWindow (dpy, DragWindow, origDragX, origDragY);
			else
				MoveOutline(Scr->Root, 0, 0, 0, 0, 0, 0);
		}
		if (!Scr->OpaqueMove) UninstallRootColormap();
	    }

#if 0 /* done in menus.c:ExecuteFunction() now - djhjr - 11/4/03 */
	    /* this 'else if ...' - djhjr - 11/3/03 */
	    else if (moving_window)
		EndMoveWindowOnDesktop();
#endif

		XUnmapWindow(dpy, Scr->SizeWindow);
		ResizeWindow = None;
		DragWindow = None;
		cur = LeftButt;
		if (Event.xbutton.button == Button2) cur = MiddleButt;
		else if (Event.xbutton.button >= Button3) cur = RightButt;

		XGrabPointer(dpy, Scr->Root, True,
			ButtonReleaseMask | ButtonPressMask,
			GrabModeAsync, GrabModeAsync,
			Scr->Root, cur, CurrentTime);
		return;
	}
	else
	{	ButtonPressed = Event.xbutton.button;
	}

	if ( ResizeWindow != None
	|| DragWindow != None
	|| moving_window != None
	/* ||ActiveMenu != NULL ** tvtwm StayUpMenus */
	)
	{
		return;
	}

	if ( ButtonPressed == Button1 && Tmp_win && Tmp_win->title_height && Tmp_win->titlebuttons )
	{	/* check the title bar buttons */
		register int i;
		register TBWindow *tbw;
		int nb = Scr->TBInfo.nleft + Scr->TBInfo.nright;

		for (i = 0, tbw = Tmp_win->titlebuttons; i < nb; i++, tbw++)
		{
			if (Event.xany.window == tbw->window)
			{
				if (tbw->info->func == F_MENU)
				{
					Context = C_TITLE;
					ButtonEvent = Event;
					ButtonWindow = Tmp_win;
					do_menu (tbw->info->menuroot, tbw->window);
				}
				else
				{
					/* djhjr - 9/15/99 */
					Context = C_TITLE;

					ExecuteFunction (tbw->info->func, tbw->info->action,
						Event.xany.window, Tmp_win, &Event, C_TITLE, FALSE);

					/*
					 * For some reason, we don't get the button up event.
					 * Submitted by Caveh Frank Jalali
					 */
					ButtonPressed = -1;
				}

				return;
			}
		}
	}

	Context = C_NO_CONTEXT;
	if ( Event.xany.window == Scr->InfoWindow ) Context = C_IDENTIFY;
	if ( Event.xany.window == Scr->Root ) Context = C_ROOT;

/* djhjr - 9/12/96 - moved to the bottom of this context decision chain...
	if
	(	Context == C_NO_CONTEXT
		&&
		(	Tmp_win == Scr->VirtualDesktopDisplayTwin
			||
			Event.xany.window == Scr->VirtualDesktopDisplayOuter
			||
			Event.xany.window == Scr->VirtualDesktopDisplay
		)
	)
	{	TwmWindow *tmp_win;

		if ( Event.xbutton.subwindow
		&& XFindContext( dpy, Event.xbutton.subwindow, VirtualContext,
			(caddr_t *) &tmp_win )
				!= XCNOENT
		)
		{	* Click in a little window in the panner. *
			Tmp_win = tmp_win;
			Context = C_VIRTUAL_WIN;
		}
		else
		{	* Click in the panner. *
			Tmp_win = Scr->VirtualDesktopDisplayTwin;
			Context = C_VIRTUAL;
		}
	}
*/

	if (XFindContext(dpy, Event.xany.window,
		DoorContext, (caddr_t *)&door) != XCNOENT)
			Context = C_DOOR;

	if ( Tmp_win && Context == C_NO_CONTEXT )
	{
/* have I really determined that this isn't needed? - djhjr - 9/15/99
		if
		(	Tmp_win->list
			&&
			RootFunction != F_NOFUNCTION
			&&
			(	Event.xany.window == Tmp_win->list->w
				||
				Event.xany.window == Tmp_win->list->icon
			)
		)
		{
			Tmp_win = Tmp_win->list->iconmgr->twm_win;
			XTranslateCoordinates(dpy, Event.xany.window, Tmp_win->w,
				Event.xbutton.x, Event.xbutton.y,
				&JunkX, &JunkY, &JunkChild);

* djhjr - 4/21/96
			Event.xbutton.x = JunkX;
			Event.xbutton.y = JunkY - Tmp_win->title_height;
*
			Event.xbutton.x = JunkX - Tmp_win->frame_bw3D;
			Event.xbutton.y = JunkY - Tmp_win->title_height - Tmp_win->frame_bw3D;

			Event.xany.window = Tmp_win->w;
			Context = C_WINDOW;
		}
		else
*/
		if ( Event.xany.window == Tmp_win->title_w )
		{
			Context = C_TITLE;
		}
		else if (Event.xany.window == Tmp_win->w)
		{
			printf("ERROR! ERROR! ERROR! YOU SHOULD NOT BE HERE!!!\n");
			Context = C_WINDOW;
		}
		else if (Event.xany.window == Tmp_win->icon_w)
		{
			Context = C_ICON;
		}
		else if (Event.xany.window == Tmp_win->frame)
		{	/* since we now place a button grab on the frame instead
			* of the window, (see GrabButtons() in add_window.c), we
			* need to figure out where the pointer exactly is before
			* assigning Context.  If the pointer is on the application
			* window we will change the event structure to look as if
			* it came from the application window.
			*/
			if (Event.xbutton.subwindow == Tmp_win->w)
			{	Event.xbutton.window = Tmp_win->w;

/* djhjr - 4/21/96
				Event.xbutton.y -= Tmp_win->title_height;
*/
				Event.xbutton.x -= Tmp_win->frame_bw3D;
				Event.xbutton.y -= (Tmp_win->title_height + Tmp_win->frame_bw3D);

				/*****
				Event.xbutton.x -= Tmp_win->frame_bw;
				*****/
				Context = C_WINDOW;
			}

/* not needed after all - djhjr - 9/10/99
			* djhjr - 5/13/99 *
			else if (Scr->Doors)
			{
				for (door = Scr->Doors; door != NULL; door = door->next)
					if (door->twin->frame == Tmp_win->frame)
					{
						Context = C_DOOR;

						break;
					}

				if (!door) Context = C_FRAME;
			}
*/

			else Context = C_FRAME;
		}
		else if
		(	Tmp_win->list
			&&
			(	Event.xany.window == Tmp_win->list->w
				||
				Event.xany.window == Tmp_win->list->icon
			)
		)
		{
			Tmp_win->list->down = TRUE;

/* djhjr - 4/19/96
			if (Scr->Highlight) DrawIconManagerBorder(Tmp_win->list);
*/
			if (Scr->Highlight) DrawIconManagerBorder(Tmp_win->list, False);

			DownIconManager = Tmp_win->list;
			Context = C_ICONMGR;
		}
	}

/* djhjr - 9/12/96 - moved from the top of this context decision chain...*/
	if
	(	Context == C_NO_CONTEXT
		&&
		(	Tmp_win == Scr->VirtualDesktopDisplayTwin
			||
			Event.xany.window == Scr->VirtualDesktopDisplayOuter
			||
			Event.xany.window == Scr->VirtualDesktopDisplay
		)
	)
	{	TwmWindow *tmp_win;

		if ( Event.xbutton.subwindow
		&& XFindContext( dpy, Event.xbutton.subwindow, VirtualContext,
			(caddr_t *) &tmp_win )
				!= XCNOENT
		)
		{	/* Click in a little window in the panner. */
			Tmp_win = tmp_win;
			Context = C_VIRTUAL_WIN;
		}
		else
		{	/* Click in the panner. */
			Tmp_win = Scr->VirtualDesktopDisplayTwin;
			Context = C_VIRTUAL;
		}
	}

	/* this section of code checks to see if we were in the middle of
	* a command executed from a menu
	*/
	if (RootFunction != F_NOFUNCTION)
	{
		if (Event.xany.window == Scr->Root)
		{
			/* if the window was the Root, we don't know for sure it
			* it was the root.  We must check to see if it happened to be
			* inside of a client that was getting button press events.
			*/
			XTranslateCoordinates(dpy, Scr->Root, Scr->Root,
				Event.xbutton.x,
				Event.xbutton.y,
				&JunkX, &JunkY, &Event.xany.window);

			if (Event.xany.window == 0 ||
					XFindContext(dpy, Event.xany.window, TwmContext,
							(caddr_t *)&Tmp_win) == XCNOENT)
			{
				RootFunction = F_NOFUNCTION;
				DoAudible(); /* was 'XBell()' - djhjr - 6/22/01 */

				/*
				 * If stay up menus is set, then the menu may still be active
				 * and should be popped down - Submitted by Steve Ratcliffe
				 */
				if (ActiveMenu != NULL)
					PopDownMenu();

				return;
			}

			XTranslateCoordinates(dpy, Scr->Root, Event.xany.window,
				Event.xbutton.x,
				Event.xbutton.y,
				&JunkX, &JunkY, &JunkChild);

			Event.xbutton.x = JunkX;
			Event.xbutton.y = JunkY;
			Context = C_WINDOW;
		}

		/* make sure we are not trying to move an identify window */
		if (Scr->InfoWindow && Event.xany.window != Scr->InfoWindow)
		{
			ExecuteFunction(RootFunction, Action, Event.xany.window,
				Tmp_win, &Event, Context, FALSE);
			if (Scr->StayUpMenus)
			{	/* pop down the menu, if any */
				if (ActiveMenu != NULL) PopDownMenu();
			}
		}

		RootFunction = F_NOFUNCTION;
		return;
	}

	ButtonEvent = Event;
	ButtonWindow = Tmp_win;

	/* if we get to here, we have to execute a function or pop up a
	* menu
	*/
	modifier = (Event.xbutton.state & mods_used);

	if (Context == C_NO_CONTEXT) return;

	RootFunction = F_NOFUNCTION;
	if (Scr->Mouse[Event.xbutton.button][Context][modifier].func == F_MENU)
	{
		do_menu (Scr->Mouse[Event.xbutton.button][Context][modifier].menu,
		(Window) None);
		if (Scr->StayUpMenus)
		{
			GlobalMenuButton = False;
		}
	}
	else if (Scr->Mouse[Event.xbutton.button][Context][modifier].func != F_NOFUNCTION)
	{
		Action = Scr->Mouse
			[Event.xbutton.button][Context][modifier].item
				? Scr->Mouse
					[Event.xbutton.button][Context][modifier]
						.item->action
				: NULL;
		ExecuteFunction( Scr->Mouse
			[Event.xbutton.button][Context][modifier].func,
			Action, Event.xany.window, Tmp_win, &Event, Context, FALSE);
	}
	else if (Scr->DefaultFunction.func != F_NOFUNCTION)
	{
		if (Scr->DefaultFunction.func == F_MENU)
		{
			do_menu (Scr->DefaultFunction.menu, (Window) None);
		}
		else
		{
			Action = Scr->DefaultFunction.item
				? Scr->DefaultFunction.item->action
				: NULL;
			ExecuteFunction(Scr->DefaultFunction.func, Action,
				Event.xany.window, Tmp_win, &Event, Context, FALSE);
		}
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	HENQueueScanner - EnterNotify event q scanner
 *
 *	Looks at the queued events and determines if any matching
 *	LeaveNotify events or EnterEvents deriving from the
 *	termination of a grab are behind this event to allow
 *	skipping of unnecessary processing.
 *
 ***********************************************************************
 */

typedef struct HENScanArgs {
	Window w;		/* Window we are currently entering */
	Bool leaves;	/* Any LeaveNotifies found for this window */
	Bool inferior;	/* Was NotifyInferior the mode for LeaveNotify */
	Bool enters;	/* Any EnterNotify events with NotifyUngrab */
} HENScanArgs;

/* ARGSUSED*/
static Bool
HENQueueScanner(dpy, ev, args)
	Display *dpy;
	XEvent *ev;
	char *args;
{
	if (ev->type == LeaveNotify) {
	if (ev->xcrossing.window == ((HENScanArgs *) args)->w &&
		ev->xcrossing.mode == NotifyNormal) {
		((HENScanArgs *) args)->leaves = True;
		/*
		 * Only the last event found matters for the Inferior field.
		 */
		((HENScanArgs *) args)->inferior =
		(ev->xcrossing.detail == NotifyInferior);
	}
	} else if (ev->type == EnterNotify) {
	if (ev->xcrossing.mode == NotifyUngrab)
		((HENScanArgs *) args)->enters = True;
	}

	return (False);
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleEnterNotify - EnterNotify event handler
 *
 ***********************************************************************
 */

void
HandleEnterNotify()
{
	MenuRoot *mr;
	XEnterWindowEvent *ewp = &Event.xcrossing;
	HENScanArgs scanArgs;
	XEvent dummy;
	short l;
	extern int RaiseDelay;/*RAISEDELAY*/

	/*
	 * Save the id of the window entered.  This will be used to remove
	 * border highlight on entering the next application window.
	 */
	if (UnHighLight_win && ewp->window != UnHighLight_win->w) {
	  SetBorder (UnHighLight_win, False);	/* application window */
	  if (UnHighLight_win->list) /* in the icon box */
	NotActiveIconManager(UnHighLight_win->list);
	}
	if (ewp->window == Scr->Root)
	  UnHighLight_win = NULL;
	else if (Tmp_win)
	  UnHighLight_win = Tmp_win;

	/*
	 * if we aren't in the middle of menu processing
	 */
	if (!ActiveMenu) {
	/*
	 * We're not interested in pseudo Enter/Leave events generated
	 * from grab initiations.
	 */
	if (ewp->mode == NotifyGrab)
		return;

	/*
	 * Scan for Leave and Enter Notify events to see if we can avoid some
	 * unnecessary processing.
	 */
	scanArgs.w = ewp->window;
	scanArgs.leaves = scanArgs.enters = False;
	(void) XCheckIfEvent(dpy, &dummy, HENQueueScanner, (char *) &scanArgs);

  	/*
	 * if it is one of the autopan windows, do the pan
	 */
	if ( Scr->AutoPanX )/*RFB F_AUTOPAN*/
	for (l = 0; l <= 3; l++)
		if (ewp->window == Scr->VirtualDesktopAutoPan[l])
		{
			int xdiff, ydiff, xwarp, ywarp;

			/*
			 * Code from FVWM-1.23b, modified to reflect "real time"
			 * values of the resource.
			 *
			 * djhjr - 9/8/98
			 */
			if (Scr->VirtualDesktopPanResistance > 0 &&
					Scr->VirtualDesktopPanResistance < 10000)
			{
				int x, y, i;
				static struct timeval timeoutval = {0, 12500};
				struct timeval timeout;

				/* The granularity of PanResistance is about 25 ms.
				 * The timeout variable is set to 12.5 ms since we
				 * pass this way twice each time an autopan window
				 * is entered.
				 */
				for (i = 25; i < Scr->VirtualDesktopPanResistance; i += 25)
				{
					timeout = timeoutval;
					select(0, 0, 0, 0, &timeout);

					scanArgs.w = ewp->window;
					scanArgs.leaves = scanArgs.enters = False;
					(void)XCheckIfEvent(dpy, &dummy, HENQueueScanner,
							(char *)&scanArgs);

					if (scanArgs.leaves)
						return;
				}

				XQueryPointer(dpy, Scr->Root, &JunkRoot, &JunkChild,
						&x, &y, &JunkX, &JunkY, &JunkMask);

				if (x < Scr->AutoPanBorderWidth)
					l = 0;
				else if (x >= Scr->MyDisplayWidth - Scr->AutoPanBorderWidth)
					l = 1;
				else if (y < Scr->AutoPanBorderWidth)
					l = 2;
				else if (y >= Scr->MyDisplayHeight - Scr->AutoPanBorderWidth)
					l = 3;
				else
					l = 4; /* oops */
			}

			/* figure out which one it is */
			switch (l)
			{
				case 0: /* left */
					xdiff = -(Scr->AutoPanX);
					ydiff = 0;
					/* xwarp = AP_SIZE + 2; */
					xwarp = AP_SIZE + Scr->AutoPanExtraWarp; /* DSE */
					ywarp = 0;
					break;
				case 1: /* right */
					xdiff = Scr->AutoPanX;
					ydiff = 0;
					/* xwarp = -(AP_SIZE + 2); */
					xwarp = -(AP_SIZE + Scr->AutoPanExtraWarp); /* DSE */
					ywarp = 0;
					break;
				case 2: /* up */
					xdiff = 0;
					ydiff = -(Scr->AutoPanY);
					xwarp = 0;
					/* ywarp = AP_SIZE + 2; */
					ywarp = AP_SIZE + Scr->AutoPanExtraWarp; /* DSE */
					break;
				case 3: /* down */
					xdiff = 0;
					ydiff = Scr->AutoPanY;
					xwarp = 0;
					/* ywarp = -(AP_SIZE + 2); */
					ywarp = -(AP_SIZE + Scr->AutoPanExtraWarp); /* DSE */
					break;
				default: /* oops */
					/* this is to stop the compiler complaining */
					xdiff = ydiff = xwarp = ywarp = 0;
/* not with the PanResistance resource! - djhjr - 9/8/98
					fprintf(stderr, "vtwm: major problems with autopan\n");
*/
			}

/* djhjr - 6/22/01 */
#ifndef NO_SOUND_SUPPORT
			PlaySound(S_APAN);
#endif

			/* do the pan */
			PanRealScreen(xdiff, ydiff, &xwarp, &ywarp); /* DSE */

			/*
			 * warp the pointer out of the window so that they can keep
			 * moving the mouse
			 */
			XWarpPointer(dpy, None, None, 0, 0, 0, 0, xwarp, ywarp);

			return;
		} /* end if ewp->window = autopan */

	/*
	 * if entering root window, restore twm default colormap so that
	 * titlebars are legible
	 */
	if (ewp->window == Scr->Root) {
		if (!scanArgs.leaves && !scanArgs.enters)
		InstallWindowColormaps(EnterNotify, &Scr->TwmRoot);
		return;
	}

/*RAISEDELAY*/ 	/* Handle RaiseDelay, if any..... */
/*RAISEDELAY*/ 	if (RaiseDelay > 0) {
/*RAISEDELAY*/ 	    if (Tmp_win && Tmp_win->auto_raise
/*RAISEDELAY*/ 		&& (!Tmp_win->list || Tmp_win->list->w != ewp->window)) {
/*RAISEDELAY*/ 		ColormapWindow *cwin;

#ifdef NEVER
/*RAISEDELAY*/ 		static struct timeval timeout = {0,12500};
#else
/*
 * Submitted by Steve Ratcliffe
 */
/*RAISEDELAY*/ 		static struct timeval timeoutval = {0,12500};
/*RAISEDELAY*/ 		struct timeval timeout;
#endif

/*RAISEDELAY*/
/*RAISEDELAY*/ 		if (XFindContext(dpy, Tmp_win->w, ColormapContext,
/*RAISEDELAY*/ 				 (caddr_t *)&cwin) == XCNOENT) {
/*RAISEDELAY*/ 		    cwin = (ColormapWindow *)NULL;
/*RAISEDELAY*/ 		}
/*RAISEDELAY*/
/*RAISEDELAY*/ 		if ((ewp->detail != NotifyInferior
/*RAISEDELAY*/ 		     || Tmp_win->frame == ewp->window)
/*RAISEDELAY*/ 		     && (!cwin || cwin->visibility != VisibilityUnobscured)) {
/*RAISEDELAY*/ 		    int x, y, px, py, d, i;
/*RAISEDELAY*/ 		    Window w;
/*RAISEDELAY*/
/*RAISEDELAY*/ 		    XQueryPointer(dpy, Scr->Root, &w, &w, &px, &py,
/*RAISEDELAY*/ 				  &d, &d, (unsigned int *)&d);
/*RAISEDELAY*/
/*RAISEDELAY*/ 		    /* The granularity of RaiseDelay is about 25 ms.
						* The timeout variable is set to 12.5 ms since we
						* pass this way twice each time a twm window is
						* entered.
						*/
/*RAISEDELAY*/ 		    for (i = 25; i < RaiseDelay; i += 25) {

/*
 * Submitted by Steve Ratcliffe
 */
/*RAISEDELAY*/ 			/* The timeout needs initialising each time on Linux */
/*RAISEDELAY*/ 			timeout = timeoutval;

/*RAISEDELAY*/ 			select(0, 0, 0, 0, &timeout);
/*RAISEDELAY*/ 			/* Did we leave this window already? */
/*RAISEDELAY*/ 			scanArgs.w = ewp->window;
/*RAISEDELAY*/ 			scanArgs.leaves = scanArgs.enters = False;
/*RAISEDELAY*/ 			(void) XCheckIfEvent(dpy, &dummy, HENQueueScanner,
/*RAISEDELAY*/ 					     (char *) &scanArgs);
/*RAISEDELAY*/ 			if (scanArgs.leaves && !scanArgs.inferior) return;
/*RAISEDELAY*/
/*RAISEDELAY*/ 			XQueryPointer(dpy, Scr->Root, &w, &w, &x, &y,
/*RAISEDELAY*/ 				      &d, &d, (unsigned int *)&d);
/*RAISEDELAY*/
/*RAISEDELAY*/ 			/* Has the pointer moved?  If so reset the loop cnt.
						* We want the pointer to be still for RaiseDelay
						* milliseconds before terminating the loop
						*/
/*RAISEDELAY*/ 			if (x != px || y != py) {
/*RAISEDELAY*/ 			    i = 0; px = x; py = y;
/*RAISEDELAY*/ 			}
/*RAISEDELAY*/ 		    }
/*RAISEDELAY*/ 		}
/*RAISEDELAY*/ 	    }
/*RAISEDELAY*/
/*RAISEDELAY*/ 	    /*
					* Scan for Leave and Enter Notify events to see if we can avoid some
					* unnecessary processing.
					*/
/*RAISEDELAY*/ 	    scanArgs.w = ewp->window;
/*RAISEDELAY*/ 	    scanArgs.leaves = scanArgs.enters = False;
/*RAISEDELAY*/ 	    (void) XCheckIfEvent(dpy, &dummy, HENQueueScanner, (char *) &scanArgs);
/*RAISEDELAY*/
/*RAISEDELAY*/ 	    /*
					* if entering root window, restore twm default colormap so that
					* titlebars are legible
					*/
/*RAISEDELAY*/ 	    if (ewp->window == Scr->Root) {
/*RAISEDELAY*/ 		if (!scanArgs.leaves && !scanArgs.enters)
/*RAISEDELAY*/ 		    InstallWindowColormaps(EnterNotify, &Scr->TwmRoot);
/*RAISEDELAY*/ 		return;
/*RAISEDELAY*/ 	    }
/*RAISEDELAY*/ 	}
/*RAISEDELAY*/ 	/* End of RaiseDelay modification. */

	/*
	 * if we have an event for a specific one of our windows
	 */
	if (Tmp_win) {
		/*
		 * If currently in PointerRoot mode (indicated by FocusRoot), then
		 * focus on this window
		 */
		if (Scr->FocusRoot && (!scanArgs.leaves || scanArgs.inferior)) {
		if (Tmp_win->list) ActiveIconManager(Tmp_win->list);

		if (Tmp_win->mapped) {
		    /*
		     * unhighlight old focus window
		     */

/* djhjr - 4/25/96
		    if (Scr->Focus && Scr->Focus != Tmp_win && Tmp_win->hilite_w)
		      XUnmapWindow(dpy, Scr->Focus->hilite_w);
*/
		    if (Scr->Focus && Scr->Focus != Tmp_win)
				PaintTitleHighlight(Scr->Focus, off);

		    /*
		     * If entering the frame or the icon manager, then do
		     * "window activation things":
		     *
		     *     1.  turn on highlight window (if any)
		     *     2.  install frame colormap
		     *     3.  set frame and highlight window (if any) border
		     *     3a. set titlebutton highlight (if button color is frame)
		     *     if IconManagerFocus is set or not in icon mgr
		     *         4.  focus on client window to forward typing
		     *         4a. same as 4 but for icon mgr and/or NoTitlebar set
		     *     5.  send WM_TAKE_FOCUS if requested
		     */
		    if (ewp->window == Tmp_win->frame ||
			(Tmp_win->list && ewp->window == Tmp_win->list->w)) {

/* djhjr - 4/25/96
			if (Tmp_win->hilite_w)				* 1 *
			  XMapWindow (dpy, Tmp_win->hilite_w);
*/
			PaintTitleHighlight(Tmp_win, on);		/* 1 */

			if (!scanArgs.leaves && !scanArgs.enters)	/* 2 */
			    InstallWindowColormaps (EnterNotify,
						    &Scr->TwmRoot);
			SetBorder (Tmp_win, True);			/* 3, 3a */

			/* added this 'if()' - djhjr - 5/27/98 */
			/* added hack for StrictIconManager - djhjr - 10/2/01 */
			/* added test for transients - djhjr - 4/9/02 */
			if (Scr->IconManagerFocus ||
					(Scr->FocusRoot &&
					Scr->StrictIconManager &&
					!Tmp_win->list) ||
					(Tmp_win->list && Tmp_win->list->w &&
					Tmp_win->list->w != ewp->window) ||
					Tmp_win->transient)
			{
			/* added test for transients - djhjr - 4/9/02 */
			if ((((Tmp_win->title_w || Scr->NoTitlebar) &&	/* 4, 4a */
					Scr->TitleFocus) ||
					Tmp_win->transient) &&
					Tmp_win->wmhints &&
					Tmp_win->wmhints->input)
				SetFocus (Tmp_win, ewp->time);
			}

			if (Tmp_win->protocols & DoesWmTakeFocus)	/* 5 */
			  SendTakeFocusMessage (Tmp_win, ewp->time);
			Scr->Focus = Tmp_win;
		    } else if (ewp->window == Tmp_win->w) {
			/*
			 * If we are entering the application window, install
			 * its colormap(s).
			 */
			if (!scanArgs.leaves || scanArgs.inferior)
			    InstallWindowColormaps(EnterNotify, Tmp_win);
		    }
		}			/* end if Tmp_win->mapped */
		if (Tmp_win->wmhints != NULL &&
			ewp->window == Tmp_win->wmhints->icon_window &&
			(!scanArgs.leaves || scanArgs.inferior))
			    InstallWindowColormaps(EnterNotify, Tmp_win);
		}				/* end if FocusRoot */
		/*
		 * If this window is to be autoraised, mark it so
		 */
		if (Tmp_win->auto_raise) {
		enter_win = Tmp_win;
		if (enter_flag == FALSE) AutoRaiseWindow (Tmp_win);
		} else if (enter_flag && raise_win == Tmp_win)
		  enter_win = Tmp_win;
		/*
		 * set ring leader
		 */
		if (Tmp_win->ring.next && (!enter_flag || raise_win == enter_win))
		{
			/*
			 * If this window is an icon manager window, make
			 * the ring leader the icon manager - djhjr - 11/8/01
			 *
			 * Is the icon manager in the ring? - djhjr - 10/27/02
			 */
			if (Tmp_win->list && ewp->window == Tmp_win->list->w &&
					Tmp_win->list->iconmgr->twm_win->ring.next)
			{
				Scr->RingLeader = Tmp_win->list->iconmgr->twm_win;
			}
			else
				Scr->RingLeader = Tmp_win;
		}
		XSync (dpy, 0);
		return;
	}				/* end if Tmp_win */
	}					/* end if !ActiveMenu */

	/*
	 * Find the menu that we are dealing with now; punt if unknown
	 */
	if (XFindContext (dpy, ewp->window, MenuContext, (caddr_t *)&mr) != XCSUCCESS) return;

	mr->entered = TRUE;
/* djhjr - 4/23/96
	if (ActiveMenu && mr == ActiveMenu->prev && RootFunction == F_NOFUNCTION) {
		if (Scr->Shadow) XUnmapWindow (dpy, ActiveMenu->shadow);
		XUnmapWindow (dpy, ActiveMenu->w);
		ActiveMenu->mapped = UNMAPPED;
		UninstallRootColormap ();
		if (ActiveItem) {
			ActiveItem->state = 0;
			PaintEntry (ActiveMenu, ActiveItem,  False);
		}
		ActiveItem = NULL;
		ActiveMenu = mr;
		MenuDepth--;
	}
*/
    if (RootFunction == F_NOFUNCTION) {
		MenuRoot *tmp;
		for (tmp = ActiveMenu; tmp; tmp = tmp->prev) {
	    	if (tmp == mr) break;
		}
		if (! tmp) return;
		for (tmp = ActiveMenu; tmp != mr; tmp = tmp->prev) {
			/* all 'tmp' were 'ActiveMenu'... DUH! - djhjr - 11/16/98 */
			if (Scr->Shadow) XUnmapWindow (dpy, tmp->shadow);
			XUnmapWindow (dpy, tmp->w);
			tmp->mapped = UNMAPPED;
	    	MenuDepth--;
		}
		UninstallRootColormap ();
		if (ActiveItem) {
	    	ActiveItem->state = 0;
	    	PaintEntry (ActiveMenu, ActiveItem,  False);
		}
		ActiveItem = NULL;
		ActiveMenu = mr;
	}

	return;
}



/***********************************************************************
 *
 *  Procedure:
 *	HLNQueueScanner - LeaveNotify event q scanner
 *
 *	Looks at the queued events and determines if any
 *	EnterNotify events are behind this event to allow
 *	skipping of unnecessary processing.
 *
 ***********************************************************************
 */

typedef struct HLNScanArgs {
	Window w;		/* The window getting the LeaveNotify */
	Bool enters;	/* Any EnterNotify event at all */
	Bool matches;	/* Any matching EnterNotify events */
} HLNScanArgs;

/* ARGSUSED*/
static Bool
HLNQueueScanner(dpy, ev, args)
	Display *dpy;
	XEvent *ev;
	char *args;
{
	if (ev->type == EnterNotify && ev->xcrossing.mode != NotifyGrab) {
	((HLNScanArgs *) args)->enters = True;
	if (ev->xcrossing.window == ((HLNScanArgs *) args)->w)
		((HLNScanArgs *) args)->matches = True;
	}

	return (False);
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleLeaveNotify - LeaveNotify event handler
 *
 ***********************************************************************
 */

void
HandleLeaveNotify()
{
	HLNScanArgs scanArgs;
	XEvent dummy;

	if (Tmp_win != NULL)
	{
	Bool inicon;

	/*
	 * We're not interested in pseudo Enter/Leave events generated
	 * from grab initiations and terminations.
	 */
	if (Event.xcrossing.mode != NotifyNormal)
		return;

	inicon = (Tmp_win->list &&
		  Tmp_win->list->w == Event.xcrossing.window);

/*
 * rem'ing this allows the window crossed out of onto the root window
 * to be remembered, so an f.warpring event occuring on the root window
 * will return to that window (see WarpAlongRing() in menus.c).
 *
 * no, I don't fully understand... djhjr - 5/11/98
 *
	if (Scr->RingLeader && Scr->RingLeader == Tmp_win &&
		(Event.xcrossing.detail != NotifyInferior &&
		 Event.xcrossing.window != Tmp_win->w)) {

#ifdef ORIGINAL_WARPRINGCOORDINATES * djhjr - 5/11/98 *
		if (!inicon) {
		if (Tmp_win->mapped) {
		    Tmp_win->ring.cursor_valid = False;
		} else {
		    Tmp_win->ring.cursor_valid = True;
		    Tmp_win->ring.curs_x = (Event.xcrossing.x_root -
					    Tmp_win->frame_x);
		    Tmp_win->ring.curs_y = (Event.xcrossing.y_root -
					    Tmp_win->frame_y);
		}
		}
#endif

		Scr->RingLeader = (TwmWindow *) NULL;
	}
*/

	if (Scr->FocusRoot) {

		if (Event.xcrossing.detail != NotifyInferior) {

		/*
		 * Scan for EnterNotify events to see if we can avoid some
		 * unnecessary processing.
		 */
		scanArgs.w = Event.xcrossing.window;
		scanArgs.enters = scanArgs.matches = False;
		(void) XCheckIfEvent(dpy, &dummy, HLNQueueScanner,
				     (char *) &scanArgs);

		if ((Event.xcrossing.window == Tmp_win->frame &&
			!scanArgs.matches) || inicon) {
		    if (Tmp_win->list) NotActiveIconManager(Tmp_win->list);

/* djhjr - 4/25/96
		    if (Tmp_win->hilite_w)
		      XUnmapWindow (dpy, Tmp_win->hilite_w);
*/
			PaintTitleHighlight(Tmp_win, off);

		    SetBorder (Tmp_win, False);
		    if (Scr->TitleFocus ||
			Tmp_win->protocols & DoesWmTakeFocus)
		      SetFocus ((TwmWindow *) NULL, Event.xcrossing.time);
		    Scr->Focus = NULL;
		} else if (Event.xcrossing.window == Tmp_win->w &&
				!scanArgs.enters) {
		    InstallWindowColormaps (LeaveNotify, &Scr->TwmRoot);
		}
		}
	}
	XSync (dpy, 0);
	return;
	}
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleConfigureRequest - ConfigureRequest event handler
 *
 ***********************************************************************
 */

void
HandleConfigureRequest()
{
	XWindowChanges xwc;
	unsigned long xwcm;
	int x, y, width, height, bw;
	int gravx, gravy;
	XConfigureRequestEvent *cre = &Event.xconfigurerequest;

#ifdef DEBUG_EVENTS
	fprintf(stderr, "ConfigureRequest\n");
	if (cre->value_mask & CWX)
	fprintf(stderr, "  x = %d\n", cre->x);
	if (cre->value_mask & CWY)
	fprintf(stderr, "  y = %d\n", cre->y);
	if (cre->value_mask & CWWidth)
	fprintf(stderr, "  width = %d\n", cre->width);
	if (cre->value_mask & CWHeight)
	fprintf(stderr, "  height = %d\n", cre->height);
	if (cre->value_mask & CWSibling)
	fprintf(stderr, "  above = 0x%x\n", cre->above);
	if (cre->value_mask & CWStackMode)
	fprintf(stderr, "  stack = %d\n", cre->detail);
#endif

	/*
	 * Event.xany.window is Event.xconfigurerequest.parent, so Tmp_win will
	 * be wrong
	 */
	Event.xany.window = cre->window;	/* mash parent field */
	if (XFindContext (dpy, cre->window, TwmContext, (caddr_t *) &Tmp_win) ==
	XCNOENT)
	  Tmp_win = NULL;


	/*
	 * According to the July 27, 1988 ICCCM draft, we should ignore size and
	 * position fields in the WM_NORMAL_HINTS property when we map a window.
	 * Instead, we'll read the current geometry.  Therefore, we should respond
	 * to configuration requests for windows which have never been mapped.
	 */
	if (!Tmp_win || Tmp_win->icon_w == cre->window) {
	xwcm = cre->value_mask &
		(CWX | CWY | CWWidth | CWHeight | CWBorderWidth);
	xwc.x = cre->x;
	xwc.y = cre->y;
	xwc.width = cre->width;
	xwc.height = cre->height;
	xwc.border_width = cre->border_width;
	XConfigureWindow(dpy, Event.xany.window, xwcm, &xwc);
	return;
	}

	if ((cre->value_mask & CWStackMode) && Tmp_win->stackmode) {
	TwmWindow *otherwin;

	xwc.sibling = (((cre->value_mask & CWSibling) &&
			(XFindContext (dpy, cre->above, TwmContext,
				       (caddr_t *) &otherwin) == XCSUCCESS))
		       ? otherwin->frame : cre->above);
	xwc.stack_mode = cre->detail;
	XConfigureWindow (dpy, Tmp_win->frame,
			  cre->value_mask & (CWSibling | CWStackMode), &xwc);
	}


	/* Don't modify frame_XXX fields before calling SetupWindow! */
	x = Tmp_win->frame_x;
	y = Tmp_win->frame_y;
	width = Tmp_win->frame_width;
	height = Tmp_win->frame_height;
	bw = Tmp_win->frame_bw;

	/*
	 * Section 4.1.5 of the ICCCM states that the (x,y) coordinates in the
	 * configure request are for the upper-left outer corner of the window.
	 * This means that we need to adjust for the additional title height as
	 * well as for any border width changes that we decide to allow.  The
	 * current window gravity is to be used in computing the adjustments, just
	 * as when initially locating the window.  Note that if we do decide to
	 * allow border width changes, we will need to send the synthetic
	 * ConfigureNotify event.
	 */
	GetGravityOffsets (Tmp_win, &gravx, &gravy);

	if (cre->value_mask & CWBorderWidth) {
	int bwdelta = cre->border_width - Tmp_win->old_bw;  /* posit growth */
	if (bwdelta && Scr->ClientBorderWidth) {  /* if change allowed */
		x += gravx * bwdelta;	/* change default values only */
		y += gravy * bwdelta;	/* ditto */
		bw = cre->border_width;
		if (Tmp_win->title_height) height += bwdelta;
		x += (gravx < 0) ? bwdelta : -bwdelta;
		y += (gravy < 0) ? bwdelta : -bwdelta;
	}
	Tmp_win->old_bw = cre->border_width;  /* for restoring */
	}

	if (cre->value_mask & CWX) {	/* override even if border change */
	x = cre->x - bw;

	/* djhjr - 4/21/96 */
	x -= ((gravx < 0) ? 0 : Tmp_win->frame_bw3D);

	}
	if (cre->value_mask & CWY) {
	y = cre->y - ((gravy < 0) ? 0 : Tmp_win->title_height) - bw;

	/* djhjr - 4/21/96 */
	y -= ((gravy < 0) ? 0 : Tmp_win->frame_bw3D);

	}

	if (cre->value_mask & CWWidth) {

/* djhjr - 4/21/96
	width = cre->width;
*/
	width = cre->width + 2 * Tmp_win->frame_bw3D;

	}
	if (cre->value_mask & CWHeight) {

/* djhjr - 4/21/96
	height = cre->height + Tmp_win->title_height;
*/
	height = cre->height + Tmp_win->title_height + 2 * Tmp_win->frame_bw3D;

	}

	if (width != Tmp_win->frame_width || height != Tmp_win->frame_height)
	Tmp_win->zoomed = ZOOM_NONE;

	/*
	 * SetupWindow (x,y) are the location of the upper-left outer corner and
	 * are passed directly to XMoveResizeWindow (frame).  The (width,height)
	 * are the inner size of the frame.  The inner width is the same as the
	 * requested client window width; the inner height is the same as the
	 * requested client window height plus any title bar slop.
	 */
/* propogate ConfigureNotify events - submitted by Jonathan Paisley - 11/11/02
	SetupWindow (Tmp_win, x, y, width, height, bw);
*/
	SetupFrame(Tmp_win, x, y, width, height, bw, True);

	/* Change the size of the desktop representation */
	MoveResizeDesktop (Tmp_win, TRUE);

	/*
	 * Raise the autopan windows in case the current window covers them.
	 * Submitted by Steve Ratcliffe
	 */
	RaiseAutoPan();
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleShapeNotify - shape notification event handler
 *
 ***********************************************************************
 */
void
HandleShapeNotify ()
{
	XShapeEvent	    *sev = (XShapeEvent *) &Event;

	if (Tmp_win == NULL)
	return;
	if (sev->kind != ShapeBounding)
	return;
	if (!Tmp_win->wShaped && sev->shaped) {
	XShapeCombineMask (dpy, Tmp_win->frame, ShapeClip, 0, 0, None,
			   ShapeSet);
	}
	Tmp_win->wShaped = sev->shaped;
	SetFrameShape (Tmp_win);
}



/***********************************************************************
 *
 *  Procedure:
 *	HandleUnknown - unknown event handler
 *
 ***********************************************************************
 */

void
HandleUnknown()
{
#ifdef DEBUG_EVENTS
	fprintf(stderr, "type = %d\n", Event.type);
#endif
}



/***********************************************************************
 *
 *  Procedure:
 *	Transient - checks to see if the window is a transient
 *
 *  Returned Value:
 *	TRUE	- window is a transient
 *	FALSE	- window is not a transient
 *
 *  Inputs:
 *	w	- the window to check
 *
 ***********************************************************************
 */

int
Transient(w, propw)
	Window w, *propw;
{
	return (XGetTransientForHint(dpy, w, propw));
}



/***********************************************************************
 *
 *  Procedure:
 *	FindScreenInfo - get ScreenInfo struct associated with a given window
 *
 *  Returned Value:
 *	ScreenInfo struct
 *
 *  Inputs:
 *	w	- the window
 *
 ***********************************************************************
 */

ScreenInfo *
FindScreenInfo(w)
	Window w;
{
	XWindowAttributes attr;
	int scrnum;

	attr.screen = NULL;
	if (XGetWindowAttributes(dpy, w, &attr)) {
	for (scrnum = 0; scrnum < NumScreens; scrnum++) {
		if (ScreenList[scrnum] != NULL &&
		(ScreenOfDisplay(dpy, ScreenList[scrnum]->screen) ==
		 attr.screen))
		  return ScreenList[scrnum];
	}
	}

	return NULL;
}



static void flush_expose (w)
	Window w;
{
	XEvent dummy;

				/* SUPPRESS 530 */
	while (XCheckTypedWindowEvent (dpy, w, Expose, &dummy)) ;
}



/***********************************************************************
 *
 *  Procedure:
 *	InstallWindowColormaps - install the colormaps for one twm window
 *
 *  Inputs:
 *	type	- type of event that caused the installation
 *	tmp	- for a subset of event types, the address of the
 *		  window structure, whose colormaps are to be installed.
 *
 ***********************************************************************
 */

void InstallWindowColormaps (type, tmp)
	int type;
	TwmWindow *tmp;
{
	int i, j, n, number_cwins, state;
	ColormapWindow **cwins, *cwin, **maxcwin = NULL;
	TwmColormap *cmap;
	char *row, *scoreboard;

	switch (type) {
	case EnterNotify:
	case LeaveNotify:
	case DestroyNotify:
	default:
	/* Save the colormap to be loaded for when force loading of
	 * root colormap(s) ends.
	 */
	Scr->cmapInfo.pushed_window = tmp;
	/* Don't load any new colormap if root colormap(s) has been
	 * force loaded.
	 */
	if (Scr->cmapInfo.root_pushes)
		return;
	/* Don't reload the currend window colormap list.
	 */
	if (Scr->cmapInfo.cmaps == &tmp->cmaps)
		return;
	if (Scr->cmapInfo.cmaps)
		for (i = Scr->cmapInfo.cmaps->number_cwins,
		 cwins = Scr->cmapInfo.cmaps->cwins; i-- > 0; cwins++)
		(*cwins)->colormap->state &= ~CM_INSTALLABLE;
	Scr->cmapInfo.cmaps = &tmp->cmaps;
	break;

	case PropertyNotify:
	case VisibilityNotify:
	case ColormapNotify:
	break;
	}

	number_cwins = Scr->cmapInfo.cmaps->number_cwins;
	cwins = Scr->cmapInfo.cmaps->cwins;
	scoreboard = Scr->cmapInfo.cmaps->scoreboard;

	ColortableThrashing = FALSE; /* in case installation aborted */

	state = CM_INSTALLED;

/*
 * Submitted by Caveh Frank Jalali
 *
	  for (i = n = 0; i < number_cwins
		&& n < Scr->cmapInfo.maxCmaps
*/
	  for (i = 0; i < number_cwins

		/* comp.windows.x
		** Article <21sn92INNbiv@sirius.isi.com> Mon 18:06
		** Path: ..!news.isi.com!not-for-mail (Mark Kent @
		** Integrated Systems, Inc.)
		*/
	  ; i++) {
	cwin = cwins[i];
	cmap = cwin->colormap;
	cmap->state |= CM_INSTALLABLE;
	cmap->state &= ~CM_INSTALL;
	cmap->w = cwin->w;
	  }
	  for (i = n = 0; i < number_cwins; i++) {
  	cwin = cwins[i];
  	cmap = cwin->colormap;
	if (cwin->visibility != VisibilityFullyObscured
		/* && n < Scr->cmapInfo.maxCmaps
		** <21sn92INNbiv@sirius.isi.com>
		*/
	) {
		row = scoreboard + (i*(i-1)/2);
		for (j = 0; j < i; j++)
		if (row[j] && (cwins[j]->colormap->state & CM_INSTALL))
		    break;
		if (j != i)
		continue;
		n++;
		maxcwin = &cwins[i];
		state &= (cmap->state & CM_INSTALLED);
		cmap->state |= CM_INSTALL;
	}
	}

	Scr->cmapInfo.first_req = NextRequest(dpy);

/*
 * Submitted by Caveh Frank Jalali
 *
	for ( ; n > 0; maxcwin--) {
*/
	for ( ; n > 0; n--, maxcwin--) {

		cmap = (*maxcwin)->colormap;
		if (cmap->state & CM_INSTALL) {
			cmap->state &= ~CM_INSTALL;
			if (!(state & CM_INSTALLED)) {
				cmap->install_req = NextRequest(dpy);
				XInstallColormap(dpy, cmap->c);
			}
			cmap->state |= CM_INSTALLED;
/* see above 'for (...)'
			n--;
*/
		}
	}
}



/***********************************************************************
 *
 *  Procedures:
 *	<Uni/I>nstallRootColormap - Force (un)loads root colormap(s)
 *
 *	   These matching routines provide a mechanism to insure that
 *	   the root colormap(s) is installed during operations like
 *	   rubber banding or menu display that require colors from
 *	   that colormap.  Calls may be nested arbitrarily deeply,
 *	   as long as there is one UninstallRootColormap call per
 *	   InstallRootColormap call.
 *
 *	   The final UninstallRootColormap will cause the colormap list
 *	   which would otherwise have be loaded to be loaded, unless
 *	   Enter or Leave Notify events are queued, indicating some
 *	   other colormap list would potentially be loaded anyway.
 ***********************************************************************
 */

void InstallRootColormap()
{
	TwmWindow *tmp;
	if (Scr->cmapInfo.root_pushes == 0) {
	/*
	 * The saving and restoring of cmapInfo.pushed_window here
	 * is a slimy way to remember the actual pushed list and
	 * not that of the root window.
	 */
	tmp = Scr->cmapInfo.pushed_window;
	InstallWindowColormaps(0, &Scr->TwmRoot);
	Scr->cmapInfo.pushed_window = tmp;
	}
	Scr->cmapInfo.root_pushes++;
}



/* ARGSUSED*/
static Bool
UninstallRootColormapQScanner(dpy, ev, args)
	Display *dpy;
	XEvent *ev;
	char *args;
{
	if (!*args)
	{
	if (ev->type == EnterNotify) {
		if (ev->xcrossing.mode != NotifyGrab)
		*args = 1;
	} else if (ev->type == LeaveNotify) {
		if (ev->xcrossing.mode == NotifyNormal)
		*args = 1;
	}
	}

	return (False);
}



void UninstallRootColormap()
{
	char args;
	XEvent dummy;

	if (Scr->cmapInfo.root_pushes)
	Scr->cmapInfo.root_pushes--;

	if (!Scr->cmapInfo.root_pushes) {
	/*
	 * If we have subsequent Enter or Leave Notify events,
	 * we can skip the reload of pushed colormaps.
	 */
	XSync (dpy, 0);
	args = 0;
	(void) XCheckIfEvent(dpy, &dummy, UninstallRootColormapQScanner, &args);

	if (!args)
		InstallWindowColormaps(0, Scr->cmapInfo.pushed_window);
	}
}

void SendConfigureNotify(tmp_win, x, y)
TwmWindow *tmp_win;
int x, y;
{
	XEvent client_event;

    client_event.type = ConfigureNotify;
    client_event.xconfigure.display = dpy;
    client_event.xconfigure.event = tmp_win->w;
    client_event.xconfigure.window = tmp_win->w;

/* djhjr - 4/24/96
    client_event.xconfigure.x = (x + tmp_win->frame_bw - tmp_win->old_bw);
    client_event.xconfigure.y = (y + tmp_win->frame_bw +
			     tmp_win->title_height - tmp_win->old_bw);
    client_event.xconfigure.width = tmp_win->frame_width;
    client_event.xconfigure.height = tmp_win->frame_height -
            tmp_win->title_height;
*/
    client_event.xconfigure.x = (x + tmp_win->frame_bw - tmp_win->old_bw
			+ tmp_win->frame_bw3D);
    client_event.xconfigure.y = (y + tmp_win->frame_bw +
		     tmp_win->title_height - tmp_win->old_bw
			+ tmp_win->frame_bw3D);
    client_event.xconfigure.width = tmp_win->attr.width;
    client_event.xconfigure.height = tmp_win->attr.height;

    client_event.xconfigure.border_width = tmp_win->old_bw;
    /* Real ConfigureNotify events say we're above title window, so ... */
    /* what if we don't have a title ????? */
    client_event.xconfigure.above = tmp_win->frame;
    client_event.xconfigure.override_redirect = False;

    XSendEvent(dpy, tmp_win->w, False, StructureNotifyMask, &client_event);
}

#ifdef TRACE
dumpevent (e)
	XEvent *e;
{
	char *name = NULL;

	switch (e->type) {
	  case KeyPress:  name = "KeyPress"; break;
	  case KeyRelease:  name = "KeyRelease"; break;
	  case ButtonPress:  name = "ButtonPress"; break;
	  case ButtonRelease:  name = "ButtonRelease"; break;
	  case MotionNotify:  name = "MotionNotify"; break;
	  case EnterNotify:  name = "EnterNotify"; break;
	  case LeaveNotify:  name = "LeaveNotify"; break;
	  case FocusIn:  name = "FocusIn"; break;
	  case FocusOut:  name = "FocusOut"; break;
	  case KeymapNotify:  name = "KeymapNotify"; break;
	  case Expose:  name = "Expose"; break;
	  case GraphicsExpose:  name = "GraphicsExpose"; break;
	  case NoExpose:  name = "NoExpose"; break;
	  case VisibilityNotify:  name = "VisibilityNotify"; break;
	  case CreateNotify:  name = "CreateNotify"; break;
	  case DestroyNotify:  name = "DestroyNotify"; break;
	  case UnmapNotify:  name = "UnmapNotify"; break;
	  case MapNotify:  name = "MapNotify"; break;
	  case MapRequest:  name = "MapRequest"; break;
	  case ReparentNotify:  name = "ReparentNotify"; break;
	  case ConfigureNotify:  name = "ConfigureNotify"; break;
	  case ConfigureRequest:  name = "ConfigureRequest"; break;
	  case GravityNotify:  name = "GravityNotify"; break;
	  case ResizeRequest:  name = "ResizeRequest"; break;
	  case CirculateNotify:  name = "CirculateNotify"; break;
	  case CirculateRequest:  name = "CirculateRequest"; break;
	  case PropertyNotify:  name = "PropertyNotify"; break;
	  case SelectionClear:  name = "SelectionClear"; break;
	  case SelectionRequest:  name = "SelectionRequest"; break;
	  case SelectionNotify:  name = "SelectionNotify"; break;
	  case ColormapNotify:  name = "ColormapNotify"; break;
	  case ClientMessage:  name = "ClientMessage"; break;
	  case MappingNotify:  name = "MappingNotify"; break;
	}

	if (name) {
	printf ("event:  %s, %d remaining\n", name, QLength(dpy));
	} else {
	printf ("unknown event %d, %d remaining\n", e->type, QLength(dpy));
	}
}
#endif /* TRACE */