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
|
/* $Id: draganddrop.cpp $ */
/** @file
* X11 guest client - Drag and drop implementation.
*/
/*
* Copyright (C) 2011-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#ifdef VBOX_DND_WITH_XTEST
# include <X11/extensions/XTest.h>
#endif
#include <iprt/asm.h>
#include <iprt/buildconfig.h>
#include <iprt/critsect.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include <iprt/cpp/mtlist.h>
#include <iprt/cpp/ministring.h>
#include <limits.h>
#ifdef LOG_GROUP
# undef LOG_GROUP
#endif
#define LOG_GROUP LOG_GROUP_GUEST_DND
#include <VBox/log.h>
#include <VBox/VBoxGuestLib.h>
#include <VBox/version.h>
#include "VBox/HostServices/DragAndDropSvc.h"
#include "VBoxClient.h"
/* Enable this to handle drag'n drop "promises".
* This is needed for supporting certain applications (i.e. PcManFM on LXDE),
* which require the drag'n drop meta data a lot earlier than actually needed.
* That behavior is similar to macOS' drag'n drop promises, hence the name.
*
* Those applications query the data right while dragging over them (see GtkWidget::drag-motion),
* instead of when the source dropped the data (GtkWidget::drag-drop).
*
* This might be entirely implementation-specific, so not being a bug in GTK/GDK. Also see #9820.
*/
#ifdef VBOX_WITH_DRAG_AND_DROP_PROMISES
# undef VBOX_WITH_DRAG_AND_DROP_PROMISES
#endif
/**
* For X11 guest Xdnd is used. See http://www.acc.umu.se/~vatten/XDND.html for
* a walk trough.
*
* Also useful pages:
* - https://www.freedesktop.org/wiki/Draganddropwarts/
* - https://www.freedesktop.org/wiki/Specifications/XDNDRevision/
*
* Host -> Guest:
* For X11 this means mainly forwarding all the events from HGCM to the
* appropriate X11 events. There exists a proxy window, which is invisible and
* used for all the X11 communication. On a HGCM Enter event, we set our proxy
* window as XdndSelection owner with the given mime-types. On every HGCM move
* event, we move the X11 mouse cursor to the new position and query for the
* window below that position. Depending on if it is XdndAware, a new window or
* a known window, we send the appropriate X11 messages to it. On HGCM drop, we
* send a XdndDrop message to the current window and wait for a X11
* SelectionMessage from the target window. Because we didn't have the data in
* the requested mime-type, yet, we save that message and ask the host for the
* data. When the data is successfully received from the host, we put the data
* as a property to the window and send a X11 SelectionNotify event to the
* target window.
*
* Guest -> Host:
* This is a lot more trickery than H->G. When a pending event from HGCM
* arrives, we ask if there currently is an owner of the XdndSelection
* property. If so, our proxy window is shown (1x1, but without backing store)
* and some mouse event is triggered. This should be followed by an XdndEnter
* event send to the proxy window. From this event we can fetch the necessary
* info of the MIME types and allowed actions and send this back to the host.
* On a drop request from the host, we query for the selection and should get
* the data in the specified mime-type. This data is send back to the host.
* After that we send a XdndLeave event to the source window.
*
** @todo Cancelling (e.g. with ESC key) doesn't work.
** @todo INCR (incremental transfers) support.
** @todo Really check for the Xdnd version and the supported features.
** @todo Either get rid of the xHelpers class or properly unify the code with the drag instance class.
*/
/*********************************************************************************************************************************
* Definitions *
********************************************************************************************************************************/
/** The Xdnd protocol version we support. */
#define VBOX_XDND_VERSION (5)
/** No flags specified. */
#define VBOX_XDND_STATUS_FLAG_NONE 0
/** Whether the target window accepts the data being dragged over or not. */
#define VBOX_XDND_STATUS_FLAG_ACCEPT RT_BIT(0)
/** Whether the target window wants XdndPosition messages while dragging stuff over it. */
#define VBOX_XDND_STATUS_FLAG_WANTS_POS RT_BIT(1)
/** Whether the target window accepted the drop data or not. */
#define VBOX_XDND_FINISHED_FLAG_SUCCEEDED RT_BIT(0)
/** How many X properties our proxy window can hold. */
#define VBOX_MAX_XPROPERTIES (LONG_MAX-1)
/** The notification header text for VBClShowNotify(). */
#define VBOX_DND_SHOWNOTIFY_HEADER VBOX_PRODUCT " Drag'n Drop"
/**
* Structure for storing new X11 events and HGCM messages
* into a single event queue.
*/
typedef struct DNDEVENT
{
enum DnDEventType
{
/** Unknown event, do not use. */
DnDEventType_Unknown = 0,
/** VBGLR3DNDEVENT event. */
DnDEventType_HGCM,
/** X11 event. */
DnDEventType_X11,
/** Blow the type up to 32-bit. */
DnDEventType_32BIT_HACK = 0x7fffffff
};
/** Event type. */
DnDEventType enmType;
union
{
PVBGLR3DNDEVENT hgcm;
XEvent x11;
};
#ifdef IN_GUEST
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#endif
} DNDEVENT;
/** Pointer to a DnD event. */
typedef DNDEVENT *PDNDEVENT;
enum XA_Type
{
/* States */
XA_WM_STATE = 0,
/* Properties */
XA_TARGETS,
XA_MULTIPLE,
XA_INCR,
/* Mime Types */
XA_image_bmp,
XA_image_jpg,
XA_image_tiff,
XA_image_png,
XA_text_uri_list,
XA_text_uri,
XA_text_plain,
XA_TEXT,
/* Xdnd */
XA_XdndSelection,
XA_XdndAware,
XA_XdndEnter,
XA_XdndLeave,
XA_XdndTypeList,
XA_XdndActionList,
XA_XdndPosition,
XA_XdndActionCopy,
XA_XdndActionMove,
XA_XdndActionLink,
XA_XdndStatus,
XA_XdndDrop,
XA_XdndFinished,
/* Our own stop marker */
XA_dndstop,
/* End marker */
XA_End
};
/**
* Xdnd message value indices, sorted by message type.
*/
typedef enum XdndMsg
{
/** XdndEnter. */
XdndEnterTypeCount = 3, /* Maximum number of types in XdndEnter message. */
XdndEnterWindow = 0, /* Source window (sender). */
XdndEnterFlags, /* Version in high byte, bit 0 => more data types. */
XdndEnterType1, /* First available data type. */
XdndEnterType2, /* Second available data type. */
XdndEnterType3, /* Third available data type. */
XdndEnterMoreTypesFlag = 1, /* Set if there are more than XdndEnterTypeCount. */
XdndEnterVersionRShift = 24, /* Right shift to position version number. */
XdndEnterVersionMask = 0xFF, /* Mask to get version after shifting. */
/** XdndHere. */
XdndHereWindow = 0, /* Source window (sender). */
XdndHereFlags, /* Reserved. */
XdndHerePt, /* X + Y coordinates of mouse (root window coords). */
XdndHereTimeStamp, /* Timestamp for requesting data. */
XdndHereAction, /* Action requested by user. */
/** XdndPosition. */
XdndPositionWindow = 0, /* Source window (sender). */
XdndPositionFlags, /* Flags. */
XdndPositionXY, /* X/Y coordinates of the mouse position relative to the root window. */
XdndPositionTimeStamp, /* Time stamp for retrieving the data. */
XdndPositionAction, /* Action requested by the user. */
/** XdndStatus. */
XdndStatusWindow = 0, /* Target window (sender).*/
XdndStatusFlags, /* Flags returned by target. */
XdndStatusNoMsgXY, /* X + Y of "no msg" rectangle (root window coords). */
XdndStatusNoMsgWH, /* Width + height of "no msg" rectangle. */
XdndStatusAction, /* Action accepted by target. */
XdndStatusAcceptDropFlag = 1, /* Set if target will accept the drop. */
XdndStatusSendHereFlag = 2, /* Set if target wants a stream of XdndPosition. */
/** XdndLeave. */
XdndLeaveWindow = 0, /* Source window (sender). */
XdndLeaveFlags, /* Reserved. */
/** XdndDrop. */
XdndDropWindow = 0, /* Source window (sender). */
XdndDropFlags, /* Reserved. */
XdndDropTimeStamp, /* Timestamp for requesting data. */
/** XdndFinished. */
XdndFinishedWindow = 0, /* Target window (sender). */
XdndFinishedFlags, /* Since version 5: Bit 0 is set if the current target accepted the drop. */
XdndFinishedAction /* Since version 5: Contains the action performed by the target. */
} XdndMsg;
class DragAndDropService;
/** List of Atoms. */
#define VBoxDnDAtomList RTCList<Atom>
class xHelpers
{
public:
static xHelpers *getInstance(Display *pDisplay = 0)
{
if (!m_pInstance)
{
AssertPtrReturn(pDisplay, NULL);
m_pInstance = new xHelpers(pDisplay);
}
return m_pInstance;
}
static void destroyInstance(void)
{
if (m_pInstance)
{
delete m_pInstance;
m_pInstance = NULL;
}
}
inline Display *display() const { return m_pDisplay; }
inline Atom xAtom(XA_Type e) const { return m_xAtoms[e]; }
inline Atom stringToxAtom(const char *pcszString) const
{
return XInternAtom(m_pDisplay, pcszString, False);
}
inline RTCString xAtomToString(Atom atom) const
{
if (atom == None) return "None";
char* pcsAtom = XGetAtomName(m_pDisplay, atom);
RTCString strAtom(pcsAtom);
XFree(pcsAtom);
return strAtom;
}
inline RTCString xAtomListToString(const VBoxDnDAtomList &formatList, const RTCString &strSep = DND_FORMATS_SEPARATOR_STR)
{
RTCString format;
for (size_t i = 0; i < formatList.size(); ++i)
format += xAtomToString(formatList.at(i)) + strSep;
return format;
}
/**
* Returns a filtered X11 atom list.
*
* @returns Filtered list.
* @param formatList Atom list to convert.
* @param filterList Atom list to filter out.
*/
inline VBoxDnDAtomList xAtomListFiltered(const VBoxDnDAtomList &formatList, const VBoxDnDAtomList &filterList)
{
VBoxDnDAtomList tempList = formatList;
tempList.filter(filterList);
return tempList;
}
RTCString xErrorToString(int xRc) const;
Window applicationWindowBelowCursor(Window parentWin) const;
private:
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#endif
xHelpers(Display *pDisplay)
: m_pDisplay(pDisplay)
{
/* Not all x11 atoms we use are defined in the headers. Create the
* additional one we need here. */
for (int i = 0; i < XA_End; ++i)
m_xAtoms[i] = XInternAtom(m_pDisplay, m_xAtomNames[i], False);
};
/* Private member vars */
static xHelpers *m_pInstance;
Display *m_pDisplay;
Atom m_xAtoms[XA_End];
static const char *m_xAtomNames[XA_End];
};
/* Some xHelpers convenience defines. */
#define gX11 xHelpers::getInstance()
#define xAtom(xa) xHelpers::getInstance()->xAtom((xa))
#define xAtomToString(xa) xHelpers::getInstance()->xAtomToString((xa))
/*********************************************************************************************************************************
* xHelpers implementation. *
********************************************************************************************************************************/
xHelpers *xHelpers::m_pInstance = NULL;
/* Has to be in sync with the XA_Type enum. */
const char *xHelpers::m_xAtomNames[] =
{
/* States */
"WM_STATE",
/* Properties */
"TARGETS",
"MULTIPLE",
"INCR",
/* Mime Types */
"image/bmp",
"image/jpg",
"image/tiff",
"image/png",
"text/uri-list",
"text/uri",
"text/plain",
"TEXT",
/* Xdnd */
"XdndSelection",
"XdndAware",
"XdndEnter",
"XdndLeave",
"XdndTypeList",
"XdndActionList",
"XdndPosition",
"XdndActionCopy",
"XdndActionMove",
"XdndActionLink",
"XdndStatus",
"XdndDrop",
"XdndFinished",
/* Our own stop marker */
"dndstop"
};
RTCString xHelpers::xErrorToString(int xRc) const
{
switch (xRc)
{
case Success: return RTCStringFmt("%d (Success)", xRc); break;
case BadRequest: return RTCStringFmt("%d (BadRequest)", xRc); break;
case BadValue: return RTCStringFmt("%d (BadValue)", xRc); break;
case BadWindow: return RTCStringFmt("%d (BadWindow)", xRc); break;
case BadPixmap: return RTCStringFmt("%d (BadPixmap)", xRc); break;
case BadAtom: return RTCStringFmt("%d (BadAtom)", xRc); break;
case BadCursor: return RTCStringFmt("%d (BadCursor)", xRc); break;
case BadFont: return RTCStringFmt("%d (BadFont)", xRc); break;
case BadMatch: return RTCStringFmt("%d (BadMatch)", xRc); break;
case BadDrawable: return RTCStringFmt("%d (BadDrawable)", xRc); break;
case BadAccess: return RTCStringFmt("%d (BadAccess)", xRc); break;
case BadAlloc: return RTCStringFmt("%d (BadAlloc)", xRc); break;
case BadColor: return RTCStringFmt("%d (BadColor)", xRc); break;
case BadGC: return RTCStringFmt("%d (BadGC)", xRc); break;
case BadIDChoice: return RTCStringFmt("%d (BadIDChoice)", xRc); break;
case BadName: return RTCStringFmt("%d (BadName)", xRc); break;
case BadLength: return RTCStringFmt("%d (BadLength)", xRc); break;
case BadImplementation: return RTCStringFmt("%d (BadImplementation)", xRc); break;
}
return RTCStringFmt("%d (unknown)", xRc);
}
/** @todo Make this iterative. */
Window xHelpers::applicationWindowBelowCursor(Window wndParent) const
{
/* No parent, nothing to do. */
if(wndParent == 0)
return 0;
Window wndApp = 0;
int cProps = -1;
/* Fetch all x11 window properties of the parent window. */
Atom *pProps = XListProperties(m_pDisplay, wndParent, &cProps);
if (cProps > 0)
{
/* We check the window for the WM_STATE property. */
for (int i = 0; i < cProps; ++i)
{
if (pProps[i] == xAtom(XA_WM_STATE))
{
/* Found it. */
wndApp = wndParent;
break;
}
}
/* Cleanup */
XFree(pProps);
}
if (!wndApp)
{
Window wndChild, wndTemp;
int tmp;
unsigned int utmp;
/* Query the next child window of the parent window at the current
* mouse position. */
XQueryPointer(m_pDisplay, wndParent, &wndTemp, &wndChild, &tmp, &tmp, &tmp, &tmp, &utmp);
/* Recursive call our self to dive into the child tree. */
wndApp = applicationWindowBelowCursor(wndChild);
}
return wndApp;
}
#ifdef DEBUG
# define VBOX_DND_FN_DECL_LOG(x) inline x /* For LogFlowXXX logging. */
#else
# define VBOX_DND_FN_DECL_LOG(x) x
#endif
/**
* Class which handles a single drag'n drop proxy window.
** @todo Move all proxy window-related stuff into this class! Clean up this mess.
*/
class VBoxDnDProxyWnd
{
public:
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#endif
VBoxDnDProxyWnd(void);
virtual ~VBoxDnDProxyWnd(void);
public:
int init(Display *pDisplay);
void destroy();
int sendFinished(Window hWndSource, VBOXDNDACTION dndAction);
public:
Display *pDisp;
/** Proxy window handle. */
Window hWnd;
int iX;
int iY;
int iWidth;
int iHeight;
};
/** This class only serve to avoid dragging in generic new() and delete(). */
class WrappedXEvent
{
public:
XEvent m_Event;
public:
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#endif
WrappedXEvent(const XEvent &a_rSrcEvent)
{
m_Event = a_rSrcEvent;
}
WrappedXEvent()
{
RT_ZERO(m_Event);
}
WrappedXEvent &operator=(const XEvent &a_rSrcEvent)
{
m_Event = a_rSrcEvent;
return *this;
}
};
/**
* Class for handling a single drag and drop operation, that is,
* one source and one target at a time.
*
* For now only one DragInstance will exits when the app is running.
*/
class DragInstance
{
public:
enum State
{
Uninitialized = 0,
Initialized,
Dragging,
Dropped,
State_32BIT_Hack = 0x7fffffff
};
enum Mode
{
Unknown = 0,
HG,
GH,
Mode_32Bit_Hack = 0x7fffffff
};
#ifdef RT_NEED_NEW_AND_DELETE
RTMEM_IMPLEMENT_NEW_AND_DELETE();
#endif
DragInstance(Display *pDisplay, DragAndDropService *pParent);
public:
int init(uint32_t uScreenID);
int term(void);
void stop(void);
void reset(void);
/* X11 message processing. */
int onX11ClientMessage(const XEvent &e);
int onX11MotionNotify(const XEvent &e);
int onX11SelectionClear(const XEvent &e);
int onX11SelectionNotify(const XEvent &e);
int onX11SelectionRequest(const XEvent &evReq);
int onX11Event(const XEvent &e);
int waitForStatusChange(uint32_t enmState, RTMSINTERVAL uTimeoutMS = 30000);
bool waitForX11Msg(XEvent &evX, int iType, RTMSINTERVAL uTimeoutMS = 100);
bool waitForX11ClientMsg(XClientMessageEvent &evMsg, Atom aType, RTMSINTERVAL uTimeoutMS = 100);
/* Session handling. */
int checkForSessionChange(void);
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
/* Guest -> Host handling. */
int ghIsDnDPending(void);
int ghDropped(const RTCString &strFormat, VBOXDNDACTION dndActionRequested);
#endif
/* Host -> Guest handling. */
int hgEnter(const RTCList<RTCString> &formats, VBOXDNDACTIONLIST dndListActionsAllowed);
int hgLeave(void);
int hgMove(uint32_t uPosX, uint32_t uPosY, VBOXDNDACTION dndActionDefault);
int hgDrop(uint32_t uPosX, uint32_t uPosY, VBOXDNDACTION dndActionDefault);
int hgDataReceive(PVBGLR3GUESTDNDMETADATA pMeta);
/* X11 helpers. */
int mouseCursorFakeMove(void);
int mouseCursorMove(int iPosX, int iPosY);
void mouseButtonSet(Window wndDest, int rx, int ry, int iButton, bool fPress);
int proxyWinShow(int *piRootX = NULL, int *piRootY = NULL) const;
int proxyWinHide(void);
/* X11 window helpers. */
char *wndX11GetNameA(Window wndThis) const;
/* Xdnd protocol helpers. */
void wndXDnDClearActionList(Window wndThis) const;
void wndXDnDClearFormatList(Window wndThis) const;
int wndXDnDGetActionList(Window wndThis, VBoxDnDAtomList &lstActions) const;
int wndXDnDGetFormatList(Window wndThis, VBoxDnDAtomList &lstTypes) const;
int wndXDnDSetActionList(Window wndThis, const VBoxDnDAtomList &lstActions) const;
int wndXDnDSetFormatList(Window wndThis, Atom atmProp, const VBoxDnDAtomList &lstFormats) const;
/* Atom / HGCM formatting helpers. */
int appendFormatsToList(const RTCList<RTCString> &lstFormats, VBoxDnDAtomList &lstAtoms) const;
int appendDataToList(const void *pvData, uint32_t cbData, VBoxDnDAtomList &lstAtoms) const;
static Atom toAtomAction(VBOXDNDACTION dndAction);
static int toAtomActions(VBOXDNDACTIONLIST dndActionList, VBoxDnDAtomList &lstAtoms);
static uint32_t toHGCMAction(Atom atom);
static uint32_t toHGCMActions(const VBoxDnDAtomList &actionsList);
protected:
/** The instance's own DnD context. */
VBGLR3GUESTDNDCMDCTX m_dndCtx;
/** Pointer to service instance. */
DragAndDropService *m_pParent;
/** Pointer to X display operating on. */
Display *m_pDisplay;
/** X screen ID to operate on. */
int m_screenID;
/** Pointer to X screen operating on. */
Screen *m_pScreen;
/** Root window handle. */
Window m_wndRoot;
/** Proxy window. */
VBoxDnDProxyWnd m_wndProxy;
/** Current source/target window handle. */
Window m_wndCur;
/** The XDnD protocol version the current source/target window is using.
* Set to 0 if not available / not set yet. */
uint8_t m_uXdndVer;
/** Last mouse X position (in pixels, absolute to root window).
* Set to -1 if not set yet. */
int m_lastMouseX;
/** Last mouse Y position (in pixels, absolute to root window).
* Set to -1 if not set yet. */
int m_lastMouseY;
/** List of default (Atom) formats required for X11 Xdnd handling.
* This list will be included by \a m_lstAtomFormats. */
VBoxDnDAtomList m_lstAtomFormatsX11;
/** List of (Atom) formats the current source/target window supports. */
VBoxDnDAtomList m_lstAtomFormats;
/** List of (Atom) actions the current source/target window supports. */
VBoxDnDAtomList m_lstAtomActions;
/** Buffer for answering the target window's selection request. */
void *m_pvSelReqData;
/** Size (in bytes) of selection request data buffer. */
uint32_t m_cbSelReqData;
/** Current operation mode. */
volatile uint32_t m_enmMode;
/** Current state of operation mode. */
volatile uint32_t m_enmState;
/** The instance's own X event queue. */
RTCMTList<WrappedXEvent> m_eventQueueList;
/** Critical section for providing serialized access to list event queue's contents. */
RTCRITSECT m_eventQueueCS;
/** Event for notifying this instance in case of a new event. */
RTSEMEVENT m_eventQueueEvent;
/** Critical section for data access. */
RTCRITSECT m_dataCS;
/** List of allowed formats. */
RTCList<RTCString> m_lstAllowedFormats;
/** Number of failed attempts by the host
* to query for an active drag and drop operation on the guest. */
uint16_t m_cFailedPendingAttempts;
};
/**
* Service class which implements drag'n drop.
*/
class DragAndDropService
{
public:
DragAndDropService(void)
: m_pDisplay(NULL)
, m_hHGCMThread(NIL_RTTHREAD)
, m_hX11Thread(NIL_RTTHREAD)
, m_hEventSem(NIL_RTSEMEVENT)
, m_pCurDnD(NULL)
, m_fStop(false)
{
RT_ZERO(m_dndCtx);
}
int init(void);
int worker(bool volatile *pfShutdown);
void reset(void);
void stop(void);
int term(void);
private:
static DECLCALLBACK(int) hgcmEventThread(RTTHREAD hThread, void *pvUser);
static DECLCALLBACK(int) x11EventThread(RTTHREAD hThread, void *pvUser);
/* Private member vars */
Display *m_pDisplay;
/** Our (thread-safe) event queue with mixed events (DnD HGCM / X11). */
RTCMTList<DNDEVENT> m_eventQueue;
/** Critical section for providing serialized access to list
* event queue's contents. */
RTCRITSECT m_eventQueueCS;
/** Thread handle for the HGCM message pumping thread. */
RTTHREAD m_hHGCMThread;
/** Thread handle for the X11 message pumping thread. */
RTTHREAD m_hX11Thread;
/** This service' DnD command context. */
VBGLR3GUESTDNDCMDCTX m_dndCtx;
/** Event semaphore for new DnD events. */
RTSEMEVENT m_hEventSem;
/** Pointer to the allocated DnD instance.
Currently we only support and handle one instance at a time. */
DragInstance *m_pCurDnD;
/** Stop indicator flag to signal the thread that it should shut down. */
bool m_fStop;
friend class DragInstance;
} g_Svc;
/*********************************************************************************************************************************
* DragInstanc implementation. *
********************************************************************************************************************************/
DragInstance::DragInstance(Display *pDisplay, DragAndDropService *pParent)
: m_pParent(pParent)
, m_pDisplay(pDisplay)
, m_pScreen(0)
, m_wndRoot(0)
, m_wndCur(0)
, m_uXdndVer(0)
, m_pvSelReqData(NULL)
, m_cbSelReqData(0)
, m_enmMode(Unknown)
, m_enmState(Uninitialized)
{
/* Append default targets we support.
* Note: The order is sorted by preference; be careful when changing this. */
m_lstAtomFormatsX11.append(xAtom(XA_TARGETS));
m_lstAtomFormatsX11.append(xAtom(XA_MULTIPLE));
/** @todo Support INC (incremental transfers). */
}
/**
* Stops this drag instance.
*/
void DragInstance::stop(void)
{
LogFlowFuncEnter();
int rc2 = VbglR3DnDDisconnect(&m_dndCtx);
AssertRC(rc2);
LogFlowFuncLeave();
}
/**
* Terminates (destroys) this drag instance.
*
* @return VBox status code.
*/
int DragInstance::term(void)
{
LogFlowFuncEnter();
if (m_wndProxy.hWnd != 0)
XDestroyWindow(m_pDisplay, m_wndProxy.hWnd);
int rc = VbglR3DnDDisconnect(&m_dndCtx);
AssertRCReturn(rc, rc);
if (m_pvSelReqData)
RTMemFree(m_pvSelReqData);
rc = RTSemEventDestroy(m_eventQueueEvent);
AssertRCReturn(rc, rc);
rc = RTCritSectDelete(&m_eventQueueCS);
AssertRCReturn(rc, rc);
rc = RTCritSectDelete(&m_dataCS);
AssertRCReturn(rc, rc);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Resets this drag instance.
*/
void DragInstance::reset(void)
{
LogFlowFuncEnter();
/* Hide the proxy win. */
proxyWinHide();
int rc2 = RTCritSectEnter(&m_dataCS);
if (RT_SUCCESS(rc2))
{
/* If we are currently the Xdnd selection owner, clear that. */
Window pWnd = XGetSelectionOwner(m_pDisplay, xAtom(XA_XdndSelection));
if (pWnd == m_wndProxy.hWnd)
XSetSelectionOwner(m_pDisplay, xAtom(XA_XdndSelection), None, CurrentTime);
/* Clear any other DnD specific data on the proxy window. */
wndXDnDClearFormatList(m_wndProxy.hWnd);
wndXDnDClearActionList(m_wndProxy.hWnd);
m_lstAtomActions.clear();
/* First, clear the formats list and apply the X11-specific default formats,
* required for making Xdnd to work. */
m_lstAtomFormats.clear();
m_lstAtomFormats.append(m_lstAtomFormatsX11);
m_wndCur = 0;
m_uXdndVer = 0;
m_lastMouseX = -1;
m_lastMouseY = -1;
m_enmState = Initialized;
m_enmMode = Unknown;
m_cFailedPendingAttempts = 0;
/* Reset the selection request buffer. */
if (m_pvSelReqData)
{
RTMemFree(m_pvSelReqData);
m_pvSelReqData = NULL;
Assert(m_cbSelReqData);
m_cbSelReqData = 0;
}
rc2 = RTCritSectEnter(&m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
m_eventQueueList.clear();
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
}
RTCritSectLeave(&m_dataCS);
}
LogFlowFuncLeave();
}
/**
* Initializes this drag instance.
*
* @return IPRT status code.
* @param uScreenID X' screen ID to use.
*/
int DragInstance::init(uint32_t uScreenID)
{
int rc = VbglR3DnDConnect(&m_dndCtx);
/* Note: Can return VINF_PERMISSION_DENIED if HGCM host service is not available. */
if (rc != VINF_SUCCESS)
return rc;
if (g_cVerbosity)
{
RTCString strBody = RTCStringFmt("Connected (screen %RU32, verbosity %u)", uScreenID, g_cVerbosity);
VBClShowNotify(VBOX_DND_SHOWNOTIFY_HEADER, strBody.c_str());
}
do
{
rc = RTSemEventCreate(&m_eventQueueEvent);
if (RT_FAILURE(rc))
break;
rc = RTCritSectInit(&m_eventQueueCS);
if (RT_FAILURE(rc))
break;
rc = RTCritSectInit(&m_dataCS);
if (RT_FAILURE(rc))
break;
/*
* Enough screens configured in the x11 server?
*/
if ((int)uScreenID > ScreenCount(m_pDisplay))
{
rc = VERR_INVALID_PARAMETER;
break;
}
#if 0
/* Get the screen number from the x11 server. */
pDrag->screen = ScreenOfDisplay(m_pDisplay, uScreenID);
if (!pDrag->screen)
{
rc = VERR_GENERAL_FAILURE;
break;
}
#endif
m_screenID = uScreenID;
/* Now query the corresponding root window of this screen. */
m_wndRoot = RootWindow(m_pDisplay, m_screenID);
if (!m_wndRoot)
{
rc = VERR_GENERAL_FAILURE;
break;
}
/*
* Create an invisible window which will act as proxy for the DnD
* operation. This window will be used for both the GH and HG
* direction.
*/
XSetWindowAttributes attr;
RT_ZERO(attr);
attr.event_mask = EnterWindowMask | LeaveWindowMask
| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
attr.override_redirect = True;
attr.do_not_propagate_mask = NoEventMask;
if (g_cVerbosity >= 3)
{
attr.background_pixel = XWhitePixel(m_pDisplay, m_screenID);
attr.border_pixel = XBlackPixel(m_pDisplay, m_screenID);
m_wndProxy.hWnd = XCreateWindow(m_pDisplay, m_wndRoot /* Parent */,
100, 100, /* Position */
100, 100, /* Width + height */
2, /* Border width */
CopyFromParent, /* Depth */
InputOutput, /* Class */
CopyFromParent, /* Visual */
CWBackPixel
| CWBorderPixel
| CWOverrideRedirect
| CWDontPropagate, /* Value mask */
&attr); /* Attributes for value mask */
}
m_wndProxy.hWnd = XCreateWindow(m_pDisplay, m_wndRoot /* Parent */,
0, 0, /* Position */
1, 1, /* Width + height */
0, /* Border width */
CopyFromParent, /* Depth */
InputOnly, /* Class */
CopyFromParent, /* Visual */
CWOverrideRedirect | CWDontPropagate, /* Value mask */
&attr); /* Attributes for value mask */
if (!m_wndProxy.hWnd)
{
VBClLogError("Error creating proxy window\n");
rc = VERR_GENERAL_FAILURE;
break;
}
rc = m_wndProxy.init(m_pDisplay);
if (RT_FAILURE(rc))
{
VBClLogError("Error initializing proxy window, rc=%Rrc\n", rc);
break;
}
if (g_cVerbosity >= 3) /* Make debug window visible. */
{
XFlush(m_pDisplay);
XMapWindow(m_pDisplay, m_wndProxy.hWnd);
XRaiseWindow(m_pDisplay, m_wndProxy.hWnd);
XFlush(m_pDisplay);
}
VBClLogInfo("Proxy window=%#x (debug mode: %RTbool), root window=%#x ...\n",
m_wndProxy.hWnd, RT_BOOL(g_cVerbosity >= 3), m_wndRoot);
/* Set the window's name for easier lookup. */
XStoreName(m_pDisplay, m_wndProxy.hWnd, "VBoxClientWndDnD");
/* Make the new window Xdnd aware. */
Atom atmVer = VBOX_XDND_VERSION;
XChangeProperty(m_pDisplay, m_wndProxy.hWnd, xAtom(XA_XdndAware), XA_ATOM, 32, PropModeReplace,
reinterpret_cast<unsigned char*>(&atmVer), 1);
} while (0);
if (RT_SUCCESS(rc))
{
reset();
}
else
VBClLogError("Initializing drag instance for screen %RU32 failed with rc=%Rrc\n", uScreenID, rc);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Callback handler for a generic client message from a window.
*
* @return IPRT status code.
* @param e X11 event to handle.
*/
int DragInstance::onX11ClientMessage(const XEvent &e)
{
AssertReturn(e.type == ClientMessage, VERR_INVALID_PARAMETER);
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
LogFlowThisFunc(("Event wnd=%#x, msg=%s\n", e.xclient.window, xAtomToString(e.xclient.message_type).c_str()));
int rc = VINF_SUCCESS;
char *pszWndCurName = wndX11GetNameA(m_wndCur);
AssertPtrReturn(pszWndCurName, VERR_NO_MEMORY);
switch (m_enmMode)
{
case HG:
{
/*
* Client messages are used to inform us about the status of a XdndAware
* window, in response of some events we send to them.
*/
/* The target window informs us of the current Xdnd status. */
if (e.xclient.message_type == xAtom(XA_XdndStatus))
{
Window wndTgt = static_cast<Window>(e.xclient.data.l[XdndStatusWindow]);
char *pszWndTgtName = wndX11GetNameA(wndTgt);
AssertPtrBreakStmt(pszWndTgtName, VERR_NO_MEMORY);
/* Does the target accept the drop? */
bool const fAcceptDrop = RT_BOOL(e.xclient.data.l[XdndStatusFlags] & VBOX_XDND_STATUS_FLAG_ACCEPT);
/* Does the target want XdndPosition messages? */
bool const fWantsPosition = RT_BOOL(e.xclient.data.l[XdndStatusFlags] & VBOX_XDND_STATUS_FLAG_WANTS_POS);
/*
* The XdndStatus message tell us if the window will accept the DnD
* event and with which action. We immediately send this info down to
* the host as a response of a previous DnD message.
*/
RTCString strActions = xAtomToString(e.xclient.data.l[XdndStatusAction]);
VBClLogInfo("Target window %#x ('%s')\n", wndTgt, pszWndTgtName);
VBClLogInfo(" - %s accept data (actions '%s')\n", fAcceptDrop ? "does" : "does not", strActions.c_str());
VBClLogInfo(" - %s want position messages\n", fWantsPosition ? "does" : "does not");
uint16_t const x = RT_HI_U16((uint32_t)e.xclient.data.l[XdndStatusNoMsgXY]);
uint16_t const y = RT_LO_U16((uint32_t)e.xclient.data.l[XdndStatusNoMsgXY]);
uint16_t const cx = RT_HI_U16((uint32_t)e.xclient.data.l[XdndStatusNoMsgWH]);
uint16_t const cy = RT_LO_U16((uint32_t)e.xclient.data.l[XdndStatusNoMsgWH]);
if (cx && cy)
{
VBClLogInfo("Target window %#x ('%s') reported dead area at %RU16,%RU16 (%RU16 x %RU16)\n",
wndTgt, pszWndTgtName, x, y, cx, cy);
/** @todo Save dead area and don't send XdndPosition messages anymore into it. */
}
if (m_wndCur == wndTgt)
{
VBOXDNDACTION dndAction = VBOX_DND_ACTION_IGNORE; /* Default is ignoring. */
/** @todo Compare this with the allowed actions. */
if (fAcceptDrop)
dndAction = toHGCMAction(static_cast<Atom>(e.xclient.data.l[XdndStatusAction]));
rc = VbglR3DnDHGSendAckOp(&m_dndCtx, dndAction);
}
else
VBClLogInfo("Target window %#x ('%s') is not our current window, skipping\n", wndTgt, pszWndTgtName);
RTStrFree(pszWndTgtName);
}
/* The target window informs us that it finished the Xdnd operation and that we may free all data. */
else if (e.xclient.message_type == xAtom(XA_XdndFinished))
{
Window wndTarget = static_cast<Window>(e.xclient.data.l[XdndFinishedWindow]);
char *pszWndTgtName = wndX11GetNameA(wndTarget);
AssertPtrBreakStmt(pszWndTgtName, VERR_NO_MEMORY);
if (m_uXdndVer >= 5)
{
const bool fSucceeded = e.xclient.data.l[XdndFinishedFlags] & VBOX_XDND_FINISHED_FLAG_SUCCEEDED;
#if 0 /** @todo Returns garbage -- investigate this! */
//const char *pcszAction = fSucceeded ? xAtomToString(e.xclient.data.l[XdndFinishedAction]).c_str() : NULL;
#endif
VBClLogInfo("Target window %#x ('%s') has %s the data\n",
wndTarget, pszWndTgtName, fSucceeded ? "accepted" : "rejected");
}
else /* Xdnd < version 5 did not have the XdndFinishedFlags / XdndFinishedAction properties. */
VBClLogInfo("Target window %#x ('%s') has accepted the data\n", wndTarget, pszWndTgtName);
RTStrFree(pszWndTgtName);
reset();
}
else
{
LogFlowThisFunc(("Unhandled client message '%s'\n", xAtomToString(e.xclient.message_type).c_str()));
rc = VERR_NOT_SUPPORTED;
}
break;
}
case Unknown: /* Mode not set (yet). */
RT_FALL_THROUGH();
case GH:
{
/*
* This message marks the beginning of a new drag and drop
* operation on the guest.
*/
if (e.xclient.message_type == xAtom(XA_XdndEnter))
{
/*
* Get the window which currently has the XA_XdndSelection
* bit set.
*/
Window wndSel = XGetSelectionOwner(m_pDisplay, xAtom(XA_XdndSelection));
char *pszWndSelName = wndX11GetNameA(wndSel);
AssertPtrBreakStmt(pszWndSelName, VERR_NO_MEMORY);
mouseButtonSet(m_wndProxy.hWnd, -1, -1, 1, true /* fPress */);
/*
* Update our state and the window handle to process.
*/
rc = RTCritSectEnter(&m_dataCS);
if (RT_SUCCESS(rc))
{
uint8_t const uXdndVer = (uint8_t)e.xclient.data.l[XdndEnterFlags] >> XdndEnterVersionRShift;
VBClLogInfo("Entered new source window %#x ('%s'), supports Xdnd version %u\n", wndSel, pszWndSelName, uXdndVer);
#ifdef DEBUG
XWindowAttributes xwa;
XGetWindowAttributes(m_pDisplay, m_wndCur, &xwa);
LogFlowThisFunc(("wndCur=%#x, x=%d, y=%d, width=%d, height=%d\n", m_wndCur, xwa.x, xwa.y, xwa.width, xwa.height));
#endif
/*
* Retrieve supported formats.
*/
/* Check if the MIME types are in the message itself or if we need
* to fetch the XdndTypeList property from the window. */
bool fMoreTypes = e.xclient.data.l[XdndEnterFlags] & XdndEnterMoreTypesFlag;
if (!fMoreTypes)
{
/* Only up to 3 format types supported. */
/* Start with index 2 (first item). */
for (int i = 2; i < 5; i++)
{
LogFlowThisFunc(("\t%s\n", gX11->xAtomToString(e.xclient.data.l[i]).c_str()));
m_lstAtomFormats.append(e.xclient.data.l[i]);
}
}
else
{
/* More than 3 format types supported. */
rc = wndXDnDGetFormatList(wndSel, m_lstAtomFormats);
}
if (RT_FAILURE(rc))
{
VBClLogError("Error retrieving supported formats, rc=%Rrc\n", rc);
break;
}
/*
* Retrieve supported actions.
*/
if (uXdndVer >= 2) /* More than one action allowed since protocol version 2. */
{
rc = wndXDnDGetActionList(wndSel, m_lstAtomActions);
}
else /* Only "copy" action allowed on legacy applications. */
m_lstAtomActions.append(XA_XdndActionCopy);
if (RT_FAILURE(rc))
{
VBClLogError("Error retrieving supported actions, rc=%Rrc\n", rc);
break;
}
VBClLogInfo("Source window %#x ('%s')\n", wndSel, pszWndSelName);
VBClLogInfo(" - supports the formats ");
for (size_t i = 0; i < m_lstAtomFormats.size(); i++)
{
if (i > 0)
VBClLogInfo(", ");
VBClLogInfo("%s", gX11->xAtomToString(m_lstAtomFormats[i]).c_str());
}
VBClLogInfo("\n");
VBClLogInfo(" - supports the actions ");
for (size_t i = 0; i < m_lstAtomActions.size(); i++)
{
if (i > 0)
VBClLogInfo(", ");
VBClLogInfo("%s", gX11->xAtomToString(m_lstAtomActions[i]).c_str());
}
VBClLogInfo("\n");
AssertBreakStmt(wndSel == (Window)e.xclient.data.l[XdndEnterWindow],
rc = VERR_INVALID_PARAMETER); /* Source window. */
m_wndCur = wndSel;
m_uXdndVer = uXdndVer;
m_enmMode = GH;
m_enmState = Dragging;
RTCritSectLeave(&m_dataCS);
}
RTStrFree(pszWndSelName);
}
else if ( e.xclient.message_type == xAtom(XA_XdndPosition)
&& m_wndCur == static_cast<Window>(e.xclient.data.l[XdndPositionWindow]))
{
if (m_enmState != Dragging) /* Wrong mode? Bail out. */
{
reset();
break;
}
#ifdef LOG_ENABLED
int32_t iPos = e.xclient.data.l[XdndPositionXY];
Atom atmAction = m_uXdndVer >= 2 /* Actions other than "copy" or only supported since protocol version 2. */
? e.xclient.data.l[XdndPositionAction] : xAtom(XA_XdndActionCopy);
LogFlowThisFunc(("XA_XdndPosition: wndProxy=%#x, wndCur=%#x, x=%RI32, y=%RI32, strAction=%s\n",
m_wndProxy.hWnd, m_wndCur, RT_HIWORD(iPos), RT_LOWORD(iPos),
xAtomToString(atmAction).c_str()));
#endif
bool fAcceptDrop = true;
/* Reply with a XdndStatus message to tell the source whether
* the data can be dropped or not. */
XClientMessageEvent m;
RT_ZERO(m);
m.type = ClientMessage;
m.display = m_pDisplay;
m.window = e.xclient.data.l[XdndPositionWindow];
m.message_type = xAtom(XA_XdndStatus);
m.format = 32;
m.data.l[XdndStatusWindow] = m_wndProxy.hWnd;
m.data.l[XdndStatusFlags] = fAcceptDrop ? VBOX_XDND_STATUS_FLAG_ACCEPT : VBOX_XDND_STATUS_FLAG_NONE; /* Whether to accept the drop or not. */
/* We don't want any new XA_XdndPosition messages while being
* in our proxy window. */
m.data.l[XdndStatusNoMsgXY] = RT_MAKE_U32(m_wndProxy.iY, m_wndProxy.iX);
m.data.l[XdndStatusNoMsgWH] = RT_MAKE_U32(m_wndProxy.iHeight, m_wndProxy.iWidth);
/** @todo Handle default action! */
m.data.l[XdndStatusAction] = fAcceptDrop ? toAtomAction(VBOX_DND_ACTION_COPY) : None;
int xRc = XSendEvent(m_pDisplay, e.xclient.data.l[XdndPositionWindow],
False /* Propagate */, NoEventMask, reinterpret_cast<XEvent *>(&m));
if (xRc == 0)
VBClLogError("Error sending position status event to current window %#x ('%s'): %s\n",
m_wndCur, pszWndCurName, gX11->xErrorToString(xRc).c_str());
}
else if ( e.xclient.message_type == xAtom(XA_XdndLeave)
&& m_wndCur == static_cast<Window>(e.xclient.data.l[XdndLeaveWindow]))
{
LogFlowThisFunc(("XA_XdndLeave\n"));
VBClLogInfo("Guest to host transfer canceled by the guest source window\n");
/* Start over. */
reset();
}
else if ( e.xclient.message_type == xAtom(XA_XdndDrop)
&& m_wndCur == static_cast<Window>(e.xclient.data.l[XdndDropWindow]))
{
LogFlowThisFunc(("XA_XdndDrop\n"));
if (m_enmState != Dropped) /* Wrong mode? Bail out. */
{
/* Can occur when dragging from guest->host, but then back in to the guest again. */
VBClLogInfo("Could not drop on own proxy window\n"); /* Not fatal. */
/* Let the source know. */
rc = m_wndProxy.sendFinished(m_wndCur, VBOX_DND_ACTION_IGNORE);
/* Start over. */
reset();
break;
}
m_eventQueueList.append(e);
rc = RTSemEventSignal(m_eventQueueEvent);
}
else /* Unhandled event, abort. */
{
VBClLogInfo("Unhandled event from wnd=%#x, msg=%s\n", e.xclient.window, xAtomToString(e.xclient.message_type).c_str());
/* Let the source know. */
rc = m_wndProxy.sendFinished(m_wndCur, VBOX_DND_ACTION_IGNORE);
/* Start over. */
reset();
}
break;
}
default:
{
AssertMsgFailed(("Drag and drop mode not implemented: %RU32\n", m_enmMode));
rc = VERR_NOT_IMPLEMENTED;
break;
}
}
RTStrFree(pszWndCurName);
LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
return rc;
}
int DragInstance::onX11MotionNotify(const XEvent &e)
{
RT_NOREF1(e);
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
return VINF_SUCCESS;
}
/**
* Callback handler for being notified if some other window now
* is the owner of the current selection.
*
* @return IPRT status code.
* @param e X11 event to handle.
*
* @remark
*/
int DragInstance::onX11SelectionClear(const XEvent &e)
{
RT_NOREF1(e);
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
return VINF_SUCCESS;
}
/**
* Callback handler for a XDnD selection notify from a window. This is needed
* to let the us know if a certain window has drag'n drop data to share with us,
* e.g. our proxy window.
*
* @return IPRT status code.
* @param e X11 event to handle.
*/
int DragInstance::onX11SelectionNotify(const XEvent &e)
{
AssertReturn(e.type == SelectionNotify, VERR_INVALID_PARAMETER);
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
int rc;
switch (m_enmMode)
{
case GH:
{
if (m_enmState == Dropped)
{
m_eventQueueList.append(e);
rc = RTSemEventSignal(m_eventQueueEvent);
}
else
rc = VERR_WRONG_ORDER;
break;
}
default:
{
LogFlowThisFunc(("Unhandled: wnd=%#x, msg=%s\n",
e.xclient.data.l[0], xAtomToString(e.xclient.message_type).c_str()));
rc = VERR_INVALID_STATE;
break;
}
}
LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
return rc;
}
/**
* Callback handler for a XDnD selection request from a window. This is needed
* to retrieve the data required to complete the actual drag'n drop operation.
*
* @returns IPRT status code.
* @param evReq X11 event to handle.
*/
int DragInstance::onX11SelectionRequest(const XEvent &evReq)
{
AssertReturn(evReq.type == SelectionRequest, VERR_INVALID_PARAMETER);
const XSelectionRequestEvent *pEvReq = &evReq.xselectionrequest;
char *pszWndSrcName = wndX11GetNameA(pEvReq->owner);
AssertPtrReturn(pszWndSrcName, VERR_INVALID_POINTER);
char *pszWndTgtName = wndX11GetNameA(pEvReq->requestor);
AssertPtrReturn(pszWndTgtName, VERR_INVALID_POINTER);
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
LogFlowThisFunc(("Event owner=%#x ('%s'), requestor=%#x ('%s'), selection=%s, target=%s, prop=%s, time=%u\n",
pEvReq->owner, pszWndSrcName,
pEvReq->requestor, pszWndTgtName,
xAtomToString(pEvReq->selection).c_str(),
xAtomToString(pEvReq->target).c_str(),
xAtomToString(pEvReq->property).c_str(),
pEvReq->time));
VBClLogInfo("Window '%s' is asking '%s' for '%s' / '%s'\n",
pszWndTgtName, pszWndSrcName, xAtomToString(pEvReq->selection).c_str(), xAtomToString(pEvReq->property).c_str());
RTStrFree(pszWndSrcName);
/* Note: pszWndTgtName will be free'd below. */
int rc;
switch (m_enmMode)
{
case HG:
{
rc = VINF_SUCCESS;
/*
* Start by creating a refusal selection notify message.
* That way we only need to care for the success case.
*/
XEvent evResp;
RT_ZERO(evResp);
XSelectionEvent *pEvResp = &evResp.xselection;
pEvResp->type = SelectionNotify;
pEvResp->display = pEvReq->display;
pEvResp->requestor = pEvReq->requestor;
pEvResp->selection = pEvReq->selection;
pEvResp->target = pEvReq->target;
pEvResp->property = None; /* "None" means refusal. */
pEvResp->time = pEvReq->time;
if (g_cVerbosity)
{
VBClLogVerbose(1, "Supported formats by VBoxClient:\n");
for (size_t i = 0; i < m_lstAtomFormats.size(); i++)
VBClLogVerbose(1, "\t%s\n", xAtomToString(m_lstAtomFormats.at(i)).c_str());
}
/* Is the requestor asking for the possible MIME types? */
if (pEvReq->target == xAtom(XA_TARGETS))
{
VBClLogInfo("Target window %#x ('%s') asking for target list\n", pEvReq->requestor, pszWndTgtName);
/* If so, set the window property with the formats on the requestor
* window. */
rc = wndXDnDSetFormatList(pEvReq->requestor, pEvReq->property, m_lstAtomFormats);
if (RT_SUCCESS(rc))
pEvResp->property = pEvReq->property;
}
/* Is the requestor asking for a specific MIME type (we support)? */
else if (m_lstAtomFormats.contains(pEvReq->target))
{
VBClLogInfo("Target window %#x ('%s') is asking for data as '%s'\n",
pEvReq->requestor, pszWndTgtName, xAtomToString(pEvReq->target).c_str());
#ifdef VBOX_WITH_DRAG_AND_DROP_PROMISES
# error "Implement me!"
#else
/* Did we not drop our stuff to the guest yet? Bail out. */
if (m_enmState != Dropped)
{
VBClLogError("Data not dropped by the host on the guest yet (client state %RU32, mode %RU32), refusing selection request by guest\n",
m_enmState, m_enmMode);
}
/* Did we not store the requestor's initial selection request yet? Then do so now. */
else
{
#endif /* VBOX_WITH_DRAG_AND_DROP_PROMISES */
/* Get the data format the requestor wants from us. */
VBClLogInfo("Target window %#x ('%s') requested data from host as '%s', rc=%Rrc\n",
pEvReq->requestor, pszWndTgtName, xAtomToString(pEvReq->target).c_str(), rc);
/* Make a copy of the MIME data to be passed back. The X server will be become
* the new owner of that data, so no deletion needed. */
/** @todo Do we need to do some more conversion here? XConvertSelection? */
AssertMsgBreakStmt(m_pvSelReqData != NULL, ("Selection request data is NULL\n"), rc = VERR_INVALID_PARAMETER);
AssertMsgBreakStmt(m_cbSelReqData > 0, ("Selection request data size is 0\n"), rc = VERR_INVALID_PARAMETER);
void const *pvData = RTMemDup(m_pvSelReqData, m_cbSelReqData);
AssertMsgBreakStmt(pvData != NULL, ("Duplicating selection request failed\n"), rc = VERR_NO_MEMORY);
uint32_t const cbData = m_cbSelReqData;
/* Always return the requested property. */
evResp.xselection.property = pEvReq->property;
/* Note: Always seems to return BadRequest. Seems fine. */
int xRc = XChangeProperty(pEvResp->display, pEvResp->requestor, pEvResp->property,
pEvResp->target, 8, PropModeReplace,
reinterpret_cast<const unsigned char*>(pvData), cbData);
LogFlowFunc(("Changing property '%s' (of type '%s') of window %#x ('%s'): %s\n",
xAtomToString(pEvReq->property).c_str(),
xAtomToString(pEvReq->target).c_str(),
pEvReq->requestor, pszWndTgtName,
gX11->xErrorToString(xRc).c_str()));
RT_NOREF(xRc);
#ifndef VBOX_WITH_DRAG_AND_DROP_PROMISES
}
#endif
}
/* Anything else. */
else
{
VBClLogError("Refusing unknown command/format '%s' of wnd=%#x ('%s')\n",
xAtomToString(pEvReq->target).c_str(), pEvReq->requestor, pszWndTgtName);
rc = VERR_NOT_SUPPORTED;
}
VBClLogVerbose(1, "Offering type '%s', property '%s' to window %#x ('%s') ...\n",
xAtomToString(pEvReq->target).c_str(),
xAtomToString(pEvReq->property).c_str(), pEvReq->requestor, pszWndTgtName);
int xRc = XSendEvent(pEvReq->display, pEvReq->requestor, True /* Propagate */, 0, &evResp);
if (xRc == 0)
VBClLogError("Error sending SelectionNotify(1) event to window %#x ('%s'): %s\n",
pEvReq->requestor, pszWndTgtName, gX11->xErrorToString(xRc).c_str());
XFlush(pEvReq->display);
break;
}
default:
rc = VERR_INVALID_STATE;
break;
}
RTStrFree(pszWndTgtName);
pszWndTgtName = NULL;
LogFlowThisFunc(("Returning rc=%Rrc\n", rc));
return rc;
}
/**
* Handles X11 events, called by x11EventThread.
*
* @returns IPRT status code.
* @param e X11 event to handle.
*/
int DragInstance::onX11Event(const XEvent &e)
{
int rc;
LogFlowThisFunc(("X11 event, type=%d\n", e.type));
switch (e.type)
{
/*
* This can happen if a guest->host drag operation
* goes back from the host to the guest. This is not what
* we want and thus resetting everything.
*/
case ButtonPress:
RT_FALL_THROUGH();
case ButtonRelease:
{
VBClLogInfo("Mouse button %s\n", e.type == ButtonPress ? "pressed" : "released");
reset();
rc = VINF_SUCCESS;
break;
}
case ClientMessage:
rc = onX11ClientMessage(e);
break;
case SelectionClear:
rc = onX11SelectionClear(e);
break;
case SelectionNotify:
rc = onX11SelectionNotify(e);
break;
case SelectionRequest:
rc = onX11SelectionRequest(e);
break;
case MotionNotify:
rc = onX11MotionNotify(e);
break;
default:
rc = VERR_NOT_IMPLEMENTED;
break;
}
LogFlowThisFunc(("rc=%Rrc\n", rc));
return rc;
}
int DragInstance::waitForStatusChange(uint32_t enmState, RTMSINTERVAL uTimeoutMS /* = 30000 */)
{
const uint64_t uiStart = RTTimeMilliTS();
volatile uint32_t enmCurState;
int rc = VERR_TIMEOUT;
LogFlowFunc(("enmState=%RU32, uTimeoutMS=%RU32\n", enmState, uTimeoutMS));
do
{
enmCurState = ASMAtomicReadU32(&m_enmState);
if (enmCurState == enmState)
{
rc = VINF_SUCCESS;
break;
}
}
while (RTTimeMilliTS() - uiStart < uTimeoutMS);
LogFlowThisFunc(("Returning %Rrc\n", rc));
return rc;
}
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
/**
* Waits for an X11 event of a specific type.
*
* @returns IPRT status code.
* @param evX Reference where to store the event into.
* @param iType Event type to wait for.
* @param uTimeoutMS Timeout (in ms) to wait for the event.
*/
bool DragInstance::waitForX11Msg(XEvent &evX, int iType, RTMSINTERVAL uTimeoutMS /* = 100 */)
{
LogFlowThisFunc(("iType=%d, uTimeoutMS=%RU32, cEventQueue=%zu\n", iType, uTimeoutMS, m_eventQueueList.size()));
bool fFound = false;
uint64_t const tsStartMs = RTTimeMilliTS();
do
{
/* Check if there is a client message in the queue. */
for (size_t i = 0; i < m_eventQueueList.size(); i++)
{
int rc2 = RTCritSectEnter(&m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
XEvent e = m_eventQueueList.at(i).m_Event;
fFound = e.type == iType;
if (fFound)
{
m_eventQueueList.removeAt(i);
evX = e;
}
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
if (fFound)
break;
}
}
if (fFound)
break;
int rc2 = RTSemEventWait(m_eventQueueEvent, 25 /* ms */);
if ( RT_FAILURE(rc2)
&& rc2 != VERR_TIMEOUT)
{
LogFlowFunc(("Waiting failed with rc=%Rrc\n", rc2));
break;
}
}
while (RTTimeMilliTS() - tsStartMs < uTimeoutMS);
LogFlowThisFunc(("Returning fFound=%RTbool, msRuntime=%RU64\n", fFound, RTTimeMilliTS() - tsStartMs));
return fFound;
}
/**
* Waits for an X11 client message of a specific type.
*
* @returns IPRT status code.
* @param evMsg Reference where to store the event into.
* @param aType Event type to wait for.
* @param uTimeoutMS Timeout (in ms) to wait for the event.
*/
bool DragInstance::waitForX11ClientMsg(XClientMessageEvent &evMsg, Atom aType,
RTMSINTERVAL uTimeoutMS /* = 100 */)
{
LogFlowThisFunc(("aType=%s, uTimeoutMS=%RU32, cEventQueue=%zu\n",
xAtomToString(aType).c_str(), uTimeoutMS, m_eventQueueList.size()));
bool fFound = false;
const uint64_t uiStart = RTTimeMilliTS();
do
{
/* Check if there is a client message in the queue. */
for (size_t i = 0; i < m_eventQueueList.size(); i++)
{
int rc2 = RTCritSectEnter(&m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
XEvent e = m_eventQueueList.at(i).m_Event;
if ( e.type == ClientMessage
&& e.xclient.message_type == aType)
{
m_eventQueueList.removeAt(i);
evMsg = e.xclient;
fFound = true;
}
if (e.type == ClientMessage)
{
LogFlowThisFunc(("Client message: Type=%ld (%s)\n",
e.xclient.message_type, xAtomToString(e.xclient.message_type).c_str()));
}
else
LogFlowThisFunc(("X message: Type=%d\n", e.type));
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
if (fFound)
break;
}
}
if (fFound)
break;
int rc2 = RTSemEventWait(m_eventQueueEvent, 25 /* ms */);
if ( RT_FAILURE(rc2)
&& rc2 != VERR_TIMEOUT)
{
LogFlowFunc(("Waiting failed with rc=%Rrc\n", rc2));
break;
}
}
while (RTTimeMilliTS() - uiStart < uTimeoutMS);
LogFlowThisFunc(("Returning fFound=%RTbool, msRuntime=%RU64\n", fFound, RTTimeMilliTS() - uiStart));
return fFound;
}
#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
/*
* Host -> Guest
*/
/**
* Host -> Guest: Event signalling that the host's (mouse) cursor just entered the VM's (guest's) display
* area.
*
* @returns IPRT status code.
* @param lstFormats List of supported formats from the host.
* @param dndListActionsAllowed (ORed) List of supported actions from the host.
*/
int DragInstance::hgEnter(const RTCList<RTCString> &lstFormats, uint32_t dndListActionsAllowed)
{
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
if (m_enmMode != Unknown)
return VERR_INVALID_STATE;
reset();
#ifdef DEBUG
LogFlowThisFunc(("dndListActionsAllowed=0x%x, lstFormats=%zu: ", dndListActionsAllowed, lstFormats.size()));
for (size_t i = 0; i < lstFormats.size(); ++i)
LogFlow(("'%s' ", lstFormats.at(i).c_str()));
LogFlow(("\n"));
#endif
int rc;
do
{
/* Check if the VM session has changed and reconnect to the HGCM service if necessary. */
rc = checkForSessionChange();
AssertRCBreak(rc);
/* Append all actual (MIME) formats we support to the list.
* These must come last, after the default Atoms above. */
rc = appendFormatsToList(lstFormats, m_lstAtomFormats);
AssertRCBreak(rc);
rc = wndXDnDSetFormatList(m_wndProxy.hWnd, xAtom(XA_XdndTypeList), m_lstAtomFormats);
AssertRCBreak(rc);
/* Announce the possible actions. */
VBoxDnDAtomList lstActions;
rc = toAtomActions(dndListActionsAllowed, lstActions);
AssertRCBreak(rc);
rc = wndXDnDSetActionList(m_wndProxy.hWnd, lstActions);
AssertRCBreak(rc);
/* Set the DnD selection owner to our window. */
/** @todo Don't use CurrentTime -- according to ICCCM section 2.1. */
XSetSelectionOwner(m_pDisplay, xAtom(XA_XdndSelection), m_wndProxy.hWnd, CurrentTime);
if (g_cVerbosity)
{
RTCString strMsg("Enter: Host -> Guest\n");
strMsg += RTCStringFmt("Allowed actions: ");
for (size_t i = 0; i < lstActions.size(); i++)
{
if (i > 0)
strMsg += ", ";
strMsg += DnDActionToStr(toHGCMAction(lstActions.at(i)));
}
strMsg += " - Formats: ";
for (size_t i = 0; i < lstFormats.size(); i++)
{
if (i > 0)
strMsg += ", ";
strMsg += lstFormats.at(i);
}
VBClShowNotify(VBOX_DND_SHOWNOTIFY_HEADER, strMsg.c_str());
}
m_enmMode = HG;
m_enmState = Dragging;
} while (0);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Host -> Guest: Event signalling that the host's (mouse) cursor has left the VM's (guest's)
* display area.
*/
int DragInstance::hgLeave(void)
{
if (g_cVerbosity)
VBClShowNotify(VBOX_DND_SHOWNOTIFY_HEADER, "Leave: Host -> Guest");
if (m_enmMode == HG) /* Only reset if in the right operation mode. */
reset();
return VINF_SUCCESS;
}
/**
* Host -> Guest: Event signalling that the host's (mouse) cursor has been moved within the VM's
* (guest's) display area.
*
* @returns IPRT status code.
* @param uPosX Relative X position within the guest's display area.
* @param uPosY Relative Y position within the guest's display area.
* @param dndActionDefault Default action the host wants to perform on the guest
* as soon as the operation successfully finishes.
*/
int DragInstance::hgMove(uint32_t uPosX, uint32_t uPosY, VBOXDNDACTION dndActionDefault)
{
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
LogFlowThisFunc(("uPosX=%RU32, uPosY=%RU32, dndActionDefault=0x%x\n", uPosX, uPosY, dndActionDefault));
if ( m_enmMode != HG
|| m_enmState != Dragging)
{
return VERR_INVALID_STATE;
}
int rc = VINF_SUCCESS;
int xRc = Success;
/* Move the mouse cursor within the guest. */
mouseCursorMove(uPosX, uPosY);
/* Search for the application window below the cursor. */
Window wndBelowCursor = gX11->applicationWindowBelowCursor(m_wndRoot);
char *pszWndBelowCursorName = wndX11GetNameA(wndBelowCursor);
AssertPtrReturn(pszWndBelowCursorName, VERR_NO_MEMORY);
uint8_t uBelowCursorXdndVer = 0; /* 0 means the current window is _not_ XdndAware. */
if (wndBelowCursor != None)
{
/* Temp stuff for the XGetWindowProperty call. */
Atom atmTmp;
int fmt;
unsigned long cItems, cbRemaining;
unsigned char *pcData = NULL;
/* Query the XdndAware property from the window. We are interested in
* the version and if it is XdndAware at all. */
xRc = XGetWindowProperty(m_pDisplay, wndBelowCursor, xAtom(XA_XdndAware),
0, 2, False, AnyPropertyType,
&atmTmp, &fmt, &cItems, &cbRemaining, &pcData);
if (xRc != Success)
{
VBClLogError("Error getting properties of cursor window=%#x: %s\n", wndBelowCursor, gX11->xErrorToString(xRc).c_str());
}
else
{
if (pcData == NULL || fmt != 32 || cItems != 1)
{
/** @todo Do we need to deal with this? */
VBClLogError("Wrong window properties for window %#x: pcData=%#x, iFmt=%d, cItems=%ul\n",
wndBelowCursor, pcData, fmt, cItems);
}
else
{
/* Get the current window's Xdnd version. */
uBelowCursorXdndVer = (uint8_t)reinterpret_cast<long *>(pcData)[0];
}
XFree(pcData);
}
}
char *pszWndCurName = wndX11GetNameA(m_wndCur);
AssertPtrReturn(pszWndCurName, VERR_NO_MEMORY);
LogFlowThisFunc(("wndCursor=%x ('%s', Xdnd version %u), wndCur=%x ('%s', Xdnd version %u)\n",
wndBelowCursor, pszWndBelowCursorName, uBelowCursorXdndVer, m_wndCur, pszWndCurName, m_uXdndVer));
if ( wndBelowCursor != m_wndCur
&& m_uXdndVer)
{
VBClLogInfo("Left old window %#x ('%s'), supported Xdnd version %u\n", m_wndCur, pszWndCurName, m_uXdndVer);
/* We left the current XdndAware window. Announce this to the current indow. */
XClientMessageEvent m;
RT_ZERO(m);
m.type = ClientMessage;
m.display = m_pDisplay;
m.window = m_wndCur;
m.message_type = xAtom(XA_XdndLeave);
m.format = 32;
m.data.l[XdndLeaveWindow] = m_wndProxy.hWnd;
xRc = XSendEvent(m_pDisplay, m_wndCur, False, NoEventMask, reinterpret_cast<XEvent*>(&m));
if (xRc == 0)
VBClLogError("Error sending leave event to old window %#x: %s\n", m_wndCur, gX11->xErrorToString(xRc).c_str());
/* Reset our current window. */
m_wndCur = 0;
m_uXdndVer = 0;
}
/*
* Do we have a new Xdnd-aware window which now is under the cursor?
*/
if ( wndBelowCursor != m_wndCur
&& uBelowCursorXdndVer)
{
VBClLogInfo("Entered new window %#x ('%s'), supports Xdnd version=%u\n",
wndBelowCursor, pszWndBelowCursorName, uBelowCursorXdndVer);
/*
* We enter a new window. Announce the XdndEnter event to the new
* window. The first three mime types are attached to the event (the
* others could be requested by the XdndTypeList property from the
* window itself).
*/
XClientMessageEvent m;
RT_ZERO(m);
m.type = ClientMessage;
m.display = m_pDisplay;
m.window = wndBelowCursor;
m.message_type = xAtom(XA_XdndEnter);
m.format = 32;
m.data.l[XdndEnterWindow] = m_wndProxy.hWnd;
m.data.l[XdndEnterFlags] = RT_MAKE_U32_FROM_U8(
/* Bit 0 is set if the source supports more than three data types. */
m_lstAtomFormats.size() > 3 ? RT_BIT(0) : 0,
/* Reserved for future use. */
0, 0,
/* Protocol version to use. */
RT_MIN(VBOX_XDND_VERSION, uBelowCursorXdndVer));
m.data.l[XdndEnterType1] = m_lstAtomFormats.value(0, None); /* First data type to use. */
m.data.l[XdndEnterType2] = m_lstAtomFormats.value(1, None); /* Second data type to use. */
m.data.l[XdndEnterType3] = m_lstAtomFormats.value(2, None); /* Third data type to use. */
xRc = XSendEvent(m_pDisplay, wndBelowCursor, False, NoEventMask, reinterpret_cast<XEvent*>(&m));
if (xRc == 0)
VBClLogError("Error sending enter event to window %#x: %s\n", wndBelowCursor, gX11->xErrorToString(xRc).c_str());
}
if (uBelowCursorXdndVer)
{
Assert(wndBelowCursor != None);
Atom atmAction = toAtomAction(dndActionDefault);
LogFlowThisFunc(("strAction=%s\n", xAtomToString(atmAction).c_str()));
VBClLogInfo("Sent position event (%RU32 x %RU32) to window %#x ('%s') with actions '%s'\n",
uPosX, uPosY, wndBelowCursor, pszWndBelowCursorName, xAtomToString(atmAction).c_str());
/*
* Send a XdndPosition event with the proposed action to the guest.
*/
XClientMessageEvent m;
RT_ZERO(m);
m.type = ClientMessage;
m.display = m_pDisplay;
m.window = wndBelowCursor;
m.message_type = xAtom(XA_XdndPosition);
m.format = 32;
m.data.l[XdndPositionWindow] = m_wndProxy.hWnd; /* X window ID of source window. */
m.data.l[XdndPositionFlags] = 0; /* Reserved, set to 0. */
m.data.l[XdndPositionXY] = RT_MAKE_U32(uPosY, uPosX); /* Cursor coordinates relative to the root window. */
m.data.l[XdndPositionTimeStamp] = CurrentTime; /* Timestamp for retrieving data. */
m.data.l[XdndPositionAction] = atmAction; /* Actions requested by the user. */
xRc = XSendEvent(m_pDisplay, wndBelowCursor, False, NoEventMask, reinterpret_cast<XEvent*>(&m));
if (xRc == 0)
VBClLogError("Error sending position event to current window %#x: %s\n", wndBelowCursor, gX11->xErrorToString(xRc).c_str());
}
if (uBelowCursorXdndVer == 0)
{
/* No window to process, so send a ignore ack event to the host. */
rc = VbglR3DnDHGSendAckOp(&m_dndCtx, VBOX_DND_ACTION_IGNORE);
}
else
{
Assert(wndBelowCursor != None);
m_wndCur = wndBelowCursor;
m_uXdndVer = uBelowCursorXdndVer;
}
RTStrFree(pszWndBelowCursorName);
RTStrFree(pszWndCurName);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Host -> Guest: Event signalling that the host has dropped the data over the VM (guest) window.
*
* @returns IPRT status code.
* @param uPosX Relative X position within the guest's display area.
* @param uPosY Relative Y position within the guest's display area.
* @param dndActionDefault Default action the host wants to perform on the guest
* as soon as the operation successfully finishes.
*/
int DragInstance::hgDrop(uint32_t uPosX, uint32_t uPosY, VBOXDNDACTION dndActionDefault)
{
RT_NOREF3(uPosX, uPosY, dndActionDefault);
LogFlowThisFunc(("wndCur=%RU32, wndProxy=%RU32, mode=%RU32, state=%RU32\n", m_wndCur, m_wndProxy.hWnd, m_enmMode, m_enmState));
LogFlowThisFunc(("uPosX=%RU32, uPosY=%RU32, dndActionDefault=0x%x\n", uPosX, uPosY, dndActionDefault));
if ( m_enmMode != HG
|| m_enmState != Dragging)
{
return VERR_INVALID_STATE;
}
/* Set the state accordingly. */
m_enmState = Dropped;
/*
* Ask the host to send the raw data, as we don't (yet) know which format
* the guest exactly expects. As blocking in a SelectionRequest message turned
* out to be very unreliable (e.g. with KDE apps) we request to start transferring
* file/directory data (if any) here.
*/
char szFormat[] = { "text/uri-list" };
int rc = VbglR3DnDHGSendReqData(&m_dndCtx, szFormat);
VBClLogInfo("Drop event from host resulted in: %Rrc\n", rc);
if (g_cVerbosity)
VBClShowNotify(VBOX_DND_SHOWNOTIFY_HEADER, "Drop: Host -> Guest");
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Host -> Guest: Event signalling that the host has finished sending drag'n drop
* data to the guest for further processing.
*
* @returns IPRT status code.
* @param pMeta Pointer to meta data from host.
*/
int DragInstance::hgDataReceive(PVBGLR3GUESTDNDMETADATA pMeta)
{
LogFlowThisFunc(("enmMode=%RU32, enmState=%RU32\n", m_enmMode, m_enmState));
LogFlowThisFunc(("enmMetaType=%RU32\n", pMeta->enmType));
if ( m_enmMode != HG
|| m_enmState != Dropped)
{
return VERR_INVALID_STATE;
}
void *pvData = NULL;
size_t cbData = 0;
int rc = VINF_SUCCESS; /* Shut up GCC. */
switch (pMeta->enmType)
{
case VBGLR3GUESTDNDMETADATATYPE_RAW:
{
AssertBreakStmt(pMeta->u.Raw.pvMeta != NULL, rc = VERR_INVALID_POINTER);
pvData = pMeta->u.Raw.pvMeta;
AssertBreakStmt(pMeta->u.Raw.cbMeta, rc = VERR_INVALID_PARAMETER);
cbData = pMeta->u.Raw.cbMeta;
rc = VINF_SUCCESS;
break;
}
case VBGLR3GUESTDNDMETADATATYPE_URI_LIST:
{
const char *pcszRootPath = DnDTransferListGetRootPathAbs(&pMeta->u.URI.Transfer);
AssertPtrBreakStmt(pcszRootPath, VERR_INVALID_POINTER);
VBClLogInfo("Transfer list root directory is '%s'\n", pcszRootPath);
/* Note: Use the URI format here, as X' DnD spec says so. */
rc = DnDTransferListGetRootsEx(&pMeta->u.URI.Transfer, DNDTRANSFERLISTFMT_URI, pcszRootPath,
DND_PATH_SEPARATOR_STR, (char **)&pvData, &cbData);
break;
}
default:
AssertFailedStmt(rc = VERR_NOT_IMPLEMENTED);
break;
}
if (RT_FAILURE(rc))
return rc;
/*
* At this point all data needed (including sent files/directories) should
* be on the guest, so proceed working on communicating with the target window.
*/
VBClLogInfo("Received %RU32 bytes of meta data from host\n", cbData);
/* Destroy any old data. */
if (m_pvSelReqData)
{
Assert(m_cbSelReqData);
RTMemFree(m_pvSelReqData); /** @todo RTMemRealloc? */
m_cbSelReqData = 0;
}
/** @todo Handle incremental transfers. */
/* Make a copy of the data. This data later then will be used to fill into
* the selection request. */
if (cbData)
{
m_pvSelReqData = RTMemAlloc(cbData);
if (!m_pvSelReqData)
return VERR_NO_MEMORY;
memcpy(m_pvSelReqData, pvData, cbData);
m_cbSelReqData = cbData;
}
/*
* Send a drop event to the current window (target).
* This window in turn then will raise a SelectionRequest message to our proxy window,
* which we will handle in our onX11SelectionRequest handler.
*
* The SelectionRequest will tell us in which format the target wants the data from the host.
*/
XClientMessageEvent m;
RT_ZERO(m);
m.type = ClientMessage;
m.display = m_pDisplay;
m.window = m_wndCur;
m.message_type = xAtom(XA_XdndDrop);
m.format = 32;
m.data.l[XdndDropWindow] = m_wndProxy.hWnd; /* Source window. */
m.data.l[XdndDropFlags] = 0; /* Reserved for future use. */
m.data.l[XdndDropTimeStamp] = CurrentTime; /* Our DnD data does not rely on any timing, so just use the current time. */
int xRc = XSendEvent(m_pDisplay, m_wndCur, False /* Propagate */, NoEventMask, reinterpret_cast<XEvent*>(&m));
if (xRc == 0)
VBClLogError("Error sending XA_XdndDrop event to window=%#x: %s\n", m_wndCur, gX11->xErrorToString(xRc).c_str());
XFlush(m_pDisplay);
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Checks if the VM session has changed (can happen when restoring the VM from a saved state)
* and do a reconnect to the DnD HGCM service.
*
* @returns IPRT status code.
*/
int DragInstance::checkForSessionChange(void)
{
uint64_t uSessionID;
int rc = VbglR3GetSessionId(&uSessionID);
if ( RT_SUCCESS(rc)
&& uSessionID != m_dndCtx.uSessionID)
{
LogFlowThisFunc(("VM session has changed to %RU64\n", uSessionID));
rc = VbglR3DnDDisconnect(&m_dndCtx);
AssertRC(rc);
rc = VbglR3DnDConnect(&m_dndCtx);
AssertRC(rc);
}
LogFlowFuncLeaveRC(rc);
return rc;
}
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
/**
* Guest -> Host: Event signalling that the host is asking whether there is a pending
* drag event on the guest (to the host).
*
* @returns IPRT status code.
*/
int DragInstance::ghIsDnDPending(void)
{
LogFlowThisFunc(("mode=%RU32, state=%RU32\n", m_enmMode, m_enmState));
int rc;
RTCString strFormats = DND_PATH_SEPARATOR_STR; /** @todo If empty, IOCTL fails with VERR_ACCESS_DENIED. */
VBOXDNDACTION dndActionDefault = VBOX_DND_ACTION_IGNORE;
VBOXDNDACTIONLIST dndActionList = VBOX_DND_ACTION_IGNORE;
/* Currently in wrong mode? Bail out. */
if (m_enmMode == HG)
{
rc = VERR_INVALID_STATE;
}
/* Message already processed successfully? */
else if ( m_enmMode == GH
&& ( m_enmState == Dragging
|| m_enmState == Dropped)
)
{
/* No need to query for the source window again. */
rc = VINF_SUCCESS;
}
else
{
/* Check if the VM session has changed and reconnect to the HGCM service if necessary. */
rc = checkForSessionChange();
/* Determine the current window which currently has the XdndSelection set. */
Window wndSel = XGetSelectionOwner(m_pDisplay, xAtom(XA_XdndSelection));
LogFlowThisFunc(("wndSel=%#x, wndProxy=%#x, wndCur=%#x\n", wndSel, m_wndProxy.hWnd, m_wndCur));
/* Is this another window which has a Xdnd selection and not our proxy window? */
if ( RT_SUCCESS(rc)
&& wndSel
&& wndSel != m_wndCur)
{
char *pszWndSelName = wndX11GetNameA(wndSel);
AssertPtrReturn(pszWndSelName, VERR_NO_MEMORY);
VBClLogInfo("New guest source window %#x ('%s')\n", wndSel, pszWndSelName);
/* Start over. */
reset();
/* Map the window on the current cursor position, which should provoke
* an XdndEnter event. */
rc = proxyWinShow();
if (RT_SUCCESS(rc))
{
rc = mouseCursorFakeMove();
if (RT_SUCCESS(rc))
{
bool fWaitFailed = false; /* Waiting for status changed failed? */
/* Wait until we're in "Dragging" state. */
rc = waitForStatusChange(Dragging, 100 /* 100ms timeout */);
/*
* Note: Don't wait too long here, as this mostly will make
* the drag and drop experience on the host being laggy
* and unresponsive.
*
* Instead, let the host query multiple times with 100ms
* timeout each (see above) and only report an error if
* the overall querying time has been exceeded.<
*/
if (RT_SUCCESS(rc))
{
m_enmMode = GH;
}
else if (rc == VERR_TIMEOUT)
{
/** @todo Make m_cFailedPendingAttempts configurable. For slower window managers? */
if (m_cFailedPendingAttempts++ > 50) /* Tolerate up to 5s total (100ms for each slot). */
fWaitFailed = true;
else
rc = VINF_SUCCESS;
}
else if (RT_FAILURE(rc))
fWaitFailed = true;
if (fWaitFailed)
{
VBClLogError("Error mapping proxy window to guest source window %#x ('%s'), rc=%Rrc\n",
wndSel, pszWndSelName, rc);
/* Reset the counter in any case. */
m_cFailedPendingAttempts = 0;
}
}
}
RTStrFree(pszWndSelName);
}
else
VBClLogInfo("No guest source window\n");
}
/*
* Acknowledge to the host in any case, regardless
* if something failed here or not. Be responsive.
*/
int rc2 = RTCritSectEnter(&m_dataCS);
if (RT_SUCCESS(rc2))
{
/* Filter out the default X11-specific formats (required for Xdnd, 'TARGET' / 'MULTIPLE');
* those will not be supported by VirtualBox. */
VBoxDnDAtomList const lstAtomFormatsFiltered = gX11->xAtomListFiltered(m_lstAtomFormats, m_lstAtomFormatsX11);
/* Anything left to report to the host? */
if (lstAtomFormatsFiltered.size())
{
strFormats = gX11->xAtomListToString(lstAtomFormatsFiltered);
dndActionDefault = VBOX_DND_ACTION_COPY; /** @todo Handle default action! */
dndActionList = VBOX_DND_ACTION_COPY; /** @todo Ditto. */
dndActionList |= toHGCMActions(m_lstAtomActions);
}
RTCritSectLeave(&m_dataCS);
}
if (g_cVerbosity)
{
char *pszActions = DnDActionListToStrA(dndActionList);
AssertPtrReturn(pszActions, VERR_NO_MEMORY);
VBClLogVerbose(1, "Reporting formats '%s' (actions '%s' / %#x, default action is '%s' (%#x)\n",
strFormats.c_str(), pszActions, dndActionList, DnDActionToStr(dndActionDefault), dndActionDefault);
RTStrFree(pszActions);
}
rc2 = VbglR3DnDGHSendAckPending(&m_dndCtx, dndActionDefault, dndActionList,
strFormats.c_str(), strFormats.length() + 1 /* Include termination */);
LogFlowThisFunc(("uClientID=%RU32, dndActionDefault=0x%x, dndActionList=0x%x, strFormats=%s, rc=%Rrc\n",
m_dndCtx.uClientID, dndActionDefault, dndActionList, strFormats.c_str(), rc2));
if (RT_FAILURE(rc2))
{
switch (rc2)
{
case VERR_ACCESS_DENIED:
{
rc = VBClShowNotify(VBOX_DND_SHOWNOTIFY_HEADER,
"Drag and drop to the host either is not supported or disabled. "
"Please enable Guest to Host or Bidirectional drag and drop mode "
"or re-install the VirtualBox Guest Additions.");
AssertRC(rc);
break;
}
default:
break;
}
VBClLogError("Error reporting pending drag and drop operation status to host: %Rrc\n", rc2);
if (RT_SUCCESS(rc))
rc = rc2;
}
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Guest -> Host: Event signalling that the host has dropped the item(s) on the
* host side.
*
* @returns IPRT status code.
* @param strFormat Requested format to send to the host.
* @param dndActionRequested Requested action to perform on the guest.
*/
int DragInstance::ghDropped(const RTCString &strFormat, VBOXDNDACTION dndActionRequested)
{
LogFlowThisFunc(("mode=%RU32, state=%RU32, strFormat=%s, dndActionRequested=0x%x\n",
m_enmMode, m_enmState, strFormat.c_str(), dndActionRequested));
/* Currently in wrong mode? Bail out. */
if ( m_enmMode == Unknown
|| m_enmMode == HG)
{
return VERR_INVALID_STATE;
}
if ( m_enmMode == GH
&& m_enmState != Dragging)
{
return VERR_INVALID_STATE;
}
int rc = VINF_SUCCESS;
m_enmState = Dropped;
#ifdef DEBUG
XWindowAttributes xwa;
XGetWindowAttributes(m_pDisplay, m_wndCur, &xwa);
LogFlowThisFunc(("wndProxy=%RU32, wndCur=%RU32, x=%d, y=%d, width=%d, height=%d\n",
m_wndProxy.hWnd, m_wndCur, xwa.x, xwa.y, xwa.width, xwa.height));
Window wndSelection = XGetSelectionOwner(m_pDisplay, xAtom(XA_XdndSelection));
LogFlowThisFunc(("wndSelection=%#x\n", wndSelection));
#endif
/* We send a fake mouse move event to the current window, cause
* this should have the grab. */
mouseCursorFakeMove();
/**
* The fake button release event above should lead to a XdndDrop event from the
* source window. Because of showing our proxy window, other Xdnd events can
* occur before, e.g. a XdndPosition event. We are not interested
* in those, so just try to get the right one.
*/
XClientMessageEvent evDnDDrop;
bool fDrop = waitForX11ClientMsg(evDnDDrop, xAtom(XA_XdndDrop), 5 * 1000 /* 5s timeout */);
if (fDrop)
{
LogFlowThisFunc(("XA_XdndDrop\n"));
/* Request to convert the selection in the specific format and
* place it to our proxy window as property. */
Assert(evDnDDrop.message_type == xAtom(XA_XdndDrop));
Window wndSource = evDnDDrop.data.l[XdndDropWindow]; /* Source window which has sent the message. */
Assert(wndSource == m_wndCur);
Atom aFormat = gX11->stringToxAtom(strFormat.c_str());
Time tsDrop;
if (m_uXdndVer >= 1)
tsDrop = evDnDDrop.data.l[XdndDropTimeStamp];
else
tsDrop = CurrentTime;
XConvertSelection(m_pDisplay, xAtom(XA_XdndSelection), aFormat, xAtom(XA_XdndSelection),
m_wndProxy.hWnd, tsDrop);
/* Wait for the selection notify event. */
XEvent evSelNotify;
RT_ZERO(evSelNotify);
if (waitForX11Msg(evSelNotify, SelectionNotify, 5 * 1000 /* 5s timeout */))
{
bool fCancel = false;
/* Make some paranoid checks. */
if ( evSelNotify.xselection.type == SelectionNotify
&& evSelNotify.xselection.display == m_pDisplay
&& evSelNotify.xselection.selection == xAtom(XA_XdndSelection)
&& evSelNotify.xselection.requestor == m_wndProxy.hWnd
&& evSelNotify.xselection.target == aFormat)
{
LogFlowThisFunc(("Selection notfiy (from wnd=%#x)\n", m_wndCur));
Atom aPropType;
int iPropFormat;
unsigned long cItems, cbRemaining;
unsigned char *pcData = NULL;
int xRc = XGetWindowProperty(m_pDisplay, m_wndProxy.hWnd,
xAtom(XA_XdndSelection) /* Property */,
0 /* Offset */,
VBOX_MAX_XPROPERTIES /* Length of 32-bit multiples */,
True /* Delete property? */,
AnyPropertyType, /* Property type */
&aPropType, &iPropFormat, &cItems, &cbRemaining, &pcData);
if (xRc != Success)
VBClLogError("Error getting XA_XdndSelection property of proxy window=%#x: %s\n",
m_wndProxy.hWnd, gX11->xErrorToString(xRc).c_str());
LogFlowThisFunc(("strType=%s, iPropFormat=%d, cItems=%RU32, cbRemaining=%RU32\n",
gX11->xAtomToString(aPropType).c_str(), iPropFormat, cItems, cbRemaining));
if ( aPropType != None
&& pcData != NULL
&& iPropFormat >= 8
&& cItems > 0
&& cbRemaining == 0)
{
size_t cbData = cItems * (iPropFormat / 8);
LogFlowThisFunc(("cbData=%zu\n", cbData));
/* For whatever reason some of the string MIME types are not
* zero terminated. Check that and correct it when necessary,
* because the guest side wants this in any case. */
if ( m_lstAllowedFormats.contains(strFormat)
&& pcData[cbData - 1] != '\0')
{
unsigned char *pvDataTmp = static_cast<unsigned char*>(RTMemAlloc(cbData + 1));
if (pvDataTmp)
{
memcpy(pvDataTmp, pcData, cbData);
pvDataTmp[cbData++] = '\0';
rc = VbglR3DnDGHSendData(&m_dndCtx, strFormat.c_str(), pvDataTmp, cbData);
RTMemFree(pvDataTmp);
}
else
rc = VERR_NO_MEMORY;
}
else
{
/* Send the raw data to the host. */
rc = VbglR3DnDGHSendData(&m_dndCtx, strFormat.c_str(), pcData, cbData);
LogFlowThisFunc(("Sent strFormat=%s, rc=%Rrc\n", strFormat.c_str(), rc));
}
if (RT_SUCCESS(rc))
{
rc = m_wndProxy.sendFinished(wndSource, dndActionRequested);
}
else
fCancel = true;
}
else
{
if (aPropType == xAtom(XA_INCR))
{
/** @todo Support incremental transfers. */
AssertMsgFailed(("Incremental transfers are not supported yet\n"));
VBClLogError("Incremental transfers are not supported yet\n");
rc = VERR_NOT_IMPLEMENTED;
}
else
{
VBClLogError("Not supported data type: %s\n", gX11->xAtomToString(aPropType).c_str());
rc = VERR_NOT_SUPPORTED;
}
fCancel = true;
}
if (fCancel)
{
VBClLogInfo("Cancelling dropping to host\n");
/* Cancel the operation -- inform the source window by
* sending a XdndFinished message so that the source can toss the required data. */
rc = m_wndProxy.sendFinished(wndSource, VBOX_DND_ACTION_IGNORE);
}
/* Cleanup. */
if (pcData)
XFree(pcData);
}
else
rc = VERR_INVALID_PARAMETER;
}
else
rc = VERR_TIMEOUT;
}
else
rc = VERR_TIMEOUT;
/* Inform the host on error. */
if (RT_FAILURE(rc))
{
int rc2 = VbglR3DnDSendError(&m_dndCtx, rc);
LogFlowThisFunc(("Sending error %Rrc to host resulted in %Rrc\n", rc, rc2)); RT_NOREF(rc2);
/* This is not fatal for us, just ignore. */
}
/* At this point, we have either successfully transfered any data or not.
* So reset our internal state because we are done here for the current (ongoing)
* drag and drop operation. */
reset();
LogFlowFuncLeaveRC(rc);
return rc;
}
#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
/*
* Helpers
*/
/**
* Fakes moving the mouse cursor to provoke various drag and drop
* events such as entering a target window or moving within a
* source window.
*
* Not the most elegant and probably correct function, but does
* the work for now.
*
* @returns IPRT status code.
*/
int DragInstance::mouseCursorFakeMove(void)
{
int iScreenID = XDefaultScreen(m_pDisplay);
/** @todo What about multiple screens? Test this! */
const int iScrX = XDisplayWidth(m_pDisplay, iScreenID);
const int iScrY = XDisplayHeight(m_pDisplay, iScreenID);
int fx, fy, rx, ry;
Window wndTemp, wndChild;
int wx, wy; unsigned int mask;
XQueryPointer(m_pDisplay, m_wndRoot, &wndTemp, &wndChild, &rx, &ry, &wx, &wy, &mask);
/*
* Apply some simple clipping and change the position slightly.
*/
/* FakeX */
if (rx == 0) fx = 1;
else if (rx == iScrX) fx = iScrX - 1;
else fx = rx + 1;
/* FakeY */
if (ry == 0) fy = 1;
else if (ry == iScrY) fy = iScrY - 1;
else fy = ry + 1;
/*
* Move the cursor to trigger the wanted events.
*/
LogFlowThisFunc(("cursorRootX=%d, cursorRootY=%d\n", fx, fy));
int rc = mouseCursorMove(fx, fy);
if (RT_SUCCESS(rc))
{
/* Move the cursor back to its original position. */
rc = mouseCursorMove(rx, ry);
}
return rc;
}
/**
* Moves the mouse pointer to a specific position.
*
* @returns IPRT status code.
* @param iPosX Absolute X coordinate.
* @param iPosY Absolute Y coordinate.
*/
int DragInstance::mouseCursorMove(int iPosX, int iPosY)
{
int const iScreenID = XDefaultScreen(m_pDisplay);
/** @todo What about multiple screens? Test this! */
int const iScreenWidth = XDisplayWidth (m_pDisplay, iScreenID);
int const iScreenHeight = XDisplayHeight(m_pDisplay, iScreenID);
iPosX = RT_CLAMP(iPosX, 0, iScreenWidth);
iPosY = RT_CLAMP(iPosY, 0, iScreenHeight);
/* Same mouse position as before? No need to do anything. */
if ( m_lastMouseX == iPosX
&& m_lastMouseY == iPosY)
{
return VINF_SUCCESS;
}
LogFlowThisFunc(("iPosX=%d, iPosY=%d, m_wndRoot=%#x\n", iPosX, iPosY, m_wndRoot));
/* Move the guest pointer to the DnD position, so we can find the window
* below that position. */
int xRc = XWarpPointer(m_pDisplay, None, m_wndRoot, 0, 0, 0, 0, iPosX, iPosY);
if (xRc == Success)
{
XFlush(m_pDisplay);
m_lastMouseX = iPosX;
m_lastMouseY = iPosY;
}
else
VBClLogError("Moving mouse cursor failed: %s", gX11->xErrorToString(xRc).c_str());
return VINF_SUCCESS;
}
/**
* Sends a mouse button event to a specific window.
*
* @param wndDest Window to send the mouse button event to.
* @param rx X coordinate relative to the root window's origin.
* @param ry Y coordinate relative to the root window's origin.
* @param iButton Mouse button to press/release.
* @param fPress Whether to press or release the mouse button.
*/
void DragInstance::mouseButtonSet(Window wndDest, int rx, int ry, int iButton, bool fPress)
{
LogFlowThisFunc(("wndDest=%#x, rx=%d, ry=%d, iBtn=%d, fPress=%RTbool\n",
wndDest, rx, ry, iButton, fPress));
#ifdef VBOX_DND_WITH_XTEST
/** @todo Make this check run only once. */
int ev, er, ma, mi;
if (XTestQueryExtension(m_pDisplay, &ev, &er, &ma, &mi))
{
LogFlowThisFunc(("XText extension available\n"));
int xRc = XTestFakeButtonEvent(m_pDisplay, 1, fPress ? True : False, CurrentTime);
if (Rc == 0)
VBClLogError("Error sending XTestFakeButtonEvent event: %s\n", gX11->xErrorToString(xRc).c_str());
XFlush(m_pDisplay);
}
else
{
#endif
LogFlowThisFunc(("Note: XText extension not available or disabled\n"));
unsigned int mask = 0;
if ( rx == -1
&& ry == -1)
{
Window wndRoot, wndChild;
int wx, wy;
XQueryPointer(m_pDisplay, m_wndRoot, &wndRoot, &wndChild, &rx, &ry, &wx, &wy, &mask);
LogFlowThisFunc(("Mouse pointer is at root x=%d, y=%d\n", rx, ry));
}
XButtonEvent eBtn;
RT_ZERO(eBtn);
eBtn.display = m_pDisplay;
eBtn.root = m_wndRoot;
eBtn.window = wndDest;
eBtn.subwindow = None;
eBtn.same_screen = True;
eBtn.time = CurrentTime;
eBtn.button = iButton;
eBtn.state = mask | (iButton == 1 ? Button1MotionMask :
iButton == 2 ? Button2MotionMask :
iButton == 3 ? Button3MotionMask :
iButton == 4 ? Button4MotionMask :
iButton == 5 ? Button5MotionMask : 0);
eBtn.type = fPress ? ButtonPress : ButtonRelease;
eBtn.send_event = False;
eBtn.x_root = rx;
eBtn.y_root = ry;
XTranslateCoordinates(m_pDisplay, eBtn.root, eBtn.window, eBtn.x_root, eBtn.y_root, &eBtn.x, &eBtn.y, &eBtn.subwindow);
LogFlowThisFunc(("state=0x%x, x=%d, y=%d\n", eBtn.state, eBtn.x, eBtn.y));
int xRc = XSendEvent(m_pDisplay, wndDest, True /* fPropagate */,
ButtonPressMask,
reinterpret_cast<XEvent*>(&eBtn));
if (xRc == 0)
VBClLogError("Error sending XButtonEvent event to window=%#x: %s\n", wndDest, gX11->xErrorToString(xRc).c_str());
XFlush(m_pDisplay);
#ifdef VBOX_DND_WITH_XTEST
}
#endif
}
/**
* Shows the (invisible) proxy window. The proxy window is needed for intercepting
* drags from the host to the guest or from the guest to the host. It acts as a proxy
* between the host and the actual (UI) element on the guest OS.
*
* To not make it miss any actions this window gets spawned across the entire guest
* screen (think of an umbrella) to (hopefully) capture everything. A proxy window
* which follows the cursor would be far too slow here.
*
* @returns IPRT status code.
* @param piRootX X coordinate relative to the root window's origin. Optional.
* @param piRootY Y coordinate relative to the root window's origin. Optional.
*/
int DragInstance::proxyWinShow(int *piRootX /* = NULL */, int *piRootY /* = NULL */) const
{
/* piRootX is optional. */
/* piRootY is optional. */
LogFlowThisFuncEnter();
int rc = VINF_SUCCESS;
#if 0
# ifdef VBOX_DND_WITH_XTEST
XTestGrabControl(m_pDisplay, False);
# endif
#endif
/* Get the mouse pointer position and determine if we're on the same screen as the root window
* and return the current child window beneath our mouse pointer, if any. */
int iRootX, iRootY;
int iChildX, iChildY;
unsigned int iMask;
Window wndRoot, wndChild;
Bool fInRootWnd = XQueryPointer(m_pDisplay, m_wndRoot, &wndRoot, &wndChild,
&iRootX, &iRootY, &iChildX, &iChildY, &iMask);
LogFlowThisFunc(("fInRootWnd=%RTbool, wndRoot=%RU32, wndChild=%RU32, iRootX=%d, iRootY=%d\n",
RT_BOOL(fInRootWnd), wndRoot, wndChild, iRootX, iRootY)); RT_NOREF(fInRootWnd);
if (piRootX)
*piRootX = iRootX;
if (piRootY)
*piRootY = iRootY;
XSynchronize(m_pDisplay, True /* Enable sync */);
/* Bring our proxy window into foreground. */
XMapWindow(m_pDisplay, m_wndProxy.hWnd);
XRaiseWindow(m_pDisplay, m_wndProxy.hWnd);
/* Spawn our proxy window over the entire screen, making it an easy drop target for the host's cursor. */
LogFlowThisFunc(("Proxy window x=%d, y=%d, width=%d, height=%d\n",
m_wndProxy.iX, m_wndProxy.iY, m_wndProxy.iWidth, m_wndProxy.iHeight));
XMoveResizeWindow(m_pDisplay, m_wndProxy.hWnd, m_wndProxy.iX, m_wndProxy.iY, m_wndProxy.iWidth, m_wndProxy.iHeight);
XFlush(m_pDisplay);
XSynchronize(m_pDisplay, False /* Disable sync */);
#if 0
# ifdef VBOX_DND_WITH_XTEST
XTestGrabControl(m_pDisplay, True);
# endif
#endif
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Hides the (invisible) proxy window.
*/
int DragInstance::proxyWinHide(void)
{
LogFlowFuncEnter();
XUnmapWindow(m_pDisplay, m_wndProxy.hWnd);
XFlush(m_pDisplay);
return VINF_SUCCESS; /** @todo Add error checking. */
}
/**
* Allocates the name (title) of an X window.
* The returned pointer must be freed using RTStrFree().
*
* @returns Pointer to the allocated window name.
* @retval NULL on allocation failure.
* @retval "<No name>" if window name was not found / invalid window handle.
* @param wndThis Window to retrieve name for.
*/
char *DragInstance::wndX11GetNameA(Window wndThis) const
{
char *pszName = NULL;
XTextProperty propName;
if ( wndThis != None
&& XGetWMName(m_pDisplay, wndThis, &propName))
{
if (propName.value)
pszName = RTStrDup((char *)propName.value); /** @todo UTF8? */
XFree(propName.value);
}
if (!pszName) /* No window name found? */
pszName = RTStrDup("<No name>");
return pszName;
}
/**
* Clear a window's supported/accepted actions list.
*
* @param wndThis Window to clear the list for.
*/
void DragInstance::wndXDnDClearActionList(Window wndThis) const
{
XDeleteProperty(m_pDisplay, wndThis, xAtom(XA_XdndActionList));
}
/**
* Clear a window's supported/accepted formats list.
*
* @param wndThis Window to clear the list for.
*/
void DragInstance::wndXDnDClearFormatList(Window wndThis) const
{
XDeleteProperty(m_pDisplay, wndThis, xAtom(XA_XdndTypeList));
}
/**
* Retrieves a window's supported/accepted XDnD actions.
*
* @returns IPRT status code.
* @param wndThis Window to retrieve the XDnD actions for.
* @param lstActions Reference to VBoxDnDAtomList to store the action into.
*/
int DragInstance::wndXDnDGetActionList(Window wndThis, VBoxDnDAtomList &lstActions) const
{
Atom iActType = None;
int iActFmt;
unsigned long cItems, cbData;
unsigned char *pcbData = NULL;
/* Fetch the possible list of actions, if this property is set. */
int xRc = XGetWindowProperty(m_pDisplay, wndThis,
xAtom(XA_XdndActionList),
0, VBOX_MAX_XPROPERTIES,
False, XA_ATOM, &iActType, &iActFmt, &cItems, &cbData, &pcbData);
if (xRc != Success)
{
LogFlowThisFunc(("Error getting XA_XdndActionList atoms from window=%#x: %s\n",
wndThis, gX11->xErrorToString(xRc).c_str()));
return VERR_NOT_FOUND;
}
LogFlowThisFunc(("wndThis=%#x, cItems=%RU32, pcbData=%p\n", wndThis, cItems, pcbData));
if (cItems > 0)
{
AssertPtr(pcbData);
Atom *paData = reinterpret_cast<Atom *>(pcbData);
for (unsigned i = 0; i < RT_MIN(VBOX_MAX_XPROPERTIES, cItems); i++)
{
LogFlowThisFunc(("\t%s\n", gX11->xAtomToString(paData[i]).c_str()));
lstActions.append(paData[i]);
}
XFree(pcbData);
}
return VINF_SUCCESS;
}
/**
* Retrieves a window's supported/accepted XDnD formats.
*
* @returns IPRT status code.
* @param wndThis Window to retrieve the XDnD formats for.
* @param lstTypes Reference to VBoxDnDAtomList to store the formats into.
*/
int DragInstance::wndXDnDGetFormatList(Window wndThis, VBoxDnDAtomList &lstTypes) const
{
Atom iActType = None;
int iActFmt;
unsigned long cItems, cbData;
unsigned char *pcbData = NULL;
int xRc = XGetWindowProperty(m_pDisplay, wndThis,
xAtom(XA_XdndTypeList),
0, VBOX_MAX_XPROPERTIES,
False, XA_ATOM, &iActType, &iActFmt, &cItems, &cbData, &pcbData);
if (xRc != Success)
{
LogFlowThisFunc(("Error getting XA_XdndTypeList atoms from window=%#x: %s\n",
wndThis, gX11->xErrorToString(xRc).c_str()));
return VERR_NOT_FOUND;
}
LogFlowThisFunc(("wndThis=%#x, cItems=%RU32, pcbData=%p\n", wndThis, cItems, pcbData));
if (cItems > 0)
{
AssertPtr(pcbData);
Atom *paData = reinterpret_cast<Atom *>(pcbData);
for (unsigned i = 0; i < RT_MIN(VBOX_MAX_XPROPERTIES, cItems); i++)
{
LogFlowThisFunc(("\t%s\n", gX11->xAtomToString(paData[i]).c_str()));
lstTypes.append(paData[i]);
}
XFree(pcbData);
}
return VINF_SUCCESS;
}
/**
* Sets (replaces) a window's XDnD accepted/allowed actions.
*
* @returns IPRT status code.
* @param wndThis Window to set the format list for.
* @param lstActions Reference to list of XDnD actions to set.
*/
int DragInstance::wndXDnDSetActionList(Window wndThis, const VBoxDnDAtomList &lstActions) const
{
if (lstActions.isEmpty())
return VINF_SUCCESS;
XChangeProperty(m_pDisplay, wndThis,
xAtom(XA_XdndActionList),
XA_ATOM, 32, PropModeReplace,
reinterpret_cast<const unsigned char*>(lstActions.raw()),
lstActions.size());
return VINF_SUCCESS;
}
/**
* Sets (replaces) a window's XDnD accepted format list.
*
* @returns IPRT status code.
* @param wndThis Window to set the format list for.
* @param atmProp Property to set.
* @param lstFormats Reference to list of XDnD formats to set.
*/
int DragInstance::wndXDnDSetFormatList(Window wndThis, Atom atmProp, const VBoxDnDAtomList &lstFormats) const
{
if (lstFormats.isEmpty())
return VERR_INVALID_PARAMETER;
/* Add the property with the property data to the window. */
XChangeProperty(m_pDisplay, wndThis, atmProp,
XA_ATOM, 32, PropModeReplace,
reinterpret_cast<const unsigned char*>(lstFormats.raw()),
lstFormats.size());
return VINF_SUCCESS;
}
/**
* Appends a RTCString list to VBoxDnDAtomList list.
*
* @returns IPRT status code.
* @param lstFormats Reference to RTCString list to convert.
* @param lstAtoms Reference to VBoxDnDAtomList list to store results in.
*/
int DragInstance::appendFormatsToList(const RTCList<RTCString> &lstFormats, VBoxDnDAtomList &lstAtoms) const
{
for (size_t i = 0; i < lstFormats.size(); ++i)
lstAtoms.append(XInternAtom(m_pDisplay, lstFormats.at(i).c_str(), False));
return VINF_SUCCESS;
}
/**
* Appends a raw-data string list to VBoxDnDAtomList list.
*
* @returns IPRT status code.
* @param pvData Pointer to string data to convert.
* @param cbData Size (in bytes) to convert.
* @param lstAtoms Reference to VBoxDnDAtomList list to store results in.
*/
int DragInstance::appendDataToList(const void *pvData, uint32_t cbData, VBoxDnDAtomList &lstAtoms) const
{
RT_NOREF1(lstAtoms);
AssertPtrReturn(pvData, VERR_INVALID_POINTER);
AssertReturn(cbData, VERR_INVALID_PARAMETER);
const char *pszStr = (char *)pvData;
uint32_t cbStr = cbData;
int rc = VINF_SUCCESS;
VBoxDnDAtomList lstAtom;
while (cbStr)
{
size_t cbSize = RTStrNLen(pszStr, cbStr);
/* Create a copy with max N chars, so that we are on the save side,
* even if the data isn't zero terminated. */
char *pszTmp = RTStrDupN(pszStr, cbSize);
if (!pszTmp)
{
rc = VERR_NO_MEMORY;
break;
}
lstAtom.append(XInternAtom(m_pDisplay, pszTmp, False));
RTStrFree(pszTmp);
pszStr += cbSize + 1;
cbStr -= cbSize + 1;
}
return rc;
}
/**
* Converts a HGCM-based drag'n drop action to a Atom-based drag'n drop action.
*
* @returns Converted Atom-based drag'n drop action.
* @param dndAction HGCM drag'n drop actions to convert.
*/
/* static */
Atom DragInstance::toAtomAction(VBOXDNDACTION dndAction)
{
/* Ignore is None. */
return (isDnDCopyAction(dndAction) ? xAtom(XA_XdndActionCopy) :
isDnDMoveAction(dndAction) ? xAtom(XA_XdndActionMove) :
isDnDLinkAction(dndAction) ? xAtom(XA_XdndActionLink) :
None);
}
/**
* Converts HGCM-based drag'n drop actions to a VBoxDnDAtomList list.
*
* @returns IPRT status code.
* @param dndActionList HGCM drag'n drop actions to convert.
* @param lstAtoms Reference to VBoxDnDAtomList to store actions in.
*/
/* static */
int DragInstance::toAtomActions(VBOXDNDACTIONLIST dndActionList, VBoxDnDAtomList &lstAtoms)
{
if (hasDnDCopyAction(dndActionList))
lstAtoms.append(xAtom(XA_XdndActionCopy));
if (hasDnDMoveAction(dndActionList))
lstAtoms.append(xAtom(XA_XdndActionMove));
if (hasDnDLinkAction(dndActionList))
lstAtoms.append(xAtom(XA_XdndActionLink));
return VINF_SUCCESS;
}
/**
* Converts an Atom-based drag'n drop action to a HGCM drag'n drop action.
*
* @returns HGCM drag'n drop action.
* @param atom Atom-based drag'n drop action to convert.
*/
/* static */
uint32_t DragInstance::toHGCMAction(Atom atom)
{
uint32_t uAction = VBOX_DND_ACTION_IGNORE;
if (atom == xAtom(XA_XdndActionCopy))
uAction = VBOX_DND_ACTION_COPY;
else if (atom == xAtom(XA_XdndActionMove))
uAction = VBOX_DND_ACTION_MOVE;
else if (atom == xAtom(XA_XdndActionLink))
uAction = VBOX_DND_ACTION_LINK;
return uAction;
}
/**
* Converts an VBoxDnDAtomList list to an HGCM action list.
*
* @returns ORed HGCM action list.
* @param lstActions List of Atom-based actions to convert.
*/
/* static */
uint32_t DragInstance::toHGCMActions(const VBoxDnDAtomList &lstActions)
{
uint32_t uActions = VBOX_DND_ACTION_IGNORE;
for (size_t i = 0; i < lstActions.size(); i++)
uActions |= toHGCMAction(lstActions.at(i));
return uActions;
}
/*********************************************************************************************************************************
* VBoxDnDProxyWnd implementation. *
********************************************************************************************************************************/
VBoxDnDProxyWnd::VBoxDnDProxyWnd(void)
: pDisp(NULL)
, hWnd(0)
, iX(0)
, iY(0)
, iWidth(0)
, iHeight(0)
{
}
VBoxDnDProxyWnd::~VBoxDnDProxyWnd(void)
{
destroy();
}
int VBoxDnDProxyWnd::init(Display *pDisplay)
{
/** @todo What about multiple screens? Test this! */
int iScreenID = XDefaultScreen(pDisplay);
iWidth = XDisplayWidth(pDisplay, iScreenID);
iHeight = XDisplayHeight(pDisplay, iScreenID);
pDisp = pDisplay;
return VINF_SUCCESS;
}
void VBoxDnDProxyWnd::destroy(void)
{
}
int VBoxDnDProxyWnd::sendFinished(Window hWndSource, VBOXDNDACTION dndAction)
{
/* Was the drop accepted by the host? That is, anything than ignoring. */
bool fDropAccepted = dndAction > VBOX_DND_ACTION_IGNORE;
LogFlowFunc(("dndAction=0x%x\n", dndAction));
/* Confirm the result of the transfer to the target window. */
XClientMessageEvent m;
RT_ZERO(m);
m.type = ClientMessage;
m.display = pDisp;
m.window = hWnd;
m.message_type = xAtom(XA_XdndFinished);
m.format = 32;
m.data.l[XdndFinishedWindow] = hWnd; /* Target window. */
m.data.l[XdndFinishedFlags] = fDropAccepted ? RT_BIT(0) : 0; /* Was the drop accepted? */
m.data.l[XdndFinishedAction] = fDropAccepted ? DragInstance::toAtomAction(dndAction) : None; /* Action used on accept. */
int xRc = XSendEvent(pDisp, hWndSource, True, NoEventMask, reinterpret_cast<XEvent*>(&m));
if (xRc == 0)
{
VBClLogError("Error sending finished event to source window=%#x: %s\n",
hWndSource, gX11->xErrorToString(xRc).c_str());
return VERR_GENERAL_FAILURE; /** @todo Fudge. */
}
return VINF_SUCCESS;
}
/*********************************************************************************************************************************
* DragAndDropService implementation. *
********************************************************************************************************************************/
/** @copydoc VBCLSERVICE::pfnInit */
int DragAndDropService::init(void)
{
LogFlowFuncEnter();
/* Connect to the x11 server. */
m_pDisplay = XOpenDisplay(NULL);
if (!m_pDisplay)
{
VBClLogFatalError("Unable to connect to X server -- running in a terminal session?\n");
return VERR_NOT_FOUND;
}
xHelpers *pHelpers = xHelpers::getInstance(m_pDisplay);
if (!pHelpers)
return VERR_NO_MEMORY;
int rc;
do
{
rc = RTSemEventCreate(&m_hEventSem);
AssertRCBreak(rc);
rc = RTCritSectInit(&m_eventQueueCS);
AssertRCBreak(rc);
rc = VbglR3DnDConnect(&m_dndCtx);
AssertRCBreak(rc);
/* Event thread for events coming from the HGCM device. */
rc = RTThreadCreate(&m_hHGCMThread, hgcmEventThread, this,
0, RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE, "dndHGCM");
AssertRCBreak(rc);
rc = RTThreadUserWait(m_hHGCMThread, RT_MS_30SEC);
AssertRCBreak(rc);
if (ASMAtomicReadBool(&m_fStop))
break;
/* Event thread for events coming from the x11 system. */
rc = RTThreadCreate(&m_hX11Thread, x11EventThread, this,
0, RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE, "dndX11");
AssertRCBreak(rc);
rc = RTThreadUserWait(m_hX11Thread, RT_MS_30SEC);
AssertRCBreak(rc);
if (ASMAtomicReadBool(&m_fStop))
break;
} while (0);
if (m_fStop)
rc = VERR_GENERAL_FAILURE; /** @todo Fudge! */
if (RT_FAILURE(rc))
VBClLogError("Failed to initialize, rc=%Rrc\n", rc);
LogFlowFuncLeaveRC(rc);
return rc;
}
/** @copydoc VBCLSERVICE::pfnWorker */
int DragAndDropService::worker(bool volatile *pfShutdown)
{
int rc;
do
{
m_pCurDnD = new DragInstance(m_pDisplay, this);
if (!m_pCurDnD)
{
rc = VERR_NO_MEMORY;
break;
}
/* Note: For multiple screen support in VBox it is not necessary to use
* another screen number than zero. Maybe in the future it will become
* necessary if VBox supports multiple X11 screens. */
rc = m_pCurDnD->init(0 /* uScreenID */);
/* Note: Can return VINF_PERMISSION_DENIED if HGCM host service is not available. */
if (rc != VINF_SUCCESS)
{
if (RT_FAILURE(rc))
VBClLogError("Unable to connect to drag and drop service, rc=%Rrc\n", rc);
else if (rc == VINF_PERMISSION_DENIED) /* No error, DnD might be just disabled. */
VBClLogInfo("Not available on host, terminating\n");
break;
}
/* Let the main thread know that it can continue spawning services. */
RTThreadUserSignal(RTThreadSelf());
/* Enter the main event processing loop. */
do
{
DNDEVENT e;
RT_ZERO(e);
LogFlowFunc(("Waiting for new events ...\n"));
rc = RTSemEventWait(m_hEventSem, RT_INDEFINITE_WAIT);
if (RT_FAILURE(rc))
break;
size_t cEvents = 0;
int rc2 = RTCritSectEnter(&m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
cEvents = m_eventQueue.size();
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
}
while (cEvents)
{
rc2 = RTCritSectEnter(&m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
if (m_eventQueue.isEmpty())
{
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
break;
}
e = m_eventQueue.first();
m_eventQueue.removeFirst();
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
}
if (e.enmType == DNDEVENT::DnDEventType_HGCM)
{
PVBGLR3DNDEVENT pVbglR3Event = e.hgcm;
AssertPtrBreak(pVbglR3Event);
LogFlowThisFunc(("HGCM event enmType=%RU32\n", pVbglR3Event->enmType));
switch (pVbglR3Event->enmType)
{
case VBGLR3DNDEVENTTYPE_HG_ENTER:
{
if (pVbglR3Event->u.HG_Enter.cbFormats)
{
RTCList<RTCString> lstFormats =
RTCString(pVbglR3Event->u.HG_Enter.pszFormats, pVbglR3Event->u.HG_Enter.cbFormats - 1).split(DND_PATH_SEPARATOR_STR);
rc = m_pCurDnD->hgEnter(lstFormats, pVbglR3Event->u.HG_Enter.dndLstActionsAllowed);
if (RT_FAILURE(rc))
break;
/* Enter is always followed by a move event. */
}
else
{
AssertMsgFailed(("cbFormats is 0\n"));
rc = VERR_INVALID_PARAMETER;
break;
}
/* Note: After HOST_DND_FN_HG_EVT_ENTER there immediately is a move
* event, so fall through is intentional here. */
RT_FALL_THROUGH();
}
case VBGLR3DNDEVENTTYPE_HG_MOVE:
{
rc = m_pCurDnD->hgMove(pVbglR3Event->u.HG_Move.uXpos, pVbglR3Event->u.HG_Move.uYpos,
pVbglR3Event->u.HG_Move.dndActionDefault);
break;
}
case VBGLR3DNDEVENTTYPE_HG_LEAVE:
{
rc = m_pCurDnD->hgLeave();
break;
}
case VBGLR3DNDEVENTTYPE_HG_DROP:
{
rc = m_pCurDnD->hgDrop(pVbglR3Event->u.HG_Drop.uXpos, pVbglR3Event->u.HG_Drop.uYpos,
pVbglR3Event->u.HG_Drop.dndActionDefault);
break;
}
/* Note: VbglR3DnDRecvNextMsg() will return HOST_DND_FN_HG_SND_DATA_HDR when
* the host has finished copying over all the data to the guest.
*
* The actual data transfer (and message processing for it) will be done
* internally by VbglR3DnDRecvNextMsg() to not duplicate any code for different
* platforms.
*
* The data header now will contain all the (meta) data the guest needs in
* order to complete the DnD operation. */
case VBGLR3DNDEVENTTYPE_HG_RECEIVE:
{
rc = m_pCurDnD->hgDataReceive(&pVbglR3Event->u.HG_Received.Meta);
break;
}
case VBGLR3DNDEVENTTYPE_CANCEL:
{
m_pCurDnD->reset();
break;
}
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
case VBGLR3DNDEVENTTYPE_GH_ERROR:
{
m_pCurDnD->reset();
break;
}
case VBGLR3DNDEVENTTYPE_GH_REQ_PENDING:
{
rc = m_pCurDnD->ghIsDnDPending();
break;
}
case VBGLR3DNDEVENTTYPE_GH_DROP:
{
rc = m_pCurDnD->ghDropped(pVbglR3Event->u.GH_Drop.pszFormat, pVbglR3Event->u.GH_Drop.dndActionRequested);
break;
}
#endif
case VBGLR3DNDEVENTTYPE_QUIT:
{
rc = VINF_SUCCESS;
break;
}
default:
{
VBClLogError("Received unsupported message type %RU32\n", pVbglR3Event->enmType);
rc = VERR_NOT_SUPPORTED;
break;
}
}
LogFlowFunc(("Message %RU32 processed with %Rrc\n", pVbglR3Event->enmType, rc));
if (RT_FAILURE(rc))
{
/* Tell the user. */
VBClLogError("Processing message %RU32 failed with %Rrc\n", pVbglR3Event->enmType, rc);
/* If anything went wrong, do a reset and start over. */
reset();
}
const bool fQuit = pVbglR3Event->enmType == VBGLR3DNDEVENTTYPE_QUIT;
VbglR3DnDEventFree(e.hgcm);
e.hgcm = NULL;
if (fQuit)
break;
}
else if (e.enmType == DNDEVENT::DnDEventType_X11)
{
LogFlowThisFunc(("X11 event (type %#x)\n", e.x11.type));
m_pCurDnD->onX11Event(e.x11);
}
else
AssertMsgFailed(("Unknown event queue type %RU32\n", e.enmType));
--cEvents;
} /* for */
/*
* Make sure that any X11 requests have actually been sent to the
* server, since we are waiting for responses using poll() on
* another thread which will not automatically trigger flushing.
*/
XFlush(m_pDisplay);
if (m_fStop)
break;
} while (!ASMAtomicReadBool(pfShutdown));
} while (0);
if (m_pCurDnD)
{
delete m_pCurDnD;
m_pCurDnD = NULL;
}
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Resets the DnD service' data.
*/
void DragAndDropService::reset(void)
{
LogFlowFuncEnter();
if (m_pCurDnD)
m_pCurDnD->reset();
/*
* Clear the event queue.
*/
int rc2 = RTCritSectEnter(&m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
for (size_t i = 0; i < m_eventQueue.size(); i++)
{
switch (m_eventQueue[i].enmType)
{
case DNDEVENT::DnDEventType_HGCM:
{
VbglR3DnDEventFree(m_eventQueue[i].hgcm);
break;
}
default:
break;
}
}
m_eventQueue.clear();
rc2 = RTCritSectLeave(&m_eventQueueCS);
AssertRC(rc2);
}
LogFlowFuncLeave();
}
/** @copydoc VBCLSERVICE::pfnStop */
void DragAndDropService::stop(void)
{
LogFlowFuncEnter();
/* Set stop flag first. */
ASMAtomicXchgBool(&m_fStop, true);
/* First, disconnect any instances. */
if (m_pCurDnD)
m_pCurDnD->stop();
/* Second, disconnect the service's DnD connection. */
VbglR3DnDDisconnect(&m_dndCtx);
LogFlowFuncLeave();
}
/** @copydoc VBCLSERVICE::pfnTerm */
int DragAndDropService::term(void)
{
int rc = VINF_SUCCESS;
/*
* Wait for threads to terminate.
*/
int rcThread;
if (m_hX11Thread != NIL_RTTHREAD)
{
VBClLogVerbose(2, "Terminating X11 thread ...\n");
int rc2 = RTThreadWait(m_hX11Thread, RT_MS_30SEC, &rcThread);
if (RT_SUCCESS(rc2))
rc2 = rcThread;
if (RT_FAILURE(rc2))
VBClLogError("Error waiting for X11 thread to terminate: %Rrc\n", rc2);
if (RT_SUCCESS(rc))
rc = rc2;
m_hX11Thread = NIL_RTTHREAD;
VBClLogVerbose(2, "X11 thread terminated\n");
}
if (m_hHGCMThread != NIL_RTTHREAD)
{
VBClLogVerbose(2, "Terminating HGCM thread ...\n");
int rc2 = RTThreadWait(m_hHGCMThread, RT_MS_30SEC, &rcThread);
if (RT_SUCCESS(rc2))
rc2 = rcThread;
if (RT_FAILURE(rc2))
VBClLogError("Error waiting for HGCM thread to terminate: %Rrc\n", rc2);
if (RT_SUCCESS(rc))
rc = rc2;
m_hHGCMThread = NIL_RTTHREAD;
VBClLogVerbose(2, "HGCM thread terminated\n");
}
reset();
if (m_pCurDnD)
{
delete m_pCurDnD;
m_pCurDnD = NULL;
}
xHelpers::destroyInstance();
return rc;
}
/**
* Static callback function for HGCM message processing thread. An internal
* message queue will be filled which then will be processed by the according
* drag'n drop instance.
*
* @returns IPRT status code.
* @param hThread Thread handle to use.
* @param pvUser Pointer to DragAndDropService instance to use.
*/
/* static */
DECLCALLBACK(int) DragAndDropService::hgcmEventThread(RTTHREAD hThread, void *pvUser)
{
AssertPtrReturn(pvUser, VERR_INVALID_PARAMETER);
DragAndDropService *pThis = static_cast<DragAndDropService*>(pvUser);
/* Let the service instance know in any case. */
int rc = RTThreadUserSignal(hThread);
AssertRCReturn(rc, rc);
VBClLogVerbose(2, "HGCM thread started\n");
/* Number of invalid messages skipped in a row. */
int cMsgSkippedInvalid = 0;
DNDEVENT e;
do
{
RT_ZERO(e);
e.enmType = DNDEVENT::DnDEventType_HGCM;
/* Wait for new events. */
rc = VbglR3DnDEventGetNext(&pThis->m_dndCtx, &e.hgcm);
if (RT_SUCCESS(rc))
{
cMsgSkippedInvalid = 0; /* Reset skipped messages count. */
int rc2 = RTCritSectEnter(&pThis->m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
VBClLogVerbose(2, "Received new HGCM message (type %#x)\n", e.hgcm->enmType);
pThis->m_eventQueue.append(e);
rc2 = RTCritSectLeave(&pThis->m_eventQueueCS);
AssertRC(rc2);
}
rc = RTSemEventSignal(pThis->m_hEventSem);
if (RT_FAILURE(rc))
break;
}
else
{
VBClLogError("Processing next message failed with rc=%Rrc\n", rc);
/* Old(er) hosts either are broken regarding DnD support or otherwise
* don't support the stuff we do on the guest side, so make sure we
* don't process invalid messages forever. */
if (cMsgSkippedInvalid++ > 32)
{
VBClLogError("Too many invalid/skipped messages from host, exiting ...\n");
break;
}
}
} while (!ASMAtomicReadBool(&pThis->m_fStop));
VBClLogVerbose(2, "HGCM thread ended\n");
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Static callback function for X11 message processing thread. All X11 messages
* will be directly routed to the according drag'n drop instance.
*
* @returns IPRT status code.
* @param hThread Thread handle to use.
* @param pvUser Pointer to DragAndDropService instance to use.
*/
/* static */
DECLCALLBACK(int) DragAndDropService::x11EventThread(RTTHREAD hThread, void *pvUser)
{
AssertPtrReturn(pvUser, VERR_INVALID_PARAMETER);
DragAndDropService *pThis = static_cast<DragAndDropService*>(pvUser);
AssertPtr(pThis);
int rc = VINF_SUCCESS;
/* Note: Nothing to initialize here (yet). */
/* Let the service instance know in any case. */
int rc2 = RTThreadUserSignal(hThread);
AssertRC(rc2);
VBClLogVerbose(2, "X11 thread started\n");
DNDEVENT e;
RT_ZERO(e);
e.enmType = DNDEVENT::DnDEventType_X11;
do
{
/*
* Wait for new events. We can't use XIfEvent here, cause this locks
* the window connection with a mutex and if no X11 events occurs this
* blocks any other calls we made to X11. So instead check for new
* events and if there are not any new one, sleep for a certain amount
* of time.
*/
unsigned cNewEvents = 0;
unsigned cQueued = XEventsQueued(pThis->m_pDisplay, QueuedAfterFlush);
while (cQueued)
{
/* XNextEvent will block until a new X event becomes available. */
XNextEvent(pThis->m_pDisplay, &e.x11);
{
rc2 = RTCritSectEnter(&pThis->m_eventQueueCS);
if (RT_SUCCESS(rc2))
{
LogFlowFunc(("Added new X11 event, type=%d\n", e.x11.type));
pThis->m_eventQueue.append(e);
cNewEvents++;
rc2 = RTCritSectLeave(&pThis->m_eventQueueCS);
AssertRC(rc2);
}
}
cQueued--;
}
if (cNewEvents)
{
rc = RTSemEventSignal(pThis->m_hEventSem);
if (RT_FAILURE(rc))
break;
continue;
}
/* No new events; wait a bit. */
RTThreadSleep(25 /* ms */);
} while (!ASMAtomicReadBool(&pThis->m_fStop));
VBClLogVerbose(2, "X11 thread ended\n");
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* @interface_method_impl{VBCLSERVICE,pfnInit}
*/
static DECLCALLBACK(int) vbclDnDInit(void)
{
return g_Svc.init();
}
/**
* @interface_method_impl{VBCLSERVICE,pfnWorker}
*/
static DECLCALLBACK(int) vbclDnDWorker(bool volatile *pfShutdown)
{
return g_Svc.worker(pfShutdown);
}
/**
* @interface_method_impl{VBCLSERVICE,pfnStop}
*/
static DECLCALLBACK(void) vbclDnDStop(void)
{
g_Svc.stop();
}
/**
* @interface_method_impl{VBCLSERVICE,pfnTerm}
*/
static DECLCALLBACK(int) vbclDnDTerm(void)
{
return g_Svc.term();
}
VBCLSERVICE g_SvcDragAndDrop =
{
"dnd", /* szName */
"Drag'n'Drop", /* pszDescription */
".vboxclient-draganddrop.pid", /* pszPidFilePath */
NULL, /* pszUsage */
NULL, /* pszOptions */
NULL, /* pfnOption */
vbclDnDInit, /* pfnInit */
vbclDnDWorker, /* pfnWorker */
vbclDnDStop, /* pfnStop*/
vbclDnDTerm /* pfnTerm */
};
|