https://github.com/difrex/gosway
Golang Sway IPC bindings
https://github.com/difrex/gosway
golang ipc sway tiling
Last synced: about 1 year ago
JSON representation
Golang Sway IPC bindings
- Host: GitHub
- URL: https://github.com/difrex/gosway
- Owner: Difrex
- License: apache-2.0
- Created: 2019-04-07T13:51:40.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-13T18:43:47.000Z (over 1 year ago)
- Last Synced: 2025-03-29T21:22:30.367Z (about 1 year ago)
- Topics: golang, ipc, sway, tiling
- Language: Go
- Homepage:
- Size: 61.5 KB
- Stars: 13
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.org
- License: LICENSE
Awesome Lists containing this project
README
* GoSway
Golang [[https://github.com/swaywm/sway][Sway]] IPC bindings.
* Install
#+begin_src
go get github.com/Difrex/gosway/ipc
#+end_src
* Usage
Initialize an new connection to the Sway socket
#+begin_src go
import (
"github.com/Difrex/gosway/ipc"
)
sc, err := ipc.NewSwayConnection()
if err != nil {
panic(err)
}
#+end_src
*** Workspaces
Workspaces list
#+begin_src go
ws, err := sc.GetWorkspaces()
if err != nil {
panic(err)
}
for _, workspace := range ws {
fmt.Println(workspace.Name)
}
#+end_src
Get focused workspace
#+begin_src go
ws, err := sc.GetFocusedWorkspace()
if err != nil {
panic(err)
}
#+end_src
Get focused workspace windows
#+begin_src go
windows, err := sc.GetFocusedWorkspaceWindows()
if err != nil {
panic(err)
}
for _, window := range windows {
fmt.Println(window.Name)
}
#+end_src
* Events subscribe
You needs a connection for sending command to the Sway and another one for events listener.
#+begin_src go
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
}
}
#+end_src