FS#10365 - Optional debug output for albumart.c
[kugel-rb.git] / apps / gui / option_select.c
blob7b6c489bf3239bf29dd63282d9836367a1370690
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 char *option_get_valuestring(const struct settings_list *setting,
73 char *buffer, int buf_len,
74 intptr_t temp_var)
76 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
78 bool val = (bool)temp_var;
79 strncpy(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 void (*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 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 = ' ', *unit;
115 unit = (char*)sound_unit(setting->sound_setting->setting);
116 if (sound_numdecimals(setting->sound_setting->setting))
118 int integer, dec;
119 int val = sound_val2phys(setting->sound_setting->setting,
120 (int)temp_var);
121 if(val < 0)
123 sign = '-';
124 val = abs(val);
126 integer = val / 10; 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", (int)temp_var, 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 strncpy(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 strncpy(buffer, val, buf_len);
155 return buffer;
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 const char *unit = sound_unit(setting->sound_setting->setting);
196 if (!strcmp(unit, "dB"))
197 talkunit = UNIT_DB;
198 else if (!strcmp(unit, "%"))
199 talkunit = UNIT_PERCENT;
200 else if (!strcmp(unit, "Hz"))
201 talkunit = UNIT_HERTZ;
202 talk_value(value, talkunit, false);
204 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
206 if (setting->flags & F_CHOICETALKS)
208 talk_id(setting->choice_setting->talks[value], enqueue);
210 else
212 talk_id(P2ID(setting->choice_setting->desc[value]), enqueue);
217 static int option_talk(int selected_item, void * data)
219 struct settings_list *setting = (struct settings_list *)data;
220 int temp_var = selection_to_val(setting, selected_item);
221 option_talk_value(setting, temp_var, false);
222 return 0;
225 #if defined(HAVE_QUICKSCREEN) || defined(HAVE_RECORDING)
226 /* only the quickscreen and recording trigger needs this */
227 void option_select_next_val(const struct settings_list *setting,
228 bool previous, bool apply)
230 int val = 0;
231 int *value = setting->setting;
232 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
234 *(bool*)value = !*(bool*)value;
235 if (apply && setting->bool_setting->option_callback)
236 setting->bool_setting->option_callback(*(bool*)value);
237 return;
239 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
241 struct int_setting *info = (struct int_setting *)setting->int_setting;
242 int step = info->step;
243 if (step < 0)
244 step = -step;
245 if (!previous)
247 val = *value + step;
248 if (val > info->max)
249 val = info->min;
251 else
253 val = *value - step;
254 if (val < info->min)
255 val = info->max;
257 if (apply && info->option_callback)
258 info->option_callback(val);
260 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
262 int setting_id = setting->sound_setting->setting;
263 int steps = sound_steps(setting_id);
264 int min = sound_min(setting_id);
265 int max = sound_max(setting_id);
266 if (!previous)
268 val = *value + steps;
269 if (val >= max)
270 val = min;
272 else
274 val = *value - steps;
275 if (val < min)
276 val = max;
279 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
281 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
282 val = *value + 1;
283 if (!previous)
285 val = *value + 1;
286 if (val >= info->count)
287 val = 0;
289 else
291 val = *value - 1;
292 if (val < 0)
293 val = info->count-1;
295 if (apply && info->option_callback)
296 info->option_callback(val);
298 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
300 const struct table_setting *tbl_info = setting->table_setting;
301 int i, add;
302 add = previous?tbl_info->count-1:1;
303 for (i=0; i<tbl_info->count;i++)
305 if ((*value == tbl_info->values[i]) ||
306 (settings->flags&F_ALLOW_ARBITRARY_VALS &&
307 *value < tbl_info->values[i]))
309 val = tbl_info->values[(i+add)%tbl_info->count];
310 break;
314 *value = val;
316 #endif
318 static int selection_to_val(const struct settings_list *setting, int selection)
320 int min = 0, max = 0, step = 1;
321 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
322 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
323 return selection;
324 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
326 const struct table_setting *info = setting->table_setting;
327 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
328 table_setting_array_position != -1 &&
329 (selection >= table_setting_array_position))
331 if (selection == table_setting_array_position)
332 return table_setting_oldval;
333 return info->values[selection-1];
335 else
336 return info->values[selection];
338 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
340 int setting_id = setting->sound_setting->setting;
341 #ifndef ASCENDING_INT_SETTINGS
342 step = sound_steps(setting_id);
343 max = sound_max(setting_id);
344 min = sound_min(setting_id);
345 #else
346 step = -sound_steps(setting_id);
347 min = sound_max(setting_id);
348 max = sound_min(setting_id);
349 #endif
351 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
353 const struct int_setting *info = setting->int_setting;
354 #ifndef ASCENDING_INT_SETTINGS
355 min = info->min;
356 max = info->max;
357 step = info->step;
358 #else
359 max = info->min;
360 min = info->max;
361 step = -info->step;
362 #endif
364 return max- (selection * step);
366 static char * value_setting_get_name_cb(int selected_item,
367 void * data,
368 char *buffer,
369 size_t buffer_len)
371 selected_item = selection_to_val(data, selected_item);
372 return option_get_valuestring(data, buffer, buffer_len, selected_item);
375 /* wrapper to convert from int param to bool param in option_screen */
376 static void (*boolfunction)(bool);
377 static void bool_funcwrapper(int value)
379 if (value)
380 boolfunction(true);
381 else
382 boolfunction(false);
385 static void val_to_selection(const struct settings_list *setting, int oldvalue,
386 int *nb_items, int *selected,
387 void (**function)(int))
389 int var_type = setting->flags&F_T_MASK;
390 /* set the number of items and current selection */
391 if (var_type == F_T_INT || var_type == F_T_UINT)
393 if (setting->flags&F_CHOICE_SETTING)
395 *nb_items = setting->choice_setting->count;
396 *selected = oldvalue;
397 *function = setting->choice_setting->option_callback;
399 else if (setting->flags&F_TABLE_SETTING)
401 const struct table_setting *info = setting->table_setting;
402 int i;
403 *nb_items = info->count;
404 *selected = -1;
405 table_setting_array_position = -1;
406 for (i=0;*selected==-1 && i<*nb_items;i++)
408 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
409 (oldvalue < info->values[i]))
411 table_setting_oldval = oldvalue;
412 table_setting_array_position = i;
413 *selected = i;
414 (*nb_items)++;
416 else if (oldvalue == info->values[i])
417 *selected = i;
419 *function = info->option_callback;
421 else if (setting->flags&F_T_SOUND)
423 int setting_id = setting->sound_setting->setting;
424 int steps = sound_steps(setting_id);
425 int min = sound_min(setting_id);
426 int max = sound_max(setting_id);
427 *nb_items = (max-min)/steps + 1;
428 #ifndef ASCENDING_INT_SETTINGS
429 *selected = (max - oldvalue) / steps;
430 #else
431 *selected = (oldvalue - min) / steps;
432 #endif
433 *function = sound_get_fn(setting_id);
435 else
437 const struct int_setting *info = setting->int_setting;
438 int min, max, step;
439 max = info->max;
440 min = info->min;
441 step = info->step;
442 *nb_items = (max-min)/step + 1;
443 #ifndef ASCENDING_INT_SETTINGS
444 *selected = (max - oldvalue) / step;
445 #else
446 *selected = (oldvalue - min) / step;
447 #endif
448 *function = info->option_callback;
451 else if (var_type == F_T_BOOL)
453 *selected = oldvalue;
454 *nb_items = 2;
455 boolfunction = setting->bool_setting->option_callback;
456 if (boolfunction)
457 *function = bool_funcwrapper;
461 bool option_screen(const struct settings_list *setting,
462 struct viewport parent[NB_SCREENS],
463 bool use_temp_var, unsigned char* option_title)
465 int action;
466 bool done = false;
467 struct gui_synclist lists;
468 int oldvalue, nb_items = 0, selected = 0, temp_var;
469 int *variable;
470 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
471 int var_type = setting->flags&F_T_MASK;
472 void (*function)(int) = NULL;
473 char *title;
474 if (var_type == F_T_INT || var_type == F_T_UINT)
476 variable = use_temp_var ? &temp_var: (int*)setting->setting;
477 temp_var = oldvalue = *(int*)setting->setting;
479 else if (var_type == F_T_BOOL)
481 /* bools always use the temp variable...
482 if use_temp_var is false it will be copied to setting->setting every change */
483 variable = &temp_var;
484 temp_var = oldvalue = *(bool*)setting->setting?1:0;
486 else return false; /* only int/bools can go here */
487 gui_synclist_init(&lists, value_setting_get_name_cb,
488 (void*)setting, false, 1, parent);
489 if (setting->lang_id == -1)
490 title = (char*)setting->cfg_vals;
491 else
492 title = P2STR(option_title);
494 gui_synclist_set_title(&lists, title, Icon_Questionmark);
495 gui_synclist_set_icon_callback(&lists, NULL);
496 if(global_settings.talk_menu)
497 gui_synclist_set_voice_callback(&lists, option_talk);
499 val_to_selection(setting, oldvalue, &nb_items, &selected, &function);
500 gui_synclist_set_nb_items(&lists, nb_items);
501 gui_synclist_select_item(&lists, selected);
503 gui_synclist_limit_scroll(&lists, true);
504 gui_synclist_draw(&lists);
505 /* talk the item */
506 gui_synclist_speak_item(&lists);
507 while (!done)
509 if (list_do_action(CONTEXT_LIST, TIMEOUT_BLOCK,
510 &lists, &action,
511 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
513 /* setting changed */
514 selected = gui_synclist_get_sel_pos(&lists);
515 *variable = selection_to_val(setting, selected);
516 if (var_type == F_T_BOOL && !use_temp_var)
517 *(bool*)setting->setting = (*variable==1);
519 else if (action == ACTION_NONE)
520 continue;
521 else if (action == ACTION_STD_CANCEL)
523 /* setting canceled, restore old value if changed */
524 if (*variable != oldvalue)
526 *variable = oldvalue;
527 if (var_type == F_T_BOOL && !use_temp_var)
528 *(bool*)setting->setting = (oldvalue==1);
529 splash(HZ/2, ID2P(LANG_CANCEL));
531 done = true;
533 else if (action == ACTION_STD_CONTEXT)
535 /* reset setting to default */
536 reset_setting(setting, variable);
537 if (var_type == F_T_BOOL && !use_temp_var)
538 *(bool*)setting->setting = (*variable==1);
539 val_to_selection(setting, *variable, &nb_items,
540 &selected, &function);
541 gui_synclist_select_item(&lists, selected);
542 gui_synclist_draw(&lists);
543 gui_synclist_speak_item(&lists);
545 else if (action == ACTION_STD_OK)
547 /* setting accepted, store now if it used a temp var */
548 if (use_temp_var)
550 if (var_type == F_T_INT || var_type == F_T_UINT)
551 *(int*)setting->setting = *variable;
552 else
553 *(bool*)setting->setting = (*variable==1);
555 settings_save();
556 done = true;
558 else if(default_event_handler(action) == SYS_USB_CONNECTED)
559 return true;
560 /* callback */
561 if ( function )
562 function(*variable);
564 return false;