Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * |
| 3 | * __________ __ ___. |
| 4 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 5 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 6 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 7 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 8 | * \/ \/ \/ \/ \/ |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 9 | * |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2002 Gilles Roux, 2003 Garrett Derner |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 12 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License |
| 15 | * as published by the Free Software Foundation; either version 2 |
| 16 | * of the License, or (at your option) any later version. |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ****************************************************************************/ |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 22 | #include "plugin.h" |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 23 | #include <ctype.h> |
Hristo Kovachev | e16339e | 2006-04-04 12:04:15 +0000 | [diff] [blame] | 24 | #include "playback_control.h" |
Jonathan Gordon | 77a458a | 2007-05-08 11:55:43 +0000 | [diff] [blame] | 25 | #include "oldmenuapi.h" |
Robert Hak | fe18ca5 | 2003-07-24 12:22:10 +0000 | [diff] [blame] | 26 | |
Jens Arnold | a36b1d4 | 2006-01-15 18:20:18 +0000 | [diff] [blame] | 27 | PLUGIN_HEADER |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 28 | |
Daniel Stenberg | c9ee052 | 2007-08-21 21:15:32 +0000 | [diff] [blame] | 29 | #define SETTINGS_FILE VIEWERS_DIR "/viewer.dat" /* binary file, so dont use .cfg */ |
| 30 | #define BOOKMARKS_FILE VIEWERS_DIR "/viewer_bookmarks.dat" |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 31 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 32 | #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ |
| 33 | #define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ |
| 34 | #define MAX_WIDTH 910 /* Max line length in WIDE mode */ |
| 35 | #define READ_PREV_ZONE 910 /* Arbitrary number less than SMALL_BLOCK_SIZE */ |
| 36 | #define SMALL_BLOCK_SIZE 0x1000 /* 4k: Smallest file chunk we will read */ |
| 37 | #define LARGE_BLOCK_SIZE 0x2000 /* 8k: Preferable size of file chunk to read */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 38 | #define TOP_SECTOR buffer |
| 39 | #define MID_SECTOR (buffer + SMALL_BLOCK_SIZE) |
| 40 | #define BOTTOM_SECTOR (buffer + 2*(SMALL_BLOCK_SIZE)) |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 41 | #define SCROLLBAR_WIDTH 6 |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 42 | |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 43 | #define MAX_BOOKMARKED_FILES ((buffer_size/(signed)sizeof(struct bookmarked_file_info))-1) |
Jonathan Gordon | c4c0583 | 2006-09-17 10:32:49 +0000 | [diff] [blame] | 44 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 45 | /* Out-Of-Bounds test for any pointer to data in the buffer */ |
| 46 | #define BUFFER_OOB(p) ((p) < buffer || (p) >= buffer_end) |
| 47 | |
| 48 | /* Does the buffer contain the beginning of the file? */ |
| 49 | #define BUFFER_BOF() (file_pos==0) |
| 50 | |
| 51 | /* Does the buffer contain the end of the file? */ |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 52 | #define BUFFER_EOF() (file_size-file_pos <= buffer_size) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 53 | |
| 54 | /* Formula for the endpoint address outside of buffer data */ |
| 55 | #define BUFFER_END() \ |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 56 | ((BUFFER_EOF()) ? (file_size-file_pos+buffer) : (buffer+buffer_size)) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 57 | |
| 58 | /* Is the entire file being shown in one screen? */ |
| 59 | #define ONE_SCREEN_FITS_ALL() \ |
| 60 | (next_screen_ptr==NULL && screen_top_ptr==buffer && BUFFER_BOF()) |
| 61 | |
| 62 | /* Is a scrollbar called for on the current screen? */ |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 63 | #define NEED_SCROLLBAR() \ |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 64 | ((!(ONE_SCREEN_FITS_ALL())) && (prefs.scrollbar_mode==SB_ON)) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 65 | |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 66 | /* variable button definitions */ |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 67 | |
| 68 | /* Recorder keys */ |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 69 | #if CONFIG_KEYPAD == RECORDER_PAD |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 70 | #define VIEWER_QUIT BUTTON_OFF |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 71 | #define VIEWER_PAGE_UP BUTTON_UP |
| 72 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 73 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 74 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 75 | #define VIEWER_MENU BUTTON_F1 |
| 76 | #define VIEWER_AUTOSCROLL BUTTON_PLAY |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 77 | #define VIEWER_LINE_UP (BUTTON_ON | BUTTON_UP) |
| 78 | #define VIEWER_LINE_DOWN (BUTTON_ON | BUTTON_DOWN) |
| 79 | #define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT) |
| 80 | #define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT) |
| 81 | |
Dave Chapman | d64e626 | 2007-01-14 13:48:09 +0000 | [diff] [blame] | 82 | #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD |
| 83 | #define VIEWER_QUIT BUTTON_OFF |
| 84 | #define VIEWER_PAGE_UP BUTTON_UP |
| 85 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 86 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 87 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
| 88 | #define VIEWER_MENU BUTTON_F1 |
| 89 | #define VIEWER_AUTOSCROLL BUTTON_SELECT |
| 90 | #define VIEWER_LINE_UP (BUTTON_ON | BUTTON_UP) |
| 91 | #define VIEWER_LINE_DOWN (BUTTON_ON | BUTTON_DOWN) |
| 92 | #define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT) |
| 93 | #define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT) |
| 94 | |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 95 | /* Ondio keys */ |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 96 | #elif CONFIG_KEYPAD == ONDIO_PAD |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 97 | #define VIEWER_QUIT BUTTON_OFF |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 98 | #define VIEWER_PAGE_UP BUTTON_UP |
| 99 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 100 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 101 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 102 | #define VIEWER_MENU (BUTTON_MENU|BUTTON_REPEAT) |
| 103 | #define VIEWER_AUTOSCROLL_PRE BUTTON_MENU |
| 104 | #define VIEWER_AUTOSCROLL (BUTTON_MENU|BUTTON_REL) |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 105 | |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 106 | /* Player keys */ |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 107 | #elif CONFIG_KEYPAD == PLAYER_PAD |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 108 | #define VIEWER_QUIT BUTTON_STOP |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 109 | #define VIEWER_PAGE_UP BUTTON_LEFT |
| 110 | #define VIEWER_PAGE_DOWN BUTTON_RIGHT |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 111 | #define VIEWER_SCREEN_LEFT (BUTTON_ON|BUTTON_LEFT) |
| 112 | #define VIEWER_SCREEN_RIGHT (BUTTON_ON|BUTTON_RIGHT) |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 113 | #define VIEWER_MENU BUTTON_MENU |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 114 | #define VIEWER_AUTOSCROLL BUTTON_PLAY |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 115 | |
Daniel Stenberg | 01377e2 | 2005-06-29 12:47:24 +0000 | [diff] [blame] | 116 | /* iRiver H1x0 && H3x0 keys */ |
| 117 | #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \ |
| 118 | (CONFIG_KEYPAD == IRIVER_H300_PAD) |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 119 | #define VIEWER_QUIT BUTTON_OFF |
Daniel Stenberg | 6700ad8 | 2005-02-04 12:41:09 +0000 | [diff] [blame] | 120 | #define VIEWER_PAGE_UP BUTTON_UP |
| 121 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 122 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 123 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 124 | #define VIEWER_MENU BUTTON_MODE |
| 125 | #define VIEWER_AUTOSCROLL BUTTON_SELECT |
Jens Arnold | dbe1911 | 2005-02-12 02:02:20 +0000 | [diff] [blame] | 126 | #define VIEWER_LINE_UP (BUTTON_ON | BUTTON_UP) |
| 127 | #define VIEWER_LINE_DOWN (BUTTON_ON | BUTTON_DOWN) |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 128 | #define VIEWER_COLUMN_LEFT (BUTTON_ON | BUTTON_LEFT) |
| 129 | #define VIEWER_COLUMN_RIGHT (BUTTON_ON | BUTTON_RIGHT) |
Daniel Stenberg | 6700ad8 | 2005-02-04 12:41:09 +0000 | [diff] [blame] | 130 | |
Kevin Ferrare | 0e027bd | 2006-06-30 16:43:47 +0000 | [diff] [blame] | 131 | #define VIEWER_RC_QUIT BUTTON_RC_STOP |
| 132 | |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 133 | /* iPods */ |
Dave Chapman | fb4e384 | 2006-02-24 20:54:09 +0000 | [diff] [blame] | 134 | #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \ |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 135 | (CONFIG_KEYPAD == IPOD_3G_PAD) || \ |
| 136 | (CONFIG_KEYPAD == IPOD_1G2G_PAD) |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 137 | #define VIEWER_QUIT_PRE BUTTON_SELECT |
| 138 | #define VIEWER_QUIT (BUTTON_SELECT | BUTTON_MENU) |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 139 | #define VIEWER_PAGE_UP BUTTON_SCROLL_BACK |
| 140 | #define VIEWER_PAGE_DOWN BUTTON_SCROLL_FWD |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 141 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 142 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 143 | #define VIEWER_MENU BUTTON_MENU |
| 144 | #define VIEWER_AUTOSCROLL BUTTON_PLAY |
Dave Chapman | 54d44c8 | 2005-12-14 01:31:37 +0000 | [diff] [blame] | 145 | |
Dave Chapman | d83e929 | 2006-01-12 00:35:50 +0000 | [diff] [blame] | 146 | /* iFP7xx keys */ |
| 147 | #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 148 | #define VIEWER_QUIT BUTTON_PLAY |
Dave Chapman | d83e929 | 2006-01-12 00:35:50 +0000 | [diff] [blame] | 149 | #define VIEWER_PAGE_UP BUTTON_UP |
| 150 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 151 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 152 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 153 | #define VIEWER_MENU BUTTON_MODE |
| 154 | #define VIEWER_AUTOSCROLL BUTTON_SELECT |
Dave Chapman | d83e929 | 2006-01-12 00:35:50 +0000 | [diff] [blame] | 155 | |
Daniel Stenberg | cedba88 | 2006-01-18 11:09:06 +0000 | [diff] [blame] | 156 | /* iAudio X5 keys */ |
Jens Arnold | 85a226d | 2007-03-16 23:02:39 +0000 | [diff] [blame] | 157 | #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 158 | #define VIEWER_QUIT BUTTON_POWER |
Daniel Stenberg | cedba88 | 2006-01-18 11:09:06 +0000 | [diff] [blame] | 159 | #define VIEWER_PAGE_UP BUTTON_UP |
| 160 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 161 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 162 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 163 | #define VIEWER_MENU BUTTON_SELECT |
| 164 | #define VIEWER_AUTOSCROLL BUTTON_PLAY |
Daniel Stenberg | cedba88 | 2006-01-18 11:09:06 +0000 | [diff] [blame] | 165 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 166 | /* GIGABEAT keys */ |
Marcoen Hirschberg | 338e2bb | 2006-02-24 15:42:52 +0000 | [diff] [blame] | 167 | #elif CONFIG_KEYPAD == GIGABEAT_PAD |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 168 | #define VIEWER_QUIT BUTTON_POWER |
Marcoen Hirschberg | 338e2bb | 2006-02-24 15:42:52 +0000 | [diff] [blame] | 169 | #define VIEWER_PAGE_UP BUTTON_UP |
| 170 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 171 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 172 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 173 | #define VIEWER_MENU BUTTON_MENU |
| 174 | #define VIEWER_AUTOSCROLL BUTTON_A |
Marcoen Hirschberg | 338e2bb | 2006-02-24 15:42:52 +0000 | [diff] [blame] | 175 | |
Barry Wardell | 24f4a2a | 2006-10-26 13:38:09 +0000 | [diff] [blame] | 176 | /* Sansa E200 keys */ |
| 177 | #elif CONFIG_KEYPAD == SANSA_E200_PAD |
| 178 | #define VIEWER_QUIT BUTTON_POWER |
| 179 | #define VIEWER_PAGE_UP BUTTON_UP |
| 180 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 181 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 182 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
| 183 | #define VIEWER_MENU BUTTON_SELECT |
Jonathan Gordon | 0e8a9e4 | 2007-05-06 15:41:10 +0000 | [diff] [blame] | 184 | #define VIEWER_AUTOSCROLL BUTTON_REC |
Dave Chapman | d7d073a | 2008-01-17 11:29:14 +0000 | [diff] [blame] | 185 | #define VIEWER_LINE_UP BUTTON_SCROLL_BACK |
| 186 | #define VIEWER_LINE_DOWN BUTTON_SCROLL_FWD |
Barry Wardell | 24f4a2a | 2006-10-26 13:38:09 +0000 | [diff] [blame] | 187 | |
Marianne Arnold | 12ddb8e | 2007-09-20 10:49:48 +0000 | [diff] [blame] | 188 | /* Sansa C200 keys */ |
| 189 | #elif CONFIG_KEYPAD == SANSA_C200_PAD |
| 190 | #define VIEWER_QUIT BUTTON_POWER |
| 191 | #define VIEWER_PAGE_UP BUTTON_VOL_UP |
| 192 | #define VIEWER_PAGE_DOWN BUTTON_VOL_DOWN |
| 193 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 194 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
| 195 | #define VIEWER_MENU BUTTON_SELECT |
| 196 | #define VIEWER_AUTOSCROLL BUTTON_REC |
| 197 | #define VIEWER_LINE_UP BUTTON_UP |
| 198 | #define VIEWER_LINE_DOWN BUTTON_DOWN |
| 199 | |
Daniel Stenberg | 1e88be5 | 2006-08-03 20:17:25 +0000 | [diff] [blame] | 200 | /* iriver H10 keys */ |
| 201 | #elif CONFIG_KEYPAD == IRIVER_H10_PAD |
| 202 | #define VIEWER_QUIT BUTTON_POWER |
| 203 | #define VIEWER_PAGE_UP BUTTON_SCROLL_UP |
| 204 | #define VIEWER_PAGE_DOWN BUTTON_SCROLL_DOWN |
| 205 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 206 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
| 207 | #define VIEWER_MENU BUTTON_REW |
| 208 | #define VIEWER_AUTOSCROLL BUTTON_PLAY |
| 209 | |
Mustapha Senhaji | 79ba669 | 2008-05-18 14:09:33 +0000 | [diff] [blame] | 210 | /*M-Robe 500 keys */ |
Jonathan Gordon | 56ddddc | 2007-10-23 15:40:51 +0000 | [diff] [blame] | 211 | #elif CONFIG_KEYPAD == MROBE500_PAD |
| 212 | #define VIEWER_QUIT BUTTON_POWER |
| 213 | #define VIEWER_PAGE_UP BUTTON_RC_PLAY |
| 214 | #define VIEWER_PAGE_DOWN BUTTON_RC_DOWN |
| 215 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 216 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
| 217 | #define VIEWER_MENU BUTTON_RC_HEART |
| 218 | #define VIEWER_AUTOSCROLL BUTTON_RC_MODE |
| 219 | |
Mustapha Senhaji | 79ba669 | 2008-05-18 14:09:33 +0000 | [diff] [blame] | 220 | /*Gigabeat S keys */ |
Will Robertson | 8215b34 | 2008-02-17 12:23:02 +0000 | [diff] [blame] | 221 | #elif CONFIG_KEYPAD == GIGABEAT_S_PAD |
| 222 | #define VIEWER_QUIT BUTTON_BACK |
Mustapha Senhaji | 79ba669 | 2008-05-18 14:09:33 +0000 | [diff] [blame] | 223 | #define VIEWER_PAGE_UP BUTTON_PREV |
| 224 | #define VIEWER_PAGE_DOWN BUTTON_NEXT |
| 225 | #define VIEWER_SCREEN_LEFT (BUTTON_PLAY | BUTTON_LEFT) |
| 226 | #define VIEWER_SCREEN_RIGHT (BUTTON_PLAY | BUTTON_RIGHT) |
Will Robertson | 8215b34 | 2008-02-17 12:23:02 +0000 | [diff] [blame] | 227 | #define VIEWER_MENU BUTTON_MENU |
Mustapha Senhaji | 79ba669 | 2008-05-18 14:09:33 +0000 | [diff] [blame] | 228 | #define VIEWER_AUTOSCROLL_PRE BUTTON_PLAY |
| 229 | #define VIEWER_AUTOSCROLL (BUTTON_PLAY|BUTTON_REL) |
| 230 | #define VIEWER_LINE_UP BUTTON_UP |
| 231 | #define VIEWER_LINE_DOWN BUTTON_DOWN |
| 232 | #define VIEWER_COLUMN_LEFT BUTTON_LEFT |
| 233 | #define VIEWER_COLUMN_RIGHT BUTTON_RIGHT |
Will Robertson | 8215b34 | 2008-02-17 12:23:02 +0000 | [diff] [blame] | 234 | |
Mustapha Senhaji | 79ba669 | 2008-05-18 14:09:33 +0000 | [diff] [blame] | 235 | /*M-Robe 100 keys */ |
Robert Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 236 | #elif CONFIG_KEYPAD == MROBE100_PAD |
| 237 | #define VIEWER_QUIT BUTTON_POWER |
| 238 | #define VIEWER_PAGE_UP BUTTON_UP |
| 239 | #define VIEWER_PAGE_DOWN BUTTON_DOWN |
| 240 | #define VIEWER_SCREEN_LEFT BUTTON_LEFT |
| 241 | #define VIEWER_SCREEN_RIGHT BUTTON_RIGHT |
| 242 | #define VIEWER_MENU BUTTON_MENU |
| 243 | #define VIEWER_AUTOSCROLL BUTTON_DISPLAY |
| 244 | |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 245 | /* iAUdio M3 keys */ |
| 246 | #elif CONFIG_KEYPAD == IAUDIO_M3_PAD |
| 247 | #define VIEWER_QUIT BUTTON_RC_REC |
| 248 | #define VIEWER_PAGE_UP BUTTON_RC_VOL_UP |
| 249 | #define VIEWER_PAGE_DOWN BUTTON_RC_VOL_DOWN |
| 250 | #define VIEWER_SCREEN_LEFT BUTTON_RC_REW |
| 251 | #define VIEWER_SCREEN_RIGHT BUTTON_RC_FF |
| 252 | #define VIEWER_MENU BUTTON_RC_MENU |
| 253 | #define VIEWER_AUTOSCROLL BUTTON_RC_MODE |
Jens Arnold | 29361ab | 2008-03-22 10:24:28 +0000 | [diff] [blame] | 254 | #define VIEWER_RC_QUIT BUTTON_REC |
| 255 | |
Mustapha Senhaji | 79ba669 | 2008-05-18 14:09:33 +0000 | [diff] [blame] | 256 | /* Cowon D2 keys */ |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 257 | #elif CONFIG_KEYPAD == COWOND2_PAD |
| 258 | #define VIEWER_QUIT BUTTON_POWER |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 259 | #define VIEWER_MENU BUTTON_MENU |
Rob Purchase | 554d7ed | 2008-03-22 22:03:34 +0000 | [diff] [blame] | 260 | |
Robert Kukla | d6c8b57 | 2008-03-01 22:55:09 +0000 | [diff] [blame] | 261 | #else |
| 262 | #error No keymap defined! |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 263 | #endif |
| 264 | |
Rob Purchase | 297e050 | 2008-04-27 15:30:19 +0000 | [diff] [blame] | 265 | #ifdef HAVE_TOUCHPAD |
| 266 | #ifndef VIEWER_QUIT |
| 267 | #define VIEWER_QUIT BUTTON_TOPLEFT |
| 268 | #endif |
| 269 | #ifndef VIEWER_PAGE_UP |
| 270 | #define VIEWER_PAGE_UP BUTTON_TOPMIDDLE |
| 271 | #endif |
| 272 | #ifndef VIEWER_PAGE_DOWN |
| 273 | #define VIEWER_PAGE_DOWN BUTTON_BOTTOMMIDDLE |
| 274 | #endif |
| 275 | #ifndef VIEWER_SCREEN_LEFT |
| 276 | #define VIEWER_SCREEN_LEFT BUTTON_MIDLEFT |
| 277 | #endif |
| 278 | #ifndef VIEWER_SCREEN_RIGHT |
| 279 | #define VIEWER_SCREEN_RIGHT BUTTON_MIDRIGHT |
| 280 | #endif |
| 281 | #ifndef VIEWER_MENU |
| 282 | #define VIEWER_MENU BUTTON_TOPRIGHT |
| 283 | #endif |
| 284 | #ifndef VIEWER_AUTOSCROLL |
| 285 | #define VIEWER_AUTOSCROLL BUTTON_CENTER |
| 286 | #endif |
| 287 | #endif |
| 288 | |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 289 | /* stuff for the bookmarking */ |
Jonathan Gordon | c4c0583 | 2006-09-17 10:32:49 +0000 | [diff] [blame] | 290 | struct bookmarked_file_info { |
| 291 | long file_position; |
| 292 | int top_ptr_pos; |
| 293 | char filename[MAX_PATH]; |
| 294 | }; |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 295 | |
| 296 | struct bookmark_file_data { |
| 297 | signed int bookmarked_files_count; |
| 298 | struct bookmarked_file_info bookmarks[]; |
| 299 | }; |
| 300 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 301 | struct preferences { |
| 302 | enum { |
| 303 | WRAP=0, |
| 304 | CHOP, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 305 | } word_mode; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 306 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 307 | enum { |
| 308 | NORMAL=0, |
| 309 | JOIN, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 310 | EXPAND, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 311 | REFLOW, /* won't be set on charcell LCD, must be last */ |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 312 | } line_mode; |
Jens Arnold | 5bb7072 | 2005-05-14 16:46:31 +0000 | [diff] [blame] | 313 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 314 | enum { |
| 315 | NARROW=0, |
| 316 | WIDE, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 317 | } view_mode; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 318 | |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 319 | enum { |
| 320 | ISO_8859_1=0, |
| 321 | ISO_8859_7, |
| 322 | ISO_8859_8, |
| 323 | CP1251, |
| 324 | ISO_8859_11, |
| 325 | ISO_8859_6, |
| 326 | ISO_8859_9, |
| 327 | ISO_8859_2, |
Marcoen Hirschberg | ded94e6 | 2007-10-01 17:16:31 +0000 | [diff] [blame] | 328 | CP1250, |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 329 | SJIS, |
| 330 | GB2312, |
| 331 | KSX1001, |
| 332 | BIG5, |
| 333 | UTF8, |
| 334 | ENCODINGS |
| 335 | } encoding; /* FIXME: What should default encoding be? */ |
| 336 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 337 | #ifdef HAVE_LCD_BITMAP |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 338 | enum { |
| 339 | SB_OFF=0, |
| 340 | SB_ON, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 341 | } scrollbar_mode; |
| 342 | bool need_scrollbar; |
Daniel Stenberg | 1ca465a | 2006-01-27 13:30:57 +0000 | [diff] [blame] | 343 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 344 | enum { |
| 345 | NO_OVERLAP=0, |
| 346 | OVERLAP, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 347 | } page_mode; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 348 | #endif /* HAVE_LCD_BITMAP */ |
Daniel Stenberg | 1ca465a | 2006-01-27 13:30:57 +0000 | [diff] [blame] | 349 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 350 | enum { |
| 351 | PAGE=0, |
| 352 | LINE, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 353 | } scroll_mode; |
Daniel Stenberg | 1ca465a | 2006-01-27 13:30:57 +0000 | [diff] [blame] | 354 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 355 | int autoscroll_speed; |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 356 | |
Marcoen Hirschberg | 2b30bf9 | 2007-11-19 20:51:42 +0000 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | struct preferences prefs; |
| 360 | struct preferences old_prefs; |
Daniel Stenberg | 1ca465a | 2006-01-27 13:30:57 +0000 | [diff] [blame] | 361 | |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 362 | static unsigned char *buffer; |
| 363 | static long buffer_size; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 364 | static unsigned char line_break[] = {0,0x20,9,0xB,0xC,'-'}; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 365 | static int display_columns; /* number of (pixel) columns on the display */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 366 | static int display_lines; /* number of lines on the display */ |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 367 | static int draw_columns; /* number of (pixel) columns available for text */ |
| 368 | static int par_indent_spaces; /* number of spaces to indent first paragraph */ |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 369 | static int fd; |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 370 | static const char *file_name; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 371 | static long file_size; |
Marcoen Hirschberg | 4c62c00 | 2007-11-19 16:02:45 +0000 | [diff] [blame] | 372 | static long start_position; /* position in the file after the viewer is started */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 373 | static bool mac_text; |
| 374 | static long file_pos; /* Position of the top of the buffer in the file */ |
| 375 | static unsigned char *buffer_end; /*Set to BUFFER_END() when file_pos changes*/ |
| 376 | static int max_line_len; |
| 377 | static unsigned char *screen_top_ptr; |
| 378 | static unsigned char *next_screen_ptr; |
| 379 | static unsigned char *next_screen_to_draw_ptr; |
| 380 | static unsigned char *next_line_ptr; |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 381 | static const struct plugin_api* rb; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 382 | #ifdef HAVE_LCD_BITMAP |
| 383 | static struct font *pf; |
| 384 | #endif |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 385 | |
Jonathan Gordon | c4c0583 | 2006-09-17 10:32:49 +0000 | [diff] [blame] | 386 | |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 387 | int glyph_width(int ch) |
| 388 | { |
| 389 | if (ch == 0) |
| 390 | ch = ' '; |
| 391 | |
| 392 | #ifdef HAVE_LCD_BITMAP |
| 393 | return rb->font_get_width(pf, ch); |
| 394 | #else |
| 395 | return 1; |
| 396 | #endif |
| 397 | } |
| 398 | |
| 399 | unsigned char* get_ucs(const unsigned char* str, unsigned short* ch) |
| 400 | { |
| 401 | unsigned char utf8_tmp[6]; |
| 402 | int count; |
| 403 | |
| 404 | if (prefs.encoding == UTF8) |
| 405 | return (unsigned char*)rb->utf8decode(str, ch); |
| 406 | |
| 407 | count = BUFFER_OOB(str+2)? 1:2; |
| 408 | rb->iso_decode(str, utf8_tmp, prefs.encoding, count); |
| 409 | rb->utf8decode(utf8_tmp, ch); |
| 410 | |
| 411 | if ((prefs.encoding == SJIS && *str > 0xA0 && *str < 0xE0) || prefs.encoding < SJIS) |
| 412 | return (unsigned char*)str+1; |
| 413 | else |
| 414 | return (unsigned char*)str+2; |
| 415 | } |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 416 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 417 | bool done = false; |
| 418 | int col = 0; |
| 419 | |
Karl Kurbjun | ca8fa65 | 2006-06-03 07:04:02 +0000 | [diff] [blame] | 420 | #define ADVANCE_COUNTERS(c) { width += glyph_width(c); k++; } |
Linus Nielsen Feltzing | 73c283e | 2006-08-01 17:48:38 +0000 | [diff] [blame] | 421 | #define LINE_IS_FULL ((k>=MAX_COLUMNS-1) ||( width >= draw_columns)) |
Karl Kurbjun | ca8fa65 | 2006-06-03 07:04:02 +0000 | [diff] [blame] | 422 | #define LINE_IS_NOT_FULL ((k<MAX_COLUMNS-1) &&( width < draw_columns)) |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 423 | static unsigned char* crop_at_width(const unsigned char* p) |
| 424 | { |
| 425 | int k,width; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 426 | unsigned short ch; |
| 427 | const unsigned char *oldp = p; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 428 | |
| 429 | k=width=0; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 430 | |
Karl Kurbjun | ca8fa65 | 2006-06-03 07:04:02 +0000 | [diff] [blame] | 431 | while (LINE_IS_NOT_FULL) { |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 432 | oldp = p; |
| 433 | p = get_ucs(p, &ch); |
| 434 | ADVANCE_COUNTERS(ch); |
| 435 | } |
| 436 | |
| 437 | return (unsigned char*)oldp; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 440 | static unsigned char* find_first_feed(const unsigned char* p, int size) |
| 441 | { |
| 442 | int i; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 443 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 444 | for (i=0; i < size; i++) |
| 445 | if (p[i] == 0) |
| 446 | return (unsigned char*) p+i; |
| 447 | |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | static unsigned char* find_last_feed(const unsigned char* p, int size) |
| 452 | { |
| 453 | int i; |
| 454 | |
| 455 | for (i=size-1; i>=0; i--) |
| 456 | if (p[i] == 0) |
| 457 | return (unsigned char*) p+i; |
| 458 | |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | static unsigned char* find_last_space(const unsigned char* p, int size) |
| 463 | { |
| 464 | int i, j, k; |
| 465 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 466 | k = (prefs.line_mode==JOIN) || (prefs.line_mode==REFLOW) ? 0:1; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 467 | |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 468 | if (!BUFFER_OOB(&p[size])) |
| 469 | for (j=k; j < ((int) sizeof(line_break)) - 1; j++) |
| 470 | if (p[size] == line_break[j]) |
| 471 | return (unsigned char*) p+size; |
| 472 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 473 | for (i=size-1; i>=0; i--) |
| 474 | for (j=k; j < (int) sizeof(line_break); j++) |
Jonathan Gordon | 739f0ec | 2007-08-05 04:16:21 +0000 | [diff] [blame] | 475 | { |
Robert Kukla | 2ae0d63 | 2007-08-13 13:53:33 +0000 | [diff] [blame] | 476 | if (!((p[i] == '-') && (prefs.word_mode == WRAP))) |
Jonathan Gordon | 739f0ec | 2007-08-05 04:16:21 +0000 | [diff] [blame] | 477 | if (p[i] == line_break[j]) |
| 478 | return (unsigned char*) p+i; |
| 479 | } |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 480 | |
| 481 | return NULL; |
| 482 | } |
| 483 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 484 | static unsigned char* find_next_line(const unsigned char* cur_line, bool *is_short) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 485 | { |
| 486 | const unsigned char *next_line = NULL; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 487 | int size, i, j, k, width, search_len, spaces, newlines; |
| 488 | bool first_chars; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 489 | unsigned char c; |
| 490 | |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 491 | if (is_short != NULL) |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 492 | *is_short = true; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 493 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 494 | if BUFFER_OOB(cur_line) |
| 495 | return NULL; |
| 496 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 497 | if (prefs.view_mode == WIDE) { |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 498 | search_len = MAX_WIDTH; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 499 | } |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 500 | else { /* prefs.view_mode == NARROW */ |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 501 | search_len = crop_at_width(cur_line) - cur_line; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | size = BUFFER_OOB(cur_line+search_len) ? buffer_end-cur_line : search_len; |
| 505 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 506 | if ((prefs.line_mode == JOIN) || (prefs.line_mode == REFLOW)) { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 507 | /* Need to scan ahead and possibly increase search_len and size, |
| 508 | or possibly set next_line at second hard return in a row. */ |
| 509 | next_line = NULL; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 510 | first_chars=true; |
| 511 | for (j=k=width=spaces=newlines=0; ; j++) { |
Jens Arnold | 5bb7072 | 2005-05-14 16:46:31 +0000 | [diff] [blame] | 512 | if (BUFFER_OOB(cur_line+j)) |
| 513 | return NULL; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 514 | if (LINE_IS_FULL) { |
| 515 | size = search_len = j; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 516 | break; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 517 | } |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 518 | |
| 519 | c = cur_line[j]; |
| 520 | switch (c) { |
| 521 | case ' ': |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 522 | if (prefs.line_mode == REFLOW) { |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 523 | if (newlines > 0) { |
| 524 | size = j; |
| 525 | next_line = cur_line + size; |
| 526 | return (unsigned char*) next_line; |
| 527 | } |
| 528 | if (j==0) /* i=1 is intentional */ |
| 529 | for (i=0; i<par_indent_spaces; i++) |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 530 | ADVANCE_COUNTERS(' '); |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 531 | } |
| 532 | if (!first_chars) spaces++; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 533 | break; |
| 534 | |
| 535 | case 0: |
| 536 | if (newlines > 0) { |
| 537 | size = j; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 538 | next_line = cur_line + size - spaces; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 539 | if (next_line != cur_line) |
| 540 | return (unsigned char*) next_line; |
| 541 | break; |
| 542 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 543 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 544 | newlines++; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 545 | size += spaces -1; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 546 | if (BUFFER_OOB(cur_line+size) || size > 2*search_len) |
| 547 | return NULL; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 548 | search_len = size; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 549 | spaces = first_chars? 0:1; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 550 | break; |
| 551 | |
| 552 | default: |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 553 | if (prefs.line_mode==JOIN || newlines>0) { |
Jens Arnold | 5bb7072 | 2005-05-14 16:46:31 +0000 | [diff] [blame] | 554 | while (spaces) { |
| 555 | spaces--; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 556 | ADVANCE_COUNTERS(' '); |
| 557 | if (LINE_IS_FULL) { |
| 558 | size = search_len = j; |
Jens Arnold | 5bb7072 | 2005-05-14 16:46:31 +0000 | [diff] [blame] | 559 | break; |
| 560 | } |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 561 | } |
| 562 | newlines=0; |
Jens Arnold | 5bb7072 | 2005-05-14 16:46:31 +0000 | [diff] [blame] | 563 | } else if (spaces) { |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 564 | /* REFLOW, multiple spaces between words: count only |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 565 | * one. If more are needed, they will be added |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 566 | * while drawing. */ |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 567 | search_len = size; |
| 568 | spaces=0; |
| 569 | ADVANCE_COUNTERS(' '); |
| 570 | if (LINE_IS_FULL) { |
| 571 | size = search_len = j; |
| 572 | break; |
| 573 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 574 | } |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 575 | first_chars = false; |
| 576 | ADVANCE_COUNTERS(c); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 577 | break; |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | else { |
| 582 | /* find first hard return */ |
| 583 | next_line = find_first_feed(cur_line, size); |
| 584 | } |
| 585 | |
| 586 | if (next_line == NULL) |
| 587 | if (size == search_len) { |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 588 | if (prefs.word_mode == WRAP) /* Find last space */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 589 | next_line = find_last_space(cur_line, size); |
| 590 | |
| 591 | if (next_line == NULL) |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 592 | next_line = crop_at_width(cur_line); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 593 | else |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 594 | if (prefs.word_mode == WRAP) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 595 | for (i=0; |
| 596 | i<WRAP_TRIM && isspace(next_line[0]) && !BUFFER_OOB(next_line); |
| 597 | i++) |
| 598 | next_line++; |
| 599 | } |
| 600 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 601 | if (prefs.line_mode == EXPAND) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 602 | if (!BUFFER_OOB(next_line)) /* Not Null & not out of bounds */ |
| 603 | if (next_line[0] == 0) |
| 604 | if (next_line != cur_line) |
| 605 | return (unsigned char*) next_line; |
| 606 | |
| 607 | /* If next_line is pointing to a zero, increment it; i.e., |
| 608 | leave the terminator at the end of cur_line. If pointing |
| 609 | to a hyphen, increment only if there is room to display |
| 610 | the hyphen on current line (won't apply in WIDE mode, |
| 611 | since it's guarenteed there won't be room). */ |
| 612 | if (!BUFFER_OOB(next_line)) /* Not Null & not out of bounds */ |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 613 | if (next_line[0] == 0)/* || |
| 614 | (next_line[0] == '-' && next_line-cur_line < draw_columns)) */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 615 | next_line++; |
| 616 | |
| 617 | if (BUFFER_OOB(next_line)) |
| 618 | return NULL; |
| 619 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 620 | if (is_short) |
| 621 | *is_short = false; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 622 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 623 | return (unsigned char*) next_line; |
| 624 | } |
| 625 | |
| 626 | static unsigned char* find_prev_line(const unsigned char* cur_line) |
| 627 | { |
| 628 | const unsigned char *prev_line = NULL; |
| 629 | const unsigned char *p; |
| 630 | |
| 631 | if BUFFER_OOB(cur_line) |
| 632 | return NULL; |
| 633 | |
| 634 | /* To wrap consistently at the same places, we must |
| 635 | start with a known hard return, then work downwards. |
| 636 | We can either search backwards for a hard return, |
| 637 | or simply start wrapping downwards from top of buffer. |
| 638 | If current line is not near top of buffer, this is |
| 639 | a file with long lines (paragraphs). We would need to |
| 640 | read earlier sectors before we could decide how to |
| 641 | properly wrap the lines above the current line, but |
| 642 | it probably is not worth the disk access. Instead, |
| 643 | start with top of buffer and wrap down from there. |
| 644 | This may result in some lines wrapping at different |
| 645 | points from where they wrap when scrolling down. |
| 646 | If buffer is at top of file, start at top of buffer. */ |
| 647 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 648 | if ((prefs.line_mode == JOIN) || (prefs.line_mode == REFLOW)) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 649 | prev_line = p = NULL; |
| 650 | else |
| 651 | prev_line = p = find_last_feed(buffer, cur_line-buffer-1); |
| 652 | /* Null means no line feeds in buffer above current line. */ |
| 653 | |
| 654 | if (prev_line == NULL) |
| 655 | if (BUFFER_BOF() || cur_line - buffer > READ_PREV_ZONE) |
| 656 | prev_line = p = buffer; |
| 657 | /* (else return NULL and read previous block) */ |
| 658 | |
| 659 | /* Wrap downwards until too far, then use the one before. */ |
| 660 | while (p < cur_line && p != NULL) { |
| 661 | prev_line = p; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 662 | p = find_next_line(prev_line, NULL); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | if (BUFFER_OOB(prev_line)) |
| 666 | return NULL; |
| 667 | |
| 668 | return (unsigned char*) prev_line; |
| 669 | } |
| 670 | |
| 671 | static void fill_buffer(long pos, unsigned char* buf, unsigned size) |
| 672 | { |
| 673 | /* Read from file and preprocess the data */ |
| 674 | /* To minimize disk access, always read on sector boundaries */ |
| 675 | unsigned numread, i; |
| 676 | bool found_CR = false; |
| 677 | |
| 678 | rb->lseek(fd, pos, SEEK_SET); |
| 679 | numread = rb->read(fd, buf, size); |
Jens Arnold | 3c348df | 2005-01-26 22:48:25 +0000 | [diff] [blame] | 680 | rb->button_clear_queue(); /* clear button queue */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 681 | |
| 682 | for(i = 0; i < numread; i++) { |
| 683 | switch(buf[i]) { |
| 684 | case '\r': |
| 685 | if (mac_text) { |
| 686 | buf[i] = 0; |
| 687 | } |
| 688 | else { |
| 689 | buf[i] = ' '; |
| 690 | found_CR = true; |
| 691 | } |
| 692 | break; |
| 693 | |
| 694 | case '\n': |
| 695 | buf[i] = 0; |
| 696 | found_CR = false; |
| 697 | break; |
| 698 | |
| 699 | case 0: /* No break between case 0 and default, intentionally */ |
| 700 | buf[i] = ' '; |
| 701 | default: |
| 702 | if (found_CR) { |
| 703 | buf[i - 1] = 0; |
| 704 | found_CR = false; |
| 705 | mac_text = true; |
| 706 | } |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | static int read_and_synch(int direction) |
| 713 | { |
| 714 | /* Read next (or prev) block, and reposition global pointers. */ |
| 715 | /* direction: 1 for down (i.e., further into file), -1 for up */ |
| 716 | int move_size, move_vector, offset; |
| 717 | unsigned char *fill_buf; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 718 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 719 | if (direction == -1) /* up */ { |
| 720 | move_size = SMALL_BLOCK_SIZE; |
| 721 | offset = 0; |
| 722 | fill_buf = TOP_SECTOR; |
| 723 | rb->memcpy(BOTTOM_SECTOR, MID_SECTOR, SMALL_BLOCK_SIZE); |
| 724 | rb->memcpy(MID_SECTOR, TOP_SECTOR, SMALL_BLOCK_SIZE); |
| 725 | } |
| 726 | else /* down */ { |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 727 | if (prefs.view_mode == WIDE) { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 728 | /* WIDE mode needs more buffer so we have to read smaller blocks */ |
| 729 | move_size = SMALL_BLOCK_SIZE; |
| 730 | offset = LARGE_BLOCK_SIZE; |
| 731 | fill_buf = BOTTOM_SECTOR; |
| 732 | rb->memcpy(TOP_SECTOR, MID_SECTOR, SMALL_BLOCK_SIZE); |
| 733 | rb->memcpy(MID_SECTOR, BOTTOM_SECTOR, SMALL_BLOCK_SIZE); |
| 734 | } |
| 735 | else { |
| 736 | move_size = LARGE_BLOCK_SIZE; |
| 737 | offset = SMALL_BLOCK_SIZE; |
| 738 | fill_buf = MID_SECTOR; |
| 739 | rb->memcpy(TOP_SECTOR, BOTTOM_SECTOR, SMALL_BLOCK_SIZE); |
| 740 | } |
| 741 | } |
| 742 | move_vector = direction * move_size; |
| 743 | screen_top_ptr -= move_vector; |
| 744 | file_pos += move_vector; |
| 745 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
| 746 | fill_buffer(file_pos + offset, fill_buf, move_size); |
| 747 | return move_vector; |
| 748 | } |
| 749 | |
| 750 | static void viewer_scroll_up(void) |
| 751 | { |
| 752 | unsigned char *p; |
| 753 | |
| 754 | p = find_prev_line(screen_top_ptr); |
| 755 | if (p == NULL && !BUFFER_BOF()) { |
| 756 | read_and_synch(-1); |
| 757 | p = find_prev_line(screen_top_ptr); |
| 758 | } |
| 759 | if (p != NULL) |
| 760 | screen_top_ptr = p; |
| 761 | } |
| 762 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 763 | static void viewer_scroll_down(void) |
| 764 | { |
| 765 | if (next_screen_ptr != NULL) |
| 766 | screen_top_ptr = next_line_ptr; |
| 767 | } |
| 768 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 769 | #ifdef HAVE_LCD_BITMAP |
| 770 | static void viewer_scrollbar(void) { |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 771 | int items, min_shown, max_shown; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 772 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 773 | items = (int) file_size; /* (SH1 int is same as long) */ |
| 774 | min_shown = (int) file_pos + (screen_top_ptr - buffer); |
| 775 | |
| 776 | if (next_screen_ptr == NULL) |
| 777 | max_shown = items; |
| 778 | else |
| 779 | max_shown = min_shown + (next_screen_ptr - screen_top_ptr); |
| 780 | |
Jonathan Gordon | 6288523 | 2006-11-19 14:11:42 +0000 | [diff] [blame] | 781 | rb->gui_scrollbar_draw(rb->screens[SCREEN_MAIN],0, 0, SCROLLBAR_WIDTH-1, |
| 782 | LCD_HEIGHT, items, min_shown, max_shown, VERTICAL); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 783 | } |
| 784 | #endif |
| 785 | |
| 786 | static void viewer_draw(int col) |
| 787 | { |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 788 | int i, j, k, line_len, line_width, resynch_move, spaces, left_col=0; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 789 | int width, extra_spaces, indent_spaces, spaces_per_word; |
| 790 | bool multiple_spacing, line_is_short; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 791 | unsigned short ch; |
| 792 | unsigned char *str, *oldstr; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 793 | unsigned char *line_begin; |
| 794 | unsigned char *line_end; |
| 795 | unsigned char c; |
| 796 | unsigned char scratch_buffer[MAX_COLUMNS + 1]; |
Linus Nielsen Feltzing | f954a53 | 2006-03-01 13:31:36 +0000 | [diff] [blame] | 797 | unsigned char utf8_buffer[MAX_COLUMNS*4 + 1]; |
Linus Nielsen Feltzing | f954a53 | 2006-03-01 13:31:36 +0000 | [diff] [blame] | 798 | unsigned char *endptr; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 799 | |
| 800 | /* If col==-1 do all calculations but don't display */ |
| 801 | if (col != -1) { |
| 802 | #ifdef HAVE_LCD_BITMAP |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 803 | left_col = prefs.need_scrollbar? SCROLLBAR_WIDTH:0; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 804 | #else |
| 805 | left_col = 0; |
| 806 | #endif |
| 807 | rb->lcd_clear_display(); |
| 808 | } |
| 809 | max_line_len = 0; |
| 810 | line_begin = line_end = screen_top_ptr; |
| 811 | |
| 812 | for (i = 0; i < display_lines; i++) { |
| 813 | if (BUFFER_OOB(line_end)) |
| 814 | break; /* Happens after display last line at BUFFER_EOF() */ |
| 815 | |
| 816 | line_begin = line_end; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 817 | line_end = find_next_line(line_begin, &line_is_short); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 818 | |
| 819 | if (line_end == NULL) { |
| 820 | if (BUFFER_EOF()) { |
| 821 | if (i < display_lines - 1 && !BUFFER_BOF()) { |
| 822 | if (col != -1) |
| 823 | rb->lcd_clear_display(); |
| 824 | |
| 825 | for (; i < display_lines - 1; i++) |
| 826 | viewer_scroll_up(); |
| 827 | |
| 828 | line_begin = line_end = screen_top_ptr; |
| 829 | i = -1; |
| 830 | continue; |
| 831 | } |
| 832 | else { |
| 833 | line_end = buffer_end; |
| 834 | } |
| 835 | } |
| 836 | else { |
| 837 | resynch_move = read_and_synch(1); /* Read block & move ptrs */ |
| 838 | line_begin -= resynch_move; |
| 839 | if (i > 0) |
| 840 | next_line_ptr -= resynch_move; |
| 841 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 842 | line_end = find_next_line(line_begin, NULL); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 843 | if (line_end == NULL) /* Should not really happen */ |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | line_len = line_end - line_begin; |
| 848 | |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 849 | /* calculate line_len */ |
| 850 | str = oldstr = line_begin; |
| 851 | j = -1; |
| 852 | while (str < line_end) { |
| 853 | oldstr = str; |
| 854 | str = crop_at_width(str); |
| 855 | j++; |
| 856 | } |
| 857 | line_width = j*draw_columns; |
| 858 | while (oldstr < line_end) { |
| 859 | oldstr = get_ucs(oldstr, &ch); |
| 860 | line_width += glyph_width(ch); |
| 861 | } |
| 862 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 863 | if (prefs.line_mode == JOIN) { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 864 | if (line_begin[0] == 0) { |
| 865 | line_begin++; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 866 | if (prefs.word_mode == CHOP) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 867 | line_end++; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 868 | else |
| 869 | line_len--; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 870 | } |
| 871 | for (j=k=spaces=0; j < line_len; j++) { |
| 872 | if (k == MAX_COLUMNS) |
| 873 | break; |
| 874 | |
| 875 | c = line_begin[j]; |
| 876 | switch (c) { |
| 877 | case ' ': |
| 878 | spaces++; |
| 879 | break; |
| 880 | case 0: |
| 881 | spaces = 0; |
| 882 | scratch_buffer[k++] = ' '; |
| 883 | break; |
| 884 | default: |
| 885 | while (spaces) { |
| 886 | spaces--; |
| 887 | scratch_buffer[k++] = ' '; |
| 888 | if (k == MAX_COLUMNS - 1) |
| 889 | break; |
| 890 | } |
| 891 | scratch_buffer[k++] = c; |
| 892 | break; |
| 893 | } |
| 894 | } |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 895 | if (col != -1) { |
| 896 | scratch_buffer[k] = 0; |
| 897 | endptr = rb->iso_decode(scratch_buffer + col, utf8_buffer, |
Linus Nielsen Feltzing | 73c283e | 2006-08-01 17:48:38 +0000 | [diff] [blame] | 898 | prefs.encoding, draw_columns/glyph_width('i')); |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 899 | *endptr = 0; |
| 900 | } |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 901 | } |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 902 | else if (prefs.line_mode == REFLOW) { |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 903 | if (line_begin[0] == 0) { |
| 904 | line_begin++; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 905 | if (prefs.word_mode == CHOP) |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 906 | line_end++; |
| 907 | else |
| 908 | line_len--; |
| 909 | } |
| 910 | |
| 911 | indent_spaces = 0; |
| 912 | if (!line_is_short) { |
| 913 | multiple_spacing = false; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 914 | width=spaces=0; |
| 915 | for (str = line_begin; str < line_end; ) { |
| 916 | str = get_ucs(str, &ch); |
| 917 | switch (ch) { |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 918 | case ' ': |
| 919 | case 0: |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 920 | if ((str == line_begin) && (prefs.word_mode==WRAP)) |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 921 | /* special case: indent the paragraph, |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 922 | * don't count spaces */ |
| 923 | indent_spaces = par_indent_spaces; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 924 | else if (!multiple_spacing) |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 925 | spaces++; |
| 926 | multiple_spacing = true; |
| 927 | break; |
| 928 | default: |
| 929 | multiple_spacing = false; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 930 | width += glyph_width(ch); |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 931 | break; |
| 932 | } |
| 933 | } |
| 934 | if (multiple_spacing) spaces--; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 935 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 936 | if (spaces) { |
| 937 | /* total number of spaces to insert between words */ |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 938 | extra_spaces = (draw_columns-width)/glyph_width(' ') |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 939 | - indent_spaces; |
| 940 | /* number of spaces between each word*/ |
| 941 | spaces_per_word = extra_spaces / spaces; |
| 942 | /* number of words with n+1 spaces (to fill up) */ |
| 943 | extra_spaces = extra_spaces % spaces; |
| 944 | if (spaces_per_word > 2) { /* too much spacing is awful */ |
| 945 | spaces_per_word = 3; |
| 946 | extra_spaces = 0; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 947 | } |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 948 | } else { /* this doesn't matter much... no spaces anyway */ |
| 949 | spaces_per_word = extra_spaces = 0; |
| 950 | } |
| 951 | } else { /* end of a paragraph: don't fill line */ |
| 952 | spaces_per_word = 1; |
| 953 | extra_spaces = 0; |
| 954 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 955 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 956 | multiple_spacing = false; |
| 957 | for (j=k=spaces=0; j < line_len; j++) { |
| 958 | if (k == MAX_COLUMNS) |
| 959 | break; |
| 960 | |
| 961 | c = line_begin[j]; |
| 962 | switch (c) { |
| 963 | case ' ': |
| 964 | case 0: |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 965 | if (j==0 && prefs.word_mode==WRAP) { /* indent paragraph */ |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 966 | for (j=0; j<par_indent_spaces; j++) |
| 967 | scratch_buffer[k++] = ' '; |
| 968 | j=0; |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 969 | } |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 970 | else if (!multiple_spacing) { |
| 971 | for (width = spaces<extra_spaces ? -1:0; width < spaces_per_word; width++) |
| 972 | scratch_buffer[k++] = ' '; |
| 973 | spaces++; |
| 974 | } |
| 975 | multiple_spacing = true; |
| 976 | break; |
| 977 | default: |
| 978 | scratch_buffer[k++] = c; |
| 979 | multiple_spacing = false; |
| 980 | break; |
| 981 | } |
| 982 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 983 | |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 984 | if (col != -1) { |
| 985 | scratch_buffer[k] = 0; |
| 986 | endptr = rb->iso_decode(scratch_buffer + col, utf8_buffer, |
| 987 | prefs.encoding, k-col); |
| 988 | *endptr = 0; |
| 989 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 990 | } |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 991 | else { /* prefs.line_mode != JOIN && prefs.line_mode != REFLOW */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 992 | if (col != -1) |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 993 | if (line_width > col) { |
| 994 | str = oldstr = line_begin; |
| 995 | k = col; |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 996 | width = 0; |
Karl Kurbjun | ca8fa65 | 2006-06-03 07:04:02 +0000 | [diff] [blame] | 997 | while( (width<draw_columns) && (oldstr<line_end) ) |
| 998 | { |
| 999 | oldstr = get_ucs(oldstr, &ch); |
Mark Arigo | 073d73b | 2006-07-25 20:49:53 +0000 | [diff] [blame] | 1000 | if (k > 0) { |
| 1001 | k -= glyph_width(ch); |
| 1002 | line_begin = oldstr; |
| 1003 | } else { |
| 1004 | width += glyph_width(ch); |
| 1005 | } |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1006 | } |
Karl Kurbjun | ca8fa65 | 2006-06-03 07:04:02 +0000 | [diff] [blame] | 1007 | |
| 1008 | if(prefs.view_mode==WIDE) |
| 1009 | endptr = rb->iso_decode(line_begin, utf8_buffer, |
| 1010 | prefs.encoding, oldstr-line_begin); |
| 1011 | else |
| 1012 | endptr = rb->iso_decode(line_begin, utf8_buffer, |
| 1013 | prefs.encoding, line_end-line_begin); |
Linus Nielsen Feltzing | f954a53 | 2006-03-01 13:31:36 +0000 | [diff] [blame] | 1014 | *endptr = 0; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1017 | if (col != -1 && line_width > col) |
| 1018 | #ifdef HAVE_LCD_BITMAP |
| 1019 | rb->lcd_putsxy(left_col, i*pf->height, utf8_buffer); |
| 1020 | #else |
| 1021 | rb->lcd_puts(left_col, i, utf8_buffer); |
| 1022 | #endif |
| 1023 | if (line_width > max_line_len) |
| 1024 | max_line_len = line_width; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1025 | |
| 1026 | if (i == 0) |
| 1027 | next_line_ptr = line_end; |
| 1028 | } |
| 1029 | next_screen_ptr = line_end; |
| 1030 | if (BUFFER_OOB(next_screen_ptr)) |
| 1031 | next_screen_ptr = NULL; |
| 1032 | |
| 1033 | #ifdef HAVE_LCD_BITMAP |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1034 | next_screen_to_draw_ptr = prefs.page_mode==OVERLAP? line_begin: next_screen_ptr; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1035 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1036 | if (prefs.need_scrollbar) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1037 | viewer_scrollbar(); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1038 | #else |
| 1039 | next_screen_to_draw_ptr = next_screen_ptr; |
| 1040 | #endif |
Jens Arnold | f9b90e9 | 2007-04-06 22:55:00 +0000 | [diff] [blame] | 1041 | |
| 1042 | if (col != -1) |
| 1043 | rb->lcd_update(); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static void viewer_top(void) |
| 1047 | { |
| 1048 | /* Read top of file into buffer |
| 1049 | and point screen pointer to top */ |
Marcoen Hirschberg | cb62c97 | 2007-11-19 16:46:39 +0000 | [diff] [blame] | 1050 | if (file_pos != 0) |
| 1051 | { |
| 1052 | file_pos = 0; |
| 1053 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1054 | fill_buffer(0, buffer, buffer_size); |
Marcoen Hirschberg | cb62c97 | 2007-11-19 16:46:39 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1057 | screen_top_ptr = buffer; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | static void viewer_bottom(void) |
| 1061 | { |
| 1062 | /* Read bottom of file into buffer |
| 1063 | and point screen pointer to bottom */ |
| 1064 | long last_sectors; |
| 1065 | |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1066 | if (file_size > buffer_size) { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1067 | /* Find last buffer in file, round up to next sector boundary */ |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1068 | last_sectors = file_size - buffer_size + SMALL_BLOCK_SIZE; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1069 | last_sectors /= SMALL_BLOCK_SIZE; |
| 1070 | last_sectors *= SMALL_BLOCK_SIZE; |
| 1071 | } |
| 1072 | else { |
| 1073 | last_sectors = 0; |
| 1074 | } |
Marcoen Hirschberg | cb62c97 | 2007-11-19 16:46:39 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (file_pos != last_sectors) |
| 1077 | { |
| 1078 | file_pos = last_sectors; |
| 1079 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1080 | fill_buffer(last_sectors, buffer, buffer_size); |
Marcoen Hirschberg | cb62c97 | 2007-11-19 16:46:39 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1083 | screen_top_ptr = buffer_end-1; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1086 | #ifdef HAVE_LCD_BITMAP |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1087 | static void init_need_scrollbar(void) { |
| 1088 | /* Call viewer_draw in quiet mode to initialize next_screen_ptr, |
| 1089 | and thus ONE_SCREEN_FITS_ALL(), and thus NEED_SCROLLBAR() */ |
| 1090 | viewer_draw(-1); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1091 | prefs.need_scrollbar = NEED_SCROLLBAR(); |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1092 | draw_columns = prefs.need_scrollbar? display_columns-SCROLLBAR_WIDTH : display_columns; |
| 1093 | par_indent_spaces = draw_columns/(5*glyph_width(' ')); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1094 | } |
Daniel Stenberg | 30dbb95 | 2006-01-18 11:30:46 +0000 | [diff] [blame] | 1095 | #else |
| 1096 | #define init_need_scrollbar() |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1097 | #endif |
| 1098 | |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1099 | static bool viewer_init(void) |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1100 | { |
| 1101 | #ifdef HAVE_LCD_BITMAP |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1102 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 1103 | pf = rb->font_get(FONT_UI); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1104 | |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 1105 | display_lines = LCD_HEIGHT / pf->height; |
Peter D'Hoye | 2076363 | 2006-07-15 13:53:18 +0000 | [diff] [blame] | 1106 | draw_columns = display_columns = LCD_WIDTH; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1107 | #else |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 1108 | /* REAL fixed pitch :) all chars use up 1 cell */ |
Björn Stenberg | f975944 | 2002-10-02 11:37:37 +0000 | [diff] [blame] | 1109 | display_lines = 2; |
Linus Nielsen Feltzing | eec2b7e | 2005-05-13 13:32:57 +0000 | [diff] [blame] | 1110 | draw_columns = display_columns = 11; |
| 1111 | par_indent_spaces = 2; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1112 | #endif |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1113 | |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1114 | fd = rb->open(file_name, O_RDONLY); |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1115 | if (fd==-1) |
| 1116 | return false; |
| 1117 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1118 | file_size = rb->filesize(fd); |
| 1119 | if (file_size==-1) |
| 1120 | return false; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1121 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1122 | /* Init mac_text value used in processing buffer */ |
| 1123 | mac_text = false; |
| 1124 | |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1125 | return true; |
| 1126 | } |
| 1127 | |
Marcoen Hirschberg | 3f6461a | 2007-11-23 09:11:09 +0000 | [diff] [blame] | 1128 | static void viewer_default_settings(void) |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1129 | { |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1130 | prefs.word_mode = WRAP; |
| 1131 | prefs.line_mode = NORMAL; |
| 1132 | prefs.view_mode = NARROW; |
| 1133 | prefs.scroll_mode = PAGE; |
| 1134 | #ifdef HAVE_LCD_BITMAP |
| 1135 | prefs.page_mode = NO_OVERLAP; |
| 1136 | prefs.scrollbar_mode = SB_OFF; |
| 1137 | #endif |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1138 | prefs.autoscroll_speed = 1; |
Marcoen Hirschberg | 3f6461a | 2007-11-23 09:11:09 +0000 | [diff] [blame] | 1139 | /* Set codepage to system default */ |
| 1140 | prefs.encoding = rb->global_settings->default_codepage; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static void viewer_load_settings(void) /* same name as global, but not the same file.. */ |
| 1144 | { |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1145 | int settings_fd, i; |
| 1146 | struct bookmark_file_data *data; |
Marcoen Hirschberg | 77af93b | 2007-11-19 20:51:38 +0000 | [diff] [blame] | 1147 | struct bookmarked_file_info this_bookmark; |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1148 | |
| 1149 | /* read settings file */ |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1150 | settings_fd=rb->open(SETTINGS_FILE, O_RDONLY); |
Dave Chapman | 14ac7ca | 2007-11-22 00:32:50 +0000 | [diff] [blame] | 1151 | if ((settings_fd >= 0) && (rb->filesize(settings_fd) == sizeof(struct preferences))) |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1152 | { |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1153 | rb->read(settings_fd, &prefs, sizeof(struct preferences)); |
| 1154 | rb->close(settings_fd); |
Marcoen Hirschberg | 3f6461a | 2007-11-23 09:11:09 +0000 | [diff] [blame] | 1155 | } |
| 1156 | else |
| 1157 | { |
| 1158 | /* load default settings if there is no settings file */ |
| 1159 | viewer_default_settings(); |
| 1160 | } |
| 1161 | |
| 1162 | rb->memcpy(&old_prefs, &prefs, sizeof(struct preferences)); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1163 | |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1164 | data = (struct bookmark_file_data*)buffer; /* grab the text buffer */ |
| 1165 | data->bookmarked_files_count = 0; |
| 1166 | |
| 1167 | /* read bookmarks if file exists */ |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 1168 | settings_fd = rb->open(BOOKMARKS_FILE, O_RDONLY); |
| 1169 | if (settings_fd >= 0) |
| 1170 | { |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1171 | /* figure out how many items to read */ |
| 1172 | rb->read(settings_fd, &data->bookmarked_files_count, sizeof(signed int)); |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 1173 | if (data->bookmarked_files_count > MAX_BOOKMARKED_FILES) |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1174 | data->bookmarked_files_count = MAX_BOOKMARKED_FILES; |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 1175 | rb->read(settings_fd, data->bookmarks, |
| 1176 | sizeof(struct bookmarked_file_info) * data->bookmarked_files_count); |
| 1177 | rb->close(settings_fd); |
Marcoen Hirschberg | 77af93b | 2007-11-19 20:51:38 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | file_pos = 0; |
| 1181 | screen_top_ptr = buffer; |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1182 | |
| 1183 | /* check if current file is in list */ |
| 1184 | for (i=0; i < data->bookmarked_files_count; i++) |
| 1185 | { |
| 1186 | if (!rb->strcmp(file_name, data->bookmarks[i].filename)) |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 1187 | { |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1188 | int screen_pos = data->bookmarks[i].file_position + data->bookmarks[i].top_ptr_pos; |
| 1189 | int screen_top = screen_pos % buffer_size; |
| 1190 | file_pos = screen_pos - screen_top; |
| 1191 | screen_top_ptr = buffer + screen_top; |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | } |
| 1194 | } |
| 1195 | |
Marcoen Hirschberg | 77af93b | 2007-11-19 20:51:38 +0000 | [diff] [blame] | 1196 | this_bookmark.file_position = file_pos; |
| 1197 | this_bookmark.top_ptr_pos = screen_top_ptr - buffer; |
| 1198 | |
| 1199 | rb->memset(&this_bookmark.filename[0],0,MAX_PATH); |
| 1200 | rb->strcpy(this_bookmark.filename,file_name); |
| 1201 | |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1202 | /* prevent potential slot overflow */ |
| 1203 | if (i >= data->bookmarked_files_count) |
| 1204 | { |
| 1205 | if (i < MAX_BOOKMARKED_FILES) |
| 1206 | data->bookmarked_files_count++; |
| 1207 | else |
| 1208 | i = MAX_BOOKMARKED_FILES-1; |
| 1209 | } |
| 1210 | |
| 1211 | /* write bookmark file with spare slot in first position |
| 1212 | to be filled in by viewer_save_settings */ |
| 1213 | settings_fd = rb->open(BOOKMARKS_FILE, O_WRONLY|O_CREAT); |
| 1214 | if (settings_fd >=0 ) |
| 1215 | { |
Marcoen Hirschberg | 77af93b | 2007-11-19 20:51:38 +0000 | [diff] [blame] | 1216 | /* write count */ |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1217 | rb->write (settings_fd, &data->bookmarked_files_count, sizeof(signed int)); |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1218 | |
Marcoen Hirschberg | 77af93b | 2007-11-19 20:51:38 +0000 | [diff] [blame] | 1219 | /* write the current bookmark */ |
| 1220 | rb->write (settings_fd, &this_bookmark, sizeof(struct bookmarked_file_info)); |
| 1221 | |
| 1222 | /* write everything that was before this bookmark */ |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1223 | rb->write (settings_fd, data->bookmarks, sizeof(struct bookmarked_file_info)*i); |
Marcoen Hirschberg | 77af93b | 2007-11-19 20:51:38 +0000 | [diff] [blame] | 1224 | |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1225 | rb->close(settings_fd); |
| 1226 | } |
| 1227 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1228 | buffer_end = BUFFER_END(); /* Update whenever file_pos changes */ |
| 1229 | |
Robert Kukla | c9a00ab | 2007-09-13 00:18:28 +0000 | [diff] [blame] | 1230 | if (BUFFER_OOB(screen_top_ptr)) |
| 1231 | { |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1232 | screen_top_ptr = buffer; |
| 1233 | } |
| 1234 | |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1235 | fill_buffer(file_pos, buffer, buffer_size); |
Marcoen Hirschberg | 4c62c00 | 2007-11-19 16:02:45 +0000 | [diff] [blame] | 1236 | |
| 1237 | /* remember the current position */ |
| 1238 | start_position = file_pos + screen_top_ptr - buffer; |
Marcoen Hirschberg | 3f6461a | 2007-11-23 09:11:09 +0000 | [diff] [blame] | 1239 | |
| 1240 | init_need_scrollbar(); |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1241 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 1242 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1243 | static void viewer_save_settings(void)/* same name as global, but not the same file.. */ |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1244 | { |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1245 | int settings_fd; |
Robert Kukla | 2ae0d63 | 2007-08-13 13:53:33 +0000 | [diff] [blame] | 1246 | |
Marcoen Hirschberg | 2b30bf9 | 2007-11-19 20:51:42 +0000 | [diff] [blame] | 1247 | /* save the viewer settings if they have been changed */ |
| 1248 | if (rb->memcmp(&prefs, &old_prefs, sizeof(struct preferences))) |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 1249 | { |
Marcoen Hirschberg | 2b30bf9 | 2007-11-19 20:51:42 +0000 | [diff] [blame] | 1250 | settings_fd = rb->creat(SETTINGS_FILE); /* create the settings file */ |
| 1251 | |
| 1252 | if (settings_fd >= 0 ) |
| 1253 | { |
| 1254 | rb->write (settings_fd, &prefs, sizeof(struct preferences)); |
| 1255 | rb->close(settings_fd); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | /* save the bookmark if the position has changed */ |
| 1260 | if (file_pos + screen_top_ptr - buffer != start_position) |
| 1261 | { |
| 1262 | settings_fd = rb->open(BOOKMARKS_FILE, O_WRONLY|O_CREAT); |
| 1263 | |
| 1264 | if (settings_fd >= 0 ) |
| 1265 | { |
| 1266 | struct bookmarked_file_info b; |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1267 | b.file_position = file_pos + screen_top_ptr - buffer; |
| 1268 | b.top_ptr_pos = 0; /* this is only kept for legassy reasons */ |
Marcoen Hirschberg | 2b30bf9 | 2007-11-19 20:51:42 +0000 | [diff] [blame] | 1269 | rb->memset(&b.filename[0],0,MAX_PATH); |
| 1270 | rb->strcpy(b.filename,file_name); |
| 1271 | rb->PREFIX(lseek)(settings_fd,sizeof(signed int),SEEK_SET); |
| 1272 | rb->write (settings_fd, &b, sizeof(struct bookmarked_file_info)); |
| 1273 | rb->close(settings_fd); |
| 1274 | } |
Jonathan Gordon | 09a990c | 2006-09-18 15:31:51 +0000 | [diff] [blame] | 1275 | } |
Zakk Roberts | de23679 | 2006-02-16 01:18:21 +0000 | [diff] [blame] | 1276 | } |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1277 | |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1278 | static void viewer_exit(void *parameter) |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1279 | { |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1280 | (void)parameter; |
| 1281 | |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1282 | viewer_save_settings(); |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1283 | rb->close(fd); |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1286 | static int col_limit(int col) |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1287 | { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1288 | if (col < 0) |
| 1289 | col = 0; |
| 1290 | else |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1291 | if (col > max_line_len - 2*glyph_width('o')) |
| 1292 | col = max_line_len - 2*glyph_width('o'); |
Björn Stenberg | f975944 | 2002-10-02 11:37:37 +0000 | [diff] [blame] | 1293 | |
| 1294 | return col; |
| 1295 | } |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1296 | |
| 1297 | /* settings helper functions */ |
| 1298 | |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1299 | static bool encoding_setting(void) |
| 1300 | { |
| 1301 | static const struct opt_items names[] = { |
| 1302 | {"ISO-8859-1", -1}, |
| 1303 | {"ISO-8859-7", -1}, |
| 1304 | {"ISO-8859-8", -1}, |
| 1305 | {"CP1251", -1}, |
| 1306 | {"ISO-8859-11", -1}, |
| 1307 | {"ISO-8859-6", -1}, |
| 1308 | {"ISO-8859-9", -1}, |
| 1309 | {"ISO-8859-2", -1}, |
Marcoen Hirschberg | ded94e6 | 2007-10-01 17:16:31 +0000 | [diff] [blame] | 1310 | {"CP1250", -1}, |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1311 | {"SJIS", -1}, |
| 1312 | {"GB-2312", -1}, |
| 1313 | {"KSX-1001", -1}, |
| 1314 | {"BIG5", -1}, |
| 1315 | {"UTF-8", -1}, |
| 1316 | }; |
| 1317 | |
| 1318 | return rb->set_option("Encoding", &prefs.encoding, INT, names, |
| 1319 | sizeof(names) / sizeof(names[0]), NULL); |
| 1320 | } |
| 1321 | |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1322 | static bool word_wrap_setting(void) |
| 1323 | { |
| 1324 | static const struct opt_items names[] = { |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1325 | {"On", -1}, |
| 1326 | {"Off (Chop Words)", -1}, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1327 | }; |
| 1328 | |
| 1329 | return rb->set_option("Word Wrap", &prefs.word_mode, INT, |
| 1330 | names, 2, NULL); |
| 1331 | } |
| 1332 | |
| 1333 | static bool line_mode_setting(void) |
| 1334 | { |
| 1335 | static const struct opt_items names[] = { |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1336 | {"Normal", -1}, |
| 1337 | {"Join Lines", -1}, |
| 1338 | {"Expand Lines", -1}, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1339 | #ifdef HAVE_LCD_BITMAP |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1340 | {"Reflow Lines", -1}, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1341 | #endif |
| 1342 | }; |
| 1343 | |
| 1344 | return rb->set_option("Line Mode", &prefs.line_mode, INT, names, |
| 1345 | sizeof(names) / sizeof(names[0]), NULL); |
| 1346 | } |
| 1347 | |
| 1348 | static bool view_mode_setting(void) |
| 1349 | { |
| 1350 | static const struct opt_items names[] = { |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1351 | {"No (Narrow)", -1}, |
| 1352 | {"Yes", -1}, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1353 | }; |
Jonathan Gordon | ef6d394 | 2006-09-17 14:04:58 +0000 | [diff] [blame] | 1354 | bool ret; |
| 1355 | ret = rb->set_option("Wide View", &prefs.view_mode, INT, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1356 | names , 2, NULL); |
Jonathan Gordon | ef6d394 | 2006-09-17 14:04:58 +0000 | [diff] [blame] | 1357 | if (prefs.view_mode == NARROW) |
| 1358 | col = 0; |
| 1359 | return ret; |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static bool scroll_mode_setting(void) |
| 1363 | { |
| 1364 | static const struct opt_items names[] = { |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1365 | {"Scroll by Page", -1}, |
| 1366 | {"Scroll by Line", -1}, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1367 | }; |
| 1368 | |
| 1369 | return rb->set_option("Scroll Mode", &prefs.scroll_mode, INT, |
| 1370 | names, 2, NULL); |
| 1371 | } |
| 1372 | |
| 1373 | #ifdef HAVE_LCD_BITMAP |
| 1374 | static bool page_mode_setting(void) |
| 1375 | { |
| 1376 | static const struct opt_items names[] = { |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1377 | {"No", -1}, |
| 1378 | {"Yes", -1}, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1379 | }; |
| 1380 | |
| 1381 | return rb->set_option("Overlap Pages", &prefs.page_mode, INT, |
| 1382 | names, 2, NULL); |
| 1383 | } |
| 1384 | |
| 1385 | static bool scrollbar_setting(void) |
| 1386 | { |
| 1387 | static const struct opt_items names[] = { |
Jens Arnold | b4efbdc | 2006-05-05 17:49:27 +0000 | [diff] [blame] | 1388 | {"Off", -1}, |
| 1389 | {"On", -1} |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1390 | }; |
| 1391 | |
| 1392 | return rb->set_option("Show Scrollbar", &prefs.scrollbar_mode, INT, |
| 1393 | names, 2, NULL); |
| 1394 | } |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1395 | #endif |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1396 | |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1397 | static bool autoscroll_speed_setting(void) |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1398 | { |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1399 | return rb->set_int("Auto-scroll Speed", "", UNIT_INT, |
| 1400 | &prefs.autoscroll_speed, NULL, 1, 1, 10, NULL); |
| 1401 | } |
| 1402 | |
| 1403 | static bool viewer_options_menu(void) |
| 1404 | { |
| 1405 | int m; |
| 1406 | bool result; |
Hristo Kovachev | e16339e | 2006-04-04 12:04:15 +0000 | [diff] [blame] | 1407 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1408 | static const struct menu_item items[] = { |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1409 | {"Encoding", encoding_setting }, |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1410 | {"Word Wrap", word_wrap_setting }, |
| 1411 | {"Line Mode", line_mode_setting }, |
| 1412 | {"Wide View", view_mode_setting }, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1413 | #ifdef HAVE_LCD_BITMAP |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1414 | {"Show Scrollbar", scrollbar_setting }, |
| 1415 | {"Overlap Pages", page_mode_setting }, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1416 | #endif |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1417 | {"Scroll Mode", scroll_mode_setting}, |
| 1418 | {"Auto-Scroll Speed", autoscroll_speed_setting }, |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1419 | }; |
Jonathan Gordon | 77a458a | 2007-05-08 11:55:43 +0000 | [diff] [blame] | 1420 | m = menu_init(rb, items, sizeof(items) / sizeof(*items), |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1421 | NULL, NULL, NULL, NULL); |
| 1422 | |
Jonathan Gordon | 77a458a | 2007-05-08 11:55:43 +0000 | [diff] [blame] | 1423 | result = menu_run(m); |
| 1424 | menu_exit(m); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1425 | #ifdef HAVE_LCD_BITMAP |
Jonathan Gordon | f83d6eb | 2007-03-11 08:05:05 +0000 | [diff] [blame] | 1426 | |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1427 | /* Show-scrollbar mode for current view-width mode */ |
Marcoen Hirschberg | b3f155d | 2007-11-23 15:42:08 +0000 | [diff] [blame] | 1428 | init_need_scrollbar(); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1429 | #endif |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1430 | return result; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1431 | } |
Hristo Kovachev | e16339e | 2006-04-04 12:04:15 +0000 | [diff] [blame] | 1432 | |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1433 | static void viewer_menu(void) |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1434 | { |
| 1435 | int m; |
| 1436 | int result; |
| 1437 | static const struct menu_item items[] = { |
| 1438 | {"Quit", NULL }, |
| 1439 | {"Viewer Options", NULL }, |
| 1440 | {"Show Playback Menu", NULL }, |
| 1441 | {"Return", NULL }, |
| 1442 | }; |
| 1443 | |
Jonathan Gordon | 77a458a | 2007-05-08 11:55:43 +0000 | [diff] [blame] | 1444 | m = menu_init(rb, items, sizeof(items) / sizeof(*items), NULL, NULL, NULL, NULL); |
| 1445 | result=menu_show(m); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1446 | switch (result) |
| 1447 | { |
| 1448 | case 0: /* quit */ |
Jonathan Gordon | 77a458a | 2007-05-08 11:55:43 +0000 | [diff] [blame] | 1449 | menu_exit(m); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1450 | viewer_exit(NULL); |
| 1451 | done = true; |
| 1452 | break; |
| 1453 | case 1: /* change settings */ |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1454 | done = viewer_options_menu(); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | case 2: /* playback control */ |
Jonathan Gordon | 33b785e | 2008-04-23 10:28:34 +0000 | [diff] [blame] | 1457 | playback_control(rb, NULL); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1458 | break; |
| 1459 | case 3: /* return */ |
| 1460 | break; |
| 1461 | } |
Jonathan Gordon | 77a458a | 2007-05-08 11:55:43 +0000 | [diff] [blame] | 1462 | menu_exit(m); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1463 | viewer_draw(col); |
| 1464 | } |
Hristo Kovachev | e16339e | 2006-04-04 12:04:15 +0000 | [diff] [blame] | 1465 | |
Steve Bavin | 6526577 | 2008-05-13 09:57:56 +0000 | [diff] [blame] | 1466 | enum plugin_status plugin_start(const struct plugin_api* api, const void* file) |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1467 | { |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1468 | int button, i, ok; |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1469 | int lastbutton = BUTTON_NONE; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1470 | bool autoscroll = false; |
Jens Arnold | 15a0a17 | 2006-05-04 19:53:26 +0000 | [diff] [blame] | 1471 | long old_tick; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1472 | |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1473 | rb = api; |
Jens Arnold | 15a0a17 | 2006-05-04 19:53:26 +0000 | [diff] [blame] | 1474 | old_tick = *rb->current_tick; |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1475 | |
Marcoen Hirschberg | f8232e7 | 2007-11-23 09:11:11 +0000 | [diff] [blame] | 1476 | /* get the plugin buffer */ |
| 1477 | buffer = rb->plugin_get_buffer((size_t *)&buffer_size); |
| 1478 | |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1479 | if (!file) |
| 1480 | return PLUGIN_ERROR; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1481 | |
Linus Nielsen Feltzing | 7ee4103 | 2005-05-17 13:14:09 +0000 | [diff] [blame] | 1482 | file_name = file; |
| 1483 | ok = viewer_init(); |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1484 | if (!ok) { |
Robert Kukla | 2ae0d63 | 2007-08-13 13:53:33 +0000 | [diff] [blame] | 1485 | rb->splash(HZ, "Error opening file."); |
| 1486 | return PLUGIN_ERROR; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Marcoen Hirschberg | 3f6461a | 2007-11-23 09:11:09 +0000 | [diff] [blame] | 1489 | viewer_load_settings(); /* load the preferences and bookmark */ |
Jonathan Gordon | c4c0583 | 2006-09-17 10:32:49 +0000 | [diff] [blame] | 1490 | |
Nils Wallménius | d969a42 | 2007-09-23 19:15:56 +0000 | [diff] [blame] | 1491 | #if LCD_DEPTH > 1 |
| 1492 | rb->lcd_set_backdrop(NULL); |
| 1493 | #endif |
| 1494 | |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1495 | viewer_draw(col); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1496 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1497 | while (!done) { |
| 1498 | |
| 1499 | if(autoscroll) |
| 1500 | { |
| 1501 | if(old_tick <= *rb->current_tick - (110-prefs.autoscroll_speed*10)) |
| 1502 | { |
| 1503 | viewer_scroll_down(); |
| 1504 | viewer_draw(col); |
| 1505 | old_tick = *rb->current_tick; |
| 1506 | } |
| 1507 | } |
Jonathan Gordon | c4c0583 | 2006-09-17 10:32:49 +0000 | [diff] [blame] | 1508 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1509 | button = rb->button_get_w_tmo(HZ/10); |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1510 | switch (button) { |
Hristo Kovachev | e16339e | 2006-04-04 12:04:15 +0000 | [diff] [blame] | 1511 | case VIEWER_MENU: |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1512 | viewer_menu(); |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1513 | break; |
| 1514 | |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1515 | case VIEWER_AUTOSCROLL: |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1516 | #ifdef VIEWER_AUTOSCROLL_PRE |
| 1517 | if (lastbutton != VIEWER_AUTOSCROLL_PRE) |
| 1518 | break; |
| 1519 | #endif |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1520 | autoscroll = !autoscroll; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1523 | case VIEWER_PAGE_UP: |
| 1524 | case VIEWER_PAGE_UP | BUTTON_REPEAT: |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1525 | if (prefs.scroll_mode == PAGE) |
| 1526 | { |
| 1527 | /* Page up */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1528 | #ifdef HAVE_LCD_BITMAP |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1529 | for (i = prefs.page_mode==OVERLAP? 1:0; i < display_lines; i++) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1530 | #else |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1531 | for (i = 0; i < display_lines; i++) |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1532 | #endif |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1533 | viewer_scroll_up(); |
| 1534 | } |
| 1535 | else |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1536 | viewer_scroll_up(); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1537 | old_tick = *rb->current_tick; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1538 | viewer_draw(col); |
| 1539 | break; |
| 1540 | |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1541 | case VIEWER_PAGE_DOWN: |
| 1542 | case VIEWER_PAGE_DOWN | BUTTON_REPEAT: |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1543 | if (prefs.scroll_mode == PAGE) |
| 1544 | { |
| 1545 | /* Page down */ |
| 1546 | if (next_screen_ptr != NULL) |
| 1547 | screen_top_ptr = next_screen_to_draw_ptr; |
| 1548 | } |
| 1549 | else |
| 1550 | viewer_scroll_down(); |
| 1551 | old_tick = *rb->current_tick; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1552 | viewer_draw(col); |
| 1553 | break; |
| 1554 | |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1555 | case VIEWER_SCREEN_LEFT: |
| 1556 | case VIEWER_SCREEN_LEFT | BUTTON_REPEAT: |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1557 | if (prefs.view_mode == WIDE) { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1558 | /* Screen left */ |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1559 | col -= draw_columns; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1560 | col = col_limit(col); |
| 1561 | } |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1562 | else { /* prefs.view_mode == NARROW */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1563 | /* Top of file */ |
| 1564 | viewer_top(); |
| 1565 | } |
| 1566 | |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1567 | viewer_draw(col); |
| 1568 | break; |
| 1569 | |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1570 | case VIEWER_SCREEN_RIGHT: |
| 1571 | case VIEWER_SCREEN_RIGHT | BUTTON_REPEAT: |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1572 | if (prefs.view_mode == WIDE) { |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1573 | /* Screen right */ |
Marcoen Hirschberg | 5725b8c | 2006-05-11 20:34:37 +0000 | [diff] [blame] | 1574 | col += draw_columns; |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1575 | col = col_limit(col); |
| 1576 | } |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1577 | else { /* prefs.view_mode == NARROW */ |
Daniel Stenberg | df9b04c | 2003-07-24 09:47:46 +0000 | [diff] [blame] | 1578 | /* Bottom of file */ |
| 1579 | viewer_bottom(); |
| 1580 | } |
| 1581 | |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1582 | viewer_draw(col); |
| 1583 | break; |
Björn Stenberg | f975944 | 2002-10-02 11:37:37 +0000 | [diff] [blame] | 1584 | |
Daniel Stenberg | 1ca465a | 2006-01-27 13:30:57 +0000 | [diff] [blame] | 1585 | #ifdef VIEWER_LINE_UP |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1586 | case VIEWER_LINE_UP: |
| 1587 | case VIEWER_LINE_UP | BUTTON_REPEAT: |
| 1588 | /* Scroll up one line */ |
| 1589 | viewer_scroll_up(); |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1590 | old_tick = *rb->current_tick; |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1591 | viewer_draw(col); |
| 1592 | break; |
| 1593 | |
| 1594 | case VIEWER_LINE_DOWN: |
| 1595 | case VIEWER_LINE_DOWN | BUTTON_REPEAT: |
| 1596 | /* Scroll down one line */ |
| 1597 | if (next_screen_ptr != NULL) |
| 1598 | screen_top_ptr = next_line_ptr; |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1599 | old_tick = *rb->current_tick; |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1600 | viewer_draw(col); |
| 1601 | break; |
Daniel Stenberg | 1ca465a | 2006-01-27 13:30:57 +0000 | [diff] [blame] | 1602 | #endif |
| 1603 | #ifdef VIEWER_COLUMN_LEFT |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1604 | case VIEWER_COLUMN_LEFT: |
| 1605 | case VIEWER_COLUMN_LEFT | BUTTON_REPEAT: |
Mark Arigo | 073d73b | 2006-07-25 20:49:53 +0000 | [diff] [blame] | 1606 | if (prefs.view_mode == WIDE) { |
| 1607 | /* Scroll left one column */ |
| 1608 | col -= glyph_width('o'); |
| 1609 | col = col_limit(col); |
| 1610 | viewer_draw(col); |
| 1611 | } |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | |
| 1614 | case VIEWER_COLUMN_RIGHT: |
| 1615 | case VIEWER_COLUMN_RIGHT | BUTTON_REPEAT: |
Mark Arigo | 073d73b | 2006-07-25 20:49:53 +0000 | [diff] [blame] | 1616 | if (prefs.view_mode == WIDE) { |
| 1617 | /* Scroll right one column */ |
| 1618 | col += glyph_width('o'); |
| 1619 | col = col_limit(col); |
| 1620 | viewer_draw(col); |
| 1621 | } |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1622 | break; |
| 1623 | #endif |
Zakk Roberts | a90f092 | 2006-05-01 06:01:17 +0000 | [diff] [blame] | 1624 | |
Kevin Ferrare | 0e027bd | 2006-06-30 16:43:47 +0000 | [diff] [blame] | 1625 | #ifdef VIEWER_RC_QUIT |
| 1626 | case VIEWER_RC_QUIT: |
| 1627 | #endif |
Zakk Roberts | 07fcf77 | 2006-05-01 05:45:18 +0000 | [diff] [blame] | 1628 | case VIEWER_QUIT: |
| 1629 | viewer_exit(NULL); |
| 1630 | done = true; |
| 1631 | break; |
Jens Arnold | cc8cff2 | 2004-10-16 00:07:43 +0000 | [diff] [blame] | 1632 | |
| 1633 | default: |
| 1634 | if (rb->default_event_handler_ex(button, viewer_exit, NULL) |
| 1635 | == SYS_USB_CONNECTED) |
| 1636 | return PLUGIN_USB_CONNECTED; |
| 1637 | break; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1638 | } |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1639 | if (button != BUTTON_NONE) |
Jonathan Gordon | a7d9859 | 2006-09-17 14:37:59 +0000 | [diff] [blame] | 1640 | { |
Jens Arnold | 2343a19 | 2006-05-03 23:16:53 +0000 | [diff] [blame] | 1641 | lastbutton = button; |
Jonathan Gordon | a7d9859 | 2006-09-17 14:37:59 +0000 | [diff] [blame] | 1642 | rb->yield(); |
| 1643 | } |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1644 | } |
Björn Stenberg | ba371fb | 2003-06-29 16:33:04 +0000 | [diff] [blame] | 1645 | return PLUGIN_OK; |
Daniel Stenberg | 773e7a7 | 2002-09-30 19:05:03 +0000 | [diff] [blame] | 1646 | } |
Hristo Kovachev | e16339e | 2006-04-04 12:04:15 +0000 | [diff] [blame] | 1647 | |
Rob Purchase | 297e050 | 2008-04-27 15:30:19 +0000 | [diff] [blame] | 1648 | |