Correct beast manual install instructions in Windows.
[kugel-rb.git] / apps / settings.c
blobf12bd92bfd64763af4b97192691c2fdc164f0cb1
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"
74 #include "statusbar-skinned.h"
76 #if CONFIG_CODEC == MAS3507D
77 void dac_line_in(bool enable);
78 #endif
79 struct user_settings global_settings;
80 struct system_status global_status;
82 #if CONFIG_CODEC == SWCODEC
83 #include "dsp.h"
84 #include "playback.h"
85 #ifdef HAVE_RECORDING
86 #include "enc_config.h"
87 #endif
88 #endif /* CONFIG_CODEC == SWCODEC */
90 #define NVRAM_BLOCK_SIZE 44
92 #ifdef HAVE_LCD_BITMAP
93 #define MAX_LINES 10
94 #else
95 #define MAX_LINES 2
96 #endif
98 #ifdef HAVE_REMOTE_LCD
99 #include "lcd-remote.h"
100 #endif
102 long lasttime = 0;
104 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
105 /* NVRAM is set out as
106 [0] 'R'
107 [1] 'b'
108 [2] version
109 [3] stored variable count
110 [4-7] crc32 checksum
111 [8-NVRAM_BLOCK_SIZE] data
113 #define NVRAM_DATA_START 8
114 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
115 static char nvram_buffer[NVRAM_BLOCK_SIZE];
117 static bool read_nvram_data(char* buf, int max_len)
119 unsigned crc32 = 0xffffffff;
120 int var_count = 0, i = 0, buf_pos = 0;
121 #ifndef HAVE_RTC_RAM
122 int fd = open(NVRAM_FILE,O_RDONLY);
123 if (fd < 0)
124 return false;
125 memset(buf,0,max_len);
126 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
127 return false;
128 close(fd);
129 #else
130 memset(buf,0,max_len);
131 /* read rtc block */
132 for (i=0; i < max_len; i++ )
133 buf[i] = rtc_read(0x14+i);
134 #endif
135 /* check magic, version */
136 if ((buf[0] != 'R') || (buf[1] != 'b')
137 || (buf[2] != NVRAM_CONFIG_VERSION))
138 return false;
139 /* check crc32 */
140 crc32 = crc_32(&buf[NVRAM_DATA_START],
141 max_len-NVRAM_DATA_START-1,0xffffffff);
142 if (memcmp(&crc32,&buf[4],4))
143 return false;
144 /* all good, so read in the settings */
145 var_count = buf[3];
146 buf_pos = NVRAM_DATA_START;
147 for(i=0; i<nb_settings; i++)
149 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
150 >>F_NVRAM_MASK_SHIFT;
151 if (nvram_bytes)
153 if ((var_count>0) && (buf_pos<max_len))
155 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
156 buf_pos += nvram_bytes;
157 var_count--;
159 else /* should only happen when new items are added to the end */
161 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
165 return true;
167 static bool write_nvram_data(char* buf, int max_len)
169 unsigned crc32 = 0xffffffff;
170 int i = 0, buf_pos = 0;
171 char var_count = 0;
172 #ifndef HAVE_RTC_RAM
173 int fd;
174 #endif
175 memset(buf,0,max_len);
176 /* magic, version */
177 buf[0] = 'R'; buf[1] = 'b';
178 buf[2] = NVRAM_CONFIG_VERSION;
179 buf_pos = NVRAM_DATA_START;
180 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
182 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
183 >>F_NVRAM_MASK_SHIFT;
184 if (nvram_bytes)
186 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
187 buf_pos += nvram_bytes;
188 var_count++;
191 /* count and crc32 */
192 buf[3] = var_count;
193 crc32 = crc_32(&buf[NVRAM_DATA_START],
194 max_len-NVRAM_DATA_START-1,0xffffffff);
195 memcpy(&buf[4],&crc32,4);
196 #ifndef HAVE_RTC_RAM
197 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
198 if (fd >= 0)
200 int len = write(fd,buf,max_len);
201 close(fd);
202 if (len < 8)
203 return false;
205 #else
206 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
207 that it would write a number of bytes at a time since the RTC chip
208 supports that, but this will have to do for now 8-) */
209 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
210 int r = rtc_write(0x14+i, buf[i]);
211 if (r)
212 return false;
214 #endif
215 return true;
218 /** Reading from a config file **/
220 * load settings from disk or RTC RAM
222 void settings_load(int which)
224 if (which&SETTINGS_RTC)
225 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
226 if (which&SETTINGS_HD)
228 settings_load_config(CONFIGFILE,false);
229 settings_load_config(FIXEDSETTINGSFILE,false);
233 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
235 const char* start = settings[setting_id].cfg_vals;
236 char* end = NULL;
237 char temp[MAX_PATH];
238 int count = 0;
239 while (1)
241 end = strchr(start, ',');
242 if (!end)
244 if (!strcmp(str, start))
246 *out = count;
247 return true;
249 else return false;
251 strlcpy(temp, start, end-start+1);
252 if (!strcmp(str, temp))
254 *out = count;
255 return true;
257 start = end +1;
258 count++;
260 return false;
263 bool settings_load_config(const char* file, bool apply)
265 int fd;
266 char line[128];
267 char* name;
268 char* value;
269 int i;
270 fd = open_utf8(file, O_RDONLY);
271 if (fd < 0)
272 return false;
274 while (read_line(fd, line, sizeof line) > 0)
276 if (!settings_parseline(line, &name, &value))
277 continue;
278 for(i=0; i<nb_settings; i++)
280 if (settings[i].cfg_name == NULL)
281 continue;
282 if (!strcasecmp(name,settings[i].cfg_name))
284 switch (settings[i].flags&F_T_MASK)
286 case F_T_CUSTOM:
287 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
288 break;
289 case F_T_INT:
290 case F_T_UINT:
291 #ifdef HAVE_LCD_COLOR
292 if (settings[i].flags&F_RGB)
293 hex_to_rgb(value, (int*)settings[i].setting);
294 else
295 #endif
296 if (settings[i].cfg_vals == NULL)
298 *(int*)settings[i].setting = atoi(value);
300 else
302 int temp, *v = (int*)settings[i].setting;
303 bool found = cfg_string_to_int(i, &temp, value);
304 if (found)
306 if (settings[i].flags&F_TABLE_SETTING)
307 *v = settings[i].table_setting->values[temp];
308 else
309 *v = temp;
311 else
312 { /* atoi breaks choice settings because they
313 * don't have int-like values, and would
314 * fall back to the first value (i.e. 0)
315 * due to atoi */
316 if (!(settings[i].flags&F_CHOICE_SETTING))
317 *v = atoi(value);
320 break;
321 case F_T_BOOL:
323 int temp;
324 if (cfg_string_to_int(i,&temp,value))
325 *(bool*)settings[i].setting = (temp!=0);
326 if (settings[i].bool_setting->option_callback)
327 settings[i].bool_setting->option_callback(temp!=0);
328 break;
330 case F_T_CHARPTR:
331 case F_T_UCHARPTR:
333 char storage[MAX_PATH];
334 if (settings[i].filename_setting->prefix)
336 int len = strlen(settings[i].filename_setting->prefix);
337 if (!strncasecmp(value,
338 settings[i].filename_setting->prefix,
339 len))
341 strlcpy(storage, &value[len], MAX_PATH);
343 else strlcpy(storage, value, MAX_PATH);
345 else strlcpy(storage, value, MAX_PATH);
346 if (settings[i].filename_setting->suffix)
348 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
349 if (s) *s = '\0';
351 strlcpy((char*)settings[i].setting, storage,
352 settings[i].filename_setting->max_len);
353 break;
356 break;
357 } /* if (!strcmp(name,settings[i].cfg_name)) */
358 } /* for(...) */
359 } /* while(...) */
361 close(fd);
362 settings_save();
363 if (apply)
364 settings_apply(true);
365 return true;
368 /** Writing to a config file and saving settings **/
370 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
372 int flags = settings[setting_id].flags;
373 const char* start = settings[setting_id].cfg_vals;
374 char* end = NULL;
375 int count = 0;
377 if ((flags&F_T_MASK)==F_T_INT &&
378 flags&F_TABLE_SETTING)
380 const int *value = settings[setting_id].table_setting->values;
381 while (start)
383 end = strchr(start,',');
384 if (value[count] == val)
386 if (end == NULL)
387 strlcpy(buf, start, buf_len);
388 else
390 int len = (buf_len > (end-start))? end-start: buf_len;
391 strlcpy(buf, start, len+1);
393 return true;
395 count++;
397 if (end)
398 start = end+1;
399 else
400 break;
402 return false;
405 while (count < val)
407 start = strchr(start,',');
408 if (!start)
409 return false;
410 count++;
411 start++;
413 end = strchr(start,',');
414 if (end == NULL)
415 strlcpy(buf, start, buf_len);
416 else
418 int len = (buf_len > (end-start))? end-start: buf_len;
419 strlcpy(buf, start, len+1);
421 return true;
424 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
426 switch (settings[i].flags&F_T_MASK)
428 case F_T_CUSTOM:
429 settings[i].custom_setting->write_to_cfg(settings[i].setting,
430 buf, buf_len);
431 break;
432 case F_T_INT:
433 case F_T_UINT:
434 #ifdef HAVE_LCD_COLOR
435 if (settings[i].flags&F_RGB)
437 int colour = *(int*)settings[i].setting;
438 snprintf(buf,buf_len,"%02x%02x%02x",
439 (int)RGB_UNPACK_RED(colour),
440 (int)RGB_UNPACK_GREEN(colour),
441 (int)RGB_UNPACK_BLUE(colour));
443 else
444 #endif
445 if (settings[i].cfg_vals == NULL)
447 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
449 else
451 if (cfg_int_to_string(i, *(int*)settings[i].setting,
452 buf, buf_len) == false)
454 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
456 else
457 return false;
459 break;
460 case F_T_BOOL:
461 cfg_int_to_string(i,
462 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
463 break;
464 case F_T_CHARPTR:
465 case F_T_UCHARPTR:
466 if (((char*)settings[i].setting)[0]
467 && settings[i].filename_setting->prefix)
469 snprintf(buf,buf_len,"%s%s%s",
470 settings[i].filename_setting->prefix,
471 (char*)settings[i].setting,
472 settings[i].filename_setting->suffix);
474 else strlcpy(buf,(char*)settings[i].setting,
475 settings[i].filename_setting->max_len);
476 break;
477 } /* switch () */
478 return true;
482 static bool is_changed(int setting_id)
484 const struct settings_list *setting = &settings[setting_id];
485 switch (setting->flags&F_T_MASK)
487 case F_T_CUSTOM:
488 return setting->custom_setting->is_changed(setting->setting,
489 setting->default_val.custom);
490 break;
491 case F_T_INT:
492 case F_T_UINT:
493 if (setting->flags&F_DEF_ISFUNC)
495 if (*(int*)setting->setting == setting->default_val.func())
496 return false;
498 else if (setting->flags&F_T_SOUND)
500 if (*(int*)setting->setting ==
501 sound_default(setting->sound_setting->setting))
502 return false;
504 else if (*(int*)setting->setting == setting->default_val.int_)
505 return false;
506 break;
507 case F_T_BOOL:
508 if (*(bool*)setting->setting == setting->default_val.bool_)
509 return false;
510 break;
511 case F_T_CHARPTR:
512 case F_T_UCHARPTR:
513 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
514 return false;
515 break;
517 return true;
520 static bool settings_write_config(const char* filename, int options)
522 int i;
523 int fd;
524 char value[MAX_PATH];
525 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
526 if (fd < 0)
527 return false;
528 fdprintf(fd, "# .cfg file created by rockbox %s - "
529 "http://www.rockbox.org\r\n\r\n", appsversion);
530 for(i=0; i<nb_settings; i++)
532 if (settings[i].cfg_name == NULL)
533 continue;
534 value[0] = '\0';
536 switch (options)
538 case SETTINGS_SAVE_CHANGED:
539 if (!is_changed(i))
540 continue;
541 break;
542 case SETTINGS_SAVE_SOUND:
543 if ((settings[i].flags&F_SOUNDSETTING) == 0)
544 continue;
545 break;
546 case SETTINGS_SAVE_THEME:
547 if ((settings[i].flags&F_THEMESETTING) == 0)
548 continue;
549 break;
550 #ifdef HAVE_RECORDING
551 case SETTINGS_SAVE_RECPRESETS:
552 if ((settings[i].flags&F_RECSETTING) == 0)
553 continue;
554 break;
555 #endif
556 #if CONFIG_CODEC == SWCODEC
557 case SETTINGS_SAVE_EQPRESET:
558 if ((settings[i].flags&F_EQSETTING) == 0)
559 continue;
560 break;
561 #endif
564 cfg_to_string(i, value, MAX_PATH);
565 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
566 } /* for(...) */
567 close(fd);
568 return true;
570 #ifndef HAVE_RTC_RAM
571 static void flush_global_status_callback(void *data)
573 (void)data;
574 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
576 #endif
577 static void flush_config_block_callback(void *data)
579 (void)data;
580 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
581 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
585 * persist all runtime user settings to disk or RTC RAM
587 static void update_runtime(void)
589 int elapsed_secs;
591 elapsed_secs = (current_tick - lasttime) / HZ;
592 global_status.runtime += elapsed_secs;
593 lasttime += (elapsed_secs * HZ);
595 if ( global_status.runtime > global_status.topruntime )
596 global_status.topruntime = global_status.runtime;
599 void status_save(void)
601 update_runtime();
602 #ifdef HAVE_RTC_RAM
603 /* this will be done in the storage_callback if
604 target doesnt have rtc ram */
605 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
606 #else
607 register_storage_idle_func(flush_global_status_callback);
608 #endif
611 int settings_save(void)
613 update_runtime();
614 #ifdef HAVE_RTC_RAM
615 /* this will be done in the storage_callback if
616 target doesnt have rtc ram */
617 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
618 #endif
619 register_storage_idle_func(flush_config_block_callback);
620 return 0;
623 bool settings_save_config(int options)
625 char filename[MAX_PATH];
626 char *folder, *namebase;
627 switch (options)
629 case SETTINGS_SAVE_THEME:
630 folder = THEME_DIR;
631 namebase = "theme";
632 break;
633 #ifdef HAVE_RECORDING
634 case SETTINGS_SAVE_RECPRESETS:
635 folder = RECPRESETS_DIR;
636 namebase = "recording";
637 break;
638 #endif
639 #if CONFIG_CODEC == SWCODEC
640 case SETTINGS_SAVE_EQPRESET:
641 folder = EQS_DIR;
642 namebase = "eq";
643 break;
644 #endif
645 case SETTINGS_SAVE_SOUND:
646 folder = ROCKBOX_DIR;
647 namebase = "sound";
648 break;
649 default:
650 folder = ROCKBOX_DIR;
651 namebase = "config";
652 break;
654 create_numbered_filename(filename, folder, namebase, ".cfg", 2
655 IF_CNFN_NUM_(, NULL));
657 /* allow user to modify filename */
658 while (true) {
659 if (!kbd_input(filename, sizeof filename)) {
660 break;
662 else {
663 return false;
667 if (settings_write_config(filename, options))
668 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
669 else
670 splash(HZ, ID2P(LANG_FAILED));
671 return true;
674 /** Apply and Reset settings **/
677 #ifdef HAVE_LCD_BITMAP
679 * Applies the range infos stored in global_settings to
680 * the peak meter.
682 void settings_apply_pm_range(void)
684 int pm_min, pm_max;
686 /* depending on the scale mode (dBfs or percent) the values
687 of global_settings.peak_meter_dbfs have different meanings */
688 if (global_settings.peak_meter_dbfs)
690 /* convert to dBfs * 100 */
691 pm_min = -(((int)global_settings.peak_meter_min) * 100);
692 pm_max = -(((int)global_settings.peak_meter_max) * 100);
694 else
696 /* percent is stored directly -> no conversion */
697 pm_min = global_settings.peak_meter_min;
698 pm_max = global_settings.peak_meter_max;
701 /* apply the range */
702 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
704 #endif /* HAVE_LCD_BITMAP */
706 void sound_settings_apply(void)
708 #if CONFIG_CODEC == SWCODEC
709 sound_set_dsp_callback(dsp_callback);
710 #endif
711 sound_set(SOUND_BASS, global_settings.bass);
712 sound_set(SOUND_TREBLE, global_settings.treble);
713 sound_set(SOUND_BALANCE, global_settings.balance);
714 sound_set(SOUND_VOLUME, global_settings.volume);
715 sound_set(SOUND_CHANNELS, global_settings.channel_config);
716 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
717 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
718 sound_set(SOUND_LOUDNESS, global_settings.loudness);
719 sound_set(SOUND_AVC, global_settings.avc);
720 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
721 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
722 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
723 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
724 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
725 sound_set(SOUND_SUPERBASS, global_settings.superbass);
726 #endif
728 #ifdef HAVE_WM8758
729 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
730 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
731 #endif
736 /* call this after loading a .wps/.rwps pr other skin files, so that the
737 * skin buffer is reset properly
739 void settings_apply_skins(void)
741 char buf[MAX_PATH];
742 /* re-initialize the skin buffer before we start reloading skins */
743 skin_buffer_init();
744 #ifdef HAVE_LCD_BITMAP
745 if ( global_settings.sbs_file[0] &&
746 global_settings.sbs_file[0] != 0xff )
748 snprintf(buf, sizeof buf, SBS_DIR "/%s.sbs",
749 global_settings.sbs_file);
750 sb_skin_data_load(SCREEN_MAIN, buf, true);
752 else
754 sb_skin_data_load(SCREEN_MAIN, NULL, true);
756 #endif
757 if ( global_settings.wps_file[0] &&
758 global_settings.wps_file[0] != 0xff )
760 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
761 global_settings.wps_file);
762 wps_data_load(SCREEN_MAIN, buf, true);
764 else
766 wps_data_load(SCREEN_MAIN, NULL, true);
768 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
769 if ( global_settings.rsbs_file[0] &&
770 global_settings.rsbs_file[0] != 0xff ) {
771 snprintf(buf, sizeof buf, SBS_DIR "/%s.rsbs",
772 global_settings.rsbs_file);
773 sb_skin_data_load(SCREEN_REMOTE, buf, true);
775 else
777 sb_skin_data_load(SCREEN_REMOTE, NULL, true);
779 if ( global_settings.rwps_file[0])
781 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
782 global_settings.rwps_file);
783 wps_data_load(SCREEN_REMOTE, buf, true);
785 else
787 wps_data_load(SCREEN_REMOTE, NULL, true);
789 #endif
790 viewportmanager_theme_changed(THEME_STATUSBAR);
793 void settings_apply(bool read_disk)
796 char buf[64];
797 #if CONFIG_CODEC == SWCODEC
798 int i;
799 #endif
800 int screen;
802 sound_settings_apply();
804 #ifdef HAVE_DISK_STORAGE
805 audio_set_buffer_margin(global_settings.buffer_margin);
806 #endif
808 #ifdef HAVE_LCD_CONTRAST
809 lcd_set_contrast(global_settings.contrast);
810 #endif
811 lcd_scroll_speed(global_settings.scroll_speed);
812 #ifdef HAVE_REMOTE_LCD
813 lcd_remote_set_contrast(global_settings.remote_contrast);
814 lcd_remote_set_invert_display(global_settings.remote_invert);
816 #ifdef HAVE_LCD_FLIP
817 lcd_remote_set_flip(global_settings.remote_flip_display);
818 #endif
820 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
821 lcd_remote_scroll_step(global_settings.remote_scroll_step);
822 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
823 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
824 #ifdef HAVE_REMOTE_LCD_TICKING
825 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
826 #endif
827 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
828 #if CONFIG_CHARGING
829 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
830 #endif
831 #ifdef HAS_REMOTE_BUTTON_HOLD
832 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
833 #endif
834 #endif /* HAVE_REMOTE_LCD */
835 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
836 backlight_set_brightness(global_settings.brightness);
837 #endif
838 #ifdef HAVE_BACKLIGHT
839 backlight_set_timeout(global_settings.backlight_timeout);
840 #if CONFIG_CHARGING
841 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
842 #endif
843 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
844 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
845 backlight_set_fade_in(global_settings.backlight_fade_in);
846 backlight_set_fade_out(global_settings.backlight_fade_out);
847 #endif
848 #endif
849 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
850 buttonlight_set_brightness(global_settings.buttonlight_brightness);
851 #endif
852 #ifdef HAVE_BUTTON_LIGHT
853 buttonlight_set_timeout(global_settings.buttonlight_timeout);
854 #endif
855 #ifdef HAVE_DISK_STORAGE
856 storage_spindown(global_settings.disk_spindown);
857 #endif
858 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
859 dac_line_in(global_settings.line_in);
860 #endif
861 set_poweroff_timeout(global_settings.poweroff);
863 set_battery_capacity(global_settings.battery_capacity);
864 #if BATTERY_TYPES_COUNT > 1
865 set_battery_type(global_settings.battery_type);
866 #endif
868 #ifdef HAVE_LCD_BITMAP
869 #ifdef HAVE_LCD_INVERT
870 lcd_set_invert_display(global_settings.invert);
871 #endif
872 #ifdef HAVE_LCD_FLIP
873 lcd_set_flip(global_settings.flip_display);
874 button_set_flip(global_settings.flip_display);
875 #endif
876 lcd_update(); /* refresh after flipping the screen */
877 settings_apply_pm_range();
878 peak_meter_init_times(
879 global_settings.peak_meter_release, global_settings.peak_meter_hold,
880 global_settings.peak_meter_clip_hold);
881 #endif
883 #ifdef HAVE_SPEAKER
884 audiohw_enable_speaker(global_settings.speaker_enabled);
885 #endif
887 if (read_disk)
890 #ifdef HAVE_LCD_BITMAP
891 /* fonts need to be loaded before the WPS */
892 if ( global_settings.font_file[0]) {
893 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
894 global_settings.font_file);
895 if (font_load(buf) == NULL)
896 font_reset();
898 else
899 font_reset();
901 if ( global_settings.kbd_file[0]) {
902 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
903 global_settings.kbd_file);
904 load_kbd(buf);
906 else
907 load_kbd(NULL);
908 #endif
911 #if LCD_DEPTH > 1
912 if ( global_settings.backdrop_file[0] &&
913 global_settings.backdrop_file[0] != 0xff ) {
914 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
915 global_settings.backdrop_file);
916 backdrop_load(BACKDROP_MAIN, buf);
917 } else {
918 backdrop_unload(BACKDROP_MAIN);
920 #endif
922 FOR_NB_SCREENS(screen)
923 screens[screen].backdrop_show(BACKDROP_MAIN);
925 if ( global_settings.lang_file[0]) {
926 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
927 global_settings.lang_file);
928 lang_core_load(buf);
929 talk_init(); /* use voice of same language */
932 /* reload wpses */
933 settings_apply_skins();
935 /* load the icon set */
936 icons_init();
938 #ifdef HAVE_LCD_COLOR
939 if (global_settings.colors_file[0])
940 read_color_theme_file();
941 #endif
944 #ifdef HAVE_LCD_COLOR
945 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
946 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
947 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
948 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
949 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
950 #endif
952 #ifdef HAVE_LCD_BITMAP
953 lcd_scroll_step(global_settings.scroll_step);
954 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
955 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
956 #else
957 lcd_jump_scroll(global_settings.jump_scroll);
958 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
959 #endif
960 lcd_bidir_scroll(global_settings.bidir_limit);
961 lcd_scroll_delay(global_settings.scroll_delay);
964 set_codepage(global_settings.default_codepage);
966 #if CONFIG_CODEC == SWCODEC
967 audio_set_crossfade(global_settings.crossfade);
968 dsp_set_replaygain();
969 dsp_set_crossfeed(global_settings.crossfeed);
970 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
971 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
972 global_settings.crossfeed_hf_attenuation,
973 global_settings.crossfeed_hf_cutoff);
975 /* Configure software equalizer, hardware eq is handled in audio_init() */
976 dsp_set_eq(global_settings.eq_enabled);
977 dsp_set_eq_precut(global_settings.eq_precut);
978 for(i = 0; i < 5; i++) {
979 dsp_set_eq_coefs(i);
982 dsp_dither_enable(global_settings.dithering_enabled);
983 dsp_timestretch_enable(global_settings.timestretch_enabled);
984 dsp_set_compressor(global_settings.compressor_threshold,
985 global_settings.compressor_ratio,
986 global_settings.compressor_makeup_gain,
987 global_settings.compressor_knee,
988 global_settings.compressor_release_time);
989 #endif
991 #ifdef HAVE_SPDIF_POWER
992 spdif_power_enable(global_settings.spdif_enable);
993 #endif
995 #ifdef HAVE_BACKLIGHT
996 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
997 #ifdef HAVE_REMOTE_LCD
998 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
999 #endif
1000 #ifdef HAS_BUTTON_HOLD
1001 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
1002 #endif
1003 #ifdef HAVE_LCD_SLEEP_SETTING
1004 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
1005 #endif
1006 #endif /* HAVE_BACKLIGHT */
1008 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1009 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1010 #endif
1012 #ifdef HAVE_USB_CHARGING_ENABLE
1013 usb_charging_enable(global_settings.usb_charging);
1014 #endif
1016 #ifdef HAVE_TOUCHSCREEN
1017 touchscreen_set_mode(global_settings.touch_mode);
1018 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1019 #endif
1021 /* This should stay last */
1022 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1023 enc_global_settings_apply();
1024 #endif
1025 #ifdef HAVE_LCD_BITMAP
1026 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1027 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE);
1028 #endif
1033 * reset all settings to their default value
1035 void reset_setting(const struct settings_list *setting, void *var)
1037 switch (setting->flags&F_T_MASK)
1039 case F_T_CUSTOM:
1040 setting->custom_setting->set_default(setting->setting,
1041 setting->default_val.custom);
1042 break;
1043 case F_T_INT:
1044 case F_T_UINT:
1045 if (setting->flags&F_DEF_ISFUNC)
1046 *(int*)var = setting->default_val.func();
1047 else if (setting->flags&F_T_SOUND)
1048 *(int*)var = sound_default(setting->sound_setting->setting);
1049 else *(int*)var = setting->default_val.int_;
1050 break;
1051 case F_T_BOOL:
1052 *(bool*)var = setting->default_val.bool_;
1053 break;
1054 case F_T_CHARPTR:
1055 case F_T_UCHARPTR:
1056 strlcpy((char*)var, setting->default_val.charptr,
1057 setting->filename_setting->max_len);
1058 break;
1062 void settings_reset(void)
1064 int i;
1066 for(i=0; i<nb_settings; i++)
1067 reset_setting(&settings[i], settings[i].setting);
1068 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1069 enc_global_settings_reset();
1070 #endif
1073 /** Changing setting values **/
1074 const struct settings_list* find_setting(const void* variable, int *id)
1076 int i;
1077 for(i=0;i<nb_settings;i++)
1079 if (settings[i].setting == variable)
1081 if (id)
1082 *id = i;
1083 return &settings[i];
1086 return NULL;
1089 bool set_bool(const char* string, const bool* variable )
1091 return set_bool_options(string, variable,
1092 (char *)STR(LANG_SET_BOOL_YES),
1093 (char *)STR(LANG_SET_BOOL_NO),
1094 NULL);
1098 bool set_bool_options(const char* string, const bool* variable,
1099 const char* yes_str, int yes_voice,
1100 const char* no_str, int no_voice,
1101 void (*function)(bool))
1103 struct opt_items names[] = {
1104 {(unsigned const char *)no_str, no_voice},
1105 {(unsigned const char *)yes_str, yes_voice}
1107 bool result;
1109 result = set_option(string, variable, BOOL, names, 2,
1110 (void (*)(int))function);
1111 return result;
1114 bool set_int(const unsigned char* string,
1115 const char* unit,
1116 int voice_unit,
1117 const int* variable,
1118 void (*function)(int),
1119 int step,
1120 int min,
1121 int max,
1122 const char* (*formatter)(char*, size_t, int, const char*) )
1124 return set_int_ex(string, unit, voice_unit, variable, function,
1125 step, min, max, formatter, NULL);
1128 bool set_int_ex(const unsigned char* string,
1129 const char* unit,
1130 int voice_unit,
1131 const int* variable,
1132 void (*function)(int),
1133 int step,
1134 int min,
1135 int max,
1136 const char* (*formatter)(char*, size_t, int, const char*),
1137 int32_t (*get_talk_id)(int, int))
1139 (void)unit;
1140 struct settings_list item;
1141 struct int_setting data = {
1142 function, voice_unit, min, max, step,
1143 formatter, get_talk_id
1145 item.int_setting = &data;
1146 item.flags = F_INT_SETTING|F_T_INT;
1147 item.lang_id = -1;
1148 item.cfg_vals = (char*)string;
1149 item.setting = (void *)variable;
1150 return option_screen(&item,
1151 viewport_get_current_vp(), false, NULL);
1155 static const struct opt_items *set_option_options;
1156 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1158 (void)buf, (void)unit, (void)size;
1159 return P2STR(set_option_options[item].string);
1162 static int32_t set_option_get_talk_id(int value, int unit)
1164 (void)unit;
1165 return set_option_options[value].voice_id;
1168 bool set_option(const char* string, const void* variable, enum optiontype type,
1169 const struct opt_items* options,
1170 int numoptions, void (*function)(int))
1172 int temp;
1173 struct settings_list item;
1174 struct int_setting data = {
1175 function, UNIT_INT, 0, numoptions-1, 1,
1176 set_option_formatter, set_option_get_talk_id
1178 set_option_options = options;
1179 item.int_setting = &data;
1180 item.flags = F_INT_SETTING|F_T_INT;
1181 item.lang_id = -1;
1182 item.cfg_vals = (char*)string;
1183 item.setting = &temp;
1184 if (type == BOOL)
1185 temp = *(bool*)variable? 1: 0;
1186 else
1187 temp = *(int*)variable;
1188 if (!option_screen(&item, NULL, false, NULL))
1190 if (type == BOOL)
1191 *(bool*)variable = (temp == 1);
1192 else
1193 *(int*)variable = temp;
1194 return false;
1196 return true;
1200 void set_file(const char* filename, char* setting, int maxlen)
1202 const char* fptr = strrchr(filename,'/');
1203 int len;
1204 int extlen = 0;
1205 const char* ptr;
1207 if (!fptr)
1208 return;
1210 fptr++;
1212 len = strlen(fptr);
1213 ptr = fptr + len;
1214 while ((*ptr != '.') && (ptr != fptr)) {
1215 extlen++;
1216 ptr--;
1218 if(ptr == fptr) extlen = 0;
1220 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1221 (len-extlen > maxlen))
1222 return;
1224 strlcpy(setting, fptr, len-extlen+1);
1226 settings_save();