Forgot the #
[kugel-rb.git] / apps / gui / viewport.c
blob03f004ba87bff312161bf69b26afca9123c56b1a
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 static bool after_boot[NB_SCREENS] = {false};
103 int i;
105 FOR_NB_SCREENS(i)
107 enable_event = enable_event || is_theme_enabled(i);
108 sb_set_title_text(NULL, Icon_NOICON, i);
110 toggle_events(enable_event);
112 if (is_theme_enabled(screen))
114 bool first_boot = theme_stack_top[screen] == 0;
115 /* remove the left overs from the previous screen.
116 * could cause a tiny flicker. Redo your screen code if that happens */
117 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
118 screens[screen].backdrop_show(sb_get_backdrop(screen));
119 #endif
120 if (LIKELY(after_boot[screen]) && (!was_enabled[screen] || force))
122 struct viewport deadspace, user;
123 viewport_set_defaults(&user, screen);
124 deadspace = user; /* get colours and everything */
125 /* above */
126 deadspace.x = 0;
127 deadspace.y = 0;
128 deadspace.width = screens[screen].lcdwidth;
129 deadspace.height = user.y;
130 if (deadspace.width && deadspace.height)
132 screens[screen].set_viewport(&deadspace);
133 screens[screen].clear_viewport();
134 screens[screen].update_viewport();
136 /* below */
137 deadspace.y = user.y + user.height;
138 deadspace.height = screens[screen].lcdheight - deadspace.y;
139 if (deadspace.width && deadspace.height)
141 screens[screen].set_viewport(&deadspace);
142 screens[screen].clear_viewport();
143 screens[screen].update_viewport();
145 /* left */
146 deadspace.x = 0;
147 deadspace.y = 0;
148 deadspace.width = user.x;
149 deadspace.height = screens[screen].lcdheight;
150 if (deadspace.width && deadspace.height)
152 screens[screen].set_viewport(&deadspace);
153 screens[screen].clear_viewport();
154 screens[screen].update_viewport();
156 /* below */
157 deadspace.x = user.x + user.width;
158 deadspace.width = screens[screen].lcdwidth - deadspace.x;
159 if (deadspace.width && deadspace.height)
161 screens[screen].set_viewport(&deadspace);
162 screens[screen].clear_viewport();
163 screens[screen].update_viewport();
165 screens[screen].set_viewport(NULL);
167 intptr_t force = first_boot?0:1;
168 send_event(GUI_EVENT_ACTIONUPDATE, (void*)force);
170 else
172 #if LCD_DEPTH > 1 || (defined(LCD_REMOTE_DEPTH) && LCD_REMOTE_DEPTH > 1)
173 screens[screen].backdrop_show(NULL);
174 #endif
175 screens[screen].stop_scroll();
177 /* let list initialize viewport in case viewport dimensions is changed. */
178 send_event(GUI_EVENT_THEME_CHANGED, NULL);
179 FOR_NB_SCREENS(i)
180 was_enabled[i] = is_theme_enabled(i);
182 after_boot[screen] = true;
185 void viewportmanager_theme_enable(enum screen_type screen, bool enable,
186 struct viewport *viewport)
188 int top = ++theme_stack_top[screen];
189 if (top >= VPSTACK_DEPTH-1)
190 panicf("Stack overflow... viewportmanager");
191 theme_stack[screen][top].enabled = enable;
192 theme_stack[screen][top].vp = viewport;
193 toggle_theme(screen, false);
194 /* then be nice and set the viewport up */
195 if (viewport)
196 viewport_set_defaults(viewport, screen);
199 void viewportmanager_theme_undo(enum screen_type screen, bool force_redraw)
201 int top = --theme_stack_top[screen];
202 if (top < 0)
203 panicf("Stack underflow... viewportmanager");
205 toggle_theme(screen, force_redraw);
209 static bool is_theme_enabled(enum screen_type screen)
211 int top = theme_stack_top[screen];
212 return theme_stack[screen][top].enabled;
214 #endif /* HAVE_LCD_BITMAP */
216 int viewport_get_nb_lines(const struct viewport *vp)
218 #ifdef HAVE_LCD_BITMAP
219 return vp->height/font_get(vp->font)->height;
220 #else
221 (void)vp;
222 return 2;
223 #endif
226 static void viewportmanager_redraw(void* data)
228 int i;
229 FOR_NB_SCREENS(i)
231 #ifdef HAVE_LCD_BITMAP
232 if (is_theme_enabled(i))
233 sb_skin_update(i, NULL != data);
234 #else
235 (void)data;
236 gui_statusbar_draw(&statusbars.statusbars[i], NULL, NULL);
237 #endif
241 void viewportmanager_init()
243 #ifdef HAVE_LCD_BITMAP
244 int i;
245 FOR_NB_SCREENS(i)
247 theme_stack_top[i] = -1; /* the next call fixes this to 0 */
248 /* We always want the theme enabled by default... */
249 viewportmanager_theme_enable(i, true, NULL);
251 #else
252 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
253 #endif
256 #ifdef HAVE_LCD_BITMAP
257 void viewportmanager_theme_changed(const int which)
259 int i;
260 #ifdef HAVE_BUTTONBAR
261 if (which & THEME_BUTTONBAR)
262 { /* don't handle further, the custom ui viewport ignores the buttonbar,
263 * as does viewport_set_defaults(), since only lists use it*/
264 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
266 #endif
267 if (which & THEME_UI_VIEWPORT)
270 if (which & THEME_LANGUAGE)
273 if (which & THEME_STATUSBAR)
275 FOR_NB_SCREENS(i)
277 /* This can probably be done better...
278 * disable the theme so it's forced to do a full redraw */
279 viewportmanager_theme_enable(i, false, NULL);
280 viewportmanager_theme_undo(i, true);
283 send_event(GUI_EVENT_THEME_CHANGED, NULL);
286 #ifdef HAVE_TOUCHSCREEN
287 /* check if a point (x and y coordinates) are within a viewport */
288 bool viewport_point_within_vp(const struct viewport *vp,
289 const int x, const int y)
291 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
292 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
293 return (is_x && is_y);
295 #endif /* HAVE_TOUCHSCREEN */
297 static void set_default_align_flags(struct viewport *vp)
299 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
300 if (UNLIKELY(lang_is_rtl()))
301 vp->flags |= VP_FLAG_ALIGN_RIGHT;
304 #endif /* HAVE_LCD_BITMAP */
305 #endif /* __PCTOOL__ */
307 #ifdef HAVE_LCD_COLOR
308 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
309 #else
310 #define ARG_STRING(_depth) "dddddgg"
311 #endif
314 void viewport_set_fullscreen(struct viewport *vp,
315 const enum screen_type screen)
317 vp->x = 0;
318 vp->y = 0;
319 vp->width = screens[screen].lcdwidth;
320 vp->height = screens[screen].lcdheight;
322 #ifdef HAVE_LCD_BITMAP
323 #ifndef __PCTOOL__
324 set_default_align_flags(vp);
325 #endif
326 vp->font = FONT_UI + screen; /* default to UI to discourage SYSFONT use */
327 vp->drawmode = DRMODE_SOLID;
328 #if LCD_DEPTH > 1
329 #ifdef HAVE_REMOTE_LCD
330 /* We only need this test if there is a remote LCD */
331 if (screen == SCREEN_MAIN)
332 #endif
334 vp->fg_pattern = FG_FALLBACK;
335 vp->bg_pattern = BG_FALLBACK;
336 #ifdef HAVE_LCD_COLOR
337 vp->lss_pattern = global_settings.lss_color;
338 vp->lse_pattern = global_settings.lse_color;
339 vp->lst_pattern = global_settings.lst_color;
340 #endif
342 #endif
344 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
345 if (screen == SCREEN_REMOTE)
347 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
348 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
350 #endif
351 #endif
354 void viewport_set_defaults(struct viewport *vp,
355 const enum screen_type screen)
357 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
358 struct viewport *sbs_area = NULL;
359 if (!is_theme_enabled(screen))
361 viewport_set_fullscreen(vp, screen);
362 return;
364 sbs_area = sb_skin_get_info_vp(screen);
366 if (sbs_area)
367 *vp = *sbs_area;
368 else
369 #endif /* HAVE_LCD_BITMAP */
370 viewport_set_fullscreen(vp, screen);
374 #ifdef HAVE_LCD_BITMAP
375 const char* viewport_parse_viewport(struct viewport *vp,
376 enum screen_type screen,
377 const char *bufptr,
378 const char separator)
380 /* parse the list to the viewport struct */
381 const char *ptr = bufptr;
382 int depth;
383 uint32_t set = 0;
385 enum {
386 PL_X = 0,
387 PL_Y,
388 PL_WIDTH,
389 PL_HEIGHT,
390 PL_FONT,
391 PL_FG,
392 PL_BG,
395 /* Work out the depth of this display */
396 depth = screens[screen].depth;
397 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
398 if (depth == 1)
400 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
401 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
402 return NULL;
404 else
405 #endif
406 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
407 if (depth >= 2)
409 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
410 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
411 &vp->fg_pattern,&vp->bg_pattern)))
412 return NULL;
414 else
415 #endif
417 #undef ARG_STRING
419 /* X and Y *must* be set */
420 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
421 return NULL;
422 /* check for negative values */
423 if (vp->x < 0)
424 vp->x += screens[screen].lcdwidth;
425 if (vp->y < 0)
426 vp->y += screens[screen].lcdheight;
428 /* fix defaults,
429 * and negative width/height which means "extend to edge minus value */
430 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
431 vp->width = screens[screen].lcdwidth - vp->x;
432 else if (vp->width < 0)
433 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
434 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
435 vp->height = screens[screen].lcdheight - vp->y;
436 else if (vp->height < 0)
437 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
439 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
440 if (!LIST_VALUE_PARSED(set, PL_FG))
441 vp->fg_pattern = FG_FALLBACK;
442 if (!LIST_VALUE_PARSED(set, PL_BG))
443 vp->bg_pattern = BG_FALLBACK;
444 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
446 #ifdef HAVE_LCD_COLOR
447 vp->lss_pattern = global_settings.lss_color;
448 vp->lse_pattern = global_settings.lse_color;
449 vp->lst_pattern = global_settings.lst_color;
450 #endif
452 /* Validate the viewport dimensions - we know that the numbers are
453 non-negative integers, ignore bars and assume the viewport takes them
454 * into account */
455 if ((vp->x >= screens[screen].lcdwidth) ||
456 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
457 (vp->y >= screens[screen].lcdheight) ||
458 ((vp->y + vp->height) > screens[screen].lcdheight))
460 return NULL;
463 /* Default to using the user font if the font was an invalid number or '-'
464 * font 1 is *always* the UI font for the current screen
465 * 2 is always the first extra font */
466 if (!LIST_VALUE_PARSED(set, PL_FONT))
467 vp->font = FONT_UI;
469 /* Set the defaults for fields not user-specified */
470 vp->drawmode = DRMODE_SOLID;
471 #ifndef __PCTOOL__
472 set_default_align_flags(vp);
473 #endif
475 return ptr;
477 #endif