| 1 | # $Id: Pipe.pm 2067 2006-05-15 20:27:34Z joern $ | ||
| 2 | |||
| 3 | package Video::DVDRip::GUI::Pipe; | ||
| 4 | |||
| 5 | use Locale::TextDomain qw (video.dvdrip); | ||
| 6 | |||
| 7 | use base Video::DVDRip::GUI::Base; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use Cwd; | ||
| 13 | use FileHandle; | ||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | use Gtk2::Helper; | ||
| 17 | use POSIX qw(:errno_h); | ||
| 18 | |||
| 19 | sub fh { shift->{fh} } | ||
| 20 | sub command { shift->{command} } | ||
| 21 | sub args { shift->{args} } | ||
| 22 | sub need_output { shift->{need_output} } | ||
| 23 | sub output { shift->{output} } | ||
| 24 | sub cb_line_read { shift->{cb_line_read} } | ||
| 25 | sub cb_finished { shift->{cb_finished} } | ||
| 26 | sub pid { shift->{pid} } | ||
| 27 | sub watcher_tag { shift->{watcher_tag} } | ||
| 28 | |||
| 29 | sub set_fh { shift->{fh} = $_[1] } | ||
| 30 | sub set_command { shift->{command} = $_[1] } | ||
| 31 | sub set_args { shift->{args} = $_[1] } | ||
| 32 | sub set_need_output { shift->{need_output} = $_[1] } | ||
| 33 | sub set_output { shift->{output} = $_[1] } | ||
| 34 | sub set_cb_line_read { shift->{cb_line_read} = $_[1] } | ||
| 35 | sub set_cb_finished { shift->{cb_finished} = $_[1] } | ||
| 36 | sub set_pid { shift->{pid} = $_[1] } | ||
| 37 | sub set_watcher_tag { shift->{watcher_tag} = $_[1] } | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my $class = shift; | ||
| 41 | my %par = @_; | ||
| 42 | my ($command, $need_output, $cb_line_read, $cb_finished) = | ||
| 43 | @par{'command','need_output','cb_line_read','cb_finished'}; | ||
| 44 | my ($args) = | ||
| 45 | @par{'args'}; | ||
| 46 | |||
| 47 | my $self = { | ||
| 48 | command => $command, | ||
| 49 | args => ($args || []), | ||
| 50 | need_output => $need_output, | ||
| 51 | cb_line_read => $cb_line_read, | ||
| 52 | cb_finished => $cb_finished, | ||
| 53 | }; | ||
| 54 | |||
| 55 | return bless $self, $class; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub open { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | my $fh = FileHandle->new; | ||
| 62 | |||
| 63 | # we use fork & exec, because we want to have | ||
| 64 | # STDERR on STDOUT in the child. | ||
| 65 | my $pid = open($fh, "-|"); | ||
| 66 | croak "can't fork child process" if not defined $pid; | ||
| 67 | |||
| 68 | if ( not $pid ) { | ||
| 69 | # we are the child. Copy STDERR to STDOUT | ||
| 70 | close STDERR; | ||
| 71 | open (STDERR, ">&STDOUT") | ||
| 72 | or croak "can't dup STDOUT to STDERR"; | ||
| 73 | my $command = $self->command; | ||
| 74 | my $dvdrip_exec = $command =~ /dvdrip-exec/ ? "" : "dvdrip-exec "; | ||
| 75 | exec ($dvdrip_exec.$command, @{$self->args}) | ||
| 76 | or croak "can't exec program: $!"; | ||
| 77 | } | ||
| 78 | |||
| 79 | $self->log ("Executing command: ".$self->command); | ||
| 80 | |||
| 81 | $self->set_fh ( $fh ); | ||
| 82 | $self->set_pid ( $pid ); | ||
| 83 | $self->set_output ( "" ); | ||
| 84 | |||
| 85 | $self->set_watcher_tag ( | ||
| 86 | Gtk2::Helper->add_watch ( | ||
| 87 | $fh->fileno, | ||
| 88 | 'in', sub { $self->progress; 1; } | ||
| 89 | ), | ||
| 90 | ); | ||
| 91 | |||
| 92 | 1; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub close { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | Gtk2::Helper->remove_watch ( $self->watcher_tag ) | ||
| 99 | if $self->watcher_tag; | ||
| 100 | |||
| 101 | close ($self->fh) | ||
| 102 | if $self->fh; | ||
| 103 | |||
| 104 | $self->set_watcher_tag(undef); | ||
| 105 | $self->set_fh (undef); | ||
| 106 | |||
| 107 | 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub cancel { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | my $pid = $self->pid; | ||
| 114 | |||
| 115 | if ( $pid ) { | ||
| 116 | $self->log ("Aborting command. Sending signal 9 to PID $pid..."); | ||
| 117 | kill 9, $pid; | ||
| 118 | } | ||
| 119 | |||
| 120 | $self->close; | ||
| 121 | |||
| 122 | 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub progress { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | my $fh = $self->fh; | ||
| 129 | |||
| 130 | # read a chunk from the filehandle | ||
| 131 | # (no Perl I/O here, instead low level sysread, since | ||
| 132 | # Gtk watches the low level filehandle, not the | ||
| 133 | # buffered Perl handle, otherwise evil deadlocking | ||
| 134 | # is promised) | ||
| 135 | my $buffer; | ||
| 136 | if ( !sysread($fh, $buffer, 4096) ) { | ||
| 137 | my $cb_finished = $self->cb_finished; | ||
| 138 | &$cb_finished() if $cb_finished; | ||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | # store output | ||
| 143 | if ( $self->need_output ) { | ||
| 144 | $self->{output} .= $buffer; | ||
| 145 | } else { | ||
| 146 | $self->{output} = substr($self->{output}.$buffer,-16384); | ||
| 147 | } | ||
| 148 | |||
| 149 | # get job's PID | ||
| 150 | my ($pid) = ( $buffer =~ /DVDRIP_JOB_PID=(\d+)/ ); | ||
| 151 | if ( defined $pid ) { | ||
| 152 | $self->set_pid ( $pid ); | ||
| 153 | $self->log ("Job has PID $pid"); | ||
| 154 | $buffer =~ s/DVDRIP_JOB_PID=(\d+)\n//; | ||
| 155 | } | ||
| 156 | |||
| 157 | # prepend rest data from previous run | ||
| 158 | my $buffer = $self->{buffer}.$buffer; | ||
| 159 | |||
| 160 | # line callback | ||
| 161 | my $cb_line_read = $self->cb_line_read; | ||
| 162 | |||
| 163 | # process by line | ||
| 164 | my $has_line_breaks; | ||
| 165 | while ( $buffer =~ s/(.*)\n// ) { | ||
| 166 | $has_line_breaks = 1; | ||
| 167 | &$cb_line_read ( $1 ) if $cb_line_read; | ||
| 168 | } | ||
| 169 | |||
| 170 | # process buffer as-is if there is no line break | ||
| 171 | # in command's output | ||
| 172 | if ( !$has_line_breaks ) { | ||
| 173 | &$cb_line_read ( $buffer ) if $cb_line_read; | ||
| 174 | $buffer = ""; | ||
| 175 | } | ||
| 176 | |||
| 177 | # save rest of buffer | ||
| 178 | $self->{buffer} = $buffer; | ||
| 179 | |||
| 180 | 1; | ||
| 181 | } | ||
| 182 | |||
| 183 | 1; |