Add new tarball generation script.
[maemo-rb.git] / apps / settings.c
blob13913f2d855a18194577568fd2569d9bc86dc5d7
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 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';
551 switch (options)
553 case SETTINGS_SAVE_CHANGED:
554 if (!is_changed(i))
555 continue;
556 break;
557 case SETTINGS_SAVE_SOUND:
558 if ((settings[i].flags&F_SOUNDSETTING) == 0)
559 continue;
560 break;
561 case SETTINGS_SAVE_THEME:
562 if ((settings[i].flags&F_THEMESETTING) == 0)
563 continue;
564 break;
565 #ifdef HAVE_RECORDING
566 case SETTINGS_SAVE_RECPRESETS:
567 if ((settings[i].flags&F_RECSETTING) == 0)
568 continue;
569 break;
570 #endif
571 #if CONFIG_CODEC == SWCODEC
572 case SETTINGS_SAVE_EQPRESET:
573 if ((settings[i].flags&F_EQSETTING) == 0)
574 continue;
575 break;
576 #endif
579 cfg_to_string(i, value, MAX_PATH);
580 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
581 } /* for(...) */
582 close(fd);
583 return true;
585 #ifndef HAVE_RTC_RAM
586 static void flush_global_status_callback(void *data)
588 (void)data;
589 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
591 #endif
592 static void flush_config_block_callback(void *data)
594 (void)data;
595 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
596 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
600 * persist all runtime user settings to disk or RTC RAM
602 static void update_runtime(void)
604 int elapsed_secs;
606 elapsed_secs = (current_tick - lasttime) / HZ;
607 global_status.runtime += elapsed_secs;
608 lasttime += (elapsed_secs * HZ);
610 if ( global_status.runtime > global_status.topruntime )
611 global_status.topruntime = global_status.runtime;
614 void status_save(void)
616 update_runtime();
617 #ifdef HAVE_RTC_RAM
618 /* this will be done in the storage_callback if
619 target doesnt have rtc ram */
620 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
621 #else
622 register_storage_idle_func(flush_global_status_callback);
623 #endif
626 int settings_save(void)
628 update_runtime();
629 #ifdef HAVE_RTC_RAM
630 /* this will be done in the storage_callback if
631 target doesnt have rtc ram */
632 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
633 #endif
634 register_storage_idle_func(flush_config_block_callback);
635 return 0;
638 bool settings_save_config(int options)
640 char filename[MAX_PATH];
641 const char *folder, *namebase;
642 switch (options)
644 case SETTINGS_SAVE_THEME:
645 folder = THEME_DIR;
646 namebase = "theme";
647 break;
648 #ifdef HAVE_RECORDING
649 case SETTINGS_SAVE_RECPRESETS:
650 folder = RECPRESETS_DIR;
651 namebase = "recording";
652 break;
653 #endif
654 #if CONFIG_CODEC == SWCODEC
655 case SETTINGS_SAVE_EQPRESET:
656 folder = EQS_DIR;
657 namebase = "eq";
658 break;
659 #endif
660 case SETTINGS_SAVE_SOUND:
661 folder = ROCKBOX_DIR;
662 namebase = "sound";
663 break;
664 default:
665 folder = ROCKBOX_DIR;
666 namebase = "config";
667 break;
669 create_numbered_filename(filename, folder, namebase, ".cfg", 2
670 IF_CNFN_NUM_(, NULL));
672 /* allow user to modify filename */
673 while (true) {
674 if (!kbd_input(filename, sizeof filename)) {
675 break;
677 else {
678 return false;
682 if (settings_write_config(filename, options))
683 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
684 else
685 splash(HZ, ID2P(LANG_FAILED));
686 return true;
689 /** Apply and Reset settings **/
692 #ifdef HAVE_LCD_BITMAP
694 * Applies the range infos stored in global_settings to
695 * the peak meter.
697 void settings_apply_pm_range(void)
699 int pm_min, pm_max;
701 /* depending on the scale mode (dBfs or percent) the values
702 of global_settings.peak_meter_dbfs have different meanings */
703 if (global_settings.peak_meter_dbfs)
705 /* convert to dBfs * 100 */
706 pm_min = -(((int)global_settings.peak_meter_min) * 100);
707 pm_max = -(((int)global_settings.peak_meter_max) * 100);
709 else
711 /* percent is stored directly -> no conversion */
712 pm_min = global_settings.peak_meter_min;
713 pm_max = global_settings.peak_meter_max;
716 /* apply the range */
717 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
719 #endif /* HAVE_LCD_BITMAP */
721 void sound_settings_apply(void)
723 #if CONFIG_CODEC == SWCODEC
724 sound_set_dsp_callback(dsp_callback);
725 #endif
726 #ifdef AUDIOHW_HAVE_BASS
727 sound_set(SOUND_BASS, global_settings.bass);
728 #endif
729 #ifdef AUDIOHW_HAVE_TREBLE
730 sound_set(SOUND_TREBLE, global_settings.treble);
731 #endif
732 sound_set(SOUND_BALANCE, global_settings.balance);
733 #ifndef PLATFORM_HAS_VOLUME_CHANGE
734 sound_set(SOUND_VOLUME, global_settings.volume);
735 #endif
736 sound_set(SOUND_CHANNELS, global_settings.channel_config);
737 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
738 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
739 sound_set(SOUND_LOUDNESS, global_settings.loudness);
740 sound_set(SOUND_AVC, global_settings.avc);
741 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
742 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
743 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
744 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
745 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
746 sound_set(SOUND_SUPERBASS, global_settings.superbass);
747 #endif
748 #ifdef AUDIOHW_HAVE_BASS_CUTOFF
749 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
750 #endif
751 #ifdef AUDIOHW_HAVE_TREBLE_CUTOFF
752 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
753 #endif
754 #ifdef AUDIOHW_HAVE_DEPTH_3D
755 sound_set(SOUND_DEPTH_3D, global_settings.depth_3d);
756 #endif
757 #ifdef AUDIOHW_HAVE_EQ
758 int b;
760 for (b = 0; b < AUDIOHW_EQ_BAND_NUM; b++)
762 int setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_GAIN);
763 sound_set(setting, global_settings.hw_eq_bands[b].gain);
765 #ifdef AUDIOHW_HAVE_EQ_FREQUENCY
766 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_FREQUENCY);
767 if (setting != -1)
768 sound_set(setting, global_settings.hw_eq_bands[b].frequency);
769 #endif /* AUDIOHW_HAVE_EQ_FREQUENCY */
770 #ifdef AUDIOHW_HAVE_EQ_WIDTH
771 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_WIDTH);
772 if (setting != -1)
773 sound_set(setting, global_settings.hw_eq_bands[b].width);
774 #endif /* AUDIOHW_HAVE_EQ_WIDTH */
776 #endif
779 void settings_apply(bool read_disk)
781 #ifdef HAVE_LCD_BITMAP
782 int rc;
783 #endif
784 #if CONFIG_CODEC == SWCODEC
785 int i;
786 #endif
787 sound_settings_apply();
789 #ifdef HAVE_DISK_STORAGE
790 audio_set_buffer_margin(global_settings.buffer_margin);
791 #endif
793 #ifdef HAVE_LCD_CONTRAST
794 lcd_set_contrast(global_settings.contrast);
795 #endif
796 lcd_scroll_speed(global_settings.scroll_speed);
797 #ifdef HAVE_REMOTE_LCD
798 lcd_remote_set_contrast(global_settings.remote_contrast);
799 lcd_remote_set_invert_display(global_settings.remote_invert);
801 #ifdef HAVE_LCD_FLIP
802 lcd_remote_set_flip(global_settings.remote_flip_display);
803 #endif
805 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
806 lcd_remote_scroll_step(global_settings.remote_scroll_step);
807 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
808 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
809 #ifdef HAVE_REMOTE_LCD_TICKING
810 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
811 #endif
812 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
813 #if CONFIG_CHARGING
814 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
815 #endif
816 #ifdef HAS_REMOTE_BUTTON_HOLD
817 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
818 #endif
819 #endif /* HAVE_REMOTE_LCD */
820 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
821 backlight_set_brightness(global_settings.brightness);
822 #endif
823 #ifdef HAVE_BACKLIGHT
824 backlight_set_timeout(global_settings.backlight_timeout);
825 #if CONFIG_CHARGING
826 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
827 #endif
828 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
829 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
830 backlight_set_fade_in(global_settings.backlight_fade_in);
831 backlight_set_fade_out(global_settings.backlight_fade_out);
832 #endif
833 #endif
834 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
835 buttonlight_set_brightness(global_settings.buttonlight_brightness);
836 #endif
837 #ifdef HAVE_BUTTON_LIGHT
838 buttonlight_set_timeout(global_settings.buttonlight_timeout);
839 #endif
840 #ifdef HAVE_DISK_STORAGE
841 storage_spindown(global_settings.disk_spindown);
842 #endif
843 #if (CONFIG_CODEC == MAS3507D) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
844 dac_line_in(global_settings.line_in);
845 #endif
846 set_poweroff_timeout(global_settings.poweroff);
847 if (global_settings.sleeptimer_on_startup)
848 set_sleep_timer(global_settings.sleeptimer_duration * 60);
849 set_keypress_restarts_sleep_timer(
850 global_settings.keypress_restarts_sleeptimer);
852 #if defined(BATTERY_CAPACITY_INC) && BATTERY_CAPACITY_INC > 0
853 /* only call if it's really exchangable */
854 set_battery_capacity(global_settings.battery_capacity);
855 #endif
857 #if BATTERY_TYPES_COUNT > 1
858 set_battery_type(global_settings.battery_type);
859 #endif
861 #ifdef HAVE_LCD_BITMAP
862 #ifdef HAVE_LCD_INVERT
863 lcd_set_invert_display(global_settings.invert);
864 #endif
865 #ifdef HAVE_LCD_FLIP
866 lcd_set_flip(global_settings.flip_display);
867 button_set_flip(global_settings.flip_display);
868 #endif
869 lcd_update(); /* refresh after flipping the screen */
870 settings_apply_pm_range();
871 peak_meter_init_times(
872 global_settings.peak_meter_release, global_settings.peak_meter_hold,
873 global_settings.peak_meter_clip_hold);
874 #endif
876 #ifdef HAVE_SPEAKER
877 audiohw_enable_speaker(global_settings.speaker_enabled);
878 #endif
880 if (read_disk)
882 char buf[MAX_PATH];
883 #ifdef HAVE_LCD_BITMAP
884 /* fonts need to be loaded before the WPS */
885 if (global_settings.font_file[0]
886 && global_settings.font_file[0] != '-') {
887 int font_ui = screens[SCREEN_MAIN].getuifont();
888 const char* loaded_font = font_filename(font_ui);
890 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
891 global_settings.font_file);
892 if (!loaded_font || strcmp(loaded_font, buf))
894 CHART2(">font_load ", global_settings.font_file);
895 if (font_ui >= 0)
896 font_unload(font_ui);
897 rc = font_load_ex(buf, 0, global_settings.glyphs_to_cache);
898 CHART2("<font_load ", global_settings.font_file);
899 screens[SCREEN_MAIN].setuifont(rc);
900 screens[SCREEN_MAIN].setfont(rc);
903 #ifdef HAVE_REMOTE_LCD
904 if ( global_settings.remote_font_file[0]
905 && global_settings.remote_font_file[0] != '-') {
906 int font_ui = screens[SCREEN_REMOTE].getuifont();
907 const char* loaded_font = font_filename(font_ui);
908 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
909 global_settings.remote_font_file);
910 if (!loaded_font || strcmp(loaded_font, buf))
912 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
913 if (font_ui >= 0)
914 font_unload(font_ui);
915 rc = font_load(buf);
916 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
917 screens[SCREEN_REMOTE].setuifont(rc);
918 screens[SCREEN_REMOTE].setfont(rc);
921 #endif
922 if ( global_settings.kbd_file[0]
923 && global_settings.kbd_file[0] != '-') {
924 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
925 global_settings.kbd_file);
926 CHART(">load_kbd");
927 load_kbd(buf);
928 CHART("<load_kbd");
930 else
931 load_kbd(NULL);
932 #endif /* HAVE_LCD_BITMAP */
933 if ( global_settings.lang_file[0]) {
934 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
935 global_settings.lang_file);
936 CHART(">lang_core_load");
937 lang_core_load(buf);
938 CHART("<lang_core_load");
939 CHART(">talk_init");
940 talk_init(); /* use voice of same language */
941 CHART("<talk_init");
944 /* load the icon set */
945 CHART(">icons_init");
946 icons_init();
947 CHART("<icons_init");
949 #ifdef HAVE_LCD_COLOR
950 if (global_settings.colors_file[0]
951 && global_settings.colors_file[0] != '-')
953 CHART(">read_color_theme_file");
954 read_color_theme_file();
955 CHART("<read_color_theme_file");
957 #endif
959 #ifdef HAVE_LCD_COLOR
960 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
961 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
962 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
963 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
964 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
965 #endif
967 #ifdef HAVE_LCD_BITMAP
968 lcd_scroll_step(global_settings.scroll_step);
969 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
970 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
971 #endif
972 lcd_bidir_scroll(global_settings.bidir_limit);
973 lcd_scroll_delay(global_settings.scroll_delay);
976 CHART(">set_codepage");
977 set_codepage(global_settings.default_codepage);
978 CHART("<set_codepage");
980 #if CONFIG_CODEC == SWCODEC
981 #ifdef HAVE_CROSSFADE
982 audio_set_crossfade(global_settings.crossfade);
983 #endif
984 dsp_set_replaygain();
985 dsp_set_crossfeed(global_settings.crossfeed);
986 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
987 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
988 global_settings.crossfeed_hf_attenuation,
989 global_settings.crossfeed_hf_cutoff);
991 /* Configure software equalizer, hardware eq is handled in audio_init() */
992 dsp_set_eq(global_settings.eq_enabled);
993 dsp_set_eq_precut(global_settings.eq_precut);
994 for(i = 0; i < 5; i++) {
995 dsp_set_eq_coefs(i);
998 dsp_dither_enable(global_settings.dithering_enabled);
999 #ifdef HAVE_PITCHSCREEN
1000 dsp_timestretch_enable(global_settings.timestretch_enabled);
1001 #endif
1002 dsp_set_compressor();
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 for(int i=0; i<nb_settings; i++)
1081 reset_setting(&settings[i], settings[i].setting);
1082 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1083 enc_global_settings_reset();
1084 #endif
1085 #ifdef HAVE_LCD_BITMAP
1086 FOR_NB_SCREENS(i)
1088 if (screens[i].getuifont() > FONT_SYSFIXED)
1090 font_unload(screens[i].getuifont());
1091 screens[i].setuifont(FONT_SYSFIXED);
1092 screens[i].setfont(FONT_SYSFIXED);
1095 #endif
1098 /** Changing setting values **/
1099 const struct settings_list* find_setting(const void* variable, int *id)
1101 int i;
1102 for(i=0;i<nb_settings;i++)
1104 if (settings[i].setting == variable)
1106 if (id)
1107 *id = i;
1108 return &settings[i];
1111 return NULL;
1113 const struct settings_list* find_setting_by_cfgname(const char* name, int *id)
1115 int i;
1116 for (i=0; i<nb_settings; i++)
1118 if (settings[i].cfg_name &&
1119 !strcmp(settings[i].cfg_name, name))
1121 if (id) *id = i;
1122 return &settings[i];
1125 return NULL;
1128 bool set_bool(const char* string, const bool* variable )
1130 return set_bool_options(string, variable,
1131 (char *)STR(LANG_SET_BOOL_YES),
1132 (char *)STR(LANG_SET_BOOL_NO),
1133 NULL);
1137 bool set_bool_options(const char* string, const bool* variable,
1138 const char* yes_str, int yes_voice,
1139 const char* no_str, int no_voice,
1140 void (*function)(bool))
1142 struct opt_items names[] = {
1143 {(unsigned const char *)no_str, no_voice},
1144 {(unsigned const char *)yes_str, yes_voice}
1146 bool result;
1148 result = set_option(string, variable, BOOL, names, 2,
1149 (void (*)(int))function);
1150 return result;
1153 bool set_int(const unsigned char* string,
1154 const char* unit,
1155 int voice_unit,
1156 const int* variable,
1157 void (*function)(int),
1158 int step,
1159 int min,
1160 int max,
1161 const char* (*formatter)(char*, size_t, int, const char*) )
1163 return set_int_ex(string, unit, voice_unit, variable, function,
1164 step, min, max, formatter, NULL);
1167 bool set_int_ex(const unsigned char* string,
1168 const char* unit,
1169 int voice_unit,
1170 const int* variable,
1171 void (*function)(int),
1172 int step,
1173 int min,
1174 int max,
1175 const char* (*formatter)(char*, size_t, int, const char*),
1176 int32_t (*get_talk_id)(int, int))
1178 (void)unit;
1179 struct settings_list item;
1180 struct int_setting data = {
1181 function, voice_unit, min, max, step,
1182 formatter, get_talk_id
1184 item.int_setting = &data;
1185 item.flags = F_INT_SETTING|F_T_INT;
1186 item.lang_id = -1;
1187 item.cfg_vals = (char*)string;
1188 item.setting = (void *)variable;
1189 return option_screen(&item, NULL, false, NULL);
1193 static const struct opt_items *set_option_options;
1194 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1196 (void)buf, (void)unit, (void)size;
1197 return P2STR(set_option_options[item].string);
1200 static int32_t set_option_get_talk_id(int value, int unit)
1202 (void)unit;
1203 return set_option_options[value].voice_id;
1206 bool set_option(const char* string, const void* variable, enum optiontype type,
1207 const struct opt_items* options,
1208 int numoptions, void (*function)(int))
1210 int temp;
1211 struct settings_list item;
1212 struct int_setting data = {
1213 function, UNIT_INT, 0, numoptions-1, 1,
1214 set_option_formatter, set_option_get_talk_id
1216 set_option_options = options;
1217 item.int_setting = &data;
1218 item.flags = F_INT_SETTING|F_T_INT;
1219 item.lang_id = -1;
1220 item.cfg_vals = (char*)string;
1221 item.setting = &temp;
1222 if (type == BOOL)
1223 temp = *(bool*)variable? 1: 0;
1224 else
1225 temp = *(int*)variable;
1226 if (!option_screen(&item, NULL, false, NULL))
1228 if (type == BOOL)
1230 *(bool*)variable = (temp == 1);
1231 else
1232 *(int*)variable = temp;
1233 return false;
1235 return true;
1239 * Takes filename, removes the directory and the extension,
1240 * and then copies the basename into setting, unless the basename exceeds maxlen
1242 void set_file(const char* filename, char* setting, const int maxlen)
1244 const char* fptr = strrchr(filename,'/');
1245 const char* extptr;
1246 int len;
1247 int extlen = 0;
1249 if (!fptr)
1250 return;
1252 fptr++;
1254 extptr = strrchr(fptr, '.');
1256 if (!extptr || extptr < fptr)
1257 extlen = 0;
1258 else
1259 extlen = strlen(extptr);
1261 len = strlen(fptr) - extlen + 1;
1263 /* error if filename isn't in ROCKBOX_DIR */
1264 if (len > maxlen)
1265 return;
1267 strlcpy(setting, fptr, len);
1268 settings_save();