Split the system status variables out of global_settings and put them into a new struct global_status. Use status_save() if these need 
saving.
Added car_adapter_mode to the nvram settings, so nvram settings will be reset.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12101 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/debug_menu.c b/apps/debug_menu.c
index 22db43d..c5240d0 100644
--- a/apps/debug_menu.c
+++ b/apps/debug_menu.c
@@ -1830,7 +1830,7 @@
         lcd_puts(0, line++, buf);
 
         snprintf(buf, sizeof(buf), "Last size: %d B",
-                 global_settings.dircache_size);
+                 global_status.dircache_size);
         lcd_puts(0, line++, buf);
 
         snprintf(buf, sizeof(buf), "Limit: %d B", DIRCACHE_LIMIT);
diff --git a/apps/filetree.c b/apps/filetree.c
index cd020d0..10174db 100644
--- a/apps/filetree.c
+++ b/apps/filetree.c
@@ -572,9 +572,9 @@
         if ( play ) {
             /* the resume_index must always be the index in the
                shuffled list in case shuffle is enabled */
-            global_settings.resume_index = start_index;
-            global_settings.resume_offset = 0;
-            settings_save();
+            global_status.resume_index = start_index;
+            global_status.resume_offset = 0;
+            status_save();
 
             start_wps = true;
         }
diff --git a/apps/gui/gwps-common.c b/apps/gui/gwps-common.c
index f2aa497..41edc00 100644
--- a/apps/gui/gwps-common.c
+++ b/apps/gui/gwps-common.c
@@ -2483,7 +2483,7 @@
     int i;
     if (!wps_state.id3 && !(audio_status() & AUDIO_STATUS_PLAY))
     {
-        global_settings.resume_index = -1;
+        global_status.resume_index = -1;
 #ifdef HAVE_LCD_CHARCELLS
         gui_syncsplash(HZ, true, str(LANG_END_PLAYLIST_PLAYER));
 #else
diff --git a/apps/main.c b/apps/main.c
index 5137ff9..dac7ac7 100644
--- a/apps/main.c
+++ b/apps/main.c
@@ -140,7 +140,7 @@
         if (result < 0)
         {
             firmware_settings.disk_clean = false;
-            if (global_settings.dircache_size <= 0)
+            if (global_status.dircache_size <= 0)
             {
                 /* This will be in default language, settings are not
                    applied yet. Not really any easy way to fix that. */
@@ -148,7 +148,7 @@
                 clear = true;
             }
             
-            dircache_build(global_settings.dircache_size);
+            dircache_build(global_status.dircache_size);
         }
     }
     else
@@ -160,12 +160,12 @@
         if (!dircache_is_enabled()
             && !dircache_is_initializing())
         {
-            if (global_settings.dircache_size <= 0)
+            if (global_status.dircache_size <= 0)
             {
                 gui_syncsplash(0, true, str(LANG_DIRCACHE_BUILDING));
                 clear = true;
             }
-            result = dircache_build(global_settings.dircache_size);
+            result = dircache_build(global_status.dircache_size);
         }
         
         if (result < 0)
@@ -176,8 +176,8 @@
     {
         backlight_on();
         show_logo();
-        global_settings.dircache_size = dircache_get_cache_size();
-        settings_save();
+        global_status.dircache_size = dircache_get_cache_size();
+        status_save();
     }
     
     return result;
diff --git a/apps/playlist.c b/apps/playlist.c
index 2928db8..56ffccc 100644
--- a/apps/playlist.c
+++ b/apps/playlist.c
@@ -243,8 +243,8 @@
         create_control(playlist);
 
         /* Reset resume settings */
-        global_settings.resume_first_index = 0;
-        global_settings.resume_seed = -1;
+        global_status.resume_first_index = 0;
+        global_status.resume_seed = -1;
     }
 }
 
@@ -386,8 +386,8 @@
 
     if (playlist->current)
     {
-        global_settings.resume_seed = -1;
-        settings_save();
+        global_status.resume_seed = -1;
+        status_save();
     }
 
     for (i=0; i<playlist->amount; i++)
