Lua optimize combine and rework similar functions

rb.strncasecmp
    strcasecmp just exclude count -> rb.strncasecmp(s1, s2)

rb.backlight_brightness_set
    backlight_set_brightness -- redundant
    rb.backlight_brightness_use_setting -> rb.backlight_brightness_set()

rb.buttonlight_brightness_set
    buttonlight_set_brightness -- redundant
    rb.buttonlight_brightness_use_setting -> rb.buttonlight_brightness_set()

rb.mixer_frequency
    rb.mixer_set_frequency -> mixer_frequency(freq)
    rb.mixer_get_frequency -> mixer_frequency

rb.backlight_onoff
    rb.backlight_on  -> rb.backlight_onoff(true)
    rb.backlight_off -> rb.backlight_onoff(false)

rb.touchscreen_mode
    rb.touchscreen_set_mode -> rb.touchscreen_mode(mode)
    rb.touchscreen_get_mode -> rb.touchscreen_mode()

rb.schedule_cpu_boost
    rb.trigger_cpu_boost -> rb.schedule_cpu_boost(true)
    rb.cancel_cpu_boost -> rb.schedule_cpu_boost(false)

Includes rbcompat.lua for backwards compatibility
if your script is broken by this change you simply add
`require("rbcompat")` to the top for the old functionality

