-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathguiinject.cpp
More file actions
380 lines (336 loc) · 9.38 KB
/
guiinject.cpp
File metadata and controls
380 lines (336 loc) · 9.38 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "guiinject.h"
#include <QFile>
#include <QApplication>
#include <QWidget>
#include <QMouseEvent>
#include <QComboBox>
#include "startuphelper.h"
#define LIB_INIT_FUNC __attribute__((constructor))
LIB_INIT_FUNC void guiInjectInit()
{
qDebug() << "lib init";
putenv(QString("LD_PRELOAD=").toLatin1().data());
StartupHelper* initHelper = new StartupHelper(guiInject);
QObject::connect(initHelper, SIGNAL(startupComplete()), initHelper, SLOT(deleteLater()));
initHelper->watchForStartup();
}
void guiInject()
{
const char* TOK = "GI_TOKEN";
QString token = QString(getenv(TOK));
qDebug() << "lib inject with token " << token;
putenv(QString("GI_TOKEN=").toLatin1().data());
new GuiInject(QCoreApplication::instance(), token);
}
GuiInject::GuiInject(QObject* parent, const QString& token)
: QObject(parent)
,_token(token)
,_server(nullptr)
,_objMap(QHash<QString, QObject*>())
,_objMapIdx(QHash<QObject*, QString>())
,_pick(new DirectPick(parent))
{
_server = new MaiaXmlRpcServer(8888, this);
// dynamic RF api
_server->addMethod("get_keyword_names", this, "getKeywordNames");
_server->addMethod("run_keyword", this, "runKeyword");
_server->addMethod("get_keyword_arguments", this, "getKeywordArguments");
_server->addMethod("get_keyword_documentation", this, "getKeywordDoc");
_server->addMethod("stop_remote_server", this, "stopRemoteServer");
_pick->start();
createObjMap();
_pick->setMap(&_objMapIdx);
}
QVariantList GuiInject::getKeywordNames()
{
QVariantList list;
list.append(CMD_PING);
list.append(CMD_READ_ALL);
list.append(CMD_CLICK);
list.append(CMD_KEYPRESS);
list.append(CMD_READ_PROP);
list.append(CMD_SET_PROP);
list.append(CMD_SET_COMBO_IDX);
list.append(CMD_FIND_PATH);
list.append(CMD_PICK_START);
list.append(CMD_PICK_STOP);
list.append(CMD_REFRESH);
return list;
}
QVariantMap GuiInject::runKeyword(QString name, QVariantList args)
{
qDebug() << "executing" << name;
QVariantMap result;
result["status"] = "FAIL";
if (!checkArguments(name, args.count(), result))
{
return result;
}
if (name == CMD_PING)
{
result["return"] = ping(args[0].toString());
}
else if (name == CMD_READ_ALL)
{
result["return"] = readAllObjects();
}
else if (name == CMD_CLICK)
{
click(args[0].toString());
result["return"] = "";
}
else if (name == CMD_KEYPRESS)
{
keyPress(args[0].toString());
result["return"] = "";
}
else if ( name == CMD_READ_PROP )
{
result["return"] = readProperty(args[0].toString(), args[1].toString());
}
else if ( name == CMD_SET_PROP )
{
if ( setProperty(args[0].toString(), args[1].toString(), args[2].toString()) )
result["return"] = "true";
else
return result;
}
else if ( name == CMD_SET_COMBO_IDX )
{
result["return"] = setComboIdx(args[0].toString(), args[1].toInt());
}
else if ( name == CMD_FIND_PATH )
{
result["return"] = findPaths(args[0].toList());
}
else if ( name == CMD_PICK_START )
{
_pick->start();
result["return"] = "true";
}
else if ( name == CMD_PICK_STOP )
{
_pick->stop();
result["return"] = "true";
}
else if ( name == CMD_REFRESH )
{
createObjMap();
result["return"] = "true";
}
result["status"] = "PASS";
return result;
}
QVariantList GuiInject::getKeywordArguments(QString name)
{
QVariantList list;
QList<QString> oneStringParam {CMD_PING, CMD_CLICK, CMD_KEYPRESS};
QList<QString> oneListStringParam {CMD_FIND_PATH};
QList<QString> twoStringParam {CMD_READ_PROP, CMD_SET_COMBO_IDX};
QList<QString> threeStringParam {CMD_SET_PROP};
if (oneStringParam.indexOf( name )!= -1)
{
list.append("QString");
}
else if (oneListStringParam.indexOf(name) != -1)
{
list.append("QVariantList<QString>");
}
else if (twoStringParam.indexOf(name) != -1)
{
list.append("QString");
list.append("QString");
}
else if (threeStringParam.indexOf(name) != -1)
{
list.append("QString");
list.append("QString");
list.append("QString");
}
return list;
}
QString GuiInject::getKeywordDoc(QString name)
{
if(name == CMD_PING)
return QString("retruns pong + string");
if(name == CMD_CLICK)
return QString("click on object with name");
if(name == CMD_KEYPRESS)
return QString("sends text string to focused object");
if(name == CMD_READ_ALL)
return QString("retrun list of all objects");
if(name == CMD_READ_PROP)
return QString("retrun property of selected object");
if(name == CMD_SET_PROP)
return QString("set property of selected object");
if(name == CMD_SET_COMBO_IDX)
return QString("set combobox by index");
if(name == CMD_FIND_PATH)
return QString("find path based on string list");
return QString();
}
bool GuiInject::stopRemoteServer()
{
return true;
}
void GuiInject::readObjectTree(QHash<QString, QObject*>& map, QObject* obj, QString path)
{
QString name = obj->objectName();
path = QString("%1_%2").arg(path).arg(name);
for(auto& child : obj->children())
{
readObjectTree(map, child, path);
}
if (QWidget* pb = (QWidget*)obj)
{
Q_UNUSED(pb);
if (path.contains(_token))
{
map[path] = obj;
_objMapIdx[obj] = path;
}
}
}
void GuiInject::createObjMap()
{
auto widgets = QApplication::topLevelWidgets();
QString path = QString();
_objMap.clear();
_objMapIdx.clear();
for(auto& object : widgets)
{
readObjectTree(_objMap, object, path);
}
}
bool GuiInject::checkArguments(QString command, int count, QVariantMap &result)
{
int countShouldBe = getKeywordArguments(command).count();
if (count < countShouldBe)
{
result["error"] = QString("Invalit arguments number %1/%2").arg(count).arg(countShouldBe);
return false;
}
return true;
}
QString GuiInject::ping(QString str)
{
QString ret = QString("pong %1").arg(str);
return ret;
}
QVariantList GuiInject::readAllObjects()
{
QVariantList list;
for(auto& key : _objMap.keys())
{
list.append(key);
}
return list;
}
QVariantList GuiInject::findPaths(QVariantList marks)
{
QVariantList list;
for (auto& key : _objMap.keys())
{
//qDebug() << key;
bool escape = false;
for (auto& mark : marks)
{
qDebug() << mark;
if (!key.contains(mark.toString()))
escape = true;
}
if (escape)
continue;
list.append(key);
}
return list;
}
void GuiInject::click(QString objName)
{
QObject *obj = _objMap[objName];
if (!obj)
{
qDebug() << QString("Object %1 not found !").arg(objName);
}
// click on the desired object
if (QWidget* pb = (QWidget*)obj)
{
int w = pb->width();
int h = pb->height();
QPoint pos(w/2, h/2);
QApplication::postEvent(pb, new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier) );
QApplication::postEvent(pb, new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::MouseButton::LeftButton, Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier) );
}
}
void GuiInject::keyPress(QString key)
{
QObject *obj = QApplication::focusWidget();
if (!obj)
{
qDebug() << QString("No focused object");
return;
}
if (QWidget* pb = (QWidget*)obj)
{
pb->setFocus();
QKeyEvent keyPress( QKeyEvent::KeyPress, key.at(0).toLatin1(), Qt::NoModifier, key, false, 0 );
QApplication::sendEvent( pb, &keyPress );
QKeyEvent keyRelease( QKeyEvent::KeyRelease, key.at(0).toLatin1(), Qt::NoModifier, key, false, 0 );
QApplication::sendEvent( pb, &keyRelease );
}
}
QString GuiInject::readProperty(QString objName, QString property)
{
QString ret;
QObject *obj = _objMap[objName];
if (!obj)
{
qDebug() << QString("Object %1 not found !").arg(objName);
}
if (QWidget* pb = (QWidget*)obj)
{
QVariant var = pb->property(property.toStdString().c_str());
ret = var.toString();
if (ret.isEmpty())
{
qDebug() << QString("Property %1 not found").arg(property);
}
}
return ret;
}
bool GuiInject::setProperty(QString objName, QString property, QString value)
{
bool ret = false;
QObject *obj = _objMap[objName];
if (!obj)
{
qDebug() << QString("Object %1 not found !").arg(objName);
}
if (QWidget* pb = (QWidget*)obj)
{
ret = pb->setProperty(property.toStdString().c_str(), QVariant(value));
}
return ret;
}
bool GuiInject::setComboIdx(QString objName, int index)
{
bool ret = false;
QObject *obj = _objMap[objName];
if (!obj)
{
qDebug() << QString("Object %1 not found !").arg(objName);
}
if (QComboBox* pb = (QComboBox*)obj)
{
if (index <= pb->count())
{
pb->setCurrentIndex(index);
ret = true;
}
else
{
ret = false;
}
}
return ret;
}