@@ -710,8 +710,8 @@
 
         if (seek_pos < 0 && playlist->current)
         {
-            global_settings.resume_first_index = playlist->first_index;
-            settings_save();
+            global_status.resume_first_index = playlist->first_index;
+            status_save();
         }
     }
 
@@ -826,8 +826,8 @@
 
         if (write)
         {
-            global_settings.resume_first_index = playlist->first_index;
-            settings_save();
+            global_status.resume_first_index = playlist->first_index;
+            status_save();
         }
     }
 
@@ -902,8 +902,8 @@
     {
         update_control(playlist, PLAYLIST_COMMAND_SHUFFLE, seed,
             playlist->first_index, NULL, NULL, NULL);
-        global_settings.resume_seed = seed;
-        settings_save();
+        global_status.resume_seed = seed;
+        status_save();
     }
 
     return 0;
@@ -942,8 +942,8 @@
     {
         update_control(playlist, PLAYLIST_COMMAND_UNSHUFFLE,
             playlist->first_index, -1, NULL, NULL, NULL);
-        global_settings.resume_seed = 0;
-        settings_save();
+        global_status.resume_seed = 0;
+        status_save();
     }
 
     return 0;
@@ -1117,8 +1117,8 @@
 
             if (playlist->current)
             {
-                global_settings.resume_first_index = i;
-                settings_save();
+                global_status.resume_first_index = i;
+                status_save();
             }
 
             break;
@@ -1742,10 +1742,10 @@
 
     if (result > 0)
     {
-        if (global_settings.resume_seed >= 0)
+        if (global_status.resume_seed >= 0)
         {
-            global_settings.resume_seed = -1;
-            settings_save();
+            global_status.resume_seed = -1;
+            status_save();
         }
 
         playlist->num_cached = 0;
@@ -2294,22 +2294,22 @@
         /* Terminate on EOF */
         if(nread <= 0)
         {
-            if (global_settings.resume_seed >= 0)
+            if (global_status.resume_seed >= 0)
             {
                 /* Apply shuffle command saved in settings */
-                if (global_settings.resume_seed == 0)
+                if (global_status.resume_seed == 0)
                     sort_playlist(playlist, false, true);
                 else
                 {
                     if (!sorted)
                         sort_playlist(playlist, false, false);
 
-                    randomise_playlist(playlist, global_settings.resume_seed,
+                    randomise_playlist(playlist, global_status.resume_seed,
                         false, true);
                 }
             }
 
-            playlist->first_index = global_settings.resume_first_index;
+            playlist->first_index = global_status.resume_first_index;
             break;
         }
     }
@@ -2361,7 +2361,7 @@
     {
         /* store the seek position before the shuffle */
         seek_pos = playlist->indices[start_index];
-        playlist->index = global_settings.resume_first_index =
+        playlist->index = global_status.resume_first_index =
             playlist->first_index = start_index;
         start_current = true;
     }
@@ -2508,7 +2508,7 @@
             playlist->amount > 1)
         {
             /* Repeat shuffle mode.  Re-shuffle playlist and resume play */
-            playlist->first_index = global_settings.resume_first_index = 0;
+            playlist->first_index = global_status.resume_first_index = 0;
             sort_playlist(playlist, false, false);
             randomise_playlist(playlist, current_tick, false, true);
 #if CONFIG_CODEC != SWCODEC
@@ -2651,19 +2651,19 @@
 
     if (id3)
     {
-        if (global_settings.resume_index != playlist->index ||
-            global_settings.resume_offset != id3->offset)
+        if (global_status.resume_index != playlist->index ||
+            global_status.resume_offset != id3->offset)
         {
-            global_settings.resume_index = playlist->index;
-            global_settings.resume_offset = id3->offset;
-            settings_save();
+            global_status.resume_index = playlist->index;
+            global_status.resume_offset = id3->offset;
+            status_save();
         }
     }
     else
     {
-        global_settings.resume_index = -1;
-        global_settings.resume_offset = -1;
-        settings_save();
+        global_status.resume_index = -1;
+        global_status.resume_offset = -1;
+        status_save();
     }
 
     return 0;
diff --git a/apps/plugin.h b/apps/plugin.h
index 64cc208..70b5ebf 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -110,12 +110,12 @@
 #define PLUGIN_MAGIC 0x526F634B /* RocK */
 
 /* increase this every time the api struct changes */
-#define PLUGIN_API_VERSION 40
+#define PLUGIN_API_VERSION 41
 
 /* update this to latest version if a change to the api struct breaks
    backwards compatibility (and please take the opportunity to sort in any
    new function which are "waiting" at the end of the function table) */
-#define PLUGIN_MIN_API_VERSION 38
+#define PLUGIN_MIN_API_VERSION 41
 
 /* plugin return codes */
 enum plugin_status {
diff --git a/apps/recorder/radio.c b/apps/recorder/radio.c
index 2fba14f..45ddd3e 100644
--- a/apps/recorder/radio.c
+++ b/apps/recorder/radio.c
@@ -212,7 +212,7 @@
     if(radio_status == FMRADIO_OFF)
         radio_power(true);
 
-    curr_freq = global_settings.last_frequency 
+    curr_freq = global_status.last_frequency 
         * fm_region[global_settings.fm_region].freq_step 
         + fm_region[global_settings.fm_region].freq_min;
 
@@ -332,10 +332,10 @@
 
 static void remember_frequency(void)
 {
-    global_settings.last_frequency = (curr_freq 
+    global_status.last_frequency = (curr_freq 
         - fm_region[global_settings.fm_region].freq_min) 
         / fm_region[global_settings.fm_region].freq_step;
-    settings_save();
+    status_save();
 }
 
 static void next_preset(int direction)
diff --git a/apps/screens.c b/apps/screens.c
index ab4c5da..4cf56b8 100644
--- a/apps/screens.c
+++ b/apps/screens.c
@@ -1337,21 +1337,21 @@
 #endif
                     )
             {
-                global_settings.runtime = 0;
+                global_status.runtime = 0;
             }
             else
 #endif
             {
-                global_settings.runtime += ((current_tick - lasttime) / HZ);
+                global_status.runtime += ((current_tick - lasttime) / HZ);
             }
             lasttime = current_tick;
 
-            t = global_settings.runtime;
+            t = global_status.runtime;
             FOR_NB_SCREENS(i)
                 screens[i].puts(0, y[i]++, str(LANG_CURRENT_TIME));
         }
         else {
-            t = global_settings.topruntime;
+            t = global_status.topruntime;
             FOR_NB_SCREENS(i)
                 screens[i].puts(0, y[i]++, str(LANG_TOP_TIME));
         }
@@ -1382,9 +1382,9 @@
                 if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
                 {
                     if ( state == 1 )
-                        global_settings.runtime = 0;
+                        global_status.runtime = 0;
                     else
-                        global_settings.topruntime = 0;
+                        global_status.topruntime = 0;
                 }
                 break;
             default:
diff --git a/apps/settings.c b/apps/settings.c
index 1a7d159..4280ff9 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -86,6 +86,7 @@
 void dac_line_in(bool enable);
 #endif
 struct user_settings global_settings;
+struct system_status global_status;
 #ifdef HAVE_RECORDING
 const char rec_base_directory[] = REC_BASE_DIR;
 #endif
@@ -346,6 +347,12 @@
     close(fd);
     return true;
 }
