offscreen-android 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +195 -2
- package/assets/injector/Injector.smali +1179 -0
- package/assets/injector/README.md +101 -0
- package/assets/injector/inject.dex +0 -0
- package/assets/overlay/ai-overlay.apk +0 -0
- package/assets/scrcpy-server.jar +0 -0
- package/bin/aim.mjs +220 -0
- package/bin/asr.mjs +81 -0
- package/bin/audcap.mjs +128 -0
- package/bin/mcp-server.mjs +410 -0
- package/bin/vdisplay.mjs +305 -0
- package/bin/vdisplayd.mjs +398 -0
- package/bin/vidcap.mjs +82 -0
- package/bin/vidgrab.mjs +172 -0
- package/package.json +55 -4
- package/src/framesrv.js +348 -0
- package/src/human.js +302 -0
- package/src/index.js +161 -0
- package/src/inject.js +415 -0
- package/src/lease.js +314 -0
- package/src/occupancy.js +214 -0
- package/src/ocr.js +209 -0
- package/src/ocr_helper.py +125 -0
- package/src/pool.js +195 -0
- package/src/sampling.js +163 -0
- package/src/stream.js +571 -0
- package/src/target.js +29 -0
- package/src/timeline.js +324 -0
- package/src/tool-spec.js +873 -0
- package/src/tools.js +2034 -0
- package/src/vd.js +2354 -0
- package/src/video.js +751 -0
- package/src/vision.js +193 -0
- package/src/vision_helper.py +334 -0
package/src/inject.js
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* inject.js — 通过 scrcpy 控制通道发 INJECT_TOUCH_EVENT,替代 `adb shell input tap`。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么要换掉 `adb shell input`
|
|
5
|
+
*
|
|
6
|
+
* `input` 那条路的破绽是结构性的,改不动:
|
|
7
|
+
* · 每次起一个子进程,30-80ms 开销
|
|
8
|
+
* · 压力恒 1.0(`MotionEvent.obtain(..., pressure=1.0f, size=1.0f, ...)`)
|
|
9
|
+
* · `getHistorySize()` 恒 0
|
|
10
|
+
* · `swipe` 的 MOVE 是严格线性等距、间隔 1ms 级 —— 比没有更假
|
|
11
|
+
* · deviceId 取"第一个支持 TOUCHSCREEN 的设备",可能挑中某个虚拟触屏
|
|
12
|
+
*
|
|
13
|
+
* scrcpy 的 INJECT_TOUCH_EVENT 不一样:**pressure 是报文里的独立字段**,
|
|
14
|
+
* 服务端原样写进 `MotionEvent.PointerCoords.pressure`,系统不会重算。
|
|
15
|
+
* 路径(上游源码,已核对):
|
|
16
|
+
* ControlMessageReader.parseInjectTouchEvent → Controller.injectTouch
|
|
17
|
+
* → pointer.setPressure → PointersState.update → MotionEvent.obtain
|
|
18
|
+
* → Device.injectEvent(隐藏 API InputManager.injectInputEvent)
|
|
19
|
+
*
|
|
20
|
+
* ## 报文格式(scrcpy 5.0.1,32 字节定长,全大端)
|
|
21
|
+
*
|
|
22
|
+
* type u8 = 2
|
|
23
|
+
* action u8 0=DOWN 1=UP 2=MOVE 3=CANCEL
|
|
24
|
+
* pointerId i64 BE
|
|
25
|
+
* x i32
|
|
26
|
+
* y i32
|
|
27
|
+
* screenWidth u16
|
|
28
|
+
* screenHeight u16
|
|
29
|
+
* pressure u16 定点:0xFFFF = 1.0
|
|
30
|
+
* actionButton i32
|
|
31
|
+
* buttons i32
|
|
32
|
+
*
|
|
33
|
+
* v2.0 起就是这个格式,v3.3.4 / v4.1 / v5.0.1 逐字节一致(v1.25 少 actionButton)。
|
|
34
|
+
*
|
|
35
|
+
* ## 仍然修不掉的(诚实记录)
|
|
36
|
+
*
|
|
37
|
+
* · deviceId 服务端写死 0 → `InputDevice.getDevice(0)` 可能返回 null
|
|
38
|
+
* · 接触面积 size 协议里**根本没有这个字段** → PointerCoords.size 恒 0
|
|
39
|
+
* · x/y 只能是整数
|
|
40
|
+
* · getHistorySize() 靠 InputDispatcher 合并,客户端控制不了
|
|
41
|
+
*
|
|
42
|
+
* 要连 size 和 history 一起解决,只能自己写 app_process 注入器(照 input 的
|
|
43
|
+
* getInputDeviceId 拿真实触摸屏 id,用 MotionEvent.addBatch 造历史采样)。
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
const TYPE_INJECT_TOUCH = 2;
|
|
47
|
+
|
|
48
|
+
export const ACTION = { DOWN: 0, UP: 1, MOVE: 2, CANCEL: 3 };
|
|
49
|
+
|
|
50
|
+
export const BUTTON_PRIMARY = 1;
|
|
51
|
+
|
|
52
|
+
/** 压力转定点:0..1 → 0..65535 */
|
|
53
|
+
export function toFixedPressure(p) {
|
|
54
|
+
const v = Math.max(0, Math.min(1, Number(p)));
|
|
55
|
+
return Math.round(v * 0xffff) & 0xffff;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 生成一条 INJECT_TOUCH_EVENT 报文。
|
|
60
|
+
*
|
|
61
|
+
* @param {object} o
|
|
62
|
+
* @param {number} o.action ACTION.DOWN / UP / MOVE / CANCEL
|
|
63
|
+
* @param {number|bigint} [o.pointerId=0] 0 表示用鼠标指针(单指)
|
|
64
|
+
* @param {number} o.x
|
|
65
|
+
* @param {number} o.y
|
|
66
|
+
* @param {number} o.screenW 必须是虚拟屏的真实宽(**不匹配服务端会静默丢弃**)
|
|
67
|
+
* @param {number} o.screenH
|
|
68
|
+
* @param {number} [o.pressure=1] 0..1
|
|
69
|
+
* @param {number} [o.actionButton=0]
|
|
70
|
+
* @param {number} [o.buttons=0]
|
|
71
|
+
* @returns {Buffer} 32 字节
|
|
72
|
+
*/
|
|
73
|
+
export function buildTouch(o) {
|
|
74
|
+
const buf = Buffer.alloc(32);
|
|
75
|
+
let off = 0;
|
|
76
|
+
|
|
77
|
+
buf.writeUInt8(TYPE_INJECT_TOUCH, off); off += 1;
|
|
78
|
+
buf.writeUInt8(Number(o.action) & 0xff, off); off += 1;
|
|
79
|
+
|
|
80
|
+
// pointerId 是 i64。scrcpy 用 -1 表示鼠标指针;这里默认 -1,跟它的约定一致。
|
|
81
|
+
const pid = o.pointerId === undefined ? -1n : BigInt(o.pointerId);
|
|
82
|
+
buf.writeBigInt64BE(pid, off); off += 8;
|
|
83
|
+
|
|
84
|
+
buf.writeInt32BE(Math.round(Number(o.x) || 0), off); off += 4;
|
|
85
|
+
buf.writeInt32BE(Math.round(Number(o.y) || 0), off); off += 4;
|
|
86
|
+
|
|
87
|
+
buf.writeUInt16BE(Math.max(0, Math.min(65535, Math.round(Number(o.screenW) || 0))), off); off += 2;
|
|
88
|
+
buf.writeUInt16BE(Math.max(0, Math.min(65535, Math.round(Number(o.screenH) || 0))), off); off += 2;
|
|
89
|
+
|
|
90
|
+
buf.writeUInt16BE(toFixedPressure(o.pressure === undefined ? 1 : o.pressure), off); off += 2;
|
|
91
|
+
|
|
92
|
+
buf.writeInt32BE(Number(o.actionButton) | 0, off); off += 4;
|
|
93
|
+
buf.writeInt32BE(Number(o.buttons) | 0, off); off += 4;
|
|
94
|
+
|
|
95
|
+
return buf;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 生成一次完整点按的报文序列(DOWN → 若干 MOVE → UP)。
|
|
100
|
+
*
|
|
101
|
+
* **压力沿轨迹变化**是这条路的真正价值:`input tap` 只能给恒定的 1.0,
|
|
102
|
+
* 而真实手指按下去时压力是先升后降的。这里按正弦包络模拟。
|
|
103
|
+
*
|
|
104
|
+
* @param {object} o
|
|
105
|
+
* @param {number} o.x
|
|
106
|
+
* @param {number} o.y
|
|
107
|
+
* @param {number} o.screenW
|
|
108
|
+
* @param {number} o.screenH
|
|
109
|
+
* @param {number} [o.holdMs=95] 按下到抬起的总时长
|
|
110
|
+
* @param {number} [o.moves=0] 中间插几个 MOVE
|
|
111
|
+
* @returns {{bufs:Buffer[], totalMs:number}}
|
|
112
|
+
*/
|
|
113
|
+
export function buildTap(o) {
|
|
114
|
+
const { x, y, screenW, screenH } = o;
|
|
115
|
+
const holdMs = Number(o.holdMs) || 95;
|
|
116
|
+
const moves = Math.max(0, Number(o.moves) || 0);
|
|
117
|
+
const bufs = [];
|
|
118
|
+
|
|
119
|
+
// DOWN:压力不是从 1.0 开始,真实触摸是逐渐压实
|
|
120
|
+
bufs.push(buildTouch({
|
|
121
|
+
action: ACTION.DOWN, x, y, screenW, screenH,
|
|
122
|
+
pressure: 0.42 + Math.random() * 0.12,
|
|
123
|
+
actionButton: BUTTON_PRIMARY, buttons: BUTTON_PRIMARY,
|
|
124
|
+
}));
|
|
125
|
+
|
|
126
|
+
// 中间 MOVE:压力按正弦包络走(先升后降),坐标带亚像素抖动
|
|
127
|
+
for (let i = 1; i <= moves; i += 1) {
|
|
128
|
+
const t = i / (moves + 1);
|
|
129
|
+
const env = 0.42 + Math.sin(Math.PI * t) * 0.5; // 0.42 → 0.92 → 0.42
|
|
130
|
+
bufs.push(buildTouch({
|
|
131
|
+
action: ACTION.MOVE,
|
|
132
|
+
x: x + (Math.random() - 0.5) * 1.2,
|
|
133
|
+
y: y + (Math.random() - 0.5) * 1.2,
|
|
134
|
+
screenW, screenH,
|
|
135
|
+
pressure: Math.max(0.2, Math.min(0.95, env + (Math.random() - 0.5) * 0.06)),
|
|
136
|
+
actionButton: BUTTON_PRIMARY, buttons: BUTTON_PRIMARY,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// UP:压力已经衰减
|
|
141
|
+
bufs.push(buildTouch({
|
|
142
|
+
action: ACTION.UP, x, y, screenW, screenH,
|
|
143
|
+
pressure: 0.18 + Math.random() * 0.1,
|
|
144
|
+
actionButton: BUTTON_PRIMARY, buttons: 0,
|
|
145
|
+
}));
|
|
146
|
+
|
|
147
|
+
return { bufs, totalMs: holdMs };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 生成一次滑动的报文序列,带**变速**(真人滑动是先加速后减速,不是匀速)。
|
|
152
|
+
*
|
|
153
|
+
* `adb input swipe` 的 MOVE 是严格等距的,这一点在行为统计里很明显。
|
|
154
|
+
*
|
|
155
|
+
* @param {object} o
|
|
156
|
+
* @param {number} o.x1 @param {number} o.y1
|
|
157
|
+
* @param {number} o.x2 @param {number} o.y2
|
|
158
|
+
* @param {number} o.screenW @param {number} o.screenH
|
|
159
|
+
* @param {number} [o.ms=300]
|
|
160
|
+
* @param {number} [o.steps=14]
|
|
161
|
+
* @returns {{bufs:Buffer[], totalMs:number, delays:number[]}}
|
|
162
|
+
*/
|
|
163
|
+
/**
|
|
164
|
+
* 生成一次「按住」的报文序列(长时间保持按下,手指几乎不动)。
|
|
165
|
+
*
|
|
166
|
+
* ## 为什么需要
|
|
167
|
+
*
|
|
168
|
+
* `swipe` 是「从 A 划到 B」,起点和终点不同。但有两类操作它不是:
|
|
169
|
+
*
|
|
170
|
+
* ① **游戏虚拟摇杆** —— 按住左下角不放,角色持续行走。摇杆的位移很小
|
|
171
|
+
* (几十像素内),但必须**持续**送 MOVE,引擎才认为手指还在推。
|
|
172
|
+
* ② **长按菜单 / 挖方块** —— 按住同一位置一段时间。
|
|
173
|
+
*
|
|
174
|
+
* ## 关键:MOVE 必须够密
|
|
175
|
+
*
|
|
176
|
+
* 实测教训:原来 `buildSwipe` 固定 14 步,300ms 时是 47Hz 还行,
|
|
177
|
+
* 但「按住 5 秒」就退化成 3Hz —— 游戏直接把手指当成没动。
|
|
178
|
+
*
|
|
179
|
+
* 这里按 **60Hz** 出 MOVE(16.7ms 一帧),并且叠一点微小抖动模拟真人手指。
|
|
180
|
+
* 摇杆的抖动不能太大,否则角色会走歪;默认半径 2px 以内。
|
|
181
|
+
*
|
|
182
|
+
* @param {object} o
|
|
183
|
+
* @param {number} o.x @param {number} o.y 按住的位置
|
|
184
|
+
* @param {number} o.screenW @param {number} o.screenH
|
|
185
|
+
* @param {number} [o.ms=1000] 按住多久
|
|
186
|
+
* @param {number} [o.jitter=2] 手指抖动半径(px),0 = 完全不动
|
|
187
|
+
* @param {number} [o.driftX=0] 持续偏移方向 x(摇杆推方向用)
|
|
188
|
+
* @param {number} [o.driftY=0] 持续偏移方向 y
|
|
189
|
+
* @returns {{bufs:Buffer[], totalMs:number, delays:number[]}}
|
|
190
|
+
*/
|
|
191
|
+
export function buildHold(o) {
|
|
192
|
+
const { x, y, screenW, screenH } = o;
|
|
193
|
+
const totalMs = Math.max(80, Number(o.ms) || 1000);
|
|
194
|
+
const steps = Math.max(2, Math.min(600, Math.round(totalMs / 16.7))); // ~60Hz
|
|
195
|
+
const jitter = Math.max(0, Number(o.jitter) || 0);
|
|
196
|
+
const driftX = Number(o.driftX) || 0;
|
|
197
|
+
const driftY = Number(o.driftY) || 0;
|
|
198
|
+
|
|
199
|
+
const bufs = [];
|
|
200
|
+
// 按下
|
|
201
|
+
bufs.push(buildTouch({
|
|
202
|
+
action: ACTION.DOWN, x, y, screenW, screenH,
|
|
203
|
+
pressure: 0.45 + Math.random() * 0.1,
|
|
204
|
+
actionButton: BUTTON_PRIMARY, buttons: BUTTON_PRIMARY,
|
|
205
|
+
}));
|
|
206
|
+
|
|
207
|
+
const delays = [];
|
|
208
|
+
const stepMs = totalMs / steps;
|
|
209
|
+
for (let i = 1; i <= steps; i += 1) {
|
|
210
|
+
// 沿 drift 方向缓慢推过去(摇杆推满需要一点时间),再叠抖动
|
|
211
|
+
const t = i / steps;
|
|
212
|
+
const px = x + driftX * t + (Math.random() - 0.5) * jitter * 2;
|
|
213
|
+
const py = y + driftY * t + (Math.random() - 0.5) * jitter * 2;
|
|
214
|
+
bufs.push(buildTouch({
|
|
215
|
+
action: ACTION.MOVE, x: px, y: py, screenW, screenH,
|
|
216
|
+
pressure: Math.max(0.3, Math.min(0.9, 0.6 + (Math.random() - 0.5) * 0.1)),
|
|
217
|
+
actionButton: BUTTON_PRIMARY, buttons: BUTTON_PRIMARY,
|
|
218
|
+
}));
|
|
219
|
+
// 间隔带一点随机,但不偏离 60Hz 太多
|
|
220
|
+
delays.push(Math.max(8, Math.round(stepMs * (0.85 + Math.random() * 0.3))));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// 抬起(位置是最后的落点)
|
|
224
|
+
const endX = x + driftX;
|
|
225
|
+
const endY = y + driftY;
|
|
226
|
+
bufs.push(buildTouch({
|
|
227
|
+
action: ACTION.UP, x: endX, y: endY, screenW, screenH,
|
|
228
|
+
pressure: 0.2 + Math.random() * 0.1,
|
|
229
|
+
actionButton: BUTTON_PRIMARY, buttons: 0,
|
|
230
|
+
}));
|
|
231
|
+
|
|
232
|
+
return { bufs, totalMs, delays };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function buildSwipe(o) {
|
|
236
|
+
const { x1, y1, x2, y2, screenW, screenH } = o;
|
|
237
|
+
const totalMs = Math.max(60, Number(o.ms) || 300);
|
|
238
|
+
/*
|
|
239
|
+
* **步数必须跟时长匹配,不能固定 14。**
|
|
240
|
+
*
|
|
241
|
+
* 固定 14 步只在 300ms 左右合理(≈47Hz,够用)。
|
|
242
|
+
* 但「按住摇杆 5 秒」时 14 步 = 2.8Hz —— 游戏会当成手指根本没动,
|
|
243
|
+
* 因为触摸帧率掉到个位数,引擎的摇杆采样读不到连续位移。
|
|
244
|
+
*
|
|
245
|
+
* 目标 ~60Hz(16.7ms 一帧),上限 400 步防止极端时长把报文撑爆。
|
|
246
|
+
* 调用方显式传 steps 时以调用方为准。
|
|
247
|
+
*/
|
|
248
|
+
const steps = o.steps != null
|
|
249
|
+
? Math.max(3, Number(o.steps))
|
|
250
|
+
: Math.max(3, Math.min(400, Math.round(totalMs / 16.7)));
|
|
251
|
+
const bufs = [];
|
|
252
|
+
|
|
253
|
+
// 缓动:真人手指是"快起 - 匀 - 缓停",用 easeInOutQuad
|
|
254
|
+
const ease = (t) => (t < 0.5 ? 2 * t * t : 1 - ((-2 * t + 2) ** 2) / 2);
|
|
255
|
+
|
|
256
|
+
bufs.push(buildTouch({
|
|
257
|
+
action: ACTION.DOWN, x: x1, y: y1, screenW, screenH,
|
|
258
|
+
pressure: 0.45 + Math.random() * 0.1,
|
|
259
|
+
actionButton: BUTTON_PRIMARY, buttons: BUTTON_PRIMARY,
|
|
260
|
+
}));
|
|
261
|
+
|
|
262
|
+
const delays = [];
|
|
263
|
+
let prevT = 0;
|
|
264
|
+
for (let i = 1; i <= steps; i += 1) {
|
|
265
|
+
const t = i / steps;
|
|
266
|
+
const e = ease(t);
|
|
267
|
+
// 坐标沿缓动曲线走,再叠一点垂直抖动(真人滑动不会完美直线)
|
|
268
|
+
const x = x1 + (x2 - x1) * e + (Math.random() - 0.5) * 1.5;
|
|
269
|
+
const y = y1 + (y2 - y1) * e + (Math.random() - 0.5) * 2.5;
|
|
270
|
+
const p = 0.5 + Math.sin(Math.PI * t) * 0.35;
|
|
271
|
+
bufs.push(buildTouch({
|
|
272
|
+
action: ACTION.MOVE, x, y, screenW, screenH,
|
|
273
|
+
pressure: Math.max(0.25, Math.min(0.95, p + (Math.random() - 0.5) * 0.05)),
|
|
274
|
+
actionButton: BUTTON_PRIMARY, buttons: BUTTON_PRIMARY,
|
|
275
|
+
}));
|
|
276
|
+
// 每步的时间间隔按缓动导数走:中间快、两头慢
|
|
277
|
+
delays.push(Math.max(4, Math.round((t - prevT) * totalMs * (0.6 + Math.random() * 0.8))));
|
|
278
|
+
prevT = t;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
bufs.push(buildTouch({
|
|
282
|
+
action: ACTION.UP, x: x2, y: y2, screenW, screenH,
|
|
283
|
+
pressure: 0.2 + Math.random() * 0.1,
|
|
284
|
+
actionButton: BUTTON_PRIMARY, buttons: 0,
|
|
285
|
+
}));
|
|
286
|
+
|
|
287
|
+
return { bufs, totalMs, delays };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/* ═══════════════════════════════════════════════════════════════════
|
|
291
|
+
* 多点触控(双指)
|
|
292
|
+
*
|
|
293
|
+
* ## 为什么之前没有
|
|
294
|
+
*
|
|
295
|
+
* `buildTouch` 本来就带 pointerId 字段,但一直只发单指。
|
|
296
|
+
* 游戏里双手操作(一边走一边转视角、双指缩放、同时按两个技能)
|
|
297
|
+
* 全都需要两指同时按下。
|
|
298
|
+
*
|
|
299
|
+
* ## **关键:不要自己编码 ACTION_POINTER_DOWN**
|
|
300
|
+
*
|
|
301
|
+
* 第一版我按 Android 的约定把 index 编进 action 高 8 位(5 | 1<<8 = 261),
|
|
302
|
+
* 结果设备报 `InputDispatcher: Inconsistent event`。
|
|
303
|
+
*
|
|
304
|
+
* 反编译 scrcpy-server 的 classes.dex(baksmali 到 smali)后看到真相:
|
|
305
|
+
*
|
|
306
|
+
* :cond_8c # action == UP 且还有其他指针
|
|
307
|
+
* shl-int/lit8 v1, v10, 0x8 # action = (pointerIndex << 8)
|
|
308
|
+
* or-int/lit8 v1, v1, 0x6 # action |= 6 → ACTION_POINTER_UP
|
|
309
|
+
* :cond_93 # action == DOWN 且还有其他指针
|
|
310
|
+
* shl-int/lit8 v1, v10, 0x8 # action = (pointerIndex << 8)
|
|
311
|
+
* or-int/lit8 v1, v1, 0x5 # action |= 5 → ACTION_POINTER_DOWN
|
|
312
|
+
*
|
|
313
|
+
* **scrcpy 服务端自己算 index 并 OR 上 5/6。**
|
|
314
|
+
* 我们只要发基础 action(0=DOWN / 1=UP / 2=MOVE),它负责转换。
|
|
315
|
+
* 自己编码反而被截断成 5,scrcpy 收到不认识的值 → 事件被丢。
|
|
316
|
+
*
|
|
317
|
+
* ## 序列(全部用基础 action)
|
|
318
|
+
*
|
|
319
|
+
* 指0 DOWN(0) pointerId=0
|
|
320
|
+
* 指1 DOWN(0) pointerId=1 ← scrcpy 自动转成 POINTER_DOWN
|
|
321
|
+
* 两指 MOVE(2) 各发一条
|
|
322
|
+
* 指1 UP(1) pointerId=1 ← scrcpy 自动转成 POINTER_UP
|
|
323
|
+
* 指0 UP(1) pointerId=0
|
|
324
|
+
*/
|
|
325
|
+
|
|
326
|
+
export const ACTION_MULTI = {
|
|
327
|
+
DOWN: 0, UP: 1, MOVE: 2, CANCEL: 3,
|
|
328
|
+
POINTER_DOWN: 5, POINTER_UP: 6,
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
/** 把「第几指」编进 action。 */
|
|
332
|
+
function withIndex(action, index) {
|
|
333
|
+
return (action | (index << 8)) & 0xffff;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* 生成双指手势的报文序列。
|
|
338
|
+
*
|
|
339
|
+
* @param {object} o
|
|
340
|
+
* @param {{x:number,y:number}} o.p1 第一指(比如摇杆位置)
|
|
341
|
+
* @param {{x:number,y:number}} o.p2 第二指(比如视角滑动起点)
|
|
342
|
+
* @param {{x:number,y:number}} [o.p2To] 第二指的终点(滑动)
|
|
343
|
+
* @param {number} o.screenW
|
|
344
|
+
* @param {number} o.screenH
|
|
345
|
+
* @param {number} [o.ms=600] 总时长
|
|
346
|
+
* @param {number} [o.p1DriftX=0] 第一指期间横向漂移
|
|
347
|
+
* @param {number} [o.p1DriftY=0] 第一指期间纵向漂移
|
|
348
|
+
* @param {boolean} [o.releaseP1=true] 结束时抬不抬第一指
|
|
349
|
+
*/
|
|
350
|
+
export function buildMultiTouch(o) {
|
|
351
|
+
const { p1, p2, p2To, screenW, screenH } = o;
|
|
352
|
+
const totalMs = Math.max(120, Number(o.ms) || 600);
|
|
353
|
+
const steps = Math.max(4, Math.min(240, Math.round(totalMs / 16.7))); // ~60Hz
|
|
354
|
+
const driftX = Number(o.p1DriftX) || 0;
|
|
355
|
+
const driftY = Number(o.p1DriftY) || 0;
|
|
356
|
+
const to = p2To || p2;
|
|
357
|
+
const bufs = [];
|
|
358
|
+
const delays = [];
|
|
359
|
+
|
|
360
|
+
const P1 = 0n, P2 = 1n; // pointerId
|
|
361
|
+
const btn = BUTTON_PRIMARY;
|
|
362
|
+
|
|
363
|
+
// ① 第一指按下
|
|
364
|
+
bufs.push(buildTouch({
|
|
365
|
+
action: ACTION.DOWN, x: p1.x, y: p1.y, screenW, screenH,
|
|
366
|
+
pointerId: P1, pressure: 0.45, actionButton: btn, buttons: btn,
|
|
367
|
+
}));
|
|
368
|
+
delays.push(30);
|
|
369
|
+
|
|
370
|
+
// ② 第二指按下(ACTION_POINTER_DOWN,index=1)
|
|
371
|
+
bufs.push(buildTouch({
|
|
372
|
+
action: ACTION.DOWN, x: p2.x, y: p2.y, screenW, screenH, // scrcpy 自动转 POINTER_DOWN
|
|
373
|
+
pointerId: P2, pressure: 0.45, actionButton: btn, buttons: btn,
|
|
374
|
+
}));
|
|
375
|
+
delays.push(30);
|
|
376
|
+
|
|
377
|
+
// ③ 两指同时 MOVE
|
|
378
|
+
const frameMs = Math.max(8, Math.round(totalMs / steps));
|
|
379
|
+
for (let i = 1; i <= steps; i += 1) {
|
|
380
|
+
const t = i / steps;
|
|
381
|
+
// 第一指:缓慢漂移(摇杆推方向)
|
|
382
|
+
const x1 = p1.x + driftX * t + (Math.random() - 0.5) * 2;
|
|
383
|
+
const y1 = p1.y + driftY * t + (Math.random() - 0.5) * 2;
|
|
384
|
+
// 第二指:从 p2 线性走到 p2To(转视角)
|
|
385
|
+
const x2 = p2.x + (to.x - p2.x) * t;
|
|
386
|
+
const y2 = p2.y + (to.y - p2.y) * t;
|
|
387
|
+
const env = 0.45 + Math.sin(Math.PI * t) * 0.45;
|
|
388
|
+
bufs.push(buildTouch({
|
|
389
|
+
action: ACTION.MOVE, x: x1, y: y1, screenW, screenH,
|
|
390
|
+
pointerId: P1, pressure: env, actionButton: btn, buttons: btn,
|
|
391
|
+
}));
|
|
392
|
+
bufs.push(buildTouch({
|
|
393
|
+
action: ACTION.MOVE, x: x2, y: y2, screenW, screenH,
|
|
394
|
+
pointerId: P2, pressure: env, actionButton: btn, buttons: btn,
|
|
395
|
+
}));
|
|
396
|
+
delays.push(frameMs);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ④ 第二指抬起(ACTION_POINTER_UP,index=1)
|
|
400
|
+
bufs.push(buildTouch({
|
|
401
|
+
action: ACTION.UP, x: to.x, y: to.y, screenW, screenH, // scrcpy 自动转 POINTER_UP
|
|
402
|
+
pointerId: P2, pressure: 0.3, actionButton: 0, buttons: 0,
|
|
403
|
+
}));
|
|
404
|
+
delays.push(30);
|
|
405
|
+
|
|
406
|
+
// ⑤ 第一指抬起
|
|
407
|
+
if (o.releaseP1 !== false) {
|
|
408
|
+
bufs.push(buildTouch({
|
|
409
|
+
action: ACTION.UP, x: p1.x + driftX, y: p1.y + driftY, screenW, screenH,
|
|
410
|
+
pointerId: P1, pressure: 0.2, actionButton: 0, buttons: 0,
|
|
411
|
+
}));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return { bufs, delays, totalMs };
|
|
415
|
+
}
|