Compare commits

...

4 Commits

Author SHA1 Message Date
John Mertz 895b9b5588 Thinkpad control scripts
SystemD unit file to give ownership or necessary sys files to user

toggle_outputs.sh simply swaps between two most common layouts with
sway/displays.pl

kbd_backlight.pl simply increments the current backlight brightness then
%3 to cycle between off, low, high

blc.pl is an overly complicated screen backlight control
2020-11-15 23:55:20 -05:00
John Mertz dd6a04088f 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.
2020-11-15 23:47:18 -05:00
John Mertz 7f41003cdc Updated for WireGuard, prevented reloading waybar on abort 2020-11-15 23:46:04 -05:00
John Mertz c1d609d8a2 Change to wireguard IP 2020-11-13 20:59:45 -05:00
7 changed files with 460 additions and 62 deletions

View File

@ -7,23 +7,25 @@ res=$(echo "Connection|John.Me.tz|MailCleaner|Disconnect|Restart" | rofi -sep "|
if [ $res = "Connection" ]; then
/usr/bin/uxterm -e 'sudo /usr/bin/nmtui'
elif [ $res = "John.Me.tz" ]; then
sudo /usr/bin/systemctl stop openvpn-client@*
sudo /usr/bin/systemctl start openvpn-client@john.me.tz
sudo /usr/bin/systemctl stop openvpn-client@mailcleaner
sudo /usr/bin/systemctl restart wg-quick@wg0
elif [ $res = "MailCleaner" ]; then
sudo /usr/bin/systemctl stop openvpn-client@*
sudo /usr/bin/systemctl start openvpn-client@mailcleaner
sudo /usr/bin/systemctl stop wg-quick@wg0
sudo /usr/bin/systemctl restart openvpn-client@mailcleaner
elif [ $res = "Disconnect" ]; then
sudo /usr/bin/systemctl stop openvpn-client@*
sudo /usr/bin/systemctl stop openvpn-client@mailcleaner
sudo /usr/bin/systemctl stop wg-quick@wg0
elif [ $res = "Restart" ]; then
sudo /usr/bin/systemctl restart openvpn-client@*
if [ "`ip addr show wg0 2> /dev/null`" != "" ]; then
sudo /usr/bin/systemctl restart wg-quick@wg0
fi
if [ "`ip addr show tun0 2> /dev/null`" != "" ]; then
sudo /usr/bin/systemctl restart openvpn-client@mailcleaner
fi
else
exit
fi
exit
# Waybar sometimes doesn't update with the VPN IP, for whatever reason. Restart it.
# exits above because I change outputs more often than I change VPNs
if [ "$(pgrep -c waybar)" -gt 0 ]; then
sleep 2s
pkill waybar
waybar
fi
/home/jpm/scripts/sway/displays.pl -w

View File

@ -1,7 +1,7 @@
#!/bin/bash
# Required settings
host=10.8.0.66
host=10.10.0.66
port=8080
# Optional login for Kodi

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,46 +298,45 @@ 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 .= $_;
}
close $fh;
chomp $waybar;
# Replace basic preferences
$waybar =~ s/__OUTPUT__/"$on->{$out}->{output}"/;
$waybar =~ s/__POSITION__/"$on->{$out}->{waybar}"/;
if (defined $on->{$out}->{width}) {
$waybar =~ s/__WIDTH__/$on->{$out}->{width}/;
# If width is not set, comment that line out to use default
} else {
$waybar =~ s/([^\s]*\s*)__WIDTH__/\/\/ $1__WIDTH__/gg;
}
# Write config to a temporary file
my $tmp = $waybar_temporary . "/" . $on->{$out}->{output} . ".tmp";
open ($fh, '>', $tmp);
print $fh $waybar;
close $fh;
`nohup waybar --config=$tmp`;
# Remove config
unlink $tmp;
# If there's already a display set up, add a comma
unless ($waybar eq '') {
$waybar .= ',';
}
$waybar .= $template;
# Replace basic preferences
$waybar =~ s/__OUTPUT__/"$on->{$out}->{output}"/;
$waybar =~ s/__POSITION__/"$on->{$out}->{waybar}"/;
if (defined $on->{$out}->{width}) {
$waybar =~ s/__WIDTH__/$on->{$out}->{width}/;
# If width is not set, comment that line out to use default
} else {
$waybar =~ s/([^\s]*\s*)__WIDTH__/\/\/ $1__WIDTH__/gg;
}
}
}
open(my $fh, '>', $last);
print $fh $config;
close($fh);
# 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 . "/waybar-" . time() .".config";
open ($fh, '>', $tmp);
print $fh $waybar;
close $fh;
`nohup waybar --config=$tmp`;
# Remove config
unlink $tmp;
}

