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
|
@ -2,9 +2,9 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.annabunches.net/annabunches/joyful/internal/logger"
|
||||
"git.annabunches.net/annabunches/joyful/internal/virtualdevice"
|
||||
"github.com/holoplot/go-evdev"
|
||||
)
|
||||
|
@ -38,7 +38,7 @@ func main() {
|
|||
},
|
||||
},
|
||||
)
|
||||
fatalIfError(err, "Failed to create virtual device")
|
||||
logger.FatalIfError(err, "Failed to create virtual device")
|
||||
|
||||
buffer := virtualdevice.NewEventBuffer(vDevice)
|
||||
|
||||
|
@ -49,7 +49,7 @@ func main() {
|
|||
fmt.Printf("Virtual device created as %s.\n", name)
|
||||
|
||||
pDevice, err := evdev.Open("/dev/input/event12")
|
||||
fatalIfError(err, "Couldn't open physical device")
|
||||
logger.FatalIfError(err, "Couldn't open physical device")
|
||||
|
||||
name, err = pDevice.Name()
|
||||
if err != nil {
|
||||
|
@ -63,7 +63,7 @@ func main() {
|
|||
last := combo
|
||||
|
||||
event, err := pDevice.ReadOne()
|
||||
logIfError(err, "Error while reading event")
|
||||
logger.LogIfError(err, "Error while reading event")
|
||||
|
||||
// FIXME: test code
|
||||
for event.Code != evdev.SYN_REPORT {
|
||||
|
@ -103,7 +103,7 @@ func main() {
|
|||
}
|
||||
|
||||
event, err = pDevice.ReadOne()
|
||||
logIfError(err, "Error while reading event")
|
||||
logger.LogIfError(err, "Error while reading event")
|
||||
}
|
||||
|
||||
if combo > last && combo == 3 {
|
||||
|
@ -144,20 +144,3 @@ func jsButtons() []evdev.EvCode {
|
|||
|
||||
return buttons
|
||||
}
|
||||
|
||||
func logIfError(err error, msg string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%s: %s\n", msg, err.Error())
|
||||
}
|
||||
|
||||
func fatalIfError(err error, msg string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
logIfError(err, msg)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
|
27
internal/logger/logger.go
Normal file
27
internal/logger/logger.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Log(msg string) {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
|
||||
func LogIfError(err error, msg string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("%s: %s\n", msg, err.Error())
|
||||
}
|
||||
|
||||
func FatalIfError(err error, msg string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
LogIfError(err, msg)
|
||||
os.Exit(1)
|
||||
}
|
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,
|
||||
}
|
||||
}
|
10
internal/rules/types.go
Normal file
10
internal/rules/types.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package rules
|
||||
|
||||
import "github.com/holoplot/go-evdev"
|
||||
|
||||
type RuleTarget struct {
|
||||
Device *evdev.InputDevice
|
||||
Type evdev.EvType
|
||||
Code evdev.EvCode
|
||||
Inverted bool
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue