summaryrefslogtreecommitdiffstats
path: root/roles/hyprland/files/scripts/brightness.sh
blob: 7489d9d9a540c3d90913a5a3f064ac7004cef50c (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

# Original Inspiration:
#  - https://github.com/basecamp/omarchy/issues/2509
#  - https://albert.nz/hyprland-brightness
# ---------------------

# Get focused monitor name and serial (single hyprctl call)
get_focused_display() {
    local monitor=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true)')
    local name=$(echo "$monitor" | jq -r '.name')
    local serial=$(echo "$monitor" | jq -r '.serial')
    echo "$name $serial"
}

set_brightness_ddcutil() {
    local display_serial="$1"
    local step="$2"
    local direction="$3"
    ddcutil \
        --noconfig \
        --skip-ddc-checks \
        --sleep-multiplier=0.1 \
        --sn="$display_serial" \
        setvcp 10 $direction "$step"
}

set_brightness_brightnessctl() {
    local step="$1"
    local direction="$2"
    brightnessctl set "${step}%${direction}"
}

get_brightness_ddcutil() {
    local display_serial="$1"
    ddcutil \
        --noconfig \
        --skip-ddc-checks \
        --sleep-multiplier=0.1 \
        --sn="$display_serial" \
        --terse getvcp 10 \
        | awk '{print $4}'
}

get_brightness_brightnessctl() {
    brightnessctl -m | cut -d, -f4 | sed 's/%//'
}

# Get current brightness for a specific monitor
get_current_brightness() {
    local display_name="$1"
    local display_serial="$2"
    if [ "$display_name" = "eDP-1" ]; then
        get_brightness_brightnessctl
    elif [ -n "$display_serial" ]; then
        get_brightness_ddcutil "$display_serial"
    else
        echo "N/A"
    fi
}

# Set brightness for a specific monitor
set_brightness() {
    local step="$1"
    local direction="$2"
    read -r display_name display_serial <<< "$(get_focused_display)"

    if [ "$display_name" = "eDP-1" ]; then
        set_brightness_brightnessctl "$step" "$direction"
        local value=$(get_brightness_brightnessctl)
    elif [ -n "$display_serial" ]; then
        # Hold a single lock across both set and get to prevent races
        exec 9>/tmp/ddcutil-brightness.lock
        flock -n 9 || return
        set_brightness_ddcutil "$display_serial" "$step" "$direction"
        local value=$(get_brightness_ddcutil "$display_serial")
        exec 9>&-
    else
        return
    fi
    notify "$display_serial" "$display_name" "$value"
}

notify() {
    local serial="$1"
    local name="$2"
    local value="$3"
    notify-send -e \
        -h string:x-canonical-private-synchronous:brightness-"$serial" \
        -u low \
        "Brightness" \
        "   $name: ${value}%"
}

STEP="${2:-1}"
case "$1" in
    "--get")
        read -r display_name display_serial <<< "$(get_focused_display)"
        value=$(get_current_brightness "$display_name" "$display_serial")
        echo "${value}%"
        ;;
    "--set")
        set_brightness "$STEP"
        ;;
    "--inc")
        set_brightness "$STEP" +
        ;;
    "--dec")
        set_brightness "$STEP" -
        ;;
    *)
        echo -e "Usage:"
        echo -e "  --get          to get current brightness value"
        echo -e "  --set [arg]    to set current brightness value to [arg]%"
        echo -e "  --inc [arg]    to increment current brightness value by [arg]%"
        echo -e "  --dec [arg]    to decrement current brightness value by [arg]%"
        exit 0
        ;;
esac