Change-Id: Ibffd79a0d9be6d7d6a65cc4af5c0a1c6a0f3f94d
diff --git a/apps/plugins/lua/include_lua/rbcompat.lua b/apps/plugins/lua/include_lua/rbcompat.lua
new file mode 100644
index 0000000..377bc2e
--- /dev/null
+++ b/apps/plugins/lua/include_lua/rbcompat.lua
@@ -0,0 +1,60 @@
+--[[ Lua RB Compatibility Operations
+/***************************************************************************
+ *             __________               __   ___.
+ *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
+ *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
+ *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
+ *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+ *                     \/            \/     \/    \/            \/
+ * $Id$
+ *
+ * Copyright (C) 2018 William Wilgus
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+]]
+
+-- [[ compatibility with old functions ]]
+if rb.strncasecmp then rb.strcasecmp = function(s1, s2) return rb.strncasecmp(s1, s2) end end
+
+if rb.backlight_brightness_set then
+    rb.backlight_set_brightness = function(brightness) rb.backlight_brightness_set(brightness) end
+    rb.backlight_brightness_use_setting = function() rb.backlight_brightness_set(nil) end
+end
+
+if rb.buttonlight_brightness_set then
+    rb.buttonlight_set_brightness = function(brightness) rb.buttonlight_brightness_set(brightness) end
+    rb.buttonlight_brightness_use_setting = function() rb.buttonlight_brightness_set(nil) end
+end
+
+if rb.mixer_frequency then
+    rb.mixer_set_frequency = function(freq) rb.mixer_frequency(freq) end
+    rb.mixer_get_frequency = function() return rb.mixer_frequency(nil) end
+end
+
+if rb.backlight_onoff then
+    rb.backlight_on = function() rb.backlight_onoff(true) end
+    rb.backlight_off = function() rb.backlight_onoff(false) end
+end
+
+if rb.buttonlight_brightness_set then
+    rb.buttonlight_set_brightness = function(brightness) rb.buttonlight_brightness_set(brightness) end
+    rb.buttonlight_brightness_use_setting = function() rb.buttonlight_brightness_set(nil) end
+end
+
+if rb.touchscreen_mode then
+    rb.touchscreen_set_mode = function(mode) rb.touchscreen_mode(mode) end
+    rb.touchscreen_get_mode = function() return rb.touchscreen_mode(nil) end
+end
+
+if rb.schedule_cpu_boost then
+    rb.trigger_cpu_boost = function() rb.schedule_cpu_boost(true) end
+    rb.cancel_cpu_boost = function() rb.schedule_cpu_boost(false) end
+end
diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make
index 56df860..a464bdd 100644
--- a/apps/plugins/lua/lua.make
+++ b/apps/plugins/lua/lua.make
@@ -17,8 +17,7 @@
 
 LUA_INCLUDEDIR := $(LUA_SRCDIR)/include_lua
 LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua draw.lua \
-	image.lua lcd.lua math_ex.lua print.lua \
-	timer.lua playlist.lua pcm.lua)
+	image.lua lcd.lua math_ex.lua print.lua timer.lua playlist.lua pcm.lua rbcompat.lua)
 
 
 ifndef APP_TYPE
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 44eb549..5995fd8 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -9,6 +9,7 @@
  *
  * Copyright (C) 2008 Dan Everton (safetydan)
  * Copyright (C) 2009 Maurus Cuelenaere
+ * Copyright (C) 2018 William Wilgus
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -62,6 +63,73 @@
     return 1;
 }
 
+#ifdef HAVE_SCHEDULER_BOOSTCTRL
+RB_WRAP(schedule_cpu_boost)
+{
+    bool boost = luaL_checkboolean(L, 1);
+
+    if(boost)
+        rb->trigger_cpu_boost();
+    else
+        rb->cancel_cpu_boost();
+
+    return 0;
+}
+#endif
+
+RB_WRAP(current_path)
+{
+    return get_current_path(L, 1);
+}
+
+
+/* DEVICE INPUT CONTROL */
+
+RB_WRAP(get_plugin_action)
+{
+    static const struct button_mapping *m1[] = { pla_main_ctx };
+    int timeout = luaL_checkint(L, 1);
+    int btn;
+
+#ifdef HAVE_REMOTE_LCD
+    static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx };
+    bool with_remote = luaL_optint(L, 2, 0);
+    if (with_remote)
+        btn = pluginlib_getaction(timeout, m2, 2);
+    else
+#endif
+        btn = pluginlib_getaction(timeout, m1, 1);
+
+    lua_pushinteger(L, btn);
+    return 1;
+}
+
+#ifdef HAVE_TOUCHSCREEN
+RB_WRAP(action_get_touchscreen_press)
+{
+    short x, y;
+    int result = rb->action_get_touchscreen_press(&x, &y);
+
+    lua_pushinteger(L, result);
+    lua_pushinteger(L, x);
+    lua_pushinteger(L, y);
+    return 3;
+}
+
+RB_WRAP(touchscreen_mode)
+{
+    int origmode = rb->touchscreen_get_mode();
+    if(!lua_isnoneornil(L, 1))
+    {
+        enum touchscreen_mode mode = luaL_checkint(L, 1);
+        rb->touchscreen_set_mode(mode);
+    }
+    lua_pushinteger(L, origmode);
+    return 1;
+}
+
+#endif
+
 RB_WRAP(kbd_input)
 {
     luaL_Buffer b;
@@ -86,37 +154,6 @@
     return 1;
 }
 
-#ifdef HAVE_TOUCHSCREEN
-RB_WRAP(action_get_touchscreen_press)
-{
-    short x, y;
-    int result = rb->action_get_touchscreen_press(&x, &y);
-
-    lua_pushinteger(L, result);
-    lua_pushinteger(L, x);
-    lua_pushinteger(L, y);
-    return 3;
-}
-
-RB_WRAP(touchscreen_set_mode)
-{
-    enum touchscreen_mode mode = luaL_checkint(L, 1);
-    rb->touchscreen_set_mode(mode);
-    return 0;
-}
-
-RB_WRAP(touchscreen_get_mode)
-{
-    lua_pushinteger(L, rb->touchscreen_get_mode());
-    return 1;
-}
-#endif
-
-RB_WRAP(current_path)
-{
-    return get_current_path(L, 1);
-}
-
 static const char ** get_table_items(lua_State *L, int pos, int *count)
 {
     int i;
@@ -189,6 +226,9 @@
     return 1;
 }
 
+
+/* DEVICE AUDIO / PLAYLIST CONTROL */
+
 RB_WRAP(playlist)
 {
     /* just passes NULL to work with the current playlist */
@@ -391,8 +431,33 @@
     rb->yield();
     return 1;
 }
+
+RB_WRAP(mixer_frequency)
+{
+    unsigned int result = rb->mixer_get_frequency();
+
+    if(!lua_isnoneornil(L, 1))
+    {
+        unsigned int samplerate = (unsigned int) luaL_checkint(L, 1);
+        rb->mixer_set_frequency(samplerate);
+    }
+    lua_pushinteger(L, result);
+    return 1;
+}
 #endif /*CONFIG_CODEC == SWCODEC*/
 
+/* DEVICE LIGHTING CONTROL */
+RB_WRAP(backlight_onoff)
+{
+    bool on = luaL_checkboolean(L, 1);
+    if(on)
+        rb->backlight_on();
+    else
+        rb->backlight_off();
+
+    return 0;
+}
+
 SIMPLE_VOID_WRAPPER(backlight_force_on);
 SIMPLE_VOID_WRAPPER(backlight_use_settings);
 
@@ -409,32 +474,35 @@
 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
 RB_WRAP(backlight_brightness_set)
 {
-    int brightness = luaL_checkint(L, 1);
-    backlight_brightness_set(brightness);
+    if(lua_isnoneornil(L, 1))
+        backlight_brightness_use_setting();
+    else
+    {
+        int brightness = luaL_checkint(L, 1);
+        backlight_brightness_set(brightness);
+    }
 
     return 0;
 }
-SIMPLE_VOID_WRAPPER(backlight_brightness_use_setting);
 #endif
 
-RB_WRAP(get_plugin_action)
+#ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
+RB_WRAP(buttonlight_brightness_set)
 {
-    static const struct button_mapping *m1[] = { pla_main_ctx };
-    int timeout = luaL_checkint(L, 1);
-    int btn;
-
-#ifdef HAVE_REMOTE_LCD
-    static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx };
-    bool with_remote = luaL_optint(L, 2, 0);
-    if (with_remote)
-        btn = pluginlib_getaction(timeout, m2, 2);
+    if(lua_isnoneornil(L, 1))
+        buttonlight_brightness_use_setting();
     else
-#endif
-        btn = pluginlib_getaction(timeout, m1, 1);
+    {
+        int brightness = luaL_checkint(L, 1);
+        buttonlight_brightness_set(brightness);
+    }
 
-    lua_pushinteger(L, btn);
-    return 1;
+    return 0;
 }
