sway/get_outputs.go

48 lines
949 B
Go
Raw Normal View History

2024-02-14 15:26:04 +01:00
package sway
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
)
func (c *Client) GetOutputs(ctx context.Context) ([]Output, error) {
c.Lock()
defer c.Unlock()
if _, err := fmt.Fprint(c.conn, "i3-ipc"); err != nil {
return nil, err
}
if err := binary.Write(c.conn, binary.LittleEndian, uint32(0)); err != nil {
return nil, err
}
if err := binary.Write(c.conn, binary.LittleEndian, uint32(3)); err != nil {
return nil, err
}
if _, err := c.conn.Read(make([]byte, 6)); err != nil {
return nil, err
}
var length uint32
if err := binary.Read(c.conn, binary.LittleEndian, &length); err != nil {
return nil, err
}
var messageType uint32
if err := binary.Read(c.conn, binary.LittleEndian, &messageType); err != nil {
return nil, err
}
results := []Output{}
if err := json.NewDecoder(io.LimitReader(c.conn, int64(length))).Decode(&results); err != nil {
return nil, err
}
return results, nil
}