Speed up the predictor a little by using ldrd/strd on ARMv5+. This required shuffling...
[kugel-rb.git] / apps / settings.c
blob609cb3f93f744cc8edc0a190c53149896f1991f2
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;
420 static bool is_changed(int setting_id)
422 const struct settings_list *setting = &settings[setting_id];
423 switch (setting->flags&F_T_MASK)
425 case F_T_CUSTOM:
426 return setting->custom_setting->is_changed(setting->setting,
427 setting->default_val.custom);
428 break;
429 case F_T_INT:
430 case F_T_UINT:
431 if (setting->flags&F_DEF_ISFUNC)
433 if (*(int*)setting->setting == setting->default_val.func())
434 return false;
436 else if (setting->flags&F_T_SOUND)
438 if (*(int*)setting->setting ==
439 sound_default(setting->sound_setting->setting))
440 return false;
442 else if (*(int*)setting->setting == setting->default_val.int_)
443 return false;
444 break;
445 case F_T_BOOL:
446 if (*(bool*)setting->setting == setting->default_val.bool_)
447 return false;
448 break;
449 case F_T_CHARPTR:
450 case F_T_UCHARPTR:
451 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
452 return false;
453 break;
455 return true;
458 static bool settings_write_config(const char* filename, int options)
460 int i;
461 int fd;
462 char value[MAX_PATH];
463 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
464 if (fd < 0)
465 return false;
466 #if CONFIG_TUNER
467 bool statusbar = global_settings.statusbar;
468 if (global_status.statusbar_forced != 0 && statusbar)
469 global_settings.statusbar = false;
470 #endif
471 fdprintf(fd, "# .cfg file created by rockbox %s - "
472 "http://www.rockbox.org\r\n\r\n", appsversion);
473 for(i=0; i<nb_settings; i++)
475 if (settings[i].cfg_name == NULL)
476 continue;
477 value[0] = '\0';
479 switch (options)
481 case SETTINGS_SAVE_CHANGED:
482 if (!is_changed(i))
483 continue;
484 break;
485 case SETTINGS_SAVE_SOUND:
486 if ((settings[i].flags&F_SOUNDSETTING) == 0)
487 continue;
488 break;
489 case SETTINGS_SAVE_THEME:
490 if ((settings[i].flags&F_THEMESETTING) == 0)
491 continue;
492 break;
493 #ifdef HAVE_RECORDING
494 case SETTINGS_SAVE_RECPRESETS:
495 if ((settings[i].flags&F_RECSETTING) == 0)
496 continue;
497 break;
498 #endif
499 #if CONFIG_CODEC == SWCODEC
500 case SETTINGS_SAVE_EQPRESET:
501 if ((settings[i].flags&F_EQSETTING) == 0)
502 continue;
503 break;
504 #endif
506 switch (settings[i].flags&F_T_MASK)
508 case F_T_CUSTOM:
509 settings[i].custom_setting->write_to_cfg(settings[i].setting,
510 value, MAX_PATH);
511 break;
512 case F_T_INT:
513 case F_T_UINT:
514 #ifdef HAVE_LCD_COLOR
515 if (settings[i].flags&F_RGB)
517 int colour = *(int*)settings[i].setting;
518 snprintf(value,MAX_PATH,"%02x%02x%02x",
519 (int)RGB_UNPACK_RED(colour),
520 (int)RGB_UNPACK_GREEN(colour),
521 (int)RGB_UNPACK_BLUE(colour));
523 else
524 #endif
525 if (settings[i].cfg_vals == NULL)
527 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
529 else
531 if (cfg_int_to_string(i, *(int*)settings[i].setting,
532 value, MAX_PATH) == false)
534 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
537 break;
538 case F_T_BOOL:
539 cfg_int_to_string(i,
540 *(bool*)settings[i].setting==false?0:1, value, MAX_PATH);
541 break;
542 case F_T_CHARPTR:
543 case F_T_UCHARPTR:
544 if (((char*)settings[i].setting)[0]
545 && settings[i].filename_setting->prefix)
547 snprintf(value,MAX_PATH,"%s%s%s",
548 settings[i].filename_setting->prefix,
549 (char*)settings[i].setting,
550 settings[i].filename_setting->suffix);
552 else strncpy(value,(char*)settings[i].setting,
553 settings[i].filename_setting->max_len);
554 break;
555 } /* switch () */
556 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
557 } /* for(...) */
558 close(fd);
559 #if CONFIG_TUNER
560 global_settings.statusbar = statusbar;
561 #endif
562 return true;
564 #ifndef HAVE_RTC_RAM
565 static bool flush_global_status_callback(void)
567 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
569 #endif
570 static bool flush_config_block_callback(void)
572 bool r1, r2;
573 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
574 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
575 return r1 || r2;
579 * persist all runtime user settings to disk or RTC RAM
581 static void update_runtime(void)
583 int elapsed_secs;
585 elapsed_secs = (current_tick - lasttime) / HZ;
586 global_status.runtime += elapsed_secs;
587 lasttime += (elapsed_secs * HZ);
589 if ( global_status.runtime > global_status.topruntime )
590 global_status.topruntime = global_status.runtime;
593 void status_save(void)
595 update_runtime();
596 #ifdef HAVE_RTC_RAM
597 /* this will be done in the storage_callback if
598 target doesnt have rtc ram */
599 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
600 #else
601 register_storage_idle_func(flush_global_status_callback);
602 #endif
605 int settings_save(void)
607 update_runtime();
608 #ifdef HAVE_RTC_RAM
609 /* this will be done in the storage_callback if
610 target doesnt have rtc ram */
611 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
612 #endif
613 register_storage_idle_func(flush_config_block_callback);
614 return 0;
617 bool settings_save_config(int options)
619 char filename[MAX_PATH];
620 char *folder;
621 switch (options)
623 case SETTINGS_SAVE_THEME:
624 folder = THEME_DIR;
625 break;
626 #ifdef HAVE_RECORDING
627 case SETTINGS_SAVE_RECPRESETS:
628 folder = RECPRESETS_DIR;
629 break;
630 #endif
631 #if CONFIG_CODEC == SWCODEC
632 case SETTINGS_SAVE_EQPRESET:
633 folder = EQS_DIR;
634 break;
635 #endif
636 case SETTINGS_SAVE_SOUND:
637 default:
638 folder = ROCKBOX_DIR;
640 create_numbered_filename(filename, folder, "config", ".cfg", 2
641 IF_CNFN_NUM_(, NULL));
643 /* allow user to modify filename */
644 while (true) {
645 if (!kbd_input(filename, sizeof filename)) {
646 break;
648 else {
649 splash(HZ, ID2P(LANG_CANCEL));
650 return false;
654 if (settings_write_config(filename, options))
655 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
656 else
657 splash(HZ, ID2P(LANG_FAILED));
658 return true;
661 /** Apply and Reset settings **/
664 #ifdef HAVE_LCD_BITMAP
666 * Applies the range infos stored in global_settings to
667 * the peak meter.
669 void settings_apply_pm_range(void)
671 int pm_min, pm_max;
673 /* depending on the scale mode (dBfs or percent) the values
674 of global_settings.peak_meter_dbfs have different meanings */
675 if (global_settings.peak_meter_dbfs)
677 /* convert to dBfs * 100 */
678 pm_min = -(((int)global_settings.peak_meter_min) * 100);
679 pm_max = -(((int)global_settings.peak_meter_max) * 100);
681 else
683 /* percent is stored directly -> no conversion */
684 pm_min = global_settings.peak_meter_min;
685 pm_max = global_settings.peak_meter_max;
688 /* apply the range */
689 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
691 #endif /* HAVE_LCD_BITMAP */
693 void sound_settings_apply(void)
695 #if CONFIG_CODEC == SWCODEC
696 sound_set_dsp_callback(dsp_callback);
697 #endif
698 sound_set(SOUND_BASS, global_settings.bass);
699 sound_set(SOUND_TREBLE, global_settings.treble);
700 sound_set(SOUND_BALANCE, global_settings.balance);
701 sound_set(SOUND_VOLUME, global_settings.volume);
702 sound_set(SOUND_CHANNELS, global_settings.channel_config);
703 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
704 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
705 sound_set(SOUND_LOUDNESS, global_settings.loudness);
706 sound_set(SOUND_AVC, global_settings.avc);
707 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
708 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
709 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
710 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
711 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
712 sound_set(SOUND_SUPERBASS, global_settings.superbass);
713 #endif
715 #ifdef HAVE_WM8758
716 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
717 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
718 #endif
720 #ifdef HAVE_USB_POWER
721 #if CONFIG_CHARGING
722 usb_charging_enable(global_settings.usb_charging);
723 #endif
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);
747 lcd_remote_set_flip(global_settings.remote_flip_display);
748 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
749 lcd_remote_scroll_step(global_settings.remote_scroll_step);
750 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
751 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
752 #ifdef HAVE_REMOTE_LCD_TICKING
753 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
754 #endif
755 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
756 #if CONFIG_CHARGING
757 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
758 #endif
759 #ifdef HAS_REMOTE_BUTTON_HOLD
760 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
761 #endif
762 #endif /* HAVE_REMOTE_LCD */
763 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
764 backlight_set_brightness(global_settings.brightness);
765 #endif
766 #ifdef HAVE_BACKLIGHT
767 backlight_set_timeout(global_settings.backlight_timeout);
768 #if CONFIG_CHARGING
769 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
770 #endif
771 #if (defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)) \
772 || defined(USE_BACKLIGHT_SW_FADING) \
773 || defined(USE_BACKLIGHT_CUSTOM_FADING_BOOL)
774 backlight_set_fade_in(global_settings.backlight_fade_in);
775 backlight_set_fade_out(global_settings.backlight_fade_out);
776 #endif
777 #endif
778 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
779 buttonlight_set_brightness(global_settings.buttonlight_brightness);
780 #endif
781 #ifdef HAVE_BUTTON_LIGHT
782 buttonlight_set_timeout(global_settings.buttonlight_timeout);
783 #endif
784 #ifdef HAVE_DISK_STORAGE
785 storage_spindown(global_settings.disk_spindown);
786 #endif
787 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
788 dac_line_in(global_settings.line_in);
789 #endif
790 set_poweroff_timeout(global_settings.poweroff);
792 set_battery_capacity(global_settings.battery_capacity);
793 #if BATTERY_TYPES_COUNT > 1
794 set_battery_type(global_settings.battery_type);
795 #endif
797 #ifdef HAVE_LCD_BITMAP
798 lcd_set_invert_display(global_settings.invert);
799 lcd_set_flip(global_settings.flip_display);
800 button_set_flip(global_settings.flip_display);
801 lcd_update(); /* refresh after flipping the screen */
802 settings_apply_pm_range();
803 peak_meter_init_times(
804 global_settings.peak_meter_release, global_settings.peak_meter_hold,
805 global_settings.peak_meter_clip_hold);
806 #endif
808 if (read_disk)
811 #ifdef HAVE_LCD_BITMAP
812 /* fonts need to be loaded before the WPS */
813 if ( global_settings.font_file[0]) {
814 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
815 global_settings.font_file);
816 font_load(buf);
818 else
819 font_reset();
821 if ( global_settings.kbd_file[0]) {
822 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
823 global_settings.kbd_file);
824 load_kbd(buf);
826 else
827 load_kbd(NULL);
828 #endif
829 #if LCD_DEPTH > 1
830 unload_wps_backdrop();
831 #endif
832 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
833 unload_remote_wps_backdrop();
834 #endif
835 if ( global_settings.wps_file[0] &&
836 global_settings.wps_file[0] != 0xff ) {
837 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
838 global_settings.wps_file);
839 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
841 else
843 wps_data_init(gui_wps[0].data);
844 #ifdef HAVE_REMOTE_LCD
845 gui_wps[0].data->remote_wps = false;
846 #endif
849 #if LCD_DEPTH > 1
850 if ( global_settings.backdrop_file[0] &&
851 global_settings.backdrop_file[0] != 0xff ) {
852 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
853 global_settings.backdrop_file);
854 load_main_backdrop(buf);
855 } else {
856 unload_main_backdrop();
858 show_main_backdrop();
859 #endif
860 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
861 show_remote_main_backdrop();
862 #endif
864 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
865 if ( global_settings.rwps_file[0]) {
866 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
867 global_settings.rwps_file);
868 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
870 else
872 wps_data_init(gui_wps[1].data);
873 gui_wps[1].data->remote_wps = true;
875 #endif
876 if ( global_settings.lang_file[0]) {
877 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
878 global_settings.lang_file);
879 lang_load(buf);
880 talk_init(); /* use voice of same language */
882 /* load the icon set */
883 icons_init();
885 #ifdef HAVE_LCD_COLOR
886 if (global_settings.colors_file[0])
887 read_color_theme_file();
888 #endif
891 #ifdef HAVE_LCD_COLOR
892 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
893 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
894 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
895 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
896 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
897 #endif
899 #ifdef HAVE_LCD_BITMAP
900 lcd_scroll_step(global_settings.scroll_step);
901 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
902 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
903 #else
904 lcd_jump_scroll(global_settings.jump_scroll);
905 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
906 #endif
907 lcd_bidir_scroll(global_settings.bidir_limit);
908 lcd_scroll_delay(global_settings.scroll_delay);
911 set_codepage(global_settings.default_codepage);
913 #if CONFIG_CODEC == SWCODEC
914 audio_set_crossfade(global_settings.crossfade);
915 dsp_set_replaygain();
916 dsp_set_crossfeed(global_settings.crossfeed);
917 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
918 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
919 global_settings.crossfeed_hf_attenuation,
920 global_settings.crossfeed_hf_cutoff);
922 /* Configure software equalizer, hardware eq is handled in audio_init() */
923 dsp_set_eq(global_settings.eq_enabled);
924 dsp_set_eq_precut(global_settings.eq_precut);
925 for(i = 0; i < 5; i++) {
926 dsp_set_eq_coefs(i);
929 dsp_dither_enable(global_settings.dithering_enabled);
930 #endif
932 #ifdef HAVE_SPDIF_POWER
933 spdif_power_enable(global_settings.spdif_enable);
934 #endif
936 #ifdef HAVE_BACKLIGHT
937 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
938 #ifdef HAVE_REMOTE_LCD
939 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
940 #endif
941 #ifdef HAS_BUTTON_HOLD
942 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
943 #endif
944 #ifdef HAVE_LCD_SLEEP_SETTING
945 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
946 #endif
947 #endif /* HAVE_BACKLIGHT */
949 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
950 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
951 #endif
953 /* This should stay last */
954 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
955 enc_global_settings_apply();
956 #endif
957 list_init_viewports(NULL);
962 * reset all settings to their default value
964 void reset_setting(const struct settings_list *setting, void *var)
966 switch (setting->flags&F_T_MASK)
968 case F_T_CUSTOM:
969 setting->custom_setting->set_default(setting->setting,
970 setting->default_val.custom);
971 break;
972 case F_T_INT:
973 case F_T_UINT:
974 if (setting->flags&F_DEF_ISFUNC)
975 *(int*)var = setting->default_val.func();
976 else if (setting->flags&F_T_SOUND)
977 *(int*)var = sound_default(setting->sound_setting->setting);
978 else *(int*)var = setting->default_val.int_;
979 break;
980 case F_T_BOOL:
981 *(bool*)var = setting->default_val.bool_;
982 break;
983 case F_T_CHARPTR:
984 case F_T_UCHARPTR:
985 strncpy((char*)var, setting->default_val.charptr,
986 setting->filename_setting->max_len);
987 break;
991 void settings_reset(void)
993 int i;
995 for(i=0; i<nb_settings; i++)
996 reset_setting(&settings[i], settings[i].setting);
997 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
998 enc_global_settings_reset();
999 #endif
1002 /** Changing setting values **/
1003 const struct settings_list* find_setting(const void* variable, int *id)
1005 int i;
1006 for(i=0;i<nb_settings;i++)
1008 if (settings[i].setting == variable)
1010 if (id)
1011 *id = i;
1012 return &settings[i];
1015 return NULL;
1018 bool set_bool(const char* string, const bool* variable )
1020 return set_bool_options(string, variable,
1021 (char *)STR(LANG_SET_BOOL_YES),
1022 (char *)STR(LANG_SET_BOOL_NO),
1023 NULL);
1027 bool set_bool_options(const char* string, const bool* variable,
1028 const char* yes_str, int yes_voice,
1029 const char* no_str, int no_voice,
1030 void (*function)(bool))
1032 struct opt_items names[] = {
1033 {(unsigned const char *)no_str, no_voice},
1034 {(unsigned const char *)yes_str, yes_voice}
1036 bool result;
1038 result = set_option(string, variable, BOOL, names, 2,
1039 (void (*)(int))function);
1040 return result;
1043 bool set_int(const unsigned char* string,
1044 const char* unit,
1045 int voice_unit,
1046 const int* variable,
1047 void (*function)(int),
1048 int step,
1049 int min,
1050 int max,
1051 void (*formatter)(char*, size_t, int, const char*) )
1053 return set_int_ex(string, unit, voice_unit, variable, function,
1054 step, min, max, formatter, NULL);
1057 bool set_int_ex(const unsigned char* string,
1058 const char* unit,
1059 int voice_unit,
1060 const int* variable,
1061 void (*function)(int),
1062 int step,
1063 int min,
1064 int max,
1065 void (*formatter)(char*, size_t, int, const char*),
1066 int32_t (*get_talk_id)(int, int))
1068 (void)unit;
1069 struct settings_list item;
1070 struct int_setting data = {
1071 function, voice_unit, min, max, step,
1072 formatter, get_talk_id
1074 item.int_setting = &data;
1075 item.flags = F_INT_SETTING|F_T_INT;
1076 item.lang_id = -1;
1077 item.cfg_vals = (char*)string;
1078 item.setting = (void *)variable;
1079 return option_screen(&item, NULL, false, NULL);
1083 static const struct opt_items *set_option_options;
1084 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1086 (void)unit;
1087 const unsigned char *text = set_option_options[item].string;
1088 strncpy(buf, P2STR(text), size);
1090 static int32_t set_option_get_talk_id(int value, int unit)
1092 (void)unit;
1093 return set_option_options[value].voice_id;
1095 bool set_option(const char* string, const void* variable, enum optiontype type,
1096 const struct opt_items* options,
1097 int numoptions, void (*function)(int))
1099 int temp;
1100 struct settings_list item;
1101 struct int_setting data = {
1102 function, UNIT_INT, 0, numoptions-1, 1,
1103 set_option_formatter, set_option_get_talk_id
1105 set_option_options = options;
1106 item.int_setting = &data;
1107 item.flags = F_INT_SETTING|F_T_INT;
1108 item.lang_id = -1;
1109 item.cfg_vals = (char*)string;
1110 item.setting = &temp;
1111 if (type == BOOL)
1112 temp = *(bool*)variable? 1: 0;
1113 else
1114 temp = *(int*)variable;
1115 if (!option_screen(&item, NULL, false, NULL))
1117 if (type == BOOL)
1118 *(bool*)variable = (temp == 1? true: false);
1119 else
1120 *(int*)variable = temp;
1121 return false;
1123 return true;
1127 void set_file(const char* filename, char* setting, int maxlen)
1129 char* fptr = strrchr(filename,'/');
1130 int len;
1131 int extlen = 0;
1132 char* ptr;
1134 if (!fptr)
1135 return;
1137 *fptr = 0;
1138 fptr++;
1140 len = strlen(fptr);
1141 ptr = fptr + len;
1142 while ((*ptr != '.') && (ptr != fptr)) {
1143 extlen++;
1144 ptr--;
1146 if(ptr == fptr) extlen = 0;
1148 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1149 (len-extlen > maxlen))
1150 return;
1152 strncpy(setting, fptr, len-extlen);
1153 setting[len-extlen]=0;
1155 settings_save();