+#endif
+
+
+/* DEVICE STRING / FILENAME MANIPULATION */
 
 RB_WRAP(strip_extension)
 {
@@ -482,25 +550,53 @@
     return 1;
 }
 
+RB_WRAP(strncasecmp)
+{
+    int result;
+    const char * s1 = luaL_checkstring(L, 1);
+    const char * s2 = luaL_checkstring(L, 2);
+    if(lua_isnoneornil(L, 3))
+        result = rb->strcasecmp(s1, s2);
+    else
+        result = rb->strncasecmp(s1, s2, (size_t) luaL_checkint(L, 3));
+
+    lua_pushinteger(L, result);
+    return 1;
+}
+
 #define RB_FUNC(func) {#func, rock_##func}
+#define RB_ALIAS(name, func) {name, rock_##func}
 static const luaL_Reg rocklib[] =
 {
     /* Kernel */
     RB_FUNC(current_tick),
-
-    /* Buttons */
-#ifdef HAVE_TOUCHSCREEN
-    RB_FUNC(action_get_touchscreen_press),
-    RB_FUNC(touchscreen_set_mode),
-    RB_FUNC(touchscreen_get_mode),
+#ifdef HAVE_SCHEDULER_BOOSTCTRL
+    RB_FUNC(schedule_cpu_boost),
 #endif
 
-    RB_FUNC(kbd_input),
-
     RB_FUNC(current_path),
+
+    /* DEVICE INPUT CONTROL */
+    RB_FUNC(get_plugin_action),
+#ifdef HAVE_TOUCHSCREEN
+    RB_FUNC(action_get_touchscreen_press),
+    RB_FUNC(touchscreen_mode),
+#endif
+    RB_FUNC(kbd_input),
     RB_FUNC(gui_syncyesno_run),
     RB_FUNC(do_menu),
 
+    /* DEVICE AUDIO / PLAYLIST CONTROL */
+    RB_FUNC(audio),
+    RB_FUNC(playlist),
+#if CONFIG_CODEC == SWCODEC
+    RB_FUNC(pcm),
+    RB_FUNC(mixer_frequency),
+#endif
+
+    /* DEVICE LIGHTING CONTROL */
+    RB_FUNC(backlight_onoff),
+
     /* Backlight helper */
     RB_FUNC(backlight_force_on),
     RB_FUNC(backlight_use_settings),
@@ -517,25 +613,22 @@
 
 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
     RB_FUNC(backlight_brightness_set),
-    RB_FUNC(backlight_brightness_use_setting),
 #endif
 
-    RB_FUNC(get_plugin_action),
+#ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
+    RB_FUNC(buttonlight_brightness_set),
+#endif
 
+    /* DEVICE STRING / FILENAME MANIPULATION */
     RB_FUNC(strip_extension),
     RB_FUNC(create_numbered_filename),
-
-    RB_FUNC(audio),
-    RB_FUNC(playlist),
-#if CONFIG_CODEC == SWCODEC
-    RB_FUNC(pcm),
-#endif
-
     RB_FUNC(utf8encode),
+    RB_FUNC(strncasecmp),
 
     {NULL, NULL}
 };
 #undef RB_FUNC
