-
Notifications
You must be signed in to change notification settings - Fork 35
/
SlaveViewer.cs
298 lines (270 loc) · 9.4 KB
/
SlaveViewer.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpAdbClient;
using System.Threading;
using System.IO;
using System.Diagnostics;
namespace DroidLord
{
public partial class SlaveViewer : UserControl
{
private Slavery.Slave slave;
private Graphics graph;
private Image curFrame = null;
// 上次轮训窗口状态时间
private DateTime lastQuery;
// 显示右键菜单
public bool ShowMenu
{
get; set;
}
= false;
// 响应鼠标事件
public bool ResponseClick
{
get;
set;
}
public Slavery.Slave GetSlave()
{
return slave;
}
public SlaveViewer()
{
InitializeComponent();
// 鼠标事件
ResponseClick = true;
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MainForm_KeyPress);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainForm_MouseUp);
// 默认大小
this.Width = 180;
this.Height = 360;
// 轮训窗口状态, 被遮挡则断开连接
var timer = new System.Threading.Timer((obj) =>
{
// 轮询间隔保持1秒
if (DateTime.Now.Subtract(lastQuery).TotalMilliseconds > 1000)
{
LayoutScrolled();
}
}, null, 0, 1000);
}
public void LayoutScrolled()
{
if (slave != null)
{
lastQuery = DateTime.Now;
var scRect = RectangleToScreen(ClientRectangle);
if (IsOnScreen(scRect))
{
// 滚动没有遮住
slave.ScreenService.Start();
}
else
{
slave.ScreenService.Stop();
}
}
}
// Return True if at least the percent specified of the rectangle is shown on the total screen area of all monitors, otherwise return False.
public bool IsOnScreen(System.Drawing.Rectangle Rec, double MinPercentOnScreen = 0.001)
{
double PixelsVisible = 0;
foreach (Screen scrn in Screen.AllScreens)
{
System.Drawing.Rectangle r = System.Drawing.Rectangle.Intersect(Rec, scrn.WorkingArea);
// intersect rectangle with screen
if (r.Width != 0 & r.Height != 0)
{
PixelsVisible += (r.Width * r.Height);
// tally visible pixels
}
}
return PixelsVisible >= (Rec.Width * Rec.Height) * MinPercentOnScreen;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (curFrame != null)
{
lock (curFrame)
{
lock (e.Graphics)
{
try
{
e.Graphics.DrawImage(curFrame,
new Rectangle(0, 0, Width, Height),
new Rectangle(0, 0, curFrame.Width, curFrame.Height), GraphicsUnit.Pixel);
}
catch
{
}
}
}
}
}
public void Display(Slavery.Slave sl)
{
this.slave = sl;
slave.ScreenService.FrameArrived += ScreenService_FrameArrived;
graph = this.CreateGraphics();
}
public void Display(DeviceData dev)
{
graph = this.CreateGraphics();
new Thread(() =>
{
slave = new Slavery.Slave(dev, 360, 720);
slave.ScreenService.FrameArrived += ScreenService_FrameArrived;
slave.ScreenService?.Start();
// 加入设备管理器
Program.Slaves.Add(new SlavedDevice() { Key = slave.Device.Serial, Object = slave });
})
{ IsBackground = true }.Start();
}
// 鼠标坐标映射到触屏坐标
Point transTouch(int x, int y)
{
var wid = (double)Width;
var hei = (double)Height;
var dstX = x / wid;
var dstY = y / hei;
return new
Point(
(int)(dstX * slave.ScreenService.Width),
(int)(dstY * slave.ScreenService.Height));
}
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
if (ResponseClick && e.Button == MouseButtons.Left)
{
down = true;
slave.Manipulation.TouchDown(transTouch(e.X, e.Y));
}
}
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
if (ResponseClick)
{
down = false;
slave.Manipulation.TouchUp(transTouch(e.X, e.Y));
}
}
bool down = false;
private void MainForm_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right
&& ResponseClick)
{
if (down)
{
slave.Manipulation.TouchMove(transTouch(e.X, e.Y));
}
var sc = Parent as ScreenForm;
sc.ScreenForm_MouseMove(sender, e);
}
}
private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
{
slave.Manipulation.Keypress(e.KeyChar.ToString());
}
private bool ScreenService_FrameArrived(DeviceData device, object sender, Image frame)
{
if (this.Visible)
{
curFrame = frame;
graph.DrawImage(frame,
new Rectangle(0, 0, Width, Height),
new Rectangle(0, 0, frame.Width, frame.Height), GraphicsUnit.Pixel);
GC.Collect();
return true;
}
return false;
}
private void 命令行ToolStripMenuItem_Click(object sender, EventArgs e)
{
var adbPath = Program.usrDir + @"\bin\adb.exe";
var serial = slave.Device.Serial;
var proc = new Process()
{
StartInfo = new ProcessStartInfo()
{
FileName = adbPath,
Arguments = $"-s \"{serial}\" shell",
WindowStyle = ProcessWindowStyle.Normal
}
};
proc.Start();
}
private void SlaveViewer_MouseDown(object sender, MouseEventArgs e)
{
// 加载中...等待加载完毕
while (slave == null)
{
Thread.Sleep(10);
}
if (e.Button == MouseButtons.Right)
{
if (!ShowMenu)
{
// 只显示按键选项
for(int i = 0; i < devMenu.Items.Count; i++)
{
if (devMenu.Items[i].Name.Contains("keys"))
{
break;
}
devMenu.Items[i].Visible = false;
}
}
devMenu.Items[0].Text = slave.Device.Serial;
devMenu.Show(this, new Point(e.X, e.Y));
}
}
private void 控制设备ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 加载中...等待加载完毕
while (slave == null)
{
Thread.Sleep(10);
}
var frm = new ScreenForm();
var sla = slave;
frm.Width = 360;
frm.Height = 720;
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Display(sla);
frm.ShowDialog();
}
private void 删除设备ToolStripMenuItem_Click(object sender, EventArgs e)
{
Program.Events.Raise(null, "adb_disconnect", slave.Device);
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
slave.Manipulation.Keypress(Slavery.DroidKey.HOME);
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
slave.Manipulation.Keypress(Slavery.DroidKey.POWER);
}
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
slave.Manipulation.Keypress(Slavery.DroidKey.MENU);
}
private void 返回ToolStripMenuItem_Click(object sender, EventArgs e)
{
slave.Manipulation.Keypress(Slavery.DroidKey.BACK);
}
}
}