diff --git a/internal/config/rules.go b/internal/config/rules.go index a394372..6822ea7 100644 --- a/internal/config/rules.go +++ b/internal/config/rules.go @@ -109,6 +109,11 @@ func makeLatchedRule(ruleConfig RuleConfig, pDevs map[string]*evdev.InputDevice, func makeRuleTarget(targetConfig RuleTargetConfig, devs map[string]*evdev.InputDevice) (mappingrules.RuleTarget, error) { ruleTarget := mappingrules.RuleTarget{} + if len(targetConfig.ModeSelect) > 0 { + ruleTarget.ModeSelect = targetConfig.ModeSelect + return ruleTarget, nil + } + device, ok := devs[targetConfig.Device] if !ok { return mappingrules.RuleTarget{}, fmt.Errorf("couldn't build rule due to non-existent device '%s'", targetConfig.Device) diff --git a/internal/config/schema.go b/internal/config/schema.go index 60490c8..787bafe 100644 --- a/internal/config/schema.go +++ b/internal/config/schema.go @@ -28,8 +28,9 @@ type RuleConfig struct { } type RuleTargetConfig struct { - Device string `yaml:"device"` - Button string `yaml:"button,omitempty"` - Axis string `yaml:"axis,omitempty"` - Inverted bool `yaml:"inverted,omitempty"` + Device string `yaml:"device,omitempty"` + Button string `yaml:"button,omitempty"` + Axis string `yaml:"axis,omitempty"` + Inverted bool `yaml:"inverted,omitempty"` + ModeSelect []string `yaml:"mode_select,omitempty"` } diff --git a/internal/mappingrules/matching.go b/internal/mappingrules/matching.go index cb58782..8c20dee 100644 --- a/internal/mappingrules/matching.go +++ b/internal/mappingrules/matching.go @@ -19,7 +19,17 @@ func (rule *MappingRuleBase) modeCheck(mode *string) bool { } // eventFromTarget creates an outputtable event from a RuleTarget -func eventFromTarget(output RuleTarget, value int32) *evdev.InputEvent { +func eventFromTarget(output RuleTarget, value int32, mode *string) *evdev.InputEvent { + if len(output.ModeSelect) > 0 { + index := 0 + if currentMode := slices.Index(output.ModeSelect, *mode); currentMode != -1 { + // find the next mode + index = (currentMode + 1) % len(output.ModeSelect) + } + + *mode = output.ModeSelect[index] + return nil + } return &evdev.InputEvent{ Type: output.Type, Code: output.Code, @@ -54,7 +64,7 @@ func (rule *SimpleMappingRule) MatchEvent(device *evdev.InputDevice, event *evde return nil } - if event.Type == evdev.EV_KEY { + if event.Type != evdev.EV_ABS { logger.Logf("DEBUG: mode check passed for rule '%s'. Mode '%s' modes '%v'", rule.Name, *mode, rule.Modes) } @@ -63,7 +73,7 @@ func (rule *SimpleMappingRule) MatchEvent(device *evdev.InputDevice, event *evde return nil } - return eventFromTarget(rule.Output, valueFromTarget(rule.Input, event)) + return eventFromTarget(rule.Output, valueFromTarget(rule.Input, event), mode) } func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent, mode *string) *evdev.InputEvent { @@ -96,10 +106,10 @@ func (rule *ComboMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev targetState := len(rule.Inputs) if oldState == targetState-1 && rule.State == targetState { - return eventFromTarget(rule.Output, 1) + return eventFromTarget(rule.Output, 1, mode) } if oldState == targetState && rule.State == targetState-1 { - return eventFromTarget(rule.Output, 0) + return eventFromTarget(rule.Output, 0, mode) } return nil } @@ -124,5 +134,5 @@ func (rule *LatchedMappingRule) MatchEvent(device *evdev.InputDevice, event *evd value = 0 } - return eventFromTarget(rule.Output, value) + return eventFromTarget(rule.Output, value, mode) } diff --git a/internal/mappingrules/types.go b/internal/mappingrules/types.go index 192c919..228ee31 100644 --- a/internal/mappingrules/types.go +++ b/internal/mappingrules/types.go @@ -34,6 +34,7 @@ type LatchedMappingRule struct { type RuleTarget struct { DeviceName string + ModeSelect []string Device *evdev.InputDevice Type evdev.EvType Code evdev.EvCode