blib/lib/X11/WindowHierarchy.pm

filter: Statementstmt 28.0% 16 / 57 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 4 / 8 Podpod 100% 2 / 2 totaltotal 26.1% 22 / 84 CC 12 SCAR 30.1
CC
12
Cov
22%
__ANON__
42.0
x11_filter_hierarchy
20.0
x11_hierarchy
12.0
CRAP 20.2
↑ Summary
1 package X11::WindowHierarchy;
2 # ABSTRACT: Retrieve information from X11 windows
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.004';
8  
9 =head1 NAME
10  
11 X11::WindowHierarchy - wrapper around L<X11::Protocol> for retrieving the current window hierarchy
12  
13 =head1 VERSION
14  
15 version 0.004
16  
17 =head1 SYNOPSIS
18  
19 use X11::WindowHierarchy;
20  
21 # Returns a list of all windows with at least one 'word' character in the
22 # window title, using the current $ENV{DISPLAY} to select the display and
23 # screen
24 my @windows = x11_filter_hierarchy(
25 filter => qr/\w/
26 );
27 printf "Found window [%s] (id %d)%s\n", $_->{title}, $_->{id}, $_->{pid} ? ' pid ' . $_->{pid} : '' for @windows;
28  
29 # Dump all information we have about all windows on display :1
30 use Data::TreeDumper;
31 print DumpTree(x11_hierarchy(display => ':1'));
32  
33 =head1 DESCRIPTION
34  
35 Provides a couple of helper functions based on L<X11::Protocol> for
36 extracting the current window hierarchy.
37  
38 =cut
39  
40 3 ▶use X11::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 our @EXPORT_OK = qw(x11_hierarchy x11_filter_hierarchy);
43 3 our @EXPORT = qw(x11_hierarchy x11_filter_hierarchy);
44  
45 =head1 FUNCTIONS
46  
47 The following functions are exported by default, to avoid this:
48  
49 use X11::WindowHierarchy qw();
50  
51 =cut
52  
53 =head2 x11_hierarchy
54  
55 Returns a hashref representing the current window hierarchy.
56  
57 Takes the following named parameters, all of which are optional:
58  
59 =over 4
60  
61 =item * display - DISPLAY string, such as ':0'
62  
63 =item * screen - the screen to use, such as 0 or 1
64  
65 =back
66  
67 Returns a hashref structure which contains the following keys:
68  
69 =over 4
70  
71 =item * id - the ID for this window
72  
73 =item * parent - the ID for the parent window
74  
75 =item * pid - the process ID for this window, if it has one
76  
77 =item * title - the window name, with any vertical whitespace (such as \n) converted to a single space
78  
79 =item * icon_name - the icon name
80  
81 =item * children - an arrayref of any child windows under this
82  
83 =back
84  
85 =cut
86  
87 0 ▶sub x11_hierarchy {
Subroutine x11_hierarchy: not called CC=3 SCAR=24.8
Pod documented
88 0 my %args = @_;
89  
90 # Only pass display if we have it
91 0 ▶ my $x = X11::Protocol->new(exists $args{display} ? (delete $args{display}) : ());
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'display'} ? : 0 0 0
92 0 ▶ my $screen = delete $args{screen} || 0;
Condition0%0/2
delete $args{'screen'} || 0
l!l
00
MC/DC0%0/1
delete $args{'screen'} || 0
delete $args{'screen'}
Truth table0%0/2
A
A= delete $args{'screen'}
Aresult
11
00
93  
94 # Tree walker
95 0 ▶ my $code; $code = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
96 # We get a window ID.
97 0 my $win = shift;
98  
99 # Extract all the properties we can
100 my %props = map {
101 # not entirely sure of the correct parameters for the API here, but, uh... "seems to work"
102 0 $_->[0] => ($x->GetProperty($win, $_->[1], 'AnyPropertyType', 0, 255))[0]
103 } map [
104 # pretty sure this only returns a scalar, and if it doesn't then we'll break in other ways,
105 # but the tests will save us!
106 $_ => scalar $x->atom($_, 1)
107 ], qw(
108 _NET_WM_ICON_NAME
109 _NET_WM_NAME
110 _NET_WM_PID
111 );
112  
113 # Get all the geometry info apart from the root, since we know that already
114 0 my %geom = $x->GetGeometry($win);
115 0 delete $geom{root};
116 0 @props{keys %geom} = values %geom;
117  
118 # Apply our ID
119 0 $props{id} = $win;
120  
121 # Grab the pid if we have it
122 0 ▶ if(my $pid = delete $props{_NET_WM_PID}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = delete $props{"_NET_WM_PID"}) 0 0 0
123 0 $props{pid} = unpack 'L1', $pid;
124 }
125  
126 # Get rid of any \n or similar chars, which seem to be legal in window titles for example
127 0 s/[\r\f\n\t\x0B]+/ /g for grep defined, values %props;
128  
129 # Remap to something more friendly
130 0 $props{title} = delete $props{_NET_WM_NAME};
131 0 $props{icon_name} = delete $props{_NET_WM_ICON_NAME};
132  
133 # Pull a list of all the child windows
134 0 my (undef, $parent, @kids) = $x->QueryTree($win);
135  
136 # TODO seems to be consistent, but should check on l10n
137 0 ▶ undef $parent if $parent eq 'None';
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq "None" 0 0 0
138 0 ▶ $props{parent} = $parent if $parent;
Branch: 1 branch
BranchTrueFalseTotal
if $parent 0 0 0
139  
140 # ... and recurse for each child window.
141 0 $props{children} = [ ];
142 0 push @{$props{children}}, $code->($_, $win) for @kids;
143 0 return \%props;
144 0 };
145  
146 # Start at the root, work down.
147 0 my $tree = $code->($x->{screens}[$screen]{root});
148  
149 # and we're done.
150 0 return $tree;
151 }
152  
153 =head2 x11_filter_hierarchy
154  
155 Similar to L</x11_hierarchy> function, but instead of returning a tree hierarchy,
156 returns a list of windows which match the given criteria.
157  
158 Takes the same parameters as L</x11_hierarchy>, with the addition of a C< filter >
159 parameter.
160  
161 If given a coderef as the filter, this will be called for each window found,
162 including the window in the output list if the coderef returns a true value.
163 The hashref representing the window will be passed as the first parameter and
164 for convenience is also available in $_. The full hierarchy will be constructed
165 before filtering the list of windows, so you can perform matches based on
166 the child elements if required.
167  
168 If given a regex as the filter, returns only the windows whose title matches
169 the given regex.
170  
171 =cut
172  
173 0 ▶sub x11_filter_hierarchy {
Subroutine x11_filter_hierarchy: not called CC=4 SCAR=30.0
Pod documented
174 0 my %args = @_;
175 0 my $code = delete $args{filter};
176  
177 0 ▶ if(ref($code) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code eq "Regexp") 0 0 0
178 0 my $re = $code;
179 0 ▶ $code = sub { return unless defined $_->{title}; $_->{title} =~ /$re/ };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_->{"title"} 0 0 0
180 }
181  
182 0 my @out;
183 0 my @pending = x11_hierarchy(%args);
184 0 ▶ while(@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 0 0 0
185 0 my $item = shift @pending;
186 0 push @pending, @{$item->{children}};
187  
188 # Pass in $_[0] and $_ for convenience.
189 0 push @out, $item for grep $code->($item), $item;
190 }
191 @out
192 0 }
193  
194 3 1;
195  
196 __END__
↑ Summary