only build the clipcounter for targets with recording
[maemo-rb.git] / apps / settings.c
blobcd1c252426c7239a54bfed880d24fae168c3e6b9
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; i++)
148 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
149 >>F_NVRAM_MASK_SHIFT;
150 if (nvram_bytes)
152 if ((var_count>0) && (buf_pos<max_len))
154 memcpy(settings[i].setting,&buf[buf_pos],nvram_bytes);
155 buf_pos += nvram_bytes;
156 var_count--;
158 else /* should only happen when new items are added to the end */
160 memcpy(settings[i].setting, &settings[i].default_val, nvram_bytes);
164 return true;
166 static bool write_nvram_data(char* buf, int max_len)
168 unsigned crc32 = 0xffffffff;
169 int i = 0, buf_pos = 0;
170 char var_count = 0;
171 #ifndef HAVE_RTC_RAM
172 int fd;
173 #endif
174 memset(buf,0,max_len);
175 /* magic, version */
176 buf[0] = 'R'; buf[1] = 'b';
177 buf[2] = NVRAM_CONFIG_VERSION;
178 buf_pos = NVRAM_DATA_START;
179 for(i=0; (i<nb_settings) && (buf_pos<max_len); i++)
181 int nvram_bytes = (settings[i].flags&F_NVRAM_BYTES_MASK)
182 >>F_NVRAM_MASK_SHIFT;
183 if (nvram_bytes)
185 memcpy(&buf[buf_pos],settings[i].setting,nvram_bytes);
186 buf_pos += nvram_bytes;
187 var_count++;
190 /* count and crc32 */
191 buf[3] = var_count;
192 crc32 = crc_32(&buf[NVRAM_DATA_START],
193 max_len-NVRAM_DATA_START-1,0xffffffff);
194 memcpy(&buf[4],&crc32,4);
195 #ifndef HAVE_RTC_RAM
196 fd = open(NVRAM_FILE,O_CREAT|O_TRUNC|O_WRONLY);
197 if (fd >= 0)
199 int len = write(fd,buf,max_len);
200 close(fd);
201 if (len < 8)
202 return false;
204 #else
205 /* FIXME: okay, it _would_ be cleaner and faster to implement rtc_write so
206 that it would write a number of bytes at a time since the RTC chip
207 supports that, but this will have to do for now 8-) */
208 for (i=0; i < NVRAM_BLOCK_SIZE; i++ ) {
209 int r = rtc_write(0x14+i, buf[i]);
210 if (r) {
211 DEBUGF( "save_config_buffer: rtc_write failed at addr 0x%02x: %d\n",
212 14+i, r );
213 return false;
216 #endif
217 return true;
220 /** Reading from a config file **/
222 * load settings from disk or RTC RAM
224 void settings_load(int which)
226 DEBUGF( "reload_all_settings()\n" );
227 if (which&SETTINGS_RTC)
228 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
229 if (which&SETTINGS_HD)
231 settings_load_config(CONFIGFILE,false);
232 settings_load_config(FIXEDSETTINGSFILE,false);
236 static bool cfg_string_to_int(int setting_id, int* out, char* str)
238 const char* start = settings[setting_id].cfg_vals;
239 char* end = NULL;
240 char temp[MAX_PATH];
241 int count = 0;
242 while (1)
244 end = strchr(start, ',');
245 if (!end)
247 if (!strcmp(str, start))
249 *out = count;
250 return true;
252 else return false;
254 strncpy(temp, start, end-start);
255 temp[end-start] = '\0';
256 if (!strcmp(str, temp))
258 *out = count;
259 return true;
261 start = end +1;
262 count++;
264 return false;
267 bool settings_load_config(const char* file, bool apply)
269 int fd;
270 char line[128];
271 char* name;
272 char* value;
273 int i;
274 fd = open(file, O_RDONLY);
275 if (fd < 0)
276 return false;
278 while (read_line(fd, line, sizeof line) > 0)
280 if (!settings_parseline(line, &name, &value))
281 continue;
282 for(i=0; i<nb_settings; i++)
284 if (settings[i].cfg_name == NULL)
285 continue;
286 if (!strcasecmp(name,settings[i].cfg_name))
288 switch (settings[i].flags&F_T_MASK)
290 case F_T_INT:
291 case F_T_UINT:
292 #ifdef HAVE_LCD_COLOR
293 if (settings[i].flags&F_RGB)
294 *(int*)settings[i].setting = hex_to_rgb(value);
295 else
296 #endif
297 if (settings[i].cfg_vals == NULL)
299 *(int*)settings[i].setting = atoi(value);
301 else
303 cfg_string_to_int(i,(int*)settings[i].setting,value);
305 break;
306 case F_T_BOOL:
308 int temp;
309 if (cfg_string_to_int(i,&temp,value))
310 *(bool*)settings[i].setting = (temp==0?false:true);
311 break;
313 case F_T_CHARPTR:
314 case F_T_UCHARPTR:
316 char storage[MAX_PATH];
317 if (settings[i].filename_setting->prefix)
319 int len = strlen(settings[i].filename_setting->prefix);
320 if (!strncasecmp(value,
321 settings[i].filename_setting->prefix,
322 len))
324 strncpy(storage,&value[len],MAX_PATH);
326 else strncpy(storage,value,MAX_PATH);
328 else strncpy(storage,value,MAX_PATH);
329 if (settings[i].filename_setting->suffix)
331 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
332 if (s) *s = '\0';
334 strncpy((char*)settings[i].setting,storage,
335 settings[i].filename_setting->max_len);
336 ((char*)settings[i].setting)
337 [settings[i].filename_setting->max_len-1] = '\0';
338 break;
341 break;
342 } /* if (!strcmp(name,settings[i].cfg_name)) */
343 } /* for(...) */
344 } /* while(...) */
346 close(fd);
347 settings_save();
348 if (apply)
349 settings_apply();
350 return true;
353 /** Writing to a config file and saving settings **/
355 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
357 const char* start = settings[setting_id].cfg_vals;
358 char* end = NULL;
359 int count = 0;
360 while (count < val)
362 start = strchr(start,',');
363 if (!start)
364 return false;
365 count++;
366 start++;
368 end = strchr(start,',');
369 if (end == NULL)
370 strncpy(buf, start, buf_len);
371 else
373 int len = (buf_len > (end-start))? end-start: buf_len;
374 strncpy(buf, start, len);
375 buf[len] = '\0';
377 return true;
379 static bool is_changed(int setting_id)
381 const struct settings_list *setting = &settings[setting_id];
382 switch (setting->flags&F_T_MASK)
384 case F_T_INT:
385 case F_T_UINT:
386 if (setting->flags&F_DEF_ISFUNC)
388 if (*(int*)setting->setting == setting->default_val.func())
389 return false;
391 else if (setting->flags&F_T_SOUND)
393 if (*(int*)setting->setting ==
394 sound_default(setting->sound_setting->setting))
395 return false;
397 else if (*(int*)setting->setting == setting->default_val.int_)
398 return false;
399 break;
400 case F_T_BOOL:
401 if (*(bool*)setting->setting == setting->default_val.bool_)
402 return false;
403 break;
404 case F_T_CHARPTR:
405 case F_T_UCHARPTR:
406 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
407 return false;
408 break;
410 return true;
413 static bool settings_write_config(char* filename, int options)
415 int i;
416 int fd;
417 char value[MAX_PATH];
418 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
419 if (fd < 0)
420 return false;
421 fdprintf(fd, "# .cfg file created by rockbox %s - "
422 "http://www.rockbox.org\r\n\r\n", appsversion);
423 for(i=0; i<nb_settings; i++)
425 if (settings[i].cfg_name == NULL)
426 continue;
427 value[0] = '\0';
429 if ((options == SETTINGS_SAVE_CHANGED) &&
430 !is_changed(i))
431 continue;
432 else if ((options == SETTINGS_SAVE_THEME) &&
433 ((settings[i].flags&F_THEMESETTING) == 0))
434 continue;
435 #ifdef HAVE_RECORDING
436 else if ((options == SETTINGS_SAVE_RECPRESETS) &&
437 ((settings[i].flags&F_RECSETTING) == 0))
438 continue;
439 #endif
440 switch (settings[i].flags&F_T_MASK)
442 case F_T_INT:
443 case F_T_UINT:
444 #ifdef HAVE_LCD_COLOR
445 if (settings[i].flags&F_RGB)
447 int colour = *(int*)settings[i].setting;
448 snprintf(value,MAX_PATH,"%02x%02x%02x",
449 (int)RGB_UNPACK_RED(colour),
450 (int)RGB_UNPACK_GREEN(colour),
451 (int)RGB_UNPACK_BLUE(colour));
453 else
454 #endif
455 if (settings[i].cfg_vals == NULL)
457 snprintf(value,MAX_PATH,"%d",*(int*)settings[i].setting);
459 else
461 cfg_int_to_string(i, *(int*)settings[i].setting,
462 value, MAX_PATH);
464 break;
465 case F_T_BOOL:
466 cfg_int_to_string(i,
467 *(bool*)settings[i].setting==false?0:1, value, MAX_PATH);
468 break;
469 case F_T_CHARPTR:
470 case F_T_UCHARPTR:
471 if (((char*)settings[i].setting)[0] == '\0')
472 break;
473 if (settings[i].filename_setting->prefix)
475 snprintf(value,MAX_PATH,"%s%s%s",
476 settings[i].filename_setting->prefix,
477 (char*)settings[i].setting,
478 settings[i].filename_setting->suffix);
480 else strncpy(value,(char*)settings[i].setting,
481 settings[i].filename_setting->max_len);
482 break;
483 } /* switch () */
484 if (value[0])
485 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
486 } /* for(...) */
487 close(fd);
488 return true;
490 #ifndef HAVE_RTC_RAM
491 static bool flush_global_status_callback(void)
493 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
495 #endif
496 static bool flush_config_block_callback(void)
498 bool r1, r2;
499 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
500 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
501 return r1 || r2;
505 * persist all runtime user settings to disk or RTC RAM
507 static void update_runtime(void)
509 int elapsed_secs;
511 elapsed_secs = (current_tick - lasttime) / HZ;
512 global_status.runtime += elapsed_secs;
513 lasttime += (elapsed_secs * HZ);
515 if ( global_status.runtime > global_status.topruntime )
516 global_status.topruntime = global_status.runtime;
519 void status_save( void )
521 update_runtime();
522 #ifdef HAVE_RTC_RAM
523 /* this will be done in the ata_callback if
524 target doesnt have rtc ram */
525 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
526 #else
527 register_ata_idle_func(flush_global_status_callback);
528 #endif
531 int settings_save( void )
533 update_runtime();
534 #ifdef HAVE_RTC_RAM
535 /* this will be done in the ata_callback if
536 target doesnt have rtc ram */
537 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
538 #endif
539 if(!register_ata_idle_func(flush_config_block_callback))
541 int i;
542 FOR_NB_SCREENS(i)
544 screens[i].clear_display();
545 #ifdef HAVE_LCD_CHARCELLS
546 screens[i].puts(0, 0, str(LANG_SETTINGS_SAVE_FAILED));
547 screens[i].puts(0, 1, str(LANG_SETTINGS_PARTITION));
548 #else
549 screens[i].puts(4, 2, str(LANG_SETTINGS_SAVE_FAILED));
550 screens[i].puts(2, 4, str(LANG_SETTINGS_PARTITION));
551 screens[i].update();
552 #endif
554 cond_talk_ids_fq(LANG_SETTINGS_SAVE_FAILED);
555 sleep(HZ*2);
556 return -1;
558 return 0;
560 bool settings_save_config(int options)
562 char filename[MAX_PATH];
563 char *folder;
564 switch (options)
566 case SETTINGS_SAVE_THEME:
567 folder = THEME_DIR;
568 break;
569 #ifdef HAVE_RECORDING
570 case SETTINGS_SAVE_RECPRESETS:
571 folder = RECPRESETS_DIR;
572 break;
573 #endif
574 default:
575 folder = ROCKBOX_DIR;
577 create_numbered_filename(filename, folder, "config", ".cfg", 2
578 IF_CNFN_NUM_(, NULL));
580 /* allow user to modify filename */
581 while (true) {
582 if (!kbd_input(filename, sizeof filename)) {
583 break;
585 else {
586 gui_syncsplash(HZ, ID2P(LANG_CANCEL));
587 return false;
591 if (settings_write_config(filename, options))
592 gui_syncsplash(HZ, ID2P(LANG_SETTINGS_SAVED));
593 else
594 gui_syncsplash(HZ, ID2P(LANG_FAILED));
595 return true;
598 /** Apply and Reset settings **/
601 #ifdef HAVE_LCD_BITMAP
603 * Applies the range infos stored in global_settings to
604 * the peak meter.
606 void settings_apply_pm_range(void)
608 int pm_min, pm_max;
610 /* depending on the scale mode (dBfs or percent) the values
611 of global_settings.peak_meter_dbfs have different meanings */
612 if (global_settings.peak_meter_dbfs)
614 /* convert to dBfs * 100 */
615 pm_min = -(((int)global_settings.peak_meter_min) * 100);
616 pm_max = -(((int)global_settings.peak_meter_max) * 100);
618 else
620 /* percent is stored directly -> no conversion */
621 pm_min = global_settings.peak_meter_min;
622 pm_max = global_settings.peak_meter_max;
625 /* apply the range */
626 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
628 #endif /* HAVE_LCD_BITMAP */
630 void sound_settings_apply(void)
632 #if CONFIG_CODEC == SWCODEC
633 sound_set_dsp_callback(dsp_callback);
634 #endif
635 sound_set(SOUND_BASS, global_settings.bass);
636 sound_set(SOUND_TREBLE, global_settings.treble);
637 sound_set(SOUND_BALANCE, global_settings.balance);
638 sound_set(SOUND_VOLUME, global_settings.volume);
639 sound_set(SOUND_CHANNELS, global_settings.channel_config);
640 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
641 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
642 sound_set(SOUND_LOUDNESS, global_settings.loudness);
643 sound_set(SOUND_AVC, global_settings.avc);
644 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
645 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
646 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
647 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
648 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
649 sound_set(SOUND_SUPERBASS, global_settings.superbass);
650 #endif
652 #ifdef HAVE_USB_POWER
653 #if CONFIG_CHARGING
654 usb_charging_enable(global_settings.usb_charging);
655 #endif
656 #endif
659 void settings_apply(void)
661 char buf[64];
662 #if CONFIG_CODEC == SWCODEC
663 int i;
664 #endif
666 DEBUGF( "settings_apply()\n" );
667 sound_settings_apply();
669 #ifndef HAVE_FLASH_STORAGE
670 audio_set_buffer_margin(global_settings.buffer_margin);
671 #endif
673 #ifdef HAVE_LCD_CONTRAST
674 lcd_set_contrast(global_settings.contrast);
675 #endif
676 lcd_scroll_speed(global_settings.scroll_speed);
677 #ifdef HAVE_REMOTE_LCD
678 lcd_remote_set_contrast(global_settings.remote_contrast);
679 lcd_remote_set_invert_display(global_settings.remote_invert);
680 lcd_remote_set_flip(global_settings.remote_flip_display);
681 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
682 lcd_remote_scroll_step(global_settings.remote_scroll_step);
683 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
684 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
685 #ifdef HAVE_REMOTE_LCD_TICKING
686 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
687 #endif
688 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
689 #if CONFIG_CHARGING
690 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
691 #endif
692 #ifdef HAS_REMOTE_BUTTON_HOLD
693 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
694 #endif
695 #endif /* HAVE_REMOTE_LCD */
696 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
697 backlight_set_brightness(global_settings.brightness);
698 #endif
699 #ifdef HAVE_BACKLIGHT
700 backlight_set_timeout(global_settings.backlight_timeout);
701 #if CONFIG_CHARGING
702 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
703 #endif
704 #if defined(HAVE_BACKLIGHT_PWM_FADING) && !defined(SIMULATOR)
705 backlight_set_fade_in(global_settings.backlight_fade_in);
706 backlight_set_fade_out(global_settings.backlight_fade_out);
707 #endif
708 #endif
709 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
710 buttonlight_set_brightness(global_settings.buttonlight_brightness);
711 #endif
712 #ifdef HAVE_BUTTON_LIGHT
713 button_backlight_set_timeout(global_settings.button_light_timeout);
714 #endif
715 #ifndef HAVE_FLASH_STORAGE
716 ata_spindown(global_settings.disk_spindown);
717 #endif
718 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
719 dac_line_in(global_settings.line_in);
720 #endif
721 mpeg_id3_options(global_settings.id3_v1_first);
723 set_poweroff_timeout(global_settings.poweroff);
725 set_battery_capacity(global_settings.battery_capacity);
726 #if BATTERY_TYPES_COUNT > 1
727 set_battery_type(global_settings.battery_type);
728 #endif
730 #ifdef HAVE_LCD_BITMAP
731 lcd_set_invert_display(global_settings.invert);
732 lcd_set_flip(global_settings.flip_display);
733 button_set_flip(global_settings.flip_display);
734 lcd_update(); /* refresh after flipping the screen */
735 settings_apply_pm_range();
736 peak_meter_init_times(
737 global_settings.peak_meter_release, global_settings.peak_meter_hold,
738 global_settings.peak_meter_clip_hold);
739 #endif
741 #if LCD_DEPTH > 1
742 unload_wps_backdrop();
743 #endif
744 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
745 unload_remote_wps_backdrop();
746 #endif
747 if ( global_settings.wps_file[0] &&
748 global_settings.wps_file[0] != 0xff ) {
749 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
750 global_settings.wps_file);
751 wps_data_load(gui_wps[0].data, buf, true);
753 else
755 wps_data_init(gui_wps[0].data);
756 #ifdef HAVE_REMOTE_LCD
757 gui_wps[0].data->remote_wps = false;
758 #endif
761 #if LCD_DEPTH > 1
762 if ( global_settings.backdrop_file[0] &&
763 global_settings.backdrop_file[0] != 0xff ) {
764 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
765 global_settings.backdrop_file);
766 load_main_backdrop(buf);
767 } else {
768 unload_main_backdrop();
770 show_main_backdrop();
771 #endif
772 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
773 show_remote_main_backdrop();
774 #endif
776 #ifdef HAVE_LCD_COLOR
777 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
778 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
779 #endif
781 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
782 if ( global_settings.rwps_file[0]) {
783 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
784 global_settings.rwps_file);
785 wps_data_load(gui_wps[1].data, buf, true);
787 else
789 wps_data_init(gui_wps[1].data);
790 gui_wps[1].data->remote_wps = true;
792 #endif
794 #ifdef HAVE_LCD_BITMAP
795 if ( global_settings.font_file[0]) {
796 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
797 global_settings.font_file);
798 font_load(buf);
800 else
801 font_reset();
803 if ( global_settings.kbd_file[0]) {
804 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
805 global_settings.kbd_file);
806 load_kbd(buf);
808 else
809 load_kbd(NULL);
811 lcd_scroll_step(global_settings.scroll_step);
812 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
813 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
814 #else
815 lcd_jump_scroll(global_settings.jump_scroll);
816 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
817 #endif
818 lcd_bidir_scroll(global_settings.bidir_limit);
819 lcd_scroll_delay(global_settings.scroll_delay);
821 if ( global_settings.lang_file[0]) {
822 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
823 global_settings.lang_file);
824 lang_load(buf);
825 talk_init(); /* use voice of same language */
828 set_codepage(global_settings.default_codepage);
830 #if CONFIG_CODEC == SWCODEC
831 audio_set_crossfade(global_settings.crossfade);
832 dsp_set_replaygain();
833 dsp_set_crossfeed(global_settings.crossfeed);
834 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
835 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
836 global_settings.crossfeed_hf_attenuation,
837 global_settings.crossfeed_hf_cutoff);
839 /* Configure software equalizer, hardware eq is handled in audio_init() */
840 dsp_set_eq(global_settings.eq_enabled);
841 dsp_set_eq_precut(global_settings.eq_precut);
842 for(i = 0; i < 5; i++) {
843 dsp_set_eq_coefs(i);
846 dsp_dither_enable(global_settings.dithering_enabled);
847 #endif
849 #ifdef HAVE_SPDIF_POWER
850 spdif_power_enable(global_settings.spdif_enable);
851 #endif
853 #ifdef HAVE_BACKLIGHT
854 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
855 #ifdef HAVE_REMOTE_LCD
856 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
857 #endif
858 #ifdef HAS_BUTTON_HOLD
859 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
860 #endif
861 #ifdef HAVE_LCD_SLEEP
862 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
863 #endif
864 #endif /* HAVE_BACKLIGHT */
866 /* This should stay last */
867 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
868 enc_global_settings_apply();
869 #endif
870 /* load the icon set */
871 icons_init();
873 #ifdef HAVE_LCD_COLOR
874 if (global_settings.colors_file)
875 read_color_theme_file();
876 #endif
884 * reset all settings to their default value
886 void settings_reset(void) {
888 int i;
889 DEBUGF( "settings_reset()\n" );
891 for(i=0; i<nb_settings; i++)
893 switch (settings[i].flags&F_T_MASK)
895 case F_T_INT:
896 case F_T_UINT:
897 if (settings[i].flags&F_DEF_ISFUNC)
898 *(int*)settings[i].setting = settings[i].default_val.func();
899 else if (settings[i].flags&F_T_SOUND)
900 *(int*)settings[i].setting =
901 sound_default(settings[i].sound_setting->setting);
902 else *(int*)settings[i].setting = settings[i].default_val.int_;
903 break;
904 case F_T_BOOL:
905 *(bool*)settings[i].setting = settings[i].default_val.bool_;
906 break;
907 case F_T_CHARPTR:
908 case F_T_UCHARPTR:
909 strncpy((char*)settings[i].setting,
910 settings[i].default_val.charptr,MAX_FILENAME);
911 break;
913 } /* for(...) */
914 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
915 enc_global_settings_reset();
916 #endif
919 /** Changing setting values **/
920 const struct settings_list* find_setting(void* variable, int *id)
922 int i;
923 for(i=0;i<nb_settings;i++)
925 if (settings[i].setting == variable)
927 if (id)
928 *id = i;
929 return &settings[i];
932 return NULL;
935 void talk_setting(void *global_settings_variable)
937 const struct settings_list *setting;
938 if (!talk_menus_enabled())
939 return;
940 setting = find_setting(global_settings_variable, NULL);
941 if (setting == NULL)
942 return;
943 if (setting->lang_id)
944 talk_id(setting->lang_id,false);
947 bool set_bool(const char* string, bool* variable )
949 return set_bool_options(string, variable,
950 (char *)STR(LANG_SET_BOOL_YES),
951 (char *)STR(LANG_SET_BOOL_NO),
952 NULL);
956 bool set_bool_options(const char* string, bool* variable,
957 const char* yes_str, int yes_voice,
958 const char* no_str, int no_voice,
959 void (*function)(bool))
961 struct opt_items names[] = {
962 {(unsigned char *)no_str, no_voice},
963 {(unsigned char *)yes_str, yes_voice}
965 bool result;
967 result = set_option(string, variable, BOOL, names, 2,
968 (void (*)(int))function);
969 return result;
972 bool set_int(const unsigned char* string,
973 const char* unit,
974 int voice_unit,
975 int* variable,
976 void (*function)(int),
977 int step,
978 int min,
979 int max,
980 void (*formatter)(char*, size_t, int, const char*) )
982 return set_int_ex(string, unit, voice_unit, variable, function,
983 step, min, max, formatter, NULL);
987 /** extra stuff which is probably misplaced **/
989 void set_file(char* filename, char* setting, int maxlen)
991 char* fptr = strrchr(filename,'/');
992 int len;
993 int extlen = 0;
994 char* ptr;
996 if (!fptr)
997 return;
999 *fptr = 0;
1000 fptr++;
1002 len = strlen(fptr);
1003 ptr = fptr + len;
1004 while ((*ptr != '.') && (ptr != fptr)) {
1005 extlen++;
1006 ptr--;
1008 if(ptr == fptr) extlen = 0;
1010 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1011 (len-extlen > maxlen))
1012 return;
1014 strncpy(setting, fptr, len-extlen);
1015 setting[len-extlen]=0;
1017 settings_save();