benchmark: do not build serial driver in context_switch if CONFIG_USE_HP_TIMER is...
[bertos.git] / bertos / gui / menu.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2006, 2010 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \author Bernie Innocenti <bernie@codewiz.org>
35  * \author Stefano Fedrigo <aleph@develer.com>
36  *
37  * \brief General pourpose menu handling functions
38  */
39
40 #include "menu.h"
41
42 #include "cfg/cfg_gfx.h"
43 #include "cfg/cfg_arch.h"
44 #include <cfg/compiler.h>
45 #include <cfg/debug.h>
46
47 #include <gfx/gfx.h>
48 #include <gfx/font.h>
49 #include <gfx/text.h>
50
51 #include <drv/kbd.h>
52
53 #include <string.h> /* strcpy() */
54
55 #if CPU_HARVARD
56 #include <avr/pgmspace.h> /* strncpy_P() */
57 #endif
58
59 #if CONFIG_MENU_SMOOTH
60 #include <drv/lcd_gfx.h>
61 #endif
62
63 #if (CONFIG_MENU_TIMEOUT != 0)
64 #include <drv/timer.h>
65 #endif
66
67 #if CONFIG_MENU_MENUBAR
68 #include "menubar.h"
69 #endif
70
71 #if defined(CONFIG_LOCALE) && (CONFIG_LOCALE == 1)
72 #include "msg.h"
73 #else
74 #define PTRMSG(x) ((const char *)x)
75 #endif
76
77
78 /* Temporary fake defines for ABORT stuff... */
79 #define abort_top  0
80 #define PUSH_ABORT false
81 #define POP_ABORT  do {} while(0)
82 #define DO_ABORT   do {} while(0)
83
84
85 /**
86  * Return the total number of items in in a menu.
87  */
88 static int menu_count(const struct Menu *menu)
89 {
90         int cnt = 0;
91
92         for (cnt = 0; /*NOP*/; ++cnt)
93         {
94                 const MenuItem *item = &menu->items[cnt];
95 #if CPU_HARVARD
96                 MenuItem ram_item;
97                 if (menu->flags & MF_ROMITEMS)
98                 {
99                         memcpy_P(&ram_item, item, sizeof(ram_item));
100                         item = &ram_item;
101                 }
102 #endif
103                 if (!(item->label || item->hook))
104                         break;
105         }
106
107         return cnt;
108 }
109
110 #if CONFIG_MENU_MENUBAR
111
112 /**
113  * Update the menu bar according to the selected item and redraw it.
114  */
115 static void menu_update_menubar(
116                 const struct Menu *menu,
117                 struct MenuBar *mb,
118                 int selected)
119 {
120         int item_flags;
121 #if CPU_HARVARD
122         if (menu->flags & MF_ROMITEMS)
123         {
124                 ASSERT(sizeof(menu->items[selected].flags) == sizeof(int));
125                 item_flags = pgm_read_int(&menu->items[selected].flags);
126         }
127         else
128 #endif
129                 item_flags = menu->items[selected].flags;
130
131         const_iptr_t newlabel = (const_iptr_t)LABEL_OK;
132
133         if (item_flags & MIF_DISABLED)
134                 newlabel = (const_iptr_t)LABEL_EMPTY;
135         else if (item_flags & MIF_TOGGLE)
136                 newlabel = (const_iptr_t)LABEL_SEL;
137         else if (item_flags & MIF_CHECKIT)
138         {
139                 newlabel = (item_flags & MIF_CHECKED) ?
140                         (const_iptr_t)LABEL_EMPTY : (const_iptr_t)LABEL_SEL;
141         }
142
143         mb->labels[3] = newlabel;
144         mbar_draw(mb);
145 }
146 #endif /* CONFIG_MENU_MENUBAR */
147
148
149 static void menu_defaultRenderHook(struct Bitmap *bm, int ypos, bool selected, const struct MenuItem *item)
150 {
151         if (item->flags & MIF_CHECKIT)
152         {
153                 gfx_rectClear(bm, 0, ypos,
154                                 bm->font->height, ypos + bm->font->height);
155
156                 if (item->flags & MIF_TOGGLE)
157                         gfx_rectDraw(bm, 2, ypos + 2,
158                                         bm->font->height - 2, ypos + bm->font->height - 2);
159                 if (item->flags & MIF_CHECKED)
160                 {
161                         gfx_line(bm,
162                                         3, ypos + 3,
163                                         bm->font->height - 3, ypos + bm->font->height - 3);
164                         gfx_line(bm,
165                                         bm->font->height - 3, ypos + 3,
166                                         3, ypos + bm->font->height - 3);
167                 }
168         }
169
170 #if CPU_HARVARD
171         ((item->flags & MIF_RAMLABEL) ? text_xyprintf : text_xyprintf_P)
172 #else
173         text_xyprintf
174 #endif
175         (
176                 bm, (item->flags & MIF_CHECKIT) ? bm->font->height : 0, ypos,
177                 selected ? (STYLEF_INVERT | TEXT_FILL) : TEXT_FILL,
178                 PTRMSG(item->label)
179         );
180 }
181
182 /**
183  * Show a menu on the display.
184  */
185 static void menu_layout(
186                 const struct Menu *menu,
187                 int first_item,
188                 int selected,
189                 bool redraw)
190 {
191         coord_t ypos;
192         int i;
193         const char * PROGMEM title = PTRMSG(menu->title);
194         Bitmap *bm = menu->bitmap;
195
196         ypos = bm->cr.ymin;
197
198 #if 1
199         if (redraw)
200         {
201                 /* Clear screen */
202                 text_clear(menu->bitmap);
203         }
204 #endif
205
206         if (title)
207         {
208                 if (redraw)
209                         text_xyprintf(bm, 0, ypos, STYLEF_UNDERLINE | STYLEF_BOLD | TEXT_CENTER | TEXT_FILL, title);
210                 ypos += bm->font->height;
211         }
212
213 #if CONFIG_MENU_SMOOTH
214         static coord_t yoffset = 0;
215         static int old_first_item = 0;
216         static int speed;
217         coord_t old_ymin = bm->cr.ymin;
218
219         /* Clip drawing inside menu items area */
220         gfx_setClipRect(bm,
221                 bm->cr.xmin, bm->cr.ymin + ypos,
222                 bm->cr.xmax, bm->cr.ymax);
223
224         if (old_first_item != first_item)
225         {
226                 /* Speed proportional to distance */
227                 speed = ABS(old_first_item - first_item) * 3;
228
229                 if (old_first_item > first_item)
230                 {
231                         yoffset += speed;
232                         if (yoffset > bm->font->height)
233                         {
234                                         yoffset = 0;
235                                         --old_first_item;
236                         }
237                 }
238                 else
239                 {
240                         yoffset -= speed;
241                         if (yoffset < -bm->font->height)
242                         {
243                                         yoffset = 0;
244                                         ++old_first_item;
245                         }
246                 }
247                 first_item = MIN(old_first_item, menu_count(menu));
248
249                 ypos += yoffset;
250                 redraw = true;
251         }
252 #endif /* CONFIG_MENU_SMOOTH */
253
254         if (redraw) for (i = first_item; /**/; ++i)
255         {
256                 const MenuItem *item = &menu->items[i];
257 #if CPU_HARVARD
258                 MenuItem ram_item;
259                 if (menu->flags & MF_ROMITEMS)
260                 {
261                         memcpy_P(&ram_item, item, sizeof(ram_item));
262                         item = &ram_item;
263                 }
264 #endif /* CPU_HARVARD */
265
266                 /* Check for end of room */
267                 if (ypos > bm->cr.ymax)
268                         break;
269
270                 /* Check for end of menu */
271                 if (!(item->label || item->hook))
272                         break;
273
274                 /* Only print visible items */
275                 if (!(item->flags & MIF_HIDDEN))
276                 {
277                         /* Check if a special render function is supplied, otherwise use defaults */
278                         #if (ARCH & ARCH_NIGHTTEST)
279                                 #warning __FILTER_NEXT_WARNING__
280                         #endif
281                         RenderHook renderhook = (item->flags & MIF_RENDERHOOK) ? (RenderHook)item->label : menu_defaultRenderHook;
282
283                         /* Render menuitem */
284                         renderhook(menu->bitmap, ypos++, (i == selected), item);
285
286                         ypos += bm->font->height;
287                 }
288         }
289
290 #if CONFIG_MENU_SMOOTH
291         if (redraw)
292         {
293                 /* Clear rest of area */
294                 gfx_rectClear(bm, bm->cr.xmin, ypos, bm->cr.xmax, bm->cr.ymax);
295
296                 lcd_blitBitmap(bm);
297         }
298
299         /* Restore old cliprect */
300         gfx_setClipRect(bm,
301                         bm->cr.xmin, old_ymin,
302                         bm->cr.xmax, bm->cr.ymax);
303
304 #endif /* CONFIG_MENU_SMOOTH */
305 }
306
307
308 /**
309  * Handle menu item selection
310  */
311 static iptr_t menu_doselect(const struct Menu *menu, struct MenuItem *item)
312 {
313         iptr_t result = 0;
314
315         /* Exclude other items */
316         int mask, i;
317         for (mask = item->flags & MIF_EXCLUDE_MASK, i = 0; mask; mask >>= 1, ++i)
318         {
319                 if (mask & 1)
320                         menu->items[i].flags &= ~MIF_CHECKED;
321         }
322
323         if (item->flags & MIF_DISABLED)
324                 return MENU_DISABLED;
325
326         /* Handle checkable items */
327         if (item->flags & MIF_TOGGLE)
328                 item->flags ^= MIF_CHECKED;
329         else if (item->flags & MIF_CHECKIT)
330                 item->flags |= MIF_CHECKED;
331
332         /* Handle items with callback hooks */
333         if (item->hook)
334         {
335                 /* Push a jmp buffer to abort the operation with the STOP/CANCEL key */
336                 if (!PUSH_ABORT)
337                 {
338                         result = item->hook(item->userdata);
339                         POP_ABORT;
340                 }
341         }
342         else
343                 result = item->userdata;
344
345         return result;
346 }
347
348
349 /**
350  * Return the next visible item (rolls back to the first item)
351  */
352 static int menu_next_visible_item(const struct Menu *menu, int index)
353 {
354         int total = menu_count(menu);
355         int item_flags;
356
357         do
358         {
359                 if (++index >= total)
360                    index = 0;
361
362 #if CPU_HARVARD
363                 if (menu->flags & MF_ROMITEMS)
364                 {
365                         ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
366                         item_flags = pgm_read_int(&menu->items[index].flags);
367                 }
368                 else
369 #endif
370                         item_flags = menu->items[index].flags;
371         }
372         while (item_flags & MIF_HIDDEN);
373
374         return index;
375 }
376
377
378 /**
379  * Return the previous visible item (rolls back to the last item)
380  */
381 static int menu_prev_visible_item(const struct Menu *menu, int index)
382 {
383         int total = menu_count(menu);
384         int item_flags;
385
386         do
387         {
388                 if (--index < 0)
389                         index = total - 1;
390
391 #if CPU_HARVARD
392                 if (menu->flags & MF_ROMITEMS)
393                 {
394                         ASSERT(sizeof(menu->items[index].flags) == sizeof(int));
395                         item_flags = pgm_read_int(&menu->items[index].flags);
396                 }
397                 else
398 #endif
399                         item_flags = menu->items[index].flags;
400         }
401         while (item_flags & MIF_HIDDEN);
402
403         return index;
404 }
405
406
407 /**
408  * Handle a menu and invoke hook functions for the selected menu items.
409  */
410 iptr_t menu_handle(const struct Menu *menu)
411 {
412         uint8_t items_per_page;
413         uint8_t first_item = 0;
414         uint8_t selected;
415         iptr_t result = 0;
416         bool redraw = true;
417
418 #if (CONFIG_MENU_TIMEOUT != 0)
419         ticks_t now, menu_idle_time = timer_clock();
420 #endif
421
422 #if CONFIG_MENU_MENUBAR
423         struct MenuBar mb;
424         const_iptr_t labels[] =
425         {
426                 (const_iptr_t)LABEL_BACK,
427                 (const_iptr_t)LABEL_UPARROW,
428                 (const_iptr_t)LABEL_DOWNARROW,
429                 (const_iptr_t)0
430         };
431
432         /*
433          * Initialize menu bar
434          */
435         if (menu->flags & MF_TOPLEVEL)
436                 labels[0] = (const_iptr_t)LABEL_EMPTY;
437
438         mbar_init(&mb, menu->bitmap, labels, countof(labels));
439 #endif /* CONFIG_MENU_MENUBAR */
440
441
442         items_per_page =
443                 (menu->bitmap->height / menu->bitmap->font->height - 1)
444 #if CONFIG_MENU_MENUBAR
445                 - 1 /* menu bar labels */
446 #endif
447                 - (menu->title ? 1 : 0);
448
449         /* Selected item should be a visible entry */
450         //first_item = selected = menu_next_visible_item(menu, menu->selected - 1);
451         selected = menu->selected;
452         first_item = 0;
453
454         for(;;)
455         {
456                 keymask_t key;
457
458                 /*
459                  * Keep selected item visible
460                  */
461                 while (selected < first_item)
462                         first_item = menu_prev_visible_item(menu, first_item);
463                 while (selected >= first_item + items_per_page)
464                         first_item = menu_next_visible_item(menu, first_item);
465
466                 menu_layout(menu, first_item, selected, redraw);
467                 redraw = false;
468
469                 #if CONFIG_MENU_MENUBAR
470                         menu_update_menubar(menu, &mb, selected);
471                 #endif
472
473                 #if CONFIG_MENU_SMOOTH || (CONFIG_MENU_TIMEOUT != 0)
474                         key = kbd_peek();
475                 #else
476                         key = kbd_get();
477                 #endif
478
479                 #if (CONFIG_MENU_TIMEOUT != 0)
480                         /* Reset idle timer on key press. */
481                         now = timer_clock();
482                         if (key)
483                                 menu_idle_time = now;
484                 #endif
485
486                 if (key & K_OK)
487                 {
488                         struct MenuItem *item = &(menu->items[selected]);
489 #if CPU_HARVARD
490                         MenuItem ram_item;
491                         if (menu->flags & MF_ROMITEMS)
492                         {
493                                 memcpy_P(&ram_item, item, sizeof(ram_item));
494                                 item = &ram_item;
495                         }
496 #endif
497                         result = menu_doselect(menu, item);
498                         redraw = true;
499
500                         /* Return immediately */
501                         if (!(menu->flags & MF_STICKY))
502                                 break;
503
504                         #if (CONFIG_MENU_TIMEOUT != 0)
505                                 /* Chain timeout */
506                                 if ((result == MENU_TIMEOUT) && !(menu->flags & MF_TOPLEVEL))
507                                         break;
508
509                                 /* Reset timeout */
510                                 menu_idle_time = timer_clock();
511                         #endif
512                 }
513                 else if (key & K_UP)
514                 {
515                         selected = menu_prev_visible_item(menu, selected);
516                         redraw = true;
517                 }
518                 else if (key & K_DOWN)
519                 {
520                         selected = menu_next_visible_item(menu, selected);
521                         redraw = true;
522                 }
523                 else if (!(menu->flags & MF_TOPLEVEL))
524                 {
525                         if (key & K_CANCEL)
526                         {
527                                 result = MENU_CANCEL;
528                                 break;
529                         }
530
531                         #if CONFIG_MENU_TIMEOUT != 0
532                                 if (now - menu_idle_time > ms_to_ticks(CONFIG_MENU_TIMEOUT))
533                                 {
534                                         result = MENU_TIMEOUT;
535                                         break;
536                                 }
537                         #endif
538                 }
539         }
540
541         /* Store currently selected item before leaving. */
542         if (menu->flags & MF_SAVESEL)
543                 #if (ARCH & ARCH_NIGHTTEST)
544                         #warning __FILTER_NEXT_WARNING__
545                 #endif
546                 CONST_CAST(struct Menu *, menu)->selected = selected;
547
548         return result;
549 }
550
551
552 /**
553  * Set flags on a menuitem.
554  *
555  * \param menu  Menu owner of the item to change.
556  * \param idx   Index of the menu item.
557  * \param flags Bit mask of the flags to set.
558  *
559  * \return Old flags.
560  */
561 int menu_setFlags(struct Menu *menu, int idx, int flags)
562 {
563         ASSERT(idx < menu_count(menu));
564         ASSERT(!(menu->flags & MF_ROMITEMS));
565
566         int old = menu->items[idx].flags;
567         menu->items[idx].flags |= flags;
568         return old;
569 }
570
571
572 /**
573  * Clear flags on a menuitem.
574  *
575  * \param menu  Menu owner of the item to change.
576  * \param idx   Index of the menu item.
577  * \param flags Bit mask of the flags to clear.
578  *
579  * \return Old flags.
580  */
581 int menu_clearFlags(struct Menu *menu, int idx, int flags)
582 {
583         ASSERT(idx < menu_count(menu));
584         ASSERT(!(menu->flags & MF_ROMITEMS));
585
586         int old = menu->items[idx].flags;
587         menu->items[idx].flags &= ~flags;
588         return old;
589 }