From bec954ef31515b675d41b776bb362a4264179ede Mon Sep 17 00:00:00 2001 From: Milarin Date: Fri, 9 Feb 2024 20:57:18 +0100 Subject: [PATCH] event subscription implemented --- model_event.go | 52 +++++++++++++++++++++++----------------------- subscribe_event.go | 33 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 subscribe_event.go diff --git a/model_event.go b/model_event.go index e68b79b..a73c73a 100644 --- a/model_event.go +++ b/model_event.go @@ -27,38 +27,38 @@ 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" + 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" + EventTypeMonitorAdded EventType = "monitoradded" + EventTypeMonitorRemoved EventType = "monitorremoved" + EventTypeCreateWorkspace EventType = "createworkspace" + EventTypeDestroyWorkspace EventType = "destroyworkspace" EventTypeFullscreen EventType = "fullscreen" - EventTypeChangeFloatingMode EventType = "changeFloatingMode" + 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" + 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" + EventTypeConfigReloaded EventType = "configreloaded" + EventTypePreConfigReload EventType = "preconfigreload" + EventTypeKeyPress EventType = "keypress" ) diff --git a/subscribe_event.go b/subscribe_event.go new file mode 100644 index 0000000..d2d10b6 --- /dev/null +++ b/subscribe_event.go @@ -0,0 +1,33 @@ +package hypr + +import ( + "bufio" + "strings" + + "git.milar.in/milarin/slices" +) + +func (i *Instance) Subscribe(events ...EventType) (<-chan Event, error) { + r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader("")) + if err != nil { + return nil, err + } + + out := make(chan Event, 10) + eventMap := slices.ToStructMap(events) + + go func() { + defer r.Close() + defer close(out) + + sc := bufio.NewScanner(r) + for sc.Scan() { + event := parseEvent(sc.Text()) + if _, ok := eventMap[event.Type]; ok { + out <- event + } + } + }() + + return out, nil +}