initial custom statusbar commit
[kugel-rb.git] / apps / gui / quickscreen.c
blobc10e688c3eb948e1011821608740a1ce0c2364d3
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2008 by Jonathan Gordon
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 ****************************************************************************/
22 #include <stdio.h>
23 #include "config.h"
24 #include "system.h"
25 #include "icons.h"
26 #include "font.h"
27 #include "kernel.h"
28 #include "misc.h"
29 #include "action.h"
30 #include "settings_list.h"
31 #include "lang.h"
32 #include "playlist.h"
33 #include "dsp.h"
34 #include "viewport.h"
35 #include "audio.h"
36 #include "quickscreen.h"
37 #include "talk.h"
38 #include "list.h"
39 #include "option_select.h"
40 #include "debug.h"
42 /* 1 top, 1 bottom, 2 on either side, 1 for the icons
43 * if enough space, top and bottom have 2 lines */
44 #define MIN_LINES 5
45 #define MAX_NEEDED_LINES 10
46 /* pixels between the 2 center items minimum or between text and icons,
47 * and between text and parent boundaries */
48 #define MARGIN 10
49 #define CENTER_ICONAREA_SIZE (MARGIN+8*2)
51 static void quickscreen_fix_viewports(struct gui_quickscreen *qs,
52 struct screen *display,
53 struct viewport *parent,
54 struct viewport
55 vps[QUICKSCREEN_ITEM_COUNT],
56 struct viewport *vp_icons)
58 int char_height, width, pad = 0;
59 int left_width, right_width, vert_lines;
60 unsigned char *s;
61 int nb_lines = viewport_get_nb_lines(parent);
63 /* nb_lines only returns the number of fully visible lines, small screens
64 or really large fonts could cause problems with the calculation below.
66 if (nb_lines == 0)
67 nb_lines++;
69 char_height = parent->height/nb_lines;
71 /* center the icons VP first */
72 *vp_icons = *parent;
73 vp_icons->width = CENTER_ICONAREA_SIZE; /* abosulte smallest allowed */
74 vp_icons->x = parent->x;
75 vp_icons->x += (parent->width-CENTER_ICONAREA_SIZE)/2;
78 vps[QUICKSCREEN_BOTTOM] = *parent;
79 vps[QUICKSCREEN_TOP] = *parent;
80 /* depending on the space the top/buttom items use 1 or 2 lines */
81 if (nb_lines < MIN_LINES)
82 vert_lines = 1;
83 else
84 vert_lines = 2;
85 vps[QUICKSCREEN_TOP].y = parent->y;
86 vps[QUICKSCREEN_TOP].height = vps[QUICKSCREEN_BOTTOM].height
87 = vert_lines*char_height;
88 vps[QUICKSCREEN_BOTTOM].y
89 = parent->y + parent->height - vps[QUICKSCREEN_BOTTOM].height;
91 /* enough space vertically, so put a nice margin */
92 if (nb_lines >= MAX_NEEDED_LINES)
94 vps[QUICKSCREEN_TOP].y += MARGIN;
95 vps[QUICKSCREEN_BOTTOM].y -= MARGIN;
98 vp_icons->y = vps[QUICKSCREEN_TOP].y
99 + vps[QUICKSCREEN_TOP].height;
100 vp_icons->height = parent->height - vp_icons->y;
101 vp_icons->height -= parent->height - vps[QUICKSCREEN_BOTTOM].y;
103 /* adjust the left/right items widths to fit the screen nicely */
104 s = P2STR(ID2P(qs->items[QUICKSCREEN_LEFT]->lang_id));
105 left_width = display->getstringsize(s, NULL, NULL);
106 s = P2STR(ID2P(qs->items[QUICKSCREEN_RIGHT]->lang_id));
107 right_width = display->getstringsize(s, NULL, NULL);
109 width = MAX(left_width, right_width);
110 if (width*2 + vp_icons->width > parent->width)
111 { /* crop text viewports */
112 width = (parent->width - vp_icons->width)/2;
114 else
115 { /* add more gap in icons vp */
116 int excess = parent->width - vp_icons->width - width*2;
117 if (excess > MARGIN*4)
119 pad = MARGIN;
120 excess -= MARGIN*2;
122 vp_icons->x -= excess/2;
123 vp_icons->width += excess;
126 vps[QUICKSCREEN_LEFT] = *parent;
127 vps[QUICKSCREEN_LEFT].x = parent->x + pad;
128 vps[QUICKSCREEN_LEFT].width = width;
130 vps[QUICKSCREEN_RIGHT] = *parent;
131 vps[QUICKSCREEN_RIGHT].x = parent->x + parent->width - width - pad;
132 vps[QUICKSCREEN_RIGHT].width = width;
134 vps[QUICKSCREEN_LEFT].height = vps[QUICKSCREEN_RIGHT].height
135 = 2*char_height;
137 vps[QUICKSCREEN_LEFT].y = vps[QUICKSCREEN_RIGHT].y
138 = parent->y + (parent->height/2) - char_height;
140 /* shrink the icons vp by a few pixels if there is room so the arrows
141 aren't drawn right next to the text */
142 if (vp_icons->width > CENTER_ICONAREA_SIZE*2)
144 vp_icons->width -= CENTER_ICONAREA_SIZE*2/3;
145 vp_icons->x += CENTER_ICONAREA_SIZE*2/6;
147 if (vp_icons->height > CENTER_ICONAREA_SIZE*2)
149 vp_icons->height -= CENTER_ICONAREA_SIZE*2/3;
150 vp_icons->y += CENTER_ICONAREA_SIZE*2/6;
154 static void quickscreen_draw_text(const char *s, int item, bool title,
155 struct screen *display, struct viewport *vp)
157 int nb_lines = viewport_get_nb_lines(vp);
158 int w, h, line = 0, x = 0;
159 display->getstringsize(s, &w, &h);
161 if (nb_lines > 1 && !title)
162 line = 1;
163 switch (item)
165 case QUICKSCREEN_TOP:
166 case QUICKSCREEN_BOTTOM:
167 x = (vp->width - w)/2;
168 break;
169 case QUICKSCREEN_LEFT:
170 x = 0;
171 break;
172 case QUICKSCREEN_RIGHT:
173 x = vp->width - w;
174 break;
176 if (w>vp->width)
177 display->puts_scroll(0, line, s);
178 else
179 display->putsxy(x, line*h, s);
182 static void gui_quickscreen_draw(const struct gui_quickscreen *qs,
183 struct screen *display,
184 struct viewport *parent,
185 struct viewport vps[QUICKSCREEN_ITEM_COUNT],
186 struct viewport *vp_icons)
188 int i;
189 char buf[MAX_PATH];
190 unsigned const char *title, *value;
191 void *setting;
192 int temp;
193 display->set_viewport(parent);
194 display->clear_viewport();
195 for (i = 0; i < QUICKSCREEN_ITEM_COUNT; i++)
197 if (!qs->items[i])
198 continue;
199 display->set_viewport(&vps[i]);
200 display->scroll_stop(&vps[i]);
202 title = P2STR(ID2P(qs->items[i]->lang_id));
203 setting = qs->items[i]->setting;
204 temp = option_value_as_int(qs->items[i]);
205 value = option_get_valuestring((struct settings_list*)qs->items[i],
206 buf, MAX_PATH, temp);
208 if (vps[i].height < display->getcharheight()*2)
210 char text[MAX_PATH];
211 snprintf(text, MAX_PATH, "%s: %s", title, value);
212 quickscreen_draw_text(text, i, true, display, &vps[i]);
214 else
216 quickscreen_draw_text(title, i, true, display, &vps[i]);
217 quickscreen_draw_text(value, i, false, display, &vps[i]);
219 display->update_viewport();
221 /* draw the icons */
222 display->set_viewport(vp_icons);
224 display->mono_bitmap(bitmap_icons_7x8[Icon_UpArrow],
225 (vp_icons->width/2) - 4, 0, 7, 8);
226 display->mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
227 vp_icons->width - 8,
228 (vp_icons->height/2) - 4, 7, 8);
229 display->mono_bitmap(bitmap_icons_7x8[Icon_FastBackward], 0,
230 (vp_icons->height/2) - 4, 7, 8);
232 display->mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
233 (vp_icons->width/2) - 4,
234 vp_icons->height - 8, 7, 8);
236 display->set_viewport(parent);
237 display->update_viewport();
238 display->set_viewport(NULL);
241 static void talk_qs_option(struct settings_list *opt, bool enqueue)
243 if (global_settings.talk_menu) {
244 if (!enqueue)
245 talk_shutup();
246 talk_id(opt->lang_id, true);
247 option_talk_value(opt, option_value_as_int(opt), true);
252 * Does the actions associated to the given button if any
253 * - qs : the quickscreen
254 * - button : the key we are going to analyse
255 * returns : true if the button corresponded to an action, false otherwise
257 static bool gui_quickscreen_do_button(struct gui_quickscreen * qs, int button)
259 int item;
260 bool invert = false;
261 switch(button)
263 case ACTION_QS_TOP:
264 invert = true;
265 item = QUICKSCREEN_TOP;
266 break;
267 case ACTION_QS_LEFT:
268 invert = true;
269 item = QUICKSCREEN_LEFT;
270 break;
272 case ACTION_QS_DOWN:
273 item = QUICKSCREEN_BOTTOM;
274 break;
276 case ACTION_QS_RIGHT:
277 item = QUICKSCREEN_RIGHT;
278 break;
280 default:
281 return false;
283 option_select_next_val((struct settings_list *)qs->items[item], invert, true);
284 talk_qs_option((struct settings_list *)qs->items[item], false);
285 return true;
288 #ifdef HAVE_TOUCHSCREEN
289 static int quickscreen_touchscreen_button(const struct viewport
290 vps[QUICKSCREEN_ITEM_COUNT])
292 short x,y;
293 /* only hitting the text counts, everything else is exit */
294 if (action_get_touchscreen_press(&x, &y) != BUTTON_REL)
295 return ACTION_NONE;
296 else if (viewport_point_within_vp(&vps[QUICKSCREEN_TOP], x, y))
297 return ACTION_QS_TOP;
298 else if (viewport_point_within_vp(&vps[QUICKSCREEN_BOTTOM], x, y))
299 return ACTION_QS_DOWN;
300 else if (viewport_point_within_vp(&vps[QUICKSCREEN_LEFT], x, y))
301 return ACTION_QS_LEFT;
302 else if (viewport_point_within_vp(&vps[QUICKSCREEN_RIGHT], x, y))
303 return ACTION_QS_RIGHT;
304 return ACTION_STD_CANCEL;
306 #endif
308 static bool gui_syncquickscreen_run(struct gui_quickscreen * qs, int button_enter)
310 int button, i, j;
311 struct viewport parent[NB_SCREENS];
312 struct viewport vps[NB_SCREENS][QUICKSCREEN_ITEM_COUNT];
313 struct viewport vp_icons[NB_SCREENS];
314 bool changed = false;
315 /* To quit we need either :
316 * - a second press on the button that made us enter
317 * - an action taken while pressing the enter button,
318 * then release the enter button*/
319 bool can_quit = false;
320 FOR_NB_SCREENS(i)
322 screens[i].set_viewport(NULL);
323 screens[i].stop_scroll();
324 viewport_set_defaults(&parent[i], i);
325 quickscreen_fix_viewports(qs, &screens[i], &parent[i], vps[i], &vp_icons[i]);
326 gui_quickscreen_draw(qs, &screens[i], &parent[i], vps[i], &vp_icons[i]);
328 /* Announce current selection on entering this screen. This is all
329 queued up, but can be interrupted as soon as a setting is
330 changed. */
331 cond_talk_ids(VOICE_QUICKSCREEN);
332 talk_qs_option((struct settings_list *)qs->items[QUICKSCREEN_TOP], true);
333 talk_qs_option((struct settings_list *)qs->items[QUICKSCREEN_LEFT], true);
334 talk_qs_option((struct settings_list *)qs->items[QUICKSCREEN_BOTTOM], true);
335 talk_qs_option((struct settings_list *)qs->items[QUICKSCREEN_RIGHT], true);
336 while (true) {
337 button = get_action(CONTEXT_QUICKSCREEN, HZ/5);
338 #ifdef HAVE_TOUCHSCREEN
339 if (button == ACTION_TOUCHSCREEN)
340 button = quickscreen_touchscreen_button(vps[SCREEN_MAIN]);
341 #endif
342 if (default_event_handler(button) == SYS_USB_CONNECTED)
343 return(true);
344 if (gui_quickscreen_do_button(qs, button))
346 changed = true;
347 can_quit = true;
348 FOR_NB_SCREENS(i)
349 gui_quickscreen_draw(qs, &screens[i], &parent[i],
350 vps[i], &vp_icons[i]);
351 if (qs->callback)
352 qs->callback(qs);
354 else if (button == button_enter)
355 can_quit = true;
357 if ((button == button_enter) && can_quit)
358 break;
360 if (button == ACTION_STD_CANCEL)
361 break;
363 /* Notify that we're exiting this screen */
364 cond_talk_ids_fq(VOICE_OK);
365 FOR_NB_SCREENS(i)
366 { /* stop scrolling before exiting */
367 for (j = 0; j < QUICKSCREEN_ITEM_COUNT; j++)
368 screens[i].scroll_stop(&vps[i][j]);
371 return changed;
374 static const struct settings_list *get_setting(int gs_value,
375 const struct settings_list *defaultval)
377 if (gs_value != -1 && gs_value < nb_settings &&
378 is_setting_quickscreenable(&settings[gs_value]))
379 return &settings[gs_value];
380 return defaultval;
383 bool quick_screen_quick(int button_enter)
385 struct gui_quickscreen qs;
386 bool oldshuffle = global_settings.playlist_shuffle;
387 int oldrepeat = global_settings.repeat_mode;
389 qs.items[QUICKSCREEN_TOP] =
390 get_setting(global_settings.qs_item_top,
391 find_setting(&global_settings.party_mode, NULL));
392 qs.items[QUICKSCREEN_LEFT] =
393 get_setting(global_settings.qs_item_left,
394 find_setting(&global_settings.playlist_shuffle, NULL));
395 qs.items[QUICKSCREEN_RIGHT] =
396 get_setting(global_settings.qs_item_right,
397 find_setting(&global_settings.repeat_mode, NULL));
398 qs.items[QUICKSCREEN_BOTTOM] =
399 get_setting(global_settings.qs_item_bottom,
400 find_setting(&global_settings.dirfilter, NULL));
402 qs.callback = NULL;
403 if (gui_syncquickscreen_run(&qs, button_enter))
405 settings_save();
406 settings_apply(false);
407 /* make sure repeat/shuffle/any other nasty ones get updated */
408 if ( oldrepeat != global_settings.repeat_mode &&
409 (audio_status() & AUDIO_STATUS_PLAY) )
411 audio_flush_and_reload_tracks();
413 if (oldshuffle != global_settings.playlist_shuffle
414 && audio_status() & AUDIO_STATUS_PLAY)
416 #if CONFIG_CODEC == SWCODEC
417 dsp_set_replaygain();
418 #endif
419 if (global_settings.playlist_shuffle)
420 playlist_randomise(NULL, current_tick, true);
421 else
422 playlist_sort(NULL, true);
425 return(0);
428 #ifdef BUTTON_F3
429 bool quick_screen_f3(int button_enter)
431 struct gui_quickscreen qs;
432 qs.items[QUICKSCREEN_LEFT] =
433 find_setting(&global_settings.scrollbar, NULL);
434 qs.items[QUICKSCREEN_RIGHT] =
435 find_setting(&global_settings.statusbar, NULL);
436 qs.items[QUICKSCREEN_BOTTOM] =
437 find_setting(&global_settings.flip_display, NULL);
438 qs.callback = NULL;
439 if (gui_syncquickscreen_run(&qs, button_enter))
441 settings_save();
442 settings_apply(false);
444 return(0);
446 #endif /* BUTTON_F3 */
448 /* stuff to make the quickscreen configurable */
449 bool is_setting_quickscreenable(const struct settings_list *setting)
451 /* to keep things simple, only settings which have a lang_id set are ok */
452 if (setting->lang_id < 0 || (setting->flags&F_BANFROMQS))
453 return false;
454 switch (setting->flags&F_T_MASK)
456 case F_T_BOOL:
457 return true;
458 case F_T_INT:
459 case F_T_UINT:
460 return (setting->RESERVED != NULL);
461 default:
462 return false;
466 void set_as_qs_item(const struct settings_list *setting,
467 enum quickscreen_item item)
469 int i;
470 for (i = 0; i < nb_settings; i++)
472 if (&settings[i] == setting)
473 break;
475 switch (item)
477 case QUICKSCREEN_TOP:
478 global_settings.qs_item_top = i;
479 break;
480 case QUICKSCREEN_LEFT:
481 global_settings.qs_item_left = i;
482 break;
483 case QUICKSCREEN_RIGHT:
484 global_settings.qs_item_right = i;
485 break;
486 case QUICKSCREEN_BOTTOM:
487 global_settings.qs_item_bottom = i;
488 break;
489 default: /* shut the compiler up */
490 break;