Convert splashes to viewports for bitmap targets and only draw/update the viewport...
[kugel-rb/myfork.git] / apps / gui / splash.c
blob69691793a88560c8fc7aa60c1e7c7b98ff65cbd9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) Daniel Stenberg (2002)
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 ****************************************************************************/
21 #include "stdarg.h"
22 #include "string.h"
23 #include "rbunicode.h"
24 #include "stdio.h"
25 #include "kernel.h"
26 #include "screen_access.h"
27 #include "lang.h"
28 #include "settings.h"
29 #include "talk.h"
30 #include "splash.h"
31 #include "viewport.h"
33 #ifdef HAVE_LCD_BITMAP
35 #define MAXLINES (LCD_HEIGHT/6)
36 #define MAXBUFFER 512
38 #else /* HAVE_LCD_CHARCELLS */
40 #define MAXLINES 2
41 #define MAXBUFFER 64
43 #endif
45 #define RECT_SPACING 2
47 static void splash_internal(struct screen * screen, const char *fmt, va_list ap)
49 char splash_buf[MAXBUFFER];
50 short widths[MAXLINES];
51 char *lines[MAXLINES];
53 char *next;
54 char *lastbreak = NULL;
55 char *store = NULL;
56 int line = 0;
57 int x = 0;
58 int y, i;
59 int space_w, w, h;
60 #ifdef HAVE_LCD_BITMAP
61 struct viewport vp;
62 int maxw = 0;
64 screen->getstringsize(" ", &space_w, &h);
65 #else /* HAVE_LCD_CHARCELLS */
67 space_w = h = 1;
68 screen->double_height (false);
69 #endif
70 y = h;
72 vsnprintf(splash_buf, sizeof(splash_buf), fmt, ap);
73 va_end(ap);
75 /* break splash string into display lines, doing proper word wrap */
77 next = strtok_r(splash_buf, " ", &store);
78 if (!next)
79 return; /* nothing to display */
81 lines[0] = next;
82 while (true)
84 #ifdef HAVE_LCD_BITMAP
85 screen->getstringsize(next, &w, NULL);
86 #else
87 w = utf8length(next);
88 #endif
89 if (lastbreak)
91 if (x + (next - lastbreak) * space_w + w
92 > screen->lcdwidth - RECT_SPACING*2)
93 { /* too wide, wrap */
94 widths[line] = x;
95 #ifdef HAVE_LCD_BITMAP
96 if (x > maxw)
97 maxw = x;
98 #endif
99 if ((y + h > screen->lcdheight) || (line >= (MAXLINES-1)))
100 break; /* screen full or out of lines */
101 x = 0;
102 y += h;
103 lines[++line] = next;
105 else
107 /* restore & calculate spacing */
108 *lastbreak = ' ';
109 x += (next - lastbreak) * space_w;
112 x += w;
113 lastbreak = next + strlen(next);
114 next = strtok_r(NULL, " ", &store);
115 if (!next)
116 { /* no more words */
117 widths[line] = x;
118 #ifdef HAVE_LCD_BITMAP
119 if (x > maxw)
120 maxw = x;
121 #endif
122 break;
126 /* prepare viewport
127 * First boundaries, then the grey filling, then the black border and finally
128 * the text*/
130 screen->stop_scroll();
132 #ifdef HAVE_LCD_BITMAP
133 viewport_set_defaults(&vp, screen->screen_type);
134 /* If we center the display, then just clear the box we need and put
135 a nice little frame and put the text in there! */
136 vp.y = (screen->lcdheight - y) / 2 - RECT_SPACING; /* height => y start position */
137 vp.x = (screen->lcdwidth - maxw) / 2 - RECT_SPACING;
138 vp.width = maxw + 2*RECT_SPACING;
139 vp.height = screen->lcdheight - (vp.y*2) + RECT_SPACING;
141 if (vp.y < 0)
142 vp.y = 0;
143 if (vp.x < 0)
144 vp.x = 0;
145 if (vp.width > screen->lcdwidth)
146 vp.width = screen->lcdwidth;
147 if (vp.height > screen->lcdheight)
148 vp.height = screen->lcdheight;
150 #if LCD_DEPTH > 1
151 if (screen->depth > 1)
153 vp.drawmode = DRMODE_FG;
154 vp.fg_pattern = SCREEN_COLOR_TO_NATIVE(screen, LCD_LIGHTGRAY);
156 else
157 #endif
158 vp.drawmode = (DRMODE_SOLID|DRMODE_INVERSEVID);
160 screen->set_viewport(&vp);
161 screen->fillrect(0, 0, vp.width, vp.height);
163 #if LCD_DEPTH > 1
164 if (screen->depth > 1)
165 vp.fg_pattern = SCREEN_COLOR_TO_NATIVE(screen, LCD_BLACK);
166 else
167 #endif
168 vp.drawmode = DRMODE_SOLID;
170 screen->drawrect(0, 0, vp.width, vp.height);
172 /* prepare putting the text */
173 y = RECT_SPACING;
174 #else /* HAVE_LCD_CHARCELLS */
175 y = 0; /* vertical centering on 2 lines would be silly */
176 x = 0;
177 screen->clear_display();
178 #endif
180 /* print the message to screen */
181 for (i = 0; i <= line; i++, y+=h)
183 #ifdef HAVE_LCD_BITMAP
184 #define W (vp.width - RECT_SPACING*2)
185 #else
186 #define W (screens->lcdwidth)
187 #endif
188 x = (W - widths[i])/2;
189 if (x < 0)
190 x = 0;
191 #ifdef HAVE_LCD_BITMAP
192 screen->putsxy(x+RECT_SPACING, y, lines[i]);
193 #else
194 screen->puts(x, y, lines[i]);
195 #endif
196 #undef W
198 screen->update_viewport();
199 screen->set_viewport(NULL);
202 void splashf(int ticks, const char *fmt, ...)
204 va_list ap;
205 int i;
207 /* If fmt is a lang ID then get the corresponding string (which
208 still might contain % place holders). */
209 fmt = P2STR((unsigned char *)fmt);
210 FOR_NB_SCREENS(i)
212 va_start(ap, fmt);
213 splash_internal(&(screens[i]), fmt, ap);
214 va_end(ap);
216 if (ticks)
217 sleep(ticks);
220 void splash(int ticks, const char *str)
222 #if !defined(SIMULATOR) || CONFIG_CODEC == SWCODEC
223 long id;
224 /* fmt may be a so called virtual pointer. See settings.h. */
225 if((id = P2ID((const unsigned char*)str)) >= 0)
226 /* If fmt specifies a voicefont ID, and voice menus are
227 enabled, then speak it. */
228 cond_talk_ids_fq(id);
229 #endif
230 splashf(ticks, "%s", P2STR((const unsigned char*)str));