Files
swayipc/swayipc.go

105 lines
2.1 KiB
Go
Raw Normal View History

// Package swayipc can be used to control the sway and swayfx window
// managers ()and possibly i3wm)via a unix domain socket.
2025-08-16 19:50:30 +02:00
package swayipc
2025-08-14 14:16:05 +02:00
import (
"net"
)
const (
VERSION = "v2.1.1"
IpcHeaderSize = 14
IpcMagix = "i3-ipc"
IpcMagicLen = 6
)
2025-08-14 14:16:05 +02:00
// message types
const (
MsgRunCommand = iota
MsgGetWorkspaces
MsgSubscribe
MsgGetOutputs
MsgGettTree
MsgGetMarks
MsgGetBarConfig
MsgGetVersion
MsgGetBindingModes
MsgGetConfig
MsgSendTick
MsgSync
MsgGetBindingState
2025-08-14 14:16:05 +02:00
)
const (
MsgGetInputs = 100
MsgGetSeats = 101
2025-08-14 14:16:05 +02:00
)
// SwayIPC is the primary struct to work with the swayipc module.
2025-08-16 19:50:30 +02:00
type SwayIPC struct {
2025-08-14 14:16:05 +02:00
socket net.Conn
SocketFile string // filename of the i3 IPC socket
2025-08-16 19:50:30 +02:00
Events *Event // store subscribed events, see swayipc.Subscribe()
2025-08-14 14:16:05 +02:00
}
// Rect stores geometrical information, used at various places for geometry etc.
2025-08-14 14:16:05 +02:00
type Rect struct {
X int `json:"x"` // X coordinate
Y int `json:"y"` // Y coordinate
2025-08-14 14:16:05 +02:00
Width int `json:"width"`
Height int `json:"height"`
}
// Response stores meta data retrieved via ipc
2025-08-14 14:16:05 +02:00
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).
2025-08-16 19:50:30 +02:00
func NewSwayIPC(file ...string) *SwayIPC {
ipc := &SwayIPC{}
if len(file) == 0 {
ipc.SocketFile = "SWAYSOCK"
} else {
ipc.SocketFile = file[0]
2025-08-14 14:16:05 +02:00
}
return ipc
2025-08-14 14:16:05 +02:00
}
// get is a wrapper around sendHeader+readResponse
2025-08-16 19:50:30 +02:00
func (ipc *SwayIPC) get(command uint32) (*RawResponse, error) {
err := ipc.sendHeader(command, 0)
2025-08-14 14:16:05 +02:00
if err != nil {
return nil, err
}
payload, err := ipc.readResponse()
if err != nil {
return nil, err
}
return payload, nil
}