Skip to content

Commit

Permalink
feat: 新增 press_key 全流程支持,修复一些小问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jul 16, 2023
1 parent e9d5354 commit f6e3c04
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 12 deletions.
9 changes: 8 additions & 1 deletion docs/zh_cn/3.3-任务流水线协议.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

- `action`: *string*
执行的动作。可选,默认 `DoNothing`
可选的值:`DoNothing` | `Click` | `Swipe` | `StartApp` | `StopApp` | `CustomTask`
可选的值:`DoNothing` | `Click` | `Swipe` | `Key` | `StartApp` | `StopApp` | `CustomTask`
详见 [动作类型](#动作类型)

- `next` : *string* | *list<string, >*
Expand Down Expand Up @@ -185,6 +185,13 @@
- `duration`: *uint*
滑动持续时间,单位毫秒。可选,默认 200

### `Key`

按键。

- `key`: *string* | *int* | *list<int, >*
要按的键,仅支持 ascii。

### `StartApp`

启动 App。
Expand Down
5 changes: 4 additions & 1 deletion include/interfaces/ThriftController.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ struct SwipeStep {
struct SwipeParams {
1: list<SwipeStep> steps,
}
struct PressKeyParams {
1: i32 keycode,
}

struct CustomImage {
1: Size size,
Expand All @@ -31,7 +34,7 @@ service ThriftController {
bool connect(),
bool click(1: ClickParams param),
bool swipe(1: SwipeParams param),
bool press_key(1: i32 keycode),
bool press_key(1: PressKeyParams param),

bool start_game(1: string activity),
bool stop_game(1: string activity),
Expand Down
14 changes: 14 additions & 0 deletions source/Controller/AdbController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ void AdbController::_swipe(SwipeParams param)
}
}

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

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

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

cv::Mat AdbController::_screencap()
{
if (!unit_mgr_ || !unit_mgr_->screencap_obj()) {
Expand Down
1 change: 1 addition & 0 deletions source/Controller/AdbController.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AdbController : public ControllerMgr
virtual std::pair<int, int> _get_resolution() const override;
virtual void _click(ClickParams param) override;
virtual void _swipe(SwipeParams param) override;
virtual void _press_key(PressKeyParams param) override;
virtual cv::Mat _screencap() override;
virtual bool _start_app(AppParams param) override;
virtual bool _stop_app(AppParams param) override;
Expand Down
8 changes: 8 additions & 0 deletions source/Controller/ControllerMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ void ControllerMgr::swipe(const cv::Point& p1, const cv::Point& p2, int duration
action_runner_->post({ .type = Action::Type::swipe, .params = std::move(params) }, true);
}

void ControllerMgr::press_key(int keycode)
{
action_runner_->post({ .type = Action::Type::press_key, .params = PressKeyParams { .keycode = keycode } }, true);
}

cv::Mat ControllerMgr::screencap()
{
std::unique_lock<std::mutex> lock(image_mutex_);
Expand Down Expand Up @@ -221,6 +226,9 @@ bool ControllerMgr::run_action(typename AsyncRunner<Action>::Id id, Action actio
case Action::Type::swipe:
_swipe(std::get<SwipeParams>(action.params));
return true;
case Action::Type::press_key:
_press_key(std::get<PressKeyParams>(action.params));
return true;

case Action::Type::screencap:
return postproc_screenshot(_screencap());
Expand Down
9 changes: 8 additions & 1 deletion source/Controller/ControllerMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ struct SwipeParams
};
std::vector<Step> steps;
};
struct PressKeyParams
{
int keycode = 0;
};
struct AppParams
{
std::string package;
};

std::ostream& operator<<(std::ostream& os, const SwipeParams::Step& step);

using Params = std::variant<ClickParams, SwipeParams, AppParams>;
using Params = std::variant<ClickParams, SwipeParams, PressKeyParams, AppParams>;

struct Action
{
Expand All @@ -48,6 +52,7 @@ struct Action
connect,
click,
swipe,
press_key,
screencap,
start_app,
stop_app,
Expand Down Expand Up @@ -85,6 +90,7 @@ class ControllerMgr : public MaaControllerAPI
void click(const cv::Point& p);
void swipe(const cv::Rect& r1, const cv::Rect& r2, int duration);
void swipe(const cv::Point& p1, const cv::Point& p2, int duration);
void press_key(int keycode);
cv::Mat screencap();

void start_app();
Expand All @@ -97,6 +103,7 @@ class ControllerMgr : public MaaControllerAPI
virtual std::pair<int, int> _get_resolution() const = 0;
virtual void _click(ClickParams param) = 0;
virtual void _swipe(SwipeParams param) = 0;
virtual void _press_key(PressKeyParams param) = 0;
virtual cv::Mat _screencap() = 0;
virtual bool _start_app(AppParams param) = 0;
virtual bool _stop_app(AppParams param) = 0;
Expand Down
12 changes: 12 additions & 0 deletions source/Controller/CustomController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ void CustomController::_swipe(SwipeParams param)
handle_->swipe(x_buf.data(), y_buf.data(), delay_buf.data(), size);
}

void CustomController::_press_key(PressKeyParams param)
{
LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->press_key) << VAR(param.keycode);

if (!handle_ || !handle_->press_key) {
LogError << "handle_ or handle_->press_key is nullptr";
return;
}

handle_->press_key(param.keycode);
}

cv::Mat CustomController::_screencap()
{
LogFunc << VAR_VOIDP(handle_) << VAR_VOIDP(handle_->get_image);
Expand Down
1 change: 1 addition & 0 deletions source/Controller/CustomController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CustomController : public ControllerMgr
virtual std::pair<int, int> _get_resolution() const override;
virtual void _click(ClickParams param) override;
virtual void _swipe(SwipeParams param) override;
virtual void _press_key(PressKeyParams param) override;
virtual cv::Mat _screencap() override;
virtual bool _start_app(AppParams param) override;
virtual bool _stop_app(AppParams param) override;
Expand Down
15 changes: 15 additions & 0 deletions source/Controller/CustomThriftController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ void CustomThriftController::_swipe(SwipeParams param)
client_->swipe(swipe_param);
}

void CustomThriftController::_press_key(PressKeyParams param)
{
LogFunc;

if (!client_ || !transport_->isOpen()) {
LogError << "client_ is nullptr or transport_ is not open";
return;
}

ThriftController::PressKeyParams thrift_param;
thrift_param.keycode = param.keycode;

client_->press_key(thrift_param);
}

cv::Mat CustomThriftController::_screencap()
{
LogFunc;
Expand Down
1 change: 1 addition & 0 deletions source/Controller/CustomThriftController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CustomThriftController : public ControllerMgr
virtual std::pair<int, int> _get_resolution() const override;
virtual void _click(ClickParams param) override;
virtual void _swipe(SwipeParams param) override;
virtual void _press_key(PressKeyParams param) override;
virtual cv::Mat _screencap() override;
virtual bool _start_app(AppParams param) override;
virtual bool _stop_app(AppParams param) override;
Expand Down
24 changes: 15 additions & 9 deletions source/Resource/PipelineConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,10 @@ bool PipelineConfig::parse_action(const json::value& input, MAA_PIPELINE_RES_NS:
}

static const std::unordered_map<std::string, Type> kActTypeMap = {
{ "DoNothing", Type::DoNothing }, //
{ "Click", Type::Click }, //
{ "Swipe", Type::Swipe }, //
{ "Key", Type::Key }, //
{ "StopApp", Type::StopApp }, //
{ "CustomTask", Type::CustomTask }, //
{ "DoNothing", Type::DoNothing }, { "Click", Type::Click },
{ "Swipe", Type::Swipe }, { "Key", Type::Key },
{ "StartApp", Type::StartApp }, { "StopApp", Type::StopApp },
{ "CustomTask", Type::CustomTask },
};
auto act_type_iter = kActTypeMap.find(act_type_name);
if (act_type_iter == kActTypeMap.cend()) {
Expand Down Expand Up @@ -550,10 +548,18 @@ bool PipelineConfig::parse_swipe(const json::value& input, MAA_PIPELINE_RES_NS::

bool PipelineConfig::parse_key_press(const json::value& input, MAA_PIPELINE_RES_NS::Action::KeyParams& output)
{
if (!get_and_check_value_or_array(input, "key", output.keys)) {
LogError << "failed to get_and_check_value_or_array key" << VAR(input);
return false;
std::string str_keys;
if (!get_and_check_value(input, "key", str_keys, std::string())) {
if (!get_and_check_value_or_array(input, "key", output.keys)) {
LogError << "failed to get_and_check_value_or_array key" << VAR(input);
return false;
}
}
else {
ranges::transform(str_keys, std::back_inserter(output.keys), [](char c) { return static_cast<int>(c); });
LogTrace << "key press" << VAR(str_keys) << VAR(output.keys);
}

return true;
}

Expand Down
12 changes: 12 additions & 0 deletions source/Task/PipelineTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ void PipelineTask::start_to_act(const FoundResult& act)
case Type::Swipe:
swipe(std::get<SwipeParams>(act.task_data.action_params), act.rec.box);
break;
case Type::Key:
break;
case Type::StartApp:
start_app(std::get<AppInfo>(act.task_data.action_params));
Expand Down Expand Up @@ -306,6 +307,17 @@ void PipelineTask::swipe(const MAA_PIPELINE_RES_NS::Action::SwipeParams& param,
controller()->swipe(begin, end, param.duration);
}

void PipelineTask::press_key(const MAA_PIPELINE_RES_NS::Action::KeyParams& param)
{
if (!controller()) {
LogError << "Controller is null";
return;
}
for (const auto& key : param.keys) {
controller()->press_key(key);
}
}

void PipelineTask::wait_freezes(const MAA_PIPELINE_RES_NS::WaitFreezesParams& param, const cv::Rect& cur_box)
{
if (param.time <= std::chrono::milliseconds(0)) {
Expand Down
2 changes: 2 additions & 0 deletions source/Task/PipelineTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class PipelineTask : public TaskBase
private:
void click(const MAA_PIPELINE_RES_NS::Action::ClickParams& param, const cv::Rect& cur_box);
void swipe(const MAA_PIPELINE_RES_NS::Action::SwipeParams& param, const cv::Rect& cur_box);
void press_key(const MAA_PIPELINE_RES_NS::Action::KeyParams& param);

void start_app(const MAA_PIPELINE_RES_NS::Action::AppInfo& param);
void stop_app(const MAA_PIPELINE_RES_NS::Action::AppInfo& param);
void run_custom_task(const MAA_PIPELINE_RES_NS::Action::CustomTaskParams& param);
Expand Down

0 comments on commit f6e3c04

Please sign in to comment.