fix %pb and other progressbars which are shown one line down from where they should...
[kugel-rb.git] / apps / gui / option_select.c
blobac8ba4379363c5317c53181730407bfc7749a389
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Kevin Ferrare
11 * Copyright (C) 2007 by Jonathan Gordon
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <stdlib.h>
23 #include "string-extra.h"
24 #include "config.h"
25 #include "option_select.h"
26 #include "kernel.h"
27 #include "lang.h"
28 #include "talk.h"
29 #include "settings_list.h"
30 #include "sound.h"
31 #include "list.h"
32 #include "action.h"
33 #include "misc.h"
34 #include "splash.h"
35 #include "menu.h"
36 #include "quickscreen.h"
38 #if defined (HAVE_SCROLLWHEEL) || \
39 (CONFIG_KEYPAD == PLAYER_PAD)
40 /* Define this if your target makes sense to have
41 smaller values at the top of the list increasing down the list */
42 #define ASCENDING_INT_SETTINGS
43 #endif
45 static int selection_to_val(const struct settings_list *setting, int selection);
46 int option_value_as_int(const struct settings_list *setting)
48 int type = (setting->flags & F_T_MASK);
49 int temp = 0;
50 if (type == F_T_BOOL)
51 temp = *(bool*)setting->setting?1:0;
52 else if (type == F_T_UINT || type == F_T_INT)
53 temp = *(int*)setting->setting;
54 return temp;
56 static const char *unit_strings[] =
58 [UNIT_INT] = "", [UNIT_MS] = "ms",
59 [UNIT_SEC] = "s", [UNIT_MIN] = "min",
60 [UNIT_HOUR]= "hr", [UNIT_KHZ] = "kHz",
61 [UNIT_DB] = "dB", [UNIT_PERCENT] = "%",
62 [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
63 [UNIT_PER_SEC] = "per sec",
64 [UNIT_HERTZ] = "Hz",
65 [UNIT_MB] = "MB", [UNIT_KBIT] = "kb/s",
66 [UNIT_PM_TICK] = "units/10ms",
68 /* these two vars are needed so arbitrary values can be added to the
69 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
70 static int table_setting_oldval = 0, table_setting_array_position = 0;
71 const char *option_get_valuestring(const struct settings_list *setting,
72 char *buffer, int buf_len,
73 intptr_t temp_var)
75 const char* str = buffer;
76 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
78 bool val = (bool)temp_var;
79 strlcpy(buffer, str(val? setting->bool_setting->lang_yes :
80 setting->bool_setting->lang_no), buf_len);
82 #if 0 /* probably dont need this one */
83 else if ((setting->flags & F_FILENAME) == F_FILENAME)
85 struct filename_setting *info = setting->filename_setting;
86 snprintf(buffer, buf_len, "%s%s%s", info->prefix,
87 (char*)temp_var, info->suffix);
89 #endif
90 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
91 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
93 const struct int_setting *int_info = setting->int_setting;
94 const struct table_setting *tbl_info = setting->table_setting;
95 const char *unit;
96 const char* (*formatter)(char*, size_t, int, const char*);
97 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
99 formatter = int_info->formatter;
100 unit = unit_strings[int_info->unit];
102 else
104 formatter = tbl_info->formatter;
105 unit = unit_strings[tbl_info->unit];
107 if (formatter)
108 str = formatter(buffer, buf_len, (int)temp_var, unit);
109 else
110 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit?unit:"");
112 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
114 char sign = ' ';
115 const char *unit = sound_unit(setting->sound_setting->setting);
116 int val = sound_val2phys(setting->sound_setting->setting, (int)temp_var);
117 if (sound_numdecimals(setting->sound_setting->setting))
119 int integer, dec;
120 if(val < 0)
122 sign = '-';
123 val = abs(val);
125 integer = val / 10;
126 dec = val % 10;
127 snprintf(buffer, buf_len, "%c%d.%d %s", sign, integer, dec, unit);
129 else
130 snprintf(buffer, buf_len, "%d %s", val, unit);
132 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
134 if (setting->flags & F_CHOICETALKS)
136 int setting_id;
137 const struct choice_setting *info = setting->choice_setting;
138 if (info->talks[(int)temp_var] < LANG_LAST_INDEX_IN_ARRAY)
140 strlcpy(buffer, str(info->talks[(int)temp_var]), buf_len);
142 else
144 find_setting(setting->setting, &setting_id);
145 cfg_int_to_string(setting_id, (int)temp_var, buffer, buf_len);
148 else
150 int value = (int)temp_var;
151 char *val = P2STR(setting->choice_setting->desc[value]);
152 strlcpy(buffer, val, buf_len);
155 return str;
157 void option_talk_value(const struct settings_list *setting, int value, bool enqueue)
159 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
161 bool val = (value==1);
162 talk_id(val? setting->bool_setting->lang_yes :
163 setting->bool_setting->lang_no, enqueue);
165 #if 0 /* probably dont need this one */
166 else if ((setting->flags & F_FILENAME) == F_FILENAME)
169 #endif
170 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
171 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
173 const struct int_setting *int_info = setting->int_setting;
174 const struct table_setting *tbl_info = setting->table_setting;
175 int unit;
176 int32_t (*get_talk_id)(int, int);
177 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
179 unit = int_info->unit;
180 get_talk_id = int_info->get_talk_id;
182 else
184 unit = tbl_info->unit;
185 get_talk_id = tbl_info->get_talk_id;
187 if (get_talk_id)
188 talk_id(get_talk_id(value, unit), enqueue);
189 else
190 talk_value(value, unit, enqueue);
192 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
194 int talkunit = UNIT_INT;
195 int sound_setting = setting->sound_setting->setting;
196 const char *unit = sound_unit(sound_setting);
197 int decimals = sound_numdecimals(sound_setting);
198 int phys = sound_val2phys(sound_setting, value);
199 if (!strcmp(unit, "dB"))
200 talkunit = UNIT_DB;
201 else if (!strcmp(unit, "%"))
202 talkunit = UNIT_PERCENT;
203 else if (!strcmp(unit, "Hz"))
204 talkunit = UNIT_HERTZ;
205 talk_value_decimal(phys, talkunit, decimals, false);
207 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
209 if (setting->flags & F_CHOICETALKS)
211 talk_id(setting->choice_setting->talks[value], enqueue);
213 else
215 talk_id(P2ID(setting->choice_setting->desc[value]), enqueue);
220 static int option_talk(int selected_item, void * data)
222 struct settings_list *setting = (struct settings_list *)data;
223 int temp_var = selection_to_val(setting, selected_item);
224 option_talk_value(setting, temp_var, false);
225 return 0;
228 #if defined(HAVE_QUICKSCREEN) || defined(HAVE_RECORDING)
229 /* only the quickscreen and recording trigger needs this */
230 void option_select_next_val(const struct settings_list *setting,
231 bool previous, bool apply)
233 int val = 0;
234 int *value = setting->setting;
235 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
237 *(bool*)value = !*(bool*)value;
238 if (apply && setting->bool_setting->option_callback)
239 setting->bool_setting->option_callback(*(bool*)value);
240 return;
242 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
244 struct int_setting *info = (struct int_setting *)setting->int_setting;
245 bool neg_step = (info->step < 0);
246 if (!previous)
248 val = *value + info->step;
249 if (neg_step ? (val < info->max) : (val > info->max))
250 val = info->min;
252 else
254 val = *value - info->step;
255 if (neg_step ? (val > info->min) : (val < info->min))
256 val = info->max;
258 *value = val;
259 if (apply && info->option_callback)
260 info->option_callback(val);
262 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
264 int setting_id = setting->sound_setting->setting;
265 int steps = sound_steps(setting_id);
266 int min = sound_min(setting_id);
267 int max = sound_max(setting_id);
268 if (!previous)
270 val = *value + steps;
271 if (val >= max)
272 val = min;
274 else
276 val = *value - steps;
277 if (val < min)
278 val = max;
280 *value = val;
282 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
284 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
285 val = *value + 1;
286 if (!previous)
288 val = *value + 1;
289 if (val >= info->count)
290 val = 0;
292 else
294 val = *value - 1;
295 if (val < 0)
296 val = info->count-1;
298 *value = val;
299 if (apply && info->option_callback)
300 info->option_callback(val);
302 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
304 const struct table_setting *tbl_info = setting->table_setting;
305 int i, add;
306 add = previous?tbl_info->count-1:1;
307 for (i=0; i<tbl_info->count;i++)
309 if ((*value == tbl_info->values[i]) ||
310 (settings->flags&F_ALLOW_ARBITRARY_VALS &&
311 *value < tbl_info->values[i]))
313 val = tbl_info->values[(i+add)%tbl_info->count];
314 break;
317 *value = val;
320 #endif
322 static int selection_to_val(const struct settings_list *setting, int selection)
324 int min = 0, max = 0, step = 1;
325 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
326 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
327 return selection;
328 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
330 const struct table_setting *info = setting->table_setting;
331 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
332 table_setting_array_position != -1 &&
333 (selection >= table_setting_array_position))
335 if (selection == table_setting_array_position)
336 return table_setting_oldval;
337 return info->values[selection-1];
339 else
340 return info->values[selection];
342 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
344 int setting_id = setting->sound_setting->setting;
345 #ifndef ASCENDING_INT_SETTINGS
346 step = sound_steps(setting_id);
347 max = sound_max(setting_id);
348 min = sound_min(setting_id);
349 #else
350 step = -sound_steps(setting_id);
351 min = sound_max(setting_id);
352 max = sound_min(setting_id);
353 #endif
355 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
357 const struct int_setting *info = setting->int_setting;
358 #ifndef ASCENDING_INT_SETTINGS
359 min = info->min;
360 max = info->max;
361 step = info->step;
362 #else
363 max = info->min;
364 min = info->max;
365 step = -info->step;
366 #endif
368 return max- (selection * step);
371 static const char * value_setting_get_name_cb(int selected_item,
372 void * data,
373 char *buffer,
374 size_t buffer_len)
376 selected_item = selection_to_val(data, selected_item);
377 return option_get_valuestring(data, buffer, buffer_len, selected_item);
380 /* wrapper to convert from int param to bool param in option_screen */
381 static void (*boolfunction)(bool);
382 static void bool_funcwrapper(int value)
384 if (value)
385 boolfunction(true);
386 else
387 boolfunction(false);
390 static void val_to_selection(const struct settings_list *setting, int oldvalue,
391 int *nb_items, int *selected,
392 void (**function)(int))
394 int var_type = setting->flags&F_T_MASK;
395 /* set the number of items and current selection */
396 if (var_type == F_T_INT || var_type == F_T_UINT)
398 if (setting->flags&F_CHOICE_SETTING)
400 *nb_items = setting->choice_setting->count;
401 *selected = oldvalue;
402 *function = setting->choice_setting->option_callback;
404 else if (setting->flags&F_TABLE_SETTING)
406 const struct table_setting *info = setting->table_setting;
407 int i;
408 *nb_items = info->count;
409 *selected = -1;
410 table_setting_array_position = -1;
411 for (i=0;*selected==-1 && i<*nb_items;i++)
413 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
414 (oldvalue < info->values[i]))
416 table_setting_oldval = oldvalue;
417 table_setting_array_position = i;
418 *selected = i;
419 (*nb_items)++;
421 else if (oldvalue == info->values[i])
422 *selected = i;
424 *function = info->option_callback;
426 else if (setting->flags&F_T_SOUND)
428 int setting_id = setting->sound_setting->setting;
429 int steps = sound_steps(setting_id);
430 int min = sound_min(setting_id);
431 int max = sound_max(setting_id);
432 *nb_items = (max-min)/steps + 1;
433 #ifndef ASCENDING_INT_SETTINGS
434 *selected = (max - oldvalue) / steps;
435 #else
436 *selected = (oldvalue - min) / steps;
437 #endif
438 *function = sound_get_fn(setting_id);
440 else
442 const struct int_setting *info = setting->int_setting;
443 int min, max, step;
444 max = info->max;
445 min = info->min;
446 step = info->step;
447 *nb_items = (max-min)/step + 1;
448 #ifndef ASCENDING_INT_SETTINGS
449 *selected = (max - oldvalue) / step;
450 #else
451 *selected = (oldvalue - min) / step;
452 #endif
453 *function = info->option_callback;
456 else if (var_type == F_T_BOOL)
458 *selected = oldvalue;
459 *nb_items = 2;
460 boolfunction = setting->bool_setting->option_callback;
461 if (boolfunction)
462 *function = bool_funcwrapper;
466 bool option_screen(const struct settings_list *setting,
467 struct viewport parent[NB_SCREENS],
468 bool use_temp_var, unsigned char* option_title)
470 int action;
471 bool done = false;
472 struct gui_synclist lists;
473 int oldvalue, nb_items = 0, selected = 0, temp_var;
474 int *variable;
475 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
476 int var_type = setting->flags&F_T_MASK;
477 void (*function)(int) = NULL;
478 char *title;
479 if (var_type == F_T_INT || var_type == F_T_UINT)
481 variable = use_temp_var ? &temp_var: (int*)setting->setting;
482 temp_var = oldvalue = *(int*)setting->setting;
484 else if (var_type == F_T_BOOL)
486 /* bools always use the temp variable...
487 if use_temp_var is false it will be copied to setting->setting every change */
488 variable = &temp_var;
489 temp_var = oldvalue = *(bool*)setting->setting?1:0;
491 else return false; /* only int/bools can go here */
492 gui_synclist_init(&lists, value_setting_get_name_cb,
493 (void*)setting, false, 1, parent);
494 if (setting->lang_id == -1)
495 title = (char*)setting->cfg_vals;
496 else
497 title = P2STR(option_title);
499 gui_synclist_set_title(&lists, title, Icon_Questionmark);
500 gui_synclist_set_icon_callback(&lists, NULL);
501 if(global_settings.talk_menu)
502 gui_synclist_set_voice_callback(&lists, option_talk);
504 val_to_selection(setting, oldvalue, &nb_items, &selected, &function);
505 gui_synclist_set_nb_items(&lists, nb_items);
506 gui_synclist_select_item(&lists, selected);
508 gui_synclist_limit_scroll(&lists, true);
509 gui_synclist_draw(&lists);
510 /* talk the item */
511 gui_synclist_speak_item(&lists);
512 while (!done)
514 if (list_do_action(CONTEXT_LIST, HZ, /* HZ so the status bar redraws */
515 &lists, &action,
516 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
518 /* setting changed */
519 selected = gui_synclist_get_sel_pos(&lists);
520 *variable = selection_to_val(setting, selected);
521 if (var_type == F_T_BOOL && !use_temp_var)
522 *(bool*)setting->setting = (*variable==1);
524 else if (action == ACTION_NONE)
525 continue;
526 else if (action == ACTION_STD_CANCEL)
528 /* setting canceled, restore old value if changed */
529 if (*variable != oldvalue)
531 *variable = oldvalue;
532 if (var_type == F_T_BOOL && !use_temp_var)
533 *(bool*)setting->setting = (oldvalue==1);
534 splash(HZ/2, ID2P(LANG_CANCEL));
536 done = true;
538 else if (action == ACTION_STD_CONTEXT)
540 /* reset setting to default */
541 reset_setting(setting, variable);
542 if (var_type == F_T_BOOL && !use_temp_var)
543 *(bool*)setting->setting = (*variable==1);
544 val_to_selection(setting, *variable, &nb_items,
545 &selected, &function);
546 gui_synclist_select_item(&lists, selected);
547 gui_synclist_draw(&lists);
548 gui_synclist_speak_item(&lists);
550 else if (action == ACTION_STD_OK)
552 /* setting accepted, store now if it used a temp var */
553 if (use_temp_var)
555 if (var_type == F_T_INT || var_type == F_T_UINT)
556 *(int*)setting->setting = *variable;
557 else
558 *(bool*)setting->setting = (*variable==1);
560 settings_save();
561 done = true;
563 else if(default_event_handler(action) == SYS_USB_CONNECTED)
564 return true;
565 /* callback */
566 if ( function )
567 function(*variable);
568 /* if the volume is changing we need to let the skins know */
569 if (function == sound_get_fn(SOUND_VOLUME))
570 global_status.last_volume_change = current_tick;
572 return false;