minitouch控制函数封装
minitouch的github地址:
1.原函数
void click(FILE *wirteFile, const std::string *ADB_IP, int x, int y)
{
std::string s = "d 0 " + std::to_string(x) + " " + std::to_string(y) + " " + "50\n";
fwrite(s.data(), 1, s.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(30);
fwrite("u 0\n", 1, sizeof("u 0"), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
}
void swipe(FILE *wirteFile, const std::string *ADB_IP, int x1, int y1, int x2, int y2, const int time)
{
std::string s = "d 0 " + std::to_string(x1) + " " + std::to_string(y1) + " " + "50\n";
fwrite(s.data(), 1, s.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(time);
float r = (float)(y2 - y1) / (x2 - x1);
std::cout << "r = " << r << std::endl;
short space = 20;
if (x1 < x2)
{
std::cout << "r为正向步进:" << r << " and x1 < x2" << std::endl;
for (size_t i = 0; i * space + x1 < x2; i++)
{
s = "m 0 " + std::to_string(i * space + x1) + " " + std::to_string((short)(y1 + (short)i * space * r)) + " " + "50\n";
// std::cout << s.data()<< std::endl;
fwrite(s.data(), 1, s.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(30);
}
}
else if (x1 >= x2)
{
std::cout << "r为正向步进:" << r << " and *x1 >= *x2" << std::endl;
for (size_t i = 0; x1 - i * space > x2; i++)
{
s = "m 0 " + std::to_string(x1 - i * space) + " " + std::to_string((short)(y1 - (short)i * space * r)) + " " + "50\n";
fwrite(s.data(), 1, s.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(30);
}
}
fwrite("u 0\n", 1, sizeof("u 0"), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
// std::string cmd = "adb -s " + *ADB_IP + " shell input swipe " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + time;
// runShellNoReturn(cmd.data());
}
void doubleSwipe(FILE *wirteFile, const std::string *ADB_IP, int x1, int y1, int x2, int y2, const int time)
{
std::string s0 = "d 0 " + std::to_string(x1) + " " + std::to_string(y1) + " " + "50\n";
std::string s1 = "d 1 " + std::to_string(x2) + " " + std::to_string(y2) + " " + "50\n";
fwrite(s0.data(), 1, s0.length(), wirteFile);
fwrite(s1.data(), 1, s1.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(1000);
float r = (float)(y2 - y1) / (x2 - x1);
std::cout << "r = " << r << std::endl;
short space = 20;
if (x1 < x2)
{
std::cout << "r为正向步进:" << r << " and x1 < x2" << std::endl;
for (size_t i = 0; i * space + x1 < (x1 + x2) / 2; i++)
{
s0 = "m 0 " + std::to_string(i * space + x1) + " " + std::to_string((short)(y1 + (short)i * space * r)) + " " + "50\n";
s1 = "m 1 " + std::to_string(x2 - i * space) + " " + std::to_string((short)(y2 - (short)i * space * r)) + " " + "50\n";
// std::cout << s.data()<< std::endl;
fwrite(s0.data(), 1, s0.length(), wirteFile);
fwrite(s1.data(), 1, s1.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(90);
}
}
else if (x1 >= x2)
{
std::cout << "r为正向步进:" << r << " and *x1 >= *x2" << std::endl;
for (size_t i = 0; x1 - i * space > (x1 + x2) / 2; i++)
{
s0 = "m 0 " + std::to_string(x1 - i * space) + " " + std::to_string((short)(y1 - (short)i * space * r)) + " " + "50\n";
s1 = "m 1 " + std::to_string(i * space + x2) + " " + std::to_string((short)(y2 + (short)i * space * r)) + " " + "50\n";
fwrite(s0.data(), 1, s0.length(), wirteFile);
fwrite(s1.data(), 1, s1.length(), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
sleepMills(90);
}
}
fwrite("u 0\n", 1, sizeof("u 0"), wirteFile);
fwrite("u 1\n", 1, sizeof("u 1"), wirteFile);
fwrite("c\n", 1, sizeof("c"), wirteFile);
fflush(wirteFile);
// std::string cmd = "adb -s " + *ADB_IP + " shell input swipe " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + time;
// runShellNoReturn(cmd.data());
}
void maginfy(FILE *wirteFile, const std::string *ADB_IP)
{
int homeAttackMapX = 977;
int homeAttackMapY = 1769; // 家乡搜索坐标
int homeLookforX = 666;
int homeLookforY = 394; // 搜索地图
doubleSwipe(wirteFile, ADB_IP, homeAttackMapX - 300, homeAttackMapY, homeLookforX - 300, homeLookforY);
}
2.函数解析
*其实整体思路很简单,就是去调用minitouch的api来控制手机
*首先要通过添加popen()来创建一个输入流,也就是mode要设置成“w”,然后接收这个io流指针。
*对这个io流写入命令:
*fwrite()写入时要注意,命令的最后最好带上"\n"。
*每次fwirte("c\n)后,都要通过fflush刷新缓存区。
*每次刷新后,都要适当设置延迟。
*其实触控的运动轨迹是可以设置的很“真人”的,不过算法这块得花上不少功夫。