Skip to content

Commit 7fae994

Browse files
committed
feat(controller): 添加获取真实分辨率接口 MaaControllerGetResolution (#1040)
fix #1024
1 parent 87dfbdf commit 7fae994

16 files changed

Lines changed: 225 additions & 1 deletion

File tree

docs/en_us/2.2-IntegratedInterfaceOverview.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ Asynchronously scroll. This is an asynchronous operation that immediately return
416416

417417
Asynchronously screenshot. This is an asynchronous operation that immediately returns an operation id. You can query the status via `MaaControllerStatus` and `MaaControllerWait`, and get the screenshot via `MaaControllerCachedImage`.
418418

419+
> [!NOTE]
420+
>
421+
> The screenshot is scaled according to the screenshot target size settings (long side / short side). The resulting image dimensions may differ from the raw device resolution.
422+
> Use `MaaControllerGetResolution` to get the raw (unscaled) device resolution.
423+
419424
### MaaControllerPostShell
420425

421426
- `cmd`: Shell command to execute
@@ -446,6 +451,24 @@ Check if connected.
446451

447452
Get the latest screenshot, write to `buffer`.
448453

454+
> [!NOTE]
455+
>
456+
> The returned image is scaled according to the screenshot target size settings (long side / short side). The image dimensions may differ from the raw device resolution.
457+
> Use `MaaControllerGetResolution` to get the raw (unscaled) device resolution.
458+
459+
### MaaControllerGetResolution
460+
461+
- `width [out]`: Output raw width
462+
- `height [out]`: Output raw height
463+
464+
Get the raw (unscaled) device resolution.
465+
466+
> [!NOTE]
467+
>
468+
> This returns the actual device screen resolution before any scaling.
469+
> The screenshot obtained via `MaaControllerCachedImage` is scaled according to the screenshot target size settings, so its dimensions may differ from this raw resolution.
470+
> Valid values are only available after the first screenshot is taken.
471+
449472
### MaaControllerGetShellOutput
450473

451474
- `buffer [out]`: Output buffer

docs/zh_cn/2.2-集成接口一览.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@
416416

417417
异步截图。这是一个异步操作,会立即返回一个操作 id,可通过 `MaaControllerStatus``MaaControllerWait` 查询状态,通过 `MaaControllerCachedImage` 获取截图。
418418

419+
> [!NOTE]
420+
>
421+
> 截图会根据截图目标尺寸设置(长边/短边)进行缩放,获取到的图像尺寸可能与设备原始分辨率不同。
422+
> 使用 `MaaControllerGetResolution` 可获取设备的原始(未缩放)分辨率。
423+
419424
### MaaControllerPostShell
420425

421426
- `cmd`: 要执行的 shell 命令
@@ -446,6 +451,24 @@
446451

447452
获取最新一次截图,写入到 `buffer`
448453

454+
> [!NOTE]
455+
>
456+
> 返回的图像是经过缩放的,尺寸根据截图目标尺寸设置(长边/短边)而定,可能与设备原始分辨率不同。
457+
> 使用 `MaaControllerGetResolution` 可获取设备的原始(未缩放)分辨率。
458+
459+
### MaaControllerGetResolution
460+
461+
- `width [out]`: 输出原始宽度
462+
- `height [out]`: 输出原始高度
463+
464+
获取设备的原始(未缩放)分辨率。
465+
466+
> [!NOTE]
467+
>
468+
> 返回的是设备屏幕的实际分辨率,未经任何缩放处理。
469+
> 而通过 `MaaControllerCachedImage` 获取的截图是经过缩放的,其尺寸可能与此原始分辨率不同。
470+
> 需要在首次截图后才能获取到有效值。
471+
449472
### MaaControllerGetShellOutput
450473

451474
- `buffer [out]`: 输出缓冲区

include/MaaFramework/Instance/MaaController.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ extern "C"
9999

100100
MAA_FRAMEWORK_API MaaCtrlId MaaControllerPostKeyUp(MaaController* ctrl, int32_t keycode);
101101

102+
/**
103+
* @brief Post a screenshot request to the controller.
104+
*
105+
* @param ctrl The controller handle.
106+
* @return The control id of the screenshot action.
107+
*
108+
* @note The screenshot image is scaled according to the screenshot target size settings (long side / short side).
109+
* Use MaaControllerGetResolution to get the raw (unscaled) device resolution.
110+
* @see MaaControllerCachedImage, MaaControllerSetOption, MaaControllerGetResolution
111+
*/
102112
MAA_FRAMEWORK_API MaaCtrlId MaaControllerPostScreencap(MaaController* ctrl);
103113

104114
/**
@@ -145,10 +155,37 @@ extern "C"
145155

146156
MAA_FRAMEWORK_API MaaBool MaaControllerConnected(const MaaController* ctrl);
147157

158+
/**
159+
* @brief Get the cached screenshot image.
160+
*
161+
* @param ctrl The controller handle.
162+
* @param buffer The output buffer to store the screenshot image.
163+
* @return true if the screenshot is available, false otherwise.
164+
*
165+
* @note The returned image is scaled according to the screenshot target size settings (long side / short side).
166+
* The image dimensions may differ from the raw device resolution.
167+
* Use MaaControllerGetResolution to get the raw (unscaled) device resolution.
168+
* @see MaaControllerPostScreencap, MaaControllerSetOption, MaaControllerGetResolution
169+
*/
148170
MAA_FRAMEWORK_API MaaBool MaaControllerCachedImage(const MaaController* ctrl, /* out */ MaaImageBuffer* buffer);
149171

150172
MAA_FRAMEWORK_API MaaBool MaaControllerGetUuid(MaaController* ctrl, /* out */ MaaStringBuffer* buffer);
151173

174+
/**
175+
* @brief Get the raw (unscaled) device resolution.
176+
*
177+
* @param ctrl The controller handle.
178+
* @param[out] width Output parameter for the raw width.
179+
* @param[out] height Output parameter for the raw height.
180+
* @return true if the resolution is available, false otherwise (e.g., not connected or no screenshot taken yet).
181+
*
182+
* @note This returns the actual device screen resolution before any scaling.
183+
* The screenshot obtained via MaaControllerCachedImage is scaled according to the screenshot target size settings,
184+
* so its dimensions may differ from this raw resolution.
185+
* @see MaaControllerCachedImage, MaaControllerPostScreencap
186+
*/
187+
MAA_FRAMEWORK_API MaaBool MaaControllerGetResolution(const MaaController* ctrl, /* out */ int32_t* width, /* out */ int32_t* height);
188+
152189
MAA_DEPRECATED MAA_FRAMEWORK_API MaaCtrlId MaaControllerPostPressKey(MaaController* ctrl, int32_t keycode);
153190

154191
#ifdef __cplusplus

source/Common/MaaController.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,13 @@ MaaBool MaaControllerGetUuid(MaaController* ctrl, MaaStringBuffer* buffer)
344344
buffer->set(std::move(uuid));
345345
return true;
346346
}
347+
348+
MaaBool MaaControllerGetResolution(const MaaController* ctrl, int32_t* width, int32_t* height)
349+
{
350+
if (!ctrl || !width || !height) {
351+
LogError << "handle or output params are null";
352+
return false;
353+
}
354+
355+
return ctrl->get_resolution(*width, *height);
356+
}

