User definable UI viewport, to be able to restrict the UI into a viewport for all...
[kugel-rb/myfork.git] / apps / gui / viewport.c
blobbb4c291cc09b3416820b2433b369bb04912b1b2f
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 "sprintf.h"
28 #include "string.h"
29 #include "settings.h"
30 #include "kernel.h"
31 #include "system.h"
32 #include "misc.h"
33 #include "viewport.h"
34 #include "statusbar.h"
35 #include "screen_access.h"
36 #include "appevents.h"
40 /*some short cuts for fg/bg/line selector handling */
41 #ifdef HAVE_LCD_COLOR
42 #define LINE_SEL_FROM_SETTINGS(vp) \
43 do { \
44 vp->lss_pattern = global_settings.lss_color; \
45 vp->lse_pattern = global_settings.lse_color; \
46 vp->lst_pattern = global_settings.lst_color; \
47 } while (0)
48 #define FG_FALLBACK global_settings.fg_color
49 #define BG_FALLBACK global_settings.bg_color
50 #else
51 /* mono/greyscale doesn't have most of the above */
52 #define LINE_SEL_FROM_SETTINGS(vp)
53 #define FG_FALLBACK LCD_DEFAULT_FG
54 #define BG_FALLBACK LCD_DEFAULT_BG
55 #endif
57 static int statusbar_enabled = 0;
59 #ifdef HAVE_LCD_BITMAP
61 static struct {
62 struct viewport* vp;
63 int active;
64 } ui_vp_info;
66 static struct viewport custom_vp[NB_SCREENS];
68 /* callbacks for GUI_EVENT_* events */
69 static void viewportmanager_ui_vp_changed(void *param);
70 static void statusbar_toggled(void* param);
71 static int viewport_init_ui_vp(void);
72 #endif
73 static void viewportmanager_redraw(void* data);
75 int viewport_get_nb_lines(struct viewport *vp)
77 #ifdef HAVE_LCD_BITMAP
78 return vp->height/font_get(vp->font)->height;
79 #else
80 (void)vp;
81 return 2;
82 #endif
85 static bool showing_bars(enum screen_type screen)
87 if (statusbar_enabled & VP_SB_ONSCREEN(screen))
89 #ifdef HAVE_LCD_BITMAP
90 bool ignore = statusbar_enabled & VP_SB_IGNORE_SETTING(screen);
91 return ignore || (statusbar_position(screen));
92 #else
93 return true;
94 #endif
96 return false;
99 void viewport_set_fullscreen(struct viewport *vp, enum screen_type screen)
101 vp->x = 0;
102 vp->width = screens[screen].lcdwidth;
104 #ifdef HAVE_LCD_BITMAP
105 vp->drawmode = DRMODE_SOLID;
106 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
108 vp->height = screens[screen].lcdheight;
109 if (statusbar_position(screen) != STATUSBAR_BOTTOM && showing_bars(screen))
110 vp->y = STATUSBAR_HEIGHT;
111 else
112 vp->y = 0;
113 #else
114 vp->y = 0;
115 #endif
116 vp->height = screens[screen].lcdheight - (showing_bars(screen)?STATUSBAR_HEIGHT:0);
118 #if LCD_DEPTH > 1
119 #ifdef HAVE_REMOTE_LCD
120 /* We only need this test if there is a remote LCD */
121 if (screen == SCREEN_MAIN)
122 #endif
124 vp->fg_pattern = FG_FALLBACK;
125 vp->bg_pattern = BG_FALLBACK;
126 LINE_SEL_FROM_SETTINGS(vp);
128 #endif
130 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
131 if (screen == SCREEN_REMOTE)
133 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
134 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
136 #endif
139 void viewport_set_defaults(struct viewport *vp, enum screen_type screen)
141 #ifdef HAVE_LCD_BITMAP
142 if (ui_vp_info.active)
143 *vp = custom_vp[screen];
144 else
145 #endif
146 viewport_set_fullscreen(vp, screen);
149 void viewportmanager_init(void)
151 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
152 #ifdef HAVE_LCD_BITMAP
153 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
154 ui_vp_info.active = viewport_init_ui_vp();
155 ui_vp_info.vp = custom_vp;
156 #endif
159 int viewportmanager_get_statusbar(void)
161 return statusbar_enabled;
164 int viewportmanager_set_statusbar(int enabled)
166 int old = statusbar_enabled;
167 statusbar_enabled = enabled;
168 if (enabled)
170 int i;
171 FOR_NB_SCREENS(i)
173 if (showing_bars(i))
174 gui_statusbar_draw(&statusbars.statusbars[i], true);
176 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
178 else
180 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
182 return old;
185 static void viewportmanager_redraw(void* data)
187 int i;
189 FOR_NB_SCREENS(i)
191 if (showing_bars(i))
192 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
195 #ifdef HAVE_LCD_BITMAP
197 static void statusbar_toggled(void* param)
199 (void)param;
200 /* update vp manager for the new setting and reposition vps
201 * if necessary */
202 viewportmanager_theme_changed(THEME_STATUSBAR);
205 void viewportmanager_theme_changed(int which)
207 if (which & THEME_UI_VIEWPORT)
209 /* reset the ui viewport */
210 if ((ui_vp_info.active = viewport_init_ui_vp()))
211 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
212 else
213 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
214 /* and point to it */
215 ui_vp_info.vp = custom_vp;
217 if (which & THEME_STATUSBAR)
219 statusbar_enabled = 0;
220 if (global_settings.statusbar != STATUSBAR_OFF)
221 statusbar_enabled = VP_SB_ONSCREEN(SCREEN_MAIN);
222 #ifdef HAVE_REMOTE_LCD
223 if (global_settings.remote_statusbar != STATUSBAR_OFF)
224 statusbar_enabled |= VP_SB_ONSCREEN(SCREEN_REMOTE);
225 #endif
226 if (statusbar_enabled)
227 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
228 else
229 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
231 /* reposition viewport to fit statusbar, only if not using the ui vp */
232 if (!ui_vp_info.active)
234 int i;
235 FOR_NB_SCREENS(i)
236 viewport_set_fullscreen(&custom_vp[i], i);
241 static void viewportmanager_ui_vp_changed(void *param)
243 /* if the user changed the theme, we need to initiate a full redraw */
244 int i;
245 /* cast param to a function */
246 void (*draw_func)(void) = ((void(*)(void))param);
247 /* start with clearing the screen */
248 FOR_NB_SCREENS(i)
249 screens[i].clear_display();
250 /* redraw the statusbar if it was enabled */
251 viewportmanager_set_statusbar(statusbar_enabled);
252 /* call the passed function which will redraw the content of
253 * the current screen */
254 if (param != NULL)
255 draw_func();
256 FOR_NB_SCREENS(i)
257 screens[i].update();
260 void viewport_set_current_vp(struct viewport* vp)
262 if (vp != NULL)
263 ui_vp_info.vp = vp;
264 else
265 ui_vp_info.vp = custom_vp;
268 struct viewport* viewport_get_current_vp(void)
270 return ui_vp_info.vp;
273 #ifdef HAVE_LCD_COLOR
274 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
275 #else
276 #define ARG_STRING(_depth) "dddddgg"
277 #endif
279 const char* viewport_parse_viewport(struct viewport *vp,
280 enum screen_type screen,
281 const char *bufptr,
282 const char separator)
284 /* parse the list to the viewport struct */
285 const char *ptr = bufptr;
286 int depth;
287 uint32_t set = 0;
289 enum {
290 PL_X = 0,
291 PL_Y,
292 PL_WIDTH,
293 PL_HEIGHT,
294 PL_FONT,
295 PL_FG,
296 PL_BG,
299 /* Work out the depth of this display */
300 depth = screens[screen].depth;
301 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
302 if (depth == 1)
304 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
305 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
306 return NULL;
308 else
309 #endif
310 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
311 if (depth >= 2)
313 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
314 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
315 &vp->fg_pattern,&vp->bg_pattern)))
316 return NULL;
318 else
319 #endif
321 #undef ARG_STRING
323 /* X and Y *must* be set */
324 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
325 return NULL;
327 /* fix defaults */
328 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
329 vp->width = screens[screen].lcdwidth - vp->x;
330 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
331 vp->height = screens[screen].lcdheight - vp->y;
333 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
334 if (!LIST_VALUE_PARSED(set, PL_FG))
335 vp->fg_pattern = FG_FALLBACK;
336 if (!LIST_VALUE_PARSED(set, PL_BG))
337 vp->bg_pattern = BG_FALLBACK;
338 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
340 LINE_SEL_FROM_SETTINGS(vp);
342 /* Validate the viewport dimensions - we know that the numbers are
343 non-negative integers, ignore bars and assume the viewport takes them
344 * into account */
345 if ((vp->x >= screens[screen].lcdwidth) ||
346 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
347 (vp->y >= screens[screen].lcdheight) ||
348 ((vp->y + vp->height) > screens[screen].lcdheight))
350 return NULL;
353 /* Default to using the user font if the font was an invalid number or '-'*/
354 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
355 || !LIST_VALUE_PARSED(set, PL_FONT)
357 vp->font = FONT_UI;
359 /* Set the defaults for fields not user-specified */
360 vp->drawmode = DRMODE_SOLID;
362 return ptr;
366 * (re)parse the UI vp from the settings
367 * - Returns
368 * 0 if no UI vp is used
369 * >0 if it's used at least partly (on multiscreen targets)
370 * NB_SCREENS if all screens have a UI vp
372 static int viewport_init_ui_vp(void)
374 int screen, ret = NB_SCREENS;
375 FOR_NB_SCREENS(screen)
377 #ifdef HAVE_REMOTE_LCD
378 if ((screen == SCREEN_REMOTE))
380 if(!(viewport_parse_viewport(&custom_vp[screen], screen,
381 global_settings.remote_ui_vp_config, ',')))
383 viewport_set_fullscreen(&custom_vp[screen], screen);
384 ret--;
387 else
388 #endif
390 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
391 global_settings.ui_vp_config, ',')))
393 viewport_set_fullscreen(&custom_vp[screen], screen);
394 ret--;
398 return ret;
401 #endif /* HAVE_LCD_BITMAP */