Implement volume limiter feature

Add a "volume limit" parameter to the configuration file. The maximum
value of volume setting in sound menu will be limited to the same as
volume limit. Also each time when setvol() is excuted, Rockbox will
check if the global_settings.volume value larger than
global_settings.volume_limit. If larger, take the value of volume_limit
instead. The volume_limit argument shares the same maximum and minimum values
with volume argument, while taking the maximum volume as its default value.

Change-Id: I8ca9bc6ea6e617b48fc346aae5f2a0a1d259ae36
Reviewed-on: http://gerrit.rockbox.org/697
Reviewed-by: Jonathan Gordon <rockbox@jdgordon.info>
diff --git a/apps/gui/option_select.c b/apps/gui/option_select.c
index d42d081..0452467 100644
--- a/apps/gui/option_select.c
+++ b/apps/gui/option_select.c
@@ -345,7 +345,8 @@
         int setting_id = setting->sound_setting->setting;
 #ifndef ASCENDING_INT_SETTINGS
         step = sound_steps(setting_id);
-        max = sound_max(setting_id);
+        max = (setting_id == SOUND_VOLUME) ?
+            global_settings.volume_limit : sound_max(setting_id);
         /* min = sound_min(setting_id); */
 #else
         step = -sound_steps(setting_id);
@@ -429,7 +430,8 @@
             int setting_id = setting->sound_setting->setting;
             int steps = sound_steps(setting_id);
             int min = sound_min(setting_id);
-            int max = sound_max(setting_id);
+            int max = (setting_id == SOUND_VOLUME) ?
+                global_settings.volume_limit : sound_max(setting_id);
             *nb_items = (max-min)/steps + 1;
 #ifndef ASCENDING_INT_SETTINGS
             *selected = (max - oldvalue) / steps;
diff --git a/apps/lang/chinese-simp.lang b/apps/lang/chinese-simp.lang
index 4c3dd3c..e90ec51 100644
--- a/apps/lang/chinese-simp.lang
+++ b/apps/lang/chinese-simp.lang
@@ -13039,3 +13039,17 @@
     *: "扫描媒体时扫描指定文件夹"
   </voice>
 </phrase>
+<phrase>
+  id: LANG_VOLUME_LIMIT
+  desc: in sound_settings
+  user: core
+  <source>
+    *: "Maximum Volume Limit"
+  </source>
+  <dest>
+    *: "音量限制"
+  </dest>
+  <voice>
+    *: "音量限制"
+  </voice>
+</phrase>
diff --git a/apps/lang/chinese-trad.lang b/apps/lang/chinese-trad.lang
index f8dc5c3..0d0b68d 100644
--- a/apps/lang/chinese-trad.lang
+++ b/apps/lang/chinese-trad.lang
@@ -11240,3 +11240,17 @@
     *: "檔案大小"
   </voice>
 </phrase>
+<phrase>
+  id: LANG_VOLUME_LIMIT
+  desc: in sound_settings
+  user: core
+  <source>
+    *: "Maximum Volume Limit"
+  </source>
+  <dest>
+    *: "音量限制"
+  </dest>
+  <voice>
+    *: "音量限制"
+  </voice>
+</phrase>
diff --git a/apps/lang/english.lang b/apps/lang/english.lang
index dcad532..d7f4476 100644
--- a/apps/lang/english.lang
+++ b/apps/lang/english.lang
@@ -13178,3 +13178,17 @@
     *: "Slow"
   </voice>
 </phrase>
+<phrase>
+  id: LANG_VOLUME_LIMIT
+  desc: in sound_settings
+  user: core
+  <source>
+    *: "Maximum Volume Limit"
+  </source>
+  <dest>
+    *: "Maximum Volume Limit"
+  </dest>
+  <voice>
+    *: "Maximum Volume Limit"
+  </voice>
+</phrase>
diff --git a/apps/menus/sound_menu.c b/apps/menus/sound_menu.c
index 28cc257..8913266 100644
--- a/apps/menus/sound_menu.c
+++ b/apps/menus/sound_menu.c
@@ -34,10 +34,50 @@
 #include "menu_common.h"
 #include "splash.h"
 #include "kernel.h"
+#include "talk.h"
+#include "option_select.h"
+#include "misc.h"
+
+static int32_t get_dec_talkid(int value, int unit)
+{
+    return TALK_ID_DECIMAL(value, 1, unit);
+}
+
+static int volume_limit_callback(int action,const struct menu_item_ex *this_item)
+{
+    (void)this_item;
+
+    static struct int_setting volume_limit_int_setting;
+    volume_limit_int_setting.option_callback = NULL;
+    volume_limit_int_setting.unit = UNIT_DB;
+    volume_limit_int_setting.min = sound_min(SOUND_VOLUME);
+    volume_limit_int_setting.max = sound_max(SOUND_VOLUME);
+    volume_limit_int_setting.step = sound_steps(SOUND_VOLUME);
+    volume_limit_int_setting.formatter = NULL;
+    volume_limit_int_setting.get_talk_id = get_dec_talkid;
+
+    struct settings_list setting;
+    setting.flags = F_BANFROMQS|F_INT_SETTING|F_T_INT|F_NO_WRAP;
+    setting.lang_id = LANG_VOLUME_LIMIT;
+    setting.default_val.int_ = sound_max(SOUND_VOLUME);
+    setting.int_setting = &volume_limit_int_setting;
+
+    switch (action)
+    {
+        case ACTION_ENTER_MENUITEM:
+            setting.setting = &global_settings.volume_limit;
+            option_screen(&setting, NULL, false, ID2P(LANG_VOLUME_LIMIT));
+        case ACTION_EXIT_MENUITEM: /* on exit */
+            setvol();
+            break;
+    }
+    return action;
+}
 
 /***********************************/
 /*    SOUND MENU                   */
 MENUITEM_SETTING(volume, &global_settings.volume, NULL);
