summaryrefslogtreecommitdiffstats
path: root/apps/plugins/puzzles/rockbox.c
blob: 7fe7998c39387e34373ef123abd3687660494b41 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016-2020 Franklin Wei
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

/* rockbox frontend for puzzles */

/* This file contains the majority of the rockbox-specific code for
 * the sgt-puzzles port. It implements a set of functions for the
 * backend to call to actually run the games, as well as rockbox UI
 * code (menus, input, etc). For a good overview of the rest of the
 * puzzles code, see:
 *
 * <https://www.chiark.greenend.org.uk/~sgtatham/puzzles/devel/>.
 */

#include "plugin.h"

#include "help.h"
#include "lz4tiny.h"

#include "src/puzzles.h"

#include "lib/keymaps.h"
#include "lib/playback_control.h"
#include "lib/simple_viewer.h"
#include "lib/xlcd.h"

#include "fixedpoint.h"

#include "pluginbitmaps/puzzles_cursor.h"

/* how many ticks between timer callbacks */
#define TIMER_INTERVAL (HZ / 50)

/* Disable some features if we're memory constrained (c200v2) */
#if PLUGIN_BUFFER_SIZE > 0x14000
#define DEBUG_MENU
#define FONT_CACHING
#endif

/* background color (mimicking from the JS frontend) */
#define BG_R .9f /* very light gray */
#define BG_G .9f
#define BG_B .9f
#define BG_COLOR LCD_RGBPACK((int)(255*BG_R), (int)(255*BG_G), (int)(255*BG_B))

/* used for invalid config value message */
#define ERROR_COLOR LCD_RGBPACK(255, 0, 0)

/* subtract two to allow for the fixed and UI fonts */
#define MAX_FONTS (MAXUSERFONTS - 2)
#define FONT_TABLE PLUGIN_GAMES_DATA_DIR "/.sgt-puzzles.fnttab"

/* font bundle size range (in pixels) */
#define BUNDLE_MIN 7
#define BUNDLE_MAX 36
#define BUNDLE_COUNT (BUNDLE_MAX - BUNDLE_MIN + 1)

/* max length of C_STRING config vals */
#define MAX_STRLEN 128

/* attempt to increment a numeric config value up to this much */
#define CHOOSER_MAX_INCR 2

/* max font table line */
#define MAX_LINE 128

/* Sorry. */
#define MURICA

#ifdef MURICA
#define midend_serialize midend_serialise
#define midend_deserialize midend_deserialise
#define frontend_default_color frontend_default_colour
#define midend_colors midend_colours
#endif

/* magnification factor */
#define ZOOM_FACTOR 3

/* distance to pan per click (in pixels) */
#define PAN_X (MIN(LCD_HEIGHT, LCD_WIDTH) / 4)
#define PAN_Y (MIN(LCD_HEIGHT, LCD_WIDTH) / 4)

/* utility macros */
#undef ABS
#define ABS(a) ((a)<0?-(a):(a))
#define SWAP(a, b, t) do { t = a; a = b; b = t; } while(0)

/* fixed-point stuff (for antialiased lines) */
#define fp_fpart(f, bits) ((f) & ((1 << (bits)) - 1))
#define fp_rfpart(f, bits) ((1 << (bits)) - fp_fpart(f, bits))
#define FRACBITS 16

static void fix_size(void);
static int pause_menu(void);
static inline void plot(fb_data *fb, int w, int h,
                        unsigned x, unsigned y, unsigned long a,
                        unsigned long r1, unsigned long g1, unsigned long b1,
                        unsigned cl, unsigned cr, unsigned cu, unsigned cd);

static midend *me = NULL;
static unsigned *colors = NULL;
static int ncolors = 0;
static long last_keystate = 0;

#if defined(FOR_REAL) && defined(DEBUG_MENU)
/* the "debug menu" is hidden by default in order to avoid the
 * naturally ensuing complaints from users */
static bool debug_mode = false;
static int help_times = 0;
#endif

/* clipping stuff */
static struct viewport clip_rect;
static bool clipped = false, zoom_enabled = false, view_mode = true, mouse_mode = false;

static int mouse_x, mouse_y;

extern bool audiobuf_available; /* defined in rbmalloc.c */

static fb_data *zoom_fb; /* dynamically allocated */
static int zoom_x, zoom_y, zoom_w, zoom_h, zoom_clipu, zoom_clipd, zoom_clipl, zoom_clipr;
static int cur_font = FONT_UI;

static bool need_draw_update = false;
static int ud_l = 0, ud_u = 0, ud_r = LCD_WIDTH, ud_d = LCD_HEIGHT;

static char *titlebar = NULL;

/* some games can run in a separate thread for a larger stack (only
 * Solo for now) */
static int thread = -1;

/* how to process the input (custom, per-game) */
static struct {
    bool want_spacebar; /* send spacebar event on long-press of select */
    bool falling_edge; /* send events upon button release, not initial press */
    bool ignore_repeats; /* ignore repeated button events (currently in all games but Untangle) */
    bool rclick_on_hold; /* if in mouse mode, send right-click on long-press of select */
    bool numerical_chooser; /* repurpose select to activate a numerical chooser */
} input_settings;

static bool accept_input = true;

/* last timer call */
static long last_tstamp;
static bool timer_on = false;

static bool load_success;

/* debug settings */
/* ...did I mention there's a secret debug menu? */
static struct {
    int slowmo_factor;
    bool timerflash, clipoff, shortcuts, no_aa, polyanim;
} debug_settings;

// used in menu titles - make sure to initialize!
static char menu_desc[32];

/*
 * These are re-implementations of many rockbox drawing functions, adapted to
 * draw into a custom framebuffer (used for the zoom feature):
 */

static void zoom_drawpixel(int x, int y)
{
    if(y < zoom_clipu || y >= zoom_clipd)
        return;

    if(x < zoom_clipl || x >= zoom_clipr)
        return;

#if LCD_DEPTH > 24
    unsigned int pix = rb->lcd_get_foreground();
    zoom_fb[y * zoom_w + x].b = RGB_UNPACK_BLUE(pix);
    zoom_fb[y * zoom_w + x].g = RGB_UNPACK_GREEN(pix);
    zoom_fb[y * zoom_w + x].r = RGB_UNPACK_RED(pix);
    zoom_fb[y * zoom_w + x].x = 255;
#elif LCD_DEPTH == 24
    /* I hate these */
    unsigned int pix = rb->lcd_get_foreground();
    zoom_fb[y * zoom_w + x].b = RGB_UNPACK_BLUE(pix);
    zoom_fb[y * zoom_w + x].g = RGB_UNPACK_GREEN(pix);
    zoom_fb[y * zoom_w + x].r = RGB_UNPACK_RED(pix);
#else
    zoom_fb[y * zoom_w + x] = rb->lcd_get_foreground();
#endif
}

static void zoom_hline(int l, int r, int y)
{
    if(y < zoom_clipu || y >= zoom_clipd)
        return;
    if(l > r)
    {
        int t = l;
        l = r;
        r = t;
    }

    if(l < zoom_clipl)
        l = zoom_clipl;
    if(r >= zoom_clipr)
        r = zoom_clipr;

#if LCD_DEPTH > 24
    fb_data pixel = { RGB_UNPACK_BLUE(rb->lcd_get_foreground()),
                      RGB_UNPACK_GREEN(rb->lcd_get_foreground()),
                      RGB_UNPACK_RED(rb->lcd_get_foreground()),
                      255
    };
#elif LCD_DEPTH == 24
    fb_data pixel = { RGB_UNPACK_BLUE(rb->lcd_get_foreground()),
                      RGB_UNPACK_GREEN(rb->lcd_get_foreground()),
                      RGB_UNPACK_RED(rb->lcd_get_foreground()) };
#else
    fb_data pixel = rb->lcd_get_foreground();
#endif

    fb_data *ptr = zoom_fb + y * zoom_w + l;
    for(int i = 0; i < r - l; ++i)
        *ptr++ = pixel;
}

static void zoom_fillcircle(int cx, int cy, int radius)
{
    /* copied straight from xlcd_draw.c */
    int d = 3 - (radius * 2);
    int x = 0;
    int y = radius;
    while(x <= y)
    {
        zoom_hline(cx - x, cx + x, cy + y);
        zoom_hline(cx - x, cx + x, cy - y);
        zoom_hline(cx - y, cx + y, cy + x);
        zoom_hline(cx - y, cx + y, cy - x);
        if(d < 0)
        {
            d += (x * 4) + 6;
        }
        else
        {
            d += ((x - y) * 4) + 10;
            --y;
        }
        ++x;
    }
}

static void zoom_drawcircle(int cx, int cy, int radius)
{
    int d = 3 - (radius * 2);
    int x = 0;
    int y = radius;
    while(x <= y)
    {
        zoom_drawpixel(cx + x, cy + y);
        zoom_drawpixel(cx - x, cy + y);
        zoom_drawpixel(cx + x, cy - y);
        zoom_drawpixel(cx - x, cy - y);
        zoom_drawpixel(cx + y, cy + x);
        zoom_drawpixel(cx - y, cy + x);
        zoom_drawpixel(cx + y, cy - x);
        zoom_drawpixel(cx - y, cy - x);
        if(d < 0)
        {
            d += (x * 4) + 6;
        }
        else
        {
            d += ((x - y) * 4) + 10;
            --y;
        }
        ++x;
    }
}

/* This format is pretty crazy: each byte holds the states of 8 pixels
 * in a column, with bit 0 being the topmost pixel. Who needs cache
 * efficiency? */
static void zoom_mono_bitmap(const unsigned char *bits, int x, int y, int w, int h)
{
    unsigned int pix = rb->lcd_get_foreground();
    for(int i = 0; i < h / 8 + 1; ++i)
    {
        for(int j = 0; j < w; ++j)
        {
            unsigned char column = bits[i * w + j];
            for(int dy = 0; dy < 8; ++dy)
            {
                if(column & 1)
                {
#if LCD_DEPTH > 24
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].b = RGB_UNPACK_BLUE(pix);
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].g = RGB_UNPACK_GREEN(pix);
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].r = RGB_UNPACK_RED(pix);
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].x = 255;
#elif LCD_DEPTH == 24
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].b = RGB_UNPACK_BLUE(pix);
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].g = RGB_UNPACK_GREEN(pix);
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j].r = RGB_UNPACK_RED(pix);
#else
                    zoom_fb[(y + i * 8 + dy) * zoom_w + x + j] = pix;
#endif
                }
                column >>= 1;
            }
        }
    }
}

/*
 * Rockbox's alpha format is actually pretty sane: each byte holds
 * alpha values for two horizontally adjacent pixels. Low half is
 * leftmost pixel. See lcd-16bit-common.c for more info.
 */
static void zoom_alpha_bitmap(const unsigned char *bits, int x, int y, int w, int h)
{
    const unsigned char *ptr = bits;
    unsigned char buf;
    int n_read = 0; /* how many 4-bit nibbles we've read (read new when even) */

    unsigned int pix = rb->lcd_get_foreground();
    unsigned int r = RGB_UNPACK_RED(pix), g = RGB_UNPACK_GREEN(pix), b = RGB_UNPACK_BLUE(pix);

    for(int i = 0; i < h; ++i)
    {
        for(int j = 0; j < w; ++j)
        {
            if(n_read % 2 == 0)
            {
                buf = *ptr++;
            }
            int pix_alpha = (n_read++ % 2) ? buf >> 4 : buf & 0xF; /* in reverse order */

            pix_alpha = 15 - pix_alpha; /* correct order now, still 0-F */

            int plot_alpha = (pix_alpha << 4) | pix_alpha; /* so 0xF -> 0xFF, 0x1 -> 0x11, etc. */

            plot(zoom_fb, zoom_w, zoom_h, x + j, y + i, plot_alpha, r, g, b,
                 0, zoom_w, 0, zoom_h);
        }
    }
}

