events

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package events handles events from Niri event-stream.

Listens to the event stream on the NIRI_SOCKET and reacts to events in a specified manner.

Index

Constants

This section is empty.

Variables

View Source
var EventRegistry = map[string]func() Event{
	"WorkspacesChanged":            func() Event { return &WorkspacesChanged{EName: EName{Name: "WorkspacesChanged"}} },
	"WorkspaceUrgencyChanged":      func() Event { return &WorkspaceUrgencyChanged{EName: EName{Name: "WorkspaceUrgencyChanged"}} },
	"WorkspaceActivated":           func() Event { return &WorkspaceActivated{EName: EName{Name: "WorkspaceActivated"}} },
	"WorkspaceActiveWindowChanged": func() Event { return &WorkspaceActiveWindowChanged{EName: EName{Name: "WorkspaceActiveWindowChanged"}} },
	"WindowsChanged":               func() Event { return &WindowsChanged{EName: EName{Name: "WindowsChanged"}} },
	"WindowOpenedOrChanged":        func() Event { return &WindowOpenedOrChanged{EName: EName{Name: "WindowOpenedOrChanged"}} },
	"WindowClosed":                 func() Event { return &WindowClosed{EName: EName{Name: "WindowClosed"}} },
	"WindowFocusChanged":           func() Event { return &WindowFocusChanged{EName: EName{Name: "WindowFocusChanged"}} },
	"WindowUrgencyChanged":         func() Event { return &WindowUrgencyChanged{EName: EName{Name: "WindowUrgencyChanged"}} },
	"KeyboardLayoutsChanged":       func() Event { return &KeyboardLayoutsChanged{EName: EName{Name: "KeyboardLayoutsChanged"}} },
	"KeyboardLayoutSwitched":       func() Event { return &KeyboardLayoutSwitched{EName: EName{Name: "KeyboardLayoutSwitched"}} },
	"OverviewOpenedOrClosed":       func() Event { return &OverviewOpenedOrClosed{EName: EName{Name: "OverviewOpenedOrClosed"}} },
}

EventRegistry contains all the events Niri currently sends.

The key needs to be the event name, and it should return the correct event model, and set its EName embedded struct. If you know of a better way to handle this, please let me know.

Functions

func ActionsFromRaw added in v0.5.0

func ActionsFromRaw(rawActions map[string]json.RawMessage) []actions.Action

ActionsFromRaw converts the raw actions from the config into a list of Action structs.

func EventStream

func EventStream() (<-chan Event, error)

EventStream listens on the events in Niri event-stream.

The function will use a goroutine to return the event models. Inspiration from: https://github.com/probeldev/niri-float-sticky

func Run

func Run()

Run starts listening on the event stream, and handle the events.

Currently we only handle a few events, `WindowsChanged`, `WindowOpenedOrChanged` and `WindowClosed`. If you need to handle more events, add them in the switch statement. Initially the thought was to support the "Dynamic open-float script, for Bitwarden and other windows that set title/app-id late": https://github.com/YaLTeR/niri/discussions/1599 But it doesn't stop us from handling other types of events as well.

Types

type EName

type EName struct {
	Name string
}

EName defines the name of the event.

func (EName) GetName

func (e EName) GetName() string

GetName returns the event name.

func (EName) GetPossibleKeys added in v0.5.0

func (e EName) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts relevant IDs and fields from any event. This is a default implementation that should work for most events.

type Event

type Event interface {
	GetName() string
	GetPossibleKeys() models.PossibleKeys
}

Event defines the "base" interface for all the events.

NOTE: We have to use GetName, since the field is called Name.

func FromRegistry

func FromRegistry(name string, data []byte) Event

FromRegistry returns the populated model from the EventRegistry by given name.

func ParseEvent

func ParseEvent(event map[string]json.RawMessage) (string, Event, error)

ParseEvent parses the given event into it's struct.

Returns the name, model, error. The name is the name of the event, model is the populated struct.

type KeyboardLayoutSwitched

type KeyboardLayoutSwitched struct {
	EName
	// Idx contains the index of the newly active layout.
	Idx uint8 `json:"idx"`
}

KeyboardLayoutSwitched when the keyboard layout switched.

func (KeyboardLayoutSwitched) GetPossibleKeys added in v0.5.0

func (k KeyboardLayoutSwitched) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the layout index from this event.

type KeyboardLayoutsChanged

