Minor English correction.
[maemo-rb.git] / apps / settings.c
blob85b0489c097d8dfc583a1a61e0f4b341392caf69
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_save();
373 settings_apply(true);
374 settings_apply_skins();
376 return true;
379 /** Writing to a config file and saving settings **/
381 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
383 int flags = settings[setting_id].flags;
384 const char* start = settings[setting_id].cfg_vals;
385 char* end = NULL;
386 int count = 0;
388 if ((flags&F_T_MASK)==F_T_INT &&
389 flags&F_TABLE_SETTING)
391 const int *value = settings[setting_id].table_setting->values;
392 while (start)
394 end = strchr(start,',');
395 if (value[count] == val)
397 if (end == NULL)
398 strlcpy(buf, start, buf_len);
399 else
401 int len = (buf_len > (end-start))? end-start: buf_len;
402 strlcpy(buf, start, len+1);
404 return true;
406 count++;
408 if (end)
409 start = end+1;
410 else
411 break;
413 return false;
416 while (count < val)
418 start = strchr(start,',');
419 if (!start)
420 return false;
421 count++;
422 start++;
424 end = strchr(start,',');
425 if (end == NULL)
426 strlcpy(buf, start, buf_len);
427 else
429 int len = (buf_len > (end-start))? end-start: buf_len;
430 strlcpy(buf, start, len+1);
432 return true;
435 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
437 switch (settings[i].flags&F_T_MASK)
439 case F_T_CUSTOM:
440 settings[i].custom_setting->write_to_cfg(settings[i].setting,
441 buf, buf_len);
442 break;
443 case F_T_INT:
444 case F_T_UINT:
445 #ifdef HAVE_LCD_COLOR
446 if (settings[i].flags&F_RGB)
448 int colour = *(int*)settings[i].setting;
449 snprintf(buf,buf_len,"%02x%02x%02x",
450 (int)RGB_UNPACK_RED(colour),
451 (int)RGB_UNPACK_GREEN(colour),
452 (int)RGB_UNPACK_BLUE(colour));
454 else
455 #endif
456 if (settings[i].cfg_vals == NULL)
458 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
460 else
462 if (cfg_int_to_string(i, *(int*)settings[i].setting,
463 buf, buf_len) == false)
465 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
467 else
468 return false;
470 break;
471 case F_T_BOOL:
472 cfg_int_to_string(i,
473 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
474 break;
475 case F_T_CHARPTR:
476 case F_T_UCHARPTR:
477 if (((char*)settings[i].setting)[0]
478 && settings[i].filename_setting->prefix)
480 if (((char*)settings[i].setting)[0] == '-')
482 buf[0] = '-';
483 buf[1] = '\0';
485 else
487 snprintf(buf,buf_len,"%s%s%s",
488 settings[i].filename_setting->prefix,
489 (char*)settings[i].setting,
490 settings[i].filename_setting->suffix);
493 else strlcpy(buf,(char*)settings[i].setting,
494 settings[i].filename_setting->max_len);
495 break;
496 } /* switch () */
497 return true;
501 static bool is_changed(int setting_id)
503 const struct settings_list *setting = &settings[setting_id];
504 switch (setting->flags&F_T_MASK)
506 case F_T_CUSTOM:
507 return setting->custom_setting->is_changed(setting->setting,
508 setting->default_val.custom);
509 break;
510 case F_T_INT:
511 case F_T_UINT:
512 if (setting->flags&F_DEF_ISFUNC)
514 if (*(int*)setting->setting == setting->default_val.func())
515 return false;
517 else if (setting->flags&F_T_SOUND)
519 if (*(int*)setting->setting ==
520 sound_default(setting->sound_setting->setting))
521 return false;
523 else if (*(int*)setting->setting == setting->default_val.int_)
524 return false;
525 break;
526 case F_T_BOOL:
527 if (*(bool*)setting->setting == setting->default_val.bool_)
528 return false;
529 break;
530 case F_T_CHARPTR:
531 case F_T_UCHARPTR:
532 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
533 return false;
534 break;
536 return true;
539 static bool settings_write_config(const char* filename, int options)
541 int i;
542 int fd;
543 char value[MAX_PATH];
544 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY, 0666);
545 if (fd < 0)
546 return false;
547 fdprintf(fd, "# .cfg file created by rockbox %s - "
548 "http://www.rockbox.org\r\n\r\n", rbversion);
549 for(i=0; i<nb_settings; i++)
551 if (settings[i].cfg_name == NULL)
552 continue;
553 value[0] = '\0';
555 switch (options)
557 case SETTINGS_SAVE_CHANGED:
558 if (!is_changed(i))
559 continue;
560 break;
561 case SETTINGS_SAVE_SOUND:
562 if ((settings[i].flags&F_SOUNDSETTING) == 0)
563 continue;
564 break;
565 case SETTINGS_SAVE_THEME:
566 if ((settings[i].flags&F_THEMESETTING) == 0)
567 continue;
568 break;
569 #ifdef HAVE_RECORDING
570 case SETTINGS_SAVE_RECPRESETS:
571 if ((settings[i].flags&F_RECSETTING) == 0)
572 continue;
573 break;
574 #endif
575 #if CONFIG_CODEC == SWCODEC
576 case SETTINGS_SAVE_EQPRESET:
577 if ((settings[i].flags&F_EQSETTING) == 0)
578 continue;
579 break;
580 #endif
583 cfg_to_string(i, value, MAX_PATH);
584 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
585 } /* for(...) */
586 close(fd);
587 return true;
589 #ifndef HAVE_RTC_RAM
590 static void flush_global_status_callback(void *data)
592 (void)data;
593 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
595 #endif
596 static void flush_config_block_callback(void *data)
598 (void)data;
599 char path[MAX_PATH];
600 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
601 settings_write_config(
602 get_user_file_path(CONFIGFILE, IS_FILE|NEED_WRITE, path, sizeof(path)),
603 SETTINGS_SAVE_CHANGED);
607 * persist all runtime user settings to disk or RTC RAM
609 static void update_runtime(void)
611 int elapsed_secs;
613 elapsed_secs = (current_tick - lasttime) / HZ;
614 global_status.runtime += elapsed_secs;
615 lasttime += (elapsed_secs * HZ);
617 if ( global_status.runtime > global_status.topruntime )
618 global_status.topruntime = global_status.runtime;
621 void status_save(void)
623 update_runtime();
624 #ifdef HAVE_RTC_RAM
625 /* this will be done in the storage_callback if
626 target doesnt have rtc ram */
627 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
628 #else
629 register_storage_idle_func(flush_global_status_callback);
630 #endif
633 int settings_save(void)
635 update_runtime();
636 #ifdef HAVE_RTC_RAM
637 /* this will be done in the storage_callback if
638 target doesnt have rtc ram */
639 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
640 #endif
641 register_storage_idle_func(flush_config_block_callback);
642 return 0;
645 bool settings_save_config(int options)
647 char filename[MAX_PATH], path[MAX_PATH];
648 const char *folder, *namebase;
649 switch (options)
651 case SETTINGS_SAVE_THEME:
652 folder = THEME_DIR;
653 namebase = "theme";
654 break;
655 #ifdef HAVE_RECORDING
656 case SETTINGS_SAVE_RECPRESETS:
657 folder = RECPRESETS_DIR;
658 namebase = "recording";
659 break;
660 #endif
661 #if CONFIG_CODEC == SWCODEC
662 case SETTINGS_SAVE_EQPRESET:
663 folder = EQS_DIR;
664 namebase = "eq";
665 break;
666 #endif
667 case SETTINGS_SAVE_SOUND:
668 folder = ROCKBOX_DIR;
669 namebase = "sound";
670 break;
671 default:
672 folder = ROCKBOX_DIR;
673 namebase = "config";
674 break;
677 folder = get_user_file_path(folder, NEED_WRITE, path, sizeof(path));
678 create_numbered_filename(filename, folder, namebase, ".cfg", 2
679 IF_CNFN_NUM_(, NULL));
681 /* allow user to modify filename */
682 while (true) {
683 if (!kbd_input(filename, sizeof filename)) {
684 break;
686 else {
687 return false;
691 if (settings_write_config(filename, options))
692 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
693 else
694 splash(HZ, ID2P(LANG_FAILED));
695 return true;
698 /** Apply and Reset settings **/
701 #ifdef HAVE_LCD_BITMAP
703 * Applies the range infos stored in global_settings to
704 * the peak meter.
706 void settings_apply_pm_range(void)
708 int pm_min, pm_max;
710 /* depending on the scale mode (dBfs or percent) the values
711 of global_settings.peak_meter_dbfs have different meanings */
712 if (global_settings.peak_meter_dbfs)
714 /* convert to dBfs * 100 */
715 pm_min = -(((int)global_settings.peak_meter_min) * 100);
716 pm_max = -(((int)global_settings.peak_meter_max) * 100);
718 else
720 /* percent is stored directly -> no conversion */
721 pm_min = global_settings.peak_meter_min;
722 pm_max = global_settings.peak_meter_max;
725 /* apply the range */
726 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
728 #endif /* HAVE_LCD_BITMAP */
730 void sound_settings_apply(void)
732 #if CONFIG_CODEC == SWCODEC
733 sound_set_dsp_callback(dsp_callback);
734 #endif
735 #ifdef AUDIOHW_HAVE_BASS
736 sound_set(SOUND_BASS, global_settings.bass);
737 #endif
738 #ifdef AUDIOHW_HAVE_TREBLE
739 sound_set(SOUND_TREBLE, global_settings.treble);
740 #endif
741 sound_set(SOUND_BALANCE, global_settings.balance);
742 sound_set(SOUND_VOLUME, global_settings.volume);
743 sound_set(SOUND_CHANNELS, global_settings.channel_config);
744 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
745 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
746 sound_set(SOUND_LOUDNESS, global_settings.loudness);
747 sound_set(SOUND_AVC, global_settings.avc);
748 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
749 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
750 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
751 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
752 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
753 sound_set(SOUND_SUPERBASS, global_settings.superbass);
754 #endif
755 #ifdef AUDIOHW_HAVE_BASS_CUTOFF
756 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
757 #endif
758 #ifdef AUDIOHW_HAVE_TREBLE_CUTOFF
759 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
760 #endif
761 #ifdef AUDIOHW_HAVE_DEPTH_3D
762 sound_set(SOUND_DEPTH_3D, global_settings.depth_3d);
763 #endif
764 #ifdef AUDIOHW_HAVE_EQ
765 int b;
767 for (b = 0; b < AUDIOHW_EQ_BAND_NUM; b++)
769 int setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_GAIN);
770 sound_set(setting, global_settings.hw_eq_bands[b].gain);
772 #ifdef AUDIOHW_HAVE_EQ_FREQUENCY
773 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_FREQUENCY);
774 if (setting != -1)
775 sound_set(setting, global_settings.hw_eq_bands[b].frequency);
776 #endif /* AUDIOHW_HAVE_EQ_FREQUENCY */
777 #ifdef AUDIOHW_HAVE_EQ_WIDTH
778 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_WIDTH);
779 if (setting != -1)
780 sound_set(setting, global_settings.hw_eq_bands[b].width);
781 #endif /* AUDIOHW_HAVE_EQ_WIDTH */
783 #endif
786 void settings_apply(bool read_disk)
788 #ifdef HAVE_LCD_BITMAP
789 int rc;
790 #endif
791 #if CONFIG_CODEC == SWCODEC
792 int i;
793 #endif
794 sound_settings_apply();
796 #ifdef HAVE_DISK_STORAGE
797 audio_set_buffer_margin(global_settings.buffer_margin);
798 #endif
800 #ifdef HAVE_LCD_CONTRAST
801 lcd_set_contrast(global_settings.contrast);
802 #endif
803 lcd_scroll_speed(global_settings.scroll_speed);
804 #ifdef HAVE_REMOTE_LCD
805 lcd_remote_set_contrast(global_settings.remote_contrast);
806 lcd_remote_set_invert_display(global_settings.remote_invert);
808 #ifdef HAVE_LCD_FLIP
809 lcd_remote_set_flip(global_settings.remote_flip_display);
810 #endif
812 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
813 lcd_remote_scroll_step(global_settings.remote_scroll_step);
814 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
815 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
816 #ifdef HAVE_REMOTE_LCD_TICKING
817 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
818 #endif
819 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
820 #if CONFIG_CHARGING
821 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
822 #endif
823 #ifdef HAS_REMOTE_BUTTON_HOLD
824 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
825 #endif
826 #endif /* HAVE_REMOTE_LCD */
827 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
828 backlight_set_brightness(global_settings.brightness);
829 #endif
830 #ifdef HAVE_BACKLIGHT
831 backlight_set_timeout(global_settings.backlight_timeout);
832 #if CONFIG_CHARGING
833 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
834 #endif
835 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
836 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
837 backlight_set_fade_in(global_settings.backlight_fade_in);
838 backlight_set_fade_out(global_settings.backlight_fade_out);
839 #endif
840 #endif
841 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
842 buttonlight_set_brightness(global_settings.buttonlight_brightness);
843 #endif
844 #ifdef HAVE_BUTTON_LIGHT
845 buttonlight_set_timeout(global_settings.buttonlight_timeout);
846 #endif
847 #ifdef HAVE_DISK_STORAGE
848 storage_spindown(global_settings.disk_spindown);
849 #endif
850 #if (CONFIG_CODEC == MAS3507D) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
851 dac_line_in(global_settings.line_in);
852 #endif
853 set_poweroff_timeout(global_settings.poweroff);
855 #if defined(BATTERY_CAPACITY_INC) && BATTERY_CAPACITY_INC > 0
856 /* only call if it's really exchangable */
857 set_battery_capacity(global_settings.battery_capacity);
858 #endif
860 #if BATTERY_TYPES_COUNT > 1
861 set_battery_type(global_settings.battery_type);
862 #endif
864 #ifdef HAVE_LCD_BITMAP
865 #ifdef HAVE_LCD_INVERT
866 lcd_set_invert_display(global_settings.invert);
867 #endif
868 #ifdef HAVE_LCD_FLIP
869 lcd_set_flip(global_settings.flip_display);
870 button_set_flip(global_settings.flip_display);
871 #endif
872 lcd_update(); /* refresh after flipping the screen */
873 settings_apply_pm_range();
874 peak_meter_init_times(
875 global_settings.peak_meter_release, global_settings.peak_meter_hold,
876 global_settings.peak_meter_clip_hold);
877 #endif
879 #ifdef HAVE_SPEAKER
880 audiohw_enable_speaker(global_settings.speaker_enabled);
881 #endif
883 if (read_disk)
885 char buf[MAX_PATH];
886 #ifdef HAVE_LCD_BITMAP
887 char dir[MAX_PATH];
888 const char *font_path = get_user_file_path(FONT_DIR, 0, dir, sizeof(dir));
889 /* fonts need to be loaded before the WPS */
890 if (global_settings.font_file[0]
891 && global_settings.font_file[0] != '-') {
893 snprintf(buf, sizeof buf, "%s/%s.fnt", font_path,
894 global_settings.font_file);
895 CHART2(">font_load ", global_settings.font_file);
896 rc = font_load(NULL, buf);
897 CHART2("<font_load ", global_settings.font_file);
898 if (rc < 0)
899 font_reset(NULL);
901 else
902 font_reset(NULL);
903 #ifdef HAVE_REMOTE_LCD
904 if ( global_settings.remote_font_file[0]
905 && global_settings.remote_font_file[0] != '-') {
906 snprintf(buf, sizeof buf, "%s/%s.fnt", font_path,
907 global_settings.remote_font_file);
908 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
909 rc = font_load_remoteui(buf);
910 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
911 if (rc < 0)
912 font_load_remoteui(NULL);
914 else
915 font_load_remoteui(NULL);
916 #endif
917 if ( global_settings.kbd_file[0]) {
918 snprintf(buf, sizeof buf, "%s/%s.kbd",
919 get_user_file_path(ROCKBOX_DIR, 0, dir, sizeof(dir)),
920 global_settings.kbd_file);
921 CHART(">load_kbd");
922 load_kbd(buf);
923 CHART("<load_kbd");
925 else
926 load_kbd(NULL);
927 #endif /* HAVE_LCD_BITMAP */
928 /* no get_user_file_path() here because we don't really support
929 * langs that don't come with rockbox */
930 if ( global_settings.lang_file[0]) {
931 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
932 global_settings.lang_file);
933 CHART(">lang_core_load");
934 lang_core_load(buf);
935 CHART("<lang_core_load");
936 CHART(">talk_init");
937 talk_init(); /* use voice of same language */
938 CHART("<talk_init");
941 /* load the icon set */
942 CHART(">icons_init");
943 icons_init();
944 CHART("<icons_init");
946 #ifdef HAVE_LCD_COLOR
947 if (global_settings.colors_file[0])
949 CHART(">read_color_theme_file");
950 read_color_theme_file();
951 CHART("<read_color_theme_file");
953 #endif
955 #ifdef HAVE_LCD_COLOR
956 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
957 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
958 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
959 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
960 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
961 #endif
963 #ifdef HAVE_LCD_BITMAP
964 lcd_scroll_step(global_settings.scroll_step);
965 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
966 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
967 #endif
968 lcd_bidir_scroll(global_settings.bidir_limit);
969 lcd_scroll_delay(global_settings.scroll_delay);
972 CHART(">set_codepage");
973 set_codepage(global_settings.default_codepage);
974 CHART("<set_codepage");
976 #if CONFIG_CODEC == SWCODEC
977 #ifdef HAVE_CROSSFADE
978 audio_set_crossfade(global_settings.crossfade);
979 #endif
980 dsp_set_replaygain();
981 dsp_set_crossfeed(global_settings.crossfeed);
982 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
983 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
984 global_settings.crossfeed_hf_attenuation,
985 global_settings.crossfeed_hf_cutoff);
987 /* Configure software equalizer, hardware eq is handled in audio_init() */
988 dsp_set_eq(global_settings.eq_enabled);
989 dsp_set_eq_precut(global_settings.eq_precut);
990 for(i = 0; i < 5; i++) {
991 dsp_set_eq_coefs(i);
994 dsp_dither_enable(global_settings.dithering_enabled);
995 #ifdef HAVE_PITCHSCREEN
996 dsp_timestretch_enable(global_settings.timestretch_enabled);
997 #endif
998 dsp_set_compressor(global_settings.compressor_threshold,
999 global_settings.compressor_makeup_gain,
1000 global_settings.compressor_ratio,
1001 global_settings.compressor_knee,
1002 global_settings.compressor_release_time);
1003 #endif
1005 #ifdef HAVE_SPDIF_POWER
1006 spdif_power_enable(global_settings.spdif_enable);
1007 #endif
1009 #ifdef HAVE_BACKLIGHT
1010 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
1011 #ifdef HAVE_REMOTE_LCD
1012 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
1013 #endif
1014 #ifdef HAS_BUTTON_HOLD
1015 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
1016 #endif
1017 #ifdef HAVE_LCD_SLEEP_SETTING
1018 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
1019 #endif
1020 #endif /* HAVE_BACKLIGHT */
1022 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1023 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1024 #endif
1026 #ifdef HAVE_USB_CHARGING_ENABLE
1027 usb_charging_enable(global_settings.usb_charging);
1028 #endif
1030 #ifdef HAVE_TOUCHSCREEN
1031 touchscreen_set_mode(global_settings.touch_mode);
1032 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1033 #endif
1035 /* This should stay last */
1036 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1037 enc_global_settings_apply();
1038 #endif
1039 #ifdef HAVE_LCD_BITMAP
1040 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1041 CHART(">viewportmanager_theme_changed");
1042 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE|THEME_BUTTONBAR);
1043 CHART("<viewportmanager_theme_changed");
1044 #endif
1049 * reset all settings to their default value
1051 void reset_setting(const struct settings_list *setting, void *var)
1053 switch (setting->flags&F_T_MASK)
1055 case F_T_CUSTOM:
1056 setting->custom_setting->set_default(setting->setting,
1057 setting->default_val.custom);
1058 break;
1059 case F_T_INT:
1060 case F_T_UINT:
1061 if (setting->flags&F_DEF_ISFUNC)
1062 *(int*)var = setting->default_val.func();
1063 else if (setting->flags&F_T_SOUND)
1064 *(int*)var = sound_default(setting->sound_setting->setting);
1065 else *(int*)var = setting->default_val.int_;
1066 break;
1067 case F_T_BOOL:
1068 *(bool*)var = setting->default_val.bool_;
1069 break;
1070 case F_T_CHARPTR:
1071 case F_T_UCHARPTR:
1072 strlcpy((char*)var, setting->default_val.charptr,
1073 setting->filename_setting->max_len);
1074 break;
1078 void settings_reset(void)
1080 int i;
1082 for(i=0; i<nb_settings; i++)
1083 reset_setting(&settings[i], settings[i].setting);
1084 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1085 enc_global_settings_reset();
1086 #endif
1089 /** Changing setting values **/
1090 const struct settings_list* find_setting(const void* variable, int *id)
1092 int i;
1093 for(i=0;i<nb_settings;i++)
1095 if (settings[i].setting == variable)
1097 if (id)
1098 *id = i;
1099 return &settings[i];
1102 return NULL;
1105 bool set_bool(const char* string, const bool* variable )
1107 return set_bool_options(string, variable,
1108 (char *)STR(LANG_SET_BOOL_YES),
1109 (char *)STR(LANG_SET_BOOL_NO),
1110 NULL);
1114 bool set_bool_options(const char* string, const bool* variable,
1115 const char* yes_str, int yes_voice,
1116 const char* no_str, int no_voice,
1117 void (*function)(bool))
1119 struct opt_items names[] = {
1120 {(unsigned const char *)no_str, no_voice},
1121 {(unsigned const char *)yes_str, yes_voice}
1123 bool result;
1125 result = set_option(string, variable, BOOL, names, 2,
1126 (void (*)(int))function);
1127 return result;
1130 bool set_int(const unsigned char* string,
1131 const char* unit,
1132 int voice_unit,
1133 const int* variable,
1134 void (*function)(int),
1135 int step,
1136 int min,
1137 int max,
1138 const char* (*formatter)(char*, size_t, int, const char*) )
1140 return set_int_ex(string, unit, voice_unit, variable, function,
1141 step, min, max, formatter, NULL);
1144 bool set_int_ex(const unsigned char* string,
1145 const char* unit,
1146 int voice_unit,
1147 const int* variable,
1148 void (*function)(int),
1149 int step,
1150 int min,
1151 int max,
1152 const char* (*formatter)(char*, size_t, int, const char*),
1153 int32_t (*get_talk_id)(int, int))
1155 (void)unit;
1156 struct settings_list item;
1157 struct int_setting data = {
1158 function, voice_unit, min, max, step,
1159 formatter, get_talk_id
1161 item.int_setting = &data;
1162 item.flags = F_INT_SETTING|F_T_INT;
1163 item.lang_id = -1;
1164 item.cfg_vals = (char*)string;
1165 item.setting = (void *)variable;
1166 return option_screen(&item, NULL, false, NULL);
1170 static const struct opt_items *set_option_options;
1171 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1173 (void)buf, (void)unit, (void)size;
1174 return P2STR(set_option_options[item].string);
1177 static int32_t set_option_get_talk_id(int value, int unit)
1179 (void)unit;
1180 return set_option_options[value].voice_id;
1183 bool set_option(const char* string, const void* variable, enum optiontype type,
1184 const struct opt_items* options,
1185 int numoptions, void (*function)(int))
1187 int temp;
1188 struct settings_list item;
1189 struct int_setting data = {
1190 function, UNIT_INT, 0, numoptions-1, 1,
1191 set_option_formatter, set_option_get_talk_id
1193 set_option_options = options;
1194 item.int_setting = &data;
1195 item.flags = F_INT_SETTING|F_T_INT;
1196 item.lang_id = -1;
1197 item.cfg_vals = (char*)string;
1198 item.setting = &temp;
1199 if (type == BOOL)
1200 temp = *(bool*)variable? 1: 0;
1201 else
1202 temp = *(int*)variable;
1203 if (!option_screen(&item, NULL, false, NULL))
1205 if (type == BOOL)
1207 *(bool*)variable = (temp == 1);
1208 else
1209 *(int*)variable = temp;
1210 return false;
1212 return true;
1216 * Takes filename, removes the directory and the extension,
1217 * and then copies the basename into setting, unless the basename exceeds maxlen
1219 void set_file(const char* filename, char* setting, const int maxlen)
1221 const char* fptr = strrchr(filename,'/');
1222 const char* extptr;
1223 int len;
1224 int extlen = 0;
1226 if (!fptr)
1227 return;
1229 fptr++;
1231 extptr = strrchr(fptr, '.');
1233 if (!extptr || extptr < fptr)
1234 extlen = 0;
1235 else
1236 extlen = strlen(extptr);
1238 len = strlen(fptr) - extlen + 1;
1240 /* error if filename isn't in ROCKBOX_DIR */
1241 if (len > maxlen)
1242 return;
1244 strlcpy(setting, fptr, len);
1245 settings_save();