summaryrefslogtreecommitdiffstats
path: root/.local/bin/setwp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-x.local/bin/setwp104
1 files changed, 54 insertions, 50 deletions
diff --git a/.local/bin/setwp b/.local/bin/setwp
index 98938c5..fa0d255 100755
--- a/.local/bin/setwp
+++ b/.local/bin/setwp
@@ -4,6 +4,7 @@ use strict;
use warnings;
use File::HomeDir qw(home);
use File::Copy;
+use File::Find;
use constant {
WP_POOL => home() . '/pics/wp/'
@@ -12,11 +13,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,73 +30,74 @@ 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
- );
- return 0;
-}
-
-sub notify
-{
- system(
- 'notify-send',
- '-u',
- 'low',
- '-t',
- '2000',
- 'setwp',
- ' wp set'
- );
- return;
-}
-sub notify_error
-{
- system(
- 'notify-send',
- '-u',
- 'critical',
- '-t',
- '4000',
- 'setwp',
- ' Wallpaper does not exist or is not a valid file'
- );
- return;
+ if (defined $ENV{WAYLAND_DISPLAY}) {
+ $old_pid = `pidof wbg`;
+ chomp $old_pid;
+ $pid = fork();
+ if (not $pid) {
+ setsid();
+ exec("wbg", $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;
}
sub main
{
my $wp;
my $ret;
+ my $arg;
- if (@ARGV != 0 && -f $ARGV[0]) {
- $ret = set_wp($ARGV[0]);
+ $arg = 0;
+ if (@ARGV != 0 && $ARGV[0] eq '-nw') {
+ $arg = 1;
+ }
+ if (@ARGV != 0 && -f $ARGV[$arg]) {
+ $ret = set_wp($ARGV[$arg]);
+ }
+ elsif (@ARGV != 0 && -d $ARGV[$arg]) {
+ $wp = choose_wp(get_pool_files($ARGV[$arg]));
+ $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")) {
- notify() if ($ret == 0);
- notify_error() if ($ret != 0);
+ if (@ARGV == 0 || (@ARGV != 0 && $ARGV[0] ne '-nw')) {
+ my $arg = fork();
+ if (not $arg) {
+ exec('herbe', 'wp set') if ($ret == 0);
+ exec('herbe', 'wp is not a valid file') if ($ret != 0);
+ }
}
return;
}