Add Gigabeat S to Rockbox Utility.
[kugel-rb.git] / apps / settings.c
blob412bedc4647d02e1a6b3a20596135a4cccf00969
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Stuart Martin
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include "inttypes.h"
27 #include "config.h"
28 #include "action.h"
29 #include "crc32.h"
30 #include "settings.h"
31 #include "debug.h"
32 #include "usb.h"
33 #include "backlight.h"
34 #include "audio.h"
35 #include "talk.h"
36 #include "string.h"
37 #include "rtc.h"
38 #include "power.h"
39 #include "ata_idle_notify.h"
40 #include "storage.h"
41 #include "screens.h"
42 #include "ctype.h"
43 #include "file.h"
44 #include "system.h"
45 #include "general.h"
46 #include "misc.h"
47 #ifdef HAVE_LCD_BITMAP
48 #include "icons.h"
49 #include "font.h"
50 #include "peakmeter.h"
51 #endif
52 #include "lang.h"
53 #include "language.h"
54 #include "powermgmt.h"
55 #include "sprintf.h"
56 #include "keyboard.h"
57 #include "version.h"
58 #include "sound.h"
59 #include "rbunicode.h"
60 #include "dircache.h"
61 #include "splash.h"
62 #include "list.h"
63 #include "settings_list.h"
64 #include "filetypes.h"
65 #include "option_select.h"
66 #if CONFIG_TUNER
67 #include "radio.h"
68 #endif
69 #include "wps.h"
70 #include "skin_engine/skin_engine.h"
71 #include "viewport.h"
72 #include "statusbar-skinned.h"
74 #if CONFIG_CODEC == MAS3507D
75 void dac_line_in(bool enable);
76 #endif
77 struct user_settings global_settings;
78 struct system_status global_status;
80 #if CONFIG_CODEC == SWCODEC
81 #include "dsp.h"
82 #include "playback.h"
83 #ifdef HAVE_RECORDING
84 #include "enc_config.h"
85 #endif
86 #endif /* CONFIG_CODEC == SWCODEC */
88 #define NVRAM_BLOCK_SIZE 44
90 #ifdef HAVE_LCD_BITMAP
91 #define MAX_LINES 10
92 #else
93 #define MAX_LINES 2
94 #endif
96 #ifdef HAVE_REMOTE_LCD
97 #include "lcd-remote.h"
98 #endif
100 long lasttime = 0;
102 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
103 /* NVRAM is set out as
104 [0] 'R'
105 [1] 'b'
106 [2] version
107 [3] stored variable count
108 [4-7] crc32 checksum
109 [8-NVRAM_BLOCK_SIZE] data
111 #define NVRAM_DATA_START 8
112 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
113 static char nvram_buffer[NVRAM_BLOCK_SIZE];
115 static bool read_nvram_data(char* buf, int max_len)
117 unsigned crc32 = 0xffffffff;
118 int var_count = 0, i = 0, buf_pos = 0;
119 #ifndef HAVE_RTC_RAM
120 int fd = open(NVRAM_FILE,O_RDONLY);
121 if (fd < 0)
122 return false;
123 memset(buf,0,max_len);
124 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
125 return false;
126 close(fd);
127 #else
128 memset(buf,0,max_len);
129 /* read rtc block */
130 for (i=0; i < max_len; i++ )
131 buf[i] = rtc_read(0x14+i);
132 #endif
133 /* check magic, version */
134 if ((buf[0] != 'R') || (buf[1] != 'b')
135 || (buf[2] != NVRAM_CONFIG_VERSION))
136 return false;
137 /* check crc32 */
138 crc32 = crc_32(&buf[NVRAM_DATA_START],
139 max_len-NVRAM_DATA_START-1,0xffffffff);
140 if (memcmp(&crc32,&buf[4],4))
141 return false;
142 /* all good, so read in the settings */
143 var_count = buf[3];
144 buf_pos = NVRAM_DATA_START;
145 for(i=0; i<nb_settings; i++)
147 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
148 >>F_NVRAM_MASK_SHIFT;
149 if (nvram_bytes)
151 if ((var_count>0) && (buf_pos<max_len))
153 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
154 buf_pos += nvram_bytes;
155 var_count--;
157 else /* should only happen when new items are added to the end */
159 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
163 return true;
165 static bool write_nvram_data(char* buf, int max_len)
167 unsigned crc32 = 0xffffffff;
168 int i = 0, buf_pos = 0;
169 char var_count = 0;
170 #ifndef HAVE_RTC_RAM
171 int fd;
172 #endif
173 memset(buf,0,max_len);
174 /* magic, version */
175 buf[0] = 'R'; buf[1] = 'b';
176 buf[2] = NVRAM_CONFIG_VERSION;
177 buf_pos = NVRAM_DATA_START;
178 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
180 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
181 >>F_NVRAM_MASK_SHIFT;
182 if (nvram_bytes)
184 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
185 buf_pos += nvram_bytes;
186 var_count++;
189 /* count and crc32 */
190 buf[3] = var_count;
191 crc32 = crc_32(&buf[NVRAM_DATA_START],
192 max_len-NVRAM_DATA_START-1,0xffffffff);
193 memcpy(&buf[4],&crc32,4);
194 #ifndef HAVE_RTC_RAM
195 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
196 if (fd >= 0)
198 int len = write(fd,buf,max_len);
199 close(fd);
200 if (len < 8)
201 return false;
203 #else
204 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
205 that it would write a number of bytes at a time since the RTC chip
206 supports that, but this will have to do for now 8-) */
207 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
208 int r = rtc_write(0x14+i, buf[i]);
209 if (r)
210 return false;
212 #endif
213 return true;
216 /** Reading from a config file **/
218 * load settings from disk or RTC RAM
220 void settings_load(int which)
222 if (which&SETTINGS_RTC)
223 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
224 if (which&SETTINGS_HD)
226 settings_load_config(CONFIGFILE,false);
227 settings_load_config(FIXEDSETTINGSFILE,false);
231 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
233 const char* start = settings[setting_id].cfg_vals;
234 char* end = NULL;
235 char temp[MAX_PATH];
236 int count = 0;
237 while (1)
239 end = strchr(start, ',');
240 if (!end)
242 if (!strcmp(str, start))
244 *out = count;
245 return true;
247 else return false;
249 strlcpy(temp, start, end-start+1);
250 if (!strcmp(str, temp))
252 *out = count;
253 return true;
255 start = end +1;
256 count++;
258 return false;
261 bool settings_load_config(const char* file, bool apply)
263 int fd;
264 char line[128];
265 char* name;
266 char* value;
267 int i;
268 fd = open_utf8(file, O_RDONLY);
269 if (fd < 0)
270 return false;
272 while (read_line(fd, line, sizeof line) > 0)
274 if (!settings_parseline(line, &name, &value))
275 continue;
276 for(i=0; i<nb_settings; i++)
278 if (settings[i].cfg_name == NULL)
279 continue;
280 if (!strcasecmp(name,settings[i].cfg_name))
282 switch (settings[i].flags&F_T_MASK)
284 case F_T_CUSTOM:
285 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
286 break;
287 case F_T_INT:
288 case F_T_UINT:
289 #ifdef HAVE_LCD_COLOR
290 if (settings[i].flags&F_RGB)
291 hex_to_rgb(value, (int*)settings[i].setting);
292 else
293 #endif
294 if (settings[i].cfg_vals == NULL)
296 *(int*)settings[i].setting = atoi(value);
298 else
300 int temp, *v = (int*)settings[i].setting;
301 bool found = cfg_string_to_int(i, &temp, value);
302 if (found)
304 if (settings[i].flags&F_TABLE_SETTING)
305 *v = settings[i].table_setting->values[temp];
306 else
307 *v = temp;
309 else
310 { /* atoi breaks choice settings because they
311 * don't have int-like values, and would
312 * fall back to the first value (i.e. 0)
313 * due to atoi */
314 if (!(settings[i].flags&F_CHOICE_SETTING))
315 *v = atoi(value);
318 break;
319 case F_T_BOOL:
321 int temp;
322 if (cfg_string_to_int(i,&temp,value))
323 *(bool*)settings[i].setting = (temp!=0);
324 if (settings[i].bool_setting->option_callback)
325 settings[i].bool_setting->option_callback(temp!=0);
326 break;
328 case F_T_CHARPTR:
329 case F_T_UCHARPTR:
331 char storage[MAX_PATH];
332 if (settings[i].filename_setting->prefix)
334 int len = strlen(settings[i].filename_setting->prefix);
335 if (!strncasecmp(value,
336 settings[i].filename_setting->prefix,
337 len))
339 strlcpy(storage, &value[len], MAX_PATH);
341 else strlcpy(storage, value, MAX_PATH);
343 else strlcpy(storage, value, MAX_PATH);
344 if (settings[i].filename_setting->suffix)
346 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
347 if (s) *s = '\0';
349 strlcpy((char*)settings[i].setting, storage,
350 settings[i].filename_setting->max_len);
351 break;
354 break;
355 } /* if (!strcmp(name,settings[i].cfg_name)) */
356 } /* for(...) */
357 } /* while(...) */
359 close(fd);
360 settings_save();
361 if (apply)
363 settings_apply(true);
364 settings_apply_skins();
366 return true;
369 /** Writing to a config file and saving settings **/
371 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
373 int flags = settings[setting_id].flags;
374 const char* start = settings[setting_id].cfg_vals;
375 char* end = NULL;
376 int count = 0;
378 if ((flags&F_T_MASK)==F_T_INT &&
379 flags&F_TABLE_SETTING)
381 const int *value = settings[setting_id].table_setting->values;
382 while (start)
384 end = strchr(start,',');
385 if (value[count] == val)
387 if (end == NULL)
388 strlcpy(buf, start, buf_len);
389 else
391 int len = (buf_len > (end-start))? end-start: buf_len;
392 strlcpy(buf, start, len+1);
394 return true;
396 count++;
398 if (end)
399 start = end+1;
400 else
401 break;
403 return false;
406 while (count < val)
408 start = strchr(start,',');
409 if (!start)
410 return false;
411 count++;
412 start++;
414 end = strchr(start,',');
415 if (end == NULL)
416 strlcpy(buf, start, buf_len);
417 else
419 int len = (buf_len > (end-start))? end-start: buf_len;
420 strlcpy(buf, start, len+1);
422 return true;
425 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
427 switch (settings[i].flags&F_T_MASK)
429 case F_T_CUSTOM:
430 settings[i].custom_setting->write_to_cfg(settings[i].setting,
431 buf, buf_len);
432 break;
433 case F_T_INT:
434 case F_T_UINT:
435 #ifdef HAVE_LCD_COLOR
436 if (settings[i].flags&F_RGB)
438 int colour = *(int*)settings[i].setting;
439 snprintf(buf,buf_len,"%02x%02x%02x",
440 (int)RGB_UNPACK_RED(colour),
441 (int)RGB_UNPACK_GREEN(colour),
442 (int)RGB_UNPACK_BLUE(colour));
444 else
445 #endif
446 if (settings[i].cfg_vals == NULL)
448 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
450 else
452 if (cfg_int_to_string(i, *(int*)settings[i].setting,
453 buf, buf_len) == false)
455 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
457 else
458 return false;
460 break;
461 case F_T_BOOL:
462 cfg_int_to_string(i,
463 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
464 break;
465 case F_T_CHARPTR:
466 case F_T_UCHARPTR:
467 if (((char*)settings[i].setting)[0]
468 && settings[i].filename_setting->prefix)
470 if (((char*)settings[i].setting)[0] == '-')
472 buf[0] = '-';
473 buf[1] = '\0';
475 else
477 snprintf(buf,buf_len,"%s%s%s",
478 settings[i].filename_setting->prefix,
479 (char*)settings[i].setting,
480 settings[i].filename_setting->suffix);
483 else strlcpy(buf,(char*)settings[i].setting,
484 settings[i].filename_setting->max_len);
485 break;
486 } /* switch () */
487 return true;
491 static bool is_changed(int setting_id)
493 const struct settings_list *setting = &settings[setting_id];
494 switch (setting->flags&F_T_MASK)
496 case F_T_CUSTOM:
497 return setting->custom_setting->is_changed(setting->setting,
498 setting->default_val.custom);
499 break;
500 case F_T_INT:
501 case F_T_UINT:
502 if (setting->flags&F_DEF_ISFUNC)
504 if (*(int*)setting->setting == setting->default_val.func())
505 return false;
507 else if (setting->flags&F_T_SOUND)
509 if (*(int*)setting->setting ==
510 sound_default(setting->sound_setting->setting))
511 return false;
513 else if (*(int*)setting->setting == setting->default_val.int_)
514 return false;
515 break;
516 case F_T_BOOL:
517 if (*(bool*)setting->setting == setting->default_val.bool_)
518 return false;
519 break;
520 case F_T_CHARPTR:
521 case F_T_UCHARPTR:
522 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
523 return false;
524 break;
526 return true;
529 static bool settings_write_config(const char* filename, int options)
531 int i;
532 int fd;
533 char value[MAX_PATH];
534 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
535 if (fd < 0)
536 return false;
537 fdprintf(fd, "# .cfg file created by rockbox %s - "
538 "http://www.rockbox.org\r\n\r\n", appsversion);
539 for(i=0; i<nb_settings; i++)
541 if (settings[i].cfg_name == NULL)
542 continue;
543 value[0] = '\0';
545 switch (options)
547 case SETTINGS_SAVE_CHANGED:
548 if (!is_changed(i))
549 continue;
550 break;
551 case SETTINGS_SAVE_SOUND:
552 if ((settings[i].flags&F_SOUNDSETTING) == 0)
553 continue;
554 break;
555 case SETTINGS_SAVE_THEME:
556 if ((settings[i].flags&F_THEMESETTING) == 0)
557 continue;
558 break;
559 #ifdef HAVE_RECORDING
560 case SETTINGS_SAVE_RECPRESETS:
561 if ((settings[i].flags&F_RECSETTING) == 0)
562 continue;
563 break;
564 #endif
565 #if CONFIG_CODEC == SWCODEC
566 case SETTINGS_SAVE_EQPRESET:
567 if ((settings[i].flags&F_EQSETTING) == 0)
568 continue;
569 break;
570 #endif
573 cfg_to_string(i, value, MAX_PATH);
574 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
575 } /* for(...) */
576 close(fd);
577 return true;
579 #ifndef HAVE_RTC_RAM
580 static void flush_global_status_callback(void *data)
582 (void)data;
583 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
585 #endif
586 static void flush_config_block_callback(void *data)
588 (void)data;
589 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
590 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
594 * persist all runtime user settings to disk or RTC RAM
596 static void update_runtime(void)
598 int elapsed_secs;
600 elapsed_secs = (current_tick - lasttime) / HZ;
601 global_status.runtime += elapsed_secs;
602 lasttime += (elapsed_secs * HZ);
604 if ( global_status.runtime > global_status.topruntime )
605 global_status.topruntime = global_status.runtime;
608 void status_save(void)
610 update_runtime();
611 #ifdef HAVE_RTC_RAM
612 /* this will be done in the storage_callback if
613 target doesnt have rtc ram */
614 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
615 #else
616 register_storage_idle_func(flush_global_status_callback);
617 #endif
620 int settings_save(void)
622 update_runtime();
623 #ifdef HAVE_RTC_RAM
624 /* this will be done in the storage_callback if
625 target doesnt have rtc ram */
626 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
627 #endif
628 register_storage_idle_func(flush_config_block_callback);
629 return 0;
632 bool settings_save_config(int options)
634 char filename[MAX_PATH];
635 char *folder, *namebase;
636 switch (options)
638 case SETTINGS_SAVE_THEME:
639 folder = THEME_DIR;
640 namebase = "theme";
641 break;
642 #ifdef HAVE_RECORDING
643 case SETTINGS_SAVE_RECPRESETS:
644 folder = RECPRESETS_DIR;
645 namebase = "recording";
646 break;
647 #endif
648 #if CONFIG_CODEC == SWCODEC
649 case SETTINGS_SAVE_EQPRESET:
650 folder = EQS_DIR;
651 namebase = "eq";
652 break;
653 #endif
654 case SETTINGS_SAVE_SOUND:
655 folder = ROCKBOX_DIR;
656 namebase = "sound";
657 break;
658 default:
659 folder = ROCKBOX_DIR;
660 namebase = "config";
661 break;
663 create_numbered_filename(filename, folder, namebase, ".cfg", 2
664 IF_CNFN_NUM_(, NULL));
666 /* allow user to modify filename */
667 while (true) {
668 if (!kbd_input(filename, sizeof filename)) {
669 break;
671 else {
672 return false;
676 if (settings_write_config(filename, options))
677 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
678 else
679 splash(HZ, ID2P(LANG_FAILED));
680 return true;
683 /** Apply and Reset settings **/
686 #ifdef HAVE_LCD_BITMAP
688 * Applies the range infos stored in global_settings to
689 * the peak meter.
691 void settings_apply_pm_range(void)
693 int pm_min, pm_max;
695 /* depending on the scale mode (dBfs or percent) the values
696 of global_settings.peak_meter_dbfs have different meanings */
697 if (global_settings.peak_meter_dbfs)
699 /* convert to dBfs * 100 */
700 pm_min = -(((int)global_settings.peak_meter_min) * 100);
701 pm_max = -(((int)global_settings.peak_meter_max) * 100);
703 else
705 /* percent is stored directly -> no conversion */
706 pm_min = global_settings.peak_meter_min;
707 pm_max = global_settings.peak_meter_max;
710 /* apply the range */
711 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
713 #endif /* HAVE_LCD_BITMAP */
715 void sound_settings_apply(void)
717 #if CONFIG_CODEC == SWCODEC
718 sound_set_dsp_callback(dsp_callback);
719 #endif
720 sound_set(SOUND_BASS, global_settings.bass);
721 sound_set(SOUND_TREBLE, global_settings.treble);
722 sound_set(SOUND_BALANCE, global_settings.balance);
723 sound_set(SOUND_VOLUME, global_settings.volume);
724 sound_set(SOUND_CHANNELS, global_settings.channel_config);
725 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
726 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
727 sound_set(SOUND_LOUDNESS, global_settings.loudness);
728 sound_set(SOUND_AVC, global_settings.avc);
729 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
730 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
731 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
732 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
733 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
734 sound_set(SOUND_SUPERBASS, global_settings.superbass);
735 #endif
737 #ifdef HAVE_WM8758
738 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
739 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
740 #endif
744 void settings_apply(bool read_disk)
747 char buf[64];
748 #if CONFIG_CODEC == SWCODEC
749 int i;
750 #endif
751 sound_settings_apply();
753 #ifdef HAVE_DISK_STORAGE
754 audio_set_buffer_margin(global_settings.buffer_margin);
755 #endif
757 #ifdef HAVE_LCD_CONTRAST
758 lcd_set_contrast(global_settings.contrast);
759 #endif
760 lcd_scroll_speed(global_settings.scroll_speed);
761 #ifdef HAVE_REMOTE_LCD
762 lcd_remote_set_contrast(global_settings.remote_contrast);
763 lcd_remote_set_invert_display(global_settings.remote_invert);
765 #ifdef HAVE_LCD_FLIP
766 lcd_remote_set_flip(global_settings.remote_flip_display);
767 #endif
769 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
770 lcd_remote_scroll_step(global_settings.remote_scroll_step);
771 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
772 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
773 #ifdef HAVE_REMOTE_LCD_TICKING
774 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
775 #endif
776 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
777 #if CONFIG_CHARGING
778 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
779 #endif
780 #ifdef HAS_REMOTE_BUTTON_HOLD
781 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
782 #endif
783 #endif /* HAVE_REMOTE_LCD */
784 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
785 backlight_set_brightness(global_settings.brightness);
786 #endif
787 #ifdef HAVE_BACKLIGHT
788 backlight_set_timeout(global_settings.backlight_timeout);
789 #if CONFIG_CHARGING
790 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
791 #endif
792 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
793 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
794 backlight_set_fade_in(global_settings.backlight_fade_in);
795 backlight_set_fade_out(global_settings.backlight_fade_out);
796 #endif
797 #endif
798 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
799 buttonlight_set_brightness(global_settings.buttonlight_brightness);
800 #endif
801 #ifdef HAVE_BUTTON_LIGHT
802 buttonlight_set_timeout(global_settings.buttonlight_timeout);
803 #endif
804 #ifdef HAVE_DISK_STORAGE
805 storage_spindown(global_settings.disk_spindown);
806 #endif
807 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
808 dac_line_in(global_settings.line_in);
809 #endif
810 set_poweroff_timeout(global_settings.poweroff);
812 set_battery_capacity(global_settings.battery_capacity);
813 #if BATTERY_TYPES_COUNT > 1
814 set_battery_type(global_settings.battery_type);
815 #endif
817 #ifdef HAVE_LCD_BITMAP
818 #ifdef HAVE_LCD_INVERT
819 lcd_set_invert_display(global_settings.invert);
820 #endif
821 #ifdef HAVE_LCD_FLIP
822 lcd_set_flip(global_settings.flip_display);
823 button_set_flip(global_settings.flip_display);
824 #endif
825 lcd_update(); /* refresh after flipping the screen */
826 settings_apply_pm_range();
827 peak_meter_init_times(
828 global_settings.peak_meter_release, global_settings.peak_meter_hold,
829 global_settings.peak_meter_clip_hold);
830 #endif
832 #ifdef HAVE_SPEAKER
833 audiohw_enable_speaker(global_settings.speaker_enabled);
834 #endif
836 if (read_disk)
838 #ifdef HAVE_LCD_BITMAP
839 /* fonts need to be loaded before the WPS */
840 if (global_settings.font_file[0]
841 && global_settings.font_file[0] != '-') {
842 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
843 global_settings.font_file);
844 if (font_load(NULL, buf) < 0)
845 font_reset(NULL);
847 else
848 font_reset(NULL);
849 #ifdef HAVE_REMOTE_LCD
850 if ( global_settings.remote_font_file[0]
851 && global_settings.remote_font_file[0] != '-') {
852 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
853 global_settings.remote_font_file);
854 if (font_load_remoteui(buf) < 0)
855 font_load_remoteui(NULL);
857 else
858 font_load_remoteui(NULL);
859 #endif
860 if ( global_settings.kbd_file[0]) {
861 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
862 global_settings.kbd_file);
863 load_kbd(buf);
865 else
866 load_kbd(NULL);
867 #endif
869 if ( global_settings.lang_file[0]) {
870 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
871 global_settings.lang_file);
872 lang_core_load(buf);
873 talk_init(); /* use voice of same language */
876 /* load the icon set */
877 icons_init();
879 #ifdef HAVE_LCD_COLOR
880 if (global_settings.colors_file[0])
881 read_color_theme_file();
882 #endif
884 #ifdef HAVE_LCD_COLOR
885 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
886 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
887 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
888 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
889 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
890 #endif
892 #ifdef HAVE_LCD_BITMAP
893 lcd_scroll_step(global_settings.scroll_step);
894 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
895 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
896 #else
897 lcd_jump_scroll(global_settings.jump_scroll);
898 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
899 #endif
900 lcd_bidir_scroll(global_settings.bidir_limit);
901 lcd_scroll_delay(global_settings.scroll_delay);
904 set_codepage(global_settings.default_codepage);
906 #if CONFIG_CODEC == SWCODEC
907 #ifdef HAVE_CROSSFADE
908 audio_set_crossfade(global_settings.crossfade);
909 #endif
910 dsp_set_replaygain();
911 dsp_set_crossfeed(global_settings.crossfeed);
912 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
913 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
914 global_settings.crossfeed_hf_attenuation,
915 global_settings.crossfeed_hf_cutoff);
917 /* Configure software equalizer, hardware eq is handled in audio_init() */
918 dsp_set_eq(global_settings.eq_enabled);
919 dsp_set_eq_precut(global_settings.eq_precut);
920 for(i = 0; i < 5; i++) {
921 dsp_set_eq_coefs(i);
924 dsp_dither_enable(global_settings.dithering_enabled);
925 dsp_timestretch_enable(global_settings.timestretch_enabled);
926 dsp_set_compressor(global_settings.compressor_threshold,
927 global_settings.compressor_makeup_gain,
928 global_settings.compressor_ratio,
929 global_settings.compressor_knee,
930 global_settings.compressor_release_time);
931 #endif
933 #ifdef HAVE_SPDIF_POWER
934 spdif_power_enable(global_settings.spdif_enable);
935 #endif
937 #ifdef HAVE_BACKLIGHT
938 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
939 #ifdef HAVE_REMOTE_LCD
940 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
941 #endif
942 #ifdef HAS_BUTTON_HOLD
943 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
944 #endif
945 #ifdef HAVE_LCD_SLEEP_SETTING
946 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
947 #endif
948 #endif /* HAVE_BACKLIGHT */
950 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
951 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
952 #endif
954 #ifdef HAVE_USB_CHARGING_ENABLE
955 usb_charging_enable(global_settings.usb_charging);
956 #endif
958 #ifdef HAVE_TOUCHSCREEN
959 touchscreen_set_mode(global_settings.touch_mode);
960 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
961 #endif
963 /* This should stay last */
964 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
965 enc_global_settings_apply();
966 #endif
967 #ifdef HAVE_LCD_BITMAP
968 /* already called with THEME_STATUSBAR in settings_apply_skins() */
969 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE|THEME_BUTTONBAR);
970 #endif
975 * reset all settings to their default value
977 void reset_setting(const struct settings_list *setting, void *var)
979 switch (setting->flags&F_T_MASK)
981 case F_T_CUSTOM:
982 setting->custom_setting->set_default(setting->setting,
983 setting->default_val.custom);
984 break;
985 case F_T_INT:
986 case F_T_UINT:
987 if (setting->flags&F_DEF_ISFUNC)
988 *(int*)var = setting->default_val.func();
989 else if (setting->flags&F_T_SOUND)
990 *(int*)var = sound_default(setting->sound_setting->setting);
991 else *(int*)var = setting->default_val.int_;
992 break;
993 case F_T_BOOL:
994 *(bool*)var = setting->default_val.bool_;
995 break;
996 case F_T_CHARPTR:
997 case F_T_UCHARPTR:
998 strlcpy((char*)var, setting->default_val.charptr,
999 setting->filename_setting->max_len);
1000 break;
1004 void settings_reset(void)
1006 int i;
1008 for(i=0; i<nb_settings; i++)
1009 reset_setting(&settings[i], settings[i].setting);
1010 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1011 enc_global_settings_reset();
1012 #endif
1015 /** Changing setting values **/
1016 const struct settings_list* find_setting(const void* variable, int *id)
1018 int i;
1019 for(i=0;i<nb_settings;i++)
1021 if (settings[i].setting == variable)
1023 if (id)
1024 *id = i;
1025 return &settings[i];
1028 return NULL;
1031 bool set_bool(const char* string, const bool* variable )
1033 return set_bool_options(string, variable,
1034 (char *)STR(LANG_SET_BOOL_YES),
1035 (char *)STR(LANG_SET_BOOL_NO),
1036 NULL);
1040 bool set_bool_options(const char* string, const bool* variable,
1041 const char* yes_str, int yes_voice,
1042 const char* no_str, int no_voice,
1043 void (*function)(bool))
1045 struct opt_items names[] = {
1046 {(unsigned const char *)no_str, no_voice},
1047 {(unsigned const char *)yes_str, yes_voice}
1049 bool result;
1051 result = set_option(string, variable, BOOL, names, 2,
1052 (void (*)(int))function);
1053 return result;
1056 bool set_int(const unsigned char* string,
1057 const char* unit,
1058 int voice_unit,
1059 const int* variable,
1060 void (*function)(int),
1061 int step,
1062 int min,
1063 int max,
1064 const char* (*formatter)(char*, size_t, int, const char*) )
1066 return set_int_ex(string, unit, voice_unit, variable, function,
1067 step, min, max, formatter, NULL);
1070 bool set_int_ex(const unsigned char* string,
1071 const char* unit,
1072 int voice_unit,
1073 const int* variable,
1074 void (*function)(int),
1075 int step,
1076 int min,
1077 int max,
1078 const char* (*formatter)(char*, size_t, int, const char*),
1079 int32_t (*get_talk_id)(int, int))
1081 (void)unit;
1082 struct settings_list item;
1083 struct int_setting data = {
1084 function, voice_unit, min, max, step,
1085 formatter, get_talk_id
1087 item.int_setting = &data;
1088 item.flags = F_INT_SETTING|F_T_INT;
1089 item.lang_id = -1;
1090 item.cfg_vals = (char*)string;
1091 item.setting = (void *)variable;
1092 return option_screen(&item, NULL, false, NULL);
1096 static const struct opt_items *set_option_options;
1097 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1099 (void)buf, (void)unit, (void)size;
1100 return P2STR(set_option_options[item].string);
1103 static int32_t set_option_get_talk_id(int value, int unit)
1105 (void)unit;
1106 return set_option_options[value].voice_id;
1109 bool set_option(const char* string, const void* variable, enum optiontype type,
1110 const struct opt_items* options,
1111 int numoptions, void (*function)(int))
1113 int temp;
1114 struct settings_list item;
1115 struct int_setting data = {
1116 function, UNIT_INT, 0, numoptions-1, 1,
1117 set_option_formatter, set_option_get_talk_id
1119 set_option_options = options;
1120 item.int_setting = &data;
1121 item.flags = F_INT_SETTING|F_T_INT;
1122 item.lang_id = -1;
1123 item.cfg_vals = (char*)string;
1124 item.setting = &temp;
1125 if (type == BOOL)
1126 temp = *(bool*)variable? 1: 0;
1127 else
1128 temp = *(int*)variable;
1129 if (!option_screen(&item, NULL, false, NULL))
1131 if (type == BOOL)
1132 *(bool*)variable = (temp == 1);
1133 else
1134 *(int*)variable = temp;
1135 return false;
1137 return true;
1141 void set_file(const char* filename, char* setting, int maxlen)
1143 const char* fptr = strrchr(filename,'/');
1144 int len;
1145 int extlen = 0;
1146 const char* ptr;
1148 if (!fptr)
1149 return;
1151 fptr++;
1153 len = strlen(fptr);
1154 ptr = fptr + len;
1155 while ((*ptr != '.') && (ptr != fptr)) {
1156 extlen++;
1157 ptr--;
1159 if(ptr == fptr) extlen = 0;
1161 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1162 (len-extlen > maxlen))
1163 return;
1165 strlcpy(setting, fptr, len-extlen+1);
1167 settings_save();