rbutil: store the voice language in the correct setting.
[kugel-rb.git] / apps / gui / viewport.c
blobfdd06bdc46d8d6b11fae91ec930acb2c67c0f207
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"
58 #ifdef HAVE_LCD_BITMAP
59 #include "language.h"
60 #endif
62 static int statusbar_enabled = 0;
64 #ifdef HAVE_LCD_BITMAP
65 static void set_default_align_flags(struct viewport *vp);
67 static struct {
68 struct viewport* vp;
69 int active[NB_SCREENS];
70 } ui_vp_info;
72 static struct viewport custom_vp[NB_SCREENS];
74 /* callbacks for GUI_EVENT_* events */
75 static void viewportmanager_ui_vp_changed(void *param);
76 static void statusbar_toggled(void* param);
77 static unsigned viewport_init_ui_vp(void);
78 #endif
79 static void viewportmanager_redraw(void* data);
81 int viewport_get_nb_lines(const struct viewport *vp)
83 #ifdef HAVE_LCD_BITMAP
84 return vp->height/font_get(vp->font)->height;
85 #else
86 (void)vp;
87 return 2;
88 #endif
91 static bool showing_bars(enum screen_type screen)
93 if (statusbar_enabled & VP_SB_ONSCREEN(screen))
95 #ifdef HAVE_LCD_BITMAP
96 bool ignore = statusbar_enabled & VP_SB_IGNORE_SETTING(screen);
97 return ignore || (statusbar_position(screen));
98 #else
99 return true;
100 #endif
102 return false;
105 void viewport_set_fullscreen(struct viewport *vp,
106 const enum screen_type screen)
108 vp->x = 0;
109 vp->width = screens[screen].lcdwidth;
111 #ifdef HAVE_LCD_BITMAP
112 set_default_align_flags(vp);
113 vp->drawmode = DRMODE_SOLID;
114 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
116 vp->height = screens[screen].lcdheight;
117 if (statusbar_position(screen) != STATUSBAR_BOTTOM
118 && showing_bars(screen))
119 vp->y = STATUSBAR_HEIGHT;
120 else
121 vp->y = 0;
122 #else
123 vp->y = 0;
124 #endif
125 vp->height = screens[screen].lcdheight
126 - (showing_bars(screen)?STATUSBAR_HEIGHT:0);
128 #if LCD_DEPTH > 1
129 #ifdef HAVE_REMOTE_LCD
130 /* We only need this test if there is a remote LCD */
131 if (screen == SCREEN_MAIN)
132 #endif
134 vp->fg_pattern = FG_FALLBACK;
135 vp->bg_pattern = BG_FALLBACK;
136 LINE_SEL_FROM_SETTINGS(vp);
138 #endif
140 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
141 if (screen == SCREEN_REMOTE)
143 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
144 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
146 #endif
150 void viewport_set_defaults(struct viewport *vp,
151 const enum screen_type screen)
153 #ifdef HAVE_LCD_BITMAP
154 if (ui_vp_info.active[screen])
155 *vp = ui_vp_info.vp[screen];
156 else
157 #endif
158 viewport_set_fullscreen(vp, screen);
162 void viewportmanager_init(void)
164 #ifdef HAVE_LCD_BITMAP
165 int retval, i;
166 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
167 retval = viewport_init_ui_vp();
168 FOR_NB_SCREENS(i)
169 ui_vp_info.active[i] = retval & BIT_N(i);
170 ui_vp_info.vp = custom_vp;
171 #endif
172 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
175 int viewportmanager_get_statusbar(void)
177 return statusbar_enabled;
180 int viewportmanager_set_statusbar(const int enabled)
182 int old = statusbar_enabled;
183 statusbar_enabled = enabled;
184 if (enabled)
186 int i;
187 FOR_NB_SCREENS(i)
189 if (showing_bars(i))
190 gui_statusbar_draw(&statusbars.statusbars[i], true);
192 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
194 else
196 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
198 return old;
201 static void viewportmanager_redraw(void* data)
203 int i;
205 FOR_NB_SCREENS(i)
207 if (showing_bars(i))
208 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
211 #ifdef HAVE_LCD_BITMAP
213 static void statusbar_toggled(void* param)
215 (void)param;
216 /* update vp manager for the new setting and reposition vps
217 * if necessary */
218 viewportmanager_theme_changed(THEME_STATUSBAR);
221 void viewportmanager_theme_changed(const int which)
223 int i;
224 #ifdef HAVE_BUTTONBAR
225 if (which & THEME_BUTTONBAR)
226 { /* don't handle further, the custom ui viewport ignores the buttonbar,
227 * as does viewport_set_defaults(), since only lists use it*/
228 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
230 #endif
231 if (which & THEME_UI_VIEWPORT)
233 int retval = viewport_init_ui_vp();
234 /* reset the ui viewport */
235 FOR_NB_SCREENS(i)
236 ui_vp_info.active[i] = retval & BIT_N(i);
238 if (retval != 0)
239 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
240 else
241 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
242 /* and point to it */
243 ui_vp_info.vp = custom_vp;
245 else if (which & THEME_LANGUAGE)
246 { /* THEME_UI_VIEWPORT handles rtl already */
247 FOR_NB_SCREENS(i)
248 set_default_align_flags(&custom_vp[i]);
250 if (which & THEME_STATUSBAR)
252 statusbar_enabled = VP_SB_HIDE_ALL;
254 FOR_NB_SCREENS(i)
256 if (statusbar_position(i) != STATUSBAR_OFF)
257 statusbar_enabled |= VP_SB_ONSCREEN(i);
260 if (statusbar_enabled != VP_SB_HIDE_ALL)
261 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
262 else
263 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
265 /* reposition viewport to fit statusbar, only if not using the ui vp */
267 FOR_NB_SCREENS(i)
269 if (!ui_vp_info.active[i])
270 viewport_set_fullscreen(&custom_vp[i], i);
273 send_event(GUI_EVENT_THEME_CHANGED, NULL);
276 static void viewportmanager_ui_vp_changed(void *param)
278 /* if the user changed the theme, we need to initiate a full redraw */
279 int i;
280 /* cast param to a function */
281 void (*draw_func)(void) = ((void(*)(void))param);
282 /* start with clearing the screen */
283 FOR_NB_SCREENS(i)
284 screens[i].clear_display();
285 /* redraw the statusbar if it was enabled */
286 viewportmanager_set_statusbar(statusbar_enabled);
287 /* call the passed function which will redraw the content of
288 * the current screen */
289 if (param != NULL)
290 draw_func();
291 FOR_NB_SCREENS(i)
292 screens[i].update();
295 void viewport_set_current_vp(struct viewport* vp)
297 if (vp != NULL)
298 ui_vp_info.vp = vp;
299 else
300 ui_vp_info.vp = custom_vp;
302 /* must be done after the assignment above or event handler get old vps */
303 send_event(GUI_EVENT_THEME_CHANGED, NULL);
306 struct viewport* viewport_get_current_vp(void)
308 return ui_vp_info.vp;
311 bool viewport_ui_vp_get_state(enum screen_type screen)
313 return ui_vp_info.active[screen];
317 * (re)parse the UI vp from the settings
318 * - Returns
319 * 0 if no UI vp is used at all
320 * else the bit for the screen (1<<screen) is set
322 static unsigned viewport_init_ui_vp(void)
324 int screen;
325 unsigned ret = 0;
326 char *setting;
327 FOR_NB_SCREENS(screen)
329 #ifdef HAVE_REMOTE_LCD
330 if ((screen == SCREEN_REMOTE))
331 setting = global_settings.remote_ui_vp_config;
332 else
333 #endif
334 setting = global_settings.ui_vp_config;
337 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
338 setting, ',')))
339 viewport_set_fullscreen(&custom_vp[screen], screen);
340 else
341 ret |= BIT_N(screen);
343 return ret;
346 #ifdef HAVE_TOUCHSCREEN
347 /* check if a point (x and y coordinates) are within a viewport */
348 bool viewport_point_within_vp(const struct viewport *vp,
349 const int x, const int y)
351 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
352 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
353 return (is_x && is_y);
355 #endif /* HAVE_TOUCHSCREEN */
356 #endif /* HAVE_LCD_BITMAP */
357 #endif /* __PCTOOL__ */
359 #ifdef HAVE_LCD_COLOR
360 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
361 #else
362 #define ARG_STRING(_depth) "dddddgg"
363 #endif
365 #ifdef HAVE_LCD_BITMAP
367 static void set_default_align_flags(struct viewport *vp)
369 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
370 #ifndef __PCTOOL__
371 if (UNLIKELY(lang_is_rtl()))
372 vp->flags |= VP_FLAG_ALIGN_RIGHT;
373 #endif
376 const char* viewport_parse_viewport(struct viewport *vp,
377 enum screen_type screen,
378 const char *bufptr,
379 const char separator)
381 /* parse the list to the viewport struct */
382 const char *ptr = bufptr;
383 int depth;
384 uint32_t set = 0;
386 enum {
387 PL_X = 0,
388 PL_Y,
389 PL_WIDTH,
390 PL_HEIGHT,
391 PL_FONT,
392 PL_FG,
393 PL_BG,
396 /* Work out the depth of this display */
397 depth = screens[screen].depth;
398 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
399 if (depth == 1)
401 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
402 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
403 return NULL;
405 else
406 #endif
407 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
408 if (depth >= 2)
410 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
411 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
412 &vp->fg_pattern,&vp->bg_pattern)))
413 return NULL;
415 else
416 #endif
418 #undef ARG_STRING
420 /* X and Y *must* be set */
421 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
422 return NULL;
424 /* fix defaults */
425 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
426 vp->width = screens[screen].lcdwidth - vp->x;
427 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
428 vp->height = screens[screen].lcdheight - vp->y;
430 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
431 if (!LIST_VALUE_PARSED(set, PL_FG))
432 vp->fg_pattern = FG_FALLBACK;
433 if (!LIST_VALUE_PARSED(set, PL_BG))
434 vp->bg_pattern = BG_FALLBACK;
435 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
437 LINE_SEL_FROM_SETTINGS(vp);
439 /* Validate the viewport dimensions - we know that the numbers are
440 non-negative integers, ignore bars and assume the viewport takes them
441 * into account */
442 if ((vp->x >= screens[screen].lcdwidth) ||
443 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
444 (vp->y >= screens[screen].lcdheight) ||
445 ((vp->y + vp->height) > screens[screen].lcdheight))
447 return NULL;
450 /* Default to using the user font if the font was an invalid number or '-'*/
451 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
452 || !LIST_VALUE_PARSED(set, PL_FONT)
454 vp->font = FONT_UI;
456 /* Set the defaults for fields not user-specified */
457 vp->drawmode = DRMODE_SOLID;
458 set_default_align_flags(vp);
460 return ptr;
462 #endif