Fixed m200v4 red build.
[kugel-rb.git] / apps / settings.c
blobb6c721ca32943297b873e61bb254ec7ec1c5a3ad
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 "misc.h"
47 #ifdef HAVE_LCD_BITMAP
48 #include "icons.h"
49 #include "font.h"
50 #include "peakmeter.h"
51 #endif
52 #include "lang.h"
53 #include "language.h"
54 #include "gwps.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 "statusbar.h"
63 #include "splash.h"
64 #include "list.h"
65 #include "settings_list.h"
66 #include "filetypes.h"
67 #include "option_select.h"
68 #include "backdrop.h"
70 #if CONFIG_TUNER
71 #include "radio.h"
72 #endif
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 strncpy(temp, start, end-start);
250 temp[end-start] = '\0';
251 if (!strcmp(str, temp))
253 *out = count;
254 return true;
256 start = end +1;
257 count++;
259 return false;
262 bool settings_load_config(const char* file, bool apply)
264 int fd;
265 char line[128];
266 char* name;
267 char* value;
268 int i;
269 fd = open_utf8(file, O_RDONLY);
270 if (fd < 0)
271 return false;
273 while (read_line(fd, line, sizeof line) > 0)
275 if (!settings_parseline(line, &name, &value))
276 continue;
277 for(i=0; i<nb_settings; i++)
279 if (settings[i].cfg_name == NULL)
280 continue;
281 if (!strcasecmp(name,settings[i].cfg_name))
283 switch (settings[i].flags&F_T_MASK)
285 case F_T_CUSTOM:
286 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
287 break;
288 case F_T_INT:
289 case F_T_UINT:
290 #ifdef HAVE_LCD_COLOR
291 if (settings[i].flags&F_RGB)
292 hex_to_rgb(value, (int*)settings[i].setting);
293 else
294 #endif
295 if (settings[i].cfg_vals == NULL)
297 *(int*)settings[i].setting = atoi(value);
299 else
301 int temp, *v = (int*)settings[i].setting;
302 bool found = cfg_string_to_int(i, &temp, value);
303 if (found)
305 if (settings[i].flags&F_TABLE_SETTING)
306 *v = settings[i].table_setting->values[temp];
307 else
308 *v = temp;
310 else
311 *v = atoi(value);
313 break;
314 case F_T_BOOL:
316 int temp;
317 if (cfg_string_to_int(i,&temp,value))
318 *(bool*)settings[i].setting = (temp==0?false:true);
319 break;
321 case F_T_CHARPTR:
322 case F_T_UCHARPTR:
324 char storage[MAX_PATH];
325 if (settings[i].filename_setting->prefix)
327 int len = strlen(settings[i].filename_setting->prefix);
328 if (!strncasecmp(value,
329 settings[i].filename_setting->prefix,
330 len))
332 strncpy(storage,&value[len],MAX_PATH);
334 else strncpy(storage,value,MAX_PATH);
336 else strncpy(storage,value,MAX_PATH);
337 if (settings[i].filename_setting->suffix)
339 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
340 if (s) *s = '\0';
342 strncpy((char*)settings[i].setting,storage,
343 settings[i].filename_setting->max_len);
344 ((char*)settings[i].setting)
345 [settings[i].filename_setting->max_len-1] = '\0';
346 break;
349 break;
350 } /* if (!strcmp(name,settings[i].cfg_name)) */
351 } /* for(...) */
352 } /* while(...) */
354 close(fd);
355 settings_save();
356 if (apply)
357 settings_apply(true);
358 return true;
361 /** Writing to a config file and saving settings **/
363 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
365 int flags = settings[setting_id].flags;
366 const char* start = settings[setting_id].cfg_vals;
367 char* end = NULL;
368 int count = 0;
370 if ((flags&F_T_MASK)==F_T_INT &&
371 flags&F_TABLE_SETTING)
373 const int *value = settings[setting_id].table_setting->values;
374 while (start)
376 end = strchr(start,',');
377 if (value[count] == val)
379 if (end == NULL)
380 strncpy(buf, start, buf_len);
381 else
383 int len = (buf_len > (end-start))? end-start: buf_len;
384 strncpy(buf, start, len);
385 buf[len] = '\0';
387 return true;
389 count++;
391 if (end)
392 start = end+1;
393 else
394 break;
396 return false;
399 while (count < val)
401 start = strchr(start,',');
402 if (!start)
403 return false;
404 count++;
405 start++;
407 end = strchr(start,',');
408 if (end == NULL)
409 strncpy(buf, start, buf_len);
410 else
412 int len = (buf_len > (end-start))? end-start: buf_len;
413 strncpy(buf, start, len);
414 buf[len] = '\0';
416 return true;
419 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
421 switch (settings[i].flags&F_T_MASK)
423 case F_T_CUSTOM:
424 settings[i].custom_setting->write_to_cfg(settings[i].setting,
425 buf, buf_len);
426 break;
427 case F_T_INT:
428 case F_T_UINT:
429 #ifdef HAVE_LCD_COLOR
430 if (settings[i].flags&F_RGB)
432 int colour = *(int*)settings[i].setting;
433 snprintf(buf,buf_len,"%02x%02x%02x",
434 (int)RGB_UNPACK_RED(colour),
435 (int)RGB_UNPACK_GREEN(colour),
436 (int)RGB_UNPACK_BLUE(colour));
438 else
439 #endif
440 if (settings[i].cfg_vals == NULL)
442 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
444 else
446 if (cfg_int_to_string(i, *(int*)settings[i].setting,
447 buf, buf_len) == false)
449 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
451 else
452 return false;
454 break;
455 case F_T_BOOL:
456 cfg_int_to_string(i,
457 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
458 break;
459 case F_T_CHARPTR:
460 case F_T_UCHARPTR:
461 if (((char*)settings[i].setting)[0]
462 && settings[i].filename_setting->prefix)
464 snprintf(buf,buf_len,"%s%s%s",
465 settings[i].filename_setting->prefix,
466 (char*)settings[i].setting,
467 settings[i].filename_setting->suffix);
469 else strncpy(buf,(char*)settings[i].setting,
470 settings[i].filename_setting->max_len);
471 break;
472 } /* switch () */
473 return true;
477 static bool is_changed(int setting_id)
479 const struct settings_list *setting = &settings[setting_id];
480 switch (setting->flags&F_T_MASK)
482 case F_T_CUSTOM:
483 return setting->custom_setting->is_changed(setting->setting,
484 setting->default_val.custom);
485 break;
486 case F_T_INT:
487 case F_T_UINT:
488 if (setting->flags&F_DEF_ISFUNC)
490 if (*(int*)setting->setting == setting->default_val.func())
491 return false;
493 else if (setting->flags&F_T_SOUND)
495 if (*(int*)setting->setting ==
496 sound_default(setting->sound_setting->setting))
497 return false;
499 else if (*(int*)setting->setting == setting->default_val.int_)
500 return false;
501 break;
502 case F_T_BOOL:
503 if (*(bool*)setting->setting == setting->default_val.bool_)
504 return false;
505 break;
506 case F_T_CHARPTR:
507 case F_T_UCHARPTR:
508 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
509 return false;
510 break;
512 return true;
515 static bool settings_write_config(const char* filename, int options)
517 int i;
518 int fd;
519 char value[MAX_PATH];
520 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
521 if (fd < 0)
522 return false;
523 #if CONFIG_TUNER
524 bool statusbar = global_settings.statusbar;
525 if (global_status.statusbar_forced != 0 && statusbar)
526 global_settings.statusbar = false;
527 #endif
528 fdprintf(fd, "# .cfg file created by rockbox %s - "
529 "http://www.rockbox.org\r\n\r\n", appsversion);
530 for(i=0; i<nb_settings; i++)
532 if (settings[i].cfg_name == NULL)
533 continue;
534 value[0] = '\0';
536 switch (options)
538 case SETTINGS_SAVE_CHANGED:
539 if (!is_changed(i))
540 continue;
541 break;
542 case SETTINGS_SAVE_SOUND:
543 if ((settings[i].flags&F_SOUNDSETTING) == 0)
544 continue;
545 break;
546 case SETTINGS_SAVE_THEME:
547 if ((settings[i].flags&F_THEMESETTING) == 0)
548 continue;
549 break;
550 #ifdef HAVE_RECORDING
551 case SETTINGS_SAVE_RECPRESETS:
552 if ((settings[i].flags&F_RECSETTING) == 0)
553 continue;
554 break;
555 #endif
556 #if CONFIG_CODEC == SWCODEC
557 case SETTINGS_SAVE_EQPRESET:
558 if ((settings[i].flags&F_EQSETTING) == 0)
559 continue;
560 break;
561 #endif
564 cfg_to_string(i, value, MAX_PATH);
565 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
566 } /* for(...) */
567 close(fd);
568 #if CONFIG_TUNER
569 global_settings.statusbar = statusbar;
570 #endif
571 return true;
573 #ifndef HAVE_RTC_RAM
574 static bool flush_global_status_callback(void)
576 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
578 #endif
579 static bool flush_config_block_callback(void)
581 bool r1, r2;
582 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
583 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
584 return r1 || r2;
588 * persist all runtime user settings to disk or RTC RAM
590 static void update_runtime(void)
592 int elapsed_secs;
594 elapsed_secs = (current_tick - lasttime) / HZ;
595 global_status.runtime += elapsed_secs;
596 lasttime += (elapsed_secs * HZ);
598 if ( global_status.runtime > global_status.topruntime )
599 global_status.topruntime = global_status.runtime;
602 void status_save(void)
604 update_runtime();
605 #ifdef HAVE_RTC_RAM
606 /* this will be done in the storage_callback if
607 target doesnt have rtc ram */
608 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
609 #else
610 register_storage_idle_func(flush_global_status_callback);
611 #endif
614 int settings_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 #endif
622 register_storage_idle_func(flush_config_block_callback);
623 return 0;
626 bool settings_save_config(int options)
628 char filename[MAX_PATH];
629 char *folder;
630 switch (options)
632 case SETTINGS_SAVE_THEME:
633 folder = THEME_DIR;
634 break;
635 #ifdef HAVE_RECORDING
636 case SETTINGS_SAVE_RECPRESETS:
637 folder = RECPRESETS_DIR;
638 break;
639 #endif
640 #if CONFIG_CODEC == SWCODEC
641 case SETTINGS_SAVE_EQPRESET:
642 folder = EQS_DIR;
643 break;
644 #endif
645 case SETTINGS_SAVE_SOUND:
646 default:
647 folder = ROCKBOX_DIR;
649 create_numbered_filename(filename, folder, "config", ".cfg", 2
650 IF_CNFN_NUM_(, NULL));
652 /* allow user to modify filename */
653 while (true) {
654 if (!kbd_input(filename, sizeof filename)) {
655 break;
657 else {
658 splash(HZ, ID2P(LANG_CANCEL));
659 return false;
663 if (settings_write_config(filename, options))
664 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
665 else
666 splash(HZ, ID2P(LANG_FAILED));
667 return true;
670 /** Apply and Reset settings **/
673 #ifdef HAVE_LCD_BITMAP
675 * Applies the range infos stored in global_settings to
676 * the peak meter.
678 void settings_apply_pm_range(void)
680 int pm_min, pm_max;
682 /* depending on the scale mode (dBfs or percent) the values
683 of global_settings.peak_meter_dbfs have different meanings */
684 if (global_settings.peak_meter_dbfs)
686 /* convert to dBfs * 100 */
687 pm_min = -(((int)global_settings.peak_meter_min) * 100);
688 pm_max = -(((int)global_settings.peak_meter_max) * 100);
690 else
692 /* percent is stored directly -> no conversion */
693 pm_min = global_settings.peak_meter_min;
694 pm_max = global_settings.peak_meter_max;
697 /* apply the range */
698 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
700 #endif /* HAVE_LCD_BITMAP */
702 void sound_settings_apply(void)
704 #if CONFIG_CODEC == SWCODEC
705 sound_set_dsp_callback(dsp_callback);
706 #endif
707 sound_set(SOUND_BASS, global_settings.bass);
708 sound_set(SOUND_TREBLE, global_settings.treble);
709 sound_set(SOUND_BALANCE, global_settings.balance);
710 sound_set(SOUND_VOLUME, global_settings.volume);
711 sound_set(SOUND_CHANNELS, global_settings.channel_config);
712 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
713 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
714 sound_set(SOUND_LOUDNESS, global_settings.loudness);
715 sound_set(SOUND_AVC, global_settings.avc);
716 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
717 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
718 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
719 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
720 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
721 sound_set(SOUND_SUPERBASS, global_settings.superbass);
722 #endif
724 #ifdef HAVE_WM8758
725 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
726 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
727 #endif
730 void settings_apply(bool read_disk)
732 char buf[64];
733 #if CONFIG_CODEC == SWCODEC
734 int i;
735 #endif
737 sound_settings_apply();
739 #ifdef HAVE_DISK_STORAGE
740 audio_set_buffer_margin(global_settings.buffer_margin);
741 #endif
743 #ifdef HAVE_LCD_CONTRAST
744 lcd_set_contrast(global_settings.contrast);
745 #endif
746 lcd_scroll_speed(global_settings.scroll_speed);
747 #ifdef HAVE_REMOTE_LCD
748 lcd_remote_set_contrast(global_settings.remote_contrast);
749 lcd_remote_set_invert_display(global_settings.remote_invert);
750 lcd_remote_set_flip(global_settings.remote_flip_display);
751 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
752 lcd_remote_scroll_step(global_settings.remote_scroll_step);
753 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
754 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
755 #ifdef HAVE_REMOTE_LCD_TICKING
756 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
757 #endif
758 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
759 #if CONFIG_CHARGING
760 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
761 #endif
762 #ifdef HAS_REMOTE_BUTTON_HOLD
763 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
764 #endif
765 #endif /* HAVE_REMOTE_LCD */
766 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
767 backlight_set_brightness(global_settings.brightness);
768 #endif
769 #ifdef HAVE_BACKLIGHT
770 backlight_set_timeout(global_settings.backlight_timeout);
771 #if CONFIG_CHARGING
772 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
773 #endif
774 #if (defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)) \
775 || defined(USE_BACKLIGHT_SW_FADING) \
776 || defined(USE_BACKLIGHT_CUSTOM_FADING_BOOL)
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 lcd_set_invert_display(global_settings.invert);
802 lcd_set_flip(global_settings.flip_display);
803 button_set_flip(global_settings.flip_display);
804 lcd_update(); /* refresh after flipping the screen */
805 settings_apply_pm_range();
806 peak_meter_init_times(
807 global_settings.peak_meter_release, global_settings.peak_meter_hold,
808 global_settings.peak_meter_clip_hold);
809 #endif
811 if (read_disk)
814 #ifdef HAVE_LCD_BITMAP
815 /* fonts need to be loaded before the WPS */
816 if ( global_settings.font_file[0]) {
817 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
818 global_settings.font_file);
819 if (font_load(buf) == NULL)
820 font_reset();
822 else
823 font_reset();
825 if ( global_settings.kbd_file[0]) {
826 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
827 global_settings.kbd_file);
828 load_kbd(buf);
830 else
831 load_kbd(NULL);
832 #endif
833 #if LCD_DEPTH > 1
834 unload_wps_backdrop();
835 #endif
836 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
837 unload_remote_wps_backdrop();
838 #endif
839 if ( global_settings.wps_file[0] &&
840 global_settings.wps_file[0] != 0xff ) {
841 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
842 global_settings.wps_file);
843 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
845 else
847 wps_data_init(gui_wps[0].data);
848 #ifdef HAVE_REMOTE_LCD
849 gui_wps[0].data->remote_wps = false;
850 #endif
853 #if LCD_DEPTH > 1
854 if ( global_settings.backdrop_file[0] &&
855 global_settings.backdrop_file[0] != 0xff ) {
856 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
857 global_settings.backdrop_file);
858 load_main_backdrop(buf);
859 } else {
860 unload_main_backdrop();
862 show_main_backdrop();
863 #endif
864 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
865 show_remote_main_backdrop();
866 #endif
868 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
869 if ( global_settings.rwps_file[0]) {
870 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
871 global_settings.rwps_file);
872 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
874 else
876 wps_data_init(gui_wps[1].data);
877 gui_wps[1].data->remote_wps = true;
879 #endif
880 if ( global_settings.lang_file[0]) {
881 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
882 global_settings.lang_file);
883 lang_load(buf);
884 talk_init(); /* use voice of same language */
886 /* load the icon set */
887 icons_init();
889 #ifdef HAVE_LCD_COLOR
890 if (global_settings.colors_file[0])
891 read_color_theme_file();
892 #endif
895 #ifdef HAVE_LCD_COLOR
896 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
897 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
898 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
899 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
900 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
901 #endif
903 #ifdef HAVE_LCD_BITMAP
904 lcd_scroll_step(global_settings.scroll_step);
905 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
906 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
907 #else
908 lcd_jump_scroll(global_settings.jump_scroll);
909 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
910 #endif
911 lcd_bidir_scroll(global_settings.bidir_limit);
912 lcd_scroll_delay(global_settings.scroll_delay);
915 set_codepage(global_settings.default_codepage);
917 #if CONFIG_CODEC == SWCODEC
918 audio_set_crossfade(global_settings.crossfade);
919 dsp_set_replaygain();
920 dsp_set_crossfeed(global_settings.crossfeed);
921 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
922 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
923 global_settings.crossfeed_hf_attenuation,
924 global_settings.crossfeed_hf_cutoff);
926 /* Configure software equalizer, hardware eq is handled in audio_init() */
927 dsp_set_eq(global_settings.eq_enabled);
928 dsp_set_eq_precut(global_settings.eq_precut);
929 for(i = 0; i < 5; i++) {
930 dsp_set_eq_coefs(i);
933 dsp_dither_enable(global_settings.dithering_enabled);
934 #endif
936 #ifdef HAVE_SPDIF_POWER
937 spdif_power_enable(global_settings.spdif_enable);
938 #endif
940 #ifdef HAVE_BACKLIGHT
941 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
942 #ifdef HAVE_REMOTE_LCD
943 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
944 #endif
945 #ifdef HAS_BUTTON_HOLD
946 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
947 #endif
948 #ifdef HAVE_LCD_SLEEP_SETTING
949 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
950 #endif
951 #endif /* HAVE_BACKLIGHT */
953 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
954 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
955 #endif
957 #ifdef HAVE_USB_CHARGING_ENABLE
958 usb_charging_enable(global_settings.usb_charging);
959 #endif
961 /* This should stay last */
962 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
963 enc_global_settings_apply();
964 #endif
965 list_init_viewports(NULL);
970 * reset all settings to their default value
972 void reset_setting(const struct settings_list *setting, void *var)
974 switch (setting->flags&F_T_MASK)
976 case F_T_CUSTOM:
977 setting->custom_setting->set_default(setting->setting,
978 setting->default_val.custom);
979 break;
980 case F_T_INT:
981 case F_T_UINT:
982 if (setting->flags&F_DEF_ISFUNC)
983 *(int*)var = setting->default_val.func();
984 else if (setting->flags&F_T_SOUND)
985 *(int*)var = sound_default(setting->sound_setting->setting);
986 else *(int*)var = setting->default_val.int_;
987 break;
988 case F_T_BOOL:
989 *(bool*)var = setting->default_val.bool_;
990 break;
991 case F_T_CHARPTR:
992 case F_T_UCHARPTR:
993 strncpy((char*)var, setting->default_val.charptr,
994 setting->filename_setting->max_len);
995 break;
999 void settings_reset(void)
1001 int i;
1003 for(i=0; i<nb_settings; i++)
1004 reset_setting(&settings[i], settings[i].setting);
1005 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1006 enc_global_settings_reset();
1007 #endif
1010 /** Changing setting values **/
1011 const struct settings_list* find_setting(const void* variable, int *id)
1013 int i;
1014 for(i=0;i<nb_settings;i++)
1016 if (settings[i].setting == variable)
1018 if (id)
1019 *id = i;
1020 return &settings[i];
1023 return NULL;
1026 bool set_bool(const char* string, const bool* variable )
1028 return set_bool_options(string, variable,
1029 (char *)STR(LANG_SET_BOOL_YES),
1030 (char *)STR(LANG_SET_BOOL_NO),
1031 NULL);
1035 bool set_bool_options(const char* string, const bool* variable,
1036 const char* yes_str, int yes_voice,
1037 const char* no_str, int no_voice,
1038 void (*function)(bool))
1040 struct opt_items names[] = {
1041 {(unsigned const char *)no_str, no_voice},
1042 {(unsigned const char *)yes_str, yes_voice}
1044 bool result;
1046 result = set_option(string, variable, BOOL, names, 2,
1047 (void (*)(int))function);
1048 return result;
1051 bool set_int(const unsigned char* string,
1052 const char* unit,
1053 int voice_unit,
1054 const int* variable,
1055 void (*function)(int),
1056 int step,
1057 int min,
1058 int max,
1059 void (*formatter)(char*, size_t, int, const char*) )
1061 return set_int_ex(string, unit, voice_unit, variable, function,
1062 step, min, max, formatter, NULL);
1065 bool set_int_ex(const unsigned char* string,
1066 const char* unit,
1067 int voice_unit,
1068 const int* variable,
1069 void (*function)(int),
1070 int step,
1071 int min,
1072 int max,
1073 void (*formatter)(char*, size_t, int, const char*),
1074 int32_t (*get_talk_id)(int, int))
1076 (void)unit;
1077 struct settings_list item;
1078 struct int_setting data = {
1079 function, voice_unit, min, max, step,
1080 formatter, get_talk_id
1082 item.int_setting = &data;
1083 item.flags = F_INT_SETTING|F_T_INT;
1084 item.lang_id = -1;
1085 item.cfg_vals = (char*)string;
1086 item.setting = (void *)variable;
1087 return option_screen(&item, NULL, false, NULL);
1091 static const struct opt_items *set_option_options;
1092 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1094 (void)unit;
1095 const unsigned char *text = set_option_options[item].string;
1096 strncpy(buf, P2STR(text), size);
1098 static int32_t set_option_get_talk_id(int value, int unit)
1100 (void)unit;
1101 return set_option_options[value].voice_id;
1103 bool set_option(const char* string, const void* variable, enum optiontype type,
1104 const struct opt_items* options,
1105 int numoptions, void (*function)(int))
1107 int temp;
1108 struct settings_list item;
1109 struct int_setting data = {
1110 function, UNIT_INT, 0, numoptions-1, 1,
1111 set_option_formatter, set_option_get_talk_id
1113 set_option_options = options;
1114 item.int_setting = &data;
1115 item.flags = F_INT_SETTING|F_T_INT;
1116 item.lang_id = -1;
1117 item.cfg_vals = (char*)string;
1118 item.setting = &temp;
1119 if (type == BOOL)
1120 temp = *(bool*)variable? 1: 0;
1121 else
1122 temp = *(int*)variable;
1123 if (!option_screen(&item, NULL, false, NULL))
1125 if (type == BOOL)
1126 *(bool*)variable = (temp == 1? true: false);
1127 else
1128 *(int*)variable = temp;
1129 return false;
1131 return true;
1135 void set_file(const char* filename, char* setting, int maxlen)
1137 char* fptr = strrchr(filename,'/');
1138 int len;
1139 int extlen = 0;
1140 char* ptr;
1142 if (!fptr)
1143 return;
1145 *fptr = 0;
1146 fptr++;
1148 len = strlen(fptr);
1149 ptr = fptr + len;
1150 while ((*ptr != '.') && (ptr != fptr)) {
1151 extlen++;
1152 ptr--;
1154 if(ptr == fptr) extlen = 0;
1156 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1157 (len-extlen > maxlen))
1158 return;
1160 strncpy(setting, fptr, len-extlen);
1161 setting[len-extlen]=0;
1163 settings_save();