Files
swayipc/swayipc.go

105 lines
2.1 KiB
Go

// Package swayipc can be used to control the sway and swayfx window
// managers ()and possibly i3wm)via a unix domain socket.
package swayipc
import (
"net"
)
const (
VERSION = "v2.1.0"
IpcHeaderSize = 14
IpcMagix = "i3-ipc"
IpcMagicLen = 6
)
// message types
const (
MsgRunCommand = iota
MsgGetWorkspaces
MsgSubscribe
MsgGetOutputs
MsgGettTree
MsgGetMarks
MsgGetBarConfig
MsgGetVersion
MsgGetBindingModes
MsgGetConfig
MsgSendTick
MsgSync
MsgGetBindingState
)
const (
MsgGetInputs = 100
MsgGetSeats = 101
)
// SwayIPC is the primary struct to work with the swayipc module.
type SwayIPC struct {
socket net.Conn
SocketFile string // filename of the i3 IPC socket
Events *Event // store subscribed events, see swayipc.Subscribe()
}
// Rect stores geometrical information, used at various places for geometry etc.
type Rect struct {
X int `json:"x"` // X coordinate
Y int `json:"y"` // Y coordinate
Width int `json:"width"`
Height int `json:"height"`
}
// Response stores meta data retrieved via ipc
type Response struct {
Success bool `json:"success"`
ParseError bool `json:"parse_error"`
Error string `json:"error"`
}
// Config stores the user config for the WM
type Config struct {
Config string `json:"config"`
}
// State stores the binding state
type State struct {
Name string `json:"name"`
}
// NewSwayIPC returns a new swayipc.SwayIPC object. Filename argument
// is optional and may denote a filename or the name of an environment
// variable.
//
// By default and if nothing is specified we look for the environment
// variable SWAYSOCK and use the file it points to as unix domain
// socket to communicate with sway (and possible i3).
func NewSwayIPC(file ...string) *SwayIPC {
ipc := &SwayIPC{}
if len(file) == 0 {
ipc.SocketFile = "SWAYSOCK"
} else {
ipc.SocketFile = file[0]
}
return ipc
}
// get is a wrapper around sendHeader+readResponse
func (ipc *SwayIPC) get(command uint32) (*RawResponse, error) {
err := ipc.sendHeader(command, 0)
if err != nil {
return nil, err
}
payload, err := ipc.readResponse()
if err != nil {
return nil, err
}
return payload, nil
}