39 lines
665 B
Go
39 lines
665 B
Go
package sway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os/exec"
|
|
)
|
|
|
|
func Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) {
|
|
data, err := json.Marshal(events)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cmd := exec.CommandContext(ctx, "swaymsg", "-t", "subscribe", "-m", string(data))
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
out := make(chan Event, 10)
|
|
|
|
go func() {
|
|
defer cmd.Wait()
|
|
dec := json.NewDecoder(stdout)
|
|
var event Event
|
|
for err := dec.Decode(&event); err == nil; err = dec.Decode(&event) {
|
|
out <- event
|
|
}
|
|
}()
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return out, nil
|
|
}
|