kotonebot 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kotonebot/__init__.py +40 -0
- kotonebot/backend/__init__.py +0 -0
- kotonebot/backend/bot.py +302 -0
- kotonebot/backend/color.py +525 -0
- kotonebot/backend/context/__init__.py +3 -0
- kotonebot/backend/context/context.py +1001 -0
- kotonebot/backend/context/task_action.py +176 -0
- kotonebot/backend/core.py +126 -0
- kotonebot/backend/debug/__init__.py +1 -0
- kotonebot/backend/debug/entry.py +89 -0
- kotonebot/backend/debug/mock.py +79 -0
- kotonebot/backend/debug/server.py +223 -0
- kotonebot/backend/debug/vars.py +346 -0
- kotonebot/backend/dispatch.py +228 -0
- kotonebot/backend/flow_controller.py +197 -0
- kotonebot/backend/image.py +748 -0
- kotonebot/backend/loop.py +277 -0
- kotonebot/backend/ocr.py +511 -0
- kotonebot/backend/preprocessor.py +103 -0
- kotonebot/client/__init__.py +10 -0
- kotonebot/client/device.py +500 -0
- kotonebot/client/fast_screenshot.py +378 -0
- kotonebot/client/host/__init__.py +12 -0
- kotonebot/client/host/adb_common.py +94 -0
- kotonebot/client/host/custom.py +114 -0
- kotonebot/client/host/leidian_host.py +202 -0
- kotonebot/client/host/mumu12_host.py +245 -0
- kotonebot/client/host/protocol.py +213 -0
- kotonebot/client/host/windows_common.py +55 -0
- kotonebot/client/implements/__init__.py +7 -0
- kotonebot/client/implements/adb.py +85 -0
- kotonebot/client/implements/adb_raw.py +159 -0
- kotonebot/client/implements/nemu_ipc/__init__.py +8 -0
- kotonebot/client/implements/nemu_ipc/external_renderer_ipc.py +280 -0
- kotonebot/client/implements/nemu_ipc/nemu_ipc.py +327 -0
- kotonebot/client/implements/remote_windows.py +193 -0
- kotonebot/client/implements/uiautomator2.py +82 -0
- kotonebot/client/implements/windows.py +168 -0
- kotonebot/client/protocol.py +69 -0
- kotonebot/client/registration.py +24 -0
- kotonebot/config/__init__.py +1 -0
- kotonebot/config/base_config.py +96 -0
- kotonebot/config/manager.py +36 -0
- kotonebot/errors.py +72 -0
- kotonebot/interop/win/__init__.py +0 -0
- kotonebot/interop/win/message_box.py +314 -0
- kotonebot/interop/win/reg.py +37 -0
- kotonebot/interop/win/shortcut.py +43 -0
- kotonebot/interop/win/task_dialog.py +469 -0
- kotonebot/logging/__init__.py +2 -0
- kotonebot/logging/log.py +18 -0
- kotonebot/primitives/__init__.py +17 -0
- kotonebot/primitives/geometry.py +290 -0
- kotonebot/primitives/visual.py +63 -0
- kotonebot/tools/__init__.py +0 -0
- kotonebot/tools/mirror.py +354 -0
- kotonebot/ui/__init__.py +0 -0
- kotonebot/ui/file_host/sensio.py +36 -0
- kotonebot/ui/file_host/tmp_send.py +54 -0
- kotonebot/ui/pushkit/__init__.py +3 -0
- kotonebot/ui/pushkit/image_host.py +87 -0
- kotonebot/ui/pushkit/protocol.py +13 -0
- kotonebot/ui/pushkit/wxpusher.py +53 -0
- kotonebot/ui/user.py +144 -0
- kotonebot/util.py +409 -0
- kotonebot-0.1.0.dist-info/METADATA +204 -0
- kotonebot-0.1.0.dist-info/RECORD +70 -0
- kotonebot-0.1.0.dist-info/WHEEL +5 -0
- kotonebot-0.1.0.dist-info/licenses/LICENSE +674 -0
- kotonebot-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
from typing import Literal
|
|
3
|
+
from typing_extensions import assert_never
|
|
4
|
+
|
|
5
|
+
from kotonebot import logging
|
|
6
|
+
from kotonebot.client.device import WindowsDevice
|
|
7
|
+
from .protocol import Device, WindowsHostConfig, RemoteWindowsHostConfig
|
|
8
|
+
|
|
9
|
+
logger = logging.getLogger(__name__)
|
|
10
|
+
WindowsRecipes = Literal['windows', 'remote_windows']
|
|
11
|
+
|
|
12
|
+
# Windows 相关的配置类型联合
|
|
13
|
+
WindowsHostConfigs = WindowsHostConfig | RemoteWindowsHostConfig
|
|
14
|
+
|
|
15
|
+
class CommonWindowsCreateDeviceMixin(ABC):
|
|
16
|
+
"""
|
|
17
|
+
通用 Windows 创建设备的 Mixin。
|
|
18
|
+
该 Mixin 定义了创建 Windows 设备的通用接口。
|
|
19
|
+
"""
|
|
20
|
+
def __init__(self, *args, **kwargs) -> None:
|
|
21
|
+
super().__init__(*args, **kwargs)
|
|
22
|
+
|
|
23
|
+
def create_device(self, recipe: WindowsRecipes, config: WindowsHostConfigs) -> Device:
|
|
24
|
+
"""
|
|
25
|
+
创建 Windows 设备。
|
|
26
|
+
"""
|
|
27
|
+
match recipe:
|
|
28
|
+
case 'windows':
|
|
29
|
+
if not isinstance(config, WindowsHostConfig):
|
|
30
|
+
raise ValueError(f"Expected WindowsHostConfig for 'windows' recipe, got {type(config)}")
|
|
31
|
+
from kotonebot.client.implements.windows import WindowsImpl
|
|
32
|
+
d = WindowsDevice()
|
|
33
|
+
impl = WindowsImpl(
|
|
34
|
+
device=d,
|
|
35
|
+
window_title=config.window_title,
|
|
36
|
+
ahk_exe_path=config.ahk_exe_path
|
|
37
|
+
)
|
|
38
|
+
d._screenshot = impl
|
|
39
|
+
d._touch = impl
|
|
40
|
+
return d
|
|
41
|
+
case 'remote_windows':
|
|
42
|
+
if not isinstance(config, RemoteWindowsHostConfig):
|
|
43
|
+
raise ValueError(f"Expected RemoteWindowsHostConfig for 'remote_windows' recipe, got {type(config)}")
|
|
44
|
+
from kotonebot.client.implements.remote_windows import RemoteWindowsImpl
|
|
45
|
+
d = WindowsDevice()
|
|
46
|
+
impl = RemoteWindowsImpl(
|
|
47
|
+
device=d,
|
|
48
|
+
host=config.host,
|
|
49
|
+
port=config.port
|
|
50
|
+
)
|
|
51
|
+
d._screenshot = impl
|
|
52
|
+
d._touch = impl
|
|
53
|
+
return d
|
|
54
|
+
case _:
|
|
55
|
+
assert_never(f'Unsupported Windows recipe: {recipe}')
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
from typing import cast
|
|
3
|
+
from typing_extensions import override
|
|
4
|
+
|
|
5
|
+
import cv2
|
|
6
|
+
import numpy as np
|
|
7
|
+
from cv2.typing import MatLike
|
|
8
|
+
from adbutils._device import AdbDevice as AdbUtilsDevice
|
|
9
|
+
|
|
10
|
+
from ..device import AndroidDevice
|
|
11
|
+
from ..protocol import AndroidCommandable, Touchable, Screenshotable
|
|
12
|
+
from ..registration import ImplConfig
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
# 定义配置模型
|
|
18
|
+
@dataclass
|
|
19
|
+
class AdbImplConfig(ImplConfig):
|
|
20
|
+
addr: str
|
|
21
|
+
connect: bool = True
|
|
22
|
+
disconnect: bool = True
|
|
23
|
+
device_serial: str | None = None
|
|
24
|
+
timeout: float = 180
|
|
25
|
+
|
|
26
|
+
class AdbImpl(AndroidCommandable, Touchable, Screenshotable):
|
|
27
|
+
def __init__(self, adb_connection: AdbUtilsDevice):
|
|
28
|
+
self.adb = adb_connection
|
|
29
|
+
|
|
30
|
+
@override
|
|
31
|
+
def launch_app(self, package_name: str) -> None:
|
|
32
|
+
self.adb.shell(f"monkey -p {package_name} 1")
|
|
33
|
+
|
|
34
|
+
@override
|
|
35
|
+
def current_package(self) -> str | None:
|
|
36
|
+
# https://blog.csdn.net/guangdeshishe/article/details/117154406
|
|
37
|
+
result_text = self.adb.shell('dumpsys activity top | grep ACTIVITY | tail -n 1')
|
|
38
|
+
logger.debug(f"adb returned: {result_text}")
|
|
39
|
+
if not isinstance(result_text, str):
|
|
40
|
+
logger.error(f"Invalid result_text: {result_text}")
|
|
41
|
+
return None
|
|
42
|
+
result_text = result_text.strip()
|
|
43
|
+
if result_text == '':
|
|
44
|
+
logger.error("No current package found")
|
|
45
|
+
return None
|
|
46
|
+
_, activity, *_ = result_text.split(' ')
|
|
47
|
+
package = activity.split('/')[0]
|
|
48
|
+
return package
|
|
49
|
+
|
|
50
|
+
def adb_shell(self, cmd: str) -> str:
|
|
51
|
+
"""执行 ADB shell 命令"""
|
|
52
|
+
return cast(str, self.adb.shell(cmd))
|
|
53
|
+
|
|
54
|
+
@override
|
|
55
|
+
def detect_orientation(self):
|
|
56
|
+
# 判断方向:https://stackoverflow.com/questions/10040624/check-if-device-is-landscape-via-adb
|
|
57
|
+
# 但是上面这种方法不准确
|
|
58
|
+
# 因此这里直接通过截图判断方向
|
|
59
|
+
img = self.screenshot()
|
|
60
|
+
if img.shape[0] > img.shape[1]:
|
|
61
|
+
return 'portrait'
|
|
62
|
+
return 'landscape'
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def screen_size(self) -> tuple[int, int]:
|
|
66
|
+
ret = cast(str, self.adb.shell("wm size")).strip('Physical size: ')
|
|
67
|
+
spiltted = tuple(map(int, ret.split("x")))
|
|
68
|
+
# 检测当前方向
|
|
69
|
+
orientation = self.detect_orientation()
|
|
70
|
+
landscape = orientation == 'landscape'
|
|
71
|
+
spiltted = tuple(sorted(spiltted, reverse=landscape))
|
|
72
|
+
if len(spiltted) != 2:
|
|
73
|
+
raise ValueError(f"Invalid screen size: {ret}")
|
|
74
|
+
return spiltted
|
|
75
|
+
|
|
76
|
+
def screenshot(self) -> MatLike:
|
|
77
|
+
return cv2.cvtColor(np.array(self.adb.screenshot()), cv2.COLOR_RGB2BGR)
|
|
78
|
+
|
|
79
|
+
def click(self, x: int, y: int) -> None:
|
|
80
|
+
self.adb.shell(f"input tap {x} {y}")
|
|
81
|
+
|
|
82
|
+
def swipe(self, x1: int, y1: int, x2: int, y2: int, duration: float | None = None) -> None:
|
|
83
|
+
if duration is not None:
|
|
84
|
+
logger.warning("Swipe duration is not supported with AdbDevice. Ignoring duration.")
|
|
85
|
+
self.adb.shell(f"input touchscreen swipe {x1} {y1} {x2} {y2}")
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import time
|
|
3
|
+
import subprocess
|
|
4
|
+
import struct
|
|
5
|
+
from threading import Thread, Lock
|
|
6
|
+
from functools import cached_property
|
|
7
|
+
from typing_extensions import override
|
|
8
|
+
|
|
9
|
+
import cv2
|
|
10
|
+
import numpy as np
|
|
11
|
+
from cv2.typing import MatLike
|
|
12
|
+
from adbutils._utils import adb_path
|
|
13
|
+
|
|
14
|
+
from .adb import AdbImpl
|
|
15
|
+
from adbutils._device import AdbDevice as AdbUtilsDevice
|
|
16
|
+
from kotonebot import logging
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
WAIT_TIMEOUT = 10
|
|
21
|
+
MAX_RETRY_COUNT = 5
|
|
22
|
+
SCRIPT: str = """#!/bin/sh
|
|
23
|
+
while true; do
|
|
24
|
+
screencap
|
|
25
|
+
sleep 0.3
|
|
26
|
+
done
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
class AdbRawImpl(AdbImpl):
|
|
30
|
+
def __init__(self, adb_connection: AdbUtilsDevice):
|
|
31
|
+
super().__init__(adb_connection)
|
|
32
|
+
self.__worker: Thread | None = None
|
|
33
|
+
self.__process: subprocess.Popen | None = None
|
|
34
|
+
self.__data: MatLike | None = None
|
|
35
|
+
self.__retry_count = 0
|
|
36
|
+
self.__lock = Lock()
|
|
37
|
+
self.__stopping = False
|
|
38
|
+
|
|
39
|
+
def __cleanup_worker(self) -> None:
|
|
40
|
+
if self.__process:
|
|
41
|
+
try:
|
|
42
|
+
self.__process.kill()
|
|
43
|
+
except:
|
|
44
|
+
pass
|
|
45
|
+
self.__process = None
|
|
46
|
+
if self.__worker:
|
|
47
|
+
try:
|
|
48
|
+
self.__worker.join()
|
|
49
|
+
except:
|
|
50
|
+
pass
|
|
51
|
+
self.__worker = None
|
|
52
|
+
self.__data = None
|
|
53
|
+
|
|
54
|
+
def __start_worker(self) -> None:
|
|
55
|
+
self.__stopping = True
|
|
56
|
+
self.__cleanup_worker()
|
|
57
|
+
self.__stopping = False
|
|
58
|
+
self.__worker = Thread(target=self.__worker_thread_with_retry, daemon=True)
|
|
59
|
+
self.__worker.start()
|
|
60
|
+
|
|
61
|
+
def __worker_thread_with_retry(self) -> None:
|
|
62
|
+
try:
|
|
63
|
+
self.__worker_thread()
|
|
64
|
+
except Exception as e:
|
|
65
|
+
logger.error(f"Worker thread failed: {e}")
|
|
66
|
+
with self.__lock:
|
|
67
|
+
self.__retry_count += 1
|
|
68
|
+
raise
|
|
69
|
+
|
|
70
|
+
def __worker_thread(self) -> None:
|
|
71
|
+
with open('screenshot.sh', 'w', encoding='utf-8', newline='\n') as f:
|
|
72
|
+
f.write(SCRIPT)
|
|
73
|
+
self.adb.push('screenshot.sh', '/data/local/tmp/screenshot.sh')
|
|
74
|
+
self.adb.shell(f'chmod 755 /data/local/tmp/screenshot.sh')
|
|
75
|
+
os.remove('screenshot.sh')
|
|
76
|
+
|
|
77
|
+
cmd = fr'{adb_path()} -s {self.adb.serial} exec-out "sh /data/local/tmp/screenshot.sh"'
|
|
78
|
+
self.__process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
|
|
79
|
+
|
|
80
|
+
while not self.__stopping and self.__process.poll() is None:
|
|
81
|
+
if self.__process.stdout is None:
|
|
82
|
+
logger.error("Failed to get stdout from process")
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
# 解析 header
|
|
86
|
+
# https://stackoverflow.com/questions/22034959/what-format-does-adb-screencap-sdcard-screenshot-raw-produce-without-p-f
|
|
87
|
+
if self.__api_level >= 26:
|
|
88
|
+
metadata = self.__process.stdout.read(16)
|
|
89
|
+
w, h, p, c = struct.unpack('<IIII', metadata)
|
|
90
|
+
# w=width, h=height, p=pixel_format, c=color_space
|
|
91
|
+
# 详见:https://android.googlesource.com/platform/frameworks/base/+/26a2b97dbe48ee45e9ae70110714048f2f360f97/cmds/screencap/screencap.cpp#209
|
|
92
|
+
else:
|
|
93
|
+
metadata = self.__process.stdout.read(12)
|
|
94
|
+
w, h, p = struct.unpack('<III', metadata)
|
|
95
|
+
if p == 1: # PixelFormat.RGBA_8888
|
|
96
|
+
channel = 4
|
|
97
|
+
else:
|
|
98
|
+
raise ValueError(f"Unsupported pixel format: {p}")
|
|
99
|
+
data_size = w * h * channel
|
|
100
|
+
|
|
101
|
+
if (data_size < 100 * 100 * 4) or (data_size > 3000 * 3000 * 4):
|
|
102
|
+
raise ValueError(f"Invaild data_size: {w}x{h}.")
|
|
103
|
+
|
|
104
|
+
# 读取图像数据
|
|
105
|
+
# logger.verbose(f"receiving image data: {w}x{h} {data_size} bytes")
|
|
106
|
+
image_data = self.__process.stdout.read(data_size)
|
|
107
|
+
if not isinstance(image_data, bytes) or len(image_data) != data_size:
|
|
108
|
+
logger.error(f"Failed to read image data, expected {data_size} bytes but got {len(image_data) if isinstance(image_data, bytes) else 'non-bytes'}")
|
|
109
|
+
raise RuntimeError("Failed to read image data")
|
|
110
|
+
|
|
111
|
+
np_data = np.frombuffer(image_data, np.uint8)
|
|
112
|
+
np_data = np_data.reshape(h, w, channel)
|
|
113
|
+
self.__data = cv2.cvtColor(np_data, cv2.COLOR_RGBA2BGR)
|
|
114
|
+
|
|
115
|
+
@cached_property
|
|
116
|
+
def __api_level(self) -> int:
|
|
117
|
+
try:
|
|
118
|
+
output = self.adb.shell("getprop ro.build.version.sdk")
|
|
119
|
+
assert isinstance(output, str)
|
|
120
|
+
return int(output.strip())
|
|
121
|
+
except Exception as e:
|
|
122
|
+
logger.error(f"Failed to get API level: {e}")
|
|
123
|
+
return 0
|
|
124
|
+
|
|
125
|
+
@override
|
|
126
|
+
def screenshot(self) -> MatLike:
|
|
127
|
+
with self.__lock:
|
|
128
|
+
if self.__retry_count >= MAX_RETRY_COUNT:
|
|
129
|
+
raise RuntimeError(f"Maximum retry count ({MAX_RETRY_COUNT}) exceeded")
|
|
130
|
+
|
|
131
|
+
if not self.__worker or (self.__worker and not self.__worker.is_alive()):
|
|
132
|
+
self.__start_worker()
|
|
133
|
+
|
|
134
|
+
start_time = time.time()
|
|
135
|
+
while self.__data is None:
|
|
136
|
+
time.sleep(0.01)
|
|
137
|
+
if time.time() - start_time > WAIT_TIMEOUT:
|
|
138
|
+
logger.warning("Screenshot timeout, cleaning up and restarting worker...")
|
|
139
|
+
with self.__lock:
|
|
140
|
+
if self.__retry_count < MAX_RETRY_COUNT:
|
|
141
|
+
self.__start_worker()
|
|
142
|
+
start_time = time.time() # 重置超时计时器
|
|
143
|
+
continue
|
|
144
|
+
else:
|
|
145
|
+
raise RuntimeError(f"Maximum retry count ({MAX_RETRY_COUNT}) exceeded")
|
|
146
|
+
|
|
147
|
+
# 检查 worker 是否还活着
|
|
148
|
+
if self.__worker and not self.__worker.is_alive():
|
|
149
|
+
with self.__lock:
|
|
150
|
+
if self.__retry_count < MAX_RETRY_COUNT:
|
|
151
|
+
logger.warning("Worker thread died, restarting...")
|
|
152
|
+
self.__start_worker()
|
|
153
|
+
else:
|
|
154
|
+
raise RuntimeError(f"Maximum retry count ({MAX_RETRY_COUNT}) exceeded")
|
|
155
|
+
|
|
156
|
+
logger.verbose(f"adb raw screenshot wait time: {time.time() - start_time:.4f}s")
|
|
157
|
+
data = self.__data
|
|
158
|
+
self.__data = None
|
|
159
|
+
return data
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
logger = logging.getLogger(__name__)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NemuIpcIncompatible(RuntimeError):
|
|
9
|
+
"""MuMu12 IPC 环境不兼容或 DLL 加载失败"""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ExternalRendererIpc:
|
|
13
|
+
r"""对 `external_renderer_ipc.dll` 的轻量封装。
|
|
14
|
+
|
|
15
|
+
该类仅处理 DLL 加载与原型声明,并提供带有类型提示的薄包装方法,
|
|
16
|
+
方便在其他模块中调用且保持类型安全。
|
|
17
|
+
传入参数为 MuMu 根目录(如 F:\Apps\Netease\MuMuPlayer-12.0)。
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, mumu_root_folder: str):
|
|
21
|
+
if os.name != "nt":
|
|
22
|
+
raise NemuIpcIncompatible("ExternalRendererIpc only supports Windows.")
|
|
23
|
+
|
|
24
|
+
self.lib = self.__load_dll(mumu_root_folder)
|
|
25
|
+
self.raise_on_error: bool = True
|
|
26
|
+
"""是否在调用 DLL 函数失败时抛出异常。"""
|
|
27
|
+
self.__declare_prototypes()
|
|
28
|
+
|
|
29
|
+
def connect(self, nemu_folder: str, instance_id: int) -> int:
|
|
30
|
+
"""
|
|
31
|
+
建立连接。
|
|
32
|
+
|
|
33
|
+
API 原型:
|
|
34
|
+
`int nemu_connect(const wchar_t* path, int index)`
|
|
35
|
+
|
|
36
|
+
:param nemu_folder: 模拟器安装路径。
|
|
37
|
+
:param instance_id: 模拟器实例 ID。
|
|
38
|
+
:return: 成功返回连接 ID,失败返回 0。
|
|
39
|
+
"""
|
|
40
|
+
return self.lib.nemu_connect(nemu_folder, instance_id)
|
|
41
|
+
|
|
42
|
+
def disconnect(self, connect_id: int) -> None:
|
|
43
|
+
"""
|
|
44
|
+
断开连接。
|
|
45
|
+
|
|
46
|
+
API 原型:
|
|
47
|
+
`void nemu_disconnect(int handle)`
|
|
48
|
+
|
|
49
|
+
:param connect_id: 连接 ID。
|
|
50
|
+
:return: 无返回值。
|
|
51
|
+
"""
|
|
52
|
+
return self.lib.nemu_disconnect(connect_id)
|
|
53
|
+
|
|
54
|
+
def get_display_id(self, connect_id: int, pkg: str, app_index: int) -> int:
|
|
55
|
+
"""
|
|
56
|
+
获取指定包的 display id。
|
|
57
|
+
|
|
58
|
+
API 原型:
|
|
59
|
+
`int nemu_get_display_id(int handle, const char* pkg, int appIndex)`
|
|
60
|
+
|
|
61
|
+
:param connect_id: 连接 ID。
|
|
62
|
+
:param pkg: 包名。
|
|
63
|
+
:param app_index: 多开应用索引。
|
|
64
|
+
:return: <0 表示失败,>=0 表示有效 display id。
|
|
65
|
+
"""
|
|
66
|
+
return self.lib.nemu_get_display_id(connect_id, pkg.encode('utf-8'), app_index)
|
|
67
|
+
|
|
68
|
+
def capture_display(
|
|
69
|
+
self,
|
|
70
|
+
connect_id: int,
|
|
71
|
+
display_id: int,
|
|
72
|
+
buf_len: int,
|
|
73
|
+
width_ptr: ctypes.c_void_p,
|
|
74
|
+
height_ptr: ctypes.c_void_p,
|
|
75
|
+
buffer_ptr: ctypes.c_void_p,
|
|
76
|
+
) -> int:
|
|
77
|
+
"""
|
|
78
|
+
截取指定显示屏内容。
|
|
79
|
+
|
|
80
|
+
API 原型:
|
|
81
|
+
`int nemu_capture_display(int handle, unsigned int displayid, int buffer_size, int *width, int *height, unsigned char* pixels)`
|
|
82
|
+
|
|
83
|
+
:param connect_id: 连接 ID。
|
|
84
|
+
:param display_id: 显示屏 ID。
|
|
85
|
+
:param buf_len: 缓冲区长度(字节)。
|
|
86
|
+
:param width_ptr: 用于接收宽度的指针(ctypes.c_void_p/int 指针)。
|
|
87
|
+
:param height_ptr: 用于接收高度的指针(ctypes.c_void_p/int 指针)。
|
|
88
|
+
:param buffer_ptr: 用于接收像素数据的指针(ctypes.c_void_p/unsigned char* 指针)。
|
|
89
|
+
:return: 0 表示成功,>0 表示失败。
|
|
90
|
+
"""
|
|
91
|
+
return self.lib.nemu_capture_display(
|
|
92
|
+
connect_id,
|
|
93
|
+
display_id,
|
|
94
|
+
buf_len,
|
|
95
|
+
width_ptr,
|
|
96
|
+
height_ptr,
|
|
97
|
+
buffer_ptr,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def input_text(self, connect_id: int, text: str) -> int:
|
|
101
|
+
"""
|
|
102
|
+
输入文本。
|
|
103
|
+
|
|
104
|
+
API 原型:
|
|
105
|
+
`int nemu_input_text(int handle, int size, const char* buf)`
|
|
106
|
+
|
|
107
|
+
:param connect_id: 连接 ID。
|
|
108
|
+
:param text: 输入文本(utf-8)。
|
|
109
|
+
:return: 0 表示成功,>0 表示失败。
|
|
110
|
+
"""
|
|
111
|
+
buf = text.encode('utf-8')
|
|
112
|
+
return self.lib.nemu_input_text(connect_id, len(buf), buf)
|
|
113
|
+
|
|
114
|
+
def input_touch_down(self, connect_id: int, display_id: int, x: int, y: int) -> int:
|
|
115
|
+
"""
|
|
116
|
+
发送触摸按下事件。
|
|
117
|
+
|
|
118
|
+
API 原型:
|
|
119
|
+
`int nemu_input_event_touch_down(int handle, int displayid, int x_point, int y_point)`
|
|
120
|
+
|
|
121
|
+
:param connect_id: 连接 ID。
|
|
122
|
+
:param display_id: 显示屏 ID。
|
|
123
|
+
:param x: 触摸点 X 坐标。
|
|
124
|
+
:param y: 触摸点 Y 坐标。
|
|
125
|
+
:return: 0 表示成功,>0 表示失败。
|
|
126
|
+
"""
|
|
127
|
+
return self.lib.nemu_input_event_touch_down(connect_id, display_id, x, y)
|
|
128
|
+
|
|
129
|
+
def input_touch_up(self, connect_id: int, display_id: int) -> int:
|
|
130
|
+
"""
|
|
131
|
+
发送触摸抬起事件。
|
|
132
|
+
|
|
133
|
+
API 原型:
|
|
134
|
+
`int nemu_input_event_touch_up(int handle, int displayid)`
|
|
135
|
+
|
|
136
|
+
:param connect_id: 连接 ID。
|
|
137
|
+
:param display_id: 显示屏 ID。
|
|
138
|
+
:return: 0 表示成功,>0 表示失败。
|
|
139
|
+
"""
|
|
140
|
+
return self.lib.nemu_input_event_touch_up(connect_id, display_id)
|
|
141
|
+
|
|
142
|
+
def input_key_down(self, connect_id: int, display_id: int, key_code: int) -> int:
|
|
143
|
+
"""
|
|
144
|
+
发送按键按下事件。
|
|
145
|
+
|
|
146
|
+
API 原型:
|
|
147
|
+
`int nemu_input_event_key_down(int handle, int displayid, int key_code)`
|
|
148
|
+
|
|
149
|
+
:param connect_id: 连接 ID。
|
|
150
|
+
:param display_id: 显示屏 ID。
|
|
151
|
+
:param key_code: 按键码。
|
|
152
|
+
:return: 0 表示成功,>0 表示失败。
|
|
153
|
+
"""
|
|
154
|
+
return self.lib.nemu_input_event_key_down(connect_id, display_id, key_code)
|
|
155
|
+
|
|
156
|
+
def input_key_up(self, connect_id: int, display_id: int, key_code: int) -> int:
|
|
157
|
+
"""
|
|
158
|
+
发送按键抬起事件。
|
|
159
|
+
|
|
160
|
+
API 原型:
|
|
161
|
+
`int nemu_input_event_key_up(int handle, int displayid, int key_code)`
|
|
162
|
+
|
|
163
|
+
:param connect_id: 连接 ID。
|
|
164
|
+
:param display_id: 显示屏 ID。
|
|
165
|
+
:param key_code: 按键码。
|
|
166
|
+
:return: 0 表示成功,>0 表示失败。
|
|
167
|
+
"""
|
|
168
|
+
return self.lib.nemu_input_event_key_up(connect_id, display_id, key_code)
|
|
169
|
+
|
|
170
|
+
def input_finger_touch_down(self, connect_id: int, display_id: int, finger_id: int, x: int, y: int) -> int:
|
|
171
|
+
"""
|
|
172
|
+
多指触摸按下。
|
|
173
|
+
|
|
174
|
+
API 原型:
|
|
175
|
+
`int nemu_input_event_finger_touch_down(int handle, int displayid, int finger_id, int x_point, int y_point)`
|
|
176
|
+
|
|
177
|
+
:param connect_id: 连接 ID。
|
|
178
|
+
:param display_id: 显示屏 ID。
|
|
179
|
+
:param finger_id: 手指编号(1-10)。
|
|
180
|
+
:param x: 触摸点 X 坐标。
|
|
181
|
+
:param y: 触摸点 Y 坐标。
|
|
182
|
+
:return: 0 表示成功,>0 表示失败。
|
|
183
|
+
"""
|
|
184
|
+
return self.lib.nemu_input_event_finger_touch_down(connect_id, display_id, finger_id, x, y)
|
|
185
|
+
|
|
186
|
+
def input_finger_touch_up(self, connect_id: int, display_id: int, finger_id: int) -> int:
|
|
187
|
+
"""
|
|
188
|
+
多指触摸抬起。
|
|
189
|
+
|
|
190
|
+
API 原型:
|
|
191
|
+
`int nemu_input_event_finger_touch_up(int handle, int displayid, int slot_id)`
|
|
192
|
+
|
|
193
|
+
:param connect_id: 连接 ID。
|
|
194
|
+
:param display_id: 显示屏 ID。
|
|
195
|
+
:param finger_id: 手指编号(1-10)。
|
|
196
|
+
:return: 0 表示成功,>0 表示失败。
|
|
197
|
+
"""
|
|
198
|
+
return self.lib.nemu_input_event_finger_touch_up(connect_id, display_id, finger_id)
|
|
199
|
+
|
|
200
|
+
# ------------------------------------------------------------------
|
|
201
|
+
# 内部工具
|
|
202
|
+
# ------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
def __load_dll(self, mumu_root_folder: str) -> ctypes.CDLL:
|
|
205
|
+
"""尝试多条路径加载 DLL。传入为 MuMu 根目录。"""
|
|
206
|
+
candidate_paths = [
|
|
207
|
+
os.path.join(mumu_root_folder, "shell", "sdk", "external_renderer_ipc.dll"),
|
|
208
|
+
os.path.join(
|
|
209
|
+
mumu_root_folder,
|
|
210
|
+
"shell",
|
|
211
|
+
"nx_device",
|
|
212
|
+
"12.0",
|
|
213
|
+
"sdk",
|
|
214
|
+
"external_renderer_ipc.dll",
|
|
215
|
+
),
|
|
216
|
+
]
|
|
217
|
+
for p in candidate_paths:
|
|
218
|
+
if not os.path.exists(p):
|
|
219
|
+
continue
|
|
220
|
+
try:
|
|
221
|
+
return ctypes.CDLL(p)
|
|
222
|
+
except OSError as e: # pragma: no cover
|
|
223
|
+
logger.warning("Failed to load DLL (%s): %s", p, e)
|
|
224
|
+
raise NemuIpcIncompatible("external_renderer_ipc.dll not found or failed to load.")
|
|
225
|
+
|
|
226
|
+
def __declare_prototypes(self) -> None:
|
|
227
|
+
"""声明 DLL 函数原型,确保 ctypes 类型安全。"""
|
|
228
|
+
# 连接 / 断开
|
|
229
|
+
self.lib.nemu_connect.argtypes = [ctypes.c_wchar_p, ctypes.c_int]
|
|
230
|
+
self.lib.nemu_connect.restype = ctypes.c_int
|
|
231
|
+
|
|
232
|
+
self.lib.nemu_disconnect.argtypes = [ctypes.c_int]
|
|
233
|
+
self.lib.nemu_disconnect.restype = None
|
|
234
|
+
|
|
235
|
+
# 获取 display id
|
|
236
|
+
self.lib.nemu_get_display_id.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
|
|
237
|
+
self.lib.nemu_get_display_id.restype = ctypes.c_int
|
|
238
|
+
|
|
239
|
+
# 截图
|
|
240
|
+
self.lib.nemu_capture_display.argtypes = [
|
|
241
|
+
ctypes.c_int,
|
|
242
|
+
ctypes.c_uint,
|
|
243
|
+
ctypes.c_int,
|
|
244
|
+
ctypes.c_void_p,
|
|
245
|
+
ctypes.c_void_p,
|
|
246
|
+
ctypes.c_void_p,
|
|
247
|
+
]
|
|
248
|
+
self.lib.nemu_capture_display.restype = ctypes.c_int
|
|
249
|
+
|
|
250
|
+
# 输入文本
|
|
251
|
+
self.lib.nemu_input_text.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_char_p]
|
|
252
|
+
self.lib.nemu_input_text.restype = ctypes.c_int
|
|
253
|
+
|
|
254
|
+
# 触摸
|
|
255
|
+
self.lib.nemu_input_event_touch_down.argtypes = [
|
|
256
|
+
ctypes.c_int,
|
|
257
|
+
ctypes.c_int,
|
|
258
|
+
ctypes.c_int,
|
|
259
|
+
ctypes.c_int,
|
|
260
|
+
]
|
|
261
|
+
self.lib.nemu_input_event_touch_down.restype = ctypes.c_int
|
|
262
|
+
|
|
263
|
+
self.lib.nemu_input_event_touch_up.argtypes = [ctypes.c_int, ctypes.c_int]
|
|
264
|
+
self.lib.nemu_input_event_touch_up.restype = ctypes.c_int
|
|
265
|
+
|
|
266
|
+
# 按键
|
|
267
|
+
self.lib.nemu_input_event_key_down.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]
|
|
268
|
+
self.lib.nemu_input_event_key_down.restype = ctypes.c_int
|
|
269
|
+
|
|
270
|
+
self.lib.nemu_input_event_key_up.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]
|
|
271
|
+
self.lib.nemu_input_event_key_up.restype = ctypes.c_int
|
|
272
|
+
|
|
273
|
+
# 多指触摸
|
|
274
|
+
self.lib.nemu_input_event_finger_touch_down.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int]
|
|
275
|
+
self.lib.nemu_input_event_finger_touch_down.restype = ctypes.c_int
|
|
276
|
+
|
|
277
|
+
self.lib.nemu_input_event_finger_touch_up.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int]
|
|
278
|
+
self.lib.nemu_input_event_finger_touch_up.restype = ctypes.c_int
|
|
279
|
+
|
|
280
|
+
logger.debug("DLL function prototypes declared")
|