Implement speaker enable/disable on jack (un)plug

The implementation is not very complicated but there are a few things worth
noting. There was a previous "speaker enable" setting but it was a boolean.
I decided to replace it with a choice setting that has 2 options (on, off)
if headphones cannot be detect on this target, or 3 options (on, off, auto)
if we can detect headphones. This will break the old setting on target that
cannot detect jack but it makes the code more uniform and avoid maintaining
two settings with more #ifdef. The third option (auto) uses the LANG_AUTO
text, which I think is clear enough (disable speaker on jack plug).
In order to avoid code duplication (both in apps and firmware), I decided to
keep the audiohw_enable_speaker function as-is: it takes a boolean and doesn't
care about the speaker policy. I introduced a new audio_enable_speaker that
takes directly the mode (which follows the setting encoding): 0=off, 1=on
and 2=auto. This way one calls audio_enable_speaker and it changes the speaker
once to reflect the request mode. The apps code then uses this function in the
places where it makes sense: on setting load, setting change and jack (un)plug
event.

Change-Id: I027873f698eb4bc365d7c02b515297806355d9e2
diff --git a/apps/audio_path.c b/apps/audio_path.c
index 6709d44..3f43bad 100644
--- a/apps/audio_path.c
+++ b/apps/audio_path.c
@@ -164,3 +164,16 @@
 #endif /* HAVE_SPDIF_IN */
 
 #endif /* PLATFORM_NATIVE */
+
+#ifdef HAVE_SPEAKER
+void audio_enable_speaker(int mode)
+{
+#ifdef HAVE_HEADPHONE_DETECTION
+    /* if needed, query jack state */
+    if(mode == 2)
+        mode = !headphones_inserted();
+#endif
+    /* treat any nonzero value as enable */
+    audiohw_enable_speaker(mode);
+}
+#endif
diff --git a/apps/menus/sound_menu.c b/apps/menus/sound_menu.c
index 7c04662..69e8e36 100644
--- a/apps/menus/sound_menu.c
+++ b/apps/menus/sound_menu.c
@@ -225,7 +225,7 @@
 #endif
 
 #ifdef HAVE_SPEAKER
-    MENUITEM_SETTING(speaker_enabled, &global_settings.speaker_enabled, NULL);
+    MENUITEM_SETTING(speaker_mode, &global_settings.speaker_mode, NULL);
 #endif
 
 #ifdef AUDIOHW_HAVE_EQ
@@ -269,7 +269,7 @@
          ,&mdb_harmonics,&mdb_center,&mdb_shape
 #endif
 #ifdef HAVE_SPEAKER
-         ,&speaker_enabled
+         ,&speaker_mode
 #endif
          );
 
diff --git a/apps/misc.c b/apps/misc.c
index 15e1b6e..e460eb5 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -522,6 +522,11 @@
             }
         }
     }
+
+#ifdef HAVE_SPEAKER
+    /* update speaker status */
+    audio_enable_speaker(global_settings.speaker_mode);
+#endif
 }
 #endif
 
diff --git a/apps/plugin.h b/apps/plugin.h
index f781f60..c96c4ac 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -160,12 +160,12 @@
 #define PLUGIN_MAGIC 0x526F634B /* RocK */
 
 /* increase this every time the api struct changes */
-#define PLUGIN_API_VERSION 233
+#define PLUGIN_API_VERSION 234
 
 /* 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 233
+#define PLUGIN_MIN_API_VERSION 234
 
 /* plugin return codes */
 /* internal returns start at 0x100 to make exit(1..255) work */
diff --git a/apps/recorder/recording.c b/apps/recorder/recording.c
index 1c53c80..4816b3b 100644
--- a/apps/recorder/recording.c
+++ b/apps/recorder/recording.c
@@ -1097,7 +1097,7 @@
 
 #ifdef HAVE_SPEAKER
     /* Disable speaker to prevent feedback */
-    audiohw_enable_speaker(false);
+    audio_enable_speaker(0);
 #endif
 
     audio_init_recording();
@@ -1959,7 +1959,7 @@
 
 #ifdef HAVE_SPEAKER
     /* Re-enable speaker */
-    audiohw_enable_speaker(global_settings.speaker_enabled);
+    audio_enable_speaker(global_settings.speaker_mode);
 #endif
 
     /* make sure the trigger is really turned off */
diff --git a/apps/settings.c b/apps/settings.c
index 992cc1f..aa51e05 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -927,7 +927,7 @@
 #endif
 
 #ifdef HAVE_SPEAKER
-    audiohw_enable_speaker(global_settings.speaker_enabled);
+    audio_enable_speaker(global_settings.speaker_mode);
 #endif
 
     if (read_disk)
diff --git a/apps/settings.h b/apps/settings.h
index c79aeb3..5de65bb 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -746,8 +746,8 @@
 #endif
 
 #ifdef HAVE_SPEAKER
-    bool speaker_enabled;
-#endif
+    int speaker_mode; /* 0: off, 1: on, 2: auto (only if headphone detection) */
+#endif /* HAVE_SPEAKER */
     bool prevent_skip;
 
 #ifdef HAVE_TOUCHSCREEN
diff --git a/apps/settings_list.c b/apps/settings_list.c
index 58be3d2..57763d3 100644
--- a/apps/settings_list.c
+++ b/apps/settings_list.c
@@ -2118,9 +2118,13 @@
                   false, "shortcuts instead of quickscreen", NULL),
 #endif
 #ifdef HAVE_SPEAKER
-    OFFON_SETTING(0, speaker_enabled, LANG_ENABLE_SPEAKER, false, "speaker",
-                  audiohw_enable_speaker),
-#endif
+    CHOICE_SETTING(0, speaker_mode, LANG_ENABLE_SPEAKER, 0, "speaker mode",
+# ifdef HAVE_HEADPHONE_DETECTION
+                   "on,off,auto", audio_enable_speaker, 3, ID2P(LANG_OFF), ID2P(LANG_ON), ID2P(LANG_AUTO)),
+#else /* HAVE_HEADPHONE_DETECTION */
+                   "on,off", audio_enable_speaker, 2, ID2P(LANG_OFF), ID2P(LANG_ON)),
+#endif /* HAVE_HEADPHONE_DETECTION */
+#endif /* HAVE_SPEAKER */
 #ifdef HAVE_TOUCHSCREEN
     CHOICE_SETTING(0, touch_mode, LANG_TOUCHSCREEN_MODE, DEFAULT_TOUCHSCREEN_MODE,
                    "touchscreen mode", "point,grid", NULL, 2,
diff --git a/firmware/export/audio.h b/firmware/export/audio.h
index 08a88d6..5710f9f 100644
--- a/firmware/export/audio.h
+++ b/firmware/export/audio.h
@@ -232,6 +232,15 @@
 void audio_spdif_set_monitor(int monitor_spdif);
 #endif /* HAVE_SPDIF_IN */
 
+#ifdef HAVE_SPEAKER
+/* enable/disable the speaker: 0=off, 1=on, 2=on if jack unpluged, off otherwise
+ * NOTE this is a one time thing, this function doesn't implement the logic to
+ * check for jack events, it merely changes the speaker state to the expected
+ * state based on the requested mode.
+ */
+void audio_enable_speaker(int mode);
+#endif
+
 /***********************************************************************/
 /* audio event handling */
 enum track_event_flags