Rename EventHandler to Handler
Update README.md file with additional link to the source code
Update README.md file
swayipc
is a Go library that enables interaction with the Sway compositor
using its IPC protocol.
I created this library because I couldn't find anything that met my preferences (which is hard to explain). However, please note that this work is still in progress. Currently, I have only implemented the messages and events that I need for my projects. Adding new functionality should be simple, and I would be grateful to receive contributions.
To install the library, please use the following command:
$ go get gobytes.dev/swayipc
Below is an example
program
that subscribes to sway events. You can also find it in the cmds/subscribe
directory in the source tree.
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"gobytes.dev/swayipc"
)
func handler(ev swayipc.Event) {
switch ev := ev.(type) {
case *swayipc.TickEvent:
fmt.Println(ev.First, ev.Payload)
case *swayipc.WindowEvent:
if ev.Change == "focus" || ev.Change == "title" {
appId := ev.Container.AppId
name := ev.Container.Name
if appId == "" {
appId = ev.Container.WindowProperties.Class
}
appId = " - " + appId
if len(name) > 80 {
name = name[:77] + "..."
}
if !strings.Contains(strings.ToLower(name), strings.ToLower(appId)) {
name = name + appId
}
fmt.Println(name)
}
}
}
func main() {
conn, err := swayipc.Connect(context.Background())
abortOnErr(err)
conn.RegisterEventHandler(swayipc.HandlerFunc(handler))
resp, err := conn.Subscribe(swayipc.WindowEventType, swayipc.TickEventType)
abortOnErr(err)
if resp.Success {
fmt.Println("Successfully subscribed")
}
time.Sleep(3 * time.Minute)
}
func abortOnErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
os.Exit(1)
}
}
Adding new messages and events is fairly easy. You can check this commit to see what is involved. If you have any patches, feel free to send them. I will gladly accept them.