summaryrefslogtreecommitdiff
path: root/tools/virtual.c
blob: 9d0188461d74876b8bad7e18ba56eec16b213442 (plain)
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
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xatom.h>

#include <X11/Xlibint.h>
#include <X11/extensions/record.h>
#include <X11/extensions/XShm.h>
#if HAVE_X11_EXTENSIONS_SHMPROTO_H
#include <X11/extensions/shmproto.h>
#elif HAVE_X11_EXTENSIONS_SHMSTR_H
#include <X11/extensions/shmstr.h>
#else
#error Failed to find the right header for X11 MIT-SHM protocol definitions
#endif
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/Xinerama.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/Xrender.h>
#include <X11/Xcursor/Xcursor.h>
#include <pixman.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/timerfd.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <getopt.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>

#if 0
#define DBG(x) printf x
#define EXTRA_DBG 1
#else
#define DBG(x)
#define EXTRA_DBG 0
#endif

#define FORCE_FULL_REDRAW 0

struct display {
	Display *dpy;
	struct clone *clone;
	struct context *ctx;

	int damage_event, damage_error;
	int xfixes_event, xfixes_error;
	int rr_event, rr_error, rr_active;
	int xinerama_event, xinerama_error, xinerama_active;
	Window root;
	Visual *visual;
	Damage damage;

	int width;
	int height;
	int depth;

	XRenderPictFormat *root_format;
	XRenderPictFormat *rgb16_format;
	XRenderPictFormat *rgb24_format;

	int has_shm;
	int has_shm_pixmap;
	int shm_opcode;
	int shm_event;

	Cursor invisible_cursor;
	Cursor visible_cursor;

	XcursorImage cursor_image;
	int cursor_serial;
	int cursor_x;
	int cursor_y;
	int cursor_moved;
	int cursor_visible;
	int cursor;

	int flush;
	int send;
	int skip_clone;
	int skip_frame;
};

struct output {
	struct display *display;
	Display *dpy;
	char *name;
	RROutput rr_output;
	RRCrtc rr_crtc;
	Window window;
	Picture win_picture;
	Picture pix_picture;
	Pixmap pixmap;
	GC gc;

	long serial;
	int use_shm;
	int use_shm_pixmap;
	XShmSegmentInfo shm;

	XRenderPictFormat *use_render;

	int x, y;
	XRRModeInfo mode;
	Rotation rotation;
};

struct clone {
	struct clone *next;
	struct clone *active;

	struct output src, dst;
	long timestamp;

	XShmSegmentInfo shm;
	XImage image;

	int width, height, depth;
	struct { int x1, x2, y1, y2; } damaged;
	int rr_update;
};

struct context {
	struct display *display;
	struct clone *clones;
	struct clone *active;
	struct pollfd *pfd;
#define timer pfd[0].fd
	Display *record;
	int nclone;
	int ndisplay;
	int nfd;

	int timer_active;

	long timestamp;
	long configTimestamp;

	Atom singleton;
	char command[1024];
	int command_continuation;
};

static inline int is_power_of_2(unsigned long n)
{
	return n && ((n & (n - 1)) == 0);
}

static int xlib_vendor_is_xorg(Display *dpy)
{
	const char *const vendor = ServerVendor(dpy);
	return strstr(vendor, "X.Org") || strstr(vendor, "Xorg");
}

static inline XRRScreenResources *_XRRGetScreenResourcesCurrent(Display *dpy, Window window)
{
	XRRScreenResources *res;

	res = XRRGetScreenResourcesCurrent(dpy, window);
	if (res == NULL)
		res = XRRGetScreenResources(dpy, window);

	return res;
}

#define XORG_VERSION_ENCODE(major,minor,patch,snap) \
    (((major) * 10000000) + ((minor) * 100000) + ((patch) * 1000) + snap)

static int _x_error_occurred;

static int
_check_error_handler(Display     *display,
		     XErrorEvent *event)
{
	DBG(("X11 error from display %s, serial=%ld, error=%d, req=%d.%d\n",
	     DisplayString(display),
	     event->serial,
	     event->error_code,
	     event->request_code,
	     event->minor_code));
	_x_error_occurred = 1;
	return False; /* ignored */
}

static int
can_use_shm(Display *dpy,
	    Window window,
	    int *shm_event,
	    int *shm_opcode,
	    int *shm_pixmap)
{
	XShmSegmentInfo shm;
	Status success;
	XExtCodes *codes;
	int major, minor, has_shm, has_pixmap;

	if (!XShmQueryExtension(dpy))
		return 0;

	XShmQueryVersion(dpy, &major, &minor, &has_pixmap);

	shm.shmid = shmget(IPC_PRIVATE, 0x1000, IPC_CREAT | 0600);
	if (shm.shmid == -1)
		return 0;

	shm.readOnly = 0;
	shm.shmaddr = shmat(shm.shmid, NULL, 0);
	if (shm.shmaddr == (char *) -1) {
		shmctl(shm.shmid, IPC_RMID, NULL);
		return 0;
	}

	XSync(dpy, False);
	_x_error_occurred = 0;

	success = XShmAttach(dpy, &shm);

	XSync(dpy, False);
	has_shm = success && _x_error_occurred == 0;

	/* As libXext sets the SEND_EVENT bit in the ShmCompletionEvent,
	 * the Xserver may crash if it does not take care when processing
	 * the event type. For instance versions of Xorg prior to 1.11.1
	 * exhibited this bug, and was fixed by:
	 *
	 * commit 2d2dce558d24eeea0eb011ec9ebaa6c5c2273c39
	 * Author: Sam Spilsbury <sam.spilsbury@canonical.com>
	 * Date:   Wed Sep 14 09:58:34 2011 +0800
	 *
	 * Remove the SendEvent bit (0x80) before doing range checks on event type.
	 */
	codes = 0;
	if (has_shm)
		codes = XInitExtension(dpy, SHMNAME);
	if (xlib_vendor_is_xorg(dpy) &&
	    VendorRelease(dpy) < XORG_VERSION_ENCODE(1,11,0,1))
		codes = 0;
	if (codes) {
		XShmCompletionEvent e;

		memset(&e, 0, sizeof(e));

		e.type = codes->first_event;
		e.send_event = 1;
		e.serial = 1;
		e.drawable = window;
		e.major_code = codes->major_opcode;
		e.minor_code = X_ShmPutImage;

		e.shmseg = shm.shmid;
		e.offset = 0;

		XSendEvent(dpy, e.drawable, False, 0, (XEvent *)&e);
		XSync(dpy, False);

		if (_x_error_occurred == 0) {
			*shm_opcode = codes->major_opcode;
			*shm_event = codes->first_event;
			*shm_pixmap = has_pixmap;
		}
	}

	XShmDetach(dpy, &shm);
	shmctl(shm.shmid, IPC_RMID, NULL);
	shmdt(shm.shmaddr);

	return has_shm;
}

static int timerfd(int hz)
{
	struct itimerspec it;
	int fd;

	fd = -1;
#ifdef CLOCK_MONOTONIC_COARSE
	fd = timerfd_create(CLOCK_MONOTONIC_COARSE, TFD_NONBLOCK);
#endif
	if (fd < 0)
		fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
	if (fd < 0)
		return -ETIME;

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_nsec = 1000000000 / hz;
	it.it_value = it.it_interval;
	if (timerfd_settime(fd, 0, &it, NULL) < 0) {
		close(fd);
		return -ETIME;
	}

	return fd;
}

static int context_init(struct context *ctx)
{
	struct pollfd *pfd;

	memset(ctx, 0, sizeof(*ctx));

	ctx->pfd = malloc(2*sizeof(struct pollfd));
	if (ctx->pfd == NULL)
		return -ENOMEM;

	ctx->clones = malloc(sizeof(struct clone));
	if (ctx->clones == NULL)
		return -ENOMEM;

	ctx->display = malloc(sizeof(struct display));
	if (ctx->display == NULL)
		return -ENOMEM;

	pfd = memset(&ctx->pfd[ctx->nfd++], 0, sizeof(struct pollfd));
	pfd->fd = timerfd(60);
	if (pfd->fd < 0)
		return pfd->fd;
	pfd->events = POLLIN;

	return 0;
}

static void context_enable_timer(struct context *ctx)
{
	uint64_t count;

	DBG(("%s timer active? %d\n", __func__, ctx->timer_active));

	if (ctx->timer_active)
		return;

	/* reset timer */
	count = read(ctx->timer, &count, sizeof(count));

	ctx->timer_active = 1;
}

static int add_fd(struct context *ctx, int fd)
{
	struct pollfd *pfd;

	if (fd < 0)
		return fd;

	if (is_power_of_2(ctx->nfd)) {
		ctx->pfd = realloc(ctx->pfd, 2*ctx->nfd*sizeof(struct pollfd));
		if (ctx->pfd == NULL)
			return -ENOMEM;
	}

	pfd = memset(&ctx->pfd[ctx->nfd++], 0, sizeof(struct pollfd));
	pfd->fd = fd;
	pfd->events = POLLIN;
	return 0;
}

static void display_mark_flush(struct display *display)
{
	DBG(("%s mark flush (flush=%d)\n",
	     DisplayString(display->dpy), display->flush));

	if (display->flush)
		return;

	context_enable_timer(display->ctx);
	display->flush = 1;
}

static int mode_equal(const XRRModeInfo *a, const XRRModeInfo *b)
{
	return (a->width == b->width &&
		a->height == b->height &&
		a->dotClock == b->dotClock &&
		a->hSyncStart == b->hSyncStart &&
		a->hSyncEnd == b->hSyncEnd &&
		a->hTotal == b->hTotal &&
		a->hSkew == b->hSkew &&
		a->vSyncStart == b->vSyncStart &&
		a->vSyncEnd == b->vSyncEnd &&
		a->vTotal == b->vTotal &&
		a->modeFlags == b->modeFlags);
}

static XRRModeInfo *lookup_mode(XRRScreenResources *res, int id)
{
	int i;

	for (i = 0; i < res->nmode; i++) {
		if (res->modes[i].id == id)
			return &res->modes[i];
	}

	return NULL;
}

static void clone_update_edid(struct clone *clone)
{
	unsigned long nitems, after;
	unsigned char *data;
	int format;
	Atom type;

	if (XRRGetOutputProperty(clone->dst.dpy, clone->dst.rr_output,
				 XInternAtom(clone->dst.dpy, "EDID", False),
				 0, 100, False, False, AnyPropertyType,
				 &type, &format, &nitems, &after, &data) == Success) {
		XRRChangeOutputProperty(clone->src.dpy, clone->src.rr_output,
					XInternAtom(clone->src.dpy, "EDID", False),
					type, format, PropModeReplace, data, nitems);
	}
}

static void disable_crtc(Display *dpy, XRRScreenResources *res, RRCrtc crtc)
{
	XRRPanning panning;

	if (crtc == 0)
		return;

	XRRSetPanning(dpy, res, crtc, memset(&panning, 0, sizeof(panning)));
	XRRSetCrtcConfig(dpy, res, crtc, CurrentTime, 0, 0, None, RR_Rotate_0, NULL, 0);
}

