jpeg,png: Merge user interface code and plugin entry point of the two plugins (part...
[kugel-rb.git] / apps / gui / quickscreen.c
blob6675407795d8f01384e5b36a2a61edd5509bc7ea
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;
153 /* text alignment */
154 vps[QUICKSCREEN_LEFT].flags &= ~VP_FLAG_ALIGNMENT_MASK; /* left-aligned */
155 vps[QUICKSCREEN_TOP].flags |= VP_FLAG_ALIGN_CENTER; /* centered */
156 vps[QUICKSCREEN_BOTTOM].flags |= VP_FLAG_ALIGN_CENTER; /* centered */
157 vps[QUICKSCREEN_RIGHT].flags &= ~VP_FLAG_ALIGNMENT_MASK;/* right aligned*/
158 vps[QUICKSCREEN_RIGHT].flags |= VP_FLAG_ALIGN_RIGHT;
161 static void gui_quickscreen_draw(const struct gui_quickscreen *qs,
162 struct screen *display,
163 struct viewport *parent,
164 struct viewport vps[QUICKSCREEN_ITEM_COUNT],
165 struct viewport *vp_icons)
167 int i;
168 char buf[MAX_PATH];
169 unsigned const char *title, *value;
170 void *setting;
171 int temp;
172 display->set_viewport(parent);
173 display->clear_viewport();
174 for (i = 0; i < QUICKSCREEN_ITEM_COUNT; i++)
176 struct viewport *vp = &vps[i];
177 if (!qs->items[i])
178 continue;
179 display->set_viewport(vp);
180 display->scroll_stop(vp);
182 title = P2STR(ID2P(qs->items[i]->lang_id));
183 setting = qs->items[i]->setting;
184 temp = option_value_as_int(qs->items[i]);
185 value = option_get_valuestring(qs->items[i],
186 buf, MAX_PATH, temp);
188 if (viewport_get_nb_lines(vp) < 2)
190 char text[MAX_PATH];
191 snprintf(text, MAX_PATH, "%s: %s", title, value);
192 display->puts_scroll(0, 0, text);
194 else
196 display->puts_scroll(0, 0, title);
197 display->puts_scroll(0, 1, value);
199 display->update_viewport();
201 /* draw the icons */
202 display->set_viewport(vp_icons);
204 display->mono_bitmap(bitmap_icons_7x8[Icon_UpArrow],
205 (vp_icons->width/2) - 4, 0, 7, 8);
206 display->mono_bitmap(bitmap_icons_7x8[Icon_FastForward],
207 vp_icons->width - 8,
208 (vp_icons->height/2) - 4, 7, 8);
209 display->mono_bitmap(bitmap_icons_7x8[Icon_FastBackward], 0,
210 (vp_icons->height/2) - 4, 7, 8);
212 display->mono_bitmap(bitmap_icons_7x8[Icon_DownArrow],
213 (vp_icons->width/2) - 4,
214 vp_icons->height - 8, 7, 8);
216 display->set_viewport(parent);
217 display->update_viewport();
218 display->set_viewport(NULL);
221 static void talk_qs_option(const struct settings_list *opt, bool enqueue)
223 if (global_settings.talk_menu) {
224 if (!enqueue)
225 talk_shutup();
226 talk_id(opt->lang_id, true);
227 option_talk_value(opt, option_value_as_int(opt), true);
232 * Does the actions associated to the given button if any
233 * - qs : the quickscreen
234 * - button : the key we are going to analyse
235 * returns : true if the button corresponded to an action, false otherwise
237 static bool gui_quickscreen_do_button(struct gui_quickscreen * qs, int button)
239 int item;
240 bool invert = false;
241 switch(button)
243 case ACTION_QS_TOP:
244 invert = true;
245 item = QUICKSCREEN_TOP;
246 break;
247 case ACTION_QS_LEFT:
248 invert = true;
249 item = QUICKSCREEN_LEFT;
250 break;
252 case ACTION_QS_DOWN:
253 item = QUICKSCREEN_BOTTOM;
254 break;
256 case ACTION_QS_RIGHT:
257 item = QUICKSCREEN_RIGHT;
258 break;
260 default:
261 return false;
263 option_select_next_val(qs->items[item], invert, true);
264 talk_qs_option(qs->items[item], false);
265 return true;
268 #ifdef HAVE_TOUCHSCREEN
269 static int quickscreen_touchscreen_button(const struct viewport
270 vps[QUICKSCREEN_ITEM_COUNT])
272 short x,y;
273 /* only hitting the text counts, everything else is exit */
274 if (action_get_touchscreen_press(&x, &y) != BUTTON_REL)
275 return ACTION_NONE;
276 else if (viewport_point_within_vp(&vps[QUICKSCREEN_TOP], x, y))
277 return ACTION_QS_TOP;
278 else if (viewport_point_within_vp(&vps[QUICKSCREEN_BOTTOM], x, y))
279 return ACTION_QS_DOWN;
280 else if (viewport_point_within_vp(&vps[QUICKSCREEN_LEFT], x, y))
281 return ACTION_QS_LEFT;
282 else if (viewport_point_within_vp(&vps[QUICKSCREEN_RIGHT], x, y))
283 return ACTION_QS_RIGHT;
284 return ACTION_STD_CANCEL;
286 #endif
288 static bool gui_syncquickscreen_run(struct gui_quickscreen * qs, int button_enter)
290 int button, i, j;
291 struct viewport parent[NB_SCREENS];
292 struct viewport vps[NB_SCREENS][QUICKSCREEN_ITEM_COUNT];
293 struct viewport vp_icons[NB_SCREENS];
294 bool changed = false;
295 /* To quit we need either :
296 * - a second press on the button that made us enter
297 * - an action taken while pressing the enter button,
298 * then release the enter button*/
299 bool can_quit = false;
300 FOR_NB_SCREENS(i)
302 screens[i].set_viewport(NULL);
303 screens[i].stop_scroll();
304 viewport_set_defaults(&parent[i], i);
305 quickscreen_fix_viewports(qs, &screens[i], &parent[i], vps[i], &vp_icons[i]);
306 gui_quickscreen_draw(qs, &screens[i], &parent[i], vps[i], &vp_icons[i]);
308 /* Announce current selection on entering this screen. This is all
309 queued up, but can be interrupted as soon as a setting is
310 changed. */
311 cond_talk_ids(VOICE_QUICKSCREEN);
312 talk_qs_option(qs->items[QUICKSCREEN_TOP], true);
313 talk_qs_option(qs->items[QUICKSCREEN_LEFT], true);
314 talk_qs_option(qs->items[QUICKSCREEN_BOTTOM], true);
315 talk_qs_option(qs->items[QUICKSCREEN_RIGHT], true);
316 while (true) {
317 button = get_action(CONTEXT_QUICKSCREEN, HZ/5);
318 #ifdef HAVE_TOUCHSCREEN
319 if (button == ACTION_TOUCHSCREEN)
320 button = quickscreen_touchscreen_button(vps[SCREEN_MAIN]);
321 #endif
322 if (default_event_handler(button) == SYS_USB_CONNECTED)
323 return(true);
324 if (gui_quickscreen_do_button(qs, button))
326 changed = true;
327 can_quit = true;
328 FOR_NB_SCREENS(i)
329 gui_quickscreen_draw(qs, &screens[i], &parent[i],
330 vps[i], &vp_icons[i]);
331 if (qs->callback)
332 qs->callback(qs);
334 else if (button == button_enter)
335 can_quit = true;
337 if ((button == button_enter) && can_quit)
338 break;
340 if (button == ACTION_STD_CANCEL)
341 break;
343 /* Notify that we're exiting this screen */
344 cond_talk_ids_fq(VOICE_OK);
345 FOR_NB_SCREENS(i)
346 { /* stop scrolling before exiting */
347 for (j = 0; j < QUICKSCREEN_ITEM_COUNT; j++)
348 screens[i].scroll_stop(&vps[i][j]);
351 return changed;
354 static const struct settings_list *get_setting(int gs_value,
355 const struct settings_list *defaultval)
357 if (gs_value != -1 && gs_value < nb_settings &&
358 is_setting_quickscreenable(&settings[gs_value]))
359 return &settings[gs_value];
360 return defaultval;
363 bool quick_screen_quick(int button_enter)
365 struct gui_quickscreen qs;
366 bool oldshuffle = global_settings.playlist_shuffle;
367 int oldrepeat = global_settings.repeat_mode;
369 qs.items[QUICKSCREEN_TOP] =
370 get_setting(global_settings.qs_items[QUICKSCREEN_TOP],
371 find_setting(&global_settings.party_mode, NULL));
372 qs.items[QUICKSCREEN_LEFT] =
373 get_setting(global_settings.qs_items[QUICKSCREEN_LEFT],
374 find_setting(&global_settings.playlist_shuffle, NULL));
375 qs.items[QUICKSCREEN_RIGHT] =
376 get_setting(global_settings.qs_items[QUICKSCREEN_RIGHT],
377 find_setting(&global_settings.repeat_mode, NULL));
378 qs.items[QUICKSCREEN_BOTTOM] =
379 get_setting(global_settings.qs_items[QUICKSCREEN_BOTTOM],
380 find_setting(&global_settings.dirfilter, NULL));
382 qs.callback = NULL;
383 if (gui_syncquickscreen_run(&qs, button_enter))
385 settings_save();
386 settings_apply(false);
387 /* make sure repeat/shuffle/any other nasty ones get updated */
388 if ( oldrepeat != global_settings.repeat_mode &&
389 (audio_status() & AUDIO_STATUS_PLAY) )
391 audio_flush_and_reload_tracks();
393 if (oldshuffle != global_settings.playlist_shuffle
394 && audio_status() & AUDIO_STATUS_PLAY)
396 #if CONFIG_CODEC == SWCODEC
397 dsp_set_replaygain();
398 #endif
399 if (global_settings.playlist_shuffle)
400 playlist_randomise(NULL, current_tick, true);
401 else
402 playlist_sort(NULL, true);
405 return(0);
408 #ifdef BUTTON_F3
409 bool quick_screen_f3(int button_enter)
411 struct gui_quickscreen qs;
412 qs.items[QUICKSCREEN_LEFT] =
413 find_setting(&global_settings.scrollbar, NULL);
414 qs.items[QUICKSCREEN_RIGHT] =
415 find_setting(&global_settings.statusbar, NULL);
416 qs.items[QUICKSCREEN_BOTTOM] =
417 find_setting(&global_settings.flip_display, NULL);
418 qs.callback = NULL;
419 if (gui_syncquickscreen_run(&qs, button_enter))
421 settings_save();
422 settings_apply(false);
424 return(0);
426 #endif /* BUTTON_F3 */
428 /* stuff to make the quickscreen configurable */
429 bool is_setting_quickscreenable(const struct settings_list *setting)
431 /* to keep things simple, only settings which have a lang_id set are ok */
432 if (setting->lang_id < 0 || (setting->flags&F_BANFROMQS))
433 return false;
434 switch (setting->flags&F_T_MASK)
436 case F_T_BOOL:
437 return true;
438 case F_T_INT:
439 case F_T_UINT:
440 return (setting->RESERVED != NULL);
441 default:
442 return false;
446 void set_as_qs_item(const struct settings_list *setting,
447 enum quickscreen_item item)
449 int i;
450 for (i = 0; i < nb_settings; i++)
452 if (&settings[i] == setting)
453 break;
456 global_settings.qs_items[item] = i;