+#undef RB_ALIAS
 
 extern const luaL_Reg rocklib_aux[];
 
@@ -546,7 +639,7 @@
 {
     luaL_register(L, LUA_ROCKLIBNAME, rocklib);
     luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux);
-    
+
     static const struct lua_int_reg rlib_const_int[] =
     {
         /* useful integer constants */
@@ -555,10 +648,12 @@
         RB_CONSTANT(LCD_DEPTH),
         RB_CONSTANT(LCD_HEIGHT),
         RB_CONSTANT(LCD_WIDTH),
+        RB_CONSTANT(SCREEN_MAIN),
 #ifdef HAVE_REMOTE_LCD
         RB_CONSTANT(LCD_REMOTE_DEPTH),
         RB_CONSTANT(LCD_REMOTE_HEIGHT),
         RB_CONSTANT(LCD_REMOTE_WIDTH),
+        RB_CONSTANT(SCREEN_REMOTE),
 #endif
 
         RB_CONSTANT(FONT_SYSFIXED),
@@ -572,12 +667,6 @@
         RB_CONSTANT(PLAYLIST_PREPEND),
         RB_CONSTANT(PLAYLIST_REPLACE),
 
-
-        RB_CONSTANT(SCREEN_MAIN),
-#ifdef HAVE_REMOTE_LCD
-        RB_CONSTANT(SCREEN_REMOTE),
-#endif
-
 #ifdef HAVE_TOUCHSCREEN
         RB_CONSTANT(TOUCHSCREEN_POINT),
         RB_CONSTANT(TOUCHSCREEN_BUTTON),
diff --git a/apps/plugins/lua/rocklib_aux.pl b/apps/plugins/lua/rocklib_aux.pl
index f3705bf..cad2f88 100755
--- a/apps/plugins/lua/rocklib_aux.pl
+++ b/apps/plugins/lua/rocklib_aux.pl
@@ -70,6 +70,7 @@
                            '^s?+rand$',
                            '^strl?+cpy$',
                            '^strl?+cat$',
+                           'strn?+casecmp$',
                            '^iso_decode$',
                            '^utf8encode$',
                            '^utf16',
@@ -84,6 +85,9 @@
                            '^lcd_(set|get)_(fore|back)ground$',
                            '^lcd_put(s|sxy|s_scroll)$',
                            '^lcd_scroll_stop$',
+                           '^backlight_o(n|ff)$',
+                           '^backlight_set_brightness$',
+                           '^buttonlight_set_brightness$',
                            '^__.+$',
                            '^.+_(un)?cached$',
                            '^audio_(status|get_file_pos|flush_and_reload_tracks)$',
@@ -95,6 +99,8 @@
                            '^pcm_play_(stop|pause|lock|unlock)$',
                            '^pcm_(apply_settings|get_bytes_waiting)$',
                            '^pcm_(set_frequency|calculate_peaks)$',
+                           '^mixer_(set|get)_frequency$',
+                           '^(trigger|cancel)_cpu_boost$',
                            '^round_value_to_list32$');
 
 my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);