summaryrefslogtreecommitdiffstats
path: root/apps/plugins/viewer.c
blob: 299c392177a1a510dad463bb89b5018ee1cf0aae (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 Gilles Roux, 2003 Garrett Derner
 *
 * 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"

PLUGIN_HEADER

#define SETTINGS_FILE   VIEWERS_DIR "/viewer.dat" /* binary file, so dont use .cfg */
#define BOOKMARKS_FILE  VIEWERS_DIR "/viewer_bookmarks.dat"

#define WRAP_TRIM          44  /* Max number of spaces to trim (arbitrary) */
#define MAX_COLUMNS        64  /* Max displayable string len (over-estimate) */
#define MAX_WIDTH         910  /* Max line length in WIDE mode */
#define READ_PREV_ZONE    910  /* Arbitrary number less than SMALL_BLOCK_SIZE */
#define SMALL_BLOCK_SIZE  0x1000 /* 4k: Smallest file chunk we will read */
#define LARGE_BLOCK_SIZE  0x2000 /* 8k: Preferable size of file chunk to read */
#define TOP_SECTOR     buffer
#define MID_SECTOR     (buffer + SMALL_BLOCK_SIZE)
#define BOTTOM_SECTOR  (buffer + 2*(SMALL_BLOCK_SIZE))
#undef SCROLLBAR_WIDTH
#define SCROLLBAR_WIDTH rb->global_settings->scrollbar_width

#define MAX_BOOKMARKED_FILES ((buffer_size/(signed)sizeof(struct bookmarked_file_info))-1)

/* Out-Of-Bounds test for any pointer to data in the buffer */
#define BUFFER_OOB(p)    ((p) < buffer || (p) >= buffer_end)

/* Does the buffer contain the beginning of the file? */
#define BUFFER_BOF()     (file_pos==0)

/* Does the buffer contain the end of the file? */
#define BUFFER_EOF()     (file_size-file_pos <= buffer_size)

/* Formula for the endpoint address outside of buffer data */
#define BUFFER_END() \
 ((BUFFER_EOF()) ? (file_size-file_pos+buffer) : (buffer+buffer_size))

/* Is the entire file being shown in one screen? */
#define ONE_SCREEN_FITS_ALL() \
 (next_screen_ptr==NULL && screen_top_ptr==buffer && BUFFER_BOF())

/* Is a scrollbar called for on the current screen? */
#define NEED_SCROLLBAR() \
 ((!(ONE_SCREEN_FITS_ALL())) && (prefs.scrollbar_mode==SB_ON))

/* variable button definitions */

/* Recorder keys */
#if CONFIG_KEYPAD == RECORDER_PAD
#define VIEWER_QUIT BUTTON_OFF
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_F1
#define VIEWER_AUTOSCROLL BUTTON_PLAY
#define VIEWER_LINE_UP (BUTTON_ON | BUTTON_UP)
#define VIEWER_LINE_DOWN (BUTTON_ON | BUTTON_DOWN)
#define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT)
#define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT)

#elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
#define VIEWER_QUIT BUTTON_OFF
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_F1
#define VIEWER_AUTOSCROLL BUTTON_SELECT
#define VIEWER_LINE_UP (BUTTON_ON | BUTTON_UP)
#define VIEWER_LINE_DOWN (BUTTON_ON | BUTTON_DOWN)
#define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT)
#define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT)

/* Ondio keys */
#elif CONFIG_KEYPAD == ONDIO_PAD
#define VIEWER_QUIT BUTTON_OFF
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU (BUTTON_MENU|BUTTON_REPEAT)
#define VIEWER_AUTOSCROLL_PRE BUTTON_MENU
#define VIEWER_AUTOSCROLL (BUTTON_MENU|BUTTON_REL)

/* Player keys */
#elif CONFIG_KEYPAD == PLAYER_PAD
#define VIEWER_QUIT BUTTON_STOP
#define VIEWER_PAGE_UP BUTTON_LEFT
#define VIEWER_PAGE_DOWN BUTTON_RIGHT
#define VIEWER_SCREEN_LEFT (BUTTON_ON|BUTTON_LEFT)
#define VIEWER_SCREEN_RIGHT (BUTTON_ON|BUTTON_RIGHT)
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_PLAY

/* iRiver H1x0 && H3x0 keys */
#elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
      (CONFIG_KEYPAD == IRIVER_H300_PAD)
#define VIEWER_QUIT BUTTON_OFF
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MODE
#define VIEWER_AUTOSCROLL BUTTON_SELECT
#define VIEWER_LINE_UP (BUTTON_ON | BUTTON_UP)
#define VIEWER_LINE_DOWN (BUTTON_ON | BUTTON_DOWN)
#define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT)
#define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT)

#define VIEWER_RC_QUIT BUTTON_RC_STOP

/* iPods */
#elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
      (CONFIG_KEYPAD == IPOD_3G_PAD) || \
      (CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define VIEWER_QUIT_PRE BUTTON_SELECT
#define VIEWER_QUIT (BUTTON_SELECT | BUTTON_MENU)
#define VIEWER_PAGE_UP BUTTON_SCROLL_BACK
#define VIEWER_PAGE_DOWN BUTTON_SCROLL_FWD
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_PLAY

/* iFP7xx keys */
#elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
#define VIEWER_QUIT BUTTON_PLAY
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MODE
#define VIEWER_AUTOSCROLL BUTTON_SELECT

/* iAudio X5 keys */
#elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_SELECT
#define VIEWER_AUTOSCROLL BUTTON_PLAY

/* GIGABEAT keys */
#elif CONFIG_KEYPAD == GIGABEAT_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_A

/* Sansa E200 keys */
#elif CONFIG_KEYPAD == SANSA_E200_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_SELECT
#define VIEWER_AUTOSCROLL BUTTON_REC
#define VIEWER_LINE_UP BUTTON_SCROLL_BACK
#define VIEWER_LINE_DOWN BUTTON_SCROLL_FWD

/* Sansa Fuze keys */
#elif CONFIG_KEYPAD == SANSA_FUZE_PAD
#define VIEWER_QUIT         (BUTTON_HOME|BUTTON_REPEAT)
#define VIEWER_PAGE_UP      BUTTON_UP
#define VIEWER_PAGE_DOWN    BUTTON_DOWN
#define VIEWER_SCREEN_LEFT  BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU         BUTTON_SELECT|BUTTON_REPEAT
#define VIEWER_AUTOSCROLL   BUTTON_SELECT|BUTTON_DOWN
#define VIEWER_LINE_UP      BUTTON_SCROLL_BACK
#define VIEWER_LINE_DOWN    BUTTON_SCROLL_FWD

/* Sansa C200 keys */
#elif CONFIG_KEYPAD == SANSA_C200_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_VOL_UP
#define VIEWER_PAGE_DOWN BUTTON_VOL_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_SELECT
#define VIEWER_AUTOSCROLL BUTTON_REC
#define VIEWER_LINE_UP BUTTON_UP
#define VIEWER_LINE_DOWN BUTTON_DOWN

/* Sansa Clip keys */
#elif CONFIG_KEYPAD == SANSA_CLIP_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_VOL_UP
#define VIEWER_PAGE_DOWN BUTTON_VOL_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_SELECT
#define VIEWER_AUTOSCROLL BUTTON_HOME
#define VIEWER_LINE_UP BUTTON_UP
#define VIEWER_LINE_DOWN BUTTON_DOWN

/* Sansa M200 keys */
#elif CONFIG_KEYPAD == SANSA_M200_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_VOL_UP
#define VIEWER_PAGE_DOWN BUTTON_VOL_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU  (BUTTON_SELECT | BUTTON_UP)
#define VIEWER_AUTOSCROLL (BUTTON_SELECT | BUTTON_REL)
#define VIEWER_LINE_UP BUTTON_UP
#define VIEWER_LINE_DOWN BUTTON_DOWN

/* iriver H10 keys */
#elif CONFIG_KEYPAD == IRIVER_H10_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_SCROLL_UP
#define VIEWER_PAGE_DOWN BUTTON_SCROLL_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_REW
#define VIEWER_AUTOSCROLL BUTTON_PLAY

/*M-Robe 500 keys */
#elif CONFIG_KEYPAD == MROBE500_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_RC_PLAY
#define VIEWER_PAGE_DOWN BUTTON_RC_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_RC_HEART
#define VIEWER_AUTOSCROLL BUTTON_RC_MODE

/*Gigabeat S keys */
#elif CONFIG_KEYPAD == GIGABEAT_S_PAD
#define VIEWER_QUIT BUTTON_BACK
#define VIEWER_PAGE_UP BUTTON_PREV
#define VIEWER_PAGE_DOWN BUTTON_NEXT
#define VIEWER_SCREEN_LEFT (BUTTON_PLAY | BUTTON_LEFT)
#define VIEWER_SCREEN_RIGHT (BUTTON_PLAY | BUTTON_RIGHT)
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL_PRE BUTTON_PLAY
#define VIEWER_AUTOSCROLL (BUTTON_PLAY|BUTTON_REL)
#define VIEWER_LINE_UP BUTTON_UP
#define VIEWER_LINE_DOWN BUTTON_DOWN
#define VIEWER_COLUMN_LEFT BUTTON_LEFT
#define VIEWER_COLUMN_RIGHT BUTTON_RIGHT

/*M-Robe 100 keys */
#elif CONFIG_KEYPAD == MROBE100_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_DISPLAY

/* iAUdio M3 keys */
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define VIEWER_QUIT BUTTON_RC_REC
#define VIEWER_PAGE_UP BUTTON_RC_VOL_UP
#define VIEWER_PAGE_DOWN BUTTON_RC_VOL_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_RC_REW
#define VIEWER_SCREEN_RIGHT BUTTON_RC_FF
#define VIEWER_MENU BUTTON_RC_MENU
#define VIEWER_AUTOSCROLL BUTTON_RC_MODE
#define VIEWER_RC_QUIT BUTTON_REC

/* Cowon D2 keys */
#elif CONFIG_KEYPAD == COWON_D2_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_MENU BUTTON_MENU

#elif CONFIG_KEYPAD == IAUDIO67_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_VOLUP
#define VIEWER_PAGE_DOWN BUTTON_VOLDOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_PLAY
#define VIEWER_RC_QUIT BUTTON_STOP

/* Creative Zen Vision:M keys */
#elif CONFIG_KEYPAD == CREATIVEZVM_PAD
#define VIEWER_QUIT BUTTON_BACK
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_SELECT

/* Philips HDD1630 keys */
#elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_VIEW

/* Philips SA9200 keys */
#elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_PAGE_UP BUTTON_UP
#define VIEWER_PAGE_DOWN BUTTON_DOWN
#define VIEWER_SCREEN_LEFT BUTTON_PREV
#define VIEWER_SCREEN_RIGHT BUTTON_NEXT
#define VIEWER_MENU BUTTON_MENU
#define VIEWER_AUTOSCROLL BUTTON_PLAY

/* Onda VX747 keys */
#elif CONFIG_KEYPAD == ONDAVX747_PAD
#define VIEWER_QUIT BUTTON_POWER
#define VIEWER_MENU BUTTON_MENU

/* Onda VX777 keys */
#elif CONFIG_KEYPAD == ONDAVX777_PAD
#define VIEWER_QUIT BUTTON_POWER

/* SAMSUNG YH-820 / YH-920 / YH-925 keys */
#elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
#define VIEWER_QUIT         BUTTON_REC
#define VIEWER_PAGE_UP      BUTTON_UP
#define VIEWER_PAGE_DOWN    BUTTON_DOWN
#define VIEWER_SCREEN_LEFT  BUTTON_LEFT
#define VIEWER_SCREEN_RIGHT BUTTON_RIGHT
#define VIEWER_MENU         BUTTON_PLAY
#define VIEWER_AUTOSCROLL   BUTTON_REW

/* Packard Bell Vibe 500 keys */
#elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
#define VIEWER_QUIT         BUTTON_REC
#define VIEWER_PAGE_UP      BUTTON_OK
#define VIEWER_PAGE_DOWN    BUTTON_CANCEL
#define VIEWER_LINE_UP      BUTTON_UP
#define VIEWER_LINE_DOWN    BUTTON_DOWN
#define VIEWER_SCREEN_LEFT  BUTTON_PREV
#define VIEWER_SCREEN_RIGHT BUTTON_NEXT
#define VIEWER_MENU         BUTTON_MENU
#define VIEWER_AUTOSCROLL   BUTTON_PLAY

#else
#error No keymap defined!
#endif

#ifdef HAVE_TOUCHSCREEN
#ifndef VIEWER_QUIT
#define VIEWER_QUIT         BUTTON_TOPLEFT
#endif
#ifndef VIEWER_PAGE_UP
#define VIEWER_PAGE_UP      BUTTON_TOPMIDDLE
#endif
#ifndef VIEWER_PAGE_DOWN
#define VIEWER_PAGE_DOWN    BUTTON_BOTTOMMIDDLE
#endif
#ifndef VIEWER_SCREEN_LEFT
#define VIEWER_SCREEN_LEFT  BUTTON_MIDLEFT
#endif
#ifndef VIEWER_SCREEN_RIGHT
#define VIEWER_SCREEN_RIGHT BUTTON_MIDRIGHT
#endif
#ifndef VIEWER_MENU
#define VIEWER_MENU         BUTTON_TOPRIGHT
#endif
#ifndef VIEWER_AUTOSCROLL
#define VIEWER_AUTOSCROLL   BUTTON_CENTER
#endif
#endif

/* stuff for the bookmarking */
struct bookmarked_file_info {
    long file_position;
    int  top_ptr_pos;
    char filename[MAX_PATH];
};

struct bookmark_file_data {
    signed int bookmarked_files_count;
    struct bookmarked_file_info bookmarks[];
};

struct preferences {
    enum {
        WRAP=0,
        CHOP,
    } word_mode;

    enum {
        NORMAL=0,
        JOIN,
        EXPAND,
        REFLOW, /* won't be set on charcell LCD, must be last */
    } line_mode;

    enum {
        NARROW=0,
        WIDE,
    } view_mode;

    enum codepages encoding;

#ifdef HAVE_LCD_BITMAP
    enum {
        SB_OFF=0,
        SB_ON,
    } scrollbar_mode;
    bool need_scrollbar;

    enum {
        NO_OVERLAP=0,
        OVERLAP,
    } page_mode;
#endif /* HAVE_LCD_BITMAP */

    enum {
        PAGE=0,
        LINE,
    } scroll_mode;

    int autoscroll_speed;
};

struct preferences prefs;
struct preferences old_prefs;

static unsigned char *buffer;
static long buffer_size;
static unsigned char line_break[] = {0,0x20,9,0xB,0xC,'-'};
static int display_columns; /* number of (pixel) columns on the display */
static int display_lines; /* number of lines on the display */
static int draw_columns; /* number of (pixel) columns available for text */
static int par_indent_spaces; /* number of spaces to indent first paragraph */
static int fd;
static const char *file_name;
static long file_size;
static long start_position; /* position in the file after the viewer is started */
static bool mac_text;
static long file_pos; /* Position of the top of the buffer in the file */
static unsigned char *buffer_end; /*Set to BUFFER_END() when file_pos changes*/
static int max_line_len;
static unsigned char *screen_top_ptr;
static unsigned char *next_screen_ptr;
static unsigned char *next_screen_to_draw_ptr;
static unsigned char *next_line_ptr;
#ifdef HAVE_LCD_BITMAP
static struct font *pf;
#endif


int glyph_width(int ch)
{
    if (ch == 0)
        ch = ' ';

#ifdef HAVE_LCD_BITMAP
    return rb->font_get_width(pf, ch);
#else
    return 1;
#endif
}

unsigned char* get_ucs(const unsigned char* str, unsigned short* ch)
{
    unsigned char utf8_tmp[6];
    int count;

    if (prefs.encoding == UTF_8)
        return (unsigned char*)rb->utf8decode(str, ch);

    count = BUFFER_OOB(str+2)? 1:2;
    rb->iso_decode(str, utf8_tmp, prefs.encoding, count);
    rb->utf8decode(utf8_tmp, ch);

#ifdef HAVE_LCD_BITMAP
    if (prefs.encoding >= SJIS && *str >= 0x80
        && !(prefs.encoding == SJIS && *str > 0xA0 && *str < 0xE0))
        return (unsigned char*)str+2;
    else
#endif
        return (unsigned char*)str+1;
}

bool done = false;
int col = 0;

#define ADVANCE_COUNTERS(c) { width += glyph_width(c); k++; }
#define LINE_IS_FULL ((k>=MAX_COLUMNS-1) ||( width >= draw_columns))
#define LINE_IS_NOT_FULL ((k<MAX_COLUMNS-1) &&( width < draw_columns))
static unsigned char* crop_at_width(const unsigned char* p)
{
    int k,width;
    unsigned short ch;
    const unsigned char *oldp = p;

    k=width=0;

    while (LINE_IS_NOT_FULL) {
        oldp = p;
        p = get_ucs(p, &ch);
        ADVANCE_COUNTERS(ch);
    }

    return (unsigned char*)oldp;
}

static unsigned char* find_first_feed(const unsigned char* p, int size)
{
    int i;

    for (i=0; i < size; i++)
        if (p[i] == 0)
            return (unsigned char*) p+i;

    return NULL;
}

static unsigned char* find_last_feed(const unsigned char* p, int size)
{
    int i;

    for (i=size-1; i>=0; i--)
        if (p[i] == 0)
            return (unsigned char*) p+i;

    return NULL;
}

static unsigned char* find_last_space(const unsigned char* p, int size)
{
    int i, j, k;

    k = (prefs.line_mode==JOIN) || (prefs.line_mode==REFLOW) ? 0:1;

    if (!BUFFER_OOB(&p[size]))
        for (j=k; j < ((int) sizeof(line_break)) - 1; j++)
            if (p[size] == line_break[j])
                return (unsigned char*) p+size;

    for (i=size-1; i>=0; i--)
        for (j=k; j < (int) sizeof(line_break); j++)
        {
            if (!((p[i] == '-') && (prefs.word_mode == WRAP)))
                if (p[i] == line_break[j])
                    return (unsigned char*) p+i;
        }

    return NULL;
}

static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_short)
{
    const unsigned char *next_line = NULL;
    int size, i, j, k, width, search_len, spaces, newlines;
    bool first_chars;
    unsigned char c;

    if (is_short != NULL)
        *is_short = true;

    if BUFFER_OOB(cur_line)
        return NULL;

    if (prefs.view_mode == WIDE) {
        search_len = MAX_WIDTH;
    }
    else {   /* prefs.view_mode == NARROW */
        search_len = crop_at_width(cur_line) - cur_line;
    }

    size = BUFFER_OOB(cur_line+search_len) ? buffer_end-cur_line : search_len;

    if ((prefs.line_mode == JOIN) || (prefs.line_mode == REFLOW)) {
        /* Need to scan ahead and possibly increase search_len and size,
         or possibly set next_line at second hard return in a row. */
        next_line = NULL;
        first_chars=true;
        for (j=k=width=spaces=newlines=0; ; j++) {
            if (BUFFER_OOB(cur_line+j))
                return NULL;
            if (LINE_IS_FULL) {
                size = search_len = j;
                break;
            }

            c = cur_line[j];
            switch (c) {
                case ' ':
                    if (prefs.line_mode == REFLOW) {
                        if (newlines > 0) {
                            size = j;
                            next_line = cur_line + size;
                            return (unsigned char*) next_line;
                        }
                        if (j==0) /* i=1 is intentional */
                            for (i=0; i<par_indent_spaces; i++)
                                ADVANCE_COUNTERS(' ');
                    }
                    if (!first_chars) spaces++;
                    break;

                case 0:
                    if (newlines > 0) {
                        size = j;
                        next_line = cur_line + size - spaces;
                        if (next_line != cur_line)
                            return (unsigned char*) next_line;
                        break;
                    }

                    newlines++;
                    size += spaces -1;
                    if (BUFFER_OOB(cur_line+size) || size > 2*search_len)
                        return NULL;
                    search_len = size;
                    spaces = first_chars? 0:1;
                    break;

                default:
                    if (prefs.line_mode==JOIN || newlines>0) {
                        while (spaces) {
                            spaces--;
                            ADVANCE_COUNTERS(' ');
                            if (LINE_IS_FULL) {
                                size = search_len = j;
                                break;
                            }
                        }
                        newlines=0;
                   } else if (spaces) {
                        /* REFLOW, multiple spaces between words: count only
                         * one. If more are needed, they will be added
                         * while drawing. */
                        search_len = size;
                        spaces=0;
                        ADVANCE_COUNTERS(' ');
                        if (LINE_IS_FULL) {
                            size = search_len = j;
                            break;
                        }
                    }
                    first_chars = false;
                    ADVANCE_COUNTERS(c);
                    break;
            }
        }
    }
    else {
        /* find first hard return */
        next_line = find_first_feed(cur_line, size);
    }

    if (next_line == NULL)
        if (size == search_len) {
            if (prefs.word_mode == WRAP)  /* Find last space */
                next_line = find_last_space(cur_line, size);

            if (next_line == NULL)
                next_line = crop_at_width(cur_line);
            else
                if (prefs.word_mode == WRAP)
                    for (i=0;
                    i<WRAP_TRIM && isspace(next_line[0]) && !BUFFER_OOB(next_line);
                    i++)
                        next_line++;
        }

    if (prefs.line_mode == EXPAND)
        if (!BUFFER_OOB(next_line))  /* Not Null & not out of bounds */
            if (next_line[0] == 0)
                if (next_line != cur_line)
                    return (unsigned char*) next_line;

    /* If next_line is pointing to a zero, increment it; i.e.,
     leave the terminator at the end of cur_line. If pointing
     to a hyphen, increment only if there is room to display
     the hyphen on current line (won't apply in WIDE mode,
     since it's guarenteed there won't be room). */
    if (!BUFFER_OOB(next_line))  /* Not Null & not out of bounds */
        if (next_line[0] == 0)/* ||
        (next_line[0] == '-' && next_line-cur_line < draw_columns)) */
            next_line++;

    if (BUFFER_OOB(next_line))
        return NULL;

    if (is_short)
        *is_short = false;

    return (unsigned char*) next_line;
}

static unsigned char* find_prev_line(const unsigned char* cur_line)
{
    const unsigned char *prev_line = NULL;
    const unsigned char *p;

    if BUFFER_OOB(cur_line)
        return NULL;

    /* To wrap consistently at the same places, we must
     start with a known hard return, then work downwards.
     We can either search backwards for a hard return,
     or simply start wrapping downwards from top of buffer.
       If current line is not near top of buffer, this is
     a file with long lines (paragraphs). We would need to
     read earlier sectors before we could decide how to
     properly wrap the lines above the current line, but
     it probably is not worth the disk access. Instead,
     start with top of buffer and wrap down from there.
     This may result in some lines wrapping at different
     points from where they wrap when scrolling down.
       If buffer is at top of file, start at top of buffer. */

    if ((prefs.line_mode == JOIN) || (prefs.line_mode == REFLOW))
        prev_line = p = NULL;
    else
        prev_line = p = find_last_feed(buffer, cur_line-buffer-1);
        /* Null means no line feeds in buffer above current line. */

    if (prev_line == NULL)
        if (BUFFER_BOF() || cur_line - buffer > READ_PREV_ZONE)
            prev_line = p = buffer;
        /* (else return NULL and read previous block) */

    /* Wrap downwards until too far, then use the one before. */
    while (p < cur_line && p != NULL) {
        prev_line = p;
        p = find_next_line(prev_line, NULL);
    }

    if (BUFFER_OOB(prev_line))
        return NULL;

    return (unsigned char*) prev_line;
}

static void fill_buffer(long pos, unsigned char* buf, unsigned size)
{
    /* Read from file and preprocess the data */
    /* To minimize disk access, always read on sector boundaries */
    unsigned numread, i;
    bool found_CR = false;

    rb->lseek(fd, pos, SEEK_SET);
    numread = rb->read(fd, buf, size);
    rb->button_clear_queue(); /* clear button queue */

    for(i = 0; i < numread; i++) {
        switch(buf[i]) {
            case '\r':
                if (mac_text) {
                    buf[i] = 0;
                }
                else {
                    buf[i] = ' ';
                    found_CR = true;
                }
                break;

            case '\n':
                buf[i] = 0;
                found_CR = false;
                break;

            case 0:  /* No break between case 0 and default, intentionally */
                buf[i] = ' ';
            default:
                if (found_CR) {
                    buf[i - 1] = 0;
                    found_CR = false;
                    mac_text = true;
                }
                break;
        }
    }
}

static int read_and_synch(int direction)
{
/* Read next (or prev) block, and reposition global pointers. */
/* direction: 1 for down (i.e., further into file), -1 for up */
    int move_size, move_vector, offset;
    unsigned char *fill_buf;

    if (direction == -1) /* up */ {
        move_size = SMALL_BLOCK_SIZE;
        offset = 0;
        fill_buf = TOP_SECTOR;
        rb->memcpy(BOTTOM_SECTOR, MID_SECTOR, SMALL_BLOCK_SIZE);
        rb->memcpy(MID_SECTOR, TOP_SECTOR, SMALL_BLOCK_SIZE);
    }
    else /* down */ {
        if (prefs.view_mode == WIDE) {
            /* WIDE mode needs more buffer so we have to read smaller blocks */
            move_size = SMALL_BLOCK_SIZE;
            offset = LARGE_BLOCK_SIZE;
            fill_buf = BOTTOM_SECTOR;
            rb->memcpy(TOP_SECTOR, MID_SECTOR, SMALL_BLOCK_SIZE);
            rb->memcpy(MID_SECTOR, BOTTOM_SECTOR, SMALL_BLOCK_SIZE);
        }
        else {
            move_size = LARGE_BLOCK_SIZE;
            offset = SMALL_BLOCK_SIZE;
            fill_buf = MID_SECTOR;
            rb->memcpy(TOP_SECTOR, BOTTOM_SECTOR, SMALL_BLOCK_SIZE);
        }
    }
    move_vector = direction * move_size;
    screen_top_ptr -= move_vector;
    file_pos += move_vector;
    buffer_end = BUFFER_END();  /* Update whenever file_pos changes */
    fill_buffer(file_pos + offset, fill_buf, move_size);
    return move_vector;
}

static void viewer_scroll_up(void)
{
    unsigned char *p;

    p = find_prev_line(screen_top_ptr);
    if (p == NULL && !BUFFER_BOF()) {
        read_and_synch(-1);
        p = find_prev_line(screen_top_ptr);
    }
    if (p != NULL)
        screen_top_ptr = p;
}

static void viewer_scroll_down(void)
{
    if (next_screen_ptr != NULL)
        screen_top_ptr = next_line_ptr;
}

#ifdef HAVE_LCD_BITMAP
static void viewer_scrollbar(void) {
    int items, min_shown, max_shown;

    items = (int) file_size;  /* (SH1 int is same as long) */
    min_shown = (int) file_pos + (screen_top_ptr - buffer);

    if (next_screen_ptr == NULL)
        max_shown = items;
    else
        max_shown = min_shown + (next_screen_ptr - screen_top_ptr);

    rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],0, 0, SCROLLBAR_WIDTH-1,
                         LCD_HEIGHT, items, min_shown, max_shown, VERTICAL);
}
#endif

