package sway import ( "context" "encoding/binary" "encoding/json" "fmt" "io" ) func (c *Client) GetWorkspaces(ctx context.Context) ([]Workspace, 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(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 }