add a configurable daemon that warns on low battery
Signed-off-by: Maarten van Gompel <proycon@anaproy.nl>
This commit is contained in:
parent
60dd08fa27
commit
c7e0e3b656
7 changed files with 131 additions and 1 deletions
11
Makefile
11
Makefile
|
@ -5,6 +5,7 @@ PREFIX:=/usr
|
|||
PROGRAMS = \
|
||||
programs/sxmo_setpineled \
|
||||
programs/sxmo_megiaudioroute \
|
||||
programs/sxmo_batterymonitor \
|
||||
programs/sxmo_vibratepine
|
||||
|
||||
all: $(PROGRAMS)
|
||||
|
@ -23,8 +24,11 @@ programs/sxmo_megiaudioroute: programs/sxmo_megiaudioroute.c
|
|||
programs/sxmo_vibratepine: programs/sxmo_vibratepine.c
|
||||
gcc -o programs/sxmo_vibratepine programs/sxmo_vibratepine.c
|
||||
|
||||
programs/sxmo_batterymonitor: programs/sxmo_batterymonitor.c
|
||||
gcc -o programs/sxmo_batterymonitor programs/sxmo_batterymonitor.c
|
||||
|
||||
clean:
|
||||
rm -f programs/sxmo_setpineled programs/sxmo_megiaudioroute programs/sxmo_vibratepine
|
||||
rm -f programs/sxmo_setpineled programs/sxmo_megiaudioroute programs/sxmo_vibratepine programs/sxmo_batterymonitor
|
||||
|
||||
install: $(PROGRAMS)
|
||||
cd configs && find . -type f -exec install -D -m 0644 "{}" "$(DESTDIR)$(PREFIX)/share/sxmo/{}" \; && cd ..
|
||||
|
@ -40,6 +44,8 @@ install: $(PROGRAMS)
|
|||
|
||||
install -D -m 0644 -t $(DESTDIR)$(PREFIX)/share/applications/ configs/xdg/mimeapps.list
|
||||
|
||||
install -D -m 0644 -t $(DESTDIR)/etc/sudoers.d/ configs/sudo/poweroff
|
||||
|
||||
install -D -m 0644 -T configs/xorg/monitor.conf $(DESTDIR)$(PREFIX)/share/X11/xorg.conf.d/90-monitor.conf
|
||||
|
||||
mkdir -p $(DESTDIR)/etc/NetworkManager/dispatcher.d
|
||||
|
@ -49,9 +55,12 @@ install: $(PROGRAMS)
|
|||
# Bin
|
||||
install -D -t $(DESTDIR)$(PREFIX)/bin scripts/*/*
|
||||
|
||||
install -D -m 0755 programs/sxmo_batterymonitor $(DESTDIR)$(PREFIX)/bin/
|
||||
|
||||
install -D -m 0755 programs/sxmo_setpineled $(DESTDIR)$(PREFIX)/bin/
|
||||
|
||||
install -D programs/sxmo_megiaudioroute $(DESTDIR)$(PREFIX)/bin/
|
||||
|
||||
install -D programs/sxmo_vibratepine $(DESTDIR)$(PREFIX)/bin/
|
||||
|
||||
echo "NOTICE: Do not forget to add sxmo-setpermissions to your init system, e.g. for openrc: rc-update add sxmo-setpermissions default && rc-service sxmo-setpermissions start" >&2
|
||||
|
|
|
@ -69,3 +69,22 @@ export SXMO_LOCK_SCREEN_OFF=1
|
|||
|
||||
# Or, enable vibration feedback on keypress
|
||||
#export KEYBOARD_ARGS="-o | clickclack -V"
|
||||
|
||||
## Configuration parameters for sxmo_batterymonitor ##
|
||||
|
||||
# The following values are the defaults
|
||||
|
||||
# enable (1) or disable (0) the warning
|
||||
#export BM_WARN=1
|
||||
|
||||
# enable (1) or disable (0) repeating warnings and hook executions
|
||||
#export BM_LOOP=0
|
||||
|
||||
# set a threshold for the warning
|
||||
#export BM_WARN_THRESHOLD=15
|
||||
|
||||
# set a threshold for the action (executes battery hook)
|
||||
#export BM_WARN_CRIT_THRESHOLD=5
|
||||
|
||||
#let the daemon check every 5 minutes (300s)
|
||||
sxmo_batterymonitor -i 300
|
||||
|
|
6
configs/default_hooks/battery
Normal file
6
configs/default_hooks/battery
Normal file
|
@ -0,0 +1,6 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
# This script is executed when sxmo_batterymonitor detects a battery capacity below the set threshold
|
||||
# You can use it to shutdown, play a notification sound, send a message, etc
|
||||
|
||||
mpv --no-video /usr/share/sxmo/notify.ogg >/dev/null && notify-send \"battery low\" && sxmo_vibratepine 1500 &
|
6
configs/default_hooks/battery_crit
Normal file
6
configs/default_hooks/battery_crit
Normal file
|
@ -0,0 +1,6 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
# This script is executed when sxmo_batterymonitor detects a battery capacity below the set threshold
|
||||
# You can use it to shutdown, play a notification sound, send a message, etc
|
||||
|
||||
sudo busybox poweroff
|
1
configs/sudo/poweroff
Normal file
1
configs/sudo/poweroff
Normal file
|
@ -0,0 +1 @@
|
|||
ALL ALL=NOPASSWD: /bin/busybox poweroff
|
87
programs/sxmo_batterymonitor.c
Normal file
87
programs/sxmo_batterymonitor.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <syslog.h>
|
||||
|
||||
static void help(){
|
||||
printf("Usage: sxmo_batterymonitor [POSIX style options]\nPOSIX options:\n-h\t\tprint help\n-i\tinterval\t set an interval in seconds");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void term(){
|
||||
syslog (LOG_NOTICE, "sxmo_batterymonitor daemon terminated.");
|
||||
closelog();
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[], char* envp[]){
|
||||
int interval=10;
|
||||
if (argc < 3){
|
||||
help();
|
||||
}
|
||||
if ( strcmp(argv[1], "-i") == 0 ){
|
||||
interval = atoi(argv[2]);
|
||||
}
|
||||
else{
|
||||
help();
|
||||
}
|
||||
if (system("pidof -o $PPID sxmo_batterymonitor > /dev/null") == 0){
|
||||
printf("Another daemon is already running -- exiting");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if( daemon(0,0) != 0){
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
openlog ("sxmo_batterymonitor", LOG_PID, LOG_DAEMON);
|
||||
syslog (LOG_NOTICE, "sxmo_batterymonitor daemon started.");
|
||||
setenv("BATTERY_DEVICE","/sys/class/power_supply/axp20x-battery",0);
|
||||
setenv("DISPLAY",":0",0);
|
||||
setenv("BM_LOOP","0",0);
|
||||
setenv("BM_WARN","1",0);
|
||||
setenv("BM_WARN_THRESHOLD","15",0);
|
||||
setenv("BM_WARN_CRIT_THRESHOLD","5",0);
|
||||
char* device = getenv("BATTERY_DEVICE");
|
||||
char* cap = "/capacity",data=0;
|
||||
char* path= strcat(device,cap);
|
||||
int c=0,wrn=0,bm_disable_warn=0,bm_warn_threshold=15,bm_warn_crit_threshold=5,loop=0;
|
||||
loop = atoi(getenv("BM_LOOP"));
|
||||
bm_disable_warn = atoi(getenv("BM_WARN"));
|
||||
bm_warn_threshold = atoi(getenv("BM_WARN_THRESHOLD"));
|
||||
bm_warn_crit_threshold = atoi(getenv("BM_WARN_CRIT_THRESHOLD"));
|
||||
while (1){
|
||||
FILE *ptr = fopen(path,"r");
|
||||
fgets(&data,5,ptr);
|
||||
c = atoi(&data);
|
||||
switch( loop ){
|
||||
case 1:
|
||||
if( (bm_disable_warn == 1) && (c < bm_warn_threshold) ){
|
||||
system("$XDG_CONFIG_HOME/sxmo/hooks/battery &");
|
||||
}
|
||||
if( c < bm_warn_crit_threshold ){
|
||||
system("$XDG_CONFIG_HOME/sxmo/hooks/battery_crit &");
|
||||
}
|
||||
default:
|
||||
if( (bm_disable_warn == 1) && (c < bm_warn_threshold) && (wrn == 0)){
|
||||
wrn = 1;
|
||||
system("$XDG_CONFIG_HOME/sxmo/hooks/battery &");
|
||||
}
|
||||
if( (c < bm_warn_crit_threshold) && (wrn != 2) ){
|
||||
wrn=2;
|
||||
system("$XDG_CONFIG_HOME/sxmo/hooks/battery_crit &");
|
||||
}
|
||||
if( c >= bm_warn_crit_threshold ){
|
||||
wrn = 1;
|
||||
}
|
||||
if( c >= bm_warn_threshold ){
|
||||
wrn = 0;
|
||||
}
|
||||
}
|
||||
fclose(ptr);
|
||||
sleep(interval);
|
||||
}
|
||||
term();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
|
@ -38,6 +38,8 @@ defaultconfigs() {
|
|||
defaultconfig /usr/share/sxmo/default_hooks/ring "$XDG_CONFIG_HOME/sxmo/hooks/ring" 744
|
||||
defaultconfig /usr/share/sxmo/default_hooks/rtcwake "$XDG_CONFIG_HOME/sxmo/hooks/rtcwake" 744
|
||||
defaultconfig /usr/share/sxmo/default_hooks/sms "$XDG_CONFIG_HOME/sxmo/hooks/sms" 744
|
||||
defaultconfig /usr/share/sxmo/default_hooks/battery "$XDG_CONFIG_HOME/sxmo/hooks/battery" 744
|
||||
defaultconfig /usr/share/sxmo/default_hooks/battery_crit "$XDG_CONFIG_HOME/sxmo/hooks/battery_crit" 744
|
||||
}
|
||||
|
||||
defaultconfigs
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue