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.6 by root, Sun Mar 19 23:24:20 2023 UTC vs.
Revision 1.8 by root, Mon Mar 20 11:12:40 2023 UTC

386 my $trace = delete $self->{trace} || sub { }; 386 my $trace = delete $self->{trace} || sub { };
387 387
388 $trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace; 388 $trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace;
389 389
390 my $buf; 390 my $buf;
391 my $wbuf;
392 391
393 Scalar::Util::weaken $self; 392 Scalar::Util::weaken $self;
394 393
395 $self->{rw} = AE::io $fh, 0, sub { 394 $self->{rw} = AE::io $fh, 0, sub {
396 if (sysread $fh, $buf, 8192, length $buf) { 395 if (sysread $fh, $buf, 8192, length $buf) {
409 if ($reply->{args}[1] eq "key") { 408 if ($reply->{args}[1] eq "key") {
410 (my $key = $reply->{args}[2]) =~ s/\\x(..)/chr hex $1/ge; 409 (my $key = $reply->{args}[2]) =~ s/\\x(..)/chr hex $1/ge;
411 $self->on_key ($key); 410 $self->on_key ($key);
412 } 411 }
413 } else { 412 } else {
414 $self->on_event ($reply->{event}, $reply); 413 $self->on_event (delete $reply->{event}, $reply);
415 } 414 }
416 } elsif (exists $reply->{request_id}) { 415 } elsif (exists $reply->{request_id}) {
417 my $cv = delete $self->{cmd_cv}{$reply->{request_id}}; 416 my $cv = delete $self->{cmd_cv}{$reply->{request_id}};
418 417
419 unless ($cv) { 418 unless ($cv) {
442 $self->stop; 441 $self->stop;
443 $self->on_eof; 442 $self->on_eof;
444 } 443 }
445 }; 444 };
446 445
446 my $wbuf;
447 my $reqid;
448
447 $self->{_send} = sub { 449 $self->{_cmd} = sub {
450 my $cv = AE::cv;
451
452 $self->{cmd_cv}{++$reqid} = $cv;
453
454 my $cmd = $JSON->new->utf8->encode ({ command => ref $_[0] ? $_[0] : \@_, request_id => $reqid*1 });
455
456 # (un-)apply escape_binary hack
457 $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8
458
448 $wbuf .= "$_[0]\n"; 459 $wbuf .= "$cmd\n";
449 460
450 $trace->(">mpv" => "$_[0]"); 461 $trace->(">mpv" => "$_[0]");
451 462
452 $self->{ww} ||= AE::io $fh, 1, sub { 463 $self->{ww} ||= AE::io $fh, 1, sub {
453 my $len = syswrite $fh, $wbuf; 464 my $len = syswrite $fh, $wbuf;
454 substr $wbuf, 0, $len, ""; 465 substr $wbuf, 0, $len, "";
455 undef $self->{ww} unless length $wbuf; 466 undef $self->{ww} unless length $wbuf;
456 }; 467 };
468
469 $cv
457 }; 470 };
458 471
459 1 472 1
473}
474
475sub DESTROY {
476 $_[0]->stop;
460} 477}
461 478
462=item $mpv->stop 479=item $mpv->stop
463 480
464Ensures that F<mpv> is being stopped, by killing F<mpv> with a C<TERM> 481Ensures that F<mpv> is being stopped, by killing F<mpv> with a C<TERM>
480 497
481 } 498 }
482 499
483 delete $self->{pid}; 500 delete $self->{pid};
484 delete $self->{cmd_cv}; 501 delete $self->{cmd_cv};
502 delete $self->{obsid};
503 delete $self->{wbuf};
485} 504}
486 505
487=item $mpv->on_eof 506=item $mpv->on_eof
488 507
489This method is called when F<mpv> quits - usually unexpectedly. The 508This method is called when F<mpv> quits - usually unexpectedly. The
504 523
505This method is called when F<mpv> sends an asynchronous event. The default 524This method is called when F<mpv> sends an asynchronous event. The default
506implementation will call the C<on_event> code reference specified in the 525implementation will call the C<on_event> code reference specified in the
507constructor, or do nothing if none was given. 526constructor, or do nothing if none was given.
508 527
509The first/implicit argument is the C<$mpv> object, the second is the event 528The first/implicit argument is the C<$mpv> object, the second is the
510name (same as C<< $data->{event} >>, purely for convenience), and the 529event name (same as C<< $data->{event} >>, purely for convenience), and
511third argument is the full event object as sent by F<mpv>. See L<List of 530the third argument is the event object as sent by F<mpv> (sans C<event>
512events|https://mpv.io/manual/stable/#list-of-events> in its documentation. 531key). See L<List of events|https://mpv.io/manual/stable/#list-of-events>
532in its documentation.
513 533
514For subclassing, see I<SUBCLASSING>, below. 534For subclassing, see I<SUBCLASSING>, below.
515 535
516=cut 536=cut
517 537
576On error, the condvar will croak when C<recv> is called. 596On error, the condvar will croak when C<recv> is called.
577 597
578=cut 598=cut
579 599
580sub cmd { 600sub cmd {
581 my ($self, @cmd) = @_; 601 my $self = shift;
582 602
583 my $cv = AE::cv; 603 $self->{_cmd}->(@_)
584
585 my $reqid = ++$self->{reqid};
586 $self->{cmd_cv}{$reqid} = $cv;
587
588 my $cmd = $JSON->new->utf8->encode ({ command => ref $cmd[0] ? $cmd[0] : \@cmd, request_id => $reqid*1 });
589
590 # (un-)apply escape_binary hack
591 $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8
592
593 $self->{_send}($cmd);
594
595 $cv
596} 604}
597 605
598=item $result = $mpv->cmd_recv ($command => $arg, $arg...) 606=item $result = $mpv->cmd_recv ($command => $arg, $arg...)
599 607
600The same as calling C<cmd> and immediately C<recv> on its return 608The same as calling C<cmd> and immediately C<recv> on its return

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines