-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
317 lines (250 loc) · 6.66 KB
/
client.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package sway
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strings"
"github.com/joshuarubin/lifecycle"
"go.uber.org/multierr"
)
type client struct {
conn net.Conn
path string
}
// A Client provides simple communication with the sway IPC
type Client interface {
// Runs the payload as sway commands
RunCommand(context.Context, string) ([]RunCommandReply, error)
// Get the list of current workspaces
GetWorkspaces(context.Context) ([]Workspace, error)
// Get the list of current outputs
GetOutputs(context.Context) ([]Output, error)
// Get the node layout tree
GetTree(context.Context) (*Node, error)
// Get the names of all the marks currently set
GetMarks(context.Context) ([]string, error)
// Get the list of configured bar IDs
GetBarIDs(context.Context) ([]string, error)
// Get the specified bar config
GetBarConfig(context.Context, string) (*BarConfig, error)
// Get the version of sway that owns the IPC socket
GetVersion(context.Context) (*Version, error)
// Get the list of binding mode names
GetBindingModes(context.Context) ([]string, error)
// Returns the config that was last loaded
GetConfig(context.Context) (*Config, error)
// Sends a tick event with the specified payload
SendTick(context.Context, string) (*TickReply, error)
// Get the list of input devices
GetInputs(context.Context) ([]Input, error)
// Get the list of seats
GetSeats(context.Context) ([]Seat, error)
}
// Option can be passed to New to specify runtime configuration settings
type Option func(*client)
// WithSocketPath explicitly sets the sway socket path so it isn't read from
// $SWAYSOCK
func WithSocketPath(socketPath string) Option {
return func(c *client) {
c.path = socketPath
}
}
// New returns a Client configured to connect to $SWAYSOCK
func New(ctx context.Context, opts ...Option) (_ Client, err error) {
c := &client{}
for _, opt := range opts {
opt(c)
}
if c.path == "" {
c.path = strings.TrimSpace(os.Getenv("SWAYSOCK"))
}
if c.path == "" {
return nil, fmt.Errorf("$SWAYSOCK is empty")
}
if c.conn, err = (&net.Dialer{}).DialContext(ctx, "unix", c.path); err != nil {
return nil, err
}
if lifecycle.Exists(ctx) {
lifecycle.DeferErr(ctx, c.conn.Close)
} else {
go func() {
<-ctx.Done()
_ = c.conn.Close()
}()
}
return c, nil
}
type payloadReader struct {
io.Reader
}
func (r payloadReader) Close() error {
_, err := ioutil.ReadAll(r)
return err
}
func (c *client) recvMsg(ctx context.Context) (*message, error) {
var h header
err := do(ctx, func() error {
return binary.Read(c.conn, binary.LittleEndian, &h)
})
if err != nil {
return nil, err
}
return &message{
Type: h.Type,
Payload: payloadReader{io.LimitReader(c.conn, int64(h.Length))},
}, nil
}
func (c *client) roundTrip(ctx context.Context, t messageType, payload []byte) (*message, error) {
if c == nil {
return nil, fmt.Errorf("not connected")
}
err := do(ctx, func() error {
err := binary.Write(c.conn, binary.LittleEndian, &header{magic, uint32(len(payload)), t})
if err != nil {
return nil
}
_, err = c.conn.Write(payload)
return err
})
if err != nil {
return nil, err
}
return c.recvMsg(ctx)
}
func (c *client) GetTree(ctx context.Context) (*Node, error) {
b, err := c.roundTrip(ctx, messageTypeGetTree, nil)
if err != nil {
return nil, err
}
var n Node
return &n, b.Decode(&n)
}
func (c *client) subscribe(ctx context.Context, events ...EventType) error {
payload, err := json.Marshal(events)
if err != nil {
return err
}
msg, err := c.roundTrip(ctx, messageTypeSubscribe, payload)
if err != nil {
return err
}
var reply struct {
Success bool `json:"success"`
}
if err = msg.Decode(&reply); err != nil {
return err
}
if !reply.Success {
return fmt.Errorf("subscribe unsuccessful")
}
return nil
}
func (c *client) RunCommand(ctx context.Context, command string) ([]RunCommandReply, error) {
msg, err := c.roundTrip(ctx, messageTypeRunCommand, []byte(command))
if err != nil {
return nil, err
}
var replies []RunCommandReply
if err = msg.Decode(&replies); err != nil {
return nil, err
}
for _, reply := range replies {
if !reply.Success {
err = multierr.Append(err, fmt.Errorf("command %q unsuccessful: %v", command, reply.Error))
}
}
return replies, err
}
func (c *client) GetWorkspaces(ctx context.Context) ([]Workspace, error) {
msg, err := c.roundTrip(ctx, messageTypeGetWorkspaces, nil)
if err != nil {
return nil, err
}
var ret []Workspace
return ret, msg.Decode(&ret)
}
func (c *client) GetOutputs(ctx context.Context) ([]Output, error) {
msg, err := c.roundTrip(ctx, messageTypeGetOutputs, nil)
if err != nil {
return nil, err
}
var ret []Output
return ret, msg.Decode(&ret)
}
func (c *client) GetMarks(ctx context.Context) ([]string, error) {
msg, err := c.roundTrip(ctx, messageTypeGetMarks, nil)
if err != nil {
return nil, err
}
var ret []string
return ret, msg.Decode(&ret)
}
func (c *client) GetBarIDs(ctx context.Context) ([]string, error) {
msg, err := c.roundTrip(ctx, messageTypeGetBarConfig, nil)
if err != nil {
return nil, err
}
var ret []string
return ret, msg.Decode(&ret)
}
func (c *client) GetBarConfig(ctx context.Context, id string) (*BarConfig, error) {
msg, err := c.roundTrip(ctx, messageTypeGetBarConfig, []byte(id))
if err != nil {
return nil, err
}
var ret BarConfig
return &ret, msg.Decode(&ret)
}
func (c *client) GetVersion(ctx context.Context) (*Version, error) {
msg, err := c.roundTrip(ctx, messageTypeGetVersion, nil)
if err != nil {
return nil, err
}
var ret Version
return &ret, msg.Decode(&ret)
}
func (c *client) GetBindingModes(ctx context.Context) ([]string, error) {
msg, err := c.roundTrip(ctx, messageTypeGetBindingModes, nil)
if err != nil {
return nil, err
}
var ret []string
return ret, msg.Decode(&ret)
}
func (c *client) GetConfig(ctx context.Context) (*Config, error) {
msg, err := c.roundTrip(ctx, messageTypeGetConfig, nil)
if err != nil {
return nil, err
}
var ret Config
return &ret, msg.Decode(&ret)
}
func (c *client) SendTick(ctx context.Context, payload string) (*TickReply, error) {
msg, err := c.roundTrip(ctx, messageTypeSendTick, []byte(payload))
if err != nil {
return nil, err
}
var ret TickReply
return &ret, msg.Decode(&ret)
}
func (c *client) GetInputs(ctx context.Context) ([]Input, error) {
msg, err := c.roundTrip(ctx, messageTypeGetInputs, nil)
if err != nil {
return nil, err
}
var ret []Input
return ret, msg.Decode(&ret)
}
func (c *client) GetSeats(ctx context.Context) ([]Seat, error) {
msg, err := c.roundTrip(ctx, messageTypeGetSeats, nil)
if err != nil {
return nil, err
}
var ret []Seat
return ret, msg.Decode(&ret)
}