summaryrefslogtreecommitdiff
path: root/src/androidterm.c
blob: 2e4ee64f39058be40d7fa06708fde08667b6412b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
/* Communication module for Android terminals.

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <semaphore.h>

#include "lisp.h"
#include "androidterm.h"
#include "keyboard.h"
#include "blockinput.h"
#include "android.h"
#include "buffer.h"
#include "window.h"
#include "textconv.h"
#include "coding.h"
#include "pdumper.h"

/* This is a chain of structures for all the X displays currently in
   use.  */

struct android_display_info *x_display_list;



/* Android terminal interface functions.  */

#ifndef ANDROID_STUBIFY

#include <android/log.h>

/* Non-zero means that a HELP_EVENT has been generated since Emacs
   start.  */

static bool any_help_event_p;

/* Counters for tallying up scroll wheel events if
   mwheel_coalesce_scroll_events is true.  */

static double wheel_event_x, wheel_event_y;

enum
  {
    ANDROID_EVENT_NORMAL,
    ANDROID_EVENT_GOTO_OUT,
    ANDROID_EVENT_DROP,
  };

/* Find the frame whose window has the identifier WDESC.

   This is like x_window_to_frame in xterm.c, except that DPYINFO may
   be NULL, as there is only at most one Android display, and is only
   specified in order to stay consistent with X.  */

static struct frame *
android_window_to_frame (struct android_display_info *dpyinfo,
			 android_window wdesc)
{
  Lisp_Object tail, frame;
  struct frame *f;

  if (wdesc == ANDROID_NONE)
    return NULL;

  FOR_EACH_FRAME (tail, frame)
    {
      f = XFRAME (frame);

      if (!FRAME_ANDROID_P (f))
	continue;

      if (FRAME_ANDROID_WINDOW (f) == wdesc)
        return f;
    }

  return NULL;
}

static void
android_clear_frame (struct frame *f)
{
  /* Clearing the frame will erase any cursor, so mark them all as no
     longer visible.  */
  mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
  android_clear_window (FRAME_ANDROID_DRAWABLE (f));
}

static void
android_show_hourglass (struct frame *f)
{
  struct android_output *x;

  /* This isn't implemented like X because a window brings alongside
     too many unneeded resources.  */

  x = FRAME_ANDROID_OUTPUT (f);

  /* If the hourglass window is mapped inside a popup menu, input
     could be lost if the menu is popped down and the grab is
     relinquished, but the hourglass window is still up.  Just
     avoid displaying the hourglass at all while popups are
     active.  */

  if (popup_activated ())
    return;

  x->hourglass = true;

  if (!f->pointer_invisible)
    android_define_cursor (FRAME_ANDROID_WINDOW (f),
			   x->hourglass_cursor);
}

static void
android_hide_hourglass (struct frame *f)
{
  struct android_output *x;

  x = FRAME_ANDROID_OUTPUT (f);
  x->hourglass = false;

  if (!f->pointer_invisible)
    android_define_cursor (FRAME_ANDROID_WINDOW (f),
			   x->current_cursor);
}

static void
android_flash (struct frame *f)
{
  struct android_gc *gc;
  struct android_gc_values values;
  int rc;
  fd_set fds;

  block_input ();

  values.function = ANDROID_GC_XOR;
  values.foreground = (FRAME_FOREGROUND_PIXEL (f)
		       ^ FRAME_BACKGROUND_PIXEL (f));

  gc = android_create_gc ((ANDROID_GC_FUNCTION
			   | ANDROID_GC_FOREGROUND),
			  &values);

  /* Get the height not including a menu bar widget.  */
  int height = FRAME_PIXEL_HEIGHT (f);
  /* Height of each line to flash.  */
  int flash_height = FRAME_LINE_HEIGHT (f);
  /* These will be the left and right margins of the rectangles.  */
  int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
  int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
  int width = flash_right - flash_left;

  /* If window is tall, flash top and bottom line.  */
  if (height > 3 * FRAME_LINE_HEIGHT (f))
    {
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			      flash_left,
			      (FRAME_INTERNAL_BORDER_WIDTH (f)
			       + FRAME_TOP_MARGIN_HEIGHT (f)),
			      width, flash_height);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			      flash_left,
			      (height - flash_height
			       - FRAME_INTERNAL_BORDER_WIDTH (f)
			       - FRAME_BOTTOM_MARGIN_HEIGHT (f)),
			      width, flash_height);

    }
  else
    /* If it is short, flash it all.  */
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			    flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
			    width, (height - 2
				    * FRAME_INTERNAL_BORDER_WIDTH (f)));

  flush_frame (f);

  struct timespec delay = make_timespec (0, 150 * 1000 * 1000);
  struct timespec wakeup = timespec_add (current_timespec (), delay);

  /* Keep waiting until past the time wakeup or any input gets
     available.  */
  while (! detect_input_pending ())
    {
      struct timespec current = current_timespec ();
      struct timespec timeout;

      /* Break if result would not be positive.  */
      if (timespec_cmp (wakeup, current) <= 0)
	break;

      /* How long `select' should wait.  */
      timeout = make_timespec (0, 10 * 1000 * 1000);

      /* Wait for some input to become available on the X
	 connection.  */
      FD_ZERO (&fds);

      /* Try to wait that long--but we might wake up sooner.  */
      rc = pselect (0, &fds, NULL, NULL, &timeout, NULL);

      /* Some input is available, exit the visible bell.  */
      if (rc >= 0)
	break;
    }

  /* If window is tall, flash top and bottom line.  */
  if (height > 3 * FRAME_LINE_HEIGHT (f))
    {
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			      flash_left,
			      (FRAME_INTERNAL_BORDER_WIDTH (f)
			       + FRAME_TOP_MARGIN_HEIGHT (f)),
			      width, flash_height);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			      flash_left,
			      (height - flash_height
			       - FRAME_INTERNAL_BORDER_WIDTH (f)
			       - FRAME_BOTTOM_MARGIN_HEIGHT (f)),
			      width, flash_height);
    }
  else
    /* If it is short, flash it all.  */
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			    flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
			    width, (height - 2
				    * FRAME_INTERNAL_BORDER_WIDTH (f)));

  android_free_gc (gc);
  flush_frame (f);

  unblock_input ();
}

static void
android_ring_bell (struct frame *f)
{
  if (visible_bell)
    android_flash (f);
  else
    {
      block_input ();
      android_bell ();
      unblock_input ();
    }
}

static android_cursor
make_invisible_cursor (struct android_display_info *dpyinfo)
{
  return android_create_font_cursor (ANDROID_XC_NULL);
}

static void
android_toggle_visible_pointer (struct frame *f, bool invisible)
{
  struct android_display_info *dpyinfo;

  dpyinfo = FRAME_DISPLAY_INFO (f);

  if (!dpyinfo->invisible_cursor)
    dpyinfo->invisible_cursor = make_invisible_cursor (dpyinfo);

  if (invisible)
    android_define_cursor (FRAME_ANDROID_WINDOW (f),
			   dpyinfo->invisible_cursor);
  else
    android_define_cursor (FRAME_ANDROID_WINDOW (f),
			   (FRAME_ANDROID_OUTPUT (f)->hourglass
			    ? f->output_data.android->hourglass_cursor
			    : f->output_data.android->current_cursor));

  f->pointer_invisible = invisible;
}

static void
android_toggle_invisible_pointer (struct frame *f, bool invisible)
{
  block_input ();
  android_toggle_visible_pointer (f, invisible);
  unblock_input ();
}

/* Start an update of frame F.  This function is installed as a hook
   for update_begin, i.e. it is called when update_begin is called.
   This function is called prior to calls to gui_update_window_begin
   for each window being updated.  Currently, there is nothing to do
   here because all interesting stuff is done on a window basis.  */

static void
android_update_begin (struct frame *f)
{
  /* The frame is no longer complete, as it is in the midst of an
     update.  */
  FRAME_ANDROID_COMPLETE_P (f) = false;
}

/* End update of frame F.  This function is installed as a hook in
   update_end.  */

static void
android_update_end (struct frame *f)
{
  /* Mouse highlight may be displayed again.  */
  MOUSE_HL_INFO (f)->mouse_face_defer = false;
}

static void
show_back_buffer (struct frame *f)
{
  struct android_swap_info swap_info;

  memset (&swap_info, 0, sizeof (swap_info));
  swap_info.swap_window = FRAME_ANDROID_WINDOW (f);
  swap_info.swap_action = ANDROID_COPIED;
  android_swap_buffers (&swap_info, 1);

  /* Now the back buffer no longer needs to be flipped.  */
  FRAME_ANDROID_NEED_BUFFER_FLIP (f) = false;
}

/* Flip back buffers on F if it has undrawn content.  */

static void
android_flush_dirty_back_buffer_on (struct frame *f)
{
  if (FRAME_GARBAGED_P (f)
      || buffer_flipping_blocked_p ()
      /* If the frame is not already up to date, do not flush buffers
	 on input, as that will result in flicker.  */
      || !FRAME_ANDROID_COMPLETE_P (f)
      || !FRAME_ANDROID_NEED_BUFFER_FLIP (f))
    return;

  show_back_buffer (f);
}

/* Convert between the modifier bits Android uses and the modifier
   bits Emacs uses.  */

static int
android_android_to_emacs_modifiers (struct android_display_info *dpyinfo,
				    int state)
{
  return (((state & ANDROID_CONTROL_MASK) ? ctrl_modifier  : 0)
	  | ((state & ANDROID_SHIFT_MASK) ? shift_modifier : 0)
	  | ((state & ANDROID_ALT_MASK)   ? meta_modifier  : 0)
	  | ((state & ANDROID_SUPER_MASK) ? super_modifier : 0)
	  | ((state & ANDROID_META_MASK)  ? alt_modifier   : 0));
}

static int
android_emacs_to_android_modifiers (struct android_display_info *dpyinfo,
				    intmax_t state)
{
  return (((state & ctrl_modifier)    ? ANDROID_CONTROL_MASK : 0)
	  | ((state & shift_modifier) ? ANDROID_SHIFT_MASK   : 0)
	  | ((state & meta_modifier)  ? ANDROID_ALT_MASK     : 0)
	  | ((state & super_modifier) ? ANDROID_SUPER_MASK   : 0)
	  | ((state & alt_modifier)   ? ANDROID_META_MASK    : 0));
}

static void android_frame_rehighlight (struct android_display_info *);

static void
android_lower_frame (struct frame *f)
{
  android_lower_window (FRAME_ANDROID_WINDOW (f));
}

static void
android_raise_frame (struct frame *f)
{
  android_raise_window (FRAME_ANDROID_WINDOW (f));
}

static void
android_new_focus_frame (struct android_display_info *dpyinfo,
			 struct frame *frame)
{
  struct frame *old_focus;

  old_focus = dpyinfo->focus_frame;

  if (frame != dpyinfo->focus_frame)
    {
      /* Set this before calling other routines, so that they see
	 the correct value of x_focus_frame.  */
      dpyinfo->focus_frame = frame;

      if (old_focus && old_focus->auto_lower)
	android_lower_frame (old_focus);

      if (dpyinfo->focus_frame && dpyinfo->focus_frame->auto_raise)
	dpyinfo->pending_autoraise_frame = dpyinfo->focus_frame;
      else
	dpyinfo->pending_autoraise_frame = NULL;
    }

  android_frame_rehighlight (dpyinfo);
}

static void
android_focus_changed (int type, int state,
		       struct android_display_info *dpyinfo,
		       struct frame *frame, struct input_event *bufp)
{
  if (type == ANDROID_FOCUS_IN)
    {
      if (dpyinfo->x_focus_event_frame != frame)
        {
          android_new_focus_frame (dpyinfo, frame);
          dpyinfo->x_focus_event_frame = frame;
          bufp->kind = FOCUS_IN_EVENT;
          XSETFRAME (bufp->frame_or_window, frame);
        }

      frame->output_data.android->focus_state |= state;
    }
  else if (type == ANDROID_FOCUS_OUT)
    {
      frame->output_data.android->focus_state &= ~state;

      if (dpyinfo->x_focus_event_frame == frame)
        {
          dpyinfo->x_focus_event_frame = 0;
          android_new_focus_frame (dpyinfo, 0);

          bufp->kind = FOCUS_OUT_EVENT;
          XSETFRAME (bufp->frame_or_window, frame);
        }

      if (frame->pointer_invisible)
        android_toggle_invisible_pointer (frame, false);
    }
}

static void
android_detect_focus_change (struct android_display_info *dpyinfo,
			     struct frame *frame,
			     union android_event *event,
			     struct input_event *bufp)
{
  if (!frame)
    return;

  switch (event->type)
    {
    case ANDROID_FOCUS_IN:
    case ANDROID_FOCUS_OUT:
      android_focus_changed (event->type, FOCUS_EXPLICIT,
			     dpyinfo, frame, bufp);
      break;

    default:
      break;
    }
}

static bool
android_note_mouse_movement (struct frame *frame,
			     struct android_motion_event *event)
{
  struct android_display_info *dpyinfo;
  Emacs_Rectangle *r;

  if (!FRAME_ANDROID_OUTPUT (frame))
    return false;

  dpyinfo = FRAME_DISPLAY_INFO (frame);
  dpyinfo->last_mouse_motion_frame = frame;
  dpyinfo->last_mouse_motion_x = event->x;
  dpyinfo->last_mouse_motion_y = event->y;
  dpyinfo->last_mouse_movement_time = event->time;

  /* Has the mouse moved off the glyph it was on at the last sighting?  */
  r = &dpyinfo->last_mouse_glyph;
  if (frame != dpyinfo->last_mouse_glyph_frame
      || event->x < r->x || event->x >= r->x + r->width
      || event->y < r->y || event->y >= r->y + r->height)
    {
      frame->mouse_moved = true;
      note_mouse_highlight (frame, event->x, event->y);
      /* Remember which glyph we're now on.  */
      remember_mouse_glyph (frame, event->x, event->y, r);
      dpyinfo->last_mouse_glyph_frame = frame;
      return true;
    }

  return false;
}

static struct frame *
mouse_or_wdesc_frame (struct android_display_info *dpyinfo, int wdesc)
{
  struct frame *lm_f = (gui_mouse_grabbed (dpyinfo)
			? dpyinfo->last_mouse_frame
			: NULL);

  if (lm_f && !EQ (track_mouse, Qdropping)
      && !EQ (track_mouse, Qdrag_source))
    return lm_f;
  else
    {
      struct frame *w_f = android_window_to_frame (dpyinfo, wdesc);

      /* Do not return a tooltip frame.  */
      if (!w_f || FRAME_TOOLTIP_P (w_f))
	return EQ (track_mouse, Qdropping) ? lm_f : NULL;
      else
	/* When dropping it would be probably nice to raise w_f
	   here.  */
	return w_f;
    }
}

static Lisp_Object
android_construct_mouse_click (struct input_event *result,
			       struct android_button_event *event,
			       struct frame *f)
{
  struct android_display_info *dpyinfo;
  int x, y;

  dpyinfo = FRAME_DISPLAY_INFO (f);
  x = event->x;
  y = event->y;

  /* Make the event type NO_EVENT; we'll change that when we decide
     otherwise.  */
  result->kind = MOUSE_CLICK_EVENT;
  result->code = event->button - 1;
  result->timestamp = event->time;
  result->modifiers = (android_android_to_emacs_modifiers (dpyinfo,
							   event->state)
		       | (event->type == ANDROID_BUTTON_RELEASE
			  ? up_modifier : down_modifier));

  XSETINT (result->x, x);
  XSETINT (result->y, y);
  XSETFRAME (result->frame_or_window, f);
  result->arg = Qnil;
  return Qnil;
}

/* Generate a TOUCHSCREEN_UPDATE_EVENT for all pressed tools in FRAME.
   Return the event in IE.  Do not set IE->timestamp, as that is left
   to the caller.  */

static void
android_update_tools (struct frame *f, struct input_event *ie)
{
  struct android_touch_point *touchpoint;

  ie->kind = TOUCHSCREEN_UPDATE_EVENT;
  XSETFRAME (ie->frame_or_window, f);
  ie->arg = Qnil;

  /* Build the list of active touches.  */
  for (touchpoint = FRAME_OUTPUT_DATA (f)->touch_points;
       touchpoint; touchpoint = touchpoint->next)
    {
      /* Skip touch points which originated on the tool bar.  */

      if (touchpoint->tool_bar_p)
	continue;

      ie->arg = Fcons (list3i (touchpoint->x,
			       touchpoint->y,
			       touchpoint->tool_id),
		       ie->arg);
    }
}

/* Find and return an existing tool pressed against FRAME, identified
   by POINTER_ID.  Return NULL if no tool by that ID was found.  */

static struct android_touch_point *
android_find_tool (struct frame *f, int pointer_id)
{
  struct android_touch_point *touchpoint;

  for (touchpoint = FRAME_OUTPUT_DATA (f)->touch_points;
       touchpoint; touchpoint = touchpoint->next)
    {
      if (touchpoint->tool_id == pointer_id)
	return touchpoint;
    }

  return NULL;
}

/* Decode STRING, an array of N little endian UTF-16 characters, into
   a Lisp string.  Return Qnil if the string is too large, and the
   encoded string otherwise.  */

static Lisp_Object
android_decode_utf16 (unsigned short *utf16, size_t n)
{
  struct coding_system coding;
  ptrdiff_t size;

  if (INT_MULTIPLY_WRAPV (n, sizeof *utf16, &size))
    return Qnil;

  /* Set up the coding system.  Decoding a UTF-16 string (with no BOM)
     should not signal.  */

  memset (&coding, 0, sizeof coding);

  setup_coding_system (Qutf_16le, &coding);
  coding.source = (const unsigned char *) utf16;
  decode_coding_object (&coding, Qnil, 0, 0, size,
			size, Qt);

  return coding.dst_object;
}

/* Handle a cursor update request for F from the input method.
   MODE specifies whether or not an update should be sent immediately,
   and whether or not they are needed in the future.

   If MODE & ANDROID_CURSOR_UPDATE_IMMEDIATE, report the position of
   F's old selected window's phys cursor now.

   If MODE & ANDROID_CURSOR_UPDATE_MONITOR, set
   `need_cursor_updates'.  */

static void
android_request_cursor_updates (struct frame *f, int mode)
{
  struct window *w;

  if (mode & ANDROID_CURSOR_UPDATE_IMMEDIATE
      && WINDOWP (WINDOW_LIVE_P (f->old_selected_window)
		  ? f->old_selected_window
		  : f->selected_window))
    {
      /* Prefer the old selected window, as its selection is what was
	 reported to the IME previously.  */

      w = XWINDOW (WINDOW_LIVE_P (f->old_selected_window)
		   ? f->old_selected_window
		   : f->selected_window);
      android_set_preeditarea (w, w->cursor.x, w->cursor.y);
    }

  /* Now say whether or not updates are needed in the future.  */
  FRAME_OUTPUT_DATA (f)->need_cursor_updates
    = (mode & ANDROID_CURSOR_UPDATE_MONITOR);
}

/* Handle a single input method event EVENT, delivered to the frame
   F.

   Perform the text conversion action specified inside.  */

static void
android_handle_ime_event (union android_event *event, struct frame *f)
{
  Lisp_Object text UNINIT;
  struct android_output *output;

  /* First, decode the text if necessary.  */

  switch (event->ime.operation)
    {
    case ANDROID_IME_COMMIT_TEXT:
    case ANDROID_IME_SET_COMPOSING_TEXT:
    case ANDROID_IME_REPLACE_TEXT:
      text = android_decode_utf16 (event->ime.text,
				   event->ime.length);
      xfree (event->ime.text);

      /* Return should text be long enough that it overflows ptrdiff_t.
	 Such circumstances are detected within android_decode_utf16.  */

      if (NILP (text))
	return;

      break;

    default:
      break;
    }

  /* Finally, perform the appropriate conversion action.  */

  switch (event->ime.operation)
    {
    case ANDROID_IME_COMMIT_TEXT:
      commit_text (f, text, event->ime.position,
		   event->ime.counter);
      break;

    case ANDROID_IME_DELETE_SURROUNDING_TEXT:
      delete_surrounding_text (f, event->ime.start,
			       event->ime.end,
			       event->ime.counter);
      break;

    case ANDROID_IME_FINISH_COMPOSING_TEXT:

      if (event->ime.length == 2)
	{
	  output = FRAME_ANDROID_OUTPUT (f);

	  /* A new input method has connected to Emacs.  Stop
	     reporting changes that the previous input method has
	     asked to monitor.  */

	  output->extracted_text_flags = 0;
	  output->extracted_text_token = 0;
	  output->extracted_text_hint = 0;
	  output->need_cursor_updates = false;
	}

      finish_composing_text (f, event->ime.counter,
			     event->ime.length == 1);

      if (event->ime.length == 2)
	{
	  /* Now cancel outstanding batch edits if a new input method
	     has connected.  */

	  f->conversion.batch_edit_flags = 0;
	  f->conversion.batch_edit_count = 0;
	}

      break;

    case ANDROID_IME_SET_COMPOSING_TEXT:
      set_composing_text (f, text, event->ime.position,
			  event->ime.counter);
      break;

    case ANDROID_IME_SET_COMPOSING_REGION:
      set_composing_region (f, event->ime.start,
			    event->ime.end,
			    event->ime.counter);
      break;

    case ANDROID_IME_SET_POINT:
      textconv_set_point_and_mark (f, event->ime.start,
				   event->ime.end,
				   event->ime.counter);
      break;

    case ANDROID_IME_START_BATCH_EDIT:
      start_batch_edit (f, event->ime.counter);
      break;

    case ANDROID_IME_END_BATCH_EDIT:
      end_batch_edit (f, event->ime.counter);
      break;

    case ANDROID_IME_REQUEST_SELECTION_UPDATE:
      request_point_update (f, event->ime.counter);
      break;

    case ANDROID_IME_REQUEST_CURSOR_UPDATES:
      android_request_cursor_updates (f, event->ime.length);
      break;

    case ANDROID_IME_REPLACE_TEXT:
      replace_text (f, event->ime.start, event->ime.end,
		    text, event->ime.position,
		    event->ime.counter);
      break;
    }
}



/* Forward declaration.  */
static void android_notify_conversion (unsigned long);

static int
handle_one_android_event (struct android_display_info *dpyinfo,
			  union android_event *event, int *finish,
			  struct input_event *hold_quit)
{
  union android_event configureEvent;
  struct frame *f, *any, *mouse_frame;
  Mouse_HLInfo *hlinfo;
  union buffered_input_event inev;
  int modifiers, count, do_help;
  struct android_touch_point *touchpoint, **last;
  Lisp_Object window;
  int scroll_height;
  double scroll_unit;
  int keysym;
  ptrdiff_t nchars, i;
  struct window *w;

  /* It is okay for this to not resemble handle_one_xevent so much.
     Differences in event handling code are much less nasty than
     stuble differences in the graphics code.  */

  do_help = count = 0;
  hlinfo = &dpyinfo->mouse_highlight;
  *finish = ANDROID_EVENT_NORMAL;
  any = android_window_to_frame (dpyinfo, event->xany.window);
  nchars = 0;

  if (any && any->wait_event_type == event->type)
    any->wait_event_type = 0; /* Indicates we got it.  */

  EVENT_INIT (inev.ie);