static void viewer_draw(int col)
{
    int i, j, k, line_len, line_width, resynch_move, spaces, left_col=0;
    int width, extra_spaces, indent_spaces, spaces_per_word;
    bool multiple_spacing, line_is_short;
    unsigned short ch;
    unsigned char *str, *oldstr;
    unsigned char *line_begin;
    unsigned char *line_end;
    unsigned char c;
    unsigned char scratch_buffer[MAX_COLUMNS + 1];
    unsigned char utf8_buffer[MAX_COLUMNS*4 + 1];
    unsigned char *endptr;

    /* If col==-1 do all calculations but don't display */
    if (col != -1) {
#ifdef HAVE_LCD_BITMAP
        left_col = prefs.need_scrollbar? SCROLLBAR_WIDTH:0;
#else
        left_col = 0;
#endif
        rb->lcd_clear_display();
    }
    max_line_len = 0;
    line_begin = line_end = screen_top_ptr;

    for (i = 0; i < display_lines; i++) {
        if (BUFFER_OOB(line_end))
            break;  /* Happens after display last line at BUFFER_EOF() */

        line_begin = line_end;
        line_end = find_next_line(line_begin, &line_is_short);

        if (line_end == NULL) {
            if (BUFFER_EOF()) {
                if (i < display_lines - 1 && !BUFFER_BOF()) {
                    if (col != -1)
                        rb->lcd_clear_display();

                    for (; i < display_lines - 1; i++)
                        viewer_scroll_up();

                    line_begin = line_end = screen_top_ptr;
                    i = -1;
                    continue;
                }
                else {
                    line_end = buffer_end;
                }
            }
            else {
                resynch_move = read_and_synch(1); /* Read block & move ptrs */
                line_begin -= resynch_move;
                if (i > 0)
                    next_line_ptr -= resynch_move;

                line_end = find_next_line(line_begin, NULL);
                if (line_end == NULL)  /* Should not really happen */
                    break;
            }
        }
        line_len = line_end - line_begin;

        /* calculate line_len */
        str = oldstr = line_begin;
        j = -1;
        while (str < line_end) {
            oldstr = str;
            str = crop_at_width(str);
            j++;
        }
        line_width = j*draw_columns;
        while (oldstr < line_end) {
            oldstr = get_ucs(oldstr, &ch);
            line_width += glyph_width(ch);
        }

        if (prefs.line_mode == JOIN) {
            if (line_begin[0] == 0) {
                line_begin++;
                if (prefs.word_mode == CHOP)
                    line_end++;
                else
                    line_len--;
            }
            for (j=k=spaces=0; j < line_len; j++) {
                if (k == MAX_COLUMNS)
                    break;

                c = line_begin[j];
                switch (c) {
                    case ' ':
                        spaces++;
                        break;
                    case 0:
                        spaces = 0;
                        scratch_buffer[k++] = ' ';
                        break;
                    default:
                        while (spaces) {
                            spaces--;
                            scratch_buffer[k++] = ' ';
                            if (k == MAX_COLUMNS - 1)
                                break;
                        }
                        scratch_buffer[k++] = c;
                        break;
                }
            }
            if (col != -1) {
                scratch_buffer[k] = 0;
                endptr = rb->iso_decode(scratch_buffer + col, utf8_buffer,
                                        prefs.encoding, draw_columns/glyph_width('i'));
                *endptr = 0;
            }
        }
        else if (prefs.line_mode == REFLOW) {
            if (line_begin[0] == 0) {
                line_begin++;
                if (prefs.word_mode == CHOP)
                    line_end++;
                else
                    line_len--;
            }

            indent_spaces = 0;
            if (!line_is_short) {
                multiple_spacing = false;
                width=spaces=0;
                for (str = line_begin; str < line_end; ) {
                    str = get_ucs(str, &ch);
                    switch (ch) {
                        case ' ':
                        case 0:
                            if ((str == line_begin) && (prefs.word_mode==WRAP))
                                /* special case: indent the paragraph,
                                 * don't count spaces */
                                indent_spaces = par_indent_spaces;
                            else if (!multiple_spacing)
                                spaces++;
                            multiple_spacing = true;
                            break;
                        default:
                            multiple_spacing = false;
                            width += glyph_width(ch);
                            break;
                    }
                }
                if (multiple_spacing) spaces--;

                if (spaces) {
                    /* total number of spaces to insert between words */
                    extra_spaces = (draw_columns-width)/glyph_width(' ')
                            - indent_spaces;
                    /* number of spaces between each word*/
                    spaces_per_word = extra_spaces / spaces;
                    /* number of words with n+1 spaces (to fill up) */
                    extra_spaces = extra_spaces % spaces;
                    if (spaces_per_word > 2) { /* too much spacing is awful */
                        spaces_per_word = 3;
                        extra_spaces = 0;
                    }
                } else { /* this doesn't matter much... no spaces anyway */
                    spaces_per_word = extra_spaces = 0;
                }
            } else { /* end of a paragraph: don't fill line */
                spaces_per_word = 1;
                extra_spaces = 0;
            }

            multiple_spacing = false;
            for (j=k=spaces=0; j < line_len; j++) {
                if (k == MAX_COLUMNS)
                    break;

                c = line_begin[j];
                switch (c) {
                    case ' ':
                    case 0:
                        if (j==0 && prefs.word_mode==WRAP) { /* indent paragraph */
                            for (j=0; j<par_indent_spaces; j++)
                                scratch_buffer[k++] = ' ';
                            j=0;
                        }
                        else if (!multiple_spacing) {
                            for (width = spaces<extra_spaces ? -1:0; width < spaces_per_word; width++)
                                    scratch_buffer[k++] = ' ';
                            spaces++;
                        }
                        multiple_spacing = true;
                        break;
                    default:
                        scratch_buffer[k++] = c;
                        multiple_spacing = false;
                        break;
                }
            }

            if (col != -1) {
                scratch_buffer[k] = 0;
                endptr = rb->iso_decode(scratch_buffer + col, utf8_buffer,
                                        prefs.encoding, k-col);
                *endptr = 0;
            }
        }
        else { /* prefs.line_mode != JOIN && prefs.line_mode != REFLOW */
            if (col != -1)
                if (line_width > col) {
                    str = oldstr = line_begin;
                    k = col;
                    width = 0;
                    while( (width<draw_columns) && (oldstr<line_end) )
                    {
                        oldstr = get_ucs(oldstr, &ch);
                        if (k > 0) {
                            k -= glyph_width(ch);
                            line_begin = oldstr;
                        } else {
                            width += glyph_width(ch);
                        }
                    }

                    if(prefs.view_mode==WIDE)
                        endptr = rb->iso_decode(line_begin, utf8_buffer,
                                            prefs.encoding, oldstr-line_begin);
                    else
                        endptr = rb->iso_decode(line_begin, utf8_buffer,
                                            prefs.encoding, line_end-line_begin);
                    *endptr = 0;
                }
        }
        if (col != -1 && line_width > col)
#ifdef HAVE_LCD_BITMAP
            rb->lcd_putsxy(left_col, i*pf->height, utf8_buffer);
#else
            rb->lcd_puts(left_col, i, utf8_buffer);
#endif
        if (line_width > max_line_len)
            max_line_len = line_width;

        if (i == 0)
            next_line_ptr = line_end;
    }
    next_screen_ptr = line_end;
    if (BUFFER_OOB(next_screen_ptr))
        next_screen_ptr = NULL;

#ifdef HAVE_LCD_BITMAP
    next_screen_to_draw_ptr = prefs.page_mode==OVERLAP? line_begin: next_screen_ptr;

    if (prefs.need_scrollbar)
        viewer_scrollbar();
#else
    next_screen_to_draw_ptr = next_screen_ptr;
#endif

    if (col != -1)
        rb->lcd_update();
}

