sway/get_workspaces.go

48 lines
958 B
Go
Raw Normal View History

2023-10-07 16:41:59 +02:00
package sway
import (
"context"
2024-02-14 15:12:39 +01:00
"encoding/binary"
2023-10-07 16:41:59 +02:00
"encoding/json"
2024-02-14 15:12:39 +01:00
"fmt"
"io"
2023-10-07 16:41:59 +02:00
)
2024-02-14 15:12:39 +01:00
func (c *Client) GetWorkspaces(ctx context.Context) ([]Workspace, error) {
c.Lock()
defer c.Unlock()
2023-10-07 16:41:59 +02:00
2024-02-14 15:12:39 +01:00
if _, err := fmt.Fprint(c.conn, "i3-ipc"); err != nil {
2023-10-07 16:41:59 +02:00
return nil, err
}
2024-02-14 15:12:39 +01:00
if err := binary.Write(c.conn, binary.LittleEndian, uint32(0)); err != nil {
2023-10-07 16:41:59 +02:00
return nil, err
}
2024-02-14 15:12:39 +01:00
if err := binary.Write(c.conn, binary.LittleEndian, uint32(1)); 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 := []Workspace{}
if err := json.NewDecoder(io.LimitReader(c.conn, int64(length))).Decode(&results); err != nil {
return nil, err
}
return results, nil
2023-10-07 16:41:59 +02:00
}