1
0
mirror of https://github.com/joshuarubin/go-sway synced 2024-11-22 21:11:59 +01:00
go-sway/subscribe.go

155 lines
4.2 KiB
Go
Raw Permalink Normal View History

2019-04-13 00:31:58 +02:00
package sway
import (
"context"
)
// EventType is used to choose which events to Subscribe to
2019-04-13 00:31:58 +02:00
type EventType string
const (
// EventTypeWorkspace is sent whenever an event involving a workspace occurs
// such as initialization of a new workspace or a different workspace gains
// focus
EventTypeWorkspace EventType = "workspace"
// EventTypeMode is sent whenever the binding mode changes
EventTypeMode EventType = "mode"
// EventTypeWindow is sent whenever an event involving a view occurs such as
// being reparented, focused, or closed
EventTypeWindow EventType = "window"
// EventTypeBarConfigUpdate is sent whenever a bar config changes
2019-04-13 00:31:58 +02:00
EventTypeBarConfigUpdate EventType = "barconfig_update"
// EventTypeBinding is sent when a configured binding is executed
EventTypeBinding EventType = "binding"
// EventTypeShutdown is sent when the ipc shuts down because sway is exiting
EventTypeShutdown EventType = "shutdown"
// EventTypeTick is sent when an ipc client sends a SEND_TICK message
EventTypeTick EventType = "tick"
//EventTypeBarStatusUpdate send when the visibility of a bar should change
//due to a modifier
2019-04-13 00:31:58 +02:00
EventTypeBarStatusUpdate EventType = "bar_status_update"
)
// An EventHandler is passed to Subscribe and its methods are called in response
// to sway events
type EventHandler interface {
Workspace(context.Context, WorkspaceEvent)
Mode(context.Context, ModeEvent)
Window(context.Context, WindowEvent)
BarConfigUpdate(context.Context, BarConfigUpdateEvent)
Binding(context.Context, BindingEvent)
Shutdown(context.Context, ShutdownEvent)
Tick(context.Context, TickEvent)
BarStatusUpdate(context.Context, BarStatusUpdateEvent)
2019-04-13 00:31:58 +02:00
}
// NoOpEventHandler is used to help provide empty methods that aren't intended
// to be handled by Subscribe
//
// type handler struct {
// sway.EventHandler
// }
//
// func (h handler) Window(ctx context.Context, e sway.WindowEvent) {
// ...
// }
//
// func main() {
// h := handler{
// EventHandler: sway.NoOpEventHandler(),
// }
//
// ctx := context.Background()
//
// sway.Subscribe(ctx, h, sway.EventTypeWindow)
// }
func NoOpEventHandler() EventHandler {
return noOpEventHandler{}
}
type noOpEventHandler struct{}
func (h noOpEventHandler) Workspace(context.Context, WorkspaceEvent) {}
func (h noOpEventHandler) Mode(context.Context, ModeEvent) {}
func (h noOpEventHandler) Window(context.Context, WindowEvent) {}
func (h noOpEventHandler) BarConfigUpdate(context.Context, BarConfigUpdateEvent) {}
func (h noOpEventHandler) Binding(context.Context, BindingEvent) {}
func (h noOpEventHandler) Shutdown(context.Context, ShutdownEvent) {}
func (h noOpEventHandler) Tick(context.Context, TickEvent) {}
func (h noOpEventHandler) BarStatusUpdate(context.Context, BarStatusUpdateEvent) {}
// Subscribe the IPC connection to the events listed in the payload
2019-04-13 00:31:58 +02:00
func Subscribe(ctx context.Context, handler EventHandler, events ...EventType) error {
n, err := New(ctx)
if err != nil {
return err
}
c := n.(*client)
if err = c.subscribe(ctx, events...); err != nil {
return err
}
for {
msg, err := c.recvMsg(ctx)
2019-04-13 00:31:58 +02:00
if err != nil {
return err
}
processEvent(ctx, handler, msg)
2019-04-13 00:31:58 +02:00
}
}
func processEvent(ctx context.Context, h EventHandler, msg *message) {
switch msg.Type {
case eventTypeWorkspace:
2019-04-13 00:31:58 +02:00
var e WorkspaceEvent
if err := msg.Decode(&e); err == nil {
h.Workspace(ctx, e)
}
case eventTypeMode:
var e ModeEvent
if err := msg.Decode(&e); err == nil {
h.Mode(ctx, e)
}
case eventTypeWindow:
2019-04-13 00:31:58 +02:00
var e WindowEvent
if err := msg.Decode(&e); err == nil {
h.Window(ctx, e)
}
case eventTypeBarConfigUpdate:
var e BarConfigUpdateEvent
if err := msg.Decode(&e); err == nil {
h.BarConfigUpdate(ctx, e)
}
case eventTypeBinding:
var e BindingEvent
if err := msg.Decode(&e); err == nil {
h.Binding(ctx, e)
}
case eventTypeShutdown:
2019-04-13 00:31:58 +02:00
var e ShutdownEvent
if err := msg.Decode(&e); err == nil {
h.Shutdown(ctx, e)
}
case eventTypeTick:
var e TickEvent
if err := msg.Decode(&e); err == nil {
h.Tick(ctx, e)
}
case eventTypeBarStatusUpdate:
var e BarStatusUpdateEvent
if err := msg.Decode(&e); err == nil {
h.BarStatusUpdate(ctx, e)
}
2019-04-13 00:31:58 +02:00
}
}