-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
213 lines (170 loc) · 4.15 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
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
package main
import (
"encoding/json"
"fmt"
"net"
"os"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/util/conn"
"github.com/pkg/errors"
"github.com/s-urbaniak/i3-focus-last/i3"
)
func focused(root *i3.Node) *i3.Node {
if root.Focused {
return root
}
for _, node := range root.Nodes {
if f := focused(&node); f != nil {
return f
}
}
return nil
}
func within(d time.Duration, f func() bool) bool {
deadline := time.Now().Add(d)
for {
if time.Now().After(deadline) {
return false
}
if f() {
return true
}
time.Sleep(d / 10)
}
}
type connTaker func(error) (net.Conn, error)
func newConnTaker(cm *conn.Manager, timeout time.Duration) connTaker {
return func(err error) (net.Conn, error) {
var conn net.Conn
if err != nil {
cm.Put(err)
}
if !within(timeout, func() bool {
conn = cm.Take()
return conn != nil
}) {
return nil, errors.New("taking connection timed out")
}
return conn, nil
}
}
func switchWindow(id int, take connTaker) error {
conn, err := take(nil)
if err != nil {
return fmt.Errorf("error taking connection: %v", err)
}
client := i3.NewClient(conn)
if err := client.Command(fmt.Sprintf("[con_id=%d] focus", id)); err != nil {
return fmt.Errorf("focus failed", err)
}
return nil
}
func evLoop(evChan chan []byte, take connTaker, l log.Logger) {
subscribe := func(err error) *i3.Client {
conn, err := take(err)
if err != nil {
l.Log("err", fmt.Errorf("error taking connection: %v", err))
os.Exit(1)
}
client := i3.NewClient(conn)
if err := client.Subscribe("window"); err != nil {
l.Log("err", fmt.Errorf("subscribe failed: %v", err))
os.Exit(1)
}
return client
}
client := subscribe(nil)
for {
_, ev, err := client.Read()
if err != nil {
client = subscribe(fmt.Errorf("error reading event: %v", err))
} else {
evChan <- ev
}
}
}
func newConnectionManager(logger log.Logger) (*conn.Manager, error) {
dialer := func(_, _ string) (net.Conn, error) {
socketpath, err := i3.Socketpath()
if err != nil {
return nil, fmt.Errorf("error creating socketpath: %v", err)
}
return i3.NewConnection(socketpath)
}
return conn.NewManager(dialer, "", "", time.After, log.With(logger, "component", "manager")), nil
}
func main() {
w := log.NewSyncWriter(os.Stderr)
logger := log.NewLogfmtLogger(w)
if len(os.Args) > 1 && os.Args[1] == "switch" {
if err := remoteSwitch(); err != nil {
logger.Log("err", fmt.Errorf("error switching: %v", err))
os.Exit(1)
}
os.Exit(0)
}
switchChan := make(chan struct{})
switchFn := func() { switchChan <- struct{}{} }
go func() {
if err := startServer(switchFn); err != nil {
logger.Log("err", fmt.Errorf("error starting server: %v", err))
os.Exit(1)
}
}()
cm, err := newConnectionManager(logger)
if err != nil {
logger.Log("err", fmt.Errorf("error creating connection manager: %v", err))
os.Exit(1)
}
taker := newConnTaker(cm, 3*time.Second)
conn, err := taker(nil)
if err != nil {
logger.Log("err", fmt.Errorf("error taking connection: %v", err))
os.Exit(1)
}
i3c := i3.NewClient(conn)
root, err := i3c.Tree()
if err != nil {
logger.Log("err", fmt.Errorf("tree command failed: %v", err))
os.Exit(1)
}
history := []int{-1, -1}
if fn := focused(root); fn != nil {
history[1] = fn.ID
}
evChan := make(chan []byte)
go evLoop(evChan, taker, logger)
logger.Log("status", "i3-focus-last started")
for {
select {
case ev := <-evChan:
logger.Log("event", string(ev))
if len(ev) == 0 || ev[0] == '[' {
continue // some other response, not a JSON object
}
evJson := struct {
Change string `json:"change"`
Container struct {
ID int `json:"id"`
} `json:"container"`
}{}
if err := json.Unmarshal(ev, &evJson); err != nil {
logger.Log("err", fmt.Errorf("error unmarshaling event: %v", err))
continue
}
if evJson.Change != "focus" {
continue
}
history[0], history[1] = history[1], evJson.Container.ID
case <-switchChan:
if history[0] < 0 {
continue
}
if err := switchWindow(history[0], taker); err != nil {
logger.Log("err", fmt.Errorf("focus command failed: %v", err))
os.Exit(1)
}
}
}
}