305
thinkpad/blc.pl Executable file
View File

@ -0,0 +1,305 @@
#!/usr/bin/perl -w
# Files containing current and max brightness values
my $cur_file = "/sys/class/backlight/intel_backlight/brightness";
my $max_file = "/sys/class/backlight/intel_backlight/max_brightness";
my $last_file = "/home/jpm/.config/blc.last";
sub to_percent {
my $value = shift;
if ($value eq "Permission Denied") {
return $value;
} else {
$value = int($value/get_max()*100);
return $value . '%';
}
}
sub get_offset {
return int(get_max()/100);
}
sub get_current {
open(my $c,'<',"$cur_file");
my $current = <$c>;
close $c;
chomp $current;
return $current;
}
sub get_max {
open(my $m,'<',"$max_file");
my $max = <$m>;
close $m;
chomp $max;
return $max;
}
sub get_min {
return int((get_max()/100)+2);
}
sub writable {
if (! -w $cur_file) {
return 0;
} else {
return 1;
}
}
sub increment {
if (writable()) {
my $current = get_current();
my $max = get_max();
my $target = $current+get_offset();
if ($target > $max) {
$target = $max;
}
open(my $c,'>',"$cur_file");
print $c $target;
close $c;
return $target;
} else {
return "Permission Denied";
}
}
sub decrement {
if (writable()) {
my $current = get_current();
my $min = get_min();
my $target = $current-get_offset();
if ($target < $min) {
$target = $min;
}
open(my $c,'>',"$cur_file");
print $c $target;
close $c;
return $target;
} else {
return "Permission Denied";
}
}
sub set {
my $value = shift;
if (writable()) {
$current = get_current();
if ($value > $current) {
for (my $i=$current;$i<=$value;$i++) {
open(my $c,'>',"$cur_file");
print $c $i;
close $c;
}
} else {
for (my $i=$current;$i>=$value;$i--) {
open(my $c,'>',"$cur_file");
print $c $i;
close $c;
}
}
return $value;
} else {
return "Permission Denied";
}
}
sub help {
print "
Backlight Control
Usage: blc.pl [--silent|--notify] [OPTION] [VALUE]
With no option the backlight information is printed as JSON.
Output printed to STDOUT unless:
--silent Do not display output (overrides --notify)
--notify Send output to notification daemon. --notify=N will
change the display duration in ms. Default is 200ms
Output option must preceed the first Action option.
Actions:
= VALUE Set backlight to specific value. VALUE greater than
100 will be treated as absolute value. VALUE eqaul to
or less than 100 will be treated as a percentage
++ Increment by 1%
+= VALUE Increment by VALUE percent
-- Decrement by 1%
-= VALUE Decrement by VALUE percent
Actions corrected to 1% or 100% if over or under. All actions
provide output as a percentage, with the % symbol.
Final percentage printed as below, skipping other options.
Print:
== Print current absolute value
^ Print maximum absolute value
% Print current percentage (does not include % symbol)
--help Help (not impacted by --silent or --notify)
--HELP Advanced help (same as above)
Any other option will be printed literally";
}
sub advanced {
$current = get_current();
$max = get_max();
print ".
Print functions can be strung together but command will exit
with the first non-print option. eg.
\$ blc.pl == / ^
21/100
\$ blc.pl == ++
('==' ignored)
(Backlight incremented)
Escape options with \\ in quotes to display them literally. eg.
\$ blc.pl == / ^ '\\=' % \'\\%\'
" . $current . "/" . $max . "=" . int($current/$max*100) . "%
Only one \'\\' is removed per block. eg.
\$ blc.pl '\\% \\'
% \\
";
}
my $current = get_current();
my (@output, $target, $silent, $notify);
if (scalar @ARGV) {
for (my $i=0;$i<scalar @ARGV;$i++) {
if ($ARGV[$i] eq '++') {
$target = increment();
@output = to_percent($target);
last;
} elsif ($ARGV[$i] eq '+=') {
for (my $i=0;$i<scalar @ARGV;$i++) {
if ($ARGV[$i] eq '+=') {
if (defined $ARGV[($i+1)]) {
$count = $ARGV[($i+1)];
for (my $j=0;$j<$count;$j++) {
$target = increment();
if ($target eq "Permission Denied") {
last;
}
}
@output = to_percent($target);
} else {
@output = ("No value after $ARGV[$i]");
}
last;
}
}
last;
} elsif ($ARGV[$i] eq '--') {
$target = decrement();
@output = to_percent($target);
last;
} elsif ($ARGV[$i] eq '-=') {
for (my $i=0;$i<scalar @ARGV;$i++) {
if ($ARGV[$i] eq '-=') {
if (defined $ARGV[($i+1)]) {
$count = $ARGV[($i+1)];
for (my $j=0;$j<$count;$j++) {
$target = decrement();
if ($target eq "Permission Denied") {
last;
}
}
@output = to_percent($target);
} else {
@output = ("No value after $ARGV[$i]");
}
last;
}
}
last;
} elsif ($ARGV[$i] eq '=') {
$target = $ARGV[($i+1)];
if (defined $target) {
if ($target < 1) {
$target = set(get_min());
} elsif ($target > get_max()) {
$target = set(get_max());
} elsif ($target > 100) {
$target = set($target);
} else {
$target = set(int((get_max()*$target/100)+1));
}
if ($target eq "Permission Denied") {
@output = $target;
} else {
@output = to_percent($target);
}
last;
} else {
@output = ("No value after $ARGV[$i]");
last;
}
} elsif ($ARGV[$i] eq '%') {
push @output,int(get_current()/get_max()*100);
} elsif ($ARGV[$i] eq '^') {
push @output,get_max();
} elsif ($ARGV[$i] eq '==') {
push @output,get_current();
} elsif ($ARGV[$i] eq '--help') {
help();
print " (see --HELP).\n\n";
exit();
} elsif ($ARGV[$i] eq '--HELP') {
help();
advanced();
exit();
} elsif ($ARGV[$i] eq '--silent') {
$silent = 'TRUE';
} elsif ($ARGV[$i] =~ /^--notify/) {
$notify = 'TRUE';
if ($ARGV[$i] =~ /=[0-9]+$/) {
$duration = $ARGV[$i];
$duration =~ s/.*=([0-9]+)/$1/;
} else {
$duration = 200;
}
} else {
my $add = $ARGV[$i];
$add =~ s/\\//;
push @output,$add;
}
}
} else {
@output = ('{"Backlight":{"Max":"' . get_max() . '","Current":"' . get_current() . '","Percentage","' . int(get_current()/get_max()*100) . '%"}}');
}
open(my $fh,'>',$last_file);
print $fh get_current();
close($fh);
if ($silent) {
exit();
} elsif ($notify) {
# Don't output anything if the value didn't change
if ($current == get_current()) {
exit();
}
my $concat;
foreach (@output) {
$concat .= $_;
}
system "notify-send --urgency=normal -i /usr/share/icons/Papirus-Dark-Grey/48x48/status/notification-display-brightness.svg -t $duration \"$concat\"";
exit();
} else {
print foreach @output;
print "\n";
exit();
}