/*
 * Font management routines
 *
 * Many puzzles need a dynamic font size, especially when zooming
 * in. Rockbox's default font set does not provide the consistency we
 * need across different sizes, so instead we ship a custom font pack
 * for sgt-puzzles, available from [1] or through Rockbox Utility.
 *
 * The font pack consists of 3 small-size fonts, and the Deja Vu
 * Sans/Mono fonts, rasterized in sizes from 10 to BUNDLE_MAX
 * (currently 36).
 *
 * The font loading code below tries to be smart about loading fonts:
 * when games are saved, the set of fonts that were loaded during
 * execution is written to a "font table" on disk. On subsequent
 * loads, the fonts in this table are precached while the game is
 * loaded (and the disk is spinning, on hard drive devices). We also
 * have a form of LRU caching implemented to dynamically evict fonts
 * from Rockbox's in-memory cache, which is of limited size.
 *
 * [1]: http://download.rockbox.org/useful/sgt-fonts.zip
 */

static struct bundled_font {
    int status; /* -3 = never tried loading, or unloaded, -2 = failed to load, >= -1: loaded successfully */
    int last_use;
} *loaded_fonts = NULL; /* monospace are first, then proportional */

static int n_fonts, access_counter = -1;

/* called on exit and before entering help viewer (workaround for a
   possible bug in simple_viewer) */
static void unload_fonts(void)
{
    for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
        if(loaded_fonts[i].status > 0) /* don't unload FONT_UI */
        {
            rb->font_unload(loaded_fonts[i].status);
            loaded_fonts[i].status = -3;
        }
    access_counter = -1;
    rb->lcd_setfont(cur_font = FONT_UI);
}

static void init_fonttab(void)
{
    loaded_fonts = smalloc(2 * BUNDLE_COUNT * sizeof(struct bundled_font));
    for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
        loaded_fonts[i].status = -3;
    access_counter = 0;
    n_fonts = 0;
}

static void font_path(char *buf, int type, int size)
{
    if(size < 10) /* Deja Vu only goes down to 10px, below that it's a giant blob */
    {
        if(size < 7)
            size = 7; /* we're not going to force anyone to read 05-Tiny :P */
        /* we also don't care about monospace/proportional at this point */
        switch(size)
        {
        case 7:
            rb->snprintf(buf, MAX_PATH, FONT_DIR "/07-Fixed.fnt");
            break;
        case 8:
            rb->snprintf(buf, MAX_PATH, FONT_DIR "/08-Rockfont.fnt");
            break;
        case 9:
            rb->snprintf(buf, MAX_PATH, FONT_DIR "/09-Fixed.fnt");
            break;
        default:
            assert(false);
        }
    }
    else
        rb->snprintf(buf, MAX_PATH, FONT_DIR "/%02d-%s.fnt", size, type == FONT_FIXED ? "DejaVuSansMono" : "DejaVuSans");
}

static void rb_setfont(int type, int size)
{
    /*
     * First, clamp to range. No puzzle should ever need this large of
     * a font, anyways.
     */
    if(size > BUNDLE_MAX)
        size = BUNDLE_MAX;

    if(size < 10)
    {
        if(size < 7) /* no teeny-tiny fonts */
            size = 7;
        /* assume monospace for 7-9px fonts */
        type = FONT_FIXED;
    }

    int font_idx = (type == FONT_FIXED ? 0 : BUNDLE_COUNT) + size - BUNDLE_MIN;
    switch(loaded_fonts[font_idx].status)
    {
    case -3:
    {
        /* never loaded */
        char buf[MAX_PATH];
        font_path(buf, type, size);
        if(n_fonts >= MAX_FONTS)
        {
            /* unload an old font */
            int oldest_use = -1, oldest_idx = -1;
            for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
            {
                if((loaded_fonts[i].status >= 0 && loaded_fonts[i].last_use < oldest_use) || oldest_use < 0)
                {
                    oldest_use = loaded_fonts[i].last_use;
                    oldest_idx = i;
                }
            }
            assert(oldest_idx >= 0);
            rb->font_unload(loaded_fonts[oldest_idx].status);
            loaded_fonts[oldest_idx].status = -3;
            n_fonts--;
        }

        loaded_fonts[font_idx].status = rb->font_load(buf);
        if(loaded_fonts[font_idx].status < 0)
            goto fallback;
        loaded_fonts[font_idx].last_use = access_counter++;
        n_fonts++;
        cur_font = loaded_fonts[font_idx].status;
        rb->lcd_setfont(cur_font);
        break;
    }
    case -2:
    case -1:
        goto fallback;
    default:
        loaded_fonts[font_idx].last_use = access_counter++;
        cur_font = loaded_fonts[font_idx].status;
        rb->lcd_setfont(cur_font);
        break;
    }

    return;

fallback:
    cur_font = type == FONT_FIXED ? FONT_SYSFIXED : FONT_UI;
    rb->lcd_setfont(cur_font);

    return;
}

/*** Drawing API (normal, no zoom) ***/

static void offset_coords(int *x, int *y)
{
    if(clipped)
    {
        *x -= clip_rect.x;
        *y -= clip_rect.y;
    }
}

static void rb_color(int n)
{
    if(n < 0)
    {
        fatal("bad color %d", n);
        return;
    }
    rb->lcd_set_foreground(colors[n]);
}

/* clipping is implemented through viewports and offsetting
 * coordinates */
static void rb_clip(void *handle, int x, int y, int w, int h)
{
    if(!zoom_enabled)
    {
        if(!debug_settings.clipoff)
        {
            LOGF("rb_clip(%d %d %d %d)", x, y, w, h);
            clip_rect.x = MAX(0, x);
            clip_rect.y = MAX(0, y);
            clip_rect.width  = MIN(LCD_WIDTH, w);
            clip_rect.height = MIN(LCD_HEIGHT, h);
            clip_rect.font = FONT_UI;
            clip_rect.drawmode = DRMODE_SOLID;
#if LCD_DEPTH > 1
            clip_rect.fg_pattern = LCD_DEFAULT_FG;
            clip_rect.bg_pattern = LCD_DEFAULT_BG;
#endif
            rb->lcd_set_viewport(&clip_rect);
            clipped = true;
        }
    }
    else
    {
        zoom_clipu = y;
        zoom_clipd = y + h;
        zoom_clipl = x;
        zoom_clipr = x + w;
    }
}

static void rb_unclip(void *handle)
{
    if(!zoom_enabled)
    {
        LOGF("rb_unclip");
        rb->lcd_set_viewport(NULL);
        clipped = false;
    }
    else
    {
        zoom_clipu = 0;
        zoom_clipd = zoom_h;
        zoom_clipl = 0;
        zoom_clipr = zoom_w;
    }
}

static void rb_draw_text(void *handle, int x, int y, int fonttype,
                         int fontsize, int align, int color, const char *text)
{
    (void) fontsize;

    LOGF("rb_draw_text(%d %d \"%s\" size=%d)", x, y, text, fontsize);

    rb_color(color);
    rb_setfont(fonttype, fontsize); /* size will be clamped if too large */

    int w, h;
    rb->font_getstringsize(text, &w, &h, cur_font);

    if(align & ALIGN_VNORMAL)
        y -= h;
    else if(align & ALIGN_VCENTRE)
        y -= h / 2;

    if(align & ALIGN_HCENTRE)
        x -= w / 2;
    else if(align & ALIGN_HRIGHT)
        x -= w;

    LOGF("calculated origin: (%d, %d) size: (%d, %d)", x, y, w, h);

    if(!zoom_enabled)
    {
        offset_coords(&x, &y);

        rb->lcd_set_drawmode(DRMODE_FG);
        rb->lcd_putsxy(x, y, text);
        rb->lcd_set_drawmode(DRMODE_SOLID);
    }
    else
    {
        /* we need to access the font bitmap directly */
        struct font *pf = rb->font_get(cur_font);

        while(*text)
        {
            /* still only reads 1 byte */
            unsigned short c = *text++;
            const unsigned char *bits = rb->font_get_bits(pf, c);
            int width = rb->font_get_width(pf, c);

            /* straight from lcd-bitmap-common.c */
#if defined(HAVE_LCD_COLOR)
            if (pf->depth)
            {
                /* lcd_alpha_bitmap_part isn't exported directly. However,
                 * we can create a null bitmap struct with only an alpha
                 * channel to make lcd_bmp_part call it for us. */

                zoom_alpha_bitmap(bits, x, y, width, pf->height);
            }
            else
#endif
                zoom_mono_bitmap(bits, x, y, width, pf->height);
            x += width;
        }
    }
}

static void rb_draw_rect(void *handle, int x, int y, int w, int h, int color)
{
    rb_color(color);
    if(!zoom_enabled)
    {
        LOGF("rb_draw_rect(%d, %d, %d, %d, %d)", x, y, w, h, color);
        offset_coords(&x, &y);
        rb->lcd_fillrect(x, y, w, h);
    }
    else
    {
        /* TODO: clipping */
        for(int i = y; i < y + h; ++i)
        {
            zoom_hline(x, x + w, i);
        }
    }
}

/* a goes from 0-255, with a = 255 being fully opaque and a = 0 transparent */
static inline void plot(fb_data *fb, int w, int h,
                        unsigned x, unsigned y, unsigned long a,
                        unsigned long r1, unsigned long g1, unsigned long b1,
                        unsigned cl, unsigned cr, unsigned cu, unsigned cd)
{
    /* This is really quite possibly the least efficient way of doing
       this. A better way would be in draw_antialiased_line(), but the
       problem with that is that the algorithms I investigated at
       least were incorrect at least part of the time and didn't make
       drawing much faster overall. */
    if(!(cl <= x && x < cr && cu <= y && y < cd))
        return;

    fb_data *ptr = fb + y * w + x;
    fb_data orig = *ptr;
    unsigned long r2, g2, b2;
#if LCD_DEPTH < 24
    r2 = RGB_UNPACK_RED(orig);
    g2 = RGB_UNPACK_GREEN(orig);
    b2 = RGB_UNPACK_BLUE(orig);
#else
    r2 = orig.r;
    g2 = orig.g;
    b2 = orig.b;
#endif

    unsigned long r, g, b;
    r = ((r1 * a) + (r2 * (256 - a))) >> 8;
    g = ((g1 * a) + (g2 * (256 - a))) >> 8;
    b = ((b1 * a) + (b2 * (256 - a))) >> 8;

#if LCD_DEPTH < 24
    *ptr = LCD_RGBPACK(r, g, b);
#elif LCD_DEPTH > 24
    *ptr = (fb_data) {b, g, r, 255};
#else
    *ptr = (fb_data) {b, g, r};
#endif
}

/* speed benchmark: 34392 lines/sec vs 112687 non-antialiased
 * lines/sec at full optimization on ipod6g */

/* expects UN-OFFSET coordinates, directly access framebuffer */
static void draw_antialiased_line(fb_data *fb, int w, int h, int x0, int y0, int x1, int y1)
{
    /* fixed-point Wu's algorithm, modified for integer-only endpoints */

    /* passed to plot() to avoid re-calculation */
    unsigned short l = 0, r = w, u = 0, d = h;
    if(!zoom_enabled)
    {
        if(clipped)
        {
            l = clip_rect.x;
            r = clip_rect.x + clip_rect.width;
            u = clip_rect.y;
            d = clip_rect.y + clip_rect.height;
        }
    }
    else
    {
        l = zoom_clipl;
        r = zoom_clipr;
        u = zoom_clipu;
        d = zoom_clipd;
    }

    bool steep = ABS(y1 - y0) > ABS(x1 - x0);
    int tmp;
    if(steep)
    {
        SWAP(x0, y0, tmp);
        SWAP(x1, y1, tmp);
    }
    if(x0 > x1)
    {
        SWAP(x0, x1, tmp);
        SWAP(y0, y1, tmp);
    }

    int dx, dy;
    dx = x1 - x0;
    dy = y1 - y0;

    if(!(dx << FRACBITS))
        return; /* bail out */

    long gradient = fp_div(dy << FRACBITS, dx << FRACBITS, FRACBITS);
    long intery = (y0 << FRACBITS);

    unsigned color = rb->lcd_get_foreground();
    unsigned long r1, g1, b1;
    r1 = RGB_UNPACK_RED(color);
    g1 = RGB_UNPACK_GREEN(color);
    b1 = RGB_UNPACK_BLUE(color);

    /* main loop */
    if(steep)
    {
        for(int x = x0; x <= x1; ++x, intery += gradient)
        {
            unsigned y = intery >> FRACBITS;
            unsigned alpha = fp_fpart(intery, FRACBITS) >> (FRACBITS - 8);

            plot(fb, w, h, y, x, (1 << 8) - alpha, r1, g1, b1, l, r, u, d);
            plot(fb, w, h, y + 1, x, alpha, r1, g1, b1, l, r, u, d);
        }
    }
    else
    {
        for(int x = x0; x <= x1; ++x, intery += gradient)
        {
            unsigned y = intery >> FRACBITS;
            unsigned alpha = fp_fpart(intery, FRACBITS) >> (FRACBITS - 8);

            plot(fb, w, h, x, y, (1 << 8) - alpha, r1, g1, b1, l, r, u, d);
            plot(fb, w, h, x, y + 1, alpha, r1, g1, b1, l, r, u, d);
        }
    }
}