static int clone_update_modes__randr(struct clone *clone)
{
	XRRScreenResources *from_res = NULL, *to_res = NULL;
	XRROutputInfo *from_info = NULL, *to_info = NULL;
	int i, j, ret = ENOENT;

	assert(clone->src.rr_output);
	assert(clone->dst.rr_output);
	assert(clone->dst.display->rr_event);

	from_res = _XRRGetScreenResourcesCurrent(clone->dst.dpy, clone->dst.window);
	if (from_res == NULL)
		goto err;

	from_info = XRRGetOutputInfo(clone->dst.dpy, from_res, clone->dst.rr_output);
	if (from_info == NULL)
		goto err;

	DBG(("%s(%s-%s <- %s-%s): timestamp %ld (last %ld)\n", __func__,
	     DisplayString(clone->src.dpy), clone->src.name,
	     DisplayString(clone->dst.dpy), clone->dst.name,
	     from_info->timestamp, clone->timestamp));

	to_res = _XRRGetScreenResourcesCurrent(clone->src.dpy, clone->src.window);
	if (to_res == NULL)
		goto err;

	to_info = XRRGetOutputInfo(clone->src.dpy, to_res, clone->src.rr_output);
	if (to_info == NULL)
		goto err;

	DBG(("%s: dst.rr_crtc=%ld, now %ld\n",
	     __func__, (long)clone->dst.rr_crtc, (long)from_info->crtc));
	if (clone->dst.rr_crtc == from_info->crtc) {
		for (i = 0; i < to_info->nmode; i++) {
			XRRModeInfo *mode, *old;

			mode = lookup_mode(to_res, to_info->modes[i]);
			if (mode == NULL)
				break;

			DBG(("%s(%s-%s): lookup mode %s\n", __func__,
			     DisplayString(clone->src.dpy), clone->src.name,
			     mode->name));

			for (j = 0; j < from_info->nmode; j++) {
				old = lookup_mode(from_res, from_info->modes[j]);
				if (old && mode_equal(mode, old)) {
					mode = NULL;
					break;
				}
			}
			if (mode) {
				DBG(("%s(%s-%s): unknown mode %s\n", __func__,
				     DisplayString(clone->src.dpy), clone->src.name,
				     mode->name));
				break;
			}
		}
		if (i == from_info->nmode && i == to_info->nmode) {
			DBG(("%s(%s-%s): no change in output\n", __func__,
			     DisplayString(clone->src.dpy), clone->src.name));
			goto done;
		}
	}

	/* Disable the remote output */
	if (from_info->crtc != clone->dst.rr_crtc) {
		DBG(("%s(%s-%s): disabling active CRTC\n", __func__,
		     DisplayString(clone->dst.dpy), clone->dst.name));
		disable_crtc(clone->dst.dpy, from_res, from_info->crtc);
		clone->dst.rr_crtc = 0;
		clone->dst.mode.id = 0;
	}

	/* Clear all current UserModes on the output, including any active ones */
	if (to_info->crtc) {
		DBG(("%s(%s-%s): disabling active CRTC\n", __func__,
		     DisplayString(clone->src.dpy), clone->src.name));
		disable_crtc(clone->src.dpy, to_res, to_info->crtc);
	}
	for (i = 0; i < to_info->nmode; i++) {
		DBG(("%s(%s-%s): deleting mode %ld\n", __func__,
		     DisplayString(clone->src.dpy), clone->src.name, (long)to_info->modes[i]));
		XRRDeleteOutputMode(clone->src.dpy, clone->src.rr_output, to_info->modes[i]);
	}

	clone->src.rr_crtc = 0;

	/* Create matching modes for the real output on the virtual */
	XGrabServer(clone->src.dpy);
	for (i = 0; i < from_info->nmode; i++) {
		XRRModeInfo *mode, *old;
		RRMode id;

		mode = lookup_mode(from_res, from_info->modes[i]);
		if (mode == NULL)
			continue;
		for (j = 0; j < i; j++) {
			old = lookup_mode(from_res, from_info->modes[j]);
			if (old && mode_equal(mode, old)) {
				mode = NULL;
				break;
			}
		}
		if (mode == NULL)
			continue;

		id = 0;
		for (j = 0; j < to_res->nmode; j++) {
			old = &to_res->modes[j];
			if (mode_equal(mode, old)) {
				id = old->id;
				DBG(("%s(%s-%s): reusing mode %ld: %s\n", __func__,
				     DisplayString(clone->src.dpy), clone->src.name, id, mode->name));
				break;
			}
		}
		if (id == 0) {
			XRRModeInfo m;
			char buf[256];

			/* XXX User names must be unique! */
			m = *mode;
			m.nameLength = snprintf(buf, sizeof(buf),
						"%s.%ld-%s", clone->src.name, (long)from_info->modes[i], mode->name);
			m.name = buf;

			id = XRRCreateMode(clone->src.dpy, clone->src.window, &m);
			DBG(("%s(%s-%s): adding mode %ld: %s\n", __func__,
			     DisplayString(clone->src.dpy), clone->src.name, id, mode->name));
		}

		XRRAddOutputMode(clone->src.dpy, clone->src.rr_output, id);
	}
	clone_update_edid(clone);
	XUngrabServer(clone->src.dpy);
done:
	ret = 0;
	clone->timestamp = from_info->timestamp;

err:
	if (to_info)
		XRRFreeOutputInfo(to_info);
	if (to_res)
		XRRFreeScreenResources(to_res);
	if (from_info)
		XRRFreeOutputInfo(from_info);
	if (from_res)
		XRRFreeScreenResources(from_res);

	return ret;
}

static int clone_update_modes__fixed(struct clone *clone)
{
	char mode_name[80];
	XRRScreenResources *res = NULL;
	XRROutputInfo *info = NULL;
	XRRModeInfo mode;
	RRMode id;
	int i, j, ret = ENOENT;

	assert(clone->src.rr_output);

	res = _XRRGetScreenResourcesCurrent(clone->src.dpy, clone->src.window);
	if (res == NULL)
		goto err;

	info = XRRGetOutputInfo(clone->src.dpy, res, clone->src.rr_output);
	if (info == NULL)
		goto err;

	XGrabServer(clone->src.dpy);

	/* Clear all current UserModes on the output, including any active ones */
	if (info->crtc) {
		DBG(("%s(%s-%s): disabling active CRTC\n", __func__,
		     DisplayString(clone->src.dpy), clone->src.name));
		disable_crtc(clone->src.dpy, res, info->crtc);
	}
	for (i = 0; i < info->nmode; i++) {
		DBG(("%s(%s-%s): deleting mode %ld\n", __func__,
		     DisplayString(clone->src.dpy), clone->src.name, (long)info->modes[i]));
		XRRDeleteOutputMode(clone->src.dpy, clone->src.rr_output, info->modes[i]);
	}

	clone->src.rr_crtc = 0;

	/* Create matching mode for the real output on the virtual */
	memset(&mode, 0, sizeof(mode));
	mode.width = clone->width;
	mode.height = clone->height;
	mode.nameLength = sprintf(mode_name, "FAKE-%dx%d", mode.width, mode.height);
	mode.name = mode_name;

	id = 0;
	for (j = 0; j < res->nmode; j++) {
		if (mode_equal(&mode, &res->modes[j])) {
			id = res->modes[j].id;
			break;
		}
	}
	if (id == 0)
		id = XRRCreateMode(clone->src.dpy, clone->src.window, &mode);

	XRRAddOutputMode(clone->src.dpy, clone->src.rr_output, id);

	XUngrabServer(clone->src.dpy);
	ret = 0;
err:
	if (info)
		XRRFreeOutputInfo(info);
	if (res)
		XRRFreeScreenResources(res);

	return ret;
}

static RROutput claim_virtual(struct display *display, char *output_name, int nclone)
{
	char mode_name[] = "ClaimVirtualHead";
	Display *dpy = display->dpy;
	XRRScreenResources *res;
	XRROutputInfo *output;
	XRRModeInfo mode;
	RRMode id;
	RROutput rr_output = 0;
	int i;

	DBG(("%s(%d)\n", __func__, nclone));
	XGrabServer(dpy);

	res = _XRRGetScreenResourcesCurrent(dpy, display->root);
	if (res == NULL)
		goto out;

	sprintf(output_name, "VIRTUAL%d", nclone);

	for (i = rr_output = 0; rr_output == 0 && i < res->noutput; i++) {
		output = XRRGetOutputInfo(dpy, res, res->outputs[i]);
		if (output == NULL)
			continue;

		if (strcmp(output->name, output_name) == 0)
			rr_output = res->outputs[i];

		XRRFreeOutputInfo(output);
	}
	for (i = id = 0; id == 0 && i < res->nmode; i++) {
		if (strcmp(res->modes[i].name, mode_name) == 0)
			id = res->modes[i].id;
	}
	XRRFreeScreenResources(res);

	DBG(("%s(%s): rr_output=%ld\n", __func__, output_name, (long)rr_output));
	if (rr_output == 0)
		goto out;

	/* Set any mode on the VirtualHead to make the Xserver allocate another */
	memset(&mode, 0, sizeof(mode));
	mode.width = 1024;
	mode.height = 768;
	mode.name = mode_name;
	mode.nameLength = sizeof(mode_name) - 1;

	if (id == 0)
		id = XRRCreateMode(dpy, display->root, &mode);
	XRRAddOutputMode(dpy, rr_output, id);

	/* Force a redetection for the ddx to spot the new outputs */
	res = XRRGetScreenResources(dpy, display->root);
	if (res == NULL)
		goto out;

	/* Some else may have interrupted us and installed that new mode! */
	output = XRRGetOutputInfo(dpy, res, rr_output);
	if (output) {
		disable_crtc(dpy, res, output->crtc);
		XRRFreeOutputInfo(output);
	}
	XRRFreeScreenResources(res);

	XRRDeleteOutputMode(dpy, rr_output, id);
	XRRDestroyMode(dpy, id);

out:
	XUngrabServer(dpy);

	return rr_output;
}

static int stride_for_depth(int width, int depth)
{
	if (depth == 24)
		depth = 32;
	return ((width * depth + 7) / 8 + 3) & ~3;
}

static void init_image(struct clone *clone)
{
	XImage *image = &clone->image;
	int ret;

	image->width = clone->width;
	image->height = clone->height;
	image->format = ZPixmap;
	image->xoffset = 0;
	image->byte_order = LSBFirst;
	image->bitmap_unit = 32;
	image->bitmap_bit_order = LSBFirst;
	image->bitmap_pad = 32;
	image->data = clone->shm.shmaddr;
	image->bytes_per_line = stride_for_depth(clone->width, clone->depth);
	switch (clone->depth) {
	case 24:
		image->red_mask = 0xff << 16;
		image->green_mask = 0xff << 8;
		image->blue_mask = 0xff << 0;;
		image->depth = 24;
		image->bits_per_pixel = 32;
		break;
	case 16:
		image->red_mask = 0x1f << 11;
		image->green_mask = 0x3f << 5;
		image->blue_mask = 0x1f << 0;;
		image->depth = 16;
		image->bits_per_pixel = 16;
		break;
	}

	ret = XInitImage(image);
	assert(ret);
	(void)ret;
}

static int mode_height(const XRRModeInfo *mode, Rotation rotation)
{
	switch (rotation & 0xf) {
	case RR_Rotate_0:
	case RR_Rotate_180:
		return mode->height;
	case RR_Rotate_90:
	case RR_Rotate_270:
		return mode->width;
	default:
		return 0;
	}
}

static int mode_width(const XRRModeInfo *mode, Rotation rotation)
{
	switch (rotation & 0xf) {
	case RR_Rotate_0:
	case RR_Rotate_180:
		return mode->width;
	case RR_Rotate_90:
	case RR_Rotate_270:
		return mode->height;
	default:
		return 0;
	}
}

static void output_init_xfer(struct clone *clone, struct output *output)
{
	if (output->use_shm_pixmap) {
		DBG(("%s-%s: creating shm pixmap\n", DisplayString(output->dpy), output->name));
		if (output->pixmap)
			XFreePixmap(output->dpy, output->pixmap);
		output->pixmap = XShmCreatePixmap(output->dpy, output->window,
						  clone->shm.shmaddr, &output->shm,
						  clone->width, clone->height, clone->depth);
		if (output->pix_picture) {
			XRenderFreePicture(output->dpy, output->pix_picture);
			output->pix_picture = None;
		}
	}
	if (output->use_render) {
		DBG(("%s-%s: creating picture\n", DisplayString(output->dpy), output->name));
		if (output->win_picture == None)
			output->win_picture = XRenderCreatePicture(output->dpy, output->window,
								   output->display->root_format, 0, NULL);
		if (output->pixmap == None)
			output->pixmap = XCreatePixmap(output->dpy, output->window,
						       clone->width, clone->height, clone->depth);
		if (output->pix_picture == None)
			output->pix_picture = XRenderCreatePicture(output->dpy, output->pixmap,
								   output->use_render, 0, NULL);
	}

	if (output->gc == None) {
		XGCValues gcv;

		gcv.graphics_exposures = False;
		gcv.subwindow_mode = IncludeInferiors;

		output->gc = XCreateGC(output->dpy, output->pixmap ?: output->window, GCGraphicsExposures | GCSubwindowMode, &gcv);
	}
}