+#ifndef HAVE_RTC_RAM
+static bool flush_global_status_callback(void)
+{
+    return write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
+}
+#endif
 static bool flush_config_block_callback(void)
 {
     bool r1, r2;
@@ -357,16 +364,33 @@
 /*
  * persist all runtime user settings to disk or RTC RAM
  */
-int settings_save( void )
+static void update_runtime(void)
 {
     int elapsed_secs;
 
     elapsed_secs = (current_tick - lasttime) / HZ;
-    global_settings.runtime += elapsed_secs;
+    global_status.runtime += elapsed_secs;
     lasttime += (elapsed_secs * HZ);
 
-    if ( global_settings.runtime > global_settings.topruntime )
-        global_settings.topruntime = global_settings.runtime;
+    if ( global_status.runtime > global_status.topruntime )
+        global_status.topruntime = global_status.runtime;
+}
+
+void status_save( void )
+{
+    update_runtime();
+#ifdef HAVE_RTC_RAM
+    /* this will be done in the ata_callback if
+       target doesnt have rtc ram */
+    write_nvram_data(nvram_buffer,NVRAM_BLOCK_SIZE);
+#else
+    register_ata_idle_func(flush_global_status_callback);
+#endif
+}
+
+int settings_save( void )
+{
+    update_runtime();
 #ifdef HAVE_RTC_RAM
     /* this will be done in the ata_callback if
        target doesnt have rtc ram */
diff --git a/apps/settings.h b/apps/settings.h
index 3ff3fe6..8a495a0 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -128,7 +128,23 @@
 #if !defined(HAVE_LCD_COLOR)
 #define HAVE_LCD_CONTRAST
 #endif
-
+struct system_status
+{
+    int resume_index;  /* index in playlist (-1 for no active resume) */
+    int resume_first_index;  /* index of first track in playlist */
+    uint32_t resume_offset; /* byte offset in mp3 file */
+    int resume_seed;   /* shuffle seed (-1=no resume shuffle 0=sorted
+                          >0=shuffled) */
+    int runtime;       /* current runtime since last charge */
+    int topruntime;    /* top known runtime */
+#ifdef HAVE_DIRCACHE
+    int dircache_size;      /* directory cache structure last size, 22 bits */
+#endif
+#ifdef CONFIG_TUNER
+    int last_frequency;  /* Last frequency for resuming, in FREQ_STEP units,
+                            relative to MIN_FREQ */
+#endif
+};
 struct user_settings
 {
     /* audio settings */
@@ -254,11 +270,6 @@
     /* resume settings */
 
     bool resume;        /* resume option: 0=off, 1=on */
-    int resume_index;  /* index in playlist (-1 for no active resume) */
-    int resume_first_index;  /* index of first track in playlist */
-    uint32_t resume_offset; /* byte offset in mp3 file */
-    int resume_seed;   /* shuffle seed (-1=no resume shuffle 0=sorted
-                          >0=shuffled) */
 
 #ifdef CONFIG_TUNER
     unsigned char fmr_file[MAX_FILENAME+1]; /* last fmr preset */
@@ -304,8 +315,6 @@
     bool browse_current; /* 1=goto current song,
                             0=goto previous location */
 
-    int runtime;       /* current runtime since last charge */
-    int topruntime;    /* top known runtime */
 
     int scroll_speed;  /* long texts scrolling speed: 1-30 */
     int bidir_limit;   /* bidir scroll length limit */
@@ -339,8 +348,6 @@
     bool fm_force_mono;  /* Forces Mono mode if true */
     bool fm_full_range;  /* Enables full 10MHz-160MHz range if true, else
                             only 88MHz-108MHz */
-    int last_frequency;  /* Last frequency for resuming, in FREQ_STEP units,
-                            relative to MIN_FREQ */
 #endif
 
     int max_files_in_dir; /* Max entries in directory (file browser) */
@@ -406,7 +413,6 @@
 #endif
 #ifdef HAVE_DIRCACHE
     bool dircache;          /* enable directory cache */
-    int dircache_size;      /* directory cache structure last size, 22 bits */
 #endif
 #ifdef HAVE_TAGCACHE
 #ifdef HAVE_TC_RAMCACHE
@@ -548,7 +554,7 @@
 };
 
 /* prototypes */
-
+void status_save( void );
 int settings_save(void);
 void settings_load(int which);
 void settings_reset(void);
@@ -581,6 +587,8 @@
 
 /* global settings */
 extern struct user_settings global_settings;
+/* global status */
+extern struct system_status global_status;
 /* name of directory where configuration, fonts and other data
  * files are stored */
 extern long lasttime;
diff --git a/apps/settings_list.c b/apps/settings_list.c
index bffee25..861c3df 100644
--- a/apps/settings_list.c
+++ b/apps/settings_list.c
@@ -104,7 +104,7 @@
             {{cb,LANG_SET_BOOL_YES,LANG_SET_BOOL_NO}}} }
 
 #define SYSTEM_SETTING(flags,var,default) \
