ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MPV/MPV.pm
(Generate patch)

Comparing AnyEvent-MPV/MPV.pm (file contents):
Revision 1.11 by root, Mon Mar 20 13:32:52 2023 UTC vs.
Revision 1.14 by root, Mon Mar 20 22:10:37 2023 UTC

218sub OBSID() { 0x10000000000000 } # 2**52 218sub OBSID() { 0x10000000000000 } # 2**52
219 219
220our $JSON = eval { require JSON::XS; JSON::XS:: } 220our $JSON = eval { require JSON::XS; JSON::XS:: }
221 || do { require JSON::PP; JSON::PP:: }; 221 || do { require JSON::PP; JSON::PP:: };
222 222
223our $JSON_CODER = 223our $JSON_ENCODER = $JSON->new->utf8;
224our $JSON_DECODER = $JSON->new->latin1;
224 225
225our $mpv_path; # last mpv path used 226our $mpv_path; # last mpv path used
226our $mpv_optionlist; # output of mpv --list-options 227our $mpv_optionlist; # output of mpv --list-options
227 228
228=item $mpv = AnyEvent::MPV->new (key => value...) 229=item $mpv = AnyEvent::MPV->new (key => value...)
400 while ($buf =~ s/^([^\n]+)\n//) { 401 while ($buf =~ s/^([^\n]+)\n//) {
401 $trace->("mpv>" => "$1"); 402 $trace->("mpv>" => "$1");
402 403
403 if ("{" eq substr $1, 0, 1) { 404 if ("{" eq substr $1, 0, 1) {
404 eval { 405 eval {
405 my $reply = $JSON->new->latin1->decode ($1); 406 my $reply = $JSON_DECODER->decode ($1);
406 407
407 if (defined (my $event = delete $reply->{event})) { 408 if (defined (my $event = delete $reply->{event})) {
408 if ( 409 if (
409 $event eq "client-message" 410 $event eq "client-message"
410 and $reply->{args}[0] eq "AnyEvent::MPV" 411 and $reply->{args}[0] eq "AnyEvent::MPV"
468 $self->{_cmd} = sub { 469 $self->{_cmd} = sub {
469 my $cv = AE::cv; 470 my $cv = AE::cv;
470 471
471 $self->{cmdcv}{++$reqid} = $cv; 472 $self->{cmdcv}{++$reqid} = $cv;
472 473
473 my $cmd = $JSON->new->utf8->encode ({ command => ref $_[0] ? $_[0] : \@_, request_id => $reqid*1 }); 474 my $cmd = $JSON_ENCODER->encode ({ command => ref $_[0] ? $_[0] : \@_, request_id => $reqid*1 });
474 475
475 # (un-)apply escape_binary hack 476 # (un-)apply escape_binary hack
476 $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8 477 $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8
477 478
478 $trace->(">mpv" => $cmd); 479 $trace->(">mpv" => $cmd);
694You can register multiple handlers for the same event, and this method 695You can register multiple handlers for the same event, and this method
695does not interfere with the C<on_event> mechanism. That is, you can 696does not interfere with the C<on_event> mechanism. That is, you can
696completely ignore this method and handle events in a C<on_event> handler, 697completely ignore this method and handle events in a C<on_event> handler,
697or mix both approaches as you see fit. 698or mix both approaches as you see fit.
698 699
700Note that unlike commands, event handlers are registered immediately, that
701is, you can issue a command, then register an event handler and then get
702an event for this handler I<before> the command is even sent to F<mpv>. If
703this kind of race is an issue, you can issue a dummy command such as
704C<get_version> and register the handler when the reply is received.
705
699=cut 706=cut
700 707
701sub AnyEvent::MPV::Unevent::DESTROY { 708sub AnyEvent::MPV::Unevent::DESTROY {
702 my ($evtcb, $evtid) = @{$_[0]}; 709 my ($evtcb, $event, $evtid) = @{$_[0]};
703 delete $evtcb->{$evtid}; 710 delete $evtcb->{$event}{$evtid};
704} 711}
705 712
706sub register_event { 713sub register_event {
707 my ($self, $event, $cb) = @_; 714 my ($self, $event, $cb) = @_;
708 715
710 717
711 my $evtid = ++$self->{evtid}; 718 my $evtid = ++$self->{evtid};
712 $self->{evtcb}{$event}{$evtid} = $cb; 719 $self->{evtcb}{$event}{$evtid} = $cb;
713 720
714 defined wantarray 721 defined wantarray
715 and bless [$self->{evtcb}, $evtid], AnyEvent::MPV::Unevent:: 722 and bless [$self->{evtcb}, $event, $evtid], AnyEvent::MPV::Unevent::
716} 723}
717 724
718=item [$guard] = $mpv->observe_property ($name => $coderef->($mpv, $name, $value)) 725=item [$guard] = $mpv->observe_property ($name => $coderef->($mpv, $name, $value))
719 726
720=item [$guard] = $mpv->observe_property_string ($name => $coderef->($mpv, $name, $value)) 727=item [$guard] = $mpv->observe_property_string ($name => $coderef->($mpv, $name, $value))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines