Also fix Debug->View partitions when SECTOR_SIZE!=512
[kugel-rb.git] / apps / gui / option_select.c
blob1f5e8706f990be797a4ee8d772b0e8eadc54b69c
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.h>
24 #include "config.h"
25 #include "option_select.h"
26 #include "sprintf.h"
27 #include "kernel.h"
28 #include "lang.h"
29 #include "talk.h"
30 #include "settings_list.h"
31 #include "sound.h"
32 #include "list.h"
33 #include "action.h"
34 #include "misc.h"
35 #include "splash.h"
36 #include "menu.h"
37 #include "quickscreen.h"
39 #if defined (HAVE_SCROLLWHEEL) || \
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(const struct settings_list *setting, int selection);
47 int option_value_as_int(const struct settings_list *setting)
49 int type = (setting->flags & F_T_MASK);
50 int temp = 0;
51 if (type == F_T_BOOL)
52 temp = *(bool*)setting->setting?1:0;
53 else if (type == F_T_UINT || type == F_T_INT)
54 temp = *(int*)setting->setting;
55 return temp;
57 static const char *unit_strings[] =
59 [UNIT_INT] = "", [UNIT_MS] = "ms",
60 [UNIT_SEC] = "s", [UNIT_MIN] = "min",
61 [UNIT_HOUR]= "hr", [UNIT_KHZ] = "kHz",
62 [UNIT_DB] = "dB", [UNIT_PERCENT] = "%",
63 [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
64 [UNIT_PER_SEC] = "per sec",
65 [UNIT_HERTZ] = "Hz",
66 [UNIT_MB] = "MB", [UNIT_KBIT] = "kb/s",
67 [UNIT_PM_TICK] = "units/10ms",
69 /* these two vars are needed so arbitrary values can be added to the
70 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
71 static int table_setting_oldval = 0, table_setting_array_position = 0;
72 const char *option_get_valuestring(const struct settings_list *setting,
73 char *buffer, int buf_len,
74 intptr_t temp_var)
76 const char* str = buffer;
77 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
79 bool val = (bool)temp_var;
80 strlcpy(buffer, str(val? setting->bool_setting->lang_yes :
81 setting->bool_setting->lang_no), buf_len);
83 #if 0 /* probably dont need this one */
84 else if ((setting->flags & F_FILENAME) == F_FILENAME)
86 struct filename_setting *info = setting->filename_setting;
87 snprintf(buffer, buf_len, "%s%s%s", info->prefix,
88 (char*)temp_var, info->suffix);
90 #endif
91 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
92 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
94 const struct int_setting *int_info = setting->int_setting;
95 const struct table_setting *tbl_info = setting->table_setting;
96 const char *unit;
97 const char* (*formatter)(char*, size_t, int, const char*);
98 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
100 formatter = int_info->formatter;
101 unit = unit_strings[int_info->unit];
103 else
105 formatter = tbl_info->formatter;
106 unit = unit_strings[tbl_info->unit];
108 if (formatter)
109 str = formatter(buffer, buf_len, (int)temp_var, unit);
110 else
111 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit?unit:"");
113 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
115 char sign = ' ';
116 const char *unit = sound_unit(setting->sound_setting->setting);
117 if (sound_numdecimals(setting->sound_setting->setting))
119 int integer, dec;
120 int val = sound_val2phys(setting->sound_setting->setting,
121 (int)temp_var);
122 if(val < 0)
124 sign = '-';
125 val = abs(val);
127 integer = val / 10;
128 dec = val % 10;
129 snprintf(buffer, buf_len, "%c%d.%d %s", sign, integer, dec, unit);
131 else
132 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit);
134 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
136 if (setting->flags & F_CHOICETALKS)
138 int setting_id;
139 const struct choice_setting *info = setting->choice_setting;
140 if (info->talks[(int)temp_var] < LANG_LAST_INDEX_IN_ARRAY)
142 strlcpy(buffer, str(info->talks[(int)temp_var]), buf_len);
144 else
146 find_setting(setting->setting, &setting_id);
147 cfg_int_to_string(setting_id, (int)temp_var, buffer, buf_len);
150 else
152 int value = (int)temp_var;
153 char *val = P2STR(setting->choice_setting->desc[value]);
154 strlcpy(buffer, val, buf_len);
157 return str;
159 void option_talk_value(const struct settings_list *setting, int value, bool enqueue)
161 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
163 bool val = (value==1);
164 talk_id(val? setting->bool_setting->lang_yes :
165 setting->bool_setting->lang_no, enqueue);
167 #if 0 /* probably dont need this one */
168 else if ((setting->flags & F_FILENAME) == F_FILENAME)
171 #endif
172 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
173 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
175 const struct int_setting *int_info = setting->int_setting;
176 const struct table_setting *tbl_info = setting->table_setting;
177 int unit;
178 int32_t (*get_talk_id)(int, int);
179 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
181 unit = int_info->unit;
182 get_talk_id = int_info->get_talk_id;
184 else
186 unit = tbl_info->unit;
187 get_talk_id = tbl_info->get_talk_id;
189 if (get_talk_id)
190 talk_id(get_talk_id(value, unit), enqueue);
191 else
192 talk_value(value, unit, enqueue);
194 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
196 int talkunit = UNIT_INT;
197 const char *unit = sound_unit(setting->sound_setting->setting);
198 if (!strcmp(unit, "dB"))
199 talkunit = UNIT_DB;
200 else if (!strcmp(unit, "%"))
201 talkunit = UNIT_PERCENT;
202 else if (!strcmp(unit, "Hz"))
203 talkunit = UNIT_HERTZ;
204 talk_value(value, talkunit, false);
206 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
208 if (setting->flags & F_CHOICETALKS)
210 talk_id(setting->choice_setting->talks[value], enqueue);
212 else
214 talk_id(P2ID(setting->choice_setting->desc[value]), enqueue);
219 static int option_talk(int selected_item, void * data)
221 struct settings_list *setting = (struct settings_list *)data;
222 int temp_var = selection_to_val(setting, selected_item);
223 option_talk_value(setting, temp_var, false);
224 return 0;
227 #if defined(HAVE_QUICKSCREEN) || defined(HAVE_RECORDING)
228 /* only the quickscreen and recording trigger needs this */
229 void option_select_next_val(const struct settings_list *setting,
230 bool previous, bool apply)
232 int val = 0;
233 int *value = setting->setting;
234 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
236 *(bool*)value = !*(bool*)value;
237 if (apply && setting->bool_setting->option_callback)
238 setting->bool_setting->option_callback(*(bool*)value);
239 return;
241 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
243 struct int_setting *info = (struct int_setting *)setting->int_setting;
244 bool neg_step = (info->step < 0);
245 if (!previous)
247 val = *value + info->step;
248 if (neg_step ? (val < info->max) : (val > info->max))
249 val = info->min;
251 else
253 val = *value - info->step;
254 if (neg_step ? (val > info->min) : (val < info->min))
255 val = info->max;
257 *value = val;
258 if (apply && info->option_callback)
259 info->option_callback(val);
261 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
263 int setting_id = setting->sound_setting->setting;
264 int steps = sound_steps(setting_id);
265 int min = sound_min(setting_id);
266 int max = sound_max(setting_id);
267 if (!previous)
269 val = *value + steps;
270 if (val >= max)
271 val = min;
273 else
275 val = *value - steps;
276 if (val < min)
277 val = max;
279 *value = val;
281 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
283 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
284 val = *value + 1;
285 if (!previous)
287 val = *value + 1;
288 if (val >= info->count)
289 val = 0;
291 else
293 val = *value - 1;
294 if (val < 0)
295 val = info->count-1;
297 *value = val;
298 if (apply && info->option_callback)
299 info->option_callback(val);
301 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
303 const struct table_setting *tbl_info = setting->table_setting;
304 int i, add;
305 add = previous?tbl_info->count-1:1;
306 for (i=0; i<tbl_info->count;i++)
308 if ((*value == tbl_info->values[i]) ||
309 (settings->flags&F_ALLOW_ARBITRARY_VALS &&
310 *value < tbl_info->values[i]))
312 val = tbl_info->values[(i+add)%tbl_info->count];
313 break;
316 *value = val;
319 #endif
321 static int selection_to_val(const struct settings_list *setting, int selection)
323 int min = 0, max = 0, step = 1;
324 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
325 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
326 return selection;
327 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
329 const struct table_setting *info = setting->table_setting;
330 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
331 table_setting_array_position != -1 &&
332 (selection >= table_setting_array_position))
334 if (selection == table_setting_array_position)
335 return table_setting_oldval;
336 return info->values[selection-1];
338 else
339 return info->values[selection];
341 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
343 int setting_id = setting->sound_setting->setting;
344 #ifndef ASCENDING_INT_SETTINGS
345 step = sound_steps(setting_id);
346 max = sound_max(setting_id);
347 min = sound_min(setting_id);
348 #else
349 step = -sound_steps(setting_id);
350 min = sound_max(setting_id);
351 max = sound_min(setting_id);
352 #endif
354 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
356 const struct int_setting *info = setting->int_setting;
357 #ifndef ASCENDING_INT_SETTINGS
358 min = info->min;
359 max = info->max;
360 step = info->step;
361 #else
362 max = info->min;
363 min = info->max;
364 step = -info->step;
365 #endif
367 return max- (selection * step);
370 static const char * value_setting_get_name_cb(int selected_item,
371 void * data,
372 char *buffer,
373 size_t buffer_len)
375 selected_item = selection_to_val(data, selected_item);
376 return option_get_valuestring(data, buffer, buffer_len, selected_item);
379 /* wrapper to convert from int param to bool param in option_screen */
380 static void (*boolfunction)(bool);
381 static void bool_funcwrapper(int value)
383 if (value)
384 boolfunction(true);
385 else
386 boolfunction(false);
389 static void val_to_selection(const struct settings_list *setting, int oldvalue,
390 int *nb_items, int *selected,
391 void (**function)(int))
393 int var_type = setting->flags&F_T_MASK;
394 /* set the number of items and current selection */
395 if (var_type == F_T_INT || var_type == F_T_UINT)
397 if (setting->flags&F_CHOICE_SETTING)
399 *nb_items = setting->choice_setting->count;
400 *selected = oldvalue;
401 *function = setting->choice_setting->option_callback;
403 else if (setting->flags&F_TABLE_SETTING)
405 const struct table_setting *info = setting->table_setting;
406 int i;
407 *nb_items = info->count;
408 *selected = -1;
409 table_setting_array_position = -1;
410 for (i=0;*selected==-1 && i<*nb_items;i++)
412 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
413 (oldvalue < info->values[i]))
415 table_setting_oldval = oldvalue;
416 table_setting_array_position = i;
417 *selected = i;
418 (*nb_items)++;
420 else if (oldvalue == info->values[i])
421 *selected = i;
423 *function = info->option_callback;
425 else if (setting->flags&F_T_SOUND)
427 int setting_id = setting->sound_setting->setting;
428 int steps = sound_steps(setting_id);
429 int min = sound_min(setting_id);
430 int max = sound_max(setting_id);
431 *nb_items = (max-min)/steps + 1;
432 #ifndef ASCENDING_INT_SETTINGS
433 *selected = (max - oldvalue) / steps;
434 #else
435 *selected = (oldvalue - min) / steps;
436 #endif
437 *function = sound_get_fn(setting_id);
439 else
441 const struct int_setting *info = setting->int_setting;
442 int min, max, step;
443 max = info->max;
444 min = info->min;
445 step = info->step;
446 *nb_items = (max-min)/step + 1;
447 #ifndef ASCENDING_INT_SETTINGS
448 *selected = (max - oldvalue) / step;
449 #else
450 *selected = (oldvalue - min) / step;
451 #endif
452 *function = info->option_callback;
455 else if (var_type == F_T_BOOL)
457 *selected = oldvalue;
458 *nb_items = 2;
459 boolfunction = setting->bool_setting->option_callback;
460 if (boolfunction)
461 *function = bool_funcwrapper;
465 bool option_screen(const struct settings_list *setting,
466 struct viewport parent[NB_SCREENS],
467 bool use_temp_var, unsigned char* option_title)
469 int action;
470 bool done = false;
471 struct gui_synclist lists;
472 int oldvalue, nb_items = 0, selected = 0, temp_var;
473 int *variable;
474 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
475 int var_type = setting->flags&F_T_MASK;
476 void (*function)(int) = NULL;
477 char *title;
478 if (var_type == F_T_INT || var_type == F_T_UINT)
480 variable = use_temp_var ? &temp_var: (int*)setting->setting;
481 temp_var = oldvalue = *(int*)setting->setting;
483 else if (var_type == F_T_BOOL)
485 /* bools always use the temp variable...
486 if use_temp_var is false it will be copied to setting->setting every change */
487 variable = &temp_var;
488 temp_var = oldvalue = *(bool*)setting->setting?1:0;
490 else return false; /* only int/bools can go here */
491 gui_synclist_init(&lists, value_setting_get_name_cb,
492 (void*)setting, false, 1, parent);
493 if (setting->lang_id == -1)
494 title = (char*)setting->cfg_vals;
495 else
496 title = P2STR(option_title);
498 gui_synclist_set_title(&lists, title, Icon_Questionmark);
499 gui_synclist_set_icon_callback(&lists, NULL);
500 if(global_settings.talk_menu)
501 gui_synclist_set_voice_callback(&lists, option_talk);
503 val_to_selection(setting, oldvalue, &nb_items, &selected, &function);
504 gui_synclist_set_nb_items(&lists, nb_items);
505 gui_synclist_select_item(&lists, selected);
507 gui_synclist_limit_scroll(&lists, true);
508 gui_synclist_draw(&lists);
509 /* talk the item */
510 gui_synclist_speak_item(&lists);
511 while (!done)
513 if (list_do_action(CONTEXT_LIST, TIMEOUT_BLOCK,
514 &lists, &action,
515 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
517 /* setting changed */
518 selected = gui_synclist_get_sel_pos(&lists);
519 *variable = selection_to_val(setting, selected);
520 if (var_type == F_T_BOOL && !use_temp_var)
521 *(bool*)setting->setting = (*variable==1);
523 else if (action == ACTION_NONE)
524 continue;
525 else if (action == ACTION_STD_CANCEL)
527 /* setting canceled, restore old value if changed */
528 if (*variable != oldvalue)
530 *variable = oldvalue;
531 if (var_type == F_T_BOOL && !use_temp_var)
532 *(bool*)setting->setting = (oldvalue==1);
533 splash(HZ/2, ID2P(LANG_CANCEL));
535 done = true;
537 else if (action == ACTION_STD_CONTEXT)
539 /* reset setting to default */
540 reset_setting(setting, variable);
541 if (var_type == F_T_BOOL && !use_temp_var)
542 *(bool*)setting->setting = (*variable==1);
543 val_to_selection(setting, *variable, &nb_items,
544 &selected, &function);
545 gui_synclist_select_item(&lists, selected);
546 gui_synclist_draw(&lists);
547 gui_synclist_speak_item(&lists);
549 else if (action == ACTION_STD_OK)
551 /* setting accepted, store now if it used a temp var */
552 if (use_temp_var)
554 if (var_type == F_T_INT || var_type == F_T_UINT)
555 *(int*)setting->setting = *variable;
556 else
557 *(bool*)setting->setting = (*variable==1);
559 settings_save();
560 done = true;
562 else if(default_event_handler(action) == SYS_USB_CONNECTED)
563 return true;
564 /* callback */
565 if ( function )
566 function(*variable);
567 /* if the volume is changing we need to let the skins know */
568 if (function == sound_get_fn(SOUND_VOLUME))
569 global_status.last_volume_change = current_tick;
571 return false;