Update Swedish translation.
[kugel-rb.git] / apps / gui / viewport.c
blob9a7cfbd3cbd9b85e2b198065191d95d35f5a5b05
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 FG_FALLBACK global_settings.fg_color
35 #define BG_FALLBACK global_settings.bg_color
36 #else
37 #define FG_FALLBACK LCD_DEFAULT_FG
38 #define BG_FALLBACK LCD_DEFAULT_BG
39 #endif
41 #ifdef HAVE_LCD_BITMAP
42 static void set_default_align_flags(struct viewport *vp);
43 #endif
45 /* all below isn't needed for pc tools (i.e. checkwps/wps editor)
46 * only viewport_parse_viewport() is */
47 #ifndef __PCTOOL__
48 #include "sprintf.h"
49 #include "string.h"
50 #include "kernel.h"
51 #include "system.h"
52 #include "statusbar.h"
53 #include "appevents.h"
54 #ifdef HAVE_LCD_BITMAP
55 #include "language.h"
56 #endif
57 #include "statusbar-skinned.h"
58 #include "debug.h"
61 static int statusbar_enabled = 0;
63 #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 viewportmanager_call_draw_func(void *param);
74 static void statusbar_toggled(void* param);
75 static unsigned viewport_init_ui_vp(void);
76 #endif
77 static void viewportmanager_redraw(void* data);
79 int viewport_get_nb_lines(const struct viewport *vp)
81 #ifdef HAVE_LCD_BITMAP
82 return vp->height/font_get(vp->font)->height;
83 #else
84 (void)vp;
85 return 2;
86 #endif
89 static bool showing_bars(enum screen_type screen)
91 if (statusbar_enabled & VP_SB_ONSCREEN(screen))
93 #ifdef HAVE_LCD_BITMAP
94 int ignore;
95 ignore = statusbar_enabled & VP_SB_IGNORE_SETTING(screen);
96 return ignore || (statusbar_position(screen) != STATUSBAR_OFF);
97 #else
98 return true;
99 #endif
101 return false;
105 void viewportmanager_init(void)
107 #ifdef HAVE_LCD_BITMAP
108 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
109 #endif
110 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
113 int viewportmanager_get_statusbar(void)
115 return statusbar_enabled;
118 int viewportmanager_set_statusbar(const int enabled)
120 int old = statusbar_enabled;
121 int i;
123 statusbar_enabled = enabled;
125 FOR_NB_SCREENS(i)
127 if (showing_bars(i)
128 && statusbar_position(i) != STATUSBAR_CUSTOM)
130 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
131 gui_statusbar_draw(&statusbars.statusbars[i], true);
133 else
134 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
137 #ifdef HAVE_LCD_BITMAP
138 FOR_NB_SCREENS(i)
140 sb_skin_set_state(showing_bars(i)
141 && statusbar_position(i) == STATUSBAR_CUSTOM, i);
143 #endif
144 return old;
147 static void viewportmanager_redraw(void* data)
149 int i;
151 FOR_NB_SCREENS(i)
153 if (showing_bars(i)
154 && statusbar_position(i) != STATUSBAR_CUSTOM)
155 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
158 #ifdef HAVE_LCD_BITMAP
160 static void statusbar_toggled(void* param)
162 (void)param;
163 /* update vp manager for the new setting and reposition vps
164 * if necessary */
165 viewportmanager_theme_changed(THEME_STATUSBAR);
168 void viewportmanager_theme_changed(const int which)
170 int i;
171 #ifdef HAVE_BUTTONBAR
172 if (which & THEME_BUTTONBAR)
173 { /* don't handle further, the custom ui viewport ignores the buttonbar,
174 * as does viewport_set_defaults(), since only lists use it*/
175 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
177 #endif
178 if (which & THEME_UI_VIEWPORT)
180 int retval = viewport_init_ui_vp();
181 /* reset the ui viewport */
182 FOR_NB_SCREENS(i)
183 ui_vp_info.active[i] = retval & BIT_N(i);
184 /* and point to it */
185 ui_vp_info.vp = custom_vp;
187 else if (which & THEME_LANGUAGE)
188 { /* THEME_UI_VIEWPORT handles rtl already */
189 FOR_NB_SCREENS(i)
190 set_default_align_flags(&custom_vp[i]);
192 if (which & THEME_STATUSBAR)
194 statusbar_enabled = 0;
195 FOR_NB_SCREENS(i)
197 if (statusbar_position(i) != STATUSBAR_OFF)
198 statusbar_enabled |= VP_SB_ONSCREEN(i);
201 viewportmanager_set_statusbar(statusbar_enabled);
203 /* reposition viewport to fit statusbar, only if not using the ui vp */
205 FOR_NB_SCREENS(i)
207 if (!ui_vp_info.active[i])
208 viewport_set_fullscreen(&custom_vp[i], i);
212 int event_add = 0;
213 FOR_NB_SCREENS(i)
215 event_add |= ui_vp_info.active[i];
216 event_add |= (statusbar_position(i) == STATUSBAR_CUSTOM);
219 /* add one of those to ensure the draw function is called always */
220 if (event_add)
222 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
223 remove_event(GUI_EVENT_REFRESH, viewportmanager_call_draw_func);
225 else
227 add_event(GUI_EVENT_REFRESH, false, viewportmanager_call_draw_func);
228 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
231 send_event(GUI_EVENT_THEME_CHANGED, NULL);
235 * simply calls a function that draws stuff, this exists to ensure the
236 * drawing function call in the GUI_EVENT_REFRESH event
238 * param should be 'void func(void)' */
239 static void viewportmanager_call_draw_func(void *param)
241 /* cast param to a function */
242 void (*draw_func)(void) = ((void(*)(void))param);
243 /* call the passed function which will redraw the content of
244 * the current screen */
245 if (draw_func != NULL)
246 draw_func();
249 static void viewportmanager_ui_vp_changed(void *param)
251 /* if the user changed the theme, we need to initiate a full redraw */
252 int i;
253 /* start with clearing the screen */
254 FOR_NB_SCREENS(i)
255 screens[i].clear_display();
256 /* redraw the statusbar if it was enabled */
257 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
258 /* call redraw function */
259 viewportmanager_call_draw_func(param);
260 FOR_NB_SCREENS(i)
261 screens[i].update();
265 * (re)parse the UI vp from the settings
266 * - Returns
267 * 0 if no UI vp is used at all
268 * else the bit for the screen (1<<screen) is set
270 static unsigned viewport_init_ui_vp(void)
272 int screen;
273 unsigned ret = 0;
274 char *setting;
275 FOR_NB_SCREENS(screen)
277 #ifdef HAVE_REMOTE_LCD
278 if ((screen == SCREEN_REMOTE))
279 setting = global_settings.remote_ui_vp_config;
280 else
281 #endif
282 setting = global_settings.ui_vp_config;
285 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
286 setting, ',')))
287 viewport_set_fullscreen(&custom_vp[screen], screen);
288 else
289 ret |= BIT_N(screen);
291 return ret;
294 #ifdef HAVE_TOUCHSCREEN
295 /* check if a point (x and y coordinates) are within a viewport */
296 bool viewport_point_within_vp(const struct viewport *vp,
297 const int x, const int y)
299 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
300 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
301 return (is_x && is_y);
303 #endif /* HAVE_TOUCHSCREEN */
304 #endif /* HAVE_LCD_BITMAP */
305 #endif /* __PCTOOL__ */
307 #ifdef HAVE_LCD_COLOR
308 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
309 #else
310 #define ARG_STRING(_depth) "dddddgg"
311 #endif
314 void viewport_set_fullscreen(struct viewport *vp,
315 const enum screen_type screen)
317 vp->x = 0;
318 vp->y = 0;
319 vp->width = screens[screen].lcdwidth;
320 vp->height = screens[screen].lcdheight;
322 #ifdef HAVE_LCD_BITMAP
323 set_default_align_flags(vp);
324 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
325 vp->drawmode = DRMODE_SOLID;
326 #if LCD_DEPTH > 1
327 #ifdef HAVE_REMOTE_LCD
328 /* We only need this test if there is a remote LCD */
329 if (screen == SCREEN_MAIN)
330 #endif
332 vp->fg_pattern = FG_FALLBACK;
333 vp->bg_pattern = BG_FALLBACK;
334 #ifdef HAVE_LCD_COLOR
335 vp->lss_pattern = global_settings.lss_color;
336 vp->lse_pattern = global_settings.lse_color;
337 vp->lst_pattern = global_settings.lst_color;
338 #endif
340 #endif
342 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
343 if (screen == SCREEN_REMOTE)
345 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
346 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
348 #endif
349 #endif
352 void viewport_set_defaults(struct viewport *vp,
353 const enum screen_type screen)
355 /* Reposition:
356 1) If the "ui viewport" setting is set, and a sbs is loaded which specifies a %Vi
357 return the intersection of those two viewports
358 2) If only one of the "ui viewport" setting, or sbs %Vi is set
359 return it
360 3) No user viewports set
361 return the full display
363 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
365 struct viewport *sbs_area = NULL, *user_setting = NULL;
366 /* get the two viewports */
367 if (ui_vp_info.active[screen])
368 user_setting = &ui_vp_info.vp[screen];
369 if (sb_skin_get_state(screen))
370 sbs_area = sb_skin_get_info_vp(screen);
371 /* have both? get their intersection */
372 if (sbs_area && user_setting)
374 struct viewport *a = sbs_area, *b = user_setting;
375 /* if ui vp and info vp overlap, intersect */
376 if (a->x < b->x + b->width &&
377 a->x + a->width > b->x &&
378 a->y < b->y + b->height &&
379 a->y + a->height > b->y)
381 /* copy from ui vp first (for other field),fix coordinates after */
382 *vp = *user_setting;
383 vp->x = MAX(a->x, b->x);
384 vp->y = MAX(a->y, b->y);
385 vp->width = MIN(a->x + a->width, b->x + b->width) - vp->x;
386 vp->height = MIN(a->y + a->height, b->y + b->height) - vp->y;
387 return;
389 /* else (no overlap at all) fall back to info vp from sbs, that
390 * has no redraw problems */
393 /* if only one is active use it
394 * or if the above check for overlapping failed, use info vp then, because
395 * that doesn't give redraw problems */
396 if (sbs_area)
397 *vp = *sbs_area;
398 else if (user_setting)
399 *vp = *user_setting;
400 /* have neither so its fullscreen which was fixed at the beginning */
401 else
402 #endif /* HAVE_LCD_BITMAP */
403 viewport_set_fullscreen(vp, screen);
407 #ifdef HAVE_LCD_BITMAP
409 static void set_default_align_flags(struct viewport *vp)
411 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
412 #ifndef __PCTOOL__
413 if (UNLIKELY(lang_is_rtl()))
414 vp->flags |= VP_FLAG_ALIGN_RIGHT;
415 #endif
418 const char* viewport_parse_viewport(struct viewport *vp,
419 enum screen_type screen,
420 const char *bufptr,
421 const char separator)
423 /* parse the list to the viewport struct */
424 const char *ptr = bufptr;
425 int depth;
426 uint32_t set = 0;
428 enum {
429 PL_X = 0,
430 PL_Y,
431 PL_WIDTH,
432 PL_HEIGHT,
433 PL_FONT,
434 PL_FG,
435 PL_BG,
438 /* Work out the depth of this display */
439 depth = screens[screen].depth;
440 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
441 if (depth == 1)
443 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
444 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
445 return NULL;
447 else
448 #endif
449 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
450 if (depth >= 2)
452 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
453 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
454 &vp->fg_pattern,&vp->bg_pattern)))
455 return NULL;
457 else
458 #endif
460 #undef ARG_STRING
462 /* X and Y *must* be set */
463 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
464 return NULL;
465 /* check for negative values */
466 if (vp->x < 0)
467 vp->x += screens[screen].lcdwidth;
468 if (vp->y < 0)
469 vp->y += screens[screen].lcdheight;
471 /* fix defaults,
472 * and negative width/height which means "extend to edge minus value */
473 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
474 vp->width = screens[screen].lcdwidth - vp->x;
475 else if (vp->width < 0)
476 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
477 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
478 vp->height = screens[screen].lcdheight - vp->y;
479 else if (vp->height < 0)
480 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
482 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
483 if (!LIST_VALUE_PARSED(set, PL_FG))
484 vp->fg_pattern = FG_FALLBACK;
485 if (!LIST_VALUE_PARSED(set, PL_BG))
486 vp->bg_pattern = BG_FALLBACK;
487 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
489 #ifdef HAVE_LCD_COLOR
490 vp->lss_pattern = global_settings.lss_color;
491 vp->lse_pattern = global_settings.lse_color;
492 vp->lst_pattern = global_settings.lst_color;
493 #endif
495 /* Validate the viewport dimensions - we know that the numbers are
496 non-negative integers, ignore bars and assume the viewport takes them
497 * into account */
498 if ((vp->x >= screens[screen].lcdwidth) ||
499 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
500 (vp->y >= screens[screen].lcdheight) ||
501 ((vp->y + vp->height) > screens[screen].lcdheight))
503 return NULL;
506 /* Default to using the user font if the font was an invalid number or '-'*/
507 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
508 || !LIST_VALUE_PARSED(set, PL_FONT)
510 vp->font = FONT_UI;
512 /* Set the defaults for fields not user-specified */
513 vp->drawmode = DRMODE_SOLID;
514 set_default_align_flags(vp);
516 return ptr;
518 #endif