static void rb_draw_line(void *handle, int x1, int y1, int x2, int y2,
                         int color)
{
    rb_color(color);

    if(!zoom_enabled)
    {
        LOGF("rb_draw_line(%d, %d, %d, %d, %d)", x1, y1, x2, y2, color);
#if defined(FOR_REAL) && defined(DEBUG_MENU)
        if(debug_settings.no_aa)
        {
            offset_coords(&x1, &y1);
            offset_coords(&x2, &y2);
            rb->lcd_drawline(x1, y1, x2, y2);
        }
        else
#endif
            draw_antialiased_line(rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT, x1, y1, x2, y2);
    }
    else
    {
        /* draw_antialiased_line uses rb->lcd_get_foreground() to get
         * the color */

        draw_antialiased_line(zoom_fb, zoom_w, zoom_h, x1, y1, x2, y2);
    }
}

#if 0
/*
 * draw filled polygon
 * originally by Sebastian Leonhardt (ulmutul)
 * 'count' : number of coordinate pairs
 * 'pxy': array of coordinates. pxy[0]=x0,pxy[1]=y0,...
 * note: provide space for one extra coordinate, because the starting point
 * will automatically be inserted as end point.
 */

/*
 * helper function:
 * find points of intersection between polygon and scanline
 */

#define MAX_INTERSECTION 32

static void fill_poly_line(int scanline, int count, int *pxy)
{
    int i;
    int j;
    int num_of_intersects;
    int direct, old_direct;
    //intersections of every line with scanline (y-coord)
    int intersection[MAX_INTERSECTION];
    /* add starting point as ending point */
    pxy[count*2] = pxy[0];
    pxy[count*2+1] = pxy[1];

    old_direct=0;
    num_of_intersects=0;
    for (i=0; i<count*2; i+=2) {
        int x1=pxy[i];
        int y1=pxy[i+1];
        int x2=pxy[i+2];
        int y2=pxy[i+3];
        // skip if line is outside of scanline
        if (y1 < y2) {
            if (scanline < y1 || scanline > y2)
                continue;
        }
        else {
            if (scanline < y2 || scanline > y1)
                continue;
        }
        // calculate x-coord of intersection
        if (y1==y2) {
            direct=0;
        }
        else {
            direct = y1>y2 ? 1 : -1;
            // omit double intersections, if both lines lead in the same direction
            intersection[num_of_intersects] =
                x1+((scanline-y1)*(x2-x1))/(y2-y1);
            if ( (direct!=old_direct)
                 || (intersection[num_of_intersects] != intersection[num_of_intersects-1])
                )
                ++num_of_intersects;
        }
        old_direct = direct;
    }

    // sort points of intersection
    for (i=0; i<num_of_intersects-1; ++i) {
        for (j=i+1; j<num_of_intersects; ++j) {
            if (intersection[j]<intersection[i]) {
                int temp=intersection[i];
                intersection[i]=intersection[j];
                intersection[j]=temp;
            }
        }
    }
    // draw
    for (i=0; i<num_of_intersects; i+=2) {
        rb->lcd_hline(intersection[i], intersection[i+1], scanline);
    }
}

/* two extra elements at end of pxy needed */
static void v_fillarea(int count, int *pxy)
{
    int i;
    int y1, y2;

    // find min and max y coords
    y1=y2=pxy[1];
    for (i=3; i<count*2; i+=2) {
        if (pxy[i] < y1) y1 = pxy[i];
        else if (pxy[i] > y2) y2 = pxy[i];
    }

    for (i=y1; i<=y2; ++i) {
        fill_poly_line(i, count, pxy);
    }
}
#endif

/* I'm a horrible person: this was copy-pasta'd straight from
 * xlcd_draw.c */

/* sort the given coordinates by increasing x value */
static void sort_points_by_increasing_x(int* x1, int* y1,
                                        int* x2, int* y2,
                                        int* x3, int* y3)
{
    int x, y;
    if (*x1 > *x3)
    {
        if (*x2 < *x3)       /* x2 < x3 < x1 */
        {
            x = *x1; *x1 = *x2; *x2 = *x3; *x3 = x;
            y = *y1; *y1 = *y2; *y2 = *y3; *y3 = y;
        }
        else if (*x2 > *x1)  /* x3 < x1 < x2 */
        {
            x = *x1; *x1 = *x3; *x3 = *x2; *x2 = x;
            y = *y1; *y1 = *y3; *y3 = *y2; *y2 = y;
        }
        else               /* x3 <= x2 <= x1 */
        {
            x = *x1; *x1 = *x3; *x3 = x;
            y = *y1; *y1 = *y3; *y3 = y;
        }
    }
    else
    {
        if (*x2 < *x1)       /* x2 < x1 <= x3 */
        {
            x = *x1; *x1 = *x2; *x2 = x;
            y = *y1; *y1 = *y2; *y2 = y;
        }
        else if (*x2 > *x3)  /* x1 <= x3 < x2 */
        {
            x = *x2; *x2 = *x3; *x3 = x;
            y = *y2; *y2 = *y3; *y3 = y;
        }
        /* else already sorted */
    }
}

#define sort_points_by_increasing_y(x1, y1, x2, y2, x3, y3)     \
    sort_points_by_increasing_x(y1, x1, y2, x2, y3, x3)

/* draw a filled triangle, using horizontal lines for speed */
static void zoom_filltriangle(int x1, int y1,
                              int x2, int y2,
                              int x3, int y3)
{
    long fp_x1, fp_x2, fp_dx1, fp_dx2;
    int y;
    sort_points_by_increasing_y(&x1, &y1, &x2, &y2, &x3, &y3);

    if (y1 < y3)  /* draw */
    {
        fp_dx1 = ((x3 - x1) << 16) / (y3 - y1);
        fp_x1  = (x1 << 16) + (1<<15) + (fp_dx1 >> 1);

        if (y1 < y2)  /* first part */
        {
            fp_dx2 = ((x2 - x1) << 16) / (y2 - y1);
            fp_x2  = (x1 << 16) + (1<<15) + (fp_dx2 >> 1);
            for (y = y1; y < y2; y++)
            {
                zoom_hline(fp_x1 >> 16, fp_x2 >> 16, y);
                fp_x1 += fp_dx1;
                fp_x2 += fp_dx2;
            }
        }
        if (y2 < y3)  /* second part */
        {
            fp_dx2 = ((x3 - x2) << 16) / (y3 - y2);
            fp_x2 = (x2 << 16) + (1<<15) + (fp_dx2 >> 1);
            for (y = y2; y < y3; y++)
            {
                zoom_hline(fp_x1 >> 16, fp_x2 >> 16, y);
                fp_x1 += fp_dx1;
                fp_x2 += fp_dx2;
            }
        }
    }
}

/* Should probably refactor this */
static void rb_draw_poly(void *handle, int *coords, int npoints,
                         int fillcolor, int outlinecolor)
{
    if(!zoom_enabled)
    {
        LOGF("rb_draw_poly");

        if(fillcolor >= 0)
        {
            rb_color(fillcolor);
#if 1
            /* serious hack: draw a bunch of triangles between adjacent points */
            /* this generally works, even with some concave polygons */
            for(int i = 2; i < npoints; ++i)
            {
                int x1, y1, x2, y2, x3, y3;
                x1 = coords[0];
                y1 = coords[1];
                x2 = coords[(i - 1) * 2];
                y2 = coords[(i - 1) * 2 + 1];
                x3 = coords[i * 2];
                y3 = coords[i * 2 + 1];
                offset_coords(&x1, &y1);
                offset_coords(&x2, &y2);
                offset_coords(&x3, &y3);
                xlcd_filltriangle(x1, y1,
                                  x2, y2,
                                  x3, y3);

#ifdef DEBUG_MENU
                if(debug_settings.polyanim)
                {
                    rb->lcd_update();
                    rb->sleep(HZ/4);
                }
#endif
#if 0
                /* debug code */
                rb->lcd_set_foreground(LCD_RGBPACK(255,0,0));
                rb->lcd_drawpixel(x1, y1);
                rb->lcd_drawpixel(x2, y2);
                rb->lcd_drawpixel(x3, y3);
                rb->lcd_update();
                rb->sleep(HZ);
                rb_color(fillcolor);
                rb->lcd_drawpixel(x1, y1);
                rb->lcd_drawpixel(x2, y2);
                rb->lcd_drawpixel(x3, y3);
                rb->lcd_update();
#endif
            }
#else
            int *pxy = smalloc(sizeof(int) * 2 * npoints + 2);
            /* copy points, offsetted */
            for(int i = 0; i < npoints; ++i)
            {
                pxy[2 * i + 0] = coords[2 * i + 0];
                pxy[2 * i + 1] = coords[2 * i + 1];
                offset_coords(&pxy[2*i+0], &pxy[2*i+1]);
            }
            v_fillarea(npoints, pxy);
            sfree(pxy);
#endif
        }

        /* draw outlines last so they're not covered by the fill */
        assert(outlinecolor >= 0);
        rb_color(outlinecolor);

        for(int i = 1; i < npoints; ++i)
        {
            int x1, y1, x2, y2;
            x1 = coords[2 * (i - 1)];
            y1 = coords[2 * (i - 1) + 1];
            x2 = coords[2 * i];
            y2 = coords[2 * i + 1];
            if(debug_settings.no_aa)
            {
                offset_coords(&x1, &y1);
                offset_coords(&x2, &y2);
                rb->lcd_drawline(x1, y1,
                                 x2, y2);
            }
            else
                draw_antialiased_line(rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT, x1, y1, x2, y2);

#ifdef DEBUG_MENU
            if(debug_settings.polyanim)
            {
                rb->lcd_update();
                rb->sleep(HZ/4);
            }
#endif
        }

        int x1, y1, x2, y2;
        x1 = coords[0];
        y1 = coords[1];
        x2 = coords[2 * (npoints - 1)];
        y2 = coords[2 * (npoints - 1) + 1];
        if(debug_settings.no_aa)
        {
            offset_coords(&x1, &y1);
            offset_coords(&x2, &y2);

            rb->lcd_drawline(x1, y1,
                             x2, y2);
        }
        else
            draw_antialiased_line(rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT, x1, y1, x2, y2);
    }
    else
    {
        LOGF("rb_draw_poly");

        if(fillcolor >= 0)
        {
            rb_color(fillcolor);

            /* serious hack: draw a bunch of triangles between adjacent points */
            /* this generally works, even with some concave polygons */
            for(int i = 2; i < npoints; ++i)
            {
                int x1, y1, x2, y2, x3, y3;
                x1 = coords[0];
                y1 = coords[1];
                x2 = coords[(i - 1) * 2];
                y2 = coords[(i - 1) * 2 + 1];
                x3 = coords[i * 2];
                y3 = coords[i * 2 + 1];
                zoom_filltriangle(x1, y1,
                                  x2, y2,
                                  x3, y3);
            }
        }

        /* draw outlines last so they're not covered by the fill */
        assert(outlinecolor >= 0);
        rb_color(outlinecolor);

        for(int i = 1; i < npoints; ++i)
        {
            int x1, y1, x2, y2;
            x1 = coords[2 * (i - 1)];
            y1 = coords[2 * (i - 1) + 1];
            x2 = coords[2 * i];
            y2 = coords[2 * i + 1];
            draw_antialiased_line(zoom_fb, zoom_w, zoom_h, x1, y1, x2, y2);
        }

        int x1, y1, x2, y2;
        x1 = coords[0];
        y1 = coords[1];
        x2 = coords[2 * (npoints - 1)];
        y2 = coords[2 * (npoints - 1) + 1];
        draw_antialiased_line(zoom_fb, zoom_w, zoom_h, x1, y1, x2, y2);
    }
}

