Feature: Support remote clipboard and input on Android hosts
The MCP remote-control family (remote_open / remote_close / remote_mouse / remote_keyboard / remote_clipboard) previously stopped at Windows and Linux, even though the Android client already translated injected events into touch gestures and global actions. This wires up the missing piece and opens the five tools to APK hosts. Client side, the screen sub-connection now handles COMMAND_SCREEN_SET_CLIPBOARD: the native handler forwards the raw GBK bytes to CaptureService.onClipboardText, which decodes them, strips the trailing NUL that the human clipboard-sync senders append, and writes the system clipboard from a background app (allowed on Android 10+, only reads are restricted). Pasting cannot be a Ctrl+V combo, so VK_V is mapped to ACTION_PASTE on the focused node, falling back to the accessibility focus and then to the first editable node, since WebView and self-drawn inputs often do not flag FOCUS_INPUT or isEditable. A new capability bit CLIENT_CAP_SCREEN_CLIPBOARD (0x0800) tells the server the APK understands both paths, so builds without the handler get an explicit -32005 instead of a package that is silently dropped. action=type is rejected outright for APK hosts - physical key events would only turn 'v' into a paste - and the error points at remote_clipboard + key=V. The platform table, server instructions and the five tool descriptions now describe Android support, which keys actually act as global actions (ESC, HOME, APPS, PRINTSCREEN), and that injection needs the accessibility service while capture needs an authorized MediaProjection session, both without receipts. Verified on the API 35 emulator against a Debug server: ASCII and GBK Chinese text round-tripped through the clipboard into a focused Settings search field, type / expect_hwnd / window_hwnd returned -32005, and PRINTSCREEN produced a saved screenshot. check_platform_table.py and verify_tools.py --offline pass. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
ac43ad92ef
commit
0a0b709e8a
9 files changed
+351
-179
No files matched your search
@@ -22,6 +22,7 @@
|
||||
extern uint64_t g_myClientID;
|
||||
// 定义在 main.cpp,供子连接的 OnReceive 调用
|
||||
extern void DispatchControlEvent(uint32_t msgVal, uint64_t wParam, int32_t ptX, int32_t ptY);
|
||||
extern void DispatchClipboardText(const uint8_t* data, size_t size);
|
||||
|
||||
// Linux/macOS 共用的 BITMAPINFOHEADER 布局(与 Windows 完全一致)
|
||||
#pragma pack(push, 1)
|
||||
@@ -123,6 +124,11 @@ public:
|
||||
case CMD_QUALITY_LEVEL:
|
||||
if (size >= 2) LOGI_SH("QualityLevel=%d", (int)(int8_t)data[1]);
|
||||
break;
|
||||
case COMMAND_SCREEN_SET_CLIPBOARD:
|
||||
// 服务端写剪贴板: [cmd:1][GBK text:N](与 Windows CF_TEXT 同编码;人类剪贴板
|
||||
// 同步会带 '\0' 结尾,MCP 路径不带,Kotlin 侧统一去掉尾随 NUL)
|
||||
if (size > 1) DispatchClipboardText(data + 1, (size_t)size - 1);
|
||||
break;
|
||||
case COMMAND_SCREEN_CONTROL: {
|
||||
// 服务端通过子连接下发鼠标/键盘控制包(MSG64 固定 48 字节)
|
||||
// 坐标从 lParam(offset 24)读取,与 Windows/Linux/macOS 客户端一致:
|
||||
|
||||
@@ -242,6 +242,7 @@ static jmethodID g_forceFirstFrameMid = nullptr;
|
||||
static jmethodID g_statusMid = nullptr; // CaptureService.onNativeStatus(String)
|
||||
static jmethodID g_nativeExitMid = nullptr; // CaptureService.onNativeExit() — 服务端主动断开时停止服务
|
||||
static jmethodID g_updateReceivedMid = nullptr; // CaptureService.onUpdateReceived(String) — 升级 APK 已落盘
|
||||
static jmethodID g_clipboardMid = nullptr; // CaptureService.onClipboardText([B)V — 远程剪贴板
|
||||
// ScreenShot(无障碍 takeScreenshot 抓屏,MCP get_screenshot 用)
|
||||
static jclass g_shotClass = nullptr;
|
||||
static jmethodID g_shotCanMid = nullptr; // ScreenShot.canCapture()Z
|
||||
@@ -282,6 +283,52 @@ void DispatchControlEvent(uint32_t msgVal, uint64_t wParam, int32_t ptX, int32_t
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// 远程剪贴板:服务端下发 [COMMAND_SCREEN_SET_CLIPBOARD][GBK text],字节原样交给
|
||||
// CaptureService.onClipboardText(Kotlin 侧按 GBK 解码后写系统剪贴板)。
|
||||
// 与 DispatchControlEvent 同一套 attach/detach 套路,供屏幕子连接 OnReceive 调用。
|
||||
void DispatchClipboardText(const uint8_t* data, size_t size)
|
||||
{
|
||||
if (!g_jvm || !g_captureClass || !g_clipboardMid) {
|
||||
LOGE("CLIPBOARD: JNI not ready (cls=%p method=%p)", g_captureClass, g_clipboardMid);
|
||||
return;
|
||||
}
|
||||
JNIEnv* jenv = nullptr;
|
||||
bool attached = false;
|
||||
jint st = g_jvm->GetEnv((void**)&jenv, JNI_VERSION_1_6);
|
||||
if (st == JNI_EDETACHED) {
|
||||
if (g_jvm->AttachCurrentThread(&jenv, nullptr) != JNI_OK || !jenv) return;
|
||||
attached = true;
|
||||
} else if (st != JNI_OK || !jenv) {
|
||||
return;
|
||||
}
|
||||
// 服务端已把剪贴板文本限制在 256 KB,这里再兜一层 1 MiB:超出直接丢,
|
||||
// 不截断(半截文本写进剪贴板比不写更危险)
|
||||
const size_t kMaxClipboardBytes = 1u << 20;
|
||||
if (size > kMaxClipboardBytes) {
|
||||
LOGE("CLIPBOARD: payload %zu bytes exceeds %zu, dropped", size, kMaxClipboardBytes);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
return;
|
||||
}
|
||||
jbyteArray arr = jenv->NewByteArray((jsize)size);
|
||||
if (!arr) {
|
||||
if (jenv->ExceptionOccurred()) jenv->ExceptionClear();
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
LOGE("CLIPBOARD: NewByteArray(%zu) failed", size);
|
||||
return;
|
||||
}
|
||||
jenv->SetByteArrayRegion(arr, 0, (jsize)size, (const jbyte*)data);
|
||||
if (!jenv->ExceptionCheck()) {
|
||||
jenv->CallStaticVoidMethod(g_captureClass, g_clipboardMid, arr);
|
||||
if (jenv->ExceptionCheck()) jenv->ExceptionClear();
|
||||
LOGI("CLIPBOARD: dispatched %zu bytes", size);
|
||||
} else {
|
||||
jenv->ExceptionClear();
|
||||
LOGE("CLIPBOARD: SetByteArrayRegion failed");
|
||||
}
|
||||
jenv->DeleteLocalRef(arr);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
static std::string GetActiveWindowFromJava()
|
||||
{
|
||||
if (!g_jvm || !g_ctrlClass || !g_getActiveWindowMid) return "Android";
|
||||
@@ -1369,6 +1416,9 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
g_updateReceivedMid = env->GetStaticMethodID(g_captureClass, "onUpdateReceived", "(Ljava/lang/String;)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_updateReceivedMid = nullptr; }
|
||||
if (!g_updateReceivedMid) LOGE("CaptureService.onUpdateReceived not found");
|
||||
g_clipboardMid = env->GetStaticMethodID(g_captureClass, "onClipboardText", "([B)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_clipboardMid = nullptr; }
|
||||
if (!g_clipboardMid) LOGE("CaptureService.onClipboardText not found");
|
||||
UseAndroidLog(PostStatus);
|
||||
} else {
|
||||
LOGE("CaptureService class not found");
|
||||
|
||||
@@ -4,6 +4,9 @@ import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.Service
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.hardware.display.VirtualDisplay
|
||||
@@ -23,6 +26,8 @@ import android.util.Log
|
||||
import android.view.WindowManager
|
||||
import android.widget.Toast
|
||||
|
||||
import java.nio.charset.Charset
|
||||
|
||||
class CaptureService : Service() {
|
||||
|
||||
companion object {
|
||||
@@ -59,6 +64,36 @@ class CaptureService : Service() {
|
||||
UpdateInstaller.onApkReceived(ctx, path)
|
||||
}
|
||||
|
||||
/**
|
||||
* 远程剪贴板(COMMAND_SCREEN_SET_CLIPBOARD):服务端按 GBK/ANSI 下发字节(Windows
|
||||
* CF_TEXT 语义),这里解成字符串写系统剪贴板。写入不要求应用在前台(Android 10+
|
||||
* 只限制「读」剪贴板),所以主控能往后台的客户端塞文本;粘到目标输入框由用户在
|
||||
* 远端的输入框里用 VK_V(本客户端映射为 ACTION_PASTE)完成。
|
||||
*/
|
||||
@JvmStatic
|
||||
fun onClipboardText(bytes: ByteArray) {
|
||||
val ctx = instance ?: appContext
|
||||
if (ctx == null) {
|
||||
Log.w(TAG, "onClipboardText: no context, ${bytes.size} bytes dropped")
|
||||
return
|
||||
}
|
||||
val cm = ctx.getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager
|
||||
if (cm == null) {
|
||||
Log.e(TAG, "onClipboardText: no ClipboardManager")
|
||||
return
|
||||
}
|
||||
// 人类剪贴板同步(ScreenSpyDlg / HideScreenSpyDlg)发的是带 '\0' 结尾的 CF_TEXT,
|
||||
// MCP 路径不带;统一在这里去掉尾随 NUL,否则粘进输入框会多一个不可见字符
|
||||
val text = String(bytes, Charset.forName("GBK")).trimEnd('\u0000')
|
||||
try {
|
||||
cm.setPrimaryClip(ClipData.newPlainText("YAMA", text))
|
||||
Log.i(TAG, "onClipboardText: ${bytes.size} bytes, ${text.length} chars")
|
||||
} catch (e: Throwable) {
|
||||
// Android 10+ 极少见地会在某些受限场景抛 SecurityException;记日志不崩 JNI 线程
|
||||
Log.e(TAG, "onClipboardText: setPrimaryClip failed: $e")
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun onNativeExit() {
|
||||
val svc = instance ?: return
|
||||
|
||||
@@ -436,9 +436,48 @@ class ControlService : AccessibilityService() {
|
||||
0x24 -> performGlobalAction(GLOBAL_ACTION_HOME) // VK_HOME
|
||||
0x5D -> performGlobalAction(GLOBAL_ACTION_RECENTS) // VK_APPS
|
||||
0x2C -> performGlobalAction(GLOBAL_ACTION_TAKE_SCREENSHOT) // PrtSc
|
||||
0x56 -> pasteIntoFocused() // VK_V → 粘贴
|
||||
}
|
||||
}
|
||||
|
||||
// 远程剪贴板的粘贴动作。Android 没有 Ctrl+V 组合键,而且本客户端不注入文本,
|
||||
// 所以把 VK_V 映射成输入框的 ACTION_PASTE:由目标应用自己读剪贴板,
|
||||
// 不受 Android 10+「后台应用不能读剪贴板」的限制。
|
||||
@Suppress("DEPRECATION")
|
||||
private fun pasteIntoFocused() {
|
||||
val root = rootInActiveWindow ?: run {
|
||||
Log.w(TAG, "paste: no active window")
|
||||
return
|
||||
}
|
||||
// 焦点链:输入焦点 → 无障碍焦点 → 整棵树里第一个可编辑节点。部分应用只置
|
||||
// FOCUS_ACCESSIBILITY;也不拿 isEditable 当门槛(WebView / 自绘输入框常常不置位
|
||||
// 但照样响应 ACTION_PASTE),真正的判据是 performAction 的返回值。
|
||||
val target = root.findFocus(AccessibilityNodeInfo.FOCUS_INPUT)
|
||||
?: root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY)
|
||||
?: findFirstEditable(root)
|
||||
if (target == null) {
|
||||
Log.w(TAG, "paste: no focused or editable node")
|
||||
} else {
|
||||
if (!target.performAction(AccessibilityNodeInfo.ACTION_PASTE))
|
||||
Log.w(TAG, "paste: ACTION_PASTE rejected by ${target.packageName}")
|
||||
target.recycle()
|
||||
}
|
||||
root.recycle()
|
||||
}
|
||||
|
||||
// 找不到焦点时的兜底:返回整棵树里第一个可见的可编辑节点(调用方负责 recycle)
|
||||
@Suppress("DEPRECATION")
|
||||
private fun findFirstEditable(node: AccessibilityNodeInfo): AccessibilityNodeInfo? {
|
||||
if (node.isEditable && node.isVisibleToUser) return AccessibilityNodeInfo.obtain(node)
|
||||
for (i in 0 until node.childCount) {
|
||||
val child = node.getChild(i) ?: continue
|
||||
val hit = findFirstEditable(child)
|
||||
child.recycle()
|
||||
if (hit != null) return hit
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun dispatchTap(x: Float, y: Float) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
|
||||
val path = Path().apply { moveTo(x, y); lineTo(x + 1f, y) }
|
||||
|
||||
Reference in new issue
Block a user