  switch (event->type)
    {
    case ANDROID_CONFIGURE_NOTIFY:
      configureEvent = *event;

      f = android_window_to_frame (dpyinfo,
				   configureEvent.xconfigure.window);

      if (!f)
	goto OTHER;

      if (FRAME_TOOLTIP_P (f))
	{
	  if (FRAME_PIXEL_HEIGHT (f) != configureEvent.xconfigure.height
	      || FRAME_PIXEL_WIDTH (f) != configureEvent.xconfigure.width)
	    SET_FRAME_GARBAGED (f);

	  FRAME_PIXEL_HEIGHT (f) = configureEvent.xconfigure.height;
	  FRAME_PIXEL_WIDTH (f) = configureEvent.xconfigure.width;
	}

      int width = configureEvent.xconfigure.width;
      int height = configureEvent.xconfigure.height;

      if (CONSP (frame_size_history))
	frame_size_history_extra (f, build_string ("ConfigureNotify"),
				  FRAME_PIXEL_WIDTH (f),
				  FRAME_PIXEL_HEIGHT (f),
				  width, height, f->new_width,
				  f->new_height);

      /* Even if the number of character rows and columns has
	 not changed, the font size may have changed, so we need
	 to check the pixel dimensions as well.  */

      if (width != FRAME_PIXEL_WIDTH (f)
	  || height != FRAME_PIXEL_HEIGHT (f)
	  || (f->new_size_p
	      && ((f->new_width >= 0 && width != f->new_width)
		  || (f->new_height >= 0 && height != f->new_height))))
	{
	  change_frame_size (f, width, height, false, true, false);
	  android_clear_under_internal_border (f);
	  SET_FRAME_GARBAGED (f);
	  cancel_mouse_face (f);
	}

      /* Now change the left and top position of this window.  */

      {
	int old_left = f->left_pos;
	int old_top = f->top_pos;
	Lisp_Object frame;

	XSETFRAME (frame, f);

	{
	  android_window root;
	  unsigned int dummy_uint;

	  android_get_geometry (FRAME_ANDROID_WINDOW (f),
				&root, &f->left_pos, &f->top_pos,
				&dummy_uint, &dummy_uint,
				&dummy_uint);
	}

	if (!FRAME_TOOLTIP_P (f)
	    && (old_left != f->left_pos || old_top != f->top_pos))
	  {
	    inev.ie.kind = MOVE_FRAME_EVENT;
	    XSETFRAME (inev.ie.frame_or_window, f);
	  }

      if (f && FRAME_OUTPUT_DATA (f)->need_cursor_updates)
	{
	  w = XWINDOW (f->selected_window);
	  android_set_preeditarea (w, w->cursor.x, w->cursor.y);
	}
      }

      goto OTHER;

    case ANDROID_KEY_PRESS:

      /* Set f to any.  There are no ``outer windows'' on Android.  */
      f = any;

      /* If mouse-highlight is an integer, input clears out
	 mouse highlighting.  */
      if (!hlinfo->mouse_face_hidden && FIXNUMP (Vmouse_highlight)
	  && (any == 0
	      || !EQ (any->tool_bar_window, hlinfo->mouse_face_window)
	      || !EQ (any->tab_bar_window, hlinfo->mouse_face_window)))
        {
	  mouse_frame = hlinfo->mouse_face_mouse_frame;

	  clear_mouse_face (hlinfo);
	  hlinfo->mouse_face_hidden = true;

	  if (mouse_frame)
	    android_flush_dirty_back_buffer_on (mouse_frame);
	}

      if (!f)
	goto OTHER;

      if (event->xkey.counter)
	/* This event was generated by `performEditorAction'.  Make
	   sure it is processed before any subsequent edits.  */
	textconv_barrier (f, event->xkey.counter);

      wchar_t copy_buffer[129];
      wchar_t *copy_bufptr = copy_buffer;
      int copy_bufsiz = 128 * sizeof (wchar_t);

      event->xkey.state
	|= android_emacs_to_android_modifiers (dpyinfo,
					       extra_keyboard_modifiers);
      modifiers = event->xkey.state;

      /* Common for all keysym input events.  */
      XSETFRAME (inev.ie.frame_or_window, any);
      inev.ie.modifiers
	= android_android_to_emacs_modifiers (dpyinfo, modifiers);
      inev.ie.timestamp = event->xkey.time;

      keysym = event->xkey.keycode;

      {
	enum android_lookup_status status_return;

	nchars = android_wc_lookup_string (&event->xkey, copy_bufptr,
					   copy_bufsiz, &keysym,
					   &status_return);

	/* android_lookup_string can't be called twice, so there's no
	   way to recover from buffer overflow.  */
	if (status_return == ANDROID_BUFFER_OVERFLOW)
	  goto done_keysym;
	else if (status_return == ANDROID_LOOKUP_NONE)
	  {
	    /* Don't skip preedit text events.  */
	    if (event->xkey.keycode != (uint32_t) -1)
	      goto done_keysym;
	  }
	else if (status_return == ANDROID_LOOKUP_CHARS)
	  keysym = ANDROID_NO_SYMBOL;
	else if (status_return != ANDROID_LOOKUP_KEYSYM
		 && status_return != ANDROID_LOOKUP_BOTH)
	  emacs_abort ();

	/* Deal with pre-edit text events.  On Android, these are
	   simply encoded as events with associated strings and a
	   keycode set to ``-1''.  */

	if (event->xkey.keycode == (uint32_t) -1)
	  {
	    inev.ie.kind = PREEDIT_TEXT_EVENT;
	    inev.ie.arg = Qnil;

	    /* If text was looked up, decode it and make it the
	       preedit text.  */

	    if (status_return == ANDROID_LOOKUP_CHARS && nchars)
	      {
		copy_bufptr[nchars] = 0;
		inev.ie.arg = from_unicode_buffer (copy_bufptr);
	      }

	    goto done_keysym;
	  }
      }

      if (nchars == 1 && copy_bufptr[0] >= 32)
	{
	  /* Deal with characters.  */

	  if (copy_bufptr[0] < 128)
	    inev.ie.kind = ASCII_KEYSTROKE_EVENT;
	  else
	    inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;

	  inev.ie.code = copy_bufptr[0];
	}
      else if (nchars < 2 && keysym)
	{
	  /* If the key is a modifier key, just return.  */
	  if (ANDROID_IS_MODIFIER_KEY (keysym))
	    goto done_keysym;

	  /* Next, deal with special ``characters'' by giving the
	     keycode to keyboard.c.  */
	  inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
	  inev.ie.code = keysym;
	}
      else
	{
	  /* Finally, deal with strings.  */

	  for (i = 0; i < nchars; ++i)
	    {
	      inev.ie.kind = (SINGLE_BYTE_CHAR_P (copy_bufptr[i])
			      ? ASCII_KEYSTROKE_EVENT
			      : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
	      inev.ie.code = copy_bufptr[i];

	      /* If the character is actually '\n', then change this
		 to RET.  */

	      if (copy_bufptr[i] == '\n')
		{
		  inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
		  inev.ie.code = 66;
		}

	      kbd_buffer_store_buffered_event (&inev, hold_quit);
	    }

	  count += nchars;
	  inev.ie.kind = NO_EVENT;  /* Already stored above.  */
	}

      goto done_keysym;

    done_keysym:

      /* Now proceed to tell the input method the current position of
	 the cursor, if required.  */

      if (f && FRAME_OUTPUT_DATA (f)->need_cursor_updates)
	{
	  w = XWINDOW (f->selected_window);
	  android_set_preeditarea (w, w->cursor.x, w->cursor.y);
	}

      goto OTHER;

    case ANDROID_FOCUS_IN:
    case ANDROID_FOCUS_OUT:
      android_detect_focus_change (dpyinfo, any, event, &inev.ie);
      goto OTHER;

    case ANDROID_WINDOW_ACTION:

      /* This is a special event sent by android_run_in_emacs_thread
	 used to make Android run stuff.  */

      if (!event->xaction.window && !event->xaction.action)
	/* Don't run queries here, as it may run inside editor
	   commands, which can expose an inconsistent view of buffer
	   contents to the input method during command execution.

	   Instead, wait for Emacs to return to `android_select'.  */
	goto OTHER;

      f = any;

      if (event->xaction.action == 0)
	{
	  /* Action 0 either means that a window has been destroyed
	     and its associated frame should be as well.  */

	  if (event->xaction.window)
	    {
	      if (!f)
		goto OTHER;

	      inev.ie.kind = DELETE_WINDOW_EVENT;
	      XSETFRAME (inev.ie.frame_or_window, f);
	    }
	}

    case ANDROID_ENTER_NOTIFY:
      f = any;

      if (f)
	android_note_mouse_movement (f, &event->xmotion);
      goto OTHER;

    case ANDROID_MOTION_NOTIFY:

      previous_help_echo_string = help_echo_string;
      help_echo_string = Qnil;

      if (hlinfo->mouse_face_hidden)
	{
	  hlinfo->mouse_face_hidden = false;
	  clear_mouse_face (hlinfo);
	}

      f = any;

      if (f)
	{
	  /* Maybe generate a SELECT_WINDOW_EVENT for
	     `mouse-autoselect-window' but don't let popup menus
	     interfere with this (Bug#1261).  */
	  if (!NILP (Vmouse_autoselect_window)
	      && !popup_activated ()
	      /* Don't switch if we're currently in the minibuffer.
		 This tries to work around problems where the
		 minibuffer gets unselected unexpectedly, and where
		 you then have to move your mouse all the way down to
		 the minibuffer to select it.  */
	      && !MINI_WINDOW_P (XWINDOW (selected_window))
	      /* With `focus-follows-mouse' non-nil create an event
		 also when the target window is on another frame.  */
	      && (f == XFRAME (selected_frame)
		  || !NILP (focus_follows_mouse)))
	    {
	      static Lisp_Object last_mouse_window;
	      Lisp_Object window
		= window_from_coordinates (f, event->xmotion.x,
					   event->xmotion.y, 0,
					   false, false, false);

	      /* A window will be autoselected only when it is not
		 selected now and the last mouse movement event was
		 not in it.  The remainder of the code is a bit vague
		 wrt what a "window" is.  For immediate autoselection,
		 the window is usually the entire window but for GTK
		 where the scroll bars don't count.  For delayed
		 autoselection the window is usually the window's text
		 area including the margins.  */
	      if (WINDOWP (window)
		  && !EQ (window, last_mouse_window)
		  && !EQ (window, selected_window))
		{
		  inev.ie.kind = SELECT_WINDOW_EVENT;
		  inev.ie.frame_or_window = window;
		}

	      /* Remember the last window where we saw the mouse.  */
	      last_mouse_window = window;
	    }

	  if (!android_note_mouse_movement (f, &event->xmotion))
	    help_echo_string = previous_help_echo_string;
	}

      /* If the contents of the global variable help_echo_string
	 has changed, generate a HELP_EVENT.  */
      if (!NILP (help_echo_string)
	  || !NILP (previous_help_echo_string))
	do_help = 1;

      if (f)
	android_flush_dirty_back_buffer_on (f);

      goto OTHER;

    case ANDROID_LEAVE_NOTIFY:
      f = any;

      if (f)
        {
	  /* Now clear dpyinfo->last_mouse_motion_frame, or
	     gui_redo_mouse_highlight will end up highlighting the
	     last known position of the mouse if a tooltip frame is
	     later unmapped.  */

	  if (f == dpyinfo->last_mouse_motion_frame)
	    dpyinfo->last_mouse_motion_frame = NULL;

	  /* Something similar applies to
	     dpyinfo->last_mouse_glyph_frame.  */
	  if (f == dpyinfo->last_mouse_glyph_frame)
	    dpyinfo->last_mouse_glyph_frame = NULL;

          if (f == hlinfo->mouse_face_mouse_frame)
            {
              /* If we move outside the frame, then we're
                 certainly no longer on any text in the frame.  */
              clear_mouse_face (hlinfo);
              hlinfo->mouse_face_mouse_frame = 0;
	      android_flush_dirty_back_buffer_on (f);
            }

          /* Generate a nil HELP_EVENT to cancel a help-echo.
             Do it only if there's something to cancel.
             Otherwise, the startup message is cleared when
             the mouse leaves the frame.  */
          if (any_help_event_p
	      /* But never if `mouse-drag-and-drop-region' is in
		 progress, since that results in the tooltip being
		 dismissed when the mouse moves on top.  */
	      && !((EQ (track_mouse, Qdrag_source)
		    || EQ (track_mouse, Qdropping))
		   && gui_mouse_grabbed (dpyinfo)))
	    do_help = -1;
        }

      goto OTHER;

    case ANDROID_EXPOSE:

      f = any;

      if (f)
        {
          if (!FRAME_VISIBLE_P (f))
            {
              f->output_data.android->has_been_visible = true;
              SET_FRAME_GARBAGED (f);
            }

          if (!FRAME_GARBAGED_P (f))
            {
              expose_frame (f, event->xexpose.x, event->xexpose.y,
			    event->xexpose.width, event->xexpose.height);
	      show_back_buffer (f);
	    }
        }

      goto OTHER;

    case ANDROID_BUTTON_PRESS:
    case ANDROID_BUTTON_RELEASE:
      /* If we decide we want to generate an event to be seen
	 by the rest of Emacs, we put it here.  */

      f = any;

      Lisp_Object tab_bar_arg = Qnil;
      bool tab_bar_p = false;
      bool tool_bar_p = false;

      dpyinfo->last_mouse_glyph_frame = NULL;

      f = mouse_or_wdesc_frame (dpyinfo, event->xbutton.window);

      if (f && event->xbutton.type == ANDROID_BUTTON_PRESS
	  && !popup_activated ()
	  /* && !x_window_to_scroll_bar (event->xbutton.display, */
	  /* 			      event->xbutton.window, 2) */
	  && !FRAME_NO_ACCEPT_FOCUS (f))
	{
	  /* When clicking into a child frame or when clicking
	     into a parent frame with the child frame selected and
	     `no-accept-focus' is not set, select the clicked
	     frame.  */
	  struct frame *hf = dpyinfo->highlight_frame;

	  if (FRAME_PARENT_FRAME (f) || (hf && frame_ancestor_p (f, hf)))
	    {
	      android_set_input_focus (FRAME_ANDROID_WINDOW (f),
				       event->xbutton.time);

	      if (FRAME_PARENT_FRAME (f))
		android_raise_window (FRAME_ANDROID_WINDOW (f));
	    }
	}

      if (f)
	{
	  /* Is this in the tab-bar?  */
	  if (WINDOWP (f->tab_bar_window)
	      && WINDOW_TOTAL_LINES (XWINDOW (f->tab_bar_window)))
	    {
	      Lisp_Object window;
	      int x = event->xbutton.x;
	      int y = event->xbutton.y;

	      window = window_from_coordinates (f, x, y, 0, true, true, true);
	      tab_bar_p = EQ (window, f->tab_bar_window);

	      if (tab_bar_p)
		{
		  tab_bar_arg = handle_tab_bar_click
		    (f, x, y, (event->xbutton.type
			       == ANDROID_BUTTON_PRESS),
		     android_android_to_emacs_modifiers (dpyinfo,
							 event->xbutton.state));
		  android_flush_dirty_back_buffer_on (f);
		}
	    }

	  /* Is this in the tool-bar?  */
	  if (WINDOWP (f->tool_bar_window)
	      && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
	    {
	      Lisp_Object window;
	      int x = event->xbutton.x;
	      int y = event->xbutton.y;

	      window = window_from_coordinates (f, x, y, 0, true, true, true);
	      tool_bar_p = (EQ (window, f->tool_bar_window)
			    && ((event->xbutton.type
				 != ANDROID_BUTTON_RELEASE)
				|| f->last_tool_bar_item != -1));

	      if (tool_bar_p && event->xbutton.button < 4)
		{
		  handle_tool_bar_click
		    (f, x, y, (event->xbutton.type
			       == ANDROID_BUTTON_PRESS),
		     android_android_to_emacs_modifiers (dpyinfo,
							 event->xbutton.state));
		  android_flush_dirty_back_buffer_on (f);
		}
	    }

	  if (!(tab_bar_p && NILP (tab_bar_arg)) && !tool_bar_p)
	    if (! popup_activated ())
	      {
		android_construct_mouse_click (&inev.ie, &event->xbutton, f);

		if (!NILP (tab_bar_arg))
		  inev.ie.arg = tab_bar_arg;
	      }
	}

      if (event->type == ANDROID_BUTTON_PRESS)
	{
	  dpyinfo->grabbed |= (1 << event->xbutton.button);
	  dpyinfo->last_mouse_frame = f;
	  if (f && !tab_bar_p)
	    f->last_tab_bar_item = -1;
	  if (f && !tool_bar_p)
	    f->last_tool_bar_item = -1;
	}
      else
	dpyinfo->grabbed &= ~(1 << event->xbutton.button);

      /* Ignore any mouse motion that happened before this event;
	 any subsequent mouse-movement Emacs events should reflect
	 only motion after the ButtonPress/Release.  */
      if (f != 0)
	f->mouse_moved = false;

      goto OTHER;

      /* Touch events.  The events here don't parallel X so much.  */
    case ANDROID_TOUCH_DOWN:

      if (!any)
	goto OTHER;

      /* This event is sent when a tool is put on the screen.  X and Y
	 are the location of the finger, and pointer_id identifies the
	 tool for as long as it is still held down.  First, see if the
	 touch point already exists and can be reused (this shouldn't
	 happen, but be safe.)  */

      touchpoint = android_find_tool (any, event->touch.pointer_id);

      if (touchpoint)
	{
	  /* Simply update the tool position and send an update.  */
	  touchpoint->x = event->touch.x;
	  touchpoint->y = event->touch.y;
	  android_update_tools (any, &inev.ie);
	  inev.ie.timestamp = event->touch.time;

	  goto OTHER;
	}

      /* Otherwise, link a new touchpoint onto the output's list of
	 pressed tools.  */

      touchpoint = xmalloc (sizeof *touchpoint);
      touchpoint->tool_id = event->touch.pointer_id;
      touchpoint->x = event->touch.x;
      touchpoint->y = event->touch.y;
      touchpoint->next = FRAME_OUTPUT_DATA (any)->touch_points;
      touchpoint->tool_bar_p = false;
      FRAME_OUTPUT_DATA (any)->touch_points = touchpoint;

      /* Figure out whether or not the tool was pressed on the tool
	 bar.  Note that the code which runs when it was is more or
	 less an abuse of the mouse highlight machinery, but it works
	 well enough in practice.  */

      if (WINDOWP (any->tool_bar_window)
	  && WINDOW_TOTAL_LINES (XWINDOW (any->tool_bar_window)))
	{
	  Lisp_Object window;
	  int x = event->touch.x;
	  int y = event->touch.y;

	  window = window_from_coordinates (any, x, y, 0, true,
					    true, true);

	  /* If this touch has started in the tool bar, do not
	     send it to Lisp.  Instead, simulate a tool bar
	     click, releasing it once it goes away.  */

	  if (EQ (window, any->tool_bar_window))
	    {
	      /* Call note_mouse_highlight on the tool bar
		 item.  Otherwise, get_tool_bar_item will
		 return 1.

		 This is not necessary when mouse-highlight is
		 nil.  */

	      if (!NILP (Vmouse_highlight))
		{
		  /* Clear the pointer invisible flag to always make
		     note_mouse_highlight do its thing.  */
		  any->pointer_invisible = false;
		  note_mouse_highlight (any, x, y);

		  /* Always allow future mouse motion to
		     update the mouse highlight, no matter
		     where it is.  */
		  memset (&dpyinfo->last_mouse_glyph, 0,
			  sizeof dpyinfo->last_mouse_glyph);
		  dpyinfo->last_mouse_glyph_frame = any;
		}

	      handle_tool_bar_click (any, x, y, true, 0);

	      /* Flush any changes made by that to the front
		 buffer.  */
	      android_flush_dirty_back_buffer_on (any);

	      /* Mark the touch point as being grabbed by the tool
		 bar.  */
	      touchpoint->tool_bar_p = true;
	      goto OTHER;
	    }
	}

      /* Now generate the Emacs event.  */
      inev.ie.kind = TOUCHSCREEN_BEGIN_EVENT;
      inev.ie.timestamp = event->touch.time;
      XSETFRAME (inev.ie.frame_or_window, any);
      XSETINT (inev.ie.x, event->touch.x);
      XSETINT (inev.ie.y, event->touch.y);
      XSETINT (inev.ie.arg, event->touch.pointer_id);

      goto OTHER;

    case ANDROID_TOUCH_MOVE:

      if (!any)
	goto OTHER;

      /* Look for the tool that moved.  */

      touchpoint = android_find_tool (any, event->touch.pointer_id);

      /* If it doesn't exist or has been grabbed by the tool bar, skip
	 processing this event.  */

      if (!touchpoint || touchpoint->tool_bar_p)
	goto OTHER;

      /* Otherwise, update the position and send the update event.  */

      touchpoint->x = event->touch.x;
      touchpoint->y = event->touch.y;
      android_update_tools (any, &inev.ie);
      inev.ie.timestamp = event->touch.time;

      goto OTHER;

    case ANDROID_TOUCH_UP:

      if (!any)
	goto OTHER;

      /* Now find and unlink the tool in question.  */

      last = &FRAME_OUTPUT_DATA (any)->touch_points;
      while ((touchpoint = *last))
	{
	  if (touchpoint->tool_id == event->touch.pointer_id)
	    {
	      *last = touchpoint->next;

	      if (touchpoint->tool_bar_p)
		{
		  xfree (touchpoint);

		  /* Do what is necessary to release the tool bar and
		     possibly trigger a click.  */

		  if (any->last_tool_bar_item != -1)
		    handle_tool_bar_click (any, event->touch.x,
					   event->touch.y, false,
					   0);

		  /* Cancel any outstanding mouse highlight.  */
		  note_mouse_highlight (any, -1, -1);
		  android_flush_dirty_back_buffer_on (any);

		  goto OTHER;
		}

	      /* The tool was unlinked.  Free it and generate the
		 appropriate Emacs event (assuming that it was not
		 grabbed by the tool bar).  */
	      xfree (touchpoint);

	      inev.ie.kind = TOUCHSCREEN_END_EVENT;
	      inev.ie.timestamp = event->touch.time;

	      /* Report whether the sequence has been canceled.  */

	      if (event->touch.flags & ANDROID_TOUCH_SEQUENCE_CANCELED)
		inev.ie.modifiers = 1;

	      XSETFRAME (inev.ie.frame_or_window, any);
	      XSETINT (inev.ie.x, event->touch.x);
	      XSETINT (inev.ie.y, event->touch.y);
	      XSETINT (inev.ie.arg, event->touch.pointer_id);

	      /* Break out of the loop.  */
	      goto OTHER;
	    }
	  else
	    last = &touchpoint->next;
	}

      /* No touch point was found.  This shouldn't happen.  */
      goto OTHER;

      /* Wheel motion.  The events here don't parallel X because
	 Android doesn't have scroll valuators.  */

    case ANDROID_WHEEL:

      if (!any)
	goto OTHER;

      if (fabs (event->wheel.x_delta) > 0
	  || fabs (event->wheel.y_delta) > 0)
	{
	  if (mwheel_coalesce_scroll_events)
	    {
	      if (signbit (event->wheel.x_delta)
		  != signbit (wheel_event_x))
		wheel_event_x = 0.0;

	      if (signbit (event->wheel.y_delta)
		  != signbit (wheel_event_y))
		wheel_event_y = 0.0;

	      /* Tally up deltas until one of them exceeds 1.0.  */
	      wheel_event_x += event->wheel.x_delta;
	      wheel_event_y += event->wheel.y_delta;

	      if (fabs (wheel_event_x) < 1.0
		  && fabs (wheel_event_y) < 1.0)
		goto OTHER;
	    }
	  else
	    {
	      /* Use the deltas in the event.  */
	      wheel_event_x = event->wheel.x_delta;
	      wheel_event_y = event->wheel.y_delta;
	    }

	  /* Determine what kind of event to send.  */
	  inev.ie.kind = ((fabs (wheel_event_y)
			   >= fabs (wheel_event_x))
			  ? WHEEL_EVENT : HORIZ_WHEEL_EVENT);
	  inev.ie.timestamp = event->wheel.time;

	  /* Set the event coordinates.  */
	  XSETINT (inev.ie.x, event->wheel.x);
	  XSETINT (inev.ie.y, event->wheel.y);

	  /* Set the frame.  */
	  XSETFRAME (inev.ie.frame_or_window, any);

	  /* Figure out the scroll direction.  */
	  inev.ie.modifiers = (signbit ((fabs (wheel_event_x)
					 >= fabs (wheel_event_y))
					? wheel_event_x
					: wheel_event_y)
			       ? down_modifier : up_modifier);

	  /* Figure out how much to scale the deltas by.  */
	  window = window_from_coordinates (any, event->wheel.x,
					    event->wheel.y, NULL,
					    false, false, false);

	  if (WINDOWP (window))
	    scroll_height = XWINDOW (window)->pixel_height;
	  else
	    /* EVENT_X and EVENT_Y can be outside the
	       frame if F holds the input grab, so fall
	       back to the height of the frame instead.  */
	    scroll_height = FRAME_PIXEL_HEIGHT (any);

	  scroll_unit = pow (scroll_height, 2.0 / 3.0);

	  /* Add the keyboard modifiers.  */
	  inev.ie.modifiers
	    |= android_android_to_emacs_modifiers (dpyinfo,
						   event->wheel.state);

	  /* Finally include the scroll deltas.  */
	  inev.ie.arg = list3 (Qnil,
			       make_float (wheel_event_x
					   * scroll_unit),
			       make_float (wheel_event_y
					   * scroll_unit));

	  wheel_event_x = 0.0;
	  wheel_event_y = 0.0;
	}

      goto OTHER;

      /* Iconification.  This is vastly simpler than on X.  */
    case ANDROID_ICONIFIED:

      if (!any)
	goto OTHER;

      if (FRAME_ICONIFIED_P (any))
	goto OTHER;

      SET_FRAME_VISIBLE (any, false);
      SET_FRAME_ICONIFIED (any, true);

      inev.ie.kind = ICONIFY_EVENT;
      XSETFRAME (inev.ie.frame_or_window, any);
      goto OTHER;

    case ANDROID_DEICONIFIED:

      if (!any)
	goto OTHER;

      if (!FRAME_ICONIFIED_P (any))
	goto OTHER;

      SET_FRAME_VISIBLE (any, true);
      SET_FRAME_ICONIFIED (any, false);

      inev.ie.kind = DEICONIFY_EVENT;
      XSETFRAME (inev.ie.frame_or_window, any);
      goto OTHER;

      /* Context menu handling.  */
    case ANDROID_CONTEXT_MENU:

      if (dpyinfo->menu_event_id == -1
	  /* Previously displayed popup menus might generate events
	     after dismissal, which might interfere.
	     `current_menu_serial' is always set to an identifier
	     identifying the last context menu to be displayed.  */
	  && event->menu.menu_event_serial == current_menu_serial)
	dpyinfo->menu_event_id = event->menu.menu_event_id;

      goto OTHER;

      /* Input method events.  textconv.c functions are called here to
	 queue events, which are then executed in a safe context
	 inside keyboard.c.  */
    case ANDROID_INPUT_METHOD:

      if (!any)
	{
	  /* Free any text allocated for this event.  */
	  xfree (event->ime.text);

	  /* If edits associated with this event haven't been
	     processed yet, signal their completion to avoid delays
	     the next time a call to `android_sync_edit' is made.

	     If events for a deleted frame are interleaved with events
	     for another frame, the edit counter may be prematurely
	     incremented before edits associated with the other frames
	     are processed.  This is not a problem in practice.  */

	  android_notify_conversion (event->ime.counter);
	}
      else
	android_handle_ime_event (event, any);

      goto OTHER;

    case ANDROID_DND_DRAG_EVENT:

      if (!any)
	goto OTHER;

      /* Generate a drag and drop event to convey its position.  */
      inev.ie.kind = DRAG_N_DROP_EVENT;
      XSETFRAME (inev.ie.frame_or_window, any);
      inev.ie.timestamp = ANDROID_CURRENT_TIME;
      XSETINT (inev.ie.x, event->dnd.x);
      XSETINT (inev.ie.y, event->dnd.y);
      inev.ie.arg = Fcons (inev.ie.x, inev.ie.y);
      goto OTHER;

    case ANDROID_DND_URI_EVENT:
    case ANDROID_DND_TEXT_EVENT:

      if (!any)
	{
	  free (event->dnd.uri_or_string);
	  goto OTHER;
	}

      /* An item was dropped over ANY, and is a file in the form of a
	 content or file URI or a string to be inserted.  Generate an
	 event with this information.  */

      inev.ie.kind = DRAG_N_DROP_EVENT;
      XSETFRAME (inev.ie.frame_or_window, any);
      inev.ie.timestamp = ANDROID_CURRENT_TIME;
      XSETINT (inev.ie.x, event->dnd.x);
      XSETINT (inev.ie.y, event->dnd.y);
      inev.ie.arg = Fcons ((event->type == ANDROID_DND_TEXT_EVENT
			    ? Qtext : Quri),
			   android_decode_utf16 (event->dnd.uri_or_string,
						 event->dnd.length));
      free (event->dnd.uri_or_string);
      goto OTHER;

    default:
      goto OTHER;
    }

 OTHER:
  if (inev.ie.kind != NO_EVENT)
    {
      kbd_buffer_store_buffered_event (&inev, hold_quit);
      count++;
    }

  if (do_help
      && !(hold_quit && hold_quit->kind != NO_EVENT))
    {
      Lisp_Object frame;

      if (f)
	XSETFRAME (frame, f);
      else
	frame = Qnil;

      if (do_help > 0)
	{
	  any_help_event_p = true;
	  gen_help_event (help_echo_string, frame, help_echo_window,
			  help_echo_object, help_echo_pos);
	}
      else
	{
	  help_echo_string = Qnil;
	  gen_help_event (Qnil, frame, Qnil, Qnil, 0);
	}
      count++;
    }

  return count;
}

static int
android_read_socket (struct terminal *terminal,
		     struct input_event *hold_quit)
{
  int count = 0;
  struct android_display_info *dpyinfo;

  dpyinfo = terminal->display_info.android;

  block_input ();
  while (android_pending ())
    {
      int finish;
      union android_event event;

      android_next_event (&event);
      count += handle_one_android_event (dpyinfo, &event, &finish,
					 hold_quit);

      if (finish == ANDROID_EVENT_GOTO_OUT)
	break;
    }
  unblock_input ();

  /* If the focus was just given to an auto-raising frame, raise it
     now.  */
  if (dpyinfo->pending_autoraise_frame)
    {
      android_raise_frame (dpyinfo->pending_autoraise_frame);
      dpyinfo->pending_autoraise_frame = NULL;
    }

  return count;
}

static void
android_frame_up_to_date (struct frame *f)
{
  eassert (FRAME_ANDROID_P (f));
  block_input ();
  FRAME_MOUSE_UPDATE (f);

  if (!buffer_flipping_blocked_p ()
      && FRAME_ANDROID_NEED_BUFFER_FLIP (f))
    show_back_buffer (f);

  /* The frame is now complete, as its contents have been drawn.  */
  FRAME_ANDROID_COMPLETE_P (f) = true;

  /* Shrink the scanline buffer used by the font backend.  */
  sfntfont_android_shrink_scanline_buffer ();
  unblock_input ();
}

static void
android_buffer_flipping_unblocked_hook (struct frame *f)
{
  block_input ();

  if (FRAME_ANDROID_NEED_BUFFER_FLIP (f))
    show_back_buffer (f);

  unblock_input ();
}

static void
android_query_frame_background_color (struct frame *f, Emacs_Color *bgcolor)
{
  unsigned long background;

  background = FRAME_BACKGROUND_PIXEL (f);
  bgcolor->pixel = background;

  android_query_colors (f, bgcolor, 1);
}

int
android_parse_color (struct frame *f, const char *color_name,
		     Emacs_Color *color)
{
  unsigned short r, g, b;
  Lisp_Object tem, tem1;
  unsigned long lisp_color;

  if (parse_color_spec (color_name, &r, &g, &b))
    {
      color->red = r;
      color->green = g;
      color->blue = b;

      return 1;
    }

  tem = x_display_list->color_map;
  for (; CONSP (tem); tem = XCDR (tem))
    {
      tem1 = XCAR (tem);

      if (CONSP (tem1)
	  && !xstrcasecmp (SSDATA (XCAR (tem1)), color_name))
	{
	  lisp_color = XFIXNUM (XCDR (tem1));
	  color->red = RED_FROM_ULONG (lisp_color) * 257;
	  color->green = GREEN_FROM_ULONG (lisp_color) * 257;
	  color->blue = BLUE_FROM_ULONG (lisp_color) * 257;
	  return 1;
	}
    }

  return 0;
}

bool
android_alloc_nearest_color (struct frame *f, Emacs_Color *color)
{
  gamma_correct (f, color);
  color->pixel = RGB_TO_ULONG (color->red / 256,
			       color->green / 256,
			       color->blue / 256);

  return true;
}

void
android_query_colors (struct frame *f, Emacs_Color *colors, int ncolors)
{
  int i;

  for (i = 0; i < ncolors; ++i)
    {
      colors[i].red = RED_FROM_ULONG (colors[i].pixel) * 257;
      colors[i].green = RED_FROM_ULONG (colors[i].pixel) * 257;
      colors[i].blue = RED_FROM_ULONG (colors[i].pixel) * 257;
    }
}

static void
android_mouse_position (struct frame **fp, int insist,
			Lisp_Object *bar_window,
			enum scroll_bar_part *part, Lisp_Object *x,
			Lisp_Object *y, Time *timestamp)
{
  Lisp_Object tail, frame;
  struct android_display_info *dpyinfo;

  dpyinfo = FRAME_DISPLAY_INFO (*fp);

  /* This is the best implementation possible on Android, where the
     system doesn't let Emacs obtain any information about the mouse
     pointer at all.  */

  if (dpyinfo->last_mouse_motion_frame)
    {
      *fp = dpyinfo->last_mouse_motion_frame;
      *timestamp = dpyinfo->last_mouse_movement_time;
      *x = make_fixnum (dpyinfo->last_mouse_motion_x);
      *y = make_fixnum (dpyinfo->last_mouse_motion_y);
      *bar_window = Qnil;
      *part = scroll_bar_nowhere;

      FOR_EACH_FRAME (tail, frame)
	{
	  if (FRAME_ANDROID_P (XFRAME (frame)))
	    XFRAME (frame)->mouse_moved = false;
	}

      dpyinfo->last_mouse_motion_frame->mouse_moved = false;
    }
}

static Lisp_Object
android_get_focus_frame (struct frame *f)
{
  Lisp_Object lisp_focus;
  struct frame *focus;

  focus = FRAME_DISPLAY_INFO (f)->focus_frame;

  if (!focus)
    return Qnil;

  XSETFRAME (lisp_focus, focus);
  return lisp_focus;
}

static void
android_focus_frame (struct frame *f, bool noactivate)
{
  /* Set the input focus to the frame's window.  The system only lets
     this work on child frames.  */
  android_set_input_focus (FRAME_ANDROID_WINDOW (f),
			   ANDROID_CURRENT_TIME);
}

/* The two procedures below only have to update the cursor on Android,
   as there are no window borders there.  */

static void
android_frame_highlight (struct frame *f)
{
  gui_update_cursor (f, true);
}

static void
android_frame_unhighlight (struct frame *f)
{
  gui_update_cursor (f, true);
}

static void
android_frame_rehighlight (struct android_display_info *dpyinfo)
{
  struct frame *old_highlight;

  old_highlight = dpyinfo->highlight_frame;

  if (dpyinfo->focus_frame)
    {
      dpyinfo->highlight_frame
	= ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->focus_frame)))
	   ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->focus_frame))
	   : dpyinfo->focus_frame);
      if (!FRAME_LIVE_P (dpyinfo->highlight_frame))
	{
	  fset_focus_frame (dpyinfo->focus_frame, Qnil);
	  dpyinfo->highlight_frame = dpyinfo->focus_frame;
	}
    }
  else
    dpyinfo->highlight_frame = 0;

  if (dpyinfo->highlight_frame != old_highlight)
    {
      /* This is not yet required on Android.  */
      if (old_highlight)
	android_frame_unhighlight (old_highlight);
      if (dpyinfo->highlight_frame)
	android_frame_highlight (dpyinfo->highlight_frame);
    }
}

