Compare commits

...

2 Commits

2 changed files with 376 additions and 198 deletions

View File

@ -45,18 +45,13 @@ my $active_outputs = "$ENV{'HOME'}/.spool/active_outputs";
# Display Serials and Names
########################################################################
# Hash to match display Serial with a friendly name.
# You can get the serial from (quoted with *):
# $ swaymsg -t get_outputs
# Output eDP-1 'Unknown 0x057D *0x00000000*'
# Hash to match display model name to internal name
# You can get the model from (quoted with *):
# $ swaymsg -t get_outputs --raw
my %outputs = (
#'0x00000101' => 'Sam',
'HTNCB00059' => 'Sam',
#'3CQ4342S6W' => 'HP-1',
'0x00000101' => 'HP-1',
'3CQ3310Q1Q' => 'HP-2',
'0x00000000' => 'LVDS',
'0x00000101' => 'TV',
'LG TV SSCR2' => 'TV',
'Paperlike253' => 'eInk',
'0x4140' => 'yoga'
);
########################################################################
@ -67,14 +62,76 @@ my %outputs = (
# Second-level keys are the display friendly-names, above
# Third-level are the actual settings for that display
my %configs = (
'Both' => {
'yoga' => {
'on' => 0
},
'eInk' => {
'on' => 1,
'width' => 3200,
'height' => 1800,
'x' => 0,
'y' => 0,
'rotate' => 90,
'scale' => 2,
'waybar' => 'top',
'fallback' => '#fefefe',
},
'TV' => {
'on' => 1,
'width' => 3840,
'height' => 2160,
'x' => 900,
'y' => 0,
'rotate' => 0,
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
},
'detached' => {
'yoga' => {
'on' => 1,
'width' => 2560,
'height' => 1440,
'x' => 0,
'y' => 0,
'rotate' => 0,
'scale' => 1.33333,
'waybar' => 'bottom',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
'eInk' => {
'on' => 0
},
'TV' => {
'on' => 0
}
},
'eInk' => {
'yoga' => {
'on' => 0
},
'eInk' => {
'on' => 1,
'width' => 3840,
'height' => 2160,
'x' => 0,
'y' => 0,
'rotate' => 0,
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
'TV' => {
'on' => 0
},
},
'TV' => {
'HP-1' => {
'on' => 0
},
'HP-2' => {
'on' => 0
},
'Sam' => {
'yoga' => {
'on' => 0
},
'TV' => {
@ -84,185 +141,14 @@ my %configs = (
'x' => 0,
'y' => 0,
'rotate' => 0,
'scale' => 1.333333333,
'scale' => 1.33333,
'waybar' => 'top',
'fallback' => '#282828'
'fallback' => '#010101',
'bg' => "$ENV{HOME}/wallpapers/wallpaper.png"
},
'eDP-1' => {
'on' => 1,
'width' => 1920,
'height' => 1080,
'x' => 3079,
'y' => 0,
'rotate' => 0,
'waybar' => 'bottom',
'fallback' => '#282828'
}
},
'detached' => {
'HP-1' => {
'on' => 0
},
'HP-2' => {
'on' => 0
},
'Sam' => {
'on' => 0
},
'TV' => {
'on' => 0
},
'eDP-1' => {
'on' => 1,
'width' => 1920,
'height' => 1080,
'x' => 0,
'y' => 0,
'rotate' => 0,
'waybar' => 'bottom',
'fallback' => '#282828'
}
},
'stacked-laptop' => {
'Sam' => {
'on' => 1,
'width' => 1920,
'height' => 1200,
'x' => 960,
'y' => 0,
'rotate' => 0,
'waybar' => 'bottom',
'fallback' => '#282828'
},
'HP-1' => {
'on' => 1,
'width' => 1920,
'height' => 1080,
'x' => 1920,
'y' => 1200,
'rotate' => 0,
'waybar' => 'top',
'fallback' => '#282828'
},
'HP-2' => {
'on' => 0
},
'TV' => {
'on' => 0
},
'eDP-1' => {
'on' => 1,
'width' => 1920,
'height' => 1080,
'x' => 0,
'y' => 1200,
'rotate' => 0,
'waybar' => 'bottom',
'fallback' => '#282828'
}
},
'stacked' => {
'Sam' => {
'on' => 1,
'width' => 1920,
'height' => 1200,
'x' => 960,
'y' => 0,
'rotate' => 0,
'waybar' => 'bottom',
'fallback' => '#282828'
},
'HP-1' => {
'on' => 1,
'width' => 1920,
'height' => 1080,
'x' => 1920,
'y' => 1200,
'rotate' => 0,
'waybar' => 'top',
'fallback' => '#282828'
},
'HP-2' => {
'on' => 1,
'width' => 1920,
'height' => 1080,
'x' => 0,
'y' => 1200,
'rotate' => 0,
'waybar' => 'top',
'fallback' => '#282828'
},
'TV' => {
'on' => 0
},
'eDP-1' => {
'on' => 0
}
},
'sidebyside' => {
'HP-1' => {
'on' => 1,
'width' => 1080,
'height' => 1920,
'x' => 0,
'y' => 225,
'rotate' => 270,
'waybar' => 'top',
'fallback' => '#282828'
},
'Sam' => {
'on' => 1,
'width' => 1200,
'height' => 1920,
'x' => 1080,
'y' => 0,
'rotate' => 90,
'waybar' => 'top',
'fallback' => '#282828'
},
'HP-2' => {
'on' => 1,
'width' => 1080,
'height' => 1920,
'x' => 2280,
'y' => 225,
'rotate' => 90,
'waybar' => 'top',
'fallback' => '#282828'
},
'TV' => {
'on' => 0
},
'eDP-1' => {
'on' => 0,
}
}
);
my $hostname = `hostname`;
chomp($hostname);
if ($hostname eq "yoga.lan.john.me.tz") {
$configs{'detached'}->{'eDP-1'}->{'width'} = 2560;
$configs{'detached'}->{'eDP-1'}->{'height'} = 1440;
$configs{'detached'}->{'eDP-1'}->{'scale'} = 1.333333;
}
########################################################################
# Disable display from other laptop
########################################################################
if ($ENV{'SSH_AUTH_SOCK'} eq '/home/jpm/.ssh/ssh-agent.sock') {
foreach my $layout (keys %configs) {
delete($configs{$layout}{'LVDS'});
}
$outputs{'0x00000000'} = 'eDP-1';
} else {
foreach my $layout (keys %configs) {
delete($configs{$layout}{'eDP-1'});
}
}
########################################################################
# Program (do not edit below)
########################################################################
@ -330,19 +216,19 @@ my @off;
for (my $i = 0; $i < scalar(@$displays); $i++) {
# If a display is found without any settings print error and turn it off
unless (defined $configs{$config}{$outputs{$displays->[$i]->{serial}}}){
unless (defined $configs{$config}{$outputs{$displays->[$i]->{model}}}){
print STDERR "Output $displays->[$i]->{name} ("
. $displays->[$i]->{serial}
. $displays->[$i]->{model}
. ") found without defined function. Disabling.\n";
push @off, $displays->[$i]->{name};
next;
}
# If display is enabled, copy all of the desired settings
if ($configs{$config}{$outputs{$displays->[$i]->{serial}}}{on}) {
$on->{$outputs{$displays->[$i]->{serial}}} =
$configs{$config}{$outputs{$displays->[$i]->{serial}}};
$on->{$outputs{$displays->[$i]->{serial}}}{output} =
if ($configs{$config}{$outputs{$displays->[$i]->{model}}}{on}) {
$on->{$outputs{$displays->[$i]->{model}}} =
$configs{$config}{$outputs{$displays->[$i]->{model}}};
$on->{$outputs{$displays->[$i]->{model}}}{output} =
$displays->[$i]->{name};
# Otherwise simply list it for disabling
} else {

292
sway/wallpaper.pl Executable file
View File

@ -0,0 +1,292 @@
#!/usr/bin/env perl
use strict;
use warnings;
use lib "$ENV{HOME}/perl5/lib/perl5";
our $debug = 1; # For testing, will output configuration and errors if true
use AnyEvent::Sway;
our $s = AnyEvent::Sway->new();
our $o = $s->get_outputs->recv();
die "No outputs detected.\n" unless (scalar(@$o) > 1);
sub usage()
{
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
in my sway/displays.pl script. If no outputs are provided, all available that
are currently enabled will be set.\n
--path=<path> Path to wallpaper directory.
-p <path> Default: $ENV{HOME}/wallpapers\n
--daemon=N Configures the wallpaper to be periodically rotated for all
-d N of the given outputs. N indicates the number of seconds between
each rotation, if provided (default: 300).\n
--help This menu
-h\n");
exit(0);
}
$SIG{USR1} = sub {
# TODO: Update the timeout method so that the daemon will simply run the job as a function.
# when SIGUSR1 is received, force the alarm to expire immediately in order to rotate the image without delay.
print "This should rotate the image, but it doesn't yet";
};
$SIG{TERM} = sub {
clean();
exit(0);
};
$SIG{INT} = sub {
clean();
exit(0);
};
sub clean
{
my $pidfile = "/var/run/$ENV{HOME}-wallpaper.pid";
if (-e $pidfile) {
open (my $fh, '<', $pidfile);
my $p = <$fh>;
chomp $p;
kill($p);
unlink($pidfile);
}
}
sub choose_image
{
my $path = shift;
my @w = glob("$path/*");
return undef unless (scalar(@w));
my @i;
foreach (@w) {
if (-d "$path/$_") {
next;
}
if ($_ =~ m/\.(png|jpg)$/) {
push(@i,$_);
}
}
return $i[rand(scalar(@i))] || return undef;
}
sub get_size
{
my $target = shift;
foreach my $output (@$o) {
if ($output->{'name'} eq $target) {
return (
#$output->{'current_mode'}->{'width'},
#$output->{'current_mode'}->{'height'}
$output->{'rect'}->{'width'},
$output->{'rect'}->{'height'}
);
}
}
return undef;
}
sub crop
{
my $image = shift;
my $ow = shift;
my $oh = shift;
my $cropped = $image;
$cropped =~ s#^.*/([^/]*)\.([^\.]+)$#$1-cropped.$2#;
use Image::Magick;
my $im = Image::Magick->new();
$im->Read($image);
my ($iw, $ih) = $im->Get("columns", "rows");
# Return full size if it is smaller in either dimension then the output
if ($iw < $ow || $ih < $oh) {
print "Not cropping $image because it is too small\n";
$cropped =~ s#-cropped\.([^\.]+)$#\.$1#;
use File::Copy;
File::Copy::copy($image,$cropped);
return $cropped if (-e $cropped);
return undef;
}
print "Image size: $iw $ih\n";
print "output size: $ow $oh\n";
my ($x, $y);
$x = int(rand($iw-$ow));
$y = int(rand($ih-$oh));
print "Cropping $image ${ow}x${oh}+${x}+${y}\n";
my $err = $im->Crop(geometry=>"${ow}x${oh}+${x}+${y}");
die "$err" if ($err);
print "Writing $cropped\n";
$err = $im->Write($cropped);
die "$err" if ($err);
return $cropped if ( -e $cropped );
}
sub get_active
{
my @targets;
foreach (@$o) {
push (@targets, $_->{name});
}
return @targets;
}
sub set_background
{
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";
print "Running $cmd\n";
my $ret = $s->message(0,$cmd)->recv;
if ($ret->[0]->{success}) {
print "Success!\n";
return undef;
}
return "Failed to run Sway IPC command '$cmd'";
}
my @targets;
my $daemon = 0;
my $delay = 300;
my $crop = 1;
my $path;
while (my $arg = shift(@ARGV)) {
if ($arg eq '-h' || $arg eq '--help') {
usage();
} elsif ($arg =~ m/^\-\-path=?(.+)$/) {
die "Redundant argument '$arg'. Wallpaper path already set.\n" if ($path);
$path = $1;
} elsif ($arg eq '-p') {
die "Redundant argument '$arg'. Wallpaper path already set.\n" if ($path);
$path = shift(@ARGV);
} elsif ($arg =~ m/^\-\-daemon=?(.+)$/) {
die "Redundant argument '$arg'. Daemon mode already set.\n" if ($daemon);
$delay = $1;
$daemon = 1;
} elsif ($arg eq '-d') {
die "Redundant argument '$arg'. Daemon mode already set.\n" if ($daemon);
if ($ARGV[0] =~ m/^\d+$/) {
$delay = shift(@ARGV);
}
$daemon = 1;
} elsif ($arg eq '--nocrop' || $arg eq '-') {
die "Redundant argument '$arg'. No-crop already set.\n" unless ($crop);
} else {
push(@targets,$arg);
}
}
die "Invalid rotation delay: $delay" unless ($delay =~ m/^\d+$/);
if ($path) {
die "Invalid wallpaper path '$path'. Not a directory." unless (-d $path);
} else {
$path = "$ENV{HOME}/wallpapers";
}
if (scalar(@targets)) {
my @kept;
foreach my $t (@targets) {
my $hit = 0;
foreach (@$o) {
if ($_->{name} eq $t) {
push(@kept, $t);
$hit++;
last;
}
}
print STDERR "Requested output '$t' not found\n" unless ($hit);
}
die "None of the requested outputs are active" unless (scalar(@kept));
@targets = @kept;
}
if ($daemon) {
my $p = fork();
if ($p) {
my $pidfile = "/var/run/$ENV{HOME}-wallpaper.pid";
open(my $fh, ">", $pidfile) || die "Failed to open pidfile: $pidfile";
print $fh "Daemonized as PID: $p\n" || die "Failed to write pid ($p) to pidfile $pidfile";
close($fh);
exit(0);
}
}
if ($debug) {
print "Initial configuration:\n";
print " Targets: ( " . ( join(" ",@targets) || "All active" ) . " )\n";
print " Daemon: $daemon\n";
print " Path: $path\n";
}
my @e;
do {
my @err;
# Local copy of targets so that it will re-check active every time
my @t = @targets;
unless (scalar(@t)) {
@t = get_active();
push(@err, "No target outputs") unless (scalar(@t));
}
foreach my $a (@t) {
my $image = choose_image($path);
if (defined($image)) {
if ( -r "$image" ) {
print "selected $image\n";
my $cropped;
if ($crop) {
if ($debug) {
print "Cropping image for '$a' using '$image'\n";
}
my ($ow, $oh) = get_size($a);
$cropped = crop($image, $ow, $oh);
unless ($cropped) {
push(@err, "Failed to generate cropped image") unless ($cropped);
next;
}
} else {
$cropped = $image;
}
my $e = set_background($a,$cropped);
push(@err, $e) if (defined($e));
if ($crop) {
print "Deleting $cropped\n";
#unlink($cropped) || push(@err, "Failed to delete $cropped after setting: $!");
}
} else {
push(@err, "$a: Unable to read image $image");
}
} else {
push(@err, "$a: Unable to select image from $path");
}
}
if ($debug && $daemon) {
print STDERR join("\n",@err);
sleep($delay);
} else {
@e = @err;
}
} while ($daemon);
if (scalar(@e)) {
die "Failure while not running as daemon:\n" .
join("\n",@e);
}
exit(0);