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
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
|
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
/* Copyright (c) 2008-2017 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de> */
/* Copyright (c) 2011-2022 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2014-2019 Mihai Moldovan <ionic@ionic.de> */
/* Copyright (c) 2014-2022 Ulrich Sibiller <uli42@gmx.de> */
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
/* */
/* NXAGENT, NX protocol compression and NX extensions to this software */
/* are copyright of the aforementioned persons and companies. */
/* */
/* Redistribution and use of the present software is allowed according */
/* to terms specified in the file LICENSE which comes in the source */
/* distribution. */
/* */
/* All rights reserved. */
/* */
/* NOTE: This software has received contributions from various other */
/* contributors, only the core maintainers and supporters are listed as */
/* copyright holders. Please contact us, if you feel you should be listed */
/* as copyright holder, as well. */
/* */
/**************************************************************************/
#include "X.h"
#include "signal.h"
#include "unistd.h"
#include "Xproto.h"
#include "screenint.h"
#include "input.h"
#include "dix.h"
#include "misc.h"
#include "scrnintstr.h"
#include "windowstr.h"
#include "servermd.h"
#include "mi.h"
#include "selection.h"
#include "keysym.h"
#include "fb.h"
#include "mibstorest.h"
#include "osdep.h"
#include "Agent.h"
#include "Args.h"
#include "Atoms.h"
#include "Colormap.h"
#include "Display.h"
#include "Screen.h"
#include "Windows.h"
#include "Pixmaps.h"
#include "Keyboard.h"
#include "Keystroke.h"
#include "Events.h"
#include "Pointer.h"
#include "Rootless.h"
#include "Splash.h"
#include "Trap.h"
#include "Dialog.h"
#include "Client.h"
#include "Clipboard.h"
#include "Split.h"
#include "Drawable.h"
#include "Handlers.h"
#include "Utils.h"
#include "Error.h"
#include <nx/NX.h>
#include <nx/NXvars.h>
#include <nx/NXproto.h>
#include "xfixesproto.h"
#define Window XlibWindow
#define Atom XlibAtom
#define Time XlibXID
#include "X11/include/Xfixes_nxagent.h"
#undef Window
#undef Atom
#undef Time
#ifdef NXAGENT_FIXKEYS
#include "inputstr.h"
#include "input.h"
#endif
#define Time XlibXID
#include "XKBlib.h"
#undef Time
#define GC XlibGC
#define Font XlibFont
#define KeySym XlibKeySym
#define XID XlibXID
#include "Xlibint.h"
#undef GC
#undef Font
#undef KeySym
#undef XID
#include <nx-X11/cursorfont.h>
#include <nx/Shadow.h>
#include "X11/include/Xrandr_nxagent.h"
#include "compext/Compext.h"
/*
* Set here the required log level. Please note that if you want to
* enable DEBUG here, then you need to enable DEBUG even in Rootless.c
*/
#define PANIC
#define WARNING
#undef TEST
#undef DEBUG
/* debug individual subsystems */
#undef DEBUG_AUTOGRAB
/* activate subsystems if generic DEBUG is activated */
#ifdef DEBUG
#ifndef DEBUG_AUTOGRAB
#define DEBUG_AUTOGRAB
#endif
#endif
/*
* Log begin and end of the important handlers.
*/
#undef BLOCKS
extern Bool nxagentOnce;
#ifdef NX_DEBUG_INPUT
Bool nxagentDebugInput = False;
#endif
#ifdef DEBUG
extern Bool nxagentRootlessTreesMatch(void);
#endif
extern Selection *CurrentSelections;
extern int NumCurrentSelections;
typedef union _XFixesSelectionEvent {
int type;
XFixesSelectionNotifyEvent xfixesselection;
XEvent core;
} XFixesSelectionEvent;
Bool xkbdRunning = False;
pid_t pidkbd;
WindowPtr nxagentLastEnteredWindow = NULL;
PropertyRequestRec nxagentPropertyRequests[NXNumberOfResources];
void nxagentHandleCollectPropertyEvent(XEvent*);
/*
* Finalize the asynchronous handling of the X_GrabPointer requests.
*/
void nxagentHandleCollectGrabPointerEvent(int resource);
Bool nxagentCollectGrabPointerPredicate(Display *disp, XEvent *X, XPointer ptr);
/*
* Used in Handlers.c to synchronize the agent with the remote X
* server.
*/
void nxagentHandleCollectInputFocusEvent(int resource);
/*
* Viewport navigation.
*/
static int viewportInc = 1;
static enum HandleEventResult viewportLastKeyPressResult;
static int viewportLastX;
static int viewportLastY;
static Cursor viewportCursor;
#define MAX_INC 200
#define INC_STEP 5
#define nextinc(x) ((x) < MAX_INC ? (x) += INC_STEP : (x))
/*
* Keyboard and pointer are handled as they were real devices by Xnest
* and we inherit this behaviour. The following mask will contain the
* event mask selected for the root window of the agent. All the
* keyboard and pointer events will be translated by the agent and
* sent to the internal clients according to events selected by the
* inferior windows.
*/
static Mask defaultEventMask;
static int lastEventSerial = 0;
/*
* Used to mask the appropriate bits in the state reported by
* XkbStateNotify and XkbGetIndicatorState.
*/
#define CAPSFLAG_IN_REPLY 1
#define CAPSFLAG_IN_EVENT 2
#define NUMFLAG_IN_EVENT 16
#define NUMFLAG_IN_REPLY 2
CARD32 nxagentLastEventTime = 0;
CARD32 nxagentLastKeyPressTime = 0;
Time nxagentLastServerTime = 0;
/*
* Used for storing windows that need to receive expose events from
* the agent.
*/
#define nxagentExposeQueueHead nxagentExposeQueue.exposures[nxagentExposeQueue.start]
ExposeQueue nxagentExposeQueue;
RegionPtr nxagentRemoteExposeRegion = NULL;
static void nxagentForwardRemoteExpose(void);
static int nxagentClipAndSendExpose(WindowPtr pWin, void * ptr);
/*
* This is from NXproperty.c.
*/
int GetWindowProperty(WindowPtr pWin, Atom property, long longOffset,
long longLength, Bool delete, Atom type,
Atom *actualType, int *format, unsigned
long *nItems, unsigned long *bytesAfter,
unsigned char **propData);
/*
* Associate a resource to a drawable and store the region affected by
* the split operation.
*/
SplitResourceRec nxagentSplitResources[NXNumberOfResources];
/*
* Associate a resource to an unpack operation.
*/
UnpackResourceRec nxagentUnpackResources[NXNumberOfResources];
/*
* We have to check these before launching
* the terminate dialog in rootless mode.
*/
Bool nxagentLastWindowDestroyed = False;
Time nxagentLastWindowDestroyedTime = 0;
/*
* Set this flag when an user input event is received.
*/
int nxagentInputEvent = 0;
int nxagentKeyDown = 0;
void nxagentSwitchResizeMode(ScreenPtr pScreen);
int nxagentCheckWindowConfiguration(XConfigureEvent* X);
#define nxagentMonitoredDuplicate(keysym) \
((keysym) == XK_Left || (keysym) == XK_Up || \
(keysym) == XK_Right || (keysym) == XK_Down || \
(keysym) == XK_Page_Up || (keysym) == XK_Page_Down || \
(keysym) == XK_Delete || (keysym) == XK_BackSpace)
void nxagentRemoveDuplicatedKeys(XEvent *X);
void ProcessInputEvents(void)
{
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Processing input.\n", __func__);
}
#endif
mieqProcessInputEvents();
}
#ifdef DEBUG
char * nxagentGetNotifyMode(int mode)
{
switch (mode)
{
case NotifyNormal: return "NotifyNormal";
case NotifyGrab: return "NotifyGrab";
case NotifyUngrab: return "NotifyUngrab";
case NotifyWhileGrabbed: return "NotifyWhileGrabbed";
}
return "Unknown";
}
#endif
#ifdef DEBUG_TREE
/*
* Print ID and name of window.
*/
void nxagentRemoteWindowID(Window window, Bool newline)
{
#ifdef NO_I18N
char *winName;
#else
XTextProperty tp;
#endif
fprintf(stderr, "0x%x", window);
if (!window)
{
fprintf(stderr, " (none) ");
}
else
{
if (window == DefaultRootWindow(nxagentDisplay))
{
fprintf(stderr, " (the root window) ");
}
#ifdef NO_I18N
if (!XFetchName(nxagentDisplay, window, &winName))
{
fprintf(stderr, " (has no name) ");
}
else if (winName)
{
fprintf(stderr, " \"%s\" ", winName);
SAFE_XFree(winName);
}
#else
if (XGetWMName(nxagentDisplay, window, &tp) == 0)
{
fprintf(stderr, " (has no name) ");
}
else if (tp.nitems > 0)
{
fprintf(stderr, " \"");
int count = 0;
char **list = NULL;
int ret = XmbTextPropertyToTextList(nxagentDisplay, &tp, &list, &count);
if ((ret == Success || ret > 0) && list != NULL)
{
for (int i = 0; i < count; i++)
{
fprintf(stderr, "%s", list[i]);
}
XFreeStringList(list);
}
else
{
fprintf(stderr, "%s", tp.value);
}
fprintf(stderr, "\" ");
}
#endif
else
{
fprintf(stderr, " (has no name) ");
}
}
if (newline)
{
fprintf(stderr, "\n");
}
return;
}
/*
* Print info about remote window.
*/
void nxagentRemoteWindowInfo(Window win, int indent, Bool newLine)
{
XWindowAttributes attributes;
if (XGetWindowAttributes(nxagentDisplay, win, &attributes) == 0)
{
return;
}
fprintf(stderr, "%*sx=%d y=%d width=%d height=%d class=%s map_state=%s "
"override_redirect=%s\n", indent, "", attributes.x, attributes.y,
attributes.width, attributes.height,
(attributes.class == 0) ? "CopyFromParent" :
((attributes.class == 1) ? "InputOutput" : "InputOnly"),
(attributes.map_state == 0) ?
"IsUnmapped" : (attributes.map_state == 1 ?
"IsUnviewable" : "IsViewable"),
(attributes.override_redirect == 0) ?
"No" : "Yes" );
if (newLine)
{
fprintf(stderr, "\n");
}
}
/*
* Walk remote windows tree.
*
* FIXME:
* ========== nxagentRemoteWindowsTree ============
*
* Root Window ID: 0x169 (the root window) (has no name)
* Parent window ID: 0x2a00063 "NX Agent"
* 0 children.
*
* ========== nxagentInternalWindowsTree ==========
* Window ID=[0x9d] Remote ID=[0x2a0007e] Name: ( has no name )
* x=0 y=0 width=1440 height=810 class=InputOutput map_state=IsViewable override_redirect=No
*
* -> Internal root window's remote id is not listed in RemoteWindowsTree.
*/
void nxagentRemoteWindowsTree(Window window, int level)
{
unsigned long rootWin, parentWin;
unsigned int numChildren;
unsigned long *childList = NULL;
if (!XQueryTree(nxagentDisplay, window, &rootWin, &parentWin, &childList,
&numChildren))
{
fprintf(stderr, "%s - XQueryTree failed.\n", __func__);
return;
}
if (level == 0)
{
fprintf(stderr, "\n");
fprintf(stderr, " Root Window ID: ");
nxagentRemoteWindowID(rootWin, TRUE);
fprintf(stderr, " Parent window ID: ");
nxagentRemoteWindowID(parentWin, TRUE);
}
if (level == 0 || numChildren > 0)
{
fprintf(stderr, "%*s", (level * 4) + 5, ""); /* 4 spaces per level */
fprintf(stderr, "%d child%s%s\n", numChildren, (numChildren == 1) ? "" :
"ren", (numChildren == 1) ? ":" : ".");
}
for (int i = (int) numChildren - 1; i >= 0; i--)
{
fprintf(stderr, "%*s", (level * 5) + 6, ""); /* 5 spaces per level */
nxagentRemoteWindowID(childList[i], TRUE);
nxagentRemoteWindowInfo(childList[i], (level * 5) + 6, TRUE);
nxagentRemoteWindowsTree(childList[i], level + 1);
}
SAFE_XFree(childList);
}
/*
* Print info about internal window.
*/
void nxagentInternalWindowInfo(WindowPtr pWin, int indent, Bool newLine)
{
unsigned long ulReturnItems;
unsigned long ulReturnBytesLeft;
Atom atomReturnType;
int iReturnFormat;
unsigned char *pszReturnData = NULL;
fprintf(stderr, "Window ID=[0x%x] %s Remote ID=[0x%x] ", pWin -> drawable.id,
pWin->parent ? "" : "(the root window)", nxagentWindow(pWin));
int result = GetWindowProperty(pWin, MakeAtom("WM_NAME", 7, False) , 0,
sizeof(CARD32), False, AnyPropertyType,
&atomReturnType, &iReturnFormat,
&ulReturnItems, &ulReturnBytesLeft,
&pszReturnData);
fprintf(stderr, "Name: ");
if (result == Success && pszReturnData != NULL)
{
fprintf(stderr, "\"%*.*s\"\n", (int)ulReturnItems, (int)ulReturnItems, (char *) pszReturnData);
}
else
{
fprintf(stderr, "%s\n", "( has no name )");
}
fprintf(stderr, "%*sx=%d y=%d width=%d height=%d class=%s map_state=%s "
"override_redirect=%s", indent, "", pWin -> drawable.x, pWin -> drawable.y,
pWin -> drawable.width, pWin -> drawable.height,
(pWin -> drawable.class == 0) ? "CopyFromParent" :
((pWin -> drawable.class == 1) ? "InputOutput" :
"InputOnly"),
(pWin -> mapped == 0) ?
"IsUnmapped" : (pWin -> realized == 0 ?
"IsUnviewable" : "IsViewable"),
(pWin -> overrideRedirect == 0) ?
"No" : "Yes");
if (newLine)
{
fprintf(stderr, "\n");
}
}
/*
* Walk internal windows tree.
*/
void nxagentInternalWindowsTree(WindowPtr pWin, int indent)
{
for (; pWin; pWin = pWin -> nextSib)
{
fprintf(stderr, "%*s", indent, "");
nxagentInternalWindowInfo(pWin, indent, TRUE);
fprintf(stderr, "\n");
nxagentInternalWindowsTree(pWin -> firstChild, indent + 4);
}
}
#endif /* DEBUG_TREE */
void nxagentSwitchResizeMode(ScreenPtr pScreen)
{
#ifdef DEBUG
fprintf(stderr, "%s: Called.\n", __func__);
#endif
int desktopResize = nxagentOption(DesktopResize);
nxagentChangeOption(DesktopResize, !desktopResize);
if (!nxagentOption(DesktopResize))
{
fprintf(stderr,"Info: Disabled desktop resize mode in agent.\n");
nxagentLaunchDialog(DIALOG_DISABLE_DESKTOP_RESIZE_MODE);
if (!nxagentOption(Fullscreen))
{
nxagentSetWMNormalHintsMaxsize(pScreen,
nxagentOption(RootWidth),
nxagentOption(RootHeight));
}
}
else
{
fprintf(stderr,"Info: Enabled desktop resize mode in agent.\n");
nxagentLaunchDialog(DIALOG_ENABLE_DESKTOP_RESIZE_MODE);
nxagentChangeScreenConfig(0, nxagentOption(Width), nxagentOption(Height), True);
if (nxagentOption(ClientOs) == ClientOsWinnt)
{
NXSetExposeParameters(nxagentDisplay, 0, 0, 0);
}
nxagentSetWMNormalHintsMaxsize(pScreen,
WidthOfScreen(DefaultScreenOfDisplay(nxagentDisplay)),
HeightOfScreen(DefaultScreenOfDisplay(nxagentDisplay)));
}
}
void nxagentShadowSwitchResizeMode(ScreenPtr pScreen)
{
int desktopResize = nxagentOption(DesktopResize);
nxagentChangeOption(DesktopResize, !desktopResize);
if (!nxagentOption(DesktopResize))
{
nxagentShadowSetRatio(1.0, 1.0);
nxagentShadowCreateMainWindow(screenInfo.screens[DefaultScreen(nxagentDisplay)], screenInfo.screens[0]->root,
screenInfo.screens[0]->root -> drawable.width, screenInfo.screens[0]->root -> drawable.height);
nxagentSetWMNormalHintsMaxsize(pScreen,
nxagentOption(RootWidth),
nxagentOption(RootHeight));
fprintf(stderr,"Info: Disabled resize mode in shadow agent.\n");
}
else
{
nxagentShadowSetRatio(nxagentOption(Width) * 1.0 /
screenInfo.screens[0]->root -> drawable.width,
nxagentOption(Height) * 1.0 /
screenInfo.screens[0]->root -> drawable.height);
nxagentShadowCreateMainWindow(screenInfo.screens[DefaultScreen(nxagentDisplay)],
screenInfo.screens[0]->root, screenInfo.screens[0]->root -> drawable.width,
screenInfo.screens[0]->root -> drawable.height);
nxagentSetWMNormalHintsMaxsize(pScreen,
WidthOfScreen(DefaultScreenOfDisplay(nxagentDisplay)),
HeightOfScreen(DefaultScreenOfDisplay(nxagentDisplay)));
fprintf(stderr,"Info: Enabled resize mode in shadow agent.\n");
}
}
static void nxagentSwitchDeferMode(void)
{
if (nxagentOption(DeferLevel) == 0)
{
nxagentChangeOption(DeferLevel, UNDEFINED);
nxagentSetDeferLevel();
}
else
{
nxagentChangeOption(DeferLevel, 0);
}
if (nxagentOption(DeferLevel) != 0)
{
nxagentLaunchDialog(DIALOG_ENABLE_DEFER_MODE);
}
else
{
nxagentLaunchDialog(DIALOG_DISABLE_DEFER_MODE);
nxagentForceSynchronization = True;
}
}
static void nxagentEnableAutoGrab(void)
{
#ifdef DEBUG_AUTOGRAB
fprintf(stderr, "enabling autograb\n");
#endif
nxagentGrabPointerAndKeyboard(NULL);
nxagentChangeOption(AutoGrab, True);
nxagentLaunchDialog(DIALOG_ENABLE_AUTOGRAB_MODE);
}
static void nxagentDisableAutoGrab(void)
{
#ifdef DEBUG_AUTOGRAB
fprintf(stderr, "disabling autograb\n");
#endif
nxagentUngrabPointerAndKeyboard(NULL);
nxagentChangeOption(AutoGrab, False);
nxagentLaunchDialog(DIALOG_DISABLE_AUTOGRAB_MODE);
}
static void nxagentToggleAutoGrab(void)
{
/* autograb only works in windowed mode */
if (nxagentOption(Rootless) || nxagentOption(Fullscreen))
return;
if (!nxagentOption(AutoGrab))
nxagentEnableAutoGrab();
else
nxagentDisableAutoGrab();
}
static Bool nxagentExposurePredicate(Display *disp, XEvent *event, XPointer window)
{
/*
* Handle both Expose and ProcessedExpose events. The latters are
* those not filtered by function nxagentWindowExposures().
*/
if (window)
{
return ((event -> type == Expose || event -> type == ProcessedExpose) &&
event -> xexpose.window == *((Window *) window));
}
else
{
return (event -> type == Expose || event -> type == ProcessedExpose);
}
}
static int nxagentAnyEventPredicate(Display *disp, XEvent *event, XPointer parameter)
{
return 1;
}
int nxagentInputEventPredicate(Display *disp, XEvent *event, XPointer parameter)
{
switch (event -> type)
{
case KeyPress:
case KeyRelease:
case ButtonPress:
case ButtonRelease:
{
return 1;
}
default:
{
return 0;
}
}
}
void nxagentInitDefaultEventMask(void)
{
Mask mask = NoEventMask;
mask |= (StructureNotifyMask | VisibilityChangeMask);
mask |= ExposureMask;
mask |= NXAGENT_KEYBOARD_EVENT_MASK;
mask |= NXAGENT_POINTER_EVENT_MASK;
defaultEventMask = mask;
}
Mask nxagentGetDefaultEventMask(void)
{
return defaultEventMask;
}
void nxagentSetDefaultEventMask(Mask mask)
{
defaultEventMask = mask;
}
Mask nxagentGetEventMask(WindowPtr pWin)
{
Mask mask = NoEventMask;
if (nxagentOption(Rootless))
{
/*
* mask = pWin -> eventMask &
* ~(NXAGENT_KEYBOARD_EVENT_MASK | NXAGENT_POINTER_EVENT_MASK);
*/
if (pWin -> drawable.class == InputOutput)
{
if (nxagentWindowTopLevel(pWin))
{
mask = defaultEventMask;
}
else
{
mask = ExposureMask | VisibilityChangeMask | PointerMotionMask;
}
}
mask |= PropertyChangeMask;
}
else if (pWin -> drawable.class != InputOnly)
{
mask = ExposureMask | VisibilityChangeMask;
}
return mask;
}
static int nxagentChangeMapPrivate(WindowPtr pWin, void * ptr)
{
if (pWin && nxagentWindowPriv(pWin))
{
nxagentWindowPriv(pWin) -> isMapped = *((Bool *) ptr);
}
return WT_WALKCHILDREN;
}
static int nxagentChangeVisibilityPrivate(WindowPtr pWin, void * ptr)
{
if (pWin && nxagentWindowPriv(pWin))
{
nxagentWindowPriv(pWin) -> visibilityState = *((int *) ptr);
}
return WT_WALKCHILDREN;
}
void nxagentDispatchEvents(PredicateFuncPtr predicate)
{
XEvent X;
xEvent x;
ScreenPtr pScreen = NULL;
Bool minimize = False;
Bool closeSession = False;
Bool switchFullscreen = False;
Bool switchAllScreens = False;
/*
* Last entered top level window.
*/
static WindowPtr nxagentLastEnteredTopLevelWindow = NULL;
#ifdef BLOCKS
fprintf(stderr, "[Begin read]\n");
#endif
#ifdef TEST
fprintf(stderr, "%s: Going to handle new events with predicate [%p].\n", __func__,
*(void **)&predicate);
#endif
if (nxagentRemoteExposeRegion == NULL)
{
nxagentInitRemoteExposeRegion();
}
/*
* We must read here, even if apparently there is nothing to
* read. The ioctl() based readable function, in fact, is often
* unable to detect a failure of the socket, in particular if the
* agent was connected to the proxy and the proxy is gone. Thus we
* must trust the wakeup handler that called us after the select().
*/
#ifdef TEST
if (nxagentPendingEvents(nxagentDisplay) == 0)
{
fprintf(stderr, "%s: PANIC! No event needs to be dispatched.\n", __func__);
}
#endif
/*
* We want to process all the events already in the queue, plus any
* additional event that may be read from the network. If no event
* can be read, we want to continue handling our clients without
* flushing the output buffer.
*/
while (nxagentCheckEvents(nxagentDisplay, &X, predicate != NULL ? predicate :
nxagentAnyEventPredicate, NULL) == 1)
{
#ifdef DEBUG
fprintf(stderr, "%s: Going to handle new event type [%d].\n", __func__,
X.type);
#endif
/*
* Handle the incoming event.
*/
switch (X.type)
{
#ifdef NXAGENT_CLIPBOARD
case SelectionClear:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new SelectionClear event.\n", __func__);
#endif
nxagentHandleSelectionClearFromXServer(&X);
break;
}
case SelectionRequest:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new SelectionRequest event.\n", __func__);
#endif
nxagentHandleSelectionRequestFromXServer(&X);
break;
}
case SelectionNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new SelectionNotify event.\n", __func__);
#endif
nxagentHandleSelectionNotifyFromXServer(&X);
break;
}
#endif /* NXAGENT_CLIPBOARD */
case PropertyNotify:
{
#ifdef TEST
fprintf(stderr, "%s: PropertyNotify on prop %d[%s] window %lx state %d\n", __func__,
(int)X.xproperty.atom, validateString(XGetAtomName(nxagentDisplay, X.xproperty.atom)),
X.xproperty.window, X.xproperty.state);
#endif
nxagentHandlePropertyNotify(&X);
break;
}
case KeyPress:
{
enum HandleEventResult result;
#ifdef TEST
fprintf(stderr, "%s: Going to handle new KeyPress event.\n", __func__);
#endif
nxagentInputEvent = 1;
nxagentKeyDown++;
nxagentHandleKeyPress(&X, &result);
if (viewportLastKeyPressResult != result)
{
viewportInc = 1;
viewportLastKeyPressResult = result;
}
if (result != doNothing)
{
pScreen = nxagentScreen(X.xkey.window);
}
switch (result)
{
case doNothing:
{
break;
}
#ifdef DEBUG_TREE
case doDebugTree:
{
fprintf(stderr, "\n========== nxagentRemoteWindowsTree ============\n");
nxagentRemoteWindowsTree(nxagentWindow(screenInfo.screens[0]->root), 0);
fprintf(stderr, "\n========== nxagentInternalWindowsTree ==========\n");
nxagentInternalWindowsTree(screenInfo.screens[0]->root, 0);
break;
}
#endif /* DEBUG_TREE */
case doCloseSession:
{
closeSession = TRUE;
break;
}
case doMinimize:
{
minimize = TRUE;
break;
}
case doSwitchFullscreen:
{
switchFullscreen = TRUE;
break;
}
case doSwitchAllScreens:
{
switchAllScreens = TRUE;
break;
}
case doViewportMoveUp:
{
nxagentMoveViewport(pScreen, 0, -nxagentOption(Height));
break;
}
case doViewportMoveDown:
{
nxagentMoveViewport(pScreen, 0, nxagentOption(Height));
break;
}
case doViewportMoveLeft:
{
nxagentMoveViewport(pScreen, -nxagentOption(Width), 0);
break;
}
case doViewportMoveRight:
{
nxagentMoveViewport(pScreen, nxagentOption(Width), 0);
break;
}
case doViewportUp:
{
nxagentMoveViewport(pScreen, 0, -nextinc(viewportInc));
break;
}
case doViewportDown:
{
nxagentMoveViewport(pScreen, 0, +nextinc(viewportInc));
break;
}
case doViewportLeft:
{
nxagentMoveViewport(pScreen, -nextinc(viewportInc), 0);
break;
}
case doViewportRight:
{
nxagentMoveViewport(pScreen, +nextinc(viewportInc), 0);
break;
}
case doSwitchResizeMode:
{
if (!nxagentOption(Shadow))
{
if (nxagentNoDialogIsRunning)
{
nxagentSwitchResizeMode(pScreen);
}
}
else
{
nxagentShadowSwitchResizeMode(pScreen);
}
break;
}
case doSwitchDeferMode:
{
if (nxagentNoDialogIsRunning)
{
nxagentSwitchDeferMode();
}
break;
}
case doAutoGrab:
{
nxagentToggleAutoGrab();
break;
}
case doDumpClipboard:
{
nxagentDumpClipboardStat();
break;
}
default:
{
FatalError("nxagentDispatchEvent: handleKeyPress returned unknown value\n");
break;
}
}
/*
* Elide multiple KeyPress/KeyRelease events of the same key
* and generate a single pair. This is intended to reduce the
* impact of the latency on the key auto-repeat, handled by
* the remote X server. We may optionally do that only if the
* timestamps in the events show an excessive delay.
*/
KeySym keysym = XKeycodeToKeysym(nxagentDisplay, X.xkey.keycode, 0);
if (nxagentMonitoredDuplicate(keysym) == 1)
{
nxagentRemoveDuplicatedKeys(&X);
}
if (!nxagentOption(ViewOnly) && nxagentOption(Shadow) && result == doNothing)
{
X.xkey.keycode = nxagentConvertKeycode(X.xkey.keycode);
NXShadowEvent(nxagentDisplay, X);
}
break;
}
case KeyRelease:
{
enum HandleEventResult result;
int sendKey = 0;
/*
FIXME: If we don't flush the queue here, it could happen that the
inputInfo structure will not be up to date when we perform the
following check on down keys.
*/
ProcessInputEvents();
/*
FIXME: Don't enqueue the KeyRelease event if the key was not already
pressed. This workaround avoids a fake KeyPress being enqueued
by the XKEYBOARD extension. Another solution would be to let
the events enqueued and to remove the KeyPress afterwards.
*/
if (BitIsOn(inputInfo.keyboard -> key -> down,
nxagentConvertKeycode(X.xkey.keycode)))
{
sendKey = 1;
}
#ifdef TEST
fprintf(stderr, "%s: Going to handle new KeyRelease event.\n", __func__);
#endif
nxagentInputEvent = 1;
nxagentKeyDown--;
if (nxagentKeyDown <= 0)
{
nxagentKeyDown = 0;
}
if (!nxagentXkbState.Initialized)
{
if (X.xkey.keycode == nxagentCapsLockKeycode)
{
nxagentXkbCapsTrap = True;
}
else if (X.xkey.keycode == nxagentNumLockKeycode)
{
nxagentXkbNumTrap = True;
}
nxagentInitXkbKeyboardState();
nxagentXkbCapsTrap = False;
nxagentXkbNumTrap = False;
}
/* Calculate the time elapsed between this and the last event
we received. Add this delta to time we recorded for the
last KeyPress event we passed on to our clients. */
memset(&x, 0, sizeof(xEvent));
x.u.u.type = KeyRelease;
x.u.u.detail = nxagentConvertKeycode(X.xkey.keycode);
x.u.keyButtonPointer.time = nxagentLastKeyPressTime +
(X.xkey.time - nxagentLastServerTime);
nxagentLastServerTime = X.xkey.time;
nxagentLastEventTime = GetTimeInMillis();
if (x.u.keyButtonPointer.time > nxagentLastEventTime)
{
x.u.keyButtonPointer.time = nxagentLastEventTime;
}
/* do not send a KeyRelease for a special keystroke since we
also did not send a KeyPress event in that case */
if (!(nxagentCheckSpecialKeystroke(&X.xkey, &result)) && (sendKey == 1))
{
#ifdef TEST
fprintf(stderr, "%s: passing KeyRelease event to clients\n", __func__);
#endif
mieqEnqueue(&x);
SetCriticalOutputPending();
if (!nxagentOption(ViewOnly) && nxagentOption(Shadow))
{
X.xkey.keycode = nxagentConvertKeycode(X.xkey.keycode);
NXShadowEvent(nxagentDisplay, X);
}
}
else
{
#ifdef TEST
fprintf(stderr, "%s: NOT passing KeyRelease event to clients\n", __func__);
#endif
}
break;
}
case ButtonPress:
{
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Going to handle new ButtonPress event.\n", __func__);
}
#endif
nxagentInputEvent = 1;
if (nxagentOption(Fullscreen))
{
if ( nxagentOption(MagicPixel) && nxagentMagicPixelZone(X.xbutton.x, X.xbutton.y) )
{
pScreen = nxagentScreen(X.xbutton.window);
minimize = True;
break;
}
}
if (!nxagentOption(DesktopResize) &&
(X.xbutton.state & (ControlMask | Mod1Mask)) == (ControlMask | Mod1Mask))
{
/*
* Start viewport navigation mode.
*/
int resource = nxagentWaitForResource(NXGetCollectGrabPointerResource,
nxagentCollectGrabPointerPredicate);
pScreen = nxagentScreen(X.xbutton.window);
viewportCursor = XCreateFontCursor(nxagentDisplay, XC_fleur);
NXCollectGrabPointer(nxagentDisplay, resource,
nxagentDefaultWindows[pScreen -> myNum], True,
NXAGENT_POINTER_EVENT_MASK, GrabModeAsync,
GrabModeAsync, None, viewportCursor,
CurrentTime);
viewportLastX = X.xbutton.x;
viewportLastY = X.xbutton.y;
break;
}
if (!(nxagentOption(Fullscreen) &&
X.xbutton.window == nxagentFullscreenWindow &&
X.xbutton.subwindow == None))
{
memset(&x, 0, sizeof(xEvent));
x.u.u.type = ButtonPress;
x.u.u.detail = inputInfo.pointer -> button -> map[nxagentReversePointerMap[X.xbutton.button]];
x.u.keyButtonPointer.time = nxagentLastEventTime = GetTimeInMillis();
if (nxagentOption(Rootless))
{
x.u.keyButtonPointer.rootX = X.xmotion.x_root;
x.u.keyButtonPointer.rootY = X.xmotion.y_root;
}
else
{
x.u.keyButtonPointer.rootX = X.xmotion.x - nxagentOption(RootX);
x.u.keyButtonPointer.rootY = X.xmotion.y - nxagentOption(RootY);
}
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Adding ButtonPress event.\n", __func__);
}
#endif
mieqEnqueue(&x);
SetCriticalOutputPending();
}
if (!nxagentOption(ViewOnly) && nxagentOption(Shadow))
{
X.xbutton.x -= nxagentOption(RootX);
X.xbutton.y -= nxagentOption(RootY);
if (nxagentOption(YRatio) != DONT_SCALE)
{
X.xbutton.x = (X.xbutton.x << PRECISION) / nxagentOption(YRatio);
}
if (nxagentOption(XRatio) != DONT_SCALE)
{
X.xbutton.y = (X.xbutton.y << PRECISION) / nxagentOption(YRatio);
}
NXShadowEvent(nxagentDisplay, X);
}
break;
}
case ButtonRelease:
{
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Going to handle new ButtonRelease event.\n", __func__);
}
#endif
nxagentInputEvent = 1;
if (viewportCursor)
{
/*
* Leave viewport navigation mode.
*/
XUngrabPointer(nxagentDisplay, CurrentTime);
XFreeCursor(nxagentDisplay, viewportCursor);
viewportCursor = None;
}
if (minimize != True)
{
memset(&x, 0, sizeof(xEvent));
x.u.u.type = ButtonRelease;
x.u.u.detail = inputInfo.pointer -> button -> map[nxagentReversePointerMap[X.xbutton.button]];
x.u.keyButtonPointer.time = nxagentLastEventTime = GetTimeInMillis();
if (nxagentOption(Rootless))
{
x.u.keyButtonPointer.rootX = X.xmotion.x_root;
x.u.keyButtonPointer.rootY = X.xmotion.y_root;
}
else
{
x.u.keyButtonPointer.rootX = X.xmotion.x - nxagentOption(RootX);
x.u.keyButtonPointer.rootY = X.xmotion.y - nxagentOption(RootY);
}
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Adding ButtonRelease event.\n", __func__);
}
#endif
mieqEnqueue(&x);
SetCriticalOutputPending();
}
if (!nxagentOption(ViewOnly) && nxagentOption(Shadow))
{
X.xbutton.x -= nxagentOption(RootX);
X.xbutton.y -= nxagentOption(RootY);
if (nxagentOption(XRatio) != DONT_SCALE)
{
X.xbutton.x = (X.xbutton.x << PRECISION) / nxagentOption(XRatio);
}
if (nxagentOption(YRatio) != DONT_SCALE)
{
X.xbutton.y = (X.xbutton.y << PRECISION) / nxagentOption(YRatio);
}
NXShadowEvent(nxagentDisplay, X);
}
break;
}
case MotionNotify:
{
pScreen = nxagentScreen(X.xmotion.window);
#ifdef TEST
fprintf(stderr, "%s: Going to handle new MotionNotify event.\n", __func__);
#endif
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Handling motion notify window [%ld] root [%ld] child [%ld].\n",
__func__, X.xmotion.window, X.xmotion.root, X.xmotion.subwindow);
fprintf(stderr, "%s: Pointer at [%d][%d] relative root [%d][%d].\n", __func__,
X.xmotion.x, X.xmotion.y, X.xmotion.x_root, X.xmotion.y_root);
}
#endif
memset(&x, 0, sizeof(xEvent));
x.u.u.type = MotionNotify;
if (nxagentOption(Rootless))
{
WindowPtr pWin = nxagentWindowPtr(X.xmotion.window);
if (pWin)
{
nxagentLastEnteredWindow = pWin;
}
if (nxagentPulldownDialogPid == 0 && nxagentLastEnteredTopLevelWindow &&
(X.xmotion.y_root < nxagentLastEnteredTopLevelWindow -> drawable.y + 4))
{
if (pWin && nxagentClientIsDialog(wClient(pWin)) == 0 &&
nxagentLastEnteredTopLevelWindow -> parent == screenInfo.screens[0]->root &&
nxagentLastEnteredTopLevelWindow -> overrideRedirect == False &&
X.xmotion.x_root > (nxagentLastEnteredTopLevelWindow -> drawable.x +
(nxagentLastEnteredTopLevelWindow -> drawable.width >> 1) - 50) &&
X.xmotion.x_root < (nxagentLastEnteredTopLevelWindow -> drawable.x +
(nxagentLastEnteredTopLevelWindow -> drawable.width >> 1) + 50) &&
nxagentOption(Menu))
{
nxagentPulldownDialog(nxagentLastEnteredTopLevelWindow -> drawable.id);
}
}
x.u.keyButtonPointer.rootX = X.xmotion.x_root;
x.u.keyButtonPointer.rootY = X.xmotion.y_root;
}
else
{
x.u.keyButtonPointer.rootX = X.xmotion.x - nxagentOption(RootX);
x.u.keyButtonPointer.rootY = X.xmotion.y - nxagentOption(RootY);
}
x.u.keyButtonPointer.time = nxagentLastEventTime = GetTimeInMillis();
if (viewportCursor == None &&
!(nxagentOption(Fullscreen) &&
X.xmotion.window == nxagentDefaultWindows[pScreen -> myNum]
&& X.xmotion.subwindow == None))
{
#ifdef NX_DEBUG_INPUT
if (nxagentDebugInput)
{
fprintf(stderr, "%s: Adding motion event [%d, %d] to the queue.\n", __func__,
x.u.keyButtonPointer.rootX, x.u.keyButtonPointer.rootY);
}
#endif
mieqEnqueue(&x);
}
/*
* This test is more complicated and probably not necessary, compared
* to a simple check on viewportCursor.
*
* if (!nxagentOption(Fullscreen) &&
* (X.xmotion.state & (ControlMask | Mod1Mask | Button1Mask)) ==
* (ControlMask | Mod1Mask | Button1Mask))
*/
if (viewportCursor)
{
/*
* Pointer is in viewport navigation mode.
*/
nxagentMoveViewport(pScreen, viewportLastX - X.xmotion.x, viewportLastY - X.xmotion.y);
viewportLastX = X.xmotion.x;
viewportLastY = X.xmotion.y;
}
if (!nxagentOption(ViewOnly) && nxagentOption(Shadow) && !viewportCursor)
{
X.xmotion.x -= nxagentOption(RootX);
X.xmotion.y -= nxagentOption(RootY);
if (nxagentOption(XRatio) != DONT_SCALE)
{
X.xmotion.x = (X.xmotion.x << PRECISION) / nxagentOption(XRatio);
}
if (nxagentOption(YRatio) != DONT_SCALE)
{
X.xmotion.y = (X.xmotion.y << PRECISION) / nxagentOption(YRatio);
}
NXShadowEvent(nxagentDisplay, X);
}
if (!nxagentOption(Shadow))
{
nxagentInputEvent = 1;
}
break;
}
case FocusIn:
{
WindowPtr pWin;
#ifdef DEBUG
fprintf(stderr, "%s: Going to handle new FocusIn event [0x%lx] mode: [%s]\n", __func__, X.xfocus.window, nxagentGetNotifyMode(X.xfocus.mode));
{
XlibWindow w;
int revert_to;
XGetInputFocus(nxagentDisplay, &w, &revert_to);
fprintf(stderr, "%s: (FocusIn): Event win [0x%lx] Focus owner [0x%lx] nxagentDefaultWindows[0] [0x%x]\n", __func__, X.xfocus.window, w, nxagentDefaultWindows[0]);
}
#else
#ifdef TEST
fprintf(stderr, "%s: Going to handle new FocusIn event\n", __func__);
#endif
#endif
/*
* Here we change the focus state in the agent. It looks like
* this is needed only for rootless mode at present.
*/
if (nxagentOption(Rootless) &&
(pWin = nxagentWindowPtr(X.xfocus.window)))
{
SetInputFocus(serverClient, inputInfo.keyboard, pWin -> drawable.id,
RevertToPointerRoot, GetTimeInMillis(), False);
}
if (X.xfocus.detail != NotifyInferior)
{
pScreen = nxagentScreen(X.xfocus.window);
if (pScreen)
{
nxagentDirectInstallColormaps(pScreen);
}
}
if (nxagentOption(AutoGrab) && !(nxagentOption(AllScreens) || nxagentOption(Fullscreen) || nxagentOption(Rootless)))
{
if (X.xfocus.window == nxagentDefaultWindows[0] && X.xfocus.mode == NotifyNormal)
{
#if defined(DEBUG) || defined(DEBUG_AUTOGRAB)
fprintf(stderr, "%s: (FocusIn): grabbing\n", __func__);
#endif
nxagentGrabPointerAndKeyboard(NULL);
}
/* else
{
#if defined(DEBUG) || defined(DEBUG_AUTOGRAB)
fprintf(stderr, "%s: (FocusIn): ungrabbing\n", __func__);
#endif
nxagentUngrabPointerAndKeyboard(NULL);
}
*/
}
break;
}
case FocusOut:
{
#ifdef DEBUG
fprintf(stderr, "%s: Going to handle new FocusOut event [0x%lx] mode: [%s]\n", __func__, X.xfocus.window, nxagentGetNotifyMode(X.xfocus.mode));
#else
#ifdef TEST
fprintf(stderr, "%s: Going to handle new FocusOut event.\n", __func__);
#endif
#endif
if (X.xfocus.detail != NotifyInferior)
{
pScreen = nxagentScreen(X.xfocus.window);
if (pScreen)
{
nxagentDirectUninstallColormaps(pScreen);
}
}
#ifdef NXAGENT_FIXKEYS
{
/*
* Force the keys all up when focus is lost.
*/
for (int i = 0; i < DOWN_LENGTH; i++) /* input.h */
{
CARD8 val = inputInfo.keyboard->key->down[i];
if (val != 0)
{
for (int k = 0; k < 8; k++)
{
if (val & (1 << k))
{
#ifdef NXAGENT_FIXKEYS_DEBUG
fprintf(stderr, "sending KeyRelease event for keycode: %x\n",
i * 8 + k);
#endif
if (!nxagentOption(Rootless) ||
inputInfo.keyboard->key->modifierMap[i * 8 + k])
{
memset(&x, 0, sizeof(xEvent));
x.u.u.type = KeyRelease;
x.u.u.detail = i * 8 + k;
x.u.keyButtonPointer.time = nxagentLastEventTime = GetTimeInMillis();
if (!nxagentOption(ViewOnly) && nxagentOption(Shadow))
{
XEvent xM = {0};
xM.type = KeyRelease;
xM.xkey.display = nxagentDisplay;
xM.xkey.type = KeyRelease;
xM.xkey.keycode = i * 8 + k;
xM.xkey.state = inputInfo.keyboard->key->state;
xM.xkey.time = GetTimeInMillis();
NXShadowEvent(nxagentDisplay, xM);
}
mieqEnqueue(&x);
}
}
}
}
}
nxagentKeyDown = 0;
}
#endif /* NXAGENT_FIXKEYS */
if (nxagentOption(AutoGrab) && !nxagentFullscreenWindow)
{
XlibWindow w;
int revert_to;
XGetInputFocus(nxagentDisplay, &w, &revert_to);
if (w != nxagentDefaultWindows[0] && X.xfocus.mode == NotifyWhileGrabbed)
{
#if defined(DEBUG) || defined(DEBUG_AUTOGRAB)
fprintf(stderr, "%s: (FocusOut): ungrabbing\n", __func__);
#endif
nxagentUngrabPointerAndKeyboard(NULL);
}
}
break;
}
case KeymapNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new KeymapNotify event.\n", __func__);
#endif
break;
}
case EnterNotify:
{
WindowPtr pWin;
#ifdef TEST
fprintf(stderr, "%s: Going to handle new EnterNotify event.\n", __func__);
#endif
if (nxagentOption(Rootless))
{
WindowPtr pTLWin = NULL;
pWin = nxagentWindowPtr(X.xcrossing.window);
if (pWin != NULL)
{
for (pTLWin = pWin;
pTLWin -> parent != pTLWin -> drawable.pScreen -> root;
pTLWin = pTLWin -> parent);
}
if (pTLWin)
{
nxagentLastEnteredTopLevelWindow = pTLWin;
}
#ifdef TEST
fprintf(stderr, "%s: nxagentLastEnteredTopLevelWindow [%p].\n", __func__,
(void *)nxagentLastEnteredTopLevelWindow);
#endif
}
if (nxagentOption(Rootless) && nxagentWMIsRunning &&
(pWin = nxagentWindowPtr(X.xcrossing.window)) &&
nxagentWindowTopLevel(pWin) && !pWin -> overrideRedirect &&
(pWin -> drawable.x != X.xcrossing.x_root - X.xcrossing.x - pWin -> borderWidth ||
pWin -> drawable.y != X.xcrossing.y_root - X.xcrossing.y - pWin -> borderWidth))
{
/*
* This code is useful for finding the window position. It
* should be re-implemented by following the ICCCM 4.1.5
* recommendations.
*/
#ifdef TEST
fprintf(stderr, "%s: pWin -> drawable.x [%d] pWin -> drawable.y [%d].\n", __func__,
pWin -> drawable.x, pWin -> drawable.y);
#endif
XID values[4];
register XID *value = values;
*value++ = (XID) (X.xcrossing.x_root - X.xcrossing.x - pWin -> borderWidth);
*value++ = (XID) (X.xcrossing.y_root - X.xcrossing.y - pWin -> borderWidth);
/*
* nxagentWindowPriv(pWin)->x = (X.xcrossing.x_root - X.xcrossing.x);
* nxagentWindowPriv(pWin)->y = (X.xcrossing.y_root - X.xcrossing.y);
*/
Mask mask = CWX | CWY;
nxagentScreenTrap = True;
ConfigureWindow(pWin, mask, (XID *) values, wClient(pWin));
nxagentScreenTrap = False;
}
if (nxagentOption(Fullscreen) &&
X.xcrossing.window == nxagentFullscreenWindow &&
X.xcrossing.detail != NotifyInferior)
{
nxagentGrabPointerAndKeyboard(&X);
}
if (X.xcrossing.detail != NotifyInferior)
{
pScreen = nxagentScreen(X.xcrossing.window);
if (pScreen)
{
NewCurrentScreen(pScreen, X.xcrossing.x, X.xcrossing.y);
memset(&x, 0, sizeof(xEvent));
x.u.u.type = MotionNotify;
if (nxagentOption(Rootless))
{
nxagentLastEnteredWindow = nxagentWindowPtr(X.xcrossing.window);
x.u.keyButtonPointer.rootX = X.xcrossing.x_root;
x.u.keyButtonPointer.rootY = X.xcrossing.y_root;
}
else
{
x.u.keyButtonPointer.rootX = X.xcrossing.x - nxagentOption(RootX);
x.u.keyButtonPointer.rootY = X.xcrossing.y - nxagentOption(RootY);
}
x.u.keyButtonPointer.time = nxagentLastEventTime = GetTimeInMillis();
mieqEnqueue(&x);
nxagentDirectInstallColormaps(pScreen);
}
}
nxagentInputEvent = 1;
break;
}
case LeaveNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new LeaveNotify event.\n", __func__);
#endif
if (nxagentOption(Rootless) && X.xcrossing.mode == NotifyNormal &&
X.xcrossing.detail != NotifyInferior)
{
nxagentLastEnteredWindow = NULL;
}
if (!nxagentOption(AutoGrab))
{
if (X.xcrossing.window == nxagentDefaultWindows[0] &&
X.xcrossing.detail != NotifyInferior &&
X.xcrossing.mode == NotifyNormal)
{
nxagentUngrabPointerAndKeyboard(&X);
}
}
if (X.xcrossing.detail != NotifyInferior)
{
pScreen = nxagentScreen(X.xcrossing.window);
if (pScreen)
{
nxagentDirectUninstallColormaps(pScreen);
}
}
nxagentInputEvent = 1;
break;
}
case DestroyNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new DestroyNotify event.\n", __func__);
#endif
if (nxagentParentWindow != (Window) 0 &&
X.xdestroywindow.window == nxagentParentWindow)
{
fprintf(stderr, "Warning: Unhandled destroy notify event received in agent.\n");
}
break;
}
case ClientMessage:
{
enum HandleEventResult result;
#ifdef TEST
fprintf(stderr, "%s: Going to handle new ClientMessage event.\n", __func__);
#endif
nxagentHandleClientMessageEvent(&X, &result);
if (result == doCloseSession)
{
closeSession = TRUE;
}
break;
}
case VisibilityNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new VisibilityNotify event.\n", __func__);
#endif
if (X.xvisibility.window != nxagentDefaultWindows[0])
{
Window window = X.xvisibility.window;
WindowPtr pWin = nxagentWindowPtr(window);
if (pWin && nxagentWindowPriv(pWin))
{
if (nxagentWindowPriv(pWin) -> visibilityState != X.xvisibility.state)
{
int value = X.xvisibility.state;
if (nxagentOption(Rootless))
{
TraverseTree(pWin, nxagentChangeVisibilityPrivate, &value);
}
else
{
nxagentChangeVisibilityPrivate(pWin, &value);
}
}
}
#ifdef TEST
fprintf(stderr, "%s: Suppressing visibility notify on window [%lx].\n", __func__,
X.xvisibility.window);
#endif
break;
}
#ifdef TEST
fprintf(stderr, "%s: Visibility notify state is [%d] with previous [%d].\n", __func__,
X.xvisibility.state, nxagentVisibility);
#endif
nxagentVisibility = X.xvisibility.state;
break;
}
case Expose:
{
#ifdef DEBUG
fprintf(stderr, "%s: Going to handle new Expose event.\n", __func__);
fprintf(stderr, "%s: WARNING! Received Expose event for drawable [%lx]"
" geometry [%d, %d, %d, %d] count [%d].\n", __func__,
X.xexpose.window, X.xexpose.x, X.xexpose.y, X.xexpose.width,
X.xexpose.height, X.xexpose.count);
#endif
nxagentHandleExposeEvent(&X);
break;
}
case GraphicsExpose:
{
#ifdef DEBUG
fprintf(stderr, "%s: Going to handle new GraphicsExpose event.\n", __func__);
fprintf(stderr, "%s: WARNING! Received GraphicsExpose event "
"for drawable [%lx] geometry [%d, %d, %d, %d] count [%d].\n", __func__,
X.xgraphicsexpose.drawable, X.xgraphicsexpose.x, X.xgraphicsexpose.y,
X.xgraphicsexpose.width, X.xgraphicsexpose.height,
X.xgraphicsexpose.count);
#endif
nxagentHandleGraphicsExposeEvent(&X);
break;
}
case NoExpose:
{
#ifdef DEBUG
fprintf(stderr, "%s: Going to handle new NoExpose event.\n", __func__);
fprintf(stderr, "%s: WARNING! Received NoExpose event for drawable [%lx].\n", __func__, X.xnoexpose.drawable);
#endif
break;
}
case CirculateNotify:
{
#ifdef WARNING
fprintf(stderr, "%s: Going to handle new CirculateNotify event.\n", __func__);
#endif
/*
* FIXME: Do we need this?
*
* WindowPtr pWin = nxagentWindowPtr(X.xcirculate.window);
*
* if (!pWin)
* {
* pWin = nxagentRootlessTopLevelWindow(X.xcirculate.window);
* }
*
* if (!pWin)
* {
* break;
* }
*
* XQueryTree(nxagentDisplay, DefaultRootWindow(nxagentDisplay),
* &root_return, &parent_return, &children_return, &nchildren_return);
*
* nxagentRootlessRestack(children_return, nchildren_return);
*/
break;
}
case ConfigureNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new ConfigureNotify event.\n", __func__);
#endif
if (nxagentConfiguredSynchroWindow == X.xconfigure.window)
{
if (nxagentExposeQueue.exposures[nxagentExposeQueue.start].serial != X.xconfigure.x)
{
#ifdef WARNING
if (nxagentVerbose)
{
fprintf(stderr, "%s: Requested ConfigureNotify changes didn't take place.\n", __func__);
}
#endif
}
#ifdef TEST
fprintf(stderr, "%s: Received ConfigureNotify and going to call nxagentSynchronizeExpose.\n", __func__);
#endif
nxagentSynchronizeExpose();
break;
}
nxagentHandleConfigureNotify(&X);
break;
}
case GravityNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new GravityNotify event.\n", __func__);
#endif
break;
}
case ReparentNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new ReparentNotify event.\n", __func__);
#endif
nxagentHandleReparentNotify(&X);
break;
}
case UnmapNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new UnmapNotify event.\n", __func__);
#endif
if (nxagentOption(Rootless))
{
WindowPtr pWin;
if ((pWin = nxagentRootlessTopLevelWindow(X.xunmap.window)) != NULL ||
((pWin = nxagentWindowPtr(X.xunmap.window)) != NULL &&
nxagentWindowTopLevel(pWin)))
{
nxagentScreenTrap = True;
UnmapWindow(pWin, False);
nxagentScreenTrap = False;
}
}
if (nxagentUseNXTrans == 1 && !nxagentOption(Rootless) &&
!nxagentOption(Nested) &&
X.xmap.window != nxagentIconWindow)
{
nxagentVisibility = VisibilityFullyObscured;
}
break;
}
case MapNotify:
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle new MapNotify event.\n", __func__);
#endif
if (nxagentOption(Rootless))
{
WindowPtr pWin;
if ((pWin = nxagentRootlessTopLevelWindow(X.xmap.window)) != NULL ||
((pWin = nxagentWindowPtr(X.xmap.window)) != NULL &&
nxagentWindowTopLevel(pWin)))
{
nxagentScreenTrap = True;
MapWindow(pWin, wClient(pWin));
nxagentScreenTrap = False;
}
if (pWin != NULL)
{
Bool value = 1;
TraverseTree(pWin, nxagentChangeMapPrivate, &value);
}
}
if (nxagentOption(AllScreens))
{
if (X.xmap.window == nxagentIconWindow)
{
pScreen = nxagentScreen(X.xmap.window);
nxagentMaximizeToFullScreen(pScreen);
}
}
if (nxagentOption(Fullscreen))
{
nxagentVisibility = VisibilityUnobscured;
}
/*
* without window manager there will be no ConfigureNotify
* event that would trigger xinerama updates. So we do that
* once the nxagent window gets mapped.
*/
if (!nxagentWMIsRunning &&
X.xmap.window == nxagentDefaultWindows[nxagentScreen(X.xmap.window)->myNum])
{
nxagentChangeScreenConfig(nxagentScreen(X.xmap.window)->myNum, nxagentOption(Width),
nxagentOption(Height), True);
}
break;
}
case MappingNotify:
{
XMappingEvent *mappingEvent = (XMappingEvent *) &X;
#ifdef DEBUG
fprintf(stderr, "%s: WARNING! Going to handle new MappingNotify event.\n", __func__);
#endif
if (mappingEvent -> request == MappingPointer)
{
nxagentInitPointerMap();
}
break;
}
default:
{
/*
* Let's check if this is a XKB state modification event.
*/
if (nxagentHandleXkbKeyboardStateEvent(&X) == 0 && nxagentHandleXFixesSelectionNotify(&X) == 0)
{
#ifdef TEST
fprintf(stderr, "%s: WARNING! Unhandled event code [%d].\n", __func__, X.type);
#endif
}
break;
}
} /* End of switch (X.type) */
if (X.xany.serial < lastEventSerial)
{
/*
* Start over.
*/
nxagentDeleteStaticResizedWindow(0);
}
else
{
nxagentDeleteStaticResizedWindow(X.xany.serial - 1);
}
lastEventSerial = X.xany.serial;
} /* End of while (...) */
/*
* Send the exposed regions to the clients.
*/
nxagentForwardRemoteExpose();
/*
* Handle the agent window's changes.
*/
if (closeSession)
{
if (nxagentOption(Persistent))
{
if (nxagentNoDialogIsRunning)
{
nxagentLaunchDialog(DIALOG_SUSPEND_SESSION);
}
}
else
{
if (nxagentNoDialogIsRunning)
{
nxagentLaunchDialog(DIALOG_KILL_SESSION);
}
}
}
if (minimize)
{
nxagentWMDetect();
if (nxagentWMIsRunning)
{
if (nxagentOption(AllScreens))
{
nxagentMinimizeFromFullScreen(pScreen);
}
else
{
XIconifyWindow(nxagentDisplay, nxagentDefaultWindows[0],
DefaultScreen(nxagentDisplay));
}
}
}
if (switchFullscreen)
{
if (nxagentOption(AllScreens) && nxagentOption(Fullscreen))
{
nxagentSwitchAllScreens(pScreen, 0);
}
else
{
nxagentSwitchFullscreen(pScreen, !nxagentOption(Fullscreen));
}
}
if (switchAllScreens)
{
if (!nxagentOption(AllScreens) && nxagentOption(Fullscreen))
{
nxagentSwitchFullscreen(pScreen, False);
}
else
{
nxagentSwitchAllScreens(pScreen, !nxagentOption(AllScreens));
}
}
#ifdef BLOCKS
fprintf(stderr, "[End read]\n");
#endif
/*
* Let the underlying X server code process the input events.
*/
#ifdef BLOCKS
fprintf(stderr, "[Begin events]\n");
#endif
ProcessInputEvents();
#ifdef TEST
fprintf(stderr, "%s: Output pending flag is [%d] critical [%d].\n", __func__,
NewOutputPending, CriticalOutputPending);
#endif
/*
* Write the events to our clients. We may flush only in the case of
* critical output but this doesn't seem beneficial.
*
* FlushIfCriticalOutputPending();
*/
if (NewOutputPending == 1)
{
#ifdef TEST
fprintf(stderr, "%s: Flushed the processed events to clients.\n", __func__);
#endif
FlushAllOutput();
}
#ifdef TEST
if (nxagentPendingEvents(nxagentDisplay) > 0)
{
fprintf(stderr, "%s: WARNING! More events need to be dispatched.\n", __func__);
}
#endif
#ifdef BLOCKS
fprintf(stderr, "[End events]\n");
#endif
}
/*
* Functions providing the ad-hoc handling of the remote X events.
*/
int nxagentHandleKeyPress(XEvent *X, enum HandleEventResult *result)
{
if (!nxagentXkbState.Initialized)
{
if (X -> xkey.keycode == nxagentCapsLockKeycode)
{
nxagentXkbCapsTrap = True;
}
else if (X -> xkey.keycode == nxagentNumLockKeycode)
{
nxagentXkbNumTrap = True;
}
nxagentInitXkbKeyboardState();
nxagentXkbCapsTrap = False;
nxagentXkbNumTrap = False;
}
if (nxagentCheckSpecialKeystroke(&X -> xkey, result))
{
#ifdef TEST
fprintf(stderr, "%s: NOT passing KeyPress event to clients\n", __func__);
#endif
return 1;
}
if (X -> xkey.keycode == nxagentCapsLockKeycode)
{
nxagentXkbState.Caps = (~nxagentXkbState.Caps & 1);
}
else if (X -> xkey.keycode == nxagentNumLockKeycode)
{
nxagentXkbState.Num = (~nxagentXkbState.Num & 1);
}
nxagentLastServerTime = X -> xkey.time;
nxagentLastEventTime = nxagentLastKeyPressTime = GetTimeInMillis();
xEvent x = {0};
x.u.u.type = KeyPress;
x.u.u.detail = nxagentConvertKeycode(X -> xkey.keycode);
x.u.keyButtonPointer.time = nxagentLastKeyPressTime;
#ifdef TEST
fprintf(stderr, "%s: passing KeyPress event to clients\n", __func__);
#endif
mieqEnqueue(&x);
SetCriticalOutputPending();
return 1;
}
int nxagentHandlePropertyNotify(XEvent *X)
{
if (nxagentOption(Rootless) && !nxagentNotifyMatchChangeProperty((XPropertyEvent *) X))
{
#ifdef TEST
fprintf(stderr, "%s: Property %ld on window %lx.\n", __func__,
X -> xproperty.atom, X -> xproperty.window);
#endif
if (nxagentWindowPtr(X -> xproperty.window) != NULL)
{
int resource = NXGetCollectPropertyResource(nxagentDisplay);
if (resource == -1)
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING! Asynchronous get property queue is full.\n", __func__);
#endif
return 0;
}
NXCollectProperty(nxagentDisplay, resource,
X -> xproperty.window, X -> xproperty.atom, 0,
MAX_RETRIEVED_PROPERTY_SIZE, False, AnyPropertyType);
nxagentPropertyRequests[resource].window = X -> xproperty.window;
nxagentPropertyRequests[resource].property = X -> xproperty.atom;
}
#ifdef TEST
else
{
fprintf(stderr, "%s: Failed to look up remote window property.\n", __func__);
}
#endif
}
return 1;
}
int nxagentHandleExposeEvent(XEvent *X)
{
StaticResizedWindowStruct *resizedWinPtr = NULL;
#ifdef DEBUG
fprintf(stderr, "%s: Checking remote expose events.\n", __func__);
fprintf(stderr, "%s: Looking for window id [%ld].\n", __func__, X -> xexpose.window);
#endif
Window window = X -> xexpose.window;
WindowPtr pWin = nxagentWindowPtr(window);
if (pWin != NULL)
{
RegionRec sum;
RegionRec add;
BoxRec box;
RegionInit(&sum, (BoxRec *) NULL, 1);
/*
FIXME: This can be maybe optimized by consuming the
events that do not match the predicate.
*/
do
{
#ifdef DEBUG
fprintf(stderr, "%s: Adding event for window id [%ld].\n", __func__, X -> xexpose.window);
#endif
box.x1 = pWin -> drawable.x + wBorderWidth(pWin) + X -> xexpose.x;
box.y1 = pWin -> drawable.y + wBorderWidth(pWin) + X -> xexpose.y;
resizedWinPtr = nxagentFindStaticResizedWindow(X -> xany.serial);
while (resizedWinPtr)
{
if (resizedWinPtr -> pWin == pWin)
{
box.x1 += resizedWinPtr -> offX;
box.y1 += resizedWinPtr -> offY;
}
resizedWinPtr = resizedWinPtr -> prev;
}
box.x2 = box.x1 + X -> xexpose.width;
box.y2 = box.y1 + X -> xexpose.height;
RegionInit(&add, &box, 1);
RegionAppend(&sum, &add);
RegionUninit(&add);
if (X -> xexpose.count == 0)
{
break;
}
}
while (nxagentCheckEvents(nxagentDisplay, X, nxagentExposurePredicate,
(XPointer) &window) == 1);
int overlap = 0;
RegionValidate(&sum, &overlap);
RegionIntersect(&sum, &sum,
&pWin->drawable.pScreen->root->winSize);
#ifdef DEBUG
fprintf(stderr, "%s: Sending events for window id [%ld].\n", __func__, X -> xexpose.window);
#endif
/*
* If the agent has already sent auto-generated expose,
* save received exposes for later processing.
*/
int index = nxagentLookupByWindow(pWin);
if (index == -1)
{
miWindowExposures(pWin, &sum, NullRegion);
}
else
{
RegionTranslate(&sum, -pWin -> drawable.x, -pWin -> drawable.y);
if (nxagentExposeQueue.exposures[index].remoteRegion == NullRegion)
{
nxagentExposeQueue.exposures[index].remoteRegion = RegionCreate(NULL, 1);
}
RegionUnion(nxagentExposeQueue.exposures[index].remoteRegion,
nxagentExposeQueue.exposures[index].remoteRegion, &sum);
#ifdef TEST
fprintf(stderr, "%s: Added region for window [%u] to position [%d].\n", __func__,
nxagentWindow(pWin), index);
#endif
if (X -> xexpose.count == 0)
{
nxagentExposeQueue.exposures[index].remoteRegionIsCompleted = True;
}
else
{
nxagentExposeQueue.exposures[index].remoteRegionIsCompleted = False;
}
}
RegionUninit(&sum);
}
return 1;
}
int nxagentHandleGraphicsExposeEvent(XEvent *X)
{
/*
* Send an expose event to client, instead of graphics expose. If
* target drawable is a backing pixmap, send expose event for the
* saved window, else do nothing.
*/
StoringPixmapPtr pStoringPixmapRec = NULL;
miBSWindowPtr pBSwindow = NULL;
int drawableType;
WindowPtr pWin = nxagentWindowPtr(X -> xgraphicsexpose.drawable);
if (pWin != NULL)
{
drawableType = DRAWABLE_WINDOW;
}
else
{
drawableType = DRAWABLE_PIXMAP;
}
if (drawableType == DRAWABLE_PIXMAP)
{
pStoringPixmapRec = nxagentFindItemBSPixmapList(X -> xgraphicsexpose.drawable);
if (pStoringPixmapRec == NULL)
{
#ifdef TEST
fprintf(stderr, "%s: WARNING! Storing pixmap not found.\n", __func__);
#endif
return 1;
}
pBSwindow = (miBSWindowPtr) pStoringPixmapRec -> pSavedWindow -> backStorage;
if (pBSwindow == NULL)
{
#ifdef TEST
fprintf(stderr, "%s: WARNING! Back storage not found.\n", __func__);
#endif
return 1;
}
pWin = pStoringPixmapRec -> pSavedWindow;
}
/*
* Rectangle affected by GraphicsExpose event.
*/
BoxRec rect = {
.x1 = X -> xgraphicsexpose.x,
.y1 = X -> xgraphicsexpose.y,
.x2 = rect.x1 + X -> xgraphicsexpose.width,
.y2 = rect.y1 + X -> xgraphicsexpose.height,
};
RegionPtr exposeRegion = RegionCreate(&rect, 0);
if (drawableType == DRAWABLE_PIXMAP)
{
#ifdef TEST
fprintf(stderr, "%s: Handling GraphicsExpose event on pixmap with id [%lu].\n",
__func__, X -> xgraphicsexpose.drawable);
#endif
/*
* The exposeRegion coordinates are relative to the pixmap to
* which GraphicsExpose event refers. But the BS coordinates of
* the savedRegion are relative to the window.
*/
RegionTranslate(exposeRegion, pStoringPixmapRec -> backingStoreX,
pStoringPixmapRec -> backingStoreY);
/*
* We remove from SavedRegion the part affected by the
* GraphicsExpose event.
*/
RegionSubtract(&(pBSwindow -> SavedRegion), &(pBSwindow -> SavedRegion),
exposeRegion);
}
/*
* Store the exposeRegion in order to send the expose event
* later. The coordinates must be relative to the screen.
*/
RegionTranslate(exposeRegion, pWin -> drawable.x, pWin -> drawable.y);
RegionUnion(nxagentRemoteExposeRegion, nxagentRemoteExposeRegion, exposeRegion);
RegionDestroy(exposeRegion);
return 1;
}
int nxagentHandleClientMessageEvent(XEvent *X, enum HandleEventResult *result)
{
*result = doNothing;
/*
* If window is 0, message_type is 0 and format is 32 then we assume
* event is coming from proxy.
*/
if (X -> xclient.window == 0 &&
X -> xclient.message_type == 0 &&
X -> xclient.format == 32)
{
#ifdef TEST
fprintf(stderr, "%s: got nxproxy event\n", __func__);
#endif
nxagentHandleProxyEvent(X);
return 1;
}
#ifdef TEST
char * name = XGetAtomName(nxagentDisplay, X -> xclient.message_type);
fprintf(stderr, "nxagentHandleClientMessageEvent: ClientMessage event window [0x%lx] with "
"message_type [%ld][%s] format [%d] type [%d] source_indication [%ld][%s] timestamp [%ld] "
"curwin [0x%lx].\n", X -> xclient.window, X -> xclient.message_type, name,
X -> xclient.format, X -> xclient.type, X -> xclient.data.l[0],
X -> xclient.data.l[0] == 1 ? "'application'" : X -> xclient.data.l[0] == 1 ? "'pager'" : "'none (old spec)'",
X -> xclient.data.l[1], X -> xclient.data.l[2]);
SAFE_XFree(name);
#endif
if (nxagentOption(Rootless))
{
Atom message_type = nxagentRemoteToLocalAtom(X -> xclient.message_type);
if (!ValidAtom(message_type))
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING Invalid type in client message.\n", __func__);
#endif
return 0;
}
WindowPtr pWin = nxagentWindowPtr(X -> xclient.window);
if (pWin == NULL)
{
/*
* If some window on the real X server sends a
* _NET_ACTIVE_WINDOW ClientMessage to indicate the active
* window that window will be one not belonging to nxagent so
* this situation is perfectly legal. For all other situations
* we print a warning.
*/
#ifdef WARNING
if (message_type != MakeAtom("_NET_ACTIVE_WINDOW", strlen("_NET_ACTIVE_WINDOW"), False))
{
fprintf(stderr, "WARNING: Invalid window in ClientMessage xclient.window [0x%lx].\n", X->xclient.window);
}
#endif
return 0;
}
if (message_type == MakeAtom("WM_PROTOCOLS", strlen("WM_PROTOCOLS"), False))
{
xEvent x = {0};
x.u.u.type = ClientMessage;
x.u.u.detail = X -> xclient.format;
x.u.clientMessage.window = pWin -> drawable.id;
x.u.clientMessage.u.l.type = message_type;
x.u.clientMessage.u.l.longs0 = nxagentRemoteToLocalAtom(X -> xclient.data.l[0]);
x.u.clientMessage.u.l.longs1 = GetTimeInMillis();
if (!ValidAtom(x.u.clientMessage.u.l.longs0))
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING Invalid value in client message of type WM_PROTOCOLS.\n", __func__);
#endif
return 0;
}
#ifdef TEST
else
{
fprintf(stderr, "%s: Sent client message of type WM_PROTOCOLS and value [%s].\n", __func__,
validateString(NameForAtom(x.u.clientMessage.u.l.longs0)));
}
#endif
TryClientEvents(wClient(pWin), &x, 1, 1, 1, 0);
}
else
{
#ifdef WARNING
fprintf(stderr, "%s: Ignored message type %ld [%s].\n", __func__,
(long int) message_type, validateString(NameForAtom(message_type)));
#endif
return 0;
}
return 1;
}
if (X -> xclient.message_type == nxagentAtoms[1]) /* WM_PROTOCOLS */
{
Atom wmAtom = (Atom) X -> xclient.data.l[0];
Atom deleteWMatom = nxagentAtoms[2]; /* WM_DELETE_WINDOW */
if (wmAtom == deleteWMatom)
{
if (nxagentOnce && (nxagentClients == 0))
{
GiveUp(0);
}
else
{
#ifdef TEST
fprintf(stderr, "%s: WM_DELETE_WINDOW arrived Atom = %u.\n", __func__, wmAtom);
#endif
if (X -> xclient.window == nxagentIconWindow)
{
XMapRaised(nxagentDisplay, nxagentFullscreenWindow);
XIconifyWindow(nxagentDisplay, nxagentIconWindow,
DefaultScreen(nxagentDisplay));
}
if (X -> xclient.window == (nxagentOption(Fullscreen) ?
nxagentIconWindow : nxagentDefaultWindows[0]) ||
!nxagentWMIsRunning)
{
*result = doCloseSession;
}
}
}
}
return 1;
}
int nxagentHandleXkbKeyboardStateEvent(XEvent *X)
{
XkbEvent *xkbev = (XkbEvent *) X;
if (nxagentXkbInfo.EventBase != -1 &&
xkbev -> type == nxagentXkbInfo.EventBase + XkbEventCode &&
xkbev -> any.xkb_type == XkbStateNotify)
{
#ifdef TEST
fprintf(stderr, "%s: Handling event with caps [%d] num [%d] locked [%d].\n", __func__,
nxagentXkbState.Caps, nxagentXkbState.Num, nxagentXkbState.Locked);
#endif
nxagentXkbState.Locked = xkbev -> state.locked_mods;
#ifdef TEST
fprintf(stderr, "%s: Updated XKB locked modifier bits to [%x].\n", __func__,
nxagentXkbState.Locked);
#endif
nxagentXkbState.Initialized = True;
if (nxagentXkbState.Caps == 0 &&
(nxagentXkbState.Locked & CAPSFLAG_IN_EVENT))
{
nxagentXkbState.Caps = 1;
#ifdef TEST
fprintf(stderr, "%s: Sending fake key [66] to engage capslock.\n", __func__);
#endif
if (!nxagentXkbCapsTrap)
{
nxagentSendFakeKey(66);
}
}
if (nxagentXkbState.Caps == 1 &&
!(nxagentXkbState.Locked & CAPSFLAG_IN_EVENT))
{
nxagentXkbState.Caps = 0;
#ifdef TEST
fprintf(stderr, "%s: Sending fake key [66] to release capslock.\n", __func__);
#endif
nxagentSendFakeKey(66);
}
if (nxagentXkbState.Caps == 0 &&
!(nxagentXkbState.Locked & CAPSFLAG_IN_EVENT) &&
nxagentXkbCapsTrap)
{
#ifdef TEST
fprintf(stderr, "%s: Sending fake key [66] to release capslock.\n", __func__);
#endif
nxagentSendFakeKey(66);
}
if (nxagentXkbState.Num == 0 &&
(nxagentXkbState.Locked & NUMFLAG_IN_EVENT))
{
nxagentXkbState.Num = 1;
#ifdef TEST
fprintf(stderr, "%s: Sending fake key [77] to engage numlock.\n", __func__);
#endif
if (!nxagentXkbNumTrap)
{
nxagentSendFakeKey(77);
}
}
if (nxagentXkbState.Num == 1 &&
!(nxagentXkbState.Locked & NUMFLAG_IN_EVENT))
{
nxagentXkbState.Num = 0;
#ifdef TEST
fprintf(stderr, "%s: Sending fake key [77] to release numlock.\n", __func__);
#endif
nxagentSendFakeKey(77);
}
if (nxagentXkbState.Num == 0 &&
!(nxagentXkbState.Locked & NUMFLAG_IN_EVENT) &&
nxagentXkbNumTrap)
{
#ifdef TEST
fprintf(stderr, "%s: Sending fake key [77] to release numlock.\n", __func__);
#endif
nxagentSendFakeKey(77);
}
return 1;
}
return 0;
}
int nxagentHandleXFixesSelectionNotify(XEvent *X)
{
XFixesSelectionEvent *xfixesEvent = (XFixesSelectionEvent *) X;
if (!nxagentXFixesInfo.Initialized)
{
#ifdef DEBUG
fprintf(stderr, "%s: XFixes not initialized - doing nothing.\n", __func__);
#endif
return 0;
}
if (xfixesEvent -> type != (nxagentXFixesInfo.EventBase + XFixesSelectionNotify))
{
#ifdef DEBUG
fprintf(stderr, "%s: event type is [%d] - doing nothing.\n", __func__, xfixesEvent->type);
#endif
return 0;
}
#ifdef DEBUG
fprintf(stderr, "---------\n");
#endif
#ifdef TEST
fprintf(stderr, "%s: Handling event.\n", __func__);
#endif
#ifdef DEBUG
fprintf(stderr, "%s: Event timestamp [%ld]\n", __func__, xfixesEvent->xfixesselection.timestamp);
fprintf(stderr, "%s: Event selection timestamp [%ld]\n", __func__, xfixesEvent->xfixesselection.selection_timestamp);
fprintf(stderr, "%s: Event selection window [0x%lx]\n", __func__, xfixesEvent->xfixesselection.window);
fprintf(stderr, "%s: Event selection owner [0x%lx]\n", __func__, xfixesEvent->xfixesselection.owner);
fprintf(stderr, "%s: Event selection [%s]\n", __func__, NameForAtom(nxagentRemoteToLocalAtom(xfixesEvent->xfixesselection.selection)));
fprintf(stderr, "%s: Subtype ", __func__);
switch (xfixesEvent -> xfixesselection.subtype)
{
case SelectionSetOwner: fprintf(stderr, "SelectionSetOwner.\n"); break;
case SelectionWindowDestroy: fprintf(stderr, "SelectionWindowDestroy.\n"); break;
case SelectionClientClose: fprintf(stderr, "SelectionClientClose.\n"); break;
default: fprintf(stderr, ".\n"); break;
}
#endif
if (xfixesEvent->xfixesselection.owner && xfixesEvent->xfixesselection.owner == nxagentWindow(screenInfo.screens[0]->root))
{
/*
* This is an event that must have been triggered by nxagent itself
* - by calling XSetSelectionOwner(). As this is no news for us we
* can ignore the event.
*/
#ifdef DEBUG
fprintf(stderr, "%s: (new) owner is nxagent (window is [0x%lx]) - ignoring it.\n", __func__, xfixesEvent->xfixesselection.window);
#endif
return 0;
}
/*
* Realistically the only situation where we can receive
* WindowDestroy or ClientClose will also end nxagent, so we do not
* need to handle them. But the code is here, so let's keep it.
*/
if (xfixesEvent -> xfixesselection.subtype == SelectionSetOwner||
xfixesEvent -> xfixesselection.subtype == SelectionWindowDestroy ||
xfixesEvent -> xfixesselection.subtype == SelectionClientClose)
{
/*
* Reception of an owner change on the real X server is - for nxagent - the same as
* receiving a SelectionClear event. We just need to tell a (possible) internal
* owner that it is no longer owning the selection.
*/
nxagentHandleSelectionClearFromXServerByAtom(xfixesEvent -> xfixesselection.selection);
}
else
{
#ifdef DEBUG
fprintf(stderr, "%s: WARNING unexpected xfixesselection subtype [%d]\n", __func__, xfixesEvent -> xfixesselection.subtype);
#endif
}
return 1;
}
int nxagentHandleProxyEvent(XEvent *X)
{
switch (X -> xclient.data.l[0])
{
case NXNoSplitNotify:
case NXStartSplitNotify:
{
/*
* We should never receive such events in the event loop, as
* they should be caught at the time the split is initiated.
*/
#ifdef PANIC
int client = (int) X -> xclient.data.l[1];
if (X -> xclient.data.l[0] == NXNoSplitNotify)
{
fprintf(stderr, "%s: PANIC! NXNoSplitNotify received with client [%d].\n", __func__, client);
}
else
{
fprintf(stderr, "%s: PANIC! NXStartSplitNotify received with client [%d].\n", __func__, client);
}
#endif
return 1;
}
case NXCommitSplitNotify:
{
/*
* We need to commit an image. Image can be the result of a
* PutSubImage() generated by Xlib, so there can be more than a
* single image to commit, even if only one PutImage was perfor-
* med by the agent.
*/
int client = (int) X -> xclient.data.l[1];
int request = (int) X -> xclient.data.l[2];
int position = (int) X -> xclient.data.l[3];
#ifdef TEST
fprintf(stderr, "%s: NXCommitSplitNotify received with client [%d]"
" request [%d] and position [%d].\n", __func__,
client, request, position);
#endif
nxagentHandleCommitSplitEvent(client, request, position);
return 1;
}
case NXEndSplitNotify:
{
/*
* All images for the split were transferred and we need to
* restart the client.
*/
int client = (int) X -> xclient.data.l[1];
#ifdef TEST
fprintf(stderr, "%s: NXEndSplitNotify received with client [%d].\n", __func__, client);
#endif
nxagentHandleEndSplitEvent(client);
return 1;
}
case NXEmptySplitNotify:
{
/*
* All splits have been completed and none remain.
*/
#ifdef TEST
fprintf(stderr, "%s: NXEmptySplitNotify received.\n", __func__);
#endif
nxagentHandleEmptySplitEvent();
return 1;
}
case NXCollectPropertyNotify:
{
#ifdef TEST
int resource = (int) X -> xclient.data.l[1];
fprintf(stderr, "%s: NXCollectPropertyNotify received with resource [%d].\n", __func__, resource);
#endif
nxagentHandleCollectPropertyEvent(X);
return 1;
}
case NXCollectGrabPointerNotify:
{
int resource = (int) X -> xclient.data.l[1];
#ifdef TEST
fprintf(stderr, "%s: NXCollectGrabPointerNotify received with resource [%d].\n", __func__, resource);
#endif
nxagentHandleCollectGrabPointerEvent(resource);
return 1;
}
case NXCollectInputFocusNotify:
{
int resource = (int) X -> xclient.data.l[1];
/*
* This is not used at the present moment.
*/
#ifdef TEST
fprintf(stderr, "%s: NXCollectInputFocusNotify received with resource [%d].\n", __func__, resource);
#endif
nxagentHandleCollectInputFocusEvent(resource);
return 1;
}
default:
{
/*
* Not a recognized ClientMessage event.
*/
#ifdef WARNING
fprintf(stderr, "%s: WARNING! Not a recognized ClientMessage proxy event [%d].\n", __func__,
(int) X -> xclient.data.l[0]);
#endif
return 0;
}
}
}
/*
* In this function it is assumed that we never get a configure with
* both stacking order and geometry changed, this way we can ignore
* stacking changes if the geometry has changed.
*/
int nxagentCheckWindowConfiguration(XConfigureEvent* X)
{
static int x = 0;
static int y = 0;
static int width = 0;
static int height = 0;
static Window win = None;
Bool geometryChanged = False;
XlibWindow root_return = 0;
XlibWindow parent_return = 0;
XlibWindow *children_return = NULL;
unsigned int nchildren_return = 0;
Status result;
if (win == X -> window)
{
if (x != X -> x ||
y != X -> y ||
width != X -> width ||
height != X -> height)
{
geometryChanged = True;
}
}
win = X -> window;
x = X -> x;
y = X -> y;
width = X -> width;
height = X -> height;
if (geometryChanged)
{
#ifdef TEST
fprintf(stderr, "%s: Configure frame. No restack.\n", __func__);
#endif
return 1;
}
#ifdef TEST
{
fprintf(stderr, "%s: Before restacking top level window [%p]\n", __func__,
(void *) nxagentWindowPtr(X -> window));
for (WindowPtr pSib = screenInfo.screens[0]->root -> firstChild; pSib; pSib = pSib -> nextSib)
{
fprintf(stderr, "%s: Top level window: [%p].\n", __func__, (void *) pSib);
}
}
#endif
result = XQueryTree(nxagentDisplay, DefaultRootWindow(nxagentDisplay),
&root_return, &parent_return, &children_return, &nchildren_return);
if (result)
{
nxagentRootlessRestack(children_return, nchildren_return);
}
else
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING! Failed QueryTree request.\n", __func__);
#endif
}
SAFE_XFree(children_return);
#if 0
fprintf(stderr, "%s: Trees match: %s\n", __func__, nxagentRootlessTreesMatch() ? "Yes" : "No");
#endif
return 1;
}
int nxagentHandleConfigureNotify(XEvent* X)
{
#ifdef DEBUG
fprintf(stderr, "%s: Event info:\n", __func__);
fprintf(stderr, "%s: X->serial [%ld]\n", __func__, X->xconfigure.serial);
fprintf(stderr, "%s: X->override_redirect [%d]\n", __func__, X->xconfigure.override_redirect);
fprintf(stderr, "%s: X->border_width [%d]\n", __func__, X->xconfigure.border_width);
fprintf(stderr, "%s: X->send_event [%d]\n", __func__, X->xconfigure.send_event);
fprintf(stderr, "%s: X->window [0x%lx]\n", __func__, X->xconfigure.window);
fprintf(stderr, "%s: X->event [0x%lx]\n", __func__, X->xconfigure.event);
fprintf(stderr, "%s: X->x, X->y [%d][%d]\n", __func__, X->xconfigure.x, X->xconfigure.y);
fprintf(stderr, "%s: X->width, X->height [%d][%d]\n", __func__, X->xconfigure.width, X->xconfigure.height);
fprintf(stderr, "%s: References:\n", __func__);
fprintf(stderr, "%s: DefaultWindow[0]: [0x%x]\n", __func__, nxagentDefaultWindows[0]);
fprintf(stderr, "%s: DefaultRootWindow(DISPLAY) [0x%lx]\n", __func__, DefaultRootWindow(nxagentDisplay));
#endif
if (nxagentOption(Rootless))
{
int sendEventAnyway = 0;
WindowPtr pWinWindow = nxagentWindowPtr(X -> xconfigure.window);
#ifdef TEST
{
WindowPtr pWinEvent = nxagentWindowPtr(X -> xconfigure.event);
fprintf(stderr, "%s: Generating window is [%p][%ld] target [%p][%ld].\n", __func__,
(void *) pWinEvent, X -> xconfigure.event, (void *) pWinWindow, X -> xconfigure.window);
}
#endif
#ifdef TEST
fprintf(stderr, "%s: New configuration for window [%p][%ld] is [%d][%d][%d][%d] send_event [%i].\n",
__func__, (void *) pWinWindow, X -> xconfigure.window,
X -> xconfigure.x, X -> xconfigure.y, X -> xconfigure.width,
X -> xconfigure.height, X -> xconfigure.send_event);
#endif
WindowPtr pWin = nxagentRootlessTopLevelWindow(X -> xconfigure.window);
if (pWin != NULL)
{
/*
* Checking for new geometry or stacking order changes.
*/
nxagentCheckWindowConfiguration((XConfigureEvent*)X);
return 1;
}
if (nxagentWindowTopLevel(pWinWindow) && !X -> xconfigure.override_redirect)
{
XID values[5];
register XID *value = values;
Mask mask = CWHeight | CWWidth | CWBorderWidth;
/* FIXME: override_redirect is always FALSE here */
if (X -> xconfigure.send_event || !nxagentWMIsRunning ||
X -> xconfigure.override_redirect)
{
*value++ = (XID)X -> xconfigure.x;
*value++ = (XID)X -> xconfigure.y;
/*
* nxagentWindowPriv(pWinWindow)->x = X -> xconfigure.x;
* nxagentWindowPriv(pWinWindow)->y = X -> xconfigure.y;
*/
mask |= CWX | CWY;
}
*value++ = (XID)X -> xconfigure.width;
*value++ = (XID)X -> xconfigure.height;
*value++ = (XID)X -> xconfigure.border_width;
/*
* We don't need width and height here.
*
* nxagentWindowPriv(pWinWindow)->width = X -> xconfigure.width;
* nxagentWindowPriv(pWinWindow)->height = X -> xconfigure.height;
*/
nxagentScreenTrap = True;
ConfigureWindow(pWinWindow, mask, (XID *) values, wClient(pWinWindow));
nxagentScreenTrap = False;
nxagentCheckWindowConfiguration((XConfigureEvent*)X);
/*
* This workaround should help with Java 1.6.0 that seems to
* ignore non-synthetic events.
*/
if (nxagentOption(ClientOs) == ClientOsWinnt)
{
#ifdef TEST
fprintf(stderr, "%s: Apply workaround for NXWin.\n", __func__);
#endif
sendEventAnyway = 1;
}
if (sendEventAnyway || X -> xconfigure.send_event)
{
xEvent x = {0};
x.u.u.type = X -> xconfigure.type | 0x80;
x.u.configureNotify.event = pWinWindow -> drawable.id;
x.u.configureNotify.window = pWinWindow -> drawable.id;
if (pWinWindow -> nextSib)
{
x.u.configureNotify.aboveSibling = pWinWindow -> nextSib -> drawable.id;
}
else
{
x.u.configureNotify.aboveSibling = None;
}
x.u.configureNotify.x = X -> xconfigure.x;
x.u.configureNotify.y = X -> xconfigure.y;
x.u.configureNotify.width = X -> xconfigure.width;
x.u.configureNotify.height = X -> xconfigure.height;
x.u.configureNotify.borderWidth = X -> xconfigure.border_width;
x.u.configureNotify.override = X -> xconfigure.override_redirect;
TryClientEvents(wClient(pWinWindow), &x, 1, 1, 1, 0);
}
return 1;
}
}
else /* (nxagentOption(Rootless)) */
{
/*
* Save the position of the agent default window. Don't save the
* values if the agent is in fullscreen mode.
*
* If we use these values to restore the position of a window
* after that we have dynamically changed the fullscreen
* attribute, depending on the behaviour of window manager, we
* could be not able to place the window exactly in the requested
* position, so let the window manager do the job for us.
*/
ScreenPtr pScreen = nxagentScreen(X -> xconfigure.window);
Bool doRandR = False;
if (X -> xconfigure.window == nxagentDefaultWindows[pScreen -> myNum])
{
if (!nxagentOption(AllScreens))
{
/*
* - WITHOUT window manager any position change is relevant
* - WITH window manager only synthetic position changes sent
* by the window manager are relevant, see ICCCM Chapter 4,
* "Configuring the Window"
*/
Bool updatePos = (!nxagentWMIsRunning || X -> xconfigure.send_event != 0);
int newX = X -> xconfigure.x;
int newY = X -> xconfigure.y;
if (nxagentOption(DesktopResize))
{
if (nxagentOption(Width) != X -> xconfigure.width ||
nxagentOption(Height) != X -> xconfigure.height ||
(updatePos && (nxagentOption(X) != newX ||
nxagentOption(Y) != newY)))
{
#ifdef DEBUG
int count = 0;
#endif
Bool newEvents = False;
doRandR = True;
NXFlushDisplay(nxagentDisplay, NXFlushLink);
do
{
newEvents = False;
nxagentWaitEvents(nxagentDisplay, 500);
/*
* This should also flush the NX link for us.
*/
XSync(nxagentDisplay, 0);
while (XCheckTypedWindowEvent(nxagentDisplay, nxagentDefaultWindows[pScreen -> myNum],
ConfigureNotify, X))
{
#ifdef DEBUG
count++;
#endif
if (!nxagentWMIsRunning || X -> xconfigure.send_event)
{
updatePos = True;
#ifdef DEBUG
fprintf(stderr, "%s: Accumulating event %d: x [%d] y [%d] width [%d] height [%d]\n", __func__, count,
X -> xconfigure.x, X -> xconfigure.y, X -> xconfigure.width, X -> xconfigure.height);
#endif
newX = X -> xconfigure.x;
newY = X -> xconfigure.y;
}
newEvents = True;
}
} while (newEvents);
#ifdef DEBUG
fprintf(stderr, "%s: accumulated %d events\n", __func__, count);
#endif
}
}
if (updatePos && (nxagentOption(X) != newX || nxagentOption(Y) != newY))
{
#ifdef DEBUG
fprintf(stderr, "%s: Updating nxagent window position [%d,%d] -> [%d,%d]\n", __func__,
nxagentOption(X), nxagentOption(Y), newX, newY);
#endif
nxagentChangeOption(X, newX);
nxagentChangeOption(Y, newY);
}
if (nxagentOption(Shadow) && nxagentOption(DesktopResize) &&
(nxagentOption(Width) != X -> xconfigure.width ||
nxagentOption(Height) != X -> xconfigure.height))
{
nxagentShadowResize = True;
}
if (nxagentOption(Width) != X->xconfigure.width || nxagentOption(Height) != X->xconfigure.height)
{
#ifdef DEBUG
fprintf(stderr, "%s: Updating width and height [%d,%d] -> [%d,%d]\n", __func__,
nxagentOption(Width), nxagentOption(Height),
X->xconfigure.width, X->xconfigure.height);
#endif
nxagentChangeOption(Width, X -> xconfigure.width);
nxagentChangeOption(Height, X -> xconfigure.height);
}
nxagentChangeOption(ViewportXSpan, (int) X -> xconfigure.width -
(int) nxagentOption(RootWidth));
nxagentChangeOption(ViewportYSpan, (int) X -> xconfigure.height -
(int) nxagentOption(RootHeight));
nxagentMoveViewport(pScreen, 0, 0);
/* if in shadowing mode or if neither size nor position have
changed we do not need to adjust RandR */
/* FIXME: Comment makes no sense */
if (nxagentOption(Shadow) ||
(nxagentOption(Width) == nxagentOption(RootWidth) &&
nxagentOption(Height) == nxagentOption(RootHeight) &&
nxagentOption(X) == nxagentOption(RootX) &&
nxagentOption(Y) == nxagentOption(RootY)))
{
doRandR = False;
}
XMoveResizeWindow(nxagentDisplay, nxagentInputWindows[0], 0, 0,
X -> xconfigure.width, X -> xconfigure.height);
if (!nxagentOption(Fullscreen))
{
/* FIXME: has already been done some lines above */
nxagentMoveViewport(pScreen, 0, 0);
}
else
{
nxagentChangeOption(RootX, (nxagentOption(Width) -
nxagentOption(RootWidth)) / 2);
nxagentChangeOption(RootY, (nxagentOption(Height) -
nxagentOption(RootHeight)) / 2);
nxagentChangeOption(ViewportXSpan, nxagentOption(Width) -
nxagentOption(RootWidth));
nxagentChangeOption(ViewportYSpan, nxagentOption(Height) -
nxagentOption(RootHeight));
nxagentUpdateViewportFrame(0, 0, nxagentOption(RootWidth),
nxagentOption(RootHeight));
XMoveWindow(nxagentDisplay, nxagentWindow(pScreen->root),
nxagentOption(RootX), nxagentOption(RootY));
}
if (doRandR)
{
#ifdef TEST
fprintf(stderr,"%s: Width %d Height %d.\n", __func__,
nxagentOption(Width), nxagentOption(Height));
#endif
/*
* we are processing a ConfigureNotifyEvent that brought us
* the current window size. If we issue a XResizeWindow()
* again with these values we might end up in loop if the
* window manager adjusts the size, which is perfectly
* legal for it to do. So we prevent the XResizeWindow call
* from happening.
*/
nxagentChangeScreenConfig(0, nxagentOption(Width),
nxagentOption(Height), False);
}
}
return 1;
}
else
{
if ( (X -> xconfigure.window == DefaultRootWindow(nxagentDisplay)) || nxagentFullscreenWindow )
{
#ifdef TEST
fprintf(stderr, "%s: remote root window has changed: %d,%d %dx%d\n", __func__,
X -> xconfigure.x, X -> xconfigure.y, X -> xconfigure.width, X -> xconfigure.height);
#endif
nxagentChangeOption(RootX, X -> xconfigure.x);
nxagentChangeOption(RootY, X -> xconfigure.y);
nxagentChangeOption(RootWidth, X -> xconfigure.width);
nxagentChangeOption(RootHeight, X -> xconfigure.height);
nxagentChangeScreenConfig(0, nxagentOption(Width),
nxagentOption(Height), True);
return 1;
}
}
}
#ifdef TEST
fprintf(stderr, "%s: received for unexpected window [%ld]\n", __func__, X -> xconfigure.window);
#endif
return 0;
}
int nxagentHandleReparentNotify(XEvent* X)
{
#ifdef TEST
fprintf(stderr, "%s: Going to handle a new reparent event (serial [%ld].\n", __func__, X->xreparent.serial);
#endif
#ifdef DEBUG
fprintf(stderr, "%s: Event info:\n", __func__);
fprintf(stderr, "%s: X->send_event [%d]\n", __func__, X->xreparent.send_event);
fprintf(stderr, "%s: X->event [0x%lx]\n", __func__, X->xreparent.event);
fprintf(stderr, "%s: X->window [0x%lx]\n", __func__, X->xreparent.window);
fprintf(stderr, "%s: X->parent [0x%lx]\n", __func__, X->xreparent.parent);
fprintf(stderr, "%s: X->x, X->y [%d][%d]\n", __func__, X->xreparent.x, X->xreparent.y);
fprintf(stderr, "%s: X->override_redirect [%d]\n", __func__, X->xreparent.override_redirect);
fprintf(stderr, "%s: References:\n", __func__);
fprintf(stderr, "%s: DefaultWindow[0]: [0x%x]\n", __func__, nxagentDefaultWindows[0]);
fprintf(stderr, "%s: RootWindow(DISPLAY, 0): [0x%lx]\n", __func__, RootWindow(nxagentDisplay, 0));
fprintf(stderr, "%s: DefaultRootWindow(DISPLAY): [0x%lx]\n", __func__, DefaultRootWindow(nxagentDisplay));
#endif
if (nxagentOption(Rootless))
{
WindowPtr pWin = nxagentWindowPtr(X -> xreparent.window);
#ifdef TEST
{
WindowPtr pParent = nxagentWindowPtr(X -> xreparent.parent);
WindowPtr pEvent = nxagentWindowPtr(X -> xreparent.event);
fprintf(stderr, "%s: event %p[%lx] window %p[%lx] parent %p[%lx] at (%d, %d)\n", __func__,
(void*)pEvent, X -> xreparent.event, (void*)pWin, X -> xreparent.window,
(void*)pParent, X -> xreparent.parent, X -> xreparent.x, X -> xreparent.y);
}
#endif
if (nxagentWindowTopLevel(pWin))
{
/*
* If the window manager reparents our top level window, we need
* to know the new top level ancestor.
*/
XlibWindow w = None;
XlibWindow root_return = 0;
XlibWindow *children_return = NULL;
unsigned int nchildren_return = 0;
Status result;
XlibWindow parent_return = X -> xreparent.parent;
while (parent_return != RootWindow(nxagentDisplay, 0))
{
w = parent_return;
result = XQueryTree(nxagentDisplay, w, &root_return,
&parent_return, &children_return, &nchildren_return);
SAFE_XFree(children_return);
if (!result)
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING! Failed QueryTree request.\n", __func__);
#endif
break;
}
}
if (w && !nxagentWindowPtr(w))
{
XSelectInput(nxagentDisplay, w, StructureNotifyMask);
nxagentRootlessAddTopLevelWindow(pWin, w);
#ifdef TEST
fprintf(stderr, "%s: new top level window [%ld].\n", __func__, w);
fprintf(stderr, "%s: reparented window [%ld].\n", __func__, X -> xreparent.window);
#endif
result = XQueryTree(nxagentDisplay, DefaultRootWindow(nxagentDisplay),
&root_return, &parent_return, &children_return, &nchildren_return);
if (result)
{
nxagentRootlessRestack(children_return, nchildren_return);
}
else
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING! Failed QueryTree request.\n", __func__);
#endif
}
SAFE_XFree(children_return);
}
else
{
#ifdef TEST
fprintf(stderr, "%s: Window at [%p] has been reparented to [%ld] top level parent [%ld].\n",
__func__, (void *) pWin, X -> xreparent.parent, w);
#endif
nxagentRootlessDelTopLevelWindow(pWin);
}
}
return 1;
}
else if (nxagentWMIsRunning && !nxagentOption(Fullscreen) &&
nxagentOption(WMBorderWidth) == -1)
{
/*
* Calculate the absolute upper-left X e Y
*/
XlibWindow parent = X -> xreparent.parent;
XWindowAttributes attributes;
if ((XGetWindowAttributes(nxagentDisplay, parent, &attributes) == 0))
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING! XGetWindowAttributes for parent window failed.\n", __func__);
#endif
return 1;
}
XlibWindow junk;
int x = attributes.x;
int y = attributes.y;
#ifdef DEBUG
int before_x = x;
int before_y = y;
#endif
XTranslateCoordinates(nxagentDisplay, parent,
attributes.root, -attributes.border_width,
-attributes.border_width, &x, &y, &junk);
#ifdef DEBUG
fprintf(stderr, "%s: translated coordinates x,y [%d,%d] -> [%d,%d].\n", __func__, before_x, before_y, x, y);
#endif
/*
* Calculate the parent X and parent Y.
*/
if (parent != DefaultRootWindow(nxagentDisplay))
{
XlibWindow rootReturn = 0;
XlibWindow parentReturn = 0;
XlibWindow *childrenReturn = NULL;
unsigned int nchildrenReturn = 0;
do
{
Status result = XQueryTree(nxagentDisplay, parent, &rootReturn, &parentReturn,
&childrenReturn, &nchildrenReturn);
SAFE_XFree(childrenReturn);
if (parentReturn == rootReturn || parentReturn == 0 || result == 0)
{
break;
}
parent = parentReturn;
}
while (True);
/*
* WM reparented. Find edge of the frame.
*/
if (XGetWindowAttributes(nxagentDisplay, parent, &attributes) == 0)
{
#ifdef WARNING
fprintf(stderr, "%s: WARNING! XGetWindowAttributes failed for parent window.\n", __func__);
#endif
return 1;
}
/*
* Difference between Absolute X and Parent X gives thickness of side frame.
* Difference between Absolute Y and Parent Y gives thickness of title bar.
*/
nxagentChangeOption(WMBorderWidth, (x - attributes.x));
nxagentChangeOption(WMTitleHeight, (y - attributes.y));
#ifdef DEBUG
fprintf(stderr, "%s: WMBorderWidth [%d]\n", __func__, nxagentOption(WMBorderWidth));
fprintf(stderr, "%s: WMTitleHeight [%d]\n", __func__, nxagentOption(WMTitleHeight));
fprintf(stderr, "%s: win_gravity [%d]\n", __func__, attributes.win_gravity);
fprintf(stderr, "%s: bit_gravity [%d]\n", __func__, attributes.bit_gravity);
fprintf(stderr, "%s: border_width [%d]\n", __func__, attributes.border_width);
fprintf(stderr, "%s: height [%d]\n", __func__, attributes.height);
fprintf(stderr, "%s: width [%d]\n", __func__, attributes.width);
fprintf(stderr, "%s: x [%d]\n", __func__, attributes.x);
fprintf(stderr, "%s: y [%d]\n", __func__, attributes.y);
#endif
}
}
return 1;
}
/*
* Helper for nxagent(Enable|Disable)(Keyboard|Pointer)Events
*/
static void nxagentSwitchEventsAllScreens(Mask mask, Bool enable)
{
Mask newmask = nxagentGetDefaultEventMask();
if (enable)
newmask |= mask;
else
newmask &= ~mask;
nxagentSetDefaultEventMask(newmask);
for (int i = 0; i < nxagentNumScreens; i++)
{
XSelectInput(nxagentDisplay, nxagentDefaultWindows[i], newmask);
}
}
void nxagentEnableKeyboardEvents(void)
{
nxagentSwitchEventsAllScreens(NXAGENT_KEYBOARD_EVENT_MASK, True);
XkbSelectEvents(nxagentDisplay, XkbUseCoreKbd,
NXAGENT_KEYBOARD_EXTENSION_EVENT_MASK,
NXAGENT_KEYBOARD_EXTENSION_EVENT_MASK);
}
void nxagentDisableKeyboardEvents(void)
{
nxagentSwitchEventsAllScreens(NXAGENT_KEYBOARD_EVENT_MASK, False);
XkbSelectEvents(nxagentDisplay, XkbUseCoreKbd, 0x0, 0x0);
}
void nxagentEnablePointerEvents(void)
{
nxagentSwitchEventsAllScreens(NXAGENT_POINTER_EVENT_MASK, True);
}
void nxagentDisablePointerEvents(void)
{
nxagentSwitchEventsAllScreens(NXAGENT_POINTER_EVENT_MASK, False);
}
void nxagentSendFakeKey(int key)
{
Time now = GetTimeInMillis();
xEvent fake = {0};
fake.u.u.type = KeyPress;
fake.u.u.detail = key;
fake.u.keyButtonPointer.time = now;
mieqEnqueue(&fake);
fake.u.u.type = KeyRelease;
fake.u.u.detail = key;
fake.u.keyButtonPointer.time = now;
mieqEnqueue(&fake);
}
int nxagentInitXkbKeyboardState(void)
{
XEvent X = {0};
XkbEvent *xkbev = (XkbEvent *) &X;
if (nxagentXkbInfo.EventBase == -1)
{
return 1;
}
#ifdef TEST
fprintf(stderr, "%s: Initializing XKB state.\n", __func__);
#endif
unsigned int modifiers;
XkbGetIndicatorState(nxagentDisplay, XkbUseCoreKbd, &modifiers);
xkbev -> state.locked_mods = 0x0;
if (modifiers & CAPSFLAG_IN_REPLY)
{
xkbev -> state.locked_mods |= CAPSFLAG_IN_EVENT;
}
if (modifiers & NUMFLAG_IN_REPLY)
{
xkbev -> state.locked_mods |= NUMFLAG_IN_EVENT;
}
#ifdef TEST
fprintf(stderr, "%s: Assuming XKB locked modifier bits [%x].\n", __func__,
xkbev -> state.locked_mods);
#endif
xkbev -> type = nxagentXkbInfo.EventBase + XkbEventCode;
xkbev -> any.xkb_type = XkbStateNotify;
nxagentHandleXkbKeyboardStateEvent(&X);
return 1;
}
int nxagentWaitForResource(GetResourceFuncPtr pGetResource, PredicateFuncPtr pPredicate)
{
int resource;
while ((resource = (*pGetResource)(nxagentDisplay)) == -1)
{
if (nxagentWaitEvents(nxagentDisplay, 0) == -1)
{
return -1;
}
nxagentDispatchEvents(pPredicate);
}
return resource;
}
void nxagentGrabPointerAndKeyboard(XEvent *X)
{
#ifdef TEST
fprintf(stderr, "%s: Grabbing pointer and keyboard with event at [%p].\n", __func__,
(void *) X);
#endif
unsigned long now;
if (X != NULL)
{
now = X -> xcrossing.time;
}
else
{
now = CurrentTime;
}
#ifdef TEST
fprintf(stderr, "%s: Going to grab the keyboard in context [B1].\n", __func__);
#endif
int result = XGrabKeyboard(nxagentDisplay,
nxagentFullscreenWindow ? nxagentFullscreenWindow
: RootWindow(nxagentDisplay, DefaultScreen(nxagentDisplay)),
True, GrabModeAsync, GrabModeAsync, now);
if (result != GrabSuccess)
{
#ifdef DEBUG
fprintf(stderr, "%s: keyboard grab failed.\n", __func__);
#endif
return;
}
#ifdef DEBUG
else
{
fprintf(stderr, "%s: keyboard grab successful.\n", __func__);
}
#endif
/*
* The smart scheduler could be stopped while waiting for the
* reply. In this case we need to yield explicitly to avoid to be
* stuck in the dispatch loop forever.
*/
isItTimeToYield = 1;
#ifdef TEST
fprintf(stderr, "%s: Going to grab the pointer in context [B2].\n", __func__);
#endif
int resource = nxagentWaitForResource(NXGetCollectGrabPointerResource,
nxagentCollectGrabPointerPredicate);
if (nxagentFullscreenWindow)
NXCollectGrabPointer(nxagentDisplay, resource,
nxagentFullscreenWindow, True, NXAGENT_POINTER_EVENT_MASK,
GrabModeAsync, GrabModeAsync, None, None, now);
/*
* This should not be needed.
*
* XGrabKey(nxagentDisplay, AnyKey, AnyModifier, nxagentFullscreenWindow,
* True, GrabModeAsync, GrabModeAsync);
*/
if (X != NULL)
{
#ifdef TEST
fprintf(stderr, "%s: Going to force focus in context [B4].\n", __func__);
#endif
XSetInputFocus(nxagentDisplay, nxagentFullscreenWindow,
RevertToParent, now);
}
}
void nxagentUngrabPointerAndKeyboard(XEvent *X)
{
unsigned long now;
#ifdef TEST
fprintf(stderr, "%s: Ungrabbing pointer and keyboard with event at [%p].\n", __func__,
(void *) X);
#endif
if (X != NULL)
{
now = X -> xcrossing.time;
}
else
{
now = CurrentTime;
}
#ifdef TEST
fprintf(stderr, "%s: Going to ungrab the keyboard in context [B5].\n", __func__);
#endif
XUngrabKeyboard(nxagentDisplay, now);
#ifdef TEST
fprintf(stderr, "%s: Going to ungrab the pointer in context [B6].\n", __func__);
#endif
XUngrabPointer(nxagentDisplay, now);
}
void nxagentDeactivatePointerGrab(void)
{
GrabPtr grab = inputInfo.pointer -> grab;
if (grab)
{
XButtonEvent X = {
.type = ButtonRelease,
.serial = 0,
.send_event = FALSE,
.time = currentTime.milliseconds,
.display = nxagentDisplay,
.window = nxagentWindow(grab -> window),
.root = RootWindow(nxagentDisplay, 0),
.subwindow = 0,
.x = 0,
.y = 0,
.x_root = 0,
.y_root = 0,
.state = 0x100,
.button = 1,
.same_screen = TRUE,
};
XPutBackEvent(nxagentDisplay, (XEvent*)&X);
}
}
Bool nxagentCollectGrabPointerPredicate(Display *disp, XEvent *X, XPointer ptr)
{
return (X -> xclient.window == 0 &&
X -> xclient.message_type == 0 &&
X -> xclient.format == 32 &&
X -> xclient.data.l[0] == NXCollectGrabPointerNotify);
}
void nxagentHandleCollectGrabPointerEvent(int resource)
{
int status;
if (NXGetCollectedGrabPointer(nxagentDisplay, resource, &status) == 0)
{
#ifdef PANIC
fprintf(stderr, "%s: PANIC! Failed to get GrabPointer reply for resource [%d].\n", __func__, resource);
#endif
}
}
void nxagentHandleCollectPropertyEvent(XEvent *X)
{
int resource = X -> xclient.data.l[1];
if (X -> xclient.data.l[2] == False)
{
#ifdef DEBUG
fprintf (stderr, "%s: Failed to get reply data for client [%d].\n", __func__,
resource);
#endif
return;
}
if (!nxagentCollectPropertyEventFromXServer(resource))
{
XlibAtom atomReturnType;
int resultFormat;
unsigned long ulReturnItems;
unsigned long ulReturnBytesLeft;
unsigned char *pszReturnData = NULL;
int result = NXGetCollectedProperty(nxagentDisplay,
resource,
&atomReturnType,
&resultFormat,
&ulReturnItems,
&ulReturnBytesLeft,
&pszReturnData);
if (result == True)
{
XlibWindow window = nxagentPropertyRequests[resource].window;
XlibAtom property = nxagentPropertyRequests[resource].property;
nxagentImportProperty(window, property, atomReturnType, resultFormat,
ulReturnItems, ulReturnBytesLeft, pszReturnData);
}
else
{
#ifdef DEBUG
fprintf (stderr, "%s: Failed to get reply data for client [%d].\n", __func__,
resource);
#endif
}
SAFE_XFree(pszReturnData);
return;
}
}
void nxagentSynchronizeExpose(void)
{
if (nxagentExposeQueue.length <= 0)
{
#ifdef TEST
fprintf(stderr, "%s: PANIC! Called with nxagentExposeQueue.length [%d].\n", __func__,
nxagentExposeQueue.length);
#endif
return;
}
WindowPtr pWin = nxagentExposeQueueHead.pWindow;
if (pWin)
{
if ((nxagentExposeQueueHead.localRegion) != NullRegion)
{
RegionTranslate((nxagentExposeQueueHead.localRegion),
pWin -> drawable.x, pWin -> drawable.y);
}
if ((nxagentExposeQueueHead.remoteRegion) != NullRegion)
{
RegionTranslate((nxagentExposeQueueHead.remoteRegion),
pWin -> drawable.x, pWin -> drawable.y);
}
if ((nxagentExposeQueueHead.localRegion) != NullRegion &&
(nxagentExposeQueueHead.remoteRegion) != NullRegion)
{
RegionSubtract((nxagentExposeQueueHead.remoteRegion),
(nxagentExposeQueueHead.remoteRegion),
(nxagentExposeQueueHead.localRegion));
if (!RegionNil(nxagentExposeQueueHead.remoteRegion) &&
((pWin -> eventMask|wOtherEventMasks(pWin)) & ExposureMask))
{
#ifdef TEST
fprintf(stderr, "%s: Going to call miWindowExposures for window [%d] - rects [%d].\n",
__func__, nxagentWindow(pWin),
RegionNumRects(nxagentExposeQueueHead.remoteRegion));
#endif
miWindowExposures(pWin, nxagentExposeQueueHead.remoteRegion, NullRegion);
}
}
}
nxagentExposeQueueHead.pWindow = NULL;
if (nxagentExposeQueueHead.localRegion != NullRegion)
{
RegionDestroy(nxagentExposeQueueHead.localRegion);
}
nxagentExposeQueueHead.localRegion = NullRegion;
if (nxagentExposeQueueHead.remoteRegion != NullRegion)
{
RegionDestroy(nxagentExposeQueueHead.remoteRegion);
}
nxagentExposeQueueHead.remoteRegion = NullRegion;
nxagentExposeQueueHead.remoteRegionIsCompleted = False;
nxagentExposeQueue.start = (nxagentExposeQueue.start + 1) % EXPOSED_SIZE;
nxagentExposeQueue.length--;
return;
}
int nxagentLookupByWindow(WindowPtr pWin)
{
for (int j = 0; j < nxagentExposeQueue.length; j++)
{
int i = (nxagentExposeQueue.start + j) % EXPOSED_SIZE;
if (nxagentExposeQueue.exposures[i].pWindow == pWin &&
!nxagentExposeQueue.exposures[i].remoteRegionIsCompleted)
{
return i;
}
}
return -1;
}
void nxagentRemoveDuplicatedKeys(XEvent *X)
{
_XQEvent *qelt = nxagentDisplay -> head;
KeyCode lastKeycode = X -> xkey.keycode;
if (qelt == NULL)
{
#ifdef TEST
fprintf(stderr, "%s: Trying to read more events from the X server.\n", __func__);
if (nxagentReadEvents(nxagentDisplay) > 0)
{
fprintf(stderr, "%s: Successfully read more events from the X server.\n", __func__);
}
#else
nxagentReadEvents(nxagentDisplay);
#endif
qelt = nxagentDisplay -> head;
}
if (qelt != NULL)
{
_XQEvent *prev;
_XQEvent *qeltKeyRelease;
_XQEvent *prevKeyRelease;
prev = qeltKeyRelease = prevKeyRelease = NULL;
LockDisplay(nxagentDisplay);
while (qelt != NULL)
{
if (qelt -> event.type == KeyRelease ||
qelt -> event.type == KeyPress)
{
if (qelt -> event.xkey.keycode != lastKeycode ||
(qelt -> event.type == KeyPress && qeltKeyRelease == NULL) ||
(qelt -> event.type == KeyRelease && qeltKeyRelease != NULL))
{
break;
}
if (qelt -> event.type == KeyRelease)
{
prevKeyRelease = prev;
qeltKeyRelease = qelt;
}
else if (qelt -> event.type == KeyPress)
{
_XDeq(nxagentDisplay, prev, qelt);
qelt = prev -> next;
if (prev == qeltKeyRelease)
{
prev = prevKeyRelease;
}
_XDeq(nxagentDisplay, prevKeyRelease, qeltKeyRelease);
qeltKeyRelease = prevKeyRelease = NULL;
continue;
}
}
prev = qelt;
qelt = qelt -> next;
}
UnlockDisplay(nxagentDisplay);
}
}
void nxagentInitRemoteExposeRegion(void)
{
if (nxagentRemoteExposeRegion == NULL)
{
nxagentRemoteExposeRegion = RegionCreate(NULL, 1);
if (nxagentRemoteExposeRegion == NULL)
{
#ifdef PANIC
fprintf(stderr, "%s: PANIC! Failed to create expose region.\n", __func__);
#endif
}
}
}
void nxagentForwardRemoteExpose(void)
{
if (RegionNotEmpty(nxagentRemoteExposeRegion))
{
#ifdef DEBUG
fprintf(stderr, "%s: Going to forward events.\n", __func__);
#endif
TraverseTree(screenInfo.screens[0]->root, nxagentClipAndSendExpose, (void *)nxagentRemoteExposeRegion);
/*
* Now this region should be empty.
*/
RegionEmpty(nxagentRemoteExposeRegion);
}
}
void nxagentAddRectToRemoteExposeRegion(BoxPtr rect)
{
if (nxagentRemoteExposeRegion == NULL)
{
return;
}
RegionRec exposeRegion;
RegionInit(&exposeRegion, rect, 1);
RegionUnion(nxagentRemoteExposeRegion,
nxagentRemoteExposeRegion, &exposeRegion);
RegionUninit(&exposeRegion);
}
int nxagentClipAndSendExpose(WindowPtr pWin, void * ptr)
{
RegionPtr remoteExposeRgn = (RegionRec *) ptr;
#ifdef DEBUG
fprintf(stderr, "%s: Called.\n", __func__);
#endif
if (pWin -> drawable.class != InputOnly)
{
RegionPtr exposeRgn = RegionCreate(NULL, 1);
#ifdef DEBUG
BoxRec box = *RegionExtents(remoteExposeRgn);
fprintf(stderr, "%s: Root expose extents: [%d] [%d] [%d] [%d].\n", __func__,
box.x1, box.y1, box.x2, box.y2);
box = *RegionExtents(&pWin -> clipList);
fprintf(stderr, "%s: Clip list extents for window at [%p]: [%d] [%d] [%d] [%d].\n", __func__,
(void *)pWin, box.x1, box.y1, box.x2, box.y2);
#endif
RegionIntersect(exposeRgn, remoteExposeRgn, &pWin -> clipList);
if (RegionNotEmpty(exposeRgn))
{
#ifdef DEBUG
fprintf(stderr, "%s: Forwarding expose to window at [%p] pWin.\n", __func__,
(void *)pWin);
#endif
/*
* The miWindowExposures() clears out the region parameters, so
* the subtract operation must be done before calling it.
*/
RegionSubtract(remoteExposeRgn, remoteExposeRgn, exposeRgn);
miWindowExposures(pWin, exposeRgn, NullRegion);
}
RegionDestroy(exposeRgn);
}
if (RegionNotEmpty(remoteExposeRgn))
{
#ifdef DEBUG
fprintf(stderr, "%s: Region not empty. Walk children.\n", __func__);
#endif
return WT_WALKCHILDREN;
}
else
{
#ifdef DEBUG
fprintf(stderr, "%s: Region empty. Stop walking.\n", __func__);
#endif
return WT_STOPWALKING;
}
}
int nxagentUserInput(void *p)
{
int result = 0;
/*
* This function is used as callback in the polling handler of agent
* in shadow mode. When inside the polling loop the handlers are
* never called, so we have to dispatch enqueued events to
* eventually change the nxagentInputEvent status.
*/
if (nxagentOption(Shadow) &&
nxagentPendingEvents(nxagentDisplay) > 0)
{
nxagentDispatchEvents(NULL);
}
if (nxagentInputEvent == 1)
{
nxagentInputEvent = 0;
result = 1;
}
/*
* The agent working in shadow mode synchronizes the remote X server
* even if a button/key is not released (i.e. when scrolling a long
* browser's page), in order to update the screen smoothly.
*/
if (nxagentOption(Shadow))
{
return result;
}
if (result == 0)
{
/*
* If there is at least one button/key down, we are receiving an
* input. This is not a condition to break a synchronization loop
* if there is enough bandwidth.
*/
if (nxagentCongestion > 0 &&
(inputInfo.pointer -> button -> buttonsDown > 0 ||
nxagentKeyDown > 0))
{
#ifdef TEST
fprintf(stderr, "%s: Buttons [%d] Keys [%d].\n", __func__,
inputInfo.pointer -> button -> buttonsDown, nxagentKeyDown);
#endif
result = 1;
}
}
return result;
}
int nxagentHandleRRScreenChangeNotify(XEvent *X)
{
XRRScreenChangeNotifyEvent *Xr = (XRRScreenChangeNotifyEvent *) X;
#ifdef DEBUG
fprintf(stderr, "%s: Called.\n", __func__);
#endif
nxagentResizeScreen(screenInfo.screens[DefaultScreen(nxagentDisplay)],
Xr -> width, Xr -> height,
Xr -> mwidth, Xr -> mheight, True);
nxagentShadowCreateMainWindow(screenInfo.screens[DefaultScreen(nxagentDisplay)],
screenInfo.screens[0]->root,
Xr -> width, Xr -> height);
nxagentShadowSetWindowsSize();
return 1;
}
/*
* Returns true if there is any event waiting to be dispatched. This
* function is critical for the performance because it is called very,
* very often. It must also handle the case when the display is
* down. The display descriptor, in fact, may have been reused by some
* other client.
*/
int nxagentPendingEvents(Display *dpy)
{
if (_XGetIOError(dpy) != 0)
{
#ifdef DEBUG
fprintf(stderr, "%s: Returning error with display down.\n", __func__);
#endif
return -1;
}
else if (XQLength(dpy) > 0)
{
#ifdef DEBUG
fprintf(stderr, "%s: Returning true with [%d] events queued.\n", __func__,
XQLength(dpy));
#endif
return 1;
}
else
{
int readable;
if (NXTransReadable(dpy -> fd, &readable) == 0)
{
if (readable > 0)
{
#ifdef DEBUG
fprintf(stderr, "%s: Returning true with [%d] bytes readable.\n", __func__,
readable);
#endif
return 1;
}
#ifdef DEBUG
fprintf(stderr, "%s: Returning false with [%d] bytes readable.\n", __func__,
readable);
#endif
return 0;
}
#ifdef TEST
fprintf(stderr, "%s: WARNING! Error detected on the X display.\n", __func__);
#endif
NXForceDisplayError(dpy);
return -1;
}
}
/*
* Blocks until an event becomes available.
*/
int nxagentWaitEvents(Display *dpy, useconds_t msec)
{
#ifdef DEBUG
fprintf(stderr, "%s: Called.\n", __func__);
#endif
NXFlushDisplay(dpy, NXFlushLink);
/*
* If the transport is not running we have to rely on Xlib to wait
* for an event. In this case the timeout is ignored.
*/
if (NXTransRunning(NX_FD_ANY) == 1)
{
if (msec > 0)
{
struct timeval tm = {
.tv_sec = 0,
.tv_usec = msec * 1000
};
NXTransContinue(&tm);
}
else
{
NXTransContinue(NULL);
}
}
else
{
XEvent ev;
XPeekEvent(dpy, &ev);
}
/*
* Check if we encountered a display error. If we did, wait for the
* time requested by the caller.
*/
if (NXDisplayError(dpy) == 1)
{
if (msec > 0)
{
usleep(msec * 1000);
}
return -1;
}
return 1;
}
void ForwardClientMessage(ClientPtr client, xSendEventReq *stuff)
{
Atom netwmstate = MakeAtom("_NET_WM_STATE", strlen("_NET_WM_STATE"), False);
Atom wmchangestate = MakeAtom("WM_CHANGE_STATE", strlen("WM_CHANGE_STATE"), False);
WindowPtr pWin = (WindowPtr)SecurityLookupWindow(stuff->destination, client,
DixReadAccess);
if (stuff->event.u.clientMessage.u.l.type == netwmstate || stuff->event.u.clientMessage.u.l.type == wmchangestate)
{
if (pWin->drawable.id == pWin->drawable.pScreen->root->drawable.id)
{
#ifdef DEBUG
fprintf(stderr, "%s: dest [0x%x] window [0x%x] clmsg.type [%d]->[%d]\n", __func__, stuff->destination, stuff->event.u.clientMessage.window, stuff->event.u.clientMessage.u.l.type, nxagentLocalToRemoteAtom(stuff->event.u.clientMessage.u.l.type));
#endif
XEvent X = {0};
X.xany.type = ClientMessage;
WindowPtr pWin2 = (WindowPtr)SecurityLookupWindow(stuff->event.u.clientMessage.window, client,
DixReadAccess);
X.xclient.window = nxagentWindowPriv(pWin2)->window;
X.xclient.format = stuff->event.u.u.detail;
X.xclient.send_event = True;
X.xclient.serial = 0;
if (X.xclient.format == 32)
{
X.xclient.message_type = nxagentLocalToRemoteAtom(stuff->event.u.clientMessage.u.l.type);
X.xclient.data.l[0] = stuff->event.u.clientMessage.u.l.longs0;
X.xclient.data.l[1] = nxagentLocalToRemoteAtom(stuff->event.u.clientMessage.u.l.longs1);
X.xclient.data.l[2] = nxagentLocalToRemoteAtom(stuff->event.u.clientMessage.u.l.longs2);
X.xclient.data.l[3] = nxagentLocalToRemoteAtom(stuff->event.u.clientMessage.u.l.longs3);
X.xclient.data.l[4] = nxagentLocalToRemoteAtom(stuff->event.u.clientMessage.u.l.longs4);
//X.xclient.data.l[3] = stuff->event.u.clientMessage.u.l.longs3;
//X.xclient.data.l[4] = stuff->event.u.clientMessage.u.l.longs4;
#ifdef DEBUG
for (int i = 0; i < 5; i++)
{
fprintf(stderr, "%s: data[%d] [%ld]\n", __func__, i, X.xclient.data.l[i]);
}
#endif
}
else
return; // ERROR!
#ifdef DEBUG
fprintf(stderr, "%s: window [0x%lx]\n", __func__, X.xclient.window);
fprintf(stderr, "%s: message_type [%ld]\n", __func__, X.xclient.message_type);
fprintf(stderr, "%s: format [%d]\n", __func__, X.xclient.format);
#endif
XlibWindow dest;
dest = DefaultRootWindow(nxagentDisplay);
#ifdef DEBUG
Status stat =
#endif
XSendEvent(nxagentDisplay, dest, stuff->propagate, stuff->eventMask, &X);
XFlush(nxagentDisplay);
#ifdef DEBUG
fprintf(stderr, "%s: send to window [0x%lx]\n", __func__, dest);
fprintf(stderr, "%s: return Status [%d]\n", __func__, stat);
#endif
}
}
}
#ifdef NX_DEBUG_INPUT
void nxagentGuessDumpInputInfo(ClientPtr client, Atom property, char *data)
{
if (strcmp(validateString(NameForAtom(property)), "NX_DEBUG_INPUT") == 0)
{
if (*data != 0)
{
nxagentDebugInput = True;
}
else
{
nxagentDebugInput = False;
}
}
}
void nxagentDeactivateInputDevicesGrabs(void)
{
fprintf(stderr, "Info: Deactivating input devices grabs.\n");
if (inputInfo.pointer -> grab)
{
(*inputInfo.pointer -> DeactivateGrab)(inputInfo.pointer);
}
if (inputInfo.keyboard -> grab)
{
(*inputInfo.keyboard -> DeactivateGrab)(inputInfo.keyboard);
}
}
static const char *nxagentGrabStateToString(int state)
{
switch (state)
{
case 0: return "NOT_GRABBED";
case 1: return "THAWED";
case 2: return "THAWED_BOTH";
case 3: return "FREEZE_NEXT_EVENT";
case 4: return "FREEZE_BOTH_NEXT_EVENT";
case 5: return "FROZEN_NO_EVENT";
case 6: return "FROZEN_WITH_EVENT";
case 7: return "THAW_OTHERS";
default: return "unknown state";
}
}
void nxagentDumpInputDevicesState(void)
{
WindowPtr pWin = NULL;
fprintf(stderr, "\n*** Dump input devices state: BEGIN ***"
"\nKeys down:");
DeviceIntPtr dev = inputInfo.keyboard;
for (int i = 0; i < DOWN_LENGTH; i++)
{
CARD8 val = dev -> key -> down[i];
if (val != 0)
{
for (int k = 0; k < 8; k++)
{
if (val & (1 << k))
{
fprintf(stderr, "\n\t[%d] [%s]", i * 8 + k,
XKeysymToString(XKeycodeToKeysym(nxagentDisplay, i * 8 + k, 0)));
}
}
}
}
fprintf(stderr, "\nKeyboard device state: \n\tdevice [%p]\n\tlast grab time [%u]"
"\n\tfrozen [%s]\n\tstate [%s]\n\tother [%p]\n\tevent count [%d]"
"\n\tfrom passive grab [%s]\n\tactivating key [%d]", (void *)dev,
dev -> grabTime.milliseconds, dev -> sync.frozen ? "Yes": "No",
nxagentGrabStateToString(dev -> sync.state),
(void *)dev -> sync.other, dev -> sync.evcount,
dev -> fromPassiveGrab ? "Yes" : "No",
dev -> activatingKey);
GrabPtr grab = dev -> grab;
if (grab)
{
fprintf(stderr, "\nKeyboard grab state: \n\twindow pointer [%p]"
"\n\towner events flag [%s]\n\tgrab mode [%s]",
(void *)grab -> window, grab -> ownerEvents ? "True" : "False",
grab -> keyboardMode ? "asynchronous" : "synchronous");
/*
* Passive grabs.
*/
pWin = grab -> window;
grab = wPassiveGrabs(pWin);
while (grab)
{
fprintf(stderr, "\nPassive grab state: \n\tdevice [%p]\n\towner events flag [%s]"
"\n\tpointer grab mode [%s]\n\tkeyboard grab mode [%s]\n\tevent type [%d]"
"\n\tmodifiers [%x]\n\tbutton/key [%u]\n\tevent mask [%x]",
(void *)grab -> device, grab -> ownerEvents ? "True" : "False",
grab -> pointerMode ? "asynchronous" : "synchronous",
grab -> keyboardMode ? "asynchronous" : "synchronous",
grab -> type, grab -> modifiersDetail.exact,
grab -> detail.exact, grab -> eventMask);
grab = grab -> next;
}
}
fprintf(stderr, "\nButtons down:");
dev = inputInfo.pointer;
for (int i = 0; i < DOWN_LENGTH; i++)
{
CARD8 val = dev -> button -> down[i];
if (val != 0)
{
for (int k = 0; k < 8; k++)
{
if (val & (1 << k))
{
fprintf(stderr, "\n\t[%d]", i * 8 + k);
}
}
}
}
fprintf(stderr, "\nPointer device state: \n\tdevice [%p]\n\tlast grab time [%u]"
"\n\tfrozen [%s]\n\tstate [%s]\n\tother [%p]\n\tevent count [%d]"
"\n\tfrom passive grab [%s]\n\tactivating button [%d]", (void *)dev,
dev -> grabTime.milliseconds, dev -> sync.frozen ? "Yes" : "No",
nxagentGrabStateToString(dev -> sync.state),
(void *)dev -> sync.other, dev -> sync.evcount,
dev -> fromPassiveGrab ? "Yes" : "No",
dev -> activatingKey);
grab = dev -> grab;
if (grab)
{
fprintf(stderr, "\nPointer grab state: \n\twindow pointer [%p]"
"\n\towner events flag [%s]\n\tgrab mode [%s]",
(void *)grab -> window, grab -> ownerEvents ? "True" : "False",
grab -> pointerMode ? "asynchronous" : "synchronous");
if (grab -> window != pWin)
{
/*
* Passive grabs.
*/
grab = wPassiveGrabs(grab -> window);
while (grab)
{
fprintf(stderr, "\nPassive grab state: \n\tdevice [%p]\n\towner events flag [%s]"
"\n\tpointer grab mode [%s]\n\tkeyboard grab mode [%s]\n\tevent type [%d]"
"\n\tmodifiers [%x]\n\tbutton/key [%u]\n\tevent mask [%x]",
(void *)grab -> device, grab -> ownerEvents ? "True" : "False",
grab -> pointerMode ? "asynchronous" : "synchronous",
grab -> keyboardMode ? "asynchronous" : "synchronous",
grab -> type, grab -> modifiersDetail.exact,
grab -> detail.exact, grab -> eventMask);
grab = grab -> next;
}
}
}
fprintf(stderr, "\n*** Dump input devices state: FINISH ***\n");
}
#endif
|