GetEventChannel implemented
This commit is contained in:
parent
6b31cc8ef5
commit
e4272f8478
46
get_events.go
Normal file
46
get_events.go
Normal file
@ -0,0 +1,46 @@
|
||||
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
|
||||
}
|
5
go.mod
5
go.mod
@ -2,6 +2,9 @@ module git.milar.in/milarin/hypr
|
||||
|
||||
go 1.21.6
|
||||
|
||||
require git.milar.in/milarin/slices v0.0.8
|
||||
require (
|
||||
git.milar.in/milarin/channel v0.1.2
|
||||
git.milar.in/milarin/slices v0.0.8
|
||||
)
|
||||
|
||||
require git.milar.in/milarin/gmath v0.0.3 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -1,3 +1,5 @@
|
||||
git.milar.in/milarin/channel v0.1.2 h1:43nWriSppDxa2dO6Midhy2RymzMz2F/Mp1D6lgapQnQ=
|
||||
git.milar.in/milarin/channel v0.1.2/go.mod h1:We83LTI8S7u7II3pD+A2ChCDWJfCkcBUCUqii9HjTtM=
|
||||
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
|
||||
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
|
||||
git.milar.in/milarin/slices v0.0.8 h1:qN9TE3tkArdTixMKSnwvNPcApwAjxpLVwA5a9k1rm2s=
|
||||
|
64
model_event.go
Normal file
64
model_event.go
Normal file
@ -0,0 +1,64 @@
|
||||
package hypr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Type EventType `json:"type"`
|
||||
Data []string `json:"data"`
|
||||
}
|
||||
|
||||
func parseEvent(str string) Event {
|
||||
data := strings.Split(str, ">>")
|
||||
return Event{
|
||||
Type: EventType(data[0]),
|
||||
Data: strings.Split(data[1], ","),
|
||||
}
|
||||
}
|
||||
|
||||
func (e Event) String() string {
|
||||
data, _ := json.MarshalIndent(e, "", "\t")
|
||||
return string(data)
|
||||
}
|
||||
|
||||
type EventType = string
|
||||
|
||||
const (
|
||||
EventTypeTick EventType = "tick"
|
||||
EventTypeActiveWindow EventType = "activeWindow"
|
||||
EventTypeActiveWindowV2 EventType = "activeWindowV2"
|
||||
EventTypeKeyboardFocus EventType = "keyboardFocus"
|
||||
EventTypeMoveWorkspace EventType = "moveWorkspace"
|
||||
EventTypeFocusedMon EventType = "focusedMon"
|
||||
EventTypeMoveWindow EventType = "moveWindow"
|
||||
EventTypeOpenLayer EventType = "openLayer"
|
||||
EventTypeCloseLayer EventType = "closeLayer"
|
||||
EventTypeOpenWindow EventType = "openWindow"
|
||||
EventTypeCloseWindow EventType = "closeWindow"
|
||||
EventTypeUrgent EventType = "urgent"
|
||||
EventTypeMinimize EventType = "minimize"
|
||||
EventTypeMonitorAdded EventType = "monitorAdded"
|
||||
EventTypeMonitorRemoved EventType = "monitorRemoved"
|
||||
EventTypeCreateWorkspace EventType = "createWorkspace"
|
||||
EventTypeDestroyWorkspace EventType = "destroyWorkspace"
|
||||
EventTypeFullscreen EventType = "fullscreen"
|
||||
EventTypeChangeFloatingMode EventType = "changeFloatingMode"
|
||||
EventTypeWorkspace EventType = "workspace"
|
||||
EventTypeSubmap EventType = "submap"
|
||||
EventTypeMouseMove EventType = "mouseMove"
|
||||
EventTypeMouseButton EventType = "mouseButton"
|
||||
EventTypeMouseAxis EventType = "mouseAxis"
|
||||
EventTypeTouchDown EventType = "touchDown"
|
||||
EventTypeTouchUp EventType = "touchUp"
|
||||
EventTypeTouchMove EventType = "touchMove"
|
||||
EventTypeActiveLayout EventType = "activeLayout"
|
||||
EventTypePreRender EventType = "preRender"
|
||||
EventTypeScreencast EventType = "screencast"
|
||||
EventTypeRender EventType = "render"
|
||||
EventTypeWindowtitle EventType = "windowtitle"
|
||||
EventTypeConfigReloaded EventType = "configReloaded"
|
||||
EventTypePreConfigReload EventType = "preConfigReload"
|
||||
EventTypeKeyPress EventType = "keyPress"
|
||||
)
|
Loading…
Reference in New Issue
Block a user