M:Robe 500: Get the rest of the games except doom working with the touchscreen.
[kugel-rb.git] / apps / settings.c
blob598e39e7abe34b30f199905a4ee2ee843fe71d36
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Stuart Martin
11 * RTC config saving code (C) 2002 by hessu@hes.iki.fi
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include "inttypes.h"
27 #include "config.h"
28 #include "action.h"
29 #include "crc32.h"
30 #include "settings.h"
31 #include "debug.h"
32 #include "usb.h"
33 #include "backlight.h"
34 #include "audio.h"
35 #include "mpeg.h"
36 #include "talk.h"
37 #include "string.h"
38 #include "rtc.h"
39 #include "power.h"
40 #include "ata_idle_notify.h"
41 #include "storage.h"
42 #include "screens.h"
43 #include "ctype.h"
44 #include "file.h"
45 #include "system.h"
46 #include "general.h"
47 #include "misc.h"
48 #ifdef HAVE_LCD_BITMAP
49 #include "icons.h"
50 #include "font.h"
51 #include "peakmeter.h"
52 #endif
53 #include "lang.h"
54 #include "language.h"
55 #include "gwps.h"
56 #include "powermgmt.h"
57 #include "sprintf.h"
58 #include "keyboard.h"
59 #include "version.h"
60 #include "sound.h"
61 #include "rbunicode.h"
62 #include "dircache.h"
63 #include "statusbar.h"
64 #include "splash.h"
65 #include "list.h"
66 #include "settings_list.h"
67 #include "filetypes.h"
68 #include "option_select.h"
69 #include "backdrop.h"
71 #if CONFIG_TUNER
72 #include "radio.h"
73 #endif
75 #if CONFIG_CODEC == MAS3507D
76 void dac_line_in(bool enable);
77 #endif
78 struct user_settings global_settings;
79 struct system_status global_status;
81 #if CONFIG_CODEC == SWCODEC
82 #include "dsp.h"
83 #include "playback.h"
84 #ifdef HAVE_RECORDING
85 #include "enc_config.h"
86 #endif
87 #endif /* CONFIG_CODEC == SWCODEC */
89 #define NVRAM_BLOCK_SIZE 44
91 #ifdef HAVE_LCD_BITMAP
92 #define MAX_LINES 10
93 #else
94 #define MAX_LINES 2
95 #endif
97 #ifdef HAVE_REMOTE_LCD
98 #include "lcd-remote.h"
99 #endif
101 long lasttime = 0;
103 /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/
104 /* NVRAM is set out as
105 [0] 'R'
106 [1] 'b'
107 [2] version
108 [3] stored variable count
109 [4-7] crc32 checksum
110 [8-NVRAM_BLOCK_SIZE] data
112 #define NVRAM_DATA_START 8
113 #define NVRAM_FILE ROCKBOX_DIR "/nvram.bin"
114 static char nvram_buffer[NVRAM_BLOCK_SIZE];
116 static bool read_nvram_data(char* buf, int max_len)
118 unsigned crc32 = 0xffffffff;
119 int var_count = 0, i = 0, buf_pos = 0;
120 #ifndef HAVE_RTC_RAM
121 int fd = open(NVRAM_FILE,O_RDONLY);
122 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 return false;
213 #endif
214 return true;
217 /** Reading from a config file **/
219 * load settings from disk or RTC RAM
221 void settings_load(int which)
223 if (which&SETTINGS_RTC)
224 read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
225 if (which&SETTINGS_HD)
227 settings_load_config(CONFIGFILE,false);
228 settings_load_config(FIXEDSETTINGSFILE,false);
232 static bool cfg_string_to_int(int setting_id, int* out, const char* str)
234 const char* start = settings[setting_id].cfg_vals;
235 char* end = NULL;
236 char temp[MAX_PATH];
237 int count = 0;
238 while (1)
240 end = strchr(start, ',');
241 if (!end)
243 if (!strcmp(str, start))
245 *out = count;
246 return true;
248 else return false;
250 strncpy(temp, start, end-start);
251 temp[end-start] = '\0';
252 if (!strcmp(str, temp))
254 *out = count;
255 return true;
257 start = end +1;
258 count++;
260 return false;
263 bool settings_load_config(const char* file, bool apply)
265 int fd;
266 char line[128];
267 char* name;
268 char* value;
269 int i;
270 fd = open_utf8(file, O_RDONLY);
271 if (fd < 0)
272 return false;
274 while (read_line(fd, line, sizeof line) > 0)
276 if (!settings_parseline(line, &name, &value))
277 continue;
278 for(i=0; i<nb_settings; i++)
280 if (settings[i].cfg_name == NULL)
281 continue;
282 if (!strcasecmp(name,settings[i].cfg_name))
284 switch (settings[i].flags&F_T_MASK)
286 case F_T_CUSTOM:
287 settings[i].custom_setting->load_from_cfg(settings[i].setting, value);
288 break;
289 case F_T_INT:
290 case F_T_UINT:
291 #ifdef HAVE_LCD_COLOR
292 if (settings[i].flags&F_RGB)
293 hex_to_rgb(value, (int*)settings[i].setting);
294 else
295 #endif
296 if (settings[i].cfg_vals == NULL)
298 *(int*)settings[i].setting = atoi(value);
300 else
302 int temp, *v = (int*)settings[i].setting;
303 bool found = cfg_string_to_int(i, &temp, value);
304 if (found)
306 if (settings[i].flags&F_TABLE_SETTING)
307 *v = settings[i].table_setting->values[temp];
308 else
309 *v = temp;
311 else
312 *v = atoi(value);
314 break;
315 case F_T_BOOL:
317 int temp;
318 if (cfg_string_to_int(i,&temp,value))
319 *(bool*)settings[i].setting = (temp==0?false:true);
320 if (settings[i].bool_setting->option_callback)
321 settings[i].bool_setting->option_callback(temp==0?false:true);
322 break;
324 case F_T_CHARPTR:
325 case F_T_UCHARPTR:
327 char storage[MAX_PATH];
328 if (settings[i].filename_setting->prefix)
330 int len = strlen(settings[i].filename_setting->prefix);
331 if (!strncasecmp(value,
332 settings[i].filename_setting->prefix,
333 len))
335 strncpy(storage,&value[len],MAX_PATH);
337 else strncpy(storage,value,MAX_PATH);
339 else strncpy(storage,value,MAX_PATH);
340 if (settings[i].filename_setting->suffix)
342 char *s = strcasestr(storage,settings[i].filename_setting->suffix);
343 if (s) *s = '\0';
345 strncpy((char*)settings[i].setting,storage,
346 settings[i].filename_setting->max_len);
347 ((char*)settings[i].setting)
348 [settings[i].filename_setting->max_len-1] = '\0';
349 break;
352 break;
353 } /* if (!strcmp(name,settings[i].cfg_name)) */
354 } /* for(...) */
355 } /* while(...) */
357 close(fd);
358 settings_save();
359 if (apply)
360 settings_apply(true);
361 return true;
364 /** Writing to a config file and saving settings **/
366 bool cfg_int_to_string(int setting_id, int val, char* buf, int buf_len)
368 int flags = settings[setting_id].flags;
369 const char* start = settings[setting_id].cfg_vals;
370 char* end = NULL;
371 int count = 0;
373 if ((flags&F_T_MASK)==F_T_INT &&
374 flags&F_TABLE_SETTING)
376 const int *value = settings[setting_id].table_setting->values;
377 while (start)
379 end = strchr(start,',');
380 if (value[count] == val)
382 if (end == NULL)
383 strncpy(buf, start, buf_len);
384 else
386 int len = (buf_len > (end-start))? end-start: buf_len;
387 strncpy(buf, start, len);
388 buf[len] = '\0';
390 return true;
392 count++;
394 if (end)
395 start = end+1;
396 else
397 break;
399 return false;
402 while (count < val)
404 start = strchr(start,',');
405 if (!start)
406 return false;
407 count++;
408 start++;
410 end = strchr(start,',');
411 if (end == NULL)
412 strncpy(buf, start, buf_len);
413 else
415 int len = (buf_len > (end-start))? end-start: buf_len;
416 strncpy(buf, start, len);
417 buf[len] = '\0';
419 return true;
422 bool cfg_to_string(int i/*setting_id*/, char* buf, int buf_len)
424 switch (settings[i].flags&F_T_MASK)
426 case F_T_CUSTOM:
427 settings[i].custom_setting->write_to_cfg(settings[i].setting,
428 buf, buf_len);
429 break;
430 case F_T_INT:
431 case F_T_UINT:
432 #ifdef HAVE_LCD_COLOR
433 if (settings[i].flags&F_RGB)
435 int colour = *(int*)settings[i].setting;
436 snprintf(buf,buf_len,"%02x%02x%02x",
437 (int)RGB_UNPACK_RED(colour),
438 (int)RGB_UNPACK_GREEN(colour),
439 (int)RGB_UNPACK_BLUE(colour));
441 else
442 #endif
443 if (settings[i].cfg_vals == NULL)
445 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
447 else
449 if (cfg_int_to_string(i, *(int*)settings[i].setting,
450 buf, buf_len) == false)
452 snprintf(buf,buf_len,"%d",*(int*)settings[i].setting);
454 else
455 return false;
457 break;
458 case F_T_BOOL:
459 cfg_int_to_string(i,
460 *(bool*)settings[i].setting==false?0:1, buf, buf_len);
461 break;
462 case F_T_CHARPTR:
463 case F_T_UCHARPTR:
464 if (((char*)settings[i].setting)[0]
465 && settings[i].filename_setting->prefix)
467 snprintf(buf,buf_len,"%s%s%s",
468 settings[i].filename_setting->prefix,
469 (char*)settings[i].setting,
470 settings[i].filename_setting->suffix);
472 else strncpy(buf,(char*)settings[i].setting,
473 settings[i].filename_setting->max_len);
474 break;
475 } /* switch () */
476 return true;
480 static bool is_changed(int setting_id)
482 const struct settings_list *setting = &settings[setting_id];
483 switch (setting->flags&F_T_MASK)
485 case F_T_CUSTOM:
486 return setting->custom_setting->is_changed(setting->setting,
487 setting->default_val.custom);
488 break;
489 case F_T_INT:
490 case F_T_UINT:
491 if (setting->flags&F_DEF_ISFUNC)
493 if (*(int*)setting->setting == setting->default_val.func())
494 return false;
496 else if (setting->flags&F_T_SOUND)
498 if (*(int*)setting->setting ==
499 sound_default(setting->sound_setting->setting))
500 return false;
502 else if (*(int*)setting->setting == setting->default_val.int_)
503 return false;
504 break;
505 case F_T_BOOL:
506 if (*(bool*)setting->setting == setting->default_val.bool_)
507 return false;
508 break;
509 case F_T_CHARPTR:
510 case F_T_UCHARPTR:
511 if (!strcmp((char*)setting->setting, setting->default_val.charptr))
512 return false;
513 break;
515 return true;
518 static bool settings_write_config(const char* filename, int options)
520 int i;
521 int fd;
522 char value[MAX_PATH];
523 fd = open(filename,O_CREAT|O_TRUNC|O_WRONLY);
524 if (fd < 0)
525 return false;
526 fdprintf(fd, "# .cfg file created by rockbox %s - "
527 "http://www.rockbox.org\r\n\r\n", appsversion);
528 for(i=0; i<nb_settings; i++)
530 if (settings[i].cfg_name == NULL)
531 continue;
532 value[0] = '\0';
534 switch (options)
536 case SETTINGS_SAVE_CHANGED:
537 if (!is_changed(i))
538 continue;
539 break;
540 case SETTINGS_SAVE_SOUND:
541 if ((settings[i].flags&F_SOUNDSETTING) == 0)
542 continue;
543 break;
544 case SETTINGS_SAVE_THEME:
545 if ((settings[i].flags&F_THEMESETTING) == 0)
546 continue;
547 break;
548 #ifdef HAVE_RECORDING
549 case SETTINGS_SAVE_RECPRESETS:
550 if ((settings[i].flags&F_RECSETTING) == 0)
551 continue;
552 break;
553 #endif
554 #if CONFIG_CODEC == SWCODEC
555 case SETTINGS_SAVE_EQPRESET:
556 if ((settings[i].flags&F_EQSETTING) == 0)
557 continue;
558 break;
559 #endif
562 cfg_to_string(i, value, MAX_PATH);
563 fdprintf(fd,"%s: %s\r\n",settings[i].cfg_name,value);
564 } /* for(...) */
565 close(fd);
566 return true;
568 #ifndef HAVE_RTC_RAM
569 static bool flush_global_status_callback(void)
571 return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
573 #endif
574 static bool flush_config_block_callback(void)
576 bool r1, r2;
577 r1 = write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
578 r2 = settings_write_config(CONFIGFILE, SETTINGS_SAVE_CHANGED);
579 return r1 || r2;
583 * persist all runtime user settings to disk or RTC RAM
585 static void update_runtime(void)
587 int elapsed_secs;
589 elapsed_secs = (current_tick - lasttime) / HZ;
590 global_status.runtime += elapsed_secs;
591 lasttime += (elapsed_secs * HZ);
593 if ( global_status.runtime > global_status.topruntime )
594 global_status.topruntime = global_status.runtime;
597 void status_save(void)
599 update_runtime();
600 #ifdef HAVE_RTC_RAM
601 /* this will be done in the storage_callback if
602 target doesnt have rtc ram */
603 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
604 #else
605 register_storage_idle_func(flush_global_status_callback);
606 #endif
609 int settings_save(void)
611 update_runtime();
612 #ifdef HAVE_RTC_RAM
613 /* this will be done in the storage_callback if
614 target doesnt have rtc ram */
615 write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
616 #endif
617 register_storage_idle_func(flush_config_block_callback);
618 return 0;
621 bool settings_save_config(int options)
623 char filename[MAX_PATH];
624 char *folder;
625 switch (options)
627 case SETTINGS_SAVE_THEME:
628 folder = THEME_DIR;
629 break;
630 #ifdef HAVE_RECORDING
631 case SETTINGS_SAVE_RECPRESETS:
632 folder = RECPRESETS_DIR;
633 break;
634 #endif
635 #if CONFIG_CODEC == SWCODEC
636 case SETTINGS_SAVE_EQPRESET:
637 folder = EQS_DIR;
638 break;
639 #endif
640 case SETTINGS_SAVE_SOUND:
641 default:
642 folder = ROCKBOX_DIR;
644 create_numbered_filename(filename, folder, "config", ".cfg", 2
645 IF_CNFN_NUM_(, NULL));
647 /* allow user to modify filename */
648 while (true) {
649 if (!kbd_input(filename, sizeof filename)) {
650 break;
652 else {
653 splash(HZ, ID2P(LANG_CANCEL));
654 return false;
658 if (settings_write_config(filename, options))
659 splash(HZ, ID2P(LANG_SETTINGS_SAVED));
660 else
661 splash(HZ, ID2P(LANG_FAILED));
662 return true;
665 /** Apply and Reset settings **/
668 #ifdef HAVE_LCD_BITMAP
670 * Applies the range infos stored in global_settings to
671 * the peak meter.
673 void settings_apply_pm_range(void)
675 int pm_min, pm_max;
677 /* depending on the scale mode (dBfs or percent) the values
678 of global_settings.peak_meter_dbfs have different meanings */
679 if (global_settings.peak_meter_dbfs)
681 /* convert to dBfs * 100 */
682 pm_min = -(((int)global_settings.peak_meter_min) * 100);
683 pm_max = -(((int)global_settings.peak_meter_max) * 100);
685 else
687 /* percent is stored directly -> no conversion */
688 pm_min = global_settings.peak_meter_min;
689 pm_max = global_settings.peak_meter_max;
692 /* apply the range */
693 peak_meter_init_range(global_settings.peak_meter_dbfs, pm_min, pm_max);
695 #endif /* HAVE_LCD_BITMAP */
697 void sound_settings_apply(void)
699 #if CONFIG_CODEC == SWCODEC
700 sound_set_dsp_callback(dsp_callback);
701 #endif
702 sound_set(SOUND_BASS, global_settings.bass);
703 sound_set(SOUND_TREBLE, global_settings.treble);
704 sound_set(SOUND_BALANCE, global_settings.balance);
705 sound_set(SOUND_VOLUME, global_settings.volume);
706 sound_set(SOUND_CHANNELS, global_settings.channel_config);
707 sound_set(SOUND_STEREO_WIDTH, global_settings.stereo_width);
708 #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F)
709 sound_set(SOUND_LOUDNESS, global_settings.loudness);
710 sound_set(SOUND_AVC, global_settings.avc);
711 sound_set(SOUND_MDB_STRENGTH, global_settings.mdb_strength);
712 sound_set(SOUND_MDB_HARMONICS, global_settings.mdb_harmonics);
713 sound_set(SOUND_MDB_CENTER, global_settings.mdb_center);
714 sound_set(SOUND_MDB_SHAPE, global_settings.mdb_shape);
715 sound_set(SOUND_MDB_ENABLE, global_settings.mdb_enable);
716 sound_set(SOUND_SUPERBASS, global_settings.superbass);
717 #endif
719 #ifdef HAVE_WM8758
720 sound_set(SOUND_BASS_CUTOFF, global_settings.bass_cutoff);
721 sound_set(SOUND_TREBLE_CUTOFF, global_settings.treble_cutoff);
722 #endif
725 void settings_apply(bool read_disk)
727 char buf[64];
728 #if CONFIG_CODEC == SWCODEC
729 int i;
730 #endif
732 sound_settings_apply();
734 #ifdef HAVE_DISK_STORAGE
735 audio_set_buffer_margin(global_settings.buffer_margin);
736 #endif
738 #ifdef HAVE_LCD_CONTRAST
739 lcd_set_contrast(global_settings.contrast);
740 #endif
741 lcd_scroll_speed(global_settings.scroll_speed);
742 #ifdef HAVE_REMOTE_LCD
743 lcd_remote_set_contrast(global_settings.remote_contrast);
744 lcd_remote_set_invert_display(global_settings.remote_invert);
746 #ifdef HAVE_LCD_FLIP
747 lcd_remote_set_flip(global_settings.remote_flip_display);
748 #endif
750 lcd_remote_scroll_speed(global_settings.remote_scroll_speed);
751 lcd_remote_scroll_step(global_settings.remote_scroll_step);
752 lcd_remote_scroll_delay(global_settings.remote_scroll_delay);
753 lcd_remote_bidir_scroll(global_settings.remote_bidir_limit);
754 #ifdef HAVE_REMOTE_LCD_TICKING
755 lcd_remote_emireduce(global_settings.remote_reduce_ticking);
756 #endif
757 remote_backlight_set_timeout(global_settings.remote_backlight_timeout);
758 #if CONFIG_CHARGING
759 remote_backlight_set_timeout_plugged(global_settings.remote_backlight_timeout_plugged);
760 #endif
761 #ifdef HAS_REMOTE_BUTTON_HOLD
762 remote_backlight_set_on_button_hold(global_settings.remote_backlight_on_button_hold);
763 #endif
764 #endif /* HAVE_REMOTE_LCD */
765 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
766 backlight_set_brightness(global_settings.brightness);
767 #endif
768 #ifdef HAVE_BACKLIGHT
769 backlight_set_timeout(global_settings.backlight_timeout);
770 #if CONFIG_CHARGING
771 backlight_set_timeout_plugged(global_settings.backlight_timeout_plugged);
772 #endif
773 #if defined(HAVE_BACKLIGHT_FADING_INT_SETTING) \
774 || defined(HAVE_BACKLIGHT_FADING_BOOL_SETTING)
775 backlight_set_fade_in(global_settings.backlight_fade_in);
776 backlight_set_fade_out(global_settings.backlight_fade_out);
777 #endif
778 #endif
779 #ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
780 buttonlight_set_brightness(global_settings.buttonlight_brightness);
781 #endif
782 #ifdef HAVE_BUTTON_LIGHT
783 buttonlight_set_timeout(global_settings.buttonlight_timeout);
784 #endif
785 #ifdef HAVE_DISK_STORAGE
786 storage_spindown(global_settings.disk_spindown);
787 #endif
788 #if (CONFIG_CODEC == MAS3507D) && !defined(SIMULATOR)
789 dac_line_in(global_settings.line_in);
790 #endif
791 set_poweroff_timeout(global_settings.poweroff);
793 set_battery_capacity(global_settings.battery_capacity);
794 #if BATTERY_TYPES_COUNT > 1
795 set_battery_type(global_settings.battery_type);
796 #endif
798 #ifdef HAVE_LCD_BITMAP
799 #ifdef HAVE_LCD_INVERT
800 lcd_set_invert_display(global_settings.invert);
801 #endif
802 #ifdef HAVE_LCD_FLIP
803 lcd_set_flip(global_settings.flip_display);
804 button_set_flip(global_settings.flip_display);
805 #endif
806 lcd_update(); /* refresh after flipping the screen */
807 settings_apply_pm_range();
808 peak_meter_init_times(
809 global_settings.peak_meter_release, global_settings.peak_meter_hold,
810 global_settings.peak_meter_clip_hold);
811 #endif
813 #ifdef HAVE_SPEAKER
814 audiohw_enable_speaker(global_settings.speaker_enabled);
815 #endif
817 if (read_disk)
820 #ifdef HAVE_LCD_BITMAP
821 /* fonts need to be loaded before the WPS */
822 if ( global_settings.font_file[0]) {
823 snprintf(buf, sizeof buf, FONT_DIR "/%s.fnt",
824 global_settings.font_file);
825 if (font_load(buf) == NULL)
826 font_reset();
828 else
829 font_reset();
831 if ( global_settings.kbd_file[0]) {
832 snprintf(buf, sizeof buf, ROCKBOX_DIR "/%s.kbd",
833 global_settings.kbd_file);
834 load_kbd(buf);
836 else
837 load_kbd(NULL);
838 #endif
839 #if LCD_DEPTH > 1
840 unload_wps_backdrop();
841 #endif
842 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
843 unload_remote_wps_backdrop();
844 #endif
845 if ( global_settings.wps_file[0] &&
846 global_settings.wps_file[0] != 0xff ) {
847 snprintf(buf, sizeof buf, WPS_DIR "/%s.wps",
848 global_settings.wps_file);
849 wps_data_load(gui_wps[0].data, &screens[0], buf, true);
851 else
853 wps_data_init(gui_wps[0].data);
854 #ifdef HAVE_REMOTE_LCD
855 gui_wps[0].data->remote_wps = false;
856 #endif
859 #if LCD_DEPTH > 1
860 if ( global_settings.backdrop_file[0] &&
861 global_settings.backdrop_file[0] != 0xff ) {
862 snprintf(buf, sizeof buf, BACKDROP_DIR "/%s.bmp",
863 global_settings.backdrop_file);
864 load_main_backdrop(buf);
865 } else {
866 unload_main_backdrop();
868 show_main_backdrop();
869 #endif
870 #if defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH > 1
871 show_remote_main_backdrop();
872 #endif
874 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
875 if ( global_settings.rwps_file[0]) {
876 snprintf(buf, sizeof buf, WPS_DIR "/%s.rwps",
877 global_settings.rwps_file);
878 wps_data_load(gui_wps[1].data, &screens[1], buf, true);
880 else
882 wps_data_init(gui_wps[1].data);
883 gui_wps[1].data->remote_wps = true;
885 #endif
886 if ( global_settings.lang_file[0]) {
887 snprintf(buf, sizeof buf, LANG_DIR "/%s.lng",
888 global_settings.lang_file);
889 lang_load(buf);
890 talk_init(); /* use voice of same language */
892 /* load the icon set */
893 icons_init();
895 #ifdef HAVE_LCD_COLOR
896 if (global_settings.colors_file[0])
897 read_color_theme_file();
898 #endif
901 #ifdef HAVE_LCD_COLOR
902 screens[SCREEN_MAIN].set_foreground(global_settings.fg_color);
903 screens[SCREEN_MAIN].set_background(global_settings.bg_color);
904 screens[SCREEN_MAIN].set_selector_start(global_settings.lss_color);
905 screens[SCREEN_MAIN].set_selector_end(global_settings.lse_color);
906 screens[SCREEN_MAIN].set_selector_text(global_settings.lst_color);
907 #endif
909 #ifdef HAVE_LCD_BITMAP
910 lcd_scroll_step(global_settings.scroll_step);
911 gui_list_screen_scroll_step(global_settings.screen_scroll_step);
912 gui_list_screen_scroll_out_of_view(global_settings.offset_out_of_view);
913 #else
914 lcd_jump_scroll(global_settings.jump_scroll);
915 lcd_jump_scroll_delay(global_settings.jump_scroll_delay);
916 #endif
917 lcd_bidir_scroll(global_settings.bidir_limit);
918 lcd_scroll_delay(global_settings.scroll_delay);
921 set_codepage(global_settings.default_codepage);
923 #if CONFIG_CODEC == SWCODEC
924 audio_set_crossfade(global_settings.crossfade);
925 dsp_set_replaygain();
926 dsp_set_crossfeed(global_settings.crossfeed);
927 dsp_set_crossfeed_direct_gain(global_settings.crossfeed_direct_gain);
928 dsp_set_crossfeed_cross_params(global_settings.crossfeed_cross_gain,
929 global_settings.crossfeed_hf_attenuation,
930 global_settings.crossfeed_hf_cutoff);
932 /* Configure software equalizer, hardware eq is handled in audio_init() */
933 dsp_set_eq(global_settings.eq_enabled);
934 dsp_set_eq_precut(global_settings.eq_precut);
935 for(i = 0; i < 5; i++) {
936 dsp_set_eq_coefs(i);
939 dsp_dither_enable(global_settings.dithering_enabled);
940 #endif
942 #ifdef HAVE_SPDIF_POWER
943 spdif_power_enable(global_settings.spdif_enable);
944 #endif
946 #ifdef HAVE_BACKLIGHT
947 set_backlight_filter_keypress(global_settings.bl_filter_first_keypress);
948 #ifdef HAVE_REMOTE_LCD
949 set_remote_backlight_filter_keypress(global_settings.remote_bl_filter_first_keypress);
950 #endif
951 #ifdef HAS_BUTTON_HOLD
952 backlight_set_on_button_hold(global_settings.backlight_on_button_hold);
953 #endif
954 #ifdef HAVE_LCD_SLEEP_SETTING
955 lcd_set_sleep_after_backlight_off(global_settings.lcd_sleep_after_backlight_off);
956 #endif
957 #endif /* HAVE_BACKLIGHT */
959 #ifdef HAVE_TOUCHPAD_SENSITIVITY_SETTING
960 touchpad_set_sensitivity(global_settings.touchpad_sensitivity);
961 #endif
963 #ifdef HAVE_USB_CHARGING_ENABLE
964 usb_charging_enable(global_settings.usb_charging);
965 #endif
967 #ifdef HAVE_TOUCHSCREEN
968 touchscreen_set_mode(global_settings.touch_mode);
969 #endif
971 /* This should stay last */
972 #if defined(HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
973 enc_global_settings_apply();
974 #endif
975 list_init_viewports(NULL);
980 * reset all settings to their default value
982 void reset_setting(const struct settings_list *setting, void *var)
984 switch (setting->flags&F_T_MASK)
986 case F_T_CUSTOM:
987 setting->custom_setting->set_default(setting->setting,
988 setting->default_val.custom);
989 break;
990 case F_T_INT:
991 case F_T_UINT:
992 if (setting->flags&F_DEF_ISFUNC)
993 *(int*)var = setting->default_val.func();
994 else if (setting->flags&F_T_SOUND)
995 *(int*)var = sound_default(setting->sound_setting->setting);
996 else *(int*)var = setting->default_val.int_;
997 break;
998 case F_T_BOOL:
999 *(bool*)var = setting->default_val.bool_;
1000 break;
1001 case F_T_CHARPTR:
1002 case F_T_UCHARPTR:
1003 strncpy((char*)var, setting->default_val.charptr,
1004 setting->filename_setting->max_len);
1005 break;
1009 void settings_reset(void)
1011 int i;
1013 for(i=0; i<nb_settings; i++)
1014 reset_setting(&settings[i], settings[i].setting);
1015 #if defined (HAVE_RECORDING) && CONFIG_CODEC == SWCODEC
1016 enc_global_settings_reset();
1017 #endif
1020 /** Changing setting values **/
1021 const struct settings_list* find_setting(const void* variable, int *id)
1023 int i;
1024 for(i=0;i<nb_settings;i++)
1026 if (settings[i].setting == variable)
1028 if (id)
1029 *id = i;
1030 return &settings[i];
1033 return NULL;
1036 bool set_bool(const char* string, const bool* variable )
1038 return set_bool_options(string, variable,
1039 (char *)STR(LANG_SET_BOOL_YES),
1040 (char *)STR(LANG_SET_BOOL_NO),
1041 NULL);
1045 bool set_bool_options(const char* string, const bool* variable,
1046 const char* yes_str, int yes_voice,
1047 const char* no_str, int no_voice,
1048 void (*function)(bool))
1050 struct opt_items names[] = {
1051 {(unsigned const char *)no_str, no_voice},
1052 {(unsigned const char *)yes_str, yes_voice}
1054 bool result;
1056 result = set_option(string, variable, BOOL, names, 2,
1057 (void (*)(int))function);
1058 return result;
1061 bool set_int(const unsigned char* string,
1062 const char* unit,
1063 int voice_unit,
1064 const int* variable,
1065 void (*function)(int),
1066 int step,
1067 int min,
1068 int max,
1069 void (*formatter)(char*, size_t, int, const char*) )
1071 return set_int_ex(string, unit, voice_unit, variable, function,
1072 step, min, max, formatter, NULL);
1075 bool set_int_ex(const unsigned char* string,
1076 const char* unit,
1077 int voice_unit,
1078 const int* variable,
1079 void (*function)(int),
1080 int step,
1081 int min,
1082 int max,
1083 void (*formatter)(char*, size_t, int, const char*),
1084 int32_t (*get_talk_id)(int, int))
1086 (void)unit;
1087 struct settings_list item;
1088 struct int_setting data = {
1089 function, voice_unit, min, max, step,
1090 formatter, get_talk_id
1092 item.int_setting = &data;
1093 item.flags = F_INT_SETTING|F_T_INT;
1094 item.lang_id = -1;
1095 item.cfg_vals = (char*)string;
1096 item.setting = (void *)variable;
1097 return option_screen(&item, NULL, false, NULL);
1101 static const struct opt_items *set_option_options;
1102 static void set_option_formatter(char* buf, size_t size, int item, const char* unit)
1104 (void)unit;
1105 const unsigned char *text = set_option_options[item].string;
1106 strncpy(buf, P2STR(text), size);
1108 static int32_t set_option_get_talk_id(int value, int unit)
1110 (void)unit;
1111 return set_option_options[value].voice_id;
1113 bool set_option(const char* string, const void* variable, enum optiontype type,
1114 const struct opt_items* options,
1115 int numoptions, void (*function)(int))
1117 int temp;
1118 struct settings_list item;
1119 struct int_setting data = {
1120 function, UNIT_INT, 0, numoptions-1, 1,
1121 set_option_formatter, set_option_get_talk_id
1123 set_option_options = options;
1124 item.int_setting = &data;
1125 item.flags = F_INT_SETTING|F_T_INT;
1126 item.lang_id = -1;
1127 item.cfg_vals = (char*)string;
1128 item.setting = &temp;
1129 if (type == BOOL)
1130 temp = *(bool*)variable? 1: 0;
1131 else
1132 temp = *(int*)variable;
1133 if (!option_screen(&item, NULL, false, NULL))
1135 if (type == BOOL)
1136 *(bool*)variable = (temp == 1? true: false);
1137 else
1138 *(int*)variable = temp;
1139 return false;
1141 return true;
1145 void set_file(const char* filename, char* setting, int maxlen)
1147 char* fptr = strrchr(filename,'/');
1148 int len;
1149 int extlen = 0;
1150 char* ptr;
1152 if (!fptr)
1153 return;
1155 *fptr = 0;
1156 fptr++;
1158 len = strlen(fptr);
1159 ptr = fptr + len;
1160 while ((*ptr != '.') && (ptr != fptr)) {
1161 extlen++;
1162 ptr--;
1164 if(ptr == fptr) extlen = 0;
1166 if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) ||
1167 (len-extlen > maxlen))
1168 return;
1170 strncpy(setting, fptr, len-extlen);
1171 setting[len-extlen]=0;
1173 settings_save();