Move declaration of button_int and clickwheel_int to the proper header file instead...
[Rockbox.git] / apps / settings.c
blob4c181593db3076e2be1c2a349a526cf318cde7a8
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 "screens.h"
42 #include "ctype.h"
43 #include "file.h"
44 #include "system.h"
45 #include "misc.h"
46 #ifdef HAVE_LCD_BITMAP
47 #include "icons.h"
48 #include "font.h"
49 #include "peakmeter.h"
50 #endif
51 #include "lang.h"
52 #include "language.h"
53 #include "gwps.h"
54 #include "powermgmt.h"
55 #include "sprintf.h"
56 #include "keyboard.h"
57 #include "version.h"
58 #include "sound.h"
59 #include "rbunicode.h"
60 #include "dircache.h"
61 #include "statusbar.h"
62 #include "splash.h"
63 #include "list.h"
64 #include "settings_list.h"
65 #include "filetypes.h"
66 #include "option_select.h"
67 #include "backdrop.h"
69 #if CONFIG_TUNER
70 #include "radio.h"
71 #endif
73 #if CONFIG_CODEC == MAS3507D
74 void dac_line_in(bool enable);
75 #endif
76 struct user_settings global_settings;
77 struct system_status global_status;
79 #if CONFIG_CODEC == SWCODEC
80 #include "dsp.h"
81 #include "playback.h"
82 #ifdef HAVE_RECORDING
83 #include "enc_config.h"
84 #endif
85 #endif /* CONFIG_CODEC == SWCODEC */
87 #define NVRAM_BLOCK_SIZE 44
89 #ifdef HAVE_LCD_BITMAP
90 #define MAX_LINES 10
91 #else
92 #define MAX_LINES 2
93 #endif
95 #ifdef HAVE_REMOTE_LCD
96 #include "lcd-remote.h"
97 #endif
99 long lasttime = 0;
101 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
102 /* NVRAM is set out as
103 [0] 'R'
104 [1] 'b'
105 [2] version
106 [3] stored variable count
107 [4-7] crc32 checksum
108 [8-NVRAM_BLOCK_SIZE] data
110 #define NVRAM_DATA_START 8
111 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
112 static char nvram_buffer[NVRAM_BLOCK_SIZE];
114 static bool read_nvram_data(char* buf, int max_len)
116 unsigned crc32 = 0xffffffff;
117 int var_count = 0, i = 0, buf_pos = 0;
118 #ifndef HAVE_RTC_RAM
119 int fd = open(NVRAM_FILE,O_RDONLY);
120 if (fd < 0)
121 return false;
122 memset(buf,0,max_len);
123 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
124 return false;
125 close(fd);
126 #else
127 memset(buf,0,max_len);
128 /* read rtc block */
129 for (i=0; i < max_len; i++ )
130 buf[i] = rtc_read(0x14+i);
131 #endif
132 /* check magic, version */
133 if ((buf[0] != 'R') || (buf[1] != 'b')
134 || (buf[2] != NVRAM_CONFIG_VERSION))
135 return false;
136 /* check crc32 */
137 crc32 = crc_32(&buf[NVRAM_DATA_START],
138 max_len-NVRAM_DATA_START-1,0xffffffff);
139 if (memcmp(&crc32,&buf[4],4))
140 return false;
141 /* all good, so read in the settings */
142 var_count = buf[3];
143 buf_pos = NVRAM_DATA_START;
144 for(i=0; i<nb_settings; i++)
146 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
147 >>F_NVRAM_MASK_SHIFT;
148 if (nvram_bytes)
150 if ((var_count>0) && (buf_pos<max_len))
152 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
153 buf_pos += nvram_bytes;
154 var_count--;
156 else /* should only happen when new items are added to the end */
158 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
162 return true;
164 static bool write_nvram_data(char* buf, int max_len)
166 unsigned crc32 = 0xffffffff;
167 int i = 0, buf_pos = 0;
168 char var_count = 0;
169 #ifndef HAVE_RTC_RAM
170 int fd;
171 #endif
172 memset(buf,0,max_len);
173 /* magic, version */
174 buf[0] = 'R'; buf[1] = 'b';
175 buf[2] = NVRAM_CONFIG_VERSION;
176 buf_pos = NVRAM_DATA_START;
177 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
179 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
180 >>F_NVRAM_MASK_SHIFT;
181 if (nvram_bytes)
183 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
184 buf_pos += nvram_bytes;
185 var_count++;
188 /* count and crc32 */
189 buf[3] = var_count;
190 crc32 = crc_32(&buf[NVRAM_DATA_START],
191 max_len-NVRAM_DATA_START-1,0xffffffff);
192 memcpy(&buf[4],&crc32,4);
193 #ifndef HAVE_RTC_RAM
194 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
195 if (fd >= 0)
197 int len = write(fd,buf,max_len);
198 close(fd);
199 if (len < 8)
200 return false;
202 #else
203 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
204 that it would write a number of bytes at a time since the RTC chip
205 supports that, but this will have to do for now 8-) */
206 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
207 int r = rtc_write(0x14+i, buf[i]);
208 if (r)
209 return false;
211 #endif
212 return true;
215 /** Reading from a config file **/
217 * load settings from disk or RTC RAM
219 void settings_load(int which)
221 if (which&SETTINGS_RTC)
222 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
223 if (which&SETTINGS_HD)
225 settings_load_config(CONFIGFILE,false);
226 settings_load_config(FIXEDSETTINGSFILE,false);
230 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
232 const char* start = settings[setting_id].cfg_vals;
233 char* end = NULL;
234 char temp[MAX_PATH];
235 int count = 0;
236 while (1)
238 end = strchr(start, ',');
239 if (!end)
241 if (!strcmp(str, start))
243 *out = count;
244 return true;
246 else return false;
248 strncpy(temp, start, end-start);
249 temp[end-start] = '\0';
250 if (!strcmp(str, temp))
252 *out = count;
253 return true;
255 start = end +1;
256 count++;
258 return false;
261 bool settings_load_config(const char* file, bool apply)
263 int fd;
264 char line[128];
265 char* name;
266 char* value;
267 int i;
268 fd = open(file, O_RDONLY);
269 if (fd < 0)
270 return false;
272 while (read_line(fd, line, sizeof line) > 0)
274 if (!settings_parseline(line, &name, &value))
275 continue;
276 for(i=0; i<nb_settings; i++)
278 if (settings[i].cfg_name == NULL)
279 continue;
280 if (!strcasecmp(name,settings[i].cfg_name))
282 switch (settings[i].flags&F_T_MASK)
284 case F_T_INT:
285 case F_T_UINT:
286 #ifdef HAVE_LCD_COLOR
287 if (settings[i].flags&F_RGB)
288 hex_to_rgb(value, (int*)settings[i].setting);
289 else
290 #endif
291 if (settings[i].cfg_vals == NULL)
293 *(int*)settings[i].setting = atoi(value);
295 else
297 int temp, *v = (int*)settings[i].setting;
298 bool found = cfg_string_to_int(i, &temp, value);
299 if (found)
301 if (settings[i].flags&F_TABLE_SETTING)
302 *v = settings[i].table_setting->values[temp];
303 else
304 *v = temp;
306 else
307 *v = atoi(value);
309 break;
310 case F_T_BOOL:
312 int temp;
313 if (cfg_string_to_int(i,&temp,value))
314 *(bool*)settings[i].setting = (temp==0?false:true);
315 break;
317 case F_T_CHARPTR:
318 case F_T_UCHARPTR:
320 char storage[MAX_PATH];
321 if (settings[i].filename_setting->prefix)
323 int len = strlen(settings[i].filename_setting->prefix);
324 if (!strncasecmp(value,
325 settings[i].filename_setting->prefix,
326 len))
328 strncpy(storage,&value[len],MAX_PATH);
330 else strncpy(storage,value,MAX_PATH);
332 else strncpy(storage,value,MAX_PATH);
333 if (settings[i].filename_setting->suffix)
335 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
336 if (s) *s = '\0';
338 strncpy((char*)settings[i].setting,storage,
339 settings[i].filename_setting->max_len);
340 ((char*)settings[i].setting)
341 [settings[i].filename_setting->max_len-1] = '\0';
342 break;
345 break;
346 } /* if (!strcmp(name,settings[i].cfg_name)) */
347 } /* for(...) */
348 } /* while(...) */
350 close(fd);
351 settings_save();
352 if (apply)
353 settings_apply(true);
354 return true;
357 /** Writing to a config file and saving settings **/
359 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
361 int flags = settings[setting_id].flags;
362 const char* start = settings[setting_id].cfg_vals;
363 char* end = NULL;
364 int count = 0;
366 if ((flags&F_T_MASK)==F_T_INT &&
367 flags&F_TABLE_SETTING)
369 const int *value = settings[setting_id].table_setting->values;
370 while (start)
372 end = strchr(start,',');
373 if (value[count] == val)
375 if (end == NULL)
376 strncpy(buf, start, buf_len);
377 else
379 int len = (buf_len > (end-start))? end-start: buf_len;
380 strncpy(buf, start, len);
381 buf[len] = '\0';
383 return true;
385 count++;
387 if (end)
388 start = end+1;
389 else
390 break;
392 return false;
395 while (count < val)
397 start = strchr(start,',');
398 if (!start)
399 return false;
400 count++;
401 start++;
403 end = strchr(start,',');
404 if (end == NULL)
405 strncpy(buf, start, buf_len);
406 else
408 int len = (buf_len > (end-start))? end-start: buf_len;
409 strncpy(buf, start, len);
410 buf[len] = '\0';
412 return true;
416 static bool is_changed(int setting_id)
418 const struct settings_list *setting = &settings[setting_id];
419 switch (setting->flags&F_T_MASK)
421 case F_T_INT:
422 case F_T_UINT:
423 if (setting->flags&F_DEF_ISFUNC)
425 if (*(int*)setting->setting == setting->default_val.func())
426 return false;
428 else if (setting->flags&F_T_SOUND)
430 if (*(int*)setting->setting ==
431 sound_default(setting->sound_setting->setting))
432 return false;
434 else if (*(int*)setting->setting == setting->default_val.int_)
435 return false;
436 break;
437 case F_T_BOOL:
438 if (*(bool*)setting->setting == setting->default_val.bool_)
439 return false;
440 break;
441 case F_T_CHARPTR:
442 case F_T_UCHARPTR:
443 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
444 return false;
445 break;
447 return true;
450 static bool settings_write_config(const char* filename, int options)
452 int i;
453 int fd;
454 char value[MAX_PATH];
455 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
456 if (fd < 0)
457 return false;
458 #if CONFIG_TUNER
459 bool statusbar = global_settings.statusbar;
460 if (global_status.statusbar_forced != 0 && statusbar)
461 global_settings.statusbar = false;
462 #endif
463 fdprintf(fd, "# .cfg file created by rockbox %s - "
464 "http://www.rockbox.org\r\n\r\n", appsversion);
465 for(i=0; i<nb_settings; i++)
467 if (settings[i].cfg_name == NULL)
468 continue;
469 value[0] = '\0';
471 switch (options)
473 case SETTINGS_SAVE_CHANGED:
474 if (!is_changed(i))
475 continue;
476 break;
477 case SETTINGS_SAVE_SOUND:
478 if ((settings[i].flags&F_SOUNDSETTING) == 0)
479 continue;
480 break;
481 case SETTINGS_SAVE_THEME:
482 if ((settings[i].flags&F_THEMESETTING) == 0)
483 continue;
484 break;
485 #ifdef HAVE_RECORDING
486 case SETTINGS_SAVE_RECPRESETS:
487 if ((settings[i].flags&F_RECSETTING) == 0)
488 continue;
489 break;
490 #endif
491 #if CONFIG_CODEC == SWCODEC
492 case SETTINGS_SAVE_EQPRESET:
493 if ((settings[i].flags&F_EQSETTING) == 0)
494 continue;
495 break;
496 #endif
498 switch (settings[i].flags&F_T_MASK)
500 case F_T_INT:
501 case F_T_UINT:
502 #ifdef HAVE_LCD_COLOR
503 if (settings[i].flags&F_RGB)
505 int colour = *(int*)settings[i].setting;
506 snprintf(value,MAX_PATH,"%02x%02x%02x",
507 (int)RGB_UNPACK_RED(colour),
508 (int)RGB_UNPACK_GREEN(colour),
509 (int)RGB_UNPACK_BLUE(colour));
511 else
512 #endif
513 if (settings[i].cfg_vals == NULL)
515 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
517 else
519 if (cfg_int_to_string(i, *(int*)settings[i].setting,
520 value, MAX_PATH) == false)
522 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
525 break;
526 case F_T_BOOL:
527 cfg_int_to_string(i,
528 *(bool*)settings[i].setting==false?0:1, value, MAX_PATH);
529 break;
530 case F_T_CHARPTR:
531 case F_T_UCHARPTR:
532 if (((char*)settings[i].setting)[0]
533 && settings[i].filename_setting->prefix)
535 snprintf(value,MAX_PATH,"%s%s%s",
536 settings[i].filename_setting->prefix,
537 (char*)settings[i].setting,
538 settings[i].filename_setting->suffix);
540 else strncpy(value,(char*)settings[i].setting,
541 settings[i].filename_setting->max_len);
542 break;
543 } /* switch () */
544 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
545 } /* for(...) */
546 close(fd);
547 #if CONFIG_TUNER
548 global_settings.statusbar = statusbar;
549 #endif
550 return true;
552 #ifndef HAVE_RTC_RAM
553 static bool flush_global_status_callback(void)
555 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
557 #endif
558 static bool flush_config_block_callback(void)
560 bool r1, r2;
561 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
562 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
563 return r1 || r2;
567 * persist all runtime user settings to disk or RTC RAM
569 static void update_runtime(void)
571 int elapsed_secs;
573 elapsed_secs = (current_tick - lasttime) / HZ;
574 global_status.runtime += elapsed_secs;
575 lasttime += (elapsed_secs * HZ);
577 if ( global_status.runtime > global_status.topruntime )
578 global_status.topruntime = global_status.runtime;
581 void status_save(void)
583 update_runtime();
584 #ifdef HAVE_RTC_RAM
585 /* this will be done in the ata_callback if
586 target doesnt have rtc ram */
587 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
588 #else
589 register_ata_idle_func(flush_global_status_callback);
590 #endif
593 int settings_save(void)
595 update_runtime();
596 #ifdef HAVE_RTC_RAM
597 /* this will be done in the ata_callback if
598 target doesnt have rtc ram */
599 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
600 #endif
601 register_ata_idle_func(flush_config_block_callback);
602 return 0;
605 bool settings_save_config(int options)
607 char filename[MAX_PATH];
608 char *folder;
609 switch (options)
611 case SETTINGS_SAVE_THEME:
612 folder = THEME_DIR;
613 break;
614 #ifdef HAVE_RECORDING
615 case SETTINGS_SAVE_RECPRESETS:
616 folder = RECPRESETS_DIR;
617 break;
618 #endif
619 #if CONFIG_CODEC == SWCODEC
620 case SETTINGS_SAVE_EQPRESET:
621 folder = EQS_DIR;
622 break;
623 #endif
624 case SETTINGS_SAVE_SOUND:
625 default:
626 folder = ROCKBOX_DIR;
628 create_numbered_filename(filename, folder, "config", ".cfg", 2
629 IF_CNFN_NUM_(, NULL));
631 /* allow user to modify filename */
632 while (true) {
633 if (!kbd_input(filename, sizeof filename)) {
634 break;
636 else {
637 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
638 return false;
642 if (settings_write_config(filename, options))
643 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_SAVED));
644 else
645 gui_syncsplash(HZ, ID2P(LANG_FAILED));
646 return true;
649 /** Apply and Reset settings **/
652 #ifdef HAVE_LCD_BITMAP
654 * Applies the range infos stored in global_settings to
655 * the peak meter.
657 void settings_apply_pm_range(void)
659 int pm_min, pm_max;
661 /* depending on the scale mode (dBfs or percent) the values
662 of global_settings.peak_meter_dbfs have different meanings */
663 if (global_settings.peak_meter_dbfs)
665 /* convert to dBfs * 100 */
666 pm_min = -(((int)global_settings.peak_meter_min) * 100);
667 pm_max = -(((int)global_settings.peak_meter_max) * 100);
669 else
671 /* percent is stored directly -> no conversion */
672 pm_min = global_settings.peak_meter_min;
673 pm_max = global_settings.peak_meter_max;
676 /* apply the range */
677 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
679 #endif /* HAVE_LCD_BITMAP */
681 void sound_settings_apply(void)
683 #if CONFIG_CODEC == SWCODEC
684 sound_set_dsp_callback(dsp_callback);
685 #endif
686 sound_set(SOUND_BASS, global_settings.bass);
687 sound_set(SOUND_TREBLE, global_settings.treble);
688 sound_set(SOUND_BALANCE, global_settings.balance);
689 sound_set(SOUND_VOLUME, global_settings.volume);
690 sound_set(SOUND_CHANNELS, global_settings.channel_config);
691 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
692 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
693 sound_set(SOUND_LOUDNESS, global_settings.loudness);
694 sound_set(SOUND_AVC, global_settings.avc);
695 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
696 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
697 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
698 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
699 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
700 sound_set(SOUND_SUPERBASS, global_settings.superbass);
701 #endif
703 #ifdef HAVE_WM8758
704 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
705 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
706 #endif
708 #ifdef HAVE_USB_POWER
709 #if CONFIG_CHARGING
710 usb_charging_enable(global_settings.usb_charging);
711 #endif
712 #endif
715 void settings_apply(bool read_disk)
717 char buf[64];
718 #if CONFIG_CODEC == SWCODEC
719 int i;
720 #endif
722 sound_settings_apply();
724 #ifndef HAVE_FLASH_STORAGE
725 audio_set_buffer_margin(global_settings.buffer_margin);
726 #endif
728 #ifdef HAVE_LCD_CONTRAST
729 lcd_set_contrast(global_settings.contrast);
730 #endif
731 lcd_scroll_speed(global_settings.scroll_speed);
732 #ifdef HAVE_REMOTE_LCD
733 lcd_remote_set_contrast(global_settings.remote_contrast);
734 lcd_remote_set_invert_display(global_settings.remote_invert);
735 lcd_remote_set_flip(global_settings.remote_flip_display);
736 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
737 lcd_remote_scroll_step(global_settings.remote_scroll_step);
738 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
739 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
740 #ifdef HAVE_REMOTE_LCD_TICKING
741 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
742 #endif
743 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
744 #if CONFIG_CHARGING
745 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
746 #endif
747 #ifdef HAS_REMOTE_BUTTON_HOLD
748 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
749 #endif
750 #endif /* HAVE_REMOTE_LCD */
751 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
752 backlight_set_brightness(global_settings.brightness);
753 #endif
754 #ifdef HAVE_BACKLIGHT
755 backlight_set_timeout(global_settings.backlight_timeout);
756 #if CONFIG_CHARGING
757 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
758 #endif
759 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
760 backlight_set_fade_in(global_settings.backlight_fade_in);
761 backlight_set_fade_out(global_settings.backlight_fade_out);
762 #endif
763 #endif
764 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
765 buttonlight_set_brightness(global_settings.buttonlight_brightness);
766 #endif
767 #ifdef HAVE_BUTTON_LIGHT
768 buttonlight_set_timeout(global_settings.buttonlight_timeout);
769 #endif
770 #ifndef HAVE_FLASH_STORAGE
771 ata_spindown(global_settings.disk_spindown);
772 #endif
773 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
774 dac_line_in(global_settings.line_in);
775 #endif
776 set_poweroff_timeout(global_settings.poweroff);
778 set_battery_capacity(global_settings.battery_capacity);
779 #if BATTERY_TYPES_COUNT > 1
780 set_battery_type(global_settings.battery_type);
781 #endif
783 #ifdef HAVE_LCD_BITMAP
784 lcd_set_invert_display(global_settings.invert);
785 lcd_set_flip(global_settings.flip_display);
786 button_set_flip(global_settings.flip_display);
787 lcd_update(); /* refresh after flipping the screen */
788 settings_apply_pm_range();
789 peak_meter_init_times(
790 global_settings.peak_meter_release, global_settings.peak_meter_hold,
791 global_settings.peak_meter_clip_hold);
792 #endif
794 if (read_disk)
797 #ifdef HAVE_LCD_BITMAP
798 /* fonts need to be loaded before the WPS */
799 if ( global_settings.font_file[0]) {
800 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
801 global_settings.font_file);
802 font_load(buf);
804 else
805 font_reset();
807 if ( global_settings.kbd_file[0]) {
808 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
809 global_settings.kbd_file);
810 load_kbd(buf);
812 else
813 load_kbd(NULL);
814 #endif
815 #if LCD_DEPTH > 1
816 unload_wps_backdrop();
817 #endif
818 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
819 unload_remote_wps_backdrop();
820 #endif
821 if ( global_settings.wps_file[0] &&
822 global_settings.wps_file[0] != 0xff ) {
823 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
824 global_settings.wps_file);
825 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
827 else
829 wps_data_init(gui_wps[0].data);
830 #ifdef HAVE_REMOTE_LCD
831 gui_wps[0].data->remote_wps = false;
832 #endif
835 #if LCD_DEPTH > 1
836 if ( global_settings.backdrop_file[0] &&
837 global_settings.backdrop_file[0] != 0xff ) {
838 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
839 global_settings.backdrop_file);
840 load_main_backdrop(buf);
841 } else {
842 unload_main_backdrop();
844 show_main_backdrop();
845 #endif
846 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
847 show_remote_main_backdrop();
848 #endif
850 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
851 if ( global_settings.rwps_file[0]) {
852 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
853 global_settings.rwps_file);
854 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
856 else
858 wps_data_init(gui_wps[1].data);
859 gui_wps[1].data->remote_wps = true;
861 #endif
862 if ( global_settings.lang_file[0]) {
863 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
864 global_settings.lang_file);
865 lang_load(buf);
866 talk_init(); /* use voice of same language */
868 /* load the icon set */
869 icons_init();
871 #ifdef HAVE_LCD_COLOR
872 if (global_settings.colors_file[0])
873 read_color_theme_file();
874 #endif
877 #ifdef HAVE_LCD_COLOR
878 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
879 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
880 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
881 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
882 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
883 #endif
885 #ifdef HAVE_LCD_BITMAP
886 lcd_scroll_step(global_settings.scroll_step);
887 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
888 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
889 #else
890 lcd_jump_scroll(global_settings.jump_scroll);
891 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
892 #endif
893 lcd_bidir_scroll(global_settings.bidir_limit);
894 lcd_scroll_delay(global_settings.scroll_delay);
897 set_codepage(global_settings.default_codepage);
899 #if CONFIG_CODEC == SWCODEC
900 audio_set_crossfade(global_settings.crossfade);
901 dsp_set_replaygain();
902 dsp_set_crossfeed(global_settings.crossfeed);
903 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
904 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
905 global_settings.crossfeed_hf_attenuation,
906 global_settings.crossfeed_hf_cutoff);
908 /* Configure software equalizer, hardware eq is handled in audio_init() */
909 dsp_set_eq(global_settings.eq_enabled);
910 dsp_set_eq_precut(global_settings.eq_precut);
911 for(i = 0; i < 5; i++) {
912 dsp_set_eq_coefs(i);
915 dsp_dither_enable(global_settings.dithering_enabled);
916 #endif
918 #ifdef HAVE_SPDIF_POWER
919 spdif_power_enable(global_settings.spdif_enable);
920 #endif
922 #ifdef HAVE_BACKLIGHT
923 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
924 #ifdef HAVE_REMOTE_LCD
925 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
926 #endif
927 #ifdef HAS_BUTTON_HOLD
928 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
929 #endif
930 #ifdef HAVE_LCD_SLEEP_SETTING
931 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
932 #endif
933 #endif /* HAVE_BACKLIGHT */
935 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
936 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
937 #endif
939 /* This should stay last */
940 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
941 enc_global_settings_apply();
942 #endif
943 list_init_viewports(NULL);
948 * reset all settings to their default value
950 void reset_setting(const struct settings_list *setting, void *var)
952 switch (setting->flags&F_T_MASK)
954 case F_T_INT:
955 case F_T_UINT:
956 if (setting->flags&F_DEF_ISFUNC)
957 *(int*)var = setting->default_val.func();
958 else if (setting->flags&F_T_SOUND)
959 *(int*)var = sound_default(setting->sound_setting->setting);
960 else *(int*)var = setting->default_val.int_;
961 break;
962 case F_T_BOOL:
963 *(bool*)var = setting->default_val.bool_;
964 break;
965 case F_T_CHARPTR:
966 case F_T_UCHARPTR:
967 strncpy((char*)var, setting->default_val.charptr,
968 setting->filename_setting->max_len);
969 break;
973 void settings_reset(void)
975 int i;
977 for(i=0; i<nb_settings; i++)
978 reset_setting(&settings[i], settings[i].setting);
979 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
980 enc_global_settings_reset();
981 #endif
984 /** Changing setting values **/
985 const struct settings_list* find_setting(const void* variable, int *id)
987 int i;
988 for(i=0;i<nb_settings;i++)
990 if (settings[i].setting == variable)
992 if (id)
993 *id = i;
994 return &settings[i];
997 return NULL;
1000 bool set_bool(const char* string, const bool* variable )
1002 return set_bool_options(string, variable,
1003 (char *)STR(LANG_SET_BOOL_YES),
1004 (char *)STR(LANG_SET_BOOL_NO),
1005 NULL);
1009 bool set_bool_options(const char* string, const bool* variable,
1010 const char* yes_str, int yes_voice,
1011 const char* no_str, int no_voice,
1012 void (*function)(bool))
1014 struct opt_items names[] = {
1015 {(unsigned const char *)no_str, no_voice},
1016 {(unsigned const char *)yes_str, yes_voice}
1018 bool result;
1020 result = set_option(string, variable, BOOL, names, 2,
1021 (void (*)(int))function);
1022 return result;
1025 bool set_int(const unsigned char* string,
1026 const char* unit,
1027 int voice_unit,
1028 const int* variable,
1029 void (*function)(int),
1030 int step,
1031 int min,
1032 int max,
1033 void (*formatter)(char*, size_t, int, const char*) )
1035 return set_int_ex(string, unit, voice_unit, variable, function,
1036 step, min, max, formatter, NULL);
1039 bool set_int_ex(const unsigned char* string,
1040 const char* unit,
1041 int voice_unit,
1042 const int* variable,
1043 void (*function)(int),
1044 int step,
1045 int min,
1046 int max,
1047 void (*formatter)(char*, size_t, int, const char*),
1048 int32_t (*get_talk_id)(int, int))
1050 (void)unit;
1051 struct settings_list item;
1052 struct int_setting data = {
1053 function, voice_unit, min, max, step,
1054 formatter, get_talk_id
1056 item.int_setting = &data;
1057 item.flags = F_INT_SETTING|F_T_INT;
1058 item.lang_id = -1;
1059 item.cfg_vals = (char*)string;
1060 item.setting = (void *)variable;
1061 return option_screen(&item, NULL, false, NULL);
1065 static const struct opt_items *set_option_options;
1066 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1068 (void)unit;
1069 const unsigned char *text = set_option_options[item].string;
1070 snprintf(buf, size, "%s", P2STR(text));
1072 static int32_t set_option_get_talk_id(int value, int unit)
1074 (void)unit;
1075 return set_option_options[value].voice_id;
1077 bool set_option(const char* string, const void* variable, enum optiontype type,
1078 const struct opt_items* options,
1079 int numoptions, void (*function)(int))
1081 int temp;
1082 struct settings_list item;
1083 struct int_setting data = {
1084 function, UNIT_INT, 0, numoptions-1, 1,
1085 set_option_formatter, set_option_get_talk_id
1087 set_option_options = options;
1088 item.int_setting = &data;
1089 item.flags = F_INT_SETTING|F_T_INT;
1090 item.lang_id = -1;
1091 item.cfg_vals = (char*)string;
1092 item.setting = &temp;
1093 if (type == BOOL)
1094 temp = *(bool*)variable? 1: 0;
1095 else
1096 temp = *(int*)variable;
1097 if (!option_screen(&item, NULL, false, NULL))
1099 if (type == BOOL)
1100 *(bool*)variable = (temp == 1? true: false);
1101 else
1102 *(int*)variable = temp;
1103 return false;
1105 return true;
1109 void set_file(const char* filename, char* setting, int maxlen)
1111 char* fptr = strrchr(filename,'/');
1112 int len;
1113 int extlen = 0;
1114 char* ptr;
1116 if (!fptr)
1117 return;
1119 *fptr = 0;
1120 fptr++;
1122 len = strlen(fptr);
1123 ptr = fptr + len;
1124 while ((*ptr != '.') && (ptr != fptr)) {
1125 extlen++;
1126 ptr--;
1128 if(ptr == fptr) extlen = 0;
1130 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1131 (len-extlen > maxlen))
1132 return;
1134 strncpy(setting, fptr, len-extlen);
1135 setting[len-extlen]=0;
1137 settings_save();