static void
android_frame_rehighlight_hook (struct frame *f)
{
  android_frame_rehighlight (FRAME_DISPLAY_INFO (f));
}

static void
android_frame_raise_lower (struct frame *f, bool raise_flag)
{
  if (raise_flag)
    android_raise_frame (f);
  else
    android_lower_frame (f);
}

void
android_make_frame_visible (struct frame *f)
{
  android_map_window (FRAME_ANDROID_WINDOW (f));

  SET_FRAME_VISIBLE (f, true);
  SET_FRAME_ICONIFIED (f, false);
}

void
android_make_frame_invisible (struct frame *f)
{
  /* Don't keep the highlight on an invisible frame.  */
  if (FRAME_DISPLAY_INFO (f)->highlight_frame == f)
    FRAME_DISPLAY_INFO (f)->highlight_frame = 0;

  android_unmap_window (FRAME_ANDROID_WINDOW (f));

  SET_FRAME_VISIBLE (f, false);
  SET_FRAME_ICONIFIED (f, false);
}

static void
android_make_frame_visible_invisible (struct frame *f, bool visible)
{
  if (visible)
    android_make_frame_visible (f);
  else
    android_make_frame_invisible (f);
}

static void
android_fullscreen_hook (struct frame *f)
{
  Lisp_Object wanted;

  if (!FRAME_PARENT_FRAME (f))
    {
      /* Explicitly setting fullscreen is not supported on older
	 Android versions.  */

      wanted = (f->want_fullscreen == FULLSCREEN_BOTH
		? Qfullscreen : Qmaximized);

      if (android_set_fullscreen (FRAME_ANDROID_WINDOW (f),
				  EQ (wanted, Qfullscreen)))
	store_frame_param (f, Qfullscreen, Qmaximized);
      else
        store_frame_param (f, Qfullscreen, wanted);
    }
  else
    {
      store_frame_param (f, Qfullscreen, Qnil);

      /* If this is a child frame, don't keep it fullscreen
	 anymore.  */
      android_set_fullscreen (FRAME_ANDROID_WINDOW (f), false);
    }
}

void
android_iconify_frame (struct frame *f)
{
  /* This really doesn't work on Android.  */
  error ("Can't notify window manager of iconification");
}

static void
android_wait_for_event (struct frame *f, int eventtype)
{
  if (!FLOATP (Vandroid_wait_for_event_timeout))
    return;

  int level = interrupt_input_blocked;
  struct timespec tmo, tmo_at, time_now;

  f->wait_event_type = eventtype;

  /* Default timeout is 0.1 second.  Hopefully not noticeable.  */
  double timeout = XFLOAT_DATA (Vandroid_wait_for_event_timeout);
  time_t timeout_seconds = (time_t) timeout;
  tmo = make_timespec (timeout_seconds,
		       (long int) ((timeout - timeout_seconds)
				   * 1000 * 1000 * 1000));
  tmo_at = timespec_add (current_timespec (), tmo);

  while (f->wait_event_type)
    {
      pending_signals = true;
      totally_unblock_input ();
      /* XTread_socket is called after unblock.  */
      block_input ();
      interrupt_input_blocked = level;

      time_now = current_timespec ();
      if (timespec_cmp (tmo_at, time_now) < 0)
	break;

      tmo = timespec_sub (tmo_at, time_now);
      if (android_select (0, NULL, NULL, NULL, &tmo) == 0)
        break; /* Timeout */
    }

  f->wait_event_type = 0;
}

static void
android_set_window_size_1 (struct frame *f, bool change_gravity,
			   int width, int height)
{
  if (change_gravity)
    f->win_gravity = NorthWestGravity;

  android_resize_window (FRAME_ANDROID_WINDOW (f), width,
			 height);

  SET_FRAME_GARBAGED (f);

  if (FRAME_VISIBLE_P (f))
    {
      android_wait_for_event (f, ANDROID_CONFIGURE_NOTIFY);

      if (CONSP (frame_size_history))
	frame_size_history_extra (f, build_string ("set_window_size_1 visible"),
				  FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
				  width, height, f->new_width, f->new_height);
    }
  else
    {
      if (CONSP (frame_size_history))
	frame_size_history_extra (f, build_string ("set_window_size_1 "
						   "invisible"),
				  FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
				  width, height, f->new_width, f->new_height);

      adjust_frame_size (f, FRAME_PIXEL_TO_TEXT_WIDTH (f, width),
			 FRAME_PIXEL_TO_TEXT_HEIGHT (f, height),
			 5, 0, Qx_set_window_size_1);
    }
}

void
android_set_window_size (struct frame *f, bool change_gravity,
			 int width, int height)
{
  block_input ();

  android_set_window_size_1 (f, change_gravity, width, height);
  android_clear_under_internal_border (f);

  /* If cursor was outside the new size, mark it as off.  */
  mark_window_cursors_off (XWINDOW (f->root_window));

  /* Clear out any recollection of where the mouse highlighting was,
     since it might be in a place that's outside the new frame size.
     Actually checking whether it is outside is a pain in the neck,
     so don't try--just let the highlighting be done afresh with new size.  */
  cancel_mouse_face (f);

  unblock_input ();

  do_pending_window_change (false);
}

static void
android_set_offset (struct frame *f, int xoff, int yoff,
		    int change_gravity)
{
  if (change_gravity > 0)
    {
      f->top_pos = yoff;
      f->left_pos = xoff;
      f->size_hint_flags &= ~ (XNegative | YNegative);
      if (xoff < 0)
	f->size_hint_flags |= XNegative;
      if (yoff < 0)
	f->size_hint_flags |= YNegative;
      f->win_gravity = NorthWestGravity;
    }

  android_move_window (FRAME_ANDROID_WINDOW (f), xoff, yoff);
}

static void
android_set_alpha (struct frame *f)
{
  /* Not supported on Android.  */
}

static Lisp_Object
android_new_font (struct frame *f, Lisp_Object font_object, int fontset)
{
  struct font *font = XFONT_OBJECT (font_object);
  int unit, font_ascent, font_descent;

  if (fontset < 0)
    fontset = fontset_from_font (font_object);
  FRAME_FONTSET (f) = fontset;
  if (FRAME_FONT (f) == font)
    /* This font is already set in frame F.  There's nothing more to
       do.  */
    return font_object;

  FRAME_FONT (f) = font;
  FRAME_BASELINE_OFFSET (f) = font->baseline_offset;
  FRAME_COLUMN_WIDTH (f) = font->average_width;
  get_font_ascent_descent (font, &font_ascent, &font_descent);
  FRAME_LINE_HEIGHT (f) = font_ascent + font_descent;

  /* We could use a more elaborate calculation here.  */
  FRAME_TAB_BAR_HEIGHT (f) = FRAME_TAB_BAR_LINES (f) * FRAME_LINE_HEIGHT (f);

  /* Compute character columns occupied by scrollbar.

     Don't do things differently for non-toolkit scrollbars
     (Bug#17163).  */
  unit = FRAME_COLUMN_WIDTH (f);
  if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
    FRAME_CONFIG_SCROLL_BAR_COLS (f)
      = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + unit - 1) / unit;
  else
    FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;


  /* Don't change the size of a tip frame; there's no point in doing it
     because it's done in Fx_show_tip, and it leads to problems because
     the tip frame has no widget.  */
  if (FRAME_ANDROID_WINDOW (f) != 0 && !FRAME_TOOLTIP_P (f))
    adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
		       FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 3,
		       false, Qfont);

  return font_object;
}

static bool
android_bitmap_icon (struct frame *f, Lisp_Object file)
{
  return false;
}

static void
android_free_pixmap_hook (struct frame *f, Emacs_Pixmap pixmap)
{
  android_free_pixmap (pixmap);
}

void
android_free_frame_resources (struct frame *f)
{
  struct android_display_info *dpyinfo;
  Mouse_HLInfo *hlinfo;
  struct android_touch_point *last, *next;

  dpyinfo = FRAME_DISPLAY_INFO (f);
  hlinfo = &dpyinfo->mouse_highlight;

  block_input ();
  free_frame_faces (f);

  /* FRAME_ANDROID_WINDOW can be 0 if frame creation failed.  */
  if (FRAME_ANDROID_WINDOW (f))
    android_destroy_window (FRAME_ANDROID_WINDOW (f));

  android_free_gcs (f);

  /* Free cursors.  */
  if (f->output_data.android->text_cursor)
    android_free_cursor (f->output_data.android->text_cursor);
  if (f->output_data.android->nontext_cursor)
    android_free_cursor (f->output_data.android->nontext_cursor);
  if (f->output_data.android->modeline_cursor)
    android_free_cursor (f->output_data.android->modeline_cursor);
  if (f->output_data.android->hand_cursor)
    android_free_cursor (f->output_data.android->hand_cursor);
  if (f->output_data.android->hourglass_cursor)
    android_free_cursor (f->output_data.android->hourglass_cursor);
  if (f->output_data.android->horizontal_drag_cursor)
    android_free_cursor (f->output_data.android->horizontal_drag_cursor);
  if (f->output_data.android->vertical_drag_cursor)
    android_free_cursor (f->output_data.android->vertical_drag_cursor);
  if (f->output_data.android->left_edge_cursor)
    android_free_cursor (f->output_data.android->left_edge_cursor);
  if (f->output_data.android->top_left_corner_cursor)
    android_free_cursor (f->output_data.android->top_left_corner_cursor);
  if (f->output_data.android->top_edge_cursor)
    android_free_cursor (f->output_data.android->top_edge_cursor);
  if (f->output_data.android->top_right_corner_cursor)
    android_free_cursor (f->output_data.android->top_right_corner_cursor);
  if (f->output_data.android->right_edge_cursor)
    android_free_cursor (f->output_data.android->right_edge_cursor);
  if (f->output_data.android->bottom_right_corner_cursor)
    android_free_cursor (f->output_data.android->bottom_right_corner_cursor);
  if (f->output_data.android->bottom_edge_cursor)
    android_free_cursor (f->output_data.android->bottom_edge_cursor);
  if (f->output_data.android->bottom_left_corner_cursor)
    android_free_cursor (f->output_data.android->bottom_left_corner_cursor);

  /* Free extra GCs allocated by android_setup_relief_colors.  */
  if (f->output_data.android->white_relief.gc)
    {
      android_free_gc (f->output_data.android->white_relief.gc);
      f->output_data.android->white_relief.gc = 0;
    }
  if (f->output_data.android->black_relief.gc)
    {
      android_free_gc (f->output_data.android->black_relief.gc);
      f->output_data.android->black_relief.gc = 0;
    }

  if (f == dpyinfo->focus_frame)
    dpyinfo->focus_frame = 0;
  if (f == dpyinfo->x_focus_event_frame)
    dpyinfo->x_focus_event_frame = 0;
  if (f == dpyinfo->highlight_frame)
    dpyinfo->highlight_frame = 0;
  if (f == hlinfo->mouse_face_mouse_frame)
    reset_mouse_highlight (hlinfo);

  /* These two need to be freed now that they are used to compute the
     mouse position, I think.  */
  if (f == dpyinfo->last_mouse_motion_frame)
    dpyinfo->last_mouse_motion_frame = NULL;
  if (f == dpyinfo->last_mouse_frame)
    dpyinfo->last_mouse_frame = NULL;

  /* Free all tool presses currently active on this frame.  */
  next = FRAME_OUTPUT_DATA (f)->touch_points;
  while (next)
    {
      last = next;
      next = next->next;
      xfree (last);
    }

  /* Clear this in case unblock_input reads events.  */
  FRAME_OUTPUT_DATA (f)->touch_points = NULL;

  unblock_input ();
}

static void
android_delete_frame (struct frame *f)
{
  android_free_frame_resources (f);
  xfree (f->output_data.android);
  f->output_data.android = NULL;
}

static void
android_delete_terminal (struct terminal *terminal)
{
  error ("Cannot terminate connection to Android display server");
}



/* RIF functions.  */

static void
android_scroll_run (struct window *w, struct run *run)
{
  struct frame *f = XFRAME (w->frame);
  int x, y, width, height, from_y, to_y, bottom_y;

  /* Get frame-relative bounding box of the text display area of W,
     without mode lines.  Include in this box the left and right
     fringe of W.  */
  window_box (w, ANY_AREA, &x, &y, &width, &height);

  from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
  to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
  bottom_y = y + height;

  if (to_y < from_y)
    {
      /* Scrolling up.  Make sure we don't copy part of the mode
	 line at the bottom.  */
      if (from_y + run->height > bottom_y)
	height = bottom_y - from_y;
      else
	height = run->height;
    }
  else
    {
      /* Scrolling down.  Make sure we don't copy over the mode line.
	 at the bottom.  */
      if (to_y + run->height > bottom_y)
	height = bottom_y - to_y;
      else
	height = run->height;
    }

  block_input ();

  /* Cursor off.  Will be switched on again in gui_update_window_end.  */
  gui_clear_cursor (w);

  /* To avoid sequence point problems, make sure to only call
     FRAME_ANDROID_DRAWABLE once.  */
  android_copy_area (FRAME_ANDROID_DRAWABLE (f),
		     FRAME_ANDROID_WINDOW (f),
		     f->output_data.android->normal_gc,
		     x, from_y, width, height, x, to_y);

  unblock_input ();
}

static void
android_after_update_window_line (struct window *w, struct glyph_row *desired_row)
{
  eassert (w);

  if (!desired_row->mode_line_p && !w->pseudo_window_p)
    desired_row->redraw_fringe_bitmaps_p = true;
}

static void
android_flip_and_flush (struct frame *f)
{
  block_input ();

  if (FRAME_ANDROID_NEED_BUFFER_FLIP (f))
    show_back_buffer (f);

  /* The frame is complete again as its contents were just
     flushed.  */
  FRAME_ANDROID_COMPLETE_P (f) = true;
  unblock_input ();
}

static void
android_clear_rectangle (struct frame *f, struct android_gc *gc, int x,
			 int y, int width, int height)
{
  struct android_gc_values xgcv;

  android_get_gc_values (gc, (ANDROID_GC_BACKGROUND
			      | ANDROID_GC_FOREGROUND),
			 &xgcv);
  android_set_foreground (gc, xgcv.background);
  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			  x, y, width, height);
  android_set_foreground (gc, xgcv.foreground);
}

static void
android_reset_clip_rectangles (struct frame *f, struct android_gc *gc)
{
  android_set_clip_mask (gc, ANDROID_NONE);
}

static void
android_clip_to_row (struct window *w, struct glyph_row *row,
		     enum glyph_row_area area, struct android_gc *gc,
		     struct android_rectangle *rect_return)
{
  struct android_rectangle clip_rect;
  int window_x, window_y, window_width;

  window_box (w, area, &window_x, &window_y, &window_width, 0);

  clip_rect.x = window_x;
  clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
  clip_rect.y = max (clip_rect.y, window_y);
  clip_rect.width = window_width;
  clip_rect.height = row->visible_height;

  android_set_clip_rectangles (gc, 0, 0, &clip_rect, 1);

  if (rect_return)
    *rect_return = clip_rect;
}

static void
android_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
			    struct draw_fringe_bitmap_params *p)
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  struct android_gc *gc = f->output_data.android->normal_gc;
  struct face *face = p->face;
  struct android_rectangle clip_rect;

  /* Must clip because of partially visible lines.  */
  android_clip_to_row (w, row, ANY_AREA, gc, &clip_rect);

  if (p->bx >= 0 && !p->overlay_p)
    {
      /* In case the same realized face is used for fringes and for
	 something displayed in the text (e.g. face `region' on
	 mono-displays, the fill style may have been changed to
	 ANDROID_FILL_SOLID in
	 android_draw_glyph_string_background.  */
      if (face->stipple)
	{
	  android_set_fill_style (face->gc, ANDROID_FILL_OPAQUE_STIPPLED);
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), face->gc,
				  p->bx, p->by, p->nx, p->ny);
	  android_set_fill_style (face->gc, ANDROID_FILL_SOLID);

	  row->stipple_p = true;
	}
      else
	{
	  android_set_background (face->gc, face->background);
	  android_clear_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);
	  android_set_foreground (face->gc, face->foreground);
	}
    }

  if (p->which)
    {
      android_drawable drawable;
      char *bits;
      android_pixmap pixmap, clipmask;
      struct android_gc_values gcv;
      unsigned long background, cursor_pixel;
      int depth;
      struct android_rectangle image_rect, dest;
      int px, py, pwidth, pheight;

      drawable = FRAME_ANDROID_DRAWABLE (f);
      clipmask = ANDROID_NONE;
      background = face->background;
      cursor_pixel = f->output_data.android->cursor_pixel;
      depth = FRAME_DISPLAY_INFO (f)->n_planes;

      /* Intersect the destination rectangle with that of the row.
	 Setting a clip mask overrides the clip rectangles provided by
	 android_clip_to_row, so clipping must be performed by
	 hand.  */

      image_rect.x = p->x;
      image_rect.y = p->y;
      image_rect.width = p->wd;
      image_rect.height = p->h;

      if (!gui_intersect_rectangles (&clip_rect, &image_rect, &dest))
	/* The entire destination rectangle falls outside the row.  */
	goto undo_clip;

      /* Extrapolate the source rectangle from the difference between
	 the destination and image rectangles.  */

      px = dest.x - image_rect.x;
      py = dest.y - image_rect.y;
      pwidth = dest.width;
      pheight = dest.height;

      if (p->wd > 8)
	bits = (char *) (p->bits + p->dh);
      else
	bits = (char *) p->bits + p->dh;

      pixmap = android_create_pixmap_from_bitmap_data (bits, p->wd, p->h,
						       (p->cursor_p
							? (p->overlay_p
							   ? face->background
							   : cursor_pixel)
							: face->foreground),
						       background, depth);

      if (p->overlay_p)
	{
	  clipmask = android_create_pixmap_from_bitmap_data (bits, p->wd, p->h,
							     1, 0, 1);

	  gcv.clip_mask = clipmask;
	  gcv.clip_x_origin = p->x;
	  gcv.clip_y_origin = p->y;
	  android_change_gc (gc, (ANDROID_GC_CLIP_MASK
				  | ANDROID_GC_CLIP_X_ORIGIN
				  | ANDROID_GC_CLIP_Y_ORIGIN),
			     &gcv);
	}

      android_copy_area (pixmap, drawable, gc, px, py,
			 pwidth, pheight, dest.x, dest.y);
      android_free_pixmap (pixmap);

      if (p->overlay_p)
	{
	  gcv.clip_mask = ANDROID_NONE;
	  android_change_gc (gc, ANDROID_GC_CLIP_MASK, &gcv);
	  android_free_pixmap (clipmask);
	}
    }

 undo_clip:
  android_reset_clip_rectangles (f, gc);
}

/* Set S->gc to a suitable GC for drawing glyph string S in cursor
   face.  */

static void
android_set_cursor_gc (struct glyph_string *s)
{
  if (s->font == FRAME_FONT (s->f)
      && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
      && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
      && !s->cmp)
    s->gc = s->f->output_data.android->cursor_gc;
  else
    {
      /* Cursor on non-default face: must merge.  */
      struct android_gc_values xgcv;
      unsigned long mask;

      xgcv.background = s->f->output_data.android->cursor_pixel;
      xgcv.foreground = s->face->background;

      /* If the glyph would be invisible, try a different foreground.  */
      if (xgcv.foreground == xgcv.background)
	xgcv.foreground = s->face->foreground;
      if (xgcv.foreground == xgcv.background)
	xgcv.foreground = s->f->output_data.android->cursor_foreground_pixel;
      if (xgcv.foreground == xgcv.background)
	xgcv.foreground = s->face->foreground;

      /* Make sure the cursor is distinct from text in this face.  */
      if (xgcv.background == s->face->background
	  && xgcv.foreground == s->face->foreground)
	{
	  xgcv.background = s->face->foreground;
	  xgcv.foreground = s->face->background;
	}

      mask = (ANDROID_GC_FOREGROUND | ANDROID_GC_BACKGROUND);

      if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
	android_change_gc (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
			   mask, &xgcv);
      else
	FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
          = android_create_gc (mask, &xgcv);

      s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
    }
}


/* Set up S->gc of glyph string S for drawing text in mouse face.  */

static void
android_set_mouse_face_gc (struct glyph_string *s)
{
  if (s->font == s->face->font)
    s->gc = s->face->gc;
  else
    {
      /* Otherwise construct scratch_cursor_gc with values from FACE
	 except for FONT.  */
      struct android_gc_values xgcv;
      unsigned long mask;

      xgcv.background = s->face->background;
      xgcv.foreground = s->face->foreground;

      mask = (ANDROID_GC_FOREGROUND | ANDROID_GC_BACKGROUND);

      if (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc)
	android_change_gc (FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc,
			   mask, &xgcv);
      else
	FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc
          = android_create_gc (mask, &xgcv);

      s->gc = FRAME_DISPLAY_INFO (s->f)->scratch_cursor_gc;
    }

  eassert (s->gc != 0);
}


/* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
   Faces to use in the mode line have already been computed when the
   matrix was built, so there isn't much to do, here.  */

static void
android_set_mode_line_face_gc (struct glyph_string *s)
{
  s->gc = s->face->gc;
}

/* Set S->gc of glyph string S for drawing that glyph string.  Set
   S->stippled_p to a non-zero value if the face of S has a stipple
   pattern.  */

static void
android_set_glyph_string_gc (struct glyph_string *s)
{
  prepare_face_for_display (s->f, s->face);

  if (s->hl == DRAW_NORMAL_TEXT)
    {
      s->gc = s->face->gc;
      s->stippled_p = s->face->stipple != 0;
    }
  else if (s->hl == DRAW_INVERSE_VIDEO)
    {
      android_set_mode_line_face_gc (s);
      s->stippled_p = s->face->stipple != 0;
    }
  else if (s->hl == DRAW_CURSOR)
    {
      android_set_cursor_gc (s);
      s->stippled_p = false;
    }
  else if (s->hl == DRAW_MOUSE_FACE)
    {
      android_set_mouse_face_gc (s);
      s->stippled_p = s->face->stipple != 0;
    }
  else if (s->hl == DRAW_IMAGE_RAISED
	   || s->hl == DRAW_IMAGE_SUNKEN)
    {
      s->gc = s->face->gc;
      s->stippled_p = s->face->stipple != 0;
    }
  else
    emacs_abort ();

  /* GC must have been set.  */
  eassert (s->gc != 0);
}


/* Set clipping for output of glyph string S.  S may be part of a mode
   line or menu if we don't have X toolkit support.  */

static void
android_set_glyph_string_clipping (struct glyph_string *s)
{
  struct android_rectangle *r = s->clip;
  int n = get_glyph_string_clip_rects (s, r, 2);

  if (n > 0)
    android_set_clip_rectangles (s->gc, 0, 0, r, n);
  s->num_clips = n;
}


/* Set SRC's clipping for output of glyph string DST.  This is called
   when we are drawing DST's left_overhang or right_overhang only in
   the area of SRC.  */

static void
android_set_glyph_string_clipping_exactly (struct glyph_string *src,
					   struct glyph_string *dst)
{
  struct android_rectangle r;

  r.x = src->x;
  r.width = src->width;
  r.y = src->y;
  r.height = src->height;
  dst->clip[0] = r;
  dst->num_clips = 1;
  android_set_clip_rectangles (dst->gc, 0, 0, &r, 1);
}

static void
android_compute_glyph_string_overhangs (struct glyph_string *s)
{
  if (s->cmp == NULL
      && (s->first_glyph->type == CHAR_GLYPH
	  || s->first_glyph->type == COMPOSITE_GLYPH))
    {
      struct font_metrics metrics;

      if (s->first_glyph->type == CHAR_GLYPH)
	{
	  struct font *font = s->font;
	  font->driver->text_extents (font, s->char2b, s->nchars, &metrics);
	}
      else
	{
	  Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);

	  composition_gstring_width (gstring, s->cmp_from, s->cmp_to, &metrics);
	}
      s->right_overhang = (metrics.rbearing > metrics.width
			   ? metrics.rbearing - metrics.width : 0);
      s->left_overhang = metrics.lbearing < 0 ? - metrics.lbearing : 0;
    }
  else if (s->cmp)
    {
      s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
      s->left_overhang = - s->cmp->lbearing;
    }
}

static void
android_clear_glyph_string_rect (struct glyph_string *s, int x, int y,
				 int w, int h)
{
  android_clear_rectangle (s->f, s->gc, x, y, w, h);
}

static void
android_draw_glyph_string_background (struct glyph_string *s, bool force_p)
{
  /* Nothing to do if background has already been drawn or if it
     shouldn't be drawn in the first place.  */
  if (!s->background_filled_p)
    {
      int box_line_width = max (s->face->box_horizontal_line_width, 0);

      if (s->stippled_p)
	{
	  /* Fill background with a stipple pattern.  */
	  android_set_fill_style (s->gc, ANDROID_FILL_OPAQUE_STIPPLED);
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
				  s->x, s->y + box_line_width,
				  s->background_width,
				  s->height - 2 * box_line_width);
	  android_set_fill_style (s->gc, ANDROID_FILL_SOLID);
	  s->background_filled_p = true;
	}
      else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
	       /* When xdisp.c ignores FONT_HEIGHT, we cannot trust
		  font dimensions, since the actual glyphs might be
		  much smaller.  So in that case we always clear the
		  rectangle with background color.  */
	       || FONT_TOO_HIGH (s->font)
	       || s->font_not_found_p
	       || s->extends_to_end_of_line_p
	       || force_p)
	{
	  android_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
					   s->background_width,
					   s->height - 2 * box_line_width);
	  s->background_filled_p = true;
	}
    }
}

static void
android_fill_triangle (struct frame *f, struct android_gc *gc,
		       struct android_point point1,
		       struct android_point point2,
		       struct android_point point3)
{
  struct android_point abc[3];

  abc[0] = point1;
  abc[1] = point2;
  abc[2] = point3;

  android_fill_polygon (FRAME_ANDROID_DRAWABLE (f),
			gc, abc, 3, ANDROID_CONVEX,
			ANDROID_COORD_MODE_ORIGIN);
}

static struct android_point
android_make_point (int x, int y)
{
  struct android_point pt;

  pt.x = x;
  pt.y = y;

  return pt;
}

static bool
android_inside_rect_p (struct android_rectangle *rects, int nrects, int x,
		       int y)
{
  int i;

  for (i = 0; i < nrects; ++i)
    {
      if (x >= rects[i].x && y >= rects[i].y
	  && x < rects[i].x + rects[i].width
	  && y < rects[i].y + rects[i].height)
	return true;
    }

  return false;
}

static void
android_clear_point (struct frame *f, struct android_gc *gc,
		     int x, int y)
{
  struct android_gc_values xgcv;

  android_get_gc_values (gc, ANDROID_GC_BACKGROUND | ANDROID_GC_FOREGROUND,
			 &xgcv);
  android_set_foreground (gc, xgcv.background);
  android_draw_point (FRAME_ANDROID_DRAWABLE (f), gc, x, y);
  android_set_foreground (gc, xgcv.foreground);
}

static void
android_draw_relief_rect (struct frame *f, int left_x, int top_y, int right_x,
			  int bottom_y, int hwidth, int vwidth, bool raised_p,
			  bool top_p, bool bot_p, bool left_p, bool right_p,
			  struct android_rectangle *clip_rect)
{
  struct android_gc *gc, *white_gc, *black_gc, *normal_gc;
  android_drawable drawable;

  /* This code is more complicated than it has to be, because of two
     minor hacks to make the boxes look nicer: (i) if width > 1, draw
     the outermost line using the black relief.  (ii) Omit the four
     corner pixels.  */

  white_gc = f->output_data.android->white_relief.gc;
  black_gc = f->output_data.android->black_relief.gc;
  normal_gc = f->output_data.android->normal_gc;

  drawable = FRAME_ANDROID_DRAWABLE (f);

  android_set_clip_rectangles (white_gc, 0, 0, clip_rect, 1);
  android_set_clip_rectangles (black_gc, 0, 0, clip_rect, 1);

  if (raised_p)
    gc = white_gc;
  else
    gc = black_gc;

  /* Draw lines.  */

  if (top_p)
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, left_x, top_y,
			    right_x - left_x + 1, hwidth);

  if (left_p)
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, left_x, top_y,
			    vwidth, bottom_y - top_y + 1);

  if (raised_p)
    gc = black_gc;
  else
    gc = white_gc;

  if (bot_p)
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, left_x,
			    bottom_y - hwidth + 1,
			    right_x - left_x + 1, hwidth);

  if (right_p)
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc,
			    right_x - vwidth + 1,
			    top_y, vwidth, bottom_y - top_y + 1);

  /* Draw corners.  */

  if (bot_p && left_p)
    android_fill_triangle (f, raised_p ? white_gc : black_gc,
			   android_make_point (left_x, bottom_y - hwidth),
			   android_make_point (left_x + vwidth,
					       bottom_y - hwidth),
			   android_make_point (left_x, bottom_y));

  if (top_p && right_p)
    android_fill_triangle (f, raised_p ? white_gc : black_gc,
			   android_make_point (right_x - vwidth, top_y),
			   android_make_point (right_x, top_y),
			   android_make_point (right_x - vwidth,
					       top_y + hwidth));

  /* Draw outer line.  */

  if (top_p && left_p && bot_p && right_p
      && hwidth > 1 && vwidth > 1)
    android_draw_rectangle (FRAME_ANDROID_DRAWABLE (f),
			    black_gc, left_x, top_y,
			    right_x - left_x, bottom_y - top_y);
  else
    {
      if (top_p && hwidth > 1)
	android_draw_line (drawable, black_gc, left_x, top_y,
			   right_x + 1, top_y);

      if (bot_p && hwidth > 1)
	android_draw_line (drawable, black_gc, left_x, bottom_y,
			   right_x + 1, bottom_y);

      if (left_p && vwidth > 1)
	android_draw_line (drawable, black_gc, left_x, top_y,
			   left_x, bottom_y + 1);

      if (right_p && vwidth > 1)
	android_draw_line (drawable, black_gc, right_x, top_y,
			   right_x, bottom_y + 1);
    }

  /* Erase corners.  */

  if (hwidth > 1 && vwidth > 1)
    {
      if (left_p && top_p && android_inside_rect_p (clip_rect, 1,
						    left_x, top_y))
	android_clear_point (f, normal_gc, left_x, top_y);

      if (left_p && bot_p && android_inside_rect_p (clip_rect, 1,
						    left_x, bottom_y))
	android_clear_point (f, normal_gc, left_x, bottom_y);

      if (right_p && top_p && android_inside_rect_p (clip_rect, 1,
						     right_x, top_y))
	android_clear_point (f, normal_gc, right_x, top_y);

      if (right_p && bot_p && android_inside_rect_p (clip_rect, 1,
						     right_x, bottom_y))
	android_clear_point (f, normal_gc, right_x, bottom_y);
    }

  android_reset_clip_rectangles (f, white_gc);
  android_reset_clip_rectangles (f, black_gc);
}

static void
android_draw_box_rect (struct glyph_string *s,
		       int left_x, int top_y, int right_x, int bottom_y,
		       int hwidth, int vwidth, bool left_p, bool right_p,
		       struct android_rectangle *clip_rect)
{
  struct android_gc_values xgcv;

  android_get_gc_values (s->gc, ANDROID_GC_FOREGROUND, &xgcv);
  android_set_foreground (s->gc, s->face->box_color);
  android_set_clip_rectangles (s->gc, 0, 0, clip_rect, 1);

  /* Top.  */
  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc, left_x,
			  top_y, right_x - left_x + 1, hwidth);

  /* Left.  */
  if (left_p)
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc, left_x,
			    top_y, vwidth, bottom_y - top_y + 1);

  /* Bottom.  */
  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc, left_x,
			  bottom_y - hwidth + 1, right_x - left_x + 1,
			  hwidth);

  /* Right.  */
  if (right_p)
    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
			    right_x - vwidth + 1, top_y, vwidth,
			    bottom_y - top_y + 1);

  android_set_foreground (s->gc, xgcv.foreground);
  android_reset_clip_rectangles (s->f, s->gc);
}

#define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000

static bool
android_alloc_lighter_color (struct frame *f, unsigned long *pixel,
			     double factor, int delta)
{
  Emacs_Color color, new;
  long bright;
  bool success_p;

  /* Get RGB color values.  */
  color.pixel = *pixel;
  android_query_colors (f, &color, 1);

  /* Change RGB values by specified FACTOR.  Avoid overflow!  */
  eassert (factor >= 0);
  new.red = min (0xffff, factor * color.red);
  new.green = min (0xffff, factor * color.green);
  new.blue = min (0xffff, factor * color.blue);

  /* Calculate brightness of COLOR.  */
  bright = (2 * color.red + 3 * color.green + color.blue) / 6;

  /* We only boost colors that are darker than
     HIGHLIGHT_COLOR_DARK_BOOST_LIMIT.  */
  if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
    /* Make an additive adjustment to NEW, because it's dark enough so
       that scaling by FACTOR alone isn't enough.  */
    {
      /* How far below the limit this color is (0 - 1, 1 being darker).  */
      double dimness = 1 - (double) bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
      /* The additive adjustment.  */
      int min_delta = delta * dimness * factor / 2;

      if (factor < 1)
	{
	  new.red =   max (0, new.red -   min_delta);
	  new.green = max (0, new.green - min_delta);
	  new.blue =  max (0, new.blue -  min_delta);
	}
      else
	{
	  new.red =   min (0xffff, min_delta + new.red);
	  new.green = min (0xffff, min_delta + new.green);
	  new.blue =  min (0xffff, min_delta + new.blue);
	}
    }

  /* Try to allocate the color.  */
  success_p = android_alloc_nearest_color (f, &new);

  if (success_p)
    {
      if (new.pixel == *pixel)
	{
	  /* If we end up with the same color as before, try adding
	     delta to the RGB values.  */
	  new.red = min (0xffff, delta + color.red);
	  new.green = min (0xffff, delta + color.green);
	  new.blue = min (0xffff, delta + color.blue);
	  success_p = android_alloc_nearest_color (f, &new);
	}
      else
	success_p = true;

      *pixel = new.pixel;
    }

  return success_p;
}

/* Set up the foreground color for drawing relief lines of glyph
   string S.  RELIEF is a pointer to a struct relief containing the GC
   with which lines will be drawn.  Use a color that is FACTOR or
   DELTA lighter or darker than the relief's background which is found
   in S->f->output_data.android->relief_background.  If such a color
   cannot be allocated, use DEFAULT_PIXEL, instead.  */

static void
android_setup_relief_color (struct frame *f, struct relief *relief,
			    double factor, int delta,
			    unsigned long default_pixel)
{
  struct android_gc_values xgcv;
  struct android_output *di = f->output_data.android;
  unsigned long mask = ANDROID_GC_FOREGROUND;
  unsigned long pixel;
  unsigned long background = di->relief_background;
  struct android_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);

  if (relief->gc && relief->pixel != -1)
    relief->pixel = -1;

  /* Allocate new color.  */
  xgcv.foreground = default_pixel;
  pixel = background;

  if (dpyinfo->n_planes != 1
      && android_alloc_lighter_color (f, &pixel, factor, delta))
    xgcv.foreground = relief->pixel = pixel;

  if (relief->gc == 0)
    relief->gc = android_create_gc (mask, &xgcv);
  else
    android_change_gc (relief->gc, mask, &xgcv);
}

/* Set up colors for the relief lines around glyph string S.  */