source/MaaAgentClient/Client/AgentClient.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ bool AgentClient::handle_inserted_request(const json::value& j)
426426
else if (handle_controller_get_uuid(j)) {
427427
return true;
428428
}
429+
else if (handle_controller_get_resolution(j)) {
430+
return true;
431+
}
429432
else if (handle_event_response(j)) {
430433
return true;
431434
}
@@ -2082,6 +2085,30 @@ bool AgentClient::handle_controller_get_uuid(const json::value& j)
20822085
return true;
20832086
}
20842087

2088+
bool AgentClient::handle_controller_get_resolution(const json::value& j)
2089+
{
2090+
if (!j.is<ControllerGetResolutionReverseRequest>()) {
2091+
return false;
2092+
}
2093+
const ControllerGetResolutionReverseRequest& req = j.as<ControllerGetResolutionReverseRequest>();
2094+
LogFunc << VAR(req) << VAR(ipc_addr_);
2095+
MaaController* controller = query_controller(req.controller_id);
2096+
if (!controller) {
2097+
LogError << "controller not found" << VAR(req.controller_id);
2098+
return false;
2099+
}
2100+
int32_t width = 0;
2101+
int32_t height = 0;
2102+
bool success = controller->get_resolution(width, height);
2103+
ControllerGetResolutionReverseResponse resp {
2104+
.success = success,
2105+
.width = width,
2106+
.height = height,
2107+
};
2108+
send(resp);
2109+
return true;
2110+
}
2111+
20852112
// FIXME: 不应该存在这个函数
20862113
// 等 send_and_recv 支持在子线程中使用后,应该删除这个函数
20872114
bool AgentClient::handle_event_response(const json::value& j)