static void viewer_top(void)
{
    /* Read top of file into buffer
      and point screen pointer to top */
    if (file_pos != 0)
    {
        file_pos = 0;
        buffer_end = BUFFER_END();  /* Update whenever file_pos changes */
        fill_buffer(0, buffer, buffer_size);
    }

    screen_top_ptr = buffer;
}

static void viewer_bottom(void)
{
    /* Read bottom of file into buffer
      and point screen pointer to bottom */
    long last_sectors;

    if (file_size > buffer_size) {
        /* Find last buffer in file, round up to next sector boundary */
        last_sectors = file_size - buffer_size + SMALL_BLOCK_SIZE;
        last_sectors /= SMALL_BLOCK_SIZE;
        last_sectors *= SMALL_BLOCK_SIZE;
    }
    else {
        last_sectors = 0;
    }

    if (file_pos != last_sectors)
    {
        file_pos = last_sectors;
        buffer_end = BUFFER_END();  /* Update whenever file_pos changes */
        fill_buffer(last_sectors, buffer, buffer_size);
    }

    screen_top_ptr = buffer_end-1;
}

#ifdef HAVE_LCD_BITMAP
static void init_need_scrollbar(void) {
    /* Call viewer_draw in quiet mode to initialize next_screen_ptr,
     and thus ONE_SCREEN_FITS_ALL(), and thus NEED_SCROLLBAR() */
    viewer_draw(-1);
    prefs.need_scrollbar = NEED_SCROLLBAR();
    draw_columns = prefs.need_scrollbar? display_columns-SCROLLBAR_WIDTH : display_columns;
    par_indent_spaces = draw_columns/(5*glyph_width(' '));
}
#else
#define init_need_scrollbar()
#endif