static void
android_setup_relief_colors (struct glyph_string *s)
{
  struct android_output *di;
  unsigned long color;

  di = s->f->output_data.android;

  if (s->face->use_box_color_for_shadows_p)
    color = s->face->box_color;
  else if (s->first_glyph->type == IMAGE_GLYPH
	   && s->img->pixmap
	   && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
    color = IMAGE_BACKGROUND (s->img, s->f, 0);
  else
    {
      struct android_gc_values xgcv;

      /* Get the background color of the face.  */
      android_get_gc_values (s->gc, ANDROID_GC_BACKGROUND, &xgcv);
      color = xgcv.background;
    }

  if (di->white_relief.gc == 0
      || color != di->relief_background)
    {
      di->relief_background = color;
      android_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
				  WHITE_PIX_DEFAULT (s->f));
      android_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
				  BLACK_PIX_DEFAULT (s->f));
    }
}

static void
android_draw_glyph_string_box (struct glyph_string *s)
{
  int hwidth, vwidth, left_x, right_x, top_y, bottom_y, last_x;
  bool raised_p, left_p, right_p;
  struct glyph *last_glyph;
  struct android_rectangle clip_rect;

  last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
	    ? WINDOW_RIGHT_EDGE_X (s->w)
	    : window_box_right (s->w, s->area));

  /* The glyph that may have a right box line.  For static
     compositions and images, the right-box flag is on the first glyph
     of the glyph string; for other types it's on the last glyph.  */
  if (s->cmp || s->img)
    last_glyph = s->first_glyph;
  else if (s->first_glyph->type == COMPOSITE_GLYPH
	   && s->first_glyph->u.cmp.automatic)
    {
      /* For automatic compositions, we need to look up the last glyph
	 in the composition.  */
        struct glyph *end = s->row->glyphs[s->area] + s->row->used[s->area];
	struct glyph *g = s->first_glyph;
	for (last_glyph = g++;
	     g < end && g->u.cmp.automatic && g->u.cmp.id == s->cmp_id
	       && g->slice.cmp.to < s->cmp_to;
	     last_glyph = g++)
	  ;
    }
  else
    last_glyph = s->first_glyph + s->nchars - 1;

  vwidth = eabs (s->face->box_vertical_line_width);
  hwidth = eabs (s->face->box_horizontal_line_width);
  raised_p = s->face->box == FACE_RAISED_BOX;
  left_x = s->x;
  right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
	     ? last_x - 1
	     : min (last_x, s->x + s->background_width) - 1);
  top_y = s->y;
  bottom_y = top_y + s->height - 1;

  left_p = (s->first_glyph->left_box_line_p
	    || (s->hl == DRAW_MOUSE_FACE
		&& (s->prev == NULL
		    || s->prev->hl != s->hl)));
  right_p = (last_glyph->right_box_line_p
	     || (s->hl == DRAW_MOUSE_FACE
		 && (s->next == NULL
		     || s->next->hl != s->hl)));

  get_glyph_string_clip_rect (s, &clip_rect);

  if (s->face->box == FACE_SIMPLE_BOX)
    android_draw_box_rect (s, left_x, top_y, right_x, bottom_y, hwidth,
			   vwidth, left_p, right_p, &clip_rect);
  else
    {
      android_setup_relief_colors (s);
      android_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y, hwidth,
				vwidth, raised_p, true, true, left_p, right_p,
				&clip_rect);
    }
}

static void
android_draw_glyph_string_bg_rect (struct glyph_string *s, int x, int y,
				   int w, int h)
{
  if (s->stippled_p)
    {
      /* Fill background with a stipple pattern.  */
      android_set_fill_style (s->gc, ANDROID_FILL_OPAQUE_STIPPLED);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc, x,
			      y, w, h);
      android_set_fill_style (s->gc, ANDROID_FILL_SOLID);
    }
  else
    android_clear_glyph_string_rect (s, x, y, w, h);
}

static void
android_draw_image_relief (struct glyph_string *s)
{
  int x1, y1, thick;
  bool raised_p, top_p, bot_p, left_p, right_p;
  int extra_x, extra_y;
  struct android_rectangle r;
  int x = s->x;
  int y = s->ybase - image_ascent (s->img, s->face, &s->slice);

  /* If first glyph of S has a left box line, start drawing it to the
     right of that line.  */
  if (s->face->box != FACE_NO_BOX
      && s->first_glyph->left_box_line_p
      && s->slice.x == 0)
    x += max (s->face->box_vertical_line_width, 0);

  /* If there is a margin around the image, adjust x- and y-position
     by that margin.  */
  if (s->slice.x == 0)
    x += s->img->hmargin;
  if (s->slice.y == 0)
    y += s->img->vmargin;

  if (s->hl == DRAW_IMAGE_SUNKEN
      || s->hl == DRAW_IMAGE_RAISED)
    {
      if (s->face->id == TAB_BAR_FACE_ID)
	thick = (tab_bar_button_relief < 0
		 ? DEFAULT_TAB_BAR_BUTTON_RELIEF
		 : min (tab_bar_button_relief, 1000000));
      else
	thick = (tool_bar_button_relief < 0
		 ? DEFAULT_TOOL_BAR_BUTTON_RELIEF
		 : min (tool_bar_button_relief, 1000000));
      raised_p = s->hl == DRAW_IMAGE_RAISED;
    }
  else
    {
      thick = eabs (s->img->relief);
      raised_p = s->img->relief > 0;
    }

  x1 = x + s->slice.width - 1;
  y1 = y + s->slice.height - 1;

  extra_x = extra_y = 0;
  if (s->face->id == TAB_BAR_FACE_ID)
    {
      if (CONSP (Vtab_bar_button_margin)
	  && FIXNUMP (XCAR (Vtab_bar_button_margin))
	  && FIXNUMP (XCDR (Vtab_bar_button_margin)))
	{
	  extra_x = XFIXNUM (XCAR (Vtab_bar_button_margin)) - thick;
	  extra_y = XFIXNUM (XCDR (Vtab_bar_button_margin)) - thick;
	}
      else if (FIXNUMP (Vtab_bar_button_margin))
	extra_x = extra_y = XFIXNUM (Vtab_bar_button_margin) - thick;
    }

  if (s->face->id == TOOL_BAR_FACE_ID)
    {
      if (CONSP (Vtool_bar_button_margin)
	  && FIXNUMP (XCAR (Vtool_bar_button_margin))
	  && FIXNUMP (XCDR (Vtool_bar_button_margin)))
	{
	  extra_x = XFIXNUM (XCAR (Vtool_bar_button_margin));
	  extra_y = XFIXNUM (XCDR (Vtool_bar_button_margin));
	}
      else if (FIXNUMP (Vtool_bar_button_margin))
	extra_x = extra_y = XFIXNUM (Vtool_bar_button_margin);
    }

  top_p = bot_p = left_p = right_p = false;

  if (s->slice.x == 0)
    x -= thick + extra_x, left_p = true;
  if (s->slice.y == 0)
    y -= thick + extra_y, top_p = true;
  if (s->slice.x + s->slice.width == s->img->width)
    x1 += thick + extra_x, right_p = true;
  if (s->slice.y + s->slice.height == s->img->height)
    y1 += thick + extra_y, bot_p = true;

  android_setup_relief_colors (s);
  get_glyph_string_clip_rect (s, &r);
  android_draw_relief_rect (s->f, x, y, x1, y1, thick, thick, raised_p,
			    top_p, bot_p, left_p, right_p, &r);
}

static void
android_draw_image_foreground (struct glyph_string *s)
{
  int x = s->x;
  int y = s->ybase - image_ascent (s->img, s->face, &s->slice);

  /* If first glyph of S has a left box line, start drawing it to the
     right of that line.  */
  if (s->face->box != FACE_NO_BOX
      && s->first_glyph->left_box_line_p
      && s->slice.x == 0)
    x += max (s->face->box_vertical_line_width, 0);

  /* If there is a margin around the image, adjust x- and y-position
     by that margin.  */
  if (s->slice.x == 0)
    x += s->img->hmargin;
  if (s->slice.y == 0)
    y += s->img->vmargin;

  if (s->img->pixmap)
    {
      unsigned long mask = (ANDROID_GC_CLIP_MASK
			    | ANDROID_GC_CLIP_X_ORIGIN
			    | ANDROID_GC_CLIP_Y_ORIGIN
			    | ANDROID_GC_FUNCTION);
      struct android_gc_values xgcv;
      struct android_rectangle clip_rect, image_rect, r;

      xgcv.clip_mask = s->img->mask;
      xgcv.clip_x_origin = x - s->slice.x;
      xgcv.clip_y_origin = y - s->slice.y;
      xgcv.function = ANDROID_GC_COPY;
      android_change_gc (s->gc, mask, &xgcv);

      get_glyph_string_clip_rect (s, &clip_rect);
      image_rect.x = x;
      image_rect.y = y;
      image_rect.width = s->slice.width;
      image_rect.height = s->slice.height;

      if (gui_intersect_rectangles (&clip_rect, &image_rect, &r))
	android_copy_area (s->img->pixmap,
			   FRAME_ANDROID_DRAWABLE (s->f),
			   s->gc, s->slice.x + r.x - x,
			   s->slice.y + r.y - y,
			   r.width, r.height, r.x, r.y);

      /* When the image has a mask, we can expect that at least part
	 of a mouse highlight or a block cursor will be visible.  If
	 the image doesn't have a mask, make a block cursor visible by
	 drawing a rectangle around the image.  I believe it's looking
	 better if we do nothing here for mouse-face.  */
      if (s->hl == DRAW_CURSOR && !s->img->mask)
	{
	  int relief = eabs (s->img->relief);
	  android_draw_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
				  x - relief, y - relief,
				  s->slice.width + relief*2 - 1,
				  s->slice.height + relief*2 - 1);
	}

      android_set_clip_mask (s->gc, ANDROID_NONE);
    }
  else
    /* Draw a rectangle if image could not be loaded.  */
    android_draw_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc, x, y,
			    s->slice.width - 1, s->slice.height - 1);
}

static void
android_draw_image_glyph_string (struct glyph_string *s)
{
  int box_line_hwidth = max (s->face->box_vertical_line_width, 0);
  int box_line_vwidth = max (s->face->box_horizontal_line_width, 0);
  int height;

  height = s->height;
  if (s->slice.y == 0)
    height -= box_line_vwidth;
  if (s->slice.y + s->slice.height >= s->img->height)
    height -= box_line_vwidth;

  /* Fill background with face under the image.  Do it only if row is
     taller than image or if image has a clip mask to reduce
     flickering.  */
  s->stippled_p = s->face->stipple != 0;
  if (height > s->slice.height
      || s->img->hmargin
      || s->img->vmargin
      || s->img->mask
      || s->img->pixmap == 0
      || s->width != s->background_width)
    {
      if (s->stippled_p)
	s->row->stipple_p = true;

      int x = s->x;
      int y = s->y;
      int width = s->background_width;

      if (s->first_glyph->left_box_line_p
	  && s->slice.x == 0)
	{
	  x += box_line_hwidth;
	  width -= box_line_hwidth;
	}

      if (s->slice.y == 0)
	y += box_line_vwidth;

      android_draw_glyph_string_bg_rect (s, x, y, width, height);

      s->background_filled_p = true;
    }

  /* Draw the foreground.  */
  android_draw_image_foreground (s);
  android_set_glyph_string_clipping (s);

  /* If we must draw a relief around the image, do it.  */
  if (s->img->relief
      || s->hl == DRAW_IMAGE_RAISED
      || s->hl == DRAW_IMAGE_SUNKEN)
    android_draw_image_relief (s);
}

static void
android_draw_stretch_glyph_string (struct glyph_string *s)
{
  eassert (s->first_glyph->type == STRETCH_GLYPH);

  if (s->hl == DRAW_CURSOR && !x_stretch_cursor_p)
    {
      /* If `x-stretch-cursor' is nil, don't draw a block cursor as
	 wide as the stretch glyph.  */
      int width, background_width = s->background_width;
      int x = s->x;

      if (!s->row->reversed_p)
	{
	  int left_x = window_box_left_offset (s->w, TEXT_AREA);

	  if (x < left_x)
	    {
	      background_width -= left_x - x;
	      x = left_x;
	    }
	}
      else
	{
	  /* In R2L rows, draw the cursor on the right edge of the
	     stretch glyph.  */
	  int right_x = window_box_right (s->w, TEXT_AREA);

	  if (x + background_width > right_x)
	    background_width -= x - right_x;
	  x += background_width;
	}
      width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
      if (s->row->reversed_p)
	x -= width;

      /* Draw cursor.  */
      android_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);

      /* Clear rest using the GC of the original non-cursor face.  */
      if (width < background_width)
	{
	  int y = s->y;
	  int w = background_width - width, h = s->height;
	  struct android_rectangle r;
	  struct android_gc *gc;

	  if (!s->row->reversed_p)
	    x += width;
	  else
	    x = s->x;
	  if (s->row->mouse_face_p
	      && cursor_in_mouse_face_p (s->w))
	    {
	      android_set_mouse_face_gc (s);
	      gc = s->gc;
	    }
	  else
	    gc = s->face->gc;

	  get_glyph_string_clip_rect (s, &r);
	  android_set_clip_rectangles (gc, 0, 0, &r, 1);

	  if (s->face->stipple)
	    {
	      /* Fill background with a stipple pattern.  */
	      android_set_fill_style (gc, ANDROID_FILL_OPAQUE_STIPPLED);
	      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f),
				      gc, x, y, w, h);
	      android_set_fill_style (gc, ANDROID_FILL_SOLID);

	      s->row->stipple_p = true;
	    }
	  else
	    {
	      struct android_gc_values xgcv;
	      android_get_gc_values (gc, (ANDROID_GC_FOREGROUND
					  | ANDROID_GC_BACKGROUND),
				     &xgcv);
	      android_set_foreground (gc, xgcv.background);
	      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f),
				      gc, x, y, w, h);
	      android_set_foreground (gc, xgcv.foreground);
	    }

	  android_reset_clip_rectangles (s->f, gc);
	}
    }
  else if (!s->background_filled_p)
    {
      int background_width = s->background_width;
      int x = s->x, text_left_x = window_box_left (s->w, TEXT_AREA);

      /* Don't draw into left fringe or scrollbar area except for
         header line and mode line.  */
      if (s->area == TEXT_AREA
	  && x < text_left_x && !s->row->mode_line_p)
	{
	  background_width -= text_left_x - x;
	  x = text_left_x;
	}

      if (!s->row->stipple_p)
	s->row->stipple_p = s->stippled_p;

      if (background_width > 0)
	android_draw_glyph_string_bg_rect (s, x, s->y,
					   background_width,
					   s->height);
    }

  s->background_filled_p = true;
}

static void
android_get_scale_factor (int *scale_x, int *scale_y)
{
  /* This is 96 everywhere else, but 160 on Android.  */
  const int base_res = 160;
  struct android_display_info *dpyinfo;

  dpyinfo = x_display_list;
  *scale_x = *scale_y = 1;

  if (dpyinfo)
    {
      if (dpyinfo->resx > base_res)
	*scale_x = floor (dpyinfo->resx / base_res);
      if (dpyinfo->resy > base_res)
	*scale_y = floor (dpyinfo->resy / base_res);
    }
}

static void
android_draw_underwave (struct glyph_string *s, int decoration_width)
{
  int scale_x, scale_y;

  android_get_scale_factor (&scale_x, &scale_y);

  int wave_height = 3 * scale_y, wave_length = 2 * scale_x;

  int dx, dy, x0, y0, width, x1, y1, x2, y2, xmax;
  bool odd;
  struct android_rectangle wave_clip, string_clip, final_clip;

  dx = wave_length;
  dy = wave_height - 1;
  x0 = s->x;
  y0 = s->ybase + wave_height / 2;
  width = decoration_width;
  xmax = x0 + width;

  /* Find and set clipping rectangle */

  wave_clip.x = x0;
  wave_clip.y = y0;
  wave_clip.width = width;
  wave_clip.height = wave_height;
  get_glyph_string_clip_rect (s, &string_clip);

  if (!gui_intersect_rectangles (&wave_clip, &string_clip, &final_clip))
    return;

  android_set_clip_rectangles (s->gc, 0, 0, &final_clip, 1);

  /* Draw the waves */

  x1 = x0 - (x0 % dx);
  x2 = x1 + dx;
  odd = (x1 / dx) & 1;
  y1 = y2 = y0;

  if (odd)
    y1 += dy;
  else
    y2 += dy;

  if (INT_MAX - dx < xmax)
    emacs_abort ();

  while (x1 <= xmax)
    {
      android_draw_line (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
			 x1, y1, x2, y2);
      x1  = x2, y1 = y2;
      x2 += dx, y2 = y0 + odd*dy;
      odd = !odd;
    }

  /* Restore previous clipping rectangle(s) */
  android_set_clip_rectangles (s->gc, 0, 0, s->clip, s->num_clips);
}

static void
android_draw_glyph_string_foreground (struct glyph_string *s)
{
  int i, x;

  /* If first glyph of S has a left box line, start drawing the text
     of S to the right of that box line.  */
  if (s->face->box != FACE_NO_BOX
      && s->first_glyph->left_box_line_p)
    x = s->x + max (s->face->box_vertical_line_width, 0);
  else
    x = s->x;

  /* Draw characters of S as rectangles if S's font could not be
     loaded.  */
  if (s->font_not_found_p)
    {
      for (i = 0; i < s->nchars; ++i)
	{
	  struct glyph *g = s->first_glyph + i;
	  android_draw_rectangle (FRAME_ANDROID_DRAWABLE (s->f),
				  s->gc, x, s->y,
				  g->pixel_width - 1,
				  s->height - 1);
	  x += g->pixel_width;
	}
    }
  else
    {
      struct font *font = s->font;
      int boff = font->baseline_offset;
      int y;

      if (font->vertical_centering)
	boff = VCENTER_BASELINE_OFFSET (font, s->f) - boff;

      y = s->ybase - boff;
      if (s->for_overlaps
	  || (s->background_filled_p && s->hl != DRAW_CURSOR))
	font->driver->draw (s, 0, s->nchars, x, y, false);
      else
	font->driver->draw (s, 0, s->nchars, x, y, true);
      if (s->face->overstrike)
	font->driver->draw (s, 0, s->nchars, x + 1, y, false);
    }
}

static void
android_draw_composite_glyph_string_foreground (struct glyph_string *s)
{
  int i, j, x;
  struct font *font = s->font;

  /* If first glyph of S has a left box line, start drawing the text
     of S to the right of that box line.  */
  if (s->face && s->face->box != FACE_NO_BOX
      && s->first_glyph->left_box_line_p)
    x = s->x + max (s->face->box_vertical_line_width, 0);
  else
    x = s->x;

  /* S is a glyph string for a composition.  S->cmp_from is the index
     of the first character drawn for glyphs of this composition.
     S->cmp_from == 0 means we are drawing the very first character of
     this composition.  */

  /* Draw a rectangle for the composition if the font for the very
     first character of the composition could not be loaded.  */
  if (s->font_not_found_p)
    {
      if (s->cmp_from == 0)
	android_draw_rectangle (FRAME_ANDROID_DRAWABLE (s->f),
				s->gc, x, s->y,
				s->width - 1, s->height - 1);
    }
  else if (! s->first_glyph->u.cmp.automatic)
    {
      int y = s->ybase;

      for (i = 0, j = s->cmp_from; i < s->nchars; i++, j++)
	/* TAB in a composition means display glyphs with
	   padding space on the left or right.  */
	if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
	  {
	    int xx = x + s->cmp->offsets[j * 2];
	    int yy = y - s->cmp->offsets[j * 2 + 1];

	    font->driver->draw (s, j, j + 1, xx, yy, false);
	    if (s->face->overstrike)
	      font->driver->draw (s, j, j + 1, xx + 1, yy, false);
	  }
    }
  else
    {
      Lisp_Object gstring = composition_gstring_from_id (s->cmp_id);
      Lisp_Object glyph;
      int y = s->ybase;
      int width = 0;

      for (i = j = s->cmp_from; i < s->cmp_to; i++)
	{
	  glyph = LGSTRING_GLYPH (gstring, i);
	  if (NILP (LGLYPH_ADJUSTMENT (glyph)))
	    width += LGLYPH_WIDTH (glyph);
	  else
	    {
	      int xoff, yoff, wadjust;

	      if (j < i)
		{
		  font->driver->draw (s, j, i, x, y, false);
		  if (s->face->overstrike)
		    font->driver->draw (s, j, i, x + 1, y, false);
		  x += width;
		}
	      xoff = LGLYPH_XOFF (glyph);
	      yoff = LGLYPH_YOFF (glyph);
	      wadjust = LGLYPH_WADJUST (glyph);
	      font->driver->draw (s, i, i + 1, x + xoff, y + yoff, false);
	      if (s->face->overstrike)
		font->driver->draw (s, i, i + 1, x + xoff + 1, y + yoff,
				    false);
	      x += wadjust;
	      j = i + 1;
	      width = 0;
	    }
	}
      if (j < i)
	{
	  font->driver->draw (s, j, i, x, y, false);
	  if (s->face->overstrike)
	    font->driver->draw (s, j, i, x + 1, y, false);
	}
    }
}

static void
android_draw_glyphless_glyph_string_foreground (struct glyph_string *s)
{
  struct glyph *glyph = s->first_glyph;
  unsigned char2b[8];
  int x, i, j;

  /* If first glyph of S has a left box line, start drawing the text
     of S to the right of that box line.  */
  if (s->face && s->face->box != FACE_NO_BOX
      && s->first_glyph->left_box_line_p)
    x = s->x + max (s->face->box_vertical_line_width, 0);
  else
    x = s->x;

  s->char2b = char2b;

  for (i = 0; i < s->nchars; i++, glyph++)
    {
#ifdef GCC_LINT
      enum { PACIFY_GCC_BUG_81401 = 1 };
#else
      enum { PACIFY_GCC_BUG_81401 = 0 };
#endif
      char buf[7 + PACIFY_GCC_BUG_81401];
      char *str = NULL;
      int len = glyph->u.glyphless.len;

      if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_ACRONYM)
	{
	  if (len > 0
	      && CHAR_TABLE_P (Vglyphless_char_display)
	      && (CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display))
		  >= 1))
	    {
	      Lisp_Object acronym
		= (! glyph->u.glyphless.for_no_font
		   ? CHAR_TABLE_REF (Vglyphless_char_display,
				     glyph->u.glyphless.ch)
		   : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
	      if (CONSP (acronym))
		acronym = XCAR (acronym);
	      if (STRINGP (acronym))
		str = SSDATA (acronym);
	    }
	}
      else if (glyph->u.glyphless.method == GLYPHLESS_DISPLAY_HEX_CODE)
	{
	  unsigned int ch = glyph->u.glyphless.ch;
	  eassume (ch <= MAX_CHAR);
	  sprintf (buf, "%0*X", ch < 0x10000 ? 4 : 6, ch);
	  str = buf;
	}

      if (str)
	{
	  int upper_len = (len + 1) / 2;

	  /* It is assured that all LEN characters in STR is ASCII.  */
	  for (j = 0; j < len; j++)
            char2b[j] = s->font->driver->encode_char (s->font, str[j]) & 0xFFFF;
	  s->font->driver->draw (s, 0, upper_len,
				 x + glyph->slice.glyphless.upper_xoff,
				 s->ybase + glyph->slice.glyphless.upper_yoff,
				 false);
	  s->font->driver->draw (s, upper_len, len,
				 x + glyph->slice.glyphless.lower_xoff,
				 s->ybase + glyph->slice.glyphless.lower_yoff,
				 false);
	}
      if (glyph->u.glyphless.method != GLYPHLESS_DISPLAY_THIN_SPACE)
	android_draw_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
				x, s->ybase - glyph->ascent,
				glyph->pixel_width - 1,
				glyph->ascent + glyph->descent - 1);
      x += glyph->pixel_width;
   }

  /* Defend against hypothetical bad code elsewhere that uses
     s->char2b after this function returns.  */
  s->char2b = NULL;
}

