Compare commits
28 Commits
b6d1722b25
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc58f0c6a9 | ||
|
|
044acbf148 | ||
|
|
b36eaf7e40 | ||
|
|
04fdac8891 | ||
|
|
035afae865 | ||
|
|
58d896c597 | ||
|
|
d1fcd2b8a4 | ||
|
|
27904b8d50 | ||
|
|
7d4e8dccb4 | ||
|
|
0d2453623c | ||
|
|
139b014f7b | ||
|
|
7a270291b1 | ||
|
|
b287c84eed | ||
|
|
02bb1399fb | ||
|
|
8fc99eeb50 | ||
|
|
fbb3fbdb07 | ||
|
|
ccf477bc65 | ||
|
|
217a86ecff | ||
|
|
3a09fcf22f | ||
|
|
1cb4103dd6 | ||
|
|
bb41416926 | ||
|
|
4452e2fe0f | ||
|
|
20789b2a2c | ||
|
|
949ce2f0c7 | ||
|
|
96fc306ef7 | ||
|
|
a102651d7a | ||
|
|
4fbb5170d2 | ||
|
|
169f4ef309 |
114
acpid.pl
114
acpid.pl
@@ -2,15 +2,10 @@
|
|||||||
#
|
#
|
||||||
# Hibernates system if battery charge drops under $MIN_CHARGE and battery discharging
|
# Hibernates system if battery charge drops under $MIN_CHARGE and battery discharging
|
||||||
|
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings "all";
|
use warnings;
|
||||||
use Fcntl;
|
|
||||||
|
|
||||||
sub logger($); # logs messages if $debug != 0
|
|
||||||
# maybe one time i'll use it
|
|
||||||
sub i3wmstate; # returns 1 if i3wm running, else - 0
|
|
||||||
|
|
||||||
|
sub logger; # logs messages if $debug != 0
|
||||||
|
|
||||||
$0 = 'acpid.pl';
|
$0 = 'acpid.pl';
|
||||||
my $debug = 0;
|
my $debug = 0;
|
||||||
@@ -18,8 +13,8 @@ my $MIN_CHARGE = 10;
|
|||||||
|
|
||||||
my $base = '/sys/class/power_supply/BAT0';
|
my $base = '/sys/class/power_supply/BAT0';
|
||||||
|
|
||||||
unless(-f "$base/present") {
|
unless (-f "$base/present") {
|
||||||
logger("Battery info at /sys/class/BAT0/present not found, quitting.\n");
|
logger "Battery info at /sys/class/BAT0/present not found, quitting.";
|
||||||
exit 0;
|
exit 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,90 +22,89 @@ my $battery_present = 2;
|
|||||||
my $battery_check = 2;
|
my $battery_check = 2;
|
||||||
my $charging = 2;
|
my $charging = 2;
|
||||||
|
|
||||||
while ( sleep(180) ) {
|
while (sleep 180) {
|
||||||
# check that battery is in place
|
open (my $batteryFH, '<', "$base/present") or next;
|
||||||
open(ACPI, "$base/present") or next;
|
my $buf;
|
||||||
my $buf = '';
|
|
||||||
|
|
||||||
if (read(ACPI, $buf, 1)) {
|
if (read ($batteryFH, $buf, 1)) {
|
||||||
if ($battery_check == 0) {
|
if ($battery_check == 0) {
|
||||||
logger("Ability to read from /sys/class/BAT0/present restored\n");
|
logger "Ability to read from $base/present restored";
|
||||||
$battery_check = 1;
|
$battery_check = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($buf == '0') {
|
if ($buf == '0') {
|
||||||
if ($battery_present != 0) {
|
if ($battery_present) {
|
||||||
logger("We have no battery, skipping charge level polling until it appears\n");
|
logger "We have no battery, skipping charge level polling until it appears";
|
||||||
$battery_present = 0;
|
$battery_present = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
close ACPI;
|
close $batteryFH;
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($battery_check != 0) {
|
if ($battery_check) {
|
||||||
logger("Unable to read from /sys/class/BAT0/present\n");
|
logger "Unable to read from /sys/class/BAT0/present";
|
||||||
$battery_check = 0;
|
$battery_check = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
close ACPI;
|
close $batteryFH;
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
|
|
||||||
close ACPI;
|
close $batteryFH;
|
||||||
$battery_present = 1;
|
$battery_present = 1;
|
||||||
|
|
||||||
# check status, we have no need to poll battery if it charging/charged
|
# check status, we have no need to poll battery if it charging/charged
|
||||||
unless (-f "$base/status") {
|
unless (-f "$base/status") {
|
||||||
logger("Something broken in /sys, unable to find $base/status, quitting\n");
|
logger "Something broken in /sys, unable to find $base/status, quitting";
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: file access save state and print on change
|
# TODO: file access save state and print on change
|
||||||
open(STATUS, "$base/status") or do {
|
open (my $statusFH, '<', "$base/status") or do {
|
||||||
logger("Unable to open $base/status\n");
|
logger "Unable to open $base/status";
|
||||||
next;
|
next;
|
||||||
};
|
};
|
||||||
|
|
||||||
$buf = <STATUS>;
|
$buf = <$statusFH>;
|
||||||
close STATUS;
|
close $statusFH;
|
||||||
|
chomp $buf;
|
||||||
chomp($buf);
|
|
||||||
|
|
||||||
if ($buf ne 'Discharging') {
|
if ($buf ne 'Discharging') {
|
||||||
if ($charging != 1) {
|
if ($charging != 1) {
|
||||||
logger("Battery charging.\n");
|
logger "Battery charging.";
|
||||||
$charging = 1;
|
$charging = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($charging != 0) {
|
if ($charging) {
|
||||||
logger("Battery discharging.\n");
|
logger "Battery discharging.";
|
||||||
$charging = 0;
|
$charging = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# read charge state
|
# read charge state
|
||||||
unless (-f "$base/capacity") {
|
unless (-f "$base/capacity") {
|
||||||
logger("Something broken in /sys, unable to find $base/capacity, quitting\n");
|
logger "Something broken in /sys, unable to find $base/capacity, quitting";
|
||||||
exit 1;
|
exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: file access save state and print on change
|
# TODO: file access save state and print on change
|
||||||
open(CHARGE, "$base/capacity") or do {
|
open (my $chargeFH, '<', "$base/capacity") or do {
|
||||||
logger("Unable to open $base/capacity\n");
|
logger("Unable to open $base/capacity\n");
|
||||||
next;
|
next;
|
||||||
};
|
};
|
||||||
|
|
||||||
read(CHARGE, $buf, 3) or do {
|
read ($chargeFH, $buf, 3) or do {
|
||||||
logger("Unable to read $base/capacity, but file is in place\n");
|
logger "Unable to read $base/capacity, but file is in place";
|
||||||
next;
|
next;
|
||||||
};
|
};
|
||||||
|
|
||||||
chomp($buf); # just in case
|
close $chargeFH;
|
||||||
|
chomp $buf; # just in case
|
||||||
|
|
||||||
if ($buf < $MIN_CHARGE) {
|
if ($buf < $MIN_CHARGE) {
|
||||||
if ($charging != 0) { # discharging
|
if ($charging != 0) { # discharging
|
||||||
logger("Battery charge is less than 10%, hibernating\n");
|
logger "Battery charge is less than 10%, hibernating";
|
||||||
`dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate`;
|
`dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate`;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -122,51 +116,15 @@ while ( sleep(180) ) {
|
|||||||
`notify-send --urgency=normal --expire-time=3000 "Battery overcharging"`;
|
`notify-send --urgency=normal --expire-time=3000 "Battery overcharging"`;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger("Charge is $buf%\n");
|
logger "Charge is $buf%";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
sub logger ($) {
|
sub logger {
|
||||||
if ($debug != 0) {
|
if ($debug) {
|
||||||
my $msg = shift;
|
my $msg = shift;
|
||||||
$msg = localtime() . ' ' . $msg;
|
$msg = sprintf '%s %s\n', localtime (), $msg;
|
||||||
syswrite STDOUT, $msg;
|
syswrite STDOUT, $msg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub i3wmstate {
|
|
||||||
my $flag = 0;
|
|
||||||
opendir(P, '/proc') || die 'no /proc found!';
|
|
||||||
|
|
||||||
while (readdir(P)) {
|
|
||||||
my $pid = $_;
|
|
||||||
next unless(defined($pid));
|
|
||||||
next if ($pid eq '');
|
|
||||||
|
|
||||||
if (-d "/proc/$pid") {
|
|
||||||
# dir UID == APCID.pl process user owner
|
|
||||||
if ((stat("/proc/$pid"))[4] == int(getpwnam($ENV{'LOGNAME'}))) {
|
|
||||||
next unless(-r "/proc/$pid/cmdline");
|
|
||||||
open(CMDLINE, "/proc/$pid/cmdline") || die "unable to open /proc/$pid/cmdline";
|
|
||||||
my $cl = <CMDLINE>;
|
|
||||||
close(CMDLINE);
|
|
||||||
|
|
||||||
if(defined($cl)) {
|
|
||||||
if ((substr($cl, 0, 2) eq 'i3') and (length($cl) <= 4)) {
|
|
||||||
$flag = 1;
|
|
||||||
undef $cl;
|
|
||||||
undef $pid;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
undef $cl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
undef $pid;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $flag;
|
|
||||||
}
|
|
||||||
|
|||||||
72
config.json
72
config.json
@@ -1,72 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"full_text" : "u",
|
|
||||||
"name" : "urxvt",
|
|
||||||
"cmd" : "urxvtc"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "l",
|
|
||||||
"name" : "lxterm",
|
|
||||||
"cmd" : "lxterminal"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "f",
|
|
||||||
"name" : "firefox",
|
|
||||||
"cmd" : "firefox"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "v",
|
|
||||||
"name" : "vivaldi",
|
|
||||||
"cmd" : "vivaldi"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "c",
|
|
||||||
"name" : "chromium",
|
|
||||||
"cmd" : "chromium"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "o",
|
|
||||||
"name" : "opera",
|
|
||||||
"cmd" : "opera"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "t",
|
|
||||||
"name" : "thunderbird",
|
|
||||||
"cmd" : "thunderbird"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "t",
|
|
||||||
"name" : "telegram",
|
|
||||||
"cmd" : "Telegram"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "i",
|
|
||||||
"name" : "icq",
|
|
||||||
"cmd" : "icq"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "s",
|
|
||||||
"name" : "slack",
|
|
||||||
"cmd" : "slack"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "z",
|
|
||||||
"name" : "zim",
|
|
||||||
"cmd" : "zim"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "m",
|
|
||||||
"name" : "mousepad",
|
|
||||||
"cmd" : "mousepad"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "c",
|
|
||||||
"name" : "code",
|
|
||||||
"cmd" : "code"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"full_text" : "k",
|
|
||||||
"name" : "keepassxc",
|
|
||||||
"cmd" : "keepassxc"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -1,22 +1,26 @@
|
|||||||
#!/usr/bin/perl
|
#!/usr/bin/perl
|
||||||
# vim: set ai noet ts=4 sw=4 sts=4 ft=perl :
|
# vim: set ai noet ts=4 sw=4 sts=4 ft=perl :
|
||||||
|
|
||||||
use strict;
|
|
||||||
use warnings "all";
|
|
||||||
use Fcntl qw(:seek :mode :DEFAULT);
|
|
||||||
use 5.10.0;
|
|
||||||
use utf8;
|
use utf8;
|
||||||
|
use open qw (:std :utf8);
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use Carp qw (confess);
|
||||||
|
use Data::Dumper;
|
||||||
|
use Fcntl qw(:seek :mode :DEFAULT);
|
||||||
|
use 5.22.2;
|
||||||
use JSON::XS;
|
use JSON::XS;
|
||||||
|
use Net::DBus;
|
||||||
|
use POSIX qw (setsid);
|
||||||
use threads;
|
use threads;
|
||||||
|
|
||||||
binmode STDOUT;
|
|
||||||
binmode STDERR;
|
|
||||||
|
|
||||||
sub input_thread();
|
sub input_thread();
|
||||||
sub readconf();
|
sub readconf();
|
||||||
sub gettimestr();
|
sub gettimestr();
|
||||||
sub getlastr();
|
sub getlastr();
|
||||||
sub getbatterychargestr2();
|
sub getbatterychargestr2();
|
||||||
|
sub getbatterychargestr3();
|
||||||
|
sub to_hours ($);
|
||||||
sub meminfo();
|
sub meminfo();
|
||||||
sub ifaces();
|
sub ifaces();
|
||||||
sub cpuspeed();
|
sub cpuspeed();
|
||||||
@@ -26,9 +30,18 @@ sub caps_find();
|
|||||||
|
|
||||||
sub spawn(@);
|
sub spawn(@);
|
||||||
|
|
||||||
|
binmode STDIN;
|
||||||
|
binmode STDERR;
|
||||||
|
binmode STDOUT;
|
||||||
|
|
||||||
my $read = 0;
|
my $read = 0;
|
||||||
|
$SIG{__DIE__} = \&Carp::confess;;
|
||||||
$SIG{"CONT"} = sub { $read = 1 };
|
$SIG{"CONT"} = sub { $read = 1 };
|
||||||
$SIG{"STOP"} = sub { $read = 0 };
|
$SIG{"STOP"} = sub { $read = 0 };
|
||||||
|
$SIG{"PIPE"} = sub { warn "SIGPIPE" };
|
||||||
|
$SIG{"HUP"} = sub { warn "SIGHUP" };
|
||||||
|
$SIG{"USR1"} = sub { warn "SIGUSR1" };
|
||||||
|
$SIG{"WINCH"} = sub { warn "SIGWINCH" };
|
||||||
|
|
||||||
my $slow_timer = 360; # 0.333 * 360 ~= 2 min
|
my $slow_timer = 360; # 0.333 * 360 ~= 2 min
|
||||||
my $onesec_timer = 3; # 0.333 * 3 ~= 1 sec
|
my $onesec_timer = 3; # 0.333 * 3 ~= 1 sec
|
||||||
@@ -83,14 +96,20 @@ if ( -f "/sys/class/power_supply/ADP1/online") {
|
|||||||
$ACFILE = "/sys/class/power_supply/AC/online";
|
$ACFILE = "/sys/class/power_supply/AC/online";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# prep dee-boos connection
|
||||||
|
my $bus = Net::DBus->system;
|
||||||
|
my $upower = $bus->get_service("org.freedesktop.UPower");
|
||||||
|
my $properties = $upower->get_object("/org/freedesktop/UPower/devices/battery_BAT0", "org.freedesktop.DBus.Properties");
|
||||||
|
|
||||||
threads->create('input_thread')->detach();
|
threads->create('input_thread')->detach();
|
||||||
|
|
||||||
sleep 1;
|
# capture click events and do not override sigstop and sigcont numbers
|
||||||
|
syswrite STDOUT, '{"version": 1, "click_events": true}
|
||||||
syswrite STDOUT, '{"version": 1, "stop_signal": 10, "cont_signal": 12, "click_events": true}
|
|
||||||
[ [],
|
[ [],
|
||||||
';
|
';
|
||||||
|
|
||||||
|
sleep 1;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
# json message that will be spewed to i3bar
|
# json message that will be spewed to i3bar
|
||||||
my $je;
|
my $je;
|
||||||
@@ -126,7 +145,7 @@ while (1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($slow_timer >= 360) {
|
if ($slow_timer >= 360) {
|
||||||
$tmp = getbatterychargestr2();
|
$tmp = getbatterychargestr3();
|
||||||
if ($charge ne $tmp) {
|
if ($charge ne $tmp) {
|
||||||
$jf = 1;
|
$jf = 1;
|
||||||
$charge = $tmp;
|
$charge = $tmp;
|
||||||
@@ -166,8 +185,10 @@ while (1) {
|
|||||||
@{$je} = (@{$jo}, @{$C});
|
@{$je} = (@{$jo}, @{$C});
|
||||||
@{$je} = reverse(@{$je});
|
@{$je} = reverse(@{$je});
|
||||||
|
|
||||||
if (my $output = encode_json($je)) {
|
my $output = eval { encode_json($je); };
|
||||||
syswrite STDOUT, encode_json($je) . ",\n";
|
|
||||||
|
if ($output) {
|
||||||
|
syswrite STDOUT, $output . ",\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
$jf = 0;
|
$jf = 0;
|
||||||
@@ -181,13 +202,18 @@ while (1) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub readconf() {
|
sub readconf() {
|
||||||
open (CONF, "$ENV{HOME}/.i3/config.json") or die "Unable to open $ENV{HOME}/.i3/config.json: $!\n";
|
open (my $CONF, '<:encoding(UTF-8)', "$ENV{HOME}/.i3/config.json") or die "Unable to open $ENV{HOME}/.i3/config.json: $!\n";
|
||||||
my $sep = $/;
|
my $len = (stat "$ENV{HOME}/.i3/config.json")[7];
|
||||||
$/ = '';
|
use bytes;
|
||||||
my $c = readline(*CONF);
|
read ($CONF, my $c, $len) or die "unable to read $ENV{HOME}/.i3/config.json";
|
||||||
close CONF;
|
no bytes;
|
||||||
$/ = $sep;
|
close $CONF;
|
||||||
$c = decode_json($c) or die "Unable to decode $ENV{HOME}/.i3/config.json: $!\n";
|
$c = eval { decode_json ($c); };
|
||||||
|
|
||||||
|
if ($@) {
|
||||||
|
die "Unable to decode $ENV{HOME}/.i3/config.json: $@\n";
|
||||||
|
}
|
||||||
|
|
||||||
my ($j, %cmd);
|
my ($j, %cmd);
|
||||||
my $cnt = 0;
|
my $cnt = 0;
|
||||||
|
|
||||||
@@ -210,11 +236,12 @@ sub input_thread() {
|
|||||||
# elements as independed json messages, but we have to strip leading ","
|
# elements as independed json messages, but we have to strip leading ","
|
||||||
|
|
||||||
readline(*STDIN); # throw away leading "[\n"
|
readline(*STDIN); # throw away leading "[\n"
|
||||||
|
my $char;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
next if (eof(STDIN));
|
my $msg = eval { readline(*STDIN) };
|
||||||
my $msg = readline(*STDIN);
|
next unless ($msg);
|
||||||
$msg = substr($msg, 1, -1) if (substr($msg, 0, 1) eq ','); # drop leading ',' if present
|
$msg = substr($msg, 1) if (substr($msg, 0, 1) eq ','); # drop leading ',' if present
|
||||||
my $j = decode_json($msg);
|
my $j = decode_json($msg);
|
||||||
|
|
||||||
if (defined($CMD{$j->{name}})) {
|
if (defined($CMD{$j->{name}})) {
|
||||||
@@ -276,6 +303,42 @@ sub getbatterychargestr2() {
|
|||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub getbatterychargestr3() {
|
||||||
|
my $capacity = $properties->Get('org.freedesktop.UPower.Device', 'Percentage');
|
||||||
|
my $state = $properties->Get('org.freedesktop.UPower.Device', 'State');
|
||||||
|
my $time = "?";
|
||||||
|
|
||||||
|
if ($state == 2) {
|
||||||
|
# discharging
|
||||||
|
$time = $properties->Get('org.freedesktop.UPower.Device', 'TimeToEmpty');
|
||||||
|
# time in seconds, let's translate it to hours:minutes
|
||||||
|
$state = '▼';
|
||||||
|
} elsif ($state == 1) {
|
||||||
|
# charging
|
||||||
|
$time = $properties->Get('org.freedesktop.UPower.Device', 'TimeToFull');
|
||||||
|
$state = '▲';
|
||||||
|
} else {
|
||||||
|
# already charged
|
||||||
|
$time = 0;
|
||||||
|
$state = '•';
|
||||||
|
}
|
||||||
|
|
||||||
|
$time = to_hours ($time) unless ($time eq '?');
|
||||||
|
|
||||||
|
return sprintf("⚡ %s%% %s %s", $capacity, $state, $time);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub to_hours ($) {
|
||||||
|
my $sec = shift;
|
||||||
|
|
||||||
|
my $hrs = $sec / (60 * 60);
|
||||||
|
$hrs = 0 if ( $hrs < 1 );
|
||||||
|
my $min = ($sec - ($hrs * 60 * 60)) / 60;
|
||||||
|
$min = 0 if ( $min < 1 );
|
||||||
|
|
||||||
|
return sprintf("%02d:%02d", $hrs, $min);
|
||||||
|
}
|
||||||
|
|
||||||
sub meminfo() {
|
sub meminfo() {
|
||||||
if (defined($MEMINFOHANDLE)) {
|
if (defined($MEMINFOHANDLE)) {
|
||||||
open ($MEMINFOHANDLE, "/proc/meminfo") if (tell($MEMINFOHANDLE) == -1);
|
open ($MEMINFOHANDLE, "/proc/meminfo") if (tell($MEMINFOHANDLE) == -1);
|
||||||
@@ -507,19 +570,29 @@ sub spawn (@) {
|
|||||||
if ($pid) { # Parent
|
if ($pid) { # Parent
|
||||||
return;
|
return;
|
||||||
} elsif ($pid == 0) { # Child
|
} elsif ($pid == 0) { # Child
|
||||||
|
POSIX::setsid ();
|
||||||
# we need no io streams here, right?
|
# we need no io streams here, right?
|
||||||
# it seems that at least vscode creashes if we close stdio handles
|
# it seems that at least vscode creashes if we close stdio handles
|
||||||
open(STDOUT, ">", "/dev/null");
|
open(STDOUT, ">", "/dev/null");
|
||||||
open(STDERR, ">", "/dev/null");
|
open(STDERR, ">", "/dev/null");
|
||||||
open(STDIN, "<", "/dev/null");
|
open(STDIN, "<", "/dev/null");
|
||||||
my $prg = shift(@args);
|
$pid = fork ();
|
||||||
# weird situation, silently quit here :)
|
|
||||||
exit 0 unless(defined($prg));
|
|
||||||
|
|
||||||
if (@args > 0) {
|
if ($pid) { # Parent
|
||||||
exec $prg @args;
|
# in this place parent quits
|
||||||
} else {
|
exit 0;
|
||||||
exec $prg;
|
} elsif ($pid == 0) { # Child
|
||||||
|
my $prg = shift(@args);
|
||||||
|
# weird situation, silently quit here :)
|
||||||
|
exit 0 unless(defined($prg));
|
||||||
|
|
||||||
|
if (@args > 0) {
|
||||||
|
exec $prg @args;
|
||||||
|
} else {
|
||||||
|
exec $prg;
|
||||||
|
}
|
||||||
|
} else { # Unable to fork()
|
||||||
|
syswrite STDERR, "[{\"full_text\":\"ERROR: Could not fork new process: $!\",\"color\":\"#ff0000\"}],\n";
|
||||||
}
|
}
|
||||||
} else { # Unable to fork()
|
} else { # Unable to fork()
|
||||||
syswrite STDERR, "[{\"full_text\":\"ERROR: Could not fork new process: $!\",\"color\":\"#ff0000\"}],\n";
|
syswrite STDERR, "[{\"full_text\":\"ERROR: Could not fork new process: $!\",\"color\":\"#ff0000\"}],\n";
|
||||||
|
|||||||
149
tpingd.go
Normal file
149
tpingd.go
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
"github.com/godbus/dbus/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
var pingres = false
|
||||||
|
var sleeptime time.Duration = 5
|
||||||
|
var charge = "[??% ? 00:00]"
|
||||||
|
|
||||||
|
func secondsToClock(inSeconds int64) string {
|
||||||
|
hours := (inSeconds / 60) / 60
|
||||||
|
minutes := inSeconds % 60
|
||||||
|
|
||||||
|
str := fmt.Sprintf("%02d:%02d", hours, minutes)
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func mycharge () {
|
||||||
|
for true {
|
||||||
|
conn, err := dbus.SystemBus()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
obj := conn.Object("org.freedesktop.UPower", "/org/freedesktop/UPower/devices/battery_BAT0")
|
||||||
|
percentage, err := obj.GetProperty("org.freedesktop.UPower.Percentage")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var pcharge int = int(percentage.Value().(float64))
|
||||||
|
|
||||||
|
state, err := obj.GetProperty("org.freedesktop.UPower.State")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var status string = "?"
|
||||||
|
var chtime string = "00:00"
|
||||||
|
|
||||||
|
if state.Value().(uint32) == 1 {
|
||||||
|
mytime, err := obj.GetProperty("org.freedesktop.UPower.TimeToFull")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
chtime = "00:00"
|
||||||
|
} else {
|
||||||
|
var seconds = mytime.Value().(int64)
|
||||||
|
chtime = secondsToClock(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
status = "▲"
|
||||||
|
} else if state.Value().(uint32) == 2 {
|
||||||
|
mytime, err := obj.GetProperty("org.freedesktop.UPower.TimeToEmpty")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
chtime = "00:00"
|
||||||
|
} else {
|
||||||
|
var seconds = mytime.Value().(int64)
|
||||||
|
chtime = secondsToClock(seconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
status = "▼"
|
||||||
|
} else if state.Value().(uint32) == 4 {
|
||||||
|
status = "•"
|
||||||
|
}
|
||||||
|
|
||||||
|
charge = fmt.Sprintf("[%02d%% %s %s]\n", pcharge, status, chtime)
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func myping() {
|
||||||
|
for true {
|
||||||
|
conn, e := net.DialTimeout("tcp", "jenkins:443", 3000 * time.Millisecond)
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
pingres = false
|
||||||
|
} else {
|
||||||
|
conn.Close()
|
||||||
|
pingres = true
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(sleeptime * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleConnection(conn net.Conn) {
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
bufLen, err := conn.Read(buf)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
tail := string(buf[bufLen - 4 : bufLen])
|
||||||
|
|
||||||
|
// ah, fuck, yea. if you check buffer with \n\r\n\r against "\n\r\n\r" you'll get always FALSE, but if check against hex values, it works
|
||||||
|
if bufLen > 4 && fmt.Sprintf("%x", tail) == "0d0a0d0a" {
|
||||||
|
if pingres {
|
||||||
|
var answer = fmt.Sprintf("HTTP/1.1 200 OK\nContent-Type: text/plain\nConnection: close\n\nReachable %s\n", charge)
|
||||||
|
conn.Write([]byte(answer))
|
||||||
|
} else {
|
||||||
|
var answer = fmt.Sprintf("HTTP/1.1 200 OK\nContent-Type: text/plain\nConnection: close\n\nUnreachable %s\n", charge)
|
||||||
|
conn.Write([]byte(answer))
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.Close()
|
||||||
|
} else {
|
||||||
|
conn.Write([]byte("HTTP/1.1 400 Bad Request\nContent-Type: text/plain\nConnection: close\n\nBad Request\n"))
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ln, e := net.Listen("tcp", "127.0.0.1:9000")
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
fmt.Println(e)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
go myping()
|
||||||
|
go mycharge()
|
||||||
|
|
||||||
|
for {
|
||||||
|
// infinitely handle incomming connections
|
||||||
|
conn, e := ln.Accept()
|
||||||
|
|
||||||
|
if e != nil {
|
||||||
|
// shit happens, let's try one more time
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
go handleConnection(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user