Fix the pitch detector plugin for iaudioM3
[kugel-rb.git] / apps / gui / viewport.c
blob06caa2379d9275227eb37c1ecf0901c21330ffe8
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[NB_SCREENS];
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 unsigned 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[screen])
143 *vp = ui_vp_info.vp[screen];
144 else
145 #endif
146 viewport_set_fullscreen(vp, screen);
149 void viewportmanager_init(void)
151 #ifdef HAVE_LCD_BITMAP
152 int retval, i;
153 add_event(GUI_EVENT_STATUSBAR_TOGGLE, false, statusbar_toggled);
154 retval = viewport_init_ui_vp();
155 FOR_NB_SCREENS(i)
156 ui_vp_info.active[i] = retval & BIT_N(i);
157 ui_vp_info.vp = custom_vp;
158 #endif
159 viewportmanager_set_statusbar(VP_SB_ALLSCREENS);
162 int viewportmanager_get_statusbar(void)
164 return statusbar_enabled;
167 int viewportmanager_set_statusbar(int enabled)
169 int old = statusbar_enabled;
170 statusbar_enabled = enabled;
171 if (enabled)
173 int i;
174 FOR_NB_SCREENS(i)
176 if (showing_bars(i))
177 gui_statusbar_draw(&statusbars.statusbars[i], true);
179 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
181 else
183 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
185 return old;
188 static void viewportmanager_redraw(void* data)
190 int i;
192 FOR_NB_SCREENS(i)
194 if (showing_bars(i))
195 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
198 #ifdef HAVE_LCD_BITMAP
200 static void statusbar_toggled(void* param)
202 (void)param;
203 /* update vp manager for the new setting and reposition vps
204 * if necessary */
205 viewportmanager_theme_changed(THEME_STATUSBAR);
208 void viewportmanager_theme_changed(int which)
210 int i;
211 #ifdef HAVE_BUTTONBAR
212 if (which & THEME_BUTTONBAR)
213 { /* don't handle further, the custom ui viewport ignores the buttonbar,
214 * as does viewport_set_defaults(), since only lists use it*/
215 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
217 #endif
218 if (which & THEME_UI_VIEWPORT)
220 int retval = viewport_init_ui_vp();
221 /* reset the ui viewport */
222 FOR_NB_SCREENS(i)
223 ui_vp_info.active[i] = retval & BIT_N(i);
225 if (retval != 0)
226 add_event(GUI_EVENT_REFRESH, false, viewportmanager_ui_vp_changed);
227 else
228 remove_event(GUI_EVENT_REFRESH, viewportmanager_ui_vp_changed);
229 /* and point to it */
230 ui_vp_info.vp = custom_vp;
232 if (which & THEME_STATUSBAR)
234 statusbar_enabled = VP_SB_HIDE_ALL;
236 FOR_NB_SCREENS(i)
238 if (statusbar_position(i) != STATUSBAR_OFF)
239 statusbar_enabled |= VP_SB_ONSCREEN(i);
242 if (statusbar_enabled != VP_SB_HIDE_ALL)
243 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
244 else
245 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
247 /* reposition viewport to fit statusbar, only if not using the ui vp */
249 FOR_NB_SCREENS(i)
251 if (!ui_vp_info.active[i])
252 viewport_set_fullscreen(&custom_vp[i], i);
255 send_event(GUI_EVENT_THEME_CHANGED, NULL);
258 static void viewportmanager_ui_vp_changed(void *param)
260 /* if the user changed the theme, we need to initiate a full redraw */
261 int i;
262 /* cast param to a function */
263 void (*draw_func)(void) = ((void(*)(void))param);
264 /* start with clearing the screen */
265 FOR_NB_SCREENS(i)
266 screens[i].clear_display();
267 /* redraw the statusbar if it was enabled */
268 viewportmanager_set_statusbar(statusbar_enabled);
269 /* call the passed function which will redraw the content of
270 * the current screen */
271 if (param != NULL)
272 draw_func();
273 FOR_NB_SCREENS(i)
274 screens[i].update();
277 void viewport_set_current_vp(struct viewport* vp)
279 if (vp != NULL)
280 ui_vp_info.vp = vp;
281 else
282 ui_vp_info.vp = custom_vp;
284 /* must be done after the assignment above or event handler get old vps */
285 send_event(GUI_EVENT_THEME_CHANGED, NULL);
288 struct viewport* viewport_get_current_vp(void)
290 return ui_vp_info.vp;
293 bool viewport_ui_vp_get_state(enum screen_type screen)
295 return ui_vp_info.active[screen];
298 #ifdef HAVE_LCD_COLOR
299 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
300 #else
301 #define ARG_STRING(_depth) "dddddgg"
302 #endif
304 const char* viewport_parse_viewport(struct viewport *vp,
305 enum screen_type screen,
306 const char *bufptr,
307 const char separator)
309 /* parse the list to the viewport struct */
310 const char *ptr = bufptr;
311 int depth;
312 uint32_t set = 0;
314 enum {
315 PL_X = 0,
316 PL_Y,
317 PL_WIDTH,
318 PL_HEIGHT,
319 PL_FONT,
320 PL_FG,
321 PL_BG,
324 /* Work out the depth of this display */
325 depth = screens[screen].depth;
326 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
327 if (depth == 1)
329 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
330 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
331 return NULL;
333 else
334 #endif
335 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
336 if (depth >= 2)
338 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
339 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
340 &vp->fg_pattern,&vp->bg_pattern)))
341 return NULL;
343 else
344 #endif
346 #undef ARG_STRING
348 /* X and Y *must* be set */
349 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
350 return NULL;
352 /* fix defaults */
353 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
354 vp->width = screens[screen].lcdwidth - vp->x;
355 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
356 vp->height = screens[screen].lcdheight - vp->y;
358 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
359 if (!LIST_VALUE_PARSED(set, PL_FG))
360 vp->fg_pattern = FG_FALLBACK;
361 if (!LIST_VALUE_PARSED(set, PL_BG))
362 vp->bg_pattern = BG_FALLBACK;
363 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
365 LINE_SEL_FROM_SETTINGS(vp);
367 /* Validate the viewport dimensions - we know that the numbers are
368 non-negative integers, ignore bars and assume the viewport takes them
369 * into account */
370 if ((vp->x >= screens[screen].lcdwidth) ||
371 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
372 (vp->y >= screens[screen].lcdheight) ||
373 ((vp->y + vp->height) > screens[screen].lcdheight))
375 return NULL;
378 /* Default to using the user font if the font was an invalid number or '-'*/
379 if (((vp->font != FONT_SYSFIXED) && (vp->font != FONT_UI))
380 || !LIST_VALUE_PARSED(set, PL_FONT)
382 vp->font = FONT_UI;
384 /* Set the defaults for fields not user-specified */
385 vp->drawmode = DRMODE_SOLID;
387 return ptr;
391 * (re)parse the UI vp from the settings
392 * - Returns
393 * 0 if no UI vp is used at all
394 * else the bit for the screen (1<<screen) is set
396 static unsigned viewport_init_ui_vp(void)
398 int screen;
399 unsigned ret = 0;
400 char *setting;
401 FOR_NB_SCREENS(screen)
403 #ifdef HAVE_REMOTE_LCD
404 if ((screen == SCREEN_REMOTE))
405 setting = global_settings.remote_ui_vp_config;
406 else
407 #endif
408 setting = global_settings.ui_vp_config;
411 if (!(viewport_parse_viewport(&custom_vp[screen], screen,
412 setting, ',')))
413 viewport_set_fullscreen(&custom_vp[screen], screen);
414 else
415 ret |= BIT_N(screen);
417 return ret;
420 #ifdef HAVE_TOUCHSCREEN
421 /* check if a point (x and y coordinates) are within a viewport */
422 bool viewport_point_within_vp(const struct viewport *vp, int x, int y)
424 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
425 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
426 return (is_x && is_y);
428 #endif /* HAVE_TOUCHSCREEN */
429 #endif /* HAVE_LCD_BITMAP */