waybar-brightness (2023B)
1 #!/usr/bin/env bash 2 3 # --- CONFIGURATION --- 4 DISPLAY_NUM=1 5 STEP=5 6 STATE_FILE="/tmp/waybar_brightness.tmp" 7 # --------------------- 8 9 # Function to send the actual DDC/CI command in the background 10 set_brightness_in_background() { 11 pkill -f "ddcutil.*setvcp 10" 12 (ddcutil --display $DISPLAY_NUM setvcp 10 $1) & 13 } 14 15 # Ensure state file exists, or create it by querying the monitor 16 if [ ! -f "$STATE_FILE" ]; then 17 # ddcutil terse output format: "VCP 10 C <current> <max>" 18 initial_brightness=$(ddcutil --display $DISPLAY_NUM getvcp 10 -t 2>/dev/null | awk '{print $4}') 19 # Fallback to 50 if ddcutil fails or returns non-numeric 20 if ! [[ "$initial_brightness" =~ ^[0-9]+$ ]]; then 21 initial_brightness=50 22 fi 23 echo "$initial_brightness" >"$STATE_FILE" 24 fi 25 26 current=$(cat "$STATE_FILE") 27 # Validate current is a number, fallback to 50 if corrupted 28 if ! [[ "$current" =~ ^[0-9]+$ ]]; then 29 current=50 30 echo "$current" >"$STATE_FILE" 31 fi 32 33 case "$1" in 34 "get") 35 echo "$current" 36 ;; 37 "up") 38 new_brightness=$((current + STEP > 100 ? 100 : current + STEP)) 39 if [ "$current" -ne "$new_brightness" ]; then 40 echo "$new_brightness" >"$STATE_FILE" 41 set_brightness_in_background "$new_brightness" 42 fi 43 pkill -RTMIN+9 waybar # Can add whatever RTMIN+n you please, as long as it's an integer. Make sure the signals match between the config and script. 44 ;; 45 "down") 46 new_brightness=$((current - STEP < 0 ? 0 : current - STEP)) 47 if [ "$current" -ne "$new_brightness" ]; then 48 echo "$new_brightness" >"$STATE_FILE" 49 set_brightness_in_background "$new_brightness" 50 fi 51 pkill -RTMIN+9 waybar 52 ;; 53 "right_click") 54 new_brightness=0 55 if [ "$current" -ne "$new_brightness" ]; then 56 echo "$new_brightness" >"$STATE_FILE" 57 set_brightness_in_background "$new_brightness" 58 fi 59 pkill -RTMIN+9 waybar 60 ;; 61 "left_click") 62 new_brightness=100 63 if [ "$current" -ne "$new_brightness" ]; then 64 echo "$new_brightness" >"$STATE_FILE" 65 set_brightness_in_background "$new_brightness" 66 fi 67 pkill -RTMIN+9 waybar 68 ;; 69 esac