Move Info tab content to a separate widget.
[maemo-rb.git] / apps / gui / option_select.c
blob7c3d34f024143dfa17832f3b0b143ba069cac836
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"
39 static int selection_to_val(const struct settings_list *setting, int selection);
40 int option_value_as_int(const struct settings_list *setting)
42 int type = (setting->flags & F_T_MASK);
43 int temp = 0;
44 if (type == F_T_BOOL)
45 temp = *(bool*)setting->setting?1:0;
46 else if (type == F_T_UINT || type == F_T_INT)
47 temp = *(int*)setting->setting;
48 return temp;
50 static const char *unit_strings[] =
52 [UNIT_INT] = "", [UNIT_MS] = "ms",
53 [UNIT_SEC] = "s", [UNIT_MIN] = "min",
54 [UNIT_HOUR]= "hr", [UNIT_KHZ] = "kHz",
55 [UNIT_DB] = "dB", [UNIT_PERCENT] = "%",
56 [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
57 [UNIT_PER_SEC] = "per sec",
58 [UNIT_HERTZ] = "Hz",
59 [UNIT_MB] = "MB", [UNIT_KBIT] = "kb/s",
60 [UNIT_PM_TICK] = "units/10ms",
62 /* these two vars are needed so arbitrary values can be added to the
63 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
64 static int table_setting_oldval = 0, table_setting_array_position = 0;
65 const char *option_get_valuestring(const struct settings_list *setting,
66 char *buffer, int buf_len,
67 intptr_t temp_var)
69 const char* str = buffer;
70 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
72 bool val = (bool)temp_var;
73 strlcpy(buffer, str(val? setting->bool_setting->lang_yes :
74 setting->bool_setting->lang_no), buf_len);
76 #if 0 /* probably dont need this one */
77 else if ((setting->flags & F_FILENAME) == F_FILENAME)
79 struct filename_setting *info = setting->filename_setting;
80 snprintf(buffer, buf_len, "%s%s%s", info->prefix,
81 (char*)temp_var, info->suffix);
83 #endif
84 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
85 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
87 const struct int_setting *int_info = setting->int_setting;
88 const struct table_setting *tbl_info = setting->table_setting;
89 const char *unit;
90 const char* (*formatter)(char*, size_t, int, const char*);
91 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
93 formatter = int_info->formatter;
94 unit = unit_strings[int_info->unit];
96 else
98 formatter = tbl_info->formatter;
99 unit = unit_strings[tbl_info->unit];
101 if (formatter)
102 str = formatter(buffer, buf_len, (int)temp_var, unit);
103 else
104 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit?unit:"");
106 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
108 char sign = ' ';
109 const char *unit = sound_unit(setting->sound_setting->setting);
110 int val = sound_val2phys(setting->sound_setting->setting, (int)temp_var);
111 if (sound_numdecimals(setting->sound_setting->setting))
113 int integer, dec;
114 if(val < 0)
116 sign = '-';
117 val = abs(val);
119 integer = val / 10;
120 dec = val % 10;
121 snprintf(buffer, buf_len, "%c%d.%d %s", sign, integer, dec, unit);
123 else
124 snprintf(buffer, buf_len, "%d %s", val, unit);
126 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
128 if (setting->flags & F_CHOICETALKS)
130 int setting_id;
131 const struct choice_setting *info = setting->choice_setting;
132 if (info->talks[(int)temp_var] < LANG_LAST_INDEX_IN_ARRAY)
134 strlcpy(buffer, str(info->talks[(int)temp_var]), buf_len);
136 else
138 find_setting(setting->setting, &setting_id);
139 cfg_int_to_string(setting_id, (int)temp_var, buffer, buf_len);
142 else
144 int value = (int)temp_var;
145 char *val = P2STR(setting->choice_setting->desc[value]);
146 strlcpy(buffer, val, buf_len);
149 return str;
151 void option_talk_value(const struct settings_list *setting, int value, bool enqueue)
153 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
155 bool val = (value==1);
156 talk_id(val? setting->bool_setting->lang_yes :
157 setting->bool_setting->lang_no, enqueue);
159 #if 0 /* probably dont need this one */
160 else if ((setting->flags & F_FILENAME) == F_FILENAME)
163 #endif
164 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
165 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
167 const struct int_setting *int_info = setting->int_setting;
168 const struct table_setting *tbl_info = setting->table_setting;
169 int unit;
170 int32_t (*get_talk_id)(int, int);
171 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
173 unit = int_info->unit;
174 get_talk_id = int_info->get_talk_id;
176 else
178 unit = tbl_info->unit;
179 get_talk_id = tbl_info->get_talk_id;
181 if (get_talk_id)
182 talk_id(get_talk_id(value, unit), enqueue);
183 else
184 talk_value(value, unit, enqueue);
186 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
188 int talkunit = UNIT_INT;
189 int sound_setting = setting->sound_setting->setting;
190 const char *unit = sound_unit(sound_setting);
191 int decimals = sound_numdecimals(sound_setting);
192 int phys = sound_val2phys(sound_setting, value);
193 if (!strcmp(unit, "dB"))
194 talkunit = UNIT_DB;
195 else if (!strcmp(unit, "%"))
196 talkunit = UNIT_PERCENT;
197 else if (!strcmp(unit, "Hz"))
198 talkunit = UNIT_HERTZ;
199 talk_value_decimal(phys, talkunit, decimals, false);
201 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
203 if (setting->flags & F_CHOICETALKS)
205 talk_id(setting->choice_setting->talks[value], enqueue);
207 else
209 talk_id(P2ID(setting->choice_setting->desc[value]), enqueue);
214 static int option_talk(int selected_item, void * data)
216 struct settings_list *setting = (struct settings_list *)data;
217 int temp_var = selection_to_val(setting, selected_item);
218 option_talk_value(setting, temp_var, false);
219 return 0;
222 #if defined(HAVE_QUICKSCREEN) || defined(HAVE_RECORDING) || defined(HAVE_TOUCHSCREEN)
223 /* only the quickscreen and recording trigger needs this */
224 void option_select_next_val(const struct settings_list *setting,
225 bool previous, bool apply)
227 int val = 0;
228 int *value = setting->setting;
229 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
231 *(bool*)value = !*(bool*)value;
232 if (apply && setting->bool_setting->option_callback)
233 setting->bool_setting->option_callback(*(bool*)value);
234 return;
236 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
238 struct int_setting *info = (struct int_setting *)setting->int_setting;
239 bool neg_step = (info->step < 0);
240 if (!previous)
242 val = *value + info->step;
243 if (neg_step ? (val < info->max) : (val > info->max))
244 val = info->min;
246 else
248 val = *value - info->step;
249 if (neg_step ? (val > info->min) : (val < info->min))
250 val = info->max;
252 *value = val;
253 if (apply && info->option_callback)
254 info->option_callback(val);
256 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
258 int setting_id = setting->sound_setting->setting;
259 int steps = sound_steps(setting_id);
260 int min = sound_min(setting_id);
261 int max = sound_max(setting_id);
262 if (!previous)
264 val = *value + steps;
265 if (val >= max)
266 val = min;
268 else
270 val = *value - steps;
271 if (val < min)
272 val = max;
274 *value = val;
275 if (apply)
276 sound_set(setting_id, val);
278 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
280 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
281 if (!previous)
283 val = *value + 1;
284 if (val >= info->count)
285 val = 0;
287 else
289 val = *value - 1;
290 if (val < 0)
291 val = info->count-1;
293 *value = val;
294 if (apply && info->option_callback)
295 info->option_callback(val);
297 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
299 const struct table_setting *tbl_info = setting->table_setting;
300 int i, add;
301 add = previous?tbl_info->count-1:1;
302 for (i=0; i<tbl_info->count;i++)
304 if ((*value == tbl_info->values[i]) ||
305 (settings->flags&F_ALLOW_ARBITRARY_VALS &&
306 *value < tbl_info->values[i]))
308 val = tbl_info->values[(i+add)%tbl_info->count];
309 break;
312 *value = val;
313 if (apply && tbl_info->option_callback)
314 tbl_info->option_callback(val);
317 #endif
319 static int selection_to_val(const struct settings_list *setting, int selection)
321 /* rockbox: comment 'set but unused' variables
322 int min = 0;
324 int 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 push_current_activity(ACTIVITY_OPTIONSELECT);
493 gui_synclist_init(&lists, value_setting_get_name_cb,
494 (void*)setting, false, 1, parent);
495 if (setting->lang_id == -1)
496 title = (char*)setting->cfg_vals;
497 else
498 title = P2STR(option_title);
500 gui_synclist_set_title(&lists, title, Icon_Questionmark);
501 gui_synclist_set_icon_callback(&lists, NULL);
502 if(global_settings.talk_menu)
503 gui_synclist_set_voice_callback(&lists, option_talk);
505 val_to_selection(setting, oldvalue, &nb_items, &selected, &function);
506 gui_synclist_set_nb_items(&lists, nb_items);
507 gui_synclist_select_item(&lists, selected);
509 gui_synclist_limit_scroll(&lists, true);
510 gui_synclist_draw(&lists);
511 /* talk the item */
512 gui_synclist_speak_item(&lists);
513 while (!done)
515 if (list_do_action(CONTEXT_LIST, HZ, /* HZ so the status bar redraws */
516 &lists, &action,
517 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
519 /* setting changed */
520 selected = gui_synclist_get_sel_pos(&lists);
521 *variable = selection_to_val(setting, selected);
522 if (var_type == F_T_BOOL && !use_temp_var)
523 *(bool*)setting->setting = (*variable==1);
525 else if (action == ACTION_NONE)
526 continue;
527 else if (action == ACTION_STD_CANCEL)
529 /* setting canceled, restore old value if changed */
530 if (*variable != oldvalue)
532 *variable = oldvalue;
533 if (var_type == F_T_BOOL && !use_temp_var)
534 *(bool*)setting->setting = (oldvalue==1);
535 splash(HZ/2, ID2P(LANG_CANCEL));
537 done = true;
539 else if (action == ACTION_STD_CONTEXT)
541 /* reset setting to default */
542 reset_setting(setting, variable);
543 if (var_type == F_T_BOOL && !use_temp_var)
544 *(bool*)setting->setting = (*variable==1);
545 val_to_selection(setting, *variable, &nb_items,
546 &selected, &function);
547 gui_synclist_select_item(&lists, selected);
548 gui_synclist_draw(&lists);
549 gui_synclist_speak_item(&lists);
551 else if (action == ACTION_STD_OK)
553 /* setting accepted, store now if it used a temp var */
554 if (use_temp_var)
556 if (var_type == F_T_INT || var_type == F_T_UINT)
557 *(int*)setting->setting = *variable;
558 else
559 *(bool*)setting->setting = (*variable==1);
561 settings_save();
562 done = true;
564 else if(default_event_handler(action) == SYS_USB_CONNECTED)
565 return true;
566 /* callback */
567 if ( function )
568 function(*variable);
569 /* if the volume is changing we need to let the skins know */
570 if (function == sound_get_fn(SOUND_VOLUME))
571 global_status.last_volume_change = current_tick;
573 pop_current_activity();
574 return false;