diff --git a/notifs/battery b/archived/battery-scripts/battery similarity index 96% rename from notifs/battery rename to archived/battery-scripts/battery index cebbcca..98398e8 100755 --- a/notifs/battery +++ b/archived/battery-scripts/battery @@ -25,7 +25,7 @@ battery-full() { } 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 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 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 # Debug commands diff --git a/archived/battery-scripts/battery-daemon b/archived/battery-scripts/battery-daemon new file mode 100644 index 0000000..ea05e40 --- /dev/null +++ b/archived/battery-scripts/battery-daemon @@ -0,0 +1,5 @@ +#!/bin/bash +while true; do + ~/.scripts/notifs/battery + sleep 1 +done diff --git a/notifs/battery-status b/archived/battery-scripts/battery-status similarity index 100% rename from notifs/battery-status rename to archived/battery-scripts/battery-status diff --git a/notifs/charge-status b/archived/battery-scripts/charge-status similarity index 100% rename from notifs/charge-status rename to archived/battery-scripts/charge-status diff --git a/daemons/battery-daemon b/daemons/battery-daemon old mode 100644 new mode 100755 index ea05e40..3ef384b Binary files a/daemons/battery-daemon and b/daemons/battery-daemon differ diff --git a/daemons/battery-daemon.cpp b/daemons/battery-daemon.cpp new file mode 100644 index 0000000..c2f6500 --- /dev/null +++ b/daemons/battery-daemon.cpp @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include // pid_t data type +#include +#include // 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; +} diff --git a/notifs/brightness b/notifs/brightness index cec2e0b..86c26f5 100755 --- a/notifs/brightness +++ b/notifs/brightness @@ -11,6 +11,10 @@ bright_dec() { } +keybind-func() { + echo "$CURRENT_BRIGHTNESS" +} + while test $# -gt 0; do case "$1" in inc) @@ -20,5 +24,10 @@ while test $# -gt 0; do dec) bright_dec && exit 0 ;; + + keybind) + keybind-func && exit 0 + ;; + esac done diff --git a/notifs/info b/notifs/info new file mode 100755 index 0000000..6376149 --- /dev/null +++ b/notifs/info @@ -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%" diff --git a/notifs/volume b/notifs/volume index 9202784..2db2103 100755 --- a/notifs/volume +++ b/notifs/volume @@ -92,6 +92,10 @@ mic_mute_toggle() { fi } +keybind-func() { + echo "$CURRENT_VOL" "$CURRENT_MIC_VOL" +} + while test $# -gt 0; do case "$1" in inc) @@ -118,5 +122,9 @@ while test $# -gt 0; do mic_mute_toggle && exit 0 ;; + keybind) + keybind-func && exit 0 + ;; + esac done diff --git a/notifs/window b/notifs/window new file mode 100755 index 0000000..823ea72 --- /dev/null +++ b/notifs/window @@ -0,0 +1,3 @@ +#!/bin/bash +dunstify "Switching Window" "Current Window: $1" -t 1000 -r 5000 +swaymsg workspace number "$1"