File: x11.trm

package info (click to toggle)
gnuplot 6.0.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 14,936 kB
  • sloc: ansic: 95,319; cpp: 7,590; makefile: 2,470; javascript: 2,328; sh: 1,531; lisp: 664; perl: 304; pascal: 191; tcl: 88; python: 46
file content (2940 lines) | stat: -rw-r--r-- 91,050 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
/* Hello, Emacs, this is -*-C-*- */

/* GNUPLOT - x11.trm */

/*[
 * Copyright 1986 - 1993, 1998, 2004   Thomas Williams, Colin Kelley
 *
 * Permission to use, copy, and distribute this software and its
 * documentation for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 *
 * Permission to modify the software is granted, but not the right to
 * distribute the complete modified source code.  Modifications are to
 * be distributed as patches to the released version.  Permission to
 * distribute binaries produced by compiling modified sources is granted,
 * provided you
 *   1. distribute the corresponding source modifications from the
 *    released version in the form of a patch file along with the binaries,
 *   2. add special version identification to distinguish your version
 *    in addition to the base release version number,
 *   3. provide your name and address as the primary contact for the
 *    support of your modified version, and
 *   4. retain our contact information in regard to use of the base
 *    software.
 * Permission to distribute the released version of the source code along
 * with corresponding source modifications in the form of a patch file is
 * granted with same provisions 2 through 4 for binary distributions.
 *
 * This software is provided "as is" without express or implied warranty
 * to the extent permitted by applicable law.
]*/

/*
 *    x11.trm  --- inboard terminal driver for X11
 */

/* Petr Mikulik and Johannes Zellner: added mouse support (October 1999)
 * Implementation and functionality is based on pm.trm
 */

/* X11 support for Petr Mikulik's pm3d
 * by Johannes Zellner <johannes@zellner.org>
 * (November 1999 - January 2000)
 */

/* Dynamic font support, enhanced text mode support,
 * additional feedback from outboard driver
 * Ethan A Merritt <merritt@u.washington.edu>
 * 2003
 */

/* Daniel Sebald: added X11 support for images. (27 February 2003)
 */

/* Toggle plots on/off by clicking on the key entry
 * Ethan A Merritt 2013
 */

#include "driver.h"

#ifdef TERM_REGISTER
register_term(x11)
#endif

#ifdef TERM_PROTO
int X11_args(int argc, char *argv[]);

TERM_PUBLIC void X11_options(void);
TERM_PUBLIC void X11_init(void);
TERM_PUBLIC void X11_graphics(void);
TERM_PUBLIC void X11_text(void);
TERM_PUBLIC int  X11_set_font(const char * fontname);
TERM_PUBLIC void X11_reset(void);
TERM_PUBLIC void X11_move(unsigned int x, unsigned int y);
TERM_PUBLIC void X11_vector(unsigned int x, unsigned int y);
TERM_PUBLIC void X11_linewidth(double lw);
TERM_PUBLIC void X11_pointsize(double ps);
TERM_PUBLIC void X11_linetype(int lt);
TERM_PUBLIC void X11_dashtype(int type, t_dashtype *custom_dash_type);
TERM_PUBLIC void X11_put_text(unsigned int x, unsigned int y, const char str[]);
TERM_PUBLIC int X11_text_angle(float i);
TERM_PUBLIC int X11_justify_text(enum JUSTIFY mode);
TERM_PUBLIC void X11_point(unsigned int x, unsigned int y, int number);
TERM_PUBLIC void X11_fillbox(int style, unsigned int x, unsigned int y, unsigned int width, unsigned int height);

TERM_PUBLIC void X11_send_endianess(void);

# ifdef USE_MOUSE
TERM_PUBLIC int X11_waitforinput(int);
TERM_PUBLIC void X11_set_ruler(int, int);
TERM_PUBLIC void X11_set_cursor(int, int, int);
TERM_PUBLIC void X11_put_tmptext(int, const char str[]);
TERM_PUBLIC void X11_set_clipboard(const char[]);
TERM_PUBLIC void X11_layer(t_termlayer syncpoint);
# endif

TERM_PUBLIC void X11_update_opts(void);
TERM_PUBLIC int X11_make_palette(t_sm_palette *);
TERM_PUBLIC void X11_set_color(t_colorspec *);
TERM_PUBLIC void X11_filled_polygon(int, gpiPoint *);

TERM_PUBLIC void X11_image(unsigned int, unsigned int, coordval *, gpiPoint *, t_imagecolor);

/* To support "set term x11 enhanced" */
TERM_PUBLIC void ENHX11_put_text(unsigned int x, unsigned int y, const char str[]);
TERM_PUBLIC void ENHX11_OPEN(char * fontname, double fontsize,
				double base, TBOOLEAN widthflag, TBOOLEAN showflag,
				int overprint);
TERM_PUBLIC void ENHX11_FLUSH(void);
TERM_PUBLIC void ENHX11_boxed_text(unsigned int, unsigned int, int);
TERM_PUBLIC void X11_modify_plots(unsigned int, int);

/* These are the default terminal sizing parameters. These should match the
   sizes in gplt_x11.c, and both specify 640,450 */
# define X11_XMAX 4096
# define X11_YMAX (4096*450/640)

static int X11_SIZE_X = 640;
static int X11_SIZE_Y = 450;
static int X11_POSITION_X = 0;
static int X11_POSITION_Y = 0;

/* approximations for typical font/screen sizes. The are just the defaults that
   get updated by the outboard driver. We make sure that the inboard and the
   outboard defaults match up at the start. The *10/25 is the factor of 2.5 that
   gets applied in the GE_fontprops handler in mouse.c */
# define X11_VCHAR (X11_YMAX/25)
# define X11_HCHAR (X11_XMAX/100)
# define X11_VTIC (X11_VCHAR * 10 / 25)
# define X11_HTIC (X11_VCHAR * 10 / 25)
#endif	/* TERM_PROTO */


#ifndef TERM_PROTO_ONLY

#ifdef TERM_BODY

#include "gplt_x11.h"

/* non-zero if '-display' found on command line */
static int X11_Display = 0;

/* Avoid unnecessary linetype changes */
static int X11_linetype_last = LT_UNDEFINED;

/* Terrible, horrible, no good, very bad hack for rotation of enhanced text */
static double X11_textangle = 0.;
static int X11_ycorrection;

/* Fonts can have very long names */
/* EAM FIXME - these should be dynamically allocated */
#define X11_MAX_FONTNAME_LENGTH 255
static char X11_default_font[X11_MAX_FONTNAME_LENGTH+1] = {'\0'};
static int  X11_default_fontsize = 12;
static char X11_last_font_used[X11_MAX_FONTNAME_LENGTH+1] = {'\01','\0'};
static char X11_next_font_used[X11_MAX_FONTNAME_LENGTH+1] = {'\0'};
static enum JUSTIFY X11_last_justification = LEFT;

static void X11_atexit(void);
static void X11_set_default_font(void);

static void transmit_gradient(gradient_struct *gradient, int cnt, int type);

/* Merged the old char X11_opts[] and int X11_optarg[]
 * into one array of structs.
 * Loosely based on XrmOptionDescRec, the use of which
 * would probably be overkill here. */
typedef enum { hasNoArg, hasArg } OptionArg;

static struct x11opt {
    const char *option;		/* Name of option */
    OptionArg arg;		/* Whether option has argument */
} X11_cmdopts[] = {
    { "-mono", hasNoArg}, { "-gray", hasNoArg}, { "-clear", hasNoArg},
    { "-tvtwm", hasNoArg}, { "-pointsize", hasArg},
    { "-iconic", hasNoArg}, { "-rv", hasNoArg},
    { "-reverse", hasNoArg}, { "+rv", hasNoArg},
    { "-synchronous", hasNoArg},
    { "-display", hasArg}, { "-geometry", hasArg}, { "-bg", hasArg},
    { "-background", hasArg}, { "-bd", hasArg},
    { "-bordercolor", hasArg}, { "-bw", hasArg},
    { "-borderwidth", hasArg}, { "-fg", hasArg},
    { "-foreground", hasArg}, { "-fn", hasArg}, { "-font", hasArg},
    { "-name", hasArg},
    { "-selectionTimeout", hasArg}, { "-title", hasArg},
    { "-xnllanguage", hasArg}, { "-xrm", hasArg},
    { "-raise", hasNoArg}, { "-noraise", hasNoArg},
    { "-replotonresize", hasNoArg}, { "-noreplotonresize", hasNoArg},
    { "-solid", hasNoArg}, { "-dashed", hasNoArg}
#ifdef USE_MOUSE
    , { "-nofeedback", hasNoArg}
    , { "-noevents", hasNoArg}
    , { "-ctrlq", hasNoArg}
#endif
};

#define X11_nopts (sizeof(X11_cmdopts) / sizeof(X11_cmdopts[0]))

static FILE *X11_ipc = (FILE *) 0;
#ifdef PIPE_IPC
static SELECT_TYPE_ARG1 ipc_back_fd = IPC_BACK_CLOSED;
#endif

/* reserve a minimum 10 driver opts */
static char *optvec[2 * X11_nopts + 1 + 10];
static char x11_userbkg[] = "#RRGGBB";

/* gnuplot_x11 has extension .exe on OS/2 and Windows (and on RSX/VMS, but
   these construct their own full path GNUPLOT_X11 in their build script).
*/
# if defined(OS2) || defined(_Windows)
#   ifndef GNUPLOT_X11
static char X11_default_command[] = "gnuplot_x11.exe";
#   else
static char X11_default_command[] = GNUPLOT_X11 ".exe";
#   endif
# else  /* thus !OS/2 && !Windows */
#   ifndef GNUPLOT_X11
static char X11_default_command[] = "gnuplot_x11";
#   else
static char X11_default_command[] = GNUPLOT_X11;
#   endif
# endif	/* OS/2 || Windows */

static char *X11_command = X11_default_command;
static char *X11_command_parsed = NULL;
static char *X11_full_command_path = NULL;

/* must match the definition in src/gplt_x11.c: */
static int x11_persist = UNSET;
static int do_raise = UNSET;
static int replot_on_resize = UNSET;
static int dashedlines = UNSET;
static int ctrlq = UNSET;
static int set_size = UNSET;
static int set_position = UNSET;

/* driver properties managed by x11.trm rather than gnuplot_x11 */
static double X11_linewidth_multiplier = 1.0;

/* Internal markers to avoid duplicate move/vector/set_color(rgb) commands */
static int X11_xlast = -1;
static int X11_ylast = -1;
#define X11_INVALIDATE_CURRENT_POSITION X11_xlast = X11_ylast = -1
static unsigned int X11_rgblast;
#define X11_INVALIDATE_CURRENT_RGB X11_rgblast = ~0x1FFFFFF

#ifdef USE_MOUSE
/* Interlock to prevent the mouse channel from being coopted more than
 * once per plot by the gnuplot_x11<->x11.trm font information query.
 */
static TBOOLEAN default_font_size_known = FALSE;
static TBOOLEAN X11_MOUSE_FEEDBACK = TRUE;
#endif

static int parse_driver(const char *);

static int
parse_driver(const char *cmd)
{
    int nr = 0;
    char *ptr;

    /* make a copy of cmd, as parsing will modify the string */
    X11_command_parsed = gp_realloc(X11_command_parsed,
				    strlen(cmd) + 1, "x11->parse_driver");
    strcpy(X11_command_parsed, X11_command);
    ptr = X11_command_parsed;

    while (*ptr != '\0' && nr < sizeof(optvec) / sizeof(char *)) {

	/* Strip whitespace.  Use nulls, so that
	 * the previous argument is terminated
	 * automatically.
	 */
	while (isspace((unsigned char)*ptr))
	    *ptr++ = '\0';

	if (!(*ptr))		/* don't count the terminating NULL */
	    break;

	/* Save the argument. */
	optvec[nr++] = ptr;

	/* Skip over the argument. */
	while ('\0' != *ptr && !isspace((unsigned char)*ptr)) {
	    ptr++;
	}
    }

    /* HBB 20020214: new code to prepend the environment X11_DRIVER_DIR
     * to the command name, if it doesn't contain any slashes yet */
    if (!strchr(optvec[0],'/')) {
	char *dir = getenv("GNUPLOT_DRIVER_DIR");

	if (!dir)
	    dir = X11_DRIVER_DIR;
#ifndef OS2
	if (dir[0] != '/' && dir[0] != '.') {
	    /* Can't call int_error because longjump has not been set up yet */
	    int_warn(NO_CARET, "Illegal X11 driver directory name! Using default");
	    dir = "";
	}
#endif
	X11_full_command_path = gp_realloc(X11_full_command_path,
					   strlen(dir) + strlen(optvec[0]) + 2,
					   "x11 driver pathname");
	/* optvec[0] = X11_full_command_path; */
	if (*dir)
	    sprintf(X11_full_command_path, "%s/%s", dir, optvec[0]);
	else
	    sprintf(X11_full_command_path, "%s", optvec[0]);
    }

    return nr;
}

/*   X11_args - scan gnuplot command line for standard X Toolkit options
 * called from plot.c so must not be TERM_PUBLIC (which may be static)
 */