static void rb_draw_circle(void *handle, int cx, int cy, int radius,
                           int fillcolor, int outlinecolor)
{
    if(!zoom_enabled)
    {
        LOGF("rb_draw_circle(%d, %d, %d)", cx, cy, radius);
        offset_coords(&cx, &cy);

        if(fillcolor >= 0)
        {
            rb_color(fillcolor);
            xlcd_fillcircle(cx, cy, radius - 1);
        }

        assert(outlinecolor >= 0);
        rb_color(outlinecolor);
        xlcd_drawcircle(cx, cy, radius - 1);
    }
    else
    {
        if(fillcolor >= 0)
        {
            rb_color(fillcolor);
            zoom_fillcircle(cx, cy, radius - 1);
        }

        assert(outlinecolor >= 0);
        rb_color(outlinecolor);
        zoom_drawcircle(cx, cy, radius - 1);
    }
}

/* blitters allow the game code to save/restore a piece of the
 * framebuffer */
struct blitter {
    bool have_data;
    int x, y;
    struct bitmap bmp;
};

/* originally from emcc.c */
static void trim_rect(int *x, int *y, int *w, int *h)
{
    int x0, x1, y0, y1;

    /*
     * Reduce the size of the copied rectangle to stop it going
     * outside the bounds of the canvas.
     */

    /* Transform from x,y,w,h form into coordinates of all edges */
    x0 = *x;
    y0 = *y;
    x1 = *x + *w;
    y1 = *y + *h;

    int screenw = zoom_enabled ? zoom_w : LCD_WIDTH;
    int screenh = zoom_enabled ? zoom_h : LCD_HEIGHT;

    /* Clip each coordinate at both extremes of the canvas */
    x0 = (x0 < 0 ? 0 : x0 > screenw ? screenw: x0);
    x1 = (x1 < 0 ? 0 : x1 > screenw ? screenw: x1);
    y0 = (y0 < 0 ? 0 : y0 > screenh ? screenh: y0);
    y1 = (y1 < 0 ? 0 : y1 > screenh ? screenh: y1);

    /* Transform back into x,y,w,h to return */
    *x = x0;
    *y = y0;
    *w = x1 - x0;
    *h = y1 - y0;
}

static blitter *rb_blitter_new(void *handle, int w, int h)
{
    LOGF("rb_blitter_new");
    blitter *b = snew(blitter);
    b->bmp.width = w;
    b->bmp.height = h;
    b->bmp.data = smalloc(w * h * sizeof(fb_data));
    b->have_data = false;
    return b;
}

static void rb_blitter_free(void *handle, blitter *bl)
{
    LOGF("rb_blitter_free");
    sfree(bl->bmp.data);
    sfree(bl);
    return;
}

/* copy a section of the framebuffer */
static void rb_blitter_save(void *handle, blitter *bl, int x, int y)
{
    /* no viewport offset */
#if defined(LCD_STRIDEFORMAT) && (LCD_STRIDEFORMAT == VERTICAL_STRIDE)
#error no vertical stride
#else
    if(bl && bl->bmp.data)
    {
        int w = bl->bmp.width, h = bl->bmp.height;
        int screen_w = zoom_enabled ? zoom_w : LCD_WIDTH;

        trim_rect(&x, &y, &w, &h);

        fb_data *fb = zoom_enabled ? zoom_fb : rb->lcd_framebuffer;
        LOGF("rb_blitter_save(%d, %d, %d, %d)", x, y, w, h);
        for(int i = 0; i < h; ++i)
        {
            /* copy line-by-line */
            rb->memcpy(bl->bmp.data + sizeof(fb_data) * i * w,
                       fb + (y + i) * screen_w + x,
                       w * sizeof(fb_data));
        }
        bl->x = x;
        bl->y = y;
        bl->have_data = true;
    }
#endif
}

static void rb_blitter_load(void *handle, blitter *bl, int x, int y)
{
    LOGF("rb_blitter_load");
    if(!bl->have_data)
        return;
    int w = bl->bmp.width, h = bl->bmp.height;

    if(x == BLITTER_FROMSAVED) x = bl->x;
    if(y == BLITTER_FROMSAVED) y = bl->y;

    if(!zoom_enabled)
        offset_coords(&x, &y);

    trim_rect(&x, &y, &w, &h);

    if(!zoom_enabled)
    {
        rb->lcd_bitmap((fb_data*)bl->bmp.data, x, y, w, h);
    }
    else
    {
        for(int i = 0; i < h; ++i)
        {
            rb->memcpy(zoom_fb + (y + i) * zoom_w + x,
                       bl->bmp.data + sizeof(fb_data) * i * w,
                       w * sizeof(fb_data));
        }
    }
}

static void rb_draw_update(void *handle, int x, int y, int w, int h)
{
    LOGF("rb_draw_update(%d, %d, %d, %d)", x, y, w, h);

    /*
     * It seems that the puzzles use a different definition of
     * "updating" the display than Rockbox does; by calling this
     * function, it tells us that it has either already drawn to the
     * updated area (as rockbox assumes), or that it WILL draw to the
     * said area in the future (in which case we will draw
     * nothing). Because we don't know which of these is the case, we
     * simply remember a rectangle that contains all the updated
     * regions and update it at the very end.
     */

    /* adapted from gtk.c */
    if (!need_draw_update || ud_l > x  ) ud_l = x;
    if (!need_draw_update || ud_r < x+w) ud_r = x+w;
    if (!need_draw_update || ud_u > y  ) ud_u = y;
    if (!need_draw_update || ud_d < y+h) ud_d = y+h;

    need_draw_update = true;
}

static void rb_start_draw(void *handle)
{
    (void) handle;

    /* ... mumble mumble ... not ... reentrant ... mumble mumble ... */

    need_draw_update = false;
    ud_l = 0;
    ud_r = LCD_WIDTH;
    ud_u = 0;
    ud_d = LCD_HEIGHT;
}

static void rb_end_draw(void *handle)
{
    (void) handle;
    /* we ignore the backend's redraw requests and just
     * unconditionally update everything */
#if 0
    if(!zoom_enabled)
    {
        LOGF("rb_end_draw");

        if(need_draw_update)
            rb->lcd_update_rect(MAX(0, ud_l), MAX(0, ud_u), MIN(LCD_WIDTH, ud_r - ud_l), MIN(LCD_HEIGHT, ud_d - ud_u));
    }
    else
    {
        /* stubbed */
    }
#endif
}

static void rb_status_bar(void *handle, const char *text)
{
    if(titlebar)
        sfree(titlebar);
    titlebar = dupstr(text);
    LOGF("game title is %s\n", text);
}

static void draw_title(bool clear_first)
{
    const char *base;
    if(titlebar)
        base = titlebar;
    else
        base = midend_which_game(me)->name;

    char str[128];
    rb->snprintf(str, sizeof(str), "%s%s", base, zoom_enabled ? (view_mode ? " (viewing)" : " (interaction)") : "");

    /* quick hack */
    bool orig_clipped = false;
    if(!zoom_enabled)
    {
        orig_clipped = clipped;
        if(orig_clipped)
            rb_unclip(NULL);
    }

    int w, h;
    rb->lcd_setfont(cur_font = FONT_UI);
    rb->lcd_getstringsize(str, &w, &h);

    rb->lcd_set_foreground(BG_COLOR);
    rb->lcd_fillrect(0, LCD_HEIGHT - h, clear_first ? LCD_WIDTH : w, h);

    rb->lcd_set_drawmode(DRMODE_FG);
    rb->lcd_set_foreground(LCD_BLACK);
    rb->lcd_putsxy(0, LCD_HEIGHT - h, str);

    if(!zoom_enabled)
    {
        if(orig_clipped)
            rb_clip(NULL, clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height);
    }
}

#define MOUSE_W BMPWIDTH_puzzles_cursor
#define MOUSE_H BMPHEIGHT_puzzles_cursor

static blitter *mouse_bl = NULL;

static void clear_mouse(void)
{
    bool orig_clipped = clipped;
    if(!zoom_enabled)
    {
        if(orig_clipped)
            rb_unclip(NULL);
    }

    if(mouse_bl)
        rb_blitter_load(NULL, mouse_bl, BLITTER_FROMSAVED, BLITTER_FROMSAVED);

    if(!zoom_enabled)
    {
        if(orig_clipped)
            rb_clip(NULL, clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height);
    }
}

static void draw_mouse(void)
{
    bool orig_clipped = clipped;
    if(!zoom_enabled)
    {
        if(orig_clipped)
            rb_unclip(NULL);
    }

    if(!mouse_bl)
        mouse_bl = rb_blitter_new(NULL, MOUSE_W, MOUSE_H);

    /* save area being covered (will be restored elsewhere) */
    rb_blitter_save(NULL, mouse_bl, mouse_x, mouse_y);

    rb->lcd_bitmap_transparent(puzzles_cursor, mouse_x, mouse_y, BMPWIDTH_puzzles_cursor, BMPHEIGHT_puzzles_cursor);

    if(!zoom_enabled)
    {
        if(orig_clipped)
            rb_clip(NULL, clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height);
    }
}

/* doesn't work, disabled (can't find a good mechanism to check if a
 * glyph exists in a font) */
#if 0
/* See: https://www.chiark.greenend.org.uk/~sgtatham/puzzles/devel/drawing.html#drawing-text-fallback */
static char *rb_text_fallback(void *handle, const char *const *strings,
                              int nstrings)
{
    struct font *pf = rb->font_get(cur_font);

    for(int i = 0; i < nstrings; i++)
    {
        LOGF("checking alternative \"%s\"", strings[i]);
        const unsigned char *ptr = strings[i];
        unsigned short code;
        bool valid = true;

        while(*ptr)
        {
            ptr = rb->utf8decode(ptr, &code);

            if(!rb->font_get_bits(pf, code))
            {
                valid = false;
                break;
            }
        }

        if(valid)
            return dupstr(strings[i]);
    }

    /* shouldn't get here */
    return dupstr(strings[nstrings - 1]);
}
#endif

const drawing_api rb_drawing = {
    rb_draw_text,
    rb_draw_rect,
    rb_draw_line,
    rb_draw_poly,
    rb_draw_circle,
    rb_draw_update,
    rb_clip,
    rb_unclip,
    rb_start_draw,
    rb_end_draw,
    rb_status_bar,
    rb_blitter_new,
    rb_blitter_free,
    rb_blitter_save,
    rb_blitter_load,
    /* printing functions */
    NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
    NULL, NULL,                         /* line_width, line_dotted */
    NULL, /* fall back to ASCII */
    NULL,
};

/** utility functions exported to puzzles code **/

void fatal(const char *fmt, ...)
{
    va_list ap;
    char buf[256];

    rb->splash(HZ, "FATAL");

    va_start(ap, fmt);
    rb->vsnprintf(buf, sizeof(buf), fmt, ap);
    va_end(ap);

    LOGF("%s", buf);
    rb->splash(HZ * 2, buf);

    if(rb->thread_self() == thread)
        rb->thread_exit();
    else
        exit(PLUGIN_ERROR);
}

void get_random_seed(void **randseed, int *randseedsize)
{
    *randseed = snew(long);
    long seed = *rb->current_tick;
    rb->memcpy(*randseed, &seed, sizeof(seed));
    *randseedsize = sizeof(seed);
}

static void timer_cb(void)
{
#if LCD_DEPTH < 24
    if(debug_settings.timerflash)
    {
        static bool what = false;
        what = !what;
        if(what)
            rb->lcd_framebuffer[0] = LCD_BLACK;
        else
            rb->lcd_framebuffer[0] = LCD_WHITE;
        rb->lcd_update();
    }
#endif

    LOGF("timer callback");
    midend_timer(me, ((float)(*rb->current_tick - last_tstamp) / (float)HZ) / debug_settings.slowmo_factor);
    last_tstamp = *rb->current_tick;
}

void activate_timer(frontend *fe)
{
    last_tstamp = *rb->current_tick;
    timer_on = true;
}

void deactivate_timer(frontend *fe)
{
    timer_on = false;
}

void frontend_default_color(frontend *fe, float *out)
{
    *out++ = BG_R;
    *out++ = BG_G;
    *out++ = BG_B;
}

/** frontend code -- mostly UI stuff **/

