fix warning in viewport.c
[kugel-rb.git] / apps / gui / viewport.c
blob40efbfc30e690fabdeb223512e950431b0afebe7
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 "statusbar-skinned.h"
37 #include "appevents.h"
41 /*some short cuts for fg/bg/line selector handling */
42 #ifdef HAVE_LCD_COLOR
43 #define LINE_SEL_FROM_SETTINGS(vp) \
44 do { \
45 vp->lss_pattern = global_settings.lss_color; \
46 vp->lse_pattern = global_settings.lse_color; \
47 vp->lst_pattern = global_settings.lst_color; \
48 } while (0)
49 #define FG_FALLBACK global_settings.fg_color
50 #define BG_FALLBACK global_settings.bg_color
51 #else
52 /* mono/greyscale doesn't have most of the above */
53 #define LINE_SEL_FROM_SETTINGS(vp)
54 #define FG_FALLBACK LCD_DEFAULT_FG
55 #define BG_FALLBACK LCD_DEFAULT_BG
56 #endif
58 static int statusbar_enabled = 0;
60 #ifdef HAVE_LCD_BITMAP
62 static struct {
63 struct viewport* vp;
64 int active;
65 } ui_vp_info;
67 static struct viewport custom_vp[NB_SCREENS];
69 /* callbacks for GUI_EVENT_* events */
70 static void viewportmanager_ui_vp_changed(void *param);
71 static void statusbar_toggled(void* param);
72 static int viewport_init_ui_vp(void);
73 #endif
74 static void viewportmanager_redraw(void* data);
76 int viewport_get_nb_lines(struct viewport *vp)
78 #ifdef HAVE_LCD_BITMAP
79 return vp->height/font_get(vp->font)->height;
80 #else
81 (void)vp;
82 return 2;
83 #endif
86 static bool showing_bars(enum screen_type screen)
88 if (statusbar_enabled & VP_SB_ONSCREEN(screen))
90 #ifdef HAVE_LCD_BITMAP
91 bool ignore = statusbar_enabled & VP_SB_IGNORE_SETTING(screen);
92 return ignore || (statusbar_position(screen));
93 #else
94 return true;
95 #endif
97 return false;
100 void viewport_set_fullscreen(struct viewport *vp, enum screen_type screen)
102 vp->x = 0;
103 vp->width = screens[screen].lcdwidth;
105 #ifdef HAVE_LCD_BITMAP
106 vp->drawmode = DRMODE_SOLID;
107 vp->font = FONT_UI; /* default to UI to discourage SYSFONT use */
109 vp->height = screens[screen].lcdheight;
110 if (statusbar_position(screen) != STATUSBAR_BOTTOM && showing_bars(screen))
111 vp->y = STATUSBAR_HEIGHT;
112 else
113 vp->y = 0;
114 #else
115 vp->y = 0;
116 #endif
117 vp->height = screens[screen].lcdheight - (showing_bars(screen)?STATUSBAR_HEIGHT:0);
119 #if LCD_DEPTH > 1
120 #ifdef HAVE_REMOTE_LCD
121 /* We only need this test if there is a remote LCD */
122 if (screen == SCREEN_MAIN)
123 #endif
125 vp->fg_pattern = FG_FALLBACK;
126 vp->bg_pattern = BG_FALLBACK;
127 LINE_SEL_FROM_SETTINGS(vp);
129 #endif
131 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
132 if (screen == SCREEN_REMOTE)
134 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
135 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
137 #endif
140 void viewport_set_defaults(struct viewport *vp, enum screen_type screen)
142 #ifdef HAVE_LCD_BITMAP
143 if (ui_vp_info.active)
144 *vp = custom_vp[screen];
145 else
146 #endif
147 viewport_set_fullscreen(vp, screen);
150 void viewportmanager_init(void)
152 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
153 #ifdef HAVE_LCD_BITMAP
154 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
155 ui_vp_info.active = viewport_init_ui_vp();
156 ui_vp_info.vp = custom_vp;
157 #endif
160 int viewportmanager_get_statusbar(void)
162 return statusbar_enabled;
165 int viewportmanager_set_statusbar(int enabled)
167 int old = statusbar_enabled;
168 statusbar_enabled = enabled;
169 if (enabled)
171 int i;
172 FOR_NB_SCREENS(i)
174 if (showing_bars(i))
175 gui_statusbar_draw(&statusbars.statusbars[i], true);
177 if (!sb_skin_active())
178 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
180 else
182 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
184 return old;
187 static void viewportmanager_redraw(void* data)
189 int i;
191 FOR_NB_SCREENS(i)
193 if (showing_bars(i))
194 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
197 #ifdef HAVE_LCD_BITMAP
199 static void statusbar_toggled(void* param)
201 (void)param;
202 /* update vp manager for the new setting and reposition vps
203 * if necessary */
204 viewportmanager_theme_changed(THEME_STATUSBAR);
207 void viewportmanager_theme_changed(int which)
209 if (which & THEME_UI_VIEWPORT)
211 /* reset the ui viewport */
212 if ((ui_vp_info.active = viewport_init_ui_vp()))
213 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
214 else
215 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
216 /* and point to it */
217 ui_vp_info.vp = custom_vp;
219 if (which & THEME_STATUSBAR)
221 statusbar_enabled = 0;
222 if (global_settings.statusbar != STATUSBAR_OFF)
223 statusbar_enabled = VP_SB_ONSCREEN(SCREEN_MAIN);
224 #ifdef HAVE_REMOTE_LCD
225 if (global_settings.remote_statusbar != STATUSBAR_OFF)
226 statusbar_enabled |= VP_SB_ONSCREEN(SCREEN_REMOTE);
227 #endif
228 if (statusbar_enabled && !sb_skin_active())
229 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
230 else
231 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
233 /* reposition viewport to fit statusbar, only if not using the ui vp */
234 if (!ui_vp_info.active)
236 int i;
237 FOR_NB_SCREENS(i)
238 viewport_set_fullscreen(&custom_vp[i], i);
243 static void viewportmanager_ui_vp_changed(void *param)
245 /* if the user changed the theme, we need to initiate a full redraw */
246 int i;
247 /* cast param to a function */
248 void (*draw_func)(void) = ((void(*)(void))param);
249 /* start with clearing the screen */
250 FOR_NB_SCREENS(i)
251 screens[i].clear_display();
252 /* redraw the statusbar if it was enabled */
253 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
254 /* call the passed function which will redraw the content of
255 * the current screen */
256 if (param != NULL)
257 draw_func();
258 FOR_NB_SCREENS(i)
259 screens[i].update();
262 void viewport_set_current_vp(struct viewport* vp)
264 if (vp != NULL)
265 ui_vp_info.vp = vp;
266 else
267 ui_vp_info.vp = custom_vp;
270 struct viewport* viewport_get_current_vp(void)
272 return ui_vp_info.vp;
275 #ifdef HAVE_LCD_COLOR
276 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
277 #else
278 #define ARG_STRING(_depth) "dddddgg"
279 #endif
281 const char* viewport_parse_viewport(struct viewport *vp,
282 enum screen_type screen,
283 const char *bufptr,
284 const char separator)
286 /* parse the list to the viewport struct */
287 const char *ptr = bufptr;
288 int depth;
289 uint32_t set = 0;
291 enum {
292 PL_X = 0,
293 PL_Y,
294 PL_WIDTH,
295 PL_HEIGHT,
296 PL_FONT,
297 PL_FG,
298 PL_BG,
301 /* Work out the depth of this display */
302 depth = screens[screen].depth;
303 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
304 if (depth == 1)
306 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
307 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
308 return NULL;
310 else
311 #endif
312 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
313 if (depth >= 2)
315 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
316 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
317 &vp->fg_pattern,&vp->bg_pattern)))
318 return NULL;
320 else
321 #endif
323 #undef ARG_STRING
325 /* X and Y *must* be set */
326 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
327 return NULL;
329 /* fix defaults */
330 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
331 vp->width = screens[screen].lcdwidth - vp->x;
332 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
333 vp->height = screens[screen].lcdheight - vp->y;
335 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
336 if (!LIST_VALUE_PARSED(set, PL_FG))
337 vp->fg_pattern = FG_FALLBACK;
338 if (!LIST_VALUE_PARSED(set, PL_BG))
339 vp->bg_pattern = BG_FALLBACK;
340 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
342 LINE_SEL_FROM_SETTINGS(vp);
344 /* Validate the viewport dimensions - we know that the numbers are
345 non-negative integers, ignore bars and assume the viewport takes them
346 * into account */
347 if ((vp->x >= screens[screen].lcdwidth) ||
348 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
349 (vp->y >= screens[screen].lcdheight) ||
350 ((vp->y + vp->height) > screens[screen].lcdheight))
352 return NULL;
355 /* Default to using the user font if the font was an invalid number or '-'*/
356 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
357 || !LIST_VALUE_PARSED(set, PL_FONT)
359 vp->font = FONT_UI;
361 /* Set the defaults for fields not user-specified */
362 vp->drawmode = DRMODE_SOLID;
364 return ptr;
368 * (re)parse the UI vp from the settings
369 * - Returns
370 * 0 if no UI vp is used
371 * >0 if it's used at least partly (on multiscreen targets)
372 * NB_SCREENS if all screens have a UI vp
374 static int viewport_init_ui_vp(void)
376 int screen, ret = NB_SCREENS;
377 FOR_NB_SCREENS(screen)
379 #ifdef HAVE_REMOTE_LCD
380 if ((screen == SCREEN_REMOTE))
382 if(!(viewport_parse_viewport(&custom_vp[screen], screen,
383 global_settings.remote_ui_vp_config, ',')))
385 viewport_set_fullscreen(&custom_vp[screen], screen);
386 ret--;
389 else
390 #endif
392 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
393 global_settings.ui_vp_config, ',')))
395 viewport_set_fullscreen(&custom_vp[screen], screen);
396 ret--;
400 return ret;
403 #endif /* HAVE_LCD_BITMAP */