int
X11_args(int argc, char *argv[])
{
    int nx11 = 0, i, n;
    char **xargv, **xargv_orig;

    xargv = xargv_orig = (char **) gp_alloc(argc * sizeof(char *), "<xargv>");

    /* We make a copy of the argument vector because
     * argv is modified later. */
    memcpy(xargv, argv, argc * sizeof(char *));
    i = parse_driver(X11_command);

    if (persist_cl) {
	optvec[i++] = "-persist";
    }

    while (++argv, ++xargv, --argc > 0) {
	if (!strcmp(*argv,"--persist") || !strncmp(*argv,"-p",2)) {
	    nx11++;
	    continue;
	}
	for (n = 0; n < X11_nopts; n++) {
	    if (strcmp(*argv, X11_cmdopts[n].option) == 0) {
		optvec[i++] = *xargv;
#ifdef USE_MOUSE
		if (strcmp(*argv, "-nofeedback") == 0)
		    X11_MOUSE_FEEDBACK = FALSE;
#endif
		if (strcmp(*argv, "-display") == 0)
		    X11_Display++;
		if (X11_cmdopts[n].arg == hasArg) {
		    if (--argc <= 0) {
			free(xargv_orig);
			return nx11;
		    }
		    optvec[i++] = *++xargv, ++argv;
		    nx11++;
		}
		if (i >= (sizeof(optvec) / sizeof(char *))) {
		    free(xargv_orig);
		    return nx11;	/* optvec is full */
		}
		nx11++;
		break;
	    }
	}
	if (n == X11_nopts)
	    break;
    }
    free(xargv_orig);
    return (nx11);
}


enum X11_id {
    X11_RESET,
    X11_CLOSE,
    X11_PERSIST,
    X11_NOPERSIST,
    X11_RAISE,
    X11_NORAISE,
    X11_REPLOTONRESIZE,
    X11_NOREPLOTONRESIZE,
    X11_FONT,
    X11_TITLE,
    X11_ENHANCED,
    X11_NOENHANCED,
    X11_SOLID,
    X11_DASHED,
    X11_LINEWIDTH,
    X11_CTRLQ,
    X11_NOCTRLQ,
    X11_SIZE,
    X11_POSITION,
    X11_BACKGROUND,
#ifdef EXTERNAL_X11_WINDOW
    X11_WINDOW,
#endif
    X11_OTHER
};

static struct gen_table X11_opts[] = {
    {"res$et", X11_RESET},
    {"cl$ose", X11_CLOSE},
    {"per$sist", X11_PERSIST},
    {"noper$sist", X11_NOPERSIST},
    {"rai$se", X11_RAISE},
    {"norai$se", X11_NORAISE},
    {"rep$lotonresize", X11_REPLOTONRESIZE},
    {"norep$lotonresize", X11_NOREPLOTONRESIZE},
    {"font",   X11_FONT},
    {"fn$ame", X11_FONT},
    {"ti$tle", X11_TITLE},
    {"enh$anced", X11_ENHANCED},
    {"noenh$anced", X11_NOENHANCED},
    {"solid", X11_SOLID},
    {"dash$ed", X11_DASHED},
    {"linew$idth", X11_LINEWIDTH},
    {"lw", X11_LINEWIDTH},
    {"ctrl$q", X11_CTRLQ},
    {"noctrl$q", X11_NOCTRLQ},
    {"si$ze", X11_SIZE},
    {"pos$ition", X11_POSITION},
    {"backg$round", X11_BACKGROUND},
#ifdef EXTERNAL_X11_WINDOW
    {"w$indow", X11_WINDOW},
#endif
    {NULL, X11_OTHER}
};


TERM_PUBLIC void
X11_options()
{

#define NOT_PROCESS_IF_DUPLICATION 1

    int c_title_token = 0;
    int new_term_number = 0;
    int background;
    TBOOLEAN duplication = FALSE;
    TBOOLEAN set_reset = FALSE, set_persist = FALSE, set_raise = FALSE, set_font = FALSE;
    TBOOLEAN set_replotonresize = FALSE;
    TBOOLEAN set_ctrlq = FALSE;
    TBOOLEAN set_title = FALSE, set_number = FALSE, set_close = FALSE;
#ifdef EXTERNAL_X11_WINDOW
    unsigned long existing_X11_window_id = 0;
    TBOOLEAN set_window = FALSE;
#endif
    char *s;

    do_raise = ctrlq = UNSET;
    dashedlines = yes;
    replot_on_resize = UNSET;
    set_size = set_position = UNSET;
    x11_persist = persist_cl		/* previously set from command line */
	    || (x11_persist > 0);	/* or previously set here */
    strcpy(x11_userbkg,"#RRGGBB");

    while (!END_OF_COMMAND) {
	switch (lookup_table(&X11_opts[0], c_token)) {
	case X11_RESET:
	    c_token++;
	    if (set_reset) duplication=TRUE;
	    set_reset = TRUE;
	    break;
	case X11_CLOSE:
	    c_token++;
	    if (set_close) duplication=TRUE;
	    set_close = TRUE;
	    break;
	case X11_PERSIST:
	    x11_persist = yes;
	    c_token++;
	    if (set_persist) duplication=TRUE;
	    set_persist = TRUE;
	    break;
	case X11_NOPERSIST:
	    x11_persist = no;
	    c_token++;
	    if (set_persist) duplication=TRUE;
	    set_persist = TRUE;
	    break;
	case X11_CTRLQ:
	    ctrlq = yes;
	    c_token++;
	    if (set_ctrlq) duplication=TRUE;
	    set_ctrlq = TRUE;
	    break;
	case X11_NOCTRLQ:
	    ctrlq = no;
	    c_token++;
	    if (set_ctrlq) duplication=TRUE;
	    set_ctrlq = TRUE;
	    break;
	case X11_SOLID:
	case X11_DASHED:
	    /* Dashes always allowed in version 5 */
	    c_token++;
	    break;
	case X11_LINEWIDTH:
	    c_token++;
	    X11_linewidth_multiplier = real_expression();
	    if (X11_linewidth_multiplier <= 0)
		X11_linewidth_multiplier = 1.0;
	    break;
	case X11_RAISE:
	    do_raise = yes;
	    c_token++;
	    if (set_raise) duplication=TRUE;
	    set_raise = TRUE;
	    break;
	case X11_NORAISE:
	    do_raise = no;
	    c_token++;
	    if (set_raise) duplication=TRUE;
	    set_raise = TRUE;
	    break;
	case X11_REPLOTONRESIZE:
	    replot_on_resize = yes;
	    c_token++;
	    if (set_replotonresize) duplication=TRUE;
	    set_replotonresize = TRUE;
	    break;
	case X11_NOREPLOTONRESIZE:
	    replot_on_resize = no;
	    c_token++;
	    if (set_replotonresize) duplication=TRUE;
	    set_replotonresize = TRUE;
	    break;
	case X11_FONT:
	    c_token++;
	    if (END_OF_COMMAND)
		int_error(c_token, "expecting font name");
	    if (isstringvalue(c_token) && (s = try_to_get_string())) {
		strncpy(X11_default_font, s, sizeof(X11_default_font)-1);
		free(s);
	    } else {
		copy_str(X11_default_font, c_token, sizeof(X11_default_font));
		c_token++;
	    }
	    if (strchr(X11_default_font,','))
		sscanf(strchr(X11_default_font,',')+1, "%d",
			&X11_default_fontsize);
	    if (set_font) duplication=TRUE;
	    set_font = TRUE;
	    break;
	case X11_TITLE:
	    c_token++;
	    if (END_OF_COMMAND)
		int_error(c_token, "expecting title text");
	    c_title_token = c_token;
	    c_token++;
	    if (set_title) duplication=TRUE;
	    set_title = TRUE;
	    break;
	case X11_ENHANCED:
	    term->put_text = ENHX11_put_text;
	    term->flags |= TERM_ENHANCED_TEXT;
	    c_token++;
	    break;
	case X11_NOENHANCED:
	    term->put_text = X11_put_text;
	    term->flags &= ~TERM_ENHANCED_TEXT;
	    c_token++;
	    break;
	case X11_SIZE:
	    c_token++;
	    if (END_OF_COMMAND) {
		int_error(c_token, "expecting X[,Y]");
	    } else {
		int x_in, y_in;
		x_in = int_expression();
		if (x_in > 0)
		    X11_SIZE_X = x_in;
		else
		    int_error(c_token, "X size must be > 0");
		if (equals(c_token, ",")) {
		    c_token++;
		    y_in = int_expression();
		    if (y_in > 0)
			X11_SIZE_Y = y_in;
		    else
			int_error(c_token, "Y size must be > 0");
		}
		set_size = yes;
	    }
	    break;
	case X11_POSITION:
	    c_token++;
	    if (END_OF_COMMAND) {
		int_error(c_token, "expecting X[,Y]");
	    } else {
		X11_POSITION_X = int_expression();
		if (equals(c_token, ",")) {
		    c_token++;
		    X11_POSITION_Y = int_expression();
		}
		set_position = yes;
	    }
	    break;
	case X11_BACKGROUND:
	    c_token++;
	    background = parse_color_name();
	    sprintf(x11_userbkg,"#%06x",background);
	    break;

#ifdef EXTERNAL_X11_WINDOW
	case X11_WINDOW:
	{
	    char *windowid;
	    c_token++;
	    if (!(windowid = try_to_get_string()))
		int_error(c_token, "expecting X window ID as string in hex");
	    sscanf(windowid,"%lx",&existing_X11_window_id);
	    free(windowid);
	    if (!existing_X11_window_id)
		int_error(c_token, "expecting X window ID as string in hex");
	    if (set_window) duplication=TRUE;
	    set_window = TRUE;
	    break;
	}
#endif
	case X11_OTHER:
	default:
	    /* Silently ignore these, as they are not yet implemented */
	    if (equals(c_token, "dl") || almost_equals(c_token, "dashl$ength")) {
		c_token++;
		(void) real_expression();
		break;
	    }
	    if (set_number) duplication=TRUE;
	    /* let gnuplot_x11 check range */
	    new_term_number = int_expression();
	    if (new_term_number < 0)
		int_error(c_token, "plot number must be non-negative");
	    set_number = TRUE;
	    break;
	}

	if (duplication) {
	    int_error(c_token-1, "duplicated or contradicting arguments in X11 term options");
	}

    }


    /* Call own init routine; this might be xlib rather than x11 */
    term->init();

    if (set_reset)
	X11_atexit();	/* tell gnuplot_x11 to shut down */

    /* Leave the current window unchanged when closing an old window */
    if (X11_ipc) {
	if (set_close) {
	    if (set_number) {
		fprintf(X11_ipc, "C%d\n", new_term_number);
	    } else {
		fputs("C\n", X11_ipc);
	    }
	    fflush(X11_ipc);
	} else if (set_number) {
#ifdef EXTERNAL_X11_WINDOW
	    if (set_window)
		int_error(NO_CARET,"an existing X11 window cannot be assigned to a plot number");
#endif
	    fprintf(X11_ipc, "N%d\n", new_term_number);
	    fflush(X11_ipc);
#ifdef EXTERNAL_X11_WINDOW
	} else if (set_window) {
	    fprintf(X11_ipc, "%c%lx\n", X11_GR_SET_WINDOW_ID, existing_X11_window_id);
	    fflush(X11_ipc);
#endif
	}
    }

#ifdef EXTERNAL_X11_WINDOW
    if (set_window) {
	sprintf(term_options, "XID 0x%lX", existing_X11_window_id);
    } else
#endif
    if (set_number && new_term_number >= 0) {
	sprintf(term_options, "%d", new_term_number);
    }
    if (UNSET != do_raise) {
	strcat(term_options, (yes == do_raise ? " raise" : " noraise"));
    }
    if (UNSET != replot_on_resize) {
	strcat(term_options, (yes == replot_on_resize ? " replotonresize" : " noreplotonresize"));
    }
    if (UNSET != x11_persist) {
	strcat(term_options, (yes == x11_persist ? " persist" : " nopersist"));
    }
    if (UNSET != ctrlq) {
	strcat(term_options, (yes == ctrlq ? " ctrlq" : " noctrlq"));
    }
    if (term->put_text == ENHX11_put_text) {
	strcat(term_options, " enhanced");
    }
    if (X11_linewidth_multiplier != 1.0) {
	sprintf(term_options + strlen(term_options),
		" linewidth %.2g", X11_linewidth_multiplier);
    }
    if (*X11_default_font) {
	strcat(term_options, " font \"");
	strcat(term_options, X11_default_font);
	strcat(term_options, "\"");
    }
    if (set_title) {
	char *title;
	int save_token = c_token;
	c_token = c_title_token;
	strncat(term_options, " title \"", sizeof(term_options)-strlen(term_options)-1);
	title = term_options + strlen(term_options);
	if (isstringvalue(c_title_token) && (s = try_to_get_string())) {
	    strncat(term_options, s, sizeof(term_options)-strlen(term_options)-1);
	    free(s);
	} else
	    copy_str(term_options+strlen(term_options), c_title_token, MAX_LINE_LEN-strlen(term_options));
	if (X11_ipc) {
	    /* Send up to maximum buffer length minus three characters of
	     * title string to account for required 'n', '\0', '\n'.
	     */
	    int i;
	    fputc('n', X11_ipc);
	    for (i=0; i < X11_COMMAND_BUFFER_LENGTH-3 && title[i] != '\0'; i++)
		fputc(title[i], X11_ipc);
	    fputc('\0', X11_ipc);
	    fputc('\n', X11_ipc);
	    fflush(X11_ipc);
	}
	strncat(term_options, "\"", sizeof(term_options)-strlen(term_options)-1);
	c_token = save_token;
    }
    if (set_size != UNSET)
	sprintf(term_options + strlen(term_options),
	      " size %d,%d ", X11_SIZE_X,X11_SIZE_Y);

    if (set_position != UNSET)
	sprintf(term_options + strlen(term_options),
	      " position %d,%d ", X11_POSITION_X,X11_POSITION_Y);

    X11_update_opts();
}


