ARM DSP: fore some reason I neglected dsp_downsample completely. Do a small reordering.
[kugel-rb.git] / apps / settings.c
blob4901957263cff56b63cc836064e2c70521a2eb9a
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 "strlcpy.h"
37 #include "strcasestr.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 "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 "viewport.h"
72 #include "statusbar-skinned.h"
73 #include "bootchart.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, 0666);
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)
364 settings_apply(true);
365 settings_apply_skins();
367 return true;
370 /** Writing to a config file and saving settings **/
372 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
374 int flags = settings[setting_id].flags;
375 const char* start = settings[setting_id].cfg_vals;
376 char* end = NULL;
377 int count = 0;
379 if ((flags&F_T_MASK)==F_T_INT &&
380 flags&F_TABLE_SETTING)
382 const int *value = settings[setting_id].table_setting->values;
383 while (start)
385 end = strchr(start,',');
386 if (value[count] == val)
388 if (end == NULL)
389 strlcpy(buf, start, buf_len);
390 else
392 int len = (buf_len > (end-start))? end-start: buf_len;
393 strlcpy(buf, start, len+1);
395 return true;
397 count++;
399 if (end)
400 start = end+1;
401 else
402 break;
404 return false;
407 while (count < val)
409 start = strchr(start,',');
410 if (!start)
411 return false;
412 count++;
413 start++;
415 end = strchr(start,',');
416 if (end == NULL)
417 strlcpy(buf, start, buf_len);
418 else
420 int len = (buf_len > (end-start))? end-start: buf_len;
421 strlcpy(buf, start, len+1);
423 return true;
426 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
428 switch (settings[i].flags&F_T_MASK)
430 case F_T_CUSTOM:
431 settings[i].custom_setting->write_to_cfg(settings[i].setting,
432 buf, buf_len);
433 break;
434 case F_T_INT:
435 case F_T_UINT:
436 #ifdef HAVE_LCD_COLOR
437 if (settings[i].flags&F_RGB)
439 int colour = *(int*)settings[i].setting;
440 snprintf(buf,buf_len,"%02x%02x%02x",
441 (int)RGB_UNPACK_RED(colour),
442 (int)RGB_UNPACK_GREEN(colour),
443 (int)RGB_UNPACK_BLUE(colour));
445 else
446 #endif
447 if (settings[i].cfg_vals == NULL)
449 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
451 else
453 if (cfg_int_to_string(i, *(int*)settings[i].setting,
454 buf, buf_len) == false)
456 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
458 else
459 return false;
461 break;
462 case F_T_BOOL:
463 cfg_int_to_string(i,
464 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
465 break;
466 case F_T_CHARPTR:
467 case F_T_UCHARPTR:
468 if (((char*)settings[i].setting)[0]
469 && settings[i].filename_setting->prefix)
471 if (((char*)settings[i].setting)[0] == '-')
473 buf[0] = '-';
474 buf[1] = '\0';
476 else
478 snprintf(buf,buf_len,"%s%s%s",
479 settings[i].filename_setting->prefix,
480 (char*)settings[i].setting,
481 settings[i].filename_setting->suffix);
484 else strlcpy(buf,(char*)settings[i].setting,
485 settings[i].filename_setting->max_len);
486 break;
487 } /* switch () */
488 return true;
492 static bool is_changed(int setting_id)
494 const struct settings_list *setting = &settings[setting_id];
495 switch (setting->flags&F_T_MASK)
497 case F_T_CUSTOM:
498 return setting->custom_setting->is_changed(setting->setting,
499 setting->default_val.custom);
500 break;
501 case F_T_INT:
502 case F_T_UINT:
503 if (setting->flags&F_DEF_ISFUNC)
505 if (*(int*)setting->setting == setting->default_val.func())
506 return false;
508 else if (setting->flags&F_T_SOUND)
510 if (*(int*)setting->setting ==
511 sound_default(setting->sound_setting->setting))
512 return false;
514 else if (*(int*)setting->setting == setting->default_val.int_)
515 return false;
516 break;
517 case F_T_BOOL:
518 if (*(bool*)setting->setting == setting->default_val.bool_)
519 return false;
520 break;
521 case F_T_CHARPTR:
522 case F_T_UCHARPTR:
523 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
524 return false;
525 break;
527 return true;
530 static bool settings_write_config(const char* filename, int options)
532 int i;
533 int fd;
534 char value[MAX_PATH];
535 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY, 0666);
536 if (fd < 0)
537 return false;
538 fdprintf(fd, "# .cfg file created by rockbox %s - "
539 "http://www.rockbox.org\r\n\r\n", appsversion);
540 for(i=0; i<nb_settings; i++)
542 if (settings[i].cfg_name == NULL)
543 continue;
544 value[0] = '\0';
546 switch (options)
548 case SETTINGS_SAVE_CHANGED:
549 if (!is_changed(i))
550 continue;
551 break;
552 case SETTINGS_SAVE_SOUND:
553 if ((settings[i].flags&F_SOUNDSETTING) == 0)
554 continue;
555 break;
556 case SETTINGS_SAVE_THEME:
557 if ((settings[i].flags&F_THEMESETTING) == 0)
558 continue;
559 break;
560 #ifdef HAVE_RECORDING
561 case SETTINGS_SAVE_RECPRESETS:
562 if ((settings[i].flags&F_RECSETTING) == 0)
563 continue;
564 break;
565 #endif
566 #if CONFIG_CODEC == SWCODEC
567 case SETTINGS_SAVE_EQPRESET:
568 if ((settings[i].flags&F_EQSETTING) == 0)
569 continue;
570 break;
571 #endif
574 cfg_to_string(i, value, MAX_PATH);
575 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
576 } /* for(...) */
577 close(fd);
578 return true;
580 #ifndef HAVE_RTC_RAM
581 static void flush_global_status_callback(void *data)
583 (void)data;
584 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
586 #endif
587 static void flush_config_block_callback(void *data)
589 (void)data;
590 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
591 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
595 * persist all runtime user settings to disk or RTC RAM
597 static void update_runtime(void)
599 int elapsed_secs;
601 elapsed_secs = (current_tick - lasttime) / HZ;
602 global_status.runtime += elapsed_secs;
603 lasttime += (elapsed_secs * HZ);
605 if ( global_status.runtime > global_status.topruntime )
606 global_status.topruntime = global_status.runtime;
609 void status_save(void)
611 update_runtime();
612 #ifdef HAVE_RTC_RAM
613 /* this will be done in the storage_callback if
614 target doesnt have rtc ram */
615 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
616 #else
617 register_storage_idle_func(flush_global_status_callback);
618 #endif
621 int settings_save(void)
623 update_runtime();
624 #ifdef HAVE_RTC_RAM
625 /* this will be done in the storage_callback if
626 target doesnt have rtc ram */
627 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
628 #endif
629 register_storage_idle_func(flush_config_block_callback);
630 return 0;
633 bool settings_save_config(int options)
635 char filename[MAX_PATH];
636 char *folder, *namebase;
637 switch (options)
639 case SETTINGS_SAVE_THEME:
640 folder = THEME_DIR;
641 namebase = "theme";
642 break;
643 #ifdef HAVE_RECORDING
644 case SETTINGS_SAVE_RECPRESETS:
645 folder = RECPRESETS_DIR;
646 namebase = "recording";
647 break;
648 #endif
649 #if CONFIG_CODEC == SWCODEC
650 case SETTINGS_SAVE_EQPRESET:
651 folder = EQS_DIR;
652 namebase = "eq";
653 break;
654 #endif
655 case SETTINGS_SAVE_SOUND:
656 folder = ROCKBOX_DIR;
657 namebase = "sound";
658 break;
659 default:
660 folder = ROCKBOX_DIR;
661 namebase = "config";
662 break;
664 create_numbered_filename(filename, folder, namebase, ".cfg", 2
665 IF_CNFN_NUM_(, NULL));
667 /* allow user to modify filename */
668 while (true) {
669 if (!kbd_input(filename, sizeof filename)) {
670 break;
672 else {
673 return false;
677 if (settings_write_config(filename, options))
678 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
679 else
680 splash(HZ, ID2P(LANG_FAILED));
681 return true;
684 /** Apply and Reset settings **/
687 #ifdef HAVE_LCD_BITMAP
689 * Applies the range infos stored in global_settings to
690 * the peak meter.
692 void settings_apply_pm_range(void)
694 int pm_min, pm_max;
696 /* depending on the scale mode (dBfs or percent) the values
697 of global_settings.peak_meter_dbfs have different meanings */
698 if (global_settings.peak_meter_dbfs)
700 /* convert to dBfs * 100 */
701 pm_min = -(((int)global_settings.peak_meter_min) * 100);
702 pm_max = -(((int)global_settings.peak_meter_max) * 100);
704 else
706 /* percent is stored directly -> no conversion */
707 pm_min = global_settings.peak_meter_min;
708 pm_max = global_settings.peak_meter_max;
711 /* apply the range */
712 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
714 #endif /* HAVE_LCD_BITMAP */
716 void sound_settings_apply(void)
718 #if CONFIG_CODEC == SWCODEC
719 sound_set_dsp_callback(dsp_callback);
720 #endif
721 sound_set(SOUND_BASS, global_settings.bass);
722 sound_set(SOUND_TREBLE, global_settings.treble);
723 sound_set(SOUND_BALANCE, global_settings.balance);
724 sound_set(SOUND_VOLUME, global_settings.volume);
725 sound_set(SOUND_CHANNELS, global_settings.channel_config);
726 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
727 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
728 sound_set(SOUND_LOUDNESS, global_settings.loudness);
729 sound_set(SOUND_AVC, global_settings.avc);
730 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
731 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
732 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
733 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
734 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
735 sound_set(SOUND_SUPERBASS, global_settings.superbass);
736 #endif
738 #ifdef HAVE_WM8758
739 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
740 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
741 #endif
745 void settings_apply(bool read_disk)
748 char buf[64];
749 #ifdef HAVE_LCD_BITMAP
750 int rc;
751 #endif
752 #if CONFIG_CODEC == SWCODEC
753 int i;
754 #endif
755 sound_settings_apply();
757 #ifdef HAVE_DISK_STORAGE
758 audio_set_buffer_margin(global_settings.buffer_margin);
759 #endif
761 #ifdef HAVE_LCD_CONTRAST
762 lcd_set_contrast(global_settings.contrast);
763 #endif
764 lcd_scroll_speed(global_settings.scroll_speed);
765 #ifdef HAVE_REMOTE_LCD
766 lcd_remote_set_contrast(global_settings.remote_contrast);
767 lcd_remote_set_invert_display(global_settings.remote_invert);
769 #ifdef HAVE_LCD_FLIP
770 lcd_remote_set_flip(global_settings.remote_flip_display);
771 #endif
773 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
774 lcd_remote_scroll_step(global_settings.remote_scroll_step);
775 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
776 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
777 #ifdef HAVE_REMOTE_LCD_TICKING
778 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
779 #endif
780 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
781 #if CONFIG_CHARGING
782 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
783 #endif
784 #ifdef HAS_REMOTE_BUTTON_HOLD
785 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
786 #endif
787 #endif /* HAVE_REMOTE_LCD */
788 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
789 backlight_set_brightness(global_settings.brightness);
790 #endif
791 #ifdef HAVE_BACKLIGHT
792 backlight_set_timeout(global_settings.backlight_timeout);
793 #if CONFIG_CHARGING
794 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
795 #endif
796 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
797 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
798 backlight_set_fade_in(global_settings.backlight_fade_in);
799 backlight_set_fade_out(global_settings.backlight_fade_out);
800 #endif
801 #endif
802 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
803 buttonlight_set_brightness(global_settings.buttonlight_brightness);
804 #endif
805 #ifdef HAVE_BUTTON_LIGHT
806 buttonlight_set_timeout(global_settings.buttonlight_timeout);
807 #endif
808 #ifdef HAVE_DISK_STORAGE
809 storage_spindown(global_settings.disk_spindown);
810 #endif
811 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
812 dac_line_in(global_settings.line_in);
813 #endif
814 set_poweroff_timeout(global_settings.poweroff);
816 set_battery_capacity(global_settings.battery_capacity);
817 #if BATTERY_TYPES_COUNT > 1
818 set_battery_type(global_settings.battery_type);
819 #endif
821 #ifdef HAVE_LCD_BITMAP
822 #ifdef HAVE_LCD_INVERT
823 lcd_set_invert_display(global_settings.invert);
824 #endif
825 #ifdef HAVE_LCD_FLIP
826 lcd_set_flip(global_settings.flip_display);
827 button_set_flip(global_settings.flip_display);
828 #endif
829 lcd_update(); /* refresh after flipping the screen */
830 settings_apply_pm_range();
831 peak_meter_init_times(
832 global_settings.peak_meter_release, global_settings.peak_meter_hold,
833 global_settings.peak_meter_clip_hold);
834 #endif
836 #ifdef HAVE_SPEAKER
837 audiohw_enable_speaker(global_settings.speaker_enabled);
838 #endif
840 if (read_disk)
842 #ifdef HAVE_LCD_BITMAP
843 /* fonts need to be loaded before the WPS */
844 if (global_settings.font_file[0]
845 && global_settings.font_file[0] != '-') {
846 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
847 global_settings.font_file);
848 CHART2(">font_load ", global_settings.font_file);
849 rc = font_load(NULL, buf);
850 CHART2("<font_load ", global_settings.font_file);
851 if (rc < 0)
852 font_reset(NULL);
854 else
855 font_reset(NULL);
856 #ifdef HAVE_REMOTE_LCD
857 if ( global_settings.remote_font_file[0]
858 && global_settings.remote_font_file[0] != '-') {
859 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
860 global_settings.remote_font_file);
861 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
862 rc = font_load_remoteui(buf);
863 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
864 if (rc < 0)
865 font_load_remoteui(NULL);
867 else
868 font_load_remoteui(NULL);
869 #endif
870 if ( global_settings.kbd_file[0]) {
871 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
872 global_settings.kbd_file);
873 CHART(">load_kbd");
874 load_kbd(buf);
875 CHART("<load_kbd");
877 else
878 load_kbd(NULL);
879 #endif
881 if ( global_settings.lang_file[0]) {
882 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
883 global_settings.lang_file);
884 CHART(">lang_core_load");
885 lang_core_load(buf);
886 CHART("<lang_core_load");
887 CHART(">talk_init");
888 talk_init(); /* use voice of same language */
889 CHART("<talk_init");
892 /* load the icon set */
893 CHART(">icons_init");
894 icons_init();
895 CHART("<icons_init");
897 #ifdef HAVE_LCD_COLOR
898 if (global_settings.colors_file[0])
900 CHART(">read_color_theme_file");
901 read_color_theme_file();
902 CHART("<read_color_theme_file");
904 #endif
906 #ifdef HAVE_LCD_COLOR
907 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
908 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
909 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
910 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
911 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
912 #endif
914 #ifdef HAVE_LCD_BITMAP
915 lcd_scroll_step(global_settings.scroll_step);
916 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
917 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
918 #else
919 lcd_jump_scroll(global_settings.jump_scroll);
920 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
921 #endif
922 lcd_bidir_scroll(global_settings.bidir_limit);
923 lcd_scroll_delay(global_settings.scroll_delay);
926 CHART(">set_codepage");
927 set_codepage(global_settings.default_codepage);
928 CHART("<set_codepage");
930 #if CONFIG_CODEC == SWCODEC
931 #ifdef HAVE_CROSSFADE
932 audio_set_crossfade(global_settings.crossfade);
933 #endif
934 dsp_set_replaygain();
935 dsp_set_crossfeed(global_settings.crossfeed);
936 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
937 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
938 global_settings.crossfeed_hf_attenuation,
939 global_settings.crossfeed_hf_cutoff);
941 /* Configure software equalizer, hardware eq is handled in audio_init() */
942 dsp_set_eq(global_settings.eq_enabled);
943 dsp_set_eq_precut(global_settings.eq_precut);
944 for(i = 0; i < 5; i++) {
945 dsp_set_eq_coefs(i);
948 dsp_dither_enable(global_settings.dithering_enabled);
949 dsp_timestretch_enable(global_settings.timestretch_enabled);
950 dsp_set_compressor(global_settings.compressor_threshold,
951 global_settings.compressor_makeup_gain,
952 global_settings.compressor_ratio,
953 global_settings.compressor_knee,
954 global_settings.compressor_release_time);
955 #endif
957 #ifdef HAVE_SPDIF_POWER
958 spdif_power_enable(global_settings.spdif_enable);
959 #endif
961 #ifdef HAVE_BACKLIGHT
962 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
963 #ifdef HAVE_REMOTE_LCD
964 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
965 #endif
966 #ifdef HAS_BUTTON_HOLD
967 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
968 #endif
969 #ifdef HAVE_LCD_SLEEP_SETTING
970 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
971 #endif
972 #endif /* HAVE_BACKLIGHT */
974 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
975 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
976 #endif
978 #ifdef HAVE_USB_CHARGING_ENABLE
979 usb_charging_enable(global_settings.usb_charging);
980 #endif
982 #ifdef HAVE_TOUCHSCREEN
983 touchscreen_set_mode(global_settings.touch_mode);
984 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
985 #endif
987 /* This should stay last */
988 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
989 enc_global_settings_apply();
990 #endif
991 #ifdef HAVE_LCD_BITMAP
992 /* already called with THEME_STATUSBAR in settings_apply_skins() */
993 CHART(">viewportmanager_theme_changed");
994 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE|THEME_BUTTONBAR);
995 CHART("<viewportmanager_theme_changed");
996 #endif
1001 * reset all settings to their default value
1003 void reset_setting(const struct settings_list *setting, void *var)
1005 switch (setting->flags&F_T_MASK)
1007 case F_T_CUSTOM:
1008 setting->custom_setting->set_default(setting->setting,
1009 setting->default_val.custom);
1010 break;
1011 case F_T_INT:
1012 case F_T_UINT:
1013 if (setting->flags&F_DEF_ISFUNC)
1014 *(int*)var = setting->default_val.func();
1015 else if (setting->flags&F_T_SOUND)
1016 *(int*)var = sound_default(setting->sound_setting->setting);
1017 else *(int*)var = setting->default_val.int_;
1018 break;
1019 case F_T_BOOL:
1020 *(bool*)var = setting->default_val.bool_;
1021 break;
1022 case F_T_CHARPTR:
1023 case F_T_UCHARPTR:
1024 strlcpy((char*)var, setting->default_val.charptr,
1025 setting->filename_setting->max_len);
1026 break;
1030 void settings_reset(void)
1032 int i;
1034 for(i=0; i<nb_settings; i++)
1035 reset_setting(&settings[i], settings[i].setting);
1036 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1037 enc_global_settings_reset();
1038 #endif
1041 /** Changing setting values **/
1042 const struct settings_list* find_setting(const void* variable, int *id)
1044 int i;
1045 for(i=0;i<nb_settings;i++)
1047 if (settings[i].setting == variable)
1049 if (id)
1050 *id = i;
1051 return &settings[i];
1054 return NULL;
1057 bool set_bool(const char* string, const bool* variable )
1059 return set_bool_options(string, variable,
1060 (char *)STR(LANG_SET_BOOL_YES),
1061 (char *)STR(LANG_SET_BOOL_NO),
1062 NULL);
1066 bool set_bool_options(const char* string, const bool* variable,
1067 const char* yes_str, int yes_voice,
1068 const char* no_str, int no_voice,
1069 void (*function)(bool))
1071 struct opt_items names[] = {
1072 {(unsigned const char *)no_str, no_voice},
1073 {(unsigned const char *)yes_str, yes_voice}
1075 bool result;
1077 result = set_option(string, variable, BOOL, names, 2,
1078 (void (*)(int))function);
1079 return result;
1082 bool set_int(const unsigned char* string,
1083 const char* unit,
1084 int voice_unit,
1085 const int* variable,
1086 void (*function)(int),
1087 int step,
1088 int min,
1089 int max,
1090 const char* (*formatter)(char*, size_t, int, const char*) )
1092 return set_int_ex(string, unit, voice_unit, variable, function,
1093 step, min, max, formatter, NULL);
1096 bool set_int_ex(const unsigned char* string,
1097 const char* unit,
1098 int voice_unit,
1099 const int* variable,
1100 void (*function)(int),
1101 int step,
1102 int min,
1103 int max,
1104 const char* (*formatter)(char*, size_t, int, const char*),
1105 int32_t (*get_talk_id)(int, int))
1107 (void)unit;
1108 struct settings_list item;
1109 struct int_setting data = {
1110 function, voice_unit, min, max, step,
1111 formatter, get_talk_id
1113 item.int_setting = &data;
1114 item.flags = F_INT_SETTING|F_T_INT;
1115 item.lang_id = -1;
1116 item.cfg_vals = (char*)string;
1117 item.setting = (void *)variable;
1118 return option_screen(&item, NULL, false, NULL);
1122 static const struct opt_items *set_option_options;
1123 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1125 (void)buf, (void)unit, (void)size;
1126 return P2STR(set_option_options[item].string);
1129 static int32_t set_option_get_talk_id(int value, int unit)
1131 (void)unit;
1132 return set_option_options[value].voice_id;
1135 bool set_option(const char* string, const void* variable, enum optiontype type,
1136 const struct opt_items* options,
1137 int numoptions, void (*function)(int))
1139 int temp;
1140 struct settings_list item;
1141 struct int_setting data = {
1142 function, UNIT_INT, 0, numoptions-1, 1,
1143 set_option_formatter, set_option_get_talk_id
1145 set_option_options = options;
1146 item.int_setting = &data;
1147 item.flags = F_INT_SETTING|F_T_INT;
1148 item.lang_id = -1;
1149 item.cfg_vals = (char*)string;
1150 item.setting = &temp;
1151 if (type == BOOL)
1152 temp = *(bool*)variable? 1: 0;
1153 else
1154 temp = *(int*)variable;
1155 if (!option_screen(&item, NULL, false, NULL))
1157 if (type == BOOL)
1158 *(bool*)variable = (temp == 1);
1159 else
1160 *(int*)variable = temp;
1161 return false;
1163 return true;
1167 void set_file(const char* filename, char* setting, int maxlen)
1169 const char* fptr = strrchr(filename,'/');
1170 int len;
1171 int extlen = 0;
1172 const char* ptr;
1174 if (!fptr)
1175 return;
1177 fptr++;
1179 len = strlen(fptr);
1180 ptr = fptr + len;
1181 while ((*ptr != '.') && (ptr != fptr)) {
1182 extlen++;
1183 ptr--;
1185 if(ptr == fptr) extlen = 0;
1187 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1188 (len-extlen > maxlen))
1189 return;
1191 strlcpy(setting, fptr, len-extlen+1);
1193 settings_save();