diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..1a8def0 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,18 @@ +# Code owners list only the high-risk paths: signing, the release, and the checks themselves. GitHub asks the owner +# to review a pull request that touches one of them. Everything else is merged once CI is green. How pull requests +# are reviewed and merged: https://github.com/ModAVD/ModAVD/blob/main/CONTRIBUTING.en.md +# 只列高风险路径:动了这些路径的 PR,GitHub 会自动请负责人审查;其余的 CI 绿了就能合。 +# 流程见 https://github.com/ModAVD/ModAVD/blob/main/CONTRIBUTING.md + +# Workflows and their permissions, Dependabot, this file +/.github/ @qiyinxi + +# The CI gate, the privacy scan and its allowlist +/scripts/repo_checks.py @qiyinxi +/scripts/privacy.py @qiyinxi +/scripts/privacy-allowlist.json @qiyinxi + +# Signing and release +/scripts/release-local.ps1 @qiyinxi +/scripts/verify_release_apk.py @qiyinxi +/release/ @qiyinxi diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..169e83c --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,28 @@ + + +## 改了什么 / What changed + + + +## 怎么验证的 / How it was verified + + + + + +## 核对 / Checklist + +- [ ] 用户看得见的改动带了 `changelog.d/<名字>..md`(只动开发、测试、CI、文档的不用) / A change users can see comes with `changelog.d/..md` (not needed for development, tests, CI or docs) +- [ ] 中英成对的文档(`X.md`、`X.en.md`)两份都改了;只会一种语言的在上面说一声 / Both files of a Chinese / English pair (`X.md`, `X.en.md`) are changed; if you write only one of the languages, say so above + + +Refs ModAVD/ModAVD# diff --git a/README.en.md b/README.en.md index b4b9f24..aa0b8c4 100644 --- a/README.en.md +++ b/README.en.md @@ -93,7 +93,7 @@ The build pins Temurin 25.0.4.1+1, build-tools 36.1.0 and android-36 (it checks | `ime/` | The product APK: `HostImeService` (the input method service), `HostLink` (the connection thread and the write thread), `HostProtocol` (frame encoding and decoding) | | `ime/test/ProtocolTest.java` | The protocol self-test on the host JVM, with the expected values hand-written in hex from the spec; `build.ps1` runs it before packaging, and CI runs it too | | `testapp/` | The test APK `org.modavd.ime.testapp`, not distributed with the product | -| `docs/protocol-v1.md` | Channel protocol v1 | +| `docs/protocol-v1.en.md` (English), `docs/protocol-v1.md` (Chinese) | Channel protocol v1 | | `build.ps1` | The build script (Windows, pwsh 7.2 or newer) | | `scripts/` | The CI privacy scan and author check, and the release and verification scripts | | `release/` | The release notes of each version; v1.0.0 also has a pre-committed `SHA256SUMS.txt` | @@ -193,7 +193,7 @@ adb -s emulator- shell setprop log.tag.ModAVDIme DEBUG ## Protocol -The spec is in [docs/protocol-v1.md](docs/protocol-v1.md) (in Chinese). The host side is implemented in the emulator patches of [ModAVD/ModAVD](https://github.com/ModAVD/ModAVD): +The spec is in [docs/protocol-v1.en.md](docs/protocol-v1.en.md). The host side is implemented in the emulator patches of [ModAVD/ModAVD](https://github.com/ModAVD/ModAVD): three patches under `patches/qemu/mas/` (N ≥ 35) whose file names contain `modavd_ime`, `console-command-ime` and `host-input-method` (in 0.35.0 these are 0058–0060 in `patches/qemu/mas35/`). The key points: diff --git a/README.md b/README.md index 666c649..b876d05 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ CI(`ci.yml`)只在 Pull Request 和手动触发时跑:协议自测、在 W | `ime/` | 产品 APK:`HostImeService`(输入法服务)、`HostLink`(连接线程和写线程)、`HostProtocol`(帧编解码) | | `ime/test/ProtocolTest.java` | 宿主 JVM 上的协议自测,期望值照规格手写成十六进制;`build.ps1` 打包前跑,CI 也跑 | | `testapp/` | 测试 APK `org.modavd.ime.testapp`,不随产品分发 | -| `docs/protocol-v1.md` | 通道协议 v1 | +| `docs/protocol-v1.md`(中文)、`docs/protocol-v1.en.md`(英文) | 通道协议 v1 | | `build.ps1` | 构建脚本(Windows,pwsh 7.2 以上) | | `scripts/` | CI 的隐私扫描、作者检查,发版和核对脚本 | | `release/` | 各版本的发布说明;v1.0.0 还有预先提交的 `SHA256SUMS.txt` | diff --git a/changelog.d/README.md b/changelog.d/README.md index f58ac25..822c5cc 100644 --- a/changelog.d/README.md +++ b/changelog.d/README.md @@ -4,7 +4,7 @@ - **文件名**:`<名字>.<类型>.md`。名字用小写字母、数字、`-`、`_`,起一个不会和别的改动重复的(比如 issue 号加几个词:`210-resize-crash.fix.md`)。 - **类型**:`feat` 新增,`fix` 修复,`change` 变更,`remove` 移除。 -- **内容**:只有一行,不超过 60 字,不能有 `--`。写用户会看到的结果,不写原因和实现,比如「调高分辨率后开机不再卡死退出」。 +- **内容**:只有一行,不超过 60 字,不能有 `--`。写用户会看到的结果,不写原因和实现,比如「调高分辨率后开机不再卡死退出」。发版公告是中文的,这一行写中文;不会写中文就用英文写,在 PR 里说一声,由维护者在合并前改成中文。 - 想让公告指向 issue 而不是 PR,就自己在行尾写上,issue 都在 ModAVD 仓库,要写全称:`……不再卡死退出 (ModAVD/ModAVD#210)`;这样工具不再补 PR 编号。 - 只和开发、测试、CI、文档有关的 PR 不用加。 - 已经发过版的文件不要改内容、也不要重复用它的名字;以后的改动另起一个文件。 @@ -17,7 +17,7 @@ A change that users can see adds one file here, in the same pull request, with a - **Name**: `..md`, the name in lowercase letters, digits, `-`, `_`, and unique (for example the issue number and a few words: `210-resize-crash.fix.md`). - **Type**: `feat` (新增), `fix` (修复), `change` (变更), `remove` (移除). -- **Content**: one line in Chinese, at most 60 characters, without `--`. Say what users will notice, not the cause or the implementation. +- **Content**: one line, at most 60 characters, without `--`. Say what users will notice, not the cause or the implementation, for example "Starting no longer hangs after raising the resolution". The release notes are in Chinese, so the line is in Chinese; if you do not write Chinese, write it in English and say so in the pull request, and the maintainer will put it into Chinese before merging. - To point at an issue instead of the pull request, end the line with it yourself, in full: `…… (ModAVD/ModAVD#210)`. - Pull requests that only touch development, tests, CI or documentation need no file. - Do not edit or reuse a file that has already been released; a later change takes a new file. diff --git a/docs/protocol-v1.en.md b/docs/protocol-v1.en.md new file mode 100644 index 0000000..c30c6eb --- /dev/null +++ b/docs/protocol-v1.en.md @@ -0,0 +1,134 @@ +# Host IME channel protocol v1 + +[简体中文](protocol-v1.md) | **English** + +The channel between the ModAVD emulator (from 0.35.0) and the input method `org.modavd.ime` in the guest. Both sides must agree byte for byte. +In this repository, `ime/src/org/modavd/ime/HostProtocol.java` is the guest-side encoder and decoder, and `ime/test/ProtocolTest.java` has the expected bytes written by hand from this document. + +## What each side does + +- **Guest** (the input method in this repository): tells the host the current input focus (whether it is an editable text field, `inputType`, `imeOptions`) and where the cursor is on the screen; + hands the text and editing keys sent by the host to the current edit field through `InputConnection`. +- **Host** (the emulator): when the guest reports that the focus is on a text field, the emulator window enters "text mode" so the host system's IME works in the window. + The typed text, the composing text and the editing keys are sent to the guest through this channel. Outside text mode, the keyboard, mouse and shortcuts work exactly as before. + The `ime` command of the emulator console also uses this channel, for automation (see the end of this document). + +## Connection + +- The channel is the goldfish pipe service `modavd_ime`. +- The guest opens `/dev/goldfish_pipe_dprctd` (read and write), writes the ASCII string `pipe:modavd_ime` followed by one `\0` byte, and then sends and receives frames both ways on the same fd. + - On the Android 14 (API 34) google_apis x86_64 image, this device node is `crw-rw-rw- system system u:object_r:qemu_device:s0`. An ordinary app can open it and needs no permission. + - The service name and the first frame are sent in two separate `write` calls: the host's connector reads only up to the `\0`. +- The first frame the guest sends after connecting must be `V` (handshake). The host replies with a `V`. +- Only the newest connection is valid at any time: a new connection makes the host close the old one. +- The channel is not part of snapshots: loading a snapshot drops it, and the guest reconnects as usual. +- On an emulator without this service (before 0.35.0), opening the device or writing the service name fails, and the guest reconnects with backoff. See "Guest-side conventions" below. + +## Frame format + +``` ++--------------------+-----------------------------+ +| length N (u32, LE) | payload (N bytes) | ++--------------------+-----------------------------+ + | type (1 ASCII letter) | fields ... | +``` + +- A frame is a 4-byte little-endian unsigned length N (counting only the payload, 1 ≤ N ≤ 65536) followed by N bytes of payload. +- The first byte of the payload is the type (an ASCII letter). The rest is parsed according to the type. +- All integers are little-endian: u8 and u32 are unsigned, i32 is signed (two's complement). +- Text is always UTF-8, has no trailing `\0`, and fills the rest of the payload. +- If either side receives a frame with a length out of range, an unknown type or missing fields, it drops this connection (the host closes the pipe; the guest closes the fd and reconnects with backoff). It must not crash or read or write out of bounds. +- The guest implementation in this repository ignores any extra bytes after the fixed-length fields (`V`, `E`, `K`, `D`). + +## Guest → host + +| Type | Fields | Meaning | +| --- | --- | --- | +| `V` | u32 version = 1 | Handshake. It must be the first frame after connecting. | +| `F` | u8 active, u32 inputType, u32 imeOptions | Input focus. active = 1 means the current focus is an editable text field. inputType and imeOptions are the raw values of `EditorInfo`. | +| `R` | i32 left, i32 top, i32 right, i32 bottom | Cursor position, in physical pixels of the guest's display 0 (screen coordinates, already multiplied by the matrix of `CursorAnchorInfo`). Not sent when it is not available. | + +- `F`: sent on every `onStartInput` / `onFinishInput`. After a successful reconnect, the current state is sent again right away. +- `F` and `R` are sent only by the guest. An `F` or `R` sent by the host is an unknown type to the guest. + +## Host → guest + +| Type | Fields | Guest action | +| --- | --- | --- | +| `V` | u32 version | Handshake reply (a v1 host replies with 1). The guest in this repository treats version 0 as a protocol error. | +| `C` | text | Commit: `commitText(text, 1)`. If there is a composing region, it is replaced. This is the Android semantics. | +| `P` | i32 cursor, text | Composing: `setComposingText(text, 1)`. Empty text = cancel composing. The cursor is a UTF-16 index in the composing string, -1 = the end. A v1 guest may ignore it and always use the end. | +| `E` | none | End composing: `finishComposingText()`. | +| `K` | u32 keycode, u32 metaState, u8 action | Key: `InputConnection.sendKeyEvent`. keycode is the Android `KeyEvent.KEYCODE_*`, metaState holds `KeyEvent.META_*` bits. action 0 = down, 1 = up, 2 = down then up (with the same downTime). | +| `D` | i32 before, i32 after | Delete text before and after the cursor (in UTF-16 units): `deleteSurroundingText(before, after)`. | + +- The `KeyEvent` built for `K` has source = `InputDevice.SOURCE_KEYBOARD`, deviceId = `KeyCharacterMap.VIRTUAL_KEYBOARD`, + and flags = `FLAG_SOFT_KEYBOARD | FLAG_KEEP_TOUCH_MODE`. +- When the action of `K` is not 0, 1 or 2, the guest in this repository ignores the frame (it does not disconnect). It does the same when before or after of `D` is negative. + +## Examples (hex) + +| Frame | Bytes | +| --- | --- | +| Service name | `70 69 70 65 3a 6d 6f 64 61 76 64 5f 69 6d 65 00` | +| Guest `V` 1 | `05 00 00 00 56 01 00 00 00` | +| Guest `F` active=1, inputType=0x00020001, imeOptions=0x12000006 | `0a 00 00 00 46 01 01 00 02 00 06 00 00 12` | +| Guest `R` (-1, 2, 0x01020304, -2147483648) | `11 00 00 00 52 ff ff ff ff 02 00 00 00 04 03 02 01 00 00 00 80` | +| Host `C` "你好a" | `08 00 00 00 43 e4 bd a0 e5 a5 bd 61` | +| Host `P` cursor -1, "ab" | `07 00 00 00 50 ff ff ff ff 61 62` | +| Host `K` KEYCODE_A(29), META_CTRL_ON\|META_SHIFT_ON(0x1001), action 2 | `0a 00 00 00 4b 1d 00 00 00 01 10 00 00 02` | +| Host `D` before=2, after=3 | `09 00 00 00 44 02 00 00 00 03 00 00 00` | + +For more positive and negative cases (length out of range, missing fields, unknown type), see `ime/test/ProtocolTest.java`. + +## Extension rules + +Later versions only add new types and never change the format of existing ones. + +## Guest-side conventions + +The input method in this repository follows these rules. If you write another guest implementation, we suggest you follow them too. + +- **It shows no input UI**: `onEvaluateInputViewShown()` and `onEvaluateFullscreenMode()` both return false. +- **It does not override `onKeyDown` / `onKeyUp` / `onKeyMultiple` / `onKeyLongPress`**: physical keys and keys injected by `adb shell input` still go straight to the app. Games and automation rely on this. +- **Connection thread**: a background daemon thread loops: open the device, write the service name, send `V`, then block reading frames. On failure or disconnect it closes the fd and reconnects with backoff at 1 s, 2 s, 4 s... capped at 30 s. + After connecting, it sends the current `F` right away (and `R` if there is one). It stops the thread when the service is destroyed. + When the host has no such service (an old emulator), it does not crash or busy-wait, and it does not flood the log. +- **Writing**: a separate single-thread executor writes in order, so the main thread never blocks. When not connected, writes are dropped, and the resend after connecting brings the host up to date. +- **Reading**: a frame read from the host is switched to the main thread and run on `getCurrentInputConnection()`. If it is null, the frame is dropped and one log line is written. +- **Focus**: in `onStartInput(EditorInfo, restarting)`, active = `(inputType & TYPE_MASK_CLASS) != TYPE_NULL`, and it sends `F`. + When active, it calls `ic.requestCursorUpdates(CURSOR_UPDATE_IMMEDIATE | CURSOR_UPDATE_MONITOR)`. `onFinishInput` sends `F` with active=0. +- **Cursor**: in `onUpdateCursorAnchorInfo`, it prefers the bounding box of the last character of the composing region, and otherwise uses the insertion marker (horizontal, top, bottom, width 1). + It maps this to screen coordinates with `info.getMatrix()` and sends `R`, and does not send it when it is the same as the last one. When the edit field changes, the old cursor position is discarded. +- **Log**: normal operation does not flood the log, and each key press is not logged. Frames sent and received are logged only when debug logging is on. The text of `C` / `P` is not logged (it may be a password), only its length. + +## Host-side behavior (reference) + +The ModAVD emulator's implementation is the reference. Only the parts the guest needs to know are listed here. + +- **Text mode**: entered when the guest is connected and the latest `F` has active=1. Left when `F` has active=0 or the pipe drops. When it is left, any unfinished composing is discarded. +- In text mode, the keys in the window are handled like this: + 1. Keys that produce printable text (including AltGr combinations and characters composed from dead keys) → `C`; + 2. Editing keys → `K` action=2: Backspace→`KEYCODE_DEL`(67), Delete→`KEYCODE_FORWARD_DEL`(112), Enter→`KEYCODE_ENTER`(66), + Tab→`KEYCODE_TAB`(61), arrow keys→`KEYCODE_DPAD_LEFT/UP/RIGHT/DOWN`(21/19/22/20), Home→`KEYCODE_MOVE_HOME`(122), + End→`KEYCODE_MOVE_END`(123), PageUp→`KEYCODE_PAGE_UP`(92), PageDown→`KEYCODE_PAGE_DOWN`(93); + Ctrl (with Shift allowed, without Alt) + letter A–Z → `KEYCODE_A`(29)…`KEYCODE_Z`(54). metaState is filled from the Shift / Ctrl / Alt state at that moment; + 3. All other keys (Esc, F1–F12, a modifier key pressed alone, Alt+letter and so on) still take the original key path, and the guest receives them as ordinary key events. +- Composing of the host IME: the composing string → `P` (an empty string means cancel), the confirmed text → `C`, and the end of composing → `E`. +- The host uses `inputType` to give hints to the host IME: for password types it turns off suggestions, and for number types it prefers digits. It uses `R` to place the candidate window of the host IME. Before any `R` is received, it puts the window at the bottom left of the display area. + +## Console command `ime` + +The `ime` command of the emulator console (`adb -s emulator- emu `, or telnet to the console port) is for automated tests. You can also use it to type in any language: + +| Command | Effect | +| --- | --- | +| `ime status` | Prints the state: connected, version, textmode, inputType, imeOptions, cursor (none if there is no cursor) and so on | +| `ime commit ` | Sends `C`. `` is the whole rest of the line after the first space following the subcommand (raw UTF-8, spaces included) | +| `ime preedit ` | Sends `P` with cursor -1. The text is taken the same way, and empty text cancels composing | +| `ime finish` | Sends `E` | +| `ime key [metaState]` | Sends `K` with action=2 | +| `ime delete ` | Sends `D` | + +When the text of `commit` or `preedit` is longer than one frame can hold, the host splits it into several frames, cutting between UTF-8 characters. +When the guest IME is not connected, these commands return `KO` with a reason. Example: `adb -s emulator-5554 emu ime commit 你好`. diff --git a/docs/protocol-v1.md b/docs/protocol-v1.md index 7e8a31e..2fd532b 100644 --- a/docs/protocol-v1.md +++ b/docs/protocol-v1.md @@ -1,5 +1,7 @@ # 宿主输入法通道协议 v1 +**简体中文** | [English](protocol-v1.en.md) + ModAVD 模拟器(0.35.0 起)和客体里的输入法 `org.modavd.ime` 之间的通道。两边必须逐字节一致; 本仓库的 `ime/src/org/modavd/ime/HostProtocol.java` 是客体侧的编解码,`ime/test/ProtocolTest.java` 按本文手写了期望字节。