static int clone_init_xfer(struct clone *clone)
{
	int width, height;

	if (clone->src.mode.id == 0) {
		if (clone->width == 0 && clone->height == 0)
			return 0;

		clone->width = 0;
		clone->height = 0;

		if (clone->src.use_shm)
			XShmDetach(clone->src.dpy, &clone->src.shm);
		if (clone->dst.use_shm)
			XShmDetach(clone->dst.dpy, &clone->dst.shm);

		if (clone->shm.shmaddr) {
			shmdt(clone->shm.shmaddr);
			clone->shm.shmaddr = 0;
		}

		clone->damaged.x2 = clone->damaged.y2 = INT_MIN;
		clone->damaged.x1 = clone->damaged.y1 = INT_MAX;
		return 0;
	}

	width = mode_width(&clone->src.mode, clone->src.rotation);
	height = mode_height(&clone->src.mode, clone->src.rotation);

	if (width == clone->width && height == clone->height)
		return 0;

	DBG(("%s-%s create xfer, %dx%d\n",
	     DisplayString(clone->dst.dpy), clone->dst.name,
	     width, height));

	clone->width = width;
	clone->height = height;

	if (clone->shm.shmaddr)
		shmdt(clone->shm.shmaddr);

	clone->shm.shmid = shmget(IPC_PRIVATE,
				  height * stride_for_depth(width, clone->depth),
				  IPC_CREAT | 0666);
	if (clone->shm.shmid == -1)
		return errno;

	clone->shm.shmaddr = shmat(clone->shm.shmid, 0, 0);
	if (clone->shm.shmaddr == (char *) -1) {
		shmctl(clone->shm.shmid, IPC_RMID, NULL);
		return ENOMEM;
	}

	init_image(clone);

	if (clone->src.use_shm) {
		clone->src.shm = clone->shm;
		clone->dst.shm.readOnly = False;
		XShmAttach(clone->src.dpy, &clone->src.shm);
		XSync(clone->src.dpy, False);
	}
	if (clone->dst.use_shm) {
		clone->dst.shm = clone->shm;
		clone->dst.shm.readOnly = True;
		XShmAttach(clone->dst.dpy, &clone->dst.shm);
		XSync(clone->dst.dpy, False);
	}

	shmctl(clone->shm.shmid, IPC_RMID, NULL);

	output_init_xfer(clone, &clone->src);
	output_init_xfer(clone, &clone->dst);

	clone->damaged.x1 = clone->src.x;
	clone->damaged.x2 = clone->src.x + width;
	clone->damaged.y1 = clone->src.y;
	clone->damaged.y2 = clone->src.y + height;

	display_mark_flush(clone->dst.display);
	return 0;
}

static void clone_update(struct clone *clone)
{
	if (!clone->rr_update)
		return;

	DBG(("%s-%s cloning modes\n",
	     DisplayString(clone->dst.dpy), clone->dst.name));

	clone_update_modes__randr(clone);
	clone->rr_update = 0;
}

static int context_update(struct context *ctx)
{
	Display *dpy = ctx->display->dpy;
	XRRScreenResources *res;
	int context_changed = 0;
	int i, n;

	DBG(("%s\n", __func__));

	res = _XRRGetScreenResourcesCurrent(dpy, ctx->display->root);
	if (res == NULL)
		return 0;

	DBG(("%s timestamp %ld (last %ld), config %ld (last %ld)\n",
	     DisplayString(dpy),
	     res->timestamp, ctx->timestamp,
	     res->configTimestamp, ctx->configTimestamp));
	if (res->timestamp == ctx->timestamp &&
	    res->configTimestamp == ctx->configTimestamp &&
	    res->timestamp != res->configTimestamp) { /* mutter be damned */
		XRRFreeScreenResources(res);
		return 0;
	}

	ctx->timestamp = res->timestamp;
	ctx->configTimestamp = res->configTimestamp;

	for (n = 0; n < ctx->nclone; n++) {
		struct output *output = &ctx->clones[n].src;
		XRROutputInfo *o;
		XRRCrtcInfo *c;
		RRMode mode = 0;
		int changed = 0;

		o = XRRGetOutputInfo(dpy, res, output->rr_output);
		if (o == NULL)
			continue;

		c = NULL;
		if (o->crtc)
			c = XRRGetCrtcInfo(dpy, res, o->crtc);
		if (c) {
			DBG(("%s-%s: (x=%d, y=%d, rotation=%d, mode=%ld) -> (x=%d, y=%d, rotation=%d, mode=%ld)\n",
			     DisplayString(dpy), output->name,
			     output->x, output->y, output->rotation, output->mode.id,
			     c->x, c->y, c->rotation, c->mode));

			changed |= output->rotation != c->rotation;
			output->rotation = c->rotation;

			changed |= output->x != c->x;
			output->x = c->x;

			changed |= output->y != c->y;
			output->y = c->y;

			changed |= output->mode.id != c->mode;
			mode = c->mode;
			XRRFreeCrtcInfo(c);
		} else {
			DBG(("%s-%s: (x=%d, y=%d, rotation=%d, mode=%ld) -> off\n",
			     DisplayString(dpy), output->name,
			     output->x, output->y, output->rotation, output->mode.id));
		}
		output->rr_crtc = o->crtc;
		XRRFreeOutputInfo(o);

		DBG(("%s-%s crtc changed? %d\n",
		     DisplayString(ctx->clones[n].dst.display->dpy), ctx->clones[n].dst.name, changed));

		if (mode) {
			if (output->mode.id != mode) {
				for (i = 0; i < res->nmode; i++) {
					if (res->modes[i].id == mode) {
						output->mode = res->modes[i];
						break;
					}
				}
			}
		} else {
			changed = output->mode.id != 0;
			output->mode.id = 0;
		}

		DBG(("%s-%s output changed? %d\n",
		     DisplayString(ctx->clones[n].dst.display->dpy), ctx->clones[n].dst.name, changed));

		if (changed)
			clone_init_xfer(&ctx->clones[n]);
		context_changed |= changed;
	}
	XRRFreeScreenResources(res);

	DBG(("%s changed? %d\n", DisplayString(dpy), context_changed));
	if (!context_changed)
		return 0;

	for (n = 1; n < ctx->ndisplay; n++) {
		struct display *display = &ctx->display[n];
		struct clone *clone;
		int x1, x2, y1, y2;

		if (display->rr_active == 0)
			continue;

		x1 = y1 = INT_MAX;
		x2 = y2 = INT_MIN;

		for (clone = display->clone; clone; clone = clone->next) {
			struct output *output = &clone->src;
			int v;

			assert(clone->dst.display == display);

			if (output->mode.id == 0)
				continue;

			DBG(("%s: source %s enabled (%d, %d)x(%d, %d)\n",
			     DisplayString(clone->dst.dpy), output->name,
			     output->x, output->y,
			     mode_width(&output->mode, output->rotation),
			     mode_height(&output->mode, output->rotation)));

			if (output->x < x1)
				x1 = output->x;
			if (output->y < y1)
				y1 = output->y;

			v = (int)output->x + mode_width(&output->mode, output->rotation);
			if (v > x2)
				x2 = v;
			v = (int)output->y + mode_height(&output->mode, output->rotation);
			if (v > y2)
				y2 = v;
		}

		DBG(("%s fb bounds (%d, %d)x(%d, %d)\n", DisplayString(display->dpy),
		     x1, y1, x2, y2));

		XGrabServer(display->dpy);
		res = _XRRGetScreenResourcesCurrent(display->dpy, display->root);
		if (res == NULL)
			goto ungrab;

		if (x2 <= x1 || y2 <= y1) {
			/* Nothing enabled, preserve the current fb, and turn everything off */
			for (clone = display->clone; clone; clone = clone->next) {
				struct output *dst = &clone->dst;

				if (!dst->rr_crtc)
					continue;

				DBG(("%s: disabling output '%s'\n",
				     DisplayString(dst->dpy), dst->name));
				disable_crtc(dpy, res, dst->rr_crtc);
				dst->rr_crtc = 0;
				dst->mode.id = 0;
			}
			goto free_res;
		}

		x2 -= x1;
		y2 -= y1;
		DBG(("%s: current size %dx%d, need %dx%d\n",
		     DisplayString(display->dpy),
		     display->width, display->height,
		     x2, y2));

		if (display->width != x2 || display->height != y2) {
			/* When shrinking we have to manually resize the fb */
			for (clone = display->clone; clone; clone = clone->next) {
				struct output *dst = &clone->dst;

				if (!dst->rr_crtc)
					continue;

				DBG(("%s: disabling output '%s'\n",
				     DisplayString(dst->dpy), dst->name));
				disable_crtc(dpy, res, dst->rr_crtc);
				dst->rr_crtc = 0;
				dst->mode.id = 0;
			}

			DBG(("%s: XRRSetScreenSize %dx%d\n", DisplayString(display->dpy), x2, y2));
			XRRSetScreenSize(display->dpy, display->root, x2, y2, x2 * 96 / 25.4, y2 * 96 / 25.4);
			display->width = x2;
			display->height = y2;
		}

		for (clone = display->clone; clone; clone = clone->next) {
			struct output *src = &clone->src;
			struct output *dst = &clone->dst;
			XRROutputInfo *o;
			XRRPanning panning;
			struct clone *set;
			RRCrtc rr_crtc;
			Status ret;

			DBG(("%s: copying configuration from %s (mode=%ld: %dx%d) to %s\n",
			     DisplayString(dst->dpy),
			     src->name, (long)src->mode.id, src->mode.width, src->mode.height,
			     dst->name));

			if (src->mode.id == 0) {
err:
				if (dst->rr_crtc) {
					DBG(("%s: disabling unused output '%s'\n",
					     DisplayString(dst->dpy), dst->name));
					disable_crtc(dpy, res, dst->rr_crtc);
					dst->rr_crtc = 0;
					dst->mode.id = 0;
				}
				continue;
			}

			dst->x = src->x - x1;
			dst->y = src->y - y1;
			dst->rotation = src->rotation;
			dst->mode = src->mode;

			dst->mode.id = 0;
			for (i = 0; i < res->nmode; i++) {
				if (mode_equal(&src->mode, &res->modes[i])) {
					dst->mode.id = res->modes[i].id;
					break;
				}
			}
			if (dst->mode.id == 0) {
				XRRModeInfo m;
				char buf[256];
				RRMode id;

				/* XXX User names must be unique! */
				m = src->mode;
				m.nameLength = snprintf(buf, sizeof(buf),
							"%s.%ld-%dx%d", src->name,
							(long)src->mode.id,
							src->mode.width,
							src->mode.height);
				m.name = buf;

				id = XRRCreateMode(dst->dpy, dst->window, &m);
				if (id) {
					DBG(("%s: adding mode %ld: %dx%d to %s, new mode %ld\n",
					     DisplayString(dst->dpy),
					     (long)src->mode.id,
					     src->mode.width,
					     src->mode.height,
					     dst->name, (long)id));
					XRRAddOutputMode(dst->dpy, dst->rr_output, id);
					dst->mode.id = id;
				} else {
					DBG(("%s: failed to find suitable mode for %s\n",
					     DisplayString(dst->dpy), dst->name));
					goto err;
				}
			}

			rr_crtc = dst->rr_crtc;
			if (rr_crtc) {
				for (set = display->clone; set != clone; set = set->next) {
					if (set->dst.rr_crtc == rr_crtc) {
						DBG(("%s: CRTC reassigned from %s\n",
						     DisplayString(dst->dpy), dst->name));
						rr_crtc = 0;
						break;
					}
				}
			}
			if (rr_crtc == 0) {
				o = XRRGetOutputInfo(dst->dpy, res, dst->rr_output);
				for (i = 0; i < o->ncrtc; i++) {
					DBG(("%s: checking whether CRTC:%ld is available\n",
					     DisplayString(dst->dpy), (long)o->crtcs[i]));
					for (set = display->clone; set != clone; set = set->next) {
						if (set->dst.rr_crtc == o->crtcs[i]) {
							DBG(("%s: CRTC:%ld already assigned to %s\n",
							     DisplayString(dst->dpy), (long)o->crtcs[i], set->dst.name));
							break;
						}
					}
					if (set == clone) {
						rr_crtc = o->crtcs[i];
						break;
					}
				}
				XRRFreeOutputInfo(o);
			}
			if (rr_crtc == 0) {
				DBG(("%s: failed to find available CRTC for %s\n",
				     DisplayString(dst->dpy), dst->name));
				goto err;
			}

			DBG(("%s: enabling output '%s' (%d,%d)x(%d,%d), rotation %d, on CRTC:%ld, using mode %ld\n",
			     DisplayString(dst->dpy), dst->name,
			     dst->x, dst->y, dst->mode.width, dst->mode.height,
			     dst->rotation, (long)rr_crtc, dst->mode.id));

			ret = XRRSetPanning(dst->dpy, res, rr_crtc, memset(&panning, 0, sizeof(panning)));
			DBG(("%s-%s: XRRSetPanning %s\n", DisplayString(dst->dpy), dst->name, ret ? "failed" : "success"));
			(void)ret;

			ret = XRRSetCrtcConfig(dst->dpy, res, rr_crtc, CurrentTime,
					       dst->x, dst->y, dst->mode.id, dst->rotation,
					       &dst->rr_output, 1);
			DBG(("%s-%s: XRRSetCrtcConfig %s\n", DisplayString(dst->dpy), dst->name, ret ? "failed" : "success"));
			if (ret)
				goto err;

			if (EXTRA_DBG) {
				XRRCrtcInfo *c;
				XRRPanning *p;

				c = XRRGetCrtcInfo(dst->dpy, res, rr_crtc);
				if (c) {
					DBG(("%s-%s: x=%d, y=%d, rotation=%d, mode=%ld\n",
					     DisplayString(dst->dpy), dst->name,
					     c->x, c->y, c->rotation, c->mode));
					XRRFreeCrtcInfo(c);
				}

				p = XRRGetPanning(dst->dpy, res, rr_crtc);
				if (p) {
					DBG(("%s-%s: panning (%d, %d)x(%d, %d), tracking (%d, %d)x(%d, %d), border (%d, %d),(%d, %d)\n",
					     DisplayString(dst->dpy), dst->name,
					     p->left, p->top, p->width, p->height,
					     p->track_left, p->track_top, p->track_width, p->track_height,
					     p->border_left, p->border_top, p->border_right, p->border_bottom));
					XRRFreePanning(p);
				}
			}

			dst->rr_crtc = rr_crtc;
		}
free_res:
		XRRFreeScreenResources(res);
ungrab:
		XUngrabServer(display->dpy);
	}

	ctx->active = NULL;
	for (n = 0; n < ctx->nclone; n++) {
		struct clone *clone = &ctx->clones[n];

		if (clone->dst.rr_crtc == 0)
			continue;

		DBG(("%s-%s: added to active list\n",
		     DisplayString(dst->dpy), dst->name));

		clone->active = ctx->active;
		ctx->active = clone;
	}

	return 1;
}

