Skip to content

Commit

Permalink
evdev: enable marshaling and unmarshaling from outside, docs improvem…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
joonas-fi committed Apr 3, 2022
1 parent 5e89d4b commit 80c4607
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 7 additions & 4 deletions app/evdev/evdev.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ func (d *Device) SetLedOff(led Led) error {
}

func (d *Device) writeEvent(evType EventType, code uint16, value int32) error {
return binary.Write(d.handle, binary.LittleEndian, &InputEvent{
event := &InputEvent{
// TODO: we're sending time as zero?
Type: evType,
Code: code,
Value: value,
})
}
_, err := d.handle.Write(event.AsBytes())
return err
}

func readOneInputEvent(inputDevice *os.File) (*InputEvent, error) {
Expand All @@ -121,7 +124,7 @@ func readOneInputEvent(inputDevice *os.File) (*InputEvent, error) {
if n <= 0 {
return nil, nil
}
return eventFromBuffer(buffer)
return InputEventFromBytes(buffer)
}

func grabExclusiveInputDeviceAccess(inputDevice *os.File) error {
Expand All @@ -133,7 +136,7 @@ func grabExclusiveInputDeviceAccess(inputDevice *os.File) error {
return nil
}

func eventFromBuffer(buffer []byte) (*InputEvent, error) {
func InputEventFromBytes(buffer []byte) (*InputEvent, error) {
event := &InputEvent{}
err := binary.Read(bytes.NewBuffer(buffer), binary.LittleEndian, event)
return event, err
Expand Down
17 changes: 14 additions & 3 deletions app/evdev/input_event.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package evdev

import (
"bytes"
"encoding/binary"
"fmt"
"syscall"
"time"
Expand All @@ -20,9 +22,9 @@ var eventsize = int(unsafe.Sizeof(InputEvent{}))
// Has to match the C struct/wire protocol
type InputEvent struct {
Time syscall.Timeval
Type EventType
Code uint16
Value int32
Type EventType // EvKey | EvSyn | EvLed | ...
Code uint16 // Code is the actual key code, e.g. KeyLEFTSHIFT
Value int32 // additional specifier like Press/Hold/Release when Type=EvKey
}

func (i *InputEvent) TimevalToTime() time.Time {
Expand Down Expand Up @@ -70,3 +72,12 @@ func (i *InputEvent) String() string {
return fmt.Sprintf("unknown: %d", i.Type)
}
}

// marshals to evdev "wire format"
func (i *InputEvent) AsBytes() []byte {
buf := bytes.NewBuffer(make([]byte, 0, 24)) // 24 = sizeof(InputEvent)
if err := binary.Write(buf, binary.LittleEndian, i); err != nil {
panic(err)
}
return buf.Bytes()
}

0 comments on commit 80c4607

Please sign in to comment.