From 60195aa04325c7954b41257be1a05ec01d079275 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 17 Mar 2024 14:00:07 +0100 Subject: [PATCH] Add initial files --- .visudo | 9 +++++ Makefile | 33 ++++++++++++++++ README.md | 44 +++++++++++++++++++++ cbattery | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 .visudo create mode 100644 Makefile create mode 100644 README.md create mode 100755 cbattery diff --git a/.visudo b/.visudo new file mode 100644 index 0000000..742151b --- /dev/null +++ b/.visudo @@ -0,0 +1,9 @@ +Cmnd_Alias BATTERYOFF = /usr/local/bin/smc -k CH0B -w 02, /usr/local/bin/smc -k CH0C -w 02, /usr/local/bin/smc -k CH0B -r, /usr/local/bin/smc -k CH0C -r +Cmnd_Alias BATTERYON = /usr/local/bin/smc -k CH0B -w 00, /usr/local/bin/smc -k CH0C -w 00 +Cmnd_Alias DISCHARGEOFF = /usr/local/bin/smc -k CH0I -w 00, /usr/local/bin/smc -k CH0I -r +Cmnd_Alias DISCHARGEON = /usr/local/bin/smc -k CH0I -w 01 + +ALL ALL = NOPASSWD: BATTERYOFF +ALL ALL = NOPASSWD: BATTERYON +ALL ALL = NOPASSWD: DISCHARGEOFF +ALL ALL = NOPASSWD: DISCHARGEON diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f378058 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +DESTDIR = /usr/local/bin +SUDOERSDIR = /private/etc/sudoers.d +INSTALL_PATH_CBATTERY = $(DESTDIR)/cbattery +INSTALL_PATH_VISUDO = $(SUDOERSDIR)/cbattery + +all: install + +install: $(INSTALL_PATH_CBATTERY) $(INSTALL_PATH_VISUDO) + +$(INSTALL_PATH_VISUDO): .visudo + @echo "Installing visudo file to $(SUDOERSDIR)" + @if ! test -d "$(SUDOERSDIR)"; then \ + mkdir -p "$(SUDOERSDIR)"; \ + fi + @cp $< $(SUDOERSDIR)/cbattery + @chmod 440 $(SUDOERSDIR)/cbattery + +$(INSTALL_PATH_CBATTERY): cbattery + @echo "Installing cbattery to $(DESTDIR)" + @install -m 755 $< $(DESTDIR) + +uninstall: + @echo "Removing cbattery from $(DESTDIR)" + @cbattery uninstall || (echo "Uninstalling cbattery failed"; exit 1) + @rm -f $(INSTALL_PATH_CBATTERY) $(INSTALL_PATH_SMC) + +help: + @echo "Available targets:" + @echo " make install - Install cbattery to $(DESTDIR)" + @echo " make uninstall - Remove cbattery from $(DESTDIR)" + @echo " make help - Show this help message" + +.PHONY: all install uninstall help diff --git a/README.md b/README.md new file mode 100644 index 0000000..146b630 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# cbattery + +cbattery is a script for managing battery-related settings on Apple Silicon systems. + +## Installation + +You can install `cbattery` using Homebrew: + +```bash +brew tap santilococo/cmtap +brew install santilococo/cbattery +``` + +This will also install the necessary `smc` dependency. + +## Usage + +Once installed, you can use `cbattery` to manage battery-related settings on your M1 system. Here are some common commands: + +- **Toggle charging**: `cbattery charging [on|off]` +- **Toggle adapter connection**: `cbattery adapter [on|off]` +- **Get status information**: `cbattery status` + +For more information on available commands, you can use the `help` command: + +```bash +cbattery help +``` + +## Uninstallation + +To uninstall `cbattery` from your system: + +```bash +brew uninstall cbattery +``` + +## Contributing + +If you encounter any issues with `cbattery` or would like to contribute to its development, please feel free to open an issue or submit a pull request on the [GitHub repository](https://git.slc.ar/slococo/cbattery). We welcome any feedback or contributions! + +## License + +This project is licensed under the GPL-2.0 License - see the [LICENSE](LICENSE.md) file for details. diff --git a/cbattery b/cbattery new file mode 100755 index 0000000..a8a856f --- /dev/null +++ b/cbattery @@ -0,0 +1,113 @@ +#!/usr/bin/env bash + +set -e + +function usage() { + cat << EOF +usage: ${0##*/} [command] + charging [on|off] - Toggle charging + adapter [on|off] - Toggle adapter connection + status - Get status information + uninstall - Revert to default settings + help - Display this help message +EOF +} + +function enable_discharging() { + echo "Enabling battery discharging" + sudo smc -k CH0I -w 01 +} + +function disable_discharging() { + echo "Disabling battery discharging" + sudo smc -k CH0I -w 00 +} + +function enable_charging() { + echo "Enabling battery charging" + sudo smc -k CH0B -w 00 + sudo smc -k CH0C -w 00 + disable_discharging +} + +function disable_charging() { + echo "Disabling battery charging" + sudo smc -k CH0B -w 02 + sudo smc -k CH0C -w 02 +} + +function get_smc_charging_hex() { + smc -k CH0B -r | awk '{print $4}' | sed 's:\)::' +} + +function get_smc_charging_status() { + hex_status=$(get_smc_charging_hex) + if [[ "$hex_status" == "00" ]]; then + echo "enabled" + else + echo "disabled" + fi +} + +function get_smc_discharging_status() { + hex_status=$(smc -k CH0I -r | awk '{print $4}' | sed 's:\)::') + if [[ "$hex_status" == "0" ]]; then + echo "not discharging" + else + echo "discharging" + fi +} + +function get_battery_percentage() { + battery_percentage=$(pmset -g batt | tail -n1 | awk '{print $3}' | sed 's:%\;::') + echo "$battery_percentage" +} + +function get_remaining_time() { + time_remaining=$(pmset -g batt | tail -n1 | awk '{print $5}') + echo "$time_remaining" +} + +action=$1 +setting=$2 + +if [ -z "$action" ] || [[ "$action" == "help" ]]; then + usage + exit 0 +fi + +if [[ "$action" == "uninstall" ]]; then + enable_charging + disable_discharging + exit 0 +fi + +if [[ "$action" == "charging" ]]; then + echo "Setting $action to $setting" + if [[ "$setting" == "on" ]]; then + enable_charging + elif [[ "$setting" == "off" ]]; then + disable_charging + fi + exit 0 +fi + +if [[ "$action" == "adapter" ]]; then + echo "Setting $action to $setting" + if [[ "$setting" == "on" ]]; then + enable_discharging + elif [[ "$setting" == "off" ]]; then + disable_discharging + fi + exit 0 +fi + +if [[ "$action" == "status" ]]; then + if [[ "$setting" == "charging" ]]; then + exit "$(get_smc_charging_hex)" + else + echo "Battery at $(get_battery_percentage)% ($(get_remaining_time) remaining), smc charging $(get_smc_charging_status)" + fi + exit 0 +fi +