-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathqueue.go
83 lines (74 loc) · 2.01 KB
/
queue.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
package app
import (
"fmt"
"log"
"github.com/xlab/android-go/android"
)
var SkipInputEvents = func(ev *android.InputEvent) {}
var LogInputEvents = func(ev *android.InputEvent) {
switch android.InputEventGetType(ev) {
case android.InputEventTypeKey:
key := android.KeyEventGetKeyCode(ev)
log.Printf("key event [%d]", key)
case android.InputEventTypeMotion:
str := "motion event "
fingers := android.MotionEventGetPointerCount(ev)
for i := uint32(0); i < fingers; i++ {
x := android.MotionEventGetX(ev, i)
y := android.MotionEventGetY(ev, i)
pressure := android.MotionEventGetPressure(ev, i)
str += fmt.Sprintf("[%.0f; %.0f; %.2f]", x, y, pressure)
}
log.Println(str)
}
}
func HandleInputQueues(queueChan <-chan *android.InputQueue, onProcessed func(),
evHandler func(ev *android.InputEvent)) {
looper := android.LooperPrepare(android.LooperPrepareAllowNonCallbacks)
pending := make(chan *android.InputQueue, 1)
go func() {
for queue := range queueChan {
pending <- queue
android.LooperWake(looper)
}
}()
var current *android.InputQueue
for {
if android.LooperPollAll(-1, nil, nil, nil) == android.LooperPollWake {
select {
default:
case p := <-pending:
if current != nil {
handleEvents(current, evHandler)
android.InputQueueDetachLooper(current)
}
current = p
if current != nil {
android.InputQueueAttachLooper(current, looper, 0, nil, nil)
}
onProcessed()
}
}
if current != nil {
handleEvents(current, evHandler)
}
}
}
func handleEvents(queue *android.InputQueue, evHandler func(ev *android.InputEvent)) {
var ev *android.InputEvent
for android.InputQueueGetEvent(queue, &ev) >= 0 {
if android.InputQueuePreDispatchEvent(queue, ev) != 0 {
continue
}
evHandler(ev)
var response int32 = 0
switch android.InputEventGetType(ev) {
case android.InputEventTypeKey:
key := android.KeyEventGetKeyCode(ev)
if key == android.KeycodeBack {
response = 1
}
}
android.InputQueueFinishEvent(queue, ev, response)
}
}