Big Refactor (#2)

Refactor Everything.

Co-authored-by: Anna Rose Wiggins <annabunches@gmail.com>
Co-committed-by: Anna Rose Wiggins <annabunches@gmail.com>
This commit is contained in:
Anna Rose Wiggins 2025-07-08 03:26:24 +00:00 committed by Anna Rose
parent a0949e719f
commit ff38db6596
21 changed files with 413 additions and 309 deletions

View file

@ -13,11 +13,16 @@ type RuleTarget interface {
// (e.g., inverting the value if Inverted == true)
NormalizeValue(int32) int32
// CreateEvent typically takes the (probably normalized) value and returns an event that can be emitted
// on a virtual device.
//
// MatchEvent returns true if the provided device and input event are a match for this rule target
ValidateEvent(*evdev.InputDevice, *evdev.InputEvent) bool
// CreateEvent creates an event that can be emitted on a virtual device.
// For RuleTargetModeSelect, this method modifies the active mode and returns nil.
//
// TODO: should we normalize inside this function to simplify the interface?
// CreateEvent does not do any error checking; it assumes it is receiving verified and sanitized output.
// The caller is responsible for determining whether an event *should* be emitted.
//
// Typically int32 is the input event's normalized value. *string is the current mode, but is optional
// for most implementations.
CreateEvent(int32, *string) *evdev.InputEvent
}

View file

@ -0,0 +1,27 @@
package mappingrules
import "github.com/holoplot/go-evdev"
// A Simple Mapping Rule can map a button to a button or an axis to an axis.
type MappingRuleAxis struct {
MappingRuleBase
Input *RuleTargetAxis
Output *RuleTargetAxis
}
func NewMappingRuleAxis(base MappingRuleBase, input *RuleTargetAxis, output *RuleTargetAxis) *MappingRuleAxis {
return &MappingRuleAxis{
MappingRuleBase: base,
Input: input,
Output: output,
}
}
func (rule *MappingRuleAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) ||
!rule.Input.MatchEvent(device, event) {
return nil, nil
}
return rule.Output.Device, rule.Output.CreateEvent(rule.Input.NormalizeValue(event.Value), mode)
}

View file

@ -0,0 +1,51 @@
package mappingrules
import (
"time"
"github.com/holoplot/go-evdev"
)
// TODO: This whole file is still WIP
type MappingRuleAxisToButton struct {
MappingRuleBase
Input *RuleTargetAxis
Output *RuleTargetButton
RepeatSpeedMin int32
RepeatSpeedMax int32
lastValue int32
lastEvent time.Time
}
func (rule *MappingRuleAxisToButton) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) ||
!rule.Input.MatchEvent(device, event) {
return nil, nil
}
// set the last value to the normalized input value
rule.lastValue = rule.Input.NormalizeValue(event.Value)
return nil, nil
}
// TimerEvent returns an event when enough time has passed (compared to the last recorded axis value)
// to emit an event.
func (rule *MappingRuleAxisToButton) TimerEvent() *evdev.InputEvent {
// 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.DeadzoneStart {
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
}

View file

