Skip to content

Difrex/gosway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

GoSway

Golang Sway IPC bindings.

Install

go get github.com/Difrex/gosway/ipc

Usage

Initialize an new connection to the Sway socket

import (
	"github.com/Difrex/gosway/ipc"
)

sc, err := ipc.NewSwayConnection()
if err != nil {
    panic(err)
}

Workspaces

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)
}

Events subscribe

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
	}
}