Better fix for FS#12337. Use 0 to make the line height calculated from the font heigh...
[maemo-rb.git] / apps / gui / viewport.c
blob33ffed79f38782e52cd637682ed1ff70e94f6ef3
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"
31 #include "list.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 "skin_engine/skin_engine.h"
60 #include "debug.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};
108 FOR_NB_SCREENS(i)
110 enable_event = enable_event || is_theme_enabled(i);
111 sb_set_title_text(NULL, Icon_NOICON, i);
113 toggle_events(enable_event);
115 if (is_theme_enabled(screen))
117 bool first_boot = theme_stack_top[screen] == 0;
118 /* remove the left overs from the previous screen.
119 * could cause a tiny flicker. Redo your screen code if that happens */
120 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
121 skin_backdrop_show(sb_get_backdrop(screen));
122 #endif
123 if (LIKELY(after_boot[screen]) && (!was_enabled[screen] || force))
125 struct viewport deadspace, user;
126 viewport_set_defaults(&user, screen);
127 deadspace = user; /* get colours and everything */
128 /* above */
129 deadspace.x = 0;
130 deadspace.y = 0;
131 deadspace.width = screens[screen].lcdwidth;
132 deadspace.height = user.y;
133 if (deadspace.width && deadspace.height)
135 screens[screen].set_viewport(&deadspace);
136 screens[screen].clear_viewport();
137 screens[screen].update_viewport();
139 /* below */
140 deadspace.y = user.y + user.height;
141 deadspace.height = screens[screen].lcdheight - deadspace.y;
142 if (deadspace.width && deadspace.height)
144 screens[screen].set_viewport(&deadspace);
145 screens[screen].clear_viewport();
146 screens[screen].update_viewport();
148 /* left */
149 deadspace.x = 0;
150 deadspace.y = 0;
151 deadspace.width = user.x;
152 deadspace.height = screens[screen].lcdheight;
153 if (deadspace.width && deadspace.height)
155 screens[screen].set_viewport(&deadspace);
156 screens[screen].clear_viewport();
157 screens[screen].update_viewport();
159 /* below */
160 deadspace.x = user.x + user.width;
161 deadspace.width = screens[screen].lcdwidth - deadspace.x;
162 if (deadspace.width && deadspace.height)
164 screens[screen].set_viewport(&deadspace);
165 screens[screen].clear_viewport();
166 screens[screen].update_viewport();
168 screens[screen].set_viewport(NULL);
170 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();
180 skinlist_set_cfg(screen, NULL);
182 /* let list initialize viewport in case viewport dimensions is changed. */
183 send_event(GUI_EVENT_THEME_CHANGED, NULL);
184 FOR_NB_SCREENS(i)
185 was_enabled[i] = is_theme_enabled(i);
186 #ifdef HAVE_TOUCHSCREEN
187 sb_bypass_touchregions(!is_theme_enabled(SCREEN_MAIN));
188 #endif
189 after_boot[screen] = true;
192 void viewportmanager_theme_enable(enum screen_type screen, bool enable,
193 struct viewport *viewport)
195 int top = ++theme_stack_top[screen];
196 if (top >= VPSTACK_DEPTH-1)
197 panicf("Stack overflow... viewportmanager");
198 theme_stack[screen][top].enabled = enable;
199 theme_stack[screen][top].vp = viewport;
200 toggle_theme(screen, false);
201 /* then be nice and set the viewport up */
202 if (viewport)
203 viewport_set_defaults(viewport, screen);
206 void viewportmanager_theme_undo(enum screen_type screen, bool force_redraw)
208 int top = --theme_stack_top[screen];
209 if (top < 0)
210 panicf("Stack underflow... viewportmanager");
212 toggle_theme(screen, force_redraw);
216 static bool is_theme_enabled(enum screen_type screen)
218 int top = theme_stack_top[screen];
219 return theme_stack[screen][top].enabled;
221 #endif /* HAVE_LCD_BITMAP */
223 int viewport_get_nb_lines(const struct viewport *vp)
225 #ifdef HAVE_LCD_BITMAP
226 if (!vp->line_height)
227 return vp->height/font_get(vp->font)->height;
228 return vp->height/vp->line_height;
229 #else
230 (void)vp;
231 return 2;
232 #endif
235 static void viewportmanager_redraw(void* data)
237 FOR_NB_SCREENS(i)
239 #ifdef HAVE_LCD_BITMAP
240 if (is_theme_enabled(i))
241 sb_skin_update(i, NULL != data);
242 #else
243 (void)data;
244 gui_statusbar_draw(&statusbars.statusbars[i], NULL, NULL);
245 #endif
249 void viewportmanager_init()
251 #ifdef HAVE_LCD_BITMAP
252 FOR_NB_SCREENS(i)
254 theme_stack_top[i] = -1; /* the next call fixes this to 0 */
255 /* We always want the theme enabled by default... */
256 viewportmanager_theme_enable(i, true, NULL);
258 #else
259 add_event(GUI_EVENT_ACTIONUPDATE, false, viewportmanager_redraw);
260 #endif
263 #ifdef HAVE_LCD_BITMAP
264 void viewportmanager_theme_changed(const int which)
266 #ifdef HAVE_BUTTONBAR
267 if (which & THEME_BUTTONBAR)
268 { /* don't handle further, the custom ui viewport ignores the buttonbar,
269 * as does viewport_set_defaults(), since only lists use it*/
270 screens[SCREEN_MAIN].has_buttonbar = global_settings.buttonbar;
272 #endif
273 if (which & THEME_LANGUAGE)
276 if (which & (THEME_STATUSBAR|THEME_UI_VIEWPORT))
278 FOR_NB_SCREENS(i)
280 /* This can probably be done better...
281 * disable the theme so it's forced to do a full redraw */
282 viewportmanager_theme_enable(i, false, NULL);
283 viewportmanager_theme_undo(i, true);
286 send_event(GUI_EVENT_THEME_CHANGED, NULL);
289 #ifdef HAVE_TOUCHSCREEN
290 /* check if a point (x and y coordinates) are within a viewport */
291 bool viewport_point_within_vp(const struct viewport *vp,
292 const int x, const int y)
294 bool is_x = (x >= vp->x && x < (vp->x + vp->width));
295 bool is_y = (y >= vp->y && y < (vp->y + vp->height));
296 return (is_x && is_y);
298 #endif /* HAVE_TOUCHSCREEN */
300 static void set_default_align_flags(struct viewport *vp)
302 vp->flags &= ~VP_FLAG_ALIGNMENT_MASK;
303 if (UNLIKELY(lang_is_rtl()))
304 vp->flags |= VP_FLAG_ALIGN_RIGHT;
307 #endif /* HAVE_LCD_BITMAP */
308 #endif /* __PCTOOL__ */
310 void viewport_set_fullscreen(struct viewport *vp,
311 const enum screen_type screen)
313 vp->x = 0;
314 vp->y = 0;
315 vp->width = screens[screen].lcdwidth;
316 vp->height = screens[screen].lcdheight;
318 #ifdef HAVE_LCD_BITMAP
319 #ifndef __PCTOOL__
320 set_default_align_flags(vp);
321 #endif
322 vp->font = global_status.font_id[screen];
323 vp->line_height = 0; /* calculate from font height */
324 vp->drawmode = DRMODE_SOLID;
325 #if LCD_DEPTH > 1
326 #ifdef HAVE_REMOTE_LCD
327 /* We only need this test if there is a remote LCD */
328 if (screen == SCREEN_MAIN)
329 #endif
331 vp->fg_pattern = FG_FALLBACK;
332 vp->bg_pattern = BG_FALLBACK;
333 #ifdef HAVE_LCD_COLOR
334 vp->lss_pattern = global_settings.lss_color;
335 vp->lse_pattern = global_settings.lse_color;
336 vp->lst_pattern = global_settings.lst_color;
337 #endif
339 #endif
341 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
342 if (screen == SCREEN_REMOTE)
344 vp->fg_pattern = LCD_REMOTE_DEFAULT_FG;
345 vp->bg_pattern = LCD_REMOTE_DEFAULT_BG;
347 #endif
348 #endif
351 void viewport_set_defaults(struct viewport *vp,
352 const enum screen_type screen)
354 #if defined(HAVE_LCD_BITMAP) && !defined(__PCTOOL__)
355 struct viewport *sbs_area = NULL;
356 if (!is_theme_enabled(screen))
358 viewport_set_fullscreen(vp, screen);
359 return;
361 sbs_area = sb_skin_get_info_vp(screen);
363 if (sbs_area)
364 *vp = *sbs_area;
365 else
366 #endif /* HAVE_LCD_BITMAP */
367 viewport_set_fullscreen(vp, screen);
371 #ifdef HAVE_LCD_BITMAP
373 int get_viewport_default_colour(enum screen_type screen, bool fgcolour)
375 (void)screen; (void)fgcolour;
376 #if (LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
377 int colour;
378 if (fgcolour)
380 #if (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
381 if (screen == SCREEN_REMOTE)
382 colour = REMOTE_FG_FALLBACK;
383 else
384 #endif
385 #if defined(HAVE_LCD_COLOR)
386 colour = global_settings.fg_color;
387 #else
388 colour = FG_FALLBACK;
389 #endif
391 else
393 #if (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1)
394 if (screen == SCREEN_REMOTE)
395 colour = REMOTE_BG_FALLBACK;
396 else
397 #endif
398 #if defined(HAVE_LCD_COLOR)
399 colour = global_settings.bg_color;
400 #else
401 colour = BG_FALLBACK;
402 #endif
404 return colour;
405 #else
406 return 0;
407 #endif /* LCD_DEPTH > 1 || LCD_REMOTE_DEPTH > 1 */
410 #endif