Prepare new maemo release
[maemo-rb.git] / apps / settings.c
blob1bb6c74b43d36436f962af4c9f5089d96a80557a
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_proc_settings.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 int fd = open(NVRAM_FILE, O_RDONLY);
122 int bytes;
123 if (fd < 0)
124 return false;
125 memset(buf,0,max_len);
126 bytes = read(fd,buf,max_len);
127 close(fd);
128 if (bytes < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
129 return false;
130 #else
131 memset(buf,0,max_len);
132 /* read rtc block */
133 for (i=0; i < max_len; i++ )
134 buf[i] = rtc_read(0x14+i);
135 #endif
136 /* check magic, version */
137 if ((buf[0] != 'R') || (buf[1] != 'b')
138 || (buf[2] != NVRAM_CONFIG_VERSION))
139 return false;
140 /* check crc32 */
141 crc32 = crc_32(&buf[NVRAM_DATA_START],
142 max_len-NVRAM_DATA_START-1,0xffffffff);
143 if (memcmp(&crc32,&buf[4],4))
144 return false;
145 /* all good, so read in the settings */
146 var_count = buf[3];
147 buf_pos = NVRAM_DATA_START;
148 for(i=0; i<nb_settings; i++)
150 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
151 >>F_NVRAM_MASK_SHIFT;
152 if (nvram_bytes)
154 if ((var_count>0) && (buf_pos<max_len))
156 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
157 buf_pos += nvram_bytes;
158 var_count--;
160 else /* should only happen when new items are added to the end */
162 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
166 return true;
168 static bool write_nvram_data(char* buf, int max_len)
170 unsigned crc32 = 0xffffffff;
171 int i = 0, buf_pos = 0;
172 char var_count = 0;
173 #ifndef HAVE_RTC_RAM
174 int fd;
175 #endif
176 memset(buf,0,max_len);
177 /* magic, version */
178 buf[0] = 'R'; buf[1] = 'b';
179 buf[2] = NVRAM_CONFIG_VERSION;
180 buf_pos = NVRAM_DATA_START;
181 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
183 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
184 >>F_NVRAM_MASK_SHIFT;
185 if (nvram_bytes)
187 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
188 buf_pos += nvram_bytes;
189 var_count++;
192 /* count and crc32 */
193 buf[3] = var_count;
194 crc32 = crc_32(&buf[NVRAM_DATA_START],
195 max_len-NVRAM_DATA_START-1,0xffffffff);
196 memcpy(&buf[4],&crc32,4);
197 #ifndef HAVE_RTC_RAM
198 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY, 0666);
199 if (fd >= 0)
201 int len = write(fd,buf,max_len);
202 close(fd);
203 if (len < 8)
204 return false;
206 #else
207 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
208 that it would write a number of bytes at a time since the RTC chip
209 supports that, but this will have to do for now 8-) */
210 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
211 int r = rtc_write(0x14+i, buf[i]);
212 if (r)
213 return false;
215 #endif
216 return true;
219 /** Reading from a config file **/
221 * load settings from disk or RTC RAM
223 void settings_load(int which)
225 if (which&SETTINGS_RTC)
226 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
227 if (which&SETTINGS_HD)
229 settings_load_config(CONFIGFILE, false);
230 settings_load_config(FIXEDSETTINGSFILE, false);
234 bool cfg_string_to_int(int setting_id, int* out, const char* str)
236 const char* start = settings[setting_id].cfg_vals;
237 char* end = NULL;
238 char temp[MAX_PATH];
239 int count = 0;
240 while (1)
242 end = strchr(start, ',');
243 if (!end)
245 if (!strcmp(str, start))
247 *out = count;
248 return true;
250 else return false;
252 strlcpy(temp, start, end-start+1);
253 if (!strcmp(str, temp))
255 *out = count;
256 return true;
258 start = end +1;
259 count++;
261 return false;
264 bool settings_load_config(const char* file, bool apply)
266 int fd;
267 char line[128];
268 char* name;
269 char* value;
270 int i;
271 bool theme_changed = false;
272 fd = open_utf8(file, O_RDONLY);
273 if (fd < 0)
274 return false;
276 while (read_line(fd, line, sizeof line) > 0)
278 if (!settings_parseline(line, &name, &value))
279 continue;
280 for(i=0; i<nb_settings; i++)
282 if (settings[i].cfg_name == NULL)
283 continue;
284 if (!strcasecmp(name,settings[i].cfg_name))
286 if (settings[i].flags&F_THEMESETTING)
287 theme_changed = true;
288 switch (settings[i].flags&F_T_MASK)
290 case F_T_CUSTOM:
291 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
292 break;
293 case F_T_INT:
294 case F_T_UINT:
295 #ifdef HAVE_LCD_COLOR
296 if (settings[i].flags&F_RGB)
297 hex_to_rgb(value, (int*)settings[i].setting);
298 else
299 #endif
300 if (settings[i].cfg_vals == NULL)
302 *(int*)settings[i].setting = atoi(value);
304 else
306 int temp, *v = (int*)settings[i].setting;
307 bool found = cfg_string_to_int(i, &temp, value);
308 if (found)
310 if (settings[i].flags&F_TABLE_SETTING)
311 *v = settings[i].table_setting->values[temp];
312 else
313 *v = temp;
315 else
316 { /* atoi breaks choice settings because they
317 * don't have int-like values, and would
318 * fall back to the first value (i.e. 0)
319 * due to atoi */
320 if (!(settings[i].flags&F_CHOICE_SETTING))
321 *v = atoi(value);
324 break;
325 case F_T_BOOL:
327 int temp;
328 if (cfg_string_to_int(i,&temp,value))
329 *(bool*)settings[i].setting = (temp!=0);
330 if (settings[i].bool_setting->option_callback)
331 settings[i].bool_setting->option_callback(temp!=0);
332 break;
334 case F_T_CHARPTR:
335 case F_T_UCHARPTR:
337 char storage[MAX_PATH];
338 if (settings[i].filename_setting->prefix)
340 const char *dir = settings[i].filename_setting->prefix;
341 size_t len = strlen(dir);
342 if (!strncasecmp(value, dir, len))
344 strlcpy(storage, &value[len], MAX_PATH);
346 else strlcpy(storage, value, MAX_PATH);
348 else strlcpy(storage, value, MAX_PATH);
349 if (settings[i].filename_setting->suffix)
351 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
352 if (s) *s = '\0';
354 strlcpy((char*)settings[i].setting, storage,
355 settings[i].filename_setting->max_len);
356 break;
359 break;
360 } /* if (!strcmp(name,settings[i].cfg_name)) */
361 } /* for(...) */
362 } /* while(...) */
364 close(fd);
365 if (apply)
367 settings_save();
368 settings_apply(true);
369 if (theme_changed)
370 settings_apply_skins();
372 return true;
375 /** Writing to a config file and saving settings **/
377 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
379 int flags = settings[setting_id].flags;
380 const char* start = settings[setting_id].cfg_vals;
381 char* end = NULL;
382 int count = 0;
384 if ((flags&F_T_MASK)==F_T_INT &&
385 flags&F_TABLE_SETTING)
387 const int *value = settings[setting_id].table_setting->values;
388 while (start)
390 end = strchr(start,',');
391 if (value[count] == val)
393 if (end == NULL)
394 strlcpy(buf, start, buf_len);
395 else
397 int len = (buf_len > (end-start))? end-start: buf_len;
398 strlcpy(buf, start, len+1);
400 return true;
402 count++;
404 if (end)
405 start = end+1;
406 else
407 break;
409 return false;
412 while (count < val)
414 start = strchr(start,',');
415 if (!start)
416 return false;
417 count++;
418 start++;
420 end = strchr(start,',');
421 if (end == NULL)
422 strlcpy(buf, start, buf_len);
423 else
425 int len = (buf_len > (end-start))? end-start: buf_len;
426 strlcpy(buf, start, len+1);
428 return true;
431 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
433 switch (settings[i].flags&F_T_MASK)
435 case F_T_CUSTOM:
436 settings[i].custom_setting->write_to_cfg(settings[i].setting,
437 buf, buf_len);
438 break;
439 case F_T_INT:
440 case F_T_UINT:
441 #ifdef HAVE_LCD_COLOR
442 if (settings[i].flags&F_RGB)
444 int colour = *(int*)settings[i].setting;
445 snprintf(buf,buf_len,"%02x%02x%02x",
446 (int)RGB_UNPACK_RED(colour),
447 (int)RGB_UNPACK_GREEN(colour),
448 (int)RGB_UNPACK_BLUE(colour));
450 else
451 #endif
452 if (settings[i].cfg_vals == NULL)
454 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
456 else
458 if (cfg_int_to_string(i, *(int*)settings[i].setting,
459 buf, buf_len) == false)
461 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
463 else
464 return false;
466 break;
467 case F_T_BOOL:
468 cfg_int_to_string(i,
469 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
470 break;
471 case F_T_CHARPTR:
472 case F_T_UCHARPTR:
473 if (((char*)settings[i].setting)[0]
474 && settings[i].filename_setting->prefix)
476 if (((char*)settings[i].setting)[0] == '-')
478 buf[0] = '-';
479 buf[1] = '\0';
481 else
483 snprintf(buf,buf_len,"%s%s%s",
484 settings[i].filename_setting->prefix,
485 (char*)settings[i].setting,
486 settings[i].filename_setting->suffix);
489 else strlcpy(buf,(char*)settings[i].setting,
490 settings[i].filename_setting->max_len);
491 break;
492 } /* switch () */
493 return true;
497 static bool is_changed(int setting_id)
499 const struct settings_list *setting = &settings[setting_id];
500 switch (setting->flags&F_T_MASK)
502 case F_T_CUSTOM:
503 return setting->custom_setting->is_changed(setting->setting,
504 setting->default_val.custom);
505 break;
506 case F_T_INT:
507 case F_T_UINT:
508 if (setting->flags&F_DEF_ISFUNC)
510 if (*(int*)setting->setting == setting->default_val.func())
511 return false;
513 else if (setting->flags&F_T_SOUND)
515 if (*(int*)setting->setting ==
516 sound_default(setting->sound_setting->setting))
517 return false;
519 else if (*(int*)setting->setting == setting->default_val.int_)
520 return false;
521 break;
522 case F_T_BOOL:
523 if (*(bool*)setting->setting == setting->default_val.bool_)
524 return false;
525 break;
526 case F_T_CHARPTR:
527 case F_T_UCHARPTR:
528 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
529 return false;
530 break;
532 return true;
535 static bool settings_write_config(const char* filename, int options)
537 int i;
538 int fd;
539 char value[MAX_PATH];
540 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY, 0666);
541 if (fd < 0)
542 return false;
543 fdprintf(fd, "# .cfg file created by rockbox %s - "
544 "http://www.rockbox.org\r\n\r\n", rbversion);
545 for(i=0; i<nb_settings; i++)
547 if (settings[i].cfg_name == NULL)
548 continue;
549 value[0] = '\0';
550 if (settings[i].flags & F_DEPRECATED)
551 continue;
553 switch (options)
555 case SETTINGS_SAVE_CHANGED:
556 if (!is_changed(i))
557 continue;
558 break;
559 case SETTINGS_SAVE_SOUND:
560 if ((settings[i].flags&F_SOUNDSETTING) == 0)
561 continue;
562 break;
563 case SETTINGS_SAVE_THEME:
564 if ((settings[i].flags&F_THEMESETTING) == 0)
565 continue;
566 break;
567 #ifdef HAVE_RECORDING
568 case SETTINGS_SAVE_RECPRESETS:
569 if ((settings[i].flags&F_RECSETTING) == 0)
570 continue;
571 break;
572 #endif
573 #if CONFIG_CODEC == SWCODEC
574 case SETTINGS_SAVE_EQPRESET:
575 if ((settings[i].flags&F_EQSETTING) == 0)
576 continue;
577 break;
578 #endif
581 cfg_to_string(i, value, MAX_PATH);
582 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
583 } /* for(...) */
584 close(fd);
585 return true;
587 #ifndef HAVE_RTC_RAM
588 static void flush_global_status_callback(void *data)
590 (void)data;
591 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
593 #endif
594 static void flush_config_block_callback(void *data)
596 (void)data;
597 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
598 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
602 * persist all runtime user settings to disk or RTC RAM
604 static void update_runtime(void)
606 int elapsed_secs;
608 elapsed_secs = (current_tick - lasttime) / HZ;
609 global_status.runtime += elapsed_secs;
610 lasttime += (elapsed_secs * HZ);
612 if ( global_status.runtime > global_status.topruntime )
613 global_status.topruntime = global_status.runtime;
616 void status_save(void)
618 update_runtime();
619 #ifdef HAVE_RTC_RAM
620 /* this will be done in the storage_callback if
621 target doesnt have rtc ram */
622 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
623 #else
624 register_storage_idle_func(flush_global_status_callback);
625 #endif
628 int settings_save(void)
630 update_runtime();
631 #ifdef HAVE_RTC_RAM
632 /* this will be done in the storage_callback if
633 target doesnt have rtc ram */
634 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
635 #endif
636 register_storage_idle_func(flush_config_block_callback);
637 return 0;
640 bool settings_save_config(int options)
642 char filename[MAX_PATH];
643 const char *folder, *namebase;
644 switch (options)
646 case SETTINGS_SAVE_THEME:
647 folder = THEME_DIR;
648 namebase = "theme";
649 break;
650 #ifdef HAVE_RECORDING
651 case SETTINGS_SAVE_RECPRESETS:
652 folder = RECPRESETS_DIR;
653 namebase = "recording";
654 break;
655 #endif
656 #if CONFIG_CODEC == SWCODEC
657 case SETTINGS_SAVE_EQPRESET:
658 folder = EQS_DIR;
659 namebase = "eq";
660 break;
661 #endif
662 case SETTINGS_SAVE_SOUND:
663 folder = ROCKBOX_DIR;
664 namebase = "sound";
665 break;
666 default:
667 folder = ROCKBOX_DIR;
668 namebase = "config";
669 break;
671 create_numbered_filename(filename, folder, namebase, ".cfg", 2
672 IF_CNFN_NUM_(, NULL));
674 /* allow user to modify filename */
675 while (true) {
676 if (!kbd_input(filename, sizeof filename)) {
677 break;
679 else {
680 return false;
684 if (settings_write_config(filename, options))
685 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
686 else
687 splash(HZ, ID2P(LANG_FAILED));
688 return true;
691 /** Apply and Reset settings **/
694 #ifdef HAVE_LCD_BITMAP
696 * Applies the range infos stored in global_settings to
697 * the peak meter.
699 void settings_apply_pm_range(void)
701 int pm_min, pm_max;
703 /* depending on the scale mode (dBfs or percent) the values
704 of global_settings.peak_meter_dbfs have different meanings */
705 if (global_settings.peak_meter_dbfs)
707 /* convert to dBfs * 100 */
708 pm_min = -(((int)global_settings.peak_meter_min) * 100);
709 pm_max = -(((int)global_settings.peak_meter_max) * 100);
711 else
713 /* percent is stored directly -> no conversion */
714 pm_min = global_settings.peak_meter_min;
715 pm_max = global_settings.peak_meter_max;
718 /* apply the range */
719 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
721 #endif /* HAVE_LCD_BITMAP */
723 void sound_settings_apply(void)
725 #if CONFIG_CODEC == SWCODEC
726 sound_set_dsp_callback(dsp_callback);
727 #endif
728 #ifdef AUDIOHW_HAVE_BASS
729 sound_set(SOUND_BASS, global_settings.bass);
730 #endif
731 #ifdef AUDIOHW_HAVE_TREBLE
732 sound_set(SOUND_TREBLE, global_settings.treble);
733 #endif
734 sound_set(SOUND_BALANCE, global_settings.balance);
735 #ifndef PLATFORM_HAS_VOLUME_CHANGE
736 sound_set(SOUND_VOLUME, global_settings.volume);
737 #endif
738 sound_set(SOUND_CHANNELS, global_settings.channel_config);
739 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
740 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
741 sound_set(SOUND_LOUDNESS, global_settings.loudness);
742 sound_set(SOUND_AVC, global_settings.avc);
743 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
744 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
745 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
746 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
747 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
748 sound_set(SOUND_SUPERBASS, global_settings.superbass);
749 #endif
750 #ifdef AUDIOHW_HAVE_BASS_CUTOFF
751 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
752 #endif
753 #ifdef AUDIOHW_HAVE_TREBLE_CUTOFF
754 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
755 #endif
756 #ifdef AUDIOHW_HAVE_DEPTH_3D
757 sound_set(SOUND_DEPTH_3D, global_settings.depth_3d);
758 #endif
759 #ifdef AUDIOHW_HAVE_EQ
760 int b;
762 for (b = 0; b < AUDIOHW_EQ_BAND_NUM; b++)
764 int setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_GAIN);
765 sound_set(setting, global_settings.hw_eq_bands[b].gain);
767 #ifdef AUDIOHW_HAVE_EQ_FREQUENCY
768 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_FREQUENCY);
769 if (setting != -1)
770 sound_set(setting, global_settings.hw_eq_bands[b].frequency);
771 #endif /* AUDIOHW_HAVE_EQ_FREQUENCY */
772 #ifdef AUDIOHW_HAVE_EQ_WIDTH
773 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_WIDTH);
774 if (setting != -1)
775 sound_set(setting, global_settings.hw_eq_bands[b].width);
776 #endif /* AUDIOHW_HAVE_EQ_WIDTH */
778 #endif
781 void settings_apply(bool read_disk)
783 #ifdef HAVE_LCD_BITMAP
784 int rc;
785 #endif
786 sound_settings_apply();
788 #ifdef HAVE_DISK_STORAGE
789 audio_set_buffer_margin(global_settings.buffer_margin);
790 #endif
792 #ifdef HAVE_LCD_CONTRAST
793 lcd_set_contrast(global_settings.contrast);
794 #endif
795 lcd_scroll_speed(global_settings.scroll_speed);
796 #ifdef HAVE_REMOTE_LCD
797 lcd_remote_set_contrast(global_settings.remote_contrast);
798 lcd_remote_set_invert_display(global_settings.remote_invert);
800 #ifdef HAVE_LCD_FLIP
801 lcd_remote_set_flip(global_settings.remote_flip_display);
802 #endif
804 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
805 lcd_remote_scroll_step(global_settings.remote_scroll_step);
806 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
807 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
808 #ifdef HAVE_REMOTE_LCD_TICKING
809 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
810 #endif
811 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
812 #if CONFIG_CHARGING
813 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
814 #endif
815 #ifdef HAS_REMOTE_BUTTON_HOLD
816 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
817 #endif
818 #endif /* HAVE_REMOTE_LCD */
819 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
820 backlight_set_brightness(global_settings.brightness);
821 #endif
822 #ifdef HAVE_BACKLIGHT
823 backlight_set_timeout(global_settings.backlight_timeout);
824 #if CONFIG_CHARGING
825 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
826 #endif
827 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
828 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
829 backlight_set_fade_in(global_settings.backlight_fade_in);
830 backlight_set_fade_out(global_settings.backlight_fade_out);
831 #endif
832 #endif
833 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
834 buttonlight_set_brightness(global_settings.buttonlight_brightness);
835 #endif
836 #ifdef HAVE_BUTTON_LIGHT
837 buttonlight_set_timeout(global_settings.buttonlight_timeout);
838 #endif
839 #ifdef HAVE_DISK_STORAGE
840 storage_spindown(global_settings.disk_spindown);
841 #endif
842 #if (CONFIG_CODEC == MAS3507D) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
843 dac_line_in(global_settings.line_in);
844 #endif
845 set_poweroff_timeout(global_settings.poweroff);
846 if (global_settings.sleeptimer_on_startup)
847 set_sleeptimer_duration(global_settings.sleeptimer_duration);
848 set_keypress_restarts_sleep_timer(
849 global_settings.keypress_restarts_sleeptimer);
851 #if defined(BATTERY_CAPACITY_INC) && BATTERY_CAPACITY_INC > 0
852 /* only call if it's really exchangable */
853 set_battery_capacity(global_settings.battery_capacity);
854 #endif
856 #if BATTERY_TYPES_COUNT > 1
857 set_battery_type(global_settings.battery_type);
858 #endif
860 #ifdef HAVE_LCD_BITMAP
861 #ifdef HAVE_LCD_INVERT
862 lcd_set_invert_display(global_settings.invert);
863 #endif
864 #ifdef HAVE_LCD_FLIP
865 lcd_set_flip(global_settings.flip_display);
866 button_set_flip(global_settings.flip_display);
867 #endif
868 lcd_update(); /* refresh after flipping the screen */
869 settings_apply_pm_range();
870 peak_meter_init_times(
871 global_settings.peak_meter_release, global_settings.peak_meter_hold,
872 global_settings.peak_meter_clip_hold);
873 #endif
875 #ifdef HAVE_SPEAKER
876 audiohw_enable_speaker(global_settings.speaker_enabled);
877 #endif
879 if (read_disk)
881 char buf[MAX_PATH];
882 #ifdef HAVE_LCD_BITMAP
883 /* fonts need to be loaded before the WPS */
884 if (global_settings.font_file[0]
885 && global_settings.font_file[0] != '-') {
886 int font_ui = screens[SCREEN_MAIN].getuifont();
887 const char* loaded_font = font_filename(font_ui);
889 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
890 global_settings.font_file);
891 if (!loaded_font || strcmp(loaded_font, buf))
893 CHART2(">font_load ", global_settings.font_file);
894 if (font_ui >= 0)
895 font_unload(font_ui);
896 rc = font_load_ex(buf, 0, global_settings.glyphs_to_cache);
897 CHART2("<font_load ", global_settings.font_file);
898 screens[SCREEN_MAIN].setuifont(rc);
899 screens[SCREEN_MAIN].setfont(rc);
902 #ifdef HAVE_REMOTE_LCD
903 if ( global_settings.remote_font_file[0]
904 && global_settings.remote_font_file[0] != '-') {
905 int font_ui = screens[SCREEN_REMOTE].getuifont();
906 const char* loaded_font = font_filename(font_ui);
907 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
908 global_settings.remote_font_file);
909 if (!loaded_font || strcmp(loaded_font, buf))
911 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
912 if (font_ui >= 0)
913 font_unload(font_ui);
914 rc = font_load(buf);
915 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
916 screens[SCREEN_REMOTE].setuifont(rc);
917 screens[SCREEN_REMOTE].setfont(rc);
920 #endif
921 if ( global_settings.kbd_file[0]
922 && global_settings.kbd_file[0] != '-') {
923 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
924 global_settings.kbd_file);
925 CHART(">load_kbd");
926 load_kbd(buf);
927 CHART("<load_kbd");
929 else
930 load_kbd(NULL);
931 #endif /* HAVE_LCD_BITMAP */
932 if ( global_settings.lang_file[0]) {
933 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
934 global_settings.lang_file);
935 CHART(">lang_core_load");
936 lang_core_load(buf);
937 CHART("<lang_core_load");
938 CHART(">talk_init");
939 talk_init(); /* use voice of same language */
940 CHART("<talk_init");
943 /* load the icon set */
944 CHART(">icons_init");
945 icons_init();
946 CHART("<icons_init");
948 #ifdef HAVE_LCD_COLOR
949 if (global_settings.colors_file[0]
950 && global_settings.colors_file[0] != '-')
952 CHART(">read_color_theme_file");
953 read_color_theme_file();
954 CHART("<read_color_theme_file");
956 #endif
958 #ifdef HAVE_LCD_COLOR
959 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
960 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
961 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
962 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
963 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
964 #endif
966 #ifdef HAVE_LCD_BITMAP
967 lcd_scroll_step(global_settings.scroll_step);
968 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
969 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
970 #endif
971 lcd_bidir_scroll(global_settings.bidir_limit);
972 lcd_scroll_delay(global_settings.scroll_delay);
975 CHART(">set_codepage");
976 set_codepage(global_settings.default_codepage);
977 CHART("<set_codepage");
979 #if CONFIG_CODEC == SWCODEC
980 #ifdef HAVE_CROSSFADE
981 audio_set_crossfade(global_settings.crossfade);
982 #endif
983 replaygain_update();
984 dsp_set_crossfeed_type(global_settings.crossfeed);
985 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
986 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
987 global_settings.crossfeed_hf_attenuation,
988 global_settings.crossfeed_hf_cutoff);
990 /* Configure software equalizer, hardware eq is handled in audio_init() */
991 dsp_eq_enable(global_settings.eq_enabled);
992 dsp_set_eq_precut(global_settings.eq_precut);
993 for(int i = 0; i < EQ_NUM_BANDS; i++) {
994 dsp_set_eq_coefs(i, &global_settings.eq_band_settings[i]);
997 dsp_dither_enable(global_settings.dithering_enabled);
998 #ifdef HAVE_PITCHCONTROL
999 dsp_timestretch_enable(global_settings.timestretch_enabled);
1000 #endif
1001 dsp_set_compressor(&global_settings.compressor_settings);
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 for(int i=0; i<nb_settings; i++)
1080 reset_setting(&settings[i], settings[i].setting);
1081 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1082 enc_global_settings_reset();
1083 #endif
1084 #ifdef HAVE_LCD_BITMAP
1085 FOR_NB_SCREENS(i)
1087 if (screens[i].getuifont() > FONT_SYSFIXED)
1089 font_unload(screens[i].getuifont());
1090 screens[i].setuifont(FONT_SYSFIXED);
1091 screens[i].setfont(FONT_SYSFIXED);
1094 #endif
1097 /** Changing setting values **/
1098 const struct settings_list* find_setting(const void* variable, int *id)
1100 int i;
1101 for(i=0;i<nb_settings;i++)
1103 if (settings[i].setting == variable)
1105 if (id)
1106 *id = i;
1107 return &settings[i];
1110 return NULL;
1112 const struct settings_list* find_setting_by_cfgname(const char* name, int *id)
1114 int i;
1115 for (i=0; i<nb_settings; i++)
1117 if (settings[i].cfg_name &&
1118 !strcmp(settings[i].cfg_name, name))
1120 if (id) *id = i;
1121 return &settings[i];
1124 return NULL;
1127 bool set_bool(const char* string, const bool* variable )
1129 return set_bool_options(string, variable,
1130 (char *)STR(LANG_SET_BOOL_YES),
1131 (char *)STR(LANG_SET_BOOL_NO),
1132 NULL);
1136 bool set_bool_options(const char* string, const bool* variable,
1137 const char* yes_str, int yes_voice,
1138 const char* no_str, int no_voice,
1139 void (*function)(bool))
1141 struct opt_items names[] = {
1142 {(unsigned const char *)no_str, no_voice},
1143 {(unsigned const char *)yes_str, yes_voice}
1145 bool result;
1147 result = set_option(string, variable, BOOL, names, 2,
1148 (void (*)(int))function);
1149 return result;
1152 bool set_int(const unsigned char* string,
1153 const char* unit,
1154 int voice_unit,
1155 const int* variable,
1156 void (*function)(int),
1157 int step,
1158 int min,
1159 int max,
1160 const char* (*formatter)(char*, size_t, int, const char*) )
1162 return set_int_ex(string, unit, voice_unit, variable, function,
1163 step, min, max, formatter, NULL);
1166 bool set_int_ex(const unsigned char* string,
1167 const char* unit,
1168 int voice_unit,
1169 const int* variable,
1170 void (*function)(int),
1171 int step,
1172 int min,
1173 int max,
1174 const char* (*formatter)(char*, size_t, int, const char*),
1175 int32_t (*get_talk_id)(int, int))
1177 (void)unit;
1178 struct settings_list item;
1179 struct int_setting data = {
1180 function, voice_unit, min, max, step,
1181 formatter, get_talk_id
1183 item.int_setting = &data;
1184 item.flags = F_INT_SETTING|F_T_INT;
1185 item.lang_id = -1;
1186 item.cfg_vals = (char*)string;
1187 item.setting = (void *)variable;
1188 return option_screen(&item, NULL, false, NULL);
1192 static const struct opt_items *set_option_options;
1193 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1195 (void)buf, (void)unit, (void)size;
1196 return P2STR(set_option_options[item].string);
1199 static int32_t set_option_get_talk_id(int value, int unit)
1201 (void)unit;
1202 return set_option_options[value].voice_id;
1205 bool set_option(const char* string, const void* variable, enum optiontype type,
1206 const struct opt_items* options,
1207 int numoptions, void (*function)(int))
1209 int temp;
1210 struct settings_list item;
1211 struct int_setting data = {
1212 function, UNIT_INT, 0, numoptions-1, 1,
1213 set_option_formatter, set_option_get_talk_id
1215 set_option_options = options;
1216 item.int_setting = &data;
1217 item.flags = F_INT_SETTING|F_T_INT;
1218 item.lang_id = -1;
1219 item.cfg_vals = (char*)string;
1220 item.setting = &temp;
1221 if (type == BOOL)
1222 temp = *(bool*)variable? 1: 0;
1223 else
1224 temp = *(int*)variable;
1225 if (!option_screen(&item, NULL, false, NULL))
1227 if (type == BOOL)
1229 *(bool*)variable = (temp == 1);
1230 else
1231 *(int*)variable = temp;
1232 return false;
1234 return true;
1238 * Takes filename, removes the directory and the extension,
1239 * and then copies the basename into setting, unless the basename exceeds maxlen
1241 void set_file(const char* filename, char* setting, const int maxlen)
1243 const char* fptr = strrchr(filename,'/');
1244 const char* extptr;
1245 int len;
1246 int extlen = 0;
1248 if (!fptr)
1249 return;
1251 fptr++;
1253 extptr = strrchr(fptr, '.');
1255 if (!extptr || extptr < fptr)
1256 extlen = 0;
1257 else
1258 extlen = strlen(extptr);
1260 len = strlen(fptr) - extlen + 1;
1262 /* error if filename isn't in ROCKBOX_DIR */
1263 if (len > maxlen)
1264 return;
1266 strlcpy(setting, fptr, len);
1267 settings_save();