1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Kevin Ferrare
11 * Copyright (C) 2007 by Jonathan Gordon
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
23 #include "option_select.h"
28 #include "settings_list.h"
32 #include "statusbar.h"
36 #if defined (HAVE_SCROLLWHEEL) || \
37 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
38 (CONFIG_KEYPAD == IPOD_4G_PAD) || \
39 (CONFIG_KEYPAD == IPOD_1G2G_PAD) || \
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(struct settings_list
*setting
, int selection
);
48 static const char *unit_strings
[] =
50 [UNIT_INT
] = "", [UNIT_MS
] = "ms",
51 [UNIT_SEC
] = "s", [UNIT_MIN
] = "min",
52 [UNIT_HOUR
]= "hr", [UNIT_KHZ
] = "kHz",
53 [UNIT_DB
] = "dB", [UNIT_PERCENT
] = "%",
54 [UNIT_MAH
] = "mAh", [UNIT_PIXEL
] = "px",
55 [UNIT_PER_SEC
] = "per sec",
57 [UNIT_MB
] = "MB", [UNIT_KBIT
] = "kb/s",
58 [UNIT_PM_TICK
] = "units/10ms",
60 /* these two vars are needed so arbitrary values can be added to the
61 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
62 static int table_setting_oldval
= 0, table_setting_array_position
= 0;
63 char *option_get_valuestring(struct settings_list
*setting
,
64 char *buffer
, int buf_len
,
67 if ((setting
->flags
& F_BOOL_SETTING
) == F_BOOL_SETTING
)
69 bool val
= (bool)temp_var
;
70 snprintf(buffer
, buf_len
, "%s",
71 str(val
? setting
->bool_setting
->lang_yes
:
72 setting
->bool_setting
->lang_no
));
74 #if 0 /* probably dont need this one */
75 else if ((setting
->flags
& F_FILENAME
) == F_FILENAME
)
77 struct filename_setting
*info
= setting
->filename_setting
;
78 snprintf(buffer
, buf_len
, "%s%s%s", info
->prefix
,
79 (char*)temp_var
, info
->suffix
);
82 else if (((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
) ||
83 ((setting
->flags
& F_TABLE_SETTING
) == F_TABLE_SETTING
))
85 const struct int_setting
*int_info
= setting
->int_setting
;
86 const struct table_setting
*tbl_info
= setting
->table_setting
;
88 void (*formatter
)(char*, size_t, int, const char*);
89 if ((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
)
91 formatter
= int_info
->formatter
;
92 unit
= unit_strings
[int_info
->unit
];
96 formatter
= tbl_info
->formatter
;
97 unit
= unit_strings
[tbl_info
->unit
];
100 formatter(buffer
, buf_len
, (int)temp_var
, unit
);
102 snprintf(buffer
, buf_len
, "%d %s", (int)temp_var
, unit
?unit
:"");
104 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
106 char sign
= ' ', *unit
;
107 unit
= (char*)sound_unit(setting
->sound_setting
->setting
);
108 if (sound_numdecimals(setting
->sound_setting
->setting
))
111 int val
= sound_val2phys(setting
->sound_setting
->setting
,
118 integer
= val
/ 10; dec
= val
% 10;
119 snprintf(buffer
, buf_len
, "%c%d.%d %s", sign
, integer
, dec
, unit
);
122 snprintf(buffer
, buf_len
, "%d %s", (int)temp_var
, unit
);
124 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
126 if (setting
->flags
& F_CHOICETALKS
)
129 const struct choice_setting
*info
= setting
->choice_setting
;
130 if (info
->talks
[(int)temp_var
] < LANG_LAST_INDEX_IN_ARRAY
)
132 snprintf(buffer
, buf_len
, "%s", str(info
->talks
[(int)temp_var
]));
136 find_setting(setting
->setting
, &setting_id
);
137 cfg_int_to_string(setting_id
, (int)temp_var
, buffer
, buf_len
);
142 int value
= (int)temp_var
;
143 char *val
= P2STR(setting
->choice_setting
->desc
[value
]);
144 snprintf(buffer
, buf_len
, "%s", val
);
150 static int option_talk(int selected_item
, void * data
)
152 struct settings_list
*setting
= (struct settings_list
*)data
;
153 int temp_var
= selection_to_val(setting
, selected_item
);
154 if ((setting
->flags
& F_BOOL_SETTING
) == F_BOOL_SETTING
)
156 bool val
= temp_var
==1?true:false;
157 talk_id(val
? setting
->bool_setting
->lang_yes
:
158 setting
->bool_setting
->lang_no
, false);
160 #if 0 /* probably dont need this one */
161 else if ((setting
->flags
& F_FILENAME
) == F_FILENAME
)
165 else if (((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
) ||
166 ((setting
->flags
& F_TABLE_SETTING
) == F_TABLE_SETTING
))
168 const struct int_setting
*int_info
= setting
->int_setting
;
169 const struct table_setting
*tbl_info
= setting
->table_setting
;
171 int32_t (*get_talk_id
)(int, int);
172 if ((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
)
174 unit
= int_info
->unit
;
175 get_talk_id
= int_info
->get_talk_id
;
179 unit
= tbl_info
->unit
;
180 get_talk_id
= tbl_info
->get_talk_id
;
183 talk_id(get_talk_id((int)temp_var
, unit
), false);
185 talk_value((int)temp_var
, unit
, false);
187 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
189 int talkunit
= UNIT_INT
;
190 const char *unit
= sound_unit(setting
->sound_setting
->setting
);
191 if (!strcmp(unit
, "dB"))
193 else if (!strcmp(unit
, "%"))
194 talkunit
= UNIT_PERCENT
;
195 else if (!strcmp(unit
, "Hz"))
196 talkunit
= UNIT_HERTZ
;
197 talk_value((int)temp_var
, talkunit
, false);
199 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
201 int value
= (int)temp_var
;
202 if (setting
->flags
& F_CHOICETALKS
)
204 talk_id(setting
->choice_setting
->talks
[value
], false);
208 talk_id(P2ID(setting
->choice_setting
->desc
[value
]), false);
214 #ifdef HAVE_QUICKSCREEN /* only the quickscreen uses this so far */
215 void option_select_next_val(struct settings_list
*setting
)
218 int *value
= setting
->setting
;
219 if ((setting
->flags
& F_BOOL_SETTING
) == F_BOOL_SETTING
)
221 *(bool*)value
= !*(bool*)value
;
224 else if ((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
)
226 struct int_setting
*info
= (struct int_setting
*)setting
->int_setting
;
227 val
= *value
+ info
->step
;
231 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
233 int setting_id
= setting
->sound_setting
->setting
;
234 int steps
= sound_steps(setting_id
);
235 int min
= sound_min(setting_id
);
236 int max
= sound_max(setting_id
);
237 val
= *value
+ steps
;
241 else if ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
)
243 struct choice_setting
*info
= (struct choice_setting
*)setting
->choice_setting
;
245 if (val
>= info
->count
)
252 static int selection_to_val(struct settings_list
*setting
, int selection
)
254 int min
= 0, max
= 0, step
= 1;
255 if (((setting
->flags
& F_BOOL_SETTING
) == F_BOOL_SETTING
) ||
256 ((setting
->flags
& F_CHOICE_SETTING
) == F_CHOICE_SETTING
))
258 else if ((setting
->flags
& F_TABLE_SETTING
) == F_TABLE_SETTING
)
260 const struct table_setting
*info
= setting
->table_setting
;
261 if (setting
->flags
&F_ALLOW_ARBITRARY_VALS
&&
262 table_setting_array_position
!= -1 &&
263 (selection
>= table_setting_array_position
))
265 if (selection
== table_setting_array_position
)
266 return table_setting_oldval
;
267 return info
->values
[selection
-1];
270 return info
->values
[selection
];
272 else if ((setting
->flags
& F_T_SOUND
) == F_T_SOUND
)
274 int setting_id
= setting
->sound_setting
->setting
;
275 #ifndef ASCENDING_INT_SETTINGS
276 step
= sound_steps(setting_id
);
277 max
= sound_max(setting_id
);
278 min
= sound_min(setting_id
);
280 step
= -sound_steps(setting_id
);
281 min
= sound_max(setting_id
);
282 max
= sound_min(setting_id
);
285 else if ((setting
->flags
& F_INT_SETTING
) == F_INT_SETTING
)
287 const struct int_setting
*info
= setting
->int_setting
;
288 #ifndef ASCENDING_INT_SETTINGS
298 return max
- (selection
* step
);
300 static char * value_setting_get_name_cb(int selected_item
,
301 void * data
, char *buffer
)
303 selected_item
= selection_to_val(data
, selected_item
);
304 return option_get_valuestring(data
, buffer
, MAX_PATH
, selected_item
);
307 /* wrapper to convert from int param to bool param in option_screen */
308 static void (*boolfunction
)(bool);
309 static void bool_funcwrapper(int value
)
317 bool option_screen(struct settings_list
*setting
,
318 bool use_temp_var
, unsigned char* option_title
)
322 struct gui_synclist lists
;
323 int oldvalue
, nb_items
= 0, selected
= 0, temp_var
;
325 bool allow_wrap
= setting
->flags
& F_NO_WRAP
? false : true;
326 int var_type
= setting
->flags
&F_T_MASK
;
327 void (*function
)(int) = NULL
;
329 if (var_type
== F_T_INT
|| var_type
== F_T_UINT
)
331 variable
= use_temp_var
? &temp_var
: (int*)setting
->setting
;
332 temp_var
= oldvalue
= *(int*)setting
->setting
;
334 else if (var_type
== F_T_BOOL
)
336 /* bools always use the temp variable...
337 if use_temp_var is false it will be copied to setting->setting every change */
338 variable
= &temp_var
;
339 temp_var
= oldvalue
= *(bool*)setting
->setting
?1:0;
341 else return false; /* only int/bools can go here */
342 gui_synclist_init(&lists
, value_setting_get_name_cb
,
343 (void*)setting
, false, 1);
344 if (setting
->lang_id
== -1)
345 title
= (char*)setting
->cfg_vals
;
347 title
= P2STR(option_title
);
349 gui_synclist_set_title(&lists
, title
, Icon_Questionmark
);
350 gui_synclist_set_icon_callback(&lists
, NULL
);
351 if(global_settings
.talk_menu
)
352 gui_synclist_set_voice_callback(&lists
, option_talk
);
354 /* set the number of items and current selection */
355 if (var_type
== F_T_INT
|| var_type
== F_T_UINT
)
357 if (setting
->flags
&F_CHOICE_SETTING
)
359 nb_items
= setting
->choice_setting
->count
;
361 function
= setting
->choice_setting
->option_callback
;
363 else if (setting
->flags
&F_TABLE_SETTING
)
365 const struct table_setting
*info
= setting
->table_setting
;
367 nb_items
= info
->count
;
369 table_setting_array_position
= -1;
370 for (i
=0;selected
==-1 && i
<nb_items
;i
++)
372 if (setting
->flags
&F_ALLOW_ARBITRARY_VALS
&&
373 (oldvalue
< info
->values
[i
]))
375 table_setting_oldval
= oldvalue
;
376 table_setting_array_position
= i
;
380 else if (oldvalue
== info
->values
[i
])
383 function
= info
->option_callback
;
385 else if (setting
->flags
&F_T_SOUND
)
387 int setting_id
= setting
->sound_setting
->setting
;
388 int steps
= sound_steps(setting_id
);
389 int min
= sound_min(setting_id
);
390 int max
= sound_max(setting_id
);
391 nb_items
= (max
-min
)/steps
+ 1;
392 #ifndef ASCENDING_INT_SETTINGS
393 selected
= (max
- oldvalue
) / steps
;
395 selected
= (oldvalue
- min
) / steps
;
397 function
= sound_get_fn(setting_id
);
401 const struct int_setting
*info
= setting
->int_setting
;
406 nb_items
= (max
-min
)/step
+ 1;
407 #ifndef ASCENDING_INT_SETTINGS
408 selected
= (max
- oldvalue
) / step
;
410 selected
= (oldvalue
- min
) / step
;
412 function
= info
->option_callback
;
415 else if (var_type
== F_T_BOOL
)
419 boolfunction
= setting
->bool_setting
->option_callback
;
421 function
= bool_funcwrapper
;
423 gui_synclist_set_nb_items(&lists
, nb_items
);
424 gui_synclist_select_item(&lists
, selected
);
426 gui_synclist_limit_scroll(&lists
, true);
427 gui_synclist_draw(&lists
);
429 gui_synclist_speak_item(&lists
);
432 if (list_do_action(CONTEXT_LIST
, TIMEOUT_BLOCK
,
434 allow_wrap
? LIST_WRAP_UNLESS_HELD
: LIST_WRAP_OFF
))
436 selected
= gui_synclist_get_sel_pos(&lists
);
437 *variable
= selection_to_val(setting
, selected
);
438 if (var_type
== F_T_BOOL
)
441 *(bool*)setting
->setting
= selected
==1?true:false;
444 else if (action
== ACTION_NONE
)
446 else if (action
== ACTION_STD_CANCEL
)
448 bool show_cancel
= false;
451 else if (var_type
== F_T_INT
|| var_type
== F_T_UINT
)
453 if (*variable
!= oldvalue
)
456 *variable
= oldvalue
;
461 if (*variable
!= oldvalue
)
465 *(bool*)setting
->setting
= oldvalue
==1?true:false;
466 *variable
= oldvalue
;
470 gui_syncsplash(HZ
/2, ID2P(LANG_CANCEL
));
473 else if (action
== ACTION_STD_OK
)
477 else if(default_event_handler(action
) == SYS_USB_CONNECTED
)
479 gui_syncstatusbar_draw(&statusbars
, false);
485 if (oldvalue
!= *variable
&& (action
!= ACTION_STD_CANCEL
))
489 if (var_type
== F_T_INT
|| var_type
== F_T_UINT
)
490 *(int*)setting
->setting
= *variable
;
492 *(bool*)setting
->setting
= *variable
?true:false;
500 /******************************************************
501 Compatability functions
502 *******************************************************/
503 #define MAX_OPTIONS 32
504 const struct opt_items
*set_option_options
;
505 void set_option_formatter(char* buf
, size_t size
, int item
, const char* unit
)
508 const unsigned char *text
= set_option_options
[item
].string
;
509 snprintf(buf
, size
, "%s", P2STR(text
));
511 int32_t set_option_get_talk_id(int value
, int unit
)
514 return set_option_options
[value
].voice_id
;
516 bool set_option(const char* string
, void* variable
, enum optiontype type
,
517 const struct opt_items
* options
,
518 int numoptions
, void (*function
)(int))
521 struct settings_list item
;
522 struct int_setting data
= {
523 function
, UNIT_INT
, 0, numoptions
-1, 1,
524 set_option_formatter
, set_option_get_talk_id
526 set_option_options
= options
;
527 item
.int_setting
= &data
;
528 item
.flags
= F_INT_SETTING
|F_T_INT
;
530 item
.cfg_vals
= (char*)string
;
531 item
.setting
= &temp
;
533 temp
= *(bool*)variable
? 1: 0;
535 temp
= *(int*)variable
;
536 if (!option_screen(&item
, false, NULL
))
539 *(bool*)variable
= (temp
== 1? true: false);
541 *(int*)variable
= temp
;
547 bool set_int_ex(const unsigned char* string
,
551 void (*function
)(int),
555 void (*formatter
)(char*, size_t, int, const char*),
556 int32_t (*get_talk_id
)(int, int))
559 struct settings_list item
;
560 struct int_setting data
= {
561 function
, voice_unit
, min
, max
, step
,
562 formatter
, get_talk_id
564 item
.int_setting
= &data
;
565 item
.flags
= F_INT_SETTING
|F_T_INT
;
567 item
.cfg_vals
= (char*)string
;
568 item
.setting
= variable
;
569 return option_screen(&item
, false, NULL
);
573 void option_select_init_items(struct option_select
* opt
,
576 const struct opt_items
* items
,
581 opt
->max_value
=nb_items
;
582 opt
->option
=selected
;
586 void option_select_next(struct option_select
* opt
)
588 if(opt
->option
+ 1 >= opt
->max_value
)
590 if(opt
->option
==opt
->max_value
-1)
591 opt
->option
=opt
->min_value
;
593 opt
->option
=opt
->max_value
-1;
599 void option_select_prev(struct option_select
* opt
)
601 if(opt
->option
- 1 < opt
->min_value
)
603 /* the dissimilarity to option_select_next() arises from the
604 * sleep timer problem (bug #5000 and #5001):
605 * there we have min=0, step = 5 but the value itself might
606 * not be a multiple of 5 -- as time elapsed;
607 * We need to be able to set timer to 0 (= Off) nevertheless. */
608 if(opt
->option
!=opt
->min_value
)
609 opt
->option
=opt
->min_value
;
611 opt
->option
=opt
->max_value
-1;
617 const char * option_select_get_text(struct option_select
* opt
)
619 return(P2STR(opt
->items
[opt
->option
].string
));