static bool viewer_init(void)
{
#ifdef HAVE_LCD_BITMAP

    pf = rb->font_get(FONT_UI);

    display_lines = LCD_HEIGHT / pf->height;
    draw_columns = display_columns = LCD_WIDTH;
#else
    /* REAL fixed pitch :) all chars use up 1 cell */
    display_lines = 2;
    draw_columns = display_columns = 11;
    par_indent_spaces = 2;
#endif

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

    file_size = rb->filesize(fd);
    if (file_size==-1)
        return false;

    /* Init mac_text value used in processing buffer */
    mac_text = false;

    return true;
}

static void viewer_default_settings(void)
{
    prefs.word_mode = WRAP;
    prefs.line_mode = NORMAL;
    prefs.view_mode = NARROW;
    prefs.scroll_mode = PAGE;
#ifdef HAVE_LCD_BITMAP
    prefs.page_mode = NO_OVERLAP;
    prefs.scrollbar_mode = SB_OFF;
#endif
    prefs.autoscroll_speed = 1;
    /* Set codepage to system default */
    prefs.encoding = rb->global_settings->default_codepage;
}

static void viewer_load_settings(void) /* same name as global, but not the same file.. */
{
    int settings_fd, i;
    struct bookmark_file_data *data;
    struct bookmarked_file_info this_bookmark;

    /* read settings file */
    settings_fd=rb->open(SETTINGS_FILE, O_RDONLY);
    if ((settings_fd >= 0) && (rb->filesize(settings_fd) == sizeof(struct preferences)))
    {
        rb->read(settings_fd, &prefs, sizeof(struct preferences));
        rb->close(settings_fd);
    }
    else
    {
        /* load default settings if there is no settings file */
        viewer_default_settings();
    }

    rb->memcpy(&old_prefs, &prefs, sizeof(struct preferences));

    data = (struct bookmark_file_data*)buffer; /* grab the text buffer */
    data->bookmarked_files_count = 0;

    /* read bookmarks if file exists */
    settings_fd = rb->open(BOOKMARKS_FILE, O_RDONLY);
    if (settings_fd >= 0)
    {
        /* figure out how many items to read */
        rb->read(settings_fd, &data->bookmarked_files_count, sizeof(signed int)); 
        if (data->bookmarked_files_count > MAX_BOOKMARKED_FILES)
            data->bookmarked_files_count = MAX_BOOKMARKED_FILES;
        rb->read(settings_fd, data->bookmarks, 
                 sizeof(struct bookmarked_file_info) * data->bookmarked_files_count);
        rb->close(settings_fd);
    }

    file_pos = 0;
    screen_top_ptr = buffer;

    /* check if current file is in list */
    for (i=0; i < data->bookmarked_files_count; i++)
    {
        if (!rb->strcmp(file_name, data->bookmarks[i].filename)) 
        {
            int screen_pos = data->bookmarks[i].file_position + data->bookmarks[i].top_ptr_pos;
            int screen_top = screen_pos % buffer_size;
            file_pos = screen_pos - screen_top;
            screen_top_ptr = buffer + screen_top;
            break;
        }
    }

    this_bookmark.file_position = file_pos;
    this_bookmark.top_ptr_pos = screen_top_ptr - buffer;

    rb->memset(&this_bookmark.filename[0],0,MAX_PATH);
    rb->strcpy(this_bookmark.filename,file_name);

    /* prevent potential slot overflow */
    if (i >= data->bookmarked_files_count) 
    {
        if (i < MAX_BOOKMARKED_FILES) 
            data->bookmarked_files_count++;
        else
            i = MAX_BOOKMARKED_FILES-1;
    }

    /* write bookmark file with spare slot in first position 
       to be filled in by viewer_save_settings */
    settings_fd = rb->open(BOOKMARKS_FILE, O_WRONLY|O_CREAT);
    if (settings_fd >=0 )
    {
        /* write count */
        rb->write (settings_fd, &data->bookmarked_files_count, sizeof(signed int));

        /* write the current bookmark */
        rb->write (settings_fd, &this_bookmark, sizeof(struct bookmarked_file_info));

        /* write everything that was before this bookmark */
        rb->write (settings_fd, data->bookmarks, sizeof(struct bookmarked_file_info)*i);

        rb->close(settings_fd);
    }

    buffer_end = BUFFER_END();  /* Update whenever file_pos changes */

    if (BUFFER_OOB(screen_top_ptr)) 
    {
        screen_top_ptr = buffer;
    }

    fill_buffer(file_pos, buffer, buffer_size);

    /* remember the current position */
    start_position = file_pos + screen_top_ptr - buffer;

    init_need_scrollbar();
}

