-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
200 lines (153 loc) · 4.61 KB
/
main.cpp
File metadata and controls
200 lines (153 loc) · 4.61 KB
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
#include <cstring>
#include <iostream>
#include <string>
#include <thread>
#include <linux/input.h>
#include <unistd.h>
#include <fcntl.h>
#include <filesystem>
#include <chrono>
#include <atomic>
#include <fstream>
#include <vector>
#include "header.h"
std::atomic<bool> hold(false);
std::atomic<bool> running(true);
class Trigger {
private:
short delay;
short actionKeyCode;
public:
void setDelay(short delay_in_ms) {
delay = delay_in_ms;
}
void setKey(short keyCode) {
actionKeyCode = keyCode;
}
short getDelay() {
return delay;
}
void screenClip(int width, int height) {
std::string cmd = "grim -g \"" + std::to_string(width) + "," + std::to_string(height) + " 10x10\" clip.png";
system(cmd.c_str());
}
void screenStart(int width, int height){
std::string cmd = "grim -g \"" + std::to_string(width) + "," + std::to_string(height) + " 10x10\" action.png";
system(cmd.c_str());
}
short getKeyCode(){
return actionKeyCode;
}
};
int keyLog(short actionKey, std::string keyboardEvent) {
std::string cmd = "sudo chmod a+r " + keyboardEvent;
system(cmd.c_str()); // needs to access the /dev/input/eventX file
system("ydotoold > /dev/null 2>&1 &"); // ydotool daemon
int fd = open(keyboardEvent.c_str(), O_RDONLY);
if (fd == -1) {
std::cerr << "Failed to open a device " << keyboardEvent << ". Try sudo.\n";
return 1;
}
input_event ev;
while (true) {
ssize_t n = read(fd, &ev, sizeof(ev));
if (n == (ssize_t)sizeof(ev)) {
if (ev.type == EV_KEY) {
if (ev.value == 1) {
if (ev.code == 54) { //rShift to exit
std::cout << "Exit.\n";
running = false;
return 0;
}
}
if(ev.code == actionKey) {
if (ev.value == 1 || ev.value == 2) {
hold = true;
}
if (ev.value == 0) {
hold = false;
}
}
}
}
}
close(fd);
return 0;
}
int main(int argc, char* argv[]) {
int screenWidth;
int screenHeight;
int triggerDelay;
std::string keyboardEvent;
if(!std::filesystem::exists("config.cfg")) {
std::ofstream file("config.cfg");
std::cout << "Enter your screen width: ";
std::cin >> screenWidth;
file << screenWidth << "\n";
std::cout << "Enter your screen height: ";
std::cin >> screenHeight;
file << screenHeight << "\n";
std::cout << "Enter triggerbot delay (70 is highly recommended): ";
std::cin >> triggerDelay;
file << triggerDelay << "\n";
keyboardEvent = findKeyboard();
file << keyboardEvent << "\n";
} else {
if(argc > 1 && strcmp(argv[1], "-k") == 0) {
std::ifstream in("config.cfg");
std::vector<std::string> lines;
for(std::string line; std::getline(in, line);) {
lines.push_back(line);
}
if(!lines.empty()) lines.pop_back();
std::ofstream out("config.cfg");
for(int i=0; i<lines.size(); i++) {
out << lines[i] << "\n";
}
keyboardEvent = findKeyboard();
out << keyboardEvent << "\n";
}
std::ifstream file("config.cfg");
if(file.is_open()) {
file >> screenWidth >> screenHeight >> triggerDelay >> keyboardEvent;
}
}
std::cout << "Using " << screenWidth << "x" << screenHeight << " resolution.\n";
std::cout << "Delay: " << triggerDelay << "ms\n";
screenWidth = (screenWidth / 2) - 5;
screenHeight = (screenHeight / 2) - 5;
Trigger trig;
trig.setDelay((short)triggerDelay); //ms
trig.setKey(58); //CAPSLOCK
std::thread keyThread(keyLog, trig.getKeyCode(), keyboardEvent);
bool clipExist = false;
bool singleClick = false;
const std::filesystem::path currentDir = std::filesystem::current_path();
const std::filesystem::path clip = currentDir / "clip.png";
const std::filesystem::path action = currentDir / "action.png";
while(running){
if (hold){
if (!clipExist) {
trig.screenClip(screenWidth, screenHeight);
clipExist = true;
}
trig.screenStart(screenWidth, screenHeight);
if(differentpixels(clip.string(), action.string())) {
if (!singleClick) {
std::this_thread::sleep_for(std::chrono::milliseconds(trig.getDelay()));
system("ydotool click 0xC0");
std::cout << "SHOT!!!\n";
singleClick = true;
}
}
}
if (!hold) {
clipExist = false;
singleClick = false;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
keyThread.join();
system("pkill ydotoold");
return 0;
}