Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 上跑;还没有在模拟器上实测。
Expand Down Expand Up @@ -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<port>` on Windows, `videmulator<port>` 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.
Expand Down
33 changes: 27 additions & 6 deletions include/modavd_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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. */
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
Loading
Loading