add SMAF codec (.mmf extension)(FS#10432)
[kugel-rb.git] / apps / settings.c
bloba8e4cf24a8011d41cc7803e0db6e07d75dbfe327
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Stuart Martin
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
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 <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include "inttypes.h"
27 #include "config.h"
28 #include "action.h"
29 #include "crc32.h"
30 #include "settings.h"
31 #include "debug.h"
32 #include "usb.h"
33 #include "backlight.h"
34 #include "audio.h"
35 #include "talk.h"
36 #include "string.h"
37 #include "rtc.h"
38 #include "power.h"
39 #include "ata_idle_notify.h"
40 #include "storage.h"
41 #include "screens.h"
42 #include "ctype.h"
43 #include "file.h"
44 #include "system.h"
45 #include "general.h"
46 #include "misc.h"
47 #ifdef HAVE_LCD_BITMAP
48 #include "icons.h"
49 #include "font.h"
50 #include "peakmeter.h"
51 #endif
52 #include "lang.h"
53 #include "language.h"
54 #include "powermgmt.h"
55 #include "sprintf.h"
56 #include "keyboard.h"
57 #include "version.h"
58 #include "sound.h"
59 #include "rbunicode.h"
60 #include "dircache.h"
61 #include "splash.h"
62 #include "list.h"
63 #include "settings_list.h"
64 #include "filetypes.h"
65 #include "option_select.h"
66 #if CONFIG_TUNER
67 #include "radio.h"
68 #endif
69 #include "wps.h"
70 #include "skin_engine/skin_engine.h"
71 #include "skin_engine/skin_fonts.h"
72 #include "viewport.h"
73 #include "statusbar-skinned.h"
75 #if CONFIG_CODEC == MAS3507D
76 void dac_line_in(bool enable);
77 #endif
78 struct user_settings global_settings;
79 struct system_status global_status;
81 #if CONFIG_CODEC == SWCODEC
82 #include "dsp.h"
83 #include "playback.h"
84 #ifdef HAVE_RECORDING
85 #include "enc_config.h"
86 #endif
87 #endif /* CONFIG_CODEC == SWCODEC */
89 #define NVRAM_BLOCK_SIZE 44
91 #ifdef HAVE_LCD_BITMAP
92 #define MAX_LINES 10
93 #else
94 #define MAX_LINES 2
95 #endif
97 #ifdef HAVE_REMOTE_LCD
98 #include "lcd-remote.h"
99 #endif
101 long lasttime = 0;
103 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
104 /* NVRAM is set out as
105 [0] 'R'
106 [1] 'b'
107 [2] version
108 [3] stored variable count
109 [4-7] crc32 checksum
110 [8-NVRAM_BLOCK_SIZE] data
112 #define NVRAM_DATA_START 8
113 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
114 static char nvram_buffer[NVRAM_BLOCK_SIZE];
116 static bool read_nvram_data(char* buf, int max_len)
118 unsigned crc32 = 0xffffffff;
119 int var_count = 0, i = 0, buf_pos = 0;
120 #ifndef HAVE_RTC_RAM
121 int fd = open(NVRAM_FILE,O_RDONLY);
122 if (fd < 0)
123 return false;
124 memset(buf,0,max_len);
125 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
126 return false;
127 close(fd);
128 #else
129 memset(buf,0,max_len);
130 /* read rtc block */
131 for (i=0; i < max_len; i++ )
132 buf[i] = rtc_read(0x14+i);
133 #endif
134 /* check magic, version */
135 if ((buf[0] != 'R') || (buf[1] != 'b')
136 || (buf[2] != NVRAM_CONFIG_VERSION))
137 return false;
138 /* check crc32 */
139 crc32 = crc_32(&buf[NVRAM_DATA_START],
140 max_len-NVRAM_DATA_START-1,0xffffffff);
141 if (memcmp(&crc32,&buf[4],4))
142 return false;
143 /* all good, so read in the settings */
144 var_count = buf[3];
145 buf_pos = NVRAM_DATA_START;
146 for(i=0; i<nb_settings; i++)
148 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
149 >>F_NVRAM_MASK_SHIFT;
150 if (nvram_bytes)
152 if ((var_count>0) && (buf_pos<max_len))
154 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
155 buf_pos += nvram_bytes;
156 var_count--;
158 else /* should only happen when new items are added to the end */
160 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
164 return true;
166 static bool write_nvram_data(char* buf, int max_len)
168 unsigned crc32 = 0xffffffff;
169 int i = 0, buf_pos = 0;
170 char var_count = 0;
171 #ifndef HAVE_RTC_RAM
172 int fd;
173 #endif
174 memset(buf,0,max_len);
175 /* magic, version */
176 buf[0] = 'R'; buf[1] = 'b';
177 buf[2] = NVRAM_CONFIG_VERSION;
178 buf_pos = NVRAM_DATA_START;
179 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
181 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
182 >>F_NVRAM_MASK_SHIFT;
183 if (nvram_bytes)
185 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
186 buf_pos += nvram_bytes;
187 var_count++;
190 /* count and crc32 */
191 buf[3] = var_count;
192 crc32 = crc_32(&buf[NVRAM_DATA_START],
193 max_len-NVRAM_DATA_START-1,0xffffffff);
194 memcpy(&buf[4],&crc32,4);
195 #ifndef HAVE_RTC_RAM
196 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
197 if (fd >= 0)
199 int len = write(fd,buf,max_len);
200 close(fd);
201 if (len < 8)
202 return false;
204 #else
205 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
206 that it would write a number of bytes at a time since the RTC chip
207 supports that, but this will have to do for now 8-) */
208 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
209 int r = rtc_write(0x14+i, buf[i]);
210 if (r)
211 return false;
213 #endif
214 return true;
217 /** Reading from a config file **/
219 * load settings from disk or RTC RAM
221 void settings_load(int which)
223 if (which&SETTINGS_RTC)
224 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
225 if (which&SETTINGS_HD)
227 settings_load_config(CONFIGFILE,false);
228 settings_load_config(FIXEDSETTINGSFILE,false);
232 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
234 const char* start = settings[setting_id].cfg_vals;
235 char* end = NULL;
236 char temp[MAX_PATH];
237 int count = 0;
238 while (1)
240 end = strchr(start, ',');
241 if (!end)
243 if (!strcmp(str, start))
245 *out = count;
246 return true;
248 else return false;
250 strlcpy(temp, start, end-start+1);
251 if (!strcmp(str, temp))
253 *out = count;
254 return true;
256 start = end +1;
257 count++;
259 return false;
262 bool settings_load_config(const char* file, bool apply)
264 int fd;
265 char line[128];
266 char* name;
267 char* value;
268 int i;
269 fd = open_utf8(file, O_RDONLY);
270 if (fd < 0)
271 return false;
273 while (read_line(fd, line, sizeof line) > 0)
275 if (!settings_parseline(line, &name, &value))
276 continue;
277 for(i=0; i<nb_settings; i++)
279 if (settings[i].cfg_name == NULL)
280 continue;
281 if (!strcasecmp(name,settings[i].cfg_name))
283 switch (settings[i].flags&F_T_MASK)
285 case F_T_CUSTOM:
286 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
287 break;
288 case F_T_INT:
289 case F_T_UINT:
290 #ifdef HAVE_LCD_COLOR
291 if (settings[i].flags&F_RGB)
292 hex_to_rgb(value, (int*)settings[i].setting);
293 else
294 #endif
295 if (settings[i].cfg_vals == NULL)
297 *(int*)settings[i].setting = atoi(value);
299 else
301 int temp, *v = (int*)settings[i].setting;
302 bool found = cfg_string_to_int(i, &temp, value);
303 if (found)
305 if (settings[i].flags&F_TABLE_SETTING)
306 *v = settings[i].table_setting->values[temp];
307 else
308 *v = temp;
310 else
311 { /* atoi breaks choice settings because they
312 * don't have int-like values, and would
313 * fall back to the first value (i.e. 0)
314 * due to atoi */
315 if (!(settings[i].flags&F_CHOICE_SETTING))
316 *v = atoi(value);
319 break;
320 case F_T_BOOL:
322 int temp;
323 if (cfg_string_to_int(i,&temp,value))
324 *(bool*)settings[i].setting = (temp!=0);
325 if (settings[i].bool_setting->option_callback)
326 settings[i].bool_setting->option_callback(temp!=0);
327 break;
329 case F_T_CHARPTR:
330 case F_T_UCHARPTR:
332 char storage[MAX_PATH];
333 if (settings[i].filename_setting->prefix)
335 int len = strlen(settings[i].filename_setting->prefix);
336 if (!strncasecmp(value,
337 settings[i].filename_setting->prefix,
338 len))
340 strlcpy(storage, &value[len], MAX_PATH);
342 else strlcpy(storage, value, MAX_PATH);
344 else strlcpy(storage, value, MAX_PATH);
345 if (settings[i].filename_setting->suffix)
347 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
348 if (s) *s = '\0';
350 strlcpy((char*)settings[i].setting, storage,
351 settings[i].filename_setting->max_len);
352 break;
355 break;
356 } /* if (!strcmp(name,settings[i].cfg_name)) */
357 } /* for(...) */
358 } /* while(...) */
360 close(fd);
361 settings_save();
362 if (apply)
363 settings_apply(true);
364 return true;
367 /** Writing to a config file and saving settings **/
369 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
371 int flags = settings[setting_id].flags;
372 const char* start = settings[setting_id].cfg_vals;
373 char* end = NULL;
374 int count = 0;
376 if ((flags&F_T_MASK)==F_T_INT &&
377 flags&F_TABLE_SETTING)
379 const int *value = settings[setting_id].table_setting->values;
380 while (start)
382 end = strchr(start,',');
383 if (value[count] == val)
385 if (end == NULL)
386 strlcpy(buf, start, buf_len);
387 else
389 int len = (buf_len > (end-start))? end-start: buf_len;
390 strlcpy(buf, start, len+1);
392 return true;
394 count++;
396 if (end)
397 start = end+1;
398 else
399 break;
401 return false;
404 while (count < val)
406 start = strchr(start,',');
407 if (!start)
408 return false;
409 count++;
410 start++;
412 end = strchr(start,',');
413 if (end == NULL)
414 strlcpy(buf, start, buf_len);
415 else
417 int len = (buf_len > (end-start))? end-start: buf_len;
418 strlcpy(buf, start, len+1);
420 return true;
423 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
425 switch (settings[i].flags&F_T_MASK)
427 case F_T_CUSTOM:
428 settings[i].custom_setting->write_to_cfg(settings[i].setting,
429 buf, buf_len);
430 break;
431 case F_T_INT:
432 case F_T_UINT:
433 #ifdef HAVE_LCD_COLOR
434 if (settings[i].flags&F_RGB)
436 int colour = *(int*)settings[i].setting;
437 snprintf(buf,buf_len,"%02x%02x%02x",
438 (int)RGB_UNPACK_RED(colour),
439 (int)RGB_UNPACK_GREEN(colour),
440 (int)RGB_UNPACK_BLUE(colour));
442 else
443 #endif
444 if (settings[i].cfg_vals == NULL)
446 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
448 else
450 if (cfg_int_to_string(i, *(int*)settings[i].setting,
451 buf, buf_len) == false)
453 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
455 else
456 return false;
458 break;
459 case F_T_BOOL:
460 cfg_int_to_string(i,
461 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
462 break;
463 case F_T_CHARPTR:
464 case F_T_UCHARPTR:
465 if (((char*)settings[i].setting)[0]
466 && settings[i].filename_setting->prefix)
468 if (((char*)settings[i].setting)[0] == '-')
470 buf[0] = '-';
471 buf[1] = '\0';
473 else
475 snprintf(buf,buf_len,"%s%s%s",
476 settings[i].filename_setting->prefix,
477 (char*)settings[i].setting,
478 settings[i].filename_setting->suffix);
481 else strlcpy(buf,(char*)settings[i].setting,
482 settings[i].filename_setting->max_len);
483 break;
484 } /* switch () */
485 return true;
489 static bool is_changed(int setting_id)
491 const struct settings_list *setting = &settings[setting_id];
492 switch (setting->flags&F_T_MASK)
494 case F_T_CUSTOM:
495 return setting->custom_setting->is_changed(setting->setting,
496 setting->default_val.custom);
497 break;
498 case F_T_INT:
499 case F_T_UINT:
500 if (setting->flags&F_DEF_ISFUNC)
502 if (*(int*)setting->setting == setting->default_val.func())
503 return false;
505 else if (setting->flags&F_T_SOUND)
507 if (*(int*)setting->setting ==
508 sound_default(setting->sound_setting->setting))
509 return false;
511 else if (*(int*)setting->setting == setting->default_val.int_)
512 return false;
513 break;
514 case F_T_BOOL:
515 if (*(bool*)setting->setting == setting->default_val.bool_)
516 return false;
517 break;
518 case F_T_CHARPTR:
519 case F_T_UCHARPTR:
520 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
521 return false;
522 break;
524 return true;
527 static bool settings_write_config(const char* filename, int options)
529 int i;
530 int fd;
531 char value[MAX_PATH];
532 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
533 if (fd < 0)
534 return false;
535 fdprintf(fd, "# .cfg file created by rockbox %s - "
536 "http://www.rockbox.org\r\n\r\n", appsversion);
537 for(i=0; i<nb_settings; i++)
539 if (settings[i].cfg_name == NULL)
540 continue;
541 value[0] = '\0';
543 switch (options)
545 case SETTINGS_SAVE_CHANGED:
546 if (!is_changed(i))
547 continue;
548 break;
549 case SETTINGS_SAVE_SOUND:
550 if ((settings[i].flags&F_SOUNDSETTING) == 0)
551 continue;
552 break;
553 case SETTINGS_SAVE_THEME:
554 if ((settings[i].flags&F_THEMESETTING) == 0)
555 continue;
556 break;
557 #ifdef HAVE_RECORDING
558 case SETTINGS_SAVE_RECPRESETS:
559 if ((settings[i].flags&F_RECSETTING) == 0)
560 continue;
561 break;
562 #endif
563 #if CONFIG_CODEC == SWCODEC
564 case SETTINGS_SAVE_EQPRESET:
565 if ((settings[i].flags&F_EQSETTING) == 0)
566 continue;
567 break;
568 #endif
571 cfg_to_string(i, value, MAX_PATH);
572 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
573 } /* for(...) */
574 close(fd);
575 return true;
577 #ifndef HAVE_RTC_RAM
578 static void flush_global_status_callback(void *data)
580 (void)data;
581 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
583 #endif
584 static void flush_config_block_callback(void *data)
586 (void)data;
587 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
588 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
592 * persist all runtime user settings to disk or RTC RAM
594 static void update_runtime(void)
596 int elapsed_secs;
598 elapsed_secs = (current_tick - lasttime) / HZ;
599 global_status.runtime += elapsed_secs;
600 lasttime += (elapsed_secs * HZ);
602 if ( global_status.runtime > global_status.topruntime )
603 global_status.topruntime = global_status.runtime;
606 void status_save(void)
608 update_runtime();
609 #ifdef HAVE_RTC_RAM
610 /* this will be done in the storage_callback if
611 target doesnt have rtc ram */
612 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
613 #else
614 register_storage_idle_func(flush_global_status_callback);
615 #endif
618 int settings_save(void)
620 update_runtime();
621 #ifdef HAVE_RTC_RAM
622 /* this will be done in the storage_callback if
623 target doesnt have rtc ram */
624 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
625 #endif
626 register_storage_idle_func(flush_config_block_callback);
627 return 0;
630 bool settings_save_config(int options)
632 char filename[MAX_PATH];
633 char *folder, *namebase;
634 switch (options)
636 case SETTINGS_SAVE_THEME:
637 folder = THEME_DIR;
638 namebase = "theme";
639 break;
640 #ifdef HAVE_RECORDING
641 case SETTINGS_SAVE_RECPRESETS:
642 folder = RECPRESETS_DIR;
643 namebase = "recording";
644 break;
645 #endif
646 #if CONFIG_CODEC == SWCODEC
647 case SETTINGS_SAVE_EQPRESET:
648 folder = EQS_DIR;
649 namebase = "eq";
650 break;
651 #endif
652 case SETTINGS_SAVE_SOUND:
653 folder = ROCKBOX_DIR;
654 namebase = "sound";
655 break;
656 default:
657 folder = ROCKBOX_DIR;
658 namebase = "config";
659 break;
661 create_numbered_filename(filename, folder, namebase, ".cfg", 2
662 IF_CNFN_NUM_(, NULL));
664 /* allow user to modify filename */
665 while (true) {
666 if (!kbd_input(filename, sizeof filename)) {
667 break;
669 else {
670 return false;
674 if (settings_write_config(filename, options))
675 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
676 else
677 splash(HZ, ID2P(LANG_FAILED));
678 return true;
681 /** Apply and Reset settings **/
684 #ifdef HAVE_LCD_BITMAP
686 * Applies the range infos stored in global_settings to
687 * the peak meter.
689 void settings_apply_pm_range(void)
691 int pm_min, pm_max;
693 /* depending on the scale mode (dBfs or percent) the values
694 of global_settings.peak_meter_dbfs have different meanings */
695 if (global_settings.peak_meter_dbfs)
697 /* convert to dBfs * 100 */
698 pm_min = -(((int)global_settings.peak_meter_min) * 100);
699 pm_max = -(((int)global_settings.peak_meter_max) * 100);
701 else
703 /* percent is stored directly -> no conversion */
704 pm_min = global_settings.peak_meter_min;
705 pm_max = global_settings.peak_meter_max;
708 /* apply the range */
709 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
711 #endif /* HAVE_LCD_BITMAP */
713 void sound_settings_apply(void)
715 #if CONFIG_CODEC == SWCODEC
716 sound_set_dsp_callback(dsp_callback);
717 #endif
718 sound_set(SOUND_BASS, global_settings.bass);
719 sound_set(SOUND_TREBLE, global_settings.treble);
720 sound_set(SOUND_BALANCE, global_settings.balance);
721 sound_set(SOUND_VOLUME, global_settings.volume);
722 sound_set(SOUND_CHANNELS, global_settings.channel_config);
723 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
724 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
725 sound_set(SOUND_LOUDNESS, global_settings.loudness);
726 sound_set(SOUND_AVC, global_settings.avc);
727 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
728 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
729 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
730 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
731 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
732 sound_set(SOUND_SUPERBASS, global_settings.superbass);
733 #endif
735 #ifdef HAVE_WM8758
736 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
737 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
738 #endif
743 /* call this after loading a .wps/.rwps or other skin files, so that the
744 * skin buffer is reset properly
746 struct skin_load_setting {
747 char* setting;
748 char* suffix;
749 void (*loadfunc)(enum screen_type screen, const char *buf, bool isfile);
751 static struct skin_load_setting skins[] = {
752 /* This determins the load order. *sbs must be loaded before any other
753 * skin on that screen */
754 #ifdef HAVE_LCD_BITMAP
755 { global_settings.sbs_file, "sbs", sb_skin_data_load},
756 #endif
757 { global_settings.wps_file, "wps", wps_data_load},
758 #ifdef HAVE_REMOTE_LCD
759 { global_settings.rsbs_file, "rsbs", sb_skin_data_load},
760 { global_settings.rwps_file, "rwps", wps_data_load},
761 #endif
763 void settings_apply_skins(void)
765 char buf[MAX_PATH];
766 /* re-initialize the skin buffer before we start reloading skins */
767 skin_buffer_init();
768 enum screen_type screen = SCREEN_MAIN;
769 unsigned int i;
770 #ifdef HAVE_LCD_BITMAP
771 skin_backdrop_init();
772 skin_font_init();
773 #endif
774 for (i=0; i<sizeof(skins)/sizeof(*skins); i++)
776 #ifdef HAVE_REMOTE_LCD
777 screen = skins[i].suffix[0] == 'r' ? SCREEN_REMOTE : SCREEN_MAIN;
778 #endif
779 if (skins[i].setting[0] && skins[i].setting[0] != '-')
781 snprintf(buf, sizeof buf, WPS_DIR "/%s.%s",
782 skins[i].setting, skins[i].suffix);
783 skins[i].loadfunc(screen, buf, true);
785 else
787 skins[i].loadfunc(screen, NULL, true);
790 viewportmanager_theme_changed(THEME_STATUSBAR);
791 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
792 FOR_NB_SCREENS(i)
793 screens[i].backdrop_show(sb_get_backdrop(i));
794 #endif
797 void settings_apply(bool read_disk)
800 char buf[64];
801 #if CONFIG_CODEC == SWCODEC
802 int i;
803 #endif
804 sound_settings_apply();
806 #ifdef HAVE_DISK_STORAGE
807 audio_set_buffer_margin(global_settings.buffer_margin);
808 #endif
810 #ifdef HAVE_LCD_CONTRAST
811 lcd_set_contrast(global_settings.contrast);
812 #endif
813 lcd_scroll_speed(global_settings.scroll_speed);
814 #ifdef HAVE_REMOTE_LCD
815 lcd_remote_set_contrast(global_settings.remote_contrast);
816 lcd_remote_set_invert_display(global_settings.remote_invert);
818 #ifdef HAVE_LCD_FLIP
819 lcd_remote_set_flip(global_settings.remote_flip_display);
820 #endif
822 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
823 lcd_remote_scroll_step(global_settings.remote_scroll_step);
824 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
825 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
826 #ifdef HAVE_REMOTE_LCD_TICKING
827 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
828 #endif
829 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
830 #if CONFIG_CHARGING
831 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
832 #endif
833 #ifdef HAS_REMOTE_BUTTON_HOLD
834 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
835 #endif
836 #endif /* HAVE_REMOTE_LCD */
837 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
838 backlight_set_brightness(global_settings.brightness);
839 #endif
840 #ifdef HAVE_BACKLIGHT
841 backlight_set_timeout(global_settings.backlight_timeout);
842 #if CONFIG_CHARGING
843 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
844 #endif
845 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
846 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
847 backlight_set_fade_in(global_settings.backlight_fade_in);
848 backlight_set_fade_out(global_settings.backlight_fade_out);
849 #endif
850 #endif
851 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
852 buttonlight_set_brightness(global_settings.buttonlight_brightness);
853 #endif
854 #ifdef HAVE_BUTTON_LIGHT
855 buttonlight_set_timeout(global_settings.buttonlight_timeout);
856 #endif
857 #ifdef HAVE_DISK_STORAGE
858 storage_spindown(global_settings.disk_spindown);
859 #endif
860 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
861 dac_line_in(global_settings.line_in);
862 #endif
863 set_poweroff_timeout(global_settings.poweroff);
865 set_battery_capacity(global_settings.battery_capacity);
866 #if BATTERY_TYPES_COUNT > 1
867 set_battery_type(global_settings.battery_type);
868 #endif
870 #ifdef HAVE_LCD_BITMAP
871 #ifdef HAVE_LCD_INVERT
872 lcd_set_invert_display(global_settings.invert);
873 #endif
874 #ifdef HAVE_LCD_FLIP
875 lcd_set_flip(global_settings.flip_display);
876 button_set_flip(global_settings.flip_display);
877 #endif
878 lcd_update(); /* refresh after flipping the screen */
879 settings_apply_pm_range();
880 peak_meter_init_times(
881 global_settings.peak_meter_release, global_settings.peak_meter_hold,
882 global_settings.peak_meter_clip_hold);
883 #endif
885 #ifdef HAVE_SPEAKER
886 audiohw_enable_speaker(global_settings.speaker_enabled);
887 #endif
889 if (read_disk)
891 #ifdef HAVE_LCD_BITMAP
892 /* fonts need to be loaded before the WPS */
893 if ( global_settings.font_file[0]) {
894 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
895 global_settings.font_file);
896 if (font_load(NULL, buf) < 0)
897 font_reset(NULL);
899 else
900 font_reset(NULL);
901 #ifdef HAVE_REMOTE_LCD
902 if ( global_settings.remote_font_file[0]) {
903 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
904 global_settings.remote_font_file);
905 if (font_load_remoteui(buf) < 0)
906 font_load_remoteui(NULL);
908 else
909 font_load_remoteui(NULL);
910 #endif
911 if ( global_settings.kbd_file[0]) {
912 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
913 global_settings.kbd_file);
914 load_kbd(buf);
916 else
917 load_kbd(NULL);
918 #endif
920 if ( global_settings.lang_file[0]) {
921 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
922 global_settings.lang_file);
923 lang_core_load(buf);
924 talk_init(); /* use voice of same language */
927 /* reload wpses */
928 settings_apply_skins();
930 /* load the icon set */
931 icons_init();
933 #ifdef HAVE_LCD_COLOR
934 if (global_settings.colors_file[0])
935 read_color_theme_file();
936 #endif
939 #ifdef HAVE_LCD_COLOR
940 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
941 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
942 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
943 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
944 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
945 #endif
947 #ifdef HAVE_LCD_BITMAP
948 lcd_scroll_step(global_settings.scroll_step);
949 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
950 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
951 #else
952 lcd_jump_scroll(global_settings.jump_scroll);
953 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
954 #endif
955 lcd_bidir_scroll(global_settings.bidir_limit);
956 lcd_scroll_delay(global_settings.scroll_delay);
959 set_codepage(global_settings.default_codepage);
961 #if CONFIG_CODEC == SWCODEC
962 #ifdef HAVE_CROSSFADE
963 audio_set_crossfade(global_settings.crossfade);
964 #endif
965 dsp_set_replaygain();
966 dsp_set_crossfeed(global_settings.crossfeed);
967 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
968 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
969 global_settings.crossfeed_hf_attenuation,
970 global_settings.crossfeed_hf_cutoff);
972 /* Configure software equalizer, hardware eq is handled in audio_init() */
973 dsp_set_eq(global_settings.eq_enabled);
974 dsp_set_eq_precut(global_settings.eq_precut);
975 for(i = 0; i < 5; i++) {
976 dsp_set_eq_coefs(i);
979 dsp_dither_enable(global_settings.dithering_enabled);
980 dsp_timestretch_enable(global_settings.timestretch_enabled);
981 dsp_set_compressor(global_settings.compressor_threshold,
982 global_settings.compressor_makeup_gain,
983 global_settings.compressor_ratio,
984 global_settings.compressor_knee,
985 global_settings.compressor_release_time);
986 #endif
988 #ifdef HAVE_SPDIF_POWER
989 spdif_power_enable(global_settings.spdif_enable);
990 #endif
992 #ifdef HAVE_BACKLIGHT
993 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
994 #ifdef HAVE_REMOTE_LCD
995 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
996 #endif
997 #ifdef HAS_BUTTON_HOLD
998 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
999 #endif
1000 #ifdef HAVE_LCD_SLEEP_SETTING
1001 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
1002 #endif
1003 #endif /* HAVE_BACKLIGHT */
1005 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1006 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1007 #endif
1009 #ifdef HAVE_USB_CHARGING_ENABLE
1010 usb_charging_enable(global_settings.usb_charging);
1011 #endif
1013 #ifdef HAVE_TOUCHSCREEN
1014 touchscreen_set_mode(global_settings.touch_mode);
1015 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1016 #endif
1018 /* This should stay last */
1019 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1020 enc_global_settings_apply();
1021 #endif
1022 #ifdef HAVE_LCD_BITMAP
1023 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1024 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE);
1025 #endif
1030 * reset all settings to their default value
1032 void reset_setting(const struct settings_list *setting, void *var)
1034 switch (setting->flags&F_T_MASK)
1036 case F_T_CUSTOM:
1037 setting->custom_setting->set_default(setting->setting,
1038 setting->default_val.custom);
1039 break;
1040 case F_T_INT:
1041 case F_T_UINT:
1042 if (setting->flags&F_DEF_ISFUNC)
1043 *(int*)var = setting->default_val.func();
1044 else if (setting->flags&F_T_SOUND)
1045 *(int*)var = sound_default(setting->sound_setting->setting);
1046 else *(int*)var = setting->default_val.int_;
1047 break;
1048 case F_T_BOOL:
1049 *(bool*)var = setting->default_val.bool_;
1050 break;
1051 case F_T_CHARPTR:
1052 case F_T_UCHARPTR:
1053 strlcpy((char*)var, setting->default_val.charptr,
1054 setting->filename_setting->max_len);
1055 break;
1059 void settings_reset(void)
1061 int i;
1063 for(i=0; i<nb_settings; i++)
1064 reset_setting(&settings[i], settings[i].setting);
1065 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1066 enc_global_settings_reset();
1067 #endif
1070 /** Changing setting values **/
1071 const struct settings_list* find_setting(const void* variable, int *id)
1073 int i;
1074 for(i=0;i<nb_settings;i++)
1076 if (settings[i].setting == variable)
1078 if (id)
1079 *id = i;
1080 return &settings[i];
1083 return NULL;
1086 bool set_bool(const char* string, const bool* variable )
1088 return set_bool_options(string, variable,
1089 (char *)STR(LANG_SET_BOOL_YES),
1090 (char *)STR(LANG_SET_BOOL_NO),
1091 NULL);
1095 bool set_bool_options(const char* string, const bool* variable,
1096 const char* yes_str, int yes_voice,
1097 const char* no_str, int no_voice,
1098 void (*function)(bool))
1100 struct opt_items names[] = {
1101 {(unsigned const char *)no_str, no_voice},
1102 {(unsigned const char *)yes_str, yes_voice}
1104 bool result;
1106 result = set_option(string, variable, BOOL, names, 2,
1107 (void (*)(int))function);
1108 return result;
1111 bool set_int(const unsigned char* string,
1112 const char* unit,
1113 int voice_unit,
1114 const int* variable,
1115 void (*function)(int),
1116 int step,
1117 int min,
1118 int max,
1119 const char* (*formatter)(char*, size_t, int, const char*) )
1121 return set_int_ex(string, unit, voice_unit, variable, function,
1122 step, min, max, formatter, NULL);
1125 bool set_int_ex(const unsigned char* string,
1126 const char* unit,
1127 int voice_unit,
1128 const int* variable,
1129 void (*function)(int),
1130 int step,
1131 int min,
1132 int max,
1133 const char* (*formatter)(char*, size_t, int, const char*),
1134 int32_t (*get_talk_id)(int, int))
1136 (void)unit;
1137 struct settings_list item;
1138 struct int_setting data = {
1139 function, voice_unit, min, max, step,
1140 formatter, get_talk_id
1142 item.int_setting = &data;
1143 item.flags = F_INT_SETTING|F_T_INT;
1144 item.lang_id = -1;
1145 item.cfg_vals = (char*)string;
1146 item.setting = (void *)variable;
1147 return option_screen(&item, NULL, false, NULL);
1151 static const struct opt_items *set_option_options;
1152 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1154 (void)buf, (void)unit, (void)size;
1155 return P2STR(set_option_options[item].string);
1158 static int32_t set_option_get_talk_id(int value, int unit)
1160 (void)unit;
1161 return set_option_options[value].voice_id;
1164 bool set_option(const char* string, const void* variable, enum optiontype type,
1165 const struct opt_items* options,
1166 int numoptions, void (*function)(int))
1168 int temp;
1169 struct settings_list item;
1170 struct int_setting data = {
1171 function, UNIT_INT, 0, numoptions-1, 1,
1172 set_option_formatter, set_option_get_talk_id
1174 set_option_options = options;
1175 item.int_setting = &data;
1176 item.flags = F_INT_SETTING|F_T_INT;
1177 item.lang_id = -1;
1178 item.cfg_vals = (char*)string;
1179 item.setting = &temp;
1180 if (type == BOOL)
1181 temp = *(bool*)variable? 1: 0;
1182 else
1183 temp = *(int*)variable;
1184 if (!option_screen(&item, NULL, false, NULL))
1186 if (type == BOOL)
1187 *(bool*)variable = (temp == 1);
1188 else
1189 *(int*)variable = temp;
1190 return false;
1192 return true;
1196 void set_file(const char* filename, char* setting, int maxlen)
1198 const char* fptr = strrchr(filename,'/');
1199 int len;
1200 int extlen = 0;
1201 const char* ptr;
1203 if (!fptr)
1204 return;
1206 fptr++;
1208 len = strlen(fptr);
1209 ptr = fptr + len;
1210 while ((*ptr != '.') && (ptr != fptr)) {
1211 extlen++;
1212 ptr--;
1214 if(ptr == fptr) extlen = 0;
1216 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1217 (len-extlen > maxlen))
1218 return;
1220 strlcpy(setting, fptr, len-extlen+1);
1222 settings_save();