5ac10d81583d9e8997f10af311f98931984772aa
[bertos.git] / app / demo / demo.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Windowing system test.
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.10  2007/09/29 16:30:37  bernie
18  *#* RASTER_SIZE(): Remove obsolete macro.
19  *#*
20  *#* Revision 1.9  2006/09/20 14:29:34  marco
21  *#* Add proc demo (not yet working).
22  *#*
23  *#* Revision 1.8  2006/06/02 12:27:36  bernie
24  *#* Tweak apparence; enable assertions.
25  *#*
26  *#* Revision 1.7  2006/05/27 22:31:18  bernie
27  *#* Clean it up a bit more.
28  *#*
29  *#* Revision 1.6  2006/05/27 17:16:38  bernie
30  *#* Make demos a bit more interesting.
31  *#*
32  *#* Revision 1.5  2006/05/15 07:25:29  bernie
33  *#* Move menu to gui/.
34  *#*
35  *#* Revision 1.4  2006/04/27 05:43:07  bernie
36  *#* Fix naming conventions.
37  *#*
38  *#* Revision 1.3  2006/04/11 00:07:32  bernie
39  *#* Implemenent MF_SAVESEL flag.
40  *#*
41  *#* Revision 1.2  2006/03/27 04:49:50  bernie
42  *#* Add bouncing logo demo.
43  *#*
44  *#* Revision 1.1  2006/03/22 09:52:13  bernie
45  *#* Add demo application.
46  *#*
47  *#* Revision 1.1  2006/01/23 23:14:29  bernie
48  *#* Implement simple, but impressive windowing system.
49  *#*
50  *#*/
51
52 #include <emul/emul.h>
53 #include <kern/proc.h>
54 #include <drv/timer.h>
55 #include <drv/buzzer.h>
56 #include <drv/lcd_gfx.h>
57 #include <drv/kbd.h>
58 #include <gfx/gfx.h>
59 #include <gfx/win.h>
60 #include <gfx/text.h>
61 #include <gfx/font.h>
62 #include <gui/menu.h>
63 #include <icons/logo.h>
64 #include <cfg/macros.h>
65
66 /**
67  * Draw a pentacle in the provided bitmap.
68  */
69 void schedule(void)
70 {
71         lcd_blitBitmap(&lcd_bitmap);
72         emul_idle();
73 }
74
75 /**
76  * Draw a pentacle in the provided bitmap.
77  */
78 static void magic(struct Bitmap *bitmap, coord_t x, coord_t y)
79 {
80         static const coord_t coords[] = { 120, 34, 90, 90, 30, 90, 0, 34, 60, 0, 90, 90, 0, 34, 120, 34, 30, 90, 60, 0 };
81         unsigned int i;
82
83         gfx_moveTo(bitmap, coords[countof(coords)-2]/2 + x, coords[countof(coords)-1]/3 + y);
84         for (i = 0; i < countof(coords); i += 2)
85                 gfx_lineTo(bitmap, coords[i]/2 + x, coords[i+1]/3 + y);
86 }
87
88 static void hello_world(Bitmap *bm)
89 {
90         extern const Font font_ncenB18;
91     const Font *old_font = bm->font;
92
93         gfx_bitmapClear(bm);
94
95         /* Set big font */
96         gfx_setFont(bm, &font_ncenB18);
97
98         text_xprintf(bm, 1, 0, STYLEF_BOLD | TEXT_FILL | TEXT_CENTER,
99                         "Hello, world!");
100
101         lcd_blitBitmap(bm);
102         timer_delay(1000);
103
104         /* Restore old font */
105     gfx_setFont(bm, old_font);
106 }
107
108 /**
109  * Show the splash screen
110  */
111 static void bouncing_logo(Bitmap *bm)
112 {
113         const long SPEED_SCALE = 1000;
114         const long GRAVITY_ACCEL = 10;
115         const long BOUNCE_ELASTICITY = 2;
116         long h = (long)(-project_grl_logo.height) * SPEED_SCALE;
117         long speed = 1000;
118
119         /* Repeat until logo stands still on the bottom edge */
120         while (!((speed == 0) && (h == 0)))
121         {
122                 /* Move */
123                 h += speed;
124
125                 /* Gravity acceleration */
126                 speed += GRAVITY_ACCEL;
127
128                 if (h > 0 && speed > 0)
129                 {
130                         /* Bounce */
131                         speed = - (speed / BOUNCE_ELASTICITY);
132
133                 }
134
135                 /* Update graphics */
136                 gfx_bitmapClear(bm);
137                 gfx_blitImage(bm,
138                         (bm->width - project_grl_logo.width) / 2,
139                         h / SPEED_SCALE,
140                         &project_grl_logo);
141                 lcd_blitBitmap(bm);
142
143                 timer_delay(10);
144         }
145 }
146
147 void win_demo(Bitmap *bm)
148 {
149         const coord_t small_left = 45, small_top = 30, small_width = 50, small_height = 30;
150         const coord_t large_left = -10, large_top = 10, large_width = 85, large_height = 41;
151
152         Window root_win, small_win, large_win;
153         Bitmap small_bm, large_bm;
154         uint8_t small_raster[RAST_SIZE(small_width, small_height)];
155         uint8_t large_raster[RAST_SIZE(large_width, large_height)];
156
157         win_create(&root_win, bm);
158
159         gfx_bitmapInit(&large_bm, large_raster, large_width, large_height);
160         win_create(&large_win, &large_bm);
161         win_open(&large_win, &root_win);
162         win_move(&large_win, large_left, large_top);
163
164         gfx_bitmapInit(&small_bm, small_raster, small_width, small_height);
165         win_create(&small_win, &small_bm);
166         win_open(&small_win, &root_win);
167         win_move(&small_win, small_left, small_top);
168
169
170         coord_t x = 0, y = LCD_WIDTH / 2;
171         coord_t xdir = +1, ydir = -1;
172         coord_t xdir_large = +1;
173         coord_t ydir_small = +1;
174         int raise_counter = 0;
175         int i;
176
177         for(;;)
178         {
179                 /* Background animation */
180                 bm = root_win.bitmap;
181                 gfx_bitmapClear(bm);
182 //              gfx_setClipRect(bm, 0, 0, bm->width, bm->height);
183 //              gfx_rectDraw(bm, 10, 10, bm->width-10, bm->height-10);
184 //              gfx_setClipRect(bm, 11, 11, bm->width-11, bm->height-11);
185                 magic(bm, x, y);
186                 x += xdir;
187                 y += ydir;
188                 if (x >= bm->width)  xdir = -1;
189                 if (x <= -50)        xdir = +1;
190                 if (y >= bm->height) ydir = -1;
191                 if (y <= -50)        ydir = +1;
192
193                 /* Large window animation */
194                 bm = large_win.bitmap;
195                 gfx_bitmapClear(bm);
196                 for (i = 0; i < bm->height / 2; i += 2)
197                         gfx_rectDraw(bm, 0 + i, 0 + i, bm->width - i, bm->height - i);
198
199
200                 /* Small window animation */
201                 bm = small_win.bitmap;
202                 gfx_bitmapClear(bm);
203                 gfx_rectDraw(bm, 0, 0, bm->width, bm->height);
204                 gfx_line(bm, 0, 0, bm->width, bm->height);
205                 gfx_line(bm, bm->width, 0, 0, bm->height);
206
207                 /* Move windows around */
208                 win_move(&large_win, large_win.geom.xmin + xdir_large, large_top);
209                 if (large_win.geom.xmin < -20) xdir_large = +1;
210                 if (large_win.geom.xmin > RECT_WIDTH(&root_win.geom) - 5) xdir_large = -1;
211
212                 win_move(&small_win, small_left, small_win.geom.ymin + ydir_small);
213                 if (small_win.geom.ymin < -20) ydir_small = +1;
214                 if (small_win.geom.ymin > RECT_HEIGHT(&root_win.geom) - 5) ydir_small = -1;
215
216                 ++raise_counter;
217                 if (raise_counter % 997 == 0)
218                         win_raise(&small_win);
219                 else if (raise_counter % 731 == 0)
220                         win_raise(&large_win);
221
222                 win_compose(&root_win);
223
224                 /* Also does LCD refresh, etc. */
225                 if (kbd_peek())
226                         break;
227         }
228 }
229
230 void proc_demo(void)
231 {
232         extern void proc_test(void);
233 // FIXME: proc_test() cause segmentation fault. 
234         proc_test();
235 }
236
237
238 /* SETTINGS SUBMENU */
239
240 static struct MenuItem settings_items[] =
241 {
242         { (const_iptr_t)"System",     0, (MenuHook)0,  (iptr_t)0 },
243         { (const_iptr_t)"Language",   0, (MenuHook)0,  (iptr_t)0 },
244         { (const_iptr_t)"Networking", 0, (MenuHook)0,  (iptr_t)0 },
245         { (const_iptr_t)"Date & Time",0, (MenuHook)0,  (iptr_t)0 },
246         { (const_iptr_t)"Power Saving", MIF_TOGGLE, (MenuHook)0, (iptr_t)0 },
247         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
248 };
249 static struct Menu settings_menu = { settings_items, "Settings Menu", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
250
251
252 /* MX SUBMENU */
253
254 static struct MenuItem mx_items[] =
255 {
256         { (const_iptr_t)"Mouse",                  MIF_CHECKIT | MIF_EXCLUDE_1 | MIF_EXCLUDE_2, (MenuHook)0,  (iptr_t)0 },
257         { (const_iptr_t)"Keyboard", MIF_CHECKED | MIF_CHECKIT | MIF_EXCLUDE_0 | MIF_EXCLUDE_2, (MenuHook)0,  (iptr_t)0 },
258         { (const_iptr_t)"Joystick", MIF_CHECKIT | MIF_EXCLUDE_0 | MIF_EXCLUDE_1, (MenuHook)0,  (iptr_t)0 },
259         { (const_iptr_t)"Autosave", MIF_CHECKED | MIF_CHECKIT | MIF_TOGGLE, (MenuHook)0,  (iptr_t)0 },
260         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
261 };
262
263 static struct Menu mx_menu = { mx_items, (const_iptr_t)0, MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
264
265
266 /* DISPLAY SUBMENU */
267
268 static struct MenuItem display_items[] =
269 {
270         { (const_iptr_t)"Background", 0, (MenuHook)0, (iptr_t)0 },
271         { (const_iptr_t)"Colors",     0, (MenuHook)0, (iptr_t)0 },
272         { (const_iptr_t)"Style",      0, (MenuHook)0, (iptr_t)0 },
273         { (const_iptr_t)"Icon Theme", 0, (MenuHook)0, (iptr_t)0 },
274         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
275 };
276 static struct Menu display_menu = { display_items, "Display Menu", MF_SAVESEL, &lcd_bitmap, 0 };
277
278
279 /* MAIN MENU */
280
281 static struct MenuItem main_items[] =
282 {
283         { (const_iptr_t)"Win Fly",     0, (MenuHook)win_demo,     (iptr_t)&lcd_bitmap    },
284         { (const_iptr_t)"Bounce!",     0, (MenuHook)bouncing_logo,(iptr_t)&lcd_bitmap    },
285         { (const_iptr_t)"Hello World", 0, (MenuHook)hello_world,  (iptr_t)&lcd_bitmap    },
286         { (const_iptr_t)"Scheduling",  0, (MenuHook)proc_demo,    (iptr_t)&lcd_bitmap    },
287         { (const_iptr_t)"Menu MX",     0, (MenuHook)menu_handle,  (iptr_t)&mx_menu       },
288         { (const_iptr_t)"Display",     0, (MenuHook)menu_handle,  (iptr_t)&display_menu  },
289         { (const_iptr_t)"Settings",    0, (MenuHook)menu_handle,  (iptr_t)&settings_menu },
290         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
291 };
292 static struct Menu main_menu = { main_items, "Main Menu", MF_STICKY, &lcd_bitmap, 0 };
293
294
295 int main(int argc, char *argv[])
296 {
297         emul_init(&argc, argv);
298         timer_init();
299         buz_init();
300         kbd_init();
301         lcd_init();
302         proc_init();
303
304         menu_handle(&main_menu);
305
306         emul_cleanup();
307         return 0;
308 }