Add the viewport functions to the screens API, including a new getfont() function...
[Rockbox.git] / apps / settings.c
blob2814a6f57a3026e2e2620e3d4801f576ddb16aca
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by wavey@wavey.org
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include "inttypes.h"
25 #include "config.h"
26 #include "action.h"
27 #include "crc32.h"
28 #include "settings.h"
29 #include "debug.h"
30 #include "usb.h"
31 #include "backlight.h"
32 #include "audio.h"
33 #include "mpeg.h"
34 #include "talk.h"
35 #include "string.h"
36 #include "rtc.h"
37 #include "power.h"
38 #include "ata_idle_notify.h"
39 #include "atoi.h"
40 #include "screens.h"
41 #include "ctype.h"
42 #include "file.h"
43 #include "system.h"
44 #include "misc.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #include "font.h"
48 #include "peakmeter.h"
49 #endif
50 #include "lang.h"
51 #include "language.h"
52 #include "gwps.h"
53 #include "powermgmt.h"
54 #include "sprintf.h"
55 #include "keyboard.h"
56 #include "version.h"
57 #include "sound.h"
58 #include "rbunicode.h"
59 #include "dircache.h"
60 #include "statusbar.h"
61 #include "splash.h"
62 #include "list.h"
63 #include "settings_list.h"
64 #include "filetypes.h"
66 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
67 #include "backdrop.h"
68 #endif
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 "pcmbuf.h"
82 #include "dsp.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 DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n",
211 14+i, r );
212 return false;
215 #endif
216 return true;
219 /** Reading from a config file **/
221 * load settings from disk or RTC RAM
223 void settings_load(int which)
225 DEBUGF( "reload_all_settings()\n" );
226 if (which&SETTINGS_RTC)
227 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
228 if (which&SETTINGS_HD)
230 settings_load_config(CONFIGFILE,false);
231 settings_load_config(FIXEDSETTINGSFILE,false);
235 static bool cfg_string_to_int(int setting_id, int* out, char* str)
237 const char* start = settings[setting_id].cfg_vals;
238 char* end = NULL;
239 char temp[MAX_PATH];
240 int count = 0;
241 while (1)
243 end = strchr(start, ',');
244 if (!end)
246 if (!strcmp(str, start))
248 *out = count;
249 return true;
251 else return false;
253 strncpy(temp, start, end-start);
254 temp[end-start] = '\0';
255 if (!strcmp(str, temp))
257 *out = count;
258 return true;
260 start = end +1;
261 count++;
263 return false;
266 bool settings_load_config(const char* file, bool apply)
268 int fd;
269 char line[128];
270 char* name;
271 char* value;
272 int i;
273 fd = open(file, O_RDONLY);
274 if (fd < 0)
275 return false;
277 while (read_line(fd, line, sizeof line) > 0)
279 if (!settings_parseline(line, &name, &value))
280 continue;
281 for(i=0; i<nb_settings; i++)
283 if (settings[i].cfg_name == NULL)
284 continue;
285 if (!strcasecmp(name,settings[i].cfg_name))
287 switch (settings[i].flags&F_T_MASK)
289 case F_T_INT:
290 case F_T_UINT:
291 #ifdef HAVE_LCD_COLOR
292 if (settings[i].flags&F_RGB)
293 *(int*)settings[i].setting = hex_to_rgb(value);
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 break;
322 case F_T_CHARPTR:
323 case F_T_UCHARPTR:
325 char storage[MAX_PATH];
326 if (settings[i].filename_setting->prefix)
328 int len = strlen(settings[i].filename_setting->prefix);
329 if (!strncasecmp(value,
330 settings[i].filename_setting->prefix,
331 len))
333 strncpy(storage,&value[len],MAX_PATH);
335 else strncpy(storage,value,MAX_PATH);
337 else strncpy(storage,value,MAX_PATH);
338 if (settings[i].filename_setting->suffix)
340 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
341 if (s) *s = '\0';
343 strncpy((char*)settings[i].setting,storage,
344 settings[i].filename_setting->max_len);
345 ((char*)settings[i].setting)
346 [settings[i].filename_setting->max_len-1] = '\0';
347 break;
350 break;
351 } /* if (!strcmp(name,settings[i].cfg_name)) */
352 } /* for(...) */
353 } /* while(...) */
355 close(fd);
356 settings_save();
357 if (apply)
358 settings_apply();
359 return true;
362 /** Writing to a config file and saving settings **/
364 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
366 int flags = settings[setting_id].flags;
367 const char* start = settings[setting_id].cfg_vals;
368 char* end = NULL;
369 int count = 0;
371 if ((flags&F_T_MASK)==F_T_INT &&
372 flags&F_TABLE_SETTING)
374 const int *value = settings[setting_id].table_setting->values;
375 while (start)
377 end = strchr(start,',');
378 if (value[count] == val)
380 if (end == NULL)
381 strncpy(buf, start, buf_len);
382 else
384 int len = (buf_len > (end-start))? end-start: buf_len;
385 strncpy(buf, start, len);
386 buf[len] = '\0';
388 return true;
390 count++;
392 if (end)
393 start = end+1;
394 else
395 break;
397 return false;
400 while (count < val)
402 start = strchr(start,',');
403 if (!start)
404 return false;
405 count++;
406 start++;
408 end = strchr(start,',');
409 if (end == NULL)
410 strncpy(buf, start, buf_len);
411 else
413 int len = (buf_len > (end-start))? end-start: buf_len;
414 strncpy(buf, start, len);
415 buf[len] = '\0';
417 return true;
419 static bool is_changed(int setting_id)
421 const struct settings_list *setting = &settings[setting_id];
422 switch (setting->flags&F_T_MASK)
424 case F_T_INT:
425 case F_T_UINT:
426 if (setting->flags&F_DEF_ISFUNC)
428 if (*(int*)setting->setting == setting->default_val.func())
429 return false;
431 else if (setting->flags&F_T_SOUND)
433 if (*(int*)setting->setting ==
434 sound_default(setting->sound_setting->setting))
435 return false;
437 else if (*(int*)setting->setting == setting->default_val.int_)
438 return false;
439 break;
440 case F_T_BOOL:
441 if (*(bool*)setting->setting == setting->default_val.bool_)
442 return false;
443 break;
444 case F_T_CHARPTR:
445 case F_T_UCHARPTR:
446 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
447 return false;
448 break;
450 return true;
453 static bool settings_write_config(char* filename, int options)
455 int i;
456 int fd;
457 char value[MAX_PATH];
458 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
459 if (fd < 0)
460 return false;
461 fdprintf(fd, "# .cfg file created by rockbox %s - "
462 "http://www.rockbox.org\r\n\r\n", appsversion);
463 for(i=0; i<nb_settings; i++)
465 if (settings[i].cfg_name == NULL)
466 continue;
467 value[0] = '\0';
469 if ((options == SETTINGS_SAVE_CHANGED) &&
470 !is_changed(i))
471 continue;
472 else if ((options == SETTINGS_SAVE_SOUND) &&
473 ((settings[i].flags&F_SOUNDSETTING) == 0))
474 continue;
475 else if ((options == SETTINGS_SAVE_THEME) &&
476 ((settings[i].flags&F_THEMESETTING) == 0))
477 continue;
478 #ifdef HAVE_RECORDING
479 else if ((options == SETTINGS_SAVE_RECPRESETS) &&
480 ((settings[i].flags&F_RECSETTING) == 0))
481 continue;
482 #endif
483 #if CONFIG_CODEC == SWCODEC
484 else if ((options == SETTINGS_SAVE_EQPRESET) &&
485 ((settings[i].flags&F_EQSETTING) == 0))
486 continue;
487 #endif
488 switch (settings[i].flags&F_T_MASK)
490 case F_T_INT:
491 case F_T_UINT:
492 #ifdef HAVE_LCD_COLOR
493 if (settings[i].flags&F_RGB)
495 int colour = *(int*)settings[i].setting;
496 snprintf(value,MAX_PATH,"%02x%02x%02x",
497 (int)RGB_UNPACK_RED(colour),
498 (int)RGB_UNPACK_GREEN(colour),
499 (int)RGB_UNPACK_BLUE(colour));
501 else
502 #endif
503 if (settings[i].cfg_vals == NULL)
505 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
507 else
509 if (cfg_int_to_string(i, *(int*)settings[i].setting,
510 value, MAX_PATH) == false)
512 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
515 break;
516 case F_T_BOOL:
517 cfg_int_to_string(i,
518 *(bool*)settings[i].setting==false?0:1, value, MAX_PATH);
519 break;
520 case F_T_CHARPTR:
521 case F_T_UCHARPTR:
522 if (((char*)settings[i].setting)[0] == '\0')
523 break;
524 if (settings[i].filename_setting->prefix)
526 snprintf(value,MAX_PATH,"%s%s%s",
527 settings[i].filename_setting->prefix,
528 (char*)settings[i].setting,
529 settings[i].filename_setting->suffix);
531 else strncpy(value,(char*)settings[i].setting,
532 settings[i].filename_setting->max_len);
533 break;
534 } /* switch () */
535 if (value[0])
536 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
537 } /* for(...) */
538 close(fd);
539 return true;
541 #ifndef HAVE_RTC_RAM
542 static bool flush_global_status_callback(void)
544 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
546 #endif
547 static bool flush_config_block_callback(void)
549 bool r1, r2;
550 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
551 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
552 return r1 || r2;
556 * persist all runtime user settings to disk or RTC RAM
558 static void update_runtime(void)
560 int elapsed_secs;
562 elapsed_secs = (current_tick - lasttime) / HZ;
563 global_status.runtime += elapsed_secs;
564 lasttime += (elapsed_secs * HZ);
566 if ( global_status.runtime > global_status.topruntime )
567 global_status.topruntime = global_status.runtime;
570 void status_save( void )
572 update_runtime();
573 #ifdef HAVE_RTC_RAM
574 /* this will be done in the ata_callback if
575 target doesnt have rtc ram */
576 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
577 #else
578 register_ata_idle_func(flush_global_status_callback);
579 #endif
582 int settings_save( void )
584 update_runtime();
585 #ifdef HAVE_RTC_RAM
586 /* this will be done in the ata_callback if
587 target doesnt have rtc ram */
588 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
589 #endif
590 if(!register_ata_idle_func(flush_config_block_callback))
592 int i;
593 FOR_NB_SCREENS(i)
595 screens[i].clear_display();
596 #ifdef HAVE_LCD_CHARCELLS
597 screens[i].puts(0, 0, str(LANG_SETTINGS_SAVE_FAILED));
598 screens[i].puts(0, 1, str(LANG_SETTINGS_PARTITION));
599 #else
600 screens[i].puts(4, 2, str(LANG_SETTINGS_SAVE_FAILED));
601 screens[i].puts(2, 4, str(LANG_SETTINGS_PARTITION));
602 screens[i].update();
603 #endif
605 cond_talk_ids_fq(LANG_SETTINGS_SAVE_FAILED);
606 sleep(HZ*2);
607 return -1;
609 return 0;
611 bool settings_save_config(int options)
613 char filename[MAX_PATH];
614 char *folder;
615 switch (options)
617 case SETTINGS_SAVE_THEME:
618 folder = THEME_DIR;
619 break;
620 #ifdef HAVE_RECORDING
621 case SETTINGS_SAVE_RECPRESETS:
622 folder = RECPRESETS_DIR;
623 break;
624 #endif
625 #if CONFIG_CODEC == SWCODEC
626 case SETTINGS_SAVE_EQPRESET:
627 folder = EQS_DIR;
628 break;
629 #endif
630 case SETTINGS_SAVE_SOUND:
631 default:
632 folder = ROCKBOX_DIR;
634 create_numbered_filename(filename, folder, "config", ".cfg", 2
635 IF_CNFN_NUM_(, NULL));
637 /* allow user to modify filename */
638 while (true) {
639 if (!kbd_input(filename, sizeof filename)) {
640 break;
642 else {
643 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
644 return false;
648 if (settings_write_config(filename, options))
649 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_SAVED));
650 else
651 gui_syncsplash(HZ, ID2P(LANG_FAILED));
652 return true;
655 /** Apply and Reset settings **/
658 #ifdef HAVE_LCD_BITMAP
660 * Applies the range infos stored in global_settings to
661 * the peak meter.
663 void settings_apply_pm_range(void)
665 int pm_min, pm_max;
667 /* depending on the scale mode (dBfs or percent) the values
668 of global_settings.peak_meter_dbfs have different meanings */
669 if (global_settings.peak_meter_dbfs)
671 /* convert to dBfs * 100 */
672 pm_min = -(((int)global_settings.peak_meter_min) * 100);
673 pm_max = -(((int)global_settings.peak_meter_max) * 100);
675 else
677 /* percent is stored directly -> no conversion */
678 pm_min = global_settings.peak_meter_min;
679 pm_max = global_settings.peak_meter_max;
682 /* apply the range */
683 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
685 #endif /* HAVE_LCD_BITMAP */
687 void sound_settings_apply(void)
689 #if CONFIG_CODEC == SWCODEC
690 sound_set_dsp_callback(dsp_callback);
691 #endif
692 sound_set(SOUND_BASS, global_settings.bass);
693 sound_set(SOUND_TREBLE, global_settings.treble);
694 sound_set(SOUND_BALANCE, global_settings.balance);
695 sound_set(SOUND_VOLUME, global_settings.volume);
696 sound_set(SOUND_CHANNELS, global_settings.channel_config);
697 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
698 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
699 sound_set(SOUND_LOUDNESS, global_settings.loudness);
700 sound_set(SOUND_AVC, global_settings.avc);
701 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
702 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
703 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
704 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
705 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
706 sound_set(SOUND_SUPERBASS, global_settings.superbass);
707 #endif
709 #ifdef HAVE_WM8758
710 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
711 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
712 #endif
714 #ifdef HAVE_USB_POWER
715 #if CONFIG_CHARGING
716 usb_charging_enable(global_settings.usb_charging);
717 #endif
718 #endif
721 void settings_apply(void)
723 char buf[64];
724 #if CONFIG_CODEC == SWCODEC
725 int i;
726 #endif
728 DEBUGF( "settings_apply()\n" );
729 sound_settings_apply();
731 #ifndef HAVE_FLASH_STORAGE
732 audio_set_buffer_margin(global_settings.buffer_margin);
733 #endif
735 #ifdef HAVE_LCD_CONTRAST
736 lcd_set_contrast(global_settings.contrast);
737 #endif
738 lcd_scroll_speed(global_settings.scroll_speed);
739 #ifdef HAVE_REMOTE_LCD
740 lcd_remote_set_contrast(global_settings.remote_contrast);
741 lcd_remote_set_invert_display(global_settings.remote_invert);
742 lcd_remote_set_flip(global_settings.remote_flip_display);
743 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
744 lcd_remote_scroll_step(global_settings.remote_scroll_step);
745 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
746 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
747 #ifdef HAVE_REMOTE_LCD_TICKING
748 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
749 #endif
750 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
751 #if CONFIG_CHARGING
752 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
753 #endif
754 #ifdef HAS_REMOTE_BUTTON_HOLD
755 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
756 #endif
757 #endif /* HAVE_REMOTE_LCD */
758 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
759 backlight_set_brightness(global_settings.brightness);
760 #endif
761 #ifdef HAVE_BACKLIGHT
762 backlight_set_timeout(global_settings.backlight_timeout);
763 #if CONFIG_CHARGING
764 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
765 #endif
766 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
767 backlight_set_fade_in(global_settings.backlight_fade_in);
768 backlight_set_fade_out(global_settings.backlight_fade_out);
769 #endif
770 #endif
771 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
772 buttonlight_set_brightness(global_settings.buttonlight_brightness);
773 #endif
774 #ifdef HAVE_BUTTON_LIGHT
775 buttonlight_set_timeout(global_settings.buttonlight_timeout);
776 #endif
777 #ifndef HAVE_FLASH_STORAGE
778 ata_spindown(global_settings.disk_spindown);
779 #endif
780 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
781 dac_line_in(global_settings.line_in);
782 #endif
783 set_poweroff_timeout(global_settings.poweroff);
785 set_battery_capacity(global_settings.battery_capacity);
786 #if BATTERY_TYPES_COUNT > 1
787 set_battery_type(global_settings.battery_type);
788 #endif
790 #ifdef HAVE_LCD_BITMAP
791 lcd_set_invert_display(global_settings.invert);
792 lcd_set_flip(global_settings.flip_display);
793 button_set_flip(global_settings.flip_display);
794 lcd_update(); /* refresh after flipping the screen */
795 settings_apply_pm_range();
796 peak_meter_init_times(
797 global_settings.peak_meter_release, global_settings.peak_meter_hold,
798 global_settings.peak_meter_clip_hold);
799 #endif
801 #if LCD_DEPTH > 1
802 unload_wps_backdrop();
803 #endif
804 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
805 unload_remote_wps_backdrop();
806 #endif
807 if ( global_settings.wps_file[0] &&
808 global_settings.wps_file[0] != 0xff ) {
809 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
810 global_settings.wps_file);
811 wps_data_load(gui_wps[0].data, buf, true);
813 else
815 wps_data_init(gui_wps[0].data);
816 #ifdef HAVE_REMOTE_LCD
817 gui_wps[0].data->remote_wps = false;
818 #endif
821 #if LCD_DEPTH > 1
822 if ( global_settings.backdrop_file[0] &&
823 global_settings.backdrop_file[0] != 0xff ) {
824 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
825 global_settings.backdrop_file);
826 load_main_backdrop(buf);
827 } else {
828 unload_main_backdrop();
830 show_main_backdrop();
831 #endif
832 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
833 show_remote_main_backdrop();
834 #endif
836 #ifdef HAVE_LCD_COLOR
837 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
838 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
839 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
840 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
841 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
842 #endif
844 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
845 if ( global_settings.rwps_file[0]) {
846 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
847 global_settings.rwps_file);
848 wps_data_load(gui_wps[1].data, buf, true);
850 else
852 wps_data_init(gui_wps[1].data);
853 gui_wps[1].data->remote_wps = true;
855 #endif
857 #ifdef HAVE_LCD_BITMAP
858 if ( global_settings.font_file[0]) {
859 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
860 global_settings.font_file);
861 font_load(buf);
863 else
864 font_reset();
866 if ( global_settings.kbd_file[0]) {
867 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
868 global_settings.kbd_file);
869 load_kbd(buf);
871 else
872 load_kbd(NULL);
874 lcd_scroll_step(global_settings.scroll_step);
875 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
876 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
877 #else
878 lcd_jump_scroll(global_settings.jump_scroll);
879 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
880 #endif
881 lcd_bidir_scroll(global_settings.bidir_limit);
882 lcd_scroll_delay(global_settings.scroll_delay);
884 if ( global_settings.lang_file[0]) {
885 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
886 global_settings.lang_file);
887 lang_load(buf);
888 talk_init(); /* use voice of same language */
891 set_codepage(global_settings.default_codepage);
893 #if CONFIG_CODEC == SWCODEC
894 audio_set_crossfade(global_settings.crossfade);
895 dsp_set_replaygain();
896 dsp_set_crossfeed(global_settings.crossfeed);
897 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
898 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
899 global_settings.crossfeed_hf_attenuation,
900 global_settings.crossfeed_hf_cutoff);
902 /* Configure software equalizer, hardware eq is handled in audio_init() */
903 dsp_set_eq(global_settings.eq_enabled);
904 dsp_set_eq_precut(global_settings.eq_precut);
905 for(i = 0; i < 5; i++) {
906 dsp_set_eq_coefs(i);
909 dsp_dither_enable(global_settings.dithering_enabled);
910 #endif
912 #ifdef HAVE_SPDIF_POWER
913 spdif_power_enable(global_settings.spdif_enable);
914 #endif
916 #ifdef HAVE_BACKLIGHT
917 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
918 #ifdef HAVE_REMOTE_LCD
919 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
920 #endif
921 #ifdef HAS_BUTTON_HOLD
922 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
923 #endif
924 #ifdef HAVE_LCD_SLEEP
925 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
926 #endif
927 #endif /* HAVE_BACKLIGHT */
929 /* This should stay last */
930 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
931 enc_global_settings_apply();
932 #endif
933 /* load the icon set */
934 icons_init();
936 #ifdef HAVE_LCD_COLOR
937 if (global_settings.colors_file)
938 read_color_theme_file();
939 #endif
945 * reset all settings to their default value
947 void settings_reset(void) {
949 int i;
950 DEBUGF( "settings_reset()\n" );
952 for(i=0; i<nb_settings; i++)
954 switch (settings[i].flags&F_T_MASK)
956 case F_T_INT:
957 case F_T_UINT:
958 if (settings[i].flags&F_DEF_ISFUNC)
959 *(int*)settings[i].setting = settings[i].default_val.func();
960 else if (settings[i].flags&F_T_SOUND)
961 *(int*)settings[i].setting =
962 sound_default(settings[i].sound_setting->setting);
963 else *(int*)settings[i].setting = settings[i].default_val.int_;
964 break;
965 case F_T_BOOL:
966 *(bool*)settings[i].setting = settings[i].default_val.bool_;
967 break;
968 case F_T_CHARPTR:
969 case F_T_UCHARPTR:
970 strncpy((char*)settings[i].setting,
971 settings[i].default_val.charptr,MAX_FILENAME);
972 break;
974 } /* for(...) */
975 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
976 enc_global_settings_reset();
977 #endif
980 /** Changing setting values **/
981 const struct settings_list* find_setting(void* variable, int *id)
983 int i;
984 for(i=0;i<nb_settings;i++)
986 if (settings[i].setting == variable)
988 if (id)
989 *id = i;
990 return &settings[i];
993 return NULL;
996 void talk_setting(void *global_settings_variable)
998 const struct settings_list *setting;
999 if (!global_settings.talk_menu)
1000 return;
1001 setting = find_setting(global_settings_variable, NULL);
1002 if (setting == NULL)
1003 return;
1004 if (setting->lang_id)
1005 talk_id(setting->lang_id,false);
1008 bool set_bool(const char* string, bool* variable )
1010 return set_bool_options(string, variable,
1011 (char *)STR(LANG_SET_BOOL_YES),
1012 (char *)STR(LANG_SET_BOOL_NO),
1013 NULL);
1017 bool set_bool_options(const char* string, bool* variable,
1018 const char* yes_str, int yes_voice,
1019 const char* no_str, int no_voice,
1020 void (*function)(bool))
1022 struct opt_items names[] = {
1023 {(unsigned char *)no_str, no_voice},
1024 {(unsigned char *)yes_str, yes_voice}
1026 bool result;
1028 result = set_option(string, variable, BOOL, names, 2,
1029 (void (*)(int))function);
1030 return result;
1033 bool set_int(const unsigned char* string,
1034 const char* unit,
1035 int voice_unit,
1036 int* variable,
1037 void (*function)(int),
1038 int step,
1039 int min,
1040 int max,
1041 void (*formatter)(char*, size_t, int, const char*) )
1043 return set_int_ex(string, unit, voice_unit, variable, function,
1044 step, min, max, formatter, NULL);
1048 /** extra stuff which is probably misplaced **/
1050 void set_file(char* filename, char* setting, int maxlen)
1052 char* fptr = strrchr(filename,'/');
1053 int len;
1054 int extlen = 0;
1055 char* ptr;
1057 if (!fptr)
1058 return;
1060 *fptr = 0;
1061 fptr++;
1063 len = strlen(fptr);
1064 ptr = fptr + len;
1065 while ((*ptr != '.') && (ptr != fptr)) {
1066 extlen++;
1067 ptr--;
1069 if(ptr == fptr) extlen = 0;
1071 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1072 (len-extlen > maxlen))
1073 return;
1075 strncpy(setting, fptr, len-extlen);
1076 setting[len-extlen]=0;
1078 settings_save();