void
x11_raise_terminal_window(int number)
{
    /* Send the raise character and a number. */
    if (X11_ipc) {
	fprintf(X11_ipc, "^%d\n", number);
	fflush(X11_ipc);
    }
}

void
x11_raise_terminal_group(void)
{
    /* Send just the raise character. */
    if (X11_ipc) {
	fprintf(X11_ipc, "^\n");
	fflush(X11_ipc);
    }
}

void
x11_lower_terminal_window(int number)
{
    /* Send the raise character and a number. */
    if (X11_ipc) {
	fprintf(X11_ipc, "v%d\n", number);
	fflush(X11_ipc);
    }
}

void
x11_lower_terminal_group(void)
{
    /* Send just the raise character. */
    if (X11_ipc) {
	fprintf(X11_ipc, "v\n");
	fflush(X11_ipc);
    }
}


TERM_PUBLIC void
X11_update_opts()
{
    if (!X11_ipc)
	return;

    if (!strcmp("xlib",term->name))
	return;

    if (UNSET != do_raise || UNSET != x11_persist ||
        UNSET != ctrlq || UNSET != replot_on_resize)
    {
	fprintf(X11_ipc, "X %d %d %d %d %d\n",
		do_raise, x11_persist, dashedlines, ctrlq, replot_on_resize);
	fflush(X11_ipc);
    }

    /* pass size as a valid X11 geometry string WIDTHxHEIGHT[+XPOS+YPOS] */
    if (set_size != UNSET || set_position != UNSET) {
	if (set_size != UNSET && set_position == UNSET)
	    fprintf(X11_ipc, "s %dx%d\n", X11_SIZE_X, X11_SIZE_Y);
	else if (set_size == UNSET && set_position != UNSET)
	    fprintf(X11_ipc, "s %+d%+d\n", X11_POSITION_X, X11_POSITION_Y);
	else if (set_size != UNSET && set_position != UNSET)
	    fprintf(X11_ipc, "s %dx%d%+d%+d\n", X11_SIZE_X, X11_SIZE_Y,
		X11_POSITION_X, X11_POSITION_Y);
	fflush(X11_ipc);
    }
}


/*-----------------------------------------------------------------------------
 *   Three different versions of the remainder of the X11 terminal driver
 *   are provided to support three different types of IPC with the
 *   gnuplot_x11 outboard terminal driver:
 *
 *   DEFAULT_X11:      popen() pipe for most un*x platforms
 *
 *   CRIPPLED_SELECT : file IPC for un*x platforms with incomplete or faulty
 *                     implementation of BSD select()
 *
 *   VMS :             mailbox/spawn IPC
 *---------------------------------------------------------------------------*/

#define DEFAULT_X11
#if defined(VMS) || defined(CRIPPLED_SELECT)
# undef DEFAULT_X11
#endif
#if defined(VMS) && defined(CRIPPLED_SELECT)
Error.Incompatible options.
#endif
/* we do not want to have to duplicate all the code, so we
 * do most of it with macros.
 * PRINT0(format), PRINT1(format, p1), PRINT2(format, p1, p2) etc
 * also  FLUSH0(format), etc, which do an additional flush
 */
#ifdef DEFAULT_X11
/*-----------------------------------------------------------------------------
 *   DEFAULT_X11 popen() pipe IPC
 *---------------------------------------------------------------------------*/
static void
X11_atexit()
{
    if (X11_ipc) {
	fputs("R\n", X11_ipc);
	fclose(X11_ipc);
	/* dont wait(), since they might be -persist */
	X11_ipc = NULL;
#ifdef PIPE_IPC
	close(ipc_back_fd);
	ipc_back_fd = -1;
#endif
    }
}

#ifdef USE_MOUSE

TERM_PUBLIC int
X11_waitforinput(int options)
{
    /* There're several modes this function can take:

       if (options & TERM_ONLY_CHECK_MOUSING)

         Not reading stdin. Non-blocking reads from ipc_back_fd. We keep
         processing ipc_back_fd events as long as they're available. When no
         more data is available, we return 0.

         This mode trumps the others


       if( paused_for_mouse )

         Not reading stdin. Blocking reads from ipc_back_fd. We keep processing
         ipc_back_fd events in a loop


       if (options & TERM_WAIT_FOR_FONTPROPS)

         Very similar to if(paused_for_mouse). Not reading stdin. Blocking reads
         from ipc_back_fd in a loop

       Otherwise the logic is:

         if( any ipc_back_fd data is available )
           read and process it ALL;
         read and return one byte from stdin
     */

#ifndef PIPE_IPC
    if (options == TERM_ONLY_CHECK_MOUSING)
	return 0;
    return getc(stdin);
#else

    /* normal path */
    fd_set fds;
    static struct gp_event_t ge;
    static int l = 0;
    struct timeval one_msec;
    int n;

    bool blocking_loop_reading_ipc_back_fd =
        paused_for_mouse || (options & TERM_WAIT_FOR_FONTPROPS);
    bool have_any_ipc_back_fd_data = false;

    if (ipc_back_fd < 0) {
        if (paused_for_mouse) {
            /* We're waiting for a command from the back-channel IPC, but this pipe
             * doesn't exist, so we'll never get the command we're waiting for.
             * Give up.
	     */
            paused_for_mouse = 0;
            int_error(NO_CARET,"Mousing not active");
        }

        if (options & TERM_WAIT_FOR_FONTPROPS) {
            /* Similar. No back-channel, so we'll never get the "done" event.
             * Here I just silently return.
	     */
            return 0;
        }
        read_and_return_character();
    }

    /* XXX:  if the input device it not a tty (e.g. /dev/null)
     *       mouse events are not processed. This is necessary
     *       as on some systems /dev/null is not selectable.
     * TODO: should we close the ipc_back_fd in this case ? */

    /* While we have ipc_back_fd data to process, process it.
     * If not, read the stdin data.
     */
    do {
        int ierr;
        struct timeval *timeout = NULL; /* wait forever by default */
        FD_ZERO(&fds);
        FD_SET(ipc_back_fd, &fds);

        /* When taking input from the console, we are willing to wait here	*/
        /* until the next character is typed. But if input is from a script	*/
        /* we just want to check for hotkeys or mouse input and then leave	*/
        /*  again without waiting on stdin.					*/
        if (options == TERM_ONLY_CHECK_MOUSING) {
            timeout = &one_msec;
            one_msec.tv_sec = 0;
            one_msec.tv_usec = TERM_EVENT_POLL_TIMEOUT;
        }
        else if (!blocking_loop_reading_ipc_back_fd)
            FD_SET(fileno(stdin), &fds);

        ierr = select(ipc_back_fd + 1, SELECT_TYPE_ARG234 &fds, 0, 0, timeout);
        if (ierr < 0 && errno == EINTR) {
            wrap_readline_signal_handler();
            continue;
        }

        if (!FD_ISSET(ipc_back_fd, &fds)) {
            /* no ipc_back_fd data available. I can move on */
            if (options & TERM_ONLY_CHECK_MOUSING)
                return 0;
            if (blocking_loop_reading_ipc_back_fd)
                /* This shouldn't happen. In this case we should be waiting
                 * for ipc_back_fd data forever, so select() should never
                 * return with no data. In any case, I ask for more data if
                 * there isn't any.
		 */
                continue;

            break;
        }

        /* got some ipc_back_fd data. Process all of it before moving on to stdin */
        have_any_ipc_back_fd_data = true;

        n = read(ipc_back_fd, (void *) (l + (char *) &ge), sizeof(ge) - l);
        if (n == 0) {
            close(ipc_back_fd);
            ipc_back_fd = -1;
            /* don't close X11_ipc, otherwise later writes
             * to it will cause a segfault
             * outboard driver has stopped */
            paused_for_mouse = 0;
            if( options & (TERM_WAIT_FOR_FONTPROPS | TERM_ONLY_CHECK_MOUSING) )
                return 0;
            break;
        }
        l += n;
        if (l == sizeof(ge)) {
            /* do_event() may not return (if an error occurs), so reset l first.
	     * do_event() may clear paused_for_mouse, so save initial state.
	     */
	    TBOOLEAN was_paused_for_mouse = paused_for_mouse;
            l = 0;
            do_event(&ge);

            /* I react only to the first GE_fontprops event. Subsequent
               GE_fontprops events only save the sizing information for
               later. We also can't use the main pipe at that point. The
               'pause' command gets confused, for example. */
            if (ge.type == GE_fontprops && !default_font_size_known &&
                (options & TERM_WAIT_FOR_FONTPROPS))
                return GE_fontprops;

            if (ge.type == GE_buttonrelease && (paused_for_mouse & PAUSE_CLICK)) {
                int button = ge.par1;
                if (button == 1 && (paused_for_mouse & PAUSE_BUTTON1))
                    paused_for_mouse = 0;
                if (button == 2 && (paused_for_mouse & PAUSE_BUTTON2))
                    paused_for_mouse = 0;
                if (button == 3 && (paused_for_mouse & PAUSE_BUTTON3))
                    paused_for_mouse = 0;
                if (paused_for_mouse == 0)
                    return '\0';
            }
            if (ge.type == GE_keypress && (paused_for_mouse & PAUSE_KEYSTROKE)) {
                /* Ignore NULL keycode */
                if (ge.par1 > '\0') {
                    paused_for_mouse = 0;
                    return '\0';
                }
            }
            if (ge.type == GE_reset && was_paused_for_mouse) {
                /* Error or window close */
		return '\n';
            }
        }

    } while(blocking_loop_reading_ipc_back_fd || have_any_ipc_back_fd_data);

    read_and_return_character();

#endif /* PIPE_IPC */
}

#endif /* USE_MOUSE */


TERM_PUBLIC void
X11_init()
{
    static int been_here = 0;

    /* If the program started with some other terminal driver
     * and is only now changing to x11, more setup is needed.
     * In particular we need to start the outboard driver gnuplot_x11.
     */
    if (!X11_full_command_path)
	parse_driver(X11_command);

    if (!X11_ipc) {
	/* first time through or after a reset */
#if defined(OS2)
      /* FIXME amai 20020219: nice try...
       * But popen() does return a non-NULL handle to almost command,
       * it's just a new session which will stop if the command does
       * not exist... We should stat() for the argument?! */
      /* X11_ipc = popen(X11_full_command_path, "w");
      if (X11_ipc==NULL) */
      {
	   X11_ipc = popen(X11_command, "w");
      }
#else /* !(OS/2) */
	int fdes[2];
# ifdef PIPE_IPC
	int fdes_back[2];

#define X11_ALLOW_EVENTS (mouse_setting.on)
	if (X11_ALLOW_EVENTS) {
	    if (pipe(fdes_back))
		perror("pipe() failed:");
	}
# endif	/* PIPE_IPC */
	if (pipe(fdes))
	    perror("pipe() failed:");

	if (fork() == 0) {
	    /* child */
# ifdef PIPE_IPC
	    char noevents[] = "-noevents";
	    if (X11_ALLOW_EVENTS) {
		dup2(fdes_back[1], 1);	/* stdout to pipe */
		close(fdes_back[0]);
	    } else {
		char **ptr;
		for (ptr = optvec; ptr && *ptr; ptr++)
		    ;		/* do nothing: skip over set arguments */
		/* tell the driver not to supply any events by
		 * appending "-noevents" to the optvec list. */
		*ptr = noevents;
		*++ptr = (char *) 0;	/* terminate */
	    }
# endif	/* PIPE_IPC */

	    /* Add user-specified background to list of command options */
	    if (strcmp(x11_userbkg,"#RRGGBB")) {
		char **ptr;
		for (ptr = optvec; ptr && *ptr; ptr++)
		    ;		/* do nothing: skip over set arguments */
		*ptr++ = "-bg";
		*ptr++ = x11_userbkg;
		*ptr = (char *) 0;	/* terminate */
	    }

	    /* close the write side of the child's forward fd */
	    close(fdes[1]);
	    dup2(fdes[0], 0);	/* stdin from pipe */
	    execvp(X11_full_command_path, optvec);
	    /* if we get here, something went wrong */
	    fprintf(stderr,"Couldn't exec expected X11 driver: %s\n",X11_full_command_path);
	    fprintf(stderr, "Did you set environmental variable GNUPLOT_DRIVER_DIR?\n");
	    perror("Exec failed");
	    gp_exit(EXIT_FAILURE);
	}
	/* parent */
# ifdef PIPE_IPC
	/* X11_ipc_out = fdopen(fdes[0], "r"); */
	if (ipc_back_fd > 0) {
	    fprintf(stderr, "(X11_init) warning: unclosed ipc_back_fd.\n");
	    fprintf(stderr, "           this is probably a program error.\n");
	    close(ipc_back_fd);
	}
	if (X11_ALLOW_EVENTS) {
	    ipc_back_fd = fdes_back[0];
	    close(fdes_back[1]);	/* the parent doesn't need this */
	} else {
	    /* we do not open a bidirectional communication
	     * for non-tty's by default. If this is desired,
	     * the mouse must be turned on explicitly *before*
	     * starting the x11 driver.
	     * So: if we're here, we close the ipc-back-channel
	     * which will cause a SIGPIPE in the driver. This
	     * is *ugly* but it works. (joze) */
	    /* close(fdes_back[0]); */
	    /* mark ipc_back_fd as unusable */
	    ipc_back_fd = IPC_BACK_UNUSABLE;
	}
# endif	/* PIPE_IPC */
	/* close the read side of the parent's forward fd */
	close(fdes[0]);
	X11_ipc = fdopen(fdes[1], "w");
#endif /* !OS/2 */
    }

    if (!been_here) {
	gp_atexit(X11_atexit);
	been_here++;
    }
    X11_send_endianess();
#ifdef USE_MOUSE
    default_font_size_known = FALSE;
#endif
    X11_update_opts();
}

