summaryrefslogtreecommitdiffstats
path: root/.local/bin/setwp
diff options
context:
space:
mode:
Diffstat (limited to '.local/bin/setwp')
-rwxr-xr-x.local/bin/setwp59
1 files changed, 42 insertions, 17 deletions
diff --git a/.local/bin/setwp b/.local/bin/setwp
index 98938c5..5340138 100755
--- a/.local/bin/setwp
+++ b/.local/bin/setwp
@@ -4,6 +4,8 @@ use strict;
use warnings;
use File::HomeDir qw(home);
use File::Copy;
+use File::Find;
+use POSIX qw(setsid);
use constant {
WP_POOL => home() . '/pics/wp/'
@@ -12,11 +14,13 @@ use constant FEH_PATH => 'feh';
sub get_pool_files
{
+ my ($pool) = (@_);
my @files;
- opendir(DIR, WP_POOL) or die "Couldn't open directory " . WP_POOL . ": $!";
- @files = grep { !/^\./ } readdir(DIR);
- closedir(DIR);
+ $pool = WP_POOL if not @_;
+ find(sub {
+ push @files, $File::Find::name if -f;
+ }, $pool);
return @files;
}
@@ -27,27 +31,44 @@ sub choose_wp
my $wp;
$rand = int(rand(@files));
- $wp = WP_POOL . $files[$rand];
+ $wp = $files[$rand];
return $wp;
}
sub set_wp
{
my ($wp) = @_;
+ my $pid;
+ my $old_pid;
return 1 if !(-r $wp) || !(-f $wp);
- system(
- FEH_PATH,
- '--no-fehbg',
- '--image-bg',
- '#1d2021',
- '--bg-fill',
- $wp,
- '--bg-fill',
- $wp,
- '--bg-fill',
- $wp
- );
+
+ if (defined $ENV{WAYLAND_DISPLAY}) {
+ $old_pid = `pidof swaybg`;
+ chomp $old_pid;
+ $pid = fork();
+ if (not $pid) {
+ setsid();
+ exec("swaybg", "--image", $wp);
+ } else {
+ sleep(1);
+ exec("kill", $old_pid);
+ }
+ }
+ else {
+ system(
+ FEH_PATH,
+ '--no-fehbg',
+ '--image-bg',
+ '#1d2021',
+ '--bg-fill',
+ $wp,
+ '--bg-fill',
+ $wp,
+ '--bg-fill',
+ $wp
+ );
+ }
return 0;
}
@@ -87,8 +108,12 @@ sub main
if (@ARGV != 0 && -f $ARGV[0]) {
$ret = set_wp($ARGV[0]);
}
+ elsif (@ARGV != 0 && -d $ARGV[0]) {
+ $wp = choose_wp(get_pool_files($ARGV[0]));
+ $ret = set_wp($wp);
+ }
else {
- $wp = choose_wp(get_pool_files());
+ $wp = choose_wp(get_pool_files(WP_POOL));
$ret = set_wp($wp);
}
if (@ARGV == 0 || (@ARGV != 0 && $ARGV[0] ne "-nw")) {