Compare commits

...

19 Commits
v0.5 ... master

Author SHA1 Message Date
Santiago Lo Coco 0c58b2c27d Read before writing smc 2024-04-10 14:09:05 +02:00
Santiago Lo Coco b54f0fbbfd Add log function 2024-04-10 13:58:21 +02:00
Santiago Lo Coco 9204bc1c07 Revert "Remove useless echo"
This reverts commit 4ced07c359.
2024-04-10 13:35:46 +02:00
Santiago Lo Coco 4ced07c359 Remove useless echo 2024-04-10 13:23:44 +02:00
Santiago Lo Coco 662c7c6dd6 Add LICENSE.md 2024-04-10 12:29:36 +02:00
Santiago Lo Coco 7661855869 Revert "Fix PATH for homebrew"
This reverts commit 8e8dcf8dd4.
2024-04-10 12:29:27 +02:00
Santiago Lo Coco 8e8dcf8dd4 Fix PATH for homebrew 2024-04-10 12:18:00 +02:00
Santiago Lo Coco 324c4ed063 Fix more and more bugs 2024-04-10 11:56:11 +02:00
Santiago Lo Coco b67dc54996 Do not remove files 2024-04-10 11:28:44 +02:00
Santiago Lo Coco d058087729 Use temp file for cache 2024-04-10 11:27:56 +02:00
Santiago Lo Coco 0b3c6bfe92 Fix more bugs 2024-04-10 11:22:26 +02:00
Santiago Lo Coco 3caf0d7df6 Update and fix bugs 2024-04-10 11:16:17 +02:00
Santiago Lo Coco 75871c1618 Fix PATH bugs 2024-04-09 23:43:32 +02:00
Santiago Lo Coco 12f40d39e1 Add cbattery.plist 2024-04-09 23:18:19 +02:00
Santiago Lo Coco b9b96dcc07 Fix bug 2024-04-09 22:34:19 +02:00
Santiago Lo Coco f2c0820ef2 Refactor and add cache 2024-04-09 22:23:10 +02:00
Santiago Lo Coco 1ef1a7825b Fix more bugs 2024-03-17 15:48:33 +01:00
Santiago Lo Coco ed316bb564 Update regex 2024-03-17 15:33:17 +01:00
Santiago Lo Coco 620e539852 Update cbattery 2024-03-17 15:25:16 +01:00
5 changed files with 207 additions and 89 deletions

View File

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

21
LICENSE.md Normal file
View File

@ -0,0 +1,21 @@
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,20 +1,39 @@
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) install: $(INSTALL_PATH) $(DAEMON_PATH) $(VISUDO_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)
@echo "Removing .visudo from $(VISUDO_PATH)"
@rm -f $(VISUDO_PATH)
@echo "Removing $(SHAREDIR)"
@rmdir $(SHAREDIR) || true
help: help:
@echo "Available targets:" @echo "Available targets:"

217
cbattery
View File

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

29
cbattery.plist Normal file
View File

@ -0,0 +1,29 @@
<?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>