Compare commits

...

4 Commits

2 changed files with 73 additions and 76 deletions

View File

@ -4,7 +4,6 @@ use v5.36;
#no warnings 'builtin::blessed'; #no warnings 'builtin::blessed';
#use experimental qw( builtin::blessed ); #use experimental qw( builtin::blessed );
# TODO: push list with dynamic wallpapers to ~/.spool/wallpaper_outputs
######################################################################## ########################################################################
# Usage # Usage
######################################################################## ########################################################################
@ -50,6 +49,10 @@ my $last = "$ENV{'HOME'}/.spool/last_display";
# File to log active outputs (used by swayidle, and to attempt to restore # File to log active outputs (used by swayidle, and to attempt to restore
my $active_outputs = "$ENV{'HOME'}/.spool/active_outputs"; my $active_outputs = "$ENV{'HOME'}/.spool/active_outputs";
# File to log which displays have a wallpaper active, will be written to restart wallpapers.pl
my $wallpaper_outputs .= "$ENV{'HOME'}/.spool/wallpaper_outputs";
my @wallpapers;
######################################################################## ########################################################################
# Display Serials and Names # Display Serials and Names
######################################################################## ########################################################################
@ -60,7 +63,6 @@ my $active_outputs = "$ENV{'HOME'}/.spool/active_outputs";
my %outputs = ( my %outputs = (
'DENON-AVAMP' => 'TV', 'DENON-AVAMP' => 'TV',
'Paperlike253' => 'eInk', 'Paperlike253' => 'eInk',
'0x4140' => 'x1',
'0x1305' => 'x13' '0x1305' => 'x13'
); );
@ -83,7 +85,6 @@ my %configs = (
'scale' => 1.33333, 'scale' => 1.33333,
'waybar' => 'top', 'waybar' => 'top',
'fallback' => '#010101', 'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
}, },
'TV' => { 'TV' => {
'on' => 0 'on' => 0
@ -106,7 +107,7 @@ my %configs = (
'scale' => 1.33333, 'scale' => 1.33333,
'waybar' => 'top', 'waybar' => 'top',
'fallback' => '#010101', 'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
'x13' => { 'x13' => {
'on' => 0 'on' => 0
@ -129,22 +130,10 @@ my %configs = (
'scale' => 1.5, 'scale' => 1.5,
'waybar' => 'bottom', 'waybar' => 'bottom',
'fallback' => '#010101', 'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
}, },
'eInk+x13' => { 'eInk+x13' => {
'x13' => {
'on' => 1,
'width' => 2560,
'height' => 1440,
'x' => 0,
'y' => 0,
'rotate' => 0,
'scale' => 1.5,
'waybar' => 'bottom',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
'eInk' => { 'eInk' => {
'on' => 1, 'on' => 1,
'width' => 3200, 'width' => 3200,
@ -158,7 +147,19 @@ my %configs = (
}, },
'TV' => { 'TV' => {
'on' => 0, 'on' => 0,
} },
'x13' => {
'on' => 1,
'width' => 2560,
'height' => 1440,
'x' => 0,
'y' => 0,
'rotate' => 0,
'scale' => 1.5,
'waybar' => 'bottom',
'fallback' => '#000000',
'bg' => 1,
},
}, },
'TV+eInk' => { 'TV+eInk' => {
'eInk' => { 'eInk' => {
@ -183,9 +184,9 @@ my %configs = (
'scale' => 1.3333333333, 'scale' => 1.3333333333,
'waybar' => 'top', 'waybar' => 'top',
'fallback' => '#000000', 'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
'x1' => { 'x13' => {
'on' => 0 'on' => 0
}, },
}, },
@ -204,7 +205,7 @@ my %configs = (
'scale' => 1.333333333, 'scale' => 1.333333333,
'waybar' => 'top', 'waybar' => 'top',
'fallback' => '#000000', 'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
'x13' => { 'x13' => {
'on' => 1, 'on' => 1,
@ -217,7 +218,7 @@ my %configs = (
'scale' => 1.5, 'scale' => 1.5,
'waybar' => 'bottom', 'waybar' => 'bottom',
'fallback' => '#000000', 'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
}, },
'all' => { 'all' => {
@ -243,7 +244,7 @@ my %configs = (
'scale' => 1.3333333, 'scale' => 1.3333333,
'waybar' => 'top', 'waybar' => 'top',
'fallback' => '#000000', 'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
'x13' => { 'x13' => {
'on' => 1, 'on' => 1,
@ -255,7 +256,7 @@ my %configs = (
'scale' => 1.5, 'scale' => 1.5,
'waybar' => 'bottom', 'waybar' => 'bottom',
'fallback' => '#000000', 'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png" 'bg' => 1,
}, },
}, },
); );
@ -331,7 +332,7 @@ for (my $i = 0; $i < scalar(@$displays); $i++) {
print STDERR "Output $displays->[$i]->{name} (" print STDERR "Output $displays->[$i]->{name} ("
. $displays->[$i]->{model} . $displays->[$i]->{model}
. ") found without defined function. Disabling.\n"; . ") found without defined function. Disabling.\n";
push @off, $displays->[$i]->{name}; push(@off, $displays->[$i]->{name});
next; next;
} }
@ -343,7 +344,7 @@ for (my $i = 0; $i < scalar(@$displays); $i++) {
$displays->[$i]->{name}; $displays->[$i]->{name};
# Otherwise simply list it for disabling # Otherwise simply list it for disabling
} else { } else {
push @off, $displays->[$i]->{name}; push(@off, $displays->[$i]->{name});
} }
} }
@ -359,7 +360,7 @@ foreach my $output (keys %$on) {
} }
} }
unless ($found) { unless ($found) {
push @unavailable, $output; push(@unavailable, $output);
} }
} }
if (scalar(@unavailable)) { if (scalar(@unavailable)) {
@ -386,13 +387,13 @@ unless ($waybar_only) {
} }
} }
# Kill existing Waybars # Kill existing Waybars and wallpaper rotation script
require Proc::ProcessTable; require Proc::ProcessTable;
my $t = Proc::ProcessTable->new(); my $t = Proc::ProcessTable->new();
foreach my $p ( @{ $t->table } ) { foreach my $p ( @{ $t->table } ) {
my $cmndline = $p->{'cmndline'}; my $cmndline = $p->{'cmndline'};
$cmndline =~ s/\s*$//g; $cmndline =~ s/\s*$//g;
if ($cmndline =~ /^waybar.*/) { if ($cmndline =~ /^(waybar|wallpaper\.pl).*/) {
# Never kill this process # Never kill this process
if ($p->{'pid'} == $$) { if ($p->{'pid'} == $$) {
next; next;
@ -425,7 +426,7 @@ my $waybar = '';
my @active; my @active;
foreach my $out (keys %$on) { foreach my $out (keys %$on) {
push @active, $on->{$out}->{output}; push(@active, $on->{$out}->{output});
unless ($waybar_only) { unless ($waybar_only) {
# Build command, starting by enabling and powering on # Build command, starting by enabling and powering on
my $cmd = "swaymsg -s $swaysock output $on->{$out}->{output}" . my $cmd = "swaymsg -s $swaysock output $on->{$out}->{output}" .
@ -452,13 +453,12 @@ foreach my $out (keys %$on) {
"$on->{$out}->{height}" . "$on->{$out}->{height}" .
(defined($on->{$out}->{refresh}) ? '@'.$on->{$out}->{refresh}.'Hz' : ''); (defined($on->{$out}->{refresh}) ? '@'.$on->{$out}->{refresh}.'Hz' : '');
} }
if (defined $on->{$out}->{bg} && if (defined($on->{$out}->{bg}) && $on->{$out}->{bg}) {
-f $on->{$out}->{bg}) push(@wallpapers, $on->{$out}->{output});
{ #$cmd .= " bg $on->{$out}->{bg} fit";
$cmd .= " bg $on->{$out}->{bg} fit"; #if (defined $on->{$out}->{fallback}) {
if (defined $on->{$out}->{fallback}) { #$cmd .= " $on->{$out}->{fallback}";
$cmd .= " $on->{$out}->{fallback}"; #}
}
} elsif (defined $on->{$out}->{fallback}) { } elsif (defined $on->{$out}->{fallback}) {
$cmd .= " bg $on->{$out}->{fallback} solid_color"; $cmd .= " bg $on->{$out}->{fallback} solid_color";
} }
@ -516,6 +516,19 @@ if (open(my $fh, '>', $active_outputs)) {
# Restore array formatting # Restore array formatting
$waybar = '[' . $waybar . ']'; $waybar = '[' . $waybar . ']';
# Write wallpaper outputs
my $w;
$w = join(' ', @wallpapers) if (scalar(@wallpapers));
if ($w) {
if (open(my $fh, '>', $wallpaper_outputs)) {
my $w = join(' ', @wallpapers);
print $fh $w;
close($fh);
} else {
print STDERR "Cannot write wallpaper outputs to: $wallpaper_outputs: $!\n";
}
}
# Start Waybar as fork # Start Waybar as fork
my $pid = fork; my $pid = fork;
unless ($pid) { unless ($pid) {
@ -532,3 +545,14 @@ unless ($pid) {
my $waydisplay = $ENV{'WAYLAND_DISPLAY'} || 'wayland-0'; my $waydisplay = $ENV{'WAYLAND_DISPLAY'} || 'wayland-0';
`WAYLAND_DISPLAY=$waydisplay nohup $waybar_bin -b waybar0 --config=$waybar_config >> $ENV{'HOME'}/.waybar.log`; `WAYLAND_DISPLAY=$waydisplay nohup $waybar_bin -b waybar0 --config=$waybar_config >> $ENV{'HOME'}/.waybar.log`;
} }
# Start wallpapers script as fork
if ($w) {
$pid = fork;
unless ($pid) {
open STDIN, '/dev/null';
open STDOUT, '>>/dev/null';
open STDERR, '>>/dev/null';
`/usr/bin/perl $ENV{'HOME'}/scripts/sway/wallpaper.pl -d $w`;
}
}

View File

@ -3,8 +3,7 @@
# TODO: add option to allow recursive sub-directories (in choose_image()) # TODO: add option to allow recursive sub-directories (in choose_image())
# TODO: license, etc. # TODO: license, etc.
use strict; use v5.36;
use warnings;
use POSIX; use POSIX;
use File::Copy; use File::Copy;
@ -24,9 +23,8 @@ use constant ERROR => {
our @e; our @e;
sub usage() sub usage($self)
{ {
my $self = shift;
print("$0 [output(s)] [-d|--daemon]\n print("$0 [output(s)] [-d|--daemon]\n
Sets a wallpaper by cropping an appropriate sized section of a larger image.\n Sets a wallpaper by cropping an appropriate sized section of a larger image.\n
All arguments other than the below are assumed to be output names, as defined All arguments other than the below are assumed to be output names, as defined
@ -51,10 +49,8 @@ will be reset.\n");
exit(0); exit(0);
} }
sub new sub new($class, %args)
{ {
my ($class, %args) = @_;
use AnyEvent::Sway; use AnyEvent::Sway;
$args{ipc} = AnyEvent::Sway->new(); $args{ipc} = AnyEvent::Sway->new();
use Image::Magick; use Image::Magick;
@ -125,18 +121,16 @@ $SIG{KILL} = sub {
}; };
# simply returns the array of hashes provided by swaymsg # simply returns the array of hashes provided by swaymsg
sub get_outputs sub get_outputs($self)
{ {
my $self = shift;
my $o = $self->{ipc}->get_outputs->recv() || $self->do_log('LOG_WARNING',"Failed to query 'get_outputs'"); my $o = $self->{ipc}->get_outputs->recv() || $self->do_log('LOG_WARNING',"Failed to query 'get_outputs'");
die "No outputs detected.\n" unless (scalar(@$o) > 0); die "No outputs detected.\n" unless (scalar(@$o) > 0);
return $o; return $o;
} }
# returns the same as above but with the 'name' as a hash key for easier lookup # returns the same as above but with the 'name' as a hash key for easier lookup
sub get_active sub get_active($self)
{ {
my $self = shift;
if (defined($self->{outputs}) && scalar($self->{outputs})) { if (defined($self->{outputs}) && scalar($self->{outputs})) {
my %active = (); my %active = ();
foreach my $o (@{$self->{outputs}}) { foreach my $o (@{$self->{outputs}}) {
@ -157,9 +151,8 @@ sub get_active
} }
} }
sub clean sub clean($self)
{ {
my $self = shift;
if (-e $self->{pidfile}) { if (-e $self->{pidfile}) {
open (my $fh, '<', $self->{pidfile}); open (my $fh, '<', $self->{pidfile});
my $p = <$fh>; my $p = <$fh>;
@ -170,12 +163,8 @@ sub clean
} }
} }
sub dig_dirs sub dig_dirs($self, $paths_ref, $path, $depth=0)
{ {
my $self = shift;
my $paths_ref = shift;
my $path = shift;
my $depth = shift || 0;
unless (-e $path) { unless (-e $path) {
return(undef); return(undef);
} }
@ -195,10 +184,8 @@ sub dig_dirs
} }
} }
sub choose_image sub choose_image($self)
{ {
my $self = shift;
my @i; my @i;
my $depth = 0; my $depth = 0;
my @w; my @w;
@ -218,17 +205,11 @@ sub choose_image
return $i[rand(scalar(@i))] || return undef; return $i[rand(scalar(@i))] || return undef;
} }
sub crop sub crop($self, $image, $ow, $oh)
{ {
my $self = shift;
my $image = shift;
my $ow = shift;
my $oh = shift;
my $cropped = $image; my $cropped = $image;
$cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$self->{'path'}$1-cropped.$2#; $cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$self->{'path'}$1-cropped.$2#;
#$image = "/tmp/Pharma-out.png.jpg";
use Image::Magick; use Image::Magick;
my $im = Image::Magick->new(); my $im = Image::Magick->new();
die "$image is not readable" unless (-r $image); die "$image is not readable" unless (-r $image);
@ -254,11 +235,8 @@ sub crop
return $cropped if ( -e $cropped ); return $cropped if ( -e $cropped );
} }
sub set_background sub set_background($self, $target, $cropped)
{ {
my $self = shift;
my $target = shift || return "No target or image provided";
my $cropped = shift || return "No image provided";
# TODO get fallback from javascript # TODO get fallback from javascript
my $cmd = "output $target background $cropped fill #000000"; my $cmd = "output $target background $cropped fill #000000";
$self->do_log("LOG_DEBUG", "Running $cmd\n"); $self->do_log("LOG_DEBUG", "Running $cmd\n");
@ -270,12 +248,8 @@ sub set_background
return "Failed to run Sway IPC command '$cmd'"; return "Failed to run Sway IPC command '$cmd'";
} }
sub do_log sub do_log($self, $level, $msg, $die=0)
{ {
my $self = shift;
my $level = shift;
my $msg = shift;
my $die = shift || 0;
my $min = $self->{verbose} || 5; my $min = $self->{verbose} || 5;
# Journald is borked. Just don't bother logging # Journald is borked. Just don't bother logging
@ -302,9 +276,8 @@ sub do_log
} }
} }
sub run sub run($self)
{ {
my $self = shift;
$self->do_log("LOG_DEBUG", "Fetching outputs from IPC"); $self->do_log("LOG_DEBUG", "Fetching outputs from IPC");
$self->{outputs} = $self->get_outputs(); $self->{outputs} = $self->get_outputs();
# Local copy of targets so that it will re-check active every time # Local copy of targets so that it will re-check active every time