2019-04-13 00:31:58 +02:00
|
|
|
package sway
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2019-04-15 04:21:31 +02:00
|
|
|
// EventType is used to choose which events to Subscribe to
|
2019-04-13 00:31:58 +02:00
|
|
|
type EventType string
|
|
|
|
|
|
|
|
const (
|
2019-04-15 04:21:31 +02:00
|
|
|
// 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"
|
2019-04-15 04:21:31 +02:00
|
|
|
|
|
|
|
// 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"
|
|
|
|
)
|
|
|
|
|
2019-04-15 04:21:31 +02:00
|
|
|
// An EventHandler is passed to Subscribe and its methods are called in response
|
|
|
|
// to sway events
|
2019-04-14 08:35:40 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-15 04:21:31 +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{}
|
2019-04-14 08:35:40 +02:00
|
|
|
|
2019-04-15 04:21:31 +02:00
|
|
|
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) {}
|
2019-04-14 08:35:40 +02:00
|
|
|
|
2019-04-15 04:21:31 +02:00
|
|
|
// 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 {
|
2019-04-14 08:35:40 +02:00
|
|
|
msg, err := c.recvMsg(ctx)
|
2019-04-13 00:31:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:35:40 +02:00
|
|
|
processEvent(ctx, handler, msg)
|
2019-04-13 00:31:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:35:40 +02:00
|
|
|
func processEvent(ctx context.Context, h EventHandler, msg *message) {
|
|
|
|
switch msg.Type {
|
2019-04-14 08:13:12 +02:00
|
|
|
case eventTypeWorkspace:
|
2019-04-13 00:31:58 +02:00
|
|
|
var e WorkspaceEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.Workspace(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeMode:
|
|
|
|
var e ModeEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.Mode(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeWindow:
|
2019-04-13 00:31:58 +02:00
|
|
|
var e WindowEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.Window(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeBarConfigUpdate:
|
|
|
|
var e BarConfigUpdateEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.BarConfigUpdate(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeBinding:
|
|
|
|
var e BindingEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.Binding(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeShutdown:
|
2019-04-13 00:31:58 +02:00
|
|
|
var e ShutdownEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.Shutdown(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeTick:
|
|
|
|
var e TickEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.Tick(ctx, e)
|
|
|
|
}
|
|
|
|
case eventTypeBarStatusUpdate:
|
|
|
|
var e BarStatusUpdateEvent
|
2019-04-14 08:35:40 +02:00
|
|
|
if err := msg.Decode(&e); err == nil {
|
2019-04-14 08:13:12 +02:00
|
|
|
h.BarStatusUpdate(ctx, e)
|
|
|
|
}
|
2019-04-13 00:31:58 +02:00
|
|
|
}
|
|
|
|
}
|