Merge branch 'actuallymentor:main' into main

This commit is contained in:
Nicola De Rosa
2023-08-25 18:05:01 +02:00
committed by GitHub
4 changed files with 87 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
Development notes:
- logo icons are 20px black/white for theme and 100% vs 50% for active inactive (inside bit only.)
- these are auto-generated by running `node index.js` inside `app/assets`
- deploy new version:
- Update version in `package.json`
- `npm run build`

View File

@@ -130,15 +130,17 @@ const initialize_battery = async () => {
battery_installed,
smc_installed,
charging_in_visudo,
discharging_in_visudo
discharging_in_visudo,
magsafe_led_in_visudo
] = await Promise.all( [
exec_async( `${ path_fix } which battery` ).catch( () => false ),
exec_async( `${ path_fix } which smc` ).catch( () => false ),
exec_async( `${ path_fix } sudo -n /usr/local/bin/smc -k CH0C -r` ).catch( () => false ),
exec_async( `${ path_fix } sudo -n /usr/local/bin/smc -k CH0I -r` ).catch( () => false )
exec_async( `${ path_fix } sudo -n /usr/local/bin/smc -k CH0I -r` ).catch( () => false ),
exec_async( `${ path_fix } sudo -n /usr/local/bin/smc -k ACLC -r` ).catch( () => false )
] )
const visudo_complete = charging_in_visudo && discharging_in_visudo
const visudo_complete = charging_in_visudo && discharging_in_visudo && magsafe_led_in_visudo
const is_installed = battery_installed && smc_installed
log( 'Is installed? ', is_installed )
@@ -161,7 +163,8 @@ const initialize_battery = async () => {
if( !is_installed || !visudo_complete ) {
log( `Installing battery for ${ USER }...` )
if( !online ) return alert( `Battery needs an internet connection to download the latest version, please connect to the internet and open the app again.` )
await alert( `Welcome to the Battery limiting tool. The app needs to install/update some components, so it will ask for your password. This should only be needed once.` )
if( !is_installed ) await alert( `Welcome to the Battery limiting tool. The app needs to install/update some components, so it will ask for your password. This should only be needed once.` )
if( !visudo_complete ) await alert( `Battery needs to apply a backwards incompatible update, to do this it will ask for your password. This should not happen frequently.` )
const result = await exec_sudo_async( `curl -s https://raw.githubusercontent.com/actuallymentor/battery/main/setup.sh | bash -s -- $USER` )
log( `Install result success `, result )
await alert( `Battery background components installed successfully. You can find the battery limiter icon in the top right of your menu bar.` )

View File

@@ -1,6 +1,6 @@
{
"name": "battery",
"version": "1.1.6",
"version": "1.2.0",
"description": "A battery charge limiter for Apple silicon Mac devices",
"main": "main.js",
"build": {
@@ -56,4 +56,4 @@
"electron-store": "^8.1.0",
"update-electron-app": "^2.0.1"
}
}
}

View File

@@ -4,7 +4,7 @@
## Update management
## variables are used by this binary as well at the update script
## ###############
BATTERY_CLI_VERSION="v1.0.13"
BATTERY_CLI_VERSION="v1.1.5"
# Path fixes for unexpected environments
PATH=/bin:/usr/bin:/usr/local/bin:/usr/sbin:/opt/homebrew
@@ -99,10 +99,12 @@ Cmnd_Alias BATTERYOFF = $binfolder/smc -k CH0B -w 02, $binfolder/smc -k CH0
Cmnd_Alias BATTERYON = $binfolder/smc -k CH0B -w 00, $binfolder/smc -k CH0C -w 00
Cmnd_Alias DISCHARGEOFF = $binfolder/smc -k CH0I -w 00, $binfolder/smc -k CH0I -r
Cmnd_Alias DISCHARGEON = $binfolder/smc -k CH0I -w 01
Cmnd_Alias LEDCONTROL = $binfolder/smc -k ACLC -w 04, $binfolder/smc -k ACLC -w 03, $binfolder/smc -k ACLC -w 00, $binfolder/smc -k ACLC -r
ALL ALL = NOPASSWD: BATTERYOFF
ALL ALL = NOPASSWD: BATTERYON
ALL ALL = NOPASSWD: DISCHARGEOFF
ALL ALL = NOPASSWD: DISCHARGEON
ALL ALL = NOPASSWD: LEDCONTROL
"
# Get parameters
@@ -115,14 +117,35 @@ subsetting=$3
## ###############
function log() {
echo -e "$(date +%D-%T) - $1"
}
## ###############
## Statistics
## ###############
## #################
## SMC Manipulation
## #################
# Change magsafe color
# see community sleuthing: https://github.com/actuallymentor/battery/issues/71
function change_magsafe_led_color() {
color=$1
# Check whether user can run color changes without password (required for backwards compatibility)
if sudo -n smc -k ACLC -r &>/dev/null; then
log "💡 Setting magsafe color to $color"
else
log "🚨 Your version of battery is using an old visudo file, please run 'battery visudo' to fix this, until you do battery cannot change magsafe led colors"
return
fi
if [[ "$color" == "green" ]]; then
sudo smc -k ACLC -w 03
elif [[ "$color" == "orange" ]]; then
sudo smc -k ACLC -w 04
else
# Default action: reset. Value 00 is a guess and needs confirmation
sudo smc -k ACLC -w 00
fi
}
# Re:discharging, we're using keys uncovered by @howie65: https://github.com/actuallymentor/battery/issues/20#issuecomment-1364540704
# CH0I seems to be the "disable the adapter" key
@@ -170,6 +193,10 @@ function get_smc_discharging_status() {
fi
}
## ###############
## Statistics
## ###############
function get_battery_percentage() {
battery_percentage=$(pmset -g batt | tail -n1 | awk '{print $3}' | sed s:\%\;::)
echo "$battery_percentage"
@@ -197,16 +224,51 @@ fi
# Visudo message
if [[ "$action" == "visudo" ]]; then
echo -e "$visudoconfig" >>$configfolder/visudo.tmp
sudo visudo -c -f $configfolder/visudo.tmp &>/dev/null
if [ "$?" -eq "0" ]; then
# Write the visudo file to a tempfile
visudo_tmpfile="$configfolder/visudo.tmp"
echo -e "$visudoconfig" >$visudo_tmpfile
# If the visudo file is the same (no error, exit code 0), set the permissions just
if sudo cmp $visudo_file $visudo_tmpfile &>/dev/null; then
echo "The existing battery visudo file is what it should be for version $BATTERY_CLI_VERSION"
# Check if file permissions are correct, if not, set them
current_visudo_file_permissions=$(stat -f "%Lp" $visudo_file)
if [[ "$current_visudo_file_permissions" != "440" ]]; then
sudo chmod 440 $visudo_file
fi
# exit because no changes are needed
exit 0
fi
# Validate that the visudo tempfile is valid
if sudo visudo -c -f $visudo_tmpfile &>/dev/null; then
# If the visudo folder does not exist, make it
if ! test -d "$visudo_folder"; then
sudo mkdir -p "$visudo_folder"
fi
sudo cp $configfolder/visudo.tmp $visudo_file
rm $configfolder/visudo.tmp
# Copy the visudo file from tempfile to live location
sudo cp $visudo_tmpfile $visudo_file
# Delete tempfile
rm $visudo_tmpfile
# Set correct permissions on visudo file
sudo chmod 440 $visudo_file
echo "Visudo file updated successfully"
else
echo "Error validating visudo file, this should never happen:"
sudo visudo -c -f $visudo_tmpfile
fi
sudo chmod 440 $visudo_file
exit 0
fi
@@ -389,11 +451,13 @@ if [[ "$action" == "maintain_synchronous" ]]; then
log "Charge above $setting"
disable_charging
change_magsafe_led_color "green"
elif [[ "$battery_percentage" -lt "$setting" && "$is_charging" == "disabled" ]]; then
log "Charge below $setting"
enable_charging
change_magsafe_led_color "orange"
fi
@@ -421,6 +485,7 @@ if [[ "$action" == "maintain" ]]; then
rm $pidfile 2>/dev/null
battery disable_daemon
enable_charging
change_magsafe_led_color
battery status
exit 0
fi