Better comments, prevent duplicate waybars

Generate a single config file. Multiple processes seems to have run into
a bug where waybar will restore killed bars.
This commit is contained in:
John Mertz 2020-11-15 23:47:18 -05:00 committed by John Mertz
parent f8bc44a733
commit 8baad221bd
Signed by: jpm
GPG Key ID: E9C5EA2D867501AB
1 changed files with 83 additions and 48 deletions

View File

@ -1,5 +1,16 @@
#!/usr/bin/perl
########################################################################
# Usage
########################################################################
#
# $ displays.pl [layout] [-w]
#
# layout Name of desired layout, as configured starting at line 100
# If missing, last known layout is used (logged to file $last)
# -w (Re)load waybar only. Skip display configuration. Only kill
# existing waybars and start new ones for desired layout
########################################################################
# Dependencies
########################################################################
@ -17,11 +28,14 @@
# "output": __OUTPUT__
# "position": __POSITION__
# "width": __WIDTH__ (optional)
my $waybar_template = '/home/jpm/.config/waybar/config.template';
my $waybar_template = "$ENV{'HOME'}/.config/waybar/config.template";
# Temporary directory to save generated waybar config(s)
my $waybar_temporary = '/tmp';
# File to log and recover last used layout name
my $last = "$ENV{'HOME'}/.config/last_display";
########################################################################
# Display Serials and Names
########################################################################
@ -138,8 +152,6 @@ my %configs = (
use strict;
use warnings;
# Bail if zero or >1 args
my $last = "/home/jpm/.config/last_display";
my $waybar_only = 0;
my $config;
@ -157,12 +169,12 @@ if (scalar(@ARGV)) {
}
}
# Get previous config if one is not provided
unless (defined $config) {
open(my $fh, '<', $last);
open(my $fh, '<', $last) || die "Config name not provided and failed to open $last\n";
$config = <$fh>;
close($fh);
chomp $config;
print "$config\n";
}
# Bail if requested config doesn't exist
@ -170,11 +182,14 @@ unless (defined $configs{$config}) {
die "$config is not a defined configuration: " . join(', ', keys %configs) . "\n";
}
# Import and setup JSON object
use JSON::XS;
my $json = JSON::XS->new();
# Write config that is to be used so that it can be recovered as default
open(my $fh, '>', $last) || print STDERR "Config name cannot be written to $last\n";
print $fh $config;
close($fh);
# Fetch connected displays
use JSON::XS;
my $json = JSON::XS->new();
my $displays_raw = `swaymsg -t get_outputs --raw`;
my $displays = $json->decode($displays_raw);
@ -182,7 +197,6 @@ my $displays = $json->decode($displays_raw);
# displays and a list of displays to disable
my $on;
my @off;
for (my $i = 0; $i < scalar(@$displays); $i++) {
# If a display is found without any settings, print error and turn it off
@ -203,8 +217,9 @@ for (my $i = 0; $i < scalar(@$displays); $i++) {
}
# Skip enabling/disabling displays if only running waybar (re)start
unless ($waybar_only) {
# Number of simultaneous outputs is limited by possible, so disabled displays first
# Number of simultaneous outputs is limited by gpu, so disabled displays first
foreach (@off) {
# Sway returns status as JSON
@ -226,14 +241,35 @@ foreach my $p ( @{ $t->table } ) {
my $cmndline = $p->{'cmndline'};
$cmndline =~ s/\s*$//g;
if ($cmndline =~ /^waybar.*/) {
# Never kill this process
if ($p->{'pid'} == $$) {
next;
# SIGKILL match
# TODO BUG: when multiple processes are running, some respawn with new PIDs. IDK why?
} else {
$p->kill(9);
}
}
}
# Load in config template
my $template;
if (open (my $fh, '<', $waybar_template)) {
while (<$fh>) {
$template .= $_;
}
close $fh;
chomp $template;
} else {
print STDERR "Failed to load template $waybar_template\n";
}
# If template is already set up as an array, remove the square brackets so that we can concatenate multiple displays
$template =~ s/^[^\[\{]*\[(.*)\]$/$1/s;
# Setup waybar config file
my $waybar = '';
# Configure each enabled display
foreach my $out (keys %$on) {
@ -262,22 +298,15 @@ foreach my $out (keys %$on) {
}
}
# If waybar position is set, fork, generate config and execute it
if (defined $on->{$out}->{waybar}) {
my $pid = fork;
unless ($pid) {
open STDIN, '/dev/null';
open STDOUT, '>>/dev/null';
open STDERR, '>>/dev/null';
# Skip waybar setup if template failed to be loaded
if ( (defined $template) && (defined $on->{$out}->{waybar}) && ($on->{$out}->{waybar} =~ m/(top|bottom|left|right)/) ) {
# Load in config template
my $waybar;
open (my $fh, '<', $waybar_template);
while (<$fh>) {
$waybar .= $_;
# If there's already a display set up, add a comma
unless ($waybar eq '') {
$waybar .= ',';
}
close $fh;
chomp $waybar;
$waybar .= $template;
# Replace basic preferences
$waybar =~ s/__OUTPUT__/"$on->{$out}->{output}"/;
@ -289,8 +318,20 @@ foreach my $out (keys %$on) {
$waybar =~ s/([^\s]*\s*)__WIDTH__/\/\/ $1__WIDTH__/gg;
}
}
}
# Restore array formatting
$waybar = '[' . $waybar . ']';
# Start Waybar as fork
my $pid = fork;
unless ($pid) {
open STDIN, '/dev/null';
open STDOUT, '>>/dev/null';
open STDERR, '>>/dev/null';
# Write config to a temporary file
my $tmp = $waybar_temporary . "/" . $on->{$out}->{output} . ".tmp";
my $tmp = $waybar_temporary . "/waybar-" . time() .".config";
open ($fh, '>', $tmp);
print $fh $waybar;
close $fh;
@ -299,9 +340,3 @@ foreach my $out (keys %$on) {
# Remove config
unlink $tmp;
}
}
}
open(my $fh, '>', $last);
print $fh $config;
close($fh);