Add OF LCD init
[kugel-rb.git] / apps / gui / list.c
blob6b4229545d09a6aa3a32977e4e06b68c3cfd9c0f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Kevin Ferrare
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
21 #include "lcd.h"
22 #include "font.h"
23 #include "button.h"
24 #include "sprintf.h"
25 #include "string.h"
26 #include "settings.h"
27 #include "kernel.h"
28 #include "system.h"
30 #include "action.h"
31 #include "screen_access.h"
32 #include "list.h"
33 #include "scrollbar.h"
34 #include "statusbar.h"
35 #include "textarea.h"
36 #include "lang.h"
37 #include "sound.h"
38 #include "misc.h"
39 #include "talk.h"
40 #include "viewport.h"
41 #include "list.h"
43 #ifdef HAVE_LCD_CHARCELLS
44 #define SCROLL_LIMIT 1
45 #else
46 #define SCROLL_LIMIT (nb_lines<3?1:2)
47 #endif
49 /* The minimum number of pending button events in queue before starting
50 * to limit list drawing interval.
52 #define FRAMEDROP_TRIGGER 6
54 #ifdef HAVE_LCD_BITMAP
55 static int offset_step = 16; /* pixels per screen scroll step */
56 /* should lines scroll out of the screen */
57 static bool offset_out_of_view = false;
58 #endif
59 static int force_list_reinit = false;
61 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
62 int offset);
63 void list_draw(struct screen *display, struct viewport *parent, struct gui_synclist *list);
65 #ifdef HAVE_LCD_BITMAP
66 static struct viewport parent[NB_SCREENS];
67 void list_init_viewports(struct gui_synclist *list)
69 int i;
70 struct viewport *vp;
71 FOR_NB_SCREENS(i)
73 vp = &parent[i];
74 if (!list)
75 viewport_set_defaults(vp, i);
76 else if (list->parent[i] == vp)
78 viewport_set_defaults(vp, i);
79 list->parent[i]->y = global_settings.statusbar?STATUSBAR_HEIGHT:0;
80 list->parent[i]->height = screens[i].height - list->parent[i]->y;
83 #ifdef HAS_BUTTONBAR
84 if (list && (list->parent[0] == &parent[0]) && global_settings.buttonbar)
85 list->parent[0]->height -= BUTTONBAR_HEIGHT;
86 #endif
87 force_list_reinit = false;
89 #else
90 static struct viewport parent[NB_SCREENS] =
92 [SCREEN_MAIN] =
94 .x = 0,
95 .y = 0,
96 .width = LCD_WIDTH,
97 .height = LCD_HEIGHT
100 void list_init_viewports(struct gui_synclist *list)
102 (void)list;
104 #endif
106 #ifdef HAVE_LCD_BITMAP
107 bool list_display_title(struct gui_synclist *list, struct viewport *vp)
109 return list->title != NULL && viewport_get_nb_lines(vp)>2;
111 #else
112 #define list_display_title(l,v) false
113 #endif
116 * Initializes a scrolling list
117 * - gui_list : the list structure to initialize
118 * - callback_get_item_name : pointer to a function that associates a label
119 * to a given item number
120 * - data : extra data passed to the list callback
121 * - scroll_all :
122 * - selected_size :
123 * - parent : the parent viewports to use. NULL means the full screen minus
124 * statusbar if enabled. NOTE: new screens should NOT set this to NULL.
126 void gui_synclist_init(struct gui_synclist * gui_list,
127 list_get_name callback_get_item_name,
128 void * data,
129 bool scroll_all,
130 int selected_size, struct viewport list_parent[NB_SCREENS]
133 int i;
134 gui_list->callback_get_item_icon = NULL;
135 gui_list->callback_get_item_name = callback_get_item_name;
136 gui_list->callback_speak_item = NULL;
137 gui_list->nb_items = 0;
138 gui_list->selected_item = 0;
139 FOR_NB_SCREENS(i)
141 gui_list->start_item[i] = 0;
142 gui_list->last_displayed_start_item[i] = -1 ;
143 #ifdef HAVE_LCD_BITMAP
144 gui_list->offset_position[i] = 0;
145 #endif
146 if (list_parent)
147 gui_list->parent[i] = &list_parent[i];
148 else
150 gui_list->parent[i] = &parent[i];
153 list_init_viewports(gui_list);
154 gui_list->limit_scroll = false;
155 gui_list->data=data;
156 gui_list->scroll_all=scroll_all;
157 gui_list->selected_size=selected_size;
158 gui_list->title = NULL;
159 gui_list->title_width = 0;
160 gui_list->title_icon = Icon_NOICON;
162 gui_list->scheduled_talk_tick = gui_list->last_talked_tick = 0;
163 gui_list->show_selection_marker = true;
164 gui_list->last_displayed_selected_item = -1 ;
166 #ifdef HAVE_LCD_COLOR
167 gui_list->title_color = -1;
168 gui_list->callback_get_item_color = NULL;
169 #endif
170 force_list_reinit = true;
173 /* this toggles the selection bar or cursor */
174 void gui_synclist_hide_selection_marker(struct gui_synclist * lists, bool hide)
176 lists->show_selection_marker = !hide;
180 #ifdef HAVE_LCD_BITMAP
181 int list_title_height(struct gui_synclist *list, struct viewport *vp);
183 int gui_list_get_item_offset(struct gui_synclist * gui_list, int item_width,
184 int text_pos, struct screen * display, struct viewport *vp)
186 int item_offset;
188 if (offset_out_of_view)
190 item_offset = gui_list->offset_position[display->screen_type];
192 else
194 /* if text is smaller then view */
195 if (item_width <= vp->width - text_pos)
197 item_offset = 0;
199 else
201 /* if text got out of view */
202 if (gui_list->offset_position[display->screen_type] >
203 item_width - (vp->width - text_pos))
204 item_offset = item_width - (vp->width - text_pos);
205 else
206 item_offset = gui_list->offset_position[display->screen_type];
210 return item_offset;
212 #endif
214 * Force a full screen update.
217 void gui_synclist_draw(struct gui_synclist *gui_list)
219 int i;
220 static struct gui_synclist *last_list = NULL;
221 static int last_count = -1;
222 #ifdef HAS_BUTTONBAR
223 static bool last_buttonbar = false;
224 #endif
225 if (force_list_reinit ||
226 #ifdef HAS_BUTTONBAR
227 last_buttonbar != screens[SCREEN_MAIN].has_buttonbar ||
228 #endif
229 last_list != gui_list ||
230 gui_list->nb_items != last_count)
232 list_init_viewports(gui_list);
233 force_list_reinit = false;
235 #ifdef HAS_BUTTONBAR
236 last_buttonbar = screens[SCREEN_MAIN].has_buttonbar;
237 #endif
238 last_count = gui_list->nb_items;
239 last_list = gui_list;
240 FOR_NB_SCREENS(i)
242 list_draw(&screens[i], gui_list->parent[i], gui_list);
246 /* sets up the list so the selection is shown correctly on the screen */
247 static void gui_list_put_selection_on_screen(struct gui_synclist * gui_list,
248 enum screen_type screen)
250 int nb_lines;
251 int difference = gui_list->selected_item - gui_list->start_item[screen];
252 struct viewport vp = *gui_list->parent[screen];
253 #ifdef HAVE_LCD_BITMAP
254 if (list_display_title(gui_list, gui_list->parent[screen]))
255 vp.height -= list_title_height(gui_list,gui_list->parent[screen]);
256 #endif
257 nb_lines = viewport_get_nb_lines(&vp);
259 /* edge case,, selected last item */
260 if (gui_list->selected_item == gui_list->nb_items -1)
262 gui_list->start_item[screen] = MAX(0, gui_list->nb_items - nb_lines);
264 /* selected first item */
265 else if (gui_list->selected_item == 0)
267 gui_list->start_item[screen] = 0;
269 else if (difference < SCROLL_LIMIT) /* list moved up */
271 if (global_settings.scroll_paginated)
273 if (gui_list->start_item[screen] > gui_list->selected_item)
274 gui_list->start_item[screen] = (gui_list->selected_item/nb_lines)*nb_lines;
276 else
278 int top_of_screen = gui_list->selected_item - SCROLL_LIMIT;
279 int temp = MIN(top_of_screen, gui_list->nb_items - nb_lines);
280 gui_list->start_item[screen] = MAX(0, temp);
283 else if (difference > nb_lines - SCROLL_LIMIT) /* list moved down */
285 int bottom = gui_list->nb_items - nb_lines;
286 /* always move the screen if selection isnt "visible" */
287 if (gui_list->show_selection_marker == false)
289 if (bottom < 0)
290 bottom = 0;
291 gui_list->start_item[screen] = MIN(bottom, gui_list->start_item[screen] +
292 2*gui_list->selected_size);
294 else if (global_settings.scroll_paginated)
296 if (gui_list->start_item[screen] + nb_lines <= gui_list->selected_item)
297 gui_list->start_item[screen] = MIN(bottom, gui_list->selected_item);
299 else
301 int top_of_screen = gui_list->selected_item + SCROLL_LIMIT - nb_lines;
302 int temp = MAX(0, top_of_screen);
303 gui_list->start_item[screen] = MIN(bottom, temp);
308 * Selects an item in the list
309 * - gui_list : the list structure
310 * - item_number : the number of the item which will be selected
312 void gui_synclist_select_item(struct gui_synclist * gui_list, int item_number)
314 int i;
315 if( item_number > gui_list->nb_items-1 || item_number < 0 )
316 return;
317 gui_list->selected_item = item_number;
318 FOR_NB_SCREENS(i)
319 gui_list_put_selection_on_screen(gui_list, i);
322 static void gui_list_select_at_offset(struct gui_synclist * gui_list,
323 int offset)
325 int new_selection;
326 if (gui_list->selected_size > 1)
328 offset *= gui_list->selected_size;
329 /* always select the first item of multi-line lists */
330 offset -= offset%gui_list->selected_size;
332 new_selection = gui_list->selected_item + offset;
333 if (new_selection >= gui_list->nb_items)
335 gui_list->selected_item = gui_list->limit_scroll ?
336 gui_list->nb_items - gui_list->selected_size : 0;
338 else if (new_selection < 0)
340 gui_list->selected_item = gui_list->limit_scroll ?
341 0 : gui_list->nb_items - gui_list->selected_size;
343 else if (gui_list->show_selection_marker == false)
345 /* NOTE: this part doesnt work as well as it used to, the problem is
346 we want to scroll the lists seperatly but we only have one
347 selected item now, I dont think this is such a big deal though */
348 int i, nb_lines, screen_top;
349 FOR_NB_SCREENS(i)
351 struct viewport vp = *gui_list->parent[i];
352 #ifdef HAVE_LCD_BITMAP
353 if (list_display_title(gui_list, gui_list->parent[i]))
354 vp.height -= list_title_height(gui_list,gui_list->parent[i]);
355 #endif
356 nb_lines = viewport_get_nb_lines(&vp);
357 if (offset > 0)
359 screen_top = gui_list->nb_items-nb_lines;
360 if (screen_top < 0)
361 screen_top = 0;
362 gui_list->start_item[i] = MIN(screen_top, gui_list->start_item[i] +
363 gui_list->selected_size);
365 else
367 gui_list->start_item[i] = MAX(0, gui_list->start_item[i] -
368 gui_list->selected_size);
371 return;
373 else gui_list->selected_item += offset;
374 gui_synclist_select_item(gui_list, gui_list->selected_item);
378 * Adds an item to the list (the callback will be asked for one more item)
379 * - gui_list : the list structure
381 void gui_synclist_add_item(struct gui_synclist * gui_list)
383 gui_list->nb_items++;
384 /* if only one item in the list, select it */
385 if(gui_list->nb_items == 1)
386 gui_list->selected_item = 0;
390 * Removes an item to the list (the callback will be asked for one less item)
391 * - gui_list : the list structure
393 void gui_synclist_del_item(struct gui_synclist * gui_list)
395 if(gui_list->nb_items > 0)
397 if (gui_list->selected_item == gui_list->nb_items-1)
398 gui_list->selected_item--;
399 gui_list->nb_items--;
400 gui_synclist_select_item(gui_list, gui_list->selected_item);
404 #ifdef HAVE_LCD_BITMAP
405 void gui_list_screen_scroll_step(int ofs)
407 offset_step = ofs;
410 void gui_list_screen_scroll_out_of_view(bool enable)
412 if (enable)
413 offset_out_of_view = true;
414 else
415 offset_out_of_view = false;
417 #endif /* HAVE_LCD_BITMAP */
420 * Set the title and title icon of the list. Setting title to NULL disables
421 * both the title and icon. Use NOICON if there is no icon.
423 void gui_synclist_set_title(struct gui_synclist * gui_list,
424 char * title, enum themable_icons icon)
426 gui_list->title = title;
427 gui_list->title_icon = icon;
428 if (title) {
429 #ifdef HAVE_LCD_BITMAP
430 int i;
431 FOR_NB_SCREENS(i)
432 screens[i].getstringsize(title, &gui_list->title_width, NULL);
433 #else
434 gui_list->title_width = strlen(title);
435 #endif
436 } else {
437 gui_list->title_width = 0;
439 force_list_reinit = true;
443 void gui_synclist_set_nb_items(struct gui_synclist * lists, int nb_items)
445 #ifdef HAVE_LCD_BITMAP
446 int i;
447 #endif
448 lists->nb_items = nb_items;
449 #ifdef HAVE_LCD_BITMAP
450 FOR_NB_SCREENS(i)
452 lists->offset_position[i] = 0;
454 #endif
456 int gui_synclist_get_nb_items(struct gui_synclist * lists)
458 return lists->nb_items;
460 int gui_synclist_get_sel_pos(struct gui_synclist * lists)
462 return lists->selected_item;
464 void gui_synclist_set_icon_callback(struct gui_synclist * lists,
465 list_get_icon icon_callback)
467 lists->callback_get_item_icon = icon_callback;
470 void gui_synclist_set_voice_callback(struct gui_synclist * lists,
471 list_speak_item voice_callback)
473 lists->callback_speak_item = voice_callback;
476 #ifdef HAVE_LCD_COLOR
477 void gui_synclist_set_color_callback(struct gui_synclist * lists,
478 list_get_color color_callback)
480 lists->callback_get_item_color = color_callback;
482 #endif
484 static void gui_synclist_select_next_page(struct gui_synclist * lists,
485 enum screen_type screen)
487 gui_list_select_at_offset(lists, screens[screen].nb_lines);
490 static void gui_synclist_select_previous_page(struct gui_synclist * lists,
491 enum screen_type screen)
493 gui_list_select_at_offset(lists, -screens[screen].nb_lines);
496 void gui_synclist_limit_scroll(struct gui_synclist * lists, bool scroll)
498 lists->limit_scroll = scroll;
501 #ifdef HAVE_LCD_BITMAP
503 * Makes all the item in the list scroll by one step to the right.
504 * Should stop increasing the value when reaching the widest item value
505 * in the list.
507 static void gui_synclist_scroll_right(struct gui_synclist * lists)
509 int i;
510 FOR_NB_SCREENS(i)
512 /* FIXME: This is a fake right boundry limiter. there should be some
513 * callback function to find the longest item on the list in pixels,
514 * to stop the list from scrolling past that point */
515 lists->offset_position[i]+=offset_step;
516 if (lists->offset_position[i] > 1000)
517 lists->offset_position[i] = 1000;
522 * Makes all the item in the list scroll by one step to the left.
523 * stops at starting position.
525 static void gui_synclist_scroll_left(struct gui_synclist * lists)
527 int i;
528 FOR_NB_SCREENS(i)
530 lists->offset_position[i]-=offset_step;
531 if (lists->offset_position[i] < 0)
532 lists->offset_position[i] = 0;
535 #endif /* HAVE_LCD_BITMAP */
537 static void _gui_synclist_speak_item(struct gui_synclist *lists, bool repeating)
539 list_speak_item *cb = lists->callback_speak_item;
540 if(cb && gui_synclist_get_nb_items(lists) != 0)
542 int sel = gui_synclist_get_sel_pos(lists);
543 talk_shutup();
544 /* If we got a repeating key action, or we have just very
545 recently started talking, then we want to stay silent for a
546 while until things settle. Likewise if we already had a
547 pending scheduled announcement not yet due: we need to
548 reschedule it. */
549 if(repeating
550 || (lists->scheduled_talk_tick
551 && TIME_BEFORE(current_tick, lists->scheduled_talk_tick))
552 || (lists->last_talked_tick
553 && TIME_BEFORE(current_tick, lists->last_talked_tick +HZ/4)))
555 lists->scheduled_talk_tick = current_tick +HZ/4;
556 return;
557 } else {
558 lists->scheduled_talk_tick = 0; /* work done */
559 cb(sel, lists->data);
560 lists->last_talked_tick = current_tick;
564 void gui_synclist_speak_item(struct gui_synclist * lists)
565 /* The list user should call this to speak the first item on entering
566 the list, and whenever the list is updated. */
568 if(gui_synclist_get_nb_items(lists) == 0 && global_settings.talk_menu)
569 talk_id(VOICE_EMPTY_LIST, true);
570 else _gui_synclist_speak_item(lists, false);
573 extern intptr_t get_action_data(void);
574 #if defined(HAVE_TOUCHPAD)
575 /* this needs to be fixed if we ever get more than 1 touchscreen on a target */
576 unsigned gui_synclist_do_touchpad(struct gui_synclist * gui_list, struct viewport *parent);
577 #endif
579 bool gui_synclist_do_button(struct gui_synclist * lists,
580 unsigned *actionptr, enum list_wrap wrap)
582 int action = *actionptr;
583 #ifdef HAVE_LCD_BITMAP
584 static bool scrolling_left = false;
585 #endif
587 #ifdef HAVE_SCROLLWHEEL
588 int next_item_modifier = button_apply_acceleration(get_action_data());
589 #else
590 static int next_item_modifier = 1;
591 static int last_accel_tick = 0;
593 if (global_settings.list_accel_start_delay)
595 int start_delay = global_settings.list_accel_start_delay * (HZ/2);
596 int accel_wait = global_settings.list_accel_wait * HZ/2;
598 if (get_action_statuscode(NULL)&ACTION_REPEAT)
600 if (!last_accel_tick)
601 last_accel_tick = current_tick + start_delay;
602 else if (current_tick >=
603 last_accel_tick + accel_wait)
605 last_accel_tick = current_tick;
606 next_item_modifier++;
609 else if (last_accel_tick)
611 next_item_modifier = 1;
612 last_accel_tick = 0;
615 #endif
617 #if defined(HAVE_TOUCHPAD)
618 if (action == ACTION_TOUCHPAD)
619 action = *actionptr = gui_synclist_do_touchpad(lists, &parent[SCREEN_MAIN]);
620 #endif
622 switch (wrap)
624 case LIST_WRAP_ON:
625 gui_synclist_limit_scroll(lists, false);
626 break;
627 case LIST_WRAP_OFF:
628 gui_synclist_limit_scroll(lists, true);
629 break;
630 case LIST_WRAP_UNLESS_HELD:
631 if (action == ACTION_STD_PREVREPEAT ||
632 action == ACTION_STD_NEXTREPEAT ||
633 action == ACTION_LISTTREE_PGUP ||
634 action == ACTION_LISTTREE_PGDOWN)
635 gui_synclist_limit_scroll(lists, true);
636 else gui_synclist_limit_scroll(lists, false);
637 break;
640 switch (action)
642 case ACTION_REDRAW:
643 gui_synclist_draw(lists);
644 return true;
646 #ifdef HAVE_VOLUME_IN_LIST
647 case ACTION_LIST_VOLUP:
648 global_settings.volume += 2;
649 /* up two because the falthrough brings it down one */
650 case ACTION_LIST_VOLDOWN:
651 global_settings.volume--;
652 setvol();
653 return true;
654 #endif
655 case ACTION_STD_PREV:
656 case ACTION_STD_PREVREPEAT:
657 gui_list_select_at_offset(lists, -next_item_modifier);
658 #ifndef HAVE_SCROLLWHEEL
659 if (queue_count(&button_queue) < FRAMEDROP_TRIGGER)
660 #endif
661 gui_synclist_draw(lists);
662 _gui_synclist_speak_item(lists,
663 action == ACTION_STD_PREVREPEAT
664 || next_item_modifier >1);
665 yield();
666 *actionptr = ACTION_STD_PREV;
667 return true;
669 case ACTION_STD_NEXT:
670 case ACTION_STD_NEXTREPEAT:
671 gui_list_select_at_offset(lists, next_item_modifier);
672 #ifndef HAVE_SCROLLWHEEL
673 if (queue_count(&button_queue) < FRAMEDROP_TRIGGER)
674 #endif
675 gui_synclist_draw(lists);
676 _gui_synclist_speak_item(lists,
677 action == ACTION_STD_NEXTREPEAT
678 || next_item_modifier >1);
679 yield();
680 *actionptr = ACTION_STD_NEXT;
681 return true;
683 #ifdef HAVE_LCD_BITMAP
684 case ACTION_TREE_PGRIGHT:
685 gui_synclist_scroll_right(lists);
686 gui_synclist_draw(lists);
687 return true;
688 case ACTION_TREE_ROOT_INIT:
689 /* After this button press ACTION_TREE_PGLEFT is allowed
690 to skip to root. ACTION_TREE_ROOT_INIT must be defined in the
691 keymaps as a repeated button press (the same as the repeated
692 ACTION_TREE_PGLEFT) with the pre condition being the non-repeated
693 button press */
694 if (lists->offset_position[0] == 0)
696 scrolling_left = false;
697 *actionptr = ACTION_STD_CANCEL;
698 return true;
700 *actionptr = ACTION_TREE_PGLEFT;
701 case ACTION_TREE_PGLEFT:
702 if(!scrolling_left && (lists->offset_position[0] == 0))
704 *actionptr = ACTION_STD_CANCEL;
705 return false;
707 gui_synclist_scroll_left(lists);
708 gui_synclist_draw(lists);
709 scrolling_left = true; /* stop ACTION_TREE_PAGE_LEFT
710 skipping to root */
711 return true;
712 #endif
713 /* for pgup / pgdown, we are obliged to have a different behaviour depending
714 * on the screen for which the user pressed the key since for example, remote
715 * and main screen doesn't have the same number of lines */
716 case ACTION_LISTTREE_PGUP:
718 int screen =
719 #ifdef HAVE_REMOTE_LCD
720 get_action_statuscode(NULL)&ACTION_REMOTE ?
721 SCREEN_REMOTE :
722 #endif
723 SCREEN_MAIN;
724 gui_synclist_select_previous_page(lists, screen);
725 gui_synclist_draw(lists);
726 _gui_synclist_speak_item(lists, false);
727 yield();
728 *actionptr = ACTION_STD_NEXT;
730 return true;
732 case ACTION_LISTTREE_PGDOWN:
734 int screen =
735 #ifdef HAVE_REMOTE_LCD
736 get_action_statuscode(NULL)&ACTION_REMOTE ?
737 SCREEN_REMOTE :
738 #endif
739 SCREEN_MAIN;
740 gui_synclist_select_next_page(lists, screen);
741 gui_synclist_draw(lists);
742 _gui_synclist_speak_item(lists, false);
743 yield();
744 *actionptr = ACTION_STD_PREV;
746 return true;
748 if(lists->scheduled_talk_tick
749 && TIME_AFTER(current_tick, lists->scheduled_talk_tick))
750 /* scheduled postponed item announcement is due */
751 _gui_synclist_speak_item(lists, false);
752 return false;
755 int list_do_action_timeout(struct gui_synclist *lists, int timeout)
756 /* Returns the lowest of timeout or the delay until a postponed
757 scheduled announcement is due (if any). */
759 if(lists->scheduled_talk_tick)
761 long delay = lists->scheduled_talk_tick -current_tick +1;
762 /* +1 because the trigger condition uses TIME_AFTER(), which
763 is implemented as strictly greater than. */
764 if(delay < 0)
765 delay = 0;
766 if(timeout > delay || timeout == TIMEOUT_BLOCK)
767 timeout = delay;
769 return timeout;
772 bool list_do_action(int context, int timeout,
773 struct gui_synclist *lists, int *action,
774 enum list_wrap wrap)
775 /* Combines the get_action() (with possibly overridden timeout) and
776 gui_synclist_do_button() calls. Returns the list action from
777 do_button, and places the action from get_action in *action. */
779 timeout = list_do_action_timeout(lists, timeout);
780 *action = get_action(context, timeout);
781 return gui_synclist_do_button(lists, action, wrap);
784 /* Simple use list implementation */
785 static int simplelist_line_count = 0;
786 static char simplelist_text[SIMPLELIST_MAX_LINES][SIMPLELIST_MAX_LINELENGTH];
787 /* set the amount of lines shown in the list */
788 void simplelist_set_line_count(int lines)
790 if (lines < 0)
791 simplelist_line_count = 0;
792 else if (lines >= SIMPLELIST_MAX_LINES)
793 simplelist_line_count = SIMPLELIST_MAX_LINES;
794 else
795 simplelist_line_count = lines;
797 /* get the current amount of lines shown */
798 int simplelist_get_line_count(void)
800 return simplelist_line_count;
802 /* add/edit a line in the list.
803 if line_number > number of lines shown it adds the line, else it edits the line */
804 void simplelist_addline(int line_number, const char *fmt, ...)
806 va_list ap;
808 if (line_number > simplelist_line_count)
810 if (simplelist_line_count < SIMPLELIST_MAX_LINES)
811 line_number = simplelist_line_count++;
812 else
813 return;
815 va_start(ap, fmt);
816 vsnprintf(simplelist_text[line_number], SIMPLELIST_MAX_LINELENGTH, fmt, ap);
817 va_end(ap);
820 static char* simplelist_static_getname(int item,
821 void * data,
822 char *buffer,
823 size_t buffer_len)
825 (void)data; (void)buffer; (void)buffer_len;
826 return simplelist_text[item];
829 bool simplelist_show_list(struct simplelist_info *info)
831 struct gui_synclist lists;
832 struct viewport vp[NB_SCREENS];
833 int action, old_line_count = simplelist_line_count,i;
834 char* (*getname)(int item, void * data, char *buffer, size_t buffer_len);
835 if (info->get_name)
836 getname = info->get_name;
837 else
838 getname = simplelist_static_getname;
839 FOR_NB_SCREENS(i)
841 viewport_set_defaults(&vp[i], i);
843 gui_synclist_init(&lists, getname, info->callback_data,
844 info->scroll_all, info->selection_size, vp);
846 if (info->title)
847 gui_synclist_set_title(&lists, info->title, NOICON);
848 if (info->get_icon)
849 gui_synclist_set_icon_callback(&lists, info->get_icon);
850 if (info->get_talk)
851 gui_synclist_set_voice_callback(&lists, info->get_talk);
853 gui_synclist_hide_selection_marker(&lists, info->hide_selection);
855 if (info->action_callback)
856 info->action_callback(ACTION_REDRAW, &lists);
858 if (info->get_name == NULL)
859 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
860 else
861 gui_synclist_set_nb_items(&lists, info->count*info->selection_size);
863 gui_synclist_select_item(&lists, info->start_selection);
865 gui_synclist_draw(&lists);
866 gui_synclist_speak_item(&lists);
868 while(1)
870 gui_syncstatusbar_draw(&statusbars, true);
871 list_do_action(CONTEXT_STD, info->timeout,
872 &lists, &action, LIST_WRAP_UNLESS_HELD);
874 /* We must yield in this case or no other thread can run */
875 if (info->timeout == TIMEOUT_NOBLOCK)
876 yield();
878 if (info->action_callback)
880 action = info->action_callback(action, &lists);
881 if (info->get_name == NULL)
882 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
884 if (action == ACTION_STD_CANCEL)
885 break;
886 else if ((action == ACTION_REDRAW) ||
887 (old_line_count != simplelist_line_count))
889 if (info->get_name == NULL)
890 gui_synclist_set_nb_items(&lists, simplelist_line_count*info->selection_size);
891 gui_synclist_draw(&lists);
892 if (action != ACTION_NONE)
893 gui_synclist_speak_item(&lists);
894 old_line_count = simplelist_line_count;
896 else if(default_event_handler(action) == SYS_USB_CONNECTED)
897 return true;
899 talk_shutup();
900 return false;
903 void simplelist_info_init(struct simplelist_info *info, char* title,
904 int count, void* data)
906 info->title = title;
907 info->count = count;
908 info->selection_size = 1;
909 info->hide_selection = false;
910 info->scroll_all = false;
911 info->timeout = HZ/10;
912 info->start_selection = 0;
913 info->action_callback = NULL;
914 info->get_icon = NULL;
915 info->get_name = NULL;
916 info->get_talk = NULL;
917 info->callback_data = data;