split the theme settings apply() sutff out of settings_apply(). this should fix splas...
[kugel-rb.git] / apps / gui / viewport.c
blob9b35c86c172f1d179fe3206f9ba148461e7b217c
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 /* all below isn't needed for pc tools (i.e. checkwps/wps editor)
42 * only viewport_parse_viewport() is */
43 #ifndef __PCTOOL__
44 #include "sprintf.h"
45 #include "string.h"
46 #include "kernel.h"
47 #include "system.h"
48 #include "statusbar.h"
49 #include "appevents.h"
50 #include "panic.h"
51 #ifdef HAVE_LCD_BITMAP
52 #include "language.h"
53 #endif
54 #include "statusbar-skinned.h"
55 #include "debug.h"
56 #include "viewport.h"
58 #define VPSTACK_DEPTH 16
59 struct viewport_stack_item
61 struct viewport* vp;
62 bool enabled;
65 #ifdef HAVE_LCD_BITMAP
66 static void viewportmanager_redraw(void* data);
68 static int theme_stack_top[NB_SCREENS]; /* the last item added */
69 static struct viewport_stack_item theme_stack[NB_SCREENS][VPSTACK_DEPTH];
70 static bool is_theme_enabled(enum screen_type screen);
73 static void toggle_events(bool enable)
75 if (enable)
77 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
78 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
79 add_event(LCD_EVENT_ACTIVATION, false, do_sbs_update_callback);
80 #endif
81 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false,
82 do_sbs_update_callback);
83 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false,
84 do_sbs_update_callback);
86 else
88 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
89 remove_event(LCD_EVENT_ACTIVATION, do_sbs_update_callback);
90 #endif
91 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, do_sbs_update_callback);
92 remove_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, do_sbs_update_callback);
93 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
98 static void toggle_theme(enum screen_type screen, bool force)
100 bool enable_event = false;
101 static bool was_enabled[NB_SCREENS] = {false};
102 int i;
104 FOR_NB_SCREENS(i)
106 enable_event = enable_event || is_theme_enabled(i);
107 sb_set_title_text(NULL, Icon_NOICON, i);
109 toggle_events(enable_event);
111 if (is_theme_enabled(screen))
113 bool first_boot = theme_stack_top[screen] == 0;
114 /* remove the left overs from the previous screen.
115 * could cause a tiny flicker. Redo your screen code if that happens */
116 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
117 screens[screen].backdrop_show(sb_get_backdrop(screen));
118 #endif
119 if (!first_boot && (!was_enabled[screen] || force))
121 struct viewport deadspace, user;
122 viewport_set_defaults(&user, screen);
123 deadspace = user; /* get colours and everything */
124 /* above */
125 deadspace.x = 0;
126 deadspace.y = 0;
127 deadspace.width = screens[screen].lcdwidth;
128 deadspace.height = user.y;
129 if (deadspace.width && deadspace.height)
131 screens[screen].set_viewport(&deadspace);
132 screens[screen].clear_viewport();
133 screens[screen].update_viewport();
135 /* below */
136 deadspace.y = user.y + user.height;
137 deadspace.height = screens[screen].lcdheight - deadspace.y;
138 if (deadspace.width && deadspace.height)
140 screens[screen].set_viewport(&deadspace);
141 screens[screen].clear_viewport();
142 screens[screen].update_viewport();
144 /* left */
145 deadspace.x = 0;
146 deadspace.y = 0;
147 deadspace.width = user.x;
148 deadspace.height = screens[screen].lcdheight;
149 if (deadspace.width && deadspace.height)
151 screens[screen].set_viewport(&deadspace);
152 screens[screen].clear_viewport();
153 screens[screen].update_viewport();
155 /* below */
156 deadspace.x = user.x + user.width;
157 deadspace.width = screens[screen].lcdwidth - deadspace.x;
158 if (deadspace.width && deadspace.height)
160 screens[screen].set_viewport(&deadspace);
161 screens[screen].clear_viewport();
162 screens[screen].update_viewport();
165 send_event(GUI_EVENT_ACTIONUPDATE, (void*)!first_boot);
167 else
169 #if LCD_DEPTH > 1 || (defined(LCD_REMOTE_DEPTH) && LCD_REMOTE_DEPTH > 1)
170 screens[screen].backdrop_show(NULL);
171 #endif
172 screens[screen].stop_scroll();
174 /* let list initialize viewport in case viewport dimensions is changed. */
175 send_event(GUI_EVENT_THEME_CHANGED, NULL);
176 FOR_NB_SCREENS(i)
177 was_enabled[i] = is_theme_enabled(i);
180 void viewportmanager_theme_enable(enum screen_type screen, bool enable,
181 struct viewport *viewport)
183 int top = ++theme_stack_top[screen];
184 if (top >= VPSTACK_DEPTH-1)
185 panicf("Stack overflow... viewportmanager");
186 theme_stack[screen][top].enabled = enable;
187 theme_stack[screen][top].vp = viewport;
188 toggle_theme(screen, false);
189 /* then be nice and set the viewport up */
190 if (viewport)
191 viewport_set_defaults(viewport, screen);
194 void viewportmanager_theme_undo(enum screen_type screen, bool force_redraw)
196 int top = --theme_stack_top[screen];
197 if (top < 0)
198 panicf("Stack underflow... viewportmanager");
200 toggle_theme(screen, force_redraw);
204 static bool is_theme_enabled(enum screen_type screen)
206 int top = theme_stack_top[screen];
207 return theme_stack[screen][top].enabled;
209 #endif /* HAVE_LCD_BITMAP */
211 int viewport_get_nb_lines(const struct viewport *vp)
213 #ifdef HAVE_LCD_BITMAP
214 return vp->height/font_get(vp->font)->height;
215 #else
216 (void)vp;
217 return 2;
218 #endif
221 static void viewportmanager_redraw(void* data)
223 int i;
224 FOR_NB_SCREENS(i)
226 #ifdef HAVE_LCD_BITMAP
227 if (statusbar_position(i) == STATUSBAR_CUSTOM)
228 sb_skin_update(i, NULL != data);
229 else if (statusbar_position(i) != STATUSBAR_OFF)
230 #endif
231 gui_statusbar_draw(&statusbars.statusbars[i], NULL != data);
235 void viewportmanager_init()
237 #ifdef HAVE_LCD_BITMAP
238 int i;
239 FOR_NB_SCREENS(i)
241 theme_stack_top[i] = -1; /* the next call fixes this to 0 */
242 /* We always want the theme enabled by default... */
243 viewportmanager_theme_enable(i, true, NULL);
245 #else
246 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
247 #endif
250 #ifdef HAVE_LCD_BITMAP
251 void viewportmanager_theme_changed(const int which)
253 int i;
254 #ifdef HAVE_BUTTONBAR
255 if (which & THEME_BUTTONBAR)
256 { /* don't handle further, the custom ui viewport ignores the buttonbar,
257 * as does viewport_set_defaults(), since only lists use it*/
258 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
260 #endif
261 if (which & THEME_UI_VIEWPORT)
264 if (which & THEME_LANGUAGE)
267 if (which & THEME_STATUSBAR)
269 FOR_NB_SCREENS(i)
271 /* This can probably be done better...
272 * disable the theme so it's forced to do a full redraw */
273 viewportmanager_theme_enable(i, false, NULL);
274 viewportmanager_theme_undo(i, true);
277 send_event(GUI_EVENT_THEME_CHANGED, NULL);
280 #ifdef HAVE_TOUCHSCREEN
281 /* check if a point (x and y coordinates) are within a viewport */
282 bool viewport_point_within_vp(const struct viewport *vp,
283 const int x, const int y)
285 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
286 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
287 return (is_x && is_y);
289 #endif /* HAVE_TOUCHSCREEN */
291 static void set_default_align_flags(struct viewport *vp)
293 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
294 if (UNLIKELY(lang_is_rtl()))
295 vp->flags |= VP_FLAG_ALIGN_RIGHT;
298 #endif /* HAVE_LCD_BITMAP */
299 #endif /* __PCTOOL__ */
301 #ifdef HAVE_LCD_COLOR
302 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
303 #else
304 #define ARG_STRING(_depth) "dddddgg"
305 #endif
308 void viewport_set_fullscreen(struct viewport *vp,
309 const enum screen_type screen)
311 vp->x = 0;
312 vp->y = 0;
313 vp->width = screens[screen].lcdwidth;
314 vp->height = screens[screen].lcdheight;
316 #ifdef HAVE_LCD_BITMAP
317 #ifndef __PCTOOL__
318 set_default_align_flags(vp);
319 #endif
320 vp->font = FONT_UI + screen; /* default to UI to discourage SYSFONT use */
321 vp->drawmode = DRMODE_SOLID;
322 #if LCD_DEPTH > 1
323 #ifdef HAVE_REMOTE_LCD
324 /* We only need this test if there is a remote LCD */
325 if (screen == SCREEN_MAIN)
326 #endif
328 vp->fg_pattern = FG_FALLBACK;
329 vp->bg_pattern = BG_FALLBACK;
330 #ifdef HAVE_LCD_COLOR
331 vp->lss_pattern = global_settings.lss_color;
332 vp->lse_pattern = global_settings.lse_color;
333 vp->lst_pattern = global_settings.lst_color;
334 #endif
336 #endif
338 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
339 if (screen == SCREEN_REMOTE)
341 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
342 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
344 #endif
345 #endif
348 void viewport_set_defaults(struct viewport *vp,
349 const enum screen_type screen)
351 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
353 struct viewport *sbs_area = NULL;
354 if (!is_theme_enabled(screen))
356 viewport_set_fullscreen(vp, screen);
357 return;
359 sbs_area = sb_skin_get_info_vp(screen);
361 if (sbs_area)
362 *vp = *sbs_area;
363 else
364 #endif /* HAVE_LCD_BITMAP */
365 viewport_set_fullscreen(vp, screen);
369 #ifdef HAVE_LCD_BITMAP
370 const char* viewport_parse_viewport(struct viewport *vp,
371 enum screen_type screen,
372 const char *bufptr,
373 const char separator)
375 /* parse the list to the viewport struct */
376 const char *ptr = bufptr;
377 int depth;
378 uint32_t set = 0;
380 enum {
381 PL_X = 0,
382 PL_Y,
383 PL_WIDTH,
384 PL_HEIGHT,
385 PL_FONT,
386 PL_FG,
387 PL_BG,
390 /* Work out the depth of this display */
391 depth = screens[screen].depth;
392 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
393 if (depth == 1)
395 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
396 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
397 return NULL;
399 else
400 #endif
401 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
402 if (depth >= 2)
404 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
405 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
406 &vp->fg_pattern,&vp->bg_pattern)))
407 return NULL;
409 else
410 #endif
412 #undef ARG_STRING
414 /* X and Y *must* be set */
415 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
416 return NULL;
417 /* check for negative values */
418 if (vp->x < 0)
419 vp->x += screens[screen].lcdwidth;
420 if (vp->y < 0)
421 vp->y += screens[screen].lcdheight;
423 /* fix defaults,
424 * and negative width/height which means "extend to edge minus value */
425 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
426 vp->width = screens[screen].lcdwidth - vp->x;
427 else if (vp->width < 0)
428 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
429 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
430 vp->height = screens[screen].lcdheight - vp->y;
431 else if (vp->height < 0)
432 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
434 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
435 if (!LIST_VALUE_PARSED(set, PL_FG))
436 vp->fg_pattern = FG_FALLBACK;
437 if (!LIST_VALUE_PARSED(set, PL_BG))
438 vp->bg_pattern = BG_FALLBACK;
439 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
441 #ifdef HAVE_LCD_COLOR
442 vp->lss_pattern = global_settings.lss_color;
443 vp->lse_pattern = global_settings.lse_color;
444 vp->lst_pattern = global_settings.lst_color;
445 #endif
447 /* Validate the viewport dimensions - we know that the numbers are
448 non-negative integers, ignore bars and assume the viewport takes them
449 * into account */
450 if ((vp->x >= screens[screen].lcdwidth) ||
451 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
452 (vp->y >= screens[screen].lcdheight) ||
453 ((vp->y + vp->height) > screens[screen].lcdheight))
455 return NULL;
458 /* Default to using the user font if the font was an invalid number or '-'
459 * font 1 is *always* the UI font for the current screen
460 * 2 is always the first extra font */
461 if (!LIST_VALUE_PARSED(set, PL_FONT))
462 vp->font = FONT_UI;
464 /* Set the defaults for fields not user-specified */
465 vp->drawmode = DRMODE_SOLID;
466 #ifndef __PCTOOL__
467 set_default_align_flags(vp);
468 #endif
470 return ptr;
472 #endif