Make some global function/variables local (by making them static)
[maemo-rb.git] / apps / settings.c
blob6f1fd7ad2e5a3295a84363ebc44d8f64d74b782f
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 "sound.h"
31 #include "settings.h"
32 #include "debug.h"
33 #include "usb.h"
34 #include "backlight.h"
35 #include "audio.h"
36 #include "talk.h"
37 #include "strlcpy.h"
38 #include "strcasestr.h"
39 #include "rtc.h"
40 #include "power.h"
41 #include "ata_idle_notify.h"
42 #include "storage.h"
43 #include "screens.h"
44 #include "ctype.h"
45 #include "file.h"
46 #include "system.h"
47 #include "general.h"
48 #include "misc.h"
49 #ifdef HAVE_LCD_BITMAP
50 #include "icons.h"
51 #include "font.h"
52 #include "peakmeter.h"
53 #endif
54 #include "lang.h"
55 #include "language.h"
56 #include "powermgmt.h"
57 #include "keyboard.h"
58 #include "version.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 "viewport.h"
72 #include "statusbar-skinned.h"
73 #include "bootchart.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 int bytes;
123 if (fd < 0)
124 return false;
125 memset(buf,0,max_len);
126 bytes = read(fd,buf,max_len);
127 close(fd);
128 if (bytes < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
129 return false;
130 #else
131 memset(buf,0,max_len);
132 /* read rtc block */
133 for (i=0; i < max_len; i++ )
134 buf[i] = rtc_read(0x14+i);
135 #endif
136 /* check magic, version */
137 if ((buf[0] != 'R') || (buf[1] != 'b')
138 || (buf[2] != NVRAM_CONFIG_VERSION))
139 return false;
140 /* check crc32 */
141 crc32 = crc_32(&buf[NVRAM_DATA_START],
142 max_len-NVRAM_DATA_START-1,0xffffffff);
143 if (memcmp(&crc32,&buf[4],4))
144 return false;
145 /* all good, so read in the settings */
146 var_count = buf[3];
147 buf_pos = NVRAM_DATA_START;
148 for(i=0; i<nb_settings; i++)
150 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
151 >>F_NVRAM_MASK_SHIFT;
152 if (nvram_bytes)
154 if ((var_count>0) && (buf_pos<max_len))
156 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
157 buf_pos += nvram_bytes;
158 var_count--;
160 else /* should only happen when new items are added to the end */
162 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
166 return true;
168 static bool write_nvram_data(char* buf, int max_len)
170 unsigned crc32 = 0xffffffff;
171 int i = 0, buf_pos = 0;
172 char var_count = 0;
173 #ifndef HAVE_RTC_RAM
174 int fd;
175 #endif
176 memset(buf,0,max_len);
177 /* magic, version */
178 buf[0] = 'R'; buf[1] = 'b';
179 buf[2] = NVRAM_CONFIG_VERSION;
180 buf_pos = NVRAM_DATA_START;
181 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
183 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
184 >>F_NVRAM_MASK_SHIFT;
185 if (nvram_bytes)
187 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
188 buf_pos += nvram_bytes;
189 var_count++;
192 /* count and crc32 */
193 buf[3] = var_count;
194 crc32 = crc_32(&buf[NVRAM_DATA_START],
195 max_len-NVRAM_DATA_START-1,0xffffffff);
196 memcpy(&buf[4],&crc32,4);
197 #ifndef HAVE_RTC_RAM
198 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY, 0666);
199 if (fd >= 0)
201 int len = write(fd,buf,max_len);
202 close(fd);
203 if (len < 8)
204 return false;
206 #else
207 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
208 that it would write a number of bytes at a time since the RTC chip
209 supports that, but this will have to do for now 8-) */
210 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
211 int r = rtc_write(0x14+i, buf[i]);
212 if (r)
213 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 if (which&SETTINGS_RTC)
226 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
227 if (which&SETTINGS_HD)
229 settings_load_config(CONFIGFILE,false);
230 settings_load_config(FIXEDSETTINGSFILE,false);
234 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
236 const char* start = settings[setting_id].cfg_vals;
237 char* end = NULL;
238 char temp[MAX_PATH];
239 int count = 0;
240 while (1)
242 end = strchr(start, ',');
243 if (!end)
245 if (!strcmp(str, start))
247 *out = count;
248 return true;
250 else return false;
252 strlcpy(temp, start, end-start+1);
253 if (!strcmp(str, temp))
255 *out = count;
256 return true;
258 start = end +1;
259 count++;
261 return false;
264 bool settings_load_config(const char* file, bool apply)
266 int fd;
267 char line[128];
268 char* name;
269 char* value;
270 int i;
271 fd = open_utf8(file, O_RDONLY);
272 if (fd < 0)
273 return false;
275 while (read_line(fd, line, sizeof line) > 0)
277 if (!settings_parseline(line, &name, &value))
278 continue;
279 for(i=0; i<nb_settings; i++)
281 if (settings[i].cfg_name == NULL)
282 continue;
283 if (!strcasecmp(name,settings[i].cfg_name))
285 switch (settings[i].flags&F_T_MASK)
287 case F_T_CUSTOM:
288 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
289 break;
290 case F_T_INT:
291 case F_T_UINT:
292 #ifdef HAVE_LCD_COLOR
293 if (settings[i].flags&F_RGB)
294 hex_to_rgb(value, (int*)settings[i].setting);
295 else
296 #endif
297 if (settings[i].cfg_vals == NULL)
299 *(int*)settings[i].setting = atoi(value);
301 else
303 int temp, *v = (int*)settings[i].setting;
304 bool found = cfg_string_to_int(i, &temp, value);
305 if (found)
307 if (settings[i].flags&F_TABLE_SETTING)
308 *v = settings[i].table_setting->values[temp];
309 else
310 *v = temp;
312 else
313 { /* atoi breaks choice settings because they
314 * don't have int-like values, and would
315 * fall back to the first value (i.e. 0)
316 * due to atoi */
317 if (!(settings[i].flags&F_CHOICE_SETTING))
318 *v = atoi(value);
321 break;
322 case F_T_BOOL:
324 int temp;
325 if (cfg_string_to_int(i,&temp,value))
326 *(bool*)settings[i].setting = (temp!=0);
327 if (settings[i].bool_setting->option_callback)
328 settings[i].bool_setting->option_callback(temp!=0);
329 break;
331 case F_T_CHARPTR:
332 case F_T_UCHARPTR:
334 char storage[MAX_PATH];
335 if (settings[i].filename_setting->prefix)
337 int len = strlen(settings[i].filename_setting->prefix);
338 if (!strncasecmp(value,
339 settings[i].filename_setting->prefix,
340 len))
342 strlcpy(storage, &value[len], MAX_PATH);
344 else strlcpy(storage, value, MAX_PATH);
346 else strlcpy(storage, value, MAX_PATH);
347 if (settings[i].filename_setting->suffix)
349 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
350 if (s) *s = '\0';
352 strlcpy((char*)settings[i].setting, storage,
353 settings[i].filename_setting->max_len);
354 break;
357 break;
358 } /* if (!strcmp(name,settings[i].cfg_name)) */
359 } /* for(...) */
360 } /* while(...) */
362 close(fd);
363 settings_save();
364 if (apply)
366 settings_apply(true);
367 settings_apply_skins();
369 return true;
372 /** Writing to a config file and saving settings **/
374 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
376 int flags = settings[setting_id].flags;
377 const char* start = settings[setting_id].cfg_vals;
378 char* end = NULL;
379 int count = 0;
381 if ((flags&F_T_MASK)==F_T_INT &&
382 flags&F_TABLE_SETTING)
384 const int *value = settings[setting_id].table_setting->values;
385 while (start)
387 end = strchr(start,',');
388 if (value[count] == val)
390 if (end == NULL)
391 strlcpy(buf, start, buf_len);
392 else
394 int len = (buf_len > (end-start))? end-start: buf_len;
395 strlcpy(buf, start, len+1);
397 return true;
399 count++;
401 if (end)
402 start = end+1;
403 else
404 break;
406 return false;
409 while (count < val)
411 start = strchr(start,',');
412 if (!start)
413 return false;
414 count++;
415 start++;
417 end = strchr(start,',');
418 if (end == NULL)
419 strlcpy(buf, start, buf_len);
420 else
422 int len = (buf_len > (end-start))? end-start: buf_len;
423 strlcpy(buf, start, len+1);
425 return true;
428 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
430 switch (settings[i].flags&F_T_MASK)
432 case F_T_CUSTOM:
433 settings[i].custom_setting->write_to_cfg(settings[i].setting,
434 buf, buf_len);
435 break;
436 case F_T_INT:
437 case F_T_UINT:
438 #ifdef HAVE_LCD_COLOR
439 if (settings[i].flags&F_RGB)
441 int colour = *(int*)settings[i].setting;
442 snprintf(buf,buf_len,"%02x%02x%02x",
443 (int)RGB_UNPACK_RED(colour),
444 (int)RGB_UNPACK_GREEN(colour),
445 (int)RGB_UNPACK_BLUE(colour));
447 else
448 #endif
449 if (settings[i].cfg_vals == NULL)
451 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
453 else
455 if (cfg_int_to_string(i, *(int*)settings[i].setting,
456 buf, buf_len) == false)
458 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
460 else
461 return false;
463 break;
464 case F_T_BOOL:
465 cfg_int_to_string(i,
466 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
467 break;
468 case F_T_CHARPTR:
469 case F_T_UCHARPTR:
470 if (((char*)settings[i].setting)[0]
471 && settings[i].filename_setting->prefix)
473 if (((char*)settings[i].setting)[0] == '-')
475 buf[0] = '-';
476 buf[1] = '\0';
478 else
480 snprintf(buf,buf_len,"%s%s%s",
481 settings[i].filename_setting->prefix,
482 (char*)settings[i].setting,
483 settings[i].filename_setting->suffix);
486 else strlcpy(buf,(char*)settings[i].setting,
487 settings[i].filename_setting->max_len);
488 break;
489 } /* switch () */
490 return true;
494 static bool is_changed(int setting_id)
496 const struct settings_list *setting = &settings[setting_id];
497 switch (setting->flags&F_T_MASK)
499 case F_T_CUSTOM:
500 return setting->custom_setting->is_changed(setting->setting,
501 setting->default_val.custom);
502 break;
503 case F_T_INT:
504 case F_T_UINT:
505 if (setting->flags&F_DEF_ISFUNC)
507 if (*(int*)setting->setting == setting->default_val.func())
508 return false;
510 else if (setting->flags&F_T_SOUND)
512 if (*(int*)setting->setting ==
513 sound_default(setting->sound_setting->setting))
514 return false;
516 else if (*(int*)setting->setting == setting->default_val.int_)
517 return false;
518 break;
519 case F_T_BOOL:
520 if (*(bool*)setting->setting == setting->default_val.bool_)
521 return false;
522 break;
523 case F_T_CHARPTR:
524 case F_T_UCHARPTR:
525 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
526 return false;
527 break;
529 return true;
532 static bool settings_write_config(const char* filename, int options)
534 int i;
535 int fd;
536 char value[MAX_PATH];
537 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY, 0666);
538 if (fd < 0)
539 return false;
540 fdprintf(fd, "# .cfg file created by rockbox %s - "
541 "http://www.rockbox.org\r\n\r\n", rbversion);
542 for(i=0; i<nb_settings; i++)
544 if (settings[i].cfg_name == NULL)
545 continue;
546 value[0] = '\0';
548 switch (options)
550 case SETTINGS_SAVE_CHANGED:
551 if (!is_changed(i))
552 continue;
553 break;
554 case SETTINGS_SAVE_SOUND:
555 if ((settings[i].flags&F_SOUNDSETTING) == 0)
556 continue;
557 break;
558 case SETTINGS_SAVE_THEME:
559 if ((settings[i].flags&F_THEMESETTING) == 0)
560 continue;
561 break;
562 #ifdef HAVE_RECORDING
563 case SETTINGS_SAVE_RECPRESETS:
564 if ((settings[i].flags&F_RECSETTING) == 0)
565 continue;
566 break;
567 #endif
568 #if CONFIG_CODEC == SWCODEC
569 case SETTINGS_SAVE_EQPRESET:
570 if ((settings[i].flags&F_EQSETTING) == 0)
571 continue;
572 break;
573 #endif
576 cfg_to_string(i, value, MAX_PATH);
577 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
578 } /* for(...) */
579 close(fd);
580 return true;
582 #ifndef HAVE_RTC_RAM
583 static void flush_global_status_callback(void *data)
585 (void)data;
586 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
588 #endif
589 static void flush_config_block_callback(void *data)
591 (void)data;
592 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
593 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
597 * persist all runtime user settings to disk or RTC RAM
599 static void update_runtime(void)
601 int elapsed_secs;
603 elapsed_secs = (current_tick - lasttime) / HZ;
604 global_status.runtime += elapsed_secs;
605 lasttime += (elapsed_secs * HZ);
607 if ( global_status.runtime > global_status.topruntime )
608 global_status.topruntime = global_status.runtime;
611 void status_save(void)
613 update_runtime();
614 #ifdef HAVE_RTC_RAM
615 /* this will be done in the storage_callback if
616 target doesnt have rtc ram */
617 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
618 #else
619 register_storage_idle_func(flush_global_status_callback);
620 #endif
623 int settings_save(void)
625 update_runtime();
626 #ifdef HAVE_RTC_RAM
627 /* this will be done in the storage_callback if
628 target doesnt have rtc ram */
629 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
630 #endif
631 register_storage_idle_func(flush_config_block_callback);
632 return 0;
635 bool settings_save_config(int options)
637 char filename[MAX_PATH];
638 char *folder, *namebase;
639 switch (options)
641 case SETTINGS_SAVE_THEME:
642 folder = THEME_DIR;
643 namebase = "theme";
644 break;
645 #ifdef HAVE_RECORDING
646 case SETTINGS_SAVE_RECPRESETS:
647 folder = RECPRESETS_DIR;
648 namebase = "recording";
649 break;
650 #endif
651 #if CONFIG_CODEC == SWCODEC
652 case SETTINGS_SAVE_EQPRESET:
653 folder = EQS_DIR;
654 namebase = "eq";
655 break;
656 #endif
657 case SETTINGS_SAVE_SOUND:
658 folder = ROCKBOX_DIR;
659 namebase = "sound";
660 break;
661 default:
662 folder = ROCKBOX_DIR;
663 namebase = "config";
664 break;
666 create_numbered_filename(filename, folder, namebase, ".cfg", 2
667 IF_CNFN_NUM_(, NULL));
669 /* allow user to modify filename */
670 while (true) {
671 if (!kbd_input(filename, sizeof filename)) {
672 break;
674 else {
675 return false;
679 if (settings_write_config(filename, options))
680 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
681 else
682 splash(HZ, ID2P(LANG_FAILED));
683 return true;
686 /** Apply and Reset settings **/
689 #ifdef HAVE_LCD_BITMAP
691 * Applies the range infos stored in global_settings to
692 * the peak meter.
694 void settings_apply_pm_range(void)
696 int pm_min, pm_max;
698 /* depending on the scale mode (dBfs or percent) the values
699 of global_settings.peak_meter_dbfs have different meanings */
700 if (global_settings.peak_meter_dbfs)
702 /* convert to dBfs * 100 */
703 pm_min = -(((int)global_settings.peak_meter_min) * 100);
704 pm_max = -(((int)global_settings.peak_meter_max) * 100);
706 else
708 /* percent is stored directly -> no conversion */
709 pm_min = global_settings.peak_meter_min;
710 pm_max = global_settings.peak_meter_max;
713 /* apply the range */
714 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
716 #endif /* HAVE_LCD_BITMAP */
718 void sound_settings_apply(void)
720 #if CONFIG_CODEC == SWCODEC
721 sound_set_dsp_callback(dsp_callback);
722 #endif
723 #ifdef AUDIOHW_HAVE_BASS
724 sound_set(SOUND_BASS, global_settings.bass);
725 #endif
726 #ifdef AUDIOHW_HAVE_TREBLE
727 sound_set(SOUND_TREBLE, global_settings.treble);
728 #endif
729 sound_set(SOUND_BALANCE, global_settings.balance);
730 sound_set(SOUND_VOLUME, global_settings.volume);
731 sound_set(SOUND_CHANNELS, global_settings.channel_config);
732 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
733 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
734 sound_set(SOUND_LOUDNESS, global_settings.loudness);
735 sound_set(SOUND_AVC, global_settings.avc);
736 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
737 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
738 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
739 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
740 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
741 sound_set(SOUND_SUPERBASS, global_settings.superbass);
742 #endif
743 #ifdef AUDIOHW_HAVE_BASS_CUTOFF
744 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
745 #endif
746 #ifdef AUDIOHW_HAVE_TREBLE_CUTOFF
747 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
748 #endif
749 #ifdef AUDIOHW_HAVE_DEPTH_3D
750 sound_set(SOUND_DEPTH_3D, global_settings.depth_3d);
751 #endif
752 #ifdef AUDIOHW_HAVE_EQ
753 int b;
755 for (b = 0; b < AUDIOHW_EQ_BAND_NUM; b++)
757 int setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_GAIN);
758 sound_set(setting, global_settings.hw_eq_bands[b].gain);
760 #ifdef AUDIOHW_HAVE_EQ_FREQUENCY
761 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_FREQUENCY);
762 if (setting != -1)
763 sound_set(setting, global_settings.hw_eq_bands[b].frequency);
764 #endif /* AUDIOHW_HAVE_EQ_FREQUENCY */
765 #ifdef AUDIOHW_HAVE_EQ_WIDTH
766 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_WIDTH);
767 if (setting != -1)
768 sound_set(setting, global_settings.hw_eq_bands[b].width);
769 #endif /* AUDIOHW_HAVE_EQ_WIDTH */
771 #endif
774 void settings_apply(bool read_disk)
776 #ifdef HAVE_LCD_BITMAP
777 int rc;
778 #endif
779 #if CONFIG_CODEC == SWCODEC
780 int i;
781 #endif
782 sound_settings_apply();
784 #ifdef HAVE_DISK_STORAGE
785 audio_set_buffer_margin(global_settings.buffer_margin);
786 #endif
788 #ifdef HAVE_LCD_CONTRAST
789 lcd_set_contrast(global_settings.contrast);
790 #endif
791 lcd_scroll_speed(global_settings.scroll_speed);
792 #ifdef HAVE_REMOTE_LCD
793 lcd_remote_set_contrast(global_settings.remote_contrast);
794 lcd_remote_set_invert_display(global_settings.remote_invert);
796 #ifdef HAVE_LCD_FLIP
797 lcd_remote_set_flip(global_settings.remote_flip_display);
798 #endif
800 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
801 lcd_remote_scroll_step(global_settings.remote_scroll_step);
802 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
803 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
804 #ifdef HAVE_REMOTE_LCD_TICKING
805 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
806 #endif
807 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
808 #if CONFIG_CHARGING
809 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
810 #endif
811 #ifdef HAS_REMOTE_BUTTON_HOLD
812 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
813 #endif
814 #endif /* HAVE_REMOTE_LCD */
815 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
816 backlight_set_brightness(global_settings.brightness);
817 #endif
818 #ifdef HAVE_BACKLIGHT
819 backlight_set_timeout(global_settings.backlight_timeout);
820 #if CONFIG_CHARGING
821 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
822 #endif
823 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
824 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
825 backlight_set_fade_in(global_settings.backlight_fade_in);
826 backlight_set_fade_out(global_settings.backlight_fade_out);
827 #endif
828 #endif
829 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
830 buttonlight_set_brightness(global_settings.buttonlight_brightness);
831 #endif
832 #ifdef HAVE_BUTTON_LIGHT
833 buttonlight_set_timeout(global_settings.buttonlight_timeout);
834 #endif
835 #ifdef HAVE_DISK_STORAGE
836 storage_spindown(global_settings.disk_spindown);
837 #endif
838 #if (CONFIG_CODEC == MAS3507D) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
839 dac_line_in(global_settings.line_in);
840 #endif
841 set_poweroff_timeout(global_settings.poweroff);
843 set_battery_capacity(global_settings.battery_capacity);
844 #if BATTERY_TYPES_COUNT > 1
845 set_battery_type(global_settings.battery_type);
846 #endif
848 #ifdef HAVE_LCD_BITMAP
849 #ifdef HAVE_LCD_INVERT
850 lcd_set_invert_display(global_settings.invert);
851 #endif
852 #ifdef HAVE_LCD_FLIP
853 lcd_set_flip(global_settings.flip_display);
854 button_set_flip(global_settings.flip_display);
855 #endif
856 lcd_update(); /* refresh after flipping the screen */
857 settings_apply_pm_range();
858 peak_meter_init_times(
859 global_settings.peak_meter_release, global_settings.peak_meter_hold,
860 global_settings.peak_meter_clip_hold);
861 #endif
863 #ifdef HAVE_SPEAKER
864 audiohw_enable_speaker(global_settings.speaker_enabled);
865 #endif
867 if (read_disk)
869 char buf[MAX_PATH];
870 #ifdef HAVE_LCD_BITMAP
871 /* fonts need to be loaded before the WPS */
872 if (global_settings.font_file[0]
873 && global_settings.font_file[0] != '-') {
874 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
875 global_settings.font_file);
876 CHART2(">font_load ", global_settings.font_file);
877 rc = font_load(NULL, buf);
878 CHART2("<font_load ", global_settings.font_file);
879 if (rc < 0)
880 font_reset(NULL);
882 else
883 font_reset(NULL);
884 #ifdef HAVE_REMOTE_LCD
885 if ( global_settings.remote_font_file[0]
886 && global_settings.remote_font_file[0] != '-') {
887 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
888 global_settings.remote_font_file);
889 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
890 rc = font_load_remoteui(buf);
891 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
892 if (rc < 0)
893 font_load_remoteui(NULL);
895 else
896 font_load_remoteui(NULL);
897 #endif
898 if ( global_settings.kbd_file[0]) {
899 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
900 global_settings.kbd_file);
901 CHART(">load_kbd");
902 load_kbd(buf);
903 CHART("<load_kbd");
905 else
906 load_kbd(NULL);
907 #endif
909 if ( global_settings.lang_file[0]) {
910 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
911 global_settings.lang_file);
912 CHART(">lang_core_load");
913 lang_core_load(buf);
914 CHART("<lang_core_load");
915 CHART(">talk_init");
916 talk_init(); /* use voice of same language */
917 CHART("<talk_init");
920 /* load the icon set */
921 CHART(">icons_init");
922 icons_init();
923 CHART("<icons_init");
925 #ifdef HAVE_LCD_COLOR
926 if (global_settings.colors_file[0])
928 CHART(">read_color_theme_file");
929 read_color_theme_file();
930 CHART("<read_color_theme_file");
932 #endif
934 #ifdef HAVE_LCD_COLOR
935 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
936 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
937 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
938 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
939 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
940 #endif
942 #ifdef HAVE_LCD_BITMAP
943 lcd_scroll_step(global_settings.scroll_step);
944 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
945 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
946 #endif
947 lcd_bidir_scroll(global_settings.bidir_limit);
948 lcd_scroll_delay(global_settings.scroll_delay);
951 CHART(">set_codepage");
952 set_codepage(global_settings.default_codepage);
953 CHART("<set_codepage");
955 #if CONFIG_CODEC == SWCODEC
956 #ifdef HAVE_CROSSFADE
957 audio_set_crossfade(global_settings.crossfade);
958 #endif
959 dsp_set_replaygain();
960 dsp_set_crossfeed(global_settings.crossfeed);
961 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
962 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
963 global_settings.crossfeed_hf_attenuation,
964 global_settings.crossfeed_hf_cutoff);
966 /* Configure software equalizer, hardware eq is handled in audio_init() */
967 dsp_set_eq(global_settings.eq_enabled);
968 dsp_set_eq_precut(global_settings.eq_precut);
969 for(i = 0; i < 5; i++) {
970 dsp_set_eq_coefs(i);
973 dsp_dither_enable(global_settings.dithering_enabled);
974 dsp_timestretch_enable(global_settings.timestretch_enabled);
975 dsp_set_compressor(global_settings.compressor_threshold,
976 global_settings.compressor_makeup_gain,
977 global_settings.compressor_ratio,
978 global_settings.compressor_knee,
979 global_settings.compressor_release_time);
980 #endif
982 #ifdef HAVE_SPDIF_POWER
983 spdif_power_enable(global_settings.spdif_enable);
984 #endif
986 #ifdef HAVE_BACKLIGHT
987 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
988 #ifdef HAVE_REMOTE_LCD
989 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
990 #endif
991 #ifdef HAS_BUTTON_HOLD
992 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
993 #endif
994 #ifdef HAVE_LCD_SLEEP_SETTING
995 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
996 #endif
997 #endif /* HAVE_BACKLIGHT */
999 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1000 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1001 #endif
1003 #ifdef HAVE_USB_CHARGING_ENABLE
1004 usb_charging_enable(global_settings.usb_charging);
1005 #endif
1007 #ifdef HAVE_TOUCHSCREEN
1008 touchscreen_set_mode(global_settings.touch_mode);
1009 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1010 #endif
1012 /* This should stay last */
1013 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1014 enc_global_settings_apply();
1015 #endif
1016 #ifdef HAVE_LCD_BITMAP
1017 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1018 CHART(">viewportmanager_theme_changed");
1019 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE|THEME_BUTTONBAR);
1020 CHART("<viewportmanager_theme_changed");
1021 #endif
1026 * reset all settings to their default value
1028 void reset_setting(const struct settings_list *setting, void *var)
1030 switch (setting->flags&F_T_MASK)
1032 case F_T_CUSTOM:
1033 setting->custom_setting->set_default(setting->setting,
1034 setting->default_val.custom);
1035 break;
1036 case F_T_INT:
1037 case F_T_UINT:
1038 if (setting->flags&F_DEF_ISFUNC)
1039 *(int*)var = setting->default_val.func();
1040 else if (setting->flags&F_T_SOUND)
1041 *(int*)var = sound_default(setting->sound_setting->setting);
1042 else *(int*)var = setting->default_val.int_;
1043 break;
1044 case F_T_BOOL:
1045 *(bool*)var = setting->default_val.bool_;
1046 break;
1047 case F_T_CHARPTR:
1048 case F_T_UCHARPTR:
1049 strlcpy((char*)var, setting->default_val.charptr,
1050 setting->filename_setting->max_len);
1051 break;
1055 void settings_reset(void)
1057 int i;
1059 for(i=0; i<nb_settings; i++)
1060 reset_setting(&settings[i], settings[i].setting);
1061 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1062 enc_global_settings_reset();
1063 #endif
1066 /** Changing setting values **/
1067 const struct settings_list* find_setting(const void* variable, int *id)
1069 int i;
1070 for(i=0;i<nb_settings;i++)
1072 if (settings[i].setting == variable)
1074 if (id)
1075 *id = i;
1076 return &settings[i];
1079 return NULL;
1082 bool set_bool(const char* string, const bool* variable )
1084 return set_bool_options(string, variable,
1085 (char *)STR(LANG_SET_BOOL_YES),
1086 (char *)STR(LANG_SET_BOOL_NO),
1087 NULL);
1091 bool set_bool_options(const char* string, const bool* variable,
1092 const char* yes_str, int yes_voice,
1093 const char* no_str, int no_voice,
1094 void (*function)(bool))
1096 struct opt_items names[] = {
1097 {(unsigned const char *)no_str, no_voice},
1098 {(unsigned const char *)yes_str, yes_voice}
1100 bool result;
1102 result = set_option(string, variable, BOOL, names, 2,
1103 (void (*)(int))function);
1104 return result;
1107 bool set_int(const unsigned char* string,
1108 const char* unit,
1109 int voice_unit,
1110 const int* variable,
1111 void (*function)(int),
1112 int step,
1113 int min,
1114 int max,
1115 const char* (*formatter)(char*, size_t, int, const char*) )
1117 return set_int_ex(string, unit, voice_unit, variable, function,
1118 step, min, max, formatter, NULL);
1121 bool set_int_ex(const unsigned char* string,
1122 const char* unit,
1123 int voice_unit,
1124 const int* variable,
1125 void (*function)(int),
1126 int step,
1127 int min,
1128 int max,
1129 const char* (*formatter)(char*, size_t, int, const char*),
1130 int32_t (*get_talk_id)(int, int))
1132 (void)unit;
1133 struct settings_list item;
1134 struct int_setting data = {
1135 function, voice_unit, min, max, step,
1136 formatter, get_talk_id
1138 item.int_setting = &data;
1139 item.flags = F_INT_SETTING|F_T_INT;
1140 item.lang_id = -1;
1141 item.cfg_vals = (char*)string;
1142 item.setting = (void *)variable;
1143 return option_screen(&item, NULL, false, NULL);
1147 static const struct opt_items *set_option_options;
1148 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1150 (void)buf, (void)unit, (void)size;
1151 return P2STR(set_option_options[item].string);
1154 static int32_t set_option_get_talk_id(int value, int unit)
1156 (void)unit;
1157 return set_option_options[value].voice_id;
1160 bool set_option(const char* string, const void* variable, enum optiontype type,
1161 const struct opt_items* options,
1162 int numoptions, void (*function)(int))
1164 int temp;
1165 struct settings_list item;
1166 struct int_setting data = {
1167 function, UNIT_INT, 0, numoptions-1, 1,
1168 set_option_formatter, set_option_get_talk_id
1170 set_option_options = options;
1171 item.int_setting = &data;
1172 item.flags = F_INT_SETTING|F_T_INT;
1173 item.lang_id = -1;
1174 item.cfg_vals = (char*)string;
1175 item.setting = &temp;
1176 if (type == BOOL)
1177 temp = *(bool*)variable? 1: 0;
1178 else
1179 temp = *(int*)variable;
1180 if (!option_screen(&item, NULL, false, NULL))
1182 if (type == BOOL)
1183 *(bool*)variable = (temp == 1);
1184 else
1185 *(int*)variable = temp;
1186 return false;
1188 return true;
1192 * Takes filename, removes the directory (assumed to be ROCKBOX_DIR) its in
1193 * and the extension, and then copies the basename into setting
1195 void set_file(const char* filename, char* setting, const int maxlen)
1197 const char* fptr = strrchr(filename,'/');
1198 const char* extptr;
1199 int len;
1200 int extlen = 0;
1202 if (!fptr)
1203 return;
1205 fptr++;
1207 extptr = strrchr(fptr, '.');
1209 if (!extptr || extptr < fptr)
1210 extlen = 0;
1211 else
1212 extlen = strlen(extptr);
1214 len = strlen(fptr) - extlen + 1;
1216 /* error if filename isn't in ROCKBOX_DIR */
1217 if (strncasecmp(ROCKBOX_DIR, filename, ROCKBOX_DIR_LEN) || (len > maxlen))
1218 return;
1220 strlcpy(setting, fptr, len);
1221 settings_save();