static void viewer_save_settings(void)/* same name as global, but not the same file.. */
{
    int settings_fd;

    /* save the viewer settings if they have been changed */
    if (rb->memcmp(&prefs, &old_prefs, sizeof(struct preferences)))
    {
        settings_fd = rb->creat(SETTINGS_FILE); /* create the settings file */

        if (settings_fd >= 0 )
        {
            rb->write (settings_fd, &prefs, sizeof(struct preferences));
            rb->close(settings_fd);
        }
    }

    /* save the bookmark if the position has changed */
    if (file_pos + screen_top_ptr - buffer != start_position)
    {
        settings_fd = rb->open(BOOKMARKS_FILE, O_WRONLY|O_CREAT);

        if (settings_fd >= 0 )
        {
            struct bookmarked_file_info b;
            b.file_position = file_pos + screen_top_ptr - buffer;
            b.top_ptr_pos = 0; /* this is only kept for legassy reasons */
            rb->memset(&b.filename[0],0,MAX_PATH);
            rb->strcpy(b.filename,file_name);
            rb->lseek(settings_fd,sizeof(signed int),SEEK_SET);
            rb->write (settings_fd, &b, sizeof(struct bookmarked_file_info));
            rb->close(settings_fd);
        }
    }
}

static void viewer_exit(void *parameter)
{
    (void)parameter;

    viewer_save_settings();
    rb->close(fd);
}

