text_viewer: cleanup & bugfix
[kugel-rb.git] / apps / plugins / text_viewer / text_viewer.c
blob4817710cb3107ee02ccfab6f12fad487cb6540af
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Gilles Roux
11 * 2003 Garrett Derner
12 * 2010 Yoshihisa Uchida
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "plugin.h"
24 #include "tv_action.h"
25 #include "tv_button.h"
26 #include "tv_preferences.h"
28 PLUGIN_HEADER
30 enum plugin_status plugin_start(const void* file)
32 int button;
33 int lastbutton = BUTTON_NONE;
34 bool autoscroll = false;
35 long old_tick;
36 bool done = false;
37 bool display_update = true;
39 old_tick = *rb->current_tick;
41 if (!file)
42 return PLUGIN_ERROR;
44 if (!tv_init(file)) {
45 rb->splash(HZ, "Error opening file");
46 return PLUGIN_ERROR;
49 #if LCD_DEPTH > 1
50 rb->lcd_set_backdrop(NULL);
51 #endif
53 while (!done) {
55 if (display_update)
56 tv_draw();
58 display_update = true;
60 button = rb->button_get_w_tmo(HZ/10);
62 switch (button) {
63 case TV_MENU:
64 #ifdef TV_MENU2
65 case TV_MENU2:
66 #endif
68 unsigned res = tv_menu();
70 if (res != TV_MENU_RESULT_EXIT_MENU)
72 tv_exit(NULL);
73 done = true;
74 if (res == TV_MENU_RESULT_ATTACHED_USB)
75 return PLUGIN_USB_CONNECTED;
78 break;
80 case TV_AUTOSCROLL:
81 #ifdef TV_AUTOSCROLL_PRE
82 if (lastbutton != TV_AUTOSCROLL_PRE)
83 break;
84 #endif
85 autoscroll = !autoscroll;
86 break;
88 case TV_SCROLL_UP:
89 case TV_SCROLL_UP | BUTTON_REPEAT:
90 #ifdef TV_SCROLL_UP2
91 case TV_SCROLL_UP2:
92 case TV_SCROLL_UP2 | BUTTON_REPEAT:
93 #endif
94 tv_scroll_up(TV_VERTICAL_SCROLL_PREFS);
95 old_tick = *rb->current_tick;
96 break;
98 case TV_SCROLL_DOWN:
99 case TV_SCROLL_DOWN | BUTTON_REPEAT:
100 #ifdef TV_PAGE_DOWN2
101 case TV_SCROLL_DOWN2:
102 case TV_SCROLL_DOWN2 | BUTTON_REPEAT:
103 #endif
104 tv_scroll_down(TV_VERTICAL_SCROLL_PREFS);
105 old_tick = *rb->current_tick;
106 break;
108 case TV_SCREEN_LEFT:
109 case TV_SCREEN_LEFT | BUTTON_REPEAT:
110 if (preferences->windows > 1)
112 /* Screen left */
113 tv_scroll_left(TV_HORIZONTAL_SCROLL_PREFS);
115 else { /* prefs->windows == 1 */
116 if (preferences->narrow_mode == NM_PAGE)
118 /* scroll to previous page */
119 tv_scroll_up(TV_VERTICAL_SCROLL_PAGE);
121 else
123 /* Top of file */
124 tv_top();
127 break;
129 case TV_SCREEN_RIGHT:
130 case TV_SCREEN_RIGHT | BUTTON_REPEAT:
131 if (preferences->windows > 1)
133 /* Screen right */
134 tv_scroll_right(TV_HORIZONTAL_SCROLL_PREFS);
136 else { /* prefs->windows == 1 */
137 if (preferences->narrow_mode == NM_PAGE)
139 /* scroll to next page */
140 tv_scroll_down(TV_VERTICAL_SCROLL_PAGE);
142 else
144 /* Bottom of file */
145 tv_bottom();
148 break;
150 #ifdef TV_LINE_UP
151 case TV_LINE_UP:
152 case TV_LINE_UP | BUTTON_REPEAT:
153 /* Scroll up one line */
154 tv_scroll_up(TV_VERTICAL_SCROLL_LINE);
155 old_tick = *rb->current_tick;
156 break;
158 case TV_LINE_DOWN:
159 case TV_LINE_DOWN | BUTTON_REPEAT:
160 /* Scroll down one line */
161 tv_scroll_down(TV_VERTICAL_SCROLL_LINE);
162 old_tick = *rb->current_tick;
163 break;
164 #endif
165 #ifdef TV_COLUMN_LEFT
166 case TV_COLUMN_LEFT:
167 case TV_COLUMN_LEFT | BUTTON_REPEAT:
168 /* Scroll left one column */
169 tv_scroll_left(TV_HORIZONTAL_SCROLL_COLUMN);
170 break;
172 case TV_COLUMN_RIGHT:
173 case TV_COLUMN_RIGHT | BUTTON_REPEAT:
174 /* Scroll right one column */
175 tv_scroll_right(TV_HORIZONTAL_SCROLL_COLUMN);
176 break;
177 #endif
179 #ifdef TV_RC_QUIT
180 case TV_RC_QUIT:
181 #endif
182 case TV_QUIT:
183 #ifdef TV_QUIT2
184 case TV_QUIT2:
185 #endif
186 tv_exit(NULL);
187 done = true;
188 break;
190 case TV_BOOKMARK:
191 tv_add_or_remove_bookmark();
192 break;
194 default:
195 if (rb->default_event_handler_ex(button, tv_exit, NULL)
196 == SYS_USB_CONNECTED)
197 return PLUGIN_USB_CONNECTED;
198 display_update = false;
199 break;
201 if (button != BUTTON_NONE)
203 lastbutton = button;
204 rb->yield();
206 if (autoscroll)
208 if(old_tick <= *rb->current_tick - (110 - preferences->autoscroll_speed * 10))
210 tv_scroll_down(TV_VERTICAL_SCROLL_PREFS);
211 old_tick = *rb->current_tick;
212 display_update = true;
216 return PLUGIN_OK;