From f6e10b84882387e304467f22ea2f6126cbaa1264 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Thu, 1 Nov 2018 14:20:33 -0400 Subject: 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 --- apps/plugins/lua/include_lua/rbcompat.lua | 60 ++++++++ apps/plugins/lua/lua.make | 3 +- apps/plugins/lua/rocklib.c | 219 +++++++++++++++++++++--------- apps/plugins/lua/rocklib_aux.pl | 6 + 4 files changed, 221 insertions(+), 67 deletions(-) create mode 100644 apps/plugins/lua/include_lua/rbcompat.lua diff --git a/apps/plugins/lua/include_lua/rbcompat.lua b/apps/plugins/lua/include_lua/rbcompat.lua new file mode 100644 index 0000000000..377bc2e4e0 --- /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 56df860545..a464bddace 100644 --- a/apps/plugins/lua/lua.make +++ b/apps/plugins/lua/lua.make @@ -17,8 +17,7 @@ OTHER_SRC += $(LUA_SRC) 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 44eb549dae..5995fd89e9 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,27 +63,44 @@ RB_WRAP(current_tick) return 1; } -RB_WRAP(kbd_input) +#ifdef HAVE_SCHEDULER_BOOSTCTRL +RB_WRAP(schedule_cpu_boost) { - luaL_Buffer b; - luaL_buffinit(L, &b); - - const char *input = lua_tostring(L, 1); - char *buffer = luaL_prepbuffer(&b); + bool boost = luaL_checkboolean(L, 1); - if(input != NULL) - rb->strlcpy(buffer, input, LUAL_BUFFERSIZE); + if(boost) + rb->trigger_cpu_boost(); else - buffer[0] = '\0'; + rb->cancel_cpu_boost(); - if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE)) - { - luaL_addstring(&b, buffer); - luaL_pushresult(&b); - } + 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 - return 0; +#endif + btn = pluginlib_getaction(timeout, m1, 1); + lua_pushinteger(L, btn); return 1; } @@ -98,23 +116,42 @@ RB_WRAP(action_get_touchscreen_press) 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) +RB_WRAP(touchscreen_mode) { - lua_pushinteger(L, rb->touchscreen_get_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(current_path) +RB_WRAP(kbd_input) { - return get_current_path(L, 1); + luaL_Buffer b; + luaL_buffinit(L, &b); + + const char *input = lua_tostring(L, 1); + char *buffer = luaL_prepbuffer(&b); + + if(input != NULL) + rb->strlcpy(buffer, input, LUAL_BUFFERSIZE); + else + buffer[0] = '\0'; + + if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE)) + { + luaL_addstring(&b, buffer); + luaL_pushresult(&b); + } + else + return 0; + + return 1; } static const char ** get_table_items(lua_State *L, int pos, int *count) @@ -189,6 +226,9 @@ RB_WRAP(do_menu) return 1; } + +/* DEVICE AUDIO / PLAYLIST CONTROL */ + RB_WRAP(playlist) { /* just passes NULL to work with the current playlist */ @@ -391,8 +431,33 @@ RB_WRAP(pcm) 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 @@ SIMPLE_VOID_WRAPPER(buttonlight_use_settings); #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 @@ RB_WRAP(utf8encode) 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), +#ifdef HAVE_SCHEDULER_BOOSTCTRL + RB_FUNC(schedule_cpu_boost), +#endif + + RB_FUNC(current_path), - /* Buttons */ + /* DEVICE INPUT CONTROL */ + RB_FUNC(get_plugin_action), #ifdef HAVE_TOUCHSCREEN RB_FUNC(action_get_touchscreen_press), - RB_FUNC(touchscreen_set_mode), - RB_FUNC(touchscreen_get_mode), + RB_FUNC(touchscreen_mode), #endif - RB_FUNC(kbd_input), - - RB_FUNC(current_path), 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 @@ static const luaL_Reg rocklib[] = #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 @@ LUALIB_API int luaopen_rock(lua_State *L) { 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 @@ LUALIB_API int luaopen_rock(lua_State *L) 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 @@ LUALIB_API int luaopen_rock(lua_State *L) 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 f3705bf2d1..cad2f887ef 100755 --- a/apps/plugins/lua/rocklib_aux.pl +++ b/apps/plugins/lua/rocklib_aux.pl @@ -70,6 +70,7 @@ my @forbidden_functions = ('^open$', '^s?+rand$', '^strl?+cpy$', '^strl?+cat$', + 'strn?+casecmp$', '^iso_decode$', '^utf8encode$', '^utf16', @@ -84,6 +85,9 @@ my @forbidden_functions = ('^open$', '^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 @@ my @forbidden_functions = ('^open$', '^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]); -- cgit v1.2.3