rbutilQt: renamed installbl to installbootloaderwindow.
[Rockbox.git] / apps / settings.c
blob8b826066a1a370c106e526eca93e666af18eeb63
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by wavey@wavey.org
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include "inttypes.h"
25 #include "config.h"
26 #include "action.h"
27 #include "crc32.h"
28 #include "settings.h"
29 #include "debug.h"
30 #include "usb.h"
31 #include "backlight.h"
32 #include "audio.h"
33 #include "mpeg.h"
34 #include "talk.h"
35 #include "string.h"
36 #include "rtc.h"
37 #include "power.h"
38 #include "ata_idle_notify.h"
39 #include "atoi.h"
40 #include "screens.h"
41 #include "ctype.h"
42 #include "file.h"
43 #include "system.h"
44 #include "misc.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #include "font.h"
48 #include "peakmeter.h"
49 #endif
50 #include "lang.h"
51 #include "language.h"
52 #include "gwps.h"
53 #include "powermgmt.h"
54 #include "sprintf.h"
55 #include "keyboard.h"
56 #include "version.h"
57 #include "sound.h"
58 #include "rbunicode.h"
59 #include "dircache.h"
60 #include "statusbar.h"
61 #include "splash.h"
62 #include "list.h"
63 #include "settings_list.h"
64 #include "filetypes.h"
66 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
67 #include "backdrop.h"
68 #endif
70 #if CONFIG_TUNER
71 #include "radio.h"
72 #endif
74 #if CONFIG_CODEC == MAS3507D
75 void dac_line_in(bool enable);
76 #endif
77 struct user_settings global_settings;
78 struct system_status global_status;
80 #if CONFIG_CODEC == SWCODEC
81 #include "pcmbuf.h"
82 #include "pcm_playback.h"
83 #include "dsp.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) && (var_count>0) && (buf_pos<max_len); i++)
148 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
149 >>F_NVRAM_MASK_SHIFT;
150 if (nvram_bytes)
152 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
153 buf_pos += nvram_bytes;
154 var_count--;
157 return true;
159 static bool write_nvram_data(char* buf, int max_len)
161 unsigned crc32 = 0xffffffff;
162 int i = 0, buf_pos = 0;
163 char var_count = 0;
164 #ifndef HAVE_RTC_RAM
165 int fd;
166 #endif
167 memset(buf,0,max_len);
168 /* magic, version */
169 buf[0] = 'R'; buf[1] = 'b';
170 buf[2] = NVRAM_CONFIG_VERSION;
171 buf_pos = NVRAM_DATA_START;
172 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
174 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
175 >>F_NVRAM_MASK_SHIFT;
176 if (nvram_bytes)
178 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
179 buf_pos += nvram_bytes;
180 var_count++;
183 /* count and crc32 */
184 buf[3] = var_count;
185 crc32 = crc_32(&buf[NVRAM_DATA_START],
186 max_len-NVRAM_DATA_START-1,0xffffffff);
187 memcpy(&buf[4],&crc32,4);
188 #ifndef HAVE_RTC_RAM
189 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
190 if (fd >= 0)
192 int len = write(fd,buf,max_len);
193 close(fd);
194 if (len < 8)
195 return false;
197 #else
198 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
199 that it would write a number of bytes at a time since the RTC chip
200 supports that, but this will have to do for now 8-) */
201 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
202 int r = rtc_write(0x14+i, buf[i]);
203 if (r) {
204 DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n",
205 14+i, r );
206 return false;
209 #endif
210 return true;
213 /** Reading from a config file **/
215 * load settings from disk or RTC RAM
217 void settings_load(int which)
219 DEBUGF( "reload_all_settings()\n" );
220 if (which&SETTINGS_RTC)
221 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
222 if (which&SETTINGS_HD)
224 settings_load_config(CONFIGFILE,false);
225 settings_load_config(FIXEDSETTINGSFILE,false);
229 static bool cfg_string_to_int(int setting_id, int* out, char* str)
231 const char* start = settings[setting_id].cfg_vals;
232 char* end = NULL;
233 char temp[MAX_PATH];
234 int count = 0;
235 while (1)
237 end = strchr(start, ',');
238 if (!end)
240 if (!strcmp(str, start))
242 *out = count;
243 return true;
245 else return false;
247 strncpy(temp, start, end-start);
248 temp[end-start] = '\0';
249 if (!strcmp(str, temp))
251 *out = count;
252 return true;
254 start = end +1;
255 count++;
257 return false;
260 bool settings_load_config(const char* file, bool apply)
262 int fd;
263 char line[128];
264 char* name;
265 char* value;
266 int i;
267 fd = open(file, O_RDONLY);
268 if (fd < 0)
269 return false;
271 while (read_line(fd, line, sizeof line) > 0)
273 if (!settings_parseline(line, &name, &value))
274 continue;
275 for(i=0; i<nb_settings; i++)
277 if (settings[i].cfg_name == NULL)
278 continue;
279 if (!strcasecmp(name,settings[i].cfg_name))
281 switch (settings[i].flags&F_T_MASK)
283 case F_T_INT:
284 case F_T_UINT:
285 #ifdef HAVE_LCD_COLOR
286 if (settings[i].flags&F_RGB)
287 *(int*)settings[i].setting = hex_to_rgb(value);
288 else
289 #endif
290 if (settings[i].cfg_vals == NULL)
292 *(int*)settings[i].setting = atoi(value);
294 else
296 cfg_string_to_int(i,(int*)settings[i].setting,value);
298 break;
299 case F_T_BOOL:
301 int temp;
302 if (cfg_string_to_int(i,&temp,value))
303 *(bool*)settings[i].setting = (temp==0?false:true);
304 break;
306 case F_T_CHARPTR:
307 case F_T_UCHARPTR:
309 char storage[MAX_PATH];
310 if (settings[i].filename_setting->prefix)
312 int len = strlen(settings[i].filename_setting->prefix);
313 if (!strncasecmp(value,
314 settings[i].filename_setting->prefix,
315 len))
317 strncpy(storage,&value[len],MAX_PATH);
319 else strncpy(storage,value,MAX_PATH);
321 else strncpy(storage,value,MAX_PATH);
322 if (settings[i].filename_setting->suffix)
324 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
325 if (s) *s = '\0';
327 strncpy((char*)settings[i].setting,storage,
328 settings[i].filename_setting->max_len);
329 ((char*)settings[i].setting)
330 [settings[i].filename_setting->max_len-1] = '\0';
331 break;
334 break;
335 } /* if (!strcmp(name,settings[i].cfg_name)) */
336 } /* for(...) */
337 } /* while(...) */
339 close(fd);
340 settings_save();
341 if (apply)
342 settings_apply();
343 return true;
346 /** Writing to a config file and saving settings **/
348 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
350 const char* start = settings[setting_id].cfg_vals;
351 char* end = NULL;
352 int count = 0;
353 while (count < val)
355 start = strchr(start,',');
356 if (!start)
357 return false;
358 count++;
359 start++;
361 end = strchr(start,',');
362 if (end == NULL)
363 strncpy(buf, start, buf_len);
364 else
366 int len = (buf_len > (end-start))? end-start: buf_len;
367 strncpy(buf, start, len);
368 buf[len] = '\0';
370 return true;
372 static bool is_changed(int setting_id)
374 const struct settings_list *setting = &settings[setting_id];
375 switch (setting->flags&F_T_MASK)
377 case F_T_INT:
378 case F_T_UINT:
379 if (setting->flags&F_DEF_ISFUNC)
381 if (*(int*)setting->setting == setting->default_val.func())
382 return false;
384 else if (setting->flags&F_T_SOUND)
386 if (*(int*)setting->setting ==
387 sound_default(setting->sound_setting->setting))
388 return false;
390 else if (*(int*)setting->setting == setting->default_val.int_)
391 return false;
392 break;
393 case F_T_BOOL:
394 if (*(bool*)setting->setting == setting->default_val.bool_)
395 return false;
396 break;
397 case F_T_CHARPTR:
398 case F_T_UCHARPTR:
399 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
400 return false;
401 break;
403 return true;
406 static bool settings_write_config(char* filename, int options)
408 int i;
409 int fd;
410 char value[MAX_PATH];
411 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
412 if (fd < 0)
413 return false;
414 fdprintf(fd, "# .cfg file created by rockbox %s - "
415 "http://www.rockbox.org\r\n\r\n", appsversion);
416 for(i=0; i<nb_settings; i++)
418 if (settings[i].cfg_name == NULL)
419 continue;
420 value[0] = '\0';
422 if ((options == SETTINGS_SAVE_CHANGED) &&
423 !is_changed(i))
424 continue;
425 else if ((options == SETTINGS_SAVE_THEME) &&
426 ((settings[i].flags&F_THEMESETTING) == 0))
427 continue;
428 #ifdef HAVE_RECORDING
429 else if ((options == SETTINGS_SAVE_RECPRESETS) &&
430 ((settings[i].flags&F_RECSETTING) == 0))
431 continue;
432 #endif
433 switch (settings[i].flags&F_T_MASK)
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(value,MAX_PATH,"%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(value,MAX_PATH,"%d",*(int*)settings[i].setting);
452 else
454 cfg_int_to_string(i, *(int*)settings[i].setting,
455 value, MAX_PATH);
457 break;
458 case F_T_BOOL:
459 cfg_int_to_string(i,
460 *(bool*)settings[i].setting==false?0:1, value, MAX_PATH);
461 break;
462 case F_T_CHARPTR:
463 case F_T_UCHARPTR:
464 if (((char*)settings[i].setting)[0] == '\0')
465 break;
466 if (settings[i].filename_setting->prefix)
468 snprintf(value,MAX_PATH,"%s%s%s",
469 settings[i].filename_setting->prefix,
470 (char*)settings[i].setting,
471 settings[i].filename_setting->suffix);
473 else strncpy(value,(char*)settings[i].setting,
474 settings[i].filename_setting->max_len);
475 break;
476 } /* switch () */
477 if (value[0])
478 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
479 } /* for(...) */
480 close(fd);
481 return true;
483 #ifndef HAVE_RTC_RAM
484 static bool flush_global_status_callback(void)
486 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
488 #endif
489 static bool flush_config_block_callback(void)
491 bool r1, r2;
492 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
493 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
494 return r1 || r2;
498 * persist all runtime user settings to disk or RTC RAM
500 static void update_runtime(void)
502 int elapsed_secs;
504 elapsed_secs = (current_tick - lasttime) / HZ;
505 global_status.runtime += elapsed_secs;
506 lasttime += (elapsed_secs * HZ);
508 if ( global_status.runtime > global_status.topruntime )
509 global_status.topruntime = global_status.runtime;
512 void status_save( void )
514 update_runtime();
515 #ifdef HAVE_RTC_RAM
516 /* this will be done in the ata_callback if
517 target doesnt have rtc ram */
518 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
519 #else
520 register_ata_idle_func(flush_global_status_callback);
521 #endif
524 int settings_save( void )
526 update_runtime();
527 #ifdef HAVE_RTC_RAM
528 /* this will be done in the ata_callback if
529 target doesnt have rtc ram */
530 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
531 #endif
532 if(!register_ata_idle_func(flush_config_block_callback))
534 int i;
535 FOR_NB_SCREENS(i)
537 screens[i].clear_display();
538 #ifdef HAVE_LCD_CHARCELLS
539 screens[i].puts(0, 0, str(LANG_SETTINGS_SAVE_FAILED));
540 screens[i].puts(0, 1, str(LANG_SETTINGS_PARTITION));
541 #else
542 screens[i].puts(4, 2, str(LANG_SETTINGS_SAVE_FAILED));
543 screens[i].puts(2, 4, str(LANG_SETTINGS_PARTITION));
544 screens[i].update();
545 #endif
547 cond_talk_ids_fq(LANG_SETTINGS_SAVE_FAILED);
548 sleep(HZ*2);
549 return -1;
551 return 0;
553 bool settings_save_config(int options)
555 char filename[MAX_PATH];
556 char *folder;
557 switch (options)
559 case SETTINGS_SAVE_THEME:
560 folder = THEME_DIR;
561 break;
562 #ifdef HAVE_RECORDING
563 case SETTINGS_SAVE_RECPRESETS:
564 folder = RECPRESETS_DIR;
565 break;
566 #endif
567 default:
568 folder = ROCKBOX_DIR;
570 create_numbered_filename(filename, folder, "config", ".cfg", 2
571 IF_CNFN_NUM_(, NULL));
573 /* allow user to modify filename */
574 while (true) {
575 if (!kbd_input(filename, sizeof filename)) {
576 break;
578 else {
579 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
580 return false;
584 if (settings_write_config(filename, options))
585 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_SAVED));
586 else
587 gui_syncsplash(HZ, ID2P(LANG_FAILED));
588 return true;
591 /** Apply and Reset settings **/
594 #ifdef HAVE_LCD_BITMAP
596 * Applies the range infos stored in global_settings to
597 * the peak meter.
599 void settings_apply_pm_range(void)
601 int pm_min, pm_max;
603 /* depending on the scale mode (dBfs or percent) the values
604 of global_settings.peak_meter_dbfs have different meanings */
605 if (global_settings.peak_meter_dbfs)
607 /* convert to dBfs * 100 */
608 pm_min = -(((int)global_settings.peak_meter_min) * 100);
609 pm_max = -(((int)global_settings.peak_meter_max) * 100);
611 else
613 /* percent is stored directly -> no conversion */
614 pm_min = global_settings.peak_meter_min;
615 pm_max = global_settings.peak_meter_max;
618 /* apply the range */
619 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
621 #endif /* HAVE_LCD_BITMAP */
623 void sound_settings_apply(void)
625 #if CONFIG_CODEC == SWCODEC
626 sound_set_dsp_callback(dsp_callback);
627 #endif
628 sound_set(SOUND_BASS, global_settings.bass);
629 sound_set(SOUND_TREBLE, global_settings.treble);
630 sound_set(SOUND_BALANCE, global_settings.balance);
631 sound_set(SOUND_VOLUME, global_settings.volume);
632 sound_set(SOUND_CHANNELS, global_settings.channel_config);
633 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
634 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
635 sound_set(SOUND_LOUDNESS, global_settings.loudness);
636 sound_set(SOUND_AVC, global_settings.avc);
637 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
638 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
639 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
640 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
641 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
642 sound_set(SOUND_SUPERBASS, global_settings.superbass);
643 #endif
645 #ifdef HAVE_USB_POWER
646 #if CONFIG_CHARGING
647 usb_charging_enable(global_settings.usb_charging);
648 #endif
649 #endif
652 void settings_apply(void)
654 char buf[64];
655 #if CONFIG_CODEC == SWCODEC
656 int i;
657 #endif
659 DEBUGF( "settings_apply()\n" );
660 sound_settings_apply();
662 #ifndef HAVE_FLASH_STORAGE
663 audio_set_buffer_margin(global_settings.buffer_margin);
664 #endif
666 #ifdef HAVE_LCD_CONTRAST
667 lcd_set_contrast(global_settings.contrast);
668 #endif
669 lcd_scroll_speed(global_settings.scroll_speed);
670 #ifdef HAVE_REMOTE_LCD
671 lcd_remote_set_contrast(global_settings.remote_contrast);
672 lcd_remote_set_invert_display(global_settings.remote_invert);
673 lcd_remote_set_flip(global_settings.remote_flip_display);
674 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
675 lcd_remote_scroll_step(global_settings.remote_scroll_step);
676 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
677 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
678 #ifdef HAVE_REMOTE_LCD_TICKING
679 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
680 #endif
681 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
682 #if CONFIG_CHARGING
683 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
684 #endif
685 #ifdef HAS_REMOTE_BUTTON_HOLD
686 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
687 #endif
688 #endif /* HAVE_REMOTE_LCD */
689 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
690 backlight_set_brightness(global_settings.brightness);
691 #endif
692 #ifdef HAVE_BACKLIGHT
693 backlight_set_timeout(global_settings.backlight_timeout);
694 #if CONFIG_CHARGING
695 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
696 #endif
697 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
698 backlight_set_fade_in(global_settings.backlight_fade_in);
699 backlight_set_fade_out(global_settings.backlight_fade_out);
700 #endif
701 #endif
702 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
703 buttonlight_set_brightness(global_settings.buttonlight_brightness);
704 #endif
705 #ifdef HAVE_BUTTON_LIGHT
706 button_backlight_set_timeout(global_settings.button_light_timeout);
707 #endif
708 #ifndef HAVE_FLASH_STORAGE
709 ata_spindown(global_settings.disk_spindown);
710 #endif
711 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
712 dac_line_in(global_settings.line_in);
713 #endif
714 mpeg_id3_options(global_settings.id3_v1_first);
716 set_poweroff_timeout(global_settings.poweroff);
718 set_battery_capacity(global_settings.battery_capacity);
719 #if BATTERY_TYPES_COUNT > 1
720 set_battery_type(global_settings.battery_type);
721 #endif
723 #ifdef HAVE_LCD_BITMAP
724 lcd_set_invert_display(global_settings.invert);
725 lcd_set_flip(global_settings.flip_display);
726 button_set_flip(global_settings.flip_display);
727 lcd_update(); /* refresh after flipping the screen */
728 settings_apply_pm_range();
729 peak_meter_init_times(
730 global_settings.peak_meter_release, global_settings.peak_meter_hold,
731 global_settings.peak_meter_clip_hold);
732 #endif
734 #if LCD_DEPTH > 1
735 unload_wps_backdrop();
736 #endif
737 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
738 unload_remote_wps_backdrop();
739 #endif
740 if ( global_settings.wps_file[0] &&
741 global_settings.wps_file[0] != 0xff ) {
742 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
743 global_settings.wps_file);
744 wps_data_load(gui_wps[0].data, buf, true);
746 else
748 wps_data_init(gui_wps[0].data);
749 #ifdef HAVE_REMOTE_LCD
750 gui_wps[0].data->remote_wps = false;
751 #endif
754 #if LCD_DEPTH > 1
755 if ( global_settings.backdrop_file[0] &&
756 global_settings.backdrop_file[0] != 0xff ) {
757 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
758 global_settings.backdrop_file);
759 load_main_backdrop(buf);
760 } else {
761 unload_main_backdrop();
763 show_main_backdrop();
764 #endif
765 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
766 show_remote_main_backdrop();
767 #endif
769 #ifdef HAVE_LCD_COLOR
770 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
771 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
772 #endif
774 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
775 if ( global_settings.rwps_file[0]) {
776 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
777 global_settings.rwps_file);
778 wps_data_load(gui_wps[1].data, buf, true);
780 else
782 wps_data_init(gui_wps[1].data);
783 gui_wps[1].data->remote_wps = true;
785 #endif
787 #ifdef HAVE_LCD_BITMAP
788 if ( global_settings.font_file[0]) {
789 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
790 global_settings.font_file);
791 font_load(buf);
793 else
794 font_reset();
796 if ( global_settings.kbd_file[0]) {
797 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
798 global_settings.kbd_file);
799 load_kbd(buf);
801 else
802 load_kbd(NULL);
804 lcd_scroll_step(global_settings.scroll_step);
805 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
806 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
807 #else
808 lcd_jump_scroll(global_settings.jump_scroll);
809 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
810 #endif
811 lcd_bidir_scroll(global_settings.bidir_limit);
812 lcd_scroll_delay(global_settings.scroll_delay);
814 if ( global_settings.lang_file[0]) {
815 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
816 global_settings.lang_file);
817 lang_load(buf);
818 talk_init(); /* use voice of same language */
821 set_codepage(global_settings.default_codepage);
823 #if CONFIG_CODEC == SWCODEC
824 audio_set_crossfade(global_settings.crossfade);
825 dsp_set_replaygain();
826 dsp_set_crossfeed(global_settings.crossfeed);
827 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
828 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
829 global_settings.crossfeed_hf_attenuation,
830 global_settings.crossfeed_hf_cutoff);
832 /* Configure software equalizer, hardware eq is handled in audio_init() */
833 dsp_set_eq(global_settings.eq_enabled);
834 dsp_set_eq_precut(global_settings.eq_precut);
835 for(i = 0; i < 5; i++) {
836 dsp_set_eq_coefs(i);
839 dsp_dither_enable(global_settings.dithering_enabled);
840 #endif
842 #ifdef HAVE_SPDIF_POWER
843 spdif_power_enable(global_settings.spdif_enable);
844 #endif
846 #ifdef HAVE_BACKLIGHT
847 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
848 #ifdef HAVE_REMOTE_LCD
849 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
850 #endif
851 #ifdef HAS_BUTTON_HOLD
852 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
853 #endif
854 #ifdef HAVE_LCD_SLEEP
855 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
856 #endif
857 #endif /* HAVE_BACKLIGHT */
859 /* This should stay last */
860 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
861 enc_global_settings_apply();
862 #endif
863 /* load the icon set */
864 icons_init();
866 #ifdef HAVE_LCD_COLOR
867 if (global_settings.colors_file)
868 read_color_theme_file();
869 #endif
877 * reset all settings to their default value
879 void settings_reset(void) {
881 int i;
882 DEBUGF( "settings_reset()\n" );
884 for(i=0; i<nb_settings; i++)
886 switch (settings[i].flags&F_T_MASK)
888 case F_T_INT:
889 case F_T_UINT:
890 if (settings[i].flags&F_DEF_ISFUNC)
891 *(int*)settings[i].setting = settings[i].default_val.func();
892 else if (settings[i].flags&F_T_SOUND)
893 *(int*)settings[i].setting =
894 sound_default(settings[i].sound_setting->setting);
895 else *(int*)settings[i].setting = settings[i].default_val.int_;
896 break;
897 case F_T_BOOL:
898 *(bool*)settings[i].setting = settings[i].default_val.bool_;
899 break;
900 case F_T_CHARPTR:
901 case F_T_UCHARPTR:
902 strncpy((char*)settings[i].setting,
903 settings[i].default_val.charptr,MAX_FILENAME);
904 break;
906 } /* for(...) */
907 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
908 enc_global_settings_reset();
909 #endif
912 /** Changing setting values **/
913 const struct settings_list* find_setting(void* variable, int *id)
915 int i;
916 for(i=0;i<nb_settings;i++)
918 if (settings[i].setting == variable)
920 if (id)
921 *id = i;
922 return &settings[i];
925 return NULL;
928 void talk_setting(void *global_settings_variable)
930 const struct settings_list *setting;
931 if (!talk_menus_enabled())
932 return;
933 setting = find_setting(global_settings_variable, NULL);
934 if (setting == NULL)
935 return;
936 if (setting->lang_id)
937 talk_id(setting->lang_id,false);
940 bool set_bool(const char* string, bool* variable )
942 return set_bool_options(string, variable,
943 (char *)STR(LANG_SET_BOOL_YES),
944 (char *)STR(LANG_SET_BOOL_NO),
945 NULL);
949 bool set_bool_options(const char* string, bool* variable,
950 const char* yes_str, int yes_voice,
951 const char* no_str, int no_voice,
952 void (*function)(bool))
954 struct opt_items names[] = {
955 {(unsigned char *)no_str, no_voice},
956 {(unsigned char *)yes_str, yes_voice}
958 bool result;
960 result = set_option(string, variable, BOOL, names, 2,
961 (void (*)(int))function);
962 return result;
965 bool set_int(const unsigned char* string,
966 const char* unit,
967 int voice_unit,
968 int* variable,
969 void (*function)(int),
970 int step,
971 int min,
972 int max,
973 void (*formatter)(char*, int, int, const char*) )
975 return set_int_ex(string, unit, voice_unit, variable, function,
976 step, min, max, formatter, NULL);
980 /** extra stuff which is probably misplaced **/
982 void set_file(char* filename, char* setting, int maxlen)
984 char* fptr = strrchr(filename,'/');
985 int len;
986 int extlen = 0;
987 char* ptr;
989 if (!fptr)
990 return;
992 *fptr = 0;
993 fptr++;
995 len = strlen(fptr);
996 ptr = fptr + len;
997 while ((*ptr != '.') && (ptr != fptr)) {
998 extlen++;
999 ptr--;
1001 if(ptr == fptr) extlen = 0;
1003 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1004 (len-extlen > maxlen))
1005 return;
1007 strncpy(setting, fptr, len-extlen);
1008 setting[len-extlen]=0;
1010 settings_save();