2009-07-05 15:33:08 +00:00
|
|
|
#!/usr/bin/env perl
|
2009-07-05 15:39:17 +00:00
|
|
|
############################################################################
|
|
|
|
# __________ __ ___.
|
|
|
|
# Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
# \/ \/ \/ \/ \/
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 by Maurus Cuelenaere
|
|
|
|
#
|
|
|
|
# All files in this archive are subject to the GNU General Public License.
|
|
|
|
# See the file COPYING in the source tree root for full license agreement.
|
|
|
|
#
|
|
|
|
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, either express or implied.
|
|
|
|
#
|
|
|
|
############################################################################
|
2009-07-05 15:33:08 +00:00
|
|
|
|
2010-06-18 12:28:34 +00:00
|
|
|
|
|
|
|
# The purpose of this script is to automatically generate Lua wrappers for
|
|
|
|
# (easily) portable C functions used in the Rockbox plugin API.
|
2019-09-27 04:23:16 +00:00
|
|
|
# It doesn't contain support for structs or pointers (apart from char*).
|
2010-06-18 12:28:34 +00:00
|
|
|
#
|
|
|
|
# The output will be written to <build_dir>/apps/plugins/lua/rocklib_aux.c
|
|
|
|
|
2009-07-05 15:33:08 +00:00
|
|
|
sub trim
|
|
|
|
{
|
|
|
|
my $text = $_[0];
|
|
|
|
$text =~ s/^\s+//;
|
|
|
|
$text =~ s/\s+$//;
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rand_string
|
|
|
|
{
|
|
|
|
my @chars=('a'..'z');
|
|
|
|
my $ret;
|
|
|
|
foreach (1..5)
|
|
|
|
{
|
|
|
|
$ret .= $chars[rand @chars];
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my @functions;
|
|
|
|
my @ported_functions;
|
2010-06-18 12:28:34 +00:00
|
|
|
# These functions are excluded from automatically wrapping. This is useful if
|
|
|
|
# you want to manually port them to Lua. The format is a standard Perl regular
|
|
|
|
# expression.
|
2019-09-27 04:23:16 +00:00
|
|
|
my @forbidden_functions = ('^atoi$',
|
|
|
|
'^open$',
|
2018-10-12 15:58:09 +00:00
|
|
|
'^open_utf8$',
|
2009-07-05 15:33:08 +00:00
|
|
|
'^close$',
|
2019-07-28 12:26:20 +00:00
|
|
|
'dcache',
|
2009-07-05 15:33:08 +00:00
|
|
|
'^read$',
|
|
|
|
'^write$',
|
2010-06-18 13:10:14 +00:00
|
|
|
'^mkdir$',
|
|
|
|
'^rmdir$',
|
2018-10-12 15:58:09 +00:00
|
|
|
'^remove$',
|
|
|
|
'^rename$',
|
2009-07-05 15:33:08 +00:00
|
|
|
'^lseek$',
|
|
|
|
'^ftruncate$',
|
|
|
|
'^filesize$',
|
|
|
|
'^fdprintf$',
|
|
|
|
'^read_line$',
|
|
|
|
'^[a-z]+dir$',
|
2018-10-23 04:11:34 +00:00
|
|
|
'^strip_extension$',
|
|
|
|
'^create_numbered_filename$',
|
2018-10-12 15:58:09 +00:00
|
|
|
'^s?+rand$',
|
2019-09-27 04:23:16 +00:00
|
|
|
'^strlen$',
|
2018-10-12 15:58:09 +00:00
|
|
|
'^strl?+cpy$',
|
|
|
|
'^strl?+cat$',
|
2018-11-01 18:20:33 +00:00
|
|
|
'strn?+casecmp$',
|
2018-10-31 02:40:23 +00:00
|
|
|
'^iso_decode$',
|
|
|
|
'^utf8encode$',
|
|
|
|
'^utf16',
|
2018-10-12 15:58:09 +00:00
|
|
|
'^codec_',
|
|
|
|
'^timer_',
|
2019-07-29 09:41:56 +00:00
|
|
|
'^lcd_blit_(mono|grey_phase)$',
|
2018-10-22 18:00:58 +00:00
|
|
|
'^lcd_(mono_)?+bitmap',
|
2018-10-27 10:24:27 +00:00
|
|
|
'^lcd_(draw|fill|update_)rect$',
|
|
|
|
'^lcd_draw(line|pixel)$',
|
|
|
|
'^lcd_(h|v)line$',
|
|
|
|
'^lcd_(update|clear_display|set_drawmode)$',
|
|
|
|
'^lcd_setfont$',
|
|
|
|
'^lcd_(set|get)_(fore|back)ground$',
|
|
|
|
'^lcd_put(s|sxy|s_scroll)$',
|
|
|
|
'^lcd_scroll_stop$',
|
2018-11-01 18:20:33 +00:00
|
|
|
'^backlight_o(n|ff)$',
|
|
|
|
'^backlight_set_brightness$',
|
|
|
|
'^buttonlight_set_brightness$',
|
2009-07-05 15:33:08 +00:00
|
|
|
'^__.+$',
|
|
|
|
'^.+_(un)?cached$',
|
2018-10-30 16:05:15 +00:00
|
|
|
'^audio_(status|get_file_pos|flush_and_reload_tracks)$',
|
|
|
|
'^audio_(ff_rewind|next|prev|play|pause|resume|stop)$',
|
|
|
|
'^playlist_(amount|add|create|start|resume|shuffle)$',
|
|
|
|
'^playlist_(sync|resume_track|remove_all_tracks)$',
|
|
|
|
'^playlist_(insert_track|insert_directory)$',
|
2018-10-30 17:56:36 +00:00
|
|
|
'^pcm_is_(playing|paused)$',
|
|
|
|
'^pcm_play_(stop|pause|lock|unlock)$',
|
|
|
|
'^pcm_(apply_settings|get_bytes_waiting)$',
|
|
|
|
'^pcm_(set_frequency|calculate_peaks)$',
|
lua add sound_current, consolidate sound_ functions
add defines for sound functions
SOUND_VOLUME, SOUND_BALANCE, SOUND_CHANNELS, SOUND_STEREO_WIDTH ...
defines depend on target
require "sound_defines" to add them to rb.sound_settings[]
consolidates:
sound_set, sound_current, sound_default, sound_min, sound_max,
sound_unit, sound_pitch, sound_val2phys to a single function
rb.sound("name", setting, [value])
require "sound.lua" for old functionality
Change-Id: Ice695218aa433f4fcbb48fbd6b8a9bf29c994110
2019-07-25 15:17:40 +00:00
|
|
|
'^sound_(set|current|default|min|max|unit|pitch|val2phys)$',
|
2018-11-01 18:20:33 +00:00
|
|
|
'^mixer_(set|get)_frequency$',
|
2019-09-27 04:23:16 +00:00
|
|
|
'^plugin_get_current_filename$',
|
2019-07-28 12:26:20 +00:00
|
|
|
'^plugin_release_audio_buffer$',
|
|
|
|
'^reload_directory$',
|
|
|
|
'^set_current_file$',
|
|
|
|
'^set_dirfilter$',
|
2019-09-27 04:23:16 +00:00
|
|
|
'^sleep$',
|
|
|
|
'^system_memory_guard$',
|
|
|
|
'^talk_number$',
|
|
|
|
'^talk_force_shutup$',
|
|
|
|
'^talk_spell$',
|
|
|
|
'^talk_shutup$',
|
2018-11-01 18:20:33 +00:00
|
|
|
'^(trigger|cancel)_cpu_boost$',
|
2019-07-28 12:26:20 +00:00
|
|
|
'^thread_',
|
2010-05-14 07:17:03 +00:00
|
|
|
'^round_value_to_list32$');
|
2009-07-05 15:33:08 +00:00
|
|
|
|
|
|
|
my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);
|
|
|
|
open ROCKLIB, "<$rocklib" or die("Couldn't open $rocklib: $!");
|
|
|
|
while(<ROCKLIB>)
|
|
|
|
{
|
|
|
|
if(/^RB_WRAP\(([^)]+)\)/)
|
|
|
|
{
|
|
|
|
push(@ported_functions, $1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close ROCKLIB;
|
|
|
|
|
|
|
|
# Parse plugin.h
|
|
|
|
my $start = 0;
|
|
|
|
while(<STDIN>)
|
|
|
|
{
|
|
|
|
if(/struct plugin_api \{/)
|
|
|
|
{
|
|
|
|
$start = 1;
|
|
|
|
}
|
|
|
|
elsif($start && /\};/)
|
|
|
|
{
|
|
|
|
$start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($start == 1)
|
|
|
|
{
|
|
|
|
my $line = $_;
|
|
|
|
while($line !~ /;/)
|
|
|
|
{
|
|
|
|
$line .= <STDIN>;
|
|
|
|
}
|
|
|
|
|
|
|
|
$line =~ s/(\n|\r)//g;
|
|
|
|
|
|
|
|
if($line =~ /([a-zA-Z *_]+)?\s?\(\*([^)]+)?\)\(([^)]+)\).*?;/)
|
|
|
|
{
|
|
|
|
$return_type = $1;
|
|
|
|
$name = $2;
|
|
|
|
$arguments = $3;
|
|
|
|
|
|
|
|
$return_type = trim($return_type);
|
|
|
|
$arguments =~ s/\s{2,}/ /g;
|
|
|
|
|
|
|
|
if( !grep($_ eq $name, @ported_functions) &&
|
|
|
|
!grep($name =~ $_, @forbidden_functions))
|
|
|
|
{
|
|
|
|
push(@functions, {'name' => $name, 'return' => $return_type, 'arg' => $arguments});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $svnrev = '$Revision$';
|
|
|
|
|
|
|
|
# Print the header
|
|
|
|
print <<EOF
|
2010-06-18 12:28:34 +00:00
|
|
|
/* Automatically generated from rocklib.c & plugin.h ($svnrev)
|
|
|
|
*
|
|
|
|
* See apps/plugins/lua/rocklib_aux.pl for the generator.
|
|
|
|
*/
|
2009-07-05 15:33:08 +00:00
|
|
|
|
|
|
|
#define lrocklib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#define _ROCKCONF_H_ /* We don't need strcmp() etc. wrappers */
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
EOF
|
|
|
|
;
|
|
|
|
|
|
|
|
my %in_types = ('void' => \&in_void,
|
2019-09-27 04:23:16 +00:00
|
|
|
'enum' => \&in_int,
|
2009-07-05 15:33:08 +00:00
|
|
|
'int' => \&in_int,
|
|
|
|
'unsigned' => \&in_int,
|
|
|
|
'unsignedint' => \&in_int,
|
|
|
|
'signed' => \&in_int,
|
|
|
|
'signedint' => \&in_int,
|
|
|
|
'short' => \&in_int,
|
|
|
|
'unsignedshort' => \&in_int,
|
|
|
|
'signedshort' => \&in_int,
|
|
|
|
'long' => \&in_int,
|
|
|
|
'unsignedlong' => \&in_int,
|
|
|
|
'signedlong' => \&in_int,
|
|
|
|
'char' => \&in_int,
|
|
|
|
'unsignedchar' => \&in_int,
|
|
|
|
'signedchar' => \&in_int,
|
2009-10-29 16:52:09 +00:00
|
|
|
'size_t' => \&in_int,
|
|
|
|
'ssize_t' => \&in_int,
|
|
|
|
'off_t' => \&in_int,
|
2009-07-05 15:33:08 +00:00
|
|
|
'char*' => \&in_string,
|
|
|
|
'signedchar*' => \&in_string,
|
|
|
|
'unsignedchar*' => \&in_string,
|
|
|
|
'bool' => \&in_bool,
|
|
|
|
'_Bool' => \&in_bool
|
|
|
|
), %out_types = ('void' => \&out_void,
|
|
|
|
'int' => \&out_int,
|
|
|
|
'unsigned' => \&out_int,
|
|
|
|
'unsignedint' => \&out_int,
|
|
|
|
'signed' => \&out_int,
|
|
|
|
'signedint' => \&out_int,
|
|
|
|
'short' => \&out_int,
|
|
|
|
'unsignedshort' => \&out_int,
|
|
|
|
'signedshort' => \&out_int,
|
|
|
|
'long' => \&out_int,
|
|
|
|
'unsignedlong' => \&out_int,
|
|
|
|
'signedlong' => \&out_int,
|
|
|
|
'char' => \&out_int,
|
|
|
|
'unsignedchar' => \&out_int,
|
|
|
|
'signedchar' => \&out_int,
|
2009-10-29 16:52:09 +00:00
|
|
|
'size_t' => \&out_int,
|
|
|
|
'ssize_t' => \&out_int,
|
|
|
|
'off_t' => \&out_int,
|
2009-07-05 15:33:08 +00:00
|
|
|
'char*' => \&out_string,
|
|
|
|
'signedchar*' => \&out_string,
|
|
|
|
'unsignedchar*' => \&out_string,
|
|
|
|
'bool' => \&out_bool,
|
|
|
|
'_Bool' => \&out_bool
|
|
|
|
);
|
|
|
|
|
|
|
|
sub in_void
|
|
|
|
{
|
|
|
|
return "\t(void)L;\n";
|
|
|
|
}
|
|
|
|
|
2019-09-27 04:23:16 +00:00
|
|
|
sub in_null
|
|
|
|
{
|
|
|
|
my ($name, $type, $pos) = @_;
|
|
|
|
return sprintf("\t%s %s = NULL;\n", $type, $name, $type, $pos)
|
|
|
|
}
|
|
|
|
|
2009-07-05 15:33:08 +00:00
|
|
|
sub in_int
|
|
|
|
{
|
|
|
|
my ($name, $type, $pos) = @_;
|
|
|
|
return sprintf("\t%s %s = (%s) luaL_checkint(L, %d);\n", $type, $name, $type, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub in_string
|
|
|
|
{
|
|
|
|
my ($name, $type, $pos) = @_;
|
|
|
|
return sprintf("\t%s %s = (%s) luaL_checkstring(L, %d);\n", $type, $name, $type, $pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub in_bool
|
|
|
|
{
|
|
|
|
my ($name, $type, $pos) = @_;
|
2014-04-02 18:46:06 +00:00
|
|
|
return sprintf("\tbool %s = luaL_checkboolean(L, %d);\n", $name, $pos)
|
2009-07-05 15:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub out_void
|
|
|
|
{
|
|
|
|
my $name = $_[0];
|
|
|
|
return sprintf("\t%s;\n\treturn 0;\n", $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub out_int
|
|
|
|
{
|
|
|
|
my ($name, $type) = @_;
|
|
|
|
return sprintf("\t%s result = %s;\n\tlua_pushinteger(L, result);\n\treturn 1;\n", $type, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub out_string
|
|
|
|
{
|
|
|
|
my ($name, $type) = @_;
|
|
|
|
return sprintf("\t%s result = %s;\n\tlua_pushstring(L, result);\n\treturn 1;\n", $type, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub out_bool
|
|
|
|
{
|
|
|
|
my ($name, $type) = @_;
|
|
|
|
return sprintf("\tbool result = %s;\n\tlua_pushboolean(L, result);\n\treturn 1;\n", $name);
|
|
|
|
}
|
|
|
|
|
2019-07-17 15:22:21 +00:00
|
|
|
#Sort the functions
|
|
|
|
my @sorted_functions = sort { @$a{'name'} cmp @$b{'name'} } @functions;
|
|
|
|
|
2009-07-05 15:33:08 +00:00
|
|
|
# Print the functions
|
|
|
|
my @valid_functions;
|
2019-07-17 15:22:21 +00:00
|
|
|
foreach my $function (@sorted_functions)
|
2009-07-05 15:33:08 +00:00
|
|
|
{
|
|
|
|
my $valid = 1, @arguments = ();
|
|
|
|
# Check for supported arguments
|
|
|
|
foreach my $argument (split(/,/, @$function{'arg'}))
|
|
|
|
{
|
|
|
|
$argument = trim($argument);
|
|
|
|
if($argument !~ /\[.+\]/ && ($argument =~ /^(.+[\s*])([^[*\s]*)/
|
|
|
|
|| $argument eq "void"))
|
|
|
|
{
|
|
|
|
my $literal_type, $type, $name;
|
|
|
|
if($argument eq "void")
|
|
|
|
{
|
|
|
|
$literal_type = "void", $type = "void", $name = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$literal_type = trim($1), $name = trim($2), $type = trim($1);
|
|
|
|
$type =~ s/(\s|const)//g;
|
|
|
|
|
|
|
|
if($name eq "")
|
|
|
|
{
|
|
|
|
$name = rand_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#printf "/* %s: %s|%s */\n", @$function{'name'}, $type, $name;
|
|
|
|
if(!defined $in_types{$type})
|
|
|
|
{
|
|
|
|
$valid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
push(@arguments, {'name' => $name,
|
|
|
|
'type' => $type,
|
|
|
|
'literal_type' => $literal_type
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$valid = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check for supported return value
|
|
|
|
my $return = @$function{'return'};
|
|
|
|
$return =~ s/(\s|const)//g;
|
|
|
|
#printf "/* %s: %s [%d] */\n", @$function{'name'}, $return, $valid;
|
|
|
|
if(!defined $out_types{$return})
|
|
|
|
{
|
|
|
|
$valid = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($valid == 1)
|
|
|
|
{
|
|
|
|
# Print the header
|
|
|
|
printf "static int rock_%s(lua_State *L)\n".
|
|
|
|
"{\n",
|
|
|
|
@$function{'name'};
|
|
|
|
|
|
|
|
# Print the arguments
|
|
|
|
my $i = 1;
|
|
|
|
foreach my $argument (@arguments)
|
|
|
|
{
|
|
|
|
print $in_types{@$argument{'type'}}->(@$argument{'name'}, @$argument{'literal_type'}, $i++);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Generate the arguments string
|
|
|
|
my $func_args = $arguments[0]{'name'};
|
|
|
|
for(my $i = 1; $i < $#arguments + 1; $i++)
|
|
|
|
{
|
|
|
|
$func_args .= ", ".$arguments[$i]{'name'};
|
|
|
|
}
|
|
|
|
|
|
|
|
# Print the function call
|
|
|
|
my $func = sprintf("rb->%s(%s)", @$function{'name'}, $func_args);
|
|
|
|
|
|
|
|
# Print the footer
|
|
|
|
print $out_types{$return}->($func, @$function{'return'});
|
|
|
|
print "}\n\n";
|
|
|
|
|
|
|
|
push(@valid_functions, $function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Print the C array
|
|
|
|
print "const luaL_Reg rocklib_aux[] =\n{\n";
|
|
|
|
foreach my $function (@valid_functions)
|
|
|
|
{
|
|
|
|
printf "\t{\"%s\", rock_%s},\n", @$function{'name'}, @$function{'name'};
|
|
|
|
}
|
|
|
|
print "\t{NULL, NULL}\n};\n\n";
|