static void send_click(int button, bool release)
{
    int x = (zoom_enabled ? zoom_x : 0) + mouse_x,
        y = (zoom_enabled ? zoom_y : 0) + mouse_y;
    assert(LEFT_BUTTON + 6 == LEFT_RELEASE);

    midend_process_key(me, x, y, button);

    if(release)
        midend_process_key(me, x, y, button + 6);
}

static int choose_key(void)
{
    int options = 0;

    key_label *game_keys = midend_request_keys(me, &options);

    if(!game_keys || !options)
        return 0;

    int sel = 0;

    while(1)
    {
        if(timer_on)
            timer_cb();
        midend_process_key(me, 0, 0, game_keys[sel].button);
        midend_force_redraw(me);

        if(zoom_enabled)
            rb->lcd_bitmap_part(zoom_fb, zoom_x, zoom_y, STRIDE(SCREEN_MAIN, zoom_w, zoom_h),
                                0, 0, LCD_WIDTH, LCD_HEIGHT);

        rb->lcd_update();
        rb->yield();

        int button = rb->button_get_w_tmo(timer_on ? TIMER_INTERVAL : -1);
        switch(button)
        {
        case BTN_LEFT:
            if(--sel < 0)
                sel = options - 1;
            break;
        case BTN_RIGHT:
            if(++sel >= options)
                sel = 0;
            break;
        case BTN_PAUSE:
            return -1;
        case BTN_FIRE:
            free_keys(game_keys, options);

            /* the key has already been sent to the game, just return */
            return 0;
        }
    }
}

/* This function handles most user input. It has specific workarounds
 * and fixes for certain games to allow them to work well on
 * Rockbox. It will either return a positive value that can be passed
 * to the midend, or a negative flag value. `do_pausemenu' sets how
 * this function will handle BUTTON_PAUSE: if true, it will handle it
 * all, otherwise it will simply return -1 and let the caller do the
 * work (this is used for zoom mode). */
static int process_input(int tmo, bool do_pausemenu)
{
    LOGF("process_input start");
    LOGF("-------------------");
    int state = 0;

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(false); /* about to block for button input */
#endif

    int button = rb->button_get_w_tmo(tmo);

    /* weird stuff */
    exit_on_usb(button);

    /* See if the button is a long-press. */
    if(accept_input && (button == (BTN_FIRE | BUTTON_REPEAT)))
    {
        LOGF("button is long-press, ignoring subsequent input until release");
        /* Ignore repeated long presses. */
        accept_input = false;

        if(mouse_mode && input_settings.rclick_on_hold)
        {
            /* simulate right-click */
            LOGF("sending right click");
            send_click(RIGHT_BUTTON, true);
            return 0;
        }

        /* These games want a spacebar in the event of a long press. */
        if(!mouse_mode && input_settings.want_spacebar)
            return ' ';
    }

    button = rb->button_status();

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(true);
#endif

    if(button == BTN_PAUSE)
    {
        if(do_pausemenu)
        {
            /* quick hack to preserve the clipping state */
            bool orig_clipped = clipped;
            if(orig_clipped)
                rb_unclip(NULL);

            int rc = pause_menu();

            if(orig_clipped)
                rb_clip(NULL, clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height);

            last_keystate = 0;
            accept_input = true;

            return rc;
        }
        else
            return -1;
    }

    /* Mouse movement (if enabled). This goes here since none of the
     * following code is needed for mouse mode. */
    if(mouse_mode)
    {
        static int last_mousedir = 0, held_count = 0, v = 1;

        if(button & BTN_UP)
            state = CURSOR_UP;
        else if(button & BTN_DOWN)
            state = CURSOR_DOWN;
        else if(button & BTN_LEFT)
            state = CURSOR_LEFT;
        else if(button & BTN_RIGHT)
            state = CURSOR_RIGHT;

        unsigned released = ~button & last_keystate,
            pressed = button & ~last_keystate;

        last_keystate = button;

        /* move */
        /* get the direction vector the cursor is moving in. */
        int new_x = mouse_x, new_y = mouse_y;

        /* in src/misc.c */
        move_cursor(state, &new_x, &new_y, LCD_WIDTH, LCD_HEIGHT, false);

        int dx = new_x - mouse_x, dy = new_y - mouse_y;

        mouse_x += dx * v;
        mouse_y += dy * v;

        /* clamp */
        /* The % operator with negative operands is messy; this is much
         * simpler. */
        if(mouse_x < 0)
            mouse_x = 0;
        if(mouse_y < 0)
            mouse_y = 0;

        if(mouse_x >= LCD_WIDTH)
            mouse_x = LCD_WIDTH - 1;
        if(mouse_y >= LCD_HEIGHT)
            mouse_y = LCD_HEIGHT - 1;

        /* clicking/dragging */
        /* rclick on hold requires that we fire left-click on a
         * release, otherwise it's impossible to distinguish the
         * two. */
        if(input_settings.rclick_on_hold)
        {
            if(accept_input && released == BTN_FIRE)
            {
                LOGF("sending left click");
                send_click(LEFT_BUTTON, true); /* right-click is handled earlier */
            }
        }
        else
        {
            if(pressed & BTN_FIRE)
                send_click(LEFT_BUTTON, false);
            else if(released & BTN_FIRE)
                send_click(LEFT_RELEASE, false);
            else if(button & BTN_FIRE)
                send_click(LEFT_DRAG, false);
        }

        /* acceleration */
        if(state && state == last_mousedir)
        {
            if(++held_count % 5 == 0 && v < 15)
                v++;
        }
        else
        {
            if(!button)
            {
                LOGF("all keys released, accepting further input");
                accept_input = true;
            }
            last_mousedir = state;
            v = 1;
            held_count = 0;
        }

        /* no buttons are sent to the midend in mouse mode */
        return 0;
    }

    /* These games require, for one reason or another, that events
     * fire upon buttons being released rather than when they are
     * pressed. For Inertia, it is because it needs to be able to
     * sense multiple simultaneous keypresses (to move diagonally),
     * and the others require a long press to map to a secondary
     * "action" key. */
    if(input_settings.falling_edge)
    {
        LOGF("received button 0x%08x", button);

        unsigned released = ~button & last_keystate;

        last_keystate = button;

        if(!button)
        {
            if(!accept_input)
            {
                LOGF("ignoring, all keys released but not accepting input before, can accept input later");
                accept_input = true;
                return 0;
            }
        }

        if(!released || !accept_input)
        {
            LOGF("released keys detected: 0x%08x", released);
            LOGF("ignoring, either no keys released or not accepting input");
            return 0;
        }

        if(button)
        {
            LOGF("ignoring input from now until all released");
            accept_input = false;
        }

        button |= released;
        LOGF("accepting event 0x%08x", button);
    }
    /* Ignore repeats in all games which are not Untangle. */
    else if(input_settings.ignore_repeats)
    {
        /* start accepting input again after a release */
        if(!button)
        {
            accept_input = true;
            return 0;
        }

        /* ignore repeats (in mouse mode, only ignore repeats of BTN_FIRE) */
        if(!accept_input)
            return 0;

        accept_input = false;
    }

    switch(button)
    {
    case BTN_UP:
        state = CURSOR_UP;
        break;
    case BTN_DOWN:
        state = CURSOR_DOWN;
        break;
    case BTN_LEFT:
        state = CURSOR_LEFT;
        break;
    case BTN_RIGHT:
        state = CURSOR_RIGHT;
        break;

        /* handle diagonals (mainly for Inertia) */
    case BTN_DOWN | BTN_LEFT:
#ifdef BTN_DOWN_LEFT
    case BTN_DOWN_LEFT:
#endif
        state = '1' | MOD_NUM_KEYPAD;
        break;
    case BTN_DOWN | BTN_RIGHT:
#ifdef BTN_DOWN_RIGHT
    case BTN_DOWN_RIGHT:
#endif
        state = '3' | MOD_NUM_KEYPAD;
        break;
    case BTN_UP | BTN_LEFT:
#ifdef BTN_UP_LEFT
    case BTN_UP_LEFT:
#endif
        state = '7' | MOD_NUM_KEYPAD;
        break;
    case BTN_UP | BTN_RIGHT:
#ifdef BTN_UP_RIGHT
    case BTN_UP_RIGHT:
#endif
        state = '9' | MOD_NUM_KEYPAD;
        break;

    case BTN_FIRE:
        if(input_settings.numerical_chooser)
        {
            if(choose_key() < 0)
            {
                if(do_pausemenu)
                {
                    /* quick hack to preserve the clipping state */
                    bool orig_clipped = clipped;
                    if(orig_clipped)
                        rb_unclip(NULL);

                    int rc = pause_menu();

                    if(orig_clipped)
                        rb_clip(NULL, clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height);

                    last_keystate = 0;
                    accept_input = true;

                    return rc;
                }
                else
                    return -1;
            }
        }
        else
        {
            if(!strcmp("Fifteen", midend_which_game(me)->name))
                state = 'h'; /* hint */
            else
                state = CURSOR_SELECT;
        }
        break;

    default:
        break;
    }

    if(debug_settings.shortcuts)
    {
        static bool shortcuts_ok = true;
        switch(button)
        {
        case BTN_LEFT | BTN_FIRE:
            if(shortcuts_ok)
                midend_process_key(me, 0, 0, 'u');
            shortcuts_ok = false;
            break;
        case BTN_RIGHT | BTN_FIRE:
            if(shortcuts_ok)
                midend_process_key(me, 0, 0, 'r');
            shortcuts_ok = false;
            break;
        case 0:
            shortcuts_ok = true;
            break;
        default:
            break;
        }
    }

    LOGF("process_input done");
    LOGF("------------------");
    return state;
}

/* This function handles zoom mode, where the user can either pan
 * around a zoomed-in image or play a zoomed-in version of the game. */
static void zoom(void)
{
    rb->splash(0, "Please wait...");
    zoom_w = LCD_WIDTH * ZOOM_FACTOR, zoom_h = LCD_HEIGHT * ZOOM_FACTOR;

    zoom_clipu = 0;
    zoom_clipd = zoom_h;
    zoom_clipl = 0;
    zoom_clipr = zoom_w;

    midend_size(me, &zoom_w, &zoom_h, true);

    /* Allocating the framebuffer will mostly likely grab the
     * audiobuffer, which will make impossible to load new fonts, and
     * lead to zoomed puzzles being drawn with the default fallback
     * fonts. As a semi-workaround, we go ahead and load the biggest available
     * monospace and proportional fonts. */
    rb_setfont(FONT_FIXED, BUNDLE_MAX);
    rb_setfont(FONT_VARIABLE, BUNDLE_MAX);

    zoom_fb = smalloc(zoom_w * zoom_h * sizeof(fb_data));
    if(!zoom_fb)
    {
        rb->splash(HZ, "Out of memory. Cannot zoom in.");
        return;
    }

    zoom_enabled = true;

    /* draws go to the zoom framebuffer */
    midend_force_redraw(me);

    zoom_x = zoom_y = 0;

    rb->lcd_bitmap_part(zoom_fb, zoom_x, zoom_y, STRIDE(SCREEN_MAIN, zoom_w, zoom_h),
                        0, 0, LCD_WIDTH, LCD_HEIGHT);

    draw_title(false); /* false since we don't want to use more screen space than we need. */

    rb->lcd_update();

    /* Here's how this works: pressing select (or the target's
     * equivalent, it's whatever BTN_FIRE is) while in viewing mode
     * will toggle the mode to interaction mode. In interaction mode,
     * the buttons will behave as normal and be sent to the puzzle,
     * except for the pause/quit (BTN_PAUSE) button, which will return
     * to view mode. Finally, when in view mode, pause/quit will
     * return to the pause menu. */

    view_mode = true;

    /* pan around the image */
    while(1)
    {
        if(view_mode)
        {
            int button = rb->button_get_w_tmo(timer_on ? TIMER_INTERVAL : -1);
            switch(button)
            {
            case BTN_UP:
                zoom_y -= PAN_Y; /* clamped later */
                break;
            case BTN_DOWN:
                zoom_y += PAN_Y; /* clamped later */
                break;
            case BTN_LEFT:
                zoom_x -= PAN_X; /* clamped later */
                break;
            case BTN_RIGHT:
                zoom_x += PAN_X; /* clamped later */
                break;
            case BTN_PAUSE:
                zoom_enabled = false;
                sfree(zoom_fb);
                fix_size();
                return;
            case BTN_FIRE:
                view_mode = false;
                continue;
            default:
                break;
            }

            if(zoom_y < 0)
                zoom_y = 0;
            if(zoom_x < 0)
                zoom_x = 0;

            if(zoom_y + LCD_HEIGHT >= zoom_h)
                zoom_y = zoom_h - LCD_HEIGHT;
            if(zoom_x + LCD_WIDTH >= zoom_w)
                zoom_x = zoom_w - LCD_WIDTH;

            if(timer_on)
                timer_cb();

            /* goes to zoom_fb */
            midend_redraw(me);

            rb->lcd_bitmap_part(zoom_fb, zoom_x, zoom_y, STRIDE(SCREEN_MAIN, zoom_w, zoom_h),
                                0, 0, LCD_WIDTH, LCD_HEIGHT);
            draw_title(false);
            rb->lcd_update();
            rb->yield();
        }
        else
        {
            /* basically a copy-pasta'd main loop */
            int button = process_input(timer_on ? TIMER_INTERVAL : -1, false);

            if(button < 0)
            {
                view_mode = true;
                continue;
            }

            if(button)
                midend_process_key(me, 0, 0, button);

            if(timer_on)
                timer_cb();

            midend_redraw(me);

            /* blit */
            rb->lcd_bitmap_part(zoom_fb, zoom_x, zoom_y, STRIDE(SCREEN_MAIN, zoom_w, zoom_h),
                                0, 0, LCD_WIDTH, LCD_HEIGHT);

            draw_title(false);

            /* The cursor is always in screenspace coordinates; when
             * zoomed, this means the mouse is always restricted to
             * the bounds of the physical display, not the virtual
             * zoom framebuffer. */
            if(mouse_mode)
                draw_mouse();

            rb->lcd_update();

            if(mouse_mode)
                clear_mouse();

            rb->yield();
        }
    }
}

