sway/client.go

109 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-02-14 15:12:39 +01:00
package sway
import (
"encoding/binary"
"encoding/json"
2024-02-14 15:12:39 +01:00
"errors"
"fmt"
"io"
2024-02-14 15:12:39 +01:00
"net"
"os"
"sync"
)
2024-02-14 17:21:50 +01:00
// Client is a single connection to a sway socket.
// All requests are synchronized in order to be thread-safe.
// That means they are processed in incoming order.
// If you want to be truly concurrent, use multiple clients
// connected to the same sway socket path.
// Subscriptions are the exception: They use their own socket connection
// so other requests are still possible.
// Subscription connections get closed as soon as the provided context is done.
// Use Client.Close after all requests are processed
// to close the connection to the socket.
// Client.Close does not handle subscriptions.
2024-02-14 15:12:39 +01:00
type Client struct {
sync.Mutex
socket string
conn net.Conn
}
2024-02-14 17:21:50 +01:00
// GetDefaultClient returns a sway client for the current seat.
// It determines the current seat by the SWAYSOCK environment variable
2024-02-14 15:12:39 +01:00
func GetDefaultClient() (*Client, error) {
socket, ok := os.LookupEnv("SWAYSOCK")
if !ok {
return nil, errors.New("could not find sway socket. is $SWAYSOCK set properly?")
}
return GetClientBySocket(socket)
}
2024-02-14 17:21:50 +01:00
// GetClientBySocket returns a sway client for the provided socket.
2024-02-14 15:12:39 +01:00
func GetClientBySocket(socket string) (*Client, error) {
conn, err := net.Dial("unix", socket)
if err != nil {
return nil, err
}
return &Client{
socket: socket,
conn: conn,
}, nil
}
2024-02-14 17:21:50 +01:00
// Close closes the socket connection.
// All requests will return errors after calling Close
2024-02-14 15:12:39 +01:00
func (c *Client) Close() error {
c.Lock()
defer c.Unlock()
return c.conn.Close()
}
func sendMessage[T any](client *Client, messageType uint32, payload string) (T, error) {
client.Lock()
defer client.Unlock()
if _, err := fmt.Fprint(client.conn, "i3-ipc"); err != nil {
return *new(T), err
}
if err := binary.Write(client.conn, binary.LittleEndian, uint32(len(payload))); err != nil {
return *new(T), err
}
if err := binary.Write(client.conn, binary.LittleEndian, messageType); err != nil {
return *new(T), err
}
if _, err := fmt.Fprint(client.conn, payload); err != nil {
return *new(T), err
}
if _, err := client.conn.Read(make([]byte, 6)); err != nil {
return *new(T), err
}
var length uint32
if err := binary.Read(client.conn, binary.LittleEndian, &length); err != nil {
return *new(T), err
}
var responseType uint32
if err := binary.Read(client.conn, binary.LittleEndian, &responseType); err != nil {
return *new(T), err
}
// io.Copy(os.Stdout, client.conn)
// return *new(T), nil
result := new(T)
if err := json.NewDecoder(io.LimitReader(client.conn, int64(length))).Decode(result); err != nil {
return *new(T), err
}
return *result, nil
}