-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace.go
369 lines (313 loc) · 9.03 KB
/
trace.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package contraption
import (
"encoding/gob"
"io"
"log"
"os"
"time"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/neputevshina/geom"
)
// Event trace buffer length
const tracelen = 128
var endOfTimes = time.Unix(1<<63-62135596801, 999999999)
type EventTraceLast struct {
StartedAt time.Time // Time of a first matched event.
FirstTouch geom.Point
Duration time.Duration // Duration of match.
Freshness time.Duration // Difference between start of match and now.
Box geom.Rectangle // Total cursor position distribution of a match.
Choked bool
}
type Events struct {
Trace []EventPoint
Last EventTraceLast
Now time.Time
Dt time.Duration
Viewport geom.Point
deadline time.Time
tr [tracelen * 2]EventPoint
trcur int
held [tracelen / 2]EventPoint
heldcur int
evuser [tracelen]EventPoint
temp [tracelen / 4]EventPoint
tempcur int
regexps map[string][]rinst
MatchCount int
// 0 — normal operation
// 1 — recording
// 2 — replaying
// 3 — end of replay
rec int
rec0 time.Time
playc chan time.Time
RecordPath string
records []EventPoint
future EventPoint
}
// scrollPush pushes a new event point to the circular trace buffer and
// returns a slice of it where the new value is the first element.
func scrollPush(buffer []EventPoint, cursor *int, v EventPoint) (slice []EventPoint) {
gl := len(buffer)
buffer[(*cursor+gl/2)%gl] = v
buffer[*cursor] = v
*cursor--
if *cursor < 0 {
*cursor = gl/2 - 1
}
// *cursor+1 because in other case it will repeat event triggers after period of a buffer.
return buffer[*cursor+1 : *cursor+gl/2]
}
func (u *Events) SetDeadline(t time.Time) {
u.deadline = t
}
func (wo *Events) next() bool {
now := time.Now()
if wo.tempcur == 0 {
// Don't update time on catching up events or else they won't be matched as fresh.
wo.Dt = wo.Now.Sub(now)
wo.Now = now // Current frame events are “from future” so they are fresh if deadline is “now”.
}
if wo.rec >= 2 {
now = <-wo.playc
// We need to poll events to get the window close event.
// If it is not done, window can't be closed in replay mode.
concretepoll(wo)
}
if wo.rec < 2 {
if wo.tempcur > 0 {
m := wo.temp[0]
copy(wo.temp[:], wo.temp[1:])
wo.trueemit(m.E, m.Pt, m.T)
wo.tempcur--
} else {
if wo.Now.Compare(wo.deadline) >= 0 {
concretewait(wo)
} else {
concretepoll(wo)
}
}
}
return true
}
func (u *Events) emit(ev interface{}, pt geom.Point, t time.Time) {
m := EventPoint{ev, pt, t, 0, 0}
// Skip events if application is lagging.
u.tempcur = min(len(u.temp)-1, u.tempcur)
u.temp[u.tempcur] = m
u.tempcur++
}
func (u *Events) develop() {
for i := range u.tr {
u.tr[i].z = 0
u.tr[i].zc = 0
}
}
// trueemit pushes the new event to the trace.
func (u *Events) trueemit(ev interface{}, pt geom.Point, t time.Time) {
if _, yes := ev.(EventPoint); yes {
panic("can't emit EventPoint")
}
m := EventPoint{ev, pt, t, 0, 0}
if u.rec == 1 {
u.records = append(u.records, m)
}
// // Push paint deadline further.
// u.SetDeadline(t.Add(100 * time.Millisecond))
// If the just happened event is the same type and value as the latest event in Trace,
// the latter is pushed to Details and replaced with former.
// This behavior may be not the same for types of events that might be added in future.
if ev == u.Trace[0].E && pt == u.Trace[0].Pt {
u.Trace[0] = m
return
}
base := scrollPush(u.tr[:], &u.trcur, m)
relative := len(base) - u.heldcur
// If our click is unclicked we can obviously stop holding it.
// No matter in which place it is held.
for i := range u.held[:u.heldcur] {
if u.held[i].E == complement(base[relative].E) {
copy(u.held[i:], u.held[i+1:])
u.heldcur--
// We can't have two same held events: a button can't be held twice.
break
}
}
// If the holdable type is in the bottom of the trace, it is copied to the hold trace,
// which is then copied over the end of Trace.
// This way user can accurately match, for instance, any action that begins with a press or
// click and ends several other presses/clicks later.
if bottom := base[relative-1]; holdable(bottom.E) {
if u.heldcur < len(u.held) {
copy(u.held[1:], u.held[:])
u.held[0] = bottom
u.heldcur++
}
}
// FIXME -1 because rinterp needs a tail to get rmatch
relative = len(u.Trace) - u.heldcur - 1
u.Trace = u.evuser[:]
copy(u.Trace, base)
copy(u.Trace[relative:], u.held[:u.heldcur])
u.Trace[len(u.Trace)-1] = EventPoint{}
// println(u.Trace)
}
func (u *Events) In(r geom.Rectangle) Matcher {
m := newMatcher(u)
m.rect = r
m.alwaysin = true
return m
}
func (u *Events) Anywhere() Matcher {
m := newMatcher(u)
m.rect = geom.Rect(-123456, -132412, 142323, 123111)
m.alwaysin = false
return m
}
// Matcher is a builder interface for regular expressions.
type Matcher struct {
u *Events
pattern string
rect geom.Rectangle
dur time.Duration
deadline time.Time
z int
alwaysin bool
}
func newMatcher(e *Events) Matcher {
return Matcher{
u: e,
rect: geom.Rect(-99999, -99999, 99999, 99999),
dur: time.Duration(^uint64(0) >> 1),
deadline: e.Now,
z: 0,
}
}
func (m Matcher) Rect() geom.Rectangle {
return m.rect
}
func (m Matcher) Z() int {
return m.z
}
func (m Matcher) WithZ(z int) Matcher {
m.z = z
return m
}
func (m Matcher) Duration(d time.Duration) Matcher {
m.dur = d
return m
}
func (m Matcher) Deadline(t time.Time) Matcher {
m.deadline = t
return m
}
func (m Matcher) Nochoke() Matcher {
m.z = 0
return m
}
func (m Matcher) Anywhere() Matcher {
m.alwaysin = false
return m
}
func (m Matcher) Indef() Matcher {
m.deadline = time.Time{}
return m
}
func (m Matcher) Match(pattern string) bool {
return m.u.match(pattern, m.rect, m.dur, m.deadline, m.z, m.alwaysin)
}
func (u *Events) Match(pattern string) bool {
return u.match(pattern, geom.Rect(-99999, -99999, 99999, 99999), time.Duration(^uint64(0)>>1), u.Now, 0, false)
}
// Hint: :in. And add MatchAllIn later, for fuck's sake.
func (u *Events) MatchIn(pattern string, r geom.Rectangle) bool {
return u.match(pattern, r, time.Duration(^uint64(0)>>1), u.Now, 0, false)
}
func (u *Events) MatchInNochoke(pattern string, r geom.Rectangle) bool {
// TODO
return u.match(pattern, r, time.Duration(^uint64(0)>>1), u.Now, maxint, false)
}
func (u *Events) MatchIndef(pattern string) bool {
return u.match(pattern, geom.Rect(-99999, -99999, 99999, 99999), time.Duration(^uint64(0)>>1), time.Time{}, 0, false)
}
func (u *Events) MatchInIndef(pattern string, rect geom.Rectangle) bool {
return u.match(pattern, rect, time.Duration(^uint64(0)>>1), time.Time{}, 0, false)
}
func (u *Events) MatchInFreshness(pattern string, rect geom.Rectangle, freshness time.Duration) bool {
return u.match(pattern, rect, time.Duration(^uint64(0)>>1), u.Now.Add(-freshness), 0, false)
}
func (u *Events) MatchInDuration(pattern string, rect geom.Rectangle, duration time.Duration) bool {
return u.match(pattern, rect, duration, time.Time{}, 0, false)
}
func (u *Events) MatchFreshness(pattern string, freshness time.Duration) bool {
return u.match(pattern, geom.Rect(-99999, -99999, 99999, 99999), time.Duration(^uint64(0)>>1), u.Now.Add(-freshness), 0, false)
}
func (u *Events) MatchDeadline(pattern string, deadline time.Time) bool {
return u.match(pattern, geom.Rect(-99999, -99999, 99999, 99999), time.Duration(^uint64(0)>>1), deadline, 0, false)
}
func (u *Events) MatchInDeadline(pattern string, rect geom.Rectangle, deadline time.Time) bool {
return u.match(pattern, rect, time.Duration(^uint64(0)>>1), deadline, 0, false)
}
func (u *Events) match(pattern string, rect geom.Rectangle, dur time.Duration, deadline time.Time, z int, alwaysin bool) bool {
u.MatchCount++
r, ok := u.regexps[pattern]
if !ok {
var err error
r, err = rcompile(pattern)
if err != nil {
log.Println("error parsing pattern “" + pattern + "”")
panic(err)
}
u.regexps[pattern] = r
}
ok, last := rinterp(r, u.Trace, rect, dur, deadline, z, alwaysin)
if ok {
u.Last = last
u.Last.Freshness = u.Now.Sub(u.Last.StartedAt)
}
return ok
}
func NewEventTracer(w *glfw.Window, replay io.Reader) *Events {
var u Events
u.heldcur = 0
u.regexps = map[string][]rinst{}
if replay == nil {
setupcallbacks(&u, w)
} else {
// Replay mode disables vsync.
// This is the simplest way to synchronize recorded events and state.
glfw.SwapInterval(0)
u.rec = 2
u.rec0 = time.Now()
u.playc = make(chan time.Time)
go u.replay(replay)
}
u.Trace = u.tr[tracelen/2 : tracelen-1]
return &u
}
func (u *Events) replay(r io.Reader) {
err := gob.NewDecoder(r).Decode(&(u.records))
if err != nil && err != io.EOF {
panic(err)
}
re := u.records
// Last event is F5 press, skip it.
d := 0 * time.Second
for i, r := range re[:len(re)-1] {
d = re[i+1].T.Sub(r.T)
u.future = re[i+1]
u.emit(r.E, r.Pt, r.T.Add(1*time.Millisecond))
u.playc <- r.T
time.Sleep(d)
}
u.rec = 3
os.Exit(0)
// t := last(re).T
// for {
// m := 100 * time.Millisecond
// t = t.Add(m)
// u.playc <- t
// time.Sleep(m)
// }
}