Move menu to gui/.
[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.5  2006/05/15 07:25:29  bernie
18  *#* Move menu to gui/.
19  *#*
20  *#* Revision 1.4  2006/04/27 05:43:07  bernie
21  *#* Fix naming conventions.
22  *#*
23  *#* Revision 1.3  2006/04/11 00:07:32  bernie
24  *#* Implemenent MF_SAVESEL flag.
25  *#*
26  *#* Revision 1.2  2006/03/27 04:49:50  bernie
27  *#* Add bouncing logo demo.
28  *#*
29  *#* Revision 1.1  2006/03/22 09:52:13  bernie
30  *#* Add demo application.
31  *#*
32  *#* Revision 1.1  2006/01/23 23:14:29  bernie
33  *#* Implement simple, but impressive windowing system.
34  *#*
35  *#*/
36
37 #include <emul/emul.h>
38 #include <kern/proc.h>
39 #include <drv/timer.h>
40 #include <drv/buzzer.h>
41 #include <drv/lcd_gfx.h>
42 #include <drv/kbd.h>
43 #include <gfx/gfx.h>
44 #include <gfx/win.h>
45 #include <gfx/text.h>
46 #include <gfx/font.h>
47 #include <gui/menu.h>
48 #include <icons/logo.h>
49 #include <cfg/macros.h>
50
51
52 /* SETTINGS SUBMENU ***************************/
53
54 static struct MenuItem settings_items[] =
55 {
56         { (const_iptr_t)"System",     0, (MenuHook)0,  (iptr_t)0 },
57         { (const_iptr_t)"Mouse",      0, (MenuHook)0,  (iptr_t)0 },
58         { (const_iptr_t)"Keyboard",   0, (MenuHook)0,  (iptr_t)0 },
59         { (const_iptr_t)"Networking", 0, (MenuHook)0,  (iptr_t)0 },
60         { (const_iptr_t)"Date & Time",0, (MenuHook)0,  (iptr_t)0 },
61         { (const_iptr_t)"Power Saving", MIF_TOGGLE, (MenuHook)0, (iptr_t)0 },
62         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
63 };
64 static struct Menu settings_menu = { settings_items, "Settings Menu", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
65
66 /*** DISPLAY MENU ****************************/
67
68 static struct MenuItem display_items[] =
69 {
70         { (const_iptr_t)"Background", 0, (MenuHook)0, (iptr_t)0 },
71         { (const_iptr_t)"Colors",     0, (MenuHook)0, (iptr_t)0 },
72         { (const_iptr_t)"Style",      0, (MenuHook)0, (iptr_t)0 },
73         { (const_iptr_t)"Icon Theme", 0, (MenuHook)0, (iptr_t)0 },
74         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
75 };
76 static struct Menu display_menu = { display_items, "Display Menu", MF_SAVESEL, &lcd_bitmap, 0 };
77
78
79 /*** SETUP MENU ******************************/
80
81 static struct MenuItem setup_items[] =
82 {
83         { (const_iptr_t)"Setup 0", 0, (MenuHook)NULL, (iptr_t)0    },
84         { (const_iptr_t)"Setup 1", 0, (MenuHook)NULL, (iptr_t)0    },
85         { (const_iptr_t)"Setup 2", 0, (MenuHook)NULL, (iptr_t)0    },
86         { (const_iptr_t)"Setup 3", 0, (MenuHook)NULL, (iptr_t)0    },
87         { (const_iptr_t)0, 0, NULL, NULL }
88 };
89 static struct Menu setup_menu = { setup_items, "Setup Menu", MF_STICKY | MF_SAVESEL, &lcd_bitmap, 0 };
90
91
92 /*** MAIN MENU *******************************/
93
94 static struct MenuItem main_items[] =
95 {
96         { (const_iptr_t)"Settings", 0, (MenuHook)menu_handle, (iptr_t)&settings_menu },
97         { (const_iptr_t)"Display", 0, (MenuHook)menu_handle, (iptr_t)&display_menu  },
98         { (const_iptr_t)"Setup",   0, (MenuHook)menu_handle, (iptr_t)&setup_menu    },
99         { (const_iptr_t)0, 0, NULL, (iptr_t)0 }
100 };
101 static struct Menu main_menu = { main_items, "Main Menu", MF_STICKY, &lcd_bitmap, 0 };
102
103 static void magic(struct Bitmap *bitmap, coord_t x, coord_t y)
104 {
105         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 };
106         unsigned int i;
107
108         gfx_moveTo(bitmap, coords[countof(coords)-2]/2 + x, coords[countof(coords)-1]/3 + y);
109         for (i = 0; i < countof(coords); i += 2)
110                 gfx_lineTo(bitmap, coords[i]/2 + x, coords[i+1]/3 + y);
111 }
112
113 Window root_win;
114
115 void schedule(void)
116 {
117 //      win_compose(&root_win);
118         lcd_blitBitmap(root_win.bitmap);
119         emul_idle();
120         usleep(10000);
121 }
122
123 void hello_world(void)
124 {
125         gfx_bitmapClear(&lcd_bitmap);
126         extern const Font font_10x20;
127         extern const Font font_helvB10;
128         extern const Font font_courB08;
129         extern const Font font_courB14;
130         extern const Font font_mono17;
131         extern const Font font_luBS14;
132         extern const Font font_ncenB18;
133         gfx_setFont(&lcd_bitmap, &font_luBS14);
134
135         text_xprintf(&lcd_bitmap, 1, 0, STYLEF_BOLD | TEXT_FILL | TEXT_CENTER, "Hello, world!");
136         for (int i = 0; i < 1000; ++i)
137         {
138                 lcd_blitBitmap(&lcd_bitmap);
139                 emul_idle();
140         }
141 }
142
143 /**
144  * Show the splash screen
145  */
146 void bouncing_logo(void)
147 {
148         const long SPEED_SCALE = 1000;
149         const long GRAVITY_ACCEL = 10;
150         const long BOUNCE_ELASTICITY = 2;
151         long h = (long)(-project_grl_logo.height) * SPEED_SCALE;
152         long speed = 1000;
153
154         /* Repeat until logo stands still on the bottom edge */
155         while (!((speed == 0) && (h == 0)))
156         {
157                 /* Move */
158                 h += speed;
159
160                 /* Gravity acceleration */
161                 speed += GRAVITY_ACCEL;
162
163                 if (h > 0 && speed > 0)
164                 {
165                         /* Bounce */
166                         speed = - (speed / BOUNCE_ELASTICITY);
167
168                 }
169
170                 /* Update graphics */
171                 gfx_bitmapClear(&lcd_bitmap);
172                 gfx_blitImage(&lcd_bitmap,
173                         (lcd_bitmap.width - project_grl_logo.width) / 2,
174                         h / SPEED_SCALE,
175                         &project_grl_logo);
176                 lcd_blitBitmap(&lcd_bitmap);
177
178                 timer_delay(10);
179         }
180 }
181
182 int main(int argc, char *argv[])
183 {
184         emul_init(&argc, argv);
185         timer_init();
186         buz_init();
187         kbd_init();
188         lcd_init();
189         proc_init();
190
191         hello_world();
192         bouncing_logo();
193
194         const coord_t small_left = 45, small_top = 30, small_width = 50, small_height = 30;
195         const coord_t large_left = -10, large_top = 10, large_width = 85, large_height = 41;
196
197         Window small_win, large_win;
198         Bitmap small_bm, large_bm;
199         uint8_t small_raster[RASTER_SIZE(small_width, small_height)];
200         uint8_t large_raster[RASTER_SIZE(large_width, large_height)];
201
202         win_create(&root_win,  &lcd_bitmap);
203
204         gfx_bitmapInit(&large_bm, large_raster, large_width, large_height);
205         win_create(&large_win, &large_bm);
206         win_open(&large_win, &root_win);
207         win_move(&large_win, large_left, large_top);
208
209         gfx_bitmapInit(&small_bm, small_raster, small_width, small_height);
210         win_create(&small_win, &small_bm);
211         win_open(&small_win, &root_win);
212         win_move(&small_win, small_left, small_top);
213
214
215         coord_t x = 0, y = LCD_WIDTH / 2;
216         coord_t xdir = +1, ydir = -1;
217         coord_t xdir_large = +1;
218         coord_t ydir_small = +1;
219         int raise_counter = 0;
220         int i;
221         Bitmap *bm;
222
223         for(;;)
224         {
225                 /* Background animation */
226                 bm = &lcd_bitmap;
227                 gfx_bitmapClear(bm);
228 /*              gfx_setClipRect(bm, 0, 0, bm->width, bm->height);
229                 gfx_rectDraw(bm, 10, 10, bm->width-10, bm->height-10);
230                 gfx_setClipRect(bm, 11, 11, bm->width-11, bm->height-11);
231 */              magic(bm, x, y);
232                 x += xdir;
233                 y += ydir;
234                 if (x >= bm->width)  xdir = -1;
235                 if (x <= -50)        xdir = +1;
236                 if (y >= bm->height) ydir = -1;
237                 if (y <= -50)        ydir = +1;
238
239                 menu_handle(&main_menu);
240
241                 /* Large window animation */
242                 bm = large_win.bitmap;
243                 gfx_bitmapClear(bm);
244                 for (i = 0; i < bm->height / 2; i += 2)
245                         gfx_rectDraw(bm, 0 + i, 0 + i, bm->width - i, bm->height - i);
246
247
248                 /* Small window animation */
249                 bm = small_win.bitmap;
250                 gfx_bitmapClear(bm);
251                 gfx_rectDraw(bm, 0, 0, bm->width, bm->height);
252                 gfx_line(bm, 0, 0, bm->width, bm->height);
253                 gfx_line(bm, bm->width, 0, 0, bm->height);
254
255                 /* Move windows around */
256                 win_move(&large_win, large_win.geom.xmin + xdir_large, large_top);
257                 if (large_win.geom.xmin < -20) xdir_large = +1;
258                 if (large_win.geom.xmin > RECT_WIDTH(&root_win.geom) - 5) xdir_large = -1;
259
260                 win_move(&small_win, small_left, small_win.geom.ymin + ydir_small);
261                 if (small_win.geom.ymin < -20) ydir_small = +1;
262                 if (small_win.geom.ymin > RECT_HEIGHT(&root_win.geom) - 5) ydir_small = -1;
263
264                 ++raise_counter;
265                 if (raise_counter % 997 == 0)
266                         win_raise(&small_win);
267                 else if (raise_counter % 731 == 0)
268                         win_raise(&large_win);
269
270                 win_compose(&root_win);
271                 lcd_blitBitmap(root_win.bitmap);
272                 emul_idle();
273                 usleep(10000);
274         }
275
276         emul_cleanup();
277         return 0;
278 }