Refactor logging and start adding rule matching code.
This commit is contained in:
parent
8549f36c8f
commit
33837895d9
4 changed files with 92 additions and 22 deletions
50
internal/rules/keymapping.go
Normal file
50
internal/rules/keymapping.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package rules
|
||||
|
||||
import (
|
||||
"git.annabunches.net/annabunches/joyful/internal/logger"
|
||||
"github.com/holoplot/go-evdev"
|
||||
)
|
||||
|
||||
type KeyMappingRule interface {
|
||||
MatchEvent(*evdev.InputDevice, *evdev.InputEvent)
|
||||
}
|
||||
|
||||
type SimpleMappingRule struct {
|
||||
Input RuleTarget
|
||||
Output RuleTarget
|
||||
}
|
||||
|
||||
type ComboMappingRule struct {
|
||||
Input []RuleTarget
|
||||
Output RuleTarget
|
||||
}
|
||||
|
||||
func (rule *SimpleMappingRule) MatchEvent(device *evdev.InputDevice, event *evdev.InputEvent) *evdev.InputEvent {
|
||||
if event.Type != evdev.EV_KEY ||
|
||||
device != rule.Input.Device ||
|
||||
event.Code != rule.Input.Code {
|
||||
return nil
|
||||
}
|
||||
|
||||
// how we process inverted rules depends on the event type
|
||||
value := event.Value
|
||||
if rule.Input.Inverted {
|
||||
switch rule.Input.Type {
|
||||
case evdev.EV_KEY:
|
||||
if value == 0 {
|
||||
value = 1
|
||||
} else {
|
||||
value = 0
|
||||
}
|
||||
default:
|
||||
logger.Log("Tried to handle inverted rule for unknown event type. Skipping rule.")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return &evdev.InputEvent{
|
||||
Type: rule.Output.Type,
|
||||
Code: rule.Output.Code,
|
||||
Value: value,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue