… | |
… | |
218 | sub OBSID() { 0x10000000000000 } # 2**52 |
218 | sub OBSID() { 0x10000000000000 } # 2**52 |
219 | |
219 | |
220 | our $JSON = eval { require JSON::XS; JSON::XS:: } |
220 | our $JSON = eval { require JSON::XS; JSON::XS:: } |
221 | || do { require JSON::PP; JSON::PP:: }; |
221 | || do { require JSON::PP; JSON::PP:: }; |
222 | |
222 | |
223 | our $JSON_CODER = |
223 | our $JSON_ENCODER = $JSON->new->utf8; |
|
|
224 | our $JSON_DECODER = $JSON->new->latin1; |
224 | |
225 | |
225 | our $mpv_path; # last mpv path used |
226 | our $mpv_path; # last mpv path used |
226 | our $mpv_optionlist; # output of mpv --list-options |
227 | our $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); |
… | |
… | |
694 | You can register multiple handlers for the same event, and this method |
695 | You can register multiple handlers for the same event, and this method |
695 | does not interfere with the C<on_event> mechanism. That is, you can |
696 | does not interfere with the C<on_event> mechanism. That is, you can |
696 | completely ignore this method and handle events in a C<on_event> handler, |
697 | completely ignore this method and handle events in a C<on_event> handler, |
697 | or mix both approaches as you see fit. |
698 | or mix both approaches as you see fit. |
698 | |
699 | |
|
|
700 | Note that unlike commands, event handlers are registered immediately, that |
|
|
701 | is, you can issue a command, then register an event handler and then get |
|
|
702 | an event for this handler I<before> the command is even sent to F<mpv>. If |
|
|
703 | this kind of race is an issue, you can issue a dummy command such as |
|
|
704 | C<get_version> and register the handler when the reply is received. |
|
|
705 | |
699 | =cut |
706 | =cut |
700 | |
707 | |
701 | sub AnyEvent::MPV::Unevent::DESTROY { |
708 | sub 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 | |
706 | sub register_event { |
713 | sub 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)) |