static int col_limit(int col)
{
    if (col < 0)
        col = 0;
    else
        if (col > max_line_len - 2*glyph_width('o'))
            col = max_line_len - 2*glyph_width('o');

    return col;
}

/* settings helper functions */

static bool encoding_setting(void)
{
    static struct opt_items names[NUM_CODEPAGES];
    int idx;

    for (idx = 0; idx < NUM_CODEPAGES; idx++)
    {
        names[idx].string = rb->get_codepage_name(idx);
        names[idx].voice_id = -1;
    }

    return rb->set_option("Encoding", &prefs.encoding, INT, names,
                          sizeof(names) / sizeof(names[0]), NULL);
}

static bool word_wrap_setting(void)
{
    static const struct opt_items names[] = {
        {"On",               -1},
        {"Off (Chop Words)", -1},
    };

    return rb->set_option("Word Wrap", &prefs.word_mode, INT,
                          names, 2, NULL);
}

static bool line_mode_setting(void)
{
    static const struct opt_items names[] = {
        {"Normal",       -1},
        {"Join Lines",   -1},
        {"Expand Lines", -1},
#ifdef HAVE_LCD_BITMAP
        {"Reflow Lines", -1},
#endif
    };

    return rb->set_option("Line Mode", &prefs.line_mode, INT, names,
                          sizeof(names) / sizeof(names[0]), NULL);
}

