-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwallpaper.pl
executable file
·195 lines (149 loc) · 3.98 KB
/
wallpaper.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env perl
use local::lib;
use Modern::Perl;
use DB_File;
use Data::Dumper;
use File::Find::Object;
use Getopt::Long qw(HelpMessage);
use File::Spec;
use File::Stat;
use IO::All;
use DateTime;
my $PREFIX = qq($ENV{HOME}/.wallpapers);
my $LOCK = qq{$PREFIX/lock};
my $CATEGORY = qq{$PREFIX/category};
my $WALLPAPER_DIR = qq{$PREFIX/Wallpapers};
my $CURRENT = qq{$PREFIX/current};
my $PREVIOUS = qq{$PREFIX/previous};
my $SOURCES = qq{$PREFIX/sources};
my $DEFAULT_CATEGORY = q{all};
unless (-e $WALLPAPER_DIR) {
die "Wallpaper directory, $WALLPAPER_DIR, does not exist";
}
my %history = ();
tie %history, 'DB_File', qq{$PREFIX/history}; ## no critic (ProhibitTies)
GetOptions(
'category|c=s' => sub {$_[1] > io($CATEGORY)},
'clear' => sub {unlink $CATEGORY},
'flush-cache|f' => sub {
%history = ();
exit;
},
'lock|l' => sub {2 > io($LOCK)},
'unlock|u' => sub {unlink($LOCK)},
'previous|p' => sub {
set_wallpaper(io($PREVIOUS)->all);
exit;
},
'image|i=s' => sub {
set_wallpaper($_[1]);
exit;
},
'dump-cache|d' => sub {
print {*STDOUT} Dumper \%history;
exit;
},
'h|help' => \&HelpMessage,
)
or HelpMessage(1);
exit if -e $LOCK;
set() unless -e $LOCK;
sub set {
my @wallpaper_dirs = get_wallpaper_dirs();
my @wallpapers = get_wallpapers(@wallpaper_dirs);
return _set(\@wallpapers);
}
sub _set {
my ($papers) = @_;
my $rc = 0;
my $set_paper = 0;
if (scalar @{$papers} == 1) {
$rc = set_wallpaper($papers->[0]);
$set_paper = 1;
} else {
while (my $paper = get_random_wallpaper($papers)) {
next if exists $history{$paper};
$rc = set_wallpaper($paper);
$set_paper = 1;
last;
}
}
if (not $set_paper) {
%history = ();
} else {
return $rc;
}
return _set($papers);
}
sub get_wallpaper_dirs {
my @paths = ();
my $category = (-e $CATEGORY) ? io($CATEGORY)->getline : $DEFAULT_CATEGORY;
if ($category eq $DEFAULT_CATEGORY) {
return map {File::Spec->join($WALLPAPER_DIR, $_)} io($SOURCES)->chomp->getlines;
} else {
push @paths, $WALLPAPER_DIR;
}
push @paths, $category;
my $dir = File::Spec->join(@paths);
die qq{Wallpaper directory ($dir) does not exist} if not -e $dir;
return ($dir);
}
sub get_wallpapers {
my (@dirs) = @_;
my $now = DateTime->now();
my $tree = File::Find::Object->new({}, @dirs);
my @papers = ();
my %weights = (
86400 => 1000,
604800 => 500,
2592000 => 200,
);
while (my $leaf = $tree->next()) {
next if -d $leaf;
next if exists $history{$leaf};
my $stat = File::Stat->new($leaf);
for my $weight (keys %weights) {
if ((($now->epoch - $weight) < $stat->ctime)) {
push @papers, $leaf for (1 .. ($weights{$weight} - 1));
}
}
push @papers, $leaf;
}
# TODO - can @papers be empty?
return @papers;
}
sub get_random_wallpaper {
my ($papers) = @_;
my $pos = int(rand(scalar @{$papers}));
my $paper = $papers->[$pos];
return splice(@{$papers}, $pos, 1);
}
sub set_wallpaper {
my ($paper) = @_;
my $rv = io->pipe(qq{fbsetbg -f '$paper'})->all;
$history{$paper} = 1;
rename $CURRENT, $PREVIOUS;
shift > io($CURRENT);
return $rv;
}
__END__
=head1 NAME
wallpaper.pl - Script to manage my desktop wallpapers.
=head1 SYNOPSIS
wallpaper.pl [OPTIONS]
Options:
-c, --category Wallpaper category
-f, --flush-cache Flush the wallpaper cache
-d, --dump-cache Dump the current wallpaper cache to STDOUT
-l, --lock Lock the current wallpaper
-u, --unlock Unlock the current wallpaper
--clear Clear the previous wallpaper category
-p, --previous Set the wallpaper to the previous paper
-i, --image Set image as the current wallpaper
=head1 DESCRIPTION
This sciprt is used to manage the desktop wallpaper displayed on my system.
=head1 ENVIRONMENT VARIABLES
None
=head1 VERSION
0.2
=cut