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 ****************************************************************************/
25 #include "option_select.h"
30 #include "settings_list.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
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
);
52 temp
= *(bool*)setting
->setting
?1:0;
53 else if (type
== F_T_UINT
|| type
== F_T_INT
)
54 temp
= *(int*)setting
->setting
;
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",
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
,
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
);
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 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
];
104 formatter
= tbl_info
->formatter
;
105 unit
= unit_strings
[tbl_info
->unit
];
108 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
)
114 char sign
= ' ', *unit
;
115 unit
= (char*)sound_unit(setting
->sound_setting
->setting
);
116 if (sound_numdecimals(setting
->sound_setting
->setting
))
119 int val
= sound_val2phys(setting
->sound_setting
->setting
,
126 integer
= val
/ 10; dec
= val
% 10;
127 snprintf(buffer
, buf_len
, "%c%d.%d %s", sign
, integer
, dec
, unit
);
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
)
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
);
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 strncpy(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 const char *unit
= sound_unit(setting
->sound_setting
->setting
);
196 if (!strcmp(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
);
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);
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
)
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
);
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
;
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
);
268 val
= *value
+ steps
;
274 val
= *value
- steps
;
279 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
281 struct choice_setting
*info
= (struct choice_setting
*)setting
->choice_setting
;
286 if (val
>= info
->count
)
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
;
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
];
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
))
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];
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
);
346 step
= -sound_steps(setting_id
);
347 min
= sound_max(setting_id
);
348 max
= sound_min(setting_id
);
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
364 return max
- (selection
* step
);
366 static char * value_setting_get_name_cb(int selected_item
,
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
)
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
;
403 *nb_items
= info
->count
;
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
;
416 else if (oldvalue
== info
->values
[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
;
431 *selected
= (oldvalue
- min
) / steps
;
433 *function
= sound_get_fn(setting_id
);
437 const struct int_setting
*info
= setting
->int_setting
;
442 *nb_items
= (max
-min
)/step
+ 1;
443 #ifndef ASCENDING_INT_SETTINGS
444 *selected
= (max
- oldvalue
) / step
;
446 *selected
= (oldvalue
- min
) / step
;
448 *function
= info
->option_callback
;
451 else if (var_type
== F_T_BOOL
)
453 *selected
= oldvalue
;
455 boolfunction
= setting
->bool_setting
->option_callback
;
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
)
467 struct gui_synclist lists
;
468 int oldvalue
, nb_items
= 0, selected
= 0, temp_var
;
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
;
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
;
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
);
506 gui_synclist_speak_item(&lists
);
509 if (list_do_action(CONTEXT_LIST
, TIMEOUT_BLOCK
,
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
)
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
));
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 */
550 if (var_type
== F_T_INT
|| var_type
== F_T_UINT
)
551 *(int*)setting
->setting
= *variable
;
553 *(bool*)setting
->setting
= (*variable
==1);
558 else if(default_event_handler(action
) == SYS_USB_CONNECTED
)