44 lines
627 B
Go
44 lines
627 B
Go
|
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()
|
||
|
}
|