Correct the BBX clause (no visual changes)
[kugel-rb/myfork.git] / apps / settings.c
blob30b056da53003796fd4c2ce3d09788d35766082d
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 if (read_disk)
812 #ifdef HAVE_LCD_BITMAP
813 /* fonts need to be loaded before the WPS */
814 if ( global_settings.font_file[0]) {
815 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
816 global_settings.font_file);
817 if (font_load(buf) == NULL)
818 font_reset();
820 else
821 font_reset();
823 if ( global_settings.kbd_file[0]) {
824 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
825 global_settings.kbd_file);
826 load_kbd(buf);
828 else
829 load_kbd(NULL);
830 #endif
831 #if LCD_DEPTH > 1
832 unload_wps_backdrop();
833 #endif
834 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
835 unload_remote_wps_backdrop();
836 #endif
837 if ( global_settings.wps_file[0] &&
838 global_settings.wps_file[0] != 0xff ) {
839 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
840 global_settings.wps_file);
841 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
843 else
845 wps_data_init(gui_wps[0].data);
846 #ifdef HAVE_REMOTE_LCD
847 gui_wps[0].data->remote_wps = false;
848 #endif
851 #if LCD_DEPTH > 1
852 if ( global_settings.backdrop_file[0] &&
853 global_settings.backdrop_file[0] != 0xff ) {
854 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
855 global_settings.backdrop_file);
856 load_main_backdrop(buf);
857 } else {
858 unload_main_backdrop();
860 show_main_backdrop();
861 #endif
862 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
863 show_remote_main_backdrop();
864 #endif
866 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
867 if ( global_settings.rwps_file[0]) {
868 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
869 global_settings.rwps_file);
870 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
872 else
874 wps_data_init(gui_wps[1].data);
875 gui_wps[1].data->remote_wps = true;
877 #endif
878 if ( global_settings.lang_file[0]) {
879 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
880 global_settings.lang_file);
881 lang_load(buf);
882 talk_init(); /* use voice of same language */
884 /* load the icon set */
885 icons_init();
887 #ifdef HAVE_LCD_COLOR
888 if (global_settings.colors_file[0])
889 read_color_theme_file();
890 #endif
893 #ifdef HAVE_LCD_COLOR
894 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
895 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
896 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
897 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
898 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
899 #endif
901 #ifdef HAVE_LCD_BITMAP
902 lcd_scroll_step(global_settings.scroll_step);
903 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
904 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
905 #else
906 lcd_jump_scroll(global_settings.jump_scroll);
907 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
908 #endif
909 lcd_bidir_scroll(global_settings.bidir_limit);
910 lcd_scroll_delay(global_settings.scroll_delay);
913 set_codepage(global_settings.default_codepage);
915 #if CONFIG_CODEC == SWCODEC
916 audio_set_crossfade(global_settings.crossfade);
917 dsp_set_replaygain();
918 dsp_set_crossfeed(global_settings.crossfeed);
919 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
920 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
921 global_settings.crossfeed_hf_attenuation,
922 global_settings.crossfeed_hf_cutoff);
924 /* Configure software equalizer, hardware eq is handled in audio_init() */
925 dsp_set_eq(global_settings.eq_enabled);
926 dsp_set_eq_precut(global_settings.eq_precut);
927 for(i = 0; i < 5; i++) {
928 dsp_set_eq_coefs(i);
931 dsp_dither_enable(global_settings.dithering_enabled);
932 #endif
934 #ifdef HAVE_SPDIF_POWER
935 spdif_power_enable(global_settings.spdif_enable);
936 #endif
938 #ifdef HAVE_BACKLIGHT
939 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
940 #ifdef HAVE_REMOTE_LCD
941 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
942 #endif
943 #ifdef HAS_BUTTON_HOLD
944 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
945 #endif
946 #ifdef HAVE_LCD_SLEEP_SETTING
947 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
948 #endif
949 #endif /* HAVE_BACKLIGHT */
951 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
952 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
953 #endif
955 #ifdef HAVE_USB_CHARGING_ENABLE
956 usb_charging_enable(global_settings.usb_charging);
957 #endif
959 /* This should stay last */
960 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
961 enc_global_settings_apply();
962 #endif
963 list_init_viewports(NULL);
968 * reset all settings to their default value
970 void reset_setting(const struct settings_list *setting, void *var)
972 switch (setting->flags&F_T_MASK)
974 case F_T_CUSTOM:
975 setting->custom_setting->set_default(setting->setting,
976 setting->default_val.custom);
977 break;
978 case F_T_INT:
979 case F_T_UINT:
980 if (setting->flags&F_DEF_ISFUNC)
981 *(int*)var = setting->default_val.func();
982 else if (setting->flags&F_T_SOUND)
983 *(int*)var = sound_default(setting->sound_setting->setting);
984 else *(int*)var = setting->default_val.int_;
985 break;
986 case F_T_BOOL:
987 *(bool*)var = setting->default_val.bool_;
988 break;
989 case F_T_CHARPTR:
990 case F_T_UCHARPTR:
991 strncpy((char*)var, setting->default_val.charptr,
992 setting->filename_setting->max_len);
993 break;
997 void settings_reset(void)
999 int i;
1001 for(i=0; i<nb_settings; i++)
1002 reset_setting(&settings[i], settings[i].setting);
1003 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1004 enc_global_settings_reset();
1005 #endif
1008 /** Changing setting values **/
1009 const struct settings_list* find_setting(const void* variable, int *id)
1011 int i;
1012 for(i=0;i<nb_settings;i++)
1014 if (settings[i].setting == variable)
1016 if (id)
1017 *id = i;
1018 return &settings[i];
1021 return NULL;
1024 bool set_bool(const char* string, const bool* variable )
1026 return set_bool_options(string, variable,
1027 (char *)STR(LANG_SET_BOOL_YES),
1028 (char *)STR(LANG_SET_BOOL_NO),
1029 NULL);
1033 bool set_bool_options(const char* string, const bool* variable,
1034 const char* yes_str, int yes_voice,
1035 const char* no_str, int no_voice,
1036 void (*function)(bool))
1038 struct opt_items names[] = {
1039 {(unsigned const char *)no_str, no_voice},
1040 {(unsigned const char *)yes_str, yes_voice}
1042 bool result;
1044 result = set_option(string, variable, BOOL, names, 2,
1045 (void (*)(int))function);
1046 return result;
1049 bool set_int(const unsigned char* string,
1050 const char* unit,
1051 int voice_unit,
1052 const int* variable,
1053 void (*function)(int),
1054 int step,
1055 int min,
1056 int max,
1057 void (*formatter)(char*, size_t, int, const char*) )
1059 return set_int_ex(string, unit, voice_unit, variable, function,
1060 step, min, max, formatter, NULL);
1063 bool set_int_ex(const unsigned char* string,
1064 const char* unit,
1065 int voice_unit,
1066 const int* variable,
1067 void (*function)(int),
1068 int step,
1069 int min,
1070 int max,
1071 void (*formatter)(char*, size_t, int, const char*),
1072 int32_t (*get_talk_id)(int, int))
1074 (void)unit;
1075 struct settings_list item;
1076 struct int_setting data = {
1077 function, voice_unit, min, max, step,
1078 formatter, get_talk_id
1080 item.int_setting = &data;
1081 item.flags = F_INT_SETTING|F_T_INT;
1082 item.lang_id = -1;
1083 item.cfg_vals = (char*)string;
1084 item.setting = (void *)variable;
1085 return option_screen(&item, NULL, false, NULL);
1089 static const struct opt_items *set_option_options;
1090 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1092 (void)unit;
1093 const unsigned char *text = set_option_options[item].string;
1094 strncpy(buf, P2STR(text), size);
1096 static int32_t set_option_get_talk_id(int value, int unit)
1098 (void)unit;
1099 return set_option_options[value].voice_id;
1101 bool set_option(const char* string, const void* variable, enum optiontype type,
1102 const struct opt_items* options,
1103 int numoptions, void (*function)(int))
1105 int temp;
1106 struct settings_list item;
1107 struct int_setting data = {
1108 function, UNIT_INT, 0, numoptions-1, 1,
1109 set_option_formatter, set_option_get_talk_id
1111 set_option_options = options;
1112 item.int_setting = &data;
1113 item.flags = F_INT_SETTING|F_T_INT;
1114 item.lang_id = -1;
1115 item.cfg_vals = (char*)string;
1116 item.setting = &temp;
1117 if (type == BOOL)
1118 temp = *(bool*)variable? 1: 0;
1119 else
1120 temp = *(int*)variable;
1121 if (!option_screen(&item, NULL, false, NULL))
1123 if (type == BOOL)
1124 *(bool*)variable = (temp == 1? true: false);
1125 else
1126 *(int*)variable = temp;
1127 return false;
1129 return true;
1133 void set_file(const char* filename, char* setting, int maxlen)
1135 char* fptr = strrchr(filename,'/');
1136 int len;
1137 int extlen = 0;
1138 char* ptr;
1140 if (!fptr)
1141 return;
1143 *fptr = 0;
1144 fptr++;
1146 len = strlen(fptr);
1147 ptr = fptr + len;
1148 while ((*ptr != '.') && (ptr != fptr)) {
1149 extlen++;
1150 ptr--;
1152 if(ptr == fptr) extlen = 0;
1154 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1155 (len-extlen > maxlen))
1156 return;
1158 strncpy(setting, fptr, len-extlen);
1159 setting[len-extlen]=0;
1161 settings_save();