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';
#use experimental qw( builtin::blessed );
# TODO: push list with dynamic wallpapers to ~/.spool/wallpaper_outputs
########################################################################
# 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
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
########################################################################
@ -60,7 +63,6 @@ my $active_outputs = "$ENV{'HOME'}/.spool/active_outputs";
my %outputs = (
'DENON-AVAMP' => 'TV',
'Paperlike253' => 'eInk',
'0x4140' => 'x1',
'0x1305' => 'x13'
);
@ -83,7 +85,6 @@ my %configs = (
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
'TV' => {
'on' => 0
@ -106,7 +107,7 @@ my %configs = (
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 0
@ -129,22 +130,10 @@ my %configs = (
'scale' => 1.5,
'waybar' => 'bottom',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
},
'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' => {
'on' => 1,
'width' => 3200,
@ -158,7 +147,19 @@ my %configs = (
},
'TV' => {
'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' => {
'eInk' => {
@ -183,9 +184,9 @@ my %configs = (
'scale' => 1.3333333333,
'waybar' => 'top',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x1' => {
'x13' => {
'on' => 0
},
},
@ -204,7 +205,7 @@ my %configs = (
'scale' => 1.333333333,
'waybar' => 'top',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 1,
@ -217,7 +218,7 @@ my %configs = (
'scale' => 1.5,
'waybar' => 'bottom',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
},
'all' => {
@ -243,7 +244,7 @@ my %configs = (
'scale' => 1.3333333,
'waybar' => 'top',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 1,
@ -255,7 +256,7 @@ my %configs = (
'scale' => 1.5,
'waybar' => 'bottom',
'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} ("
. $displays->[$i]->{model}
. ") found without defined function. Disabling.\n";
push @off, $displays->[$i]->{name};
push(@off, $displays->[$i]->{name});
next;
}
@ -343,7 +344,7 @@ for (my $i = 0; $i < scalar(@$displays); $i++) {
$displays->[$i]->{name};
# Otherwise simply list it for disabling
} else {
push @off, $displays->[$i]->{name};
push(@off, $displays->[$i]->{name});
}
}
@ -359,7 +360,7 @@ foreach my $output (keys %$on) {
}
}
unless ($found) {
push @unavailable, $output;
push(@unavailable, $output);
}
}
if (scalar(@unavailable)) {
@ -386,13 +387,13 @@ unless ($waybar_only) {
}
}
# Kill existing Waybars
# Kill existing Waybars and wallpaper rotation script
require Proc::ProcessTable;
my $t = Proc::ProcessTable->new();
foreach my $p ( @{ $t->table } ) {
my $cmndline = $p->{'cmndline'};
$cmndline =~ s/\s*$//g;
if ($cmndline =~ /^waybar.*/) {
if ($cmndline =~ /^(waybar|wallpaper\.pl).*/) {
# Never kill this process
if ($p->{'pid'} == $$) {
next;
@ -425,7 +426,7 @@ my $waybar = '';
my @active;
foreach my $out (keys %$on) {
push @active, $on->{$out}->{output};
push(@active, $on->{$out}->{output});
unless ($waybar_only) {
# Build command, starting by enabling and powering on
my $cmd = "swaymsg -s $swaysock output $on->{$out}->{output}" .
@ -452,13 +453,12 @@ foreach my $out (keys %$on) {
"$on->{$out}->{height}" .
(defined($on->{$out}->{refresh}) ? '@'.$on->{$out}->{refresh}.'Hz' : '');
}
if (defined $on->{$out}->{bg} &&
-f $on->{$out}->{bg})
{
$cmd .= " bg $on->{$out}->{bg} fit";
if (defined $on->{$out}->{fallback}) {
$cmd .= " $on->{$out}->{fallback}";
}
if (defined($on->{$out}->{bg}) && $on->{$out}->{bg}) {
push(@wallpapers, $on->{$out}->{output});
#$cmd .= " bg $on->{$out}->{bg} fit";
#if (defined $on->{$out}->{fallback}) {
#$cmd .= " $on->{$out}->{fallback}";
#}
} elsif (defined $on->{$out}->{fallback}) {
$cmd .= " bg $on->{$out}->{fallback} solid_color";
}
@ -516,6 +516,19 @@ if (open(my $fh, '>', $active_outputs)) {
# Restore array formatting
$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
my $pid = fork;
unless ($pid) {
@ -532,3 +545,14 @@ unless ($pid) {
my $waydisplay = $ENV{'WAYLAND_DISPLAY'} || 'wayland-0';
`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: license, etc.
use strict;
use warnings;
use v5.36;
use POSIX;
use File::Copy;
@ -24,9 +23,8 @@ use constant ERROR => {
our @e;
sub usage()
sub usage($self)
{
my $self = shift;
print("$0 [output(s)] [-d|--daemon]\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
@ -51,10 +49,8 @@ will be reset.\n");
exit(0);
}
sub new
sub new($class, %args)
{
my ($class, %args) = @_;
use AnyEvent::Sway;
$args{ipc} = AnyEvent::Sway->new();
use Image::Magick;
@ -125,18 +121,16 @@ $SIG{KILL} = sub {
};
# 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'");
die "No outputs detected.\n" unless (scalar(@$o) > 0);
return $o;
}
# 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})) {
my %active = ();
foreach my $o (@{$self->{outputs}}) {
@ -157,9 +151,8 @@ sub get_active
}
}
sub clean
sub clean($self)
{
my $self = shift;
if (-e $self->{pidfile}) {
open (my $fh, '<', $self->{pidfile});
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) {
return(undef);
}
@ -195,10 +184,8 @@ sub dig_dirs
}
}
sub choose_image
sub choose_image($self)
{
my $self = shift;
my @i;
my $depth = 0;
my @w;
@ -218,17 +205,11 @@ sub choose_image
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;
$cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$self->{'path'}$1-cropped.$2#;
#$image = "/tmp/Pharma-out.png.jpg";
use Image::Magick;
my $im = Image::Magick->new();
die "$image is not readable" unless (-r $image);
@ -254,11 +235,8 @@ sub crop
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
my $cmd = "output $target background $cropped fill #000000";
$self->do_log("LOG_DEBUG", "Running $cmd\n");
@ -270,12 +248,8 @@ sub set_background
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;
# 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->{outputs} = $self->get_outputs();
# Local copy of targets so that it will re-check active every time