Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / gui / viewport.c
blob7d128188c39581eb9b58b2ca434fabe5f08b5839
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 "string.h"
45 #include "kernel.h"
46 #include "system.h"
47 #include "statusbar.h"
48 #include "appevents.h"
49 #include "panic.h"
50 #ifdef HAVE_LCD_BITMAP
51 #include "language.h"
52 #endif
53 #include "statusbar-skinned.h"
54 #include "debug.h"
55 #include "viewport.h"
57 #define VPSTACK_DEPTH 16
58 struct viewport_stack_item
60 struct viewport* vp;
61 bool enabled;
64 #ifdef HAVE_LCD_BITMAP
65 static void viewportmanager_redraw(void* data);
67 static int theme_stack_top[NB_SCREENS]; /* the last item added */
68 static struct viewport_stack_item theme_stack[NB_SCREENS][VPSTACK_DEPTH];
69 static bool is_theme_enabled(enum screen_type screen);
72 static void toggle_events(bool enable)
74 if (enable)
76 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
77 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
78 add_event(LCD_EVENT_ACTIVATION, false, do_sbs_update_callback);
79 #endif
80 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false,
81 do_sbs_update_callback);
82 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false,
83 do_sbs_update_callback);
85 else
87 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
88 remove_event(LCD_EVENT_ACTIVATION, do_sbs_update_callback);
89 #endif
90 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, do_sbs_update_callback);
91 remove_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, do_sbs_update_callback);
92 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
97 static void toggle_theme(enum screen_type screen, bool force)
99 bool enable_event = false;
100 static bool was_enabled[NB_SCREENS] = {false};
101 static bool after_boot[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 (LIKELY(after_boot[screen]) && (!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();
164 screens[screen].set_viewport(NULL);
166 intptr_t force = first_boot?0:1;
167 send_event(GUI_EVENT_ACTIONUPDATE, (void*)force);
169 else
171 #if LCD_DEPTH > 1 || (defined(LCD_REMOTE_DEPTH) && LCD_REMOTE_DEPTH > 1)
172 screens[screen].backdrop_show(NULL);
173 #endif
174 screens[screen].stop_scroll();
176 /* let list initialize viewport in case viewport dimensions is changed. */
177 send_event(GUI_EVENT_THEME_CHANGED, NULL);
178 FOR_NB_SCREENS(i)
179 was_enabled[i] = is_theme_enabled(i);
181 after_boot[screen] = true;
184 void viewportmanager_theme_enable(enum screen_type screen, bool enable,
185 struct viewport *viewport)
187 int top = ++theme_stack_top[screen];
188 if (top >= VPSTACK_DEPTH-1)
189 panicf("Stack overflow... viewportmanager");
190 theme_stack[screen][top].enabled = enable;
191 theme_stack[screen][top].vp = viewport;
192 toggle_theme(screen, false);
193 /* then be nice and set the viewport up */
194 if (viewport)
195 viewport_set_defaults(viewport, screen);
198 void viewportmanager_theme_undo(enum screen_type screen, bool force_redraw)
200 int top = --theme_stack_top[screen];
201 if (top < 0)
202 panicf("Stack underflow... viewportmanager");
204 toggle_theme(screen, force_redraw);
208 static bool is_theme_enabled(enum screen_type screen)
210 int top = theme_stack_top[screen];
211 return theme_stack[screen][top].enabled;
213 #endif /* HAVE_LCD_BITMAP */
215 int viewport_get_nb_lines(const struct viewport *vp)
217 #ifdef HAVE_LCD_BITMAP
218 return vp->height/font_get(vp->font)->height;
219 #else
220 (void)vp;
221 return 2;
222 #endif
225 static void viewportmanager_redraw(void* data)
227 int i;
228 FOR_NB_SCREENS(i)
230 #ifdef HAVE_LCD_BITMAP
231 if (is_theme_enabled(i))
232 sb_skin_update(i, NULL != data);
233 #else
234 (void)data;
235 gui_statusbar_draw(&statusbars.statusbars[i], NULL, NULL);
236 #endif
240 void viewportmanager_init()
242 #ifdef HAVE_LCD_BITMAP
243 int i;
244 FOR_NB_SCREENS(i)
246 theme_stack_top[i] = -1; /* the next call fixes this to 0 */
247 /* We always want the theme enabled by default... */
248 viewportmanager_theme_enable(i, true, NULL);
250 #else
251 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
252 #endif
255 #ifdef HAVE_LCD_BITMAP
256 void viewportmanager_theme_changed(const int which)
258 int i;
259 #ifdef HAVE_BUTTONBAR
260 if (which & THEME_BUTTONBAR)
261 { /* don't handle further, the custom ui viewport ignores the buttonbar,
262 * as does viewport_set_defaults(), since only lists use it*/
263 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
265 #endif
266 if (which & THEME_UI_VIEWPORT)
269 if (which & THEME_LANGUAGE)
272 if (which & THEME_STATUSBAR)
274 FOR_NB_SCREENS(i)
276 /* This can probably be done better...
277 * disable the theme so it's forced to do a full redraw */
278 viewportmanager_theme_enable(i, false, NULL);
279 viewportmanager_theme_undo(i, true);
282 send_event(GUI_EVENT_THEME_CHANGED, NULL);
285 #ifdef HAVE_TOUCHSCREEN
286 /* check if a point (x and y coordinates) are within a viewport */
287 bool viewport_point_within_vp(const struct viewport *vp,
288 const int x, const int y)
290 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
291 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
292 return (is_x && is_y);
294 #endif /* HAVE_TOUCHSCREEN */
296 static void set_default_align_flags(struct viewport *vp)
298 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
299 if (UNLIKELY(lang_is_rtl()))
300 vp->flags |= VP_FLAG_ALIGN_RIGHT;
303 #endif /* HAVE_LCD_BITMAP */
304 #endif /* __PCTOOL__ */
306 #ifdef HAVE_LCD_COLOR
307 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
308 #else
309 #define ARG_STRING(_depth) "dddddgg"
310 #endif
313 void viewport_set_fullscreen(struct viewport *vp,
314 const enum screen_type screen)
316 vp->x = 0;
317 vp->y = 0;
318 vp->width = screens[screen].lcdwidth;
319 vp->height = screens[screen].lcdheight;
321 #ifdef HAVE_LCD_BITMAP
322 #ifndef __PCTOOL__
323 set_default_align_flags(vp);
324 #endif
325 vp->font = FONT_UI + screen; /* default to UI to discourage SYSFONT use */
326 vp->drawmode = DRMODE_SOLID;
327 #if LCD_DEPTH > 1
328 #ifdef HAVE_REMOTE_LCD
329 /* We only need this test if there is a remote LCD */
330 if (screen == SCREEN_MAIN)
331 #endif
333 vp->fg_pattern = FG_FALLBACK;
334 vp->bg_pattern = BG_FALLBACK;
335 #ifdef HAVE_LCD_COLOR
336 vp->lss_pattern = global_settings.lss_color;
337 vp->lse_pattern = global_settings.lse_color;
338 vp->lst_pattern = global_settings.lst_color;
339 #endif
341 #endif
343 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
344 if (screen == SCREEN_REMOTE)
346 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
347 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
349 #endif
350 #endif
353 void viewport_set_defaults(struct viewport *vp,
354 const enum screen_type screen)
356 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
357 struct viewport *sbs_area = NULL;
358 if (!is_theme_enabled(screen))
360 viewport_set_fullscreen(vp, screen);
361 return;
363 sbs_area = sb_skin_get_info_vp(screen);
365 if (sbs_area)
366 *vp = *sbs_area;
367 else
368 #endif /* HAVE_LCD_BITMAP */
369 viewport_set_fullscreen(vp, screen);
373 #ifdef HAVE_LCD_BITMAP
374 const char* viewport_parse_viewport(struct viewport *vp,
375 enum screen_type screen,
376 const char *bufptr,
377 const char separator)
379 /* parse the list to the viewport struct */
380 const char *ptr = bufptr;
381 int depth;
382 uint32_t set = 0;
384 enum {
385 PL_X = 0,
386 PL_Y,
387 PL_WIDTH,
388 PL_HEIGHT,
389 PL_FONT,
390 PL_FG,
391 PL_BG,
394 /* Work out the depth of this display */
395 depth = screens[screen].depth;
396 #if (LCD_DEPTH == 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 1)
397 if (depth == 1)
399 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
400 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
401 return NULL;
403 else
404 #endif
405 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
406 if (depth >= 2)
408 if (!(ptr = parse_list(ARG_STRING(depth), &set, separator, ptr,
409 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font,
410 &vp->fg_pattern,&vp->bg_pattern)))
411 return NULL;
413 else
414 #endif
416 #undef ARG_STRING
418 /* X and Y *must* be set */
419 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
420 return NULL;
421 /* check for negative values */
422 if (vp->x < 0)
423 vp->x += screens[screen].lcdwidth;
424 if (vp->y < 0)
425 vp->y += screens[screen].lcdheight;
427 /* fix defaults,
428 * and negative width/height which means "extend to edge minus value */
429 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
430 vp->width = screens[screen].lcdwidth - vp->x;
431 else if (vp->width < 0)
432 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
433 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
434 vp->height = screens[screen].lcdheight - vp->y;
435 else if (vp->height < 0)
436 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
438 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
439 if (!LIST_VALUE_PARSED(set, PL_FG))
440 vp->fg_pattern = FG_FALLBACK;
441 if (!LIST_VALUE_PARSED(set, PL_BG))
442 vp->bg_pattern = BG_FALLBACK;
443 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
445 #ifdef HAVE_LCD_COLOR
446 vp->lss_pattern = global_settings.lss_color;
447 vp->lse_pattern = global_settings.lse_color;
448 vp->lst_pattern = global_settings.lst_color;
449 #endif
451 /* Validate the viewport dimensions - we know that the numbers are
452 non-negative integers, ignore bars and assume the viewport takes them
453 * into account */
454 if ((vp->x >= screens[screen].lcdwidth) ||
455 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
456 (vp->y >= screens[screen].lcdheight) ||
457 ((vp->y + vp->height) > screens[screen].lcdheight))
459 return NULL;
462 /* Default to using the user font if the font was an invalid number or '-'
463 * font 1 is *always* the UI font for the current screen
464 * 2 is always the first extra font */
465 if (!LIST_VALUE_PARSED(set, PL_FONT))
466 vp->font = FONT_UI;
468 /* Set the defaults for fields not user-specified */
469 vp->drawmode = DRMODE_SOLID;
470 #ifndef __PCTOOL__
471 set_default_align_flags(vp);
472 #endif
474 return ptr;
476 #endif