static void
android_draw_glyph_string (struct glyph_string *s)
{
  bool relief_drawn_p = false;

  /* If S draws into the background of its successors, draw the
     background of the successors first so that S can draw into it.
     This makes S->next use XDrawString instead of XDrawImageString.  */
  if (s->next && s->right_overhang && !s->for_overlaps)
    {
      int width;
      struct glyph_string *next;

      for (width = 0, next = s->next;
	   next && width < s->right_overhang;
	   width += next->width, next = next->next)
	if (next->first_glyph->type != IMAGE_GLYPH)
	  {
	    android_set_glyph_string_gc (next);
	    android_set_glyph_string_clipping (next);
	    if (next->first_glyph->type == STRETCH_GLYPH)
	      android_draw_stretch_glyph_string (next);
	    else
	      android_draw_glyph_string_background (next, true);
	    next->num_clips = 0;
	  }
    }

  /* Set up S->gc, set clipping and draw S.  */
  android_set_glyph_string_gc (s);

  /* Draw relief (if any) in advance for char/composition so that the
     glyph string can be drawn over it.  */
  if (!s->for_overlaps
      && s->face->box != FACE_NO_BOX
      && (s->first_glyph->type == CHAR_GLYPH
	  || s->first_glyph->type == COMPOSITE_GLYPH))

    {
      android_set_glyph_string_clipping (s);
      android_draw_glyph_string_background (s, true);
      android_draw_glyph_string_box (s);
      android_set_glyph_string_clipping (s);
      relief_drawn_p = true;
    }
  else if (!s->clip_head /* draw_glyphs didn't specify a clip mask. */
	   && !s->clip_tail
	   && ((s->prev && s->prev->hl != s->hl && s->left_overhang)
	       || (s->next && s->next->hl != s->hl && s->right_overhang)))
    /* We must clip just this glyph.  left_overhang part has already
       drawn when s->prev was drawn, and right_overhang part will be
       drawn later when s->next is drawn. */
    android_set_glyph_string_clipping_exactly (s, s);
  else
    android_set_glyph_string_clipping (s);

  switch (s->first_glyph->type)
    {
    case IMAGE_GLYPH:
      android_draw_image_glyph_string (s);
      break;

    case XWIDGET_GLYPH:
      emacs_abort ();
      break;

    case STRETCH_GLYPH:
      android_draw_stretch_glyph_string (s);
      break;

    case CHAR_GLYPH:
      if (s->for_overlaps)
	s->background_filled_p = true;
      else
	android_draw_glyph_string_background (s, false);
      android_draw_glyph_string_foreground (s);
      break;

    case COMPOSITE_GLYPH:
      if (s->for_overlaps || (s->cmp_from > 0
			      && ! s->first_glyph->u.cmp.automatic))
	s->background_filled_p = true;
      else
	android_draw_glyph_string_background (s, true);
      android_draw_composite_glyph_string_foreground (s);
      break;

    case GLYPHLESS_GLYPH:
      if (s->for_overlaps)
	s->background_filled_p = true;
      else
	android_draw_glyph_string_background (s, true);
      android_draw_glyphless_glyph_string_foreground (s);
      break;

    default:
      emacs_abort ();
    }

  if (!s->for_overlaps)
    {
      int area_x, area_y, area_width, area_height;
      int area_max_x, decoration_width;

      /* Prevent the underline from overwriting surrounding areas
	 and the fringe.  */
      window_box (s->w, s->area, &area_x, &area_y,
		  &area_width, &area_height);
      area_max_x = area_x + area_width - 1;

      decoration_width = s->width;
      if (!s->row->mode_line_p
	  && !s->row->tab_line_p
	  && area_max_x < (s->x + decoration_width - 1))
	decoration_width -= (s->x + decoration_width - 1) - area_max_x;

      /* Draw relief if not yet drawn.  */
      if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
	android_draw_glyph_string_box (s);

      /* Draw underline.  */
      if (s->face->underline)
        {
          if (s->face->underline == FACE_UNDER_WAVE)
            {
              if (s->face->underline_defaulted_p)
                android_draw_underwave (s, decoration_width);
              else
                {
                  struct android_gc_values xgcv;
                  android_get_gc_values (s->gc, ANDROID_GC_FOREGROUND, &xgcv);
                  android_set_foreground (s->gc, s->face->underline_color);
                  android_draw_underwave (s, decoration_width);
                  android_set_foreground (s->gc, xgcv.foreground);
                }
            }
          else if (s->face->underline == FACE_UNDER_LINE)
            {
              unsigned long thickness, position;
              int y;

              if (s->prev
		  && s->prev->face->underline == FACE_UNDER_LINE
		  && (s->prev->face->underline_at_descent_line_p
		      == s->face->underline_at_descent_line_p)
		  && (s->prev->face->underline_pixels_above_descent_line
		      == s->face->underline_pixels_above_descent_line))
                {
                  /* We use the same underline style as the previous one.  */
                  thickness = s->prev->underline_thickness;
                  position = s->prev->underline_position;
                }
              else
                {
		  struct font *font = font_for_underline_metrics (s);
		  unsigned long minimum_offset;
		  bool underline_at_descent_line;
		  bool use_underline_position_properties;
		  Lisp_Object val = (WINDOW_BUFFER_LOCAL_VALUE
				     (Qunderline_minimum_offset, s->w));

		  if (FIXNUMP (val))
		    minimum_offset = max (0, XFIXNUM (val));
		  else
		    minimum_offset = 1;

		  val = (WINDOW_BUFFER_LOCAL_VALUE
			 (Qx_underline_at_descent_line, s->w));
		  underline_at_descent_line
		    = (!(NILP (val) || BASE_EQ (val, Qunbound))
		       || s->face->underline_at_descent_line_p);

		  val = (WINDOW_BUFFER_LOCAL_VALUE
			 (Qx_use_underline_position_properties, s->w));
		  use_underline_position_properties
		    = !(NILP (val) || BASE_EQ (val, Qunbound));

                  /* Get the underline thickness.  Default is 1 pixel.  */
                  if (font && font->underline_thickness > 0)
                    thickness = font->underline_thickness;
                  else
                    thickness = 1;
                  if (underline_at_descent_line)
		    position = ((s->height - thickness)
				- (s->ybase - s->y)
				- s->face->underline_pixels_above_descent_line);
                  else
                    {
                      /* Get the underline position.  This is the
                         recommended vertical offset in pixels from
                         the baseline to the top of the underline.
                         This is a signed value according to the
                         specs, and its default is

                         ROUND ((maximum descent) / 2), with
                         ROUND(x) = floor (x + 0.5)  */

                      if (use_underline_position_properties
                          && font && font->underline_position >= 0)
                        position = font->underline_position;
                      else if (font)
                        position = (font->descent + 1) / 2;
                      else
                        position = minimum_offset;
                    }

		  /* Ignore minimum_offset if the amount of pixels was
		     explicitly specified.  */
		  if (!s->face->underline_pixels_above_descent_line)
		    position = max (position, minimum_offset);
                }
              /* Check the sanity of thickness and position.  We should
                 avoid drawing underline out of the current line area.  */
	      if (s->y + s->height <= s->ybase + position)
		position = (s->height - 1) - (s->ybase - s->y);
              if (s->y + s->height < s->ybase + position + thickness)
                thickness = (s->y + s->height) - (s->ybase + position);
              s->underline_thickness = thickness;
              s->underline_position = position;
              y = s->ybase + position;
              if (s->face->underline_defaulted_p)
                android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
					s->x, y, decoration_width, thickness);
              else
                {
                  struct android_gc_values xgcv;
                  android_get_gc_values (s->gc, ANDROID_GC_FOREGROUND, &xgcv);
                  android_set_foreground (s->gc, s->face->underline_color);
                  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
					  s->x, y, decoration_width, thickness);
                  android_set_foreground (s->gc, xgcv.foreground);
                }
            }
        }
      /* Draw overline.  */
      if (s->face->overline_p)
	{
	  unsigned long dy = 0, h = 1;

	  if (s->face->overline_color_defaulted_p)
	    android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f),
				    s->gc, s->x, s->y + dy,
				    decoration_width, h);
	  else
	    {
	      struct android_gc_values xgcv;
	      android_get_gc_values (s->gc, ANDROID_GC_FOREGROUND, &xgcv);
	      android_set_foreground (s->gc, s->face->overline_color);
	      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
				      s->x, s->y + dy, decoration_width, h);
	      android_set_foreground (s->gc, xgcv.foreground);
	    }
	}

      /* Draw strike-through.  */
      if (s->face->strike_through_p)
	{
	  /* Y-coordinate and height of the glyph string's first
	     glyph.  We cannot use s->y and s->height because those
	     could be larger if there are taller display elements
	     (e.g., characters displayed with a larger font) in the
	     same glyph row.  */
	  int glyph_y = s->ybase - s->first_glyph->ascent;
	  int glyph_height = s->first_glyph->ascent + s->first_glyph->descent;
	  /* Strike-through width and offset from the glyph string's
	     top edge.  */
          unsigned long h = 1;
          unsigned long dy = (glyph_height - h) / 2;

	  if (s->face->strike_through_color_defaulted_p)
	    android_fill_rectangle (FRAME_ANDROID_WINDOW (s->f),
				    s->gc, s->x, glyph_y + dy,
				    s->width, h);
	  else
	    {
	      struct android_gc_values xgcv;
	      android_get_gc_values (s->gc, ANDROID_GC_FOREGROUND, &xgcv);
	      android_set_foreground (s->gc, s->face->strike_through_color);
	      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (s->f), s->gc,
				      s->x, glyph_y + dy, decoration_width,
				      h);
	      android_set_foreground (s->gc, xgcv.foreground);
	    }
	}

      if (s->prev)
	{
	  struct glyph_string *prev;

	  for (prev = s->prev; prev; prev = prev->prev)
	    if (prev->hl != s->hl
		&& prev->x + prev->width + prev->right_overhang > s->x)
	      {
		/* As prev was drawn while clipped to its own area, we
		   must draw the right_overhang part using s->hl now.  */
		enum draw_glyphs_face save = prev->hl;

		prev->hl = s->hl;
		android_set_glyph_string_gc (prev);
		android_set_glyph_string_clipping_exactly (s, prev);
		if (prev->first_glyph->type == CHAR_GLYPH)
		  android_draw_glyph_string_foreground (prev);
		else
		  android_draw_composite_glyph_string_foreground (prev);
		android_reset_clip_rectangles (prev->f, prev->gc);
		prev->hl = save;
		prev->num_clips = 0;
	      }
	}

      if (s->next)
	{
	  struct glyph_string *next;

	  for (next = s->next; next; next = next->next)
	    if (next->hl != s->hl
		&& next->x - next->left_overhang < s->x + s->width)
	      {
		/* As next will be drawn while clipped to its own area,
		   we must draw the left_overhang part using s->hl now.  */
		enum draw_glyphs_face save = next->hl;

		next->hl = s->hl;
		android_set_glyph_string_gc (next);
		android_set_glyph_string_clipping_exactly (s, next);
		if (next->first_glyph->type == CHAR_GLYPH)
		  android_draw_glyph_string_foreground (next);
		else
		  android_draw_composite_glyph_string_foreground (next);
		android_reset_clip_rectangles (next->f, next->gc);
		next->hl = save;
		next->num_clips = 0;
		next->clip_head = s->next;
	      }
	}
    }

  /* Reset clipping.  */
  android_reset_clip_rectangles (s->f, s->gc);
  s->num_clips = 0;

  /* Set the stippled flag that tells redisplay whether or not a
     stipple was actually draw.  */

  if (s->first_glyph->type != STRETCH_GLYPH
      && s->first_glyph->type != IMAGE_GLYPH
      && !s->row->stipple_p)
    s->row->stipple_p = s->stippled_p;
}

static void
android_define_frame_cursor (struct frame *f, Emacs_Cursor cursor)
{
  if (!f->pointer_invisible
      && !FRAME_ANDROID_OUTPUT (f)->hourglass
      && f->output_data.android->current_cursor != cursor)
    android_define_cursor (FRAME_ANDROID_WINDOW (f), cursor);

  f->output_data.android->current_cursor = cursor;
}

static void
android_clear_frame_area (struct frame *f, int x, int y,
			  int width, int height)
{
  android_clear_area (FRAME_ANDROID_DRAWABLE (f),
		      x, y, width, height);
}

void
android_clear_under_internal_border (struct frame *f)
{
  if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
    {
      int border = FRAME_INTERNAL_BORDER_WIDTH (f);
      int width = FRAME_PIXEL_WIDTH (f);
      int height = FRAME_PIXEL_HEIGHT (f);
      int margin = FRAME_TOP_MARGIN_HEIGHT (f);
      int bottom_margin = FRAME_BOTTOM_MARGIN_HEIGHT (f);
      int face_id = (FRAME_PARENT_FRAME (f)
		     ? (!NILP (Vface_remapping_alist)
			? lookup_basic_face (NULL, f,
					     CHILD_FRAME_BORDER_FACE_ID)
			: CHILD_FRAME_BORDER_FACE_ID)
		     : (!NILP (Vface_remapping_alist)
			? lookup_basic_face (NULL, f,
					     INTERNAL_BORDER_FACE_ID)
			: INTERNAL_BORDER_FACE_ID));
      struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);

      if (face)
	{
	  unsigned long color = face->background;
	  struct android_gc *gc = f->output_data.android->normal_gc;

	  android_set_foreground (gc, color);
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, 0, margin,
				  width, border);
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, 0, 0,
				  border, height);
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, width - border,
				  0, border, height);
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, 0,
				  height - bottom_margin - border,
				  width, border);
	  android_set_foreground (gc, FRAME_FOREGROUND_PIXEL (f));
	}
      else
	{
	  android_clear_area (FRAME_ANDROID_DRAWABLE (f), 0, 0,
			      border, height);
	  android_clear_area (FRAME_ANDROID_DRAWABLE (f), 0,
			      margin, width, border);
	  android_clear_area (FRAME_ANDROID_DRAWABLE (f), width - border,
			      0, border, height);
	  android_clear_area (FRAME_ANDROID_DRAWABLE (f), 0,
			      height - bottom_margin - border,
			      width, border);
	}
    }
}

static void
android_draw_hollow_cursor (struct window *w, struct glyph_row *row)
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  struct android_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
  int x, y, wd, h;
  struct android_gc_values xgcv;
  struct glyph *cursor_glyph;
  struct android_gc *gc;

  /* Get the glyph the cursor is on.  If we can't tell because
     the current matrix is invalid or such, give up.  */
  cursor_glyph = get_phys_cursor_glyph (w);
  if (cursor_glyph == NULL)
    return;

  /* Compute frame-relative coordinates for phys cursor.  */
  get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
  wd = w->phys_cursor_width - 1;

  /* The foreground of cursor_gc is typically the same as the normal
     background color, which can cause the cursor box to be invisible.  */
  xgcv.foreground = f->output_data.android->cursor_pixel;
  if (dpyinfo->scratch_cursor_gc)
    android_change_gc (dpyinfo->scratch_cursor_gc,
		       ANDROID_GC_FOREGROUND, &xgcv);
  else
    dpyinfo->scratch_cursor_gc
      =  android_create_gc (ANDROID_GC_FOREGROUND, &xgcv);
  gc = dpyinfo->scratch_cursor_gc;

  /* When on R2L character, show cursor at the right edge of the
     glyph, unless the cursor box is as wide as the glyph or wider
     (the latter happens when x-stretch-cursor is non-nil).  */
  if ((cursor_glyph->resolved_level & 1) != 0
      && cursor_glyph->pixel_width > wd)
    {
      x += cursor_glyph->pixel_width - wd;
      if (wd > 0)
	wd -= 1;
    }
  /* Set clipping, draw the rectangle, and reset clipping again.  */
  android_clip_to_row (w, row, TEXT_AREA, gc, NULL);
  android_draw_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, x, y, wd, h - 1);
  android_reset_clip_rectangles (f, gc);
}

static void
android_draw_bar_cursor (struct window *w, struct glyph_row *row, int width,
			 enum text_cursor_kinds kind)
{
  struct frame *f = XFRAME (w->frame);
  struct glyph *cursor_glyph;
  int cursor_start_y;

  /* If cursor is out of bounds, don't draw garbage.  This can happen
     in mini-buffer windows when switching between echo area glyphs
     and mini-buffer.  */
  cursor_glyph = get_phys_cursor_glyph (w);
  if (cursor_glyph == NULL)
    return;

  /* Experimental avoidance of cursor on xwidget.  */
  if (cursor_glyph->type == XWIDGET_GLYPH)
    return;

  /* If on an image, draw like a normal cursor.  That's usually better
     visible than drawing a bar, esp. if the image is large so that
     the bar might not be in the window.  */
  if (cursor_glyph->type == IMAGE_GLYPH)
    {
      struct glyph_row *r;
      r = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
      draw_phys_cursor_glyph (w, r, DRAW_CURSOR);
    }
  else
    {
      struct android_gc *gc = FRAME_DISPLAY_INFO (f)->scratch_cursor_gc;
      unsigned long mask = ANDROID_GC_FOREGROUND | ANDROID_GC_BACKGROUND;
      struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
      struct android_gc_values xgcv;

      /* If the glyph's background equals the color we normally draw
	 the bars cursor in, the bar cursor in its normal color is
	 invisible.  Use the glyph's foreground color instead in this
	 case, on the assumption that the glyph's colors are chosen so
	 that the glyph is legible.  */
      if (face->background == f->output_data.android->cursor_pixel)
	xgcv.background = xgcv.foreground = face->foreground;
      else
	xgcv.background = xgcv.foreground = f->output_data.android->cursor_pixel;

      if (gc)
	android_change_gc (gc, mask, &xgcv);
      else
	{
          gc = android_create_gc (mask, &xgcv);
	  FRAME_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
	}

      android_clip_to_row (w, row, TEXT_AREA, gc, NULL);

      if (kind == BAR_CURSOR)
	{
	  int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);

	  if (width < 0)
	    width = FRAME_CURSOR_WIDTH (f);
	  width = min (cursor_glyph->pixel_width, width);

	  w->phys_cursor_width = width;

	  /* If the character under cursor is R2L, draw the bar cursor
	     on the right of its glyph, rather than on the left.  */
	  if ((cursor_glyph->resolved_level & 1) != 0)
	    x += cursor_glyph->pixel_width - width;

	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, x,
				  WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
				  width, row->height);
	}
      else /* HBAR_CURSOR */
	{
	  int dummy_x, dummy_y, dummy_h;
	  int x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);

	  if (width < 0)
	    width = row->height;

	  width = min (row->height, width);

	  get_phys_cursor_geometry (w, row, cursor_glyph, &dummy_x,
				    &dummy_y, &dummy_h);

	  cursor_start_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y
						    + row->height - width);

	  if ((cursor_glyph->resolved_level & 1) != 0
	      && cursor_glyph->pixel_width > w->phys_cursor_width - 1)
	    x += cursor_glyph->pixel_width - w->phys_cursor_width + 1;
	  android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f), gc, x,
				  cursor_start_y,
				  w->phys_cursor_width - 1, width);
	}

      android_reset_clip_rectangles (f, gc);
    }
}

static void
android_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
			    int x, int y, enum text_cursor_kinds cursor_type,
			    int cursor_width, bool on_p, bool active_p)
{
  struct frame *f;

  f = WINDOW_XFRAME (w);

  if (on_p)
    {
      w->phys_cursor_type = cursor_type;
      w->phys_cursor_on_p = true;

      if (glyph_row->exact_window_width_line_p
	  && (glyph_row->reversed_p
	      ? (w->phys_cursor.hpos < 0)
	      : (w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])))
	{
	  glyph_row->cursor_in_fringe_p = true;
	  draw_fringe_bitmap (w, glyph_row, glyph_row->reversed_p);
	}
      else
	{
	  switch (cursor_type)
	    {
	    case HOLLOW_BOX_CURSOR:
	      android_draw_hollow_cursor (w, glyph_row);
	      break;

	    case FILLED_BOX_CURSOR:
	      draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
	      break;

	    case BAR_CURSOR:
	      android_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
	      break;

	    case HBAR_CURSOR:
	      android_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
	      break;

	    case NO_CURSOR:
	      w->phys_cursor_width = 0;
	      break;

	    default:
	      emacs_abort ();
	    }
	}

      /* Now proceed to tell the input method the current position of
	 the cursor, if required.  */

      if (FRAME_OUTPUT_DATA (f)->need_cursor_updates
	  && w == XWINDOW (f->selected_window))
	android_set_preeditarea (w, x, y);
    }
}

static void
android_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  struct face *face;

  face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
  if (face)
    android_set_foreground (f->output_data.android->normal_gc,
			    face->foreground);

  android_draw_line (FRAME_ANDROID_DRAWABLE (f),
		     f->output_data.android->normal_gc,
		     x, y0, x, y1);
}

static void
android_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
  struct face *face_first
    = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
  struct face *face_last
    = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
  unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
  unsigned long color_first = (face_first
			       ? face_first->foreground
			       : FRAME_FOREGROUND_PIXEL (f));
  unsigned long color_last = (face_last
			      ? face_last->foreground
			      : FRAME_FOREGROUND_PIXEL (f));

  if ((y1 - y0 > x1 - x0) && (x1 - x0 >= 3))
    /* A vertical divider, at least three pixels wide: Draw first and
       last pixels differently.  */
    {
      android_set_foreground (f->output_data.android->normal_gc,
			      color_first);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x0, y0, 1, y1 - y0);
      android_set_foreground (f->output_data.android->normal_gc,
			      color);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x0 + 1, y0, x1 - x0 - 2, y1 - y0);
      android_set_foreground (f->output_data.android->normal_gc,
			      color_last);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x1 - 1, y0, 1, y1 - y0);
    }
  else if ((x1 - x0 > y1 - y0) && (y1 - y0 >= 3))
    /* A horizontal divider, at least three pixels high: Draw first
       and last pixels differently.  */
    {
      android_set_foreground (f->output_data.android->normal_gc,
			      color_first);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x0, y0, x1 - x0, 1);
      android_set_foreground (f->output_data.android->normal_gc, color);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x0, y0 + 1, x1 - x0, y1 - y0 - 2);
      android_set_foreground (f->output_data.android->normal_gc,
			      color_last);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x0, y1 - 1, x1 - x0, 1);
    }
  else
    {
      /* In any other case do not draw the first and last pixels
	 differently.  */
      android_set_foreground (f->output_data.android->normal_gc, color);
      android_fill_rectangle (FRAME_ANDROID_DRAWABLE (f),
			      f->output_data.android->normal_gc,
			      x0, y0, x1 - x0, y1 - y0);
    }
}



#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#endif

/* Input method related functions.  Some of these are called from Java
   within the UI thread.  */

/* A counter used to decide when an editing request completes.  */
static unsigned long edit_counter;

/* The last counter known to have completed.  */
static unsigned long last_edit_counter;

/* Semaphore posted every time the counter increases.  */
static sem_t edit_sem;

/* Try to synchronize with the UI thread, waiting a certain amount of
   time for outstanding editing requests to complete.

   Every time one of the text retrieval functions is called and an
   editing request is made, Emacs gives the main thread approximately
   100 ms to process it, in order to mostly keep the input method in
   sync with the buffer contents.  */

static void
android_sync_edit (void)
{
  struct timespec start, end, rem;
  unsigned long counter;

  counter = __atomic_load_n (&last_edit_counter,
			     __ATOMIC_SEQ_CST);

  if (counter == edit_counter)
    return;

  start = current_timespec ();
  end = timespec_add (start, make_timespec (0, 100000000));

  while (true)
    {
      rem = timespec_sub (end, current_timespec ());

      /* Timeout.  */
      if (timespec_sign (rem) < 0)
	break;

      if (__atomic_load_n (&last_edit_counter,
			   __ATOMIC_SEQ_CST)
	  == edit_counter)
	break;

      sem_timedwait (&edit_sem, &end);
    }
}

/* Return a copy of the specified Java string and its length in
   *LENGTH.  Use the JNI environment ENV.  Value is NULL if copying
   *the string fails.  */

