Changeover to the new skin format. SkinBreakingChange in the wiki has the runown...
[kugel-rb.git] / apps / gui / viewport.c
blob8731015c79ac4b38345f2c6a57220931b0379a17
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
40 #ifdef HAVE_REMOTE_LCD
41 #define REMOTE_FG_FALLBACK LCD_REMOTE_DEFAULT_FG
42 #define REMOTE_BG_FALLBACK LCD_REMOTE_DEFAULT_BG
43 #endif
46 /* all below isn't needed for pc tools (i.e. checkwps/wps editor)
47 * only viewport_parse_viewport() is */
48 #ifndef __PCTOOL__
49 #include "string.h"
50 #include "kernel.h"
51 #include "system.h"
52 #include "statusbar.h"
53 #include "appevents.h"
54 #include "panic.h"
55 #ifdef HAVE_LCD_BITMAP
56 #include "language.h"
57 #endif
58 #include "statusbar-skinned.h"
59 #include "debug.h"
60 #include "viewport.h"
62 #define VPSTACK_DEPTH 16
63 struct viewport_stack_item
65 struct viewport* vp;
66 bool enabled;
69 #ifdef HAVE_LCD_BITMAP
70 static void viewportmanager_redraw(void* data);
72 static int theme_stack_top[NB_SCREENS]; /* the last item added */
73 static struct viewport_stack_item theme_stack[NB_SCREENS][VPSTACK_DEPTH];
74 static bool is_theme_enabled(enum screen_type screen);
77 static void toggle_events(bool enable)
79 if (enable)
81 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
82 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
83 add_event(LCD_EVENT_ACTIVATION, false, do_sbs_update_callback);
84 #endif
85 add_event(PLAYBACK_EVENT_TRACK_CHANGE, false,
86 do_sbs_update_callback);
87 add_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, false,
88 do_sbs_update_callback);
90 else
92 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
93 remove_event(LCD_EVENT_ACTIVATION, do_sbs_update_callback);
94 #endif
95 remove_event(PLAYBACK_EVENT_TRACK_CHANGE, do_sbs_update_callback);
96 remove_event(PLAYBACK_EVENT_NEXTTRACKID3_AVAILABLE, do_sbs_update_callback);
97 remove_event(GUI_EVENT_ACTIONUPDATE, viewportmanager_redraw);
102 static void toggle_theme(enum screen_type screen, bool force)
104 bool enable_event = false;
105 static bool was_enabled[NB_SCREENS] = {false};
106 static bool after_boot[NB_SCREENS] = {false};
107 int i;
109 FOR_NB_SCREENS(i)
111 enable_event = enable_event || is_theme_enabled(i);
112 sb_set_title_text(NULL, Icon_NOICON, i);
114 toggle_events(enable_event);
116 if (is_theme_enabled(screen))
118 bool first_boot = theme_stack_top[screen] == 0;
119 /* remove the left overs from the previous screen.
120 * could cause a tiny flicker. Redo your screen code if that happens */
121 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
122 screens[screen].backdrop_show(sb_get_backdrop(screen));
123 #endif
124 if (LIKELY(after_boot[screen]) && (!was_enabled[screen] || force))
126 struct viewport deadspace, user;
127 viewport_set_defaults(&user, screen);
128 deadspace = user; /* get colours and everything */
129 /* above */
130 deadspace.x = 0;
131 deadspace.y = 0;
132 deadspace.width = screens[screen].lcdwidth;
133 deadspace.height = user.y;
134 if (deadspace.width && deadspace.height)
136 screens[screen].set_viewport(&deadspace);
137 screens[screen].clear_viewport();
138 screens[screen].update_viewport();
140 /* below */
141 deadspace.y = user.y + user.height;
142 deadspace.height = screens[screen].lcdheight - deadspace.y;
143 if (deadspace.width && deadspace.height)
145 screens[screen].set_viewport(&deadspace);
146 screens[screen].clear_viewport();
147 screens[screen].update_viewport();
149 /* left */
150 deadspace.x = 0;
151 deadspace.y = 0;
152 deadspace.width = user.x;
153 deadspace.height = screens[screen].lcdheight;
154 if (deadspace.width && deadspace.height)
156 screens[screen].set_viewport(&deadspace);
157 screens[screen].clear_viewport();
158 screens[screen].update_viewport();
160 /* below */
161 deadspace.x = user.x + user.width;
162 deadspace.width = screens[screen].lcdwidth - deadspace.x;
163 if (deadspace.width && deadspace.height)
165 screens[screen].set_viewport(&deadspace);
166 screens[screen].clear_viewport();
167 screens[screen].update_viewport();
169 screens[screen].set_viewport(NULL);
171 intptr_t force = first_boot?0:1;
172 send_event(GUI_EVENT_ACTIONUPDATE, (void*)force);
174 else
176 #if LCD_DEPTH > 1 || (defined(LCD_REMOTE_DEPTH) && LCD_REMOTE_DEPTH > 1)
177 screens[screen].backdrop_show(NULL);
178 #endif
179 screens[screen].stop_scroll();
181 /* let list initialize viewport in case viewport dimensions is changed. */
182 send_event(GUI_EVENT_THEME_CHANGED, NULL);
183 FOR_NB_SCREENS(i)
184 was_enabled[i] = is_theme_enabled(i);
186 after_boot[screen] = true;
189 void viewportmanager_theme_enable(enum screen_type screen, bool enable,
190 struct viewport *viewport)
192 int top = ++theme_stack_top[screen];
193 if (top >= VPSTACK_DEPTH-1)
194 panicf("Stack overflow... viewportmanager");
195 theme_stack[screen][top].enabled = enable;
196 theme_stack[screen][top].vp = viewport;
197 toggle_theme(screen, false);
198 /* then be nice and set the viewport up */
199 if (viewport)
200 viewport_set_defaults(viewport, screen);
203 void viewportmanager_theme_undo(enum screen_type screen, bool force_redraw)
205 int top = --theme_stack_top[screen];
206 if (top < 0)
207 panicf("Stack underflow... viewportmanager");
209 toggle_theme(screen, force_redraw);
213 static bool is_theme_enabled(enum screen_type screen)
215 int top = theme_stack_top[screen];
216 return theme_stack[screen][top].enabled;
218 #endif /* HAVE_LCD_BITMAP */
220 int viewport_get_nb_lines(const struct viewport *vp)
222 #ifdef HAVE_LCD_BITMAP
223 return vp->height/font_get(vp->font)->height;
224 #else
225 (void)vp;
226 return 2;
227 #endif
230 static void viewportmanager_redraw(void* data)
232 int i;
233 FOR_NB_SCREENS(i)
235 #ifdef HAVE_LCD_BITMAP
236 if (is_theme_enabled(i))
237 sb_skin_update(i, NULL != data);
238 #else
239 (void)data;
240 gui_statusbar_draw(&statusbars.statusbars[i], NULL, NULL);
241 #endif
245 void viewportmanager_init()
247 #ifdef HAVE_LCD_BITMAP
248 int i;
249 FOR_NB_SCREENS(i)
251 theme_stack_top[i] = -1; /* the next call fixes this to 0 */
252 /* We always want the theme enabled by default... */
253 viewportmanager_theme_enable(i, true, NULL);
255 #else
256 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
257 #endif
260 #ifdef HAVE_LCD_BITMAP
261 void viewportmanager_theme_changed(const int which)
263 int i;
264 #ifdef HAVE_BUTTONBAR
265 if (which & THEME_BUTTONBAR)
266 { /* don't handle further, the custom ui viewport ignores the buttonbar,
267 * as does viewport_set_defaults(), since only lists use it*/
268 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
270 #endif
271 if (which & THEME_UI_VIEWPORT)
274 if (which & THEME_LANGUAGE)
277 if (which & THEME_STATUSBAR)
279 FOR_NB_SCREENS(i)
281 /* This can probably be done better...
282 * disable the theme so it's forced to do a full redraw */
283 viewportmanager_theme_enable(i, false, NULL);
284 viewportmanager_theme_undo(i, true);
287 send_event(GUI_EVENT_THEME_CHANGED, NULL);
290 #ifdef HAVE_TOUCHSCREEN
291 /* check if a point (x and y coordinates) are within a viewport */
292 bool viewport_point_within_vp(const struct viewport *vp,
293 const int x, const int y)
295 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
296 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
297 return (is_x && is_y);
299 #endif /* HAVE_TOUCHSCREEN */
301 static void set_default_align_flags(struct viewport *vp)
303 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
304 if (UNLIKELY(lang_is_rtl()))
305 vp->flags |= VP_FLAG_ALIGN_RIGHT;
308 #endif /* HAVE_LCD_BITMAP */
309 #endif /* __PCTOOL__ */
311 #ifdef HAVE_LCD_COLOR
312 #define ARG_STRING(_depth) ((_depth) == 2 ? "dddddgg":"dddddcc")
313 #else
314 #define ARG_STRING(_depth) "dddddgg"
315 #endif
318 void viewport_set_fullscreen(struct viewport *vp,
319 const enum screen_type screen)
321 vp->x = 0;
322 vp->y = 0;
323 vp->width = screens[screen].lcdwidth;
324 vp->height = screens[screen].lcdheight;
326 #ifdef HAVE_LCD_BITMAP
327 #ifndef __PCTOOL__
328 set_default_align_flags(vp);
329 #endif
330 vp->font = FONT_UI + screen; /* default to UI to discourage SYSFONT use */
331 vp->drawmode = DRMODE_SOLID;
332 #if LCD_DEPTH > 1
333 #ifdef HAVE_REMOTE_LCD
334 /* We only need this test if there is a remote LCD */
335 if (screen == SCREEN_MAIN)
336 #endif
338 vp->fg_pattern = FG_FALLBACK;
339 vp->bg_pattern = BG_FALLBACK;
340 #ifdef HAVE_LCD_COLOR
341 vp->lss_pattern = global_settings.lss_color;
342 vp->lse_pattern = global_settings.lse_color;
343 vp->lst_pattern = global_settings.lst_color;
344 #endif
346 #endif
348 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
349 if (screen == SCREEN_REMOTE)
351 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
352 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
354 #endif
355 #endif
358 void viewport_set_defaults(struct viewport *vp,
359 const enum screen_type screen)
361 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
362 struct viewport *sbs_area = NULL;
363 if (!is_theme_enabled(screen))
365 viewport_set_fullscreen(vp, screen);
366 return;
368 sbs_area = sb_skin_get_info_vp(screen);
370 if (sbs_area)
371 *vp = *sbs_area;
372 else
373 #endif /* HAVE_LCD_BITMAP */
374 viewport_set_fullscreen(vp, screen);
378 #ifdef HAVE_LCD_BITMAP
380 int get_viewport_default_colour(enum screen_type screen, bool fgcolour)
382 (void)screen;
383 int colour;
384 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
385 if (fgcolour)
387 #if (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
388 if (screen == SCREEN_REMOTE)
389 colour = REMOTE_FG_FALLBACK;
390 else
391 #endif
392 colour = global_settings.fg_color;
394 else
396 #if (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
397 if (screen == SCREEN_REMOTE)
398 colour = REMOTE_BG_FALLBACK;
399 else
400 #endif
401 colour = global_settings.bg_color;
403 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
404 return colour;
407 const char* viewport_parse_viewport(struct viewport *vp,
408 enum screen_type screen,
409 const char *bufptr,
410 const char separator)
412 /* parse the list to the viewport struct */
413 const char *ptr = bufptr;
414 uint32_t set = 0;
416 enum {
417 PL_X = 0,
418 PL_Y,
419 PL_WIDTH,
420 PL_HEIGHT,
421 PL_FONT,
424 if (!(ptr = parse_list("ddddd", &set, separator, ptr,
425 &vp->x, &vp->y, &vp->width, &vp->height, &vp->font)))
426 return NULL;
428 /* X and Y *must* be set */
429 if (!LIST_VALUE_PARSED(set, PL_X) || !LIST_VALUE_PARSED(set, PL_Y))
430 return NULL;
431 /* check for negative values */
432 if (vp->x < 0)
433 vp->x += screens[screen].lcdwidth;
434 if (vp->y < 0)
435 vp->y += screens[screen].lcdheight;
437 /* fix defaults,
438 * and negative width/height which means "extend to edge minus value */
439 if (!LIST_VALUE_PARSED(set, PL_WIDTH))
440 vp->width = screens[screen].lcdwidth - vp->x;
441 else if (vp->width < 0)
442 vp->width = (vp->width + screens[screen].lcdwidth) - vp->x;
443 if (!LIST_VALUE_PARSED(set, PL_HEIGHT))
444 vp->height = screens[screen].lcdheight - vp->y;
445 else if (vp->height < 0)
446 vp->height = (vp->height + screens[screen].lcdheight) - vp->y;
448 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
449 vp->fg_pattern = get_viewport_default_colour(screen, true);
450 vp->bg_pattern = get_viewport_default_colour(screen, false);
451 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
453 #ifdef HAVE_LCD_COLOR
454 vp->lss_pattern = global_settings.lss_color;
455 vp->lse_pattern = global_settings.lse_color;
456 vp->lst_pattern = global_settings.lst_color;
457 #endif
459 /* Validate the viewport dimensions - we know that the numbers are
460 non-negative integers, ignore bars and assume the viewport takes them
461 * into account */
462 if ((vp->x >= screens[screen].lcdwidth) ||
463 ((vp->x + vp->width) > screens[screen].lcdwidth) ||
464 (vp->y >= screens[screen].lcdheight) ||
465 ((vp->y + vp->height) > screens[screen].lcdheight))
467 return NULL;
470 /* Default to using the user font if the font was an invalid number or '-'
471 * font 1 is *always* the UI font for the current screen
472 * 2 is always the first extra font */
473 if (!LIST_VALUE_PARSED(set, PL_FONT))
474 vp->font = FONT_UI;
476 /* Set the defaults for fields not user-specified */
477 vp->drawmode = DRMODE_SOLID;
478 #ifndef __PCTOOL__
479 set_default_align_flags(vp);
480 #endif
482 return ptr;
484 #endif