Also fix Debug->View partitions when SECTOR_SIZE!=512
[kugel-rb.git] / apps / gui / viewport.c
blob063f59cec6c16167bc3ec11c0960b5e7888e9532
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 int retval, i;
108 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
109 retval = viewport_init_ui_vp();
110 FOR_NB_SCREENS(i)
111 ui_vp_info.active[i] = retval & BIT_N(i);
112 ui_vp_info.vp = custom_vp;
113 #endif
114 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
117 int viewportmanager_get_statusbar(void)
119 return statusbar_enabled;
122 int viewportmanager_set_statusbar(const int enabled)
124 int old = statusbar_enabled;
125 int i;
127 statusbar_enabled = enabled;
129 FOR_NB_SCREENS(i)
131 if (showing_bars(i)
132 && statusbar_position(i) != STATUSBAR_CUSTOM)
134 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
135 gui_statusbar_draw(&statusbars.statusbars[i], true);
137 else
138 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
141 #ifdef HAVE_LCD_BITMAP
142 FOR_NB_SCREENS(i)
144 sb_skin_set_state(showing_bars(i)
145 && statusbar_position(i) == STATUSBAR_CUSTOM, i);
147 #endif
148 return old;
151 static void viewportmanager_redraw(void* data)
153 int i;
155 FOR_NB_SCREENS(i)
157 if (showing_bars(i)
158 && statusbar_position(i) != STATUSBAR_CUSTOM)
159 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
162 #ifdef HAVE_LCD_BITMAP
164 static void statusbar_toggled(void* param)
166 (void)param;
167 /* update vp manager for the new setting and reposition vps
168 * if necessary */
169 viewportmanager_theme_changed(THEME_STATUSBAR);
172 void viewportmanager_theme_changed(const int which)
174 int i;
175 #ifdef HAVE_BUTTONBAR
176 if (which & THEME_BUTTONBAR)
177 { /* don't handle further, the custom ui viewport ignores the buttonbar,
178 * as does viewport_set_defaults(), since only lists use it*/
179 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
181 #endif
182 if (which & THEME_UI_VIEWPORT)
184 int retval = viewport_init_ui_vp();
185 /* reset the ui viewport */
186 FOR_NB_SCREENS(i)
187 ui_vp_info.active[i] = retval & BIT_N(i);
188 /* and point to it */
189 ui_vp_info.vp = custom_vp;
191 else if (which & THEME_LANGUAGE)
192 { /* THEME_UI_VIEWPORT handles rtl already */
193 FOR_NB_SCREENS(i)
194 set_default_align_flags(&custom_vp[i]);
196 if (which & THEME_STATUSBAR)
198 statusbar_enabled = 0;
199 FOR_NB_SCREENS(i)
201 if (statusbar_position(i) != STATUSBAR_OFF)
202 statusbar_enabled |= VP_SB_ONSCREEN(i);
205 viewportmanager_set_statusbar(statusbar_enabled);
207 /* reposition viewport to fit statusbar, only if not using the ui vp */
209 FOR_NB_SCREENS(i)
211 if (!ui_vp_info.active[i])
212 viewport_set_fullscreen(&custom_vp[i], i);
216 int event_add = 0;
217 FOR_NB_SCREENS(i)
219 event_add |= ui_vp_info.active[i];
220 event_add |= (statusbar_position(i) == STATUSBAR_CUSTOM);
223 if (event_add)
224 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
225 else
226 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
228 send_event(GUI_EVENT_THEME_CHANGED, NULL);
231 static void viewportmanager_ui_vp_changed(void *param)
233 /* if the user changed the theme, we need to initiate a full redraw */
234 int i;
235 /* cast param to a function */
236 void (*draw_func)(void) = ((void(*)(void))param);
237 /* start with clearing the screen */
238 FOR_NB_SCREENS(i)
239 screens[i].clear_display();
240 /* redraw the statusbar if it was enabled */
241 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
242 /* call the passed function which will redraw the content of
243 * the current screen */
244 if (draw_func != NULL)
245 draw_func();
246 FOR_NB_SCREENS(i)
247 screens[i].update();
250 void viewport_set_current_vp(struct viewport* vp)
252 if (vp != NULL)
253 ui_vp_info.vp = vp;
254 else
255 ui_vp_info.vp = custom_vp;
257 /* must be done after the assignment above or event handler get old vps */
258 send_event(GUI_EVENT_THEME_CHANGED, NULL);
261 struct viewport* viewport_get_current_vp(void)
263 return ui_vp_info.vp;
266 bool viewport_ui_vp_get_state(enum screen_type screen)
268 return ui_vp_info.active[screen];
272 * (re)parse the UI vp from the settings
273 * - Returns
274 * 0 if no UI vp is used at all
275 * else the bit for the screen (1<<screen) is set
277 static unsigned viewport_init_ui_vp(void)
279 int screen;
280 unsigned ret = 0;
281 char *setting;
282 FOR_NB_SCREENS(screen)
284 #ifdef HAVE_REMOTE_LCD
285 if ((screen == SCREEN_REMOTE))
286 setting = global_settings.remote_ui_vp_config;
287 else
288 #endif
289 setting = global_settings.ui_vp_config;
292 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
293 setting, ',')))
294 viewport_set_fullscreen(&custom_vp[screen], screen);
295 else
296 ret |= BIT_N(screen);
298 return ret;
301 #ifdef HAVE_TOUCHSCREEN
302 /* check if a point (x and y coordinates) are within a viewport */
303 bool viewport_point_within_vp(const struct viewport *vp,
304 const int x, const int y)
306 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
307 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
308 return (is_x && is_y);
310 #endif /* HAVE_TOUCHSCREEN */
311 #endif /* HAVE_LCD_BITMAP */
312 #endif /* __PCTOOL__ */
314 #ifdef HAVE_LCD_COLOR
315 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
316 #else
317 #define ARG_STRING(_depth) "dddddgg"
318 #endif
321 void viewport_set_fullscreen(struct viewport *vp,
322 const enum screen_type screen)
324 vp->x = 0;
325 vp->y = 0;
326 vp->width = screens[screen].lcdwidth;
327 vp->height = screens[screen].lcdheight;
329 #ifdef HAVE_LCD_BITMAP
330 set_default_align_flags(vp);
331 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
332 vp->drawmode = DRMODE_SOLID;
333 #if LCD_DEPTH > 1
334 #ifdef HAVE_REMOTE_LCD
335 /* We only need this test if there is a remote LCD */
336 if (screen == SCREEN_MAIN)
337 #endif
339 vp->fg_pattern = FG_FALLBACK;
340 vp->bg_pattern = BG_FALLBACK;
341 #ifdef HAVE_LCD_COLOR
342 vp->lss_pattern = global_settings.lss_color;
343 vp->lse_pattern = global_settings.lse_color;
344 vp->lst_pattern = global_settings.lst_color;
345 #endif
347 #endif
349 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
350 if (screen == SCREEN_REMOTE)
352 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
353 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
355 #endif
356 #endif
359 void viewport_set_defaults(struct viewport *vp,
360 const enum screen_type screen)
362 /* Reposition:
363 1) If the "ui viewport" setting is set, and a sbs is loaded which specifies a %Vi
364 return the intersection of those two viewports
365 2) If only one of the "ui viewport" setting, or sbs %Vi is set
366 return it
367 3) No user viewports set
368 return the full display
370 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
372 struct viewport *sbs_area = NULL, *user_setting = NULL;
373 /* get the two viewports */
374 if (ui_vp_info.active[screen])
375 user_setting = &ui_vp_info.vp[screen];
376 if (sb_skin_get_state(screen))
377 sbs_area = sb_skin_get_info_vp(screen);
378 /* have both? get their intersection */
379 if (sbs_area && user_setting)
381 struct viewport *a = sbs_area, *b = user_setting;
382 /* if ui vp and info vp overlap, intersect */
383 if (a->x < b->x + b->width &&
384 a->x + a->width > b->x &&
385 a->y < b->y + b->height &&
386 a->y + a->height > b->y)
388 /* copy from ui vp first (for other field),fix coordinates after */
389 *vp = *user_setting;
390 vp->x = MAX(a->x, b->x);
391 vp->y = MAX(a->y, b->y);
392 vp->width = MIN(a->x + a->width, b->x + b->width) - vp->x;
393 vp->height = MIN(a->y + a->height, b->y + b->height) - vp->y;
394 return;
396 else /* They don't overlap, so the best wrong answer is show fullscreen */
397 viewport_set_fullscreen(vp, screen);
400 /* if only one is active use it
401 * or if the above check for overlapping failed, use info vp then, because
402 * that doesn't give redraw problems */
403 if (sbs_area)
404 *vp = *sbs_area;
405 else if (user_setting)
406 *vp = *user_setting;
407 /* have neither so its fullscreen which was fixed at the beginning */
408 else
409 #endif /* HAVE_LCD_BITMAP */
410 viewport_set_fullscreen(vp, screen);
414 #ifdef HAVE_LCD_BITMAP
416 static void set_default_align_flags(struct viewport *vp)
418 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
419 #ifndef __PCTOOL__
420 if (UNLIKELY(lang_is_rtl()))
421 vp->flags |= VP_FLAG_ALIGN_RIGHT;
422 #endif
425 const char* viewport_parse_viewport(struct viewport *vp,
426 enum screen_type screen,
427 const char *bufptr,
428 const char separator)
430 /* parse the list to the viewport struct */
431 const char *ptr = bufptr;
432 int depth;
433 uint32_t set = 0;
435 enum {
436 PL_X = 0,
437 PL_Y,
438 PL_WIDTH,
439 PL_HEIGHT,
440 PL_FONT,
441 PL_FG,
442 PL_BG,
445 /* Work out the depth of this display */
446 depth = screens[screen].depth;
447 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
448 if (depth == 1)
450 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
451 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
452 return NULL;
454 else
455 #endif
456 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
457 if (depth >= 2)
459 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
460 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
461 &vp->fg_pattern,&vp->bg_pattern)))
462 return NULL;
464 else
465 #endif
467 #undef ARG_STRING
469 /* X and Y *must* be set */
470 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
471 return NULL;
472 /* check for negative values */
473 if (vp->x < 0)
474 vp->x += screens[screen].lcdwidth;
475 if (vp->y < 0)
476 vp->y += screens[screen].lcdheight;
478 /* fix defaults,
479 * and negative width/height which means "extend to edge minus value */
480 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
481 vp->width = screens[screen].lcdwidth - vp->x;
482 else if (vp->width < 0)
483 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
484 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
485 vp->height = screens[screen].lcdheight - vp->y;
486 else if (vp->height < 0)
487 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
489 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
490 if (!LIST_VALUE_PARSED(set, PL_FG))
491 vp->fg_pattern = FG_FALLBACK;
492 if (!LIST_VALUE_PARSED(set, PL_BG))
493 vp->bg_pattern = BG_FALLBACK;
494 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
496 #ifdef HAVE_LCD_COLOR
497 vp->lss_pattern = global_settings.lss_color;
498 vp->lse_pattern = global_settings.lse_color;
499 vp->lst_pattern = global_settings.lst_color;
500 #endif
502 /* Validate the viewport dimensions - we know that the numbers are
503 non-negative integers, ignore bars and assume the viewport takes them
504 * into account */
505 if ((vp->x >= screens[screen].lcdwidth) ||
506 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
507 (vp->y >= screens[screen].lcdheight) ||
508 ((vp->y + vp->height) > screens[screen].lcdheight))
510 return NULL;
513 /* Default to using the user font if the font was an invalid number or '-'*/
514 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
515 || !LIST_VALUE_PARSED(set, PL_FONT)
517 vp->font = FONT_UI;
519 /* Set the defaults for fields not user-specified */
520 vp->drawmode = DRMODE_SOLID;
521 set_default_align_flags(vp);
523 return ptr;
525 #endif