Write wallpaper_outputs file and start wallpaper daemon

This commit is contained in:
John Mertz 2024-01-21 21:14:15 -07:00
parent fd73739229
commit b88b08cd10
1 changed files with 60 additions and 35 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
########################################################################
@ -82,7 +85,6 @@ my %configs = (
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
'TV' => {
'on' => 0
@ -105,7 +107,7 @@ my %configs = (
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 0
@ -128,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,
@ -157,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' => {
@ -182,7 +184,7 @@ my %configs = (
'scale' => 1.3333333333,
'waybar' => 'top',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 0
@ -203,7 +205,7 @@ my %configs = (
'scale' => 1.333333333,
'waybar' => 'top',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 1,
@ -216,7 +218,7 @@ my %configs = (
'scale' => 1.5,
'waybar' => 'bottom',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
},
'all' => {
@ -242,7 +244,7 @@ my %configs = (
'scale' => 1.3333333,
'waybar' => 'top',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
'x13' => {
'on' => 1,
@ -254,7 +256,7 @@ my %configs = (
'scale' => 1.5,
'waybar' => 'bottom',
'fallback' => '#000000',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
'bg' => 1,
},
},
);
@ -330,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;
}
@ -342,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});
}
}
@ -358,7 +360,7 @@ foreach my $output (keys %$on) {
}
}
unless ($found) {
push @unavailable, $output;
push(@unavailable, $output);
}
}
if (scalar(@unavailable)) {
@ -385,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;
@ -424,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}" .
@ -451,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";
}
@ -515,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) {
@ -531,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`;
}
}