Fix a typo in the comment and add a (somewhat vague) description of the values returned.
[kugel-rb/myfork.git] / apps / settings.c
blob71c29f67c45004e38ae7eaae9218b9ef0d72de1b
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 "gwps.h"
56 #include "powermgmt.h"
57 #include "sprintf.h"
58 #include "keyboard.h"
59 #include "version.h"
60 #include "sound.h"
61 #include "rbunicode.h"
62 #include "dircache.h"
63 #include "statusbar.h"
64 #include "splash.h"
65 #include "list.h"
66 #include "settings_list.h"
67 #include "filetypes.h"
68 #include "option_select.h"
69 #include "backdrop.h"
71 #if CONFIG_TUNER
72 #include "radio.h"
73 #endif
75 #if CONFIG_CODEC == MAS3507D
76 void dac_line_in(bool enable);
77 #endif
78 struct user_settings global_settings;
79 struct system_status global_status;
81 #if CONFIG_CODEC == SWCODEC
82 #include "dsp.h"
83 #include "playback.h"
84 #ifdef HAVE_RECORDING
85 #include "enc_config.h"
86 #endif
87 #endif /* CONFIG_CODEC == SWCODEC */
89 #define NVRAM_BLOCK_SIZE 44
91 #ifdef HAVE_LCD_BITMAP
92 #define MAX_LINES 10
93 #else
94 #define MAX_LINES 2
95 #endif
97 #ifdef HAVE_REMOTE_LCD
98 #include "lcd-remote.h"
99 #endif
101 long lasttime = 0;
103 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
104 /* NVRAM is set out as
105 [0] 'R'
106 [1] 'b'
107 [2] version
108 [3] stored variable count
109 [4-7] crc32 checksum
110 [8-NVRAM_BLOCK_SIZE] data
112 #define NVRAM_DATA_START 8
113 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
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 if (fd < 0)
123 return false;
124 memset(buf,0,max_len);
125 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
126 return false;
127 close(fd);
128 #else
129 memset(buf,0,max_len);
130 /* read rtc block */
131 for (i=0; i < max_len; i++ )
132 buf[i] = rtc_read(0x14+i);
133 #endif
134 /* check magic, version */
135 if ((buf[0] != 'R') || (buf[1] != 'b')
136 || (buf[2] != NVRAM_CONFIG_VERSION))
137 return false;
138 /* check crc32 */
139 crc32 = crc_32(&buf[NVRAM_DATA_START],
140 max_len-NVRAM_DATA_START-1,0xffffffff);
141 if (memcmp(&crc32,&buf[4],4))
142 return false;
143 /* all good, so read in the settings */
144 var_count = buf[3];
145 buf_pos = NVRAM_DATA_START;
146 for(i=0; i<nb_settings; i++)
148 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
149 >>F_NVRAM_MASK_SHIFT;
150 if (nvram_bytes)
152 if ((var_count>0) && (buf_pos<max_len))
154 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
155 buf_pos += nvram_bytes;
156 var_count--;
158 else /* should only happen when new items are added to the end */
160 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
164 return true;
166 static bool write_nvram_data(char* buf, int max_len)
168 unsigned crc32 = 0xffffffff;
169 int i = 0, buf_pos = 0;
170 char var_count = 0;
171 #ifndef HAVE_RTC_RAM
172 int fd;
173 #endif
174 memset(buf,0,max_len);
175 /* magic, version */
176 buf[0] = 'R'; buf[1] = 'b';
177 buf[2] = NVRAM_CONFIG_VERSION;
178 buf_pos = NVRAM_DATA_START;
179 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
181 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
182 >>F_NVRAM_MASK_SHIFT;
183 if (nvram_bytes)
185 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
186 buf_pos += nvram_bytes;
187 var_count++;
190 /* count and crc32 */
191 buf[3] = var_count;
192 crc32 = crc_32(&buf[NVRAM_DATA_START],
193 max_len-NVRAM_DATA_START-1,0xffffffff);
194 memcpy(&buf[4],&crc32,4);
195 #ifndef HAVE_RTC_RAM
196 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
197 if (fd >= 0)
199 int len = write(fd,buf,max_len);
200 close(fd);
201 if (len < 8)
202 return false;
204 #else
205 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
206 that it would write a number of bytes at a time since the RTC chip
207 supports that, but this will have to do for now 8-) */
208 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
209 int r = rtc_write(0x14+i, buf[i]);
210 if (r)
211 return false;
213 #endif
214 return true;
217 /** Reading from a config file **/
219 * load settings from disk or RTC RAM
221 void settings_load(int which)
223 if (which&SETTINGS_RTC)
224 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
225 if (which&SETTINGS_HD)
227 settings_load_config(CONFIGFILE,false);
228 settings_load_config(FIXEDSETTINGSFILE,false);
232 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
234 const char* start = settings[setting_id].cfg_vals;
235 char* end = NULL;
236 char temp[MAX_PATH];
237 int count = 0;
238 while (1)
240 end = strchr(start, ',');
241 if (!end)
243 if (!strcmp(str, start))
245 *out = count;
246 return true;
248 else return false;
250 strncpy(temp, start, end-start);
251 temp[end-start] = '\0';
252 if (!strcmp(str, temp))
254 *out = count;
255 return true;
257 start = end +1;
258 count++;
260 return false;
263 bool settings_load_config(const char* file, bool apply)
265 int fd;
266 char line[128];
267 char* name;
268 char* value;
269 int i;
270 fd = open_utf8(file, O_RDONLY);
271 if (fd < 0)
272 return false;
274 while (read_line(fd, line, sizeof line) > 0)
276 if (!settings_parseline(line, &name, &value))
277 continue;
278 for(i=0; i<nb_settings; i++)
280 if (settings[i].cfg_name == NULL)
281 continue;
282 if (!strcasecmp(name,settings[i].cfg_name))
284 switch (settings[i].flags&F_T_MASK)
286 case F_T_CUSTOM:
287 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
288 break;
289 case F_T_INT:
290 case F_T_UINT:
291 #ifdef HAVE_LCD_COLOR
292 if (settings[i].flags&F_RGB)
293 hex_to_rgb(value, (int*)settings[i].setting);
294 else
295 #endif
296 if (settings[i].cfg_vals == NULL)
298 *(int*)settings[i].setting = atoi(value);
300 else
302 int temp, *v = (int*)settings[i].setting;
303 bool found = cfg_string_to_int(i, &temp, value);
304 if (found)
306 if (settings[i].flags&F_TABLE_SETTING)
307 *v = settings[i].table_setting->values[temp];
308 else
309 *v = temp;
311 else
312 *v = atoi(value);
314 break;
315 case F_T_BOOL:
317 int temp;
318 if (cfg_string_to_int(i,&temp,value))
319 *(bool*)settings[i].setting = (temp==0?false:true);
320 if (settings[i].bool_setting->option_callback)
321 settings[i].bool_setting->option_callback(temp==0?false:true);
322 break;
324 case F_T_CHARPTR:
325 case F_T_UCHARPTR:
327 char storage[MAX_PATH];
328 if (settings[i].filename_setting->prefix)
330 int len = strlen(settings[i].filename_setting->prefix);
331 if (!strncasecmp(value,
332 settings[i].filename_setting->prefix,
333 len))
335 strncpy(storage,&value[len],MAX_PATH);
337 else strncpy(storage,value,MAX_PATH);
339 else strncpy(storage,value,MAX_PATH);
340 if (settings[i].filename_setting->suffix)
342 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
343 if (s) *s = '\0';
345 strncpy((char*)settings[i].setting,storage,
346 settings[i].filename_setting->max_len);
347 ((char*)settings[i].setting)
348 [settings[i].filename_setting->max_len-1] = '\0';
349 break;
352 break;
353 } /* if (!strcmp(name,settings[i].cfg_name)) */
354 } /* for(...) */
355 } /* while(...) */
357 close(fd);
358 settings_save();
359 if (apply)
360 settings_apply(true);
361 return true;
364 /** Writing to a config file and saving settings **/
366 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
368 int flags = settings[setting_id].flags;
369 const char* start = settings[setting_id].cfg_vals;
370 char* end = NULL;
371 int count = 0;
373 if ((flags&F_T_MASK)==F_T_INT &&
374 flags&F_TABLE_SETTING)
376 const int *value = settings[setting_id].table_setting->values;
377 while (start)
379 end = strchr(start,',');
380 if (value[count] == val)
382 if (end == NULL)
383 strncpy(buf, start, buf_len);
384 else
386 int len = (buf_len > (end-start))? end-start: buf_len;
387 strncpy(buf, start, len);
388 buf[len] = '\0';
390 return true;
392 count++;
394 if (end)
395 start = end+1;
396 else
397 break;
399 return false;
402 while (count < val)
404 start = strchr(start,',');
405 if (!start)
406 return false;
407 count++;
408 start++;
410 end = strchr(start,',');
411 if (end == NULL)
412 strncpy(buf, start, buf_len);
413 else
415 int len = (buf_len > (end-start))? end-start: buf_len;
416 strncpy(buf, start, len);
417 buf[len] = '\0';
419 return true;
422 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
424 switch (settings[i].flags&F_T_MASK)
426 case F_T_CUSTOM:
427 settings[i].custom_setting->write_to_cfg(settings[i].setting,
428 buf, buf_len);
429 break;
430 case F_T_INT:
431 case F_T_UINT:
432 #ifdef HAVE_LCD_COLOR
433 if (settings[i].flags&F_RGB)
435 int colour = *(int*)settings[i].setting;
436 snprintf(buf,buf_len,"%02x%02x%02x",
437 (int)RGB_UNPACK_RED(colour),
438 (int)RGB_UNPACK_GREEN(colour),
439 (int)RGB_UNPACK_BLUE(colour));
441 else
442 #endif
443 if (settings[i].cfg_vals == NULL)
445 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
447 else
449 if (cfg_int_to_string(i, *(int*)settings[i].setting,
450 buf, buf_len) == false)
452 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
454 else
455 return false;
457 break;
458 case F_T_BOOL:
459 cfg_int_to_string(i,
460 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
461 break;
462 case F_T_CHARPTR:
463 case F_T_UCHARPTR:
464 if (((char*)settings[i].setting)[0]
465 && settings[i].filename_setting->prefix)
467 snprintf(buf,buf_len,"%s%s%s",
468 settings[i].filename_setting->prefix,
469 (char*)settings[i].setting,
470 settings[i].filename_setting->suffix);
472 else strncpy(buf,(char*)settings[i].setting,
473 settings[i].filename_setting->max_len);
474 break;
475 } /* switch () */
476 return true;
480 static bool is_changed(int setting_id)
482 const struct settings_list *setting = &settings[setting_id];
483 switch (setting->flags&F_T_MASK)
485 case F_T_CUSTOM:
486 return setting->custom_setting->is_changed(setting->setting,
487 setting->default_val.custom);
488 break;
489 case F_T_INT:
490 case F_T_UINT:
491 if (setting->flags&F_DEF_ISFUNC)
493 if (*(int*)setting->setting == setting->default_val.func())
494 return false;
496 else if (setting->flags&F_T_SOUND)
498 if (*(int*)setting->setting ==
499 sound_default(setting->sound_setting->setting))
500 return false;
502 else if (*(int*)setting->setting == setting->default_val.int_)
503 return false;
504 break;
505 case F_T_BOOL:
506 if (*(bool*)setting->setting == setting->default_val.bool_)
507 return false;
508 break;
509 case F_T_CHARPTR:
510 case F_T_UCHARPTR:
511 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
512 return false;
513 break;
515 return true;
518 static bool settings_write_config(const char* filename, int options)
520 int i;
521 int fd;
522 char value[MAX_PATH];
523 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
524 if (fd < 0)
525 return false;
526 fdprintf(fd, "# .cfg file created by rockbox %s - "
527 "http://www.rockbox.org\r\n\r\n", appsversion);
528 for(i=0; i<nb_settings; i++)
530 if (settings[i].cfg_name == NULL)
531 continue;
532 value[0] = '\0';
534 switch (options)
536 case SETTINGS_SAVE_CHANGED:
537 if (!is_changed(i))
538 continue;
539 break;
540 case SETTINGS_SAVE_SOUND:
541 if ((settings[i].flags&F_SOUNDSETTING) == 0)
542 continue;
543 break;
544 case SETTINGS_SAVE_THEME:
545 if ((settings[i].flags&F_THEMESETTING) == 0)
546 continue;
547 break;
548 #ifdef HAVE_RECORDING
549 case SETTINGS_SAVE_RECPRESETS:
550 if ((settings[i].flags&F_RECSETTING) == 0)
551 continue;
552 break;
553 #endif
554 #if CONFIG_CODEC == SWCODEC
555 case SETTINGS_SAVE_EQPRESET:
556 if ((settings[i].flags&F_EQSETTING) == 0)
557 continue;
558 break;
559 #endif
562 cfg_to_string(i, value, MAX_PATH);
563 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
564 } /* for(...) */
565 close(fd);
566 return true;
568 #ifndef HAVE_RTC_RAM
569 static bool flush_global_status_callback(void)
571 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
573 #endif
574 static bool flush_config_block_callback(void)
576 bool r1, r2;
577 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
578 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
579 return r1 || r2;
583 * persist all runtime user settings to disk or RTC RAM
585 static void update_runtime(void)
587 int elapsed_secs;
589 elapsed_secs = (current_tick - lasttime) / HZ;
590 global_status.runtime += elapsed_secs;
591 lasttime += (elapsed_secs * HZ);
593 if ( global_status.runtime > global_status.topruntime )
594 global_status.topruntime = global_status.runtime;
597 void status_save(void)
599 update_runtime();
600 #ifdef HAVE_RTC_RAM
601 /* this will be done in the storage_callback if
602 target doesnt have rtc ram */
603 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
604 #else
605 register_storage_idle_func(flush_global_status_callback);
606 #endif
609 int settings_save(void)
611 update_runtime();
612 #ifdef HAVE_RTC_RAM
613 /* this will be done in the storage_callback if
614 target doesnt have rtc ram */
615 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
616 #endif
617 register_storage_idle_func(flush_config_block_callback);
618 return 0;
621 bool settings_save_config(int options)
623 char filename[MAX_PATH];
624 char *folder;
625 switch (options)
627 case SETTINGS_SAVE_THEME:
628 folder = THEME_DIR;
629 break;
630 #ifdef HAVE_RECORDING
631 case SETTINGS_SAVE_RECPRESETS:
632 folder = RECPRESETS_DIR;
633 break;
634 #endif
635 #if CONFIG_CODEC == SWCODEC
636 case SETTINGS_SAVE_EQPRESET:
637 folder = EQS_DIR;
638 break;
639 #endif
640 case SETTINGS_SAVE_SOUND:
641 default:
642 folder = ROCKBOX_DIR;
644 create_numbered_filename(filename, folder, "config", ".cfg", 2
645 IF_CNFN_NUM_(, NULL));
647 /* allow user to modify filename */
648 while (true) {
649 if (!kbd_input(filename, sizeof filename)) {
650 break;
652 else {
653 splash(HZ, ID2P(LANG_CANCEL));
654 return false;
658 if (settings_write_config(filename, options))
659 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
660 else
661 splash(HZ, ID2P(LANG_FAILED));
662 return true;
665 /** Apply and Reset settings **/
668 #ifdef HAVE_LCD_BITMAP
670 * Applies the range infos stored in global_settings to
671 * the peak meter.
673 void settings_apply_pm_range(void)
675 int pm_min, pm_max;
677 /* depending on the scale mode (dBfs or percent) the values
678 of global_settings.peak_meter_dbfs have different meanings */
679 if (global_settings.peak_meter_dbfs)
681 /* convert to dBfs * 100 */
682 pm_min = -(((int)global_settings.peak_meter_min) * 100);
683 pm_max = -(((int)global_settings.peak_meter_max) * 100);
685 else
687 /* percent is stored directly -> no conversion */
688 pm_min = global_settings.peak_meter_min;
689 pm_max = global_settings.peak_meter_max;
692 /* apply the range */
693 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
695 #endif /* HAVE_LCD_BITMAP */
697 void sound_settings_apply(void)
699 #if CONFIG_CODEC == SWCODEC
700 sound_set_dsp_callback(dsp_callback);
701 #endif
702 sound_set(SOUND_BASS, global_settings.bass);
703 sound_set(SOUND_TREBLE, global_settings.treble);
704 sound_set(SOUND_BALANCE, global_settings.balance);
705 sound_set(SOUND_VOLUME, global_settings.volume);
706 sound_set(SOUND_CHANNELS, global_settings.channel_config);
707 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
708 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
709 sound_set(SOUND_LOUDNESS, global_settings.loudness);
710 sound_set(SOUND_AVC, global_settings.avc);
711 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
712 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
713 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
714 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
715 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
716 sound_set(SOUND_SUPERBASS, global_settings.superbass);
717 #endif
719 #ifdef HAVE_WM8758
720 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
721 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
722 #endif
725 void settings_apply(bool read_disk)
727 char buf[64];
728 #if CONFIG_CODEC == SWCODEC
729 int i;
730 #endif
732 sound_settings_apply();
734 #ifdef HAVE_DISK_STORAGE
735 audio_set_buffer_margin(global_settings.buffer_margin);
736 #endif
738 #ifdef HAVE_LCD_CONTRAST
739 lcd_set_contrast(global_settings.contrast);
740 #endif
741 lcd_scroll_speed(global_settings.scroll_speed);
742 #ifdef HAVE_REMOTE_LCD
743 lcd_remote_set_contrast(global_settings.remote_contrast);
744 lcd_remote_set_invert_display(global_settings.remote_invert);
745 lcd_remote_set_flip(global_settings.remote_flip_display);
746 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
747 lcd_remote_scroll_step(global_settings.remote_scroll_step);
748 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
749 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
750 #ifdef HAVE_REMOTE_LCD_TICKING
751 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
752 #endif
753 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
754 #if CONFIG_CHARGING
755 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
756 #endif
757 #ifdef HAS_REMOTE_BUTTON_HOLD
758 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
759 #endif
760 #endif /* HAVE_REMOTE_LCD */
761 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
762 backlight_set_brightness(global_settings.brightness);
763 #endif
764 #ifdef HAVE_BACKLIGHT
765 backlight_set_timeout(global_settings.backlight_timeout);
766 #if CONFIG_CHARGING
767 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
768 #endif
769 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
770 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
771 backlight_set_fade_in(global_settings.backlight_fade_in);
772 backlight_set_fade_out(global_settings.backlight_fade_out);
773 #endif
774 #endif
775 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
776 buttonlight_set_brightness(global_settings.buttonlight_brightness);
777 #endif
778 #ifdef HAVE_BUTTON_LIGHT
779 buttonlight_set_timeout(global_settings.buttonlight_timeout);
780 #endif
781 #ifdef HAVE_DISK_STORAGE
782 storage_spindown(global_settings.disk_spindown);
783 #endif
784 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
785 dac_line_in(global_settings.line_in);
786 #endif
787 set_poweroff_timeout(global_settings.poweroff);
789 set_battery_capacity(global_settings.battery_capacity);
790 #if BATTERY_TYPES_COUNT > 1
791 set_battery_type(global_settings.battery_type);
792 #endif
794 #ifdef HAVE_LCD_BITMAP
795 #ifdef HAVE_LCD_INVERT
796 lcd_set_invert_display(global_settings.invert);
797 #endif
798 #ifdef HAVE_LCD_FLIP
799 lcd_set_flip(global_settings.flip_display);
800 button_set_flip(global_settings.flip_display);
801 #endif
802 lcd_update(); /* refresh after flipping the screen */
803 settings_apply_pm_range();
804 peak_meter_init_times(
805 global_settings.peak_meter_release, global_settings.peak_meter_hold,
806 global_settings.peak_meter_clip_hold);
807 #endif
809 #ifdef HAVE_SPEAKER
810 audiohw_enable_speaker(global_settings.speaker_enabled);
811 #endif
813 if (read_disk)
816 #ifdef HAVE_LCD_BITMAP
817 /* fonts need to be loaded before the WPS */
818 if ( global_settings.font_file[0]) {
819 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
820 global_settings.font_file);
821 if (font_load(buf) == NULL)
822 font_reset();
824 else
825 font_reset();
827 if ( global_settings.kbd_file[0]) {
828 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
829 global_settings.kbd_file);
830 load_kbd(buf);
832 else
833 load_kbd(NULL);
834 #endif
835 #if LCD_DEPTH > 1
836 unload_wps_backdrop();
837 #endif
838 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
839 unload_remote_wps_backdrop();
840 #endif
841 if ( global_settings.wps_file[0] &&
842 global_settings.wps_file[0] != 0xff ) {
843 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
844 global_settings.wps_file);
845 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
847 else
849 wps_data_init(gui_wps[0].data);
850 #ifdef HAVE_REMOTE_LCD
851 gui_wps[0].data->remote_wps = false;
852 #endif
855 #if LCD_DEPTH > 1
856 if ( global_settings.backdrop_file[0] &&
857 global_settings.backdrop_file[0] != 0xff ) {
858 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
859 global_settings.backdrop_file);
860 load_main_backdrop(buf);
861 } else {
862 unload_main_backdrop();
864 show_main_backdrop();
865 #endif
866 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
867 show_remote_main_backdrop();
868 #endif
870 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
871 if ( global_settings.rwps_file[0]) {
872 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
873 global_settings.rwps_file);
874 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
876 else
878 wps_data_init(gui_wps[1].data);
879 gui_wps[1].data->remote_wps = true;
881 #endif
882 if ( global_settings.lang_file[0]) {
883 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
884 global_settings.lang_file);
885 lang_load(buf);
886 talk_init(); /* use voice of same language */
888 /* load the icon set */
889 icons_init();
891 #ifdef HAVE_LCD_COLOR
892 if (global_settings.colors_file[0])
893 read_color_theme_file();
894 #endif
897 #ifdef HAVE_LCD_COLOR
898 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
899 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
900 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
901 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
902 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
903 #endif
905 #ifdef HAVE_LCD_BITMAP
906 lcd_scroll_step(global_settings.scroll_step);
907 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
908 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
909 #else
910 lcd_jump_scroll(global_settings.jump_scroll);
911 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
912 #endif
913 lcd_bidir_scroll(global_settings.bidir_limit);
914 lcd_scroll_delay(global_settings.scroll_delay);
917 set_codepage(global_settings.default_codepage);
919 #if CONFIG_CODEC == SWCODEC
920 audio_set_crossfade(global_settings.crossfade);
921 dsp_set_replaygain();
922 dsp_set_crossfeed(global_settings.crossfeed);
923 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
924 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
925 global_settings.crossfeed_hf_attenuation,
926 global_settings.crossfeed_hf_cutoff);
928 /* Configure software equalizer, hardware eq is handled in audio_init() */
929 dsp_set_eq(global_settings.eq_enabled);
930 dsp_set_eq_precut(global_settings.eq_precut);
931 for(i = 0; i < 5; i++) {
932 dsp_set_eq_coefs(i);
935 dsp_dither_enable(global_settings.dithering_enabled);
936 #endif
938 #ifdef HAVE_SPDIF_POWER
939 spdif_power_enable(global_settings.spdif_enable);
940 #endif
942 #ifdef HAVE_BACKLIGHT
943 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
944 #ifdef HAVE_REMOTE_LCD
945 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
946 #endif
947 #ifdef HAS_BUTTON_HOLD
948 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
949 #endif
950 #ifdef HAVE_LCD_SLEEP_SETTING
951 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
952 #endif
953 #endif /* HAVE_BACKLIGHT */
955 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
956 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
957 #endif
959 #ifdef HAVE_USB_CHARGING_ENABLE
960 usb_charging_enable(global_settings.usb_charging);
961 #endif
963 /* This should stay last */
964 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
965 enc_global_settings_apply();
966 #endif
967 list_init_viewports(NULL);
972 * reset all settings to their default value
974 void reset_setting(const struct settings_list *setting, void *var)
976 switch (setting->flags&F_T_MASK)
978 case F_T_CUSTOM:
979 setting->custom_setting->set_default(setting->setting,
980 setting->default_val.custom);
981 break;
982 case F_T_INT:
983 case F_T_UINT:
984 if (setting->flags&F_DEF_ISFUNC)
985 *(int*)var = setting->default_val.func();
986 else if (setting->flags&F_T_SOUND)
987 *(int*)var = sound_default(setting->sound_setting->setting);
988 else *(int*)var = setting->default_val.int_;
989 break;
990 case F_T_BOOL:
991 *(bool*)var = setting->default_val.bool_;
992 break;
993 case F_T_CHARPTR:
994 case F_T_UCHARPTR:
995 strncpy((char*)var, setting->default_val.charptr,
996 setting->filename_setting->max_len);
997 break;
1001 void settings_reset(void)
1003 int i;
1005 for(i=0; i<nb_settings; i++)
1006 reset_setting(&settings[i], settings[i].setting);
1007 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1008 enc_global_settings_reset();
1009 #endif
1012 /** Changing setting values **/
1013 const struct settings_list* find_setting(const void* variable, int *id)
1015 int i;
1016 for(i=0;i<nb_settings;i++)
1018 if (settings[i].setting == variable)
1020 if (id)
1021 *id = i;
1022 return &settings[i];
1025 return NULL;
1028 bool set_bool(const char* string, const bool* variable )
1030 return set_bool_options(string, variable,
1031 (char *)STR(LANG_SET_BOOL_YES),
1032 (char *)STR(LANG_SET_BOOL_NO),
1033 NULL);
1037 bool set_bool_options(const char* string, const bool* variable,
1038 const char* yes_str, int yes_voice,
1039 const char* no_str, int no_voice,
1040 void (*function)(bool))
1042 struct opt_items names[] = {
1043 {(unsigned const char *)no_str, no_voice},
1044 {(unsigned const char *)yes_str, yes_voice}
1046 bool result;
1048 result = set_option(string, variable, BOOL, names, 2,
1049 (void (*)(int))function);
1050 return result;
1053 bool set_int(const unsigned char* string,
1054 const char* unit,
1055 int voice_unit,
1056 const int* variable,
1057 void (*function)(int),
1058 int step,
1059 int min,
1060 int max,
1061 void (*formatter)(char*, size_t, int, const char*) )
1063 return set_int_ex(string, unit, voice_unit, variable, function,
1064 step, min, max, formatter, NULL);
1067 bool set_int_ex(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*),
1076 int32_t (*get_talk_id)(int, int))
1078 (void)unit;
1079 struct settings_list item;
1080 struct int_setting data = {
1081 function, voice_unit, min, max, step,
1082 formatter, get_talk_id
1084 item.int_setting = &data;
1085 item.flags = F_INT_SETTING|F_T_INT;
1086 item.lang_id = -1;
1087 item.cfg_vals = (char*)string;
1088 item.setting = (void *)variable;
1089 return option_screen(&item, NULL, false, NULL);
1093 static const struct opt_items *set_option_options;
1094 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1096 (void)unit;
1097 const unsigned char *text = set_option_options[item].string;
1098 strncpy(buf, P2STR(text), size);
1100 static int32_t set_option_get_talk_id(int value, int unit)
1102 (void)unit;
1103 return set_option_options[value].voice_id;
1105 bool set_option(const char* string, const void* variable, enum optiontype type,
1106 const struct opt_items* options,
1107 int numoptions, void (*function)(int))
1109 int temp;
1110 struct settings_list item;
1111 struct int_setting data = {
1112 function, UNIT_INT, 0, numoptions-1, 1,
1113 set_option_formatter, set_option_get_talk_id
1115 set_option_options = options;
1116 item.int_setting = &data;
1117 item.flags = F_INT_SETTING|F_T_INT;
1118 item.lang_id = -1;
1119 item.cfg_vals = (char*)string;
1120 item.setting = &temp;
1121 if (type == BOOL)
1122 temp = *(bool*)variable? 1: 0;
1123 else
1124 temp = *(int*)variable;
1125 if (!option_screen(&item, NULL, false, NULL))
1127 if (type == BOOL)
1128 *(bool*)variable = (temp == 1? true: false);
1129 else
1130 *(int*)variable = temp;
1131 return false;
1133 return true;
1137 void set_file(const char* filename, char* setting, int maxlen)
1139 char* fptr = strrchr(filename,'/');
1140 int len;
1141 int extlen = 0;
1142 char* ptr;
1144 if (!fptr)
1145 return;
1147 *fptr = 0;
1148 fptr++;
1150 len = strlen(fptr);
1151 ptr = fptr + len;
1152 while ((*ptr != '.') && (ptr != fptr)) {
1153 extlen++;
1154 ptr--;
1156 if(ptr == fptr) extlen = 0;
1158 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1159 (len-extlen > maxlen))
1160 return;
1162 strncpy(setting, fptr, len-extlen);
1163 setting[len-extlen]=0;
1165 settings_save();