TERM_PUBLIC void
X11_reset()
{
#ifdef USE_MOUSE
    paused_for_mouse = 0;
#endif
}

#define PRINT0(fmt)          fprintf(X11_ipc, fmt)
#define PRINT1(fmt,p1)       fprintf(X11_ipc, fmt,p1)
#define PRINT2(fmt,p1,p2)    fprintf(X11_ipc, fmt,p1,p2)
#define PRINT3(fmt,p1,p2,p3) fprintf(X11_ipc, fmt,p1,p2,p3)
#define PRINT4(fmt,p1,p2,p3,p4) fprintf(X11_ipc, fmt,p1,p2,p3,p4)
#define PRINT5(fmt,p1,p2,p3,p4,p5) fprintf(X11_ipc, fmt,p1,p2,p3,p4,p5)

#define FFLUSH()             fflush(X11_ipc)

#define BEFORE_GRAPHICS		/* nowt */
#define AFTER_TEXT		/* nowt */


#elif defined(CRIPPLED_SELECT)
/* PLEASE CAN SOMEONE CHECK THAT THIS STILL WORKS !!! */
/*-----------------------------------------------------------------------------
 *   CRIPPLED_SELECT file IPC
 *---------------------------------------------------------------------------*/
static char X11_tmp[32], X11_tmp0[32], X11_shutdown[32];
static int X11_pid;

TERM_PUBLIC void
X11_init()
{
    if (!(X11_pid = fork())) {
	execvp(X11_full_command_path, optvec);
	_exit(1);
    }
    sprintf(X11_tmp, "/tmp/Gnuplot_%d", X11_pid);
    sprintf(X11_tmp0, "%s-", X11_tmp);
    sprintf(X11_shutdown, "echo R >%s", X11_tmp);
}

TERM_PUBLIC void
X11_reset()
{
    system(X11_shutdown);
}

#define BEFORE_GRAPHICS \
 if (!(X11_ipc = fopen(X11_tmp0, "w"))) { \
   perror(X11_tmp0); system(X11_shutdown); gp_exit(EXIT_FAILURE); \
 }

#define AFTER_TEXT \
 { fclose(X11_ipc); rename(X11_tmp0, X11_tmp); }

#define PRINT0(fmt)          fprintf(X11_ipc, fmt)
#define PRINT1(fmt,p1)       fprintf(X11_ipc, fmt,p1)
#define PRINT2(fmt,p1,p2)    fprintf(X11_ipc, fmt,p1,p2)
#define PRINT3(fmt,p1,p2,p3) fprintf(X11_ipc, fmt,p1,p2,p3)
#define PRINT4(fmt,p1,p2,p3,p4) fprintf(X11_ipc, fmt,p1,p2,p3,p4)
#define PRINT5(fmt,p1,p2,p3,p4,p5) fprintf(X11_ipc, fmt,p1,p2,p3,p4,p5)
#define FFLUSH()             fflush(X11_ipc)

static void
X11_atexit()
{
    /* WHAT SHOULD I DO ? */
}
#elif defined(VMS)
/*-----------------------------------------------------------------------------
 *   VMS mailbox/spawn IPC - Yehavi Bourvine - YEHAVI@VMS.HUJI.AC.IL
 *---------------------------------------------------------------------------*/
#include <iodef.h>
#include <descrip.h>
#include <dvidef.h>
#ifdef __DECC
#include <lib$routines.h>
#include <starlet.h>
#endif
#ifdef __GNUC__
#include <errno.h>
#else
int vaxc$errno;
#endif

#define SS$_NORMAL 1		/* or include <ssdef.h> for all SS$_ def's */

#define MBXMXMSG 128		/* DEFMBXMXMSG is set by SYSGEN */

static short X11_channel;

struct iosb {
    unsigned short stat;
    unsigned short count;
    unsigned long info;
};



TERM_PUBLIC void
X11_init()
{

    struct iosb iosb;

    static char devnam_string[64];
    static $DESCRIPTOR(devnam, devnam_string);

    struct {
	short int buf_len;
	short int item;
	char *buf_addr;
	unsigned short int *ret_len;
	int end;
    } item_list = {
    devnam.dsc$w_length, DVI$_DEVNAM, devnam.dsc$a_pointer, &devnam.dsc$w_length, 0};
#define CMDLEN 1024
    char cmdline[CMDLEN], *cmdp;
    int optindex;

    if (!X11_channel) {
	int one = 1;

	/* Create a descriptor for the command line that starts
	   GNUPLOT_X11. $DESCRIP doesn't work in this context... */

	/* FIXME!
	 * This does not work anymore since X11 option passing has been
	 * changed to use execvp() in the DEFAULT_X11 case
	 */
	struct dsc$descriptor_s pgmdsc = { 0, DSC$K_DTYPE_T,
	    DSC$K_CLASS_S, 0
	};
	optindex = 0;
	strcpy(cmdline, optvec[optindex]);
	cmdp = cmdline + strlen(optvec[optindex]);
	while (optvec[++optindex] != NULL) {
	    *cmdp++ = ' ';
	    *cmdp++ = '\"';
	    strcpy(cmdp, optvec[optindex]);
	    cmdp += strlen(optvec[optindex]);
	    *cmdp++ = '\"';
	}
	pgmdsc.dsc$w_length = cmdp - cmdline;
	pgmdsc.dsc$a_pointer = cmdline;

	/* Create a mailbox which will be used as a pipe for commands to the
	 * subprocess.  What we'll write to it will be read by the subprocess
	 * as its STDIN.  Use an unnamed mailbox and refer to it by its device
	 * number */

	vaxc$errno = sys$crembx(0, &X11_channel, MBXMXMSG, MBXMXMSG, 0, 0, 0, 0);
	if ((vaxc$errno & SS$_NORMAL) != SS$_NORMAL) {
	    printf("SYS$CreMbx failed with status=%d\r\n", vaxc$errno);
	    os_error(NO_CARET, "sys$crembx failed");
	}
	vaxc$errno = sys$getdviw(0, X11_channel, 0, &item_list, &iosb, 0, 0, 0);
	if ((vaxc$errno & SS$_NORMAL) == SS$_NORMAL)
	    vaxc$errno = iosb.stat;
	if ((vaxc$errno & SS$_NORMAL) != SS$_NORMAL) {
	    printf("SYS$Getdviw failed with status=%d\r\n", vaxc$errno);
	    sys$dassgn(X11_channel);
	    X11_channel = 0;
	    os_error(NO_CARET, "sys$getdviw failed");
	}
	/* Create a subprocess whose input is this mailbox. */
	vaxc$errno = lib$spawn(&pgmdsc, &devnam, 0, &one, 0, 0, 0, 0, 0, 0, 0, 0, 0);
	if ((vaxc$errno & SS$_NORMAL) != SS$_NORMAL) {
	    printf("LIB$SPAWN failed with status=%d\r\n", vaxc$errno);
	    sys$dassgn(X11_channel);
	    X11_channel = 0;
	    os_error(NO_CARET, "lib$spawn failed");
	}
    }
    {
	static int been_here = 0;
	if (!been_here) {
	    gp_atexit(X11_atexit);
	    been_here = 1;
	}
    }
}


/*   We use $QIO in order to avoid buffering problems, although it might
 *   work  as well with simple Fprintf calls.
 */

#define GO(x) \
do { \
   char buffer[512]; int status; struct iosb iosb;\
   sprintf x; \
   if (strlen(buffer) >= MBXMXMSG)  { \
     printf("buffer contents (%d char) catenated to mailbox size (%d bytes)\n", \
     strlen(buffer), MBXMXMSG); \
     buffer[MBXMXMSG-1] = '\0';\
     printf("%s\n", buffer); \
   } \
   status = sys$qiow(0, X11_channel, IO$_WRITEVBLK, &iosb, 0, 0, buffer, strlen(buffer), 0, 0, 0, 0); \
   if ((status & SS$_NORMAL) == SS$_NORMAL) status = iosb.stat; \
   if((status & SS$_NORMAL) != SS$_NORMAL) gp_exit(status); \
 } while (0)

#define PRINT0(fmt)          GO((buffer, fmt))
#define PRINT1(fmt,p1)       GO((buffer, fmt,p1))
#define PRINT2(fmt,p1,p2)    GO((buffer, fmt,p1,p2))
#define PRINT3(fmt,p1,p2,p3) GO((buffer, fmt,p1,p2,p3))
#define PRINT4(fmt,p1,p2,p3,p4) GO((buffer, fmt,p1,p2,p3,p4))
#define PRINT5(fmt,p1,p2,p3,p4,p5) GO((buffer, fmt,p1,p2,p3,p4,p5))

#define FFLUSH()		/* nowt */
#define BEFORE_GRAPHICS		/* nowt */
#define AFTER_TEXT		/* nowt */

static void
X11_atexit()
{
    if (X11_channel) {
	PRINT0("R\n");
	sleep(2);		/* Wait for subprocess to finish */
	sys$dassgn(X11_channel);
	X11_channel = 0;
    }
}

TERM_PUBLIC void
X11_reset()
{
    /* do nothing until exit */
}

#else /* !VMS */
    You lose.
#endif /* !VMS */


/* Cached sizing values for the x11 terminal.
 * Updated/Maintained in mouse.c
 */
int          X11_hchar_saved, X11_vchar_saved;
double       X11_ymax_saved = -1.0;

/* common stuff, using macros defined above */

TERM_PUBLIC void
X11_graphics()
{
#ifdef USE_MOUSE
    static unsigned long windowid = 0;
#endif
    static enum set_encoding_id last_encoding = S_ENC_DEFAULT;

    BEFORE_GRAPHICS;		/* kludge for crippled select */

#ifndef USE_MOUSE
    /* for VMS sake, keep as separate prints */
    PRINT0("G\n");
#else
#ifdef PIPE_IPC
    /* if we know the outboard driver has stopped, restart it */
    if (ipc_back_fd == IPC_BACK_CLOSED) {
	fclose(X11_ipc);
	X11_ipc = NULL;
	X11_init();
    }
#endif
    /* send also XID of gnuplot window (<space> then raises it up) */
    if (!windowid) {
	char *window_env = getenv("WINDOWID");
	if (window_env)
	    sscanf(window_env, "%lu", &windowid);
    }
#ifndef OS2
    PRINT1("G%lu\n", windowid);
#else
    PRINT2("G%lu %i\n", windowid, getpid());
#endif
#endif /* USE_MOUSE */

    if (encoding != last_encoding) {
	PRINT1("QE%d\n",encoding);
	last_encoding = encoding;
    }

#if defined(USE_MOUSE)
    /* EAM Jan 2013 - multiplot is incompatible with replot_on_resize.	*/
    /* FIXME: This disables replot_on_resize until the next 'set term'.	*/
    /* A cleaner solution would be to have gnuplot_x11 itself track 	*/
    /* whether the current window contains a multiplot, and not	send	*/
    /* replot requests if it does.					*/
    if (multiplot) {
	fprintf(X11_ipc, "X %d %d %d %d %d\n",
		do_raise, x11_persist, dashedlines, ctrlq, no);
	fflush(X11_ipc);
    }

    /* EAM June 2003 - Flush the set font command through the pipe */
    /* to gnuplot_x11, then wait for it to return the resulting    */
    /* font size information (v_char and h_char). The feedback is  */
    /* caught by do_event() as an event of type GE_fontprops.      */

    /* Dima Kogan Sep 2012: Above is true when generating the plot THE FIRST
       TIME.  I now save the sizing information every time the window sizes
       change (in the ..._saved variables). Thus I no longer need
       to ask for this information NOW, and can just use it.
     */
    if (!default_font_size_known) {
#ifdef PIPE_IPC
	PRINT1("QG%s\n", X11_default_font);
	FFLUSH();
	if (ipc_back_fd >= 0 && X11_MOUSE_FEEDBACK)
	    X11_waitforinput(TERM_WAIT_FOR_FONTPROPS);

	default_font_size_known = TRUE;
#else
	/* We still get the information for OS2_IPC... */
	PRINT1("QG%s\n", X11_default_font);
	FFLUSH();
	default_font_size_known = TRUE;
#endif
    } else {
	/* replot. I send a Qg to tell the outboard driver to use the new
	   window sizes. I also use the stored window sizes to update myself
	   (inboard driver) */
	PRINT0("Qg\n");
	FFLUSH();

	if (X11_ymax_saved > 0.0) {
	    /* use saved sizes if they're valid */
	    term->h_char = X11_hchar_saved;
	    term->v_char = X11_vchar_saved;
	    term->h_tic = term->v_tic = X11_vchar_saved / 2.5;
	    term->ymax = X11_ymax_saved;
	}
    }
#endif

    X11_linetype_last = LT_UNDEFINED;

    /* Force default font at start of plot */
    *X11_last_font_used = '\01';
    X11_set_default_font();
    X11_set_font("");
    X11_INVALIDATE_CURRENT_POSITION;
    X11_INVALIDATE_CURRENT_RGB;
}

/*
 * Send over a snapshot of the final axis scaling.
 * Jul 2024: Special case for logscale axes (log_base = axis->log_base)
 *           Special case for other nonlinear axes (log_base = -1)
 */
#ifdef USE_MOUSE
static void X11_send_axis_state(int axis_index)
{
    double log_base;
    struct axis *mousing_axis = &axis_array[axis_index];

    if (mousing_axis->log)
	log_base = mousing_axis->log_base;
    else if (nonlinear(mousing_axis))
	log_base = -1;
    else
	log_base = 0;
    if (mousing_axis->log && mousing_axis->linked_to_primary)
	mousing_axis = mousing_axis->linked_to_primary;
    PRINT5("S %2d %16.6g %14d %16.6g %16.6g\n", axis_index,
	    mousing_axis->min, mousing_axis->term_lower,
	    mousing_axis->term_scale, log_base);
}
#endif

