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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2006 Frederik M.J. Vestre
* Copyright (C) 2006 Adam Gashlin
*
* 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.
*
****************************************************************************/
#include "plugin.h"
#include <ctype.h>
#include "lib/playback_control.h"
#include <inttypes.h>
#include "mallocer.h" /*mallocer resetable memory allocator*/
#include "mwdb.h"
#include "lib/pluginlib_exit.h"
#if LCD_DEPTH > 1
#define COLORLINKS
#endif
/* keymappings */
#if (CONFIG_KEYPAD == IPOD_4G_PAD)
#define WIKIVIEWER_MENU BUTTON_MENU
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_SCROLL_FWD
#define WIKIVIEWER_FWD_REPEAT (BUTTON_SCROLL_FWD|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_SCROLL_BACK
#define WIKIVIEWER_BACK_REPEAT (BUTTON_SCROLL_BACK|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_PLAY
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_PLAY|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
(CONFIG_KEYPAD == IRIVER_H300_PAD)
#define WIKIVIEWER_MENU BUTTON_OFF
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_REC|BUTTON_MODE)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_MODE)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_MODE)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_REC
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_REC|BUTTON_MODE)
#elif (CONFIG_KEYPAD == GIGABEAT_PAD)
#define WIKIVIEWER_MENU BUTTON_MENU
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_A
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_A|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == GIGABEAT_S_PAD)
#define WIKIVIEWER_MENU BUTTON_MENU
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_BACK
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_BACK|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == MROBE100_PAD)
#define WIKIVIEWER_MENU BUTTON_MENU
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_DISPLAY
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_DISPLAY|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
#define WIKIVIEWER_MENU BUTTON_REC
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_PLAY
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_PLAY|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == SANSA_C200_PAD)
#define WIKIVIEWER_MENU BUTTON_POWER
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_FWD2 BUTTON_VOL_UP
#define WIKIVIEWER_FWD2_REPEAT (BUTTON_VOL_UP|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_BACK2 BUTTON_VOL_DOWN
#define WIKIVIEWER_BACK2_REPEAT (BUTTON_VOL_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_REC
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_REC|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == SANSA_E200_PAD)
#define WIKIVIEWER_MENU BUTTON_POWER
#define WIKIVIEWER_SELECT BUTTON_SELECT
#define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT)
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_FWD2 BUTTON_SCROLL_FWD
#define WIKIVIEWER_FWD2_REPEAT (BUTTON_SCROLL_FWD|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_BACK2 BUTTON_SCROLL_BACK
#define WIKIVIEWER_BACK2_REPEAT (BUTTON_SCROLL_BACK|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
#define WIKIVIEWER_RECORD BUTTON_REC
#define WIKIVIEWER_RECORD_REPEAT (BUTTON_REC|BUTTON_REPEAT)
#elif (CONFIG_KEYPAD == SANSA_FUZE_PAD)
#define WIKIVIEWER_MENU BUTTON_HOME
#define WIKIVIEWER_SELECT BUTTON_SELECT
/* #define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT) */
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_FWD2 BUTTON_SCROLL_FWD
#define WIKIVIEWER_FWD2_REPEAT (BUTTON_SCROLL_FWD|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_BACK2 BUTTON_SCROLL_BACK
#define WIKIVIEWER_BACK2_REPEAT (BUTTON_SCROLL_BACK|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
/* #define WIKIVIEWER_RECORD (BUTTON_SELECT|BUTTON_REPEAT) */
/* #define WIKIVIEWER_RECORD_REPEAT (BUTTON_HOME|BUTTON_REPEAT) */
#elif (CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD)
#define WIKIVIEWER_MENU BUTTON_POWER
#define WIKIVIEWER_SELECT BUTTON_SELECT
/* #define WIKIVIEWER_STOP_RECORD (BUTTON_SELECT|BUTTON_REPEAT) */
#define WIKIVIEWER_FWD BUTTON_DOWN
#define WIKIVIEWER_FWD_REPEAT (BUTTON_DOWN|BUTTON_REPEAT)
#define WIKIVIEWER_FWD2 BUTTON_PLAYPAUSE
#define WIKIVIEWER_FWD2_REPEAT (BUTTON_PLAYPAUSE|BUTTON_REPEAT)
#define WIKIVIEWER_BACK BUTTON_UP
#define WIKIVIEWER_BACK_REPEAT (BUTTON_UP|BUTTON_REPEAT)
#define WIKIVIEWER_BACK2 BUTTON_BACK
#define WIKIVIEWER_BACK2_REPEAT (BUTTON_BACK|BUTTON_REPEAT)
#define WIKIVIEWER_PREV BUTTON_LEFT
#define WIKIVIEWER_NEXT BUTTON_RIGHT
/* #define WIKIVIEWER_RECORD (BUTTON_SELECT|BUTTON_REPEAT) */
/* #define WIKIVIEWER_RECORD_REPEAT (BUTTON_HOME|BUTTON_REPEAT) */
#elif (CONFIG_KEYPAD == ONDAVX747_PAD)
#define WIKIVIEWER_MENU BUTTON_POWER
#endif
#ifdef HAVE_TOUCHSCREEN
#ifndef WIKIVIEWER_SELECT
#define WIKIVIEWER_SELECT BUTTON_CENTER
#endif
#ifndef WIKIVIEWER_MENU
#define WIKIVIEWER_MENU BUTTON_TOPLEFT
#endif
#ifndef WIKIVIEWER_FWD
#define WIKIVIEWER_FWD BUTTON_BOTTOMMIDDLE
#define WIKIVIEWER_FWD_REPEAT (BUTTON_BOTTOMMIDDLE|BUTTON_REPEAT)
#endif
#ifndef WIKIVIEWER_BACK
#define WIKIVIEWER_BACK BUTTON_TOPMIDDLE
#define WIKIVIEWER_BACK_REPEAT (BUTTON_TOPMIDDLE|BUTTON_REPEAT)
#endif
#ifndef WIKIVIEWER_PREV
#define WIKIVIEWER_PREV BUTTON_MIDLEFT
#endif
#ifndef WIKIVIEWER_NEXT
#define WIKIVIEWER_NEXT BUTTON_MIDRIGHT
#endif
#endif
#define LINEBUFLEN 255
#define MARKUP_STARTULINE 1
#define MARKUP_ENDULINE 2
#define MARKUP_STARTBOLD 3
#define MARKUP_ENDBOLD 4
#define MARKUP_STARTITALIC 5
#define MARKUP_ENDITALIC 6
#define MARKUP_STARTLINK 7
#define MARKUP_ENDLINK 8
#define MARKUP_LINEFEED 10
#define MARKUP_MODE 15
/* change on any change to file format, histstate */
#define SAVE_FILE_VERSION_MAGIC 0x12340004
static bool wrquit=false;
static char filename[MAX_PATH+1]; /* name of database, without extension */
#define MAINMEMBUF 0
static void *inflatebuf; /* heap for gunzip */
static char *articlebuf; /* destination for uncompressed article */
static uint32_t articlebuflen=0; /* size of the article buffer */
static uint32_t articlelen=0; /* used size of the article buffer */
static int lastlinkcount=0; /* set by render() to the number of links
found */
/* provides everything render() needs to know to render a screen */
struct linestate {
int32_t renderoff;
int renderlen;
int32_t linknameoff;
int inlink,inlinkname;
#ifdef COLORLINKS
uint8_t inem;
#endif
int eof; /* should be interpreted as disallowing further scrolling down */
};
#define ARTICLENAMEBUF_LENGTH 255
/* where we'll be scrolling next */
static struct linestate curlinestate,nextlinestate;
/* scrollback, to allow scrolling backwards fairly quickly (without some memory
entire article might need to be reparsed to find context of previous line)
stack, up to SCROLLBACK_LENGTH, past that the oldest entry is discarded */
#define SCROLLBACK_LENGTH 50
static int curlinehist,linehistdepth;
static int curline;
static struct linestate linehist[SCROLLBACK_LENGTH];
/* bookmarks, for going back to a previous article */
struct bookstate {
char name[ARTICLENAMEBUF_LENGTH];
int32_t curoff;
uint32_t res_lo; /* avoid searching again */
uint32_t res_hi;
};
/* history, for going back to a previous article */
struct histstate {
char name[ARTICLENAMEBUF_LENGTH];
int32_t curoff;
uint32_t res_lo; /* avoid searching again */
uint32_t res_hi;
};
/*
It should be possible to reconstruct a linestate from a histstate as follows:
1) load the article given by res_lo, res_hi 2) linestate=0 3) while
linestate.renderoff<histstate.curoff && !eof, advance
*/
/* stack, up to HISTORY_LENGTH deep, past that oldest entry is discarded */
/* the current article occupies hist[curhist] */
#define HISTORY_LENGTH 20
static int curhist,histdepth;
static int bookdepth;
static bool record_stop_flag=true;
static struct histstate hist[HISTORY_LENGTH];
static struct bookstate book[200];
static int record_flag=0; /*set to 1 when recording a line to an external
file*/
static int last_line_i=0; /*set to linespace when recording a line to an
external file*/
static char last_line[256]; /* the current line of text to be recorded */
static int end_of_file=0; /*flag for end of file in recording*/
static int record_linebreak=0; /*flag for line break in recorded line*/
static void viewer_exit(void *parameter);
static struct linestate render(struct linestate cur, int norender,
int linktonote);
static void readlink_wiki(struct linestate cur, int link, char * namebuf,
int buflen);
static void viewer_exit(void *parameter)
{
(void)parameter;
}
static void set_article_offset(int32_t off);
static void reset_scrollback(void);
static void advance_scrollback(int a, int dorender);
static void reset_history(void);
static void advance_history(int d);
static int load_bookmarks(const char * filename)
{
char fnbuf[MAX_PATH+1];
int fd, i, booki, ret = 0;
uint32_t magic=0;
rb->snprintf(fnbuf,MAX_PATH,"%s.wwb",filename);
fd = rb->open(fnbuf, O_RDONLY);
if (fd<0)
goto err;
if (rb->read(fd,&magic,sizeof(magic))<=0)
goto err;
if (magic!=SAVE_FILE_VERSION_MAGIC)
goto err;
if (rb->read(fd,&booki,sizeof(booki))<=0)
goto err;
if (booki<=0)
goto err;
for (i=0; i<booki; i++)
{
if (rb->read(fd,book+i,sizeof(struct bookstate))<=0)
goto err;
}
bookdepth = booki;
ret = 1;
err:
if(fd >= 0)
rb->close(fd);
return ret;
}
static void save_bookmark(const char * filename)
{
char fnbuf[MAX_PATH+1];
int fd;
int i,booki;
uint32_t magic;
rb->snprintf(fnbuf,MAX_PATH,"%s.wwb",filename);
fd = rb->open(fnbuf, O_WRONLY|O_CREAT);
if (fd<0) return;
magic=SAVE_FILE_VERSION_MAGIC;
booki=0;
rb->write(fd, &magic, sizeof(magic));
rb->write(fd, &bookdepth, sizeof(bookdepth));
for (i=0; i<bookdepth; i++)
{
rb->write(fd, book+booki, sizeof(struct bookstate));
booki=(booki+1);
}
rb->close(fd);
}
static void add_bookmark(void)
{
rb->strcpy(book[bookdepth].name,hist[curhist].name);
book[bookdepth].curoff=hist[curhist].curoff;
book[bookdepth].res_lo=hist[curhist].res_lo;
book[bookdepth].res_hi=hist[curhist].res_hi;
bookdepth++;
}
static void render_bookmarks(int off)
{
int i=(bookdepth-1),j,d;
int x,y,fontheight,t;
char buf[10];
rb->lcd_clear_display();
y=1;
fontheight = rb->font_get(FONT_UI)->height;
for (j=1,d=(bookdepth); d>0 && y+fontheight<LCD_HEIGHT; d--,j++)
{
if (j>=(off+1))
{
rb->snprintf(buf,10,"%3d. ",j);
rb->lcd_putsxy(1,y,buf);
rb->lcd_getstringsize(buf,&x,&t);
rb->lcd_putsxy(1+x,y,book[i].name);
y+=fontheight;
}
i--;
}
rb->lcd_update();
}
static void remove_bookmark(int bookviewoff)
{
int i,booki;
booki=bookdepth-bookviewoff-1;
for (i=booki; i<bookdepth; i++)
{
rb->strcpy(book[i].name,book[(i+1)].name);
book[i].curoff=book[(i+1)].curoff;
book[i].res_lo=book[(i+1)].res_lo;
book[i].res_hi=book[(i+1)].res_hi;
}
bookdepth--;
}
static void save_status(const char * filename)
{
char fnbuf[MAX_PATH+1];
int fd;
int i,histi;
uint32_t magic;
rb->snprintf(fnbuf,MAX_PATH,"%s.wws",filename);
fd = rb->open(fnbuf, O_WRONLY|O_CREAT);
if (fd<0) return;
magic=SAVE_FILE_VERSION_MAGIC;
rb->write(fd, &magic, sizeof(magic));
rb->write(fd, &histdepth, sizeof(histdepth));
histi=curhist-(histdepth-1);
if (histi<0) histi+=HISTORY_LENGTH;
for (i=0; i<HISTORY_LENGTH; i++)
{
rb->write(fd, hist+histi, sizeof(struct histstate));
histi=(histi+1)%HISTORY_LENGTH;
}
rb->close(fd);
}
static int load_status(const char * filename)
{
char fnbuf[MAX_PATH+1];
int fd, i, histi, ret = 0;
uint32_t magic=0;
rb->snprintf(fnbuf,MAX_PATH,"%s.wws",filename);
fd = rb->open(fnbuf, O_RDONLY);
if (fd<0)
goto err;
if (rb->read(fd,&magic,sizeof(magic))<=0)
goto err;
if (magic!=SAVE_FILE_VERSION_MAGIC)
goto err;
if (rb->read(fd,&histi,sizeof(histi))<=0)
goto err;
if (histi<=0)
goto err;
for (i=0; i<histi; i++)
{
if (rb->read(fd,hist+i,sizeof(struct histstate))<=0)
goto err;
}
histdepth=histi;
curhist=i-1;
ret = 1;
err:
if(fd >= 0)
rb->close(fd);
return ret;
}
static void set_article_offset(int32_t off)
{
/* go to offset off in current article */
reset_scrollback();
curline=0;
rb->memset(&curlinestate,0,sizeof(curlinestate));
rb->memset(&nextlinestate,0,sizeof(nextlinestate));
hist[curhist].curoff=curlinestate.renderoff=10+articlebuf[8];
curlinestate.renderlen=articlelen-10-articlebuf[8];
nextlinestate=render(curlinestate,1,0);
while (!nextlinestate.eof && hist[curhist].curoff<off)
{
advance_scrollback(1,0);
}
}
static void reset_scrollback(void)
{
curlinehist=0;
linehistdepth=0;
}
/* all scrolling done through this */
static void advance_scrollback(int a, int dorender)
{
int i;
if (a<-1)
{
if ((a+curline)<0)
a=(-curline);
for (i=0; i<-a; i++)
{
advance_scrollback(-1,(dorender && i==-a-1));
}
}
else if (a>1)
for (i=0; i<a; i++)
{
advance_scrollback(1,(dorender && i==a-1));
}
else if (a==0)
return;
else if (a<0) /* a==-1 */
{ /* scroll backwards one line */
if (curline==0) return;
curline--;
if (linehistdepth>0)
{
/* if we have records to fall back on */
linehistdepth--;
curlinehist--;
if (curlinehist<0)
curlinehist=SCROLLBACK_LENGTH-1;
curlinestate=linehist[curlinehist];
nextlinestate=render(curlinestate,!dorender,0);
hist[curhist].curoff=curlinestate.renderoff;
}
else
{
int amounttoscroll=curline;
/* redo from start */
reset_scrollback(); /* clear scrollback */
set_article_offset(0); /* go to top */
advance_scrollback(amounttoscroll,dorender); /* scoll down */
}
}
else /* a==1 */
/* scroll forwards one line */
if (!nextlinestate.eof || ((record_flag==1) && (end_of_file==0)))
{
linehist[curlinehist]=curlinestate;
curlinehist=(curlinehist+1)%SCROLLBACK_LENGTH;
linehistdepth++;
if (linehistdepth>SCROLLBACK_LENGTH)
linehistdepth=SCROLLBACK_LENGTH;
curlinestate=nextlinestate;
curline++;
nextlinestate=render(curlinestate,!dorender,0);
hist[curhist].curoff=curlinestate.renderoff;
}
}
static void reset_history(void)
{
curhist=0;
histdepth=1;
}
static void advance_history(int d)
{
if (d==1)
{
curhist=(curhist+1)%HISTORY_LENGTH;
histdepth++;
if (histdepth>HISTORY_LENGTH) histdepth=HISTORY_LENGTH;
}
else if (d==-1)
if (histdepth>1)
{
curhist--;
if (curhist<0) curhist=HISTORY_LENGTH-1;
histdepth--;
}
}
static int iswhitespace(uint16_t c)
{
return (c==' ' || c=='\n'); /* what about tab character? */
}
static int islinebreak(uint16_t c)
{
return (c=='\n');
}
/* return next place to scroll to */
static struct linestate render(struct linestate cur, int norender,
int linktonote)
{
int i; /* offset in buf */
int x,y; /* current render location */
const char * nextchar; /* character after the current UTF-8 character
*/
int charsize; /* size of the current UTF-8 character */
uint16_t ucs; /* current UTF-8 character */
char buf[LINEBUFLEN+1]; /* the current line of text to be rendered */
uint8_t underline[LCD_WIDTH]; /* should there be an underline at this pixel?
*/
int fontheight;
int first = 0; /*record only first line through*/
char fnbuf1[MAX_PATH+1]; /*for recording line of text*/
int fd1; /*for recording line of text*/
end_of_file=0;
#ifdef COLORLINKS
uint8_t em[LCD_WIDTH]; /* type of emphasis for this pixel */
unsigned lastcol=0;
rb->memset(em,0,sizeof(em));
#endif
struct linestate nextline; /* state to return */
/* state to back up to when doing line break */
int lastspace_i=0;
int lastspace_linkcount=0;
struct linestate lastspace;
int linkcount;
int linedone=0;
/* if the last character was whitespace we'll ignore subsequent whitespace
*/
int lastwaswhitespace;
fontheight = rb->font_get(FONT_UI)->height;
y=1;
linkcount=1;
lastlinkcount=0;
nextline.renderoff=-1;
/* it's not going to be any less the end... */
nextline.eof=cur.eof;
if (!norender) rb->memset(underline,0,sizeof(underline));
while (((y+fontheight) < LCD_HEIGHT) && cur.renderlen>0)
{
lastspace.renderoff=-1;
ucs=0;
/* prevent whitespace from appearing at the beginning of a line */
lastwaswhitespace=1;
/* scroll halfway down the screen */
/* if (y==(LCD_HEIGHT/fontheight/2)*fontheight+1) halfway=article; */
/* see how much we can fit on a line */
for (x=1,i=0,linedone=0; !linedone && i<LINEBUFLEN && cur.renderlen>0; )
{
/*handle markup*/
switch (articlebuf[cur.renderoff])
{
case MARKUP_STARTULINE:
case MARKUP_STARTBOLD:
case MARKUP_STARTITALIC:
#ifdef COLORLINKS
switch (articlebuf[cur.renderoff])
{
case MARKUP_STARTULINE:
cur.inem=1;
break;
case MARKUP_STARTBOLD:
cur.inem=2;
break;
case MARKUP_STARTITALIC:
cur.inem=3;
break;
}
#endif
cur.renderlen--;
cur.renderoff++;
break;
case MARKUP_ENDULINE:
case MARKUP_ENDBOLD:
case MARKUP_ENDITALIC:
#ifdef COLORLINKS
if (cur.inem)
cur.inem=0;
#endif
cur.renderlen--;
cur.renderoff++;
break;
case MARKUP_STARTLINK:
cur.inlink=1;
cur.linknameoff=cur.renderoff;
cur.renderlen--;
cur.renderoff++;
break;
case MARKUP_ENDLINK:
if (cur.inlinkname)
{
cur.inlinkname=0;
linkcount++;
}
if (cur.inlink)
{
/* start outputting link text */
cur.renderlen+=cur.renderoff-cur.linknameoff;
cur.renderoff-=cur.renderoff-cur.linknameoff;
cur.linknameoff=0;
cur.inlink=0;
cur.inlinkname=1;
}
cur.renderlen--;
cur.renderoff++;
break;
case MARKUP_MODE:
if (cur.inlink) cur.linknameoff=cur.renderoff;
cur.renderlen--;
cur.renderoff++;
break;
default:
nextchar = rb->utf8decode(articlebuf+cur.renderoff,&ucs);
charsize = nextchar-cur.renderoff-articlebuf;
/* multiple newlines work */
if (islinebreak(ucs)) linedone=1; /* break;*/ /* but multiple
other whitespace will be
ignored */
if (!linedone && !cur.inlink && !(iswhitespace(ucs) &&
lastwaswhitespace))
{
/* display */
int charwidth=rb->font_get_width(rb->font_get(FONT_UI),ucs);
if (iswhitespace(ucs))
{
lastspace=cur;
lastspace_i=i;
lastspace_linkcount=linkcount;
lastwaswhitespace=1;
}
else lastwaswhitespace=0;
if ((x+=charwidth) > LCD_WIDTH)
{
linedone=1;
break;
}
if (!norender)
{
rb->memcpy(buf+i,articlebuf+cur.renderoff,charsize);
if (cur.inlinkname)
rb->memset(underline+(x-charwidth),linkcount,
charwidth);
#ifdef COLORLINKS
if (cur.inem)
rb->memset(em+(x-charwidth),cur.inem,charwidth);
#endif
}
i+=charsize;
}
else
{
/* hidden */
}
cur.renderlen-=charsize;
cur.renderoff+=charsize;
} /* end markup switch */
} /* end for characters in a line */
if (x>=LCD_WIDTH)
{
/* the next character would be offscreen, terminate at previous
space */
if (lastspace.renderoff==-1)
{
/* if we have a long word, break it here */
}
else
{
cur=lastspace;
linkcount=lastspace_linkcount;
i=lastspace_i;
}
}
if (!norender)
{
int uc,ucm,t;
if (first<1)
{
if (record_flag==1)
{
record_flag=0;
rb->snprintf(fnbuf1,MAX_PATH,"/wiki/%s.txt",hist[curhist].name);
fd1 = rb->open(fnbuf1, O_WRONLY|O_APPEND|O_CREAT);
if (fd1<0)
rb->splash(HZ*2, "Recording NOT done");
else
{
if (record_stop_flag)
{
rb->write(fd1,"\n\r",2);
record_stop_flag=false;
}
rb->write(fd1,(unsigned char *)last_line,last_line_i);
if (record_linebreak>0)
rb->write(fd1,"\n",2);
else
rb->write(fd1," ",1);
rb->close(fd1);
}
}
last_line_i=i;
rb->memcpy(last_line,buf,(last_line_i));
if (islinebreak(ucs))
record_linebreak=1;
else
record_linebreak=0;
}
first++;
buf[(i<=LINEBUFLEN) ? i : LINEBUFLEN]='\0';
rb->lcd_putsxy(1,y, (unsigned char *)buf);
rb->lcd_getstringsize(buf,&ucm,&t);
int oldmode=rb->lcd_get_drawmode();
/* mark links */
for (uc=0; uc<LCD_WIDTH; uc++)
{
#ifdef COLORLINKS
/* mark emphasis */
if (em[uc])
{
lastcol=rb->lcd_get_foreground();
rb->lcd_set_drawmode(DRMODE_SOLID);
switch (em[uc])
{
case 1:
rb->lcd_set_foreground(LCD_RGBPACK(255,0,0));
break;
case 2:
rb->lcd_set_foreground(LCD_RGBPACK(0,255,0));
break;
case 3:
rb->lcd_set_foreground(LCD_RGBPACK(0,0,255));
break;
}
rb->lcd_drawpixel(uc,y+fontheight-1);
rb->lcd_set_foreground(lastcol);
}
em[uc]=0;
#endif
if (underline[uc] && uc > 1 && uc < (ucm+1))
{
if (underline[uc]==linktonote)
{
rb->lcd_set_drawmode(DRMODE_COMPLEMENT);
rb->lcd_vline(uc,y,y+fontheight-1);
}
else
{
rb->lcd_set_drawmode(DRMODE_SOLID);
rb->lcd_drawpixel(uc,y+fontheight-1);
}
lastlinkcount=underline[uc];
}
underline[uc]=0;
/* clear rest of line */
if (uc >= ucm+1)
{
rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
rb->lcd_vline(uc,y,y+fontheight-1);
}
}
rb->lcd_set_drawmode(oldmode);
}
y+=fontheight;
/* scroll one line */
if (nextline.renderoff==-1)
{
nextline=cur;
if (norender) return nextline;
}
}
if (cur.renderlen<=0)
{
if (first<2)
end_of_file=1;
cur.eof=1;
nextline.eof=1;
while ((y+fontheight) < LCD_HEIGHT)
{
rb->lcd_putsxy(1,y," ");
y+=fontheight;
}
}
if (!norender)
rb->lcd_update();
return ((nextline.renderoff==-1) ? cur : nextline);
}
static void readlink_wiki(struct linestate cur, int link, char * namebuf, int buflen)
{
int linkcount=0;
rb->memset(namebuf,0,buflen);
if (!link)
return;
/*
screen starts partway through a link
*/
if (cur.inlink || cur.inlinkname)
/* seek back to the start of the link */
while (articlebuf[cur.renderoff] != MARKUP_STARTLINK)
{
cur.renderoff--;
cur.renderlen++;
}
while (cur.renderlen)
{
if (articlebuf[cur.renderoff] == MARKUP_STARTLINK)
{
linkcount++;
if (linkcount==link)
{
cur.renderoff++;
cur.renderlen--;
break;
}
}
cur.renderoff++;
cur.renderlen--;
}
if (linkcount==link)
{
int i;
for (i=0; i<buflen-1 && cur.renderlen>0; )
{
int charsize;
if (articlebuf[cur.renderoff]==MARKUP_ENDLINK || articlebuf[cur.renderoff]==MARKUP_MODE) break;
charsize = rb->utf8seek(articlebuf+cur.renderoff,1);
rb->memcpy(namebuf+i,articlebuf+cur.renderoff,charsize);
cur.renderlen-=charsize;
i+=charsize;
cur.renderoff+=charsize;
}
}
}
/* locate headings, indicated by a line beginning with = */
/* this is unfortunately quite stupid about markup in headings */
static int render_outline(int inoff, int norender)
{
int renderoff;
int renderlen;
int nextlineoff;
int linecount;
int intdepth=0;
int extdepth=0;
int linestart;
int charsize;
int fontheight;
int y;
char preserv;
enum {
STATE_START, /* start of line */
STATE_DD, /* determining depth */
STATE_DD2, /* check closing =s */
STATE_HTEXT, /* heading text */
STATE_OTHERTEXT /* other text */
} state;
if (inoff<10) inoff=10;
renderoff=inoff;
renderlen=articlelen-renderoff;
linecount=0;
y=1;
fontheight=rb->font_get(FONT_UI)->height;
if (!norender)
rb->lcd_clear_display();
if (renderoff==10) /* we will include the article title */
{
if (!norender)
{
preserv=articlebuf[articlebuf[8]+10];
articlebuf[articlebuf[8]+10]='\0';
rb->lcd_putsxy(1,y,articlebuf+10);
articlebuf[articlebuf[8]+10]=preserv;
}
y+=fontheight;
linecount++;
renderlen-=articlebuf[8];
renderoff+=articlebuf[8];
}
nextlineoff=renderoff;
linestart=renderoff;
state=STATE_START;
while (renderlen>0 && y+fontheight < LCD_HEIGHT)
{
switch (state)
{
case STATE_START:
linestart=renderoff;
switch (articlebuf[renderoff])
{
case '=':
if (linecount==1)
{
nextlineoff=renderoff; /* beginning of second line
*/
if (norender) return nextlineoff;
}
intdepth=1;
state=STATE_DD;
break;
case MARKUP_LINEFEED:
break;
default:
state=STATE_OTHERTEXT;
break;
}
break;
case STATE_DD:
switch (articlebuf[renderoff])
{
case '=':
intdepth++;
break;
case MARKUP_LINEFEED:
state=STATE_START; /* give up */
break;
default:
state=STATE_HTEXT;
break;
}
break;
case STATE_DD2:
switch (articlebuf[renderoff])
{
case '=':
extdepth++;
break;
case MARKUP_LINEFEED:
/* final acceptance here */
if (!norender)
{
if (linecount==0)
rb->lcd_clear_display();
int depth;
if(extdepth<intdepth)
depth=extdepth;
else
depth=intdepth;
int whitey=0;
preserv=articlebuf[renderoff-depth];
articlebuf[renderoff-depth]='\0';
/* trim leading whitespace */
while (iswhitespace(articlebuf[linestart+depth+
whitey]))
{
charsize = rb->utf8seek(articlebuf+linestart+
depth+whitey,1);
whitey+=charsize;
}
rb->lcd_putsxy(1+depth*rb->font_get_width(
rb->font_get(FONT_UI),'_'),y,
articlebuf+linestart+depth+whitey);
articlebuf[renderoff-depth]=preserv;
}
linecount++;
y+=fontheight;
state=STATE_START;
break;
default:
/* return to HTEXT, might be an = within a heading */
state=STATE_HTEXT;
break;
}
break;
case STATE_HTEXT:
switch (articlebuf[renderoff])
{
case '=':
extdepth=1;
state=STATE_DD2;
break;
case MARKUP_LINEFEED:
state=STATE_START; /* give up */
break;
default:
/* continue to accept text within the heading */
break;
}
break;
case STATE_OTHERTEXT:
switch (articlebuf[renderoff])
{
case MARKUP_LINEFEED:
state=STATE_START;
break;
default:
break;
}
break;
} /* end switch (state) */
charsize = rb->utf8seek(articlebuf+renderoff,1);
renderlen-=charsize;
renderoff+=charsize;
}
if (!norender)
rb->lcd_update();
return nextlineoff;
}
static void render_history(int off)
{
int i=curhist,j,d;
int x,y,fontheight,t;
char buf[10];
rb->lcd_clear_display();
y=1;
fontheight = rb->font_get(FONT_UI)->height;
for (j=1,d=histdepth; d>0 && y+fontheight<LCD_HEIGHT; d--,j++)
{
if (j>=(off+1))
{
rb->snprintf(buf,10,"%2d. ",j);
rb->lcd_putsxy(1,y,buf);
rb->lcd_getstringsize(buf,&x,&t);
rb->lcd_putsxy(1+x,y,hist[i].name);
y+=fontheight;
}
i--;
if (i<0)
i=HISTORY_LENGTH-1;
}
rb->lcd_update();
}
/* read 2 character ascii hex */
static int readhex(unsigned char * buf)
{
int t=0;
if (buf[0]>='0'&&buf[0]<='9')
t=(buf[0]-'0')*16;
else if (buf[0]>='A'&&buf[0]<='F')
t=(buf[0]-'A'+10)*16;
else return -1;
if (buf[1]>='0'&&buf[1]<='9')
t+=(buf[1]-'0');
else if (buf[1]>='A'&&buf[1]<='F')
t+=(buf[1]-'A'+10);
else return -1;
return t;
}
static bool viewer_init(void)
{
enum {
MODE_LINK,
MODE_NAVVIEW,
MODE_HISTVIEW,
MODE_NORMAL,
MODE_BOOKVIEW
} mode;
int linkno; /* currently selected link */
int navcur; /* current offset for navigation */
int navnext; /* next offset for navigation mode */
int navline; /* current line for navigation (important for scrolling up)
*/
int histviewoff; /* how far back */
int bookviewoff; /* how far back */
int fontheight;
fontheight = rb->font_get(FONT_UI)->height;
char * target; /* an anchor to seek out */
bool prompt=1; /* prompt for article name? */
bool dofind=1; /* run mwdb_findarticle? */
curline=0; /* current line */
reset_history();
hist[curhist].curoff=0; /* first article starts at beginning */
/* allocate memory */
wpw_init_mempool(MAINMEMBUF);
inflatebuf=wpw_malloc(MAINMEMBUF,0x13500);
articlebuflen=wpw_available(MAINMEMBUF)>0x32000 ? 0x32000 : wpw_available(MAINMEMBUF);
articlebuf=wpw_malloc(MAINMEMBUF,articlebuflen);
/* initially no name in prompt, subsequently default to previous name */
rb->memset(hist[curhist].name,0,ARTICLENAMEBUF_LENGTH);
#ifdef HAVE_TOUCHSCREEN
rb->touchscreen_set_mode(TOUCHSCREEN_POINT);
#endif
if (load_status(filename))
{
prompt=0;
dofind=0;
}
if (load_bookmarks(filename)) /*UNSURE*/
{
}
target=0;
loadnewarticle:
if (prompt || dofind)
if (!mwdb_findarticle(filename,hist[curhist].name,ARTICLENAMEBUF_LENGTH,
&hist[curhist].res_lo,&hist[curhist].res_hi,true,
prompt,1))
{
/* try case-insensitive match if case-sensitive match fails */
if (!rb->strlen(hist[curhist].name))
{
/* bailed out */
/* automatically go back */
if (histdepth>1)
{
prompt=0;
dofind=0;
advance_history(-1);
goto loadnewarticle; /* few lines up */
}
return true;
}
if (!mwdb_findarticle(filename,hist[curhist].name,
ARTICLENAMEBUF_LENGTH,&hist[curhist].res_lo,
&hist[curhist].res_hi,true,0,0))
{
rb->splashf(HZ*2, "didn't find \"%s\"",hist[curhist].name);
/* automatically go back */
if (histdepth>1)
{
if (prompt==0)
{
prompt=0;
dofind=0;
advance_history(-1);
}
goto loadnewarticle; /* few lines up */
}
return true; /* No reason to make the plugin fail */
}
}
prompt=0;
if (!mwdb_loadarticle(filename,inflatebuf,articlebuf,articlebuflen,
&articlelen,hist[curhist].res_lo,hist[curhist].res_hi))
return false; /* load error is a problem */
rb->lcd_clear_display();
/* if a target has been specified */
if (target)
{
int i,j;
/* find headings */
navcur=-1;
navnext=0;
while (navcur!=navnext)
{
navcur=navnext;
navnext=render_outline(navcur,1);
/* navnext will be pointing at the start of the line with the target
in it
* Attempt match, this is a bit tricky (and it is probably equally
*tricky to get an anchor pointing to another document in wikicode
*in the first place).
*/
for (i=0,j=navnext; target[i] && articlebuf[j]; )
{
if (target[i]==articlebuf[j])
{
i++; j++;
}
else
{
if (i==0 && (articlebuf[j]=='=' || articlebuf[j]==' '))
j++; /* ignore starting =s, spaces */
else if (target[i]=='_' && articlebuf[j]==' ')
{
i++;
j++; /* spaces converted to underscores */
}
else if (target[i]=='.') /* check for escape sequences */
{
if (articlebuf[j]==readhex(target+i+1))
{
i+=3;
j++;
}
else break;
}
else break;
}
}
if (target[i])
continue;
while (articlebuf[j]==' ')
j++; /* sometimes we see spaces before the closing = */
if (articlebuf[j]!='=')
continue; /* we want the exact heading */
/* we have a winner! */
hist[curhist].curoff=navnext;
}
target=0;
}
set_article_offset(hist[curhist].curoff);
nextlinestate=render(curlinestate,0,0);
/* get authoritative title name */
rb->memcpy(hist[curhist].name,articlebuf+10,articlebuf[8]);
hist[curhist].name[(int)(articlebuf[8])]='\0';
mode=MODE_NORMAL;
linkno=0;
navcur=0;
navnext=0;
navline=0;
histviewoff=0;
bookviewoff=0;
int button;
while (!wrquit)
{
button = rb->button_get(true);
if (rb->default_event_handler_ex(button, viewer_exit, NULL)==
SYS_USB_CONNECTED)
return PLUGIN_USB_CONNECTED;
#ifdef HAVE_TOUCHSCREEN
if (button & BUTTON_TOUCHSCREEN)
{
static int prevY = 0;
short x, y;
int fontheight = rb->font_get(FONT_UI)->height;
x = rb->button_get_data() >> 16;
y = rb->button_get_data() & 0xFFFF;
if(button & BUTTON_REL)
prevY = 0;
else
{
if (prevY != 0)
{
if (mode == MODE_NORMAL)
{
int pageDelta = (y - prevY) / fontheight;
advance_scrollback(-pageDelta, 1);
}
}
prevY = y;
}
}
#endif
switch(button)
{
case WIKIVIEWER_MENU:
switch (mode)
{
case MODE_BOOKVIEW:
save_bookmark(filename);
case MODE_HISTVIEW:
case MODE_LINK:
case MODE_NAVVIEW:
rb->lcd_clear_display();
mode=MODE_NORMAL;
nextlinestate=render(curlinestate,0,0);
break;
case MODE_NORMAL:
{
/* wrquit=true; */
MENUITEM_STRINGLIST(main_menu, "Wikiviewer Menu", NULL,
"Save History",
"Navigate",
"Find Similar Article",
"Find Article",
"Clear History",
"View History",
"Show Playback Menu",
"Bookmarks",
"Add Bookmark",
"Exit");
int selection = 0;
int lasthist;
switch (rb->do_menu(&main_menu, &selection, NULL, false))
{
case MENU_ATTACHED_USB:
return PLUGIN_USB_CONNECTED;
case 0: /* save status */
save_status(filename);
break;
case 1: /* navigate */
mode=MODE_NAVVIEW;
navcur=0;
navnext=render_outline(navcur,0);
navline=0;
break;
case 2: /* find similar article */
lasthist=curhist;
prompt=1;
dofind=1;
advance_history(1);
rb->strcpy(hist[curhist].name,hist[lasthist].name);
hist[curhist].curoff=0;
goto loadnewarticle;
break;
case 3: /* find article (blank name) */
lasthist=curhist;
prompt=1;
dofind=1;
advance_history(1);
rb->memset(hist[curhist].name,0,ARTICLENAMEBUF_LENGTH);
hist[curhist].curoff=0;
goto loadnewarticle;
break;
case 4: /* clear hist */
reset_history();
rb->memset(hist[curhist].name,0,ARTICLENAMEBUF_LENGTH);
hist[curhist].curoff=0;
prompt=1;
dofind=1;
goto loadnewarticle;
break;
case 5: /* view hist */
mode=MODE_HISTVIEW;
histviewoff=0;
render_history(histviewoff);
break;
case 6: /* playback control */
playback_control(NULL);
break;
case 7: /* Bookmarks */
mode=MODE_BOOKVIEW;
render_bookmarks(0);
break;
case 8: /* Add Bookmark */
add_bookmark();
save_bookmark(filename);
break;
case 9: /* exit */
wrquit=true;
break;
} /* end menu switch */
if ((mode==MODE_NORMAL) && !wrquit)
{
rb->lcd_clear_display();
nextlinestate=render(curlinestate,0,0);
}
} /* end menu block */
} /* end mode switch */
break;
case WIKIVIEWER_SELECT:
switch (mode)
{
case MODE_NAVVIEW:
set_article_offset(navcur);
nextlinestate=render(curlinestate,0,0);
mode=MODE_NORMAL;
break;
case MODE_NORMAL:
/* enter link select mode */
mode=MODE_LINK;
linkno=1;
nextlinestate=render(curlinestate,0,linkno);
if (lastlinkcount==0) mode=MODE_NORMAL;
break;
case MODE_LINK:
/* load a new article */
advance_history(1);
readlink_wiki(curlinestate,linkno,hist[curhist].name,ARTICLENAMEBUF_LENGTH);
if ((target=rb->strrchr(hist[curhist].name,'#')))
{
/* cut the target name off the end of the string */
target[0]='\0';
target++; /* to point at the name of the target */
}
hist[curhist].curoff=0;
dofind=1;
goto loadnewarticle; /* ~line 1165 */
case MODE_HISTVIEW:
/* jump back */
if (histviewoff>0)
{
int i;
for (i=0; i<histviewoff; i++)
advance_history(-1);
dofind=0;
mode=MODE_NORMAL;
goto loadnewarticle; /* ~line 1165 */
}
nextlinestate=render(curlinestate,0,0);
mode=MODE_NORMAL;
break;
case MODE_BOOKVIEW:
if ((bookdepth-bookviewoff)<0)
rb->splash(HZ*2, "error");
else
{
save_bookmark(filename);
int booki;
booki=bookdepth-bookviewoff-1;
curhist++;
histdepth++;
rb->strcpy(hist[curhist].name,book[booki].name);
hist[curhist].curoff=book[booki].curoff;
hist[curhist].res_lo=book[booki].res_lo;
hist[curhist].res_hi=book[booki].res_hi;
dofind=0;
mode=MODE_NORMAL;
goto loadnewarticle; /* ~line 1165 */
}
nextlinestate=render(curlinestate,0,0);
mode=MODE_NORMAL;
break;
default:
break;
} /* end mode switch */
break;
#ifdef WIKIVIEWER_STOP_RECORD
case WIKIVIEWER_STOP_RECORD:
switch (mode)
{
case MODE_NORMAL: /*reset recording flag*/
record_stop_flag=true;
break;
default:
break;
} /* end mode switch */
break;
#endif
case WIKIVIEWER_FWD:
case WIKIVIEWER_FWD_REPEAT:
#ifdef WIKIVIEWER_FWD2
case WIKIVIEWER_FWD2:
case WIKIVIEWER_FWD2_REPEAT:
#endif
switch (mode)
{
case MODE_NAVVIEW:
if (navnext!=navcur)
navline++;
navcur=navnext;
navnext=render_outline(navcur,0);
break;
case MODE_LINK:
/* select next link */
if (linkno<lastlinkcount)
{
linkno++;
nextlinestate=render(curlinestate,0,linkno);
}
break;
case MODE_NORMAL:
#ifdef WIKIVIEWER_FWD2
if (button==WIKIVIEWER_FWD2)
{
/* enter link select mode */
mode=MODE_LINK;
linkno=1;
nextlinestate=render(curlinestate,0,linkno);
if (lastlinkcount==0)
mode=MODE_NORMAL;
break;
}
else
#endif
{ /* scroll down */
advance_scrollback(1,1);
}
break;
case MODE_HISTVIEW:
if (histviewoff<histdepth-1)
{
histviewoff++;
render_history(histviewoff);
}
break;
case MODE_BOOKVIEW:
if (bookviewoff<bookdepth-1)
{
bookviewoff++;
render_bookmarks(bookviewoff);
}
break;
} /* end mode switch */
break;
case WIKIVIEWER_BACK:
case WIKIVIEWER_BACK_REPEAT:
#ifdef WIKIVIEWER_BACK2
case WIKIVIEWER_BACK2:
case WIKIVIEWER_BACK2_REPEAT:
#endif
switch (mode)
{
case MODE_NAVVIEW:
{
int i;
if (navline>0)
{
navline--;
navnext=0;
for (i=0; i<navline; i++)
{
navcur=navnext;
navnext=render_outline(navcur,1);
}
navcur=navnext;
navnext=render_outline(navcur,0);
}
}
break;
case MODE_LINK:
/* select previous link */
if (linkno>1)
{
linkno--;
nextlinestate=render(curlinestate,0,linkno);
}
break;
case MODE_NORMAL:
#ifdef WIKIVIEWER_BACK2
if (button==WIKIVIEWER_BACK2) /*scroll up page*/
advance_scrollback((1-(LCD_HEIGHT/fontheight)),1);
else
#endif
{ /* scroll up */
advance_scrollback(-1,1);
}
break;
case MODE_HISTVIEW:
if (histviewoff>0)
{
histviewoff--;
render_history(histviewoff);
}
break;
case MODE_BOOKVIEW:
if (bookviewoff>0)
{
bookviewoff--;
render_bookmarks(bookviewoff);
}
break;
} /* end mode switch */
break;
case WIKIVIEWER_PREV: /* go back */
switch (mode)
{
case MODE_NORMAL:
if (histdepth>1)
{
advance_history(-1);
dofind=0;
goto loadnewarticle; /* ~line 1165 */
}
break;
case MODE_BOOKVIEW:
remove_bookmark(bookviewoff);
bookviewoff=0;
render_bookmarks(bookviewoff);
break;
default:
break;
} /* end mode switch */
break;
case WIKIVIEWER_NEXT: /* go forward */
switch (mode)
{
case MODE_NORMAL:
if (hist[(curhist+1)].curoff>0)
{
advance_history(1);
dofind=0;
bookviewoff=0;
goto loadnewarticle; /* ~line 1165 */
}
break;
case MODE_BOOKVIEW:
if (load_bookmarks(filename))
render_bookmarks(bookviewoff);
break;
default:
break;
} /* end mode switch */
break;
#ifdef WIKIVIEWER_RECORD
case WIKIVIEWER_RECORD: /*record line*/
case WIKIVIEWER_RECORD_REPEAT:
switch (mode)
{
case MODE_NORMAL:
record_flag=1;
advance_scrollback(1,1);
break;
case MODE_BOOKVIEW:
save_bookmark(filename);
break;
default:
break;
} /* end mode switch */
break;
#endif
default:
break;
} /* end button switch */
} /* end main loop */
return true;
}
static bool check_dir(char *folder)
{
DIR *dir = rb->opendir(folder);
if (!dir && rb->strcmp(folder, "/"))
{
int rc = rb->mkdir(folder);
if(rc < 0)
return false;
return true;
}
rb->closedir(dir);
return true;
}
enum plugin_status plugin_start(const void* file)
{
rb->backlight_set_timeout(0); /*Turn off backlight timeout*/
check_dir("/wiki");
if (!file)
return PLUGIN_ERROR;
rb->strlcpy(filename,file,sizeof(filename));
filename[rb->strlen(filename)-4]='\0';
rb->lcd_set_drawmode(DRMODE_SOLID);
if (!viewer_init())
return PLUGIN_ERROR;
viewer_exit(NULL);
rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
/*Turn on backlight timeout*/
return PLUGIN_OK;
}
|