hypr/get_events.go

47 lines
800 B
Go
Raw Normal View History

2024-01-05 19:16:58 +01:00
package hypr
import (
"bufio"
"context"
"net"
"os"
"strings"
"git.milar.in/milarin/channel"
"git.milar.in/milarin/slices"
)
func GetEventChannel(ctx context.Context, types ...EventType) (<-chan Event, error) {
conn, err := net.Dial("unix", os.ExpandEnv("/tmp/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"))
if err != nil {
return nil, err
}
out := make(chan Event, 10)
go func() {
defer conn.Close()
<-ctx.Done()
}()
go func() {
defer close(out)
defer conn.Close()
sc := bufio.NewScanner(conn)
for sc.Scan() {
out <- parseEvent(sc.Text())
}
}()
if len(types) == 0 {
return out, nil
}
typeMap := slices.ToStructMap(slices.Map(types, strings.ToLower))
return channel.Filter(out, func(e Event) bool {
_, ok := typeMap[e.Type]
return ok
}), nil
}