Update the french translation.
[kugel-rb.git] / apps / gui / option_select.c
blob7223197fccf8eb04e442f5cc5fb8e5fdf3de01d9
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 "statusbar.h"
35 #include "misc.h"
36 #include "splash.h"
37 #include "menu.h"
38 #include "quickscreen.h"
40 #if defined (HAVE_SCROLLWHEEL) || \
41 (CONFIG_KEYPAD == PLAYER_PAD)
42 /* Define this if your target makes sense to have
43 smaller values at the top of the list increasing down the list */
44 #define ASCENDING_INT_SETTINGS
45 #endif
47 static int selection_to_val(const struct settings_list *setting, int selection);
48 int option_value_as_int(const struct settings_list *setting)
50 int type = (setting->flags & F_T_MASK);
51 int temp = 0;
52 if (type == F_T_BOOL)
53 temp = *(bool*)setting->setting?1:0;
54 else if (type == F_T_UINT || type == F_T_INT)
55 temp = *(int*)setting->setting;
56 return temp;
58 static const char *unit_strings[] =
60 [UNIT_INT] = "", [UNIT_MS] = "ms",
61 [UNIT_SEC] = "s", [UNIT_MIN] = "min",
62 [UNIT_HOUR]= "hr", [UNIT_KHZ] = "kHz",
63 [UNIT_DB] = "dB", [UNIT_PERCENT] = "%",
64 [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
65 [UNIT_PER_SEC] = "per sec",
66 [UNIT_HERTZ] = "Hz",
67 [UNIT_MB] = "MB", [UNIT_KBIT] = "kb/s",
68 [UNIT_PM_TICK] = "units/10ms",
70 /* these two vars are needed so arbitrary values can be added to the
71 TABLE_SETTING settings if the F_ALLOW_ARBITRARY_VALS flag is set */
72 static int table_setting_oldval = 0, table_setting_array_position = 0;
73 char *option_get_valuestring(const struct settings_list *setting,
74 char *buffer, int buf_len,
75 intptr_t temp_var)
77 if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
79 bool val = (bool)temp_var;
80 strncpy(buffer, str(val? setting->bool_setting->lang_yes :
81 setting->bool_setting->lang_no), buf_len);
83 #if 0 /* probably dont need this one */
84 else if ((setting->flags & F_FILENAME) == F_FILENAME)
86 struct filename_setting *info = setting->filename_setting;
87 snprintf(buffer, buf_len, "%s%s%s", info->prefix,
88 (char*)temp_var, info->suffix);
90 #endif
91 else if (((setting->flags & F_INT_SETTING) == F_INT_SETTING) ||
92 ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING))
94 const struct int_setting *int_info = setting->int_setting;
95 const struct table_setting *tbl_info = setting->table_setting;
96 const char *unit;
97 void (*formatter)(char*, size_t, int, const char*);
98 if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
100 formatter = int_info->formatter;
101 unit = unit_strings[int_info->unit];
103 else
105 formatter = tbl_info->formatter;
106 unit = unit_strings[tbl_info->unit];
108 if (formatter)
109 formatter(buffer, buf_len, (int)temp_var, unit);
110 else
111 snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit?unit:"");
113 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
115 char sign = ' ', *unit;
116 unit = (char*)sound_unit(setting->sound_setting->setting);
117 if (sound_numdecimals(setting->sound_setting->setting))
119 int integer, dec;
120 int val = sound_val2phys(setting->sound_setting->setting,
121 (int)temp_var);
122 if(val < 0)
124 sign = '-';
125 val = abs(val);
127 integer = val / 10; 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 strncpy(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 strncpy(buffer, val, buf_len);
156 return buffer;
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?true:false;
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 int step = info->step;
244 if (step < 0)
245 step = -step;
246 if (!previous)
248 val = *value + step;
249 if (val > info->max)
250 val = info->min;
252 else
254 val = *value - step;
255 if (val < info->min)
256 val = info->max;
258 if (apply && info->option_callback)
259 info->option_callback(val);
261 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
263 int setting_id = setting->sound_setting->setting;
264 int steps = sound_steps(setting_id);
265 int min = sound_min(setting_id);
266 int max = sound_max(setting_id);
267 if (!previous)
269 val = *value + steps;
270 if (val >= max)
271 val = min;
273 else
275 val = *value - steps;
276 if (val < min)
277 val = max;
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 if (apply && info->option_callback)
297 info->option_callback(val);
299 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
301 const struct table_setting *tbl_info = setting->table_setting;
302 int i, add;
303 add = previous?tbl_info->count-1:1;
304 for (i=0; i<tbl_info->count;i++)
306 if ((*value == tbl_info->values[i]) ||
307 (settings->flags&F_ALLOW_ARBITRARY_VALS &&
308 *value < tbl_info->values[i]))
310 val = tbl_info->values[(i+add)%tbl_info->count];
311 break;
315 *value = val;
317 #endif
319 static int selection_to_val(const struct settings_list *setting, int selection)
321 int min = 0, max = 0, step = 1;
322 if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
323 ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
324 return selection;
325 else if ((setting->flags & F_TABLE_SETTING) == F_TABLE_SETTING)
327 const struct table_setting *info = setting->table_setting;
328 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
329 table_setting_array_position != -1 &&
330 (selection >= table_setting_array_position))
332 if (selection == table_setting_array_position)
333 return table_setting_oldval;
334 return info->values[selection-1];
336 else
337 return info->values[selection];
339 else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
341 int setting_id = setting->sound_setting->setting;
342 #ifndef ASCENDING_INT_SETTINGS
343 step = sound_steps(setting_id);
344 max = sound_max(setting_id);
345 min = sound_min(setting_id);
346 #else
347 step = -sound_steps(setting_id);
348 min = sound_max(setting_id);
349 max = sound_min(setting_id);
350 #endif
352 else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
354 const struct int_setting *info = setting->int_setting;
355 #ifndef ASCENDING_INT_SETTINGS
356 min = info->min;
357 max = info->max;
358 step = info->step;
359 #else
360 max = info->min;
361 min = info->max;
362 step = -info->step;
363 #endif
365 return max- (selection * step);
367 static char * value_setting_get_name_cb(int selected_item,
368 void * data,
369 char *buffer,
370 size_t buffer_len)
372 selected_item = selection_to_val(data, selected_item);
373 return option_get_valuestring(data, buffer, buffer_len, selected_item);
376 /* wrapper to convert from int param to bool param in option_screen */
377 static void (*boolfunction)(bool);
378 static void bool_funcwrapper(int value)
380 if (value)
381 boolfunction(true);
382 else
383 boolfunction(false);
386 static void val_to_selection(const struct settings_list *setting, int oldvalue,
387 int *nb_items, int *selected,
388 void (**function)(int))
390 int var_type = setting->flags&F_T_MASK;
391 /* set the number of items and current selection */
392 if (var_type == F_T_INT || var_type == F_T_UINT)
394 if (setting->flags&F_CHOICE_SETTING)
396 *nb_items = setting->choice_setting->count;
397 *selected = oldvalue;
398 *function = setting->choice_setting->option_callback;
400 else if (setting->flags&F_TABLE_SETTING)
402 const struct table_setting *info = setting->table_setting;
403 int i;
404 *nb_items = info->count;
405 *selected = -1;
406 table_setting_array_position = -1;
407 for (i=0;*selected==-1 && i<*nb_items;i++)
409 if (setting->flags&F_ALLOW_ARBITRARY_VALS &&
410 (oldvalue < info->values[i]))
412 table_setting_oldval = oldvalue;
413 table_setting_array_position = i;
414 *selected = i;
415 (*nb_items)++;
417 else if (oldvalue == info->values[i])
418 *selected = i;
420 *function = info->option_callback;
422 else if (setting->flags&F_T_SOUND)
424 int setting_id = setting->sound_setting->setting;
425 int steps = sound_steps(setting_id);
426 int min = sound_min(setting_id);
427 int max = sound_max(setting_id);
428 *nb_items = (max-min)/steps + 1;
429 #ifndef ASCENDING_INT_SETTINGS
430 *selected = (max - oldvalue) / steps;
431 #else
432 *selected = (oldvalue - min) / steps;
433 #endif
434 *function = sound_get_fn(setting_id);
436 else
438 const struct int_setting *info = setting->int_setting;
439 int min, max, step;
440 max = info->max;
441 min = info->min;
442 step = info->step;
443 *nb_items = (max-min)/step + 1;
444 #ifndef ASCENDING_INT_SETTINGS
445 *selected = (max - oldvalue) / step;
446 #else
447 *selected = (oldvalue - min) / step;
448 #endif
449 *function = info->option_callback;
452 else if (var_type == F_T_BOOL)
454 *selected = oldvalue;
455 *nb_items = 2;
456 boolfunction = setting->bool_setting->option_callback;
457 if (boolfunction)
458 *function = bool_funcwrapper;
462 bool option_screen(const struct settings_list *setting,
463 struct viewport parent[NB_SCREENS],
464 bool use_temp_var, unsigned char* option_title)
466 int action;
467 bool done = false;
468 struct gui_synclist lists;
469 int oldvalue, nb_items = 0, selected = 0, temp_var;
470 int *variable;
471 bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
472 int var_type = setting->flags&F_T_MASK;
473 void (*function)(int) = NULL;
474 char *title;
475 if (var_type == F_T_INT || var_type == F_T_UINT)
477 variable = use_temp_var ? &temp_var: (int*)setting->setting;
478 temp_var = oldvalue = *(int*)setting->setting;
480 else if (var_type == F_T_BOOL)
482 /* bools always use the temp variable...
483 if use_temp_var is false it will be copied to setting->setting every change */
484 variable = &temp_var;
485 temp_var = oldvalue = *(bool*)setting->setting?1:0;
487 else return false; /* only int/bools can go here */
488 gui_synclist_init(&lists, value_setting_get_name_cb,
489 (void*)setting, false, 1, parent);
490 if (setting->lang_id == -1)
491 title = (char*)setting->cfg_vals;
492 else
493 title = P2STR(option_title);
495 gui_synclist_set_title(&lists, title, Icon_Questionmark);
496 gui_synclist_set_icon_callback(&lists, NULL);
497 if(global_settings.talk_menu)
498 gui_synclist_set_voice_callback(&lists, option_talk);
500 val_to_selection(setting, oldvalue, &nb_items, &selected, &function);
501 gui_synclist_set_nb_items(&lists, nb_items);
502 gui_synclist_select_item(&lists, selected);
504 gui_synclist_limit_scroll(&lists, true);
505 gui_synclist_draw(&lists);
506 /* talk the item */
507 gui_synclist_speak_item(&lists);
508 while (!done)
510 if (list_do_action(CONTEXT_LIST, TIMEOUT_BLOCK,
511 &lists, &action,
512 allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
514 /* setting changed */
515 selected = gui_synclist_get_sel_pos(&lists);
516 *variable = selection_to_val(setting, selected);
517 if (var_type == F_T_BOOL && !use_temp_var)
518 *(bool*)setting->setting = (*variable==1);
520 else if (action == ACTION_NONE)
521 continue;
522 else if (action == ACTION_STD_CANCEL)
524 /* setting canceled, restore old value if changed */
525 if (*variable != oldvalue)
527 *variable = oldvalue;
528 if (var_type == F_T_BOOL && !use_temp_var)
529 *(bool*)setting->setting = (oldvalue==1);
530 splash(HZ/2, ID2P(LANG_CANCEL));
532 done = true;
534 else if (action == ACTION_STD_CONTEXT)
536 /* reset setting to default */
537 reset_setting(setting, variable);
538 if (var_type == F_T_BOOL && !use_temp_var)
539 *(bool*)setting->setting = (*variable==1);
540 val_to_selection(setting, *variable, &nb_items,
541 &selected, &function);
542 gui_synclist_select_item(&lists, selected);
543 gui_synclist_draw(&lists);
544 gui_synclist_speak_item(&lists);
546 else if (action == ACTION_STD_OK)
548 /* setting accepted, store now if it used a temp var */
549 if (use_temp_var)
551 if (var_type == F_T_INT || var_type == F_T_UINT)
552 *(int*)setting->setting = *variable;
553 else
554 *(bool*)setting->setting = (*variable==1);
556 settings_save();
557 done = true;
559 else if(default_event_handler(action) == SYS_USB_CONNECTED)
560 return true;
561 /* callback */
562 if ( function )
563 function(*variable);
565 return false;