summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2017-01-14 01:40:12 +0100
committerAmaury Pouly <amaury.pouly@gmail.com>2017-02-04 17:22:08 +0100
commit1245c5fe61f6ca8e1980a33a8b8f7ea4322829fd (patch)
tree117ae10a7d6489070321dc2d5d790daa71f7921b
parentd052f13999a53e440975e0192c67c8a42c75e4ea (diff)
downloadrockbox-1245c5f.tar.gz
rockbox-1245c5f.tar.bz2
rockbox-1245c5f.zip
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
-rw-r--r--apps/audio_path.c13
-rw-r--r--apps/menus/sound_menu.c4
-rw-r--r--apps/misc.c5
-rw-r--r--apps/plugin.h4
-rw-r--r--apps/recorder/recording.c4
-rw-r--r--apps/settings.c2
-rw-r--r--apps/settings.h4
-rw-r--r--apps/settings_list.c10
-rw-r--r--firmware/export/audio.h9
9 files changed, 43 insertions, 12 deletions
diff --git a/apps/audio_path.c b/apps/audio_path.c
index 6709d4421d..3f43badc25 100644
--- a/apps/audio_path.c
+++ b/apps/audio_path.c
@@ -164,3 +164,16 @@ int audio_get_spdif_sample_rate(void)
#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 7c04662233..69e8e3676a 100644
--- a/apps/menus/sound_menu.c
+++ b/apps/menus/sound_menu.c
@@ -225,7 +225,7 @@ static int timestretch_callback(int action,const struct menu_item_ex *this_item)
#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 @@ MAKE_MENU(sound_settings, ID2P(LANG_SOUND_SETTINGS), NULL, Icon_Audio,
,&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 15e1b6ecad..e460eb542c 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -522,6 +522,11 @@ static void unplug_change(bool inserted)
}
}
}
+
+#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 f781f60350..c96c4ac899 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -160,12 +160,12 @@ void* plugin_get_buffer(size_t *buffer_size);
#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 1c53c8026f..4816b3bad4 100644
--- a/apps/recorder/recording.c
+++ b/apps/recorder/recording.c
@@ -1097,7 +1097,7 @@ bool recording_screen(bool no_source)
#ifdef HAVE_SPEAKER
/* Disable speaker to prevent feedback */
- audiohw_enable_speaker(false);
+ audio_enable_speaker(0);
#endif
audio_init_recording();
@@ -1959,7 +1959,7 @@ rec_abort:
#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 992cc1f391..aa51e051e9 100644
--- a/apps/settings.c
+++ b/apps/settings.c
@@ -927,7 +927,7 @@ void settings_apply(bool read_disk)
#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 c79aeb37f4..5de65bb193 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -746,8 +746,8 @@ struct user_settings
#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 58be3d2575..57763d345a 100644
--- a/apps/settings_list.c
+++ b/apps/settings_list.c
@@ -2118,9 +2118,13 @@ const struct settings_list settings[] = {
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 08a88d6325..5710f9f653 100644
--- a/firmware/export/audio.h
+++ b/firmware/export/audio.h
@@ -232,6 +232,15 @@ int audio_get_spdif_sample_rate(void);
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