Re: Gtk2::Gdk::Window->foreign_new, help needed

Oh, thanks alot. Staring at the same lines of code for a while makes you
blind ;-) Thank you Torsten.

I've got the old code running now and i am wondering if it is possible
to select a foreign window and get the mouse movement in this window via
event. At the moment i am using this dirty code to select a window and
print out the mouse movement.

It works pretty well but is not really a solution because it'll never
reach the gtk main loop...

Does anyone know a better solution to get the mouse movement just via a
normal event? As you see i was trying to use a drawing area and get the
events but this is not working.

Thank you all.

#! /usr/bin/perl
use strict;
use Gtk2;


my @anids=();

my @xwininfo_resp = `xwininfo -tree`;
        if ($_ =~ /.*(0x[a-f0-9]+) .*/){
                my $id = $1;
                #~ print "Checking Window: $_\n";
                #~ print "Detected-Id: $id Window\n"; 
                push(@anids, Gtk2::Gdk::Window->foreign_new(hex($id)));
                #~ print "--------------------------------------\n";    

my $counter = 0;
my @drawing_areas; 

my $area = new Gtk2::DrawingArea;
$area->set_events ([qw/exposure-mask

$area->signal_connect (motion_notify_event => \&motion_notify_event);
$area->signal_connect (key_press_event => \&key_press_event);

foreach my $root (@anids){
        if($root && $root->is_viewable){
                my ($w, $h) = $root->get_size;                  
                next unless ($w > 10 && $h > 10);
                my $state = $root->get_state;
                next if $state eq 'GDK_WINDOW_STATE_WITHDRAWN';

                                my ($window, $win_x, $win_y) = $root->at_pointer;
                                my ($w, $h) = $window->get_size;                        
                                print $window." ".$win_x." ".$win_y."\n";
                                my ($x_start, $y_start) = $window->get_position;
                                print $area." at size ".$w." and ".$h."\n";
                                #~ &draw_rect($window, [$x_start-5,$y_start-5,$w-5,$h-5],'red');              
                                sleep 1;

                #~ my $pixbuf = Gtk2::Gdk::Pixbuf->get_from_drawable ($root,
Gtk2::Gdk::Colormap->get_system, 0, 0, 0, 0, $w, $h);
                #~ $pixbuf->get_from_drawable ($root, undef, 0, 0, 0, 0, $w, $h) if
                #~ $pixbuf->save ("screenshot_$counter.png", 'png') if
                #~ print "$root - State = $state - Number = $counter\n";

my %allocated_colors;
sub button_press_event {
  my $widget = shift;    # GtkWidget      *widget
  my $event = shift;    # GdkEventButton *event

  print join ' ', $event->coords,"\n";
  return 1;

sub motion_notify_event {
my $widget=shift;
my $event=shift;

 my ($x, $y, $state);

  if ($event->is_hint) {
    (undef, $x, $y, $state) = $event->window->get_pointer;
  } else {
    $x = $event->x;
    $y = $event->y;
    $state = $event->state;

print "value of x and y are $x and $y \n" ;

  if ($state >= "button1-mask" ) {
                print "value of x and y and button1 are $x and $y \n" ;
  if ($state >= "button2-mask" ) {
                print "value of x and y and button2 are $x and $y \n" ;

  return 1; #was TRUE in source (was

sub draw_rect {
    my($widget,$coords,$color) = @_;
   # see Gdk::Gdk::Window, Gtk2::Gdk::Drawable, Gtk2::Gdk::GC

    my $colormap = Gtk2::Gdk::Colormap->get_system;

    my $gc = new Gtk2::Gdk::GC($widget);
    $gc->set_foreground(get_color($colormap, $color));

    $widget->draw_rectangle($gc,0, @$coords);
sub get_color {
    my ($colormap, $name) = @_;
    my $ret;

    if ($ret = $allocated_colors{$name}) {
        return $ret;

    my $color = Gtk2::Gdk::Color->parse($name);

    $allocated_colors{$name} = $color;

    return $color;


Am Mittwoch, den 02.07.2008, 21:16 +0200 schrieb Torsten Schoenfeld:
Mario Kemper wrote:

#! /usr/bin/perl
use strict;
use Gtk2;

I think you're missing the '-init' argument here.  Or manually: Gtk2->init.

For doing stuff with windows, you might also want to take a look at Gnome2::Wnck:


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