Golang Sway IPC bindings.
go get github.com/Difrex/gosway/ipc
Initialize an new connection to the Sway socket
import (
"github.com/Difrex/gosway/ipc"
)
sc, err := ipc.NewSwayConnection()
if err != nil {
panic(err)
}
Workspaces list
ws, err := sc.GetWorkspaces()
if err != nil {
panic(err)
}
for _, workspace := range ws {
fmt.Println(workspace.Name)
}
Get focused workspace
ws, err := sc.GetFocusedWorkspace()
if err != nil {
panic(err)
}
Get focused workspace windows
windows, err := sc.GetFocusedWorkspaceWindows()
if err != nil {
panic(err)
}
for _, window := range windows {
fmt.Println(window.Name)
}
You needs a connection for sending command to the Sway and another one for events listener.
commandConn, err := ipc.NewSwayConnection()
if err != nil {
panic(err)
}
subCon, err := ipc.NewSwayConnection()
if err != nil {
panic(err)
}
// Subscribe only to the window related events
_, err = subCon.SendCommand(ipc.IPC_SUBSCRIBE, `["window"]`)
if err != nil {
panic(err)
}
// Listen for the events
s := subCon.Subscribe()
defer s.Close()
for {
select {
case event := <-s.Events:
if event.Change == "new" {
commandConn.RunSwayCommand(fmt.Sprintf("[con_id=%d] split h", event.Container.ID))
commandConn.RunSwayCommand(fmt.Sprintf("[con_id=%d] move down", event.Container.ID))
}
case err := <-s.Errors:
fmt.Println("Error:", err)
break
}
}