Skip to content

Instantly share code, notes, and snippets.

@notpushkin
Created October 26, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notpushkin/00b256c89c60f1eb6d303f285db436f3 to your computer and use it in GitHub Desktop.
Save notpushkin/00b256c89c60f1eb6d303f285db436f3 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Wrapper for brightnessctl, adding:
# - custom maximum value (useful when you override PWM settings)
# - smooooooooooooooooooooooooooooooooothiness
# - desktop notification
#
# Author: Alexander Pushkov <backlit@ale.sh>
# SPDX-License-Identifier: Apache-2.0 OR ISC
DELTA=2
STEP=35
MAX=560
NOTIFICATION_ICON="contrast"
NOTIFICATION_EXPIRE_TIME=3000
NOTIFICATION_ID_PATH="${XDG_RUNTIME_DIR}/backlit_notification_id"
notify() {
touch "${NOTIFICATION_ID_PATH}"
notification_id="$(cat ${NOTIFICATION_ID_PATH})"
if [[ "${notification_id}x" -eq "x" ]]; then
notification_id="0"
fi
gdbus call --session \
--dest org.freedesktop.Notifications \
--object-path /org/freedesktop/Notifications \
--method org.freedesktop.Notifications.Notify \
-- "" "${notification_id}" "${NOTIFICATION_ICON}" "" "" "[]" "{\"value\": <int32 ${1}>}" "int32 ${NOTIFICATION_EXPIRE_TIME}" \
| sed 's/(uint32 \([0-9]\+\),)/\1/g' \
> "${NOTIFICATION_ID_PATH}"
}
case "$1" in
-inc)
notify $(( ( $(brightnessctl --machine-readable | cut -d ',' -f3) + ${DELTA} * ${STEP}) * 100 / "${MAX}" )) &
for i in $(seq "${STEP}"); do
brightnessctl s "+${DELTA}" > /dev/null
done
[[ "$(brightnessctl --machine-readable | cut -d ',' -f3)" -gt "${MAX}" ]] && brightnessctl s "${MAX}"
;;
-dec)
notify $(( ( $(brightnessctl --machine-readable | cut -d ',' -f3) - ${DELTA} * ${STEP}) * 100 / "${MAX}" )) &
[[ "$(brightnessctl --machine-readable | cut -d ',' -f3)" -gt "${MAX}" ]] && brightnessctl s "${MAX}"
for i in $(seq "${STEP}"); do
brightnessctl s "${DELTA}-" > /dev/null
done
;;
*)
echo "Usage: backlit [-inc|-dec]" > /dev/stderr
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment