forked from MaaXYZ/MaaFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteController.cpp
More file actions
445 lines (402 loc) · 12 KB
/
RemoteController.cpp
File metadata and controls
445 lines (402 loc) · 12 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "RemoteController.h"
#include "MaaAgent/Message.hpp"
#include "MaaUtils/Encoding.h"
#include "MaaUtils/Logger.h"
MAA_AGENT_SERVER_NS_BEGIN
RemoteController::RemoteController(Transceiver& server, const std::string& controller_id)
: server_(server)
, controller_id_(controller_id)
{
}
bool RemoteController::set_option(MaaCtrlOption key, MaaOptionValue value, MaaOptionValueSize val_size)
{
LogFunc << VAR(key) << VAR_VOIDP(value) << VAR(val_size);
if (!value) {
LogError << "value is null" << VAR(key);
return false;
}
json::value jvalue;
switch (key) {
case MaaCtrlOption_ScreenshotTargetLongSide:
case MaaCtrlOption_ScreenshotTargetShortSide:
case MaaCtrlOption_ScreenshotResizeMethod:
if (val_size != sizeof(int32_t)) {
LogError << "invalid val_size for int32_t option" << VAR(key) << VAR(val_size);
return false;
}
jvalue = *reinterpret_cast<const int32_t*>(value);
break;
case MaaCtrlOption_ScreenshotUseRawSize:
case MaaCtrlOption_MouseLockFollow:
if (val_size != sizeof(bool)) {
LogError << "invalid val_size for bool option" << VAR(key) << VAR(val_size);
return false;
}
jvalue = *reinterpret_cast<const bool*>(value);
break;
case MaaCtrlOption_BackgroundManagedKeys: {
if (val_size != 0 && val_size % sizeof(int32_t) != 0) {
LogError << "invalid val_size for int32_t[] option" << VAR(key) << VAR(val_size);
return false;
}
size_t count = val_size / sizeof(int32_t);
auto keycodes = reinterpret_cast<const int32_t*>(value);
json::array arr;
for (size_t i = 0; i < count; ++i) {
arr.emplace_back(keycodes[i]);
}
jvalue = std::move(arr);
break;
}
default:
LogError << "unknown key" << VAR(key);
return false;
}
ControllerSetOptionReverseRequest req {
.controller_id = controller_id_,
.key = key,
.value = std::move(jvalue),
};
auto resp_opt = server_.send_and_recv<ControllerSetOptionReverseResponse>(req);
if (!resp_opt) {
return false;
}
return resp_opt->ret;
}
MaaCtrlId RemoteController::post_connection()
{
ControllerPostConnectionReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerPostConnectionReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_click(int x, int y, int contact, int pressure)
{
ControllerPostClickReverseRequest req {
.controller_id = controller_id_,
.x = x,
.y = y,
.contact = contact,
.pressure = pressure,
};
auto resp_opt = server_.send_and_recv<ControllerPostClickReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_swipe(int x1, int y1, int x2, int y2, int duration, int contact, int pressure)
{
ControllerPostSwipeReverseRequest req {
.controller_id = controller_id_,
.x1 = x1,
.y1 = y1,
.x2 = x2,
.y2 = y2,
.duration = duration,
.contact = contact,
.pressure = pressure,
};
auto resp_opt = server_.send_and_recv<ControllerPostSwipeReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_click_key(int keycode)
{
ControllerPostClickKeyReverseRequest req {
.controller_id = controller_id_,
.keycode = keycode,
};
auto resp_opt = server_.send_and_recv<ControllerPostClickKeyReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_input_text(const std::string& text)
{
ControllerPostInputTextReverseRequest req {
.controller_id = controller_id_,
.text = text,
};
auto resp_opt = server_.send_and_recv<ControllerPostInputTextReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_start_app(const std::string& intent)
{
ControllerPostStartAppReverseRequest req {
.controller_id = controller_id_,
.intent = intent,
};
auto resp_opt = server_.send_and_recv<ControllerPostStartAppReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_stop_app(const std::string& intent)
{
ControllerPostStopAppReverseRequest req {
.controller_id = controller_id_,
.intent = intent,
};
auto resp_opt = server_.send_and_recv<ControllerPostStopAppReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_screencap()
{
ControllerPostScreencapReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerPostScreencapReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_touch_down(int contact, int x, int y, int pressure)
{
ControllerPostTouchDownReverseRequest req {
.controller_id = controller_id_,
.contact = contact,
.x = x,
.y = y,
.pressure = pressure,
};
auto resp_opt = server_.send_and_recv<ControllerPostTouchDownReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_touch_move(int contact, int x, int y, int pressure)
{
ControllerPostTouchMoveReverseRequest req {
.controller_id = controller_id_,
.contact = contact,
.x = x,
.y = y,
.pressure = pressure,
};
auto resp_opt = server_.send_and_recv<ControllerPostTouchMoveReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_touch_up(int contact)
{
ControllerPostTouchUpReverseRequest req {
.controller_id = controller_id_,
.contact = contact,
};
auto resp_opt = server_.send_and_recv<ControllerPostTouchUpReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_relative_move(int dx, int dy)
{
ControllerPostRelativeMoveReverseRequest req {
.controller_id = controller_id_,
.dx = dx,
.dy = dy,
};
auto resp_opt = server_.send_and_recv<ControllerPostRelativeMoveReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_key_down(int keycode)
{
ControllerPostKeyDownReverseRequest req {
.controller_id = controller_id_,
.keycode = keycode,
};
auto resp_opt = server_.send_and_recv<ControllerPostKeyDownReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_key_up(int keycode)
{
ControllerPostKeyUpReverseRequest req {
.controller_id = controller_id_,
.keycode = keycode,
};
auto resp_opt = server_.send_and_recv<ControllerPostKeyUpReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_scroll(int dx, int dy)
{
ControllerPostScrollReverseRequest req {
.controller_id = controller_id_,
.dx = dx,
.dy = dy,
};
auto resp_opt = server_.send_and_recv<ControllerPostScrollReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_shell(const std::string& cmd, int64_t timeout)
{
ControllerPostShellReverseRequest req {
.controller_id = controller_id_,
.cmd = cmd,
.timeout = timeout,
};
auto resp_opt = server_.send_and_recv<ControllerPostShellReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaCtrlId RemoteController::post_inactive()
{
ControllerPostInactiveReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerPostInactiveReverseResponse>(req);
if (!resp_opt) {
return MaaInvalidId;
}
return resp_opt->ctrl_id;
}
MaaStatus RemoteController::status(MaaCtrlId ctrl_id) const
{
ControllerStatusReverseRequest req {
.controller_id = controller_id_,
.ctrl_id = ctrl_id,
};
auto resp_opt = server_.send_and_recv<ControllerStatusReverseResponse>(req);
if (!resp_opt) {
return MaaStatus_Invalid;
}
return static_cast<MaaStatus>(resp_opt->status);
}
MaaStatus RemoteController::wait(MaaCtrlId ctrl_id) const
{
ControllerWaitReverseRequest req {
.controller_id = controller_id_,
.ctrl_id = ctrl_id,
};
auto resp_opt = server_.send_and_recv<ControllerWaitReverseResponse>(req);
if (!resp_opt) {
return MaaStatus_Invalid;
}
return static_cast<MaaStatus>(resp_opt->status);
}
bool RemoteController::connected() const
{
ControllerConnectedReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerConnectedReverseResponse>(req);
if (!resp_opt) {
return false;
}
return resp_opt->ret;
}
bool RemoteController::running() const
{
ControllerRunningReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerRunningReverseResponse>(req);
if (!resp_opt) {
return false;
}
return resp_opt->ret;
}
cv::Mat RemoteController::cached_image() const
{
ControllerCachedImageReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerCachedImageReverseResponse>(req);
if (!resp_opt) {
return { };
}
return server_.get_image_cache(resp_opt->image);
}
std::string RemoteController::cached_shell_output() const
{
ControllerGetShellOutputReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerGetShellOutputReverseResponse>(req);
if (!resp_opt) {
return { };
}
return resp_opt->output;
}
std::string RemoteController::get_uuid()
{
ControllerGetUuidReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerGetUuidReverseResponse>(req);
if (!resp_opt) {
return { };
}
return resp_opt->uuid;
}
bool RemoteController::get_resolution(int32_t& width, int32_t& height) const
{
ControllerGetResolutionReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerGetResolutionReverseResponse>(req);
if (!resp_opt) {
return false;
}
width = resp_opt->width;
height = resp_opt->height;
return resp_opt->success;
}
json::object RemoteController::get_info() const
{
ControllerGetInfoReverseRequest req {
.controller_id = controller_id_,
};
auto resp_opt = server_.send_and_recv<ControllerGetInfoReverseResponse>(req);
if (!resp_opt) {
return { };
}
return resp_opt->info;
}
MaaSinkId RemoteController::add_sink(MaaEventCallback callback, void* trans_arg)
{
LogError << "Can NOT add sink for remote instance, use AgentServer.add_controller_sink instead" << VAR_VOIDP(callback)
<< VAR_VOIDP(trans_arg);
return MaaInvalidId;
}
void RemoteController::remove_sink(MaaSinkId sink_id)
{
LogError << "Can NOT remove sink for remote instance" << VAR(sink_id);
}
void RemoteController::clear_sinks()
{
LogError << "Can NOT clear sink for remote instance";
}
MAA_AGENT_SERVER_NS_END