i3status-mt-json-a-optimized: added reader for mouse click events, code cleanup, ident fix
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
# vim: set ai et ts=8 sw=8 sts=8 ft=perl :
|
|
||||||
use Fcntl;
|
use Fcntl;
|
||||||
use POSIX;
|
use POSIX;
|
||||||
|
|
||||||
@@ -19,6 +19,8 @@ sub getbatterychargestr2();
|
|||||||
|
|
||||||
sub slow_thread();
|
sub slow_thread();
|
||||||
sub onesec_thread();
|
sub onesec_thread();
|
||||||
|
sub reader();
|
||||||
|
sub launcher($);
|
||||||
|
|
||||||
my @charge :shared;
|
my @charge :shared;
|
||||||
my $la :shared;
|
my $la :shared;
|
||||||
@@ -26,43 +28,61 @@ my $uptime :shared;
|
|||||||
my $time :shared;
|
my $time :shared;
|
||||||
my $shoot :shared = 1;
|
my $shoot :shared = 1;
|
||||||
|
|
||||||
my $default_color = '#3e78fd';
|
my $default_color = '#5c5b5a';
|
||||||
|
my $blue_color = '#3e78fd';
|
||||||
|
|
||||||
my $a->[0]->{'name'} = 'title';
|
my @name = qw /
|
||||||
$a->[0]->{'full_text'} = ' ';
|
ff vv ch con tel pid hc mp db kee
|
||||||
$a->[0]->{'color'} = $default_color;
|
title username uptime la powersource time /;
|
||||||
|
|
||||||
$a->[1]->{'name'} = 'username';
|
my @align = qw /
|
||||||
$a->[1]->{'full_text'} = $ENV{'USER'};
|
left left left left left left left left left left
|
||||||
$a->[1]->{'color'} = $default_color;
|
center right right right right right /;
|
||||||
|
|
||||||
$a->[2]->{'name'} = 'uptime';
|
my $a;
|
||||||
$a->[2]->{'full_text'} = ' ';
|
my %name_index;
|
||||||
$a->[2]->{'color'} = $default_color;
|
|
||||||
|
|
||||||
$a->[3]->{'name'} = 'la';
|
for (my $i = 0; $i < @name; $i++) {
|
||||||
$a->[3]->{'full_text'} = ' ';
|
$name_index{$name[$i]} = $i;
|
||||||
$a->[3]->{'color'} = $default_color;
|
$a->[$i]->{'name'} = $name[$i];
|
||||||
|
|
||||||
$a->[4]->{'name'} = 'powersource';
|
if ($name[$i] eq 'username') {
|
||||||
$a->[4]->{'full_text'} = ' ';
|
$a->[$i]->{'full_text'} = $ENV{'USER'};
|
||||||
$a->[4]->{'color'} = $default_color;
|
} elsif ($name[$i] eq 'title') {
|
||||||
|
$a->[$i]->{'min_width'} = ' ';
|
||||||
|
} else {
|
||||||
|
$a->[$i]->{'full_text'} = $name[$i];
|
||||||
|
}
|
||||||
|
|
||||||
|
$a->[$i]->{'color'} = $blue_color;
|
||||||
|
|
||||||
|
if ($align[$i] eq 'left') {
|
||||||
|
$align[$i] = "center";
|
||||||
|
$a->[$i]->{'color'} = $default_color;
|
||||||
|
$a->[$i]->{'separator_block_width'} = 1;
|
||||||
|
$a->[$i]->{'separator'} = JSON::PP::false;
|
||||||
|
$a->[$i]->{'border'} = '#3dbaf4';
|
||||||
|
$a->[$i]->{'min_width'} = sprintf(' %s ', $name[$i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$a->[$i]->{'align'} = $align[$i];
|
||||||
|
}
|
||||||
|
|
||||||
$a->[5]->{'name'} = 'time';
|
|
||||||
$a->[5]->{'full_text'} = ' ';
|
|
||||||
$a->[5]->{'color'} = $default_color;
|
|
||||||
my $title;
|
my $title;
|
||||||
my $prev_title = '';
|
my $prev_title = '';
|
||||||
|
|
||||||
threads->create('slow_thread');
|
threads->create('slow_thread');
|
||||||
threads->create('onesec_thread');
|
threads->create('onesec_thread');
|
||||||
|
threads->create('reader');
|
||||||
|
|
||||||
sleep 1;
|
sleep 1;
|
||||||
|
|
||||||
syswrite STDOUT, "{\"version\":1, \"click_events\": true}\n";
|
syswrite STDOUT, "{\"version\":1, \"click_events\": true}\n";
|
||||||
syswrite STDOUT, "[\n[],\n";
|
syswrite STDOUT, "[\n[],\n";
|
||||||
|
|
||||||
while(1) {
|
while (1) {
|
||||||
|
my $in = '';
|
||||||
|
|
||||||
$title = getfocusedwidowtitlestr();
|
$title = getfocusedwidowtitlestr();
|
||||||
|
|
||||||
if ($title ne $prev_title) {
|
if ($title ne $prev_title) {
|
||||||
@@ -71,24 +91,26 @@ while(1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($shoot == 1) {
|
if ($shoot == 1) {
|
||||||
$a->[0]->{'full_text'} = $title;
|
$a->[$name_index{'title'}]->{'full_text'} = $title;
|
||||||
$a->[2]->{'full_text'} = $uptime;
|
$a->[$name_index{'uptime'}]->{'full_text'} = $uptime;
|
||||||
$a->[3]->{'full_text'} = $la;
|
$a->[$name_index{'la'}]->{'full_text'} = $la;
|
||||||
$a->[4]->{'full_text'} = $charge[0];
|
$a->[$name_index{'powersource'}]->{'full_text'} = $charge[0];
|
||||||
$a->[4]->{'color'} = $charge[1];
|
$a->[$name_index{'powersource'}]->{'color'} = $charge[1];
|
||||||
$a->[5]->{'full_text'} = $time;
|
$a->[$name_index{'time'}]->{'full_text'} = $time;
|
||||||
my $json = JSON::PP->new;
|
my $json = JSON::PP->new;
|
||||||
$json = $json->ascii(0);
|
$json = $json->ascii(0);
|
||||||
$json = $json->utf8(0);
|
$json = $json->utf8(0);
|
||||||
$json = $json->allow_unknown (1);
|
$json = $json->allow_unknown (0);
|
||||||
$json = $json->canonical(1);
|
$json = $json->canonical(1);
|
||||||
$json = $json->space_before(0);
|
$json = $json->space_before(0);
|
||||||
$json = $json->space_after(0);
|
$json = $json->space_after(0);
|
||||||
$json = $json->indent(0);
|
$json = $json->indent(1);
|
||||||
my $str = $json->encode($a);
|
my $str = $json->encode($a);
|
||||||
undef $json;
|
undef $json;
|
||||||
|
|
||||||
|
if ((utf8::valid($str)) and utf8::decode($string)) {
|
||||||
syswrite STDOUT, $str . ",\n";
|
syswrite STDOUT, $str . ",\n";
|
||||||
|
}
|
||||||
|
|
||||||
undef $str;
|
undef $str;
|
||||||
$shoot = 0;
|
$shoot = 0;
|
||||||
@@ -98,14 +120,14 @@ while(1) {
|
|||||||
select(undef, undef, undef, 0.333);
|
select(undef, undef, undef, 0.333);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub slow_thread(){
|
sub slow_thread() {
|
||||||
threads->detach();
|
threads->detach();
|
||||||
my @prev_charge = ('','');
|
my @prev_charge = ('','');
|
||||||
my @current_charge;
|
my @current_charge;
|
||||||
my $prev_la = '';
|
my $prev_la = '';
|
||||||
my $current_la;
|
my $current_la;
|
||||||
|
|
||||||
while(1){
|
while (1) {
|
||||||
@current_charge = getbatterychargestr2();
|
@current_charge = getbatterychargestr2();
|
||||||
|
|
||||||
if ($current_charge[0] ne $prev_charge[0]) {
|
if ($current_charge[0] ne $prev_charge[0]) {
|
||||||
@@ -129,7 +151,7 @@ sub slow_thread(){
|
|||||||
sub onesec_thread() {
|
sub onesec_thread() {
|
||||||
threads->detach();
|
threads->detach();
|
||||||
|
|
||||||
while(1){
|
while (1) {
|
||||||
$uptime = getuptimestr();
|
$uptime = getuptimestr();
|
||||||
$time = gettimestr();
|
$time = gettimestr();
|
||||||
# as we report time with precision of 1 minute, update 1/60 seconds
|
# as we report time with precision of 1 minute, update 1/60 seconds
|
||||||
@@ -157,9 +179,9 @@ sub gettimestr() {
|
|||||||
sub getfocusedwidowtitlestr() {
|
sub getfocusedwidowtitlestr() {
|
||||||
my $str;
|
my $str;
|
||||||
|
|
||||||
if (-x '/usr/bin/xdotool'){
|
if (-x '/usr/bin/xdotool') {
|
||||||
$str = `xdotool getactivewindow getwindowname`;
|
$str = `xdotool getactivewindow getwindowname`;
|
||||||
}elsif (-x '/usr/bin/xprop'){
|
} elsif (-x '/usr/bin/xprop') {
|
||||||
$str = (split( /=/, `xprop -id \$(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) _NET_WM_NAME`))[1];
|
$str = (split( /=/, `xprop -id \$(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) _NET_WM_NAME`))[1];
|
||||||
chop $str; chop $str; $str = reverse $str; chop $str; chop $str; $str = reverse $str;
|
chop $str; chop $str; $str = reverse $str; chop $str; chop $str; $str = reverse $str;
|
||||||
}
|
}
|
||||||
@@ -167,8 +189,10 @@ sub getfocusedwidowtitlestr() {
|
|||||||
chomp($str);
|
chomp($str);
|
||||||
$str =~ s/[\"|\{|\}|\'|,|\(|\)|\[|\]]/ /g;
|
$str =~ s/[\"|\{|\}|\'|,|\(|\)|\[|\]]/ /g;
|
||||||
|
|
||||||
if (int(length($str)) > 64){
|
if (int(length($str)) > 24) {
|
||||||
$str = substr($str, 0, 65);
|
$str = substr($str, 0, 25);
|
||||||
|
} elsif (int(length($str)) < 1) {
|
||||||
|
$str = ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $str;
|
return $str;
|
||||||
@@ -211,7 +235,7 @@ sub getbatterychargestr1() {
|
|||||||
close A;
|
close A;
|
||||||
chomp($P);
|
chomp($P);
|
||||||
|
|
||||||
if($P eq '1'){
|
if($P eq '1') {
|
||||||
open A, "/sys/class/power_supply/BAT0/energy_full";
|
open A, "/sys/class/power_supply/BAT0/energy_full";
|
||||||
my $full_charge = <A>; chomp($full_charge); close A;
|
my $full_charge = <A>; chomp($full_charge); close A;
|
||||||
open A, "/sys/class/power_supply/BAT0/energy_now";
|
open A, "/sys/class/power_supply/BAT0/energy_now";
|
||||||
@@ -243,23 +267,23 @@ sub getbatterychargestr2() {
|
|||||||
|
|
||||||
open AC, $file or return ('??', '??%');
|
open AC, $file or return ('??', '??%');
|
||||||
|
|
||||||
if(<AC> == 1){
|
if (<AC> == 1) {
|
||||||
$str .= "AC";
|
$str .= "AC";
|
||||||
}else{
|
} else {
|
||||||
$str .= "DC";
|
$str .= "DC";
|
||||||
}
|
}
|
||||||
|
|
||||||
close AC; # display % charge only if possible
|
close AC; # display % charge only if possible
|
||||||
if (open DC,"/sys/class/power_supply/BAT0/capacity"){
|
if (open DC,"/sys/class/power_supply/BAT0/capacity") {
|
||||||
my $charge = <DC>;
|
my $charge = <DC>;
|
||||||
close DC;
|
close DC;
|
||||||
chomp($charge);
|
chomp($charge);
|
||||||
|
|
||||||
if ($charge < 10){
|
if ($charge < 10){
|
||||||
$color = '#800000';
|
$color = '#800000';
|
||||||
} elsif (($charge >= 10) and ($charge < 25)){
|
} elsif (($charge >= 10) and ($charge < 25)) {
|
||||||
$color = '#ffa500';
|
$color = '#ffa500';
|
||||||
} elsif (($charge >= 25) and ($charge < 80)){
|
} elsif (($charge >= 25) and ($charge < 80)) {
|
||||||
$color = '#008000';
|
$color = '#008000';
|
||||||
} else {
|
} else {
|
||||||
$color = '#0000a0';
|
$color = '#0000a0';
|
||||||
@@ -272,3 +296,45 @@ sub getbatterychargestr2() {
|
|||||||
|
|
||||||
return @z;
|
return @z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub reader() {
|
||||||
|
threads->detach();
|
||||||
|
my $str = '';
|
||||||
|
my $j;
|
||||||
|
|
||||||
|
while (<STDIN>) {
|
||||||
|
$str = $_;
|
||||||
|
|
||||||
|
if (substr($str, 0, 1) ne '{') {
|
||||||
|
$str = substr($str, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
chomp($str);
|
||||||
|
next if ((length($str) < 2) or (substr($str, -1, 1) ne '}'));
|
||||||
|
|
||||||
|
$j = decode_json($str);
|
||||||
|
if ($j->{'button'} eq '1') {
|
||||||
|
if ($j->{'name'} eq 'ff') { threads->create('launcher', '/usr/bin/firefox'); }
|
||||||
|
if ($j->{'name'} eq 'vv') { threads->create('launcher', '/usr/local/bin/vivaldi'); }
|
||||||
|
if ($j->{'name'} eq 'ch') { threads->create('launcher', '/usr/bin/chromium'); }
|
||||||
|
if ($j->{'name'} eq 'con') { threads->create('launcher', '/usr/bin/urxvt'); }
|
||||||
|
if ($j->{'name'} eq 'tel') { threads->create('launcher', '/home/eleksir/.local/bin/Telegram'); }
|
||||||
|
if ($j->{'name'} eq 'pid') { threads->create('launcher', '/usr/bin/pidgin'); }
|
||||||
|
if ($j->{'name'} eq 'hc') { threads->create('launcher', '/usr/bin/hexchat'); }
|
||||||
|
if ($j->{'name'} eq 'mp') { threads->create('launcher', '/usr/bin/mousepad'); }
|
||||||
|
if ($j->{'name'} eq 'db') { threads->create('launcher', '/usr/bin/deadbeef'); }
|
||||||
|
if ($j->{'name'} eq 'kee') { threads->create('launcher', '/home/eleksir/.local/bin/kee'); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub launcher($) {
|
||||||
|
my $prog = shift;
|
||||||
|
threads->detach();
|
||||||
|
|
||||||
|
if (fork() == 0) {
|
||||||
|
exec ($prog);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# vim: set ai tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab ft=perl : #
|
||||||
|
|||||||
Reference in New Issue
Block a user