Thread GUI-Terminal mittels Button füttern (11 answers)
Opened by mika at 2012-04-30 15:48

topeg
 2012-05-02 20:48
#157987 #157987
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Ich hatte mal einen kleinen Test mit Gtk2::Socket geschrieben. das macht ungefähr das was du willst. Es gibt da noch Probleme was den Tastaturfokus betrifft. Man muss mit [TAB] wieder in das richtige Widget wechseln. Das sollte sich aber mit einem Mouse-Handler lösen lassen.

more (22.5kb):
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Gtk2 '-init';

my $cmd='echo "Das ist nur ein Test!"';

my $pid=undef;

my $window = Gtk2::Window->new( 'toplevel' );
$window->signal_connect (delete_event => sub{ exit(); });

my $box=Gtk2::VBox->new();
$window->add($box);


my $frame = Gtk2::Frame->new( "Terminal" );
$frame->set_size_request (700, 400);
$box->pack_start($frame, 1, 1, 5);

my $term = shell_exec($cmd,$frame);

my $cmd_txt=Gtk2::Entry->new();
$box->pack_start($cmd_txt, 0, 1, 5);

my $btn=Gtk2::Button->new_with_label('AUSFÜHREN');
$btn->signal_connect('clicked' => sub{ shell_exec($cmd_txt->get_text(),\$term) } );
$box->pack_start($btn, 0, 1, 5);

$window->show_all();

$SIG{CHLD}=sub{ exit() if(wait() == $pid); };
Gtk2->main();

# using '$pid';
sub shell_exec
{
  my $cmd=shift;
  my $widget=shift;
  return unless($cmd);

  my $sock;
  my $parent;

  if(ref($widget) eq 'REF')
  {
    $sock=$$widget;
    $parent=$sock->parent();
  }
  elsif($widget->isa('Gtk2::Socket'))
  {
    $sock=$widget;
    $parent=$sock->parent();
  }
  elsif($widget->isa('Gtk2::Container'))
  { $parent=$widget; }

  if($pid)
  {
    __stop($pid);
    if($sock)
    {
      $parent->remove($sock) if($parent);
      $sock->destroy();
    }
  }

  $sock = Gtk2::Socket->new();
  if($parent)
  {
    $parent->add($sock);
    $parent->show_all();
    $pid=__start($sock->get_id,$cmd);
  }

  $$widget=$sock if(ref($widget) eq 'REF');
  return $sock;
}

sub __stop
{
  my $pid=shift;
  return 1 unless($pid);
  local $SIG{CHLD}=sub{$pid=undef if(wait() == $pid)};
  for my $v (15,15,15,9)
  {
    return 1 unless($pid && kill(0,$pid));
    kill($v,$pid);
    sleep 1;
  }
  return 0;
}

sub __start
{
  my $xid=shift;
  my $cmd=shift;
  my $pid=fork();
  die() unless defined($pid);
  unless($pid)
  {
   exec('/usr/bin/rxvt', '-embed', $xid, '-e', 'sh', '-c', "$cmd; sh");
   exit();
  }
  return $pid;
}

View full thread GUI-Terminal mittels Button füttern