aboutsummaryrefslogtreecommitdiff
path: root/yoga/bin/setmonitor
diff options
context:
space:
mode:
authorCamil Staps2024-09-06 09:39:25 +0200
committerCamil Staps2024-09-06 09:39:25 +0200
commita63216cc145fb5587d410dc707ed9a1afa75b23d (patch)
tree5ddfb0a4dcb4a0c0fbd645909205594428df129b /yoga/bin/setmonitor
parentUpdate vim spellfiles (diff)
Add key bindings to change external monitor brightness
Diffstat (limited to 'yoga/bin/setmonitor')
-rwxr-xr-xyoga/bin/setmonitor89
1 files changed, 89 insertions, 0 deletions
diff --git a/yoga/bin/setmonitor b/yoga/bin/setmonitor
new file mode 100755
index 0000000..f9fa5c1
--- /dev/null
+++ b/yoga/bin/setmonitor
@@ -0,0 +1,89 @@
+#!/bin/bash
+# Usage:
+# - setmonitor dp-brightness [increase|decrease|min|max]
+# - setmonitor night-light [increase|decrease|day|night]
+set -e
+
+if [ $# -ne 2 ]; then
+ echo 'Check setmonitor source for usage'
+ exit 1
+fi
+
+MONITORS="$(xrandr --listactivemonitors | tail -n +2 | cut -d'+' -f2 | cut -d' ' -f1 | sed 's/^\*//')"
+EXTERNAL_MONITORS="$(echo $MONITORS | tr ' ' '\n' | grep -v eDP)"
+
+# Usage: brightness MONITOR
+brightness () {
+ echo "$(xrandr --verbose | grep "^$1" -A5 | grep Brightness | cut -d':' -f2 | sed 's/\s*//')"
+}
+
+# Usage: dp_brightness [increase|decrease|min|max] MONITOR
+dp_brightness () {
+ STEP=5
+ BRIGHTNESS="$(bc <<< "$(brightness "$2")*100")"
+
+ case "$1" in
+ increase) BRIGHTNESS="$(bc <<< "$BRIGHTNESS+$STEP")" ;;
+ decrease) BRIGHTNESS="$(bc <<< "$BRIGHTNESS-$STEP")" ;;
+ min) BRIGHTNESS=0 ;;
+ max) BRIGHTNESS=100 ;;
+ *)
+ echo "Invalid argument '$1'"
+ exit 1
+ ;;
+ esac
+
+ BRIGHTNESS="$(bc <<< "scale=2; $BRIGHTNESS/100")"
+
+ xrandr --output "$MONITOR" --brightness "$BRIGHTNESS"
+}
+
+# Usage: nightlight [increase|decrease|day|night]
+nightlight () {
+ TMPFILE=/tmp/.night-light-index
+
+ # https://askubuntu.com/a/1061304
+ STEPS=(
+ "1:0.18172716:0.00000001" "1:0.42322816:0.00000001" "1:0.54360078:0.08679949"
+ "1:0.64373109:0.28819679" "1:0.71976951:0.42860152" "1:0.77987699:0.54642268"
+ "1:0.82854786:0.64816570" "1:0.86860704:0.73688797" "1:0.90198230:0.81465502"
+ "1:0.93853986:0.88130458" "1:0.97107439:0.94305985" "1:1:1"
+ )
+
+ CUR=$((${#STEPS[@]}-1))
+ if [ -f $TMPFILE ]; then
+ CUR=$(cat $TMPFILE)
+ fi
+
+ case "$1" in
+ increase) NEW=$(($CUR >= ${#STEPS[@]} ? $CUR : $CUR+1)) ;;
+ decrease) NEW=$(($CUR <= 0 ? $CUR : $CUR-1)) ;;
+ day) NEW=$((${#STEPS[@]}-1)) ;;
+ night) NEW=0 ;;
+ *)
+ echo "Invalid argument '$1'"
+ exit 1
+ ;;
+ esac
+
+ echo $NEW > $TMPFILE
+
+ for MONITOR in $MONITORS; do
+ BRIGHTNESS=$(brightness "$MONITOR")
+ xrandr --output "$MONITOR" --gamma "${STEPS[$NEW]}" --brightness "$BRIGHTNESS" || true
+ done
+}
+
+case "$1" in
+ dp-brightness)
+ for MONITOR in $EXTERNAL_MONITORS; do
+ dp_brightness "$2" "$MONITOR"
+ done
+ ;;
+ night-light)
+ nightlight "$2"
+ ;;
+ *)
+ echo 'Check setmonitor source for usage'
+ exit 1
+esac