type KeyboardLayoutsChanged struct {
	EName
	// KeyboardLayouts contains the new keyboard layout configuration.
	KeyboardLayouts models.KeyboardLayouts `json:"keyboard_layouts"`
}

KeyboardLayoutsChanged when the configured keyboard layouts have changed.

type OverviewOpenedOrClosed

type OverviewOpenedOrClosed struct {
	EName
	// IsOpen contains the new state of the overview.
	IsOpen bool `json:"is_open"`
}

OverviewOpenedOrClosed when the overview was opened or closed.

type WindowClosed

type WindowClosed struct {
	EName
	// ID the ID of the removed window.
	ID uint64 `json:"id"`
}

WindowClosed when a toplevel window was closed.

func (WindowClosed) GetPossibleKeys added in v0.5.0

func (w WindowClosed) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the window ID from this event.

type WindowFocusChanged

type WindowFocusChanged struct {
	EName
	// ID the ID of the newly focused window, or omitted if no window is now focused.
	ID uint64 `json:"id"`
}

WindowFocusChanged when a window focus changed.

All other windows are no longer focused.

func (WindowFocusChanged) GetPossibleKeys added in v0.5.0

func (w WindowFocusChanged) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the window ID from this event.

type WindowOpenedOrChanged

type WindowOpenedOrChanged struct {
	EName
	// Window contains the new or updated window.
	//
	// If the window is focused, all other windows are no longer focused.
	Window *models.Window `json:"window"`
}

WindowOpenedOrChanged when a new toplevel window was opened, or an existing toplevel window changed.

type WindowUrgencyChanged

type WindowUrgencyChanged struct {
	EName
	// ID the ID of the window.
	ID uint64 `json:"id"`
	// Urgent the new urgency state of the window.
	Urgent bool `json:"urgent"`
}

WindowUrgencyChanged when a window urgency changed.

func (WindowUrgencyChanged) GetPossibleKeys added in v0.5.0

func (w WindowUrgencyChanged) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the window ID from this event.

type WindowsChanged

type WindowsChanged struct {
	EName
	// Windows contains the new window configuration.
	//
	// This configuration completely replaces the previous configuration. If any windows
	// are missing from here, then they were closed.
	Windows []*models.Window `json:"windows"`
}

WindowsChanged when the window configuration has changed.

type WorkspaceActivated

type WorkspaceActivated struct {
	EName
	// ID the ID of the newly active workspace.
	ID uint64 `json:"id"`
	// Focused tells if this workspace also became focused.
	//
	// If true, this is now the single focused workspace. All other workspaces are no longer
	// focused, but they may remain active on their respective outputs.
	Focused bool `json:"focused"`
}

WorkspaceActivated when a workspace was activated on an output.

func (WorkspaceActivated) GetPossibleKeys added in v0.5.0

func (w WorkspaceActivated) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the workspace ID from this event.

type WorkspaceActiveWindowChanged

type WorkspaceActiveWindowChanged struct {
	EName
	// WorkspaceID the ID of the workspace on which the active window changed.
	WorkspaceID uint64 `json:"workspace_id"`
	// ActiveWindowID the ID of the new active window, if any.
	ActiveWindowID uint64 `json:"active_window_id"`
}

WorkspaceActiveWindowChanged when an active window changed on a workspace.

func (WorkspaceActiveWindowChanged) GetPossibleKeys added in v0.5.0

func (w WorkspaceActiveWindowChanged) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the workspace and window IDs from this event.

type WorkspaceUrgencyChanged

type WorkspaceUrgencyChanged struct {
	EName
	// ID the ID of the workspace.
	ID uint64 `json:"id"`
	// Urgent tells if this workspace has an urgent window.
	Urgent bool `json:"urgent"`
}

WorkspaceUrgencyChanged when the workspace urgency changed.

func (WorkspaceUrgencyChanged) GetPossibleKeys added in v0.5.0

func (w WorkspaceUrgencyChanged) GetPossibleKeys() models.PossibleKeys

GetPossibleKeys extracts the workspace ID from this event.

type WorkspacesChanged

type WorkspacesChanged struct {
	EName
	// Workspaces contains the new workspace configuration.
	//
	// This configuration completely replaces the previous configuration. If any workspaces
	// are missing from here, then they were deleted.
	Workspaces []*models.Workspace `json:"workspaces"`
}

WorkspacesChanged when the workspace configuration has changed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL