Add sock and display id to allow non-attached execution

SWAYSOCK is now static so it can be referenced by all of the swaymsg
commands.

waybar now includes a static WAYLAND_DISPLAY declaration.

This allows for recovery/configuration via SSH as well as groundwork for
udev (un)docking rules.
This commit is contained in:
John Mertz 2021-08-12 17:27:40 -04:00 committed by John Mertz
parent 567d271786
commit 24457ca338
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
1 changed files with 15 additions and 10 deletions

View File

@ -30,6 +30,8 @@
# "width": __WIDTH__ (optional)
my $waybar_template = "$ENV{'HOME'}/.config/waybar/config.template";
my $swaysock = "/home/jpm/.config/sway-ipc.sock";
# Path to actual config file generated from template
my $waybar_config = "$ENV{'HOME'}/.config/waybar/config";
@ -206,7 +208,7 @@ close($fh);
# Fetch connected displays
use JSON::XS;
my $json = JSON::XS->new();
my $displays_raw = `swaymsg -t get_outputs --raw`;
my $displays_raw = `swaymsg -s $swaysock -t get_outputs --raw`;
my $displays = $json->decode($displays_raw);
# For each connected display, collect the desired settings for enabled
@ -252,7 +254,8 @@ foreach my $output (keys %$on) {
}
}
if (scalar(@unavailable)) {
die "Config requires unavailable output(s) " . join(', ', @unavailable) . "\n";
die "Config requires unavailable output(s) " . join(', ', @unavailable)
. "\n";
}
# Skip enabling/disabling displays if only running waybar (re)start
@ -262,13 +265,13 @@ unless ($waybar_only) {
foreach (@off) {
# Sway returns status as JSON
my $res_raw = `sway output $_ disable`;
my $res_raw = `swaymsg -s $swaysock output $_ disable`;
my $res = $json->decode($res_raw)->[0];
# If failed, print to STDERR
unless ($res->{success}) {
print STDERR "Error ($res->{error}) in command 'sway"
. "output $_ disable'\n";
print STDERR "Error ($res->{error}) in command 'swaymsg"
. " -s $swaysock output $_ disable'\n";
}
}
@ -316,7 +319,8 @@ foreach my $out (keys %$on) {
push @active, $on->{$out}->{output};
unless ($waybar_only) {
# Build command, starting by enabling and powering on
my $cmd = "sway output $on->{$out}->{output} enable dpms on";
my $cmd = "swaymsg -s $swaysock output $on->{$out}->{output}" .
" enable dpms on";
# If additional options are provided, add them to command
if (defined $on->{$out}->{rotate}) {
@ -328,7 +332,8 @@ foreach my $out (keys %$on) {
if (defined $on->{$out}->{width} &&
defined $on->{$out}->{height} )
{
$cmd .= " mode $on->{$out}->{width}x$on->{$out}->{height}";
$cmd .= " mode $on->{$out}->{width}x" .
"$on->{$out}->{height}";
}
# Sway returns status as JSON
@ -337,7 +342,8 @@ foreach my $out (keys %$on) {
# If failed, print to STDERR
unless ($res->{success}) {
print STDERR "Error ($res->{error}) in command '$cmd'\n";
print STDERR "Error ($res->{error}) in command " .
"'$cmd'\n";
}
}
@ -371,7 +377,6 @@ open($fh, '>', $active_outputs);
print $fh join(' ', @active);
close($fh);
# Restore array formatting
$waybar = '[' . $waybar . ']';
@ -385,5 +390,5 @@ unless ($pid) {
open ($fh, '>', $waybar_config);
print $fh $waybar;
close $fh;
`nohup waybar --config=$waybar_config >> waybar.log`;
`WAYLAND_DISPLAY=wayland-1 nohup waybar --config=$waybar_config >> waybar.log`;
}