remote multifont fixes. it shoud be working again (blame AlexP! he made me break...
[kugel-rb.git] / apps / settings.c
blob536e5e90eeb830b7dc146360caa379cef06cfc2b
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 "talk.h"
36 #include "string.h"
37 #include "rtc.h"
38 #include "power.h"
39 #include "ata_idle_notify.h"
40 #include "storage.h"
41 #include "screens.h"
42 #include "ctype.h"
43 #include "file.h"
44 #include "system.h"
45 #include "general.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 "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 "splash.h"
62 #include "list.h"
63 #include "settings_list.h"
64 #include "filetypes.h"
65 #include "option_select.h"
66 #if CONFIG_TUNER
67 #include "radio.h"
68 #endif
69 #include "wps.h"
70 #include "skin_engine/skin_engine.h"
71 #include "skin_engine/skin_fonts.h"
72 #include "viewport.h"
73 #include "statusbar-skinned.h"
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 strlcpy(temp, start, end-start+1);
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 { /* atoi breaks choice settings because they
312 * don't have int-like values, and would
313 * fall back to the first value (i.e. 0)
314 * due to atoi */
315 if (!(settings[i].flags&F_CHOICE_SETTING))
316 *v = atoi(value);
319 break;
320 case F_T_BOOL:
322 int temp;
323 if (cfg_string_to_int(i,&temp,value))
324 *(bool*)settings[i].setting = (temp!=0);
325 if (settings[i].bool_setting->option_callback)
326 settings[i].bool_setting->option_callback(temp!=0);
327 break;
329 case F_T_CHARPTR:
330 case F_T_UCHARPTR:
332 char storage[MAX_PATH];
333 if (settings[i].filename_setting->prefix)
335 int len = strlen(settings[i].filename_setting->prefix);
336 if (!strncasecmp(value,
337 settings[i].filename_setting->prefix,
338 len))
340 strlcpy(storage, &value[len], MAX_PATH);
342 else strlcpy(storage, value, MAX_PATH);
344 else strlcpy(storage, value, MAX_PATH);
345 if (settings[i].filename_setting->suffix)
347 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
348 if (s) *s = '\0';
350 strlcpy((char*)settings[i].setting, storage,
351 settings[i].filename_setting->max_len);
352 break;
355 break;
356 } /* if (!strcmp(name,settings[i].cfg_name)) */
357 } /* for(...) */
358 } /* while(...) */
360 close(fd);
361 settings_save();
362 if (apply)
363 settings_apply(true);
364 return true;
367 /** Writing to a config file and saving settings **/
369 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
371 int flags = settings[setting_id].flags;
372 const char* start = settings[setting_id].cfg_vals;
373 char* end = NULL;
374 int count = 0;
376 if ((flags&F_T_MASK)==F_T_INT &&
377 flags&F_TABLE_SETTING)
379 const int *value = settings[setting_id].table_setting->values;
380 while (start)
382 end = strchr(start,',');
383 if (value[count] == val)
385 if (end == NULL)
386 strlcpy(buf, start, buf_len);
387 else
389 int len = (buf_len > (end-start))? end-start: buf_len;
390 strlcpy(buf, start, len+1);
392 return true;
394 count++;
396 if (end)
397 start = end+1;
398 else
399 break;
401 return false;
404 while (count < val)
406 start = strchr(start,',');
407 if (!start)
408 return false;
409 count++;
410 start++;
412 end = strchr(start,',');
413 if (end == NULL)
414 strlcpy(buf, start, buf_len);
415 else
417 int len = (buf_len > (end-start))? end-start: buf_len;
418 strlcpy(buf, start, len+1);
420 return true;
423 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
425 switch (settings[i].flags&F_T_MASK)
427 case F_T_CUSTOM:
428 settings[i].custom_setting->write_to_cfg(settings[i].setting,
429 buf, buf_len);
430 break;
431 case F_T_INT:
432 case F_T_UINT:
433 #ifdef HAVE_LCD_COLOR
434 if (settings[i].flags&F_RGB)
436 int colour = *(int*)settings[i].setting;
437 snprintf(buf,buf_len,"%02x%02x%02x",
438 (int)RGB_UNPACK_RED(colour),
439 (int)RGB_UNPACK_GREEN(colour),
440 (int)RGB_UNPACK_BLUE(colour));
442 else
443 #endif
444 if (settings[i].cfg_vals == NULL)
446 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
448 else
450 if (cfg_int_to_string(i, *(int*)settings[i].setting,
451 buf, buf_len) == false)
453 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
455 else
456 return false;
458 break;
459 case F_T_BOOL:
460 cfg_int_to_string(i,
461 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
462 break;
463 case F_T_CHARPTR:
464 case F_T_UCHARPTR:
465 if (((char*)settings[i].setting)[0]
466 && settings[i].filename_setting->prefix)
468 snprintf(buf,buf_len,"%s%s%s",
469 settings[i].filename_setting->prefix,
470 (char*)settings[i].setting,
471 settings[i].filename_setting->suffix);
473 else strlcpy(buf,(char*)settings[i].setting,
474 settings[i].filename_setting->max_len);
475 break;
476 } /* switch () */
477 return true;
481 static bool is_changed(int setting_id)
483 const struct settings_list *setting = &settings[setting_id];
484 switch (setting->flags&F_T_MASK)
486 case F_T_CUSTOM:
487 return setting->custom_setting->is_changed(setting->setting,
488 setting->default_val.custom);
489 break;
490 case F_T_INT:
491 case F_T_UINT:
492 if (setting->flags&F_DEF_ISFUNC)
494 if (*(int*)setting->setting == setting->default_val.func())
495 return false;
497 else if (setting->flags&F_T_SOUND)
499 if (*(int*)setting->setting ==
500 sound_default(setting->sound_setting->setting))
501 return false;
503 else if (*(int*)setting->setting == setting->default_val.int_)
504 return false;
505 break;
506 case F_T_BOOL:
507 if (*(bool*)setting->setting == setting->default_val.bool_)
508 return false;
509 break;
510 case F_T_CHARPTR:
511 case F_T_UCHARPTR:
512 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
513 return false;
514 break;
516 return true;
519 static bool settings_write_config(const char* filename, int options)
521 int i;
522 int fd;
523 char value[MAX_PATH];
524 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
525 if (fd < 0)
526 return false;
527 fdprintf(fd, "# .cfg file created by rockbox %s - "
528 "http://www.rockbox.org\r\n\r\n", appsversion);
529 for(i=0; i<nb_settings; i++)
531 if (settings[i].cfg_name == NULL)
532 continue;
533 value[0] = '\0';
535 switch (options)
537 case SETTINGS_SAVE_CHANGED:
538 if (!is_changed(i))
539 continue;
540 break;
541 case SETTINGS_SAVE_SOUND:
542 if ((settings[i].flags&F_SOUNDSETTING) == 0)
543 continue;
544 break;
545 case SETTINGS_SAVE_THEME:
546 if ((settings[i].flags&F_THEMESETTING) == 0)
547 continue;
548 break;
549 #ifdef HAVE_RECORDING
550 case SETTINGS_SAVE_RECPRESETS:
551 if ((settings[i].flags&F_RECSETTING) == 0)
552 continue;
553 break;
554 #endif
555 #if CONFIG_CODEC == SWCODEC
556 case SETTINGS_SAVE_EQPRESET:
557 if ((settings[i].flags&F_EQSETTING) == 0)
558 continue;
559 break;
560 #endif
563 cfg_to_string(i, value, MAX_PATH);
564 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
565 } /* for(...) */
566 close(fd);
567 return true;
569 #ifndef HAVE_RTC_RAM
570 static void flush_global_status_callback(void *data)
572 (void)data;
573 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
575 #endif
576 static void flush_config_block_callback(void *data)
578 (void)data;
579 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
580 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
584 * persist all runtime user settings to disk or RTC RAM
586 static void update_runtime(void)
588 int elapsed_secs;
590 elapsed_secs = (current_tick - lasttime) / HZ;
591 global_status.runtime += elapsed_secs;
592 lasttime += (elapsed_secs * HZ);
594 if ( global_status.runtime > global_status.topruntime )
595 global_status.topruntime = global_status.runtime;
598 void status_save(void)
600 update_runtime();
601 #ifdef HAVE_RTC_RAM
602 /* this will be done in the storage_callback if
603 target doesnt have rtc ram */
604 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
605 #else
606 register_storage_idle_func(flush_global_status_callback);
607 #endif
610 int settings_save(void)
612 update_runtime();
613 #ifdef HAVE_RTC_RAM
614 /* this will be done in the storage_callback if
615 target doesnt have rtc ram */
616 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
617 #endif
618 register_storage_idle_func(flush_config_block_callback);
619 return 0;
622 bool settings_save_config(int options)
624 char filename[MAX_PATH];
625 char *folder, *namebase;
626 switch (options)
628 case SETTINGS_SAVE_THEME:
629 folder = THEME_DIR;
630 namebase = "theme";
631 break;
632 #ifdef HAVE_RECORDING
633 case SETTINGS_SAVE_RECPRESETS:
634 folder = RECPRESETS_DIR;
635 namebase = "recording";
636 break;
637 #endif
638 #if CONFIG_CODEC == SWCODEC
639 case SETTINGS_SAVE_EQPRESET:
640 folder = EQS_DIR;
641 namebase = "eq";
642 break;
643 #endif
644 case SETTINGS_SAVE_SOUND:
645 folder = ROCKBOX_DIR;
646 namebase = "sound";
647 break;
648 default:
649 folder = ROCKBOX_DIR;
650 namebase = "config";
651 break;
653 create_numbered_filename(filename, folder, namebase, ".cfg", 2
654 IF_CNFN_NUM_(, NULL));
656 /* allow user to modify filename */
657 while (true) {
658 if (!kbd_input(filename, sizeof filename)) {
659 break;
661 else {
662 return false;
666 if (settings_write_config(filename, options))
667 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
668 else
669 splash(HZ, ID2P(LANG_FAILED));
670 return true;
673 /** Apply and Reset settings **/
676 #ifdef HAVE_LCD_BITMAP
678 * Applies the range infos stored in global_settings to
679 * the peak meter.
681 void settings_apply_pm_range(void)
683 int pm_min, pm_max;
685 /* depending on the scale mode (dBfs or percent) the values
686 of global_settings.peak_meter_dbfs have different meanings */
687 if (global_settings.peak_meter_dbfs)
689 /* convert to dBfs * 100 */
690 pm_min = -(((int)global_settings.peak_meter_min) * 100);
691 pm_max = -(((int)global_settings.peak_meter_max) * 100);
693 else
695 /* percent is stored directly -> no conversion */
696 pm_min = global_settings.peak_meter_min;
697 pm_max = global_settings.peak_meter_max;
700 /* apply the range */
701 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
703 #endif /* HAVE_LCD_BITMAP */
705 void sound_settings_apply(void)
707 #if CONFIG_CODEC == SWCODEC
708 sound_set_dsp_callback(dsp_callback);
709 #endif
710 sound_set(SOUND_BASS, global_settings.bass);
711 sound_set(SOUND_TREBLE, global_settings.treble);
712 sound_set(SOUND_BALANCE, global_settings.balance);
713 sound_set(SOUND_VOLUME, global_settings.volume);
714 sound_set(SOUND_CHANNELS, global_settings.channel_config);
715 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
716 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
717 sound_set(SOUND_LOUDNESS, global_settings.loudness);
718 sound_set(SOUND_AVC, global_settings.avc);
719 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
720 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
721 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
722 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
723 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
724 sound_set(SOUND_SUPERBASS, global_settings.superbass);
725 #endif
727 #ifdef HAVE_WM8758
728 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
729 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
730 #endif
735 /* call this after loading a .wps/.rwps pr other skin files, so that the
736 * skin buffer is reset properly
738 void settings_apply_skins(void)
740 char buf[MAX_PATH];
741 /* re-initialize the skin buffer before we start reloading skins */
742 skin_buffer_init();
743 #ifdef HAVE_LCD_BITMAP
744 skin_backdrop_init();
745 skin_font_init();
746 if ( global_settings.sbs_file[0] &&
747 global_settings.sbs_file[0] != 0xff )
749 snprintf(buf, sizeof buf, SBS_DIR "/%s.sbs",
750 global_settings.sbs_file);
751 sb_skin_data_load(SCREEN_MAIN, buf, true);
753 else
755 sb_skin_data_load(SCREEN_MAIN, NULL, true);
757 #endif
758 if ( global_settings.wps_file[0] &&
759 global_settings.wps_file[0] != 0xff )
761 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
762 global_settings.wps_file);
763 wps_data_load(SCREEN_MAIN, buf, true);
765 else
767 wps_data_load(SCREEN_MAIN, NULL, true);
769 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
770 if ( global_settings.rsbs_file[0] &&
771 global_settings.rsbs_file[0] != 0xff ) {
772 snprintf(buf, sizeof buf, SBS_DIR "/%s.rsbs",
773 global_settings.rsbs_file);
774 sb_skin_data_load(SCREEN_REMOTE, buf, true);
776 else
778 sb_skin_data_load(SCREEN_REMOTE, NULL, true);
780 if ( global_settings.rwps_file[0])
782 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
783 global_settings.rwps_file);
784 wps_data_load(SCREEN_REMOTE, buf, true);
786 else
788 wps_data_load(SCREEN_REMOTE, NULL, true);
790 #endif
791 viewportmanager_theme_changed(THEME_STATUSBAR);
792 #if LCD_DEPTH > 1 || defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
793 int i;
794 FOR_NB_SCREENS(i)
795 screens[i].backdrop_show(sb_get_backdrop(i));
796 #endif
799 void settings_apply(bool read_disk)
802 char buf[64];
803 #if CONFIG_CODEC == SWCODEC
804 int i;
805 #endif
806 sound_settings_apply();
808 #ifdef HAVE_DISK_STORAGE
809 audio_set_buffer_margin(global_settings.buffer_margin);
810 #endif
812 #ifdef HAVE_LCD_CONTRAST
813 lcd_set_contrast(global_settings.contrast);
814 #endif
815 lcd_scroll_speed(global_settings.scroll_speed);
816 #ifdef HAVE_REMOTE_LCD
817 lcd_remote_set_contrast(global_settings.remote_contrast);
818 lcd_remote_set_invert_display(global_settings.remote_invert);
820 #ifdef HAVE_LCD_FLIP
821 lcd_remote_set_flip(global_settings.remote_flip_display);
822 #endif
824 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
825 lcd_remote_scroll_step(global_settings.remote_scroll_step);
826 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
827 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
828 #ifdef HAVE_REMOTE_LCD_TICKING
829 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
830 #endif
831 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
832 #if CONFIG_CHARGING
833 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
834 #endif
835 #ifdef HAS_REMOTE_BUTTON_HOLD
836 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
837 #endif
838 #endif /* HAVE_REMOTE_LCD */
839 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
840 backlight_set_brightness(global_settings.brightness);
841 #endif
842 #ifdef HAVE_BACKLIGHT
843 backlight_set_timeout(global_settings.backlight_timeout);
844 #if CONFIG_CHARGING
845 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
846 #endif
847 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
848 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
849 backlight_set_fade_in(global_settings.backlight_fade_in);
850 backlight_set_fade_out(global_settings.backlight_fade_out);
851 #endif
852 #endif
853 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
854 buttonlight_set_brightness(global_settings.buttonlight_brightness);
855 #endif
856 #ifdef HAVE_BUTTON_LIGHT
857 buttonlight_set_timeout(global_settings.buttonlight_timeout);
858 #endif
859 #ifdef HAVE_DISK_STORAGE
860 storage_spindown(global_settings.disk_spindown);
861 #endif
862 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
863 dac_line_in(global_settings.line_in);
864 #endif
865 set_poweroff_timeout(global_settings.poweroff);
867 set_battery_capacity(global_settings.battery_capacity);
868 #if BATTERY_TYPES_COUNT > 1
869 set_battery_type(global_settings.battery_type);
870 #endif
872 #ifdef HAVE_LCD_BITMAP
873 #ifdef HAVE_LCD_INVERT
874 lcd_set_invert_display(global_settings.invert);
875 #endif
876 #ifdef HAVE_LCD_FLIP
877 lcd_set_flip(global_settings.flip_display);
878 button_set_flip(global_settings.flip_display);
879 #endif
880 lcd_update(); /* refresh after flipping the screen */
881 settings_apply_pm_range();
882 peak_meter_init_times(
883 global_settings.peak_meter_release, global_settings.peak_meter_hold,
884 global_settings.peak_meter_clip_hold);
885 #endif
887 #ifdef HAVE_SPEAKER
888 audiohw_enable_speaker(global_settings.speaker_enabled);
889 #endif
891 if (read_disk)
893 #ifdef HAVE_LCD_BITMAP
894 /* fonts need to be loaded before the WPS */
895 if ( global_settings.font_file[0]) {
896 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
897 global_settings.font_file);
898 if (font_load(NULL, buf) < 0)
899 font_reset(NULL);
901 else
902 font_reset(NULL);
903 #ifdef HAVE_REMOTE_LCD
904 if ( global_settings.remote_font_file[0]) {
905 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
906 global_settings.remote_font_file);
907 if (font_load_remoteui(buf) < 0)
908 font_load_remoteui(NULL);
910 else
911 font_load_remoteui(NULL);
912 #endif
913 if ( global_settings.kbd_file[0]) {
914 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
915 global_settings.kbd_file);
916 load_kbd(buf);
918 else
919 load_kbd(NULL);
920 #endif
922 if ( global_settings.lang_file[0]) {
923 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
924 global_settings.lang_file);
925 lang_core_load(buf);
926 talk_init(); /* use voice of same language */
929 /* reload wpses */
930 settings_apply_skins();
932 /* load the icon set */
933 icons_init();
935 #ifdef HAVE_LCD_COLOR
936 if (global_settings.colors_file[0])
937 read_color_theme_file();
938 #endif
941 #ifdef HAVE_LCD_COLOR
942 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
943 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
944 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
945 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
946 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
947 #endif
949 #ifdef HAVE_LCD_BITMAP
950 lcd_scroll_step(global_settings.scroll_step);
951 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
952 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
953 #else
954 lcd_jump_scroll(global_settings.jump_scroll);
955 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
956 #endif
957 lcd_bidir_scroll(global_settings.bidir_limit);
958 lcd_scroll_delay(global_settings.scroll_delay);
961 set_codepage(global_settings.default_codepage);
963 #if CONFIG_CODEC == SWCODEC
964 #ifdef HAVE_CROSSFADE
965 audio_set_crossfade(global_settings.crossfade);
966 #endif
967 dsp_set_replaygain();
968 dsp_set_crossfeed(global_settings.crossfeed);
969 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
970 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
971 global_settings.crossfeed_hf_attenuation,
972 global_settings.crossfeed_hf_cutoff);
974 /* Configure software equalizer, hardware eq is handled in audio_init() */
975 dsp_set_eq(global_settings.eq_enabled);
976 dsp_set_eq_precut(global_settings.eq_precut);
977 for(i = 0; i < 5; i++) {
978 dsp_set_eq_coefs(i);
981 dsp_dither_enable(global_settings.dithering_enabled);
982 dsp_timestretch_enable(global_settings.timestretch_enabled);
983 dsp_set_compressor(global_settings.compressor_threshold,
984 global_settings.compressor_makeup_gain,
985 global_settings.compressor_ratio,
986 global_settings.compressor_knee,
987 global_settings.compressor_release_time);
988 #endif
990 #ifdef HAVE_SPDIF_POWER
991 spdif_power_enable(global_settings.spdif_enable);
992 #endif
994 #ifdef HAVE_BACKLIGHT
995 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
996 #ifdef HAVE_REMOTE_LCD
997 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
998 #endif
999 #ifdef HAS_BUTTON_HOLD
1000 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
1001 #endif
1002 #ifdef HAVE_LCD_SLEEP_SETTING
1003 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
1004 #endif
1005 #endif /* HAVE_BACKLIGHT */
1007 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1008 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1009 #endif
1011 #ifdef HAVE_USB_CHARGING_ENABLE
1012 usb_charging_enable(global_settings.usb_charging);
1013 #endif
1015 #ifdef HAVE_TOUCHSCREEN
1016 touchscreen_set_mode(global_settings.touch_mode);
1017 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1018 #endif
1020 /* This should stay last */
1021 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1022 enc_global_settings_apply();
1023 #endif
1024 #ifdef HAVE_LCD_BITMAP
1025 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1026 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE);
1027 #endif
1032 * reset all settings to their default value
1034 void reset_setting(const struct settings_list *setting, void *var)
1036 switch (setting->flags&F_T_MASK)
1038 case F_T_CUSTOM:
1039 setting->custom_setting->set_default(setting->setting,
1040 setting->default_val.custom);
1041 break;
1042 case F_T_INT:
1043 case F_T_UINT:
1044 if (setting->flags&F_DEF_ISFUNC)
1045 *(int*)var = setting->default_val.func();
1046 else if (setting->flags&F_T_SOUND)
1047 *(int*)var = sound_default(setting->sound_setting->setting);
1048 else *(int*)var = setting->default_val.int_;
1049 break;
1050 case F_T_BOOL:
1051 *(bool*)var = setting->default_val.bool_;
1052 break;
1053 case F_T_CHARPTR:
1054 case F_T_UCHARPTR:
1055 strlcpy((char*)var, setting->default_val.charptr,
1056 setting->filename_setting->max_len);
1057 break;
1061 void settings_reset(void)
1063 int i;
1065 for(i=0; i<nb_settings; i++)
1066 reset_setting(&settings[i], settings[i].setting);
1067 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1068 enc_global_settings_reset();
1069 #endif
1072 /** Changing setting values **/
1073 const struct settings_list* find_setting(const void* variable, int *id)
1075 int i;
1076 for(i=0;i<nb_settings;i++)
1078 if (settings[i].setting == variable)
1080 if (id)
1081 *id = i;
1082 return &settings[i];
1085 return NULL;
1088 bool set_bool(const char* string, const bool* variable )
1090 return set_bool_options(string, variable,
1091 (char *)STR(LANG_SET_BOOL_YES),
1092 (char *)STR(LANG_SET_BOOL_NO),
1093 NULL);
1097 bool set_bool_options(const char* string, const bool* variable,
1098 const char* yes_str, int yes_voice,
1099 const char* no_str, int no_voice,
1100 void (*function)(bool))
1102 struct opt_items names[] = {
1103 {(unsigned const char *)no_str, no_voice},
1104 {(unsigned const char *)yes_str, yes_voice}
1106 bool result;
1108 result = set_option(string, variable, BOOL, names, 2,
1109 (void (*)(int))function);
1110 return result;
1113 bool set_int(const unsigned char* string,
1114 const char* unit,
1115 int voice_unit,
1116 const int* variable,
1117 void (*function)(int),
1118 int step,
1119 int min,
1120 int max,
1121 const char* (*formatter)(char*, size_t, int, const char*) )
1123 return set_int_ex(string, unit, voice_unit, variable, function,
1124 step, min, max, formatter, NULL);
1127 bool set_int_ex(const unsigned char* string,
1128 const char* unit,
1129 int voice_unit,
1130 const int* variable,
1131 void (*function)(int),
1132 int step,
1133 int min,
1134 int max,
1135 const char* (*formatter)(char*, size_t, int, const char*),
1136 int32_t (*get_talk_id)(int, int))
1138 (void)unit;
1139 struct settings_list item;
1140 struct int_setting data = {
1141 function, voice_unit, min, max, step,
1142 formatter, get_talk_id
1144 item.int_setting = &data;
1145 item.flags = F_INT_SETTING|F_T_INT;
1146 item.lang_id = -1;
1147 item.cfg_vals = (char*)string;
1148 item.setting = (void *)variable;
1149 return option_screen(&item, NULL, false, NULL);
1153 static const struct opt_items *set_option_options;
1154 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1156 (void)buf, (void)unit, (void)size;
1157 return P2STR(set_option_options[item].string);
1160 static int32_t set_option_get_talk_id(int value, int unit)
1162 (void)unit;
1163 return set_option_options[value].voice_id;
1166 bool set_option(const char* string, const void* variable, enum optiontype type,
1167 const struct opt_items* options,
1168 int numoptions, void (*function)(int))
1170 int temp;
1171 struct settings_list item;
1172 struct int_setting data = {
1173 function, UNIT_INT, 0, numoptions-1, 1,
1174 set_option_formatter, set_option_get_talk_id
1176 set_option_options = options;
1177 item.int_setting = &data;
1178 item.flags = F_INT_SETTING|F_T_INT;
1179 item.lang_id = -1;
1180 item.cfg_vals = (char*)string;
1181 item.setting = &temp;
1182 if (type == BOOL)
1183 temp = *(bool*)variable? 1: 0;
1184 else
1185 temp = *(int*)variable;
1186 if (!option_screen(&item, NULL, false, NULL))
1188 if (type == BOOL)
1189 *(bool*)variable = (temp == 1);
1190 else
1191 *(int*)variable = temp;
1192 return false;
1194 return true;
1198 void set_file(const char* filename, char* setting, int maxlen)
1200 const char* fptr = strrchr(filename,'/');
1201 int len;
1202 int extlen = 0;
1203 const char* ptr;
1205 if (!fptr)
1206 return;
1208 fptr++;
1210 len = strlen(fptr);
1211 ptr = fptr + len;
1212 while ((*ptr != '.') && (ptr != fptr)) {
1213 extlen++;
1214 ptr--;
1216 if(ptr == fptr) extlen = 0;
1218 if (strncasecmp(ROCKBOX_DIR, filename, strlen(ROCKBOX_DIR)) ||
1219 (len-extlen > maxlen))
1220 return;
1222 strlcpy(setting, fptr, len-extlen+1);
1224 settings_save();