2016-06-21 133 views
2

我有一個Perl Gtk3腳本,其中我想獲取當前顯示屏上打開的所有X11窗口的列表。如何使用Gtk3 :: Gdk列出所有X11窗口

到目前爲止,我有:

#!/usr/bin/perl -w 
package Gtk3::Gdk::X11; 
use strict; 
use Glib::Object::Introspection; 
Glib::Object::Introspection->setup(basename => 'GdkX11', version => '3.0', package => 'Gtk3::Gdk'); 

package main; 
use strict; 
use Gtk3 -init; 
use Data::Dumper; 
my $x_root = Gtk3::Gdk::x11_get_default_root_xwindow(); 
print "x_root = " . Dumper $x_root; 
my $gdk_screen = Gtk3::Gdk::Screen::get_default(); 
print "gdk_screen = " . Dumper $gdk_screen; 
my $wm_name = $gdk_screen->get_window_manager_name; 
print "wm_name = '$wm_name'\n"; 
my $gdk_display = Gtk3::Gdk::Display::open(':0'); 
print "gdk_display = " . Dumper $gdk_display; 
my $gdk_root = Gtk3::Gdk::X11Window::foreign_new_for_display($x_root, $gdk_display, $x_root); 
print "gdk_root = " . Dumper $gdk_root; 
my $a_net_client_list = Gtk3::Gdk::atom_intern('_NET_CLIENT_LIST', 1); 
print "a_net_client_list = " . Dumper $a_net_client_list; 
my $a_window = Gtk3::Gdk::atom_intern('WINDOW', 1); 
print "a_window = " . Dumper $a_window; 
my $wins = Gtk3::Gdk::property_get($gdk_root, $a_net_client_list, $a_window, 0, 1024, 0); 
print "wins = " . Dumper $wins; 

但是,其結果是:

x_root = $VAR1 = 2443; 
gdk_screen = $VAR1 = bless({}, 'Gtk3::Gdk::X11Screen'); 
wm_name = 'Xfwm4' 
gdk_display = $VAR1 = bless({}, 'Gtk3::Gdk::X11Display'); 
gdk_root = $VAR1 = bless({}, 'Gtk3::Gdk::X11Window'); 
a_net_client_list = $VAR1 = bless(do{\(my $o = 129)}, 'Gtk3::Gdk::Atom'); 
a_window = $VAR1 = bless(do{\(my $o = 33)}, 'Gtk3::Gdk::Atom'); 
** 
ERROR:gperl-i11n-marshal-struct.c:43:struct_to_sv: assertion failed: (!own) 
Aborted (core dumped) 

如果有一種方法在Perl使用libwnck3,可能工作了。

+0

我不熟悉在Gtk3編程,所以我不能幫你解決特定的錯誤..但是我使用了一個實用程序['wmctrl'](http://linux.die.net/man/1/ wmctrl)可以列出所有打開的窗口。它[用C編寫](https://packages.debian.org/source/jessie/wmctrl),所以應該可以使用XS或Inline :: C移植列出窗口的代碼部分到Perl –

+0

爲什麼要使用GTK呢?只需使用Xlib進行簡單調用並查詢根窗口的樹。 –

+0

我不想從頭開始編寫一個工具,我正在嘗試將一個功能添加到現有的大型Gtk3應用程序中。 – TheAmigo

回答

1

我找到了解決方案(雖然不是我的首選之一):

#!/usr/bin/perl -w 
use strict; 
use Gtk3 -init; 
use Glib::Object::Introspection; 
Glib::Object::Introspection->setup(basename => 'GdkX11', version => '3.0', package => 'Gtk3::Gdk'); 
Glib::Object::Introspection->setup(basename => 'Wnck', version => '3.0', package => 'Wnck'); 

my $screen = Wnck::Screen::get_default(); 
$screen->force_update; 
for my $win (@{$screen->get_windows}) { 
     my $name = $win->get_name; 
     my $ws = $win->get_workspace; 
     my $w = $ws ? $ws->get_number : -1; 
     printf "(%04d, %04d) %04dx%04d:%2s $name\n", $win->get_geometry, $w; 
} 

我寧可不使用Wnck的原因是因爲the documentation說:the use of this library is relatively expensive in terms of resources。我的應用程序不需要Wnck提供的大部分功能,所以這有點矯枉過正。