summaryrefslogtreecommitdiff
path: root/common/modules/mango/scripts.nix
blob: 28a388e7e06f8bdc0b9b407564ea63bfb80321fd (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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
{ pkgs, ... }:
let
  volume-control = pkgs.writeShellScriptBin "volume-control" ''
    TAG="audio-volume"

    function get_vol {
      ${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{print int($2 * 100)}'
    }

    case $1 in
      up)   ${pkgs.wireplumber}/bin/wpctl set-volume -l 1.5 @DEFAULT_AUDIO_SINK@ 5%+ ;;
      down) ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%- ;;
      mute) ${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle ;;
    esac

    vol=$(get_vol)
    is_muted=$(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SINK@ | grep "MUTED")

    if [ -n "$is_muted" ]; then
      ${pkgs.dunst}/bin/dunstify -a "Volume" -u low \
        -h string:x-canonical-private-synchronous:$TAG \
        "Volume: Muted"
    else
      ${pkgs.dunst}/bin/dunstify -a "Volume" -u low \
        -h string:x-canonical-private-synchronous:$TAG \
        -h int:value:"$vol" \
        "Volume: $vol%"
    fi
  '';

  brightness-control = pkgs.writeShellScriptBin "brightness-control" ''
    TAG="screen-brightness"

    case $1 in
      up)   ${pkgs.brightnessctl}/bin/brightnessctl set +5% ;;
      down) ${pkgs.brightnessctl}/bin/brightnessctl set 5%- ;;
    esac

    # Get current percentage
    current=$(${pkgs.brightnessctl}/bin/brightnessctl info | grep -oP '\(\K[0-9]+(?=%\))')

    ${pkgs.dunst}/bin/dunstify -a "Brightness" -u low \
      -h string:x-canonical-private-synchronous:$TAG \
      -h int:value:"$current" \
      "Brightness: $current%"
  '';

  mic-control = pkgs.writeShellScriptBin "mic-control" ''
    TAG="audio-mic"

    case $1 in
      toggle) ${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle ;;
    esac

    is_muted=$(${pkgs.wireplumber}/bin/wpctl get-volume @DEFAULT_AUDIO_SOURCE@ | grep "MUTED")

    if [ -n "$is_muted" ]; then
        ${pkgs.dunst}/bin/dunstify -a "Microphone" -u low \
          -h string:x-canonical-private-synchronous:$TAG \
          "Microphone: Muted"
    else
        ${pkgs.dunst}/bin/dunstify -a "Microphone" -u low \
          -h string:x-canonical-private-synchronous:$TAG \
          "Microphone: On"
    fi
  '';
  beesettings = pkgs.writeShellScriptBin "beesettings" ''
    CONFIG_FILE="$HOME/.config/beeSettings"
    WALL_DIR="$HOME/Pictures/Wallpapers"

    init_settings() {
        if [ ! -f "$CONFIG_FILE" ]; then
            mkdir -p "$(dirname "$CONFIG_FILE")"
            echo -e "theme=dark\nnightlight=off\nwallpaper=$WALL_DIR/default.jpg" > "$CONFIG_FILE"
        fi
    }

    get_val() { grep "^$1=" "$CONFIG_FILE" | cut -d'=' -f2; }
    set_val() { sed -i "s|^$1=.*|$1=$2|" "$CONFIG_FILE"; }
    set_layout() { mmsg -l "$1"; }

    apply_all() {
        local theme=$(get_val "theme")
        local wall=$(get_val "wallpaper")
        local mode_flag="dark"
        [ "$theme" == "light" ] && mode_flag="light"

        matugen image "$wall" -m "$mode_flag" -t scheme-content --source-color-index 0 2>/dev/null
        wallust -q -s run "$wall" 2>/dev/null

        dconf write /org/gnome/desktop/interface/color-scheme "'prefer-$mode_flag'"

        if [ -z "$WAYLAND_DISPLAY" ] && [ -n "$DISPLAY" ]; then
            xrdb -merge "$HOME/.Xresources"
        fi
        [ -f "$HOME/.config/sequences" ] && sh "$HOME/.config/sequences"
    }

    cmd_wall() {
        list_walls() {
            for wall in "$WALL_DIR"/*.{jpg,jpeg,png,webp}; do
                [ -f "$wall" ] && echo -en "$(basename "$wall")\0icon\x1f$wall\n"
            done
        }

        SELECTED=$(list_walls | rofi -dmenu -i -p "Wallpaper" \
            -show-icons \
            -theme-str 'element { orientation: vertical; padding: 15px; }' \
            -theme-str 'element-icon { size: 10em; horizontal-align: 0.5; }' \
            -theme-str 'listview { columns: 3; lines: 2; }' \
            -theme-str 'window { width: 50%; }')

        if [ -n "$SELECTED" ]; then
            FULL_PATH="$WALL_DIR/$SELECTED"
            set_val "wallpaper" "$FULL_PATH"
            
            if [ -n "$WAYLAND_DISPLAY" ]; then
                if command -v swww >/dev/null 2>&1; then
                    swww img "$FULL_PATH" --transition-type grow --transition-fps 120 --transition-step 60 --transition-duration 1.5
                else
                    pkill swaybg; swaybg -i "$FULL_PATH" -m fill &
                fi
            else 
                feh --bg-fill "$FULL_PATH"
            fi

            apply_all
        fi
    }

    cmd_settings() {
        local theme=$(get_val "theme")
        local nl=$(get_val "nightlight")

        CHOICE=$(echo -e "Mode: $theme\nNight Light: $nl\nWallpaper\nLayouts" | rofi -dmenu -p "Settings" -i)

        case "$CHOICE" in
            "Mode: "*)
                [ "$theme" == "dark" ] && set_val "theme" "light" || set_val "theme" "dark"
                apply_all
                ;;
            "Night Light: "*)
                if [ "$nl" == "on" ]; then
                    set_val "nightlight" "off"; pkill wlsunset
                else
                    set_val "nightlight" "on"; wlsunset -T 4500 &
                fi
                ;;
            "Wallpaper")
                cmd_wall
                ;;
            "Layouts")
                L=$(echo -e "tile\nmonocle\nscroller\ntgmix\ngrid\ndeck\ncenter tile\nright tile\nvertical scroller\nvertical tile\nvertical grid\nvertical deck" | rofi -dmenu -p "Layout")
                case "$L" in
                    "tile")
                    set_layout "T"
                    ;;
                    "monocle")
                    set_layout "M"
                    ;;
                    "scroller")
                    set_layout "S"
                    ;;
                    "tgmix")
                    set_layout "TG"
                    ;;
                    "grid")
                    set_layout "G"
                    ;;
                    "deck")
                    set_layout "K"
                    ;;
                    "center tile")
                    set_layout "CT"
                    ;;
                    "right tile")
                    set_layout "RT"
                    ;;
                    "vertical grid")
                    set_layout "VG"
                    ;;
                    "vertical tile")
                    set_layout "VT"
                    ;;
                    "vertical deck")
                    set_layout "VK"
                    ;;
                    "vertical scroller")
                    set_layout "VS"
                    ;;
                esac
                ;;
        esac
    }


    init_settings
    case "$1" in
        wall)     cmd_wall ;;
        settings) cmd_settings ;;
        apply)    apply_all ;; 
        *)        echo "Usage: bee {wall|settings|apply}" ;;
    esac
  '';

in
{
  home.packages = [
    volume-control
    brightness-control
    mic-control
    beesettings
  ];
}