brightness-slider.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/bin/bash
  2. # Brightness slider for external monitor using ddcutil
  3. # Monitor configuration
  4. MONITOR_SERIAL="6L1M413"
  5. VCP_CODE=10
  6. # Get current brightness (VCP code 10)
  7. current_output=$(ddcutil getvcp --sn "$MONITOR_SERIAL" "$VCP_CODE" 2>/dev/null)
  8. if [ $? -ne 0 ] || [ -z "$current_output" ]; then
  9. echo "Error: Could not communicate with monitor $MONITOR_SERIAL" >&2
  10. exit 1
  11. fi
  12. # Parse current brightness from output like "VCP code 0x10 (Brightness): current value = 50, max value = 100"
  13. current_percent=$(echo "$current_output" | grep -oP 'current value\s*=\s*\K\d+')
  14. if [ -z "$current_percent" ]; then
  15. echo "Error: Could not parse brightness value" >&2
  16. exit 1
  17. fi
  18. # Round to nearest 5
  19. current_percent=$(((current_percent + 2) / 5 * 5))
  20. # Generate brightness levels
  21. levels=""
  22. for i in {0..100..5}; do
  23. if [ "$i" -eq "$current_percent" ]; then
  24. levels+="● $i%\n"
  25. else
  26. levels+=" $i%\n"
  27. fi
  28. done
  29. # Show menu and get selection
  30. selected=$(echo -e "$levels" | bemenu -l 15 -p "󰃞 Monitor Brightness" --counter always -c -W 0.2 -B 10 \
  31. --fixed-height --fn 'B612 11' \
  32. --bdr "#323232" --tf "#FFFFFF" --hf "#FFFFFF")
  33. # Exit if nothing selected
  34. if [ -z "$selected" ]; then
  35. exit 0
  36. fi
  37. # Extract percentage from selection
  38. percent=$(echo "$selected" | grep -oP '\d+(?=%)')
  39. # Set brightness
  40. if [ -n "$percent" ]; then
  41. ddcutil setvcp --sn "$MONITOR_SERIAL" "$VCP_CODE" "$percent"
  42. fi