summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <me.theuser@yahoo.com>2018-11-01 14:20:33 -0400
committerWilliam Wilgus <me.theuser@yahoo.com>2018-11-02 18:00:06 +0100
commitf6e10b84882387e304467f22ea2f6126cbaa1264 (patch)
tree1dd56d2d7d524abd66ce286968d83b2b2302500e
parentbb025e3962225491785cc8e73e89190adbacaa4e (diff)
downloadrockbox-f6e10b84882387e304467f22ea2f6126cbaa1264.tar.gz
rockbox-f6e10b84882387e304467f22ea2f6126cbaa1264.zip
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
-rw-r--r--apps/plugins/lua/include_lua/rbcompat.lua60
-rw-r--r--apps/plugins/lua/lua.make3
-rw-r--r--apps/plugins/lua/rocklib.c219
-rwxr-xr-xapps/plugins/lua/rocklib_aux.pl6
4 files changed, 221 insertions, 67 deletions
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 @@
1--[[ Lua RB Compatibility Operations
2/***************************************************************************
3 * __________ __ ___.
4 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 * \/ \/ \/ \/ \/
9 * $Id$
10 *
11 * Copyright (C) 2018 William Wilgus
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22]]
23
24-- [[ compatibility with old functions ]]
25if rb.strncasecmp then rb.strcasecmp = function(s1, s2) return rb.strncasecmp(s1, s2) end end
26
27if rb.backlight_brightness_set then
28 rb.backlight_set_brightness = function(brightness) rb.backlight_brightness_set(brightness) end
29 rb.backlight_brightness_use_setting = function() rb.backlight_brightness_set(nil) end
30end
31
32if rb.buttonlight_brightness_set then
33 rb.buttonlight_set_brightness = function(brightness) rb.buttonlight_brightness_set(brightness) end
34 rb.buttonlight_brightness_use_setting = function() rb.buttonlight_brightness_set(nil) end
35end
36
37if rb.mixer_frequency then
38 rb.mixer_set_frequency = function(freq) rb.mixer_frequency(freq) end
39 rb.mixer_get_frequency = function() return rb.mixer_frequency(nil) end
40end
41
42if rb.backlight_onoff then
43 rb.backlight_on = function() rb.backlight_onoff(true) end
44 rb.backlight_off = function() rb.backlight_onoff(false) end
45end
46
47if rb.buttonlight_brightness_set then
48 rb.buttonlight_set_brightness = function(brightness) rb.buttonlight_brightness_set(brightness) end
49 rb.buttonlight_brightness_use_setting = function() rb.buttonlight_brightness_set(nil) end
50end
51
52if rb.touchscreen_mode then
53 rb.touchscreen_set_mode = function(mode) rb.touchscreen_mode(mode) end
54 rb.touchscreen_get_mode = function() return rb.touchscreen_mode(nil) end
55end
56
57if rb.schedule_cpu_boost then
58 rb.trigger_cpu_boost = function() rb.schedule_cpu_boost(true) end
59 rb.cancel_cpu_boost = function() rb.schedule_cpu_boost(false) end
60end
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)
17 17
18LUA_INCLUDEDIR := $(LUA_SRCDIR)/include_lua 18LUA_INCLUDEDIR := $(LUA_SRCDIR)/include_lua
19LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua draw.lua \ 19LUA_INCLUDELIST := $(addprefix $(LUA_BUILDDIR)/,audio.lua blit.lua color.lua draw.lua \
20 image.lua lcd.lua math_ex.lua print.lua \ 20 image.lua lcd.lua math_ex.lua print.lua timer.lua playlist.lua pcm.lua rbcompat.lua)
21 timer.lua playlist.lua pcm.lua)
22 21
23 22
24ifndef APP_TYPE 23ifndef 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 @@
9 * 9 *
10 * Copyright (C) 2008 Dan Everton (safetydan) 10 * Copyright (C) 2008 Dan Everton (safetydan)
11 * Copyright (C) 2009 Maurus Cuelenaere 11 * Copyright (C) 2009 Maurus Cuelenaere
12 * Copyright (C) 2018 William Wilgus
12 * 13 *
13 * This program is free software; you can redistribute it and/or 14 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License 15 * modify it under the terms of the GNU General Public License
@@ -62,27 +63,44 @@ RB_WRAP(current_tick)
62 return 1; 63 return 1;
63} 64}
64 65
65RB_WRAP(kbd_input) 66#ifdef HAVE_SCHEDULER_BOOSTCTRL
67RB_WRAP(schedule_cpu_boost)
66{ 68{
67 luaL_Buffer b; 69 bool boost = luaL_checkboolean(L, 1);
68 luaL_buffinit(L, &b);
69
70 const char *input = lua_tostring(L, 1);
71 char *buffer = luaL_prepbuffer(&b);
72 70
73 if(input != NULL) 71 if(boost)
74 rb->strlcpy(buffer, input, LUAL_BUFFERSIZE); 72 rb->trigger_cpu_boost();
75 else 73 else
76 buffer[0] = '\0'; 74 rb->cancel_cpu_boost();
77 75
78 if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE)) 76 return 0;
79 { 77}
80 luaL_addstring(&b, buffer); 78#endif
81 luaL_pushresult(&b); 79
82 } 80RB_WRAP(current_path)
81{
82 return get_current_path(L, 1);
83}
84
85
86/* DEVICE INPUT CONTROL */
87
88RB_WRAP(get_plugin_action)
89{
90 static const struct button_mapping *m1[] = { pla_main_ctx };
91 int timeout = luaL_checkint(L, 1);
92 int btn;
93
94#ifdef HAVE_REMOTE_LCD
95 static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx };
96 bool with_remote = luaL_optint(L, 2, 0);
97 if (with_remote)
98 btn = pluginlib_getaction(timeout, m2, 2);
83 else 99 else
84 return 0; 100#endif
101 btn = pluginlib_getaction(timeout, m1, 1);
85 102
103 lua_pushinteger(L, btn);
86 return 1; 104 return 1;
87} 105}
88 106
@@ -98,23 +116,42 @@ RB_WRAP(action_get_touchscreen_press)
98 return 3; 116 return 3;
99} 117}
100 118
101RB_WRAP(touchscreen_set_mode) 119RB_WRAP(touchscreen_mode)
102{
103 enum touchscreen_mode mode = luaL_checkint(L, 1);
104 rb->touchscreen_set_mode(mode);
105 return 0;
106}
107
108RB_WRAP(touchscreen_get_mode)
109{ 120{
110 lua_pushinteger(L, rb->touchscreen_get_mode()); 121 int origmode = rb->touchscreen_get_mode();
122 if(!lua_isnoneornil(L, 1))
123 {
124 enum touchscreen_mode mode = luaL_checkint(L, 1);
125 rb->touchscreen_set_mode(mode);
126 }
127 lua_pushinteger(L, origmode);
111 return 1; 128 return 1;
112} 129}
130
113#endif 131#endif
114 132
115RB_WRAP(current_path) 133RB_WRAP(kbd_input)
116{ 134{
117 return get_current_path(L, 1); 135 luaL_Buffer b;
136 luaL_buffinit(L, &b);
137
138 const char *input = lua_tostring(L, 1);
139 char *buffer = luaL_prepbuffer(&b);
140
141 if(input != NULL)
142 rb->strlcpy(buffer, input, LUAL_BUFFERSIZE);
143 else
144 buffer[0] = '\0';
145
146 if(!rb->kbd_input(buffer, LUAL_BUFFERSIZE))
147 {
148 luaL_addstring(&b, buffer);
149 luaL_pushresult(&b);
150 }
151 else
152 return 0;
153
154 return 1;
118} 155}
119 156
120static const char ** get_table_items(lua_State *L, int pos, int *count) 157static const char ** get_table_items(lua_State *L, int pos, int *count)
@@ -189,6 +226,9 @@ RB_WRAP(do_menu)
189 return 1; 226 return 1;
190} 227}
191 228
229
230/* DEVICE AUDIO / PLAYLIST CONTROL */
231
192RB_WRAP(playlist) 232RB_WRAP(playlist)
193{ 233{
194 /* just passes NULL to work with the current playlist */ 234 /* just passes NULL to work with the current playlist */
@@ -391,8 +431,33 @@ RB_WRAP(pcm)
391 rb->yield(); 431 rb->yield();
392 return 1; 432 return 1;
393} 433}
434
435RB_WRAP(mixer_frequency)
436{
437 unsigned int result = rb->mixer_get_frequency();
438
439 if(!lua_isnoneornil(L, 1))
440 {
441 unsigned int samplerate = (unsigned int) luaL_checkint(L, 1);
442 rb->mixer_set_frequency(samplerate);
443 }
444 lua_pushinteger(L, result);
445 return 1;
446}
394#endif /*CONFIG_CODEC == SWCODEC*/ 447#endif /*CONFIG_CODEC == SWCODEC*/
395 448
449/* DEVICE LIGHTING CONTROL */
450RB_WRAP(backlight_onoff)
451{
452 bool on = luaL_checkboolean(L, 1);
453 if(on)
454 rb->backlight_on();
455 else
456 rb->backlight_off();
457
458 return 0;
459}
460
396SIMPLE_VOID_WRAPPER(backlight_force_on); 461SIMPLE_VOID_WRAPPER(backlight_force_on);
397SIMPLE_VOID_WRAPPER(backlight_use_settings); 462SIMPLE_VOID_WRAPPER(backlight_use_settings);
398 463
@@ -409,32 +474,35 @@ SIMPLE_VOID_WRAPPER(buttonlight_use_settings);
409#ifdef HAVE_BACKLIGHT_BRIGHTNESS 474#ifdef HAVE_BACKLIGHT_BRIGHTNESS
410RB_WRAP(backlight_brightness_set) 475RB_WRAP(backlight_brightness_set)
411{ 476{
412 int brightness = luaL_checkint(L, 1); 477 if(lua_isnoneornil(L, 1))
413 backlight_brightness_set(brightness); 478 backlight_brightness_use_setting();
479 else
480 {
481 int brightness = luaL_checkint(L, 1);
482 backlight_brightness_set(brightness);
483 }
414 484
415 return 0; 485 return 0;
416} 486}
417SIMPLE_VOID_WRAPPER(backlight_brightness_use_setting);
418#endif 487#endif
419 488
420RB_WRAP(get_plugin_action) 489#ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
490RB_WRAP(buttonlight_brightness_set)
421{ 491{
422 static const struct button_mapping *m1[] = { pla_main_ctx }; 492 if(lua_isnoneornil(L, 1))
423 int timeout = luaL_checkint(L, 1); 493 buttonlight_brightness_use_setting();
424 int btn;
425
426#ifdef HAVE_REMOTE_LCD
427 static const struct button_mapping *m2[] = { pla_main_ctx, pla_remote_ctx };
428 bool with_remote = luaL_optint(L, 2, 0);
429 if (with_remote)
430 btn = pluginlib_getaction(timeout, m2, 2);
431 else 494 else
432#endif 495 {
433 btn = pluginlib_getaction(timeout, m1, 1); 496 int brightness = luaL_checkint(L, 1);
497 buttonlight_brightness_set(brightness);
498 }
434 499
435 lua_pushinteger(L, btn); 500 return 0;
436 return 1;
437} 501}
502#endif
503
504
505/* DEVICE STRING / FILENAME MANIPULATION */
438 506
439RB_WRAP(strip_extension) 507RB_WRAP(strip_extension)
440{ 508{
@@ -482,25 +550,53 @@ RB_WRAP(utf8encode)
482 return 1; 550 return 1;
483} 551}
484 552
553RB_WRAP(strncasecmp)
554{
555 int result;
556 const char * s1 = luaL_checkstring(L, 1);
557 const char * s2 = luaL_checkstring(L, 2);
558 if(lua_isnoneornil(L, 3))
559 result = rb->strcasecmp(s1, s2);
560 else
561 result = rb->strncasecmp(s1, s2, (size_t) luaL_checkint(L, 3));
562
563 lua_pushinteger(L, result);
564 return 1;
565}
566
485#define RB_FUNC(func) {#func, rock_##func} 567#define RB_FUNC(func) {#func, rock_##func}
568#define RB_ALIAS(name, func) {name, rock_##func}
486static const luaL_Reg rocklib[] = 569static const luaL_Reg rocklib[] =
487{ 570{
488 /* Kernel */ 571 /* Kernel */
489 RB_FUNC(current_tick), 572 RB_FUNC(current_tick),
573#ifdef HAVE_SCHEDULER_BOOSTCTRL
574 RB_FUNC(schedule_cpu_boost),
575#endif
576
577 RB_FUNC(current_path),
490 578
491 /* Buttons */ 579 /* DEVICE INPUT CONTROL */
580 RB_FUNC(get_plugin_action),
492#ifdef HAVE_TOUCHSCREEN 581#ifdef HAVE_TOUCHSCREEN
493 RB_FUNC(action_get_touchscreen_press), 582 RB_FUNC(action_get_touchscreen_press),
494 RB_FUNC(touchscreen_set_mode), 583 RB_FUNC(touchscreen_mode),
495 RB_FUNC(touchscreen_get_mode),
496#endif 584#endif
497
498 RB_FUNC(kbd_input), 585 RB_FUNC(kbd_input),
499
500 RB_FUNC(current_path),
501 RB_FUNC(gui_syncyesno_run), 586 RB_FUNC(gui_syncyesno_run),
502 RB_FUNC(do_menu), 587 RB_FUNC(do_menu),
503 588
589 /* DEVICE AUDIO / PLAYLIST CONTROL */
590 RB_FUNC(audio),
591 RB_FUNC(playlist),
592#if CONFIG_CODEC == SWCODEC
593 RB_FUNC(pcm),
594 RB_FUNC(mixer_frequency),
595#endif
596
597 /* DEVICE LIGHTING CONTROL */
598 RB_FUNC(backlight_onoff),
599
504 /* Backlight helper */ 600 /* Backlight helper */
505 RB_FUNC(backlight_force_on), 601 RB_FUNC(backlight_force_on),
506 RB_FUNC(backlight_use_settings), 602 RB_FUNC(backlight_use_settings),
@@ -517,25 +613,22 @@ static const luaL_Reg rocklib[] =
517 613
518#ifdef HAVE_BACKLIGHT_BRIGHTNESS 614#ifdef HAVE_BACKLIGHT_BRIGHTNESS
519 RB_FUNC(backlight_brightness_set), 615 RB_FUNC(backlight_brightness_set),
520 RB_FUNC(backlight_brightness_use_setting),
521#endif 616#endif
522 617
523 RB_FUNC(get_plugin_action), 618#ifdef HAVE_BUTTONLIGHT_BRIGHTNESS
619 RB_FUNC(buttonlight_brightness_set),
620#endif
524 621
622 /* DEVICE STRING / FILENAME MANIPULATION */
525 RB_FUNC(strip_extension), 623 RB_FUNC(strip_extension),
526 RB_FUNC(create_numbered_filename), 624 RB_FUNC(create_numbered_filename),
527
528 RB_FUNC(audio),
529 RB_FUNC(playlist),
530#if CONFIG_CODEC == SWCODEC
531 RB_FUNC(pcm),
532#endif
533
534 RB_FUNC(utf8encode), 625 RB_FUNC(utf8encode),
626 RB_FUNC(strncasecmp),
535 627
536 {NULL, NULL} 628 {NULL, NULL}
537}; 629};
538#undef RB_FUNC 630#undef RB_FUNC
631#undef RB_ALIAS
539 632
540extern const luaL_Reg rocklib_aux[]; 633extern const luaL_Reg rocklib_aux[];
541 634
@@ -546,7 +639,7 @@ LUALIB_API int luaopen_rock(lua_State *L)
546{ 639{
547 luaL_register(L, LUA_ROCKLIBNAME, rocklib); 640 luaL_register(L, LUA_ROCKLIBNAME, rocklib);
548 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux); 641 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux);
549 642
550 static const struct lua_int_reg rlib_const_int[] = 643 static const struct lua_int_reg rlib_const_int[] =
551 { 644 {
552 /* useful integer constants */ 645 /* useful integer constants */
@@ -555,10 +648,12 @@ LUALIB_API int luaopen_rock(lua_State *L)
555 RB_CONSTANT(LCD_DEPTH), 648 RB_CONSTANT(LCD_DEPTH),
556 RB_CONSTANT(LCD_HEIGHT), 649 RB_CONSTANT(LCD_HEIGHT),
557 RB_CONSTANT(LCD_WIDTH), 650 RB_CONSTANT(LCD_WIDTH),
651 RB_CONSTANT(SCREEN_MAIN),
558#ifdef HAVE_REMOTE_LCD 652#ifdef HAVE_REMOTE_LCD
559 RB_CONSTANT(LCD_REMOTE_DEPTH), 653 RB_CONSTANT(LCD_REMOTE_DEPTH),
560 RB_CONSTANT(LCD_REMOTE_HEIGHT), 654 RB_CONSTANT(LCD_REMOTE_HEIGHT),
561 RB_CONSTANT(LCD_REMOTE_WIDTH), 655 RB_CONSTANT(LCD_REMOTE_WIDTH),
656 RB_CONSTANT(SCREEN_REMOTE),
562#endif 657#endif
563 658
564 RB_CONSTANT(FONT_SYSFIXED), 659 RB_CONSTANT(FONT_SYSFIXED),
@@ -572,12 +667,6 @@ LUALIB_API int luaopen_rock(lua_State *L)
572 RB_CONSTANT(PLAYLIST_PREPEND), 667 RB_CONSTANT(PLAYLIST_PREPEND),
573 RB_CONSTANT(PLAYLIST_REPLACE), 668 RB_CONSTANT(PLAYLIST_REPLACE),
574 669
575
576 RB_CONSTANT(SCREEN_MAIN),
577#ifdef HAVE_REMOTE_LCD
578 RB_CONSTANT(SCREEN_REMOTE),
579#endif
580
581#ifdef HAVE_TOUCHSCREEN 670#ifdef HAVE_TOUCHSCREEN
582 RB_CONSTANT(TOUCHSCREEN_POINT), 671 RB_CONSTANT(TOUCHSCREEN_POINT),
583 RB_CONSTANT(TOUCHSCREEN_BUTTON), 672 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$',
70 '^s?+rand$', 70 '^s?+rand$',
71 '^strl?+cpy$', 71 '^strl?+cpy$',
72 '^strl?+cat$', 72 '^strl?+cat$',
73 'strn?+casecmp$',
73 '^iso_decode$', 74 '^iso_decode$',
74 '^utf8encode$', 75 '^utf8encode$',
75 '^utf16', 76 '^utf16',
@@ -84,6 +85,9 @@ my @forbidden_functions = ('^open$',
84 '^lcd_(set|get)_(fore|back)ground$', 85 '^lcd_(set|get)_(fore|back)ground$',
85 '^lcd_put(s|sxy|s_scroll)$', 86 '^lcd_put(s|sxy|s_scroll)$',
86 '^lcd_scroll_stop$', 87 '^lcd_scroll_stop$',
88 '^backlight_o(n|ff)$',
89 '^backlight_set_brightness$',
90 '^buttonlight_set_brightness$',
87 '^__.+$', 91 '^__.+$',
88 '^.+_(un)?cached$', 92 '^.+_(un)?cached$',
89 '^audio_(status|get_file_pos|flush_and_reload_tracks)$', 93 '^audio_(status|get_file_pos|flush_and_reload_tracks)$',
@@ -95,6 +99,8 @@ my @forbidden_functions = ('^open$',
95 '^pcm_play_(stop|pause|lock|unlock)$', 99 '^pcm_play_(stop|pause|lock|unlock)$',
96 '^pcm_(apply_settings|get_bytes_waiting)$', 100 '^pcm_(apply_settings|get_bytes_waiting)$',
97 '^pcm_(set_frequency|calculate_peaks)$', 101 '^pcm_(set_frequency|calculate_peaks)$',
102 '^mixer_(set|get)_frequency$',
103 '^(trigger|cancel)_cpu_boost$',
98 '^round_value_to_list32$'); 104 '^round_value_to_list32$');
99 105
100my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]); 106my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);