static unsigned short *
android_copy_java_string (JNIEnv *env, jstring string, size_t *length)
{
  jsize size, i;
  const jchar *java;
  unsigned short *buffer;

  size = (*env)->GetStringLength (env, string);
  buffer = malloc (size * sizeof *buffer);

  if (!buffer)
    return NULL;

  java = (*env)->GetStringChars (env, string, NULL);

  if (!java)
    {
      free (buffer);
      return NULL;
    }

  for (i = 0; i < size; ++i)
    buffer[i] = java[i];

  *length = size;
  (*env)->ReleaseStringChars (env, string, java);
  return buffer;
}

JNIEXPORT void JNICALL
NATIVE_NAME (beginBatchEdit) (JNIEnv *env, jobject object, jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_START_BATCH_EDIT;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = 0;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (endBatchEdit) (JNIEnv *env, jobject object, jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_END_BATCH_EDIT;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = 0;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (commitCompletion) (JNIEnv *env, jobject object, jshort window,
				jstring completion_text, jint position)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;
  unsigned short *text;
  size_t length;

  /* First, obtain a copy of the Java string.  */
  text = android_copy_java_string (env, completion_text, &length);

  if (!text)
    return;

  /* Next, populate the event.  Events will always eventually be
     delivered on Android, so handle_one_android_event can be relied
     on to free text.  */

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_COMMIT_TEXT;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = min (length, PTRDIFF_MAX);
  event.ime.position = position;
  event.ime.text = text;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (commitText) (JNIEnv *env, jobject object, jshort window,
			  jstring commit_text, jint position)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;
  unsigned short *text;
  size_t length;

  /* First, obtain a copy of the Java string.  */
  text = android_copy_java_string (env, commit_text, &length);

  if (!text)
    return;

  /* Next, populate the event.  Events will always eventually be
     delivered on Android, so handle_one_android_event can be relied
     on to free text.  */

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_COMMIT_TEXT;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = min (length, PTRDIFF_MAX);
  event.ime.position = position;
  event.ime.text = text;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (deleteSurroundingText) (JNIEnv *env, jobject object,
				     jshort window, jint left_length,
				     jint right_length)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_DELETE_SURROUNDING_TEXT;
  event.ime.start = left_length;
  event.ime.end = right_length;
  event.ime.length = 0;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (finishComposingText) (JNIEnv *env, jobject object,
				   jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_FINISH_COMPOSING_TEXT;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = 0;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (replaceText) (JNIEnv *env, jobject object, jshort window,
			   jint start, jint end, jobject text,
			   int new_cursor_position, jobject attribute)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;
  size_t length;

  /* First, obtain a copy of the Java string.  */
  text = android_copy_java_string (env, text, &length);

  if (!text)
    return;

  /* Next, populate the event with the information in this function's
     arguments.  */

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_REPLACE_TEXT;
  event.ime.start = start + 1;
  event.ime.end = end + 1;
  event.ime.length = length;
  event.ime.position = new_cursor_position;
  event.ime.text = text;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

/* Structure describing the context used for a text query.  */

struct android_conversion_query_context
{
  /* The conversion request.  */
  struct textconv_callback_struct query;

  /* The window the request is being made on.  */
  android_window window;

  /* Whether or not the request was successful.  */
  bool success;
};

/* Obtain the text from the frame whose window is that specified in
   DATA using the text conversion query specified there.

   Set ((struct android_conversion_query_context *) DATA)->success on
   success.  */

static void
android_perform_conversion_query (void *data)
{
  struct android_conversion_query_context *context;
  struct frame *f;

  context = data;

  /* Find the frame associated with the window.  */
  f = android_window_to_frame (NULL, context->window);

  if (!f)
    return;

  textconv_query (f, &context->query, 0);

  /* context->query.text will have been set even if textconv_query
     returns 1.  */

  context->success = true;
}

/* Convert a string in BUFFER, containing N characters in Emacs's
   internal multibyte encoding, to a Java string utilizing the
   specified JNI environment ENV.

   If N is equal to BYTES, then BUFFER holds unibyte or plain-ASCII
   characters.  Otherwise, BUFFER holds multibyte characters.

   Make sure N and BYTES are absolutely correct, or you are asking for
   trouble.

   Value is a jstring upon success, NULL otherwise.  Any exceptions
   generated are not cleared.  */

static jstring
android_text_to_string (JNIEnv *env, char *buffer, ptrdiff_t n,
			ptrdiff_t bytes)
{
  jchar *utf16;
  size_t size, index;
  jstring string;
  int encoded;

  if (n == bytes)
    {
      /* This buffer holds no multibyte characters.  */

      if (INT_MULTIPLY_WRAPV (n, sizeof *utf16, &size))
	return NULL;

      utf16 = malloc (size);
      index = 0;

      if (!utf16)
	return NULL;

      while (n--)
	{
	  utf16[index] = buffer[index];
	  index++;
	}

      string = (*env)->NewString (env, utf16, bytes);
      free (utf16);

      return string;
    }

  /* Allocate enough to hold N characters.  */

  if (INT_MULTIPLY_WRAPV (n, sizeof *utf16, &size))
    return NULL;

  utf16 = malloc (size);
  index = 0;

  if (!utf16)
    return NULL;

  while (n--)
    {
      eassert (CHAR_HEAD_P (*buffer));
      encoded = STRING_CHAR ((unsigned char *) buffer);

      /* Now establish how to save ENCODED into the string.
         Emacs operates on multibyte characters, not UTF-16 characters
         with surrogate pairs as Android does.

         However, character positions in Java are represented as
         character (rather than codepoint) indices into UTF-16
         strings, meaning that text positions reported to Android can
         become decoupled from their actual values if the text
         returned incorporates characters that must be encoded as
         surrogate pairs.

         The hack used by Emacs is to simply replace each multibyte
         character that doesn't fit in a jchar with the NULL
         character.  */

      if (encoded >= 65536)
	encoded = 0;

      utf16[index++] = encoded;
      buffer += BYTES_BY_CHAR_HEAD (*buffer);
    }

  /* Create the string.  */
  string = (*env)->NewString (env, utf16, index);
  free (utf16);
  return string;
}

JNIEXPORT jstring JNICALL
NATIVE_NAME (getTextAfterCursor) (JNIEnv *env, jobject object, jshort window,
				  jint length, jint flags)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  struct android_conversion_query_context context;
  jstring string;

  /* First, set up the conversion query.  */
  context.query.position = EMACS_INT_MAX;
  context.query.direction = TEXTCONV_FORWARD_CHAR;
  context.query.factor = min (length, 65535);
  context.query.operation = TEXTCONV_RETRIEVAL;

  /* Next, set the rest of the context.  */
  context.window = window;
  context.success = false;

  /* Now try to perform the query.  */
  android_sync_edit ();
  if (android_run_in_emacs_thread (android_perform_conversion_query,
				   &context))
    return NULL;

  if (!context.success)
    return NULL;

  /* context->query.text now contains the text in Emacs's internal
     UTF-8 based encoding.

     Convert it to Java's UTF-16 encoding, which is the same as
     UTF-16, except that NULL bytes are encoded as surrogate pairs.

     This assumes that `free' can free data allocated with xmalloc.  */

  string = android_text_to_string (env, context.query.text.text,
				   context.query.text.length,
				   context.query.text.bytes);
  free (context.query.text.text);

  return string;
}

JNIEXPORT jstring JNICALL
NATIVE_NAME (getTextBeforeCursor) (JNIEnv *env, jobject object, jshort window,
				   jint length, jint flags)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  struct android_conversion_query_context context;
  jstring string;

  /* First, set up the conversion query.  */
  context.query.position = TYPE_MINIMUM (EMACS_INT);
  context.query.direction = TEXTCONV_BACKWARD_CHAR;
  context.query.factor = min (length, 65535);
  context.query.operation = TEXTCONV_RETRIEVAL;

  /* Next, set the rest of the context.  */
  context.window = window;
  context.success = false;

  /* Now try to perform the query.  */
  android_sync_edit ();
  if (android_run_in_emacs_thread (android_perform_conversion_query,
				   &context))
    return NULL;

  if (!context.success)
    return NULL;

  /* context->query.text now contains the text in Emacs's internal
     UTF-8 based encoding.

     Convert it to Java's UTF-16 encoding, which is the same as
     UTF-16, except that NULL bytes are encoded as surrogate pairs.

     This assumes that `free' can free data allocated with xmalloc.  */

  string = android_text_to_string (env, context.query.text.text,
				   context.query.text.length,
				   context.query.text.bytes);
  free (context.query.text.text);

  return string;
}

JNIEXPORT void JNICALL
NATIVE_NAME (setComposingText) (JNIEnv *env, jobject object, jshort window,
				jstring composing_text,
				jint new_cursor_position)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;
  unsigned short *text;
  size_t length;

  /* First, obtain a copy of the Java string.  */
  text = android_copy_java_string (env, composing_text, &length);

  if (!text)
    return;

  /* Next, populate the event.  Events will always eventually be
     delivered on Android, so handle_one_android_event can be relied
     on to free text.  */

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_SET_COMPOSING_TEXT;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = min (length, PTRDIFF_MAX);
  event.ime.position = new_cursor_position;
  event.ime.text = text;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (setComposingRegion) (JNIEnv *env, jobject object, jshort window,
				  jint start, jint end)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_SET_COMPOSING_REGION;
  event.ime.start = start + 1;
  event.ime.end = end + 1;
  event.ime.length = 0;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (setSelection) (JNIEnv *env, jobject object, jshort window,
			    jint start, jint end)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  /* While IMEs want access to the entire selection, Emacs only
     supports setting the point.  */

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_SET_POINT;
  event.ime.start = start + 1;
  event.ime.end = end + 1;
  event.ime.length = 0;
  event.ime.position = start;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

/* Structure describing the context for `getSelection'.  */

struct android_get_selection_context
{
  /* The window in question.  */
  android_window window;

  /* The position of the window's point when it was last
     redisplayed, and its last mark if active.  */
  ptrdiff_t point, mark;
};

/* Function run on the main thread by `getSelection'.
   Place the character position of point in PT.  */

static void
android_get_selection (void *data)
{
  struct android_get_selection_context *context;
  struct frame *f;
  struct window *w;
  struct buffer *b;

  context = data;

  /* Look up the associated frame and its selected window.  */
  f = android_window_to_frame (NULL, context->window);

  if (!f)
    context->point = -1;
  else
    {
      w = XWINDOW (f->selected_window);

      /* Return W's point as it is now.  Then, set
	 W->ephemeral_last_point to match the current point.  */
      context->point = window_point (w);
      w->ephemeral_last_point = context->point;

      /* Default context->mark to w->last_point too.  */
      context->mark = context->point;

      /* If the mark is active, then set it properly.  Also, adjust
	 w->last_mark to match.  */
      b = XBUFFER (w->contents);
      if (!NILP (BVAR (b, mark_active)))
	{
	  context->mark = marker_position (BVAR (b, mark));
	  w->last_mark = context->mark;
	}
    }
}

JNIEXPORT jintArray JNICALL
NATIVE_NAME (getSelection) (JNIEnv *env, jobject object, jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  struct android_get_selection_context context;
  jintArray array;
  jint contents[2];

  context.window = window;

  android_sync_edit ();
  if (android_run_in_emacs_thread (android_get_selection,
				   &context))
    return NULL;

  if (context.point == -1)
    return NULL;

  /* Wraparound actually makes more sense than truncation; at least
     editing will sort of work.  Convert the positions to start from
     index 0, as that is what Android expects.  */
  contents[0] = (unsigned int) min (context.point,
				    context.mark) - 1;
  contents[1] = (unsigned int) max (context.point,
				    context.mark) - 1;

  /* Now create the array.  */
  array = (*env)->NewIntArray (env, 2);

  if (!array)
    return NULL;

  /* Set its contents.  */
  (*env)->SetIntArrayRegion (env, array, 0, 2, contents);
  return array;
}

JNIEXPORT void JNICALL
NATIVE_NAME (performEditorAction) (JNIEnv *env, jobject object,
				   jshort window, int action)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  /* It's a good idea to call `android_sync_edit' before sending the
     key event.  Otherwise, if RET causes the current window to be
     changed, any text previously committed might end up in the newly
     selected window.  */

  android_sync_edit ();

  /* Undocumented behavior: performEditorAction is apparently expected
     to finish composing any text.  */

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_FINISH_COMPOSING_TEXT;
  event.ime.start = 0;
  event.ime.end = 0;

  /* This value of `length' means that the input method should receive
     an update containing the new conversion region.  */

  event.ime.length = 1;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);

  /* Finally, send the return key press.  `counter' is set; this means
     that a text conversion barrier will be generated once the event
     is read, which will cause subsequent edits to wait until the
     edits associated with this key press complete.  */

  event.xkey.type = ANDROID_KEY_PRESS;
  event.xkey.serial = ++event_serial;
  event.xkey.window = window;
  event.xkey.time = 0;
  event.xkey.state = 0;
  event.xkey.keycode = 66;
  event.xkey.unicode_char = 0;
  event.xkey.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (performContextMenuAction) (JNIEnv *env, jobject object,
					jshort window, int action)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;
  int key;

  /* Note that ACTION is determined in EmacsInputConnection, and as
     such they are not actual resource IDs.  */

  switch (action)
    {
      /* The subsequent three keycodes are addressed by
	 android_get_keysym_name rather than in keyboard.c.  */

    case 0: /* android.R.id.selectAll */
      key = 65536 + 1;
      break;

    case 1: /* android.R.id.startSelectingText */
      key = 65536 + 2;
      break;

    case 2: /* android.R.id.stopSelectingText */
      key = 65536 + 3;
      break;

    default:
      return;

    case 3: /* android.R.id.cut */
      key = 277;
      break;

    case 4: /* android.R.id.copy */
      key = 278;
      break;

    case 5: /* android.R.id.paste */
      key = 279;
      break;
    }

  event.xkey.type = ANDROID_KEY_PRESS;
  event.xkey.serial = ++event_serial;
  event.xkey.window = window;
  event.xkey.time = 0;
  event.xkey.state = 0;
  event.xkey.keycode = key;
  event.xkey.unicode_char = 0;
  event.xkey.counter = ++edit_counter;

  android_write_event (&event);
}



/* Text extraction.  */

struct android_get_extracted_text_context
{
  /* The parameters of the request.  */
  int hint_max_chars;

  /* Token for the request.  */
  int token;

  /* Flags associated with the request.  */
  int flags;

  /* The returned text, or NULL.  */
  char *text;

  /* The size of that text in characters and bytes.  */
  ptrdiff_t length, bytes;

  /* Offsets into that text.  */
  ptrdiff_t start, start_offset, end_offset;

  /* The window.  */
  android_window window;

  /* Whether or not the mark is active.  */
  bool mark_active;
};

/* Return the extracted text in the extracted text context specified
   by DATA.  Save its flags and token into its frame's state.  */

static void
android_get_extracted_text (void *data)
{
  struct android_get_extracted_text_context *request;
  struct frame *f;

  request = data;

  /* Find the frame associated with the window.  */
  f = android_window_to_frame (NULL, request->window);

  if (!f)
    return;

  /* Now get the extracted text.  */
  request->text
    = get_extracted_text (f, min (request->hint_max_chars, 600),
			  &request->start, &request->start_offset,
			  &request->end_offset, &request->length,
			  &request->bytes, &request->mark_active);

  /* See if request->flags & GET_EXTRACTED_TEXT_MONITOR.  If so, then
     the input method has asked to monitor changes to the extracted
     text until the next IM context reset.  */

  FRAME_ANDROID_OUTPUT (f)->extracted_text_flags = request->flags;
  FRAME_ANDROID_OUTPUT (f)->extracted_text_token = request->token;
  FRAME_ANDROID_OUTPUT (f)->extracted_text_hint = request->hint_max_chars;
}

/* Structure describing the `ExtractedTextRequest' class.
   Valid only on the UI thread.  */

struct android_extracted_text_request_class
{
  bool initialized;
  jfieldID hint_max_chars;
  jfieldID token;
};

/* Structure describing the `ExtractedText' class.
   Valid only on the UI thread.  */

struct android_extracted_text_class
{
  jclass class;
  jmethodID constructor;
  jfieldID flags;
  jfieldID partial_start_offset;
  jfieldID partial_end_offset;
  jfieldID selection_start;
  jfieldID selection_end;
  jfieldID start_offset;
  jfieldID text;
};

/* Fields and methods associated with the `ExtractedTextRequest'
   class.  */
struct android_extracted_text_request_class request_class;

/* Fields and methods associated with the `ExtractedText' class.  */
struct android_extracted_text_class text_class;

/* Return an ExtractedText object corresponding to the extracted text
   TEXT.  START is a character position describing the offset of the
   first character in TEXT.  START_OFFSET is the offset of the lesser
   of point or mark relative to START, and END_OFFSET is that of the
   greater of point or mark relative to START.  MARK_ACTIVE specifies
   whether or not the mark is currently active.

   Assume that request_class and text_class have already been
   initialized.

   Value is NULL if an error occurs; the exception is not cleared,
   else a local reference to the ExtractedText object.  */

static jobject
android_build_extracted_text (jstring text, ptrdiff_t start,
			      ptrdiff_t start_offset,
			      ptrdiff_t end_offset, bool mark_active)
{
  JNIEnv *env;
  jobject object;

  env = android_java_env;

  /* Return NULL if the class has not yet been obtained.  */
  if (!text_class.class)
    return NULL;

  /* Create an ExtractedText object containing this information.  */
  object = (*env)->NewObject (env, text_class.class,
			      text_class.constructor);
  if (!object)
    return NULL;

  (*env)->SetIntField (env, object, text_class.flags,
		       /* ExtractedText.FLAG_SELECTING */
		       mark_active ? 2 : 0);
  (*env)->SetIntField (env, object, text_class.partial_start_offset, -1);
  (*env)->SetIntField (env, object, text_class.partial_end_offset, -1);
  (*env)->SetIntField (env, object, text_class.selection_start,
		       min (start_offset, TYPE_MAXIMUM (jint)));
  (*env)->SetIntField (env, object, text_class.selection_end,
		       min (end_offset, TYPE_MAXIMUM (jint)));

  /* Subtract 1 from start: point indices in Emacs start from 1, but
     Android expects 0.  */
  (*env)->SetIntField (env, object, text_class.start_offset,
		       min (start - 1, TYPE_MAXIMUM (jint)));
  (*env)->SetObjectField (env, object, text_class.text, text);
  return object;
}

JNIEXPORT jobject JNICALL
NATIVE_NAME (getExtractedText) (JNIEnv *env, jobject ignored_object,
				jshort window, jobject request,
				jint flags)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  struct android_get_extracted_text_context context;
  jstring string;
  jclass class;
  jobject object;

  /* Initialize both classes if necessary.  */

  if (!request_class.initialized)
    {
      class
	= (*env)->FindClass (env, ("android/view/inputmethod"
				   "/ExtractedTextRequest"));
      eassert (class);

      request_class.hint_max_chars
	= (*env)->GetFieldID (env, class, "hintMaxChars", "I");
      eassert (request_class.hint_max_chars);

      request_class.token
	= (*env)->GetFieldID (env, class, "token", "I");
      eassert (request_class.token);

      request_class.initialized = true;
    }

  if (!text_class.class)
    {
      text_class.class
	= (*env)->FindClass (env, ("android/view/inputmethod"
				   "/ExtractedText"));
      eassert (text_class.class);

      class
	= text_class.class
	= (*env)->NewGlobalRef (env, text_class.class);
      eassert (text_class.class);

      text_class.flags
	= (*env)->GetFieldID (env, class, "flags", "I");
      text_class.partial_start_offset
	= (*env)->GetFieldID (env, class, "partialStartOffset", "I");
      text_class.partial_end_offset
	= (*env)->GetFieldID (env, class, "partialEndOffset", "I");
      text_class.selection_start
	= (*env)->GetFieldID (env, class, "selectionStart", "I");
      text_class.selection_end
	= (*env)->GetFieldID (env, class, "selectionEnd", "I");
      text_class.start_offset
	= (*env)->GetFieldID (env, class, "startOffset", "I");
      text_class.text
	= (*env)->GetFieldID (env, class, "text", "Ljava/lang/CharSequence;");
      text_class.constructor
	= (*env)->GetMethodID (env, class, "<init>", "()V");
    }

  context.hint_max_chars
    = (*env)->GetIntField (env, request, request_class.hint_max_chars);
  context.token
    = (*env)->GetIntField (env, request, request_class.token);
  context.flags = flags;
  context.text = NULL;
  context.window = window;

  android_sync_edit ();
  if (android_run_in_emacs_thread (android_get_extracted_text,
				   &context))
    return NULL;

  if (!context.text)
    return NULL;

  /* Encode the returned text.  */
  string = android_text_to_string (env, context.text, context.length,
				   context.bytes);
  free (context.text);

  if (!string)
    return NULL;

  /* Create an ExtractedText object containing this information.  */
  object = (*env)->NewObject (env, text_class.class,
			      text_class.constructor);
  if (!object)
    return NULL;

  (*env)->SetIntField (env, object, text_class.flags,
		       /* ExtractedText.FLAG_SELECTING */
		       context.mark_active ? 2 : 0);
  (*env)->SetIntField (env, object, text_class.partial_start_offset, -1);
  (*env)->SetIntField (env, object, text_class.partial_end_offset, -1);
  (*env)->SetIntField (env, object, text_class.selection_start,
		       min (context.start_offset, TYPE_MAXIMUM (jint)));
  (*env)->SetIntField (env, object, text_class.selection_end,
		       min (context.end_offset, TYPE_MAXIMUM (jint)));

  /* Subtract 1 from start: point indices in Emacs start from 1, but
     Android expects 0.  */
  (*env)->SetIntField (env, object, text_class.start_offset,
		       min (context.start - 1, TYPE_MAXIMUM (jint)));
  (*env)->SetObjectField (env, object, text_class.text, string);
  return object;
}



JNIEXPORT jstring JNICALL
NATIVE_NAME (getSelectedText) (JNIEnv *env, jobject object,
			       jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  struct android_get_extracted_text_context context;
  jstring string;

  context.hint_max_chars = -1;
  context.token = 0;
  context.text = NULL;
  context.window = window;

  android_sync_edit ();
  if (android_run_in_emacs_thread (android_get_extracted_text,
				   &context))
    return NULL;

  if (!context.text)
    return NULL;

  /* Encode the returned text.  */
  string = android_text_to_string (env, context.text, context.length,
				   context.bytes);
  free (context.text);

  return string;
}

JNIEXPORT void JNICALL
NATIVE_NAME (requestSelectionUpdate) (JNIEnv *env, jobject object,
				      jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_REQUEST_SELECTION_UPDATE;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = 0;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}

JNIEXPORT void JNICALL
NATIVE_NAME (requestCursorUpdates) (JNIEnv *env, jobject object,
				    jshort window, jint mode)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_REQUEST_CURSOR_UPDATES;
  event.ime.start = 0;
  event.ime.end = 0;
  event.ime.length = mode;
  event.ime.position = 0;
  event.ime.text = NULL;

  /* Since this does not affect the state of the buffer text, there is
     no need to apply synchronization to this event.  */
  event.ime.counter = 0;

  android_write_event (&event);
}

/* Notice that a new input method connection has been initialized and
   clear cursor update requests, extracted text requests, and the
   composing region.  */

JNIEXPORT void JNICALL
NATIVE_NAME (clearInputFlags) (JNIEnv *env, jobject object,
			       jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  union android_event event;

  event.ime.type = ANDROID_INPUT_METHOD;
  event.ime.serial = ++event_serial;
  event.ime.window = window;
  event.ime.operation = ANDROID_IME_FINISH_COMPOSING_TEXT;
  event.ime.start = 0;
  event.ime.end = 0;

  /* This value of `length' means that updates to the cursor position
     and extracted text should not be reported anymore.  */

  event.ime.length = 2;
  event.ime.position = 0;
  event.ime.text = NULL;
  event.ime.counter = ++edit_counter;

  android_write_event (&event);
}



/* Context for a call to `getSurroundingText'.  */

struct android_get_surrounding_text_context
{
  /* Number of characters before the region to return.  */
  int before_length;

