Compare commits

..

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

4 changed files with 90 additions and 203 deletions

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,39 +1,20 @@
PREFIX ?= /usr/local
DESTDIR = $(PREFIX)/bin
INSTALL_PATH = $(DESTDIR)/cbattery
SHAREDIR = $(PREFIX)/share/cbattery
DAEMON_PATH = $(SHAREDIR)/cbattery.plist
VISUDO_PATH = $(SHAREDIR)/.visudo
all: install
install: $(INSTALL_PATH) $(DAEMON_PATH) $(VISUDO_PATH)
install: $(INSTALL_PATH)
$(INSTALL_PATH): cbattery
@echo "Installing cbattery to $(DESTDIR)"
@install -d $(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:
@echo "Removing cbattery from $(DESTDIR)"
@$(DESTDIR)/cbattery revert || (echo "Reverting cbattery configurations failed"; exit 1)
@rm -f $(INSTALL_PATH)
@echo "Removing cbattery.plist from $(DAEMON_PATH)"
@rm -f $(DAEMON_PATH)
@echo "Removing .visudo from $(VISUDO_PATH)"
@rm -f $(VISUDO_PATH)
@echo "Removing $(SHAREDIR)"
@rmdir $(SHAREDIR) || true
help:
@echo "Available targets:"

222
cbattery
View File

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

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>