summaryrefslogtreecommitdiff
path: root/.local/bin/waybar-brightness
blob: 138f5ee63dad83ebae58b42b3d537daf69e350d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash

# --- CONFIGURATION ---
DISPLAY_NUM=1
STEP=5
STATE_FILE="/tmp/waybar_brightness.tmp"
# ---------------------

# Function to send the actual DDC/CI command in the background
set_brightness_in_background() {
  pkill -f "ddcutil.*setvcp 10"
  (ddcutil --display $DISPLAY_NUM setvcp 10 $1) &
}

# Ensure state file exists, or create it by querying the monitor
if [ ! -f "$STATE_FILE" ]; then
  # ddcutil terse output format: "VCP 10 C <current> <max>"
  initial_brightness=$(ddcutil --display $DISPLAY_NUM getvcp 10 -t 2>/dev/null | awk '{print $4}')
  # Fallback to 50 if ddcutil fails or returns non-numeric
  if ! [[ "$initial_brightness" =~ ^[0-9]+$ ]]; then
    initial_brightness=50
  fi
  echo "$initial_brightness" >"$STATE_FILE"
fi

current=$(cat "$STATE_FILE")
# Validate current is a number, fallback to 50 if corrupted
if ! [[ "$current" =~ ^[0-9]+$ ]]; then
  current=50
  echo "$current" >"$STATE_FILE"
fi

case "$1" in
"get")
  echo "$current"
  ;;
"up")
  new_brightness=$((current + STEP > 100 ? 100 : current + STEP))
  if [ "$current" -ne "$new_brightness" ]; then
    echo "$new_brightness" >"$STATE_FILE"
    set_brightness_in_background "$new_brightness"
  fi
  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.
  ;;
"down")
  new_brightness=$((current - STEP < 0 ? 0 : current - STEP))
  if [ "$current" -ne "$new_brightness" ]; then
    echo "$new_brightness" >"$STATE_FILE"
    set_brightness_in_background "$new_brightness"
  fi
  pkill -RTMIN+9 waybar
  ;;
"right_click")
  new_brightness=0
  if [ "$current" -ne "$new_brightness" ]; then
    echo "$new_brightness" >"$STATE_FILE"
    set_brightness_in_background "$new_brightness"
  fi
  pkill -RTMIN+9 waybar
  ;;
"left_click")
  new_brightness=100
  if [ "$current" -ne "$new_brightness" ]; then
    echo "$new_brightness" >"$STATE_FILE"
    set_brightness_in_background "$new_brightness"
  fi
  pkill -RTMIN+9 waybar
  ;;
esac