diff options
author | Jonathan Gordon <rockbox@jdgordon.info> | 2007-01-29 13:18:57 +0000 |
---|---|---|
committer | Jonathan Gordon <rockbox@jdgordon.info> | 2007-01-29 13:18:57 +0000 |
commit | 41bd24e7dc595f8f3ffdc8a81567293010f88e75 (patch) | |
tree | 11605c4f34237a8f14e6a52bba5ce0043662e33d /apps/settings.c | |
parent | bc60af1dafe4f223cca8a8b4fe58e6b537a419c4 (diff) | |
download | rockbox-41bd24e7dc595f8f3ffdc8a81567293010f88e75.tar.gz rockbox-41bd24e7dc595f8f3ffdc8a81567293010f88e75.zip |
Rearrange these files so they are more managable. No real code changes. Some functions/variables in these are possibly out of place and
should be considered for moving.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12142 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/settings.c')
-rw-r--r-- | apps/settings.c | 398 |
1 files changed, 197 insertions, 201 deletions
diff --git a/apps/settings.c b/apps/settings.c index ed104d9b82..b0f016e418 100644 --- a/apps/settings.c +++ b/apps/settings.c | |||
@@ -22,36 +22,23 @@ | |||
22 | #include <limits.h> | 22 | #include <limits.h> |
23 | #include "inttypes.h" | 23 | #include "inttypes.h" |
24 | #include "config.h" | 24 | #include "config.h" |
25 | #include "kernel.h" | ||
26 | #include "thread.h" | ||
27 | #include "action.h" | 25 | #include "action.h" |
28 | #include "crc32.h" | 26 | #include "crc32.h" |
29 | #include "settings.h" | 27 | #include "settings.h" |
30 | #include "disk.h" | ||
31 | #include "panic.h" | ||
32 | #include "debug.h" | 28 | #include "debug.h" |
33 | #include "usb.h" | 29 | #include "usb.h" |
34 | #include "backlight.h" | 30 | #include "backlight.h" |
35 | #include "lcd.h" | ||
36 | #include "audio.h" | 31 | #include "audio.h" |
37 | #include "mp3_playback.h" | ||
38 | #include "mpeg.h" | 32 | #include "mpeg.h" |
39 | #include "talk.h" | 33 | #include "talk.h" |
40 | #include "string.h" | 34 | #include "string.h" |
41 | #include "ata.h" | ||
42 | #include "ata_idle_notify.h" | 35 | #include "ata_idle_notify.h" |
43 | #include "fat.h" | ||
44 | #include "power.h" | ||
45 | #include "powermgmt.h" | ||
46 | #include "status.h" | ||
47 | #include "atoi.h" | 36 | #include "atoi.h" |
48 | #include "screens.h" | 37 | #include "screens.h" |
49 | #include "ctype.h" | 38 | #include "ctype.h" |
50 | #include "file.h" | 39 | #include "file.h" |
51 | #include "errno.h" | ||
52 | #include "system.h" | 40 | #include "system.h" |
53 | #include "misc.h" | 41 | #include "misc.h" |
54 | #include "timefuncs.h" | ||
55 | #ifdef HAVE_LCD_BITMAP | 42 | #ifdef HAVE_LCD_BITMAP |
56 | #include "icons.h" | 43 | #include "icons.h" |
57 | #include "font.h" | 44 | #include "font.h" |
@@ -62,11 +49,9 @@ | |||
62 | #include "language.h" | 49 | #include "language.h" |
63 | #include "gwps.h" | 50 | #include "gwps.h" |
64 | #include "powermgmt.h" | 51 | #include "powermgmt.h" |
65 | #include "bookmark.h" | ||
66 | #include "sprintf.h" | 52 | #include "sprintf.h" |
67 | #include "keyboard.h" | 53 | #include "keyboard.h" |
68 | #include "version.h" | 54 | #include "version.h" |
69 | #include "rtc.h" | ||
70 | #include "sound.h" | 55 | #include "sound.h" |
71 | #include "rbunicode.h" | 56 | #include "rbunicode.h" |
72 | #include "dircache.h" | 57 | #include "dircache.h" |
@@ -117,7 +102,7 @@ const char rec_base_directory[] = REC_BASE_DIR; | |||
117 | 102 | ||
118 | long lasttime = 0; | 103 | long lasttime = 0; |
119 | 104 | ||
120 | /* NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin */ | 105 | /** NVRAM stuff, if the target doesnt have NVRAM it is saved in ROCKBOX_DIR /nvram.bin **/ |
121 | /* NVRAM is set out as | 106 | /* NVRAM is set out as |
122 | [0] 'R' | 107 | [0] 'R' |
123 | [1] 'b' | 108 | [1] 'b' |
@@ -227,6 +212,139 @@ static bool write_nvram_data(char* buf, int max_len) | |||
227 | return true; | 212 | return true; |
228 | } | 213 | } |
229 | 214 | ||
215 | /** Reading from a config file **/ | ||
216 | /* | ||
217 | * load settings from disk or RTC RAM | ||
218 | */ | ||
219 | void settings_load(int which) | ||
220 | { | ||
221 | DEBUGF( "reload_all_settings()\n" ); | ||
222 | if (which&SETTINGS_RTC) | ||
223 | read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE); | ||
224 | if (which&SETTINGS_HD) | ||
225 | { | ||
226 | settings_load_config(CONFIGFILE,false); | ||
227 | settings_load_config(FIXEDSETTINGSFILE,false); | ||
228 | } | ||
229 | } | ||
230 | |||
231 | |||
232 | static bool cfg_string_to_int(int setting_id, int* out, char* str) | ||
233 | { | ||
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) | ||
239 | { | ||
240 | end = strchr(start, ','); | ||
241 | if (!end) | ||
242 | { | ||
243 | if (!strcmp(str, start)) | ||
244 | { | ||
245 | *out = count; | ||
246 | return true; | ||
247 | } | ||
248 | else return false; | ||
249 | } | ||
250 | strncpy(temp, start, end-start); | ||
251 | temp[end-start] = '\0'; | ||
252 | if (!strcmp(str, temp)) | ||
253 | { | ||
254 | *out = count; | ||
255 | return true; | ||
256 | } | ||
257 | start = end +1; | ||
258 | count++; | ||
259 | } | ||
260 | return false; | ||
261 | } | ||
262 | |||
263 | bool settings_load_config(const char* file, bool apply) | ||
264 | { | ||
265 | int fd; | ||
266 | char line[128]; | ||
267 | char* name; | ||
268 | char* value; | ||
269 | int i; | ||
270 | fd = open(file, O_RDONLY); | ||
271 | if (fd < 0) | ||
272 | return false; | ||
273 | |||
274 | while (read_line(fd, line, sizeof line) > 0) | ||
275 | { | ||
276 | if (!settings_parseline(line, &name, &value)) | ||
277 | continue; | ||
278 | for(i=0; i<nb_settings; i++) | ||
279 | { | ||
280 | if (settings[i].cfg_name == NULL) | ||
281 | continue; | ||
282 | if (!strcasecmp(name,settings[i].cfg_name)) | ||
283 | { | ||
284 | switch (settings[i].flags&F_T_MASK) | ||
285 | { | ||
286 | case F_T_INT: | ||
287 | case F_T_UINT: | ||
288 | #ifdef HAVE_LCD_COLOR | ||
289 | if (settings[i].flags&F_RGB) | ||
290 | *(int*)settings[i].setting = hex_to_rgb(value); | ||
291 | else | ||
292 | #endif | ||
293 | if (settings[i].cfg_vals == NULL) | ||
294 | { | ||
295 | *(int*)settings[i].setting = atoi(value); | ||
296 | } | ||
297 | else | ||
298 | { | ||
299 | cfg_string_to_int(i,(int*)settings[i].setting,value); | ||
300 | } | ||
301 | break; | ||
302 | case F_T_BOOL: | ||
303 | { | ||
304 | int temp; | ||
305 | if (cfg_string_to_int(i,&temp,value)) | ||
306 | *(bool*)settings[i].setting = (temp==0?false:true); | ||
307 | break; | ||
308 | } | ||
309 | case F_T_CHARPTR: | ||
310 | case F_T_UCHARPTR: | ||
311 | { | ||
312 | char storage[MAX_PATH]; | ||
313 | if (settings[i].filename_setting->prefix) | ||
314 | { | ||
315 | int len = strlen(settings[i].filename_setting->prefix); | ||
316 | if (!strncmp(value,settings[i].filename_setting->prefix,len)) | ||
317 | { | ||
318 | strncpy(storage,&value[len],MAX_PATH); | ||
319 | } | ||
320 | else strncpy(storage,value,MAX_PATH); | ||
321 | } | ||
322 | else strncpy(storage,value,MAX_PATH); | ||
323 | if (settings[i].filename_setting->suffix) | ||
324 | { | ||
325 | char *s = strcasestr(storage,settings[i].filename_setting->suffix); | ||
326 | if (s) *s = '\0'; | ||
327 | } | ||
328 | strncpy((char*)settings[i].setting,storage, | ||
329 | settings[i].filename_setting->max_len); | ||
330 | ((char*)settings[i].setting) | ||
331 | [settings[i].filename_setting->max_len-1] = '\0'; | ||
332 | break; | ||
333 | } | ||
334 | } | ||
335 | break; | ||
336 | } /* if (!strcmp(name,settings[i].cfg_name)) */ | ||
337 | } /* for(...) */ | ||
338 | } /* while(...) */ | ||
339 | |||
340 | close(fd); | ||
341 | settings_save(); | ||
342 | if (apply) | ||
343 | settings_apply(); | ||
344 | return true; | ||
345 | } | ||
346 | |||
347 | /** Writing to a config file and saving settings **/ | ||
230 | #ifdef HAVE_LCD_COLOR | 348 | #ifdef HAVE_LCD_COLOR |
231 | /* | 349 | /* |
232 | * Helper function to convert a string of 6 hex digits to a native colour | 350 | * Helper function to convert a string of 6 hex digits to a native colour |
@@ -258,7 +376,7 @@ static int hex_to_rgb(const char* hex) | |||
258 | 376 | ||
259 | return 0; | 377 | return 0; |
260 | } | 378 | } |
261 | #endif | 379 | #endif /* HAVE_LCD_COLOR */ |
262 | static bool cfg_int_to_string(int setting_id, int val, char* buf) | 380 | static bool cfg_int_to_string(int setting_id, int val, char* buf) |
263 | { | 381 | { |
264 | const char* start = settings[setting_id].cfg_vals; | 382 | const char* start = settings[setting_id].cfg_vals; |
@@ -416,9 +534,35 @@ int settings_save( void ) | |||
416 | } | 534 | } |
417 | return 0; | 535 | return 0; |
418 | } | 536 | } |
537 | bool settings_save_config(void) | ||
538 | { | ||
539 | char filename[MAX_PATH]; | ||
540 | |||
541 | create_numbered_filename(filename, ROCKBOX_DIR, "config", ".cfg", 2 | ||
542 | IF_CNFN_NUM_(, NULL)); | ||
543 | |||
544 | /* allow user to modify filename */ | ||
545 | while (true) { | ||
546 | if (!kbd_input(filename, sizeof filename)) { | ||
547 | break; | ||
548 | } | ||
549 | else { | ||
550 | gui_syncsplash(HZ, true, str(LANG_MENU_SETTING_CANCEL)); | ||
551 | return false; | ||
552 | } | ||
553 | } | ||
554 | |||
555 | if (settings_write_config(filename)) | ||
556 | gui_syncsplash(HZ, true, str(LANG_SETTINGS_SAVED)); | ||
557 | else gui_syncsplash(HZ, true, str(LANG_FAILED)); | ||
558 | return true; | ||
559 | } | ||
560 | |||
561 | /** Apply and Reset settings **/ | ||
562 | |||
419 | 563 | ||
420 | #ifdef HAVE_LCD_BITMAP | 564 | #ifdef HAVE_LCD_BITMAP |
421 | /** | 565 | /* |
422 | * Applies the range infos stored in global_settings to | 566 | * Applies the range infos stored in global_settings to |
423 | * the peak meter. | 567 | * the peak meter. |
424 | */ | 568 | */ |
@@ -671,189 +815,6 @@ void settings_apply(void) | |||
671 | #endif | 815 | #endif |
672 | } | 816 | } |
673 | 817 | ||
674 | /* | ||
675 | * load settings from disk or RTC RAM | ||
676 | */ | ||
677 | void settings_load(int which) | ||
678 | { | ||
679 | DEBUGF( "reload_all_settings()\n" ); | ||
680 | if (which&SETTINGS_RTC) | ||
681 | read_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE); | ||
682 | if (which&SETTINGS_HD) | ||
683 | { | ||
684 | settings_load_config(CONFIGFILE,false); | ||
685 | settings_load_config(FIXEDSETTINGSFILE,false); | ||
686 | } | ||
687 | } | ||
688 | |||
689 | void set_file(char* filename, char* setting, int maxlen) | ||
690 | { | ||
691 | char* fptr = strrchr(filename,'/'); | ||
692 | int len; | ||
693 | int extlen = 0; | ||
694 | char* ptr; | ||
695 | |||
696 | if (!fptr) | ||
697 | return; | ||
698 | |||
699 | *fptr = 0; | ||
700 | fptr++; | ||
701 | |||
702 | len = strlen(fptr); | ||
703 | ptr = fptr + len; | ||
704 | while ((*ptr != '.') && (ptr != fptr)) { | ||
705 | extlen++; | ||
706 | ptr--; | ||
707 | } | ||
708 | if(ptr == fptr) extlen = 0; | ||
709 | |||
710 | if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) || | ||
711 | (len-extlen > maxlen)) | ||
712 | return; | ||
713 | |||
714 | strncpy(setting, fptr, len-extlen); | ||
715 | setting[len-extlen]=0; | ||
716 | |||
717 | settings_save(); | ||
718 | } | ||
719 | static bool cfg_string_to_int(int setting_id, int* out, char* str) | ||
720 | { | ||
721 | const char* start = settings[setting_id].cfg_vals; | ||
722 | char* end = NULL; | ||
723 | char temp[MAX_PATH]; | ||
724 | int count = 0; | ||
725 | while (1) | ||
726 | { | ||
727 | end = strchr(start, ','); | ||
728 | if (!end) | ||
729 | { | ||
730 | if (!strcmp(str, start)) | ||
731 | { | ||
732 | *out = count; | ||
733 | return true; | ||
734 | } | ||
735 | else return false; | ||
736 | } | ||
737 | strncpy(temp, start, end-start); | ||
738 | temp[end-start] = '\0'; | ||
739 | if (!strcmp(str, temp)) | ||
740 | { | ||
741 | *out = count; | ||
742 | return true; | ||
743 | } | ||
744 | start = end +1; | ||
745 | count++; | ||
746 | } | ||
747 | return false; | ||
748 | } | ||
749 | |||
750 | bool settings_load_config(const char* file, bool apply) | ||
751 | { | ||
752 | int fd; | ||
753 | char line[128]; | ||
754 | char* name; | ||
755 | char* value; | ||
756 | int i; | ||
757 | fd = open(file, O_RDONLY); | ||
758 | if (fd < 0) | ||
759 | return false; | ||
760 | |||
761 | while (read_line(fd, line, sizeof line) > 0) | ||
762 | { | ||
763 | if (!settings_parseline(line, &name, &value)) | ||
764 | continue; | ||
765 | for(i=0; i<nb_settings; i++) | ||
766 | { | ||
767 | if (settings[i].cfg_name == NULL) | ||
768 | continue; | ||
769 | if (!strcasecmp(name,settings[i].cfg_name)) | ||
770 | { | ||
771 | switch (settings[i].flags&F_T_MASK) | ||
772 | { | ||
773 | case F_T_INT: | ||
774 | case F_T_UINT: | ||
775 | #ifdef HAVE_LCD_COLOR | ||
776 | if (settings[i].flags&F_RGB) | ||
777 | *(int*)settings[i].setting = hex_to_rgb(value); | ||
778 | else | ||
779 | #endif | ||
780 | if (settings[i].cfg_vals == NULL) | ||
781 | { | ||
782 | *(int*)settings[i].setting = atoi(value); | ||
783 | } | ||
784 | else | ||
785 | { | ||
786 | cfg_string_to_int(i,(int*)settings[i].setting,value); | ||
787 | } | ||
788 | break; | ||
789 | case F_T_BOOL: | ||
790 | { | ||
791 | int temp; | ||
792 | if (cfg_string_to_int(i,&temp,value)) | ||
793 | *(bool*)settings[i].setting = (temp==0?false:true); | ||
794 | break; | ||
795 | } | ||
796 | case F_T_CHARPTR: | ||
797 | case F_T_UCHARPTR: | ||
798 | { | ||
799 | char storage[MAX_PATH]; | ||
800 | if (settings[i].filename_setting->prefix) | ||
801 | { | ||
802 | int len = strlen(settings[i].filename_setting->prefix); | ||
803 | if (!strncmp(value,settings[i].filename_setting->prefix,len)) | ||
804 | { | ||
805 | strncpy(storage,&value[len],MAX_PATH); | ||
806 | } | ||
807 | else strncpy(storage,value,MAX_PATH); | ||
808 | } | ||
809 | else strncpy(storage,value,MAX_PATH); | ||
810 | if (settings[i].filename_setting->suffix) | ||
811 | { | ||
812 | char *s = strcasestr(storage,settings[i].filename_setting->suffix); | ||
813 | if (s) *s = '\0'; | ||
814 | } | ||
815 | strncpy((char*)settings[i].setting,storage, | ||
816 | settings[i].filename_setting->max_len); | ||
817 | ((char*)settings[i].setting) | ||
818 | [settings[i].filename_setting->max_len-1] = '\0'; | ||
819 | break; | ||
820 | } | ||
821 | } | ||
822 | break; | ||
823 | } /* if (!strcmp(name,settings[i].cfg_name)) */ | ||
824 | } /* for(...) */ | ||
825 | } /* while(...) */ | ||
826 | |||
827 | close(fd); | ||
828 | settings_save(); | ||
829 | if (apply) | ||
830 | settings_apply(); | ||
831 | return true; | ||
832 | } | ||
833 | |||
834 | bool settings_save_config(void) | ||
835 | { | ||
836 | char filename[MAX_PATH]; | ||
837 | |||
838 | create_numbered_filename(filename, ROCKBOX_DIR, "config", ".cfg", 2 | ||
839 | IF_CNFN_NUM_(, NULL)); | ||
840 | |||
841 | /* allow user to modify filename */ | ||
842 | while (true) { | ||
843 | if (!kbd_input(filename, sizeof filename)) { | ||
844 | break; | ||
845 | } | ||
846 | else { | ||
847 | gui_syncsplash(HZ, true, str(LANG_MENU_SETTING_CANCEL)); | ||
848 | return false; | ||
849 | } | ||
850 | } | ||
851 | |||
852 | if (settings_write_config(filename)) | ||
853 | gui_syncsplash(HZ, true, str(LANG_SETTINGS_SAVED)); | ||
854 | else gui_syncsplash(HZ, true, str(LANG_FAILED)); | ||
855 | return true; | ||
856 | } | ||
857 | 818 | ||
858 | 819 | ||
859 | 820 | ||
@@ -893,6 +854,8 @@ void settings_reset(void) { | |||
893 | #endif | 854 | #endif |
894 | } | 855 | } |
895 | 856 | ||
857 | /** Changing setting values **/ | ||
858 | |||
896 | bool set_bool(const char* string, bool* variable ) | 859 | bool set_bool(const char* string, bool* variable ) |
897 | { | 860 | { |
898 | return set_bool_options(string, variable, | 861 | return set_bool_options(string, variable, |
@@ -1114,6 +1077,39 @@ bool set_option(const char* string, void* variable, enum optiontype type, | |||
1114 | selected, &data,function); | 1077 | selected, &data,function); |
1115 | } | 1078 | } |
1116 | 1079 | ||
1080 | /** extra stuff which is probably misplaced **/ | ||
1081 | |||
1082 | void set_file(char* filename, char* setting, int maxlen) | ||
1083 | { | ||
1084 | char* fptr = strrchr(filename,'/'); | ||
1085 | int len; | ||
1086 | int extlen = 0; | ||
1087 | char* ptr; | ||
1088 | |||
1089 | if (!fptr) | ||
1090 | return; | ||
1091 | |||
1092 | *fptr = 0; | ||
1093 | fptr++; | ||
1094 | |||
1095 | len = strlen(fptr); | ||
1096 | ptr = fptr + len; | ||
1097 | while ((*ptr != '.') && (ptr != fptr)) { | ||
1098 | extlen++; | ||
1099 | ptr--; | ||
1100 | } | ||
1101 | if(ptr == fptr) extlen = 0; | ||
1102 | |||
1103 | if (strncasecmp(ROCKBOX_DIR, filename ,strlen(ROCKBOX_DIR)) || | ||
1104 | (len-extlen > maxlen)) | ||
1105 | return; | ||
1106 | |||
1107 | strncpy(setting, fptr, len-extlen); | ||
1108 | setting[len-extlen]=0; | ||
1109 | |||
1110 | settings_save(); | ||
1111 | } | ||
1112 | |||
1117 | #ifdef HAVE_RECORDING | 1113 | #ifdef HAVE_RECORDING |
1118 | /* This array holds the record timer interval lengths, in seconds */ | 1114 | /* This array holds the record timer interval lengths, in seconds */ |
1119 | static const unsigned long rec_timer_seconds[] = | 1115 | static const unsigned long rec_timer_seconds[] = |