TERM_PUBLIC void
X11_text()
{
     if (!X11_ipc)
 	return;

#ifdef USE_MOUSE
    /* Send over a snapshot of the final axis scaling so that subsequent
     * mouse events can be transformed into plot coordinates even though
     * the plot is no longer active.
     */
#ifdef PIPE_IPC
    if (ipc_back_fd >= 0)
#endif
	{
	/* Construct a mask showing which axes are active */
	int axis_mask = 0;
	AXIS_INDEX i;

	for (i = 0; i < NUMBER_OF_MAIN_VISIBLE_AXES; i++) {
	    if (axis_array[i].ticmode != NO_TICS)
		axis_mask |= (1 << i);
	}
	PRINT2("S %2d %d\n", -2, ALMOST2D);
	PRINT2("S %2d %d\n", -1, axis_mask);

	X11_send_axis_state(FIRST_X_AXIS);
	X11_send_axis_state(FIRST_Y_AXIS);
	X11_send_axis_state(SECOND_X_AXIS);
	X11_send_axis_state(SECOND_Y_AXIS);
    }
#endif

    PRINT0("E\n");
    FFLUSH();
#ifdef ULTRIX_KLUDGE
    PRINT0("E\n");
    FFLUSH();
#endif

    AFTER_TEXT;			/* kludge for crippled select */
}

TERM_PUBLIC void
X11_move(unsigned int x, unsigned int y)
{
    if (x == X11_xlast && y == X11_ylast)
	return;
    PRINT2("M%d %d\n", x, y);
    X11_xlast = x;
    X11_ylast = y;
}

TERM_PUBLIC void
X11_vector(unsigned int x, unsigned int y)
{
    if (x == X11_xlast && y == X11_ylast)
	return;
    PRINT2("V%d %d\n", x, y);
    X11_xlast = x;
    X11_ylast = y;
}

TERM_PUBLIC void
X11_pointsize(double ps)
{
    if (ps < 0)
	ps = 1;
    PRINT2("P-2 %d %d\n",	/* size of point symbols */
	   (int) (4096.0 * ps), (int) (4096.0 * ps));
}

TERM_PUBLIC void
X11_linewidth(double lw)
{
    PRINT1("W%d\n", (int) (lw * X11_linewidth_multiplier + 0.5));
    X11_linetype_last = LT_UNDEFINED;	/* Force linetype to be set again, including width */
}

TERM_PUBLIC void
X11_linetype(int lt)
{
    if (X11_linetype_last == lt)
	return;

    PRINT1("L%d\n", lt);
    X11_INVALIDATE_CURRENT_RGB;
    X11_linetype_last = lt;
}

TERM_PUBLIC void
X11_dashtype(int type, t_dashtype *custom_dash_type)
{
    int len;

    switch (type) {

    case DASHTYPE_SOLID:
	X11_linetype(LT_SOLID);
	break;

    case DASHTYPE_AXIS:
	/* Currently handled elsewhere? */
	break;

    case DASHTYPE_CUSTOM:
	if (custom_dash_type) {
	    char pattern[DASHPATTERN_LENGTH+1];

	    for (len = 0; len < DASHPATTERN_LENGTH && custom_dash_type->pattern[len] > 0; len++) {
		int bit = custom_dash_type->pattern[len];
		/* NB: A-Z encodes 1-26 */
		pattern[len] = 'A' + GPMIN(bit, 26) - 1;
	    }
	    pattern[len] = '\0';
	    PRINT1("D%s\n", pattern);
	}
	break;

    default:
	/* Fall back to the built-in dash patterns indexed by linetype */
	if (type > 0)
	    X11_linetype( type % 5 );	/* However many patterns we put in gplt_x11.c */
	break;
    }
}

TERM_PUBLIC void
X11_put_text(unsigned int x, unsigned int y, const char str[])
{
    /* Only send the font change request to X11 if it really is a change */
    if (strcmp(X11_last_font_used,X11_next_font_used)) {
	strcpy(X11_last_font_used,X11_next_font_used);
	PRINT1("QF%s\n", X11_next_font_used);
    }
    PRINT3("T%d %d %s\n", x, y, str);
    X11_INVALIDATE_CURRENT_POSITION;
}

TERM_PUBLIC int
X11_text_angle(float ang)
{
    X11_textangle = ang;
    PRINT1("A%.2f\n", ang);
    return TRUE;
}

TERM_PUBLIC int
X11_justify_text(enum JUSTIFY mode)
{
    PRINT1("J%d\n", mode);
    X11_last_justification = mode;
    return (TRUE);
}

TERM_PUBLIC void
X11_point(unsigned int x, unsigned int y, int number)
{
    PRINT3("P%d %d %d\n", number, x, y);
    X11_INVALIDATE_CURRENT_POSITION;
}

TERM_PUBLIC int
X11_set_font(const char *fontname)
{
    PRINT1("QF%s\n", fontname?fontname:"");
    strncpy(X11_next_font_used,fontname?fontname:"",sizeof(X11_next_font_used)-1);
    return(TRUE);
}

static void
X11_set_default_font()
{
    PRINT1("QD%s\n",X11_default_font);
}

TERM_PUBLIC void
X11_fillbox(int style, unsigned int x, unsigned int y, unsigned int w, unsigned int h)
{
    if (X11_ipc) {
	PRINT5("F%d %u %u %u %u\n", style, x, y, w, h);
    }
    X11_INVALIDATE_CURRENT_POSITION;
}

#ifdef USE_MOUSE
TERM_PUBLIC void
X11_put_tmptext(int i, const char str[])
{
    if (X11_ipc) {
	PRINT2("t%d %s\n", i, str);
	FFLUSH();
    }
    X11_INVALIDATE_CURRENT_POSITION;
}

TERM_PUBLIC void
X11_set_ruler(int x, int y)
{
    if (X11_ipc) {
	PRINT2("r%d %d\n", x, y);
	FFLUSH();
    }
}

TERM_PUBLIC void
X11_set_cursor(int c, int x, int y)
{
    if (X11_ipc) {
	PRINT3("u%d %d %d\n", c, x, y);
	FFLUSH();
    }
}

TERM_PUBLIC void
X11_set_clipboard(const char s[])
{
    if (X11_ipc) {
	PRINT1("z%s\n", s);
	FFLUSH();
    }
}

#endif /* USE_MOUSE */


static void
transmit_gradient(gradient_struct *gradient, int cnt, int type)
{
    int i = 0;

    fprintf(X11_ipc, "%d %d", cnt, type);
    for(i=0; i<cnt; i++) {
        /* this %50 *must* match the corresponding %50 in gplt_x11.c  */
        if(i%50 == 0) {
	    fputs("\n", X11_ipc);
	    fflush(X11_ipc);
	}
	/* Send gradient entry as 0.1234RRGGBB */
	fprintf(X11_ipc, "%6.4f", gradient[i].pos);
	fprintf(X11_ipc, "%02x", (unsigned int)(255.*gradient[i].col.r + 0.5));
	fprintf(X11_ipc, "%02x", (unsigned int)(255.*gradient[i].col.g + 0.5));
	fprintf(X11_ipc, "%02x", (unsigned int)(255.*gradient[i].col.b + 0.5));
    }
    fputs("\n", X11_ipc);
}


TERM_PUBLIC int
X11_make_palette(t_sm_palette *palette)
{
    char colorMode;

    if(!palette) {
        return 0;
    }

    if(!X11_ipc) {
	    fprintf(stderr, "(X11_make_palette) 0 == X11_ipc\n");
	    return -1;
	}

    colorMode = palette->colorMode;
    if (colorMode == SMPAL_COLOR_MODE_VIRIDIS)
	colorMode = SMPAL_COLOR_MODE_GRADIENT;

    fprintf(X11_ipc, "%c %c %c %c %d\n",
	     X11_GR_MAKE_PALETTE, colorMode,
	     (char)(palette->positive), (char)(palette->cmodel),
	     palette->use_maxcolors);

    switch(palette->colorMode) {
    case SMPAL_COLOR_MODE_GRAY:
        fprintf(X11_ipc,"%g\n", palette->gamma);
        break;
    case SMPAL_COLOR_MODE_RGB:
        fprintf(X11_ipc, "%d %d %d\n", palette->formulaR,
		 palette->formulaG, palette->formulaB);
	break;
    case SMPAL_COLOR_MODE_GRADIENT:
    case SMPAL_COLOR_MODE_VIRIDIS:
        transmit_gradient(palette->gradient, palette->gradient_num, palette->gradient_type);
	break;
    case SMPAL_COLOR_MODE_CUBEHELIX:
    case SMPAL_COLOR_MODE_FUNCTIONS: {
        int cnt=0;
	gradient_struct *gradient;
	gradient = approximate_palette(palette, -1, -1, &cnt);
	transmit_gradient(gradient, cnt, SMPAL_GRADIENT_TYPE_SMOOTH);
	free(gradient);
	break;
    }
    default:
        fprintf(stderr, "%s:%d ooops: Unknown colorMode '%c'.\n",
		 __FILE__, __LINE__, (char)(palette->colorMode));
    }
    fflush(X11_ipc);
    return 0;
}


#if 0
/* The following are some handy little routines to keep around
 * in case one wants to do statistics on the binary data being
 * passed to a terminal function.
 */

int char_count[256];

void histogram(unsigned char c) {
    char_count[c] += 1;
}

void dump_histogram(void) {
  int i, sum;
  fprintf(stderr,"\n\n");
  for (i=0,sum=0; i<256; i++) {
    fprintf(stderr," %3.3d:%7.7d ", i, char_count[i]);
    sum += char_count[i];
  }
  fprintf(stderr,"\n\ntotal: %d\n\n",sum);
}
#endif

TERM_PUBLIC void
X11_set_color(t_colorspec *colorspec)
{
    if (colorspec->type == TC_RGB) {
	if (colorspec->lt != X11_rgblast) {
	    fputc(X11_GR_SET_RGBCOLOR, X11_ipc);
	    PRINT1("%6.6x\n", colorspec->lt & 0xffffff);
	    FFLUSH();
	}
	X11_rgblast = colorspec->lt;
	return;
    } else {
	X11_INVALIDATE_CURRENT_RGB;
    }

    if (colorspec->type == TC_LT) {
	fputc(X11_GR_SET_LINECOLOR, X11_ipc);
	PRINT1("%d\n", colorspec->lt);
	FFLUSH();
	return;
    }

    if (colorspec->type != TC_FRAC)
	return;

    {
    char *c_ptr;
    unsigned short byte_remaining;
    float fgray = colorspec->value;

    fputc(X11_GR_BINARY_COLOR, X11_ipc);
    byte_remaining = sizeof(fgray);
    c_ptr = (char *) &fgray;

    while (byte_remaining) {

	char c_tmp = *c_ptr++ - SET_COLOR_TRANSLATION_CHAR;

	byte_remaining--;

	if ( (c_tmp == '\n') || (c_tmp == SET_COLOR_CODE_CHAR) || (c_tmp == '\0') ) {
	    fputc(SET_COLOR_CODE_CHAR, X11_ipc);
	    c_tmp += 1;
	}
	fputc(c_tmp, X11_ipc);

    }

    fputc('\n', X11_ipc);
    FFLUSH();
    }

}

TERM_PUBLIC void
X11_filled_polygon(int points, gpiPoint *corners)
{
    int int_cache[2];
    int i_corner;
    unsigned short i_buffer;
    unsigned short byte_remaining;
    unsigned int point_remaining;

    /* Encode and transfer data to the pipe, one character at a time. */

    if (!points) return;

    fputc(X11_GR_BINARY_POLYGON, X11_ipc);
    i_buffer = BINARY_MAX_CHAR_PER_TRANSFER;

    /* First put number of points into the buffer.  Initialize the "cache"
     * to the number of points and the number of bytes to transfer to the
     * size of an int.  This will be the first transfer, so increase the
     * number of points by one.
     */
    int_cache[0] = points;
    int_cache[1] = corners->style;
    byte_remaining = 2*sizeof(int_cache[0]);
    point_remaining = points + 1;
    i_corner = 0;

    while (point_remaining) {

	unsigned char *uc_ptr = (unsigned char *) int_cache;

	while (byte_remaining) {

	    int sent_val;

	    unsigned char uc_tmp = *uc_ptr++ - FILLED_POLYGON_TRANSLATION_CHAR;
	    byte_remaining--;

	    if ( (uc_tmp == '\n') || (uc_tmp == FILLED_POLYGON_CODE_CHAR) || (uc_tmp == '\0') ) {
		uc_tmp += 1;
		sent_val = fputc(FILLED_POLYGON_CODE_CHAR, X11_ipc);
		if (sent_val != (int)FILLED_POLYGON_CODE_CHAR)
		    fprintf(stderr, "Bad character mapping %d -> %d\n", (int)FILLED_POLYGON_CODE_CHAR, sent_val);
		i_buffer--;
		if (!i_buffer) {
		    fputc('\n', X11_ipc);  FFLUSH();               /* End of chunk. */
		    i_buffer = BINARY_MAX_CHAR_PER_TRANSFER;
		    fputc(X11_GR_BINARY_POLYGON, X11_ipc);         /* Will be another chunk. */
		}
	    }

	    sent_val = fputc(uc_tmp, X11_ipc);
	    if (sent_val != (int)uc_tmp)
		fprintf(stderr, "Bad character mapping %d -> %d\n", (int)uc_tmp, sent_val);
	    i_buffer--;
	    if (!i_buffer) {
		fputc('\n', X11_ipc);  FFLUSH();                   /* End of chunk. */
		i_buffer = BINARY_MAX_CHAR_PER_TRANSFER;
		if (point_remaining || byte_remaining)
		    fputc(X11_GR_BINARY_POLYGON, X11_ipc);         /* Will be another chunk. */
	    }

	}

	byte_remaining = 2*sizeof(int);
	if (--point_remaining) {
	    int_cache[0] = corners[i_corner].x;
	    int_cache[1] = corners[i_corner].y;
	    i_corner++;
	}
    }

    /* Check if some characters were put in the buffer that need to be flushed. */
    if (i_buffer != BINARY_MAX_CHAR_PER_TRANSFER) {
	fputc('\n', X11_ipc);
	FFLUSH();
    }
    X11_INVALIDATE_CURRENT_POSITION;

    return;
}

