refactor: refactored battery bash scripts into a C++ daemon
This commit is contained in:
parent
f5532e89ca
commit
16690836d6
10 changed files with 215 additions and 2 deletions
|
|
@ -25,7 +25,7 @@ battery-full() {
|
||||||
}
|
}
|
||||||
|
|
||||||
battery-low() {
|
battery-low() {
|
||||||
notify-send "Low Battery" "${BAT_PERCENT}% of battery remaining." -u critical -i "battery-caution" -r 9991
|
notify-send "Low Battery" "${BAT_PERCENT}% of battery remaining." -u critical -i "battery-caution" -r 9991 -t 5000
|
||||||
touch $LOW_BAT_FILE
|
touch $LOW_BAT_FILE
|
||||||
rm -rf $FULL_BAT_FILE
|
rm -rf $FULL_BAT_FILE
|
||||||
}
|
}
|
||||||
|
|
@ -67,7 +67,7 @@ elif [ "$BAT_PERCENT" -le $WARNING_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 1 ] &
|
||||||
|
|
||||||
# If LOW_BAT_FILE Exists, Spam Until It's Charging
|
# If LOW_BAT_FILE Exists, Spam Until It's Charging
|
||||||
elif [ "$BAT_PERCENT" -le $WARNING_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 1 ] && [ -f $LOW_BAT_FILE ]; then
|
elif [ "$BAT_PERCENT" -le $WARNING_PERCENT ] && [ "$DISCHARGING_COUNT" -eq 1 ] && [ -f $LOW_BAT_FILE ]; then
|
||||||
notify-send "Low Battery" "${BAT_PERCENT}% of battery remaining." -u critical -i "battery-caution" -r 9991
|
notify-send "Low Battery" "${BAT_PERCENT}% of battery remaining." -u critical -i "battery-caution" -r 9991 -t 5000
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Debug commands
|
# Debug commands
|
||||||
5
archived/battery-scripts/battery-daemon
Normal file
5
archived/battery-scripts/battery-daemon
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/bash
|
||||||
|
while true; do
|
||||||
|
~/.scripts/notifs/battery
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
BIN
daemons/battery-daemon
Normal file → Executable file
BIN
daemons/battery-daemon
Normal file → Executable file
Binary file not shown.
173
daemons/battery-daemon.cpp
Normal file
173
daemons/battery-daemon.cpp
Normal file
|
|
@ -0,0 +1,173 @@
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sched.h> // pid_t data type
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <unistd.h> // For fork();
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// If another instance of this program is running...
|
||||||
|
const char *lockFilePath = "/tmp/bat-daemon-run";
|
||||||
|
|
||||||
|
// Checks if the file exists. (Another instance is running)
|
||||||
|
bool isRunning() {
|
||||||
|
ifstream lockFile(lockFilePath);
|
||||||
|
return lockFile.good();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates file w/ PID
|
||||||
|
void createLockFile() {
|
||||||
|
ofstream lockFile(lockFilePath);
|
||||||
|
lockFile << getpid();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Removes file.
|
||||||
|
void removeLockFile() { remove(lockFilePath); }
|
||||||
|
|
||||||
|
void send_notifs_warn(const int &percent) {
|
||||||
|
// Formulate command
|
||||||
|
string command =
|
||||||
|
"notify-send 'Low Battery' '" + to_string(percent) +
|
||||||
|
"% of battery remaining.' -u critical -i 'battery-caution' -t 5000";
|
||||||
|
|
||||||
|
// Execute command as a C-String (same contents, but compatible with C++ code)
|
||||||
|
// "Returns a pointer to an array that contains the contents of the variable"
|
||||||
|
system(command.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_notifs_charge(const int &percent, const int status) {
|
||||||
|
string command;
|
||||||
|
|
||||||
|
switch (status) {
|
||||||
|
case 1:
|
||||||
|
command = "notify-send 'Charging' 'Charging battery at " +
|
||||||
|
to_string(percent) +
|
||||||
|
"%' -u low -i 'battery-level-50-charging-symbolic' -t 5000";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0:
|
||||||
|
command = "notify-send 'Discharging' '" + to_string(percent) +
|
||||||
|
"% remaining' -u low -i 'battery-level-70-symbolic' -t 5000";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
system(command.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_notifs_full() {
|
||||||
|
string command = "notify-send 'Battery Full' 'Battery is fully charged!' -i "
|
||||||
|
"'battery-full-charged' -t 5000";
|
||||||
|
system(command.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Turn program into a daemon.
|
||||||
|
void daemonize() {
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
// if forking fails, exit.
|
||||||
|
if (pid < 0) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pid > 0) {
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
// New session: if fails, exit.
|
||||||
|
// - New process becomes the leader
|
||||||
|
if (setsid() < 0) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change directory to root
|
||||||
|
chdir("/");
|
||||||
|
|
||||||
|
// Redirects all streams to /dev/null
|
||||||
|
freopen("/dev/null", "r", stdin);
|
||||||
|
freopen("/dev/null", "w", stdout);
|
||||||
|
freopen("/dev/null", "w", stderr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks battery info. (Charging, Discharging, etc.)
|
||||||
|
void battery() {
|
||||||
|
int OLD_BAT_PERCENT = 100; // Maxing out to prevent bugs
|
||||||
|
int CHARGE = 0; // 1 = Charging, 0 = Discharging
|
||||||
|
int BAT_FULL = 0; // 1 = Full, 0 = Not Full
|
||||||
|
while (true) {
|
||||||
|
int BAT_WARN = 36;
|
||||||
|
// Read and Grab File Info:
|
||||||
|
|
||||||
|
// ifstream (input file stream) class: operates on files (I/O)
|
||||||
|
ifstream BAT_STATUS_FILE("/sys/class/power_supply/BAT0/status");
|
||||||
|
string BAT_STATUS;
|
||||||
|
|
||||||
|
ifstream BAT_PERCENT_FILE("/sys/class/power_supply/BAT0/capacity");
|
||||||
|
int BAT_PERCENT;
|
||||||
|
|
||||||
|
// If it can't extract info, exit.
|
||||||
|
if (!(BAT_PERCENT_FILE >> BAT_PERCENT) ||
|
||||||
|
!(BAT_STATUS_FILE >> BAT_STATUS)) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timing:
|
||||||
|
// timeval = Time Value accurate from microseconds to years.
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
// Set intervals: 0sec + 100 milsec.
|
||||||
|
tv.tv_sec = 0;
|
||||||
|
tv.tv_usec = 100000;
|
||||||
|
|
||||||
|
if (BAT_PERCENT <= BAT_WARN) {
|
||||||
|
if ((BAT_PERCENT < OLD_BAT_PERCENT) && (BAT_STATUS == "Discharging")) {
|
||||||
|
CHARGE = 0;
|
||||||
|
OLD_BAT_PERCENT = BAT_PERCENT;
|
||||||
|
send_notifs_warn(BAT_PERCENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BAT_STATUS == "Charging") {
|
||||||
|
if ((BAT_PERCENT < 99) && (CHARGE == 0)) {
|
||||||
|
CHARGE = 1;
|
||||||
|
OLD_BAT_PERCENT = BAT_PERCENT;
|
||||||
|
BAT_FULL = 0;
|
||||||
|
send_notifs_charge(BAT_PERCENT, CHARGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BAT_PERCENT >= 99 && BAT_FULL == 0) {
|
||||||
|
BAT_FULL = 1;
|
||||||
|
CHARGE = 1;
|
||||||
|
send_notifs_full();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (BAT_STATUS == "Discharging" && CHARGE == 1) {
|
||||||
|
CHARGE = 0;
|
||||||
|
send_notifs_charge(BAT_PERCENT, CHARGE);
|
||||||
|
|
||||||
|
if (BAT_PERCENT < 99) {
|
||||||
|
BAT_FULL = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
select(0, NULL, NULL, NULL, &tv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initializer
|
||||||
|
int main() {
|
||||||
|
if (isRunning()) {
|
||||||
|
cout << "Another process already exists!" << endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
createLockFile();
|
||||||
|
|
||||||
|
daemonize();
|
||||||
|
battery();
|
||||||
|
|
||||||
|
removeLockFile();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -11,6 +11,10 @@ bright_dec() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keybind-func() {
|
||||||
|
echo "$CURRENT_BRIGHTNESS"
|
||||||
|
}
|
||||||
|
|
||||||
while test $# -gt 0; do
|
while test $# -gt 0; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
inc)
|
inc)
|
||||||
|
|
@ -20,5 +24,10 @@ while test $# -gt 0; do
|
||||||
dec)
|
dec)
|
||||||
bright_dec && exit 0
|
bright_dec && exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
keybind)
|
||||||
|
keybind-func && exit 0
|
||||||
|
;;
|
||||||
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
|
||||||
15
notifs/info
Executable file
15
notifs/info
Executable file
|
|
@ -0,0 +1,15 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Enter scripts directory
|
||||||
|
cd ~/.scripts/notifs || exit
|
||||||
|
|
||||||
|
# Constants
|
||||||
|
CURRENT_VOL=$(./volume keybind | grep -o '^[^ ]*')
|
||||||
|
CURRENT_MIC_VOL=$(./volume keybind | awk '{ print $2 }')
|
||||||
|
BRIGHTNESS=$(./brightness keybind)
|
||||||
|
|
||||||
|
# Variables for display, basically multiplies floats to integers
|
||||||
|
DISPLAY_VOL=$(awk '{ print $1 * $2 }' <<<"${CURRENT_VOL} 100")
|
||||||
|
DISPLAY_MIC_VOL=$(awk '{ print $1 * $2 }' <<<"${CURRENT_MIC_VOL} 100")
|
||||||
|
|
||||||
|
dunstify "All other info:" "Brightness: $BRIGHTNESS%\nCurrent Volume: $DISPLAY_VOL%\nCurrent Mic. Volume: $DISPLAY_MIC_VOL%"
|
||||||
|
|
@ -92,6 +92,10 @@ mic_mute_toggle() {
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keybind-func() {
|
||||||
|
echo "$CURRENT_VOL" "$CURRENT_MIC_VOL"
|
||||||
|
}
|
||||||
|
|
||||||
while test $# -gt 0; do
|
while test $# -gt 0; do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
inc)
|
inc)
|
||||||
|
|
@ -118,5 +122,9 @@ while test $# -gt 0; do
|
||||||
mic_mute_toggle && exit 0
|
mic_mute_toggle && exit 0
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
keybind)
|
||||||
|
keybind-func && exit 0
|
||||||
|
;;
|
||||||
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
|
||||||
3
notifs/window
Executable file
3
notifs/window
Executable file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/bash
|
||||||
|
dunstify "Switching Window" "Current Window: $1" -t 1000 -r 5000
|
||||||
|
swaymsg workspace number "$1"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue