FS#10199: Adds limiter DSP function
[kugel-rb.git] / apps / settings.c
blobb76468280e59e355aae8032ce3640f25e965e013
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 "mpeg.h"
36 #include "talk.h"
37 #include "string.h"
38 #include "rtc.h"
39 #include "power.h"
40 #include "ata_idle_notify.h"
41 #include "storage.h"
42 #include "screens.h"
43 #include "ctype.h"
44 #include "file.h"
45 #include "system.h"
46 #include "general.h"
47 #include "misc.h"
48 #ifdef HAVE_LCD_BITMAP
49 #include "icons.h"
50 #include "font.h"
51 #include "peakmeter.h"
52 #endif
53 #include "lang.h"
54 #include "language.h"
55 #include "powermgmt.h"
56 #include "sprintf.h"
57 #include "keyboard.h"
58 #include "version.h"
59 #include "sound.h"
60 #include "rbunicode.h"
61 #include "dircache.h"
62 #include "splash.h"
63 #include "list.h"
64 #include "settings_list.h"
65 #include "filetypes.h"
66 #include "option_select.h"
67 #include "backdrop.h"
68 #if CONFIG_TUNER
69 #include "radio.h"
70 #endif
71 #include "wps.h"
72 #include "skin_engine/skin_engine.h"
73 #include "viewport.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 *v = atoi(value);
313 break;
314 case F_T_BOOL:
316 int temp;
317 if (cfg_string_to_int(i,&temp,value))
318 *(bool*)settings[i].setting = (temp!=0);
319 if (settings[i].bool_setting->option_callback)
320 settings[i].bool_setting->option_callback(temp!=0);
321 break;
323 case F_T_CHARPTR:
324 case F_T_UCHARPTR:
326 char storage[MAX_PATH];
327 if (settings[i].filename_setting->prefix)
329 int len = strlen(settings[i].filename_setting->prefix);
330 if (!strncasecmp(value,
331 settings[i].filename_setting->prefix,
332 len))
334 strlcpy(storage, &value[len], MAX_PATH);
336 else strlcpy(storage, value, MAX_PATH);
338 else strlcpy(storage, value, MAX_PATH);
339 if (settings[i].filename_setting->suffix)
341 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
342 if (s) *s = '\0';
344 strlcpy((char*)settings[i].setting, storage,
345 settings[i].filename_setting->max_len);
346 break;
349 break;
350 } /* if (!strcmp(name,settings[i].cfg_name)) */
351 } /* for(...) */
352 } /* while(...) */
354 close(fd);
355 settings_save();
356 if (apply)
357 settings_apply(true);
358 return true;
361 /** Writing to a config file and saving settings **/
363 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
365 int flags = settings[setting_id].flags;
366 const char* start = settings[setting_id].cfg_vals;
367 char* end = NULL;
368 int count = 0;
370 if ((flags&F_T_MASK)==F_T_INT &&
371 flags&F_TABLE_SETTING)
373 const int *value = settings[setting_id].table_setting->values;
374 while (start)
376 end = strchr(start,',');
377 if (value[count] == val)
379 if (end == NULL)
380 strlcpy(buf, start, buf_len);
381 else
383 int len = (buf_len > (end-start))? end-start: buf_len;
384 strlcpy(buf, start, len+1);
386 return true;
388 count++;
390 if (end)
391 start = end+1;
392 else
393 break;
395 return false;
398 while (count < val)
400 start = strchr(start,',');
401 if (!start)
402 return false;
403 count++;
404 start++;
406 end = strchr(start,',');
407 if (end == NULL)
408 strlcpy(buf, start, buf_len);
409 else
411 int len = (buf_len > (end-start))? end-start: buf_len;
412 strlcpy(buf, start, len+1);
414 return true;
417 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
419 switch (settings[i].flags&F_T_MASK)
421 case F_T_CUSTOM:
422 settings[i].custom_setting->write_to_cfg(settings[i].setting,
423 buf, buf_len);
424 break;
425 case F_T_INT:
426 case F_T_UINT:
427 #ifdef HAVE_LCD_COLOR
428 if (settings[i].flags&F_RGB)
430 int colour = *(int*)settings[i].setting;
431 snprintf(buf,buf_len,"%02x%02x%02x",
432 (int)RGB_UNPACK_RED(colour),
433 (int)RGB_UNPACK_GREEN(colour),
434 (int)RGB_UNPACK_BLUE(colour));
436 else
437 #endif
438 if (settings[i].cfg_vals == NULL)
440 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
442 else
444 if (cfg_int_to_string(i, *(int*)settings[i].setting,
445 buf, buf_len) == false)
447 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
449 else
450 return false;
452 break;
453 case F_T_BOOL:
454 cfg_int_to_string(i,
455 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
456 break;
457 case F_T_CHARPTR:
458 case F_T_UCHARPTR:
459 if (((char*)settings[i].setting)[0]
460 && settings[i].filename_setting->prefix)
462 snprintf(buf,buf_len,"%s%s%s",
463 settings[i].filename_setting->prefix,
464 (char*)settings[i].setting,
465 settings[i].filename_setting->suffix);
467 else strlcpy(buf,(char*)settings[i].setting,
468 settings[i].filename_setting->max_len);
469 break;
470 } /* switch () */
471 return true;
475 static bool is_changed(int setting_id)
477 const struct settings_list *setting = &settings[setting_id];
478 switch (setting->flags&F_T_MASK)
480 case F_T_CUSTOM:
481 return setting->custom_setting->is_changed(setting->setting,
482 setting->default_val.custom);
483 break;
484 case F_T_INT:
485 case F_T_UINT:
486 if (setting->flags&F_DEF_ISFUNC)
488 if (*(int*)setting->setting == setting->default_val.func())
489 return false;
491 else if (setting->flags&F_T_SOUND)
493 if (*(int*)setting->setting ==
494 sound_default(setting->sound_setting->setting))
495 return false;
497 else if (*(int*)setting->setting == setting->default_val.int_)
498 return false;
499 break;
500 case F_T_BOOL:
501 if (*(bool*)setting->setting == setting->default_val.bool_)
502 return false;
503 break;
504 case F_T_CHARPTR:
505 case F_T_UCHARPTR:
506 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
507 return false;
508 break;
510 return true;
513 static bool settings_write_config(const char* filename, int options)
515 int i;
516 int fd;
517 char value[MAX_PATH];
518 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
519 if (fd < 0)
520 return false;
521 fdprintf(fd, "# .cfg file created by rockbox %s - "
522 "http://www.rockbox.org\r\n\r\n", appsversion);
523 for(i=0; i<nb_settings; i++)
525 if (settings[i].cfg_name == NULL)
526 continue;
527 value[0] = '\0';
529 switch (options)
531 case SETTINGS_SAVE_CHANGED:
532 if (!is_changed(i))
533 continue;
534 break;
535 case SETTINGS_SAVE_SOUND:
536 if ((settings[i].flags&F_SOUNDSETTING) == 0)
537 continue;
538 break;
539 case SETTINGS_SAVE_THEME:
540 if ((settings[i].flags&F_THEMESETTING) == 0)
541 continue;
542 break;
543 #ifdef HAVE_RECORDING
544 case SETTINGS_SAVE_RECPRESETS:
545 if ((settings[i].flags&F_RECSETTING) == 0)
546 continue;
547 break;
548 #endif
549 #if CONFIG_CODEC == SWCODEC
550 case SETTINGS_SAVE_EQPRESET:
551 if ((settings[i].flags&F_EQSETTING) == 0)
552 continue;
553 break;
554 #endif
557 cfg_to_string(i, value, MAX_PATH);
558 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
559 } /* for(...) */
560 close(fd);
561 return true;
563 #ifndef HAVE_RTC_RAM
564 static bool flush_global_status_callback(void)
566 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
568 #endif
569 static bool flush_config_block_callback(void)
571 bool r1, r2;
572 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
573 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
574 return r1 || r2;
578 * persist all runtime user settings to disk or RTC RAM
580 static void update_runtime(void)
582 int elapsed_secs;
584 elapsed_secs = (current_tick - lasttime) / HZ;
585 global_status.runtime += elapsed_secs;
586 lasttime += (elapsed_secs * HZ);
588 if ( global_status.runtime > global_status.topruntime )
589 global_status.topruntime = global_status.runtime;
592 void status_save(void)
594 update_runtime();
595 #ifdef HAVE_RTC_RAM
596 /* this will be done in the storage_callback if
597 target doesnt have rtc ram */
598 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
599 #else
600 register_storage_idle_func(flush_global_status_callback);
601 #endif
604 int settings_save(void)
606 update_runtime();
607 #ifdef HAVE_RTC_RAM
608 /* this will be done in the storage_callback if
609 target doesnt have rtc ram */
610 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
611 #endif
612 register_storage_idle_func(flush_config_block_callback);
613 return 0;
616 bool settings_save_config(int options)
618 char filename[MAX_PATH];
619 char *folder, *namebase;
620 switch (options)
622 case SETTINGS_SAVE_THEME:
623 folder = THEME_DIR;
624 namebase = "theme";
625 break;
626 #ifdef HAVE_RECORDING
627 case SETTINGS_SAVE_RECPRESETS:
628 folder = RECPRESETS_DIR;
629 namebase = "recording";
630 break;
631 #endif
632 #if CONFIG_CODEC == SWCODEC
633 case SETTINGS_SAVE_EQPRESET:
634 folder = EQS_DIR;
635 namebase = "eq";
636 break;
637 #endif
638 case SETTINGS_SAVE_SOUND:
639 folder = ROCKBOX_DIR;
640 namebase = "sound";
641 break;
642 default:
643 folder = ROCKBOX_DIR;
644 namebase = "config";
645 break;
647 create_numbered_filename(filename, folder, namebase, ".cfg", 2
648 IF_CNFN_NUM_(, NULL));
650 /* allow user to modify filename */
651 while (true) {
652 if (!kbd_input(filename, sizeof filename)) {
653 break;
655 else {
656 return false;
660 if (settings_write_config(filename, options))
661 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
662 else
663 splash(HZ, ID2P(LANG_FAILED));
664 return true;
667 /** Apply and Reset settings **/
670 #ifdef HAVE_LCD_BITMAP
672 * Applies the range infos stored in global_settings to
673 * the peak meter.
675 void settings_apply_pm_range(void)
677 int pm_min, pm_max;
679 /* depending on the scale mode (dBfs or percent) the values
680 of global_settings.peak_meter_dbfs have different meanings */
681 if (global_settings.peak_meter_dbfs)
683 /* convert to dBfs * 100 */
684 pm_min = -(((int)global_settings.peak_meter_min) * 100);
685 pm_max = -(((int)global_settings.peak_meter_max) * 100);
687 else
689 /* percent is stored directly -> no conversion */
690 pm_min = global_settings.peak_meter_min;
691 pm_max = global_settings.peak_meter_max;
694 /* apply the range */
695 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
697 #endif /* HAVE_LCD_BITMAP */
699 void sound_settings_apply(void)
701 #if CONFIG_CODEC == SWCODEC
702 sound_set_dsp_callback(dsp_callback);
703 #endif
704 sound_set(SOUND_BASS, global_settings.bass);
705 sound_set(SOUND_TREBLE, global_settings.treble);
706 sound_set(SOUND_BALANCE, global_settings.balance);
707 sound_set(SOUND_VOLUME, global_settings.volume);
708 sound_set(SOUND_CHANNELS, global_settings.channel_config);
709 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
710 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
711 sound_set(SOUND_LOUDNESS, global_settings.loudness);
712 sound_set(SOUND_AVC, global_settings.avc);
713 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
714 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
715 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
716 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
717 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
718 sound_set(SOUND_SUPERBASS, global_settings.superbass);
719 #endif
721 #ifdef HAVE_WM8758
722 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
723 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
724 #endif
729 /* call this after loading a .wps/.rwps pr other skin files, so that the
730 * skin buffer is reset properly
732 void settings_apply_skins(void)
734 char buf[MAX_PATH];
735 /* re-initialize the skin buffer before we start reloading skins */
736 skin_buffer_init();
737 if ( global_settings.wps_file[0] &&
738 global_settings.wps_file[0] != 0xff ) {
739 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
740 global_settings.wps_file);
741 wps_data_load(SCREEN_MAIN, buf, true);
743 else
745 wps_data_init(SCREEN_MAIN);
746 wps_data_load(SCREEN_MAIN, NULL, true);
748 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
749 if ( global_settings.rwps_file[0]) {
750 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
751 global_settings.rwps_file);
752 wps_data_load(SCREEN_REMOTE, buf, true);
754 else
756 wps_data_init(SCREEN_REMOTE);
757 wps_data_load(SCREEN_REMOTE, NULL, true);
759 #endif
762 void settings_apply(bool read_disk)
765 char buf[64];
766 #if CONFIG_CODEC == SWCODEC
767 int i;
768 #endif
769 int screen;
771 sound_settings_apply();
773 #ifdef HAVE_DISK_STORAGE
774 audio_set_buffer_margin(global_settings.buffer_margin);
775 #endif
777 #ifdef HAVE_LCD_CONTRAST
778 lcd_set_contrast(global_settings.contrast);
779 #endif
780 lcd_scroll_speed(global_settings.scroll_speed);
781 #ifdef HAVE_REMOTE_LCD
782 lcd_remote_set_contrast(global_settings.remote_contrast);
783 lcd_remote_set_invert_display(global_settings.remote_invert);
785 #ifdef HAVE_LCD_FLIP
786 lcd_remote_set_flip(global_settings.remote_flip_display);
787 #endif
789 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
790 lcd_remote_scroll_step(global_settings.remote_scroll_step);
791 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
792 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
793 #ifdef HAVE_REMOTE_LCD_TICKING
794 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
795 #endif
796 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
797 #if CONFIG_CHARGING
798 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
799 #endif
800 #ifdef HAS_REMOTE_BUTTON_HOLD
801 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
802 #endif
803 #endif /* HAVE_REMOTE_LCD */
804 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
805 backlight_set_brightness(global_settings.brightness);
806 #endif
807 #ifdef HAVE_BACKLIGHT
808 backlight_set_timeout(global_settings.backlight_timeout);
809 #if CONFIG_CHARGING
810 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
811 #endif
812 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
813 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
814 backlight_set_fade_in(global_settings.backlight_fade_in);
815 backlight_set_fade_out(global_settings.backlight_fade_out);
816 #endif
817 #endif
818 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
819 buttonlight_set_brightness(global_settings.buttonlight_brightness);
820 #endif
821 #ifdef HAVE_BUTTON_LIGHT
822 buttonlight_set_timeout(global_settings.buttonlight_timeout);
823 #endif
824 #ifdef HAVE_DISK_STORAGE
825 storage_spindown(global_settings.disk_spindown);
826 #endif
827 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
828 dac_line_in(global_settings.line_in);
829 #endif
830 set_poweroff_timeout(global_settings.poweroff);
832 set_battery_capacity(global_settings.battery_capacity);
833 #if BATTERY_TYPES_COUNT > 1
834 set_battery_type(global_settings.battery_type);
835 #endif
837 #ifdef HAVE_LCD_BITMAP
838 #ifdef HAVE_LCD_INVERT
839 lcd_set_invert_display(global_settings.invert);
840 #endif
841 #ifdef HAVE_LCD_FLIP
842 lcd_set_flip(global_settings.flip_display);
843 button_set_flip(global_settings.flip_display);
844 #endif
845 lcd_update(); /* refresh after flipping the screen */
846 settings_apply_pm_range();
847 peak_meter_init_times(
848 global_settings.peak_meter_release, global_settings.peak_meter_hold,
849 global_settings.peak_meter_clip_hold);
850 #endif
852 #ifdef HAVE_SPEAKER
853 audiohw_enable_speaker(global_settings.speaker_enabled);
854 #endif
856 if (read_disk)
859 #ifdef HAVE_LCD_BITMAP
860 /* fonts need to be loaded before the WPS */
861 if ( global_settings.font_file[0]) {
862 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
863 global_settings.font_file);
864 if (font_load(buf) == NULL)
865 font_reset();
867 else
868 font_reset();
870 if ( global_settings.kbd_file[0]) {
871 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
872 global_settings.kbd_file);
873 load_kbd(buf);
875 else
876 load_kbd(NULL);
877 #endif
880 #if LCD_DEPTH > 1
881 if ( global_settings.backdrop_file[0] &&
882 global_settings.backdrop_file[0] != 0xff ) {
883 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
884 global_settings.backdrop_file);
885 backdrop_load(BACKDROP_MAIN, buf);
886 } else {
887 backdrop_unload(BACKDROP_MAIN);
889 #endif
891 FOR_NB_SCREENS(screen)
892 screens[screen].backdrop_show(BACKDROP_MAIN);
894 if ( global_settings.lang_file[0]) {
895 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
896 global_settings.lang_file);
897 lang_load(buf);
898 talk_init(); /* use voice of same language */
901 /* reload wpses */
902 settings_apply_skins();
904 /* load the icon set */
905 icons_init();
907 #ifdef HAVE_LCD_COLOR
908 if (global_settings.colors_file[0])
909 read_color_theme_file();
910 #endif
913 #ifdef HAVE_LCD_COLOR
914 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
915 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
916 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
917 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
918 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
919 #endif
921 #ifdef HAVE_LCD_BITMAP
922 lcd_scroll_step(global_settings.scroll_step);
923 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
924 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
925 #else
926 lcd_jump_scroll(global_settings.jump_scroll);
927 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
928 #endif
929 lcd_bidir_scroll(global_settings.bidir_limit);
930 lcd_scroll_delay(global_settings.scroll_delay);
933 set_codepage(global_settings.default_codepage);
935 #if CONFIG_CODEC == SWCODEC
936 audio_set_crossfade(global_settings.crossfade);
937 dsp_set_replaygain();
938 dsp_set_crossfeed(global_settings.crossfeed);
939 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
940 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
941 global_settings.crossfeed_hf_attenuation,
942 global_settings.crossfeed_hf_cutoff);
944 /* Configure software equalizer, hardware eq is handled in audio_init() */
945 dsp_set_eq(global_settings.eq_enabled);
946 dsp_set_eq_precut(global_settings.eq_precut);
947 for(i = 0; i < 5; i++) {
948 dsp_set_eq_coefs(i);
951 dsp_dither_enable(global_settings.dithering_enabled);
952 dsp_timestretch_enable(global_settings.timestretch_enabled);
953 dsp_set_limiter(global_settings.limiter_level);
954 #endif
956 #ifdef HAVE_SPDIF_POWER
957 spdif_power_enable(global_settings.spdif_enable);
958 #endif
960 #ifdef HAVE_BACKLIGHT
961 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
962 #ifdef HAVE_REMOTE_LCD
963 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
964 #endif
965 #ifdef HAS_BUTTON_HOLD
966 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
967 #endif
968 #ifdef HAVE_LCD_SLEEP_SETTING
969 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
970 #endif
971 #endif /* HAVE_BACKLIGHT */
973 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
974 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
975 #endif
977 #ifdef HAVE_USB_CHARGING_ENABLE
978 usb_charging_enable(global_settings.usb_charging);
979 #endif
981 #ifdef HAVE_TOUCHSCREEN
982 touchscreen_set_mode(global_settings.touch_mode);
983 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
984 #endif
986 /* This should stay last */
987 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
988 enc_global_settings_apply();
989 #endif
990 #ifdef HAVE_LCD_BITMAP
991 viewportmanager_theme_changed(THEME_ALL);
992 #endif
997 * reset all settings to their default value
999 void reset_setting(const struct settings_list *setting, void *var)
1001 switch (setting->flags&F_T_MASK)
1003 case F_T_CUSTOM:
1004 setting->custom_setting->set_default(setting->setting,
1005 setting->default_val.custom);
1006 break;
1007 case F_T_INT:
1008 case F_T_UINT:
1009 if (setting->flags&F_DEF_ISFUNC)
1010 *(int*)var = setting->default_val.func();
1011 else if (setting->flags&F_T_SOUND)
1012 *(int*)var = sound_default(setting->sound_setting->setting);
1013 else *(int*)var = setting->default_val.int_;
1014 break;
1015 case F_T_BOOL:
1016 *(bool*)var = setting->default_val.bool_;
1017 break;
1018 case F_T_CHARPTR:
1019 case F_T_UCHARPTR:
1020 strlcpy((char*)var, setting->default_val.charptr,
1021 setting->filename_setting->max_len);
1022 break;
1026 void settings_reset(void)
1028 int i;
1030 for(i=0; i<nb_settings; i++)
1031 reset_setting(&settings[i], settings[i].setting);
1032 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1033 enc_global_settings_reset();
1034 #endif
1037 /** Changing setting values **/
1038 const struct settings_list* find_setting(const void* variable, int *id)
1040 int i;
1041 for(i=0;i<nb_settings;i++)
1043 if (settings[i].setting == variable)
1045 if (id)
1046 *id = i;
1047 return &settings[i];
1050 return NULL;
1053 bool set_bool(const char* string, const bool* variable )
1055 return set_bool_options(string, variable,
1056 (char *)STR(LANG_SET_BOOL_YES),
1057 (char *)STR(LANG_SET_BOOL_NO),
1058 NULL);
1062 bool set_bool_options(const char* string, const bool* variable,
1063 const char* yes_str, int yes_voice,
1064 const char* no_str, int no_voice,
1065 void (*function)(bool))
1067 struct opt_items names[] = {
1068 {(unsigned const char *)no_str, no_voice},
1069 {(unsigned const char *)yes_str, yes_voice}
1071 bool result;
1073 result = set_option(string, variable, BOOL, names, 2,
1074 (void (*)(int))function);
1075 return result;
1078 bool set_int(const unsigned char* string,
1079 const char* unit,
1080 int voice_unit,
1081 const int* variable,
1082 void (*function)(int),
1083 int step,
1084 int min,
1085 int max,
1086 void (*formatter)(char*, size_t, int, const char*) )
1088 return set_int_ex(string, unit, voice_unit, variable, function,
1089 step, min, max, formatter, NULL);
1092 bool set_int_ex(const unsigned char* string,
1093 const char* unit,
1094 int voice_unit,
1095 const int* variable,
1096 void (*function)(int),
1097 int step,
1098 int min,
1099 int max,
1100 void (*formatter)(char*, size_t, int, const char*),
1101 int32_t (*get_talk_id)(int, int))
1103 (void)unit;
1104 struct settings_list item;
1105 struct int_setting data = {
1106 function, voice_unit, min, max, step,
1107 formatter, get_talk_id
1109 item.int_setting = &data;
1110 item.flags = F_INT_SETTING|F_T_INT;
1111 item.lang_id = -1;
1112 item.cfg_vals = (char*)string;
1113 item.setting = (void *)variable;
1114 return option_screen(&item,
1115 viewport_get_current_vp(), false, NULL);
1119 static const struct opt_items *set_option_options;
1120 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1122 (void)unit;
1123 const unsigned char *text = set_option_options[item].string;
1124 strlcpy(buf, P2STR(text), size);
1126 static int32_t set_option_get_talk_id(int value, int unit)
1128 (void)unit;
1129 return set_option_options[value].voice_id;
1131 bool set_option(const char* string, const void* variable, enum optiontype type,
1132 const struct opt_items* options,
1133 int numoptions, void (*function)(int))
1135 int temp;
1136 struct settings_list item;
1137 struct int_setting data = {
1138 function, UNIT_INT, 0, numoptions-1, 1,
1139 set_option_formatter, set_option_get_talk_id
1141 set_option_options = options;
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 = &temp;
1147 if (type == BOOL)
1148 temp = *(bool*)variable? 1: 0;
1149 else
1150 temp = *(int*)variable;
1151 if (!option_screen(&item, NULL, false, NULL))
1153 if (type == BOOL)
1154 *(bool*)variable = (temp == 1);
1155 else
1156 *(int*)variable = temp;
1157 return false;
1159 return true;
1163 void set_file(const char* filename, char* setting, int maxlen)
1165 const char* fptr = strrchr(filename,'/');
1166 int len;
1167 int extlen = 0;
1168 const char* ptr;
1170 if (!fptr)
1171 return;
1173 fptr++;
1175 len = strlen(fptr);
1176 ptr = fptr + len;
1177 while ((*ptr != '.') && (ptr != fptr)) {
1178 extlen++;
1179 ptr--;
1181 if(ptr == fptr) extlen = 0;
1183 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1184 (len-extlen > maxlen))
1185 return;
1187 strlcpy(setting, fptr, len-extlen+1);
1189 settings_save();