From 7e6a779cf1c79a5ffcb960b0a8f868a8365202da Mon Sep 17 00:00:00 2001 From: qiyinxi <52456734+qiyinxi@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:38:12 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E5=AE=BF=E4=B8=BB=E4=BE=A7=E7=82=B9?= =?UTF-8?q?=E5=87=BB=EF=BC=9Amodavd=5Ftouch=5F*=20=E8=B5=B0=E6=A8=A1?= =?UTF-8?q?=E6=8B=9F=E5=99=A8=E6=8E=A7=E5=88=B6=E5=8F=B0=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 宿主侧点击:modavd_touch_* 走模拟器控制台的多点触控事件 - 每个句柄一条控制台长连接,第一次点击时连上,断了重连一次;关闭句柄时先抬起还按着的手指。 - 按多点触控协议 B 拼 event send(槽位、跟踪编号、坐标、压力、接触面积),坐标按共享画面的宽高换算到 0–32767,和 event mouse 的换算、默认压力一致。最多 10 根手指;已按下的再按下当移动。 - 接口版本 2,能力位加 MODAVD_CAP_TOUCH;按键、文字仍是 MODAVD_E_UNSUPPORTED。 - 控制台客户端拆成打开、发送、关闭三步,一次性命令照旧。 - 新增 test_input:假控制台核对每条命令、坐标换算、重连、关闭时抬起和认证。 * 一次性控制台命令挪到 console_command.c,测试记录命令不再用 snprintf - test_open_posix 自己定义 mx_console_command 顶替库里的;点击用到 console.c 的会话函数后,console.c 会被链接进来,两份定义冲突(macOS 链接失败)。一次性命令单独成文件,顶替照旧有效。 - gcc 的 -Wformat-truncation 把 test_input 里按 %s 拷进固定缓冲判为可能截断,改成按长度拷贝。 * test_input:realpath 的缓冲区按 PATH_MAX 给 Linux 上 PATH_MAX 是 4096,加固过的 glibc 对 1024 字节的缓冲区直接中止进程;macOS 的 PATH_MAX 正好是 1024,所以只在 Linux 上挂。 --- CMakeLists.txt | 19 +- README.md | 10 +- include/modavd_extras.h | 32 +++- src/console.c | 111 ++++++++---- src/console_command.c | 22 +++ src/core.c | 47 +---- src/input.c | 258 +++++++++++++++++++++++++++ src/mx_internal.h | 36 ++++ tests/test_core.c | 9 +- tests/test_input.c | 375 ++++++++++++++++++++++++++++++++++++++++ 10 files changed, 824 insertions(+), 95 deletions(-) create mode 100644 src/console_command.c create mode 100644 src/input.c create mode 100644 tests/test_input.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 93ac43a..5f3a7b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,13 +28,21 @@ if(NOT WIN32) endif() endif() -set(MX_SOURCES src/core.c src/shm.c src/console.c src/log.c) +set(MX_SOURCES src/core.c src/shm.c src/console.c src/console_command.c src/input.c src/log.c) + +if(NOT WIN32) + # POSIX: the per-handle input lock. + find_package(Threads REQUIRED) +endif() function(mx_link_system target) if(WIN32) target_link_libraries(${target} PRIVATE ws2_32) - elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") - target_link_libraries(${target} PRIVATE rt) + else() + target_link_libraries(${target} PRIVATE Threads::Threads) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + target_link_libraries(${target} PRIVATE rt) + endif() endif() endfunction() @@ -57,6 +65,11 @@ target_link_libraries(test_core PRIVATE mx_core) mx_link_system(test_core) add_test(NAME test_core COMMAND test_core) +add_executable(test_input tests/test_input.c) +target_link_libraries(test_input PRIVATE mx_core) +mx_link_system(test_input) +add_test(NAME test_input COMMAND test_input) + if(NOT WIN32) add_executable(test_open_posix tests/test_open_posix.c) target_link_libraries(test_open_posix PRIVATE mx_core) diff --git a/README.md b/README.md index cc6ede6..33d10a5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ## 这是什么 -一个 C 库:从 [ModAVD](https://github.com/ModAVD/ModAVD) 实例直接取画面,不经 adb。给 MAA(MaaCore)、MaaFramework 这类自动化框架接入用。以后还会加宿主侧的点击和按键。 +一个 C 库:从 [ModAVD](https://github.com/ModAVD/ModAVD) 实例直接取画面、在宿主这边点击,不经 adb。给 MAA(MaaCore)、MaaFramework 这类自动化框架接入用。 - 画面来自模拟器自己的共享内存:`screenrecord webrtc start`(或启动参数 `-share-vid`)之后,模拟器把每一帧写进一块按控制台端口命名的共享内存。 - Windows 上叫 `SHM_videmulator<端口>`,Linux、macOS 上叫 `videmulator<端口>`; @@ -21,12 +21,13 @@ - 拷出一帧,换成调用方要的格式; - 遇到新帧写到一半时重拷; - 告诉调用方现在能不能用。 +- 点击走模拟器控制台的 `event send`,发标准的多点触控事件(槽位、跟踪编号、坐标、压力),到达模拟器自己的虚拟触屏:安卓看到的是屏幕上的手指,和在模拟器窗口里点一样,不是应用注入的事件,客体里也不用装东西。最多 10 根手指。 - 库**不自己退回 adb**:出错时返回错误码,让调用方按自己的规则换别的办法。 - 只依赖系统库:Windows 上是 `KERNEL32`、`WS2_32`,C 运行库静态链接;POSIX 上是 `shm_open`、`mmap` 和套接字。 ## 状态 -- v0.1:只有截图。点击、按键、文字的函数已经声明,现在返回 `MODAVD_E_UNSUPPORTED`。 +- 截图(接口版本 1)和点击(接口版本 2,`MODAVD_CAP_TOUCH`)。按键、文字的函数已经声明,现在返回 `MODAVD_E_UNSUPPORTED`:控制台发的按键会进触屏设备、被丢掉,`event text` 会悄悄丢掉键盘映射里没有的字。 - Windows:截图这套逻辑在 ModAVD 上已经用了一个多月,形式是下面的兼容外壳。 - macOS(Apple Silicon):11-09 有贡献者用官方 Emulator 37.2.13、API 35 ARM64 实测过截图。 - Linux:能编,单元测试在 CI 上跑;还没有在模拟器上实测。 @@ -106,11 +107,12 @@ ctest --test-dir build --output-on-failure ## English -ModAVDExtras is a small C library that reads the screen of a [ModAVD](https://github.com/ModAVD/ModAVD) instance directly, without adb, for automation frameworks such as MaaCore (MAA) and MaaFramework. Host-side touch and key input will follow. +ModAVDExtras is a small C library that reads the screen of a [ModAVD](https://github.com/ModAVD/ModAVD) instance directly, without adb, for automation frameworks such as MaaCore (MAA) and MaaFramework, and touches it from the host. - Frames come from the emulator's own shared memory: after `screenrecord webrtc start` (or `-share-vid`), the emulator writes every frame to a region named after its console port: `SHM_videmulator` on Windows, `videmulator` elsewhere. The layout is a 24-byte header (width, height, fps, frame number, timestamp) followed by BGRA pixels. - The library finds the console port from an adb address, starts frame sharing through the console when it is off, copies a frame in the format the caller asks for (copying again if a new frame arrived meanwhile), and reports whether capture is usable. It never falls back to adb by itself. -- Version 0.1 does screen capture only; the input functions are declared and return `MODAVD_E_UNSUPPORTED`. See [include/modavd_extras.h](include/modavd_extras.h). +- Touch (API version 2, `MODAVD_CAP_TOUCH`) goes through the console's `event send` as standard multi-touch events (slot, tracking id, position, pressure) to the emulator's own virtual touch screen: Android sees a finger on the screen, as with a click on the emulator window, not an injected event, and nothing is installed in the guest. Up to 10 contacts. +- Keys and text are declared and return `MODAVD_E_UNSUPPORTED`: keys sent through the console reach the touch screen device and are dropped, and `event text` silently drops characters missing from the keyboard map. See [include/modavd_extras.h](include/modavd_extras.h). - `external_renderer_ipc.dll` is a Windows-only compatibility shell. It exports the four capture functions that MaaCore looks up in MuMu's DLL, on top of the same code, so MaaCore versions that do not know ModAVD can still capture from it. AUTO-MAS uses it today. It will be retired once MaaCore can connect to ModAVD directly. - Status: Windows has used this capture code for over a month through the shell below; on Apple Silicon macOS a contributor tested capture with the official Emulator 37.2.13 and API 35 ARM64; Linux builds and passes the unit tests in CI but has not been tested with an emulator. From v0.1.2 the [releases](https://github.com/ModAVD/ModAVDExtras/releases) carry `modavd_extras.dll` (Windows x64), `libmodavd_extras.dylib` (macOS arm64) and `libmodavd_extras.so` (Linux x64), plus the Windows-only shell. - Build: `build.ps1` on Windows (Visual Studio 2022 Build Tools, CMake, Ninja); CMake on Linux and macOS. The unit tests need no emulator. diff --git a/include/modavd_extras.h b/include/modavd_extras.h index 735e1ac..c76c11b 100644 --- a/include/modavd_extras.h +++ b/include/modavd_extras.h @@ -1,5 +1,5 @@ /* - * ModAVDExtras: screen capture (and, later, input) for ModAVD instances, + * ModAVDExtras: screen capture and touch input for ModAVD instances, * for automation frameworks that want to skip adb. * * Copyright 2026 qiyinxi @@ -37,8 +37,10 @@ extern "C" { #define MODAVD_API __attribute__((visibility("default"))) #endif -/* Bumped whenever a function or field is added. Nothing is ever removed. */ -#define MODAVD_API_VERSION 1 +/* Bumped whenever a function or field is added, or a declared one starts to work. + * Nothing is ever removed. + * 1: capture. 2: touch (modavd_touch_*). */ +#define MODAVD_API_VERSION 2 typedef struct modavd_instance* modavd_handle; @@ -57,7 +59,7 @@ enum { /* modavd_capabilities() */ enum { MODAVD_CAP_SCREENCAP = 1u << 0, - MODAVD_CAP_TOUCH = 1u << 1, /* not yet */ + MODAVD_CAP_TOUCH = 1u << 1, /* API version 2 */ MODAVD_CAP_KEY = 1u << 2, /* not yet */ MODAVD_CAP_TEXT = 1u << 3, /* not yet */ }; @@ -154,14 +156,30 @@ MODAVD_API int modavd_console_port(modavd_handle h); /* > 0, or MODAVD_E_IN MODAVD_API int modavd_capture(modavd_handle h, int display, uint32_t format, uint32_t flags, void* buf, size_t buf_size, modavd_frame_info* info); -/* ---- input: declared for the next version, MODAVD_E_UNSUPPORTED for now ---- */ +/* ---- touch (API version 2, MODAVD_CAP_TOUCH) ---- */ -/* Coordinates are display pixels; contact is 0-based (at most 10 at once). */ +/* Touches on the emulator's own touch screen, sent through its console: Android + * sees them as a finger on the screen, not as injected events. Needs nothing in + * the guest. The console session opens on the first call and reopens once if it + * broke (the emulator restarted). + * + * Coordinates are display pixels of the shared frame (clamped to it); contact is + * 0-based, at most 10 at once; pressure 0 = the default (what a mouse click on + * the emulator window uses). Only display 0. + * - touch_down on a contact that is already down moves it. + * - touch_move on a contact that is not down: MODAVD_E_INVALID. + * - touch_up on a contact that is not down sends nothing and returns MODAVD_OK. + * MODAVD_E_NOT_READY: no frame was published yet, so the display size is unknown. + * MODAVD_E_NOT_FOUND: the console does not answer. MODAVD_E_IO: auth failed or + * the console refused the command. */ MODAVD_API int modavd_touch_down(modavd_handle h, int display, int contact, int x, int y, int pressure); MODAVD_API int modavd_touch_move(modavd_handle h, int display, int contact, int x, int y, int pressure); MODAVD_API int modavd_touch_up(modavd_handle h, int display, int contact); -/* Lifts every contact this handle pressed. modavd_close() will do the same. */ +/* Lifts every contact this handle pressed. modavd_close() does the same. */ MODAVD_API int modavd_touch_release_all(modavd_handle h); + +/* ---- keys and text: declared, MODAVD_E_UNSUPPORTED for now ---- */ + /* Android key code (KEYCODE_*); down = 1 press, 0 release. */ MODAVD_API int modavd_key(modavd_handle h, int display, int android_keycode, int down); MODAVD_API int modavd_input_text(modavd_handle h, const char* utf8); diff --git a/src/console.c b/src/console.c index a34b643..2acb65c 100644 --- a/src/console.c +++ b/src/console.c @@ -1,6 +1,7 @@ /* * A minimal client for the emulator console (telnet-style text on 127.0.0.1). - * Version 0 only uses it to start frame sharing; input will use it too. + * Used to start frame sharing (one-off connections) and to send touch input + * (a session kept open per handle). * * Copyright 2026 qiyinxi * SPDX-License-Identifier: LGPL-3.0-or-later @@ -97,8 +98,9 @@ static int send_line(mx_socket s, const char* line) return send(s, cmd, n, MX_SEND_FLAGS) == n ? 0 : -1; } -int mx_console_command(int port, const char* command, int timeout_ms) +int mx_console_open(int port, int timeout_ms, mx_console* c) { + c->sock = (intptr_t)MX_BAD_SOCKET; char token[256]; read_auth_token(token, sizeof(token)); @@ -108,55 +110,88 @@ int mx_console_command(int port, const char* command, int timeout_ms) return MODAVD_E_IO; } #endif - int rc = MODAVD_E_NOT_FOUND; mx_socket s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (s != MX_BAD_SOCKET) { + if (s == MX_BAD_SOCKET) { #ifdef _WIN32 - DWORD tv = (DWORD)timeout_ms; + WSACleanup(); +#endif + return MODAVD_E_IO; + } +#ifdef _WIN32 + DWORD tv = (DWORD)timeout_ms; #else - struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 }; + struct timeval tv = { timeout_ms / 1000, (timeout_ms % 1000) * 1000 }; #endif - setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); #if defined(SO_NOSIGPIPE) - int one = 1; - setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); + int one = 1; + setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); #endif - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons((unsigned short)port); - addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - char buf[4096]; - if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) == 0 && read_until_ok(s, buf, sizeof(buf)) == 1) { - int authed = 1; - if (token[0]) { - char auth[300]; - snprintf(auth, sizeof(auth), "auth %s", token); - authed = send_line(s, auth) == 0 && read_until_ok(s, buf, sizeof(buf)) == 1; - } - if (!authed) { - mx_log(MODAVD_LOG_WARN, "console %d auth failed", port); - rc = MODAVD_E_IO; - } - else { - int ok = send_line(s, command) == 0 && read_until_ok(s, buf, sizeof(buf)) == 1; - mx_log(MODAVD_LOG_INFO, "console %d %s -> %s", port, command, ok ? "OK" : "fail"); - rc = ok ? MODAVD_OK : MODAVD_E_IO; - } - send_line(s, "quit"); - } - else { + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons((unsigned short)port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + char buf[4096]; + int rc = MODAVD_OK; + if (connect(s, (struct sockaddr*)&addr, sizeof(addr)) != 0 || read_until_ok(s, buf, sizeof(buf)) != 1) { #ifdef _WIN32 - int err = WSAGetLastError(); + int err = WSAGetLastError(); #else - int err = 0; + int err = 0; #endif - mx_log(MODAVD_LOG_WARN, "console %d connect failed (%d)", port, err); + mx_log(MODAVD_LOG_WARN, "console %d connect failed (%d)", port, err); + rc = MODAVD_E_NOT_FOUND; + } + else if (token[0]) { + char auth[300]; + snprintf(auth, sizeof(auth), "auth %s", token); + if (send_line(s, auth) != 0 || read_until_ok(s, buf, sizeof(buf)) != 1) { + mx_log(MODAVD_LOG_WARN, "console %d auth failed", port); + rc = MODAVD_E_IO; } + } + if (rc != MODAVD_OK) { mx_close_socket(s); +#ifdef _WIN32 + WSACleanup(); +#endif + return rc; + } + c->sock = (intptr_t)s; + return MODAVD_OK; +} + +int mx_console_send(mx_console* c, const char* command) +{ + if (c->sock == (intptr_t)MX_BAD_SOCKET) { + return MODAVD_E_NOT_FOUND; + } + mx_socket s = (mx_socket)c->sock; + char buf[4096]; + if (send_line(s, command) != 0) { + mx_console_close(c); + return MODAVD_E_NOT_FOUND; + } + int r = read_until_ok(s, buf, sizeof(buf)); + if (r < 0) { + /* The emulator went away or stopped answering: the session is unusable. */ + mx_console_close(c); + return MODAVD_E_NOT_FOUND; + } + return r == 1 ? MODAVD_OK : MODAVD_E_IO; +} + +void mx_console_close(mx_console* c) +{ + if (c->sock == (intptr_t)MX_BAD_SOCKET) { + return; } + mx_socket s = (mx_socket)c->sock; + send_line(s, "quit"); + mx_close_socket(s); + c->sock = (intptr_t)MX_BAD_SOCKET; #ifdef _WIN32 WSACleanup(); #endif - return rc; } diff --git a/src/console_command.c b/src/console_command.c new file mode 100644 index 0000000..86266cc --- /dev/null +++ b/src/console_command.c @@ -0,0 +1,22 @@ +/* + * One console command on its own connection (used to start frame sharing). + * Kept apart from console.c so that a test can replace it while the touch + * session functions still come from the library. + * + * Copyright 2026 qiyinxi + * SPDX-License-Identifier: LGPL-3.0-or-later + */ +#include "mx_internal.h" + +int mx_console_command(int port, const char* command, int timeout_ms) +{ + mx_console c; + int rc = mx_console_open(port, timeout_ms, &c); + if (rc != MODAVD_OK) { + return rc; + } + rc = mx_console_send(&c, command); + mx_log(MODAVD_LOG_INFO, "console %d %s -> %s", port, command, rc == MODAVD_OK ? "OK" : "fail"); + mx_console_close(&c); + return rc == MODAVD_OK ? MODAVD_OK : MODAVD_E_IO; +} diff --git a/src/core.c b/src/core.c index 84c9d5e..e2039e2 100644 --- a/src/core.c +++ b/src/core.c @@ -133,10 +133,14 @@ int modavd_open(const modavd_open_params* params, modavd_handle* out) } struct modavd_instance* inst = (struct modavd_instance*)calloc(1, sizeof(*inst)); - if (!inst) { + struct mx_input* input = mx_input_create(port); + if (!inst || !input) { + free(inst); + mx_input_destroy(input); mx_shm_close(&shm); return MODAVD_E_IO; } + inst->input = input; inst->console_port = port; inst->simd = mx_cpu_simd(); inst->shm = shm; @@ -152,11 +156,13 @@ void modavd_close(modavd_handle h) if (!h) { return; } + /* Lifts the contacts this handle left down. */ + mx_input_destroy(h->input); mx_shm_close(&h->shm); free(h); } -uint32_t modavd_capabilities(modavd_handle h) { return h ? MODAVD_CAP_SCREENCAP : 0; } +uint32_t modavd_capabilities(modavd_handle h) { return h ? MODAVD_CAP_SCREENCAP | MODAVD_CAP_TOUCH : 0; } int modavd_console_port(modavd_handle h) { return h ? h->console_port : MODAVD_E_INVALID; } @@ -364,40 +370,3 @@ int modavd_capture(modavd_handle h, int display, uint32_t format, uint32_t flags } return rc; } - -/* Input arrives in the next version. */ -int modavd_touch_down(modavd_handle h, int display, int contact, int x, int y, int pressure) -{ - (void)h, (void)display, (void)contact, (void)x, (void)y, (void)pressure; - return MODAVD_E_UNSUPPORTED; -} - -int modavd_touch_move(modavd_handle h, int display, int contact, int x, int y, int pressure) -{ - (void)h, (void)display, (void)contact, (void)x, (void)y, (void)pressure; - return MODAVD_E_UNSUPPORTED; -} - -int modavd_touch_up(modavd_handle h, int display, int contact) -{ - (void)h, (void)display, (void)contact; - return MODAVD_E_UNSUPPORTED; -} - -int modavd_touch_release_all(modavd_handle h) -{ - (void)h; - return MODAVD_E_UNSUPPORTED; -} - -int modavd_key(modavd_handle h, int display, int android_keycode, int down) -{ - (void)h, (void)display, (void)android_keycode, (void)down; - return MODAVD_E_UNSUPPORTED; -} - -int modavd_input_text(modavd_handle h, const char* utf8) -{ - (void)h, (void)utf8; - return MODAVD_E_UNSUPPORTED; -} diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..a305e68 --- /dev/null +++ b/src/input.c @@ -0,0 +1,258 @@ +/* + * Touch input through the emulator console: "event send" with raw multi-touch + * events (protocol B: slot, tracking id, position, pressure), which the + * emulator hands to the virtio touch screen of display 0 as they are. They + * reach Android as touches on that screen, like a finger on the window, not + * as events injected by an app. + * + * Copyright 2026 qiyinxi + * SPDX-License-Identifier: LGPL-3.0-or-later + */ +#include +#include +#include + +#include "mx_internal.h" + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +typedef SRWLOCK mx_mutex; +#define mx_mutex_init(m) InitializeSRWLock(m) +#define mx_mutex_destroy(m) ((void)(m)) +#define mx_mutex_lock(m) AcquireSRWLockExclusive(m) +#define mx_mutex_unlock(m) ReleaseSRWLockExclusive(m) +#else +#include +typedef pthread_mutex_t mx_mutex; +#define mx_mutex_init(m) pthread_mutex_init((m), NULL) +#define mx_mutex_destroy(m) pthread_mutex_destroy(m) +#define mx_mutex_lock(m) pthread_mutex_lock(m) +#define mx_mutex_unlock(m) pthread_mutex_unlock(m) +#endif + +/* Linux input codes (input-event-codes.h); the console takes numbers as well as names. */ +#define EV_SYN_ 0 +#define EV_ABS_ 3 +#define ABS_MT_SLOT_ 0x2f +#define ABS_MT_TOUCH_MAJOR_ 0x30 +#define ABS_MT_TOUCH_MINOR_ 0x31 +#define ABS_MT_POSITION_X_ 0x35 +#define ABS_MT_POSITION_Y_ 0x36 +#define ABS_MT_TRACKING_ID_ 0x39 +#define ABS_MT_PRESSURE_ 0x3a + +#define MX_INPUT_TIMEOUT_MS 1000 + +struct mx_input { + mx_mutex lock; + int port; + mx_console console; + uint32_t down; /* bit n: contact n is pressed */ +}; + +int mx_touch_axis(int v, int size) +{ + if (size <= 0 || v <= 0) { + return 0; + } + if (v >= size) { + v = size - 1; + } + /* qemu_input_scale_axis(v, 0, size, 0, MX_ABS_MAX), as for "event mouse" */ + return (int)((int64_t)v * MX_ABS_MAX / size); +} + +int mx_touch_command(char* buf, size_t cap, int action, int contact, int ax, int ay, int pressure) +{ + int n = -1; + switch (action) { + case MX_TOUCH_DOWN: + n = snprintf(buf, cap, "event send %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:0", + EV_ABS_, ABS_MT_SLOT_, contact, EV_ABS_, ABS_MT_TRACKING_ID_, contact, EV_ABS_, + ABS_MT_POSITION_X_, ax, EV_ABS_, ABS_MT_POSITION_Y_, ay, EV_ABS_, ABS_MT_PRESSURE_, pressure, + EV_ABS_, ABS_MT_TOUCH_MAJOR_, MX_TOUCH_SIZE, EV_ABS_, ABS_MT_TOUCH_MINOR_, MX_TOUCH_SIZE, EV_SYN_, + 0); + break; + case MX_TOUCH_MOVE: + n = snprintf(buf, cap, "event send %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:%d %d:%d:0", EV_ABS_, ABS_MT_SLOT_, + contact, EV_ABS_, ABS_MT_POSITION_X_, ax, EV_ABS_, ABS_MT_POSITION_Y_, ay, EV_ABS_, + ABS_MT_PRESSURE_, pressure, EV_SYN_, 0); + break; + case MX_TOUCH_UP: + n = snprintf(buf, cap, "event send %d:%d:%d %d:%d:0 %d:%d:-1 %d:%d:0", EV_ABS_, ABS_MT_SLOT_, contact, EV_ABS_, + ABS_MT_PRESSURE_, EV_ABS_, ABS_MT_TRACKING_ID_, EV_SYN_, 0); + break; + default: break; + } + return (n < 0 || (size_t)n >= cap) ? -1 : n; +} + +struct mx_input* mx_input_create(int console_port) +{ + struct mx_input* in = (struct mx_input*)calloc(1, sizeof(*in)); + if (!in) { + return NULL; + } + mx_mutex_init(&in->lock); + in->port = console_port; + in->console.sock = -1; + return in; +} + +/* Sends one command on the session, opening it first and once more if it broke + * (the emulator restarted, or closed an idle session). Caller holds the lock. */ +static int send_locked(struct mx_input* in, const char* command) +{ + for (int attempt = 0; attempt < 2; ++attempt) { + if (in->console.sock == -1) { + int rc = mx_console_open(in->port, MX_INPUT_TIMEOUT_MS, &in->console); + if (rc != MODAVD_OK) { + return rc; + } + } + int rc = mx_console_send(&in->console, command); + if (rc != MODAVD_E_NOT_FOUND) { + if (rc != MODAVD_OK) { + mx_log(MODAVD_LOG_WARN, "console %d: %s -> KO", in->port, command); + } + return rc; + } + } + return MODAVD_E_NOT_FOUND; +} + +/* Lifts every pressed contact. Caller holds the lock. */ +static int release_all_locked(struct mx_input* in) +{ + int result = MODAVD_OK; + for (int c = 0; c < MX_MAX_CONTACTS; ++c) { + if (!(in->down & (1u << c))) { + continue; + } + char cmd[256]; + mx_touch_command(cmd, sizeof(cmd), MX_TOUCH_UP, c, 0, 0, 0); + int rc = send_locked(in, cmd); + if (rc != MODAVD_OK) { + result = rc; + } + in->down &= ~(1u << c); + } + return result; +} + +void mx_input_destroy(struct mx_input* in) +{ + if (!in) { + return; + } + mx_mutex_lock(&in->lock); + if (in->down) { + release_all_locked(in); + } + mx_console_close(&in->console); + mx_mutex_unlock(&in->lock); + mx_mutex_destroy(&in->lock); + free(in); +} + +/* Display size from the shared frame header; 0 when nothing was published. */ +static void display_size(modavd_handle h, int* w, int* hgt) +{ + const volatile mx_video_info* vi = (const volatile mx_video_info*)h->shm.view; + *w = vi ? (int)vi->width : 0; + *hgt = vi ? (int)vi->height : 0; +} + +static int touch(modavd_handle h, int display, int action, int contact, int x, int y, int pressure) +{ + if (!h || !h->input) { + return MODAVD_E_INVALID; + } + if (display != 0) { + return MODAVD_E_UNSUPPORTED; + } + if (contact < 0 || contact >= MX_MAX_CONTACTS) { + return MODAVD_E_INVALID; + } + struct mx_input* in = h->input; + uint32_t bit = 1u << contact; + char cmd[256]; + int rc = MODAVD_OK; + + mx_mutex_lock(&in->lock); + if (action == MX_TOUCH_UP) { + /* Lifting a contact that is not down sends nothing. */ + if (in->down & bit) { + mx_touch_command(cmd, sizeof(cmd), MX_TOUCH_UP, contact, 0, 0, 0); + rc = send_locked(in, cmd); + in->down &= ~bit; + } + mx_mutex_unlock(&in->lock); + return rc; + } + + int w = 0, hgt = 0; + display_size(h, &w, &hgt); + if (w <= 0 || hgt <= 0) { + mx_mutex_unlock(&in->lock); + return MODAVD_E_NOT_READY; + } + if (action == MX_TOUCH_MOVE && !(in->down & bit)) { + mx_mutex_unlock(&in->lock); + return MODAVD_E_INVALID; + } + if (pressure <= 0 || pressure > MX_TOUCH_PRESSURE) { + pressure = MX_TOUCH_PRESSURE; + } + /* Pressing a contact that is already down moves it, as MuMu's library does. */ + int kind = (action == MX_TOUCH_DOWN && !(in->down & bit)) ? MX_TOUCH_DOWN : MX_TOUCH_MOVE; + mx_touch_command(cmd, sizeof(cmd), kind, contact, mx_touch_axis(x, w), mx_touch_axis(y, hgt), pressure); + rc = send_locked(in, cmd); + if (rc == MODAVD_OK && kind == MX_TOUCH_DOWN) { + in->down |= bit; + } + mx_mutex_unlock(&in->lock); + return rc; +} + +int modavd_touch_down(modavd_handle h, int display, int contact, int x, int y, int pressure) +{ + return touch(h, display, MX_TOUCH_DOWN, contact, x, y, pressure); +} + +int modavd_touch_move(modavd_handle h, int display, int contact, int x, int y, int pressure) +{ + return touch(h, display, MX_TOUCH_MOVE, contact, x, y, pressure); +} + +int modavd_touch_up(modavd_handle h, int display, int contact) +{ + return touch(h, display, MX_TOUCH_UP, contact, 0, 0, 0); +} + +int modavd_touch_release_all(modavd_handle h) +{ + if (!h || !h->input) { + return MODAVD_E_INVALID; + } + mx_mutex_lock(&h->input->lock); + int rc = release_all_locked(h->input); + mx_mutex_unlock(&h->input->lock); + return rc; +} + +/* The console's "event send" delivers keys to the touch screen device, which + * drops them, and "event text" silently drops what the keyboard map lacks: + * neither is reliable enough yet. */ +int modavd_key(modavd_handle h, int display, int android_keycode, int down) +{ + (void)h, (void)display, (void)android_keycode, (void)down; + return MODAVD_E_UNSUPPORTED; +} + +int modavd_input_text(modavd_handle h, const char* utf8) +{ + (void)h, (void)utf8; + return MODAVD_E_UNSUPPORTED; +} diff --git a/src/mx_internal.h b/src/mx_internal.h index 60618f2..73be13e 100644 --- a/src/mx_internal.h +++ b/src/mx_internal.h @@ -73,11 +73,47 @@ void mx_shm_close(mx_shm* shm); * MODAVD_E_IO: auth failed, the command answered KO, or a timeout. */ int mx_console_command(int port, const char* command, int timeout_ms); +/* A console session kept open across commands. sock is the platform socket, + * -1 (INVALID_SOCKET) when closed. */ +typedef struct mx_console { + intptr_t sock; +} mx_console; + +/* Connects and authenticates. MODAVD_OK, MODAVD_E_NOT_FOUND (could not + * connect) or MODAVD_E_IO (auth failed). timeout_ms bounds each reply. */ +int mx_console_open(int port, int timeout_ms, mx_console* c); +/* MODAVD_OK, MODAVD_E_IO (the console answered KO) or MODAVD_E_NOT_FOUND + * (the session broke; it is closed and may be opened again). */ +int mx_console_send(mx_console* c, const char* command); +/* Says quit and closes; nothing happens when already closed. */ +void mx_console_close(mx_console* c); + +/* ---- input.c ---- */ +#define MX_MAX_CONTACTS 10 +#define MX_ABS_MAX 32767 /* the virtio touch screen's axis range (INPUT_EVENT_ABS_MAX) */ +#define MX_TOUCH_PRESSURE 0x400 /* what "event mouse" presses with (MTS_PRESSURE_RANGE_MAX) */ +#define MX_TOUCH_SIZE 0x500 /* ABS_MT_TOUCH_MAJOR / MINOR of "event mouse" */ + +enum { MX_TOUCH_DOWN = 0, MX_TOUCH_MOVE = 1, MX_TOUCH_UP = 2 }; + +/* A display pixel coordinate (0 .. size-1, clamped) on the touch screen's + * 0 .. MX_ABS_MAX axis, scaled as the emulator scales "event mouse". */ +int mx_touch_axis(int v, int size); +/* Writes the console command for one touch action on contact (slot) with + * axis coordinates ax, ay. Returns the length, or -1 if it does not fit. */ +int mx_touch_command(char* buf, size_t cap, int action, int contact, int ax, int ay, int pressure); + +struct mx_input; /* lock, console session and pressed contacts of one handle */ +struct mx_input* mx_input_create(int console_port); +/* Lifts the contacts still down, closes the session and frees. */ +void mx_input_destroy(struct mx_input* in); + /* ---- core.c ---- */ struct modavd_instance { int console_port; int simd; /* mx_cpu_simd() at open */ mx_shm shm; + struct mx_input* input; }; /* 1 when this CPU runs the vector row conversion (SSSE3 on x86, NEON on diff --git a/tests/test_core.c b/tests/test_core.c index 4149bc9..05b8ec1 100644 --- a/tests/test_core.c +++ b/tests/test_core.c @@ -157,7 +157,7 @@ static void test_capture_from_fake_frame(void) return; } CHECK_EQ(modavd_console_port(h), port); - CHECK_EQ(modavd_capabilities(h), MODAVD_CAP_SCREENCAP); + CHECK_EQ(modavd_capabilities(h), MODAVD_CAP_SCREENCAP | MODAVD_CAP_TOUCH); check_capture(h, MODAVD_FMT_BGRA, 4, 0, w, hgt); check_capture(h, MODAVD_FMT_BGR, 3, 0, w, hgt); @@ -193,9 +193,10 @@ static void test_capture_from_fake_frame(void) fake_frame_set_size(&f, w, hgt); CHECK_EQ(modavd_capture(h, 0, MODAVD_FMT_BGR, 0, NULL, 0, &info), MODAVD_OK); - /* input is not in this version */ - CHECK_EQ(modavd_touch_down(h, 0, 0, 1, 1, 1), MODAVD_E_UNSUPPORTED); - CHECK_EQ(modavd_touch_release_all(h), MODAVD_E_UNSUPPORTED); + /* touch goes through the console, and nothing answers on this port (test_input covers a fake one) */ + CHECK_EQ(modavd_touch_down(h, 0, 0, 1, 1, 1), MODAVD_E_NOT_FOUND); + CHECK_EQ(modavd_touch_release_all(h), MODAVD_OK); + /* keys and text are not in this version */ CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_E_UNSUPPORTED); CHECK_EQ(modavd_input_text(h, "a"), MODAVD_E_UNSUPPORTED); diff --git a/tests/test_input.c b/tests/test_input.c new file mode 100644 index 0000000..0319599 --- /dev/null +++ b/tests/test_input.c @@ -0,0 +1,375 @@ +/* + * Touch input against a fake emulator console: the commands sent for each + * action, coordinate scaling, reconnecting after the console drops, lifting + * contacts on close, and the auth line. + * + * Copyright 2026 qiyinxi + * SPDX-License-Identifier: LGPL-3.0-or-later + */ +#include +#include +#include + +#include "fake_frame.h" +#include "modavd_extras.h" + +#ifdef _WIN32 +#include +#include +typedef SOCKET sock_t; +typedef int socklen_t; +#define BAD_SOCK INVALID_SOCKET +#define close_sock closesocket +static void sleep_ms(int ms) { Sleep((DWORD)ms); } +#else +#include +#include +#include +#include +#include +#include +#include +#include +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif +typedef int sock_t; +#define BAD_SOCK (-1) +#define close_sock close +static void sleep_ms(int ms) +{ + struct timespec ts = { ms / 1000, (long)(ms % 1000) * 1000000L }; + nanosleep(&ts, NULL); +} +#endif + +static int g_failures; + +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "%s:%d: CHECK failed: %s\n", __FILE__, __LINE__, #cond); \ + ++g_failures; \ + } \ + } while (0) + +#define CHECK_EQ(a, b) \ + do { \ + long long va_ = (long long)(a), vb_ = (long long)(b); \ + if (va_ != vb_) { \ + fprintf(stderr, "%s:%d: CHECK_EQ failed: %s = %lld, %s = %lld\n", __FILE__, __LINE__, #a, va_, #b, vb_); \ + ++g_failures; \ + } \ + } while (0) + +#define CHECK_STR(a, b) \ + do { \ + const char *sa_ = (a), *sb_ = (b); \ + if (strcmp(sa_, sb_) != 0) { \ + fprintf(stderr, "%s:%d: CHECK_STR failed:\n got %s\n want %s\n", __FILE__, __LINE__, sa_, sb_); \ + ++g_failures; \ + } \ + } while (0) + +/* ---- a fake console: replies OK to every line, records what it got ---- */ + +#define MAX_LINES 64 + +static struct { + sock_t listener; + int port; + char lines[MAX_LINES][256]; + volatile int count; + volatile int connections; + volatile int drop_next; /* close without replying to the next command */ + volatile int stop; +} g_con; + +static void record(const char* line) +{ + if (g_con.count < MAX_LINES) { + size_t n = strlen(line); + if (n >= sizeof(g_con.lines[0])) { + n = sizeof(g_con.lines[0]) - 1; + } + memcpy(g_con.lines[g_con.count], line, n); + g_con.lines[g_con.count][n] = '\0'; + g_con.count = g_con.count + 1; + } +} + +static void serve_client(sock_t c) +{ + static const char kBanner[] = "Android Console: fake\r\nOK\r\n"; + send(c, kBanner, (int)strlen(kBanner), 0); + char buf[2048]; + int len = 0; + for (;;) { + int n = (int)recv(c, buf + len, (int)sizeof(buf) - 1 - len, 0); + if (n <= 0) { + return; + } + len += n; + buf[len] = '\0'; + char* eol; + while ((eol = strstr(buf, "\r\n")) != NULL) { + *eol = '\0'; + record(buf); + int quit = strcmp(buf, "quit") == 0; + int drop = !quit && strncmp(buf, "auth ", 5) != 0 && g_con.drop_next; + int rest = len - (int)(eol + 2 - buf); + memmove(buf, eol + 2, (size_t)rest + 1); + len = rest; + if (quit) { + return; + } + if (drop) { + g_con.drop_next = 0; + return; + } + send(c, "OK\r\n", 4, 0); + } + } +} + +#ifdef _WIN32 +static DWORD WINAPI server_main(LPVOID arg) +#else +static void* server_main(void* arg) +#endif +{ + (void)arg; + while (!g_con.stop) { + sock_t c = accept(g_con.listener, NULL, NULL); + if (c == BAD_SOCK || g_con.stop) { + if (c != BAD_SOCK) { + close_sock(c); + } + break; + } + g_con.connections = g_con.connections + 1; + serve_client(c); + close_sock(c); + } + return 0; +} + +static int start_console(void) +{ + g_con.listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (g_con.listener == BAD_SOCK) { + return -1; + } + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + socklen_t alen = sizeof(addr); + if (bind(g_con.listener, (struct sockaddr*)&addr, sizeof(addr)) != 0 || listen(g_con.listener, 4) != 0 || + getsockname(g_con.listener, (struct sockaddr*)&addr, &alen) != 0) { + return -1; + } + g_con.port = ntohs(addr.sin_port); +#ifdef _WIN32 + HANDLE t = CreateThread(NULL, 0, server_main, NULL, 0, NULL); + if (!t) { + return -1; + } + CloseHandle(t); +#else + pthread_t t; + if (pthread_create(&t, NULL, server_main, NULL) != 0) { + return -1; + } + pthread_detach(t); +#endif + return 0; +} + +static void stop_console(void) +{ + g_con.stop = 1; + /* Wake the blocked accept() with a connection of our own. */ + sock_t s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons((unsigned short)g_con.port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + connect(s, (struct sockaddr*)&addr, sizeof(addr)); + close_sock(s); + sleep_ms(50); + close_sock(g_con.listener); +} + +/* Waits until the console has recorded n lines (the "quit" of a close is not replied to). */ +static int wait_lines(int n) +{ + for (int i = 0; i < 200 && g_con.count < n; ++i) { + sleep_ms(10); + } + return g_con.count; +} + +/* Points the console auth token at a file holding "secret". */ +static int use_auth_token(void) +{ + static const char kHome[] = "test_input_home"; +#ifdef _WIN32 + _mkdir(kHome); + char full[1024]; + if (!_fullpath(full, kHome, sizeof(full)) || _putenv_s("USERPROFILE", full) != 0) { + return -1; + } +#else + mkdir(kHome, 0700); + /* realpath() needs PATH_MAX bytes (4096 on Linux); fortified glibc aborts on less. */ + char full[PATH_MAX]; + if (!realpath(kHome, full) || setenv("HOME", full, 1) != 0) { + return -1; + } +#endif + char path[sizeof(full) + 64]; + snprintf(path, sizeof(path), "%s/.emulator_console_auth_token", full); + FILE* f = fopen(path, "wb"); + if (!f) { + return -1; + } + fputs("secret\n", f); + fclose(f); + return 0; +} + +/* ---- tests ---- */ + +static void test_axis_and_commands(void) +{ + CHECK_EQ(mx_touch_axis(0, 1280), 0); + CHECK_EQ(mx_touch_axis(-5, 1280), 0); + CHECK_EQ(mx_touch_axis(640, 1280), 16383); + CHECK_EQ(mx_touch_axis(1100, 1280), 28159); /* what the emulator sent for "event mouse 1100 ..." */ + CHECK_EQ(mx_touch_axis(1279, 1280), 32741); + CHECK_EQ(mx_touch_axis(5000, 1280), 32741); /* clamped to the last pixel */ + CHECK_EQ(mx_touch_axis(10, 0), 0); + + char buf[256]; + CHECK(mx_touch_command(buf, sizeof(buf), MX_TOUCH_DOWN, 2, 100, 200, 1024) > 0); + CHECK_STR(buf, "event send 3:47:2 3:57:2 3:53:100 3:54:200 3:58:1024 3:48:1280 3:49:1280 0:0:0"); + CHECK(mx_touch_command(buf, sizeof(buf), MX_TOUCH_MOVE, 2, 300, 400, 512) > 0); + CHECK_STR(buf, "event send 3:47:2 3:53:300 3:54:400 3:58:512 0:0:0"); + CHECK(mx_touch_command(buf, sizeof(buf), MX_TOUCH_UP, 2, 0, 0, 0) > 0); + CHECK_STR(buf, "event send 3:47:2 3:58:0 3:57:-1 0:0:0"); + CHECK_EQ(mx_touch_command(buf, 10, MX_TOUCH_DOWN, 0, 1, 1, 1), -1); + CHECK_EQ(mx_touch_command(buf, sizeof(buf), 9, 0, 1, 1, 1), -1); +} + +static void test_touch_session(void) +{ + if (start_console() != 0 || use_auth_token() != 0) { + fprintf(stderr, "fake console setup failed\n"); + ++g_failures; + return; + } + fake_frame f; + if (fake_frame_create(&f, g_con.port, 1280, 720, 1280, 720) != 0) { + fprintf(stderr, "fake frame setup failed\n"); + ++g_failures; + stop_console(); + return; + } + + modavd_open_params p; + memset(&p, 0, sizeof(p)); + p.size = sizeof(p); + p.console_port = g_con.port; + p.flags = MODAVD_OPEN_NO_START; + modavd_handle h = NULL; + CHECK_EQ(modavd_open(&p, &h), MODAVD_OK); + if (!h) { + fake_frame_destroy(&f); + stop_console(); + return; + } + CHECK(modavd_capabilities(h) & MODAVD_CAP_TOUCH); + CHECK_EQ(g_con.connections, 0); /* the session opens on the first touch */ + + /* tap at the centre: auth, down, up on one session */ + CHECK_EQ(modavd_touch_down(h, 0, 0, 640, 360, 0), MODAVD_OK); + CHECK_EQ(modavd_touch_up(h, 0, 0), MODAVD_OK); + CHECK_EQ(g_con.connections, 1); + CHECK_EQ(g_con.count, 3); + CHECK_STR(g_con.lines[0], "auth secret"); + CHECK_STR(g_con.lines[1], "event send 3:47:0 3:57:0 3:53:16383 3:54:16383 3:58:1024 3:48:1280 3:49:1280 0:0:0"); + CHECK_STR(g_con.lines[2], "event send 3:47:0 3:58:0 3:57:-1 0:0:0"); + + /* lifting it again sends nothing; moving a lifted contact is an error */ + CHECK_EQ(modavd_touch_up(h, 0, 0), MODAVD_OK); + CHECK_EQ(modavd_touch_move(h, 0, 0, 1, 1, 0), MODAVD_E_INVALID); + CHECK_EQ(g_con.count, 3); + + /* arguments */ + CHECK_EQ(modavd_touch_down(h, 0, MX_MAX_CONTACTS, 1, 1, 0), MODAVD_E_INVALID); + CHECK_EQ(modavd_touch_down(h, 0, -1, 1, 1, 0), MODAVD_E_INVALID); + CHECK_EQ(modavd_touch_down(h, 1, 0, 1, 1, 0), MODAVD_E_UNSUPPORTED); + CHECK_EQ(modavd_touch_down(NULL, 0, 0, 1, 1, 0), MODAVD_E_INVALID); + CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_E_UNSUPPORTED); + CHECK_EQ(modavd_input_text(h, "a"), MODAVD_E_UNSUPPORTED); + CHECK_EQ(g_con.count, 3); + + /* a swipe with its own pressure, clamped to the screen; pressing again moves */ + CHECK_EQ(modavd_touch_down(h, 0, 3, 100, 100, 500), MODAVD_OK); + CHECK_EQ(modavd_touch_move(h, 0, 3, 5000, 719, 0), MODAVD_OK); + CHECK_EQ(modavd_touch_down(h, 0, 3, 640, 360, 0), MODAVD_OK); + CHECK_STR(g_con.lines[3], "event send 3:47:3 3:57:3 3:53:2559 3:54:4550 3:58:500 3:48:1280 3:49:1280 0:0:0"); + CHECK_STR(g_con.lines[4], "event send 3:47:3 3:53:32741 3:54:32721 3:58:1024 0:0:0"); + CHECK_STR(g_con.lines[5], "event send 3:47:3 3:53:16383 3:54:16383 3:58:1024 0:0:0"); + + /* two fingers, then release all lifts both */ + CHECK_EQ(modavd_touch_down(h, 0, 1, 10, 10, 0), MODAVD_OK); + CHECK_EQ(modavd_touch_release_all(h), MODAVD_OK); + CHECK_EQ(g_con.count, 9); + CHECK_STR(g_con.lines[7], "event send 3:47:1 3:58:0 3:57:-1 0:0:0"); + CHECK_STR(g_con.lines[8], "event send 3:47:3 3:58:0 3:57:-1 0:0:0"); + + /* nothing published yet: no size to scale with */ + fake_frame_set_size(&f, 0, 0); + CHECK_EQ(modavd_touch_down(h, 0, 0, 1, 1, 0), MODAVD_E_NOT_READY); + fake_frame_set_size(&f, 1280, 720); + CHECK_EQ(g_con.count, 9); + + /* the console drops the session mid-command: reconnect once and resend */ + g_con.drop_next = 1; + CHECK_EQ(modavd_touch_down(h, 0, 4, 0, 0, 0), MODAVD_OK); + CHECK_EQ(g_con.connections, 2); + CHECK_EQ(g_con.count, 12); + CHECK_STR(g_con.lines[9], g_con.lines[11]); + CHECK_STR(g_con.lines[10], "auth secret"); + + /* closing lifts contact 4, then quits */ + modavd_close(h); + CHECK_EQ(wait_lines(14), 14); + CHECK_STR(g_con.lines[12], "event send 3:47:4 3:58:0 3:57:-1 0:0:0"); + CHECK_STR(g_con.lines[13], "quit"); + + fake_frame_destroy(&f); + stop_console(); +} + +int main(void) +{ +#ifdef _WIN32 + WSADATA wsa; + WSAStartup(MAKEWORD(2, 2), &wsa); +#endif + test_axis_and_commands(); + test_touch_session(); +#ifdef _WIN32 + WSACleanup(); +#endif + if (g_failures) { + fprintf(stderr, "%d check(s) failed\n", g_failures); + return 1; + } + printf("test_input: all passed\n"); + return 0; +} From 0b81b8286704c7efa38bdb59554113f0c3f99fc7 Mon Sep 17 00:00:00 2001 From: qiyinxi <52456734+qiyinxi@users.noreply.github.com> Date: Sat, 14 Nov 2026 11:09:35 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E6=8C=89=E9=94=AE=E5=92=8C=E6=96=87?= =?UTF-8?q?=E5=AD=97=EF=BC=9Amodavd=5Fkey=E3=80=81modavd=5Finput=5Ftext=20?= =?UTF-8?q?=E8=B5=B0=E6=A8=A1=E6=8B=9F=E5=99=A8=E6=8E=A7=E5=88=B6=E5=8F=B0?= =?UTF-8?q?=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 按键和文字:modavd_key、modavd_input_text 走模拟器控制台 文字:编辑框有焦点且宿主输入法在用时走 ime commit(任意语言),否则 ASCII 走 event text,每 4 个字符一条、间隔 4 ms(一次发多了客体键盘队列溢出,整段丢)。按键:控制台有 event key 时发真实按键;没有时只有能打出字符的键和编辑框里的编辑键可用,其余返回 MODAVD_E_UNSUPPORTED。接口版本 3,MODAVD_CAP_TEXT 恒有,MODAVD_CAP_KEY 看控制台有没有 event key。 * 按键表照模拟器键盘的 qwerty.kl:主页键是 102,Esc 键读作返回 模拟器的键盘在客体里用 /vendor/usr/keylayout/qwerty.kl,不是 Generic.kl。键盘上没有的键(FORWARD_DEL、MOVE_HOME 等)照没有 event key 时的办法走。带 event key 的模拟器上实测:返回、主页、多任务、Esc、退格、回车。 * README:按键和文字(接口版本 3) --- README.md | 7 +- include/modavd_extras.h | 33 +++- src/console.c | 12 +- src/core.c | 8 +- src/input.c | 367 ++++++++++++++++++++++++++++++++++++++-- src/mx_internal.h | 30 ++++ tests/test_core.c | 9 +- tests/test_input.c | 192 ++++++++++++++++++++- 8 files changed, 626 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 33d10a5..2a51e45 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,15 @@ - 遇到新帧写到一半时重拷; - 告诉调用方现在能不能用。 - 点击走模拟器控制台的 `event send`,发标准的多点触控事件(槽位、跟踪编号、坐标、压力),到达模拟器自己的虚拟触屏:安卓看到的是屏幕上的手指,和在模拟器窗口里点一样,不是应用注入的事件,客体里也不用装东西。最多 10 根手指。 +- 按键和文字也走控制台(接口版本 3): + - 按键(`modavd_key`,安卓 KEYCODE)按的是模拟器键盘上的键,返回、主页、多任务这些也在内。要模拟器的控制台有 `event key`(比 ModAVD 0.40 新的模拟器,`MODAVD_CAP_KEY`);更旧的模拟器上只有能打出字符的键和编辑框里的编辑键可用。 + - 文字(`modavd_input_text`,UTF-8):编辑框有焦点、ModAVD 的宿主输入法在用时,原样交给编辑框,任意语言;否则 ASCII 用键盘打。两样都不满足时返回 `MODAVD_E_UNSUPPORTED`,什么都不打。 - 库**不自己退回 adb**:出错时返回错误码,让调用方按自己的规则换别的办法。 - 只依赖系统库:Windows 上是 `KERNEL32`、`WS2_32`,C 运行库静态链接;POSIX 上是 `shm_open`、`mmap` 和套接字。 ## 状态 -- 截图(接口版本 1)和点击(接口版本 2,`MODAVD_CAP_TOUCH`)。按键、文字的函数已经声明,现在返回 `MODAVD_E_UNSUPPORTED`:控制台发的按键会进触屏设备、被丢掉,`event text` 会悄悄丢掉键盘映射里没有的字。 +- 截图(接口版本 1)、点击(接口版本 2,`MODAVD_CAP_TOUCH`)、按键和文字(接口版本 3,`MODAVD_CAP_KEY`、`MODAVD_CAP_TEXT`)。 - Windows:截图这套逻辑在 ModAVD 上已经用了一个多月,形式是下面的兼容外壳。 - macOS(Apple Silicon):11-09 有贡献者用官方 Emulator 37.2.13、API 35 ARM64 实测过截图。 - Linux:能编,单元测试在 CI 上跑;还没有在模拟器上实测。 @@ -112,7 +115,7 @@ ModAVDExtras is a small C library that reads the screen of a [ModAVD](https://gi - Frames come from the emulator's own shared memory: after `screenrecord webrtc start` (or `-share-vid`), the emulator writes every frame to a region named after its console port: `SHM_videmulator` on Windows, `videmulator` elsewhere. The layout is a 24-byte header (width, height, fps, frame number, timestamp) followed by BGRA pixels. - The library finds the console port from an adb address, starts frame sharing through the console when it is off, copies a frame in the format the caller asks for (copying again if a new frame arrived meanwhile), and reports whether capture is usable. It never falls back to adb by itself. - Touch (API version 2, `MODAVD_CAP_TOUCH`) goes through the console's `event send` as standard multi-touch events (slot, tracking id, position, pressure) to the emulator's own virtual touch screen: Android sees a finger on the screen, as with a click on the emulator window, not an injected event, and nothing is installed in the guest. Up to 10 contacts. -- Keys and text are declared and return `MODAVD_E_UNSUPPORTED`: keys sent through the console reach the touch screen device and are dropped, and `event text` silently drops characters missing from the keyboard map. See [include/modavd_extras.h](include/modavd_extras.h). +- Keys and text (API version 3) go through the console too. `modavd_key` presses keys of the emulator's keyboard by Android key code, BACK, HOME and APP_SWITCH included; it needs the console's `event key` (ModAVD emulators newer than 0.40, `MODAVD_CAP_KEY`), and on older emulators only keys that type a character and the editing keys of a focused text field work. `modavd_input_text` hands UTF-8 text in any language to the focused text field through ModAVD's host input method, or types ASCII on the keyboard when there is none. See [include/modavd_extras.h](include/modavd_extras.h). - `external_renderer_ipc.dll` is a Windows-only compatibility shell. It exports the four capture functions that MaaCore looks up in MuMu's DLL, on top of the same code, so MaaCore versions that do not know ModAVD can still capture from it. AUTO-MAS uses it today. It will be retired once MaaCore can connect to ModAVD directly. - Status: Windows has used this capture code for over a month through the shell below; on Apple Silicon macOS a contributor tested capture with the official Emulator 37.2.13 and API 35 ARM64; Linux builds and passes the unit tests in CI but has not been tested with an emulator. From v0.1.2 the [releases](https://github.com/ModAVD/ModAVDExtras/releases) carry `modavd_extras.dll` (Windows x64), `libmodavd_extras.dylib` (macOS arm64) and `libmodavd_extras.so` (Linux x64), plus the Windows-only shell. - Build: `build.ps1` on Windows (Visual Studio 2022 Build Tools, CMake, Ninja); CMake on Linux and macOS. The unit tests need no emulator. diff --git a/include/modavd_extras.h b/include/modavd_extras.h index c76c11b..e4cbb90 100644 --- a/include/modavd_extras.h +++ b/include/modavd_extras.h @@ -39,8 +39,8 @@ extern "C" { /* Bumped whenever a function or field is added, or a declared one starts to work. * Nothing is ever removed. - * 1: capture. 2: touch (modavd_touch_*). */ -#define MODAVD_API_VERSION 2 + * 1: capture. 2: touch (modavd_touch_*). 3: keys and text (modavd_key, modavd_input_text). */ +#define MODAVD_API_VERSION 3 typedef struct modavd_instance* modavd_handle; @@ -60,8 +60,8 @@ enum { enum { MODAVD_CAP_SCREENCAP = 1u << 0, MODAVD_CAP_TOUCH = 1u << 1, /* API version 2 */ - MODAVD_CAP_KEY = 1u << 2, /* not yet */ - MODAVD_CAP_TEXT = 1u << 3, /* not yet */ + MODAVD_CAP_KEY = 1u << 2, /* API version 3: this emulator takes every key of modavd_key() */ + MODAVD_CAP_TEXT = 1u << 3, /* API version 3 */ }; /* Pixel formats for modavd_capture(). Rows are packed: stride = width * bytes per pixel. */ @@ -178,10 +178,31 @@ MODAVD_API int modavd_touch_up(modavd_handle h, int display, int contact); /* Lifts every contact this handle pressed. modavd_close() does the same. */ MODAVD_API int modavd_touch_release_all(modavd_handle h); -/* ---- keys and text: declared, MODAVD_E_UNSUPPORTED for now ---- */ +/* ---- keys and text (API version 3) ---- */ -/* Android key code (KEYCODE_*); down = 1 press, 0 release. */ +/* Presses (down = 1) or releases (down = 0) a key of the emulator's keyboard, + * given as an Android key code (KEYCODE_*): Android sees a hardware key, not an + * injected event. Only display 0. + * + * With MODAVD_CAP_KEY (ModAVD emulators newer than 0.40) every key of that + * keyboard works: letters, digits, punctuation, ENTER, TAB, SPACE, DEL, the + * arrows, SHIFT / CTRL / ALT, and BACK, HOME, APP_SWITCH, MENU, SEARCH, POWER, + * the volume and media keys. ESCAPE is its Esc key, which Android reads as BACK. + * Without it, and for keys the keyboard lacks, only these work, and the press + * sends both press and release: + * - keys that type a character (letters, digits, ENTER, TAB, SPACE, punctuation); + * - the editing keys (DEL, FORWARD_DEL, the arrows, MOVE_HOME, MOVE_END) while + * a text field has the focus and ModAVD's host input method is the current one. + * MODAVD_E_UNSUPPORTED: this emulator cannot send that key now; use another way. */ MODAVD_API int modavd_key(modavd_handle h, int display, int android_keycode, int down); + +/* Types UTF-8 text into what has the focus. '\n' is ENTER; other control + * characters except '\t' are MODAVD_E_INVALID. + * - While a text field has the focus and ModAVD's host input method + * (org.modavd.ime, installed by ModAVD) is the current one, the text goes + * to the field as it is, in any language. + * - Otherwise printable ASCII is typed on the emulator's keyboard (a US layout). + * MODAVD_E_UNSUPPORTED: text beyond ASCII and no such text field; nothing was typed. */ MODAVD_API int modavd_input_text(modavd_handle h, const char* utf8); #ifdef __cplusplus diff --git a/src/console.c b/src/console.c index 2acb65c..69ff3ca 100644 --- a/src/console.c +++ b/src/console.c @@ -90,7 +90,7 @@ static int read_until_ok(mx_socket s, char* buf, int cap) static int send_line(mx_socket s, const char* line) { - char cmd[512]; + char cmd[MX_CONSOLE_LINE_MAX + 3]; int n = snprintf(cmd, sizeof(cmd), "%s\r\n", line); if (n <= 0 || (size_t)n >= sizeof(cmd)) { return -1; @@ -162,8 +162,11 @@ int mx_console_open(int port, int timeout_ms, mx_console* c) return MODAVD_OK; } -int mx_console_send(mx_console* c, const char* command) +int mx_console_query(mx_console* c, const char* command, char* reply, size_t cap) { + if (cap) { + reply[0] = '\0'; + } if (c->sock == (intptr_t)MX_BAD_SOCKET) { return MODAVD_E_NOT_FOUND; } @@ -179,9 +182,14 @@ int mx_console_send(mx_console* c, const char* command) mx_console_close(c); return MODAVD_E_NOT_FOUND; } + if (cap) { + snprintf(reply, cap, "%s", buf); + } return r == 1 ? MODAVD_OK : MODAVD_E_IO; } +int mx_console_send(mx_console* c, const char* command) { return mx_console_query(c, command, NULL, 0); } + void mx_console_close(mx_console* c) { if (c->sock == (intptr_t)MX_BAD_SOCKET) { diff --git a/src/core.c b/src/core.c index e2039e2..b4e0788 100644 --- a/src/core.c +++ b/src/core.c @@ -162,7 +162,13 @@ void modavd_close(modavd_handle h) free(h); } -uint32_t modavd_capabilities(modavd_handle h) { return h ? MODAVD_CAP_SCREENCAP | MODAVD_CAP_TOUCH : 0; } +uint32_t modavd_capabilities(modavd_handle h) +{ + if (!h) { + return 0; + } + return MODAVD_CAP_SCREENCAP | MODAVD_CAP_TOUCH | MODAVD_CAP_TEXT | mx_input_capabilities(h->input); +} int modavd_console_port(modavd_handle h) { return h ? h->console_port : MODAVD_E_INVALID; } diff --git a/src/input.c b/src/input.c index a305e68..29cdf6b 100644 --- a/src/input.c +++ b/src/input.c @@ -1,5 +1,5 @@ /* - * Touch input through the emulator console: "event send" with raw multi-touch + * Input through the emulator console. Touch: "event send" with raw multi-touch * events (protocol B: slot, tracking id, position, pressure), which the * emulator hands to the virtio touch screen of display 0 as they are. They * reach Android as touches on that screen, like a finger on the window, not @@ -24,6 +24,7 @@ typedef SRWLOCK mx_mutex; #define mx_mutex_unlock(m) ReleaseSRWLockExclusive(m) #else #include +#include typedef pthread_mutex_t mx_mutex; #define mx_mutex_init(m) pthread_mutex_init((m), NULL) #define mx_mutex_destroy(m) pthread_mutex_destroy(m) @@ -49,6 +50,7 @@ struct mx_input { int port; mx_console console; uint32_t down; /* bit n: contact n is pressed */ + int key_cmd; /* the console of this session has "event key": 1 yes, -1 no, 0 not asked */ }; int mx_touch_axis(int v, int size) @@ -102,7 +104,7 @@ struct mx_input* mx_input_create(int console_port) /* Sends one command on the session, opening it first and once more if it broke * (the emulator restarted, or closed an idle session). Caller holds the lock. */ -static int send_locked(struct mx_input* in, const char* command) +static int query_locked(struct mx_input* in, const char* command, char* reply, size_t cap) { for (int attempt = 0; attempt < 2; ++attempt) { if (in->console.sock == -1) { @@ -110,18 +112,25 @@ static int send_locked(struct mx_input* in, const char* command) if (rc != MODAVD_OK) { return rc; } + in->key_cmd = 0; /* maybe another emulator now */ } - int rc = mx_console_send(&in->console, command); + int rc = mx_console_query(&in->console, command, reply, cap); if (rc != MODAVD_E_NOT_FOUND) { - if (rc != MODAVD_OK) { - mx_log(MODAVD_LOG_WARN, "console %d: %s -> KO", in->port, command); - } return rc; } } return MODAVD_E_NOT_FOUND; } +static int send_locked(struct mx_input* in, const char* command) +{ + int rc = query_locked(in, command, NULL, 0); + if (rc == MODAVD_E_IO) { + mx_log(MODAVD_LOG_WARN, "console %d: %s -> KO", in->port, command); + } + return rc; +} + /* Lifts every pressed contact. Caller holds the lock. */ static int release_all_locked(struct mx_input* in) { @@ -242,17 +251,349 @@ int modavd_touch_release_all(modavd_handle h) return rc; } -/* The console's "event send" delivers keys to the touch screen device, which - * drops them, and "event text" silently drops what the keyboard map lacks: - * neither is reliable enough yet. */ +/* ---- keys and text ---- + * + * Three console commands carry them: + * - "event key <0|1>" presses or releases a key of the emulator's + * keyboard. ModAVD emulators newer than 0.40 have it; it is the only way to + * BACK, HOME and the other keys that type nothing. + * - "event text " types on that keyboard, every emulator has it. It can + * type printable ASCII, and drops a whole burst when more keys arrive than + * the guest keyboard's queue holds, so the text goes a few characters at a + * time. + * - "ime commit " / "ime key " hand text and editing keys to the + * focused text field through the host input method (org.modavd.ime, ModAVD + * 0.35.0 and later). Any language; needs that input method to be the + * current one and a text field to have the focus ("ime status"). + */ + +/* The emulator's keyboard, as the guest reads it (/vendor/usr/keylayout/qwerty.kl of the + * goldfish images): the Android key code, the Linux code of its key, and the character + * it types unshifted. Generic.kl does not apply to it: HOME is key 102 here. */ +static const struct { + short android; + short linux_code; + char ch; +} kKeys[] = { + { 1, 60, 0 }, /* SOFT_LEFT */ + { 2, 230, 0 }, /* SOFT_RIGHT */ + { 3, 102, 0 }, /* HOME */ + { 4, 158, 0 }, /* BACK */ + { 5, 231, 0 }, /* CALL */ + { 6, 107, 0 }, /* ENDCALL */ + { 7, 11, '0' }, { 8, 2, '1' }, { 9, 3, '2' }, { 10, 4, '3' }, { 11, 5, '4' }, + { 12, 6, '5' }, { 13, 7, '6' }, { 14, 8, '7' }, { 15, 9, '8' }, { 16, 10, '9' }, + { 17, 227, 0 }, /* STAR */ + { 18, 228, 0 }, /* POUND */ + { 19, 103, 0 }, /* DPAD_UP */ + { 20, 108, 0 }, /* DPAD_DOWN */ + { 21, 105, 0 }, /* DPAD_LEFT */ + { 22, 106, 0 }, /* DPAD_RIGHT */ + { 23, 232, 0 }, /* DPAD_CENTER */ + { 24, 115, 0 }, /* VOLUME_UP */ + { 25, 114, 0 }, /* VOLUME_DOWN */ + { 26, 116, 0 }, /* POWER */ + { 27, 212, 0 }, /* CAMERA */ + { 29, 30, 'a' }, { 30, 48, 'b' }, { 31, 46, 'c' }, { 32, 32, 'd' }, { 33, 18, 'e' }, + { 34, 33, 'f' }, { 35, 34, 'g' }, { 36, 35, 'h' }, { 37, 23, 'i' }, { 38, 36, 'j' }, + { 39, 37, 'k' }, { 40, 38, 'l' }, { 41, 50, 'm' }, { 42, 49, 'n' }, { 43, 24, 'o' }, + { 44, 25, 'p' }, { 45, 16, 'q' }, { 46, 19, 'r' }, { 47, 31, 's' }, { 48, 20, 't' }, + { 49, 22, 'u' }, { 50, 47, 'v' }, { 51, 17, 'w' }, { 52, 45, 'x' }, { 53, 21, 'y' }, + { 54, 44, 'z' }, + { 55, 51, ',' }, { 56, 52, '.' }, + { 57, 56, 0 }, /* ALT_LEFT */ + { 58, 100, 0 }, /* ALT_RIGHT */ + { 59, 42, 0 }, /* SHIFT_LEFT */ + { 60, 54, 0 }, /* SHIFT_RIGHT */ + { 61, 15, '\t' }, { 62, 57, ' ' }, + { 64, 150, 0 }, /* EXPLORER */ + { 65, 155, 0 }, /* ENVELOPE */ + { 66, 28, '\n' }, + { 67, 14, 0 }, /* DEL (backspace) */ + { 68, 399, '`' }, { 69, 12, '-' }, { 70, 13, '=' }, { 71, 26, '[' }, { 72, 27, ']' }, + { 73, 43, '\\' }, { 74, 39, ';' }, { 75, 40, '\'' }, { 76, 53, '/' }, + { 77, 215, 0 }, /* AT */ + { 82, 139, 0 }, /* MENU */ + { 84, 217, 0 }, /* SEARCH */ + { 85, 164, 0 }, /* MEDIA_PLAY_PAUSE */ + { 86, 166, 0 }, /* MEDIA_STOP */ + { 87, 163, 0 }, /* MEDIA_NEXT */ + { 88, 165, 0 }, /* MEDIA_PREVIOUS */ + { 89, 168, 0 }, /* MEDIA_REWIND */ + { 111, 1, 0 }, /* ESCAPE: the keyboard's Esc key, which this layout reads as BACK */ + { 113, 29, 0 }, /* CTRL_LEFT */ + { 114, 97, 0 }, /* CTRL_RIGHT */ + { 115, 58, 0 }, /* CAPS_LOCK */ + { 187, 580, 0 }, /* APP_SWITCH */ + { 223, 142, 0 }, /* SLEEP */ +}; + +int mx_key_linux_code(int android_keycode) +{ + for (size_t i = 0; i < sizeof(kKeys) / sizeof(kKeys[0]); ++i) { + if (kKeys[i].android == android_keycode) { + return kKeys[i].linux_code; + } + } + return 0; +} + +int mx_key_char(int android_keycode) +{ + for (size_t i = 0; i < sizeof(kKeys) / sizeof(kKeys[0]); ++i) { + if (kKeys[i].android == android_keycode) { + return kKeys[i].ch; + } + } + return 0; +} + +/* Keys the host input method may pass to the focused text field on an emulator + * without "event key": the editing keys. The others either type a character + * (the keyboard does that) or mean nothing to a text field. */ +static int is_editing_key(int android_keycode) +{ + switch (android_keycode) { + case 19: case 20: case 21: case 22: /* DPAD */ + case 67: /* DEL */ + case 112: /* FORWARD_DEL */ + case 122: case 123: /* MOVE_HOME, MOVE_END */ + return 1; + default: return 0; + } +} + +int mx_text_is_keyboard(const char* utf8) +{ + for (const unsigned char* p = (const unsigned char*)utf8; *p; ++p) { + if (!((*p >= 0x20 && *p <= 0x7e) || *p == '\n' || *p == '\t')) { + return 0; + } + } + return 1; +} + +int mx_text_keyboard_command(char* buf, size_t cap, const char** text) +{ + const char* p = *text; + if (!*p) { + return 0; + } + int n = snprintf(buf, cap, "event text "); + if (n < 0 || (size_t)n >= cap) { + return -1; + } + for (int taken = 0; *p && taken < MX_KEYBOARD_CHUNK; ++p, ++taken) { + /* The console skips leading spaces and reads '\' as an escape. */ + const char* fmt = "%c"; + if (*p == ' ' || *p == '\t') { + fmt = "\\x%02x"; + } + else if (*p == '\n') { + fmt = "\\n"; + } + else if (*p == '\\') { + fmt = "\\\\"; + } + int w = snprintf(buf + n, cap - (size_t)n, fmt, (unsigned char)*p); + if (w < 0 || (size_t)w >= cap - (size_t)n) { + return -1; + } + n += w; + } + *text = p; + return n; +} + +int mx_text_commit_command(char* buf, size_t cap, const char** text, int* newline) +{ + const char* p = *text; + size_t len = 0; + *newline = 0; + while (p[len] && p[len] != '\n' && len < MX_COMMIT_CHUNK) { + ++len; + } + if (p[len] && p[len] != '\n') { + /* Cut in front of a UTF-8 sequence, not inside one. */ + while (len > 0 && ((unsigned char)p[len] & 0xc0) == 0x80) { + --len; + } + } + int n = 0; + if (len > 0) { + n = snprintf(buf, cap, "ime commit %.*s", (int)len, p); + if (n < 0 || (size_t)n >= cap) { + return -1; + } + } + if (p[len] == '\n') { + *newline = 1; + ++len; + } + *text = p + len; + return n; +} + +static void sleep_ms(int ms) +{ +#ifdef _WIN32 + Sleep((DWORD)ms); +#else + struct timespec ts = { ms / 1000, (long)(ms % 1000) * 1000000L }; + nanosleep(&ts, NULL); +#endif +} + +/* 1 when the console has "event key" (asked once per session), 0 when not, or a MODAVD_E_* code. */ +static int key_command_locked(struct mx_input* in) +{ + if (in->console.sock != -1 && in->key_cmd != 0) { + return in->key_cmd > 0; + } + /* Without arguments it presses nothing: the usage if the command is there, "bad sub-command" if not. */ + char reply[512]; + int rc = query_locked(in, "event key", reply, sizeof(reply)); + if (rc == MODAVD_E_NOT_FOUND) { + return rc; + } + in->key_cmd = strstr(reply, "usage: event key") ? 1 : -1; + return in->key_cmd > 0; +} + +/* 1 when the host input method is connected and a text field has the focus, 0 when not, or a MODAVD_E_* code. */ +static int ime_ready_locked(struct mx_input* in) +{ + char reply[512]; + int rc = query_locked(in, "ime status", reply, sizeof(reply)); + if (rc == MODAVD_E_NOT_FOUND) { + return rc; + } + return rc == MODAVD_OK && strstr(reply, "connected=1") && strstr(reply, "active=1"); +} + +uint32_t mx_input_capabilities(struct mx_input* in) +{ + mx_mutex_lock(&in->lock); + int has = key_command_locked(in); + mx_mutex_unlock(&in->lock); + return has > 0 ? MODAVD_CAP_KEY : 0; +} + int modavd_key(modavd_handle h, int display, int android_keycode, int down) { - (void)h, (void)display, (void)android_keycode, (void)down; - return MODAVD_E_UNSUPPORTED; + if (!h || !h->input || android_keycode <= 0) { + return MODAVD_E_INVALID; + } + if (display != 0) { + return MODAVD_E_UNSUPPORTED; + } + struct mx_input* in = h->input; + char cmd[64]; + int rc; + + mx_mutex_lock(&in->lock); + int has = key_command_locked(in); + if (has < 0) { + rc = has; + } + else if (has && mx_key_linux_code(android_keycode)) { + snprintf(cmd, sizeof(cmd), "event key %d %d", mx_key_linux_code(android_keycode), down ? 1 : 0); + rc = send_locked(in, cmd); + } + else { + /* No "event key", or a key the keyboard lacks: the press types the + * key's character or goes to the focused text field as a whole; the + * release has nothing left to do. */ + int ch = mx_key_char(android_keycode); + if (ch) { + const char one[2] = { (char)ch, '\0' }; + const char* p = one; + rc = MODAVD_OK; + if (down && mx_text_keyboard_command(cmd, sizeof(cmd), &p) > 0) { + rc = send_locked(in, cmd); + } + } + else if (is_editing_key(android_keycode)) { + int ready = ime_ready_locked(in); + if (ready < 0) { + rc = ready; + } + else if (!ready) { + rc = MODAVD_E_UNSUPPORTED; + } + else if (down) { + snprintf(cmd, sizeof(cmd), "ime key %d", android_keycode); + rc = send_locked(in, cmd); + } + else { + rc = MODAVD_OK; + } + } + else { + rc = MODAVD_E_UNSUPPORTED; + } + } + mx_mutex_unlock(&in->lock); + return rc; } int modavd_input_text(modavd_handle h, const char* utf8) { - (void)h, (void)utf8; - return MODAVD_E_UNSUPPORTED; + if (!h || !h->input || !utf8) { + return MODAVD_E_INVALID; + } + for (const unsigned char* p = (const unsigned char*)utf8; *p; ++p) { + if (*p < 0x20 && *p != '\n' && *p != '\t') { + return MODAVD_E_INVALID; /* the console reads a line; other control characters have no key */ + } + } + if (!utf8[0]) { + return MODAVD_OK; + } + struct mx_input* in = h->input; + char cmd[MX_CONSOLE_LINE_MAX + 1]; + const char* p = utf8; + int rc = MODAVD_OK; + + mx_mutex_lock(&in->lock); + int ready = ime_ready_locked(in); + if (ready < 0) { + rc = ready; + } + else if (ready) { + /* The input method hands the text to the field as it is. */ + while (*p && rc == MODAVD_OK) { + int newline = 0; + int n = mx_text_commit_command(cmd, sizeof(cmd), &p, &newline); + if (n < 0) { + rc = MODAVD_E_INVALID; + break; + } + if (n > 0) { + rc = send_locked(in, cmd); + } + if (rc == MODAVD_OK && newline) { + rc = send_locked(in, "ime key 66"); /* ENTER */ + } + } + } + else if (mx_text_is_keyboard(utf8)) { + for (;;) { + int n = mx_text_keyboard_command(cmd, sizeof(cmd), &p); + if (n <= 0) { + rc = n < 0 ? MODAVD_E_INVALID : MODAVD_OK; + break; + } + rc = send_locked(in, cmd); + if (rc != MODAVD_OK) { + break; + } + sleep_ms(MX_KEYBOARD_GAP_MS); + } + } + else { + rc = MODAVD_E_UNSUPPORTED; /* no text field with the host input method, and not ASCII */ + } + mx_mutex_unlock(&in->lock); + return rc; } diff --git a/src/mx_internal.h b/src/mx_internal.h index 73be13e..c1e9a4f 100644 --- a/src/mx_internal.h +++ b/src/mx_internal.h @@ -85,9 +85,15 @@ int mx_console_open(int port, int timeout_ms, mx_console* c); /* MODAVD_OK, MODAVD_E_IO (the console answered KO) or MODAVD_E_NOT_FOUND * (the session broke; it is closed and may be opened again). */ int mx_console_send(mx_console* c, const char* command); +/* mx_console_send() that also copies what the console answered (the lines up + * to and including OK / KO, truncated to cap) into reply. */ +int mx_console_query(mx_console* c, const char* command, char* reply, size_t cap); /* Says quit and closes; nothing happens when already closed. */ void mx_console_close(mx_console* c); +/* The longest command line a session sends, without the line end. */ +#define MX_CONSOLE_LINE_MAX 768 + /* ---- input.c ---- */ #define MX_MAX_CONTACTS 10 #define MX_ABS_MAX 32767 /* the virtio touch screen's axis range (INPUT_EVENT_ABS_MAX) */ @@ -103,7 +109,31 @@ int mx_touch_axis(int v, int size); * axis coordinates ax, ay. Returns the length, or -1 if it does not fit. */ int mx_touch_command(char* buf, size_t cap, int action, int contact, int ax, int ay, int pressure); +/* ---- input.c: keys and text ---- */ +#define MX_KEYBOARD_CHUNK 4 /* characters per "event text": a longer burst overruns the guest keyboard's queue */ +#define MX_KEYBOARD_GAP_MS 4 /* pause after each of them, for the guest to read the keys */ +#define MX_COMMIT_CHUNK 480 /* bytes of text per "ime commit" */ + +/* The Linux input code (KEY_*) of an Android key code (KEYCODE_*), 0 if the + * emulator's keyboard has no such key. */ +int mx_key_linux_code(int android_keycode); +/* The character an Android key code types unshifted ('a', '1', '\n', ...), 0 if none. */ +int mx_key_char(int android_keycode); +/* 1 when the keyboard can type all of text: printable ASCII, '\n' and '\t'. */ +int mx_text_is_keyboard(const char* utf8); +/* Writes "event text ..." for the next characters of *text (at most + * MX_KEYBOARD_CHUNK) and advances *text. Returns the length, 0 at the end of + * the text, -1 if it does not fit. */ +int mx_text_keyboard_command(char* buf, size_t cap, const char** text); +/* Writes "ime commit ..." for the next bytes of *text, up to a line end or + * MX_COMMIT_CHUNK bytes (whole UTF-8 sequences), and advances *text; a line + * end is taken with it and *newline set to 1. Returns the length (0 when only + * a line end was taken, or at the end of the text), -1 if it does not fit. */ +int mx_text_commit_command(char* buf, size_t cap, const char** text, int* newline); + struct mx_input; /* lock, console session and pressed contacts of one handle */ +/* MODAVD_CAP_KEY when the console has "event key", else 0. Asks the console once per session. */ +uint32_t mx_input_capabilities(struct mx_input* in); struct mx_input* mx_input_create(int console_port); /* Lifts the contacts still down, closes the session and frees. */ void mx_input_destroy(struct mx_input* in); diff --git a/tests/test_core.c b/tests/test_core.c index 05b8ec1..509c926 100644 --- a/tests/test_core.c +++ b/tests/test_core.c @@ -157,7 +157,8 @@ static void test_capture_from_fake_frame(void) return; } CHECK_EQ(modavd_console_port(h), port); - CHECK_EQ(modavd_capabilities(h), MODAVD_CAP_SCREENCAP | MODAVD_CAP_TOUCH); + /* no console answers on this port, so nobody says it has "event key" */ + CHECK_EQ(modavd_capabilities(h), MODAVD_CAP_SCREENCAP | MODAVD_CAP_TOUCH | MODAVD_CAP_TEXT); check_capture(h, MODAVD_FMT_BGRA, 4, 0, w, hgt); check_capture(h, MODAVD_FMT_BGR, 3, 0, w, hgt); @@ -196,9 +197,9 @@ static void test_capture_from_fake_frame(void) /* touch goes through the console, and nothing answers on this port (test_input covers a fake one) */ CHECK_EQ(modavd_touch_down(h, 0, 0, 1, 1, 1), MODAVD_E_NOT_FOUND); CHECK_EQ(modavd_touch_release_all(h), MODAVD_OK); - /* keys and text are not in this version */ - CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_E_UNSUPPORTED); - CHECK_EQ(modavd_input_text(h, "a"), MODAVD_E_UNSUPPORTED); + /* so do keys and text */ + CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_E_NOT_FOUND); + CHECK_EQ(modavd_input_text(h, "a"), MODAVD_E_NOT_FOUND); modavd_close(h); modavd_close(NULL); diff --git a/tests/test_input.c b/tests/test_input.c index 0319599..3dd1bd0 100644 --- a/tests/test_input.c +++ b/tests/test_input.c @@ -83,6 +83,8 @@ static struct { volatile int connections; volatile int drop_next; /* close without replying to the next command */ volatile int stop; + volatile int event_key; /* the console has "event key" */ + volatile int ime_active; /* "ime status": the host input method is connected and a text field has the focus */ } g_con; static void record(const char* line) @@ -127,7 +129,19 @@ static void serve_client(sock_t c) g_con.drop_next = 0; return; } - send(c, "OK\r\n", 4, 0); + const char* reply = "OK\r\n"; + const char* last = g_con.lines[g_con.count - 1]; + if (strncmp(last, "event key", 9) == 0 && !g_con.event_key) { + reply = "allows you to send fake hardware events to the kernel\r\nKO: bad sub-command\r\n"; + } + else if (strcmp(last, "event key") == 0) { + reply = "KO: usage: event key <0|1>\r\n"; + } + else if (strcmp(last, "ime status") == 0) { + reply = g_con.ime_active ? "connected=1\r\nversion=1\r\ntextmode=0\r\nactive=1\r\nOK\r\n" + : "connected=1\r\nversion=1\r\ntextmode=0\r\nactive=0\r\nOK\r\n"; + } + send(c, reply, (int)strlen(reply), 0); } } } @@ -156,6 +170,7 @@ static void* server_main(void* arg) static int start_console(void) { + memset(&g_con, 0, sizeof(g_con)); g_con.listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (g_con.listener == BAD_SOCK) { return -1; @@ -290,7 +305,6 @@ static void test_touch_session(void) stop_console(); return; } - CHECK(modavd_capabilities(h) & MODAVD_CAP_TOUCH); CHECK_EQ(g_con.connections, 0); /* the session opens on the first touch */ /* tap at the centre: auth, down, up on one session */ @@ -312,8 +326,6 @@ static void test_touch_session(void) CHECK_EQ(modavd_touch_down(h, 0, -1, 1, 1, 0), MODAVD_E_INVALID); CHECK_EQ(modavd_touch_down(h, 1, 0, 1, 1, 0), MODAVD_E_UNSUPPORTED); CHECK_EQ(modavd_touch_down(NULL, 0, 0, 1, 1, 0), MODAVD_E_INVALID); - CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_E_UNSUPPORTED); - CHECK_EQ(modavd_input_text(h, "a"), MODAVD_E_UNSUPPORTED); CHECK_EQ(g_con.count, 3); /* a swipe with its own pressure, clamped to the screen; pressing again moves */ @@ -355,6 +367,175 @@ static void test_touch_session(void) stop_console(); } +static void test_key_and_text_commands(void) +{ + CHECK_EQ(mx_key_linux_code(4), 158); /* BACK */ + CHECK_EQ(mx_key_linux_code(3), 102); /* HOME: the goldfish keyboard layout, not Generic.kl */ + CHECK_EQ(mx_key_linux_code(66), 28); /* ENTER */ + CHECK_EQ(mx_key_linux_code(29), 30); /* A */ + CHECK_EQ(mx_key_linux_code(187), 580); /* APP_SWITCH */ + CHECK_EQ(mx_key_linux_code(111), 1); /* ESCAPE: the Esc key */ + CHECK_EQ(mx_key_linux_code(112), 0); /* FORWARD_DEL: not on that keyboard */ + CHECK_EQ(mx_key_linux_code(9999), 0); + CHECK_EQ(mx_key_char(29), 'a'); + CHECK_EQ(mx_key_char(7), '0'); + CHECK_EQ(mx_key_char(66), '\n'); + CHECK_EQ(mx_key_char(4), 0); + + CHECK(mx_text_is_keyboard("Ab1 ~\n\t")); + CHECK(!mx_text_is_keyboard("caf\xc3\xa9")); + + /* a few characters per command; spaces, tabs, line ends and '\' escaped */ + char buf[64]; + const char* p = "ab cd\\\n\te"; + CHECK(mx_text_keyboard_command(buf, sizeof(buf), &p) > 0); + CHECK_STR(buf, "event text ab\\x20c"); + CHECK(mx_text_keyboard_command(buf, sizeof(buf), &p) > 0); + CHECK_STR(buf, "event text d\\\\\\n\\x09"); + CHECK(mx_text_keyboard_command(buf, sizeof(buf), &p) > 0); + CHECK_STR(buf, "event text e"); + CHECK_EQ(mx_text_keyboard_command(buf, sizeof(buf), &p), 0); + p = "abcd"; + CHECK_EQ(mx_text_keyboard_command(buf, 12, &p), -1); + + /* commits stop at a line end, which is taken with them */ + char big[MX_CONSOLE_LINE_MAX + 1]; + int newline = 0; + p = "\xe4\xb8\xad\xe6\x96\x87 a\n\nb"; + CHECK(mx_text_commit_command(big, sizeof(big), &p, &newline) > 0); + CHECK_STR(big, "ime commit \xe4\xb8\xad\xe6\x96\x87 a"); + CHECK_EQ(newline, 1); + CHECK_EQ(mx_text_commit_command(big, sizeof(big), &p, &newline), 0); + CHECK_EQ(newline, 1); + CHECK(mx_text_commit_command(big, sizeof(big), &p, &newline) > 0); + CHECK_STR(big, "ime commit b"); + CHECK_EQ(newline, 0); + CHECK_EQ(*p, '\0'); + + /* a long text is cut between characters, never inside one */ + char longtext[MX_COMMIT_CHUNK + 8]; + memset(longtext, 'x', MX_COMMIT_CHUNK - 1); + memcpy(longtext + MX_COMMIT_CHUNK - 1, "\xe4\xb8\xad!", 5); + p = longtext; + CHECK_EQ(mx_text_commit_command(big, sizeof(big), &p, &newline), 11 + MX_COMMIT_CHUNK - 1); + CHECK_EQ(newline, 0); + CHECK(mx_text_commit_command(big, sizeof(big), &p, &newline) > 0); + CHECK_STR(big, "ime commit \xe4\xb8\xad!"); +} + +/* Opens a handle on the fake console and frame; NULL (and a failure counted) if that fails. */ +static modavd_handle open_fake(fake_frame* f) +{ + if (start_console() != 0 || use_auth_token() != 0) { + fprintf(stderr, "fake console setup failed\n"); + ++g_failures; + return NULL; + } + if (fake_frame_create(f, g_con.port, 1280, 720, 1280, 720) != 0) { + fprintf(stderr, "fake frame setup failed\n"); + ++g_failures; + stop_console(); + return NULL; + } + modavd_open_params p; + memset(&p, 0, sizeof(p)); + p.size = sizeof(p); + p.console_port = g_con.port; + p.flags = MODAVD_OPEN_NO_START; + modavd_handle h = NULL; + CHECK_EQ(modavd_open(&p, &h), MODAVD_OK); + if (!h) { + fake_frame_destroy(f); + stop_console(); + } + return h; +} + +/* An emulator without "event key": characters through the keyboard, editing keys and other text through the + * input method while a text field has the focus. */ +static void test_keys_and_text_old_emulator(void) +{ + fake_frame f; + modavd_handle h = open_fake(&f); + if (!h) { + return; + } + uint32_t caps = modavd_capabilities(h); + CHECK(caps & MODAVD_CAP_TOUCH); + CHECK(caps & MODAVD_CAP_TEXT); + CHECK(!(caps & MODAVD_CAP_KEY)); + CHECK_STR(g_con.lines[1], "event key"); /* asked once */ + CHECK_EQ(g_con.count, 2); + + /* no text field: a character key types, BACK and DEL cannot be sent */ + CHECK_EQ(modavd_key(h, 0, 66, 1), MODAVD_OK); + CHECK_STR(g_con.lines[2], "event text \\n"); + CHECK_EQ(modavd_key(h, 0, 66, 0), MODAVD_OK); /* the press already released it */ + CHECK_EQ(g_con.count, 3); + CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_E_UNSUPPORTED); + CHECK_EQ(g_con.count, 3); + CHECK_EQ(modavd_key(h, 0, 67, 1), MODAVD_E_UNSUPPORTED); + CHECK_STR(g_con.lines[3], "ime status"); + CHECK_EQ(modavd_key(h, 1, 66, 1), MODAVD_E_UNSUPPORTED); + CHECK_EQ(modavd_key(h, 0, 0, 1), MODAVD_E_INVALID); + CHECK_EQ(g_con.count, 4); + + /* no text field: ASCII goes to the keyboard in small bursts, other text nowhere */ + CHECK_EQ(modavd_input_text(h, "hello you"), MODAVD_OK); + CHECK_STR(g_con.lines[4], "ime status"); + CHECK_STR(g_con.lines[5], "event text hell"); + CHECK_STR(g_con.lines[6], "event text o\\x20yo"); + CHECK_STR(g_con.lines[7], "event text u"); + CHECK_EQ(modavd_input_text(h, "\xe4\xb8\xad"), MODAVD_E_UNSUPPORTED); + CHECK_STR(g_con.lines[8], "ime status"); + CHECK_EQ(g_con.count, 9); + CHECK_EQ(modavd_input_text(h, "a\x01"), MODAVD_E_INVALID); + CHECK_EQ(modavd_input_text(h, NULL), MODAVD_E_INVALID); + CHECK_EQ(modavd_input_text(h, ""), MODAVD_OK); + CHECK_EQ(g_con.count, 9); + + /* a text field has the focus: the input method takes the text and the editing keys */ + g_con.ime_active = 1; + CHECK_EQ(modavd_input_text(h, "\xe4\xb8\xad a\nb"), MODAVD_OK); + CHECK_STR(g_con.lines[10], "ime commit \xe4\xb8\xad a"); + CHECK_STR(g_con.lines[11], "ime key 66"); + CHECK_STR(g_con.lines[12], "ime commit b"); + CHECK_EQ(modavd_key(h, 0, 67, 1), MODAVD_OK); + CHECK_STR(g_con.lines[14], "ime key 67"); + CHECK_EQ(modavd_key(h, 0, 67, 0), MODAVD_OK); + CHECK_EQ(g_con.count, 16); /* the release asked for the status and sent nothing */ + + modavd_close(h); + fake_frame_destroy(&f); + stop_console(); +} + +/* An emulator with "event key": every key is a real press and release. */ +static void test_keys_new_emulator(void) +{ + fake_frame f; + modavd_handle h = open_fake(&f); + if (!h) { + return; + } + g_con.event_key = 1; + CHECK_EQ(modavd_key(h, 0, 4, 1), MODAVD_OK); + CHECK_EQ(modavd_key(h, 0, 4, 0), MODAVD_OK); + CHECK_STR(g_con.lines[1], "event key"); + CHECK_STR(g_con.lines[2], "event key 158 1"); + CHECK_STR(g_con.lines[3], "event key 158 0"); + CHECK_EQ(modavd_key(h, 0, 66, 1), MODAVD_OK); + CHECK_STR(g_con.lines[4], "event key 28 1"); + CHECK_EQ(modavd_key(h, 0, 9999, 1), MODAVD_E_UNSUPPORTED); + CHECK_EQ(g_con.count, 5); + CHECK(modavd_capabilities(h) & MODAVD_CAP_KEY); + CHECK_EQ(g_con.count, 5); /* not asked again on this session */ + + modavd_close(h); + fake_frame_destroy(&f); + stop_console(); +} + int main(void) { #ifdef _WIN32 @@ -363,6 +544,9 @@ int main(void) #endif test_axis_and_commands(); test_touch_session(); + test_key_and_text_commands(); + test_keys_and_text_old_emulator(); + test_keys_new_emulator(); #ifdef _WIN32 WSACleanup(); #endif From 8cf3fb33c6912ccfe2959be17dbe56957eea5995 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 13:04:01 +0100 Subject: [PATCH 3/5] v0.3.0: version --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c92f3ea..a7d96af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) # macOS: the oldest release the library loads on. Without it the build machine's SDK version is used. set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "Minimum macOS version") -project(ModAVDExtras VERSION 0.2.0 LANGUAGES C) +project(ModAVDExtras VERSION 0.3.0 LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) From 358887a306d49e5c1ec2ecc95f0e4c5b0aa500a0 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 13:05:35 +0100 Subject: [PATCH 4/5] v0.3.0: release notes and checksums --- release/0.3.0.SHA256SUMS.txt | 4 +++ release/0.3.0.md | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 release/0.3.0.SHA256SUMS.txt create mode 100644 release/0.3.0.md diff --git a/release/0.3.0.SHA256SUMS.txt b/release/0.3.0.SHA256SUMS.txt new file mode 100644 index 0000000..ea6495d --- /dev/null +++ b/release/0.3.0.SHA256SUMS.txt @@ -0,0 +1,4 @@ +ee0cbaff8e96f82c03e72b292e5e999afe4b4d0486c1494757798273bb1b4b61 external_renderer_ipc.dll +406813a35f504492b59a290a86612a0f235592cc57bbccefa1d8cfdcedf4ff10 libmodavd_extras.dylib +41bae8333146f59f1e5c08c579924a364ead38e74d149e8388cea04834fdac02 libmodavd_extras.so +4837c513ab4eb8a6393d2312163d4c1bffaf17fc1f7099208b1df0856d155b88 modavd_extras.dll diff --git a/release/0.3.0.md b/release/0.3.0.md new file mode 100644 index 0000000..2c7e3f4 --- /dev/null +++ b/release/0.3.0.md @@ -0,0 +1,55 @@ +# ModAVDExtras v0.3.0 + +- `modavd_extras.dll`:`4837c513ab4eb8a6393d2312163d4c1bffaf17fc1f7099208b1df0856d155b88` +- `external_renderer_ipc.dll`:`ee0cbaff8e96f82c03e72b292e5e999afe4b4d0486c1494757798273bb1b4b61` +- `libmodavd_extras.dylib`:`406813a35f504492b59a290a86612a0f235592cc57bbccefa1d8cfdcedf4ff10` +- `libmodavd_extras.so`:`41bae8333146f59f1e5c08c579924a364ead38e74d149e8388cea04834fdac02` + +这一版加上按键和输入文字:`modavd_key`、`modavd_input_text` 能用了,接口版本升到 3。截图、点击和 MuMu 外壳的行为不变。 + +## 新增 + +- 输入文字(`MODAVD_CAP_TEXT`):`modavd_input_text`,UTF-8(#19)。 + - 编辑框有焦点、ModAVD 的宿主输入法(`org.modavd.ime`)在用时,文字原样交给编辑框,任意语言;换行发成回车键。 + - 否则,可打印的 ASCII 由模拟器的键盘打出来,约 1.2 ms 一个字符。 + - 两样都不满足(不是 ASCII,又没有这样的编辑框)时返回 `MODAVD_E_UNSUPPORTED`,什么都不打。 +- 按键(`MODAVD_CAP_KEY`):`modavd_key`,安卓 KEYCODE(#19)。 + - 模拟器的控制台有 `event key` 时,按的是模拟器键盘上的真实按键,按下、抬起分开发,返回、主页、多任务也在内。这条命令要比 ModAVD 0.40 新的模拟器,那时才有 `MODAVD_CAP_KEY`。 + - ModAVD 0.40 的模拟器上:能打出字符的键(字母、数字、回车、Tab、空格、标点)可用;编辑键(退格、方向键等)在编辑框有焦点时可用;其余返回 `MODAVD_E_UNSUPPORTED`。这两条路按下时就把按下和抬起一起发了。 + - Esc 键被模拟器键盘的键位表读作返回。 +- `modavd_api_version()` 返回 3,`modavd_capabilities()` 多了 `MODAVD_CAP_TEXT`(接口版本 3 起恒有)和 `MODAVD_CAP_KEY`(控制台有 `event key` 时才有)。 +- `modavd_version_string()` 返回 `"0.3.0"`。 + +## 说明 + +- `modavd_capabilities()` 第一次调用会连一次模拟器控制台,用来判断有没有 `event key`。 +- 和点击一样不经 adb,客体里不用装东西;库不自己退回 adb,做不了时返回错误码,由调用方换别的办法。 +- 只认接口版本 1、2 的程序不受影响:原有函数和行为都没变。 +- `external_renderer_ipc.dll` 的源码没改,但它静态链接了库的代码,所以文件和 v0.2.0 不一样。 +- Linux、macOS 上按键和文字只过了编译和单元测试,没有在模拟器上实测。 + +## 许可证与源码 + +- LGPL-3.0-or-later。正文是 tag 下的 [LICENSE](https://github.com/ModAVD/ModAVDExtras/blob/v0.3.0/LICENSE)(LGPL-3.0)和 [COPYING](https://github.com/ModAVD/ModAVDExtras/blob/v0.3.0/COPYING)(GPL-3.0);转发这些库时请一起带上。 +- 源码:。 + +## 下载与核对 + +四个文件都由本仓库的 release workflow 在 GitHub 上编出,和 tag 下的 `release/0.3.0.SHA256SUMS.txt` 核对过才上传。编译环境和 v0.2.0 一样: + +- Windows:`windows-2025`,`cmake -A x64`,`--config Release`。两个 DLL 都静态链接 C 运行库,只依赖 `KERNEL32`、`WS2_32`,编译带 `/Brepro`。 +- macOS:`macos-15`(Apple Silicon),`cmake -DCMAKE_BUILD_TYPE=Release`。 +- Linux:`ubuntu-24.04`(x64),`cmake -DCMAKE_BUILD_TYPE=Release`。 + +请用附件 `SHA256SUMS.txt` 核对:Windows 上 `Get-FileHash .\modavd_extras.dll -Algorithm SHA256`,macOS 上 `shasum -a 256 libmodavd_extras.dylib`,Linux 上 `sha256sum libmodavd_extras.so`。 + +## English + +This release adds keys and text: `modavd_key` and `modavd_input_text` now work, and the API version is 3. Capture, touch and the MuMu shim behave as before. + +- Text (`MODAVD_CAP_TEXT`): `modavd_input_text`, UTF-8 (#19). With a focused text field and ModAVD's host input method (`org.modavd.ime`) active, the text is handed to the field as is, in any language, and a newline is sent as ENTER. Otherwise printable ASCII is typed on the emulator's keyboard, about 1.2 ms per character. When neither applies it returns `MODAVD_E_UNSUPPORTED` and types nothing. +- Keys (`MODAVD_CAP_KEY`): `modavd_key`, Android key codes (#19). When the emulator console has `event key`, real keys of the emulator's keyboard are pressed, down and up sent separately, BACK, HOME and APP_SWITCH included; that needs an emulator newer than ModAVD 0.40, and only then is `MODAVD_CAP_KEY` set. On ModAVD 0.40 emulators, keys that type a character work, editing keys (DEL, arrows) work in a focused text field, and the rest return `MODAVD_E_UNSUPPORTED`; on those two paths the press sends down and up together. The emulator keyboard's layout reads Esc as BACK. +- `modavd_api_version()` returns 3; `modavd_capabilities()` adds `MODAVD_CAP_TEXT` (always, from API version 3) and `MODAVD_CAP_KEY` (when the console has `event key`), and its first call opens a console session once. `modavd_version_string()` returns `"0.3.0"`. +- Functions of API versions 1 and 2 are unchanged. `external_renderer_ipc.dll` has no source changes, but it links the library code statically, so the file differs from v0.2.0. On Linux and macOS, keys and text are only built and unit-tested, not tested with an emulator. +- License: LGPL-3.0-or-later; the texts are [LICENSE](https://github.com/ModAVD/ModAVDExtras/blob/v0.3.0/LICENSE) (LGPL-3.0) and [COPYING](https://github.com/ModAVD/ModAVDExtras/blob/v0.3.0/COPYING) (GPL-3.0) at the tag. Ship them with the libraries. Source: . +- All four files are built by this repository's release workflow on GitHub, the same way as v0.2.0, and checked against `release/0.3.0.SHA256SUMS.txt` at the tag before upload. Check them with the attached `SHA256SUMS.txt`. From 801fc68a27dd0f58edae573017943c169da43bbb Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 13:22:03 +0100 Subject: [PATCH 5/5] v0.3.0: editing keys need the host input method --- release/0.3.0.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/release/0.3.0.md b/release/0.3.0.md index 2c7e3f4..8d904e3 100644 --- a/release/0.3.0.md +++ b/release/0.3.0.md @@ -15,7 +15,7 @@ - 两样都不满足(不是 ASCII,又没有这样的编辑框)时返回 `MODAVD_E_UNSUPPORTED`,什么都不打。 - 按键(`MODAVD_CAP_KEY`):`modavd_key`,安卓 KEYCODE(#19)。 - 模拟器的控制台有 `event key` 时,按的是模拟器键盘上的真实按键,按下、抬起分开发,返回、主页、多任务也在内。这条命令要比 ModAVD 0.40 新的模拟器,那时才有 `MODAVD_CAP_KEY`。 - - ModAVD 0.40 的模拟器上:能打出字符的键(字母、数字、回车、Tab、空格、标点)可用;编辑键(退格、方向键等)在编辑框有焦点时可用;其余返回 `MODAVD_E_UNSUPPORTED`。这两条路按下时就把按下和抬起一起发了。 + - ModAVD 0.40 的模拟器上:能打出字符的键(字母、数字、回车、Tab、空格、标点)可用;编辑键(退格、方向键等)在编辑框有焦点、宿主输入法在用时可用;其余返回 `MODAVD_E_UNSUPPORTED`。这两条路按下时就把按下和抬起一起发了。 - Esc 键被模拟器键盘的键位表读作返回。 - `modavd_api_version()` 返回 3,`modavd_capabilities()` 多了 `MODAVD_CAP_TEXT`(接口版本 3 起恒有)和 `MODAVD_CAP_KEY`(控制台有 `event key` 时才有)。 - `modavd_version_string()` 返回 `"0.3.0"`。 @@ -48,7 +48,7 @@ This release adds keys and text: `modavd_key` and `modavd_input_text` now work, and the API version is 3. Capture, touch and the MuMu shim behave as before. - Text (`MODAVD_CAP_TEXT`): `modavd_input_text`, UTF-8 (#19). With a focused text field and ModAVD's host input method (`org.modavd.ime`) active, the text is handed to the field as is, in any language, and a newline is sent as ENTER. Otherwise printable ASCII is typed on the emulator's keyboard, about 1.2 ms per character. When neither applies it returns `MODAVD_E_UNSUPPORTED` and types nothing. -- Keys (`MODAVD_CAP_KEY`): `modavd_key`, Android key codes (#19). When the emulator console has `event key`, real keys of the emulator's keyboard are pressed, down and up sent separately, BACK, HOME and APP_SWITCH included; that needs an emulator newer than ModAVD 0.40, and only then is `MODAVD_CAP_KEY` set. On ModAVD 0.40 emulators, keys that type a character work, editing keys (DEL, arrows) work in a focused text field, and the rest return `MODAVD_E_UNSUPPORTED`; on those two paths the press sends down and up together. The emulator keyboard's layout reads Esc as BACK. +- Keys (`MODAVD_CAP_KEY`): `modavd_key`, Android key codes (#19). When the emulator console has `event key`, real keys of the emulator's keyboard are pressed, down and up sent separately, BACK, HOME and APP_SWITCH included; that needs an emulator newer than ModAVD 0.40, and only then is `MODAVD_CAP_KEY` set. On ModAVD 0.40 emulators, keys that type a character work, editing keys (DEL, arrows) work in a focused text field with the host input method active, and the rest return `MODAVD_E_UNSUPPORTED`; on those two paths the press sends down and up together. The emulator keyboard's layout reads Esc as BACK. - `modavd_api_version()` returns 3; `modavd_capabilities()` adds `MODAVD_CAP_TEXT` (always, from API version 3) and `MODAVD_CAP_KEY` (when the console has `event key`), and its first call opens a console session once. `modavd_version_string()` returns `"0.3.0"`. - Functions of API versions 1 and 2 are unchanged. `external_renderer_ipc.dll` has no source changes, but it links the library code statically, so the file differs from v0.2.0. On Linux and macOS, keys and text are only built and unit-tested, not tested with an emulator. - License: LGPL-3.0-or-later; the texts are [LICENSE](https://github.com/ModAVD/ModAVDExtras/blob/v0.3.0/LICENSE) (LGPL-3.0) and [COPYING](https://github.com/ModAVD/ModAVDExtras/blob/v0.3.0/COPYING) (GPL-3.0) at the tag. Ship them with the libraries. Source: .