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/rocklib.c | 219 +++++++++++++++++++++++++++++++-------------- 1 file changed, 154 insertions(+), 65 deletions(-) (limited to 'apps/plugins/lua/rocklib.c') 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), -- cgit v1.2.3