source/MaaAgentClient/Client/AgentClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class AgentClient
111111
bool handle_controller_cached_image(const json::value& j);
112112
bool handle_controller_get_shell_output(const json::value& j);
113113
bool handle_controller_get_uuid(const json::value& j);
114+
bool handle_controller_get_resolution(const json::value& j);
114115

115116
bool handle_event_response(const json::value& j);
116117

source/MaaAgentServer/RemoteInstance/RemoteController.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,21 @@ std::string RemoteController::get_uuid()
311311
return resp_opt->uuid;
312312
}
313313

314+
bool RemoteController::get_resolution(int32_t& width, int32_t& height) const
315+
{
316+
ControllerGetResolutionReverseRequest req {
317+
.controller_id = controller_id_,
318+
};
319+
320+
auto resp_opt = server_.send_and_recv<ControllerGetResolutionReverseResponse>(req);
321+
if (!resp_opt) {
322+
return false;
323+
}
324+
width = resp_opt->width;
325+
height = resp_opt->height;
326+
return resp_opt->success;
327+
}
328+
314329
MaaSinkId RemoteController::add_sink(MaaEventCallback callback, void* trans_arg)
315330
{
316331
LogError << "Can NOT add sink for remote instance, use AgentServer.add_controller_sink instead" << VAR_VOIDP(callback)

source/MaaAgentServer/RemoteInstance/RemoteController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class RemoteController : public MaaController
4444
virtual std::string cached_shell_output() const override;
4545
virtual std::string get_uuid() override;
4646

47+
virtual bool get_resolution(int32_t& width, int32_t& height) const override;
48+
4749
virtual MaaSinkId add_sink(MaaEventCallback callback, void* trans_arg) override;
4850
virtual void remove_sink(MaaSinkId sink_id) override;
4951
virtual void clear_sinks() override;

source/MaaFramework/Controller/ControllerAgent.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ std::string ControllerAgent::get_uuid()
206206
return uuid_cache_;
207207
}
208208

209+
bool ControllerAgent::get_resolution(int32_t& width, int32_t& height) const
210+
{
211+
if (image_raw_width_ == 0 || image_raw_height_ == 0) {
212+
return false;
213+
}
214+
215+
width = image_raw_width_;
216+
height = image_raw_height_;
217+
return true;
218+
}
219+
209220
MaaSinkId ControllerAgent::add_sink(MaaEventCallback callback, void* trans_arg)
210221
{
211222
return notifier_.add_sink(callback, trans_arg);

source/MaaFramework/Controller/ControllerAgent.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class ControllerAgent : public MaaController
191191
virtual std::string cached_shell_output() const override;
192192
virtual std::string get_uuid() override;
193193

194+
virtual bool get_resolution(int32_t& width, int32_t& height) const override;
195+
194196
virtual MaaSinkId add_sink(MaaEventCallback callback, void* trans_arg) override;
195197
virtual void remove_sink(MaaSinkId sink_id) override;
196198
virtual void clear_sinks() override;

0 commit comments

Comments
 (0)