-        {flags|F_T_INT, GS(var), INT(default), NULL, NULL, UNUSED}
+        {flags|F_T_INT, &global_status.var, INT(default), NULL, NULL, UNUSED}
         
 #define FILENAME_SETTING(flags,var,name,default,prefix,suffix,len) \
         {flags|F_T_UCHARPTR, GS(var), CHARPTR(default),name,NULL,\
@@ -179,7 +179,7 @@
     {F_T_INT,GS(battery_capacity),INT(BATTERY_CAPACITY_DEFAULT),
         "battery capacity",NULL,UNUSED},
 #ifdef CONFIG_CHARGING
-    OFFON_SETTING(0,car_adapter_mode,false,"car adapter mode", NULL),
+    OFFON_SETTING(NVRAM(1),car_adapter_mode,false,"car adapter mode", NULL),
 #endif
     /* tuner */
 #ifdef CONFIG_TUNER
diff --git a/apps/settings_list.h b/apps/settings_list.h
index 01e8cea..d76d10a 100644
--- a/apps/settings_list.h
+++ b/apps/settings_list.h
@@ -61,7 +61,7 @@
 };
 #define F_NVRAM_BYTES_MASK     0xE00 /*0-4 bytes can be stored */
 #define F_NVRAM_MASK_SHIFT     9
-#define NVRAM_CONFIG_VERSION 1
+#define NVRAM_CONFIG_VERSION 2
 /* Above define should be bumped if
 - a new NVRAM setting is added between 2 other NVRAM settings
 - number of bytes for a NVRAM setting is changed
diff --git a/apps/tree.c b/apps/tree.c
index e9ac6b5..edd238a 100644
--- a/apps/tree.c
+++ b/apps/tree.c
@@ -474,10 +474,10 @@
 static void start_resume(bool just_powered_on)
 {
     bool do_resume = false;
-    if ( global_settings.resume_index != -1 ) {
+    if ( global_status.resume_index != -1 ) {
         DEBUGF("Resume index %X offset %X\n",
-               global_settings.resume_index,
-               global_settings.resume_offset);
+               global_status.resume_index,
+               global_status.resume_offset);
 
 #ifdef HAVE_ALARM_MOD
         if ( rtc_check_alarm_started(true) ) {
@@ -507,8 +507,8 @@
 
         if (playlist_resume() != -1)
         {
-            playlist_start(global_settings.resume_index,
-                global_settings.resume_offset);
+            playlist_start(global_status.resume_index,
+                global_status.resume_offset);
 
             start_wps = true;
         }
@@ -1439,10 +1439,10 @@
 
 #ifdef HAVE_DIRCACHE
     {
-        int old_val = global_settings.dircache_size;
+        int old_val = global_status.dircache_size;
         if (global_settings.dircache)
         {
-            global_settings.dircache_size = dircache_get_cache_size();
+            global_status.dircache_size = dircache_get_cache_size();
 # ifdef HAVE_EEPROM_SETTINGS
             dircache_save();
 # endif
@@ -1450,10 +1450,10 @@
         }
         else
         {
-            global_settings.dircache_size = 0;
+            global_status.dircache_size = 0;
         }
-        if (old_val != global_settings.dircache_size)
-            settings_save();
+        if (old_val != global_status.dircache_size)
+            status_save();
     }
 #endif
 }
@@ -1484,7 +1484,7 @@
             gui_textarea_update(&screens[i]);
         }
 
-        dircache_build(global_settings.dircache_size);
+        dircache_build(global_status.dircache_size);
 
         /* Clean the text when we are done. */
         FOR_NB_SCREENS(i)