problem with Gtk2::Helper & STDOUT


i'm asking here after turning around a strange problem for days...
i can't get to make Gtk2::Helper watch the STDOUT of the current program
(not from a forked child or whatever). The idea is to do smth like 'tail' but
in a Gtk Widget or VTE Widget, but showing current process STDOUT.

I think i tried everything so far, all supposed (from my understanding
of the docs) to catch the 'FOO'
printed on STDOUT :

Glib::IO->add_watch (fileno (STDOUT), 'in', sub {
        my $fd = shift;
        print STDERR "received \"$_\"\n" while (<$fd>);
        return TRUE;
print "FOO\n";

same thing with an anonymous file handler :

open my $fh, "+>", undef or die "blaaah : $!";
select $fh;
my $id = Glib::IO->add_watch (fileno $fh, 'in', sub {
        my $fd = shift;
        print STDERR "received \"$_\"\n" while (<$fd>);
        return TRUE;
print "FOO\n";

With those examples, 'FOO' is dropped.

Is there a way to realize this ? I'd really appreciate some help,
because this is turning me mad since days. Apparently, bare glib allows
io channels to be created with stdout.. maybe the problem lies in the
fact that i want to _read_ on  STDOUT. I also tried :

pipe ($read,$fh);
select $fh;
Gtk2::Helper->add_watch (fileno($read), 'in', sub {cbk($fh)});

With no luck.

Thanks for any pointers/insights.


Ps: here's the whole test program :

#!/usr/bin/perl -w

use strict;
use Glib 1.040, qw(TRUE FALSE);
use Gtk2;
use Data::Dumper;
use Gtk2::Helper;


my $window = Gtk2::Window->new;
$window->signal_connect (delete_event => sub {exit});
$window->set_default_size (400, 300);
my $vbox = Gtk2::VBox->new;
$window->add ($vbox);
my $scroller = Gtk2::ScrolledWindow->new;
$vbox->add ($scroller);
my $textview = Gtk2::TextView->new;
my $buffer = $textview->get_buffer;
$buffer->create_mark ('end', $buffer->get_end_iter, FALSE);
$buffer->signal_connect (insert_text => sub { $textview->scroll_to_mark ($buffer->get_mark ('end'), 0.0, 
TRUE, 0, 0.5);});
$scroller->add ($textview);
my $button = Gtk2::Button->new ('go');
$button->signal_connect (clicked => \&button_handler);
$vbox->pack_start ($button, FALSE, FALSE, 0);

sub button_handler {
        $button->set_sensitive (FALSE);
        my $fh;
        open ($fh, "+>", undef) or die "PROTCH:$!";
        select $fh;
        Gtk2::Helper->add_watch (fileno($fh), 'in', sub {cbk($fh)});
        print STDERR "FOO1\n";
        print "FOO2\n";
        sleep 1;
        print $fh "FOO3\n";
        $buffer->insert ($buffer->get_end_iter, "done");
        $button->set_sensitive (TRUE);
sub cbk {
        my $fd = shift;
        my $buf;
        print STDERR Dumper($fd).fileno($fd)."!\n";
        while (sysread ($fd,$buf,1028)) {
                print STDERR "received \"$buf\"\n";
                $buffer->insert ($buffer->get_end_iter, $buf);

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]