updated the quickscreen's:
[Rockbox.git] / apps / gui / option_select.c
blobb4b1b716bb7c93abd9274a11f87cb5c45d75a451
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 void option_select_next_val(struct settings_list *setting)
216 int val = 0;
217 int *value = setting->setting;
218 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
220 *(bool*)value = !*(bool*)value;
221 return;
223 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
225 struct int_setting *info = (struct int_setting *)setting->int_setting;
226 val = *value + info->step;
227 if (val > info->max)
228 val = info->min;
230 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
232 int setting_id = setting->sound_setting->setting;
233 int steps = sound_steps(setting_id);
234 int min = sound_min(setting_id);
235 int max = sound_max(setting_id);
236 val = *value + steps;
237 if (val >= max)
238 val = min;
240 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
242 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
243 val = *value + 1;
244 if (val >= info->count)
245 val = 0;
247 *value = val;
250 static int selection_to_val(struct settings_list *setting, int selection)
252 int min = 0, max = 0, step = 1;
253 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
254 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
255 return selection;
256 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
258 const struct table_setting *info = setting->table_setting;
259 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
260 table_setting_array_position != -1 &&
261 (selection >= table_setting_array_position))
263 if (selection == table_setting_array_position)
264 return table_setting_oldval;
265 return info->values[selection-1];
267 else
268 return info->values[selection];
270 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
272 int setting_id = setting->sound_setting->setting;
273 #ifndef ASCENDING_INT_SETTINGS
274 step = sound_steps(setting_id);
275 max = sound_max(setting_id);
276 min = sound_min(setting_id);
277 #else
278 step = -sound_steps(setting_id);
279 min = sound_max(setting_id);
280 max = sound_min(setting_id);
281 #endif
283 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
285 const struct int_setting *info = setting->int_setting;
286 #ifndef ASCENDING_INT_SETTINGS
287 min = info->min;
288 max = info->max;
289 step = info->step;
290 #else
291 max = info->min;
292 min = info->max;
293 step = -info->step;
294 #endif
296 return max- (selection * step);
298 static char * value_setting_get_name_cb(int selected_item,
299 void * data, char *buffer)
301 selected_item = selection_to_val(data, selected_item);
302 return option_get_valuestring(data, buffer, MAX_PATH, selected_item);
305 /* wrapper to convert from int param to bool param in option_screen */
306 static void (*boolfunction)(bool);
307 static void bool_funcwrapper(int value)
309 if (value)
310 boolfunction(true);
311 else
312 boolfunction(false);
315 bool option_screen(struct settings_list *setting,
316 bool use_temp_var, unsigned char* option_title)
318 int action;
319 bool done = false;
320 struct gui_synclist lists;
321 int oldvalue, nb_items = 0, selected = 0, temp_var;
322 int *variable;
323 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
324 int var_type = setting->flags&F_T_MASK;
325 void (*function)(int) = NULL;
326 char *title;
327 if (var_type == F_T_INT || var_type == F_T_UINT)
329 variable = use_temp_var ? &temp_var: (int*)setting->setting;
330 temp_var = oldvalue = *(int*)setting->setting;
332 else if (var_type == F_T_BOOL)
334 /* bools always use the temp variable...
335 if use_temp_var is false it will be copied to setting->setting every change */
336 variable = &temp_var;
337 temp_var = oldvalue = *(bool*)setting->setting?1:0;
339 else return false; /* only int/bools can go here */
340 gui_synclist_init(&lists, value_setting_get_name_cb,
341 (void*)setting, false, 1);
342 if (setting->lang_id == -1)
343 title = (char*)setting->cfg_vals;
344 else
345 title = P2STR(option_title);
347 gui_synclist_set_title(&lists, title, Icon_Questionmark);
348 gui_synclist_set_icon_callback(&lists, NULL);
349 if(global_settings.talk_menu)
350 gui_synclist_set_voice_callback(&lists, option_talk);
352 /* set the number of items and current selection */
353 if (var_type == F_T_INT || var_type == F_T_UINT)
355 if (setting->flags&F_CHOICE_SETTING)
357 nb_items = setting->choice_setting->count;
358 selected = oldvalue;
359 function = setting->choice_setting->option_callback;
361 else if (setting->flags&F_TABLE_SETTING)
363 const struct table_setting *info = setting->table_setting;
364 int i;
365 nb_items = info->count;
366 selected = -1;
367 table_setting_array_position = -1;
368 for (i=0;selected==-1 && i<nb_items;i++)
370 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
371 (oldvalue < info->values[i]))
373 table_setting_oldval = oldvalue;
374 table_setting_array_position = i;
375 selected = i;
376 nb_items++;
378 else if (oldvalue == info->values[i])
379 selected = i;
381 function = info->option_callback;
383 else if (setting->flags&F_T_SOUND)
385 int setting_id = setting->sound_setting->setting;
386 int steps = sound_steps(setting_id);
387 int min = sound_min(setting_id);
388 int max = sound_max(setting_id);
389 nb_items = (max-min)/steps + 1;
390 #ifndef ASCENDING_INT_SETTINGS
391 selected = (max - oldvalue) / steps;
392 #else
393 selected = (oldvalue - min) / steps;
394 #endif
395 function = sound_get_fn(setting_id);
397 else
399 const struct int_setting *info = setting->int_setting;
400 int min, max, step;
401 max = info->max;
402 min = info->min;
403 step = info->step;
404 nb_items = (max-min)/step + 1;
405 #ifndef ASCENDING_INT_SETTINGS
406 selected = (max - oldvalue) / step;
407 #else
408 selected = (oldvalue - min) / step;
409 #endif
410 function = info->option_callback;
413 else if (var_type == F_T_BOOL)
415 selected = oldvalue;
416 nb_items = 2;
417 boolfunction = setting->bool_setting->option_callback;
418 if (boolfunction)
419 function = bool_funcwrapper;
421 gui_synclist_set_nb_items(&lists, nb_items);
422 gui_synclist_select_item(&lists, selected);
424 gui_synclist_limit_scroll(&lists, true);
425 gui_synclist_draw(&lists);
426 /* talk the item */
427 gui_synclist_speak_item(&lists);
428 while (!done)
430 if (list_do_action(CONTEXT_LIST, TIMEOUT_BLOCK,
431 &lists, &action,
432 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
434 selected = gui_synclist_get_sel_pos(&lists);
435 *variable = selection_to_val(setting, selected);
436 if (var_type == F_T_BOOL)
438 if (!use_temp_var)
439 *(bool*)setting->setting = selected==1?true:false;
442 else if (action == ACTION_NONE)
443 continue;
444 else if (action == ACTION_STD_CANCEL)
446 bool show_cancel = false;
447 if (use_temp_var)
448 show_cancel = true;
449 else if (var_type == F_T_INT || var_type == F_T_UINT)
451 if (*variable != oldvalue)
453 show_cancel = true;
454 *variable = oldvalue;
457 else
459 if (*variable != oldvalue)
461 show_cancel = true;
462 if (!use_temp_var)
463 *(bool*)setting->setting = oldvalue==1?true:false;
464 *variable = oldvalue;
467 if (show_cancel)
468 gui_syncsplash(HZ/2, ID2P(LANG_CANCEL));
469 done = true;
471 else if (action == ACTION_STD_OK)
473 done = true;
475 else if(default_event_handler(action) == SYS_USB_CONNECTED)
476 return true;
477 gui_syncstatusbar_draw(&statusbars, false);
478 /* callback */
479 if ( function )
480 function(*variable);
483 if (oldvalue != *variable && (action != ACTION_STD_CANCEL))
485 if (use_temp_var)
487 if (var_type == F_T_INT || var_type == F_T_UINT)
488 *(int*)setting->setting = *variable;
489 else
490 *(bool*)setting->setting = *variable?true:false;
492 settings_save();
495 return false;
498 /******************************************************
499 Compatability functions
500 *******************************************************/
501 #define MAX_OPTIONS 32
502 const struct opt_items *set_option_options;
503 void set_option_formatter(char* buf, size_t size, int item, const char* unit)
505 (void)unit;
506 const unsigned char *text = set_option_options[item].string;
507 snprintf(buf, size, "%s", P2STR(text));
509 int32_t set_option_get_talk_id(int value, int unit)
511 (void)unit;
512 return set_option_options[value].voice_id;
514 bool set_option(const char* string, void* variable, enum optiontype type,
515 const struct opt_items* options,
516 int numoptions, void (*function)(int))
518 int temp;
519 struct settings_list item;
520 struct int_setting data = {
521 function, UNIT_INT, 0, numoptions-1, 1,
522 set_option_formatter, set_option_get_talk_id
524 set_option_options = options;
525 item.int_setting = &data;
526 item.flags = F_INT_SETTING|F_T_INT;
527 item.lang_id = -1;
528 item.cfg_vals = (char*)string;
529 item.setting = &temp;
530 if (type == BOOL)
531 temp = *(bool*)variable? 1: 0;
532 else
533 temp = *(int*)variable;
534 if (!option_screen(&item, false, NULL))
536 if (type == BOOL)
537 *(bool*)variable = (temp == 1? true: false);
538 else
539 *(int*)variable = temp;
540 return false;
542 return true;
545 bool set_int_ex(const unsigned char* string,
546 const char* unit,
547 int voice_unit,
548 int* variable,
549 void (*function)(int),
550 int step,
551 int min,
552 int max,
553 void (*formatter)(char*, size_t, int, const char*),
554 int32_t (*get_talk_id)(int, int))
556 (void)unit;
557 struct settings_list item;
558 struct int_setting data = {
559 function, voice_unit, min, max, step,
560 formatter, get_talk_id
562 item.int_setting = &data;
563 item.flags = F_INT_SETTING|F_T_INT;
564 item.lang_id = -1;
565 item.cfg_vals = (char*)string;
566 item.setting = variable;
567 return option_screen(&item, false, NULL);
570 /* to be replaced */
571 void option_select_init_items(struct option_select * opt,
572 const char * title,
573 int selected,
574 const struct opt_items * items,
575 int nb_items)
577 opt->title=title;
578 opt->min_value=0;
579 opt->max_value=nb_items;
580 opt->option=selected;
581 opt->items=items;
584 void option_select_next(struct option_select * opt)
586 if(opt->option + 1 >= opt->max_value)
588 if(opt->option==opt->max_value-1)
589 opt->option=opt->min_value;
590 else
591 opt->option=opt->max_value-1;
593 else
594 opt->option+=1;
597 void option_select_prev(struct option_select * opt)
599 if(opt->option - 1 < opt->min_value)
601 /* the dissimilarity to option_select_next() arises from the
602 * sleep timer problem (bug #5000 and #5001):
603 * there we have min=0, step = 5 but the value itself might
604 * not be a multiple of 5 -- as time elapsed;
605 * We need to be able to set timer to 0 (= Off) nevertheless. */
606 if(opt->option!=opt->min_value)
607 opt->option=opt->min_value;
608 else
609 opt->option=opt->max_value-1;
611 else
612 opt->option-=1;
615 const char * option_select_get_text(struct option_select * opt)
617 return(P2STR(opt->items[opt->option].string));