sway/subscribe.go

134 lines
2.7 KiB
Go
Raw Normal View History

2023-10-07 16:21:49 +02:00
package sway
import (
"context"
2024-02-14 15:12:39 +01:00
"encoding/binary"
2023-10-07 16:21:49 +02:00
"encoding/json"
2024-02-14 15:12:39 +01:00
"fmt"
"io"
"net"
"git.milar.in/milarin/slices"
2023-10-07 16:21:49 +02:00
)
2024-02-14 15:12:39 +01:00
func (c *Client) Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) {
conn, err := net.Dial("unix", c.socket)
2023-10-07 16:21:49 +02:00
if err != nil {
return nil, err
}
2024-02-14 15:12:39 +01:00
if _, err := fmt.Fprint(conn, "i3-ipc"); err != nil {
return nil, err
}
2023-10-07 16:21:49 +02:00
2024-02-14 15:12:39 +01:00
payload, err := json.Marshal(slices.Map(events, EventType.String))
2023-10-07 16:21:49 +02:00
if err != nil {
return nil, err
}
2024-02-14 15:12:39 +01:00
if err := binary.Write(conn, binary.LittleEndian, uint32(len(payload))); err != nil {
return nil, err
}
if err := binary.Write(conn, binary.LittleEndian, uint32(2)); err != nil {
return nil, err
}
if _, err := conn.Write(payload); err != nil {
return nil, err
}
if _, err := conn.Read(make([]byte, 6)); err != nil {
return nil, err
}
var length uint32
if err := binary.Read(conn, binary.LittleEndian, &length); err != nil {
return nil, err
}
var messageType uint32
if err := binary.Read(conn, binary.LittleEndian, &messageType); err != nil {
return nil, err
}
result := commandResult{}
if err := json.NewDecoder(io.LimitReader(conn, int64(length))).Decode(&result); err != nil {
return nil, err
}
if result.HasError() {
return nil, result.GetError()
}
return parseEvents(ctx, conn)
}
func parseEvents(ctx context.Context, conn net.Conn) (<-chan Event, error) {
2023-10-07 16:21:49 +02:00
out := make(chan Event, 10)
go func() {
2024-02-14 15:12:39 +01:00
defer conn.Close()
2024-02-14 15:26:17 +01:00
<-ctx.Done()
}()
go func() {
2024-02-14 15:12:39 +01:00
defer close(out)
for ctx.Err() == nil {
if _, err := conn.Read(make([]byte, 6)); err != nil {
2024-02-14 15:26:17 +01:00
return
2024-02-14 15:12:39 +01:00
}
var length uint32
if err := binary.Read(conn, binary.LittleEndian, &length); err != nil {
2024-02-14 15:26:17 +01:00
return
2024-02-14 15:12:39 +01:00
}
var messageType EventType
if err := binary.Read(conn, binary.LittleEndian, &messageType); err != nil {
2024-02-14 15:26:17 +01:00
return
2024-02-14 15:12:39 +01:00
}
event, err := parseEvent(conn, length, messageType)
if err != nil {
2024-02-14 15:26:17 +01:00
return
2024-02-14 15:12:39 +01:00
}
2023-10-07 16:21:49 +02:00
out <- event
}
}()
2024-02-14 15:12:39 +01:00
return out, nil
}
func parseEvent(conn net.Conn, length uint32, messageType EventType) (Event, error) {
var event Event
switch messageType {
case EventTypeWorkspace:
event = &WorkspaceEvent{}
case EventTypeMode:
event = &ModeEvent{}
case EventTypeWindow:
event = &WindowEvent{}
case EventTypeBinding:
event = &BindingEvent{}
case EventTypeShutdown:
event = &ShutdownEvent{}
case EventTypeTick:
event = &TickEvent{}
case EventTypeBarStateUpdate:
event = &BarStateUpdateEvent{}
case EventTypeInput:
event = &InputEvent{}
default:
return nil, fmt.Errorf("invalid event type: %x", messageType)
}
if err := json.NewDecoder(io.LimitReader(conn, int64(length))).Decode(event); err != nil {
2023-10-07 16:21:49 +02:00
return nil, err
}
2024-02-14 15:12:39 +01:00
return event, nil
2023-10-07 16:21:49 +02:00
}