static bool view_mode_setting(void)
{
    static const struct opt_items names[] = {
        {"No (Narrow)", -1},
        {"Yes",         -1},
    };
    bool ret;
    ret = rb->set_option("Wide View", &prefs.view_mode, INT,
                           names , 2, NULL);
    if (prefs.view_mode == NARROW)
        col = 0;
    return ret;
}

static bool scroll_mode_setting(void)
{
    static const struct opt_items names[] = {
        {"Scroll by Page", -1},
        {"Scroll by Line", -1},
    };

    return rb->set_option("Scroll Mode", &prefs.scroll_mode, INT,
                          names, 2, NULL);
}

#ifdef HAVE_LCD_BITMAP
static bool page_mode_setting(void)
{
    static const struct opt_items names[] = {
        {"No",  -1},
        {"Yes", -1},
    };

    return rb->set_option("Overlap Pages", &prefs.page_mode, INT,
                           names, 2, NULL);
}

static bool scrollbar_setting(void)
{
    static const struct opt_items names[] = {
        {"Off", -1},
        {"On",  -1}
    };

    return rb->set_option("Show Scrollbar", &prefs.scrollbar_mode, INT,
                           names, 2, NULL);
}
#endif

static bool autoscroll_speed_setting(void)
{
    return rb->set_int("Auto-scroll Speed", "", UNIT_INT, 
                       &prefs.autoscroll_speed, NULL, 1, 1, 10, NULL);
}

MENUITEM_FUNCTION(encoding_item, 0, "Encoding", encoding_setting,
                  NULL, NULL, Icon_NOICON);
MENUITEM_FUNCTION(word_wrap_item, 0, "Word Wrap", word_wrap_setting,
                  NULL, NULL, Icon_NOICON);
MENUITEM_FUNCTION(line_mode_item, 0, "Line Mode", line_mode_setting,
                  NULL, NULL, Icon_NOICON);
MENUITEM_FUNCTION(view_mode_item, 0, "Wide View", view_mode_setting,
                  NULL, NULL, Icon_NOICON);
