-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
executable file
·355 lines (286 loc) · 10.4 KB
/
functions.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include "functions.h"
// functions to get all running window names
Window *list (Display *disp, unsigned long *len) {
Atom prop = XInternAtom(disp,"_NET_CLIENT_LIST",False), type;
int form;
unsigned long remain;
unsigned char *list;
if (XGetWindowProperty(disp,XDefaultRootWindow(disp),prop,0,1024,False,XA_WINDOW,&type,&form,len,&remain,&list) != Success) {
return 0;
}
return (Window*)list;
}
char *name (Display *disp, Window win) {
Atom prop = XInternAtom(disp,"WM_NAME",False), type;
int form;
unsigned long remain, len;
unsigned char *list;
if (XGetWindowProperty(disp,win,prop,0,1024,False,AnyPropertyType,&type,&form,&len,&remain,&list) != Success) {
return NULL;
}
return (char*)list;
}
// function to simulate mouse click
void mouseClick(Display *display, int button, Window win)
{
XEvent event;
memset(&event, 0x00, sizeof(event));
event.type = ButtonPress;
event.xbutton.button = button;
event.xbutton.same_screen = True;
XQueryPointer(display, RootWindow(display, DefaultScreen(display)), &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
event.xbutton.subwindow = event.xbutton.window;
while(event.xbutton.subwindow)
{
event.xbutton.window = event.xbutton.subwindow;
XQueryPointer(display, event.xbutton.window, &event.xbutton.root, &event.xbutton.subwindow, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
}
if(XSendEvent(display, win, True, 0xfff, &event) == 0)
fprintf(stderr, "Error cannot send press event !\n");
XFlush(display);
usleep(100000);
event.type = ButtonRelease;
event.xbutton.state = 0x100;
if(XSendEvent(display, win, True, 0xfff, &event) == 0)
fprintf(stderr, "Error cannot send release event !\n");
XFlush(display);
}
// Get mouse coordinates
void mouseCoords(Display *display, Window root, int *x, int *y)
{
XEvent event;
XQueryPointer (display, root, &event.xbutton.root, &event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root, &event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
*x = event.xbutton.x;
*y = event.xbutton.y;
}
// function that move mouse only
void moveMouse(Display* display, Window root, int x, int y)
{
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
XFlush (display);
}
// function that move mouse and left click
void mouseMoveAndLeftClick(Display *display, Window win, int x, int y)
{
moveMouse(display, DefaultRootWindow(display), x, y);
mouseClick(display, Button1, win);
}
// function append text to console and log file
void AppendToLogAndConsole(wxString msg, wxTextCtrl* txtConsole)
{
// get date & time
wxDateTime dt = wxDateTime::Now();
// Create full msg string (include time ...)
wxString strToApprend = _("[") + dt.FormatTime() + _("] - ") + msg;
// Append To Console
txtConsole->AppendText(strToApprend + _("\n"));
wxMilliSleep(100); // on attend 100 millisecondes (pour éviter les crash)
// Append To Log File
wxTextFile file(dt.FormatISODate() + _(".txt"));
if (file.Exists())
file.Open();
else
file.Create();
file.AddLine(strToApprend);
file.Write();
file.Close();
}
// function append text to IA log
void AppendToIALog(wxString msg)
{
// get date & time
wxDateTime dt = wxDateTime::Now();
// Create full msg string (include time ...)
wxString strToApprend = _("[") + dt.FormatTime() + _("] - ") + msg;
// Append To Log File
wxTextFile file(dt.FormatISODate() + _("_IA.txt"));
if (file.Exists())
file.Open();
else
{
file.Create();
file.AddLine(_("[IA Log]"));
}
file.AddLine(strToApprend);
file.Write();
file.Close();
}
// Function to create a keyboard event
XKeyEvent createKeyEvent(Display *display, Window &win, Window &winRoot, bool press, int keycode, int modifiers)
{
XKeyEvent event;
event.display = display;
event.window = win;
event.root = winRoot;
event.subwindow = None;
event.time = CurrentTime;
event.x = 1;
event.y = 1;
event.x_root = 1;
event.y_root = 1;
event.same_screen = True;
event.keycode = XKeysymToKeycode(display, keycode);
event.state = modifiers;
if(press)
event.type = KeyPress;
else
event.type = KeyRelease;
return event;
}
// function to press key
void pressKey(Display* display, Window root, Window gameWindow, wxString keyToPress)
{
KeySym keycode;
XKeyEvent event;
bool ctrlMod = false;
if (keyToPress.Contains(_("+"))) // donc c'est un CTRL + ..
{
ctrlMod = true;
// split by '+'
wxStringTokenizer st2(keyToPress, _("+"));
wxString ctrlStr = st2.GetNextToken();
keyToPress = st2.GetNextToken();
}
if (ctrlMod)
{
keycode = XK_Control_L;
// Send a fake key press event to the window.
event = createKeyEvent(display, gameWindow, root, true, keycode, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XFlush(display);
}
// convert key to keySym/int
if (keyToPress == _("ESC")) // si ECHAP, conversion manuelle
keycode = XK_Escape;
else
keycode = XStringToKeysym(keyToPress.mb_str());
// Send a fake key press event to the window.
event = createKeyEvent(display, gameWindow, root, true, keycode, 0);
XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);
XFlush(display);
// Send a fake key release event to the window.
event = createKeyEvent(display, gameWindow, root, false, keycode, 0);
XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *)&event);
XFlush(display);
if (ctrlMod)
{
// Send a fake key release event to the window.
event = createKeyEvent(display, gameWindow, root, false, keycode, 0);
XSendEvent(event.display, event.window, True, KeyReleaseMask, (XEvent *)&event);
XFlush(display);
}
}
// function to check internet connection
bool checkInternetConnection()
{
struct addrinfo hints, *res, *p;
int status, sockfd;
char ipstr[INET6_ADDRSTRLEN];
bool isConnected;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
if ((status = getaddrinfo("www.google.com", "http", &hints, &res)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return false;
}
for(p = res;p != NULL; p = p->ai_next) {
void *addr;
//char *ipver;
// get the pointer to the address itself,
// different fields in IPv4 and IPv6:
if (p->ai_family == AF_INET)
{ // IPv4
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
//ipver = "IPv4";
}
else
{ // IPv6
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
//ipver = "IPv6";
}
// convert the IP to a string
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
}
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(connect(sockfd, res->ai_addr, res->ai_addrlen) == -1)
{
//printf("Connection error\n");
isConnected = false;
}
else
{
//printf("Connection successful\n");
isConnected = true;
}
close(sockfd);
freeaddrinfo(res); // free the linked list
return isConnected;
}
// function to show window on top
void setWindowOntop(Display* display, Window w, int windowState)
{
Atom stateAbove = XInternAtom(display, "_NET_WM_STATE_ABOVE", False);
XChangeProperty(display, w, XInternAtom(display, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *) &stateAbove, 1);
XEvent ev;
ev.xclient.type = ClientMessage;
ev.xclient.message_type = XInternAtom(display, "_NET_WM_STATE", False);
ev.xclient.window = w;
ev.xclient.format = 32;
ev.xclient.data.l[0] = windowState;
ev.xclient.data.l[1] = stateAbove;
ev.xclient.data.l[2] = 0;
ev.xclient.data.l[3] = 0;
ev.xclient.data.l[4] = 0;
XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &ev);
XFlush(display);
}
// functions to get pixel color & check it
void getPixelColor(Display *d, int x, int y, XColor *color)
{
XImage *image;
image = XGetImage (d, RootWindow (d, DefaultScreen (d)), x, y, 1, 1, AllPlanes, XYPixmap);
color->pixel = XGetPixel (image, 0, 0);
XFree (image);
XQueryColor (d, DefaultColormap(d, DefaultScreen (d)), color);
}
bool checkPixelColor(Display* display, GameCase btn)
{
XColor pixelColor;
getPixelColor(display, btn.x, btn.y, &pixelColor);
if (pixelColor.red/256 == btn.red && pixelColor.blue/256 == btn.blue && pixelColor.green/256 == btn.green)
return true;
else
return false;
}
bool checkColorsSimilarity(XColor c1, int c2Red, int c2Blue, int c2Green, int maxDifference)
{
// converting to correct RGB
c1.red /= 256;
c1.blue /= 256;
c1.green /= 256;
for (int i = 0; i <= maxDifference; i++)
{
// si la couleur rouge + différence (qui varie de 0 à maxDifference) == la 2ème couleur rouge ou l'invers
if ((c1.red + i == c2Red) || (c2Red + i == c1.red))
{
for (int j = 0; j <= maxDifference; j++)
{
// si la couleur bleu + différence (qui varie de 0 à maxDifference) == la 2ème couleur bleu ou l'invers
if ((c1.blue + j == c2Blue) || (c2Blue + j == c1.blue))
{
for (int k = 0; k <= maxDifference; k++)
{
// si la couleur verte + différence (qui varie de 0 à maxDifference) == la 2ème couleur verte ou l'invers
if ((c1.green + k == c2Green) || (c2Green + k == c1.green))
return true; // c bon les 2 couleurs se ressemble
} // fin for k
}
} // fin for j
}
} // fin for i
// aucune ressemblance
return false;
}