+MENUITEM_SETTING(volume_limit, &global_settings.volume_limit, volume_limit_callback);
 #ifdef AUDIOHW_HAVE_BASS
 MENUITEM_SETTING(bass, &global_settings.bass,
 #ifdef HAVE_SW_TONE_CONTROLS
@@ -171,6 +211,7 @@
 
 MAKE_MENU(sound_settings, ID2P(LANG_SOUND_SETTINGS), NULL, Icon_Audio,
           &volume
+          ,&volume_limit
 #ifdef AUDIOHW_HAVE_BASS
           ,&bass
 #endif
diff --git a/apps/misc.c b/apps/misc.c
index e746c43..fa33bb5 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -793,6 +793,9 @@
         global_settings.volume = min_vol;
     if (global_settings.volume > max_vol)
         global_settings.volume = max_vol;
+    if (global_settings.volume > global_settings.volume_limit)
+        global_settings.volume = global_settings.volume_limit;
+
     sound_set_volume(global_settings.volume);
     global_status.last_volume_change = current_tick;
     settings_save();
diff --git a/apps/settings.h b/apps/settings.h
index 087ff0c..62ae038 100644
--- a/apps/settings.h
+++ b/apps/settings.h
@@ -828,6 +828,7 @@
 #ifdef HAVE_PLAY_FREQ
     int play_frequency; /* core audio output frequency selection */
 #endif
+    int volume_limit; /* maximum volume limit */
 };
 
 /** global variables **/
diff --git a/apps/settings_list.c b/apps/settings_list.c
index bd2bfce..01bc5c5 100644
--- a/apps/settings_list.c
+++ b/apps/settings_list.c
@@ -661,9 +661,39 @@
     return get_hotkey_lang_id(value);
 }
 #endif /* HAVE_HOTKEY */
+
+/* volume limiter */
+static void volume_limit_load_from_cfg(void* var, char*value)
+{
+    *(int*)var = atoi(value);
+}
+static char* volume_limit_write_to_cfg(void* setting, char*buf, int buf_len)
+{
+    int current = *(int*)setting;
+    snprintf(buf, buf_len, "%d", current);
+    return buf;
+}
+static bool volume_limit_is_changed(void* setting, void* defaultval)
+{
+    int current = *(int*)setting;
+
+    if ((int*)defaultval == NULL)
+        *(int*)defaultval = sound_max(SOUND_VOLUME);
+    return (current != sound_max(SOUND_VOLUME));
+}
+static void volume_limit_set_default(void* setting, void* defaultval)
+{
+    (void)defaultval;
+    *(int*)setting = sound_max(SOUND_VOLUME);
+}
+
 const struct settings_list settings[] = {
     /* sound settings */
-    SOUND_SETTING(F_NO_WRAP,volume, LANG_VOLUME, "volume", SOUND_VOLUME),
+    SOUND_SETTING(F_NO_WRAP, volume, LANG_VOLUME, "volume", SOUND_VOLUME),
+    CUSTOM_SETTING(F_NO_WRAP, volume_limit, LANG_VOLUME_LIMIT,
+                  NULL, "volume limit",
+                  volume_limit_load_from_cfg, volume_limit_write_to_cfg,
+                  volume_limit_is_changed, volume_limit_set_default),
     SOUND_SETTING(0, balance, LANG_BALANCE, "balance", SOUND_BALANCE),
 /* Tone controls */
 #ifdef AUDIOHW_HAVE_BASS
diff --git a/manual/configure_rockbox/sound_settings.tex b/manual/configure_rockbox/sound_settings.tex
index d2da07b..7022315 100644
--- a/manual/configure_rockbox/sound_settings.tex
+++ b/manual/configure_rockbox/sound_settings.tex
@@ -48,6 +48,12 @@
   \opt{iaudiom3,iaudiom5,iaudiox5,sansa,sansaAMS,iriverh10,iriverh10_5gb,vibe500,fuzeplus}{%
       The minimum setting is -24~dB and the maximum is 24~dB.}
 
+\section{\label{ref:volume_limit}Volume Limit}
+  This setting adjusts the maximum volume of your music. The setting is by
+  default set to the maximum volume which equals to no limit. To set a volume
+  limit, select a volume from the list and the maximum volume will be limited to
+  the selected value all over the system.
+
 \opt{ipodvideo}{
 \section{Bass Cutoff}
   This setting controls the frequency below which the bass adjustment applies.