static Cursor display_load_invisible_cursor(struct display *display)
{
	char zero[8] = {};
	XColor black = {};
	Pixmap bitmap = XCreateBitmapFromData(display->dpy, display->root, zero, 8, 8);
	return XCreatePixmapCursor(display->dpy, bitmap, bitmap, &black, &black, 0, 0);
}

static Cursor display_get_visible_cursor(struct display *display)
{
	if (display->cursor_serial != display->cursor_image.size) {
		DBG(("%s updating cursor\n", DisplayString(display->dpy)));

		if (display->visible_cursor)
			XFreeCursor(display->dpy, display->visible_cursor);

		display->visible_cursor = XcursorImageLoadCursor(display->dpy, &display->cursor_image);
		display->cursor_serial = display->cursor_image.size;
	}

	return display->visible_cursor;
}

static void display_load_visible_cursor(struct display *display, XFixesCursorImage *cur)
{
	unsigned long *src; /* XXX deep sigh */
	XcursorPixel *dst;
	unsigned n;

	if (cur->width != display->cursor_image.width ||
	    cur->height != display->cursor_image.height)
		display->cursor_image.pixels = realloc(display->cursor_image.pixels,
						       4 * cur->width * cur->height);
	if (display->cursor_image.pixels == NULL)
		return;

	display->cursor_image.width  = cur->width;
	display->cursor_image.height = cur->height;
	display->cursor_image.xhot = cur->xhot;
	display->cursor_image.yhot = cur->yhot;
	display->cursor_image.size++;

	n = cur->width*cur->height;
	src = cur->pixels;
	dst = display->cursor_image.pixels;
	while (n--)
		*dst++ = *src++;

	DBG(("%s marking cursor changed\n", DisplayString(display->dpy)));
	display->cursor_moved++;
	if (display->cursor != display->invisible_cursor) {
		display->cursor_visible++;
		context_enable_timer(display->ctx);
	}
}

static void display_cursor_move(struct display *display, int x, int y, int visible)
{
	DBG(("%s cursor moved (visible=%d, (%d, %d))\n",
	     DisplayString(display->dpy), visible, x, y));
	display->cursor_moved++;
	display->cursor_visible += visible;
	if (visible) {
		display->cursor_x = x;
		display->cursor_y = y;
	}

	context_enable_timer(display->ctx);
}

static void display_flush_cursor(struct display *display)
{
	Cursor cursor;
	int x, y;

	if (!display->cursor_moved)
		return;

	if (display->cursor_visible) {
		x = display->cursor_x;
		y = display->cursor_y;
	} else {
		x = display->cursor_x++ & 31;
		y = display->cursor_y++ & 31;
	}

	DBG(("%s setting cursor position (%d, %d), visible? %d\n",
	     DisplayString(display->dpy), x, y, display->cursor_visible));
	XWarpPointer(display->dpy, None, display->root, 0, 0, 0, 0, x, y);

	cursor = None;
	if (display->cursor_visible)
		cursor = display_get_visible_cursor(display);
	if (cursor == None)
		cursor = display->invisible_cursor;
	if (cursor != display->cursor) {
		XDefineCursor(display->dpy, display->root, cursor);
		display->cursor = cursor;
	}

	display_mark_flush(display);

	display->cursor_moved = 0;
	display->cursor_visible = 0;
}

static void clone_move_cursor(struct clone *c, int x, int y)
{
	int visible;

	DBG(("%s-%s moving cursor (%d, %d) [(%d, %d), (%d, %d)]\n",
	     DisplayString(c->dst.dpy), c->dst.name,
	     x, y,
	     c->src.x, c->src.y,
	     c->src.x + c->width, c->src.y + c->height));

	visible = (x >= c->src.x && x < c->src.x + c->width &&
		   y >= c->src.y && y < c->src.y + c->height);

	x += c->dst.x - c->src.x;
	y += c->dst.y - c->src.y;

	display_cursor_move(c->dst.display, x, y, visible);
}

static int clone_output_init(struct clone *clone, struct output *output,
			     struct display *display, const char *name,
			     RROutput rr_output)
{
	Display *dpy = display->dpy;
	int depth;

	DBG(("%s(%s, %s)\n", __func__, DisplayString(dpy), name));

	output->name = strdup(name);
	if (output->name == NULL)
		return -ENOMEM;

	output->display = display;
	output->dpy = dpy;

	output->rr_output = rr_output;
	output->rotation = RR_Rotate_0;

	output->window = display->root;
	output->use_shm = display->has_shm;
	output->use_shm_pixmap = display->has_shm_pixmap;

	DBG(("%s-%s use shm? %d (use shm pixmap? %d)\n",
	     DisplayString(dpy), name, display->has_shm, display->has_shm_pixmap));

	depth = output->use_shm ? display->depth : 16;
	if (depth < clone->depth)
		clone->depth = depth;

	return 0;
}

static void ximage_prepare(XImage *image, int width, int height)
{
	image->width = width;
	image->height = height;
	image->bytes_per_line = stride_for_depth(width, image->depth);
}

static void get_src(struct clone *c, const XRectangle *clip)
{
	DBG(("%s-%s get_src(%d,%d)x(%d,%d)\n", DisplayString(c->dst.dpy), c->dst.name,
	     clip->x, clip->y, clip->width, clip->height));

	c->image.obdata = (char *)&c->src.shm;

	if (c->src.use_render) {
		XRenderComposite(c->src.dpy, PictOpSrc,
				 c->src.win_picture, 0, c->src.pix_picture,
				 clip->x, clip->y,
				 0, 0,
				 0, 0,
				 clip->width, clip->height);
		if (c->src.use_shm_pixmap) {
			XSync(c->src.dpy, False);
		} else if (c->src.use_shm) {
			ximage_prepare(&c->image, clip->width, clip->height);
			XShmGetImage(c->src.dpy, c->src.pixmap, &c->image,
				     clip->x, clip->y, AllPlanes);
		} else {
			ximage_prepare(&c->image, c->width, c->height);
			XGetSubImage(c->src.dpy, c->src.pixmap,
				     clip->x, clip->y, clip->width, clip->height,
				     AllPlanes, ZPixmap,
				     &c->image, 0, 0);
		}
	} else if (c->src.pixmap) {
		XCopyArea(c->src.dpy, c->src.window, c->src.pixmap, c->src.gc,
			  clip->x, clip->y,
			  clip->width, clip->height,
			  0, 0);
		XSync(c->src.dpy, False);
	} else if (c->src.use_shm) {
		ximage_prepare(&c->image, clip->width, clip->height);
		XShmGetImage(c->src.dpy, c->src.window, &c->image,
			     clip->x, clip->y, AllPlanes);
	} else {
		ximage_prepare(&c->image, c->width, c->height);
		XGetSubImage(c->src.dpy, c->src.window,
			     clip->x, clip->y, clip->width, clip->height,
			     AllPlanes, ZPixmap,
			     &c->image, 0, 0);
	}
	c->src.display->flush = 0;
}

static void put_dst(struct clone *c, const XRectangle *clip)
{
	DBG(("%s-%s put_dst(%d,%d)x(%d,%d)\n", DisplayString(c->dst.dpy), c->dst.name,
	     clip->x, clip->y, clip->width, clip->height));

	c->image.obdata = (char *)&c->dst.shm;

	if (c->dst.use_render) {
		if (c->dst.use_shm_pixmap) {
			DBG(("%s-%s using SHM pixmap composite\n",
			     DisplayString(c->dst.dpy), c->dst.name));
		} else if (c->dst.use_shm) {
			DBG(("%s-%s using SHM image composite\n",
			     DisplayString(c->dst.dpy), c->dst.name));
			XShmPutImage(c->dst.dpy, c->dst.pixmap, c->dst.gc, &c->image,
				     0, 0,
				     0, 0,
				     clip->width, clip->height,
				     False);
		} else {
			DBG(("%s-%s using composite\n",
			     DisplayString(c->dst.dpy), c->dst.name));
			XPutImage(c->dst.dpy, c->dst.pixmap, c->dst.gc, &c->image,
				  0, 0,
				  0, 0,
				  clip->width, clip->height);
		}
		if (c->dst.use_shm)
			c->dst.serial = NextRequest(c->dst.dpy);
		XRenderComposite(c->dst.dpy, PictOpSrc,
				 c->dst.pix_picture, 0, c->dst.win_picture,
				 0, 0,
				 0, 0,
				 clip->x, clip->y,
				 clip->width, clip->height);
		c->dst.display->send |= c->dst.use_shm;
	} else if (c->dst.pixmap) {
		DBG(("%s-%s using SHM pixmap\n",
		     DisplayString(c->dst.dpy), c->dst.name));
		c->dst.serial = NextRequest(c->dst.dpy);
		XCopyArea(c->dst.dpy, c->dst.pixmap, c->dst.window, c->dst.gc,
			  0, 0,
			  clip->width, clip->height,
			  clip->x, clip->y);
		c->dst.display->send = 1;
	} else if (c->dst.use_shm) {
		DBG(("%s-%s using SHM image\n",
		     DisplayString(c->dst.dpy), c->dst.name));
		c->dst.serial = NextRequest(c->dst.dpy);
		XShmPutImage(c->dst.dpy, c->dst.window, c->dst.gc, &c->image,
			     0, 0,
			     clip->x, clip->y,
			     clip->width, clip->height,
			     True);
	} else {
		DBG(("%s-%s using image\n",
		     DisplayString(c->dst.dpy), c->dst.name));
		XPutImage(c->dst.dpy, c->dst.window, c->dst.gc, &c->image,
			  0, 0,
			  clip->x, clip->y,
			  clip->width, clip->height);
		c->dst.serial = 0;
	}

	display_mark_flush(c->dst.display);
}

