| 1 | package SDL2::Utils { | ||
| 2 | |||
| 3 | # FFI utilities | ||
| 4 | use strictures 2; | ||
| 5 | use experimental 'signatures'; | ||
| 6 | use base 'Exporter::Tiny'; | ||
| 7 | our @EXPORT = qw[attach define deprecate has enum ffi is threads_wrapped]; | ||
| 8 | use Alien::libsdl2; | ||
| 9 | use FFI::CheckLib; | ||
| 10 | use FFI::Platypus 1.46; | ||
| 11 | use FFI::Platypus::Memory qw[malloc strcpy free]; | ||
| 12 | use FFI::C; | ||
| 13 | use FFI::C::Def; | ||
| 14 | use FFI::C::ArrayDef; | ||
| 15 | use FFI::C::StructDef; | ||
| 16 | use FFI::C::UnionDef; | ||
| 17 | use FFI::Platypus::Closure; | ||
| 18 | use File::Spec::Functions qw[catdir canonpath rel2abs]; | ||
| 19 | use Path::Tiny qw[path]; | ||
| 20 | use File::Share qw[dist_dir]; | ||
| 21 | use Config; | ||
| 22 | use SDL2::Utils::Type::Enum; | ||
| 23 | use Ref::Util qw( is_ref is_plain_arrayref is_plain_hashref ); | ||
| 24 | |||
| 25 | sub deprecate ($str) { | ||
| 26 | warnings::warn( 'deprecated', $str ) if warnings::enabled('deprecated'); | ||
| 27 | } | ||
| 28 | |||
| 29 | #sub ver() { # TODO: Remove in favor of SDL2::version | ||
| 30 | # CORE::state $ver; | ||
| 31 | # $ver // ffi()->function( SDL_GetVersion => ['SDL_Version'] ) | ||
| 32 | # ->call( $ver = SDL2::Version->new() ); | ||
| 33 | # $ver; | ||
| 34 | #} | ||
| 35 | #ddx( Alien::libsdl2->dynamic_libs ); | ||
| 36 | my $thread_safe = 0; | ||
| 37 | sub threads_wrapped () {$thread_safe} # Fake thread safe | ||
| 38 | |||
| 39 | sub ffi () { | ||
| 40 | CORE::state $ffi; | ||
| 41 | if ( !defined $ffi ) { | ||
| 42 | use FFI::Build; | ||
| 43 | use FFI::Build::File::C; | ||
| 44 | if ( defined(&Test2::V0::diag) ) { | ||
| 45 | my $Win32 = $^O eq 'MSWin32'; | ||
| 46 | # | ||
| 47 | #skip_all 'requires a shared object or DLL' | ||
| 48 | # unless Alien::libsdl2->dynamic_libs; | ||
| 49 | # | ||
| 50 | # nasty hack | ||
| 51 | #$ENV{LD_LIBRARY_PATH} = Alien::libsdl2->dist_dir . '/lib'; | ||
| 52 | #$ENV{DYLD_LIBRARY_PATH} = Alien::libsdl2->dist_dir . '/lib'; | ||
| 53 | # | ||
| 54 | eval { Test2::V0::diag( 'dist_dir: ' . Alien::libsdl2->dist_dir . '/lib' ) }; | ||
| 55 | warn $@ if $@; | ||
| 56 | eval { Test2::V0::diag( 'libs: ' . Alien::libsdl2->libs ) }; | ||
| 57 | warn $@ if $@; | ||
| 58 | eval { Test2::V0::diag( 'cflags: ' . Alien::libsdl2->cflags ) }; | ||
| 59 | warn $@ if $@; | ||
| 60 | eval { Test2::V0::diag( 'cflags static: ' . Alien::libsdl2->cflags_static ) }; | ||
| 61 | warn $@ if $@; | ||
| 62 | eval { | ||
| 63 | Test2::V0::diag( 'Dynamic libs: ' . join ':', Alien::libsdl2->dynamic_libs ); | ||
| 64 | }; | ||
| 65 | warn $@ if $@; | ||
| 66 | eval { Test2::V0::diag( 'bin dir: ' . join( ' ', Alien::libsdl2->bin_dir ) ) }; | ||
| 67 | warn $@ if $@; | ||
| 68 | } | ||
| 69 | my $lib = undef; | ||
| 70 | if (1) { | ||
| 71 | my $root = path(__FILE__)->absolute->parent(3)->realpath; | ||
| 72 | my $dir; # eval { dist_dir('SDL2-FFI') }; | ||
| 73 | $dir //= $root->child('share')->realpath; | ||
| 74 | my $c = $root->child('ffi/bundle.c'); | ||
| 75 | if ( defined(&Test2::V0::diag) ) { | ||
| 76 | eval { Test2::V0::diag( 'c file: ' . $c . ' | ' . ( -f $c ? '1' : '0' ) ) }; | ||
| 77 | } | ||
| 78 | my $build = FFI::Build->new( | ||
| 79 | 'bundle', | ||
| 80 | dir => $dir, | ||
| 81 | alien => ['Alien::libsdl2'], | ||
| 82 | source => [$c], | ||
| 83 | libs => [ Alien::libsdl2->libs_static() // Alien::libsdl2->dynamic_libs() ], | ||
| 84 | verbose => 2 | ||
| 85 | ); | ||
| 86 | $lib | ||
| 87 | = ( ( !-f $build->file->path ) || | ||
| 88 | ( ( [ stat $build->file->path ]->[9] < [ stat $c ]->[9] ) ) ) ? | ||
| 89 | $build->build : | ||
| 90 | $build->file->path; | ||
| 91 | |||
| 92 | #$lib | ||
| 93 | # = -f $build->file->path && -f $root->child('ffi/sdl2.c') && | ||
| 94 | # [ stat $build->file->path ]->[9] | ||
| 95 | # >= [ stat( $root->child('ffi/sdl2.c') ) ]->[9] ? $build->file : $build->build; | ||
| 96 | # warn $lib; | ||
| 97 | $thread_safe = defined $lib ? $lib : (); | ||
| 98 | if ( defined(&Test2::V0::diag) ) { | ||
| 99 | eval { Test2::V0::diag( 'lib: ' . $lib . ' | ' . ( -f $lib ? '1' : '0' ) ); }; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | { | ||
| 103 | $ffi = FFI::Platypus->new( | ||
| 104 | api => 2, | ||
| 105 | experimental => 2, | ||
| 106 | lib => [ Alien::libsdl2->dynamic_libs, $lib, ] | ||
| 107 | ); | ||
| 108 | FFI::C->ffi($ffi); | ||
| 109 | } | ||
| 110 | |||
| 111 | #$thread_safe = $ffi->bundle(); | ||
| 112 | #$lib //= $ffi->bundle; | ||
| 113 | #if ( defined(&Test2::V0::diag) ) { | ||
| 114 | # eval { | ||
| 115 | # Test2::V0::diag( 'bundle: ' . $lib->path . ' | ' . ( -f $lib ? '1' : '0' ) ); | ||
| 116 | # }; | ||
| 117 | #} | ||
| 118 | } | ||
| 119 | $ffi; | ||
| 120 | } | ||
| 121 | ffi(); # auto-init | ||
| 122 | |||
| 123 | sub enumX { | ||
| 124 | (undef) = shift; | ||
| 125 | my $name = defined $_[0] && !is_ref $_[0] ? shift : undef; | ||
| 126 | my @values = defined $_[0] && is_plain_arrayref $_[0] ? @{ shift() } : (); | ||
| 127 | my %config = defined $_[0] && is_plain_hashref $_[0] ? %{ shift() } : (); | ||
| 128 | my ( $class, $filename ) = caller; | ||
| 129 | unless ( defined $name ) { | ||
| 130 | $name = lcfirst [ split /::/, $class ]->[-1]; | ||
| 131 | $name =~ s/([A-Z]+)/'_' . lc($1)/ge; | ||
| 132 | $name .= "_t"; | ||
| 133 | } | ||
| 134 | my $ffi = FFI::C::_ffi_get($filename), $config{package} ||= $class; | ||
| 135 | my @maps; | ||
| 136 | $config{maps} = \@maps; | ||
| 137 | my $rev = $config{rev} ||= 'str'; | ||
| 138 | |||
| 139 | #use Data::Dump; | ||
| 140 | #ddx [$name, $rev, \%config, \@values]; | ||
| 141 | ffi->load_custom_type( 'SDL2::Utils::Type::Enum', $name, \%config, @values ); | ||
| 142 | my ( $str_lookup, $int_lookup, $type ) = @maps; | ||
| 143 | |||
| 144 | #ddx [$name, $rev, $str_lookup, $int_lookup, $type]; | ||
| 145 | require FFI::C::Def; | ||
| 146 | ffi->def( | ||
| 147 | 'FFI::C::EnumDef', | ||
| 148 | $name, | ||
| 149 | FFI::C::EnumDef->new( | ||
| 150 | str_lookup => $str_lookup, | ||
| 151 | int_lookup => $int_lookup, | ||
| 152 | type => $type, | ||
| 153 | rev => $rev, | ||
| 154 | ) | ||
| 155 | ); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub enum (%args) { | ||
| 159 | my ($package) = caller(); | ||
| 160 | $package = 'SDL2::FFI'; | ||
| 161 | for my $tag ( keys %args ) { | ||
| 162 | |||
| 163 | #use Data::Dump; | ||
| 164 | #ddx $args{$tag} if $tag eq 'WindowShapeMode'; | ||
| 165 | #use Data::Dump; | ||
| 166 | #ddx $args{$tag}; | ||
| 167 | #ddx @{$args{$tag}}; | ||
| 168 | #my $enum = | ||
| 169 | #FFI::C->enum( $tag => $args{$tag}, { package => $package } ); | ||
| 170 | #ffi->load_custom_type('SDL2::Utils::Type::Enum', $tag => { rev=>'int', package => $package, maps => \@maps } => $args{$tag} ); | ||
| 171 | enumX( | ||
| 172 | ffi, | ||
| 173 | $tag => $args{$tag}, | ||
| 174 | { rev => 'dualvar', package => $package, type => 'int', casing => 'keep' } | ||
| 175 | ); | ||
| 176 | |||
| 177 | #ffi->load_custom_type( | ||
| 178 | # '::Enum', | ||
| 179 | # $tag, | ||
| 180 | # {ref => 'int', package => $package }, | ||
| 181 | # @{$args{$tag}} | ||
| 182 | # #{ rev => 'int', package => 'Foo', prefix => 'FOO_' }, | ||
| 183 | #); | ||
| 184 | my $_tag = $tag; # Simple rules: | ||
| 185 | $_tag =~ s[^SDL_][]; # No SDL_XXXXX | ||
| 186 | $_tag = lcfirst $_tag unless $_tag =~ m[^.[A-Z]]; # Save GLattr | ||
| 187 | |||
| 188 | #ddx $enum if $tag eq 'WindowShapeMode'; | ||
| 189 | # warn $_tag if $tag eq 'WindowShapeMode'; | ||
| 190 | push @{ $SDL2::FFI::EXPORT_TAGS{$_tag} }, | ||
| 191 | sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} }; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | sub enumD (%args) { | ||
| 196 | my ($package) = caller(); | ||
| 197 | $package = 'SDL2::FFI'; | ||
| 198 | for my $tag ( keys %args ) { | ||
| 199 | use Data::Dump; | ||
| 200 | |||
| 201 | #ddx $args{$tag}; # if $tag eq 'WindowShapeMode'; | ||
| 202 | #use Data::Dump; | ||
| 203 | #warn $tag; | ||
| 204 | #ddx $args{$tag}; | ||
| 205 | #ddx @{$args{$tag}}; | ||
| 206 | #my $enum = | ||
| 207 | #FFI::C->enum( $tag => $args{$tag}, { package => $package } ); | ||
| 208 | #ffi->load_custom_type('::Enum', $tag => { package => $package } => [$args{$tag}] ); | ||
| 209 | #ffi->load_custom_type('SDL2::Utils::Type::Enum' => $tag => { ref => 'int', package => $package, casing => 'keep' } | ||
| 210 | # , $args{$tag} | ||
| 211 | #); | ||
| 212 | warn $tag; | ||
| 213 | ffi->load_custom_type( | ||
| 214 | 'SDL2::Utils::Type::Enum', $tag, { rev => 'dualvar', package => $package }, | ||
| 215 | @{ $args{$tag} } | ||
| 216 | |||
| 217 | #{ rev => 'int', package => 'Foo', prefix => 'FOO_' }, | ||
| 218 | ); | ||
| 219 | my $_tag = $tag; # Simple rules: | ||
| 220 | $_tag =~ s[^SDL_][]; # No SDL_XXXXX | ||
| 221 | $_tag = lcfirst $_tag unless $_tag =~ m[^.[A-Z]]; # Save GLattr | ||
| 222 | |||
| 223 | #ddx $enum if $tag eq 'WindowShapeMode'; | ||
| 224 | # warn $_tag if $tag eq 'WindowShapeMode'; | ||
| 225 | push @{ $SDL2::FFI::EXPORT_TAGS{$_tag} }, | ||
| 226 | sort map { ref $_ ? ref $_ eq 'CODE' ? $_->() : $_->[0] : $_ } @{ $args{$tag} }; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | sub attach (%args) { | ||
| 231 | my ($package) = caller(); | ||
| 232 | $package = 'SDL2::FFI'; | ||
| 233 | for my $tag ( sort keys %args ) { | ||
| 234 | for my $func ( sort keys %{ $args{$tag} } ) { | ||
| 235 | |||
| 236 | #warn sprintf 'ffi->attach( %s => %s);', $func, | ||
| 237 | # Data::Dump::dump( @{ $args{$tag}{$func} } ) | ||
| 238 | # if ref $args{$tag}{$func}[1] && ref $args{$tag}{$func}[1] eq 'ARRAY'; | ||
| 239 | my $perl = $func; | ||
| 240 | $perl =~ s[^Bundle_][]; | ||
| 241 | ffi->attach( [ $func => $package . '::' . $perl ] => @{ $args{$tag}{$func} } ); | ||
| 242 | push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, $perl; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||
| 246 | my %is; | ||
| 247 | |||
| 248 | sub is ($is) { | ||
| 249 | my ($package) = caller; | ||
| 250 | $is{$package} = $is; | ||
| 251 | } | ||
| 252 | sub get_is ($package) { $is{$package} // '' } | ||
| 253 | |||
| 254 | sub has (%args) { # Should be hash-like | ||
| 255 | my ($package) = caller; | ||
| 256 | my $type = $package; | ||
| 257 | $type =~ s[^SDL2::][SDL_]; | ||
| 258 | $type =~ s[::][_]g; | ||
| 259 | |||
| 260 | #$class =~ s[^SDL_(.+)$]['SDL2::' . ucfirst $1]e; | ||
| 261 | #warn sprintf '%-20s => %-20s%s', $name, $class, ( | ||
| 262 | # -f sub ($package) { $package =~ m[::(.+)]; './lib/SDL2/' . $1 . '.pod' } | ||
| 263 | # ->($class) ? '' : ' (undocumented)' | ||
| 264 | #); | ||
| 265 | my @args = ( | ||
| 266 | ffi, | ||
| 267 | name => $type, # C type | ||
| 268 | class => $package, # package | ||
| 269 | nullable => 1, | ||
| 270 | trim_string => 1, | ||
| 271 | members => \@_ # Keep order rather than use %args | ||
| 272 | ); | ||
| 273 | get_is($package) eq 'Union' ? FFI::C::UnionDef->new(@args) : FFI::C::StructDef->new(@args); | ||
| 274 | } | ||
| 275 | |||
| 276 | sub define (%args) { | ||
| 277 | my ($package) = caller(); | ||
| 278 | $package = 'SDL2::FFI'; | ||
| 279 | for my $tag ( keys %args ) { | ||
| 280 | |||
| 281 | #print $_->[0] . ' ' for sort { $a->[0] cmp $b->[0] } @{ $Defines{$tag} }; | ||
| 282 | #no strict 'refs'; | ||
| 283 | ref $_->[1] eq 'CODE' ? | ||
| 284 | |||
| 285 | #constant->import( $package . '::' .$_->[0] => $_->[1]->() ) : # | ||
| 286 | sub { no strict 'refs'; *{ $package . '::' . $_->[0] } = $_->[1] } | ||
| 287 | ->() : | ||
| 288 | constant->import( $package . '::' . $_->[0] => $_->[1] ) | ||
| 289 | for @{ $args{$tag} }; | ||
| 290 | push @{ $SDL2::FFI::EXPORT_TAGS{$tag} }, | ||
| 291 | sort map { ref $_ ? $_->[0] : $_ } @{ $args{$tag} }; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | # | ||
| 295 | sub _tokenize_in ( $str, $pointers = 1 ) { # TODO: reconciliation between this and _out | ||
| 296 | my @ret; | ||
| 297 | $str =~ s{ | ||
| 298 | \% | ||
| 299 | \d*? | ||
| 300 | (?<type> (?:[\+-]?[di]|0|x|X|u|f|F|e|E|a|A|g|G|n|p|s|c|lc|\d\[.+?\])) | ||
| 301 | }{ | ||
| 302 | push @ret, _conversion($+{type}, $pointers )}gexsm; | ||
| 303 | @ret; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub _tokenize_out ( $str, $pointers = 1 ) { | ||
| 307 | my @ret; | ||
| 308 | $str =~ s[ | ||
| 309 | %[\^\-\+\d\.#]*?(?<len>(?:\*.)?\*)?(?<type>[csdioxXufFeEaAgGnp]) | ||
| 310 | ][ | ||
| 311 | push @ret, _conversion($+{type}, $pointers, $+{len} // ())]gexsm; | ||
| 312 | @ret; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub _conversion ( $conv, $pointers = 1, $len = '' ) { | ||
| 316 | my $count = () = $len =~ m[(\*)]msg; | ||
| 317 | my $retval | ||
| 318 | = $conv eq 'c' ? 'char' : $conv eq 's' ? 'string' : $conv eq '[' ? 'string' : # set | ||
| 319 | $conv eq 'd' ? ( $pointers ? 'int*' : 'int' ) : | ||
| 320 | $conv eq 'i' ? ( $pointers ? 'int*' : 'int' ) : | ||
| 321 | $conv eq 'u' ? ( $pointers ? 'uint*' : 'uint' ) : | ||
| 322 | $conv eq 'o' ? ( $pointers ? 'int*' : 'int' ) : # octal | ||
| 323 | $conv eq 'x' ? ( $pointers ? 'string' : 'int' ) : # hex | ||
| 324 | $conv eq 'X' ? ( $pointers ? 'string' : 'int' ) : # hex | ||
| 325 | $conv eq 'n' ? ( $pointers ? 'int*' : 'int' ) : # num of chars read so far | ||
| 326 | $conv eq 'a' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 327 | $conv eq 'A' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 328 | $conv eq 'e' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 329 | $conv eq 'E' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 330 | $conv eq 'f' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 331 | $conv eq 'F' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 332 | $conv eq 'g' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 333 | $conv eq 'G' ? ( $pointers ? 'float*' : 'float' ) : | ||
| 334 | $conv eq 'p' ? 'opaque' : | ||
| 335 | $conv eq 'lc' ? 'wide_string' : | ||
| 336 | $conv =~ /^\d/ ? 'string' : | ||
| 337 | 'broken: ' . $conv; | ||
| 338 | $len ? ( ( map {'int'} 1 .. $count ), $retval ) : $retval; | ||
| 339 | } | ||
| 340 | }; | ||
| 341 | 1; |