TERM_PUBLIC void
ENHX11_boxed_text(unsigned int x, unsigned int y, int option)
{
    switch(option) {
    case TEXTBOX_INIT:
	/* Tell gnuplot_x11 to initialize the text bounding box */
	PRINT2("Tb%d %d\n", x, y);
	break;
    case TEXTBOX_OUTLINE:
	/* Tell gnuplot_x11 to draw the text bounding box */
	PRINT0("TB\n");
	break;
    case TEXTBOX_BACKGROUNDFILL:
	/* Tell gnuplot_x11 to fill the text bounding box */
	PRINT0("TF\n");
	break;
    case TEXTBOX_MARGINS:
	/* Tell gnuplot_x11 to change text margins */
	PRINT2("Tm%d %d\n", x, y);
	break;
    default:
	break;
    }
}

TERM_PUBLIC void
X11_modify_plots(unsigned int ops, int plotno)
{
    if (X11_ipc) {
	PRINT2("O%u %d\n", ops, plotno);
	FFLUSH();
    }
}


/*
 * Ethan A Merritt November 2003
 *	- Support for enhanced text mode
 *
 * PROBLEMS:
 *	- The default font must be scalable
 *	- Without more feedback from the outboard driver (gnuplot_x11) it is
 *	  hard to set up proper text rotation. The current approximation is so-so.
 *	- Right- and Center- justified text is problematic for the same reason.
 *	- The default font size is not really known, and font scaling
 *	  in general could be improved
 */

static TBOOLEAN ENHx11_opened_string;
static TBOOLEAN ENHx11_sizeonly = FALSE;
static TBOOLEAN ENHx11_show = TRUE;
static int ENHx11_overprint = 0;
static TBOOLEAN ENHx11_widthflag = TRUE;
static double ENHx11_base;
static double ENHx11_fontsize = 12;
static char  *ENHx11_font;

TERM_PUBLIC void
ENHX11_OPEN(
    char *fontname,
    double fontsize, double base,
    TBOOLEAN widthflag, TBOOLEAN showflag,
    int overprint)
{
    /* If the overprint code requests a save or request, that's all we do */
    if (overprint == 3) {
	PRINT2("Tp%d %d\n", 0, 0);
	return;
    } else if (overprint == 4) {
	PRINT2("Tr%d %d\n", 0, 0);
	return;
    }

    if (!ENHx11_opened_string) {
	ENHx11_opened_string = TRUE;
	enhanced_cur_text = &enhanced_text[0];
	ENHx11_font = fontname;
	ENHx11_fontsize = fontsize;
	ENHx11_base = base * 10;	/* FIXME - should this be v_char? v_tic? */
	ENHx11_show = showflag;
	ENHx11_overprint = overprint;
	ENHx11_widthflag = widthflag;
    }
}

/*
 * Write a string fragment and update current position.
 * We leave the real work up to gnuplot_x11!
 */
TERM_PUBLIC void
ENHX11_FLUSH()
{
    /* Send a command to print this string at current position */
    if (ENHx11_opened_string) {

	char tmpfont[128];	/* FIXME - there must be a better way! */
	*enhanced_cur_text = '\0';
	snprintf(tmpfont,127,"%s,%.1f", ENHx11_font, ENHx11_fontsize);
	tmpfont[127] = '\0';
	PRINT1("QF%s\n", tmpfont);
	*X11_last_font_used = '\01';

	if (!ENHx11_show || ENHx11_sizeonly)
	    PRINT3("Ts%d %d %s\n", 0, (int)ENHx11_base, enhanced_text);
	else if (ENHx11_overprint == 1)
	    PRINT3("Tc%d %d %s\n", 0, (int)ENHx11_base, enhanced_text);
	else if (!ENHx11_widthflag && ENHx11_overprint != 0)
	    PRINT3("To%d %d %s\n", 0, (int)ENHx11_base, enhanced_text);
	else
	    PRINT3("Tu%d %d %s\n", 0, (int)ENHx11_base, enhanced_text);

	ENHx11_opened_string = FALSE;
    }
}

TERM_PUBLIC void
ENHX11_put_text(unsigned int x, unsigned int y, const char *str)
{
    char *original_string = (char *)str;
    static char *initial_font = NULL;
    char *fontcopy = NULL;
    double firstpass_fontsize = X11_default_fontsize;
    int pass = 1;

    if (ignore_enhanced_text) {
	X11_put_text(x,y,str);
	return;
    }

    if (!strlen(str))
	return;

    /* if there are no magic characters, we should just be able
     * punt the string to X11_put_text()
     */
    if (!strpbrk(str, "{}^_@&~")) {
	X11_put_text(x,y,str);
	return;
    }

    /* set up the global variables needed by enhanced_recursion() */
    enhanced_fontscale = 1.25;
    ENHx11_opened_string = FALSE;
    strncpy(enhanced_escape_format,"%c",sizeof(enhanced_escape_format));

    /* Terrible, horrible, no good, very bad hack to fix wonky rotation of
     * enhanced text. This should be done, and done better, in gnuplot_x11.
     */
    X11_ycorrection = 0;
    if (X11_textangle != 0 && X11_last_justification != LEFT) {
	int length = (estimate_strlen(str, NULL) - 1)
		   * (double)term->h_char
		   * (double)ENHx11_fontsize / firstpass_fontsize;
	if (X11_last_justification == CENTRE)
		length = length/2;
	X11_ycorrection = length * sin(DEG2RAD * X11_textangle);
    }

    /* Tell gnuplot_x11 to set the current position to (x,y) */
	PRINT2("Tl%d %d\n", x, y - X11_ycorrection);

    /* Text justification requires two passes. During the first pass we */
    /* don't draw anything, we just measure the space it will take.     */
    if (X11_last_justification != LEFT) {
	PRINT1("J%d\n", LEFT);
	ENHx11_sizeonly = TRUE;
    }
PASS2:

    /* Make sure that we start out using the intended font.  	*/
	ENHx11_font = "";
	if (*X11_next_font_used) {
	    int sep = strcspn(X11_next_font_used,",");
	    free(initial_font);
	    initial_font = gp_strdup(X11_next_font_used);
	    initial_font[sep] = '\0';
	    sscanf(&(X11_next_font_used[sep+1]),"%lf",&firstpass_fontsize);
	    ENHx11_font = initial_font;
	    FPRINTF((stderr,"Pass %d setting font to %s,%g\n",pass,ENHx11_font,firstpass_fontsize));
	}

    /* Set the recursion going. We say to keep going until a
     * closing brace, but we don't really expect to find one.
     * If the return value is not the nul-terminator of the
     * string, that can only mean that we did find an unmatched
     * closing brace in the string. We increment past it (else
     * we get stuck in an infinite loop) and try again.
     */
    fontcopy = strdup(ENHx11_font);
    while (*(str = enhanced_recursion((char *)str, TRUE,
			fontcopy, firstpass_fontsize,
			0.0, TRUE, TRUE, 0))) {
	(term->enhanced_flush)();

	/* I think we can only get here if *str == '}' */
	    enh_err_check(str);

	if (!*++str)
	    break; /* end of string */

	/* else carry on and process the rest of the string */
    }
    free(fontcopy);
    X11_set_font("");

    /* Restore text justification flag after 2nd pass */
    if (pass > 1) {
	PRINT1("J%d\n", X11_last_justification);
	return;
    }

    /* In order to do text justification we need to do a second pass that */
    /* uses information stored by gnuplot_x11 during the first pass.      */
    if (X11_last_justification != LEFT) {
	ENHx11_sizeonly = FALSE;

	if (X11_last_justification == RIGHT) {
		PRINT2("Tj%d %d\n", x, y - X11_ycorrection);
	} else if (X11_last_justification == CENTRE) {
		PRINT2("Tk%d %d\n", x, y - X11_ycorrection);
	}
	str = original_string;
	pass = 2;
	goto PASS2;

    }
    X11_INVALIDATE_CURRENT_POSITION;

}

TERM_PUBLIC void
X11_layer(t_termlayer syncpoint)
{
    switch (syncpoint)
	{
	case TERM_LAYER_RESET:
	case TERM_LAYER_RESET_PLOTNO:
		if (multiplot)
			break;
	case TERM_LAYER_BEFORE_PLOT:
	case TERM_LAYER_AFTER_PLOT:
	case TERM_LAYER_BEGIN_KEYSAMPLE:
	case TERM_LAYER_END_KEYSAMPLE:
	case TERM_LAYER_BEFORE_ZOOM:
		PRINT1("Y%d\n", syncpoint);
		break;
	default:
		break;
    }
}

TERM_PUBLIC void
X11_send_endianess(void)
{
    /* Place an integer 1 in the pipe and see if the byte order agrees
     * with program on the other side.  Information used for routines
     * having encoded binary data.
     */
    unsigned short tmp = (unsigned short) ENDIAN_VALUE;
    fputc(X11_GR_CHECK_ENDIANESS, X11_ipc);
    fputc(((char *)&tmp)[0], X11_ipc);
    fputc(((char *)&tmp)[1], X11_ipc);
    fputc('\n', X11_ipc);
    fflush(X11_ipc);
}


TERM_PUBLIC void
X11_image(unsigned int M, unsigned int N, coordval *image, gpiPoint *corner, t_imagecolor color_mode)
{

    /* Avoid using floats or formatted I/O.  That's too slow for image data.
     *
     * To avoid using floats, we assume that the maximum resolution of the
     * color plane of the x11 device is 16 bits.  (Not an unreasonable
     * assumption.)  So we convert coordval to unsigned short and then
     * send those.  The driver then shifts these unsigned short values to
     * the right to match the size of its palette.
     *
     * The driver uses the '\n' character to mean end of command.  When the
     * core routine sees the '\n', it stops copying the remaining characters
     * that are in the buffer.  Therefore, we need to hide the '\n' character
     * from the data stream.  We also need to hide the '\0' because the core
     * routine also does an 'strcpy'.  So the scheme is as follows:
     *
     *   On the driver side, if the char CODE_WORD is found it
     *   should be ignored and replaced with the char that
     *   follows it, but subtract one from that value.
     *
     * This means that there exists the chance of the data stream enlarging
     * by a factor of two.  So the algorithm must make sure to keep the
     * number of bytes transferred less than the buffer length.
     *
     * Also note that the '\0' character is often prevalent in 16 bit image
     * data where the palette may be 8 bits or less.  For this reason, a
     * translation is first done on the character to make the '\0' occur less
     * often.  In this way, less amount of data is sent and fewer command
     * lines are used to store an image.
     */

    unsigned short i_buffer;
    unsigned int coord_remaining;
    coordval *coord_ptr;

    /* Use formatted I/O to transfer image information.  Hexadecimal uses less characters.
     *
     * Note that X11 has different frame of reference (top left origin) than does Gnuplot
     * (bottom left origin)
     */
    fputc(X11_GR_IMAGE, X11_ipc);
    fprintf(X11_ipc,"%x %x %x %x %x %x %x %x %x %x %x\n", M, N, corner[0].x, corner[0].y, corner[1].x,
      corner[1].y, corner[2].x, corner[2].y, corner[3].x, corner[3].y, color_mode);
    FFLUSH();

    /* Encode and transfer data to the pipe, one character at a time. */

    coord_remaining = M*N;
    if (color_mode == IC_RGB)
	coord_remaining *= 3;
    else if (color_mode == IC_RGBA)
	coord_remaining *= 4;

    if (!coord_remaining) return;

    fputc(X11_GR_IMAGE, X11_ipc);
    i_buffer = BINARY_MAX_CHAR_PER_TRANSFER;
    coord_ptr = image;

    while (coord_remaining) {

	unsigned short us_tmp;
	unsigned short byte_remaining;
	unsigned char *uc_ptr;

	/* Convert coordinate value to an unsigned short.
	 * Palette and RGB values are on scale [0:1] at this point,
	 * but alpha values run from [0:255].  Why? I don't know.
	 */
	if (color_mode == IC_RGBA && !((coord_remaining+3) % 4))
	    us_tmp = (*coord_ptr++);
	else
	    us_tmp = (unsigned short) ((*coord_ptr++)*IMAGE_PALETTE_VALUE_MAX + 0.5);
	coord_remaining--;

	byte_remaining = sizeof(us_tmp);
	uc_ptr = (unsigned char *) &us_tmp;

	while (byte_remaining) {

	    int sent_val;

	    unsigned char uc_tmp = *uc_ptr++ - IMAGE_TRANSLATION_CHAR;
	    byte_remaining--;

	    if ((uc_tmp == '\n') || (uc_tmp == IMAGE_CODE_CHAR) || (uc_tmp == '\0') ) {
		uc_tmp += 1;
		sent_val = fputc(IMAGE_CODE_CHAR, X11_ipc);
		if (sent_val != (int)IMAGE_CODE_CHAR)
		    fprintf(stderr, "Bad character mapping %d -> %d\n", (int)IMAGE_CODE_CHAR, sent_val);
		i_buffer--;
		if (!i_buffer) {
		    fputc('\n', X11_ipc);  FFLUSH();               /* End of chunk. */
		    i_buffer = BINARY_MAX_CHAR_PER_TRANSFER;
		    fputc(X11_GR_IMAGE, X11_ipc);                  /* Will be another chunk. */
		}
	    }

	    sent_val = fputc(uc_tmp, X11_ipc);
	    if (sent_val != (int)uc_tmp)
		fprintf(stderr, "Bad character mapping %d -> %d\n", (int)uc_tmp, sent_val);
	    i_buffer--;
	    if (!i_buffer) {
		fputc('\n', X11_ipc);  FFLUSH();                   /* End of chunk. */
		i_buffer = BINARY_MAX_CHAR_PER_TRANSFER;
		if (coord_remaining || byte_remaining)
		    fputc(X11_GR_IMAGE, X11_ipc);                  /* Will be another chunk. */
	    }

	}

    }

    /* Check if some characters were put in the buffer that need to be flushed. */
    if (i_buffer != BINARY_MAX_CHAR_PER_TRANSFER)
	fputc('\n', X11_ipc);

    /* indicate the end of the image chunk sequence */
    fputc( X11_GR_IMAGE_END, X11_ipc);
    fputc( '\n', X11_ipc);
    FFLUSH();

    X11_INVALIDATE_CURRENT_POSITION;
    return;
}

