From e2654e2e6af5115694d957da48d3ad91d80d0ef8 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 03:33:30 +0100 Subject: [PATCH 1/3] =?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?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 文字:编辑框有焦点且宿主输入法在用时走 ime commit(任意语言),否则 ASCII 走 event text,每 4 个字符一条、间隔 4 ms(一次发多了客体键盘队列溢出,整段丢)。按键:控制台有 event key 时发真实按键;没有时只有能打出字符的键和编辑框里的编辑键可用,其余返回 MODAVD_E_UNSUPPORTED。接口版本 3,MODAVD_CAP_TEXT 恒有,MODAVD_CAP_KEY 看控制台有没有 event key。 --- include/modavd_extras.h | 30 +++- src/console.c | 12 +- src/core.c | 8 +- src/input.c | 361 ++++++++++++++++++++++++++++++++++++++-- src/mx_internal.h | 30 ++++ tests/test_core.c | 9 +- tests/test_input.c | 190 ++++++++++++++++++++- 7 files changed, 610 insertions(+), 30 deletions(-) diff --git a/include/modavd_extras.h b/include/modavd_extras.h index c76c11b..3d8dacb 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,28 @@ 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 a + * keyboard works, and BACK, HOME, APP_SWITCH, MENU, POWER and the volume keys. + * Without it, only these do, 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..34fc735 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,343 @@ 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"). + */ + +/* Android key codes that type a character unshifted, and the Linux code of every key we know. */ +static const struct { + short android; + short linux_code; + char ch; +} kKeys[] = { + { 3, 172, 0 }, /* HOME -> KEY_HOMEPAGE */ + { 4, 158, 0 }, /* BACK */ + { 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' }, + { 19, 103, 0 }, /* DPAD_UP */ + { 20, 108, 0 }, /* DPAD_DOWN */ + { 21, 105, 0 }, /* DPAD_LEFT */ + { 22, 106, 0 }, /* DPAD_RIGHT */ + { 23, 353, 0 }, /* DPAD_CENTER -> KEY_SELECT */ + { 24, 115, 0 }, /* VOLUME_UP */ + { 25, 114, 0 }, /* VOLUME_DOWN */ + { 26, 116, 0 }, /* POWER */ + { 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, ' ' }, { 66, 28, '\n' }, + { 67, 14, 0 }, /* DEL (backspace) */ + { 68, 41, '`' }, { 69, 12, '-' }, { 70, 13, '=' }, { 71, 26, '[' }, { 72, 27, ']' }, + { 73, 43, '\\' }, { 74, 39, ';' }, { 75, 40, '\'' }, { 76, 53, '/' }, + { 82, 139, 0 }, /* MENU */ + { 92, 104, 0 }, /* PAGE_UP */ + { 93, 109, 0 }, /* PAGE_DOWN */ + { 111, 1, 0 }, /* ESCAPE */ + { 112, 111, 0 }, /* FORWARD_DEL */ + { 113, 29, 0 }, /* CTRL_LEFT */ + { 114, 97, 0 }, /* CTRL_RIGHT */ + { 122, 102, 0 }, /* MOVE_HOME */ + { 123, 107, 0 }, /* MOVE_END */ + { 131, 59, 0 }, { 132, 60, 0 }, { 133, 61, 0 }, { 134, 62, 0 }, { 135, 63, 0 }, /* F1 - F5 */ + { 136, 64, 0 }, { 137, 65, 0 }, { 138, 66, 0 }, { 139, 67, 0 }, { 140, 68, 0 }, /* F6 - F10 */ + { 141, 87, 0 }, { 142, 88, 0 }, /* F11, F12 */ + { 164, 113, 0 }, /* VOLUME_MUTE */ + { 187, 580, 0 }, /* APP_SWITCH -> KEY_APPSELECT */ +}; + +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) { + int code = mx_key_linux_code(android_keycode); + if (!code) { + rc = MODAVD_E_UNSUPPORTED; + } + else { + snprintf(cmd, sizeof(cmd), "event key %d %d", code, down ? 1 : 0); + rc = send_locked(in, cmd); + } + } + else { + /* An emulator without "event key": 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..f814d43 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,173 @@ 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), 172); /* HOME */ + 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(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 +542,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 823e6195c48591ae0c7141101715bcd474bf731d Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 04:01:02 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8C=89=E9=94=AE=E8=A1=A8=E7=85=A7?= =?UTF-8?q?=E6=A8=A1=E6=8B=9F=E5=99=A8=E9=94=AE=E7=9B=98=E7=9A=84=20qwerty?= =?UTF-8?q?.kl=EF=BC=9A=E4=B8=BB=E9=A1=B5=E9=94=AE=E6=98=AF=20102=EF=BC=8C?= =?UTF-8?q?Esc=20=E9=94=AE=E8=AF=BB=E4=BD=9C=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 模拟器的键盘在客体里用 /vendor/usr/keylayout/qwerty.kl,不是 Generic.kl。键盘上没有的键(FORWARD_DEL、MOVE_HOME 等)照没有 event key 时的办法走。带 event key 的模拟器上实测:返回、主页、多任务、Esc、退格、回车。 --- include/modavd_extras.h | 9 ++++-- src/input.c | 62 ++++++++++++++++++++++------------------- tests/test_input.c | 4 ++- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/include/modavd_extras.h b/include/modavd_extras.h index 3d8dacb..e4cbb90 100644 --- a/include/modavd_extras.h +++ b/include/modavd_extras.h @@ -184,9 +184,12 @@ MODAVD_API int modavd_touch_release_all(modavd_handle h); * 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 a - * keyboard works, and BACK, HOME, APP_SWITCH, MENU, POWER and the volume keys. - * Without it, only these do, and the press sends both press and release: + * 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. diff --git a/src/input.c b/src/input.c index 34fc735..29cdf6b 100644 --- a/src/input.c +++ b/src/input.c @@ -267,24 +267,33 @@ int modavd_touch_release_all(modavd_handle h) * current one and a text field to have the focus ("ime status"). */ -/* Android key codes that type a character unshifted, and the Linux code of every key we know. */ +/* 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[] = { - { 3, 172, 0 }, /* HOME -> KEY_HOMEPAGE */ + { 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, 353, 0 }, /* DPAD_CENTER -> KEY_SELECT */ + { 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' }, @@ -296,24 +305,27 @@ static const struct { { 58, 100, 0 }, /* ALT_RIGHT */ { 59, 42, 0 }, /* SHIFT_LEFT */ { 60, 54, 0 }, /* SHIFT_RIGHT */ - { 61, 15, '\t' }, { 62, 57, ' ' }, { 66, 28, '\n' }, + { 61, 15, '\t' }, { 62, 57, ' ' }, + { 64, 150, 0 }, /* EXPLORER */ + { 65, 155, 0 }, /* ENVELOPE */ + { 66, 28, '\n' }, { 67, 14, 0 }, /* DEL (backspace) */ - { 68, 41, '`' }, { 69, 12, '-' }, { 70, 13, '=' }, { 71, 26, '[' }, { 72, 27, ']' }, + { 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 */ - { 92, 104, 0 }, /* PAGE_UP */ - { 93, 109, 0 }, /* PAGE_DOWN */ - { 111, 1, 0 }, /* ESCAPE */ - { 112, 111, 0 }, /* FORWARD_DEL */ + { 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 */ - { 122, 102, 0 }, /* MOVE_HOME */ - { 123, 107, 0 }, /* MOVE_END */ - { 131, 59, 0 }, { 132, 60, 0 }, { 133, 61, 0 }, { 134, 62, 0 }, { 135, 63, 0 }, /* F1 - F5 */ - { 136, 64, 0 }, { 137, 65, 0 }, { 138, 66, 0 }, { 139, 67, 0 }, { 140, 68, 0 }, /* F6 - F10 */ - { 141, 87, 0 }, { 142, 88, 0 }, /* F11, F12 */ - { 164, 113, 0 }, /* VOLUME_MUTE */ - { 187, 580, 0 }, /* APP_SWITCH -> KEY_APPSELECT */ + { 115, 58, 0 }, /* CAPS_LOCK */ + { 187, 580, 0 }, /* APP_SWITCH */ + { 223, 142, 0 }, /* SLEEP */ }; int mx_key_linux_code(int android_keycode) @@ -484,20 +496,14 @@ int modavd_key(modavd_handle h, int display, int android_keycode, int down) if (has < 0) { rc = has; } - else if (has) { - int code = mx_key_linux_code(android_keycode); - if (!code) { - rc = MODAVD_E_UNSUPPORTED; - } - else { - snprintf(cmd, sizeof(cmd), "event key %d %d", code, down ? 1 : 0); - rc = send_locked(in, cmd); - } + 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 { - /* An emulator without "event key": the press types the key's character - * or goes to the focused text field as a whole; the release has - * nothing left to do. */ + /* 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' }; diff --git a/tests/test_input.c b/tests/test_input.c index f814d43..3dd1bd0 100644 --- a/tests/test_input.c +++ b/tests/test_input.c @@ -370,10 +370,12 @@ static void test_touch_session(void) static void test_key_and_text_commands(void) { CHECK_EQ(mx_key_linux_code(4), 158); /* BACK */ - CHECK_EQ(mx_key_linux_code(3), 172); /* HOME */ + 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'); From e8820aaaa9dbe91de5fa6b7a8e1f535d47658718 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Sat, 14 Nov 2026 04:22:06 +0100 Subject: [PATCH 3/3] =?UTF-8?q?README=EF=BC=9A=E6=8C=89=E9=94=AE=E5=92=8C?= =?UTF-8?q?=E6=96=87=E5=AD=97=EF=BC=88=E6=8E=A5=E5=8F=A3=E7=89=88=E6=9C=AC?= =?UTF-8?q?=203=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 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.