Meizu: update backlight brightness curve so backlight is still on at the minimum...
[kugel-rb.git] / apps / settings.c
blob800cc59a5682a76bd714e769b1757360d5c3623c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Stuart Martin
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include "inttypes.h"
27 #include "config.h"
28 #include "action.h"
29 #include "crc32.h"
30 #include "settings.h"
31 #include "debug.h"
32 #include "usb.h"
33 #include "backlight.h"
34 #include "audio.h"
35 #include "mpeg.h"
36 #include "talk.h"
37 #include "string.h"
38 #include "rtc.h"
39 #include "power.h"
40 #include "ata_idle_notify.h"
41 #include "storage.h"
42 #include "screens.h"
43 #include "ctype.h"
44 #include "file.h"
45 #include "system.h"
46 #include "general.h"
47 #include "misc.h"
48 #ifdef HAVE_LCD_BITMAP
49 #include "icons.h"
50 #include "font.h"
51 #include "peakmeter.h"
52 #endif
53 #include "lang.h"
54 #include "language.h"
55 #include "powermgmt.h"
56 #include "sprintf.h"
57 #include "keyboard.h"
58 #include "version.h"
59 #include "sound.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 #include "backdrop.h"
68 #include "appevents.h"
69 #if CONFIG_TUNER
70 #include "radio.h"
71 #endif
72 #include "wps_engine/wps_engine.h"
74 #if CONFIG_CODEC == MAS3507D
75 void dac_line_in(bool enable);
76 #endif
77 struct user_settings global_settings;
78 struct system_status global_status;
80 #if CONFIG_CODEC == SWCODEC
81 #include "dsp.h"
82 #include "playback.h"
83 #ifdef HAVE_RECORDING
84 #include "enc_config.h"
85 #endif
86 #endif /* CONFIG_CODEC == SWCODEC */
88 #define NVRAM_BLOCK_SIZE 44
90 #ifdef HAVE_LCD_BITMAP
91 #define MAX_LINES 10
92 #else
93 #define MAX_LINES 2
94 #endif
96 #ifdef HAVE_REMOTE_LCD
97 #include "lcd-remote.h"
98 #endif
100 long lasttime = 0;
102 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
103 /* NVRAM is set out as
104 [0] 'R'
105 [1] 'b'
106 [2] version
107 [3] stored variable count
108 [4-7] crc32 checksum
109 [8-NVRAM_BLOCK_SIZE] data
111 #define NVRAM_DATA_START 8
112 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
113 static char nvram_buffer[NVRAM_BLOCK_SIZE];
115 static bool read_nvram_data(char* buf, int max_len)
117 unsigned crc32 = 0xffffffff;
118 int var_count = 0, i = 0, buf_pos = 0;
119 #ifndef HAVE_RTC_RAM
120 int fd = open(NVRAM_FILE,O_RDONLY);
121 if (fd < 0)
122 return false;
123 memset(buf,0,max_len);
124 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
125 return false;
126 close(fd);
127 #else
128 memset(buf,0,max_len);
129 /* read rtc block */
130 for (i=0; i < max_len; i++ )
131 buf[i] = rtc_read(0x14+i);
132 #endif
133 /* check magic, version */
134 if ((buf[0] != 'R') || (buf[1] != 'b')
135 || (buf[2] != NVRAM_CONFIG_VERSION))
136 return false;
137 /* check crc32 */
138 crc32 = crc_32(&buf[NVRAM_DATA_START],
139 max_len-NVRAM_DATA_START-1,0xffffffff);
140 if (memcmp(&crc32,&buf[4],4))
141 return false;
142 /* all good, so read in the settings */
143 var_count = buf[3];
144 buf_pos = NVRAM_DATA_START;
145 for(i=0; i<nb_settings; i++)
147 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
148 >>F_NVRAM_MASK_SHIFT;
149 if (nvram_bytes)
151 if ((var_count>0) && (buf_pos<max_len))
153 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
154 buf_pos += nvram_bytes;
155 var_count--;
157 else /* should only happen when new items are added to the end */
159 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
163 return true;
165 static bool write_nvram_data(char* buf, int max_len)
167 unsigned crc32 = 0xffffffff;
168 int i = 0, buf_pos = 0;
169 char var_count = 0;
170 #ifndef HAVE_RTC_RAM
171 int fd;
172 #endif
173 memset(buf,0,max_len);
174 /* magic, version */
175 buf[0] = 'R'; buf[1] = 'b';
176 buf[2] = NVRAM_CONFIG_VERSION;
177 buf_pos = NVRAM_DATA_START;
178 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
180 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
181 >>F_NVRAM_MASK_SHIFT;
182 if (nvram_bytes)
184 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
185 buf_pos += nvram_bytes;
186 var_count++;
189 /* count and crc32 */
190 buf[3] = var_count;
191 crc32 = crc_32(&buf[NVRAM_DATA_START],
192 max_len-NVRAM_DATA_START-1,0xffffffff);
193 memcpy(&buf[4],&crc32,4);
194 #ifndef HAVE_RTC_RAM
195 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
196 if (fd >= 0)
198 int len = write(fd,buf,max_len);
199 close(fd);
200 if (len < 8)
201 return false;
203 #else
204 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
205 that it would write a number of bytes at a time since the RTC chip
206 supports that, but this will have to do for now 8-) */
207 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
208 int r = rtc_write(0x14+i, buf[i]);
209 if (r)
210 return false;
212 #endif
213 return true;
216 /** Reading from a config file **/
218 * load settings from disk or RTC RAM
220 void settings_load(int which)
222 if (which&SETTINGS_RTC)
223 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
224 if (which&SETTINGS_HD)
226 settings_load_config(CONFIGFILE,false);
227 settings_load_config(FIXEDSETTINGSFILE,false);
231 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
233 const char* start = settings[setting_id].cfg_vals;
234 char* end = NULL;
235 char temp[MAX_PATH];
236 int count = 0;
237 while (1)
239 end = strchr(start, ',');
240 if (!end)
242 if (!strcmp(str, start))
244 *out = count;
245 return true;
247 else return false;
249 strlcpy(temp, start, end-start+1);
250 if (!strcmp(str, temp))
252 *out = count;
253 return true;
255 start = end +1;
256 count++;
258 return false;
261 bool settings_load_config(const char* file, bool apply)
263 int fd;
264 char line[128];
265 char* name;
266 char* value;
267 int i;
268 fd = open_utf8(file, O_RDONLY);
269 if (fd < 0)
270 return false;
272 while (read_line(fd, line, sizeof line) > 0)
274 if (!settings_parseline(line, &name, &value))
275 continue;
276 for(i=0; i<nb_settings; i++)
278 if (settings[i].cfg_name == NULL)
279 continue;
280 if (!strcasecmp(name,settings[i].cfg_name))
282 switch (settings[i].flags&F_T_MASK)
284 case F_T_CUSTOM:
285 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
286 break;
287 case F_T_INT:
288 case F_T_UINT:
289 #ifdef HAVE_LCD_COLOR
290 if (settings[i].flags&F_RGB)
291 hex_to_rgb(value, (int*)settings[i].setting);
292 else
293 #endif
294 if (settings[i].cfg_vals == NULL)
296 *(int*)settings[i].setting = atoi(value);
298 else
300 int temp, *v = (int*)settings[i].setting;
301 bool found = cfg_string_to_int(i, &temp, value);
302 if (found)
304 if (settings[i].flags&F_TABLE_SETTING)
305 *v = settings[i].table_setting->values[temp];
306 else
307 *v = temp;
309 else
310 *v = atoi(value);
312 break;
313 case F_T_BOOL:
315 int temp;
316 if (cfg_string_to_int(i,&temp,value))
317 *(bool*)settings[i].setting = (temp!=0);
318 if (settings[i].bool_setting->option_callback)
319 settings[i].bool_setting->option_callback(temp!=0);
320 break;
322 case F_T_CHARPTR:
323 case F_T_UCHARPTR:
325 char storage[MAX_PATH];
326 if (settings[i].filename_setting->prefix)
328 int len = strlen(settings[i].filename_setting->prefix);
329 if (!strncasecmp(value,
330 settings[i].filename_setting->prefix,
331 len))
333 strlcpy(storage, &value[len], MAX_PATH);
335 else strlcpy(storage, value, MAX_PATH);
337 else strlcpy(storage, value, MAX_PATH);
338 if (settings[i].filename_setting->suffix)
340 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
341 if (s) *s = '\0';
343 strlcpy((char*)settings[i].setting, storage,
344 settings[i].filename_setting->max_len);
345 break;
348 break;
349 } /* if (!strcmp(name,settings[i].cfg_name)) */
350 } /* for(...) */
351 } /* while(...) */
353 close(fd);
354 settings_save();
355 if (apply)
356 settings_apply(true);
357 return true;
360 /** Writing to a config file and saving settings **/
362 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
364 int flags = settings[setting_id].flags;
365 const char* start = settings[setting_id].cfg_vals;
366 char* end = NULL;
367 int count = 0;
369 if ((flags&F_T_MASK)==F_T_INT &&
370 flags&F_TABLE_SETTING)
372 const int *value = settings[setting_id].table_setting->values;
373 while (start)
375 end = strchr(start,',');
376 if (value[count] == val)
378 if (end == NULL)
379 strlcpy(buf, start, buf_len);
380 else
382 int len = (buf_len > (end-start))? end-start: buf_len;
383 strlcpy(buf, start, len+1);
385 return true;
387 count++;
389 if (end)
390 start = end+1;
391 else
392 break;
394 return false;
397 while (count < val)
399 start = strchr(start,',');
400 if (!start)
401 return false;
402 count++;
403 start++;
405 end = strchr(start,',');
406 if (end == NULL)
407 strlcpy(buf, start, buf_len);
408 else
410 int len = (buf_len > (end-start))? end-start: buf_len;
411 strlcpy(buf, start, len+1);
413 return true;
416 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
418 switch (settings[i].flags&F_T_MASK)
420 case F_T_CUSTOM:
421 settings[i].custom_setting->write_to_cfg(settings[i].setting,
422 buf, buf_len);
423 break;
424 case F_T_INT:
425 case F_T_UINT:
426 #ifdef HAVE_LCD_COLOR
427 if (settings[i].flags&F_RGB)
429 int colour = *(int*)settings[i].setting;
430 snprintf(buf,buf_len,"%02x%02x%02x",
431 (int)RGB_UNPACK_RED(colour),
432 (int)RGB_UNPACK_GREEN(colour),
433 (int)RGB_UNPACK_BLUE(colour));
435 else
436 #endif
437 if (settings[i].cfg_vals == NULL)
439 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
441 else
443 if (cfg_int_to_string(i, *(int*)settings[i].setting,
444 buf, buf_len) == false)
446 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
448 else
449 return false;
451 break;
452 case F_T_BOOL:
453 cfg_int_to_string(i,
454 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
455 break;
456 case F_T_CHARPTR:
457 case F_T_UCHARPTR:
458 if (((char*)settings[i].setting)[0]
459 && settings[i].filename_setting->prefix)
461 snprintf(buf,buf_len,"%s%s%s",
462 settings[i].filename_setting->prefix,
463 (char*)settings[i].setting,
464 settings[i].filename_setting->suffix);
466 else strlcpy(buf,(char*)settings[i].setting,
467 settings[i].filename_setting->max_len);
468 break;
469 } /* switch () */
470 return true;
474 static bool is_changed(int setting_id)
476 const struct settings_list *setting = &settings[setting_id];
477 switch (setting->flags&F_T_MASK)
479 case F_T_CUSTOM:
480 return setting->custom_setting->is_changed(setting->setting,
481 setting->default_val.custom);
482 break;
483 case F_T_INT:
484 case F_T_UINT:
485 if (setting->flags&F_DEF_ISFUNC)
487 if (*(int*)setting->setting == setting->default_val.func())
488 return false;
490 else if (setting->flags&F_T_SOUND)
492 if (*(int*)setting->setting ==
493 sound_default(setting->sound_setting->setting))
494 return false;
496 else if (*(int*)setting->setting == setting->default_val.int_)
497 return false;
498 break;
499 case F_T_BOOL:
500 if (*(bool*)setting->setting == setting->default_val.bool_)
501 return false;
502 break;
503 case F_T_CHARPTR:
504 case F_T_UCHARPTR:
505 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
506 return false;
507 break;
509 return true;
512 static bool settings_write_config(const char* filename, int options)
514 int i;
515 int fd;
516 char value[MAX_PATH];
517 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
518 if (fd < 0)
519 return false;
520 fdprintf(fd, "# .cfg file created by rockbox %s - "
521 "http://www.rockbox.org\r\n\r\n", appsversion);
522 for(i=0; i<nb_settings; i++)
524 if (settings[i].cfg_name == NULL)
525 continue;
526 value[0] = '\0';
528 switch (options)
530 case SETTINGS_SAVE_CHANGED:
531 if (!is_changed(i))
532 continue;
533 break;
534 case SETTINGS_SAVE_SOUND:
535 if ((settings[i].flags&F_SOUNDSETTING) == 0)
536 continue;
537 break;
538 case SETTINGS_SAVE_THEME:
539 if ((settings[i].flags&F_THEMESETTING) == 0)
540 continue;
541 break;
542 #ifdef HAVE_RECORDING
543 case SETTINGS_SAVE_RECPRESETS:
544 if ((settings[i].flags&F_RECSETTING) == 0)
545 continue;
546 break;
547 #endif
548 #if CONFIG_CODEC == SWCODEC
549 case SETTINGS_SAVE_EQPRESET:
550 if ((settings[i].flags&F_EQSETTING) == 0)
551 continue;
552 break;
553 #endif
556 cfg_to_string(i, value, MAX_PATH);
557 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
558 } /* for(...) */
559 close(fd);
560 return true;
562 #ifndef HAVE_RTC_RAM
563 static bool flush_global_status_callback(void)
565 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
567 #endif
568 static bool flush_config_block_callback(void)
570 bool r1, r2;
571 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
572 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
573 return r1 || r2;
577 * persist all runtime user settings to disk or RTC RAM
579 static void update_runtime(void)
581 int elapsed_secs;
583 elapsed_secs = (current_tick - lasttime) / HZ;
584 global_status.runtime += elapsed_secs;
585 lasttime += (elapsed_secs * HZ);
587 if ( global_status.runtime > global_status.topruntime )
588 global_status.topruntime = global_status.runtime;
591 void status_save(void)
593 update_runtime();
594 #ifdef HAVE_RTC_RAM
595 /* this will be done in the storage_callback if
596 target doesnt have rtc ram */
597 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
598 #else
599 register_storage_idle_func(flush_global_status_callback);
600 #endif
603 int settings_save(void)
605 update_runtime();
606 #ifdef HAVE_RTC_RAM
607 /* this will be done in the storage_callback if
608 target doesnt have rtc ram */
609 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
610 #endif
611 register_storage_idle_func(flush_config_block_callback);
612 return 0;
615 bool settings_save_config(int options)
617 char filename[MAX_PATH];
618 char *folder, *namebase;
619 switch (options)
621 case SETTINGS_SAVE_THEME:
622 folder = THEME_DIR;
623 namebase = "theme";
624 break;
625 #ifdef HAVE_RECORDING
626 case SETTINGS_SAVE_RECPRESETS:
627 folder = RECPRESETS_DIR;
628 namebase = "recording";
629 break;
630 #endif
631 #if CONFIG_CODEC == SWCODEC
632 case SETTINGS_SAVE_EQPRESET:
633 folder = EQS_DIR;
634 namebase = "eq";
635 break;
636 #endif
637 case SETTINGS_SAVE_SOUND:
638 folder = ROCKBOX_DIR;
639 namebase = "sound";
640 break;
641 default:
642 folder = ROCKBOX_DIR;
643 namebase = "config";
644 break;
646 create_numbered_filename(filename, folder, namebase, ".cfg", 2
647 IF_CNFN_NUM_(, NULL));
649 /* allow user to modify filename */
650 while (true) {
651 if (!kbd_input(filename, sizeof filename)) {
652 break;
654 else {
655 splash(HZ, ID2P(LANG_CANCEL));
656 return false;
660 if (settings_write_config(filename, options))
661 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
662 else
663 splash(HZ, ID2P(LANG_FAILED));
664 return true;
667 /** Apply and Reset settings **/
670 #ifdef HAVE_LCD_BITMAP
672 * Applies the range infos stored in global_settings to
673 * the peak meter.
675 void settings_apply_pm_range(void)
677 int pm_min, pm_max;
679 /* depending on the scale mode (dBfs or percent) the values
680 of global_settings.peak_meter_dbfs have different meanings */
681 if (global_settings.peak_meter_dbfs)
683 /* convert to dBfs * 100 */
684 pm_min = -(((int)global_settings.peak_meter_min) * 100);
685 pm_max = -(((int)global_settings.peak_meter_max) * 100);
687 else
689 /* percent is stored directly -> no conversion */
690 pm_min = global_settings.peak_meter_min;
691 pm_max = global_settings.peak_meter_max;
694 /* apply the range */
695 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
697 #endif /* HAVE_LCD_BITMAP */
699 void sound_settings_apply(void)
701 #if CONFIG_CODEC == SWCODEC
702 sound_set_dsp_callback(dsp_callback);
703 #endif
704 sound_set(SOUND_BASS, global_settings.bass);
705 sound_set(SOUND_TREBLE, global_settings.treble);
706 sound_set(SOUND_BALANCE, global_settings.balance);
707 sound_set(SOUND_VOLUME, global_settings.volume);
708 sound_set(SOUND_CHANNELS, global_settings.channel_config);
709 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
710 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
711 sound_set(SOUND_LOUDNESS, global_settings.loudness);
712 sound_set(SOUND_AVC, global_settings.avc);
713 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
714 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
715 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
716 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
717 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
718 sound_set(SOUND_SUPERBASS, global_settings.superbass);
719 #endif
721 #ifdef HAVE_WM8758
722 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
723 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
724 #endif
727 void settings_apply(bool read_disk)
729 char buf[64];
730 #if CONFIG_CODEC == SWCODEC
731 int i;
732 #endif
734 sound_settings_apply();
736 #ifdef HAVE_DISK_STORAGE
737 audio_set_buffer_margin(global_settings.buffer_margin);
738 #endif
740 #ifdef HAVE_LCD_CONTRAST
741 lcd_set_contrast(global_settings.contrast);
742 #endif
743 lcd_scroll_speed(global_settings.scroll_speed);
744 #ifdef HAVE_REMOTE_LCD
745 lcd_remote_set_contrast(global_settings.remote_contrast);
746 lcd_remote_set_invert_display(global_settings.remote_invert);
748 #ifdef HAVE_LCD_FLIP
749 lcd_remote_set_flip(global_settings.remote_flip_display);
750 #endif
752 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
753 lcd_remote_scroll_step(global_settings.remote_scroll_step);
754 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
755 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
756 #ifdef HAVE_REMOTE_LCD_TICKING
757 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
758 #endif
759 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
760 #if CONFIG_CHARGING
761 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
762 #endif
763 #ifdef HAS_REMOTE_BUTTON_HOLD
764 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
765 #endif
766 #endif /* HAVE_REMOTE_LCD */
767 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
768 backlight_set_brightness(global_settings.brightness);
769 #endif
770 #ifdef HAVE_BACKLIGHT
771 backlight_set_timeout(global_settings.backlight_timeout);
772 #if CONFIG_CHARGING
773 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
774 #endif
775 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
776 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
777 backlight_set_fade_in(global_settings.backlight_fade_in);
778 backlight_set_fade_out(global_settings.backlight_fade_out);
779 #endif
780 #endif
781 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
782 buttonlight_set_brightness(global_settings.buttonlight_brightness);
783 #endif
784 #ifdef HAVE_BUTTON_LIGHT
785 buttonlight_set_timeout(global_settings.buttonlight_timeout);
786 #endif
787 #ifdef HAVE_DISK_STORAGE
788 storage_spindown(global_settings.disk_spindown);
789 #endif
790 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
791 dac_line_in(global_settings.line_in);
792 #endif
793 set_poweroff_timeout(global_settings.poweroff);
795 set_battery_capacity(global_settings.battery_capacity);
796 #if BATTERY_TYPES_COUNT > 1
797 set_battery_type(global_settings.battery_type);
798 #endif
800 #ifdef HAVE_LCD_BITMAP
801 #ifdef HAVE_LCD_INVERT
802 lcd_set_invert_display(global_settings.invert);
803 #endif
804 #ifdef HAVE_LCD_FLIP
805 lcd_set_flip(global_settings.flip_display);
806 button_set_flip(global_settings.flip_display);
807 #endif
808 lcd_update(); /* refresh after flipping the screen */
809 settings_apply_pm_range();
810 peak_meter_init_times(
811 global_settings.peak_meter_release, global_settings.peak_meter_hold,
812 global_settings.peak_meter_clip_hold);
813 #endif
815 #ifdef HAVE_SPEAKER
816 audiohw_enable_speaker(global_settings.speaker_enabled);
817 #endif
819 if (read_disk)
822 #ifdef HAVE_LCD_BITMAP
823 /* fonts need to be loaded before the WPS */
824 if ( global_settings.font_file[0]) {
825 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
826 global_settings.font_file);
827 if (font_load(buf) == NULL)
828 font_reset();
830 else
831 font_reset();
833 if ( global_settings.kbd_file[0]) {
834 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
835 global_settings.kbd_file);
836 load_kbd(buf);
838 else
839 load_kbd(NULL);
840 #endif
841 #if LCD_DEPTH > 1
842 unload_wps_backdrop();
843 #endif
844 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
845 unload_remote_wps_backdrop();
846 #endif
847 if ( global_settings.wps_file[0] &&
848 global_settings.wps_file[0] != 0xff ) {
849 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
850 global_settings.wps_file);
851 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
853 else
855 wps_data_init(gui_wps[0].data);
856 #ifdef HAVE_REMOTE_LCD
857 gui_wps[0].data->remote_wps = false;
858 #endif
861 #if LCD_DEPTH > 1
862 if ( global_settings.backdrop_file[0] &&
863 global_settings.backdrop_file[0] != 0xff ) {
864 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
865 global_settings.backdrop_file);
866 load_main_backdrop(buf);
867 } else {
868 unload_main_backdrop();
870 show_main_backdrop();
871 #endif
872 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
873 show_remote_main_backdrop();
874 #endif
876 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
877 if ( global_settings.rwps_file[0]) {
878 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
879 global_settings.rwps_file);
880 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
882 else
884 wps_data_init(gui_wps[1].data);
885 gui_wps[1].data->remote_wps = true;
887 #endif
888 if ( global_settings.lang_file[0]) {
889 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
890 global_settings.lang_file);
891 lang_load(buf);
892 talk_init(); /* use voice of same language */
894 /* load the icon set */
895 icons_init();
897 #ifdef HAVE_LCD_COLOR
898 if (global_settings.colors_file[0])
899 read_color_theme_file();
900 #endif
903 #ifdef HAVE_LCD_COLOR
904 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
905 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
906 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
907 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
908 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
909 #endif
911 #ifdef HAVE_LCD_BITMAP
912 lcd_scroll_step(global_settings.scroll_step);
913 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
914 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
915 #else
916 lcd_jump_scroll(global_settings.jump_scroll);
917 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
918 #endif
919 lcd_bidir_scroll(global_settings.bidir_limit);
920 lcd_scroll_delay(global_settings.scroll_delay);
923 set_codepage(global_settings.default_codepage);
925 #if CONFIG_CODEC == SWCODEC
926 audio_set_crossfade(global_settings.crossfade);
927 dsp_set_replaygain();
928 dsp_set_crossfeed(global_settings.crossfeed);
929 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
930 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
931 global_settings.crossfeed_hf_attenuation,
932 global_settings.crossfeed_hf_cutoff);
934 /* Configure software equalizer, hardware eq is handled in audio_init() */
935 dsp_set_eq(global_settings.eq_enabled);
936 dsp_set_eq_precut(global_settings.eq_precut);
937 for(i = 0; i < 5; i++) {
938 dsp_set_eq_coefs(i);
941 dsp_dither_enable(global_settings.dithering_enabled);
942 dsp_timestretch_enable(global_settings.timestretch_enabled);
943 #endif
945 #ifdef HAVE_SPDIF_POWER
946 spdif_power_enable(global_settings.spdif_enable);
947 #endif
949 #ifdef HAVE_BACKLIGHT
950 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
951 #ifdef HAVE_REMOTE_LCD
952 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
953 #endif
954 #ifdef HAS_BUTTON_HOLD
955 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
956 #endif
957 #ifdef HAVE_LCD_SLEEP_SETTING
958 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
959 #endif
960 #endif /* HAVE_BACKLIGHT */
962 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
963 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
964 #endif
966 #ifdef HAVE_USB_CHARGING_ENABLE
967 usb_charging_enable(global_settings.usb_charging);
968 #endif
970 #ifdef HAVE_TOUCHSCREEN
971 touchscreen_set_mode(global_settings.touch_mode);
972 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
973 #endif
975 /* This should stay last */
976 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
977 enc_global_settings_apply();
978 #endif
979 send_event(GUI_EVENT_STATUSBAR_TOGGLE, NULL);
980 list_init_viewports(NULL);
981 send_event(GUI_EVENT_ACTIONUPDATE, (void*)true);
986 * reset all settings to their default value
988 void reset_setting(const struct settings_list *setting, void *var)
990 switch (setting->flags&F_T_MASK)
992 case F_T_CUSTOM:
993 setting->custom_setting->set_default(setting->setting,
994 setting->default_val.custom);
995 break;
996 case F_T_INT:
997 case F_T_UINT:
998 if (setting->flags&F_DEF_ISFUNC)
999 *(int*)var = setting->default_val.func();
1000 else if (setting->flags&F_T_SOUND)
1001 *(int*)var = sound_default(setting->sound_setting->setting);
1002 else *(int*)var = setting->default_val.int_;
1003 break;
1004 case F_T_BOOL:
1005 *(bool*)var = setting->default_val.bool_;
1006 break;
1007 case F_T_CHARPTR:
1008 case F_T_UCHARPTR:
1009 strlcpy((char*)var, setting->default_val.charptr,
1010 setting->filename_setting->max_len);
1011 break;
1015 void settings_reset(void)
1017 int i;
1019 for(i=0; i<nb_settings; i++)
1020 reset_setting(&settings[i], settings[i].setting);
1021 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1022 enc_global_settings_reset();
1023 #endif
1026 /** Changing setting values **/
1027 const struct settings_list* find_setting(const void* variable, int *id)
1029 int i;
1030 for(i=0;i<nb_settings;i++)
1032 if (settings[i].setting == variable)
1034 if (id)
1035 *id = i;
1036 return &settings[i];
1039 return NULL;
1042 bool set_bool(const char* string, const bool* variable )
1044 return set_bool_options(string, variable,
1045 (char *)STR(LANG_SET_BOOL_YES),
1046 (char *)STR(LANG_SET_BOOL_NO),
1047 NULL);
1051 bool set_bool_options(const char* string, const bool* variable,
1052 const char* yes_str, int yes_voice,
1053 const char* no_str, int no_voice,
1054 void (*function)(bool))
1056 struct opt_items names[] = {
1057 {(unsigned const char *)no_str, no_voice},
1058 {(unsigned const char *)yes_str, yes_voice}
1060 bool result;
1062 result = set_option(string, variable, BOOL, names, 2,
1063 (void (*)(int))function);
1064 return result;
1067 bool set_int(const unsigned char* string,
1068 const char* unit,
1069 int voice_unit,
1070 const int* variable,
1071 void (*function)(int),
1072 int step,
1073 int min,
1074 int max,
1075 void (*formatter)(char*, size_t, int, const char*) )
1077 return set_int_ex(string, unit, voice_unit, variable, function,
1078 step, min, max, formatter, NULL);
1081 bool set_int_ex(const unsigned char* string,
1082 const char* unit,
1083 int voice_unit,
1084 const int* variable,
1085 void (*function)(int),
1086 int step,
1087 int min,
1088 int max,
1089 void (*formatter)(char*, size_t, int, const char*),
1090 int32_t (*get_talk_id)(int, int))
1092 (void)unit;
1093 struct settings_list item;
1094 struct int_setting data = {
1095 function, voice_unit, min, max, step,
1096 formatter, get_talk_id
1098 item.int_setting = &data;
1099 item.flags = F_INT_SETTING|F_T_INT;
1100 item.lang_id = -1;
1101 item.cfg_vals = (char*)string;
1102 item.setting = (void *)variable;
1103 return option_screen(&item, NULL, false, NULL);
1107 static const struct opt_items *set_option_options;
1108 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1110 (void)unit;
1111 const unsigned char *text = set_option_options[item].string;
1112 strlcpy(buf, P2STR(text), size);
1114 static int32_t set_option_get_talk_id(int value, int unit)
1116 (void)unit;
1117 return set_option_options[value].voice_id;
1119 bool set_option(const char* string, const void* variable, enum optiontype type,
1120 const struct opt_items* options,
1121 int numoptions, void (*function)(int))
1123 int temp;
1124 struct settings_list item;
1125 struct int_setting data = {
1126 function, UNIT_INT, 0, numoptions-1, 1,
1127 set_option_formatter, set_option_get_talk_id
1129 set_option_options = options;
1130 item.int_setting = &data;
1131 item.flags = F_INT_SETTING|F_T_INT;
1132 item.lang_id = -1;
1133 item.cfg_vals = (char*)string;
1134 item.setting = &temp;
1135 if (type == BOOL)
1136 temp = *(bool*)variable? 1: 0;
1137 else
1138 temp = *(int*)variable;
1139 if (!option_screen(&item, NULL, false, NULL))
1141 if (type == BOOL)
1142 *(bool*)variable = (temp == 1);
1143 else
1144 *(int*)variable = temp;
1145 return false;
1147 return true;
1151 void set_file(const char* filename, char* setting, int maxlen)
1153 const char* fptr = strrchr(filename,'/');
1154 int len;
1155 int extlen = 0;
1156 const char* ptr;
1158 if (!fptr)
1159 return;
1161 fptr++;
1163 len = strlen(fptr);
1164 ptr = fptr + len;
1165 while ((*ptr != '.') && (ptr != fptr)) {
1166 extlen++;
1167 ptr--;
1169 if(ptr == fptr) extlen = 0;
1171 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1172 (len-extlen > maxlen))
1173 return;
1175 strlcpy(setting, fptr, len-extlen+1);
1177 settings_save();