-
Notifications
You must be signed in to change notification settings - Fork 1
/
Touch.cpp
190 lines (161 loc) · 5.77 KB
/
Touch.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <ApplicationServices/ApplicationServices.h>
void simulateMouseClick(int x, int y) {
CGEventRef clickEvent = CGEventCreateMouseEvent(
nullptr, kCGEventLeftMouseDown, CGPointMake(x, y), kCGEventLeftMouseDown);
CGEventRef releaseEvent = CGEventCreateMouseEvent(
nullptr, kCGEventLeftMouseUp, CGPointMake(x, y), kCGEventLeftMouseUp);
CGEventPost(kCGHIDEventTap, clickEvent);
CGEventPost(kCGHIDEventTap, releaseEvent);
CFRelease(clickEvent);
CFRelease(releaseEvent);
}
void execute_type(const std::vector<std::string>& tokens) {
if (tokens.size() > 1) {
std::string textToType;
for (size_t i = 1; i < tokens.size(); ++i) {
textToType += tokens[i];
if (i < tokens.size() - 1) {
textToType += ' ';
}
}
for (size_t i = 0; i < textToType.length(); ++i) {
char c = textToType[i];
CGEventRef keyDownEvent = CGEventCreateKeyboardEvent(nullptr, 0, true);
UniChar oneChar = c;
CGEventKeyboardSetUnicodeString(keyDownEvent, 1, &oneChar);
CGEventPost(kCGHIDEventTap, keyDownEvent);
CFRelease(keyDownEvent);
CGEventRef keyUpEvent = CGEventCreateKeyboardEvent(nullptr, 0, false);
CGEventKeyboardSetUnicodeString(keyUpEvent, 1, &oneChar);
CGEventPost(kCGHIDEventTap, keyUpEvent);
CFRelease(keyUpEvent);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
} else {
std::cerr << "Missing argument for the 'type' command" << std::endl;
}
}
void execute_wait(const std::vector<std::string>& tokens) {
if (tokens.size() > 1) {
double seconds = std::stod(tokens[1]);
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(seconds * 1000)));
} else {
std::cerr << "Missing argument for the 'wait' command" << std::endl;
}
}
void execute_hold(const std::vector<std::string>& tokens) {
if (tokens.size() > 1) {
double duration = std::stod(tokens[1]);
std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<int>(duration * 1000)));
} else {
std::cerr << "Missing argument for the 'hold' command" << std::endl;
}
}
void execute_click(const std::vector<std::string>& tokens) {
if (tokens.size() > 1) {
size_t commaPos = tokens[1].find(',');
if (commaPos != std::string::npos) {
std::string xStr = tokens[1].substr(0, commaPos);
std::string yStr = tokens[1].substr(commaPos + 1);
int x = std::stoi(xStr);
int y = std::stoi(yStr);
simulateMouseClick(x, y);
} else {
std::cerr << "Invalid argument format for the 'click' command" << std::endl;
}
} else {
std::cerr << "Missing argument for the 'click' command" << std::endl;
}
}
void execute_command(const std::vector<std::string>& tokens) {
if (tokens[0] == "click") {
execute_click(tokens);
} else if (tokens[0] == "wait") {
execute_wait(tokens);
} else if (tokens[0] == "hold") {
execute_hold(tokens);
} else if (tokens[0] == "type") {
execute_type(tokens);
} else {
std::cerr << "Unknown command: " << tokens[0] << std::endl;
}
}
void execute_touch_script(std::vector<std::string>& lines, std::vector<std::string>& args, int& i) {
while (i < lines.size()) {
std::string line = lines[i];
if (line[0] == '#') {
i++;
continue;
}
std::vector<std::string> tokens;
size_t pos = 0;
std::string delimiter = " ";
while ((pos = line.find(delimiter)) != std::string::npos) {
std::string token = line.substr(0, pos);
tokens.push_back(token);
line.erase(0, pos + delimiter.length());
}
tokens.push_back(line);
if (tokens[0] == "<loop") {
if (tokens.size() > 1) {
int loop_count = std::stoi(tokens[1]);
int loop_start = i;
int loop_end = -1;
for (int j = i + 1; j < lines.size(); j++) {
if (lines[j] == "</loop>") {
loop_end = j;
break;
}
}
if (loop_end != -1) {
for (int k = 0; k < loop_count; k++) {
execute_touch_script(lines, args, i);
}
i = loop_end + 1;
} else {
std::cerr << "Missing '</loop>' for the current '<loop>'" << std::endl;
}
} else {
std::cerr << "Missing argument for the '<loop>' command" << std::endl;
}
} else if (line == "</loop>") {
i++;
} else {
execute_command(tokens);
i++;
}
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args;
for (int i = 1; i < argc; ++i) {
args.push_back(argv[i]);
}
if (args.size() == 0) {
std::cout << "Please enter the path to the script file: ";
std::string file_path;
std::cin >> file_path;
args.push_back(file_path);
}
std::string file_path = args[0];
args.erase(args.begin());
std::ifstream input_file(file_path);
if (!input_file.is_open()) {
std::cerr << "File not found" << std::endl;
return 1;
}
std::vector<std::string> commands;
std::string line;
while (std::getline(input_file, line)) {
commands.push_back(line);
}
int i = 0;
execute_touch_script(commands, args, i);
return 0;
}