Compare commits

..

No commits in common. "master" and "v0.4" have entirely different histories.
master ... v0.4

5 changed files with 99 additions and 207 deletions

View File

@ -1,7 +1,7 @@
Cmnd_Alias BATTERYOFF = smc -k CH0B -w 02, smc -k CH0C -w 02, smc -k CH0B -r, smc -k CH0C -r 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 = smc -k CH0B -w 00, smc -k CH0C -w 00 Cmnd_Alias BATTERYON = /usr/local/bin/smc -k CH0B -w 00, /usr/local/bin/smc -k CH0C -w 00
Cmnd_Alias DISCHARGEOFF = smc -k CH0I -w 00, smc -k CH0I -r Cmnd_Alias DISCHARGEOFF = /usr/local/bin/smc -k CH0I -w 00, /usr/local/bin/smc -k CH0I -r
Cmnd_Alias DISCHARGEON = smc -k CH0I -w 01 Cmnd_Alias DISCHARGEON = /usr/local/bin/smc -k CH0I -w 01
ALL ALL = NOPASSWD: BATTERYOFF ALL ALL = NOPASSWD: BATTERYOFF
ALL ALL = NOPASSWD: BATTERYON ALL ALL = NOPASSWD: BATTERYON

View File

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2024 Santiago Lo Coco
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,44 +1,37 @@
PREFIX ?= /usr/local PREFIX ?= /usr/local
DESTDIR = $(PREFIX)/bin DESTDIR = $(PREFIX)/bin
INSTALL_PATH = $(DESTDIR)/cbattery INSTALL_PATH = $(DESTDIR)/cbattery
SHAREDIR = $(PREFIX)/share/cbattery
DAEMON_PATH = $(SHAREDIR)/cbattery.plist
VISUDO_PATH = $(SHAREDIR)/.visudo
all: install all: install
install: $(INSTALL_PATH) $(DAEMON_PATH) $(VISUDO_PATH) install: $(INSTALL_PATH)
$(INSTALL_PATH): cbattery $(INSTALL_PATH): cbattery
@echo "Installing cbattery to $(DESTDIR)" @echo "Installing cbattery to $(DESTDIR)"
@install -d $(DESTDIR) @install -d $(DESTDIR)
@install -m 755 $< $(DESTDIR) @install -m 755 $< $(DESTDIR)
$(DAEMON_PATH): cbattery.plist
@echo "Installing cbattery.plist to $(DAEMON_PATH)"
@install -d $(SHAREDIR)
@install -m 644 $< $(DAEMON_PATH)
$(VISUDO_PATH): .visudo
@echo "Installing .visudo to $(VISUDO_PATH)"
@install -d $(SHAREDIR)
@install -m 644 $< $(VISUDO_PATH)
uninstall: uninstall:
@echo "Removing cbattery from $(DESTDIR)" @echo "Removing cbattery from $(DESTDIR)"
@$(DESTDIR)/cbattery revert || (echo "Reverting cbattery configurations failed"; exit 1) @$(DESTDIR)/cbattery revert || (echo "Reverting cbattery configurations failed"; exit 1)
@rm -f $(INSTALL_PATH) @rm -f $(INSTALL_PATH)
@echo "Removing cbattery.plist from $(DAEMON_PATH)"
@rm -f $(DAEMON_PATH) SUDOERSDIR = /private/etc/sudoers.d/cbattery
@echo "Removing .visudo from $(VISUDO_PATH)"
@rm -f $(VISUDO_PATH) visudo: .visudo
@echo "Removing $(SHAREDIR)" @echo "Installing visudo file to $(SUDOERSDIR)"
@rmdir $(SHAREDIR) || true @if ! test -d "$(SUDOERSDIR)"; then \
mkdir -p "$(SUDOERSDIR)"; \
fi
@cp $< $(SUDOERSDIR)/cbattery
@chmod 440 $(SUDOERSDIR)/cbattery
help: help:
@echo "Available targets:" @echo "Available targets:"
@echo " make install - Install cbattery to $(DESTDIR)" @echo " make install - Install cbattery to $(DESTDIR)"
@echo " make uninstall - Remove cbattery from $(DESTDIR)" @echo " make uninstall - Remove cbattery from $(DESTDIR)"
@echo " make visudo - Install visudo file to $(SUDOERSDIR)"
@echo " make help - Show this help message" @echo " make help - Show this help message"
.PHONY: all install uninstall help .PHONY: all install uninstall visudo help

213
cbattery
View File

@ -2,163 +2,112 @@
set -e set -e
TMP_FILE=/tmp/cbattery.status function usage() {
SHARE_DIR=/usr/local/share/cbattery cat << EOF
PATH="/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew:$PATH"
usage() {
cat << EOF
usage: ${0##*/} [command] usage: ${0##*/} [command]
charging [on|off] - Toggle charging
charging [on|off] - Toggle charging adapter [on|off] - Toggle adapter connection
adapter [on|off] - Toggle adapter connection status - Get status information
status - Get status information revert - Revert to default settings
revert - Revert to default settings help - Display this help message
visudo - Run the script without password
help - Display this help message
EOF EOF
} }
write_cache() { function enable_discharging() {
[[ -n "$NO_CACHE" ]] || echo "$1" > "$TMP_FILE" echo "Enabling battery discharging"
sudo smc -k CH0I -w 01
} }
read_cache() { function disable_discharging() {
cat "$TMP_FILE" 2> /dev/null echo "Disabling battery discharging"
sudo smc -k CH0I -w 00
} }
log_message() { function enable_charging() {
timestamp=$(date "+%b %d %H:%M:%S") echo "Enabling battery charging"
echo "$timestamp - $1" sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00
disable_discharging
} }
enable_discharging() { function disable_charging() {
log_message "Enabling battery discharging" echo "Disabling battery charging"
sudo smc -k CH0I -w 01 sudo smc -k CH0B -w 02
sudo smc -k CH0C -w 02
} }
disable_discharging() { function get_smc_charging_hex() {
log_message "Disabling battery discharging" smc -k CH0B -r | awk '{print $4}' | sed 's:\)::'
sudo smc -k CH0I -w 00
} }
enable_charging() { function get_smc_charging_status() {
[[ "$(get_smc_charging_hex)" == "00" ]] && return hex_status=$(get_smc_charging_hex)
log_message "Enabling battery charging" if [[ "$hex_status" == "00" ]]; then
sudo smc -k CH0B -w 00 echo "enabled"
sudo smc -k CH0C -w 00 else
echo "disabled"
fi
} }
disable_charging() { function get_smc_discharging_status() {
[[ "$(get_smc_charging_hex)" != "00" ]] && return hex_status=$(smc -k CH0I -r | awk '{print $4}' | sed 's:\)::')
log_message "Disabling battery charging" if [[ "$hex_status" == "0" ]]; then
sudo smc -k CH0B -w 02 echo "not discharging"
sudo smc -k CH0C -w 02 else
echo "discharging"
fi
} }
get_smc_charging_hex() { function get_battery_percentage() {
smc -k CH0B -r | awk '{print $4}' | sed s:\):: battery_percentage=$(pmset -g batt | tail -n1 | awk '{print $3}' | sed 's:%\;::')
echo "$battery_percentage"
} }
get_smc_charging_status() { function get_remaining_time() {
hex_status=$(get_smc_charging_hex) time_remaining=$(pmset -g batt | tail -n1 | awk '{print $5}')
[[ "$hex_status" == "00" ]] && echo "enabled" || echo "disabled" echo "$time_remaining"
} }
get_battery_percentage() { action=$1
pmset -g batt | awk 'END {print $3}' | sed 's/%;//' setting=$2
}
install_visudo_file() { if [ -z "$action" ] || [[ "$action" == "help" ]]; then
sudoers_file="/private/etc/sudoers.d/cbattery" usage
if ! smc_loc="$(command -v smc)"; then exit 0
echo "'smc' is required but not found." fi
exit 1
fi
sed "s:smc:$smc_loc:g" "$SHARE_DIR/.visudo" | sudo tee "$sudoers_file" > /dev/null
if ! sudo visudo -c -f "$sudoers_file"; then
echo "Failed to check sudoers file syntax."
sudo rm "$sudoers_file"
exit 1
fi
sudo chmod 440 "$sudoers_file"
}
install_launch_daemon() { if [[ "$action" == "revert" ]]; then
launch_daemon=/Library/LaunchDaemons/cbattery.plist enable_charging
script_dir="$(dirname "$(realpath "$0")")" disable_discharging
share_plist="$SHARE_DIR/cbattery.plist" exit 0
if ! grep -q "$script_dir" "$share_plist"; then fi
sed "s:/usr/local/bin:$script_dir:g" "$share_plist" | sudo tee "$share_plist" > /dev/null
fi
sudo cp "$share_plist" "$launch_daemon"
sudo chown root:wheel "$launch_daemon"
sudo launchctl load -w "$launch_daemon"
}
get_cached_status() { if [[ "$action" == "charging" ]]; then
set +e echo "Setting $action to $setting"
read_cache_result=$(read_cache) if [[ "$setting" == "on" ]]; then
read_cache_error=$? enable_charging
set -e elif [[ "$setting" == "off" ]]; then
if [ $read_cache_error -ne 0 ]; then disable_charging
status=$(get_smc_charging_status) fi
write_cache "$status" exit 0
echo "$status" fi
else
echo "$read_cache_result"
fi
}
runScript() { if [[ "$action" == "adapter" ]]; then
action=$1 echo "Setting $action to $setting"
setting=$2 if [[ "$setting" == "on" ]]; then
enable_discharging
elif [[ "$setting" == "off" ]]; then
disable_discharging
fi
exit 0
fi
case "$action" in if [[ "$action" == "status" ]]; then
"help" | "") if [[ "$setting" == "charging" ]]; then
usage exit "$(get_smc_charging_hex)"
;; else
"revert") echo "Battery at $(get_battery_percentage)% ($(get_remaining_time) remaining), smc charging $(get_smc_charging_status)"
enable_charging fi
disable_discharging exit 0
launch_daemon=/Library/LaunchDaemons/cbattery.plist fi
[[ -f "$launch_daemon" ]] && sudo launchctl unload -w "$launch_daemon"
;;
"charging")
log_message "Setting $action to $setting"
case "$setting" in
"on") enable_charging && write_cache "enabled" ;;
"off") disable_charging && write_cache "disabled" ;;
*) echo "Invalid option: $setting" ;;
esac
;;
"adapter")
log_message "Setting $action to $setting"
case "$setting" in
"on") enable_discharging ;;
"off") disable_discharging ;;
*) echo "Invalid option: $setting" ;;
esac
;;
"status")
case "$setting" in
"charging") exit "$(get_smc_charging_hex)" ;;
"cache") get_cached_status ;;
*) echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" ;;
esac
;;
"visudo")
install_visudo_file
;;
"daemon")
install_launch_daemon
;;
*)
echo "Invalid command: $action"
usage
;;
esac
}
runScript "$@"

View File

@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>cbattery</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/cbattery</string>
<string>charging</string>
<string>off</string>
</array>
<key>StandardOutPath</key>
<string>/tmp/cbattery.stdout</string>
<key>StandardErrorPath</key>
<string>/tmp/cbattery.stderr</string>
<key>EnvironmentVariables</key>
<dict>
<key>NO_CACHE</key>
<string>true</string>
</dict>
</dict>
</plist>