Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

4 changed files with 18 additions and 111 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 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) @su $(USER) -c '$(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:"

View File

@ -2,14 +2,11 @@
set -e set -e
TMP_FILE=/tmp/cbattery.status CACHE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/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() { usage() {
cat << EOF 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
@ -20,40 +17,36 @@ EOF
} }
write_cache() { write_cache() {
[[ -n "$NO_CACHE" ]] || echo "$1" > "$TMP_FILE" echo "$1" > "$CACHE_FILE"
} }
read_cache() { read_cache() {
cat "$TMP_FILE" 2> /dev/null cat "$CACHE_FILE" 2> /dev/null
}
log_message() {
timestamp=$(date "+%b %d %H:%M:%S")
echo "$timestamp - $1"
} }
enable_discharging() { enable_discharging() {
log_message "Enabling battery discharging" echo "Enabling battery discharging"
sudo smc -k CH0I -w 01 sudo smc -k CH0I -w 01
} }
disable_discharging() { disable_discharging() {
log_message "Disabling battery discharging" echo "Disabling battery discharging"
sudo smc -k CH0I -w 00 sudo smc -k CH0I -w 00
} }
enable_charging() { enable_charging() {
[[ "$(get_smc_charging_hex)" == "00" ]] && return echo "Enabling battery charging"
log_message "Enabling battery charging"
sudo smc -k CH0B -w 00 sudo smc -k CH0B -w 00
sudo smc -k CH0C -w 00 sudo smc -k CH0C -w 00
disable_discharging
write_cache "enabled"
} }
disable_charging() { disable_charging() {
[[ "$(get_smc_charging_hex)" != "00" ]] && return echo "Disabling battery charging"
log_message "Disabling battery charging"
sudo smc -k CH0B -w 02 sudo smc -k CH0B -w 02
sudo smc -k CH0C -w 02 sudo smc -k CH0C -w 02
write_cache "disabled"
} }
get_smc_charging_hex() { get_smc_charging_hex() {
@ -75,7 +68,7 @@ install_visudo_file() {
echo "'smc' is required but not found." echo "'smc' is required but not found."
exit 1 exit 1
fi fi
sed "s:smc:$smc_loc:g" "$SHARE_DIR/.visudo" | sudo tee "$sudoers_file" > /dev/null sed "s:smc:$smc_loc:g" ".visudo" | sudo tee "$sudoers_file" > /dev/null
if ! sudo visudo -c -f "$sudoers_file"; then if ! sudo visudo -c -f "$sudoers_file"; then
echo "Failed to check sudoers file syntax." echo "Failed to check sudoers file syntax."
sudo rm "$sudoers_file" sudo rm "$sudoers_file"
@ -84,18 +77,6 @@ install_visudo_file() {
sudo chmod 440 "$sudoers_file" sudo chmod 440 "$sudoers_file"
} }
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"
}
get_cached_status() { get_cached_status() {
set +e set +e
read_cache_result=$(read_cache) read_cache_result=$(read_cache)
@ -121,19 +102,17 @@ runScript() {
"revert") "revert")
enable_charging enable_charging
disable_discharging disable_discharging
launch_daemon=/Library/LaunchDaemons/cbattery.plist
[[ -f "$launch_daemon" ]] && sudo launchctl unload -w "$launch_daemon"
;; ;;
"charging") "charging")
log_message "Setting $action to $setting" echo "Setting $action to $setting"
case "$setting" in case "$setting" in
"on") enable_charging && write_cache "enabled" ;; "on") enable_charging ;;
"off") disable_charging && write_cache "disabled" ;; "off") disable_charging ;;
*) echo "Invalid option: $setting" ;; *) echo "Invalid option: $setting" ;;
esac esac
;; ;;
"adapter") "adapter")
log_message "Setting $action to $setting" echo "Setting $action to $setting"
case "$setting" in case "$setting" in
"on") enable_discharging ;; "on") enable_discharging ;;
"off") disable_discharging ;; "off") disable_discharging ;;
@ -143,16 +122,13 @@ runScript() {
"status") "status")
case "$setting" in case "$setting" in
"charging") exit "$(get_smc_charging_hex)" ;; "charging") exit "$(get_smc_charging_hex)" ;;
"cache") get_cached_status ;; "cache") echo "$(get_cached_status)" ;;
*) echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" ;; *) echo "Battery at $(get_battery_percentage)%, smc charging $(get_smc_charging_status)" ;;
esac esac
;; ;;
"visudo") "visudo")
install_visudo_file install_visudo_file
;; ;;
"daemon")
install_launch_daemon
;;
*) *)
echo "Invalid command: $action" echo "Invalid command: $action"
usage usage

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>