1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
23 #include "string-extra.h"
25 #include "option_select.h"
29 #include "settings_list.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
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
);
51 temp
= *(bool*)setting
->setting
?1:0;
52 else if (type
== F_T_UINT
|| type
== F_T_INT
)
53 temp
= *(int*)setting
->setting
;
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",
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
,
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
);
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
;
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
];
104 formatter
= tbl_info
->formatter
;
105 unit
= unit_strings
[tbl_info
->unit
];
108 str
= formatter(buffer
, buf_len
, (int)temp_var
, unit
);
110 snprintf(buffer
, buf_len
, "%d %s", (int)temp_var
, unit
?unit
:"");
112 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
115 const char *unit
= sound_unit(setting
->sound_setting
->setting
);
116 int val
= sound_val2phys(setting
->sound_setting
->setting
, (int)temp_var
);
117 if (sound_numdecimals(setting
->sound_setting
->setting
))
127 snprintf(buffer
, buf_len
, "%c%d.%d %s", sign
, integer
, dec
, unit
);
130 snprintf(buffer
, buf_len
, "%d %s", val
, unit
);
132 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
134 if (setting
->flags
& F_CHOICETALKS
)
137 const struct choice_setting
*info
= setting
->choice_setting
;
138 if (info
->talks
[(int)temp_var
] < LANG_LAST_INDEX_IN_ARRAY
)
140 strlcpy(buffer
, str(info
->talks
[(int)temp_var
]), buf_len
);
144 find_setting(setting
->setting
, &setting_id
);
145 cfg_int_to_string(setting_id
, (int)temp_var
, buffer
, buf_len
);
150 int value
= (int)temp_var
;
151 char *val
= P2STR(setting
->choice_setting
->desc
[value
]);
152 strlcpy(buffer
, val
, buf_len
);
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
)
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
;
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
;
184 unit
= tbl_info
->unit
;
185 get_talk_id
= tbl_info
->get_talk_id
;
188 talk_id(get_talk_id(value
, unit
), enqueue
);
190 talk_value(value
, unit
, enqueue
);
192 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
194 int talkunit
= UNIT_INT
;
195 int sound_setting
= setting
->sound_setting
->setting
;
196 const char *unit
= sound_unit(sound_setting
);
197 int decimals
= sound_numdecimals(sound_setting
);
198 int phys
= sound_val2phys(sound_setting
, value
);
199 if (!strcmp(unit
, "dB"))
201 else if (!strcmp(unit
, "%"))
202 talkunit
= UNIT_PERCENT
;
203 else if (!strcmp(unit
, "Hz"))
204 talkunit
= UNIT_HERTZ
;
205 talk_value_decimal(phys
, talkunit
, decimals
, false);
207 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
209 if (setting
->flags
& F_CHOICETALKS
)
211 talk_id(setting
->choice_setting
->talks
[value
], enqueue
);
215 talk_id(P2ID(setting
->choice_setting
->desc
[value
]), enqueue
);
220 static int option_talk(int selected_item
, void * data
)
222 struct settings_list
*setting
= (struct settings_list
*)data
;
223 int temp_var
= selection_to_val(setting
, selected_item
);
224 option_talk_value(setting
, temp_var
, false);
228 #if defined(HAVE_QUICKSCREEN) || defined(HAVE_RECORDING) || defined(HAVE_TOUCHSCREEN)
229 /* only the quickscreen and recording trigger needs this */
230 void option_select_next_val(const struct settings_list
*setting
,
231 bool previous
, bool apply
)
234 int *value
= setting
->setting
;
235 if ((setting
->flags
& F_BOOL_SETTING
) == F_BOOL_SETTING
)
237 *(bool*)value
= !*(bool*)value
;
238 if (apply
&& setting
->bool_setting
->option_callback
)
239 setting
->bool_setting
->option_callback(*(bool*)value
);
242 else if ((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
)
244 struct int_setting
*info
= (struct int_setting
*)setting
->int_setting
;
245 bool neg_step
= (info
->step
< 0);
248 val
= *value
+ info
->step
;
249 if (neg_step
? (val
< info
->max
) : (val
> info
->max
))
254 val
= *value
- info
->step
;
255 if (neg_step
? (val
> info
->min
) : (val
< info
->min
))
259 if (apply
&& info
->option_callback
)
260 info
->option_callback(val
);
262 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
264 int setting_id
= setting
->sound_setting
->setting
;
265 int steps
= sound_steps(setting_id
);
266 int min
= sound_min(setting_id
);
267 int max
= sound_max(setting_id
);
270 val
= *value
+ steps
;
276 val
= *value
- steps
;
282 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
284 struct choice_setting
*info
= (struct choice_setting
*)setting
->choice_setting
;
289 if (val
>= info
->count
)
299 if (apply
&& info
->option_callback
)
300 info
->option_callback(val
);
302 else if ((setting
->flags
& F_TABLE_SETTING
) == F_TABLE_SETTING
)
304 const struct table_setting
*tbl_info
= setting
->table_setting
;
306 add
= previous
?tbl_info
->count
-1:1;
307 for (i
=0; i
<tbl_info
->count
;i
++)
309 if ((*value
== tbl_info
->values
[i
]) ||
310 (settings
->flags
&F_ALLOW_ARBITRARY_VALS
&&
311 *value
< tbl_info
->values
[i
]))
313 val
= tbl_info
->values
[(i
+add
)%tbl_info
->count
];
322 static int selection_to_val(const struct settings_list
*setting
, int selection
)
324 int min
= 0, max
= 0, step
= 1;
325 if (((setting
->flags
& F_BOOL_SETTING
) == F_BOOL_SETTING
) ||
326 ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
))
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];
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
);
350 step
= -sound_steps(setting_id
);
351 min
= sound_max(setting_id
);
352 max
= sound_min(setting_id
);
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
368 return max
- (selection
* step
);
371 static const char * value_setting_get_name_cb(int selected_item
,
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
)
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
;
408 *nb_items
= info
->count
;
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
;
421 else if (oldvalue
== info
->values
[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
;
436 *selected
= (oldvalue
- min
) / steps
;
438 *function
= sound_get_fn(setting_id
);
442 const struct int_setting
*info
= setting
->int_setting
;
447 *nb_items
= (max
-min
)/step
+ 1;
448 #ifndef ASCENDING_INT_SETTINGS
449 *selected
= (max
- oldvalue
) / step
;
451 *selected
= (oldvalue
- min
) / step
;
453 *function
= info
->option_callback
;
456 else if (var_type
== F_T_BOOL
)
458 *selected
= oldvalue
;
460 boolfunction
= setting
->bool_setting
->option_callback
;
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
)
472 struct gui_synclist lists
;
473 int oldvalue
, nb_items
= 0, selected
= 0, temp_var
;
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
;
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 gui_synclist_init(&lists
, value_setting_get_name_cb
,
493 (void*)setting
, false, 1, parent
);
494 if (setting
->lang_id
== -1)
495 title
= (char*)setting
->cfg_vals
;
497 title
= P2STR(option_title
);
499 gui_synclist_set_title(&lists
, title
, Icon_Questionmark
);
500 gui_synclist_set_icon_callback(&lists
, NULL
);
501 if(global_settings
.talk_menu
)
502 gui_synclist_set_voice_callback(&lists
, option_talk
);
504 val_to_selection(setting
, oldvalue
, &nb_items
, &selected
, &function
);
505 gui_synclist_set_nb_items(&lists
, nb_items
);
506 gui_synclist_select_item(&lists
, selected
);
508 gui_synclist_limit_scroll(&lists
, true);
509 gui_synclist_draw(&lists
);
511 gui_synclist_speak_item(&lists
);
514 if (list_do_action(CONTEXT_LIST
, HZ
, /* HZ so the status bar redraws */
516 allow_wrap
? LIST_WRAP_UNLESS_HELD
: LIST_WRAP_OFF
))
518 /* setting changed */
519 selected
= gui_synclist_get_sel_pos(&lists
);
520 *variable
= selection_to_val(setting
, selected
);
521 if (var_type
== F_T_BOOL
&& !use_temp_var
)
522 *(bool*)setting
->setting
= (*variable
==1);
524 else if (action
== ACTION_NONE
)
526 else if (action
== ACTION_STD_CANCEL
)
528 /* setting canceled, restore old value if changed */
529 if (*variable
!= oldvalue
)
531 *variable
= oldvalue
;
532 if (var_type
== F_T_BOOL
&& !use_temp_var
)
533 *(bool*)setting
->setting
= (oldvalue
==1);
534 splash(HZ
/2, ID2P(LANG_CANCEL
));
538 else if (action
== ACTION_STD_CONTEXT
)
540 /* reset setting to default */
541 reset_setting(setting
, variable
);
542 if (var_type
== F_T_BOOL
&& !use_temp_var
)
543 *(bool*)setting
->setting
= (*variable
==1);
544 val_to_selection(setting
, *variable
, &nb_items
,
545 &selected
, &function
);
546 gui_synclist_select_item(&lists
, selected
);
547 gui_synclist_draw(&lists
);
548 gui_synclist_speak_item(&lists
);
550 else if (action
== ACTION_STD_OK
)
552 /* setting accepted, store now if it used a temp var */
555 if (var_type
== F_T_INT
|| var_type
== F_T_UINT
)
556 *(int*)setting
->setting
= *variable
;
558 *(bool*)setting
->setting
= (*variable
==1);
563 else if(default_event_handler(action
) == SYS_USB_CONNECTED
)
568 /* if the volume is changing we need to let the skins know */
569 if (function
== sound_get_fn(SOUND_VOLUME
))
570 global_status
.last_volume_change
= current_tick
;