#ifdef HAVE_LCD_BITMAP
MENUITEM_FUNCTION(scrollbar_item, 0, "Show Scrollbar", scrollbar_setting,
                  NULL, NULL, Icon_NOICON);
MENUITEM_FUNCTION(page_mode_item, 0, "Overlap Pages", page_mode_setting,
                  NULL, NULL, Icon_NOICON);
#endif
MENUITEM_FUNCTION(scroll_mode_item, 0, "Scroll Mode", scroll_mode_setting,
                  NULL, NULL, Icon_NOICON);
MENUITEM_FUNCTION(autoscroll_speed_item, 0, "Auto-Scroll Speed",
                  autoscroll_speed_setting, NULL, NULL, Icon_NOICON);
MAKE_MENU(option_menu, "Viewer Options", NULL, Icon_NOICON,
            &encoding_item, &word_wrap_item, &line_mode_item, &view_mode_item,
#ifdef HAVE_LCD_BITMAP
            &scrollbar_item, &page_mode_item,
#endif
            &scroll_mode_item, &autoscroll_speed_item);

static bool viewer_options_menu(void)
{
    bool result;
    result = (rb->do_menu(&option_menu, NULL, NULL, false) == MENU_ATTACHED_USB);

#ifdef HAVE_LCD_BITMAP
    /* Show-scrollbar mode for current view-width mode */
    init_need_scrollbar();
#endif
    return result;
}

static void viewer_menu(void)
{
    int result;
    MENUITEM_STRINGLIST(menu, "Viewer Menu", NULL,
                        "Return", "Viewer Options",
                        "Show Playback Menu", "Quit");

    result = rb->do_menu(&menu, NULL, NULL, false);
    switch (result)
    {
        case 0: /* return */
            break;
        case 1: /* change settings */
            done = viewer_options_menu();
            break;
        case 2: /* playback control */
            playback_control(NULL);
            break;
        case 3: /* quit */
            viewer_exit(NULL);
            done = true;
            break;
    }
    viewer_draw(col);
}

enum plugin_status plugin_start(const void* file)
{
    int button, i, ok;
    int lastbutton = BUTTON_NONE;
    bool autoscroll = false;
    long old_tick;

    old_tick = *rb->current_tick;

    /* get the plugin buffer */
    buffer = rb->plugin_get_buffer((size_t *)&buffer_size);

    if (!file)
        return PLUGIN_ERROR;

    file_name = file;
    ok = viewer_init();
    if (!ok) {
        rb->splash(HZ, "Error opening file.");
        return PLUGIN_ERROR;
    }

    viewer_load_settings(); /* load the preferences and bookmark */

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif

    viewer_draw(col);

    while (!done) {

        if(autoscroll)
        {
            if(old_tick <= *rb->current_tick - (110-prefs.autoscroll_speed*10))
            {
                viewer_scroll_down();
                viewer_draw(col);
                old_tick = *rb->current_tick;
            }
        }

        button = rb->button_get_w_tmo(HZ/10);
        switch (button) {
            case VIEWER_MENU:
                viewer_menu();
                break;

            case VIEWER_AUTOSCROLL:
#ifdef VIEWER_AUTOSCROLL_PRE
                if (lastbutton != VIEWER_AUTOSCROLL_PRE)
                    break;
#endif
                autoscroll = !autoscroll;
                break;

            case VIEWER_PAGE_UP:
            case VIEWER_PAGE_UP | BUTTON_REPEAT:
                if (prefs.scroll_mode == PAGE)
                {
                    /* Page up */
#ifdef HAVE_LCD_BITMAP
                    for (i = prefs.page_mode==OVERLAP? 1:0; i < display_lines; i++)
#else
                    for (i = 0; i < display_lines; i++)
#endif
                        viewer_scroll_up();
                }
                else
                    viewer_scroll_up();
                old_tick = *rb->current_tick;
                viewer_draw(col);
                break;

            case VIEWER_PAGE_DOWN:
            case VIEWER_PAGE_DOWN | BUTTON_REPEAT:
                if (prefs.scroll_mode == PAGE)
                {
                    /* Page down */
                    if (next_screen_ptr != NULL)
                        screen_top_ptr = next_screen_to_draw_ptr;
                }
                else
                    viewer_scroll_down();
                old_tick = *rb->current_tick;
                viewer_draw(col);
                break;

            case VIEWER_SCREEN_LEFT:
            case VIEWER_SCREEN_LEFT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE) {
                    /* Screen left */
                    col -= draw_columns;
                    col = col_limit(col);
                }
                else {   /* prefs.view_mode == NARROW */
                    /* Top of file */
                    viewer_top();
                }

                viewer_draw(col);
                break;

            case VIEWER_SCREEN_RIGHT:
            case VIEWER_SCREEN_RIGHT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE) {
                    /* Screen right */
                    col += draw_columns;
                    col = col_limit(col);
                }
                else {   /* prefs.view_mode == NARROW */
                    /* Bottom of file */
                    viewer_bottom();
                }

                viewer_draw(col);
                break;

#ifdef VIEWER_LINE_UP
            case VIEWER_LINE_UP:
            case VIEWER_LINE_UP | BUTTON_REPEAT:
                /* Scroll up one line */
                viewer_scroll_up();
                old_tick = *rb->current_tick;
                viewer_draw(col);
                break;

            case VIEWER_LINE_DOWN:
            case VIEWER_LINE_DOWN | BUTTON_REPEAT:
                /* Scroll down one line */
                if (next_screen_ptr != NULL)
                    screen_top_ptr = next_line_ptr;
                old_tick = *rb->current_tick;
                viewer_draw(col);
                break;
#endif
#ifdef VIEWER_COLUMN_LEFT
            case VIEWER_COLUMN_LEFT:
            case VIEWER_COLUMN_LEFT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE) {
                    /* Scroll left one column */
                    col -= glyph_width('o');
                    col = col_limit(col);
                    viewer_draw(col);
                }
                break;

            case VIEWER_COLUMN_RIGHT:
            case VIEWER_COLUMN_RIGHT | BUTTON_REPEAT:
                if (prefs.view_mode == WIDE) {
                    /* Scroll right one column */
                    col += glyph_width('o');
                    col = col_limit(col);
                    viewer_draw(col);
                }
                break;
#endif

#ifdef VIEWER_RC_QUIT
            case VIEWER_RC_QUIT:
#endif
            case VIEWER_QUIT:
                viewer_exit(NULL);
                done = true;
                break;

            default:
                if (rb->default_event_handler_ex(button, viewer_exit, NULL)
                    == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
                break;
        }
        if (button != BUTTON_NONE)
        {
            lastbutton = button;
            rb->yield();
        }
    }
    return PLUGIN_OK;
}