1

i3status-anyevent: highlight app "buttons" with active windows, code style fixes

This commit is contained in:
Sergei Fedosov
2021-02-19 19:32:28 +03:00
parent 20789b2a2c
commit 4452e2fe0f
2 changed files with 128 additions and 29 deletions

View File

@@ -23,7 +23,7 @@
# Also we can tcp-ping some service inside vpn network to ensure that connection is not stoned # Also we can tcp-ping some service inside vpn network to ensure that connection is not stoned
"tcp_check" : { "tcp_check" : {
"enabled": 0, "enabled": 0,
"host": "somehost.com", "host": "somehost.tld",
"port": "443", "port": "443",
"timeout" : 3 "timeout" : 3
} }
@@ -36,6 +36,7 @@
"cmd" : "killall -9 fbpanel", "cmd" : "killall -9 fbpanel",
"color": "#aa0000", "color": "#aa0000",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -43,18 +44,23 @@
"full_text" : " Term ", "full_text" : " Term ",
"name" : "lilyterm", "name" : "lilyterm",
"cmd" : "lilyterm", "cmd" : "lilyterm",
"instance" : "lilyterm",
"class" : "LilyTerm",
"color" : "#666666", "color" : "#666666",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
{ {
"full_text" : " Ff ", "full_text" : " Ff ",
"name" : "firefox", "name" : "firefox",
"cmd" : "firefox", "cmd" : "firefox",
"instance" : "Navigator",
"class" : "Firefox",
"color" : "#ffa500", "color" : "#ffa500",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -62,8 +68,11 @@
"full_text" : " V ", "full_text" : " V ",
"name" : "vivaldi", "name" : "vivaldi",
"cmd" : "vivaldi", "cmd" : "vivaldi",
"instance" : "vivaldi-stable",
"class" : "Vivaldi-stable",
"color" : "#ffa500", "color" : "#ffa500",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -71,8 +80,11 @@
"full_text" : " Chr ", "full_text" : " Chr ",
"name" : "chrome", "name" : "chrome",
"cmd" : "chrome", "cmd" : "chrome",
"instance" : "google-chrome",
"class" : "Google-chrome",
"color" : "#ffa500", "color" : "#ffa500",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -80,8 +92,11 @@
"full_text" : " O ", "full_text" : " O ",
"name" : "opera", "name" : "opera",
"cmd" : "opera", "cmd" : "opera",
"instance" : "Opera",
"class" : "Opera",
"color" : "#ffa500", "color" : "#ffa500",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -89,8 +104,11 @@
"full_text" : " @ ", "full_text" : " @ ",
"name" : "thunderbird", "name" : "thunderbird",
"cmd" : "thunderbird", "cmd" : "thunderbird",
"instance" : "Mail",
"class" : "Thunderbird",
"color" : "#006994", "color" : "#006994",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -98,8 +116,11 @@
"full_text" : " Tg ", "full_text" : " Tg ",
"name" : "telegram", "name" : "telegram",
"cmd" : "Telegram", "cmd" : "Telegram",
"instance" : "Telegram",
"class" : "TelegramDesktop",
"color" : "#5a8850", "color" : "#5a8850",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -107,8 +128,11 @@
"full_text" : " M ", "full_text" : " M ",
"name" : "myteam", "name" : "myteam",
"cmd" : "myteam", "cmd" : "myteam",
"instance" : "myteam",
"class" : "myteam",
"color" : "#5a8850", "color" : "#5a8850",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -116,8 +140,11 @@
"full_text" : " S ", "full_text" : " S ",
"name" : "slack", "name" : "slack",
"cmd" : "slack", "cmd" : "slack",
"instance" : "slack",
"class" : "Slack",
"color" : "#5a8850", "color" : "#5a8850",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -125,7 +152,10 @@
"full_text" : " Txt ", "full_text" : " Txt ",
"name" : "mousepad", "name" : "mousepad",
"cmd" : "mousepad", "cmd" : "mousepad",
"instance" : "mousepad",
"class" : "Mousepad",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -133,7 +163,10 @@
"full_text" : " VSC ", "full_text" : " VSC ",
"name" : "code", "name" : "code",
"cmd" : "code", "cmd" : "code",
"instance" : "code",
"class" : "Code",
"border": "#adadad", "border": "#adadad",
"border_active": "#3e78fd",
"separator": false, "separator": false,
"separator_block_width": 2 "separator_block_width": 2
}, },
@@ -141,8 +174,11 @@
"full_text" : " K ", "full_text" : " K ",
"name" : "keepassxc", "name" : "keepassxc",
"cmd" : "keepassxc", "cmd" : "keepassxc",
"instance" : "keepassxc",
"class" : "KeePassXC",
"color" : "#7ec850", "color" : "#7ec850",
"border": "#adadad" "border" : "#adadad",
"border_active" : "#3e78fd"
} }
] ]
} }

View File