/** settings/preset code */

static const char *config_choices_formatter(int sel, void *data, char *buf, size_t len)
{
    /* we can't rely on being called in any particular order */
    char *list = dupstr(data);
    char delimbuf[2] = { *list, 0 };
    char *save = NULL;
    char *str = rb->strtok_r(list, delimbuf, &save);
    for(int i = 0; i < sel; ++i)
        str = rb->strtok_r(NULL, delimbuf, &save);
    rb->snprintf(buf, len, "%s", str);
    sfree(list);
    return buf;
}

static int list_choose(const char *list_str, const char *title, int sel)
{
    char delim = *list_str;

    const char *ptr = list_str + 1;
    int n = 0;
    while(ptr)
    {
        n++;
        ptr = strchr(ptr + 1, delim);
    }

    struct gui_synclist list;

    rb->gui_synclist_init(&list, &config_choices_formatter, (void*)list_str, false, 1, NULL);
    rb->gui_synclist_set_icon_callback(&list, NULL);
    rb->gui_synclist_set_nb_items(&list, n);
    rb->gui_synclist_limit_scroll(&list, false);

    rb->gui_synclist_select_item(&list, sel);

    rb->gui_synclist_set_title(&list, (char*)title, NOICON);
    while (1)
    {
        rb->gui_synclist_draw(&list);
        int button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
        if(rb->gui_synclist_do_button(&list, &button, LIST_WRAP_ON))
            continue;
        switch(button)
        {
        case ACTION_STD_OK:
            return rb->gui_synclist_get_sel_pos(&list);
        case ACTION_STD_PREV:
        case ACTION_STD_CANCEL:
            return -1;
        default:
            break;
        }
    }
}

static bool is_integer(const char *str)
{
    while(*str)
    {
        char c = *str++;
        if(!isdigit(c) && c != '-')
            return false;
    }
    return true;
}

static void int_chooser(config_item *cfgs, int idx, int val)
{
    config_item *cfg = cfgs + idx;
    int old_val = val;

    rb->snprintf(cfg->u.string.sval, MAX_STRLEN, "%d", val);

    rb->lcd_clear_display();

    while(1)
    {
        rb->lcd_set_foreground(LCD_WHITE);
        rb->lcd_puts(0, 0, cfg->name);
        rb->lcd_putsf(0, 1, "< %d >", val);
        rb->lcd_update();
        rb->lcd_set_foreground(ERROR_COLOR);

        int d = 0;
        int button = rb->button_get(true);
        switch(button)
        {
        case BTN_RIGHT:
        case BTN_RIGHT | BUTTON_REPEAT:
            d = 1;
            break;
        case BTN_LEFT:
        case BTN_LEFT | BUTTON_REPEAT:
            d = -1;
            break;
        case BTN_FIRE:
            /* config is already set */
            rb->lcd_scroll_stop();
            return;
        case BTN_PAUSE:
            if(val != old_val)
                rb->splash(HZ, "Canceled.");
            val = old_val;
            rb->snprintf(cfg->u.string.sval, MAX_STRLEN, "%d", val);
            rb->lcd_scroll_stop();
            return;
        }
        if(d)
        {

            const char *ret;
            for(int i = 0; i < CHOOSER_MAX_INCR; ++i)
            {
                val += d;
                rb->snprintf(cfg->u.string.sval, MAX_STRLEN, "%d", val);
                ret = midend_set_config(me, CFG_SETTINGS, cfgs);
                if(!ret)
                {
                    /* clear any error message */
                    rb->lcd_clear_display();
                    rb->lcd_scroll_stop();
                    break;
                }

            }

            /* failure */
            if(ret)
            {
                /* bright, annoying red */
                rb->lcd_set_foreground(ERROR_COLOR);
                rb->lcd_puts_scroll(0, 2, ret);

                /* reset value */
                val -= d * CHOOSER_MAX_INCR;
                rb->snprintf(cfg->u.string.sval, MAX_STRLEN, "%d", val);
                assert(!midend_set_config(me, CFG_SETTINGS, cfgs));
            }
        }
    }
}

/* return value is only meaningful when type == C_STRING, where it
 * indicates whether cfg->sval has been freed or otherwise altered */
static bool do_configure_item(config_item *cfgs, int idx)
{
    config_item *cfg = cfgs + idx;
    switch(cfg->type)
    {
    case C_STRING:
    {
        char *newstr = smalloc(MAX_STRLEN);

        rb->lcd_set_foreground(LCD_WHITE);
        rb->lcd_set_background(LCD_BLACK);

        if(is_integer(cfg->u.string.sval))
        {
            int val = atoi(cfg->u.string.sval);

            /* we now free the original string and give int_chooser()
             * a clean buffer to work with */
            sfree(cfg->u.string.sval);
            cfg->u.string.sval = newstr;

            int_chooser(cfgs, idx, val);

            rb->lcd_set_foreground(LCD_WHITE);
            rb->lcd_set_background(LCD_BLACK);

            return true;
        }

        rb->strlcpy(newstr, cfg->u.string.sval, MAX_STRLEN);
        if(rb->kbd_input(newstr, MAX_STRLEN) < 0)
        {
            sfree(newstr);
            return false;
        }
        sfree(cfg->u.string.sval);
        cfg->u.string.sval = newstr;
        return true;
    }
    case C_BOOLEAN:
    {
        bool res = cfg->u.boolean.bval != 0;
        rb->set_bool(cfg->name, &res);

        /* seems to reset backdrop */
        rb->lcd_set_backdrop(NULL);

        cfg->u.boolean.bval = res;
        break;
    }
    case C_CHOICES:
    {
        int sel = list_choose(cfg->u.choices.choicenames, cfg->name, cfg->u.choices.selected);
        if(sel >= 0)
            cfg->u.choices.selected = sel;
        break;
    }
    default:
        fatal("");
        break;
    }
    return false;
}

const char *config_formatter(int sel, void *data, char *buf, size_t len)
{
    config_item *cfg = data;
    cfg += sel;
    rb->snprintf(buf, len, "%s", cfg->name);
    return buf;
}

static bool config_menu(void)
{
    char *title;
    config_item *config = midend_get_config(me, CFG_SETTINGS, &title);

    rb->lcd_setfont(cur_font = FONT_UI);

    bool success = false;

    if(!config)
    {
        goto done;
    }

    /* count */
    int n = 0;
    config_item *ptr = config;
    while(ptr->type != C_END)
    {
        n++;
        ptr++;
    }

    /* display a list */
    struct gui_synclist list;

    rb->gui_synclist_init(&list, &config_formatter, config, false, 1, NULL);
    rb->gui_synclist_set_icon_callback(&list, NULL);
    rb->gui_synclist_set_nb_items(&list, n);
    rb->gui_synclist_limit_scroll(&list, false);

    rb->gui_synclist_select_item(&list, 0);

    bool done = false;
    rb->gui_synclist_set_title(&list, title, NOICON);
    while (!done)
    {
        rb->gui_synclist_draw(&list);
        int button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
        if(rb->gui_synclist_do_button(&list, &button, LIST_WRAP_ON))
            continue;
        switch(button)
        {
        case ACTION_STD_OK:
        {
            int pos = rb->gui_synclist_get_sel_pos(&list);

            /* store the initial state */
            config_item old;
            memcpy(&old, config + pos, sizeof(old));

            char *old_str = NULL;
            if(old.type == C_STRING)
                old_str = dupstr(old.u.string.sval);

            bool freed_str = do_configure_item(config, pos);
            const char *err = midend_set_config(me, CFG_SETTINGS, config);

            if(err)
            {
                rb->splash(HZ, err);

                /* restore old state */
                memcpy(config + pos, &old, sizeof(old));

                if(old.type == C_STRING && freed_str)
                    config[pos].u.string.sval = old_str;
            }
            else
            {
                if(old.type == C_STRING)
                {
                    /* success, and we duplicated the old string when
                     * we didn't need to, so free it now */
                    sfree(old_str);
                }
                success = true;
            }
            break;
        }
        case ACTION_STD_PREV:
        case ACTION_STD_CANCEL:
            done = true;
            break;
        default:
            break;
        }
    }

done:
    sfree(title);
    free_cfg(config);
    return success;
}

static const char *preset_formatter(int sel, void *data, char *buf, size_t len)
{
    struct preset_menu *menu = data;
    rb->snprintf(buf, len, "%s", menu->entries[sel].title);
    return buf;
}

/* main worker function */
/* returns the index of the selected item on success, -1 on failure */
static int do_preset_menu(struct preset_menu *menu, char *title, int selected)
{
    if(!menu->n_entries)
        return false;

    /* display a list */
    struct gui_synclist list;

    rb->gui_synclist_init(&list, &preset_formatter, menu, false, 1, NULL);
    rb->gui_synclist_set_icon_callback(&list, NULL);
    rb->gui_synclist_set_nb_items(&list, menu->n_entries);
    rb->gui_synclist_limit_scroll(&list, false);

    rb->gui_synclist_select_item(&list, selected);

    static char def[] = "Game Type";
    rb->gui_synclist_set_title(&list, title ? title : def, NOICON);
    while(1)
    {
        rb->gui_synclist_draw(&list);
        int button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
        if(rb->gui_synclist_do_button(&list, &button, LIST_WRAP_ON))
            continue;
        switch(button)
        {
        case ACTION_STD_OK:
        {
            int sel = rb->gui_synclist_get_sel_pos(&list);
            struct preset_menu_entry *entry = menu->entries + sel;
            if(entry->params)
            {
                midend_set_params(me, entry->params);
                return sel;
            }
            else
            {
                /* recurse */
                if(do_preset_menu(entry->submenu, entry->title, 0)) /* select first one */
                    return sel;
            }
            break;
        }
        case ACTION_STD_PREV:
        case ACTION_STD_CANCEL:
            return -1;
        default:
            break;
        }
    }
}

/* Let user choose from game presets. Returns true if the user chooses
 * one (in which case the caller should start a new game. */
static bool presets_menu(void)
{
    /* figure out the index of the current preset
     * if it's in a submenu, give up and default to the first item */
    struct preset_menu *top = midend_get_presets(me, NULL);
    int sel = 0;
    for(int i = 0; i < top->n_entries; ++i)
    {
        if(top->entries[i].id == midend_which_preset(me))
        {
            sel = i;
            break;
        }
    }

    return do_preset_menu(midend_get_presets(me, NULL), NULL, sel) >= 0;
}

