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
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
|
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
/* Copyright (c) 2008-2014 Oleksandr Shneyder <o.shneyder@phoca-gmbh.de> */
/* Copyright (c) 2014-2016 Ulrich Sibiller <uli42@gmx.de> */
/* Copyright (c) 2014-2016 Mihai Moldovan <ionic@ionic.de> */
/* Copyright (c) 2011-2016 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>*/
/* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
/* */
/* NXCOMPEXT, 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. */
/* */
/**************************************************************************/
/*
* let the types be the Xlib types by undefining _XSERVER64. This
* means, when calling the functions of this file from nxagent (where
* Agent.h has been included) you need to use/provide XlibAtom and
* XlibWindow instead of Atom and Window
*/
#undef _XSERVER64
#include <sys/socket.h>
#ifndef __sun
#include <strings.h>
#endif
#include "dix.h"
#include "os.h"
/*
* Needed to enable definition of the callback
* functions.
*/
#define NX_TRANS_SOCKET
#include <nx-X11/Xlib.h>
#include <nx-X11/Xutil.h>
#include <nx-X11/Xlibint.h>
#include "Compext.h"
#include <nx/NX.h>
#include <nx/NXproto.h>
#include <nx/NXpack.h>
#include <nx/MD5.h>
#include "Clean.h"
#include "Mask.h"
#include "Colormap.h"
#include "Alpha.h"
#include "Bitmap.h"
#include "Jpeg.h"
#include "Png.h"
#include "Rgb.h"
#include "Rle.h"
#include "Z.h"
#include "../Utils.h"
#define PANIC
#define WARNING
#undef TEST
#undef TEST_DISPLAY
#undef TEST_IMAGE
#undef TEST_INPUT
#undef TEST_PARAMS
#undef TEST_POINTER
#undef TEST_PROPERTY
#undef TEST_SPLIT
#undef TEST_UNPACK
#undef DEBUG
#undef DEBUG_DISPLAY
#undef DEBUG_IMAGE
#undef DEBUG_INPUT
#undef DEBUG_PARAMS
#undef DEBUG_POINTER
#undef DEBUG_PROPERTY
#undef DEBUG_SPLIT
#undef DEBUG_UNPACK
#undef DUMP
/*
* Maximum number of colors allowed in
* Png encoding.
*/
#define NB_COLOR_MAX 256
/*
* Dummy error handlers used internally to catch
* Xlib failures in replies.
*/
static int _NXInternalReplyErrorFunction(Display *dpy, XErrorEvent *error);
static void _NXInternalLostSequenceFunction(Display *dpy, unsigned long newseq,
unsigned long lastseq, unsigned int type);
/*
* Resource ids that can be requested by
* the client for use in split or unpack
* operations.
*/
static unsigned char _NXSplitResources[NXNumberOfResources];
static unsigned char _NXUnpackResources[NXNumberOfResources];
static Display *_NXDisplayInitialized = NULL;
/*
* Used in asynchronous handling of
* GetImage replies.
*/
typedef struct
{
unsigned long sequence;
unsigned int resource;
unsigned long mask;
int format;
int width;
int height;
_XAsyncHandler *handler;
XImage *image;
} _NXCollectImageState;
static _NXCollectImageState *_NXCollectedImages[NXNumberOfResources];
/*
* Used in asynchronous handling of
* GetProperty replies.
*/
typedef struct
{
unsigned long sequence;
unsigned int resource;
Window window;
Atom property;
Atom type;
int format;
unsigned long items;
unsigned long after;
_XAsyncHandler *handler;
char *data;
} _NXCollectPropertyState;
static _NXCollectPropertyState *_NXCollectedProperties[NXNumberOfResources];
/*
* Used in asynchronous handling of
* GrabPointer replies.
*/
typedef struct
{
unsigned long sequence;
unsigned int resource;
int status;
_XAsyncHandler *handler;
} _NXCollectGrabPointerState;
static _NXCollectGrabPointerState *_NXCollectedGrabPointers[NXNumberOfResources];
/*
* Used in asynchronous handling of
* GetInputFocus replies.
*/
typedef struct
{
unsigned long sequence;
unsigned int resource;
Window focus;
int revert_to;
_XAsyncHandler *handler;
} _NXCollectInputFocusState;
static _NXCollectInputFocusState *_NXCollectedInputFocuses[NXNumberOfResources];
/*
* Used by functions handling cache of
* packed images.
*/
#define MD5_LENGTH 16
typedef struct
{
md5_byte_t *md5;
XImage *image;
unsigned int method;
} _NXImageCacheEntry;
int NXImageCacheSize = 0;
int NXImageCacheHits = 0;
int NXImageCacheOps = 0;
_NXImageCacheEntry *NXImageCache = NULL;
#ifdef DUMP
void _NXCacheDump(const char *label);
void _NXDumpData(const unsigned char *buffer, unsigned int size);
#endif
/*
* From X11/PutImage.c.
*
* Cancel a GetReq operation, before doing
* _XSend or Data.
*/
#if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
#define UnGetReq(name)\
dpy->bufptr -= SIZEOF(x##name##Req);\
dpy->request--
#else
#define UnGetReq(name)\
dpy->bufptr -= SIZEOF(x/**/name/**/Req);\
dpy->request--
#endif
#if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
#define UnGetEmptyReq()\
dpy->bufptr -= 4;\
dpy->request--
#else
#define UnGetEmptyReq(name)\
dpy->bufptr -= 4;\
dpy->request--
#endif
/*
* From X11/ImUtil.c.
*/
extern int _XGetBitsPerPixel(Display *dpy, int depth);
extern int _XGetScanlinePad(Display *dpy, int depth);
#define ROUNDUP(nbytes, pad) (((nbytes) + ((pad) - 1)) & \
~(long)((pad) - 1))
static unsigned int DepthOnes(unsigned long mask)
{
register unsigned long y;
y = (mask >> 1) &033333333333;
y = mask - y - ((y >>1) & 033333333333);
return ((unsigned int) (((y + (y >> 3)) &
030707070707) % 077));
}
#define CanMaskImage(image, mask) \
\
(image -> format == ZPixmap && mask != NULL && \
(image -> depth == 32 || image -> depth == 24 || \
(image -> depth == 16 && (image -> red_mask == 0xf800 && \
image -> green_mask == 0x7e0 && image -> blue_mask == 0x1f))))
#define ShouldMaskImage(image, mask) (mask -> color_mask != 0xff)
/*
* Initialize and reset the internal structures.
*/
extern int _NXInternalInitResources(Display *dpy);
extern int _NXInternalResetResources(Display *dpy);
extern int _NXInternalInitEncoders(Display *dpy);
extern int _NXInternalResetEncoders(Display *dpy);
int NXInitDisplay(Display *dpy)
{
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXInitDisplay: Called for display at [%p].\n", (void *) dpy);
#endif
if (_NXDisplayInitialized == NULL)
{
_NXInternalInitResources(dpy);
_NXInternalInitEncoders(dpy);
_NXDisplayInitialized = dpy;
return 1;
}
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXInitDisplay: WARNING! Internal structures already initialized.\n");
#endif
return 0;
}
int NXResetDisplay(Display *dpy)
{
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXResetDisplay: Called for display at [%p].\n", (void *) dpy);
#endif
if (_NXDisplayInitialized != NULL)
{
_NXInternalResetResources(dpy);
_NXInternalResetEncoders(dpy);
_NXDisplayInitialized = NULL;
return 1;
}
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXResetDisplay: WARNING! Internal structures already reset.\n");
#endif
return 0;
}
int _NXInternalInitResources(Display *dpy)
{
return _NXInternalResetResources(dpy);
}
int _NXInternalResetResources(Display *dpy)
{
#if defined(TEST_IMAGE) || defined(TEST_PROPERTY) || defined(TEST_POINTER) || defined(TEST_INPUT)
fprintf(stderr, "******_NXInternalResetResources: Clearing all the internal structures.\n");
#endif
for (int i = 0; i < NXNumberOfResources; i++)
{
_NXSplitResources[i] = 0;
_NXUnpackResources[i] = 0;
if (_NXCollectedImages[i] != NULL)
{
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXInternalResetResources: WARNING! Clearing collect image data "
"for resource [%d].\n", i);
#endif
if (_NXCollectedImages[i] -> handler != NULL)
{
DeqAsyncHandler(dpy, _NXCollectedImages[i] -> handler);
SAFE_free(_NXCollectedImages[i] -> handler);
}
if (_NXCollectedImages[i] -> image != NULL)
{
XDestroyImage(_NXCollectedImages[i] -> image);
}
SAFE_free(_NXCollectedImages[i]);
}
if (_NXCollectedProperties[i] != NULL)
{
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXInternalResetResources: WARNING! Clearing collect property data "
"for resource [%d].\n", i);
#endif
if (_NXCollectedProperties[i] -> handler != NULL)
{
DeqAsyncHandler(dpy, _NXCollectedProperties[i] -> handler);
SAFE_free(_NXCollectedProperties[i] -> handler);
}
SAFE_free(_NXCollectedProperties[i] -> data);
SAFE_free(_NXCollectedProperties[i]);
}
if (_NXCollectedGrabPointers[i] != NULL)
{
#ifdef TEST_POINTER
fprintf(stderr, "******_NXInternalResetResources: WARNING! Clearing grab pointer data "
"for resource [%d].\n", i);
#endif
if (_NXCollectedGrabPointers[i] -> handler != NULL)
{
DeqAsyncHandler(dpy, _NXCollectedGrabPointers[i] -> handler);
SAFE_free(_NXCollectedGrabPointers[i] -> handler);
}
SAFE_free(_NXCollectedGrabPointers[i]);
}
if (_NXCollectedInputFocuses[i] != NULL)
{
#ifdef TEST_INPUT
fprintf(stderr, "******_NXInternalResetResources: WARNING! Clearing collect input focus data "
"for resource [%d].\n", i);
#endif
if (_NXCollectedInputFocuses[i] -> handler != NULL)
{
DeqAsyncHandler(dpy, _NXCollectedInputFocuses[i] -> handler);
SAFE_free(_NXCollectedInputFocuses[i] -> handler);
}
SAFE_free(_NXCollectedInputFocuses[i]);
}
}
return 1;
}
int _NXInternalInitEncoders(Display *dpy)
{
ZInitEncoder();
return 1;
}
int _NXInternalResetEncoders(Display *dpy)
{
ZResetEncoder();
return 1;
}
int NXSetDisplayPolicy(Display *dpy, int policy)
{
if (policy == NXPolicyImmediate)
{
return NXTransPolicy(NX_FD_ANY, NX_POLICY_IMMEDIATE);
}
else
{
return NXTransPolicy(NX_FD_ANY, NX_POLICY_DEFERRED);
}
}
/*
* return codes:
* -1 something went wrong
* 1 success
*/
int NXSetDisplayBuffer(Display *dpy, int size)
{
/*
* This is not multi-thread safe, so,
* if you have threads, be sure that
* they are stopped.
*/
char *buffer;
XFlush(dpy);
if (dpy -> bufmax - size == dpy -> buffer)
{
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayBuffer: Nothing to do with buffer size matching.\n");
#endif
return 1;
}
else if (dpy -> bufptr != dpy -> buffer)
{
#ifdef PANIC
fprintf(stderr, "******NXSetDisplayBuffer: PANIC! The display buffer is not empty.\n");
#endif
return -1;
}
else if ((buffer = Xcalloc(1, size)) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXSetDisplayBuffer: PANIC! Can't allocate [%d] bytes for the buffer.\n",
size);
#endif
return -1;
}
SAFE_free(dpy -> buffer);
dpy -> buffer = buffer;
dpy -> bufptr = dpy -> buffer;
dpy -> bufmax = dpy -> bufptr + size;
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayBuffer: Set the display output buffer size to [%d].\n",
size);
#endif
return 1;
}
/*
* If set, the Popen() function in the X server
* will remove the LD_LIBRARY_PATH variable from
* the environment before calling the execl()
* function on the child process.
*/
int NXUnsetLibraryPath(int value)
{
int previous = _NXUnsetLibraryPath;
_NXUnsetLibraryPath = value;
#ifdef TEST
fprintf(stderr, "******NXUnsetLibraryPath: Set the flag to [%d] with previous value [%d].\n",
value, previous);
#endif
return previous;
}
/*
* If set, the Xlib I/O error handler will simply
* return, instead of quitting the program. This
* leaves to the application the responsibility
* of checking the state of the XlibDisplayIOEr-
* ror flag.
*/
int NXHandleDisplayError(int value)
{
int previous = _NXHandleDisplayError;
_NXHandleDisplayError = value;
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXHandleDisplayError: Set the flag to [%d] with previous value [%d].\n",
value, previous);
#endif
return previous;
}
/*
* Shutdown the display descriptor and force Xlib
* to set the I/O error flag.
*/
Bool NXForceDisplayError(Display *dpy)
{
if (dpy != NULL)
{
NXTransClose(dpy -> fd);
if (!(dpy -> flags & XlibDisplayIOError))
{
shutdown(dpy -> fd, SHUT_RDWR);
_XIOError(dpy);
}
return 1;
}
return 0;
}
/*
* Check if the display has become invalid. Similarly
* to the modified Xlib, we call the predicate funct-
* ion with the value of the XlibDisplayIOError flag
* only if the I/O error was not encountered already.
* The application can use this function to query the
* XlibDisplayIOError flag because Xlib doesn't expose
* the internals of the display structure to the appli-
* cation.
*/
int NXDisplayError(Display *dpy)
{
if (dpy != NULL)
{
return (_XGetIOError(dpy) ||
(_NXDisplayErrorFunction != NULL &&
(*_NXDisplayErrorFunction)(dpy, _XGetIOError(dpy))));
}
return 1;
}
/*
* Various queries related to the state of the
* display connection.
*/
int NXDisplayReadable(Display *dpy)
{
int result;
int readable;
result = NXTransReadable(dpy -> fd, &readable);
if (result == 0)
{
#ifdef DEBUG_DISPLAY
fprintf(stderr, "******NXDisplayReadable: Returning [%d] bytes readable from fd [%d].\n",
readable, dpy -> fd);
#endif
return readable;
}
#ifdef DEBUG_DISPLAY
fprintf(stderr, "******NXDisplayReadable: WARNING! Error detected on display fd [%d].\n",
dpy -> fd);
#endif
return -1;
}
int NXDisplayFlushable(Display *dpy)
{
#ifdef DEBUG_DISPLAY
int flushable;
flushable = NXTransFlushable(dpy -> fd) +
(dpy -> bufptr - dpy -> buffer);
fprintf(stderr, "******NXDisplayFlushable: Returning [%d+%d=%d] bytes flushable "
"to fd [%d].\n", (int) (dpy -> bufptr - dpy -> buffer),
(int) (flushable - (dpy -> bufptr - dpy -> buffer)),
flushable, dpy -> fd);
return flushable;
#else
return NXTransFlushable(dpy -> fd) + (dpy -> bufptr - dpy -> buffer);
#endif
}
int NXDisplayCongestion(Display *dpy)
{
#ifdef DEBUG_DISPLAY
int congestion = NXTransCongestion(dpy -> fd);
fprintf(stderr, "******NXDisplayCongestion: Returning [%d] as congestion level for fd [%d].\n",
congestion, dpy -> fd);
return congestion;
#else
return NXTransCongestion(dpy -> fd);
#endif
}
int NXFlushDisplay(Display *dpy, int what)
{
if (!(dpy -> flags & XlibDisplayWriting) &&
dpy -> bufptr - dpy -> buffer > 0)
{
#ifdef DEBUG_DISPLAY
fprintf(stderr, "******NXFlushDisplay: Writing with [%d] bytes in the buffer.\n",
(int) (dpy -> bufptr - dpy -> buffer));
#endif
XFlush(dpy);
}
if (what == NXFlushBuffer)
{
return 0;
}
#ifdef DEBUG_DISPLAY
fprintf(stderr, "******NXFlushDisplay: Flushing with [%d] bytes in the NX transport.\n",
NXDisplayFlushable(dpy));
#endif
return NXTransFlush(dpy -> fd);
}
NXDisplayErrorPredicate NXSetDisplayErrorPredicate(NXDisplayErrorPredicate predicate)
{
NXDisplayErrorPredicate previous = _NXDisplayErrorFunction;
_NXDisplayErrorFunction = predicate;
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayErrorPredicate: Set the predicate to [%p] with previous value [%p].\n",
predicate, previous);
#endif
return previous;
}
NXDisplayBlockHandler NXSetDisplayBlockHandler(NXDisplayBlockHandler handler)
{
NXDisplayBlockHandler previous = _NXDisplayBlockFunction;
_NXDisplayBlockFunction = handler;
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayBlockHandler: Set the handler to [%p] with previous value [%p].\n",
handler, previous);
#endif
return previous;
}
NXDisplayWriteHandler NXSetDisplayWriteHandler(NXDisplayWriteHandler handler)
{
NXDisplayWriteHandler previous = _NXDisplayWriteFunction;
_NXDisplayWriteFunction = handler;
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayWriteHandler: Set the handler to [%p] with previous value [%p].\n",
handler, previous);
#endif
return previous;
}
NXDisplayFlushHandler NXSetDisplayFlushHandler(NXDisplayFlushHandler handler, Display *display)
{
NXDisplayFlushHandler previous = _NXDisplayFlushFunction;
_NXDisplayFlushFunction = handler;
NXTransHandler(NX_FD_ANY, NX_HANDLER_FLUSH,
(void (*)(void *, int)) handler, (void *) display);
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayFlushHandler: Set the handler to [%p] with display [%p] "
"and previous value [%p].\n", handler, display, previous);
#endif
return previous;
}
NXDisplayStatisticsHandler NXSetDisplayStatisticsHandler(NXDisplayStatisticsHandler handler, char **buffer)
{
NXDisplayStatisticsHandler previous = _NXDisplayStatisticsFunction;
_NXDisplayStatisticsFunction = handler;
/*
* Propagate the handler.
*/
NXTransHandler(NX_FD_ANY, NX_HANDLER_STATISTICS,
(void (*)(void *, int)) handler, (void *) buffer);
#ifdef TEST_DISPLAY
fprintf(stderr, "******NXSetDisplayStatisticsHandler: Set the handler to [%p] with buffer pointer [%p] "
"and previous value [%p].\n", handler, buffer, previous);
#endif
return previous;
}
NXLostSequenceHandler NXSetLostSequenceHandler(NXLostSequenceHandler handler)
{
NXLostSequenceHandler previous = _NXLostSequenceFunction;
_NXLostSequenceFunction = handler;
#ifdef TEST
fprintf(stderr, "******NXSetLostSequenceHandler: Set the handler to [%p] with previous value [%p].\n",
handler, previous);
#endif
return previous;
}
int _NXInternalReplyErrorFunction(Display *dpy, XErrorEvent *error)
{
#ifdef TEST
fprintf(stderr, "******_NXInternalReplyErrorFunction: Internal error handler called.\n");
#endif
return 0;
}
void _NXInternalLostSequenceFunction(Display *dpy, unsigned long newseq,
unsigned long lastseq, unsigned int type)
{
#ifdef TEST
fprintf(stderr, "******_NXInternalLostSequenceFunction: WARNING! Sequence lost with new "
"sequence %ld last request %ld.\n", newseq, dpy -> request);
/*
* TODO: Reply or event info must be implemented.
*
* fprintf(stderr, "******_NXInternalLostSequenceFunction: WARNING! Expected event or reply "
* "was %ld with sequence %ld.\n", (long) rep -> type, (long) rep -> sequenceNumber);
*/
fprintf(stderr, "******_NXInternalLostSequenceFunction: WARNING! Last sequence read "
"was %ld display request is %ld.\n", lastseq & 0xffff, dpy -> request & 0xffff);
#endif
}
/*
* return codes:
* 0 error receiving reply
* 1 success
*/
Status NXGetControlParameters(Display *dpy, unsigned int *link_type, unsigned int *local_major,
unsigned int *local_minor, unsigned int *local_patch,
unsigned int *remote_major, unsigned int *remote_minor,
unsigned int *remote_patch, int *split_timeout, int *motion_timeout,
int *split_mode, int *split_size, unsigned int *pack_method,
unsigned int *pack_quality, int *data_level, int *stream_level,
int *delta_level, unsigned int *load_cache,
unsigned int *save_cache, unsigned int *startup_cache)
{
xNXGetControlParametersReply rep;
_X_UNUSED register xReq *req;
LockDisplay(dpy);
GetEmptyReq(NXGetControlParameters, req);
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetControlParameters: Sending message opcode [%d].\n",
X_NXGetControlParameters);
#endif
if (_XReply(dpy, (xReply *) &rep, 0, xTrue) == xFalse)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetControlParameters: Error receiving reply.\n");
#endif
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetControlParameters: Got reply with link type [%u].\n", rep.linkType);
fprintf(stderr, "******NXGetControlParameters: Local protocol major [%u] minor [%u] patch [%u].\n",
rep.localMajor, rep.localMinor, rep.localPatch);
fprintf(stderr, "******NXGetControlParameters: Remote protocol major [%u] minor [%u] patch [%u].\n",
rep.remoteMajor, rep.remoteMinor, rep.remotePatch);
fprintf(stderr, "******NXGetControlParameters: Split timeout [%d] motion timeout [%d].\n",
(int) rep.splitTimeout, (int) rep.motionTimeout);
fprintf(stderr, "******NXGetControlParameters: Split mode [%d] split size [%d].\n",
(int) rep.splitMode, (int) rep.splitSize);
fprintf(stderr, "******NXGetControlParameters: Preferred pack method [%d] pack quality [%d].\n",
(int) rep.packMethod, (int) rep.packQuality);
fprintf(stderr, "******NXGetControlParameters: Data level [%d] stream level [%d] delta level [%d].\n",
rep.dataLevel, rep.streamLevel, rep.deltaLevel);
#endif
*link_type = rep.linkType;
*local_major = rep.localMajor;
*local_minor = rep.localMinor;
*local_patch = rep.localPatch;
*remote_major = rep.remoteMajor;
*remote_minor = rep.remoteMinor;
*remote_patch = rep.remotePatch;
*split_timeout = rep.splitTimeout;
*motion_timeout = rep.motionTimeout;
*split_mode = rep.splitMode;
*split_size = rep.splitSize;
*pack_method = rep.packMethod;
*pack_quality = rep.packQuality;
*data_level = rep.dataLevel;
*stream_level = rep.streamLevel;
*delta_level = rep.deltaLevel;
*load_cache = rep.loadCache;
*save_cache = rep.saveCache;
*startup_cache = rep.startupCache;
UnlockDisplay(dpy);
SyncHandle();
/*
* Install our internal out-of-sync handler.
*/
_NXLostSequenceFunction = _NXInternalLostSequenceFunction;
return 1;
}
/*
* Which unpack methods are supported by the
* remote proxy?
*/
/*
* return codes:
* 0 something went wrong
* 1 success
*/
Status NXGetUnpackParameters(Display *dpy, unsigned int *entries, unsigned char supported_methods[])
{
register xNXGetUnpackParametersReq *req;
xNXGetUnpackParametersReply rep;
register unsigned n;
if (*entries < NXNumberOfPackMethods)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetUnpackParameters: Requested only [%d] entries while they should be [%d].\n",
*entries, NXNumberOfPackMethods);
#endif
return 0;
}
LockDisplay(dpy);
GetReq(NXGetUnpackParameters, req);
req -> entries = *entries;
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetUnpackParameters: Sending message opcode [%d] with [%d] requested entries.\n",
X_NXGetUnpackParameters, *entries);
#endif
if (_XReply(dpy, (xReply *) &rep, 0, xFalse) == xFalse || rep.length == 0)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetUnpackParameters: Error receiving reply.\n");
#endif
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
if ((n = rep.length << 2) > *entries)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetUnpackParameters: Got [%d] bytes of reply data while they should be [%d].\n",
n, *entries);
#endif
_XEatData(dpy, (unsigned long) n);
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
*entries = n;
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetUnpackParameters: Reading [%d] bytes of reply data.\n", n);
#endif
_XReadPad(dpy, (char *) supported_methods, n);
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetUnpackParameters: Got reply with methods: ");
for (unsigned int i = 0; i < n; i++)
{
if (supported_methods[i] != 0)
{
fprintf(stderr, "[%d]", i);
}
}
fprintf(stderr, ".\n");
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Query and enable the MIT-SHM support between the
* proxy and the X server. The 'enable' flags must be
* true if shared memory PutImages and PutPackedImages
* are desired. On return the flags will say if support
* has been successfully enabled.
*
* Note that the the client part is not useful and not
* implemented. The size of the segment is chosen by
* the proxy. The main purpose of the message is to
* reserve the XID that will be used by the remote.
*/
/*
* return codes:
* 0 something went wrong
* 1 success
*/
Status NXGetShmemParameters(Display *dpy, unsigned int *enable_client,
unsigned int *enable_server, unsigned int *client_segment,
unsigned int *server_segment, unsigned int *client_size,
unsigned int *server_size)
{
register xNXGetShmemParametersReq *req;
register int stage;
xNXGetShmemParametersReply rep;
/*
* Save the previous handler.
*/
int (*handler)(Display *, XErrorEvent *) = _XErrorFunction;
*client_segment = 0;
*server_segment = 0;
if (*enable_client)
{
*client_segment = XAllocID(dpy);
}
if (*enable_server)
{
*server_segment = XAllocID(dpy);
}
LockDisplay(dpy);
_XErrorFunction = _NXInternalReplyErrorFunction;
for (stage = 0; stage < 3; stage++)
{
GetReq(NXGetShmemParameters, req);
req -> stage = stage;
req -> enableClient = (*enable_client != 0 ? 1 : 0);
req -> enableServer = (*enable_server != 0 ? 1 : 0);
req -> clientSegment = *client_segment;
req -> serverSegment = *server_segment;
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetShmemParameters: Sending message opcode [%d] at stage [%d].\n",
X_NXGetShmemParameters, stage);
#endif
#ifdef TEST_PARAMS
if (stage == 0)
{
fprintf(stderr, "******NXGetShmemParameters: Enable client is [%u] enable server is [%u].\n",
*enable_client, *enable_server);
fprintf(stderr, "******NXGetShmemParameters: Client segment is [%u] server segment is [%u].\n",
*client_segment, *server_segment);
}
#endif
/*
* There isn't X server reply in the second stage.
* The procedure followed at X server side is:
*
* Stage 0: Send X_QueryExtension and masquerade
* the reply.
*
* Stage 1: Allocate the shared memory and send
* X_ShmAttach to the X server.
*
* Stage 2: Send X_GetInputFocus and masquerade
* the reply.
*
* The last message is used to force a reply and
* collect any X error caused by a failure in the
* shared memory initialization.
*/
if (stage != 1)
{
/*
* We are only interested in the final reply.
*/
if (_XReply(dpy, (xReply *) &rep, 0, xTrue) == xFalse)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetShmemParameters: Error receiving reply.\n");
#endif
_XErrorFunction = handler;
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
}
}
/*
* Return the settings to client.
*/
*enable_client = rep.clientEnabled;
*enable_server = rep.serverEnabled;
*client_size = rep.clientSize;
*server_size = rep.serverSize;
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetShmemParameters: Got final reply with enabled client [%u] and server [%u].\n",
*enable_client, *enable_server);
fprintf(stderr, "******NXGetShmemParameters: Client segment size [%u] server segment size [%u].\n",
*client_size, *server_size);
#endif
_XErrorFunction = handler;
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Get the path to the font server that can be used by the X
* server to tunnel the font connections across the NX link.
* The path actually represents the TCP port where the proxy
* on the NX client side is listening. The agent can tempora-
* rily enable the tunneling when it needs a font that is not
* available on the client, for example when the session is
* migrated from a different X server.
*
* Note that it is not advisable to use the font server chan-
* nel for other purposes than restoring a font that is found
* missing at the time the session is migrated to a different
* display. This is because the agent implements a caching of
* the list of fonts supported by the client as it needs to
* advertise only the fonts that can be opened at both sides.
*/
/*
* return codes:
* 0 something went wrong
* 1 success
*/
Status NXGetFontParameters(Display *dpy, unsigned int path_length, char path_data[])
{
_X_UNUSED register xNXGetFontParametersReq *req;
xNXGetFontParametersReply rep;
register unsigned n;
#ifdef TEST_PARAMS
register unsigned i;
#endif
if (path_length < 1)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: No room to store the reply.\n");
#endif
return 0;
}
*path_data = '\0';
LockDisplay(dpy);
GetReq(NXGetFontParameters, req);
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: Sending message opcode [%d].\n",
X_NXGetFontParameters);
#endif
if (_XReply(dpy, (xReply *) &rep, 0, xFalse) == xFalse || rep.length == 0)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: Error receiving reply.\n");
#endif
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
if ((n = rep.length << 2) > path_length)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: Got [%d] bytes of reply data with only room for [%d].\n",
n, path_length);
#endif
_XEatData(dpy, (unsigned long) n);
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: Reading [%d] bytes of reply data.\n", n);
#endif
_XReadPad(dpy, (char *) path_data, n);
/*
* Check if the string can be fully
* contained by the buffer.
*/
if (*path_data > path_length - 1)
{
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: Inconsistent length in the returned string.\n");
#endif
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
#ifdef TEST_PARAMS
fprintf(stderr, "******NXGetFontParameters: Got font path of [%d] bytes and value [",
(int) *path_data);
for (i = 0; i < *path_data; i++)
{
fprintf(stderr, "%c", *(path_data + i + 1));
}
fprintf(stderr, "].\n");
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
unsigned int NXAllocSplit(Display *dpy, unsigned int resource)
{
if (resource == NXAnyResource)
{
for (resource = 0; resource < NXNumberOfResources; resource++)
{
if (_NXSplitResources[resource] == 0)
{
_NXSplitResources[resource] = 1;
#ifdef TEST_SPLIT
fprintf(stderr, "******NXAllocSplit: Reserved resource [%u].\n",
resource);
#endif
return resource;
}
}
#ifdef TEST_SPLIT
fprintf(stderr, "******NXAllocSplit: WARNING! Resource limit exhausted.\n");
#endif
return NXNoResource;
}
else if (resource >= 0 && resource < NXNumberOfResources)
{
#ifdef TEST_SPLIT
if (_NXSplitResources[resource] == 0)
{
fprintf(stderr, "******NXAllocSplit: Reserved requested resource [%u].\n",
resource);
}
else
{
fprintf(stderr, "******NXAllocSplit: Requested resource [%u] already reserved.\n",
resource);
}
#endif
_NXSplitResources[resource] = 1;
}
#ifdef PANIC
fprintf(stderr, "******NXAllocSplit: PANIC! Can't reserve requested resource [%u].\n",
resource);
#endif
return NXNoResource;
}
/*
* Tell the proxy to split the next messages.
*/
int NXStartSplit(Display *dpy, unsigned int resource, unsigned int mode)
{
register xNXStartSplitReq *req;
LockDisplay(dpy);
GetReq(NXStartSplit, req);
req -> resource = resource;
req -> mode = mode;
#ifdef TEST_SPLIT
fprintf(stderr, "******NXStartSplit: Sending opcode [%d] with resource [%d] mode [%d].\n",
X_NXStartSplit, resource, mode);
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Send the closure of the split sequence and
* tell the proxy to send the results.
*/
int NXEndSplit(Display *dpy, unsigned int resource)
{
register xNXEndSplitReq *req;
LockDisplay(dpy);
GetReq(NXEndSplit, req);
req -> resource = resource;
#ifdef TEST_SPLIT
fprintf(stderr, "******NXEndSplit: Sending opcode [%d] with resource [%d].\n",
X_NXStartSplit, resource);
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* This message must be sent whenever the proxy notifies
* the client of the completion of a split. If the 'pro-
* pagate' field is 0, the proxy will not send the ori-
* ginal request to the X server, but will only free the
* internal state.
*/
int NXCommitSplit(Display *dpy, unsigned int resource, unsigned int propagate,
unsigned char request, unsigned int position)
{
register xNXCommitSplitReq *req;
LockDisplay(dpy);
GetReq(NXCommitSplit, req);
req -> resource = resource;
req -> propagate = propagate;
req -> request = request;
req -> position = position;
#ifdef TEST_SPLIT
fprintf(stderr, "******NXCommitSplit: Sending opcode [%d] with resource [%d] propagate [%d] "
"request [%d] position [%d].\n", X_NXCommitSplit, resource,
propagate, request, position);
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
int NXAbortSplit(Display *dpy, unsigned int resource)
{
register xNXAbortSplitReq *req;
LockDisplay(dpy);
GetReq(NXAbortSplit, req);
#ifdef TEST_SPLIT
fprintf(stderr, "******NXAbortSplit: Sending message opcode [%d] with resource [%u].\n",
X_NXAbortSplit, resource);
#endif
req -> resource = resource;
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
int NXFinishSplit(Display *dpy, unsigned int resource)
{
register xNXFinishSplitReq *req;
LockDisplay(dpy);
GetReq(NXFinishSplit, req);
#ifdef TEST_SPLIT
fprintf(stderr, "******NXFinishSplit: Sending message opcode [%d] with resource [%u].\n",
X_NXFinishSplit, resource);
#endif
req -> resource = resource;
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
int NXFreeSplit(Display *dpy, unsigned int resource)
{
register xNXFreeSplitReq *req;
if (_NXSplitResources[resource] != 0)
{
LockDisplay(dpy);
GetReq(NXFreeSplit, req);
#ifdef TEST_SPLIT
fprintf(stderr, "******NXFreeSplit: Sending message opcode [%d] with resource [%u].\n",
X_NXFreeSplit, resource);
#endif
req -> resource = resource;
UnlockDisplay(dpy);
SyncHandle();
#ifdef TEST_SPLIT
fprintf(stderr, "******NXFreeSplit: Making the resource [%u] newly available.\n",
resource);
#endif
_NXSplitResources[resource] = 0;
}
#ifdef TEST_SPLIT
else
{
fprintf(stderr, "******NXFreeSplit: Nothing to do for resource [%u].\n",
resource);
}
#endif
return 1;
}
/*
* Tell to remote proxy to discard expose events
* of one or more types.
*/
int NXSetExposeParameters(Display *dpy, int expose, int graphics_expose, int no_expose)
{
register xNXSetExposeParametersReq *req;
LockDisplay(dpy);
GetReq(NXSetExposeParameters, req);
req -> expose = expose;
req -> graphicsExpose = graphics_expose;
req -> noExpose = no_expose;
#ifdef TEST_PARAMS
fprintf(stderr, "******NXSetExposeParameters: Sending message opcode [%d] with flags [%d][%d][%d].\n",
X_NXSetExposeParameters, req -> expose, req -> graphicsExpose, req -> noExpose);
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Tell to the local proxy how to handle the next requests.
*/
int NXSetCacheParameters(Display *dpy, int enable_cache, int enable_split,
int enable_save, int enable_load)
{
register xNXSetCacheParametersReq *req;
LockDisplay(dpy);
GetReq(NXSetCacheParameters, req);
req -> enableCache = enable_cache;
req -> enableSplit = enable_split;
req -> enableSave = enable_save;
req -> enableLoad = enable_load;
#ifdef TEST_PARAMS
fprintf(stderr, "******NXSetCacheParameters: Sending message opcode [%d] with "
"flags [%d][%d][%d][%d].\n", X_NXSetCacheParameters, req -> enableCache,
req -> enableSplit, req -> enableSave, req -> enableLoad);
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
unsigned int NXAllocUnpack(Display *dpy, unsigned int resource)
{
if (resource == NXAnyResource)
{
for (resource = 0; resource < NXNumberOfResources; resource++)
{
if (_NXUnpackResources[resource] == 0)
{
_NXUnpackResources[resource] = 1;
#ifdef TEST_UNPACK
fprintf(stderr, "******NXAllocUnpack: Reserved resource [%u].\n",
resource);
#endif
return resource;
}
}
#ifdef TEST_UNPACK
fprintf(stderr, "******NXAllocUnpack: WARNING! Resource limit exhausted.\n");
#endif
return NXNoResource;
}
else if (resource >= 0 && resource < NXNumberOfResources)
{
#ifdef TEST_UNPACK
if (_NXUnpackResources[resource] == 0)
{
fprintf(stderr, "******NXAllocUnpack: Reserved requested resource [%u].\n",
resource);
}
else
{
fprintf(stderr, "******NXAllocUnpack: Requested resource [%u] already reserved.\n",
resource);
}
#endif
_NXUnpackResources[resource] = 1;
}
#ifdef PANIC
fprintf(stderr, "******NXAllocUnpack: PANIC! Can't reserve requested resource [%u].\n",
resource);
#endif
return NXNoResource;
}
int NXSetUnpackGeometry(Display *dpy, unsigned int resource, Visual *visual)
{
register xNXSetUnpackGeometryReq *req;
LockDisplay(dpy);
GetReq(NXSetUnpackGeometry, req);
req -> resource = resource;
req -> depth1Bpp = _XGetBitsPerPixel(dpy, 1);
req -> depth4Bpp = _XGetBitsPerPixel(dpy, 4);
req -> depth8Bpp = _XGetBitsPerPixel(dpy, 8);
req -> depth16Bpp = _XGetBitsPerPixel(dpy, 16);
req -> depth24Bpp = _XGetBitsPerPixel(dpy, 24);
req -> depth32Bpp = _XGetBitsPerPixel(dpy, 32);
if (visual != NULL)
{
req -> redMask = visual -> red_mask;
req -> greenMask = visual -> green_mask;
req -> blueMask = visual -> blue_mask;
}
else
{
#ifdef PANIC
fprintf(stderr, "******NXSetUnpackGeometry: PANIC! Can't set the geometry without a visual.\n");
#endif
UnGetReq(NXSetUnpackGeometry);
UnlockDisplay(dpy);
return -1;
}
#ifdef TEST_UNPACK
fprintf(stderr, "******NXSetUnpackGeometry: Resource [%u] Depth/Bpp [1/%d][4/%d][8/%d]"
"[16/%d][24/%d][32/%d].\n", resource, req -> depth1Bpp, req -> depth4Bpp,
req -> depth8Bpp, req -> depth16Bpp, req -> depth24Bpp, req -> depth32Bpp);
fprintf(stderr, "******NXSetUnpackGeometry: red [0x%x] green [0x%x] blue [0x%x].\n",
(unsigned) req -> redMask, (unsigned) req -> greenMask, (unsigned) req -> blueMask);
#endif
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Store a colormap table on the remote side.
* The colormap can then be used to unpack
* an image.
*/
int NXSetUnpackColormap(Display *dpy, unsigned int resource, unsigned int method,
unsigned int entries, const char *data, unsigned int data_length)
{
register xNXSetUnpackColormapReq *req;
register int dst_data_length;
LockDisplay(dpy);
GetReq(NXSetUnpackColormap, req);
req -> resource = resource;
req -> method = method;
req -> srcLength = data_length;
req -> dstLength = entries << 2;
dst_data_length = ROUNDUP(data_length, 4);
req -> length += (dst_data_length >> 2);
#ifdef TEST_UNPACK
fprintf(stderr, "******NXSetUnpackColormap: Resource [%u] data size [%u] destination "
"data size [%u].\n", resource, data_length, dst_data_length);
#endif
if (data_length > 0)
{
if (dpy -> bufptr + dst_data_length <= dpy -> bufmax)
{
/*
* Clean the padding bytes in the request.
*/
*((int *) (dpy -> bufptr + dst_data_length - 4)) = 0x0;
memcpy(dpy -> bufptr, data, data_length);
dpy -> bufptr += dst_data_length;
}
else
{
/*
* The _XSend() will pad the request for us.
*/
_XSend(dpy, data, data_length);
}
}
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Store data of the alpha blending channel
* that will be combined with the next image
* to be unpacked.
*/
int NXSetUnpackAlpha(Display *dpy, unsigned int resource, unsigned int method,
unsigned int entries, const char *data, unsigned int data_length)
{
register xNXSetUnpackAlphaReq *req;
register unsigned int dst_data_length;
LockDisplay(dpy);
GetReq(NXSetUnpackAlpha, req);
req -> resource = resource;
req -> method = method;
req -> srcLength = data_length;
req -> dstLength = entries;
dst_data_length = ROUNDUP(data_length, 4);
req -> length += (dst_data_length >> 2);
#ifdef TEST_UNPACK
fprintf(stderr, "******NXSetUnpackAlpha: Resource [%u] data size [%u] destination data size [%u].\n",
resource, data_length, dst_data_length);
#endif
if (data_length > 0)
{
if (dpy -> bufptr + dst_data_length <= dpy -> bufmax)
{
/*
* Clean the padding bytes in the request.
*/
*((int *) (dpy -> bufptr + dst_data_length - 4)) = 0x0;
memcpy(dpy -> bufptr, data, data_length);
dpy -> bufptr += dst_data_length;
}
else
{
/*
* The _XSend() will pad the request for us.
*/
_XSend(dpy, data, data_length);
}
}
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Compatibility versions to be used when
* connected to a 1.X.X proxy.
*/
/*
* These are for compatibility with the 1.X.X
* versions.
*/
#define sz_xNXSetUnpackColormapCompatReq 8
typedef struct _NXSetUnpackColormapCompatReq {
CARD8 reqType;
CARD8 resource;
CARD16 length B16;
CARD32 entries B32;
} xNXSetUnpackColormapCompatReq;
#define X_NXSetUnpackColormapCompat X_NXSetUnpackColormap
int NXSetUnpackColormapCompat(Display *dpy, unsigned int resource,
unsigned int entries, const char *data)
{
register xNXSetUnpackColormapCompatReq *req;
register char *dst_data;
register int dst_data_length;
LockDisplay(dpy);
GetReq(NXSetUnpackColormapCompat, req);
req -> resource = resource;
req -> entries = entries;
dst_data_length = entries << 2;
req -> length += (dst_data_length >> 2);
#ifdef TEST_UNPACK
fprintf(stderr, "******NXSetUnpackColormapCompat: Resource [%u] number of entries [%u] "
"destination data size [%u].\n", resource, entries, dst_data_length);
#endif
if (entries > 0)
{
if ((dpy -> bufptr + dst_data_length) <= dpy -> bufmax)
{
dst_data = dpy -> bufptr;
}
else
{
if ((dst_data = _XAllocScratch(dpy, dst_data_length)) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXSetUnpackColormapCompat: PANIC! Cannot allocate memory.\n");
#endif
UnGetReq(NXSetUnpackColormapCompat);
UnlockDisplay(dpy);
return -1;
}
}
memcpy(dst_data, data, entries << 2);
#ifdef DUMP
fprintf(stderr, "******NXSetUnpackColormapCompat: Dumping colormap entries:\n");
for (int i = 0; i < entries; i++)
{
fprintf(stderr, "******NXSetUnpackColormapCompat: [%d] -> [0x%x].\n",
i, *((int *) (dst_data + (i * 4))));
}
#endif
if (dst_data == dpy -> bufptr)
{
dpy -> bufptr += dst_data_length;
}
else
{
_XSend(dpy, dst_data, dst_data_length);
}
}
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
#define sz_xNXSetUnpackAlphaCompatReq 8
typedef struct _NXSetUnpackAlphaCompatReq {
CARD8 reqType;
CARD8 resource;
CARD16 length B16;
CARD32 entries B32;
} xNXSetUnpackAlphaCompatReq;
#define X_NXSetUnpackAlphaCompat X_NXSetUnpackAlpha
int NXSetUnpackAlphaCompat(Display *dpy, unsigned int resource,
unsigned int entries, const char *data)
{
register xNXSetUnpackAlphaCompatReq *req;
register char *dst_data;
register unsigned int dst_data_length;
LockDisplay(dpy);
GetReq(NXSetUnpackAlphaCompat, req);
req -> resource = resource;
req -> entries = entries;
dst_data_length = ROUNDUP(entries, 4);
req -> length += (dst_data_length >> 2);
#ifdef TEST_UNPACK
fprintf(stderr, "******NXSetUnpackAlphaCompat: Resource [%u] number of entries [%u] "
"destination data size [%u].\n", resource, entries, dst_data_length);
#endif
if (entries > 0)
{
if ((dpy -> bufptr + dst_data_length) <= dpy -> bufmax)
{
dst_data = dpy -> bufptr;
}
else
{
if ((dst_data = _XAllocScratch(dpy, dst_data_length)) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXSetUnpackAlphaCompat: PANIC! Cannot allocate memory.\n");
#endif
UnGetReq(NXSetUnpackAlphaCompat);
UnlockDisplay(dpy);
return -1;
}
}
memcpy(dst_data, data, entries);
if (dst_data_length != entries)
{
memset(dst_data + entries, 0, dst_data_length - entries);
}
#ifdef DUMP
fprintf(stderr, "******NXSetUnpackAlphaCompat: Dumping alpha channel data:\n");
for (int i = 0; i < dst_data_length; i++)
{
fprintf(stderr, "******NXSetUnpackAlphaCompat: [%d] -> [0x%02x].\n",
i, ((unsigned int) *(dst_data + i)) & 0xff);
}
#endif
if (dst_data == dpy -> bufptr)
{
dpy -> bufptr += dst_data_length;
}
else
{
_XSend(dpy, dst_data, dst_data_length);
}
}
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
/*
* Free any geometry, colormap and alpha channel
* data stored by the remote proxy to unpack the
* image. Resource, as usual, must be a value
* between 0 and 255.
*/
int NXFreeUnpack(Display *dpy, unsigned int resource)
{
register xNXFreeUnpackReq *req;
if (_NXUnpackResources[resource] != 0)
{
LockDisplay(dpy);
GetReq(NXFreeUnpack, req);
#ifdef TEST_UNPACK
fprintf(stderr, "******NXFreeUnpack: Sending message opcode [%d] with resource [%u].\n",
X_NXFreeUnpack, resource);
#endif
req -> resource = resource;
UnlockDisplay(dpy);
SyncHandle();
#ifdef TEST_UNPACK
fprintf(stderr, "******NXFreeUnpack: Making the resource [%u] newly available.\n",
resource);
#endif
_NXUnpackResources[resource] = 0;
}
#ifdef TEST_UNPACK
else
{
fprintf(stderr, "******NXFreeUnpack: Nothing to do for resource [%u].\n",
resource);
}
#endif
return 1;
}
/*
* Wrapper of XCreateImage(). Note that we use offset
* field of XImage to store size of source image in
* packed format. Note also that method is currently
* not stored in the NXignored.
*/
NXPackedImage *NXCreatePackedImage(Display *dpy, Visual *visual, unsigned int method,
unsigned int depth, int format, char *data,
int data_length, unsigned int width,
unsigned int height, int bitmap_pad,
int bytes_per_line)
{
XImage* image;
image = XCreateImage(dpy, visual, depth, format, 0, data,
width, height, bitmap_pad, bytes_per_line);
if (image != NULL)
{
image -> xoffset = data_length;
}
return (NXPackedImage *) image;
}
/*
* Wrapper of XDestroyImage().
*/
int NXDestroyPackedImage(NXPackedImage *image)
{
return XDestroyImage((XImage *) image);
}
/*
* Clean the image data directly in the current buffer.
*/
int NXCleanImage(XImage *image)
{
#ifdef TEST_IMAGE
fprintf(stderr, "******NXCleanImage: Cleaning image with format [%d] depth [%d] "
"bits per pixel [%d].\n", image -> format, image -> depth,
image -> bits_per_pixel);
#endif
if (image -> format == ZPixmap)
{
if (image -> depth == 1)
{
return CleanXYImage(image);
}
else
{
return CleanZImage(image);
}
}
else
{
return CleanXYImage(image);
}
}
NXPackedImage *NXPackImage(Display *dpy, XImage *src_image, unsigned int method)
{
XImage *dst_image;
const ColorMask *mask;
unsigned int dst_data_size;
unsigned int dst_packed_data_size;
unsigned int dst_bits_per_pixel;
unsigned int dst_packed_bits_per_pixel;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPackImage: Going to pack a new image with method [%d].\n",
method);
#endif
/*
* Get the mask out of the method and
* check if the visual is supported by
* the color reduction algorithm.
*/
mask = MethodColorMask(method);
if (mask == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: WARNING! No mask to apply for pack method [%d].\n",
method);
#endif
return NULL;
}
else if (CanMaskImage(src_image, mask) == 0)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: PANIC! Invalid source with format [%d] depth [%d] bits per pixel [%d].\n",
src_image -> format, src_image -> depth, src_image -> bits_per_pixel);
fprintf(stderr, "******NXPackImage: PANIC! Visual colormask is red 0x%lx green 0x%lx blue 0x%lx.\n",
src_image -> red_mask, src_image -> green_mask, src_image -> blue_mask);
#endif
return NULL;
}
/*
* Create a destination image from
* source and apply the color mask.
*/
if ((dst_image = (XImage *) malloc(sizeof(XImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: PANIC! Cannot allocate [%d] bytes for the image.\n",
(int) sizeof(XImage));
#endif
return NULL;
}
*dst_image = *src_image;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPackImage: Source width [%d], bytes per line [%d] with depth [%d].\n",
src_image -> width, src_image -> bytes_per_line, src_image -> depth);
#endif
dst_data_size = src_image -> bytes_per_line * src_image -> height;
dst_image -> data = malloc(dst_data_size);
if (dst_image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: PANIC! Cannot allocate [%d] bytes for masked image data.\n",
dst_data_size);
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* If the pixel resulting from the mask
* needs more bits than available, then
* just clean the padding bits in the
* image.
*/
dst_bits_per_pixel = dst_image -> bits_per_pixel;
dst_packed_bits_per_pixel = MethodBitsPerPixel(method);
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPackImage: Destination depth [%d], bits per pixel [%d], packed bits per pixel [%d].\n",
dst_image -> depth, dst_bits_per_pixel, dst_packed_bits_per_pixel);
#endif
if (dst_packed_bits_per_pixel > dst_bits_per_pixel ||
ShouldMaskImage(src_image, mask) == 0)
{
/*
* Should use the same data for source
* and destination to avoid the memcpy.
*/
if (CopyAndCleanImage(src_image, dst_image) <= 0)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: PANIC! Failed to clean the image.\n");
#endif
SAFE_free(dst_image -> data);
SAFE_free(dst_image);
return NULL;
}
}
else if (MaskImage(mask, src_image, dst_image) <= 0)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: PANIC! Failed to apply the color mask.\n");
#endif
SAFE_free(dst_image -> data);
SAFE_free(dst_image);
return NULL;
}
/*
* Let's pack the same pixels in fewer bytes.
* Note that we save a new memory allocation
* by using the same image as source and des-
* tination. This means that PackImage() must
* be able to handle ovelapping areas.
*/
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPackImage: Plain bits per pixel [%d], data size [%d].\n",
dst_bits_per_pixel, dst_data_size);
#endif
dst_packed_data_size = dst_data_size * dst_packed_bits_per_pixel /
dst_bits_per_pixel;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPackImage: Packed bits per pixel [%d], data size [%d].\n",
dst_packed_bits_per_pixel, dst_packed_data_size);
#endif
if (PackImage(method, dst_data_size, dst_image,
dst_packed_data_size, dst_image) <= 0)
{
#ifdef PANIC
fprintf(stderr, "******NXPackImage: PANIC! Failed to pack image from [%d] to [%d] bits per pixel.\n",
dst_bits_per_pixel, dst_packed_bits_per_pixel);
#endif
SAFE_free(dst_image -> data);
SAFE_free(dst_image);
return NULL;
}
/*
* Save data size in xoffset field
* to comply with NX packed images.
*/
dst_image -> xoffset = dst_packed_data_size;
return dst_image;
}
/*
* NXInPlacePackImage creates a NXPackedImage
* from a XImage, sharing the same data buffer.
* Is up to the caller to free the data buffer
* only once.
*/
XImage *NXInPlacePackImage(Display *dpy, XImage *src_image, unsigned int method)
{
XImage *dst_image;
const ColorMask *mask;
unsigned int dst_data_size;
unsigned int dst_packed_data_size;
unsigned int dst_bits_per_pixel;
unsigned int dst_packed_bits_per_pixel;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXInPlacePackImage: Going to pack a new image with method [%d].\n",
method);
#endif
/*
* Get mask out of method and check if
* visual is supported by current color
* reduction algorithm.
*/
mask = MethodColorMask(method);
if (mask == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXInPlacePackImage: WARNING! No mask to apply for pack method [%d].\n",
method);
#endif
return NULL;
}
else if (CanMaskImage(src_image, mask) == 0)
{
#ifdef PANIC
fprintf(stderr, "******NXInPlacePackImage: PANIC! Invalid source with format [%d] depth [%d] bits per pixel [%d].\n",
src_image -> format, src_image -> depth, src_image -> bits_per_pixel);
fprintf(stderr, "******NXInPlacePackImage: PANIC! Visual colormask is red 0x%lx green 0x%lx blue 0x%lx.\n",
src_image -> red_mask, src_image -> green_mask, src_image -> blue_mask);
#endif
return NULL;
}
/*
* Create a destination image from
* source and apply the color mask.
*/
if ((dst_image = (XImage *) malloc(sizeof(XImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXInPlacePackImage: PANIC! Cannot allocate [%d] bytes for the image.\n",
(int) sizeof(XImage));
#endif
return NULL;
}
*dst_image = *src_image;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXInPlacePackImage: Source width [%d], bytes per line [%d] with depth [%d].\n",
src_image -> width, src_image -> bytes_per_line, src_image -> depth);
#endif
dst_data_size = src_image -> bytes_per_line * src_image -> height;
dst_image -> data = src_image -> data;
/*
* If pixel resulting from mask needs
* more bits than available, then just
* clean the pad bits in image.
*/
dst_bits_per_pixel = dst_image -> bits_per_pixel;
dst_packed_bits_per_pixel = MethodBitsPerPixel(method);
#ifdef TEST_IMAGE
fprintf(stderr, "******NXInPlacePackImage: Destination depth [%d], bits per pixel [%d], packed bits per pixel [%d].\n",
dst_image -> depth, dst_bits_per_pixel, dst_packed_bits_per_pixel);
#endif
if (dst_packed_bits_per_pixel > dst_bits_per_pixel ||
ShouldMaskImage(src_image, mask) == 0)
{
#ifdef TEST_IMAGE
fprintf(stderr, "******NXInPlacePackImage: Just clean image packed_bits_per_pixel[%d], bits_per_pixel[%d].\n",
dst_packed_bits_per_pixel, dst_bits_per_pixel);
#endif
if (NXCleanImage(dst_image) <= 0)
{
#ifdef PANIC
fprintf(stderr, "******NXInPlacePackImage: PANIC! Failed to clean the image.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
}
else if (MaskInPlaceImage(mask, dst_image) <= 0)
{
#ifdef PANIC
fprintf(stderr, "******NXInPlacePackImage: PANIC! Failed to apply the color mask.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* Let's pack the same pixels in fewer bytes.
* Note that we save a new memory allocation
* by using the same image as source and des-
* tination. This means that PackImage() must
* be able to handle ovelapping areas.
*/
#ifdef TEST_IMAGE
fprintf(stderr, "******NXInPlacePackImage: Plain bits per pixel [%d], data size [%d].\n",
dst_bits_per_pixel, dst_data_size);
#endif
dst_packed_data_size = dst_data_size * dst_packed_bits_per_pixel /
dst_bits_per_pixel;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXInPlacePackImage: Packed bits per pixel [%d], data size [%d].\n",
dst_packed_bits_per_pixel, dst_packed_data_size);
#endif
/*
* Save data size in xoffset field
* to comply with NX packed images.
*/
dst_image -> xoffset = dst_packed_data_size;
return dst_image;
}
int NXPutPackedImage(Display *dpy, unsigned int resource, Drawable drawable,
void *gc, NXPackedImage *image, unsigned int method,
unsigned int depth, int src_x, int src_y, int dst_x,
int dst_y, unsigned int width, unsigned int height)
{
register xNXPutPackedImageReq *req;
register unsigned int src_data_length;
register unsigned int dst_data_length;
LockDisplay(dpy);
FlushGC(dpy, (GC) gc);
GetReq(NXPutPackedImage, req);
req -> resource = resource;
req -> drawable = drawable;
req -> gc = ((GC) gc) -> gid;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPutPackedImage: Image resource [%d] drawable [%d] gc [%d].\n",
req -> resource, (int) req -> drawable, (int) req -> gc);
#endif
/*
* There is no leftPad field in request. We only
* support a leftPad of 0. Anyway, X imposes a
* leftPad of 0 in case of ZPixmap format.
*/
req -> format = image -> format;
/*
* Source depth, as well as width and height,
* are taken from the image structure.
*/
req -> srcDepth = image -> depth;
req -> srcX = src_x;
req -> srcY = src_y;
req -> srcWidth = image -> width;
req -> srcHeight = image -> height;
/*
* The destination depth is provided
* by the caller.
*/
req -> dstDepth = depth;
req -> dstX = dst_x;
req -> dstY = dst_y;
req -> dstWidth = width;
req -> dstHeight = height;
req -> method = method;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPutPackedImage: Source image depth [%d] destination depth [%d] "
"method [%d].\n", req -> srcDepth, req -> dstDepth, req -> method);
#endif
/*
* Source data length is the size of image in packed format,
* as stored in xoffset field of XImage. Destination data
* size is calculated according to bytes per line of target
* image, so the caller must provide the right depth at the
* time XImage structure is created.
*/
req -> srcLength = image -> xoffset;
if (image -> width == (int) width &&
image -> height == (int) height)
{
req -> dstLength = image -> bytes_per_line * image -> height;
}
else if (image -> format == ZPixmap)
{
req -> dstLength = ROUNDUP((image -> bits_per_pixel * width),
image -> bitmap_pad) * height >> 3;
}
else
{
req -> dstLength = ROUNDUP(width, image -> bitmap_pad) * height >> 3;
}
src_data_length = image -> xoffset;
dst_data_length = ROUNDUP(src_data_length, 4);
#ifdef TEST_IMAGE
fprintf(stderr, "******NXPutPackedImage: Source data length [%d] request data length [%d].\n",
src_data_length, dst_data_length);
#endif
req -> length += (dst_data_length >> 2);
if (src_data_length > 0)
{
if (dpy -> bufptr + dst_data_length <= dpy -> bufmax)
{
/*
* Clean the padding bytes in the request.
*/
*((int *) (dpy -> bufptr + dst_data_length - 4)) = 0x0;
memcpy(dpy -> bufptr, image -> data, src_data_length);
dpy -> bufptr += dst_data_length;
}
else
{
/*
* The _XSend() will pad the request for us.
*/
_XSend(dpy, image -> data, src_data_length);
}
}
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
int NXAllocColors(Display *dpy, Colormap colormap, unsigned int entries,
XColor screens_in_out[], Bool results_in_out[])
{
Status result = 0;
xAllocColorReply rep;
register xAllocColorReq *req;
Bool alloc_error = False;
LockDisplay(dpy);
for (unsigned int i = 0; i < entries; i++)
{
GetReq(AllocColor, req);
req -> cmap = colormap;
req -> red = screens_in_out[i].red;
req -> green = screens_in_out[i].green;
req -> blue = screens_in_out[i].blue;
}
for (unsigned int i = 0; i < entries; i++)
{
result = _XReply(dpy, (xReply *) &rep, 0, xTrue);
if (result)
{
screens_in_out[i].pixel = rep.pixel;
screens_in_out[i].red = rep.red;
screens_in_out[i].green = rep.green;
screens_in_out[i].blue = rep.blue;
results_in_out[i] = True;
}
else
{
results_in_out[i] = False;
alloc_error = True;
}
}
UnlockDisplay(dpy);
SyncHandle();
return (alloc_error == False);
}
char *NXEncodeColormap(const char *src_data, unsigned int src_size, unsigned int *dst_size)
{
return ColormapCompressData(src_data, src_size, dst_size);
}
char *NXEncodeAlpha(const char *src_data, unsigned int src_size, unsigned int *dst_size)
{
return AlphaCompressData(src_data, src_size, dst_size);
}
NXPackedImage *NXEncodeRgb(XImage *src_image, unsigned int method, unsigned int quality)
{
NXPackedImage *dst_image = NULL;
unsigned int dst_size;
/*
* Create a new image structure as a copy
* of the source.
*/
if ((dst_image = (NXPackedImage *) malloc(sizeof(NXPackedImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeRgb: PANIC! Cannot allocate [%d] bytes for the image.\n",
(int) sizeof(XImage));
#endif
return NULL;
}
*dst_image = *src_image;
dst_image -> data = RgbCompressData(src_image, &dst_size);
if (dst_image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeRgb: PANIC! Rgb compression failed.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* Store the Rgb size in the xoffset field.
*/
dst_image -> xoffset = dst_size;
return dst_image;
}
NXPackedImage *NXEncodeRle(XImage *src_image, unsigned int method, unsigned int quality)
{
NXPackedImage *dst_image = NULL;
unsigned int dst_size;
/*
* Create a new image structure as a copy
* of the source.
*/
if ((dst_image = (NXPackedImage *) malloc(sizeof(NXPackedImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeRle: PANIC! Cannot allocate [%d] bytes for the image.\n",
(int) sizeof(XImage));
#endif
return NULL;
}
*dst_image = *src_image;
dst_image -> data = RleCompressData(src_image, &dst_size);
if (dst_image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeRle: PANIC! Rle compression failed.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* Store the Rle size in the xoffset field.
*/
dst_image -> xoffset = dst_size;
return dst_image;
}
NXPackedImage *NXEncodeBitmap(XImage *src_image, unsigned int method, unsigned int quality)
{
NXPackedImage *dst_image = NULL;
unsigned int dst_size;
/*
* Create a new image structure as a copy
* of the source.
*/
if ((dst_image = (NXPackedImage *) malloc(sizeof(NXPackedImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeBitmap: PANIC! Cannot allocate [%d] bytes for the image.\n",
(int) sizeof(XImage));
#endif
return NULL;
}
*dst_image = *src_image;
dst_image -> data = BitmapCompressData(src_image, &dst_size);
if (dst_image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeBitmap: PANIC! Bitmap compression failed.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* Store the bitmap size in the xoffset field.
*/
dst_image -> xoffset = dst_size;
return dst_image;
}
NXPackedImage *NXEncodeJpeg(XImage *src_image, unsigned int method, unsigned int quality)
{
NXPackedImage *dst_image = NULL;
int size;
/*
* Check if the bpp of the image is valid
* for the Jpeg compression.
*/
if (src_image -> bits_per_pixel < 15)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeJpeg: PANIC! Invalid bpp for Jpeg compression [%d]\n.",
src_image -> bits_per_pixel);
#endif
return NULL;
}
/*
* Create the destination image as a copy
* of the source.
*/
if ((dst_image = (NXPackedImage *) malloc(sizeof(NXPackedImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeJpeg: PANIC! Cannot allocate [%d] bytes for the Jpeg image.\n",
(int) sizeof(NXPackedImage));
#endif
return NULL;
}
*dst_image = *src_image;
dst_image -> data = JpegCompressData(src_image, quality, &size);
if (dst_image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodeJpeg: PANIC! Jpeg compression failed.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* Store the Jpeg size in the xoffset field.
*/
dst_image -> xoffset = size;
return dst_image;
}
NXPackedImage *NXEncodePng(XImage *src_image, unsigned int method, unsigned int quality)
{
NXPackedImage *dst_image = NULL;
int size;
/*
* Check if the bpp of the image is valid
* for png compression.
*/
if (src_image -> bits_per_pixel < 15)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodePng: PANIC! Invalid bpp for Png compression [%d].\n",
src_image -> bits_per_pixel);
#endif
return NULL;
}
if ((dst_image = (NXPackedImage *) malloc(sizeof(NXPackedImage))) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodePng: PANIC! Cannot allocate [%d] bytes for the Png image.\n",
(int) sizeof(NXPackedImage));
#endif
return NULL;
}
*dst_image = *src_image;
dst_image -> data = PngCompressData(dst_image, &size);
if (dst_image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXEncodePng: PANIC! Png compression failed.\n");
#endif
SAFE_free(dst_image);
return NULL;
}
/*
* Store the Png size in the xoffset field.
*/
dst_image -> xoffset = size;
return dst_image;
}
int NXEncodeColors(XImage *src_image, NXColorTable *color_table, int nb_max)
{
int x, y, t, p;
long pixel;
/*
* We need a smarter way to extract
* the colors from the image and
* create a color table.
*/
memset(color_table, 0, nb_max * sizeof(NXColorTable));
for (x = 0, p = 0; x < src_image -> width; x++)
{
for (y = 0; y < src_image -> height; y++)
{
pixel = XGetPixel(src_image, x, y);
for (t = 0; t < nb_max; t++)
{
if ( color_table[t].found == 0)
{
color_table[t].pixel = pixel;
color_table[t].found = 1;
p++;
break;
}
else if ((color_table[t].pixel) == pixel)
{
break;
}
}
if (p == nb_max)
{
return nb_max + 1;
}
}
}
return p;
}
void NXMaskImage(XImage *image, unsigned int method)
{
unsigned int maskMethod;
const ColorMask *mask;
/*
* Choose the correct mask method
*/
switch(method)
{
case PACK_JPEG_8_COLORS:
case PACK_PNG_8_COLORS:
{
maskMethod = MASK_8_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_8_COLORS\n");
#endif
break;
}
case PACK_JPEG_64_COLORS:
case PACK_PNG_64_COLORS:
{
maskMethod = MASK_64_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_64K_COLORS\n");
#endif
break;
}
case PACK_JPEG_256_COLORS:
case PACK_PNG_256_COLORS:
{
maskMethod = MASK_256_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_256_COLORS\n");
#endif
break;
}
case PACK_JPEG_512_COLORS:
case PACK_PNG_512_COLORS:
{
maskMethod = MASK_512_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_512K_COLORS\n");
#endif
break;
}
case PACK_JPEG_4K_COLORS:
case PACK_PNG_4K_COLORS:
{
maskMethod = MASK_4K_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_4K_COLORS\n");
#endif
break;
}
case PACK_JPEG_32K_COLORS:
case PACK_PNG_32K_COLORS:
{
maskMethod = MASK_32K_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_32K_COLORS\n");
#endif
break;
}
case PACK_JPEG_64K_COLORS:
case PACK_PNG_64K_COLORS:
{
maskMethod = MASK_64K_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_64K_COLORS\n");
#endif
break;
}
case PACK_JPEG_256K_COLORS:
case PACK_PNG_256K_COLORS:
{
maskMethod = MASK_256K_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_256K_COLORS\n");
#endif
break;
}
case PACK_JPEG_2M_COLORS:
case PACK_PNG_2M_COLORS:
{
maskMethod = MASK_2M_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_2M_COLORS\n");
#endif
break;
}
case PACK_JPEG_16M_COLORS:
case PACK_PNG_16M_COLORS:
{
maskMethod = MASK_16M_COLORS;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXMaskImage: Method is MASK_16M_COLORS\n");
#endif
break;
}
default:
{
#ifdef PANIC
fprintf(stderr, "******NXMaskImage: PANIC! Cannot find mask method for pack method [%d]\n",
method);
#endif
return;
}
}
#ifdef TEST_IMAGE
fprintf(stderr, "******NXMaskImage: packMethod[%d] => maskMethod[%d]\n",
method, maskMethod);
#endif
/*
* Get mask out of method and check if
* visual is supported by current color
* reduction algorithm.
*/
mask = MethodColorMask(maskMethod);
if (mask == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXMaskImage: PANIC! No mask to apply for pack method [%d].\n",
method);
#endif
return;
}
else if (CanMaskImage(image, mask) == 0)
{
#ifdef PANIC
fprintf(stderr, "******NXMaskImage: PANIC! Invalid source with format [%d] depth [%d] bits per pixel [%d].\n",
image -> format, image -> depth, image -> bits_per_pixel);
fprintf(stderr, "******NXMaskImage: PANIC! Visual colormask is red 0x%lx green 0x%lx blue 0x%lx.\n",
image -> red_mask, image -> green_mask, image -> blue_mask);
#endif
return;
}
/*
* Calling ShouldMaskImage you get 0 in the case
* of MASK_256_COLORS and MASK_64K_COLORS, which
* means that the image should not be masked.
*/
if (ShouldMaskImage(image, mask) == 0)
{
#ifdef TEST_IMAGE
fprintf(stderr, "******NXMaskImage: the image will not be masked\n");
#endif
}
else
{
if (MaskInPlaceImage(mask, image) <= 0)
{
#ifdef PANIC
fprintf(stderr, "******NXMaskImage: PANIC! Failed to apply the color mask in place.\n");
#endif
}
}
}
/*
* The display parameter is ignored.
*/
void NXInitCache(Display *dpy, int entries)
{
if (NXImageCache != NULL && NXImageCacheSize == entries)
{
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXInitCache: Nothing to do with image cache at [%p] and [%d] entries.\n",
NXImageCache, NXImageCacheSize);
#endif
return;
}
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXInitCache: Initializing the cache with [%d] entries.\n",
entries);
#endif
NXImageCacheSize = 0;
SAFE_free(NXImageCache);
if (entries > 0)
{
NXImageCache = malloc(entries * sizeof(_NXImageCacheEntry));
if (NXImageCache != NULL)
{
memset(NXImageCache, 0, entries * sizeof(_NXImageCacheEntry));
NXImageCacheSize = entries;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXInitCache: Image cache initialized with [%d] entries.\n", entries);
#endif
}
}
}
#ifdef DUMP
void _NXCacheDump(const char *label)
{
char s[MD5_LENGTH * 2 + 1];
#ifdef DEBUG_IMAGE
fprintf(stderr, "%s: Dumping the content of image cache:\n", label);
#endif
for (int i = 0; i < NXImageCacheSize; i++)
{
if (NXImageCache[i].image == NULL)
{
break;
}
for (int j = 0; j < MD5_LENGTH; j++)
{
sprintf(s + (j * 2), "%02X", ((unsigned char *) NXImageCache[i].md5)[j]);
}
#ifdef DEBUG_IMAGE
fprintf(stderr, "%s: [%d][%s].\n", label, i, s);
#endif
}
}
#endif
XImage *NXCacheFindImage(NXPackedImage *src_image, unsigned int *method, unsigned char **md5)
{
md5_state_t new_state;
md5_byte_t *new_md5;
unsigned int data_size;
if (NXImageCache == NULL)
{
return NULL;
}
/*
* Will return the allocated checksum
* if the image is not found.
*/
*md5 = NULL;
if ((new_md5 = malloc(MD5_LENGTH)) == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCacheFindImage: Can't allocate memory for the checksum.\n");
#endif
return NULL;
}
data_size = (src_image -> bytes_per_line * src_image -> height);
md5_init(&new_state);
md5_append(&new_state, (unsigned char *) &src_image -> width, sizeof(int));
md5_append(&new_state, (unsigned char *) &src_image -> height, sizeof(int));
md5_append(&new_state, (unsigned char *) src_image -> data, data_size);
md5_finish(&new_state, new_md5);
for (unsigned int i = 0; i < NXImageCacheSize; i++)
{
if (NXImageCache[i].image != NULL)
{
if (memcmp(NXImageCache[i].md5, new_md5, MD5_LENGTH) == 0)
{
_NXImageCacheEntry found;
found.image = NXImageCache[i].image;
found.method = NXImageCache[i].method;
found.md5 = NXImageCache[i].md5;
*method = found.method;
NXImageCacheHits++;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXCacheFindImage: Found at position [%d] with hits [%d] and [%d] packs.\n",
i, NXImageCacheHits, NXImageCacheOps);
#endif
SAFE_free(new_md5);
/*
* Move the images down one slot, from
* the head of the list, and place the
* image just found at top.
*/
if (i > 16)
{
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXCacheFindImage: Moving the image at the head of the list.\n");
#endif
memmove(&NXImageCache[1], &NXImageCache[0], (i * sizeof(_NXImageCacheEntry)));
NXImageCache[0].image = found.image;
NXImageCache[0].method = found.method;
NXImageCache[0].md5 = found.md5;
#ifdef DUMP
_NXCacheDump("******NXCacheFindImage");
#endif
}
/*
* Return the checksum and image
* structure allocated in cache.
*/
*md5 = found.md5;
return found.image;
}
}
else
{
break;
}
}
*md5 = new_md5;
return NULL;
}
/*
* Add a packed image to the cache. A new image
* structure is allocated and copied, data and
* checksum are inherited from the passed image.
*/
int NXCacheAddImage(NXPackedImage *image, unsigned int method, unsigned char *md5)
{
unsigned int i;
if (image == NULL || image -> data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCacheAddImage: PANIC! Invalid image passed to function.\n");
#endif
return -1;
}
i = (NXImageCacheOps < NXImageCacheSize) ? NXImageCacheOps : NXImageCacheSize;
if (NXImageCacheOps >= NXImageCacheSize)
{
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXCacheAddImage: Freeing up the oldest entry.\n");
#endif
i--;
SAFE_free(NXImageCache[NXImageCacheSize - 1].image -> data);
SAFE_free(NXImageCache[NXImageCacheSize - 1].image);
SAFE_free(NXImageCache[NXImageCacheSize - 1].md5);
}
if (i > 0)
{
memmove(&NXImageCache[1], &NXImageCache[0], i * sizeof(_NXImageCacheEntry));
}
NXImageCacheOps++;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXCacheAddImage: Going to add new image with data size [%d].\n",
image -> xoffset);
#endif
NXImageCache[0].image = image;
NXImageCache[0].method = method;
NXImageCache[0].md5 = md5;
#ifdef DUMP
_NXCacheDump("******NXCacheAddImage");
#endif
return 1;
}
/*
* The display parameter is ignored.
*/
void NXFreeCache(Display *dpy)
{
if (NXImageCache == NULL)
{
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXFreeCache: Nothing to do with a null image cache.\n");
#endif
return;
}
#ifdef DEBUG_IMAGE
fprintf(stderr, "******NXFreeCache: Freeing the cache with [%d] entries.\n",
NXImageCacheSize);
#endif
for (int i = 0; i < NXImageCacheSize; i++)
{
if (NXImageCache[i].image != NULL)
{
SAFE_free(NXImageCache[i].image -> data);
SAFE_free(NXImageCache[i].image);
}
SAFE_free(NXImageCache[i].md5);
}
SAFE_free(NXImageCache);
NXImageCacheSize = 0;
NXImageCacheHits = 0;
NXImageCacheOps = 0;
}
static void _NXNotifyImage(Display *dpy, int resource, Bool success)
{
XEvent async_event;
/*
* Enqueue an event to tell client
* the result of GetImage.
*/
async_event.type = ClientMessage;
async_event.xclient.serial = _NXCollectedImages[resource] -> sequence;
async_event.xclient.window = 0;
async_event.xclient.message_type = 0;
async_event.xclient.format = 32;
async_event.xclient.data.l[0] = NXCollectImageNotify;
async_event.xclient.data.l[1] = resource;
async_event.xclient.data.l[2] = success;
XPutBackEvent(dpy, &async_event);
}
static Bool _NXCollectImageHandler(Display *dpy, xReply *rep, char *buf,
int len, XPointer data)
{
register _NXCollectImageState *state;
register xGetImageReply *async_rep;
char *async_head;
char *async_data;
int async_size;
state = (_NXCollectImageState *) data;
#ifdef DEBUG_IMAGE
fprintf(stderr, "******%s: sequence: received [%hu] expected [%lu] 16bit received [%hu] expected [%hu]\n",
__func__,
rep -> generic.sequenceNumber, state -> sequence,
rep -> generic.sequenceNumber % 65536, (CARD16)(state -> sequence) % 65536);
#endif
if ((rep -> generic.sequenceNumber % 65536) !=
((CARD16)(state -> sequence) % 65536))
{
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Unmatched sequence [%d] of type [%d] "
"with length [%d].\n", rep -> generic.sequenceNumber, rep -> generic.type,
(int) rep -> generic.length << 2);
#endif
return False;
}
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Going to handle asynchronous GetImage reply.\n");
#endif
/*
* As even reply data is managed asynchronously,
* we can use state to get to vector and vector
* to get to handler. In this way, we can safely
* dequeue and free the handler itself.
*/
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
if (rep -> generic.type == X_Error)
{
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Error received from X server for resource [%d].\n",
state -> resource);
#endif
_NXNotifyImage(dpy, state -> resource, False);
_NXCollectedImages[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Matched request with sequence [%ld].\n",
state -> sequence);
#endif
async_size = SIZEOF(xGetImageReply);
async_head = malloc(async_size);
if (async_head == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectImageHandler: PANIC! Failed to allocate memory with resource [%d].\n",
state -> resource);
#endif
_NXNotifyImage(dpy, state -> resource, False);
_NXCollectedImages[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Going to get reply with size [%d].\n",
(int) rep -> generic.length << 2);
#endif
async_rep = (xGetImageReply *) _XGetAsyncReply(dpy, async_head, rep, buf, len, 0, False);
if (async_rep == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectImageHandler: PANIC! Failed to get reply with resource [%d].\n",
state -> resource);
#endif
_NXNotifyImage(dpy, state -> resource, False);
_NXCollectedImages[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
return False;
}
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Got reply with depth [%d] visual [%d] size [%d].\n",
async_rep -> depth, (int) async_rep -> visual, (int) async_rep -> length << 2);
#endif
async_size = async_rep -> length << 2;
if (async_size > 0)
{
async_data = malloc(async_size);
if (async_data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectImageHandler: PANIC! Failed to allocate memory with resource [%d].\n",
state -> resource);
#endif
_NXNotifyImage(dpy, state -> resource, False);
_NXCollectedImages[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
return False;
}
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Going to get data with size [%d].\n",
async_size);
#endif
_XGetAsyncData(dpy, async_data, buf, len, SIZEOF(xGetImageReply), async_size, async_size);
/*
* From now on we can return True, as all
* data has been consumed from buffer.
*/
if (state -> format == XYPixmap)
{
unsigned long depth = DepthOnes(state -> mask & (((unsigned long)0xFFFFFFFF) >>
(32 - async_rep -> depth)));
state -> image = XCreateImage(dpy, _XVIDtoVisual(dpy, async_rep -> visual),
depth, XYPixmap, 0, async_data, state -> width,
state -> height, dpy -> bitmap_pad, 0);
}
else
{
state -> image = XCreateImage(dpy, _XVIDtoVisual(dpy, async_rep -> visual),
async_rep -> depth, ZPixmap, 0, async_data, state -> width,
state -> height, _XGetScanlinePad(dpy, async_rep -> depth), 0);
}
if (state -> image == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectImageHandler: PANIC! Failed to create image for resource [%d].\n",
state -> resource);
#endif
_NXNotifyImage(dpy, state -> resource, False);
_NXCollectedImages[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
SAFE_free(async_data);
return True;
}
#ifdef TEST_IMAGE
fprintf(stderr, "******_NXCollectImageHandler: Successfully stored image data for resource [%d].\n",
state -> resource);
#endif
}
#ifdef WARNING
else
{
fprintf(stderr, "******_NXCollectImageHandler: WARNING! Null image data stored for resource [%d].\n",
state -> resource);
}
#endif
_NXNotifyImage(dpy, state -> resource, True);
SAFE_free(async_head);
return True;
}
int NXGetCollectImageResource(Display *dpy)
{
for (int i = 0; i < NXNumberOfResources; i++)
{
if (_NXCollectedImages[i] == NULL)
{
return i;
}
}
return -1;
}
/*
* return codes:
* 0 no data found for resource (currently unused)
* -1 Failed
* True (1) Handler has been installed, will generate NXCollectImageNotify event when answer arrives
*/
int NXCollectImage(Display *dpy, unsigned int resource, Drawable drawable,
int src_x, int src_y, unsigned int width, unsigned int height,
unsigned long plane_mask, int format)
{
register xGetImageReq *req;
_NXCollectImageState *state;
_XAsyncHandler *handler;
if (resource >= NXNumberOfResources)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectImage: PANIC! Provided resource [%u] is out of range.\n",
resource);
#endif
return -1;
}
state = _NXCollectedImages[resource];
if (state != NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectImage: PANIC! Having to remove previous state for resource [%u].\n",
resource);
#endif
if (state -> handler != NULL)
{
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
}
if (state -> image != NULL)
{
XDestroyImage(state -> image);
}
SAFE_free(state);
_NXCollectedImages[resource] = NULL;
}
LockDisplay(dpy);
GetReq(GetImage, req);
req -> format = format;
req -> drawable = drawable;
req -> x = src_x;
req -> y = src_y;
req -> width = width;
req -> height = height;
req -> planeMask = plane_mask;
#ifdef TEST_IMAGE
fprintf(stderr, "******NXCollectImage: Sending message opcode [%d] sequence [%ld] for resource [%d].\n",
X_GetImage, dpy -> request, resource);
fprintf(stderr, "******NXCollectImage: Format [%d] drawable [%d] src_x [%d] src_y [%d].\n",
req -> format, (int) req -> drawable, req -> x, req -> y);
fprintf(stderr, "******NXCollectImage: Width [%d] height [%d] plane_mask [%x].\n",
req -> width, req -> height, (int) req -> planeMask);
#endif
state = malloc(sizeof(_NXCollectImageState));
handler = malloc(sizeof(_XAsyncHandler));
if (state == NULL || handler == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectImage: PANIC! Failed to allocate memory with resource [%d].\n",
resource);
#endif
UnGetReq(GetImage);
SAFE_free(state);
SAFE_free(handler);
UnlockDisplay(dpy);
return -1;
}
state -> sequence = dpy -> request;
state -> resource = resource;
state -> mask = plane_mask;
state -> format = format;
state -> width = width;
state -> height = height;
state -> image = NULL;
state -> handler = handler;
handler -> next = dpy -> async_handlers;
handler -> handler = _NXCollectImageHandler;
handler -> data = (XPointer) state;
dpy -> async_handlers = handler;
_NXCollectedImages[resource] = state;
UnlockDisplay(dpy);
SyncHandle();
return 1;
}
int NXGetCollectedImage(Display *dpy, unsigned int resource, XImage **image)
{
register _NXCollectImageState *state;
state = _NXCollectedImages[resource];
if (state == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXGetCollectedImage: PANIC! No image collected for resource [%u].\n",
resource);
#endif
return 0;
}
_NXCollectedImages[resource] = NULL;
*image = state -> image;
SAFE_free(state);
#ifdef TEST_IMAGE
fprintf(stderr, "******NXGetCollectedImage: Returning GetImage data for resource [%u].\n",
resource);
#endif
return 1;
}
static void _NXNotifyProperty(Display *dpy, int resource, Bool success)
{
XEvent async_event;
/*
* Enqueue an event to tell client
* the result of GetProperty.
*/
async_event.type = ClientMessage;
async_event.xclient.serial = _NXCollectedProperties[resource] -> sequence;
async_event.xclient.window = 0;
async_event.xclient.message_type = 0;
async_event.xclient.format = 32;
async_event.xclient.data.l[0] = NXCollectPropertyNotify;
async_event.xclient.data.l[1] = resource;
async_event.xclient.data.l[2] = success;
XPutBackEvent(dpy, &async_event);
}
static Bool _NXCollectPropertyHandler(Display *dpy, xReply *rep, char *buf,
int len, XPointer data)
{
register _NXCollectPropertyState *state;
register xGetPropertyReply *async_rep;
char *async_head;
char *async_data;
int async_size;
state = (_NXCollectPropertyState *) data;
#ifdef DEBUG_PROPERTY
fprintf(stderr, "******%s: sequence: received [%hu] expected [%lu] 16bit received [%hu] expected [%hu]\n",
__func__,
rep -> generic.sequenceNumber, state -> sequence,
rep -> generic.sequenceNumber % 65536, (CARD16)(state -> sequence) % 65536);
#endif
if ((rep -> generic.sequenceNumber % 65536) !=
((CARD16)(state -> sequence) % 65536))
{
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Unmatched sequence [%d] of type [%d] "
"with length [%d].\n", rep -> generic.sequenceNumber, rep -> generic.type,
(int) rep -> generic.length << 2);
#endif
return False;
}
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Going to handle asynchronous GetProperty reply.\n");
#endif
/*
* Reply data is managed asynchronously. We can
* use state to get to vector and vector to get
* to handler. In this way, we can dequeue and
* free the handler itself.
*/
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
if (rep -> generic.type == X_Error)
{
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Error received from X server for resource [%d].\n",
state -> resource);
#endif
_NXNotifyProperty(dpy, state -> resource, False);
_NXCollectedProperties[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Matched request with sequence [%ld].\n",
state -> sequence);
#endif
async_size = SIZEOF(xGetPropertyReply);
async_head = malloc(async_size);
if (async_head == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectPropertyHandler: PANIC! Failed to allocate memory with resource [%d].\n",
state -> resource);
#endif
_NXNotifyProperty(dpy, state -> resource, False);
_NXCollectedProperties[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Going to get reply with size [%d].\n",
(int) rep -> generic.length << 2);
#endif
async_rep = (xGetPropertyReply *) _XGetAsyncReply(dpy, async_head, rep, buf, len, 0, False);
if (async_rep == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectPropertyHandler: PANIC! Failed to get reply with resource [%d].\n",
state -> resource);
#endif
_NXNotifyProperty(dpy, state -> resource, False);
_NXCollectedProperties[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
return False;
}
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Got reply with format [%d] type [%d] size [%d].\n",
async_rep -> format, (int) async_rep -> propertyType, (int) async_rep -> length << 2);
fprintf(stderr, "******_NXCollectPropertyHandler: Bytes after [%d] number of items [%d].\n",
(int) async_rep -> bytesAfter, (int) async_rep -> nItems);
#endif
state -> format = async_rep -> format;
state -> type = async_rep -> propertyType;
state -> items = async_rep -> nItems;
state -> after = async_rep -> bytesAfter;
async_size = async_rep -> length << 2;
if (async_size > 0)
{
async_data = malloc(async_size);
if (async_data == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectPropertyHandler: PANIC! Failed to allocate memory with resource [%d].\n",
state -> resource);
#endif
_NXNotifyProperty(dpy, state -> resource, False);
_NXCollectedProperties[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
return False;
}
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Going to get data with size [%d].\n",
async_size);
#endif
_XGetAsyncData(dpy, async_data, buf, len, SIZEOF(xGetPropertyReply), async_size, async_size);
/*
* From now on we can return True, as all
* data has been consumed from buffer.
*/
state -> data = async_data;
#ifdef TEST_PROPERTY
fprintf(stderr, "******_NXCollectPropertyHandler: Successfully stored property data for resource [%d].\n",
state -> resource);
#endif
}
#ifdef TEST_PROPERTY
else
{
fprintf(stderr, "******_NXCollectPropertyHandler: WARNING! Null property data stored for resource [%d].\n",
state -> resource);
}
#endif
_NXNotifyProperty(dpy, state -> resource, True);
SAFE_free(async_head);
return True;
}
int NXGetCollectPropertyResource(Display *dpy)
{
for (int i = 0; i < NXNumberOfResources; i++)
{
if (_NXCollectedProperties[i] == NULL)
{
return i;
}
}
return -1;
}
/*
* return codes:
* 0 no data found for resource (currently unused)
* -1 Failed
* True Handler has been installed, will generate NXCollectPropertyNotify event when answer arrives
*/
int NXCollectProperty(Display *dpy, unsigned int resource, Window window, Atom property,
long long_offset, long long_length, Bool delete, Atom req_type)
{
register xGetPropertyReq *req;
_NXCollectPropertyState *state;
_XAsyncHandler *handler;
if (resource >= NXNumberOfResources)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectProperty: PANIC! Provided resource [%u] is out of range.\n",
resource);
#endif
return -1;
}
state = _NXCollectedProperties[resource];
if (state != NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectProperty: PANIC! Having to remove previous state for resource [%u].\n",
resource);
#endif
if (state -> handler != NULL)
{
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
}
SAFE_free(state->data);
SAFE_free(state);
_NXCollectedProperties[resource] = NULL;
}
LockDisplay(dpy);
GetReq(GetProperty, req);
req -> delete = delete;
req -> window = window;
req -> property = property;
req -> type = req_type;
req -> longOffset = long_offset;
req -> longLength = long_length;
#ifdef TEST_PROPERTY
fprintf(stderr, "******NXCollectProperty: Sending message opcode [%d] sequence [%ld] for resource [%d].\n",
X_GetProperty, dpy -> request, resource);
fprintf(stderr, "******NXCollectProperty: Delete [%u] window [%d] property [%d] type [%d].\n",
req -> delete, (int) req -> window, (int) req -> property, (int) req -> type);
fprintf(stderr, "******NXCollectProperty: Long offset [%d] long length [%d].\n",
(int) req -> longOffset, (int) req -> longLength);
#endif
state = malloc(sizeof(_NXCollectPropertyState));
handler = malloc(sizeof(_XAsyncHandler));
if (state == NULL || handler == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectProperty: Failed to allocate memory with resource [%d].\n",
resource);
#endif
SAFE_free(state);
SAFE_free(handler);
UnGetReq(GetProperty);
UnlockDisplay(dpy);
return -1;
}
state -> sequence = dpy -> request;
state -> resource = resource;
state -> window = window;
state -> property = property;
state -> type = 0;
state -> format = 0;
state -> items = 0;
state -> after = 0;
state -> data = NULL;
state -> handler = handler;
handler -> next = dpy -> async_handlers;
handler -> handler = _NXCollectPropertyHandler;
handler -> data = (XPointer) state;
dpy -> async_handlers = handler;
_NXCollectedProperties[resource] = state;
UnlockDisplay(dpy);
SyncHandle();
return True;
}
/*
* return codes:
* 0 not data available
* True success
*/
int NXGetCollectedProperty(Display *dpy, unsigned int resource, Atom *actual_type_return,
int *actual_format_return, unsigned long *nitems_return,
unsigned long *bytes_after_return, unsigned char **data)
{
register _NXCollectPropertyState *state;
state = _NXCollectedProperties[resource];
if (state == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXGetCollectedProperty: PANIC! No data collected for resource [%u].\n",
resource);
#endif
return 0;
}
*actual_type_return = state -> type;
*actual_format_return = state -> format;
*nitems_return = state -> items;
*bytes_after_return = state -> after;
*data = (unsigned char *) _NXCollectedProperties[resource] -> data;
SAFE_free(state);
_NXCollectedProperties[resource] = NULL;
#ifdef TEST_PROPERTY
fprintf(stderr, "******NXGetCollectedProperty: Returning GetProperty data for resource [%u].\n",
resource);
#endif
return True;
}
static void _NXNotifyGrabPointer(Display *dpy, int resource, Bool success)
{
XEvent async_event;
async_event.type = ClientMessage;
async_event.xclient.serial = _NXCollectedGrabPointers[resource] -> sequence;
async_event.xclient.window = 0;
async_event.xclient.message_type = 0;
async_event.xclient.format = 32;
async_event.xclient.data.l[0] = NXCollectGrabPointerNotify;
async_event.xclient.data.l[1] = resource;
async_event.xclient.data.l[2] = success;
XPutBackEvent(dpy, &async_event);
}
static Bool _NXCollectGrabPointerHandler(Display *dpy, xReply *rep, char *buf,
int len, XPointer data)
{
register _NXCollectGrabPointerState *state;
register xGrabPointerReply *async_rep;
char *async_head;
int async_size;
state = (_NXCollectGrabPointerState *) data;
#ifdef DEBUG_POINTER
fprintf(stderr, "******%s: sequence: received [%hu] expected [%lu] 16bit received [%hu] expected [%hu]\n",
__func__,
rep -> generic.sequenceNumber, state -> sequence,
rep -> generic.sequenceNumber % 65536, (CARD16)(state -> sequence) % 65536);
#endif
if ((rep -> generic.sequenceNumber % 65536) !=
((CARD16)(state -> sequence) % 65536))
{
#ifdef TEST_POINTER
fprintf(stderr, "******_NXCollectGrabPointerHandler: Unmatched sequence [%d] of type [%d] "
"with length [%d].\n", rep -> generic.sequenceNumber, rep -> generic.type,
(int) rep -> generic.length << 2);
#endif
return False;
}
#ifdef TEST_POINTER
fprintf(stderr, "******_NXCollectGrabPointerHandler: Going to handle asynchronous GrabPointer reply.\n");
#endif
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
if (rep -> generic.type == X_Error)
{
#ifdef TEST_POINTER
fprintf(stderr, "******_NXCollectGrabPointerHandler: Error received from X server for resource [%d].\n",
state -> resource);
#endif
_NXNotifyGrabPointer(dpy, state -> resource, False);
_NXCollectedGrabPointers[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_POINTER
fprintf(stderr, "******_NXCollectGrabPointerHandler: Matched request with sequence [%ld].\n",
state -> sequence);
#endif
async_size = SIZEOF(xGrabPointerReply);
async_head = malloc(async_size);
if (async_head == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectGrabPointerHandler: PANIC! Failed to allocate memory with resource [%d].\n",
state -> resource);
#endif
_NXNotifyGrabPointer(dpy, state -> resource, False);
_NXCollectedGrabPointers[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_POINTER
fprintf(stderr, "******_NXCollectGrabPointerHandler: Going to get reply with size [%d].\n",
(int) rep -> generic.length << 2);
#endif
async_rep = (xGrabPointerReply *) _XGetAsyncReply(dpy, async_head, rep, buf, len, 0, False);
if (async_rep == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectGrabPointerHandler: PANIC! Failed to get reply with resource [%d].\n",
state -> resource);
#endif
_NXNotifyGrabPointer(dpy, state -> resource, False);
_NXCollectedGrabPointers[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
return False;
}
#ifdef TEST_POINTER
fprintf(stderr, "******_NXCollectGrabPointerHandler: Got reply with status [%d] size [%d].\n",
async_rep -> status, (int) async_rep -> length << 2);
#endif
state -> status = async_rep -> status;
_NXNotifyGrabPointer(dpy, state -> resource, True);
SAFE_free(async_head);
return True;
}
int NXGetCollectGrabPointerResource(Display *dpy)
{
for (int i = 0; i < NXNumberOfResources; i++)
{
if (_NXCollectedGrabPointers[i] == NULL)
{
return i;
}
}
return -1;
}
/*
* return codes:
* 0 no data found for resource
* -1 Failed
* True Handler has been installed, will generate NXCollectGrabPointerNotify event when answer arrives
*/
int NXCollectGrabPointer(Display *dpy, unsigned int resource, Window grab_window, Bool owner_events,
unsigned int event_mask, int pointer_mode, int keyboard_mode,
Window confine_to, Cursor cursor, Time time)
{
register xGrabPointerReq *req;
_NXCollectGrabPointerState *state;
_XAsyncHandler *handler;
if (resource >= NXNumberOfResources)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectGrabPointer: PANIC! Provided resource [%u] is out of range.\n",
resource);
#endif
return -1;
}
state = _NXCollectedGrabPointers[resource];
if (state != NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectGrabPointer: PANIC! Having to remove previous state for resource [%u].\n",
resource);
#endif
if (state -> handler != NULL)
{
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
}
SAFE_free(state);
_NXCollectedGrabPointers[resource] = NULL;
}
LockDisplay(dpy);
GetReq(GrabPointer, req);
req -> grabWindow = grab_window;
req -> ownerEvents = owner_events;
req -> eventMask = event_mask;
req -> pointerMode = pointer_mode;
req -> keyboardMode = keyboard_mode;
req -> confineTo = confine_to;
req -> cursor = cursor;
req -> time = time;
#ifdef TEST_POINTER
fprintf(stderr, "******NXCollectGrabPointer: Sending message opcode [%d] sequence [%ld] "
"for resource [%d].\n", X_GrabPointer, dpy -> request, resource);
#endif
state = malloc(sizeof(_NXCollectGrabPointerState));
handler = malloc(sizeof(_XAsyncHandler));
if (state == NULL || handler == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectGrabPointer: Failed to allocate memory with resource [%d].\n",
resource);
#endif
SAFE_free(state);
SAFE_free(handler);
UnGetReq(GrabPointer);
UnlockDisplay(dpy);
return -1;
}
state -> sequence = dpy -> request;
state -> resource = resource;
state -> status = 0;
state -> handler = handler;
handler -> next = dpy -> async_handlers;
handler -> handler = _NXCollectGrabPointerHandler;
handler -> data = (XPointer) state;
dpy -> async_handlers = handler;
_NXCollectedGrabPointers[resource] = state;
UnlockDisplay(dpy);
SyncHandle();
return True;
}
/*
* return codes:
* 0 not data available
* True success
*/
int NXGetCollectedGrabPointer(Display *dpy, unsigned int resource, int *status)
{
register _NXCollectGrabPointerState *state;
state = _NXCollectedGrabPointers[resource];
if (state == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXGetCollectedGrabPointer: PANIC! No data collected for resource [%u].\n",
resource);
#endif
return 0;
}
*status = state -> status;
SAFE_free(state);
_NXCollectedGrabPointers[resource] = NULL;
#ifdef TEST_POINTER
fprintf(stderr, "******NXGetCollectedGrabPointer: Returning GrabPointer data for resource [%u].\n",
resource);
#endif
return True;
}
static void _NXNotifyInputFocus(Display *dpy, int resource, Bool success)
{
XEvent async_event;
async_event.type = ClientMessage;
async_event.xclient.serial = _NXCollectedInputFocuses[resource] -> sequence;
async_event.xclient.window = 0;
async_event.xclient.message_type = 0;
async_event.xclient.format = 32;
async_event.xclient.data.l[0] = NXCollectInputFocusNotify;
async_event.xclient.data.l[1] = resource;
async_event.xclient.data.l[2] = success;
XPutBackEvent(dpy, &async_event);
}
static Bool _NXCollectInputFocusHandler(Display *dpy, xReply *rep, char *buf,
int len, XPointer data)
{
register _NXCollectInputFocusState *state;
register xGetInputFocusReply *async_rep;
char *async_head;
int async_size;
state = (_NXCollectInputFocusState *) data;
#ifdef DEBUG_INPUT
fprintf(stderr, "******%s: sequence: received [%hu] expected [%lu] 16bit received [%hu] expected [%hu]\n",
__func__,
rep -> generic.sequenceNumber, state -> sequence,
rep -> generic.sequenceNumber % 65536, (CARD16)(state -> sequence) % 65536);
#endif
if ((rep -> generic.sequenceNumber % 65536) !=
((CARD16)(state -> sequence) % 65536))
{
#ifdef TEST_INPUT
fprintf(stderr, "******_NXCollectInputFocusHandler: Unmatched sequence [%d] of type [%d] "
"with length [%d].\n", rep -> generic.sequenceNumber, rep -> generic.type,
(int) rep -> generic.length << 2);
#endif
return False;
}
#ifdef TEST_INPUT
fprintf(stderr, "******_NXCollectInputFocusHandler: Going to handle asynchronous GetInputFocus reply.\n");
#endif
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
if (rep -> generic.type == X_Error)
{
#ifdef TEST_INPUT
fprintf(stderr, "******_NXCollectInputFocusHandler: Error received from X server for resource [%d].\n",
state -> resource);
#endif
_NXNotifyInputFocus(dpy, state -> resource, False);
_NXCollectedInputFocuses[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_INPUT
fprintf(stderr, "******_NXCollectInputFocusHandler: Matched request with sequence [%ld].\n",
state -> sequence);
#endif
async_size = SIZEOF(xGetInputFocusReply);
async_head = malloc(async_size);
if (async_head == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectInputFocusHandler: PANIC! Failed to allocate memory with resource [%d].\n",
state -> resource);
#endif
_NXNotifyInputFocus(dpy, state -> resource, False);
_NXCollectedInputFocuses[state -> resource] = NULL;
SAFE_free(state);
return False;
}
#ifdef TEST_INPUT
fprintf(stderr, "******_NXCollectInputFocusHandler: Going to get reply with size [%d].\n",
(int) rep -> generic.length << 2);
#endif
async_rep = (xGetInputFocusReply *) _XGetAsyncReply(dpy, async_head, rep, buf, len, 0, False);
if (async_rep == NULL)
{
#ifdef PANIC
fprintf(stderr, "******_NXCollectInputFocusHandler: PANIC! Failed to get reply with resource [%d].\n",
state -> resource);
#endif
_NXNotifyInputFocus(dpy, state -> resource, False);
_NXCollectedInputFocuses[state -> resource] = NULL;
SAFE_free(state);
SAFE_free(async_head);
return False;
}
#ifdef TEST_INPUT
fprintf(stderr, "******_NXCollectInputFocusHandler: Got reply with focus [%d] revert to [%d] "
"size [%d].\n", (int) async_rep -> focus, (int) async_rep -> revertTo,
(int) async_rep -> length << 2);
#endif
state -> focus = async_rep -> focus;
state -> revert_to = async_rep -> revertTo;
_NXNotifyInputFocus(dpy, state -> resource, True);
SAFE_free(async_head);
return True;
}
int NXGetCollectInputFocusResource(Display *dpy)
{
for (int i = 0; i < NXNumberOfResources; i++)
{
if (_NXCollectedInputFocuses[i] == NULL)
{
return i;
}
}
return -1;
}
/*
* return codes:
* 0 no data found for resource (currently unused)
* -1 Failed
* True (1) Handler has been installed, will generate NXCollectInputFocusNotify event when answer arrives
*/
int NXCollectInputFocus(Display *dpy, unsigned int resource)
{
_X_UNUSED register xReq *req;
_NXCollectInputFocusState *state;
_XAsyncHandler *handler;
if (resource >= NXNumberOfResources)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectInputFocus: PANIC! Provided resource [%u] is out of range.\n",
resource);
#endif
return -1;
}
state = _NXCollectedInputFocuses[resource];
if (state != NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectInputFocus: PANIC! Having to remove previous state for resource [%u].\n",
resource);
#endif
if (state -> handler != NULL)
{
DeqAsyncHandler(dpy, state -> handler);
SAFE_free(state -> handler);
}
SAFE_free(state);
_NXCollectedInputFocuses[resource] = NULL;
}
LockDisplay(dpy);
GetEmptyReq(GetInputFocus, req);
#ifdef TEST_INPUT
fprintf(stderr, "******NXCollectInputFocus: Sending message opcode [%d] sequence [%ld] for resource [%d].\n",
X_GetInputFocus, dpy -> request, resource);
#endif
state = malloc(sizeof(_NXCollectInputFocusState));
handler = malloc(sizeof(_XAsyncHandler));
if (state == NULL || handler == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXCollectInputFocus: Failed to allocate memory with resource [%d].\n",
resource);
#endif
SAFE_free(state);
SAFE_free(handler);
UnGetEmptyReq();
UnlockDisplay(dpy);
return -1;
}
state -> sequence = dpy -> request;
state -> resource = resource;
state -> focus = 0;
state -> revert_to = 0;
state -> handler = handler;
handler -> next = dpy -> async_handlers;
handler -> handler = _NXCollectInputFocusHandler;
handler -> data = (XPointer) state;
dpy -> async_handlers = handler;
_NXCollectedInputFocuses[resource] = state;
UnlockDisplay(dpy);
SyncHandle();
return True;
}
/*
* return codes:
* 0 not data available
* True success
*/
int NXGetCollectedInputFocus(Display *dpy, unsigned int resource,
Window *focus_return, int *revert_to_return)
{
register _NXCollectInputFocusState *state;
state = _NXCollectedInputFocuses[resource];
if (state == NULL)
{
#ifdef PANIC
fprintf(stderr, "******NXGetCollectedInputFocus: PANIC! No data collected for resource [%u].\n",
resource);
#endif
return 0;
}
*focus_return = state -> focus;
*revert_to_return = state -> revert_to;
SAFE_free(state);
_NXCollectedInputFocuses[resource] = NULL;
#ifdef TEST_INPUT
fprintf(stderr, "******NXGetCollectedInputFocus: Returning GetInputFocus data for resource [%u].\n",
resource);
#endif
return True;
}
#ifdef DUMP
void _NXDumpData(const unsigned char *buffer, unsigned int size)
{
if (buffer != NULL)
{
unsigned int i = 0;
while (i < size)
{
fprintf(stderr, "[%d]\t", i);
for (unsigned int ii = 0; i < size && ii < 8; i++, ii++)
{
fprintf(stderr, "%d\t", (unsigned int) (buffer[i]));
}
fprintf(stderr, "\n");
}
}
}
#endif
|