#endif /* TERM_BODY */

#ifdef TERM_TABLE

TERM_TABLE_START(x11_driver)
    "x11", "X11 Window System interactive terminal",
    X11_XMAX, X11_YMAX, X11_VCHAR, X11_HCHAR,
    X11_VTIC, X11_HTIC, X11_options, X11_init, X11_reset,
    X11_text, null_scale, X11_graphics, X11_move, X11_vector,
    X11_linetype, ENHX11_put_text, X11_text_angle,
    X11_justify_text, X11_point, do_arrow, X11_set_font,
    X11_pointsize,
    TERM_CAN_MULTIPLOT|TERM_INIT_ON_REPLOT|TERM_NO_OUTPUTFILE|TERM_CAN_DASH|TERM_ALPHA_CHANNEL|TERM_LINEWIDTH|TERM_ENHANCED_TEXT,
    X11_text /* suspend can use same routine */ , 0 /* resume */ ,
    X11_fillbox, X11_linewidth
#ifdef USE_MOUSE
    , X11_waitforinput, X11_put_tmptext, X11_set_ruler, X11_set_cursor, X11_set_clipboard
#endif
    , X11_make_palette, 0 /* X11_previous_palette */ ,
    X11_set_color, X11_filled_polygon
    , X11_image
    , ENHX11_OPEN, ENHX11_FLUSH, do_enh_writec
    , X11_layer
    , NULL, 0.0, NULL /* path, tscale, hypertext */
    , ENHX11_boxed_text
    , X11_modify_plots
    , X11_dashtype
TERM_TABLE_END(x11_driver)

#undef LAST_TERM
#define LAST_TERM x11_driver

#endif				/* TERM_TABLE */
#endif				/* TERM_PROTO_ONLY */


#ifdef TERM_HELP
START_HELP(x11)
"1 x11",
"?commands set terminal x11",
"?set terminal x11",
"?set term x11",
"?terminal x11",
"?term x11",
"?x11",
"?X11",
"",
" Syntax:",
"    set terminal x11 {<n> | window \"<string>\"}",
"                     {title \"<string>\"}",
"                     {{no}enhanced} {font <fontspec>}",
"                     {linewidth LW}",
"                     {{no}persist} {{no}raise} {{no}ctrlq}",
"                     {{no}replotonresize}",
"                     {close}",
"                     {size XX,YY} {position XX,YY}",
"    set terminal x11 {reset}",
"",
" Multiple plot windows are supported: `set terminal x11 <n>` directs the",
" output to plot window number n.  If n is not 0, the terminal number will be",
" appended to the window title (unless a title has been supplied manually)",
" and the icon will be labeled `Gnuplot <n>`.  The active window may be",
" distinguished by a change in cursor (from default to crosshair).",
"",
" The `x11` terminal can connect to X windows previously created by an outside",
" application via the option `window` followed by a string containing the",
" X ID for the window in hexadecimal format.  Gnuplot uses that external X",
" window as a container since X does not allow for multiple clients selecting",
" the ButtonPress event.  In this way, gnuplot's mouse features work within",
" the contained plot window.",
"",
"    set term x11 window \"220001e\"",
"",
" The x11 terminal supports enhanced text mode (see `enhanced`), subject",
" to the available fonts. In order for font size commands embedded in text",
" to have any effect, the default x11 font must be scalable. Thus the first",
" example below will work as expected, but the second will not.",
"",
"    set term x11 enhanced font \"arial,15\" ",
"    set title '{/=20 Big} Medium {/=5 Small}' ",
"",
"    set term x11 enhanced font \"terminal-14\" ",
"    set title '{/=20 Big} Medium {/=5 Small}' ",
"",
" Plot windows remain open even when the `gnuplot` driver is changed to a",
" different device.  A plot window can be closed by pressing the letter q",
" while that window has input focus, or by choosing `close` from a window",
" manager menu.  All plot windows can be closed by specifying `reset`, which",
" actually terminates the subprocess which maintains the windows (unless",
" `-persist` was specified).  The `close` command can be used to close",
" individual plot windows by number.  However, after a `reset`, those plot",
" windows left due to persist cannot be closed with the command `close`.",
" A `close` without a number closes the current active plot window.",
"",
" The gnuplot outboard driver, gnuplot_x11, is searched in a default place",
" chosen when the program is compiled.  You can override that by defining",
" the environment variable GNUPLOT_DRIVER_DIR to point to a different",
" location.",
"",
" Plot windows will automatically be closed at the end of the session",
" unless the `-persist` option was given.",
"",
" The options `persist` and `raise` are unset by default, which means that",
" the defaults (persist == no and raise == yes) or the command line options",
" -persist / -raise or the Xresources are taken.  If [no]persist or",
" [no]raise are specified, they will override command line options and",
" Xresources.  Setting one of these options takes place immediately, so",
" the behaviour of an already running driver can be modified.  If the window",
" does not get raised, see discussion in `raise`.",
"",
" The option `replotonresize` (active by default) replots the data when the",
" plot window is resized. Without this option, the even-aspect-ratio scaling",
" may result in the plot filling only part of the window after resizing.",
" With this option, gnuplot does a full replot on each resize event, resulting",
" in better space utilization. This option is generally desirable, unless the",
" potentially CPU-intensive replotting during resizing is a concern. Replots",
" can be manually initiated with hotkey 'e' or the 'replot' command.",
  "",
