Android: Rewrite lcd subsystem to use the SurfaceView API.
[kugel-rb.git] / apps / settings.c
blobe491c5b6f2169eabd84d71f98c27d954528dcc2e
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 "rbpaths.h"
29 #include "action.h"
30 #include "crc32.h"
31 #include "sound.h"
32 #include "settings.h"
33 #include "debug.h"
34 #include "usb.h"
35 #include "backlight.h"
36 #include "audio.h"
37 #include "talk.h"
38 #include "strlcpy.h"
39 #include "strcasestr.h"
40 #include "rtc.h"
41 #include "power.h"
42 #include "ata_idle_notify.h"
43 #include "storage.h"
44 #include "screens.h"
45 #include "ctype.h"
46 #include "file.h"
47 #include "system.h"
48 #include "general.h"
49 #include "misc.h"
50 #ifdef HAVE_LCD_BITMAP
51 #include "icons.h"
52 #include "font.h"
53 #include "peakmeter.h"
54 #endif
55 #include "lang.h"
56 #include "language.h"
57 #include "powermgmt.h"
58 #include "keyboard.h"
59 #include "version.h"
60 #include "rbunicode.h"
61 #include "dircache.h"
62 #include "splash.h"
63 #include "list.h"
64 #include "settings_list.h"
65 #include "filetypes.h"
66 #include "option_select.h"
67 #if CONFIG_TUNER
68 #include "radio.h"
69 #endif
70 #include "wps.h"
71 #include "skin_engine/skin_engine.h"
72 #include "viewport.h"
73 #include "statusbar-skinned.h"
74 #include "bootchart.h"
76 #if CONFIG_CODEC == MAS3507D
77 void dac_line_in(bool enable);
78 #endif
79 struct user_settings global_settings;
80 struct system_status global_status;
82 #if CONFIG_CODEC == SWCODEC
83 #include "dsp.h"
84 #include "playback.h"
85 #ifdef HAVE_RECORDING
86 #include "enc_config.h"
87 #endif
88 #endif /* CONFIG_CODEC == SWCODEC */
90 #define NVRAM_BLOCK_SIZE 44
92 #ifdef HAVE_LCD_BITMAP
93 #define MAX_LINES 10
94 #else
95 #define MAX_LINES 2
96 #endif
98 #ifdef HAVE_REMOTE_LCD
99 #include "lcd-remote.h"
100 #endif
102 long lasttime = 0;
104 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
105 /* NVRAM is set out as
106 [0] 'R'
107 [1] 'b'
108 [2] version
109 [3] stored variable count
110 [4-7] crc32 checksum
111 [8-NVRAM_BLOCK_SIZE] data
113 #define NVRAM_DATA_START 8
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 const char *dir = settings[i].filename_setting->prefix;
338 size_t len = strlen(dir);
339 if (!strncasecmp(value, dir, len))
341 strlcpy(storage, &value[len], MAX_PATH);
343 else strlcpy(storage, value, MAX_PATH);
345 else strlcpy(storage, value, MAX_PATH);
346 if (settings[i].filename_setting->suffix)
348 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
349 if (s) *s = '\0';
351 strlcpy((char*)settings[i].setting, storage,
352 settings[i].filename_setting->max_len);
353 break;
356 break;
357 } /* if (!strcmp(name,settings[i].cfg_name)) */
358 } /* for(...) */
359 } /* while(...) */
361 close(fd);
362 if (apply)
364 settings_save();
365 settings_apply(true);
366 settings_apply_skins();
368 return true;
371 /** Writing to a config file and saving settings **/
373 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
375 int flags = settings[setting_id].flags;
376 const char* start = settings[setting_id].cfg_vals;
377 char* end = NULL;
378 int count = 0;
380 if ((flags&F_T_MASK)==F_T_INT &&
381 flags&F_TABLE_SETTING)
383 const int *value = settings[setting_id].table_setting->values;
384 while (start)
386 end = strchr(start,',');
387 if (value[count] == val)
389 if (end == NULL)
390 strlcpy(buf, start, buf_len);
391 else
393 int len = (buf_len > (end-start))? end-start: buf_len;
394 strlcpy(buf, start, len+1);
396 return true;
398 count++;
400 if (end)
401 start = end+1;
402 else
403 break;
405 return false;
408 while (count < val)
410 start = strchr(start,',');
411 if (!start)
412 return false;
413 count++;
414 start++;
416 end = strchr(start,',');
417 if (end == NULL)
418 strlcpy(buf, start, buf_len);
419 else
421 int len = (buf_len > (end-start))? end-start: buf_len;
422 strlcpy(buf, start, len+1);
424 return true;
427 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
429 switch (settings[i].flags&F_T_MASK)
431 case F_T_CUSTOM:
432 settings[i].custom_setting->write_to_cfg(settings[i].setting,
433 buf, buf_len);
434 break;
435 case F_T_INT:
436 case F_T_UINT:
437 #ifdef HAVE_LCD_COLOR
438 if (settings[i].flags&F_RGB)
440 int colour = *(int*)settings[i].setting;
441 snprintf(buf,buf_len,"%02x%02x%02x",
442 (int)RGB_UNPACK_RED(colour),
443 (int)RGB_UNPACK_GREEN(colour),
444 (int)RGB_UNPACK_BLUE(colour));
446 else
447 #endif
448 if (settings[i].cfg_vals == NULL)
450 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
452 else
454 if (cfg_int_to_string(i, *(int*)settings[i].setting,
455 buf, buf_len) == false)
457 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
459 else
460 return false;
462 break;
463 case F_T_BOOL:
464 cfg_int_to_string(i,
465 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
466 break;
467 case F_T_CHARPTR:
468 case F_T_UCHARPTR:
469 if (((char*)settings[i].setting)[0]
470 && settings[i].filename_setting->prefix)
472 if (((char*)settings[i].setting)[0] == '-')
474 buf[0] = '-';
475 buf[1] = '\0';
477 else
479 snprintf(buf,buf_len,"%s%s%s",
480 settings[i].filename_setting->prefix,
481 (char*)settings[i].setting,
482 settings[i].filename_setting->suffix);
485 else strlcpy(buf,(char*)settings[i].setting,
486 settings[i].filename_setting->max_len);
487 break;
488 } /* switch () */
489 return true;
493 static bool is_changed(int setting_id)
495 const struct settings_list *setting = &settings[setting_id];
496 switch (setting->flags&F_T_MASK)
498 case F_T_CUSTOM:
499 return setting->custom_setting->is_changed(setting->setting,
500 setting->default_val.custom);
501 break;
502 case F_T_INT:
503 case F_T_UINT:
504 if (setting->flags&F_DEF_ISFUNC)
506 if (*(int*)setting->setting == setting->default_val.func())
507 return false;
509 else if (setting->flags&F_T_SOUND)
511 if (*(int*)setting->setting ==
512 sound_default(setting->sound_setting->setting))
513 return false;
515 else if (*(int*)setting->setting == setting->default_val.int_)
516 return false;
517 break;
518 case F_T_BOOL:
519 if (*(bool*)setting->setting == setting->default_val.bool_)
520 return false;
521 break;
522 case F_T_CHARPTR:
523 case F_T_UCHARPTR:
524 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
525 return false;
526 break;
528 return true;
531 static bool settings_write_config(const char* filename, int options)
533 int i;
534 int fd;
535 char value[MAX_PATH];
536 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY, 0666);
537 if (fd < 0)
538 return false;
539 fdprintf(fd, "# .cfg file created by rockbox %s - "
540 "http://www.rockbox.org\r\n\r\n", rbversion);
541 for(i=0; i<nb_settings; i++)
543 if (settings[i].cfg_name == NULL)
544 continue;
545 value[0] = '\0';
547 switch (options)
549 case SETTINGS_SAVE_CHANGED:
550 if (!is_changed(i))
551 continue;
552 break;
553 case SETTINGS_SAVE_SOUND:
554 if ((settings[i].flags&F_SOUNDSETTING) == 0)
555 continue;
556 break;
557 case SETTINGS_SAVE_THEME:
558 if ((settings[i].flags&F_THEMESETTING) == 0)
559 continue;
560 break;
561 #ifdef HAVE_RECORDING
562 case SETTINGS_SAVE_RECPRESETS:
563 if ((settings[i].flags&F_RECSETTING) == 0)
564 continue;
565 break;
566 #endif
567 #if CONFIG_CODEC == SWCODEC
568 case SETTINGS_SAVE_EQPRESET:
569 if ((settings[i].flags&F_EQSETTING) == 0)
570 continue;
571 break;
572 #endif
575 cfg_to_string(i, value, MAX_PATH);
576 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
577 } /* for(...) */
578 close(fd);
579 return true;
581 #ifndef HAVE_RTC_RAM
582 static void flush_global_status_callback(void *data)
584 (void)data;
585 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
587 #endif
588 static void flush_config_block_callback(void *data)
590 (void)data;
591 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
592 settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
596 * persist all runtime user settings to disk or RTC RAM
598 static void update_runtime(void)
600 int elapsed_secs;
602 elapsed_secs = (current_tick - lasttime) / HZ;
603 global_status.runtime += elapsed_secs;
604 lasttime += (elapsed_secs * HZ);
606 if ( global_status.runtime > global_status.topruntime )
607 global_status.topruntime = global_status.runtime;
610 void status_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 #else
618 register_storage_idle_func(flush_global_status_callback);
619 #endif
622 int settings_save(void)
624 update_runtime();
625 #ifdef HAVE_RTC_RAM
626 /* this will be done in the storage_callback if
627 target doesnt have rtc ram */
628 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
629 #endif
630 register_storage_idle_func(flush_config_block_callback);
631 return 0;
634 bool settings_save_config(int options)
636 char filename[MAX_PATH];
637 const char *folder, *namebase;
638 switch (options)
640 case SETTINGS_SAVE_THEME:
641 folder = THEME_DIR;
642 namebase = "theme";
643 break;
644 #ifdef HAVE_RECORDING
645 case SETTINGS_SAVE_RECPRESETS:
646 folder = RECPRESETS_DIR;
647 namebase = "recording";
648 break;
649 #endif
650 #if CONFIG_CODEC == SWCODEC
651 case SETTINGS_SAVE_EQPRESET:
652 folder = EQS_DIR;
653 namebase = "eq";
654 break;
655 #endif
656 case SETTINGS_SAVE_SOUND:
657 folder = ROCKBOX_DIR;
658 namebase = "sound";
659 break;
660 default:
661 folder = ROCKBOX_DIR;
662 namebase = "config";
663 break;
665 create_numbered_filename(filename, folder, namebase, ".cfg", 2
666 IF_CNFN_NUM_(, NULL));
668 /* allow user to modify filename */
669 while (true) {
670 if (!kbd_input(filename, sizeof filename)) {
671 break;
673 else {
674 return false;
678 if (settings_write_config(filename, options))
679 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
680 else
681 splash(HZ, ID2P(LANG_FAILED));
682 return true;
685 /** Apply and Reset settings **/
688 #ifdef HAVE_LCD_BITMAP
690 * Applies the range infos stored in global_settings to
691 * the peak meter.
693 void settings_apply_pm_range(void)
695 int pm_min, pm_max;
697 /* depending on the scale mode (dBfs or percent) the values
698 of global_settings.peak_meter_dbfs have different meanings */
699 if (global_settings.peak_meter_dbfs)
701 /* convert to dBfs * 100 */
702 pm_min = -(((int)global_settings.peak_meter_min) * 100);
703 pm_max = -(((int)global_settings.peak_meter_max) * 100);
705 else
707 /* percent is stored directly -> no conversion */
708 pm_min = global_settings.peak_meter_min;
709 pm_max = global_settings.peak_meter_max;
712 /* apply the range */
713 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
715 #endif /* HAVE_LCD_BITMAP */
717 void sound_settings_apply(void)
719 #if CONFIG_CODEC == SWCODEC
720 sound_set_dsp_callback(dsp_callback);
721 #endif
722 #ifdef AUDIOHW_HAVE_BASS
723 sound_set(SOUND_BASS, global_settings.bass);
724 #endif
725 #ifdef AUDIOHW_HAVE_TREBLE
726 sound_set(SOUND_TREBLE, global_settings.treble);
727 #endif
728 sound_set(SOUND_BALANCE, global_settings.balance);
729 sound_set(SOUND_VOLUME, global_settings.volume);
730 sound_set(SOUND_CHANNELS, global_settings.channel_config);
731 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
732 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
733 sound_set(SOUND_LOUDNESS, global_settings.loudness);
734 sound_set(SOUND_AVC, global_settings.avc);
735 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
736 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
737 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
738 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
739 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
740 sound_set(SOUND_SUPERBASS, global_settings.superbass);
741 #endif
742 #ifdef AUDIOHW_HAVE_BASS_CUTOFF
743 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
744 #endif
745 #ifdef AUDIOHW_HAVE_TREBLE_CUTOFF
746 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
747 #endif
748 #ifdef AUDIOHW_HAVE_DEPTH_3D
749 sound_set(SOUND_DEPTH_3D, global_settings.depth_3d);
750 #endif
751 #ifdef AUDIOHW_HAVE_EQ
752 int b;
754 for (b = 0; b < AUDIOHW_EQ_BAND_NUM; b++)
756 int setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_GAIN);
757 sound_set(setting, global_settings.hw_eq_bands[b].gain);
759 #ifdef AUDIOHW_HAVE_EQ_FREQUENCY
760 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_FREQUENCY);
761 if (setting != -1)
762 sound_set(setting, global_settings.hw_eq_bands[b].frequency);
763 #endif /* AUDIOHW_HAVE_EQ_FREQUENCY */
764 #ifdef AUDIOHW_HAVE_EQ_WIDTH
765 setting = sound_enum_hw_eq_band_setting(b, AUDIOHW_EQ_WIDTH);
766 if (setting != -1)
767 sound_set(setting, global_settings.hw_eq_bands[b].width);
768 #endif /* AUDIOHW_HAVE_EQ_WIDTH */
770 #endif
773 void settings_apply(bool read_disk)
775 #ifdef HAVE_LCD_BITMAP
776 int rc;
777 #endif
778 #if CONFIG_CODEC == SWCODEC
779 int i;
780 #endif
781 sound_settings_apply();
783 #ifdef HAVE_DISK_STORAGE
784 audio_set_buffer_margin(global_settings.buffer_margin);
785 #endif
787 #ifdef HAVE_LCD_CONTRAST
788 lcd_set_contrast(global_settings.contrast);
789 #endif
790 lcd_scroll_speed(global_settings.scroll_speed);
791 #ifdef HAVE_REMOTE_LCD
792 lcd_remote_set_contrast(global_settings.remote_contrast);
793 lcd_remote_set_invert_display(global_settings.remote_invert);
795 #ifdef HAVE_LCD_FLIP
796 lcd_remote_set_flip(global_settings.remote_flip_display);
797 #endif
799 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
800 lcd_remote_scroll_step(global_settings.remote_scroll_step);
801 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
802 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
803 #ifdef HAVE_REMOTE_LCD_TICKING
804 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
805 #endif
806 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
807 #if CONFIG_CHARGING
808 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
809 #endif
810 #ifdef HAS_REMOTE_BUTTON_HOLD
811 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
812 #endif
813 #endif /* HAVE_REMOTE_LCD */
814 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
815 backlight_set_brightness(global_settings.brightness);
816 #endif
817 #ifdef HAVE_BACKLIGHT
818 backlight_set_timeout(global_settings.backlight_timeout);
819 #if CONFIG_CHARGING
820 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
821 #endif
822 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
823 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
824 backlight_set_fade_in(global_settings.backlight_fade_in);
825 backlight_set_fade_out(global_settings.backlight_fade_out);
826 #endif
827 #endif
828 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
829 buttonlight_set_brightness(global_settings.buttonlight_brightness);
830 #endif
831 #ifdef HAVE_BUTTON_LIGHT
832 buttonlight_set_timeout(global_settings.buttonlight_timeout);
833 #endif
834 #ifdef HAVE_DISK_STORAGE
835 storage_spindown(global_settings.disk_spindown);
836 #endif
837 #if (CONFIG_CODEC == MAS3507D) && (CONFIG_PLATFORM & PLATFORM_NATIVE)
838 dac_line_in(global_settings.line_in);
839 #endif
840 set_poweroff_timeout(global_settings.poweroff);
842 #if defined(BATTERY_CAPACITY_INC) && BATTERY_CAPACITY_INC > 0
843 /* only call if it's really exchangable */
844 set_battery_capacity(global_settings.battery_capacity);
845 #endif
847 #if BATTERY_TYPES_COUNT > 1
848 set_battery_type(global_settings.battery_type);
849 #endif
851 #ifdef HAVE_LCD_BITMAP
852 #ifdef HAVE_LCD_INVERT
853 lcd_set_invert_display(global_settings.invert);
854 #endif
855 #ifdef HAVE_LCD_FLIP
856 lcd_set_flip(global_settings.flip_display);
857 button_set_flip(global_settings.flip_display);
858 #endif
859 lcd_update(); /* refresh after flipping the screen */
860 settings_apply_pm_range();
861 peak_meter_init_times(
862 global_settings.peak_meter_release, global_settings.peak_meter_hold,
863 global_settings.peak_meter_clip_hold);
864 #endif
866 #ifdef HAVE_SPEAKER
867 audiohw_enable_speaker(global_settings.speaker_enabled);
868 #endif
870 if (read_disk)
872 char buf[MAX_PATH];
873 #ifdef HAVE_LCD_BITMAP
874 /* fonts need to be loaded before the WPS */
875 if (global_settings.font_file[0]
876 && global_settings.font_file[0] != '-') {
878 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
879 global_settings.font_file);
880 CHART2(">font_load ", global_settings.font_file);
881 rc = font_load(NULL, buf);
882 CHART2("<font_load ", global_settings.font_file);
883 if (rc < 0)
884 font_reset(NULL);
886 else
887 font_reset(NULL);
888 #ifdef HAVE_REMOTE_LCD
889 if ( global_settings.remote_font_file[0]
890 && global_settings.remote_font_file[0] != '-') {
891 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
892 global_settings.remote_font_file);
893 CHART2(">font_load_remoteui ", global_settings.remote_font_file);
894 rc = font_load_remoteui(buf);
895 CHART2("<font_load_remoteui ", global_settings.remote_font_file);
896 if (rc < 0)
897 font_load_remoteui(NULL);
899 else
900 font_load_remoteui(NULL);
901 #endif
902 if ( global_settings.kbd_file[0]
903 && global_settings.kbd_file[0] != '-') {
904 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
905 global_settings.kbd_file);
906 CHART(">load_kbd");
907 load_kbd(buf);
908 CHART("<load_kbd");
910 else
911 load_kbd(NULL);
912 #endif /* HAVE_LCD_BITMAP */
913 if ( global_settings.lang_file[0]) {
914 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
915 global_settings.lang_file);
916 CHART(">lang_core_load");
917 lang_core_load(buf);
918 CHART("<lang_core_load");
919 CHART(">talk_init");
920 talk_init(); /* use voice of same language */
921 CHART("<talk_init");
924 /* load the icon set */
925 CHART(">icons_init");
926 icons_init();
927 CHART("<icons_init");
929 #ifdef HAVE_LCD_COLOR
930 if (global_settings.colors_file[0]
931 && global_settings.colors_file[0] != '-')
933 CHART(">read_color_theme_file");
934 read_color_theme_file();
935 CHART("<read_color_theme_file");
937 #endif
939 #ifdef HAVE_LCD_COLOR
940 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
941 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
942 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
943 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
944 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
945 #endif
947 #ifdef HAVE_LCD_BITMAP
948 lcd_scroll_step(global_settings.scroll_step);
949 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
950 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
951 #endif
952 lcd_bidir_scroll(global_settings.bidir_limit);
953 lcd_scroll_delay(global_settings.scroll_delay);
956 CHART(">set_codepage");
957 set_codepage(global_settings.default_codepage);
958 CHART("<set_codepage");
960 #if CONFIG_CODEC == SWCODEC
961 #ifdef HAVE_CROSSFADE
962 audio_set_crossfade(global_settings.crossfade);
963 #endif
964 dsp_set_replaygain();
965 dsp_set_crossfeed(global_settings.crossfeed);
966 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
967 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
968 global_settings.crossfeed_hf_attenuation,
969 global_settings.crossfeed_hf_cutoff);
971 /* Configure software equalizer, hardware eq is handled in audio_init() */
972 dsp_set_eq(global_settings.eq_enabled);
973 dsp_set_eq_precut(global_settings.eq_precut);
974 for(i = 0; i < 5; i++) {
975 dsp_set_eq_coefs(i);
978 dsp_dither_enable(global_settings.dithering_enabled);
979 #ifdef HAVE_PITCHSCREEN
980 dsp_timestretch_enable(global_settings.timestretch_enabled);
981 #endif
982 dsp_set_compressor(global_settings.compressor_threshold,
983 global_settings.compressor_makeup_gain,
984 global_settings.compressor_ratio,
985 global_settings.compressor_knee,
986 global_settings.compressor_release_time);
987 #endif
989 #ifdef HAVE_SPDIF_POWER
990 spdif_power_enable(global_settings.spdif_enable);
991 #endif
993 #ifdef HAVE_BACKLIGHT
994 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
995 #ifdef HAVE_REMOTE_LCD
996 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
997 #endif
998 #ifdef HAS_BUTTON_HOLD
999 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
1000 #endif
1001 #ifdef HAVE_LCD_SLEEP_SETTING
1002 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
1003 #endif
1004 #endif /* HAVE_BACKLIGHT */
1006 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
1007 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
1008 #endif
1010 #ifdef HAVE_USB_CHARGING_ENABLE
1011 usb_charging_enable(global_settings.usb_charging);
1012 #endif
1014 #ifdef HAVE_TOUCHSCREEN
1015 touchscreen_set_mode(global_settings.touch_mode);
1016 memcpy(&calibration_parameters, &global_settings.ts_calibration_data, sizeof(struct touchscreen_parameter));
1017 #endif
1019 /* This should stay last */
1020 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1021 enc_global_settings_apply();
1022 #endif
1023 #ifdef HAVE_LCD_BITMAP
1024 /* already called with THEME_STATUSBAR in settings_apply_skins() */
1025 CHART(">viewportmanager_theme_changed");
1026 viewportmanager_theme_changed(THEME_UI_VIEWPORT|THEME_LANGUAGE|THEME_BUTTONBAR);
1027 CHART("<viewportmanager_theme_changed");
1028 #endif
1033 * reset all settings to their default value
1035 void reset_setting(const struct settings_list *setting, void *var)
1037 switch (setting->flags&F_T_MASK)
1039 case F_T_CUSTOM:
1040 setting->custom_setting->set_default(setting->setting,
1041 setting->default_val.custom);
1042 break;
1043 case F_T_INT:
1044 case F_T_UINT:
1045 if (setting->flags&F_DEF_ISFUNC)
1046 *(int*)var = setting->default_val.func();
1047 else if (setting->flags&F_T_SOUND)
1048 *(int*)var = sound_default(setting->sound_setting->setting);
1049 else *(int*)var = setting->default_val.int_;
1050 break;
1051 case F_T_BOOL:
1052 *(bool*)var = setting->default_val.bool_;
1053 break;
1054 case F_T_CHARPTR:
1055 case F_T_UCHARPTR:
1056 strlcpy((char*)var, setting->default_val.charptr,
1057 setting->filename_setting->max_len);
1058 break;
1062 void settings_reset(void)
1064 int i;
1066 for(i=0; i<nb_settings; i++)
1067 reset_setting(&settings[i], settings[i].setting);
1068 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1069 enc_global_settings_reset();
1070 #endif
1073 /** Changing setting values **/
1074 const struct settings_list* find_setting(const void* variable, int *id)
1076 int i;
1077 for(i=0;i<nb_settings;i++)
1079 if (settings[i].setting == variable)
1081 if (id)
1082 *id = i;
1083 return &settings[i];
1086 return NULL;
1089 bool set_bool(const char* string, const bool* variable )
1091 return set_bool_options(string, variable,
1092 (char *)STR(LANG_SET_BOOL_YES),
1093 (char *)STR(LANG_SET_BOOL_NO),
1094 NULL);
1098 bool set_bool_options(const char* string, const bool* variable,
1099 const char* yes_str, int yes_voice,
1100 const char* no_str, int no_voice,
1101 void (*function)(bool))
1103 struct opt_items names[] = {
1104 {(unsigned const char *)no_str, no_voice},
1105 {(unsigned const char *)yes_str, yes_voice}
1107 bool result;
1109 result = set_option(string, variable, BOOL, names, 2,
1110 (void (*)(int))function);
1111 return result;
1114 bool set_int(const unsigned char* string,
1115 const char* unit,
1116 int voice_unit,
1117 const int* variable,
1118 void (*function)(int),
1119 int step,
1120 int min,
1121 int max,
1122 const char* (*formatter)(char*, size_t, int, const char*) )
1124 return set_int_ex(string, unit, voice_unit, variable, function,
1125 step, min, max, formatter, NULL);
1128 bool set_int_ex(const unsigned char* string,
1129 const char* unit,
1130 int voice_unit,
1131 const int* variable,
1132 void (*function)(int),
1133 int step,
1134 int min,
1135 int max,
1136 const char* (*formatter)(char*, size_t, int, const char*),
1137 int32_t (*get_talk_id)(int, int))
1139 (void)unit;
1140 struct settings_list item;
1141 struct int_setting data = {
1142 function, voice_unit, min, max, step,
1143 formatter, get_talk_id
1145 item.int_setting = &data;
1146 item.flags = F_INT_SETTING|F_T_INT;
1147 item.lang_id = -1;
1148 item.cfg_vals = (char*)string;
1149 item.setting = (void *)variable;
1150 return option_screen(&item, NULL, false, NULL);
1154 static const struct opt_items *set_option_options;
1155 static const char* set_option_formatter(char* buf, size_t size, int item, const char* unit)
1157 (void)buf, (void)unit, (void)size;
1158 return P2STR(set_option_options[item].string);
1161 static int32_t set_option_get_talk_id(int value, int unit)
1163 (void)unit;
1164 return set_option_options[value].voice_id;
1167 bool set_option(const char* string, const void* variable, enum optiontype type,
1168 const struct opt_items* options,
1169 int numoptions, void (*function)(int))
1171 int temp;
1172 struct settings_list item;
1173 struct int_setting data = {
1174 function, UNIT_INT, 0, numoptions-1, 1,
1175 set_option_formatter, set_option_get_talk_id
1177 set_option_options = options;
1178 item.int_setting = &data;
1179 item.flags = F_INT_SETTING|F_T_INT;
1180 item.lang_id = -1;
1181 item.cfg_vals = (char*)string;
1182 item.setting = &temp;
1183 if (type == BOOL)
1184 temp = *(bool*)variable? 1: 0;
1185 else
1186 temp = *(int*)variable;
1187 if (!option_screen(&item, NULL, false, NULL))
1189 if (type == BOOL)
1191 *(bool*)variable = (temp == 1);
1192 else
1193 *(int*)variable = temp;
1194 return false;
1196 return true;
1200 * Takes filename, removes the directory and the extension,
1201 * and then copies the basename into setting, unless the basename exceeds maxlen
1203 void set_file(const char* filename, char* setting, const int maxlen)
1205 const char* fptr = strrchr(filename,'/');
1206 const char* extptr;
1207 int len;
1208 int extlen = 0;
1210 if (!fptr)
1211 return;
1213 fptr++;
1215 extptr = strrchr(fptr, '.');
1217 if (!extptr || extptr < fptr)
1218 extlen = 0;
1219 else
1220 extlen = strlen(extptr);
1222 len = strlen(fptr) - extlen + 1;
1224 /* error if filename isn't in ROCKBOX_DIR */
1225 if (len > maxlen)
1226 return;
1228 strlcpy(setting, fptr, len);
1229 settings_save();