@ -7,6 +7,20 @@ type MappingRuleBase struct {
Modes []string
}
func NewMappingRuleBase(
name string,
modes []string,
) MappingRuleBase {
if len(modes) == 0 {
modes = []string{"*"}
}
return MappingRuleBase{
Name: name,
Modes: modes,
}
}
func (rule *MappingRuleBase) modeCheck(mode *string) bool {
if rule.Modes[0] == "*" {
return true

View file

@ -9,13 +9,25 @@ type MappingRuleButton struct {
Output *RuleTargetButton
}
func NewMappingRuleButton(
base MappingRuleBase,
input *RuleTargetButton,
output *RuleTargetButton) *MappingRuleButton {
return &MappingRuleButton{
MappingRuleBase: base,
Input: input,
Output: output,
}
}
func (rule *MappingRuleButton) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil, nil
}
if device != rule.Input.GetDevice() ||
event.Code != rule.Input.GetCode() {
if device != rule.Input.Device ||
event.Code != rule.Input.Button {
return nil, nil
}

View file

@ -3,24 +3,37 @@ package mappingrules
import "github.com/holoplot/go-evdev"
// A Combo Mapping Rule can require multiple physical button presses for a single output button
type MappingRuleCombo struct {
type MappingRuleButtonCombo struct {
MappingRuleBase
Inputs []*RuleTargetButton
Output *RuleTargetButton
State int
}
func (rule *MappingRuleCombo) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
func NewMappingRuleButtonCombo(
base MappingRuleBase,
inputs []*RuleTargetButton,
output *RuleTargetButton) *MappingRuleButtonCombo {
return &MappingRuleButtonCombo{
MappingRuleBase: base,
Inputs: inputs,
Output: output,
State: 0,
}
}
func (rule *MappingRuleButtonCombo) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil, nil
}
// Check each of the inputs, and if we find a match, proceed
var match RuleTarget
var match *RuleTargetButton
for _, input := range rule.Inputs {
if device == input.GetDevice() &&
event.Code == input.GetCode() {
if input.MatchEvent(device, event) {
match = input
break
}
}
@ -40,10 +53,10 @@ func (rule *MappingRuleCombo) MatchEvent(device *evdev.InputDevice, event *evdev
targetState := len(rule.Inputs)
if oldState == targetState-1 && rule.State == targetState {
return rule.Output.GetDevice(), rule.Output.CreateEvent(1, mode)
return rule.Output.Device, rule.Output.CreateEvent(1, mode)
}
if oldState == targetState && rule.State == targetState-1 {
return rule.Output.GetDevice(), rule.Output.CreateEvent(0, mode)
return rule.Output.Device, rule.Output.CreateEvent(0, mode)
}
return nil, nil
}

View file

@ -2,20 +2,33 @@ package mappingrules
import "github.com/holoplot/go-evdev"
type MappingRuleLatched struct {
type MappingRuleButtonLatched struct {
MappingRuleBase
Input *RuleTargetButton
Output *RuleTargetButton
State bool
}
func (rule *MappingRuleLatched) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
func NewMappingRuleButtonLatched(
base MappingRuleBase,
input *RuleTargetButton,
output *RuleTargetButton) *MappingRuleButtonLatched {
return &MappingRuleButtonLatched{
MappingRuleBase: base,
Input: input,
Output: output,
State: false,
}
}
func (rule *MappingRuleButtonLatched) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil, nil
}
if device != rule.Input.Device ||
event.Code != rule.Input.Code ||
event.Code != rule.Input.Button ||
rule.Input.NormalizeValue(event.Value) == 0 {
return nil, nil
}

View file

@ -0,0 +1,40 @@
package mappingrules
import "github.com/holoplot/go-evdev"
type MappingRuleModeSelect struct {
MappingRuleBase
Input *RuleTargetButton
Output *RuleTargetModeSelect
}
func NewMappingRuleModeSelect(
base MappingRuleBase,
input *RuleTargetButton,
output *RuleTargetModeSelect,
) *MappingRuleModeSelect {
return &MappingRuleModeSelect{
MappingRuleBase: base,
Input: input,
Output: output,
}
}
func (rule *MappingRuleModeSelect) MatchEvent(
device *evdev.InputDevice,
event *evdev.InputEvent,
mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil, nil
}
if device != rule.Input.Device ||
event.Code != rule.Input.Button ||
rule.Input.NormalizeValue(event.Value) == 0 {
return nil, nil
}
return nil, rule.Output.CreateEvent(event.Value, mode)
}

View file

@ -1,54 +0,0 @@
package mappingrules
import (
"time"
"github.com/holoplot/go-evdev"
)
// 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 *MappingRuleProportionalAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) (*evdev.InputDevice, *evdev.InputEvent) {
if !rule.MappingRuleBase.modeCheck(mode) {
return nil, nil
}
if device != rule.Input.GetDevice() ||
event.Code != rule.Input.GetCode() {
return nil, nil
}
// set the last value to the normalized input value
rule.LastValue = rule.Input.NormalizeValue(event.Value)
return nil, 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 {
// 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
}

View file

@ -5,35 +5,43 @@ import (
)
type RuleTargetAxis struct {
RuleTargetBase
AxisStart int32
AxisEnd int32
Sensitivity float64
DeviceName string
Device *evdev.InputDevice
Axis evdev.EvCode
Inverted bool
DeadzoneStart int32
DeadzoneEnd int32
Sensitivity float64
}
func NewRuleTargetAxis(device_name string,
device *evdev.InputDevice,
code evdev.EvCode,
axis evdev.EvCode,
inverted bool,
axis_start int32,
axis_end int32,
deadzone_start int32,
deadzone_end int32,
sensitivity float64) *RuleTargetAxis {
return &RuleTargetAxis{
RuleTargetBase: NewRuleTargetBase(device_name, device, code, inverted),
AxisStart: axis_start,
AxisEnd: axis_end,
Sensitivity: sensitivity,
DeviceName: device_name,
Device: device,
Axis: axis,
Inverted: inverted,
DeadzoneStart: deadzone_start,
DeadzoneEnd: deadzone_end,
Sensitivity: sensitivity,
}
}
// TODO: lots of fixes and decisions to make here. Should we normalize all axes to the same range?
// How do we handle deadzones in light of that?
func (target *RuleTargetAxis) NormalizeValue(value int32) int32 {
if !target.Inverted {
return value
}
axisRange := target.AxisEnd - target.AxisStart
axisMid := target.AxisEnd - axisRange/2
axisRange := target.DeadzoneEnd - target.DeadzoneStart
axisMid := target.DeadzoneEnd - axisRange/2
delta := value - axisMid
if delta < 0 {
delta = -delta
@ -55,7 +63,13 @@ func (target *RuleTargetAxis) CreateEvent(value int32, mode *string) *evdev.Inpu
// TODO: oh no we need center deadzones actually...
return &evdev.InputEvent{
Type: evdev.EV_ABS,
Code: target.Code,
Code: target.Axis,
Value: value,
}
}
func (target *RuleTargetAxis) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) bool {
return device == target.Device &&
event.Type == evdev.EV_ABS &&
event.Code == target.Axis
}

View file

@ -1,35 +0,0 @@
package mappingrules
import "github.com/holoplot/go-evdev"
type RuleTargetBase struct {
DeviceName string
Device *evdev.InputDevice
Code evdev.EvCode
Inverted bool
}
func NewRuleTargetBase(device_name string,
device *evdev.InputDevice,
code evdev.EvCode,
inverted bool) RuleTargetBase {
return RuleTargetBase{
DeviceName: device_name,
Device: device,
Code: code,
Inverted: inverted,
}
}
func (target *RuleTargetBase) GetCode() evdev.EvCode {
return target.Code
}
func (target *RuleTargetBase) GetDeviceName() string {
return target.DeviceName
}
func (target *RuleTargetBase) GetDevice() *evdev.InputDevice {
return target.Device
}

View file

@ -3,12 +3,18 @@ package mappingrules
import "github.com/holoplot/go-evdev"
type RuleTargetButton struct {
RuleTargetBase
DeviceName string
Device *evdev.InputDevice
Button evdev.EvCode
Inverted bool
}
func NewRuleTargetButton(device_name string, device *evdev.InputDevice, code evdev.EvCode, inverted bool) *RuleTargetButton {
return &RuleTargetButton{
RuleTargetBase: NewRuleTargetBase(device_name, device, code, inverted),
DeviceName: device_name,
Device: device,
Button: code,
Inverted: inverted,
}
}
@ -22,10 +28,16 @@ func (target *RuleTargetButton) NormalizeValue(value int32) int32 {
return value
}
func (target *RuleTargetButton) CreateEvent(value int32, mode *string) *evdev.InputEvent {
func (target *RuleTargetButton) CreateEvent(value int32, _ *string) *evdev.InputEvent {
return &evdev.InputEvent{
Type: evdev.EV_KEY,
Code: target.Code,
Code: target.Button,
Value: value,
}
}
func (target *RuleTargetButton) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) bool {
return device == target.Device &&
event.Type == evdev.EV_KEY &&
event.Code == target.Button
}

View file

@ -1,6 +1,7 @@
package mappingrules
import (
"errors"
"slices"
"git.annabunches.net/annabunches/joyful/internal/logger"
@ -8,34 +9,35 @@ import (
)
type RuleTargetModeSelect struct {
RuleTargetBase
ModeSelect []string
Modes []string
}
func NewRuleTargetModeSelect(modes []string) *RuleTargetModeSelect {
return &RuleTargetModeSelect{
RuleTargetBase: NewRuleTargetBase("", nil, 0, false),
ModeSelect: modes,
func NewRuleTargetModeSelect(modes []string) (*RuleTargetModeSelect, error) {
if len(modes) == 0 {
return nil, errors.New("cannot create RuleTargetModeSelect: mode list is empty")
}
return &RuleTargetModeSelect{
Modes: modes,
}, nil
}
// RuleTargetModeSelect doesn't make sense as an input type
func (target *RuleTargetModeSelect) NormalizeValue(value int32) int32 {
func (target *RuleTargetModeSelect) NormalizeValue(_ int32) int32 {
return -1
}
func (target *RuleTargetModeSelect) CreateEvent(value int32, mode *string) *evdev.InputEvent {
if value == 0 {
return nil
}
func (target *RuleTargetModeSelect) CreateEvent(_ int32, mode *string) *evdev.InputEvent {
index := 0
if currentMode := slices.Index(target.ModeSelect, *mode); currentMode != -1 {
if currentMode := slices.Index(target.Modes, *mode); currentMode != -1 {
// find the next mode
index = (currentMode + 1) % len(target.ModeSelect)
index = (currentMode + 1) % len(target.Modes)
}
*mode = target.ModeSelect[index]
*mode = target.Modes[index]
logger.Logf("Mode changed to '%s'", *mode)
return nil
}
func (target *RuleTargetModeSelect) MatchEvent(_ *evdev.InputDevice, _ *evdev.InputEvent) bool {
return false
}