" The option `title \"<title name>\"` will supply the title name of the window",
" for the current plot window or plot window <n> if a number is given.",
" Where (or if) this title is shown depends on your X window manager.",
"",
" The size option can be used to set the size of the plot window.  The",
" size option will only apply to newly created windows.",
"",
" The position option can be used to set the position of the plot window.  The",
" position option will only apply to newly created windows.",
"",
" The size or aspect ratio of a plot may be changed by resizing the `gnuplot`",
" window.",
"",
" Linewidths and pointsizes may be changed from within `gnuplot` with",
" `set linestyle`.",
"",
" For terminal type `x11`, `gnuplot` accepts (when initialized) the standard",
" X Toolkit options and resources such as geometry, font, and name from the",
" command line arguments or a configuration file.  See the X(1) man page",
" (or its equivalent) for a description of such options.",
"",
"=X resources",
" A number of other `gnuplot` options are available for the `x11` terminal.",
" These may be specified either as command-line options when `gnuplot` is",
" invoked or as resources in the configuration file \".Xdefaults\".  They are",
" set upon initialization and cannot be altered during a `gnuplot` session.",
" (except `persist` and `raise`)",
"2 x11_fonts",
"?commands set terminal x11 x11_fonts",
"?set terminal x11 x11_fonts",
"?set term x11 x11_fonts",
"?x11 x11_fonts",
"?x11_fonts",
"=fonts",
" Upon initial startup, the default font is taken from the X11 resources",
" as set in the system or user .Xdefaults file or on the command line.",
"",
" Example:",
"       gnuplot*font: lucidasans-bold-12",
" A new default font may be specified to the x11 driver from inside",
" gnuplot using",
"      `set term x11 font \"<fontspec>\"`",
" The driver first queries the X-server for a font of the exact name given.",
" If this query fails, then it tries to interpret <fontspec> as",
" \"<font>,<size>,<slant>,<weight>\" and to construct a full X11 font name",
" of the form",
"       -*-<font>-<weight>-<s>-*-*-<size>-*-*-*-*-*-<encoding>",
"",
"  <font> is the base name of the font (e.g. Times or Symbol)",
"  <size> is the point size (defaults to 12 if not specified)",
"  <s> is 'i' if <slant>==\"italic\" 'o' if <slant>==\"oblique\" 'r' otherwise",
"  <weight> is 'medium' or 'bold' if explicitly requested, otherwise '*'",
"  <encoding> is set based on the current character set.",
" So `set term x11 font \"arial,15,italic\"` will be translated to",
" -*-arial-*-i-*-*-15-*-*-*-*-*-iso8859-1 (assuming default encoding).",
" The <size>, <slant>, and <weight> specifications are all optional.",
" If you do not specify <slant> or <weight> then you will get whatever font ",
" variant the font server offers first.",
" You may set a default encoding via the corresponding X11 resource. E.g.",
"       gnuplot*encoding: iso8859-15",
" The driver also recognizes some common PostScript font names and",
" replaces them with possible X11 or TrueType equivalents.",
" This same sequence is used to process font requests from `set label`.",
"",
" If your gnuplot was built with configuration option --enable-x11-mbfonts,",
" you can specify multi-byte fonts by using the prefix \"mbfont:\" on the font",
" name. An additional font may be given, separated by a semicolon.",
" Since multi-byte font encodings are interpreted according to the locale",
" setting, you must make sure that the environmental variable LC_CTYPE is set",
" to some appropriate locale value such as ja_JP.eucJP, ko_KR.EUC, or zh_CN.EUC.",
"",
" Example:",
"       set term x11 font 'mbfont:kana14;k14'",
"             # 'kana14' and 'k14' are Japanese X11 font aliases, and ';'",
"             # is the separator of font names.",
"       set term x11 font 'mbfont:fixed,16,r,medium'",
"             # <font>,<size>,<slant>,<weight> form is also usable.",
"       set title '(mb strings)' font 'mbfont:*-fixed-medium-r-normal--14-*'",
"",
" The same syntax applies to the default font in Xresources settings,",
" for example,",
"       gnuplot*font: \\",
"           mbfont:-misc-fixed-medium-r-normal--14-*-*-*-c-*-jisx0208.1983-0",
"",
" If gnuplot is built with --enable-x11-mbfonts, you can use two special",
" PostScript font names 'Ryumin-Light-*' and 'GothicBBB-Medium-*' (standard",
" Japanese PS fonts) without the prefix \"mbfont:\".",
"",
"2 command-line_options",
"?commands set terminal x11 command-line-options",
"?set terminal x11 command-line-options",
"?set term x11 command-line-options",
"?x11 command-line-options",
" In addition to the X Toolkit options, the following options may be specified",
" on the command line when starting `gnuplot` or as resources in your",
" \".Xdefaults\" file (note that `raise` and `persist` can be overridden",
" later by `set term x11 [no]raise [no]persist)`:",
"@start table - first is interactive cleartext form",
"  `-mono`     forces monochrome rendering on color displays.",
"  `-gray`     requests grayscale rendering on grayscale or color displays.",
"              (Grayscale displays receive monochrome rendering by default.)",
"  `-clear`    requests that the window be cleared momentarily before a",
"              new plot is displayed.",
"  `-tvtwm`    requests that geometry specifications for position of the",
"              window be made relative to the currently displayed portion",
"              of the virtual root.",
"  `-raise`    raises plot window after each plot",
"  `-noraise`  does not raise plot window after each plot",
#ifdef USE_MOUSE
"  `-ctrlq   ` closes window on ctrl-q rather than q",
#endif
"  `-persist`  plot windows survive after main gnuplot program exits",
"#\\begin{tabular}{|cl|} \\hline",
"#`-mono`     & forces monochrome rendering on color displays.\\\\",
"#`-gray`     & requests grayscale rendering on grayscale or color displays.\\\\",
"#            & (Grayscale displays receive monochrome rendering by default.) \\\\",
"#`-clear`    & requests that the window be cleared momentarily before a\\\\",
"#            & new plot is displayed. \\\\",
"#`-tvtwm`    & requests that geometry specifications for position of the\\\\",
"#            & window be made relative to the currently displayed portion\\\\",
"#            & of the virtual root. \\\\",
"#`-raise`    & raises plot window after each plot. \\\\",
"#`-noraise`  & does not raise plot window after each plot. \\\\",
"#`-persist`  & plot windows survive after main gnuplot program exits. \\\\",
"%c l .",
"%`-mono`@forces monochrome rendering on color displays.",
"%`-gray`@requests grayscale rendering on grayscale or color displays.",
"%       @(Grayscale displays receive monochrome rendering by default.)",
"%`-clear`@requests that the window be cleared momentarily before a",
"%        @new plot is displayed.",
"%`-tvtwm`@requests that geometry specifications for position of the",
"%        @window be made relative to the currently displayed portion",
"%        @of the virtual root.",
"%`-raise`@raises plot window after each plot",
"%`-noraise`@does not raise plot window after each plot",
#ifdef USE_MOUSE
"%`-novevents`@does not process mouse and key events",
"%`-ctrlq`@closes window on ctrl-q rather than q",
#endif
"%`-persist`@plot windows survive after main gnuplot program exits",
"@end table",
"=X resources",
" The options are shown above in their command-line syntax.  When entered as",
" resources in \".Xdefaults\", they require a different syntax.",
"",
" Example:",
"       gnuplot*gray:  on",
"       gnuplot*ctrlq: on",
"",
" `gnuplot` also provides a command line option (`-pointsize <v>`) and a",
" resource, `gnuplot*pointsize: <v>`, to control the size of points plotted",
" with the `points` plotting style.  The value `v` is a real number (greater",
" than 0 and less than or equal to ten) used as a scaling factor for point",
" sizes.  For example, `-pointsize 2` uses points twice the default size, and",
" `-pointsize 0.5` uses points half the normal size.",
"",
" The `-ctrlq` switch changes the hot-key that closes a plot window from `q`",
" to `<ctrl>q`. This is useful is you are using the keystroke-capture feature",
" `pause mouse keystroke`, since it allows the character `q` to be captured",
" just as all other alphanumeric characters. The `-ctrlq` switch similarly",
" replaces the <space> hot-key with <ctrl><space> for the same reason.",
"",
"2 color_resources",
"?set terminal x11 color_resources",
"?set term x11 color_resources",
"?x11 color_resources",
"=X resources",
" NB: THIS SECTION IS LARGELY IRRELEVANT IN GNUPLOT VERSION 5",
" The X11 terminal honors the following resources (shown here with their",
" default values) or the greyscale resources.  The values may be color names",
" as listed in the X11 rgb.txt file on your system, hexadecimal RGB color",
" specifications (see X11 documentation), or a color name followed by a comma",
" and an `intensity` value from 0 to 1.  For example, `blue, 0.5` means a half",
" intensity blue.",
"@start table - first is interactive cleartext form",
"  gnuplot*background:  white",
"  gnuplot*textColor:   black",
"  gnuplot*borderColor: black",
"  gnuplot*axisColor:   black",
"  gnuplot*line1Color:  red",
"  gnuplot*line2Color:  green",
"  gnuplot*line3Color:  blue",
"  gnuplot*line4Color:  magenta",
"  gnuplot*line5Color:  cyan",
"  gnuplot*line6Color:  sienna",
"  gnuplot*line7Color:  orange",
"  gnuplot*line8Color:  coral",
"#\\begin{tabular}{|cl|} \\hline",
"#&gnuplot*background: white\\\\",
"#&gnuplot*textColor: black\\\\",
"#&gnuplot*borderColor: black\\\\",
"#&gnuplot*axisColor: black\\\\",
"#&gnuplot*line1Color: red\\\\",
"#&gnuplot*line2Color: green\\\\",
"#&gnuplot*line3Color: blue\\\\",
"#&gnuplot*line4Color: magenta\\\\",
"#&gnuplot*line5Color: cyan\\\\",
"#&gnuplot*line6Color: sienna\\\\",
"#&gnuplot*line7Color: orange\\\\",
"#&gnuplot*line8Color: coral\\\\",
"%c l .",
"%@gnuplot*background: white",
"%@gnuplot*textColor: black",
"%@gnuplot*borderColor: black",
"%@gnuplot*axisColor: black",
"%@gnuplot*line1Color: red",
"%@gnuplot*line2Color: green",
"%@gnuplot*line3Color: blue",
"%@gnuplot*line4Color: magenta",
"%@gnuplot*line5Color: cyan",
"%@gnuplot*line6Color: sienna",
"%@gnuplot*line7Color: orange",
"%@gnuplot*line8Color: coral",
"@end table",
"",
" The command-line syntax for these is simple only for background,",
" which maps directly to the usual X11 toolkit option \"-bg\".  All",
" others can only be set on the command line by use of the generic",
" \"-xrm\" resource override option",
"",
" Examples:",
"",
"       gnuplot -background coral",
" to change the background color.",
"",
"       gnuplot -xrm 'gnuplot*line1Color:blue'",
" to override the first linetype color.",
"",
"2 grayscale_resources",
"?commands set terminal x11 grayscale_resources",
"?set terminal x11 grayscale_resources",
"?set term x11 grayscale_resources",
"?x11 grayscale_resources",
"?grayscale_resources",
"=X resources",
" When `-gray` is selected, `gnuplot` honors the following resources for",
" grayscale or color displays (shown here with their default values).  Note",
" that the default background is black.",
"@start table - first is interactive cleartext form",
"  gnuplot*background: black",
"  gnuplot*textGray:   white",
"  gnuplot*borderGray: gray50",
"  gnuplot*axisGray:   gray50",
"  gnuplot*line1Gray:  gray100",
"  gnuplot*line2Gray:  gray60",
"  gnuplot*line3Gray:  gray80",
"  gnuplot*line4Gray:  gray40",
"  gnuplot*line5Gray:  gray90",
"  gnuplot*line6Gray:  gray50",
"  gnuplot*line7Gray:  gray70",
"  gnuplot*line8Gray:  gray30",
"#\\begin{tabular}{|cl|} \\hline",
"#&gnuplot*background: black\\\\",
"#&gnuplot*textGray: white\\\\",
"#&gnuplot*borderGray: gray50\\\\",
"#&gnuplot*axisGray: gray50\\\\",
"#&gnuplot*line1Gray: gray100\\\\",
"#&gnuplot*line2Gray: gray60\\\\",
"#&gnuplot*line3Gray: gray80\\\\",
"#&gnuplot*line4Gray: gray40\\\\",
"#&gnuplot*line5Gray: gray90\\\\",
"#&gnuplot*line6Gray: gray50\\\\",
"#&gnuplot*line7Gray: gray70\\\\",
"#&gnuplot*line8Gray: gray30\\\\",
"%c l .",
"%@gnuplot*background: black",
"%@gnuplot*textGray: white",
"%@gnuplot*borderGray: gray50",
"%@gnuplot*axisGray: gray50",
"%@gnuplot*line1Gray: gray100",
"%@gnuplot*line2Gray: gray60",
"%@gnuplot*line3Gray: gray80",
"%@gnuplot*line4Gray: gray40",
"%@gnuplot*line5Gray: gray90",
"%@gnuplot*line6Gray: gray50",
"%@gnuplot*line7Gray: gray70",
"%@gnuplot*line8Gray: gray30",
"@end table",
"",
"2 line_resources",
"?set terminal x11 line_resources",
"?set term x11 line_resources",
"?x11 line_resources",
"=X resources",
" NB: THIS SECTION IS LARGELY IRRELEVANT IN GNUPLOT VERSION 5",
" `gnuplot` honors the following resources for setting the width (in pixels) of",
" plot lines (shown here with their default values.)  0 or 1 means a minimal",
" width line of 1 pixel width.  A value of 2 or 3 may improve the appearance of",
" some plots.",
"@start table - first is interactive cleartext form",
"  gnuplot*borderWidth: 1",
"  gnuplot*axisWidth:   0",
"  gnuplot*line1Width:  0",
"  gnuplot*line2Width:  0",
"  gnuplot*line3Width:  0",
"  gnuplot*line4Width:  0",
"  gnuplot*line5Width:  0",
"  gnuplot*line6Width:  0",
"  gnuplot*line7Width:  0",
"  gnuplot*line8Width:  0",
"#\\begin{tabular}{|cl|} \\hline",
"#&gnuplot*borderWidth: 1\\\\",
"#&gnuplot*axisWidth: 0\\\\",
"#&gnuplot*line1Width: 0\\\\",
"#&gnuplot*line2Width: 0\\\\",
"#&gnuplot*line3Width: 0\\\\",
"#&gnuplot*line4Width: 0\\\\",
"#&gnuplot*line5Width: 0\\\\",
"#&gnuplot*line6Width: 0\\\\",
"#&gnuplot*line7Width: 0\\\\",
"#&gnuplot*line8Width: 0\\\\",
"%c l .",
"%@gnuplot*borderWidth: 1",
"%@gnuplot*axisWidth: 0",
"%@gnuplot*line1Width: 0",
"%@gnuplot*line2Width: 0",
"%@gnuplot*line3Width: 0",
"%@gnuplot*line4Width: 0",
"%@gnuplot*line5Width: 0",
"%@gnuplot*line6Width: 0",
"%@gnuplot*line7Width: 0",
"%@gnuplot*line8Width: 0",
"@end table",
"",
" `gnuplot` honors the following resources for setting the dash style used for",
" plotting lines.  0 means a solid line.  A two-digit number `jk` (`j` and `k`",
" are >= 1 and <= 9) means a dashed line with a repeated pattern of `j` pixels",
" on followed by `k` pixels off.  For example, '16' is a dotted line with one",
" pixel on followed by six pixels off.  More elaborate on/off patterns can be",
" specified with a four-digit value.  For example, '4441' is four on, four off,",
" four on, one off.  The default values shown below are for monochrome displays",
" or monochrome rendering on color or grayscale displays.",
" Color displays default to dashed:off ",
"@start table - first is interactive cleartext form",
"  gnuplot*dashed:       off",
"  gnuplot*borderDashes:   0",
"  gnuplot*axisDashes:    16",
"  gnuplot*line1Dashes:    0",
"  gnuplot*line2Dashes:   42",
"  gnuplot*line3Dashes:   13",
"  gnuplot*line4Dashes:   44",
"  gnuplot*line5Dashes:   15",
"  gnuplot*line6Dashes: 4441",
"  gnuplot*line7Dashes:   42",
"  gnuplot*line8Dashes:   13",
"#\\begin{tabular}{|cl|} \\hline",
"#&gnuplot*dashed: off\\\\",
"#&gnuplot*borderDashes: 0\\\\",
"#&gnuplot*axisDashes: 16\\\\",
"#&gnuplot*line1Dashes: 0\\\\",
"#&gnuplot*line2Dashes: 42\\\\",
"#&gnuplot*line3Dashes: 13\\\\",
"#&gnuplot*line4Dashes: 44\\\\",
"#&gnuplot*line5Dashes: 15\\\\",
"#&gnuplot*line6Dashes: 4441\\\\",
"#&gnuplot*line7Dashes: 42\\\\",
"#&gnuplot*line8Dashes: 13\\\\",
"%c l .",
"%@gnuplot*dashed: off",
"%@gnuplot*borderDashes: 0",
"%@gnuplot*axisDashes: 16",
"%@gnuplot*line1Dashes: 0",
"%@gnuplot*line2Dashes: 42",
"%@gnuplot*line3Dashes: 13",
"%@gnuplot*line4Dashes: 44",
"%@gnuplot*line5Dashes: 15",
"%@gnuplot*line6Dashes: 4441",
"%@gnuplot*line7Dashes: 42",
"%@gnuplot*line8Dashes: 13",
"@end table"
, "",
"2 x11 pm3d_resources",
"?set terminal x11 pm3d_resources",
"?set term x11 pm3d_resources",
"?x11 pm3d_resources",
"?x11 pm3d",
"=X resources",
" NB: THIS SECTION IS LARGELY IRRELEVANT IN GNUPLOT VERSION 5",
"",
" By default `gnuplot` uses the default visual of the screen. The number of",
" colors which can be allocated depends on the visual class chosen. On a",
" visual class with a depth > 12bit, gnuplot starts with a maximal number",
" of 0x200 colors.  On a visual class with a depth > 8bit (but <= 12 bit)",
" the maximal number of colors is 0x100, on <= 8bit displays the maximum",
" number of colors is 240 (16 are left for line colors).",
"",
" Gnuplot first starts to allocate the maximal number of colors as stated",
" above.  If this fails, the number of colors is reduced by the factor 2",
" until gnuplot gets all colors which are requested. If dividing `maxcolors`",
" by 2 repeatedly results in a number which is smaller than `mincolors`",
" `gnuplot` tries to install a private colormap. In this case the window",
" manager is responsible for swapping colormaps when the pointer is moved",
" in and out the x11 driver's window.",
"",
" The default for `mincolors` is maxcolors / (num_colormaps > 1 ? 2 : 8),",
" where num_colormaps is the number of colormaps which are currently used",
" by gnuplot (usually 1, if only one x11 window is open).",
"",
"2 x11 other_resources",
"?commands set terminal x11 other_resources",
"?set terminal x11 other_resources",
"?set term x11 other_resources",
"?x11 other_resources",
"=X resources",
" By default the contents of the current plot window are exported to the X11",
" clipboard in response to X events in the window. Setting the resource",
" 'gnuplot*exportselection' to 'off' or 'false' will disable this.",
"",
" By default text rotation is done using a method that is fast, but can",
" corrupt nearby colors depending on the background.  If this is a problem,",
" you can set the resource 'gnuplot.fastrotate' to 'off'",
"",
"@start table - other x11 resources",
"  gnuplot*exportselection:  off",
"  gnuplot*fastrotate:  on",
"  gnuplot*ctrlq:  off",
"#\\begin{tabular}{|cl|} \\hline",
"#&gnuplot*exportselection:  off\\\\",
"#&gnuplot*fastrotate:  on\\\\",
"#&gnuplot*ctrlq:  off\\\\",
"%c l .",
"%@gnuplot*exportselection:  off",
"%@gnuplot*fastrotate:  on",
"%@gnuplot*ctrlq:  off",
"@end table"

END_HELP(x11)
#endif				/* TERM_HELP */