summaryrefslogtreecommitdiffstats
path: root/roles/hyprland/files/scripts/volume.sh
blob: d2168ce19e4dd93a9a139a7e31fa98d960b66756 (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
#!/bin/bash

get_volume() {
    local status=$(wpctl get-volume @DEFAULT_AUDIO_SINK@)
    local vol=$(echo "$status" | cut -d' ' -f2 | awk '{printf "%d\n", $1 * 100}')
    local muted=false
    [[ "$status" == *"MUTED"* ]] && muted=true
    echo "$muted $vol"
}

get_mic() {
    local status=$(wpctl get-volume @DEFAULT_AUDIO_SOURCE@)
    local vol=$(echo "$status" | cut -d' ' -f2 | awk '{printf "%d\n", $1 * 100}')
    local muted=false
    [[ "$status" == *"MUTED"* ]] && muted=true
    echo "$muted $vol"
}

set_volume() {
    wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ "$1"
}

mute_volume() {
    wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle
}

mute_mic() {
    wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle
}

notify() {
    local label="$1"
    local muted="$2"
    local value="$3"
    if [[ "$label" == "Volume" ]]; then
        local mute_icon=" "
        local icon=" "
    elif [[ "$label" == "Mic" ]]; then
        local mute_icon=" "
        local icon=""
    fi
    if [[ "$muted" == true ]]; then
        notify-send -e \
            -h string:x-canonical-private-synchronous:volume \
            -u low \
            "$label" \
            "$mute_icon  mute"
    else
        notify-send -e \
            -h string:x-canonical-private-synchronous:volume \
            -u low \
            "$label" \
            "$icon  ${value}%"
    fi
}


STEP="${2:-1}"
case "$1" in
    "--get")
        read -r muted volume <<< "$(get_volume)"
        notify "Volume" "$muted" "$volume"
        ;;
    "--set")
        set_volume "${STEP}%"
        read -r muted volume <<< "$(get_volume)"
        notify "Volume" "$muted" "$volume"
        ;;
    "--inc")
        set_volume "${STEP}%+"
        read -r muted volume <<< "$(get_volume)"
        notify "Volume" "$muted" "$volume"
        ;;
    "--dec")
        set_volume "${STEP}%-"
        read -r muted volume <<< "$(get_volume)"
        notify "Volume" "$muted" "$volume"
        ;;
    "--toggle-mute-vol")
        mute_volume
        read -r muted volume <<< "$(get_volume)"
        notify "Volume" "$muted" "$volume"
        ;;
    "--toggle-mute-mic")
        mute_mic
        read -r muted volume <<< "$(get_mic)"
        notify "Mic" "$muted" "$volume"
        ;;
    *)
        echo -e "Usage:"
        echo -e "  --get                 to get current volume value"
        echo -e "  --toggle-mute-vol     to toggle mute output volume"
        echo -e "  --toggle-mute-mic     to toggle mute input mic"
        echo -e "  --set [arg]           to set current volume value to [arg]%"
        echo -e "  --inc [arg]           to increment current volume value by [arg]%"
        echo -e "  --dec [arg]           to decrement current volume value by [arg]%"
        exit 0
        ;;
esac