Increase MAXTHREADS
[Rockbox.git] / apps / settings.c
blobadd2b24c757bf782dbf54352bfdc67efe29407ff
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by wavey@wavey.org
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
20 #include <stdio.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <limits.h>
24 #include "inttypes.h"
25 #include "config.h"
26 #include "action.h"
27 #include "crc32.h"
28 #include "settings.h"
29 #include "debug.h"
30 #include "usb.h"
31 #include "backlight.h"
32 #include "audio.h"
33 #include "mpeg.h"
34 #include "talk.h"
35 #include "string.h"
36 #include "rtc.h"
37 #include "power.h"
38 #include "ata_idle_notify.h"
39 #include "atoi.h"
40 #include "screens.h"
41 #include "ctype.h"
42 #include "file.h"
43 #include "system.h"
44 #include "misc.h"
45 #ifdef HAVE_LCD_BITMAP
46 #include "icons.h"
47 #include "font.h"
48 #include "peakmeter.h"
49 #endif
50 #include "lang.h"
51 #include "language.h"
52 #include "gwps.h"
53 #include "powermgmt.h"
54 #include "sprintf.h"
55 #include "keyboard.h"
56 #include "version.h"
57 #include "sound.h"
58 #include "rbunicode.h"
59 #include "dircache.h"
60 #include "statusbar.h"
61 #include "splash.h"
62 #include "list.h"
63 #include "settings_list.h"
64 #include "filetypes.h"
66 #if (LCD_DEPTH > 1) || (defined(HAVE_LCD_REMOTE) && (LCD_REMOTE_DEPTH > 1))
67 #include "backdrop.h"
68 #endif
70 #if CONFIG_TUNER
71 #include "radio.h"
72 #endif
74 #if CONFIG_CODEC == MAS3507D
75 void dac_line_in(bool enable);
76 #endif
77 struct user_settings global_settings;
78 struct system_status global_status;
80 #if CONFIG_CODEC == SWCODEC
81 #include "pcmbuf.h"
82 #include "dsp.h"
83 #ifdef HAVE_RECORDING
84 #include "enc_config.h"
85 #endif
86 #endif /* CONFIG_CODEC == SWCODEC */
88 #define NVRAM_BLOCK_SIZE 44
90 #ifdef HAVE_LCD_BITMAP
91 #define MAX_LINES 10
92 #else
93 #define MAX_LINES 2
94 #endif
96 #ifdef HAVE_REMOTE_LCD
97 #include "lcd-remote.h"
98 #endif
100 #ifdef HAVE_USBSTACK
101 #include "usbstack.h"
102 #endif
104 long lasttime = 0;
106 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
107 /* NVRAM is set out as
108 [0] 'R'
109 [1] 'b'
110 [2] version
111 [3] stored variable count
112 [4-7] crc32 checksum
113 [8-NVRAM_BLOCK_SIZE] data
115 #define NVRAM_DATA_START 8
116 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
117 static char nvram_buffer[NVRAM_BLOCK_SIZE];
119 static bool read_nvram_data(char* buf, int max_len)
121 unsigned crc32 = 0xffffffff;
122 int var_count = 0, i = 0, buf_pos = 0;
123 #ifndef HAVE_RTC_RAM
124 int fd = open(NVRAM_FILE,O_RDONLY);
125 if (fd < 0)
126 return false;
127 memset(buf,0,max_len);
128 if (read(fd,buf,max_len) < 8) /* min is 8 bytes,magic, ver, vars, crc32 */
129 return false;
130 close(fd);
131 #else
132 memset(buf,0,max_len);
133 /* read rtc block */
134 for (i=0; i < max_len; i++ )
135 buf[i] = rtc_read(0x14+i);
136 #endif
137 /* check magic, version */
138 if ((buf[0] != 'R') || (buf[1] != 'b')
139 || (buf[2] != NVRAM_CONFIG_VERSION))
140 return false;
141 /* check crc32 */
142 crc32 = crc_32(&buf[NVRAM_DATA_START],
143 max_len-NVRAM_DATA_START-1,0xffffffff);
144 if (memcmp(&crc32,&buf[4],4))
145 return false;
146 /* all good, so read in the settings */
147 var_count = buf[3];
148 buf_pos = NVRAM_DATA_START;
149 for(i=0; i<nb_settings; i++)
151 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
152 >>F_NVRAM_MASK_SHIFT;
153 if (nvram_bytes)
155 if ((var_count>0) && (buf_pos<max_len))
157 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
158 buf_pos += nvram_bytes;
159 var_count--;
161 else /* should only happen when new items are added to the end */
163 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
167 return true;
169 static bool write_nvram_data(char* buf, int max_len)
171 unsigned crc32 = 0xffffffff;
172 int i = 0, buf_pos = 0;
173 char var_count = 0;
174 #ifndef HAVE_RTC_RAM
175 int fd;
176 #endif
177 memset(buf,0,max_len);
178 /* magic, version */
179 buf[0] = 'R'; buf[1] = 'b';
180 buf[2] = NVRAM_CONFIG_VERSION;
181 buf_pos = NVRAM_DATA_START;
182 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
184 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
185 >>F_NVRAM_MASK_SHIFT;
186 if (nvram_bytes)
188 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
189 buf_pos += nvram_bytes;
190 var_count++;
193 /* count and crc32 */
194 buf[3] = var_count;
195 crc32 = crc_32(&buf[NVRAM_DATA_START],
196 max_len-NVRAM_DATA_START-1,0xffffffff);
197 memcpy(&buf[4],&crc32,4);
198 #ifndef HAVE_RTC_RAM
199 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
200 if (fd >= 0)
202 int len = write(fd,buf,max_len);
203 close(fd);
204 if (len < 8)
205 return false;
207 #else
208 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
209 that it would write a number of bytes at a time since the RTC chip
210 supports that, but this will have to do for now 8-) */
211 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
212 int r = rtc_write(0x14+i, buf[i]);
213 if (r) {
214 DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n",
215 14+i, r );
216 return false;
219 #endif
220 return true;
223 /** Reading from a config file **/
225 * load settings from disk or RTC RAM
227 void settings_load(int which)
229 DEBUGF( "reload_all_settings()\n" );
230 if (which&SETTINGS_RTC)
231 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
232 if (which&SETTINGS_HD)
234 settings_load_config(CONFIGFILE,false);
235 settings_load_config(FIXEDSETTINGSFILE,false);
239 static bool cfg_string_to_int(int setting_id, int* out, char* str)
241 const char* start = settings[setting_id].cfg_vals;
242 char* end = NULL;
243 char temp[MAX_PATH];
244 int count = 0;
245 while (1)
247 end = strchr(start, ',');
248 if (!end)
250 if (!strcmp(str, start))
252 *out = count;
253 return true;
255 else return false;
257 strncpy(temp, start, end-start);
258 temp[end-start] = '\0';
259 if (!strcmp(str, temp))
261 *out = count;
262 return true;
264 start = end +1;
265 count++;
267 return false;
270 bool settings_load_config(const char* file, bool apply)
272 int fd;
273 char line[128];
274 char* name;
275 char* value;
276 int i;
277 fd = open(file, O_RDONLY);
278 if (fd < 0)
279 return false;
281 while (read_line(fd, line, sizeof line) > 0)
283 if (!settings_parseline(line, &name, &value))
284 continue;
285 for(i=0; i<nb_settings; i++)
287 if (settings[i].cfg_name == NULL)
288 continue;
289 if (!strcasecmp(name,settings[i].cfg_name))
291 switch (settings[i].flags&F_T_MASK)
293 case F_T_INT:
294 case F_T_UINT:
295 #ifdef HAVE_LCD_COLOR
296 if (settings[i].flags&F_RGB)
297 *(int*)settings[i].setting = hex_to_rgb(value);
298 else
299 #endif
300 if (settings[i].cfg_vals == NULL)
302 *(int*)settings[i].setting = atoi(value);
304 else
306 cfg_string_to_int(i,(int*)settings[i].setting,value);
308 break;
309 case F_T_BOOL:
311 int temp;
312 if (cfg_string_to_int(i,&temp,value))
313 *(bool*)settings[i].setting = (temp==0?false:true);
314 break;
316 case F_T_CHARPTR:
317 case F_T_UCHARPTR:
319 char storage[MAX_PATH];
320 if (settings[i].filename_setting->prefix)
322 int len = strlen(settings[i].filename_setting->prefix);
323 if (!strncasecmp(value,
324 settings[i].filename_setting->prefix,
325 len))
327 strncpy(storage,&value[len],MAX_PATH);
329 else strncpy(storage,value,MAX_PATH);
331 else strncpy(storage,value,MAX_PATH);
332 if (settings[i].filename_setting->suffix)
334 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
335 if (s) *s = '\0';
337 strncpy((char*)settings[i].setting,storage,
338 settings[i].filename_setting->max_len);
339 ((char*)settings[i].setting)
340 [settings[i].filename_setting->max_len-1] = '\0';
341 break;
344 break;
345 } /* if (!strcmp(name,settings[i].cfg_name)) */
346 } /* for(...) */
347 } /* while(...) */
349 close(fd);
350 settings_save();
351 if (apply)
352 settings_apply();
353 return true;
356 /** Writing to a config file and saving settings **/
358 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
360 const char* start = settings[setting_id].cfg_vals;
361 char* end = NULL;
362 int count = 0;
363 while (count < val)
365 start = strchr(start,',');
366 if (!start)
367 return false;
368 count++;
369 start++;
371 end = strchr(start,',');
372 if (end == NULL)
373 strncpy(buf, start, buf_len);
374 else
376 int len = (buf_len > (end-start))? end-start: buf_len;
377 strncpy(buf, start, len);
378 buf[len] = '\0';
380 return true;
382 static bool is_changed(int setting_id)
384 const struct settings_list *setting = &settings[setting_id];
385 switch (setting->flags&F_T_MASK)
387 case F_T_INT:
388 case F_T_UINT:
389 if (setting->flags&F_DEF_ISFUNC)
391 if (*(int*)setting->setting == setting->default_val.func())
392 return false;
394 else if (setting->flags&F_T_SOUND)
396 if (*(int*)setting->setting ==
397 sound_default(setting->sound_setting->setting))
398 return false;
400 else if (*(int*)setting->setting == setting->default_val.int_)
401 return false;
402 break;
403 case F_T_BOOL:
404 if (*(bool*)setting->setting == setting->default_val.bool_)
405 return false;
406 break;
407 case F_T_CHARPTR:
408 case F_T_UCHARPTR:
409 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
410 return false;
411 break;
413 return true;
416 static bool settings_write_config(char* filename, int options)
418 int i;
419 int fd;
420 char value[MAX_PATH];
421 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
422 if (fd < 0)
423 return false;
424 fdprintf(fd, "# .cfg file created by rockbox %s - "
425 "http://www.rockbox.org\r\n\r\n", appsversion);
426 for(i=0; i<nb_settings; i++)
428 if (settings[i].cfg_name == NULL)
429 continue;
430 value[0] = '\0';
432 if ((options == SETTINGS_SAVE_CHANGED) &&
433 !is_changed(i))
434 continue;
435 else if ((options == SETTINGS_SAVE_THEME) &&
436 ((settings[i].flags&F_THEMESETTING) == 0))
437 continue;
438 #ifdef HAVE_RECORDING
439 else if ((options == SETTINGS_SAVE_RECPRESETS) &&
440 ((settings[i].flags&F_RECSETTING) == 0))
441 continue;
442 #endif
443 switch (settings[i].flags&F_T_MASK)
445 case F_T_INT:
446 case F_T_UINT:
447 #ifdef HAVE_LCD_COLOR
448 if (settings[i].flags&F_RGB)
450 int colour = *(int*)settings[i].setting;
451 snprintf(value,MAX_PATH,"%02x%02x%02x",
452 (int)RGB_UNPACK_RED(colour),
453 (int)RGB_UNPACK_GREEN(colour),
454 (int)RGB_UNPACK_BLUE(colour));
456 else
457 #endif
458 if (settings[i].cfg_vals == NULL)
460 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
462 else
464 cfg_int_to_string(i, *(int*)settings[i].setting,
465 value, MAX_PATH);
467 break;
468 case F_T_BOOL:
469 cfg_int_to_string(i,
470 *(bool*)settings[i].setting==false?0:1, value, MAX_PATH);
471 break;
472 case F_T_CHARPTR:
473 case F_T_UCHARPTR:
474 if (((char*)settings[i].setting)[0] == '\0')
475 break;
476 if (settings[i].filename_setting->prefix)
478 snprintf(value,MAX_PATH,"%s%s%s",
479 settings[i].filename_setting->prefix,
480 (char*)settings[i].setting,
481 settings[i].filename_setting->suffix);
483 else strncpy(value,(char*)settings[i].setting,
484 settings[i].filename_setting->max_len);
485 break;
486 } /* switch () */
487 if (value[0])
488 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
489 } /* for(...) */
490 close(fd);
491 return true;
493 #ifndef HAVE_RTC_RAM
494 static bool flush_global_status_callback(void)
496 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
498 #endif
499 static bool flush_config_block_callback(void)
501 bool r1, r2;
502 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
503 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
504 return r1 || r2;
508 * persist all runtime user settings to disk or RTC RAM
510 static void update_runtime(void)
512 int elapsed_secs;
514 elapsed_secs = (current_tick - lasttime) / HZ;
515 global_status.runtime += elapsed_secs;
516 lasttime += (elapsed_secs * HZ);
518 if ( global_status.runtime > global_status.topruntime )
519 global_status.topruntime = global_status.runtime;
522 void status_save( void )
524 update_runtime();
525 #ifdef HAVE_RTC_RAM
526 /* this will be done in the ata_callback if
527 target doesnt have rtc ram */
528 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
529 #else
530 register_ata_idle_func(flush_global_status_callback);
531 #endif
534 int settings_save( void )
536 update_runtime();
537 #ifdef HAVE_RTC_RAM
538 /* this will be done in the ata_callback if
539 target doesnt have rtc ram */
540 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
541 #endif
542 if(!register_ata_idle_func(flush_config_block_callback))
544 int i;
545 FOR_NB_SCREENS(i)
547 screens[i].clear_display();
548 #ifdef HAVE_LCD_CHARCELLS
549 screens[i].puts(0, 0, str(LANG_SETTINGS_SAVE_FAILED));
550 screens[i].puts(0, 1, str(LANG_SETTINGS_PARTITION));
551 #else
552 screens[i].puts(4, 2, str(LANG_SETTINGS_SAVE_FAILED));
553 screens[i].puts(2, 4, str(LANG_SETTINGS_PARTITION));
554 screens[i].update();
555 #endif
557 cond_talk_ids_fq(LANG_SETTINGS_SAVE_FAILED);
558 sleep(HZ*2);
559 return -1;
561 return 0;
563 bool settings_save_config(int options)
565 char filename[MAX_PATH];
566 char *folder;
567 switch (options)
569 case SETTINGS_SAVE_THEME:
570 folder = THEME_DIR;
571 break;
572 #ifdef HAVE_RECORDING
573 case SETTINGS_SAVE_RECPRESETS:
574 folder = RECPRESETS_DIR;
575 break;
576 #endif
577 default:
578 folder = ROCKBOX_DIR;
580 create_numbered_filename(filename, folder, "config", ".cfg", 2
581 IF_CNFN_NUM_(, NULL));
583 /* allow user to modify filename */
584 while (true) {
585 if (!kbd_input(filename, sizeof filename)) {
586 break;
588 else {
589 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
590 return false;
594 if (settings_write_config(filename, options))
595 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_SAVED));
596 else
597 gui_syncsplash(HZ, ID2P(LANG_FAILED));
598 return true;
601 /** Apply and Reset settings **/
604 #ifdef HAVE_LCD_BITMAP
606 * Applies the range infos stored in global_settings to
607 * the peak meter.
609 void settings_apply_pm_range(void)
611 int pm_min, pm_max;
613 /* depending on the scale mode (dBfs or percent) the values
614 of global_settings.peak_meter_dbfs have different meanings */
615 if (global_settings.peak_meter_dbfs)
617 /* convert to dBfs * 100 */
618 pm_min = -(((int)global_settings.peak_meter_min) * 100);
619 pm_max = -(((int)global_settings.peak_meter_max) * 100);
621 else
623 /* percent is stored directly -> no conversion */
624 pm_min = global_settings.peak_meter_min;
625 pm_max = global_settings.peak_meter_max;
628 /* apply the range */
629 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
631 #endif /* HAVE_LCD_BITMAP */
633 void sound_settings_apply(void)
635 #if CONFIG_CODEC == SWCODEC
636 sound_set_dsp_callback(dsp_callback);
637 #endif
638 sound_set(SOUND_BASS, global_settings.bass);
639 sound_set(SOUND_TREBLE, global_settings.treble);
640 sound_set(SOUND_BALANCE, global_settings.balance);
641 sound_set(SOUND_VOLUME, global_settings.volume);
642 sound_set(SOUND_CHANNELS, global_settings.channel_config);
643 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
644 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
645 sound_set(SOUND_LOUDNESS, global_settings.loudness);
646 sound_set(SOUND_AVC, global_settings.avc);
647 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
648 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
649 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
650 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
651 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
652 sound_set(SOUND_SUPERBASS, global_settings.superbass);
653 #endif
655 #ifdef HAVE_USB_POWER
656 #if CONFIG_CHARGING
657 usb_charging_enable(global_settings.usb_charging);
658 #endif
659 #endif
662 void settings_apply(void)
664 char buf[64];
665 #if CONFIG_CODEC == SWCODEC
666 int i;
667 #endif
669 DEBUGF( "settings_apply()\n" );
670 sound_settings_apply();
672 #ifndef HAVE_FLASH_STORAGE
673 audio_set_buffer_margin(global_settings.buffer_margin);
674 #endif
676 #ifdef HAVE_LCD_CONTRAST
677 lcd_set_contrast(global_settings.contrast);
678 #endif
679 lcd_scroll_speed(global_settings.scroll_speed);
680 #ifdef HAVE_REMOTE_LCD
681 lcd_remote_set_contrast(global_settings.remote_contrast);
682 lcd_remote_set_invert_display(global_settings.remote_invert);
683 lcd_remote_set_flip(global_settings.remote_flip_display);
684 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
685 lcd_remote_scroll_step(global_settings.remote_scroll_step);
686 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
687 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
688 #ifdef HAVE_REMOTE_LCD_TICKING
689 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
690 #endif
691 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
692 #if CONFIG_CHARGING
693 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
694 #endif
695 #ifdef HAS_REMOTE_BUTTON_HOLD
696 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
697 #endif
698 #endif /* HAVE_REMOTE_LCD */
699 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
700 backlight_set_brightness(global_settings.brightness);
701 #endif
702 #ifdef HAVE_BACKLIGHT
703 backlight_set_timeout(global_settings.backlight_timeout);
704 #if CONFIG_CHARGING
705 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
706 #endif
707 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
708 backlight_set_fade_in(global_settings.backlight_fade_in);
709 backlight_set_fade_out(global_settings.backlight_fade_out);
710 #endif
711 #endif
712 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
713 buttonlight_set_brightness(global_settings.buttonlight_brightness);
714 #endif
715 #ifdef HAVE_BUTTON_LIGHT
716 buttonlight_set_timeout(global_settings.buttonlight_timeout);
717 #endif
718 #ifndef HAVE_FLASH_STORAGE
719 ata_spindown(global_settings.disk_spindown);
720 #endif
721 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
722 dac_line_in(global_settings.line_in);
723 #endif
724 set_poweroff_timeout(global_settings.poweroff);
726 set_battery_capacity(global_settings.battery_capacity);
727 #if BATTERY_TYPES_COUNT > 1
728 set_battery_type(global_settings.battery_type);
729 #endif
731 #ifdef HAVE_LCD_BITMAP
732 lcd_set_invert_display(global_settings.invert);
733 lcd_set_flip(global_settings.flip_display);
734 button_set_flip(global_settings.flip_display);
735 lcd_update(); /* refresh after flipping the screen */
736 settings_apply_pm_range();
737 peak_meter_init_times(
738 global_settings.peak_meter_release, global_settings.peak_meter_hold,
739 global_settings.peak_meter_clip_hold);
740 #endif
742 #if LCD_DEPTH > 1
743 unload_wps_backdrop();
744 #endif
745 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
746 unload_remote_wps_backdrop();
747 #endif
748 if ( global_settings.wps_file[0] &&
749 global_settings.wps_file[0] != 0xff ) {
750 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
751 global_settings.wps_file);
752 wps_data_load(gui_wps[0].data, buf, true);
754 else
756 wps_data_init(gui_wps[0].data);
757 #ifdef HAVE_REMOTE_LCD
758 gui_wps[0].data->remote_wps = false;
759 #endif
762 #if LCD_DEPTH > 1
763 if ( global_settings.backdrop_file[0] &&
764 global_settings.backdrop_file[0] != 0xff ) {
765 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
766 global_settings.backdrop_file);
767 load_main_backdrop(buf);
768 } else {
769 unload_main_backdrop();
771 show_main_backdrop();
772 #endif
773 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
774 show_remote_main_backdrop();
775 #endif
777 #ifdef HAVE_LCD_COLOR
778 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
779 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
780 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
781 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
782 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
783 #endif
785 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
786 if ( global_settings.rwps_file[0]) {
787 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
788 global_settings.rwps_file);
789 wps_data_load(gui_wps[1].data, buf, true);
791 else
793 wps_data_init(gui_wps[1].data);
794 gui_wps[1].data->remote_wps = true;
796 #endif
798 #ifdef HAVE_LCD_BITMAP
799 if ( global_settings.font_file[0]) {
800 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
801 global_settings.font_file);
802 font_load(buf);
804 else
805 font_reset();
807 if ( global_settings.kbd_file[0]) {
808 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
809 global_settings.kbd_file);
810 load_kbd(buf);
812 else
813 load_kbd(NULL);
815 lcd_scroll_step(global_settings.scroll_step);
816 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
817 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
818 #else
819 lcd_jump_scroll(global_settings.jump_scroll);
820 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
821 #endif
822 lcd_bidir_scroll(global_settings.bidir_limit);
823 lcd_scroll_delay(global_settings.scroll_delay);
825 if ( global_settings.lang_file[0]) {
826 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
827 global_settings.lang_file);
828 lang_load(buf);
829 talk_init(); /* use voice of same language */
832 set_codepage(global_settings.default_codepage);
834 #if CONFIG_CODEC == SWCODEC
835 audio_set_crossfade(global_settings.crossfade);
836 dsp_set_replaygain();
837 dsp_set_crossfeed(global_settings.crossfeed);
838 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
839 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
840 global_settings.crossfeed_hf_attenuation,
841 global_settings.crossfeed_hf_cutoff);
843 /* Configure software equalizer, hardware eq is handled in audio_init() */
844 dsp_set_eq(global_settings.eq_enabled);
845 dsp_set_eq_precut(global_settings.eq_precut);
846 for(i = 0; i < 5; i++) {
847 dsp_set_eq_coefs(i);
850 dsp_dither_enable(global_settings.dithering_enabled);
851 #endif
853 #ifdef HAVE_SPDIF_POWER
854 spdif_power_enable(global_settings.spdif_enable);
855 #endif
857 #ifdef HAVE_BACKLIGHT
858 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
859 #ifdef HAVE_REMOTE_LCD
860 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
861 #endif
862 #ifdef HAS_BUTTON_HOLD
863 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
864 #endif
865 #ifdef HAVE_LCD_SLEEP
866 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
867 #endif
868 #endif /* HAVE_BACKLIGHT */
870 /* This should stay last */
871 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
872 enc_global_settings_apply();
873 #endif
874 /* load the icon set */
875 icons_init();
877 #ifdef HAVE_LCD_COLOR
878 if (global_settings.colors_file)
879 read_color_theme_file();
880 #endif
882 #ifdef HAVE_USBSTACK
884 #if USBSTACK_CAPS == (CONTROLLER_DEVICE|CONTROLLER_HOST)
885 usb_controller_select(global_settings.usb_stack_mode);
886 #elif USBSTACK_CAPS == (CONTROLLER_DEVICE)
887 usb_controller_select(DEVICE);
888 #elif USBSTACK_CAPS == (CONTROLLER_HOST)
889 usb_controller_select(HOST);
890 #endif
892 usb_device_driver_bind(global_settings.usb_stack_device_driver);
893 #endif
898 * reset all settings to their default value
900 void settings_reset(void) {
902 int i;
903 DEBUGF( "settings_reset()\n" );
905 for(i=0; i<nb_settings; i++)
907 switch (settings[i].flags&F_T_MASK)
909 case F_T_INT:
910 case F_T_UINT:
911 if (settings[i].flags&F_DEF_ISFUNC)
912 *(int*)settings[i].setting = settings[i].default_val.func();
913 else if (settings[i].flags&F_T_SOUND)
914 *(int*)settings[i].setting =
915 sound_default(settings[i].sound_setting->setting);
916 else *(int*)settings[i].setting = settings[i].default_val.int_;
917 break;
918 case F_T_BOOL:
919 *(bool*)settings[i].setting = settings[i].default_val.bool_;
920 break;
921 case F_T_CHARPTR:
922 case F_T_UCHARPTR:
923 strncpy((char*)settings[i].setting,
924 settings[i].default_val.charptr,MAX_FILENAME);
925 break;
927 } /* for(...) */
928 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
929 enc_global_settings_reset();
930 #endif
933 /** Changing setting values **/
934 const struct settings_list* find_setting(void* variable, int *id)
936 int i;
937 for(i=0;i<nb_settings;i++)
939 if (settings[i].setting == variable)
941 if (id)
942 *id = i;
943 return &settings[i];
946 return NULL;
949 void talk_setting(void *global_settings_variable)
951 const struct settings_list *setting;
952 if (!global_settings.talk_menu)
953 return;
954 setting = find_setting(global_settings_variable, NULL);
955 if (setting == NULL)
956 return;
957 if (setting->lang_id)
958 talk_id(setting->lang_id,false);
961 bool set_bool(const char* string, bool* variable )
963 return set_bool_options(string, variable,
964 (char *)STR(LANG_SET_BOOL_YES),
965 (char *)STR(LANG_SET_BOOL_NO),
966 NULL);
970 bool set_bool_options(const char* string, bool* variable,
971 const char* yes_str, int yes_voice,
972 const char* no_str, int no_voice,
973 void (*function)(bool))
975 struct opt_items names[] = {
976 {(unsigned char *)no_str, no_voice},
977 {(unsigned char *)yes_str, yes_voice}
979 bool result;
981 result = set_option(string, variable, BOOL, names, 2,
982 (void (*)(int))function);
983 return result;
986 bool set_int(const unsigned char* string,
987 const char* unit,
988 int voice_unit,
989 int* variable,
990 void (*function)(int),
991 int step,
992 int min,
993 int max,
994 void (*formatter)(char*, size_t, int, const char*) )
996 return set_int_ex(string, unit, voice_unit, variable, function,
997 step, min, max, formatter, NULL);
1001 /** extra stuff which is probably misplaced **/
1003 void set_file(char* filename, char* setting, int maxlen)
1005 char* fptr = strrchr(filename,'/');
1006 int len;
1007 int extlen = 0;
1008 char* ptr;
1010 if (!fptr)
1011 return;
1013 *fptr = 0;
1014 fptr++;
1016 len = strlen(fptr);
1017 ptr = fptr + len;
1018 while ((*ptr != '.') && (ptr != fptr)) {
1019 extlen++;
1020 ptr--;
1022 if(ptr == fptr) extlen = 0;
1024 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1025 (len-extlen > maxlen))
1026 return;
1028 strncpy(setting, fptr, len-extlen);
1029 setting[len-extlen]=0;
1031 settings_save();