static int clone_paint(struct clone *c)
{
	XRectangle clip;

	DBG(("%s-%s paint clone, damaged (%d, %d), (%d, %d) [(%d, %d), (%d,  %d)]\n",
	     DisplayString(c->dst.dpy), c->dst.name,
	     c->damaged.x1, c->damaged.y1,
	     c->damaged.x2, c->damaged.y2,
	     c->src.x, c->src.y,
	     c->src.x + c->width, c->src.y + c->height));

	if (c->damaged.x1 < c->src.x)
		c->damaged.x1 = c->src.x;
	if (c->damaged.x2 > c->src.x + c->width)
		c->damaged.x2 = c->src.x + c->width;
	if (c->damaged.x2 <= c->damaged.x1)
		goto done;

	if (c->damaged.y1 < c->src.y)
		c->damaged.y1 = c->src.y;
	if (c->damaged.y2 > c->src.y + c->height)
		c->damaged.y2 = c->src.y + c->height;
	if (c->damaged.y2 <= c->damaged.y1)
		goto done;

	DBG(("%s-%s is damaged, last SHM serial: %ld, now %ld\n",
	     DisplayString(c->dst.dpy), c->dst.name,
	     (long)c->dst.serial, (long)LastKnownRequestProcessed(c->dst.dpy)));
	if (c->dst.serial > LastKnownRequestProcessed(c->dst.dpy)) {
		struct pollfd pfd;

		pfd.fd = ConnectionNumber(c->dst.dpy);
		pfd.events = POLLIN;
		XEventsQueued(c->dst.dpy,
			      poll(&pfd, 1, 0) ? QueuedAfterReading : QueuedAfterFlush);

		if (c->dst.serial > LastKnownRequestProcessed(c->dst.dpy)) {
			c->dst.display->skip_clone++;
			return EAGAIN;
		}
	}

	c->dst.display->skip_clone = 0;
	c->dst.display->skip_frame = 0;

	if (FORCE_FULL_REDRAW) {
		c->damaged.x1 = c->src.x;
		c->damaged.y1 = c->src.y;
		c->damaged.x2 = c->src.x + c->width;
		c->damaged.y2 = c->src.y + c->height;
	}

	clip.x = c->damaged.x1;
	clip.y = c->damaged.y1;
	clip.width  = c->damaged.x2 - c->damaged.x1;
	clip.height = c->damaged.y2 - c->damaged.y1;
	get_src(c, &clip);

	clip.x += c->dst.x - c->src.x;
	clip.y += c->dst.y - c->src.y;
	put_dst(c, &clip);

done:
	c->damaged.x2 = c->damaged.y2 = INT_MIN;
	c->damaged.x1 = c->damaged.y1 = INT_MAX;
	return 0;
}

static void clone_damage(struct clone *c, const XRectangle *rec)
{
	if (rec->x < c->damaged.x1)
		c->damaged.x1 = rec->x;
	if (rec->width > c->damaged.x2 - rec->x)
		c->damaged.x2 = (int)rec->x + rec->width;
	if (rec->y < c->damaged.y1)
		c->damaged.y1 = rec->y;
	if (rec->height > c->damaged.y2 - rec->y)
		c->damaged.y2 = (int)rec->y + rec->height;

	DBG(("%s-%s damaged: (%d, %d), (%d, %d)\n",
	     DisplayString(c->dst->dpy), c->dst->name,
	     c->damaged.x1, c->damaged.y1,
	     c->damaged.x2, c->damaged.y2));
}

static void usage(const char *arg0)
{
	printf("usage: %s [-d <source display>] [-b] [<target display>]...\n", arg0);
}

static void record_callback(XPointer closure, XRecordInterceptData *data)
{
	struct context *ctx = (struct context *)closure;

	DBG(("%s\n", __func__));

	if (data->category == XRecordFromServer) {
		const xEvent *e = (const xEvent *)data->data;

		DBG(("%s -- from server, event type %d, root %ld (ours? %d)\n",
		     __func__, e->u.u.type, (long)e->u.keyButtonPointer.root,
		     ctx->display->root == e->u.keyButtonPointer.root));

		if (e->u.u.type == MotionNotify &&
		    e->u.keyButtonPointer.root == ctx->display->root) {
			struct clone *clone;

			for (clone = ctx->active; clone; clone = clone->active)
				clone_move_cursor(clone,
						  e->u.keyButtonPointer.rootX,
						  e->u.keyButtonPointer.rootY);
		}
	}

	XRecordFreeData(data);
}

static int record_mouse(struct context *ctx)
{
	Display *dpy;
	XRecordRange *rr;
	XRecordClientSpec rcs;
	XRecordContext rc;

	DBG(("%s(%s)\n", __func__, DisplayString(ctx->display->dpy)));

	dpy = XOpenDisplay(DisplayString(ctx->display->dpy));
	if (dpy == NULL)
		return -ECONNREFUSED;

	rr = XRecordAllocRange();
	if (rr == NULL)
		return -ENOMEM;

	rr->device_events.first = rr->device_events.last = MotionNotify;

	rcs = XRecordAllClients;
	rc = XRecordCreateContext(dpy, 0, &rcs, 1, &rr, 1);

	XSync(dpy, False);

	if (!XRecordEnableContextAsync(dpy, rc, record_callback, (XPointer)ctx))
		return -EINVAL;

	ctx->record = dpy;
	return ConnectionNumber(dpy);
}

static int bad_visual(Visual *visual, int depth)
{
	DBG(("%s? depth=%d, visual: class=%d, bits_per_rgb=%d, red_mask=%08lx, green_mask=%08lx, blue_mask=%08lx\n",
	     __func__, depth,
	     visual->class,
	     visual->bits_per_rgb,
	     visual->red_mask,
	     visual->green_mask,
	     visual->blue_mask));

	if (!(visual->class == TrueColor || visual->class == DirectColor))
		return 1;

	switch (depth) {
	case 16: return (/* visual->bits_per_rgb != 6          || */
			 visual->red_mask     != 0x1f << 11 ||
			 visual->green_mask   != 0x3f << 5  ||
			 visual->blue_mask    != 0x1f << 0);

	case 24: return (/* visual->bits_per_rgb != 8          || */
			 visual->red_mask     != 0xff << 16 ||
			 visual->green_mask   != 0xff << 8  ||
			 visual->blue_mask    != 0xff << 0);

	default: return 1;
	}
}

static XRenderPictFormat *
find_xrender_format(Display *dpy, pixman_format_code_t format)
{
    XRenderPictFormat tmpl;
    int mask;

#define MASK(x) ((1<<(x))-1)

    memset(&tmpl, 0, sizeof(tmpl));

    tmpl.depth = PIXMAN_FORMAT_DEPTH(format);
    mask = PictFormatType | PictFormatDepth;

    DBG(("%s(0x%08lx)\n", __func__, (long)format));

    switch (PIXMAN_FORMAT_TYPE(format)) {
    case PIXMAN_TYPE_ARGB:
	tmpl.type = PictTypeDirect;

	if (PIXMAN_FORMAT_A(format)) {
		tmpl.direct.alphaMask = MASK(PIXMAN_FORMAT_A(format));
		tmpl.direct.alpha = (PIXMAN_FORMAT_R(format) +
				     PIXMAN_FORMAT_G(format) +
				     PIXMAN_FORMAT_B(format));
	}

	tmpl.direct.redMask = MASK(PIXMAN_FORMAT_R(format));
	tmpl.direct.red = (PIXMAN_FORMAT_G(format) +
			   PIXMAN_FORMAT_B(format));

	tmpl.direct.greenMask = MASK(PIXMAN_FORMAT_G(format));
	tmpl.direct.green = PIXMAN_FORMAT_B(format);

	tmpl.direct.blueMask = MASK(PIXMAN_FORMAT_B(format));
	tmpl.direct.blue = 0;

	mask |= PictFormatRed | PictFormatRedMask;
	mask |= PictFormatGreen | PictFormatGreenMask;
	mask |= PictFormatBlue | PictFormatBlueMask;
	mask |= PictFormatAlpha | PictFormatAlphaMask;
	break;

    case PIXMAN_TYPE_ABGR:
	tmpl.type = PictTypeDirect;

	if (tmpl.direct.alphaMask) {
		tmpl.direct.alphaMask = MASK(PIXMAN_FORMAT_A(format));
		tmpl.direct.alpha = (PIXMAN_FORMAT_B(format) +
				     PIXMAN_FORMAT_G(format) +
				     PIXMAN_FORMAT_R(format));
	}

	tmpl.direct.blueMask = MASK(PIXMAN_FORMAT_B(format));
	tmpl.direct.blue = (PIXMAN_FORMAT_G(format) +
			    PIXMAN_FORMAT_R(format));

	tmpl.direct.greenMask = MASK(PIXMAN_FORMAT_G(format));
	tmpl.direct.green = PIXMAN_FORMAT_R(format);

	tmpl.direct.redMask = MASK(PIXMAN_FORMAT_R(format));
	tmpl.direct.red = 0;

	mask |= PictFormatRed | PictFormatRedMask;
	mask |= PictFormatGreen | PictFormatGreenMask;
	mask |= PictFormatBlue | PictFormatBlueMask;
	mask |= PictFormatAlpha | PictFormatAlphaMask;
	break;

    case PIXMAN_TYPE_BGRA:
	tmpl.type = PictTypeDirect;

	tmpl.direct.blueMask = MASK(PIXMAN_FORMAT_B(format));
	tmpl.direct.blue = (PIXMAN_FORMAT_BPP(format) - PIXMAN_FORMAT_B(format));

	tmpl.direct.greenMask = MASK(PIXMAN_FORMAT_G(format));
	tmpl.direct.green = (PIXMAN_FORMAT_BPP(format) - PIXMAN_FORMAT_B(format) -
			     PIXMAN_FORMAT_G(format));

	tmpl.direct.redMask = MASK(PIXMAN_FORMAT_R(format));
	tmpl.direct.red = (PIXMAN_FORMAT_BPP(format) - PIXMAN_FORMAT_B(format) -
			   PIXMAN_FORMAT_G(format) - PIXMAN_FORMAT_R(format));

	if (tmpl.direct.alphaMask) {
		tmpl.direct.alphaMask = MASK(PIXMAN_FORMAT_A(format));
		tmpl.direct.alpha = 0;
	}

	mask |= PictFormatRed | PictFormatRedMask;
	mask |= PictFormatGreen | PictFormatGreenMask;
	mask |= PictFormatBlue | PictFormatBlueMask;
	mask |= PictFormatAlpha | PictFormatAlphaMask;
	break;

    case PIXMAN_TYPE_A:
	tmpl.type = PictTypeDirect;

	tmpl.direct.alpha = 0;
	tmpl.direct.alphaMask = MASK(PIXMAN_FORMAT_A(format));

	mask |= PictFormatAlpha | PictFormatAlphaMask;
	break;

    case PIXMAN_TYPE_COLOR:
    case PIXMAN_TYPE_GRAY:
	/* XXX Find matching visual/colormap */
	tmpl.type = PictTypeIndexed;
	//tmpl.colormap = screen->visuals[PIXMAN_FORMAT_VIS(format)].vid;
	//mask |= PictFormatColormap;
	return NULL;
    }
#undef MASK

    return XRenderFindFormat(dpy, mask, &tmpl, 0);
}

