summaryrefslogtreecommitdiff
path: root/home-config/sway/scripts.d/project.sh
blob: afb8507136b5c9e397163bc0661c886f00c294fb (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
#!/usr/bin/env bash

get_mode() {
    swaymsg -t get_outputs -r | jq ".[$1].modes[0]"
}

get_name() {
    swaymsg -t get_outputs -r | jq -r ".[$1].name"
}

get_width() {
    get_mode "$1" | jq ".width"
}

get_height() {
    get_mode "$1" | jq ".width"
}

get_refresh() {
    get_mode "$1" | jq ".refresh" | rev | cut -c 4- | rev
}

get_placement() {
    # $1 - monitor placement
    # $2 - monitor index compared
    # $3 - monitor compared scale
    WIDTH="$(jq -n "$(get_width $2)/$3" | awk -F. '{print $1}')"
    case $1 in
        "NONE")
            echo "0 0"
            ;;
        "RIGHT")
            echo "$WIDTH 0"
            ;;
        *)
            echo "0 0"
            ;;
    esac
}

update() {
    # $1 - monitor index
    # $2 - monitor scale
    # $3 - monitor placement
    # $4 - monitor index compared
    # $5 - monitor compared scale
    swaymsg output "$(get_name $1)" "enable"
    swaymsg output "$(get_name $1)" resolution "$(get_width $1)x$(get_height $1)@$(get_refresh $1)hz" scale $2 position "$(get_placement $3 $4 $5)" 
}

off() {
    swaymsg output "$(get_name $1)" "disable"
}

get_id() {
    i=0
    while true; do
        NAME="$(get_name $i)"
        if [ "$NAME" == "$1" ]; then
            echo "$i"
            exit 0
        elif [ "$NAME" == "null" ]; then
            exit 1
        fi
        ((i=i+1))
    done
}

die() {
    notify-send -u critical -t 3000 "Sway" "Failed to get display"
}

PRIMARY=$(get_id "eDP-1")
EXTERNAL=$(get_id "DP-1" || get_id "HDMI-1")

PRIMARY_SCALE=1.5
EXTERNAL_SCALE=1

set_monitors() {
    update $EXTERNAL $EXTERNAL_SCALE $1 $PRIMARY $PRIMARY_SCALE
    update $PRIMARY $PRIMARY_SCALE $2 $EXTERNAL $EXTERNAL_SCALE
}

set_left() {
    set_monitors "NONE" "RIGHT"
}

set_right() {
    set_monitors "RIGHT" "NONE"
}

set_primary_only() {
    set_monitors "NONE" "NONE"
    off $EXTERNAL
}

set_external_only() {
    set_monitors "NONE" "NONE"
    off $PRIMARY
}

case $1 in
    "LEFT")
        set_left
        notify-send -t 3000 "Sway" "External display set to left aligned"
        ;;
    "RIGHT")
        set_right
        notify-send -t 3000 "Sway" "External display set to right aligned"
        ;;
    "PRIMARY_ONLY")
        set_primary_only
        notify-send -t 3000 "Sway" "Set to primary display only"
        ;;
    "EXTERNAL_ONLY")
        set_external_only
        notify-send -t 3000 "Sway" "Set to external display only"
        ;;
esac