Skip to content

Commit

Permalink
event: import event code from labi-le/hyprland-ipc-client
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Aug 31, 2024
1 parent ca14584 commit 5ef89fc
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 16 deletions.
118 changes: 117 additions & 1 deletion event/event.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hyprland
package event

import (
"fmt"
Expand Down Expand Up @@ -69,3 +69,119 @@ func (c *EventClient) Receive() ([]ReceivedData, error) {

return recv, nil
}

func (c *EventClient) Subscribe(ev EventHandler, events ...EventType) error {
for {
msg, err := c.Receive()
if err != nil {
return err
}

for _, data := range msg {
processEvent(ev, data, events)
}
}
}

func processEvent(ev EventHandler, msg ReceivedData, events []EventType) {
for _, event := range events {
raw := strings.Split(string(msg.Data), ",")
if msg.Type == event {
switch event {
case EventWorkspace:
// e.g. "1" (workspace number)
ev.Workspace(WorkspaceName(raw[0]))
break
case EventFocusedMonitor:
// idk
ev.FocusedMonitor(FocusedMonitor{
MonitorName: MonitorName(raw[0]),
WorkspaceName: WorkspaceName(raw[1]),
})
break
case EventActiveWindow:
// e.g. jetbrains-goland,hyprland-ipc-ipc – main.go
ev.ActiveWindow(ActiveWindow{
Name: raw[0],
Title: raw[1],
})

break
case EventFullscreen:
// e.g. "true" or "false"
ev.Fullscreen(raw[0] == "1")
break
case EventMonitorRemoved:
// e.g. idk
ev.MonitorRemoved(MonitorName(raw[0]))
break
case EventMonitorAdded:
// e.g. idk
ev.MonitorAdded(MonitorName(raw[0]))
break
case EventCreateWorkspace:
// e.g. "1" (workspace number)
ev.CreateWorkspace(WorkspaceName(raw[0]))
break
case EventDestroyWorkspace:
// e.g. "1" (workspace number)
ev.DestroyWorkspace(WorkspaceName(raw[0]))
break
case EventMoveWorkspace:
// e.g. idk
ev.MoveWorkspace(MoveWorkspace{
WorkspaceName: WorkspaceName(raw[0]),
MonitorName: MonitorName(raw[1]),
})
break
case EventActiveLayout:
// e.g. AT Translated Set 2 keyboard,Russian
ev.ActiveLayout(ActiveLayout{
Type: raw[0],
Name: raw[1],
})
break
case EventOpenWindow:
// e.g. 80864f60,1,Alacritty,Alacritty
ev.OpenWindow(OpenWindow{
Address: raw[0],
WorkspaceName: WorkspaceName(raw[1]),
Class: raw[2],
Title: raw[3],
})
break
case EventCloseWindow:
// e.g. 5
ev.CloseWindow(CloseWindow{
Address: raw[0],
})
break
case EventMoveWindow:
// e.g. 5
ev.MoveWindow(MoveWindow{
Address: raw[0],
WorkspaceName: WorkspaceName(raw[1]),
})
break
case EventOpenLayer:
// e.g. wofi
ev.OpenLayer(OpenLayer(raw[0]))
break
case EventCloseLayer:
// e.g. wofi
ev.CloseLayer(CloseLayer(raw[0]))
break
case EventSubMap:
// e.g. idk
ev.SubMap(SubMap(raw[0]))
break
case EventScreencast:
ev.Screencast(Screencast{
Sharing: raw[0] == "1",
Owner: raw[1],
})
break
}
}
}
}
21 changes: 21 additions & 0 deletions event/event_dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package event

type DummyEvHandler struct{}

func (e *DummyEvHandler) Workspace(WorkspaceName) {}
func (e *DummyEvHandler) FocusedMonitor(FocusedMonitor) {}
func (e *DummyEvHandler) ActiveWindow(ActiveWindow) {}
func (e *DummyEvHandler) Fullscreen(bool) {}
func (e *DummyEvHandler) MonitorRemoved(MonitorName) {}
func (e *DummyEvHandler) MonitorAdded(MonitorName) {}
func (e *DummyEvHandler) CreateWorkspace(WorkspaceName) {}
func (e *DummyEvHandler) DestroyWorkspace(WorkspaceName) {}
func (e *DummyEvHandler) MoveWorkspace(MoveWorkspace) {}
func (e *DummyEvHandler) ActiveLayout(ActiveLayout) {}
func (e *DummyEvHandler) OpenWindow(OpenWindow) {}
func (e *DummyEvHandler) CloseWindow(CloseWindow) {}
func (e *DummyEvHandler) MoveWindow(MoveWindow) {}
func (e *DummyEvHandler) OpenLayer(OpenLayer) {}
func (e *DummyEvHandler) CloseLayer(CloseLayer) {}
func (e *DummyEvHandler) SubMap(SubMap) {}
func (e *DummyEvHandler) Screencast(Screencast) {}
115 changes: 105 additions & 10 deletions event/event_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,119 @@
package hyprland
package event

import (
"os"
"testing"
)

var ec *EventClient

func init() {
if os.Getenv("HYPRLAND_INSTANCE_SIGNATURE") != "" {
ec = MustEventClient()
}
var (
h = &FakeEventHandler{}
c = &FakeEventClient{}
)

type FakeEventClient struct {
EventClient
}

func TestReceive(t *testing.T) {
t.Skip("temporary disabled")
type FakeEventHandler struct {
DummyEvHandler
}

func (f *FakeEventClient) Receive() ([]ReceivedData, error) {
return []ReceivedData{
{
Type: EventWorkspace,
Data: "1",
},
{
Type: EventFocusedMonitor,
Data: "1,1",
// TODO I only have one monitor, so I didn't check this
},
{
Type: EventActiveWindow,
Data: "jetbrains-goland,hyprland-ipc-ipc – ipc.go",
},
{
Type: EventFullscreen,
Data: "1",
},
{
Type: EventMonitorRemoved,
Data: "1",
// TODO I only have one monitor, so I didn't check this
},
{
Type: EventMonitorAdded,
Data: "1",
// TODO I only have one monitor, so I didn't check this
},
{
Type: EventCreateWorkspace,
Data: "1",
},
{
Type: EventDestroyWorkspace,
Data: "1",
},

{
Type: EventMoveWorkspace,
Data: "1,1",
// TODO I only have one monitor, so I didn't check this
},
{
Type: EventActiveLayout,
Data: "AT Translated Set 2 keyboard,Russian",
},
{
Type: EventOpenWindow,
Data: "80e62df0,2,jetbrains-goland,win430",
},
{
Type: EventCloseWindow,
Data: "80e62df0",
},
{
Type: EventMoveWindow,
Data: "80e62df0,1",
},
{
Type: EventOpenLayer,
Data: "wofi",
},
{
Type: EventCloseLayer,
Data: "wofi",
},
{
Type: EventSubMap,
Data: "1",
// idk
},
{
Type: EventScreencast,
Data: "1,0",
},
}, nil
}

msg, err := ec.Receive()
func TestSubscribe(t *testing.T) {
err := c.SubscribeWithoutLoop(h, GetAllEvents()...)
if err != nil {
t.Error(err)
}
t.Log(msg)
}

func (c *FakeEventClient) SubscribeWithoutLoop(ev EventHandler, events ...EventType) error {
msg, err := c.Receive()
if err != nil {
return err
}

for _, data := range msg {
processEvent(ev, data, events)
}

return nil
}
Loading

0 comments on commit 5ef89fc

Please sign in to comment.