static int display_init_render(struct display *display, int depth, XRenderPictFormat **use_render)
{
	Display *dpy = display->dpy;
	int major, minor;

	DBG(("%s is depth %d, want %d\n", DisplayString(dpy), display->depth, depth));

	*use_render = 0;
	if (depth == display->depth && !bad_visual(display->visual, depth))
		return 0;

	if (display->root_format == 0) {
		if (!XRenderQueryVersion(dpy, &major, &minor)) {
			fprintf(stderr, "Render extension not supported by %s\n", DisplayString(dpy));
			return -EINVAL;
		}

		display->root_format = XRenderFindVisualFormat(dpy, display->visual);
		display->rgb16_format = find_xrender_format(dpy, PIXMAN_r5g6b5);
		display->rgb24_format = XRenderFindStandardFormat(dpy, PictStandardRGB24);

		DBG(("%s: root format=%lx, rgb16 format=%lx, rgb24 format=%lx\n",
		     DisplayString(dpy),
		     (long)display->root_format,
		     (long)display->rgb16_format,
		     (long)display->rgb24_format));
	}

	switch (depth) {
	case 16: *use_render = display->rgb16_format; break;
	case 24: *use_render = display->rgb24_format; break;
	}
	if (*use_render == 0)
		return -ENOENT;

	return 0;
}

static int clone_init_depth(struct clone *clone)
{
	int ret, depth;

	DBG(("%s-%s wants depth %d\n",
	     DisplayString(clone->dst.dpy), clone->dst.name, clone->depth));

	ret = -1;
	for (depth = clone->depth; depth <= 24; depth += 8) {
		ret = display_init_render(clone->src.display, depth, &clone->src.use_render);
		if (ret)
			continue;

		ret = display_init_render(clone->dst.display, depth, &clone->dst.use_render);
		if (ret)
			continue;

		break;
	}
	if (ret)
		return ret;

	DBG(("%s-%s using depth %d, requires xrender for src? %d, for dst? %d\n",
	     DisplayString(clone->dst.dpy), clone->dst.name,
	     clone->depth,
	     clone->src.use_render != NULL,
	     clone->dst.use_render != NULL));

	return 0;
}

static int add_display(struct context *ctx, Display *dpy)
{
	struct display *display;
	int first_display = ctx->ndisplay == 0;

	if (is_power_of_2(ctx->ndisplay)) {
		struct display *new_display;

		new_display = realloc(ctx->display, 2*ctx->ndisplay*sizeof(struct display));
		if (new_display == NULL)
			return -ENOMEM;

		if (new_display != ctx->display) {
			int n;

			for (n = 0; n < ctx->nclone; n++) {
				struct clone *clone = &ctx->clones[n];
				clone->src.display = new_display + (clone->src.display - ctx->display);
				clone->dst.display = new_display + (clone->dst.display - ctx->display);
			}
		}

		ctx->display = new_display;
	}

	display = memset(&ctx->display[ctx->ndisplay++], 0, sizeof(struct display));

	display->dpy = dpy;
	display->ctx = ctx;

	display->root = DefaultRootWindow(dpy);
	display->depth = DefaultDepth(dpy, DefaultScreen(dpy));
	display->visual = DefaultVisual(dpy, DefaultScreen(dpy));

	display->has_shm = can_use_shm(dpy, display->root,
				       &display->shm_event,
				       &display->shm_opcode,
				       &display->has_shm_pixmap);

	display->rr_active = XRRQueryExtension(dpy, &display->rr_event, &display->rr_error);
	if (XineramaQueryExtension(dpy, &display->xinerama_event, &display->xinerama_error))
		display->xinerama_active = XineramaIsActive(dpy);

	/* first display (source) is slightly special */
	if (!first_display) {
		display->invisible_cursor = display_load_invisible_cursor(display);
		display_cursor_move(display, 0, 0, 0);
	}

	return ConnectionNumber(dpy);
}

static int display_open(struct context *ctx, const char *name)
{
	Display *dpy;
	int n;

	DBG(("%s(%s)\n", __func__, name));

	dpy = XOpenDisplay(name);
	if (dpy == NULL)
		return -ECONNREFUSED;

	/* Prevent cloning the same display twice */
	for (n = 0; n < ctx->ndisplay; n++) {
		if (strcmp(DisplayString(dpy), DisplayString(ctx->display[n].dpy)) == 0) {
			DBG(("%s %s is already connected\n", __func__, name));
			XCloseDisplay(dpy);
			return -EBUSY;
		}
	}

	return add_display(ctx, dpy);
}

static int bumblebee_open(struct context *ctx)
{
	char buf[256];
	struct sockaddr_un addr;
	int fd, len;

	fd = socket(PF_UNIX, SOCK_STREAM, 0);
	if (fd < 0) {
		DBG(("%s unable to create a socket: %d\n", __func__, errno));
		return -ECONNREFUSED;
	}

	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, optarg && *optarg ? optarg : "/var/run/bumblebee.socket");
	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
		DBG(("%s unable to create a socket: %d\n", __func__, errno));
		goto err;
	}

	/* Ask bumblebee to start the second server */
	buf[0] = 'C';
	if (send(fd, &buf, 1, 0) != 1 || (len = recv(fd, &buf, 255, 0)) <= 0) {
		DBG(("%s startup send/recv failed: %d\n", __func__, errno));
		goto err;
	}
	buf[len] = '\0';

	/* Query the display name */
	strcpy(buf, "Q VirtualDisplay");
	if (send(fd, buf, 17, 0) != 17 || (len = recv(fd, buf, 255, 0)) <= 0) {
		DBG(("%s query send/recv failed: %d\n", __func__, errno));
		goto err;
	}
	buf[len] = '\0';

	DBG(("%s query result '%s'\n", __func__, buf));

	if (strncmp(buf, "Value: ", 7))
		goto err;

	len = 7;
	while (buf[len] != '\n' && buf[len] != '\0')
		len++;
	buf[len] = '\0';

	/* XXX We must keep the control socket open whilst we want to keep
	 * the display around.
	 *
	 * So what we need to do is listen for new bumblee Xservers and
	 * bind only for their duration.
	 */

	return display_open(ctx, buf+7);

err:
	close(fd);
	return -ECONNREFUSED;
}

static int display_init_damage(struct display *display)
{
	DBG(("%s(%s)\n", __func__, DisplayString(display->dpy)));

	if (!XDamageQueryExtension(display->dpy, &display->damage_event, &display->damage_error) ||
	    !XFixesQueryExtension(display->dpy, &display->xfixes_event, &display->xfixes_error)) {
		fprintf(stderr, "Damage/Fixes extension not supported by %s\n", DisplayString(display->dpy));
		return EINVAL;
	}

	display->damage = XDamageCreate(display->dpy, display->root, XDamageReportBoundingBox);
	if (display->damage == 0)
		return EACCES;

	return 0;
}

static void display_reset_damage(struct display *display)
{
	Damage damage;

	damage = XDamageCreate(display->dpy, display->root, XDamageReportBoundingBox);
	if (damage) {
		XDamageDestroy(display->dpy, display->damage);
		display->damage = damage;
		XFlush(display->dpy);
		display->flush = 0;
	}
}

static void display_init_randr_hpd(struct display *display)
{
	int major, minor;

	DBG(("%s(%s)\n", __func__, DisplayString(display->dpy)));

	if (!XRRQueryVersion(display->dpy, &major, &minor))
		return;

	DBG(("%s - randr version %d.%d\n", DisplayString(display->dpy), major, minor));
	if (major > 1 || (major == 1 && minor >= 2))
		XRRSelectInput(display->dpy, display->root, RROutputChangeNotifyMask);
}

static void rebuild_clones(struct context *ctx, struct clone *new_clones)
{
	int n, m;

	for (n = 1; n < ctx->ndisplay; n++) {
		struct display *d = &ctx->display[n];

		d->clone = NULL;
		for (m = 0; m < ctx->nclone; m++) {
			struct clone *c = &new_clones[m];

			if (c->dst.display != d)
				continue;

			c->next = d->clone;
			d->clone = c;
		}
	}

	ctx->clones = new_clones;
}

static struct clone *add_clone(struct context *ctx)
{
	if (is_power_of_2(ctx->nclone)) {
		struct clone *new_clones;

		new_clones = realloc(ctx->clones, 2*ctx->nclone*sizeof(struct clone));
		if (new_clones == NULL)
			return NULL;

		if (new_clones != ctx->clones)
			rebuild_clones(ctx, new_clones);
	}

	return memset(&ctx->clones[ctx->nclone++], 0, sizeof(struct clone));
}

static struct display *last_display(struct context *ctx)
{
	return &ctx->display[ctx->ndisplay-1];
}

static void reverse_clone_list(struct display *display)
{
	struct clone *list = NULL;

	while (display->clone) {
		struct clone *clone = display->clone;
		display->clone = clone->next;
		clone->next = list;
		list = clone;
	}

	display->clone = list;
}

static int last_display_add_clones__randr(struct context *ctx)
{
	struct display *display = last_display(ctx);
	XRRScreenResources *res;
	char buf[80];
	int i, ret;

	DBG(("%s(%s)\n", __func__, DisplayString(display->dpy)));

	display_init_randr_hpd(display);

	/* Force a probe of outputs on initial connection */
	res = XRRGetScreenResources(display->dpy, display->root);
	if (res == NULL)
		return -ENOMEM;

	DBG(("%s - noutputs=%d\n", DisplayString(display->dpy), res->noutput));
	for (i = 0; i < res->noutput; i++) {
		XRROutputInfo *o = XRRGetOutputInfo(display->dpy, res, res->outputs[i]);
		struct clone *clone = add_clone(ctx);
		RROutput id;

		if (clone == NULL)
			return -ENOMEM;

		clone->depth = 24;
		clone->next = display->clone;
		display->clone = clone;

		id = claim_virtual(ctx->display, buf, ctx->nclone);
		if (id == 0) {
			fprintf(stderr, "Failed to find available VirtualHead \"%s\" for \"%s\" on display \"%s\"\n",
				buf, o->name, DisplayString(display->dpy));
			return -ENOSPC;
		}

		ret = clone_output_init(clone, &clone->src, ctx->display, buf, id);
		if (ret) {
			fprintf(stderr, "Failed to add output \"%s\" on display \"%s\"\n",
				buf, DisplayString(ctx->display->dpy));
			return ret;
		}

		ret = clone_output_init(clone, &clone->dst, display, o->name, res->outputs[i]);
		if (ret) {
			fprintf(stderr, "Failed to add output \"%s\" on display \"%s\"\n",
				o->name, DisplayString(display->dpy));
			return ret;
		}

		ret = clone_init_depth(clone);
		if (ret) {
			fprintf(stderr, "Failed to negotiate image format for display \"%s\"\n",
				DisplayString(display->dpy));
			return ret;
		}

		ret = clone_update_modes__randr(clone);
		if (ret) {
			fprintf(stderr, "Failed to clone output \"%s\" from display \"%s\"\n",
				o->name, DisplayString(display->dpy));
			return ret;
		}


		if (o->crtc) {
			DBG(("%s - disabling active output\n", DisplayString(display->dpy)));
			disable_crtc(display->dpy, res, o->crtc);
		}

		XRRFreeOutputInfo(o);
	}
	XRRFreeScreenResources(res);

	reverse_clone_list(display);
	return 0;
}

