From b9d02e64824b311cd210d8928f9a9c6e6cfccacc Mon Sep 17 00:00:00 2001 From: Anna Rose Wiggins Date: Sat, 5 Jul 2025 18:18:25 -0400 Subject: [PATCH] More refactoring, start implementing proportional axis... --- cmd/joyful/threads.go | 2 +- .../{matching.go => mapping_rules.go} | 65 ++++++++++++++++++- internal/mappingrules/types.go | 38 ----------- 3 files changed, 64 insertions(+), 41 deletions(-) rename internal/mappingrules/{matching.go => mapping_rules.go} (62%) delete mode 100644 internal/mappingrules/types.go diff --git a/cmd/joyful/threads.go b/cmd/joyful/threads.go index d3e270b..d8990cd 100644 --- a/cmd/joyful/threads.go +++ b/cmd/joyful/threads.go @@ -33,7 +33,7 @@ func timerWatcher(rule *mappingrules.MappingRuleProportionalAxis, channel chan<- event := rule.TimerEvent() if event != nil { channel <- ChannelEvent{ - Device: rule.Output.(*mappingrules.RuleTargetModeSelect).Device, + Device: rule.Output.Device, Event: event, Type: ChannelEventTimer, } diff --git a/internal/mappingrules/matching.go b/internal/mappingrules/mapping_rules.go similarity index 62% rename from internal/mappingrules/matching.go rename to internal/mappingrules/mapping_rules.go index a8eb3da..76bdc72 100644 --- a/internal/mappingrules/matching.go +++ b/internal/mappingrules/mapping_rules.go @@ -2,10 +2,46 @@ package mappingrules import ( "slices" + "time" "github.com/holoplot/go-evdev" ) +type MappingRuleBase struct { + Name string + Output RuleTarget + Modes []string +} + +// A Simple Mapping Rule can map a button to a button or an axis to an axis. +type MappingRuleSimple struct { + MappingRuleBase + Input RuleTarget +} + +// A Combo Mapping Rule can require multiple physical button presses for a single output button +type MappingRuleCombo struct { + MappingRuleBase + Inputs []RuleTarget + State int +} + +type MappingRuleLatched struct { + MappingRuleBase + Input RuleTarget + State bool +} + +// TODO: How are we going to implement this? It needs to operate on a timer... +type MappingRuleProportionalAxis struct { + MappingRuleBase + Input *RuleTargetAxis + Output *RuleTargetButton + Sensitivity int32 + LastValue int32 + LastEvent time.Time +} + func (rule *MappingRuleBase) OutputName() string { return rule.Output.GetDeviceName() } @@ -92,13 +128,38 @@ func (rule *MappingRuleLatched) MatchEvent(device *evdev.InputDevice, event *evd } func (rule *MappingRuleProportionalAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) *evdev.InputEvent { - // STUB + if !rule.MappingRuleBase.modeCheck(mode) { + return nil + } + + if device != rule.Input.GetDevice() || + event.Code != rule.Input.GetCode() { + return nil + } + + // set the last value to the normalized input value + rule.LastValue = rule.Input.NormalizeValue(event.Value) return nil } // TimerEvent returns an event when enough time has passed (compared to the last recorded axis value) // to emit an event. func (rule *MappingRuleProportionalAxis) TimerEvent() *evdev.InputEvent { - // STUB + // This is tighter coupling than we'd like, but it will do for now. + // TODO: maybe it would be better to just be more declarative about event types and their inputs and outputs. + if rule.LastValue < rule.Input.AxisStart { + rule.LastEvent = time.Now() + return nil + } + + // calculate target time until next event press + // nextEvent := rule.LastEvent + (rule.LastValue) + + // TODO: figure out what the condition should be + if false { + // TODO: emit event + rule.LastEvent = time.Now() + } + return nil } diff --git a/internal/mappingrules/types.go b/internal/mappingrules/types.go deleted file mode 100644 index 546c1a9..0000000 --- a/internal/mappingrules/types.go +++ /dev/null @@ -1,38 +0,0 @@ -package mappingrules - -import ( - "time" -) - -type MappingRuleBase struct { - Name string - Output RuleTarget - Modes []string -} - -// A Simple Mapping Rule can map a button to a button or an axis to an axis. -type MappingRuleSimple struct { - MappingRuleBase - Input RuleTarget -} - -// A Combo Mapping Rule can require multiple physical button presses for a single output button -type MappingRuleCombo struct { - MappingRuleBase - Inputs []RuleTarget - State int -} - -type MappingRuleLatched struct { - MappingRuleBase - Input RuleTarget - State bool -} - -// TODO: How are we going to implement this? It needs to operate on a timer... -type MappingRuleProportionalAxis struct { - MappingRuleBase - Input RuleTarget - Output RuleTarget - LastEvent time.Time -}