From 7fb00b5f09425d2d1f08f93a954e75a0721376f9 Mon Sep 17 00:00:00 2001 From: John Mertz Date: Sun, 17 Jan 2021 21:51:31 -0500 Subject: [PATCH] Default displays changed. Added fallback action. If a config is selected for which the displays do not exist, either bail or if in recovery mode, restore 'detached'. The latter was added in anticipation of the swayidle script which will attempt to restore a layout that may no longer be possible. Since swayidle disables dpms, it must allow a backup display to come live if the last logged layout is not possible. --- sway/displays.pl | 57 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/sway/displays.pl b/sway/displays.pl index 7ddc06f..5c1d306 100755 --- a/sway/displays.pl +++ b/sway/displays.pl @@ -36,6 +36,9 @@ my $waybar_temporary = '/tmp'; # File to log and recover last used layout name my $last = "$ENV{'HOME'}/.config/last_display"; +# File to log active outputs (used by swayidle, and to attempt to restore +my $active_outputs = "$ENV{'HOME'}/.config/active_outputs"; + ######################################################################## # Display Serials and Names ######################################################################## @@ -45,9 +48,12 @@ my $last = "$ENV{'HOME'}/.config/last_display"; # $ swaymsg -t get_outputs # Output eDP-1 'Unknown 0x057D *0x00000000*' my %outputs = ( - '0x00000101' => 'Sam', + #'0x00000101' => 'Sam', + 'HTNCB00059' => 'Sam', + #'3CQ3310Q1Q' => 'Sam', '3CQ4342S6W' => 'HP-1', - '3CQ3310Q1Q' => 'HP-2', + '0x00000101' => 'HP-2', + #'3CQ3310Q1Q' => 'HP-2', '0x00000000' => 'LVDS' ); @@ -93,7 +99,7 @@ my %configs = ( 'on' => 1, 'width' => 1920, 'height' => 1080, - 'x' => 0, + 'x' => 1920, 'y' => 1200, 'rotate' => 0, 'waybar' => 'top' @@ -102,7 +108,7 @@ my %configs = ( 'on' => 1, 'width' => 1920, 'height' => 1080, - 'x' => 1920, + 'x' => 0, 'y' => 1200, 'rotate' => 0, 'waybar' => 'top' @@ -153,6 +159,7 @@ use strict; use warnings; my $waybar_only = 0; +my $restore = 0; # Set if no config is provided. Requires validation. my $config; if (scalar(@ARGV)) { @@ -176,12 +183,18 @@ unless (defined $config) { $config = <$fh>; close($fh); chomp $config; + $restore = 1; } # Bail if requested config doesn't exist -unless (defined $configs{$config}) { - die "$config is not a defined configuration: " - . join(', ', keys %configs) . "\n"; +if (!defined($configs{$config})) { + if ($restore) { + # If restoration is attempted with invalid config, just enable eDP-1 + $config = 'detached'; + } else { + die "$config is not a defined configuration: " + . join(', ', keys %configs) . "\n"; + } } # Write config that is to be used so that it can be recovered as default @@ -190,6 +203,7 @@ open(my $fh, '>', $last) || print $fh $config; close($fh); + # Fetch connected displays use JSON::XS; my $json = JSON::XS->new(); @@ -224,6 +238,24 @@ for (my $i = 0; $i < scalar(@$displays); $i++) { } +# Verify that all requested displays are actually available +my @unavailable; +foreach my $output (keys %$on) { + my $found = 0; + foreach my $o (keys %outputs) { + if ($outputs{$o} eq $output) { + $found = 1; + last; + } + } + unless ($found) { + push @unavailable, $output; + } +} +if (scalar(@unavailable)) { + die "Config requires unavailable output(s) " . join(', ', @unavailable) . "\n"; +} + # Skip enabling/disabling displays if only running waybar (re)start unless ($waybar_only) { # Number of simultaneous outputs is limited by gpu, so disabled displays @@ -279,9 +311,11 @@ $template =~ s/^[^\[\{]*\[(.*)\]$/$1/s; my $waybar = ''; # Configure each enabled display +my @active; foreach my $out (keys %$on) { - unless ($waybar_only) { + 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"; @@ -334,6 +368,11 @@ foreach my $out (keys %$on) { } } +open($fh, '>', $active_outputs); +print $fh join(' ', @active); +close($fh); + + # Restore array formatting $waybar = '[' . $waybar . ']'; @@ -348,7 +387,7 @@ unless ($pid) { open ($fh, '>', $tmp); print $fh $waybar; close $fh; - `nohup waybar --config=$tmp`; + `nohup waybar --config=$tmp >> waybar.log`; # Remove config unlink $tmp;