rename non-camel-case vars, refactor, satisfy linter

This commit is contained in:
2026-08-22 17:47:37 +02:00
parent 9e558e1f3c
commit d64f84c887
13 changed files with 220 additions and 206 deletions

135
event.go
View File

@@ -3,136 +3,105 @@ package swayipc
import (
"encoding/json"
"fmt"
"reflect"
)
// Event types.
const (
EV_Workspace int = 0x80000000
EV_Output int = 0x80000001
EV_Mode int = 0x80000002
EV_Window int = 0x80000003
EV_BarconfigUpdate int = 0x80000004
EV_Binding int = 0x80000005
EV_Shutdown int = 0x80000006
EV_Tick int = 0x80000007
EV_BarStateUpdate int = 0x80000014
EV_Input int = 0x80000015
EvWorkspace int = 0x80000000
EvOutput int = 0x80000001
EvMode int = 0x80000002
EvWindow int = 0x80000003
EvBarconfigUpdate int = 0x80000004
EvBinding int = 0x80000005
EvShutdown int = 0x80000006
EvTick int = 0x80000007
EvBarStateUpdate int = 0x80000014
EvInput int = 0x80000015
)
// Subscriber struct, use this to tell swayipc which events you want to
// subscribe.
// Event is the subscriber struct, use this to tell swayipc which
// events you want to subscribe.
type Event struct {
Workspace bool
Output bool
Mode bool
Window bool
BarconfigUpdate bool
Binding bool
Shutdown bool
Tick bool
BarStateUpdate bool
Input bool
Workspace bool `json:"workspace"`
Output bool `json:"output"`
Mode bool `json:"mode"`
Window bool `json:"window"`
BarconfigUpdate bool `json:"barconfig_update"`
Binding bool `json:"binding"`
Shutdown bool `json:"shutdown"`
Tick bool `json:"tick"`
BarStateUpdate bool `json:"bar_state_update"`
Input bool `json:"input"`
}
// Workspace event response
// EventWorkspace stores a workspace event response
type EventWorkspace struct {
Change string `json:"change"`
Current *Node `json:"workspace"`
Old *Node `json:"old"`
}
// Output event response
// EventOutput stores an output event response
type EventOutput struct {
Change string `json:"change"`
}
// Mode event response
// EventMode stores a ode event response
type EventMode struct {
Change string `json:"change"`
PangoMarkup *Node `json:"pango_markup"`
}
// Window event response
// EventWindow stores a window event response
type EventWindow struct {
Change string `json:"change"`
Container *Node `json:"container"`
}
// BarConfig event response
// EventBarConfig stores a BarConfig event response
type EventBarConfig struct {
Change string `json:"change"`
Binding *Binding `json:"binding"`
}
// Shutdown event response
// EventShutdown stores a Shutdown event response
type EventShutdown struct {
Change string `json:"change"`
}
// Tick event response
// EventTick stores a Tick event response
type EventTick struct {
First bool `json:"first"`
Payload string `json:"payload"`
}
// BarState event response
// EventBarState stores a BarState event response
type EventBarState struct {
Id string `json:"id"`
ID string `json:"id"`
VisibleByModifier bool `json:"visible_by_modifier"`
}
// Input event response
// EventInput stores an Input event response
type EventInput struct {
Change string `json:"change"`
Input *Input `json:"input"`
}
// Subscribe to one or more events. Fill the swayipc.Event object
// accordingly.
// Subscribe allows to subscribe to one or more events. Fill the
// Event object accordingly.
//
// Returns a response list containing a response for every subscribed
// event.
func (ipc *SwayIPC) Subscribe(sub *Event) ([]*Response, error) {
events := []string{}
// looks ugly but makes it much more comfortable for the user
if sub.Workspace {
events = append(events, "workspace")
}
if sub.Output {
events = append(events, "output")
}
if sub.Mode {
events = append(events, "mode")
}
if sub.Window {
events = append(events, "window")
}
if sub.BarconfigUpdate {
events = append(events, "barconfig_update")
}
if sub.Binding {
events = append(events, "binding")
}
if sub.Shutdown {
events = append(events, "shutdown")
}
if sub.Tick {
events = append(events, "tick")
}
if sub.BarStateUpdate {
events = append(events, "bar_state_update")
}
if sub.Input {
events = append(events, "input")
}
events := eventList(sub)
jsonpayload, err := json.Marshal(events)
if err != nil {
return nil, fmt.Errorf("failed to json marshal event list: %w", err)
}
err = ipc.sendHeader(SUBSCRIBE, uint32(len(jsonpayload)))
err = ipc.sendHeader(MsgSubscribe, uint32(len(jsonpayload)))
if err != nil {
return nil, err
}
@@ -158,7 +127,9 @@ func (ipc *SwayIPC) Subscribe(sub *Event) ([]*Response, error) {
return nil, nil
}
// Event loop: Once you have subscribed to an event, you need to enter
// EventLoop starts an event loop.
//
// Once you have subscribed to an event, you need to enter
// an event loop over the same running socket connection. Sway will
// send event payloads for every subscribed event whenever it happens.
//
@@ -181,3 +152,29 @@ func (ipc *SwayIPC) EventLoop(callback func(event *RawResponse) error) error {
}
}
}
// eventList returns a list of subscribe events based on given Event objectstrucstruc
func eventList(sub *Event) []string {
out := []string{}
subv := reflect.ValueOf(sub).Elem()
subType := subv.Type()
for i := range subv.NumField() {
field := subv.Field(i)
structField := subType.Field(i)
value := field.Interface()
key := structField.Name
if tag := structField.Tag.Get(`json`); tag != "" {
key = tag
}
if value.(bool) {
out = append(out, key)
}
}
return out
}