@@ -16,6 +16,7 @@ use utf8;
use open qw (:std :utf8); use open qw (:std :utf8);
use Fcntl qw (:seek); use Fcntl qw (:seek);
use AnyEvent; use AnyEvent;
use AnyEvent::I3;
use JSON::XS; use JSON::XS;
use Net::DBus; use Net::DBus;
use Encode; use Encode;
@@ -26,9 +27,12 @@ sub spawn (@);
sub find_caps_led (); sub find_caps_led ();
sub to_hours ($); sub to_hours ($);
sub readconf (); sub readconf ();
sub active_win_borders ();
my $CONF = readconf (); my $CONF = readconf ();
my $i3 = i3 ();
my $done = AnyEvent->condvar; my $done = AnyEvent->condvar;
$i3->connect->recv or die 'Unable to connect to i3';
# event pool # event pool
my @EP; my @EP;
@@ -36,6 +40,7 @@ my @EP;
# reload config and re-apply it on SIGHUP # reload config and re-apply it on SIGHUP
$SIG{HUP} = sub { $SIG{HUP} = sub {
$CONF = readconf (); $CONF = readconf ();
active_win_borders ();
$#EP = -1; $#EP = -1;
add_events (); add_events ();
}; };
@@ -68,14 +73,55 @@ my $CAPS_LED_PATH = find_caps_led ();
# for fetching battery stats via dee-boos :) # for fetching battery stats via dee-boos :)
my $properties; my $properties;
# opened windows info storage ref
my $win;
# capture click events and do not override sigstop and sigcont numbers # capture click events and do not override sigstop and sigcont numbers
syswrite STDOUT, << 'EOJSON'; syswrite STDOUT, << 'EOJSON';
{"version": 1, "click_events": true} {"version": 1, "click_events": true}
[ [], [ [],
EOJSON EOJSON
# i3 events should persist over config relads
my $i3_subscription->{window} = sub {
my $w = shift;
if (defined $w->{change}) {
if ($w->{change} eq 'new' ) {
if (defined $w->{container}->{window_properties}->{instance}) {
if (defined $win->{instance}->{$w->{container}->{window_properties}->{instance}}) {
$win->{instance}->{$w->{container}->{window_properties}->{instance}}++;
} else {
$win->{instance}->{$w->{container}->{window_properties}->{instance}} = 1;
}
}
} elsif ($w->{change} eq 'close') {
if (defined $w->{container}->{window_properties}->{instance}) {
if (defined $win->{instance}->{$w->{container}->{window_properties}->{instance}}) {
if ($win->{instance}->{$w->{container}->{window_properties}->{instance}} > 1) {
$win->{instance}->{$w->{container}->{window_properties}->{instance}} --;
} else {
delete $win->{instance}->{$w->{container}->{window_properties}->{instance}};
}
}
}
}
active_win_borders ();
}
};
$i3_subscription->{_error} = sub {
my ($msg) = @_;
die "Connection to i3 closed: $msg";
};
$i3->subscribe($i3_subscription)->recv->{success} || die 'Unable to subscribe to i3 events';
# now we can add other events
add_events (); add_events ();
warn "events added";
# and start eventloop
$done->recv (); $done->recv ();
exit 0; exit 0;
@@ -107,22 +153,22 @@ sub add_events () {
if ($CONF->{capslock}) { if ($CONF->{capslock}) {
unless ($caps_state) { unless ($caps_state) {
push @{$OUTPUT}, { "color" => "#3e78fd", "background" => "#edeceb", border => "#edeceb", "full_text" => "caps", "markup" => "pango"}; push @{$OUTPUT}, { color => '#3e78fd', background => '#edeceb', border => '#edeceb', full_text => 'caps', markup => 'pango'};
} else { } else {
push @{$OUTPUT}, { "background" => "#3e78fd", "color" => "#edeceb", border => "#edeceb", "full_text" => "caps", "markup" => "pango"}; push @{$OUTPUT}, { background => '#3e78fd', color => '#edeceb', border => '#edeceb', full_text => 'caps', markup => 'pango'};
} }
} }
if ($CONF->{cpu_temp}) { if ($CONF->{cpu_temp}) {
push @{$OUTPUT}, { "color" => "#3e78fd", "full_text" => $cputemp }; push @{$OUTPUT}, { color => '#3e78fd', full_text => $cputemp };
} }
if ($CONF->{mem}) { if ($CONF->{mem}) {
push @{$OUTPUT}, { "color" => "#3e78fd", "full_text" => $meminfo }; push @{$OUTPUT}, { color => '#3e78fd', full_text => $meminfo };
} }
if ($CONF->{la}) { if ($CONF->{la}) {
push @{$OUTPUT}, { "color" => "#3e78fd", "full_text" => "LA: $la" }; push @{$OUTPUT}, { color => '#3e78fd', full_text => "LA: $la" };
} }
my $vpn_str = 'VPN:'; my $vpn_str = 'VPN:';
@@ -148,15 +194,15 @@ sub add_events () {
} }
if ($CONF->{vpn}->{tcp_check}->{enabled} || $CONF->{vpn}->{enabled}) { if ($CONF->{vpn}->{tcp_check}->{enabled} || $CONF->{vpn}->{enabled}) {
push @{$OUTPUT}, { "color" => "#3e78fd", "full_text" => $vpn_str }; push @{$OUTPUT}, { color => '#3e78fd', full_text => $vpn_str };
} }
if ($CONF->{battery}) { if ($CONF->{battery}) {
push @{$OUTPUT}, { "color" => "#3e78fd", "full_text" => $charge }; push @{$OUTPUT}, { color => '#3e78fd', full_text => $charge };
} }
if ($CONF->{clock}) { if ($CONF->{clock}) {
push @{$OUTPUT}, { "color" => "#000000", "full_text" => "<big>$wallclock</big>", "markup" => "pango"}; push @{$OUTPUT}, { color => '#000000', full_text => "<big>$wallclock</big>", markup => 'pango'};
} }
$output_string = eval { encode_json ($OUTPUT) }; $output_string = eval { encode_json ($OUTPUT) };
@@ -313,7 +359,7 @@ sub add_events () {
open $LAHANDLE, '<', '/proc/loadavg'; open $LAHANDLE, '<', '/proc/loadavg';
} }
$la = readline ($LAHANDLE); $la = readline $LAHANDLE;
seek $LAHANDLE, SEEK_SET, 0; seek $LAHANDLE, SEEK_SET, 0;
$la = (split / /ms, $la, 2)[0]; $la = (split / /ms, $la, 2)[0];
} }
@@ -357,14 +403,14 @@ sub add_events () {
if (defined $CPUTEMPHANDLE) { if (defined $CPUTEMPHANDLE) {
if (tell $CPUTEMPHANDLE == -1) { if (tell $CPUTEMPHANDLE == -1) {
if (open ($CPUTEMPHANDLE, '<', '/sys/class/hwmon/hwmon0/temp1_input')) { if (open $CPUTEMPHANDLE, '<', '/sys/class/hwmon/hwmon0/temp1_input') {
$hwmon = 1; $hwmon = 1;
} else { } else {
$hwmon = 0; $hwmon = 0;
} }
} }
} else { } else {
open ($CPUTEMPHANDLE, '<', '/sys/class/hwmon/hwmon0/temp1_input') or do { unless (open $CPUTEMPHANDLE, '<', '/sys/class/hwmon/hwmon0/temp1_input') {
$hwmon = 0; $hwmon = 0;
}; };
} }
@@ -379,7 +425,7 @@ sub add_events () {
$cputemp = int ($cputemp / 1000); $cputemp = int ($cputemp / 1000);
} }
$cputemp = sprintf('%s°', $cputemp); $cputemp = sprintf '%s°', $cputemp;
} }
} }
}, },
@@ -444,22 +490,17 @@ sub spawn (@) {
# Parent, work done, return # Parent, work done, return
return; return;
} elsif ($pid == 0) { } elsif ($pid == 0) {
# Child, we need to exec() given prog with args
# it seems that at least vscode creashes if we close stdio handles # it seems that at least vscode creashes if we close stdio handles
close STDOUT; open STDOUT, '>', '/dev/null';
close STDERR; open STDERR, '>', '/dev/null';
close STDIN; open STDIN, '<', '/dev/null';
# open STDOUT, '>', '/dev/null';
# open STDERR, '>', '/dev/null';
# open STDIN, '<', '/dev/null';
$pid = fork (); $pid = fork ();
if ($pid) { # Parent if ($pid) { # Parent
# in this place parent quits # in this place parent quits
exit 0; exit 0;
} elsif ($pid == 0) { # Child } elsif ($pid == 0) { # Child
my $prg = shift (@args); my $prg = shift @args;
# weird situation, silently quit here :) # weird situation, silently quit here :)
exit 0 unless (defined $prg); exit 0 unless (defined $prg);
@@ -509,8 +550,12 @@ sub to_hours ($) {
} }
sub readconf () { sub readconf () {
my $file = "$ENV{HOME}/.i3/config.json"; my $file = "$ENV{HOME}/.i3/config.json";
open (my $FH, '<', $file) or die "Unable to open $file: $!\n";
unless (open my $FH, '<', $file) {
die "Unable to open $file: $!\n";
}
my $len = (stat $file)[7]; my $len = (stat $file)[7];
my $data; my $data;
use bytes; use bytes;
@@ -528,9 +573,27 @@ sub readconf () {
my $config = eval { JSON::XS->new->utf8->relaxed->decode ($data) }; my $config = eval { JSON::XS->new->utf8->relaxed->decode ($data) };
if ($@) { unless ($config) {
die "Unable to decode $file: $@\n"; die "Unable to decode $file: $@\n";
} }
return $config; return $config;
} }
sub active_win_borders () {
foreach my $app (@{$CONF->{apps}}) {
unless (defined $app->{border_inactive}) {
$app->{border_inactive} = $app->{border};
}
unless (defined $app->{border_active}) {
$app->{border_active} = $app->{border};
}
if (defined ($app->{instance}) && defined ($win->{instance}->{$app->{instance}})) {
$app->{border} = $app->{border_active};
} else {
$app->{border} = $app->{border_inactive};
}
}
}