-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
173 lines (145 loc) · 4.13 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"slices"
"strconv"
"strings"
"time"
"github.com/mgnsk/sway-fader/fader"
"github.com/mitchellh/go-ps"
"github.com/spf13/cobra"
"go.i3wm.org/i3/v4"
)
var (
fps = fader.DefaultFPS
defaultTo = fader.DefaultTo
defaultFrom = fader.DefaultFrom
fadeDuration time.Duration = fader.DefaultDuration
appIDTargets []string
classTargets []string
)
func init() {
root.PersistentFlags().Float64Var(&fps, "fps", fps, "Frames per second for the fade")
root.PersistentFlags().Float64Var(&defaultFrom, "default-from", defaultFrom, "Default opacity when fade starts")
root.PersistentFlags().Float64Var(&defaultTo, "default-to", defaultTo, "Default final opacity of fade")
root.PersistentFlags().DurationVarP(&fadeDuration, "duration", "d", fadeDuration, "Duration of the fade")
root.PersistentFlags().StringArrayVar(&appIDTargets, "app_id", appIDTargets, `Override fade settings per container app_id. Format: "regex:from:to". Example: --app_id="foot:0.7:0.97" --app_id="org.telegram.desktop:0.8:1.0"`)
root.PersistentFlags().StringArrayVar(&classTargets, "class", classTargets, `Override fade settings per container class. Format: "regex:from:to". Example: --class="FreeTube:0.7:1.0" --class="Firefox:0.8:1.0"`)
i3.SocketPathHook = getSocketPath
i3.IsRunningHook = isSwayRunning
}
func main() {
if err := root.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err.Error())
os.Exit(1)
}
}
var root = &cobra.Command{
Short: "sway-fader fades in containers on workspace focus and window new events.",
RunE: func(c *cobra.Command, args []string) error {
if !isSwayRunning() {
return fmt.Errorf("could not find running sway process")
}
f, err := newFader()
if err != nil {
return err
}
// Fade in all windows on start.
{
tree, err := i3.GetTree()
if err != nil {
return err
}
walkTree(tree.Root, func(node *i3.Node) bool {
if node.Type == i3.Con {
f.StartFade(node)
}
return true
})
}
r := i3.Subscribe(i3.WorkspaceEventType, i3.WindowEventType)
for r.Next() {
switch ev := r.Event().(type) {
case *i3.WindowEvent:
if ev.Change == "new" {
f.StartFade(&ev.Container)
}
case *i3.WorkspaceEvent:
if ev.Change == "focus" {
walkTree(&ev.Current, func(node *i3.Node) bool {
if node.Type == i3.Con {
f.StartFade(node)
}
return true
})
}
}
}
return r.Close()
},
}
func newFader() (*fader.Fader, error) {
builder := fader.New().WithFPS(fps).WithFadeDuration(fadeDuration)
for _, target := range appIDTargets {
re, from, to, err := parseTarget(target)
if err != nil {
return nil, err
}
builder = builder.WithContainerAppIDFade(re, from, to)
}
for _, target := range classTargets {
re, from, to, err := parseTarget(target)
if err != nil {
return nil, err
}
builder = builder.WithContainerClassFade(re, from, to)
}
return builder.Build(), nil
}
func parseTarget(flagValue string) (selector *regexp.Regexp, from, to float64, err error) {
parts := strings.Split(flagValue, ":")
if len(parts) != 3 {
return nil, 0, 0, fmt.Errorf("invalid number of target components: %s", flagValue)
}
from, err = strconv.ParseFloat(parts[1], 64)
if err != nil {
return nil, 0, 0, fmt.Errorf("invalid from value in target '%s': %w", flagValue, err)
}
to, err = strconv.ParseFloat(parts[2], 64)
if err != nil {
return nil, 0, 0, fmt.Errorf("invalid to value in target '%s': %w", flagValue, err)
}
re, err := regexp.Compile(parts[0])
if err != nil {
return nil, 0, 0, err
}
return re, from, to, nil
}
func walkTree(node *i3.Node, f func(node *i3.Node) bool) bool {
if !f(node) {
return false
}
for _, n := range node.Nodes {
if !walkTree(n, f) {
return false
}
}
return true
}
func isSwayRunning() bool {
procs, err := ps.Processes()
if err != nil {
log.Fatal(err)
}
return slices.ContainsFunc(procs, func(p ps.Process) bool {
return p.Executable() == "sway"
})
}
func getSocketPath() (string, error) {
out, err := exec.Command("sway", "--get-socketpath").CombinedOutput()
return string(out), err
}