Fall back to info vp from sbs when intersection fails (again, r23575 changed it despi...
[kugel-rb.git] / apps / gui / viewport.c
blob8e00ea3a71bec23d2e6a0865e29fe02f9dfb2e64
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 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(const 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 int ignore;
94 ignore = statusbar_enabled & VP_SB_IGNORE_SETTING(screen);
95 return ignore || (statusbar_position(screen) != STATUSBAR_OFF);
96 #else
97 return true;
98 #endif
100 return false;
104 void viewportmanager_init(void)
106 #ifdef HAVE_LCD_BITMAP
107 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
108 #endif
109 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
112 int viewportmanager_get_statusbar(void)
114 return statusbar_enabled;
117 int viewportmanager_set_statusbar(const int enabled)
119 int old = statusbar_enabled;
120 int i;
122 statusbar_enabled = enabled;
124 FOR_NB_SCREENS(i)
126 if (showing_bars(i)
127 && statusbar_position(i) != STATUSBAR_CUSTOM)
129 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
130 gui_statusbar_draw(&statusbars.statusbars[i], true);
132 else
133 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
136 #ifdef HAVE_LCD_BITMAP
137 FOR_NB_SCREENS(i)
139 sb_skin_set_state(showing_bars(i)
140 && statusbar_position(i) == STATUSBAR_CUSTOM, i);
142 #endif
143 return old;
146 static void viewportmanager_redraw(void* data)
148 int i;
150 FOR_NB_SCREENS(i)
152 if (showing_bars(i)
153 && statusbar_position(i) != STATUSBAR_CUSTOM)
154 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
157 #ifdef HAVE_LCD_BITMAP
159 static void statusbar_toggled(void* param)
161 (void)param;
162 /* update vp manager for the new setting and reposition vps
163 * if necessary */
164 viewportmanager_theme_changed(THEME_STATUSBAR);
167 void viewportmanager_theme_changed(const int which)
169 int i;
170 #ifdef HAVE_BUTTONBAR
171 if (which & THEME_BUTTONBAR)
172 { /* don't handle further, the custom ui viewport ignores the buttonbar,
173 * as does viewport_set_defaults(), since only lists use it*/
174 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
176 #endif
177 if (which & THEME_UI_VIEWPORT)
179 int retval = viewport_init_ui_vp();
180 /* reset the ui viewport */
181 FOR_NB_SCREENS(i)
182 ui_vp_info.active[i] = retval & BIT_N(i);
183 /* and point to it */
184 ui_vp_info.vp = custom_vp;
186 else if (which & THEME_LANGUAGE)
187 { /* THEME_UI_VIEWPORT handles rtl already */
188 FOR_NB_SCREENS(i)
189 set_default_align_flags(&custom_vp[i]);
191 if (which & THEME_STATUSBAR)
193 statusbar_enabled = 0;
194 FOR_NB_SCREENS(i)
196 if (statusbar_position(i) != STATUSBAR_OFF)
197 statusbar_enabled |= VP_SB_ONSCREEN(i);
200 viewportmanager_set_statusbar(statusbar_enabled);
202 /* reposition viewport to fit statusbar, only if not using the ui vp */
204 FOR_NB_SCREENS(i)
206 if (!ui_vp_info.active[i])
207 viewport_set_fullscreen(&custom_vp[i], i);
211 int event_add = 0;
212 FOR_NB_SCREENS(i)
214 event_add |= ui_vp_info.active[i];
215 event_add |= (statusbar_position(i) == STATUSBAR_CUSTOM);
218 if (event_add)
219 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
220 else
221 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
223 send_event(GUI_EVENT_THEME_CHANGED, NULL);
226 static void viewportmanager_ui_vp_changed(void *param)
228 /* if the user changed the theme, we need to initiate a full redraw */
229 int i;
230 /* cast param to a function */
231 void (*draw_func)(void) = ((void(*)(void))param);
232 /* start with clearing the screen */
233 FOR_NB_SCREENS(i)
234 screens[i].clear_display();
235 /* redraw the statusbar if it was enabled */
236 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
237 /* call the passed function which will redraw the content of
238 * the current screen */
239 if (draw_func != NULL)
240 draw_func();
241 FOR_NB_SCREENS(i)
242 screens[i].update();
245 void viewport_set_current_vp(struct viewport* vp)
247 if (vp != NULL)
248 ui_vp_info.vp = vp;
249 else
250 ui_vp_info.vp = custom_vp;
252 /* must be done after the assignment above or event handler get old vps */
253 send_event(GUI_EVENT_THEME_CHANGED, NULL);
256 struct viewport* viewport_get_current_vp(void)
258 return ui_vp_info.vp;
261 bool viewport_ui_vp_get_state(enum screen_type screen)
263 return ui_vp_info.active[screen];
267 * (re)parse the UI vp from the settings
268 * - Returns
269 * 0 if no UI vp is used at all
270 * else the bit for the screen (1<<screen) is set
272 static unsigned viewport_init_ui_vp(void)
274 int screen;
275 unsigned ret = 0;
276 char *setting;
277 FOR_NB_SCREENS(screen)
279 #ifdef HAVE_REMOTE_LCD
280 if ((screen == SCREEN_REMOTE))
281 setting = global_settings.remote_ui_vp_config;
282 else
283 #endif
284 setting = global_settings.ui_vp_config;
287 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
288 setting, ',')))
289 viewport_set_fullscreen(&custom_vp[screen], screen);
290 else
291 ret |= BIT_N(screen);
293 return ret;
296 #ifdef HAVE_TOUCHSCREEN
297 /* check if a point (x and y coordinates) are within a viewport */
298 bool viewport_point_within_vp(const struct viewport *vp,
299 const int x, const int y)
301 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
302 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
303 return (is_x && is_y);
305 #endif /* HAVE_TOUCHSCREEN */
306 #endif /* HAVE_LCD_BITMAP */
307 #endif /* __PCTOOL__ */
309 #ifdef HAVE_LCD_COLOR
310 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
311 #else
312 #define ARG_STRING(_depth) "dddddgg"
313 #endif
316 void viewport_set_fullscreen(struct viewport *vp,
317 const enum screen_type screen)
319 vp->x = 0;
320 vp->y = 0;
321 vp->width = screens[screen].lcdwidth;
322 vp->height = screens[screen].lcdheight;
324 #ifdef HAVE_LCD_BITMAP
325 set_default_align_flags(vp);
326 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
327 vp->drawmode = DRMODE_SOLID;
328 #if LCD_DEPTH > 1
329 #ifdef HAVE_REMOTE_LCD
330 /* We only need this test if there is a remote LCD */
331 if (screen == SCREEN_MAIN)
332 #endif
334 vp->fg_pattern = FG_FALLBACK;
335 vp->bg_pattern = BG_FALLBACK;
336 #ifdef HAVE_LCD_COLOR
337 vp->lss_pattern = global_settings.lss_color;
338 vp->lse_pattern = global_settings.lse_color;
339 vp->lst_pattern = global_settings.lst_color;
340 #endif
342 #endif
344 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
345 if (screen == SCREEN_REMOTE)
347 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
348 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
350 #endif
351 #endif
354 void viewport_set_defaults(struct viewport *vp,
355 const enum screen_type screen)
357 /* Reposition:
358 1) If the "ui viewport" setting is set, and a sbs is loaded which specifies a %Vi
359 return the intersection of those two viewports
360 2) If only one of the "ui viewport" setting, or sbs %Vi is set
361 return it
362 3) No user viewports set
363 return the full display
365 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
367 struct viewport *sbs_area = NULL, *user_setting = NULL;
368 /* get the two viewports */
369 if (ui_vp_info.active[screen])
370 user_setting = &ui_vp_info.vp[screen];
371 if (sb_skin_get_state(screen))
372 sbs_area = sb_skin_get_info_vp(screen);
373 /* have both? get their intersection */
374 if (sbs_area && user_setting)
376 struct viewport *a = sbs_area, *b = user_setting;
377 /* if ui vp and info vp overlap, intersect */
378 if (a->x < b->x + b->width &&
379 a->x + a->width > b->x &&
380 a->y < b->y + b->height &&
381 a->y + a->height > b->y)
383 /* copy from ui vp first (for other field),fix coordinates after */
384 *vp = *user_setting;
385 vp->x = MAX(a->x, b->x);
386 vp->y = MAX(a->y, b->y);
387 vp->width = MIN(a->x + a->width, b->x + b->width) - vp->x;
388 vp->height = MIN(a->y + a->height, b->y + b->height) - vp->y;
389 return;
391 /* else (no overlap at all) fall back to info vp from sbs, that
392 * has no redraw problems */
395 /* if only one is active use it
396 * or if the above check for overlapping failed, use info vp then, because
397 * that doesn't give redraw problems */
398 if (sbs_area)
399 *vp = *sbs_area;
400 else if (user_setting)
401 *vp = *user_setting;
402 /* have neither so its fullscreen which was fixed at the beginning */
403 else
404 #endif /* HAVE_LCD_BITMAP */
405 viewport_set_fullscreen(vp, screen);
409 #ifdef HAVE_LCD_BITMAP
411 static void set_default_align_flags(struct viewport *vp)
413 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
414 #ifndef __PCTOOL__
415 if (UNLIKELY(lang_is_rtl()))
416 vp->flags |= VP_FLAG_ALIGN_RIGHT;
417 #endif
420 const char* viewport_parse_viewport(struct viewport *vp,
421 enum screen_type screen,
422 const char *bufptr,
423 const char separator)
425 /* parse the list to the viewport struct */
426 const char *ptr = bufptr;
427 int depth;
428 uint32_t set = 0;
430 enum {
431 PL_X = 0,
432 PL_Y,
433 PL_WIDTH,
434 PL_HEIGHT,
435 PL_FONT,
436 PL_FG,
437 PL_BG,
440 /* Work out the depth of this display */
441 depth = screens[screen].depth;
442 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
443 if (depth == 1)
445 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
446 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
447 return NULL;
449 else
450 #endif
451 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
452 if (depth >= 2)
454 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
455 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
456 &vp->fg_pattern,&vp->bg_pattern)))
457 return NULL;
459 else
460 #endif
462 #undef ARG_STRING
464 /* X and Y *must* be set */
465 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
466 return NULL;
467 /* check for negative values */
468 if (vp->x < 0)
469 vp->x += screens[screen].lcdwidth;
470 if (vp->y < 0)
471 vp->y += screens[screen].lcdheight;
473 /* fix defaults,
474 * and negative width/height which means "extend to edge minus value */
475 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
476 vp->width = screens[screen].lcdwidth - vp->x;
477 else if (vp->width < 0)
478 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
479 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
480 vp->height = screens[screen].lcdheight - vp->y;
481 else if (vp->height < 0)
482 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
484 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
485 if (!LIST_VALUE_PARSED(set, PL_FG))
486 vp->fg_pattern = FG_FALLBACK;
487 if (!LIST_VALUE_PARSED(set, PL_BG))
488 vp->bg_pattern = BG_FALLBACK;
489 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
491 #ifdef HAVE_LCD_COLOR
492 vp->lss_pattern = global_settings.lss_color;
493 vp->lse_pattern = global_settings.lse_color;
494 vp->lst_pattern = global_settings.lst_color;
495 #endif
497 /* Validate the viewport dimensions - we know that the numbers are
498 non-negative integers, ignore bars and assume the viewport takes them
499 * into account */
500 if ((vp->x >= screens[screen].lcdwidth) ||
501 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
502 (vp->y >= screens[screen].lcdheight) ||
503 ((vp->y + vp->height) > screens[screen].lcdheight))
505 return NULL;
508 /* Default to using the user font if the font was an invalid number or '-'*/
509 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
510 || !LIST_VALUE_PARSED(set, PL_FONT)
512 vp->font = FONT_UI;
514 /* Set the defaults for fields not user-specified */
515 vp->drawmode = DRMODE_SOLID;
516 set_default_align_flags(vp);
518 return ptr;
520 #endif