-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.go
528 lines (466 loc) · 14.3 KB
/
request.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package hyprland
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"strings"
"github.com/thiagokokada/hyprland-go/helpers"
"github.com/thiagokokada/hyprland-go/internal/assert"
)
const (
// https://github.com/hyprwm/Hyprland/blob/918d8340afd652b011b937d29d5eea0be08467f5/hyprctl/main.cpp#L278
batch = "[[BATCH]]"
// https://github.com/hyprwm/Hyprland/blob/918d8340afd652b011b937d29d5eea0be08467f5/hyprctl/main.cpp#L257
bufSize = 8192
)
var jsonReqHeader = []byte{'j', '/'}
var reqSep = []byte{' ', ';'}
func prepareRequest(buf *bytes.Buffer, command string, param string, jsonResp bool) int {
if jsonResp {
buf.Write(jsonReqHeader)
}
buf.WriteString(command)
buf.WriteByte(reqSep[0])
buf.WriteString(param)
buf.WriteByte(reqSep[1])
return buf.Len()
}
func prepareRequests(command string, params []string, jsonResp bool) (requests []RawRequest, err error) {
if command == "" {
// Panic since this is not supposed to happen, i.e.: only by
// misuse since this function is internal
panic("empty command")
}
// Buffer that will store the temporary prepared request
buf := bytes.NewBuffer(nil)
bufErr := func() error {
return fmt.Errorf(
"command is too long (%d>=%d): %s",
buf.Len(),
bufSize,
buf.String(),
)
}
switch len(params) {
case 0:
if jsonResp {
buf.Write(jsonReqHeader)
}
buf.WriteString(command)
if buf.Len() > bufSize {
return nil, bufErr()
}
case 1:
if jsonResp {
buf.Write(jsonReqHeader)
}
buf.WriteString(command)
buf.WriteByte(reqSep[0])
buf.WriteString(params[0])
if buf.Len() > bufSize {
return nil, bufErr()
}
default:
// Add [[BATCH]] to the buffer
buf.WriteString(batch)
// Initialise current length of buffer
curLen := buf.Len()
for _, param := range params {
// Get the current command + param length + request
// header and separators
cmdLen := len(command) + len(param) + len(reqSep)
if jsonResp {
cmdLen += len(jsonReqHeader)
}
// If batch + command length is bigger than bufSize,
// return an error since it will not fit the socket
if len(batch)+cmdLen > bufSize {
// Call prepare request for error
prepareRequest(buf, command, param, jsonResp)
return nil, bufErr()
}
// If the current length of the buffer + command +
// param is bigger than bufSize, we will need to split
// the request
if curLen+cmdLen > bufSize {
// Append current buffer contents to the
// requests array
requests = append(requests, buf.Bytes())
// Reset the current buffer and add [[BATCH]]
buf.Reset()
buf.WriteString(batch)
}
// Add the contents of the request to the buffer
curLen = prepareRequest(buf, command, param, jsonResp)
}
}
// Append any remaining buffer content to requests array
requests = append(requests, buf.Bytes())
return requests, nil
}
func parseResponse(raw RawResponse) (response []Response, err error) {
reader := bufio.NewReader(bytes.NewReader(raw))
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
resp := strings.TrimSpace(scanner.Text())
if resp == "" {
continue
}
response = append(response, Response(resp))
}
if err := scanner.Err(); err != nil {
return response, err
}
return response, nil
}
func validateResponse(params []string, response []Response) ([]Response, error) {
// Empty response, something went terrible wrong
if len(response) == 0 {
return []Response{}, fmt.Errorf("%w: empty response", ErrorValidation)
}
// commands without parameters will have at least one return
want := max(len(params), 1)
// we have a different number of requests and responses
if want != len(response) {
return response, fmt.Errorf(
"%w: want responses: %d, got: %d, responses: %v",
ErrorValidation,
want,
len(response),
response,
)
}
// validate that all responses are ok
for i, r := range response {
if r != "ok" {
return response, fmt.Errorf(
"%w: non-ok response from param: %s, response: %s",
ErrorValidation,
params[i],
r,
)
}
}
return response, nil
}
func parseAndValidateResponse(params []string, raw RawResponse) ([]Response, error) {
response, err := parseResponse(raw)
if err != nil {
return response, err
}
return validateResponse(params, response)
}
func unmarshalResponse[T any](response RawResponse, v *T) (T, error) {
if len(response) == 0 {
return *v, errors.New("empty response")
}
err := json.Unmarshal(response, &v)
if err != nil {
return *v, fmt.Errorf(
"error while unmarshal: %w, response: %s",
err,
response,
)
}
return *v, nil
}
func (c *RequestClient) doRequest(command string, params []string, jsonResp bool) (response RawResponse, err error) {
requests, err := prepareRequests(command, params, jsonResp)
if err != nil {
return nil, fmt.Errorf("error while preparing request: %w", err)
}
buf := bytes.NewBuffer(nil)
for _, req := range requests {
resp, err := c.RawRequest(req)
if err != nil {
return nil, fmt.Errorf("error while doing request: %w", err)
}
buf.Write(resp)
}
return buf.Bytes(), nil
}
// Initiate a new client or panic.
// This should be the preferred method for user scripts, since it will
// automatically find the proper socket to connect and use the
// HYPRLAND_INSTANCE_SIGNATURE for the current user.
// If you need to connect to arbitrary user instances or need a method that
// will not panic on error, use [NewClient] instead.
func MustClient() *RequestClient {
return NewClient(
assert.Must1(helpers.GetSocket(helpers.RequestSocket)),
)
}
// Initiate a new client.
// Receive as parameters a requestSocket that is generally localised in
// '$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket.sock'.
func NewClient(socket string) *RequestClient {
return &RequestClient{
conn: &net.UnixAddr{
Net: "unix",
Name: socket,
},
}
}
// Low-level request method, should be avoided unless there is no alternative.
// Receives a byte array as parameter that should be a valid command similar to
// 'hyprctl' command, e.g.: 'hyprctl dispatch exec kitty' will be
// '[]byte("dispatch exec kitty")'.
// Keep in mind that there is no validation. In case of an invalid request, the
// response will generally be something different from "ok".
func (c *RequestClient) RawRequest(request RawRequest) (response RawResponse, err error) {
if len(request) == 0 {
return nil, errors.New("empty request")
}
// Connect to the request socket
conn, err := net.DialUnix("unix", nil, c.conn)
if err != nil {
return nil, fmt.Errorf("error while connecting to socket: %w", err)
}
defer func() {
if e := conn.Close(); e != nil {
err = errors.Join(err, fmt.Errorf("error while closing socket: %w", e))
}
}()
// Send the request to the socket
if len(request) > bufSize {
return nil, fmt.Errorf(
"request too big (%d>%d): %s",
len(request),
bufSize,
request,
)
}
writer := bufio.NewWriter(conn)
_, err = writer.Write(request)
if err != nil {
return nil, fmt.Errorf("error while writing to socket: %w", err)
}
err = writer.Flush()
if err != nil {
return nil, fmt.Errorf("error while flushing to socket: %w", err)
}
// Get the response back
rbuf := bytes.NewBuffer(nil)
sbuf := make([]byte, bufSize)
reader := bufio.NewReader(conn)
for {
n, err := reader.Read(sbuf)
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("error while reading from socket: %w", err)
}
rbuf.Write(sbuf[:n])
if n < bufSize {
break
}
}
return rbuf.Bytes(), err
}
// Active window command, similar to 'hyprctl activewindow'.
// Returns a [Window] object.
func (c *RequestClient) ActiveWindow() (w Window, err error) {
response, err := c.doRequest("activewindow", nil, true)
if err != nil {
return w, err
}
return unmarshalResponse(response, &w)
}
// Get option command, similar to 'hyprctl activeworkspace'.
// Returns a [Workspace] object.
func (c *RequestClient) ActiveWorkspace() (w Workspace, err error) {
response, err := c.doRequest("activeworkspace", nil, true)
if err != nil {
return w, err
}
return unmarshalResponse(response, &w)
}
// Animations command, similar to 'hyprctl animations'.
// Returns a [Animation] object.
func (c *RequestClient) Animations() (a [][]Animation, err error) {
response, err := c.doRequest("animations", nil, true)
if err != nil {
return a, err
}
return unmarshalResponse(response, &a)
}
// Binds command, similar to 'hyprctl binds'.
// Returns a [Bind] object.
func (c *RequestClient) Binds() (b []Bind, err error) {
response, err := c.doRequest("binds", nil, true)
if err != nil {
return b, err
}
return unmarshalResponse(response, &b)
}
// Clients command, similar to 'hyprctl clients'.
// Returns a [Client] object.
func (c *RequestClient) Clients() (cl []Client, err error) {
response, err := c.doRequest("clients", nil, true)
if err != nil {
return cl, err
}
return unmarshalResponse(response, &cl)
}
// ConfigErrors command, similar to `hyprctl configerrors`.
// Returns a [ConfigError] object.
func (c *RequestClient) ConfigErrors() (ce []ConfigError, err error) {
response, err := c.doRequest("configerrors", nil, true)
if err != nil {
return ce, err
}
return unmarshalResponse(response, &ce)
}
// Cursor position command, similar to 'hyprctl cursorpos'.
// Returns a [CursorPos] object.
func (c *RequestClient) CursorPos() (cu CursorPos, err error) {
response, err := c.doRequest("cursorpos", nil, true)
if err != nil {
return cu, err
}
return unmarshalResponse(response, &cu)
}
// Decorations command, similar to `hyprctl decorations`.
// Returns a [Decoration] object.
func (c *RequestClient) Decorations(regex string) (d []Decoration, err error) {
response, err := c.doRequest("decorations", []string{regex}, true)
if err != nil {
return d, err
}
return unmarshalResponse(response, &d)
}
// Devices command, similar to `hyprctl devices`.
// Returns a [Devices] object.
func (c *RequestClient) Devices() (d Devices, err error) {
response, err := c.doRequest("devices", nil, true)
if err != nil {
return d, err
}
return unmarshalResponse(response, &d)
}
// Dispatch commands, similar to 'hyprctl dispatch'.
// Accept multiple commands at the same time, in this case it will use batch
// mode, similar to 'hyprctl dispatch --batch'.
// Returns a [Response] list for each parameter, that may be useful for further
// validations.
func (c *RequestClient) Dispatch(params ...string) (r []Response, err error) {
raw, err := c.doRequest("dispatch", params, false)
if err != nil {
return r, err
}
return parseAndValidateResponse(params, raw)
}
// Get option command, similar to 'hyprctl getoption'.
// Returns an [Option] object.
func (c *RequestClient) GetOption(name string) (o Option, err error) {
response, err := c.doRequest("getoption", []string{name}, true)
if err != nil {
return o, err
}
return unmarshalResponse(response, &o)
}
// Keyword command, similar to 'hyprctl keyword'.
// Accept multiple commands at the same time, in this case it will use batch
// mode, similar to 'hyprctl keyword --batch'.
// Returns a [Response] list for each parameter, that may be useful for further
// validations.
func (c *RequestClient) Keyword(params ...string) (r []Response, err error) {
raw, err := c.doRequest("keyword", params, false)
if err != nil {
return r, err
}
return parseAndValidateResponse(params, raw)
}
// Kill command, similar to 'hyprctl kill'.
// Kill an app by clicking on it, can exit with ESCAPE. Will NOT wait until the
// user to click in the window.
// Returns a [Response], that may be useful for further validations.
func (c *RequestClient) Kill() (r Response, err error) {
raw, err := c.doRequest("kill", nil, true)
if err != nil {
return r, err
}
response, err := parseAndValidateResponse(nil, raw)
return response[0], err // should return only one response
}
// Layer command, similar to 'hyprctl layers'.
// Returns a [Layer] object.
func (c *RequestClient) Layers() (l Layers, err error) {
response, err := c.doRequest("layers", nil, true)
if err != nil {
return l, err
}
return unmarshalResponse(response, &l)
}
// Monitors command, similar to 'hyprctl monitors'.
// Returns a [Monitor] object.
func (c *RequestClient) Monitors() (m []Monitor, err error) {
response, err := c.doRequest("monitors all", nil, true)
if err != nil {
return m, err
}
return unmarshalResponse(response, &m)
}
// Reload command, similar to 'hyprctl reload'.
// Returns a [Response], that may be useful for further validations.
func (c *RequestClient) Reload() (r Response, err error) {
raw, err := c.doRequest("reload", nil, false)
if err != nil {
return r, err
}
response, err := parseAndValidateResponse(nil, raw)
return response[0], err // should return only one response
}
// Set cursor command, similar to 'hyprctl setcursor'.
// Returns a [Response], that may be useful for further validations.
func (c *RequestClient) SetCursor(theme string, size int) (r Response, err error) {
raw, err := c.doRequest("setcursor", []string{fmt.Sprintf("%s %d", theme, size)}, false)
if err != nil {
return r, err
}
response, err := parseAndValidateResponse(nil, raw)
return response[0], err // should return only one response
}
// Set cursor command, similar to 'hyprctl switchxkblayout'.
// Returns a [Response], that may be useful for further validations.
// Param cmd can be either 'next', 'prev' or an ID (e.g: 0).
func (c *RequestClient) SwitchXkbLayout(device string, cmd string) (r Response, err error) {
raw, err := c.doRequest("switchxkblayout", []string{fmt.Sprintf("%s %s", device, cmd)}, false)
if err != nil {
return r, err
}
response, err := parseAndValidateResponse(nil, raw)
return response[0], err // should return only one response
}
// Splash command, similar to 'hyprctl splash'.
func (c *RequestClient) Splash() (s string, err error) {
response, err := c.doRequest("splash", nil, false)
if err != nil {
return s, err
}
return string(response), nil
}
// Version command, similar to 'hyprctl version'.
// Returns a [Version] object.
func (c *RequestClient) Version() (v Version, err error) {
response, err := c.doRequest("version", nil, true)
if err != nil {
return v, err
}
return unmarshalResponse(response, &v)
}
// Workspaces option command, similar to 'hyprctl workspaces'.
// Returns a [Workspace] object.
func (c *RequestClient) Workspaces() (w []Workspace, err error) {
response, err := c.doRequest("workspaces", nil, true)
if err != nil {
return w, err
}
return unmarshalResponse(response, &w)
}