| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::API; | ||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use AnyEvent; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use AnyEvent::Handle; | ||
| 11 | use AnyEvent::Socket; | ||
| 12 | use X11::korgwm::Common; | ||
| 13 | require X11::korgwm::Config; | ||
| 14 | require X11::korgwm::Executor; | ||
| 15 | |||
| 16 | my %clients; | ||
| 17 | my $server; | ||
| 18 | |||
| 19 | # Establish API server | ||
| 20 | sub init { | ||
| 21 | return unless defined $cfg->{api_host} and ($cfg->{api_port} // 0) > 0; | ||
| 22 | $server = tcp_server $cfg->{api_host}, $cfg->{api_port}, sub($fh, @) { | ||
| 23 | my $hdl; | ||
| 24 | my $close = sub { $hdl->destroy(); undef $fh; delete $clients{$hdl}; }; | ||
| 25 | $hdl = AnyEvent::Handle->new( | ||
| 26 | fh => $fh, | ||
| 27 | timeout => $cfg->{api_timeout}, | ||
| 28 | on_timeout => $close, | ||
| 29 | on_error => $close, | ||
| 30 | on_eof => $close, | ||
| 31 | on_read => sub { | ||
| 32 | $hdl->push_read(line => sub { | ||
| 33 | return $close->() if $_[1] =~ /^(re?set|quit)$/i; | ||
| 34 | my $cb; | ||
| 35 | eval { | ||
| 36 | $cb = X11::korgwm::Executor::parse($_[1]); | ||
| 37 | 1; | ||
| 38 | } or return $close->(); | ||
| 39 | DEBUG7 and warn "API: executing $_[1]"; | ||
| 40 | ref $cb eq "CODE" and $cb->($hdl); | ||
| 41 | }) | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | $clients{$hdl} = $hdl; | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | push @X11::korgwm::extensions, \&init; | ||
| 49 | |||
| 50 | 1; | ||