quiet the masses...
[Rockbox.git] / apps / gui / option_select.c
blob5dec9f721639a2cec9aa51e239c032d6618036ee
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 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 #include <stdlib.h>
21 #include <string.h>
22 #include "config.h"
23 #include "option_select.h"
24 #include "sprintf.h"
25 #include "kernel.h"
26 #include "lang.h"
27 #include "talk.h"
28 #include "settings_list.h"
29 #include "sound.h"
30 #include "list.h"
31 #include "action.h"
32 #include "statusbar.h"
33 #include "misc.h"
34 #include "splash.h"
36 #if defined (HAVE_SCROLLWHEEL) || \
37 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
38 (CONFIG_KEYPAD == IPOD_4G_PAD) || \
39 (CONFIG_KEYPAD == IPOD_1G2G_PAD) || \
40 (CONFIG_KEYPAD == PLAYER_PAD)
41 /* Define this if your target makes sense to have
42 smaller values at the top of the list increasing down the list */
43 #define ASCENDING_INT_SETTINGS
44 #endif
46 static int selection_to_val(struct settings_list *setting, int selection);
48 static const char *unit_strings[] =
50 [UNIT_INT] = "", [UNIT_MS] = "ms",
51 [UNIT_SEC] = "s", [UNIT_MIN] = "min",
52 [UNIT_HOUR]= "hr", [UNIT_KHZ] = "kHz",
53 [UNIT_DB] = "dB", [UNIT_PERCENT] = "%",
54 [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
55 [UNIT_PER_SEC] = "per sec",
56 [UNIT_HERTZ] = "Hz",
57 [UNIT_MB] = "MB", [UNIT_KBIT] = "kb/s",
58 [UNIT_PM_TICK] = "units/10ms",
60 /* these two vars are needed so arbitrary values can be added to the
61 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
62 static int table_setting_oldval = 0, table_setting_array_position = 0;
63 char *option_get_valuestring(struct settings_list *setting,
64 char *buffer, int buf_len,
65 intptr_t temp_var)
67 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
69 bool val = (bool)temp_var;
70 snprintf(buffer, buf_len, "%s",
71 str(val? setting->bool_setting->lang_yes :
72 setting->bool_setting->lang_no));
74 #if 0 /* probably dont need this one */
75 else if ((setting->flags & F_FILENAME) == F_FILENAME)
77 struct filename_setting *info = setting->filename_setting;
78 snprintf(buffer, buf_len, "%s%s%s", info->prefix,
79 (char*)temp_var, info->suffix);
81 #endif
82 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
83 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
85 const struct int_setting *int_info = setting->int_setting;
86 const struct table_setting *tbl_info = setting->table_setting;
87 const char *unit;
88 void (*formatter)(char*, size_t, int, const char*);
89 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
91 formatter = int_info->formatter;
92 unit = unit_strings[int_info->unit];
94 else
96 formatter = tbl_info->formatter;
97 unit = unit_strings[tbl_info->unit];
99 if (formatter)
100 formatter(buffer, buf_len, (int)temp_var, unit);
101 else
102 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit?unit:"");
104 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
106 char sign = ' ', *unit;
107 unit = (char*)sound_unit(setting->sound_setting->setting);
108 if (sound_numdecimals(setting->sound_setting->setting))
110 int integer, dec;
111 int val = sound_val2phys(setting->sound_setting->setting,
112 (int)temp_var);
113 if(val < 0)
115 sign = '-';
116 val = abs(val);
118 integer = val / 10; dec = val % 10;
119 snprintf(buffer, buf_len, "%c%d.%d %s", sign, integer, dec, unit);
121 else
122 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit);
124 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
126 if (setting->flags & F_CHOICETALKS)
128 int setting_id;
129 const struct choice_setting *info = setting->choice_setting;
130 if (info->talks[(int)temp_var] < LANG_LAST_INDEX_IN_ARRAY)
132 snprintf(buffer, buf_len, "%s", str(info->talks[(int)temp_var]));
134 else
136 find_setting(setting->setting, &setting_id);
137 cfg_int_to_string(setting_id, (int)temp_var, buffer, buf_len);
140 else
142 int value= (int)temp_var;
143 char *val = P2STR(setting->choice_setting->desc[value]);
144 snprintf(buffer, buf_len, "%s", val);
147 return buffer;
150 static int option_talk(int selected_item, void * data)
152 struct settings_list *setting = (struct settings_list *)data;
153 int temp_var = selection_to_val(setting, selected_item);
154 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
156 bool val = temp_var==1?true:false;
157 talk_id(val? setting->bool_setting->lang_yes :
158 setting->bool_setting->lang_no, false);
160 #if 0 /* probably dont need this one */
161 else if ((setting->flags & F_FILENAME) == F_FILENAME)
164 #endif
165 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
166 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
168 const struct int_setting *int_info = setting->int_setting;
169 const struct table_setting *tbl_info = setting->table_setting;
170 int unit;
171 int32_t (*get_talk_id)(int, int);
172 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
174 unit = int_info->unit;
175 get_talk_id = int_info->get_talk_id;
177 else
179 unit = tbl_info->unit;
180 get_talk_id = tbl_info->get_talk_id;
182 if (get_talk_id)
183 talk_id(get_talk_id((int)temp_var, unit), false);
184 else
185 talk_value((int)temp_var, unit, false);
187 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
189 int talkunit = UNIT_INT;
190 const char *unit = sound_unit(setting->sound_setting->setting);
191 if (!strcmp(unit, "dB"))
192 talkunit = UNIT_DB;
193 else if (!strcmp(unit, "%"))
194 talkunit = UNIT_PERCENT;
195 else if (!strcmp(unit, "Hz"))
196 talkunit = UNIT_HERTZ;
197 talk_value((int)temp_var, talkunit, false);
199 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
201 int value = (int)temp_var;
202 if (setting->flags & F_CHOICETALKS)
204 talk_id(setting->choice_setting->talks[value], false);
206 else
208 talk_id(P2ID(setting->choice_setting->desc[value]), false);
211 return 0;
214 #ifdef HAVE_QUICKSCREEN /* only the quickscreen uses this so far */
215 void option_select_next_val(struct settings_list *setting)
217 int val = 0;
218 int *value = setting->setting;
219 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
221 *(bool*)value = !*(bool*)value;
222 return;
224 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
226 struct int_setting *info = (struct int_setting *)setting->int_setting;
227 val = *value + info->step;
228 if (val > info->max)
229 val = info->min;
231 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
233 int setting_id = setting->sound_setting->setting;
234 int steps = sound_steps(setting_id);
235 int min = sound_min(setting_id);
236 int max = sound_max(setting_id);
237 val = *value + steps;
238 if (val >= max)
239 val = min;
241 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
243 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
244 val = *value + 1;
245 if (val >= info->count)
246 val = 0;
248 *value = val;
250 #endif
252 static int selection_to_val(struct settings_list *setting, int selection)
254 int min = 0, max = 0, step = 1;
255 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
256 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
257 return selection;
258 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
260 const struct table_setting *info = setting->table_setting;
261 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
262 table_setting_array_position != -1 &&
263 (selection >= table_setting_array_position))
265 if (selection == table_setting_array_position)
266 return table_setting_oldval;
267 return info->values[selection-1];
269 else
270 return info->values[selection];
272 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
274 int setting_id = setting->sound_setting->setting;
275 #ifndef ASCENDING_INT_SETTINGS
276 step = sound_steps(setting_id);
277 max = sound_max(setting_id);
278 min = sound_min(setting_id);
279 #else
280 step = -sound_steps(setting_id);
281 min = sound_max(setting_id);
282 max = sound_min(setting_id);
283 #endif
285 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
287 const struct int_setting *info = setting->int_setting;
288 #ifndef ASCENDING_INT_SETTINGS
289 min = info->min;
290 max = info->max;
291 step = info->step;
292 #else
293 max = info->min;
294 min = info->max;
295 step = -info->step;
296 #endif
298 return max- (selection * step);
300 static char * value_setting_get_name_cb(int selected_item,
301 void * data, char *buffer)
303 selected_item = selection_to_val(data, selected_item);
304 return option_get_valuestring(data, buffer, MAX_PATH, selected_item);
307 /* wrapper to convert from int param to bool param in option_screen */
308 static void (*boolfunction)(bool);
309 static void bool_funcwrapper(int value)
311 if (value)
312 boolfunction(true);
313 else
314 boolfunction(false);
317 bool option_screen(struct settings_list *setting,
318 bool use_temp_var, unsigned char* option_title)
320 int action;
321 bool done = false;
322 struct gui_synclist lists;
323 int oldvalue, nb_items = 0, selected = 0, temp_var;
324 int *variable;
325 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
326 int var_type = setting->flags&F_T_MASK;
327 void (*function)(int) = NULL;
328 char *title;
329 if (var_type == F_T_INT || var_type == F_T_UINT)
331 variable = use_temp_var ? &temp_var: (int*)setting->setting;
332 temp_var = oldvalue = *(int*)setting->setting;
334 else if (var_type == F_T_BOOL)
336 /* bools always use the temp variable...
337 if use_temp_var is false it will be copied to setting->setting every change */
338 variable = &temp_var;
339 temp_var = oldvalue = *(bool*)setting->setting?1:0;
341 else return false; /* only int/bools can go here */
342 gui_synclist_init(&lists, value_setting_get_name_cb,
343 (void*)setting, false, 1);
344 if (setting->lang_id == -1)
345 title = (char*)setting->cfg_vals;
346 else
347 title = P2STR(option_title);
349 gui_synclist_set_title(&lists, title, Icon_Questionmark);
350 gui_synclist_set_icon_callback(&lists, NULL);
351 if(global_settings.talk_menu)
352 gui_synclist_set_voice_callback(&lists, option_talk);
354 /* set the number of items and current selection */
355 if (var_type == F_T_INT || var_type == F_T_UINT)
357 if (setting->flags&F_CHOICE_SETTING)
359 nb_items = setting->choice_setting->count;
360 selected = oldvalue;
361 function = setting->choice_setting->option_callback;
363 else if (setting->flags&F_TABLE_SETTING)
365 const struct table_setting *info = setting->table_setting;
366 int i;
367 nb_items = info->count;
368 selected = -1;
369 table_setting_array_position = -1;
370 for (i=0;selected==-1 && i<nb_items;i++)
372 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
373 (oldvalue < info->values[i]))
375 table_setting_oldval = oldvalue;
376 table_setting_array_position = i;
377 selected = i;
378 nb_items++;
380 else if (oldvalue == info->values[i])
381 selected = i;
383 function = info->option_callback;
385 else if (setting->flags&F_T_SOUND)
387 int setting_id = setting->sound_setting->setting;
388 int steps = sound_steps(setting_id);
389 int min = sound_min(setting_id);
390 int max = sound_max(setting_id);
391 nb_items = (max-min)/steps + 1;
392 #ifndef ASCENDING_INT_SETTINGS
393 selected = (max - oldvalue) / steps;
394 #else
395 selected = (oldvalue - min) / steps;
396 #endif
397 function = sound_get_fn(setting_id);
399 else
401 const struct int_setting *info = setting->int_setting;
402 int min, max, step;
403 max = info->max;
404 min = info->min;
405 step = info->step;
406 nb_items = (max-min)/step + 1;
407 #ifndef ASCENDING_INT_SETTINGS
408 selected = (max - oldvalue) / step;
409 #else
410 selected = (oldvalue - min) / step;
411 #endif
412 function = info->option_callback;
415 else if (var_type == F_T_BOOL)
417 selected = oldvalue;
418 nb_items = 2;
419 boolfunction = setting->bool_setting->option_callback;
420 if (boolfunction)
421 function = bool_funcwrapper;
423 gui_synclist_set_nb_items(&lists, nb_items);
424 gui_synclist_select_item(&lists, selected);
426 gui_synclist_limit_scroll(&lists, true);
427 gui_synclist_draw(&lists);
428 /* talk the item */
429 gui_synclist_speak_item(&lists);
430 while (!done)
432 if (list_do_action(CONTEXT_LIST, TIMEOUT_BLOCK,
433 &lists, &action,
434 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
436 selected = gui_synclist_get_sel_pos(&lists);
437 *variable = selection_to_val(setting, selected);
438 if (var_type == F_T_BOOL)
440 if (!use_temp_var)
441 *(bool*)setting->setting = selected==1?true:false;
444 else if (action == ACTION_NONE)
445 continue;
446 else if (action == ACTION_STD_CANCEL)
448 bool show_cancel = false;
449 if (use_temp_var)
450 show_cancel = true;
451 else if (var_type == F_T_INT || var_type == F_T_UINT)
453 if (*variable != oldvalue)
455 show_cancel = true;
456 *variable = oldvalue;
459 else
461 if (*variable != oldvalue)
463 show_cancel = true;
464 if (!use_temp_var)
465 *(bool*)setting->setting = oldvalue==1?true:false;
466 *variable = oldvalue;
469 if (show_cancel)
470 gui_syncsplash(HZ/2, ID2P(LANG_CANCEL));
471 done = true;
473 else if (action == ACTION_STD_OK)
475 done = true;
477 else if(default_event_handler(action) == SYS_USB_CONNECTED)
478 return true;
479 gui_syncstatusbar_draw(&statusbars, false);
480 /* callback */
481 if ( function )
482 function(*variable);
485 if (oldvalue != *variable && (action != ACTION_STD_CANCEL))
487 if (use_temp_var)
489 if (var_type == F_T_INT || var_type == F_T_UINT)
490 *(int*)setting->setting = *variable;
491 else
492 *(bool*)setting->setting = *variable?true:false;
494 settings_save();
497 return false;
500 /******************************************************
501 Compatability functions
502 *******************************************************/
503 #define MAX_OPTIONS 32
504 const struct opt_items *set_option_options;
505 void set_option_formatter(char* buf, size_t size, int item, const char* unit)
507 (void)unit;
508 const unsigned char *text = set_option_options[item].string;
509 snprintf(buf, size, "%s", P2STR(text));
511 int32_t set_option_get_talk_id(int value, int unit)
513 (void)unit;
514 return set_option_options[value].voice_id;
516 bool set_option(const char* string, void* variable, enum optiontype type,
517 const struct opt_items* options,
518 int numoptions, void (*function)(int))
520 int temp;
521 struct settings_list item;
522 struct int_setting data = {
523 function, UNIT_INT, 0, numoptions-1, 1,
524 set_option_formatter, set_option_get_talk_id
526 set_option_options = options;
527 item.int_setting = &data;
528 item.flags = F_INT_SETTING|F_T_INT;
529 item.lang_id = -1;
530 item.cfg_vals = (char*)string;
531 item.setting = &temp;
532 if (type == BOOL)
533 temp = *(bool*)variable? 1: 0;
534 else
535 temp = *(int*)variable;
536 if (!option_screen(&item, false, NULL))
538 if (type == BOOL)
539 *(bool*)variable = (temp == 1? true: false);
540 else
541 *(int*)variable = temp;
542 return false;
544 return true;
547 bool set_int_ex(const unsigned char* string,
548 const char* unit,
549 int voice_unit,
550 int* variable,
551 void (*function)(int),
552 int step,
553 int min,
554 int max,
555 void (*formatter)(char*, size_t, int, const char*),
556 int32_t (*get_talk_id)(int, int))
558 (void)unit;
559 struct settings_list item;
560 struct int_setting data = {
561 function, voice_unit, min, max, step,
562 formatter, get_talk_id
564 item.int_setting = &data;
565 item.flags = F_INT_SETTING|F_T_INT;
566 item.lang_id = -1;
567 item.cfg_vals = (char*)string;
568 item.setting = variable;
569 return option_screen(&item, false, NULL);
572 /* to be replaced */
573 void option_select_init_items(struct option_select * opt,
574 const char * title,
575 int selected,
576 const struct opt_items * items,
577 int nb_items)
579 opt->title=title;
580 opt->min_value=0;
581 opt->max_value=nb_items;
582 opt->option=selected;
583 opt->items=items;
586 void option_select_next(struct option_select * opt)
588 if(opt->option + 1 >= opt->max_value)
590 if(opt->option==opt->max_value-1)
591 opt->option=opt->min_value;
592 else
593 opt->option=opt->max_value-1;
595 else
596 opt->option+=1;
599 void option_select_prev(struct option_select * opt)
601 if(opt->option - 1 < opt->min_value)
603 /* the dissimilarity to option_select_next() arises from the
604 * sleep timer problem (bug #5000 and #5001):
605 * there we have min=0, step = 5 but the value itself might
606 * not be a multiple of 5 -- as time elapsed;
607 * We need to be able to set timer to 0 (= Off) nevertheless. */
608 if(opt->option!=opt->min_value)
609 opt->option=opt->min_value;
610 else
611 opt->option=opt->max_value-1;
613 else
614 opt->option-=1;
617 const char * option_select_get_text(struct option_select * opt)
619 return(P2STR(opt->items[opt->option].string));