static void quick_help(void)
{
#if defined(FOR_REAL) && defined(DEBUG_MENU)
    if(++help_times >= 5)
    {
        rb->splash(HZ, "You are now a developer!");
        debug_mode = true;
    }
#endif

    rb->splash(0, quick_help_text);
    rb->button_get(true);
    return;
}

static void full_help(const char *name)
{
    unsigned old_bg = rb->lcd_get_background();

    bool orig_clipped = clipped;
    if(orig_clipped)
        rb_unclip(NULL);

    rb->lcd_set_foreground(LCD_WHITE);
    rb->lcd_set_background(LCD_BLACK);
    unload_fonts();
    rb->lcd_setfont(cur_font = FONT_UI);

    /* The help text is stored in compressed format in the help_text[]
     * array. display_text wants an array of pointers to
     * null-terminated words, so we create that here. */
    char *buf = smalloc(help_text_len);
    LZ4_decompress_tiny(help_text, buf, help_text_len);

    /* create the word_ptrs array to pass to display_text */
    char **word_ptrs = smalloc(sizeof(char*) * help_text_words);
    char **ptr = word_ptrs;
    bool last_was_null = false;

    *ptr++ = buf;

    for(int i = 1; i < help_text_len; ++i)
    {
        switch(buf[i])
        {
        case '\0':
            if(last_was_null)
            {
                /* newline */
                *ptr++ = buf + i;
            }
            else
                last_was_null = true;
            break;
        default:
            if(last_was_null)
                *ptr++ = buf + i;
            last_was_null = false;
            break;
        }
    }

    display_text(help_text_words, word_ptrs, help_text_style, NULL, true);

    sfree(buf);
    sfree(word_ptrs);

    rb->lcd_set_background(old_bg);

    if(orig_clipped)
        rb_clip(NULL, clip_rect.x, clip_rect.y, clip_rect.width, clip_rect.height);
}

static void init_default_settings(void)
{
    debug_settings.slowmo_factor = 1;
    debug_settings.timerflash = false;
    debug_settings.clipoff = false;
    debug_settings.shortcuts = false;
    debug_settings.no_aa = false;
    debug_settings.polyanim = false;
}

#ifdef DEBUG_MENU
/* Useless debug code. Mostly a waste of space. */
static void bench_aa(void)
{
    rb->sleep(0);
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(true);
#endif
    int next = *rb->current_tick + HZ;
    int i = 0;
    while(*rb->current_tick < next)
    {
        draw_antialiased_line(rb->lcd_framebuffer, LCD_WIDTH, LCD_HEIGHT, 0, 0, 20, 31);
        ++i;
    }
    rb->splashf(HZ, "%d AA lines/sec", i);
    next = *rb->current_tick + HZ;
    int j = 0;
    while(*rb->current_tick < next)
    {
        rb->lcd_drawline(0, 0, 20, 31);
        ++j;
    }
    rb->splashf(HZ, "%d normal lines/sec", j);
    rb->splashf(HZ, "Efficiency: %d%%", 100 * i / j);
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(false);
#endif
}

static void debug_menu(void)
{
    MENUITEM_STRINGLIST(menu, "Debug Menu", NULL,
                        "Slowmo factor",
                        "Randomize colors",
                        "Toggle flash pixel on timer",
                        "Toggle clip",
                        "Toggle shortcuts",
                        "Toggle antialias",
                        "Benchmark antialias",
                        "Toggle show poly steps",
                        "Toggle mouse mode",
                        "Toggle spacebar on long click",
                        "Toggle send keys on release",
                        "Toggle ignore repeats",
                        "Toggle right-click on hold vs. dragging",
                        "Back");
    bool quit = false;
    int sel = 0;
    while(!quit)
    {
        switch(rb->do_menu(&menu, &sel, NULL, false))
        {
        case 0:
            rb->set_int("Slowmo factor", "", UNIT_INT, &debug_settings.slowmo_factor, NULL, 1, 1, 15, NULL);
            break;
        case 1:
        {
            unsigned *ptr = colors;
            for(int i = 0; i < ncolors; ++i)
            {
                /* not seeded, who cares? */
                *ptr++ = LCD_RGBPACK(rb->rand()%255, rb->rand()%255, rb->rand()%255);
            }
            break;
        }
        case 2:
            debug_settings.timerflash = !debug_settings.timerflash;
            break;
        case 3:
            debug_settings.clipoff = !debug_settings.clipoff;
            break;
        case 4:
            debug_settings.shortcuts = !debug_settings.shortcuts;
            break;
        case 5:
            debug_settings.no_aa = !debug_settings.no_aa;
            break;
        case 6:
            bench_aa();
            break;
        case 7:
            debug_settings.polyanim = !debug_settings.polyanim;
            break;
        case 8:
            mouse_mode = !mouse_mode;
            break;
        case 9:
            input_settings.want_spacebar = !input_settings.want_spacebar;
            break;
        case 10:
            input_settings.falling_edge = !input_settings.falling_edge;
            break;
        case 11:
            input_settings.ignore_repeats = !input_settings.ignore_repeats;
            break;
        case 12:
            input_settings.rclick_on_hold = !input_settings.rclick_on_hold;
            break;
        default:
            quit = true;
            break;
        }
    }
}
#endif

static int pausemenu_cb(int action, const struct menu_item_ex *this_item)
{
    int i = (intptr_t) this_item;
    if(action == ACTION_REQUEST_MENUITEM)
    {
        switch(i)
        {
        case 3:
            if(!midend_can_undo(me))
                return ACTION_EXIT_MENUITEM;
            break;
        case 4:
            if(!midend_can_redo(me))
                return ACTION_EXIT_MENUITEM;
            break;
        case 5:
            if(!midend_which_game(me)->can_solve)
                return ACTION_EXIT_MENUITEM;
            break;
        case 9:
            if(audiobuf_available)
                break;
            else
                return ACTION_EXIT_MENUITEM;
        case 10:
            if(!midend_get_presets(me, NULL)->n_entries)
                return ACTION_EXIT_MENUITEM;
            break;
        case 11:
#if defined(FOR_REAL) && defined(DEBUG_MENU)
            if(debug_mode)
                break;
            return ACTION_EXIT_MENUITEM;
#else
            break;
#endif
        case 12:
            if(!midend_which_game(me)->can_configure)
                return ACTION_EXIT_MENUITEM;
            break;
        default:
            break;
        }
    }
    return action;
}

static void clear_and_draw(void)
{
    rb->lcd_clear_display();

    midend_force_redraw(me);

    draw_title(true);

    if(mouse_mode)
        draw_mouse();

    rb->lcd_update();

    if(mouse_mode)
        clear_mouse();
}

static void reset_drawing(void)
{
    rb->lcd_set_viewport(NULL);
    rb->lcd_set_backdrop(NULL);
    rb->lcd_set_foreground(LCD_BLACK);
    rb->lcd_set_background(BG_COLOR);
}

/* Make a new game, but tell the user through a splash so they don't
 * think we're locked up. Also performs new-game initialization
 * specific to Rockbox. */
static void new_game_notify(void)
{
    rb->splash(0, "Please wait...");
    midend_new_game(me);
    fix_size();
    rb->lcd_update();
}

static int pause_menu(void)
{
    MENUITEM_STRINGLIST(menu, menu_desc, pausemenu_cb,
                        "Resume Game",         // 0
                        "New Game",            // 1
                        "Restart Game",        // 2
                        "Undo",                // 3
                        "Redo",                // 4
                        "Solve",               // 5
                        "Zoom In",             // 6
                        "Quick Help",          // 7
                        "Extensive Help",      // 8
                        "Playback Control",    // 9
                        "Game Type",           // 10
                        "Debug Menu",          // 11
                        "Configure Game",      // 12
                        "Quit without Saving", // 13
                        "Quit");               // 14

#if defined(FOR_REAL) && defined(DEBUG_MENU)
    help_times = 0;
#endif

    bool quit = false;
    int sel = 0;
    while(!quit)
    {
        switch(rb->do_menu(&menu, &sel, NULL, false))
        {
        case 0:
            quit = true;
            break;
        case 1:
            new_game_notify();
            quit = true;
            break;
        case 2:
            midend_restart_game(me);
            fix_size();
            quit = true;
            break;
        case 3:
            if(!midend_can_undo(me))
                rb->splash(HZ, "Cannot undo.");
            else
                midend_process_key(me, 0, 0, 'u');
            quit = true;
            break;
        case 4:
            if(!midend_can_redo(me))
                rb->splash(HZ, "Cannot redo.");
            else
                midend_process_key(me, 0, 0, 'r');
            quit = true;
            break;
        case 5:
        {
            const char *msg = midend_solve(me);
            if(msg)
                rb->splash(HZ, msg);
            quit = true;
            break;
        }
        case 6:
            zoom();
            break;
        case 7:
            quick_help();
            break;
        case 8:
            full_help(midend_which_game(me)->name);
            break;
        case 9:
            playback_control(NULL);
            break;
        case 10:
            if(presets_menu())
            {
                new_game_notify();
                reset_drawing();
                clear_and_draw();
                quit = true;
            }
            break;
        case 11:
#ifdef DEBUG_MENU
            debug_menu();
#endif
            break;
        case 12:
            if(config_menu())
            {
                new_game_notify();
                reset_drawing();
                clear_and_draw();
                quit = true;
            }
            break;
        case 13:
            return -2;
        case 14:
            return -3;
        default:
            break;
        }
    }
    rb->lcd_set_background(BG_COLOR);
    rb->lcd_clear_display();
    midend_force_redraw(me);
    rb->lcd_update();
    return 0;
}

/* points to pluginbuf, used by rbmalloc.c */
/* useless side note: originally giant_buffer was a statically
 * allocated giant array (4096KB IIRC), hence its name. */
char *giant_buffer = NULL;
static size_t giant_buffer_len = 0; /* set on start */

static void fix_size(void)
{
    int w = LCD_WIDTH, h = LCD_HEIGHT, h_x;
    rb->lcd_setfont(cur_font = FONT_UI);
    rb->lcd_getstringsize("X", NULL, &h_x);
    h -= h_x;
    midend_size(me, &w, &h, true);
}

static void init_tlsf(void)
{
    /* reset tlsf by nuking the signature */
    /* will make any already-allocated memory point to garbage */
    memset(giant_buffer, 0, 4);
    init_memory_pool(giant_buffer_len, giant_buffer);
}

static int read_wrapper(void *ptr, void *buf, int len)
{
    int fd = (int) ptr;
    return rb->read(fd, buf, len);
}

static void write_wrapper(void *ptr, const void *buf, int len)
{
    int fd = (int) ptr;
    rb->write(fd, buf, len);
}

static void init_colors(void)
{
    float *floatcolors = midend_colors(me, &ncolors);

    /* convert them to packed RGB */
    colors = smalloc(ncolors * sizeof(unsigned));
    unsigned *ptr = colors;
    float *floatptr = floatcolors;
    for(int i = 0; i < ncolors; ++i)
    {
        int r = 255 * *(floatptr++);
        int g = 255 * *(floatptr++);
        int b = 255 * *(floatptr++);
        LOGF("color %d is %d %d %d", i, r, g, b);
        *ptr++ = LCD_RGBPACK(r, g, b);
    }
    sfree(floatcolors);
}

static bool string_in_list(const char *target, const char **list)
{
    /* list is terminated with NULL */
    const char *i;

    while((i = *list++))
    {
        if(!strcmp(target, i))
            return true;
    }

    return false;
}

/* this function sets game-specific input settings */
static void tune_input(const char *name)
{
    static const char *want_spacebar[] = {
        "Magnets",
        "Map",
        "Mines",
        "Palisade",
        "Rectangles",
        NULL
    };

    /* these get a spacebar on long click - you must also add to the
     * falling_edge list below! */
    input_settings.want_spacebar = string_in_list(name, want_spacebar);

    static const char *falling_edge[] = {
        "Inertia",
        "Magnets",
        "Map",
        "Mines",
        "Palisade",
        "Rectangles",
        NULL
    };

    /* wait until a key is released to send an action */
    input_settings.falling_edge = string_in_list(name, falling_edge);

    /* For want_spacebar to work, events must be sent on the falling
     * edge */
    assert(!(input_settings.want_spacebar && !input_settings.falling_edge));

    /* ignore repeated keypresses in all games but untangle (mouse
     * mode overrides this no matter what) */
    static const char *allow_repeats[] = {
        "Untangle",
        NULL
    };

    input_settings.ignore_repeats = !string_in_list(name, allow_repeats);

    /* set to false if you want dragging to be possible */
    static const char *rclick_on_hold[] = {
        "Map",
        "Signpost",
        "Untangle",
        NULL
    };

    input_settings.rclick_on_hold = !string_in_list(name, rclick_on_hold);

    static const char *mouse_games[] = {
        "Loopy",
        NULL
    };

    mouse_mode = string_in_list(name, mouse_games);

    static const char *number_chooser_games[] = {
        "Filling",
        "Keen",
        "Solo",
        "Towers",
        "Undead",
        "Unequal",
        NULL
    };

    input_settings.numerical_chooser = string_in_list(name, number_chooser_games);
}

