Removed a bit too much, libmisc.so shall still depend on rockbox.zip
[maemo-rb.git] / apps / settings.c
blob06b48067e19430fd730a642bc050a7e2b08335ff
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 "rbpaths.h"
29 #include "action.h"
30 #include "crc32.h"
31 #include "sound.h"
32 #include "settings.h"
33 #include "debug.h"
34 #include "usb.h"
35 #include "backlight.h"
36 #include "audio.h"
37 #include "talk.h"
38 #include "strlcpy.h"
39 #include "strcasestr.h"
40 #include "rtc.h"
41 #include "power.h"
42 #include "ata_idle_notify.h"
43 #include "storage.h"
44 #include "screens.h"
45 #include "ctype.h"
46 #include "file.h"
47 #include "system.h"
48 #include "general.h"
49 #include "misc.h"
50 #ifdef HAVE_LCD_BITMAP
51 #include "icons.h"
52 #include "font.h"
53 #include "peakmeter.h"
54 #endif
55 #include "lang.h"
56 #include "language.h"
57 #include "powermgmt.h"
58 #include "keyboard.h"
59 #include "version.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 #if CONFIG_TUNER
68 #include "radio.h"
69 #endif
70 #include "wps.h"
71 #include "skin_engine/skin_engine.h"
72 #include "viewport.h"
73 #include "statusbar-skinned.h"
74 #include "bootchart.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 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 char path[MAX_PATH];
122 int fd = open(get_user_file_path(NVRAM_FILE, IS_FILE|NEED_WRITE,
123 path, sizeof(path)), O_RDONLY);
124 int bytes;
125 if (fd < 0)
126 return false;
127 memset(buf,0,max_len);
128 bytes = read(fd,buf,max_len);
129 close(fd);
130 if (bytes < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
131 return false;
132 #else
133 memset(buf,0,max_len);
134 /* read rtc block */
135 for (i=0; i < max_len; i++ )
136 buf[i] = rtc_read(0x14+i);
137 #endif
138 /* check magic, version */
139 if ((buf[0] != 'R') || (buf[1] != 'b')
140 || (buf[2] != NVRAM_CONFIG_VERSION))
141 return false;
142 /* check crc32 */
143 crc32 = crc_32(&buf[NVRAM_DATA_START],
144 max_len-NVRAM_DATA_START-1,0xffffffff);
145 if (memcmp(&crc32,&buf[4],4))
146 return false;
147 /* all good, so read in the settings */
148 var_count = buf[3];
149 buf_pos = NVRAM_DATA_START;
150 for(i=0; i<nb_settings; i++)
152 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
153 >>F_NVRAM_MASK_SHIFT;
154 if (nvram_bytes)
156 if ((var_count>0) && (buf_pos<max_len))
158 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
159 buf_pos += nvram_bytes;
160 var_count--;
162 else /* should only happen when new items are added to the end */
164 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
168 return true;
170 static bool write_nvram_data(char* buf, int max_len)
172 unsigned crc32 = 0xffffffff;
173 int i = 0, buf_pos = 0;
174 char var_count = 0;
175 #ifndef HAVE_RTC_RAM
176 int fd;
177 char path[MAX_PATH];
178 #endif
179 memset(buf,0,max_len);
180 /* magic, version */
181 buf[0] = 'R'; buf[1] = 'b';
182 buf[2] = NVRAM_CONFIG_VERSION;
183 buf_pos = NVRAM_DATA_START;
184 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
186 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
187 >>F_NVRAM_MASK_SHIFT;
188 if (nvram_bytes)
190 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
191 buf_pos += nvram_bytes;
192 var_count++;
195 /* count and crc32 */
196 buf[3] = var_count;
197 crc32 = crc_32(&buf[NVRAM_DATA_START],
198 max_len-NVRAM_DATA_START-1,0xffffffff);
199 memcpy(&buf[4],&crc32,4);
200 #ifndef HAVE_RTC_RAM
201 fd = open(get_user_file_path(NVRAM_FILE, IS_FILE|NEED_WRITE,
202 path, sizeof(path)),O_CREAT|O_TRUNC|O_WRONLY, 0666);
203 if (fd >= 0)
205 int len = write(fd,buf,max_len);
206 close(fd);
207 if (len < 8)
208 return false;
210 #else
211 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
212 that it would write a number of bytes at a time since the RTC chip
213 supports that, but this will have to do for now 8-) */
214 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
215 int r = rtc_write(0x14+i, buf[i]);
216 if (r)
217 return false;
219 #endif
220 return true;
223 /** Reading from a config file **/
225 * load settings from disk or RTC RAM
227 void settings_load(int which)
229 if (which&SETTINGS_RTC)
230 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
231 if (which&SETTINGS_HD)
233 const char *file;
234 char path[MAX_PATH];
235 file = get_user_file_path(CONFIGFILE, IS_FILE|NEED_WRITE, path, sizeof(path));
236 settings_load_config(file, false);
237 file = get_user_file_path(FIXEDSETTINGSFILE, IS_FILE, path, sizeof(path));
238 settings_load_config(file, false);
242 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
244 const char* start = settings[setting_id].cfg_vals;
245 char* end = NULL;
246 char temp[MAX_PATH];
247 int count = 0;
248 while (1)
250 end = strchr(start, ',');
251 if (!end)
253 if (!strcmp(str, start))
255 *out = count;
256 return true;
258 else return false;
260 strlcpy(temp, start, end-start+1);
261 if (!strcmp(str, temp))
263 *out = count;
264 return true;
266 start = end +1;
267 count++;
269 return false;
272 bool settings_load_config(const char* file, bool apply)
274 int fd;
275 char line[128];
276 char* name;
277 char* value;
278 int i;
279 fd = open_utf8(file, O_RDONLY);
280 if (fd < 0)
281 return false;
283 while (read_line(fd, line, sizeof line) > 0)
285 if (!settings_parseline(line, &name, &value))
286 continue;
287 for(i=0; i<nb_settings; i++)
289 if (settings[i].cfg_name == NULL)
290 continue;
291 if (!strcasecmp(name,settings[i].cfg_name))
293 switch (settings[i].flags&F_T_MASK)
295 case F_T_CUSTOM:
296 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
297 break;
298 case F_T_INT:
299 case F_T_UINT:
300 #ifdef HAVE_LCD_COLOR
301 if (settings[i].flags&F_RGB)
302 hex_to_rgb(value, (int*)settings[i].setting);
303 else
304 #endif
305 if (settings[i].cfg_vals == NULL)
307 *(int*)settings[i].setting = atoi(value);
309 else
311 int temp, *v = (int*)settings[i].setting;
312 bool found = cfg_string_to_int(i, &temp, value);
313 if (found)
315 if (settings[i].flags&F_TABLE_SETTING)
316 *v = settings[i].table_setting->values[temp];
317 else
318 *v = temp;
320 else
321 { /* atoi breaks choice settings because they
322 * don't have int-like values, and would
323 * fall back to the first value (i.e. 0)
324 * due to atoi */
325 if (!(settings[i].flags&F_CHOICE_SETTING))
326 *v = atoi(value);
329 break;
330 case F_T_BOOL:
332 int temp;
333 if (cfg_string_to_int(i,&temp,value))
334 *(bool*)settings[i].setting = (temp!=0);
335 if (settings[i].bool_setting->option_callback)
336 settings[i].bool_setting->option_callback(temp!=0);
337 break;
339 case F_T_CHARPTR:
340 case F_T_UCHARPTR:
342 char storage[MAX_PATH];
343 if (settings[i].filename_setting->prefix)
345 const char *dir = settings[i].filename_setting->prefix;
346 size_t len = strlen(dir);
347 if (!strncasecmp(value, dir, len))
349 strlcpy(storage, &value[len], MAX_PATH);
351 else strlcpy(storage, value, MAX_PATH);
353 else strlcpy(storage, value, MAX_PATH);
354 if (settings[i].filename_setting->suffix)
356 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
357 if (s) *s = '\0';
359 strlcpy((char*)settings[i].setting, storage,
360 settings[i].filename_setting->max_len);
361 break;
364 break;
365 } /* if (!strcmp(name,settings[i].cfg_name)) */
366 } /* for(...) */
367 } /* while(...) */
369 close(fd);
370 if (apply)
372 settings_apply(true);
373 settings_apply_skins();
375 return true;
378 /** Writing to a config file and saving settings **/
380 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
382 int flags = settings[setting_id].flags;
383 const char* start = settings[setting_id].cfg_vals;
384 char* end = NULL;
385 int count = 0;
387 if ((flags&F_T_MASK)==F_T_INT &&
388 flags&F_TABLE_SETTING)
390 const int *value = settings[setting_id].table_setting->values;
391 while (start)
393 end = strchr(start,',');
394 if (value[count] == val)
396 if (end == NULL)
397 strlcpy(buf, start, buf_len);
398 else
400 int len = (buf_len > (end-start))? end-start: buf_len;
401 strlcpy(buf, start, len+1);
403 return true;
405 count++;
407 if (end)
408 start = end+1;
409 else
410 break;
412 return false;
415 while (count < val)
417 start = strchr(start,',');
418 if (!start)
419 return false;
420 count++;
421 start++;
423 end = strchr(start,',');
424 if (end == NULL)
425 strlcpy(buf, start, buf_len);
426 else
428 int len = (buf_len > (end-start))? end-start: buf_len;
429 strlcpy(buf, start, len+1);
431 return true;
434 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
436 switch (settings[i].flags&F_T_MASK)
438 case F_T_CUSTOM:
439 settings[i].custom_setting->write_to_cfg(settings[i].setting,
440 buf, buf_len);
441 break;
442 case F_T_INT:
443 case F_T_UINT:
444 #ifdef HAVE_LCD_COLOR
445 if (settings[i].flags&F_RGB)
447 int colour = *(int*)settings[i].setting;
448 snprintf(buf,buf_len,"%02x%02x%02x",
449 (int)RGB_UNPACK_RED(colour),
450 (int)RGB_UNPACK_GREEN(colour),
451 (int)RGB_UNPACK_BLUE(colour));
453 else
454 #endif
455 if (settings[i].cfg_vals == NULL)
457 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
459 else
461 if (cfg_int_to_string(i, *(int*)settings[i].setting,
462 buf, buf_len) == false)
464 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
466 else
467 return false;
469 break;
470 case F_T_BOOL:
471 cfg_int_to_string(i,
472 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
473 break;
474 case F_T_CHARPTR:
475 case F_T_UCHARPTR:
476 if (((char*)settings[i].setting)[0]
477 && settings[i].filename_setting->prefix)
479 if (((char*)settings[i].setting)[0] == '-')
481 buf[0] = '-';
482 buf[1] = '\0';
484 else
486 snprintf(buf,buf_len,"%s%s%s",
487 settings[i].filename_setting->prefix,
488 (char*)settings[i].setting,
489 settings[i].filename_setting->suffix);
492 else strlcpy(buf,(char*)settings[i].setting,
493 settings[i].filename_setting->max_len);
494 break;
495 } /* switch () */
496 return true;
500 static bool is_changed(int setting_id)
502 const struct settings_list *setting = &settings[setting_id];
503 switch (setting->flags&F_T_MASK)
505 case F_T_CUSTOM:
506 return setting->custom_setting->is_changed(setting->setting,
507 setting->default_val.custom);
508 break;
509 case F_T_INT:
510 case F_T_UINT:
511 if (setting->flags&F_DEF_ISFUNC)
513 if (*(int*)setting->setting == setting->default_val.func())
514 return false;
516 else if (setting->flags&F_T_SOUND)
518 if (*(int*)setting->setting ==
519 sound_default(setting->sound_setting->setting))
520 return false;
522 else if (*(int*)setting->setting == setting->default_val.int_)
523 return false;
524 break;
525 case F_T_BOOL:
526 if (*(bool*)setting->setting == setting->default_val.bool_)
527 return false;
528 break;
529 case F_T_CHARPTR:
530 case F_T_UCHARPTR:
531 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
532 return false;
533 break;
535 return true;
538 static bool settings_write_config(const char* filename, int options)
540 int i;
541 int fd;
542 char value[MAX_PATH];
543 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY, 0666);
544 if (fd < 0)
545 return false;
546 fdprintf(fd, "# .cfg file created by rockbox %s - "
547 "http://www.rockbox.org\r\n\r\n", rbversion);
548 for(i=0; i<nb_settings; i++)
550 if (settings[i].cfg_name == NULL)
551 continue;
552 value[0] = '\0';
554 switch (options)
556 case SETTINGS_SAVE_CHANGED:
557 if (!is_changed(i))
558 continue;
559 break;
560 case SETTINGS_SAVE_SOUND:
561 if ((settings[i].flags&F_SOUNDSETTING) == 0)
562 continue;
563 break;
564 case SETTINGS_SAVE_THEME:
565 if ((settings[i].flags&F_THEMESETTING) == 0)
566 continue;
567 break;
568 #ifdef HAVE_RECORDING
569 case SETTINGS_SAVE_RECPRESETS:
570 if ((settings[i].flags&F_RECSETTING) == 0)
571 continue;
572 break;
573 #endif
574 #if CONFIG_CODEC == SWCODEC
575 case SETTINGS_SAVE_EQPRESET:
576 if ((settings[i].flags&F_EQSETTING) == 0)
577 continue;
578 break;
579 #endif
582 cfg_to_string(i, value, MAX_PATH);
583 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
584 } /* for(...) */
585 close(fd);
586 return true;
588 #ifndef HAVE_RTC_RAM
589 static void flush_global_status_callback(void *data)
591 (void)data;
592 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
594 #endif
595 static void flush_config_block_callback(void *data)
597 (void)data;
598 char path[MAX_PATH];
599 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
600 settings_write_config(
601 get_user_file_path(CONFIGFILE, IS_FILE|NEED_WRITE, path, sizeof(path)),
602 SETTINGS_SAVE_CHANGED);
606 * persist all runtime user settings to disk or RTC RAM
608 static void update_runtime(void)
610 int elapsed_secs;
612 elapsed_secs = (current_tick - lasttime) / HZ;
613 global_status.runtime += elapsed_secs;
614 lasttime += (elapsed_secs * HZ);
616 if ( global_status.runtime > global_status.topruntime )
617 global_status.topruntime = global_status.runtime;
620 void status_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 #else
628 register_storage_idle_func(flush_global_status_callback);
629 #endif
632 int settings_save(void)
634 update_runtime();
635 #ifdef HAVE_RTC_RAM
636 /* this will be done in the storage_callback if
637 target doesnt have rtc ram */
638 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
639 #endif
640 register_storage_idle_func(flush_config_block_callback);
641 return 0;
644 bool settings_save_config(int options)
646 char filename[MAX_PATH], path[MAX_PATH];
647 const char *folder, *namebase;
648 switch (options)
650 case SETTINGS_SAVE_THEME:
651 folder = THEME_DIR;
652 namebase = "theme";
653 break;
654 #ifdef HAVE_RECORDING
655 case SETTINGS_SAVE_RECPRESETS:
656 folder = RECPRESETS_DIR;
657 namebase = "recording";
658 break;
659 #endif
660 #if CONFIG_CODEC == SWCODEC
661 case SETTINGS_SAVE_EQPRESET:
662 folder = EQS_DIR;
663 namebase = "eq";
664 break;
665 #endif
666 case SETTINGS_SAVE_SOUND:
667 folder = ROCKBOX_DIR;
668 namebase = "sound";
669 break;
670 default:
671 folder = ROCKBOX_DIR;
672 namebase = "config";
673 break;
676 folder = get_user_file_path(folder, NEED_WRITE, path, sizeof(path));
677 create_numbered_filename(filename, folder, namebase, ".cfg", 2
678 IF_CNFN_NUM_(, NULL));
680 /* allow user to modify filename */
681 while (true) {
682 if (!kbd_input(filename, sizeof filename)) {
683 break;
685 else {
686 return false;
690 if (settings_write_config(filename, options))
691 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
692 else
693 splash(HZ, ID2P(LANG_FAILED));
694 return true;
697 /** Apply and Reset settings **/
700 #ifdef HAVE_LCD_BITMAP
702 * Applies the range infos stored in global_settings to
703 * the peak meter.
705 void settings_apply_pm_range(void)
707 int pm_min, pm_max;
709 /* depending on the scale mode (dBfs or percent) the values
710 of global_settings.peak_meter_dbfs have different meanings */
711 if (global_settings.peak_meter_dbfs)
713 /* convert to dBfs * 100 */
714 pm_min = -(((int)global_settings.peak_meter_min) * 100);
715 pm_max = -(((int)global_settings.peak_meter_max) * 100);
717 else
719 /* percent is stored directly -> no conversion */
720 pm_min = global_settings.peak_meter_min;
721 pm_max = global_settings.peak_meter_max;
724 /* apply the range */
725 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
727 #endif /* HAVE_LCD_BITMAP */
729 void sound_settings_apply(void)
731 #if CONFIG_CODEC == SWCODEC
732 sound_set_dsp_callback(dsp_callback);
733 #endif
734 #ifdef AUDIOHW_HAVE_BASS
735 sound_set(SOUND_BASS, global_settings.bass);
736 #endif
737 #ifdef AUDIOHW_HAVE_TREBLE
738 sound_set(SOUND_TREBLE, global_settings.treble);
739 #endif
740 sound_set(SOUND_BALANCE, global_settings.balance);
741 sound_set(SOUND_VOLUME, global_settings.volume);
742 sound_set(SOUND_CHANNELS, global_settings.channel_config);
743 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
744 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
745 sound_set(SOUND_LOUDNESS, global_settings.loudness);
746 sound_set(SOUND_AVC, global_settings.avc);
747 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
748 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
749 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
750 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
751 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
752 sound_set(SOUND_SUPERBASS, global_settings.superbass);
753 #endif
754 #ifdef AUDIOHW_HAVE_BASS_CUTOFF
755 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
756 #endif
757 #ifdef AUDIOHW_HAVE_TREBLE_CUTOFF
758 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
759 #endif
760 #ifdef AUDIOHW_HAVE_DEPTH_3D
761 sound_set(SOUND_DEPTH_3D, global_settings.depth_3d);
762 #endif
763 #ifdef AUDIOHW_HAVE_EQ
764 int b;
766 for (b = 0; b < AUDIOHW_EQ_BAND_NUM; b++)
768 int setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_GAIN);
769 sound_set(setting, global_settings.hw_eq_bands[b].gain);
771 #ifdef AUDIOHW_HAVE_EQ_FREQUENCY
772 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_FREQUENCY);
773 if (setting != -1)
774 sound_set(setting, global_settings.hw_eq_bands[b].frequency);
775 #endif /* AUDIOHW_HAVE_EQ_FREQUENCY */
776 #ifdef AUDIOHW_HAVE_EQ_WIDTH
777 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_WIDTH);
778 if (setting != -1)
779 sound_set(setting, global_settings.hw_eq_bands[b].width);
780 #endif /* AUDIOHW_HAVE_EQ_WIDTH */
782 #endif
785 void settings_apply(bool read_disk)
787 #ifdef HAVE_LCD_BITMAP
788 int rc;
789 #endif
790 #if CONFIG_CODEC == SWCODEC
791 int i;
792 #endif
793 sound_settings_apply();
795 #ifdef HAVE_DISK_STORAGE
796 audio_set_buffer_margin(global_settings.buffer_margin);
797 #endif
799 #ifdef HAVE_LCD_CONTRAST
800 lcd_set_contrast(global_settings.contrast);
801 #endif
802 lcd_scroll_speed(global_settings.scroll_speed);
803 #ifdef HAVE_REMOTE_LCD
804 lcd_remote_set_contrast(global_settings.remote_contrast);
805 lcd_remote_set_invert_display(global_settings.remote_invert);
807 #ifdef HAVE_LCD_FLIP
808 lcd_remote_set_flip(global_settings.remote_flip_display);
809 #endif
811 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
812 lcd_remote_scroll_step(global_settings.remote_scroll_step);
813 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
814 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
815 #ifdef HAVE_REMOTE_LCD_TICKING
816 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
817 #endif
818 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
819 #if CONFIG_CHARGING
820 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
821 #endif
822 #ifdef HAS_REMOTE_BUTTON_HOLD
823 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
824 #endif
825 #endif /* HAVE_REMOTE_LCD */
826 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
827 backlight_set_brightness(global_settings.brightness);
828 #endif
829 #ifdef HAVE_BACKLIGHT
830 backlight_set_timeout(global_settings.backlight_timeout);
831 #if CONFIG_CHARGING
832 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
833 #endif
834 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
835 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
836 backlight_set_fade_in(global_settings.backlight_fade_in);
837 backlight_set_fade_out(global_settings.backlight_fade_out);
838 #endif
839 #endif
840 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
841 buttonlight_set_brightness(global_settings.buttonlight_brightness);
842 #endif
843 #ifdef HAVE_BUTTON_LIGHT
844 buttonlight_set_timeout(global_settings.buttonlight_timeout);
845 #endif
846 #ifdef HAVE_DISK_STORAGE
847 storage_spindown(global_settings.disk_spindown);
848 #endif
849 #if (CONFIG_CODEC == MAS3507D) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
850 dac_line_in(global_settings.line_in);
851 #endif
852 set_poweroff_timeout(global_settings.poweroff);
854 #if defined(BATTERY_CAPACITY_INC) && BATTERY_CAPACITY_INC > 0
855 /* only call if it's really exchangable */
856 set_battery_capacity(global_settings.battery_capacity);
857 #endif
859 #if BATTERY_TYPES_COUNT > 1
860 set_battery_type(global_settings.battery_type);
861 #endif
863 #ifdef HAVE_LCD_BITMAP
864 #ifdef HAVE_LCD_INVERT
865 lcd_set_invert_display(global_settings.invert);
866 #endif
867 #ifdef HAVE_LCD_FLIP
868 lcd_set_flip(global_settings.flip_display);
869 button_set_flip(global_settings.flip_display);
870 #endif
871 lcd_update(); /* refresh after flipping the screen */
872 settings_apply_pm_range();
873 peak_meter_init_times(
874 global_settings.peak_meter_release, global_settings.peak_meter_hold,
875 global_settings.peak_meter_clip_hold);
876 #endif
878 #ifdef HAVE_SPEAKER
879 audiohw_enable_speaker(global_settings.speaker_enabled);
880 #endif
882 if (read_disk)
884 char buf[MAX_PATH];
885 #ifdef HAVE_LCD_BITMAP
886 char dir[MAX_PATH];
887 const char *font_path = get_user_file_path(FONT_DIR, 0, dir, sizeof(dir));
888 /* fonts need to be loaded before the WPS */
889 if (global_settings.font_file[0]
890 && global_settings.font_file[0] != '-') {
892 snprintf(buf, sizeof buf, "%s/%s.fnt", font_path,
893 global_settings.font_file);
894 CHART2(">font_load ", global_settings.font_file);
895 rc = font_load(NULL, buf);
896 CHART2("<font_load ", global_settings.font_file);
897 if (rc < 0)
898 font_reset(NULL);
900 else
901 font_reset(NULL);
902 #ifdef HAVE_REMOTE_LCD
903 if ( global_settings.remote_font_file[0]
904 && global_settings.remote_font_file[0] != '-') {
905 snprintf(buf, sizeof buf, "%s/%s.fnt", font_path,
906 global_settings.remote_font_file);
907 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
908 rc = font_load_remoteui(buf);
909 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
910 if (rc < 0)
911 font_load_remoteui(NULL);
913 else
914 font_load_remoteui(NULL);
915 #endif
916 if ( global_settings.kbd_file[0]) {
917 snprintf(buf, sizeof buf, "%s/%s.kbd",
918 get_user_file_path(ROCKBOX_DIR, 0, dir, sizeof(dir)),
919 global_settings.kbd_file);
920 CHART(">load_kbd");
921 load_kbd(buf);
922 CHART("<load_kbd");
924 else
925 load_kbd(NULL);
926 #endif /* HAVE_LCD_BITMAP */
927 /* no get_user_file_path() here because we don't really support
928 * langs that don't come with rockbox */
929 if ( global_settings.lang_file[0]) {
930 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
931 global_settings.lang_file);
932 CHART(">lang_core_load");
933 lang_core_load(buf);
934 CHART("<lang_core_load");
935 CHART(">talk_init");
936 talk_init(); /* use voice of same language */
937 CHART("<talk_init");
940 /* load the icon set */
941 CHART(">icons_init");
942 icons_init();
943 CHART("<icons_init");
945 #ifdef HAVE_LCD_COLOR
946 if (global_settings.colors_file[0])
948 CHART(">read_color_theme_file");
949 read_color_theme_file();
950 CHART("<read_color_theme_file");
952 #endif
954 #ifdef HAVE_LCD_COLOR
955 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
956 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
957 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
958 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
959 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
960 #endif
962 #ifdef HAVE_LCD_BITMAP
963 lcd_scroll_step(global_settings.scroll_step);
964 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
965 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
966 #endif
967 lcd_bidir_scroll(global_settings.bidir_limit);
968 lcd_scroll_delay(global_settings.scroll_delay);
971 CHART(">set_codepage");
972 set_codepage(global_settings.default_codepage);
973 CHART("<set_codepage");
975 #if CONFIG_CODEC == SWCODEC
976 #ifdef HAVE_CROSSFADE
977 audio_set_crossfade(global_settings.crossfade);
978 #endif
979 dsp_set_replaygain();
980 dsp_set_crossfeed(global_settings.crossfeed);
981 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
982 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
983 global_settings.crossfeed_hf_attenuation,
984 global_settings.crossfeed_hf_cutoff);
986 /* Configure software equalizer, hardware eq is handled in audio_init() */
987 dsp_set_eq(global_settings.eq_enabled);
988 dsp_set_eq_precut(global_settings.eq_precut);
989 for(i = 0; i < 5; i++) {
990 dsp_set_eq_coefs(i);
993 dsp_dither_enable(global_settings.dithering_enabled);
994 #ifdef HAVE_PITCHSCREEN
995 dsp_timestretch_enable(global_settings.timestretch_enabled);
996 #endif
997 dsp_set_compressor(global_settings.compressor_threshold,
998 global_settings.compressor_makeup_gain,
999 global_settings.compressor_ratio,
1000 global_settings.compressor_knee,
1001 global_settings.compressor_release_time);
1002 #endif
1004 #ifdef HAVE_SPDIF_POWER
1005 spdif_power_enable(global_settings.spdif_enable);
1006 #endif
1008 #ifdef HAVE_BACKLIGHT
1009 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
1010 #ifdef HAVE_REMOTE_LCD
1011 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
1012 #endif
1013 #ifdef HAS_BUTTON_HOLD
1014 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
1015 #endif
1016 #ifdef HAVE_LCD_SLEEP_SETTING
1017 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
1018 #endif
1019 #endif /* HAVE_BACKLIGHT */
1021 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1022 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1023 #endif
1025 #ifdef HAVE_USB_CHARGING_ENABLE
1026 usb_charging_enable(global_settings.usb_charging);
1027 #endif
1029 #ifdef HAVE_TOUCHSCREEN
1030 touchscreen_set_mode(global_settings.touch_mode);
1031 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1032 #endif
1034 /* This should stay last */
1035 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1036 enc_global_settings_apply();
1037 #endif
1038 #ifdef HAVE_LCD_BITMAP
1039 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1040 CHART(">viewportmanager_theme_changed");
1041 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE|THEME_BUTTONBAR);
1042 CHART("<viewportmanager_theme_changed");
1043 #endif
1048 * reset all settings to their default value
1050 void reset_setting(const struct settings_list *setting, void *var)
1052 switch (setting->flags&F_T_MASK)
1054 case F_T_CUSTOM:
1055 setting->custom_setting->set_default(setting->setting,
1056 setting->default_val.custom);
1057 break;
1058 case F_T_INT:
1059 case F_T_UINT:
1060 if (setting->flags&F_DEF_ISFUNC)
1061 *(int*)var = setting->default_val.func();
1062 else if (setting->flags&F_T_SOUND)
1063 *(int*)var = sound_default(setting->sound_setting->setting);
1064 else *(int*)var = setting->default_val.int_;
1065 break;
1066 case F_T_BOOL:
1067 *(bool*)var = setting->default_val.bool_;
1068 break;
1069 case F_T_CHARPTR:
1070 case F_T_UCHARPTR:
1071 strlcpy((char*)var, setting->default_val.charptr,
1072 setting->filename_setting->max_len);
1073 break;
1077 void settings_reset(void)
1079 int i;
1081 for(i=0; i<nb_settings; i++)
1082 reset_setting(&settings[i], settings[i].setting);
1083 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1084 enc_global_settings_reset();
1085 #endif
1088 /** Changing setting values **/
1089 const struct settings_list* find_setting(const void* variable, int *id)
1091 int i;
1092 for(i=0;i<nb_settings;i++)
1094 if (settings[i].setting == variable)
1096 if (id)
1097 *id = i;
1098 return &settings[i];
1101 return NULL;
1104 bool set_bool(const char* string, const bool* variable )
1106 return set_bool_options(string, variable,
1107 (char *)STR(LANG_SET_BOOL_YES),
1108 (char *)STR(LANG_SET_BOOL_NO),
1109 NULL);
1113 bool set_bool_options(const char* string, const bool* variable,
1114 const char* yes_str, int yes_voice,
1115 const char* no_str, int no_voice,
1116 void (*function)(bool))
1118 struct opt_items names[] = {
1119 {(unsigned const char *)no_str, no_voice},
1120 {(unsigned const char *)yes_str, yes_voice}
1122 bool result;
1124 result = set_option(string, variable, BOOL, names, 2,
1125 (void (*)(int))function);
1126 return result;
1129 bool set_int(const unsigned char* string,
1130 const char* unit,
1131 int voice_unit,
1132 const int* variable,
1133 void (*function)(int),
1134 int step,
1135 int min,
1136 int max,
1137 const char* (*formatter)(char*, size_t, int, const char*) )
1139 return set_int_ex(string, unit, voice_unit, variable, function,
1140 step, min, max, formatter, NULL);
1143 bool set_int_ex(const unsigned char* string,
1144 const char* unit,
1145 int voice_unit,
1146 const int* variable,
1147 void (*function)(int),
1148 int step,
1149 int min,
1150 int max,
1151 const char* (*formatter)(char*, size_t, int, const char*),
1152 int32_t (*get_talk_id)(int, int))
1154 (void)unit;
1155 struct settings_list item;
1156 struct int_setting data = {
1157 function, voice_unit, min, max, step,
1158 formatter, get_talk_id
1160 item.int_setting = &data;
1161 item.flags = F_INT_SETTING|F_T_INT;
1162 item.lang_id = -1;
1163 item.cfg_vals = (char*)string;
1164 item.setting = (void *)variable;
1165 return option_screen(&item, NULL, false, NULL);
1169 static const struct opt_items *set_option_options;
1170 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1172 (void)buf, (void)unit, (void)size;
1173 return P2STR(set_option_options[item].string);
1176 static int32_t set_option_get_talk_id(int value, int unit)
1178 (void)unit;
1179 return set_option_options[value].voice_id;
1182 bool set_option(const char* string, const void* variable, enum optiontype type,
1183 const struct opt_items* options,
1184 int numoptions, void (*function)(int))
1186 int temp;
1187 struct settings_list item;
1188 struct int_setting data = {
1189 function, UNIT_INT, 0, numoptions-1, 1,
1190 set_option_formatter, set_option_get_talk_id
1192 set_option_options = options;
1193 item.int_setting = &data;
1194 item.flags = F_INT_SETTING|F_T_INT;
1195 item.lang_id = -1;
1196 item.cfg_vals = (char*)string;
1197 item.setting = &temp;
1198 if (type == BOOL)
1199 temp = *(bool*)variable? 1: 0;
1200 else
1201 temp = *(int*)variable;
1202 if (!option_screen(&item, NULL, false, NULL))
1204 if (type == BOOL)
1206 *(bool*)variable = (temp == 1);
1207 else
1208 *(int*)variable = temp;
1209 return false;
1211 return true;
1215 * Takes filename, removes the directory and the extension,
1216 * and then copies the basename into setting, unless the basename exceeds maxlen
1218 void set_file(const char* filename, char* setting, const int maxlen)
1220 const char* fptr = strrchr(filename,'/');
1221 const char* extptr;
1222 int len;
1223 int extlen = 0;
1225 if (!fptr)
1226 return;
1228 fptr++;
1230 extptr = strrchr(fptr, '.');
1232 if (!extptr || extptr < fptr)
1233 extlen = 0;
1234 else
1235 extlen = strlen(extptr);
1237 len = strlen(fptr) - extlen + 1;
1239 /* error if filename isn't in ROCKBOX_DIR */
1240 if (len > maxlen)
1241 return;
1243 strlcpy(setting, fptr, len);
1244 settings_save();