forked from gvalkov/golang-evdev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.go
109 lines (89 loc) · 2.29 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package evdev
import (
"fmt"
"syscall"
"unsafe"
)
type InputEvent struct {
Time syscall.Timeval // time in seconds since epoch at which event occurred
Type uint16 // event type - one of ecodes.EV_*
Code uint16 // event code related to the event type
Value int32 // event value related to the event type
}
// Get a useful description for an input event. Example:
// event at 1347905437.435795, code 01, type 02, val 02
func (ev *InputEvent) String() string {
return fmt.Sprintf("event at %d.%d, code %02d(%s), type %02d, val %02d",
ev.Time.Sec, ev.Time.Usec, ev.Code, KEY[int(ev.Code)], ev.Type, ev.Value)
}
var eventsize = int(unsafe.Sizeof(InputEvent{}))
type KeyEventState uint8
const (
KeyUp KeyEventState = 0x0
KeyDown KeyEventState = 0x1
KeyHold KeyEventState = 0x2
)
// KeyEvents are used to describe state changes of keyboards, buttons,
// or other key-like devices.
type KeyEvent struct {
Event *InputEvent
Scancode uint16
Keycode uint16
State KeyEventState
}
func (kev *KeyEvent) New(ev *InputEvent) {
kev.Event = ev
kev.Keycode = 0 // :todo
kev.Scancode = ev.Code
switch ev.Value {
case 0:
kev.State = KeyUp
case 2:
kev.State = KeyHold
case 1:
kev.State = KeyDown
}
}
func NewKeyEvent(ev *InputEvent) *KeyEvent {
kev := &KeyEvent{}
kev.New(ev)
return kev
}
func (ev *KeyEvent) String() string {
state := "unknown"
switch ev.State {
case KeyUp:
state = "up"
case KeyHold:
state = "hold"
case KeyDown:
state = "down"
}
return fmt.Sprintf("key event at %d.%d, %d (%d), (%s)",
ev.Event.Time.Sec, ev.Event.Time.Usec,
ev.Scancode, ev.Event.Code, state)
}
// RelEvents are used to describe relative axis value changes,
// e.g. moving the mouse 5 units to the left.
type RelEvent struct {
Event *InputEvent
}
func (rev *RelEvent) New(ev *InputEvent) {
rev.Event = ev
}
func NewRelEvent(ev *InputEvent) *RelEvent {
rev := &RelEvent{}
rev.New(ev)
return rev
}
func (ev *RelEvent) String() string {
return fmt.Sprintf("relative axis event at %d.%d, %s",
ev.Event.Time.Sec, ev.Event.Time.Usec,
REL[int(ev.Event.Code)])
}
// TODO: Make this work
var EventFactory map[uint16]interface{} = make(map[uint16]interface{})
func init() {
EventFactory[uint16(EV_KEY)] = NewKeyEvent
EventFactory[uint16(EV_REL)] = NewRelEvent
}