static int last_display_add_clones__xinerama(struct context *ctx)
{
	struct display *display = last_display(ctx);
	Display *dpy = display->dpy;
	XineramaScreenInfo *xi;
	char buf[80];
	int n, count, ret;

	DBG(("%s(%s)\n", __func__, DisplayString(display->dpy)));

	count = 0;
	xi = XineramaQueryScreens(dpy, &count);
	for (n = 0; n < count; n++) {
		struct clone *clone = add_clone(ctx);
		RROutput id;

		if (clone == NULL)
			return -ENOMEM;

		if (xi[n].width == 0 || xi[n].height == 0)
			continue;

		clone->depth = 24;
		clone->next = display->clone;
		display->clone = clone;

		id = claim_virtual(ctx->display, buf, ctx->nclone);
		if (id == 0) {
			fprintf(stderr, "Failed to find available VirtualHead \"%s\" for Xinerama screen %d on display \"%s\"\n",
				buf, n, DisplayString(dpy));
		}
		ret = clone_output_init(clone, &clone->src, ctx->display, buf, id);
		if (ret) {
			fprintf(stderr, "Failed to add Xinerama screen %d on display \"%s\"\n",
				n, DisplayString(ctx->display->dpy));
			return ret;
		}

		sprintf(buf, "XINERAMA%d", n);
		ret = clone_output_init(clone, &clone->dst, display, buf, 0);
		if (ret) {
			fprintf(stderr, "Failed to add Xinerama screen %d on display \"%s\"\n",
				n, DisplayString(dpy));
			return ret;
		}

		ret = clone_init_depth(clone);
		if (ret) {
			fprintf(stderr, "Failed to negotiate image format for display \"%s\"\n",
				DisplayString(display->dpy));
			return ret;
		}

		/* Replace the modes on the local VIRTUAL output with the remote Screen */
		clone->width = xi[n].width;
		clone->height = xi[n].height;
		clone->dst.x = xi[n].x_org;
		clone->dst.y = xi[n].y_org;
		clone->dst.rr_crtc = -1;
		ret = clone_update_modes__fixed(clone);
		if (ret) {
			fprintf(stderr, "Failed to clone Xinerama screen %d from display \"%s\"\n",
				n, DisplayString(display->dpy));
			return ret;
		}

		clone->active = ctx->active;
		ctx->active = clone;
	}
	XFree(xi);

	reverse_clone_list(display);
	return 0;
}

static int last_display_add_clones__display(struct context *ctx)
{
	struct display *display = last_display(ctx);
	Display *dpy = display->dpy;
	struct clone *clone;
	Screen *scr;
	char buf[80];
	int ret;
	RROutput id;


	DBG(("%s(%s)\n", __func__, DisplayString(dpy)));
	clone = add_clone(ctx);
	if (clone == NULL)
		return -ENOMEM;

	clone->depth = 24;
	clone->next = display->clone;
	display->clone = clone;

	id = claim_virtual(ctx->display, buf, ctx->nclone);
	if (id == 0) {
		fprintf(stderr, "Failed to find available VirtualHead \"%s\" for on display \"%s\"\n",
			buf, DisplayString(dpy));
	}
	ret = clone_output_init(clone, &clone->src, ctx->display, buf, id);
	if (ret) {
		fprintf(stderr, "Failed to add display \"%s\"\n",
			DisplayString(ctx->display->dpy));
		return ret;
	}

	sprintf(buf, "WHOLE");
	ret = clone_output_init(clone, &clone->dst, display, buf, 0);
	if (ret) {
		fprintf(stderr, "Failed to add display \"%s\"\n",
			DisplayString(dpy));
		return ret;
	}

	ret = clone_init_depth(clone);
	if (ret) {
		fprintf(stderr, "Failed to negotiate image format for display \"%s\"\n",
			DisplayString(dpy));
		return ret;
	}

	/* Replace the modes on the local VIRTUAL output with the remote Screen */
	scr = ScreenOfDisplay(dpy, DefaultScreen(dpy));
	clone->width = scr->width;
	clone->height = scr->height;
	clone->dst.x = 0;
	clone->dst.y = 0;
	clone->dst.rr_crtc = -1;
	ret = clone_update_modes__fixed(clone);
	if (ret) {
		fprintf(stderr, "Failed to clone display \"%s\"\n",
			DisplayString(dpy));
		return ret;
	}

	clone->active = ctx->active;
	ctx->active = clone;

	return 0;
}

static int last_display_add_clones(struct context *ctx)
{
	struct display *display = last_display(ctx);

	display->width = DisplayWidth(display->dpy, DefaultScreen(display->dpy));
	display->height = DisplayHeight(display->dpy, DefaultScreen(display->dpy));
	DBG(("%s - initial size %dx%d\n", DisplayString(display->dpy), display->width, display->height));

	if (display->rr_active)
		return last_display_add_clones__randr(ctx);

	if (display->xinerama_active)
		return last_display_add_clones__xinerama(ctx);

	return last_display_add_clones__display(ctx);
}

static int last_display_clone(struct context *ctx, int fd)
{
	fd = add_fd(ctx, fd);
	if (fd < 0)
		return fd;

	fd = last_display_add_clones(ctx);
	if (fd)
		return fd;

	return 0;
}

static int first_display_has_singleton(struct context *ctx)
{
	struct display *display = ctx->display;
	unsigned long nitems, bytes;
	unsigned char *prop;
	int format;
	Atom type;

	ctx->singleton = XInternAtom(display->dpy, "intel-virtual-output-singleton", False);

	XGetWindowProperty(display->dpy, display->root, ctx->singleton,
			   0, 0, 0, AnyPropertyType, &type, &format, &nitems, &bytes, &prop);
	DBG(("%s: singleton registered? %d\n", DisplayString(display->dpy), type != None));
	return type != None;
}

static int first_display_wait_for_ack(struct context *ctx, int timeout, int id)
{
	struct display *display = ctx->display;
	struct pollfd pfd;
	char expect[6]; /* "1234R\0" */

	sprintf(expect, "%04xR", id);
	DBG(("%s: wait for act '%c%c%c%c%c'\n",
	     DisplayString(display->dpy),
	     expect[0], expect[1], expect[2], expect[3], expect[4]));

	XFlush(display->dpy);

	pfd.fd = ConnectionNumber(display->dpy);
	pfd.events = POLLIN;
	do {
		if (poll(&pfd, 1, timeout) <= 0)
			return -ETIME;

		while (XPending(display->dpy)) {
			XEvent e;
			XClientMessageEvent *cme;

			XNextEvent(display->dpy, &e);
			DBG(("%s: reading event type %d\n", DisplayString(display->dpy), e.type));

			if (e.type != ClientMessage)
				continue;

			cme = (XClientMessageEvent *)&e;
			if (cme->message_type != ctx->singleton)
				continue;
			if (cme->format != 8)
				continue;

			DBG(("%s: client message '%c%c%c%c%c'\n",
			     DisplayString(display->dpy),
			     cme->data.b[0],
			     cme->data.b[1],
			     cme->data.b[2],
			     cme->data.b[3],
			     cme->data.b[4]));
			if (memcmp(cme->data.b, expect, 5))
				continue;

			return -atoi(cme->data.b + 5);
		}
	} while (1);
}

#if defined(__GNUC__) && (__GNUC__ > 3)
__attribute__((format(gnu_printf, 3, 4)))
#endif
static int first_display_send_command(struct context *ctx, int timeout,
				      const char *format,
				      ...)
{
	struct display *display = ctx->display;
	char buf[1024], *b;
	int len, id;
	va_list va;

	id = rand() & 0xffff;
	sprintf(buf, "%04x", id);
	va_start(va, format);
	len = vsnprintf(buf+4, sizeof(buf)-4, format, va)+5;
	va_end(va);
	assert(len < sizeof(buf));

	DBG(("%s: send command '%s'\n", DisplayString(display->dpy), buf));

	b = buf;
	while (len) {
		XClientMessageEvent msg;
		int n = len;
		if (n > sizeof(msg.data.b))
			n = sizeof(msg.data.b);
		len -= n;

		msg.type = ClientMessage;
		msg.serial = 0;
		msg.message_type = ctx->singleton;
		msg.format = 8;
		memcpy(msg.data.b, b, n);
		b += n;

		XSendEvent(display->dpy, display->root, False, PropertyChangeMask, (XEvent *)&msg);
	}

	return first_display_wait_for_ack(ctx, timeout, id);
}

static void first_display_reply(struct context *ctx, int result)
{
	struct display *display = ctx->display;
	XClientMessageEvent msg;

	sprintf(msg.data.b, "%c%c%c%cR%d",
	     ctx->command[0],
	     ctx->command[1],
	     ctx->command[2],
	     ctx->command[3],
	     -result);

	DBG(("%s: send reply '%s'\n", DisplayString(display->dpy), msg.data.b));

	msg.type = ClientMessage;
	msg.serial = 0;
	msg.message_type = ctx->singleton;
	msg.format = 8;

	XSendEvent(display->dpy, display->root, False, PropertyChangeMask, (XEvent *)&msg);
	XFlush(display->dpy);
}

static void first_display_handle_command(struct context *ctx,
					 const char *msg)
{
	int len;

	DBG(("client message!\n"));

	for (len = 0; len < 20 && msg[len]; len++)
		;

	if (ctx->command_continuation + len > sizeof(ctx->command)) {
		ctx->command_continuation = 0;
		return;
	}

	memcpy(ctx->command + ctx->command_continuation, msg, len);
	ctx->command_continuation += len;

	if (len < 20) {
		ctx->command[ctx->command_continuation] = 0;
		DBG(("client command complete! '%s'\n", ctx->command));
		switch (ctx->command[4]) {
		case 'B':
			first_display_reply(ctx, last_display_clone(ctx, bumblebee_open(ctx)));
			break;
		case 'C':
			first_display_reply(ctx, last_display_clone(ctx, display_open(ctx, ctx->command + 5)));
			break;
		case 'P':
			first_display_reply(ctx, 0);
			break;
		case 'R':
			break;
		}
		ctx->command_continuation = 0;
		return;
	}
}

static int first_display_register_as_singleton(struct context *ctx)
{
	struct display *display = ctx->display;
	struct pollfd pfd;

	XChangeProperty(display->dpy, display->root, ctx->singleton,
			XA_STRING, 8, PropModeReplace, (unsigned char *)".", 1);
	XFlush(display->dpy);

	/* And eat the notify (presuming that it is ours!) */

	pfd.fd = ConnectionNumber(display->dpy);
	pfd.events = POLLIN;
	do {
		if (poll(&pfd, 1, 1000) <= 0) {
			fprintf(stderr, "Failed to register as singleton\n");
			return EBUSY;
		}

		while (XPending(display->dpy)) {
			XEvent e;

			XNextEvent(display->dpy, &e);
			DBG(("%s: reading event type %d\n", DisplayString(display->dpy), e.type));

			if (e.type == PropertyNotify &&
			    ((XPropertyEvent *)&e)->atom == ctx->singleton)
				return 0;
		}
	} while (1);
}

static void display_flush_send(struct display *display)
{
	XShmCompletionEvent e;

	if (!display->send)
		return;

	DBG(("%s flushing send (serial now %ld) (has shm send? %d)\n",
	     DisplayString(display->dpy),
	     (long)NextRequest(display->dpy),
	     display->shm_event));

	display->send = 0;

	if (display->shm_event == 0) {
		XSync(display->dpy, False);
		display->flush = 0;
		return;
	}

	memset(&e, 0, sizeof(e));
	e.type = display->shm_event;
	e.send_event = 1;
	e.drawable = display->root;
	e.major_code = display->shm_opcode;
	e.minor_code = X_ShmPutImage;

	XSendEvent(display->dpy, display->root, False, 0, (XEvent *)&e);
	display_mark_flush(display);
}

static void display_sync(struct display *display)
{
	if (display->skip_clone == 0)
		return;

	if (display->skip_frame++ < 2)
		return;

	DBG(("%s forcing sync\n", DisplayString(display->dpy)));
	XSync(display->dpy, False);

	display->flush = 0;
	display->send = 0;

	/* Event tracking proven unreliable, disable */
	display->shm_event = 0;
}

static void display_flush(struct display *display)
{
	display_flush_cursor(display);
	display_flush_send(display);

	display_sync(display);

	if (!display->flush)
		return;

	DBG(("%s(%s)\n", __func__, DisplayString(display->dpy)));

	XFlush(display->dpy);
	display->flush = 0;
}

static int first_display_first_sibling(struct context *ctx)
{
	const char *str, *colon;
	int dpy, scr, len;

	str = DisplayString(ctx->display->dpy);
	colon = strrchr(str, ':');
	if (colon == NULL)
		return -1;

	if (sscanf(colon + 1, "%d.%d", &dpy, &scr) == 1)
		scr = 0;

	len = (colon - str) + 1;
	memcpy(ctx->command, str, len);
	len += sprintf(ctx->command + len, "%d.", dpy);
	ctx->command_continuation = len;

	return scr + 1;
}

static int first_display_sibling(struct context *ctx, int i)
{
	if (i < 0)
		return 0;

	sprintf(ctx->command + ctx->command_continuation, "%d", i);
	return 1;
}


#define first_display_for_each_sibling(CTX, i) \
	for (i = first_display_first_sibling(CTX); first_display_sibling(CTX, i); i++)

static void context_cleanup(struct context *ctx)
{
	Display *dpy = ctx->display->dpy;
	XRRScreenResources *res;
	int i, j;

	res = _XRRGetScreenResourcesCurrent(dpy, ctx->display->root);
	if (res == NULL)
		return;

	XGrabServer(dpy);

	for (i = 0; i < ctx->nclone; i++) {
		struct clone *clone = &ctx->clones[i];
		XRROutputInfo *output;

		assert(clone->src.display == ctx->display);

		output = XRRGetOutputInfo(dpy, res, clone->src.rr_output);
		if (output == NULL)
			continue;

		disable_crtc(dpy, res, output->crtc);
		for (j = 0; j < output->nmode; j++)
			XRRDeleteOutputMode(dpy, clone->src.rr_output, output->modes[j]);

		XRRFreeOutputInfo(output);
	}

	for (i = 0; i < res->nmode; i++) {
		if (strncmp(res->modes[i].name, "VIRTUAL", 7) == 0) {
			XRRDestroyMode(dpy, res->modes[i].id);
			continue;
		}

		if (strcmp(res->modes[i].name, "ClaimVirtualHead") == 0) {
			XRRDestroyMode(dpy, res->modes[i].id);
			continue;
		}
	}

	XUngrabServer(dpy);
	XRRFreeScreenResources(res);

	if (ctx->singleton)
		XDeleteProperty(dpy, ctx->display->root, ctx->singleton);
	XCloseDisplay(dpy);
}

static int done;

static void signal_handler(int sig)
{
	done = sig;
}

int main(int argc, char **argv)
{
	struct context ctx;
	const char *src_name = NULL;
	uint64_t count;
	int daemonize = !EXTRA_DBG, bumblebee = 0, all = 0, singleton = 1;
	int i, ret, open, fail;

	signal(SIGPIPE, SIG_IGN);

	while ((i = getopt(argc, argv, "abd:fhS")) != -1) {
		switch (i) {
		case 'd':
			src_name = optarg;
			break;
		case 'f':
			daemonize = 0;
			break;
		case 'b':
			bumblebee = 1;
			break;
		case 'a':
			all = 1;
			break;
		case 'S':
			singleton = 0;
			break;
		case 'h':
		default:
			usage(argv[0]);
			exit(0);
		}
	}

	ret = context_init(&ctx);
	if (ret)
		return -ret;

	XSetErrorHandler(_check_error_handler);

	ret = add_fd(&ctx, display_open(&ctx, src_name));
	if (ret) {
		fprintf(stderr, "Unable to connect to \"%s\".\n", src_name ?: getenv("DISPLAY") ?:
			"<unspecified>, set either the DISPLAY environment variable or pass -d <display name> on the commandline");
		return -ret;
	}

	if (singleton) {
		XSelectInput(ctx.display->dpy, ctx.display->root, PropertyChangeMask);
		if (first_display_has_singleton(&ctx)) {
			DBG(("%s: pinging singleton\n", DisplayString(ctx.display->dpy)));
			ret = first_display_send_command(&ctx, 2000, "P");
			if (ret) {
				if (ret != -ETIME)
					return -ret;
				DBG(("No reply from singleton; assuming control\n"));
			} else {
				DBG(("%s: singleton active, sending open commands\n", DisplayString(ctx.display->dpy)));

				open = fail = 0;
				for (i = optind; i < argc; i++) {
					ret = first_display_send_command(&ctx, 5000, "C%s", argv[i]);
					if (ret && ret != -EBUSY) {
						fprintf(stderr, "Unable to connect to \"%s\".\n", argv[i]);
						fail++;
					} else
						open++;
				}
				if (all || (optind == argc && !bumblebee)) {
					first_display_for_each_sibling(&ctx, i) {
						ret = first_display_send_command(&ctx, 5000, "C%s", ctx.command);
						if (ret && ret != -EBUSY)
							break;
						else
							open++;
					}
				}
				if (bumblebee || (optind == argc && !all)) {
					ret = first_display_send_command(&ctx, 5000, "B");
					if (ret && ret != -EBUSY) {
						if (bumblebee)
							fprintf(stderr, "Unable to connect to bumblebee.\n");
						fail++;
					} else
						open++;
				}
				return open || !fail ? 0 : ECONNREFUSED;
			}
		}
		ret = first_display_register_as_singleton(&ctx);
		if (ret)
			return ret;
	}

	ret = display_init_damage(ctx.display);
	if (ret)
		return ret;

	if ((ctx.display->rr_event | ctx.display->rr_error) == 0) {
		fprintf(stderr, "RandR extension not supported by %s\n", DisplayString(ctx.display->dpy));
		return EINVAL;
	}
	XRRSelectInput(ctx.display->dpy, ctx.display->root, RRScreenChangeNotifyMask);
	XFixesSelectCursorInput(ctx.display->dpy, ctx.display->root, XFixesDisplayCursorNotifyMask);

	ret = add_fd(&ctx, record_mouse(&ctx));
	if (ret) {
		fprintf(stderr, "XTEST extension not supported by display \"%s\"\n", DisplayString(ctx.display->dpy));
		return -ret;
	}

	open = fail = 0;
	for (i = optind; i < argc; i++) {
		ret = last_display_clone(&ctx, display_open(&ctx, argv[i]));
		if (ret && ret != -EBUSY) {
			fprintf(stderr, "Unable to connect to \"%s\".\n", argv[i]);
			fail++;
		} else
			open++;
	}
	if (all || (optind == argc && !bumblebee)) {
		first_display_for_each_sibling(&ctx, i) {
			ret = last_display_clone(&ctx, display_open(&ctx, ctx.command));
			if (ret && ret != -EBUSY)
				break;
			else
				open++;
		}
	}
	if (bumblebee || (optind == argc && !all)) {
		ret = last_display_clone(&ctx, bumblebee_open(&ctx));
		if (ret && ret != -EBUSY) {
			if (bumblebee)
				fprintf(stderr, "Unable to connect to bumblebee.\n");
			fail++;
		} else
			open++;
	}
	if (open == 0)
		return fail ? ECONNREFUSED : 0;

	if (daemonize && daemon(0, 0))
		return EINVAL;

	signal(SIGHUP, signal_handler);
	signal(SIGINT, signal_handler);
	signal(SIGTERM, signal_handler);

	ctx.command_continuation = 0;
	while (!done) {
		XEvent e;
		int reconfigure = 0;
		int rr_update = 0;

		DBG(("polling - enable timer? %d, nfd=%d, ndisplay=%d\n", ctx.timer_active, ctx.nfd, ctx.ndisplay));
		ret = poll(ctx.pfd + !ctx.timer_active, ctx.nfd - !ctx.timer_active, -1);
		if (ret <= 0)
			break;

		/* pfd[0] is the timer, pfd[1] is the local display, pfd[2] is the mouse, pfd[3+] are the remotes */

		DBG(("poll reports %d fd awake\n", ret));
		if (ctx.pfd[1].revents || XPending(ctx.display[0].dpy)) {
			DBG(("%s woken up\n", DisplayString(ctx.display[0].dpy)));
			do {
				XNextEvent(ctx.display->dpy, &e);

				if (e.type == ctx.display->damage_event + XDamageNotify ) {
					const XDamageNotifyEvent *de = (const XDamageNotifyEvent *)&e;
					struct clone *clone;

					DBG(("%s damaged: (%d, %d)x(%d, %d)\n",
					     DisplayString(ctx.display->dpy),
					     de->area.x, de->area.y, de->area.width, de->area.height));

					for (clone = ctx.active; clone; clone = clone->next)
						clone_damage(clone, &de->area);

					if (ctx.active)
						context_enable_timer(&ctx);
				} else if (e.type == ctx.display->xfixes_event + XFixesCursorNotify) {
					XFixesCursorImage *cur;

					DBG(("%s cursor changed\n",
					     DisplayString(ctx.display->dpy)));

					cur = XFixesGetCursorImage(ctx.display->dpy);
					if (cur == NULL)
						continue;

					for (i = 1; i < ctx.ndisplay; i++)
						display_load_visible_cursor(&ctx.display[i], cur);

					XFree(cur);
				} else if (e.type == ctx.display->rr_event + RRScreenChangeNotify) {
					DBG(("%s screen changed (reconfigure pending? %d)\n",
					     DisplayString(ctx.display->dpy), reconfigure));
					reconfigure = 1;
				} else if (e.type == PropertyNotify) {
					XPropertyEvent *pe = (XPropertyEvent *)&e;
					if (pe->atom == ctx.singleton) {
						DBG(("lost control of singleton\n"));
						return 0;
					}
				} else if (e.type == ClientMessage) {
					XClientMessageEvent *cme;

					DBG(("%s client message\n",
					     DisplayString(ctx.display->dpy)));

					cme = (XClientMessageEvent *)&e;
					if (cme->message_type != ctx.singleton)
						continue;
					if (cme->format != 8)
						continue;

					first_display_handle_command(&ctx, cme->data.b);
				} else {
					DBG(("unknown event %d\n", e.type));
				}
			} while (XEventsQueued(ctx.display->dpy, QueuedAfterReading));
		}

		for (i = 1; i < ctx.ndisplay; i++) {
			if (ctx.pfd[i+2].revents == 0 && !XPending(ctx.display[i].dpy))
				continue;

			DBG(("%s woken up\n", DisplayString(ctx.display[i].dpy)));
			do {
				XNextEvent(ctx.display[i].dpy, &e);

				DBG(("%s received event %d\n", DisplayString(ctx.display[i].dpy), e.type));
				if (ctx.display[i].rr_active && e.type == ctx.display[i].rr_event + RRNotify) {
					XRRNotifyEvent *re = (XRRNotifyEvent *)&e;

					DBG(("%s received RRNotify, type %d\n", DisplayString(ctx.display[i].dpy), re->subtype));
					if (re->subtype == RRNotify_OutputChange) {
						XRROutputPropertyNotifyEvent *ro = (XRROutputPropertyNotifyEvent *)re;
						struct clone *clone;

						DBG(("%s RRNotify_OutputChange, timestamp %ld\n", DisplayString(ctx.display[i].dpy), ro->timestamp));
						for (clone = ctx.display[i].clone; clone; clone = clone->next) {
							if (clone->dst.rr_output == ro->output)
								rr_update = clone->rr_update = 1;
						}
					}
				}
			} while (XEventsQueued(ctx.display[i].dpy, QueuedAfterReading));
		}

		if (rr_update) {
			for (i = 0; i < ctx.nclone; i++)
				clone_update(&ctx.clones[i]);
		}

		if (reconfigure && context_update(&ctx))
			display_reset_damage(ctx.display);

		while (XPending(ctx.record)) /* discard all implicit events */
			XNextEvent(ctx.record, &e);

		if (ctx.timer_active && read(ctx.timer, &count, sizeof(count)) > 0) {
			struct clone *clone;

			DBG(("%s timer expired (count=%ld)\n", DisplayString(ctx.display->dpy), (long)count));
			ret = 0;

			if (ctx.active) {
				DBG(("%s clearing damage\n", DisplayString(ctx.display->dpy)));
				XDamageSubtract(ctx.display->dpy, ctx.display->damage, None, None);
				ctx.display->flush = 1;
			}

			for (clone = ctx.active; clone; clone = clone->active)
				ret |= clone_paint(clone);

			for (i = 0; i < ctx.ndisplay; i++)
				display_flush(&ctx.display[i]);

			DBG(("%s timer still active? %d\n", DisplayString(ctx.display->dpy), ret != 0));
			ctx.timer_active = ret != 0;
		}
	}

	context_cleanup(&ctx);
	return 0;
}