  /* Number of characters after the region to return.  */
  int after_length;

  /* The returned text, or NULL.  */
  char *text;

  /* The size of that text in characters and bytes.  */
  ptrdiff_t length, bytes;

  /* Offsets into that text.  */
  ptrdiff_t offset, start, end;

  /* The start and end indices of the conversion region.
     -1 if it does not exist.  */
  ptrdiff_t conversion_start, conversion_end;

  /* The window.  */
  android_window window;
};

/* Return the surrounding text in the surrounding text context
   specified by DATA.  */

static void
android_get_surrounding_text (void *data)
{
  struct android_get_surrounding_text_context *request;
  struct frame *f;
  ptrdiff_t temp;

  request = data;

  /* Find the frame associated with the window.  */
  f = android_window_to_frame (NULL, request->window);

  if (!f)
    return;

  /* Now get the surrounding text.  */
  request->text
    = get_surrounding_text (f, request->before_length,
			    request->after_length, &request->length,
			    &request->bytes, &request->offset,
			    &request->start, &request->end);

  /* Sort request->start and request->end for compatibility with some
     bad input methods.  */

  if (request->end < request->start)
    {
      temp = request->start;
      request->start = request->end;
      request->end = temp;
    }

  /* Retrieve the conversion region.  */

  request->conversion_start = -1;
  request->conversion_end = -1;

  if (MARKERP (f->conversion.compose_region_start))
    {
      request->conversion_start
	= marker_position (f->conversion.compose_region_start) - 1;
      request->conversion_end
	= marker_position (f->conversion.compose_region_end) - 1;
    }
}

/* Return a local reference to a `SurroundingText' object describing
   WINDOW's surrounding text.  ENV should be a valid JNI environment
   for the current thread.

   BEFORE_LENGTH and AFTER_LENGTH specify the number of characters
   around point and mark to return.

   Return the conversion region (or -1) in *CONVERSION_START and
   *CONVERSION_END if non-NULL.

   Value is the object upon success, else NULL.  */

static jobject
android_get_surrounding_text_internal (JNIEnv *env, jshort window,
				       jint before_length,
				       jint after_length,
				       ptrdiff_t *conversion_start,
				       ptrdiff_t *conversion_end)
{
  struct android_get_surrounding_text_context context;
  jstring string;
  jobject object;

  static jclass class;
  static jmethodID constructor;

  /* Initialize CLASS if it has not yet been initialized.  */

  if (!class)
    {
      class
	= (*env)->FindClass (env, ("android/view/inputmethod"
				   "/SurroundingText"));

#if __ANDROID_API__ < 31
      /* If CLASS cannot be found, the version of Android currently
	 running is too old.  */

      if (!class)
	{
	  (*env)->ExceptionClear (env);
	  return NULL;
	}
#else /* __ANDROID_API__ >= 31 */
      eassert (class);
#endif /* __ANDROID_API__ < 31 */

      class = (*env)->NewGlobalRef (env, class);
      if (!class)
	/* Clear class to prevent a local reference from remaining in
	   `class'.  */
	return (class = NULL);

      /* Now look for its constructor.  */
      constructor = (*env)->GetMethodID (env, class, "<init>",
					 "(Ljava/lang/CharSequence;III)V");
      eassert (constructor);
    }

  context.before_length = before_length;
  context.after_length = after_length;
  context.window = window;
  context.text = NULL;

  android_sync_edit ();
  if (android_run_in_emacs_thread (android_get_surrounding_text,
				   &context))
    return NULL;

  if (!context.text)
    return NULL;

  /* Encode the returned text.  */
  string = android_text_to_string (env, context.text, context.length,
				   context.bytes);
  free (context.text);

  if (!string)
    return NULL;

  /* Create an SurroundingText object containing this information.  */
  object = (*env)->NewObject (env, class, constructor, string,
			      (jint) min (context.start,
					  TYPE_MAXIMUM (jint)),
			      (jint) min (context.end,
					  TYPE_MAXIMUM (jint)),
			      /* Adjust point offsets to fit into
				 Android's 0-based indexing. */
			      (jint) min (context.offset - 1,
					  TYPE_MAXIMUM (jint)));
  if (!object)
    return NULL;

  /* Now return the conversion region if that was requested.  */

  if (conversion_start)
    {
      *conversion_start = context.conversion_start;
      *conversion_end = context.conversion_start;
    }

  return object;
}

JNIEXPORT jobject JNICALL
NATIVE_NAME (getSurroundingText) (JNIEnv *env, jobject object,
				  jshort window, jint before_length,
				  jint after_length, jint flags)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  return android_get_surrounding_text_internal (env, window, before_length,
						after_length, NULL, NULL);
}

JNIEXPORT jobject JNICALL
NATIVE_NAME (takeSnapshot) (JNIEnv *env, jobject object, jshort window)
{
  JNI_STACK_ALIGNMENT_PROLOGUE;

  jobject text;
  ptrdiff_t start, end;

  static jclass class;
  static jmethodID constructor;

  /* First, obtain the surrounding text and conversion region.  */
  text = android_get_surrounding_text_internal (env, window, 600, 600,
						&start, &end);

  /* If that fails, return NULL.  */

  if (!text)
    return NULL;

  /* Next, initialize the TextSnapshot class.  */

  if (!class)
    {
      class
	= (*env)->FindClass (env, ("android/view/inputmethod"
				   "/TextSnapshot"));
#if __ANDROID_API__ < 33
      /* If CLASS cannot be found, the version of Android currently
	 running is too old.  */

      if (!class)
	{
	  (*env)->ExceptionClear (env);
	  return NULL;
	}
#else /* __ANDROID_API__ >= 33 */
      eassert (class);
#endif /* __ANDROID_API__ < 33 */

      class = (*env)->NewGlobalRef (env, class);
      if (!class)
	/* Clear class to prevent a local reference from remaining in
	   `class'.  */
	return (class = NULL);

      constructor = (*env)->GetMethodID (env, class, "<init>",
					 "(Landroid/view/inputmethod"
					 "/SurroundingText;III)V");
      eassert (constructor);
    }

  /* Try to create a TextSnapshot object.  */
  eassert (start <= end);
  object = (*env)->NewObject (env, class, constructor, text,
			      (jint) min (start, TYPE_MAXIMUM (jint)),
			      (jint) min (end, TYPE_MAXIMUM (jint)),
			      (jint) 0);
  return object;
}

#ifdef __clang__
#pragma clang diagnostic pop
#else /* GCC */
#pragma GCC diagnostic pop
#endif /* __clang__ */



/* Tell the input method where the composing region and selection of
   F's selected window is located.  W should be F's selected window;
   if it is NULL, then F->selected_window is used in its place.  */

static void
android_update_selection (struct frame *f, struct window *w)
{
  ptrdiff_t start, end, point, mark, start_offset, end_offset;
  ptrdiff_t length, bytes;
  struct buffer *b;
  int hint, token;
  char *text;
  jobject extracted;
  jstring string;
  bool mark_active;

  if (MARKERP (f->conversion.compose_region_start))
    {
      eassert (MARKERP (f->conversion.compose_region_end));

      /* Indexing in android starts from 0 instead of 1.  */
      start = marker_position (f->conversion.compose_region_start) - 1;
      end = marker_position (f->conversion.compose_region_end) - 1;
    }
  else
    start = -1, end = -1;

  /* Now constrain START and END to the maximum size of a Java
     integer.  */
  start = min (start, TYPE_MAXIMUM (jint));
  end = min (end, TYPE_MAXIMUM (jint));

  if (!w)
    w = XWINDOW (f->selected_window);

  /* Figure out where the point and mark are.  If the mark is not
     active, then point is set to equal mark.  */
  b = XBUFFER (w->contents);
  point = min (w->ephemeral_last_point,
	       TYPE_MAXIMUM (jint));
  mark = ((!NILP (BVAR (b, mark_active))
	   && w->last_mark != -1)
	  ? min (w->last_mark, TYPE_MAXIMUM (jint))
	  : point);

  /* Send the update.  Android doesn't employ a concept of ``point''
     and ``mark''; instead, it only has a selection, where the start
     of the selection is less than or equal to the end, and the region
     is ``active'' when those two values differ.  Also, convert the
     indices from 1-based Emacs indices to 0-based Android ones.  */
  android_update_ic (FRAME_ANDROID_WINDOW (f), min (point, mark) - 1,
		     max (point, mark) - 1, start, end);

  /* Update the extracted text as well, if the input method has asked
     for updates.  1 is
     InputConnection.GET_EXTRACTED_TEXT_MONITOR.  */

  if (FRAME_ANDROID_OUTPUT (f)->extracted_text_flags & 1)
    {
      hint = FRAME_ANDROID_OUTPUT (f)->extracted_text_hint;
      token = FRAME_ANDROID_OUTPUT (f)->extracted_text_token;
      text = get_extracted_text (f, min (hint, 600), &start,
				 &start_offset, &end_offset,
				 &length, &bytes, &mark_active);

      if (text)
	{
	  /* Make a string out of the extracted text.  */
	  string = android_text_to_string (android_java_env,
					   text, length, bytes);
	  xfree (text);
	  android_exception_check ();

	  /* Make extracted text out of that string.  */
	  extracted = android_build_extracted_text (string, start,
						    start_offset,
						    end_offset,
						    mark_active);
	  android_exception_check_1 (string);
	  ANDROID_DELETE_LOCAL_REF (string);

	  if (extracted)
	    {
	      /* extracted is now an associated ExtractedText object.
		 Perform the update.  */
	      android_update_extracted_text (FRAME_ANDROID_WINDOW (f),
					     extracted, token);
	      ANDROID_DELETE_LOCAL_REF (extracted);
	    }
	}
    }
}

/* Return whether or not EVENT is an input method event destined for
   the frame (struct frame *) ARG.  */

static bool
android_event_is_for_frame (union android_event *event, void *arg)
{
  struct frame *f;

  f = arg;
  return (event->type == ANDROID_INPUT_METHOD
	  && event->ime.window == FRAME_ANDROID_WINDOW (f));
}

/* Notice that the input method connection to F should be reset as a
   result of a change to its contents.  */

static void
android_reset_conversion (struct frame *f)
{
  enum android_ic_mode mode;
  struct window *w;
  struct buffer *buffer;
  Lisp_Object style;
  union android_event event;

  /* Reset the input method.

     Select an appropriate ``input mode'' based on whether or not the
     minibuffer window is selected, which in turn affects if ``RET''
     inserts a newline or sends an editor action Emacs transforms into
     a key event (refer to `performEditorAction'.)  */

  w = XWINDOW (f->selected_window);
  buffer = XBUFFER (WINDOW_BUFFER (w));

  style = (EQ (find_symbol_value (Qoverriding_text_conversion_style),
	       Qlambda)
	   ? BVAR (buffer, text_conversion_style)
	   : find_symbol_value (Qoverriding_text_conversion_style));

  if (NILP (style) || conversion_disabled_p ())
    mode = ANDROID_IC_MODE_NULL;
  else if (EQ (style, Qaction) || EQ (f->selected_window,
				      f->minibuffer_window))
    mode = ANDROID_IC_MODE_ACTION;
  else
    mode = ANDROID_IC_MODE_TEXT;

  /* Remove any existing input method events that apply to FRAME from
     the event queue.

     There's a small window between this and the call to
     android_reset_ic between which more events can be generated.  */

  while (android_check_if_event (&event, android_event_is_for_frame, f))
    {
      switch (event.ime.operation)
	{
	case ANDROID_IME_COMMIT_TEXT:
	case ANDROID_IME_FINISH_COMPOSING_TEXT:
	case ANDROID_IME_SET_COMPOSING_TEXT:
	  xfree (event.ime.text);
	  break;

	default:
	  break;
	}
    }

  android_reset_ic (FRAME_ANDROID_WINDOW (f), mode);

  /* Clear extracted text flags.  Since the IM has been reinitialized,
     it should no longer be displaying extracted text.  */
  FRAME_ANDROID_OUTPUT (f)->extracted_text_flags = 0;

  /* Move its selection to the specified position.  */
  android_update_selection (f, NULL);
}

/* Notice that point has moved in the F's selected window's selected
   buffer.  W is the window, and BUFFER is that buffer.  */

static void
android_set_point (struct frame *f, struct window *w,
		   struct buffer *buffer)
{
  android_update_selection (f, w);
}

/* Notice that the composition region on F's old selected window has
   changed.  */

static void
android_compose_region_changed (struct frame *f)
{
  android_update_selection (f, XWINDOW (f->old_selected_window));
}

/* Notice that the text conversion has completed.  */

static void
android_notify_conversion (unsigned long counter)
{
  int sval;

  if (last_edit_counter < counter)
    __atomic_store_n (&last_edit_counter, counter,
		      __ATOMIC_SEQ_CST);

  sem_getvalue (&edit_sem, &sval);

  if (sval <= 0)
    sem_post (&edit_sem);
}

/* Android text conversion interface.  */

static struct textconv_interface text_conversion_interface =
  {
    android_reset_conversion,
    android_set_point,
    android_compose_region_changed,
    android_notify_conversion,
  };



extern frame_parm_handler android_frame_parm_handlers[];

#endif /* !ANDROID_STUBIFY */

static struct redisplay_interface android_redisplay_interface =
  {
#ifndef ANDROID_STUBIFY
    android_frame_parm_handlers,
    gui_produce_glyphs,
    gui_write_glyphs,
    gui_insert_glyphs,
    gui_clear_end_of_line,
    android_scroll_run,
    android_after_update_window_line,
    NULL, /* update_window_begin */
    NULL, /* update_window_end   */
    android_flip_and_flush,
    gui_clear_window_mouse_face,
    gui_get_glyph_overhangs,
    gui_fix_overlapping_area,
    android_draw_fringe_bitmap,
    NULL, /* define_fringe_bitmap */
    NULL, /* destroy_fringe_bitmap */
    android_compute_glyph_string_overhangs,
    android_draw_glyph_string,
    android_define_frame_cursor,
    android_clear_frame_area,
    android_clear_under_internal_border,
    android_draw_window_cursor,
    android_draw_vertical_window_border,
    android_draw_window_divider,
    NULL,
    android_show_hourglass,
    android_hide_hourglass,
    android_default_font_parameter,
#endif
  };



void
frame_set_mouse_pixel_position (struct frame *f, int pix_x, int pix_y)
{
  /* This cannot be implemented on Android, and as such is left
     blank.  */
}

char *
get_keysym_name (int keysym)
{
  static char buffer[64];

#ifndef ANDROID_STUBIFY
  android_get_keysym_name (keysym, buffer, 64);
#else
  emacs_abort ();
#endif
  return buffer;
}



/* Create a struct terminal, initialize it with the Android specific
   functions and make DISPLAY->TERMINAL point to it.  */

static struct terminal *
android_create_terminal (struct android_display_info *dpyinfo)
{
  struct terminal *terminal;

  terminal = create_terminal (output_android,
			      &android_redisplay_interface);
  terminal->display_info.android = dpyinfo;
  dpyinfo->terminal = terminal;

  /* kboard is initialized in android_term_init.  */

#ifndef ANDROID_STUBIFY

  terminal->clear_frame_hook = android_clear_frame;
  terminal->ring_bell_hook = android_ring_bell;
  terminal->toggle_invisible_pointer_hook
    = android_toggle_invisible_pointer;
  terminal->update_begin_hook = android_update_begin;
  terminal->update_end_hook = android_update_end;
  terminal->read_socket_hook = android_read_socket;
  terminal->frame_up_to_date_hook = android_frame_up_to_date;
  terminal->buffer_flipping_unblocked_hook
    = android_buffer_flipping_unblocked_hook;
  terminal->defined_color_hook = android_defined_color;
  terminal->query_frame_background_color
    = android_query_frame_background_color;
  terminal->query_colors = android_query_colors;
  terminal->mouse_position_hook = android_mouse_position;
  terminal->get_focus_frame = android_get_focus_frame;
  terminal->focus_frame_hook = android_focus_frame;
  terminal->frame_rehighlight_hook = android_frame_rehighlight_hook;
  terminal->frame_raise_lower_hook = android_frame_raise_lower;
  terminal->frame_visible_invisible_hook
    = android_make_frame_visible_invisible;
  terminal->fullscreen_hook = android_fullscreen_hook;
  terminal->iconify_frame_hook = android_iconify_frame;
  terminal->set_window_size_hook = android_set_window_size;
  terminal->set_frame_offset_hook = android_set_offset;
  terminal->set_frame_alpha_hook = android_set_alpha;
  terminal->set_new_font_hook = android_new_font;
  terminal->set_bitmap_icon_hook = android_bitmap_icon;
  terminal->implicit_set_name_hook = android_implicitly_set_name;
  terminal->menu_show_hook = android_menu_show;
  terminal->popup_dialog_hook = android_popup_dialog;
  terminal->change_tab_bar_height_hook = android_change_tab_bar_height;
  terminal->change_tool_bar_height_hook = android_change_tool_bar_height;
  terminal->set_scroll_bar_default_width_hook
    = android_set_scroll_bar_default_width;
  terminal->set_scroll_bar_default_height_hook
    = android_set_scroll_bar_default_height;
  terminal->free_pixmap = android_free_pixmap_hook;
  terminal->delete_frame_hook = android_delete_frame;
  terminal->delete_terminal_hook = android_delete_terminal;

#else
  emacs_abort ();
#endif

  return terminal;
}

/* Initialize the Android terminal interface.  The display connection
   has already been set up by the system at this point.  */

void
android_term_init (void)
{
  struct terminal *terminal;
  struct android_display_info *dpyinfo;
  Lisp_Object color_file, color_map;

  dpyinfo = xzalloc (sizeof *dpyinfo);
  terminal = android_create_terminal (dpyinfo);
  terminal->kboard = allocate_kboard (Qandroid);
  terminal->kboard->reference_count++;

  dpyinfo->n_planes = 24;

  /* This function should only be called once at startup.  */
  eassert (!x_display_list);
  x_display_list = dpyinfo;

  dpyinfo->name_list_element
    = Fcons (build_pure_c_string ("android"), Qnil);

  color_file = Fexpand_file_name (build_string ("rgb.txt"),
				  Vdata_directory);
  color_map = Fx_load_color_file (color_file);

  if (NILP (color_map))
    fatal ("Could not read %s.\n", SDATA (color_file));

  dpyinfo->color_map = color_map;

#ifndef ANDROID_STUBIFY
  dpyinfo->resx = android_pixel_density_x;
  dpyinfo->resy = android_pixel_density_y;
  dpyinfo->font_resolution = android_scaled_pixel_density;
#endif /* ANDROID_STUBIFY */

  /* https://lists.gnu.org/r/emacs-devel/2015-11/msg00194.html  */
  dpyinfo->smallest_font_height = 1;
  dpyinfo->smallest_char_width = 1;

  terminal->name = xstrdup ("android");

  /* The display "connection" is now set up, and it must never go
     away.  */
  terminal->reference_count = 30000;

  /* Set the baud rate to the same value it gets set to on X.  */
  baud_rate = 19200;

#ifndef ANDROID_STUBIFY
  sem_init (&edit_sem, false, 0);
  register_textconv_interface (&text_conversion_interface);
#endif
}



/* Set Vandroid_build_fingerprint to a reasonable value, and also
   Vandroid_build_manufacturer.  */

static void
android_set_build_fingerprint (void)
{
#ifdef ANDROID_STUBIFY
  Vandroid_build_fingerprint = Qnil;
#else /* !ANDROID_STUBIFY */
  jclass class;
  jfieldID field;
  jobject string;
  const char *data;

  /* Set class to NULL so freeing an uninitialized local ref can be
     avoided.  */
  class = NULL;

  /* Likewise for string.  */
  string = NULL;

  if (!android_init_gui)
    goto fail;
  else
    {
      /* Obtain Build.FINGERPRINT.  Clear exceptions after each query;
	 JNI can't find Build.FINGERPRINT on some systems.  */

      class = (*android_java_env)->FindClass (android_java_env,
					      "android/os/Build");
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!class)
	goto fail;

      field = (*android_java_env)->GetStaticFieldID (android_java_env,
						     class,
						     "FINGERPRINT",
						     "Ljava/lang/String;");
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!field)
	goto fail;

      string
	= (*android_java_env)->GetStaticObjectField (android_java_env,
						     class, field);
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!string)
	goto fail;

      data = (*android_java_env)->GetStringUTFChars (android_java_env,
						     string, NULL);
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!data)
	goto fail;

      Vandroid_build_fingerprint = build_string_from_utf8 (data);
      (*android_java_env)->ReleaseStringUTFChars (android_java_env,
						  string, data);

      /* Now retrieve Build.MANUFACTURER.  */

      ANDROID_DELETE_LOCAL_REF (string);
      string = NULL;

      field = (*android_java_env)->GetStaticFieldID (android_java_env,
						     class,
						     "MANUFACTURER",
						     "Ljava/lang/String;");
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!field)
	goto fail;

      string
	= (*android_java_env)->GetStaticObjectField (android_java_env,
						     class, field);
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!string)
	goto fail;

      data = (*android_java_env)->GetStringUTFChars (android_java_env,
						     string, NULL);
      (*android_java_env)->ExceptionClear (android_java_env);

      if (!data)
	goto fail;

      Vandroid_build_manufacturer = build_string_from_utf8 (data);
      (*android_java_env)->ReleaseStringUTFChars (android_java_env,
						  string, data);
    }

  if (string)
    ANDROID_DELETE_LOCAL_REF (string);

  ANDROID_DELETE_LOCAL_REF (class);

  return;

 fail:
  if (class)
    ANDROID_DELETE_LOCAL_REF (class);

  Vandroid_build_fingerprint = Qnil;
  Vandroid_build_manufacturer = Qnil;
#endif /* ANDROID_STUBIFY */
}

void
syms_of_androidterm (void)
{
  Fprovide (Qandroid, Qnil);

  DEFVAR_LISP ("android-wait-for-event-timeout",
	       Vandroid_wait_for_event_timeout,
    doc: /* How long to wait for Android events.

Emacs will wait up to this many seconds to receive events after
making changes which affect the state of the graphical interface.
Under some situations this can take an indefinite amount of time,
so it is important to limit the wait.

If set to a non-float value, there will be no wait at all.  */);
  Vandroid_wait_for_event_timeout = make_float (0.1);

  DEFVAR_BOOL ("x-use-underline-position-properties",
	       x_use_underline_position_properties,
     doc: /* SKIP: real doc in xterm.c.  */);
  x_use_underline_position_properties = true;
  DEFSYM (Qx_use_underline_position_properties,
	  "x-use-underline-position-properties");

  DEFVAR_BOOL ("x-underline-at-descent-line",
	       x_underline_at_descent_line,
     doc: /* SKIP: real doc in xterm.c.  */);
  x_underline_at_descent_line = false;

  DEFVAR_LISP ("android-build-fingerprint", Vandroid_build_fingerprint,
    doc: /* String identifying the device's OS version.
This is a string that uniquely identifies the version of Android
Emacs is running on.  */);
  Vandroid_build_fingerprint = Qnil;

  DEFVAR_LISP ("android-build-manufacturer", Vandroid_build_manufacturer,
    doc: /* Name of the developer of the running version of Android.  */);
  Vandroid_build_manufacturer = Qnil;

  /* Only defined so loadup.el loads scroll-bar.el.  */
  DEFVAR_LISP ("x-toolkit-scroll-bars", Vx_toolkit_scroll_bars,
    doc: /* SKIP: real doc in xterm.c.  */);
  Vx_toolkit_scroll_bars = Qnil;

  /* Avoid dumping Vandroid_build_fingerprint.  */
  pdumper_do_now_and_after_load (android_set_build_fingerprint);

  DEFSYM (Qx_underline_at_descent_line, "x-underline-at-descent-line");

  /* Symbols defined for DND events.  */
  DEFSYM (Quri, "uri");
  DEFSYM (Qtext, "text");
}

void
mark_androidterm (void)
{
  if (x_display_list)
    mark_object (x_display_list->color_map);
}