39
thinkpad/kbd_backlight.pl Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/perl
use strict;
use warnings;
my $maxfile = "/sys/class/leds/tpacpi\:\:kbd_backlight/max_brightness";
my $current = "/sys/class/leds/tpacpi\:\:kbd_backlight/brightness";
my ($max, $now, $new);
if (open(my $m, '<', $maxfile)) {
$max = readline($m);
chomp $max;
close($m);
} else {
print "Failed to read $maxfile\n";
exit;
}
print STDERR "max = $max\n";
if (open(my $c, '<', $current)) {
$now = readline($c);
chomp $now;
close($c);
} else {
print "Failed to read $current\n";
exit;
}
print STDERR "now = $now\n";
if (open(my $fh, '>', $current)) {
$new = (($now+1) % ($max+1));
chomp $new;
print $fh $new;
close($fh);
} else {
print "Failed to write $current\n";
exit;
}
print STDERR "new = $new\n";

9
thinkpad/toggle_outputs.sh Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
CURRENT=`cat /home/jpm/.config/last_display`
if [ "$CURRENT" == "detached" ]; then
/home/jpm/scripts/sway/displays.pl stacked
else
/home/jpm/scripts/sway/displays.pl detached
fi

View File

@ -0,0 +1,8 @@
[Unit]
Description=Give ownership of backlight to %I
[Service]
ExecStart=chown %i:%i /sys/class/backlight/intel_backlight/brightness /sys/class/leds/tpacpi::kbd_backlight/brightness
[Install]
WantedBy=multi-user.target