Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / gui / option_select.c
blobfeed1dc2db98e174ba3736ceacdf72ce9d3d61d9
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"
38 #if defined (HAVE_SCROLLWHEEL) || \
39 (CONFIG_KEYPAD == PLAYER_PAD)
40 /* Define this if your target makes sense to have
41 smaller values at the top of the list increasing down the list */
42 #define ASCENDING_INT_SETTINGS
43 #endif
45 static int selection_to_val(const struct settings_list *setting, int selection);
46 int option_value_as_int(const struct settings_list *setting)
48 int type = (setting->flags & F_T_MASK);
49 int temp = 0;
50 if (type == F_T_BOOL)
51 temp = *(bool*)setting->setting?1:0;
52 else if (type == F_T_UINT || type == F_T_INT)
53 temp = *(int*)setting->setting;
54 return temp;
56 static const char *unit_strings[] =
58 [UNIT_INT] = "", [UNIT_MS] = "ms",
59 [UNIT_SEC] = "s", [UNIT_MIN] = "min",
60 [UNIT_HOUR]= "hr", [UNIT_KHZ] = "kHz",
61 [UNIT_DB] = "dB", [UNIT_PERCENT] = "%",
62 [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
63 [UNIT_PER_SEC] = "per sec",
64 [UNIT_HERTZ] = "Hz",
65 [UNIT_MB] = "MB", [UNIT_KBIT] = "kb/s",
66 [UNIT_PM_TICK] = "units/10ms",
68 /* these two vars are needed so arbitrary values can be added to the
69 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
70 static int table_setting_oldval = 0, table_setting_array_position = 0;
71 const char *option_get_valuestring(const struct settings_list *setting,
72 char *buffer, int buf_len,
73 intptr_t temp_var)
75 const char* str = buffer;
76 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
78 bool val = (bool)temp_var;
79 strlcpy(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 const char* (*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 str = 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 = ' ';
115 const char *unit = 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;
127 dec = val % 10;
128 snprintf(buffer, buf_len, "%c%d.%d %s", sign, integer, dec, unit);
130 else
131 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit);
133 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
135 if (setting->flags & F_CHOICETALKS)
137 int setting_id;
138 const struct choice_setting *info = setting->choice_setting;
139 if (info->talks[(int)temp_var] < LANG_LAST_INDEX_IN_ARRAY)
141 strlcpy(buffer, str(info->talks[(int)temp_var]), buf_len);
143 else
145 find_setting(setting->setting, &setting_id);
146 cfg_int_to_string(setting_id, (int)temp_var, buffer, buf_len);
149 else
151 int value = (int)temp_var;
152 char *val = P2STR(setting->choice_setting->desc[value]);
153 strlcpy(buffer, val, buf_len);
156 return str;
158 void option_talk_value(const struct settings_list *setting, int value, bool enqueue)
160 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
162 bool val = (value==1);
163 talk_id(val? setting->bool_setting->lang_yes :
164 setting->bool_setting->lang_no, enqueue);
166 #if 0 /* probably dont need this one */
167 else if ((setting->flags & F_FILENAME) == F_FILENAME)
170 #endif
171 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
172 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
174 const struct int_setting *int_info = setting->int_setting;
175 const struct table_setting *tbl_info = setting->table_setting;
176 int unit;
177 int32_t (*get_talk_id)(int, int);
178 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
180 unit = int_info->unit;
181 get_talk_id = int_info->get_talk_id;
183 else
185 unit = tbl_info->unit;
186 get_talk_id = tbl_info->get_talk_id;
188 if (get_talk_id)
189 talk_id(get_talk_id(value, unit), enqueue);
190 else
191 talk_value(value, unit, enqueue);
193 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
195 int talkunit = UNIT_INT;
196 const char *unit = sound_unit(setting->sound_setting->setting);
197 if (!strcmp(unit, "dB"))
198 talkunit = UNIT_DB;
199 else if (!strcmp(unit, "%"))
200 talkunit = UNIT_PERCENT;
201 else if (!strcmp(unit, "Hz"))
202 talkunit = UNIT_HERTZ;
203 talk_value(value, talkunit, false);
205 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
207 if (setting->flags & F_CHOICETALKS)
209 talk_id(setting->choice_setting->talks[value], enqueue);
211 else
213 talk_id(P2ID(setting->choice_setting->desc[value]), enqueue);
218 static int option_talk(int selected_item, void * data)
220 struct settings_list *setting = (struct settings_list *)data;
221 int temp_var = selection_to_val(setting, selected_item);
222 option_talk_value(setting, temp_var, false);
223 return 0;
226 #if defined(HAVE_QUICKSCREEN) || defined(HAVE_RECORDING)
227 /* only the quickscreen and recording trigger needs this */
228 void option_select_next_val(const struct settings_list *setting,
229 bool previous, bool apply)
231 int val = 0;
232 int *value = setting->setting;
233 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
235 *(bool*)value = !*(bool*)value;
236 if (apply && setting->bool_setting->option_callback)
237 setting->bool_setting->option_callback(*(bool*)value);
238 return;
240 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
242 struct int_setting *info = (struct int_setting *)setting->int_setting;
243 bool neg_step = (info->step < 0);
244 if (!previous)
246 val = *value + info->step;
247 if (neg_step ? (val < info->max) : (val > info->max))
248 val = info->min;
250 else
252 val = *value - info->step;
253 if (neg_step ? (val > info->min) : (val < info->min))
254 val = info->max;
256 *value = val;
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;
278 *value = val;
280 else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
282 struct choice_setting *info = (struct choice_setting *)setting->choice_setting;
283 val = *value + 1;
284 if (!previous)
286 val = *value + 1;
287 if (val >= info->count)
288 val = 0;
290 else
292 val = *value - 1;
293 if (val < 0)
294 val = info->count-1;
296 *value = val;
297 if (apply && info->option_callback)
298 info->option_callback(val);
300 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
302 const struct table_setting *tbl_info = setting->table_setting;
303 int i, add;
304 add = previous?tbl_info->count-1:1;
305 for (i=0; i<tbl_info->count;i++)
307 if ((*value == tbl_info->values[i]) ||
308 (settings->flags&F_ALLOW_ARBITRARY_VALS &&
309 *value < tbl_info->values[i]))
311 val = tbl_info->values[(i+add)%tbl_info->count];
312 break;
315 *value = val;
318 #endif
320 static int selection_to_val(const struct settings_list *setting, int selection)
322 int min = 0, max = 0, step = 1;
323 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
324 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
325 return selection;
326 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
328 const struct table_setting *info = setting->table_setting;
329 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
330 table_setting_array_position != -1 &&
331 (selection >= table_setting_array_position))
333 if (selection == table_setting_array_position)
334 return table_setting_oldval;
335 return info->values[selection-1];
337 else
338 return info->values[selection];
340 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
342 int setting_id = setting->sound_setting->setting;
343 #ifndef ASCENDING_INT_SETTINGS
344 step = sound_steps(setting_id);
345 max = sound_max(setting_id);
346 min = sound_min(setting_id);
347 #else
348 step = -sound_steps(setting_id);
349 min = sound_max(setting_id);
350 max = sound_min(setting_id);
351 #endif
353 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
355 const struct int_setting *info = setting->int_setting;
356 #ifndef ASCENDING_INT_SETTINGS
357 min = info->min;
358 max = info->max;
359 step = info->step;
360 #else
361 max = info->min;
362 min = info->max;
363 step = -info->step;
364 #endif
366 return max- (selection * step);
369 static const char * value_setting_get_name_cb(int selected_item,
370 void * data,
371 char *buffer,
372 size_t buffer_len)
374 selected_item = selection_to_val(data, selected_item);
375 return option_get_valuestring(data, buffer, buffer_len, selected_item);
378 /* wrapper to convert from int param to bool param in option_screen */
379 static void (*boolfunction)(bool);
380 static void bool_funcwrapper(int value)
382 if (value)
383 boolfunction(true);
384 else
385 boolfunction(false);
388 static void val_to_selection(const struct settings_list *setting, int oldvalue,
389 int *nb_items, int *selected,
390 void (**function)(int))
392 int var_type = setting->flags&F_T_MASK;
393 /* set the number of items and current selection */
394 if (var_type == F_T_INT || var_type == F_T_UINT)
396 if (setting->flags&F_CHOICE_SETTING)
398 *nb_items = setting->choice_setting->count;
399 *selected = oldvalue;
400 *function = setting->choice_setting->option_callback;
402 else if (setting->flags&F_TABLE_SETTING)
404 const struct table_setting *info = setting->table_setting;
405 int i;
406 *nb_items = info->count;
407 *selected = -1;
408 table_setting_array_position = -1;
409 for (i=0;*selected==-1 && i<*nb_items;i++)
411 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
412 (oldvalue < info->values[i]))
414 table_setting_oldval = oldvalue;
415 table_setting_array_position = i;
416 *selected = i;
417 (*nb_items)++;
419 else if (oldvalue == info->values[i])
420 *selected = i;
422 *function = info->option_callback;
424 else if (setting->flags&F_T_SOUND)
426 int setting_id = setting->sound_setting->setting;
427 int steps = sound_steps(setting_id);
428 int min = sound_min(setting_id);
429 int max = sound_max(setting_id);
430 *nb_items = (max-min)/steps + 1;
431 #ifndef ASCENDING_INT_SETTINGS
432 *selected = (max - oldvalue) / steps;
433 #else
434 *selected = (oldvalue - min) / steps;
435 #endif
436 *function = sound_get_fn(setting_id);
438 else
440 const struct int_setting *info = setting->int_setting;
441 int min, max, step;
442 max = info->max;
443 min = info->min;
444 step = info->step;
445 *nb_items = (max-min)/step + 1;
446 #ifndef ASCENDING_INT_SETTINGS
447 *selected = (max - oldvalue) / step;
448 #else
449 *selected = (oldvalue - min) / step;
450 #endif
451 *function = info->option_callback;
454 else if (var_type == F_T_BOOL)
456 *selected = oldvalue;
457 *nb_items = 2;
458 boolfunction = setting->bool_setting->option_callback;
459 if (boolfunction)
460 *function = bool_funcwrapper;
464 bool option_screen(const struct settings_list *setting,
465 struct viewport parent[NB_SCREENS],
466 bool use_temp_var, unsigned char* option_title)
468 int action;
469 bool done = false;
470 struct gui_synclist lists;
471 int oldvalue, nb_items = 0, selected = 0, temp_var;
472 int *variable;
473 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
474 int var_type = setting->flags&F_T_MASK;
475 void (*function)(int) = NULL;
476 char *title;
477 if (var_type == F_T_INT || var_type == F_T_UINT)
479 variable = use_temp_var ? &temp_var: (int*)setting->setting;
480 temp_var = oldvalue = *(int*)setting->setting;
482 else if (var_type == F_T_BOOL)
484 /* bools always use the temp variable...
485 if use_temp_var is false it will be copied to setting->setting every change */
486 variable = &temp_var;
487 temp_var = oldvalue = *(bool*)setting->setting?1:0;
489 else return false; /* only int/bools can go here */
490 gui_synclist_init(&lists, value_setting_get_name_cb,
491 (void*)setting, false, 1, parent);
492 if (setting->lang_id == -1)
493 title = (char*)setting->cfg_vals;
494 else
495 title = P2STR(option_title);
497 gui_synclist_set_title(&lists, title, Icon_Questionmark);
498 gui_synclist_set_icon_callback(&lists, NULL);
499 if(global_settings.talk_menu)
500 gui_synclist_set_voice_callback(&lists, option_talk);
502 val_to_selection(setting, oldvalue, &nb_items, &selected, &function);
503 gui_synclist_set_nb_items(&lists, nb_items);
504 gui_synclist_select_item(&lists, selected);
506 gui_synclist_limit_scroll(&lists, true);
507 gui_synclist_draw(&lists);
508 /* talk the item */
509 gui_synclist_speak_item(&lists);
510 while (!done)
512 if (list_do_action(CONTEXT_LIST, HZ, /* HZ so the status bar redraws */
513 &lists, &action,
514 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
516 /* setting changed */
517 selected = gui_synclist_get_sel_pos(&lists);
518 *variable = selection_to_val(setting, selected);
519 if (var_type == F_T_BOOL && !use_temp_var)
520 *(bool*)setting->setting = (*variable==1);
522 else if (action == ACTION_NONE)
523 continue;
524 else if (action == ACTION_STD_CANCEL)
526 /* setting canceled, restore old value if changed */
527 if (*variable != oldvalue)
529 *variable = oldvalue;
530 if (var_type == F_T_BOOL && !use_temp_var)
531 *(bool*)setting->setting = (oldvalue==1);
532 splash(HZ/2, ID2P(LANG_CANCEL));
534 done = true;
536 else if (action == ACTION_STD_CONTEXT)
538 /* reset setting to default */
539 reset_setting(setting, variable);
540 if (var_type == F_T_BOOL && !use_temp_var)
541 *(bool*)setting->setting = (*variable==1);
542 val_to_selection(setting, *variable, &nb_items,
543 &selected, &function);
544 gui_synclist_select_item(&lists, selected);
545 gui_synclist_draw(&lists);
546 gui_synclist_speak_item(&lists);
548 else if (action == ACTION_STD_OK)
550 /* setting accepted, store now if it used a temp var */
551 if (use_temp_var)
553 if (var_type == F_T_INT || var_type == F_T_UINT)
554 *(int*)setting->setting = *variable;
555 else
556 *(bool*)setting->setting = (*variable==1);
558 settings_save();
559 done = true;
561 else if(default_event_handler(action) == SYS_USB_CONNECTED)
562 return true;
563 /* callback */
564 if ( function )
565 function(*variable);
566 /* if the volume is changing we need to let the skins know */
567 if (function == sound_get_fn(SOUND_VOLUME))
568 global_status.last_volume_change = current_tick;
570 return false;