blib/lib/X11/WMCtrl.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 12 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 161 CC 7 SCAR 15.3
CC
7
Cov
0%
get_window_manager
12.0
modify_state
12.0
new
6.0
CRAP 4.6
1 # Copyright (c) 2014 Gavin Brown. All rights reserved. This program is
2 # free software; you can redistribute it and/or modify it under the same
3 # terms as Perl itself.
4 package X11::WMCtrl;
5 use vars qw($VERSION);
6 use strict;
7  
8 our $VERSION = '0.03';
9  
10 =pod
11  
12 =head1 NAME
13  
14 X11::WMCtrl - a Perl wrapper for the C<wmctrl> program.
15  
16 =head1 SYNOPSIS
17  
18 use X11::WMCtrl;
19 use strict;
20  
21 my $wmctrl = X11::WMCtrl->new;
22  
23 printf("window manager is %s\n", $wmctrl->get_window_manager->{name});
24  
25 my @windows = $wmctrl->get_windows;
26  
27 my $workspaces = $wmctrl->get_workspaces;
28  
29 $wmctrl->switch(1);
30  
31 my $app = $windows[0]->{title};
32  
33 $wmctrl->maximize($app);
34 $wmctrl->unmaximize($app);
35 $wmctrl->shade($app);
36 $wmctrl->unshade($app);
37  
38 $wmctrl->close($app);
39  
40 =head1 DESCRIPTION
41  
42 The C<wmctrl> program is a command line tool to interact with an EWMH/NetWM compatible X Window Manager.
43  
44 It provides command line access to almost all the features defined in the EWMH specification. Using it, it's possible to, for example, obtain information about the window manager, get a detailed list of desktops and managed windows, switch and resize desktops, change number of desktops, make windows full-screen, always-above or sticky, and activate, close, move, resize, maximize and minimize them.
45  
46 The X11::WMCtrl module provides a simple wrapper to this program.
47  
48 The C<wmctrl> program can be downloaded from L<http://sweb.cz/tripie/utils/wmctrl/>.
49  
50 =head1 CONSTRUCTOR
51  
52 my $wmctrl = X11::WMCtrl->new;
53  
54 This returns a new X11::WMCtrl object. It will fail if an executable C<wmctrl> program can't be found.
55  
56 =cut
57  
58 sub new {
59 my $self = {};
60 $self->{package} = shift;
61 bless($self, $self->{package});
62 chomp($self->{wmctrl} = `which wmctrl 2> /dev/null`);
63 die("can't find the wmctrl program") if (! -x $self->{wmctrl});
64 return $self;
65 }
66  
67 =pod
68  
69 my $wm = $wmctrl->get_window_manager;
70  
71 This returns a hashref of information about the current window manager. The contents of the hash will vary depending on which one is in use - about the only one you can rely on is C<name>.
72  
73 =cut
74  
75 sub get_window_manager {
76 my $self = shift;
77 my $data = $self->wmctrl('-m');
78 my $wm = {};
79 foreach my $line (split(/\n/, $data)) {
80 my ($name, $value) = split(/:/, $line, 2);
81 $value =~ s/^\s+//g;
82 $value =~ s/\s+$//g;
83 $value = ($value =~ 'OFF' ? undef : 1);
84 $name = ($name =~ /showing the desktop/i ? 'show_desktop' : $name);
85 $wm->{lc($name)} = $value;
86 }
87 return $wm;
88 }
89  
90 =pod
91  
92 my @windows = $wmctrl->get_windows;
93  
94 This method returns an array of hash references with information about the currently managed windows. Each element will contain these keys:
95  
96 =over
97  
98 =item * C<id> - the internal ID of the window
99  
100 =item * C<workspace> - the workspace number of the window. Workspaces are zero indexed. If the workspace value is -1, then the window is 'sticky'.
101  
102 =item * C<host> - the hostname of the X client drawing the window.
103  
104 =item * C<title> - the title of the window.
105  
106 =back
107  
108 =cut
109  
110 sub get_windows {
111 my $self = shift;
112 my $data = $self->wmctrl('-l');
113 my @windows;
114 foreach my $line (split(/\n/, $data)) {
115 my ($id, $strand) = split(/ +/, $line, 2);
116 my ($workspace, $host, $title);
117 if ($strand =~ /^-1/) {
118 $strand =~ s/^-1//;
119 $workspace = -1;
120 ($host, $title) = split(/ /, $strand, 2);
121 } else {
122 ($workspace, $host, $title) = split(/ /, $strand, 3);
123 }
124 push(@windows, {
125 id => $id,
126 workspace => $workspace,
127 host => $host,
128 title => $title,
129 });
130 }
131 return @windows;
132 }
133  
134 =pod
135  
136 my $workspaces = $wmctrl->get_workspaces;
137  
138 This methods returns a hash ref. The keys are the workspaces IDs, and the values are their names.
139  
140 =cut
141  
142 sub get_workspaces {
143 my $self = shift;
144 my $data = $self->wmctrl('-d');
145 my $workspaces = {};
146 foreach my $line (split(/\n/, $data)) {
147 my ($workspace, $strand) = split(/ /, $line, 2);
148 my ($name, undef) = split(/ /, reverse($strand), 2);
149 $name = reverse($name);
150 $workspaces->{$workspace} = $name;
151 }
152 return $workspaces;
153 }
154  
155 =pod
156  
157 $wmctrl->switch($workspace);
158  
159 Switch to workspace C<$workspace>.
160  
161 =cut
162  
163 sub switch {
164 my ($self, $workspace) = @_;
165 $self->wmctrl('-s', $workspace);
166 return 1;
167 }
168  
169 =pod
170  
171 $wmctrl->activate($window);
172  
173 Activate the window with the title C<$window> by switching to its workspace and raising it.
174  
175 =cut
176  
177 sub activate {
178 my ($self, $window) = @_;
179 $self->wmctrl('-a', $window);
180 return 1;
181 }
182  
183 =pod
184  
185 $wmctrl->close($window);
186  
187 Tell the window with the title C<$window> to close.
188  
189 =cut
190  
191 sub close {
192 my ($self, $window) = @_;
193 $self->wmctrl('-c', $window);
194 return 1;
195 }
196  
197 =pod
198  
199 $wmctrl->move_activate($window);
200  
201 Activate the window with the title C<$window> by moving it to the current workspace and raising it.
202  
203 =cut
204  
205 sub move_activate {
206 my ($self, $window) = @_;
207 $self->wmctrl('-R', $window);
208 return 1;
209 }
210  
211 =pod
212  
213 $wmctrl->move_to($window, $workspace);
214  
215 Moves the window with the title C<$window> to the workspace C<$workspace>.
216  
217 =cut
218  
219 sub move_to {
220 my ($self, $window, $workspace) = @_;
221 $self->wmctrl('-r', $window, '-t', $workspace);
222 return 1;
223 }
224  
225 =pod
226  
227 $wmctrl->maximize($window);
228  
229 Maximize C<$window>.
230  
231 =cut
232  
233 sub maximize {
234 my ($self, $window) = @_;
235 $self->modify_state($window, 'add', 'maximized_vert', 'maximized_horz');
236 return 1;
237 }
238  
239 =pod
240  
241 $wmctrl->unmaximize($window);
242  
243 Unaximize C<$window>.
244  
245 =cut
246  
247 sub unmaximize {
248 my ($self, $window) = @_;
249 $self->modify_state($window, 'remove', 'maximized_vert', 'maximized_horz');
250 return 1;
251 }
252  
253 =pod
254  
255 $wmctrl->minimize($window);
256  
257 Minimize C<$window>.
258  
259 =cut
260  
261 sub minimize {
262 my ($self, $window) = @_;
263 $self->unmaximize($window);
264 $self->modify_state($window, 'add', 'hidden');
265 return 1;
266 }
267  
268 =pod
269  
270 $wmctrl->unminimize($window);
271  
272 Unminimize C<$window>.
273  
274 =cut
275  
276 sub unminimize {
277 my ($self, $window) = @_;
278 $self->modify_state($window, 'remove', 'hidden');
279 return 1;
280 }
281  
282 =pod
283  
284 $wmctrl->shade($window);
285  
286 Shade C<$window>.
287  
288 =cut
289  
290 sub shade {
291 my ($self, $window) = @_;
292 $self->modify_state($window, 'add', 'shaded');
293 return 1;
294 }
295  
296 =pod
297  
298 $wmctrl->unshade($window);
299  
300 Unshade C<$window>.
301  
302 =cut
303  
304 sub unshade {
305 my ($self, $window) = @_;
306 $self->modify_state($window, 'remove', 'shaded');
307 return 1;
308 }
309  
310 =pod
311  
312 $wmctrl->sticky($window);
313  
314 Make the window C<$window> sticky.
315  
316 =cut
317  
318 sub stick {
319 my ($self, $window) = @_;
320 $self->modify_state($window, 'add', 'sticky');
321 return 1;
322 }
323  
324 =pod
325  
326 $wmctrl->unstick($window);
327  
328 Removes the 'sticky' property from C<$window>.
329  
330 =cut
331  
332 sub unstick {
333 my ($self, $window) = @_;
334 $self->modify_state($window, 'remove', 'sticky');
335 return 1;
336 }
337  
338 =pod
339  
340 $wmctrl->fullscreen($window);
341  
342 Make C<$window> full-screen.
343  
344 =cut
345  
346 sub fullscreen {
347 my ($self, $window) = @_;
348 $self->modify_state($window, 'add', 'fullscreen');
349 return 1;
350 }
351  
352 =pod
353  
354 $wmctrl->unfullscreen($window);
355  
356 Restore C<$window> from full-screen mode.
357  
358 =cut
359  
360 sub unfullscreen {
361 my ($self, $window) = @_;
362 $self->modify_state($window, 'remove', 'fullscreen');
363 return 1;
364 }
365  
366 =pod
367  
368 $wmctrl->wmctrl(@args);
369  
370 This methods allows you to send instructions directly to wmctrl. This is used by X11::WMCtrl internally, but if you want to do something that the module doesn't support, this is the easiest way.
371  
372 =cut
373  
374 sub wmctrl {
375 my $self = shift;
376 my @args = @_;
377 open(WMCTRL, sprintf('%s %s|', $self->{wmctrl}, join(' ', @args)));
378 my $data;
379 while (<WMCTRL>) {
380 $data .= $_;
381 }
382 close(WMCTRL);
383 return $data;
384 }
385  
386 =pod
387  
388 $wmctrl->modify_state($window, $mod, @params);
389  
390 This is another low-level function for sending state modifications to windows. The value of C<$mod> can be either C<add> or C<remove>. C<@params> may have either one or two elements. They may be any of the following:
391  
392 modal, sticky, maximized_vert, maximized_horz,
393 shaded, skip_taskbar, skip_pager, hidden,
394 fullscreen, above, below
395  
396 =cut
397  
398 sub modify_state {
399 my ($self, $window, $mod, @params) = @_;
400 die("invalid modifier '$mod'") if ($mod !~ /^(add|remove)$/i);
401 die("invalid number of params") if (scalar(@params) > 2);
402 $self->wmctrl('-r', $window, '-b', join(',', $mod, @params));
403 return 1;
404 }
405  
406 =pod
407  
408 =head1 INSTALLATION
409  
410 To install this module type the following:
411  
412 perl Makefile.PL
413 make
414 make test
415 make install
416  
417 =head1 BUGS
418  
419 Currently C<stick()>, C<unstick()>, C<minimize()> and C<unminimize()> don't work. This appears to be a problem with C<wmctrl> itself since.
420  
421 =head1 AUTHOR
422  
423 Gavin Brown (L<gavin.brown@uk.com>).
424  
425 =head1 COPYRIGHT
426  
427 Copyright (c) 2014 Gavin Brown. This program is free software, you can use it and/or modify it under the same terms as Perl itself.
428  
429 =head1 SEE ALSO
430  
431 L<wmctrl>
432  
433 =cut
434 1;