Update Swedish translation.
[maemo-rb.git] / apps / gui / viewport.c
blobff3a1109369b87fe4582375f3c44473332bd648b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Jonathan Gordon
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 ****************************************************************************/
22 #include <stdlib.h>
23 #include "config.h"
24 #include "lcd.h"
25 #include "lcd-remote.h"
26 #include "font.h"
27 #include "viewport.h"
28 #include "screen_access.h"
29 #include "settings.h"
30 #include "misc.h"
32 /*some short cuts for fg/bg/line selector handling */
33 #ifdef HAVE_LCD_COLOR
34 #define LINE_SEL_FROM_SETTINGS(vp) \
35 do { \
36 vp->lss_pattern = global_settings.lss_color; \
37 vp->lse_pattern = global_settings.lse_color; \
38 vp->lst_pattern = global_settings.lst_color; \
39 } while (0)
40 #define FG_FALLBACK global_settings.fg_color
41 #define BG_FALLBACK global_settings.bg_color
42 #else
43 /* mono/greyscale doesn't have most of the above */
44 #define LINE_SEL_FROM_SETTINGS(vp)
45 #define FG_FALLBACK LCD_DEFAULT_FG
46 #define BG_FALLBACK LCD_DEFAULT_BG
47 #endif
49 /* all below isn't needed for pc tools (i.e. checkwps/wps editor)
50 * only viewport_parse_viewport() is */
51 #ifndef __PCTOOL__
52 #include "sprintf.h"
53 #include "string.h"
54 #include "kernel.h"
55 #include "system.h"
56 #include "statusbar.h"
57 #include "appevents.h"
60 static int statusbar_enabled = 0;
62 #ifdef HAVE_LCD_BITMAP
64 static struct {
65 struct viewport* vp;
66 int active[NB_SCREENS];
67 } ui_vp_info;
69 static struct viewport custom_vp[NB_SCREENS];
71 /* callbacks for GUI_EVENT_* events */
72 static void viewportmanager_ui_vp_changed(void *param);
73 static void statusbar_toggled(void* param);
74 static unsigned viewport_init_ui_vp(void);
75 #endif
76 static void viewportmanager_redraw(void* data);
78 int viewport_get_nb_lines(struct viewport *vp)
80 #ifdef HAVE_LCD_BITMAP
81 return vp->height/font_get(vp->font)->height;
82 #else
83 (void)vp;
84 return 2;
85 #endif
88 static bool showing_bars(enum screen_type screen)
90 if (statusbar_enabled & VP_SB_ONSCREEN(screen))
92 #ifdef HAVE_LCD_BITMAP
93 bool ignore = statusbar_enabled & VP_SB_IGNORE_SETTING(screen);
94 return ignore || (statusbar_position(screen));
95 #else
96 return true;
97 #endif
99 return false;
102 void viewport_set_fullscreen(struct viewport *vp, enum screen_type screen)
104 vp->x = 0;
105 vp->width = screens[screen].lcdwidth;
107 #ifdef HAVE_LCD_BITMAP
108 vp->drawmode = DRMODE_SOLID;
109 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
111 vp->height = screens[screen].lcdheight;
112 if (statusbar_position(screen) != STATUSBAR_BOTTOM && showing_bars(screen))
113 vp->y = STATUSBAR_HEIGHT;
114 else
115 vp->y = 0;
116 #else
117 vp->y = 0;
118 #endif
119 vp->height = screens[screen].lcdheight - (showing_bars(screen)?STATUSBAR_HEIGHT:0);
121 #if LCD_DEPTH > 1
122 #ifdef HAVE_REMOTE_LCD
123 /* We only need this test if there is a remote LCD */
124 if (screen == SCREEN_MAIN)
125 #endif
127 vp->fg_pattern = FG_FALLBACK;
128 vp->bg_pattern = BG_FALLBACK;
129 LINE_SEL_FROM_SETTINGS(vp);
131 #endif
133 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
134 if (screen == SCREEN_REMOTE)
136 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
137 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
139 #endif
142 void viewport_set_defaults(struct viewport *vp, enum screen_type screen)
144 #ifdef HAVE_LCD_BITMAP
145 if (ui_vp_info.active[screen])
146 *vp = ui_vp_info.vp[screen];
147 else
148 #endif
149 viewport_set_fullscreen(vp, screen);
152 void viewportmanager_init(void)
154 #ifdef HAVE_LCD_BITMAP
155 int retval, i;
156 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
157 retval = viewport_init_ui_vp();
158 FOR_NB_SCREENS(i)
159 ui_vp_info.active[i] = retval & BIT_N(i);
160 ui_vp_info.vp = custom_vp;
161 #endif
162 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
165 int viewportmanager_get_statusbar(void)
167 return statusbar_enabled;
170 int viewportmanager_set_statusbar(int enabled)
172 int old = statusbar_enabled;
173 statusbar_enabled = enabled;
174 if (enabled)
176 int i;
177 FOR_NB_SCREENS(i)
179 if (showing_bars(i))
180 gui_statusbar_draw(&statusbars.statusbars[i], true);
182 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
184 else
186 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
188 return old;
191 static void viewportmanager_redraw(void* data)
193 int i;
195 FOR_NB_SCREENS(i)
197 if (showing_bars(i))
198 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
201 #ifdef HAVE_LCD_BITMAP
203 static void statusbar_toggled(void* param)
205 (void)param;
206 /* update vp manager for the new setting and reposition vps
207 * if necessary */
208 viewportmanager_theme_changed(THEME_STATUSBAR);
211 void viewportmanager_theme_changed(int which)
213 int i;
214 #ifdef HAVE_BUTTONBAR
215 if (which & THEME_BUTTONBAR)
216 { /* don't handle further, the custom ui viewport ignores the buttonbar,
217 * as does viewport_set_defaults(), since only lists use it*/
218 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
220 #endif
221 if (which & THEME_UI_VIEWPORT)
223 int retval = viewport_init_ui_vp();
224 /* reset the ui viewport */
225 FOR_NB_SCREENS(i)
226 ui_vp_info.active[i] = retval & BIT_N(i);
228 if (retval != 0)
229 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
230 else
231 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
232 /* and point to it */
233 ui_vp_info.vp = custom_vp;
235 if (which & THEME_STATUSBAR)
237 statusbar_enabled = VP_SB_HIDE_ALL;
239 FOR_NB_SCREENS(i)
241 if (statusbar_position(i) != STATUSBAR_OFF)
242 statusbar_enabled |= VP_SB_ONSCREEN(i);
245 if (statusbar_enabled != VP_SB_HIDE_ALL)
246 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
247 else
248 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
250 /* reposition viewport to fit statusbar, only if not using the ui vp */
252 FOR_NB_SCREENS(i)
254 if (!ui_vp_info.active[i])
255 viewport_set_fullscreen(&custom_vp[i], i);
258 send_event(GUI_EVENT_THEME_CHANGED, NULL);
261 static void viewportmanager_ui_vp_changed(void *param)
263 /* if the user changed the theme, we need to initiate a full redraw */
264 int i;
265 /* cast param to a function */
266 void (*draw_func)(void) = ((void(*)(void))param);
267 /* start with clearing the screen */
268 FOR_NB_SCREENS(i)
269 screens[i].clear_display();
270 /* redraw the statusbar if it was enabled */
271 viewportmanager_set_statusbar(statusbar_enabled);
272 /* call the passed function which will redraw the content of
273 * the current screen */
274 if (param != NULL)
275 draw_func();
276 FOR_NB_SCREENS(i)
277 screens[i].update();
280 void viewport_set_current_vp(struct viewport* vp)
282 if (vp != NULL)
283 ui_vp_info.vp = vp;
284 else
285 ui_vp_info.vp = custom_vp;
287 /* must be done after the assignment above or event handler get old vps */
288 send_event(GUI_EVENT_THEME_CHANGED, NULL);
291 struct viewport* viewport_get_current_vp(void)
293 return ui_vp_info.vp;
296 bool viewport_ui_vp_get_state(enum screen_type screen)
298 return ui_vp_info.active[screen];
302 * (re)parse the UI vp from the settings
303 * - Returns
304 * 0 if no UI vp is used at all
305 * else the bit for the screen (1<<screen) is set
307 static unsigned viewport_init_ui_vp(void)
309 int screen;
310 unsigned ret = 0;
311 char *setting;
312 FOR_NB_SCREENS(screen)
314 #ifdef HAVE_REMOTE_LCD
315 if ((screen == SCREEN_REMOTE))
316 setting = global_settings.remote_ui_vp_config;
317 else
318 #endif
319 setting = global_settings.ui_vp_config;
322 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
323 setting, ',')))
324 viewport_set_fullscreen(&custom_vp[screen], screen);
325 else
326 ret |= BIT_N(screen);
328 return ret;
331 #ifdef HAVE_TOUCHSCREEN
332 /* check if a point (x and y coordinates) are within a viewport */
333 bool viewport_point_within_vp(const struct viewport *vp, int x, int y)
335 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
336 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
337 return (is_x && is_y);
339 #endif /* HAVE_TOUCHSCREEN */
340 #endif /* HAVE_LCD_BITMAP */
341 #endif /* __PCTOOL__ */
343 #ifdef HAVE_LCD_COLOR
344 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
345 #else
346 #define ARG_STRING(_depth) "dddddgg"
347 #endif
349 #ifdef HAVE_LCD_BITMAP
350 const char* viewport_parse_viewport(struct viewport *vp,
351 enum screen_type screen,
352 const char *bufptr,
353 const char separator)
355 /* parse the list to the viewport struct */
356 const char *ptr = bufptr;
357 int depth;
358 uint32_t set = 0;
360 enum {
361 PL_X = 0,
362 PL_Y,
363 PL_WIDTH,
364 PL_HEIGHT,
365 PL_FONT,
366 PL_FG,
367 PL_BG,
370 /* Work out the depth of this display */
371 depth = screens[screen].depth;
372 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
373 if (depth == 1)
375 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
376 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
377 return NULL;
379 else
380 #endif
381 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
382 if (depth >= 2)
384 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
385 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
386 &vp->fg_pattern,&vp->bg_pattern)))
387 return NULL;
389 else
390 #endif
392 #undef ARG_STRING
394 /* X and Y *must* be set */
395 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
396 return NULL;
398 /* fix defaults */
399 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
400 vp->width = screens[screen].lcdwidth - vp->x;
401 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
402 vp->height = screens[screen].lcdheight - vp->y;
404 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
405 if (!LIST_VALUE_PARSED(set, PL_FG))
406 vp->fg_pattern = FG_FALLBACK;
407 if (!LIST_VALUE_PARSED(set, PL_BG))
408 vp->bg_pattern = BG_FALLBACK;
409 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
411 LINE_SEL_FROM_SETTINGS(vp);
413 /* Validate the viewport dimensions - we know that the numbers are
414 non-negative integers, ignore bars and assume the viewport takes them
415 * into account */
416 if ((vp->x >= screens[screen].lcdwidth) ||
417 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
418 (vp->y >= screens[screen].lcdheight) ||
419 ((vp->y + vp->height) > screens[screen].lcdheight))
421 return NULL;
424 /* Default to using the user font if the font was an invalid number or '-'*/
425 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
426 || !LIST_VALUE_PARSED(set, PL_FONT)
428 vp->font = FONT_UI;
430 /* Set the defaults for fields not user-specified */
431 vp->drawmode = DRMODE_SOLID;
433 return ptr;
435 #endif