Skip to content

Commit

Permalink
perf: 不再忽略部分触控实现的返回值
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Sep 14, 2023
1 parent c81e2e5 commit efbf71c
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 123 deletions.
47 changes: 35 additions & 12 deletions source/MaaControlUnit/Input/MaatouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,15 @@ bool MaatouchInput::click(int x, int y)

LogInfo << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool res = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));

if (!res) {
LogError << "click failed";
if (!ret) {
LogError << "failed to write";
return false;
}

// sleep?
return true;
return ret;
}

bool MaatouchInput::swipe(int x1, int y1, int x2, int y2, int duration)
Expand Down Expand Up @@ -232,6 +231,11 @@ bool MaatouchInput::swipe(int x1, int y1, int x2, int y2, int duration)
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

Expand All @@ -241,15 +245,15 @@ bool MaatouchInput::press_key(int key)
return false;
}

bool res = shell_handler_->write(MAA_FMT::format("k {} d\nc\n", key)) &&
bool ret = shell_handler_->write(MAA_FMT::format("k {} d\nc\n", key)) &&
shell_handler_->write(MAA_FMT::format("k {} u\nc\n", key));

if (!res) {
if (!ret) {
LogError << "failed to write";
return false;
}

// sleep?
return true;
return ret;
}

bool MaatouchInput::touch_down(int contact, int x, int y, int pressure)
Expand All @@ -263,7 +267,14 @@ bool MaatouchInput::touch_down(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

return shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MaatouchInput::touch_move(int contact, int x, int y, int pressure)
Expand All @@ -277,7 +288,13 @@ bool MaatouchInput::touch_move(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

return shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MaatouchInput::touch_up(int contact)
Expand All @@ -289,7 +306,13 @@ bool MaatouchInput::touch_up(int contact)

LogInfo << VAR(contact);

return shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));
bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));
if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

MAA_CTRL_UNIT_NS_END
41 changes: 33 additions & 8 deletions source/MaaControlUnit/Input/MinitouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,15 @@ bool MinitouchInput::click(int x, int y)

LogInfo << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool res = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));

if (!res) {
LogError << "click failed";
if (!ret) {
LogError << "failed to write";
return false;
}

// sleep?
return true;
return ret;
}

bool MinitouchInput::swipe(int x1, int y1, int x2, int y2, int duration)
Expand Down Expand Up @@ -254,6 +253,11 @@ bool MinitouchInput::swipe(int x1, int y1, int x2, int y2, int duration)
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

Expand All @@ -268,7 +272,14 @@ bool MinitouchInput::touch_down(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

return shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MinitouchInput::touch_move(int contact, int x, int y, int pressure)
Expand All @@ -282,7 +293,14 @@ bool MinitouchInput::touch_move(int contact, int x, int y, int pressure)

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

return shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
bool ret = shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MinitouchInput::touch_up(int contact)
Expand All @@ -294,7 +312,14 @@ bool MinitouchInput::touch_up(int contact)

LogInfo << VAR(contact);

return shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));
bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

MAA_CTRL_UNIT_NS_END
26 changes: 17 additions & 9 deletions source/MaaFramework/API/MaaAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,6 @@ MaaBool MaaClearCustomAction(MaaInstanceHandle inst)
}

inst->clear_custom_action();

return true;
}

Expand Down Expand Up @@ -821,8 +820,7 @@ MaaBool MaaSyncContextClick(MaaSyncContextHandle sync_context, int32_t x, int32_
return false;
}

sync_context->click(x, y);
return true;
return sync_context->click(x, y);
}

MaaBool MaaSyncContextSwipe(MaaSyncContextHandle sync_context, int32_t x1, int32_t y1, int32_t x2, int32_t y2,
Expand All @@ -835,8 +833,7 @@ MaaBool MaaSyncContextSwipe(MaaSyncContextHandle sync_context, int32_t x1, int32
return false;
}

sync_context->swipe(x1, y1, x2, y2, duration);
return true;
return sync_context->swipe(x1, y1, x2, y2, duration);
}

MaaBool MaaSyncContextPressKey(MaaSyncContextHandle sync_context, int32_t keycode)
Expand All @@ -848,8 +845,7 @@ MaaBool MaaSyncContextPressKey(MaaSyncContextHandle sync_context, int32_t keycod
return false;
}

sync_context->press_key(keycode);
return true;
return sync_context->press_key(keycode);
}

MaaBool MaaSyncContextTouchDown(MaaSyncContextHandle sync_context, int32_t contact, int32_t x, int32_t y,
Expand Down Expand Up @@ -899,7 +895,13 @@ MaaBool MaaSyncContextScreencap(MaaSyncContextHandle sync_context, MaaImageBuffe
return false;
}

buffer->set(sync_context->screencap());
auto img = sync_context->screencap();
if (img.empty()) {
LogError << "image is empty";
return false;
}

buffer->set(std::move(img));
return true;
}

Expand All @@ -912,6 +914,12 @@ MaaBool MaaSyncContextGetTaskResult(MaaSyncContextHandle sync_context, MaaString
return false;
}

buffer->set(sync_context->task_result(task));
auto res = sync_context->task_result(task);
if (res.empty()) {
LogError << "res is empty";
return false;
}

buffer->set(std::move(res));
return true;
}
6 changes: 3 additions & 3 deletions source/MaaFramework/API/MaaTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ struct MaaSyncContextAPI
/*out*/ cv::Rect& box, /*out*/ std::string& detail) = 0;
virtual bool run_action(std::string task, std::string_view param, cv::Rect cur_box, std::string cur_detail) = 0;

virtual void click(int x, int y) = 0;
virtual void swipe(int x1, int y1, int x2, int y2, int duration) = 0;
virtual void press_key(int keycode) = 0;
virtual bool click(int x, int y) = 0;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) = 0;
virtual bool press_key(int keycode) = 0;
virtual cv::Mat screencap() = 0;

virtual bool touch_down(int contact, int x, int y, int pressure) = 0;
Expand Down
18 changes: 12 additions & 6 deletions source/MaaFramework/Controller/AdbController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,32 +116,36 @@ std::pair<int, int> AdbController::_get_resolution() const
return resolution_;
}

void AdbController::_click(ClickParam param)
bool AdbController::_click(ClickParam param)
{
if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) {
LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj());
return;
return false;
}

bool ret = unit_mgr_->touch_input_obj()->click(param.x, param.y);

if (!ret) {
LogError << "failed to click";
}

return ret;
}

void AdbController::_swipe(SwipeParam param)
bool AdbController::_swipe(SwipeParam param)
{
if (!unit_mgr_ || !unit_mgr_->touch_input_obj()) {
LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->touch_input_obj());
return;
return false;
}

bool ret = unit_mgr_->touch_input_obj()->swipe(param.x1, param.y1, param.x2, param.y2, param.duration);

if (!ret) {
LogError << "failed to swipe";
}

return ret;
}

bool AdbController::_touch_down(TouchParam param)
Expand Down Expand Up @@ -192,18 +196,20 @@ bool AdbController::_touch_up(TouchParam param)
return ret;
}

void AdbController::_press_key(PressKeyParam param)
bool AdbController::_press_key(PressKeyParam param)
{
if (!unit_mgr_ || !unit_mgr_->key_input_obj()) {
LogError << "unit is nullptr" << VAR(unit_mgr_) << VAR(unit_mgr_->key_input_obj());
return;
return false;
}

bool ret = unit_mgr_->key_input_obj()->press_key(param.keycode);

if (!ret) {
LogError << "failed to press_key";
}

return ret;
}

cv::Mat AdbController::_screencap()
Expand Down
6 changes: 3 additions & 3 deletions source/MaaFramework/Controller/AdbController.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class AdbController : public ControllerMgr
protected:
virtual bool _connect() override;
virtual std::pair<int, int> _get_resolution() const override;
virtual void _click(ClickParam param) override;
virtual void _swipe(SwipeParam param) override;
virtual bool _click(ClickParam param) override;
virtual bool _swipe(SwipeParam param) override;
virtual bool _touch_down(TouchParam param) override;
virtual bool _touch_move(TouchParam param) override;
virtual bool _touch_up(TouchParam param) override;
virtual void _press_key(PressKeyParam param) override;
virtual bool _press_key(PressKeyParam param) override;
virtual cv::Mat _screencap() override;
virtual bool _start_app(AppParam param) override;
virtual bool _stop_app(AppParam param) override;
Expand Down
Loading

0 comments on commit efbf71c

Please sign in to comment.