blib/lib/Games/Axmud/Obj/WMCtrl.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 163 CC 8 SCAR 16.6
CC
8
Cov
0%
get_window_manager
12.0
get_windows
12.0
modify_state
12.0
CRAP 5.3
1 # Copyright 2004 by the Gtk3-Perl team
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # Lesser Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::WMCtrl, a modification of X11::WMCtrl (v0.03) by Gavin Brown
16 #
17 # Packaged along with Axmud, renamed and reduced to alleviate various problems
18 # List of changes (besides cosmetic ones):
19 # - Added 'use warnings' as the lack of it causes Kwalitee errors
20 # - Commented out $VERSION as it causes Kwalitee errors
21 # - Removed POD stuff
22 # - Modified ->new so it returns 'undef', rather than doing a Perl die(), if wmctrl is not
23 # available on the user's system
24 # - Modified ->get_windows which (rarely) creates a 'PERL WARNING: Use of uninitialized value
25 # $data in split' error
26 # - Commented out the close() function, which Axmud doesn't need anyway, and which sometimes
27 # produces an unexplained Perl error
28  
29 package Games::Axmud::Obj::WMCtrl;
30  
31 use vars qw($VERSION);
32 use strict;
33 use warnings;
34  
35 #our $VERSION = '0.03';
36  
37 #sub new {
38 # my $self = {};
39 # $self->{package} = shift;
40 # bless($self, $self->{package});
41 # chomp($self->{wmctrl} = `which wmctrl 2> /dev/null`);
42 # die("can't find the wmctrl program") if (! -x $self->{wmctrl});
43 # return $self;
44 #}
45 sub new {
46 my $self = {};
47 $self->{package} = shift;
48 bless($self, $self->{package});
49 chomp($self->{wmctrl} = `which wmctrl 2> /dev/null`);
50 if (! -x $self->{wmctrl}) {
51 return undef;
52 } else {
53 return $self;
54 }
55 }
56  
57 sub get_window_manager {
58 my $self = shift;
59 my $data = $self->wmctrl('-m');
60 my $wm = {};
61 foreach my $line (split(/\n/, $data)) {
62 my ($name, $value) = split(/:/, $line, 2);
63 $value =~ s/^\s+//g;
64 $value =~ s/\s+$//g;
65 $value = ($value =~ 'OFF' ? undef : 1);
66 $name = ($name =~ /showing the desktop/i ? 'show_desktop' : $name);
67 $wm->{lc($name)} = $value;
68 }
69 return $wm;
70 }
71  
72 #sub get_windows {
73 # my $self = shift;
74 # my $data = $self->wmctrl('-l');
75 # my @windows;
76 # foreach my $line (split(/\n/, $data)) {
77 # my ($id, $strand) = split(/ +/, $line, 2);
78 # my ($workspace, $host, $title);
79 # if ($strand =~ /^-1/) {
80 # $strand =~ s/^-1//;
81 # $workspace = -1;
82 # ($host, $title) = split(/ /, $strand, 2);
83 # } else {
84 # ($workspace, $host, $title) = split(/ /, $strand, 3);
85 # }
86 # push(@windows, {
87 # id => $id,
88 # workspace => $workspace,
89 # host => $host,
90 # title => $title,
91 # });
92 # }
93 # return @windows;
94 #}
95 sub get_windows {
96 my $self = shift;
97 my $data = $self->wmctrl('-l');
98 my @windows;
99 if (defined $data) {
100 foreach my $line (split(/\n/, $data)) {
101 my ($id, $strand) = split(/ +/, $line, 2);
102 my ($workspace, $host, $title);
103 if ($strand =~ /^-1/) {
104 $strand =~ s/^-1//;
105 $workspace = -1;
106 ($host, $title) = split(/ /, $strand, 2);
107 } else {
108 ($workspace, $host, $title) = split(/ /, $strand, 3);
109 }
110 push(@windows, {
111 id => $id,
112 workspace => $workspace,
113 host => $host,
114 title => $title,
115 });
116 }
117 }
118 return @windows;
119 }
120  
121 sub get_workspaces {
122 my $self = shift;
123 my $data = $self->wmctrl('-d');
124 my $workspaces = {};
125 foreach my $line (split(/\n/, $data)) {
126 my ($workspace, $strand) = split(/ /, $line, 2);
127 my ($name, undef) = split(/ /, reverse($strand), 2);
128 $name = reverse($name);
129 $workspaces->{$workspace} = $name;
130 }
131 return $workspaces;
132 }
133  
134 sub switch {
135 my ($self, $workspace) = @_;
136 $self->wmctrl('-s', $workspace);
137 return 1;
138 }
139  
140 sub activate {
141 my ($self, $window) = @_;
142 $self->wmctrl('-a', $window);
143 return 1;
144 }
145  
146 #sub close {
147 # my ($self, $window) = @_;
148 # $self->wmctrl('-c', $window);
149 # return 1;
150 #}
151  
152 sub move_activate {
153 my ($self, $window) = @_;
154 $self->wmctrl('-R', $window);
155 return 1;
156 }
157  
158 sub move_to {
159 my ($self, $window, $workspace) = @_;
160 $self->wmctrl('-r', $window, '-t', $workspace);
161 return 1;
162 }
163  
164 sub maximize {
165 my ($self, $window) = @_;
166 $self->modify_state($window, 'add', 'maximized_vert', 'maximized_horz');
167 return 1;
168 }
169  
170 sub unmaximize {
171 my ($self, $window) = @_;
172 $self->modify_state($window, 'remove', 'maximized_vert', 'maximized_horz');
173 return 1;
174 }
175  
176 sub minimize {
177 my ($self, $window) = @_;
178 $self->unmaximize($window);
179 $self->modify_state($window, 'add', 'hidden');
180 return 1;
181 }
182  
183 sub unminimize {
184 my ($self, $window) = @_;
185 $self->modify_state($window, 'remove', 'hidden');
186 return 1;
187 }
188  
189 sub shade {
190 my ($self, $window) = @_;
191 $self->modify_state($window, 'add', 'shaded');
192 return 1;
193 }
194  
195 sub unshade {
196 my ($self, $window) = @_;
197 $self->modify_state($window, 'remove', 'shaded');
198 return 1;
199 }
200  
201 sub stick {
202 my ($self, $window) = @_;
203 $self->modify_state($window, 'add', 'sticky');
204 return 1;
205 }
206  
207 sub unstick {
208 my ($self, $window) = @_;
209 $self->modify_state($window, 'remove', 'sticky');
210 return 1;
211 }
212  
213 sub fullscreen {
214 my ($self, $window) = @_;
215 $self->modify_state($window, 'add', 'fullscreen');
216 return 1;
217 }
218  
219 sub unfullscreen {
220 my ($self, $window) = @_;
221 $self->modify_state($window, 'remove', 'fullscreen');
222 return 1;
223 }
224  
225 sub wmctrl {
226 my $self = shift;
227 my @args = @_;
228 open(WMCTRL, sprintf('%s %s|', $self->{wmctrl}, join(' ', @args)));
229 my $data;
230 while (<WMCTRL>) {
231 $data .= $_;
232 }
233 close(WMCTRL);
234 return $data;
235 }
236  
237 sub modify_state {
238 my ($self, $window, $mod, @params) = @_;
239 die("invalid modifier '$mod'") if ($mod !~ /^(add|remove)$/i);
240 die("invalid number of params") if (scalar(@params) > 2);
241 $self->wmctrl('-r', $window, '-b', join(',', $mod, @params));
242 return 1;
243 }
244  
245 1;