blib/lib/X11/Wallpaper.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 88.8% 16 / 18 Conditioncond 71.4% 10 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 88.8% 96 / 108 CC 16 SCAR 14.2
CC
16
Cov
92%
_find_setter
7.0
_find_setter_args
4.9
set_wallpaper
4.0
CRAP 4.1
↑ Summary
1 package X11::Wallpaper;
2 # ABSTRACT: set X11 wallpaper using best available helper program
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IPC::System::Simple qw(systemx);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶ use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT_OK = qw(set_wallpaper set_wallpaper_command);
14 }
15  
16 # Ordered by preference
17 1 my @SETTERS = (
18 {
19 name => 'feh',
20 full => ['--bg-scale'],
21 tile => ['--bg-tile'],
22 center => ['--bg-center'],
23 aspect => ['--bg-fill'],
24 },
25 {
26 name => 'Esetroot',
27 full => ['-scale'],
28 tile => [],
29 center => ['-c'],
30 aspect => ['-fit'],
31 require_ldd => 'libImlib',
32 },
33 {
34 name => 'hsetroot',
35 full => ['-fill'],
36 tile => ['-tile'],
37 center => ['-center'],
38 aspect => ['-full'],
39 },
40 {
41 name => 'habak',
42 full => ['-full'],
43 tile => [],
44 center => ['-mC'],
45 aspect => ['-mS'],
46 },
47 {
48 name => 'imlibsetroot',
49 full => [qw(-s f)],
50 tile => [qw(-t -p c)],
51 center => [qw(-p c)],
52 aspect => [qw(-s a)],
53 },
54 {
55 name => 'chbg',
56 full => [qw(-once -mode maximize)],
57 tile => [qw(-once -mode tile)],
58 center => [qw(-once -mode center)],
59 aspect => [qw(-once -mode smart -max_grow 1000 -max_size 100)],
60 },
61 {
62 name => 'xsri',
63 full => [qw(--center-x --center-y --scale-width=100 --scale-height=100)],
64 tile => [qw(--tile)],
65 center => [qw(--center-x --center-y --color=black)],
66 aspect => [qw(--center-x --center-y --scale-width=100 --scale-height=100 --keep-aspect --color=black)],
67 },
68 {
69 name => 'wmsetbg',
70 full => ['-s', '-S'],
71 tile => ['-t'],
72 center => [qw(-b black -e)],
73 aspect => [qw(-b black -a -S)],
74 },
75 {
76 name => 'xsetbg',
77 full => { fallback => 'aspect' },
78 tile => [qw(-border black)],
79 center => [qw(-center -border black)],
80 aspect => [qw(-fullscreen -border black)],
81 },
82 {
83 name => 'xli', # aka xloadimage
84 full => { fallback => 'aspect' },
85 tile => [qw(-onroot -quiet -border black)],
86 center => [qw(-center -onroot -quiet -border black)],
87 aspect => [qw(-fullscreen -onroot -quiet -border black)],
88 },
89 {
90 name => 'icewmbg',
91 full => { fallback => 'tile' },
92 tile => ['-s'],
93 center => { fallback => 'tile' },
94 aspect => { fallback => 'tile' },
95 },
96 {
97 name => 'qiv',
98 full => ['--root_s'],
99 tile => ['--root_t'],
100 center => ['--root'],
101 aspect => ['--root', '-m'],
102 broken_transparency => 1,
103 },
104 {
105 name => 'xv',
106 full => [qw(-max -smooth -root -quit)],
107 tile => [qw(-root -quit)],
108 center => [qw(-rmode 5 -root -quit)],
109 aspect => [qw(-maxpect -smooth -root -quit)],
110 broken_transparency => 1,
111 },
112 );
113 1 my %SETTER = map { $_->{name} => $_ } @SETTERS;
114  
115 4 ▶sub set_wallpaper {
Subroutine set_wallpaper: called CC=4 SCAR=13.9
Pod documented
116 4 my ($image, %args) = @_;
117  
118 4 ▶ if (!defined $args{display} && (!defined $ENV{DISPLAY} || $ENV{DISPLAY} eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{"display"} and !defined($ENV{"DISPLAY"}) || $ENV{"DISPLAY"} eq "") 1 3 4
Condition83%5/6
not defined $args{"display"} and !defined($ENV{"DISPLAY"}) || $ENV{"DISPLAY"} eq ""
!ll&&!rl&&r
211
not defined $ENV{"DISPLAY"} or $ENV{"DISPLAY"} eq ""
l!l&&r!l&&!r
011
MC/DC66%2/3
not defined $args{"display"} and !defined($ENV{"DISPLAY"}) || $ENV{"DISPLAY"} eq ""
not defined $args{"display"} not defined $ENV{"DISPLAY"} $ENV{"DISPLAY"} eq ""
Truth table75%3/4
not(A) and not(B) or C
A= not defined $args{"display"}B= not defined $ENV{"DISPLAY"}C= $ENV{"DISPLAY"} eq ""
ABCresult
0XX0
1000
11X1
1011
119 1 croak "You are not connected to an X session, consider setting the DISPLAY"
120 . "environment variable or passing the 'display' option to set_wallpaper()";
121 }
122  
123 3 my @command = set_wallpaper_command($image, %args);
124 3 return systemx(@command);
125 }
126  
127 14 ▶sub set_wallpaper_command {
Subroutine set_wallpaper_command: called CC=4 SCAR=13.9
Pod documented
128 14 my ($image, %args) = @_;
129 14 ▶ my $mode = $args{mode} || 'full';
Condition100%2/2
$args{'mode'} || 'full'
l!l
86
MC/DC100%1/1
$args{'mode'} || 'full'
$args{'mode'}
Truth table100%2/2
A
A= $args{'mode'}
Aresult
11
00
130 14 ▶ my $setter = $args{setter} || _find_setter($mode);
Condition66%2/3
$args{'setter'} || _find_setter($mode)
l!l&&r!l&&!r
1130
MC/DC0%0/2
$args{'setter'} || _find_setter($mode)
$args{'setter'} _find_setter($mode)
Truth table66%2/3
A || B
A= $args{'setter'}B= _find_setter($mode)
ABresult
1X1
011
000
131 12 my $display = $args{display};
132  
133 # Build command
134 12 ▶ my @command = defined $display ? ('env', "DISPLAY=$display") : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $display ? : 3 9 12
135 12 push @command, ($setter, _find_setter_args($setter, $mode), $image);
136  
137 12 return @command;
138 }
139  
140 # Find the best available setter for the given 'mode'.
141 13 ▶sub _find_setter {
Subroutine _find_setter: called CC=7 SCAR=19.5
142 13 my $mode = shift;
143 13 my $best;
144 13 my $fallback = 0;
145  
146 13 for my $setter (@SETTERS) {
147 70 ▶ next if !defined which($setter->{name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined which($setter->{"name"}) 57 13 70
148 13 ▶ next if !defined $setter->{$mode};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $setter->{$mode} 1 12 13
149  
150 12 ▶ if (ref $setter->{$mode} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $setter->{$mode} eq "HASH") 1 11 12
151 1 $fallback = 1;
152 }
153  
154 12 $best = $setter->{name};
155 12 ▶ last if !$fallback; # else wait for a better one
Branch: 1 branch
BranchTrueFalseTotal
unless $fallback 10 2 12
156 }
157  
158 13 ▶ if (!defined $best) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $best) 2 11 13
159 2 croak "No setter program found for mode '$mode'!";
160 }
161  
162 11 return $best;
163 }
164  
165 12 ▶sub _find_setter_args {
Subroutine _find_setter_args: called CC=4 SCAR=15.9
166 12 my ($setter, $mode) = @_;
167 12 my $args = $SETTER{$setter}{$mode};
168 12 ▶ if (ref $args eq 'HASH' && $args->{fallback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH" and $args->{"fallback"}) 0 12 12
Condition33%1/3
ref $args eq "HASH" and $args->{"fallback"}
!ll&&!rl&&r
1200
MC/DC0%0/2
ref $args eq "HASH" and $args->{"fallback"}
ref $args eq "HASH" $args->{"fallback"}
Truth table33%1/3
A and B
A= ref $args eq "HASH"B= $args->{"fallback"}
ABresult
0X0
100
111
169 0 $args = $SETTER{$setter}{ $args->{fallback} };
170 }
171 12 ▶ ref $args eq 'ARRAY' or die "Expecting arguments for $setter/$mode. Fallback chain?";
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq "ARRAY" or die "Expecting arguments for $setter/$mode. Fallback chain?" 12 0 12
172  
173 12 return @$args;
174 }
175  
176 1 1;
177  
178  
179  
180 =pod
181  
182 =head1 NAME
183  
184 X11::Wallpaper - set X11 wallpaper using best available helper program
185  
186 =head1 VERSION
187  
188 version 1.1
189  
190 =head1 SYNOPSIS
191  
192 use X11::Wallpaper qw(set_wallpaper);
193 set_wallpaper( "./foo.jpg", {
194 mode => 'full', # default, or: aspect, center, tile
195 setter => 'feh', # override setter
196 display => ':0.0' # override X display
197 } );
198  
199 my @cmd = set_wallaper_command(...); # just give me the command
200  
201 =head1 DESCRIPTION
202  
203 This module provides an interface for setting the background on X11
204 systems, by recruiting a suitable helper script (feh, Esetroot, hsetroot,
205 chbg, xli etc.) and providing appropriate options.
206  
207 =head1 METHODS
208  
209 =head2 set_wallpaper($image_path, %args)
210  
211 Sets C<$image_path> as the desktop wallpaper. The following args are
212 supported:
213  
214 =over
215  
216 =item mode
217  
218 May be 'full' (fullscreen, stretched to fit - the default), 'tile',
219 'center' (do not stretch) or 'aspect' (fullscreen, preserving
220 aspect ratio). For the latter two options, the background for any
221 borders around the image is set to black.
222  
223 =item setter
224  
225 Manually specify the program to use, e.g. 'qiv', provided it is
226 in this module's dictionary of commands.
227  
228 =item display
229  
230 Override the X display to use, e.g. ':0.0'. Otherwise defaults to
231 the value of the DISPLAY environment variable.
232  
233 =back
234  
235 =head2 @cmd = set_wallpaper_command($image_path, %args)
236  
237 As with C<set_wallpaper>, except returns the command (as a list of
238 arguments) instead of executing it.
239  
240 =head1 CREDITS
241  
242 Inspired by the C<awsetbg> shell script by Julian Danjou, which in turn
243 is derived from C<fbsetbg> by Han Boetes.
244  
245 =head1 TODO
246  
247 Skip the middle man and code against the X11 libraries directly. But
248 that wouldn't be quite as portable...
249  
250 =head1 AUTHOR
251  
252 Richard Harris <RJH@cpan.org>
253  
254 =head1 COPYRIGHT AND LICENSE
255  
256 This software is copyright (c) 2012 by Richard Harris.
257  
258 This is free software; you can redistribute it and/or modify it under
259 the same terms as the Perl 5 programming language system itself.
260  
261 =cut
262  
263  
264 __END__
↑ Summary