sway/client.go

44 lines
627 B
Go
Raw Normal View History

2024-02-14 15:12:39 +01:00
package sway
import (
"errors"
"net"
"os"
"sync"
)
type Client struct {
sync.Mutex
socket string
conn net.Conn
}
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)
}
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
}
func (c *Client) Close() error {
c.Lock()
defer c.Unlock()
return c.conn.Close()
}