Fix buffer size used for realpath() call
[maemo-rb.git] / apps / plugins / text_viewer / tv_display.c
blobd38f1b514c438c4347ba6aa586fe61108720a485
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Yoshihisa Uchida
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "tv_display.h"
23 #include "tv_preferences.h"
26 * display layout
28 * when is defined HAVE_LCD_BITMAP
29 * +-------------------------+
30 * |statusbar (1) |
31 * +-------------------------+
32 * |filename (2) |
33 * +---+---------------------+
34 * |s | |
35 * |c | |
36 * |r | draw area |
37 * |o | |
38 * |l | |
39 * |l | |
40 * |b | |
41 * |a | |
42 * |r | |
43 * |(3)| |
44 * +---+---------------------+
45 * | |scrollbar (4) |
46 * +---+---------------------+
47 * |page (5) |
48 * +-------------------------+
49 * |statusbar (6) |
50 * +-------------------------+
52 * (1) displays when rb->global_settings->statusbar == STATUSBAR_TOP.
53 * (2) displays when preferences->header_mode is HD_PATH.
54 * (3) displays when preferences->vertical_scrollbar is SB_ON.
55 * (4) displays when preferences->horizontal_scrollbar is SB_ON.
56 * (5) displays when preferences->footer_mode is FT_PAGE.
57 * (6) displays when rb->global_settings->statusbar == STATUSBAR_BOTTOM.
60 * when isn't defined HAVE_LCD_BITMAP
61 * +---+---------------------+
62 * | | |
63 * |(7)| draw area |
64 * | | |
65 * +---+---------------------+
66 * (7) bookmark
69 #define TV_SCROLLBAR_WIDTH rb->global_settings->scrollbar_width
70 #define TV_SCROLLBAR_HEIGHT 4
72 #ifndef HAVE_LCD_BITMAP
73 #define TV_BOOKMARK_ICON 0xe101
74 #endif
76 struct tv_rect {
77 int x;
78 int y;
79 int w;
80 int h;
83 static struct viewport vp_info;
84 static struct viewport vp_text;
85 static bool is_initialized_vp = false;
87 static struct screen* display;
89 /* layout */
90 #ifdef HAVE_LCD_BITMAP
91 static struct tv_rect header;
92 static struct tv_rect footer;
93 static struct tv_rect horizontal_scrollbar;
94 static struct tv_rect vertical_scrollbar;
95 #else
96 static struct tv_rect bookmark;
97 #endif
99 static bool show_horizontal_scrollbar;
100 static bool show_vertical_scrollbar;
102 static int display_columns;
103 static int display_rows;
105 static int col_width = 1;
106 static int row_height = 1;
108 static int totalsize;
110 #ifdef HAVE_LCD_BITMAP
112 static void tv_show_header(void)
114 if (preferences->header_mode)
115 display->putsxy(header.x, header.y, preferences->file_name);
118 static void tv_show_footer(const struct tv_screen_pos *pos)
120 unsigned char buf[12];
122 if (preferences->footer_mode)
124 if (pos->line == 0)
125 rb->snprintf(buf, sizeof(buf), "%d", pos->page + 1);
126 else
127 rb->snprintf(buf, sizeof(buf), "%d - %d", pos->page + 1, pos->page + 2);
128 display->putsxy(footer.x, footer.y + 1, buf);
132 static void tv_show_scrollbar(int window, int col, off_t cur_pos, int size)
134 int items;
135 int min_shown;
136 int max_shown;
138 if (show_horizontal_scrollbar)
140 items = preferences->windows * display_columns;
141 min_shown = window * display_columns + col;
142 max_shown = min_shown + display_columns;
144 rb->gui_scrollbar_draw(display,
145 horizontal_scrollbar.x, horizontal_scrollbar.y + 1,
146 horizontal_scrollbar.w, TV_SCROLLBAR_HEIGHT,
147 items, min_shown, max_shown, HORIZONTAL);
150 if (show_vertical_scrollbar)
152 items = (int) totalsize;
153 min_shown = (int) cur_pos;
154 max_shown = min_shown + size;
156 rb->gui_scrollbar_draw(display,
157 vertical_scrollbar.x, vertical_scrollbar.y,
158 TV_SCROLLBAR_WIDTH, vertical_scrollbar.h,
159 items, min_shown, max_shown, VERTICAL);
163 #endif
165 void tv_init_scrollbar(off_t total, bool show_scrollbar)
167 totalsize = total;
168 show_horizontal_scrollbar = (show_scrollbar && preferences->horizontal_scrollbar);
169 show_vertical_scrollbar = (show_scrollbar && preferences->vertical_scrollbar);
172 void tv_show_bookmarks(const int *rows, int count)
174 #ifdef HAVE_LCD_BITMAP
175 display->set_viewport(&vp_text);
176 display->set_drawmode(DRMODE_COMPLEMENT);
177 #endif
179 while (count--)
181 #ifdef HAVE_LCD_BITMAP
182 display->fillrect(0, rows[count] * row_height,
183 vp_text.width, row_height);
184 #else
185 display->putchar(bookmark.x, bookmark.y + rows[count], TV_BOOKMARK_ICON);
186 #endif
188 #ifdef HAVE_LCD_BITMAP
189 display->set_drawmode(DRMODE_SOLID);
190 display->set_viewport(&vp_info);
191 #endif
194 void tv_update_extra(int window, int col, const struct tv_screen_pos *pos, int size)
196 #ifdef HAVE_LCD_BITMAP
197 tv_show_scrollbar(window, col, pos->file_pos, size);
198 tv_show_header();
199 tv_show_footer(pos);
200 #else
201 (void)window;
202 (void)col;
203 (void)pos;
204 (void)size;
205 #endif
208 void tv_draw_text(int row, const unsigned char *text, int offset)
210 int xpos = -offset * col_width;
211 int text_width;
213 if (row < 0 || row >= display_rows)
214 return;
216 if (preferences->alignment == AL_RIGHT)
218 display->getstringsize(text, &text_width, NULL);
219 xpos += ((offset > 0)? vp_text.width * 2 : vp_text.width) - text_width;
222 display->set_viewport(&vp_text);
223 #ifdef HAVE_LCD_BITMAP
224 display->putsxy(xpos, row * row_height, text);
225 #else
226 display->puts(xpos, row, text);
227 #endif
228 display->set_viewport(&vp_info);
231 void tv_start_display(void)
233 display->set_viewport(&vp_info);
234 #ifdef HAVE_LCD_BITMAP
235 display->set_drawmode(DRMODE_SOLID);
236 #endif
238 #if LCD_DEPTH > 1
239 rb->lcd_set_backdrop(NULL);
240 #endif
241 display->clear_viewport();
244 void tv_end_display(void)
246 display->update_viewport();
247 display->set_viewport(NULL);
250 void tv_set_layout(bool show_scrollbar)
252 #ifdef HAVE_LCD_BITMAP
253 int scrollbar_width = (show_scrollbar && preferences->vertical_scrollbar)?
254 TV_SCROLLBAR_WIDTH + 1 : 0;
255 int scrollbar_height = (show_scrollbar && preferences->horizontal_scrollbar)?
256 TV_SCROLLBAR_HEIGHT + 1 : 0;
258 row_height = preferences->font->height;
260 header.x = 0;
261 header.y = 0;
262 header.w = vp_info.width;
263 header.h = (preferences->header_mode)? row_height + 1 : 0;
265 footer.x = 0;
266 footer.w = vp_info.width;
267 footer.h = (preferences->footer_mode)? row_height + 1 : 0;
268 footer.y = vp_info.height - footer.h;
270 horizontal_scrollbar.x = scrollbar_width;
271 horizontal_scrollbar.y = footer.y - scrollbar_height;
272 horizontal_scrollbar.w = vp_info.width - scrollbar_width;
273 horizontal_scrollbar.h = scrollbar_height;
275 vertical_scrollbar.x = 0;
276 vertical_scrollbar.y = header.y + header.h;
277 vertical_scrollbar.w = scrollbar_width;
278 vertical_scrollbar.h = footer.y - vertical_scrollbar.y - scrollbar_height;
280 vp_text = vp_info;
281 vp_text.x += horizontal_scrollbar.x;
282 vp_text.y += vertical_scrollbar.y;
283 vp_text.width = horizontal_scrollbar.w;
284 vp_text.height = vertical_scrollbar.h;
285 #else
286 (void) show_scrollbar;
288 row_height = 1;
290 bookmark.x = 0;
291 bookmark.y = 0;
292 bookmark.w = 1;
293 bookmark.h = vp_info.height;
295 vp_text = vp_info;
296 vp_text.x += 1;
297 vp_text.width -= 1;
298 #endif
300 display_columns = vp_text.width / col_width;
301 display_rows = vp_text.height / row_height;
304 void tv_get_drawarea_info(int *width, int *cols, int *rows)
306 *width = vp_text.width;
307 *cols = display_columns;
308 *rows = display_rows;
311 static void tv_change_viewport(void)
313 #ifdef HAVE_LCD_BITMAP
314 bool show_statusbar = preferences->statusbar;
316 if (is_initialized_vp)
317 rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
318 else
319 is_initialized_vp = true;
321 rb->viewportmanager_theme_enable(SCREEN_MAIN, show_statusbar, &vp_info);
322 vp_info.flags &= ~VP_FLAG_ALIGNMENT_MASK;
323 display->set_viewport(&vp_info);
324 #else
325 if (!is_initialized_vp)
327 rb->viewport_set_defaults(&vp_info, SCREEN_MAIN);
328 is_initialized_vp = true;
330 #endif
333 #ifdef HAVE_LCD_BITMAP
334 static bool tv_set_font(const unsigned char *font)
336 unsigned char path[MAX_PATH];
338 if (font != NULL && *font != '\0')
340 rb->snprintf(path, MAX_PATH, "%s/%s.fnt", FONT_DIR, font);
341 if (rb->font_load(NULL, path) < 0)
343 rb->splash(HZ/2, "font load failed");
344 return false;
347 return true;
349 #endif
351 static int tv_change_preferences(const struct tv_preferences *oldp)
353 #ifdef HAVE_LCD_BITMAP
354 static bool font_changing = false;
355 const unsigned char *font_str;
356 struct tv_preferences new_prefs;
358 font_str = (oldp && !font_changing)? oldp->font_name : rb->global_settings->font_file;
360 /* change font */
361 if (font_changing || rb->strcmp(font_str, preferences->font_name))
363 if (!tv_set_font(preferences->font_name))
366 * tv_set_font(rb->global_settings->font_file) doesn't fail usually.
367 * if it fails, a fatal problem occurs in Rockbox.
369 if (!rb->strcmp(preferences->font_name, rb->global_settings->font_file))
370 return TV_CALLBACK_ERROR;
372 font_changing = true;
373 tv_copy_preferences(&new_prefs);
374 rb->strlcpy(new_prefs.font_name, font_str, MAX_PATH);
376 return (tv_set_preferences(&new_prefs))? TV_CALLBACK_STOP : TV_CALLBACK_ERROR;
378 col_width = 2 * rb->font_get_width(preferences->font, ' ');
379 font_changing = false;
381 #else
382 (void)oldp;
383 #endif
384 tv_change_viewport();
385 return TV_CALLBACK_OK;
388 bool tv_init_display(unsigned char **buf, size_t *size)
390 (void)buf;
391 (void)size;
393 display = rb->screens[SCREEN_MAIN];
394 display->clear_viewport();
396 tv_add_preferences_change_listner(tv_change_preferences);
398 return true;
401 void tv_finalize_display(void)
403 #ifdef HAVE_LCD_BITMAP
404 /* restore font */
405 if (rb->strcmp(rb->global_settings->font_file, preferences->font_name))
407 tv_set_font(rb->global_settings->font_file);
410 /* undo viewport */
411 if (is_initialized_vp)
412 rb->viewportmanager_theme_undo(SCREEN_MAIN, false);
413 #endif
416 bool tv_exist_scrollbar(void)
418 #ifdef HAVE_LCD_BITMAP
419 return true;
420 #else
421 return false;
422 #endif