static const char *init_for_game(const game *gm, int load_fd)
{
    me = midend_new(NULL, gm, &rb_drawing, NULL);

    if(load_fd < 0)
    {
        new_game_notify();
    }
    else
    {
        const char *ret = midend_deserialize(me, read_wrapper, (void*) load_fd);
        if(ret)
            return ret;
    }

    fix_size();

    tune_input(gm->name);

    mouse_x = LCD_WIDTH / 2;
    mouse_y = LCD_HEIGHT / 2;

    init_colors();

    reset_drawing();

    return NULL;
}

static void shutdown_tlsf(void)
{
    memset(giant_buffer, 0, 4);
}

static void exit_handler(void)
{
#ifdef HAVE_SW_POWEROFF
    sw_poweroff_restore();
#endif

    unload_fonts();
    shutdown_tlsf();

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(false);
#endif
}

#ifdef FONT_CACHING
/* try loading the fonts indicated in the on-disk font table */
static void load_fonts(void)
{
    int fd = rb->open(FONT_TABLE, O_RDONLY);
    if(fd < 0)
        return;

    uint64_t fontmask = 0;
    while(1)
    {
        char linebuf[MAX_LINE], *ptr = linebuf;
        int len = rb->read_line(fd, linebuf, sizeof(linebuf));
        if(len <= 0)
            break;

        char *tok, *save;
        tok = rb->strtok_r(ptr, ":", &save);
        ptr = NULL;

        if(!strcmp(tok, midend_which_game(me)->name))
        {
            uint32_t left, right;
            tok = rb->strtok_r(ptr, ":", &save);
            left = atoi(tok);
            tok = rb->strtok_r(ptr, ":", &save);
            right = atoi(tok);
            fontmask = ((uint64_t)left << 31) | right;
            break;
        }
    }

    /* nothing to do */
    if(!fontmask)
    {
        rb->close(fd);
        return;
    }

    /* loop through each bit of the mask and try loading the
       corresponding font */
    for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
    {
        if(fontmask & ((uint64_t)1 << i))
        {
            int size = (i > BUNDLE_COUNT  ? i - BUNDLE_COUNT : i) + BUNDLE_MIN;
            int type = i > BUNDLE_COUNT ? FONT_VARIABLE : FONT_FIXED;
            rb_setfont(type, size);
        }
    }

    rb->close(fd);
}

/* remember which fonts were loaded */
static void save_fonts(void)
{
#if 2*BUNDLE_COUNT > 62
#error too many fonts for 62-bit mask
#endif

    /* first assemble the bitmask */
    uint64_t fontmask = 0;
    for(int i = 0; i < 2 * BUNDLE_COUNT; ++i)
    {
        /* try loading if we attempted to load */
        if(loaded_fonts[i].status >= -2)
        {
            fontmask |= (uint64_t)1 << i;
        }
    }

    if(fontmask)
    {
        /* font table format is as follows:
         * [GAME NAME]:[32-halves of bit mask in decimal][newline]
         */
        int fd = rb->open(FONT_TABLE, O_RDONLY);
        int outfd = rb->open(FONT_TABLE ".tmp", O_WRONLY | O_CREAT | O_TRUNC, 0666);
        if(outfd < 0)
            return;

        uint64_t oldmask = 0;

        if(fd >= 0)
        {
            while(1)
            {
                char linebuf[MAX_LINE], *ptr = linebuf;
                char origbuf[MAX_LINE];
                int len = rb->read_line(fd, linebuf, sizeof(linebuf));
                if(len <= 0)
                    break;
                rb->memcpy(origbuf, linebuf, len);

                char *tok, *save;
                tok = rb->strtok_r(ptr, ":", &save);
                ptr = NULL;

                /* copy line if not matching */
                if(strcmp(tok, midend_which_game(me)->name) != 0)
                {
                    rb->write(outfd, origbuf, len);
                    rb->fdprintf(outfd, "\n");
                }
                else
                {
                    /* matching, remember the old mask */
                    assert(oldmask == 0);
                    uint32_t left, right;
                    tok = rb->strtok_r(ptr, ":", &save);
                    left = atoi(tok);
                    tok = rb->strtok_r(ptr, ":", &save);
                    right = atoi(tok);
                    oldmask = ((uint64_t)left << 31) | right;
                }
            }
            rb->close(fd);
        }
        uint64_t final = fontmask;
        if(n_fonts < MAX_FONTS)
            final |= oldmask;
        uint32_t left = final >> 31;
        uint32_t right = final & 0x7fffffff;
        rb->fdprintf(outfd, "%s:%lu:%lu\n", midend_which_game(me)->name, left, right);
        rb->close(outfd);
        rb->rename(FONT_TABLE ".tmp", FONT_TABLE);
    }
}
#endif

static void save_fname(char *buf)
{
    rb->snprintf(buf, MAX_PATH, "%s/sgt-%s.sav", PLUGIN_GAMES_DATA_DIR, thegame.htmlhelp_topic);
}

/* expects a totally free me* pointer */
static bool load_game(void)
{
    char fname[MAX_PATH];
    save_fname(fname);

    int fd = rb->open(fname, O_RDONLY);
    if(fd < 0)
        return false;

    rb->splash(0, "Loading...");

    char *game;
    const char *ret = identify_game(&game, read_wrapper, (void*)fd);

    if(!game && ret)
    {
        rb->splash(HZ, ret);
        rb->close(fd);
        return false;
    }
    else
    {
        /* seek to beginning */
        rb->lseek(fd, 0, SEEK_SET);

        if(!strcmp(game, thegame.name))
        {
            ret = init_for_game(&thegame, fd);
            if(ret)
            {
                rb->splash(HZ, ret);
                rb->close(fd);
                rb->remove(fname);
                return false;
            }
            rb->close(fd);
            rb->remove(fname);

#ifdef FONT_CACHING
            load_fonts();
#endif

            /* success */
            return true;
        }
        rb->splash(HZ, "Load failed.");

        /* clean up, even on failure */
        rb->close(fd);
        rb->remove(fname);

        return false;
    }
}

static void save_game(void)
{
    rb->splash(0, "Saving...");

    char fname[MAX_PATH];
    save_fname(fname);

    /* save game */
    int fd = rb->open(fname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
    midend_serialize(me, write_wrapper, (void*) fd);
    rb->close(fd);

#ifdef FONT_CACHING
    save_fonts();
#endif

    rb->lcd_update();
}

static int mainmenu_cb(int action, const struct menu_item_ex *this_item)
{
    int i = (intptr_t) this_item;
    if(action == ACTION_REQUEST_MENUITEM)
    {
        switch(i)
        {
        case 0:
        case 7:
            if(!load_success)
                return ACTION_EXIT_MENUITEM;
            break;
        case 3:
            break;
        case 4:
            if(audiobuf_available)
                break;
            else
                return ACTION_EXIT_MENUITEM;
        case 5:
            if(!midend_get_presets(me, NULL)->n_entries)
                return ACTION_EXIT_MENUITEM;
            break;
        case 6:
            if(!midend_which_game(me)->can_configure)
                return ACTION_EXIT_MENUITEM;
            break;
        default:
            break;
        }
    }
    return action;
}

static void puzzles_main(void)
{
    rb_atexit(exit_handler);

#ifdef HAVE_SW_POWEROFF
    sw_poweroff_disable();
#endif

    init_default_settings();
    init_fonttab();

    load_success = load_game();

    if(!load_success)
    {
        /* our main menu expects a ready-to-use midend */
        init_for_game(&thegame, -1);
    }

    /* must be done before any menu needs to be displayed */
    rb->snprintf(menu_desc, sizeof(menu_desc), "%s Menu", midend_which_game(me)->name);

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    /* about to go to menu or button block */
    rb->cpu_boost(false);
#endif

#if defined(FOR_REAL) && defined(DEBUG_MENU)
    help_times = 0;
#endif

    MENUITEM_STRINGLIST(menu, menu_desc, mainmenu_cb,
                        "Resume Game",         // 0
                        "New Game",            // 1
                        "Quick Help",          // 2
                        "Extensive Help",      // 3
                        "Playback Control",    // 4
                        "Game Type",           // 5
                        "Configure Game",      // 6
                        "Quit without Saving", // 7
                        "Quit");               // 8

    bool quit = false;
    int sel = 0;

    while(!quit)
    {
        switch(rb->do_menu(&menu, &sel, NULL, false))
        {
        case 0:
            /* Loaded. Run the game! */
            goto game_loop;
        case 1:
            if(!load_success)
            {
                /* Failed to load (so midend is already initialized
                 * with new game) */
                goto game_loop;
            }

            /* Otherwise we need to generate a new game. */
            quit = true;
            break;
        case 2:
            quick_help();
            break;
        case 3:
            full_help(midend_which_game(me)->name);
            break;
        case 4:
            playback_control(NULL);
            break;
        case 5:
            if(presets_menu())
            {
                new_game_notify();
                goto game_loop;
            }
            break;
        case 6:
            if(config_menu())
            {
                new_game_notify();
                goto game_loop;
            }
            break;
        case 8:
            if(load_success)
                save_game();
            /* fall through */
        case 7:
            /* we don't care about freeing anything because tlsf will
             * be wiped out the next time around */
            return;
        default:
            break;
        }
    }

    while(1)
    {
        init_for_game(&thegame, -1);
    game_loop:
        reset_drawing();
        clear_and_draw();

        last_keystate = 0;
        accept_input = true;

        while(1)
        {
            int button = process_input(timer_on ? TIMER_INTERVAL : -1, true);

            /* special codes are < 0 */
            if(button < 0)
            {
                rb_unclip(NULL);
                deactivate_timer(NULL);

                if(titlebar)
                {
                    sfree(titlebar);
                    titlebar = NULL;
                }

                switch(button)
                {
                case -1:
                    /* new game */
                    midend_free(me);
                    break;
                case -2:
                    /* quit without saving */
                    midend_free(me);
                    sfree(colors);
                    return;
                case -3:
                    /* save and quit */
                    save_game();
                    midend_free(me);
                    sfree(colors);
                    return;
                default:
                    break;
                }
            }

            /* we have a game input */
            if(button)
                midend_process_key(me, 0, 0, button);

            if(timer_on)
                timer_cb();

            midend_redraw(me);

            draw_title(true); /* will draw to fb */

            /* Blit mouse but immediately clear it. */
            if(mouse_mode)
                draw_mouse();

            rb->lcd_update();

            if(mouse_mode)
                clear_mouse();

            rb->yield();
        }
        sfree(colors);
    }
}

enum plugin_status plugin_start(const void *param)
{
    (void) param;

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    /* boost for init */
    rb->cpu_boost(true);
#endif

    giant_buffer = rb->plugin_get_buffer(&giant_buffer_len);
    init_tlsf();

    if(!strcmp(thegame.name, "Solo"))
    {
        /* Solo needs a big stack */
        int stack_sz = 16 * DEFAULT_STACK_SIZE;
        uintptr_t old = (uintptr_t)smalloc(stack_sz);

        /* word alignment */
        long *stack = (long*)((char*)(((uintptr_t)old & (uintptr_t)(~0x3)) + 4));
        stack_sz -= ((char*)stack - (char*)old);

        thread = rb->create_thread(puzzles_main, stack, stack_sz, 0, "puzzles"
                                   IF_PRIO(, PRIORITY_USER_INTERFACE) IF_COP(, CPU));
        rb->thread_wait(thread);
    }
    else
        puzzles_main();

    return PLUGIN_OK;
}